Home | History | Annotate | Line # | Download | only in booke
e500_timer.c revision 1.5
      1  1.5  joerg /*	$NetBSD: e500_timer.c,v 1.5 2014/07/29 23:35:00 joerg 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.5  joerg __KERNEL_RCSID(0, "$NetBSD: e500_timer.c,v 1.5 2014/07/29 23:35:00 joerg 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 void
     73  1.2   matt openpic_write(struct cpu_softc *cpu, bus_size_t offset, uint32_t val)
     74  1.2   matt {
     75  1.2   matt 
     76  1.2   matt 	return bus_space_write_4(cpu->cpu_bst, cpu->cpu_bsh,
     77  1.2   matt 	    OPENPIC_BASE + offset, val);
     78  1.2   matt }
     79  1.2   matt 
     80  1.2   matt int
     81  1.2   matt e500_clock_intr(void *v)
     82  1.2   matt {
     83  1.2   matt 	struct trapframe * const tf = v;
     84  1.2   matt 	struct cpu_info * const ci = curcpu();
     85  1.2   matt 	struct cpu_softc * const cpu = ci->ci_softc;
     86  1.2   matt 	u_int nticks;
     87  1.2   matt 
     88  1.2   matt 	/*
     89  1.2   matt 	 * Check whether we are initialized.
     90  1.2   matt 	 */
     91  1.2   matt 	if (!cpu->cpu_ticks_per_clock_intr)
     92  1.2   matt 		return 0;
     93  1.2   matt 
     94  1.2   matt 	/*
     95  1.2   matt 	 * Now let's how delayed the clock interrupt was.  Obviously it must
     96  1.2   matt 	 * at least one clock tick since the clock interrupt.  But it might
     97  1.2   matt 	 * be more if interrupts were blocked for a long time.  We keep
     98  1.2   matt 	 * suubtracting an interrupts We should be
     99  1.2   matt 	 * [well] within a single tick.
    100  1.2   matt 	 * We add back one tick (which should put us back above 0).  If we
    101  1.2   matt 	 * are still below 0, keep adding ticks until we are above 0.
    102  1.2   matt 	 */
    103  1.2   matt 	const uint64_t now = mftb();
    104  1.2   matt 	uint64_t latency = now - (ci->ci_lastintr + cpu->cpu_ticks_per_clock_intr);
    105  1.2   matt #if 0
    106  1.2   matt 	uint64_t orig_latency = latency;
    107  1.2   matt #endif
    108  1.2   matt 	if (now < ci->ci_lastintr + cpu->cpu_ticks_per_clock_intr)
    109  1.2   matt 		latency = 0;
    110  1.2   matt 
    111  1.2   matt 	nticks = 1 + latency / cpu->cpu_ticks_per_clock_intr;
    112  1.2   matt 	latency %= cpu->cpu_ticks_per_clock_intr;
    113  1.2   matt #if 0
    114  1.2   matt 	for (nticks = 1; latency >= cpu->cpu_ticks_per_clock_intr; nticks++)  {
    115  1.2   matt 		latency -= cpu->cpu_ticks_per_clock_intr;
    116  1.2   matt 	}
    117  1.2   matt #endif
    118  1.2   matt 
    119  1.2   matt 	ci->ci_ev_clock.ev_count++;
    120  1.2   matt 	cpu->cpu_ev_late_clock.ev_count += nticks - 1;
    121  1.2   matt 
    122  1.2   matt 	/*
    123  1.2   matt 	 * lasttb is used during microtime. Set it to the virtual
    124  1.2   matt 	 * start of this tick interval.
    125  1.2   matt 	 */
    126  1.2   matt #if 0
    127  1.2   matt 	if (nticks > 10 || now - ci->ci_lastintr < 7 * cpu->cpu_ticks_per_clock_intr / 8)
    128  1.2   matt 	printf("%s: nticks=%u lastintr=%#"PRIx64"(%#"PRIx64") now=%#"PRIx64" latency=%#"PRIx64" orig=%#"PRIx64"\n", __func__,
    129  1.2   matt 	    nticks, ci->ci_lastintr, now - latency, now, latency, orig_latency);
    130  1.2   matt #endif
    131  1.2   matt 	ci->ci_lastintr = now - latency;
    132  1.2   matt 	ci->ci_lasttb = now;
    133  1.2   matt 
    134  1.2   matt 	wrtee(PSL_EE);	/* Reenable interrupts */
    135  1.2   matt 
    136  1.2   matt 	/*
    137  1.2   matt 	 * Do standard timer interrupt stuff.
    138  1.2   matt 	 */
    139  1.2   matt 	while (nticks-- > 0) {
    140  1.2   matt 		hardclock(&tf->tf_cf);
    141  1.2   matt 	}
    142  1.2   matt 
    143  1.2   matt 	wrtee(0);	/* turn off interrupts */
    144  1.2   matt 
    145  1.3   matt 	tf->tf_srr1 &= ~PSL_POW;	/* make cpu_idle exit */
    146  1.3   matt 
    147  1.2   matt 	return 1;
    148  1.2   matt }
    149  1.2   matt 
    150  1.2   matt void
    151  1.2   matt cpu_initclocks(void)
    152  1.2   matt {
    153  1.2   matt 	struct cpu_info * const ci = curcpu();
    154  1.2   matt 	struct cpu_softc * const cpu = ci->ci_softc;
    155  1.2   matt 
    156  1.2   matt 	cpu->cpu_ticks_per_clock_intr = (ci->ci_data.cpu_cc_freq + hz/2 - 1) / hz;
    157  1.2   matt 
    158  1.2   matt 	/* interrupt established in e500_intr_cpu_init */
    159  1.2   matt 
    160  1.2   matt 	ci->ci_lastintr = ci->ci_lasttb = mftb();
    161  1.2   matt 	openpic_write(cpu, cpu->cpu_clock_gtbcr,
    162  1.2   matt 	     GTBCR_CI | cpu->cpu_ticks_per_clock_intr);
    163  1.2   matt 	openpic_write(cpu, cpu->cpu_clock_gtbcr,
    164  1.2   matt 	     cpu->cpu_ticks_per_clock_intr);
    165  1.2   matt 
    166  1.4   matt 	if (CPU_IS_PRIMARY(ci))
    167  1.4   matt 		init_ppcbooke_tc();
    168  1.2   matt }
    169  1.2   matt 
    170  1.2   matt void
    171  1.2   matt calc_delayconst(void)
    172  1.2   matt {
    173  1.2   matt 	struct cpu_info * const ci = curcpu();
    174  1.2   matt 
    175  1.2   matt 	ci->ci_data.cpu_cc_freq = board_info_get_number("timebase-frequency");
    176  1.2   matt 	ns_per_tick = 1000000000 / (u_int)ci->ci_data.cpu_cc_freq;
    177  1.2   matt }
    178  1.2   matt 
    179  1.2   matt static u_int
    180  1.2   matt get_ppcbooke_timecount(struct timecounter *tc)
    181  1.2   matt {
    182  1.2   matt 	return mftbl();
    183  1.2   matt }
    184  1.2   matt 
    185  1.2   matt /*
    186  1.2   matt  * Wait for about n microseconds (at least!).
    187  1.2   matt  */
    188  1.2   matt void
    189  1.2   matt delay(unsigned int n)
    190  1.2   matt {
    191  1.2   matt 	uint64_t tb;
    192  1.2   matt 	u_long tbh, tbl, scratch;
    193  1.2   matt 
    194  1.2   matt 	tb = mftb();
    195  1.2   matt 	/* use 1000ULL to force 64 bit math to avoid 32 bit overflows */
    196  1.2   matt 	tb += (n * 1000ULL + ns_per_tick - 1) / ns_per_tick;
    197  1.2   matt 	tbh = tb >> 32;
    198  1.2   matt 	tbl = tb;
    199  1.2   matt 	__asm volatile (
    200  1.2   matt 		"1:	mfspr	%0,%4"	"\n"
    201  1.2   matt 		"	cmplw	%0,%1"	"\n"
    202  1.2   matt 		"	blt	1b"	"\n"
    203  1.2   matt 		"	bgt	2f"	"\n"
    204  1.2   matt 		"	mfspr	%0,%3"	"\n"
    205  1.2   matt 		"	cmplw	%0,%2"	"\n"
    206  1.2   matt 		"	blt	1b"	"\n"
    207  1.2   matt 		"2:" 			"\n"
    208  1.2   matt 	    : "=&r"(scratch)
    209  1.2   matt 	    : "r"(tbh), "r"(tbl), "n"(SPR_TBL), "n"(SPR_TBU)
    210  1.2   matt 	     : "cr0");
    211  1.2   matt }
    212  1.2   matt 
    213  1.2   matt /*
    214  1.2   matt  * Nothing to do.
    215  1.2   matt  */
    216  1.2   matt void
    217  1.2   matt setstatclockrate(int arg)
    218  1.2   matt {
    219  1.2   matt 
    220  1.2   matt 	/* Do nothing */
    221  1.2   matt }
    222  1.2   matt 
    223  1.2   matt static void
    224  1.2   matt init_ppcbooke_tc(void)
    225  1.2   matt {
    226  1.2   matt 	/* from machdep initialization */
    227  1.2   matt 	ppcbooke_timecounter.tc_frequency = curcpu()->ci_data.cpu_cc_freq;
    228  1.2   matt 	tc_init(&ppcbooke_timecounter);
    229  1.2   matt }
    230