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