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