blob: 4551587bcb44bfd8778be7711d999c3f194115a8 [file] [log] [blame]
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001/*
2 * linux/drivers/clocksource/arm_arch_timer.c
3 *
4 * Copyright (C) 2011 ARM Ltd.
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
Marc Zyngierf005bd72016-08-01 10:54:15 +010011
12#define pr_fmt(fmt) "arm_arch_timer: " fmt
13
Mark Rutland8a4da6e2012-11-12 14:33:44 +000014#include <linux/init.h>
15#include <linux/kernel.h>
16#include <linux/device.h>
17#include <linux/smp.h>
18#include <linux/cpu.h>
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +010019#include <linux/cpu_pm.h>
Mark Rutland8a4da6e2012-11-12 14:33:44 +000020#include <linux/clockchips.h>
Richard Cochran7c8f1e72015-01-06 14:26:13 +010021#include <linux/clocksource.h>
Mark Rutland8a4da6e2012-11-12 14:33:44 +000022#include <linux/interrupt.h>
23#include <linux/of_irq.h>
Stephen Boyd22006992013-07-18 16:59:32 -070024#include <linux/of_address.h>
Mark Rutland8a4da6e2012-11-12 14:33:44 +000025#include <linux/io.h>
Stephen Boyd22006992013-07-18 16:59:32 -070026#include <linux/slab.h>
Ingo Molnare6017572017-02-01 16:36:40 +010027#include <linux/sched/clock.h>
Stephen Boyd65cd4f62013-07-18 16:21:18 -070028#include <linux/sched_clock.h>
Hanjun Guob09ca1e2015-03-24 14:02:50 +000029#include <linux/acpi.h>
Mark Rutland8a4da6e2012-11-12 14:33:44 +000030
31#include <asm/arch_timer.h>
Marc Zyngier82668912013-01-10 11:13:07 +000032#include <asm/virt.h>
Mark Rutland8a4da6e2012-11-12 14:33:44 +000033
34#include <clocksource/arm_arch_timer.h>
35
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
55#define ARCH_CP15_TIMER BIT(0)
56#define ARCH_MEM_TIMER BIT(1)
57static unsigned arch_timers_present __initdata;
58
59static void __iomem *arch_counter_base;
60
61struct arch_timer {
62 void __iomem *base;
63 struct clock_event_device evt;
64};
65
66#define to_arch_timer(e) container_of(e, struct arch_timer, evt)
67
Mark Rutland8a4da6e2012-11-12 14:33:44 +000068static u32 arch_timer_rate;
69
70enum ppi_nr {
71 PHYS_SECURE_PPI,
72 PHYS_NONSECURE_PPI,
73 VIRT_PPI,
74 HYP_PPI,
75 MAX_TIMER_PPI
76};
77
78static int arch_timer_ppi[MAX_TIMER_PPI];
79
80static struct clock_event_device __percpu *arch_timer_evt;
81
Marc Zyngierf81f03f2014-02-20 15:21:23 +000082static enum ppi_nr arch_timer_uses_ppi = VIRT_PPI;
Lorenzo Pieralisi82a561942014-04-08 10:04:32 +010083static bool arch_timer_c3stop;
Stephen Boyd22006992013-07-18 16:59:32 -070084static bool arch_timer_mem_use_virtual;
Brian Norrisd8ec7592016-10-04 11:12:09 -070085static bool arch_counter_suspend_stop;
Mark Rutland8a4da6e2012-11-12 14:33:44 +000086
Will Deacon46fd5c62016-06-27 17:30:13 +010087static bool evtstrm_enable = IS_ENABLED(CONFIG_ARM_ARCH_TIMER_EVTSTREAM);
88
89static int __init early_evtstrm_cfg(char *buf)
90{
91 return strtobool(buf, &evtstrm_enable);
92}
93early_param("clocksource.arm_arch_timer.evtstrm", early_evtstrm_cfg);
94
Mark Rutland8a4da6e2012-11-12 14:33:44 +000095/*
96 * Architected system timer support.
97 */
98
Marc Zyngierf4e00a12017-01-20 18:28:32 +000099static __always_inline
100void arch_timer_reg_write(int access, enum arch_timer_reg reg, u32 val,
101 struct clock_event_device *clk)
102{
103 if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
104 struct arch_timer *timer = to_arch_timer(clk);
105 switch (reg) {
106 case ARCH_TIMER_REG_CTRL:
107 writel_relaxed(val, timer->base + CNTP_CTL);
108 break;
109 case ARCH_TIMER_REG_TVAL:
110 writel_relaxed(val, timer->base + CNTP_TVAL);
111 break;
112 }
113 } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
114 struct arch_timer *timer = to_arch_timer(clk);
115 switch (reg) {
116 case ARCH_TIMER_REG_CTRL:
117 writel_relaxed(val, timer->base + CNTV_CTL);
118 break;
119 case ARCH_TIMER_REG_TVAL:
120 writel_relaxed(val, timer->base + CNTV_TVAL);
121 break;
122 }
123 } else {
124 arch_timer_reg_write_cp15(access, reg, val);
125 }
126}
127
128static __always_inline
129u32 arch_timer_reg_read(int access, enum arch_timer_reg reg,
130 struct clock_event_device *clk)
131{
132 u32 val;
133
134 if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
135 struct arch_timer *timer = to_arch_timer(clk);
136 switch (reg) {
137 case ARCH_TIMER_REG_CTRL:
138 val = readl_relaxed(timer->base + CNTP_CTL);
139 break;
140 case ARCH_TIMER_REG_TVAL:
141 val = readl_relaxed(timer->base + CNTP_TVAL);
142 break;
143 }
144 } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
145 struct arch_timer *timer = to_arch_timer(clk);
146 switch (reg) {
147 case ARCH_TIMER_REG_CTRL:
148 val = readl_relaxed(timer->base + CNTV_CTL);
149 break;
150 case ARCH_TIMER_REG_TVAL:
151 val = readl_relaxed(timer->base + CNTV_TVAL);
152 break;
153 }
154 } else {
155 val = arch_timer_reg_read_cp15(access, reg);
156 }
157
158 return val;
159}
160
Scott Woodf6dc1572016-09-22 03:35:17 -0500161#ifdef CONFIG_FSL_ERRATUM_A008585
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000162/*
163 * The number of retries is an arbitrary value well beyond the highest number
164 * of iterations the loop has been observed to take.
165 */
166#define __fsl_a008585_read_reg(reg) ({ \
167 u64 _old, _new; \
168 int _retries = 200; \
169 \
170 do { \
171 _old = read_sysreg(reg); \
172 _new = read_sysreg(reg); \
173 _retries--; \
174 } while (unlikely(_old != _new) && _retries); \
175 \
176 WARN_ON_ONCE(!_retries); \
177 _new; \
178})
Scott Woodf6dc1572016-09-22 03:35:17 -0500179
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000180static u32 notrace fsl_a008585_read_cntp_tval_el0(void)
Scott Woodf6dc1572016-09-22 03:35:17 -0500181{
182 return __fsl_a008585_read_reg(cntp_tval_el0);
183}
184
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000185static u32 notrace fsl_a008585_read_cntv_tval_el0(void)
Scott Woodf6dc1572016-09-22 03:35:17 -0500186{
187 return __fsl_a008585_read_reg(cntv_tval_el0);
188}
189
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000190static u64 notrace fsl_a008585_read_cntvct_el0(void)
Scott Woodf6dc1572016-09-22 03:35:17 -0500191{
192 return __fsl_a008585_read_reg(cntvct_el0);
193}
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000194#endif
195
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000196#ifdef CONFIG_HISILICON_ERRATUM_161010101
197/*
198 * Verify whether the value of the second read is larger than the first by
199 * less than 32 is the only way to confirm the value is correct, so clear the
200 * lower 5 bits to check whether the difference is greater than 32 or not.
201 * Theoretically the erratum should not occur more than twice in succession
202 * when reading the system counter, but it is possible that some interrupts
203 * may lead to more than twice read errors, triggering the warning, so setting
204 * the number of retries far beyond the number of iterations the loop has been
205 * observed to take.
206 */
207#define __hisi_161010101_read_reg(reg) ({ \
208 u64 _old, _new; \
209 int _retries = 50; \
210 \
211 do { \
212 _old = read_sysreg(reg); \
213 _new = read_sysreg(reg); \
214 _retries--; \
215 } while (unlikely((_new - _old) >> 5) && _retries); \
216 \
217 WARN_ON_ONCE(!_retries); \
218 _new; \
219})
220
221static u32 notrace hisi_161010101_read_cntp_tval_el0(void)
222{
223 return __hisi_161010101_read_reg(cntp_tval_el0);
224}
225
226static u32 notrace hisi_161010101_read_cntv_tval_el0(void)
227{
228 return __hisi_161010101_read_reg(cntv_tval_el0);
229}
230
231static u64 notrace hisi_161010101_read_cntvct_el0(void)
232{
233 return __hisi_161010101_read_reg(cntvct_el0);
234}
235#endif
236
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000237#ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000238DEFINE_PER_CPU(const struct arch_timer_erratum_workaround *,
239 timer_unstable_counter_workaround);
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000240EXPORT_SYMBOL_GPL(timer_unstable_counter_workaround);
241
242DEFINE_STATIC_KEY_FALSE(arch_timer_read_ool_enabled);
243EXPORT_SYMBOL_GPL(arch_timer_read_ool_enabled);
244
Marc Zyngier83280892017-01-27 10:27:09 +0000245static void erratum_set_next_event_tval_generic(const int access, unsigned long evt,
246 struct clock_event_device *clk)
247{
248 unsigned long ctrl;
249 u64 cval = evt + arch_counter_get_cntvct();
250
251 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
252 ctrl |= ARCH_TIMER_CTRL_ENABLE;
253 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
254
255 if (access == ARCH_TIMER_PHYS_ACCESS)
256 write_sysreg(cval, cntp_cval_el0);
257 else
258 write_sysreg(cval, cntv_cval_el0);
259
260 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
261}
262
263static int erratum_set_next_event_tval_virt(unsigned long evt,
264 struct clock_event_device *clk)
265{
266 erratum_set_next_event_tval_generic(ARCH_TIMER_VIRT_ACCESS, evt, clk);
267 return 0;
268}
269
270static int erratum_set_next_event_tval_phys(unsigned long evt,
271 struct clock_event_device *clk)
272{
273 erratum_set_next_event_tval_generic(ARCH_TIMER_PHYS_ACCESS, evt, clk);
274 return 0;
275}
276
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000277static const struct arch_timer_erratum_workaround ool_workarounds[] = {
278#ifdef CONFIG_FSL_ERRATUM_A008585
279 {
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000280 .match_type = ate_match_dt,
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000281 .id = "fsl,erratum-a008585",
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000282 .desc = "Freescale erratum a005858",
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000283 .read_cntp_tval_el0 = fsl_a008585_read_cntp_tval_el0,
284 .read_cntv_tval_el0 = fsl_a008585_read_cntv_tval_el0,
285 .read_cntvct_el0 = fsl_a008585_read_cntvct_el0,
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000286 .set_next_event_phys = erratum_set_next_event_tval_phys,
287 .set_next_event_virt = erratum_set_next_event_tval_virt,
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000288 },
289#endif
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000290#ifdef CONFIG_HISILICON_ERRATUM_161010101
291 {
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000292 .match_type = ate_match_dt,
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000293 .id = "hisilicon,erratum-161010101",
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000294 .desc = "HiSilicon erratum 161010101",
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000295 .read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
296 .read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
297 .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000298 .set_next_event_phys = erratum_set_next_event_tval_phys,
299 .set_next_event_virt = erratum_set_next_event_tval_virt,
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000300 },
301#endif
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000302};
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000303
304typedef bool (*ate_match_fn_t)(const struct arch_timer_erratum_workaround *,
305 const void *);
306
307static
308bool arch_timer_check_dt_erratum(const struct arch_timer_erratum_workaround *wa,
309 const void *arg)
310{
311 const struct device_node *np = arg;
312
313 return of_property_read_bool(np, wa->id);
314}
315
Marc Zyngier00640302017-03-20 16:47:59 +0000316static
317bool arch_timer_check_local_cap_erratum(const struct arch_timer_erratum_workaround *wa,
318 const void *arg)
319{
320 return this_cpu_has_cap((uintptr_t)wa->id);
321}
322
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000323static const struct arch_timer_erratum_workaround *
324arch_timer_iterate_errata(enum arch_timer_erratum_match_type type,
325 ate_match_fn_t match_fn,
326 void *arg)
327{
328 int i;
329
330 for (i = 0; i < ARRAY_SIZE(ool_workarounds); i++) {
331 if (ool_workarounds[i].match_type != type)
332 continue;
333
334 if (match_fn(&ool_workarounds[i], arg))
335 return &ool_workarounds[i];
336 }
337
338 return NULL;
339}
340
341static
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000342void arch_timer_enable_workaround(const struct arch_timer_erratum_workaround *wa,
343 bool local)
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000344{
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000345 int i;
346
347 if (local) {
348 __this_cpu_write(timer_unstable_counter_workaround, wa);
349 } else {
350 for_each_possible_cpu(i)
351 per_cpu(timer_unstable_counter_workaround, i) = wa;
352 }
353
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000354 static_branch_enable(&arch_timer_read_ool_enabled);
355}
356
357static void arch_timer_check_ool_workaround(enum arch_timer_erratum_match_type type,
358 void *arg)
359{
360 const struct arch_timer_erratum_workaround *wa;
361 ate_match_fn_t match_fn = NULL;
Marc Zyngier00640302017-03-20 16:47:59 +0000362 bool local = false;
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000363
364 switch (type) {
365 case ate_match_dt:
366 match_fn = arch_timer_check_dt_erratum;
367 break;
Marc Zyngier00640302017-03-20 16:47:59 +0000368 case ate_match_local_cap_id:
369 match_fn = arch_timer_check_local_cap_erratum;
370 local = true;
371 break;
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000372 default:
373 WARN_ON(1);
374 return;
375 }
376
377 wa = arch_timer_iterate_errata(type, match_fn, arg);
378 if (!wa)
379 return;
380
Marc Zyngier00640302017-03-20 16:47:59 +0000381 if (needs_unstable_timer_counter_workaround()) {
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000382 const struct arch_timer_erratum_workaround *__wa;
383 __wa = __this_cpu_read(timer_unstable_counter_workaround);
384 if (__wa && wa != __wa)
Marc Zyngier00640302017-03-20 16:47:59 +0000385 pr_warn("Can't enable workaround for %s (clashes with %s\n)",
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000386 wa->desc, __wa->desc);
387
388 if (__wa)
389 return;
Marc Zyngier00640302017-03-20 16:47:59 +0000390 }
391
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000392 arch_timer_enable_workaround(wa, local);
Marc Zyngier00640302017-03-20 16:47:59 +0000393 pr_info("Enabling %s workaround for %s\n",
394 local ? "local" : "global", wa->desc);
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000395}
396
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000397#define erratum_handler(fn, r, ...) \
398({ \
399 bool __val; \
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000400 if (needs_unstable_timer_counter_workaround()) { \
401 const struct arch_timer_erratum_workaround *__wa; \
402 __wa = __this_cpu_read(timer_unstable_counter_workaround); \
403 if (__wa && __wa->fn) { \
404 r = __wa->fn(__VA_ARGS__); \
405 __val = true; \
406 } else { \
407 __val = false; \
408 } \
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000409 } else { \
410 __val = false; \
411 } \
412 __val; \
413})
414
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000415#else
416#define arch_timer_check_ool_workaround(t,a) do { } while(0)
Marc Zyngier83280892017-01-27 10:27:09 +0000417#define erratum_set_next_event_tval_virt(...) ({BUG(); 0;})
418#define erratum_set_next_event_tval_phys(...) ({BUG(); 0;})
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000419#define erratum_handler(fn, r, ...) ({false;})
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000420#endif /* CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND */
Scott Woodf6dc1572016-09-22 03:35:17 -0500421
Stephen Boyde09f3cc2013-07-18 16:59:28 -0700422static __always_inline irqreturn_t timer_handler(const int access,
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000423 struct clock_event_device *evt)
424{
425 unsigned long ctrl;
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200426
Stephen Boyd60faddf2013-07-18 16:59:31 -0700427 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000428 if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
429 ctrl |= ARCH_TIMER_CTRL_IT_MASK;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700430 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000431 evt->event_handler(evt);
432 return IRQ_HANDLED;
433 }
434
435 return IRQ_NONE;
436}
437
438static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
439{
440 struct clock_event_device *evt = dev_id;
441
442 return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
443}
444
445static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
446{
447 struct clock_event_device *evt = dev_id;
448
449 return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
450}
451
Stephen Boyd22006992013-07-18 16:59:32 -0700452static irqreturn_t arch_timer_handler_phys_mem(int irq, void *dev_id)
453{
454 struct clock_event_device *evt = dev_id;
455
456 return timer_handler(ARCH_TIMER_MEM_PHYS_ACCESS, evt);
457}
458
459static irqreturn_t arch_timer_handler_virt_mem(int irq, void *dev_id)
460{
461 struct clock_event_device *evt = dev_id;
462
463 return timer_handler(ARCH_TIMER_MEM_VIRT_ACCESS, evt);
464}
465
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530466static __always_inline int timer_shutdown(const int access,
467 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000468{
469 unsigned long ctrl;
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530470
471 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
472 ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
473 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
474
475 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000476}
477
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530478static int arch_timer_shutdown_virt(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000479{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530480 return timer_shutdown(ARCH_TIMER_VIRT_ACCESS, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000481}
482
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530483static int arch_timer_shutdown_phys(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000484{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530485 return timer_shutdown(ARCH_TIMER_PHYS_ACCESS, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000486}
487
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530488static int arch_timer_shutdown_virt_mem(struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700489{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530490 return timer_shutdown(ARCH_TIMER_MEM_VIRT_ACCESS, clk);
Stephen Boyd22006992013-07-18 16:59:32 -0700491}
492
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530493static int arch_timer_shutdown_phys_mem(struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700494{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530495 return timer_shutdown(ARCH_TIMER_MEM_PHYS_ACCESS, clk);
Stephen Boyd22006992013-07-18 16:59:32 -0700496}
497
Stephen Boyd60faddf2013-07-18 16:59:31 -0700498static __always_inline void set_next_event(const int access, unsigned long evt,
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200499 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000500{
501 unsigned long ctrl;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700502 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000503 ctrl |= ARCH_TIMER_CTRL_ENABLE;
504 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700505 arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt, clk);
506 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000507}
508
509static int arch_timer_set_next_event_virt(unsigned long evt,
Stephen Boyd60faddf2013-07-18 16:59:31 -0700510 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000511{
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000512 int ret;
513
514 if (erratum_handler(set_next_event_virt, ret, evt, clk))
515 return ret;
Marc Zyngier83280892017-01-27 10:27:09 +0000516
Stephen Boyd60faddf2013-07-18 16:59:31 -0700517 set_next_event(ARCH_TIMER_VIRT_ACCESS, evt, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000518 return 0;
519}
520
521static int arch_timer_set_next_event_phys(unsigned long evt,
Stephen Boyd60faddf2013-07-18 16:59:31 -0700522 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000523{
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000524 int ret;
525
526 if (erratum_handler(set_next_event_phys, ret, evt, clk))
527 return ret;
Marc Zyngier83280892017-01-27 10:27:09 +0000528
Stephen Boyd60faddf2013-07-18 16:59:31 -0700529 set_next_event(ARCH_TIMER_PHYS_ACCESS, evt, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000530 return 0;
531}
532
Stephen Boyd22006992013-07-18 16:59:32 -0700533static int arch_timer_set_next_event_virt_mem(unsigned long evt,
534 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000535{
Stephen Boyd22006992013-07-18 16:59:32 -0700536 set_next_event(ARCH_TIMER_MEM_VIRT_ACCESS, evt, clk);
537 return 0;
538}
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000539
Stephen Boyd22006992013-07-18 16:59:32 -0700540static int arch_timer_set_next_event_phys_mem(unsigned long evt,
541 struct clock_event_device *clk)
542{
543 set_next_event(ARCH_TIMER_MEM_PHYS_ACCESS, evt, clk);
544 return 0;
545}
546
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200547static void __arch_timer_setup(unsigned type,
548 struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700549{
550 clk->features = CLOCK_EVT_FEAT_ONESHOT;
551
552 if (type == ARCH_CP15_TIMER) {
Lorenzo Pieralisi82a561942014-04-08 10:04:32 +0100553 if (arch_timer_c3stop)
554 clk->features |= CLOCK_EVT_FEAT_C3STOP;
Stephen Boyd22006992013-07-18 16:59:32 -0700555 clk->name = "arch_sys_timer";
556 clk->rating = 450;
557 clk->cpumask = cpumask_of(smp_processor_id());
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000558 clk->irq = arch_timer_ppi[arch_timer_uses_ppi];
559 switch (arch_timer_uses_ppi) {
560 case VIRT_PPI:
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530561 clk->set_state_shutdown = arch_timer_shutdown_virt;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530562 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt;
Stephen Boyd22006992013-07-18 16:59:32 -0700563 clk->set_next_event = arch_timer_set_next_event_virt;
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000564 break;
565 case PHYS_SECURE_PPI:
566 case PHYS_NONSECURE_PPI:
567 case HYP_PPI:
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530568 clk->set_state_shutdown = arch_timer_shutdown_phys;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530569 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys;
Stephen Boyd22006992013-07-18 16:59:32 -0700570 clk->set_next_event = arch_timer_set_next_event_phys;
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000571 break;
572 default:
573 BUG();
Stephen Boyd22006992013-07-18 16:59:32 -0700574 }
Scott Woodf6dc1572016-09-22 03:35:17 -0500575
Marc Zyngier00640302017-03-20 16:47:59 +0000576 arch_timer_check_ool_workaround(ate_match_local_cap_id, NULL);
Stephen Boyd22006992013-07-18 16:59:32 -0700577 } else {
Stephen Boyd7b52ad22014-01-06 14:56:17 -0800578 clk->features |= CLOCK_EVT_FEAT_DYNIRQ;
Stephen Boyd22006992013-07-18 16:59:32 -0700579 clk->name = "arch_mem_timer";
580 clk->rating = 400;
581 clk->cpumask = cpu_all_mask;
582 if (arch_timer_mem_use_virtual) {
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530583 clk->set_state_shutdown = arch_timer_shutdown_virt_mem;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530584 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt_mem;
Stephen Boyd22006992013-07-18 16:59:32 -0700585 clk->set_next_event =
586 arch_timer_set_next_event_virt_mem;
587 } else {
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530588 clk->set_state_shutdown = arch_timer_shutdown_phys_mem;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530589 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys_mem;
Stephen Boyd22006992013-07-18 16:59:32 -0700590 clk->set_next_event =
591 arch_timer_set_next_event_phys_mem;
592 }
593 }
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000594
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530595 clk->set_state_shutdown(clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000596
Stephen Boyd22006992013-07-18 16:59:32 -0700597 clockevents_config_and_register(clk, arch_timer_rate, 0xf, 0x7fffffff);
598}
599
Nathan Lynche1ce5c72014-09-29 01:50:06 +0200600static void arch_timer_evtstrm_enable(int divider)
601{
602 u32 cntkctl = arch_timer_get_cntkctl();
603
604 cntkctl &= ~ARCH_TIMER_EVT_TRIGGER_MASK;
605 /* Set the divider and enable virtual event stream */
606 cntkctl |= (divider << ARCH_TIMER_EVT_TRIGGER_SHIFT)
607 | ARCH_TIMER_VIRT_EVT_EN;
608 arch_timer_set_cntkctl(cntkctl);
609 elf_hwcap |= HWCAP_EVTSTRM;
610#ifdef CONFIG_COMPAT
611 compat_elf_hwcap |= COMPAT_HWCAP_EVTSTRM;
612#endif
613}
614
Will Deacon037f6372013-08-23 15:32:29 +0100615static void arch_timer_configure_evtstream(void)
616{
617 int evt_stream_div, pos;
618
619 /* Find the closest power of two to the divisor */
620 evt_stream_div = arch_timer_rate / ARCH_TIMER_EVT_STREAM_FREQ;
621 pos = fls(evt_stream_div);
622 if (pos > 1 && !(evt_stream_div & (1 << (pos - 2))))
623 pos--;
624 /* enable event stream */
625 arch_timer_evtstrm_enable(min(pos, 15));
626}
627
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200628static void arch_counter_set_user_access(void)
629{
630 u32 cntkctl = arch_timer_get_cntkctl();
631
632 /* Disable user access to the timers and the physical counter */
633 /* Also disable virtual event stream */
634 cntkctl &= ~(ARCH_TIMER_USR_PT_ACCESS_EN
635 | ARCH_TIMER_USR_VT_ACCESS_EN
636 | ARCH_TIMER_VIRT_EVT_EN
637 | ARCH_TIMER_USR_PCT_ACCESS_EN);
638
639 /* Enable user access to the virtual counter */
640 cntkctl |= ARCH_TIMER_USR_VCT_ACCESS_EN;
641
642 arch_timer_set_cntkctl(cntkctl);
643}
644
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000645static bool arch_timer_has_nonsecure_ppi(void)
646{
647 return (arch_timer_uses_ppi == PHYS_SECURE_PPI &&
648 arch_timer_ppi[PHYS_NONSECURE_PPI]);
649}
650
Marc Zyngierf005bd72016-08-01 10:54:15 +0100651static u32 check_ppi_trigger(int irq)
652{
653 u32 flags = irq_get_trigger_type(irq);
654
655 if (flags != IRQF_TRIGGER_HIGH && flags != IRQF_TRIGGER_LOW) {
656 pr_warn("WARNING: Invalid trigger for IRQ%d, assuming level low\n", irq);
657 pr_warn("WARNING: Please fix your firmware\n");
658 flags = IRQF_TRIGGER_LOW;
659 }
660
661 return flags;
662}
663
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000664static int arch_timer_starting_cpu(unsigned int cpu)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000665{
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000666 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
Marc Zyngierf005bd72016-08-01 10:54:15 +0100667 u32 flags;
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000668
Stephen Boyd22006992013-07-18 16:59:32 -0700669 __arch_timer_setup(ARCH_CP15_TIMER, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000670
Marc Zyngierf005bd72016-08-01 10:54:15 +0100671 flags = check_ppi_trigger(arch_timer_ppi[arch_timer_uses_ppi]);
672 enable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], flags);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000673
Marc Zyngierf005bd72016-08-01 10:54:15 +0100674 if (arch_timer_has_nonsecure_ppi()) {
675 flags = check_ppi_trigger(arch_timer_ppi[PHYS_NONSECURE_PPI]);
676 enable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI], flags);
677 }
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000678
679 arch_counter_set_user_access();
Will Deacon46fd5c62016-06-27 17:30:13 +0100680 if (evtstrm_enable)
Will Deacon037f6372013-08-23 15:32:29 +0100681 arch_timer_configure_evtstream();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000682
683 return 0;
684}
685
Stephen Boyd22006992013-07-18 16:59:32 -0700686static void
687arch_timer_detect_rate(void __iomem *cntbase, struct device_node *np)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000688{
Stephen Boyd22006992013-07-18 16:59:32 -0700689 /* Who has more than one independent system counter? */
690 if (arch_timer_rate)
691 return;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000692
Hanjun Guob09ca1e2015-03-24 14:02:50 +0000693 /*
694 * Try to determine the frequency from the device tree or CNTFRQ,
695 * if ACPI is enabled, get the frequency from CNTFRQ ONLY.
696 */
697 if (!acpi_disabled ||
698 of_property_read_u32(np, "clock-frequency", &arch_timer_rate)) {
Stephen Boyd22006992013-07-18 16:59:32 -0700699 if (cntbase)
700 arch_timer_rate = readl_relaxed(cntbase + CNTFRQ);
701 else
702 arch_timer_rate = arch_timer_get_cntfrq();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000703 }
704
Stephen Boyd22006992013-07-18 16:59:32 -0700705 /* Check the timer frequency. */
706 if (arch_timer_rate == 0)
707 pr_warn("Architected timer frequency not available\n");
708}
709
710static void arch_timer_banner(unsigned type)
711{
712 pr_info("Architected %s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
713 type & ARCH_CP15_TIMER ? "cp15" : "",
714 type == (ARCH_CP15_TIMER | ARCH_MEM_TIMER) ? " and " : "",
715 type & ARCH_MEM_TIMER ? "mmio" : "",
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000716 (unsigned long)arch_timer_rate / 1000000,
717 (unsigned long)(arch_timer_rate / 10000) % 100,
Stephen Boyd22006992013-07-18 16:59:32 -0700718 type & ARCH_CP15_TIMER ?
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000719 (arch_timer_uses_ppi == VIRT_PPI) ? "virt" : "phys" :
Stephen Boyd22006992013-07-18 16:59:32 -0700720 "",
721 type == (ARCH_CP15_TIMER | ARCH_MEM_TIMER) ? "/" : "",
722 type & ARCH_MEM_TIMER ?
723 arch_timer_mem_use_virtual ? "virt" : "phys" :
724 "");
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000725}
726
727u32 arch_timer_get_rate(void)
728{
729 return arch_timer_rate;
730}
731
Stephen Boyd22006992013-07-18 16:59:32 -0700732static u64 arch_counter_get_cntvct_mem(void)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000733{
Stephen Boyd22006992013-07-18 16:59:32 -0700734 u32 vct_lo, vct_hi, tmp_hi;
735
736 do {
737 vct_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
738 vct_lo = readl_relaxed(arch_counter_base + CNTVCT_LO);
739 tmp_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
740 } while (vct_hi != tmp_hi);
741
742 return ((u64) vct_hi << 32) | vct_lo;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000743}
744
Stephen Boyd22006992013-07-18 16:59:32 -0700745/*
746 * Default to cp15 based access because arm64 uses this function for
747 * sched_clock() before DT is probed and the cp15 method is guaranteed
748 * to exist on arm64. arm doesn't use this before DT is probed so even
749 * if we don't have the cp15 accessors we won't have a problem.
750 */
751u64 (*arch_timer_read_counter)(void) = arch_counter_get_cntvct;
752
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100753static u64 arch_counter_read(struct clocksource *cs)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000754{
Stephen Boyd22006992013-07-18 16:59:32 -0700755 return arch_timer_read_counter();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000756}
757
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100758static u64 arch_counter_read_cc(const struct cyclecounter *cc)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000759{
Stephen Boyd22006992013-07-18 16:59:32 -0700760 return arch_timer_read_counter();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000761}
762
763static struct clocksource clocksource_counter = {
764 .name = "arch_sys_counter",
765 .rating = 400,
766 .read = arch_counter_read,
767 .mask = CLOCKSOURCE_MASK(56),
Brian Norrisd8ec7592016-10-04 11:12:09 -0700768 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000769};
770
Bhumika Goyal3d837bc2017-02-12 00:50:18 +0530771static struct cyclecounter cyclecounter __ro_after_init = {
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000772 .read = arch_counter_read_cc,
773 .mask = CLOCKSOURCE_MASK(56),
774};
775
Julien Grallb4d6ce92016-04-11 16:32:51 +0100776static struct arch_timer_kvm_info arch_timer_kvm_info;
777
778struct arch_timer_kvm_info *arch_timer_get_kvm_info(void)
779{
780 return &arch_timer_kvm_info;
781}
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000782
Stephen Boyd22006992013-07-18 16:59:32 -0700783static void __init arch_counter_register(unsigned type)
784{
785 u64 start_count;
786
787 /* Register the CP15 based counter if we have one */
Nathan Lynch423bd692014-09-29 01:50:06 +0200788 if (type & ARCH_CP15_TIMER) {
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000789 if (IS_ENABLED(CONFIG_ARM64) || arch_timer_uses_ppi == VIRT_PPI)
Sonny Rao0b46b8a2014-11-23 23:02:44 -0800790 arch_timer_read_counter = arch_counter_get_cntvct;
791 else
792 arch_timer_read_counter = arch_counter_get_cntpct;
Scott Woodf6dc1572016-09-22 03:35:17 -0500793
Scott Wood1d8f51d2016-09-22 03:35:18 -0500794 clocksource_counter.archdata.vdso_direct = true;
795
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000796#ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
Scott Woodf6dc1572016-09-22 03:35:17 -0500797 /*
798 * Don't use the vdso fastpath if errata require using
799 * the out-of-line counter accessor.
800 */
801 if (static_branch_unlikely(&arch_timer_read_ool_enabled))
Scott Wood1d8f51d2016-09-22 03:35:18 -0500802 clocksource_counter.archdata.vdso_direct = false;
Scott Woodf6dc1572016-09-22 03:35:17 -0500803#endif
Nathan Lynch423bd692014-09-29 01:50:06 +0200804 } else {
Stephen Boyd22006992013-07-18 16:59:32 -0700805 arch_timer_read_counter = arch_counter_get_cntvct_mem;
Nathan Lynch423bd692014-09-29 01:50:06 +0200806 }
807
Brian Norrisd8ec7592016-10-04 11:12:09 -0700808 if (!arch_counter_suspend_stop)
809 clocksource_counter.flags |= CLOCK_SOURCE_SUSPEND_NONSTOP;
Stephen Boyd22006992013-07-18 16:59:32 -0700810 start_count = arch_timer_read_counter();
811 clocksource_register_hz(&clocksource_counter, arch_timer_rate);
812 cyclecounter.mult = clocksource_counter.mult;
813 cyclecounter.shift = clocksource_counter.shift;
Julien Grallb4d6ce92016-04-11 16:32:51 +0100814 timecounter_init(&arch_timer_kvm_info.timecounter,
815 &cyclecounter, start_count);
Thierry Reding4a7d3e82013-10-15 15:31:51 +0200816
817 /* 56 bits minimum, so we assume worst case rollover */
818 sched_clock_register(arch_timer_read_counter, 56, arch_timer_rate);
Stephen Boyd22006992013-07-18 16:59:32 -0700819}
820
Paul Gortmaker8c37bb32013-06-19 11:32:08 -0400821static void arch_timer_stop(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000822{
823 pr_debug("arch_timer_teardown disable IRQ%d cpu #%d\n",
824 clk->irq, smp_processor_id());
825
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000826 disable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi]);
827 if (arch_timer_has_nonsecure_ppi())
828 disable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI]);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000829
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530830 clk->set_state_shutdown(clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000831}
832
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000833static int arch_timer_dying_cpu(unsigned int cpu)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000834{
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000835 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000836
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000837 arch_timer_stop(clk);
838 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000839}
840
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100841#ifdef CONFIG_CPU_PM
842static unsigned int saved_cntkctl;
843static int arch_timer_cpu_pm_notify(struct notifier_block *self,
844 unsigned long action, void *hcpu)
845{
846 if (action == CPU_PM_ENTER)
847 saved_cntkctl = arch_timer_get_cntkctl();
848 else if (action == CPU_PM_ENTER_FAILED || action == CPU_PM_EXIT)
849 arch_timer_set_cntkctl(saved_cntkctl);
850 return NOTIFY_OK;
851}
852
853static struct notifier_block arch_timer_cpu_pm_notifier = {
854 .notifier_call = arch_timer_cpu_pm_notify,
855};
856
857static int __init arch_timer_cpu_pm_init(void)
858{
859 return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier);
860}
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000861
862static void __init arch_timer_cpu_pm_deinit(void)
863{
864 WARN_ON(cpu_pm_unregister_notifier(&arch_timer_cpu_pm_notifier));
865}
866
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100867#else
868static int __init arch_timer_cpu_pm_init(void)
869{
870 return 0;
871}
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000872
873static void __init arch_timer_cpu_pm_deinit(void)
874{
875}
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100876#endif
877
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000878static int __init arch_timer_register(void)
879{
880 int err;
881 int ppi;
882
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000883 arch_timer_evt = alloc_percpu(struct clock_event_device);
884 if (!arch_timer_evt) {
885 err = -ENOMEM;
886 goto out;
887 }
888
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000889 ppi = arch_timer_ppi[arch_timer_uses_ppi];
890 switch (arch_timer_uses_ppi) {
891 case VIRT_PPI:
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000892 err = request_percpu_irq(ppi, arch_timer_handler_virt,
893 "arch_timer", arch_timer_evt);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000894 break;
895 case PHYS_SECURE_PPI:
896 case PHYS_NONSECURE_PPI:
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000897 err = request_percpu_irq(ppi, arch_timer_handler_phys,
898 "arch_timer", arch_timer_evt);
899 if (!err && arch_timer_ppi[PHYS_NONSECURE_PPI]) {
900 ppi = arch_timer_ppi[PHYS_NONSECURE_PPI];
901 err = request_percpu_irq(ppi, arch_timer_handler_phys,
902 "arch_timer", arch_timer_evt);
903 if (err)
904 free_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI],
905 arch_timer_evt);
906 }
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000907 break;
908 case HYP_PPI:
909 err = request_percpu_irq(ppi, arch_timer_handler_phys,
910 "arch_timer", arch_timer_evt);
911 break;
912 default:
913 BUG();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000914 }
915
916 if (err) {
917 pr_err("arch_timer: can't register interrupt %d (%d)\n",
918 ppi, err);
919 goto out_free;
920 }
921
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100922 err = arch_timer_cpu_pm_init();
923 if (err)
924 goto out_unreg_notify;
925
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000926
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000927 /* Register and immediately configure the timer on the boot CPU */
928 err = cpuhp_setup_state(CPUHP_AP_ARM_ARCH_TIMER_STARTING,
Thomas Gleixner73c1b412016-12-21 20:19:54 +0100929 "clockevents/arm/arch_timer:starting",
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000930 arch_timer_starting_cpu, arch_timer_dying_cpu);
931 if (err)
932 goto out_unreg_cpupm;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000933 return 0;
934
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000935out_unreg_cpupm:
936 arch_timer_cpu_pm_deinit();
937
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100938out_unreg_notify:
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000939 free_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], arch_timer_evt);
940 if (arch_timer_has_nonsecure_ppi())
941 free_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI],
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000942 arch_timer_evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000943
944out_free:
945 free_percpu(arch_timer_evt);
946out:
947 return err;
948}
949
Stephen Boyd22006992013-07-18 16:59:32 -0700950static int __init arch_timer_mem_register(void __iomem *base, unsigned int irq)
951{
952 int ret;
953 irq_handler_t func;
954 struct arch_timer *t;
955
956 t = kzalloc(sizeof(*t), GFP_KERNEL);
957 if (!t)
958 return -ENOMEM;
959
960 t->base = base;
961 t->evt.irq = irq;
962 __arch_timer_setup(ARCH_MEM_TIMER, &t->evt);
963
964 if (arch_timer_mem_use_virtual)
965 func = arch_timer_handler_virt_mem;
966 else
967 func = arch_timer_handler_phys_mem;
968
969 ret = request_irq(irq, func, IRQF_TIMER, "arch_mem_timer", &t->evt);
970 if (ret) {
971 pr_err("arch_timer: Failed to request mem timer irq\n");
972 kfree(t);
973 }
974
975 return ret;
976}
977
978static const struct of_device_id arch_timer_of_match[] __initconst = {
979 { .compatible = "arm,armv7-timer", },
980 { .compatible = "arm,armv8-timer", },
981 {},
982};
983
984static const struct of_device_id arch_timer_mem_of_match[] __initconst = {
985 { .compatible = "arm,armv7-timer-mem", },
986 {},
987};
988
Sudeep Hollac387f072014-09-29 01:50:05 +0200989static bool __init
Laurent Pinchart566e6df2015-03-31 12:12:22 +0200990arch_timer_needs_probing(int type, const struct of_device_id *matches)
Sudeep Hollac387f072014-09-29 01:50:05 +0200991{
992 struct device_node *dn;
Laurent Pinchart566e6df2015-03-31 12:12:22 +0200993 bool needs_probing = false;
Sudeep Hollac387f072014-09-29 01:50:05 +0200994
995 dn = of_find_matching_node(NULL, matches);
Marc Zyngier59aa8962014-10-15 16:06:20 +0100996 if (dn && of_device_is_available(dn) && !(arch_timers_present & type))
Laurent Pinchart566e6df2015-03-31 12:12:22 +0200997 needs_probing = true;
Sudeep Hollac387f072014-09-29 01:50:05 +0200998 of_node_put(dn);
999
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001000 return needs_probing;
Sudeep Hollac387f072014-09-29 01:50:05 +02001001}
1002
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001003static int __init arch_timer_common_init(void)
Stephen Boyd22006992013-07-18 16:59:32 -07001004{
1005 unsigned mask = ARCH_CP15_TIMER | ARCH_MEM_TIMER;
1006
1007 /* Wait until both nodes are probed if we have two timers */
1008 if ((arch_timers_present & mask) != mask) {
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001009 if (arch_timer_needs_probing(ARCH_MEM_TIMER, arch_timer_mem_of_match))
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001010 return 0;
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001011 if (arch_timer_needs_probing(ARCH_CP15_TIMER, arch_timer_of_match))
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001012 return 0;
Stephen Boyd22006992013-07-18 16:59:32 -07001013 }
1014
1015 arch_timer_banner(arch_timers_present);
1016 arch_counter_register(arch_timers_present);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001017 return arch_timer_arch_init();
Stephen Boyd22006992013-07-18 16:59:32 -07001018}
1019
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001020static int __init arch_timer_init(void)
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001021{
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001022 int ret;
Doug Anderson65b57322014-10-08 00:33:47 -07001023 /*
Marc Zyngier82668912013-01-10 11:13:07 +00001024 * If HYP mode is available, we know that the physical timer
1025 * has been configured to be accessible from PL1. Use it, so
1026 * that a guest can use the virtual timer instead.
1027 *
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001028 * If no interrupt provided for virtual timer, we'll have to
1029 * stick to the physical timer. It'd better be accessible...
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001030 *
1031 * On ARMv8.1 with VH extensions, the kernel runs in HYP. VHE
1032 * accesses to CNTP_*_EL1 registers are silently redirected to
1033 * their CNTHP_*_EL2 counterparts, and use a different PPI
1034 * number.
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001035 */
Marc Zyngier82668912013-01-10 11:13:07 +00001036 if (is_hyp_mode_available() || !arch_timer_ppi[VIRT_PPI]) {
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001037 bool has_ppi;
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001038
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001039 if (is_kernel_in_hyp_mode()) {
1040 arch_timer_uses_ppi = HYP_PPI;
1041 has_ppi = !!arch_timer_ppi[HYP_PPI];
1042 } else {
1043 arch_timer_uses_ppi = PHYS_SECURE_PPI;
1044 has_ppi = (!!arch_timer_ppi[PHYS_SECURE_PPI] ||
1045 !!arch_timer_ppi[PHYS_NONSECURE_PPI]);
1046 }
1047
1048 if (!has_ppi) {
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001049 pr_warn("arch_timer: No interrupt available, giving up\n");
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001050 return -EINVAL;
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001051 }
1052 }
1053
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001054 ret = arch_timer_register();
1055 if (ret)
1056 return ret;
1057
1058 ret = arch_timer_common_init();
1059 if (ret)
1060 return ret;
Julien Gralld9b5e412016-04-11 16:32:52 +01001061
1062 arch_timer_kvm_info.virtual_irq = arch_timer_ppi[VIRT_PPI];
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001063
1064 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001065}
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001066
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001067static int __init arch_timer_of_init(struct device_node *np)
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001068{
1069 int i;
1070
1071 if (arch_timers_present & ARCH_CP15_TIMER) {
1072 pr_warn("arch_timer: multiple nodes in dt, skipping\n");
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001073 return 0;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001074 }
1075
1076 arch_timers_present |= ARCH_CP15_TIMER;
1077 for (i = PHYS_SECURE_PPI; i < MAX_TIMER_PPI; i++)
1078 arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
1079
1080 arch_timer_detect_rate(NULL, np);
1081
1082 arch_timer_c3stop = !of_property_read_bool(np, "always-on");
1083
Marc Zyngier651bb2e2017-01-19 17:20:59 +00001084 /* Check for globally applicable workarounds */
1085 arch_timer_check_ool_workaround(ate_match_dt, np);
Scott Woodf6dc1572016-09-22 03:35:17 -05001086
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001087 /*
1088 * If we cannot rely on firmware initializing the timer registers then
1089 * we should use the physical timers instead.
1090 */
1091 if (IS_ENABLED(CONFIG_ARM) &&
1092 of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001093 arch_timer_uses_ppi = PHYS_SECURE_PPI;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001094
Brian Norrisd8ec7592016-10-04 11:12:09 -07001095 /* On some systems, the counter stops ticking when in suspend. */
1096 arch_counter_suspend_stop = of_property_read_bool(np,
1097 "arm,no-tick-in-suspend");
1098
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001099 return arch_timer_init();
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001100}
Daniel Lezcano177cf6e2016-06-07 00:27:44 +02001101CLOCKSOURCE_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_of_init);
1102CLOCKSOURCE_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_of_init);
Stephen Boyd22006992013-07-18 16:59:32 -07001103
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001104static int __init arch_timer_mem_init(struct device_node *np)
Stephen Boyd22006992013-07-18 16:59:32 -07001105{
1106 struct device_node *frame, *best_frame = NULL;
1107 void __iomem *cntctlbase, *base;
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001108 unsigned int irq, ret = -EINVAL;
Stephen Boyd22006992013-07-18 16:59:32 -07001109 u32 cnttidr;
1110
1111 arch_timers_present |= ARCH_MEM_TIMER;
1112 cntctlbase = of_iomap(np, 0);
1113 if (!cntctlbase) {
1114 pr_err("arch_timer: Can't find CNTCTLBase\n");
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001115 return -ENXIO;
Stephen Boyd22006992013-07-18 16:59:32 -07001116 }
1117
1118 cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
Stephen Boyd22006992013-07-18 16:59:32 -07001119
1120 /*
1121 * Try to find a virtual capable frame. Otherwise fall back to a
1122 * physical capable frame.
1123 */
1124 for_each_available_child_of_node(np, frame) {
1125 int n;
Robin Murphye392d602016-02-01 12:00:48 +00001126 u32 cntacr;
Stephen Boyd22006992013-07-18 16:59:32 -07001127
1128 if (of_property_read_u32(frame, "frame-number", &n)) {
1129 pr_err("arch_timer: Missing frame-number\n");
Stephen Boyd22006992013-07-18 16:59:32 -07001130 of_node_put(frame);
Robin Murphye392d602016-02-01 12:00:48 +00001131 goto out;
Stephen Boyd22006992013-07-18 16:59:32 -07001132 }
1133
Robin Murphye392d602016-02-01 12:00:48 +00001134 /* Try enabling everything, and see what sticks */
1135 cntacr = CNTACR_RFRQ | CNTACR_RWPT | CNTACR_RPCT |
1136 CNTACR_RWVT | CNTACR_RVOFF | CNTACR_RVCT;
1137 writel_relaxed(cntacr, cntctlbase + CNTACR(n));
1138 cntacr = readl_relaxed(cntctlbase + CNTACR(n));
1139
1140 if ((cnttidr & CNTTIDR_VIRT(n)) &&
1141 !(~cntacr & (CNTACR_RWVT | CNTACR_RVCT))) {
Stephen Boyd22006992013-07-18 16:59:32 -07001142 of_node_put(best_frame);
1143 best_frame = frame;
1144 arch_timer_mem_use_virtual = true;
1145 break;
1146 }
Robin Murphye392d602016-02-01 12:00:48 +00001147
1148 if (~cntacr & (CNTACR_RWPT | CNTACR_RPCT))
1149 continue;
1150
Stephen Boyd22006992013-07-18 16:59:32 -07001151 of_node_put(best_frame);
1152 best_frame = of_node_get(frame);
1153 }
1154
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001155 ret= -ENXIO;
Stephen Boydf947ee12016-10-26 00:35:50 -07001156 base = arch_counter_base = of_io_request_and_map(best_frame, 0,
1157 "arch_mem_timer");
1158 if (IS_ERR(base)) {
Stephen Boyd22006992013-07-18 16:59:32 -07001159 pr_err("arch_timer: Can't map frame's registers\n");
Robin Murphye392d602016-02-01 12:00:48 +00001160 goto out;
Stephen Boyd22006992013-07-18 16:59:32 -07001161 }
1162
1163 if (arch_timer_mem_use_virtual)
1164 irq = irq_of_parse_and_map(best_frame, 1);
1165 else
1166 irq = irq_of_parse_and_map(best_frame, 0);
Robin Murphye392d602016-02-01 12:00:48 +00001167
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001168 ret = -EINVAL;
Stephen Boyd22006992013-07-18 16:59:32 -07001169 if (!irq) {
1170 pr_err("arch_timer: Frame missing %s irq",
Thomas Gleixnercfb6d652013-08-21 14:59:23 +02001171 arch_timer_mem_use_virtual ? "virt" : "phys");
Robin Murphye392d602016-02-01 12:00:48 +00001172 goto out;
Stephen Boyd22006992013-07-18 16:59:32 -07001173 }
1174
1175 arch_timer_detect_rate(base, np);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001176 ret = arch_timer_mem_register(base, irq);
1177 if (ret)
1178 goto out;
1179
1180 return arch_timer_common_init();
Robin Murphye392d602016-02-01 12:00:48 +00001181out:
1182 iounmap(cntctlbase);
1183 of_node_put(best_frame);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001184 return ret;
Stephen Boyd22006992013-07-18 16:59:32 -07001185}
Daniel Lezcano177cf6e2016-06-07 00:27:44 +02001186CLOCKSOURCE_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem",
Stephen Boyd22006992013-07-18 16:59:32 -07001187 arch_timer_mem_init);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001188
1189#ifdef CONFIG_ACPI
1190static int __init map_generic_timer_interrupt(u32 interrupt, u32 flags)
1191{
1192 int trigger, polarity;
1193
1194 if (!interrupt)
1195 return 0;
1196
1197 trigger = (flags & ACPI_GTDT_INTERRUPT_MODE) ? ACPI_EDGE_SENSITIVE
1198 : ACPI_LEVEL_SENSITIVE;
1199
1200 polarity = (flags & ACPI_GTDT_INTERRUPT_POLARITY) ? ACPI_ACTIVE_LOW
1201 : ACPI_ACTIVE_HIGH;
1202
1203 return acpi_register_gsi(NULL, interrupt, trigger, polarity);
1204}
1205
1206/* Initialize per-processor generic timer */
1207static int __init arch_timer_acpi_init(struct acpi_table_header *table)
1208{
1209 struct acpi_table_gtdt *gtdt;
1210
1211 if (arch_timers_present & ARCH_CP15_TIMER) {
1212 pr_warn("arch_timer: already initialized, skipping\n");
1213 return -EINVAL;
1214 }
1215
1216 gtdt = container_of(table, struct acpi_table_gtdt, header);
1217
1218 arch_timers_present |= ARCH_CP15_TIMER;
1219
1220 arch_timer_ppi[PHYS_SECURE_PPI] =
1221 map_generic_timer_interrupt(gtdt->secure_el1_interrupt,
1222 gtdt->secure_el1_flags);
1223
1224 arch_timer_ppi[PHYS_NONSECURE_PPI] =
1225 map_generic_timer_interrupt(gtdt->non_secure_el1_interrupt,
1226 gtdt->non_secure_el1_flags);
1227
1228 arch_timer_ppi[VIRT_PPI] =
1229 map_generic_timer_interrupt(gtdt->virtual_timer_interrupt,
1230 gtdt->virtual_timer_flags);
1231
1232 arch_timer_ppi[HYP_PPI] =
1233 map_generic_timer_interrupt(gtdt->non_secure_el2_interrupt,
1234 gtdt->non_secure_el2_flags);
1235
1236 /* Get the frequency from CNTFRQ */
1237 arch_timer_detect_rate(NULL, NULL);
1238
1239 /* Always-on capability */
1240 arch_timer_c3stop = !(gtdt->non_secure_el1_flags & ACPI_GTDT_ALWAYS_ON);
1241
1242 arch_timer_init();
1243 return 0;
1244}
Marc Zyngierae281cb2015-09-28 15:49:17 +01001245CLOCKSOURCE_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001246#endif