blob: 27acc9eb0f7c495c78ddc8289c649af543dc4432 [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 Thierryec5c8e422017-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 Zyngier0ea41532019-04-08 16:49:07 +0100155static u64 arch_counter_get_cntpct_stable(void)
156{
157 return __arch_counter_get_cntpct_stable();
158}
159
160static u64 arch_counter_get_cntpct(void)
161{
162 return __arch_counter_get_cntpct();
163}
164
165static u64 arch_counter_get_cntvct_stable(void)
166{
167 return __arch_counter_get_cntvct_stable();
168}
169
170static u64 arch_counter_get_cntvct(void)
171{
172 return __arch_counter_get_cntvct();
173}
174
Marc Zyngier992dd162017-02-01 11:53:46 +0000175/*
176 * Default to cp15 based access because arm64 uses this function for
177 * sched_clock() before DT is probed and the cp15 method is guaranteed
178 * to exist on arm64. arm doesn't use this before DT is probed so even
179 * if we don't have the cp15 accessors we won't have a problem.
180 */
181u64 (*arch_timer_read_counter)(void) = arch_counter_get_cntvct;
Christoffer Dalle6d68b002017-07-05 11:04:28 +0200182EXPORT_SYMBOL_GPL(arch_timer_read_counter);
Marc Zyngier992dd162017-02-01 11:53:46 +0000183
184static u64 arch_counter_read(struct clocksource *cs)
185{
186 return arch_timer_read_counter();
187}
188
189static u64 arch_counter_read_cc(const struct cyclecounter *cc)
190{
191 return arch_timer_read_counter();
192}
193
194static struct clocksource clocksource_counter = {
195 .name = "arch_sys_counter",
196 .rating = 400,
197 .read = arch_counter_read,
198 .mask = CLOCKSOURCE_MASK(56),
199 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
200};
201
202static struct cyclecounter cyclecounter __ro_after_init = {
203 .read = arch_counter_read_cc,
204 .mask = CLOCKSOURCE_MASK(56),
205};
206
Marc Zyngier5a38bca2017-02-21 14:37:30 +0000207struct ate_acpi_oem_info {
208 char oem_id[ACPI_OEM_ID_SIZE + 1];
209 char oem_table_id[ACPI_OEM_TABLE_ID_SIZE + 1];
210 u32 oem_revision;
211};
212
Scott Woodf6dc1572016-09-22 03:35:17 -0500213#ifdef CONFIG_FSL_ERRATUM_A008585
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000214/*
215 * The number of retries is an arbitrary value well beyond the highest number
216 * of iterations the loop has been observed to take.
217 */
218#define __fsl_a008585_read_reg(reg) ({ \
219 u64 _old, _new; \
220 int _retries = 200; \
221 \
222 do { \
223 _old = read_sysreg(reg); \
224 _new = read_sysreg(reg); \
225 _retries--; \
226 } while (unlikely(_old != _new) && _retries); \
227 \
228 WARN_ON_ONCE(!_retries); \
229 _new; \
230})
Scott Woodf6dc1572016-09-22 03:35:17 -0500231
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000232static u32 notrace fsl_a008585_read_cntp_tval_el0(void)
Scott Woodf6dc1572016-09-22 03:35:17 -0500233{
234 return __fsl_a008585_read_reg(cntp_tval_el0);
235}
236
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000237static u32 notrace fsl_a008585_read_cntv_tval_el0(void)
Scott Woodf6dc1572016-09-22 03:35:17 -0500238{
239 return __fsl_a008585_read_reg(cntv_tval_el0);
240}
241
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200242static u64 notrace fsl_a008585_read_cntpct_el0(void)
243{
244 return __fsl_a008585_read_reg(cntpct_el0);
245}
246
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000247static u64 notrace fsl_a008585_read_cntvct_el0(void)
Scott Woodf6dc1572016-09-22 03:35:17 -0500248{
249 return __fsl_a008585_read_reg(cntvct_el0);
250}
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000251#endif
252
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000253#ifdef CONFIG_HISILICON_ERRATUM_161010101
254/*
255 * Verify whether the value of the second read is larger than the first by
256 * less than 32 is the only way to confirm the value is correct, so clear the
257 * lower 5 bits to check whether the difference is greater than 32 or not.
258 * Theoretically the erratum should not occur more than twice in succession
259 * when reading the system counter, but it is possible that some interrupts
260 * may lead to more than twice read errors, triggering the warning, so setting
261 * the number of retries far beyond the number of iterations the loop has been
262 * observed to take.
263 */
264#define __hisi_161010101_read_reg(reg) ({ \
265 u64 _old, _new; \
266 int _retries = 50; \
267 \
268 do { \
269 _old = read_sysreg(reg); \
270 _new = read_sysreg(reg); \
271 _retries--; \
272 } while (unlikely((_new - _old) >> 5) && _retries); \
273 \
274 WARN_ON_ONCE(!_retries); \
275 _new; \
276})
277
278static u32 notrace hisi_161010101_read_cntp_tval_el0(void)
279{
280 return __hisi_161010101_read_reg(cntp_tval_el0);
281}
282
283static u32 notrace hisi_161010101_read_cntv_tval_el0(void)
284{
285 return __hisi_161010101_read_reg(cntv_tval_el0);
286}
287
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200288static u64 notrace hisi_161010101_read_cntpct_el0(void)
289{
290 return __hisi_161010101_read_reg(cntpct_el0);
291}
292
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000293static u64 notrace hisi_161010101_read_cntvct_el0(void)
294{
295 return __hisi_161010101_read_reg(cntvct_el0);
296}
Marc Zyngierd003d022017-02-21 15:04:27 +0000297
298static struct ate_acpi_oem_info hisi_161010101_oem_info[] = {
299 /*
300 * Note that trailing spaces are required to properly match
301 * the OEM table information.
302 */
303 {
304 .oem_id = "HISI ",
305 .oem_table_id = "HIP05 ",
306 .oem_revision = 0,
307 },
308 {
309 .oem_id = "HISI ",
310 .oem_table_id = "HIP06 ",
311 .oem_revision = 0,
312 },
313 {
314 .oem_id = "HISI ",
315 .oem_table_id = "HIP07 ",
316 .oem_revision = 0,
317 },
318 { /* Sentinel indicating the end of the OEM array */ },
319};
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000320#endif
321
Marc Zyngierfa8d8152017-01-27 12:52:31 +0000322#ifdef CONFIG_ARM64_ERRATUM_858921
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200323static u64 notrace arm64_858921_read_cntpct_el0(void)
324{
325 u64 old, new;
326
327 old = read_sysreg(cntpct_el0);
328 new = read_sysreg(cntpct_el0);
329 return (((old ^ new) >> 32) & 1) ? old : new;
330}
331
Marc Zyngierfa8d8152017-01-27 12:52:31 +0000332static u64 notrace arm64_858921_read_cntvct_el0(void)
333{
334 u64 old, new;
335
336 old = read_sysreg(cntvct_el0);
337 new = read_sysreg(cntvct_el0);
338 return (((old ^ new) >> 32) & 1) ? old : new;
339}
340#endif
341
Samuel Hollandc950ca82019-01-12 20:17:18 -0600342#ifdef CONFIG_SUN50I_ERRATUM_UNKNOWN1
343/*
344 * The low bits of the counter registers are indeterminate while bit 10 or
345 * greater is rolling over. Since the counter value can jump both backward
346 * (7ff -> 000 -> 800) and forward (7ff -> fff -> 800), ignore register values
347 * with all ones or all zeros in the low bits. Bound the loop by the maximum
348 * number of CPU cycles in 3 consecutive 24 MHz counter periods.
349 */
350#define __sun50i_a64_read_reg(reg) ({ \
351 u64 _val; \
352 int _retries = 150; \
353 \
354 do { \
355 _val = read_sysreg(reg); \
356 _retries--; \
357 } while (((_val + 1) & GENMASK(9, 0)) <= 1 && _retries); \
358 \
359 WARN_ON_ONCE(!_retries); \
360 _val; \
361})
362
363static u64 notrace sun50i_a64_read_cntpct_el0(void)
364{
365 return __sun50i_a64_read_reg(cntpct_el0);
366}
367
368static u64 notrace sun50i_a64_read_cntvct_el0(void)
369{
370 return __sun50i_a64_read_reg(cntvct_el0);
371}
372
373static u32 notrace sun50i_a64_read_cntp_tval_el0(void)
374{
375 return read_sysreg(cntp_cval_el0) - sun50i_a64_read_cntpct_el0();
376}
377
378static u32 notrace sun50i_a64_read_cntv_tval_el0(void)
379{
380 return read_sysreg(cntv_cval_el0) - sun50i_a64_read_cntvct_el0();
381}
382#endif
383
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000384#ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
Mark Rutlanda7fb4572017-10-16 16:28:39 +0100385DEFINE_PER_CPU(const struct arch_timer_erratum_workaround *, timer_unstable_counter_workaround);
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000386EXPORT_SYMBOL_GPL(timer_unstable_counter_workaround);
387
Marc Zyngier0ea41532019-04-08 16:49:07 +0100388static atomic_t timer_unstable_counter_workaround_in_use = ATOMIC_INIT(0);
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000389
Marc Zyngier83280892017-01-27 10:27:09 +0000390static void erratum_set_next_event_tval_generic(const int access, unsigned long evt,
391 struct clock_event_device *clk)
392{
393 unsigned long ctrl;
Christoffer Dalle6d68b002017-07-05 11:04:28 +0200394 u64 cval;
Marc Zyngier83280892017-01-27 10:27:09 +0000395
396 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
397 ctrl |= ARCH_TIMER_CTRL_ENABLE;
398 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
399
Christoffer Dalle6d68b002017-07-05 11:04:28 +0200400 if (access == ARCH_TIMER_PHYS_ACCESS) {
401 cval = evt + arch_counter_get_cntpct();
Marc Zyngier83280892017-01-27 10:27:09 +0000402 write_sysreg(cval, cntp_cval_el0);
Christoffer Dalle6d68b002017-07-05 11:04:28 +0200403 } else {
404 cval = evt + arch_counter_get_cntvct();
Marc Zyngier83280892017-01-27 10:27:09 +0000405 write_sysreg(cval, cntv_cval_el0);
Christoffer Dalle6d68b002017-07-05 11:04:28 +0200406 }
Marc Zyngier83280892017-01-27 10:27:09 +0000407
408 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
409}
410
Arnd Bergmanneb645222017-04-19 19:37:09 +0200411static __maybe_unused int erratum_set_next_event_tval_virt(unsigned long evt,
Marc Zyngier83280892017-01-27 10:27:09 +0000412 struct clock_event_device *clk)
413{
414 erratum_set_next_event_tval_generic(ARCH_TIMER_VIRT_ACCESS, evt, clk);
415 return 0;
416}
417
Arnd Bergmanneb645222017-04-19 19:37:09 +0200418static __maybe_unused int erratum_set_next_event_tval_phys(unsigned long evt,
Marc Zyngier83280892017-01-27 10:27:09 +0000419 struct clock_event_device *clk)
420{
421 erratum_set_next_event_tval_generic(ARCH_TIMER_PHYS_ACCESS, evt, clk);
422 return 0;
423}
424
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000425static const struct arch_timer_erratum_workaround ool_workarounds[] = {
426#ifdef CONFIG_FSL_ERRATUM_A008585
427 {
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000428 .match_type = ate_match_dt,
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000429 .id = "fsl,erratum-a008585",
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000430 .desc = "Freescale erratum a005858",
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000431 .read_cntp_tval_el0 = fsl_a008585_read_cntp_tval_el0,
432 .read_cntv_tval_el0 = fsl_a008585_read_cntv_tval_el0,
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200433 .read_cntpct_el0 = fsl_a008585_read_cntpct_el0,
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000434 .read_cntvct_el0 = fsl_a008585_read_cntvct_el0,
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000435 .set_next_event_phys = erratum_set_next_event_tval_phys,
436 .set_next_event_virt = erratum_set_next_event_tval_virt,
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000437 },
438#endif
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000439#ifdef CONFIG_HISILICON_ERRATUM_161010101
440 {
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000441 .match_type = ate_match_dt,
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000442 .id = "hisilicon,erratum-161010101",
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000443 .desc = "HiSilicon erratum 161010101",
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000444 .read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
445 .read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200446 .read_cntpct_el0 = hisi_161010101_read_cntpct_el0,
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000447 .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000448 .set_next_event_phys = erratum_set_next_event_tval_phys,
449 .set_next_event_virt = erratum_set_next_event_tval_virt,
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000450 },
Marc Zyngierd003d022017-02-21 15:04:27 +0000451 {
452 .match_type = ate_match_acpi_oem_info,
453 .id = hisi_161010101_oem_info,
454 .desc = "HiSilicon erratum 161010101",
455 .read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
456 .read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200457 .read_cntpct_el0 = hisi_161010101_read_cntpct_el0,
Marc Zyngierd003d022017-02-21 15:04:27 +0000458 .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
459 .set_next_event_phys = erratum_set_next_event_tval_phys,
460 .set_next_event_virt = erratum_set_next_event_tval_virt,
461 },
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000462#endif
Marc Zyngierfa8d8152017-01-27 12:52:31 +0000463#ifdef CONFIG_ARM64_ERRATUM_858921
464 {
465 .match_type = ate_match_local_cap_id,
466 .id = (void *)ARM64_WORKAROUND_858921,
467 .desc = "ARM erratum 858921",
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200468 .read_cntpct_el0 = arm64_858921_read_cntpct_el0,
Marc Zyngierfa8d8152017-01-27 12:52:31 +0000469 .read_cntvct_el0 = arm64_858921_read_cntvct_el0,
470 },
471#endif
Samuel Hollandc950ca82019-01-12 20:17:18 -0600472#ifdef CONFIG_SUN50I_ERRATUM_UNKNOWN1
473 {
474 .match_type = ate_match_dt,
475 .id = "allwinner,erratum-unknown1",
476 .desc = "Allwinner erratum UNKNOWN1",
477 .read_cntp_tval_el0 = sun50i_a64_read_cntp_tval_el0,
478 .read_cntv_tval_el0 = sun50i_a64_read_cntv_tval_el0,
479 .read_cntpct_el0 = sun50i_a64_read_cntpct_el0,
480 .read_cntvct_el0 = sun50i_a64_read_cntvct_el0,
481 .set_next_event_phys = erratum_set_next_event_tval_phys,
482 .set_next_event_virt = erratum_set_next_event_tval_virt,
483 },
484#endif
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000485};
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000486
487typedef bool (*ate_match_fn_t)(const struct arch_timer_erratum_workaround *,
488 const void *);
489
490static
491bool arch_timer_check_dt_erratum(const struct arch_timer_erratum_workaround *wa,
492 const void *arg)
493{
494 const struct device_node *np = arg;
495
496 return of_property_read_bool(np, wa->id);
497}
498
Marc Zyngier00640302017-03-20 16:47:59 +0000499static
500bool arch_timer_check_local_cap_erratum(const struct arch_timer_erratum_workaround *wa,
501 const void *arg)
502{
503 return this_cpu_has_cap((uintptr_t)wa->id);
504}
505
Marc Zyngier5a38bca2017-02-21 14:37:30 +0000506
507static
508bool arch_timer_check_acpi_oem_erratum(const struct arch_timer_erratum_workaround *wa,
509 const void *arg)
510{
511 static const struct ate_acpi_oem_info empty_oem_info = {};
512 const struct ate_acpi_oem_info *info = wa->id;
513 const struct acpi_table_header *table = arg;
514
515 /* Iterate over the ACPI OEM info array, looking for a match */
516 while (memcmp(info, &empty_oem_info, sizeof(*info))) {
517 if (!memcmp(info->oem_id, table->oem_id, ACPI_OEM_ID_SIZE) &&
518 !memcmp(info->oem_table_id, table->oem_table_id, ACPI_OEM_TABLE_ID_SIZE) &&
519 info->oem_revision == table->oem_revision)
520 return true;
521
522 info++;
523 }
524
525 return false;
526}
527
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000528static const struct arch_timer_erratum_workaround *
529arch_timer_iterate_errata(enum arch_timer_erratum_match_type type,
530 ate_match_fn_t match_fn,
531 void *arg)
532{
533 int i;
534
535 for (i = 0; i < ARRAY_SIZE(ool_workarounds); i++) {
536 if (ool_workarounds[i].match_type != type)
537 continue;
538
539 if (match_fn(&ool_workarounds[i], arg))
540 return &ool_workarounds[i];
541 }
542
543 return NULL;
544}
545
546static
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000547void arch_timer_enable_workaround(const struct arch_timer_erratum_workaround *wa,
548 bool local)
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000549{
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000550 int i;
551
552 if (local) {
553 __this_cpu_write(timer_unstable_counter_workaround, wa);
554 } else {
555 for_each_possible_cpu(i)
556 per_cpu(timer_unstable_counter_workaround, i) = wa;
557 }
558
Marc Zyngier0ea41532019-04-08 16:49:07 +0100559 if (wa->read_cntvct_el0 || wa->read_cntpct_el0)
560 atomic_set(&timer_unstable_counter_workaround_in_use, 1);
561
Marc Zyngier450f9682017-08-01 09:02:57 +0100562 /*
Marc Zyngiera86bd132017-02-01 12:07:15 +0000563 * Don't use the vdso fastpath if errata require using the
564 * out-of-line counter accessor. We may change our mind pretty
565 * late in the game (with a per-CPU erratum, for example), so
566 * change both the default value and the vdso itself.
567 */
568 if (wa->read_cntvct_el0) {
569 clocksource_counter.archdata.vdso_direct = false;
570 vdso_default = false;
571 }
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000572}
573
574static void arch_timer_check_ool_workaround(enum arch_timer_erratum_match_type type,
575 void *arg)
576{
Marc Zyngiera862fc22019-04-08 16:49:06 +0100577 const struct arch_timer_erratum_workaround *wa, *__wa;
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000578 ate_match_fn_t match_fn = NULL;
Marc Zyngier00640302017-03-20 16:47:59 +0000579 bool local = false;
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000580
581 switch (type) {
582 case ate_match_dt:
583 match_fn = arch_timer_check_dt_erratum;
584 break;
Marc Zyngier00640302017-03-20 16:47:59 +0000585 case ate_match_local_cap_id:
586 match_fn = arch_timer_check_local_cap_erratum;
587 local = true;
588 break;
Marc Zyngier5a38bca2017-02-21 14:37:30 +0000589 case ate_match_acpi_oem_info:
590 match_fn = arch_timer_check_acpi_oem_erratum;
591 break;
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000592 default:
593 WARN_ON(1);
594 return;
595 }
596
597 wa = arch_timer_iterate_errata(type, match_fn, arg);
598 if (!wa)
599 return;
600
Marc Zyngiera862fc22019-04-08 16:49:06 +0100601 __wa = __this_cpu_read(timer_unstable_counter_workaround);
602 if (__wa && wa != __wa)
603 pr_warn("Can't enable workaround for %s (clashes with %s\n)",
604 wa->desc, __wa->desc);
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000605
Marc Zyngiera862fc22019-04-08 16:49:06 +0100606 if (__wa)
607 return;
Marc Zyngier00640302017-03-20 16:47:59 +0000608
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000609 arch_timer_enable_workaround(wa, local);
Marc Zyngier00640302017-03-20 16:47:59 +0000610 pr_info("Enabling %s workaround for %s\n",
611 local ? "local" : "global", wa->desc);
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000612}
613
Marc Zyngiera86bd132017-02-01 12:07:15 +0000614static bool arch_timer_this_cpu_has_cntvct_wa(void)
615{
Marc Zyngier5ef19a12019-04-08 16:49:04 +0100616 return has_erratum_handler(read_cntvct_el0);
Marc Zyngiera86bd132017-02-01 12:07:15 +0000617}
Marc Zyngier0ea41532019-04-08 16:49:07 +0100618
619static bool arch_timer_counter_has_wa(void)
620{
621 return atomic_read(&timer_unstable_counter_workaround_in_use);
622}
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000623#else
624#define arch_timer_check_ool_workaround(t,a) do { } while(0)
Marc Zyngiera86bd132017-02-01 12:07:15 +0000625#define arch_timer_this_cpu_has_cntvct_wa() ({false;})
Marc Zyngier0ea41532019-04-08 16:49:07 +0100626#define arch_timer_counter_has_wa() ({false;})
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000627#endif /* CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND */
Scott Woodf6dc1572016-09-22 03:35:17 -0500628
Stephen Boyde09f3cc2013-07-18 16:59:28 -0700629static __always_inline irqreturn_t timer_handler(const int access,
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000630 struct clock_event_device *evt)
631{
632 unsigned long ctrl;
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200633
Stephen Boyd60faddf2013-07-18 16:59:31 -0700634 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000635 if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
636 ctrl |= ARCH_TIMER_CTRL_IT_MASK;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700637 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000638 evt->event_handler(evt);
639 return IRQ_HANDLED;
640 }
641
642 return IRQ_NONE;
643}
644
645static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
646{
647 struct clock_event_device *evt = dev_id;
648
649 return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
650}
651
652static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
653{
654 struct clock_event_device *evt = dev_id;
655
656 return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
657}
658
Stephen Boyd22006992013-07-18 16:59:32 -0700659static irqreturn_t arch_timer_handler_phys_mem(int irq, void *dev_id)
660{
661 struct clock_event_device *evt = dev_id;
662
663 return timer_handler(ARCH_TIMER_MEM_PHYS_ACCESS, evt);
664}
665
666static irqreturn_t arch_timer_handler_virt_mem(int irq, void *dev_id)
667{
668 struct clock_event_device *evt = dev_id;
669
670 return timer_handler(ARCH_TIMER_MEM_VIRT_ACCESS, evt);
671}
672
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530673static __always_inline int timer_shutdown(const int access,
674 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000675{
676 unsigned long ctrl;
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530677
678 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
679 ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
680 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
681
682 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000683}
684
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530685static int arch_timer_shutdown_virt(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000686{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530687 return timer_shutdown(ARCH_TIMER_VIRT_ACCESS, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000688}
689
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530690static int arch_timer_shutdown_phys(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000691{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530692 return timer_shutdown(ARCH_TIMER_PHYS_ACCESS, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000693}
694
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530695static int arch_timer_shutdown_virt_mem(struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700696{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530697 return timer_shutdown(ARCH_TIMER_MEM_VIRT_ACCESS, clk);
Stephen Boyd22006992013-07-18 16:59:32 -0700698}
699
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530700static int arch_timer_shutdown_phys_mem(struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700701{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530702 return timer_shutdown(ARCH_TIMER_MEM_PHYS_ACCESS, clk);
Stephen Boyd22006992013-07-18 16:59:32 -0700703}
704
Stephen Boyd60faddf2013-07-18 16:59:31 -0700705static __always_inline void set_next_event(const int access, unsigned long evt,
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200706 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000707{
708 unsigned long ctrl;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700709 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000710 ctrl |= ARCH_TIMER_CTRL_ENABLE;
711 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700712 arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt, clk);
713 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000714}
715
716static int arch_timer_set_next_event_virt(unsigned long evt,
Stephen Boyd60faddf2013-07-18 16:59:31 -0700717 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000718{
Stephen Boyd60faddf2013-07-18 16:59:31 -0700719 set_next_event(ARCH_TIMER_VIRT_ACCESS, evt, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000720 return 0;
721}
722
723static int arch_timer_set_next_event_phys(unsigned long evt,
Stephen Boyd60faddf2013-07-18 16:59:31 -0700724 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000725{
Stephen Boyd60faddf2013-07-18 16:59:31 -0700726 set_next_event(ARCH_TIMER_PHYS_ACCESS, evt, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000727 return 0;
728}
729
Stephen Boyd22006992013-07-18 16:59:32 -0700730static int arch_timer_set_next_event_virt_mem(unsigned long evt,
731 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000732{
Stephen Boyd22006992013-07-18 16:59:32 -0700733 set_next_event(ARCH_TIMER_MEM_VIRT_ACCESS, evt, clk);
734 return 0;
735}
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000736
Stephen Boyd22006992013-07-18 16:59:32 -0700737static int arch_timer_set_next_event_phys_mem(unsigned long evt,
738 struct clock_event_device *clk)
739{
740 set_next_event(ARCH_TIMER_MEM_PHYS_ACCESS, evt, clk);
741 return 0;
742}
743
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200744static void __arch_timer_setup(unsigned type,
745 struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700746{
747 clk->features = CLOCK_EVT_FEAT_ONESHOT;
748
Fu Wei8a5c21d2017-01-18 21:25:26 +0800749 if (type == ARCH_TIMER_TYPE_CP15) {
Marc Zyngier5ef19a12019-04-08 16:49:04 +0100750 typeof(clk->set_next_event) sne;
751
752 arch_timer_check_ool_workaround(ate_match_local_cap_id, NULL);
753
Lorenzo Pieralisi82a561942014-04-08 10:04:32 +0100754 if (arch_timer_c3stop)
755 clk->features |= CLOCK_EVT_FEAT_C3STOP;
Stephen Boyd22006992013-07-18 16:59:32 -0700756 clk->name = "arch_sys_timer";
757 clk->rating = 450;
758 clk->cpumask = cpumask_of(smp_processor_id());
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000759 clk->irq = arch_timer_ppi[arch_timer_uses_ppi];
760 switch (arch_timer_uses_ppi) {
Fu Weiee34f1e2017-01-18 21:25:27 +0800761 case ARCH_TIMER_VIRT_PPI:
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530762 clk->set_state_shutdown = arch_timer_shutdown_virt;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530763 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt;
Marc Zyngier5ef19a12019-04-08 16:49:04 +0100764 sne = erratum_handler(set_next_event_virt);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000765 break;
Fu Weiee34f1e2017-01-18 21:25:27 +0800766 case ARCH_TIMER_PHYS_SECURE_PPI:
767 case ARCH_TIMER_PHYS_NONSECURE_PPI:
768 case ARCH_TIMER_HYP_PPI:
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530769 clk->set_state_shutdown = arch_timer_shutdown_phys;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530770 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys;
Marc Zyngier5ef19a12019-04-08 16:49:04 +0100771 sne = erratum_handler(set_next_event_phys);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000772 break;
773 default:
774 BUG();
Stephen Boyd22006992013-07-18 16:59:32 -0700775 }
Scott Woodf6dc1572016-09-22 03:35:17 -0500776
Marc Zyngier5ef19a12019-04-08 16:49:04 +0100777 clk->set_next_event = sne;
Stephen Boyd22006992013-07-18 16:59:32 -0700778 } else {
Stephen Boyd7b52ad22014-01-06 14:56:17 -0800779 clk->features |= CLOCK_EVT_FEAT_DYNIRQ;
Stephen Boyd22006992013-07-18 16:59:32 -0700780 clk->name = "arch_mem_timer";
781 clk->rating = 400;
Sudeep Holla5e18e412018-07-09 16:45:36 +0100782 clk->cpumask = cpu_possible_mask;
Stephen Boyd22006992013-07-18 16:59:32 -0700783 if (arch_timer_mem_use_virtual) {
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530784 clk->set_state_shutdown = arch_timer_shutdown_virt_mem;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530785 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt_mem;
Stephen Boyd22006992013-07-18 16:59:32 -0700786 clk->set_next_event =
787 arch_timer_set_next_event_virt_mem;
788 } else {
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530789 clk->set_state_shutdown = arch_timer_shutdown_phys_mem;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530790 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys_mem;
Stephen Boyd22006992013-07-18 16:59:32 -0700791 clk->set_next_event =
792 arch_timer_set_next_event_phys_mem;
793 }
794 }
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000795
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530796 clk->set_state_shutdown(clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000797
Stephen Boyd22006992013-07-18 16:59:32 -0700798 clockevents_config_and_register(clk, arch_timer_rate, 0xf, 0x7fffffff);
799}
800
Nathan Lynche1ce5c72014-09-29 01:50:06 +0200801static void arch_timer_evtstrm_enable(int divider)
802{
803 u32 cntkctl = arch_timer_get_cntkctl();
804
805 cntkctl &= ~ARCH_TIMER_EVT_TRIGGER_MASK;
806 /* Set the divider and enable virtual event stream */
807 cntkctl |= (divider << ARCH_TIMER_EVT_TRIGGER_SHIFT)
808 | ARCH_TIMER_VIRT_EVT_EN;
809 arch_timer_set_cntkctl(cntkctl);
810 elf_hwcap |= HWCAP_EVTSTRM;
811#ifdef CONFIG_COMPAT
812 compat_elf_hwcap |= COMPAT_HWCAP_EVTSTRM;
813#endif
Julien Thierryec5c8e422017-10-13 14:32:55 +0100814 cpumask_set_cpu(smp_processor_id(), &evtstrm_available);
Nathan Lynche1ce5c72014-09-29 01:50:06 +0200815}
816
Will Deacon037f6372013-08-23 15:32:29 +0100817static void arch_timer_configure_evtstream(void)
818{
819 int evt_stream_div, pos;
820
821 /* Find the closest power of two to the divisor */
822 evt_stream_div = arch_timer_rate / ARCH_TIMER_EVT_STREAM_FREQ;
823 pos = fls(evt_stream_div);
824 if (pos > 1 && !(evt_stream_div & (1 << (pos - 2))))
825 pos--;
826 /* enable event stream */
827 arch_timer_evtstrm_enable(min(pos, 15));
828}
829
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200830static void arch_counter_set_user_access(void)
831{
832 u32 cntkctl = arch_timer_get_cntkctl();
833
Marc Zyngiera86bd132017-02-01 12:07:15 +0000834 /* Disable user access to the timers and both counters */
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200835 /* Also disable virtual event stream */
836 cntkctl &= ~(ARCH_TIMER_USR_PT_ACCESS_EN
837 | ARCH_TIMER_USR_VT_ACCESS_EN
Marc Zyngiera86bd132017-02-01 12:07:15 +0000838 | ARCH_TIMER_USR_VCT_ACCESS_EN
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200839 | ARCH_TIMER_VIRT_EVT_EN
840 | ARCH_TIMER_USR_PCT_ACCESS_EN);
841
Marc Zyngiera86bd132017-02-01 12:07:15 +0000842 /*
843 * Enable user access to the virtual counter if it doesn't
844 * need to be workaround. The vdso may have been already
845 * disabled though.
846 */
847 if (arch_timer_this_cpu_has_cntvct_wa())
848 pr_info("CPU%d: Trapping CNTVCT access\n", smp_processor_id());
849 else
850 cntkctl |= ARCH_TIMER_USR_VCT_ACCESS_EN;
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200851
852 arch_timer_set_cntkctl(cntkctl);
853}
854
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000855static bool arch_timer_has_nonsecure_ppi(void)
856{
Fu Weiee34f1e2017-01-18 21:25:27 +0800857 return (arch_timer_uses_ppi == ARCH_TIMER_PHYS_SECURE_PPI &&
858 arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000859}
860
Marc Zyngierf005bd72016-08-01 10:54:15 +0100861static u32 check_ppi_trigger(int irq)
862{
863 u32 flags = irq_get_trigger_type(irq);
864
865 if (flags != IRQF_TRIGGER_HIGH && flags != IRQF_TRIGGER_LOW) {
866 pr_warn("WARNING: Invalid trigger for IRQ%d, assuming level low\n", irq);
867 pr_warn("WARNING: Please fix your firmware\n");
868 flags = IRQF_TRIGGER_LOW;
869 }
870
871 return flags;
872}
873
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000874static int arch_timer_starting_cpu(unsigned int cpu)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000875{
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000876 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
Marc Zyngierf005bd72016-08-01 10:54:15 +0100877 u32 flags;
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000878
Fu Wei8a5c21d2017-01-18 21:25:26 +0800879 __arch_timer_setup(ARCH_TIMER_TYPE_CP15, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000880
Marc Zyngierf005bd72016-08-01 10:54:15 +0100881 flags = check_ppi_trigger(arch_timer_ppi[arch_timer_uses_ppi]);
882 enable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], flags);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000883
Marc Zyngierf005bd72016-08-01 10:54:15 +0100884 if (arch_timer_has_nonsecure_ppi()) {
Fu Weiee34f1e2017-01-18 21:25:27 +0800885 flags = check_ppi_trigger(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
886 enable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
887 flags);
Marc Zyngierf005bd72016-08-01 10:54:15 +0100888 }
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000889
890 arch_counter_set_user_access();
Will Deacon46fd5c62016-06-27 17:30:13 +0100891 if (evtstrm_enable)
Will Deacon037f6372013-08-23 15:32:29 +0100892 arch_timer_configure_evtstream();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000893
894 return 0;
895}
896
Fu Wei5d3dfa92017-03-22 00:31:13 +0800897/*
898 * For historical reasons, when probing with DT we use whichever (non-zero)
899 * rate was probed first, and don't verify that others match. If the first node
900 * probed has a clock-frequency property, this overrides the HW register.
901 */
902static void arch_timer_of_configure_rate(u32 rate, struct device_node *np)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000903{
Stephen Boyd22006992013-07-18 16:59:32 -0700904 /* Who has more than one independent system counter? */
905 if (arch_timer_rate)
906 return;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000907
Fu Wei5d3dfa92017-03-22 00:31:13 +0800908 if (of_property_read_u32(np, "clock-frequency", &arch_timer_rate))
909 arch_timer_rate = rate;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000910
Stephen Boyd22006992013-07-18 16:59:32 -0700911 /* Check the timer frequency. */
912 if (arch_timer_rate == 0)
Fu Weided24012017-01-18 21:25:25 +0800913 pr_warn("frequency not available\n");
Stephen Boyd22006992013-07-18 16:59:32 -0700914}
915
916static void arch_timer_banner(unsigned type)
917{
Fu Weided24012017-01-18 21:25:25 +0800918 pr_info("%s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
Fu Wei8a5c21d2017-01-18 21:25:26 +0800919 type & ARCH_TIMER_TYPE_CP15 ? "cp15" : "",
920 type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ?
921 " and " : "",
922 type & ARCH_TIMER_TYPE_MEM ? "mmio" : "",
Fu Weided24012017-01-18 21:25:25 +0800923 (unsigned long)arch_timer_rate / 1000000,
924 (unsigned long)(arch_timer_rate / 10000) % 100,
Fu Wei8a5c21d2017-01-18 21:25:26 +0800925 type & ARCH_TIMER_TYPE_CP15 ?
Fu Weiee34f1e2017-01-18 21:25:27 +0800926 (arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI) ? "virt" : "phys" :
Stephen Boyd22006992013-07-18 16:59:32 -0700927 "",
Fu Wei8a5c21d2017-01-18 21:25:26 +0800928 type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ? "/" : "",
929 type & ARCH_TIMER_TYPE_MEM ?
Stephen Boyd22006992013-07-18 16:59:32 -0700930 arch_timer_mem_use_virtual ? "virt" : "phys" :
931 "");
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000932}
933
934u32 arch_timer_get_rate(void)
935{
936 return arch_timer_rate;
937}
938
Julien Thierryec5c8e422017-10-13 14:32:55 +0100939bool arch_timer_evtstrm_available(void)
940{
941 /*
942 * We might get called from a preemptible context. This is fine
943 * because availability of the event stream should be always the same
944 * for a preemptible context and context where we might resume a task.
945 */
946 return cpumask_test_cpu(raw_smp_processor_id(), &evtstrm_available);
947}
948
Stephen Boyd22006992013-07-18 16:59:32 -0700949static u64 arch_counter_get_cntvct_mem(void)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000950{
Stephen Boyd22006992013-07-18 16:59:32 -0700951 u32 vct_lo, vct_hi, tmp_hi;
952
953 do {
954 vct_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
955 vct_lo = readl_relaxed(arch_counter_base + CNTVCT_LO);
956 tmp_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
957 } while (vct_hi != tmp_hi);
958
959 return ((u64) vct_hi << 32) | vct_lo;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000960}
961
Julien Grallb4d6ce92016-04-11 16:32:51 +0100962static struct arch_timer_kvm_info arch_timer_kvm_info;
963
964struct arch_timer_kvm_info *arch_timer_get_kvm_info(void)
965{
966 return &arch_timer_kvm_info;
967}
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000968
Stephen Boyd22006992013-07-18 16:59:32 -0700969static void __init arch_counter_register(unsigned type)
970{
971 u64 start_count;
972
973 /* Register the CP15 based counter if we have one */
Fu Wei8a5c21d2017-01-18 21:25:26 +0800974 if (type & ARCH_TIMER_TYPE_CP15) {
Marc Zyngier0ea41532019-04-08 16:49:07 +0100975 u64 (*rd)(void);
Scott Woodf6dc1572016-09-22 03:35:17 -0500976
Marc Zyngier0ea41532019-04-08 16:49:07 +0100977 if ((IS_ENABLED(CONFIG_ARM64) && !is_hyp_mode_available()) ||
978 arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI) {
979 if (arch_timer_counter_has_wa())
980 rd = arch_counter_get_cntvct_stable;
981 else
982 rd = arch_counter_get_cntvct;
983 } else {
984 if (arch_timer_counter_has_wa())
985 rd = arch_counter_get_cntpct_stable;
986 else
987 rd = arch_counter_get_cntpct;
988 }
989
990 arch_timer_read_counter = rd;
Marc Zyngiera86bd132017-02-01 12:07:15 +0000991 clocksource_counter.archdata.vdso_direct = vdso_default;
Nathan Lynch423bd692014-09-29 01:50:06 +0200992 } else {
Stephen Boyd22006992013-07-18 16:59:32 -0700993 arch_timer_read_counter = arch_counter_get_cntvct_mem;
Nathan Lynch423bd692014-09-29 01:50:06 +0200994 }
995
Brian Norrisd8ec7592016-10-04 11:12:09 -0700996 if (!arch_counter_suspend_stop)
997 clocksource_counter.flags |= CLOCK_SOURCE_SUSPEND_NONSTOP;
Stephen Boyd22006992013-07-18 16:59:32 -0700998 start_count = arch_timer_read_counter();
999 clocksource_register_hz(&clocksource_counter, arch_timer_rate);
1000 cyclecounter.mult = clocksource_counter.mult;
1001 cyclecounter.shift = clocksource_counter.shift;
Julien Grallb4d6ce92016-04-11 16:32:51 +01001002 timecounter_init(&arch_timer_kvm_info.timecounter,
1003 &cyclecounter, start_count);
Thierry Reding4a7d3e82013-10-15 15:31:51 +02001004
1005 /* 56 bits minimum, so we assume worst case rollover */
1006 sched_clock_register(arch_timer_read_counter, 56, arch_timer_rate);
Stephen Boyd22006992013-07-18 16:59:32 -07001007}
1008
Paul Gortmaker8c37bb32013-06-19 11:32:08 -04001009static void arch_timer_stop(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001010{
Fu Weided24012017-01-18 21:25:25 +08001011 pr_debug("disable IRQ%d cpu #%d\n", clk->irq, smp_processor_id());
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001012
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001013 disable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi]);
1014 if (arch_timer_has_nonsecure_ppi())
Fu Weiee34f1e2017-01-18 21:25:27 +08001015 disable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001016
Viresh Kumar46c5bfd2015-06-12 13:30:12 +05301017 clk->set_state_shutdown(clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001018}
1019
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001020static int arch_timer_dying_cpu(unsigned int cpu)
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001021{
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001022 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001023
Julien Thierryec5c8e422017-10-13 14:32:55 +01001024 cpumask_clear_cpu(smp_processor_id(), &evtstrm_available);
1025
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001026 arch_timer_stop(clk);
1027 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001028}
1029
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001030#ifdef CONFIG_CPU_PM
Marc Zyngierbee67c52017-04-04 17:05:16 +01001031static DEFINE_PER_CPU(unsigned long, saved_cntkctl);
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001032static int arch_timer_cpu_pm_notify(struct notifier_block *self,
1033 unsigned long action, void *hcpu)
1034{
Julien Thierryec5c8e422017-10-13 14:32:55 +01001035 if (action == CPU_PM_ENTER) {
Marc Zyngierbee67c52017-04-04 17:05:16 +01001036 __this_cpu_write(saved_cntkctl, arch_timer_get_cntkctl());
Julien Thierryec5c8e422017-10-13 14:32:55 +01001037
1038 cpumask_clear_cpu(smp_processor_id(), &evtstrm_available);
1039 } else if (action == CPU_PM_ENTER_FAILED || action == CPU_PM_EXIT) {
Marc Zyngierbee67c52017-04-04 17:05:16 +01001040 arch_timer_set_cntkctl(__this_cpu_read(saved_cntkctl));
Julien Thierryec5c8e422017-10-13 14:32:55 +01001041
1042 if (elf_hwcap & HWCAP_EVTSTRM)
1043 cpumask_set_cpu(smp_processor_id(), &evtstrm_available);
1044 }
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001045 return NOTIFY_OK;
1046}
1047
1048static struct notifier_block arch_timer_cpu_pm_notifier = {
1049 .notifier_call = arch_timer_cpu_pm_notify,
1050};
1051
1052static int __init arch_timer_cpu_pm_init(void)
1053{
1054 return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier);
1055}
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001056
1057static void __init arch_timer_cpu_pm_deinit(void)
1058{
1059 WARN_ON(cpu_pm_unregister_notifier(&arch_timer_cpu_pm_notifier));
1060}
1061
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001062#else
1063static int __init arch_timer_cpu_pm_init(void)
1064{
1065 return 0;
1066}
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001067
1068static void __init arch_timer_cpu_pm_deinit(void)
1069{
1070}
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001071#endif
1072
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001073static int __init arch_timer_register(void)
1074{
1075 int err;
1076 int ppi;
1077
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001078 arch_timer_evt = alloc_percpu(struct clock_event_device);
1079 if (!arch_timer_evt) {
1080 err = -ENOMEM;
1081 goto out;
1082 }
1083
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001084 ppi = arch_timer_ppi[arch_timer_uses_ppi];
1085 switch (arch_timer_uses_ppi) {
Fu Weiee34f1e2017-01-18 21:25:27 +08001086 case ARCH_TIMER_VIRT_PPI:
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001087 err = request_percpu_irq(ppi, arch_timer_handler_virt,
1088 "arch_timer", arch_timer_evt);
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001089 break;
Fu Weiee34f1e2017-01-18 21:25:27 +08001090 case ARCH_TIMER_PHYS_SECURE_PPI:
1091 case ARCH_TIMER_PHYS_NONSECURE_PPI:
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001092 err = request_percpu_irq(ppi, arch_timer_handler_phys,
1093 "arch_timer", arch_timer_evt);
Fu Wei4502b6b2017-01-18 21:25:30 +08001094 if (!err && arch_timer_has_nonsecure_ppi()) {
Fu Weiee34f1e2017-01-18 21:25:27 +08001095 ppi = arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI];
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001096 err = request_percpu_irq(ppi, arch_timer_handler_phys,
1097 "arch_timer", arch_timer_evt);
1098 if (err)
Fu Weiee34f1e2017-01-18 21:25:27 +08001099 free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_SECURE_PPI],
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001100 arch_timer_evt);
1101 }
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001102 break;
Fu Weiee34f1e2017-01-18 21:25:27 +08001103 case ARCH_TIMER_HYP_PPI:
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001104 err = request_percpu_irq(ppi, arch_timer_handler_phys,
1105 "arch_timer", arch_timer_evt);
1106 break;
1107 default:
1108 BUG();
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001109 }
1110
1111 if (err) {
Fu Weided24012017-01-18 21:25:25 +08001112 pr_err("can't register interrupt %d (%d)\n", ppi, err);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001113 goto out_free;
1114 }
1115
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001116 err = arch_timer_cpu_pm_init();
1117 if (err)
1118 goto out_unreg_notify;
1119
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001120 /* Register and immediately configure the timer on the boot CPU */
1121 err = cpuhp_setup_state(CPUHP_AP_ARM_ARCH_TIMER_STARTING,
Thomas Gleixner73c1b412016-12-21 20:19:54 +01001122 "clockevents/arm/arch_timer:starting",
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001123 arch_timer_starting_cpu, arch_timer_dying_cpu);
1124 if (err)
1125 goto out_unreg_cpupm;
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001126 return 0;
1127
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001128out_unreg_cpupm:
1129 arch_timer_cpu_pm_deinit();
1130
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001131out_unreg_notify:
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001132 free_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], arch_timer_evt);
1133 if (arch_timer_has_nonsecure_ppi())
Fu Weiee34f1e2017-01-18 21:25:27 +08001134 free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001135 arch_timer_evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001136
1137out_free:
1138 free_percpu(arch_timer_evt);
1139out:
1140 return err;
1141}
1142
Stephen Boyd22006992013-07-18 16:59:32 -07001143static int __init arch_timer_mem_register(void __iomem *base, unsigned int irq)
1144{
1145 int ret;
1146 irq_handler_t func;
1147 struct arch_timer *t;
1148
1149 t = kzalloc(sizeof(*t), GFP_KERNEL);
1150 if (!t)
1151 return -ENOMEM;
1152
1153 t->base = base;
1154 t->evt.irq = irq;
Fu Wei8a5c21d2017-01-18 21:25:26 +08001155 __arch_timer_setup(ARCH_TIMER_TYPE_MEM, &t->evt);
Stephen Boyd22006992013-07-18 16:59:32 -07001156
1157 if (arch_timer_mem_use_virtual)
1158 func = arch_timer_handler_virt_mem;
1159 else
1160 func = arch_timer_handler_phys_mem;
1161
1162 ret = request_irq(irq, func, IRQF_TIMER, "arch_mem_timer", &t->evt);
1163 if (ret) {
Fu Weided24012017-01-18 21:25:25 +08001164 pr_err("Failed to request mem timer irq\n");
Stephen Boyd22006992013-07-18 16:59:32 -07001165 kfree(t);
1166 }
1167
1168 return ret;
1169}
1170
1171static const struct of_device_id arch_timer_of_match[] __initconst = {
1172 { .compatible = "arm,armv7-timer", },
1173 { .compatible = "arm,armv8-timer", },
1174 {},
1175};
1176
1177static const struct of_device_id arch_timer_mem_of_match[] __initconst = {
1178 { .compatible = "arm,armv7-timer-mem", },
1179 {},
1180};
1181
Fu Wei13bf6992017-03-22 00:31:14 +08001182static bool __init arch_timer_needs_of_probing(void)
Sudeep Hollac387f072014-09-29 01:50:05 +02001183{
1184 struct device_node *dn;
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001185 bool needs_probing = false;
Fu Wei13bf6992017-03-22 00:31:14 +08001186 unsigned int mask = ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM;
Sudeep Hollac387f072014-09-29 01:50:05 +02001187
Fu Wei13bf6992017-03-22 00:31:14 +08001188 /* We have two timers, and both device-tree nodes are probed. */
1189 if ((arch_timers_present & mask) == mask)
1190 return false;
1191
1192 /*
1193 * Only one type of timer is probed,
1194 * check if we have another type of timer node in device-tree.
1195 */
1196 if (arch_timers_present & ARCH_TIMER_TYPE_CP15)
1197 dn = of_find_matching_node(NULL, arch_timer_mem_of_match);
1198 else
1199 dn = of_find_matching_node(NULL, arch_timer_of_match);
1200
1201 if (dn && of_device_is_available(dn))
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001202 needs_probing = true;
Fu Wei13bf6992017-03-22 00:31:14 +08001203
Sudeep Hollac387f072014-09-29 01:50:05 +02001204 of_node_put(dn);
1205
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001206 return needs_probing;
Sudeep Hollac387f072014-09-29 01:50:05 +02001207}
1208
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001209static int __init arch_timer_common_init(void)
Stephen Boyd22006992013-07-18 16:59:32 -07001210{
Stephen Boyd22006992013-07-18 16:59:32 -07001211 arch_timer_banner(arch_timers_present);
1212 arch_counter_register(arch_timers_present);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001213 return arch_timer_arch_init();
Stephen Boyd22006992013-07-18 16:59:32 -07001214}
1215
Fu Wei4502b6b2017-01-18 21:25:30 +08001216/**
1217 * arch_timer_select_ppi() - Select suitable PPI for the current system.
1218 *
1219 * If HYP mode is available, we know that the physical timer
1220 * has been configured to be accessible from PL1. Use it, so
1221 * that a guest can use the virtual timer instead.
1222 *
1223 * On ARMv8.1 with VH extensions, the kernel runs in HYP. VHE
1224 * accesses to CNTP_*_EL1 registers are silently redirected to
1225 * their CNTHP_*_EL2 counterparts, and use a different PPI
1226 * number.
1227 *
1228 * If no interrupt provided for virtual timer, we'll have to
1229 * stick to the physical timer. It'd better be accessible...
1230 * For arm64 we never use the secure interrupt.
1231 *
1232 * Return: a suitable PPI type for the current system.
1233 */
1234static enum arch_timer_ppi_nr __init arch_timer_select_ppi(void)
1235{
1236 if (is_kernel_in_hyp_mode())
1237 return ARCH_TIMER_HYP_PPI;
1238
1239 if (!is_hyp_mode_available() && arch_timer_ppi[ARCH_TIMER_VIRT_PPI])
1240 return ARCH_TIMER_VIRT_PPI;
1241
1242 if (IS_ENABLED(CONFIG_ARM64))
1243 return ARCH_TIMER_PHYS_NONSECURE_PPI;
1244
1245 return ARCH_TIMER_PHYS_SECURE_PPI;
1246}
1247
Andre Przywaraee793042018-07-06 09:11:50 +01001248static void __init arch_timer_populate_kvm_info(void)
1249{
1250 arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI];
1251 if (is_kernel_in_hyp_mode())
1252 arch_timer_kvm_info.physical_irq = arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI];
1253}
1254
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001255static int __init arch_timer_of_init(struct device_node *np)
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001256{
Fu Weica0e1b52017-03-22 00:31:15 +08001257 int i, ret;
Fu Wei5d3dfa92017-03-22 00:31:13 +08001258 u32 rate;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001259
Fu Wei8a5c21d2017-01-18 21:25:26 +08001260 if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
Fu Weided24012017-01-18 21:25:25 +08001261 pr_warn("multiple nodes in dt, skipping\n");
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001262 return 0;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001263 }
1264
Fu Wei8a5c21d2017-01-18 21:25:26 +08001265 arch_timers_present |= ARCH_TIMER_TYPE_CP15;
Fu Weiee34f1e2017-01-18 21:25:27 +08001266 for (i = ARCH_TIMER_PHYS_SECURE_PPI; i < ARCH_TIMER_MAX_TIMER_PPI; i++)
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001267 arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
1268
Andre Przywaraee793042018-07-06 09:11:50 +01001269 arch_timer_populate_kvm_info();
Fu Weica0e1b52017-03-22 00:31:15 +08001270
Fu Weic389d702017-04-01 01:51:00 +08001271 rate = arch_timer_get_cntfrq();
Fu Wei5d3dfa92017-03-22 00:31:13 +08001272 arch_timer_of_configure_rate(rate, np);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001273
1274 arch_timer_c3stop = !of_property_read_bool(np, "always-on");
1275
Marc Zyngier651bb2e2017-01-19 17:20:59 +00001276 /* Check for globally applicable workarounds */
1277 arch_timer_check_ool_workaround(ate_match_dt, np);
Scott Woodf6dc1572016-09-22 03:35:17 -05001278
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001279 /*
1280 * If we cannot rely on firmware initializing the timer registers then
1281 * we should use the physical timers instead.
1282 */
1283 if (IS_ENABLED(CONFIG_ARM) &&
1284 of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
Fu Weiee34f1e2017-01-18 21:25:27 +08001285 arch_timer_uses_ppi = ARCH_TIMER_PHYS_SECURE_PPI;
Fu Wei4502b6b2017-01-18 21:25:30 +08001286 else
1287 arch_timer_uses_ppi = arch_timer_select_ppi();
1288
1289 if (!arch_timer_ppi[arch_timer_uses_ppi]) {
1290 pr_err("No interrupt available, giving up\n");
1291 return -EINVAL;
1292 }
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001293
Brian Norrisd8ec7592016-10-04 11:12:09 -07001294 /* On some systems, the counter stops ticking when in suspend. */
1295 arch_counter_suspend_stop = of_property_read_bool(np,
1296 "arm,no-tick-in-suspend");
1297
Fu Weica0e1b52017-03-22 00:31:15 +08001298 ret = arch_timer_register();
1299 if (ret)
1300 return ret;
1301
1302 if (arch_timer_needs_of_probing())
1303 return 0;
1304
1305 return arch_timer_common_init();
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001306}
Daniel Lezcano17273392017-05-26 16:56:11 +02001307TIMER_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_of_init);
1308TIMER_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_of_init);
Stephen Boyd22006992013-07-18 16:59:32 -07001309
Fu Weic389d702017-04-01 01:51:00 +08001310static u32 __init
1311arch_timer_mem_frame_get_cntfrq(struct arch_timer_mem_frame *frame)
Stephen Boyd22006992013-07-18 16:59:32 -07001312{
Fu Weic389d702017-04-01 01:51:00 +08001313 void __iomem *base;
1314 u32 rate;
Stephen Boyd22006992013-07-18 16:59:32 -07001315
Fu Weic389d702017-04-01 01:51:00 +08001316 base = ioremap(frame->cntbase, frame->size);
1317 if (!base) {
1318 pr_err("Unable to map frame @ %pa\n", &frame->cntbase);
1319 return 0;
1320 }
1321
Frank Rowand3db12002017-06-09 17:26:32 -07001322 rate = readl_relaxed(base + CNTFRQ);
Fu Weic389d702017-04-01 01:51:00 +08001323
Frank Rowand3db12002017-06-09 17:26:32 -07001324 iounmap(base);
Fu Weic389d702017-04-01 01:51:00 +08001325
1326 return rate;
1327}
1328
1329static struct arch_timer_mem_frame * __init
1330arch_timer_mem_find_best_frame(struct arch_timer_mem *timer_mem)
1331{
1332 struct arch_timer_mem_frame *frame, *best_frame = NULL;
1333 void __iomem *cntctlbase;
1334 u32 cnttidr;
1335 int i;
1336
1337 cntctlbase = ioremap(timer_mem->cntctlbase, timer_mem->size);
Stephen Boyd22006992013-07-18 16:59:32 -07001338 if (!cntctlbase) {
Fu Weic389d702017-04-01 01:51:00 +08001339 pr_err("Can't map CNTCTLBase @ %pa\n",
1340 &timer_mem->cntctlbase);
1341 return NULL;
Stephen Boyd22006992013-07-18 16:59:32 -07001342 }
1343
1344 cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
Stephen Boyd22006992013-07-18 16:59:32 -07001345
1346 /*
1347 * Try to find a virtual capable frame. Otherwise fall back to a
1348 * physical capable frame.
1349 */
Fu Weic389d702017-04-01 01:51:00 +08001350 for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
1351 u32 cntacr = CNTACR_RFRQ | CNTACR_RWPT | CNTACR_RPCT |
1352 CNTACR_RWVT | CNTACR_RVOFF | CNTACR_RVCT;
Stephen Boyd22006992013-07-18 16:59:32 -07001353
Fu Weic389d702017-04-01 01:51:00 +08001354 frame = &timer_mem->frame[i];
1355 if (!frame->valid)
1356 continue;
Stephen Boyd22006992013-07-18 16:59:32 -07001357
Robin Murphye392d602016-02-01 12:00:48 +00001358 /* Try enabling everything, and see what sticks */
Fu Weic389d702017-04-01 01:51:00 +08001359 writel_relaxed(cntacr, cntctlbase + CNTACR(i));
1360 cntacr = readl_relaxed(cntctlbase + CNTACR(i));
Robin Murphye392d602016-02-01 12:00:48 +00001361
Fu Weic389d702017-04-01 01:51:00 +08001362 if ((cnttidr & CNTTIDR_VIRT(i)) &&
Robin Murphye392d602016-02-01 12:00:48 +00001363 !(~cntacr & (CNTACR_RWVT | CNTACR_RVCT))) {
Stephen Boyd22006992013-07-18 16:59:32 -07001364 best_frame = frame;
1365 arch_timer_mem_use_virtual = true;
1366 break;
1367 }
Robin Murphye392d602016-02-01 12:00:48 +00001368
1369 if (~cntacr & (CNTACR_RWPT | CNTACR_RPCT))
1370 continue;
1371
Fu Weic389d702017-04-01 01:51:00 +08001372 best_frame = frame;
Stephen Boyd22006992013-07-18 16:59:32 -07001373 }
1374
Fu Weic389d702017-04-01 01:51:00 +08001375 iounmap(cntctlbase);
1376
Sudeep Hollaf63d9472017-05-08 13:32:27 +01001377 return best_frame;
Fu Weic389d702017-04-01 01:51:00 +08001378}
1379
1380static int __init
1381arch_timer_mem_frame_register(struct arch_timer_mem_frame *frame)
1382{
1383 void __iomem *base;
1384 int ret, irq = 0;
Stephen Boyd22006992013-07-18 16:59:32 -07001385
1386 if (arch_timer_mem_use_virtual)
Fu Weic389d702017-04-01 01:51:00 +08001387 irq = frame->virt_irq;
Stephen Boyd22006992013-07-18 16:59:32 -07001388 else
Fu Weic389d702017-04-01 01:51:00 +08001389 irq = frame->phys_irq;
Robin Murphye392d602016-02-01 12:00:48 +00001390
Stephen Boyd22006992013-07-18 16:59:32 -07001391 if (!irq) {
Fu Weided24012017-01-18 21:25:25 +08001392 pr_err("Frame missing %s irq.\n",
Thomas Gleixnercfb6d652013-08-21 14:59:23 +02001393 arch_timer_mem_use_virtual ? "virt" : "phys");
Fu Weic389d702017-04-01 01:51:00 +08001394 return -EINVAL;
1395 }
1396
1397 if (!request_mem_region(frame->cntbase, frame->size,
1398 "arch_mem_timer"))
1399 return -EBUSY;
1400
1401 base = ioremap(frame->cntbase, frame->size);
1402 if (!base) {
1403 pr_err("Can't map frame's registers\n");
1404 return -ENXIO;
1405 }
1406
1407 ret = arch_timer_mem_register(base, irq);
1408 if (ret) {
1409 iounmap(base);
1410 return ret;
1411 }
1412
1413 arch_counter_base = base;
1414 arch_timers_present |= ARCH_TIMER_TYPE_MEM;
1415
1416 return 0;
1417}
1418
1419static int __init arch_timer_mem_of_init(struct device_node *np)
1420{
1421 struct arch_timer_mem *timer_mem;
1422 struct arch_timer_mem_frame *frame;
1423 struct device_node *frame_node;
1424 struct resource res;
1425 int ret = -EINVAL;
1426 u32 rate;
1427
1428 timer_mem = kzalloc(sizeof(*timer_mem), GFP_KERNEL);
1429 if (!timer_mem)
1430 return -ENOMEM;
1431
1432 if (of_address_to_resource(np, 0, &res))
1433 goto out;
1434 timer_mem->cntctlbase = res.start;
1435 timer_mem->size = resource_size(&res);
1436
1437 for_each_available_child_of_node(np, frame_node) {
1438 u32 n;
1439 struct arch_timer_mem_frame *frame;
1440
1441 if (of_property_read_u32(frame_node, "frame-number", &n)) {
1442 pr_err(FW_BUG "Missing frame-number.\n");
1443 of_node_put(frame_node);
1444 goto out;
1445 }
1446 if (n >= ARCH_TIMER_MEM_MAX_FRAMES) {
1447 pr_err(FW_BUG "Wrong frame-number, only 0-%u are permitted.\n",
1448 ARCH_TIMER_MEM_MAX_FRAMES - 1);
1449 of_node_put(frame_node);
1450 goto out;
1451 }
1452 frame = &timer_mem->frame[n];
1453
1454 if (frame->valid) {
1455 pr_err(FW_BUG "Duplicated frame-number.\n");
1456 of_node_put(frame_node);
1457 goto out;
1458 }
1459
1460 if (of_address_to_resource(frame_node, 0, &res)) {
1461 of_node_put(frame_node);
1462 goto out;
1463 }
1464 frame->cntbase = res.start;
1465 frame->size = resource_size(&res);
1466
1467 frame->virt_irq = irq_of_parse_and_map(frame_node,
1468 ARCH_TIMER_VIRT_SPI);
1469 frame->phys_irq = irq_of_parse_and_map(frame_node,
1470 ARCH_TIMER_PHYS_SPI);
1471
1472 frame->valid = true;
1473 }
1474
1475 frame = arch_timer_mem_find_best_frame(timer_mem);
1476 if (!frame) {
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001477 pr_err("Unable to find a suitable frame in timer @ %pa\n",
1478 &timer_mem->cntctlbase);
Fu Weic389d702017-04-01 01:51:00 +08001479 ret = -EINVAL;
Robin Murphye392d602016-02-01 12:00:48 +00001480 goto out;
Stephen Boyd22006992013-07-18 16:59:32 -07001481 }
1482
Fu Weic389d702017-04-01 01:51:00 +08001483 rate = arch_timer_mem_frame_get_cntfrq(frame);
Fu Wei5d3dfa92017-03-22 00:31:13 +08001484 arch_timer_of_configure_rate(rate, np);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001485
Fu Weic389d702017-04-01 01:51:00 +08001486 ret = arch_timer_mem_frame_register(frame);
1487 if (!ret && !arch_timer_needs_of_probing())
Fu Weica0e1b52017-03-22 00:31:15 +08001488 ret = arch_timer_common_init();
Robin Murphye392d602016-02-01 12:00:48 +00001489out:
Fu Weic389d702017-04-01 01:51:00 +08001490 kfree(timer_mem);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001491 return ret;
Stephen Boyd22006992013-07-18 16:59:32 -07001492}
Daniel Lezcano17273392017-05-26 16:56:11 +02001493TIMER_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem",
Fu Weic389d702017-04-01 01:51:00 +08001494 arch_timer_mem_of_init);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001495
Fu Weif79d2092017-04-01 01:51:02 +08001496#ifdef CONFIG_ACPI_GTDT
Fu Weic2743a32017-04-01 01:51:04 +08001497static int __init
1498arch_timer_mem_verify_cntfrq(struct arch_timer_mem *timer_mem)
1499{
1500 struct arch_timer_mem_frame *frame;
1501 u32 rate;
1502 int i;
1503
1504 for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
1505 frame = &timer_mem->frame[i];
1506
1507 if (!frame->valid)
1508 continue;
1509
1510 rate = arch_timer_mem_frame_get_cntfrq(frame);
1511 if (rate == arch_timer_rate)
1512 continue;
1513
1514 pr_err(FW_BUG "CNTFRQ mismatch: frame @ %pa: (0x%08lx), CPU: (0x%08lx)\n",
1515 &frame->cntbase,
1516 (unsigned long)rate, (unsigned long)arch_timer_rate);
1517
1518 return -EINVAL;
1519 }
1520
1521 return 0;
1522}
1523
1524static int __init arch_timer_mem_acpi_init(int platform_timer_count)
1525{
1526 struct arch_timer_mem *timers, *timer;
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001527 struct arch_timer_mem_frame *frame, *best_frame = NULL;
Fu Weic2743a32017-04-01 01:51:04 +08001528 int timer_count, i, ret = 0;
1529
1530 timers = kcalloc(platform_timer_count, sizeof(*timers),
1531 GFP_KERNEL);
1532 if (!timers)
1533 return -ENOMEM;
1534
1535 ret = acpi_arch_timer_mem_init(timers, &timer_count);
1536 if (ret || !timer_count)
1537 goto out;
1538
Fu Weic2743a32017-04-01 01:51:04 +08001539 /*
1540 * While unlikely, it's theoretically possible that none of the frames
1541 * in a timer expose the combination of feature we want.
1542 */
Matthias Kaehlcked197f792017-07-31 11:37:28 -07001543 for (i = 0; i < timer_count; i++) {
Fu Weic2743a32017-04-01 01:51:04 +08001544 timer = &timers[i];
1545
1546 frame = arch_timer_mem_find_best_frame(timer);
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001547 if (!best_frame)
1548 best_frame = frame;
1549
1550 ret = arch_timer_mem_verify_cntfrq(timer);
1551 if (ret) {
1552 pr_err("Disabling MMIO timers due to CNTFRQ mismatch\n");
1553 goto out;
1554 }
1555
1556 if (!best_frame) /* implies !frame */
1557 /*
1558 * Only complain about missing suitable frames if we
1559 * haven't already found one in a previous iteration.
1560 */
1561 pr_err("Unable to find a suitable frame in timer @ %pa\n",
1562 &timer->cntctlbase);
Fu Weic2743a32017-04-01 01:51:04 +08001563 }
1564
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001565 if (best_frame)
1566 ret = arch_timer_mem_frame_register(best_frame);
Fu Weic2743a32017-04-01 01:51:04 +08001567out:
1568 kfree(timers);
1569 return ret;
1570}
1571
1572/* Initialize per-processor generic timer and memory-mapped timer(if present) */
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001573static int __init arch_timer_acpi_init(struct acpi_table_header *table)
1574{
Fu Weic2743a32017-04-01 01:51:04 +08001575 int ret, platform_timer_count;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001576
Fu Wei8a5c21d2017-01-18 21:25:26 +08001577 if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
Fu Weided24012017-01-18 21:25:25 +08001578 pr_warn("already initialized, skipping\n");
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001579 return -EINVAL;
1580 }
1581
Fu Wei8a5c21d2017-01-18 21:25:26 +08001582 arch_timers_present |= ARCH_TIMER_TYPE_CP15;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001583
Fu Weic2743a32017-04-01 01:51:04 +08001584 ret = acpi_gtdt_init(table, &platform_timer_count);
Fu Weif79d2092017-04-01 01:51:02 +08001585 if (ret) {
1586 pr_err("Failed to init GTDT table.\n");
1587 return ret;
1588 }
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001589
Fu Weiee34f1e2017-01-18 21:25:27 +08001590 arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI] =
Fu Weif79d2092017-04-01 01:51:02 +08001591 acpi_gtdt_map_ppi(ARCH_TIMER_PHYS_NONSECURE_PPI);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001592
Fu Weiee34f1e2017-01-18 21:25:27 +08001593 arch_timer_ppi[ARCH_TIMER_VIRT_PPI] =
Fu Weif79d2092017-04-01 01:51:02 +08001594 acpi_gtdt_map_ppi(ARCH_TIMER_VIRT_PPI);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001595
Fu Weiee34f1e2017-01-18 21:25:27 +08001596 arch_timer_ppi[ARCH_TIMER_HYP_PPI] =
Fu Weif79d2092017-04-01 01:51:02 +08001597 acpi_gtdt_map_ppi(ARCH_TIMER_HYP_PPI);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001598
Andre Przywaraee793042018-07-06 09:11:50 +01001599 arch_timer_populate_kvm_info();
Fu Weica0e1b52017-03-22 00:31:15 +08001600
Fu Wei5d3dfa92017-03-22 00:31:13 +08001601 /*
1602 * When probing via ACPI, we have no mechanism to override the sysreg
1603 * CNTFRQ value. This *must* be correct.
1604 */
1605 arch_timer_rate = arch_timer_get_cntfrq();
1606 if (!arch_timer_rate) {
1607 pr_err(FW_BUG "frequency not available.\n");
1608 return -EINVAL;
1609 }
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001610
Fu Wei4502b6b2017-01-18 21:25:30 +08001611 arch_timer_uses_ppi = arch_timer_select_ppi();
1612 if (!arch_timer_ppi[arch_timer_uses_ppi]) {
1613 pr_err("No interrupt available, giving up\n");
1614 return -EINVAL;
1615 }
1616
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001617 /* Always-on capability */
Fu Weif79d2092017-04-01 01:51:02 +08001618 arch_timer_c3stop = acpi_gtdt_c3stop(arch_timer_uses_ppi);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001619
Marc Zyngier5a38bca2017-02-21 14:37:30 +00001620 /* Check for globally applicable workarounds */
1621 arch_timer_check_ool_workaround(ate_match_acpi_oem_info, table);
1622
Fu Weica0e1b52017-03-22 00:31:15 +08001623 ret = arch_timer_register();
1624 if (ret)
1625 return ret;
1626
Fu Weic2743a32017-04-01 01:51:04 +08001627 if (platform_timer_count &&
1628 arch_timer_mem_acpi_init(platform_timer_count))
1629 pr_err("Failed to initialize memory-mapped timer.\n");
1630
Fu Weica0e1b52017-03-22 00:31:15 +08001631 return arch_timer_common_init();
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001632}
Daniel Lezcano77d62f52017-05-26 17:42:25 +02001633TIMER_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001634#endif