blob: 6cc8aff83805883e097380ea0ffe357c7e94d422 [file] [log] [blame]
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001/*
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 */
Marc Zyngierf005bd72016-08-01 10:54:15 +010011
12#define pr_fmt(fmt) "arm_arch_timer: " fmt
13
Mark Rutland8a4da6e2012-11-12 14:33:44 +000014#include <linux/init.h>
15#include <linux/kernel.h>
16#include <linux/device.h>
17#include <linux/smp.h>
18#include <linux/cpu.h>
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +010019#include <linux/cpu_pm.h>
Mark Rutland8a4da6e2012-11-12 14:33:44 +000020#include <linux/clockchips.h>
Richard Cochran7c8f1e72015-01-06 14:26:13 +010021#include <linux/clocksource.h>
Mark Rutland8a4da6e2012-11-12 14:33:44 +000022#include <linux/interrupt.h>
23#include <linux/of_irq.h>
Stephen Boyd22006992013-07-18 16:59:32 -070024#include <linux/of_address.h>
Mark Rutland8a4da6e2012-11-12 14:33:44 +000025#include <linux/io.h>
Stephen Boyd22006992013-07-18 16:59:32 -070026#include <linux/slab.h>
Ingo Molnare6017572017-02-01 16:36:40 +010027#include <linux/sched/clock.h>
Stephen Boyd65cd4f62013-07-18 16:21:18 -070028#include <linux/sched_clock.h>
Hanjun Guob09ca1e2015-03-24 14:02:50 +000029#include <linux/acpi.h>
Mark Rutland8a4da6e2012-11-12 14:33:44 +000030
31#include <asm/arch_timer.h>
Marc Zyngier82668912013-01-10 11:13:07 +000032#include <asm/virt.h>
Mark Rutland8a4da6e2012-11-12 14:33:44 +000033
34#include <clocksource/arm_arch_timer.h>
35
Fu Weided24012017-01-18 21:25:25 +080036#undef pr_fmt
37#define pr_fmt(fmt) "arch_timer: " fmt
38
Stephen Boyd22006992013-07-18 16:59:32 -070039#define CNTTIDR 0x08
40#define CNTTIDR_VIRT(n) (BIT(1) << ((n) * 4))
41
Robin Murphye392d602016-02-01 12:00:48 +000042#define CNTACR(n) (0x40 + ((n) * 4))
43#define CNTACR_RPCT BIT(0)
44#define CNTACR_RVCT BIT(1)
45#define CNTACR_RFRQ BIT(2)
46#define CNTACR_RVOFF BIT(3)
47#define CNTACR_RWVT BIT(4)
48#define CNTACR_RWPT BIT(5)
49
Stephen Boyd22006992013-07-18 16:59:32 -070050#define CNTVCT_LO 0x08
51#define CNTVCT_HI 0x0c
52#define CNTFRQ 0x10
53#define CNTP_TVAL 0x28
54#define CNTP_CTL 0x2c
55#define CNTV_TVAL 0x38
56#define CNTV_CTL 0x3c
57
Stephen Boyd22006992013-07-18 16:59:32 -070058static unsigned arch_timers_present __initdata;
59
60static void __iomem *arch_counter_base;
61
62struct arch_timer {
63 void __iomem *base;
64 struct clock_event_device evt;
65};
66
67#define to_arch_timer(e) container_of(e, struct arch_timer, evt)
68
Mark Rutland8a4da6e2012-11-12 14:33:44 +000069static u32 arch_timer_rate;
Fu Weiee34f1e2017-01-18 21:25:27 +080070static int arch_timer_ppi[ARCH_TIMER_MAX_TIMER_PPI];
Mark Rutland8a4da6e2012-11-12 14:33:44 +000071
72static struct clock_event_device __percpu *arch_timer_evt;
73
Fu Weiee34f1e2017-01-18 21:25:27 +080074static enum arch_timer_ppi_nr arch_timer_uses_ppi = ARCH_TIMER_VIRT_PPI;
Lorenzo Pieralisi82a561942014-04-08 10:04:32 +010075static bool arch_timer_c3stop;
Stephen Boyd22006992013-07-18 16:59:32 -070076static bool arch_timer_mem_use_virtual;
Brian Norrisd8ec7592016-10-04 11:12:09 -070077static bool arch_counter_suspend_stop;
Marc Zyngiera86bd132017-02-01 12:07:15 +000078static bool vdso_default = true;
Mark Rutland8a4da6e2012-11-12 14:33:44 +000079
Julien Thierryec5c8e42017-10-13 14:32:55 +010080static cpumask_t evtstrm_available = CPU_MASK_NONE;
Will Deacon46fd5c62016-06-27 17:30:13 +010081static bool evtstrm_enable = IS_ENABLED(CONFIG_ARM_ARCH_TIMER_EVTSTREAM);
82
83static int __init early_evtstrm_cfg(char *buf)
84{
85 return strtobool(buf, &evtstrm_enable);
86}
87early_param("clocksource.arm_arch_timer.evtstrm", early_evtstrm_cfg);
88
Mark Rutland8a4da6e2012-11-12 14:33:44 +000089/*
90 * Architected system timer support.
91 */
92
Marc Zyngierf4e00a12017-01-20 18:28:32 +000093static __always_inline
94void arch_timer_reg_write(int access, enum arch_timer_reg reg, u32 val,
95 struct clock_event_device *clk)
96{
97 if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
98 struct arch_timer *timer = to_arch_timer(clk);
99 switch (reg) {
100 case ARCH_TIMER_REG_CTRL:
101 writel_relaxed(val, timer->base + CNTP_CTL);
102 break;
103 case ARCH_TIMER_REG_TVAL:
104 writel_relaxed(val, timer->base + CNTP_TVAL);
105 break;
106 }
107 } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
108 struct arch_timer *timer = to_arch_timer(clk);
109 switch (reg) {
110 case ARCH_TIMER_REG_CTRL:
111 writel_relaxed(val, timer->base + CNTV_CTL);
112 break;
113 case ARCH_TIMER_REG_TVAL:
114 writel_relaxed(val, timer->base + CNTV_TVAL);
115 break;
116 }
117 } else {
118 arch_timer_reg_write_cp15(access, reg, val);
119 }
120}
121
122static __always_inline
123u32 arch_timer_reg_read(int access, enum arch_timer_reg reg,
124 struct clock_event_device *clk)
125{
126 u32 val;
127
128 if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
129 struct arch_timer *timer = to_arch_timer(clk);
130 switch (reg) {
131 case ARCH_TIMER_REG_CTRL:
132 val = readl_relaxed(timer->base + CNTP_CTL);
133 break;
134 case ARCH_TIMER_REG_TVAL:
135 val = readl_relaxed(timer->base + CNTP_TVAL);
136 break;
137 }
138 } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
139 struct arch_timer *timer = to_arch_timer(clk);
140 switch (reg) {
141 case ARCH_TIMER_REG_CTRL:
142 val = readl_relaxed(timer->base + CNTV_CTL);
143 break;
144 case ARCH_TIMER_REG_TVAL:
145 val = readl_relaxed(timer->base + CNTV_TVAL);
146 break;
147 }
148 } else {
149 val = arch_timer_reg_read_cp15(access, reg);
150 }
151
152 return val;
153}
154
Marc Zyngier992dd162017-02-01 11:53:46 +0000155/*
156 * Default to cp15 based access because arm64 uses this function for
157 * sched_clock() before DT is probed and the cp15 method is guaranteed
158 * to exist on arm64. arm doesn't use this before DT is probed so even
159 * if we don't have the cp15 accessors we won't have a problem.
160 */
161u64 (*arch_timer_read_counter)(void) = arch_counter_get_cntvct;
Christoffer Dalle6d68b002017-07-05 11:04:28 +0200162EXPORT_SYMBOL_GPL(arch_timer_read_counter);
Marc Zyngier992dd162017-02-01 11:53:46 +0000163
164static u64 arch_counter_read(struct clocksource *cs)
165{
166 return arch_timer_read_counter();
167}
168
169static u64 arch_counter_read_cc(const struct cyclecounter *cc)
170{
171 return arch_timer_read_counter();
172}
173
174static struct clocksource clocksource_counter = {
175 .name = "arch_sys_counter",
176 .rating = 400,
177 .read = arch_counter_read,
178 .mask = CLOCKSOURCE_MASK(56),
179 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
180};
181
182static struct cyclecounter cyclecounter __ro_after_init = {
183 .read = arch_counter_read_cc,
184 .mask = CLOCKSOURCE_MASK(56),
185};
186
Marc Zyngier5a38bca2017-02-21 14:37:30 +0000187struct ate_acpi_oem_info {
188 char oem_id[ACPI_OEM_ID_SIZE + 1];
189 char oem_table_id[ACPI_OEM_TABLE_ID_SIZE + 1];
190 u32 oem_revision;
191};
192
Scott Woodf6dc1572016-09-22 03:35:17 -0500193#ifdef CONFIG_FSL_ERRATUM_A008585
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000194/*
195 * The number of retries is an arbitrary value well beyond the highest number
196 * of iterations the loop has been observed to take.
197 */
198#define __fsl_a008585_read_reg(reg) ({ \
199 u64 _old, _new; \
200 int _retries = 200; \
201 \
202 do { \
203 _old = read_sysreg(reg); \
204 _new = read_sysreg(reg); \
205 _retries--; \
206 } while (unlikely(_old != _new) && _retries); \
207 \
208 WARN_ON_ONCE(!_retries); \
209 _new; \
210})
Scott Woodf6dc1572016-09-22 03:35:17 -0500211
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000212static u32 notrace fsl_a008585_read_cntp_tval_el0(void)
Scott Woodf6dc1572016-09-22 03:35:17 -0500213{
214 return __fsl_a008585_read_reg(cntp_tval_el0);
215}
216
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000217static u32 notrace fsl_a008585_read_cntv_tval_el0(void)
Scott Woodf6dc1572016-09-22 03:35:17 -0500218{
219 return __fsl_a008585_read_reg(cntv_tval_el0);
220}
221
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200222static u64 notrace fsl_a008585_read_cntpct_el0(void)
223{
224 return __fsl_a008585_read_reg(cntpct_el0);
225}
226
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000227static u64 notrace fsl_a008585_read_cntvct_el0(void)
Scott Woodf6dc1572016-09-22 03:35:17 -0500228{
229 return __fsl_a008585_read_reg(cntvct_el0);
230}
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000231#endif
232
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000233#ifdef CONFIG_HISILICON_ERRATUM_161010101
234/*
235 * Verify whether the value of the second read is larger than the first by
236 * less than 32 is the only way to confirm the value is correct, so clear the
237 * lower 5 bits to check whether the difference is greater than 32 or not.
238 * Theoretically the erratum should not occur more than twice in succession
239 * when reading the system counter, but it is possible that some interrupts
240 * may lead to more than twice read errors, triggering the warning, so setting
241 * the number of retries far beyond the number of iterations the loop has been
242 * observed to take.
243 */
244#define __hisi_161010101_read_reg(reg) ({ \
245 u64 _old, _new; \
246 int _retries = 50; \
247 \
248 do { \
249 _old = read_sysreg(reg); \
250 _new = read_sysreg(reg); \
251 _retries--; \
252 } while (unlikely((_new - _old) >> 5) && _retries); \
253 \
254 WARN_ON_ONCE(!_retries); \
255 _new; \
256})
257
258static u32 notrace hisi_161010101_read_cntp_tval_el0(void)
259{
260 return __hisi_161010101_read_reg(cntp_tval_el0);
261}
262
263static u32 notrace hisi_161010101_read_cntv_tval_el0(void)
264{
265 return __hisi_161010101_read_reg(cntv_tval_el0);
266}
267
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200268static u64 notrace hisi_161010101_read_cntpct_el0(void)
269{
270 return __hisi_161010101_read_reg(cntpct_el0);
271}
272
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000273static u64 notrace hisi_161010101_read_cntvct_el0(void)
274{
275 return __hisi_161010101_read_reg(cntvct_el0);
276}
Marc Zyngierd003d022017-02-21 15:04:27 +0000277
278static struct ate_acpi_oem_info hisi_161010101_oem_info[] = {
279 /*
280 * Note that trailing spaces are required to properly match
281 * the OEM table information.
282 */
283 {
284 .oem_id = "HISI ",
285 .oem_table_id = "HIP05 ",
286 .oem_revision = 0,
287 },
288 {
289 .oem_id = "HISI ",
290 .oem_table_id = "HIP06 ",
291 .oem_revision = 0,
292 },
293 {
294 .oem_id = "HISI ",
295 .oem_table_id = "HIP07 ",
296 .oem_revision = 0,
297 },
298 { /* Sentinel indicating the end of the OEM array */ },
299};
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000300#endif
301
Marc Zyngierfa8d8152017-01-27 12:52:31 +0000302#ifdef CONFIG_ARM64_ERRATUM_858921
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200303static u64 notrace arm64_858921_read_cntpct_el0(void)
304{
305 u64 old, new;
306
307 old = read_sysreg(cntpct_el0);
308 new = read_sysreg(cntpct_el0);
309 return (((old ^ new) >> 32) & 1) ? old : new;
310}
311
Marc Zyngierfa8d8152017-01-27 12:52:31 +0000312static u64 notrace arm64_858921_read_cntvct_el0(void)
313{
314 u64 old, new;
315
316 old = read_sysreg(cntvct_el0);
317 new = read_sysreg(cntvct_el0);
318 return (((old ^ new) >> 32) & 1) ? old : new;
319}
320#endif
321
Marc Zyngier95b861a42018-09-27 17:15:34 +0100322#ifdef CONFIG_ARM64_ERRATUM_1188873
323static u64 notrace arm64_1188873_read_cntvct_el0(void)
324{
325 return read_sysreg(cntvct_el0);
326}
327#endif
328
Samuel Hollandc950ca82019-01-12 20:17:18 -0600329#ifdef CONFIG_SUN50I_ERRATUM_UNKNOWN1
330/*
331 * The low bits of the counter registers are indeterminate while bit 10 or
332 * greater is rolling over. Since the counter value can jump both backward
333 * (7ff -> 000 -> 800) and forward (7ff -> fff -> 800), ignore register values
334 * with all ones or all zeros in the low bits. Bound the loop by the maximum
335 * number of CPU cycles in 3 consecutive 24 MHz counter periods.
336 */
337#define __sun50i_a64_read_reg(reg) ({ \
338 u64 _val; \
339 int _retries = 150; \
340 \
341 do { \
342 _val = read_sysreg(reg); \
343 _retries--; \
344 } while (((_val + 1) & GENMASK(9, 0)) <= 1 && _retries); \
345 \
346 WARN_ON_ONCE(!_retries); \
347 _val; \
348})
349
350static u64 notrace sun50i_a64_read_cntpct_el0(void)
351{
352 return __sun50i_a64_read_reg(cntpct_el0);
353}
354
355static u64 notrace sun50i_a64_read_cntvct_el0(void)
356{
357 return __sun50i_a64_read_reg(cntvct_el0);
358}
359
360static u32 notrace sun50i_a64_read_cntp_tval_el0(void)
361{
362 return read_sysreg(cntp_cval_el0) - sun50i_a64_read_cntpct_el0();
363}
364
365static u32 notrace sun50i_a64_read_cntv_tval_el0(void)
366{
367 return read_sysreg(cntv_cval_el0) - sun50i_a64_read_cntvct_el0();
368}
369#endif
370
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000371#ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
Mark Rutlanda7fb4572017-10-16 16:28:39 +0100372DEFINE_PER_CPU(const struct arch_timer_erratum_workaround *, timer_unstable_counter_workaround);
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000373EXPORT_SYMBOL_GPL(timer_unstable_counter_workaround);
374
375DEFINE_STATIC_KEY_FALSE(arch_timer_read_ool_enabled);
376EXPORT_SYMBOL_GPL(arch_timer_read_ool_enabled);
377
Marc Zyngier83280892017-01-27 10:27:09 +0000378static void erratum_set_next_event_tval_generic(const int access, unsigned long evt,
379 struct clock_event_device *clk)
380{
381 unsigned long ctrl;
Christoffer Dalle6d68b002017-07-05 11:04:28 +0200382 u64 cval;
Marc Zyngier83280892017-01-27 10:27:09 +0000383
384 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
385 ctrl |= ARCH_TIMER_CTRL_ENABLE;
386 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
387
Christoffer Dalle6d68b002017-07-05 11:04:28 +0200388 if (access == ARCH_TIMER_PHYS_ACCESS) {
389 cval = evt + arch_counter_get_cntpct();
Marc Zyngier83280892017-01-27 10:27:09 +0000390 write_sysreg(cval, cntp_cval_el0);
Christoffer Dalle6d68b002017-07-05 11:04:28 +0200391 } else {
392 cval = evt + arch_counter_get_cntvct();
Marc Zyngier83280892017-01-27 10:27:09 +0000393 write_sysreg(cval, cntv_cval_el0);
Christoffer Dalle6d68b002017-07-05 11:04:28 +0200394 }
Marc Zyngier83280892017-01-27 10:27:09 +0000395
396 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
397}
398
Arnd Bergmanneb645222017-04-19 19:37:09 +0200399static __maybe_unused int erratum_set_next_event_tval_virt(unsigned long evt,
Marc Zyngier83280892017-01-27 10:27:09 +0000400 struct clock_event_device *clk)
401{
402 erratum_set_next_event_tval_generic(ARCH_TIMER_VIRT_ACCESS, evt, clk);
403 return 0;
404}
405
Arnd Bergmanneb645222017-04-19 19:37:09 +0200406static __maybe_unused int erratum_set_next_event_tval_phys(unsigned long evt,
Marc Zyngier83280892017-01-27 10:27:09 +0000407 struct clock_event_device *clk)
408{
409 erratum_set_next_event_tval_generic(ARCH_TIMER_PHYS_ACCESS, evt, clk);
410 return 0;
411}
412
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000413static const struct arch_timer_erratum_workaround ool_workarounds[] = {
414#ifdef CONFIG_FSL_ERRATUM_A008585
415 {
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000416 .match_type = ate_match_dt,
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000417 .id = "fsl,erratum-a008585",
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000418 .desc = "Freescale erratum a005858",
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000419 .read_cntp_tval_el0 = fsl_a008585_read_cntp_tval_el0,
420 .read_cntv_tval_el0 = fsl_a008585_read_cntv_tval_el0,
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200421 .read_cntpct_el0 = fsl_a008585_read_cntpct_el0,
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000422 .read_cntvct_el0 = fsl_a008585_read_cntvct_el0,
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000423 .set_next_event_phys = erratum_set_next_event_tval_phys,
424 .set_next_event_virt = erratum_set_next_event_tval_virt,
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000425 },
426#endif
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000427#ifdef CONFIG_HISILICON_ERRATUM_161010101
428 {
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000429 .match_type = ate_match_dt,
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000430 .id = "hisilicon,erratum-161010101",
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000431 .desc = "HiSilicon erratum 161010101",
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000432 .read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
433 .read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200434 .read_cntpct_el0 = hisi_161010101_read_cntpct_el0,
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000435 .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000436 .set_next_event_phys = erratum_set_next_event_tval_phys,
437 .set_next_event_virt = erratum_set_next_event_tval_virt,
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000438 },
Marc Zyngierd003d022017-02-21 15:04:27 +0000439 {
440 .match_type = ate_match_acpi_oem_info,
441 .id = hisi_161010101_oem_info,
442 .desc = "HiSilicon erratum 161010101",
443 .read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
444 .read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200445 .read_cntpct_el0 = hisi_161010101_read_cntpct_el0,
Marc Zyngierd003d022017-02-21 15:04:27 +0000446 .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
447 .set_next_event_phys = erratum_set_next_event_tval_phys,
448 .set_next_event_virt = erratum_set_next_event_tval_virt,
449 },
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000450#endif
Marc Zyngierfa8d8152017-01-27 12:52:31 +0000451#ifdef CONFIG_ARM64_ERRATUM_858921
452 {
453 .match_type = ate_match_local_cap_id,
454 .id = (void *)ARM64_WORKAROUND_858921,
455 .desc = "ARM erratum 858921",
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200456 .read_cntpct_el0 = arm64_858921_read_cntpct_el0,
Marc Zyngierfa8d8152017-01-27 12:52:31 +0000457 .read_cntvct_el0 = arm64_858921_read_cntvct_el0,
458 },
459#endif
Marc Zyngier95b861a42018-09-27 17:15:34 +0100460#ifdef CONFIG_ARM64_ERRATUM_1188873
461 {
462 .match_type = ate_match_local_cap_id,
463 .id = (void *)ARM64_WORKAROUND_1188873,
464 .desc = "ARM erratum 1188873",
465 .read_cntvct_el0 = arm64_1188873_read_cntvct_el0,
466 },
467#endif
Samuel Hollandc950ca82019-01-12 20:17:18 -0600468#ifdef CONFIG_SUN50I_ERRATUM_UNKNOWN1
469 {
470 .match_type = ate_match_dt,
471 .id = "allwinner,erratum-unknown1",
472 .desc = "Allwinner erratum UNKNOWN1",
473 .read_cntp_tval_el0 = sun50i_a64_read_cntp_tval_el0,
474 .read_cntv_tval_el0 = sun50i_a64_read_cntv_tval_el0,
475 .read_cntpct_el0 = sun50i_a64_read_cntpct_el0,
476 .read_cntvct_el0 = sun50i_a64_read_cntvct_el0,
477 .set_next_event_phys = erratum_set_next_event_tval_phys,
478 .set_next_event_virt = erratum_set_next_event_tval_virt,
479 },
480#endif
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000481};
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000482
483typedef bool (*ate_match_fn_t)(const struct arch_timer_erratum_workaround *,
484 const void *);
485
486static
487bool arch_timer_check_dt_erratum(const struct arch_timer_erratum_workaround *wa,
488 const void *arg)
489{
490 const struct device_node *np = arg;
491
492 return of_property_read_bool(np, wa->id);
493}
494
Marc Zyngier00640302017-03-20 16:47:59 +0000495static
496bool arch_timer_check_local_cap_erratum(const struct arch_timer_erratum_workaround *wa,
497 const void *arg)
498{
499 return this_cpu_has_cap((uintptr_t)wa->id);
500}
501
Marc Zyngier5a38bca2017-02-21 14:37:30 +0000502
503static
504bool arch_timer_check_acpi_oem_erratum(const struct arch_timer_erratum_workaround *wa,
505 const void *arg)
506{
507 static const struct ate_acpi_oem_info empty_oem_info = {};
508 const struct ate_acpi_oem_info *info = wa->id;
509 const struct acpi_table_header *table = arg;
510
511 /* Iterate over the ACPI OEM info array, looking for a match */
512 while (memcmp(info, &empty_oem_info, sizeof(*info))) {
513 if (!memcmp(info->oem_id, table->oem_id, ACPI_OEM_ID_SIZE) &&
514 !memcmp(info->oem_table_id, table->oem_table_id, ACPI_OEM_TABLE_ID_SIZE) &&
515 info->oem_revision == table->oem_revision)
516 return true;
517
518 info++;
519 }
520
521 return false;
522}
523
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000524static const struct arch_timer_erratum_workaround *
525arch_timer_iterate_errata(enum arch_timer_erratum_match_type type,
526 ate_match_fn_t match_fn,
527 void *arg)
528{
529 int i;
530
531 for (i = 0; i < ARRAY_SIZE(ool_workarounds); i++) {
532 if (ool_workarounds[i].match_type != type)
533 continue;
534
535 if (match_fn(&ool_workarounds[i], arg))
536 return &ool_workarounds[i];
537 }
538
539 return NULL;
540}
541
542static
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000543void arch_timer_enable_workaround(const struct arch_timer_erratum_workaround *wa,
544 bool local)
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000545{
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000546 int i;
547
548 if (local) {
549 __this_cpu_write(timer_unstable_counter_workaround, wa);
550 } else {
551 for_each_possible_cpu(i)
552 per_cpu(timer_unstable_counter_workaround, i) = wa;
553 }
554
Marc Zyngier450f9682017-08-01 09:02:57 +0100555 /*
556 * Use the locked version, as we're called from the CPU
557 * hotplug framework. Otherwise, we end-up in deadlock-land.
558 */
559 static_branch_enable_cpuslocked(&arch_timer_read_ool_enabled);
Marc Zyngiera86bd132017-02-01 12:07:15 +0000560
561 /*
562 * Don't use the vdso fastpath if errata require using the
563 * out-of-line counter accessor. We may change our mind pretty
564 * late in the game (with a per-CPU erratum, for example), so
565 * change both the default value and the vdso itself.
566 */
567 if (wa->read_cntvct_el0) {
568 clocksource_counter.archdata.vdso_direct = false;
569 vdso_default = false;
570 }
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000571}
572
573static void arch_timer_check_ool_workaround(enum arch_timer_erratum_match_type type,
574 void *arg)
575{
576 const struct arch_timer_erratum_workaround *wa;
577 ate_match_fn_t match_fn = NULL;
Marc Zyngier00640302017-03-20 16:47:59 +0000578 bool local = false;
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000579
580 switch (type) {
581 case ate_match_dt:
582 match_fn = arch_timer_check_dt_erratum;
583 break;
Marc Zyngier00640302017-03-20 16:47:59 +0000584 case ate_match_local_cap_id:
585 match_fn = arch_timer_check_local_cap_erratum;
586 local = true;
587 break;
Marc Zyngier5a38bca2017-02-21 14:37:30 +0000588 case ate_match_acpi_oem_info:
589 match_fn = arch_timer_check_acpi_oem_erratum;
590 break;
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000591 default:
592 WARN_ON(1);
593 return;
594 }
595
596 wa = arch_timer_iterate_errata(type, match_fn, arg);
597 if (!wa)
598 return;
599
Marc Zyngier00640302017-03-20 16:47:59 +0000600 if (needs_unstable_timer_counter_workaround()) {
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000601 const struct arch_timer_erratum_workaround *__wa;
602 __wa = __this_cpu_read(timer_unstable_counter_workaround);
603 if (__wa && wa != __wa)
Marc Zyngier00640302017-03-20 16:47:59 +0000604 pr_warn("Can't enable workaround for %s (clashes with %s\n)",
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000605 wa->desc, __wa->desc);
606
607 if (__wa)
608 return;
Marc Zyngier00640302017-03-20 16:47:59 +0000609 }
610
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000611 arch_timer_enable_workaround(wa, local);
Marc Zyngier00640302017-03-20 16:47:59 +0000612 pr_info("Enabling %s workaround for %s\n",
613 local ? "local" : "global", wa->desc);
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000614}
615
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000616#define erratum_handler(fn, r, ...) \
617({ \
618 bool __val; \
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000619 if (needs_unstable_timer_counter_workaround()) { \
620 const struct arch_timer_erratum_workaround *__wa; \
621 __wa = __this_cpu_read(timer_unstable_counter_workaround); \
622 if (__wa && __wa->fn) { \
623 r = __wa->fn(__VA_ARGS__); \
624 __val = true; \
625 } else { \
626 __val = false; \
627 } \
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000628 } else { \
629 __val = false; \
630 } \
631 __val; \
632})
633
Marc Zyngiera86bd132017-02-01 12:07:15 +0000634static bool arch_timer_this_cpu_has_cntvct_wa(void)
635{
636 const struct arch_timer_erratum_workaround *wa;
637
638 wa = __this_cpu_read(timer_unstable_counter_workaround);
639 return wa && wa->read_cntvct_el0;
640}
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000641#else
642#define arch_timer_check_ool_workaround(t,a) do { } while(0)
Marc Zyngier83280892017-01-27 10:27:09 +0000643#define erratum_set_next_event_tval_virt(...) ({BUG(); 0;})
644#define erratum_set_next_event_tval_phys(...) ({BUG(); 0;})
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000645#define erratum_handler(fn, r, ...) ({false;})
Marc Zyngiera86bd132017-02-01 12:07:15 +0000646#define arch_timer_this_cpu_has_cntvct_wa() ({false;})
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000647#endif /* CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND */
Scott Woodf6dc1572016-09-22 03:35:17 -0500648
Stephen Boyde09f3cc2013-07-18 16:59:28 -0700649static __always_inline irqreturn_t timer_handler(const int access,
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000650 struct clock_event_device *evt)
651{
652 unsigned long ctrl;
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200653
Stephen Boyd60faddf2013-07-18 16:59:31 -0700654 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000655 if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
656 ctrl |= ARCH_TIMER_CTRL_IT_MASK;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700657 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000658 evt->event_handler(evt);
659 return IRQ_HANDLED;
660 }
661
662 return IRQ_NONE;
663}
664
665static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
666{
667 struct clock_event_device *evt = dev_id;
668
669 return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
670}
671
672static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
673{
674 struct clock_event_device *evt = dev_id;
675
676 return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
677}
678
Stephen Boyd22006992013-07-18 16:59:32 -0700679static irqreturn_t arch_timer_handler_phys_mem(int irq, void *dev_id)
680{
681 struct clock_event_device *evt = dev_id;
682
683 return timer_handler(ARCH_TIMER_MEM_PHYS_ACCESS, evt);
684}
685
686static irqreturn_t arch_timer_handler_virt_mem(int irq, void *dev_id)
687{
688 struct clock_event_device *evt = dev_id;
689
690 return timer_handler(ARCH_TIMER_MEM_VIRT_ACCESS, evt);
691}
692
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530693static __always_inline int timer_shutdown(const int access,
694 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000695{
696 unsigned long ctrl;
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530697
698 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
699 ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
700 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
701
702 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000703}
704
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530705static int arch_timer_shutdown_virt(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000706{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530707 return timer_shutdown(ARCH_TIMER_VIRT_ACCESS, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000708}
709
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530710static int arch_timer_shutdown_phys(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000711{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530712 return timer_shutdown(ARCH_TIMER_PHYS_ACCESS, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000713}
714
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530715static int arch_timer_shutdown_virt_mem(struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700716{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530717 return timer_shutdown(ARCH_TIMER_MEM_VIRT_ACCESS, clk);
Stephen Boyd22006992013-07-18 16:59:32 -0700718}
719
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530720static int arch_timer_shutdown_phys_mem(struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700721{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530722 return timer_shutdown(ARCH_TIMER_MEM_PHYS_ACCESS, clk);
Stephen Boyd22006992013-07-18 16:59:32 -0700723}
724
Stephen Boyd60faddf2013-07-18 16:59:31 -0700725static __always_inline void set_next_event(const int access, unsigned long evt,
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200726 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000727{
728 unsigned long ctrl;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700729 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000730 ctrl |= ARCH_TIMER_CTRL_ENABLE;
731 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700732 arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt, clk);
733 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000734}
735
736static int arch_timer_set_next_event_virt(unsigned long evt,
Stephen Boyd60faddf2013-07-18 16:59:31 -0700737 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000738{
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000739 int ret;
740
741 if (erratum_handler(set_next_event_virt, ret, evt, clk))
742 return ret;
Marc Zyngier83280892017-01-27 10:27:09 +0000743
Stephen Boyd60faddf2013-07-18 16:59:31 -0700744 set_next_event(ARCH_TIMER_VIRT_ACCESS, evt, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000745 return 0;
746}
747
748static int arch_timer_set_next_event_phys(unsigned long evt,
Stephen Boyd60faddf2013-07-18 16:59:31 -0700749 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000750{
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000751 int ret;
752
753 if (erratum_handler(set_next_event_phys, ret, evt, clk))
754 return ret;
Marc Zyngier83280892017-01-27 10:27:09 +0000755
Stephen Boyd60faddf2013-07-18 16:59:31 -0700756 set_next_event(ARCH_TIMER_PHYS_ACCESS, evt, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000757 return 0;
758}
759
Stephen Boyd22006992013-07-18 16:59:32 -0700760static int arch_timer_set_next_event_virt_mem(unsigned long evt,
761 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000762{
Stephen Boyd22006992013-07-18 16:59:32 -0700763 set_next_event(ARCH_TIMER_MEM_VIRT_ACCESS, evt, clk);
764 return 0;
765}
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000766
Stephen Boyd22006992013-07-18 16:59:32 -0700767static int arch_timer_set_next_event_phys_mem(unsigned long evt,
768 struct clock_event_device *clk)
769{
770 set_next_event(ARCH_TIMER_MEM_PHYS_ACCESS, evt, clk);
771 return 0;
772}
773
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200774static void __arch_timer_setup(unsigned type,
775 struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700776{
777 clk->features = CLOCK_EVT_FEAT_ONESHOT;
778
Fu Wei8a5c21d2017-01-18 21:25:26 +0800779 if (type == ARCH_TIMER_TYPE_CP15) {
Lorenzo Pieralisi82a561942014-04-08 10:04:32 +0100780 if (arch_timer_c3stop)
781 clk->features |= CLOCK_EVT_FEAT_C3STOP;
Stephen Boyd22006992013-07-18 16:59:32 -0700782 clk->name = "arch_sys_timer";
783 clk->rating = 450;
784 clk->cpumask = cpumask_of(smp_processor_id());
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000785 clk->irq = arch_timer_ppi[arch_timer_uses_ppi];
786 switch (arch_timer_uses_ppi) {
Fu Weiee34f1e2017-01-18 21:25:27 +0800787 case ARCH_TIMER_VIRT_PPI:
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530788 clk->set_state_shutdown = arch_timer_shutdown_virt;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530789 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt;
Stephen Boyd22006992013-07-18 16:59:32 -0700790 clk->set_next_event = arch_timer_set_next_event_virt;
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000791 break;
Fu Weiee34f1e2017-01-18 21:25:27 +0800792 case ARCH_TIMER_PHYS_SECURE_PPI:
793 case ARCH_TIMER_PHYS_NONSECURE_PPI:
794 case ARCH_TIMER_HYP_PPI:
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530795 clk->set_state_shutdown = arch_timer_shutdown_phys;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530796 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys;
Stephen Boyd22006992013-07-18 16:59:32 -0700797 clk->set_next_event = arch_timer_set_next_event_phys;
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000798 break;
799 default:
800 BUG();
Stephen Boyd22006992013-07-18 16:59:32 -0700801 }
Scott Woodf6dc1572016-09-22 03:35:17 -0500802
Marc Zyngier00640302017-03-20 16:47:59 +0000803 arch_timer_check_ool_workaround(ate_match_local_cap_id, NULL);
Stephen Boyd22006992013-07-18 16:59:32 -0700804 } else {
Stephen Boyd7b52ad22014-01-06 14:56:17 -0800805 clk->features |= CLOCK_EVT_FEAT_DYNIRQ;
Stephen Boyd22006992013-07-18 16:59:32 -0700806 clk->name = "arch_mem_timer";
807 clk->rating = 400;
Sudeep Holla5e18e412018-07-09 16:45:36 +0100808 clk->cpumask = cpu_possible_mask;
Stephen Boyd22006992013-07-18 16:59:32 -0700809 if (arch_timer_mem_use_virtual) {
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530810 clk->set_state_shutdown = arch_timer_shutdown_virt_mem;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530811 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt_mem;
Stephen Boyd22006992013-07-18 16:59:32 -0700812 clk->set_next_event =
813 arch_timer_set_next_event_virt_mem;
814 } else {
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530815 clk->set_state_shutdown = arch_timer_shutdown_phys_mem;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530816 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys_mem;
Stephen Boyd22006992013-07-18 16:59:32 -0700817 clk->set_next_event =
818 arch_timer_set_next_event_phys_mem;
819 }
820 }
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000821
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530822 clk->set_state_shutdown(clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000823
Stephen Boyd22006992013-07-18 16:59:32 -0700824 clockevents_config_and_register(clk, arch_timer_rate, 0xf, 0x7fffffff);
825}
826
Nathan Lynche1ce5c72014-09-29 01:50:06 +0200827static void arch_timer_evtstrm_enable(int divider)
828{
829 u32 cntkctl = arch_timer_get_cntkctl();
830
831 cntkctl &= ~ARCH_TIMER_EVT_TRIGGER_MASK;
832 /* Set the divider and enable virtual event stream */
833 cntkctl |= (divider << ARCH_TIMER_EVT_TRIGGER_SHIFT)
834 | ARCH_TIMER_VIRT_EVT_EN;
835 arch_timer_set_cntkctl(cntkctl);
Andrew Murrayaaba0982019-04-09 10:52:40 +0100836#ifdef CONFIG_ARM64
837 cpu_set_named_feature(EVTSTRM);
838#else
Nathan Lynche1ce5c72014-09-29 01:50:06 +0200839 elf_hwcap |= HWCAP_EVTSTRM;
Andrew Murrayaaba0982019-04-09 10:52:40 +0100840#endif
Nathan Lynche1ce5c72014-09-29 01:50:06 +0200841#ifdef CONFIG_COMPAT
842 compat_elf_hwcap |= COMPAT_HWCAP_EVTSTRM;
843#endif
Julien Thierryec5c8e42017-10-13 14:32:55 +0100844 cpumask_set_cpu(smp_processor_id(), &evtstrm_available);
Nathan Lynche1ce5c72014-09-29 01:50:06 +0200845}
846
Will Deacon037f6372013-08-23 15:32:29 +0100847static void arch_timer_configure_evtstream(void)
848{
849 int evt_stream_div, pos;
850
851 /* Find the closest power of two to the divisor */
852 evt_stream_div = arch_timer_rate / ARCH_TIMER_EVT_STREAM_FREQ;
853 pos = fls(evt_stream_div);
854 if (pos > 1 && !(evt_stream_div & (1 << (pos - 2))))
855 pos--;
856 /* enable event stream */
857 arch_timer_evtstrm_enable(min(pos, 15));
858}
859
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200860static void arch_counter_set_user_access(void)
861{
862 u32 cntkctl = arch_timer_get_cntkctl();
863
Marc Zyngiera86bd132017-02-01 12:07:15 +0000864 /* Disable user access to the timers and both counters */
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200865 /* Also disable virtual event stream */
866 cntkctl &= ~(ARCH_TIMER_USR_PT_ACCESS_EN
867 | ARCH_TIMER_USR_VT_ACCESS_EN
Marc Zyngiera86bd132017-02-01 12:07:15 +0000868 | ARCH_TIMER_USR_VCT_ACCESS_EN
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200869 | ARCH_TIMER_VIRT_EVT_EN
870 | ARCH_TIMER_USR_PCT_ACCESS_EN);
871
Marc Zyngiera86bd132017-02-01 12:07:15 +0000872 /*
873 * Enable user access to the virtual counter if it doesn't
874 * need to be workaround. The vdso may have been already
875 * disabled though.
876 */
877 if (arch_timer_this_cpu_has_cntvct_wa())
878 pr_info("CPU%d: Trapping CNTVCT access\n", smp_processor_id());
879 else
880 cntkctl |= ARCH_TIMER_USR_VCT_ACCESS_EN;
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200881
882 arch_timer_set_cntkctl(cntkctl);
883}
884
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000885static bool arch_timer_has_nonsecure_ppi(void)
886{
Fu Weiee34f1e2017-01-18 21:25:27 +0800887 return (arch_timer_uses_ppi == ARCH_TIMER_PHYS_SECURE_PPI &&
888 arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000889}
890
Marc Zyngierf005bd72016-08-01 10:54:15 +0100891static u32 check_ppi_trigger(int irq)
892{
893 u32 flags = irq_get_trigger_type(irq);
894
895 if (flags != IRQF_TRIGGER_HIGH && flags != IRQF_TRIGGER_LOW) {
896 pr_warn("WARNING: Invalid trigger for IRQ%d, assuming level low\n", irq);
897 pr_warn("WARNING: Please fix your firmware\n");
898 flags = IRQF_TRIGGER_LOW;
899 }
900
901 return flags;
902}
903
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000904static int arch_timer_starting_cpu(unsigned int cpu)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000905{
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000906 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
Marc Zyngierf005bd72016-08-01 10:54:15 +0100907 u32 flags;
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000908
Fu Wei8a5c21d2017-01-18 21:25:26 +0800909 __arch_timer_setup(ARCH_TIMER_TYPE_CP15, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000910
Marc Zyngierf005bd72016-08-01 10:54:15 +0100911 flags = check_ppi_trigger(arch_timer_ppi[arch_timer_uses_ppi]);
912 enable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], flags);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000913
Marc Zyngierf005bd72016-08-01 10:54:15 +0100914 if (arch_timer_has_nonsecure_ppi()) {
Fu Weiee34f1e2017-01-18 21:25:27 +0800915 flags = check_ppi_trigger(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
916 enable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
917 flags);
Marc Zyngierf005bd72016-08-01 10:54:15 +0100918 }
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000919
920 arch_counter_set_user_access();
Will Deacon46fd5c62016-06-27 17:30:13 +0100921 if (evtstrm_enable)
Will Deacon037f6372013-08-23 15:32:29 +0100922 arch_timer_configure_evtstream();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000923
924 return 0;
925}
926
Fu Wei5d3dfa92017-03-22 00:31:13 +0800927/*
928 * For historical reasons, when probing with DT we use whichever (non-zero)
929 * rate was probed first, and don't verify that others match. If the first node
930 * probed has a clock-frequency property, this overrides the HW register.
931 */
932static void arch_timer_of_configure_rate(u32 rate, struct device_node *np)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000933{
Stephen Boyd22006992013-07-18 16:59:32 -0700934 /* Who has more than one independent system counter? */
935 if (arch_timer_rate)
936 return;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000937
Fu Wei5d3dfa92017-03-22 00:31:13 +0800938 if (of_property_read_u32(np, "clock-frequency", &arch_timer_rate))
939 arch_timer_rate = rate;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000940
Stephen Boyd22006992013-07-18 16:59:32 -0700941 /* Check the timer frequency. */
942 if (arch_timer_rate == 0)
Fu Weided24012017-01-18 21:25:25 +0800943 pr_warn("frequency not available\n");
Stephen Boyd22006992013-07-18 16:59:32 -0700944}
945
946static void arch_timer_banner(unsigned type)
947{
Fu Weided24012017-01-18 21:25:25 +0800948 pr_info("%s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
Fu Wei8a5c21d2017-01-18 21:25:26 +0800949 type & ARCH_TIMER_TYPE_CP15 ? "cp15" : "",
950 type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ?
951 " and " : "",
952 type & ARCH_TIMER_TYPE_MEM ? "mmio" : "",
Fu Weided24012017-01-18 21:25:25 +0800953 (unsigned long)arch_timer_rate / 1000000,
954 (unsigned long)(arch_timer_rate / 10000) % 100,
Fu Wei8a5c21d2017-01-18 21:25:26 +0800955 type & ARCH_TIMER_TYPE_CP15 ?
Fu Weiee34f1e2017-01-18 21:25:27 +0800956 (arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI) ? "virt" : "phys" :
Stephen Boyd22006992013-07-18 16:59:32 -0700957 "",
Fu Wei8a5c21d2017-01-18 21:25:26 +0800958 type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ? "/" : "",
959 type & ARCH_TIMER_TYPE_MEM ?
Stephen Boyd22006992013-07-18 16:59:32 -0700960 arch_timer_mem_use_virtual ? "virt" : "phys" :
961 "");
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000962}
963
964u32 arch_timer_get_rate(void)
965{
966 return arch_timer_rate;
967}
968
Julien Thierryec5c8e42017-10-13 14:32:55 +0100969bool arch_timer_evtstrm_available(void)
970{
971 /*
972 * We might get called from a preemptible context. This is fine
973 * because availability of the event stream should be always the same
974 * for a preemptible context and context where we might resume a task.
975 */
976 return cpumask_test_cpu(raw_smp_processor_id(), &evtstrm_available);
977}
978
Stephen Boyd22006992013-07-18 16:59:32 -0700979static u64 arch_counter_get_cntvct_mem(void)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000980{
Stephen Boyd22006992013-07-18 16:59:32 -0700981 u32 vct_lo, vct_hi, tmp_hi;
982
983 do {
984 vct_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
985 vct_lo = readl_relaxed(arch_counter_base + CNTVCT_LO);
986 tmp_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
987 } while (vct_hi != tmp_hi);
988
989 return ((u64) vct_hi << 32) | vct_lo;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000990}
991
Julien Grallb4d6ce92016-04-11 16:32:51 +0100992static struct arch_timer_kvm_info arch_timer_kvm_info;
993
994struct arch_timer_kvm_info *arch_timer_get_kvm_info(void)
995{
996 return &arch_timer_kvm_info;
997}
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000998
Stephen Boyd22006992013-07-18 16:59:32 -0700999static void __init arch_counter_register(unsigned type)
1000{
1001 u64 start_count;
1002
1003 /* Register the CP15 based counter if we have one */
Fu Wei8a5c21d2017-01-18 21:25:26 +08001004 if (type & ARCH_TIMER_TYPE_CP15) {
Christoffer Dalle6d68b002017-07-05 11:04:28 +02001005 if ((IS_ENABLED(CONFIG_ARM64) && !is_hyp_mode_available()) ||
Fu Weiee34f1e2017-01-18 21:25:27 +08001006 arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI)
Sonny Rao0b46b8a2014-11-23 23:02:44 -08001007 arch_timer_read_counter = arch_counter_get_cntvct;
1008 else
1009 arch_timer_read_counter = arch_counter_get_cntpct;
Scott Woodf6dc1572016-09-22 03:35:17 -05001010
Marc Zyngiera86bd132017-02-01 12:07:15 +00001011 clocksource_counter.archdata.vdso_direct = vdso_default;
Nathan Lynch423bd692014-09-29 01:50:06 +02001012 } else {
Stephen Boyd22006992013-07-18 16:59:32 -07001013 arch_timer_read_counter = arch_counter_get_cntvct_mem;
Nathan Lynch423bd692014-09-29 01:50:06 +02001014 }
1015
Brian Norrisd8ec7592016-10-04 11:12:09 -07001016 if (!arch_counter_suspend_stop)
1017 clocksource_counter.flags |= CLOCK_SOURCE_SUSPEND_NONSTOP;
Stephen Boyd22006992013-07-18 16:59:32 -07001018 start_count = arch_timer_read_counter();
1019 clocksource_register_hz(&clocksource_counter, arch_timer_rate);
1020 cyclecounter.mult = clocksource_counter.mult;
1021 cyclecounter.shift = clocksource_counter.shift;
Julien Grallb4d6ce92016-04-11 16:32:51 +01001022 timecounter_init(&arch_timer_kvm_info.timecounter,
1023 &cyclecounter, start_count);
Thierry Reding4a7d3e82013-10-15 15:31:51 +02001024
1025 /* 56 bits minimum, so we assume worst case rollover */
1026 sched_clock_register(arch_timer_read_counter, 56, arch_timer_rate);
Stephen Boyd22006992013-07-18 16:59:32 -07001027}
1028
Paul Gortmaker8c37bb32013-06-19 11:32:08 -04001029static void arch_timer_stop(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001030{
Fu Weided24012017-01-18 21:25:25 +08001031 pr_debug("disable IRQ%d cpu #%d\n", clk->irq, smp_processor_id());
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001032
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001033 disable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi]);
1034 if (arch_timer_has_nonsecure_ppi())
Fu Weiee34f1e2017-01-18 21:25:27 +08001035 disable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001036
Viresh Kumar46c5bfd2015-06-12 13:30:12 +05301037 clk->set_state_shutdown(clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001038}
1039
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001040static int arch_timer_dying_cpu(unsigned int cpu)
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001041{
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001042 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001043
Julien Thierryec5c8e42017-10-13 14:32:55 +01001044 cpumask_clear_cpu(smp_processor_id(), &evtstrm_available);
1045
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001046 arch_timer_stop(clk);
1047 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001048}
1049
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001050#ifdef CONFIG_CPU_PM
Marc Zyngierbee67c52017-04-04 17:05:16 +01001051static DEFINE_PER_CPU(unsigned long, saved_cntkctl);
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001052static int arch_timer_cpu_pm_notify(struct notifier_block *self,
1053 unsigned long action, void *hcpu)
1054{
Julien Thierryec5c8e42017-10-13 14:32:55 +01001055 if (action == CPU_PM_ENTER) {
Marc Zyngierbee67c52017-04-04 17:05:16 +01001056 __this_cpu_write(saved_cntkctl, arch_timer_get_cntkctl());
Julien Thierryec5c8e42017-10-13 14:32:55 +01001057
1058 cpumask_clear_cpu(smp_processor_id(), &evtstrm_available);
1059 } else if (action == CPU_PM_ENTER_FAILED || action == CPU_PM_EXIT) {
Marc Zyngierbee67c52017-04-04 17:05:16 +01001060 arch_timer_set_cntkctl(__this_cpu_read(saved_cntkctl));
Julien Thierryec5c8e42017-10-13 14:32:55 +01001061
Andrew Murrayaaba0982019-04-09 10:52:40 +01001062#ifdef CONFIG_ARM64
1063 if (cpu_have_named_feature(EVTSTRM))
1064#else
Julien Thierryec5c8e42017-10-13 14:32:55 +01001065 if (elf_hwcap & HWCAP_EVTSTRM)
Andrew Murrayaaba0982019-04-09 10:52:40 +01001066#endif
Julien Thierryec5c8e42017-10-13 14:32:55 +01001067 cpumask_set_cpu(smp_processor_id(), &evtstrm_available);
1068 }
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001069 return NOTIFY_OK;
1070}
1071
1072static struct notifier_block arch_timer_cpu_pm_notifier = {
1073 .notifier_call = arch_timer_cpu_pm_notify,
1074};
1075
1076static int __init arch_timer_cpu_pm_init(void)
1077{
1078 return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier);
1079}
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001080
1081static void __init arch_timer_cpu_pm_deinit(void)
1082{
1083 WARN_ON(cpu_pm_unregister_notifier(&arch_timer_cpu_pm_notifier));
1084}
1085
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001086#else
1087static int __init arch_timer_cpu_pm_init(void)
1088{
1089 return 0;
1090}
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001091
1092static void __init arch_timer_cpu_pm_deinit(void)
1093{
1094}
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001095#endif
1096
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001097static int __init arch_timer_register(void)
1098{
1099 int err;
1100 int ppi;
1101
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001102 arch_timer_evt = alloc_percpu(struct clock_event_device);
1103 if (!arch_timer_evt) {
1104 err = -ENOMEM;
1105 goto out;
1106 }
1107
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001108 ppi = arch_timer_ppi[arch_timer_uses_ppi];
1109 switch (arch_timer_uses_ppi) {
Fu Weiee34f1e2017-01-18 21:25:27 +08001110 case ARCH_TIMER_VIRT_PPI:
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001111 err = request_percpu_irq(ppi, arch_timer_handler_virt,
1112 "arch_timer", arch_timer_evt);
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001113 break;
Fu Weiee34f1e2017-01-18 21:25:27 +08001114 case ARCH_TIMER_PHYS_SECURE_PPI:
1115 case ARCH_TIMER_PHYS_NONSECURE_PPI:
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001116 err = request_percpu_irq(ppi, arch_timer_handler_phys,
1117 "arch_timer", arch_timer_evt);
Fu Wei4502b6b2017-01-18 21:25:30 +08001118 if (!err && arch_timer_has_nonsecure_ppi()) {
Fu Weiee34f1e2017-01-18 21:25:27 +08001119 ppi = arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI];
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001120 err = request_percpu_irq(ppi, arch_timer_handler_phys,
1121 "arch_timer", arch_timer_evt);
1122 if (err)
Fu Weiee34f1e2017-01-18 21:25:27 +08001123 free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_SECURE_PPI],
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001124 arch_timer_evt);
1125 }
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001126 break;
Fu Weiee34f1e2017-01-18 21:25:27 +08001127 case ARCH_TIMER_HYP_PPI:
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001128 err = request_percpu_irq(ppi, arch_timer_handler_phys,
1129 "arch_timer", arch_timer_evt);
1130 break;
1131 default:
1132 BUG();
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001133 }
1134
1135 if (err) {
Fu Weided24012017-01-18 21:25:25 +08001136 pr_err("can't register interrupt %d (%d)\n", ppi, err);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001137 goto out_free;
1138 }
1139
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001140 err = arch_timer_cpu_pm_init();
1141 if (err)
1142 goto out_unreg_notify;
1143
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001144 /* Register and immediately configure the timer on the boot CPU */
1145 err = cpuhp_setup_state(CPUHP_AP_ARM_ARCH_TIMER_STARTING,
Thomas Gleixner73c1b412016-12-21 20:19:54 +01001146 "clockevents/arm/arch_timer:starting",
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001147 arch_timer_starting_cpu, arch_timer_dying_cpu);
1148 if (err)
1149 goto out_unreg_cpupm;
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001150 return 0;
1151
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001152out_unreg_cpupm:
1153 arch_timer_cpu_pm_deinit();
1154
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001155out_unreg_notify:
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001156 free_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], arch_timer_evt);
1157 if (arch_timer_has_nonsecure_ppi())
Fu Weiee34f1e2017-01-18 21:25:27 +08001158 free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001159 arch_timer_evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001160
1161out_free:
1162 free_percpu(arch_timer_evt);
1163out:
1164 return err;
1165}
1166
Stephen Boyd22006992013-07-18 16:59:32 -07001167static int __init arch_timer_mem_register(void __iomem *base, unsigned int irq)
1168{
1169 int ret;
1170 irq_handler_t func;
1171 struct arch_timer *t;
1172
1173 t = kzalloc(sizeof(*t), GFP_KERNEL);
1174 if (!t)
1175 return -ENOMEM;
1176
1177 t->base = base;
1178 t->evt.irq = irq;
Fu Wei8a5c21d2017-01-18 21:25:26 +08001179 __arch_timer_setup(ARCH_TIMER_TYPE_MEM, &t->evt);
Stephen Boyd22006992013-07-18 16:59:32 -07001180
1181 if (arch_timer_mem_use_virtual)
1182 func = arch_timer_handler_virt_mem;
1183 else
1184 func = arch_timer_handler_phys_mem;
1185
1186 ret = request_irq(irq, func, IRQF_TIMER, "arch_mem_timer", &t->evt);
1187 if (ret) {
Fu Weided24012017-01-18 21:25:25 +08001188 pr_err("Failed to request mem timer irq\n");
Stephen Boyd22006992013-07-18 16:59:32 -07001189 kfree(t);
1190 }
1191
1192 return ret;
1193}
1194
1195static const struct of_device_id arch_timer_of_match[] __initconst = {
1196 { .compatible = "arm,armv7-timer", },
1197 { .compatible = "arm,armv8-timer", },
1198 {},
1199};
1200
1201static const struct of_device_id arch_timer_mem_of_match[] __initconst = {
1202 { .compatible = "arm,armv7-timer-mem", },
1203 {},
1204};
1205
Fu Wei13bf6992017-03-22 00:31:14 +08001206static bool __init arch_timer_needs_of_probing(void)
Sudeep Hollac387f072014-09-29 01:50:05 +02001207{
1208 struct device_node *dn;
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001209 bool needs_probing = false;
Fu Wei13bf6992017-03-22 00:31:14 +08001210 unsigned int mask = ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM;
Sudeep Hollac387f072014-09-29 01:50:05 +02001211
Fu Wei13bf6992017-03-22 00:31:14 +08001212 /* We have two timers, and both device-tree nodes are probed. */
1213 if ((arch_timers_present & mask) == mask)
1214 return false;
1215
1216 /*
1217 * Only one type of timer is probed,
1218 * check if we have another type of timer node in device-tree.
1219 */
1220 if (arch_timers_present & ARCH_TIMER_TYPE_CP15)
1221 dn = of_find_matching_node(NULL, arch_timer_mem_of_match);
1222 else
1223 dn = of_find_matching_node(NULL, arch_timer_of_match);
1224
1225 if (dn && of_device_is_available(dn))
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001226 needs_probing = true;
Fu Wei13bf6992017-03-22 00:31:14 +08001227
Sudeep Hollac387f072014-09-29 01:50:05 +02001228 of_node_put(dn);
1229
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001230 return needs_probing;
Sudeep Hollac387f072014-09-29 01:50:05 +02001231}
1232
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001233static int __init arch_timer_common_init(void)
Stephen Boyd22006992013-07-18 16:59:32 -07001234{
Stephen Boyd22006992013-07-18 16:59:32 -07001235 arch_timer_banner(arch_timers_present);
1236 arch_counter_register(arch_timers_present);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001237 return arch_timer_arch_init();
Stephen Boyd22006992013-07-18 16:59:32 -07001238}
1239
Fu Wei4502b6b2017-01-18 21:25:30 +08001240/**
1241 * arch_timer_select_ppi() - Select suitable PPI for the current system.
1242 *
1243 * If HYP mode is available, we know that the physical timer
1244 * has been configured to be accessible from PL1. Use it, so
1245 * that a guest can use the virtual timer instead.
1246 *
1247 * On ARMv8.1 with VH extensions, the kernel runs in HYP. VHE
1248 * accesses to CNTP_*_EL1 registers are silently redirected to
1249 * their CNTHP_*_EL2 counterparts, and use a different PPI
1250 * number.
1251 *
1252 * If no interrupt provided for virtual timer, we'll have to
1253 * stick to the physical timer. It'd better be accessible...
1254 * For arm64 we never use the secure interrupt.
1255 *
1256 * Return: a suitable PPI type for the current system.
1257 */
1258static enum arch_timer_ppi_nr __init arch_timer_select_ppi(void)
1259{
1260 if (is_kernel_in_hyp_mode())
1261 return ARCH_TIMER_HYP_PPI;
1262
1263 if (!is_hyp_mode_available() && arch_timer_ppi[ARCH_TIMER_VIRT_PPI])
1264 return ARCH_TIMER_VIRT_PPI;
1265
1266 if (IS_ENABLED(CONFIG_ARM64))
1267 return ARCH_TIMER_PHYS_NONSECURE_PPI;
1268
1269 return ARCH_TIMER_PHYS_SECURE_PPI;
1270}
1271
Andre Przywaraee793042018-07-06 09:11:50 +01001272static void __init arch_timer_populate_kvm_info(void)
1273{
1274 arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI];
1275 if (is_kernel_in_hyp_mode())
1276 arch_timer_kvm_info.physical_irq = arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI];
1277}
1278
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001279static int __init arch_timer_of_init(struct device_node *np)
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001280{
Fu Weica0e1b52017-03-22 00:31:15 +08001281 int i, ret;
Fu Wei5d3dfa92017-03-22 00:31:13 +08001282 u32 rate;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001283
Fu Wei8a5c21d2017-01-18 21:25:26 +08001284 if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
Fu Weided24012017-01-18 21:25:25 +08001285 pr_warn("multiple nodes in dt, skipping\n");
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001286 return 0;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001287 }
1288
Fu Wei8a5c21d2017-01-18 21:25:26 +08001289 arch_timers_present |= ARCH_TIMER_TYPE_CP15;
Fu Weiee34f1e2017-01-18 21:25:27 +08001290 for (i = ARCH_TIMER_PHYS_SECURE_PPI; i < ARCH_TIMER_MAX_TIMER_PPI; i++)
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001291 arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
1292
Andre Przywaraee793042018-07-06 09:11:50 +01001293 arch_timer_populate_kvm_info();
Fu Weica0e1b52017-03-22 00:31:15 +08001294
Fu Weic389d702017-04-01 01:51:00 +08001295 rate = arch_timer_get_cntfrq();
Fu Wei5d3dfa92017-03-22 00:31:13 +08001296 arch_timer_of_configure_rate(rate, np);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001297
1298 arch_timer_c3stop = !of_property_read_bool(np, "always-on");
1299
Marc Zyngier651bb2e2017-01-19 17:20:59 +00001300 /* Check for globally applicable workarounds */
1301 arch_timer_check_ool_workaround(ate_match_dt, np);
Scott Woodf6dc1572016-09-22 03:35:17 -05001302
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001303 /*
1304 * If we cannot rely on firmware initializing the timer registers then
1305 * we should use the physical timers instead.
1306 */
1307 if (IS_ENABLED(CONFIG_ARM) &&
1308 of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
Fu Weiee34f1e2017-01-18 21:25:27 +08001309 arch_timer_uses_ppi = ARCH_TIMER_PHYS_SECURE_PPI;
Fu Wei4502b6b2017-01-18 21:25:30 +08001310 else
1311 arch_timer_uses_ppi = arch_timer_select_ppi();
1312
1313 if (!arch_timer_ppi[arch_timer_uses_ppi]) {
1314 pr_err("No interrupt available, giving up\n");
1315 return -EINVAL;
1316 }
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001317
Brian Norrisd8ec7592016-10-04 11:12:09 -07001318 /* On some systems, the counter stops ticking when in suspend. */
1319 arch_counter_suspend_stop = of_property_read_bool(np,
1320 "arm,no-tick-in-suspend");
1321
Fu Weica0e1b52017-03-22 00:31:15 +08001322 ret = arch_timer_register();
1323 if (ret)
1324 return ret;
1325
1326 if (arch_timer_needs_of_probing())
1327 return 0;
1328
1329 return arch_timer_common_init();
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001330}
Daniel Lezcano17273392017-05-26 16:56:11 +02001331TIMER_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_of_init);
1332TIMER_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_of_init);
Stephen Boyd22006992013-07-18 16:59:32 -07001333
Fu Weic389d702017-04-01 01:51:00 +08001334static u32 __init
1335arch_timer_mem_frame_get_cntfrq(struct arch_timer_mem_frame *frame)
Stephen Boyd22006992013-07-18 16:59:32 -07001336{
Fu Weic389d702017-04-01 01:51:00 +08001337 void __iomem *base;
1338 u32 rate;
Stephen Boyd22006992013-07-18 16:59:32 -07001339
Fu Weic389d702017-04-01 01:51:00 +08001340 base = ioremap(frame->cntbase, frame->size);
1341 if (!base) {
1342 pr_err("Unable to map frame @ %pa\n", &frame->cntbase);
1343 return 0;
1344 }
1345
Frank Rowand3db12002017-06-09 17:26:32 -07001346 rate = readl_relaxed(base + CNTFRQ);
Fu Weic389d702017-04-01 01:51:00 +08001347
Frank Rowand3db12002017-06-09 17:26:32 -07001348 iounmap(base);
Fu Weic389d702017-04-01 01:51:00 +08001349
1350 return rate;
1351}
1352
1353static struct arch_timer_mem_frame * __init
1354arch_timer_mem_find_best_frame(struct arch_timer_mem *timer_mem)
1355{
1356 struct arch_timer_mem_frame *frame, *best_frame = NULL;
1357 void __iomem *cntctlbase;
1358 u32 cnttidr;
1359 int i;
1360
1361 cntctlbase = ioremap(timer_mem->cntctlbase, timer_mem->size);
Stephen Boyd22006992013-07-18 16:59:32 -07001362 if (!cntctlbase) {
Fu Weic389d702017-04-01 01:51:00 +08001363 pr_err("Can't map CNTCTLBase @ %pa\n",
1364 &timer_mem->cntctlbase);
1365 return NULL;
Stephen Boyd22006992013-07-18 16:59:32 -07001366 }
1367
1368 cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
Stephen Boyd22006992013-07-18 16:59:32 -07001369
1370 /*
1371 * Try to find a virtual capable frame. Otherwise fall back to a
1372 * physical capable frame.
1373 */
Fu Weic389d702017-04-01 01:51:00 +08001374 for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
1375 u32 cntacr = CNTACR_RFRQ | CNTACR_RWPT | CNTACR_RPCT |
1376 CNTACR_RWVT | CNTACR_RVOFF | CNTACR_RVCT;
Stephen Boyd22006992013-07-18 16:59:32 -07001377
Fu Weic389d702017-04-01 01:51:00 +08001378 frame = &timer_mem->frame[i];
1379 if (!frame->valid)
1380 continue;
Stephen Boyd22006992013-07-18 16:59:32 -07001381
Robin Murphye392d602016-02-01 12:00:48 +00001382 /* Try enabling everything, and see what sticks */
Fu Weic389d702017-04-01 01:51:00 +08001383 writel_relaxed(cntacr, cntctlbase + CNTACR(i));
1384 cntacr = readl_relaxed(cntctlbase + CNTACR(i));
Robin Murphye392d602016-02-01 12:00:48 +00001385
Fu Weic389d702017-04-01 01:51:00 +08001386 if ((cnttidr & CNTTIDR_VIRT(i)) &&
Robin Murphye392d602016-02-01 12:00:48 +00001387 !(~cntacr & (CNTACR_RWVT | CNTACR_RVCT))) {
Stephen Boyd22006992013-07-18 16:59:32 -07001388 best_frame = frame;
1389 arch_timer_mem_use_virtual = true;
1390 break;
1391 }
Robin Murphye392d602016-02-01 12:00:48 +00001392
1393 if (~cntacr & (CNTACR_RWPT | CNTACR_RPCT))
1394 continue;
1395
Fu Weic389d702017-04-01 01:51:00 +08001396 best_frame = frame;
Stephen Boyd22006992013-07-18 16:59:32 -07001397 }
1398
Fu Weic389d702017-04-01 01:51:00 +08001399 iounmap(cntctlbase);
1400
Sudeep Hollaf63d9472017-05-08 13:32:27 +01001401 return best_frame;
Fu Weic389d702017-04-01 01:51:00 +08001402}
1403
1404static int __init
1405arch_timer_mem_frame_register(struct arch_timer_mem_frame *frame)
1406{
1407 void __iomem *base;
1408 int ret, irq = 0;
Stephen Boyd22006992013-07-18 16:59:32 -07001409
1410 if (arch_timer_mem_use_virtual)
Fu Weic389d702017-04-01 01:51:00 +08001411 irq = frame->virt_irq;
Stephen Boyd22006992013-07-18 16:59:32 -07001412 else
Fu Weic389d702017-04-01 01:51:00 +08001413 irq = frame->phys_irq;
Robin Murphye392d602016-02-01 12:00:48 +00001414
Stephen Boyd22006992013-07-18 16:59:32 -07001415 if (!irq) {
Fu Weided24012017-01-18 21:25:25 +08001416 pr_err("Frame missing %s irq.\n",
Thomas Gleixnercfb6d652013-08-21 14:59:23 +02001417 arch_timer_mem_use_virtual ? "virt" : "phys");
Fu Weic389d702017-04-01 01:51:00 +08001418 return -EINVAL;
1419 }
1420
1421 if (!request_mem_region(frame->cntbase, frame->size,
1422 "arch_mem_timer"))
1423 return -EBUSY;
1424
1425 base = ioremap(frame->cntbase, frame->size);
1426 if (!base) {
1427 pr_err("Can't map frame's registers\n");
1428 return -ENXIO;
1429 }
1430
1431 ret = arch_timer_mem_register(base, irq);
1432 if (ret) {
1433 iounmap(base);
1434 return ret;
1435 }
1436
1437 arch_counter_base = base;
1438 arch_timers_present |= ARCH_TIMER_TYPE_MEM;
1439
1440 return 0;
1441}
1442
1443static int __init arch_timer_mem_of_init(struct device_node *np)
1444{
1445 struct arch_timer_mem *timer_mem;
1446 struct arch_timer_mem_frame *frame;
1447 struct device_node *frame_node;
1448 struct resource res;
1449 int ret = -EINVAL;
1450 u32 rate;
1451
1452 timer_mem = kzalloc(sizeof(*timer_mem), GFP_KERNEL);
1453 if (!timer_mem)
1454 return -ENOMEM;
1455
1456 if (of_address_to_resource(np, 0, &res))
1457 goto out;
1458 timer_mem->cntctlbase = res.start;
1459 timer_mem->size = resource_size(&res);
1460
1461 for_each_available_child_of_node(np, frame_node) {
1462 u32 n;
1463 struct arch_timer_mem_frame *frame;
1464
1465 if (of_property_read_u32(frame_node, "frame-number", &n)) {
1466 pr_err(FW_BUG "Missing frame-number.\n");
1467 of_node_put(frame_node);
1468 goto out;
1469 }
1470 if (n >= ARCH_TIMER_MEM_MAX_FRAMES) {
1471 pr_err(FW_BUG "Wrong frame-number, only 0-%u are permitted.\n",
1472 ARCH_TIMER_MEM_MAX_FRAMES - 1);
1473 of_node_put(frame_node);
1474 goto out;
1475 }
1476 frame = &timer_mem->frame[n];
1477
1478 if (frame->valid) {
1479 pr_err(FW_BUG "Duplicated frame-number.\n");
1480 of_node_put(frame_node);
1481 goto out;
1482 }
1483
1484 if (of_address_to_resource(frame_node, 0, &res)) {
1485 of_node_put(frame_node);
1486 goto out;
1487 }
1488 frame->cntbase = res.start;
1489 frame->size = resource_size(&res);
1490
1491 frame->virt_irq = irq_of_parse_and_map(frame_node,
1492 ARCH_TIMER_VIRT_SPI);
1493 frame->phys_irq = irq_of_parse_and_map(frame_node,
1494 ARCH_TIMER_PHYS_SPI);
1495
1496 frame->valid = true;
1497 }
1498
1499 frame = arch_timer_mem_find_best_frame(timer_mem);
1500 if (!frame) {
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001501 pr_err("Unable to find a suitable frame in timer @ %pa\n",
1502 &timer_mem->cntctlbase);
Fu Weic389d702017-04-01 01:51:00 +08001503 ret = -EINVAL;
Robin Murphye392d602016-02-01 12:00:48 +00001504 goto out;
Stephen Boyd22006992013-07-18 16:59:32 -07001505 }
1506
Fu Weic389d702017-04-01 01:51:00 +08001507 rate = arch_timer_mem_frame_get_cntfrq(frame);
Fu Wei5d3dfa92017-03-22 00:31:13 +08001508 arch_timer_of_configure_rate(rate, np);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001509
Fu Weic389d702017-04-01 01:51:00 +08001510 ret = arch_timer_mem_frame_register(frame);
1511 if (!ret && !arch_timer_needs_of_probing())
Fu Weica0e1b52017-03-22 00:31:15 +08001512 ret = arch_timer_common_init();
Robin Murphye392d602016-02-01 12:00:48 +00001513out:
Fu Weic389d702017-04-01 01:51:00 +08001514 kfree(timer_mem);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001515 return ret;
Stephen Boyd22006992013-07-18 16:59:32 -07001516}
Daniel Lezcano17273392017-05-26 16:56:11 +02001517TIMER_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem",
Fu Weic389d702017-04-01 01:51:00 +08001518 arch_timer_mem_of_init);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001519
Fu Weif79d2092017-04-01 01:51:02 +08001520#ifdef CONFIG_ACPI_GTDT
Fu Weic2743a32017-04-01 01:51:04 +08001521static int __init
1522arch_timer_mem_verify_cntfrq(struct arch_timer_mem *timer_mem)
1523{
1524 struct arch_timer_mem_frame *frame;
1525 u32 rate;
1526 int i;
1527
1528 for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
1529 frame = &timer_mem->frame[i];
1530
1531 if (!frame->valid)
1532 continue;
1533
1534 rate = arch_timer_mem_frame_get_cntfrq(frame);
1535 if (rate == arch_timer_rate)
1536 continue;
1537
1538 pr_err(FW_BUG "CNTFRQ mismatch: frame @ %pa: (0x%08lx), CPU: (0x%08lx)\n",
1539 &frame->cntbase,
1540 (unsigned long)rate, (unsigned long)arch_timer_rate);
1541
1542 return -EINVAL;
1543 }
1544
1545 return 0;
1546}
1547
1548static int __init arch_timer_mem_acpi_init(int platform_timer_count)
1549{
1550 struct arch_timer_mem *timers, *timer;
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001551 struct arch_timer_mem_frame *frame, *best_frame = NULL;
Fu Weic2743a32017-04-01 01:51:04 +08001552 int timer_count, i, ret = 0;
1553
1554 timers = kcalloc(platform_timer_count, sizeof(*timers),
1555 GFP_KERNEL);
1556 if (!timers)
1557 return -ENOMEM;
1558
1559 ret = acpi_arch_timer_mem_init(timers, &timer_count);
1560 if (ret || !timer_count)
1561 goto out;
1562
Fu Weic2743a32017-04-01 01:51:04 +08001563 /*
1564 * While unlikely, it's theoretically possible that none of the frames
1565 * in a timer expose the combination of feature we want.
1566 */
Matthias Kaehlcked197f792017-07-31 11:37:28 -07001567 for (i = 0; i < timer_count; i++) {
Fu Weic2743a32017-04-01 01:51:04 +08001568 timer = &timers[i];
1569
1570 frame = arch_timer_mem_find_best_frame(timer);
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001571 if (!best_frame)
1572 best_frame = frame;
1573
1574 ret = arch_timer_mem_verify_cntfrq(timer);
1575 if (ret) {
1576 pr_err("Disabling MMIO timers due to CNTFRQ mismatch\n");
1577 goto out;
1578 }
1579
1580 if (!best_frame) /* implies !frame */
1581 /*
1582 * Only complain about missing suitable frames if we
1583 * haven't already found one in a previous iteration.
1584 */
1585 pr_err("Unable to find a suitable frame in timer @ %pa\n",
1586 &timer->cntctlbase);
Fu Weic2743a32017-04-01 01:51:04 +08001587 }
1588
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001589 if (best_frame)
1590 ret = arch_timer_mem_frame_register(best_frame);
Fu Weic2743a32017-04-01 01:51:04 +08001591out:
1592 kfree(timers);
1593 return ret;
1594}
1595
1596/* Initialize per-processor generic timer and memory-mapped timer(if present) */
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001597static int __init arch_timer_acpi_init(struct acpi_table_header *table)
1598{
Fu Weic2743a32017-04-01 01:51:04 +08001599 int ret, platform_timer_count;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001600
Fu Wei8a5c21d2017-01-18 21:25:26 +08001601 if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
Fu Weided24012017-01-18 21:25:25 +08001602 pr_warn("already initialized, skipping\n");
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001603 return -EINVAL;
1604 }
1605
Fu Wei8a5c21d2017-01-18 21:25:26 +08001606 arch_timers_present |= ARCH_TIMER_TYPE_CP15;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001607
Fu Weic2743a32017-04-01 01:51:04 +08001608 ret = acpi_gtdt_init(table, &platform_timer_count);
Fu Weif79d2092017-04-01 01:51:02 +08001609 if (ret) {
1610 pr_err("Failed to init GTDT table.\n");
1611 return ret;
1612 }
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001613
Fu Weiee34f1e2017-01-18 21:25:27 +08001614 arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI] =
Fu Weif79d2092017-04-01 01:51:02 +08001615 acpi_gtdt_map_ppi(ARCH_TIMER_PHYS_NONSECURE_PPI);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001616
Fu Weiee34f1e2017-01-18 21:25:27 +08001617 arch_timer_ppi[ARCH_TIMER_VIRT_PPI] =
Fu Weif79d2092017-04-01 01:51:02 +08001618 acpi_gtdt_map_ppi(ARCH_TIMER_VIRT_PPI);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001619
Fu Weiee34f1e2017-01-18 21:25:27 +08001620 arch_timer_ppi[ARCH_TIMER_HYP_PPI] =
Fu Weif79d2092017-04-01 01:51:02 +08001621 acpi_gtdt_map_ppi(ARCH_TIMER_HYP_PPI);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001622
Andre Przywaraee793042018-07-06 09:11:50 +01001623 arch_timer_populate_kvm_info();
Fu Weica0e1b52017-03-22 00:31:15 +08001624
Fu Wei5d3dfa92017-03-22 00:31:13 +08001625 /*
1626 * When probing via ACPI, we have no mechanism to override the sysreg
1627 * CNTFRQ value. This *must* be correct.
1628 */
1629 arch_timer_rate = arch_timer_get_cntfrq();
1630 if (!arch_timer_rate) {
1631 pr_err(FW_BUG "frequency not available.\n");
1632 return -EINVAL;
1633 }
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001634
Fu Wei4502b6b2017-01-18 21:25:30 +08001635 arch_timer_uses_ppi = arch_timer_select_ppi();
1636 if (!arch_timer_ppi[arch_timer_uses_ppi]) {
1637 pr_err("No interrupt available, giving up\n");
1638 return -EINVAL;
1639 }
1640
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001641 /* Always-on capability */
Fu Weif79d2092017-04-01 01:51:02 +08001642 arch_timer_c3stop = acpi_gtdt_c3stop(arch_timer_uses_ppi);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001643
Marc Zyngier5a38bca2017-02-21 14:37:30 +00001644 /* Check for globally applicable workarounds */
1645 arch_timer_check_ool_workaround(ate_match_acpi_oem_info, table);
1646
Fu Weica0e1b52017-03-22 00:31:15 +08001647 ret = arch_timer_register();
1648 if (ret)
1649 return ret;
1650
Fu Weic2743a32017-04-01 01:51:04 +08001651 if (platform_timer_count &&
1652 arch_timer_mem_acpi_init(platform_timer_count))
1653 pr_err("Failed to initialize memory-mapped timer.\n");
1654
Fu Weica0e1b52017-03-22 00:31:15 +08001655 return arch_timer_common_init();
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001656}
Daniel Lezcano77d62f52017-05-26 17:42:25 +02001657TIMER_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001658#endif