xref: /netbsd-src/sys/arch/arm/cortex/gtmr.c (revision 796c32c94f6e154afc9de0f63da35c91bb739b45)
1 /*	$NetBSD: gtmr.c,v 1.22 2017/10/25 16:09:46 skrll Exp $	*/
2 
3 /*-
4  * Copyright (c) 2012 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Matt Thomas
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: gtmr.c,v 1.22 2017/10/25 16:09:46 skrll Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/device.h>
38 #include <sys/intr.h>
39 #include <sys/kernel.h>
40 #include <sys/percpu.h>
41 #include <sys/proc.h>
42 #include <sys/systm.h>
43 #include <sys/timetc.h>
44 
45 #include <prop/proplib.h>
46 
47 #include <arm/locore.h>
48 
49 #include <arm/cortex/gtmr_var.h>
50 
51 #include <arm/cortex/mpcore_var.h>
52 
53 static int gtmr_match(device_t, cfdata_t, void *);
54 static void gtmr_attach(device_t, device_t, void *);
55 
56 static u_int gtmr_get_timecount(struct timecounter *);
57 
58 static struct gtmr_softc gtmr_sc;
59 
60 struct gtmr_percpu {
61 	uint32_t pc_delta;
62 };
63 
64 static struct timecounter gtmr_timecounter = {
65 	.tc_get_timecount = gtmr_get_timecount,
66 	.tc_poll_pps = 0,
67 	.tc_counter_mask = ~0u,
68 	.tc_frequency = 0,			/* set by cpu_initclocks() */
69 	.tc_name = NULL,			/* set by attach */
70 	.tc_quality = 500,
71 	.tc_priv = &gtmr_sc,
72 	.tc_next = NULL,
73 };
74 
75 CFATTACH_DECL_NEW(armgtmr, 0, gtmr_match, gtmr_attach, NULL, NULL);
76 
77 /* ARGSUSED */
78 static int
79 gtmr_match(device_t parent, cfdata_t cf, void *aux)
80 {
81 	struct mpcore_attach_args * const mpcaa = aux;
82 
83 	if (gtmr_sc.sc_dev != NULL)
84 		return 0;
85 
86 	if ((armreg_pfr1_read() & ARM_PFR1_GTIMER_MASK) == 0)
87 		return 0;
88 
89 	if (strcmp(mpcaa->mpcaa_name, cf->cf_name) != 0)
90 		return 0;
91 
92 	return 1;
93 }
94 
95 static void
96 gtmr_attach(device_t parent, device_t self, void *aux)
97 {
98 	struct mpcore_attach_args * const mpcaa = aux;
99 	struct gtmr_softc *sc = &gtmr_sc;
100 	prop_dictionary_t dict = device_properties(self);
101 	char freqbuf[sizeof("X.XXX SHz")];
102 
103 	/*
104 	 * This runs at a fixed frequency of 1 to 50MHz.
105 	 */
106 	if (!prop_dictionary_get_uint32(dict, "frequency", &sc->sc_freq))
107 		sc->sc_freq = armreg_cnt_frq_read();
108 
109 	KASSERT(sc->sc_freq != 0);
110 
111 	humanize_number(freqbuf, sizeof(freqbuf), sc->sc_freq, "Hz", 1000);
112 
113 	aprint_naive("\n");
114 	aprint_normal(": ARMv7 Generic 64-bit Timer (%s)\n", freqbuf);
115 
116 	/*
117 	 * Enable the virtual counter to be accessed from usermode.
118 	 */
119 	armreg_cntk_ctl_write(armreg_cntk_ctl_read() |
120 	    ARM_CNTKCTL_PL0VCTEN | ARM_CNTKCTL_PL0PCTEN);
121 
122 	self->dv_private = sc;
123 	sc->sc_dev = self;
124 
125 #ifdef DIAGNOSTIC
126 	sc->sc_percpu = percpu_alloc(sizeof(struct gtmr_percpu));
127 #endif
128 
129 	evcnt_attach_dynamic(&sc->sc_ev_missing_ticks, EVCNT_TYPE_MISC, NULL,
130 	    device_xname(self), "missing interrupts");
131 
132 	sc->sc_global_ih = intr_establish(mpcaa->mpcaa_irq, IPL_CLOCK,
133 	    IST_LEVEL | IST_MPSAFE, gtmr_intr, NULL);
134 	if (sc->sc_global_ih == NULL)
135 		panic("%s: unable to register timer interrupt", __func__);
136 	aprint_normal_dev(self, "interrupting on irq %d\n",
137 	    mpcaa->mpcaa_irq);
138 
139 	const uint32_t cnt_frq = armreg_cnt_frq_read();
140 	if (cnt_frq == 0) {
141 		aprint_verbose_dev(self, "cp15 CNT_FRQ not set\n");
142 	} else if (cnt_frq != sc->sc_freq) {
143 		aprint_verbose_dev(self,
144 		    "cp15 CNT_FRQ (%u) differs from supplied frequency\n",
145 		    cnt_frq);
146 	}
147 
148 	gtmr_timecounter.tc_name = device_xname(sc->sc_dev);
149 	gtmr_timecounter.tc_frequency = sc->sc_freq;
150 
151 	tc_init(&gtmr_timecounter);
152 
153 	/* Disable the timer until we are ready */
154 	armreg_cntv_ctl_write(0);
155 	armreg_cntp_ctl_write(0);
156 }
157 
158 void
159 gtmr_init_cpu_clock(struct cpu_info *ci)
160 {
161 	struct gtmr_softc * const sc = &gtmr_sc;
162 
163 	KASSERT(ci == curcpu());
164 
165 	int s = splsched();
166 
167 	/*
168 	 * enable timer and stop masking the timer.
169 	 */
170 	armreg_cntv_ctl_write(ARM_CNTCTL_ENABLE);
171 	armreg_cntp_ctl_write(ARM_CNTCTL_ENABLE);
172 #if 0
173 	printf("%s: cntctl=%#x\n", __func__, armreg_cntv_ctl_read());
174 #endif
175 
176 	/*
177 	 * Get now and update the compare timer.
178 	 */
179 	arm_isb();
180 	ci->ci_lastintr = armreg_cntv_ct_read();
181 	armreg_cntv_tval_write(sc->sc_autoinc);
182 #if 0
183 	printf("%s: %s: delta cval = %"PRIu64"\n",
184 	    __func__, ci->ci_data.cpu_name,
185 	    armreg_cntv_cval_read() - ci->ci_lastintr);
186 #endif
187 	splx(s);
188 	KASSERT(armreg_cntv_ct_read() != 0);
189 #if 0
190 	printf("%s: %s: ctl %#x cmp %#"PRIx64" now %#"PRIx64"\n",
191 	    __func__, ci->ci_data.cpu_name, armreg_cntv_ctl_read(),
192 	    armreg_cntv_cval_read(), armreg_cntv_ct_read());
193 
194 	s = splsched();
195 
196 	arm_isb();
197 	uint64_t now64;
198 	uint64_t start64 = armreg_cntv_ct_read();
199 	do {
200 		arm_isb();
201 		now64 = armreg_cntv_ct_read();
202 	} while (start64 == now64);
203 	start64 = now64;
204 	uint64_t end64 = start64 + 64;
205 	uint32_t start32 = armreg_pmccntr_read();
206 	do {
207 		arm_isb();
208 		now64 = armreg_cntv_ct_read();
209 	} while (end64 != now64);
210 	uint32_t end32 = armreg_pmccntr_read();
211 
212 	uint32_t diff32 = end64 - start64;
213 	printf("%s: %s: %u cycles per tick\n",
214 	    __func__, ci->ci_data.cpu_name, (end32 - start32) / diff32);
215 
216 	printf("%s: %s: status %#x cmp %#"PRIx64" now %#"PRIx64"\n",
217 	    __func__, ci->ci_data.cpu_name, armreg_cntv_ctl_read(),
218 	    armreg_cntv_cval_read(), armreg_cntv_ct_read());
219 	splx(s);
220 #elif 0
221 	delay(1000000 / hz + 1000);
222 #endif
223 }
224 
225 void
226 gtmr_cpu_initclocks(void)
227 {
228 	struct gtmr_softc * const sc = &gtmr_sc;
229 
230 	KASSERT(sc->sc_dev != NULL);
231 	KASSERT(sc->sc_freq != 0);
232 
233 	sc->sc_autoinc = sc->sc_freq / hz;
234 
235 	gtmr_init_cpu_clock(curcpu());
236 }
237 
238 void
239 gtmr_delay(unsigned int n)
240 {
241 	struct gtmr_softc * const sc = &gtmr_sc;
242 
243 	KASSERT(sc != NULL);
244 
245 	uint32_t freq = sc->sc_freq ? sc->sc_freq : armreg_cnt_frq_read();
246 	KASSERT(freq != 0);
247 
248 	const unsigned int incr_per_us = howmany(freq, 1000000);
249 	unsigned int delta = 0, usecs = 0;
250 
251 	arm_isb();
252 	uint64_t last = armreg_cntp_ct_read();
253 
254 	while (n > usecs) {
255 		arm_isb();
256 		uint64_t curr = armreg_cntp_ct_read();
257 		if (curr < last)
258 			delta += curr + (UINT64_MAX - last);
259 		else
260 			delta += curr - last;
261 
262 		last = curr;
263 		if (delta >= incr_per_us) {
264 			usecs += delta / incr_per_us;
265 			delta %= incr_per_us;
266 		}
267 	}
268 }
269 
270 void
271 gtmr_bootdelay(unsigned int ticks)
272 {
273 	const uint32_t ctl = armreg_cntv_ctl_read();
274 	armreg_cntv_ctl_write(ctl | ARM_CNTCTL_ENABLE | ARM_CNTCTL_IMASK);
275 
276 	/* Write Timer/Value to set new compare time */
277 	armreg_cntv_tval_write(ticks);
278 
279 	/* Spin until compare time is hit */
280 	while ((armreg_cntv_ctl_read() & ARM_CNTCTL_ISTATUS) == 0) {
281 		/* spin */
282 	}
283 
284 	armreg_cntv_ctl_write(ctl);
285 }
286 
287 /*
288  * gtmr_intr:
289  *
290  *	Handle the hardclock interrupt.
291  */
292 int
293 gtmr_intr(void *arg)
294 {
295 	struct cpu_info * const ci = curcpu();
296 	struct clockframe * const cf = arg;
297 	struct gtmr_softc * const sc = &gtmr_sc;
298 
299 	arm_isb();
300 
301 	const uint32_t ctl = armreg_cntv_ctl_read();
302 	if ((ctl & ARM_CNTCTL_ISTATUS) == 0)
303 		return 0;
304 
305 	const uint64_t now = armreg_cntv_ct_read();
306 	uint64_t delta = now - ci->ci_lastintr;
307 
308 #ifdef DIAGNOSTIC
309 	const uint64_t then = armreg_cntv_cval_read();
310 	struct gtmr_percpu * const pc = percpu_getref(sc->sc_percpu);
311 	KASSERTMSG(then <= now, "%"PRId64, now - then);
312 	KASSERTMSG(then + pc->pc_delta >= ci->ci_lastintr + sc->sc_autoinc,
313 	    "%"PRId64, then + pc->pc_delta - ci->ci_lastintr - sc->sc_autoinc);
314 #endif
315 
316 #if 0
317 	printf("%s(%p): %s: now %#"PRIx64" delta %"PRIu64"\n",
318 	     __func__, cf, ci->ci_data.cpu_name, now, delta);
319 #endif
320 	KASSERTMSG(delta > sc->sc_autoinc / 100,
321 	    "%s: interrupting too quickly (delta=%"PRIu64") autoinc=%lu",
322 	    ci->ci_data.cpu_name, delta, sc->sc_autoinc);
323 
324 	/*
325 	 * If we got interrupted too soon (delta < sc->sc_autoinc)
326 	 * or we missed (or almost missed) a tick
327 	 * (delta >= 7 * sc->sc_autoinc / 4), don't try to adjust for jitter.
328 	 */
329 	if (delta >= sc->sc_autoinc && delta <= 7 * sc->sc_autoinc / 4) {
330 		delta -= sc->sc_autoinc;
331 	} else {
332 		delta = 0;
333 	}
334 	armreg_cntv_tval_write(sc->sc_autoinc - delta);
335 
336 	ci->ci_lastintr = now;
337 
338 #ifdef DIAGNOSTIC
339 	KASSERT(delta == (uint32_t) delta);
340 	pc->pc_delta = delta;
341 	percpu_putref(sc->sc_percpu);
342 #endif
343 
344 	hardclock(cf);
345 
346 	sc->sc_ev_missing_ticks.ev_count += delta / sc->sc_autoinc;
347 
348 	return 1;
349 }
350 
351 void
352 setstatclockrate(int newhz)
353 {
354 }
355 
356 static u_int
357 gtmr_get_timecount(struct timecounter *tc)
358 {
359 	arm_isb();	// we want the time NOW, not some instructions later.
360 	return (u_int) armreg_cntp_ct_read();
361 }
362