blob: 73c487da6d2a9f0004043e23ff5e1e747672a065 [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
Scott Wood1d8f51d2016-09-22 03:35:18 -0500605 clocksource_counter.archdata.vdso_direct = true;
606
Scott Woodf6dc1572016-09-22 03:35:17 -0500607#ifdef CONFIG_FSL_ERRATUM_A008585
608 /*
609 * Don't use the vdso fastpath if errata require using
610 * the out-of-line counter accessor.
611 */
612 if (static_branch_unlikely(&arch_timer_read_ool_enabled))
Scott Wood1d8f51d2016-09-22 03:35:18 -0500613 clocksource_counter.archdata.vdso_direct = false;
Scott Woodf6dc1572016-09-22 03:35:17 -0500614#endif
Nathan Lynch423bd692014-09-29 01:50:06 +0200615 } else {
Stephen Boyd22006992013-07-18 16:59:32 -0700616 arch_timer_read_counter = arch_counter_get_cntvct_mem;
Nathan Lynch423bd692014-09-29 01:50:06 +0200617 }
618
Stephen Boyd22006992013-07-18 16:59:32 -0700619 start_count = arch_timer_read_counter();
620 clocksource_register_hz(&clocksource_counter, arch_timer_rate);
621 cyclecounter.mult = clocksource_counter.mult;
622 cyclecounter.shift = clocksource_counter.shift;
Julien Grallb4d6ce92016-04-11 16:32:51 +0100623 timecounter_init(&arch_timer_kvm_info.timecounter,
624 &cyclecounter, start_count);
Thierry Reding4a7d3e82013-10-15 15:31:51 +0200625
626 /* 56 bits minimum, so we assume worst case rollover */
627 sched_clock_register(arch_timer_read_counter, 56, arch_timer_rate);
Stephen Boyd22006992013-07-18 16:59:32 -0700628}
629
Paul Gortmaker8c37bb32013-06-19 11:32:08 -0400630static void arch_timer_stop(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000631{
632 pr_debug("arch_timer_teardown disable IRQ%d cpu #%d\n",
633 clk->irq, smp_processor_id());
634
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000635 disable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi]);
636 if (arch_timer_has_nonsecure_ppi())
637 disable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI]);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000638
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530639 clk->set_state_shutdown(clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000640}
641
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000642static int arch_timer_dying_cpu(unsigned int cpu)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000643{
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000644 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000645
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000646 arch_timer_stop(clk);
647 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000648}
649
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100650#ifdef CONFIG_CPU_PM
651static unsigned int saved_cntkctl;
652static int arch_timer_cpu_pm_notify(struct notifier_block *self,
653 unsigned long action, void *hcpu)
654{
655 if (action == CPU_PM_ENTER)
656 saved_cntkctl = arch_timer_get_cntkctl();
657 else if (action == CPU_PM_ENTER_FAILED || action == CPU_PM_EXIT)
658 arch_timer_set_cntkctl(saved_cntkctl);
659 return NOTIFY_OK;
660}
661
662static struct notifier_block arch_timer_cpu_pm_notifier = {
663 .notifier_call = arch_timer_cpu_pm_notify,
664};
665
666static int __init arch_timer_cpu_pm_init(void)
667{
668 return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier);
669}
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000670
671static void __init arch_timer_cpu_pm_deinit(void)
672{
673 WARN_ON(cpu_pm_unregister_notifier(&arch_timer_cpu_pm_notifier));
674}
675
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100676#else
677static int __init arch_timer_cpu_pm_init(void)
678{
679 return 0;
680}
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000681
682static void __init arch_timer_cpu_pm_deinit(void)
683{
684}
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100685#endif
686
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000687static int __init arch_timer_register(void)
688{
689 int err;
690 int ppi;
691
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000692 arch_timer_evt = alloc_percpu(struct clock_event_device);
693 if (!arch_timer_evt) {
694 err = -ENOMEM;
695 goto out;
696 }
697
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000698 ppi = arch_timer_ppi[arch_timer_uses_ppi];
699 switch (arch_timer_uses_ppi) {
700 case VIRT_PPI:
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000701 err = request_percpu_irq(ppi, arch_timer_handler_virt,
702 "arch_timer", arch_timer_evt);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000703 break;
704 case PHYS_SECURE_PPI:
705 case PHYS_NONSECURE_PPI:
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000706 err = request_percpu_irq(ppi, arch_timer_handler_phys,
707 "arch_timer", arch_timer_evt);
708 if (!err && arch_timer_ppi[PHYS_NONSECURE_PPI]) {
709 ppi = arch_timer_ppi[PHYS_NONSECURE_PPI];
710 err = request_percpu_irq(ppi, arch_timer_handler_phys,
711 "arch_timer", arch_timer_evt);
712 if (err)
713 free_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI],
714 arch_timer_evt);
715 }
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000716 break;
717 case HYP_PPI:
718 err = request_percpu_irq(ppi, arch_timer_handler_phys,
719 "arch_timer", arch_timer_evt);
720 break;
721 default:
722 BUG();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000723 }
724
725 if (err) {
726 pr_err("arch_timer: can't register interrupt %d (%d)\n",
727 ppi, err);
728 goto out_free;
729 }
730
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100731 err = arch_timer_cpu_pm_init();
732 if (err)
733 goto out_unreg_notify;
734
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000735
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000736 /* Register and immediately configure the timer on the boot CPU */
737 err = cpuhp_setup_state(CPUHP_AP_ARM_ARCH_TIMER_STARTING,
738 "AP_ARM_ARCH_TIMER_STARTING",
739 arch_timer_starting_cpu, arch_timer_dying_cpu);
740 if (err)
741 goto out_unreg_cpupm;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000742 return 0;
743
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000744out_unreg_cpupm:
745 arch_timer_cpu_pm_deinit();
746
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100747out_unreg_notify:
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000748 free_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], arch_timer_evt);
749 if (arch_timer_has_nonsecure_ppi())
750 free_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI],
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000751 arch_timer_evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000752
753out_free:
754 free_percpu(arch_timer_evt);
755out:
756 return err;
757}
758
Stephen Boyd22006992013-07-18 16:59:32 -0700759static int __init arch_timer_mem_register(void __iomem *base, unsigned int irq)
760{
761 int ret;
762 irq_handler_t func;
763 struct arch_timer *t;
764
765 t = kzalloc(sizeof(*t), GFP_KERNEL);
766 if (!t)
767 return -ENOMEM;
768
769 t->base = base;
770 t->evt.irq = irq;
771 __arch_timer_setup(ARCH_MEM_TIMER, &t->evt);
772
773 if (arch_timer_mem_use_virtual)
774 func = arch_timer_handler_virt_mem;
775 else
776 func = arch_timer_handler_phys_mem;
777
778 ret = request_irq(irq, func, IRQF_TIMER, "arch_mem_timer", &t->evt);
779 if (ret) {
780 pr_err("arch_timer: Failed to request mem timer irq\n");
781 kfree(t);
782 }
783
784 return ret;
785}
786
787static const struct of_device_id arch_timer_of_match[] __initconst = {
788 { .compatible = "arm,armv7-timer", },
789 { .compatible = "arm,armv8-timer", },
790 {},
791};
792
793static const struct of_device_id arch_timer_mem_of_match[] __initconst = {
794 { .compatible = "arm,armv7-timer-mem", },
795 {},
796};
797
Sudeep Hollac387f072014-09-29 01:50:05 +0200798static bool __init
Laurent Pinchart566e6df2015-03-31 12:12:22 +0200799arch_timer_needs_probing(int type, const struct of_device_id *matches)
Sudeep Hollac387f072014-09-29 01:50:05 +0200800{
801 struct device_node *dn;
Laurent Pinchart566e6df2015-03-31 12:12:22 +0200802 bool needs_probing = false;
Sudeep Hollac387f072014-09-29 01:50:05 +0200803
804 dn = of_find_matching_node(NULL, matches);
Marc Zyngier59aa8962014-10-15 16:06:20 +0100805 if (dn && of_device_is_available(dn) && !(arch_timers_present & type))
Laurent Pinchart566e6df2015-03-31 12:12:22 +0200806 needs_probing = true;
Sudeep Hollac387f072014-09-29 01:50:05 +0200807 of_node_put(dn);
808
Laurent Pinchart566e6df2015-03-31 12:12:22 +0200809 return needs_probing;
Sudeep Hollac387f072014-09-29 01:50:05 +0200810}
811
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200812static int __init arch_timer_common_init(void)
Stephen Boyd22006992013-07-18 16:59:32 -0700813{
814 unsigned mask = ARCH_CP15_TIMER | ARCH_MEM_TIMER;
815
816 /* Wait until both nodes are probed if we have two timers */
817 if ((arch_timers_present & mask) != mask) {
Laurent Pinchart566e6df2015-03-31 12:12:22 +0200818 if (arch_timer_needs_probing(ARCH_MEM_TIMER, arch_timer_mem_of_match))
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200819 return 0;
Laurent Pinchart566e6df2015-03-31 12:12:22 +0200820 if (arch_timer_needs_probing(ARCH_CP15_TIMER, arch_timer_of_match))
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200821 return 0;
Stephen Boyd22006992013-07-18 16:59:32 -0700822 }
823
824 arch_timer_banner(arch_timers_present);
825 arch_counter_register(arch_timers_present);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200826 return arch_timer_arch_init();
Stephen Boyd22006992013-07-18 16:59:32 -0700827}
828
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200829static int __init arch_timer_init(void)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000830{
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200831 int ret;
Doug Anderson65b57322014-10-08 00:33:47 -0700832 /*
Marc Zyngier82668912013-01-10 11:13:07 +0000833 * If HYP mode is available, we know that the physical timer
834 * has been configured to be accessible from PL1. Use it, so
835 * that a guest can use the virtual timer instead.
836 *
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000837 * If no interrupt provided for virtual timer, we'll have to
838 * stick to the physical timer. It'd better be accessible...
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000839 *
840 * On ARMv8.1 with VH extensions, the kernel runs in HYP. VHE
841 * accesses to CNTP_*_EL1 registers are silently redirected to
842 * their CNTHP_*_EL2 counterparts, and use a different PPI
843 * number.
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000844 */
Marc Zyngier82668912013-01-10 11:13:07 +0000845 if (is_hyp_mode_available() || !arch_timer_ppi[VIRT_PPI]) {
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000846 bool has_ppi;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000847
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000848 if (is_kernel_in_hyp_mode()) {
849 arch_timer_uses_ppi = HYP_PPI;
850 has_ppi = !!arch_timer_ppi[HYP_PPI];
851 } else {
852 arch_timer_uses_ppi = PHYS_SECURE_PPI;
853 has_ppi = (!!arch_timer_ppi[PHYS_SECURE_PPI] ||
854 !!arch_timer_ppi[PHYS_NONSECURE_PPI]);
855 }
856
857 if (!has_ppi) {
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000858 pr_warn("arch_timer: No interrupt available, giving up\n");
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200859 return -EINVAL;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000860 }
861 }
862
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200863 ret = arch_timer_register();
864 if (ret)
865 return ret;
866
867 ret = arch_timer_common_init();
868 if (ret)
869 return ret;
Julien Gralld9b5e412016-04-11 16:32:52 +0100870
871 arch_timer_kvm_info.virtual_irq = arch_timer_ppi[VIRT_PPI];
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200872
873 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000874}
Hanjun Guob09ca1e2015-03-24 14:02:50 +0000875
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200876static int __init arch_timer_of_init(struct device_node *np)
Hanjun Guob09ca1e2015-03-24 14:02:50 +0000877{
878 int i;
879
880 if (arch_timers_present & ARCH_CP15_TIMER) {
881 pr_warn("arch_timer: multiple nodes in dt, skipping\n");
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200882 return 0;
Hanjun Guob09ca1e2015-03-24 14:02:50 +0000883 }
884
885 arch_timers_present |= ARCH_CP15_TIMER;
886 for (i = PHYS_SECURE_PPI; i < MAX_TIMER_PPI; i++)
887 arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
888
889 arch_timer_detect_rate(NULL, np);
890
891 arch_timer_c3stop = !of_property_read_bool(np, "always-on");
892
Scott Woodf6dc1572016-09-22 03:35:17 -0500893#ifdef CONFIG_FSL_ERRATUM_A008585
894 if (fsl_a008585_enable < 0)
895 fsl_a008585_enable = of_property_read_bool(np, "fsl,erratum-a008585");
896 if (fsl_a008585_enable) {
897 static_branch_enable(&arch_timer_read_ool_enabled);
898 pr_info("Enabling workaround for FSL erratum A-008585\n");
899 }
900#endif
901
Hanjun Guob09ca1e2015-03-24 14:02:50 +0000902 /*
903 * If we cannot rely on firmware initializing the timer registers then
904 * we should use the physical timers instead.
905 */
906 if (IS_ENABLED(CONFIG_ARM) &&
907 of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000908 arch_timer_uses_ppi = PHYS_SECURE_PPI;
Hanjun Guob09ca1e2015-03-24 14:02:50 +0000909
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200910 return arch_timer_init();
Hanjun Guob09ca1e2015-03-24 14:02:50 +0000911}
Daniel Lezcano177cf6e2016-06-07 00:27:44 +0200912CLOCKSOURCE_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_of_init);
913CLOCKSOURCE_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_of_init);
Stephen Boyd22006992013-07-18 16:59:32 -0700914
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200915static int __init arch_timer_mem_init(struct device_node *np)
Stephen Boyd22006992013-07-18 16:59:32 -0700916{
917 struct device_node *frame, *best_frame = NULL;
918 void __iomem *cntctlbase, *base;
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200919 unsigned int irq, ret = -EINVAL;
Stephen Boyd22006992013-07-18 16:59:32 -0700920 u32 cnttidr;
921
922 arch_timers_present |= ARCH_MEM_TIMER;
923 cntctlbase = of_iomap(np, 0);
924 if (!cntctlbase) {
925 pr_err("arch_timer: Can't find CNTCTLBase\n");
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200926 return -ENXIO;
Stephen Boyd22006992013-07-18 16:59:32 -0700927 }
928
929 cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
Stephen Boyd22006992013-07-18 16:59:32 -0700930
931 /*
932 * Try to find a virtual capable frame. Otherwise fall back to a
933 * physical capable frame.
934 */
935 for_each_available_child_of_node(np, frame) {
936 int n;
Robin Murphye392d602016-02-01 12:00:48 +0000937 u32 cntacr;
Stephen Boyd22006992013-07-18 16:59:32 -0700938
939 if (of_property_read_u32(frame, "frame-number", &n)) {
940 pr_err("arch_timer: Missing frame-number\n");
Stephen Boyd22006992013-07-18 16:59:32 -0700941 of_node_put(frame);
Robin Murphye392d602016-02-01 12:00:48 +0000942 goto out;
Stephen Boyd22006992013-07-18 16:59:32 -0700943 }
944
Robin Murphye392d602016-02-01 12:00:48 +0000945 /* Try enabling everything, and see what sticks */
946 cntacr = CNTACR_RFRQ | CNTACR_RWPT | CNTACR_RPCT |
947 CNTACR_RWVT | CNTACR_RVOFF | CNTACR_RVCT;
948 writel_relaxed(cntacr, cntctlbase + CNTACR(n));
949 cntacr = readl_relaxed(cntctlbase + CNTACR(n));
950
951 if ((cnttidr & CNTTIDR_VIRT(n)) &&
952 !(~cntacr & (CNTACR_RWVT | CNTACR_RVCT))) {
Stephen Boyd22006992013-07-18 16:59:32 -0700953 of_node_put(best_frame);
954 best_frame = frame;
955 arch_timer_mem_use_virtual = true;
956 break;
957 }
Robin Murphye392d602016-02-01 12:00:48 +0000958
959 if (~cntacr & (CNTACR_RWPT | CNTACR_RPCT))
960 continue;
961
Stephen Boyd22006992013-07-18 16:59:32 -0700962 of_node_put(best_frame);
963 best_frame = of_node_get(frame);
964 }
965
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200966 ret= -ENXIO;
Stephen Boyd22006992013-07-18 16:59:32 -0700967 base = arch_counter_base = of_iomap(best_frame, 0);
968 if (!base) {
969 pr_err("arch_timer: Can't map frame's registers\n");
Robin Murphye392d602016-02-01 12:00:48 +0000970 goto out;
Stephen Boyd22006992013-07-18 16:59:32 -0700971 }
972
973 if (arch_timer_mem_use_virtual)
974 irq = irq_of_parse_and_map(best_frame, 1);
975 else
976 irq = irq_of_parse_and_map(best_frame, 0);
Robin Murphye392d602016-02-01 12:00:48 +0000977
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200978 ret = -EINVAL;
Stephen Boyd22006992013-07-18 16:59:32 -0700979 if (!irq) {
980 pr_err("arch_timer: Frame missing %s irq",
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200981 arch_timer_mem_use_virtual ? "virt" : "phys");
Robin Murphye392d602016-02-01 12:00:48 +0000982 goto out;
Stephen Boyd22006992013-07-18 16:59:32 -0700983 }
984
985 arch_timer_detect_rate(base, np);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200986 ret = arch_timer_mem_register(base, irq);
987 if (ret)
988 goto out;
989
990 return arch_timer_common_init();
Robin Murphye392d602016-02-01 12:00:48 +0000991out:
992 iounmap(cntctlbase);
993 of_node_put(best_frame);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200994 return ret;
Stephen Boyd22006992013-07-18 16:59:32 -0700995}
Daniel Lezcano177cf6e2016-06-07 00:27:44 +0200996CLOCKSOURCE_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem",
Stephen Boyd22006992013-07-18 16:59:32 -0700997 arch_timer_mem_init);
Hanjun Guob09ca1e2015-03-24 14:02:50 +0000998
999#ifdef CONFIG_ACPI
1000static int __init map_generic_timer_interrupt(u32 interrupt, u32 flags)
1001{
1002 int trigger, polarity;
1003
1004 if (!interrupt)
1005 return 0;
1006
1007 trigger = (flags & ACPI_GTDT_INTERRUPT_MODE) ? ACPI_EDGE_SENSITIVE
1008 : ACPI_LEVEL_SENSITIVE;
1009
1010 polarity = (flags & ACPI_GTDT_INTERRUPT_POLARITY) ? ACPI_ACTIVE_LOW
1011 : ACPI_ACTIVE_HIGH;
1012
1013 return acpi_register_gsi(NULL, interrupt, trigger, polarity);
1014}
1015
1016/* Initialize per-processor generic timer */
1017static int __init arch_timer_acpi_init(struct acpi_table_header *table)
1018{
1019 struct acpi_table_gtdt *gtdt;
1020
1021 if (arch_timers_present & ARCH_CP15_TIMER) {
1022 pr_warn("arch_timer: already initialized, skipping\n");
1023 return -EINVAL;
1024 }
1025
1026 gtdt = container_of(table, struct acpi_table_gtdt, header);
1027
1028 arch_timers_present |= ARCH_CP15_TIMER;
1029
1030 arch_timer_ppi[PHYS_SECURE_PPI] =
1031 map_generic_timer_interrupt(gtdt->secure_el1_interrupt,
1032 gtdt->secure_el1_flags);
1033
1034 arch_timer_ppi[PHYS_NONSECURE_PPI] =
1035 map_generic_timer_interrupt(gtdt->non_secure_el1_interrupt,
1036 gtdt->non_secure_el1_flags);
1037
1038 arch_timer_ppi[VIRT_PPI] =
1039 map_generic_timer_interrupt(gtdt->virtual_timer_interrupt,
1040 gtdt->virtual_timer_flags);
1041
1042 arch_timer_ppi[HYP_PPI] =
1043 map_generic_timer_interrupt(gtdt->non_secure_el2_interrupt,
1044 gtdt->non_secure_el2_flags);
1045
1046 /* Get the frequency from CNTFRQ */
1047 arch_timer_detect_rate(NULL, NULL);
1048
1049 /* Always-on capability */
1050 arch_timer_c3stop = !(gtdt->non_secure_el1_flags & ACPI_GTDT_ALWAYS_ON);
1051
1052 arch_timer_init();
1053 return 0;
1054}
Marc Zyngierae281cb2015-09-28 15:49:17 +01001055CLOCKSOURCE_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
Hanjun Guob09ca1e2015-03-24 14:02:50 +00001056#endif