blob: 14e2419063e93aed0c95532e763f87220b0eaedf [file] [log] [blame]
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001/*
2 * linux/drivers/clocksource/arm_arch_timer.c
3 *
4 * Copyright (C) 2011 ARM Ltd.
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
Marc Zyngierf005bd72016-08-01 10:54:15 +010011
12#define pr_fmt(fmt) "arm_arch_timer: " fmt
13
Mark Rutland8a4da6e2012-11-12 14:33:44 +000014#include <linux/init.h>
15#include <linux/kernel.h>
16#include <linux/device.h>
17#include <linux/smp.h>
18#include <linux/cpu.h>
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +010019#include <linux/cpu_pm.h>
Mark Rutland8a4da6e2012-11-12 14:33:44 +000020#include <linux/clockchips.h>
Richard Cochran7c8f1e72015-01-06 14:26:13 +010021#include <linux/clocksource.h>
Mark Rutland8a4da6e2012-11-12 14:33:44 +000022#include <linux/interrupt.h>
23#include <linux/of_irq.h>
Stephen Boyd22006992013-07-18 16:59:32 -070024#include <linux/of_address.h>
Mark Rutland8a4da6e2012-11-12 14:33:44 +000025#include <linux/io.h>
Stephen Boyd22006992013-07-18 16:59:32 -070026#include <linux/slab.h>
Ingo Molnare6017572017-02-01 16:36:40 +010027#include <linux/sched/clock.h>
Stephen Boyd65cd4f62013-07-18 16:21:18 -070028#include <linux/sched_clock.h>
Hanjun Guob09ca1e2015-03-24 14:02:50 +000029#include <linux/acpi.h>
Mark Rutland8a4da6e2012-11-12 14:33:44 +000030
31#include <asm/arch_timer.h>
Marc Zyngier82668912013-01-10 11:13:07 +000032#include <asm/virt.h>
Mark Rutland8a4da6e2012-11-12 14:33:44 +000033
34#include <clocksource/arm_arch_timer.h>
35
Fu Weided24012017-01-18 21:25:25 +080036#undef pr_fmt
37#define pr_fmt(fmt) "arch_timer: " fmt
38
Stephen Boyd22006992013-07-18 16:59:32 -070039#define CNTTIDR 0x08
40#define CNTTIDR_VIRT(n) (BIT(1) << ((n) * 4))
41
Robin Murphye392d602016-02-01 12:00:48 +000042#define CNTACR(n) (0x40 + ((n) * 4))
43#define CNTACR_RPCT BIT(0)
44#define CNTACR_RVCT BIT(1)
45#define CNTACR_RFRQ BIT(2)
46#define CNTACR_RVOFF BIT(3)
47#define CNTACR_RWVT BIT(4)
48#define CNTACR_RWPT BIT(5)
49
Stephen Boyd22006992013-07-18 16:59:32 -070050#define CNTVCT_LO 0x08
51#define CNTVCT_HI 0x0c
52#define CNTFRQ 0x10
53#define CNTP_TVAL 0x28
54#define CNTP_CTL 0x2c
55#define CNTV_TVAL 0x38
56#define CNTV_CTL 0x3c
57
Stephen Boyd22006992013-07-18 16:59:32 -070058static unsigned arch_timers_present __initdata;
59
60static void __iomem *arch_counter_base;
61
62struct arch_timer {
63 void __iomem *base;
64 struct clock_event_device evt;
65};
66
67#define to_arch_timer(e) container_of(e, struct arch_timer, evt)
68
Mark Rutland8a4da6e2012-11-12 14:33:44 +000069static u32 arch_timer_rate;
Fu Weiee34f1e2017-01-18 21:25:27 +080070static int arch_timer_ppi[ARCH_TIMER_MAX_TIMER_PPI];
Mark Rutland8a4da6e2012-11-12 14:33:44 +000071
72static struct clock_event_device __percpu *arch_timer_evt;
73
Fu Weiee34f1e2017-01-18 21:25:27 +080074static enum arch_timer_ppi_nr arch_timer_uses_ppi = ARCH_TIMER_VIRT_PPI;
Lorenzo Pieralisi82a561942014-04-08 10:04:32 +010075static bool arch_timer_c3stop;
Stephen Boyd22006992013-07-18 16:59:32 -070076static bool arch_timer_mem_use_virtual;
Brian Norrisd8ec7592016-10-04 11:12:09 -070077static bool arch_counter_suspend_stop;
Marc Zyngiera86bd132017-02-01 12:07:15 +000078static bool vdso_default = true;
Mark Rutland8a4da6e2012-11-12 14:33:44 +000079
Will Deacon46fd5c62016-06-27 17:30:13 +010080static bool evtstrm_enable = IS_ENABLED(CONFIG_ARM_ARCH_TIMER_EVTSTREAM);
81
82static int __init early_evtstrm_cfg(char *buf)
83{
84 return strtobool(buf, &evtstrm_enable);
85}
86early_param("clocksource.arm_arch_timer.evtstrm", early_evtstrm_cfg);
87
Mark Rutland8a4da6e2012-11-12 14:33:44 +000088/*
89 * Architected system timer support.
90 */
91
Marc Zyngierf4e00a12017-01-20 18:28:32 +000092static __always_inline
93void arch_timer_reg_write(int access, enum arch_timer_reg reg, u32 val,
94 struct clock_event_device *clk)
95{
96 if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
97 struct arch_timer *timer = to_arch_timer(clk);
98 switch (reg) {
99 case ARCH_TIMER_REG_CTRL:
100 writel_relaxed(val, timer->base + CNTP_CTL);
101 break;
102 case ARCH_TIMER_REG_TVAL:
103 writel_relaxed(val, timer->base + CNTP_TVAL);
104 break;
105 }
106 } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
107 struct arch_timer *timer = to_arch_timer(clk);
108 switch (reg) {
109 case ARCH_TIMER_REG_CTRL:
110 writel_relaxed(val, timer->base + CNTV_CTL);
111 break;
112 case ARCH_TIMER_REG_TVAL:
113 writel_relaxed(val, timer->base + CNTV_TVAL);
114 break;
115 }
116 } else {
117 arch_timer_reg_write_cp15(access, reg, val);
118 }
119}
120
121static __always_inline
122u32 arch_timer_reg_read(int access, enum arch_timer_reg reg,
123 struct clock_event_device *clk)
124{
125 u32 val;
126
127 if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
128 struct arch_timer *timer = to_arch_timer(clk);
129 switch (reg) {
130 case ARCH_TIMER_REG_CTRL:
131 val = readl_relaxed(timer->base + CNTP_CTL);
132 break;
133 case ARCH_TIMER_REG_TVAL:
134 val = readl_relaxed(timer->base + CNTP_TVAL);
135 break;
136 }
137 } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
138 struct arch_timer *timer = to_arch_timer(clk);
139 switch (reg) {
140 case ARCH_TIMER_REG_CTRL:
141 val = readl_relaxed(timer->base + CNTV_CTL);
142 break;
143 case ARCH_TIMER_REG_TVAL:
144 val = readl_relaxed(timer->base + CNTV_TVAL);
145 break;
146 }
147 } else {
148 val = arch_timer_reg_read_cp15(access, reg);
149 }
150
151 return val;
152}
153
Marc Zyngier992dd162017-02-01 11:53:46 +0000154/*
155 * Default to cp15 based access because arm64 uses this function for
156 * sched_clock() before DT is probed and the cp15 method is guaranteed
157 * to exist on arm64. arm doesn't use this before DT is probed so even
158 * if we don't have the cp15 accessors we won't have a problem.
159 */
160u64 (*arch_timer_read_counter)(void) = arch_counter_get_cntvct;
161
162static u64 arch_counter_read(struct clocksource *cs)
163{
164 return arch_timer_read_counter();
165}
166
167static u64 arch_counter_read_cc(const struct cyclecounter *cc)
168{
169 return arch_timer_read_counter();
170}
171
172static struct clocksource clocksource_counter = {
173 .name = "arch_sys_counter",
174 .rating = 400,
175 .read = arch_counter_read,
176 .mask = CLOCKSOURCE_MASK(56),
177 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
178};
179
180static struct cyclecounter cyclecounter __ro_after_init = {
181 .read = arch_counter_read_cc,
182 .mask = CLOCKSOURCE_MASK(56),
183};
184
Marc Zyngier5a38bca2017-02-21 14:37:30 +0000185struct ate_acpi_oem_info {
186 char oem_id[ACPI_OEM_ID_SIZE + 1];
187 char oem_table_id[ACPI_OEM_TABLE_ID_SIZE + 1];
188 u32 oem_revision;
189};
190
Scott Woodf6dc1572016-09-22 03:35:17 -0500191#ifdef CONFIG_FSL_ERRATUM_A008585
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000192/*
193 * The number of retries is an arbitrary value well beyond the highest number
194 * of iterations the loop has been observed to take.
195 */
196#define __fsl_a008585_read_reg(reg) ({ \
197 u64 _old, _new; \
198 int _retries = 200; \
199 \
200 do { \
201 _old = read_sysreg(reg); \
202 _new = read_sysreg(reg); \
203 _retries--; \
204 } while (unlikely(_old != _new) && _retries); \
205 \
206 WARN_ON_ONCE(!_retries); \
207 _new; \
208})
Scott Woodf6dc1572016-09-22 03:35:17 -0500209
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000210static u32 notrace fsl_a008585_read_cntp_tval_el0(void)
Scott Woodf6dc1572016-09-22 03:35:17 -0500211{
212 return __fsl_a008585_read_reg(cntp_tval_el0);
213}
214
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000215static u32 notrace fsl_a008585_read_cntv_tval_el0(void)
Scott Woodf6dc1572016-09-22 03:35:17 -0500216{
217 return __fsl_a008585_read_reg(cntv_tval_el0);
218}
219
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000220static u64 notrace fsl_a008585_read_cntvct_el0(void)
Scott Woodf6dc1572016-09-22 03:35:17 -0500221{
222 return __fsl_a008585_read_reg(cntvct_el0);
223}
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000224#endif
225
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000226#ifdef CONFIG_HISILICON_ERRATUM_161010101
227/*
228 * Verify whether the value of the second read is larger than the first by
229 * less than 32 is the only way to confirm the value is correct, so clear the
230 * lower 5 bits to check whether the difference is greater than 32 or not.
231 * Theoretically the erratum should not occur more than twice in succession
232 * when reading the system counter, but it is possible that some interrupts
233 * may lead to more than twice read errors, triggering the warning, so setting
234 * the number of retries far beyond the number of iterations the loop has been
235 * observed to take.
236 */
237#define __hisi_161010101_read_reg(reg) ({ \
238 u64 _old, _new; \
239 int _retries = 50; \
240 \
241 do { \
242 _old = read_sysreg(reg); \
243 _new = read_sysreg(reg); \
244 _retries--; \
245 } while (unlikely((_new - _old) >> 5) && _retries); \
246 \
247 WARN_ON_ONCE(!_retries); \
248 _new; \
249})
250
251static u32 notrace hisi_161010101_read_cntp_tval_el0(void)
252{
253 return __hisi_161010101_read_reg(cntp_tval_el0);
254}
255
256static u32 notrace hisi_161010101_read_cntv_tval_el0(void)
257{
258 return __hisi_161010101_read_reg(cntv_tval_el0);
259}
260
261static u64 notrace hisi_161010101_read_cntvct_el0(void)
262{
263 return __hisi_161010101_read_reg(cntvct_el0);
264}
Marc Zyngierd003d022017-02-21 15:04:27 +0000265
266static struct ate_acpi_oem_info hisi_161010101_oem_info[] = {
267 /*
268 * Note that trailing spaces are required to properly match
269 * the OEM table information.
270 */
271 {
272 .oem_id = "HISI ",
273 .oem_table_id = "HIP05 ",
274 .oem_revision = 0,
275 },
276 {
277 .oem_id = "HISI ",
278 .oem_table_id = "HIP06 ",
279 .oem_revision = 0,
280 },
281 {
282 .oem_id = "HISI ",
283 .oem_table_id = "HIP07 ",
284 .oem_revision = 0,
285 },
286 { /* Sentinel indicating the end of the OEM array */ },
287};
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000288#endif
289
Marc Zyngierfa8d8152017-01-27 12:52:31 +0000290#ifdef CONFIG_ARM64_ERRATUM_858921
291static u64 notrace arm64_858921_read_cntvct_el0(void)
292{
293 u64 old, new;
294
295 old = read_sysreg(cntvct_el0);
296 new = read_sysreg(cntvct_el0);
297 return (((old ^ new) >> 32) & 1) ? old : new;
298}
299#endif
300
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000301#ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000302DEFINE_PER_CPU(const struct arch_timer_erratum_workaround *,
303 timer_unstable_counter_workaround);
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000304EXPORT_SYMBOL_GPL(timer_unstable_counter_workaround);
305
306DEFINE_STATIC_KEY_FALSE(arch_timer_read_ool_enabled);
307EXPORT_SYMBOL_GPL(arch_timer_read_ool_enabled);
308
Marc Zyngier83280892017-01-27 10:27:09 +0000309static void erratum_set_next_event_tval_generic(const int access, unsigned long evt,
310 struct clock_event_device *clk)
311{
312 unsigned long ctrl;
313 u64 cval = evt + arch_counter_get_cntvct();
314
315 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
316 ctrl |= ARCH_TIMER_CTRL_ENABLE;
317 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
318
319 if (access == ARCH_TIMER_PHYS_ACCESS)
320 write_sysreg(cval, cntp_cval_el0);
321 else
322 write_sysreg(cval, cntv_cval_el0);
323
324 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
325}
326
Arnd Bergmanneb645222017-04-19 19:37:09 +0200327static __maybe_unused int erratum_set_next_event_tval_virt(unsigned long evt,
Marc Zyngier83280892017-01-27 10:27:09 +0000328 struct clock_event_device *clk)
329{
330 erratum_set_next_event_tval_generic(ARCH_TIMER_VIRT_ACCESS, evt, clk);
331 return 0;
332}
333
Arnd Bergmanneb645222017-04-19 19:37:09 +0200334static __maybe_unused int erratum_set_next_event_tval_phys(unsigned long evt,
Marc Zyngier83280892017-01-27 10:27:09 +0000335 struct clock_event_device *clk)
336{
337 erratum_set_next_event_tval_generic(ARCH_TIMER_PHYS_ACCESS, evt, clk);
338 return 0;
339}
340
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000341static const struct arch_timer_erratum_workaround ool_workarounds[] = {
342#ifdef CONFIG_FSL_ERRATUM_A008585
343 {
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000344 .match_type = ate_match_dt,
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000345 .id = "fsl,erratum-a008585",
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000346 .desc = "Freescale erratum a005858",
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000347 .read_cntp_tval_el0 = fsl_a008585_read_cntp_tval_el0,
348 .read_cntv_tval_el0 = fsl_a008585_read_cntv_tval_el0,
349 .read_cntvct_el0 = fsl_a008585_read_cntvct_el0,
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000350 .set_next_event_phys = erratum_set_next_event_tval_phys,
351 .set_next_event_virt = erratum_set_next_event_tval_virt,
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000352 },
353#endif
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000354#ifdef CONFIG_HISILICON_ERRATUM_161010101
355 {
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000356 .match_type = ate_match_dt,
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000357 .id = "hisilicon,erratum-161010101",
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000358 .desc = "HiSilicon erratum 161010101",
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000359 .read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
360 .read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
361 .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000362 .set_next_event_phys = erratum_set_next_event_tval_phys,
363 .set_next_event_virt = erratum_set_next_event_tval_virt,
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000364 },
Marc Zyngierd003d022017-02-21 15:04:27 +0000365 {
366 .match_type = ate_match_acpi_oem_info,
367 .id = hisi_161010101_oem_info,
368 .desc = "HiSilicon erratum 161010101",
369 .read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
370 .read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
371 .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
372 .set_next_event_phys = erratum_set_next_event_tval_phys,
373 .set_next_event_virt = erratum_set_next_event_tval_virt,
374 },
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000375#endif
Marc Zyngierfa8d8152017-01-27 12:52:31 +0000376#ifdef CONFIG_ARM64_ERRATUM_858921
377 {
378 .match_type = ate_match_local_cap_id,
379 .id = (void *)ARM64_WORKAROUND_858921,
380 .desc = "ARM erratum 858921",
381 .read_cntvct_el0 = arm64_858921_read_cntvct_el0,
382 },
383#endif
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000384};
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000385
386typedef bool (*ate_match_fn_t)(const struct arch_timer_erratum_workaround *,
387 const void *);
388
389static
390bool arch_timer_check_dt_erratum(const struct arch_timer_erratum_workaround *wa,
391 const void *arg)
392{
393 const struct device_node *np = arg;
394
395 return of_property_read_bool(np, wa->id);
396}
397
Marc Zyngier00640302017-03-20 16:47:59 +0000398static
399bool arch_timer_check_local_cap_erratum(const struct arch_timer_erratum_workaround *wa,
400 const void *arg)
401{
402 return this_cpu_has_cap((uintptr_t)wa->id);
403}
404
Marc Zyngier5a38bca2017-02-21 14:37:30 +0000405
406static
407bool arch_timer_check_acpi_oem_erratum(const struct arch_timer_erratum_workaround *wa,
408 const void *arg)
409{
410 static const struct ate_acpi_oem_info empty_oem_info = {};
411 const struct ate_acpi_oem_info *info = wa->id;
412 const struct acpi_table_header *table = arg;
413
414 /* Iterate over the ACPI OEM info array, looking for a match */
415 while (memcmp(info, &empty_oem_info, sizeof(*info))) {
416 if (!memcmp(info->oem_id, table->oem_id, ACPI_OEM_ID_SIZE) &&
417 !memcmp(info->oem_table_id, table->oem_table_id, ACPI_OEM_TABLE_ID_SIZE) &&
418 info->oem_revision == table->oem_revision)
419 return true;
420
421 info++;
422 }
423
424 return false;
425}
426
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000427static const struct arch_timer_erratum_workaround *
428arch_timer_iterate_errata(enum arch_timer_erratum_match_type type,
429 ate_match_fn_t match_fn,
430 void *arg)
431{
432 int i;
433
434 for (i = 0; i < ARRAY_SIZE(ool_workarounds); i++) {
435 if (ool_workarounds[i].match_type != type)
436 continue;
437
438 if (match_fn(&ool_workarounds[i], arg))
439 return &ool_workarounds[i];
440 }
441
442 return NULL;
443}
444
445static
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000446void arch_timer_enable_workaround(const struct arch_timer_erratum_workaround *wa,
447 bool local)
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000448{
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000449 int i;
450
451 if (local) {
452 __this_cpu_write(timer_unstable_counter_workaround, wa);
453 } else {
454 for_each_possible_cpu(i)
455 per_cpu(timer_unstable_counter_workaround, i) = wa;
456 }
457
Marc Zyngier450f9682017-08-01 09:02:57 +0100458 /*
459 * Use the locked version, as we're called from the CPU
460 * hotplug framework. Otherwise, we end-up in deadlock-land.
461 */
462 static_branch_enable_cpuslocked(&arch_timer_read_ool_enabled);
Marc Zyngiera86bd132017-02-01 12:07:15 +0000463
464 /*
465 * Don't use the vdso fastpath if errata require using the
466 * out-of-line counter accessor. We may change our mind pretty
467 * late in the game (with a per-CPU erratum, for example), so
468 * change both the default value and the vdso itself.
469 */
470 if (wa->read_cntvct_el0) {
471 clocksource_counter.archdata.vdso_direct = false;
472 vdso_default = false;
473 }
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000474}
475
476static void arch_timer_check_ool_workaround(enum arch_timer_erratum_match_type type,
477 void *arg)
478{
479 const struct arch_timer_erratum_workaround *wa;
480 ate_match_fn_t match_fn = NULL;
Marc Zyngier00640302017-03-20 16:47:59 +0000481 bool local = false;
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000482
483 switch (type) {
484 case ate_match_dt:
485 match_fn = arch_timer_check_dt_erratum;
486 break;
Marc Zyngier00640302017-03-20 16:47:59 +0000487 case ate_match_local_cap_id:
488 match_fn = arch_timer_check_local_cap_erratum;
489 local = true;
490 break;
Marc Zyngier5a38bca2017-02-21 14:37:30 +0000491 case ate_match_acpi_oem_info:
492 match_fn = arch_timer_check_acpi_oem_erratum;
493 break;
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000494 default:
495 WARN_ON(1);
496 return;
497 }
498
499 wa = arch_timer_iterate_errata(type, match_fn, arg);
500 if (!wa)
501 return;
502
Marc Zyngier00640302017-03-20 16:47:59 +0000503 if (needs_unstable_timer_counter_workaround()) {
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000504 const struct arch_timer_erratum_workaround *__wa;
505 __wa = __this_cpu_read(timer_unstable_counter_workaround);
506 if (__wa && wa != __wa)
Marc Zyngier00640302017-03-20 16:47:59 +0000507 pr_warn("Can't enable workaround for %s (clashes with %s\n)",
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000508 wa->desc, __wa->desc);
509
510 if (__wa)
511 return;
Marc Zyngier00640302017-03-20 16:47:59 +0000512 }
513
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000514 arch_timer_enable_workaround(wa, local);
Marc Zyngier00640302017-03-20 16:47:59 +0000515 pr_info("Enabling %s workaround for %s\n",
516 local ? "local" : "global", wa->desc);
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000517}
518
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000519#define erratum_handler(fn, r, ...) \
520({ \
521 bool __val; \
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000522 if (needs_unstable_timer_counter_workaround()) { \
523 const struct arch_timer_erratum_workaround *__wa; \
524 __wa = __this_cpu_read(timer_unstable_counter_workaround); \
525 if (__wa && __wa->fn) { \
526 r = __wa->fn(__VA_ARGS__); \
527 __val = true; \
528 } else { \
529 __val = false; \
530 } \
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000531 } else { \
532 __val = false; \
533 } \
534 __val; \
535})
536
Marc Zyngiera86bd132017-02-01 12:07:15 +0000537static bool arch_timer_this_cpu_has_cntvct_wa(void)
538{
539 const struct arch_timer_erratum_workaround *wa;
540
541 wa = __this_cpu_read(timer_unstable_counter_workaround);
542 return wa && wa->read_cntvct_el0;
543}
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000544#else
545#define arch_timer_check_ool_workaround(t,a) do { } while(0)
Marc Zyngier83280892017-01-27 10:27:09 +0000546#define erratum_set_next_event_tval_virt(...) ({BUG(); 0;})
547#define erratum_set_next_event_tval_phys(...) ({BUG(); 0;})
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000548#define erratum_handler(fn, r, ...) ({false;})
Marc Zyngiera86bd132017-02-01 12:07:15 +0000549#define arch_timer_this_cpu_has_cntvct_wa() ({false;})
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000550#endif /* CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND */
Scott Woodf6dc1572016-09-22 03:35:17 -0500551
Stephen Boyde09f3cc2013-07-18 16:59:28 -0700552static __always_inline irqreturn_t timer_handler(const int access,
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000553 struct clock_event_device *evt)
554{
555 unsigned long ctrl;
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200556
Stephen Boyd60faddf2013-07-18 16:59:31 -0700557 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000558 if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
559 ctrl |= ARCH_TIMER_CTRL_IT_MASK;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700560 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000561 evt->event_handler(evt);
562 return IRQ_HANDLED;
563 }
564
565 return IRQ_NONE;
566}
567
568static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
569{
570 struct clock_event_device *evt = dev_id;
571
572 return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
573}
574
575static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
576{
577 struct clock_event_device *evt = dev_id;
578
579 return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
580}
581
Stephen Boyd22006992013-07-18 16:59:32 -0700582static irqreturn_t arch_timer_handler_phys_mem(int irq, void *dev_id)
583{
584 struct clock_event_device *evt = dev_id;
585
586 return timer_handler(ARCH_TIMER_MEM_PHYS_ACCESS, evt);
587}
588
589static irqreturn_t arch_timer_handler_virt_mem(int irq, void *dev_id)
590{
591 struct clock_event_device *evt = dev_id;
592
593 return timer_handler(ARCH_TIMER_MEM_VIRT_ACCESS, evt);
594}
595
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530596static __always_inline int timer_shutdown(const int access,
597 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000598{
599 unsigned long ctrl;
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530600
601 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
602 ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
603 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
604
605 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000606}
607
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530608static int arch_timer_shutdown_virt(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000609{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530610 return timer_shutdown(ARCH_TIMER_VIRT_ACCESS, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000611}
612
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530613static int arch_timer_shutdown_phys(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000614{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530615 return timer_shutdown(ARCH_TIMER_PHYS_ACCESS, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000616}
617
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530618static int arch_timer_shutdown_virt_mem(struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700619{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530620 return timer_shutdown(ARCH_TIMER_MEM_VIRT_ACCESS, clk);
Stephen Boyd22006992013-07-18 16:59:32 -0700621}
622
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530623static int arch_timer_shutdown_phys_mem(struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700624{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530625 return timer_shutdown(ARCH_TIMER_MEM_PHYS_ACCESS, clk);
Stephen Boyd22006992013-07-18 16:59:32 -0700626}
627
Stephen Boyd60faddf2013-07-18 16:59:31 -0700628static __always_inline void set_next_event(const int access, unsigned long evt,
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200629 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000630{
631 unsigned long ctrl;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700632 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000633 ctrl |= ARCH_TIMER_CTRL_ENABLE;
634 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700635 arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt, clk);
636 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000637}
638
639static int arch_timer_set_next_event_virt(unsigned long evt,
Stephen Boyd60faddf2013-07-18 16:59:31 -0700640 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000641{
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000642 int ret;
643
644 if (erratum_handler(set_next_event_virt, ret, evt, clk))
645 return ret;
Marc Zyngier83280892017-01-27 10:27:09 +0000646
Stephen Boyd60faddf2013-07-18 16:59:31 -0700647 set_next_event(ARCH_TIMER_VIRT_ACCESS, evt, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000648 return 0;
649}
650
651static int arch_timer_set_next_event_phys(unsigned long evt,
Stephen Boyd60faddf2013-07-18 16:59:31 -0700652 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000653{
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000654 int ret;
655
656 if (erratum_handler(set_next_event_phys, ret, evt, clk))
657 return ret;
Marc Zyngier83280892017-01-27 10:27:09 +0000658
Stephen Boyd60faddf2013-07-18 16:59:31 -0700659 set_next_event(ARCH_TIMER_PHYS_ACCESS, evt, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000660 return 0;
661}
662
Stephen Boyd22006992013-07-18 16:59:32 -0700663static int arch_timer_set_next_event_virt_mem(unsigned long evt,
664 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000665{
Stephen Boyd22006992013-07-18 16:59:32 -0700666 set_next_event(ARCH_TIMER_MEM_VIRT_ACCESS, evt, clk);
667 return 0;
668}
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000669
Stephen Boyd22006992013-07-18 16:59:32 -0700670static int arch_timer_set_next_event_phys_mem(unsigned long evt,
671 struct clock_event_device *clk)
672{
673 set_next_event(ARCH_TIMER_MEM_PHYS_ACCESS, evt, clk);
674 return 0;
675}
676
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200677static void __arch_timer_setup(unsigned type,
678 struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700679{
680 clk->features = CLOCK_EVT_FEAT_ONESHOT;
681
Fu Wei8a5c21d2017-01-18 21:25:26 +0800682 if (type == ARCH_TIMER_TYPE_CP15) {
Lorenzo Pieralisi82a561942014-04-08 10:04:32 +0100683 if (arch_timer_c3stop)
684 clk->features |= CLOCK_EVT_FEAT_C3STOP;
Stephen Boyd22006992013-07-18 16:59:32 -0700685 clk->name = "arch_sys_timer";
686 clk->rating = 450;
687 clk->cpumask = cpumask_of(smp_processor_id());
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000688 clk->irq = arch_timer_ppi[arch_timer_uses_ppi];
689 switch (arch_timer_uses_ppi) {
Fu Weiee34f1e2017-01-18 21:25:27 +0800690 case ARCH_TIMER_VIRT_PPI:
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530691 clk->set_state_shutdown = arch_timer_shutdown_virt;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530692 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt;
Stephen Boyd22006992013-07-18 16:59:32 -0700693 clk->set_next_event = arch_timer_set_next_event_virt;
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000694 break;
Fu Weiee34f1e2017-01-18 21:25:27 +0800695 case ARCH_TIMER_PHYS_SECURE_PPI:
696 case ARCH_TIMER_PHYS_NONSECURE_PPI:
697 case ARCH_TIMER_HYP_PPI:
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530698 clk->set_state_shutdown = arch_timer_shutdown_phys;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530699 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys;
Stephen Boyd22006992013-07-18 16:59:32 -0700700 clk->set_next_event = arch_timer_set_next_event_phys;
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000701 break;
702 default:
703 BUG();
Stephen Boyd22006992013-07-18 16:59:32 -0700704 }
Scott Woodf6dc1572016-09-22 03:35:17 -0500705
Marc Zyngier00640302017-03-20 16:47:59 +0000706 arch_timer_check_ool_workaround(ate_match_local_cap_id, NULL);
Stephen Boyd22006992013-07-18 16:59:32 -0700707 } else {
Stephen Boyd7b52ad22014-01-06 14:56:17 -0800708 clk->features |= CLOCK_EVT_FEAT_DYNIRQ;
Stephen Boyd22006992013-07-18 16:59:32 -0700709 clk->name = "arch_mem_timer";
710 clk->rating = 400;
711 clk->cpumask = cpu_all_mask;
712 if (arch_timer_mem_use_virtual) {
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530713 clk->set_state_shutdown = arch_timer_shutdown_virt_mem;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530714 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt_mem;
Stephen Boyd22006992013-07-18 16:59:32 -0700715 clk->set_next_event =
716 arch_timer_set_next_event_virt_mem;
717 } else {
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530718 clk->set_state_shutdown = arch_timer_shutdown_phys_mem;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530719 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys_mem;
Stephen Boyd22006992013-07-18 16:59:32 -0700720 clk->set_next_event =
721 arch_timer_set_next_event_phys_mem;
722 }
723 }
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000724
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530725 clk->set_state_shutdown(clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000726
Stephen Boyd22006992013-07-18 16:59:32 -0700727 clockevents_config_and_register(clk, arch_timer_rate, 0xf, 0x7fffffff);
728}
729
Nathan Lynche1ce5c72014-09-29 01:50:06 +0200730static void arch_timer_evtstrm_enable(int divider)
731{
732 u32 cntkctl = arch_timer_get_cntkctl();
733
734 cntkctl &= ~ARCH_TIMER_EVT_TRIGGER_MASK;
735 /* Set the divider and enable virtual event stream */
736 cntkctl |= (divider << ARCH_TIMER_EVT_TRIGGER_SHIFT)
737 | ARCH_TIMER_VIRT_EVT_EN;
738 arch_timer_set_cntkctl(cntkctl);
739 elf_hwcap |= HWCAP_EVTSTRM;
740#ifdef CONFIG_COMPAT
741 compat_elf_hwcap |= COMPAT_HWCAP_EVTSTRM;
742#endif
743}
744
Will Deacon037f6372013-08-23 15:32:29 +0100745static void arch_timer_configure_evtstream(void)
746{
747 int evt_stream_div, pos;
748
749 /* Find the closest power of two to the divisor */
750 evt_stream_div = arch_timer_rate / ARCH_TIMER_EVT_STREAM_FREQ;
751 pos = fls(evt_stream_div);
752 if (pos > 1 && !(evt_stream_div & (1 << (pos - 2))))
753 pos--;
754 /* enable event stream */
755 arch_timer_evtstrm_enable(min(pos, 15));
756}
757
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200758static void arch_counter_set_user_access(void)
759{
760 u32 cntkctl = arch_timer_get_cntkctl();
761
Marc Zyngiera86bd132017-02-01 12:07:15 +0000762 /* Disable user access to the timers and both counters */
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200763 /* Also disable virtual event stream */
764 cntkctl &= ~(ARCH_TIMER_USR_PT_ACCESS_EN
765 | ARCH_TIMER_USR_VT_ACCESS_EN
Marc Zyngiera86bd132017-02-01 12:07:15 +0000766 | ARCH_TIMER_USR_VCT_ACCESS_EN
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200767 | ARCH_TIMER_VIRT_EVT_EN
768 | ARCH_TIMER_USR_PCT_ACCESS_EN);
769
Marc Zyngiera86bd132017-02-01 12:07:15 +0000770 /*
771 * Enable user access to the virtual counter if it doesn't
772 * need to be workaround. The vdso may have been already
773 * disabled though.
774 */
775 if (arch_timer_this_cpu_has_cntvct_wa())
776 pr_info("CPU%d: Trapping CNTVCT access\n", smp_processor_id());
777 else
778 cntkctl |= ARCH_TIMER_USR_VCT_ACCESS_EN;
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200779
780 arch_timer_set_cntkctl(cntkctl);
781}
782
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000783static bool arch_timer_has_nonsecure_ppi(void)
784{
Fu Weiee34f1e2017-01-18 21:25:27 +0800785 return (arch_timer_uses_ppi == ARCH_TIMER_PHYS_SECURE_PPI &&
786 arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000787}
788
Marc Zyngierf005bd72016-08-01 10:54:15 +0100789static u32 check_ppi_trigger(int irq)
790{
791 u32 flags = irq_get_trigger_type(irq);
792
793 if (flags != IRQF_TRIGGER_HIGH && flags != IRQF_TRIGGER_LOW) {
794 pr_warn("WARNING: Invalid trigger for IRQ%d, assuming level low\n", irq);
795 pr_warn("WARNING: Please fix your firmware\n");
796 flags = IRQF_TRIGGER_LOW;
797 }
798
799 return flags;
800}
801
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000802static int arch_timer_starting_cpu(unsigned int cpu)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000803{
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000804 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
Marc Zyngierf005bd72016-08-01 10:54:15 +0100805 u32 flags;
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000806
Fu Wei8a5c21d2017-01-18 21:25:26 +0800807 __arch_timer_setup(ARCH_TIMER_TYPE_CP15, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000808
Marc Zyngierf005bd72016-08-01 10:54:15 +0100809 flags = check_ppi_trigger(arch_timer_ppi[arch_timer_uses_ppi]);
810 enable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], flags);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000811
Marc Zyngierf005bd72016-08-01 10:54:15 +0100812 if (arch_timer_has_nonsecure_ppi()) {
Fu Weiee34f1e2017-01-18 21:25:27 +0800813 flags = check_ppi_trigger(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
814 enable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
815 flags);
Marc Zyngierf005bd72016-08-01 10:54:15 +0100816 }
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000817
818 arch_counter_set_user_access();
Will Deacon46fd5c62016-06-27 17:30:13 +0100819 if (evtstrm_enable)
Will Deacon037f6372013-08-23 15:32:29 +0100820 arch_timer_configure_evtstream();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000821
822 return 0;
823}
824
Fu Wei5d3dfa92017-03-22 00:31:13 +0800825/*
826 * For historical reasons, when probing with DT we use whichever (non-zero)
827 * rate was probed first, and don't verify that others match. If the first node
828 * probed has a clock-frequency property, this overrides the HW register.
829 */
830static void arch_timer_of_configure_rate(u32 rate, struct device_node *np)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000831{
Stephen Boyd22006992013-07-18 16:59:32 -0700832 /* Who has more than one independent system counter? */
833 if (arch_timer_rate)
834 return;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000835
Fu Wei5d3dfa92017-03-22 00:31:13 +0800836 if (of_property_read_u32(np, "clock-frequency", &arch_timer_rate))
837 arch_timer_rate = rate;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000838
Stephen Boyd22006992013-07-18 16:59:32 -0700839 /* Check the timer frequency. */
840 if (arch_timer_rate == 0)
Fu Weided24012017-01-18 21:25:25 +0800841 pr_warn("frequency not available\n");
Stephen Boyd22006992013-07-18 16:59:32 -0700842}
843
844static void arch_timer_banner(unsigned type)
845{
Fu Weided24012017-01-18 21:25:25 +0800846 pr_info("%s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
Fu Wei8a5c21d2017-01-18 21:25:26 +0800847 type & ARCH_TIMER_TYPE_CP15 ? "cp15" : "",
848 type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ?
849 " and " : "",
850 type & ARCH_TIMER_TYPE_MEM ? "mmio" : "",
Fu Weided24012017-01-18 21:25:25 +0800851 (unsigned long)arch_timer_rate / 1000000,
852 (unsigned long)(arch_timer_rate / 10000) % 100,
Fu Wei8a5c21d2017-01-18 21:25:26 +0800853 type & ARCH_TIMER_TYPE_CP15 ?
Fu Weiee34f1e2017-01-18 21:25:27 +0800854 (arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI) ? "virt" : "phys" :
Stephen Boyd22006992013-07-18 16:59:32 -0700855 "",
Fu Wei8a5c21d2017-01-18 21:25:26 +0800856 type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ? "/" : "",
857 type & ARCH_TIMER_TYPE_MEM ?
Stephen Boyd22006992013-07-18 16:59:32 -0700858 arch_timer_mem_use_virtual ? "virt" : "phys" :
859 "");
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000860}
861
862u32 arch_timer_get_rate(void)
863{
864 return arch_timer_rate;
865}
866
Stephen Boyd22006992013-07-18 16:59:32 -0700867static u64 arch_counter_get_cntvct_mem(void)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000868{
Stephen Boyd22006992013-07-18 16:59:32 -0700869 u32 vct_lo, vct_hi, tmp_hi;
870
871 do {
872 vct_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
873 vct_lo = readl_relaxed(arch_counter_base + CNTVCT_LO);
874 tmp_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
875 } while (vct_hi != tmp_hi);
876
877 return ((u64) vct_hi << 32) | vct_lo;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000878}
879
Julien Grallb4d6ce92016-04-11 16:32:51 +0100880static struct arch_timer_kvm_info arch_timer_kvm_info;
881
882struct arch_timer_kvm_info *arch_timer_get_kvm_info(void)
883{
884 return &arch_timer_kvm_info;
885}
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000886
Stephen Boyd22006992013-07-18 16:59:32 -0700887static void __init arch_counter_register(unsigned type)
888{
889 u64 start_count;
890
891 /* Register the CP15 based counter if we have one */
Fu Wei8a5c21d2017-01-18 21:25:26 +0800892 if (type & ARCH_TIMER_TYPE_CP15) {
Fu Weiee34f1e2017-01-18 21:25:27 +0800893 if (IS_ENABLED(CONFIG_ARM64) ||
894 arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI)
Sonny Rao0b46b8a2014-11-23 23:02:44 -0800895 arch_timer_read_counter = arch_counter_get_cntvct;
896 else
897 arch_timer_read_counter = arch_counter_get_cntpct;
Scott Woodf6dc1572016-09-22 03:35:17 -0500898
Marc Zyngiera86bd132017-02-01 12:07:15 +0000899 clocksource_counter.archdata.vdso_direct = vdso_default;
Nathan Lynch423bd692014-09-29 01:50:06 +0200900 } else {
Stephen Boyd22006992013-07-18 16:59:32 -0700901 arch_timer_read_counter = arch_counter_get_cntvct_mem;
Nathan Lynch423bd692014-09-29 01:50:06 +0200902 }
903
Brian Norrisd8ec7592016-10-04 11:12:09 -0700904 if (!arch_counter_suspend_stop)
905 clocksource_counter.flags |= CLOCK_SOURCE_SUSPEND_NONSTOP;
Stephen Boyd22006992013-07-18 16:59:32 -0700906 start_count = arch_timer_read_counter();
907 clocksource_register_hz(&clocksource_counter, arch_timer_rate);
908 cyclecounter.mult = clocksource_counter.mult;
909 cyclecounter.shift = clocksource_counter.shift;
Julien Grallb4d6ce92016-04-11 16:32:51 +0100910 timecounter_init(&arch_timer_kvm_info.timecounter,
911 &cyclecounter, start_count);
Thierry Reding4a7d3e82013-10-15 15:31:51 +0200912
913 /* 56 bits minimum, so we assume worst case rollover */
914 sched_clock_register(arch_timer_read_counter, 56, arch_timer_rate);
Stephen Boyd22006992013-07-18 16:59:32 -0700915}
916
Paul Gortmaker8c37bb32013-06-19 11:32:08 -0400917static void arch_timer_stop(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000918{
Fu Weided24012017-01-18 21:25:25 +0800919 pr_debug("disable IRQ%d cpu #%d\n", clk->irq, smp_processor_id());
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000920
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000921 disable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi]);
922 if (arch_timer_has_nonsecure_ppi())
Fu Weiee34f1e2017-01-18 21:25:27 +0800923 disable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000924
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530925 clk->set_state_shutdown(clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000926}
927
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000928static int arch_timer_dying_cpu(unsigned int cpu)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000929{
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000930 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000931
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000932 arch_timer_stop(clk);
933 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000934}
935
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100936#ifdef CONFIG_CPU_PM
Marc Zyngierbee67c52017-04-04 17:05:16 +0100937static DEFINE_PER_CPU(unsigned long, saved_cntkctl);
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100938static int arch_timer_cpu_pm_notify(struct notifier_block *self,
939 unsigned long action, void *hcpu)
940{
941 if (action == CPU_PM_ENTER)
Marc Zyngierbee67c52017-04-04 17:05:16 +0100942 __this_cpu_write(saved_cntkctl, arch_timer_get_cntkctl());
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100943 else if (action == CPU_PM_ENTER_FAILED || action == CPU_PM_EXIT)
Marc Zyngierbee67c52017-04-04 17:05:16 +0100944 arch_timer_set_cntkctl(__this_cpu_read(saved_cntkctl));
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100945 return NOTIFY_OK;
946}
947
948static struct notifier_block arch_timer_cpu_pm_notifier = {
949 .notifier_call = arch_timer_cpu_pm_notify,
950};
951
952static int __init arch_timer_cpu_pm_init(void)
953{
954 return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier);
955}
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000956
957static void __init arch_timer_cpu_pm_deinit(void)
958{
959 WARN_ON(cpu_pm_unregister_notifier(&arch_timer_cpu_pm_notifier));
960}
961
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100962#else
963static int __init arch_timer_cpu_pm_init(void)
964{
965 return 0;
966}
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000967
968static void __init arch_timer_cpu_pm_deinit(void)
969{
970}
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100971#endif
972
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000973static int __init arch_timer_register(void)
974{
975 int err;
976 int ppi;
977
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000978 arch_timer_evt = alloc_percpu(struct clock_event_device);
979 if (!arch_timer_evt) {
980 err = -ENOMEM;
981 goto out;
982 }
983
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000984 ppi = arch_timer_ppi[arch_timer_uses_ppi];
985 switch (arch_timer_uses_ppi) {
Fu Weiee34f1e2017-01-18 21:25:27 +0800986 case ARCH_TIMER_VIRT_PPI:
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000987 err = request_percpu_irq(ppi, arch_timer_handler_virt,
988 "arch_timer", arch_timer_evt);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000989 break;
Fu Weiee34f1e2017-01-18 21:25:27 +0800990 case ARCH_TIMER_PHYS_SECURE_PPI:
991 case ARCH_TIMER_PHYS_NONSECURE_PPI:
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000992 err = request_percpu_irq(ppi, arch_timer_handler_phys,
993 "arch_timer", arch_timer_evt);
Fu Wei4502b6b2017-01-18 21:25:30 +0800994 if (!err && arch_timer_has_nonsecure_ppi()) {
Fu Weiee34f1e2017-01-18 21:25:27 +0800995 ppi = arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI];
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000996 err = request_percpu_irq(ppi, arch_timer_handler_phys,
997 "arch_timer", arch_timer_evt);
998 if (err)
Fu Weiee34f1e2017-01-18 21:25:27 +0800999 free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_SECURE_PPI],
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001000 arch_timer_evt);
1001 }
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001002 break;
Fu Weiee34f1e2017-01-18 21:25:27 +08001003 case ARCH_TIMER_HYP_PPI:
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001004 err = request_percpu_irq(ppi, arch_timer_handler_phys,
1005 "arch_timer", arch_timer_evt);
1006 break;
1007 default:
1008 BUG();
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001009 }
1010
1011 if (err) {
Fu Weided24012017-01-18 21:25:25 +08001012 pr_err("can't register interrupt %d (%d)\n", ppi, err);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001013 goto out_free;
1014 }
1015
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001016 err = arch_timer_cpu_pm_init();
1017 if (err)
1018 goto out_unreg_notify;
1019
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001020
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001021 /* Register and immediately configure the timer on the boot CPU */
1022 err = cpuhp_setup_state(CPUHP_AP_ARM_ARCH_TIMER_STARTING,
Thomas Gleixner73c1b412016-12-21 20:19:54 +01001023 "clockevents/arm/arch_timer:starting",
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001024 arch_timer_starting_cpu, arch_timer_dying_cpu);
1025 if (err)
1026 goto out_unreg_cpupm;
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001027 return 0;
1028
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001029out_unreg_cpupm:
1030 arch_timer_cpu_pm_deinit();
1031
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001032out_unreg_notify:
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001033 free_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], arch_timer_evt);
1034 if (arch_timer_has_nonsecure_ppi())
Fu Weiee34f1e2017-01-18 21:25:27 +08001035 free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001036 arch_timer_evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001037
1038out_free:
1039 free_percpu(arch_timer_evt);
1040out:
1041 return err;
1042}
1043
Stephen Boyd22006992013-07-18 16:59:32 -07001044static int __init arch_timer_mem_register(void __iomem *base, unsigned int irq)
1045{
1046 int ret;
1047 irq_handler_t func;
1048 struct arch_timer *t;
1049
1050 t = kzalloc(sizeof(*t), GFP_KERNEL);
1051 if (!t)
1052 return -ENOMEM;
1053
1054 t->base = base;
1055 t->evt.irq = irq;
Fu Wei8a5c21d2017-01-18 21:25:26 +08001056 __arch_timer_setup(ARCH_TIMER_TYPE_MEM, &t->evt);
Stephen Boyd22006992013-07-18 16:59:32 -07001057
1058 if (arch_timer_mem_use_virtual)
1059 func = arch_timer_handler_virt_mem;
1060 else
1061 func = arch_timer_handler_phys_mem;
1062
1063 ret = request_irq(irq, func, IRQF_TIMER, "arch_mem_timer", &t->evt);
1064 if (ret) {
Fu Weided24012017-01-18 21:25:25 +08001065 pr_err("Failed to request mem timer irq\n");
Stephen Boyd22006992013-07-18 16:59:32 -07001066 kfree(t);
1067 }
1068
1069 return ret;
1070}
1071
1072static const struct of_device_id arch_timer_of_match[] __initconst = {
1073 { .compatible = "arm,armv7-timer", },
1074 { .compatible = "arm,armv8-timer", },
1075 {},
1076};
1077
1078static const struct of_device_id arch_timer_mem_of_match[] __initconst = {
1079 { .compatible = "arm,armv7-timer-mem", },
1080 {},
1081};
1082
Fu Wei13bf6992017-03-22 00:31:14 +08001083static bool __init arch_timer_needs_of_probing(void)
Sudeep Hollac387f072014-09-29 01:50:05 +02001084{
1085 struct device_node *dn;
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001086 bool needs_probing = false;
Fu Wei13bf6992017-03-22 00:31:14 +08001087 unsigned int mask = ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM;
Sudeep Hollac387f072014-09-29 01:50:05 +02001088
Fu Wei13bf6992017-03-22 00:31:14 +08001089 /* We have two timers, and both device-tree nodes are probed. */
1090 if ((arch_timers_present & mask) == mask)
1091 return false;
1092
1093 /*
1094 * Only one type of timer is probed,
1095 * check if we have another type of timer node in device-tree.
1096 */
1097 if (arch_timers_present & ARCH_TIMER_TYPE_CP15)
1098 dn = of_find_matching_node(NULL, arch_timer_mem_of_match);
1099 else
1100 dn = of_find_matching_node(NULL, arch_timer_of_match);
1101
1102 if (dn && of_device_is_available(dn))
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001103 needs_probing = true;
Fu Wei13bf6992017-03-22 00:31:14 +08001104
Sudeep Hollac387f072014-09-29 01:50:05 +02001105 of_node_put(dn);
1106
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001107 return needs_probing;
Sudeep Hollac387f072014-09-29 01:50:05 +02001108}
1109
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001110static int __init arch_timer_common_init(void)
Stephen Boyd22006992013-07-18 16:59:32 -07001111{
Stephen Boyd22006992013-07-18 16:59:32 -07001112 arch_timer_banner(arch_timers_present);
1113 arch_counter_register(arch_timers_present);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001114 return arch_timer_arch_init();
Stephen Boyd22006992013-07-18 16:59:32 -07001115}
1116
Fu Wei4502b6b2017-01-18 21:25:30 +08001117/**
1118 * arch_timer_select_ppi() - Select suitable PPI for the current system.
1119 *
1120 * If HYP mode is available, we know that the physical timer
1121 * has been configured to be accessible from PL1. Use it, so
1122 * that a guest can use the virtual timer instead.
1123 *
1124 * On ARMv8.1 with VH extensions, the kernel runs in HYP. VHE
1125 * accesses to CNTP_*_EL1 registers are silently redirected to
1126 * their CNTHP_*_EL2 counterparts, and use a different PPI
1127 * number.
1128 *
1129 * If no interrupt provided for virtual timer, we'll have to
1130 * stick to the physical timer. It'd better be accessible...
1131 * For arm64 we never use the secure interrupt.
1132 *
1133 * Return: a suitable PPI type for the current system.
1134 */
1135static enum arch_timer_ppi_nr __init arch_timer_select_ppi(void)
1136{
1137 if (is_kernel_in_hyp_mode())
1138 return ARCH_TIMER_HYP_PPI;
1139
1140 if (!is_hyp_mode_available() && arch_timer_ppi[ARCH_TIMER_VIRT_PPI])
1141 return ARCH_TIMER_VIRT_PPI;
1142
1143 if (IS_ENABLED(CONFIG_ARM64))
1144 return ARCH_TIMER_PHYS_NONSECURE_PPI;
1145
1146 return ARCH_TIMER_PHYS_SECURE_PPI;
1147}
1148
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001149static int __init arch_timer_of_init(struct device_node *np)
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001150{
Fu Weica0e1b52017-03-22 00:31:15 +08001151 int i, ret;
Fu Wei5d3dfa92017-03-22 00:31:13 +08001152 u32 rate;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001153
Fu Wei8a5c21d2017-01-18 21:25:26 +08001154 if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
Fu Weided24012017-01-18 21:25:25 +08001155 pr_warn("multiple nodes in dt, skipping\n");
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001156 return 0;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001157 }
1158
Fu Wei8a5c21d2017-01-18 21:25:26 +08001159 arch_timers_present |= ARCH_TIMER_TYPE_CP15;
Fu Weiee34f1e2017-01-18 21:25:27 +08001160 for (i = ARCH_TIMER_PHYS_SECURE_PPI; i < ARCH_TIMER_MAX_TIMER_PPI; i++)
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001161 arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
1162
Fu Weica0e1b52017-03-22 00:31:15 +08001163 arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI];
1164
Fu Weic389d702017-04-01 01:51:00 +08001165 rate = arch_timer_get_cntfrq();
Fu Wei5d3dfa92017-03-22 00:31:13 +08001166 arch_timer_of_configure_rate(rate, np);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001167
1168 arch_timer_c3stop = !of_property_read_bool(np, "always-on");
1169
Marc Zyngier651bb2e2017-01-19 17:20:59 +00001170 /* Check for globally applicable workarounds */
1171 arch_timer_check_ool_workaround(ate_match_dt, np);
Scott Woodf6dc1572016-09-22 03:35:17 -05001172
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001173 /*
1174 * If we cannot rely on firmware initializing the timer registers then
1175 * we should use the physical timers instead.
1176 */
1177 if (IS_ENABLED(CONFIG_ARM) &&
1178 of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
Fu Weiee34f1e2017-01-18 21:25:27 +08001179 arch_timer_uses_ppi = ARCH_TIMER_PHYS_SECURE_PPI;
Fu Wei4502b6b2017-01-18 21:25:30 +08001180 else
1181 arch_timer_uses_ppi = arch_timer_select_ppi();
1182
1183 if (!arch_timer_ppi[arch_timer_uses_ppi]) {
1184 pr_err("No interrupt available, giving up\n");
1185 return -EINVAL;
1186 }
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001187
Brian Norrisd8ec7592016-10-04 11:12:09 -07001188 /* On some systems, the counter stops ticking when in suspend. */
1189 arch_counter_suspend_stop = of_property_read_bool(np,
1190 "arm,no-tick-in-suspend");
1191
Fu Weica0e1b52017-03-22 00:31:15 +08001192 ret = arch_timer_register();
1193 if (ret)
1194 return ret;
1195
1196 if (arch_timer_needs_of_probing())
1197 return 0;
1198
1199 return arch_timer_common_init();
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001200}
Daniel Lezcano17273392017-05-26 16:56:11 +02001201TIMER_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_of_init);
1202TIMER_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_of_init);
Stephen Boyd22006992013-07-18 16:59:32 -07001203
Fu Weic389d702017-04-01 01:51:00 +08001204static u32 __init
1205arch_timer_mem_frame_get_cntfrq(struct arch_timer_mem_frame *frame)
Stephen Boyd22006992013-07-18 16:59:32 -07001206{
Fu Weic389d702017-04-01 01:51:00 +08001207 void __iomem *base;
1208 u32 rate;
Stephen Boyd22006992013-07-18 16:59:32 -07001209
Fu Weic389d702017-04-01 01:51:00 +08001210 base = ioremap(frame->cntbase, frame->size);
1211 if (!base) {
1212 pr_err("Unable to map frame @ %pa\n", &frame->cntbase);
1213 return 0;
1214 }
1215
Frank Rowand3db12002017-06-09 17:26:32 -07001216 rate = readl_relaxed(base + CNTFRQ);
Fu Weic389d702017-04-01 01:51:00 +08001217
Frank Rowand3db12002017-06-09 17:26:32 -07001218 iounmap(base);
Fu Weic389d702017-04-01 01:51:00 +08001219
1220 return rate;
1221}
1222
1223static struct arch_timer_mem_frame * __init
1224arch_timer_mem_find_best_frame(struct arch_timer_mem *timer_mem)
1225{
1226 struct arch_timer_mem_frame *frame, *best_frame = NULL;
1227 void __iomem *cntctlbase;
1228 u32 cnttidr;
1229 int i;
1230
1231 cntctlbase = ioremap(timer_mem->cntctlbase, timer_mem->size);
Stephen Boyd22006992013-07-18 16:59:32 -07001232 if (!cntctlbase) {
Fu Weic389d702017-04-01 01:51:00 +08001233 pr_err("Can't map CNTCTLBase @ %pa\n",
1234 &timer_mem->cntctlbase);
1235 return NULL;
Stephen Boyd22006992013-07-18 16:59:32 -07001236 }
1237
1238 cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
Stephen Boyd22006992013-07-18 16:59:32 -07001239
1240 /*
1241 * Try to find a virtual capable frame. Otherwise fall back to a
1242 * physical capable frame.
1243 */
Fu Weic389d702017-04-01 01:51:00 +08001244 for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
1245 u32 cntacr = CNTACR_RFRQ | CNTACR_RWPT | CNTACR_RPCT |
1246 CNTACR_RWVT | CNTACR_RVOFF | CNTACR_RVCT;
Stephen Boyd22006992013-07-18 16:59:32 -07001247
Fu Weic389d702017-04-01 01:51:00 +08001248 frame = &timer_mem->frame[i];
1249 if (!frame->valid)
1250 continue;
Stephen Boyd22006992013-07-18 16:59:32 -07001251
Robin Murphye392d602016-02-01 12:00:48 +00001252 /* Try enabling everything, and see what sticks */
Fu Weic389d702017-04-01 01:51:00 +08001253 writel_relaxed(cntacr, cntctlbase + CNTACR(i));
1254 cntacr = readl_relaxed(cntctlbase + CNTACR(i));
Robin Murphye392d602016-02-01 12:00:48 +00001255
Fu Weic389d702017-04-01 01:51:00 +08001256 if ((cnttidr & CNTTIDR_VIRT(i)) &&
Robin Murphye392d602016-02-01 12:00:48 +00001257 !(~cntacr & (CNTACR_RWVT | CNTACR_RVCT))) {
Stephen Boyd22006992013-07-18 16:59:32 -07001258 best_frame = frame;
1259 arch_timer_mem_use_virtual = true;
1260 break;
1261 }
Robin Murphye392d602016-02-01 12:00:48 +00001262
1263 if (~cntacr & (CNTACR_RWPT | CNTACR_RPCT))
1264 continue;
1265
Fu Weic389d702017-04-01 01:51:00 +08001266 best_frame = frame;
Stephen Boyd22006992013-07-18 16:59:32 -07001267 }
1268
Fu Weic389d702017-04-01 01:51:00 +08001269 iounmap(cntctlbase);
1270
Sudeep Hollaf63d9472017-05-08 13:32:27 +01001271 return best_frame;
Fu Weic389d702017-04-01 01:51:00 +08001272}
1273
1274static int __init
1275arch_timer_mem_frame_register(struct arch_timer_mem_frame *frame)
1276{
1277 void __iomem *base;
1278 int ret, irq = 0;
Stephen Boyd22006992013-07-18 16:59:32 -07001279
1280 if (arch_timer_mem_use_virtual)
Fu Weic389d702017-04-01 01:51:00 +08001281 irq = frame->virt_irq;
Stephen Boyd22006992013-07-18 16:59:32 -07001282 else
Fu Weic389d702017-04-01 01:51:00 +08001283 irq = frame->phys_irq;
Robin Murphye392d602016-02-01 12:00:48 +00001284
Stephen Boyd22006992013-07-18 16:59:32 -07001285 if (!irq) {
Fu Weided24012017-01-18 21:25:25 +08001286 pr_err("Frame missing %s irq.\n",
Thomas Gleixnercfb6d652013-08-21 14:59:23 +02001287 arch_timer_mem_use_virtual ? "virt" : "phys");
Fu Weic389d702017-04-01 01:51:00 +08001288 return -EINVAL;
1289 }
1290
1291 if (!request_mem_region(frame->cntbase, frame->size,
1292 "arch_mem_timer"))
1293 return -EBUSY;
1294
1295 base = ioremap(frame->cntbase, frame->size);
1296 if (!base) {
1297 pr_err("Can't map frame's registers\n");
1298 return -ENXIO;
1299 }
1300
1301 ret = arch_timer_mem_register(base, irq);
1302 if (ret) {
1303 iounmap(base);
1304 return ret;
1305 }
1306
1307 arch_counter_base = base;
1308 arch_timers_present |= ARCH_TIMER_TYPE_MEM;
1309
1310 return 0;
1311}
1312
1313static int __init arch_timer_mem_of_init(struct device_node *np)
1314{
1315 struct arch_timer_mem *timer_mem;
1316 struct arch_timer_mem_frame *frame;
1317 struct device_node *frame_node;
1318 struct resource res;
1319 int ret = -EINVAL;
1320 u32 rate;
1321
1322 timer_mem = kzalloc(sizeof(*timer_mem), GFP_KERNEL);
1323 if (!timer_mem)
1324 return -ENOMEM;
1325
1326 if (of_address_to_resource(np, 0, &res))
1327 goto out;
1328 timer_mem->cntctlbase = res.start;
1329 timer_mem->size = resource_size(&res);
1330
1331 for_each_available_child_of_node(np, frame_node) {
1332 u32 n;
1333 struct arch_timer_mem_frame *frame;
1334
1335 if (of_property_read_u32(frame_node, "frame-number", &n)) {
1336 pr_err(FW_BUG "Missing frame-number.\n");
1337 of_node_put(frame_node);
1338 goto out;
1339 }
1340 if (n >= ARCH_TIMER_MEM_MAX_FRAMES) {
1341 pr_err(FW_BUG "Wrong frame-number, only 0-%u are permitted.\n",
1342 ARCH_TIMER_MEM_MAX_FRAMES - 1);
1343 of_node_put(frame_node);
1344 goto out;
1345 }
1346 frame = &timer_mem->frame[n];
1347
1348 if (frame->valid) {
1349 pr_err(FW_BUG "Duplicated frame-number.\n");
1350 of_node_put(frame_node);
1351 goto out;
1352 }
1353
1354 if (of_address_to_resource(frame_node, 0, &res)) {
1355 of_node_put(frame_node);
1356 goto out;
1357 }
1358 frame->cntbase = res.start;
1359 frame->size = resource_size(&res);
1360
1361 frame->virt_irq = irq_of_parse_and_map(frame_node,
1362 ARCH_TIMER_VIRT_SPI);
1363 frame->phys_irq = irq_of_parse_and_map(frame_node,
1364 ARCH_TIMER_PHYS_SPI);
1365
1366 frame->valid = true;
1367 }
1368
1369 frame = arch_timer_mem_find_best_frame(timer_mem);
1370 if (!frame) {
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001371 pr_err("Unable to find a suitable frame in timer @ %pa\n",
1372 &timer_mem->cntctlbase);
Fu Weic389d702017-04-01 01:51:00 +08001373 ret = -EINVAL;
Robin Murphye392d602016-02-01 12:00:48 +00001374 goto out;
Stephen Boyd22006992013-07-18 16:59:32 -07001375 }
1376
Fu Weic389d702017-04-01 01:51:00 +08001377 rate = arch_timer_mem_frame_get_cntfrq(frame);
Fu Wei5d3dfa92017-03-22 00:31:13 +08001378 arch_timer_of_configure_rate(rate, np);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001379
Fu Weic389d702017-04-01 01:51:00 +08001380 ret = arch_timer_mem_frame_register(frame);
1381 if (!ret && !arch_timer_needs_of_probing())
Fu Weica0e1b52017-03-22 00:31:15 +08001382 ret = arch_timer_common_init();
Robin Murphye392d602016-02-01 12:00:48 +00001383out:
Fu Weic389d702017-04-01 01:51:00 +08001384 kfree(timer_mem);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001385 return ret;
Stephen Boyd22006992013-07-18 16:59:32 -07001386}
Daniel Lezcano17273392017-05-26 16:56:11 +02001387TIMER_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem",
Fu Weic389d702017-04-01 01:51:00 +08001388 arch_timer_mem_of_init);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001389
Fu Weif79d2092017-04-01 01:51:02 +08001390#ifdef CONFIG_ACPI_GTDT
Fu Weic2743a32017-04-01 01:51:04 +08001391static int __init
1392arch_timer_mem_verify_cntfrq(struct arch_timer_mem *timer_mem)
1393{
1394 struct arch_timer_mem_frame *frame;
1395 u32 rate;
1396 int i;
1397
1398 for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
1399 frame = &timer_mem->frame[i];
1400
1401 if (!frame->valid)
1402 continue;
1403
1404 rate = arch_timer_mem_frame_get_cntfrq(frame);
1405 if (rate == arch_timer_rate)
1406 continue;
1407
1408 pr_err(FW_BUG "CNTFRQ mismatch: frame @ %pa: (0x%08lx), CPU: (0x%08lx)\n",
1409 &frame->cntbase,
1410 (unsigned long)rate, (unsigned long)arch_timer_rate);
1411
1412 return -EINVAL;
1413 }
1414
1415 return 0;
1416}
1417
1418static int __init arch_timer_mem_acpi_init(int platform_timer_count)
1419{
1420 struct arch_timer_mem *timers, *timer;
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001421 struct arch_timer_mem_frame *frame, *best_frame = NULL;
Fu Weic2743a32017-04-01 01:51:04 +08001422 int timer_count, i, ret = 0;
1423
1424 timers = kcalloc(platform_timer_count, sizeof(*timers),
1425 GFP_KERNEL);
1426 if (!timers)
1427 return -ENOMEM;
1428
1429 ret = acpi_arch_timer_mem_init(timers, &timer_count);
1430 if (ret || !timer_count)
1431 goto out;
1432
Fu Weic2743a32017-04-01 01:51:04 +08001433 /*
1434 * While unlikely, it's theoretically possible that none of the frames
1435 * in a timer expose the combination of feature we want.
1436 */
Matthias Kaehlcked197f792017-07-31 11:37:28 -07001437 for (i = 0; i < timer_count; i++) {
Fu Weic2743a32017-04-01 01:51:04 +08001438 timer = &timers[i];
1439
1440 frame = arch_timer_mem_find_best_frame(timer);
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001441 if (!best_frame)
1442 best_frame = frame;
1443
1444 ret = arch_timer_mem_verify_cntfrq(timer);
1445 if (ret) {
1446 pr_err("Disabling MMIO timers due to CNTFRQ mismatch\n");
1447 goto out;
1448 }
1449
1450 if (!best_frame) /* implies !frame */
1451 /*
1452 * Only complain about missing suitable frames if we
1453 * haven't already found one in a previous iteration.
1454 */
1455 pr_err("Unable to find a suitable frame in timer @ %pa\n",
1456 &timer->cntctlbase);
Fu Weic2743a32017-04-01 01:51:04 +08001457 }
1458
Ard Biesheuvel21492e12017-10-16 16:28:38 +01001459 if (best_frame)
1460 ret = arch_timer_mem_frame_register(best_frame);
Fu Weic2743a32017-04-01 01:51:04 +08001461out:
1462 kfree(timers);
1463 return ret;
1464}
1465
1466/* Initialize per-processor generic timer and memory-mapped timer(if present) */
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001467static int __init arch_timer_acpi_init(struct acpi_table_header *table)
1468{
Fu Weic2743a32017-04-01 01:51:04 +08001469 int ret, platform_timer_count;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001470
Fu Wei8a5c21d2017-01-18 21:25:26 +08001471 if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
Fu Weided24012017-01-18 21:25:25 +08001472 pr_warn("already initialized, skipping\n");
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001473 return -EINVAL;
1474 }
1475
Fu Wei8a5c21d2017-01-18 21:25:26 +08001476 arch_timers_present |= ARCH_TIMER_TYPE_CP15;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001477
Fu Weic2743a32017-04-01 01:51:04 +08001478 ret = acpi_gtdt_init(table, &platform_timer_count);
Fu Weif79d2092017-04-01 01:51:02 +08001479 if (ret) {
1480 pr_err("Failed to init GTDT table.\n");
1481 return ret;
1482 }
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001483
Fu Weiee34f1e2017-01-18 21:25:27 +08001484 arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI] =
Fu Weif79d2092017-04-01 01:51:02 +08001485 acpi_gtdt_map_ppi(ARCH_TIMER_PHYS_NONSECURE_PPI);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001486
Fu Weiee34f1e2017-01-18 21:25:27 +08001487 arch_timer_ppi[ARCH_TIMER_VIRT_PPI] =
Fu Weif79d2092017-04-01 01:51:02 +08001488 acpi_gtdt_map_ppi(ARCH_TIMER_VIRT_PPI);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001489
Fu Weiee34f1e2017-01-18 21:25:27 +08001490 arch_timer_ppi[ARCH_TIMER_HYP_PPI] =
Fu Weif79d2092017-04-01 01:51:02 +08001491 acpi_gtdt_map_ppi(ARCH_TIMER_HYP_PPI);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001492
Fu Weica0e1b52017-03-22 00:31:15 +08001493 arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI];
1494
Fu Wei5d3dfa92017-03-22 00:31:13 +08001495 /*
1496 * When probing via ACPI, we have no mechanism to override the sysreg
1497 * CNTFRQ value. This *must* be correct.
1498 */
1499 arch_timer_rate = arch_timer_get_cntfrq();
1500 if (!arch_timer_rate) {
1501 pr_err(FW_BUG "frequency not available.\n");
1502 return -EINVAL;
1503 }
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001504
Fu Wei4502b6b2017-01-18 21:25:30 +08001505 arch_timer_uses_ppi = arch_timer_select_ppi();
1506 if (!arch_timer_ppi[arch_timer_uses_ppi]) {
1507 pr_err("No interrupt available, giving up\n");
1508 return -EINVAL;
1509 }
1510
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001511 /* Always-on capability */
Fu Weif79d2092017-04-01 01:51:02 +08001512 arch_timer_c3stop = acpi_gtdt_c3stop(arch_timer_uses_ppi);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001513
Marc Zyngier5a38bca2017-02-21 14:37:30 +00001514 /* Check for globally applicable workarounds */
1515 arch_timer_check_ool_workaround(ate_match_acpi_oem_info, table);
1516
Fu Weica0e1b52017-03-22 00:31:15 +08001517 ret = arch_timer_register();
1518 if (ret)
1519 return ret;
1520
Fu Weic2743a32017-04-01 01:51:04 +08001521 if (platform_timer_count &&
1522 arch_timer_mem_acpi_init(platform_timer_count))
1523 pr_err("Failed to initialize memory-mapped timer.\n");
1524
Fu Weica0e1b52017-03-22 00:31:15 +08001525 return arch_timer_common_init();
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001526}
Daniel Lezcano77d62f52017-05-26 17:42:25 +02001527TIMER_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001528#endif