]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/clocksource/arm_arch_timer.c
UBUNTU: SAUCE: arm64: arch_timer: Add erratum handler for CPU-specific capability
[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 #define CNTTIDR 0x08
36 #define CNTTIDR_VIRT(n) (BIT(1) << ((n) * 4))
37
38 #define CNTACR(n) (0x40 + ((n) * 4))
39 #define CNTACR_RPCT BIT(0)
40 #define CNTACR_RVCT BIT(1)
41 #define CNTACR_RFRQ BIT(2)
42 #define CNTACR_RVOFF BIT(3)
43 #define CNTACR_RWVT BIT(4)
44 #define CNTACR_RWPT BIT(5)
45
46 #define CNTVCT_LO 0x08
47 #define CNTVCT_HI 0x0c
48 #define CNTFRQ 0x10
49 #define CNTP_TVAL 0x28
50 #define CNTP_CTL 0x2c
51 #define CNTV_TVAL 0x38
52 #define CNTV_CTL 0x3c
53
54 #define ARCH_CP15_TIMER BIT(0)
55 #define ARCH_MEM_TIMER BIT(1)
56 static unsigned arch_timers_present __initdata;
57
58 static void __iomem *arch_counter_base;
59
60 struct arch_timer {
61 void __iomem *base;
62 struct clock_event_device evt;
63 };
64
65 #define to_arch_timer(e) container_of(e, struct arch_timer, evt)
66
67 static u32 arch_timer_rate;
68
69 enum ppi_nr {
70 PHYS_SECURE_PPI,
71 PHYS_NONSECURE_PPI,
72 VIRT_PPI,
73 HYP_PPI,
74 MAX_TIMER_PPI
75 };
76
77 static int arch_timer_ppi[MAX_TIMER_PPI];
78
79 static struct clock_event_device __percpu *arch_timer_evt;
80
81 static enum ppi_nr arch_timer_uses_ppi = VIRT_PPI;
82 static bool arch_timer_c3stop;
83 static bool arch_timer_mem_use_virtual;
84 static bool arch_counter_suspend_stop;
85
86 static bool evtstrm_enable = IS_ENABLED(CONFIG_ARM_ARCH_TIMER_EVTSTREAM);
87
88 static int __init early_evtstrm_cfg(char *buf)
89 {
90 return strtobool(buf, &evtstrm_enable);
91 }
92 early_param("clocksource.arm_arch_timer.evtstrm", early_evtstrm_cfg);
93
94 /*
95 * Architected system timer support.
96 */
97
98 #ifdef CONFIG_FSL_ERRATUM_A008585
99 /*
100 * The number of retries is an arbitrary value well beyond the highest number
101 * of iterations the loop has been observed to take.
102 */
103 #define __fsl_a008585_read_reg(reg) ({ \
104 u64 _old, _new; \
105 int _retries = 200; \
106 \
107 do { \
108 _old = read_sysreg(reg); \
109 _new = read_sysreg(reg); \
110 _retries--; \
111 } while (unlikely(_old != _new) && _retries); \
112 \
113 WARN_ON_ONCE(!_retries); \
114 _new; \
115 })
116
117 static u32 notrace fsl_a008585_read_cntp_tval_el0(void)
118 {
119 return __fsl_a008585_read_reg(cntp_tval_el0);
120 }
121
122 static u32 notrace fsl_a008585_read_cntv_tval_el0(void)
123 {
124 return __fsl_a008585_read_reg(cntv_tval_el0);
125 }
126
127 static u64 notrace fsl_a008585_read_cntvct_el0(void)
128 {
129 return __fsl_a008585_read_reg(cntvct_el0);
130 }
131 #endif
132
133 #ifdef CONFIG_HISILICON_ERRATUM_161010101
134 /*
135 * Verify whether the value of the second read is larger than the first by
136 * less than 32 is the only way to confirm the value is correct, so clear the
137 * lower 5 bits to check whether the difference is greater than 32 or not.
138 * Theoretically the erratum should not occur more than twice in succession
139 * when reading the system counter, but it is possible that some interrupts
140 * may lead to more than twice read errors, triggering the warning, so setting
141 * the number of retries far beyond the number of iterations the loop has been
142 * observed to take.
143 */
144 #define __hisi_161010101_read_reg(reg) ({ \
145 u64 _old, _new; \
146 int _retries = 50; \
147 \
148 do { \
149 _old = read_sysreg(reg); \
150 _new = read_sysreg(reg); \
151 _retries--; \
152 } while (unlikely((_new - _old) >> 5) && _retries); \
153 \
154 WARN_ON_ONCE(!_retries); \
155 _new; \
156 })
157
158 static u32 notrace hisi_161010101_read_cntp_tval_el0(void)
159 {
160 return __hisi_161010101_read_reg(cntp_tval_el0);
161 }
162
163 static u32 notrace hisi_161010101_read_cntv_tval_el0(void)
164 {
165 return __hisi_161010101_read_reg(cntv_tval_el0);
166 }
167
168 static u64 notrace hisi_161010101_read_cntvct_el0(void)
169 {
170 return __hisi_161010101_read_reg(cntvct_el0);
171 }
172 #endif
173
174 #ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
175 const struct arch_timer_erratum_workaround *timer_unstable_counter_workaround = NULL;
176 EXPORT_SYMBOL_GPL(timer_unstable_counter_workaround);
177
178 DEFINE_STATIC_KEY_FALSE(arch_timer_read_ool_enabled);
179 EXPORT_SYMBOL_GPL(arch_timer_read_ool_enabled);
180
181 static const struct arch_timer_erratum_workaround ool_workarounds[] = {
182 #ifdef CONFIG_FSL_ERRATUM_A008585
183 {
184 .match_type = ate_match_dt,
185 .id = "fsl,erratum-a008585",
186 .desc = "Freescale erratum a005858",
187 .read_cntp_tval_el0 = fsl_a008585_read_cntp_tval_el0,
188 .read_cntv_tval_el0 = fsl_a008585_read_cntv_tval_el0,
189 .read_cntvct_el0 = fsl_a008585_read_cntvct_el0,
190 },
191 #endif
192 #ifdef CONFIG_HISILICON_ERRATUM_161010101
193 {
194 .match_type = ate_match_dt,
195 .id = "hisilicon,erratum-161010101",
196 .desc = "HiSilicon erratum 161010101",
197 .read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
198 .read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
199 .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
200 },
201 #endif
202 };
203
204 typedef bool (*ate_match_fn_t)(const struct arch_timer_erratum_workaround *,
205 const void *);
206
207 static
208 bool arch_timer_check_dt_erratum(const struct arch_timer_erratum_workaround *wa,
209 const void *arg)
210 {
211 const struct device_node *np = arg;
212
213 return of_property_read_bool(np, wa->id);
214 }
215
216 static
217 bool arch_timer_check_global_cap_erratum(const struct arch_timer_erratum_workaround *wa,
218 const void *arg)
219 {
220 return cpus_have_cap((uintptr_t)wa->id);
221 }
222
223 static
224 bool arch_timer_check_local_cap_erratum(const struct arch_timer_erratum_workaround *wa,
225 const void *arg)
226 {
227 return this_cpu_has_cap((uintptr_t)wa->id);
228 }
229
230 static const struct arch_timer_erratum_workaround *
231 arch_timer_iterate_errata(enum arch_timer_erratum_match_type type,
232 ate_match_fn_t match_fn,
233 void *arg)
234 {
235 int i;
236
237 for (i = 0; i < ARRAY_SIZE(ool_workarounds); i++) {
238 if (ool_workarounds[i].match_type != type)
239 continue;
240
241 if (match_fn(&ool_workarounds[i], arg))
242 return &ool_workarounds[i];
243 }
244
245 return NULL;
246 }
247
248 static
249 void arch_timer_enable_workaround(const struct arch_timer_erratum_workaround *wa)
250 {
251 timer_unstable_counter_workaround = wa;
252 static_branch_enable(&arch_timer_read_ool_enabled);
253 }
254
255 static void arch_timer_check_ool_workaround(enum arch_timer_erratum_match_type type,
256 void *arg)
257 {
258 const struct arch_timer_erratum_workaround *wa;
259 ate_match_fn_t match_fn = NULL;
260 bool local = false;
261
262 switch (type) {
263 case ate_match_dt:
264 match_fn = arch_timer_check_dt_erratum;
265 break;
266 case ate_match_global_cap_id:
267 match_fn = arch_timer_check_global_cap_erratum;
268 break;
269 case ate_match_local_cap_id:
270 match_fn = arch_timer_check_local_cap_erratum;
271 local = true;
272 break;
273 }
274
275 wa = arch_timer_iterate_errata(type, match_fn, arg);
276 if (!wa)
277 return;
278
279 if (static_branch_unlikely(&arch_timer_read_ool_enabled)) {
280 if (wa != timer_unstable_counter_workaround)
281 pr_warn("Can't enable workaround for %s (clashes with %s\n)",
282 wa->desc,
283 timer_unstable_counter_workaround->desc);
284 return;
285 }
286
287 arch_timer_enable_workaround(wa);
288 pr_info("Enabling %s workaround for %s\n",
289 local ? "local" : "global", wa->desc);
290 }
291
292 #else
293 #define arch_timer_check_ool_workaround(t,a) do { } while(0)
294 #endif /* CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND */
295
296 static __always_inline
297 void arch_timer_reg_write(int access, enum arch_timer_reg reg, u32 val,
298 struct clock_event_device *clk)
299 {
300 if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
301 struct arch_timer *timer = to_arch_timer(clk);
302 switch (reg) {
303 case ARCH_TIMER_REG_CTRL:
304 writel_relaxed(val, timer->base + CNTP_CTL);
305 break;
306 case ARCH_TIMER_REG_TVAL:
307 writel_relaxed(val, timer->base + CNTP_TVAL);
308 break;
309 }
310 } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
311 struct arch_timer *timer = to_arch_timer(clk);
312 switch (reg) {
313 case ARCH_TIMER_REG_CTRL:
314 writel_relaxed(val, timer->base + CNTV_CTL);
315 break;
316 case ARCH_TIMER_REG_TVAL:
317 writel_relaxed(val, timer->base + CNTV_TVAL);
318 break;
319 }
320 } else {
321 arch_timer_reg_write_cp15(access, reg, val);
322 }
323 }
324
325 static __always_inline
326 u32 arch_timer_reg_read(int access, enum arch_timer_reg reg,
327 struct clock_event_device *clk)
328 {
329 u32 val;
330
331 if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
332 struct arch_timer *timer = to_arch_timer(clk);
333 switch (reg) {
334 case ARCH_TIMER_REG_CTRL:
335 val = readl_relaxed(timer->base + CNTP_CTL);
336 break;
337 case ARCH_TIMER_REG_TVAL:
338 val = readl_relaxed(timer->base + CNTP_TVAL);
339 break;
340 }
341 } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
342 struct arch_timer *timer = to_arch_timer(clk);
343 switch (reg) {
344 case ARCH_TIMER_REG_CTRL:
345 val = readl_relaxed(timer->base + CNTV_CTL);
346 break;
347 case ARCH_TIMER_REG_TVAL:
348 val = readl_relaxed(timer->base + CNTV_TVAL);
349 break;
350 }
351 } else {
352 val = arch_timer_reg_read_cp15(access, reg);
353 }
354
355 return val;
356 }
357
358 static __always_inline irqreturn_t timer_handler(const int access,
359 struct clock_event_device *evt)
360 {
361 unsigned long ctrl;
362
363 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, evt);
364 if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
365 ctrl |= ARCH_TIMER_CTRL_IT_MASK;
366 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, evt);
367 evt->event_handler(evt);
368 return IRQ_HANDLED;
369 }
370
371 return IRQ_NONE;
372 }
373
374 static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
375 {
376 struct clock_event_device *evt = dev_id;
377
378 return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
379 }
380
381 static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
382 {
383 struct clock_event_device *evt = dev_id;
384
385 return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
386 }
387
388 static irqreturn_t arch_timer_handler_phys_mem(int irq, void *dev_id)
389 {
390 struct clock_event_device *evt = dev_id;
391
392 return timer_handler(ARCH_TIMER_MEM_PHYS_ACCESS, evt);
393 }
394
395 static irqreturn_t arch_timer_handler_virt_mem(int irq, void *dev_id)
396 {
397 struct clock_event_device *evt = dev_id;
398
399 return timer_handler(ARCH_TIMER_MEM_VIRT_ACCESS, evt);
400 }
401
402 static __always_inline int timer_shutdown(const int access,
403 struct clock_event_device *clk)
404 {
405 unsigned long ctrl;
406
407 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
408 ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
409 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
410
411 return 0;
412 }
413
414 static int arch_timer_shutdown_virt(struct clock_event_device *clk)
415 {
416 return timer_shutdown(ARCH_TIMER_VIRT_ACCESS, clk);
417 }
418
419 static int arch_timer_shutdown_phys(struct clock_event_device *clk)
420 {
421 return timer_shutdown(ARCH_TIMER_PHYS_ACCESS, clk);
422 }
423
424 static int arch_timer_shutdown_virt_mem(struct clock_event_device *clk)
425 {
426 return timer_shutdown(ARCH_TIMER_MEM_VIRT_ACCESS, clk);
427 }
428
429 static int arch_timer_shutdown_phys_mem(struct clock_event_device *clk)
430 {
431 return timer_shutdown(ARCH_TIMER_MEM_PHYS_ACCESS, clk);
432 }
433
434 static __always_inline void set_next_event(const int access, unsigned long evt,
435 struct clock_event_device *clk)
436 {
437 unsigned long ctrl;
438 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
439 ctrl |= ARCH_TIMER_CTRL_ENABLE;
440 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
441 arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt, clk);
442 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
443 }
444
445 #ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
446 static __always_inline void erratum_set_next_event_generic(const int access,
447 unsigned long evt, struct clock_event_device *clk)
448 {
449 unsigned long ctrl;
450 u64 cval = evt + arch_counter_get_cntvct();
451
452 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
453 ctrl |= ARCH_TIMER_CTRL_ENABLE;
454 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
455
456 if (access == ARCH_TIMER_PHYS_ACCESS)
457 write_sysreg(cval, cntp_cval_el0);
458 else if (access == ARCH_TIMER_VIRT_ACCESS)
459 write_sysreg(cval, cntv_cval_el0);
460
461 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
462 }
463
464 static int erratum_set_next_event_virt(unsigned long evt,
465 struct clock_event_device *clk)
466 {
467 erratum_set_next_event_generic(ARCH_TIMER_VIRT_ACCESS, evt, clk);
468 return 0;
469 }
470
471 static int erratum_set_next_event_phys(unsigned long evt,
472 struct clock_event_device *clk)
473 {
474 erratum_set_next_event_generic(ARCH_TIMER_PHYS_ACCESS, evt, clk);
475 return 0;
476 }
477 #endif /* CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND */
478
479 static int arch_timer_set_next_event_virt(unsigned long evt,
480 struct clock_event_device *clk)
481 {
482 set_next_event(ARCH_TIMER_VIRT_ACCESS, evt, clk);
483 return 0;
484 }
485
486 static int arch_timer_set_next_event_phys(unsigned long evt,
487 struct clock_event_device *clk)
488 {
489 set_next_event(ARCH_TIMER_PHYS_ACCESS, evt, clk);
490 return 0;
491 }
492
493 static int arch_timer_set_next_event_virt_mem(unsigned long evt,
494 struct clock_event_device *clk)
495 {
496 set_next_event(ARCH_TIMER_MEM_VIRT_ACCESS, evt, clk);
497 return 0;
498 }
499
500 static int arch_timer_set_next_event_phys_mem(unsigned long evt,
501 struct clock_event_device *clk)
502 {
503 set_next_event(ARCH_TIMER_MEM_PHYS_ACCESS, evt, clk);
504 return 0;
505 }
506
507 static void erratum_workaround_set_sne(struct clock_event_device *clk)
508 {
509 #ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
510 if (!static_branch_unlikely(&arch_timer_read_ool_enabled))
511 return;
512
513 if (arch_timer_uses_ppi == VIRT_PPI)
514 clk->set_next_event = erratum_set_next_event_virt;
515 else
516 clk->set_next_event = erratum_set_next_event_phys;
517 #endif
518 }
519
520 static void __arch_timer_setup(unsigned type,
521 struct clock_event_device *clk)
522 {
523 clk->features = CLOCK_EVT_FEAT_ONESHOT;
524
525 if (type == ARCH_CP15_TIMER) {
526 if (arch_timer_c3stop)
527 clk->features |= CLOCK_EVT_FEAT_C3STOP;
528 clk->name = "arch_sys_timer";
529 clk->rating = 450;
530 clk->cpumask = cpumask_of(smp_processor_id());
531 clk->irq = arch_timer_ppi[arch_timer_uses_ppi];
532 switch (arch_timer_uses_ppi) {
533 case VIRT_PPI:
534 clk->set_state_shutdown = arch_timer_shutdown_virt;
535 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt;
536 clk->set_next_event = arch_timer_set_next_event_virt;
537 break;
538 case PHYS_SECURE_PPI:
539 case PHYS_NONSECURE_PPI:
540 case HYP_PPI:
541 clk->set_state_shutdown = arch_timer_shutdown_phys;
542 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys;
543 clk->set_next_event = arch_timer_set_next_event_phys;
544 break;
545 default:
546 BUG();
547 }
548
549 arch_timer_check_ool_workaround(ate_match_local_cap_id, NULL);
550
551 erratum_workaround_set_sne(clk);
552 } else {
553 clk->features |= CLOCK_EVT_FEAT_DYNIRQ;
554 clk->name = "arch_mem_timer";
555 clk->rating = 400;
556 clk->cpumask = cpu_all_mask;
557 if (arch_timer_mem_use_virtual) {
558 clk->set_state_shutdown = arch_timer_shutdown_virt_mem;
559 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt_mem;
560 clk->set_next_event =
561 arch_timer_set_next_event_virt_mem;
562 } else {
563 clk->set_state_shutdown = arch_timer_shutdown_phys_mem;
564 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys_mem;
565 clk->set_next_event =
566 arch_timer_set_next_event_phys_mem;
567 }
568 }
569
570 clk->set_state_shutdown(clk);
571
572 clockevents_config_and_register(clk, arch_timer_rate, 0xf, 0x7fffffff);
573 }
574
575 static void arch_timer_evtstrm_enable(int divider)
576 {
577 u32 cntkctl = arch_timer_get_cntkctl();
578
579 cntkctl &= ~ARCH_TIMER_EVT_TRIGGER_MASK;
580 /* Set the divider and enable virtual event stream */
581 cntkctl |= (divider << ARCH_TIMER_EVT_TRIGGER_SHIFT)
582 | ARCH_TIMER_VIRT_EVT_EN;
583 arch_timer_set_cntkctl(cntkctl);
584 elf_hwcap |= HWCAP_EVTSTRM;
585 #ifdef CONFIG_COMPAT
586 compat_elf_hwcap |= COMPAT_HWCAP_EVTSTRM;
587 #endif
588 }
589
590 static void arch_timer_configure_evtstream(void)
591 {
592 int evt_stream_div, pos;
593
594 /* Find the closest power of two to the divisor */
595 evt_stream_div = arch_timer_rate / ARCH_TIMER_EVT_STREAM_FREQ;
596 pos = fls(evt_stream_div);
597 if (pos > 1 && !(evt_stream_div & (1 << (pos - 2))))
598 pos--;
599 /* enable event stream */
600 arch_timer_evtstrm_enable(min(pos, 15));
601 }
602
603 static void arch_counter_set_user_access(void)
604 {
605 u32 cntkctl = arch_timer_get_cntkctl();
606
607 /* Disable user access to the timers and the physical counter */
608 /* Also disable virtual event stream */
609 cntkctl &= ~(ARCH_TIMER_USR_PT_ACCESS_EN
610 | ARCH_TIMER_USR_VT_ACCESS_EN
611 | ARCH_TIMER_VIRT_EVT_EN
612 | ARCH_TIMER_USR_PCT_ACCESS_EN);
613
614 /* Enable user access to the virtual counter */
615 cntkctl |= ARCH_TIMER_USR_VCT_ACCESS_EN;
616
617 arch_timer_set_cntkctl(cntkctl);
618 }
619
620 static bool arch_timer_has_nonsecure_ppi(void)
621 {
622 return (arch_timer_uses_ppi == PHYS_SECURE_PPI &&
623 arch_timer_ppi[PHYS_NONSECURE_PPI]);
624 }
625
626 static u32 check_ppi_trigger(int irq)
627 {
628 u32 flags = irq_get_trigger_type(irq);
629
630 if (flags != IRQF_TRIGGER_HIGH && flags != IRQF_TRIGGER_LOW) {
631 pr_warn("WARNING: Invalid trigger for IRQ%d, assuming level low\n", irq);
632 pr_warn("WARNING: Please fix your firmware\n");
633 flags = IRQF_TRIGGER_LOW;
634 }
635
636 return flags;
637 }
638
639 static int arch_timer_starting_cpu(unsigned int cpu)
640 {
641 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
642 u32 flags;
643
644 __arch_timer_setup(ARCH_CP15_TIMER, clk);
645
646 flags = check_ppi_trigger(arch_timer_ppi[arch_timer_uses_ppi]);
647 enable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], flags);
648
649 if (arch_timer_has_nonsecure_ppi()) {
650 flags = check_ppi_trigger(arch_timer_ppi[PHYS_NONSECURE_PPI]);
651 enable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI], flags);
652 }
653
654 arch_counter_set_user_access();
655 if (evtstrm_enable)
656 arch_timer_configure_evtstream();
657
658 return 0;
659 }
660
661 static void
662 arch_timer_detect_rate(void __iomem *cntbase, struct device_node *np)
663 {
664 /* Who has more than one independent system counter? */
665 if (arch_timer_rate)
666 return;
667
668 /*
669 * Try to determine the frequency from the device tree or CNTFRQ,
670 * if ACPI is enabled, get the frequency from CNTFRQ ONLY.
671 */
672 if (!acpi_disabled ||
673 of_property_read_u32(np, "clock-frequency", &arch_timer_rate)) {
674 if (cntbase)
675 arch_timer_rate = readl_relaxed(cntbase + CNTFRQ);
676 else
677 arch_timer_rate = arch_timer_get_cntfrq();
678 }
679
680 /* Check the timer frequency. */
681 if (arch_timer_rate == 0)
682 pr_warn("Architected timer frequency not available\n");
683 }
684
685 static void arch_timer_banner(unsigned type)
686 {
687 pr_info("Architected %s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
688 type & ARCH_CP15_TIMER ? "cp15" : "",
689 type == (ARCH_CP15_TIMER | ARCH_MEM_TIMER) ? " and " : "",
690 type & ARCH_MEM_TIMER ? "mmio" : "",
691 (unsigned long)arch_timer_rate / 1000000,
692 (unsigned long)(arch_timer_rate / 10000) % 100,
693 type & ARCH_CP15_TIMER ?
694 (arch_timer_uses_ppi == VIRT_PPI) ? "virt" : "phys" :
695 "",
696 type == (ARCH_CP15_TIMER | ARCH_MEM_TIMER) ? "/" : "",
697 type & ARCH_MEM_TIMER ?
698 arch_timer_mem_use_virtual ? "virt" : "phys" :
699 "");
700 }
701
702 u32 arch_timer_get_rate(void)
703 {
704 return arch_timer_rate;
705 }
706
707 static u64 arch_counter_get_cntvct_mem(void)
708 {
709 u32 vct_lo, vct_hi, tmp_hi;
710
711 do {
712 vct_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
713 vct_lo = readl_relaxed(arch_counter_base + CNTVCT_LO);
714 tmp_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
715 } while (vct_hi != tmp_hi);
716
717 return ((u64) vct_hi << 32) | vct_lo;
718 }
719
720 /*
721 * Default to cp15 based access because arm64 uses this function for
722 * sched_clock() before DT is probed and the cp15 method is guaranteed
723 * to exist on arm64. arm doesn't use this before DT is probed so even
724 * if we don't have the cp15 accessors we won't have a problem.
725 */
726 u64 (*arch_timer_read_counter)(void) = arch_counter_get_cntvct;
727
728 static u64 arch_counter_read(struct clocksource *cs)
729 {
730 return arch_timer_read_counter();
731 }
732
733 static u64 arch_counter_read_cc(const struct cyclecounter *cc)
734 {
735 return arch_timer_read_counter();
736 }
737
738 static struct clocksource clocksource_counter = {
739 .name = "arch_sys_counter",
740 .rating = 400,
741 .read = arch_counter_read,
742 .mask = CLOCKSOURCE_MASK(56),
743 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
744 };
745
746 static struct cyclecounter cyclecounter = {
747 .read = arch_counter_read_cc,
748 .mask = CLOCKSOURCE_MASK(56),
749 };
750
751 static struct arch_timer_kvm_info arch_timer_kvm_info;
752
753 struct arch_timer_kvm_info *arch_timer_get_kvm_info(void)
754 {
755 return &arch_timer_kvm_info;
756 }
757
758 static void __init arch_counter_register(unsigned type)
759 {
760 u64 start_count;
761
762 /* Register the CP15 based counter if we have one */
763 if (type & ARCH_CP15_TIMER) {
764 if (IS_ENABLED(CONFIG_ARM64) || arch_timer_uses_ppi == VIRT_PPI)
765 arch_timer_read_counter = arch_counter_get_cntvct;
766 else
767 arch_timer_read_counter = arch_counter_get_cntpct;
768
769 clocksource_counter.archdata.vdso_direct = true;
770
771 #ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
772 /*
773 * Don't use the vdso fastpath if errata require using
774 * the out-of-line counter accessor.
775 */
776 if (static_branch_unlikely(&arch_timer_read_ool_enabled))
777 clocksource_counter.archdata.vdso_direct = false;
778 #endif
779 } else {
780 arch_timer_read_counter = arch_counter_get_cntvct_mem;
781 }
782
783 if (!arch_counter_suspend_stop)
784 clocksource_counter.flags |= CLOCK_SOURCE_SUSPEND_NONSTOP;
785 start_count = arch_timer_read_counter();
786 clocksource_register_hz(&clocksource_counter, arch_timer_rate);
787 cyclecounter.mult = clocksource_counter.mult;
788 cyclecounter.shift = clocksource_counter.shift;
789 timecounter_init(&arch_timer_kvm_info.timecounter,
790 &cyclecounter, start_count);
791
792 /* 56 bits minimum, so we assume worst case rollover */
793 sched_clock_register(arch_timer_read_counter, 56, arch_timer_rate);
794 }
795
796 static void arch_timer_stop(struct clock_event_device *clk)
797 {
798 pr_debug("arch_timer_teardown disable IRQ%d cpu #%d\n",
799 clk->irq, smp_processor_id());
800
801 disable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi]);
802 if (arch_timer_has_nonsecure_ppi())
803 disable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI]);
804
805 clk->set_state_shutdown(clk);
806 }
807
808 static int arch_timer_dying_cpu(unsigned int cpu)
809 {
810 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
811
812 arch_timer_stop(clk);
813 return 0;
814 }
815
816 #ifdef CONFIG_CPU_PM
817 static unsigned int saved_cntkctl;
818 static int arch_timer_cpu_pm_notify(struct notifier_block *self,
819 unsigned long action, void *hcpu)
820 {
821 if (action == CPU_PM_ENTER)
822 saved_cntkctl = arch_timer_get_cntkctl();
823 else if (action == CPU_PM_ENTER_FAILED || action == CPU_PM_EXIT)
824 arch_timer_set_cntkctl(saved_cntkctl);
825 return NOTIFY_OK;
826 }
827
828 static struct notifier_block arch_timer_cpu_pm_notifier = {
829 .notifier_call = arch_timer_cpu_pm_notify,
830 };
831
832 static int __init arch_timer_cpu_pm_init(void)
833 {
834 return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier);
835 }
836
837 static void __init arch_timer_cpu_pm_deinit(void)
838 {
839 WARN_ON(cpu_pm_unregister_notifier(&arch_timer_cpu_pm_notifier));
840 }
841
842 #else
843 static int __init arch_timer_cpu_pm_init(void)
844 {
845 return 0;
846 }
847
848 static void __init arch_timer_cpu_pm_deinit(void)
849 {
850 }
851 #endif
852
853 static int __init arch_timer_register(void)
854 {
855 int err;
856 int ppi;
857
858 arch_timer_evt = alloc_percpu(struct clock_event_device);
859 if (!arch_timer_evt) {
860 err = -ENOMEM;
861 goto out;
862 }
863
864 ppi = arch_timer_ppi[arch_timer_uses_ppi];
865 switch (arch_timer_uses_ppi) {
866 case VIRT_PPI:
867 err = request_percpu_irq(ppi, arch_timer_handler_virt,
868 "arch_timer", arch_timer_evt);
869 break;
870 case PHYS_SECURE_PPI:
871 case PHYS_NONSECURE_PPI:
872 err = request_percpu_irq(ppi, arch_timer_handler_phys,
873 "arch_timer", arch_timer_evt);
874 if (!err && arch_timer_ppi[PHYS_NONSECURE_PPI]) {
875 ppi = arch_timer_ppi[PHYS_NONSECURE_PPI];
876 err = request_percpu_irq(ppi, arch_timer_handler_phys,
877 "arch_timer", arch_timer_evt);
878 if (err)
879 free_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI],
880 arch_timer_evt);
881 }
882 break;
883 case HYP_PPI:
884 err = request_percpu_irq(ppi, arch_timer_handler_phys,
885 "arch_timer", arch_timer_evt);
886 break;
887 default:
888 BUG();
889 }
890
891 if (err) {
892 pr_err("arch_timer: can't register interrupt %d (%d)\n",
893 ppi, err);
894 goto out_free;
895 }
896
897 err = arch_timer_cpu_pm_init();
898 if (err)
899 goto out_unreg_notify;
900
901
902 /* Register and immediately configure the timer on the boot CPU */
903 err = cpuhp_setup_state(CPUHP_AP_ARM_ARCH_TIMER_STARTING,
904 "clockevents/arm/arch_timer:starting",
905 arch_timer_starting_cpu, arch_timer_dying_cpu);
906 if (err)
907 goto out_unreg_cpupm;
908 return 0;
909
910 out_unreg_cpupm:
911 arch_timer_cpu_pm_deinit();
912
913 out_unreg_notify:
914 free_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], arch_timer_evt);
915 if (arch_timer_has_nonsecure_ppi())
916 free_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI],
917 arch_timer_evt);
918
919 out_free:
920 free_percpu(arch_timer_evt);
921 out:
922 return err;
923 }
924
925 static int __init arch_timer_mem_register(void __iomem *base, unsigned int irq)
926 {
927 int ret;
928 irq_handler_t func;
929 struct arch_timer *t;
930
931 t = kzalloc(sizeof(*t), GFP_KERNEL);
932 if (!t)
933 return -ENOMEM;
934
935 t->base = base;
936 t->evt.irq = irq;
937 __arch_timer_setup(ARCH_MEM_TIMER, &t->evt);
938
939 if (arch_timer_mem_use_virtual)
940 func = arch_timer_handler_virt_mem;
941 else
942 func = arch_timer_handler_phys_mem;
943
944 ret = request_irq(irq, func, IRQF_TIMER, "arch_mem_timer", &t->evt);
945 if (ret) {
946 pr_err("arch_timer: Failed to request mem timer irq\n");
947 kfree(t);
948 }
949
950 return ret;
951 }
952
953 static const struct of_device_id arch_timer_of_match[] __initconst = {
954 { .compatible = "arm,armv7-timer", },
955 { .compatible = "arm,armv8-timer", },
956 {},
957 };
958
959 static const struct of_device_id arch_timer_mem_of_match[] __initconst = {
960 { .compatible = "arm,armv7-timer-mem", },
961 {},
962 };
963
964 static bool __init
965 arch_timer_needs_probing(int type, const struct of_device_id *matches)
966 {
967 struct device_node *dn;
968 bool needs_probing = false;
969
970 dn = of_find_matching_node(NULL, matches);
971 if (dn && of_device_is_available(dn) && !(arch_timers_present & type))
972 needs_probing = true;
973 of_node_put(dn);
974
975 return needs_probing;
976 }
977
978 static int __init arch_timer_common_init(void)
979 {
980 unsigned mask = ARCH_CP15_TIMER | ARCH_MEM_TIMER;
981
982 /* Wait until both nodes are probed if we have two timers */
983 if ((arch_timers_present & mask) != mask) {
984 if (arch_timer_needs_probing(ARCH_MEM_TIMER, arch_timer_mem_of_match))
985 return 0;
986 if (arch_timer_needs_probing(ARCH_CP15_TIMER, arch_timer_of_match))
987 return 0;
988 }
989
990 arch_timer_banner(arch_timers_present);
991 arch_counter_register(arch_timers_present);
992 return arch_timer_arch_init();
993 }
994
995 static int __init arch_timer_init(void)
996 {
997 int ret;
998 /*
999 * If HYP mode is available, we know that the physical timer
1000 * has been configured to be accessible from PL1. Use it, so
1001 * that a guest can use the virtual timer instead.
1002 *
1003 * If no interrupt provided for virtual timer, we'll have to
1004 * stick to the physical timer. It'd better be accessible...
1005 *
1006 * On ARMv8.1 with VH extensions, the kernel runs in HYP. VHE
1007 * accesses to CNTP_*_EL1 registers are silently redirected to
1008 * their CNTHP_*_EL2 counterparts, and use a different PPI
1009 * number.
1010 */
1011 if (is_hyp_mode_available() || !arch_timer_ppi[VIRT_PPI]) {
1012 bool has_ppi;
1013
1014 if (is_kernel_in_hyp_mode()) {
1015 arch_timer_uses_ppi = HYP_PPI;
1016 has_ppi = !!arch_timer_ppi[HYP_PPI];
1017 } else {
1018 arch_timer_uses_ppi = PHYS_SECURE_PPI;
1019 has_ppi = (!!arch_timer_ppi[PHYS_SECURE_PPI] ||
1020 !!arch_timer_ppi[PHYS_NONSECURE_PPI]);
1021 }
1022
1023 if (!has_ppi) {
1024 pr_warn("arch_timer: No interrupt available, giving up\n");
1025 return -EINVAL;
1026 }
1027 }
1028
1029 ret = arch_timer_register();
1030 if (ret)
1031 return ret;
1032
1033 ret = arch_timer_common_init();
1034 if (ret)
1035 return ret;
1036
1037 arch_timer_kvm_info.virtual_irq = arch_timer_ppi[VIRT_PPI];
1038
1039 return 0;
1040 }
1041
1042 static int __init arch_timer_of_init(struct device_node *np)
1043 {
1044 int i;
1045
1046 if (arch_timers_present & ARCH_CP15_TIMER) {
1047 pr_warn("arch_timer: multiple nodes in dt, skipping\n");
1048 return 0;
1049 }
1050
1051 arch_timers_present |= ARCH_CP15_TIMER;
1052 for (i = PHYS_SECURE_PPI; i < MAX_TIMER_PPI; i++)
1053 arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
1054
1055 arch_timer_detect_rate(NULL, np);
1056
1057 arch_timer_c3stop = !of_property_read_bool(np, "always-on");
1058
1059 /* Check for globally applicable workarounds */
1060 arch_timer_check_ool_workaround(ate_match_dt, np);
1061 arch_timer_check_ool_workaround(ate_match_global_cap_id, NULL);
1062
1063 /*
1064 * If we cannot rely on firmware initializing the timer registers then
1065 * we should use the physical timers instead.
1066 */
1067 if (IS_ENABLED(CONFIG_ARM) &&
1068 of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
1069 arch_timer_uses_ppi = PHYS_SECURE_PPI;
1070
1071 /* On some systems, the counter stops ticking when in suspend. */
1072 arch_counter_suspend_stop = of_property_read_bool(np,
1073 "arm,no-tick-in-suspend");
1074
1075 return arch_timer_init();
1076 }
1077 CLOCKSOURCE_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_of_init);
1078 CLOCKSOURCE_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_of_init);
1079
1080 static int __init arch_timer_mem_init(struct device_node *np)
1081 {
1082 struct device_node *frame, *best_frame = NULL;
1083 void __iomem *cntctlbase, *base;
1084 unsigned int irq, ret = -EINVAL;
1085 u32 cnttidr;
1086
1087 arch_timers_present |= ARCH_MEM_TIMER;
1088 cntctlbase = of_iomap(np, 0);
1089 if (!cntctlbase) {
1090 pr_err("arch_timer: Can't find CNTCTLBase\n");
1091 return -ENXIO;
1092 }
1093
1094 cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
1095
1096 /*
1097 * Try to find a virtual capable frame. Otherwise fall back to a
1098 * physical capable frame.
1099 */
1100 for_each_available_child_of_node(np, frame) {
1101 int n;
1102 u32 cntacr;
1103
1104 if (of_property_read_u32(frame, "frame-number", &n)) {
1105 pr_err("arch_timer: Missing frame-number\n");
1106 of_node_put(frame);
1107 goto out;
1108 }
1109
1110 /* Try enabling everything, and see what sticks */
1111 cntacr = CNTACR_RFRQ | CNTACR_RWPT | CNTACR_RPCT |
1112 CNTACR_RWVT | CNTACR_RVOFF | CNTACR_RVCT;
1113 writel_relaxed(cntacr, cntctlbase + CNTACR(n));
1114 cntacr = readl_relaxed(cntctlbase + CNTACR(n));
1115
1116 if ((cnttidr & CNTTIDR_VIRT(n)) &&
1117 !(~cntacr & (CNTACR_RWVT | CNTACR_RVCT))) {
1118 of_node_put(best_frame);
1119 best_frame = frame;
1120 arch_timer_mem_use_virtual = true;
1121 break;
1122 }
1123
1124 if (~cntacr & (CNTACR_RWPT | CNTACR_RPCT))
1125 continue;
1126
1127 of_node_put(best_frame);
1128 best_frame = of_node_get(frame);
1129 }
1130
1131 ret= -ENXIO;
1132 base = arch_counter_base = of_io_request_and_map(best_frame, 0,
1133 "arch_mem_timer");
1134 if (IS_ERR(base)) {
1135 pr_err("arch_timer: Can't map frame's registers\n");
1136 goto out;
1137 }
1138
1139 if (arch_timer_mem_use_virtual)
1140 irq = irq_of_parse_and_map(best_frame, 1);
1141 else
1142 irq = irq_of_parse_and_map(best_frame, 0);
1143
1144 ret = -EINVAL;
1145 if (!irq) {
1146 pr_err("arch_timer: Frame missing %s irq",
1147 arch_timer_mem_use_virtual ? "virt" : "phys");
1148 goto out;
1149 }
1150
1151 arch_timer_detect_rate(base, np);
1152 ret = arch_timer_mem_register(base, irq);
1153 if (ret)
1154 goto out;
1155
1156 return arch_timer_common_init();
1157 out:
1158 iounmap(cntctlbase);
1159 of_node_put(best_frame);
1160 return ret;
1161 }
1162 CLOCKSOURCE_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem",
1163 arch_timer_mem_init);
1164
1165 #ifdef CONFIG_ACPI
1166 static int __init map_generic_timer_interrupt(u32 interrupt, u32 flags)
1167 {
1168 int trigger, polarity;
1169
1170 if (!interrupt)
1171 return 0;
1172
1173 trigger = (flags & ACPI_GTDT_INTERRUPT_MODE) ? ACPI_EDGE_SENSITIVE
1174 : ACPI_LEVEL_SENSITIVE;
1175
1176 polarity = (flags & ACPI_GTDT_INTERRUPT_POLARITY) ? ACPI_ACTIVE_LOW
1177 : ACPI_ACTIVE_HIGH;
1178
1179 return acpi_register_gsi(NULL, interrupt, trigger, polarity);
1180 }
1181
1182 /* Initialize per-processor generic timer */
1183 static int __init arch_timer_acpi_init(struct acpi_table_header *table)
1184 {
1185 struct acpi_table_gtdt *gtdt;
1186
1187 if (arch_timers_present & ARCH_CP15_TIMER) {
1188 pr_warn("arch_timer: already initialized, skipping\n");
1189 return -EINVAL;
1190 }
1191
1192 gtdt = container_of(table, struct acpi_table_gtdt, header);
1193
1194 arch_timers_present |= ARCH_CP15_TIMER;
1195
1196 arch_timer_ppi[PHYS_SECURE_PPI] =
1197 map_generic_timer_interrupt(gtdt->secure_el1_interrupt,
1198 gtdt->secure_el1_flags);
1199
1200 arch_timer_ppi[PHYS_NONSECURE_PPI] =
1201 map_generic_timer_interrupt(gtdt->non_secure_el1_interrupt,
1202 gtdt->non_secure_el1_flags);
1203
1204 arch_timer_ppi[VIRT_PPI] =
1205 map_generic_timer_interrupt(gtdt->virtual_timer_interrupt,
1206 gtdt->virtual_timer_flags);
1207
1208 arch_timer_ppi[HYP_PPI] =
1209 map_generic_timer_interrupt(gtdt->non_secure_el2_interrupt,
1210 gtdt->non_secure_el2_flags);
1211
1212 /* Get the frequency from CNTFRQ */
1213 arch_timer_detect_rate(NULL, NULL);
1214
1215 /* Always-on capability */
1216 arch_timer_c3stop = !(gtdt->non_secure_el1_flags & ACPI_GTDT_ALWAYS_ON);
1217
1218 /* Check for globally applicable workarounds */
1219 arch_timer_check_ool_workaround(ate_match_global_cap_id, NULL);
1220
1221 arch_timer_init();
1222 return 0;
1223 }
1224 CLOCKSOURCE_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
1225 #endif