blob: eb5fb4121ac84f52d2a5daaed033ff4288e5a5dd [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>
Stephen Boyd65cd4f62013-07-18 16:21:18 -070027#include <linux/sched_clock.h>
Hanjun Guob09ca1e2015-03-24 14:02:50 +000028#include <linux/acpi.h>
Mark Rutland8a4da6e2012-11-12 14:33:44 +000029
30#include <asm/arch_timer.h>
Marc Zyngier82668912013-01-10 11:13:07 +000031#include <asm/virt.h>
Mark Rutland8a4da6e2012-11-12 14:33:44 +000032
33#include <clocksource/arm_arch_timer.h>
34
Stephen Boyd22006992013-07-18 16:59:32 -070035#define CNTTIDR 0x08
36#define CNTTIDR_VIRT(n) (BIT(1) << ((n) * 4))
37
Robin Murphye392d602016-02-01 12:00:48 +000038#define CNTACR(n) (0x40 + ((n) * 4))
39#define CNTACR_RPCT BIT(0)
40#define CNTACR_RVCT BIT(1)
41#define CNTACR_RFRQ BIT(2)
42#define CNTACR_RVOFF BIT(3)
43#define CNTACR_RWVT BIT(4)
44#define CNTACR_RWPT BIT(5)
45
Stephen Boyd22006992013-07-18 16:59:32 -070046#define CNTVCT_LO 0x08
47#define CNTVCT_HI 0x0c
48#define CNTFRQ 0x10
49#define CNTP_TVAL 0x28
50#define CNTP_CTL 0x2c
51#define CNTV_TVAL 0x38
52#define CNTV_CTL 0x3c
53
54#define ARCH_CP15_TIMER BIT(0)
55#define ARCH_MEM_TIMER BIT(1)
56static unsigned arch_timers_present __initdata;
57
58static void __iomem *arch_counter_base;
59
60struct arch_timer {
61 void __iomem *base;
62 struct clock_event_device evt;
63};
64
65#define to_arch_timer(e) container_of(e, struct arch_timer, evt)
66
Mark Rutland8a4da6e2012-11-12 14:33:44 +000067static u32 arch_timer_rate;
68
69enum ppi_nr {
70 PHYS_SECURE_PPI,
71 PHYS_NONSECURE_PPI,
72 VIRT_PPI,
73 HYP_PPI,
74 MAX_TIMER_PPI
75};
76
77static int arch_timer_ppi[MAX_TIMER_PPI];
78
79static struct clock_event_device __percpu *arch_timer_evt;
80
Marc Zyngierf81f03f2014-02-20 15:21:23 +000081static enum ppi_nr arch_timer_uses_ppi = VIRT_PPI;
Lorenzo Pieralisi82a561942014-04-08 10:04:32 +010082static bool arch_timer_c3stop;
Stephen Boyd22006992013-07-18 16:59:32 -070083static bool arch_timer_mem_use_virtual;
Mark Rutland8a4da6e2012-11-12 14:33:44 +000084
Will Deacon46fd5c62016-06-27 17:30:13 +010085static bool evtstrm_enable = IS_ENABLED(CONFIG_ARM_ARCH_TIMER_EVTSTREAM);
86
87static int __init early_evtstrm_cfg(char *buf)
88{
89 return strtobool(buf, &evtstrm_enable);
90}
91early_param("clocksource.arm_arch_timer.evtstrm", early_evtstrm_cfg);
92
Mark Rutland8a4da6e2012-11-12 14:33:44 +000093/*
94 * Architected system timer support.
95 */
96
Scott Woodf6dc1572016-09-22 03:35:17 -050097#ifdef CONFIG_FSL_ERRATUM_A008585
98DEFINE_STATIC_KEY_FALSE(arch_timer_read_ool_enabled);
99EXPORT_SYMBOL_GPL(arch_timer_read_ool_enabled);
100
101static int fsl_a008585_enable = -1;
102
103static int __init early_fsl_a008585_cfg(char *buf)
104{
105 int ret;
106 bool val;
107
108 ret = strtobool(buf, &val);
109 if (ret)
110 return ret;
111
112 fsl_a008585_enable = val;
113 return 0;
114}
115early_param("clocksource.arm_arch_timer.fsl-a008585", early_fsl_a008585_cfg);
116
117u32 __fsl_a008585_read_cntp_tval_el0(void)
118{
119 return __fsl_a008585_read_reg(cntp_tval_el0);
120}
121
122u32 __fsl_a008585_read_cntv_tval_el0(void)
123{
124 return __fsl_a008585_read_reg(cntv_tval_el0);
125}
126
127u64 __fsl_a008585_read_cntvct_el0(void)
128{
129 return __fsl_a008585_read_reg(cntvct_el0);
130}
131EXPORT_SYMBOL(__fsl_a008585_read_cntvct_el0);
132#endif /* CONFIG_FSL_ERRATUM_A008585 */
133
Stephen Boyd60faddf2013-07-18 16:59:31 -0700134static __always_inline
135void arch_timer_reg_write(int access, enum arch_timer_reg reg, u32 val,
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200136 struct clock_event_device *clk)
Stephen Boyd60faddf2013-07-18 16:59:31 -0700137{
Stephen Boyd22006992013-07-18 16:59:32 -0700138 if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
139 struct arch_timer *timer = to_arch_timer(clk);
140 switch (reg) {
141 case ARCH_TIMER_REG_CTRL:
142 writel_relaxed(val, timer->base + CNTP_CTL);
143 break;
144 case ARCH_TIMER_REG_TVAL:
145 writel_relaxed(val, timer->base + CNTP_TVAL);
146 break;
147 }
148 } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
149 struct arch_timer *timer = to_arch_timer(clk);
150 switch (reg) {
151 case ARCH_TIMER_REG_CTRL:
152 writel_relaxed(val, timer->base + CNTV_CTL);
153 break;
154 case ARCH_TIMER_REG_TVAL:
155 writel_relaxed(val, timer->base + CNTV_TVAL);
156 break;
157 }
158 } else {
159 arch_timer_reg_write_cp15(access, reg, val);
160 }
Stephen Boyd60faddf2013-07-18 16:59:31 -0700161}
162
163static __always_inline
164u32 arch_timer_reg_read(int access, enum arch_timer_reg reg,
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200165 struct clock_event_device *clk)
Stephen Boyd60faddf2013-07-18 16:59:31 -0700166{
Stephen Boyd22006992013-07-18 16:59:32 -0700167 u32 val;
168
169 if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
170 struct arch_timer *timer = to_arch_timer(clk);
171 switch (reg) {
172 case ARCH_TIMER_REG_CTRL:
173 val = readl_relaxed(timer->base + CNTP_CTL);
174 break;
175 case ARCH_TIMER_REG_TVAL:
176 val = readl_relaxed(timer->base + CNTP_TVAL);
177 break;
178 }
179 } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
180 struct arch_timer *timer = to_arch_timer(clk);
181 switch (reg) {
182 case ARCH_TIMER_REG_CTRL:
183 val = readl_relaxed(timer->base + CNTV_CTL);
184 break;
185 case ARCH_TIMER_REG_TVAL:
186 val = readl_relaxed(timer->base + CNTV_TVAL);
187 break;
188 }
189 } else {
190 val = arch_timer_reg_read_cp15(access, reg);
191 }
192
193 return val;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700194}
195
Stephen Boyde09f3cc2013-07-18 16:59:28 -0700196static __always_inline irqreturn_t timer_handler(const int access,
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000197 struct clock_event_device *evt)
198{
199 unsigned long ctrl;
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200200
Stephen Boyd60faddf2013-07-18 16:59:31 -0700201 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000202 if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
203 ctrl |= ARCH_TIMER_CTRL_IT_MASK;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700204 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000205 evt->event_handler(evt);
206 return IRQ_HANDLED;
207 }
208
209 return IRQ_NONE;
210}
211
212static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
213{
214 struct clock_event_device *evt = dev_id;
215
216 return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
217}
218
219static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
220{
221 struct clock_event_device *evt = dev_id;
222
223 return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
224}
225
Stephen Boyd22006992013-07-18 16:59:32 -0700226static irqreturn_t arch_timer_handler_phys_mem(int irq, void *dev_id)
227{
228 struct clock_event_device *evt = dev_id;
229
230 return timer_handler(ARCH_TIMER_MEM_PHYS_ACCESS, evt);
231}
232
233static irqreturn_t arch_timer_handler_virt_mem(int irq, void *dev_id)
234{
235 struct clock_event_device *evt = dev_id;
236
237 return timer_handler(ARCH_TIMER_MEM_VIRT_ACCESS, evt);
238}
239
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530240static __always_inline int timer_shutdown(const int access,
241 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000242{
243 unsigned long ctrl;
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530244
245 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
246 ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
247 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
248
249 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000250}
251
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530252static int arch_timer_shutdown_virt(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000253{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530254 return timer_shutdown(ARCH_TIMER_VIRT_ACCESS, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000255}
256
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530257static int arch_timer_shutdown_phys(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000258{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530259 return timer_shutdown(ARCH_TIMER_PHYS_ACCESS, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000260}
261
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530262static int arch_timer_shutdown_virt_mem(struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700263{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530264 return timer_shutdown(ARCH_TIMER_MEM_VIRT_ACCESS, clk);
Stephen Boyd22006992013-07-18 16:59:32 -0700265}
266
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530267static int arch_timer_shutdown_phys_mem(struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700268{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530269 return timer_shutdown(ARCH_TIMER_MEM_PHYS_ACCESS, clk);
Stephen Boyd22006992013-07-18 16:59:32 -0700270}
271
Stephen Boyd60faddf2013-07-18 16:59:31 -0700272static __always_inline void set_next_event(const int access, unsigned long evt,
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200273 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000274{
275 unsigned long ctrl;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700276 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000277 ctrl |= ARCH_TIMER_CTRL_ENABLE;
278 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700279 arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt, clk);
280 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000281}
282
Scott Woodf6dc1572016-09-22 03:35:17 -0500283#ifdef CONFIG_FSL_ERRATUM_A008585
284static __always_inline void fsl_a008585_set_next_event(const int access,
285 unsigned long evt, struct clock_event_device *clk)
286{
287 unsigned long ctrl;
288 u64 cval = evt + arch_counter_get_cntvct();
289
290 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
291 ctrl |= ARCH_TIMER_CTRL_ENABLE;
292 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
293
294 if (access == ARCH_TIMER_PHYS_ACCESS)
295 write_sysreg(cval, cntp_cval_el0);
296 else if (access == ARCH_TIMER_VIRT_ACCESS)
297 write_sysreg(cval, cntv_cval_el0);
298
299 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
300}
301
302static int fsl_a008585_set_next_event_virt(unsigned long evt,
303 struct clock_event_device *clk)
304{
305 fsl_a008585_set_next_event(ARCH_TIMER_VIRT_ACCESS, evt, clk);
306 return 0;
307}
308
309static int fsl_a008585_set_next_event_phys(unsigned long evt,
310 struct clock_event_device *clk)
311{
312 fsl_a008585_set_next_event(ARCH_TIMER_PHYS_ACCESS, evt, clk);
313 return 0;
314}
315#endif /* CONFIG_FSL_ERRATUM_A008585 */
316
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000317static int arch_timer_set_next_event_virt(unsigned long evt,
Stephen Boyd60faddf2013-07-18 16:59:31 -0700318 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000319{
Stephen Boyd60faddf2013-07-18 16:59:31 -0700320 set_next_event(ARCH_TIMER_VIRT_ACCESS, evt, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000321 return 0;
322}
323
324static int arch_timer_set_next_event_phys(unsigned long evt,
Stephen Boyd60faddf2013-07-18 16:59:31 -0700325 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000326{
Stephen Boyd60faddf2013-07-18 16:59:31 -0700327 set_next_event(ARCH_TIMER_PHYS_ACCESS, evt, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000328 return 0;
329}
330
Stephen Boyd22006992013-07-18 16:59:32 -0700331static int arch_timer_set_next_event_virt_mem(unsigned long evt,
332 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000333{
Stephen Boyd22006992013-07-18 16:59:32 -0700334 set_next_event(ARCH_TIMER_MEM_VIRT_ACCESS, evt, clk);
335 return 0;
336}
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000337
Stephen Boyd22006992013-07-18 16:59:32 -0700338static int arch_timer_set_next_event_phys_mem(unsigned long evt,
339 struct clock_event_device *clk)
340{
341 set_next_event(ARCH_TIMER_MEM_PHYS_ACCESS, evt, clk);
342 return 0;
343}
344
Scott Woodf6dc1572016-09-22 03:35:17 -0500345static void fsl_a008585_set_sne(struct clock_event_device *clk)
346{
347#ifdef CONFIG_FSL_ERRATUM_A008585
348 if (!static_branch_unlikely(&arch_timer_read_ool_enabled))
349 return;
350
351 if (arch_timer_uses_ppi == VIRT_PPI)
352 clk->set_next_event = fsl_a008585_set_next_event_virt;
353 else
354 clk->set_next_event = fsl_a008585_set_next_event_phys;
355#endif
356}
357
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200358static void __arch_timer_setup(unsigned type,
359 struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700360{
361 clk->features = CLOCK_EVT_FEAT_ONESHOT;
362
363 if (type == ARCH_CP15_TIMER) {
Lorenzo Pieralisi82a561942014-04-08 10:04:32 +0100364 if (arch_timer_c3stop)
365 clk->features |= CLOCK_EVT_FEAT_C3STOP;
Stephen Boyd22006992013-07-18 16:59:32 -0700366 clk->name = "arch_sys_timer";
367 clk->rating = 450;
368 clk->cpumask = cpumask_of(smp_processor_id());
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000369 clk->irq = arch_timer_ppi[arch_timer_uses_ppi];
370 switch (arch_timer_uses_ppi) {
371 case VIRT_PPI:
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530372 clk->set_state_shutdown = arch_timer_shutdown_virt;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530373 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt;
Stephen Boyd22006992013-07-18 16:59:32 -0700374 clk->set_next_event = arch_timer_set_next_event_virt;
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000375 break;
376 case PHYS_SECURE_PPI:
377 case PHYS_NONSECURE_PPI:
378 case HYP_PPI:
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530379 clk->set_state_shutdown = arch_timer_shutdown_phys;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530380 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys;
Stephen Boyd22006992013-07-18 16:59:32 -0700381 clk->set_next_event = arch_timer_set_next_event_phys;
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000382 break;
383 default:
384 BUG();
Stephen Boyd22006992013-07-18 16:59:32 -0700385 }
Scott Woodf6dc1572016-09-22 03:35:17 -0500386
387 fsl_a008585_set_sne(clk);
Stephen Boyd22006992013-07-18 16:59:32 -0700388 } else {
Stephen Boyd7b52ad22014-01-06 14:56:17 -0800389 clk->features |= CLOCK_EVT_FEAT_DYNIRQ;
Stephen Boyd22006992013-07-18 16:59:32 -0700390 clk->name = "arch_mem_timer";
391 clk->rating = 400;
392 clk->cpumask = cpu_all_mask;
393 if (arch_timer_mem_use_virtual) {
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530394 clk->set_state_shutdown = arch_timer_shutdown_virt_mem;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530395 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt_mem;
Stephen Boyd22006992013-07-18 16:59:32 -0700396 clk->set_next_event =
397 arch_timer_set_next_event_virt_mem;
398 } else {
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530399 clk->set_state_shutdown = arch_timer_shutdown_phys_mem;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530400 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys_mem;
Stephen Boyd22006992013-07-18 16:59:32 -0700401 clk->set_next_event =
402 arch_timer_set_next_event_phys_mem;
403 }
404 }
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000405
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530406 clk->set_state_shutdown(clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000407
Stephen Boyd22006992013-07-18 16:59:32 -0700408 clockevents_config_and_register(clk, arch_timer_rate, 0xf, 0x7fffffff);
409}
410
Nathan Lynche1ce5c72014-09-29 01:50:06 +0200411static void arch_timer_evtstrm_enable(int divider)
412{
413 u32 cntkctl = arch_timer_get_cntkctl();
414
415 cntkctl &= ~ARCH_TIMER_EVT_TRIGGER_MASK;
416 /* Set the divider and enable virtual event stream */
417 cntkctl |= (divider << ARCH_TIMER_EVT_TRIGGER_SHIFT)
418 | ARCH_TIMER_VIRT_EVT_EN;
419 arch_timer_set_cntkctl(cntkctl);
420 elf_hwcap |= HWCAP_EVTSTRM;
421#ifdef CONFIG_COMPAT
422 compat_elf_hwcap |= COMPAT_HWCAP_EVTSTRM;
423#endif
424}
425
Will Deacon037f6372013-08-23 15:32:29 +0100426static void arch_timer_configure_evtstream(void)
427{
428 int evt_stream_div, pos;
429
430 /* Find the closest power of two to the divisor */
431 evt_stream_div = arch_timer_rate / ARCH_TIMER_EVT_STREAM_FREQ;
432 pos = fls(evt_stream_div);
433 if (pos > 1 && !(evt_stream_div & (1 << (pos - 2))))
434 pos--;
435 /* enable event stream */
436 arch_timer_evtstrm_enable(min(pos, 15));
437}
438
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200439static void arch_counter_set_user_access(void)
440{
441 u32 cntkctl = arch_timer_get_cntkctl();
442
443 /* Disable user access to the timers and the physical counter */
444 /* Also disable virtual event stream */
445 cntkctl &= ~(ARCH_TIMER_USR_PT_ACCESS_EN
446 | ARCH_TIMER_USR_VT_ACCESS_EN
447 | ARCH_TIMER_VIRT_EVT_EN
448 | ARCH_TIMER_USR_PCT_ACCESS_EN);
449
450 /* Enable user access to the virtual counter */
451 cntkctl |= ARCH_TIMER_USR_VCT_ACCESS_EN;
452
453 arch_timer_set_cntkctl(cntkctl);
454}
455
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000456static bool arch_timer_has_nonsecure_ppi(void)
457{
458 return (arch_timer_uses_ppi == PHYS_SECURE_PPI &&
459 arch_timer_ppi[PHYS_NONSECURE_PPI]);
460}
461
Marc Zyngierf005bd72016-08-01 10:54:15 +0100462static u32 check_ppi_trigger(int irq)
463{
464 u32 flags = irq_get_trigger_type(irq);
465
466 if (flags != IRQF_TRIGGER_HIGH && flags != IRQF_TRIGGER_LOW) {
467 pr_warn("WARNING: Invalid trigger for IRQ%d, assuming level low\n", irq);
468 pr_warn("WARNING: Please fix your firmware\n");
469 flags = IRQF_TRIGGER_LOW;
470 }
471
472 return flags;
473}
474
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000475static int arch_timer_starting_cpu(unsigned int cpu)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000476{
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000477 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
Marc Zyngierf005bd72016-08-01 10:54:15 +0100478 u32 flags;
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000479
Stephen Boyd22006992013-07-18 16:59:32 -0700480 __arch_timer_setup(ARCH_CP15_TIMER, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000481
Marc Zyngierf005bd72016-08-01 10:54:15 +0100482 flags = check_ppi_trigger(arch_timer_ppi[arch_timer_uses_ppi]);
483 enable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], flags);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000484
Marc Zyngierf005bd72016-08-01 10:54:15 +0100485 if (arch_timer_has_nonsecure_ppi()) {
486 flags = check_ppi_trigger(arch_timer_ppi[PHYS_NONSECURE_PPI]);
487 enable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI], flags);
488 }
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000489
490 arch_counter_set_user_access();
Will Deacon46fd5c62016-06-27 17:30:13 +0100491 if (evtstrm_enable)
Will Deacon037f6372013-08-23 15:32:29 +0100492 arch_timer_configure_evtstream();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000493
494 return 0;
495}
496
Stephen Boyd22006992013-07-18 16:59:32 -0700497static void
498arch_timer_detect_rate(void __iomem *cntbase, struct device_node *np)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000499{
Stephen Boyd22006992013-07-18 16:59:32 -0700500 /* Who has more than one independent system counter? */
501 if (arch_timer_rate)
502 return;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000503
Hanjun Guob09ca1e2015-03-24 14:02:50 +0000504 /*
505 * Try to determine the frequency from the device tree or CNTFRQ,
506 * if ACPI is enabled, get the frequency from CNTFRQ ONLY.
507 */
508 if (!acpi_disabled ||
509 of_property_read_u32(np, "clock-frequency", &arch_timer_rate)) {
Stephen Boyd22006992013-07-18 16:59:32 -0700510 if (cntbase)
511 arch_timer_rate = readl_relaxed(cntbase + CNTFRQ);
512 else
513 arch_timer_rate = arch_timer_get_cntfrq();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000514 }
515
Stephen Boyd22006992013-07-18 16:59:32 -0700516 /* Check the timer frequency. */
517 if (arch_timer_rate == 0)
518 pr_warn("Architected timer frequency not available\n");
519}
520
521static void arch_timer_banner(unsigned type)
522{
523 pr_info("Architected %s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
524 type & ARCH_CP15_TIMER ? "cp15" : "",
525 type == (ARCH_CP15_TIMER | ARCH_MEM_TIMER) ? " and " : "",
526 type & ARCH_MEM_TIMER ? "mmio" : "",
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000527 (unsigned long)arch_timer_rate / 1000000,
528 (unsigned long)(arch_timer_rate / 10000) % 100,
Stephen Boyd22006992013-07-18 16:59:32 -0700529 type & ARCH_CP15_TIMER ?
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000530 (arch_timer_uses_ppi == VIRT_PPI) ? "virt" : "phys" :
Stephen Boyd22006992013-07-18 16:59:32 -0700531 "",
532 type == (ARCH_CP15_TIMER | ARCH_MEM_TIMER) ? "/" : "",
533 type & ARCH_MEM_TIMER ?
534 arch_timer_mem_use_virtual ? "virt" : "phys" :
535 "");
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000536}
537
538u32 arch_timer_get_rate(void)
539{
540 return arch_timer_rate;
541}
542
Stephen Boyd22006992013-07-18 16:59:32 -0700543static u64 arch_counter_get_cntvct_mem(void)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000544{
Stephen Boyd22006992013-07-18 16:59:32 -0700545 u32 vct_lo, vct_hi, tmp_hi;
546
547 do {
548 vct_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
549 vct_lo = readl_relaxed(arch_counter_base + CNTVCT_LO);
550 tmp_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
551 } while (vct_hi != tmp_hi);
552
553 return ((u64) vct_hi << 32) | vct_lo;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000554}
555
Stephen Boyd22006992013-07-18 16:59:32 -0700556/*
557 * Default to cp15 based access because arm64 uses this function for
558 * sched_clock() before DT is probed and the cp15 method is guaranteed
559 * to exist on arm64. arm doesn't use this before DT is probed so even
560 * if we don't have the cp15 accessors we won't have a problem.
561 */
562u64 (*arch_timer_read_counter)(void) = arch_counter_get_cntvct;
563
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000564static cycle_t arch_counter_read(struct clocksource *cs)
565{
Stephen Boyd22006992013-07-18 16:59:32 -0700566 return arch_timer_read_counter();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000567}
568
569static cycle_t arch_counter_read_cc(const struct cyclecounter *cc)
570{
Stephen Boyd22006992013-07-18 16:59:32 -0700571 return arch_timer_read_counter();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000572}
573
574static struct clocksource clocksource_counter = {
575 .name = "arch_sys_counter",
576 .rating = 400,
577 .read = arch_counter_read,
578 .mask = CLOCKSOURCE_MASK(56),
Stephen Boyd4fbcdc82013-09-27 13:13:12 -0700579 .flags = CLOCK_SOURCE_IS_CONTINUOUS | CLOCK_SOURCE_SUSPEND_NONSTOP,
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000580};
581
582static struct cyclecounter cyclecounter = {
583 .read = arch_counter_read_cc,
584 .mask = CLOCKSOURCE_MASK(56),
585};
586
Julien Grallb4d6ce92016-04-11 16:32:51 +0100587static struct arch_timer_kvm_info arch_timer_kvm_info;
588
589struct arch_timer_kvm_info *arch_timer_get_kvm_info(void)
590{
591 return &arch_timer_kvm_info;
592}
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000593
Stephen Boyd22006992013-07-18 16:59:32 -0700594static void __init arch_counter_register(unsigned type)
595{
596 u64 start_count;
597
598 /* Register the CP15 based counter if we have one */
Nathan Lynch423bd692014-09-29 01:50:06 +0200599 if (type & ARCH_CP15_TIMER) {
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000600 if (IS_ENABLED(CONFIG_ARM64) || arch_timer_uses_ppi == VIRT_PPI)
Sonny Rao0b46b8a2014-11-23 23:02:44 -0800601 arch_timer_read_counter = arch_counter_get_cntvct;
602 else
603 arch_timer_read_counter = arch_counter_get_cntpct;
Scott Woodf6dc1572016-09-22 03:35:17 -0500604
605#ifdef CONFIG_FSL_ERRATUM_A008585
606 /*
607 * Don't use the vdso fastpath if errata require using
608 * the out-of-line counter accessor.
609 */
610 if (static_branch_unlikely(&arch_timer_read_ool_enabled))
611 clocksource_counter.name = "arch_sys_counter_ool";
612#endif
Nathan Lynch423bd692014-09-29 01:50:06 +0200613 } else {
Stephen Boyd22006992013-07-18 16:59:32 -0700614 arch_timer_read_counter = arch_counter_get_cntvct_mem;
615
Nathan Lynch423bd692014-09-29 01:50:06 +0200616 /* If the clocksource name is "arch_sys_counter" the
617 * VDSO will attempt to read the CP15-based counter.
618 * Ensure this does not happen when CP15-based
619 * counter is not available.
620 */
621 clocksource_counter.name = "arch_mem_counter";
622 }
623
Stephen Boyd22006992013-07-18 16:59:32 -0700624 start_count = arch_timer_read_counter();
625 clocksource_register_hz(&clocksource_counter, arch_timer_rate);
626 cyclecounter.mult = clocksource_counter.mult;
627 cyclecounter.shift = clocksource_counter.shift;
Julien Grallb4d6ce92016-04-11 16:32:51 +0100628 timecounter_init(&arch_timer_kvm_info.timecounter,
629 &cyclecounter, start_count);
Thierry Reding4a7d3e82013-10-15 15:31:51 +0200630
631 /* 56 bits minimum, so we assume worst case rollover */
632 sched_clock_register(arch_timer_read_counter, 56, arch_timer_rate);
Stephen Boyd22006992013-07-18 16:59:32 -0700633}
634
Paul Gortmaker8c37bb32013-06-19 11:32:08 -0400635static void arch_timer_stop(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000636{
637 pr_debug("arch_timer_teardown disable IRQ%d cpu #%d\n",
638 clk->irq, smp_processor_id());
639
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000640 disable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi]);
641 if (arch_timer_has_nonsecure_ppi())
642 disable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI]);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000643
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530644 clk->set_state_shutdown(clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000645}
646
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000647static int arch_timer_dying_cpu(unsigned int cpu)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000648{
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000649 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000650
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000651 arch_timer_stop(clk);
652 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000653}
654
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100655#ifdef CONFIG_CPU_PM
656static unsigned int saved_cntkctl;
657static int arch_timer_cpu_pm_notify(struct notifier_block *self,
658 unsigned long action, void *hcpu)
659{
660 if (action == CPU_PM_ENTER)
661 saved_cntkctl = arch_timer_get_cntkctl();
662 else if (action == CPU_PM_ENTER_FAILED || action == CPU_PM_EXIT)
663 arch_timer_set_cntkctl(saved_cntkctl);
664 return NOTIFY_OK;
665}
666
667static struct notifier_block arch_timer_cpu_pm_notifier = {
668 .notifier_call = arch_timer_cpu_pm_notify,
669};
670
671static int __init arch_timer_cpu_pm_init(void)
672{
673 return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier);
674}
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000675
676static void __init arch_timer_cpu_pm_deinit(void)
677{
678 WARN_ON(cpu_pm_unregister_notifier(&arch_timer_cpu_pm_notifier));
679}
680
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100681#else
682static int __init arch_timer_cpu_pm_init(void)
683{
684 return 0;
685}
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000686
687static void __init arch_timer_cpu_pm_deinit(void)
688{
689}
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100690#endif
691
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000692static int __init arch_timer_register(void)
693{
694 int err;
695 int ppi;
696
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000697 arch_timer_evt = alloc_percpu(struct clock_event_device);
698 if (!arch_timer_evt) {
699 err = -ENOMEM;
700 goto out;
701 }
702
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000703 ppi = arch_timer_ppi[arch_timer_uses_ppi];
704 switch (arch_timer_uses_ppi) {
705 case VIRT_PPI:
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000706 err = request_percpu_irq(ppi, arch_timer_handler_virt,
707 "arch_timer", arch_timer_evt);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000708 break;
709 case PHYS_SECURE_PPI:
710 case PHYS_NONSECURE_PPI:
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000711 err = request_percpu_irq(ppi, arch_timer_handler_phys,
712 "arch_timer", arch_timer_evt);
713 if (!err && arch_timer_ppi[PHYS_NONSECURE_PPI]) {
714 ppi = arch_timer_ppi[PHYS_NONSECURE_PPI];
715 err = request_percpu_irq(ppi, arch_timer_handler_phys,
716 "arch_timer", arch_timer_evt);
717 if (err)
718 free_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI],
719 arch_timer_evt);
720 }
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000721 break;
722 case HYP_PPI:
723 err = request_percpu_irq(ppi, arch_timer_handler_phys,
724 "arch_timer", arch_timer_evt);
725 break;
726 default:
727 BUG();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000728 }
729
730 if (err) {
731 pr_err("arch_timer: can't register interrupt %d (%d)\n",
732 ppi, err);
733 goto out_free;
734 }
735
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100736 err = arch_timer_cpu_pm_init();
737 if (err)
738 goto out_unreg_notify;
739
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000740
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000741 /* Register and immediately configure the timer on the boot CPU */
742 err = cpuhp_setup_state(CPUHP_AP_ARM_ARCH_TIMER_STARTING,
743 "AP_ARM_ARCH_TIMER_STARTING",
744 arch_timer_starting_cpu, arch_timer_dying_cpu);
745 if (err)
746 goto out_unreg_cpupm;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000747 return 0;
748
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000749out_unreg_cpupm:
750 arch_timer_cpu_pm_deinit();
751
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100752out_unreg_notify:
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000753 free_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], arch_timer_evt);
754 if (arch_timer_has_nonsecure_ppi())
755 free_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI],
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000756 arch_timer_evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000757
758out_free:
759 free_percpu(arch_timer_evt);
760out:
761 return err;
762}
763
Stephen Boyd22006992013-07-18 16:59:32 -0700764static int __init arch_timer_mem_register(void __iomem *base, unsigned int irq)
765{
766 int ret;
767 irq_handler_t func;
768 struct arch_timer *t;
769
770 t = kzalloc(sizeof(*t), GFP_KERNEL);
771 if (!t)
772 return -ENOMEM;
773
774 t->base = base;
775 t->evt.irq = irq;
776 __arch_timer_setup(ARCH_MEM_TIMER, &t->evt);
777
778 if (arch_timer_mem_use_virtual)
779 func = arch_timer_handler_virt_mem;
780 else
781 func = arch_timer_handler_phys_mem;
782
783 ret = request_irq(irq, func, IRQF_TIMER, "arch_mem_timer", &t->evt);
784 if (ret) {
785 pr_err("arch_timer: Failed to request mem timer irq\n");
786 kfree(t);
787 }
788
789 return ret;
790}
791
792static const struct of_device_id arch_timer_of_match[] __initconst = {
793 { .compatible = "arm,armv7-timer", },
794 { .compatible = "arm,armv8-timer", },
795 {},
796};
797
798static const struct of_device_id arch_timer_mem_of_match[] __initconst = {
799 { .compatible = "arm,armv7-timer-mem", },
800 {},
801};
802
Sudeep Hollac387f072014-09-29 01:50:05 +0200803static bool __init
Laurent Pinchart566e6df2015-03-31 12:12:22 +0200804arch_timer_needs_probing(int type, const struct of_device_id *matches)
Sudeep Hollac387f072014-09-29 01:50:05 +0200805{
806 struct device_node *dn;
Laurent Pinchart566e6df2015-03-31 12:12:22 +0200807 bool needs_probing = false;
Sudeep Hollac387f072014-09-29 01:50:05 +0200808
809 dn = of_find_matching_node(NULL, matches);
Marc Zyngier59aa8962014-10-15 16:06:20 +0100810 if (dn && of_device_is_available(dn) && !(arch_timers_present & type))
Laurent Pinchart566e6df2015-03-31 12:12:22 +0200811 needs_probing = true;
Sudeep Hollac387f072014-09-29 01:50:05 +0200812 of_node_put(dn);
813
Laurent Pinchart566e6df2015-03-31 12:12:22 +0200814 return needs_probing;
Sudeep Hollac387f072014-09-29 01:50:05 +0200815}
816
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200817static int __init arch_timer_common_init(void)
Stephen Boyd22006992013-07-18 16:59:32 -0700818{
819 unsigned mask = ARCH_CP15_TIMER | ARCH_MEM_TIMER;
820
821 /* Wait until both nodes are probed if we have two timers */
822 if ((arch_timers_present & mask) != mask) {
Laurent Pinchart566e6df2015-03-31 12:12:22 +0200823 if (arch_timer_needs_probing(ARCH_MEM_TIMER, arch_timer_mem_of_match))
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200824 return 0;
Laurent Pinchart566e6df2015-03-31 12:12:22 +0200825 if (arch_timer_needs_probing(ARCH_CP15_TIMER, arch_timer_of_match))
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200826 return 0;
Stephen Boyd22006992013-07-18 16:59:32 -0700827 }
828
829 arch_timer_banner(arch_timers_present);
830 arch_counter_register(arch_timers_present);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200831 return arch_timer_arch_init();
Stephen Boyd22006992013-07-18 16:59:32 -0700832}
833
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200834static int __init arch_timer_init(void)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000835{
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200836 int ret;
Doug Anderson65b57322014-10-08 00:33:47 -0700837 /*
Marc Zyngier82668912013-01-10 11:13:07 +0000838 * If HYP mode is available, we know that the physical timer
839 * has been configured to be accessible from PL1. Use it, so
840 * that a guest can use the virtual timer instead.
841 *
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000842 * If no interrupt provided for virtual timer, we'll have to
843 * stick to the physical timer. It'd better be accessible...
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000844 *
845 * On ARMv8.1 with VH extensions, the kernel runs in HYP. VHE
846 * accesses to CNTP_*_EL1 registers are silently redirected to
847 * their CNTHP_*_EL2 counterparts, and use a different PPI
848 * number.
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000849 */
Marc Zyngier82668912013-01-10 11:13:07 +0000850 if (is_hyp_mode_available() || !arch_timer_ppi[VIRT_PPI]) {
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000851 bool has_ppi;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000852
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000853 if (is_kernel_in_hyp_mode()) {
854 arch_timer_uses_ppi = HYP_PPI;
855 has_ppi = !!arch_timer_ppi[HYP_PPI];
856 } else {
857 arch_timer_uses_ppi = PHYS_SECURE_PPI;
858 has_ppi = (!!arch_timer_ppi[PHYS_SECURE_PPI] ||
859 !!arch_timer_ppi[PHYS_NONSECURE_PPI]);
860 }
861
862 if (!has_ppi) {
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000863 pr_warn("arch_timer: No interrupt available, giving up\n");
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200864 return -EINVAL;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000865 }
866 }
867
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200868 ret = arch_timer_register();
869 if (ret)
870 return ret;
871
872 ret = arch_timer_common_init();
873 if (ret)
874 return ret;
Julien Gralld9b5e412016-04-11 16:32:52 +0100875
876 arch_timer_kvm_info.virtual_irq = arch_timer_ppi[VIRT_PPI];
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200877
878 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000879}
Hanjun Guob09ca1e2015-03-24 14:02:50 +0000880
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200881static int __init arch_timer_of_init(struct device_node *np)
Hanjun Guob09ca1e2015-03-24 14:02:50 +0000882{
883 int i;
884
885 if (arch_timers_present & ARCH_CP15_TIMER) {
886 pr_warn("arch_timer: multiple nodes in dt, skipping\n");
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200887 return 0;
Hanjun Guob09ca1e2015-03-24 14:02:50 +0000888 }
889
890 arch_timers_present |= ARCH_CP15_TIMER;
891 for (i = PHYS_SECURE_PPI; i < MAX_TIMER_PPI; i++)
892 arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
893
894 arch_timer_detect_rate(NULL, np);
895
896 arch_timer_c3stop = !of_property_read_bool(np, "always-on");
897
Scott Woodf6dc1572016-09-22 03:35:17 -0500898#ifdef CONFIG_FSL_ERRATUM_A008585
899 if (fsl_a008585_enable < 0)
900 fsl_a008585_enable = of_property_read_bool(np, "fsl,erratum-a008585");
901 if (fsl_a008585_enable) {
902 static_branch_enable(&arch_timer_read_ool_enabled);
903 pr_info("Enabling workaround for FSL erratum A-008585\n");
904 }
905#endif
906
Hanjun Guob09ca1e2015-03-24 14:02:50 +0000907 /*
908 * If we cannot rely on firmware initializing the timer registers then
909 * we should use the physical timers instead.
910 */
911 if (IS_ENABLED(CONFIG_ARM) &&
912 of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000913 arch_timer_uses_ppi = PHYS_SECURE_PPI;
Hanjun Guob09ca1e2015-03-24 14:02:50 +0000914
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200915 return arch_timer_init();
Hanjun Guob09ca1e2015-03-24 14:02:50 +0000916}
Daniel Lezcano177cf6e2016-06-07 00:27:44 +0200917CLOCKSOURCE_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_of_init);
918CLOCKSOURCE_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_of_init);
Stephen Boyd22006992013-07-18 16:59:32 -0700919
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200920static int __init arch_timer_mem_init(struct device_node *np)
Stephen Boyd22006992013-07-18 16:59:32 -0700921{
922 struct device_node *frame, *best_frame = NULL;
923 void __iomem *cntctlbase, *base;
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200924 unsigned int irq, ret = -EINVAL;
Stephen Boyd22006992013-07-18 16:59:32 -0700925 u32 cnttidr;
926
927 arch_timers_present |= ARCH_MEM_TIMER;
928 cntctlbase = of_iomap(np, 0);
929 if (!cntctlbase) {
930 pr_err("arch_timer: Can't find CNTCTLBase\n");
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200931 return -ENXIO;
Stephen Boyd22006992013-07-18 16:59:32 -0700932 }
933
934 cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
Stephen Boyd22006992013-07-18 16:59:32 -0700935
936 /*
937 * Try to find a virtual capable frame. Otherwise fall back to a
938 * physical capable frame.
939 */
940 for_each_available_child_of_node(np, frame) {
941 int n;
Robin Murphye392d602016-02-01 12:00:48 +0000942 u32 cntacr;
Stephen Boyd22006992013-07-18 16:59:32 -0700943
944 if (of_property_read_u32(frame, "frame-number", &n)) {
945 pr_err("arch_timer: Missing frame-number\n");
Stephen Boyd22006992013-07-18 16:59:32 -0700946 of_node_put(frame);
Robin Murphye392d602016-02-01 12:00:48 +0000947 goto out;
Stephen Boyd22006992013-07-18 16:59:32 -0700948 }
949
Robin Murphye392d602016-02-01 12:00:48 +0000950 /* Try enabling everything, and see what sticks */
951 cntacr = CNTACR_RFRQ | CNTACR_RWPT | CNTACR_RPCT |
952 CNTACR_RWVT | CNTACR_RVOFF | CNTACR_RVCT;
953 writel_relaxed(cntacr, cntctlbase + CNTACR(n));
954 cntacr = readl_relaxed(cntctlbase + CNTACR(n));
955
956 if ((cnttidr & CNTTIDR_VIRT(n)) &&
957 !(~cntacr & (CNTACR_RWVT | CNTACR_RVCT))) {
Stephen Boyd22006992013-07-18 16:59:32 -0700958 of_node_put(best_frame);
959 best_frame = frame;
960 arch_timer_mem_use_virtual = true;
961 break;
962 }
Robin Murphye392d602016-02-01 12:00:48 +0000963
964 if (~cntacr & (CNTACR_RWPT | CNTACR_RPCT))
965 continue;
966
Stephen Boyd22006992013-07-18 16:59:32 -0700967 of_node_put(best_frame);
968 best_frame = of_node_get(frame);
969 }
970
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200971 ret= -ENXIO;
Stephen Boyd22006992013-07-18 16:59:32 -0700972 base = arch_counter_base = of_iomap(best_frame, 0);
973 if (!base) {
974 pr_err("arch_timer: Can't map frame's registers\n");
Robin Murphye392d602016-02-01 12:00:48 +0000975 goto out;
Stephen Boyd22006992013-07-18 16:59:32 -0700976 }
977
978 if (arch_timer_mem_use_virtual)
979 irq = irq_of_parse_and_map(best_frame, 1);
980 else
981 irq = irq_of_parse_and_map(best_frame, 0);
Robin Murphye392d602016-02-01 12:00:48 +0000982
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200983 ret = -EINVAL;
Stephen Boyd22006992013-07-18 16:59:32 -0700984 if (!irq) {
985 pr_err("arch_timer: Frame missing %s irq",
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200986 arch_timer_mem_use_virtual ? "virt" : "phys");
Robin Murphye392d602016-02-01 12:00:48 +0000987 goto out;
Stephen Boyd22006992013-07-18 16:59:32 -0700988 }
989
990 arch_timer_detect_rate(base, np);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200991 ret = arch_timer_mem_register(base, irq);
992 if (ret)
993 goto out;
994
995 return arch_timer_common_init();
Robin Murphye392d602016-02-01 12:00:48 +0000996out:
997 iounmap(cntctlbase);
998 of_node_put(best_frame);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200999 return ret;
Stephen Boyd22006992013-07-18 16:59:32 -07001000}
Daniel Lezcano177cf6e2016-06-07 00:27:44 +02001001CLOCKSOURCE_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem",
Stephen Boyd22006992013-07-18 16:59:32 -07001002 arch_timer_mem_init);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001003
1004#ifdef CONFIG_ACPI
1005static int __init map_generic_timer_interrupt(u32 interrupt, u32 flags)
1006{
1007 int trigger, polarity;
1008
1009 if (!interrupt)
1010 return 0;
1011
1012 trigger = (flags & ACPI_GTDT_INTERRUPT_MODE) ? ACPI_EDGE_SENSITIVE
1013 : ACPI_LEVEL_SENSITIVE;
1014
1015 polarity = (flags & ACPI_GTDT_INTERRUPT_POLARITY) ? ACPI_ACTIVE_LOW
1016 : ACPI_ACTIVE_HIGH;
1017
1018 return acpi_register_gsi(NULL, interrupt, trigger, polarity);
1019}
1020
1021/* Initialize per-processor generic timer */
1022static int __init arch_timer_acpi_init(struct acpi_table_header *table)
1023{
1024 struct acpi_table_gtdt *gtdt;
1025
1026 if (arch_timers_present & ARCH_CP15_TIMER) {
1027 pr_warn("arch_timer: already initialized, skipping\n");
1028 return -EINVAL;
1029 }
1030
1031 gtdt = container_of(table, struct acpi_table_gtdt, header);
1032
1033 arch_timers_present |= ARCH_CP15_TIMER;
1034
1035 arch_timer_ppi[PHYS_SECURE_PPI] =
1036 map_generic_timer_interrupt(gtdt->secure_el1_interrupt,
1037 gtdt->secure_el1_flags);
1038
1039 arch_timer_ppi[PHYS_NONSECURE_PPI] =
1040 map_generic_timer_interrupt(gtdt->non_secure_el1_interrupt,
1041 gtdt->non_secure_el1_flags);
1042
1043 arch_timer_ppi[VIRT_PPI] =
1044 map_generic_timer_interrupt(gtdt->virtual_timer_interrupt,
1045 gtdt->virtual_timer_flags);
1046
1047 arch_timer_ppi[HYP_PPI] =
1048 map_generic_timer_interrupt(gtdt->non_secure_el2_interrupt,
1049 gtdt->non_secure_el2_flags);
1050
1051 /* Get the frequency from CNTFRQ */
1052 arch_timer_detect_rate(NULL, NULL);
1053
1054 /* Always-on capability */
1055 arch_timer_c3stop = !(gtdt->non_secure_el1_flags & ACPI_GTDT_ALWAYS_ON);
1056
1057 arch_timer_init();
1058 return 0;
1059}
Marc Zyngierae281cb2015-09-28 15:49:17 +01001060CLOCKSOURCE_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001061#endif