]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/clocksource/arm_arch_timer.c
1c46d9ac6f21fca4d5519ad300603c5794f052a5
[mirror_ubuntu-zesty-kernel.git] / drivers / clocksource / arm_arch_timer.c
1 /*
2 * linux/drivers/clocksource/arm_arch_timer.c
3 *
4 * Copyright (C) 2011 ARM Ltd.
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12 #define pr_fmt(fmt) "arm_arch_timer: " fmt
13
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/device.h>
17 #include <linux/smp.h>
18 #include <linux/cpu.h>
19 #include <linux/cpu_pm.h>
20 #include <linux/clockchips.h>
21 #include <linux/clocksource.h>
22 #include <linux/interrupt.h>
23 #include <linux/of_irq.h>
24 #include <linux/of_address.h>
25 #include <linux/io.h>
26 #include <linux/slab.h>
27 #include <linux/sched_clock.h>
28 #include <linux/acpi.h>
29
30 #include <asm/arch_timer.h>
31 #include <asm/virt.h>
32
33 #include <clocksource/arm_arch_timer.h>
34
35 #undef pr_fmt
36 #define pr_fmt(fmt) "arch_timer: " fmt
37
38 #define CNTTIDR 0x08
39 #define CNTTIDR_VIRT(n) (BIT(1) << ((n) * 4))
40
41 #define CNTACR(n) (0x40 + ((n) * 4))
42 #define CNTACR_RPCT BIT(0)
43 #define CNTACR_RVCT BIT(1)
44 #define CNTACR_RFRQ BIT(2)
45 #define CNTACR_RVOFF BIT(3)
46 #define CNTACR_RWVT BIT(4)
47 #define CNTACR_RWPT BIT(5)
48
49 #define CNTVCT_LO 0x08
50 #define CNTVCT_HI 0x0c
51 #define CNTFRQ 0x10
52 #define CNTP_TVAL 0x28
53 #define CNTP_CTL 0x2c
54 #define CNTV_TVAL 0x38
55 #define CNTV_CTL 0x3c
56
57 static unsigned arch_timers_present __initdata;
58
59 static void __iomem *arch_counter_base;
60
61 struct arch_timer {
62 void __iomem *base;
63 struct clock_event_device evt;
64 };
65
66 #define to_arch_timer(e) container_of(e, struct arch_timer, evt)
67
68 static u32 arch_timer_rate;
69 static int arch_timer_ppi[ARCH_TIMER_MAX_TIMER_PPI];
70
71 static struct clock_event_device __percpu *arch_timer_evt;
72
73 static enum arch_timer_ppi_nr arch_timer_uses_ppi = ARCH_TIMER_VIRT_PPI;
74 static bool arch_timer_c3stop;
75 static bool arch_timer_mem_use_virtual;
76 static bool arch_counter_suspend_stop;
77 static bool vdso_default = true;
78
79 static bool evtstrm_enable = IS_ENABLED(CONFIG_ARM_ARCH_TIMER_EVTSTREAM);
80
81 static int __init early_evtstrm_cfg(char *buf)
82 {
83 return strtobool(buf, &evtstrm_enable);
84 }
85 early_param("clocksource.arm_arch_timer.evtstrm", early_evtstrm_cfg);
86
87 /*
88 * Architected system timer support.
89 */
90
91 static __always_inline
92 void arch_timer_reg_write(int access, enum arch_timer_reg reg, u32 val,
93 struct clock_event_device *clk)
94 {
95 if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
96 struct arch_timer *timer = to_arch_timer(clk);
97 switch (reg) {
98 case ARCH_TIMER_REG_CTRL:
99 writel_relaxed(val, timer->base + CNTP_CTL);
100 break;
101 case ARCH_TIMER_REG_TVAL:
102 writel_relaxed(val, timer->base + CNTP_TVAL);
103 break;
104 }
105 } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
106 struct arch_timer *timer = to_arch_timer(clk);
107 switch (reg) {
108 case ARCH_TIMER_REG_CTRL:
109 writel_relaxed(val, timer->base + CNTV_CTL);
110 break;
111 case ARCH_TIMER_REG_TVAL:
112 writel_relaxed(val, timer->base + CNTV_TVAL);
113 break;
114 }
115 } else {
116 arch_timer_reg_write_cp15(access, reg, val);
117 }
118 }
119
120 static __always_inline
121 u32 arch_timer_reg_read(int access, enum arch_timer_reg reg,
122 struct clock_event_device *clk)
123 {
124 u32 val;
125
126 if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
127 struct arch_timer *timer = to_arch_timer(clk);
128 switch (reg) {
129 case ARCH_TIMER_REG_CTRL:
130 val = readl_relaxed(timer->base + CNTP_CTL);
131 break;
132 case ARCH_TIMER_REG_TVAL:
133 val = readl_relaxed(timer->base + CNTP_TVAL);
134 break;
135 }
136 } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
137 struct arch_timer *timer = to_arch_timer(clk);
138 switch (reg) {
139 case ARCH_TIMER_REG_CTRL:
140 val = readl_relaxed(timer->base + CNTV_CTL);
141 break;
142 case ARCH_TIMER_REG_TVAL:
143 val = readl_relaxed(timer->base + CNTV_TVAL);
144 break;
145 }
146 } else {
147 val = arch_timer_reg_read_cp15(access, reg);
148 }
149
150 return val;
151 }
152
153 /*
154 * Default to cp15 based access because arm64 uses this function for
155 * sched_clock() before DT is probed and the cp15 method is guaranteed
156 * to exist on arm64. arm doesn't use this before DT is probed so even
157 * if we don't have the cp15 accessors we won't have a problem.
158 */
159 u64 (*arch_timer_read_counter)(void) = arch_counter_get_cntvct;
160
161 static u64 arch_counter_read(struct clocksource *cs)
162 {
163 return arch_timer_read_counter();
164 }
165
166 static u64 arch_counter_read_cc(const struct cyclecounter *cc)
167 {
168 return arch_timer_read_counter();
169 }
170
171 static struct clocksource clocksource_counter = {
172 .name = "arch_sys_counter",
173 .rating = 400,
174 .read = arch_counter_read,
175 .mask = CLOCKSOURCE_MASK(56),
176 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
177 };
178
179 static struct cyclecounter cyclecounter = {
180 .read = arch_counter_read_cc,
181 .mask = CLOCKSOURCE_MASK(56),
182 };
183
184 struct ate_acpi_oem_info {
185 char oem_id[ACPI_OEM_ID_SIZE + 1];
186 char oem_table_id[ACPI_OEM_TABLE_ID_SIZE + 1];
187 u32 oem_revision;
188 };
189
190 #ifdef CONFIG_FSL_ERRATUM_A008585
191 /*
192 * The number of retries is an arbitrary value well beyond the highest number
193 * of iterations the loop has been observed to take.
194 */
195 #define __fsl_a008585_read_reg(reg) ({ \
196 u64 _old, _new; \
197 int _retries = 200; \
198 \
199 do { \
200 _old = read_sysreg(reg); \
201 _new = read_sysreg(reg); \
202 _retries--; \
203 } while (unlikely(_old != _new) && _retries); \
204 \
205 WARN_ON_ONCE(!_retries); \
206 _new; \
207 })
208
209 static u32 notrace fsl_a008585_read_cntp_tval_el0(void)
210 {
211 return __fsl_a008585_read_reg(cntp_tval_el0);
212 }
213
214 static u32 notrace fsl_a008585_read_cntv_tval_el0(void)
215 {
216 return __fsl_a008585_read_reg(cntv_tval_el0);
217 }
218
219 static u64 notrace fsl_a008585_read_cntvct_el0(void)
220 {
221 return __fsl_a008585_read_reg(cntvct_el0);
222 }
223 #endif
224
225 #ifdef CONFIG_HISILICON_ERRATUM_161010101
226 /*
227 * Verify whether the value of the second read is larger than the first by
228 * less than 32 is the only way to confirm the value is correct, so clear the
229 * lower 5 bits to check whether the difference is greater than 32 or not.
230 * Theoretically the erratum should not occur more than twice in succession
231 * when reading the system counter, but it is possible that some interrupts
232 * may lead to more than twice read errors, triggering the warning, so setting
233 * the number of retries far beyond the number of iterations the loop has been
234 * observed to take.
235 */
236 #define __hisi_161010101_read_reg(reg) ({ \
237 u64 _old, _new; \
238 int _retries = 50; \
239 \
240 do { \
241 _old = read_sysreg(reg); \
242 _new = read_sysreg(reg); \
243 _retries--; \
244 } while (unlikely((_new - _old) >> 5) && _retries); \
245 \
246 WARN_ON_ONCE(!_retries); \
247 _new; \
248 })
249
250 static u32 notrace hisi_161010101_read_cntp_tval_el0(void)
251 {
252 return __hisi_161010101_read_reg(cntp_tval_el0);
253 }
254
255 static u32 notrace hisi_161010101_read_cntv_tval_el0(void)
256 {
257 return __hisi_161010101_read_reg(cntv_tval_el0);
258 }
259
260 static u64 notrace hisi_161010101_read_cntvct_el0(void)
261 {
262 return __hisi_161010101_read_reg(cntvct_el0);
263 }
264
265 static struct ate_acpi_oem_info hisi_161010101_oem_info[] = {
266 /*
267 * Note that trailing spaces are required to properly match
268 * the OEM table information.
269 */
270 {
271 .oem_id = "HISI ",
272 .oem_table_id = "HIP05 ",
273 .oem_revision = 0,
274 },
275 {
276 .oem_id = "HISI ",
277 .oem_table_id = "HIP06 ",
278 .oem_revision = 0,
279 },
280 {
281 .oem_id = "HISI ",
282 .oem_table_id = "HIP07 ",
283 .oem_revision = 0,
284 },
285 { /* Sentinel indicating the end of the OEM array */ },
286 };
287 #endif
288
289 #ifdef CONFIG_ARM64_ERRATUM_858921
290 static u64 notrace arm64_858921_read_cntvct_el0(void)
291 {
292 u64 old, new;
293
294 old = read_sysreg(cntvct_el0);
295 new = read_sysreg(cntvct_el0);
296 return (((old ^ new) >> 32) & 1) ? old : new;
297 }
298 #endif
299
300 #ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
301 DEFINE_PER_CPU(const struct arch_timer_erratum_workaround *,
302 timer_unstable_counter_workaround);
303 EXPORT_SYMBOL_GPL(timer_unstable_counter_workaround);
304
305 DEFINE_STATIC_KEY_FALSE(arch_timer_read_ool_enabled);
306 EXPORT_SYMBOL_GPL(arch_timer_read_ool_enabled);
307
308 static void erratum_set_next_event_tval_generic(const int access, unsigned long evt,
309 struct clock_event_device *clk)
310 {
311 unsigned long ctrl;
312 u64 cval = evt + arch_counter_get_cntvct();
313
314 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
315 ctrl |= ARCH_TIMER_CTRL_ENABLE;
316 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
317
318 if (access == ARCH_TIMER_PHYS_ACCESS)
319 write_sysreg(cval, cntp_cval_el0);
320 else
321 write_sysreg(cval, cntv_cval_el0);
322
323 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
324 }
325
326 static int erratum_set_next_event_tval_virt(unsigned long evt,
327 struct clock_event_device *clk)
328 {
329 erratum_set_next_event_tval_generic(ARCH_TIMER_VIRT_ACCESS, evt, clk);
330 return 0;
331 }
332
333 static int erratum_set_next_event_tval_phys(unsigned long evt,
334 struct clock_event_device *clk)
335 {
336 erratum_set_next_event_tval_generic(ARCH_TIMER_PHYS_ACCESS, evt, clk);
337 return 0;
338 }
339
340 static const struct arch_timer_erratum_workaround ool_workarounds[] = {
341 #ifdef CONFIG_FSL_ERRATUM_A008585
342 {
343 .match_type = ate_match_dt,
344 .id = "fsl,erratum-a008585",
345 .desc = "Freescale erratum a005858",
346 .read_cntp_tval_el0 = fsl_a008585_read_cntp_tval_el0,
347 .read_cntv_tval_el0 = fsl_a008585_read_cntv_tval_el0,
348 .read_cntvct_el0 = fsl_a008585_read_cntvct_el0,
349 .set_next_event_phys = erratum_set_next_event_tval_phys,
350 .set_next_event_virt = erratum_set_next_event_tval_virt,
351 },
352 #endif
353 #ifdef CONFIG_HISILICON_ERRATUM_161010101
354 {
355 .match_type = ate_match_dt,
356 .id = "hisilicon,erratum-161010101",
357 .desc = "HiSilicon erratum 161010101",
358 .read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
359 .read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
360 .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
361 .set_next_event_phys = erratum_set_next_event_tval_phys,
362 .set_next_event_virt = erratum_set_next_event_tval_virt,
363 },
364 {
365 .match_type = ate_match_acpi_oem_info,
366 .id = hisi_161010101_oem_info,
367 .desc = "HiSilicon erratum 161010101",
368 .read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
369 .read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
370 .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
371 .set_next_event_phys = erratum_set_next_event_tval_phys,
372 .set_next_event_virt = erratum_set_next_event_tval_virt,
373 },
374 #endif
375 #ifdef CONFIG_ARM64_ERRATUM_858921
376 {
377 .match_type = ate_match_local_cap_id,
378 .id = (void *)ARM64_WORKAROUND_858921,
379 .desc = "ARM erratum 858921",
380 .read_cntvct_el0 = arm64_858921_read_cntvct_el0,
381 },
382 #endif
383 };
384
385 typedef bool (*ate_match_fn_t)(const struct arch_timer_erratum_workaround *,
386 const void *);
387
388 static
389 bool arch_timer_check_dt_erratum(const struct arch_timer_erratum_workaround *wa,
390 const void *arg)
391 {
392 const struct device_node *np = arg;
393
394 return of_property_read_bool(np, wa->id);
395 }
396
397 static
398 bool arch_timer_check_global_cap_erratum(const struct arch_timer_erratum_workaround *wa,
399 const void *arg)
400 {
401 return cpus_have_cap((uintptr_t)wa->id);
402 }
403
404 static
405 bool arch_timer_check_local_cap_erratum(const struct arch_timer_erratum_workaround *wa,
406 const void *arg)
407 {
408 return this_cpu_has_cap((uintptr_t)wa->id);
409 }
410
411
412 static
413 bool arch_timer_check_acpi_oem_erratum(const struct arch_timer_erratum_workaround *wa,
414 const void *arg)
415 {
416 static const struct ate_acpi_oem_info empty_oem_info = {};
417 const struct ate_acpi_oem_info *info = wa->id;
418 const struct acpi_table_header *table = arg;
419
420 /* Iterate over the ACPI OEM info array, looking for a match */
421 while (memcmp(info, &empty_oem_info, sizeof(*info))) {
422 if (!memcmp(info->oem_id, table->oem_id, ACPI_OEM_ID_SIZE) &&
423 !memcmp(info->oem_table_id, table->oem_table_id, ACPI_OEM_TABLE_ID_SIZE) &&
424 info->oem_revision == table->oem_revision)
425 return true;
426
427 info++;
428 }
429
430 return false;
431 }
432
433 static const struct arch_timer_erratum_workaround *
434 arch_timer_iterate_errata(enum arch_timer_erratum_match_type type,
435 ate_match_fn_t match_fn,
436 void *arg)
437 {
438 int i;
439
440 for (i = 0; i < ARRAY_SIZE(ool_workarounds); i++) {
441 if (ool_workarounds[i].match_type != type)
442 continue;
443
444 if (match_fn(&ool_workarounds[i], arg))
445 return &ool_workarounds[i];
446 }
447
448 return NULL;
449 }
450
451 static
452 void arch_timer_enable_workaround(const struct arch_timer_erratum_workaround *wa,
453 bool local)
454 {
455 int i;
456
457 if (local) {
458 __this_cpu_write(timer_unstable_counter_workaround, wa);
459 } else {
460 for_each_possible_cpu(i)
461 per_cpu(timer_unstable_counter_workaround, i) = wa;
462 }
463
464 static_branch_enable(&arch_timer_read_ool_enabled);
465
466 /*
467 * Don't use the vdso fastpath if errata require using the
468 * out-of-line counter accessor. We may change our mind pretty
469 * late in the game (with a per-CPU erratum, for example), so
470 * change both the default value and the vdso itself.
471 */
472 if (wa->read_cntvct_el0) {
473 clocksource_counter.archdata.vdso_direct = false;
474 vdso_default = false;
475 }
476 }
477
478 static void arch_timer_check_ool_workaround(enum arch_timer_erratum_match_type type,
479 void *arg)
480 {
481 const struct arch_timer_erratum_workaround *wa;
482 ate_match_fn_t match_fn = NULL;
483 bool local = false;
484
485 switch (type) {
486 case ate_match_dt:
487 match_fn = arch_timer_check_dt_erratum;
488 break;
489 case ate_match_global_cap_id:
490 match_fn = arch_timer_check_global_cap_erratum;
491 break;
492 case ate_match_local_cap_id:
493 match_fn = arch_timer_check_local_cap_erratum;
494 local = true;
495 break;
496 case ate_match_acpi_oem_info:
497 match_fn = arch_timer_check_acpi_oem_erratum;
498 break;
499 default:
500 pr_err("arch_timer: Unknown erratum workaround type specified.\n");
501 return;
502 }
503
504 wa = arch_timer_iterate_errata(type, match_fn, arg);
505 if (!wa)
506 return;
507
508 if (static_branch_unlikely(&arch_timer_read_ool_enabled)) {
509 const struct arch_timer_erratum_workaround *__wa;
510 __wa = __this_cpu_read(timer_unstable_counter_workaround);
511 if (__wa && wa != __wa)
512 pr_warn("Can't enable workaround for %s (clashes with %s\n)",
513 wa->desc, __wa->desc);
514
515 if (__wa)
516 return;
517 }
518
519 arch_timer_enable_workaround(wa, local);
520 pr_info("Enabling %s workaround for %s\n",
521 local ? "local" : "global", wa->desc);
522 }
523
524 #define erratum_handler(fn, r, ...) \
525 ({ \
526 bool __val; \
527 if (needs_unstable_timer_counter_workaround()) { \
528 const struct arch_timer_erratum_workaround *__wa; \
529 __wa = __this_cpu_read(timer_unstable_counter_workaround); \
530 if (__wa && __wa->fn) { \
531 r = __wa->fn(__VA_ARGS__); \
532 __val = true; \
533 } else { \
534 __val = false; \
535 } \
536 } else { \
537 __val = false; \
538 } \
539 __val; \
540 })
541
542 static bool arch_timer_this_cpu_has_cntvct_wa(void)
543 {
544 const struct arch_timer_erratum_workaround *wa;
545
546 wa = __this_cpu_read(timer_unstable_counter_workaround);
547 return wa && wa->read_cntvct_el0;
548 }
549 #else
550 #define arch_timer_check_ool_workaround(t,a) do { } while(0)
551 #define erratum_set_next_event_tval_virt(...) ({BUG(); 0;})
552 #define erratum_set_next_event_tval_phys(...) ({BUG(); 0;})
553 #define erratum_handler(fn, r, ...) ({false;})
554 #define arch_timer_this_cpu_has_cntvct_wa() ({false;})
555 #endif /* CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND */
556
557 static __always_inline irqreturn_t timer_handler(const int access,
558 struct clock_event_device *evt)
559 {
560 unsigned long ctrl;
561
562 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, evt);
563 if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
564 ctrl |= ARCH_TIMER_CTRL_IT_MASK;
565 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, evt);
566 evt->event_handler(evt);
567 return IRQ_HANDLED;
568 }
569
570 return IRQ_NONE;
571 }
572
573 static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
574 {
575 struct clock_event_device *evt = dev_id;
576
577 return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
578 }
579
580 static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
581 {
582 struct clock_event_device *evt = dev_id;
583
584 return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
585 }
586
587 static irqreturn_t arch_timer_handler_phys_mem(int irq, void *dev_id)
588 {
589 struct clock_event_device *evt = dev_id;
590
591 return timer_handler(ARCH_TIMER_MEM_PHYS_ACCESS, evt);
592 }
593
594 static irqreturn_t arch_timer_handler_virt_mem(int irq, void *dev_id)
595 {
596 struct clock_event_device *evt = dev_id;
597
598 return timer_handler(ARCH_TIMER_MEM_VIRT_ACCESS, evt);
599 }
600
601 static __always_inline int timer_shutdown(const int access,
602 struct clock_event_device *clk)
603 {
604 unsigned long ctrl;
605
606 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
607 ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
608 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
609
610 return 0;
611 }
612
613 static int arch_timer_shutdown_virt(struct clock_event_device *clk)
614 {
615 return timer_shutdown(ARCH_TIMER_VIRT_ACCESS, clk);
616 }
617
618 static int arch_timer_shutdown_phys(struct clock_event_device *clk)
619 {
620 return timer_shutdown(ARCH_TIMER_PHYS_ACCESS, clk);
621 }
622
623 static int arch_timer_shutdown_virt_mem(struct clock_event_device *clk)
624 {
625 return timer_shutdown(ARCH_TIMER_MEM_VIRT_ACCESS, clk);
626 }
627
628 static int arch_timer_shutdown_phys_mem(struct clock_event_device *clk)
629 {
630 return timer_shutdown(ARCH_TIMER_MEM_PHYS_ACCESS, clk);
631 }
632
633 static __always_inline void set_next_event(const int access, unsigned long evt,
634 struct clock_event_device *clk)
635 {
636 unsigned long ctrl;
637 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
638 ctrl |= ARCH_TIMER_CTRL_ENABLE;
639 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
640 arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt, clk);
641 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
642 }
643
644 static int arch_timer_set_next_event_virt(unsigned long evt,
645 struct clock_event_device *clk)
646 {
647 int ret;
648
649 if (erratum_handler(set_next_event_virt, ret, evt, clk))
650 return ret;
651
652 set_next_event(ARCH_TIMER_VIRT_ACCESS, evt, clk);
653 return 0;
654 }
655
656 static int arch_timer_set_next_event_phys(unsigned long evt,
657 struct clock_event_device *clk)
658 {
659 int ret;
660
661 if (erratum_handler(set_next_event_phys, ret, evt, clk))
662 return ret;
663
664 set_next_event(ARCH_TIMER_PHYS_ACCESS, evt, clk);
665 return 0;
666 }
667
668 static int arch_timer_set_next_event_virt_mem(unsigned long evt,
669 struct clock_event_device *clk)
670 {
671 set_next_event(ARCH_TIMER_MEM_VIRT_ACCESS, evt, clk);
672 return 0;
673 }
674
675 static int arch_timer_set_next_event_phys_mem(unsigned long evt,
676 struct clock_event_device *clk)
677 {
678 set_next_event(ARCH_TIMER_MEM_PHYS_ACCESS, evt, clk);
679 return 0;
680 }
681
682 static void __arch_timer_setup(unsigned type,
683 struct clock_event_device *clk)
684 {
685 clk->features = CLOCK_EVT_FEAT_ONESHOT;
686
687 if (type == ARCH_TIMER_TYPE_CP15) {
688 if (arch_timer_c3stop)
689 clk->features |= CLOCK_EVT_FEAT_C3STOP;
690 clk->name = "arch_sys_timer";
691 clk->rating = 450;
692 clk->cpumask = cpumask_of(smp_processor_id());
693 clk->irq = arch_timer_ppi[arch_timer_uses_ppi];
694 switch (arch_timer_uses_ppi) {
695 case ARCH_TIMER_VIRT_PPI:
696 clk->set_state_shutdown = arch_timer_shutdown_virt;
697 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt;
698 clk->set_next_event = arch_timer_set_next_event_virt;
699 break;
700 case ARCH_TIMER_PHYS_SECURE_PPI:
701 case ARCH_TIMER_PHYS_NONSECURE_PPI:
702 case ARCH_TIMER_HYP_PPI:
703 clk->set_state_shutdown = arch_timer_shutdown_phys;
704 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys;
705 clk->set_next_event = arch_timer_set_next_event_phys;
706 break;
707 default:
708 BUG();
709 }
710
711 arch_timer_check_ool_workaround(ate_match_local_cap_id, NULL);
712 } else {
713 clk->features |= CLOCK_EVT_FEAT_DYNIRQ;
714 clk->name = "arch_mem_timer";
715 clk->rating = 400;
716 clk->cpumask = cpu_all_mask;
717 if (arch_timer_mem_use_virtual) {
718 clk->set_state_shutdown = arch_timer_shutdown_virt_mem;
719 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt_mem;
720 clk->set_next_event =
721 arch_timer_set_next_event_virt_mem;
722 } else {
723 clk->set_state_shutdown = arch_timer_shutdown_phys_mem;
724 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys_mem;
725 clk->set_next_event =
726 arch_timer_set_next_event_phys_mem;
727 }
728 }
729
730 clk->set_state_shutdown(clk);
731
732 clockevents_config_and_register(clk, arch_timer_rate, 0xf, 0x7fffffff);
733 }
734
735 static void arch_timer_evtstrm_enable(int divider)
736 {
737 u32 cntkctl = arch_timer_get_cntkctl();
738
739 cntkctl &= ~ARCH_TIMER_EVT_TRIGGER_MASK;
740 /* Set the divider and enable virtual event stream */
741 cntkctl |= (divider << ARCH_TIMER_EVT_TRIGGER_SHIFT)
742 | ARCH_TIMER_VIRT_EVT_EN;
743 arch_timer_set_cntkctl(cntkctl);
744 elf_hwcap |= HWCAP_EVTSTRM;
745 #ifdef CONFIG_COMPAT
746 compat_elf_hwcap |= COMPAT_HWCAP_EVTSTRM;
747 #endif
748 }
749
750 static void arch_timer_configure_evtstream(void)
751 {
752 int evt_stream_div, pos;
753
754 /* Find the closest power of two to the divisor */
755 evt_stream_div = arch_timer_rate / ARCH_TIMER_EVT_STREAM_FREQ;
756 pos = fls(evt_stream_div);
757 if (pos > 1 && !(evt_stream_div & (1 << (pos - 2))))
758 pos--;
759 /* enable event stream */
760 arch_timer_evtstrm_enable(min(pos, 15));
761 }
762
763 static void arch_counter_set_user_access(void)
764 {
765 u32 cntkctl = arch_timer_get_cntkctl();
766
767 /* Disable user access to the timers and both counters */
768 /* Also disable virtual event stream */
769 cntkctl &= ~(ARCH_TIMER_USR_PT_ACCESS_EN
770 | ARCH_TIMER_USR_VT_ACCESS_EN
771 | ARCH_TIMER_USR_VCT_ACCESS_EN
772 | ARCH_TIMER_VIRT_EVT_EN
773 | ARCH_TIMER_USR_PCT_ACCESS_EN);
774
775 /*
776 * Enable user access to the virtual counter if it doesn't
777 * need to be workaround. The vdso may have been already
778 * disabled though.
779 */
780 if (arch_timer_this_cpu_has_cntvct_wa())
781 pr_info("CPU%d: Trapping CNTVCT access\n", smp_processor_id());
782 else
783 cntkctl |= ARCH_TIMER_USR_VCT_ACCESS_EN;
784
785 arch_timer_set_cntkctl(cntkctl);
786 }
787
788 static bool arch_timer_has_nonsecure_ppi(void)
789 {
790 return (arch_timer_uses_ppi == ARCH_TIMER_PHYS_SECURE_PPI &&
791 arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
792 }
793
794 static u32 check_ppi_trigger(int irq)
795 {
796 u32 flags = irq_get_trigger_type(irq);
797
798 if (flags != IRQF_TRIGGER_HIGH && flags != IRQF_TRIGGER_LOW) {
799 pr_warn("WARNING: Invalid trigger for IRQ%d, assuming level low\n", irq);
800 pr_warn("WARNING: Please fix your firmware\n");
801 flags = IRQF_TRIGGER_LOW;
802 }
803
804 return flags;
805 }
806
807 static int arch_timer_starting_cpu(unsigned int cpu)
808 {
809 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
810 u32 flags;
811
812 __arch_timer_setup(ARCH_TIMER_TYPE_CP15, clk);
813
814 flags = check_ppi_trigger(arch_timer_ppi[arch_timer_uses_ppi]);
815 enable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], flags);
816
817 if (arch_timer_has_nonsecure_ppi()) {
818 flags = check_ppi_trigger(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
819 enable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
820 flags);
821 }
822
823 arch_counter_set_user_access();
824 if (evtstrm_enable)
825 arch_timer_configure_evtstream();
826
827 return 0;
828 }
829
830 /*
831 * For historical reasons, when probing with DT we use whichever (non-zero)
832 * rate was probed first, and don't verify that others match. If the first node
833 * probed has a clock-frequency property, this overrides the HW register.
834 */
835 static void arch_timer_of_configure_rate(u32 rate, struct device_node *np)
836 {
837 /* Who has more than one independent system counter? */
838 if (arch_timer_rate)
839 return;
840
841 if (of_property_read_u32(np, "clock-frequency", &arch_timer_rate))
842 arch_timer_rate = rate;
843
844 /* Check the timer frequency. */
845 if (arch_timer_rate == 0)
846 pr_warn("frequency not available\n");
847 }
848
849 static void arch_timer_banner(unsigned type)
850 {
851 pr_info("%s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
852 type & ARCH_TIMER_TYPE_CP15 ? "cp15" : "",
853 type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ?
854 " and " : "",
855 type & ARCH_TIMER_TYPE_MEM ? "mmio" : "",
856 (unsigned long)arch_timer_rate / 1000000,
857 (unsigned long)(arch_timer_rate / 10000) % 100,
858 type & ARCH_TIMER_TYPE_CP15 ?
859 (arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI) ? "virt" : "phys" :
860 "",
861 type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ? "/" : "",
862 type & ARCH_TIMER_TYPE_MEM ?
863 arch_timer_mem_use_virtual ? "virt" : "phys" :
864 "");
865 }
866
867 u32 arch_timer_get_rate(void)
868 {
869 return arch_timer_rate;
870 }
871
872 static u64 arch_counter_get_cntvct_mem(void)
873 {
874 u32 vct_lo, vct_hi, tmp_hi;
875
876 do {
877 vct_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
878 vct_lo = readl_relaxed(arch_counter_base + CNTVCT_LO);
879 tmp_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
880 } while (vct_hi != tmp_hi);
881
882 return ((u64) vct_hi << 32) | vct_lo;
883 }
884
885 static struct arch_timer_kvm_info arch_timer_kvm_info;
886
887 struct arch_timer_kvm_info *arch_timer_get_kvm_info(void)
888 {
889 return &arch_timer_kvm_info;
890 }
891
892 static void __init arch_counter_register(unsigned type)
893 {
894 u64 start_count;
895
896 /* Register the CP15 based counter if we have one */
897 if (type & ARCH_TIMER_TYPE_CP15) {
898 if (IS_ENABLED(CONFIG_ARM64) ||
899 arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI)
900 arch_timer_read_counter = arch_counter_get_cntvct;
901 else
902 arch_timer_read_counter = arch_counter_get_cntpct;
903
904 clocksource_counter.archdata.vdso_direct = vdso_default;
905 } else {
906 arch_timer_read_counter = arch_counter_get_cntvct_mem;
907 }
908
909 if (!arch_counter_suspend_stop)
910 clocksource_counter.flags |= CLOCK_SOURCE_SUSPEND_NONSTOP;
911 start_count = arch_timer_read_counter();
912 clocksource_register_hz(&clocksource_counter, arch_timer_rate);
913 cyclecounter.mult = clocksource_counter.mult;
914 cyclecounter.shift = clocksource_counter.shift;
915 timecounter_init(&arch_timer_kvm_info.timecounter,
916 &cyclecounter, start_count);
917
918 /* 56 bits minimum, so we assume worst case rollover */
919 sched_clock_register(arch_timer_read_counter, 56, arch_timer_rate);
920 }
921
922 static void arch_timer_stop(struct clock_event_device *clk)
923 {
924 pr_debug("disable IRQ%d cpu #%d\n", clk->irq, smp_processor_id());
925
926 disable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi]);
927 if (arch_timer_has_nonsecure_ppi())
928 disable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
929
930 clk->set_state_shutdown(clk);
931 }
932
933 static int arch_timer_dying_cpu(unsigned int cpu)
934 {
935 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
936
937 arch_timer_stop(clk);
938 return 0;
939 }
940
941 #ifdef CONFIG_CPU_PM
942 static unsigned int saved_cntkctl;
943 static int arch_timer_cpu_pm_notify(struct notifier_block *self,
944 unsigned long action, void *hcpu)
945 {
946 if (action == CPU_PM_ENTER)
947 saved_cntkctl = arch_timer_get_cntkctl();
948 else if (action == CPU_PM_ENTER_FAILED || action == CPU_PM_EXIT)
949 arch_timer_set_cntkctl(saved_cntkctl);
950 return NOTIFY_OK;
951 }
952
953 static struct notifier_block arch_timer_cpu_pm_notifier = {
954 .notifier_call = arch_timer_cpu_pm_notify,
955 };
956
957 static int __init arch_timer_cpu_pm_init(void)
958 {
959 return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier);
960 }
961
962 static void __init arch_timer_cpu_pm_deinit(void)
963 {
964 WARN_ON(cpu_pm_unregister_notifier(&arch_timer_cpu_pm_notifier));
965 }
966
967 #else
968 static int __init arch_timer_cpu_pm_init(void)
969 {
970 return 0;
971 }
972
973 static void __init arch_timer_cpu_pm_deinit(void)
974 {
975 }
976 #endif
977
978 static int __init arch_timer_register(void)
979 {
980 int err;
981 int ppi;
982
983 arch_timer_evt = alloc_percpu(struct clock_event_device);
984 if (!arch_timer_evt) {
985 err = -ENOMEM;
986 goto out;
987 }
988
989 ppi = arch_timer_ppi[arch_timer_uses_ppi];
990 switch (arch_timer_uses_ppi) {
991 case ARCH_TIMER_VIRT_PPI:
992 err = request_percpu_irq(ppi, arch_timer_handler_virt,
993 "arch_timer", arch_timer_evt);
994 break;
995 case ARCH_TIMER_PHYS_SECURE_PPI:
996 case ARCH_TIMER_PHYS_NONSECURE_PPI:
997 err = request_percpu_irq(ppi, arch_timer_handler_phys,
998 "arch_timer", arch_timer_evt);
999 if (!err && arch_timer_has_nonsecure_ppi()) {
1000 ppi = arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI];
1001 err = request_percpu_irq(ppi, arch_timer_handler_phys,
1002 "arch_timer", arch_timer_evt);
1003 if (err)
1004 free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_SECURE_PPI],
1005 arch_timer_evt);
1006 }
1007 break;
1008 case ARCH_TIMER_HYP_PPI:
1009 err = request_percpu_irq(ppi, arch_timer_handler_phys,
1010 "arch_timer", arch_timer_evt);
1011 break;
1012 default:
1013 BUG();
1014 }
1015
1016 if (err) {
1017 pr_err("can't register interrupt %d (%d)\n", ppi, err);
1018 goto out_free;
1019 }
1020
1021 err = arch_timer_cpu_pm_init();
1022 if (err)
1023 goto out_unreg_notify;
1024
1025
1026 /* Register and immediately configure the timer on the boot CPU */
1027 err = cpuhp_setup_state(CPUHP_AP_ARM_ARCH_TIMER_STARTING,
1028 "clockevents/arm/arch_timer:starting",
1029 arch_timer_starting_cpu, arch_timer_dying_cpu);
1030 if (err)
1031 goto out_unreg_cpupm;
1032 return 0;
1033
1034 out_unreg_cpupm:
1035 arch_timer_cpu_pm_deinit();
1036
1037 out_unreg_notify:
1038 free_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], arch_timer_evt);
1039 if (arch_timer_has_nonsecure_ppi())
1040 free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
1041 arch_timer_evt);
1042
1043 out_free:
1044 free_percpu(arch_timer_evt);
1045 out:
1046 return err;
1047 }
1048
1049 static int __init arch_timer_mem_register(void __iomem *base, unsigned int irq)
1050 {
1051 int ret;
1052 irq_handler_t func;
1053 struct arch_timer *t;
1054
1055 t = kzalloc(sizeof(*t), GFP_KERNEL);
1056 if (!t)
1057 return -ENOMEM;
1058
1059 t->base = base;
1060 t->evt.irq = irq;
1061 __arch_timer_setup(ARCH_TIMER_TYPE_MEM, &t->evt);
1062
1063 if (arch_timer_mem_use_virtual)
1064 func = arch_timer_handler_virt_mem;
1065 else
1066 func = arch_timer_handler_phys_mem;
1067
1068 ret = request_irq(irq, func, IRQF_TIMER, "arch_mem_timer", &t->evt);
1069 if (ret) {
1070 pr_err("Failed to request mem timer irq\n");
1071 kfree(t);
1072 }
1073
1074 return ret;
1075 }
1076
1077 static const struct of_device_id arch_timer_of_match[] __initconst = {
1078 { .compatible = "arm,armv7-timer", },
1079 { .compatible = "arm,armv8-timer", },
1080 {},
1081 };
1082
1083 static const struct of_device_id arch_timer_mem_of_match[] __initconst = {
1084 { .compatible = "arm,armv7-timer-mem", },
1085 {},
1086 };
1087
1088 static bool __init arch_timer_needs_of_probing(void)
1089 {
1090 struct device_node *dn;
1091 bool needs_probing = false;
1092 unsigned int mask = ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM;
1093
1094 /* We have two timers, and both device-tree nodes are probed. */
1095 if ((arch_timers_present & mask) == mask)
1096 return false;
1097
1098 /*
1099 * Only one type of timer is probed,
1100 * check if we have another type of timer node in device-tree.
1101 */
1102 if (arch_timers_present & ARCH_TIMER_TYPE_CP15)
1103 dn = of_find_matching_node(NULL, arch_timer_mem_of_match);
1104 else
1105 dn = of_find_matching_node(NULL, arch_timer_of_match);
1106
1107 if (dn && of_device_is_available(dn))
1108 needs_probing = true;
1109
1110 of_node_put(dn);
1111
1112 return needs_probing;
1113 }
1114
1115 static int __init arch_timer_common_init(void)
1116 {
1117 arch_timer_banner(arch_timers_present);
1118 arch_counter_register(arch_timers_present);
1119 return arch_timer_arch_init();
1120 }
1121
1122 /**
1123 * arch_timer_select_ppi() - Select suitable PPI for the current system.
1124 *
1125 * If HYP mode is available, we know that the physical timer
1126 * has been configured to be accessible from PL1. Use it, so
1127 * that a guest can use the virtual timer instead.
1128 *
1129 * On ARMv8.1 with VH extensions, the kernel runs in HYP. VHE
1130 * accesses to CNTP_*_EL1 registers are silently redirected to
1131 * their CNTHP_*_EL2 counterparts, and use a different PPI
1132 * number.
1133 *
1134 * If no interrupt provided for virtual timer, we'll have to
1135 * stick to the physical timer. It'd better be accessible...
1136 * For arm64 we never use the secure interrupt.
1137 *
1138 * Return: a suitable PPI type for the current system.
1139 */
1140 static enum arch_timer_ppi_nr __init arch_timer_select_ppi(void)
1141 {
1142 if (is_kernel_in_hyp_mode())
1143 return ARCH_TIMER_HYP_PPI;
1144
1145 if (!is_hyp_mode_available() && arch_timer_ppi[ARCH_TIMER_VIRT_PPI])
1146 return ARCH_TIMER_VIRT_PPI;
1147
1148 if (IS_ENABLED(CONFIG_ARM64))
1149 return ARCH_TIMER_PHYS_NONSECURE_PPI;
1150
1151 return ARCH_TIMER_PHYS_SECURE_PPI;
1152 }
1153
1154 static int __init arch_timer_of_init(struct device_node *np)
1155 {
1156 int i, ret;
1157 u32 rate;
1158
1159 if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
1160 pr_warn("multiple nodes in dt, skipping\n");
1161 return 0;
1162 }
1163
1164 arch_timers_present |= ARCH_TIMER_TYPE_CP15;
1165 for (i = ARCH_TIMER_PHYS_SECURE_PPI; i < ARCH_TIMER_MAX_TIMER_PPI; i++)
1166 arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
1167
1168 arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI];
1169
1170 rate = arch_timer_get_cntfrq();
1171 arch_timer_of_configure_rate(rate, np);
1172
1173 arch_timer_c3stop = !of_property_read_bool(np, "always-on");
1174
1175 /* Check for globally applicable workarounds */
1176 arch_timer_check_ool_workaround(ate_match_dt, np);
1177 arch_timer_check_ool_workaround(ate_match_global_cap_id, NULL);
1178
1179 /*
1180 * If we cannot rely on firmware initializing the timer registers then
1181 * we should use the physical timers instead.
1182 */
1183 if (IS_ENABLED(CONFIG_ARM) &&
1184 of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
1185 arch_timer_uses_ppi = ARCH_TIMER_PHYS_SECURE_PPI;
1186 else
1187 arch_timer_uses_ppi = arch_timer_select_ppi();
1188
1189 if (!arch_timer_ppi[arch_timer_uses_ppi]) {
1190 pr_err("No interrupt available, giving up\n");
1191 return -EINVAL;
1192 }
1193
1194 /* On some systems, the counter stops ticking when in suspend. */
1195 arch_counter_suspend_stop = of_property_read_bool(np,
1196 "arm,no-tick-in-suspend");
1197
1198 ret = arch_timer_register();
1199 if (ret)
1200 return ret;
1201
1202 if (arch_timer_needs_of_probing())
1203 return 0;
1204
1205 return arch_timer_common_init();
1206 }
1207 CLOCKSOURCE_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_of_init);
1208 CLOCKSOURCE_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_of_init);
1209
1210 static u32 __init
1211 arch_timer_mem_frame_get_cntfrq(struct arch_timer_mem_frame *frame)
1212 {
1213 void __iomem *base;
1214 u32 rate;
1215
1216 base = ioremap(frame->cntbase, frame->size);
1217 if (!base) {
1218 pr_err("Unable to map frame @ %pa\n", &frame->cntbase);
1219 return 0;
1220 }
1221
1222 rate = readl_relaxed(base + CNTFRQ);
1223
1224 iounmap(base);
1225
1226 return rate;
1227 }
1228
1229 static struct arch_timer_mem_frame * __init
1230 arch_timer_mem_find_best_frame(struct arch_timer_mem *timer_mem)
1231 {
1232 struct arch_timer_mem_frame *frame, *best_frame = NULL;
1233 void __iomem *cntctlbase;
1234 u32 cnttidr;
1235 int i;
1236
1237 cntctlbase = ioremap(timer_mem->cntctlbase, timer_mem->size);
1238 if (!cntctlbase) {
1239 pr_err("Can't map CNTCTLBase @ %pa\n",
1240 &timer_mem->cntctlbase);
1241 return NULL;
1242 }
1243
1244 cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
1245
1246 /*
1247 * Try to find a virtual capable frame. Otherwise fall back to a
1248 * physical capable frame.
1249 */
1250 for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
1251 u32 cntacr = CNTACR_RFRQ | CNTACR_RWPT | CNTACR_RPCT |
1252 CNTACR_RWVT | CNTACR_RVOFF | CNTACR_RVCT;
1253
1254 frame = &timer_mem->frame[i];
1255 if (!frame->valid)
1256 continue;
1257
1258 /* Try enabling everything, and see what sticks */
1259 writel_relaxed(cntacr, cntctlbase + CNTACR(i));
1260 cntacr = readl_relaxed(cntctlbase + CNTACR(i));
1261
1262 if ((cnttidr & CNTTIDR_VIRT(i)) &&
1263 !(~cntacr & (CNTACR_RWVT | CNTACR_RVCT))) {
1264 best_frame = frame;
1265 arch_timer_mem_use_virtual = true;
1266 break;
1267 }
1268
1269 if (~cntacr & (CNTACR_RWPT | CNTACR_RPCT))
1270 continue;
1271
1272 best_frame = frame;
1273 }
1274
1275 iounmap(cntctlbase);
1276
1277 if (!best_frame)
1278 pr_err("Unable to find a suitable frame in timer @ %pa\n",
1279 &timer_mem->cntctlbase);
1280
1281 return best_frame;
1282 }
1283
1284 static int __init
1285 arch_timer_mem_frame_register(struct arch_timer_mem_frame *frame)
1286 {
1287 void __iomem *base;
1288 int ret, irq = 0;
1289
1290 if (arch_timer_mem_use_virtual)
1291 irq = frame->virt_irq;
1292 else
1293 irq = frame->phys_irq;
1294
1295 if (!irq) {
1296 pr_err("Frame missing %s irq.\n",
1297 arch_timer_mem_use_virtual ? "virt" : "phys");
1298 return -EINVAL;
1299 }
1300
1301 if (!request_mem_region(frame->cntbase, frame->size,
1302 "arch_mem_timer"))
1303 return -EBUSY;
1304
1305 base = ioremap(frame->cntbase, frame->size);
1306 if (!base) {
1307 pr_err("Can't map frame's registers\n");
1308 return -ENXIO;
1309 }
1310
1311 ret = arch_timer_mem_register(base, irq);
1312 if (ret) {
1313 iounmap(base);
1314 return ret;
1315 }
1316
1317 arch_counter_base = base;
1318 arch_timers_present |= ARCH_TIMER_TYPE_MEM;
1319
1320 return 0;
1321 }
1322
1323 static int __init arch_timer_mem_of_init(struct device_node *np)
1324 {
1325 struct arch_timer_mem *timer_mem;
1326 struct arch_timer_mem_frame *frame;
1327 struct device_node *frame_node;
1328 struct resource res;
1329 int ret = -EINVAL;
1330 u32 rate;
1331
1332 timer_mem = kzalloc(sizeof(*timer_mem), GFP_KERNEL);
1333 if (!timer_mem)
1334 return -ENOMEM;
1335
1336 if (of_address_to_resource(np, 0, &res))
1337 goto out;
1338 timer_mem->cntctlbase = res.start;
1339 timer_mem->size = resource_size(&res);
1340
1341 for_each_available_child_of_node(np, frame_node) {
1342 u32 n;
1343 struct arch_timer_mem_frame *frame;
1344
1345 if (of_property_read_u32(frame_node, "frame-number", &n)) {
1346 pr_err(FW_BUG "Missing frame-number.\n");
1347 of_node_put(frame_node);
1348 goto out;
1349 }
1350 if (n >= ARCH_TIMER_MEM_MAX_FRAMES) {
1351 pr_err(FW_BUG "Wrong frame-number, only 0-%u are permitted.\n",
1352 ARCH_TIMER_MEM_MAX_FRAMES - 1);
1353 of_node_put(frame_node);
1354 goto out;
1355 }
1356 frame = &timer_mem->frame[n];
1357
1358 if (frame->valid) {
1359 pr_err(FW_BUG "Duplicated frame-number.\n");
1360 of_node_put(frame_node);
1361 goto out;
1362 }
1363
1364 if (of_address_to_resource(frame_node, 0, &res)) {
1365 of_node_put(frame_node);
1366 goto out;
1367 }
1368 frame->cntbase = res.start;
1369 frame->size = resource_size(&res);
1370
1371 frame->virt_irq = irq_of_parse_and_map(frame_node,
1372 ARCH_TIMER_VIRT_SPI);
1373 frame->phys_irq = irq_of_parse_and_map(frame_node,
1374 ARCH_TIMER_PHYS_SPI);
1375
1376 frame->valid = true;
1377 }
1378
1379 frame = arch_timer_mem_find_best_frame(timer_mem);
1380 if (!frame) {
1381 ret = -EINVAL;
1382 goto out;
1383 }
1384
1385 rate = arch_timer_mem_frame_get_cntfrq(frame);
1386 arch_timer_of_configure_rate(rate, np);
1387
1388 ret = arch_timer_mem_frame_register(frame);
1389 if (!ret && !arch_timer_needs_of_probing())
1390 ret = arch_timer_common_init();
1391 out:
1392 kfree(timer_mem);
1393 return ret;
1394 }
1395 CLOCKSOURCE_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem",
1396 arch_timer_mem_of_init);
1397
1398 #ifdef CONFIG_ACPI_GTDT
1399 static int __init
1400 arch_timer_mem_verify_cntfrq(struct arch_timer_mem *timer_mem)
1401 {
1402 struct arch_timer_mem_frame *frame;
1403 u32 rate;
1404 int i;
1405
1406 for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
1407 frame = &timer_mem->frame[i];
1408
1409 if (!frame->valid)
1410 continue;
1411
1412 rate = arch_timer_mem_frame_get_cntfrq(frame);
1413 if (rate == arch_timer_rate)
1414 continue;
1415
1416 pr_err(FW_BUG "CNTFRQ mismatch: frame @ %pa: (0x%08lx), CPU: (0x%08lx)\n",
1417 &frame->cntbase,
1418 (unsigned long)rate, (unsigned long)arch_timer_rate);
1419
1420 return -EINVAL;
1421 }
1422
1423 return 0;
1424 }
1425
1426 static int __init arch_timer_mem_acpi_init(int platform_timer_count)
1427 {
1428 struct arch_timer_mem *timers, *timer;
1429 struct arch_timer_mem_frame *frame;
1430 int timer_count, i, ret = 0;
1431
1432 timers = kcalloc(platform_timer_count, sizeof(*timers),
1433 GFP_KERNEL);
1434 if (!timers)
1435 return -ENOMEM;
1436
1437 ret = acpi_arch_timer_mem_init(timers, &timer_count);
1438 if (ret || !timer_count)
1439 goto out;
1440
1441 for (i = 0; i < timer_count; i++) {
1442 ret = arch_timer_mem_verify_cntfrq(&timers[i]);
1443 if (ret) {
1444 pr_err("Disabling MMIO timers due to CNTFRQ mismatch\n");
1445 goto out;
1446 }
1447 }
1448
1449 /*
1450 * While unlikely, it's theoretically possible that none of the frames
1451 * in a timer expose the combination of feature we want.
1452 */
1453 for (i = i; i < timer_count; i++) {
1454 timer = &timers[i];
1455
1456 frame = arch_timer_mem_find_best_frame(timer);
1457 if (frame)
1458 break;
1459 }
1460
1461 if (frame)
1462 ret = arch_timer_mem_frame_register(frame);
1463 out:
1464 kfree(timers);
1465 return ret;
1466 }
1467
1468 /* Initialize per-processor generic timer and memory-mapped timer(if present) */
1469 static int __init arch_timer_acpi_init(struct acpi_table_header *table)
1470 {
1471 int ret, platform_timer_count;
1472
1473 if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
1474 pr_warn("already initialized, skipping\n");
1475 return -EINVAL;
1476 }
1477
1478 arch_timers_present |= ARCH_TIMER_TYPE_CP15;
1479
1480 ret = acpi_gtdt_init(table, &platform_timer_count);
1481 if (ret) {
1482 pr_err("Failed to init GTDT table.\n");
1483 return ret;
1484 }
1485
1486 arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI] =
1487 acpi_gtdt_map_ppi(ARCH_TIMER_PHYS_NONSECURE_PPI);
1488
1489 arch_timer_ppi[ARCH_TIMER_VIRT_PPI] =
1490 acpi_gtdt_map_ppi(ARCH_TIMER_VIRT_PPI);
1491
1492 arch_timer_ppi[ARCH_TIMER_HYP_PPI] =
1493 acpi_gtdt_map_ppi(ARCH_TIMER_HYP_PPI);
1494
1495 arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI];
1496
1497 /*
1498 * When probing via ACPI, we have no mechanism to override the sysreg
1499 * CNTFRQ value. This *must* be correct.
1500 */
1501 arch_timer_rate = arch_timer_get_cntfrq();
1502 if (!arch_timer_rate) {
1503 pr_err(FW_BUG "frequency not available.\n");
1504 return -EINVAL;
1505 }
1506
1507 arch_timer_uses_ppi = arch_timer_select_ppi();
1508 if (!arch_timer_ppi[arch_timer_uses_ppi]) {
1509 pr_err("No interrupt available, giving up\n");
1510 return -EINVAL;
1511 }
1512
1513 /* Always-on capability */
1514 arch_timer_c3stop = acpi_gtdt_c3stop(arch_timer_uses_ppi);
1515
1516 /* Check for globally applicable workarounds */
1517 arch_timer_check_ool_workaround(ate_match_global_cap_id, NULL);
1518 arch_timer_check_ool_workaround(ate_match_acpi_oem_info, table);
1519
1520 ret = arch_timer_register();
1521 if (ret)
1522 return ret;
1523
1524 if (platform_timer_count &&
1525 arch_timer_mem_acpi_init(platform_timer_count))
1526 pr_err("Failed to initialize memory-mapped timer.\n");
1527
1528 return arch_timer_common_init();
1529 }
1530 CLOCKSOURCE_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
1531 #endif