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