blob: 13e6baa13a89b2cb072d167dccd79aa56fa10233 [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
Julien Thierryec5c8e422017-10-13 14:32:55 +010080static cpumask_t evtstrm_available = CPU_MASK_NONE;
Will Deacon46fd5c62016-06-27 17:30:13 +010081static bool evtstrm_enable = IS_ENABLED(CONFIG_ARM_ARCH_TIMER_EVTSTREAM);
82
83static int __init early_evtstrm_cfg(char *buf)
84{
85 return strtobool(buf, &evtstrm_enable);
86}
87early_param("clocksource.arm_arch_timer.evtstrm", early_evtstrm_cfg);
88
Mark Rutland8a4da6e2012-11-12 14:33:44 +000089/*
90 * Architected system timer support.
91 */
92
Marc Zyngierf4e00a12017-01-20 18:28:32 +000093static __always_inline
94void arch_timer_reg_write(int access, enum arch_timer_reg reg, u32 val,
95 struct clock_event_device *clk)
96{
97 if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
98 struct arch_timer *timer = to_arch_timer(clk);
99 switch (reg) {
100 case ARCH_TIMER_REG_CTRL:
101 writel_relaxed(val, timer->base + CNTP_CTL);
102 break;
103 case ARCH_TIMER_REG_TVAL:
104 writel_relaxed(val, timer->base + CNTP_TVAL);
105 break;
106 }
107 } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
108 struct arch_timer *timer = to_arch_timer(clk);
109 switch (reg) {
110 case ARCH_TIMER_REG_CTRL:
111 writel_relaxed(val, timer->base + CNTV_CTL);
112 break;
113 case ARCH_TIMER_REG_TVAL:
114 writel_relaxed(val, timer->base + CNTV_TVAL);
115 break;
116 }
117 } else {
118 arch_timer_reg_write_cp15(access, reg, val);
119 }
120}
121
122static __always_inline
123u32 arch_timer_reg_read(int access, enum arch_timer_reg reg,
124 struct clock_event_device *clk)
125{
126 u32 val;
127
128 if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
129 struct arch_timer *timer = to_arch_timer(clk);
130 switch (reg) {
131 case ARCH_TIMER_REG_CTRL:
132 val = readl_relaxed(timer->base + CNTP_CTL);
133 break;
134 case ARCH_TIMER_REG_TVAL:
135 val = readl_relaxed(timer->base + CNTP_TVAL);
136 break;
137 }
138 } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
139 struct arch_timer *timer = to_arch_timer(clk);
140 switch (reg) {
141 case ARCH_TIMER_REG_CTRL:
142 val = readl_relaxed(timer->base + CNTV_CTL);
143 break;
144 case ARCH_TIMER_REG_TVAL:
145 val = readl_relaxed(timer->base + CNTV_TVAL);
146 break;
147 }
148 } else {
149 val = arch_timer_reg_read_cp15(access, reg);
150 }
151
152 return val;
153}
154
Marc Zyngier992dd162017-02-01 11:53:46 +0000155/*
156 * Default to cp15 based access because arm64 uses this function for
157 * sched_clock() before DT is probed and the cp15 method is guaranteed
158 * to exist on arm64. arm doesn't use this before DT is probed so even
159 * if we don't have the cp15 accessors we won't have a problem.
160 */
161u64 (*arch_timer_read_counter)(void) = arch_counter_get_cntvct;
162
163static u64 arch_counter_read(struct clocksource *cs)
164{
165 return arch_timer_read_counter();
166}
167
168static u64 arch_counter_read_cc(const struct cyclecounter *cc)
169{
170 return arch_timer_read_counter();
171}
172
173static struct clocksource clocksource_counter = {
174 .name = "arch_sys_counter",
175 .rating = 400,
176 .read = arch_counter_read,
177 .mask = CLOCKSOURCE_MASK(56),
178 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
179};
180
181static struct cyclecounter cyclecounter __ro_after_init = {
182 .read = arch_counter_read_cc,
183 .mask = CLOCKSOURCE_MASK(56),
184};
185
Marc Zyngier5a38bca2017-02-21 14:37:30 +0000186struct ate_acpi_oem_info {
187 char oem_id[ACPI_OEM_ID_SIZE + 1];
188 char oem_table_id[ACPI_OEM_TABLE_ID_SIZE + 1];
189 u32 oem_revision;
190};
191
Scott Woodf6dc1572016-09-22 03:35:17 -0500192#ifdef CONFIG_FSL_ERRATUM_A008585
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000193/*
194 * The number of retries is an arbitrary value well beyond the highest number
195 * of iterations the loop has been observed to take.
196 */
197#define __fsl_a008585_read_reg(reg) ({ \
198 u64 _old, _new; \
199 int _retries = 200; \
200 \
201 do { \
202 _old = read_sysreg(reg); \
203 _new = read_sysreg(reg); \
204 _retries--; \
205 } while (unlikely(_old != _new) && _retries); \
206 \
207 WARN_ON_ONCE(!_retries); \
208 _new; \
209})
Scott Woodf6dc1572016-09-22 03:35:17 -0500210
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000211static u32 notrace fsl_a008585_read_cntp_tval_el0(void)
Scott Woodf6dc1572016-09-22 03:35:17 -0500212{
213 return __fsl_a008585_read_reg(cntp_tval_el0);
214}
215
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000216static u32 notrace fsl_a008585_read_cntv_tval_el0(void)
Scott Woodf6dc1572016-09-22 03:35:17 -0500217{
218 return __fsl_a008585_read_reg(cntv_tval_el0);
219}
220
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000221static u64 notrace fsl_a008585_read_cntvct_el0(void)
Scott Woodf6dc1572016-09-22 03:35:17 -0500222{
223 return __fsl_a008585_read_reg(cntvct_el0);
224}
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000225#endif
226
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000227#ifdef CONFIG_HISILICON_ERRATUM_161010101
228/*
229 * Verify whether the value of the second read is larger than the first by
230 * less than 32 is the only way to confirm the value is correct, so clear the
231 * lower 5 bits to check whether the difference is greater than 32 or not.
232 * Theoretically the erratum should not occur more than twice in succession
233 * when reading the system counter, but it is possible that some interrupts
234 * may lead to more than twice read errors, triggering the warning, so setting
235 * the number of retries far beyond the number of iterations the loop has been
236 * observed to take.
237 */
238#define __hisi_161010101_read_reg(reg) ({ \
239 u64 _old, _new; \
240 int _retries = 50; \
241 \
242 do { \
243 _old = read_sysreg(reg); \
244 _new = read_sysreg(reg); \
245 _retries--; \
246 } while (unlikely((_new - _old) >> 5) && _retries); \
247 \
248 WARN_ON_ONCE(!_retries); \
249 _new; \
250})
251
252static u32 notrace hisi_161010101_read_cntp_tval_el0(void)
253{
254 return __hisi_161010101_read_reg(cntp_tval_el0);
255}
256
257static u32 notrace hisi_161010101_read_cntv_tval_el0(void)
258{
259 return __hisi_161010101_read_reg(cntv_tval_el0);
260}
261
262static u64 notrace hisi_161010101_read_cntvct_el0(void)
263{
264 return __hisi_161010101_read_reg(cntvct_el0);
265}
Marc Zyngierd003d022017-02-21 15:04:27 +0000266
267static struct ate_acpi_oem_info hisi_161010101_oem_info[] = {
268 /*
269 * Note that trailing spaces are required to properly match
270 * the OEM table information.
271 */
272 {
273 .oem_id = "HISI ",
274 .oem_table_id = "HIP05 ",
275 .oem_revision = 0,
276 },
277 {
278 .oem_id = "HISI ",
279 .oem_table_id = "HIP06 ",
280 .oem_revision = 0,
281 },
282 {
283 .oem_id = "HISI ",
284 .oem_table_id = "HIP07 ",
285 .oem_revision = 0,
286 },
287 { /* Sentinel indicating the end of the OEM array */ },
288};
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000289#endif
290
Marc Zyngierfa8d8152017-01-27 12:52:31 +0000291#ifdef CONFIG_ARM64_ERRATUM_858921
292static u64 notrace arm64_858921_read_cntvct_el0(void)
293{
294 u64 old, new;
295
296 old = read_sysreg(cntvct_el0);
297 new = read_sysreg(cntvct_el0);
298 return (((old ^ new) >> 32) & 1) ? old : new;
299}
300#endif
301
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000302#ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000303DEFINE_PER_CPU(const struct arch_timer_erratum_workaround *,
304 timer_unstable_counter_workaround);
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000305EXPORT_SYMBOL_GPL(timer_unstable_counter_workaround);
306
307DEFINE_STATIC_KEY_FALSE(arch_timer_read_ool_enabled);
308EXPORT_SYMBOL_GPL(arch_timer_read_ool_enabled);
309
Marc Zyngier83280892017-01-27 10:27:09 +0000310static void erratum_set_next_event_tval_generic(const int access, unsigned long evt,
311 struct clock_event_device *clk)
312{
313 unsigned long ctrl;
314 u64 cval = evt + arch_counter_get_cntvct();
315
316 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
317 ctrl |= ARCH_TIMER_CTRL_ENABLE;
318 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
319
320 if (access == ARCH_TIMER_PHYS_ACCESS)
321 write_sysreg(cval, cntp_cval_el0);
322 else
323 write_sysreg(cval, cntv_cval_el0);
324
325 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
326}
327
Arnd Bergmanneb645222017-04-19 19:37:09 +0200328static __maybe_unused int erratum_set_next_event_tval_virt(unsigned long evt,
Marc Zyngier83280892017-01-27 10:27:09 +0000329 struct clock_event_device *clk)
330{
331 erratum_set_next_event_tval_generic(ARCH_TIMER_VIRT_ACCESS, evt, clk);
332 return 0;
333}
334
Arnd Bergmanneb645222017-04-19 19:37:09 +0200335static __maybe_unused int erratum_set_next_event_tval_phys(unsigned long evt,
Marc Zyngier83280892017-01-27 10:27:09 +0000336 struct clock_event_device *clk)
337{
338 erratum_set_next_event_tval_generic(ARCH_TIMER_PHYS_ACCESS, evt, clk);
339 return 0;
340}
341
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000342static const struct arch_timer_erratum_workaround ool_workarounds[] = {
343#ifdef CONFIG_FSL_ERRATUM_A008585
344 {
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000345 .match_type = ate_match_dt,
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000346 .id = "fsl,erratum-a008585",
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000347 .desc = "Freescale erratum a005858",
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000348 .read_cntp_tval_el0 = fsl_a008585_read_cntp_tval_el0,
349 .read_cntv_tval_el0 = fsl_a008585_read_cntv_tval_el0,
350 .read_cntvct_el0 = fsl_a008585_read_cntvct_el0,
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000351 .set_next_event_phys = erratum_set_next_event_tval_phys,
352 .set_next_event_virt = erratum_set_next_event_tval_virt,
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000353 },
354#endif
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000355#ifdef CONFIG_HISILICON_ERRATUM_161010101
356 {
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000357 .match_type = ate_match_dt,
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000358 .id = "hisilicon,erratum-161010101",
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000359 .desc = "HiSilicon erratum 161010101",
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000360 .read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
361 .read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
362 .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000363 .set_next_event_phys = erratum_set_next_event_tval_phys,
364 .set_next_event_virt = erratum_set_next_event_tval_virt,
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000365 },
Marc Zyngierd003d022017-02-21 15:04:27 +0000366 {
367 .match_type = ate_match_acpi_oem_info,
368 .id = hisi_161010101_oem_info,
369 .desc = "HiSilicon erratum 161010101",
370 .read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
371 .read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
372 .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
373 .set_next_event_phys = erratum_set_next_event_tval_phys,
374 .set_next_event_virt = erratum_set_next_event_tval_virt,
375 },
Ding Tianhongbb42ca42017-02-06 16:47:42 +0000376#endif
Marc Zyngierfa8d8152017-01-27 12:52:31 +0000377#ifdef CONFIG_ARM64_ERRATUM_858921
378 {
379 .match_type = ate_match_local_cap_id,
380 .id = (void *)ARM64_WORKAROUND_858921,
381 .desc = "ARM erratum 858921",
382 .read_cntvct_el0 = arm64_858921_read_cntvct_el0,
383 },
384#endif
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000385};
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000386
387typedef bool (*ate_match_fn_t)(const struct arch_timer_erratum_workaround *,
388 const void *);
389
390static
391bool arch_timer_check_dt_erratum(const struct arch_timer_erratum_workaround *wa,
392 const void *arg)
393{
394 const struct device_node *np = arg;
395
396 return of_property_read_bool(np, wa->id);
397}
398
Marc Zyngier00640302017-03-20 16:47:59 +0000399static
400bool arch_timer_check_local_cap_erratum(const struct arch_timer_erratum_workaround *wa,
401 const void *arg)
402{
403 return this_cpu_has_cap((uintptr_t)wa->id);
404}
405
Marc Zyngier5a38bca2017-02-21 14:37:30 +0000406
407static
408bool arch_timer_check_acpi_oem_erratum(const struct arch_timer_erratum_workaround *wa,
409 const void *arg)
410{
411 static const struct ate_acpi_oem_info empty_oem_info = {};
412 const struct ate_acpi_oem_info *info = wa->id;
413 const struct acpi_table_header *table = arg;
414
415 /* Iterate over the ACPI OEM info array, looking for a match */
416 while (memcmp(info, &empty_oem_info, sizeof(*info))) {
417 if (!memcmp(info->oem_id, table->oem_id, ACPI_OEM_ID_SIZE) &&
418 !memcmp(info->oem_table_id, table->oem_table_id, ACPI_OEM_TABLE_ID_SIZE) &&
419 info->oem_revision == table->oem_revision)
420 return true;
421
422 info++;
423 }
424
425 return false;
426}
427
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000428static const struct arch_timer_erratum_workaround *
429arch_timer_iterate_errata(enum arch_timer_erratum_match_type type,
430 ate_match_fn_t match_fn,
431 void *arg)
432{
433 int i;
434
435 for (i = 0; i < ARRAY_SIZE(ool_workarounds); i++) {
436 if (ool_workarounds[i].match_type != type)
437 continue;
438
439 if (match_fn(&ool_workarounds[i], arg))
440 return &ool_workarounds[i];
441 }
442
443 return NULL;
444}
445
446static
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000447void arch_timer_enable_workaround(const struct arch_timer_erratum_workaround *wa,
448 bool local)
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000449{
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000450 int i;
451
452 if (local) {
453 __this_cpu_write(timer_unstable_counter_workaround, wa);
454 } else {
455 for_each_possible_cpu(i)
456 per_cpu(timer_unstable_counter_workaround, i) = wa;
457 }
458
Marc Zyngier450f9682017-08-01 09:02:57 +0100459 /*
460 * Use the locked version, as we're called from the CPU
461 * hotplug framework. Otherwise, we end-up in deadlock-land.
462 */
463 static_branch_enable_cpuslocked(&arch_timer_read_ool_enabled);
Marc Zyngiera86bd132017-02-01 12:07:15 +0000464
465 /*
466 * Don't use the vdso fastpath if errata require using the
467 * out-of-line counter accessor. We may change our mind pretty
468 * late in the game (with a per-CPU erratum, for example), so
469 * change both the default value and the vdso itself.
470 */
471 if (wa->read_cntvct_el0) {
472 clocksource_counter.archdata.vdso_direct = false;
473 vdso_default = false;
474 }
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000475}
476
477static void arch_timer_check_ool_workaround(enum arch_timer_erratum_match_type type,
478 void *arg)
479{
480 const struct arch_timer_erratum_workaround *wa;
481 ate_match_fn_t match_fn = NULL;
Marc Zyngier00640302017-03-20 16:47:59 +0000482 bool local = false;
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000483
484 switch (type) {
485 case ate_match_dt:
486 match_fn = arch_timer_check_dt_erratum;
487 break;
Marc Zyngier00640302017-03-20 16:47:59 +0000488 case ate_match_local_cap_id:
489 match_fn = arch_timer_check_local_cap_erratum;
490 local = true;
491 break;
Marc Zyngier5a38bca2017-02-21 14:37:30 +0000492 case ate_match_acpi_oem_info:
493 match_fn = arch_timer_check_acpi_oem_erratum;
494 break;
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000495 default:
496 WARN_ON(1);
497 return;
498 }
499
500 wa = arch_timer_iterate_errata(type, match_fn, arg);
501 if (!wa)
502 return;
503
Marc Zyngier00640302017-03-20 16:47:59 +0000504 if (needs_unstable_timer_counter_workaround()) {
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000505 const struct arch_timer_erratum_workaround *__wa;
506 __wa = __this_cpu_read(timer_unstable_counter_workaround);
507 if (__wa && wa != __wa)
Marc Zyngier00640302017-03-20 16:47:59 +0000508 pr_warn("Can't enable workaround for %s (clashes with %s\n)",
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000509 wa->desc, __wa->desc);
510
511 if (__wa)
512 return;
Marc Zyngier00640302017-03-20 16:47:59 +0000513 }
514
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000515 arch_timer_enable_workaround(wa, local);
Marc Zyngier00640302017-03-20 16:47:59 +0000516 pr_info("Enabling %s workaround for %s\n",
517 local ? "local" : "global", wa->desc);
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000518}
519
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000520#define erratum_handler(fn, r, ...) \
521({ \
522 bool __val; \
Marc Zyngier6acc71c2017-02-20 18:34:48 +0000523 if (needs_unstable_timer_counter_workaround()) { \
524 const struct arch_timer_erratum_workaround *__wa; \
525 __wa = __this_cpu_read(timer_unstable_counter_workaround); \
526 if (__wa && __wa->fn) { \
527 r = __wa->fn(__VA_ARGS__); \
528 __val = true; \
529 } else { \
530 __val = false; \
531 } \
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000532 } else { \
533 __val = false; \
534 } \
535 __val; \
536})
537
Marc Zyngiera86bd132017-02-01 12:07:15 +0000538static bool arch_timer_this_cpu_has_cntvct_wa(void)
539{
540 const struct arch_timer_erratum_workaround *wa;
541
542 wa = __this_cpu_read(timer_unstable_counter_workaround);
543 return wa && wa->read_cntvct_el0;
544}
Marc Zyngier651bb2e2017-01-19 17:20:59 +0000545#else
546#define arch_timer_check_ool_workaround(t,a) do { } while(0)
Marc Zyngier83280892017-01-27 10:27:09 +0000547#define erratum_set_next_event_tval_virt(...) ({BUG(); 0;})
548#define erratum_set_next_event_tval_phys(...) ({BUG(); 0;})
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000549#define erratum_handler(fn, r, ...) ({false;})
Marc Zyngiera86bd132017-02-01 12:07:15 +0000550#define arch_timer_this_cpu_has_cntvct_wa() ({false;})
Ding Tianhong16d10ef2017-02-06 16:47:41 +0000551#endif /* CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND */
Scott Woodf6dc1572016-09-22 03:35:17 -0500552
Stephen Boyde09f3cc2013-07-18 16:59:28 -0700553static __always_inline irqreturn_t timer_handler(const int access,
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000554 struct clock_event_device *evt)
555{
556 unsigned long ctrl;
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200557
Stephen Boyd60faddf2013-07-18 16:59:31 -0700558 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000559 if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
560 ctrl |= ARCH_TIMER_CTRL_IT_MASK;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700561 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000562 evt->event_handler(evt);
563 return IRQ_HANDLED;
564 }
565
566 return IRQ_NONE;
567}
568
569static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
570{
571 struct clock_event_device *evt = dev_id;
572
573 return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
574}
575
576static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
577{
578 struct clock_event_device *evt = dev_id;
579
580 return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
581}
582
Stephen Boyd22006992013-07-18 16:59:32 -0700583static irqreturn_t arch_timer_handler_phys_mem(int irq, void *dev_id)
584{
585 struct clock_event_device *evt = dev_id;
586
587 return timer_handler(ARCH_TIMER_MEM_PHYS_ACCESS, evt);
588}
589
590static irqreturn_t arch_timer_handler_virt_mem(int irq, void *dev_id)
591{
592 struct clock_event_device *evt = dev_id;
593
594 return timer_handler(ARCH_TIMER_MEM_VIRT_ACCESS, evt);
595}
596
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530597static __always_inline int timer_shutdown(const int access,
598 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000599{
600 unsigned long ctrl;
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530601
602 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
603 ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
604 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
605
606 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000607}
608
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530609static int arch_timer_shutdown_virt(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000610{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530611 return timer_shutdown(ARCH_TIMER_VIRT_ACCESS, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000612}
613
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530614static int arch_timer_shutdown_phys(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000615{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530616 return timer_shutdown(ARCH_TIMER_PHYS_ACCESS, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000617}
618
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530619static int arch_timer_shutdown_virt_mem(struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700620{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530621 return timer_shutdown(ARCH_TIMER_MEM_VIRT_ACCESS, clk);
Stephen Boyd22006992013-07-18 16:59:32 -0700622}
623
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530624static int arch_timer_shutdown_phys_mem(struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700625{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530626 return timer_shutdown(ARCH_TIMER_MEM_PHYS_ACCESS, clk);
Stephen Boyd22006992013-07-18 16:59:32 -0700627}
628
Stephen Boyd60faddf2013-07-18 16:59:31 -0700629static __always_inline void set_next_event(const int access, unsigned long evt,
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200630 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000631{
632 unsigned long ctrl;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700633 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000634 ctrl |= ARCH_TIMER_CTRL_ENABLE;
635 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700636 arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt, clk);
637 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000638}
639
640static int arch_timer_set_next_event_virt(unsigned long evt,
Stephen Boyd60faddf2013-07-18 16:59:31 -0700641 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000642{
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000643 int ret;
644
645 if (erratum_handler(set_next_event_virt, ret, evt, clk))
646 return ret;
Marc Zyngier83280892017-01-27 10:27:09 +0000647
Stephen Boyd60faddf2013-07-18 16:59:31 -0700648 set_next_event(ARCH_TIMER_VIRT_ACCESS, evt, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000649 return 0;
650}
651
652static int arch_timer_set_next_event_phys(unsigned long evt,
Stephen Boyd60faddf2013-07-18 16:59:31 -0700653 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000654{
Marc Zyngier01d3e3f2017-01-27 10:27:09 +0000655 int ret;
656
657 if (erratum_handler(set_next_event_phys, ret, evt, clk))
658 return ret;
Marc Zyngier83280892017-01-27 10:27:09 +0000659
Stephen Boyd60faddf2013-07-18 16:59:31 -0700660 set_next_event(ARCH_TIMER_PHYS_ACCESS, evt, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000661 return 0;
662}
663
Stephen Boyd22006992013-07-18 16:59:32 -0700664static int arch_timer_set_next_event_virt_mem(unsigned long evt,
665 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000666{
Stephen Boyd22006992013-07-18 16:59:32 -0700667 set_next_event(ARCH_TIMER_MEM_VIRT_ACCESS, evt, clk);
668 return 0;
669}
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000670
Stephen Boyd22006992013-07-18 16:59:32 -0700671static int arch_timer_set_next_event_phys_mem(unsigned long evt,
672 struct clock_event_device *clk)
673{
674 set_next_event(ARCH_TIMER_MEM_PHYS_ACCESS, evt, clk);
675 return 0;
676}
677
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200678static void __arch_timer_setup(unsigned type,
679 struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700680{
681 clk->features = CLOCK_EVT_FEAT_ONESHOT;
682
Fu Wei8a5c21d2017-01-18 21:25:26 +0800683 if (type == ARCH_TIMER_TYPE_CP15) {
Lorenzo Pieralisi82a561942014-04-08 10:04:32 +0100684 if (arch_timer_c3stop)
685 clk->features |= CLOCK_EVT_FEAT_C3STOP;
Stephen Boyd22006992013-07-18 16:59:32 -0700686 clk->name = "arch_sys_timer";
687 clk->rating = 450;
688 clk->cpumask = cpumask_of(smp_processor_id());
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000689 clk->irq = arch_timer_ppi[arch_timer_uses_ppi];
690 switch (arch_timer_uses_ppi) {
Fu Weiee34f1e2017-01-18 21:25:27 +0800691 case ARCH_TIMER_VIRT_PPI:
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530692 clk->set_state_shutdown = arch_timer_shutdown_virt;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530693 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt;
Stephen Boyd22006992013-07-18 16:59:32 -0700694 clk->set_next_event = arch_timer_set_next_event_virt;
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000695 break;
Fu Weiee34f1e2017-01-18 21:25:27 +0800696 case ARCH_TIMER_PHYS_SECURE_PPI:
697 case ARCH_TIMER_PHYS_NONSECURE_PPI:
698 case ARCH_TIMER_HYP_PPI:
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530699 clk->set_state_shutdown = arch_timer_shutdown_phys;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530700 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys;
Stephen Boyd22006992013-07-18 16:59:32 -0700701 clk->set_next_event = arch_timer_set_next_event_phys;
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000702 break;
703 default:
704 BUG();
Stephen Boyd22006992013-07-18 16:59:32 -0700705 }
Scott Woodf6dc1572016-09-22 03:35:17 -0500706
Marc Zyngier00640302017-03-20 16:47:59 +0000707 arch_timer_check_ool_workaround(ate_match_local_cap_id, NULL);
Stephen Boyd22006992013-07-18 16:59:32 -0700708 } else {
Stephen Boyd7b52ad22014-01-06 14:56:17 -0800709 clk->features |= CLOCK_EVT_FEAT_DYNIRQ;
Stephen Boyd22006992013-07-18 16:59:32 -0700710 clk->name = "arch_mem_timer";
711 clk->rating = 400;
712 clk->cpumask = cpu_all_mask;
713 if (arch_timer_mem_use_virtual) {
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530714 clk->set_state_shutdown = arch_timer_shutdown_virt_mem;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530715 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt_mem;
Stephen Boyd22006992013-07-18 16:59:32 -0700716 clk->set_next_event =
717 arch_timer_set_next_event_virt_mem;
718 } else {
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530719 clk->set_state_shutdown = arch_timer_shutdown_phys_mem;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530720 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys_mem;
Stephen Boyd22006992013-07-18 16:59:32 -0700721 clk->set_next_event =
722 arch_timer_set_next_event_phys_mem;
723 }
724 }
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000725
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530726 clk->set_state_shutdown(clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000727
Stephen Boyd22006992013-07-18 16:59:32 -0700728 clockevents_config_and_register(clk, arch_timer_rate, 0xf, 0x7fffffff);
729}
730
Nathan Lynche1ce5c72014-09-29 01:50:06 +0200731static void arch_timer_evtstrm_enable(int divider)
732{
733 u32 cntkctl = arch_timer_get_cntkctl();
734
735 cntkctl &= ~ARCH_TIMER_EVT_TRIGGER_MASK;
736 /* Set the divider and enable virtual event stream */
737 cntkctl |= (divider << ARCH_TIMER_EVT_TRIGGER_SHIFT)
738 | ARCH_TIMER_VIRT_EVT_EN;
739 arch_timer_set_cntkctl(cntkctl);
740 elf_hwcap |= HWCAP_EVTSTRM;
741#ifdef CONFIG_COMPAT
742 compat_elf_hwcap |= COMPAT_HWCAP_EVTSTRM;
743#endif
Julien Thierryec5c8e422017-10-13 14:32:55 +0100744 cpumask_set_cpu(smp_processor_id(), &evtstrm_available);
Nathan Lynche1ce5c72014-09-29 01:50:06 +0200745}
746
Will Deacon037f6372013-08-23 15:32:29 +0100747static void arch_timer_configure_evtstream(void)
748{
749 int evt_stream_div, pos;
750
751 /* Find the closest power of two to the divisor */
752 evt_stream_div = arch_timer_rate / ARCH_TIMER_EVT_STREAM_FREQ;
753 pos = fls(evt_stream_div);
754 if (pos > 1 && !(evt_stream_div & (1 << (pos - 2))))
755 pos--;
756 /* enable event stream */
757 arch_timer_evtstrm_enable(min(pos, 15));
758}
759
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200760static void arch_counter_set_user_access(void)
761{
762 u32 cntkctl = arch_timer_get_cntkctl();
763
Marc Zyngiera86bd132017-02-01 12:07:15 +0000764 /* Disable user access to the timers and both counters */
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200765 /* Also disable virtual event stream */
766 cntkctl &= ~(ARCH_TIMER_USR_PT_ACCESS_EN
767 | ARCH_TIMER_USR_VT_ACCESS_EN
Marc Zyngiera86bd132017-02-01 12:07:15 +0000768 | ARCH_TIMER_USR_VCT_ACCESS_EN
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200769 | ARCH_TIMER_VIRT_EVT_EN
770 | ARCH_TIMER_USR_PCT_ACCESS_EN);
771
Marc Zyngiera86bd132017-02-01 12:07:15 +0000772 /*
773 * Enable user access to the virtual counter if it doesn't
774 * need to be workaround. The vdso may have been already
775 * disabled though.
776 */
777 if (arch_timer_this_cpu_has_cntvct_wa())
778 pr_info("CPU%d: Trapping CNTVCT access\n", smp_processor_id());
779 else
780 cntkctl |= ARCH_TIMER_USR_VCT_ACCESS_EN;
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200781
782 arch_timer_set_cntkctl(cntkctl);
783}
784
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000785static bool arch_timer_has_nonsecure_ppi(void)
786{
Fu Weiee34f1e2017-01-18 21:25:27 +0800787 return (arch_timer_uses_ppi == ARCH_TIMER_PHYS_SECURE_PPI &&
788 arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000789}
790
Marc Zyngierf005bd72016-08-01 10:54:15 +0100791static u32 check_ppi_trigger(int irq)
792{
793 u32 flags = irq_get_trigger_type(irq);
794
795 if (flags != IRQF_TRIGGER_HIGH && flags != IRQF_TRIGGER_LOW) {
796 pr_warn("WARNING: Invalid trigger for IRQ%d, assuming level low\n", irq);
797 pr_warn("WARNING: Please fix your firmware\n");
798 flags = IRQF_TRIGGER_LOW;
799 }
800
801 return flags;
802}
803
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000804static int arch_timer_starting_cpu(unsigned int cpu)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000805{
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000806 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
Marc Zyngierf005bd72016-08-01 10:54:15 +0100807 u32 flags;
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000808
Fu Wei8a5c21d2017-01-18 21:25:26 +0800809 __arch_timer_setup(ARCH_TIMER_TYPE_CP15, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000810
Marc Zyngierf005bd72016-08-01 10:54:15 +0100811 flags = check_ppi_trigger(arch_timer_ppi[arch_timer_uses_ppi]);
812 enable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], flags);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000813
Marc Zyngierf005bd72016-08-01 10:54:15 +0100814 if (arch_timer_has_nonsecure_ppi()) {
Fu Weiee34f1e2017-01-18 21:25:27 +0800815 flags = check_ppi_trigger(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
816 enable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
817 flags);
Marc Zyngierf005bd72016-08-01 10:54:15 +0100818 }
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000819
820 arch_counter_set_user_access();
Will Deacon46fd5c62016-06-27 17:30:13 +0100821 if (evtstrm_enable)
Will Deacon037f6372013-08-23 15:32:29 +0100822 arch_timer_configure_evtstream();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000823
824 return 0;
825}
826
Fu Wei5d3dfa92017-03-22 00:31:13 +0800827/*
828 * For historical reasons, when probing with DT we use whichever (non-zero)
829 * rate was probed first, and don't verify that others match. If the first node
830 * probed has a clock-frequency property, this overrides the HW register.
831 */
832static void arch_timer_of_configure_rate(u32 rate, struct device_node *np)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000833{
Stephen Boyd22006992013-07-18 16:59:32 -0700834 /* Who has more than one independent system counter? */
835 if (arch_timer_rate)
836 return;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000837
Fu Wei5d3dfa92017-03-22 00:31:13 +0800838 if (of_property_read_u32(np, "clock-frequency", &arch_timer_rate))
839 arch_timer_rate = rate;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000840
Stephen Boyd22006992013-07-18 16:59:32 -0700841 /* Check the timer frequency. */
842 if (arch_timer_rate == 0)
Fu Weided24012017-01-18 21:25:25 +0800843 pr_warn("frequency not available\n");
Stephen Boyd22006992013-07-18 16:59:32 -0700844}
845
846static void arch_timer_banner(unsigned type)
847{
Fu Weided24012017-01-18 21:25:25 +0800848 pr_info("%s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
Fu Wei8a5c21d2017-01-18 21:25:26 +0800849 type & ARCH_TIMER_TYPE_CP15 ? "cp15" : "",
850 type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ?
851 " and " : "",
852 type & ARCH_TIMER_TYPE_MEM ? "mmio" : "",
Fu Weided24012017-01-18 21:25:25 +0800853 (unsigned long)arch_timer_rate / 1000000,
854 (unsigned long)(arch_timer_rate / 10000) % 100,
Fu Wei8a5c21d2017-01-18 21:25:26 +0800855 type & ARCH_TIMER_TYPE_CP15 ?
Fu Weiee34f1e2017-01-18 21:25:27 +0800856 (arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI) ? "virt" : "phys" :
Stephen Boyd22006992013-07-18 16:59:32 -0700857 "",
Fu Wei8a5c21d2017-01-18 21:25:26 +0800858 type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ? "/" : "",
859 type & ARCH_TIMER_TYPE_MEM ?
Stephen Boyd22006992013-07-18 16:59:32 -0700860 arch_timer_mem_use_virtual ? "virt" : "phys" :
861 "");
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000862}
863
864u32 arch_timer_get_rate(void)
865{
866 return arch_timer_rate;
867}
868
Julien Thierryec5c8e422017-10-13 14:32:55 +0100869bool arch_timer_evtstrm_available(void)
870{
871 /*
872 * We might get called from a preemptible context. This is fine
873 * because availability of the event stream should be always the same
874 * for a preemptible context and context where we might resume a task.
875 */
876 return cpumask_test_cpu(raw_smp_processor_id(), &evtstrm_available);
877}
878
Stephen Boyd22006992013-07-18 16:59:32 -0700879static u64 arch_counter_get_cntvct_mem(void)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000880{
Stephen Boyd22006992013-07-18 16:59:32 -0700881 u32 vct_lo, vct_hi, tmp_hi;
882
883 do {
884 vct_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
885 vct_lo = readl_relaxed(arch_counter_base + CNTVCT_LO);
886 tmp_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
887 } while (vct_hi != tmp_hi);
888
889 return ((u64) vct_hi << 32) | vct_lo;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000890}
891
Julien Grallb4d6ce92016-04-11 16:32:51 +0100892static struct arch_timer_kvm_info arch_timer_kvm_info;
893
894struct arch_timer_kvm_info *arch_timer_get_kvm_info(void)
895{
896 return &arch_timer_kvm_info;
897}
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000898
Stephen Boyd22006992013-07-18 16:59:32 -0700899static void __init arch_counter_register(unsigned type)
900{
901 u64 start_count;
902
903 /* Register the CP15 based counter if we have one */
Fu Wei8a5c21d2017-01-18 21:25:26 +0800904 if (type & ARCH_TIMER_TYPE_CP15) {
Fu Weiee34f1e2017-01-18 21:25:27 +0800905 if (IS_ENABLED(CONFIG_ARM64) ||
906 arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI)
Sonny Rao0b46b8a2014-11-23 23:02:44 -0800907 arch_timer_read_counter = arch_counter_get_cntvct;
908 else
909 arch_timer_read_counter = arch_counter_get_cntpct;
Scott Woodf6dc1572016-09-22 03:35:17 -0500910
Marc Zyngiera86bd132017-02-01 12:07:15 +0000911 clocksource_counter.archdata.vdso_direct = vdso_default;
Nathan Lynch423bd692014-09-29 01:50:06 +0200912 } else {
Stephen Boyd22006992013-07-18 16:59:32 -0700913 arch_timer_read_counter = arch_counter_get_cntvct_mem;
Nathan Lynch423bd692014-09-29 01:50:06 +0200914 }
915
Brian Norrisd8ec7592016-10-04 11:12:09 -0700916 if (!arch_counter_suspend_stop)
917 clocksource_counter.flags |= CLOCK_SOURCE_SUSPEND_NONSTOP;
Stephen Boyd22006992013-07-18 16:59:32 -0700918 start_count = arch_timer_read_counter();
919 clocksource_register_hz(&clocksource_counter, arch_timer_rate);
920 cyclecounter.mult = clocksource_counter.mult;
921 cyclecounter.shift = clocksource_counter.shift;
Julien Grallb4d6ce92016-04-11 16:32:51 +0100922 timecounter_init(&arch_timer_kvm_info.timecounter,
923 &cyclecounter, start_count);
Thierry Reding4a7d3e82013-10-15 15:31:51 +0200924
925 /* 56 bits minimum, so we assume worst case rollover */
926 sched_clock_register(arch_timer_read_counter, 56, arch_timer_rate);
Stephen Boyd22006992013-07-18 16:59:32 -0700927}
928
Paul Gortmaker8c37bb32013-06-19 11:32:08 -0400929static void arch_timer_stop(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000930{
Fu Weided24012017-01-18 21:25:25 +0800931 pr_debug("disable IRQ%d cpu #%d\n", clk->irq, smp_processor_id());
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000932
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000933 disable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi]);
934 if (arch_timer_has_nonsecure_ppi())
Fu Weiee34f1e2017-01-18 21:25:27 +0800935 disable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000936
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530937 clk->set_state_shutdown(clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000938}
939
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000940static int arch_timer_dying_cpu(unsigned int cpu)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000941{
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000942 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000943
Julien Thierryec5c8e422017-10-13 14:32:55 +0100944 cpumask_clear_cpu(smp_processor_id(), &evtstrm_available);
945
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000946 arch_timer_stop(clk);
947 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000948}
949
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100950#ifdef CONFIG_CPU_PM
Marc Zyngierbee67c52017-04-04 17:05:16 +0100951static DEFINE_PER_CPU(unsigned long, saved_cntkctl);
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100952static int arch_timer_cpu_pm_notify(struct notifier_block *self,
953 unsigned long action, void *hcpu)
954{
Julien Thierryec5c8e422017-10-13 14:32:55 +0100955 if (action == CPU_PM_ENTER) {
Marc Zyngierbee67c52017-04-04 17:05:16 +0100956 __this_cpu_write(saved_cntkctl, arch_timer_get_cntkctl());
Julien Thierryec5c8e422017-10-13 14:32:55 +0100957
958 cpumask_clear_cpu(smp_processor_id(), &evtstrm_available);
959 } else if (action == CPU_PM_ENTER_FAILED || action == CPU_PM_EXIT) {
Marc Zyngierbee67c52017-04-04 17:05:16 +0100960 arch_timer_set_cntkctl(__this_cpu_read(saved_cntkctl));
Julien Thierryec5c8e422017-10-13 14:32:55 +0100961
962 if (elf_hwcap & HWCAP_EVTSTRM)
963 cpumask_set_cpu(smp_processor_id(), &evtstrm_available);
964 }
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100965 return NOTIFY_OK;
966}
967
968static struct notifier_block arch_timer_cpu_pm_notifier = {
969 .notifier_call = arch_timer_cpu_pm_notify,
970};
971
972static int __init arch_timer_cpu_pm_init(void)
973{
974 return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier);
975}
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000976
977static void __init arch_timer_cpu_pm_deinit(void)
978{
979 WARN_ON(cpu_pm_unregister_notifier(&arch_timer_cpu_pm_notifier));
980}
981
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100982#else
983static int __init arch_timer_cpu_pm_init(void)
984{
985 return 0;
986}
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000987
988static void __init arch_timer_cpu_pm_deinit(void)
989{
990}
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100991#endif
992
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000993static int __init arch_timer_register(void)
994{
995 int err;
996 int ppi;
997
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000998 arch_timer_evt = alloc_percpu(struct clock_event_device);
999 if (!arch_timer_evt) {
1000 err = -ENOMEM;
1001 goto out;
1002 }
1003
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001004 ppi = arch_timer_ppi[arch_timer_uses_ppi];
1005 switch (arch_timer_uses_ppi) {
Fu Weiee34f1e2017-01-18 21:25:27 +08001006 case ARCH_TIMER_VIRT_PPI:
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001007 err = request_percpu_irq(ppi, arch_timer_handler_virt,
1008 "arch_timer", arch_timer_evt);
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001009 break;
Fu Weiee34f1e2017-01-18 21:25:27 +08001010 case ARCH_TIMER_PHYS_SECURE_PPI:
1011 case ARCH_TIMER_PHYS_NONSECURE_PPI:
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001012 err = request_percpu_irq(ppi, arch_timer_handler_phys,
1013 "arch_timer", arch_timer_evt);
Fu Wei4502b6b2017-01-18 21:25:30 +08001014 if (!err && arch_timer_has_nonsecure_ppi()) {
Fu Weiee34f1e2017-01-18 21:25:27 +08001015 ppi = arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI];
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001016 err = request_percpu_irq(ppi, arch_timer_handler_phys,
1017 "arch_timer", arch_timer_evt);
1018 if (err)
Fu Weiee34f1e2017-01-18 21:25:27 +08001019 free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_SECURE_PPI],
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001020 arch_timer_evt);
1021 }
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001022 break;
Fu Weiee34f1e2017-01-18 21:25:27 +08001023 case ARCH_TIMER_HYP_PPI:
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001024 err = request_percpu_irq(ppi, arch_timer_handler_phys,
1025 "arch_timer", arch_timer_evt);
1026 break;
1027 default:
1028 BUG();
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001029 }
1030
1031 if (err) {
Fu Weided24012017-01-18 21:25:25 +08001032 pr_err("can't register interrupt %d (%d)\n", ppi, err);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001033 goto out_free;
1034 }
1035
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001036 err = arch_timer_cpu_pm_init();
1037 if (err)
1038 goto out_unreg_notify;
1039
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001040 /* Register and immediately configure the timer on the boot CPU */
1041 err = cpuhp_setup_state(CPUHP_AP_ARM_ARCH_TIMER_STARTING,
Thomas Gleixner73c1b412016-12-21 20:19:54 +01001042 "clockevents/arm/arch_timer:starting",
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001043 arch_timer_starting_cpu, arch_timer_dying_cpu);
1044 if (err)
1045 goto out_unreg_cpupm;
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001046 return 0;
1047
Richard Cochran7e86e8b2016-07-13 17:16:39 +00001048out_unreg_cpupm:
1049 arch_timer_cpu_pm_deinit();
1050
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +01001051out_unreg_notify:
Marc Zyngierf81f03f2014-02-20 15:21:23 +00001052 free_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], arch_timer_evt);
1053 if (arch_timer_has_nonsecure_ppi())
Fu Weiee34f1e2017-01-18 21:25:27 +08001054 free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001055 arch_timer_evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +00001056
1057out_free:
1058 free_percpu(arch_timer_evt);
1059out:
1060 return err;
1061}
1062
Stephen Boyd22006992013-07-18 16:59:32 -07001063static int __init arch_timer_mem_register(void __iomem *base, unsigned int irq)
1064{
1065 int ret;
1066 irq_handler_t func;
1067 struct arch_timer *t;
1068
1069 t = kzalloc(sizeof(*t), GFP_KERNEL);
1070 if (!t)
1071 return -ENOMEM;
1072
1073 t->base = base;
1074 t->evt.irq = irq;
Fu Wei8a5c21d2017-01-18 21:25:26 +08001075 __arch_timer_setup(ARCH_TIMER_TYPE_MEM, &t->evt);
Stephen Boyd22006992013-07-18 16:59:32 -07001076
1077 if (arch_timer_mem_use_virtual)
1078 func = arch_timer_handler_virt_mem;
1079 else
1080 func = arch_timer_handler_phys_mem;
1081
1082 ret = request_irq(irq, func, IRQF_TIMER, "arch_mem_timer", &t->evt);
1083 if (ret) {
Fu Weided24012017-01-18 21:25:25 +08001084 pr_err("Failed to request mem timer irq\n");
Stephen Boyd22006992013-07-18 16:59:32 -07001085 kfree(t);
1086 }
1087
1088 return ret;
1089}
1090
1091static const struct of_device_id arch_timer_of_match[] __initconst = {
1092 { .compatible = "arm,armv7-timer", },
1093 { .compatible = "arm,armv8-timer", },
1094 {},
1095};
1096
1097static const struct of_device_id arch_timer_mem_of_match[] __initconst = {
1098 { .compatible = "arm,armv7-timer-mem", },
1099 {},
1100};
1101
Fu Wei13bf6992017-03-22 00:31:14 +08001102static bool __init arch_timer_needs_of_probing(void)
Sudeep Hollac387f072014-09-29 01:50:05 +02001103{
1104 struct device_node *dn;
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001105 bool needs_probing = false;
Fu Wei13bf6992017-03-22 00:31:14 +08001106 unsigned int mask = ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM;
Sudeep Hollac387f072014-09-29 01:50:05 +02001107
Fu Wei13bf6992017-03-22 00:31:14 +08001108 /* We have two timers, and both device-tree nodes are probed. */
1109 if ((arch_timers_present & mask) == mask)
1110 return false;
1111
1112 /*
1113 * Only one type of timer is probed,
1114 * check if we have another type of timer node in device-tree.
1115 */
1116 if (arch_timers_present & ARCH_TIMER_TYPE_CP15)
1117 dn = of_find_matching_node(NULL, arch_timer_mem_of_match);
1118 else
1119 dn = of_find_matching_node(NULL, arch_timer_of_match);
1120
1121 if (dn && of_device_is_available(dn))
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001122 needs_probing = true;
Fu Wei13bf6992017-03-22 00:31:14 +08001123
Sudeep Hollac387f072014-09-29 01:50:05 +02001124 of_node_put(dn);
1125
Laurent Pinchart566e6df2015-03-31 12:12:22 +02001126 return needs_probing;
Sudeep Hollac387f072014-09-29 01:50:05 +02001127}
1128
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001129static int __init arch_timer_common_init(void)
Stephen Boyd22006992013-07-18 16:59:32 -07001130{
Stephen Boyd22006992013-07-18 16:59:32 -07001131 arch_timer_banner(arch_timers_present);
1132 arch_counter_register(arch_timers_present);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001133 return arch_timer_arch_init();
Stephen Boyd22006992013-07-18 16:59:32 -07001134}
1135
Fu Wei4502b6b2017-01-18 21:25:30 +08001136/**
1137 * arch_timer_select_ppi() - Select suitable PPI for the current system.
1138 *
1139 * If HYP mode is available, we know that the physical timer
1140 * has been configured to be accessible from PL1. Use it, so
1141 * that a guest can use the virtual timer instead.
1142 *
1143 * On ARMv8.1 with VH extensions, the kernel runs in HYP. VHE
1144 * accesses to CNTP_*_EL1 registers are silently redirected to
1145 * their CNTHP_*_EL2 counterparts, and use a different PPI
1146 * number.
1147 *
1148 * If no interrupt provided for virtual timer, we'll have to
1149 * stick to the physical timer. It'd better be accessible...
1150 * For arm64 we never use the secure interrupt.
1151 *
1152 * Return: a suitable PPI type for the current system.
1153 */
1154static enum arch_timer_ppi_nr __init arch_timer_select_ppi(void)
1155{
1156 if (is_kernel_in_hyp_mode())
1157 return ARCH_TIMER_HYP_PPI;
1158
1159 if (!is_hyp_mode_available() && arch_timer_ppi[ARCH_TIMER_VIRT_PPI])
1160 return ARCH_TIMER_VIRT_PPI;
1161
1162 if (IS_ENABLED(CONFIG_ARM64))
1163 return ARCH_TIMER_PHYS_NONSECURE_PPI;
1164
1165 return ARCH_TIMER_PHYS_SECURE_PPI;
1166}
1167
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001168static int __init arch_timer_of_init(struct device_node *np)
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001169{
Fu Weica0e1b52017-03-22 00:31:15 +08001170 int i, ret;
Fu Wei5d3dfa92017-03-22 00:31:13 +08001171 u32 rate;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001172
Fu Wei8a5c21d2017-01-18 21:25:26 +08001173 if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
Fu Weided24012017-01-18 21:25:25 +08001174 pr_warn("multiple nodes in dt, skipping\n");
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001175 return 0;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001176 }
1177
Fu Wei8a5c21d2017-01-18 21:25:26 +08001178 arch_timers_present |= ARCH_TIMER_TYPE_CP15;
Fu Weiee34f1e2017-01-18 21:25:27 +08001179 for (i = ARCH_TIMER_PHYS_SECURE_PPI; i < ARCH_TIMER_MAX_TIMER_PPI; i++)
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001180 arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
1181
Fu Weica0e1b52017-03-22 00:31:15 +08001182 arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI];
1183
Fu Weic389d702017-04-01 01:51:00 +08001184 rate = arch_timer_get_cntfrq();
Fu Wei5d3dfa92017-03-22 00:31:13 +08001185 arch_timer_of_configure_rate(rate, np);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001186
1187 arch_timer_c3stop = !of_property_read_bool(np, "always-on");
1188
Marc Zyngier651bb2e2017-01-19 17:20:59 +00001189 /* Check for globally applicable workarounds */
1190 arch_timer_check_ool_workaround(ate_match_dt, np);
Scott Woodf6dc1572016-09-22 03:35:17 -05001191
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001192 /*
1193 * If we cannot rely on firmware initializing the timer registers then
1194 * we should use the physical timers instead.
1195 */
1196 if (IS_ENABLED(CONFIG_ARM) &&
1197 of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
Fu Weiee34f1e2017-01-18 21:25:27 +08001198 arch_timer_uses_ppi = ARCH_TIMER_PHYS_SECURE_PPI;
Fu Wei4502b6b2017-01-18 21:25:30 +08001199 else
1200 arch_timer_uses_ppi = arch_timer_select_ppi();
1201
1202 if (!arch_timer_ppi[arch_timer_uses_ppi]) {
1203 pr_err("No interrupt available, giving up\n");
1204 return -EINVAL;
1205 }
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001206
Brian Norrisd8ec7592016-10-04 11:12:09 -07001207 /* On some systems, the counter stops ticking when in suspend. */
1208 arch_counter_suspend_stop = of_property_read_bool(np,
1209 "arm,no-tick-in-suspend");
1210
Fu Weica0e1b52017-03-22 00:31:15 +08001211 ret = arch_timer_register();
1212 if (ret)
1213 return ret;
1214
1215 if (arch_timer_needs_of_probing())
1216 return 0;
1217
1218 return arch_timer_common_init();
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001219}
Daniel Lezcano17273392017-05-26 16:56:11 +02001220TIMER_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_of_init);
1221TIMER_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_of_init);
Stephen Boyd22006992013-07-18 16:59:32 -07001222
Fu Weic389d702017-04-01 01:51:00 +08001223static u32 __init
1224arch_timer_mem_frame_get_cntfrq(struct arch_timer_mem_frame *frame)
Stephen Boyd22006992013-07-18 16:59:32 -07001225{
Fu Weic389d702017-04-01 01:51:00 +08001226 void __iomem *base;
1227 u32 rate;
Stephen Boyd22006992013-07-18 16:59:32 -07001228
Fu Weic389d702017-04-01 01:51:00 +08001229 base = ioremap(frame->cntbase, frame->size);
1230 if (!base) {
1231 pr_err("Unable to map frame @ %pa\n", &frame->cntbase);
1232 return 0;
1233 }
1234
Frank Rowand3db12002017-06-09 17:26:32 -07001235 rate = readl_relaxed(base + CNTFRQ);
Fu Weic389d702017-04-01 01:51:00 +08001236
Frank Rowand3db12002017-06-09 17:26:32 -07001237 iounmap(base);
Fu Weic389d702017-04-01 01:51:00 +08001238
1239 return rate;
1240}
1241
1242static struct arch_timer_mem_frame * __init
1243arch_timer_mem_find_best_frame(struct arch_timer_mem *timer_mem)
1244{
1245 struct arch_timer_mem_frame *frame, *best_frame = NULL;
1246 void __iomem *cntctlbase;
1247 u32 cnttidr;
1248 int i;
1249
1250 cntctlbase = ioremap(timer_mem->cntctlbase, timer_mem->size);
Stephen Boyd22006992013-07-18 16:59:32 -07001251 if (!cntctlbase) {
Fu Weic389d702017-04-01 01:51:00 +08001252 pr_err("Can't map CNTCTLBase @ %pa\n",
1253 &timer_mem->cntctlbase);
1254 return NULL;
Stephen Boyd22006992013-07-18 16:59:32 -07001255 }
1256
1257 cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
Stephen Boyd22006992013-07-18 16:59:32 -07001258
1259 /*
1260 * Try to find a virtual capable frame. Otherwise fall back to a
1261 * physical capable frame.
1262 */
Fu Weic389d702017-04-01 01:51:00 +08001263 for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
1264 u32 cntacr = CNTACR_RFRQ | CNTACR_RWPT | CNTACR_RPCT |
1265 CNTACR_RWVT | CNTACR_RVOFF | CNTACR_RVCT;
Stephen Boyd22006992013-07-18 16:59:32 -07001266
Fu Weic389d702017-04-01 01:51:00 +08001267 frame = &timer_mem->frame[i];
1268 if (!frame->valid)
1269 continue;
Stephen Boyd22006992013-07-18 16:59:32 -07001270
Robin Murphye392d602016-02-01 12:00:48 +00001271 /* Try enabling everything, and see what sticks */
Fu Weic389d702017-04-01 01:51:00 +08001272 writel_relaxed(cntacr, cntctlbase + CNTACR(i));
1273 cntacr = readl_relaxed(cntctlbase + CNTACR(i));
Robin Murphye392d602016-02-01 12:00:48 +00001274
Fu Weic389d702017-04-01 01:51:00 +08001275 if ((cnttidr & CNTTIDR_VIRT(i)) &&
Robin Murphye392d602016-02-01 12:00:48 +00001276 !(~cntacr & (CNTACR_RWVT | CNTACR_RVCT))) {
Stephen Boyd22006992013-07-18 16:59:32 -07001277 best_frame = frame;
1278 arch_timer_mem_use_virtual = true;
1279 break;
1280 }
Robin Murphye392d602016-02-01 12:00:48 +00001281
1282 if (~cntacr & (CNTACR_RWPT | CNTACR_RPCT))
1283 continue;
1284
Fu Weic389d702017-04-01 01:51:00 +08001285 best_frame = frame;
Stephen Boyd22006992013-07-18 16:59:32 -07001286 }
1287
Fu Weic389d702017-04-01 01:51:00 +08001288 iounmap(cntctlbase);
1289
1290 if (!best_frame)
1291 pr_err("Unable to find a suitable frame in timer @ %pa\n",
1292 &timer_mem->cntctlbase);
1293
Sudeep Hollaf63d9472017-05-08 13:32:27 +01001294 return best_frame;
Fu Weic389d702017-04-01 01:51:00 +08001295}
1296
1297static int __init
1298arch_timer_mem_frame_register(struct arch_timer_mem_frame *frame)
1299{
1300 void __iomem *base;
1301 int ret, irq = 0;
Stephen Boyd22006992013-07-18 16:59:32 -07001302
1303 if (arch_timer_mem_use_virtual)
Fu Weic389d702017-04-01 01:51:00 +08001304 irq = frame->virt_irq;
Stephen Boyd22006992013-07-18 16:59:32 -07001305 else
Fu Weic389d702017-04-01 01:51:00 +08001306 irq = frame->phys_irq;
Robin Murphye392d602016-02-01 12:00:48 +00001307
Stephen Boyd22006992013-07-18 16:59:32 -07001308 if (!irq) {
Fu Weided24012017-01-18 21:25:25 +08001309 pr_err("Frame missing %s irq.\n",
Thomas Gleixnercfb6d652013-08-21 14:59:23 +02001310 arch_timer_mem_use_virtual ? "virt" : "phys");
Fu Weic389d702017-04-01 01:51:00 +08001311 return -EINVAL;
1312 }
1313
1314 if (!request_mem_region(frame->cntbase, frame->size,
1315 "arch_mem_timer"))
1316 return -EBUSY;
1317
1318 base = ioremap(frame->cntbase, frame->size);
1319 if (!base) {
1320 pr_err("Can't map frame's registers\n");
1321 return -ENXIO;
1322 }
1323
1324 ret = arch_timer_mem_register(base, irq);
1325 if (ret) {
1326 iounmap(base);
1327 return ret;
1328 }
1329
1330 arch_counter_base = base;
1331 arch_timers_present |= ARCH_TIMER_TYPE_MEM;
1332
1333 return 0;
1334}
1335
1336static int __init arch_timer_mem_of_init(struct device_node *np)
1337{
1338 struct arch_timer_mem *timer_mem;
1339 struct arch_timer_mem_frame *frame;
1340 struct device_node *frame_node;
1341 struct resource res;
1342 int ret = -EINVAL;
1343 u32 rate;
1344
1345 timer_mem = kzalloc(sizeof(*timer_mem), GFP_KERNEL);
1346 if (!timer_mem)
1347 return -ENOMEM;
1348
1349 if (of_address_to_resource(np, 0, &res))
1350 goto out;
1351 timer_mem->cntctlbase = res.start;
1352 timer_mem->size = resource_size(&res);
1353
1354 for_each_available_child_of_node(np, frame_node) {
1355 u32 n;
1356 struct arch_timer_mem_frame *frame;
1357
1358 if (of_property_read_u32(frame_node, "frame-number", &n)) {
1359 pr_err(FW_BUG "Missing frame-number.\n");
1360 of_node_put(frame_node);
1361 goto out;
1362 }
1363 if (n >= ARCH_TIMER_MEM_MAX_FRAMES) {
1364 pr_err(FW_BUG "Wrong frame-number, only 0-%u are permitted.\n",
1365 ARCH_TIMER_MEM_MAX_FRAMES - 1);
1366 of_node_put(frame_node);
1367 goto out;
1368 }
1369 frame = &timer_mem->frame[n];
1370
1371 if (frame->valid) {
1372 pr_err(FW_BUG "Duplicated frame-number.\n");
1373 of_node_put(frame_node);
1374 goto out;
1375 }
1376
1377 if (of_address_to_resource(frame_node, 0, &res)) {
1378 of_node_put(frame_node);
1379 goto out;
1380 }
1381 frame->cntbase = res.start;
1382 frame->size = resource_size(&res);
1383
1384 frame->virt_irq = irq_of_parse_and_map(frame_node,
1385 ARCH_TIMER_VIRT_SPI);
1386 frame->phys_irq = irq_of_parse_and_map(frame_node,
1387 ARCH_TIMER_PHYS_SPI);
1388
1389 frame->valid = true;
1390 }
1391
1392 frame = arch_timer_mem_find_best_frame(timer_mem);
1393 if (!frame) {
1394 ret = -EINVAL;
Robin Murphye392d602016-02-01 12:00:48 +00001395 goto out;
Stephen Boyd22006992013-07-18 16:59:32 -07001396 }
1397
Fu Weic389d702017-04-01 01:51:00 +08001398 rate = arch_timer_mem_frame_get_cntfrq(frame);
Fu Wei5d3dfa92017-03-22 00:31:13 +08001399 arch_timer_of_configure_rate(rate, np);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001400
Fu Weic389d702017-04-01 01:51:00 +08001401 ret = arch_timer_mem_frame_register(frame);
1402 if (!ret && !arch_timer_needs_of_probing())
Fu Weica0e1b52017-03-22 00:31:15 +08001403 ret = arch_timer_common_init();
Robin Murphye392d602016-02-01 12:00:48 +00001404out:
Fu Weic389d702017-04-01 01:51:00 +08001405 kfree(timer_mem);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +02001406 return ret;
Stephen Boyd22006992013-07-18 16:59:32 -07001407}
Daniel Lezcano17273392017-05-26 16:56:11 +02001408TIMER_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem",
Fu Weic389d702017-04-01 01:51:00 +08001409 arch_timer_mem_of_init);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001410
Fu Weif79d2092017-04-01 01:51:02 +08001411#ifdef CONFIG_ACPI_GTDT
Fu Weic2743a32017-04-01 01:51:04 +08001412static int __init
1413arch_timer_mem_verify_cntfrq(struct arch_timer_mem *timer_mem)
1414{
1415 struct arch_timer_mem_frame *frame;
1416 u32 rate;
1417 int i;
1418
1419 for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
1420 frame = &timer_mem->frame[i];
1421
1422 if (!frame->valid)
1423 continue;
1424
1425 rate = arch_timer_mem_frame_get_cntfrq(frame);
1426 if (rate == arch_timer_rate)
1427 continue;
1428
1429 pr_err(FW_BUG "CNTFRQ mismatch: frame @ %pa: (0x%08lx), CPU: (0x%08lx)\n",
1430 &frame->cntbase,
1431 (unsigned long)rate, (unsigned long)arch_timer_rate);
1432
1433 return -EINVAL;
1434 }
1435
1436 return 0;
1437}
1438
1439static int __init arch_timer_mem_acpi_init(int platform_timer_count)
1440{
1441 struct arch_timer_mem *timers, *timer;
1442 struct arch_timer_mem_frame *frame;
1443 int timer_count, i, ret = 0;
1444
1445 timers = kcalloc(platform_timer_count, sizeof(*timers),
1446 GFP_KERNEL);
1447 if (!timers)
1448 return -ENOMEM;
1449
1450 ret = acpi_arch_timer_mem_init(timers, &timer_count);
1451 if (ret || !timer_count)
1452 goto out;
1453
1454 for (i = 0; i < timer_count; i++) {
1455 ret = arch_timer_mem_verify_cntfrq(&timers[i]);
1456 if (ret) {
1457 pr_err("Disabling MMIO timers due to CNTFRQ mismatch\n");
1458 goto out;
1459 }
1460 }
1461
1462 /*
1463 * While unlikely, it's theoretically possible that none of the frames
1464 * in a timer expose the combination of feature we want.
1465 */
Matthias Kaehlcked197f792017-07-31 11:37:28 -07001466 for (i = 0; i < timer_count; i++) {
Fu Weic2743a32017-04-01 01:51:04 +08001467 timer = &timers[i];
1468
1469 frame = arch_timer_mem_find_best_frame(timer);
1470 if (frame)
1471 break;
1472 }
1473
1474 if (frame)
1475 ret = arch_timer_mem_frame_register(frame);
1476out:
1477 kfree(timers);
1478 return ret;
1479}
1480
1481/* Initialize per-processor generic timer and memory-mapped timer(if present) */
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001482static int __init arch_timer_acpi_init(struct acpi_table_header *table)
1483{
Fu Weic2743a32017-04-01 01:51:04 +08001484 int ret, platform_timer_count;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001485
Fu Wei8a5c21d2017-01-18 21:25:26 +08001486 if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
Fu Weided24012017-01-18 21:25:25 +08001487 pr_warn("already initialized, skipping\n");
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001488 return -EINVAL;
1489 }
1490
Fu Wei8a5c21d2017-01-18 21:25:26 +08001491 arch_timers_present |= ARCH_TIMER_TYPE_CP15;
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001492
Fu Weic2743a32017-04-01 01:51:04 +08001493 ret = acpi_gtdt_init(table, &platform_timer_count);
Fu Weif79d2092017-04-01 01:51:02 +08001494 if (ret) {
1495 pr_err("Failed to init GTDT table.\n");
1496 return ret;
1497 }
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001498
Fu Weiee34f1e2017-01-18 21:25:27 +08001499 arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI] =
Fu Weif79d2092017-04-01 01:51:02 +08001500 acpi_gtdt_map_ppi(ARCH_TIMER_PHYS_NONSECURE_PPI);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001501
Fu Weiee34f1e2017-01-18 21:25:27 +08001502 arch_timer_ppi[ARCH_TIMER_VIRT_PPI] =
Fu Weif79d2092017-04-01 01:51:02 +08001503 acpi_gtdt_map_ppi(ARCH_TIMER_VIRT_PPI);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001504
Fu Weiee34f1e2017-01-18 21:25:27 +08001505 arch_timer_ppi[ARCH_TIMER_HYP_PPI] =
Fu Weif79d2092017-04-01 01:51:02 +08001506 acpi_gtdt_map_ppi(ARCH_TIMER_HYP_PPI);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001507
Fu Weica0e1b52017-03-22 00:31:15 +08001508 arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI];
1509
Fu Wei5d3dfa92017-03-22 00:31:13 +08001510 /*
1511 * When probing via ACPI, we have no mechanism to override the sysreg
1512 * CNTFRQ value. This *must* be correct.
1513 */
1514 arch_timer_rate = arch_timer_get_cntfrq();
1515 if (!arch_timer_rate) {
1516 pr_err(FW_BUG "frequency not available.\n");
1517 return -EINVAL;
1518 }
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001519
Fu Wei4502b6b2017-01-18 21:25:30 +08001520 arch_timer_uses_ppi = arch_timer_select_ppi();
1521 if (!arch_timer_ppi[arch_timer_uses_ppi]) {
1522 pr_err("No interrupt available, giving up\n");
1523 return -EINVAL;
1524 }
1525
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001526 /* Always-on capability */
Fu Weif79d2092017-04-01 01:51:02 +08001527 arch_timer_c3stop = acpi_gtdt_c3stop(arch_timer_uses_ppi);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001528
Marc Zyngier5a38bca2017-02-21 14:37:30 +00001529 /* Check for globally applicable workarounds */
1530 arch_timer_check_ool_workaround(ate_match_acpi_oem_info, table);
1531
Fu Weica0e1b52017-03-22 00:31:15 +08001532 ret = arch_timer_register();
1533 if (ret)
1534 return ret;
1535
Fu Weic2743a32017-04-01 01:51:04 +08001536 if (platform_timer_count &&
1537 arch_timer_mem_acpi_init(platform_timer_count))
1538 pr_err("Failed to initialize memory-mapped timer.\n");
1539
Fu Weica0e1b52017-03-22 00:31:15 +08001540 return arch_timer_common_init();
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001541}
Daniel Lezcano77d62f52017-05-26 17:42:25 +02001542TIMER_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001543#endif