blob: 887f6d00a71d897cbc7c489eba82d6e8bb0b6be5 [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;
Marc Zyngiera86bd132017-02-01 12:07:15 +000086static bool vdso_default = true;
Mark Rutland8a4da6e2012-11-12 14:33:44 +000087
Will Deacon46fd5c62016-06-27 17:30:13 +010088static bool evtstrm_enable = IS_ENABLED(CONFIG_ARM_ARCH_TIMER_EVTSTREAM);
89
90static int __init early_evtstrm_cfg(char *buf)
91{
92 return strtobool(buf, &evtstrm_enable);
93}
94early_param("clocksource.arm_arch_timer.evtstrm", early_evtstrm_cfg);
95
Mark Rutland8a4da6e2012-11-12 14:33:44 +000096/*
97 * Architected system timer support.
98 */
99
Marc Zyngierf4e00a12017-01-20 18:28:32 +0000100static __always_inline
101void arch_timer_reg_write(int access, enum arch_timer_reg reg, u32 val,
102 struct clock_event_device *clk)
103{
104 if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
105 struct arch_timer *timer = to_arch_timer(clk);
106 switch (reg) {
107 case ARCH_TIMER_REG_CTRL:
108 writel_relaxed(val, timer->base + CNTP_CTL);
109 break;
110 case ARCH_TIMER_REG_TVAL:
111 writel_relaxed(val, timer->base + CNTP_TVAL);
112 break;
113 }
114 } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
115 struct arch_timer *timer = to_arch_timer(clk);
116 switch (reg) {
117 case ARCH_TIMER_REG_CTRL:
118 writel_relaxed(val, timer->base + CNTV_CTL);
119 break;
120 case ARCH_TIMER_REG_TVAL:
121 writel_relaxed(val, timer->base + CNTV_TVAL);
122 break;
123 }
124 } else {
125 arch_timer_reg_write_cp15(access, reg, val);
126 }
127}
128
129static __always_inline
130u32 arch_timer_reg_read(int access, enum arch_timer_reg reg,
131 struct clock_event_device *clk)
132{
133 u32 val;
134
135 if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
136 struct arch_timer *timer = to_arch_timer(clk);
137 switch (reg) {
138 case ARCH_TIMER_REG_CTRL:
139 val = readl_relaxed(timer->base + CNTP_CTL);
140 break;
141 case ARCH_TIMER_REG_TVAL:
142 val = readl_relaxed(timer->base + CNTP_TVAL);
143 break;
144 }
145 } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
146 struct arch_timer *timer = to_arch_timer(clk);
147 switch (reg) {
148 case ARCH_TIMER_REG_CTRL:
149 val = readl_relaxed(timer->base + CNTV_CTL);
150 break;
151 case ARCH_TIMER_REG_TVAL:
152 val = readl_relaxed(timer->base + CNTV_TVAL);
153 break;
154 }
155 } else {
156 val = arch_timer_reg_read_cp15(access, reg);
157 }
158
159 return val;
160}
161
Marc Zyngier992dd162017-02-01 11:53:46 +0000162/*
163 * Default to cp15 based access because arm64 uses this function for
164 * sched_clock() before DT is probed and the cp15 method is guaranteed
165 * to exist on arm64. arm doesn't use this before DT is probed so even
166 * if we don't have the cp15 accessors we won't have a problem.
167 */
168u64 (*arch_timer_read_counter)(void) = arch_counter_get_cntvct;
169
170static u64 arch_counter_read(struct clocksource *cs)
171{
172 return arch_timer_read_counter();
173}
174
175static u64 arch_counter_read_cc(const struct cyclecounter *cc)
176{
177 return arch_timer_read_counter();
178}
179
180static struct clocksource clocksource_counter = {
181 .name = "arch_sys_counter",
182 .rating = 400,
183 .read = arch_counter_read,
184 .mask = CLOCKSOURCE_MASK(56),
185 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
186};
187
188static struct cyclecounter cyclecounter __ro_after_init = {
189 .read = arch_counter_read_cc,
190 .mask = CLOCKSOURCE_MASK(56),
191};
192
Marc Zyngier5a38bca2017-02-21 14:37:30 +0000193struct ate_acpi_oem_info {
194 char oem_id[ACPI_OEM_ID_SIZE + 1];
195 char oem_table_id[ACPI_OEM_TABLE_ID_SIZE + 1];
196 u32 oem_revision;
197};
198
Scott Woodf6dc1572016-09-22 03:35:17 -0500199#ifdef CONFIG_FSL_ERRATUM_A008585
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000200/*
201 * The number of retries is an arbitrary value well beyond the highest number
202 * of iterations the loop has been observed to take.
203 */
204#define __fsl_a008585_read_reg(reg) ({ \
205 u64 _old, _new; \
206 int _retries = 200; \
207 \
208 do { \
209 _old = read_sysreg(reg); \
210 _new = read_sysreg(reg); \
211 _retries--; \
212 } while (unlikely(_old != _new) && _retries); \
213 \
214 WARN_ON_ONCE(!_retries); \
215 _new; \
216})
Scott Woodf6dc1572016-09-22 03:35:17 -0500217
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000218static u32 notrace fsl_a008585_read_cntp_tval_el0(void)
Scott Woodf6dc1572016-09-22 03:35:17 -0500219{
220 return __fsl_a008585_read_reg(cntp_tval_el0);
221}
222
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000223static u32 notrace fsl_a008585_read_cntv_tval_el0(void)
Scott Woodf6dc1572016-09-22 03:35:17 -0500224{
225 return __fsl_a008585_read_reg(cntv_tval_el0);
226}
227
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000228static u64 notrace fsl_a008585_read_cntvct_el0(void)
Scott Woodf6dc1572016-09-22 03:35:17 -0500229{
230 return __fsl_a008585_read_reg(cntvct_el0);
231}
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000232#endif
233
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000234#ifdef CONFIG_HISILICON_ERRATUM_161010101
235/*
236 * Verify whether the value of the second read is larger than the first by
237 * less than 32 is the only way to confirm the value is correct, so clear the
238 * lower 5 bits to check whether the difference is greater than 32 or not.
239 * Theoretically the erratum should not occur more than twice in succession
240 * when reading the system counter, but it is possible that some interrupts
241 * may lead to more than twice read errors, triggering the warning, so setting
242 * the number of retries far beyond the number of iterations the loop has been
243 * observed to take.
244 */
245#define __hisi_161010101_read_reg(reg) ({ \
246 u64 _old, _new; \
247 int _retries = 50; \
248 \
249 do { \
250 _old = read_sysreg(reg); \
251 _new = read_sysreg(reg); \
252 _retries--; \
253 } while (unlikely((_new - _old) >> 5) && _retries); \
254 \
255 WARN_ON_ONCE(!_retries); \
256 _new; \
257})
258
259static u32 notrace hisi_161010101_read_cntp_tval_el0(void)
260{
261 return __hisi_161010101_read_reg(cntp_tval_el0);
262}
263
264static u32 notrace hisi_161010101_read_cntv_tval_el0(void)
265{
266 return __hisi_161010101_read_reg(cntv_tval_el0);
267}
268
269static u64 notrace hisi_161010101_read_cntvct_el0(void)
270{
271 return __hisi_161010101_read_reg(cntvct_el0);
272}
273#endif
274
Marc Zyngierfa8d8152017-01-27 12:52:31 +0000275#ifdef CONFIG_ARM64_ERRATUM_858921
276static u64 notrace arm64_858921_read_cntvct_el0(void)
277{
278 u64 old, new;
279
280 old = read_sysreg(cntvct_el0);
281 new = read_sysreg(cntvct_el0);
282 return (((old ^ new) >> 32) & 1) ? old : new;
283}
284#endif
285
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000286#ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000287DEFINE_PER_CPU(const struct arch_timer_erratum_workaround *,
288 timer_unstable_counter_workaround);
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000289EXPORT_SYMBOL_GPL(timer_unstable_counter_workaround);
290
291DEFINE_STATIC_KEY_FALSE(arch_timer_read_ool_enabled);
292EXPORT_SYMBOL_GPL(arch_timer_read_ool_enabled);
293
Marc Zyngier83280892017-01-27 10:27:09 +0000294static void erratum_set_next_event_tval_generic(const int access, unsigned long evt,
295 struct clock_event_device *clk)
296{
297 unsigned long ctrl;
298 u64 cval = evt + arch_counter_get_cntvct();
299
300 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
301 ctrl |= ARCH_TIMER_CTRL_ENABLE;
302 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
303
304 if (access == ARCH_TIMER_PHYS_ACCESS)
305 write_sysreg(cval, cntp_cval_el0);
306 else
307 write_sysreg(cval, cntv_cval_el0);
308
309 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
310}
311
312static int erratum_set_next_event_tval_virt(unsigned long evt,
313 struct clock_event_device *clk)
314{
315 erratum_set_next_event_tval_generic(ARCH_TIMER_VIRT_ACCESS, evt, clk);
316 return 0;
317}
318
319static int erratum_set_next_event_tval_phys(unsigned long evt,
320 struct clock_event_device *clk)
321{
322 erratum_set_next_event_tval_generic(ARCH_TIMER_PHYS_ACCESS, evt, clk);
323 return 0;
324}
325
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000326static const struct arch_timer_erratum_workaround ool_workarounds[] = {
327#ifdef CONFIG_FSL_ERRATUM_A008585
328 {
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000329 .match_type = ate_match_dt,
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000330 .id = "fsl,erratum-a008585",
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000331 .desc = "Freescale erratum a005858",
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000332 .read_cntp_tval_el0 = fsl_a008585_read_cntp_tval_el0,
333 .read_cntv_tval_el0 = fsl_a008585_read_cntv_tval_el0,
334 .read_cntvct_el0 = fsl_a008585_read_cntvct_el0,
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000335 .set_next_event_phys = erratum_set_next_event_tval_phys,
336 .set_next_event_virt = erratum_set_next_event_tval_virt,
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000337 },
338#endif
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000339#ifdef CONFIG_HISILICON_ERRATUM_161010101
340 {
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000341 .match_type = ate_match_dt,
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000342 .id = "hisilicon,erratum-161010101",
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000343 .desc = "HiSilicon erratum 161010101",
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000344 .read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
345 .read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
346 .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000347 .set_next_event_phys = erratum_set_next_event_tval_phys,
348 .set_next_event_virt = erratum_set_next_event_tval_virt,
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000349 },
350#endif
Marc Zyngierfa8d8152017-01-27 12:52:31 +0000351#ifdef CONFIG_ARM64_ERRATUM_858921
352 {
353 .match_type = ate_match_local_cap_id,
354 .id = (void *)ARM64_WORKAROUND_858921,
355 .desc = "ARM erratum 858921",
356 .read_cntvct_el0 = arm64_858921_read_cntvct_el0,
357 },
358#endif
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000359};
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000360
361typedef bool (*ate_match_fn_t)(const struct arch_timer_erratum_workaround *,
362 const void *);
363
364static
365bool arch_timer_check_dt_erratum(const struct arch_timer_erratum_workaround *wa,
366 const void *arg)
367{
368 const struct device_node *np = arg;
369
370 return of_property_read_bool(np, wa->id);
371}
372
Marc Zyngier00640302017-03-20 16:47:59 +0000373static
374bool arch_timer_check_local_cap_erratum(const struct arch_timer_erratum_workaround *wa,
375 const void *arg)
376{
377 return this_cpu_has_cap((uintptr_t)wa->id);
378}
379
Marc Zyngier5a38bca2017-02-21 14:37:30 +0000380
381static
382bool arch_timer_check_acpi_oem_erratum(const struct arch_timer_erratum_workaround *wa,
383 const void *arg)
384{
385 static const struct ate_acpi_oem_info empty_oem_info = {};
386 const struct ate_acpi_oem_info *info = wa->id;
387 const struct acpi_table_header *table = arg;
388
389 /* Iterate over the ACPI OEM info array, looking for a match */
390 while (memcmp(info, &empty_oem_info, sizeof(*info))) {
391 if (!memcmp(info->oem_id, table->oem_id, ACPI_OEM_ID_SIZE) &&
392 !memcmp(info->oem_table_id, table->oem_table_id, ACPI_OEM_TABLE_ID_SIZE) &&
393 info->oem_revision == table->oem_revision)
394 return true;
395
396 info++;
397 }
398
399 return false;
400}
401
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000402static const struct arch_timer_erratum_workaround *
403arch_timer_iterate_errata(enum arch_timer_erratum_match_type type,
404 ate_match_fn_t match_fn,
405 void *arg)
406{
407 int i;
408
409 for (i = 0; i < ARRAY_SIZE(ool_workarounds); i++) {
410 if (ool_workarounds[i].match_type != type)
411 continue;
412
413 if (match_fn(&ool_workarounds[i], arg))
414 return &ool_workarounds[i];
415 }
416
417 return NULL;
418}
419
420static
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000421void arch_timer_enable_workaround(const struct arch_timer_erratum_workaround *wa,
422 bool local)
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000423{
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000424 int i;
425
426 if (local) {
427 __this_cpu_write(timer_unstable_counter_workaround, wa);
428 } else {
429 for_each_possible_cpu(i)
430 per_cpu(timer_unstable_counter_workaround, i) = wa;
431 }
432
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000433 static_branch_enable(&arch_timer_read_ool_enabled);
Marc Zyngiera86bd132017-02-01 12:07:15 +0000434
435 /*
436 * Don't use the vdso fastpath if errata require using the
437 * out-of-line counter accessor. We may change our mind pretty
438 * late in the game (with a per-CPU erratum, for example), so
439 * change both the default value and the vdso itself.
440 */
441 if (wa->read_cntvct_el0) {
442 clocksource_counter.archdata.vdso_direct = false;
443 vdso_default = false;
444 }
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000445}
446
447static void arch_timer_check_ool_workaround(enum arch_timer_erratum_match_type type,
448 void *arg)
449{
450 const struct arch_timer_erratum_workaround *wa;
451 ate_match_fn_t match_fn = NULL;
Marc Zyngier00640302017-03-20 16:47:59 +0000452 bool local = false;
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000453
454 switch (type) {
455 case ate_match_dt:
456 match_fn = arch_timer_check_dt_erratum;
457 break;
Marc Zyngier00640302017-03-20 16:47:59 +0000458 case ate_match_local_cap_id:
459 match_fn = arch_timer_check_local_cap_erratum;
460 local = true;
461 break;
Marc Zyngier5a38bca2017-02-21 14:37:30 +0000462 case ate_match_acpi_oem_info:
463 match_fn = arch_timer_check_acpi_oem_erratum;
464 break;
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000465 default:
466 WARN_ON(1);
467 return;
468 }
469
470 wa = arch_timer_iterate_errata(type, match_fn, arg);
471 if (!wa)
472 return;
473
Marc Zyngier00640302017-03-20 16:47:59 +0000474 if (needs_unstable_timer_counter_workaround()) {
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000475 const struct arch_timer_erratum_workaround *__wa;
476 __wa = __this_cpu_read(timer_unstable_counter_workaround);
477 if (__wa && wa != __wa)
Marc Zyngier00640302017-03-20 16:47:59 +0000478 pr_warn("Can't enable workaround for %s (clashes with %s\n)",
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000479 wa->desc, __wa->desc);
480
481 if (__wa)
482 return;
Marc Zyngier00640302017-03-20 16:47:59 +0000483 }
484
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000485 arch_timer_enable_workaround(wa, local);
Marc Zyngier00640302017-03-20 16:47:59 +0000486 pr_info("Enabling %s workaround for %s\n",
487 local ? "local" : "global", wa->desc);
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000488}
489
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000490#define erratum_handler(fn, r, ...) \
491({ \
492 bool __val; \
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000493 if (needs_unstable_timer_counter_workaround()) { \
494 const struct arch_timer_erratum_workaround *__wa; \
495 __wa = __this_cpu_read(timer_unstable_counter_workaround); \
496 if (__wa && __wa->fn) { \
497 r = __wa->fn(__VA_ARGS__); \
498 __val = true; \
499 } else { \
500 __val = false; \
501 } \
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000502 } else { \
503 __val = false; \
504 } \
505 __val; \
506})
507
Marc Zyngiera86bd132017-02-01 12:07:15 +0000508static bool arch_timer_this_cpu_has_cntvct_wa(void)
509{
510 const struct arch_timer_erratum_workaround *wa;
511
512 wa = __this_cpu_read(timer_unstable_counter_workaround);
513 return wa && wa->read_cntvct_el0;
514}
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000515#else
516#define arch_timer_check_ool_workaround(t,a) do { } while(0)
Marc Zyngier83280892017-01-27 10:27:09 +0000517#define erratum_set_next_event_tval_virt(...) ({BUG(); 0;})
518#define erratum_set_next_event_tval_phys(...) ({BUG(); 0;})
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000519#define erratum_handler(fn, r, ...) ({false;})
Marc Zyngiera86bd132017-02-01 12:07:15 +0000520#define arch_timer_this_cpu_has_cntvct_wa() ({false;})
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000521#endif /* CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND */
Scott Woodf6dc1572016-09-22 03:35:17 -0500522
Stephen Boyde09f3cc2013-07-18 16:59:28 -0700523static __always_inline irqreturn_t timer_handler(const int access,
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000524 struct clock_event_device *evt)
525{
526 unsigned long ctrl;
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200527
Stephen Boyd60faddf2013-07-18 16:59:31 -0700528 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000529 if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
530 ctrl |= ARCH_TIMER_CTRL_IT_MASK;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700531 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000532 evt->event_handler(evt);
533 return IRQ_HANDLED;
534 }
535
536 return IRQ_NONE;
537}
538
539static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
540{
541 struct clock_event_device *evt = dev_id;
542
543 return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
544}
545
546static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
547{
548 struct clock_event_device *evt = dev_id;
549
550 return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
551}
552
Stephen Boyd22006992013-07-18 16:59:32 -0700553static irqreturn_t arch_timer_handler_phys_mem(int irq, void *dev_id)
554{
555 struct clock_event_device *evt = dev_id;
556
557 return timer_handler(ARCH_TIMER_MEM_PHYS_ACCESS, evt);
558}
559
560static irqreturn_t arch_timer_handler_virt_mem(int irq, void *dev_id)
561{
562 struct clock_event_device *evt = dev_id;
563
564 return timer_handler(ARCH_TIMER_MEM_VIRT_ACCESS, evt);
565}
566
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530567static __always_inline int timer_shutdown(const int access,
568 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000569{
570 unsigned long ctrl;
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530571
572 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
573 ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
574 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
575
576 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000577}
578
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530579static int arch_timer_shutdown_virt(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000580{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530581 return timer_shutdown(ARCH_TIMER_VIRT_ACCESS, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000582}
583
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530584static int arch_timer_shutdown_phys(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000585{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530586 return timer_shutdown(ARCH_TIMER_PHYS_ACCESS, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000587}
588
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530589static int arch_timer_shutdown_virt_mem(struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700590{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530591 return timer_shutdown(ARCH_TIMER_MEM_VIRT_ACCESS, clk);
Stephen Boyd22006992013-07-18 16:59:32 -0700592}
593
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530594static int arch_timer_shutdown_phys_mem(struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700595{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530596 return timer_shutdown(ARCH_TIMER_MEM_PHYS_ACCESS, clk);
Stephen Boyd22006992013-07-18 16:59:32 -0700597}
598
Stephen Boyd60faddf2013-07-18 16:59:31 -0700599static __always_inline void set_next_event(const int access, unsigned long evt,
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200600 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000601{
602 unsigned long ctrl;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700603 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000604 ctrl |= ARCH_TIMER_CTRL_ENABLE;
605 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700606 arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt, clk);
607 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000608}
609
610static int arch_timer_set_next_event_virt(unsigned long evt,
Stephen Boyd60faddf2013-07-18 16:59:31 -0700611 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000612{
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000613 int ret;
614
615 if (erratum_handler(set_next_event_virt, ret, evt, clk))
616 return ret;
Marc Zyngier83280892017-01-27 10:27:09 +0000617
Stephen Boyd60faddf2013-07-18 16:59:31 -0700618 set_next_event(ARCH_TIMER_VIRT_ACCESS, evt, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000619 return 0;
620}
621
622static int arch_timer_set_next_event_phys(unsigned long evt,
Stephen Boyd60faddf2013-07-18 16:59:31 -0700623 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000624{
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000625 int ret;
626
627 if (erratum_handler(set_next_event_phys, ret, evt, clk))
628 return ret;
Marc Zyngier83280892017-01-27 10:27:09 +0000629
Stephen Boyd60faddf2013-07-18 16:59:31 -0700630 set_next_event(ARCH_TIMER_PHYS_ACCESS, evt, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000631 return 0;
632}
633
Stephen Boyd22006992013-07-18 16:59:32 -0700634static int arch_timer_set_next_event_virt_mem(unsigned long evt,
635 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000636{
Stephen Boyd22006992013-07-18 16:59:32 -0700637 set_next_event(ARCH_TIMER_MEM_VIRT_ACCESS, evt, clk);
638 return 0;
639}
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000640
Stephen Boyd22006992013-07-18 16:59:32 -0700641static int arch_timer_set_next_event_phys_mem(unsigned long evt,
642 struct clock_event_device *clk)
643{
644 set_next_event(ARCH_TIMER_MEM_PHYS_ACCESS, evt, clk);
645 return 0;
646}
647
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200648static void __arch_timer_setup(unsigned type,
649 struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700650{
651 clk->features = CLOCK_EVT_FEAT_ONESHOT;
652
653 if (type == ARCH_CP15_TIMER) {
Lorenzo Pieralisi82a561942014-04-08 10:04:32 +0100654 if (arch_timer_c3stop)
655 clk->features |= CLOCK_EVT_FEAT_C3STOP;
Stephen Boyd22006992013-07-18 16:59:32 -0700656 clk->name = "arch_sys_timer";
657 clk->rating = 450;
658 clk->cpumask = cpumask_of(smp_processor_id());
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000659 clk->irq = arch_timer_ppi[arch_timer_uses_ppi];
660 switch (arch_timer_uses_ppi) {
661 case VIRT_PPI:
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530662 clk->set_state_shutdown = arch_timer_shutdown_virt;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530663 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt;
Stephen Boyd22006992013-07-18 16:59:32 -0700664 clk->set_next_event = arch_timer_set_next_event_virt;
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000665 break;
666 case PHYS_SECURE_PPI:
667 case PHYS_NONSECURE_PPI:
668 case HYP_PPI:
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530669 clk->set_state_shutdown = arch_timer_shutdown_phys;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530670 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys;
Stephen Boyd22006992013-07-18 16:59:32 -0700671 clk->set_next_event = arch_timer_set_next_event_phys;
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000672 break;
673 default:
674 BUG();
Stephen Boyd22006992013-07-18 16:59:32 -0700675 }
Scott Woodf6dc1572016-09-22 03:35:17 -0500676
Marc Zyngier00640302017-03-20 16:47:59 +0000677 arch_timer_check_ool_workaround(ate_match_local_cap_id, NULL);
Stephen Boyd22006992013-07-18 16:59:32 -0700678 } else {
Stephen Boyd7b52ad22014-01-06 14:56:17 -0800679 clk->features |= CLOCK_EVT_FEAT_DYNIRQ;
Stephen Boyd22006992013-07-18 16:59:32 -0700680 clk->name = "arch_mem_timer";
681 clk->rating = 400;
682 clk->cpumask = cpu_all_mask;
683 if (arch_timer_mem_use_virtual) {
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530684 clk->set_state_shutdown = arch_timer_shutdown_virt_mem;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530685 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt_mem;
Stephen Boyd22006992013-07-18 16:59:32 -0700686 clk->set_next_event =
687 arch_timer_set_next_event_virt_mem;
688 } else {
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530689 clk->set_state_shutdown = arch_timer_shutdown_phys_mem;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530690 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys_mem;
Stephen Boyd22006992013-07-18 16:59:32 -0700691 clk->set_next_event =
692 arch_timer_set_next_event_phys_mem;
693 }
694 }
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000695
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530696 clk->set_state_shutdown(clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000697
Stephen Boyd22006992013-07-18 16:59:32 -0700698 clockevents_config_and_register(clk, arch_timer_rate, 0xf, 0x7fffffff);
699}
700
Nathan Lynche1ce5c72014-09-29 01:50:06 +0200701static void arch_timer_evtstrm_enable(int divider)
702{
703 u32 cntkctl = arch_timer_get_cntkctl();
704
705 cntkctl &= ~ARCH_TIMER_EVT_TRIGGER_MASK;
706 /* Set the divider and enable virtual event stream */
707 cntkctl |= (divider << ARCH_TIMER_EVT_TRIGGER_SHIFT)
708 | ARCH_TIMER_VIRT_EVT_EN;
709 arch_timer_set_cntkctl(cntkctl);
710 elf_hwcap |= HWCAP_EVTSTRM;
711#ifdef CONFIG_COMPAT
712 compat_elf_hwcap |= COMPAT_HWCAP_EVTSTRM;
713#endif
714}
715
Will Deacon037f6372013-08-23 15:32:29 +0100716static void arch_timer_configure_evtstream(void)
717{
718 int evt_stream_div, pos;
719
720 /* Find the closest power of two to the divisor */
721 evt_stream_div = arch_timer_rate / ARCH_TIMER_EVT_STREAM_FREQ;
722 pos = fls(evt_stream_div);
723 if (pos > 1 && !(evt_stream_div & (1 << (pos - 2))))
724 pos--;
725 /* enable event stream */
726 arch_timer_evtstrm_enable(min(pos, 15));
727}
728
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200729static void arch_counter_set_user_access(void)
730{
731 u32 cntkctl = arch_timer_get_cntkctl();
732
Marc Zyngiera86bd132017-02-01 12:07:15 +0000733 /* Disable user access to the timers and both counters */
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200734 /* Also disable virtual event stream */
735 cntkctl &= ~(ARCH_TIMER_USR_PT_ACCESS_EN
736 | ARCH_TIMER_USR_VT_ACCESS_EN
Marc Zyngiera86bd132017-02-01 12:07:15 +0000737 | ARCH_TIMER_USR_VCT_ACCESS_EN
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200738 | ARCH_TIMER_VIRT_EVT_EN
739 | ARCH_TIMER_USR_PCT_ACCESS_EN);
740
Marc Zyngiera86bd132017-02-01 12:07:15 +0000741 /*
742 * Enable user access to the virtual counter if it doesn't
743 * need to be workaround. The vdso may have been already
744 * disabled though.
745 */
746 if (arch_timer_this_cpu_has_cntvct_wa())
747 pr_info("CPU%d: Trapping CNTVCT access\n", smp_processor_id());
748 else
749 cntkctl |= ARCH_TIMER_USR_VCT_ACCESS_EN;
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200750
751 arch_timer_set_cntkctl(cntkctl);
752}
753
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000754static bool arch_timer_has_nonsecure_ppi(void)
755{
756 return (arch_timer_uses_ppi == PHYS_SECURE_PPI &&
757 arch_timer_ppi[PHYS_NONSECURE_PPI]);
758}
759
Marc Zyngierf005bd72016-08-01 10:54:15 +0100760static u32 check_ppi_trigger(int irq)
761{
762 u32 flags = irq_get_trigger_type(irq);
763
764 if (flags != IRQF_TRIGGER_HIGH && flags != IRQF_TRIGGER_LOW) {
765 pr_warn("WARNING: Invalid trigger for IRQ%d, assuming level low\n", irq);
766 pr_warn("WARNING: Please fix your firmware\n");
767 flags = IRQF_TRIGGER_LOW;
768 }
769
770 return flags;
771}
772
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000773static int arch_timer_starting_cpu(unsigned int cpu)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000774{
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000775 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
Marc Zyngierf005bd72016-08-01 10:54:15 +0100776 u32 flags;
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000777
Stephen Boyd22006992013-07-18 16:59:32 -0700778 __arch_timer_setup(ARCH_CP15_TIMER, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000779
Marc Zyngierf005bd72016-08-01 10:54:15 +0100780 flags = check_ppi_trigger(arch_timer_ppi[arch_timer_uses_ppi]);
781 enable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], flags);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000782
Marc Zyngierf005bd72016-08-01 10:54:15 +0100783 if (arch_timer_has_nonsecure_ppi()) {
784 flags = check_ppi_trigger(arch_timer_ppi[PHYS_NONSECURE_PPI]);
785 enable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI], flags);
786 }
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000787
788 arch_counter_set_user_access();
Will Deacon46fd5c62016-06-27 17:30:13 +0100789 if (evtstrm_enable)
Will Deacon037f6372013-08-23 15:32:29 +0100790 arch_timer_configure_evtstream();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000791
792 return 0;
793}
794
Stephen Boyd22006992013-07-18 16:59:32 -0700795static void
796arch_timer_detect_rate(void __iomem *cntbase, struct device_node *np)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000797{
Stephen Boyd22006992013-07-18 16:59:32 -0700798 /* Who has more than one independent system counter? */
799 if (arch_timer_rate)
800 return;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000801
Hanjun Guob09ca1e2015-03-24 14:02:50 +0000802 /*
803 * Try to determine the frequency from the device tree or CNTFRQ,
804 * if ACPI is enabled, get the frequency from CNTFRQ ONLY.
805 */
806 if (!acpi_disabled ||
807 of_property_read_u32(np, "clock-frequency", &arch_timer_rate)) {
Stephen Boyd22006992013-07-18 16:59:32 -0700808 if (cntbase)
809 arch_timer_rate = readl_relaxed(cntbase + CNTFRQ);
810 else
811 arch_timer_rate = arch_timer_get_cntfrq();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000812 }
813
Stephen Boyd22006992013-07-18 16:59:32 -0700814 /* Check the timer frequency. */
815 if (arch_timer_rate == 0)
816 pr_warn("Architected timer frequency not available\n");
817}
818
819static void arch_timer_banner(unsigned type)
820{
821 pr_info("Architected %s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
822 type & ARCH_CP15_TIMER ? "cp15" : "",
823 type == (ARCH_CP15_TIMER | ARCH_MEM_TIMER) ? " and " : "",
824 type & ARCH_MEM_TIMER ? "mmio" : "",
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000825 (unsigned long)arch_timer_rate / 1000000,
826 (unsigned long)(arch_timer_rate / 10000) % 100,
Stephen Boyd22006992013-07-18 16:59:32 -0700827 type & ARCH_CP15_TIMER ?
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000828 (arch_timer_uses_ppi == VIRT_PPI) ? "virt" : "phys" :
Stephen Boyd22006992013-07-18 16:59:32 -0700829 "",
830 type == (ARCH_CP15_TIMER | ARCH_MEM_TIMER) ? "/" : "",
831 type & ARCH_MEM_TIMER ?
832 arch_timer_mem_use_virtual ? "virt" : "phys" :
833 "");
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000834}
835
836u32 arch_timer_get_rate(void)
837{
838 return arch_timer_rate;
839}
840
Stephen Boyd22006992013-07-18 16:59:32 -0700841static u64 arch_counter_get_cntvct_mem(void)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000842{
Stephen Boyd22006992013-07-18 16:59:32 -0700843 u32 vct_lo, vct_hi, tmp_hi;
844
845 do {
846 vct_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
847 vct_lo = readl_relaxed(arch_counter_base + CNTVCT_LO);
848 tmp_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
849 } while (vct_hi != tmp_hi);
850
851 return ((u64) vct_hi << 32) | vct_lo;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000852}
853
Julien Grallb4d6ce92016-04-11 16:32:51 +0100854static struct arch_timer_kvm_info arch_timer_kvm_info;
855
856struct arch_timer_kvm_info *arch_timer_get_kvm_info(void)
857{
858 return &arch_timer_kvm_info;
859}
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000860
Stephen Boyd22006992013-07-18 16:59:32 -0700861static void __init arch_counter_register(unsigned type)
862{
863 u64 start_count;
864
865 /* Register the CP15 based counter if we have one */
Nathan Lynch423bd692014-09-29 01:50:06 +0200866 if (type & ARCH_CP15_TIMER) {
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000867 if (IS_ENABLED(CONFIG_ARM64) || arch_timer_uses_ppi == VIRT_PPI)
Sonny Rao0b46b8a2014-11-23 23:02:44 -0800868 arch_timer_read_counter = arch_counter_get_cntvct;
869 else
870 arch_timer_read_counter = arch_counter_get_cntpct;
Scott Woodf6dc1572016-09-22 03:35:17 -0500871
Marc Zyngiera86bd132017-02-01 12:07:15 +0000872 clocksource_counter.archdata.vdso_direct = vdso_default;
Nathan Lynch423bd692014-09-29 01:50:06 +0200873 } else {
Stephen Boyd22006992013-07-18 16:59:32 -0700874 arch_timer_read_counter = arch_counter_get_cntvct_mem;
Nathan Lynch423bd692014-09-29 01:50:06 +0200875 }
876
Brian Norrisd8ec7592016-10-04 11:12:09 -0700877 if (!arch_counter_suspend_stop)
878 clocksource_counter.flags |= CLOCK_SOURCE_SUSPEND_NONSTOP;
Stephen Boyd22006992013-07-18 16:59:32 -0700879 start_count = arch_timer_read_counter();
880 clocksource_register_hz(&clocksource_counter, arch_timer_rate);
881 cyclecounter.mult = clocksource_counter.mult;
882 cyclecounter.shift = clocksource_counter.shift;
Julien Grallb4d6ce92016-04-11 16:32:51 +0100883 timecounter_init(&arch_timer_kvm_info.timecounter,
884 &cyclecounter, start_count);
Thierry Reding4a7d3e82013-10-15 15:31:51 +0200885
886 /* 56 bits minimum, so we assume worst case rollover */
887 sched_clock_register(arch_timer_read_counter, 56, arch_timer_rate);
Stephen Boyd22006992013-07-18 16:59:32 -0700888}
889
Paul Gortmaker8c37bb32013-06-19 11:32:08 -0400890static void arch_timer_stop(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000891{
892 pr_debug("arch_timer_teardown disable IRQ%d cpu #%d\n",
893 clk->irq, smp_processor_id());
894
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000895 disable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi]);
896 if (arch_timer_has_nonsecure_ppi())
897 disable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI]);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000898
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530899 clk->set_state_shutdown(clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000900}
901
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000902static int arch_timer_dying_cpu(unsigned int cpu)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000903{
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000904 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000905
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000906 arch_timer_stop(clk);
907 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000908}
909
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100910#ifdef CONFIG_CPU_PM
Marc Zyngierbee67c52017-04-04 17:05:16 +0100911static DEFINE_PER_CPU(unsigned long, saved_cntkctl);
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100912static int arch_timer_cpu_pm_notify(struct notifier_block *self,
913 unsigned long action, void *hcpu)
914{
915 if (action == CPU_PM_ENTER)
Marc Zyngierbee67c52017-04-04 17:05:16 +0100916 __this_cpu_write(saved_cntkctl, arch_timer_get_cntkctl());
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100917 else if (action == CPU_PM_ENTER_FAILED || action == CPU_PM_EXIT)
Marc Zyngierbee67c52017-04-04 17:05:16 +0100918 arch_timer_set_cntkctl(__this_cpu_read(saved_cntkctl));
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100919 return NOTIFY_OK;
920}
921
922static struct notifier_block arch_timer_cpu_pm_notifier = {
923 .notifier_call = arch_timer_cpu_pm_notify,
924};
925
926static int __init arch_timer_cpu_pm_init(void)
927{
928 return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier);
929}
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000930
931static void __init arch_timer_cpu_pm_deinit(void)
932{
933 WARN_ON(cpu_pm_unregister_notifier(&arch_timer_cpu_pm_notifier));
934}
935
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100936#else
937static int __init arch_timer_cpu_pm_init(void)
938{
939 return 0;
940}
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000941
942static void __init arch_timer_cpu_pm_deinit(void)
943{
944}
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100945#endif
946
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000947static int __init arch_timer_register(void)
948{
949 int err;
950 int ppi;
951
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000952 arch_timer_evt = alloc_percpu(struct clock_event_device);
953 if (!arch_timer_evt) {
954 err = -ENOMEM;
955 goto out;
956 }
957
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000958 ppi = arch_timer_ppi[arch_timer_uses_ppi];
959 switch (arch_timer_uses_ppi) {
960 case VIRT_PPI:
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000961 err = request_percpu_irq(ppi, arch_timer_handler_virt,
962 "arch_timer", arch_timer_evt);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000963 break;
964 case PHYS_SECURE_PPI:
965 case PHYS_NONSECURE_PPI:
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000966 err = request_percpu_irq(ppi, arch_timer_handler_phys,
967 "arch_timer", arch_timer_evt);
968 if (!err && arch_timer_ppi[PHYS_NONSECURE_PPI]) {
969 ppi = arch_timer_ppi[PHYS_NONSECURE_PPI];
970 err = request_percpu_irq(ppi, arch_timer_handler_phys,
971 "arch_timer", arch_timer_evt);
972 if (err)
973 free_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI],
974 arch_timer_evt);
975 }
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000976 break;
977 case HYP_PPI:
978 err = request_percpu_irq(ppi, arch_timer_handler_phys,
979 "arch_timer", arch_timer_evt);
980 break;
981 default:
982 BUG();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000983 }
984
985 if (err) {
986 pr_err("arch_timer: can't register interrupt %d (%d)\n",
987 ppi, err);
988 goto out_free;
989 }
990
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100991 err = arch_timer_cpu_pm_init();
992 if (err)
993 goto out_unreg_notify;
994
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000995
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000996 /* Register and immediately configure the timer on the boot CPU */
997 err = cpuhp_setup_state(CPUHP_AP_ARM_ARCH_TIMER_STARTING,
Thomas Gleixner73c1b412016-12-21 20:19:54 +0100998 "clockevents/arm/arch_timer:starting",
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000999 arch_timer_starting_cpu, arch_timer_dying_cpu);
1000 if (err)
1001 goto out_unreg_cpupm;
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001002 return 0;
1003
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001004out_unreg_cpupm:
1005 arch_timer_cpu_pm_deinit();
1006
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001007out_unreg_notify:
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001008 free_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], arch_timer_evt);
1009 if (arch_timer_has_nonsecure_ppi())
1010 free_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI],
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001011 arch_timer_evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001012
1013out_free:
1014 free_percpu(arch_timer_evt);
1015out:
1016 return err;
1017}
1018
Stephen Boyd22006992013-07-18 16:59:32 -07001019static int __init arch_timer_mem_register(void __iomem *base, unsigned int irq)
1020{
1021 int ret;
1022 irq_handler_t func;
1023 struct arch_timer *t;
1024
1025 t = kzalloc(sizeof(*t), GFP_KERNEL);
1026 if (!t)
1027 return -ENOMEM;
1028
1029 t->base = base;
1030 t->evt.irq = irq;
1031 __arch_timer_setup(ARCH_MEM_TIMER, &t->evt);
1032
1033 if (arch_timer_mem_use_virtual)
1034 func = arch_timer_handler_virt_mem;
1035 else
1036 func = arch_timer_handler_phys_mem;
1037
1038 ret = request_irq(irq, func, IRQF_TIMER, "arch_mem_timer", &t->evt);
1039 if (ret) {
1040 pr_err("arch_timer: Failed to request mem timer irq\n");
1041 kfree(t);
1042 }
1043
1044 return ret;
1045}
1046
1047static const struct of_device_id arch_timer_of_match[] __initconst = {
1048 { .compatible = "arm,armv7-timer", },
1049 { .compatible = "arm,armv8-timer", },
1050 {},
1051};
1052
1053static const struct of_device_id arch_timer_mem_of_match[] __initconst = {
1054 { .compatible = "arm,armv7-timer-mem", },
1055 {},
1056};
1057
Sudeep Hollac387f072014-09-29 01:50:05 +02001058static bool __init
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001059arch_timer_needs_probing(int type, const struct of_device_id *matches)
Sudeep Hollac387f072014-09-29 01:50:05 +02001060{
1061 struct device_node *dn;
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001062 bool needs_probing = false;
Sudeep Hollac387f072014-09-29 01:50:05 +02001063
1064 dn = of_find_matching_node(NULL, matches);
Marc Zyngier59aa8962014-10-15 16:06:20 +01001065 if (dn && of_device_is_available(dn) && !(arch_timers_present & type))
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001066 needs_probing = true;
Sudeep Hollac387f072014-09-29 01:50:05 +02001067 of_node_put(dn);
1068
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001069 return needs_probing;
Sudeep Hollac387f072014-09-29 01:50:05 +02001070}
1071
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001072static int __init arch_timer_common_init(void)
Stephen Boyd22006992013-07-18 16:59:32 -07001073{
1074 unsigned mask = ARCH_CP15_TIMER | ARCH_MEM_TIMER;
1075
1076 /* Wait until both nodes are probed if we have two timers */
1077 if ((arch_timers_present & mask) != mask) {
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001078 if (arch_timer_needs_probing(ARCH_MEM_TIMER, arch_timer_mem_of_match))
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001079 return 0;
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001080 if (arch_timer_needs_probing(ARCH_CP15_TIMER, arch_timer_of_match))
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001081 return 0;
Stephen Boyd22006992013-07-18 16:59:32 -07001082 }
1083
1084 arch_timer_banner(arch_timers_present);
1085 arch_counter_register(arch_timers_present);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001086 return arch_timer_arch_init();
Stephen Boyd22006992013-07-18 16:59:32 -07001087}
1088
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001089static int __init arch_timer_init(void)
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001090{
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001091 int ret;
Doug Anderson65b57322014-10-08 00:33:47 -07001092 /*
Marc Zyngier82668912013-01-10 11:13:07 +00001093 * If HYP mode is available, we know that the physical timer
1094 * has been configured to be accessible from PL1. Use it, so
1095 * that a guest can use the virtual timer instead.
1096 *
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001097 * If no interrupt provided for virtual timer, we'll have to
1098 * stick to the physical timer. It'd better be accessible...
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001099 *
1100 * On ARMv8.1 with VH extensions, the kernel runs in HYP. VHE
1101 * accesses to CNTP_*_EL1 registers are silently redirected to
1102 * their CNTHP_*_EL2 counterparts, and use a different PPI
1103 * number.
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001104 */
Marc Zyngier82668912013-01-10 11:13:07 +00001105 if (is_hyp_mode_available() || !arch_timer_ppi[VIRT_PPI]) {
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001106 bool has_ppi;
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001107
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001108 if (is_kernel_in_hyp_mode()) {
1109 arch_timer_uses_ppi = HYP_PPI;
1110 has_ppi = !!arch_timer_ppi[HYP_PPI];
1111 } else {
1112 arch_timer_uses_ppi = PHYS_SECURE_PPI;
1113 has_ppi = (!!arch_timer_ppi[PHYS_SECURE_PPI] ||
1114 !!arch_timer_ppi[PHYS_NONSECURE_PPI]);
1115 }
1116
1117 if (!has_ppi) {
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001118 pr_warn("arch_timer: No interrupt available, giving up\n");
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001119 return -EINVAL;
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001120 }
1121 }
1122
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001123 ret = arch_timer_register();
1124 if (ret)
1125 return ret;
1126
1127 ret = arch_timer_common_init();
1128 if (ret)
1129 return ret;
Julien Gralld9b5e412016-04-11 16:32:52 +01001130
1131 arch_timer_kvm_info.virtual_irq = arch_timer_ppi[VIRT_PPI];
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001132
1133 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001134}
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001135
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001136static int __init arch_timer_of_init(struct device_node *np)
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001137{
1138 int i;
1139
1140 if (arch_timers_present & ARCH_CP15_TIMER) {
1141 pr_warn("arch_timer: multiple nodes in dt, skipping\n");
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001142 return 0;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001143 }
1144
1145 arch_timers_present |= ARCH_CP15_TIMER;
1146 for (i = PHYS_SECURE_PPI; i < MAX_TIMER_PPI; i++)
1147 arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
1148
1149 arch_timer_detect_rate(NULL, np);
1150
1151 arch_timer_c3stop = !of_property_read_bool(np, "always-on");
1152
Marc Zyngier651bb2e2017-01-19 17:20:59 +00001153 /* Check for globally applicable workarounds */
1154 arch_timer_check_ool_workaround(ate_match_dt, np);
Scott Woodf6dc1572016-09-22 03:35:17 -05001155
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001156 /*
1157 * If we cannot rely on firmware initializing the timer registers then
1158 * we should use the physical timers instead.
1159 */
1160 if (IS_ENABLED(CONFIG_ARM) &&
1161 of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001162 arch_timer_uses_ppi = PHYS_SECURE_PPI;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001163
Brian Norrisd8ec7592016-10-04 11:12:09 -07001164 /* On some systems, the counter stops ticking when in suspend. */
1165 arch_counter_suspend_stop = of_property_read_bool(np,
1166 "arm,no-tick-in-suspend");
1167
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001168 return arch_timer_init();
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001169}
Daniel Lezcano177cf6e2016-06-07 00:27:44 +02001170CLOCKSOURCE_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_of_init);
1171CLOCKSOURCE_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_of_init);
Stephen Boyd22006992013-07-18 16:59:32 -07001172
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001173static int __init arch_timer_mem_init(struct device_node *np)
Stephen Boyd22006992013-07-18 16:59:32 -07001174{
1175 struct device_node *frame, *best_frame = NULL;
1176 void __iomem *cntctlbase, *base;
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001177 unsigned int irq, ret = -EINVAL;
Stephen Boyd22006992013-07-18 16:59:32 -07001178 u32 cnttidr;
1179
1180 arch_timers_present |= ARCH_MEM_TIMER;
1181 cntctlbase = of_iomap(np, 0);
1182 if (!cntctlbase) {
1183 pr_err("arch_timer: Can't find CNTCTLBase\n");
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001184 return -ENXIO;
Stephen Boyd22006992013-07-18 16:59:32 -07001185 }
1186
1187 cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
Stephen Boyd22006992013-07-18 16:59:32 -07001188
1189 /*
1190 * Try to find a virtual capable frame. Otherwise fall back to a
1191 * physical capable frame.
1192 */
1193 for_each_available_child_of_node(np, frame) {
1194 int n;
Robin Murphye392d602016-02-01 12:00:48 +00001195 u32 cntacr;
Stephen Boyd22006992013-07-18 16:59:32 -07001196
1197 if (of_property_read_u32(frame, "frame-number", &n)) {
1198 pr_err("arch_timer: Missing frame-number\n");
Stephen Boyd22006992013-07-18 16:59:32 -07001199 of_node_put(frame);
Robin Murphye392d602016-02-01 12:00:48 +00001200 goto out;
Stephen Boyd22006992013-07-18 16:59:32 -07001201 }
1202
Robin Murphye392d602016-02-01 12:00:48 +00001203 /* Try enabling everything, and see what sticks */
1204 cntacr = CNTACR_RFRQ | CNTACR_RWPT | CNTACR_RPCT |
1205 CNTACR_RWVT | CNTACR_RVOFF | CNTACR_RVCT;
1206 writel_relaxed(cntacr, cntctlbase + CNTACR(n));
1207 cntacr = readl_relaxed(cntctlbase + CNTACR(n));
1208
1209 if ((cnttidr & CNTTIDR_VIRT(n)) &&
1210 !(~cntacr & (CNTACR_RWVT | CNTACR_RVCT))) {
Stephen Boyd22006992013-07-18 16:59:32 -07001211 of_node_put(best_frame);
1212 best_frame = frame;
1213 arch_timer_mem_use_virtual = true;
1214 break;
1215 }
Robin Murphye392d602016-02-01 12:00:48 +00001216
1217 if (~cntacr & (CNTACR_RWPT | CNTACR_RPCT))
1218 continue;
1219
Stephen Boyd22006992013-07-18 16:59:32 -07001220 of_node_put(best_frame);
1221 best_frame = of_node_get(frame);
1222 }
1223
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001224 ret= -ENXIO;
Stephen Boydf947ee12016-10-26 00:35:50 -07001225 base = arch_counter_base = of_io_request_and_map(best_frame, 0,
1226 "arch_mem_timer");
1227 if (IS_ERR(base)) {
Stephen Boyd22006992013-07-18 16:59:32 -07001228 pr_err("arch_timer: Can't map frame's registers\n");
Robin Murphye392d602016-02-01 12:00:48 +00001229 goto out;
Stephen Boyd22006992013-07-18 16:59:32 -07001230 }
1231
1232 if (arch_timer_mem_use_virtual)
1233 irq = irq_of_parse_and_map(best_frame, 1);
1234 else
1235 irq = irq_of_parse_and_map(best_frame, 0);
Robin Murphye392d602016-02-01 12:00:48 +00001236
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001237 ret = -EINVAL;
Stephen Boyd22006992013-07-18 16:59:32 -07001238 if (!irq) {
1239 pr_err("arch_timer: Frame missing %s irq",
Thomas Gleixnercfb6d652013-08-21 14:59:23 +02001240 arch_timer_mem_use_virtual ? "virt" : "phys");
Robin Murphye392d602016-02-01 12:00:48 +00001241 goto out;
Stephen Boyd22006992013-07-18 16:59:32 -07001242 }
1243
1244 arch_timer_detect_rate(base, np);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001245 ret = arch_timer_mem_register(base, irq);
1246 if (ret)
1247 goto out;
1248
1249 return arch_timer_common_init();
Robin Murphye392d602016-02-01 12:00:48 +00001250out:
1251 iounmap(cntctlbase);
1252 of_node_put(best_frame);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001253 return ret;
Stephen Boyd22006992013-07-18 16:59:32 -07001254}
Daniel Lezcano177cf6e2016-06-07 00:27:44 +02001255CLOCKSOURCE_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem",
Stephen Boyd22006992013-07-18 16:59:32 -07001256 arch_timer_mem_init);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001257
1258#ifdef CONFIG_ACPI
1259static int __init map_generic_timer_interrupt(u32 interrupt, u32 flags)
1260{
1261 int trigger, polarity;
1262
1263 if (!interrupt)
1264 return 0;
1265
1266 trigger = (flags & ACPI_GTDT_INTERRUPT_MODE) ? ACPI_EDGE_SENSITIVE
1267 : ACPI_LEVEL_SENSITIVE;
1268
1269 polarity = (flags & ACPI_GTDT_INTERRUPT_POLARITY) ? ACPI_ACTIVE_LOW
1270 : ACPI_ACTIVE_HIGH;
1271
1272 return acpi_register_gsi(NULL, interrupt, trigger, polarity);
1273}
1274
1275/* Initialize per-processor generic timer */
1276static int __init arch_timer_acpi_init(struct acpi_table_header *table)
1277{
1278 struct acpi_table_gtdt *gtdt;
1279
1280 if (arch_timers_present & ARCH_CP15_TIMER) {
1281 pr_warn("arch_timer: already initialized, skipping\n");
1282 return -EINVAL;
1283 }
1284
1285 gtdt = container_of(table, struct acpi_table_gtdt, header);
1286
1287 arch_timers_present |= ARCH_CP15_TIMER;
1288
1289 arch_timer_ppi[PHYS_SECURE_PPI] =
1290 map_generic_timer_interrupt(gtdt->secure_el1_interrupt,
1291 gtdt->secure_el1_flags);
1292
1293 arch_timer_ppi[PHYS_NONSECURE_PPI] =
1294 map_generic_timer_interrupt(gtdt->non_secure_el1_interrupt,
1295 gtdt->non_secure_el1_flags);
1296
1297 arch_timer_ppi[VIRT_PPI] =
1298 map_generic_timer_interrupt(gtdt->virtual_timer_interrupt,
1299 gtdt->virtual_timer_flags);
1300
1301 arch_timer_ppi[HYP_PPI] =
1302 map_generic_timer_interrupt(gtdt->non_secure_el2_interrupt,
1303 gtdt->non_secure_el2_flags);
1304
1305 /* Get the frequency from CNTFRQ */
1306 arch_timer_detect_rate(NULL, NULL);
1307
1308 /* Always-on capability */
1309 arch_timer_c3stop = !(gtdt->non_secure_el1_flags & ACPI_GTDT_ALWAYS_ON);
1310
Marc Zyngier5a38bca2017-02-21 14:37:30 +00001311 /* Check for globally applicable workarounds */
1312 arch_timer_check_ool_workaround(ate_match_acpi_oem_info, table);
1313
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001314 arch_timer_init();
1315 return 0;
1316}
Marc Zyngierae281cb2015-09-28 15:49:17 +01001317CLOCKSOURCE_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001318#endif