clock.c revision 1.23.2.1 1 /* $NetBSD: clock.c,v 1.23.2.1 2011/06/06 09:06:27 jruoho 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.23.2.1 2011/06/06 09:06:27 jruoho Exp $");
37
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #include <sys/timetc.h>
42
43 #include <uvm/uvm_extern.h>
44
45 #include <prop/proplib.h>
46
47 #include <machine/cpu.h>
48
49 #include <powerpc/spr.h>
50 #include <powerpc/ibm4xx/spr.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 get_ppc4xx_timecount, /* get_timecount */
67 0, /* no poll_pps */
68 ~0u, /* counter_mask */
69 0, /* frequency */
70 "ppc_timebase", /* name */
71 100, /* quality */
72 NULL, /* tc_priv */
73 NULL /* tc_next */
74 };
75
76 void decr_intr(struct clockframe *); /* called from trap_subr.S */
77 void stat_intr(struct clockframe *); /* called from trap_subr.S */
78
79 #ifdef FAST_STAT_CLOCK
80 /* Stat clock runs at ~ 1.5 kHz */
81 #define PERIOD_POWER 17
82 #define TCR_PERIOD TCR_FP_2_17
83 #else
84 /* Stat clock runs at ~ 95Hz */
85 #define PERIOD_POWER 21
86 #define TCR_PERIOD TCR_FP_2_21
87 #endif
88
89
90 void
91 stat_intr(struct clockframe *frame)
92 {
93 struct cpu_info * const ci = curcpu();
94
95 mtspr(SPR_TSR, TSR_FIS); /* Clear TSR[FIS] */
96 ci->ci_data.cpu_nintr++;
97 ci->ci_ev_statclock.ev_count++;
98
99 /* Nobody can interrupt us, but see if we're allowed to run. */
100 if (! (ci->ci_cpl & mask_statclock))
101 statclock(frame);
102 }
103
104 void
105 decr_intr(struct clockframe *frame)
106 {
107 struct cpu_info * const ci = curcpu();
108 int pri;
109 long tbtick, xticks;
110 int nticks;
111
112 /*
113 * Check whether we are initialized.
114 */
115 if (!ticks_per_intr)
116 return;
117
118 tbtick = mftbl();
119 mtspr(SPR_TSR, TSR_PIS); /* Clear TSR[PIS] */
120
121 xticks = tbtick - lasttb2; /* Number of TLB cycles since last exception */
122 for (nticks = 0; xticks > ticks_per_intr; nticks++)
123 xticks -= ticks_per_intr;
124 lasttb2 = tbtick - xticks;
125
126 ci->ci_data.cpu_nintr++;
127 ci->ci_ev_clock.ev_count++;
128 pri = splclock();
129 if (pri & mask_clock) {
130 tickspending += nticks;
131 ticksmissed += nticks;
132 } else {
133 nticks += tickspending;
134 tickspending = 0;
135
136 /*
137 * lasttb is used during microtime. Set it to the virtual
138 * start of this tick interval.
139 */
140 lasttb = lasttb2;
141
142 /*
143 * Reenable interrupts
144 */
145 __asm volatile ("wrteei 1");
146
147 /*
148 * Do standard timer interrupt stuff.
149 * Do softclock stuff only on the last iteration.
150 */
151 while (--nticks > 0)
152 hardclock(frame);
153 hardclock(frame);
154 }
155 splx(pri);
156 }
157
158 void
159 cpu_initclocks(void)
160 {
161 struct cpu_info * const ci = curcpu();
162
163 /* Initialized in powerpc/ibm4xx/cpu.c */
164 evcnt_attach_static(&ci->ci_ev_clock);
165 evcnt_attach_static(&ci->ci_ev_statclock);
166
167 ticks_per_intr = ticks_per_sec / hz;
168 stathz = profhz = ticks_per_sec / (1 << PERIOD_POWER);
169
170 printf("Setting PIT to %ld/%d = %ld\n", ticks_per_sec, hz,
171 ticks_per_intr);
172
173 lasttb2 = lasttb = mftbl();
174 mtspr(SPR_PIT, ticks_per_intr);
175
176 /* Enable PIT & FIT(2^17c = 0.655ms) interrupts and auto-reload */
177 mtspr(SPR_TCR, TCR_PIE | TCR_ARE | TCR_FIE | TCR_PERIOD);
178
179 init_ppc4xx_tc();
180 }
181
182 void
183 calc_delayconst(void)
184 {
185 prop_number_t freq;
186
187 freq = prop_dictionary_get(board_properties, "processor-frequency");
188 KASSERT(freq != NULL);
189
190 ticks_per_sec = (u_long) prop_number_integer_value(freq);
191 ns_per_tick = 1000000000 / ticks_per_sec;
192 }
193
194 static u_int
195 get_ppc4xx_timecount(struct timecounter *tc)
196 {
197 u_long tb;
198 int msr;
199
200 __asm volatile ("mfmsr %0; wrteei 0" : "=r"(msr) :);
201 tb = mftbl();
202 __asm volatile ("mtmsr %0" :: "r"(msr));
203
204 return tb;
205 }
206
207 /*
208 * Wait for about n microseconds (at least!).
209 */
210 void
211 delay(unsigned int n)
212 {
213 u_quad_t tb;
214 u_long tbh, tbl, scratch;
215
216 tb = mftb();
217 /* use 1000ULL to force 64 bit math to avoid 32 bit overflows */
218 tb += (n * 1000ULL + ns_per_tick - 1) / ns_per_tick;
219 tbh = tb >> 32;
220 tbl = tb;
221 __asm volatile (
222 #ifdef PPC_IBM403
223 "1: mftbhi %0 \n"
224 #else
225 "1: mftbu %0 \n"
226 #endif
227 " cmplw %0,%1 \n"
228 " blt 1b \n"
229 " bgt 2f \n"
230 #ifdef PPC_IBM403
231 " mftblo %0 \n"
232 #else
233 " mftb %0 \n"
234 #endif
235 " cmplw %0,%2 \n"
236 " blt 1b \n"
237 "2: \n"
238 : "=&r"(scratch) : "r"(tbh), "r"(tbl) : "cr0");
239 }
240
241 /*
242 * Nothing to do.
243 */
244 void
245 setstatclockrate(int arg)
246 {
247
248 /* Do nothing */
249 }
250
251 static void
252 init_ppc4xx_tc(void)
253 {
254 /* from machdep initialization */
255 ppc4xx_timecounter.tc_frequency = ticks_per_sec;
256 tc_init(&ppc4xx_timecounter);
257 }
258