blob: a8b20b65bd4b77ab09473aaa81057975aa0914a7 [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);
836 elf_hwcap |= HWCAP_EVTSTRM;
837#ifdef CONFIG_COMPAT
838 compat_elf_hwcap |= COMPAT_HWCAP_EVTSTRM;
839#endif
Julien Thierryec5c8e42017-10-13 14:32:55 +0100840 cpumask_set_cpu(smp_processor_id(), &evtstrm_available);
Nathan Lynche1ce5c72014-09-29 01:50:06 +0200841}
842
Will Deacon037f6372013-08-23 15:32:29 +0100843static void arch_timer_configure_evtstream(void)
844{
845 int evt_stream_div, pos;
846
847 /* Find the closest power of two to the divisor */
848 evt_stream_div = arch_timer_rate / ARCH_TIMER_EVT_STREAM_FREQ;
849 pos = fls(evt_stream_div);
850 if (pos > 1 && !(evt_stream_div & (1 << (pos - 2))))
851 pos--;
852 /* enable event stream */
853 arch_timer_evtstrm_enable(min(pos, 15));
854}
855
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200856static void arch_counter_set_user_access(void)
857{
858 u32 cntkctl = arch_timer_get_cntkctl();
859
Marc Zyngiera86bd132017-02-01 12:07:15 +0000860 /* Disable user access to the timers and both counters */
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200861 /* Also disable virtual event stream */
862 cntkctl &= ~(ARCH_TIMER_USR_PT_ACCESS_EN
863 | ARCH_TIMER_USR_VT_ACCESS_EN
Marc Zyngiera86bd132017-02-01 12:07:15 +0000864 | ARCH_TIMER_USR_VCT_ACCESS_EN
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200865 | ARCH_TIMER_VIRT_EVT_EN
866 | ARCH_TIMER_USR_PCT_ACCESS_EN);
867
Marc Zyngiera86bd132017-02-01 12:07:15 +0000868 /*
869 * Enable user access to the virtual counter if it doesn't
870 * need to be workaround. The vdso may have been already
871 * disabled though.
872 */
873 if (arch_timer_this_cpu_has_cntvct_wa())
874 pr_info("CPU%d: Trapping CNTVCT access\n", smp_processor_id());
875 else
876 cntkctl |= ARCH_TIMER_USR_VCT_ACCESS_EN;
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200877
878 arch_timer_set_cntkctl(cntkctl);
879}
880
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000881static bool arch_timer_has_nonsecure_ppi(void)
882{
Fu Weiee34f1e2017-01-18 21:25:27 +0800883 return (arch_timer_uses_ppi == ARCH_TIMER_PHYS_SECURE_PPI &&
884 arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000885}
886
Marc Zyngierf005bd72016-08-01 10:54:15 +0100887static u32 check_ppi_trigger(int irq)
888{
889 u32 flags = irq_get_trigger_type(irq);
890
891 if (flags != IRQF_TRIGGER_HIGH && flags != IRQF_TRIGGER_LOW) {
892 pr_warn("WARNING: Invalid trigger for IRQ%d, assuming level low\n", irq);
893 pr_warn("WARNING: Please fix your firmware\n");
894 flags = IRQF_TRIGGER_LOW;
895 }
896
897 return flags;
898}
899
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000900static int arch_timer_starting_cpu(unsigned int cpu)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000901{
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000902 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
Marc Zyngierf005bd72016-08-01 10:54:15 +0100903 u32 flags;
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000904
Fu Wei8a5c21d2017-01-18 21:25:26 +0800905 __arch_timer_setup(ARCH_TIMER_TYPE_CP15, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000906
Marc Zyngierf005bd72016-08-01 10:54:15 +0100907 flags = check_ppi_trigger(arch_timer_ppi[arch_timer_uses_ppi]);
908 enable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], flags);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000909
Marc Zyngierf005bd72016-08-01 10:54:15 +0100910 if (arch_timer_has_nonsecure_ppi()) {
Fu Weiee34f1e2017-01-18 21:25:27 +0800911 flags = check_ppi_trigger(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
912 enable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
913 flags);
Marc Zyngierf005bd72016-08-01 10:54:15 +0100914 }
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000915
916 arch_counter_set_user_access();
Will Deacon46fd5c62016-06-27 17:30:13 +0100917 if (evtstrm_enable)
Will Deacon037f6372013-08-23 15:32:29 +0100918 arch_timer_configure_evtstream();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000919
920 return 0;
921}
922
Fu Wei5d3dfa92017-03-22 00:31:13 +0800923/*
924 * For historical reasons, when probing with DT we use whichever (non-zero)
925 * rate was probed first, and don't verify that others match. If the first node
926 * probed has a clock-frequency property, this overrides the HW register.
927 */
928static void arch_timer_of_configure_rate(u32 rate, struct device_node *np)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000929{
Stephen Boyd22006992013-07-18 16:59:32 -0700930 /* Who has more than one independent system counter? */
931 if (arch_timer_rate)
932 return;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000933
Fu Wei5d3dfa92017-03-22 00:31:13 +0800934 if (of_property_read_u32(np, "clock-frequency", &arch_timer_rate))
935 arch_timer_rate = rate;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000936
Stephen Boyd22006992013-07-18 16:59:32 -0700937 /* Check the timer frequency. */
938 if (arch_timer_rate == 0)
Fu Weided24012017-01-18 21:25:25 +0800939 pr_warn("frequency not available\n");
Stephen Boyd22006992013-07-18 16:59:32 -0700940}
941
942static void arch_timer_banner(unsigned type)
943{
Fu Weided24012017-01-18 21:25:25 +0800944 pr_info("%s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
Fu Wei8a5c21d2017-01-18 21:25:26 +0800945 type & ARCH_TIMER_TYPE_CP15 ? "cp15" : "",
946 type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ?
947 " and " : "",
948 type & ARCH_TIMER_TYPE_MEM ? "mmio" : "",
Fu Weided24012017-01-18 21:25:25 +0800949 (unsigned long)arch_timer_rate / 1000000,
950 (unsigned long)(arch_timer_rate / 10000) % 100,
Fu Wei8a5c21d2017-01-18 21:25:26 +0800951 type & ARCH_TIMER_TYPE_CP15 ?
Fu Weiee34f1e2017-01-18 21:25:27 +0800952 (arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI) ? "virt" : "phys" :
Stephen Boyd22006992013-07-18 16:59:32 -0700953 "",
Fu Wei8a5c21d2017-01-18 21:25:26 +0800954 type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ? "/" : "",
955 type & ARCH_TIMER_TYPE_MEM ?
Stephen Boyd22006992013-07-18 16:59:32 -0700956 arch_timer_mem_use_virtual ? "virt" : "phys" :
957 "");
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000958}
959
960u32 arch_timer_get_rate(void)
961{
962 return arch_timer_rate;
963}
964
Julien Thierryec5c8e42017-10-13 14:32:55 +0100965bool arch_timer_evtstrm_available(void)
966{
967 /*
968 * We might get called from a preemptible context. This is fine
969 * because availability of the event stream should be always the same
970 * for a preemptible context and context where we might resume a task.
971 */
972 return cpumask_test_cpu(raw_smp_processor_id(), &evtstrm_available);
973}
974
Stephen Boyd22006992013-07-18 16:59:32 -0700975static u64 arch_counter_get_cntvct_mem(void)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000976{
Stephen Boyd22006992013-07-18 16:59:32 -0700977 u32 vct_lo, vct_hi, tmp_hi;
978
979 do {
980 vct_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
981 vct_lo = readl_relaxed(arch_counter_base + CNTVCT_LO);
982 tmp_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
983 } while (vct_hi != tmp_hi);
984
985 return ((u64) vct_hi << 32) | vct_lo;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000986}
987
Julien Grallb4d6ce92016-04-11 16:32:51 +0100988static struct arch_timer_kvm_info arch_timer_kvm_info;
989
990struct arch_timer_kvm_info *arch_timer_get_kvm_info(void)
991{
992 return &arch_timer_kvm_info;
993}
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000994
Stephen Boyd22006992013-07-18 16:59:32 -0700995static void __init arch_counter_register(unsigned type)
996{
997 u64 start_count;
998
999 /* Register the CP15 based counter if we have one */
Fu Wei8a5c21d2017-01-18 21:25:26 +08001000 if (type & ARCH_TIMER_TYPE_CP15) {
Christoffer Dalle6d68b002017-07-05 11:04:28 +02001001 if ((IS_ENABLED(CONFIG_ARM64) && !is_hyp_mode_available()) ||
Fu Weiee34f1e2017-01-18 21:25:27 +08001002 arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI)
Sonny Rao0b46b8a2014-11-23 23:02:44 -08001003 arch_timer_read_counter = arch_counter_get_cntvct;
1004 else
1005 arch_timer_read_counter = arch_counter_get_cntpct;
Scott Woodf6dc1572016-09-22 03:35:17 -05001006
Marc Zyngiera86bd132017-02-01 12:07:15 +00001007 clocksource_counter.archdata.vdso_direct = vdso_default;
Nathan Lynch423bd692014-09-29 01:50:06 +02001008 } else {
Stephen Boyd22006992013-07-18 16:59:32 -07001009 arch_timer_read_counter = arch_counter_get_cntvct_mem;
Nathan Lynch423bd692014-09-29 01:50:06 +02001010 }
1011
Brian Norrisd8ec7592016-10-04 11:12:09 -07001012 if (!arch_counter_suspend_stop)
1013 clocksource_counter.flags |= CLOCK_SOURCE_SUSPEND_NONSTOP;
Stephen Boyd22006992013-07-18 16:59:32 -07001014 start_count = arch_timer_read_counter();
1015 clocksource_register_hz(&clocksource_counter, arch_timer_rate);
1016 cyclecounter.mult = clocksource_counter.mult;
1017 cyclecounter.shift = clocksource_counter.shift;
Julien Grallb4d6ce92016-04-11 16:32:51 +01001018 timecounter_init(&arch_timer_kvm_info.timecounter,
1019 &cyclecounter, start_count);
Thierry Reding4a7d3e82013-10-15 15:31:51 +02001020
1021 /* 56 bits minimum, so we assume worst case rollover */
1022 sched_clock_register(arch_timer_read_counter, 56, arch_timer_rate);
Stephen Boyd22006992013-07-18 16:59:32 -07001023}
1024
Paul Gortmaker8c37bb32013-06-19 11:32:08 -04001025static void arch_timer_stop(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001026{
Fu Weided24012017-01-18 21:25:25 +08001027 pr_debug("disable IRQ%d cpu #%d\n", clk->irq, smp_processor_id());
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001028
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001029 disable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi]);
1030 if (arch_timer_has_nonsecure_ppi())
Fu Weiee34f1e2017-01-18 21:25:27 +08001031 disable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001032
Viresh Kumar46c5bfd2015-06-12 13:30:12 +05301033 clk->set_state_shutdown(clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001034}
1035
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001036static int arch_timer_dying_cpu(unsigned int cpu)
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001037{
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001038 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001039
Julien Thierryec5c8e42017-10-13 14:32:55 +01001040 cpumask_clear_cpu(smp_processor_id(), &evtstrm_available);
1041
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001042 arch_timer_stop(clk);
1043 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001044}
1045
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001046#ifdef CONFIG_CPU_PM
Marc Zyngierbee67c52017-04-04 17:05:16 +01001047static DEFINE_PER_CPU(unsigned long, saved_cntkctl);
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001048static int arch_timer_cpu_pm_notify(struct notifier_block *self,
1049 unsigned long action, void *hcpu)
1050{
Julien Thierryec5c8e42017-10-13 14:32:55 +01001051 if (action == CPU_PM_ENTER) {
Marc Zyngierbee67c52017-04-04 17:05:16 +01001052 __this_cpu_write(saved_cntkctl, arch_timer_get_cntkctl());
Julien Thierryec5c8e42017-10-13 14:32:55 +01001053
1054 cpumask_clear_cpu(smp_processor_id(), &evtstrm_available);
1055 } else if (action == CPU_PM_ENTER_FAILED || action == CPU_PM_EXIT) {
Marc Zyngierbee67c52017-04-04 17:05:16 +01001056 arch_timer_set_cntkctl(__this_cpu_read(saved_cntkctl));
Julien Thierryec5c8e42017-10-13 14:32:55 +01001057
1058 if (elf_hwcap & HWCAP_EVTSTRM)
1059 cpumask_set_cpu(smp_processor_id(), &evtstrm_available);
1060 }
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001061 return NOTIFY_OK;
1062}
1063
1064static struct notifier_block arch_timer_cpu_pm_notifier = {
1065 .notifier_call = arch_timer_cpu_pm_notify,
1066};
1067
1068static int __init arch_timer_cpu_pm_init(void)
1069{
1070 return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier);
1071}
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001072
1073static void __init arch_timer_cpu_pm_deinit(void)
1074{
1075 WARN_ON(cpu_pm_unregister_notifier(&arch_timer_cpu_pm_notifier));
1076}
1077
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001078#else
1079static int __init arch_timer_cpu_pm_init(void)
1080{
1081 return 0;
1082}
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001083
1084static void __init arch_timer_cpu_pm_deinit(void)
1085{
1086}
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001087#endif
1088
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001089static int __init arch_timer_register(void)
1090{
1091 int err;
1092 int ppi;
1093
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001094 arch_timer_evt = alloc_percpu(struct clock_event_device);
1095 if (!arch_timer_evt) {
1096 err = -ENOMEM;
1097 goto out;
1098 }
1099
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001100 ppi = arch_timer_ppi[arch_timer_uses_ppi];
1101 switch (arch_timer_uses_ppi) {
Fu Weiee34f1e2017-01-18 21:25:27 +08001102 case ARCH_TIMER_VIRT_PPI:
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001103 err = request_percpu_irq(ppi, arch_timer_handler_virt,
1104 "arch_timer", arch_timer_evt);
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001105 break;
Fu Weiee34f1e2017-01-18 21:25:27 +08001106 case ARCH_TIMER_PHYS_SECURE_PPI:
1107 case ARCH_TIMER_PHYS_NONSECURE_PPI:
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001108 err = request_percpu_irq(ppi, arch_timer_handler_phys,
1109 "arch_timer", arch_timer_evt);
Fu Wei4502b6b2017-01-18 21:25:30 +08001110 if (!err && arch_timer_has_nonsecure_ppi()) {
Fu Weiee34f1e2017-01-18 21:25:27 +08001111 ppi = arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI];
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001112 err = request_percpu_irq(ppi, arch_timer_handler_phys,
1113 "arch_timer", arch_timer_evt);
1114 if (err)
Fu Weiee34f1e2017-01-18 21:25:27 +08001115 free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_SECURE_PPI],
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001116 arch_timer_evt);
1117 }
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001118 break;
Fu Weiee34f1e2017-01-18 21:25:27 +08001119 case ARCH_TIMER_HYP_PPI:
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001120 err = request_percpu_irq(ppi, arch_timer_handler_phys,
1121 "arch_timer", arch_timer_evt);
1122 break;
1123 default:
1124 BUG();
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001125 }
1126
1127 if (err) {
Fu Weided24012017-01-18 21:25:25 +08001128 pr_err("can't register interrupt %d (%d)\n", ppi, err);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001129 goto out_free;
1130 }
1131
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001132 err = arch_timer_cpu_pm_init();
1133 if (err)
1134 goto out_unreg_notify;
1135
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001136 /* Register and immediately configure the timer on the boot CPU */
1137 err = cpuhp_setup_state(CPUHP_AP_ARM_ARCH_TIMER_STARTING,
Thomas Gleixner73c1b412016-12-21 20:19:54 +01001138 "clockevents/arm/arch_timer:starting",
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001139 arch_timer_starting_cpu, arch_timer_dying_cpu);
1140 if (err)
1141 goto out_unreg_cpupm;
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001142 return 0;
1143
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001144out_unreg_cpupm:
1145 arch_timer_cpu_pm_deinit();
1146
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001147out_unreg_notify:
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001148 free_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], arch_timer_evt);
1149 if (arch_timer_has_nonsecure_ppi())
Fu Weiee34f1e2017-01-18 21:25:27 +08001150 free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001151 arch_timer_evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001152
1153out_free:
1154 free_percpu(arch_timer_evt);
1155out:
1156 return err;
1157}
1158
Stephen Boyd22006992013-07-18 16:59:32 -07001159static int __init arch_timer_mem_register(void __iomem *base, unsigned int irq)
1160{
1161 int ret;
1162 irq_handler_t func;
1163 struct arch_timer *t;
1164
1165 t = kzalloc(sizeof(*t), GFP_KERNEL);
1166 if (!t)
1167 return -ENOMEM;
1168
1169 t->base = base;
1170 t->evt.irq = irq;
Fu Wei8a5c21d2017-01-18 21:25:26 +08001171 __arch_timer_setup(ARCH_TIMER_TYPE_MEM, &t->evt);
Stephen Boyd22006992013-07-18 16:59:32 -07001172
1173 if (arch_timer_mem_use_virtual)
1174 func = arch_timer_handler_virt_mem;
1175 else
1176 func = arch_timer_handler_phys_mem;
1177
1178 ret = request_irq(irq, func, IRQF_TIMER, "arch_mem_timer", &t->evt);
1179 if (ret) {
Fu Weided24012017-01-18 21:25:25 +08001180 pr_err("Failed to request mem timer irq\n");
Stephen Boyd22006992013-07-18 16:59:32 -07001181 kfree(t);
1182 }
1183
1184 return ret;
1185}
1186
1187static const struct of_device_id arch_timer_of_match[] __initconst = {
1188 { .compatible = "arm,armv7-timer", },
1189 { .compatible = "arm,armv8-timer", },
1190 {},
1191};
1192
1193static const struct of_device_id arch_timer_mem_of_match[] __initconst = {
1194 { .compatible = "arm,armv7-timer-mem", },
1195 {},
1196};
1197
Fu Wei13bf6992017-03-22 00:31:14 +08001198static bool __init arch_timer_needs_of_probing(void)
Sudeep Hollac387f072014-09-29 01:50:05 +02001199{
1200 struct device_node *dn;
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001201 bool needs_probing = false;
Fu Wei13bf6992017-03-22 00:31:14 +08001202 unsigned int mask = ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM;
Sudeep Hollac387f072014-09-29 01:50:05 +02001203
Fu Wei13bf6992017-03-22 00:31:14 +08001204 /* We have two timers, and both device-tree nodes are probed. */
1205 if ((arch_timers_present & mask) == mask)
1206 return false;
1207
1208 /*
1209 * Only one type of timer is probed,
1210 * check if we have another type of timer node in device-tree.
1211 */
1212 if (arch_timers_present & ARCH_TIMER_TYPE_CP15)
1213 dn = of_find_matching_node(NULL, arch_timer_mem_of_match);
1214 else
1215 dn = of_find_matching_node(NULL, arch_timer_of_match);
1216
1217 if (dn && of_device_is_available(dn))
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001218 needs_probing = true;
Fu Wei13bf6992017-03-22 00:31:14 +08001219
Sudeep Hollac387f072014-09-29 01:50:05 +02001220 of_node_put(dn);
1221
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001222 return needs_probing;
Sudeep Hollac387f072014-09-29 01:50:05 +02001223}
1224
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001225static int __init arch_timer_common_init(void)
Stephen Boyd22006992013-07-18 16:59:32 -07001226{
Stephen Boyd22006992013-07-18 16:59:32 -07001227 arch_timer_banner(arch_timers_present);
1228 arch_counter_register(arch_timers_present);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001229 return arch_timer_arch_init();
Stephen Boyd22006992013-07-18 16:59:32 -07001230}
1231
Fu Wei4502b6b2017-01-18 21:25:30 +08001232/**
1233 * arch_timer_select_ppi() - Select suitable PPI for the current system.
1234 *
1235 * If HYP mode is available, we know that the physical timer
1236 * has been configured to be accessible from PL1. Use it, so
1237 * that a guest can use the virtual timer instead.
1238 *
1239 * On ARMv8.1 with VH extensions, the kernel runs in HYP. VHE
1240 * accesses to CNTP_*_EL1 registers are silently redirected to
1241 * their CNTHP_*_EL2 counterparts, and use a different PPI
1242 * number.
1243 *
1244 * If no interrupt provided for virtual timer, we'll have to
1245 * stick to the physical timer. It'd better be accessible...
1246 * For arm64 we never use the secure interrupt.
1247 *
1248 * Return: a suitable PPI type for the current system.
1249 */
1250static enum arch_timer_ppi_nr __init arch_timer_select_ppi(void)
1251{
1252 if (is_kernel_in_hyp_mode())
1253 return ARCH_TIMER_HYP_PPI;
1254
1255 if (!is_hyp_mode_available() && arch_timer_ppi[ARCH_TIMER_VIRT_PPI])
1256 return ARCH_TIMER_VIRT_PPI;
1257
1258 if (IS_ENABLED(CONFIG_ARM64))
1259 return ARCH_TIMER_PHYS_NONSECURE_PPI;
1260
1261 return ARCH_TIMER_PHYS_SECURE_PPI;
1262}
1263
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001264static int __init arch_timer_of_init(struct device_node *np)
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001265{
Fu Weica0e1b52017-03-22 00:31:15 +08001266 int i, ret;
Fu Wei5d3dfa92017-03-22 00:31:13 +08001267 u32 rate;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001268
Fu Wei8a5c21d2017-01-18 21:25:26 +08001269 if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
Fu Weided24012017-01-18 21:25:25 +08001270 pr_warn("multiple nodes in dt, skipping\n");
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001271 return 0;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001272 }
1273
Fu Wei8a5c21d2017-01-18 21:25:26 +08001274 arch_timers_present |= ARCH_TIMER_TYPE_CP15;
Fu Weiee34f1e2017-01-18 21:25:27 +08001275 for (i = ARCH_TIMER_PHYS_SECURE_PPI; i < ARCH_TIMER_MAX_TIMER_PPI; i++)
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001276 arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
1277
Fu Weica0e1b52017-03-22 00:31:15 +08001278 arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI];
1279
Fu Weic389d702017-04-01 01:51:00 +08001280 rate = arch_timer_get_cntfrq();
Fu Wei5d3dfa92017-03-22 00:31:13 +08001281 arch_timer_of_configure_rate(rate, np);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001282
1283 arch_timer_c3stop = !of_property_read_bool(np, "always-on");
1284
Marc Zyngier651bb2e2017-01-19 17:20:59 +00001285 /* Check for globally applicable workarounds */
1286 arch_timer_check_ool_workaround(ate_match_dt, np);
Scott Woodf6dc1572016-09-22 03:35:17 -05001287
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001288 /*
1289 * If we cannot rely on firmware initializing the timer registers then
1290 * we should use the physical timers instead.
1291 */
1292 if (IS_ENABLED(CONFIG_ARM) &&
1293 of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
Fu Weiee34f1e2017-01-18 21:25:27 +08001294 arch_timer_uses_ppi = ARCH_TIMER_PHYS_SECURE_PPI;
Fu Wei4502b6b2017-01-18 21:25:30 +08001295 else
1296 arch_timer_uses_ppi = arch_timer_select_ppi();
1297
1298 if (!arch_timer_ppi[arch_timer_uses_ppi]) {
1299 pr_err("No interrupt available, giving up\n");
1300 return -EINVAL;
1301 }
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001302
Brian Norrisd8ec7592016-10-04 11:12:09 -07001303 /* On some systems, the counter stops ticking when in suspend. */
1304 arch_counter_suspend_stop = of_property_read_bool(np,
1305 "arm,no-tick-in-suspend");
1306
Fu Weica0e1b52017-03-22 00:31:15 +08001307 ret = arch_timer_register();
1308 if (ret)
1309 return ret;
1310
1311 if (arch_timer_needs_of_probing())
1312 return 0;
1313
1314 return arch_timer_common_init();
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001315}
Daniel Lezcano17273392017-05-26 16:56:11 +02001316TIMER_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_of_init);
1317TIMER_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_of_init);
Stephen Boyd22006992013-07-18 16:59:32 -07001318
Fu Weic389d702017-04-01 01:51:00 +08001319static u32 __init
1320arch_timer_mem_frame_get_cntfrq(struct arch_timer_mem_frame *frame)
Stephen Boyd22006992013-07-18 16:59:32 -07001321{
Fu Weic389d702017-04-01 01:51:00 +08001322 void __iomem *base;
1323 u32 rate;
Stephen Boyd22006992013-07-18 16:59:32 -07001324
Fu Weic389d702017-04-01 01:51:00 +08001325 base = ioremap(frame->cntbase, frame->size);
1326 if (!base) {
1327 pr_err("Unable to map frame @ %pa\n", &frame->cntbase);
1328 return 0;
1329 }
1330
Frank Rowand3db12002017-06-09 17:26:32 -07001331 rate = readl_relaxed(base + CNTFRQ);
Fu Weic389d702017-04-01 01:51:00 +08001332
Frank Rowand3db12002017-06-09 17:26:32 -07001333 iounmap(base);
Fu Weic389d702017-04-01 01:51:00 +08001334
1335 return rate;
1336}
1337
1338static struct arch_timer_mem_frame * __init
1339arch_timer_mem_find_best_frame(struct arch_timer_mem *timer_mem)
1340{
1341 struct arch_timer_mem_frame *frame, *best_frame = NULL;
1342 void __iomem *cntctlbase;
1343 u32 cnttidr;
1344 int i;
1345
1346 cntctlbase = ioremap(timer_mem->cntctlbase, timer_mem->size);
Stephen Boyd22006992013-07-18 16:59:32 -07001347 if (!cntctlbase) {
Fu Weic389d702017-04-01 01:51:00 +08001348 pr_err("Can't map CNTCTLBase @ %pa\n",
1349 &timer_mem->cntctlbase);
1350 return NULL;
Stephen Boyd22006992013-07-18 16:59:32 -07001351 }
1352
1353 cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
Stephen Boyd22006992013-07-18 16:59:32 -07001354
1355 /*
1356 * Try to find a virtual capable frame. Otherwise fall back to a
1357 * physical capable frame.
1358 */
Fu Weic389d702017-04-01 01:51:00 +08001359 for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
1360 u32 cntacr = CNTACR_RFRQ | CNTACR_RWPT | CNTACR_RPCT |
1361 CNTACR_RWVT | CNTACR_RVOFF | CNTACR_RVCT;
Stephen Boyd22006992013-07-18 16:59:32 -07001362
Fu Weic389d702017-04-01 01:51:00 +08001363 frame = &timer_mem->frame[i];
1364 if (!frame->valid)
1365 continue;
Stephen Boyd22006992013-07-18 16:59:32 -07001366
Robin Murphye392d602016-02-01 12:00:48 +00001367 /* Try enabling everything, and see what sticks */
Fu Weic389d702017-04-01 01:51:00 +08001368 writel_relaxed(cntacr, cntctlbase + CNTACR(i));
1369 cntacr = readl_relaxed(cntctlbase + CNTACR(i));
Robin Murphye392d602016-02-01 12:00:48 +00001370
Fu Weic389d702017-04-01 01:51:00 +08001371 if ((cnttidr & CNTTIDR_VIRT(i)) &&
Robin Murphye392d602016-02-01 12:00:48 +00001372 !(~cntacr & (CNTACR_RWVT | CNTACR_RVCT))) {
Stephen Boyd22006992013-07-18 16:59:32 -07001373 best_frame = frame;
1374 arch_timer_mem_use_virtual = true;
1375 break;
1376 }
Robin Murphye392d602016-02-01 12:00:48 +00001377
1378 if (~cntacr & (CNTACR_RWPT | CNTACR_RPCT))
1379 continue;
1380
Fu Weic389d702017-04-01 01:51:00 +08001381 best_frame = frame;
Stephen Boyd22006992013-07-18 16:59:32 -07001382 }
1383
Fu Weic389d702017-04-01 01:51:00 +08001384 iounmap(cntctlbase);
1385
Sudeep Hollaf63d9472017-05-08 13:32:27 +01001386 return best_frame;
Fu Weic389d702017-04-01 01:51:00 +08001387}
1388
1389static int __init
1390arch_timer_mem_frame_register(struct arch_timer_mem_frame *frame)
1391{
1392 void __iomem *base;
1393 int ret, irq = 0;
Stephen Boyd22006992013-07-18 16:59:32 -07001394
1395 if (arch_timer_mem_use_virtual)
Fu Weic389d702017-04-01 01:51:00 +08001396 irq = frame->virt_irq;
Stephen Boyd22006992013-07-18 16:59:32 -07001397 else
Fu Weic389d702017-04-01 01:51:00 +08001398 irq = frame->phys_irq;
Robin Murphye392d602016-02-01 12:00:48 +00001399
Stephen Boyd22006992013-07-18 16:59:32 -07001400 if (!irq) {
Fu Weided24012017-01-18 21:25:25 +08001401 pr_err("Frame missing %s irq.\n",
Thomas Gleixnercfb6d652013-08-21 14:59:23 +02001402 arch_timer_mem_use_virtual ? "virt" : "phys");
Fu Weic389d702017-04-01 01:51:00 +08001403 return -EINVAL;
1404 }
1405
1406 if (!request_mem_region(frame->cntbase, frame->size,
1407 "arch_mem_timer"))
1408 return -EBUSY;
1409
1410 base = ioremap(frame->cntbase, frame->size);
1411 if (!base) {
1412 pr_err("Can't map frame's registers\n");
1413 return -ENXIO;
1414 }
1415
1416 ret = arch_timer_mem_register(base, irq);
1417 if (ret) {
1418 iounmap(base);
1419 return ret;
1420 }
1421
1422 arch_counter_base = base;
1423 arch_timers_present |= ARCH_TIMER_TYPE_MEM;
1424
1425 return 0;
1426}
1427
1428static int __init arch_timer_mem_of_init(struct device_node *np)
1429{
1430 struct arch_timer_mem *timer_mem;
1431 struct arch_timer_mem_frame *frame;
1432 struct device_node *frame_node;
1433 struct resource res;
1434 int ret = -EINVAL;
1435 u32 rate;
1436
1437 timer_mem = kzalloc(sizeof(*timer_mem), GFP_KERNEL);
1438 if (!timer_mem)
1439 return -ENOMEM;
1440
1441 if (of_address_to_resource(np, 0, &res))
1442 goto out;
1443 timer_mem->cntctlbase = res.start;
1444 timer_mem->size = resource_size(&res);
1445
1446 for_each_available_child_of_node(np, frame_node) {
1447 u32 n;
1448 struct arch_timer_mem_frame *frame;
1449
1450 if (of_property_read_u32(frame_node, "frame-number", &n)) {
1451 pr_err(FW_BUG "Missing frame-number.\n");
1452 of_node_put(frame_node);
1453 goto out;
1454 }
1455 if (n >= ARCH_TIMER_MEM_MAX_FRAMES) {
1456 pr_err(FW_BUG "Wrong frame-number, only 0-%u are permitted.\n",
1457 ARCH_TIMER_MEM_MAX_FRAMES - 1);
1458 of_node_put(frame_node);
1459 goto out;
1460 }
1461 frame = &timer_mem->frame[n];
1462
1463 if (frame->valid) {
1464 pr_err(FW_BUG "Duplicated frame-number.\n");
1465 of_node_put(frame_node);
1466 goto out;
1467 }
1468
1469 if (of_address_to_resource(frame_node, 0, &res)) {
1470 of_node_put(frame_node);
1471 goto out;
1472 }
1473 frame->cntbase = res.start;
1474 frame->size = resource_size(&res);
1475
1476 frame->virt_irq = irq_of_parse_and_map(frame_node,
1477 ARCH_TIMER_VIRT_SPI);
1478 frame->phys_irq = irq_of_parse_and_map(frame_node,
1479 ARCH_TIMER_PHYS_SPI);
1480
1481 frame->valid = true;
1482 }
1483
1484 frame = arch_timer_mem_find_best_frame(timer_mem);
1485 if (!frame) {
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001486 pr_err("Unable to find a suitable frame in timer @ %pa\n",
1487 &timer_mem->cntctlbase);
Fu Weic389d702017-04-01 01:51:00 +08001488 ret = -EINVAL;
Robin Murphye392d602016-02-01 12:00:48 +00001489 goto out;
Stephen Boyd22006992013-07-18 16:59:32 -07001490 }
1491
Fu Weic389d702017-04-01 01:51:00 +08001492 rate = arch_timer_mem_frame_get_cntfrq(frame);
Fu Wei5d3dfa92017-03-22 00:31:13 +08001493 arch_timer_of_configure_rate(rate, np);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001494
Fu Weic389d702017-04-01 01:51:00 +08001495 ret = arch_timer_mem_frame_register(frame);
1496 if (!ret && !arch_timer_needs_of_probing())
Fu Weica0e1b52017-03-22 00:31:15 +08001497 ret = arch_timer_common_init();
Robin Murphye392d602016-02-01 12:00:48 +00001498out:
Fu Weic389d702017-04-01 01:51:00 +08001499 kfree(timer_mem);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001500 return ret;
Stephen Boyd22006992013-07-18 16:59:32 -07001501}
Daniel Lezcano17273392017-05-26 16:56:11 +02001502TIMER_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem",
Fu Weic389d702017-04-01 01:51:00 +08001503 arch_timer_mem_of_init);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001504
Fu Weif79d2092017-04-01 01:51:02 +08001505#ifdef CONFIG_ACPI_GTDT
Fu Weic2743a32017-04-01 01:51:04 +08001506static int __init
1507arch_timer_mem_verify_cntfrq(struct arch_timer_mem *timer_mem)
1508{
1509 struct arch_timer_mem_frame *frame;
1510 u32 rate;
1511 int i;
1512
1513 for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
1514 frame = &timer_mem->frame[i];
1515
1516 if (!frame->valid)
1517 continue;
1518
1519 rate = arch_timer_mem_frame_get_cntfrq(frame);
1520 if (rate == arch_timer_rate)
1521 continue;
1522
1523 pr_err(FW_BUG "CNTFRQ mismatch: frame @ %pa: (0x%08lx), CPU: (0x%08lx)\n",
1524 &frame->cntbase,
1525 (unsigned long)rate, (unsigned long)arch_timer_rate);
1526
1527 return -EINVAL;
1528 }
1529
1530 return 0;
1531}
1532
1533static int __init arch_timer_mem_acpi_init(int platform_timer_count)
1534{
1535 struct arch_timer_mem *timers, *timer;
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001536 struct arch_timer_mem_frame *frame, *best_frame = NULL;
Fu Weic2743a32017-04-01 01:51:04 +08001537 int timer_count, i, ret = 0;
1538
1539 timers = kcalloc(platform_timer_count, sizeof(*timers),
1540 GFP_KERNEL);
1541 if (!timers)
1542 return -ENOMEM;
1543
1544 ret = acpi_arch_timer_mem_init(timers, &timer_count);
1545 if (ret || !timer_count)
1546 goto out;
1547
Fu Weic2743a32017-04-01 01:51:04 +08001548 /*
1549 * While unlikely, it's theoretically possible that none of the frames
1550 * in a timer expose the combination of feature we want.
1551 */
Matthias Kaehlcked197f792017-07-31 11:37:28 -07001552 for (i = 0; i < timer_count; i++) {
Fu Weic2743a32017-04-01 01:51:04 +08001553 timer = &timers[i];
1554
1555 frame = arch_timer_mem_find_best_frame(timer);
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001556 if (!best_frame)
1557 best_frame = frame;
1558
1559 ret = arch_timer_mem_verify_cntfrq(timer);
1560 if (ret) {
1561 pr_err("Disabling MMIO timers due to CNTFRQ mismatch\n");
1562 goto out;
1563 }
1564
1565 if (!best_frame) /* implies !frame */
1566 /*
1567 * Only complain about missing suitable frames if we
1568 * haven't already found one in a previous iteration.
1569 */
1570 pr_err("Unable to find a suitable frame in timer @ %pa\n",
1571 &timer->cntctlbase);
Fu Weic2743a32017-04-01 01:51:04 +08001572 }
1573
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001574 if (best_frame)
1575 ret = arch_timer_mem_frame_register(best_frame);
Fu Weic2743a32017-04-01 01:51:04 +08001576out:
1577 kfree(timers);
1578 return ret;
1579}
1580
1581/* Initialize per-processor generic timer and memory-mapped timer(if present) */
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001582static int __init arch_timer_acpi_init(struct acpi_table_header *table)
1583{
Fu Weic2743a32017-04-01 01:51:04 +08001584 int ret, platform_timer_count;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001585
Fu Wei8a5c21d2017-01-18 21:25:26 +08001586 if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
Fu Weided24012017-01-18 21:25:25 +08001587 pr_warn("already initialized, skipping\n");
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001588 return -EINVAL;
1589 }
1590
Fu Wei8a5c21d2017-01-18 21:25:26 +08001591 arch_timers_present |= ARCH_TIMER_TYPE_CP15;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001592
Fu Weic2743a32017-04-01 01:51:04 +08001593 ret = acpi_gtdt_init(table, &platform_timer_count);
Fu Weif79d2092017-04-01 01:51:02 +08001594 if (ret) {
1595 pr_err("Failed to init GTDT table.\n");
1596 return ret;
1597 }
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001598
Fu Weiee34f1e2017-01-18 21:25:27 +08001599 arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI] =
Fu Weif79d2092017-04-01 01:51:02 +08001600 acpi_gtdt_map_ppi(ARCH_TIMER_PHYS_NONSECURE_PPI);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001601
Fu Weiee34f1e2017-01-18 21:25:27 +08001602 arch_timer_ppi[ARCH_TIMER_VIRT_PPI] =
Fu Weif79d2092017-04-01 01:51:02 +08001603 acpi_gtdt_map_ppi(ARCH_TIMER_VIRT_PPI);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001604
Fu Weiee34f1e2017-01-18 21:25:27 +08001605 arch_timer_ppi[ARCH_TIMER_HYP_PPI] =
Fu Weif79d2092017-04-01 01:51:02 +08001606 acpi_gtdt_map_ppi(ARCH_TIMER_HYP_PPI);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001607
Fu Weica0e1b52017-03-22 00:31:15 +08001608 arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI];
1609
Fu Wei5d3dfa92017-03-22 00:31:13 +08001610 /*
1611 * When probing via ACPI, we have no mechanism to override the sysreg
1612 * CNTFRQ value. This *must* be correct.
1613 */
1614 arch_timer_rate = arch_timer_get_cntfrq();
1615 if (!arch_timer_rate) {
1616 pr_err(FW_BUG "frequency not available.\n");
1617 return -EINVAL;
1618 }
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001619
Fu Wei4502b6b2017-01-18 21:25:30 +08001620 arch_timer_uses_ppi = arch_timer_select_ppi();
1621 if (!arch_timer_ppi[arch_timer_uses_ppi]) {
1622 pr_err("No interrupt available, giving up\n");
1623 return -EINVAL;
1624 }
1625
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001626 /* Always-on capability */
Fu Weif79d2092017-04-01 01:51:02 +08001627 arch_timer_c3stop = acpi_gtdt_c3stop(arch_timer_uses_ppi);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001628
Marc Zyngier5a38bca2017-02-21 14:37:30 +00001629 /* Check for globally applicable workarounds */
1630 arch_timer_check_ool_workaround(ate_match_acpi_oem_info, table);
1631
Fu Weica0e1b52017-03-22 00:31:15 +08001632 ret = arch_timer_register();
1633 if (ret)
1634 return ret;
1635
Fu Weic2743a32017-04-01 01:51:04 +08001636 if (platform_timer_count &&
1637 arch_timer_mem_acpi_init(platform_timer_count))
1638 pr_err("Failed to initialize memory-mapped timer.\n");
1639
Fu Weica0e1b52017-03-22 00:31:15 +08001640 return arch_timer_common_init();
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001641}
Daniel Lezcano77d62f52017-05-26 17:42:25 +02001642TIMER_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001643#endif