clock.c revision 1.2.4.5 1 1.2.4.5 nathanw /* $NetBSD: clock.c,v 1.2.4.5 2002/08/27 23:45:06 nathanw Exp $ */
2 1.2.4.2 nathanw /* $OpenBSD: clock.c,v 1.3 1997/10/13 13:42:53 pefo Exp $ */
3 1.2.4.2 nathanw
4 1.2.4.2 nathanw /*
5 1.2.4.2 nathanw * Copyright (C) 1995, 1996 Wolfgang Solfrank.
6 1.2.4.2 nathanw * Copyright (C) 1995, 1996 TooLs GmbH.
7 1.2.4.2 nathanw * All rights reserved.
8 1.2.4.2 nathanw *
9 1.2.4.2 nathanw * Redistribution and use in source and binary forms, with or without
10 1.2.4.2 nathanw * modification, are permitted provided that the following conditions
11 1.2.4.2 nathanw * are met:
12 1.2.4.2 nathanw * 1. Redistributions of source code must retain the above copyright
13 1.2.4.2 nathanw * notice, this list of conditions and the following disclaimer.
14 1.2.4.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
15 1.2.4.2 nathanw * notice, this list of conditions and the following disclaimer in the
16 1.2.4.2 nathanw * documentation and/or other materials provided with the distribution.
17 1.2.4.2 nathanw * 3. All advertising materials mentioning features or use of this software
18 1.2.4.2 nathanw * must display the following acknowledgement:
19 1.2.4.2 nathanw * This product includes software developed by TooLs GmbH.
20 1.2.4.2 nathanw * 4. The name of TooLs GmbH may not be used to endorse or promote products
21 1.2.4.2 nathanw * derived from this software without specific prior written permission.
22 1.2.4.2 nathanw *
23 1.2.4.2 nathanw * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24 1.2.4.2 nathanw * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.2.4.2 nathanw * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.2.4.2 nathanw * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 1.2.4.2 nathanw * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 1.2.4.2 nathanw * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29 1.2.4.2 nathanw * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 1.2.4.2 nathanw * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 1.2.4.2 nathanw * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 1.2.4.2 nathanw * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.2.4.2 nathanw */
34 1.2.4.2 nathanw
35 1.2.4.2 nathanw #include <sys/param.h>
36 1.2.4.2 nathanw #include <sys/kernel.h>
37 1.2.4.2 nathanw #include <sys/systm.h>
38 1.2.4.2 nathanw #include <sys/properties.h>
39 1.2.4.2 nathanw
40 1.2.4.2 nathanw #include <machine/dcr.h>
41 1.2.4.2 nathanw
42 1.2.4.2 nathanw #include <powerpc/spr.h>
43 1.2.4.2 nathanw
44 1.2.4.2 nathanw /*
45 1.2.4.2 nathanw * Initially we assume a processor with a bus frequency of 12.5 MHz.
46 1.2.4.2 nathanw */
47 1.2.4.2 nathanw static u_long ticks_per_sec;
48 1.2.4.2 nathanw static u_long ns_per_tick;
49 1.2.4.2 nathanw static long ticks_per_intr;
50 1.2.4.2 nathanw static volatile u_long lasttb;
51 1.2.4.2 nathanw static u_long ticksmissed;
52 1.2.4.2 nathanw static volatile int tickspending;
53 1.2.4.2 nathanw
54 1.2.4.2 nathanw void decr_intr(struct clockframe *); /* called from trap_subr.S */
55 1.2.4.2 nathanw void stat_intr(struct clockframe *); /* called from trap_subr.S */
56 1.2.4.2 nathanw
57 1.2.4.2 nathanw #ifdef FAST_STAT_CLOCK
58 1.2.4.2 nathanw /* Stat clock runs at ~ 1.5KHz */
59 1.2.4.2 nathanw #define PERIOD_POWER 17
60 1.2.4.2 nathanw #define TCR_PERIOD TCR_FP_2_17
61 1.2.4.2 nathanw #else
62 1.2.4.2 nathanw /* Stat clock runs at ~ 95Hz */
63 1.2.4.2 nathanw #define PERIOD_POWER 21
64 1.2.4.2 nathanw #define TCR_PERIOD TCR_FP_2_21
65 1.2.4.3 nathanw #endif
66 1.2.4.2 nathanw
67 1.2.4.2 nathanw
68 1.2.4.2 nathanw void
69 1.2.4.2 nathanw stat_intr(struct clockframe *frame)
70 1.2.4.2 nathanw {
71 1.2.4.2 nathanw extern u_long intrcnt[];
72 1.2.4.3 nathanw
73 1.2.4.2 nathanw mtspr(SPR_TSR, TSR_FIS); /* Clear TSR[FIS] */
74 1.2.4.2 nathanw intrcnt[CNT_STATCLOCK]++;
75 1.2.4.2 nathanw statclock(frame);
76 1.2.4.2 nathanw }
77 1.2.4.2 nathanw
78 1.2.4.2 nathanw void
79 1.2.4.2 nathanw decr_intr(struct clockframe *frame)
80 1.2.4.2 nathanw {
81 1.2.4.2 nathanw int pri;
82 1.2.4.2 nathanw long tick, xticks;
83 1.2.4.2 nathanw int nticks;
84 1.2.4.2 nathanw extern u_long intrcnt[];
85 1.2.4.2 nathanw /*
86 1.2.4.2 nathanw * Check whether we are initialized.
87 1.2.4.2 nathanw */
88 1.2.4.2 nathanw if (!ticks_per_intr)
89 1.2.4.2 nathanw return;
90 1.2.4.2 nathanw
91 1.2.4.2 nathanw asm volatile("mftb %0":"=r"(tick):);
92 1.2.4.2 nathanw mtspr(SPR_TSR, TSR_PIS); /* Clear TSR[PIS] */
93 1.2.4.2 nathanw /*
94 1.2.4.2 nathanw * lasttb is used during microtime. Set it to the virtual
95 1.2.4.2 nathanw * start of this tick interval.
96 1.2.4.2 nathanw */
97 1.2.4.2 nathanw xticks = tick - lasttb; /* Number of TLB cycles since last exception */
98 1.2.4.2 nathanw for (nticks = 0; xticks > ticks_per_intr; nticks++)
99 1.2.4.2 nathanw xticks -= ticks_per_intr;
100 1.2.4.2 nathanw lasttb = tick - xticks;
101 1.2.4.2 nathanw
102 1.2.4.2 nathanw intrcnt[CNT_CLOCK]++;
103 1.2.4.2 nathanw pri = splclock();
104 1.2.4.2 nathanw if (pri & SPL_CLOCK) {
105 1.2.4.2 nathanw tickspending += nticks;
106 1.2.4.2 nathanw ticksmissed+= nticks;
107 1.2.4.2 nathanw } else {
108 1.2.4.2 nathanw nticks += tickspending;
109 1.2.4.2 nathanw tickspending = 0;
110 1.2.4.2 nathanw
111 1.2.4.2 nathanw /*
112 1.2.4.2 nathanw * Reenable interrupts
113 1.2.4.2 nathanw */
114 1.2.4.2 nathanw asm volatile ("wrteei 1");
115 1.2.4.2 nathanw
116 1.2.4.2 nathanw /*
117 1.2.4.2 nathanw * Do standard timer interrupt stuff.
118 1.2.4.2 nathanw * Do softclock stuff only on the last iteration.
119 1.2.4.2 nathanw */
120 1.2.4.2 nathanw frame->pri = pri | SINT_CLOCK;
121 1.2.4.2 nathanw while (--nticks > 0)
122 1.2.4.2 nathanw hardclock(frame);
123 1.2.4.2 nathanw frame->pri = pri;
124 1.2.4.2 nathanw hardclock(frame);
125 1.2.4.2 nathanw }
126 1.2.4.2 nathanw splx(pri);
127 1.2.4.2 nathanw }
128 1.2.4.2 nathanw
129 1.2.4.2 nathanw void
130 1.2.4.2 nathanw cpu_initclocks(void)
131 1.2.4.2 nathanw {
132 1.2.4.4 nathanw
133 1.2.4.2 nathanw ticks_per_intr = ticks_per_sec / hz;
134 1.2.4.4 nathanw stathz = profhz = ticks_per_sec / (1 << PERIOD_POWER);
135 1.2.4.2 nathanw printf("Setting PIT to %ld/%d = %ld\n", ticks_per_sec, hz, ticks_per_intr);
136 1.2.4.2 nathanw asm volatile ("mftb %0" : "=r"(lasttb));
137 1.2.4.2 nathanw mtspr(SPR_PIT, ticks_per_intr);
138 1.2.4.2 nathanw /* Enable PIT & FIT(2^17c = 0.655ms) interrupts and auto-reload */
139 1.2.4.3 nathanw mtspr(SPR_TCR, TCR_PIE | TCR_ARE | TCR_FIE | TCR_PERIOD);
140 1.2.4.2 nathanw }
141 1.2.4.2 nathanw
142 1.2.4.2 nathanw void
143 1.2.4.2 nathanw calc_delayconst(void)
144 1.2.4.2 nathanw {
145 1.2.4.2 nathanw unsigned int processor_freq;
146 1.2.4.2 nathanw
147 1.2.4.3 nathanw if (board_info_get("processor-frequency",
148 1.2.4.2 nathanw &processor_freq, sizeof(processor_freq)) == -1)
149 1.2.4.2 nathanw panic("no processor-frequency");
150 1.2.4.2 nathanw
151 1.2.4.2 nathanw ticks_per_sec = processor_freq;
152 1.2.4.2 nathanw ns_per_tick = 1000000000 / ticks_per_sec;
153 1.2.4.2 nathanw }
154 1.2.4.2 nathanw
155 1.2.4.2 nathanw /*
156 1.2.4.2 nathanw * Fill in *tvp with current time with microsecond resolution.
157 1.2.4.2 nathanw */
158 1.2.4.2 nathanw void
159 1.2.4.2 nathanw microtime(struct timeval *tvp)
160 1.2.4.2 nathanw {
161 1.2.4.2 nathanw u_long tb;
162 1.2.4.2 nathanw u_long ticks;
163 1.2.4.2 nathanw int msr;
164 1.2.4.2 nathanw
165 1.2.4.2 nathanw asm volatile ("mfmsr %0; wrteei 0" : "=r"(msr) :);
166 1.2.4.2 nathanw asm ("mftb %0" : "=r"(tb));
167 1.2.4.2 nathanw ticks = (tb - lasttb) * ns_per_tick;
168 1.2.4.2 nathanw *tvp = time;
169 1.2.4.2 nathanw asm volatile ("mtmsr %0" :: "r"(msr));
170 1.2.4.2 nathanw ticks /= 1000;
171 1.2.4.2 nathanw tvp->tv_usec += ticks;
172 1.2.4.2 nathanw while (tvp->tv_usec >= 1000000) {
173 1.2.4.2 nathanw tvp->tv_usec -= 1000000;
174 1.2.4.2 nathanw tvp->tv_sec++;
175 1.2.4.2 nathanw }
176 1.2.4.2 nathanw }
177 1.2.4.2 nathanw
178 1.2.4.2 nathanw /*
179 1.2.4.2 nathanw * Wait for about n microseconds (at least!).
180 1.2.4.2 nathanw */
181 1.2.4.2 nathanw void
182 1.2.4.2 nathanw delay(unsigned int n)
183 1.2.4.2 nathanw {
184 1.2.4.2 nathanw u_quad_t tb;
185 1.2.4.2 nathanw u_long tbh, tbl, scratch;
186 1.2.4.2 nathanw
187 1.2.4.2 nathanw tb = mftb();
188 1.2.4.2 nathanw /* use 1000ULL to force 64 bit math to avoid 32 bit overflows */
189 1.2.4.2 nathanw tb += (n * 1000ULL + ns_per_tick - 1) / ns_per_tick;
190 1.2.4.2 nathanw tbh = tb >> 32;
191 1.2.4.2 nathanw tbl = tb;
192 1.2.4.2 nathanw asm volatile ("1: mftbu %0; cmplw %0,%1; blt 1b; bgt 2f;"
193 1.2.4.2 nathanw "mftb %0; cmplw %0,%2; blt 1b; 2:"
194 1.2.4.2 nathanw : "=r"(scratch) : "r"(tbh), "r"(tbl));
195 1.2.4.2 nathanw }
196 1.2.4.2 nathanw
197 1.2.4.2 nathanw /*
198 1.2.4.2 nathanw * Nothing to do.
199 1.2.4.2 nathanw */
200 1.2.4.2 nathanw void
201 1.2.4.2 nathanw setstatclockrate(int arg)
202 1.2.4.2 nathanw {
203 1.2.4.2 nathanw
204 1.2.4.2 nathanw /* Do nothing */
205 1.2.4.2 nathanw }
206