Home | History | Annotate | Line # | Download | only in booke
e500_timer.c revision 1.4
      1  1.4  matt /*	$NetBSD: e500_timer.c,v 1.4 2011/06/29 05:56:31 matt Exp $	*/
      2  1.2  matt /*-
      3  1.2  matt  * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
      4  1.2  matt  * All rights reserved.
      5  1.2  matt  *
      6  1.2  matt  * This code is derived from software contributed to The NetBSD Foundation
      7  1.2  matt  * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects
      8  1.2  matt  * Agency and which was developed by Matt Thomas of 3am Software Foundry.
      9  1.2  matt  *
     10  1.2  matt  * This material is based upon work supported by the Defense Advanced Research
     11  1.2  matt  * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under
     12  1.2  matt  * Contract No. N66001-09-C-2073.
     13  1.2  matt  * Approved for Public Release, Distribution Unlimited
     14  1.2  matt  *
     15  1.2  matt  * Redistribution and use in source and binary forms, with or without
     16  1.2  matt  * modification, are permitted provided that the following conditions
     17  1.2  matt  * are met:
     18  1.2  matt  * 1. Redistributions of source code must retain the above copyright
     19  1.2  matt  *    notice, this list of conditions and the following disclaimer.
     20  1.2  matt  * 2. Redistributions in binary form must reproduce the above copyright
     21  1.2  matt  *    notice, this list of conditions and the following disclaimer in the
     22  1.2  matt  *    documentation and/or other materials provided with the distribution.
     23  1.2  matt  *
     24  1.2  matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     25  1.2  matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26  1.2  matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     27  1.2  matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     28  1.2  matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29  1.2  matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30  1.2  matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     31  1.2  matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     32  1.2  matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     33  1.2  matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     34  1.2  matt  * POSSIBILITY OF SUCH DAMAGE.
     35  1.2  matt  */
     36  1.2  matt 
     37  1.2  matt #include <sys/cdefs.h>
     38  1.4  matt __KERNEL_RCSID(0, "$NetBSD: e500_timer.c,v 1.4 2011/06/29 05:56:31 matt Exp $");
     39  1.2  matt 
     40  1.2  matt #include <sys/param.h>
     41  1.2  matt #include <sys/kernel.h>
     42  1.2  matt #include <sys/systm.h>
     43  1.2  matt #include <sys/timetc.h>
     44  1.2  matt #include <sys/intr.h>
     45  1.2  matt #include <sys/cpu.h>
     46  1.2  matt 
     47  1.2  matt #include <uvm/uvm_extern.h>
     48  1.2  matt 
     49  1.2  matt #include <powerpc/spr.h>
     50  1.2  matt #include <powerpc/booke/spr.h>
     51  1.2  matt #include <powerpc/booke/cpuvar.h>
     52  1.2  matt #include <powerpc/booke/e500reg.h>
     53  1.2  matt #include <powerpc/booke/e500var.h>
     54  1.2  matt #include <powerpc/booke/openpicreg.h>
     55  1.2  matt 
     56  1.2  matt static u_long ns_per_tick;
     57  1.2  matt 
     58  1.2  matt static void init_ppcbooke_tc(void);
     59  1.2  matt static u_int get_ppcbooke_timecount(struct timecounter *);
     60  1.2  matt 
     61  1.2  matt static struct timecounter ppcbooke_timecounter = {
     62  1.2  matt 	get_ppcbooke_timecount,	/* get_timecount */
     63  1.2  matt 	0,			/* no poll_pps */
     64  1.2  matt 	~0u,			/* counter_mask */
     65  1.2  matt 	0,			/* frequency */
     66  1.2  matt 	"ppc_timebase",		/* name */
     67  1.2  matt 	100,			/* quality */
     68  1.2  matt 	NULL,			/* tc_priv */
     69  1.2  matt 	NULL			/* tc_next */
     70  1.2  matt };
     71  1.2  matt 
     72  1.2  matt static inline uint32_t
     73  1.2  matt openpic_read(struct cpu_softc *cpu, bus_size_t offset)
     74  1.2  matt {
     75  1.2  matt 
     76  1.2  matt 	return bus_space_read_4(cpu->cpu_bst, cpu->cpu_bsh,
     77  1.2  matt 	    OPENPIC_BASE + offset);
     78  1.2  matt }
     79  1.2  matt 
     80  1.2  matt static inline void
     81  1.2  matt openpic_write(struct cpu_softc *cpu, bus_size_t offset, uint32_t val)
     82  1.2  matt {
     83  1.2  matt 
     84  1.2  matt 	return bus_space_write_4(cpu->cpu_bst, cpu->cpu_bsh,
     85  1.2  matt 	    OPENPIC_BASE + offset, val);
     86  1.2  matt }
     87  1.2  matt 
     88  1.2  matt int
     89  1.2  matt e500_clock_intr(void *v)
     90  1.2  matt {
     91  1.2  matt 	struct trapframe * const tf = v;
     92  1.2  matt 	struct cpu_info * const ci = curcpu();
     93  1.2  matt 	struct cpu_softc * const cpu = ci->ci_softc;
     94  1.2  matt 	u_int nticks;
     95  1.2  matt 
     96  1.2  matt 	/*
     97  1.2  matt 	 * Check whether we are initialized.
     98  1.2  matt 	 */
     99  1.2  matt 	if (!cpu->cpu_ticks_per_clock_intr)
    100  1.2  matt 		return 0;
    101  1.2  matt 
    102  1.2  matt 	/*
    103  1.2  matt 	 * Now let's how delayed the clock interrupt was.  Obviously it must
    104  1.2  matt 	 * at least one clock tick since the clock interrupt.  But it might
    105  1.2  matt 	 * be more if interrupts were blocked for a long time.  We keep
    106  1.2  matt 	 * suubtracting an interrupts We should be
    107  1.2  matt 	 * [well] within a single tick.
    108  1.2  matt 	 * We add back one tick (which should put us back above 0).  If we
    109  1.2  matt 	 * are still below 0, keep adding ticks until we are above 0.
    110  1.2  matt 	 */
    111  1.2  matt 	const uint64_t now = mftb();
    112  1.2  matt 	uint64_t latency = now - (ci->ci_lastintr + cpu->cpu_ticks_per_clock_intr);
    113  1.2  matt #if 0
    114  1.2  matt 	uint64_t orig_latency = latency;
    115  1.2  matt #endif
    116  1.2  matt 	if (now < ci->ci_lastintr + cpu->cpu_ticks_per_clock_intr)
    117  1.2  matt 		latency = 0;
    118  1.2  matt 
    119  1.2  matt 	nticks = 1 + latency / cpu->cpu_ticks_per_clock_intr;
    120  1.2  matt 	latency %= cpu->cpu_ticks_per_clock_intr;
    121  1.2  matt #if 0
    122  1.2  matt 	for (nticks = 1; latency >= cpu->cpu_ticks_per_clock_intr; nticks++)  {
    123  1.2  matt 		latency -= cpu->cpu_ticks_per_clock_intr;
    124  1.2  matt 	}
    125  1.2  matt #endif
    126  1.2  matt 
    127  1.2  matt 	ci->ci_ev_clock.ev_count++;
    128  1.2  matt 	cpu->cpu_ev_late_clock.ev_count += nticks - 1;
    129  1.2  matt 
    130  1.2  matt 	/*
    131  1.2  matt 	 * lasttb is used during microtime. Set it to the virtual
    132  1.2  matt 	 * start of this tick interval.
    133  1.2  matt 	 */
    134  1.2  matt #if 0
    135  1.2  matt 	if (nticks > 10 || now - ci->ci_lastintr < 7 * cpu->cpu_ticks_per_clock_intr / 8)
    136  1.2  matt 	printf("%s: nticks=%u lastintr=%#"PRIx64"(%#"PRIx64") now=%#"PRIx64" latency=%#"PRIx64" orig=%#"PRIx64"\n", __func__,
    137  1.2  matt 	    nticks, ci->ci_lastintr, now - latency, now, latency, orig_latency);
    138  1.2  matt #endif
    139  1.2  matt 	ci->ci_lastintr = now - latency;
    140  1.2  matt 	ci->ci_lasttb = now;
    141  1.2  matt 
    142  1.2  matt 	wrtee(PSL_EE);	/* Reenable interrupts */
    143  1.2  matt 
    144  1.2  matt 	/*
    145  1.2  matt 	 * Do standard timer interrupt stuff.
    146  1.2  matt 	 */
    147  1.2  matt 	while (nticks-- > 0) {
    148  1.2  matt 		hardclock(&tf->tf_cf);
    149  1.2  matt 	}
    150  1.2  matt 
    151  1.2  matt 	wrtee(0);	/* turn off interrupts */
    152  1.2  matt 
    153  1.3  matt 	tf->tf_srr1 &= ~PSL_POW;	/* make cpu_idle exit */
    154  1.3  matt 
    155  1.2  matt 	return 1;
    156  1.2  matt }
    157  1.2  matt 
    158  1.2  matt void
    159  1.2  matt cpu_initclocks(void)
    160  1.2  matt {
    161  1.2  matt 	struct cpu_info * const ci = curcpu();
    162  1.2  matt 	struct cpu_softc * const cpu = ci->ci_softc;
    163  1.2  matt 
    164  1.2  matt 	cpu->cpu_ticks_per_clock_intr = (ci->ci_data.cpu_cc_freq + hz/2 - 1) / hz;
    165  1.2  matt 
    166  1.2  matt 	/* interrupt established in e500_intr_cpu_init */
    167  1.2  matt 
    168  1.2  matt 	ci->ci_lastintr = ci->ci_lasttb = mftb();
    169  1.2  matt 	openpic_write(cpu, cpu->cpu_clock_gtbcr,
    170  1.2  matt 	     GTBCR_CI | cpu->cpu_ticks_per_clock_intr);
    171  1.2  matt 	openpic_write(cpu, cpu->cpu_clock_gtbcr,
    172  1.2  matt 	     cpu->cpu_ticks_per_clock_intr);
    173  1.2  matt 
    174  1.4  matt 	if (CPU_IS_PRIMARY(ci))
    175  1.4  matt 		init_ppcbooke_tc();
    176  1.2  matt }
    177  1.2  matt 
    178  1.2  matt void
    179  1.2  matt calc_delayconst(void)
    180  1.2  matt {
    181  1.2  matt 	struct cpu_info * const ci = curcpu();
    182  1.2  matt 
    183  1.2  matt 	ci->ci_data.cpu_cc_freq = board_info_get_number("timebase-frequency");
    184  1.2  matt 	ns_per_tick = 1000000000 / (u_int)ci->ci_data.cpu_cc_freq;
    185  1.2  matt }
    186  1.2  matt 
    187  1.2  matt static u_int
    188  1.2  matt get_ppcbooke_timecount(struct timecounter *tc)
    189  1.2  matt {
    190  1.2  matt 	return mftbl();
    191  1.2  matt }
    192  1.2  matt 
    193  1.2  matt /*
    194  1.2  matt  * Wait for about n microseconds (at least!).
    195  1.2  matt  */
    196  1.2  matt void
    197  1.2  matt delay(unsigned int n)
    198  1.2  matt {
    199  1.2  matt 	uint64_t tb;
    200  1.2  matt 	u_long tbh, tbl, scratch;
    201  1.2  matt 
    202  1.2  matt 	tb = mftb();
    203  1.2  matt 	/* use 1000ULL to force 64 bit math to avoid 32 bit overflows */
    204  1.2  matt 	tb += (n * 1000ULL + ns_per_tick - 1) / ns_per_tick;
    205  1.2  matt 	tbh = tb >> 32;
    206  1.2  matt 	tbl = tb;
    207  1.2  matt 	__asm volatile (
    208  1.2  matt 		"1:	mfspr	%0,%4"	"\n"
    209  1.2  matt 		"	cmplw	%0,%1"	"\n"
    210  1.2  matt 		"	blt	1b"	"\n"
    211  1.2  matt 		"	bgt	2f"	"\n"
    212  1.2  matt 		"	mfspr	%0,%3"	"\n"
    213  1.2  matt 		"	cmplw	%0,%2"	"\n"
    214  1.2  matt 		"	blt	1b"	"\n"
    215  1.2  matt 		"2:" 			"\n"
    216  1.2  matt 	    : "=&r"(scratch)
    217  1.2  matt 	    : "r"(tbh), "r"(tbl), "n"(SPR_TBL), "n"(SPR_TBU)
    218  1.2  matt 	     : "cr0");
    219  1.2  matt }
    220  1.2  matt 
    221  1.2  matt /*
    222  1.2  matt  * Nothing to do.
    223  1.2  matt  */
    224  1.2  matt void
    225  1.2  matt setstatclockrate(int arg)
    226  1.2  matt {
    227  1.2  matt 
    228  1.2  matt 	/* Do nothing */
    229  1.2  matt }
    230  1.2  matt 
    231  1.2  matt static void
    232  1.2  matt init_ppcbooke_tc(void)
    233  1.2  matt {
    234  1.2  matt 	/* from machdep initialization */
    235  1.2  matt 	ppcbooke_timecounter.tc_frequency = curcpu()->ci_data.cpu_cc_freq;
    236  1.2  matt 	tc_init(&ppcbooke_timecounter);
    237  1.2  matt }
    238