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