blob: 9a7d4dc00b6ea196b37736f8b3bfc7d69e8f2558 [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
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000329#ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
Mark Rutlanda7fb4572017-10-16 16:28:39 +0100330DEFINE_PER_CPU(const struct arch_timer_erratum_workaround *, timer_unstable_counter_workaround);
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000331EXPORT_SYMBOL_GPL(timer_unstable_counter_workaround);
332
333DEFINE_STATIC_KEY_FALSE(arch_timer_read_ool_enabled);
334EXPORT_SYMBOL_GPL(arch_timer_read_ool_enabled);
335
Marc Zyngier83280892017-01-27 10:27:09 +0000336static void erratum_set_next_event_tval_generic(const int access, unsigned long evt,
337 struct clock_event_device *clk)
338{
339 unsigned long ctrl;
Christoffer Dalle6d68b002017-07-05 11:04:28 +0200340 u64 cval;
Marc Zyngier83280892017-01-27 10:27:09 +0000341
342 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
343 ctrl |= ARCH_TIMER_CTRL_ENABLE;
344 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
345
Christoffer Dalle6d68b002017-07-05 11:04:28 +0200346 if (access == ARCH_TIMER_PHYS_ACCESS) {
347 cval = evt + arch_counter_get_cntpct();
Marc Zyngier83280892017-01-27 10:27:09 +0000348 write_sysreg(cval, cntp_cval_el0);
Christoffer Dalle6d68b002017-07-05 11:04:28 +0200349 } else {
350 cval = evt + arch_counter_get_cntvct();
Marc Zyngier83280892017-01-27 10:27:09 +0000351 write_sysreg(cval, cntv_cval_el0);
Christoffer Dalle6d68b002017-07-05 11:04:28 +0200352 }
Marc Zyngier83280892017-01-27 10:27:09 +0000353
354 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
355}
356
Arnd Bergmanneb645222017-04-19 19:37:09 +0200357static __maybe_unused int erratum_set_next_event_tval_virt(unsigned long evt,
Marc Zyngier83280892017-01-27 10:27:09 +0000358 struct clock_event_device *clk)
359{
360 erratum_set_next_event_tval_generic(ARCH_TIMER_VIRT_ACCESS, evt, clk);
361 return 0;
362}
363
Arnd Bergmanneb645222017-04-19 19:37:09 +0200364static __maybe_unused int erratum_set_next_event_tval_phys(unsigned long evt,
Marc Zyngier83280892017-01-27 10:27:09 +0000365 struct clock_event_device *clk)
366{
367 erratum_set_next_event_tval_generic(ARCH_TIMER_PHYS_ACCESS, evt, clk);
368 return 0;
369}
370
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000371static const struct arch_timer_erratum_workaround ool_workarounds[] = {
372#ifdef CONFIG_FSL_ERRATUM_A008585
373 {
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000374 .match_type = ate_match_dt,
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000375 .id = "fsl,erratum-a008585",
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000376 .desc = "Freescale erratum a005858",
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000377 .read_cntp_tval_el0 = fsl_a008585_read_cntp_tval_el0,
378 .read_cntv_tval_el0 = fsl_a008585_read_cntv_tval_el0,
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200379 .read_cntpct_el0 = fsl_a008585_read_cntpct_el0,
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000380 .read_cntvct_el0 = fsl_a008585_read_cntvct_el0,
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000381 .set_next_event_phys = erratum_set_next_event_tval_phys,
382 .set_next_event_virt = erratum_set_next_event_tval_virt,
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000383 },
384#endif
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000385#ifdef CONFIG_HISILICON_ERRATUM_161010101
386 {
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000387 .match_type = ate_match_dt,
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000388 .id = "hisilicon,erratum-161010101",
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000389 .desc = "HiSilicon erratum 161010101",
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000390 .read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
391 .read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200392 .read_cntpct_el0 = hisi_161010101_read_cntpct_el0,
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000393 .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000394 .set_next_event_phys = erratum_set_next_event_tval_phys,
395 .set_next_event_virt = erratum_set_next_event_tval_virt,
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000396 },
Marc Zyngierd003d022017-02-21 15:04:27 +0000397 {
398 .match_type = ate_match_acpi_oem_info,
399 .id = hisi_161010101_oem_info,
400 .desc = "HiSilicon erratum 161010101",
401 .read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
402 .read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200403 .read_cntpct_el0 = hisi_161010101_read_cntpct_el0,
Marc Zyngierd003d022017-02-21 15:04:27 +0000404 .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
405 .set_next_event_phys = erratum_set_next_event_tval_phys,
406 .set_next_event_virt = erratum_set_next_event_tval_virt,
407 },
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000408#endif
Marc Zyngierfa8d8152017-01-27 12:52:31 +0000409#ifdef CONFIG_ARM64_ERRATUM_858921
410 {
411 .match_type = ate_match_local_cap_id,
412 .id = (void *)ARM64_WORKAROUND_858921,
413 .desc = "ARM erratum 858921",
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200414 .read_cntpct_el0 = arm64_858921_read_cntpct_el0,
Marc Zyngierfa8d8152017-01-27 12:52:31 +0000415 .read_cntvct_el0 = arm64_858921_read_cntvct_el0,
416 },
417#endif
Marc Zyngier95b861a42018-09-27 17:15:34 +0100418#ifdef CONFIG_ARM64_ERRATUM_1188873
419 {
420 .match_type = ate_match_local_cap_id,
421 .id = (void *)ARM64_WORKAROUND_1188873,
422 .desc = "ARM erratum 1188873",
423 .read_cntvct_el0 = arm64_1188873_read_cntvct_el0,
424 },
425#endif
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000426};
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000427
428typedef bool (*ate_match_fn_t)(const struct arch_timer_erratum_workaround *,
429 const void *);
430
431static
432bool arch_timer_check_dt_erratum(const struct arch_timer_erratum_workaround *wa,
433 const void *arg)
434{
435 const struct device_node *np = arg;
436
437 return of_property_read_bool(np, wa->id);
438}
439
Marc Zyngier00640302017-03-20 16:47:59 +0000440static
441bool arch_timer_check_local_cap_erratum(const struct arch_timer_erratum_workaround *wa,
442 const void *arg)
443{
444 return this_cpu_has_cap((uintptr_t)wa->id);
445}
446
Marc Zyngier5a38bca2017-02-21 14:37:30 +0000447
448static
449bool arch_timer_check_acpi_oem_erratum(const struct arch_timer_erratum_workaround *wa,
450 const void *arg)
451{
452 static const struct ate_acpi_oem_info empty_oem_info = {};
453 const struct ate_acpi_oem_info *info = wa->id;
454 const struct acpi_table_header *table = arg;
455
456 /* Iterate over the ACPI OEM info array, looking for a match */
457 while (memcmp(info, &empty_oem_info, sizeof(*info))) {
458 if (!memcmp(info->oem_id, table->oem_id, ACPI_OEM_ID_SIZE) &&
459 !memcmp(info->oem_table_id, table->oem_table_id, ACPI_OEM_TABLE_ID_SIZE) &&
460 info->oem_revision == table->oem_revision)
461 return true;
462
463 info++;
464 }
465
466 return false;
467}
468
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000469static const struct arch_timer_erratum_workaround *
470arch_timer_iterate_errata(enum arch_timer_erratum_match_type type,
471 ate_match_fn_t match_fn,
472 void *arg)
473{
474 int i;
475
476 for (i = 0; i < ARRAY_SIZE(ool_workarounds); i++) {
477 if (ool_workarounds[i].match_type != type)
478 continue;
479
480 if (match_fn(&ool_workarounds[i], arg))
481 return &ool_workarounds[i];
482 }
483
484 return NULL;
485}
486
487static
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000488void arch_timer_enable_workaround(const struct arch_timer_erratum_workaround *wa,
489 bool local)
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000490{
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000491 int i;
492
493 if (local) {
494 __this_cpu_write(timer_unstable_counter_workaround, wa);
495 } else {
496 for_each_possible_cpu(i)
497 per_cpu(timer_unstable_counter_workaround, i) = wa;
498 }
499
Marc Zyngier450f9682017-08-01 09:02:57 +0100500 /*
501 * Use the locked version, as we're called from the CPU
502 * hotplug framework. Otherwise, we end-up in deadlock-land.
503 */
504 static_branch_enable_cpuslocked(&arch_timer_read_ool_enabled);
Marc Zyngiera86bd132017-02-01 12:07:15 +0000505
506 /*
507 * Don't use the vdso fastpath if errata require using the
508 * out-of-line counter accessor. We may change our mind pretty
509 * late in the game (with a per-CPU erratum, for example), so
510 * change both the default value and the vdso itself.
511 */
512 if (wa->read_cntvct_el0) {
513 clocksource_counter.archdata.vdso_direct = false;
514 vdso_default = false;
515 }
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000516}
517
518static void arch_timer_check_ool_workaround(enum arch_timer_erratum_match_type type,
519 void *arg)
520{
521 const struct arch_timer_erratum_workaround *wa;
522 ate_match_fn_t match_fn = NULL;
Marc Zyngier00640302017-03-20 16:47:59 +0000523 bool local = false;
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000524
525 switch (type) {
526 case ate_match_dt:
527 match_fn = arch_timer_check_dt_erratum;
528 break;
Marc Zyngier00640302017-03-20 16:47:59 +0000529 case ate_match_local_cap_id:
530 match_fn = arch_timer_check_local_cap_erratum;
531 local = true;
532 break;
Marc Zyngier5a38bca2017-02-21 14:37:30 +0000533 case ate_match_acpi_oem_info:
534 match_fn = arch_timer_check_acpi_oem_erratum;
535 break;
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000536 default:
537 WARN_ON(1);
538 return;
539 }
540
541 wa = arch_timer_iterate_errata(type, match_fn, arg);
542 if (!wa)
543 return;
544
Marc Zyngier00640302017-03-20 16:47:59 +0000545 if (needs_unstable_timer_counter_workaround()) {
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000546 const struct arch_timer_erratum_workaround *__wa;
547 __wa = __this_cpu_read(timer_unstable_counter_workaround);
548 if (__wa && wa != __wa)
Marc Zyngier00640302017-03-20 16:47:59 +0000549 pr_warn("Can't enable workaround for %s (clashes with %s\n)",
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000550 wa->desc, __wa->desc);
551
552 if (__wa)
553 return;
Marc Zyngier00640302017-03-20 16:47:59 +0000554 }
555
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000556 arch_timer_enable_workaround(wa, local);
Marc Zyngier00640302017-03-20 16:47:59 +0000557 pr_info("Enabling %s workaround for %s\n",
558 local ? "local" : "global", wa->desc);
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000559}
560
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000561#define erratum_handler(fn, r, ...) \
562({ \
563 bool __val; \
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000564 if (needs_unstable_timer_counter_workaround()) { \
565 const struct arch_timer_erratum_workaround *__wa; \
566 __wa = __this_cpu_read(timer_unstable_counter_workaround); \
567 if (__wa && __wa->fn) { \
568 r = __wa->fn(__VA_ARGS__); \
569 __val = true; \
570 } else { \
571 __val = false; \
572 } \
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000573 } else { \
574 __val = false; \
575 } \
576 __val; \
577})
578
Marc Zyngiera86bd132017-02-01 12:07:15 +0000579static bool arch_timer_this_cpu_has_cntvct_wa(void)
580{
581 const struct arch_timer_erratum_workaround *wa;
582
583 wa = __this_cpu_read(timer_unstable_counter_workaround);
584 return wa && wa->read_cntvct_el0;
585}
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000586#else
587#define arch_timer_check_ool_workaround(t,a) do { } while(0)
Marc Zyngier83280892017-01-27 10:27:09 +0000588#define erratum_set_next_event_tval_virt(...) ({BUG(); 0;})
589#define erratum_set_next_event_tval_phys(...) ({BUG(); 0;})
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000590#define erratum_handler(fn, r, ...) ({false;})
Marc Zyngiera86bd132017-02-01 12:07:15 +0000591#define arch_timer_this_cpu_has_cntvct_wa() ({false;})
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000592#endif /* CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND */
Scott Woodf6dc1572016-09-22 03:35:17 -0500593
Stephen Boyde09f3cc2013-07-18 16:59:28 -0700594static __always_inline irqreturn_t timer_handler(const int access,
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000595 struct clock_event_device *evt)
596{
597 unsigned long ctrl;
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200598
Stephen Boyd60faddf2013-07-18 16:59:31 -0700599 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000600 if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
601 ctrl |= ARCH_TIMER_CTRL_IT_MASK;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700602 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000603 evt->event_handler(evt);
604 return IRQ_HANDLED;
605 }
606
607 return IRQ_NONE;
608}
609
610static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
611{
612 struct clock_event_device *evt = dev_id;
613
614 return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
615}
616
617static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
618{
619 struct clock_event_device *evt = dev_id;
620
621 return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
622}
623
Stephen Boyd22006992013-07-18 16:59:32 -0700624static irqreturn_t arch_timer_handler_phys_mem(int irq, void *dev_id)
625{
626 struct clock_event_device *evt = dev_id;
627
628 return timer_handler(ARCH_TIMER_MEM_PHYS_ACCESS, evt);
629}
630
631static irqreturn_t arch_timer_handler_virt_mem(int irq, void *dev_id)
632{
633 struct clock_event_device *evt = dev_id;
634
635 return timer_handler(ARCH_TIMER_MEM_VIRT_ACCESS, evt);
636}
637
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530638static __always_inline int timer_shutdown(const int access,
639 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000640{
641 unsigned long ctrl;
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530642
643 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
644 ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
645 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
646
647 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000648}
649
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530650static int arch_timer_shutdown_virt(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000651{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530652 return timer_shutdown(ARCH_TIMER_VIRT_ACCESS, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000653}
654
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530655static int arch_timer_shutdown_phys(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000656{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530657 return timer_shutdown(ARCH_TIMER_PHYS_ACCESS, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000658}
659
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530660static int arch_timer_shutdown_virt_mem(struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700661{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530662 return timer_shutdown(ARCH_TIMER_MEM_VIRT_ACCESS, clk);
Stephen Boyd22006992013-07-18 16:59:32 -0700663}
664
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530665static int arch_timer_shutdown_phys_mem(struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700666{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530667 return timer_shutdown(ARCH_TIMER_MEM_PHYS_ACCESS, clk);
Stephen Boyd22006992013-07-18 16:59:32 -0700668}
669
Stephen Boyd60faddf2013-07-18 16:59:31 -0700670static __always_inline void set_next_event(const int access, unsigned long evt,
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200671 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000672{
673 unsigned long ctrl;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700674 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000675 ctrl |= ARCH_TIMER_CTRL_ENABLE;
676 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700677 arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt, clk);
678 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000679}
680
681static int arch_timer_set_next_event_virt(unsigned long evt,
Stephen Boyd60faddf2013-07-18 16:59:31 -0700682 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000683{
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000684 int ret;
685
686 if (erratum_handler(set_next_event_virt, ret, evt, clk))
687 return ret;
Marc Zyngier83280892017-01-27 10:27:09 +0000688
Stephen Boyd60faddf2013-07-18 16:59:31 -0700689 set_next_event(ARCH_TIMER_VIRT_ACCESS, evt, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000690 return 0;
691}
692
693static int arch_timer_set_next_event_phys(unsigned long evt,
Stephen Boyd60faddf2013-07-18 16:59:31 -0700694 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000695{
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000696 int ret;
697
698 if (erratum_handler(set_next_event_phys, ret, evt, clk))
699 return ret;
Marc Zyngier83280892017-01-27 10:27:09 +0000700
Stephen Boyd60faddf2013-07-18 16:59:31 -0700701 set_next_event(ARCH_TIMER_PHYS_ACCESS, evt, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000702 return 0;
703}
704
Stephen Boyd22006992013-07-18 16:59:32 -0700705static int arch_timer_set_next_event_virt_mem(unsigned long evt,
706 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000707{
Stephen Boyd22006992013-07-18 16:59:32 -0700708 set_next_event(ARCH_TIMER_MEM_VIRT_ACCESS, evt, clk);
709 return 0;
710}
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000711
Stephen Boyd22006992013-07-18 16:59:32 -0700712static int arch_timer_set_next_event_phys_mem(unsigned long evt,
713 struct clock_event_device *clk)
714{
715 set_next_event(ARCH_TIMER_MEM_PHYS_ACCESS, evt, clk);
716 return 0;
717}
718
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200719static void __arch_timer_setup(unsigned type,
720 struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700721{
722 clk->features = CLOCK_EVT_FEAT_ONESHOT;
723
Fu Wei8a5c21d2017-01-18 21:25:26 +0800724 if (type == ARCH_TIMER_TYPE_CP15) {
Lorenzo Pieralisi82a561942014-04-08 10:04:32 +0100725 if (arch_timer_c3stop)
726 clk->features |= CLOCK_EVT_FEAT_C3STOP;
Stephen Boyd22006992013-07-18 16:59:32 -0700727 clk->name = "arch_sys_timer";
728 clk->rating = 450;
729 clk->cpumask = cpumask_of(smp_processor_id());
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000730 clk->irq = arch_timer_ppi[arch_timer_uses_ppi];
731 switch (arch_timer_uses_ppi) {
Fu Weiee34f1e2017-01-18 21:25:27 +0800732 case ARCH_TIMER_VIRT_PPI:
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530733 clk->set_state_shutdown = arch_timer_shutdown_virt;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530734 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt;
Stephen Boyd22006992013-07-18 16:59:32 -0700735 clk->set_next_event = arch_timer_set_next_event_virt;
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000736 break;
Fu Weiee34f1e2017-01-18 21:25:27 +0800737 case ARCH_TIMER_PHYS_SECURE_PPI:
738 case ARCH_TIMER_PHYS_NONSECURE_PPI:
739 case ARCH_TIMER_HYP_PPI:
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530740 clk->set_state_shutdown = arch_timer_shutdown_phys;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530741 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys;
Stephen Boyd22006992013-07-18 16:59:32 -0700742 clk->set_next_event = arch_timer_set_next_event_phys;
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000743 break;
744 default:
745 BUG();
Stephen Boyd22006992013-07-18 16:59:32 -0700746 }
Scott Woodf6dc1572016-09-22 03:35:17 -0500747
Marc Zyngier00640302017-03-20 16:47:59 +0000748 arch_timer_check_ool_workaround(ate_match_local_cap_id, NULL);
Stephen Boyd22006992013-07-18 16:59:32 -0700749 } else {
Stephen Boyd7b52ad22014-01-06 14:56:17 -0800750 clk->features |= CLOCK_EVT_FEAT_DYNIRQ;
Stephen Boyd22006992013-07-18 16:59:32 -0700751 clk->name = "arch_mem_timer";
752 clk->rating = 400;
Sudeep Holla5e18e412018-07-09 16:45:36 +0100753 clk->cpumask = cpu_possible_mask;
Stephen Boyd22006992013-07-18 16:59:32 -0700754 if (arch_timer_mem_use_virtual) {
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530755 clk->set_state_shutdown = arch_timer_shutdown_virt_mem;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530756 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt_mem;
Stephen Boyd22006992013-07-18 16:59:32 -0700757 clk->set_next_event =
758 arch_timer_set_next_event_virt_mem;
759 } else {
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530760 clk->set_state_shutdown = arch_timer_shutdown_phys_mem;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530761 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys_mem;
Stephen Boyd22006992013-07-18 16:59:32 -0700762 clk->set_next_event =
763 arch_timer_set_next_event_phys_mem;
764 }
765 }
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000766
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530767 clk->set_state_shutdown(clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000768
Stephen Boyd22006992013-07-18 16:59:32 -0700769 clockevents_config_and_register(clk, arch_timer_rate, 0xf, 0x7fffffff);
770}
771
Nathan Lynche1ce5c72014-09-29 01:50:06 +0200772static void arch_timer_evtstrm_enable(int divider)
773{
774 u32 cntkctl = arch_timer_get_cntkctl();
775
776 cntkctl &= ~ARCH_TIMER_EVT_TRIGGER_MASK;
777 /* Set the divider and enable virtual event stream */
778 cntkctl |= (divider << ARCH_TIMER_EVT_TRIGGER_SHIFT)
779 | ARCH_TIMER_VIRT_EVT_EN;
780 arch_timer_set_cntkctl(cntkctl);
781 elf_hwcap |= HWCAP_EVTSTRM;
782#ifdef CONFIG_COMPAT
783 compat_elf_hwcap |= COMPAT_HWCAP_EVTSTRM;
784#endif
Julien Thierryec5c8e42017-10-13 14:32:55 +0100785 cpumask_set_cpu(smp_processor_id(), &evtstrm_available);
Nathan Lynche1ce5c72014-09-29 01:50:06 +0200786}
787
Will Deacon037f6372013-08-23 15:32:29 +0100788static void arch_timer_configure_evtstream(void)
789{
790 int evt_stream_div, pos;
791
792 /* Find the closest power of two to the divisor */
793 evt_stream_div = arch_timer_rate / ARCH_TIMER_EVT_STREAM_FREQ;
794 pos = fls(evt_stream_div);
795 if (pos > 1 && !(evt_stream_div & (1 << (pos - 2))))
796 pos--;
797 /* enable event stream */
798 arch_timer_evtstrm_enable(min(pos, 15));
799}
800
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200801static void arch_counter_set_user_access(void)
802{
803 u32 cntkctl = arch_timer_get_cntkctl();
804
Marc Zyngiera86bd132017-02-01 12:07:15 +0000805 /* Disable user access to the timers and both counters */
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200806 /* Also disable virtual event stream */
807 cntkctl &= ~(ARCH_TIMER_USR_PT_ACCESS_EN
808 | ARCH_TIMER_USR_VT_ACCESS_EN
Marc Zyngiera86bd132017-02-01 12:07:15 +0000809 | ARCH_TIMER_USR_VCT_ACCESS_EN
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200810 | ARCH_TIMER_VIRT_EVT_EN
811 | ARCH_TIMER_USR_PCT_ACCESS_EN);
812
Marc Zyngiera86bd132017-02-01 12:07:15 +0000813 /*
814 * Enable user access to the virtual counter if it doesn't
815 * need to be workaround. The vdso may have been already
816 * disabled though.
817 */
818 if (arch_timer_this_cpu_has_cntvct_wa())
819 pr_info("CPU%d: Trapping CNTVCT access\n", smp_processor_id());
820 else
821 cntkctl |= ARCH_TIMER_USR_VCT_ACCESS_EN;
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200822
823 arch_timer_set_cntkctl(cntkctl);
824}
825
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000826static bool arch_timer_has_nonsecure_ppi(void)
827{
Fu Weiee34f1e2017-01-18 21:25:27 +0800828 return (arch_timer_uses_ppi == ARCH_TIMER_PHYS_SECURE_PPI &&
829 arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000830}
831
Marc Zyngierf005bd72016-08-01 10:54:15 +0100832static u32 check_ppi_trigger(int irq)
833{
834 u32 flags = irq_get_trigger_type(irq);
835
836 if (flags != IRQF_TRIGGER_HIGH && flags != IRQF_TRIGGER_LOW) {
837 pr_warn("WARNING: Invalid trigger for IRQ%d, assuming level low\n", irq);
838 pr_warn("WARNING: Please fix your firmware\n");
839 flags = IRQF_TRIGGER_LOW;
840 }
841
842 return flags;
843}
844
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000845static int arch_timer_starting_cpu(unsigned int cpu)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000846{
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000847 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
Marc Zyngierf005bd72016-08-01 10:54:15 +0100848 u32 flags;
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000849
Fu Wei8a5c21d2017-01-18 21:25:26 +0800850 __arch_timer_setup(ARCH_TIMER_TYPE_CP15, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000851
Marc Zyngierf005bd72016-08-01 10:54:15 +0100852 flags = check_ppi_trigger(arch_timer_ppi[arch_timer_uses_ppi]);
853 enable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], flags);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000854
Marc Zyngierf005bd72016-08-01 10:54:15 +0100855 if (arch_timer_has_nonsecure_ppi()) {
Fu Weiee34f1e2017-01-18 21:25:27 +0800856 flags = check_ppi_trigger(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
857 enable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
858 flags);
Marc Zyngierf005bd72016-08-01 10:54:15 +0100859 }
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000860
861 arch_counter_set_user_access();
Will Deacon46fd5c62016-06-27 17:30:13 +0100862 if (evtstrm_enable)
Will Deacon037f6372013-08-23 15:32:29 +0100863 arch_timer_configure_evtstream();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000864
865 return 0;
866}
867
Fu Wei5d3dfa92017-03-22 00:31:13 +0800868/*
869 * For historical reasons, when probing with DT we use whichever (non-zero)
870 * rate was probed first, and don't verify that others match. If the first node
871 * probed has a clock-frequency property, this overrides the HW register.
872 */
873static void arch_timer_of_configure_rate(u32 rate, struct device_node *np)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000874{
Stephen Boyd22006992013-07-18 16:59:32 -0700875 /* Who has more than one independent system counter? */
876 if (arch_timer_rate)
877 return;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000878
Fu Wei5d3dfa92017-03-22 00:31:13 +0800879 if (of_property_read_u32(np, "clock-frequency", &arch_timer_rate))
880 arch_timer_rate = rate;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000881
Stephen Boyd22006992013-07-18 16:59:32 -0700882 /* Check the timer frequency. */
883 if (arch_timer_rate == 0)
Fu Weided24012017-01-18 21:25:25 +0800884 pr_warn("frequency not available\n");
Stephen Boyd22006992013-07-18 16:59:32 -0700885}
886
887static void arch_timer_banner(unsigned type)
888{
Fu Weided24012017-01-18 21:25:25 +0800889 pr_info("%s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
Fu Wei8a5c21d2017-01-18 21:25:26 +0800890 type & ARCH_TIMER_TYPE_CP15 ? "cp15" : "",
891 type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ?
892 " and " : "",
893 type & ARCH_TIMER_TYPE_MEM ? "mmio" : "",
Fu Weided24012017-01-18 21:25:25 +0800894 (unsigned long)arch_timer_rate / 1000000,
895 (unsigned long)(arch_timer_rate / 10000) % 100,
Fu Wei8a5c21d2017-01-18 21:25:26 +0800896 type & ARCH_TIMER_TYPE_CP15 ?
Fu Weiee34f1e2017-01-18 21:25:27 +0800897 (arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI) ? "virt" : "phys" :
Stephen Boyd22006992013-07-18 16:59:32 -0700898 "",
Fu Wei8a5c21d2017-01-18 21:25:26 +0800899 type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ? "/" : "",
900 type & ARCH_TIMER_TYPE_MEM ?
Stephen Boyd22006992013-07-18 16:59:32 -0700901 arch_timer_mem_use_virtual ? "virt" : "phys" :
902 "");
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000903}
904
905u32 arch_timer_get_rate(void)
906{
907 return arch_timer_rate;
908}
909
Julien Thierryec5c8e42017-10-13 14:32:55 +0100910bool arch_timer_evtstrm_available(void)
911{
912 /*
913 * We might get called from a preemptible context. This is fine
914 * because availability of the event stream should be always the same
915 * for a preemptible context and context where we might resume a task.
916 */
917 return cpumask_test_cpu(raw_smp_processor_id(), &evtstrm_available);
918}
919
Stephen Boyd22006992013-07-18 16:59:32 -0700920static u64 arch_counter_get_cntvct_mem(void)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000921{
Stephen Boyd22006992013-07-18 16:59:32 -0700922 u32 vct_lo, vct_hi, tmp_hi;
923
924 do {
925 vct_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
926 vct_lo = readl_relaxed(arch_counter_base + CNTVCT_LO);
927 tmp_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
928 } while (vct_hi != tmp_hi);
929
930 return ((u64) vct_hi << 32) | vct_lo;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000931}
932
Julien Grallb4d6ce92016-04-11 16:32:51 +0100933static struct arch_timer_kvm_info arch_timer_kvm_info;
934
935struct arch_timer_kvm_info *arch_timer_get_kvm_info(void)
936{
937 return &arch_timer_kvm_info;
938}
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000939
Stephen Boyd22006992013-07-18 16:59:32 -0700940static void __init arch_counter_register(unsigned type)
941{
942 u64 start_count;
943
944 /* Register the CP15 based counter if we have one */
Fu Wei8a5c21d2017-01-18 21:25:26 +0800945 if (type & ARCH_TIMER_TYPE_CP15) {
Christoffer Dalle6d68b002017-07-05 11:04:28 +0200946 if ((IS_ENABLED(CONFIG_ARM64) && !is_hyp_mode_available()) ||
Fu Weiee34f1e2017-01-18 21:25:27 +0800947 arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI)
Sonny Rao0b46b8a2014-11-23 23:02:44 -0800948 arch_timer_read_counter = arch_counter_get_cntvct;
949 else
950 arch_timer_read_counter = arch_counter_get_cntpct;
Scott Woodf6dc1572016-09-22 03:35:17 -0500951
Marc Zyngiera86bd132017-02-01 12:07:15 +0000952 clocksource_counter.archdata.vdso_direct = vdso_default;
Nathan Lynch423bd692014-09-29 01:50:06 +0200953 } else {
Stephen Boyd22006992013-07-18 16:59:32 -0700954 arch_timer_read_counter = arch_counter_get_cntvct_mem;
Nathan Lynch423bd692014-09-29 01:50:06 +0200955 }
956
Brian Norrisd8ec7592016-10-04 11:12:09 -0700957 if (!arch_counter_suspend_stop)
958 clocksource_counter.flags |= CLOCK_SOURCE_SUSPEND_NONSTOP;
Stephen Boyd22006992013-07-18 16:59:32 -0700959 start_count = arch_timer_read_counter();
960 clocksource_register_hz(&clocksource_counter, arch_timer_rate);
961 cyclecounter.mult = clocksource_counter.mult;
962 cyclecounter.shift = clocksource_counter.shift;
Julien Grallb4d6ce92016-04-11 16:32:51 +0100963 timecounter_init(&arch_timer_kvm_info.timecounter,
964 &cyclecounter, start_count);
Thierry Reding4a7d3e82013-10-15 15:31:51 +0200965
966 /* 56 bits minimum, so we assume worst case rollover */
967 sched_clock_register(arch_timer_read_counter, 56, arch_timer_rate);
Stephen Boyd22006992013-07-18 16:59:32 -0700968}
969
Paul Gortmaker8c37bb32013-06-19 11:32:08 -0400970static void arch_timer_stop(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000971{
Fu Weided24012017-01-18 21:25:25 +0800972 pr_debug("disable IRQ%d cpu #%d\n", clk->irq, smp_processor_id());
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000973
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000974 disable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi]);
975 if (arch_timer_has_nonsecure_ppi())
Fu Weiee34f1e2017-01-18 21:25:27 +0800976 disable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000977
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530978 clk->set_state_shutdown(clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000979}
980
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000981static int arch_timer_dying_cpu(unsigned int cpu)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000982{
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000983 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000984
Julien Thierryec5c8e42017-10-13 14:32:55 +0100985 cpumask_clear_cpu(smp_processor_id(), &evtstrm_available);
986
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000987 arch_timer_stop(clk);
988 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000989}
990
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100991#ifdef CONFIG_CPU_PM
Marc Zyngierbee67c52017-04-04 17:05:16 +0100992static DEFINE_PER_CPU(unsigned long, saved_cntkctl);
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100993static int arch_timer_cpu_pm_notify(struct notifier_block *self,
994 unsigned long action, void *hcpu)
995{
Julien Thierryec5c8e42017-10-13 14:32:55 +0100996 if (action == CPU_PM_ENTER) {
Marc Zyngierbee67c52017-04-04 17:05:16 +0100997 __this_cpu_write(saved_cntkctl, arch_timer_get_cntkctl());
Julien Thierryec5c8e42017-10-13 14:32:55 +0100998
999 cpumask_clear_cpu(smp_processor_id(), &evtstrm_available);
1000 } else if (action == CPU_PM_ENTER_FAILED || action == CPU_PM_EXIT) {
Marc Zyngierbee67c52017-04-04 17:05:16 +01001001 arch_timer_set_cntkctl(__this_cpu_read(saved_cntkctl));
Julien Thierryec5c8e42017-10-13 14:32:55 +01001002
1003 if (elf_hwcap & HWCAP_EVTSTRM)
1004 cpumask_set_cpu(smp_processor_id(), &evtstrm_available);
1005 }
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001006 return NOTIFY_OK;
1007}
1008
1009static struct notifier_block arch_timer_cpu_pm_notifier = {
1010 .notifier_call = arch_timer_cpu_pm_notify,
1011};
1012
1013static int __init arch_timer_cpu_pm_init(void)
1014{
1015 return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier);
1016}
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001017
1018static void __init arch_timer_cpu_pm_deinit(void)
1019{
1020 WARN_ON(cpu_pm_unregister_notifier(&arch_timer_cpu_pm_notifier));
1021}
1022
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001023#else
1024static int __init arch_timer_cpu_pm_init(void)
1025{
1026 return 0;
1027}
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001028
1029static void __init arch_timer_cpu_pm_deinit(void)
1030{
1031}
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001032#endif
1033
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001034static int __init arch_timer_register(void)
1035{
1036 int err;
1037 int ppi;
1038
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001039 arch_timer_evt = alloc_percpu(struct clock_event_device);
1040 if (!arch_timer_evt) {
1041 err = -ENOMEM;
1042 goto out;
1043 }
1044
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001045 ppi = arch_timer_ppi[arch_timer_uses_ppi];
1046 switch (arch_timer_uses_ppi) {
Fu Weiee34f1e2017-01-18 21:25:27 +08001047 case ARCH_TIMER_VIRT_PPI:
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001048 err = request_percpu_irq(ppi, arch_timer_handler_virt,
1049 "arch_timer", arch_timer_evt);
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001050 break;
Fu Weiee34f1e2017-01-18 21:25:27 +08001051 case ARCH_TIMER_PHYS_SECURE_PPI:
1052 case ARCH_TIMER_PHYS_NONSECURE_PPI:
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001053 err = request_percpu_irq(ppi, arch_timer_handler_phys,
1054 "arch_timer", arch_timer_evt);
Fu Wei4502b6b2017-01-18 21:25:30 +08001055 if (!err && arch_timer_has_nonsecure_ppi()) {
Fu Weiee34f1e2017-01-18 21:25:27 +08001056 ppi = arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI];
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001057 err = request_percpu_irq(ppi, arch_timer_handler_phys,
1058 "arch_timer", arch_timer_evt);
1059 if (err)
Fu Weiee34f1e2017-01-18 21:25:27 +08001060 free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_SECURE_PPI],
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001061 arch_timer_evt);
1062 }
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001063 break;
Fu Weiee34f1e2017-01-18 21:25:27 +08001064 case ARCH_TIMER_HYP_PPI:
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001065 err = request_percpu_irq(ppi, arch_timer_handler_phys,
1066 "arch_timer", arch_timer_evt);
1067 break;
1068 default:
1069 BUG();
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001070 }
1071
1072 if (err) {
Fu Weided24012017-01-18 21:25:25 +08001073 pr_err("can't register interrupt %d (%d)\n", ppi, err);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001074 goto out_free;
1075 }
1076
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001077 err = arch_timer_cpu_pm_init();
1078 if (err)
1079 goto out_unreg_notify;
1080
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001081 /* Register and immediately configure the timer on the boot CPU */
1082 err = cpuhp_setup_state(CPUHP_AP_ARM_ARCH_TIMER_STARTING,
Thomas Gleixner73c1b412016-12-21 20:19:54 +01001083 "clockevents/arm/arch_timer:starting",
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001084 arch_timer_starting_cpu, arch_timer_dying_cpu);
1085 if (err)
1086 goto out_unreg_cpupm;
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001087 return 0;
1088
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001089out_unreg_cpupm:
1090 arch_timer_cpu_pm_deinit();
1091
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001092out_unreg_notify:
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001093 free_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], arch_timer_evt);
1094 if (arch_timer_has_nonsecure_ppi())
Fu Weiee34f1e2017-01-18 21:25:27 +08001095 free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001096 arch_timer_evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001097
1098out_free:
1099 free_percpu(arch_timer_evt);
1100out:
1101 return err;
1102}
1103
Stephen Boyd22006992013-07-18 16:59:32 -07001104static int __init arch_timer_mem_register(void __iomem *base, unsigned int irq)
1105{
1106 int ret;
1107 irq_handler_t func;
1108 struct arch_timer *t;
1109
1110 t = kzalloc(sizeof(*t), GFP_KERNEL);
1111 if (!t)
1112 return -ENOMEM;
1113
1114 t->base = base;
1115 t->evt.irq = irq;
Fu Wei8a5c21d2017-01-18 21:25:26 +08001116 __arch_timer_setup(ARCH_TIMER_TYPE_MEM, &t->evt);
Stephen Boyd22006992013-07-18 16:59:32 -07001117
1118 if (arch_timer_mem_use_virtual)
1119 func = arch_timer_handler_virt_mem;
1120 else
1121 func = arch_timer_handler_phys_mem;
1122
1123 ret = request_irq(irq, func, IRQF_TIMER, "arch_mem_timer", &t->evt);
1124 if (ret) {
Fu Weided24012017-01-18 21:25:25 +08001125 pr_err("Failed to request mem timer irq\n");
Stephen Boyd22006992013-07-18 16:59:32 -07001126 kfree(t);
1127 }
1128
1129 return ret;
1130}
1131
1132static const struct of_device_id arch_timer_of_match[] __initconst = {
1133 { .compatible = "arm,armv7-timer", },
1134 { .compatible = "arm,armv8-timer", },
1135 {},
1136};
1137
1138static const struct of_device_id arch_timer_mem_of_match[] __initconst = {
1139 { .compatible = "arm,armv7-timer-mem", },
1140 {},
1141};
1142
Fu Wei13bf6992017-03-22 00:31:14 +08001143static bool __init arch_timer_needs_of_probing(void)
Sudeep Hollac387f072014-09-29 01:50:05 +02001144{
1145 struct device_node *dn;
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001146 bool needs_probing = false;
Fu Wei13bf6992017-03-22 00:31:14 +08001147 unsigned int mask = ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM;
Sudeep Hollac387f072014-09-29 01:50:05 +02001148
Fu Wei13bf6992017-03-22 00:31:14 +08001149 /* We have two timers, and both device-tree nodes are probed. */
1150 if ((arch_timers_present & mask) == mask)
1151 return false;
1152
1153 /*
1154 * Only one type of timer is probed,
1155 * check if we have another type of timer node in device-tree.
1156 */
1157 if (arch_timers_present & ARCH_TIMER_TYPE_CP15)
1158 dn = of_find_matching_node(NULL, arch_timer_mem_of_match);
1159 else
1160 dn = of_find_matching_node(NULL, arch_timer_of_match);
1161
1162 if (dn && of_device_is_available(dn))
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001163 needs_probing = true;
Fu Wei13bf6992017-03-22 00:31:14 +08001164
Sudeep Hollac387f072014-09-29 01:50:05 +02001165 of_node_put(dn);
1166
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001167 return needs_probing;
Sudeep Hollac387f072014-09-29 01:50:05 +02001168}
1169
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001170static int __init arch_timer_common_init(void)
Stephen Boyd22006992013-07-18 16:59:32 -07001171{
Stephen Boyd22006992013-07-18 16:59:32 -07001172 arch_timer_banner(arch_timers_present);
1173 arch_counter_register(arch_timers_present);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001174 return arch_timer_arch_init();
Stephen Boyd22006992013-07-18 16:59:32 -07001175}
1176
Fu Wei4502b6b2017-01-18 21:25:30 +08001177/**
1178 * arch_timer_select_ppi() - Select suitable PPI for the current system.
1179 *
1180 * If HYP mode is available, we know that the physical timer
1181 * has been configured to be accessible from PL1. Use it, so
1182 * that a guest can use the virtual timer instead.
1183 *
1184 * On ARMv8.1 with VH extensions, the kernel runs in HYP. VHE
1185 * accesses to CNTP_*_EL1 registers are silently redirected to
1186 * their CNTHP_*_EL2 counterparts, and use a different PPI
1187 * number.
1188 *
1189 * If no interrupt provided for virtual timer, we'll have to
1190 * stick to the physical timer. It'd better be accessible...
1191 * For arm64 we never use the secure interrupt.
1192 *
1193 * Return: a suitable PPI type for the current system.
1194 */
1195static enum arch_timer_ppi_nr __init arch_timer_select_ppi(void)
1196{
1197 if (is_kernel_in_hyp_mode())
1198 return ARCH_TIMER_HYP_PPI;
1199
1200 if (!is_hyp_mode_available() && arch_timer_ppi[ARCH_TIMER_VIRT_PPI])
1201 return ARCH_TIMER_VIRT_PPI;
1202
1203 if (IS_ENABLED(CONFIG_ARM64))
1204 return ARCH_TIMER_PHYS_NONSECURE_PPI;
1205
1206 return ARCH_TIMER_PHYS_SECURE_PPI;
1207}
1208
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001209static int __init arch_timer_of_init(struct device_node *np)
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001210{
Fu Weica0e1b52017-03-22 00:31:15 +08001211 int i, ret;
Fu Wei5d3dfa92017-03-22 00:31:13 +08001212 u32 rate;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001213
Fu Wei8a5c21d2017-01-18 21:25:26 +08001214 if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
Fu Weided24012017-01-18 21:25:25 +08001215 pr_warn("multiple nodes in dt, skipping\n");
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001216 return 0;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001217 }
1218
Fu Wei8a5c21d2017-01-18 21:25:26 +08001219 arch_timers_present |= ARCH_TIMER_TYPE_CP15;
Fu Weiee34f1e2017-01-18 21:25:27 +08001220 for (i = ARCH_TIMER_PHYS_SECURE_PPI; i < ARCH_TIMER_MAX_TIMER_PPI; i++)
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001221 arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
1222
Fu Weica0e1b52017-03-22 00:31:15 +08001223 arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI];
1224
Fu Weic389d702017-04-01 01:51:00 +08001225 rate = arch_timer_get_cntfrq();
Fu Wei5d3dfa92017-03-22 00:31:13 +08001226 arch_timer_of_configure_rate(rate, np);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001227
1228 arch_timer_c3stop = !of_property_read_bool(np, "always-on");
1229
Marc Zyngier651bb2e2017-01-19 17:20:59 +00001230 /* Check for globally applicable workarounds */
1231 arch_timer_check_ool_workaround(ate_match_dt, np);
Scott Woodf6dc1572016-09-22 03:35:17 -05001232
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001233 /*
1234 * If we cannot rely on firmware initializing the timer registers then
1235 * we should use the physical timers instead.
1236 */
1237 if (IS_ENABLED(CONFIG_ARM) &&
1238 of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
Fu Weiee34f1e2017-01-18 21:25:27 +08001239 arch_timer_uses_ppi = ARCH_TIMER_PHYS_SECURE_PPI;
Fu Wei4502b6b2017-01-18 21:25:30 +08001240 else
1241 arch_timer_uses_ppi = arch_timer_select_ppi();
1242
1243 if (!arch_timer_ppi[arch_timer_uses_ppi]) {
1244 pr_err("No interrupt available, giving up\n");
1245 return -EINVAL;
1246 }
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001247
Brian Norrisd8ec7592016-10-04 11:12:09 -07001248 /* On some systems, the counter stops ticking when in suspend. */
1249 arch_counter_suspend_stop = of_property_read_bool(np,
1250 "arm,no-tick-in-suspend");
1251
Fu Weica0e1b52017-03-22 00:31:15 +08001252 ret = arch_timer_register();
1253 if (ret)
1254 return ret;
1255
1256 if (arch_timer_needs_of_probing())
1257 return 0;
1258
1259 return arch_timer_common_init();
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001260}
Daniel Lezcano17273392017-05-26 16:56:11 +02001261TIMER_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_of_init);
1262TIMER_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_of_init);
Stephen Boyd22006992013-07-18 16:59:32 -07001263
Fu Weic389d702017-04-01 01:51:00 +08001264static u32 __init
1265arch_timer_mem_frame_get_cntfrq(struct arch_timer_mem_frame *frame)
Stephen Boyd22006992013-07-18 16:59:32 -07001266{
Fu Weic389d702017-04-01 01:51:00 +08001267 void __iomem *base;
1268 u32 rate;
Stephen Boyd22006992013-07-18 16:59:32 -07001269
Fu Weic389d702017-04-01 01:51:00 +08001270 base = ioremap(frame->cntbase, frame->size);
1271 if (!base) {
1272 pr_err("Unable to map frame @ %pa\n", &frame->cntbase);
1273 return 0;
1274 }
1275
Frank Rowand3db12002017-06-09 17:26:32 -07001276 rate = readl_relaxed(base + CNTFRQ);
Fu Weic389d702017-04-01 01:51:00 +08001277
Frank Rowand3db12002017-06-09 17:26:32 -07001278 iounmap(base);
Fu Weic389d702017-04-01 01:51:00 +08001279
1280 return rate;
1281}
1282
1283static struct arch_timer_mem_frame * __init
1284arch_timer_mem_find_best_frame(struct arch_timer_mem *timer_mem)
1285{
1286 struct arch_timer_mem_frame *frame, *best_frame = NULL;
1287 void __iomem *cntctlbase;
1288 u32 cnttidr;
1289 int i;
1290
1291 cntctlbase = ioremap(timer_mem->cntctlbase, timer_mem->size);
Stephen Boyd22006992013-07-18 16:59:32 -07001292 if (!cntctlbase) {
Fu Weic389d702017-04-01 01:51:00 +08001293 pr_err("Can't map CNTCTLBase @ %pa\n",
1294 &timer_mem->cntctlbase);
1295 return NULL;
Stephen Boyd22006992013-07-18 16:59:32 -07001296 }
1297
1298 cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
Stephen Boyd22006992013-07-18 16:59:32 -07001299
1300 /*
1301 * Try to find a virtual capable frame. Otherwise fall back to a
1302 * physical capable frame.
1303 */
Fu Weic389d702017-04-01 01:51:00 +08001304 for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
1305 u32 cntacr = CNTACR_RFRQ | CNTACR_RWPT | CNTACR_RPCT |
1306 CNTACR_RWVT | CNTACR_RVOFF | CNTACR_RVCT;
Stephen Boyd22006992013-07-18 16:59:32 -07001307
Fu Weic389d702017-04-01 01:51:00 +08001308 frame = &timer_mem->frame[i];
1309 if (!frame->valid)
1310 continue;
Stephen Boyd22006992013-07-18 16:59:32 -07001311
Robin Murphye392d602016-02-01 12:00:48 +00001312 /* Try enabling everything, and see what sticks */
Fu Weic389d702017-04-01 01:51:00 +08001313 writel_relaxed(cntacr, cntctlbase + CNTACR(i));
1314 cntacr = readl_relaxed(cntctlbase + CNTACR(i));
Robin Murphye392d602016-02-01 12:00:48 +00001315
Fu Weic389d702017-04-01 01:51:00 +08001316 if ((cnttidr & CNTTIDR_VIRT(i)) &&
Robin Murphye392d602016-02-01 12:00:48 +00001317 !(~cntacr & (CNTACR_RWVT | CNTACR_RVCT))) {
Stephen Boyd22006992013-07-18 16:59:32 -07001318 best_frame = frame;
1319 arch_timer_mem_use_virtual = true;
1320 break;
1321 }
Robin Murphye392d602016-02-01 12:00:48 +00001322
1323 if (~cntacr & (CNTACR_RWPT | CNTACR_RPCT))
1324 continue;
1325
Fu Weic389d702017-04-01 01:51:00 +08001326 best_frame = frame;
Stephen Boyd22006992013-07-18 16:59:32 -07001327 }
1328
Fu Weic389d702017-04-01 01:51:00 +08001329 iounmap(cntctlbase);
1330
Sudeep Hollaf63d9472017-05-08 13:32:27 +01001331 return best_frame;
Fu Weic389d702017-04-01 01:51:00 +08001332}
1333
1334static int __init
1335arch_timer_mem_frame_register(struct arch_timer_mem_frame *frame)
1336{
1337 void __iomem *base;
1338 int ret, irq = 0;
Stephen Boyd22006992013-07-18 16:59:32 -07001339
1340 if (arch_timer_mem_use_virtual)
Fu Weic389d702017-04-01 01:51:00 +08001341 irq = frame->virt_irq;
Stephen Boyd22006992013-07-18 16:59:32 -07001342 else
Fu Weic389d702017-04-01 01:51:00 +08001343 irq = frame->phys_irq;
Robin Murphye392d602016-02-01 12:00:48 +00001344
Stephen Boyd22006992013-07-18 16:59:32 -07001345 if (!irq) {
Fu Weided24012017-01-18 21:25:25 +08001346 pr_err("Frame missing %s irq.\n",
Thomas Gleixnercfb6d652013-08-21 14:59:23 +02001347 arch_timer_mem_use_virtual ? "virt" : "phys");
Fu Weic389d702017-04-01 01:51:00 +08001348 return -EINVAL;
1349 }
1350
1351 if (!request_mem_region(frame->cntbase, frame->size,
1352 "arch_mem_timer"))
1353 return -EBUSY;
1354
1355 base = ioremap(frame->cntbase, frame->size);
1356 if (!base) {
1357 pr_err("Can't map frame's registers\n");
1358 return -ENXIO;
1359 }
1360
1361 ret = arch_timer_mem_register(base, irq);
1362 if (ret) {
1363 iounmap(base);
1364 return ret;
1365 }
1366
1367 arch_counter_base = base;
1368 arch_timers_present |= ARCH_TIMER_TYPE_MEM;
1369
1370 return 0;
1371}
1372
1373static int __init arch_timer_mem_of_init(struct device_node *np)
1374{
1375 struct arch_timer_mem *timer_mem;
1376 struct arch_timer_mem_frame *frame;
1377 struct device_node *frame_node;
1378 struct resource res;
1379 int ret = -EINVAL;
1380 u32 rate;
1381
1382 timer_mem = kzalloc(sizeof(*timer_mem), GFP_KERNEL);
1383 if (!timer_mem)
1384 return -ENOMEM;
1385
1386 if (of_address_to_resource(np, 0, &res))
1387 goto out;
1388 timer_mem->cntctlbase = res.start;
1389 timer_mem->size = resource_size(&res);
1390
1391 for_each_available_child_of_node(np, frame_node) {
1392 u32 n;
1393 struct arch_timer_mem_frame *frame;
1394
1395 if (of_property_read_u32(frame_node, "frame-number", &n)) {
1396 pr_err(FW_BUG "Missing frame-number.\n");
1397 of_node_put(frame_node);
1398 goto out;
1399 }
1400 if (n >= ARCH_TIMER_MEM_MAX_FRAMES) {
1401 pr_err(FW_BUG "Wrong frame-number, only 0-%u are permitted.\n",
1402 ARCH_TIMER_MEM_MAX_FRAMES - 1);
1403 of_node_put(frame_node);
1404 goto out;
1405 }
1406 frame = &timer_mem->frame[n];
1407
1408 if (frame->valid) {
1409 pr_err(FW_BUG "Duplicated frame-number.\n");
1410 of_node_put(frame_node);
1411 goto out;
1412 }
1413
1414 if (of_address_to_resource(frame_node, 0, &res)) {
1415 of_node_put(frame_node);
1416 goto out;
1417 }
1418 frame->cntbase = res.start;
1419 frame->size = resource_size(&res);
1420
1421 frame->virt_irq = irq_of_parse_and_map(frame_node,
1422 ARCH_TIMER_VIRT_SPI);
1423 frame->phys_irq = irq_of_parse_and_map(frame_node,
1424 ARCH_TIMER_PHYS_SPI);
1425
1426 frame->valid = true;
1427 }
1428
1429 frame = arch_timer_mem_find_best_frame(timer_mem);
1430 if (!frame) {
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001431 pr_err("Unable to find a suitable frame in timer @ %pa\n",
1432 &timer_mem->cntctlbase);
Fu Weic389d702017-04-01 01:51:00 +08001433 ret = -EINVAL;
Robin Murphye392d602016-02-01 12:00:48 +00001434 goto out;
Stephen Boyd22006992013-07-18 16:59:32 -07001435 }
1436
Fu Weic389d702017-04-01 01:51:00 +08001437 rate = arch_timer_mem_frame_get_cntfrq(frame);
Fu Wei5d3dfa92017-03-22 00:31:13 +08001438 arch_timer_of_configure_rate(rate, np);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001439
Fu Weic389d702017-04-01 01:51:00 +08001440 ret = arch_timer_mem_frame_register(frame);
1441 if (!ret && !arch_timer_needs_of_probing())
Fu Weica0e1b52017-03-22 00:31:15 +08001442 ret = arch_timer_common_init();
Robin Murphye392d602016-02-01 12:00:48 +00001443out:
Fu Weic389d702017-04-01 01:51:00 +08001444 kfree(timer_mem);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001445 return ret;
Stephen Boyd22006992013-07-18 16:59:32 -07001446}
Daniel Lezcano17273392017-05-26 16:56:11 +02001447TIMER_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem",
Fu Weic389d702017-04-01 01:51:00 +08001448 arch_timer_mem_of_init);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001449
Fu Weif79d2092017-04-01 01:51:02 +08001450#ifdef CONFIG_ACPI_GTDT
Fu Weic2743a32017-04-01 01:51:04 +08001451static int __init
1452arch_timer_mem_verify_cntfrq(struct arch_timer_mem *timer_mem)
1453{
1454 struct arch_timer_mem_frame *frame;
1455 u32 rate;
1456 int i;
1457
1458 for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
1459 frame = &timer_mem->frame[i];
1460
1461 if (!frame->valid)
1462 continue;
1463
1464 rate = arch_timer_mem_frame_get_cntfrq(frame);
1465 if (rate == arch_timer_rate)
1466 continue;
1467
1468 pr_err(FW_BUG "CNTFRQ mismatch: frame @ %pa: (0x%08lx), CPU: (0x%08lx)\n",
1469 &frame->cntbase,
1470 (unsigned long)rate, (unsigned long)arch_timer_rate);
1471
1472 return -EINVAL;
1473 }
1474
1475 return 0;
1476}
1477
1478static int __init arch_timer_mem_acpi_init(int platform_timer_count)
1479{
1480 struct arch_timer_mem *timers, *timer;
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001481 struct arch_timer_mem_frame *frame, *best_frame = NULL;
Fu Weic2743a32017-04-01 01:51:04 +08001482 int timer_count, i, ret = 0;
1483
1484 timers = kcalloc(platform_timer_count, sizeof(*timers),
1485 GFP_KERNEL);
1486 if (!timers)
1487 return -ENOMEM;
1488
1489 ret = acpi_arch_timer_mem_init(timers, &timer_count);
1490 if (ret || !timer_count)
1491 goto out;
1492
Fu Weic2743a32017-04-01 01:51:04 +08001493 /*
1494 * While unlikely, it's theoretically possible that none of the frames
1495 * in a timer expose the combination of feature we want.
1496 */
Matthias Kaehlcked197f792017-07-31 11:37:28 -07001497 for (i = 0; i < timer_count; i++) {
Fu Weic2743a32017-04-01 01:51:04 +08001498 timer = &timers[i];
1499
1500 frame = arch_timer_mem_find_best_frame(timer);
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001501 if (!best_frame)
1502 best_frame = frame;
1503
1504 ret = arch_timer_mem_verify_cntfrq(timer);
1505 if (ret) {
1506 pr_err("Disabling MMIO timers due to CNTFRQ mismatch\n");
1507 goto out;
1508 }
1509
1510 if (!best_frame) /* implies !frame */
1511 /*
1512 * Only complain about missing suitable frames if we
1513 * haven't already found one in a previous iteration.
1514 */
1515 pr_err("Unable to find a suitable frame in timer @ %pa\n",
1516 &timer->cntctlbase);
Fu Weic2743a32017-04-01 01:51:04 +08001517 }
1518
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001519 if (best_frame)
1520 ret = arch_timer_mem_frame_register(best_frame);
Fu Weic2743a32017-04-01 01:51:04 +08001521out:
1522 kfree(timers);
1523 return ret;
1524}
1525
1526/* Initialize per-processor generic timer and memory-mapped timer(if present) */
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001527static int __init arch_timer_acpi_init(struct acpi_table_header *table)
1528{
Fu Weic2743a32017-04-01 01:51:04 +08001529 int ret, platform_timer_count;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001530
Fu Wei8a5c21d2017-01-18 21:25:26 +08001531 if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
Fu Weided24012017-01-18 21:25:25 +08001532 pr_warn("already initialized, skipping\n");
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001533 return -EINVAL;
1534 }
1535
Fu Wei8a5c21d2017-01-18 21:25:26 +08001536 arch_timers_present |= ARCH_TIMER_TYPE_CP15;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001537
Fu Weic2743a32017-04-01 01:51:04 +08001538 ret = acpi_gtdt_init(table, &platform_timer_count);
Fu Weif79d2092017-04-01 01:51:02 +08001539 if (ret) {
1540 pr_err("Failed to init GTDT table.\n");
1541 return ret;
1542 }
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001543
Fu Weiee34f1e2017-01-18 21:25:27 +08001544 arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI] =
Fu Weif79d2092017-04-01 01:51:02 +08001545 acpi_gtdt_map_ppi(ARCH_TIMER_PHYS_NONSECURE_PPI);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001546
Fu Weiee34f1e2017-01-18 21:25:27 +08001547 arch_timer_ppi[ARCH_TIMER_VIRT_PPI] =
Fu Weif79d2092017-04-01 01:51:02 +08001548 acpi_gtdt_map_ppi(ARCH_TIMER_VIRT_PPI);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001549
Fu Weiee34f1e2017-01-18 21:25:27 +08001550 arch_timer_ppi[ARCH_TIMER_HYP_PPI] =
Fu Weif79d2092017-04-01 01:51:02 +08001551 acpi_gtdt_map_ppi(ARCH_TIMER_HYP_PPI);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001552
Fu Weica0e1b52017-03-22 00:31:15 +08001553 arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI];
1554
Fu Wei5d3dfa92017-03-22 00:31:13 +08001555 /*
1556 * When probing via ACPI, we have no mechanism to override the sysreg
1557 * CNTFRQ value. This *must* be correct.
1558 */
1559 arch_timer_rate = arch_timer_get_cntfrq();
1560 if (!arch_timer_rate) {
1561 pr_err(FW_BUG "frequency not available.\n");
1562 return -EINVAL;
1563 }
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001564
Fu Wei4502b6b2017-01-18 21:25:30 +08001565 arch_timer_uses_ppi = arch_timer_select_ppi();
1566 if (!arch_timer_ppi[arch_timer_uses_ppi]) {
1567 pr_err("No interrupt available, giving up\n");
1568 return -EINVAL;
1569 }
1570
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001571 /* Always-on capability */
Fu Weif79d2092017-04-01 01:51:02 +08001572 arch_timer_c3stop = acpi_gtdt_c3stop(arch_timer_uses_ppi);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001573
Marc Zyngier5a38bca2017-02-21 14:37:30 +00001574 /* Check for globally applicable workarounds */
1575 arch_timer_check_ool_workaround(ate_match_acpi_oem_info, table);
1576
Fu Weica0e1b52017-03-22 00:31:15 +08001577 ret = arch_timer_register();
1578 if (ret)
1579 return ret;
1580
Fu Weic2743a32017-04-01 01:51:04 +08001581 if (platform_timer_count &&
1582 arch_timer_mem_acpi_init(platform_timer_count))
1583 pr_err("Failed to initialize memory-mapped timer.\n");
1584
Fu Weica0e1b52017-03-22 00:31:15 +08001585 return arch_timer_common_init();
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001586}
Daniel Lezcano77d62f52017-05-26 17:42:25 +02001587TIMER_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001588#endif