loongson_clock.c revision 1.3 1 1.3 nia /* $NetBSD: loongson_clock.c,v 1.3 2020/10/25 16:39:00 nia Exp $ */
2 1.1 macallan
3 1.1 macallan /*
4 1.1 macallan * Copyright (c) 2011, 2016 Michael Lorenz
5 1.1 macallan * All rights reserved.
6 1.1 macallan *
7 1.1 macallan * Redistribution and use in source and binary forms, with or without
8 1.1 macallan * modification, are permitted provided that the following conditions
9 1.1 macallan * are met:
10 1.1 macallan * 1. Redistributions of source code must retain the above copyright
11 1.1 macallan * notice, this list of conditions and the following disclaimer.
12 1.1 macallan * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 macallan * notice, this list of conditions and the following disclaimer in the
14 1.1 macallan * documentation and/or other materials provided with the distribution.
15 1.1 macallan *
16 1.1 macallan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 macallan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 macallan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 macallan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 macallan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 macallan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 macallan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 macallan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 macallan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 macallan * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 macallan */
27 1.1 macallan
28 1.1 macallan #include <sys/cdefs.h>
29 1.3 nia __KERNEL_RCSID(0, "$NetBSD: loongson_clock.c,v 1.3 2020/10/25 16:39:00 nia Exp $");
30 1.1 macallan
31 1.1 macallan #include <sys/param.h>
32 1.1 macallan #include <sys/systm.h>
33 1.1 macallan #include <sys/kernel.h>
34 1.1 macallan #include <sys/device.h>
35 1.1 macallan #include <sys/cpu.h>
36 1.1 macallan #include <sys/timetc.h>
37 1.1 macallan #include <sys/sysctl.h>
38 1.1 macallan
39 1.1 macallan #include <mips/mips3_clock.h>
40 1.1 macallan #include <mips/locore.h>
41 1.1 macallan #include <mips/bonito/bonitoreg.h>
42 1.1 macallan #include <mips/bonito/bonitovar.h>
43 1.1 macallan
44 1.1 macallan #ifdef LOONGSON_CLOCK_DEBUG
45 1.1 macallan #define DPRINTF aprint_error
46 1.1 macallan #else
47 1.1 macallan #define DPRINTF while (0) printf
48 1.1 macallan #endif
49 1.1 macallan
50 1.1 macallan static uint32_t sc_last;
51 1.1 macallan static uint32_t sc_scale[8];
52 1.1 macallan static uint32_t sc_count; /* should probably be 64 bit */
53 1.1 macallan static int sc_step = 7;
54 1.1 macallan static int sc_step_wanted = 7;
55 1.1 macallan static void *sc_shutdown_cookie;
56 1.1 macallan
57 1.1 macallan /* 0, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, 1 */
58 1.1 macallan static int scale_m[] = {1, 1, 3, 1, 5, 3, 7, 1};
59 1.1 macallan static int scale_d[] = {0, 4, 8, 2, 8, 4, 8, 1};
60 1.1 macallan static int cycles[8];
61 1.1 macallan
62 1.1 macallan #define scale(x, f) (x * scale_d[f] / scale_m[f])
63 1.1 macallan #define rscale(x, f) (x * scale_m[f] / scale_d[f])
64 1.1 macallan
65 1.1 macallan static void loongson_set_speed(int);
66 1.1 macallan static int loongson_cpuspeed_temp(SYSCTLFN_ARGS);
67 1.1 macallan static int loongson_cpuspeed_cur(SYSCTLFN_ARGS);
68 1.1 macallan static int loongson_cpuspeed_available(SYSCTLFN_ARGS);
69 1.1 macallan
70 1.1 macallan static void loongson_clock_shutdown(void *);
71 1.1 macallan static u_int get_loongson_timecount(struct timecounter *);
72 1.1 macallan void loongson_delay(int);
73 1.1 macallan void loongson_setstatclockrate(int);
74 1.1 macallan void loongson_initclocks(void);
75 1.1 macallan
76 1.1 macallan static struct timecounter loongson_timecounter = {
77 1.2 rin .tc_get_timecount = get_loongson_timecount,
78 1.2 rin .tc_counter_mask = 0xffffffff,
79 1.2 rin .tc_name = "loongson",
80 1.2 rin .tc_quality = 100,
81 1.1 macallan };
82 1.1 macallan
83 1.1 macallan void
84 1.1 macallan loongson_initclocks(void)
85 1.1 macallan {
86 1.1 macallan const struct sysctlnode *sysctl_node, *me, *freq;
87 1.1 macallan int clk;
88 1.1 macallan
89 1.1 macallan /*
90 1.1 macallan * Establish a hook so on shutdown we can set the CPU clock back to
91 1.1 macallan * full speed. This is necessary because PMON doesn't change the
92 1.1 macallan * clock scale register on a warm boot, the MIPS clock code gets
93 1.1 macallan * confused if we're too slow and the loongson-specific bits run
94 1.1 macallan * too late in the boot process
95 1.1 macallan */
96 1.1 macallan sc_shutdown_cookie = shutdownhook_establish(loongson_clock_shutdown, NULL);
97 1.1 macallan
98 1.1 macallan for (clk = 1; clk < 8; clk++) {
99 1.1 macallan sc_scale[clk] = rscale(curcpu()->ci_cpu_freq / 1000000, clk);
100 1.1 macallan cycles[clk] =
101 1.1 macallan (rscale(curcpu()->ci_cpu_freq, clk) + hz / 2) / (2 * hz);
102 1.1 macallan }
103 1.1 macallan #ifdef LOONGSON_CLOCK_DEBUG
104 1.1 macallan for (clk = 1; clk < 8; clk++) {
105 1.1 macallan aprint_normal("frequencies: %d/8: %d\n", clk + 1,
106 1.1 macallan sc_scale[clk]);
107 1.1 macallan }
108 1.1 macallan #endif
109 1.1 macallan
110 1.1 macallan /* now setup sysctl */
111 1.1 macallan if (sysctl_createv(NULL, 0, NULL,
112 1.1 macallan &me,
113 1.3 nia CTLFLAG_READWRITE, CTLTYPE_NODE, "cpu", NULL, NULL,
114 1.1 macallan 0, NULL, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL) != 0)
115 1.3 nia aprint_error("couldn't create 'cpu' node\n");
116 1.1 macallan
117 1.1 macallan if (sysctl_createv(NULL, 0, NULL,
118 1.1 macallan &freq,
119 1.1 macallan CTLFLAG_READWRITE, CTLTYPE_NODE, "frequency", NULL, NULL, 0, NULL,
120 1.1 macallan 0, CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL) != 0)
121 1.1 macallan aprint_error("couldn't create 'frequency' node\n");
122 1.1 macallan
123 1.1 macallan if (sysctl_createv(NULL, 0, NULL,
124 1.1 macallan &sysctl_node,
125 1.1 macallan CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
126 1.1 macallan CTLTYPE_INT, "target", "CPU speed", loongson_cpuspeed_temp,
127 1.1 macallan 0, NULL, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
128 1.1 macallan CTL_CREATE, CTL_EOL) == 0) {
129 1.1 macallan } else
130 1.1 macallan aprint_error("couldn't create 'target' node\n");
131 1.1 macallan
132 1.1 macallan if (sysctl_createv(NULL, 0, NULL,
133 1.1 macallan &sysctl_node,
134 1.1 macallan CTLFLAG_READWRITE,
135 1.1 macallan CTLTYPE_INT, "current", NULL, loongson_cpuspeed_cur,
136 1.1 macallan 1, NULL, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
137 1.1 macallan CTL_CREATE, CTL_EOL) == 0) {
138 1.1 macallan } else
139 1.1 macallan aprint_error("couldn't create 'current' node\n");
140 1.1 macallan
141 1.1 macallan if (sysctl_createv(NULL, 0, NULL,
142 1.1 macallan &sysctl_node,
143 1.1 macallan CTLFLAG_READWRITE,
144 1.1 macallan CTLTYPE_STRING, "available", NULL, loongson_cpuspeed_available,
145 1.1 macallan 2, NULL, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
146 1.1 macallan CTL_CREATE, CTL_EOL) == 0) {
147 1.1 macallan } else
148 1.1 macallan aprint_error("couldn't create 'available' node\n");
149 1.1 macallan
150 1.1 macallan sc_count = 0;
151 1.1 macallan loongson_timecounter.tc_frequency = curcpu()->ci_cpu_freq / 2;
152 1.1 macallan curcpu()->ci_cctr_freq = loongson_timecounter.tc_frequency;
153 1.1 macallan
154 1.1 macallan sc_last = mips3_cp0_count_read();
155 1.1 macallan mips3_cp0_compare_write(sc_last + curcpu()->ci_cycles_per_hz);
156 1.1 macallan
157 1.1 macallan tc_init(&loongson_timecounter);
158 1.1 macallan
159 1.1 macallan /*
160 1.1 macallan * Now we can enable all interrupts including hardclock(9)
161 1.1 macallan * by CPU INT5.
162 1.1 macallan */
163 1.1 macallan spl0();
164 1.1 macallan printf("boom\n");
165 1.1 macallan }
166 1.1 macallan
167 1.1 macallan static void
168 1.1 macallan loongson_clock_shutdown(void *cookie)
169 1.1 macallan {
170 1.1 macallan
171 1.1 macallan /* just in case the interrupt handler runs again after this */
172 1.1 macallan sc_step_wanted = 7;
173 1.1 macallan /* set the clock to full speed */
174 1.1 macallan REGVAL(LS2F_CHIPCFG0) =
175 1.1 macallan (REGVAL(LS2F_CHIPCFG0) & ~LS2FCFG_FREQSCALE_MASK) | 7;
176 1.1 macallan }
177 1.1 macallan
178 1.1 macallan void
179 1.1 macallan loongson_set_speed(int speed)
180 1.1 macallan {
181 1.1 macallan
182 1.1 macallan if ((speed < 1) || (speed > 7))
183 1.1 macallan return;
184 1.1 macallan sc_step_wanted = speed;
185 1.1 macallan DPRINTF("%s: %d\n", __func__, speed);
186 1.1 macallan }
187 1.1 macallan
188 1.1 macallan /*
189 1.1 macallan * the clock interrupt handler
190 1.1 macallan * we don't have a CPU clock independent, high resolution counter so we're
191 1.1 macallan * stuck with a PWM that can't count and a CP0 counter that slows down or
192 1.1 macallan * speeds up with the actual CPU speed. In order to still get halfway
193 1.1 macallan * accurate time we do the following:
194 1.1 macallan * - only change CPU speed in the timer interrupt
195 1.1 macallan * - each timer interrupt we measure how many CP0 cycles passed since last
196 1.1 macallan * time, adjust for CPU speed since we can be sure it didn't change, use
197 1.1 macallan * that to update a separate counter
198 1.1 macallan * - when reading the time counter we take the number of CP0 ticks since
199 1.1 macallan * the last timer interrupt, scale it to CPU clock, return that plus the
200 1.1 macallan * interrupt updated counter mentioned above to get something close to
201 1.1 macallan * CP0 running at full speed
202 1.1 macallan * - when changing CPU speed do it as close to taking the time from CP0 as
203 1.1 macallan * possible to keep the period of time we spend with CP0 running at the
204 1.1 macallan * wrong frequency as short as possible - hopefully short enough to stay
205 1.1 macallan * insignificant compared to other noise since switching speeds isn't
206 1.1 macallan * going to happen all that often
207 1.1 macallan */
208 1.1 macallan
209 1.1 macallan void
210 1.1 macallan mips3_clockintr(struct clockframe *cf)
211 1.1 macallan {
212 1.1 macallan uint32_t now, diff, next, new_cnt;
213 1.1 macallan
214 1.1 macallan /*
215 1.1 macallan * this looks kinda funny but what we want here is this:
216 1.1 macallan * - reading the counter and changing the CPU clock should be as
217 1.1 macallan * close together as possible in order to remain halfway accurate
218 1.1 macallan * - we need to use the previous sc_step in order to scale the
219 1.1 macallan * interval passed since the last clock interrupt correctly, so
220 1.1 macallan * we only change sc_step after doing that
221 1.1 macallan */
222 1.1 macallan if (sc_step_wanted != sc_step) {
223 1.1 macallan
224 1.1 macallan REGVAL(LS2F_CHIPCFG0) =
225 1.1 macallan (REGVAL(LS2F_CHIPCFG0) & ~LS2FCFG_FREQSCALE_MASK) |
226 1.1 macallan sc_step_wanted;
227 1.1 macallan }
228 1.1 macallan
229 1.1 macallan now = mips3_cp0_count_read();
230 1.1 macallan diff = now - sc_last;
231 1.1 macallan sc_count += scale(diff, sc_step);
232 1.1 macallan sc_last = now;
233 1.1 macallan if (sc_step_wanted != sc_step) {
234 1.1 macallan sc_step = sc_step_wanted;
235 1.1 macallan curcpu()->ci_cycles_per_hz = cycles[sc_step];
236 1.1 macallan }
237 1.1 macallan next = now + curcpu()->ci_cycles_per_hz;
238 1.1 macallan curcpu()->ci_ev_count_compare.ev_count++;
239 1.1 macallan
240 1.1 macallan mips3_cp0_compare_write(next);
241 1.1 macallan
242 1.1 macallan /* Check for lost clock interrupts */
243 1.1 macallan new_cnt = mips3_cp0_count_read();
244 1.1 macallan
245 1.1 macallan /*
246 1.1 macallan * Missed one or more clock interrupts, so let's start
247 1.1 macallan * counting again from the current value.
248 1.1 macallan */
249 1.1 macallan if ((next - new_cnt) & 0x80000000) {
250 1.1 macallan
251 1.1 macallan next = new_cnt + curcpu()->ci_cycles_per_hz;
252 1.1 macallan mips3_cp0_compare_write(next);
253 1.1 macallan curcpu()->ci_ev_count_compare_missed.ev_count++;
254 1.1 macallan }
255 1.1 macallan
256 1.1 macallan hardclock(cf);
257 1.1 macallan }
258 1.1 macallan
259 1.1 macallan static u_int
260 1.1 macallan get_loongson_timecount(struct timecounter *tc)
261 1.1 macallan {
262 1.1 macallan uint32_t now, diff;
263 1.1 macallan
264 1.1 macallan now = mips3_cp0_count_read();
265 1.1 macallan diff = now - sc_last;
266 1.1 macallan return sc_count + scale(diff, sc_step);
267 1.1 macallan }
268 1.1 macallan
269 1.1 macallan static int
270 1.1 macallan loongson_cpuspeed_temp(SYSCTLFN_ARGS)
271 1.1 macallan {
272 1.1 macallan struct sysctlnode node = *rnode;
273 1.1 macallan int mhz, i;
274 1.1 macallan
275 1.1 macallan mhz = sc_scale[sc_step_wanted];
276 1.1 macallan
277 1.1 macallan node.sysctl_data = &mhz;
278 1.1 macallan if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
279 1.1 macallan int new_reg;
280 1.1 macallan
281 1.1 macallan new_reg = *(int *)node.sysctl_data;
282 1.1 macallan i = 1;
283 1.1 macallan while ((i < 8) && (sc_scale[i] != new_reg))
284 1.1 macallan i++;
285 1.1 macallan if (i > 7)
286 1.1 macallan return EINVAL;
287 1.1 macallan loongson_set_speed(i);
288 1.1 macallan return 0;
289 1.1 macallan }
290 1.1 macallan return EINVAL;
291 1.1 macallan }
292 1.1 macallan
293 1.1 macallan static int
294 1.1 macallan loongson_cpuspeed_cur(SYSCTLFN_ARGS)
295 1.1 macallan {
296 1.1 macallan struct sysctlnode node = *rnode;
297 1.1 macallan int mhz;
298 1.1 macallan
299 1.1 macallan mhz = sc_scale[sc_step];
300 1.1 macallan node.sysctl_data = &mhz;
301 1.1 macallan return sysctl_lookup(SYSCTLFN_CALL(&node));
302 1.1 macallan }
303 1.1 macallan
304 1.1 macallan static int
305 1.1 macallan loongson_cpuspeed_available(SYSCTLFN_ARGS)
306 1.1 macallan {
307 1.1 macallan struct sysctlnode node = *rnode;
308 1.1 macallan char buf[128];
309 1.1 macallan
310 1.1 macallan snprintf(buf, 128, "%d %d %d %d %d %d %d", sc_scale[1],
311 1.1 macallan sc_scale[2], sc_scale[3], sc_scale[4],
312 1.1 macallan sc_scale[5], sc_scale[6], sc_scale[7]);
313 1.1 macallan node.sysctl_data = buf;
314 1.1 macallan return(sysctl_lookup(SYSCTLFN_CALL(&node)));
315 1.1 macallan }
316 1.1 macallan
317 1.1 macallan /*
318 1.1 macallan * Wait for at least "n" microseconds.
319 1.1 macallan */
320 1.1 macallan void
321 1.1 macallan loongson_delay(int n)
322 1.1 macallan {
323 1.1 macallan u_long divisor_delay;
324 1.1 macallan uint32_t cur, last, delta, usecs;
325 1.1 macallan
326 1.1 macallan last = mips3_cp0_count_read();
327 1.1 macallan delta = usecs = 0;
328 1.1 macallan
329 1.1 macallan divisor_delay = rscale(curcpu()->ci_divisor_delay, sc_step);
330 1.1 macallan if (divisor_delay == 0) {
331 1.1 macallan /*
332 1.1 macallan * Frequency values in curcpu() are not initialized.
333 1.1 macallan * Assume faster frequency since longer delays are harmless.
334 1.1 macallan * Note CPU_MIPS_DOUBLE_COUNT is ignored here.
335 1.1 macallan */
336 1.1 macallan #define FAST_FREQ (300 * 1000 * 1000) /* fast enough? */
337 1.1 macallan divisor_delay = FAST_FREQ / (1000 * 1000);
338 1.1 macallan }
339 1.1 macallan
340 1.1 macallan while (n > usecs) {
341 1.1 macallan cur = mips3_cp0_count_read();
342 1.1 macallan
343 1.1 macallan /*
344 1.1 macallan * The MIPS3 CP0 counter always counts upto UINT32_MAX,
345 1.1 macallan * so no need to check wrapped around case.
346 1.1 macallan */
347 1.1 macallan delta += (cur - last);
348 1.1 macallan
349 1.1 macallan last = cur;
350 1.1 macallan
351 1.1 macallan while (delta >= divisor_delay) {
352 1.1 macallan /*
353 1.1 macallan * delta is not so larger than divisor_delay here,
354 1.1 macallan * and using DIV/DIVU ops could be much slower.
355 1.1 macallan * (though longer delay may be harmless)
356 1.1 macallan */
357 1.1 macallan usecs++;
358 1.1 macallan delta -= divisor_delay;
359 1.1 macallan }
360 1.1 macallan }
361 1.1 macallan }
362 1.1 macallan
363 1.1 macallan SYSCTL_SETUP(sysctl_ams_setup, "sysctl obio subtree setup")
364 1.1 macallan {
365 1.1 macallan
366 1.1 macallan sysctl_createv(NULL, 0, NULL, NULL,
367 1.1 macallan CTLFLAG_PERMANENT,
368 1.1 macallan CTLTYPE_NODE, "machdep", NULL,
369 1.1 macallan NULL, 0, NULL, 0,
370 1.1 macallan CTL_MACHDEP, CTL_EOL);
371 1.1 macallan }
372 1.1 macallan
373 1.1 macallan /*
374 1.1 macallan * We assume newhz is either stathz or profhz, and that neither will
375 1.1 macallan * change after being set up above. Could recalculate intervals here
376 1.1 macallan * but that would be a drag.
377 1.1 macallan */
378 1.1 macallan void
379 1.1 macallan loongson_setstatclockrate(int newhz)
380 1.1 macallan {
381 1.1 macallan
382 1.1 macallan /* nothing we can do */
383 1.1 macallan }
384 1.1 macallan
385 1.1 macallan __weak_alias(setstatclockrate, loongson_setstatclockrate);
386 1.1 macallan __weak_alias(cpu_initclocks, loongson_initclocks);
387 1.2 rin __weak_alias(delay, loongson_delay);
388