Fri Jul 24 05:20:01 2015 UTC ()
KNF


(ryo)
diff -r1.13 -r1.14 src/sys/arch/arm/cortex/a9tmr.c

cvs diff -r1.13 -r1.14 src/sys/arch/arm/cortex/a9tmr.c (expand / switch to unified diff)

--- src/sys/arch/arm/cortex/a9tmr.c 2015/07/24 05:19:13 1.13
+++ src/sys/arch/arm/cortex/a9tmr.c 2015/07/24 05:20:01 1.14
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: a9tmr.c,v 1.13 2015/07/24 05:19:13 ryo Exp $ */ 1/* $NetBSD: a9tmr.c,v 1.14 2015/07/24 05:20:01 ryo Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2012 The NetBSD Foundation, Inc. 4 * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Thomas 8 * by Matt Thomas
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -20,27 +20,27 @@ @@ -20,27 +20,27 @@
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 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 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 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 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: a9tmr.c,v 1.13 2015/07/24 05:19:13 ryo Exp $"); 33__KERNEL_RCSID(0, "$NetBSD: a9tmr.c,v 1.14 2015/07/24 05:20:01 ryo Exp $");
34 34
35#include <sys/param.h> 35#include <sys/param.h>
36#include <sys/bus.h> 36#include <sys/bus.h>
37#include <sys/device.h> 37#include <sys/device.h>
38#include <sys/intr.h> 38#include <sys/intr.h>
39#include <sys/kernel.h> 39#include <sys/kernel.h>
40#include <sys/proc.h> 40#include <sys/proc.h>
41#include <sys/systm.h> 41#include <sys/systm.h>
42#include <sys/timetc.h> 42#include <sys/timetc.h>
43#include <sys/xcall.h> 43#include <sys/xcall.h>
44 44
45#include <prop/proplib.h> 45#include <prop/proplib.h>
46 46
@@ -106,27 +106,27 @@ a9tmr_match(device_t parent, cfdata_t cf @@ -106,27 +106,27 @@ a9tmr_match(device_t parent, cfdata_t cf
106 /* 106 /*
107 * This isn't present on UP A9s (since CBAR isn't present). 107 * This isn't present on UP A9s (since CBAR isn't present).
108 */ 108 */
109 uint32_t mpidr = armreg_mpidr_read(); 109 uint32_t mpidr = armreg_mpidr_read();
110 if (mpidr == 0 || (mpidr & MPIDR_U)) 110 if (mpidr == 0 || (mpidr & MPIDR_U))
111 return 0; 111 return 0;
112 112
113 return 1; 113 return 1;
114} 114}
115 115
116static void 116static void
117a9tmr_attach(device_t parent, device_t self, void *aux) 117a9tmr_attach(device_t parent, device_t self, void *aux)
118{ 118{
119 struct a9tmr_softc *sc = &a9tmr_sc; 119 struct a9tmr_softc *sc = &a9tmr_sc;
120 struct mpcore_attach_args * const mpcaa = aux; 120 struct mpcore_attach_args * const mpcaa = aux;
121 prop_dictionary_t dict = device_properties(self); 121 prop_dictionary_t dict = device_properties(self);
122 char freqbuf[sizeof("XXX SHz")]; 122 char freqbuf[sizeof("XXX SHz")];
123 const char *cpu_type; 123 const char *cpu_type;
124 124
125 /* 125 /*
126 * This runs at the ARM PERIPHCLOCK which should be 1/2 of the CPU clock. 126 * This runs at the ARM PERIPHCLOCK which should be 1/2 of the CPU clock.
127 * The MD code should have setup our frequency for us. 127 * The MD code should have setup our frequency for us.
128 */ 128 */
129 prop_dictionary_get_uint32(dict, "frequency", &sc->sc_freq); 129 prop_dictionary_get_uint32(dict, "frequency", &sc->sc_freq);
130 130
131 humanize_number(freqbuf, sizeof(freqbuf), sc->sc_freq, "Hz", 1000); 131 humanize_number(freqbuf, sizeof(freqbuf), sc->sc_freq, "Hz", 1000);
132 132
@@ -181,41 +181,43 @@ a9tmr_init_cpu_clock(struct cpu_info *ci @@ -181,41 +181,43 @@ a9tmr_init_cpu_clock(struct cpu_info *ci
181 uint64_t now = a9tmr_gettime(sc); 181 uint64_t now = a9tmr_gettime(sc);
182 182
183 KASSERT(ci == curcpu()); 183 KASSERT(ci == curcpu());
184 184
185 ci->ci_lastintr = now; 185 ci->ci_lastintr = now;
186 186
187 a9tmr_global_write(sc, TMR_GBL_AUTOINC, sc->sc_autoinc); 187 a9tmr_global_write(sc, TMR_GBL_AUTOINC, sc->sc_autoinc);
188 188
189 /* 189 /*
190 * To update the compare register we have to disable comparisions first. 190 * To update the compare register we have to disable comparisions first.
191 */ 191 */
192 uint32_t ctl = a9tmr_global_read(sc, TMR_GBL_CTL); 192 uint32_t ctl = a9tmr_global_read(sc, TMR_GBL_CTL);
193 if (ctl & TMR_GBL_CTL_CMP_ENABLE) { 193 if (ctl & TMR_GBL_CTL_CMP_ENABLE) {
194 a9tmr_global_write(sc, TMR_GBL_CTL, ctl & ~TMR_GBL_CTL_CMP_ENABLE); 194 a9tmr_global_write(sc, TMR_GBL_CTL,
 195 ctl & ~TMR_GBL_CTL_CMP_ENABLE);
195 } 196 }
196 197
197 /* 198 /*
198 * Schedule the next interrupt. 199 * Schedule the next interrupt.
199 */ 200 */
200 now += sc->sc_autoinc; 201 now += sc->sc_autoinc;
201 a9tmr_global_write(sc, TMR_GBL_CMP_L, (uint32_t) now); 202 a9tmr_global_write(sc, TMR_GBL_CMP_L, (uint32_t) now);
202 a9tmr_global_write(sc, TMR_GBL_CMP_H, (uint32_t) (now >> 32)); 203 a9tmr_global_write(sc, TMR_GBL_CMP_H, (uint32_t) (now >> 32));
203 204
204 /* 205 /*
205 * Re-enable the comparator and now enable interrupts. 206 * Re-enable the comparator and now enable interrupts.
206 */ 207 */
207 a9tmr_global_write(sc, TMR_GBL_INT, 1); /* clear interrupt pending */ 208 a9tmr_global_write(sc, TMR_GBL_INT, 1); /* clear interrupt pending */
208 ctl |= TMR_GBL_CTL_CMP_ENABLE | TMR_GBL_CTL_INT_ENABLE | TMR_GBL_CTL_AUTO_INC | TMR_CTL_ENABLE; 209 ctl |= TMR_GBL_CTL_CMP_ENABLE | TMR_GBL_CTL_INT_ENABLE |
 210 TMR_GBL_CTL_AUTO_INC | TMR_CTL_ENABLE;
209 a9tmr_global_write(sc, TMR_GBL_CTL, ctl); 211 a9tmr_global_write(sc, TMR_GBL_CTL, ctl);
210#if 0 212#if 0
211 printf("%s: %s: ctl %#x autoinc %u cmp %#x%08x now %#"PRIx64"\n", 213 printf("%s: %s: ctl %#x autoinc %u cmp %#x%08x now %#"PRIx64"\n",
212 __func__, ci->ci_data.cpu_name, 214 __func__, ci->ci_data.cpu_name,
213 a9tmr_global_read(sc, TMR_GBL_CTL), 215 a9tmr_global_read(sc, TMR_GBL_CTL),
214 a9tmr_global_read(sc, TMR_GBL_AUTOINC), 216 a9tmr_global_read(sc, TMR_GBL_AUTOINC),
215 a9tmr_global_read(sc, TMR_GBL_CMP_H), 217 a9tmr_global_read(sc, TMR_GBL_CMP_H),
216 a9tmr_global_read(sc, TMR_GBL_CMP_L), 218 a9tmr_global_read(sc, TMR_GBL_CMP_L),
217 a9tmr_gettime(sc)); 219 a9tmr_gettime(sc));
218 220
219 int s = splsched(); 221 int s = splsched();
220 uint64_t when = now; 222 uint64_t when = now;
221 u_int n = 0; 223 u_int n = 0;
@@ -232,27 +234,27 @@ a9tmr_init_cpu_clock(struct cpu_info *ci @@ -232,27 +234,27 @@ a9tmr_init_cpu_clock(struct cpu_info *ci
232 a9tmr_global_read(sc, TMR_GBL_CMP_H), 234 a9tmr_global_read(sc, TMR_GBL_CMP_H),
233 a9tmr_global_read(sc, TMR_GBL_CMP_L), 235 a9tmr_global_read(sc, TMR_GBL_CMP_L),
234 a9tmr_gettime(sc)); 236 a9tmr_gettime(sc));
235 splx(s); 237 splx(s);
236#elif 0 238#elif 0
237 delay(1000000 / hz + 1000);  239 delay(1000000 / hz + 1000);
238#endif 240#endif
239} 241}
240 242
241void 243void
242cpu_initclocks(void) 244cpu_initclocks(void)
243{ 245{
244 struct a9tmr_softc * const sc = &a9tmr_sc; 246 struct a9tmr_softc * const sc = &a9tmr_sc;
245  247
246 KASSERT(sc->sc_dev != NULL); 248 KASSERT(sc->sc_dev != NULL);
247 KASSERT(sc->sc_freq != 0); 249 KASSERT(sc->sc_freq != 0);
248 250
249 sc->sc_autoinc = sc->sc_freq / hz; 251 sc->sc_autoinc = sc->sc_freq / hz;
250 252
251 a9tmr_init_cpu_clock(curcpu()); 253 a9tmr_init_cpu_clock(curcpu());
252 254
253 a9tmr_timecounter.tc_name = device_xname(sc->sc_dev); 255 a9tmr_timecounter.tc_name = device_xname(sc->sc_dev);
254 a9tmr_timecounter.tc_frequency = sc->sc_freq; 256 a9tmr_timecounter.tc_frequency = sc->sc_freq;
255 257
256 tc_init(&a9tmr_timecounter); 258 tc_init(&a9tmr_timecounter);
257} 259}
258 260
@@ -280,27 +282,28 @@ a9tmr_update_freq(uint32_t freq) @@ -280,27 +282,28 @@ a9tmr_update_freq(uint32_t freq)
280 xc_wait(xc); 282 xc_wait(xc);
281 283
282 a9tmr_timecounter.tc_frequency = sc->sc_freq; 284 a9tmr_timecounter.tc_frequency = sc->sc_freq;
283 tc_init(&a9tmr_timecounter); 285 tc_init(&a9tmr_timecounter);
284} 286}
285 287
286void 288void
287a9tmr_delay(unsigned int n) 289a9tmr_delay(unsigned int n)
288{ 290{
289 struct a9tmr_softc * const sc = &a9tmr_sc; 291 struct a9tmr_softc * const sc = &a9tmr_sc;
290 292
291 KASSERT(sc != NULL); 293 KASSERT(sc != NULL);
292 294
293 uint32_t freq = sc->sc_freq ? sc->sc_freq : curcpu()->ci_data.cpu_cc_freq / 2; 295 uint32_t freq = sc->sc_freq ? sc->sc_freq :
 296 curcpu()->ci_data.cpu_cc_freq / 2;
294 KASSERT(freq != 0); 297 KASSERT(freq != 0);
295 298
296 /* 299 /*
297 * not quite divide by 1000000 but close enough 300 * not quite divide by 1000000 but close enough
298 * (higher by 1.3% which means we wait 1.3% longer). 301 * (higher by 1.3% which means we wait 1.3% longer).
299 */ 302 */
300 const uint64_t incr_per_us = (freq >> 20) + (freq >> 24); 303 const uint64_t incr_per_us = (freq >> 20) + (freq >> 24);
301 304
302 const uint64_t delta = n * incr_per_us; 305 const uint64_t delta = n * incr_per_us;
303 const uint64_t base = a9tmr_gettime(sc); 306 const uint64_t base = a9tmr_gettime(sc);
304 const uint64_t finish = base + delta; 307 const uint64_t finish = base + delta;
305 308
306 while (a9tmr_gettime(sc) < finish) { 309 while (a9tmr_gettime(sc) < finish) {
@@ -309,31 +312,31 @@ a9tmr_delay(unsigned int n) @@ -309,31 +312,31 @@ a9tmr_delay(unsigned int n)
309} 312}
310 313
311/* 314/*
312 * clockhandler: 315 * clockhandler:
313 * 316 *
314 * Handle the hardclock interrupt. 317 * Handle the hardclock interrupt.
315 */ 318 */
316static int 319static int
317clockhandler(void *arg) 320clockhandler(void *arg)
318{ 321{
319 struct clockframe * const cf = arg; 322 struct clockframe * const cf = arg;
320 struct a9tmr_softc * const sc = &a9tmr_sc; 323 struct a9tmr_softc * const sc = &a9tmr_sc;
321 struct cpu_info * const ci = curcpu(); 324 struct cpu_info * const ci = curcpu();
322  325
323 const uint64_t now = a9tmr_gettime(sc); 326 const uint64_t now = a9tmr_gettime(sc);
324 uint64_t delta = now - ci->ci_lastintr; 327 uint64_t delta = now - ci->ci_lastintr;
325 328
326 a9tmr_global_write(sc, TMR_GBL_INT, 1); // Ack the interrupt 329 a9tmr_global_write(sc, TMR_GBL_INT, 1); /* Ack the interrupt */
327 330
328#if 0 331#if 0
329 printf("%s(%p): %s: now %#"PRIx64" delta %"PRIu64"\n",  332 printf("%s(%p): %s: now %#"PRIx64" delta %"PRIu64"\n",
330 __func__, cf, ci->ci_data.cpu_name, now, delta); 333 __func__, cf, ci->ci_data.cpu_name, now, delta);
331#endif 334#endif
332 KASSERTMSG(delta > sc->sc_autoinc / 64, 335 KASSERTMSG(delta > sc->sc_autoinc / 64,
333 "%s: interrupting too quickly (delta=%"PRIu64")", 336 "%s: interrupting too quickly (delta=%"PRIu64")",
334 ci->ci_data.cpu_name, delta); 337 ci->ci_data.cpu_name, delta);
335 338
336 ci->ci_lastintr = now; 339 ci->ci_lastintr = now;
337 340
338 hardclock(cf); 341 hardclock(cf);
339 342