Home | History | Annotate | Line # | Download | only in ibm4xx
clock.c revision 1.28
      1 /*	$NetBSD: clock.c,v 1.28 2020/05/29 12:30:40 rin Exp $	*/
      2 /*      $OpenBSD: clock.c,v 1.3 1997/10/13 13:42:53 pefo Exp $  */
      3 
      4 /*
      5  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
      6  * Copyright (C) 1995, 1996 TooLs GmbH.
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by TooLs GmbH.
     20  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.28 2020/05/29 12:30:40 rin Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/kernel.h>
     40 #include <sys/systm.h>
     41 #include <sys/timetc.h>
     42 #include <sys/cpu.h>
     43 
     44 #include <uvm/uvm_extern.h>
     45 
     46 #include <prop/proplib.h>
     47 
     48 #include <powerpc/spr.h>
     49 #include <powerpc/ibm4xx/spr.h>
     50 #include <powerpc/ibm4xx/cpu.h>
     51 
     52 /*
     53  * Initially we assume a processor with a bus frequency of 12.5 MHz.
     54  */
     55 static u_long ticks_per_sec;
     56 static u_long ns_per_tick;
     57 static long ticks_per_intr;
     58 static volatile u_long lasttb, lasttb2;
     59 static u_long ticksmissed;
     60 static volatile int tickspending;
     61 
     62 static void init_ppc4xx_tc(void);
     63 static u_int get_ppc4xx_timecount(struct timecounter *);
     64 
     65 static struct timecounter ppc4xx_timecounter = {
     66 	.tc_get_timecount = get_ppc4xx_timecount,
     67 	.tc_counter_mask = ~0u,
     68 	.tc_name = "ppc_timebase",
     69 	.tc_quality = 100,
     70 };
     71 
     72 void decr_intr(struct clockframe *);	/* called from trap_subr.S */
     73 void stat_intr(struct clockframe *);	/* called from trap_subr.S */
     74 
     75 #ifdef FAST_STAT_CLOCK
     76 /* Stat clock runs at ~ 1.5 kHz */
     77 #define PERIOD_POWER	17
     78 #define TCR_PERIOD	TCR_FP_2_17
     79 #else
     80 /* Stat clock runs at ~ 95Hz */
     81 #define PERIOD_POWER	21
     82 #define TCR_PERIOD	TCR_FP_2_21
     83 #endif
     84 
     85 
     86 void
     87 stat_intr(struct clockframe *frame)
     88 {
     89 	struct cpu_info * const ci = curcpu();
     90 
     91 	mtspr(SPR_TSR, TSR_FIS);	/* Clear TSR[FIS] */
     92 	ci->ci_data.cpu_nintr++;
     93 	ci->ci_ev_statclock.ev_count++;
     94 
     95 	/* Nobody can interrupt us, but see if we're allowed to run. */
     96 	int s = splclock();
     97 
     98 	/*
     99 	 * Reenable interrupts
    100 	 */
    101 	__asm volatile ("wrteei 1");
    102 
    103 	if (IPL_CLOCK > s)
    104   		statclock(frame);
    105 	splx(s);
    106 }
    107 
    108 void
    109 decr_intr(struct clockframe *frame)
    110 {
    111 	struct cpu_info * const ci = curcpu();
    112 	int pcpl;
    113 	long tbtick, xticks;
    114 	int nticks;
    115 
    116 	/*
    117 	 * Check whether we are initialized.
    118 	 */
    119 	if (!ticks_per_intr)
    120 		return;
    121 
    122 	tbtick = mftbl();
    123 	mtspr(SPR_TSR, TSR_PIS);	/* Clear TSR[PIS] */
    124 
    125 	xticks = tbtick - lasttb2;	/* Number of TLB cycles since last exception */
    126 	for (nticks = 0; xticks > ticks_per_intr; nticks++)
    127 		xticks -= ticks_per_intr;
    128 	lasttb2 = tbtick - xticks;
    129 
    130 	ci->ci_data.cpu_nintr++;
    131 	ci->ci_ev_clock.ev_count++;
    132 	pcpl = splclock();
    133 
    134 	/*
    135 	 * Reenable interrupts
    136 	 */
    137 	__asm volatile ("wrteei 1");
    138 
    139 	if (pcpl >= IPL_CLOCK) {
    140 		tickspending += nticks;
    141 		ticksmissed += nticks;
    142 	} else {
    143 		nticks += tickspending;
    144 		tickspending = 0;
    145 
    146 		/*
    147 		 * lasttb is used during microtime. Set it to the virtual
    148 		 * start of this tick interval.
    149 		 */
    150 		lasttb = lasttb2;
    151 
    152 		/*
    153 		 * Do standard timer interrupt stuff.
    154 		 * Do softclock stuff only on the last iteration.
    155 		 */
    156 		while (--nticks > 0)
    157 			hardclock(frame);
    158 		hardclock(frame);
    159 	}
    160 	splx(pcpl);
    161 }
    162 
    163 void
    164 cpu_initclocks(void)
    165 {
    166 	struct cpu_info * const ci = curcpu();
    167 
    168 	/* Initialized in powerpc/ibm4xx/cpu.c */
    169 	evcnt_attach_static(&ci->ci_ev_clock);
    170 	evcnt_attach_static(&ci->ci_ev_statclock);
    171 
    172 	ticks_per_intr = ticks_per_sec / hz;
    173 	stathz = profhz = ticks_per_sec / (1 << PERIOD_POWER);
    174 
    175 	printf("Setting PIT to %ld/%d = %ld\n", ticks_per_sec, hz,
    176 	    ticks_per_intr);
    177 
    178 	lasttb2 = lasttb = mftbl();
    179 	mtspr(SPR_PIT, ticks_per_intr);
    180 
    181 	/* Enable PIT & FIT(2^17c = 0.655ms) interrupts and auto-reload */
    182 	mtspr(SPR_TCR, TCR_PIE | TCR_ARE | TCR_FIE | TCR_PERIOD);
    183 
    184 	init_ppc4xx_tc();
    185 }
    186 
    187 void
    188 calc_delayconst(void)
    189 {
    190 	prop_number_t freq;
    191 
    192 	freq = prop_dictionary_get(board_properties, "processor-frequency");
    193 	KASSERT(freq != NULL);
    194 
    195 	ticks_per_sec = (u_long) prop_number_integer_value(freq);
    196 	ns_per_tick = 1000000000 / ticks_per_sec;
    197 }
    198 
    199 static u_int
    200 get_ppc4xx_timecount(struct timecounter *tc)
    201 {
    202 	u_long tb;
    203 	int msr;
    204 
    205 	__asm volatile ("mfmsr %0; wrteei 0" : "=r"(msr) :);
    206 	tb = mftbl();
    207 	__asm volatile ("mtmsr %0" :: "r"(msr));
    208 
    209 	return tb;
    210 }
    211 
    212 /*
    213  * Wait for about n microseconds (at least!).
    214  */
    215 void
    216 delay(unsigned int n)
    217 {
    218 	u_quad_t tb;
    219 	u_long tbh, tbl, scratch;
    220 
    221 	tb = mftb();
    222 	/* use 1000ULL to force 64 bit math to avoid 32 bit overflows */
    223 	tb += (n * 1000ULL + ns_per_tick - 1) / ns_per_tick;
    224 	tbh = tb >> 32;
    225 	tbl = tb;
    226 	__asm volatile (
    227 #ifdef PPC_IBM403
    228 	    "1:	mftbhi %0	\n"
    229 #else
    230 	    "1:	mftbu %0	\n"
    231 #endif
    232 	    "	cmplw %0,%1	\n"
    233 	    "	blt 1b		\n"
    234 	    "	bgt 2f		\n"
    235 #ifdef PPC_IBM403
    236 	    "	mftblo %0	\n"
    237 #else
    238 	    "	mftb %0		\n"
    239 #endif
    240 	    "	cmplw %0,%2	\n"
    241 	    "	blt 1b		\n"
    242 	    "2: 		\n"
    243 	    : "=&r"(scratch) : "r"(tbh), "r"(tbl) : "cr0");
    244 }
    245 
    246 /*
    247  * Nothing to do.
    248  */
    249 void
    250 setstatclockrate(int arg)
    251 {
    252 
    253 	/* Do nothing */
    254 }
    255 
    256 static void
    257 init_ppc4xx_tc(void)
    258 {
    259 	/* from machdep initialization */
    260 	ppc4xx_timecounter.tc_frequency = ticks_per_sec;
    261 	tc_init(&ppc4xx_timecounter);
    262 }
    263