blob: 5c69c9a9a6a4f58fdafdc669201a85960f9a0b04 [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
Yangtao Li91556972019-03-05 12:08:51 -050012#define pr_fmt(fmt) "arch_timer: " fmt
Marc Zyngierf005bd72016-08-01 10:54:15 +010013
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
Stephen Boyd22006992013-07-18 16:59:32 -070036#define CNTTIDR 0x08
37#define CNTTIDR_VIRT(n) (BIT(1) << ((n) * 4))
38
Robin Murphye392d602016-02-01 12:00:48 +000039#define CNTACR(n) (0x40 + ((n) * 4))
40#define CNTACR_RPCT BIT(0)
41#define CNTACR_RVCT BIT(1)
42#define CNTACR_RFRQ BIT(2)
43#define CNTACR_RVOFF BIT(3)
44#define CNTACR_RWVT BIT(4)
45#define CNTACR_RWPT BIT(5)
46
Stephen Boyd22006992013-07-18 16:59:32 -070047#define CNTVCT_LO 0x08
48#define CNTVCT_HI 0x0c
49#define CNTFRQ 0x10
50#define CNTP_TVAL 0x28
51#define CNTP_CTL 0x2c
52#define CNTV_TVAL 0x38
53#define CNTV_CTL 0x3c
54
Stephen Boyd22006992013-07-18 16:59:32 -070055static unsigned arch_timers_present __initdata;
56
57static void __iomem *arch_counter_base;
58
59struct arch_timer {
60 void __iomem *base;
61 struct clock_event_device evt;
62};
63
64#define to_arch_timer(e) container_of(e, struct arch_timer, evt)
65
Mark Rutland8a4da6e2012-11-12 14:33:44 +000066static u32 arch_timer_rate;
Fu Weiee34f1e2017-01-18 21:25:27 +080067static int arch_timer_ppi[ARCH_TIMER_MAX_TIMER_PPI];
Mark Rutland8a4da6e2012-11-12 14:33:44 +000068
69static struct clock_event_device __percpu *arch_timer_evt;
70
Fu Weiee34f1e2017-01-18 21:25:27 +080071static enum arch_timer_ppi_nr arch_timer_uses_ppi = ARCH_TIMER_VIRT_PPI;
Lorenzo Pieralisi82a561942014-04-08 10:04:32 +010072static bool arch_timer_c3stop;
Stephen Boyd22006992013-07-18 16:59:32 -070073static bool arch_timer_mem_use_virtual;
Brian Norrisd8ec7592016-10-04 11:12:09 -070074static bool arch_counter_suspend_stop;
Marc Zyngiera86bd132017-02-01 12:07:15 +000075static bool vdso_default = true;
Mark Rutland8a4da6e2012-11-12 14:33:44 +000076
Julien Thierryec5c8e42017-10-13 14:32:55 +010077static cpumask_t evtstrm_available = CPU_MASK_NONE;
Will Deacon46fd5c62016-06-27 17:30:13 +010078static bool evtstrm_enable = IS_ENABLED(CONFIG_ARM_ARCH_TIMER_EVTSTREAM);
79
80static int __init early_evtstrm_cfg(char *buf)
81{
82 return strtobool(buf, &evtstrm_enable);
83}
84early_param("clocksource.arm_arch_timer.evtstrm", early_evtstrm_cfg);
85
Mark Rutland8a4da6e2012-11-12 14:33:44 +000086/*
87 * Architected system timer support.
88 */
89
Marc Zyngierf4e00a12017-01-20 18:28:32 +000090static __always_inline
91void arch_timer_reg_write(int access, enum arch_timer_reg reg, u32 val,
92 struct clock_event_device *clk)
93{
94 if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
95 struct arch_timer *timer = to_arch_timer(clk);
96 switch (reg) {
97 case ARCH_TIMER_REG_CTRL:
98 writel_relaxed(val, timer->base + CNTP_CTL);
99 break;
100 case ARCH_TIMER_REG_TVAL:
101 writel_relaxed(val, timer->base + CNTP_TVAL);
102 break;
103 }
104 } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
105 struct arch_timer *timer = to_arch_timer(clk);
106 switch (reg) {
107 case ARCH_TIMER_REG_CTRL:
108 writel_relaxed(val, timer->base + CNTV_CTL);
109 break;
110 case ARCH_TIMER_REG_TVAL:
111 writel_relaxed(val, timer->base + CNTV_TVAL);
112 break;
113 }
114 } else {
115 arch_timer_reg_write_cp15(access, reg, val);
116 }
117}
118
119static __always_inline
120u32 arch_timer_reg_read(int access, enum arch_timer_reg reg,
121 struct clock_event_device *clk)
122{
123 u32 val;
124
125 if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
126 struct arch_timer *timer = to_arch_timer(clk);
127 switch (reg) {
128 case ARCH_TIMER_REG_CTRL:
129 val = readl_relaxed(timer->base + CNTP_CTL);
130 break;
131 case ARCH_TIMER_REG_TVAL:
132 val = readl_relaxed(timer->base + CNTP_TVAL);
133 break;
134 }
135 } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
136 struct arch_timer *timer = to_arch_timer(clk);
137 switch (reg) {
138 case ARCH_TIMER_REG_CTRL:
139 val = readl_relaxed(timer->base + CNTV_CTL);
140 break;
141 case ARCH_TIMER_REG_TVAL:
142 val = readl_relaxed(timer->base + CNTV_TVAL);
143 break;
144 }
145 } else {
146 val = arch_timer_reg_read_cp15(access, reg);
147 }
148
149 return val;
150}
151
Julien Thierry5d6168f2019-05-24 10:10:25 +0100152static notrace u64 arch_counter_get_cntpct_stable(void)
Marc Zyngier0ea41532019-04-08 16:49:07 +0100153{
154 return __arch_counter_get_cntpct_stable();
155}
156
Julien Thierry5d6168f2019-05-24 10:10:25 +0100157static notrace u64 arch_counter_get_cntpct(void)
Marc Zyngier0ea41532019-04-08 16:49:07 +0100158{
159 return __arch_counter_get_cntpct();
160}
161
Julien Thierry5d6168f2019-05-24 10:10:25 +0100162static notrace u64 arch_counter_get_cntvct_stable(void)
Marc Zyngier0ea41532019-04-08 16:49:07 +0100163{
164 return __arch_counter_get_cntvct_stable();
165}
166
Julien Thierry5d6168f2019-05-24 10:10:25 +0100167static notrace u64 arch_counter_get_cntvct(void)
Marc Zyngier0ea41532019-04-08 16:49:07 +0100168{
169 return __arch_counter_get_cntvct();
170}
171
Marc Zyngier992dd162017-02-01 11:53:46 +0000172/*
173 * Default to cp15 based access because arm64 uses this function for
174 * sched_clock() before DT is probed and the cp15 method is guaranteed
175 * to exist on arm64. arm doesn't use this before DT is probed so even
176 * if we don't have the cp15 accessors we won't have a problem.
177 */
178u64 (*arch_timer_read_counter)(void) = arch_counter_get_cntvct;
Christoffer Dalle6d68b002017-07-05 11:04:28 +0200179EXPORT_SYMBOL_GPL(arch_timer_read_counter);
Marc Zyngier992dd162017-02-01 11:53:46 +0000180
181static u64 arch_counter_read(struct clocksource *cs)
182{
183 return arch_timer_read_counter();
184}
185
186static u64 arch_counter_read_cc(const struct cyclecounter *cc)
187{
188 return arch_timer_read_counter();
189}
190
191static struct clocksource clocksource_counter = {
192 .name = "arch_sys_counter",
193 .rating = 400,
194 .read = arch_counter_read,
195 .mask = CLOCKSOURCE_MASK(56),
196 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
197};
198
199static struct cyclecounter cyclecounter __ro_after_init = {
200 .read = arch_counter_read_cc,
201 .mask = CLOCKSOURCE_MASK(56),
202};
203
Marc Zyngier5a38bca2017-02-21 14:37:30 +0000204struct ate_acpi_oem_info {
205 char oem_id[ACPI_OEM_ID_SIZE + 1];
206 char oem_table_id[ACPI_OEM_TABLE_ID_SIZE + 1];
207 u32 oem_revision;
208};
209
Scott Woodf6dc1572016-09-22 03:35:17 -0500210#ifdef CONFIG_FSL_ERRATUM_A008585
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000211/*
212 * The number of retries is an arbitrary value well beyond the highest number
213 * of iterations the loop has been observed to take.
214 */
215#define __fsl_a008585_read_reg(reg) ({ \
216 u64 _old, _new; \
217 int _retries = 200; \
218 \
219 do { \
220 _old = read_sysreg(reg); \
221 _new = read_sysreg(reg); \
222 _retries--; \
223 } while (unlikely(_old != _new) && _retries); \
224 \
225 WARN_ON_ONCE(!_retries); \
226 _new; \
227})
Scott Woodf6dc1572016-09-22 03:35:17 -0500228
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000229static u32 notrace fsl_a008585_read_cntp_tval_el0(void)
Scott Woodf6dc1572016-09-22 03:35:17 -0500230{
231 return __fsl_a008585_read_reg(cntp_tval_el0);
232}
233
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000234static u32 notrace fsl_a008585_read_cntv_tval_el0(void)
Scott Woodf6dc1572016-09-22 03:35:17 -0500235{
236 return __fsl_a008585_read_reg(cntv_tval_el0);
237}
238
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200239static u64 notrace fsl_a008585_read_cntpct_el0(void)
240{
241 return __fsl_a008585_read_reg(cntpct_el0);
242}
243
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000244static u64 notrace fsl_a008585_read_cntvct_el0(void)
Scott Woodf6dc1572016-09-22 03:35:17 -0500245{
246 return __fsl_a008585_read_reg(cntvct_el0);
247}
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000248#endif
249
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000250#ifdef CONFIG_HISILICON_ERRATUM_161010101
251/*
252 * Verify whether the value of the second read is larger than the first by
253 * less than 32 is the only way to confirm the value is correct, so clear the
254 * lower 5 bits to check whether the difference is greater than 32 or not.
255 * Theoretically the erratum should not occur more than twice in succession
256 * when reading the system counter, but it is possible that some interrupts
257 * may lead to more than twice read errors, triggering the warning, so setting
258 * the number of retries far beyond the number of iterations the loop has been
259 * observed to take.
260 */
261#define __hisi_161010101_read_reg(reg) ({ \
262 u64 _old, _new; \
263 int _retries = 50; \
264 \
265 do { \
266 _old = read_sysreg(reg); \
267 _new = read_sysreg(reg); \
268 _retries--; \
269 } while (unlikely((_new - _old) >> 5) && _retries); \
270 \
271 WARN_ON_ONCE(!_retries); \
272 _new; \
273})
274
275static u32 notrace hisi_161010101_read_cntp_tval_el0(void)
276{
277 return __hisi_161010101_read_reg(cntp_tval_el0);
278}
279
280static u32 notrace hisi_161010101_read_cntv_tval_el0(void)
281{
282 return __hisi_161010101_read_reg(cntv_tval_el0);
283}
284
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200285static u64 notrace hisi_161010101_read_cntpct_el0(void)
286{
287 return __hisi_161010101_read_reg(cntpct_el0);
288}
289
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000290static u64 notrace hisi_161010101_read_cntvct_el0(void)
291{
292 return __hisi_161010101_read_reg(cntvct_el0);
293}
Marc Zyngierd003d022017-02-21 15:04:27 +0000294
295static struct ate_acpi_oem_info hisi_161010101_oem_info[] = {
296 /*
297 * Note that trailing spaces are required to properly match
298 * the OEM table information.
299 */
300 {
301 .oem_id = "HISI ",
302 .oem_table_id = "HIP05 ",
303 .oem_revision = 0,
304 },
305 {
306 .oem_id = "HISI ",
307 .oem_table_id = "HIP06 ",
308 .oem_revision = 0,
309 },
310 {
311 .oem_id = "HISI ",
312 .oem_table_id = "HIP07 ",
313 .oem_revision = 0,
314 },
315 { /* Sentinel indicating the end of the OEM array */ },
316};
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000317#endif
318
Marc Zyngierfa8d8152017-01-27 12:52:31 +0000319#ifdef CONFIG_ARM64_ERRATUM_858921
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200320static u64 notrace arm64_858921_read_cntpct_el0(void)
321{
322 u64 old, new;
323
324 old = read_sysreg(cntpct_el0);
325 new = read_sysreg(cntpct_el0);
326 return (((old ^ new) >> 32) & 1) ? old : new;
327}
328
Marc Zyngierfa8d8152017-01-27 12:52:31 +0000329static u64 notrace arm64_858921_read_cntvct_el0(void)
330{
331 u64 old, new;
332
333 old = read_sysreg(cntvct_el0);
334 new = read_sysreg(cntvct_el0);
335 return (((old ^ new) >> 32) & 1) ? old : new;
336}
337#endif
338
Samuel Hollandc950ca82019-01-12 20:17:18 -0600339#ifdef CONFIG_SUN50I_ERRATUM_UNKNOWN1
340/*
341 * The low bits of the counter registers are indeterminate while bit 10 or
342 * greater is rolling over. Since the counter value can jump both backward
343 * (7ff -> 000 -> 800) and forward (7ff -> fff -> 800), ignore register values
344 * with all ones or all zeros in the low bits. Bound the loop by the maximum
345 * number of CPU cycles in 3 consecutive 24 MHz counter periods.
346 */
347#define __sun50i_a64_read_reg(reg) ({ \
348 u64 _val; \
349 int _retries = 150; \
350 \
351 do { \
352 _val = read_sysreg(reg); \
353 _retries--; \
354 } while (((_val + 1) & GENMASK(9, 0)) <= 1 && _retries); \
355 \
356 WARN_ON_ONCE(!_retries); \
357 _val; \
358})
359
360static u64 notrace sun50i_a64_read_cntpct_el0(void)
361{
362 return __sun50i_a64_read_reg(cntpct_el0);
363}
364
365static u64 notrace sun50i_a64_read_cntvct_el0(void)
366{
367 return __sun50i_a64_read_reg(cntvct_el0);
368}
369
370static u32 notrace sun50i_a64_read_cntp_tval_el0(void)
371{
372 return read_sysreg(cntp_cval_el0) - sun50i_a64_read_cntpct_el0();
373}
374
375static u32 notrace sun50i_a64_read_cntv_tval_el0(void)
376{
377 return read_sysreg(cntv_cval_el0) - sun50i_a64_read_cntvct_el0();
378}
379#endif
380
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000381#ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
Mark Rutlanda7fb4572017-10-16 16:28:39 +0100382DEFINE_PER_CPU(const struct arch_timer_erratum_workaround *, timer_unstable_counter_workaround);
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000383EXPORT_SYMBOL_GPL(timer_unstable_counter_workaround);
384
Marc Zyngier0ea41532019-04-08 16:49:07 +0100385static atomic_t timer_unstable_counter_workaround_in_use = ATOMIC_INIT(0);
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000386
Marc Zyngier83280892017-01-27 10:27:09 +0000387static void erratum_set_next_event_tval_generic(const int access, unsigned long evt,
388 struct clock_event_device *clk)
389{
390 unsigned long ctrl;
Christoffer Dalle6d68b002017-07-05 11:04:28 +0200391 u64 cval;
Marc Zyngier83280892017-01-27 10:27:09 +0000392
393 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
394 ctrl |= ARCH_TIMER_CTRL_ENABLE;
395 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
396
Christoffer Dalle6d68b002017-07-05 11:04:28 +0200397 if (access == ARCH_TIMER_PHYS_ACCESS) {
398 cval = evt + arch_counter_get_cntpct();
Marc Zyngier83280892017-01-27 10:27:09 +0000399 write_sysreg(cval, cntp_cval_el0);
Christoffer Dalle6d68b002017-07-05 11:04:28 +0200400 } else {
401 cval = evt + arch_counter_get_cntvct();
Marc Zyngier83280892017-01-27 10:27:09 +0000402 write_sysreg(cval, cntv_cval_el0);
Christoffer Dalle6d68b002017-07-05 11:04:28 +0200403 }
Marc Zyngier83280892017-01-27 10:27:09 +0000404
405 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
406}
407
Arnd Bergmanneb645222017-04-19 19:37:09 +0200408static __maybe_unused int erratum_set_next_event_tval_virt(unsigned long evt,
Marc Zyngier83280892017-01-27 10:27:09 +0000409 struct clock_event_device *clk)
410{
411 erratum_set_next_event_tval_generic(ARCH_TIMER_VIRT_ACCESS, evt, clk);
412 return 0;
413}
414
Arnd Bergmanneb645222017-04-19 19:37:09 +0200415static __maybe_unused int erratum_set_next_event_tval_phys(unsigned long evt,
Marc Zyngier83280892017-01-27 10:27:09 +0000416 struct clock_event_device *clk)
417{
418 erratum_set_next_event_tval_generic(ARCH_TIMER_PHYS_ACCESS, evt, clk);
419 return 0;
420}
421
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000422static const struct arch_timer_erratum_workaround ool_workarounds[] = {
423#ifdef CONFIG_FSL_ERRATUM_A008585
424 {
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000425 .match_type = ate_match_dt,
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000426 .id = "fsl,erratum-a008585",
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000427 .desc = "Freescale erratum a005858",
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000428 .read_cntp_tval_el0 = fsl_a008585_read_cntp_tval_el0,
429 .read_cntv_tval_el0 = fsl_a008585_read_cntv_tval_el0,
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200430 .read_cntpct_el0 = fsl_a008585_read_cntpct_el0,
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000431 .read_cntvct_el0 = fsl_a008585_read_cntvct_el0,
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000432 .set_next_event_phys = erratum_set_next_event_tval_phys,
433 .set_next_event_virt = erratum_set_next_event_tval_virt,
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000434 },
435#endif
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000436#ifdef CONFIG_HISILICON_ERRATUM_161010101
437 {
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000438 .match_type = ate_match_dt,
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000439 .id = "hisilicon,erratum-161010101",
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000440 .desc = "HiSilicon erratum 161010101",
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000441 .read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
442 .read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200443 .read_cntpct_el0 = hisi_161010101_read_cntpct_el0,
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000444 .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000445 .set_next_event_phys = erratum_set_next_event_tval_phys,
446 .set_next_event_virt = erratum_set_next_event_tval_virt,
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000447 },
Marc Zyngierd003d022017-02-21 15:04:27 +0000448 {
449 .match_type = ate_match_acpi_oem_info,
450 .id = hisi_161010101_oem_info,
451 .desc = "HiSilicon erratum 161010101",
452 .read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
453 .read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200454 .read_cntpct_el0 = hisi_161010101_read_cntpct_el0,
Marc Zyngierd003d022017-02-21 15:04:27 +0000455 .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
456 .set_next_event_phys = erratum_set_next_event_tval_phys,
457 .set_next_event_virt = erratum_set_next_event_tval_virt,
458 },
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000459#endif
Marc Zyngierfa8d8152017-01-27 12:52:31 +0000460#ifdef CONFIG_ARM64_ERRATUM_858921
461 {
462 .match_type = ate_match_local_cap_id,
463 .id = (void *)ARM64_WORKAROUND_858921,
464 .desc = "ARM erratum 858921",
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200465 .read_cntpct_el0 = arm64_858921_read_cntpct_el0,
Marc Zyngierfa8d8152017-01-27 12:52:31 +0000466 .read_cntvct_el0 = arm64_858921_read_cntvct_el0,
467 },
468#endif
Samuel Hollandc950ca82019-01-12 20:17:18 -0600469#ifdef CONFIG_SUN50I_ERRATUM_UNKNOWN1
470 {
471 .match_type = ate_match_dt,
472 .id = "allwinner,erratum-unknown1",
473 .desc = "Allwinner erratum UNKNOWN1",
474 .read_cntp_tval_el0 = sun50i_a64_read_cntp_tval_el0,
475 .read_cntv_tval_el0 = sun50i_a64_read_cntv_tval_el0,
476 .read_cntpct_el0 = sun50i_a64_read_cntpct_el0,
477 .read_cntvct_el0 = sun50i_a64_read_cntvct_el0,
478 .set_next_event_phys = erratum_set_next_event_tval_phys,
479 .set_next_event_virt = erratum_set_next_event_tval_virt,
480 },
481#endif
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000482};
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000483
484typedef bool (*ate_match_fn_t)(const struct arch_timer_erratum_workaround *,
485 const void *);
486
487static
488bool arch_timer_check_dt_erratum(const struct arch_timer_erratum_workaround *wa,
489 const void *arg)
490{
491 const struct device_node *np = arg;
492
493 return of_property_read_bool(np, wa->id);
494}
495
Marc Zyngier00640302017-03-20 16:47:59 +0000496static
497bool arch_timer_check_local_cap_erratum(const struct arch_timer_erratum_workaround *wa,
498 const void *arg)
499{
500 return this_cpu_has_cap((uintptr_t)wa->id);
501}
502
Marc Zyngier5a38bca2017-02-21 14:37:30 +0000503
504static
505bool arch_timer_check_acpi_oem_erratum(const struct arch_timer_erratum_workaround *wa,
506 const void *arg)
507{
508 static const struct ate_acpi_oem_info empty_oem_info = {};
509 const struct ate_acpi_oem_info *info = wa->id;
510 const struct acpi_table_header *table = arg;
511
512 /* Iterate over the ACPI OEM info array, looking for a match */
513 while (memcmp(info, &empty_oem_info, sizeof(*info))) {
514 if (!memcmp(info->oem_id, table->oem_id, ACPI_OEM_ID_SIZE) &&
515 !memcmp(info->oem_table_id, table->oem_table_id, ACPI_OEM_TABLE_ID_SIZE) &&
516 info->oem_revision == table->oem_revision)
517 return true;
518
519 info++;
520 }
521
522 return false;
523}
524
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000525static const struct arch_timer_erratum_workaround *
526arch_timer_iterate_errata(enum arch_timer_erratum_match_type type,
527 ate_match_fn_t match_fn,
528 void *arg)
529{
530 int i;
531
532 for (i = 0; i < ARRAY_SIZE(ool_workarounds); i++) {
533 if (ool_workarounds[i].match_type != type)
534 continue;
535
536 if (match_fn(&ool_workarounds[i], arg))
537 return &ool_workarounds[i];
538 }
539
540 return NULL;
541}
542
543static
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000544void arch_timer_enable_workaround(const struct arch_timer_erratum_workaround *wa,
545 bool local)
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000546{
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000547 int i;
548
549 if (local) {
550 __this_cpu_write(timer_unstable_counter_workaround, wa);
551 } else {
552 for_each_possible_cpu(i)
553 per_cpu(timer_unstable_counter_workaround, i) = wa;
554 }
555
Marc Zyngier0ea41532019-04-08 16:49:07 +0100556 if (wa->read_cntvct_el0 || wa->read_cntpct_el0)
557 atomic_set(&timer_unstable_counter_workaround_in_use, 1);
Marc Zyngiera86bd132017-02-01 12:07:15 +0000558
559 /*
560 * Don't use the vdso fastpath if errata require using the
561 * out-of-line counter accessor. We may change our mind pretty
562 * late in the game (with a per-CPU erratum, for example), so
563 * change both the default value and the vdso itself.
564 */
565 if (wa->read_cntvct_el0) {
566 clocksource_counter.archdata.vdso_direct = false;
567 vdso_default = false;
568 }
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000569}
570
571static void arch_timer_check_ool_workaround(enum arch_timer_erratum_match_type type,
572 void *arg)
573{
Marc Zyngiera862fc22019-04-08 16:49:06 +0100574 const struct arch_timer_erratum_workaround *wa, *__wa;
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000575 ate_match_fn_t match_fn = NULL;
Marc Zyngier00640302017-03-20 16:47:59 +0000576 bool local = false;
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000577
578 switch (type) {
579 case ate_match_dt:
580 match_fn = arch_timer_check_dt_erratum;
581 break;
Marc Zyngier00640302017-03-20 16:47:59 +0000582 case ate_match_local_cap_id:
583 match_fn = arch_timer_check_local_cap_erratum;
584 local = true;
585 break;
Marc Zyngier5a38bca2017-02-21 14:37:30 +0000586 case ate_match_acpi_oem_info:
587 match_fn = arch_timer_check_acpi_oem_erratum;
588 break;
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000589 default:
590 WARN_ON(1);
591 return;
592 }
593
594 wa = arch_timer_iterate_errata(type, match_fn, arg);
595 if (!wa)
596 return;
597
Marc Zyngiera862fc22019-04-08 16:49:06 +0100598 __wa = __this_cpu_read(timer_unstable_counter_workaround);
599 if (__wa && wa != __wa)
600 pr_warn("Can't enable workaround for %s (clashes with %s\n)",
601 wa->desc, __wa->desc);
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000602
Marc Zyngiera862fc22019-04-08 16:49:06 +0100603 if (__wa)
604 return;
Marc Zyngier00640302017-03-20 16:47:59 +0000605
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000606 arch_timer_enable_workaround(wa, local);
Marc Zyngier00640302017-03-20 16:47:59 +0000607 pr_info("Enabling %s workaround for %s\n",
608 local ? "local" : "global", wa->desc);
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000609}
610
Marc Zyngiera86bd132017-02-01 12:07:15 +0000611static bool arch_timer_this_cpu_has_cntvct_wa(void)
612{
Marc Zyngier5ef19a12019-04-08 16:49:04 +0100613 return has_erratum_handler(read_cntvct_el0);
Marc Zyngiera86bd132017-02-01 12:07:15 +0000614}
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000615
Marc Zyngier0ea41532019-04-08 16:49:07 +0100616static bool arch_timer_counter_has_wa(void)
617{
618 return atomic_read(&timer_unstable_counter_workaround_in_use);
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000619}
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000620#else
621#define arch_timer_check_ool_workaround(t,a) do { } while(0)
Marc Zyngiera86bd132017-02-01 12:07:15 +0000622#define arch_timer_this_cpu_has_cntvct_wa() ({false;})
Marc Zyngier0ea41532019-04-08 16:49:07 +0100623#define arch_timer_counter_has_wa() ({false;})
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000624#endif /* CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND */
Scott Woodf6dc1572016-09-22 03:35:17 -0500625
Stephen Boyde09f3cc2013-07-18 16:59:28 -0700626static __always_inline irqreturn_t timer_handler(const int access,
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000627 struct clock_event_device *evt)
628{
629 unsigned long ctrl;
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200630
Stephen Boyd60faddf2013-07-18 16:59:31 -0700631 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000632 if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
633 ctrl |= ARCH_TIMER_CTRL_IT_MASK;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700634 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000635 evt->event_handler(evt);
636 return IRQ_HANDLED;
637 }
638
639 return IRQ_NONE;
640}
641
642static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
643{
644 struct clock_event_device *evt = dev_id;
645
646 return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
647}
648
649static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
650{
651 struct clock_event_device *evt = dev_id;
652
653 return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
654}
655
Stephen Boyd22006992013-07-18 16:59:32 -0700656static irqreturn_t arch_timer_handler_phys_mem(int irq, void *dev_id)
657{
658 struct clock_event_device *evt = dev_id;
659
660 return timer_handler(ARCH_TIMER_MEM_PHYS_ACCESS, evt);
661}
662
663static irqreturn_t arch_timer_handler_virt_mem(int irq, void *dev_id)
664{
665 struct clock_event_device *evt = dev_id;
666
667 return timer_handler(ARCH_TIMER_MEM_VIRT_ACCESS, evt);
668}
669
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530670static __always_inline int timer_shutdown(const int access,
671 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000672{
673 unsigned long ctrl;
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530674
675 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
676 ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
677 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
678
679 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000680}
681
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530682static int arch_timer_shutdown_virt(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000683{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530684 return timer_shutdown(ARCH_TIMER_VIRT_ACCESS, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000685}
686
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530687static int arch_timer_shutdown_phys(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000688{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530689 return timer_shutdown(ARCH_TIMER_PHYS_ACCESS, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000690}
691
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530692static int arch_timer_shutdown_virt_mem(struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700693{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530694 return timer_shutdown(ARCH_TIMER_MEM_VIRT_ACCESS, clk);
Stephen Boyd22006992013-07-18 16:59:32 -0700695}
696
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530697static int arch_timer_shutdown_phys_mem(struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700698{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530699 return timer_shutdown(ARCH_TIMER_MEM_PHYS_ACCESS, clk);
Stephen Boyd22006992013-07-18 16:59:32 -0700700}
701
Stephen Boyd60faddf2013-07-18 16:59:31 -0700702static __always_inline void set_next_event(const int access, unsigned long evt,
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200703 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000704{
705 unsigned long ctrl;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700706 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000707 ctrl |= ARCH_TIMER_CTRL_ENABLE;
708 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700709 arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt, clk);
710 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000711}
712
713static int arch_timer_set_next_event_virt(unsigned long evt,
Stephen Boyd60faddf2013-07-18 16:59:31 -0700714 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000715{
Stephen Boyd60faddf2013-07-18 16:59:31 -0700716 set_next_event(ARCH_TIMER_VIRT_ACCESS, evt, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000717 return 0;
718}
719
720static int arch_timer_set_next_event_phys(unsigned long evt,
Stephen Boyd60faddf2013-07-18 16:59:31 -0700721 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000722{
Stephen Boyd60faddf2013-07-18 16:59:31 -0700723 set_next_event(ARCH_TIMER_PHYS_ACCESS, evt, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000724 return 0;
725}
726
Stephen Boyd22006992013-07-18 16:59:32 -0700727static int arch_timer_set_next_event_virt_mem(unsigned long evt,
728 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000729{
Stephen Boyd22006992013-07-18 16:59:32 -0700730 set_next_event(ARCH_TIMER_MEM_VIRT_ACCESS, evt, clk);
731 return 0;
732}
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000733
Stephen Boyd22006992013-07-18 16:59:32 -0700734static int arch_timer_set_next_event_phys_mem(unsigned long evt,
735 struct clock_event_device *clk)
736{
737 set_next_event(ARCH_TIMER_MEM_PHYS_ACCESS, evt, clk);
738 return 0;
739}
740
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200741static void __arch_timer_setup(unsigned type,
742 struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700743{
744 clk->features = CLOCK_EVT_FEAT_ONESHOT;
745
Fu Wei8a5c21d2017-01-18 21:25:26 +0800746 if (type == ARCH_TIMER_TYPE_CP15) {
Marc Zyngier5ef19a12019-04-08 16:49:04 +0100747 typeof(clk->set_next_event) sne;
748
749 arch_timer_check_ool_workaround(ate_match_local_cap_id, NULL);
750
Lorenzo Pieralisi82a561942014-04-08 10:04:32 +0100751 if (arch_timer_c3stop)
752 clk->features |= CLOCK_EVT_FEAT_C3STOP;
Stephen Boyd22006992013-07-18 16:59:32 -0700753 clk->name = "arch_sys_timer";
754 clk->rating = 450;
755 clk->cpumask = cpumask_of(smp_processor_id());
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000756 clk->irq = arch_timer_ppi[arch_timer_uses_ppi];
757 switch (arch_timer_uses_ppi) {
Fu Weiee34f1e2017-01-18 21:25:27 +0800758 case ARCH_TIMER_VIRT_PPI:
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530759 clk->set_state_shutdown = arch_timer_shutdown_virt;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530760 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt;
Marc Zyngier5ef19a12019-04-08 16:49:04 +0100761 sne = erratum_handler(set_next_event_virt);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000762 break;
Fu Weiee34f1e2017-01-18 21:25:27 +0800763 case ARCH_TIMER_PHYS_SECURE_PPI:
764 case ARCH_TIMER_PHYS_NONSECURE_PPI:
765 case ARCH_TIMER_HYP_PPI:
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530766 clk->set_state_shutdown = arch_timer_shutdown_phys;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530767 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys;
Marc Zyngier5ef19a12019-04-08 16:49:04 +0100768 sne = erratum_handler(set_next_event_phys);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000769 break;
770 default:
771 BUG();
Stephen Boyd22006992013-07-18 16:59:32 -0700772 }
Scott Woodf6dc1572016-09-22 03:35:17 -0500773
Marc Zyngier5ef19a12019-04-08 16:49:04 +0100774 clk->set_next_event = sne;
Stephen Boyd22006992013-07-18 16:59:32 -0700775 } else {
Stephen Boyd7b52ad22014-01-06 14:56:17 -0800776 clk->features |= CLOCK_EVT_FEAT_DYNIRQ;
Stephen Boyd22006992013-07-18 16:59:32 -0700777 clk->name = "arch_mem_timer";
778 clk->rating = 400;
Sudeep Holla5e18e412018-07-09 16:45:36 +0100779 clk->cpumask = cpu_possible_mask;
Stephen Boyd22006992013-07-18 16:59:32 -0700780 if (arch_timer_mem_use_virtual) {
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530781 clk->set_state_shutdown = arch_timer_shutdown_virt_mem;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530782 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt_mem;
Stephen Boyd22006992013-07-18 16:59:32 -0700783 clk->set_next_event =
784 arch_timer_set_next_event_virt_mem;
785 } else {
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530786 clk->set_state_shutdown = arch_timer_shutdown_phys_mem;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530787 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys_mem;
Stephen Boyd22006992013-07-18 16:59:32 -0700788 clk->set_next_event =
789 arch_timer_set_next_event_phys_mem;
790 }
791 }
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000792
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530793 clk->set_state_shutdown(clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000794
Stephen Boyd22006992013-07-18 16:59:32 -0700795 clockevents_config_and_register(clk, arch_timer_rate, 0xf, 0x7fffffff);
796}
797
Nathan Lynche1ce5c72014-09-29 01:50:06 +0200798static void arch_timer_evtstrm_enable(int divider)
799{
800 u32 cntkctl = arch_timer_get_cntkctl();
801
802 cntkctl &= ~ARCH_TIMER_EVT_TRIGGER_MASK;
803 /* Set the divider and enable virtual event stream */
804 cntkctl |= (divider << ARCH_TIMER_EVT_TRIGGER_SHIFT)
805 | ARCH_TIMER_VIRT_EVT_EN;
806 arch_timer_set_cntkctl(cntkctl);
Andrew Murrayaaba0982019-04-09 10:52:40 +0100807#ifdef CONFIG_ARM64
808 cpu_set_named_feature(EVTSTRM);
809#else
Nathan Lynche1ce5c72014-09-29 01:50:06 +0200810 elf_hwcap |= HWCAP_EVTSTRM;
Andrew Murrayaaba0982019-04-09 10:52:40 +0100811#endif
Nathan Lynche1ce5c72014-09-29 01:50:06 +0200812#ifdef CONFIG_COMPAT
813 compat_elf_hwcap |= COMPAT_HWCAP_EVTSTRM;
814#endif
Julien Thierryec5c8e42017-10-13 14:32:55 +0100815 cpumask_set_cpu(smp_processor_id(), &evtstrm_available);
Nathan Lynche1ce5c72014-09-29 01:50:06 +0200816}
817
Will Deacon037f6372013-08-23 15:32:29 +0100818static void arch_timer_configure_evtstream(void)
819{
820 int evt_stream_div, pos;
821
822 /* Find the closest power of two to the divisor */
823 evt_stream_div = arch_timer_rate / ARCH_TIMER_EVT_STREAM_FREQ;
824 pos = fls(evt_stream_div);
825 if (pos > 1 && !(evt_stream_div & (1 << (pos - 2))))
826 pos--;
827 /* enable event stream */
828 arch_timer_evtstrm_enable(min(pos, 15));
829}
830
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200831static void arch_counter_set_user_access(void)
832{
833 u32 cntkctl = arch_timer_get_cntkctl();
834
Marc Zyngiera86bd132017-02-01 12:07:15 +0000835 /* Disable user access to the timers and both counters */
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200836 /* Also disable virtual event stream */
837 cntkctl &= ~(ARCH_TIMER_USR_PT_ACCESS_EN
838 | ARCH_TIMER_USR_VT_ACCESS_EN
Marc Zyngiera86bd132017-02-01 12:07:15 +0000839 | ARCH_TIMER_USR_VCT_ACCESS_EN
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200840 | ARCH_TIMER_VIRT_EVT_EN
841 | ARCH_TIMER_USR_PCT_ACCESS_EN);
842
Marc Zyngiera86bd132017-02-01 12:07:15 +0000843 /*
844 * Enable user access to the virtual counter if it doesn't
845 * need to be workaround. The vdso may have been already
846 * disabled though.
847 */
848 if (arch_timer_this_cpu_has_cntvct_wa())
849 pr_info("CPU%d: Trapping CNTVCT access\n", smp_processor_id());
850 else
851 cntkctl |= ARCH_TIMER_USR_VCT_ACCESS_EN;
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200852
853 arch_timer_set_cntkctl(cntkctl);
854}
855
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000856static bool arch_timer_has_nonsecure_ppi(void)
857{
Fu Weiee34f1e2017-01-18 21:25:27 +0800858 return (arch_timer_uses_ppi == ARCH_TIMER_PHYS_SECURE_PPI &&
859 arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000860}
861
Marc Zyngierf005bd72016-08-01 10:54:15 +0100862static u32 check_ppi_trigger(int irq)
863{
864 u32 flags = irq_get_trigger_type(irq);
865
866 if (flags != IRQF_TRIGGER_HIGH && flags != IRQF_TRIGGER_LOW) {
867 pr_warn("WARNING: Invalid trigger for IRQ%d, assuming level low\n", irq);
868 pr_warn("WARNING: Please fix your firmware\n");
869 flags = IRQF_TRIGGER_LOW;
870 }
871
872 return flags;
873}
874
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000875static int arch_timer_starting_cpu(unsigned int cpu)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000876{
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000877 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
Marc Zyngierf005bd72016-08-01 10:54:15 +0100878 u32 flags;
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000879
Fu Wei8a5c21d2017-01-18 21:25:26 +0800880 __arch_timer_setup(ARCH_TIMER_TYPE_CP15, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000881
Marc Zyngierf005bd72016-08-01 10:54:15 +0100882 flags = check_ppi_trigger(arch_timer_ppi[arch_timer_uses_ppi]);
883 enable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], flags);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000884
Marc Zyngierf005bd72016-08-01 10:54:15 +0100885 if (arch_timer_has_nonsecure_ppi()) {
Fu Weiee34f1e2017-01-18 21:25:27 +0800886 flags = check_ppi_trigger(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
887 enable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
888 flags);
Marc Zyngierf005bd72016-08-01 10:54:15 +0100889 }
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000890
891 arch_counter_set_user_access();
Will Deacon46fd5c62016-06-27 17:30:13 +0100892 if (evtstrm_enable)
Will Deacon037f6372013-08-23 15:32:29 +0100893 arch_timer_configure_evtstream();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000894
895 return 0;
896}
897
Fu Wei5d3dfa92017-03-22 00:31:13 +0800898/*
899 * For historical reasons, when probing with DT we use whichever (non-zero)
900 * rate was probed first, and don't verify that others match. If the first node
901 * probed has a clock-frequency property, this overrides the HW register.
902 */
903static void arch_timer_of_configure_rate(u32 rate, struct device_node *np)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000904{
Stephen Boyd22006992013-07-18 16:59:32 -0700905 /* Who has more than one independent system counter? */
906 if (arch_timer_rate)
907 return;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000908
Fu Wei5d3dfa92017-03-22 00:31:13 +0800909 if (of_property_read_u32(np, "clock-frequency", &arch_timer_rate))
910 arch_timer_rate = rate;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000911
Stephen Boyd22006992013-07-18 16:59:32 -0700912 /* Check the timer frequency. */
913 if (arch_timer_rate == 0)
Fu Weided24012017-01-18 21:25:25 +0800914 pr_warn("frequency not available\n");
Stephen Boyd22006992013-07-18 16:59:32 -0700915}
916
917static void arch_timer_banner(unsigned type)
918{
Fu Weided24012017-01-18 21:25:25 +0800919 pr_info("%s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
Fu Wei8a5c21d2017-01-18 21:25:26 +0800920 type & ARCH_TIMER_TYPE_CP15 ? "cp15" : "",
921 type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ?
922 " and " : "",
923 type & ARCH_TIMER_TYPE_MEM ? "mmio" : "",
Fu Weided24012017-01-18 21:25:25 +0800924 (unsigned long)arch_timer_rate / 1000000,
925 (unsigned long)(arch_timer_rate / 10000) % 100,
Fu Wei8a5c21d2017-01-18 21:25:26 +0800926 type & ARCH_TIMER_TYPE_CP15 ?
Fu Weiee34f1e2017-01-18 21:25:27 +0800927 (arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI) ? "virt" : "phys" :
Stephen Boyd22006992013-07-18 16:59:32 -0700928 "",
Fu Wei8a5c21d2017-01-18 21:25:26 +0800929 type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ? "/" : "",
930 type & ARCH_TIMER_TYPE_MEM ?
Stephen Boyd22006992013-07-18 16:59:32 -0700931 arch_timer_mem_use_virtual ? "virt" : "phys" :
932 "");
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000933}
934
935u32 arch_timer_get_rate(void)
936{
937 return arch_timer_rate;
938}
939
Julien Thierryec5c8e42017-10-13 14:32:55 +0100940bool arch_timer_evtstrm_available(void)
941{
942 /*
943 * We might get called from a preemptible context. This is fine
944 * because availability of the event stream should be always the same
945 * for a preemptible context and context where we might resume a task.
946 */
947 return cpumask_test_cpu(raw_smp_processor_id(), &evtstrm_available);
948}
949
Stephen Boyd22006992013-07-18 16:59:32 -0700950static u64 arch_counter_get_cntvct_mem(void)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000951{
Stephen Boyd22006992013-07-18 16:59:32 -0700952 u32 vct_lo, vct_hi, tmp_hi;
953
954 do {
955 vct_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
956 vct_lo = readl_relaxed(arch_counter_base + CNTVCT_LO);
957 tmp_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
958 } while (vct_hi != tmp_hi);
959
960 return ((u64) vct_hi << 32) | vct_lo;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000961}
962
Julien Grallb4d6ce92016-04-11 16:32:51 +0100963static struct arch_timer_kvm_info arch_timer_kvm_info;
964
965struct arch_timer_kvm_info *arch_timer_get_kvm_info(void)
966{
967 return &arch_timer_kvm_info;
968}
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000969
Stephen Boyd22006992013-07-18 16:59:32 -0700970static void __init arch_counter_register(unsigned type)
971{
972 u64 start_count;
973
974 /* Register the CP15 based counter if we have one */
Fu Wei8a5c21d2017-01-18 21:25:26 +0800975 if (type & ARCH_TIMER_TYPE_CP15) {
Marc Zyngier0ea41532019-04-08 16:49:07 +0100976 u64 (*rd)(void);
Scott Woodf6dc1572016-09-22 03:35:17 -0500977
Marc Zyngier0ea41532019-04-08 16:49:07 +0100978 if ((IS_ENABLED(CONFIG_ARM64) && !is_hyp_mode_available()) ||
979 arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI) {
980 if (arch_timer_counter_has_wa())
981 rd = arch_counter_get_cntvct_stable;
982 else
983 rd = arch_counter_get_cntvct;
984 } else {
985 if (arch_timer_counter_has_wa())
986 rd = arch_counter_get_cntpct_stable;
987 else
988 rd = arch_counter_get_cntpct;
989 }
990
991 arch_timer_read_counter = rd;
Marc Zyngiera86bd132017-02-01 12:07:15 +0000992 clocksource_counter.archdata.vdso_direct = vdso_default;
Nathan Lynch423bd692014-09-29 01:50:06 +0200993 } else {
Stephen Boyd22006992013-07-18 16:59:32 -0700994 arch_timer_read_counter = arch_counter_get_cntvct_mem;
Nathan Lynch423bd692014-09-29 01:50:06 +0200995 }
996
Brian Norrisd8ec7592016-10-04 11:12:09 -0700997 if (!arch_counter_suspend_stop)
998 clocksource_counter.flags |= CLOCK_SOURCE_SUSPEND_NONSTOP;
Stephen Boyd22006992013-07-18 16:59:32 -0700999 start_count = arch_timer_read_counter();
1000 clocksource_register_hz(&clocksource_counter, arch_timer_rate);
1001 cyclecounter.mult = clocksource_counter.mult;
1002 cyclecounter.shift = clocksource_counter.shift;
Julien Grallb4d6ce92016-04-11 16:32:51 +01001003 timecounter_init(&arch_timer_kvm_info.timecounter,
1004 &cyclecounter, start_count);
Thierry Reding4a7d3e82013-10-15 15:31:51 +02001005
1006 /* 56 bits minimum, so we assume worst case rollover */
1007 sched_clock_register(arch_timer_read_counter, 56, arch_timer_rate);
Stephen Boyd22006992013-07-18 16:59:32 -07001008}
1009
Paul Gortmaker8c37bb32013-06-19 11:32:08 -04001010static void arch_timer_stop(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001011{
Fu Weided24012017-01-18 21:25:25 +08001012 pr_debug("disable IRQ%d cpu #%d\n", clk->irq, smp_processor_id());
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001013
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001014 disable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi]);
1015 if (arch_timer_has_nonsecure_ppi())
Fu Weiee34f1e2017-01-18 21:25:27 +08001016 disable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001017
Viresh Kumar46c5bfd2015-06-12 13:30:12 +05301018 clk->set_state_shutdown(clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001019}
1020
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001021static int arch_timer_dying_cpu(unsigned int cpu)
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001022{
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001023 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001024
Julien Thierryec5c8e42017-10-13 14:32:55 +01001025 cpumask_clear_cpu(smp_processor_id(), &evtstrm_available);
1026
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001027 arch_timer_stop(clk);
1028 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001029}
1030
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001031#ifdef CONFIG_CPU_PM
Marc Zyngierbee67c52017-04-04 17:05:16 +01001032static DEFINE_PER_CPU(unsigned long, saved_cntkctl);
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001033static int arch_timer_cpu_pm_notify(struct notifier_block *self,
1034 unsigned long action, void *hcpu)
1035{
Julien Thierryec5c8e42017-10-13 14:32:55 +01001036 if (action == CPU_PM_ENTER) {
Marc Zyngierbee67c52017-04-04 17:05:16 +01001037 __this_cpu_write(saved_cntkctl, arch_timer_get_cntkctl());
Julien Thierryec5c8e42017-10-13 14:32:55 +01001038
1039 cpumask_clear_cpu(smp_processor_id(), &evtstrm_available);
1040 } else if (action == CPU_PM_ENTER_FAILED || action == CPU_PM_EXIT) {
Marc Zyngierbee67c52017-04-04 17:05:16 +01001041 arch_timer_set_cntkctl(__this_cpu_read(saved_cntkctl));
Julien Thierryec5c8e42017-10-13 14:32:55 +01001042
Andrew Murrayaaba0982019-04-09 10:52:40 +01001043#ifdef CONFIG_ARM64
1044 if (cpu_have_named_feature(EVTSTRM))
1045#else
Julien Thierryec5c8e42017-10-13 14:32:55 +01001046 if (elf_hwcap & HWCAP_EVTSTRM)
Andrew Murrayaaba0982019-04-09 10:52:40 +01001047#endif
Julien Thierryec5c8e42017-10-13 14:32:55 +01001048 cpumask_set_cpu(smp_processor_id(), &evtstrm_available);
1049 }
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001050 return NOTIFY_OK;
1051}
1052
1053static struct notifier_block arch_timer_cpu_pm_notifier = {
1054 .notifier_call = arch_timer_cpu_pm_notify,
1055};
1056
1057static int __init arch_timer_cpu_pm_init(void)
1058{
1059 return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier);
1060}
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001061
1062static void __init arch_timer_cpu_pm_deinit(void)
1063{
1064 WARN_ON(cpu_pm_unregister_notifier(&arch_timer_cpu_pm_notifier));
1065}
1066
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001067#else
1068static int __init arch_timer_cpu_pm_init(void)
1069{
1070 return 0;
1071}
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001072
1073static void __init arch_timer_cpu_pm_deinit(void)
1074{
1075}
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001076#endif
1077
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001078static int __init arch_timer_register(void)
1079{
1080 int err;
1081 int ppi;
1082
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001083 arch_timer_evt = alloc_percpu(struct clock_event_device);
1084 if (!arch_timer_evt) {
1085 err = -ENOMEM;
1086 goto out;
1087 }
1088
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001089 ppi = arch_timer_ppi[arch_timer_uses_ppi];
1090 switch (arch_timer_uses_ppi) {
Fu Weiee34f1e2017-01-18 21:25:27 +08001091 case ARCH_TIMER_VIRT_PPI:
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001092 err = request_percpu_irq(ppi, arch_timer_handler_virt,
1093 "arch_timer", arch_timer_evt);
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001094 break;
Fu Weiee34f1e2017-01-18 21:25:27 +08001095 case ARCH_TIMER_PHYS_SECURE_PPI:
1096 case ARCH_TIMER_PHYS_NONSECURE_PPI:
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001097 err = request_percpu_irq(ppi, arch_timer_handler_phys,
1098 "arch_timer", arch_timer_evt);
Fu Wei4502b6b2017-01-18 21:25:30 +08001099 if (!err && arch_timer_has_nonsecure_ppi()) {
Fu Weiee34f1e2017-01-18 21:25:27 +08001100 ppi = arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI];
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001101 err = request_percpu_irq(ppi, arch_timer_handler_phys,
1102 "arch_timer", arch_timer_evt);
1103 if (err)
Fu Weiee34f1e2017-01-18 21:25:27 +08001104 free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_SECURE_PPI],
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001105 arch_timer_evt);
1106 }
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001107 break;
Fu Weiee34f1e2017-01-18 21:25:27 +08001108 case ARCH_TIMER_HYP_PPI:
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001109 err = request_percpu_irq(ppi, arch_timer_handler_phys,
1110 "arch_timer", arch_timer_evt);
1111 break;
1112 default:
1113 BUG();
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001114 }
1115
1116 if (err) {
Fu Weided24012017-01-18 21:25:25 +08001117 pr_err("can't register interrupt %d (%d)\n", ppi, err);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001118 goto out_free;
1119 }
1120
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001121 err = arch_timer_cpu_pm_init();
1122 if (err)
1123 goto out_unreg_notify;
1124
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001125 /* Register and immediately configure the timer on the boot CPU */
1126 err = cpuhp_setup_state(CPUHP_AP_ARM_ARCH_TIMER_STARTING,
Thomas Gleixner73c1b412016-12-21 20:19:54 +01001127 "clockevents/arm/arch_timer:starting",
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001128 arch_timer_starting_cpu, arch_timer_dying_cpu);
1129 if (err)
1130 goto out_unreg_cpupm;
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001131 return 0;
1132
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001133out_unreg_cpupm:
1134 arch_timer_cpu_pm_deinit();
1135
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001136out_unreg_notify:
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001137 free_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], arch_timer_evt);
1138 if (arch_timer_has_nonsecure_ppi())
Fu Weiee34f1e2017-01-18 21:25:27 +08001139 free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001140 arch_timer_evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001141
1142out_free:
1143 free_percpu(arch_timer_evt);
1144out:
1145 return err;
1146}
1147
Stephen Boyd22006992013-07-18 16:59:32 -07001148static int __init arch_timer_mem_register(void __iomem *base, unsigned int irq)
1149{
1150 int ret;
1151 irq_handler_t func;
1152 struct arch_timer *t;
1153
1154 t = kzalloc(sizeof(*t), GFP_KERNEL);
1155 if (!t)
1156 return -ENOMEM;
1157
1158 t->base = base;
1159 t->evt.irq = irq;
Fu Wei8a5c21d2017-01-18 21:25:26 +08001160 __arch_timer_setup(ARCH_TIMER_TYPE_MEM, &t->evt);
Stephen Boyd22006992013-07-18 16:59:32 -07001161
1162 if (arch_timer_mem_use_virtual)
1163 func = arch_timer_handler_virt_mem;
1164 else
1165 func = arch_timer_handler_phys_mem;
1166
1167 ret = request_irq(irq, func, IRQF_TIMER, "arch_mem_timer", &t->evt);
1168 if (ret) {
Fu Weided24012017-01-18 21:25:25 +08001169 pr_err("Failed to request mem timer irq\n");
Stephen Boyd22006992013-07-18 16:59:32 -07001170 kfree(t);
1171 }
1172
1173 return ret;
1174}
1175
1176static const struct of_device_id arch_timer_of_match[] __initconst = {
1177 { .compatible = "arm,armv7-timer", },
1178 { .compatible = "arm,armv8-timer", },
1179 {},
1180};
1181
1182static const struct of_device_id arch_timer_mem_of_match[] __initconst = {
1183 { .compatible = "arm,armv7-timer-mem", },
1184 {},
1185};
1186
Fu Wei13bf6992017-03-22 00:31:14 +08001187static bool __init arch_timer_needs_of_probing(void)
Sudeep Hollac387f072014-09-29 01:50:05 +02001188{
1189 struct device_node *dn;
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001190 bool needs_probing = false;
Fu Wei13bf6992017-03-22 00:31:14 +08001191 unsigned int mask = ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM;
Sudeep Hollac387f072014-09-29 01:50:05 +02001192
Fu Wei13bf6992017-03-22 00:31:14 +08001193 /* We have two timers, and both device-tree nodes are probed. */
1194 if ((arch_timers_present & mask) == mask)
1195 return false;
1196
1197 /*
1198 * Only one type of timer is probed,
1199 * check if we have another type of timer node in device-tree.
1200 */
1201 if (arch_timers_present & ARCH_TIMER_TYPE_CP15)
1202 dn = of_find_matching_node(NULL, arch_timer_mem_of_match);
1203 else
1204 dn = of_find_matching_node(NULL, arch_timer_of_match);
1205
1206 if (dn && of_device_is_available(dn))
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001207 needs_probing = true;
Fu Wei13bf6992017-03-22 00:31:14 +08001208
Sudeep Hollac387f072014-09-29 01:50:05 +02001209 of_node_put(dn);
1210
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001211 return needs_probing;
Sudeep Hollac387f072014-09-29 01:50:05 +02001212}
1213
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001214static int __init arch_timer_common_init(void)
Stephen Boyd22006992013-07-18 16:59:32 -07001215{
Stephen Boyd22006992013-07-18 16:59:32 -07001216 arch_timer_banner(arch_timers_present);
1217 arch_counter_register(arch_timers_present);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001218 return arch_timer_arch_init();
Stephen Boyd22006992013-07-18 16:59:32 -07001219}
1220
Fu Wei4502b6b2017-01-18 21:25:30 +08001221/**
1222 * arch_timer_select_ppi() - Select suitable PPI for the current system.
1223 *
1224 * If HYP mode is available, we know that the physical timer
1225 * has been configured to be accessible from PL1. Use it, so
1226 * that a guest can use the virtual timer instead.
1227 *
1228 * On ARMv8.1 with VH extensions, the kernel runs in HYP. VHE
1229 * accesses to CNTP_*_EL1 registers are silently redirected to
1230 * their CNTHP_*_EL2 counterparts, and use a different PPI
1231 * number.
1232 *
1233 * If no interrupt provided for virtual timer, we'll have to
1234 * stick to the physical timer. It'd better be accessible...
1235 * For arm64 we never use the secure interrupt.
1236 *
1237 * Return: a suitable PPI type for the current system.
1238 */
1239static enum arch_timer_ppi_nr __init arch_timer_select_ppi(void)
1240{
1241 if (is_kernel_in_hyp_mode())
1242 return ARCH_TIMER_HYP_PPI;
1243
1244 if (!is_hyp_mode_available() && arch_timer_ppi[ARCH_TIMER_VIRT_PPI])
1245 return ARCH_TIMER_VIRT_PPI;
1246
1247 if (IS_ENABLED(CONFIG_ARM64))
1248 return ARCH_TIMER_PHYS_NONSECURE_PPI;
1249
1250 return ARCH_TIMER_PHYS_SECURE_PPI;
1251}
1252
Andre Przywaraee793042018-07-06 09:11:50 +01001253static void __init arch_timer_populate_kvm_info(void)
1254{
1255 arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI];
1256 if (is_kernel_in_hyp_mode())
1257 arch_timer_kvm_info.physical_irq = arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI];
1258}
1259
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001260static int __init arch_timer_of_init(struct device_node *np)
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001261{
Fu Weica0e1b52017-03-22 00:31:15 +08001262 int i, ret;
Fu Wei5d3dfa92017-03-22 00:31:13 +08001263 u32 rate;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001264
Fu Wei8a5c21d2017-01-18 21:25:26 +08001265 if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
Fu Weided24012017-01-18 21:25:25 +08001266 pr_warn("multiple nodes in dt, skipping\n");
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001267 return 0;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001268 }
1269
Fu Wei8a5c21d2017-01-18 21:25:26 +08001270 arch_timers_present |= ARCH_TIMER_TYPE_CP15;
Fu Weiee34f1e2017-01-18 21:25:27 +08001271 for (i = ARCH_TIMER_PHYS_SECURE_PPI; i < ARCH_TIMER_MAX_TIMER_PPI; i++)
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001272 arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
1273
Andre Przywaraee793042018-07-06 09:11:50 +01001274 arch_timer_populate_kvm_info();
Fu Weica0e1b52017-03-22 00:31:15 +08001275
Fu Weic389d702017-04-01 01:51:00 +08001276 rate = arch_timer_get_cntfrq();
Fu Wei5d3dfa92017-03-22 00:31:13 +08001277 arch_timer_of_configure_rate(rate, np);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001278
1279 arch_timer_c3stop = !of_property_read_bool(np, "always-on");
1280
Marc Zyngier651bb2e2017-01-19 17:20:59 +00001281 /* Check for globally applicable workarounds */
1282 arch_timer_check_ool_workaround(ate_match_dt, np);
Scott Woodf6dc1572016-09-22 03:35:17 -05001283
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001284 /*
1285 * If we cannot rely on firmware initializing the timer registers then
1286 * we should use the physical timers instead.
1287 */
1288 if (IS_ENABLED(CONFIG_ARM) &&
1289 of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
Fu Weiee34f1e2017-01-18 21:25:27 +08001290 arch_timer_uses_ppi = ARCH_TIMER_PHYS_SECURE_PPI;
Fu Wei4502b6b2017-01-18 21:25:30 +08001291 else
1292 arch_timer_uses_ppi = arch_timer_select_ppi();
1293
1294 if (!arch_timer_ppi[arch_timer_uses_ppi]) {
1295 pr_err("No interrupt available, giving up\n");
1296 return -EINVAL;
1297 }
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001298
Brian Norrisd8ec7592016-10-04 11:12:09 -07001299 /* On some systems, the counter stops ticking when in suspend. */
1300 arch_counter_suspend_stop = of_property_read_bool(np,
1301 "arm,no-tick-in-suspend");
1302
Fu Weica0e1b52017-03-22 00:31:15 +08001303 ret = arch_timer_register();
1304 if (ret)
1305 return ret;
1306
1307 if (arch_timer_needs_of_probing())
1308 return 0;
1309
1310 return arch_timer_common_init();
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001311}
Daniel Lezcano17273392017-05-26 16:56:11 +02001312TIMER_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_of_init);
1313TIMER_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_of_init);
Stephen Boyd22006992013-07-18 16:59:32 -07001314
Fu Weic389d702017-04-01 01:51:00 +08001315static u32 __init
1316arch_timer_mem_frame_get_cntfrq(struct arch_timer_mem_frame *frame)
Stephen Boyd22006992013-07-18 16:59:32 -07001317{
Fu Weic389d702017-04-01 01:51:00 +08001318 void __iomem *base;
1319 u32 rate;
Stephen Boyd22006992013-07-18 16:59:32 -07001320
Fu Weic389d702017-04-01 01:51:00 +08001321 base = ioremap(frame->cntbase, frame->size);
1322 if (!base) {
1323 pr_err("Unable to map frame @ %pa\n", &frame->cntbase);
1324 return 0;
1325 }
1326
Frank Rowand3db12002017-06-09 17:26:32 -07001327 rate = readl_relaxed(base + CNTFRQ);
Fu Weic389d702017-04-01 01:51:00 +08001328
Frank Rowand3db12002017-06-09 17:26:32 -07001329 iounmap(base);
Fu Weic389d702017-04-01 01:51:00 +08001330
1331 return rate;
1332}
1333
1334static struct arch_timer_mem_frame * __init
1335arch_timer_mem_find_best_frame(struct arch_timer_mem *timer_mem)
1336{
1337 struct arch_timer_mem_frame *frame, *best_frame = NULL;
1338 void __iomem *cntctlbase;
1339 u32 cnttidr;
1340 int i;
1341
1342 cntctlbase = ioremap(timer_mem->cntctlbase, timer_mem->size);
Stephen Boyd22006992013-07-18 16:59:32 -07001343 if (!cntctlbase) {
Fu Weic389d702017-04-01 01:51:00 +08001344 pr_err("Can't map CNTCTLBase @ %pa\n",
1345 &timer_mem->cntctlbase);
1346 return NULL;
Stephen Boyd22006992013-07-18 16:59:32 -07001347 }
1348
1349 cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
Stephen Boyd22006992013-07-18 16:59:32 -07001350
1351 /*
1352 * Try to find a virtual capable frame. Otherwise fall back to a
1353 * physical capable frame.
1354 */
Fu Weic389d702017-04-01 01:51:00 +08001355 for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
1356 u32 cntacr = CNTACR_RFRQ | CNTACR_RWPT | CNTACR_RPCT |
1357 CNTACR_RWVT | CNTACR_RVOFF | CNTACR_RVCT;
Stephen Boyd22006992013-07-18 16:59:32 -07001358
Fu Weic389d702017-04-01 01:51:00 +08001359 frame = &timer_mem->frame[i];
1360 if (!frame->valid)
1361 continue;
Stephen Boyd22006992013-07-18 16:59:32 -07001362
Robin Murphye392d602016-02-01 12:00:48 +00001363 /* Try enabling everything, and see what sticks */
Fu Weic389d702017-04-01 01:51:00 +08001364 writel_relaxed(cntacr, cntctlbase + CNTACR(i));
1365 cntacr = readl_relaxed(cntctlbase + CNTACR(i));
Robin Murphye392d602016-02-01 12:00:48 +00001366
Fu Weic389d702017-04-01 01:51:00 +08001367 if ((cnttidr & CNTTIDR_VIRT(i)) &&
Robin Murphye392d602016-02-01 12:00:48 +00001368 !(~cntacr & (CNTACR_RWVT | CNTACR_RVCT))) {
Stephen Boyd22006992013-07-18 16:59:32 -07001369 best_frame = frame;
1370 arch_timer_mem_use_virtual = true;
1371 break;
1372 }
Robin Murphye392d602016-02-01 12:00:48 +00001373
1374 if (~cntacr & (CNTACR_RWPT | CNTACR_RPCT))
1375 continue;
1376
Fu Weic389d702017-04-01 01:51:00 +08001377 best_frame = frame;
Stephen Boyd22006992013-07-18 16:59:32 -07001378 }
1379
Fu Weic389d702017-04-01 01:51:00 +08001380 iounmap(cntctlbase);
1381
Sudeep Hollaf63d9472017-05-08 13:32:27 +01001382 return best_frame;
Fu Weic389d702017-04-01 01:51:00 +08001383}
1384
1385static int __init
1386arch_timer_mem_frame_register(struct arch_timer_mem_frame *frame)
1387{
1388 void __iomem *base;
1389 int ret, irq = 0;
Stephen Boyd22006992013-07-18 16:59:32 -07001390
1391 if (arch_timer_mem_use_virtual)
Fu Weic389d702017-04-01 01:51:00 +08001392 irq = frame->virt_irq;
Stephen Boyd22006992013-07-18 16:59:32 -07001393 else
Fu Weic389d702017-04-01 01:51:00 +08001394 irq = frame->phys_irq;
Robin Murphye392d602016-02-01 12:00:48 +00001395
Stephen Boyd22006992013-07-18 16:59:32 -07001396 if (!irq) {
Fu Weided24012017-01-18 21:25:25 +08001397 pr_err("Frame missing %s irq.\n",
Thomas Gleixnercfb6d652013-08-21 14:59:23 +02001398 arch_timer_mem_use_virtual ? "virt" : "phys");
Fu Weic389d702017-04-01 01:51:00 +08001399 return -EINVAL;
1400 }
1401
1402 if (!request_mem_region(frame->cntbase, frame->size,
1403 "arch_mem_timer"))
1404 return -EBUSY;
1405
1406 base = ioremap(frame->cntbase, frame->size);
1407 if (!base) {
1408 pr_err("Can't map frame's registers\n");
1409 return -ENXIO;
1410 }
1411
1412 ret = arch_timer_mem_register(base, irq);
1413 if (ret) {
1414 iounmap(base);
1415 return ret;
1416 }
1417
1418 arch_counter_base = base;
1419 arch_timers_present |= ARCH_TIMER_TYPE_MEM;
1420
1421 return 0;
1422}
1423
1424static int __init arch_timer_mem_of_init(struct device_node *np)
1425{
1426 struct arch_timer_mem *timer_mem;
1427 struct arch_timer_mem_frame *frame;
1428 struct device_node *frame_node;
1429 struct resource res;
1430 int ret = -EINVAL;
1431 u32 rate;
1432
1433 timer_mem = kzalloc(sizeof(*timer_mem), GFP_KERNEL);
1434 if (!timer_mem)
1435 return -ENOMEM;
1436
1437 if (of_address_to_resource(np, 0, &res))
1438 goto out;
1439 timer_mem->cntctlbase = res.start;
1440 timer_mem->size = resource_size(&res);
1441
1442 for_each_available_child_of_node(np, frame_node) {
1443 u32 n;
1444 struct arch_timer_mem_frame *frame;
1445
1446 if (of_property_read_u32(frame_node, "frame-number", &n)) {
1447 pr_err(FW_BUG "Missing frame-number.\n");
1448 of_node_put(frame_node);
1449 goto out;
1450 }
1451 if (n >= ARCH_TIMER_MEM_MAX_FRAMES) {
1452 pr_err(FW_BUG "Wrong frame-number, only 0-%u are permitted.\n",
1453 ARCH_TIMER_MEM_MAX_FRAMES - 1);
1454 of_node_put(frame_node);
1455 goto out;
1456 }
1457 frame = &timer_mem->frame[n];
1458
1459 if (frame->valid) {
1460 pr_err(FW_BUG "Duplicated frame-number.\n");
1461 of_node_put(frame_node);
1462 goto out;
1463 }
1464
1465 if (of_address_to_resource(frame_node, 0, &res)) {
1466 of_node_put(frame_node);
1467 goto out;
1468 }
1469 frame->cntbase = res.start;
1470 frame->size = resource_size(&res);
1471
1472 frame->virt_irq = irq_of_parse_and_map(frame_node,
1473 ARCH_TIMER_VIRT_SPI);
1474 frame->phys_irq = irq_of_parse_and_map(frame_node,
1475 ARCH_TIMER_PHYS_SPI);
1476
1477 frame->valid = true;
1478 }
1479
1480 frame = arch_timer_mem_find_best_frame(timer_mem);
1481 if (!frame) {
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001482 pr_err("Unable to find a suitable frame in timer @ %pa\n",
1483 &timer_mem->cntctlbase);
Fu Weic389d702017-04-01 01:51:00 +08001484 ret = -EINVAL;
Robin Murphye392d602016-02-01 12:00:48 +00001485 goto out;
Stephen Boyd22006992013-07-18 16:59:32 -07001486 }
1487
Fu Weic389d702017-04-01 01:51:00 +08001488 rate = arch_timer_mem_frame_get_cntfrq(frame);
Fu Wei5d3dfa92017-03-22 00:31:13 +08001489 arch_timer_of_configure_rate(rate, np);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001490
Fu Weic389d702017-04-01 01:51:00 +08001491 ret = arch_timer_mem_frame_register(frame);
1492 if (!ret && !arch_timer_needs_of_probing())
Fu Weica0e1b52017-03-22 00:31:15 +08001493 ret = arch_timer_common_init();
Robin Murphye392d602016-02-01 12:00:48 +00001494out:
Fu Weic389d702017-04-01 01:51:00 +08001495 kfree(timer_mem);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001496 return ret;
Stephen Boyd22006992013-07-18 16:59:32 -07001497}
Daniel Lezcano17273392017-05-26 16:56:11 +02001498TIMER_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem",
Fu Weic389d702017-04-01 01:51:00 +08001499 arch_timer_mem_of_init);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001500
Fu Weif79d2092017-04-01 01:51:02 +08001501#ifdef CONFIG_ACPI_GTDT
Fu Weic2743a32017-04-01 01:51:04 +08001502static int __init
1503arch_timer_mem_verify_cntfrq(struct arch_timer_mem *timer_mem)
1504{
1505 struct arch_timer_mem_frame *frame;
1506 u32 rate;
1507 int i;
1508
1509 for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
1510 frame = &timer_mem->frame[i];
1511
1512 if (!frame->valid)
1513 continue;
1514
1515 rate = arch_timer_mem_frame_get_cntfrq(frame);
1516 if (rate == arch_timer_rate)
1517 continue;
1518
1519 pr_err(FW_BUG "CNTFRQ mismatch: frame @ %pa: (0x%08lx), CPU: (0x%08lx)\n",
1520 &frame->cntbase,
1521 (unsigned long)rate, (unsigned long)arch_timer_rate);
1522
1523 return -EINVAL;
1524 }
1525
1526 return 0;
1527}
1528
1529static int __init arch_timer_mem_acpi_init(int platform_timer_count)
1530{
1531 struct arch_timer_mem *timers, *timer;
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001532 struct arch_timer_mem_frame *frame, *best_frame = NULL;
Fu Weic2743a32017-04-01 01:51:04 +08001533 int timer_count, i, ret = 0;
1534
1535 timers = kcalloc(platform_timer_count, sizeof(*timers),
1536 GFP_KERNEL);
1537 if (!timers)
1538 return -ENOMEM;
1539
1540 ret = acpi_arch_timer_mem_init(timers, &timer_count);
1541 if (ret || !timer_count)
1542 goto out;
1543
Fu Weic2743a32017-04-01 01:51:04 +08001544 /*
1545 * While unlikely, it's theoretically possible that none of the frames
1546 * in a timer expose the combination of feature we want.
1547 */
Matthias Kaehlcked197f792017-07-31 11:37:28 -07001548 for (i = 0; i < timer_count; i++) {
Fu Weic2743a32017-04-01 01:51:04 +08001549 timer = &timers[i];
1550
1551 frame = arch_timer_mem_find_best_frame(timer);
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001552 if (!best_frame)
1553 best_frame = frame;
1554
1555 ret = arch_timer_mem_verify_cntfrq(timer);
1556 if (ret) {
1557 pr_err("Disabling MMIO timers due to CNTFRQ mismatch\n");
1558 goto out;
1559 }
1560
1561 if (!best_frame) /* implies !frame */
1562 /*
1563 * Only complain about missing suitable frames if we
1564 * haven't already found one in a previous iteration.
1565 */
1566 pr_err("Unable to find a suitable frame in timer @ %pa\n",
1567 &timer->cntctlbase);
Fu Weic2743a32017-04-01 01:51:04 +08001568 }
1569
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001570 if (best_frame)
1571 ret = arch_timer_mem_frame_register(best_frame);
Fu Weic2743a32017-04-01 01:51:04 +08001572out:
1573 kfree(timers);
1574 return ret;
1575}
1576
1577/* Initialize per-processor generic timer and memory-mapped timer(if present) */
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001578static int __init arch_timer_acpi_init(struct acpi_table_header *table)
1579{
Fu Weic2743a32017-04-01 01:51:04 +08001580 int ret, platform_timer_count;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001581
Fu Wei8a5c21d2017-01-18 21:25:26 +08001582 if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
Fu Weided24012017-01-18 21:25:25 +08001583 pr_warn("already initialized, skipping\n");
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001584 return -EINVAL;
1585 }
1586
Fu Wei8a5c21d2017-01-18 21:25:26 +08001587 arch_timers_present |= ARCH_TIMER_TYPE_CP15;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001588
Fu Weic2743a32017-04-01 01:51:04 +08001589 ret = acpi_gtdt_init(table, &platform_timer_count);
Fu Weif79d2092017-04-01 01:51:02 +08001590 if (ret) {
1591 pr_err("Failed to init GTDT table.\n");
1592 return ret;
1593 }
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001594
Fu Weiee34f1e2017-01-18 21:25:27 +08001595 arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI] =
Fu Weif79d2092017-04-01 01:51:02 +08001596 acpi_gtdt_map_ppi(ARCH_TIMER_PHYS_NONSECURE_PPI);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001597
Fu Weiee34f1e2017-01-18 21:25:27 +08001598 arch_timer_ppi[ARCH_TIMER_VIRT_PPI] =
Fu Weif79d2092017-04-01 01:51:02 +08001599 acpi_gtdt_map_ppi(ARCH_TIMER_VIRT_PPI);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001600
Fu Weiee34f1e2017-01-18 21:25:27 +08001601 arch_timer_ppi[ARCH_TIMER_HYP_PPI] =
Fu Weif79d2092017-04-01 01:51:02 +08001602 acpi_gtdt_map_ppi(ARCH_TIMER_HYP_PPI);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001603
Andre Przywaraee793042018-07-06 09:11:50 +01001604 arch_timer_populate_kvm_info();
Fu Weica0e1b52017-03-22 00:31:15 +08001605
Fu Wei5d3dfa92017-03-22 00:31:13 +08001606 /*
1607 * When probing via ACPI, we have no mechanism to override the sysreg
1608 * CNTFRQ value. This *must* be correct.
1609 */
1610 arch_timer_rate = arch_timer_get_cntfrq();
1611 if (!arch_timer_rate) {
1612 pr_err(FW_BUG "frequency not available.\n");
1613 return -EINVAL;
1614 }
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001615
Fu Wei4502b6b2017-01-18 21:25:30 +08001616 arch_timer_uses_ppi = arch_timer_select_ppi();
1617 if (!arch_timer_ppi[arch_timer_uses_ppi]) {
1618 pr_err("No interrupt available, giving up\n");
1619 return -EINVAL;
1620 }
1621
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001622 /* Always-on capability */
Fu Weif79d2092017-04-01 01:51:02 +08001623 arch_timer_c3stop = acpi_gtdt_c3stop(arch_timer_uses_ppi);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001624
Marc Zyngier5a38bca2017-02-21 14:37:30 +00001625 /* Check for globally applicable workarounds */
1626 arch_timer_check_ool_workaround(ate_match_acpi_oem_info, table);
1627
Fu Weica0e1b52017-03-22 00:31:15 +08001628 ret = arch_timer_register();
1629 if (ret)
1630 return ret;
1631
Fu Weic2743a32017-04-01 01:51:04 +08001632 if (platform_timer_count &&
1633 arch_timer_mem_acpi_init(platform_timer_count))
1634 pr_err("Failed to initialize memory-mapped timer.\n");
1635
Fu Weica0e1b52017-03-22 00:31:15 +08001636 return arch_timer_common_init();
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001637}
Daniel Lezcano77d62f52017-05-26 17:42:25 +02001638TIMER_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001639#endif