]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/clocksource/arm_arch_timer.c
arm64: arch_timer: Enable CNTVCT_EL0 trap if workaround is enabled
[mirror_ubuntu-zesty-kernel.git] / drivers / clocksource / arm_arch_timer.c
CommitLineData
8a4da6e3
MR
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 */
f005bd7e
MZ
11
12#define pr_fmt(fmt) "arm_arch_timer: " fmt
13
8a4da6e3
MR
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>
346e7480 19#include <linux/cpu_pm.h>
8a4da6e3 20#include <linux/clockchips.h>
7c8f1e78 21#include <linux/clocksource.h>
8a4da6e3
MR
22#include <linux/interrupt.h>
23#include <linux/of_irq.h>
22006994 24#include <linux/of_address.h>
8a4da6e3 25#include <linux/io.h>
22006994 26#include <linux/slab.h>
65cd4f6c 27#include <linux/sched_clock.h>
b09ca1ec 28#include <linux/acpi.h>
8a4da6e3
MR
29
30#include <asm/arch_timer.h>
8266891e 31#include <asm/virt.h>
8a4da6e3
MR
32
33#include <clocksource/arm_arch_timer.h>
34
3f968fa1
FW
35#undef pr_fmt
36#define pr_fmt(fmt) "arch_timer: " fmt
37
22006994
SB
38#define CNTTIDR 0x08
39#define CNTTIDR_VIRT(n) (BIT(1) << ((n) * 4))
40
e392d603
RM
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
22006994
SB
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
22006994
SB
57static unsigned arch_timers_present __initdata;
58
59static void __iomem *arch_counter_base;
60
61struct 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
8a4da6e3 68static u32 arch_timer_rate;
5d38740d 69static int arch_timer_ppi[ARCH_TIMER_MAX_TIMER_PPI];
8a4da6e3
MR
70
71static struct clock_event_device __percpu *arch_timer_evt;
72
5d38740d 73static enum arch_timer_ppi_nr arch_timer_uses_ppi = ARCH_TIMER_VIRT_PPI;
82a56194 74static bool arch_timer_c3stop;
22006994 75static bool arch_timer_mem_use_virtual;
d8ec7595 76static bool arch_counter_suspend_stop;
1e14f36a 77static bool vdso_default = true;
8a4da6e3 78
46fd5c6b
WD
79static bool evtstrm_enable = IS_ENABLED(CONFIG_ARM_ARCH_TIMER_EVTSTREAM);
80
81static int __init early_evtstrm_cfg(char *buf)
82{
83 return strtobool(buf, &evtstrm_enable);
84}
85early_param("clocksource.arm_arch_timer.evtstrm", early_evtstrm_cfg);
86
8a4da6e3
MR
87/*
88 * Architected system timer support.
89 */
90
5271a34c
MZ
91static __always_inline
92void 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
120static __always_inline
121u32 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
d7765b0c
MZ
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 */
159u64 (*arch_timer_read_counter)(void) = arch_counter_get_cntvct;
160
161static u64 arch_counter_read(struct clocksource *cs)
162{
163 return arch_timer_read_counter();
164}
165
166static u64 arch_counter_read_cc(const struct cyclecounter *cc)
167{
168 return arch_timer_read_counter();
169}
170
171static 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
179static struct cyclecounter cyclecounter = {
180 .read = arch_counter_read_cc,
181 .mask = CLOCKSOURCE_MASK(56),
182};
183
ea36ec17
MZ
184struct 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
f6dc1576 190#ifdef CONFIG_FSL_ERRATUM_A008585
70d016c2
DT
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
209static u32 notrace fsl_a008585_read_cntp_tval_el0(void)
f6dc1576
SW
210{
211 return __fsl_a008585_read_reg(cntp_tval_el0);
212}
213
70d016c2 214static u32 notrace fsl_a008585_read_cntv_tval_el0(void)
f6dc1576
SW
215{
216 return __fsl_a008585_read_reg(cntv_tval_el0);
217}
218
70d016c2 219static u64 notrace fsl_a008585_read_cntvct_el0(void)
f6dc1576
SW
220{
221 return __fsl_a008585_read_reg(cntvct_el0);
222}
70d016c2
DT
223#endif
224
cc104462
DT
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
250static u32 notrace hisi_161010101_read_cntp_tval_el0(void)
251{
252 return __hisi_161010101_read_reg(cntp_tval_el0);
253}
254
255static u32 notrace hisi_161010101_read_cntv_tval_el0(void)
256{
257 return __hisi_161010101_read_reg(cntv_tval_el0);
258}
259
260static u64 notrace hisi_161010101_read_cntvct_el0(void)
261{
262 return __hisi_161010101_read_reg(cntvct_el0);
263}
bde4b58c
MZ
264
265static 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};
cc104462
DT
287#endif
288
47f291e2
MZ
289#ifdef CONFIG_ARM64_ERRATUM_858921
290static 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
70d016c2 300#ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
2c80d34c
MZ
301DEFINE_PER_CPU(const struct arch_timer_erratum_workaround *,
302 timer_unstable_counter_workaround);
70d016c2
DT
303EXPORT_SYMBOL_GPL(timer_unstable_counter_workaround);
304
305DEFINE_STATIC_KEY_FALSE(arch_timer_read_ool_enabled);
306EXPORT_SYMBOL_GPL(arch_timer_read_ool_enabled);
307
4b76f7fb
MZ
308static 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
326static 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
333static 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
70d016c2
DT
340static const struct arch_timer_erratum_workaround ool_workarounds[] = {
341#ifdef CONFIG_FSL_ERRATUM_A008585
342 {
63d321a7 343 .match_type = ate_match_dt,
70d016c2 344 .id = "fsl,erratum-a008585",
63d321a7 345 .desc = "Freescale erratum a005858",
70d016c2
DT
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,
5d9decde
MZ
349 .set_next_event_phys = erratum_set_next_event_tval_phys,
350 .set_next_event_virt = erratum_set_next_event_tval_virt,
70d016c2
DT
351 },
352#endif
cc104462
DT
353#ifdef CONFIG_HISILICON_ERRATUM_161010101
354 {
63d321a7 355 .match_type = ate_match_dt,
cc104462 356 .id = "hisilicon,erratum-161010101",
63d321a7 357 .desc = "HiSilicon erratum 161010101",
cc104462
DT
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,
5d9decde
MZ
361 .set_next_event_phys = erratum_set_next_event_tval_phys,
362 .set_next_event_virt = erratum_set_next_event_tval_virt,
cc104462 363 },
bde4b58c
MZ
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 },
cc104462 374#endif
47f291e2
MZ
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
70d016c2 383};
63d321a7
MZ
384
385typedef bool (*ate_match_fn_t)(const struct arch_timer_erratum_workaround *,
386 const void *);
387
388static
389bool 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
6d0a425d
MZ
397static
398bool 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
4e7813c2
MZ
404static
405bool 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
ea36ec17
MZ
411
412static
413bool 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
63d321a7
MZ
433static const struct arch_timer_erratum_workaround *
434arch_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
451static
2c80d34c
MZ
452void arch_timer_enable_workaround(const struct arch_timer_erratum_workaround *wa,
453 bool local)
63d321a7 454{
2c80d34c
MZ
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
63d321a7 464 static_branch_enable(&arch_timer_read_ool_enabled);
1e14f36a
MZ
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 }
63d321a7
MZ
476}
477
478static 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;
4e7813c2 483 bool local = false;
63d321a7
MZ
484
485 switch (type) {
486 case ate_match_dt:
487 match_fn = arch_timer_check_dt_erratum;
488 break;
6d0a425d
MZ
489 case ate_match_global_cap_id:
490 match_fn = arch_timer_check_global_cap_erratum;
491 break;
4e7813c2
MZ
492 case ate_match_local_cap_id:
493 match_fn = arch_timer_check_local_cap_erratum;
494 local = true;
495 break;
ea36ec17
MZ
496 case ate_match_acpi_oem_info:
497 match_fn = arch_timer_check_acpi_oem_erratum;
498 break;
df611637 499 default:
500 pr_err("arch_timer: Unknown erratum workaround type specified.\n");
501 return;
63d321a7
MZ
502 }
503
504 wa = arch_timer_iterate_errata(type, match_fn, arg);
505 if (!wa)
506 return;
507
4e7813c2 508 if (static_branch_unlikely(&arch_timer_read_ool_enabled)) {
2c80d34c
MZ
509 const struct arch_timer_erratum_workaround *__wa;
510 __wa = __this_cpu_read(timer_unstable_counter_workaround);
511 if (__wa && wa != __wa)
4e7813c2 512 pr_warn("Can't enable workaround for %s (clashes with %s\n)",
2c80d34c
MZ
513 wa->desc, __wa->desc);
514
515 if (__wa)
516 return;
4e7813c2
MZ
517 }
518
2c80d34c 519 arch_timer_enable_workaround(wa, local);
4e7813c2
MZ
520 pr_info("Enabling %s workaround for %s\n",
521 local ? "local" : "global", wa->desc);
63d321a7
MZ
522}
523
5d9decde
MZ
524#define erratum_handler(fn, r, ...) \
525({ \
526 bool __val; \
2c80d34c
MZ
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 } \
5d9decde
MZ
536 } else { \
537 __val = false; \
538 } \
539 __val; \
540})
541
1e14f36a
MZ
542static 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}
63d321a7
MZ
549#else
550#define arch_timer_check_ool_workaround(t,a) do { } while(0)
4b76f7fb
MZ
551#define erratum_set_next_event_tval_virt(...) ({BUG(); 0;})
552#define erratum_set_next_event_tval_phys(...) ({BUG(); 0;})
5d9decde 553#define erratum_handler(fn, r, ...) ({false;})
1e14f36a 554#define arch_timer_this_cpu_has_cntvct_wa() ({false;})
70d016c2 555#endif /* CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND */
f6dc1576 556
e09f3cc0 557static __always_inline irqreturn_t timer_handler(const int access,
8a4da6e3
MR
558 struct clock_event_device *evt)
559{
560 unsigned long ctrl;
cfb6d656 561
60faddf6 562 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, evt);
8a4da6e3
MR
563 if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
564 ctrl |= ARCH_TIMER_CTRL_IT_MASK;
60faddf6 565 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, evt);
8a4da6e3
MR
566 evt->event_handler(evt);
567 return IRQ_HANDLED;
568 }
569
570 return IRQ_NONE;
571}
572
573static 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
580static 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
22006994
SB
587static 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
594static 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
46c5bfdd
VK
601static __always_inline int timer_shutdown(const int access,
602 struct clock_event_device *clk)
8a4da6e3
MR
603{
604 unsigned long ctrl;
46c5bfdd
VK
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;
8a4da6e3
MR
611}
612
46c5bfdd 613static int arch_timer_shutdown_virt(struct clock_event_device *clk)
8a4da6e3 614{
46c5bfdd 615 return timer_shutdown(ARCH_TIMER_VIRT_ACCESS, clk);
8a4da6e3
MR
616}
617
46c5bfdd 618static int arch_timer_shutdown_phys(struct clock_event_device *clk)
8a4da6e3 619{
46c5bfdd 620 return timer_shutdown(ARCH_TIMER_PHYS_ACCESS, clk);
8a4da6e3
MR
621}
622
46c5bfdd 623static int arch_timer_shutdown_virt_mem(struct clock_event_device *clk)
22006994 624{
46c5bfdd 625 return timer_shutdown(ARCH_TIMER_MEM_VIRT_ACCESS, clk);
8a4da6e3
MR
626}
627
46c5bfdd 628static int arch_timer_shutdown_phys_mem(struct clock_event_device *clk)
22006994 629{
46c5bfdd 630 return timer_shutdown(ARCH_TIMER_MEM_PHYS_ACCESS, clk);
22006994
SB
631}
632
60faddf6 633static __always_inline void set_next_event(const int access, unsigned long evt,
cfb6d656 634 struct clock_event_device *clk)
8a4da6e3
MR
635{
636 unsigned long ctrl;
60faddf6 637 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
8a4da6e3
MR
638 ctrl |= ARCH_TIMER_CTRL_ENABLE;
639 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
60faddf6
SB
640 arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt, clk);
641 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
8a4da6e3
MR
642}
643
644static int arch_timer_set_next_event_virt(unsigned long evt,
60faddf6 645 struct clock_event_device *clk)
8a4da6e3 646{
5d9decde
MZ
647 int ret;
648
649 if (erratum_handler(set_next_event_virt, ret, evt, clk))
650 return ret;
4b76f7fb 651
60faddf6 652 set_next_event(ARCH_TIMER_VIRT_ACCESS, evt, clk);
8a4da6e3
MR
653 return 0;
654}
655
656static int arch_timer_set_next_event_phys(unsigned long evt,
60faddf6 657 struct clock_event_device *clk)
8a4da6e3 658{
5d9decde
MZ
659 int ret;
660
661 if (erratum_handler(set_next_event_phys, ret, evt, clk))
662 return ret;
4b76f7fb 663
60faddf6 664 set_next_event(ARCH_TIMER_PHYS_ACCESS, evt, clk);
8a4da6e3
MR
665 return 0;
666}
667
22006994
SB
668static int arch_timer_set_next_event_virt_mem(unsigned long evt,
669 struct clock_event_device *clk)
8a4da6e3 670{
22006994
SB
671 set_next_event(ARCH_TIMER_MEM_VIRT_ACCESS, evt, clk);
672 return 0;
673}
674
675static 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
cfb6d656
TG
682static void __arch_timer_setup(unsigned type,
683 struct clock_event_device *clk)
22006994
SB
684{
685 clk->features = CLOCK_EVT_FEAT_ONESHOT;
686
ebcb3a17 687 if (type == ARCH_TIMER_TYPE_CP15) {
82a56194
LP
688 if (arch_timer_c3stop)
689 clk->features |= CLOCK_EVT_FEAT_C3STOP;
22006994
SB
690 clk->name = "arch_sys_timer";
691 clk->rating = 450;
692 clk->cpumask = cpumask_of(smp_processor_id());
f81f03fa
MZ
693 clk->irq = arch_timer_ppi[arch_timer_uses_ppi];
694 switch (arch_timer_uses_ppi) {
5d38740d 695 case ARCH_TIMER_VIRT_PPI:
46c5bfdd 696 clk->set_state_shutdown = arch_timer_shutdown_virt;
cf8c5009 697 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt;
22006994 698 clk->set_next_event = arch_timer_set_next_event_virt;
f81f03fa 699 break;
5d38740d
FW
700 case ARCH_TIMER_PHYS_SECURE_PPI:
701 case ARCH_TIMER_PHYS_NONSECURE_PPI:
702 case ARCH_TIMER_HYP_PPI:
46c5bfdd 703 clk->set_state_shutdown = arch_timer_shutdown_phys;
cf8c5009 704 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys;
22006994 705 clk->set_next_event = arch_timer_set_next_event_phys;
f81f03fa
MZ
706 break;
707 default:
708 BUG();
22006994 709 }
f6dc1576 710
4e7813c2 711 arch_timer_check_ool_workaround(ate_match_local_cap_id, NULL);
8a4da6e3 712 } else {
7b52ad2e 713 clk->features |= CLOCK_EVT_FEAT_DYNIRQ;
22006994
SB
714 clk->name = "arch_mem_timer";
715 clk->rating = 400;
716 clk->cpumask = cpu_all_mask;
717 if (arch_timer_mem_use_virtual) {
46c5bfdd 718 clk->set_state_shutdown = arch_timer_shutdown_virt_mem;
cf8c5009 719 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt_mem;
22006994
SB
720 clk->set_next_event =
721 arch_timer_set_next_event_virt_mem;
722 } else {
46c5bfdd 723 clk->set_state_shutdown = arch_timer_shutdown_phys_mem;
cf8c5009 724 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys_mem;
22006994
SB
725 clk->set_next_event =
726 arch_timer_set_next_event_phys_mem;
727 }
8a4da6e3
MR
728 }
729
46c5bfdd 730 clk->set_state_shutdown(clk);
8a4da6e3 731
22006994
SB
732 clockevents_config_and_register(clk, arch_timer_rate, 0xf, 0x7fffffff);
733}
8a4da6e3 734
e1ce5c7a
NL
735static 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
037f6377
WD
750static 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
8b8dde00
NL
763static void arch_counter_set_user_access(void)
764{
765 u32 cntkctl = arch_timer_get_cntkctl();
766
1e14f36a 767 /* Disable user access to the timers and both counters */
8b8dde00
NL
768 /* Also disable virtual event stream */
769 cntkctl &= ~(ARCH_TIMER_USR_PT_ACCESS_EN
770 | ARCH_TIMER_USR_VT_ACCESS_EN
1e14f36a 771 | ARCH_TIMER_USR_VCT_ACCESS_EN
8b8dde00
NL
772 | ARCH_TIMER_VIRT_EVT_EN
773 | ARCH_TIMER_USR_PCT_ACCESS_EN);
774
1e14f36a
MZ
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;
8b8dde00
NL
784
785 arch_timer_set_cntkctl(cntkctl);
786}
787
f81f03fa
MZ
788static bool arch_timer_has_nonsecure_ppi(void)
789{
5d38740d
FW
790 return (arch_timer_uses_ppi == ARCH_TIMER_PHYS_SECURE_PPI &&
791 arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
f81f03fa
MZ
792}
793
f005bd7e
MZ
794static 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
7e86e8bd 807static int arch_timer_starting_cpu(unsigned int cpu)
22006994 808{
7e86e8bd 809 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
f005bd7e 810 u32 flags;
7e86e8bd 811
ebcb3a17 812 __arch_timer_setup(ARCH_TIMER_TYPE_CP15, clk);
8a4da6e3 813
f005bd7e
MZ
814 flags = check_ppi_trigger(arch_timer_ppi[arch_timer_uses_ppi]);
815 enable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], flags);
f81f03fa 816
f005bd7e 817 if (arch_timer_has_nonsecure_ppi()) {
5d38740d
FW
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);
f005bd7e 821 }
8a4da6e3
MR
822
823 arch_counter_set_user_access();
46fd5c6b 824 if (evtstrm_enable)
037f6377 825 arch_timer_configure_evtstream();
8a4da6e3
MR
826
827 return 0;
828}
829
2a1f5645
FW
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 */
835static void arch_timer_of_configure_rate(u32 rate, struct device_node *np)
8a4da6e3 836{
22006994
SB
837 /* Who has more than one independent system counter? */
838 if (arch_timer_rate)
839 return;
8a4da6e3 840
2a1f5645
FW
841 if (of_property_read_u32(np, "clock-frequency", &arch_timer_rate))
842 arch_timer_rate = rate;
8a4da6e3 843
22006994
SB
844 /* Check the timer frequency. */
845 if (arch_timer_rate == 0)
3f968fa1 846 pr_warn("frequency not available\n");
22006994
SB
847}
848
849static void arch_timer_banner(unsigned type)
850{
3f968fa1 851 pr_info("%s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
ebcb3a17
FW
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" : "",
3f968fa1
FW
856 (unsigned long)arch_timer_rate / 1000000,
857 (unsigned long)(arch_timer_rate / 10000) % 100,
ebcb3a17 858 type & ARCH_TIMER_TYPE_CP15 ?
5d38740d 859 (arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI) ? "virt" : "phys" :
22006994 860 "",
ebcb3a17
FW
861 type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ? "/" : "",
862 type & ARCH_TIMER_TYPE_MEM ?
22006994
SB
863 arch_timer_mem_use_virtual ? "virt" : "phys" :
864 "");
8a4da6e3
MR
865}
866
867u32 arch_timer_get_rate(void)
868{
869 return arch_timer_rate;
870}
871
22006994 872static u64 arch_counter_get_cntvct_mem(void)
8a4da6e3 873{
22006994
SB
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;
8a4da6e3
MR
883}
884
b4d6ce97
JG
885static struct arch_timer_kvm_info arch_timer_kvm_info;
886
887struct arch_timer_kvm_info *arch_timer_get_kvm_info(void)
888{
889 return &arch_timer_kvm_info;
890}
8a4da6e3 891
22006994
SB
892static void __init arch_counter_register(unsigned type)
893{
894 u64 start_count;
895
896 /* Register the CP15 based counter if we have one */
ebcb3a17 897 if (type & ARCH_TIMER_TYPE_CP15) {
5d38740d
FW
898 if (IS_ENABLED(CONFIG_ARM64) ||
899 arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI)
0b46b8a7
SR
900 arch_timer_read_counter = arch_counter_get_cntvct;
901 else
902 arch_timer_read_counter = arch_counter_get_cntpct;
f6dc1576 903
1e14f36a 904 clocksource_counter.archdata.vdso_direct = vdso_default;
423bd69e 905 } else {
22006994 906 arch_timer_read_counter = arch_counter_get_cntvct_mem;
423bd69e
NL
907 }
908
d8ec7595
BN
909 if (!arch_counter_suspend_stop)
910 clocksource_counter.flags |= CLOCK_SOURCE_SUSPEND_NONSTOP;
22006994
SB
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;
b4d6ce97
JG
915 timecounter_init(&arch_timer_kvm_info.timecounter,
916 &cyclecounter, start_count);
4a7d3e8a
TR
917
918 /* 56 bits minimum, so we assume worst case rollover */
919 sched_clock_register(arch_timer_read_counter, 56, arch_timer_rate);
22006994
SB
920}
921
8c37bb3a 922static void arch_timer_stop(struct clock_event_device *clk)
8a4da6e3 923{
3f968fa1 924 pr_debug("disable IRQ%d cpu #%d\n", clk->irq, smp_processor_id());
8a4da6e3 925
f81f03fa
MZ
926 disable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi]);
927 if (arch_timer_has_nonsecure_ppi())
5d38740d 928 disable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
8a4da6e3 929
46c5bfdd 930 clk->set_state_shutdown(clk);
8a4da6e3
MR
931}
932
7e86e8bd 933static int arch_timer_dying_cpu(unsigned int cpu)
8a4da6e3 934{
7e86e8bd 935 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
8a4da6e3 936
7e86e8bd
RC
937 arch_timer_stop(clk);
938 return 0;
8a4da6e3
MR
939}
940
346e7480
SH
941#ifdef CONFIG_CPU_PM
942static unsigned int saved_cntkctl;
943static 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
953static struct notifier_block arch_timer_cpu_pm_notifier = {
954 .notifier_call = arch_timer_cpu_pm_notify,
955};
956
957static int __init arch_timer_cpu_pm_init(void)
958{
959 return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier);
960}
7e86e8bd
RC
961
962static void __init arch_timer_cpu_pm_deinit(void)
963{
964 WARN_ON(cpu_pm_unregister_notifier(&arch_timer_cpu_pm_notifier));
965}
966
346e7480
SH
967#else
968static int __init arch_timer_cpu_pm_init(void)
969{
970 return 0;
971}
7e86e8bd
RC
972
973static void __init arch_timer_cpu_pm_deinit(void)
974{
975}
346e7480
SH
976#endif
977
8a4da6e3
MR
978static int __init arch_timer_register(void)
979{
980 int err;
981 int ppi;
982
8a4da6e3
MR
983 arch_timer_evt = alloc_percpu(struct clock_event_device);
984 if (!arch_timer_evt) {
985 err = -ENOMEM;
986 goto out;
987 }
988
f81f03fa
MZ
989 ppi = arch_timer_ppi[arch_timer_uses_ppi];
990 switch (arch_timer_uses_ppi) {
5d38740d 991 case ARCH_TIMER_VIRT_PPI:
8a4da6e3
MR
992 err = request_percpu_irq(ppi, arch_timer_handler_virt,
993 "arch_timer", arch_timer_evt);
f81f03fa 994 break;
5d38740d
FW
995 case ARCH_TIMER_PHYS_SECURE_PPI:
996 case ARCH_TIMER_PHYS_NONSECURE_PPI:
8a4da6e3
MR
997 err = request_percpu_irq(ppi, arch_timer_handler_phys,
998 "arch_timer", arch_timer_evt);
8e35c293 999 if (!err && arch_timer_has_nonsecure_ppi()) {
5d38740d 1000 ppi = arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI];
8a4da6e3
MR
1001 err = request_percpu_irq(ppi, arch_timer_handler_phys,
1002 "arch_timer", arch_timer_evt);
1003 if (err)
5d38740d 1004 free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_SECURE_PPI],
8a4da6e3
MR
1005 arch_timer_evt);
1006 }
f81f03fa 1007 break;
5d38740d 1008 case ARCH_TIMER_HYP_PPI:
f81f03fa
MZ
1009 err = request_percpu_irq(ppi, arch_timer_handler_phys,
1010 "arch_timer", arch_timer_evt);
1011 break;
1012 default:
1013 BUG();
8a4da6e3
MR
1014 }
1015
1016 if (err) {
3f968fa1 1017 pr_err("can't register interrupt %d (%d)\n", ppi, err);
8a4da6e3
MR
1018 goto out_free;
1019 }
1020
346e7480
SH
1021 err = arch_timer_cpu_pm_init();
1022 if (err)
1023 goto out_unreg_notify;
1024
8a4da6e3 1025
7e86e8bd
RC
1026 /* Register and immediately configure the timer on the boot CPU */
1027 err = cpuhp_setup_state(CPUHP_AP_ARM_ARCH_TIMER_STARTING,
73c1b41e 1028 "clockevents/arm/arch_timer:starting",
7e86e8bd
RC
1029 arch_timer_starting_cpu, arch_timer_dying_cpu);
1030 if (err)
1031 goto out_unreg_cpupm;
8a4da6e3
MR
1032 return 0;
1033
7e86e8bd
RC
1034out_unreg_cpupm:
1035 arch_timer_cpu_pm_deinit();
1036
346e7480 1037out_unreg_notify:
f81f03fa
MZ
1038 free_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], arch_timer_evt);
1039 if (arch_timer_has_nonsecure_ppi())
5d38740d 1040 free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
8a4da6e3 1041 arch_timer_evt);
8a4da6e3
MR
1042
1043out_free:
1044 free_percpu(arch_timer_evt);
1045out:
1046 return err;
1047}
1048
22006994
SB
1049static 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;
ebcb3a17 1061 __arch_timer_setup(ARCH_TIMER_TYPE_MEM, &t->evt);
22006994
SB
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) {
3f968fa1 1070 pr_err("Failed to request mem timer irq\n");
22006994
SB
1071 kfree(t);
1072 }
1073
1074 return ret;
1075}
1076
1077static const struct of_device_id arch_timer_of_match[] __initconst = {
1078 { .compatible = "arm,armv7-timer", },
1079 { .compatible = "arm,armv8-timer", },
1080 {},
1081};
1082
1083static const struct of_device_id arch_timer_mem_of_match[] __initconst = {
1084 { .compatible = "arm,armv7-timer-mem", },
1085 {},
1086};
1087
5438ee3b 1088static bool __init arch_timer_needs_of_probing(void)
c387f07e
SH
1089{
1090 struct device_node *dn;
566e6dfa 1091 bool needs_probing = false;
5438ee3b 1092 unsigned int mask = ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM;
c387f07e 1093
5438ee3b
FW
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))
566e6dfa 1108 needs_probing = true;
5438ee3b 1109
c387f07e
SH
1110 of_node_put(dn);
1111
566e6dfa 1112 return needs_probing;
c387f07e
SH
1113}
1114
3c0731db 1115static int __init arch_timer_common_init(void)
22006994 1116{
22006994
SB
1117 arch_timer_banner(arch_timers_present);
1118 arch_counter_register(arch_timers_present);
3c0731db 1119 return arch_timer_arch_init();
22006994
SB
1120}
1121
8e35c293
FW
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 */
1140static enum arch_timer_ppi_nr __init arch_timer_select_ppi(void)
8a4da6e3 1141{
8e35c293
FW
1142 if (is_kernel_in_hyp_mode())
1143 return ARCH_TIMER_HYP_PPI;
f81f03fa 1144
8e35c293
FW
1145 if (!is_hyp_mode_available() && arch_timer_ppi[ARCH_TIMER_VIRT_PPI])
1146 return ARCH_TIMER_VIRT_PPI;
8a4da6e3 1147
8e35c293
FW
1148 if (IS_ENABLED(CONFIG_ARM64))
1149 return ARCH_TIMER_PHYS_NONSECURE_PPI;
1150
1151 return ARCH_TIMER_PHYS_SECURE_PPI;
1152}
1153
3c0731db 1154static int __init arch_timer_of_init(struct device_node *np)
b09ca1ec 1155{
e93ed2db 1156 int i, ret;
2a1f5645 1157 u32 rate;
b09ca1ec 1158
ebcb3a17 1159 if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
3f968fa1 1160 pr_warn("multiple nodes in dt, skipping\n");
3c0731db 1161 return 0;
b09ca1ec
HG
1162 }
1163
ebcb3a17 1164 arch_timers_present |= ARCH_TIMER_TYPE_CP15;
5d38740d 1165 for (i = ARCH_TIMER_PHYS_SECURE_PPI; i < ARCH_TIMER_MAX_TIMER_PPI; i++)
b09ca1ec
HG
1166 arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
1167
e93ed2db
FW
1168 arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI];
1169
0ecdf0aa 1170 rate = arch_timer_get_cntfrq();
2a1f5645 1171 arch_timer_of_configure_rate(rate, np);
b09ca1ec
HG
1172
1173 arch_timer_c3stop = !of_property_read_bool(np, "always-on");
1174
63d321a7
MZ
1175 /* Check for globally applicable workarounds */
1176 arch_timer_check_ool_workaround(ate_match_dt, np);
6d0a425d 1177 arch_timer_check_ool_workaround(ate_match_global_cap_id, NULL);
f6dc1576 1178
b09ca1ec
HG
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"))
5d38740d 1185 arch_timer_uses_ppi = ARCH_TIMER_PHYS_SECURE_PPI;
8e35c293
FW
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 }
b09ca1ec 1193
d8ec7595
BN
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
e93ed2db
FW
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();
b09ca1ec 1206}
177cf6e5
DL
1207CLOCKSOURCE_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_of_init);
1208CLOCKSOURCE_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_of_init);
22006994 1209
0ecdf0aa
FW
1210static u32 __init
1211arch_timer_mem_frame_get_cntfrq(struct arch_timer_mem_frame *frame)
22006994 1212{
0ecdf0aa
FW
1213 void __iomem *base;
1214 u32 rate;
22006994 1215
0ecdf0aa
FW
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(frame + CNTFRQ);
1223
1224 iounmap(frame);
1225
1226 return rate;
1227}
1228
1229static struct arch_timer_mem_frame * __init
1230arch_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);
22006994 1238 if (!cntctlbase) {
0ecdf0aa
FW
1239 pr_err("Can't map CNTCTLBase @ %pa\n",
1240 &timer_mem->cntctlbase);
1241 return NULL;
22006994
SB
1242 }
1243
1244 cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
22006994
SB
1245
1246 /*
1247 * Try to find a virtual capable frame. Otherwise fall back to a
1248 * physical capable frame.
1249 */
0ecdf0aa
FW
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;
22006994 1253
0ecdf0aa
FW
1254 frame = &timer_mem->frame[i];
1255 if (!frame->valid)
1256 continue;
22006994 1257
e392d603 1258 /* Try enabling everything, and see what sticks */
0ecdf0aa
FW
1259 writel_relaxed(cntacr, cntctlbase + CNTACR(i));
1260 cntacr = readl_relaxed(cntctlbase + CNTACR(i));
e392d603 1261
0ecdf0aa 1262 if ((cnttidr & CNTTIDR_VIRT(i)) &&
e392d603 1263 !(~cntacr & (CNTACR_RWVT | CNTACR_RVCT))) {
22006994
SB
1264 best_frame = frame;
1265 arch_timer_mem_use_virtual = true;
1266 break;
1267 }
e392d603
RM
1268
1269 if (~cntacr & (CNTACR_RWPT | CNTACR_RPCT))
1270 continue;
1271
0ecdf0aa 1272 best_frame = frame;
22006994
SB
1273 }
1274
0ecdf0aa
FW
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 frame;
1282}
1283
1284static int __init
1285arch_timer_mem_frame_register(struct arch_timer_mem_frame *frame)
1286{
1287 void __iomem *base;
1288 int ret, irq = 0;
22006994
SB
1289
1290 if (arch_timer_mem_use_virtual)
0ecdf0aa 1291 irq = frame->virt_irq;
22006994 1292 else
0ecdf0aa 1293 irq = frame->phys_irq;
e392d603 1294
22006994 1295 if (!irq) {
3f968fa1 1296 pr_err("Frame missing %s irq.\n",
cfb6d656 1297 arch_timer_mem_use_virtual ? "virt" : "phys");
0ecdf0aa
FW
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;
22006994
SB
1309 }
1310
3c0731db 1311 ret = arch_timer_mem_register(base, irq);
0ecdf0aa
FW
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
1323static 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))
3c0731db 1337 goto out;
0ecdf0aa
FW
1338 timer_mem->cntctlbase = res.start;
1339 timer_mem->size = resource_size(&res);
3c0731db 1340
0ecdf0aa
FW
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())
e93ed2db 1390 ret = arch_timer_common_init();
e392d603 1391out:
0ecdf0aa 1392 kfree(timer_mem);
3c0731db 1393 return ret;
22006994 1394}
177cf6e5 1395CLOCKSOURCE_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem",
0ecdf0aa 1396 arch_timer_mem_of_init);
b09ca1ec 1397
d189cb58 1398#ifdef CONFIG_ACPI_GTDT
8dbffa16
FW
1399static int __init
1400arch_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
1426static 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);
1463out:
1464 kfree(timers);
1465 return ret;
1466}
1467
1468/* Initialize per-processor generic timer and memory-mapped timer(if present) */
b09ca1ec
HG
1469static int __init arch_timer_acpi_init(struct acpi_table_header *table)
1470{
8dbffa16 1471 int ret, platform_timer_count;
b09ca1ec 1472
ebcb3a17 1473 if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
3f968fa1 1474 pr_warn("already initialized, skipping\n");
b09ca1ec
HG
1475 return -EINVAL;
1476 }
1477
ebcb3a17 1478 arch_timers_present |= ARCH_TIMER_TYPE_CP15;
b09ca1ec 1479
8dbffa16 1480 ret = acpi_gtdt_init(table, &platform_timer_count);
d189cb58
FW
1481 if (ret) {
1482 pr_err("Failed to init GTDT table.\n");
1483 return ret;
1484 }
b09ca1ec 1485
5d38740d 1486 arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI] =
d189cb58 1487 acpi_gtdt_map_ppi(ARCH_TIMER_PHYS_NONSECURE_PPI);
b09ca1ec 1488
5d38740d 1489 arch_timer_ppi[ARCH_TIMER_VIRT_PPI] =
d189cb58 1490 acpi_gtdt_map_ppi(ARCH_TIMER_VIRT_PPI);
b09ca1ec 1491
5d38740d 1492 arch_timer_ppi[ARCH_TIMER_HYP_PPI] =
d189cb58 1493 acpi_gtdt_map_ppi(ARCH_TIMER_HYP_PPI);
b09ca1ec 1494
e93ed2db
FW
1495 arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI];
1496
2a1f5645
FW
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 }
b09ca1ec 1506
8e35c293
FW
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
b09ca1ec 1513 /* Always-on capability */
d189cb58 1514 arch_timer_c3stop = acpi_gtdt_c3stop(arch_timer_uses_ppi);
b09ca1ec 1515
6d0a425d
MZ
1516 /* Check for globally applicable workarounds */
1517 arch_timer_check_ool_workaround(ate_match_global_cap_id, NULL);
ea36ec17 1518 arch_timer_check_ool_workaround(ate_match_acpi_oem_info, table);
6d0a425d 1519
e93ed2db
FW
1520 ret = arch_timer_register();
1521 if (ret)
1522 return ret;
1523
8dbffa16
FW
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
e93ed2db 1528 return arch_timer_common_init();
b09ca1ec 1529}
ae281cbd 1530CLOCKSOURCE_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
b09ca1ec 1531#endif