blob: e0f167e5e7927920456fe6e6bfa4d6305d76d79f [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Mark Rutland8a4da6e2012-11-12 14:33:44 +00002/*
3 * linux/drivers/clocksource/arm_arch_timer.c
4 *
5 * Copyright (C) 2011 ARM Ltd.
6 * All Rights Reserved
Mark Rutland8a4da6e2012-11-12 14:33:44 +00007 */
Marc Zyngierf005bd72016-08-01 10:54:15 +01008
Yangtao Li91556972019-03-05 12:08:51 -05009#define pr_fmt(fmt) "arch_timer: " fmt
Marc Zyngierf005bd72016-08-01 10:54:15 +010010
Mark Rutland8a4da6e2012-11-12 14:33:44 +000011#include <linux/init.h>
12#include <linux/kernel.h>
13#include <linux/device.h>
14#include <linux/smp.h>
15#include <linux/cpu.h>
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +010016#include <linux/cpu_pm.h>
Mark Rutland8a4da6e2012-11-12 14:33:44 +000017#include <linux/clockchips.h>
Richard Cochran7c8f1e72015-01-06 14:26:13 +010018#include <linux/clocksource.h>
Jianyong Wu100148d2020-12-09 14:09:28 +080019#include <linux/clocksource_ids.h>
Mark Rutland8a4da6e2012-11-12 14:33:44 +000020#include <linux/interrupt.h>
21#include <linux/of_irq.h>
Stephen Boyd22006992013-07-18 16:59:32 -070022#include <linux/of_address.h>
Mark Rutland8a4da6e2012-11-12 14:33:44 +000023#include <linux/io.h>
Stephen Boyd22006992013-07-18 16:59:32 -070024#include <linux/slab.h>
Ingo Molnare6017572017-02-01 16:36:40 +010025#include <linux/sched/clock.h>
Stephen Boyd65cd4f62013-07-18 16:21:18 -070026#include <linux/sched_clock.h>
Hanjun Guob09ca1e2015-03-24 14:02:50 +000027#include <linux/acpi.h>
Jianyong Wu300bb1f2020-12-09 14:09:30 +080028#include <linux/arm-smccc.h>
29#include <linux/ptp_kvm.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;
Vincenzo Frascinoa67de482020-02-24 15:15:52 +000075#ifdef CONFIG_GENERIC_GETTIMEOFDAY
Thomas Gleixner5e3c6a32020-02-07 13:38:58 +010076static enum vdso_clock_mode vdso_default = VDSO_CLOCKMODE_ARCHTIMER;
Vincenzo Frascinoa67de482020-02-24 15:15:52 +000077#else
78static enum vdso_clock_mode vdso_default = VDSO_CLOCKMODE_NONE;
79#endif /* CONFIG_GENERIC_GETTIMEOFDAY */
Mark Rutland8a4da6e2012-11-12 14:33:44 +000080
Julien Thierryec5c8e42017-10-13 14:32:55 +010081static cpumask_t evtstrm_available = CPU_MASK_NONE;
Will Deacon46fd5c62016-06-27 17:30:13 +010082static bool evtstrm_enable = IS_ENABLED(CONFIG_ARM_ARCH_TIMER_EVTSTREAM);
83
84static int __init early_evtstrm_cfg(char *buf)
85{
86 return strtobool(buf, &evtstrm_enable);
87}
88early_param("clocksource.arm_arch_timer.evtstrm", early_evtstrm_cfg);
89
Mark Rutland8a4da6e2012-11-12 14:33:44 +000090/*
91 * Architected system timer support.
92 */
93
Marc Zyngierf4e00a12017-01-20 18:28:32 +000094static __always_inline
95void arch_timer_reg_write(int access, enum arch_timer_reg reg, u32 val,
96 struct clock_event_device *clk)
97{
98 if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
99 struct arch_timer *timer = to_arch_timer(clk);
100 switch (reg) {
101 case ARCH_TIMER_REG_CTRL:
102 writel_relaxed(val, timer->base + CNTP_CTL);
103 break;
104 case ARCH_TIMER_REG_TVAL:
105 writel_relaxed(val, timer->base + CNTP_TVAL);
106 break;
107 }
108 } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
109 struct arch_timer *timer = to_arch_timer(clk);
110 switch (reg) {
111 case ARCH_TIMER_REG_CTRL:
112 writel_relaxed(val, timer->base + CNTV_CTL);
113 break;
114 case ARCH_TIMER_REG_TVAL:
115 writel_relaxed(val, timer->base + CNTV_TVAL);
116 break;
117 }
118 } else {
119 arch_timer_reg_write_cp15(access, reg, val);
120 }
121}
122
123static __always_inline
124u32 arch_timer_reg_read(int access, enum arch_timer_reg reg,
125 struct clock_event_device *clk)
126{
127 u32 val;
128
129 if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
130 struct arch_timer *timer = to_arch_timer(clk);
131 switch (reg) {
132 case ARCH_TIMER_REG_CTRL:
133 val = readl_relaxed(timer->base + CNTP_CTL);
134 break;
135 case ARCH_TIMER_REG_TVAL:
136 val = readl_relaxed(timer->base + CNTP_TVAL);
137 break;
138 }
139 } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
140 struct arch_timer *timer = to_arch_timer(clk);
141 switch (reg) {
142 case ARCH_TIMER_REG_CTRL:
143 val = readl_relaxed(timer->base + CNTV_CTL);
144 break;
145 case ARCH_TIMER_REG_TVAL:
146 val = readl_relaxed(timer->base + CNTV_TVAL);
147 break;
148 }
149 } else {
150 val = arch_timer_reg_read_cp15(access, reg);
151 }
152
153 return val;
154}
155
Julien Thierry5d6168f2019-05-24 10:10:25 +0100156static notrace u64 arch_counter_get_cntpct_stable(void)
Marc Zyngier0ea41532019-04-08 16:49:07 +0100157{
158 return __arch_counter_get_cntpct_stable();
159}
160
Julien Thierry5d6168f2019-05-24 10:10:25 +0100161static notrace u64 arch_counter_get_cntpct(void)
Marc Zyngier0ea41532019-04-08 16:49:07 +0100162{
163 return __arch_counter_get_cntpct();
164}
165
Julien Thierry5d6168f2019-05-24 10:10:25 +0100166static notrace u64 arch_counter_get_cntvct_stable(void)
Marc Zyngier0ea41532019-04-08 16:49:07 +0100167{
168 return __arch_counter_get_cntvct_stable();
169}
170
Julien Thierry5d6168f2019-05-24 10:10:25 +0100171static notrace u64 arch_counter_get_cntvct(void)
Marc Zyngier0ea41532019-04-08 16:49:07 +0100172{
173 return __arch_counter_get_cntvct();
174}
175
Marc Zyngier992dd162017-02-01 11:53:46 +0000176/*
177 * Default to cp15 based access because arm64 uses this function for
178 * sched_clock() before DT is probed and the cp15 method is guaranteed
179 * to exist on arm64. arm doesn't use this before DT is probed so even
180 * if we don't have the cp15 accessors we won't have a problem.
181 */
182u64 (*arch_timer_read_counter)(void) = arch_counter_get_cntvct;
Christoffer Dalle6d68b002017-07-05 11:04:28 +0200183EXPORT_SYMBOL_GPL(arch_timer_read_counter);
Marc Zyngier992dd162017-02-01 11:53:46 +0000184
185static u64 arch_counter_read(struct clocksource *cs)
186{
187 return arch_timer_read_counter();
188}
189
190static u64 arch_counter_read_cc(const struct cyclecounter *cc)
191{
192 return arch_timer_read_counter();
193}
194
195static struct clocksource clocksource_counter = {
196 .name = "arch_sys_counter",
Jianyong Wu100148d2020-12-09 14:09:28 +0800197 .id = CSID_ARM_ARCH_COUNTER,
Marc Zyngier992dd162017-02-01 11:53:46 +0000198 .rating = 400,
199 .read = arch_counter_read,
200 .mask = CLOCKSOURCE_MASK(56),
201 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
202};
203
204static struct cyclecounter cyclecounter __ro_after_init = {
205 .read = arch_counter_read_cc,
206 .mask = CLOCKSOURCE_MASK(56),
207};
208
Marc Zyngier5a38bca2017-02-21 14:37:30 +0000209struct ate_acpi_oem_info {
210 char oem_id[ACPI_OEM_ID_SIZE + 1];
211 char oem_table_id[ACPI_OEM_TABLE_ID_SIZE + 1];
212 u32 oem_revision;
213};
214
Scott Woodf6dc1572016-09-22 03:35:17 -0500215#ifdef CONFIG_FSL_ERRATUM_A008585
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000216/*
217 * The number of retries is an arbitrary value well beyond the highest number
218 * of iterations the loop has been observed to take.
219 */
220#define __fsl_a008585_read_reg(reg) ({ \
221 u64 _old, _new; \
222 int _retries = 200; \
223 \
224 do { \
225 _old = read_sysreg(reg); \
226 _new = read_sysreg(reg); \
227 _retries--; \
228 } while (unlikely(_old != _new) && _retries); \
229 \
230 WARN_ON_ONCE(!_retries); \
231 _new; \
232})
Scott Woodf6dc1572016-09-22 03:35:17 -0500233
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000234static u32 notrace fsl_a008585_read_cntp_tval_el0(void)
Scott Woodf6dc1572016-09-22 03:35:17 -0500235{
236 return __fsl_a008585_read_reg(cntp_tval_el0);
237}
238
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000239static u32 notrace fsl_a008585_read_cntv_tval_el0(void)
Scott Woodf6dc1572016-09-22 03:35:17 -0500240{
241 return __fsl_a008585_read_reg(cntv_tval_el0);
242}
243
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200244static u64 notrace fsl_a008585_read_cntpct_el0(void)
245{
246 return __fsl_a008585_read_reg(cntpct_el0);
247}
248
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000249static u64 notrace fsl_a008585_read_cntvct_el0(void)
Scott Woodf6dc1572016-09-22 03:35:17 -0500250{
251 return __fsl_a008585_read_reg(cntvct_el0);
252}
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000253#endif
254
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000255#ifdef CONFIG_HISILICON_ERRATUM_161010101
256/*
257 * Verify whether the value of the second read is larger than the first by
258 * less than 32 is the only way to confirm the value is correct, so clear the
259 * lower 5 bits to check whether the difference is greater than 32 or not.
260 * Theoretically the erratum should not occur more than twice in succession
261 * when reading the system counter, but it is possible that some interrupts
262 * may lead to more than twice read errors, triggering the warning, so setting
263 * the number of retries far beyond the number of iterations the loop has been
264 * observed to take.
265 */
266#define __hisi_161010101_read_reg(reg) ({ \
267 u64 _old, _new; \
268 int _retries = 50; \
269 \
270 do { \
271 _old = read_sysreg(reg); \
272 _new = read_sysreg(reg); \
273 _retries--; \
274 } while (unlikely((_new - _old) >> 5) && _retries); \
275 \
276 WARN_ON_ONCE(!_retries); \
277 _new; \
278})
279
280static u32 notrace hisi_161010101_read_cntp_tval_el0(void)
281{
282 return __hisi_161010101_read_reg(cntp_tval_el0);
283}
284
285static u32 notrace hisi_161010101_read_cntv_tval_el0(void)
286{
287 return __hisi_161010101_read_reg(cntv_tval_el0);
288}
289
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200290static u64 notrace hisi_161010101_read_cntpct_el0(void)
291{
292 return __hisi_161010101_read_reg(cntpct_el0);
293}
294
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000295static u64 notrace hisi_161010101_read_cntvct_el0(void)
296{
297 return __hisi_161010101_read_reg(cntvct_el0);
298}
Marc Zyngierd003d022017-02-21 15:04:27 +0000299
300static struct ate_acpi_oem_info hisi_161010101_oem_info[] = {
301 /*
302 * Note that trailing spaces are required to properly match
303 * the OEM table information.
304 */
305 {
306 .oem_id = "HISI ",
307 .oem_table_id = "HIP05 ",
308 .oem_revision = 0,
309 },
310 {
311 .oem_id = "HISI ",
312 .oem_table_id = "HIP06 ",
313 .oem_revision = 0,
314 },
315 {
316 .oem_id = "HISI ",
317 .oem_table_id = "HIP07 ",
318 .oem_revision = 0,
319 },
320 { /* Sentinel indicating the end of the OEM array */ },
321};
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000322#endif
323
Marc Zyngierfa8d8152017-01-27 12:52:31 +0000324#ifdef CONFIG_ARM64_ERRATUM_858921
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200325static u64 notrace arm64_858921_read_cntpct_el0(void)
326{
327 u64 old, new;
328
329 old = read_sysreg(cntpct_el0);
330 new = read_sysreg(cntpct_el0);
331 return (((old ^ new) >> 32) & 1) ? old : new;
332}
333
Marc Zyngierfa8d8152017-01-27 12:52:31 +0000334static u64 notrace arm64_858921_read_cntvct_el0(void)
335{
336 u64 old, new;
337
338 old = read_sysreg(cntvct_el0);
339 new = read_sysreg(cntvct_el0);
340 return (((old ^ new) >> 32) & 1) ? old : new;
341}
342#endif
343
Samuel Hollandc950ca82019-01-12 20:17:18 -0600344#ifdef CONFIG_SUN50I_ERRATUM_UNKNOWN1
345/*
346 * The low bits of the counter registers are indeterminate while bit 10 or
347 * greater is rolling over. Since the counter value can jump both backward
348 * (7ff -> 000 -> 800) and forward (7ff -> fff -> 800), ignore register values
349 * with all ones or all zeros in the low bits. Bound the loop by the maximum
350 * number of CPU cycles in 3 consecutive 24 MHz counter periods.
351 */
352#define __sun50i_a64_read_reg(reg) ({ \
353 u64 _val; \
354 int _retries = 150; \
355 \
356 do { \
357 _val = read_sysreg(reg); \
358 _retries--; \
359 } while (((_val + 1) & GENMASK(9, 0)) <= 1 && _retries); \
360 \
361 WARN_ON_ONCE(!_retries); \
362 _val; \
363})
364
365static u64 notrace sun50i_a64_read_cntpct_el0(void)
366{
367 return __sun50i_a64_read_reg(cntpct_el0);
368}
369
370static u64 notrace sun50i_a64_read_cntvct_el0(void)
371{
372 return __sun50i_a64_read_reg(cntvct_el0);
373}
374
375static u32 notrace sun50i_a64_read_cntp_tval_el0(void)
376{
377 return read_sysreg(cntp_cval_el0) - sun50i_a64_read_cntpct_el0();
378}
379
380static u32 notrace sun50i_a64_read_cntv_tval_el0(void)
381{
382 return read_sysreg(cntv_cval_el0) - sun50i_a64_read_cntvct_el0();
383}
384#endif
385
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000386#ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
Mark Rutlanda7fb4572017-10-16 16:28:39 +0100387DEFINE_PER_CPU(const struct arch_timer_erratum_workaround *, timer_unstable_counter_workaround);
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000388EXPORT_SYMBOL_GPL(timer_unstable_counter_workaround);
389
Marc Zyngier0ea41532019-04-08 16:49:07 +0100390static atomic_t timer_unstable_counter_workaround_in_use = ATOMIC_INIT(0);
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000391
Marc Zyngier83280892017-01-27 10:27:09 +0000392static void erratum_set_next_event_tval_generic(const int access, unsigned long evt,
393 struct clock_event_device *clk)
394{
395 unsigned long ctrl;
Christoffer Dalle6d68b002017-07-05 11:04:28 +0200396 u64 cval;
Marc Zyngier83280892017-01-27 10:27:09 +0000397
398 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
399 ctrl |= ARCH_TIMER_CTRL_ENABLE;
400 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
401
Christoffer Dalle6d68b002017-07-05 11:04:28 +0200402 if (access == ARCH_TIMER_PHYS_ACCESS) {
Keqian Zhud8cc3902020-12-04 15:31:25 +0800403 cval = evt + arch_counter_get_cntpct_stable();
Marc Zyngier83280892017-01-27 10:27:09 +0000404 write_sysreg(cval, cntp_cval_el0);
Christoffer Dalle6d68b002017-07-05 11:04:28 +0200405 } else {
Keqian Zhud8cc3902020-12-04 15:31:25 +0800406 cval = evt + arch_counter_get_cntvct_stable();
Marc Zyngier83280892017-01-27 10:27:09 +0000407 write_sysreg(cval, cntv_cval_el0);
Christoffer Dalle6d68b002017-07-05 11:04:28 +0200408 }
Marc Zyngier83280892017-01-27 10:27:09 +0000409
410 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
411}
412
Arnd Bergmanneb645222017-04-19 19:37:09 +0200413static __maybe_unused int erratum_set_next_event_tval_virt(unsigned long evt,
Marc Zyngier83280892017-01-27 10:27:09 +0000414 struct clock_event_device *clk)
415{
416 erratum_set_next_event_tval_generic(ARCH_TIMER_VIRT_ACCESS, evt, clk);
417 return 0;
418}
419
Arnd Bergmanneb645222017-04-19 19:37:09 +0200420static __maybe_unused int erratum_set_next_event_tval_phys(unsigned long evt,
Marc Zyngier83280892017-01-27 10:27:09 +0000421 struct clock_event_device *clk)
422{
423 erratum_set_next_event_tval_generic(ARCH_TIMER_PHYS_ACCESS, evt, clk);
424 return 0;
425}
426
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000427static const struct arch_timer_erratum_workaround ool_workarounds[] = {
428#ifdef CONFIG_FSL_ERRATUM_A008585
429 {
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000430 .match_type = ate_match_dt,
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000431 .id = "fsl,erratum-a008585",
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000432 .desc = "Freescale erratum a005858",
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000433 .read_cntp_tval_el0 = fsl_a008585_read_cntp_tval_el0,
434 .read_cntv_tval_el0 = fsl_a008585_read_cntv_tval_el0,
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200435 .read_cntpct_el0 = fsl_a008585_read_cntpct_el0,
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000436 .read_cntvct_el0 = fsl_a008585_read_cntvct_el0,
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000437 .set_next_event_phys = erratum_set_next_event_tval_phys,
438 .set_next_event_virt = erratum_set_next_event_tval_virt,
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000439 },
440#endif
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000441#ifdef CONFIG_HISILICON_ERRATUM_161010101
442 {
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000443 .match_type = ate_match_dt,
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000444 .id = "hisilicon,erratum-161010101",
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000445 .desc = "HiSilicon erratum 161010101",
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000446 .read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
447 .read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200448 .read_cntpct_el0 = hisi_161010101_read_cntpct_el0,
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000449 .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000450 .set_next_event_phys = erratum_set_next_event_tval_phys,
451 .set_next_event_virt = erratum_set_next_event_tval_virt,
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000452 },
Marc Zyngierd003d022017-02-21 15:04:27 +0000453 {
454 .match_type = ate_match_acpi_oem_info,
455 .id = hisi_161010101_oem_info,
456 .desc = "HiSilicon erratum 161010101",
457 .read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
458 .read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200459 .read_cntpct_el0 = hisi_161010101_read_cntpct_el0,
Marc Zyngierd003d022017-02-21 15:04:27 +0000460 .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
461 .set_next_event_phys = erratum_set_next_event_tval_phys,
462 .set_next_event_virt = erratum_set_next_event_tval_virt,
463 },
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000464#endif
Marc Zyngierfa8d8152017-01-27 12:52:31 +0000465#ifdef CONFIG_ARM64_ERRATUM_858921
466 {
467 .match_type = ate_match_local_cap_id,
468 .id = (void *)ARM64_WORKAROUND_858921,
469 .desc = "ARM erratum 858921",
Christoffer Dallf2e600c2017-10-18 13:06:25 +0200470 .read_cntpct_el0 = arm64_858921_read_cntpct_el0,
Marc Zyngierfa8d8152017-01-27 12:52:31 +0000471 .read_cntvct_el0 = arm64_858921_read_cntvct_el0,
472 },
473#endif
Samuel Hollandc950ca82019-01-12 20:17:18 -0600474#ifdef CONFIG_SUN50I_ERRATUM_UNKNOWN1
475 {
476 .match_type = ate_match_dt,
477 .id = "allwinner,erratum-unknown1",
478 .desc = "Allwinner erratum UNKNOWN1",
479 .read_cntp_tval_el0 = sun50i_a64_read_cntp_tval_el0,
480 .read_cntv_tval_el0 = sun50i_a64_read_cntv_tval_el0,
481 .read_cntpct_el0 = sun50i_a64_read_cntpct_el0,
482 .read_cntvct_el0 = sun50i_a64_read_cntvct_el0,
483 .set_next_event_phys = erratum_set_next_event_tval_phys,
484 .set_next_event_virt = erratum_set_next_event_tval_virt,
485 },
486#endif
Marc Zyngier4b661d62020-07-06 17:38:01 +0100487#ifdef CONFIG_ARM64_ERRATUM_1418040
488 {
489 .match_type = ate_match_local_cap_id,
490 .id = (void *)ARM64_WORKAROUND_1418040,
491 .desc = "ARM erratum 1418040",
492 .disable_compat_vdso = true,
493 },
494#endif
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000495};
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000496
497typedef bool (*ate_match_fn_t)(const struct arch_timer_erratum_workaround *,
498 const void *);
499
500static
501bool arch_timer_check_dt_erratum(const struct arch_timer_erratum_workaround *wa,
502 const void *arg)
503{
504 const struct device_node *np = arg;
505
506 return of_property_read_bool(np, wa->id);
507}
508
Marc Zyngier00640302017-03-20 16:47:59 +0000509static
510bool arch_timer_check_local_cap_erratum(const struct arch_timer_erratum_workaround *wa,
511 const void *arg)
512{
513 return this_cpu_has_cap((uintptr_t)wa->id);
514}
515
Marc Zyngier5a38bca2017-02-21 14:37:30 +0000516
517static
518bool arch_timer_check_acpi_oem_erratum(const struct arch_timer_erratum_workaround *wa,
519 const void *arg)
520{
521 static const struct ate_acpi_oem_info empty_oem_info = {};
522 const struct ate_acpi_oem_info *info = wa->id;
523 const struct acpi_table_header *table = arg;
524
525 /* Iterate over the ACPI OEM info array, looking for a match */
526 while (memcmp(info, &empty_oem_info, sizeof(*info))) {
527 if (!memcmp(info->oem_id, table->oem_id, ACPI_OEM_ID_SIZE) &&
528 !memcmp(info->oem_table_id, table->oem_table_id, ACPI_OEM_TABLE_ID_SIZE) &&
529 info->oem_revision == table->oem_revision)
530 return true;
531
532 info++;
533 }
534
535 return false;
536}
537
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000538static const struct arch_timer_erratum_workaround *
539arch_timer_iterate_errata(enum arch_timer_erratum_match_type type,
540 ate_match_fn_t match_fn,
541 void *arg)
542{
543 int i;
544
545 for (i = 0; i < ARRAY_SIZE(ool_workarounds); i++) {
546 if (ool_workarounds[i].match_type != type)
547 continue;
548
549 if (match_fn(&ool_workarounds[i], arg))
550 return &ool_workarounds[i];
551 }
552
553 return NULL;
554}
555
556static
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000557void arch_timer_enable_workaround(const struct arch_timer_erratum_workaround *wa,
558 bool local)
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000559{
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000560 int i;
561
562 if (local) {
563 __this_cpu_write(timer_unstable_counter_workaround, wa);
564 } else {
565 for_each_possible_cpu(i)
566 per_cpu(timer_unstable_counter_workaround, i) = wa;
567 }
568
Marc Zyngier0ea41532019-04-08 16:49:07 +0100569 if (wa->read_cntvct_el0 || wa->read_cntpct_el0)
570 atomic_set(&timer_unstable_counter_workaround_in_use, 1);
Marc Zyngiera86bd132017-02-01 12:07:15 +0000571
572 /*
573 * Don't use the vdso fastpath if errata require using the
574 * out-of-line counter accessor. We may change our mind pretty
575 * late in the game (with a per-CPU erratum, for example), so
576 * change both the default value and the vdso itself.
577 */
578 if (wa->read_cntvct_el0) {
Thomas Gleixner5e3c6a32020-02-07 13:38:58 +0100579 clocksource_counter.vdso_clock_mode = VDSO_CLOCKMODE_NONE;
580 vdso_default = VDSO_CLOCKMODE_NONE;
Marc Zyngierc1fbec42020-07-06 17:38:00 +0100581 } else if (wa->disable_compat_vdso && vdso_default != VDSO_CLOCKMODE_NONE) {
582 vdso_default = VDSO_CLOCKMODE_ARCHTIMER_NOCOMPAT;
583 clocksource_counter.vdso_clock_mode = vdso_default;
Marc Zyngiera86bd132017-02-01 12:07:15 +0000584 }
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000585}
586
587static void arch_timer_check_ool_workaround(enum arch_timer_erratum_match_type type,
588 void *arg)
589{
Marc Zyngiera862fc22019-04-08 16:49:06 +0100590 const struct arch_timer_erratum_workaround *wa, *__wa;
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000591 ate_match_fn_t match_fn = NULL;
Marc Zyngier00640302017-03-20 16:47:59 +0000592 bool local = false;
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000593
594 switch (type) {
595 case ate_match_dt:
596 match_fn = arch_timer_check_dt_erratum;
597 break;
Marc Zyngier00640302017-03-20 16:47:59 +0000598 case ate_match_local_cap_id:
599 match_fn = arch_timer_check_local_cap_erratum;
600 local = true;
601 break;
Marc Zyngier5a38bca2017-02-21 14:37:30 +0000602 case ate_match_acpi_oem_info:
603 match_fn = arch_timer_check_acpi_oem_erratum;
604 break;
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000605 default:
606 WARN_ON(1);
607 return;
608 }
609
610 wa = arch_timer_iterate_errata(type, match_fn, arg);
611 if (!wa)
612 return;
613
Marc Zyngiera862fc22019-04-08 16:49:06 +0100614 __wa = __this_cpu_read(timer_unstable_counter_workaround);
615 if (__wa && wa != __wa)
616 pr_warn("Can't enable workaround for %s (clashes with %s\n)",
617 wa->desc, __wa->desc);
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000618
Marc Zyngiera862fc22019-04-08 16:49:06 +0100619 if (__wa)
620 return;
Marc Zyngier00640302017-03-20 16:47:59 +0000621
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000622 arch_timer_enable_workaround(wa, local);
Marc Zyngier00640302017-03-20 16:47:59 +0000623 pr_info("Enabling %s workaround for %s\n",
624 local ? "local" : "global", wa->desc);
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000625}
626
Marc Zyngiera86bd132017-02-01 12:07:15 +0000627static bool arch_timer_this_cpu_has_cntvct_wa(void)
628{
Marc Zyngier5ef19a12019-04-08 16:49:04 +0100629 return has_erratum_handler(read_cntvct_el0);
Marc Zyngiera86bd132017-02-01 12:07:15 +0000630}
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000631
Marc Zyngier0ea41532019-04-08 16:49:07 +0100632static bool arch_timer_counter_has_wa(void)
633{
634 return atomic_read(&timer_unstable_counter_workaround_in_use);
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000635}
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000636#else
637#define arch_timer_check_ool_workaround(t,a) do { } while(0)
Marc Zyngiera86bd132017-02-01 12:07:15 +0000638#define arch_timer_this_cpu_has_cntvct_wa() ({false;})
Marc Zyngier0ea41532019-04-08 16:49:07 +0100639#define arch_timer_counter_has_wa() ({false;})
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000640#endif /* CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND */
Scott Woodf6dc1572016-09-22 03:35:17 -0500641
Stephen Boyde09f3cc2013-07-18 16:59:28 -0700642static __always_inline irqreturn_t timer_handler(const int access,
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000643 struct clock_event_device *evt)
644{
645 unsigned long ctrl;
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200646
Stephen Boyd60faddf2013-07-18 16:59:31 -0700647 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000648 if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
649 ctrl |= ARCH_TIMER_CTRL_IT_MASK;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700650 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000651 evt->event_handler(evt);
652 return IRQ_HANDLED;
653 }
654
655 return IRQ_NONE;
656}
657
658static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
659{
660 struct clock_event_device *evt = dev_id;
661
662 return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
663}
664
665static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
666{
667 struct clock_event_device *evt = dev_id;
668
669 return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
670}
671
Stephen Boyd22006992013-07-18 16:59:32 -0700672static irqreturn_t arch_timer_handler_phys_mem(int irq, void *dev_id)
673{
674 struct clock_event_device *evt = dev_id;
675
676 return timer_handler(ARCH_TIMER_MEM_PHYS_ACCESS, evt);
677}
678
679static irqreturn_t arch_timer_handler_virt_mem(int irq, void *dev_id)
680{
681 struct clock_event_device *evt = dev_id;
682
683 return timer_handler(ARCH_TIMER_MEM_VIRT_ACCESS, evt);
684}
685
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530686static __always_inline int timer_shutdown(const int access,
687 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000688{
689 unsigned long ctrl;
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530690
691 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
692 ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
693 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
694
695 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000696}
697
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530698static int arch_timer_shutdown_virt(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000699{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530700 return timer_shutdown(ARCH_TIMER_VIRT_ACCESS, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000701}
702
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530703static int arch_timer_shutdown_phys(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000704{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530705 return timer_shutdown(ARCH_TIMER_PHYS_ACCESS, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000706}
707
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530708static int arch_timer_shutdown_virt_mem(struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700709{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530710 return timer_shutdown(ARCH_TIMER_MEM_VIRT_ACCESS, clk);
Stephen Boyd22006992013-07-18 16:59:32 -0700711}
712
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530713static int arch_timer_shutdown_phys_mem(struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700714{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530715 return timer_shutdown(ARCH_TIMER_MEM_PHYS_ACCESS, clk);
Stephen Boyd22006992013-07-18 16:59:32 -0700716}
717
Stephen Boyd60faddf2013-07-18 16:59:31 -0700718static __always_inline void set_next_event(const int access, unsigned long evt,
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200719 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000720{
721 unsigned long ctrl;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700722 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000723 ctrl |= ARCH_TIMER_CTRL_ENABLE;
724 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700725 arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt, clk);
726 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000727}
728
729static int arch_timer_set_next_event_virt(unsigned long evt,
Stephen Boyd60faddf2013-07-18 16:59:31 -0700730 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000731{
Stephen Boyd60faddf2013-07-18 16:59:31 -0700732 set_next_event(ARCH_TIMER_VIRT_ACCESS, evt, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000733 return 0;
734}
735
736static int arch_timer_set_next_event_phys(unsigned long evt,
Stephen Boyd60faddf2013-07-18 16:59:31 -0700737 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000738{
Stephen Boyd60faddf2013-07-18 16:59:31 -0700739 set_next_event(ARCH_TIMER_PHYS_ACCESS, evt, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000740 return 0;
741}
742
Stephen Boyd22006992013-07-18 16:59:32 -0700743static int arch_timer_set_next_event_virt_mem(unsigned long evt,
744 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000745{
Stephen Boyd22006992013-07-18 16:59:32 -0700746 set_next_event(ARCH_TIMER_MEM_VIRT_ACCESS, evt, clk);
747 return 0;
748}
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000749
Stephen Boyd22006992013-07-18 16:59:32 -0700750static int arch_timer_set_next_event_phys_mem(unsigned long evt,
751 struct clock_event_device *clk)
752{
753 set_next_event(ARCH_TIMER_MEM_PHYS_ACCESS, evt, clk);
754 return 0;
755}
756
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200757static void __arch_timer_setup(unsigned type,
758 struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700759{
760 clk->features = CLOCK_EVT_FEAT_ONESHOT;
761
Fu Wei8a5c21d2017-01-18 21:25:26 +0800762 if (type == ARCH_TIMER_TYPE_CP15) {
Marc Zyngier5ef19a12019-04-08 16:49:04 +0100763 typeof(clk->set_next_event) sne;
764
765 arch_timer_check_ool_workaround(ate_match_local_cap_id, NULL);
766
Lorenzo Pieralisi82a561942014-04-08 10:04:32 +0100767 if (arch_timer_c3stop)
768 clk->features |= CLOCK_EVT_FEAT_C3STOP;
Stephen Boyd22006992013-07-18 16:59:32 -0700769 clk->name = "arch_sys_timer";
770 clk->rating = 450;
771 clk->cpumask = cpumask_of(smp_processor_id());
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000772 clk->irq = arch_timer_ppi[arch_timer_uses_ppi];
773 switch (arch_timer_uses_ppi) {
Fu Weiee34f1e2017-01-18 21:25:27 +0800774 case ARCH_TIMER_VIRT_PPI:
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530775 clk->set_state_shutdown = arch_timer_shutdown_virt;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530776 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt;
Marc Zyngier5ef19a12019-04-08 16:49:04 +0100777 sne = erratum_handler(set_next_event_virt);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000778 break;
Fu Weiee34f1e2017-01-18 21:25:27 +0800779 case ARCH_TIMER_PHYS_SECURE_PPI:
780 case ARCH_TIMER_PHYS_NONSECURE_PPI:
781 case ARCH_TIMER_HYP_PPI:
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530782 clk->set_state_shutdown = arch_timer_shutdown_phys;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530783 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys;
Marc Zyngier5ef19a12019-04-08 16:49:04 +0100784 sne = erratum_handler(set_next_event_phys);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000785 break;
786 default:
787 BUG();
Stephen Boyd22006992013-07-18 16:59:32 -0700788 }
Scott Woodf6dc1572016-09-22 03:35:17 -0500789
Marc Zyngier5ef19a12019-04-08 16:49:04 +0100790 clk->set_next_event = sne;
Stephen Boyd22006992013-07-18 16:59:32 -0700791 } else {
Stephen Boyd7b52ad22014-01-06 14:56:17 -0800792 clk->features |= CLOCK_EVT_FEAT_DYNIRQ;
Stephen Boyd22006992013-07-18 16:59:32 -0700793 clk->name = "arch_mem_timer";
794 clk->rating = 400;
Sudeep Holla5e18e412018-07-09 16:45:36 +0100795 clk->cpumask = cpu_possible_mask;
Stephen Boyd22006992013-07-18 16:59:32 -0700796 if (arch_timer_mem_use_virtual) {
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530797 clk->set_state_shutdown = arch_timer_shutdown_virt_mem;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530798 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt_mem;
Stephen Boyd22006992013-07-18 16:59:32 -0700799 clk->set_next_event =
800 arch_timer_set_next_event_virt_mem;
801 } else {
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530802 clk->set_state_shutdown = arch_timer_shutdown_phys_mem;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530803 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys_mem;
Stephen Boyd22006992013-07-18 16:59:32 -0700804 clk->set_next_event =
805 arch_timer_set_next_event_phys_mem;
806 }
807 }
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000808
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530809 clk->set_state_shutdown(clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000810
Stephen Boyd22006992013-07-18 16:59:32 -0700811 clockevents_config_and_register(clk, arch_timer_rate, 0xf, 0x7fffffff);
812}
813
Nathan Lynche1ce5c72014-09-29 01:50:06 +0200814static void arch_timer_evtstrm_enable(int divider)
815{
816 u32 cntkctl = arch_timer_get_cntkctl();
817
818 cntkctl &= ~ARCH_TIMER_EVT_TRIGGER_MASK;
819 /* Set the divider and enable virtual event stream */
820 cntkctl |= (divider << ARCH_TIMER_EVT_TRIGGER_SHIFT)
821 | ARCH_TIMER_VIRT_EVT_EN;
822 arch_timer_set_cntkctl(cntkctl);
Andrew Murray5a354412019-06-13 13:51:02 +0100823 arch_timer_set_evtstrm_feature();
Julien Thierryec5c8e42017-10-13 14:32:55 +0100824 cpumask_set_cpu(smp_processor_id(), &evtstrm_available);
Nathan Lynche1ce5c72014-09-29 01:50:06 +0200825}
826
Will Deacon037f6372013-08-23 15:32:29 +0100827static void arch_timer_configure_evtstream(void)
828{
Keqian Zhu8b7770b2020-12-04 15:31:26 +0800829 int evt_stream_div, lsb;
Will Deacon037f6372013-08-23 15:32:29 +0100830
Keqian Zhu8b7770b2020-12-04 15:31:26 +0800831 /*
832 * As the event stream can at most be generated at half the frequency
833 * of the counter, use half the frequency when computing the divider.
834 */
835 evt_stream_div = arch_timer_rate / ARCH_TIMER_EVT_STREAM_FREQ / 2;
836
837 /*
838 * Find the closest power of two to the divisor. If the adjacent bit
839 * of lsb (last set bit, starts from 0) is set, then we use (lsb + 1).
840 */
841 lsb = fls(evt_stream_div) - 1;
842 if (lsb > 0 && (evt_stream_div & BIT(lsb - 1)))
843 lsb++;
844
Will Deacon037f6372013-08-23 15:32:29 +0100845 /* enable event stream */
Keqian Zhu8b7770b2020-12-04 15:31:26 +0800846 arch_timer_evtstrm_enable(max(0, min(lsb, 15)));
Will Deacon037f6372013-08-23 15:32:29 +0100847}
848
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200849static void arch_counter_set_user_access(void)
850{
851 u32 cntkctl = arch_timer_get_cntkctl();
852
Marc Zyngiera86bd132017-02-01 12:07:15 +0000853 /* Disable user access to the timers and both counters */
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200854 /* Also disable virtual event stream */
855 cntkctl &= ~(ARCH_TIMER_USR_PT_ACCESS_EN
856 | ARCH_TIMER_USR_VT_ACCESS_EN
Marc Zyngiera86bd132017-02-01 12:07:15 +0000857 | ARCH_TIMER_USR_VCT_ACCESS_EN
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200858 | ARCH_TIMER_VIRT_EVT_EN
859 | ARCH_TIMER_USR_PCT_ACCESS_EN);
860
Marc Zyngiera86bd132017-02-01 12:07:15 +0000861 /*
862 * Enable user access to the virtual counter if it doesn't
863 * need to be workaround. The vdso may have been already
864 * disabled though.
865 */
866 if (arch_timer_this_cpu_has_cntvct_wa())
867 pr_info("CPU%d: Trapping CNTVCT access\n", smp_processor_id());
868 else
869 cntkctl |= ARCH_TIMER_USR_VCT_ACCESS_EN;
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200870
871 arch_timer_set_cntkctl(cntkctl);
872}
873
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000874static bool arch_timer_has_nonsecure_ppi(void)
875{
Fu Weiee34f1e2017-01-18 21:25:27 +0800876 return (arch_timer_uses_ppi == ARCH_TIMER_PHYS_SECURE_PPI &&
877 arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000878}
879
Marc Zyngierf005bd72016-08-01 10:54:15 +0100880static u32 check_ppi_trigger(int irq)
881{
882 u32 flags = irq_get_trigger_type(irq);
883
884 if (flags != IRQF_TRIGGER_HIGH && flags != IRQF_TRIGGER_LOW) {
885 pr_warn("WARNING: Invalid trigger for IRQ%d, assuming level low\n", irq);
886 pr_warn("WARNING: Please fix your firmware\n");
887 flags = IRQF_TRIGGER_LOW;
888 }
889
890 return flags;
891}
892
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000893static int arch_timer_starting_cpu(unsigned int cpu)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000894{
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000895 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
Marc Zyngierf005bd72016-08-01 10:54:15 +0100896 u32 flags;
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000897
Fu Wei8a5c21d2017-01-18 21:25:26 +0800898 __arch_timer_setup(ARCH_TIMER_TYPE_CP15, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000899
Marc Zyngierf005bd72016-08-01 10:54:15 +0100900 flags = check_ppi_trigger(arch_timer_ppi[arch_timer_uses_ppi]);
901 enable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], flags);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000902
Marc Zyngierf005bd72016-08-01 10:54:15 +0100903 if (arch_timer_has_nonsecure_ppi()) {
Fu Weiee34f1e2017-01-18 21:25:27 +0800904 flags = check_ppi_trigger(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
905 enable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
906 flags);
Marc Zyngierf005bd72016-08-01 10:54:15 +0100907 }
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000908
909 arch_counter_set_user_access();
Will Deacon46fd5c62016-06-27 17:30:13 +0100910 if (evtstrm_enable)
Will Deacon037f6372013-08-23 15:32:29 +0100911 arch_timer_configure_evtstream();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000912
913 return 0;
914}
915
Ionela Voinescuc2658612020-03-05 09:06:27 +0000916static int validate_timer_rate(void)
917{
918 if (!arch_timer_rate)
919 return -EINVAL;
920
921 /* Arch timer frequency < 1MHz can cause trouble */
922 WARN_ON(arch_timer_rate < 1000000);
923
924 return 0;
925}
926
Fu Wei5d3dfa92017-03-22 00:31:13 +0800927/*
928 * For historical reasons, when probing with DT we use whichever (non-zero)
929 * rate was probed first, and don't verify that others match. If the first node
930 * probed has a clock-frequency property, this overrides the HW register.
931 */
932static void arch_timer_of_configure_rate(u32 rate, struct device_node *np)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000933{
Stephen Boyd22006992013-07-18 16:59:32 -0700934 /* Who has more than one independent system counter? */
935 if (arch_timer_rate)
936 return;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000937
Fu Wei5d3dfa92017-03-22 00:31:13 +0800938 if (of_property_read_u32(np, "clock-frequency", &arch_timer_rate))
939 arch_timer_rate = rate;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000940
Stephen Boyd22006992013-07-18 16:59:32 -0700941 /* Check the timer frequency. */
Ionela Voinescuc2658612020-03-05 09:06:27 +0000942 if (validate_timer_rate())
Fu Weided24012017-01-18 21:25:25 +0800943 pr_warn("frequency not available\n");
Stephen Boyd22006992013-07-18 16:59:32 -0700944}
945
946static void arch_timer_banner(unsigned type)
947{
Fu Weided24012017-01-18 21:25:25 +0800948 pr_info("%s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
Fu Wei8a5c21d2017-01-18 21:25:26 +0800949 type & ARCH_TIMER_TYPE_CP15 ? "cp15" : "",
950 type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ?
951 " and " : "",
952 type & ARCH_TIMER_TYPE_MEM ? "mmio" : "",
Fu Weided24012017-01-18 21:25:25 +0800953 (unsigned long)arch_timer_rate / 1000000,
954 (unsigned long)(arch_timer_rate / 10000) % 100,
Fu Wei8a5c21d2017-01-18 21:25:26 +0800955 type & ARCH_TIMER_TYPE_CP15 ?
Fu Weiee34f1e2017-01-18 21:25:27 +0800956 (arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI) ? "virt" : "phys" :
Stephen Boyd22006992013-07-18 16:59:32 -0700957 "",
Fu Wei8a5c21d2017-01-18 21:25:26 +0800958 type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ? "/" : "",
959 type & ARCH_TIMER_TYPE_MEM ?
Stephen Boyd22006992013-07-18 16:59:32 -0700960 arch_timer_mem_use_virtual ? "virt" : "phys" :
961 "");
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000962}
963
964u32 arch_timer_get_rate(void)
965{
966 return arch_timer_rate;
967}
968
Julien Thierryec5c8e42017-10-13 14:32:55 +0100969bool arch_timer_evtstrm_available(void)
970{
971 /*
972 * We might get called from a preemptible context. This is fine
973 * because availability of the event stream should be always the same
974 * for a preemptible context and context where we might resume a task.
975 */
976 return cpumask_test_cpu(raw_smp_processor_id(), &evtstrm_available);
977}
978
Stephen Boyd22006992013-07-18 16:59:32 -0700979static u64 arch_counter_get_cntvct_mem(void)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000980{
Stephen Boyd22006992013-07-18 16:59:32 -0700981 u32 vct_lo, vct_hi, tmp_hi;
982
983 do {
984 vct_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
985 vct_lo = readl_relaxed(arch_counter_base + CNTVCT_LO);
986 tmp_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
987 } while (vct_hi != tmp_hi);
988
989 return ((u64) vct_hi << 32) | vct_lo;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000990}
991
Julien Grallb4d6ce92016-04-11 16:32:51 +0100992static struct arch_timer_kvm_info arch_timer_kvm_info;
993
994struct arch_timer_kvm_info *arch_timer_get_kvm_info(void)
995{
996 return &arch_timer_kvm_info;
997}
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000998
Stephen Boyd22006992013-07-18 16:59:32 -0700999static void __init arch_counter_register(unsigned type)
1000{
1001 u64 start_count;
1002
1003 /* Register the CP15 based counter if we have one */
Fu Wei8a5c21d2017-01-18 21:25:26 +08001004 if (type & ARCH_TIMER_TYPE_CP15) {
Marc Zyngier0ea41532019-04-08 16:49:07 +01001005 u64 (*rd)(void);
Scott Woodf6dc1572016-09-22 03:35:17 -05001006
Marc Zyngier0ea41532019-04-08 16:49:07 +01001007 if ((IS_ENABLED(CONFIG_ARM64) && !is_hyp_mode_available()) ||
1008 arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI) {
1009 if (arch_timer_counter_has_wa())
1010 rd = arch_counter_get_cntvct_stable;
1011 else
1012 rd = arch_counter_get_cntvct;
1013 } else {
1014 if (arch_timer_counter_has_wa())
1015 rd = arch_counter_get_cntpct_stable;
1016 else
1017 rd = arch_counter_get_cntpct;
1018 }
1019
1020 arch_timer_read_counter = rd;
Thomas Gleixner5e3c6a32020-02-07 13:38:58 +01001021 clocksource_counter.vdso_clock_mode = vdso_default;
Nathan Lynch423bd692014-09-29 01:50:06 +02001022 } else {
Stephen Boyd22006992013-07-18 16:59:32 -07001023 arch_timer_read_counter = arch_counter_get_cntvct_mem;
Nathan Lynch423bd692014-09-29 01:50:06 +02001024 }
1025
Brian Norrisd8ec7592016-10-04 11:12:09 -07001026 if (!arch_counter_suspend_stop)
1027 clocksource_counter.flags |= CLOCK_SOURCE_SUSPEND_NONSTOP;
Stephen Boyd22006992013-07-18 16:59:32 -07001028 start_count = arch_timer_read_counter();
1029 clocksource_register_hz(&clocksource_counter, arch_timer_rate);
1030 cyclecounter.mult = clocksource_counter.mult;
1031 cyclecounter.shift = clocksource_counter.shift;
Julien Grallb4d6ce92016-04-11 16:32:51 +01001032 timecounter_init(&arch_timer_kvm_info.timecounter,
1033 &cyclecounter, start_count);
Thierry Reding4a7d3e82013-10-15 15:31:51 +02001034
1035 /* 56 bits minimum, so we assume worst case rollover */
1036 sched_clock_register(arch_timer_read_counter, 56, arch_timer_rate);
Stephen Boyd22006992013-07-18 16:59:32 -07001037}
1038
Paul Gortmaker8c37bb32013-06-19 11:32:08 -04001039static void arch_timer_stop(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001040{
Fu Weided24012017-01-18 21:25:25 +08001041 pr_debug("disable IRQ%d cpu #%d\n", clk->irq, smp_processor_id());
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001042
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001043 disable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi]);
1044 if (arch_timer_has_nonsecure_ppi())
Fu Weiee34f1e2017-01-18 21:25:27 +08001045 disable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001046
Viresh Kumar46c5bfd2015-06-12 13:30:12 +05301047 clk->set_state_shutdown(clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001048}
1049
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001050static int arch_timer_dying_cpu(unsigned int cpu)
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001051{
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001052 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001053
Julien Thierryec5c8e42017-10-13 14:32:55 +01001054 cpumask_clear_cpu(smp_processor_id(), &evtstrm_available);
1055
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001056 arch_timer_stop(clk);
1057 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001058}
1059
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001060#ifdef CONFIG_CPU_PM
Marc Zyngierbee67c52017-04-04 17:05:16 +01001061static DEFINE_PER_CPU(unsigned long, saved_cntkctl);
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001062static int arch_timer_cpu_pm_notify(struct notifier_block *self,
1063 unsigned long action, void *hcpu)
1064{
Julien Thierryec5c8e42017-10-13 14:32:55 +01001065 if (action == CPU_PM_ENTER) {
Marc Zyngierbee67c52017-04-04 17:05:16 +01001066 __this_cpu_write(saved_cntkctl, arch_timer_get_cntkctl());
Julien Thierryec5c8e42017-10-13 14:32:55 +01001067
1068 cpumask_clear_cpu(smp_processor_id(), &evtstrm_available);
1069 } else if (action == CPU_PM_ENTER_FAILED || action == CPU_PM_EXIT) {
Marc Zyngierbee67c52017-04-04 17:05:16 +01001070 arch_timer_set_cntkctl(__this_cpu_read(saved_cntkctl));
Julien Thierryec5c8e42017-10-13 14:32:55 +01001071
Andrew Murray5a354412019-06-13 13:51:02 +01001072 if (arch_timer_have_evtstrm_feature())
Julien Thierryec5c8e42017-10-13 14:32:55 +01001073 cpumask_set_cpu(smp_processor_id(), &evtstrm_available);
1074 }
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001075 return NOTIFY_OK;
1076}
1077
1078static struct notifier_block arch_timer_cpu_pm_notifier = {
1079 .notifier_call = arch_timer_cpu_pm_notify,
1080};
1081
1082static int __init arch_timer_cpu_pm_init(void)
1083{
1084 return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier);
1085}
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001086
1087static void __init arch_timer_cpu_pm_deinit(void)
1088{
1089 WARN_ON(cpu_pm_unregister_notifier(&arch_timer_cpu_pm_notifier));
1090}
1091
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001092#else
1093static int __init arch_timer_cpu_pm_init(void)
1094{
1095 return 0;
1096}
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001097
1098static void __init arch_timer_cpu_pm_deinit(void)
1099{
1100}
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001101#endif
1102
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001103static int __init arch_timer_register(void)
1104{
1105 int err;
1106 int ppi;
1107
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001108 arch_timer_evt = alloc_percpu(struct clock_event_device);
1109 if (!arch_timer_evt) {
1110 err = -ENOMEM;
1111 goto out;
1112 }
1113
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001114 ppi = arch_timer_ppi[arch_timer_uses_ppi];
1115 switch (arch_timer_uses_ppi) {
Fu Weiee34f1e2017-01-18 21:25:27 +08001116 case ARCH_TIMER_VIRT_PPI:
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001117 err = request_percpu_irq(ppi, arch_timer_handler_virt,
1118 "arch_timer", arch_timer_evt);
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001119 break;
Fu Weiee34f1e2017-01-18 21:25:27 +08001120 case ARCH_TIMER_PHYS_SECURE_PPI:
1121 case ARCH_TIMER_PHYS_NONSECURE_PPI:
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001122 err = request_percpu_irq(ppi, arch_timer_handler_phys,
1123 "arch_timer", arch_timer_evt);
Fu Wei4502b6b2017-01-18 21:25:30 +08001124 if (!err && arch_timer_has_nonsecure_ppi()) {
Fu Weiee34f1e2017-01-18 21:25:27 +08001125 ppi = arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI];
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001126 err = request_percpu_irq(ppi, arch_timer_handler_phys,
1127 "arch_timer", arch_timer_evt);
1128 if (err)
Fu Weiee34f1e2017-01-18 21:25:27 +08001129 free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_SECURE_PPI],
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001130 arch_timer_evt);
1131 }
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001132 break;
Fu Weiee34f1e2017-01-18 21:25:27 +08001133 case ARCH_TIMER_HYP_PPI:
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001134 err = request_percpu_irq(ppi, arch_timer_handler_phys,
1135 "arch_timer", arch_timer_evt);
1136 break;
1137 default:
1138 BUG();
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001139 }
1140
1141 if (err) {
Fu Weided24012017-01-18 21:25:25 +08001142 pr_err("can't register interrupt %d (%d)\n", ppi, err);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001143 goto out_free;
1144 }
1145
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001146 err = arch_timer_cpu_pm_init();
1147 if (err)
1148 goto out_unreg_notify;
1149
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001150 /* Register and immediately configure the timer on the boot CPU */
1151 err = cpuhp_setup_state(CPUHP_AP_ARM_ARCH_TIMER_STARTING,
Thomas Gleixner73c1b412016-12-21 20:19:54 +01001152 "clockevents/arm/arch_timer:starting",
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001153 arch_timer_starting_cpu, arch_timer_dying_cpu);
1154 if (err)
1155 goto out_unreg_cpupm;
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001156 return 0;
1157
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001158out_unreg_cpupm:
1159 arch_timer_cpu_pm_deinit();
1160
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001161out_unreg_notify:
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001162 free_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], arch_timer_evt);
1163 if (arch_timer_has_nonsecure_ppi())
Fu Weiee34f1e2017-01-18 21:25:27 +08001164 free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001165 arch_timer_evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001166
1167out_free:
1168 free_percpu(arch_timer_evt);
1169out:
1170 return err;
1171}
1172
Stephen Boyd22006992013-07-18 16:59:32 -07001173static int __init arch_timer_mem_register(void __iomem *base, unsigned int irq)
1174{
1175 int ret;
1176 irq_handler_t func;
1177 struct arch_timer *t;
1178
1179 t = kzalloc(sizeof(*t), GFP_KERNEL);
1180 if (!t)
1181 return -ENOMEM;
1182
1183 t->base = base;
1184 t->evt.irq = irq;
Fu Wei8a5c21d2017-01-18 21:25:26 +08001185 __arch_timer_setup(ARCH_TIMER_TYPE_MEM, &t->evt);
Stephen Boyd22006992013-07-18 16:59:32 -07001186
1187 if (arch_timer_mem_use_virtual)
1188 func = arch_timer_handler_virt_mem;
1189 else
1190 func = arch_timer_handler_phys_mem;
1191
1192 ret = request_irq(irq, func, IRQF_TIMER, "arch_mem_timer", &t->evt);
1193 if (ret) {
Fu Weided24012017-01-18 21:25:25 +08001194 pr_err("Failed to request mem timer irq\n");
Stephen Boyd22006992013-07-18 16:59:32 -07001195 kfree(t);
1196 }
1197
1198 return ret;
1199}
1200
1201static const struct of_device_id arch_timer_of_match[] __initconst = {
1202 { .compatible = "arm,armv7-timer", },
1203 { .compatible = "arm,armv8-timer", },
1204 {},
1205};
1206
1207static const struct of_device_id arch_timer_mem_of_match[] __initconst = {
1208 { .compatible = "arm,armv7-timer-mem", },
1209 {},
1210};
1211
Fu Wei13bf6992017-03-22 00:31:14 +08001212static bool __init arch_timer_needs_of_probing(void)
Sudeep Hollac387f072014-09-29 01:50:05 +02001213{
1214 struct device_node *dn;
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001215 bool needs_probing = false;
Fu Wei13bf6992017-03-22 00:31:14 +08001216 unsigned int mask = ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM;
Sudeep Hollac387f072014-09-29 01:50:05 +02001217
Fu Wei13bf6992017-03-22 00:31:14 +08001218 /* We have two timers, and both device-tree nodes are probed. */
1219 if ((arch_timers_present & mask) == mask)
1220 return false;
1221
1222 /*
1223 * Only one type of timer is probed,
1224 * check if we have another type of timer node in device-tree.
1225 */
1226 if (arch_timers_present & ARCH_TIMER_TYPE_CP15)
1227 dn = of_find_matching_node(NULL, arch_timer_mem_of_match);
1228 else
1229 dn = of_find_matching_node(NULL, arch_timer_of_match);
1230
1231 if (dn && of_device_is_available(dn))
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001232 needs_probing = true;
Fu Wei13bf6992017-03-22 00:31:14 +08001233
Sudeep Hollac387f072014-09-29 01:50:05 +02001234 of_node_put(dn);
1235
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001236 return needs_probing;
Sudeep Hollac387f072014-09-29 01:50:05 +02001237}
1238
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001239static int __init arch_timer_common_init(void)
Stephen Boyd22006992013-07-18 16:59:32 -07001240{
Stephen Boyd22006992013-07-18 16:59:32 -07001241 arch_timer_banner(arch_timers_present);
1242 arch_counter_register(arch_timers_present);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001243 return arch_timer_arch_init();
Stephen Boyd22006992013-07-18 16:59:32 -07001244}
1245
Fu Wei4502b6b2017-01-18 21:25:30 +08001246/**
1247 * arch_timer_select_ppi() - Select suitable PPI for the current system.
1248 *
1249 * If HYP mode is available, we know that the physical timer
1250 * has been configured to be accessible from PL1. Use it, so
1251 * that a guest can use the virtual timer instead.
1252 *
1253 * On ARMv8.1 with VH extensions, the kernel runs in HYP. VHE
1254 * accesses to CNTP_*_EL1 registers are silently redirected to
1255 * their CNTHP_*_EL2 counterparts, and use a different PPI
1256 * number.
1257 *
1258 * If no interrupt provided for virtual timer, we'll have to
1259 * stick to the physical timer. It'd better be accessible...
1260 * For arm64 we never use the secure interrupt.
1261 *
1262 * Return: a suitable PPI type for the current system.
1263 */
1264static enum arch_timer_ppi_nr __init arch_timer_select_ppi(void)
1265{
1266 if (is_kernel_in_hyp_mode())
1267 return ARCH_TIMER_HYP_PPI;
1268
1269 if (!is_hyp_mode_available() && arch_timer_ppi[ARCH_TIMER_VIRT_PPI])
1270 return ARCH_TIMER_VIRT_PPI;
1271
1272 if (IS_ENABLED(CONFIG_ARM64))
1273 return ARCH_TIMER_PHYS_NONSECURE_PPI;
1274
1275 return ARCH_TIMER_PHYS_SECURE_PPI;
1276}
1277
Andre Przywaraee793042018-07-06 09:11:50 +01001278static void __init arch_timer_populate_kvm_info(void)
1279{
1280 arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI];
1281 if (is_kernel_in_hyp_mode())
1282 arch_timer_kvm_info.physical_irq = arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI];
1283}
1284
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001285static int __init arch_timer_of_init(struct device_node *np)
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001286{
Fu Weica0e1b52017-03-22 00:31:15 +08001287 int i, ret;
Fu Wei5d3dfa92017-03-22 00:31:13 +08001288 u32 rate;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001289
Fu Wei8a5c21d2017-01-18 21:25:26 +08001290 if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
Fu Weided24012017-01-18 21:25:25 +08001291 pr_warn("multiple nodes in dt, skipping\n");
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001292 return 0;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001293 }
1294
Fu Wei8a5c21d2017-01-18 21:25:26 +08001295 arch_timers_present |= ARCH_TIMER_TYPE_CP15;
Fu Weiee34f1e2017-01-18 21:25:27 +08001296 for (i = ARCH_TIMER_PHYS_SECURE_PPI; i < ARCH_TIMER_MAX_TIMER_PPI; i++)
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001297 arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
1298
Andre Przywaraee793042018-07-06 09:11:50 +01001299 arch_timer_populate_kvm_info();
Fu Weica0e1b52017-03-22 00:31:15 +08001300
Fu Weic389d702017-04-01 01:51:00 +08001301 rate = arch_timer_get_cntfrq();
Fu Wei5d3dfa92017-03-22 00:31:13 +08001302 arch_timer_of_configure_rate(rate, np);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001303
1304 arch_timer_c3stop = !of_property_read_bool(np, "always-on");
1305
Marc Zyngier651bb2e2017-01-19 17:20:59 +00001306 /* Check for globally applicable workarounds */
1307 arch_timer_check_ool_workaround(ate_match_dt, np);
Scott Woodf6dc1572016-09-22 03:35:17 -05001308
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001309 /*
1310 * If we cannot rely on firmware initializing the timer registers then
1311 * we should use the physical timers instead.
1312 */
1313 if (IS_ENABLED(CONFIG_ARM) &&
1314 of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
Fu Weiee34f1e2017-01-18 21:25:27 +08001315 arch_timer_uses_ppi = ARCH_TIMER_PHYS_SECURE_PPI;
Fu Wei4502b6b2017-01-18 21:25:30 +08001316 else
1317 arch_timer_uses_ppi = arch_timer_select_ppi();
1318
1319 if (!arch_timer_ppi[arch_timer_uses_ppi]) {
1320 pr_err("No interrupt available, giving up\n");
1321 return -EINVAL;
1322 }
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001323
Brian Norrisd8ec7592016-10-04 11:12:09 -07001324 /* On some systems, the counter stops ticking when in suspend. */
1325 arch_counter_suspend_stop = of_property_read_bool(np,
1326 "arm,no-tick-in-suspend");
1327
Fu Weica0e1b52017-03-22 00:31:15 +08001328 ret = arch_timer_register();
1329 if (ret)
1330 return ret;
1331
1332 if (arch_timer_needs_of_probing())
1333 return 0;
1334
1335 return arch_timer_common_init();
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001336}
Daniel Lezcano17273392017-05-26 16:56:11 +02001337TIMER_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_of_init);
1338TIMER_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_of_init);
Stephen Boyd22006992013-07-18 16:59:32 -07001339
Fu Weic389d702017-04-01 01:51:00 +08001340static u32 __init
1341arch_timer_mem_frame_get_cntfrq(struct arch_timer_mem_frame *frame)
Stephen Boyd22006992013-07-18 16:59:32 -07001342{
Fu Weic389d702017-04-01 01:51:00 +08001343 void __iomem *base;
1344 u32 rate;
Stephen Boyd22006992013-07-18 16:59:32 -07001345
Fu Weic389d702017-04-01 01:51:00 +08001346 base = ioremap(frame->cntbase, frame->size);
1347 if (!base) {
1348 pr_err("Unable to map frame @ %pa\n", &frame->cntbase);
1349 return 0;
1350 }
1351
Frank Rowand3db12002017-06-09 17:26:32 -07001352 rate = readl_relaxed(base + CNTFRQ);
Fu Weic389d702017-04-01 01:51:00 +08001353
Frank Rowand3db12002017-06-09 17:26:32 -07001354 iounmap(base);
Fu Weic389d702017-04-01 01:51:00 +08001355
1356 return rate;
1357}
1358
1359static struct arch_timer_mem_frame * __init
1360arch_timer_mem_find_best_frame(struct arch_timer_mem *timer_mem)
1361{
1362 struct arch_timer_mem_frame *frame, *best_frame = NULL;
1363 void __iomem *cntctlbase;
1364 u32 cnttidr;
1365 int i;
1366
1367 cntctlbase = ioremap(timer_mem->cntctlbase, timer_mem->size);
Stephen Boyd22006992013-07-18 16:59:32 -07001368 if (!cntctlbase) {
Fu Weic389d702017-04-01 01:51:00 +08001369 pr_err("Can't map CNTCTLBase @ %pa\n",
1370 &timer_mem->cntctlbase);
1371 return NULL;
Stephen Boyd22006992013-07-18 16:59:32 -07001372 }
1373
1374 cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
Stephen Boyd22006992013-07-18 16:59:32 -07001375
1376 /*
1377 * Try to find a virtual capable frame. Otherwise fall back to a
1378 * physical capable frame.
1379 */
Fu Weic389d702017-04-01 01:51:00 +08001380 for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
1381 u32 cntacr = CNTACR_RFRQ | CNTACR_RWPT | CNTACR_RPCT |
1382 CNTACR_RWVT | CNTACR_RVOFF | CNTACR_RVCT;
Stephen Boyd22006992013-07-18 16:59:32 -07001383
Fu Weic389d702017-04-01 01:51:00 +08001384 frame = &timer_mem->frame[i];
1385 if (!frame->valid)
1386 continue;
Stephen Boyd22006992013-07-18 16:59:32 -07001387
Robin Murphye392d602016-02-01 12:00:48 +00001388 /* Try enabling everything, and see what sticks */
Fu Weic389d702017-04-01 01:51:00 +08001389 writel_relaxed(cntacr, cntctlbase + CNTACR(i));
1390 cntacr = readl_relaxed(cntctlbase + CNTACR(i));
Robin Murphye392d602016-02-01 12:00:48 +00001391
Fu Weic389d702017-04-01 01:51:00 +08001392 if ((cnttidr & CNTTIDR_VIRT(i)) &&
Robin Murphye392d602016-02-01 12:00:48 +00001393 !(~cntacr & (CNTACR_RWVT | CNTACR_RVCT))) {
Stephen Boyd22006992013-07-18 16:59:32 -07001394 best_frame = frame;
1395 arch_timer_mem_use_virtual = true;
1396 break;
1397 }
Robin Murphye392d602016-02-01 12:00:48 +00001398
1399 if (~cntacr & (CNTACR_RWPT | CNTACR_RPCT))
1400 continue;
1401
Fu Weic389d702017-04-01 01:51:00 +08001402 best_frame = frame;
Stephen Boyd22006992013-07-18 16:59:32 -07001403 }
1404
Fu Weic389d702017-04-01 01:51:00 +08001405 iounmap(cntctlbase);
1406
Sudeep Hollaf63d9472017-05-08 13:32:27 +01001407 return best_frame;
Fu Weic389d702017-04-01 01:51:00 +08001408}
1409
1410static int __init
1411arch_timer_mem_frame_register(struct arch_timer_mem_frame *frame)
1412{
1413 void __iomem *base;
1414 int ret, irq = 0;
Stephen Boyd22006992013-07-18 16:59:32 -07001415
1416 if (arch_timer_mem_use_virtual)
Fu Weic389d702017-04-01 01:51:00 +08001417 irq = frame->virt_irq;
Stephen Boyd22006992013-07-18 16:59:32 -07001418 else
Fu Weic389d702017-04-01 01:51:00 +08001419 irq = frame->phys_irq;
Robin Murphye392d602016-02-01 12:00:48 +00001420
Stephen Boyd22006992013-07-18 16:59:32 -07001421 if (!irq) {
Fu Weided24012017-01-18 21:25:25 +08001422 pr_err("Frame missing %s irq.\n",
Thomas Gleixnercfb6d652013-08-21 14:59:23 +02001423 arch_timer_mem_use_virtual ? "virt" : "phys");
Fu Weic389d702017-04-01 01:51:00 +08001424 return -EINVAL;
1425 }
1426
1427 if (!request_mem_region(frame->cntbase, frame->size,
1428 "arch_mem_timer"))
1429 return -EBUSY;
1430
1431 base = ioremap(frame->cntbase, frame->size);
1432 if (!base) {
1433 pr_err("Can't map frame's registers\n");
1434 return -ENXIO;
1435 }
1436
1437 ret = arch_timer_mem_register(base, irq);
1438 if (ret) {
1439 iounmap(base);
1440 return ret;
1441 }
1442
1443 arch_counter_base = base;
1444 arch_timers_present |= ARCH_TIMER_TYPE_MEM;
1445
1446 return 0;
1447}
1448
1449static int __init arch_timer_mem_of_init(struct device_node *np)
1450{
1451 struct arch_timer_mem *timer_mem;
1452 struct arch_timer_mem_frame *frame;
1453 struct device_node *frame_node;
1454 struct resource res;
1455 int ret = -EINVAL;
1456 u32 rate;
1457
1458 timer_mem = kzalloc(sizeof(*timer_mem), GFP_KERNEL);
1459 if (!timer_mem)
1460 return -ENOMEM;
1461
1462 if (of_address_to_resource(np, 0, &res))
1463 goto out;
1464 timer_mem->cntctlbase = res.start;
1465 timer_mem->size = resource_size(&res);
1466
1467 for_each_available_child_of_node(np, frame_node) {
1468 u32 n;
1469 struct arch_timer_mem_frame *frame;
1470
1471 if (of_property_read_u32(frame_node, "frame-number", &n)) {
1472 pr_err(FW_BUG "Missing frame-number.\n");
1473 of_node_put(frame_node);
1474 goto out;
1475 }
1476 if (n >= ARCH_TIMER_MEM_MAX_FRAMES) {
1477 pr_err(FW_BUG "Wrong frame-number, only 0-%u are permitted.\n",
1478 ARCH_TIMER_MEM_MAX_FRAMES - 1);
1479 of_node_put(frame_node);
1480 goto out;
1481 }
1482 frame = &timer_mem->frame[n];
1483
1484 if (frame->valid) {
1485 pr_err(FW_BUG "Duplicated frame-number.\n");
1486 of_node_put(frame_node);
1487 goto out;
1488 }
1489
1490 if (of_address_to_resource(frame_node, 0, &res)) {
1491 of_node_put(frame_node);
1492 goto out;
1493 }
1494 frame->cntbase = res.start;
1495 frame->size = resource_size(&res);
1496
1497 frame->virt_irq = irq_of_parse_and_map(frame_node,
1498 ARCH_TIMER_VIRT_SPI);
1499 frame->phys_irq = irq_of_parse_and_map(frame_node,
1500 ARCH_TIMER_PHYS_SPI);
1501
1502 frame->valid = true;
1503 }
1504
1505 frame = arch_timer_mem_find_best_frame(timer_mem);
1506 if (!frame) {
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001507 pr_err("Unable to find a suitable frame in timer @ %pa\n",
1508 &timer_mem->cntctlbase);
Fu Weic389d702017-04-01 01:51:00 +08001509 ret = -EINVAL;
Robin Murphye392d602016-02-01 12:00:48 +00001510 goto out;
Stephen Boyd22006992013-07-18 16:59:32 -07001511 }
1512
Fu Weic389d702017-04-01 01:51:00 +08001513 rate = arch_timer_mem_frame_get_cntfrq(frame);
Fu Wei5d3dfa92017-03-22 00:31:13 +08001514 arch_timer_of_configure_rate(rate, np);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001515
Fu Weic389d702017-04-01 01:51:00 +08001516 ret = arch_timer_mem_frame_register(frame);
1517 if (!ret && !arch_timer_needs_of_probing())
Fu Weica0e1b52017-03-22 00:31:15 +08001518 ret = arch_timer_common_init();
Robin Murphye392d602016-02-01 12:00:48 +00001519out:
Fu Weic389d702017-04-01 01:51:00 +08001520 kfree(timer_mem);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001521 return ret;
Stephen Boyd22006992013-07-18 16:59:32 -07001522}
Daniel Lezcano17273392017-05-26 16:56:11 +02001523TIMER_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem",
Fu Weic389d702017-04-01 01:51:00 +08001524 arch_timer_mem_of_init);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001525
Fu Weif79d2092017-04-01 01:51:02 +08001526#ifdef CONFIG_ACPI_GTDT
Fu Weic2743a32017-04-01 01:51:04 +08001527static int __init
1528arch_timer_mem_verify_cntfrq(struct arch_timer_mem *timer_mem)
1529{
1530 struct arch_timer_mem_frame *frame;
1531 u32 rate;
1532 int i;
1533
1534 for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
1535 frame = &timer_mem->frame[i];
1536
1537 if (!frame->valid)
1538 continue;
1539
1540 rate = arch_timer_mem_frame_get_cntfrq(frame);
1541 if (rate == arch_timer_rate)
1542 continue;
1543
1544 pr_err(FW_BUG "CNTFRQ mismatch: frame @ %pa: (0x%08lx), CPU: (0x%08lx)\n",
1545 &frame->cntbase,
1546 (unsigned long)rate, (unsigned long)arch_timer_rate);
1547
1548 return -EINVAL;
1549 }
1550
1551 return 0;
1552}
1553
1554static int __init arch_timer_mem_acpi_init(int platform_timer_count)
1555{
1556 struct arch_timer_mem *timers, *timer;
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001557 struct arch_timer_mem_frame *frame, *best_frame = NULL;
Fu Weic2743a32017-04-01 01:51:04 +08001558 int timer_count, i, ret = 0;
1559
1560 timers = kcalloc(platform_timer_count, sizeof(*timers),
1561 GFP_KERNEL);
1562 if (!timers)
1563 return -ENOMEM;
1564
1565 ret = acpi_arch_timer_mem_init(timers, &timer_count);
1566 if (ret || !timer_count)
1567 goto out;
1568
Fu Weic2743a32017-04-01 01:51:04 +08001569 /*
1570 * While unlikely, it's theoretically possible that none of the frames
1571 * in a timer expose the combination of feature we want.
1572 */
Matthias Kaehlcked197f792017-07-31 11:37:28 -07001573 for (i = 0; i < timer_count; i++) {
Fu Weic2743a32017-04-01 01:51:04 +08001574 timer = &timers[i];
1575
1576 frame = arch_timer_mem_find_best_frame(timer);
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001577 if (!best_frame)
1578 best_frame = frame;
1579
1580 ret = arch_timer_mem_verify_cntfrq(timer);
1581 if (ret) {
1582 pr_err("Disabling MMIO timers due to CNTFRQ mismatch\n");
1583 goto out;
1584 }
1585
1586 if (!best_frame) /* implies !frame */
1587 /*
1588 * Only complain about missing suitable frames if we
1589 * haven't already found one in a previous iteration.
1590 */
1591 pr_err("Unable to find a suitable frame in timer @ %pa\n",
1592 &timer->cntctlbase);
Fu Weic2743a32017-04-01 01:51:04 +08001593 }
1594
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001595 if (best_frame)
1596 ret = arch_timer_mem_frame_register(best_frame);
Fu Weic2743a32017-04-01 01:51:04 +08001597out:
1598 kfree(timers);
1599 return ret;
1600}
1601
1602/* Initialize per-processor generic timer and memory-mapped timer(if present) */
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001603static int __init arch_timer_acpi_init(struct acpi_table_header *table)
1604{
Fu Weic2743a32017-04-01 01:51:04 +08001605 int ret, platform_timer_count;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001606
Fu Wei8a5c21d2017-01-18 21:25:26 +08001607 if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
Fu Weided24012017-01-18 21:25:25 +08001608 pr_warn("already initialized, skipping\n");
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001609 return -EINVAL;
1610 }
1611
Fu Wei8a5c21d2017-01-18 21:25:26 +08001612 arch_timers_present |= ARCH_TIMER_TYPE_CP15;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001613
Fu Weic2743a32017-04-01 01:51:04 +08001614 ret = acpi_gtdt_init(table, &platform_timer_count);
Dejin Zhengd1b5e552020-04-29 23:35:59 +08001615 if (ret)
Fu Weif79d2092017-04-01 01:51:02 +08001616 return ret;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001617
Fu Weiee34f1e2017-01-18 21:25:27 +08001618 arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI] =
Fu Weif79d2092017-04-01 01:51:02 +08001619 acpi_gtdt_map_ppi(ARCH_TIMER_PHYS_NONSECURE_PPI);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001620
Fu Weiee34f1e2017-01-18 21:25:27 +08001621 arch_timer_ppi[ARCH_TIMER_VIRT_PPI] =
Fu Weif79d2092017-04-01 01:51:02 +08001622 acpi_gtdt_map_ppi(ARCH_TIMER_VIRT_PPI);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001623
Fu Weiee34f1e2017-01-18 21:25:27 +08001624 arch_timer_ppi[ARCH_TIMER_HYP_PPI] =
Fu Weif79d2092017-04-01 01:51:02 +08001625 acpi_gtdt_map_ppi(ARCH_TIMER_HYP_PPI);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001626
Andre Przywaraee793042018-07-06 09:11:50 +01001627 arch_timer_populate_kvm_info();
Fu Weica0e1b52017-03-22 00:31:15 +08001628
Fu Wei5d3dfa92017-03-22 00:31:13 +08001629 /*
1630 * When probing via ACPI, we have no mechanism to override the sysreg
1631 * CNTFRQ value. This *must* be correct.
1632 */
1633 arch_timer_rate = arch_timer_get_cntfrq();
Ionela Voinescuc2658612020-03-05 09:06:27 +00001634 ret = validate_timer_rate();
1635 if (ret) {
Fu Wei5d3dfa92017-03-22 00:31:13 +08001636 pr_err(FW_BUG "frequency not available.\n");
Ionela Voinescuc2658612020-03-05 09:06:27 +00001637 return ret;
Fu Wei5d3dfa92017-03-22 00:31:13 +08001638 }
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001639
Fu Wei4502b6b2017-01-18 21:25:30 +08001640 arch_timer_uses_ppi = arch_timer_select_ppi();
1641 if (!arch_timer_ppi[arch_timer_uses_ppi]) {
1642 pr_err("No interrupt available, giving up\n");
1643 return -EINVAL;
1644 }
1645
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001646 /* Always-on capability */
Fu Weif79d2092017-04-01 01:51:02 +08001647 arch_timer_c3stop = acpi_gtdt_c3stop(arch_timer_uses_ppi);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001648
Marc Zyngier5a38bca2017-02-21 14:37:30 +00001649 /* Check for globally applicable workarounds */
1650 arch_timer_check_ool_workaround(ate_match_acpi_oem_info, table);
1651
Fu Weica0e1b52017-03-22 00:31:15 +08001652 ret = arch_timer_register();
1653 if (ret)
1654 return ret;
1655
Fu Weic2743a32017-04-01 01:51:04 +08001656 if (platform_timer_count &&
1657 arch_timer_mem_acpi_init(platform_timer_count))
1658 pr_err("Failed to initialize memory-mapped timer.\n");
1659
Fu Weica0e1b52017-03-22 00:31:15 +08001660 return arch_timer_common_init();
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001661}
Daniel Lezcano77d62f52017-05-26 17:42:25 +02001662TIMER_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001663#endif
Jianyong Wu300bb1f2020-12-09 14:09:30 +08001664
1665int kvm_arch_ptp_get_crosststamp(u64 *cycle, struct timespec64 *ts,
1666 struct clocksource **cs)
1667{
1668 struct arm_smccc_res hvc_res;
1669 u32 ptp_counter;
1670 ktime_t ktime;
1671
1672 if (!IS_ENABLED(CONFIG_HAVE_ARM_SMCCC_DISCOVERY))
1673 return -EOPNOTSUPP;
1674
1675 if (arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI)
1676 ptp_counter = KVM_PTP_VIRT_COUNTER;
1677 else
1678 ptp_counter = KVM_PTP_PHYS_COUNTER;
1679
1680 arm_smccc_1_1_invoke(ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID,
1681 ptp_counter, &hvc_res);
1682
1683 if ((int)(hvc_res.a0) < 0)
1684 return -EOPNOTSUPP;
1685
1686 ktime = (u64)hvc_res.a0 << 32 | hvc_res.a1;
1687 *ts = ktime_to_timespec64(ktime);
1688 if (cycle)
1689 *cycle = (u64)hvc_res.a2 << 32 | hvc_res.a3;
1690 if (cs)
1691 *cs = &clocksource_counter;
1692
1693 return 0;
1694}
1695EXPORT_SYMBOL_GPL(kvm_arch_ptp_get_crosststamp);