blob: 57700541f95129e6f8f194ede23afeeda35842da [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
Stephen Boyd60faddf2013-07-18 16:59:31 -070097static __always_inline
98void arch_timer_reg_write(int access, enum arch_timer_reg reg, u32 val,
Thomas Gleixnercfb6d652013-08-21 14:59:23 +020099 struct clock_event_device *clk)
Stephen Boyd60faddf2013-07-18 16:59:31 -0700100{
Stephen Boyd22006992013-07-18 16:59:32 -0700101 if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
102 struct arch_timer *timer = to_arch_timer(clk);
103 switch (reg) {
104 case ARCH_TIMER_REG_CTRL:
105 writel_relaxed(val, timer->base + CNTP_CTL);
106 break;
107 case ARCH_TIMER_REG_TVAL:
108 writel_relaxed(val, timer->base + CNTP_TVAL);
109 break;
110 }
111 } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
112 struct arch_timer *timer = to_arch_timer(clk);
113 switch (reg) {
114 case ARCH_TIMER_REG_CTRL:
115 writel_relaxed(val, timer->base + CNTV_CTL);
116 break;
117 case ARCH_TIMER_REG_TVAL:
118 writel_relaxed(val, timer->base + CNTV_TVAL);
119 break;
120 }
121 } else {
122 arch_timer_reg_write_cp15(access, reg, val);
123 }
Stephen Boyd60faddf2013-07-18 16:59:31 -0700124}
125
126static __always_inline
127u32 arch_timer_reg_read(int access, enum arch_timer_reg reg,
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200128 struct clock_event_device *clk)
Stephen Boyd60faddf2013-07-18 16:59:31 -0700129{
Stephen Boyd22006992013-07-18 16:59:32 -0700130 u32 val;
131
132 if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
133 struct arch_timer *timer = to_arch_timer(clk);
134 switch (reg) {
135 case ARCH_TIMER_REG_CTRL:
136 val = readl_relaxed(timer->base + CNTP_CTL);
137 break;
138 case ARCH_TIMER_REG_TVAL:
139 val = readl_relaxed(timer->base + CNTP_TVAL);
140 break;
141 }
142 } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
143 struct arch_timer *timer = to_arch_timer(clk);
144 switch (reg) {
145 case ARCH_TIMER_REG_CTRL:
146 val = readl_relaxed(timer->base + CNTV_CTL);
147 break;
148 case ARCH_TIMER_REG_TVAL:
149 val = readl_relaxed(timer->base + CNTV_TVAL);
150 break;
151 }
152 } else {
153 val = arch_timer_reg_read_cp15(access, reg);
154 }
155
156 return val;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700157}
158
Stephen Boyde09f3cc2013-07-18 16:59:28 -0700159static __always_inline irqreturn_t timer_handler(const int access,
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000160 struct clock_event_device *evt)
161{
162 unsigned long ctrl;
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200163
Stephen Boyd60faddf2013-07-18 16:59:31 -0700164 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000165 if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
166 ctrl |= ARCH_TIMER_CTRL_IT_MASK;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700167 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000168 evt->event_handler(evt);
169 return IRQ_HANDLED;
170 }
171
172 return IRQ_NONE;
173}
174
175static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
176{
177 struct clock_event_device *evt = dev_id;
178
179 return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
180}
181
182static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
183{
184 struct clock_event_device *evt = dev_id;
185
186 return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
187}
188
Stephen Boyd22006992013-07-18 16:59:32 -0700189static irqreturn_t arch_timer_handler_phys_mem(int irq, void *dev_id)
190{
191 struct clock_event_device *evt = dev_id;
192
193 return timer_handler(ARCH_TIMER_MEM_PHYS_ACCESS, evt);
194}
195
196static irqreturn_t arch_timer_handler_virt_mem(int irq, void *dev_id)
197{
198 struct clock_event_device *evt = dev_id;
199
200 return timer_handler(ARCH_TIMER_MEM_VIRT_ACCESS, evt);
201}
202
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530203static __always_inline int timer_shutdown(const int access,
204 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000205{
206 unsigned long ctrl;
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530207
208 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
209 ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
210 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
211
212 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000213}
214
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530215static int arch_timer_shutdown_virt(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000216{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530217 return timer_shutdown(ARCH_TIMER_VIRT_ACCESS, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000218}
219
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530220static int arch_timer_shutdown_phys(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000221{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530222 return timer_shutdown(ARCH_TIMER_PHYS_ACCESS, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000223}
224
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530225static int arch_timer_shutdown_virt_mem(struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700226{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530227 return timer_shutdown(ARCH_TIMER_MEM_VIRT_ACCESS, clk);
Stephen Boyd22006992013-07-18 16:59:32 -0700228}
229
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530230static int arch_timer_shutdown_phys_mem(struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700231{
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530232 return timer_shutdown(ARCH_TIMER_MEM_PHYS_ACCESS, clk);
Stephen Boyd22006992013-07-18 16:59:32 -0700233}
234
Stephen Boyd60faddf2013-07-18 16:59:31 -0700235static __always_inline void set_next_event(const int access, unsigned long evt,
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200236 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000237{
238 unsigned long ctrl;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700239 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000240 ctrl |= ARCH_TIMER_CTRL_ENABLE;
241 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700242 arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt, clk);
243 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000244}
245
246static int arch_timer_set_next_event_virt(unsigned long evt,
Stephen Boyd60faddf2013-07-18 16:59:31 -0700247 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000248{
Stephen Boyd60faddf2013-07-18 16:59:31 -0700249 set_next_event(ARCH_TIMER_VIRT_ACCESS, evt, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000250 return 0;
251}
252
253static int arch_timer_set_next_event_phys(unsigned long evt,
Stephen Boyd60faddf2013-07-18 16:59:31 -0700254 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000255{
Stephen Boyd60faddf2013-07-18 16:59:31 -0700256 set_next_event(ARCH_TIMER_PHYS_ACCESS, evt, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000257 return 0;
258}
259
Stephen Boyd22006992013-07-18 16:59:32 -0700260static int arch_timer_set_next_event_virt_mem(unsigned long evt,
261 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000262{
Stephen Boyd22006992013-07-18 16:59:32 -0700263 set_next_event(ARCH_TIMER_MEM_VIRT_ACCESS, evt, clk);
264 return 0;
265}
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000266
Stephen Boyd22006992013-07-18 16:59:32 -0700267static int arch_timer_set_next_event_phys_mem(unsigned long evt,
268 struct clock_event_device *clk)
269{
270 set_next_event(ARCH_TIMER_MEM_PHYS_ACCESS, evt, clk);
271 return 0;
272}
273
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200274static void __arch_timer_setup(unsigned type,
275 struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700276{
277 clk->features = CLOCK_EVT_FEAT_ONESHOT;
278
279 if (type == ARCH_CP15_TIMER) {
Lorenzo Pieralisi82a561942014-04-08 10:04:32 +0100280 if (arch_timer_c3stop)
281 clk->features |= CLOCK_EVT_FEAT_C3STOP;
Stephen Boyd22006992013-07-18 16:59:32 -0700282 clk->name = "arch_sys_timer";
283 clk->rating = 450;
284 clk->cpumask = cpumask_of(smp_processor_id());
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000285 clk->irq = arch_timer_ppi[arch_timer_uses_ppi];
286 switch (arch_timer_uses_ppi) {
287 case VIRT_PPI:
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530288 clk->set_state_shutdown = arch_timer_shutdown_virt;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530289 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt;
Stephen Boyd22006992013-07-18 16:59:32 -0700290 clk->set_next_event = arch_timer_set_next_event_virt;
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000291 break;
292 case PHYS_SECURE_PPI:
293 case PHYS_NONSECURE_PPI:
294 case HYP_PPI:
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530295 clk->set_state_shutdown = arch_timer_shutdown_phys;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530296 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys;
Stephen Boyd22006992013-07-18 16:59:32 -0700297 clk->set_next_event = arch_timer_set_next_event_phys;
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000298 break;
299 default:
300 BUG();
Stephen Boyd22006992013-07-18 16:59:32 -0700301 }
302 } else {
Stephen Boyd7b52ad22014-01-06 14:56:17 -0800303 clk->features |= CLOCK_EVT_FEAT_DYNIRQ;
Stephen Boyd22006992013-07-18 16:59:32 -0700304 clk->name = "arch_mem_timer";
305 clk->rating = 400;
306 clk->cpumask = cpu_all_mask;
307 if (arch_timer_mem_use_virtual) {
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530308 clk->set_state_shutdown = arch_timer_shutdown_virt_mem;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530309 clk->set_state_oneshot_stopped = arch_timer_shutdown_virt_mem;
Stephen Boyd22006992013-07-18 16:59:32 -0700310 clk->set_next_event =
311 arch_timer_set_next_event_virt_mem;
312 } else {
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530313 clk->set_state_shutdown = arch_timer_shutdown_phys_mem;
Viresh Kumarcf8c5002015-12-23 16:59:12 +0530314 clk->set_state_oneshot_stopped = arch_timer_shutdown_phys_mem;
Stephen Boyd22006992013-07-18 16:59:32 -0700315 clk->set_next_event =
316 arch_timer_set_next_event_phys_mem;
317 }
318 }
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000319
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530320 clk->set_state_shutdown(clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000321
Stephen Boyd22006992013-07-18 16:59:32 -0700322 clockevents_config_and_register(clk, arch_timer_rate, 0xf, 0x7fffffff);
323}
324
Nathan Lynche1ce5c72014-09-29 01:50:06 +0200325static void arch_timer_evtstrm_enable(int divider)
326{
327 u32 cntkctl = arch_timer_get_cntkctl();
328
329 cntkctl &= ~ARCH_TIMER_EVT_TRIGGER_MASK;
330 /* Set the divider and enable virtual event stream */
331 cntkctl |= (divider << ARCH_TIMER_EVT_TRIGGER_SHIFT)
332 | ARCH_TIMER_VIRT_EVT_EN;
333 arch_timer_set_cntkctl(cntkctl);
334 elf_hwcap |= HWCAP_EVTSTRM;
335#ifdef CONFIG_COMPAT
336 compat_elf_hwcap |= COMPAT_HWCAP_EVTSTRM;
337#endif
338}
339
Will Deacon037f6372013-08-23 15:32:29 +0100340static void arch_timer_configure_evtstream(void)
341{
342 int evt_stream_div, pos;
343
344 /* Find the closest power of two to the divisor */
345 evt_stream_div = arch_timer_rate / ARCH_TIMER_EVT_STREAM_FREQ;
346 pos = fls(evt_stream_div);
347 if (pos > 1 && !(evt_stream_div & (1 << (pos - 2))))
348 pos--;
349 /* enable event stream */
350 arch_timer_evtstrm_enable(min(pos, 15));
351}
352
Nathan Lynch8b8dde02014-09-29 01:50:06 +0200353static void arch_counter_set_user_access(void)
354{
355 u32 cntkctl = arch_timer_get_cntkctl();
356
357 /* Disable user access to the timers and the physical counter */
358 /* Also disable virtual event stream */
359 cntkctl &= ~(ARCH_TIMER_USR_PT_ACCESS_EN
360 | ARCH_TIMER_USR_VT_ACCESS_EN
361 | ARCH_TIMER_VIRT_EVT_EN
362 | ARCH_TIMER_USR_PCT_ACCESS_EN);
363
364 /* Enable user access to the virtual counter */
365 cntkctl |= ARCH_TIMER_USR_VCT_ACCESS_EN;
366
367 arch_timer_set_cntkctl(cntkctl);
368}
369
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000370static bool arch_timer_has_nonsecure_ppi(void)
371{
372 return (arch_timer_uses_ppi == PHYS_SECURE_PPI &&
373 arch_timer_ppi[PHYS_NONSECURE_PPI]);
374}
375
Marc Zyngierf005bd72016-08-01 10:54:15 +0100376static u32 check_ppi_trigger(int irq)
377{
378 u32 flags = irq_get_trigger_type(irq);
379
380 if (flags != IRQF_TRIGGER_HIGH && flags != IRQF_TRIGGER_LOW) {
381 pr_warn("WARNING: Invalid trigger for IRQ%d, assuming level low\n", irq);
382 pr_warn("WARNING: Please fix your firmware\n");
383 flags = IRQF_TRIGGER_LOW;
384 }
385
386 return flags;
387}
388
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000389static int arch_timer_starting_cpu(unsigned int cpu)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000390{
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000391 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
Marc Zyngierf005bd72016-08-01 10:54:15 +0100392 u32 flags;
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000393
Stephen Boyd22006992013-07-18 16:59:32 -0700394 __arch_timer_setup(ARCH_CP15_TIMER, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000395
Marc Zyngierf005bd72016-08-01 10:54:15 +0100396 flags = check_ppi_trigger(arch_timer_ppi[arch_timer_uses_ppi]);
397 enable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], flags);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000398
Marc Zyngierf005bd72016-08-01 10:54:15 +0100399 if (arch_timer_has_nonsecure_ppi()) {
400 flags = check_ppi_trigger(arch_timer_ppi[PHYS_NONSECURE_PPI]);
401 enable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI], flags);
402 }
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000403
404 arch_counter_set_user_access();
Will Deacon46fd5c62016-06-27 17:30:13 +0100405 if (evtstrm_enable)
Will Deacon037f6372013-08-23 15:32:29 +0100406 arch_timer_configure_evtstream();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000407
408 return 0;
409}
410
Stephen Boyd22006992013-07-18 16:59:32 -0700411static void
412arch_timer_detect_rate(void __iomem *cntbase, struct device_node *np)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000413{
Stephen Boyd22006992013-07-18 16:59:32 -0700414 /* Who has more than one independent system counter? */
415 if (arch_timer_rate)
416 return;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000417
Hanjun Guob09ca1e2015-03-24 14:02:50 +0000418 /*
419 * Try to determine the frequency from the device tree or CNTFRQ,
420 * if ACPI is enabled, get the frequency from CNTFRQ ONLY.
421 */
422 if (!acpi_disabled ||
423 of_property_read_u32(np, "clock-frequency", &arch_timer_rate)) {
Stephen Boyd22006992013-07-18 16:59:32 -0700424 if (cntbase)
425 arch_timer_rate = readl_relaxed(cntbase + CNTFRQ);
426 else
427 arch_timer_rate = arch_timer_get_cntfrq();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000428 }
429
Stephen Boyd22006992013-07-18 16:59:32 -0700430 /* Check the timer frequency. */
431 if (arch_timer_rate == 0)
432 pr_warn("Architected timer frequency not available\n");
433}
434
435static void arch_timer_banner(unsigned type)
436{
437 pr_info("Architected %s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
438 type & ARCH_CP15_TIMER ? "cp15" : "",
439 type == (ARCH_CP15_TIMER | ARCH_MEM_TIMER) ? " and " : "",
440 type & ARCH_MEM_TIMER ? "mmio" : "",
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000441 (unsigned long)arch_timer_rate / 1000000,
442 (unsigned long)(arch_timer_rate / 10000) % 100,
Stephen Boyd22006992013-07-18 16:59:32 -0700443 type & ARCH_CP15_TIMER ?
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000444 (arch_timer_uses_ppi == VIRT_PPI) ? "virt" : "phys" :
Stephen Boyd22006992013-07-18 16:59:32 -0700445 "",
446 type == (ARCH_CP15_TIMER | ARCH_MEM_TIMER) ? "/" : "",
447 type & ARCH_MEM_TIMER ?
448 arch_timer_mem_use_virtual ? "virt" : "phys" :
449 "");
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000450}
451
452u32 arch_timer_get_rate(void)
453{
454 return arch_timer_rate;
455}
456
Stephen Boyd22006992013-07-18 16:59:32 -0700457static u64 arch_counter_get_cntvct_mem(void)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000458{
Stephen Boyd22006992013-07-18 16:59:32 -0700459 u32 vct_lo, vct_hi, tmp_hi;
460
461 do {
462 vct_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
463 vct_lo = readl_relaxed(arch_counter_base + CNTVCT_LO);
464 tmp_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
465 } while (vct_hi != tmp_hi);
466
467 return ((u64) vct_hi << 32) | vct_lo;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000468}
469
Stephen Boyd22006992013-07-18 16:59:32 -0700470/*
471 * Default to cp15 based access because arm64 uses this function for
472 * sched_clock() before DT is probed and the cp15 method is guaranteed
473 * to exist on arm64. arm doesn't use this before DT is probed so even
474 * if we don't have the cp15 accessors we won't have a problem.
475 */
476u64 (*arch_timer_read_counter)(void) = arch_counter_get_cntvct;
477
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000478static cycle_t arch_counter_read(struct clocksource *cs)
479{
Stephen Boyd22006992013-07-18 16:59:32 -0700480 return arch_timer_read_counter();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000481}
482
483static cycle_t arch_counter_read_cc(const struct cyclecounter *cc)
484{
Stephen Boyd22006992013-07-18 16:59:32 -0700485 return arch_timer_read_counter();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000486}
487
488static struct clocksource clocksource_counter = {
489 .name = "arch_sys_counter",
490 .rating = 400,
491 .read = arch_counter_read,
492 .mask = CLOCKSOURCE_MASK(56),
Stephen Boyd4fbcdc82013-09-27 13:13:12 -0700493 .flags = CLOCK_SOURCE_IS_CONTINUOUS | CLOCK_SOURCE_SUSPEND_NONSTOP,
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000494};
495
496static struct cyclecounter cyclecounter = {
497 .read = arch_counter_read_cc,
498 .mask = CLOCKSOURCE_MASK(56),
499};
500
Julien Grallb4d6ce92016-04-11 16:32:51 +0100501static struct arch_timer_kvm_info arch_timer_kvm_info;
502
503struct arch_timer_kvm_info *arch_timer_get_kvm_info(void)
504{
505 return &arch_timer_kvm_info;
506}
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000507
Stephen Boyd22006992013-07-18 16:59:32 -0700508static void __init arch_counter_register(unsigned type)
509{
510 u64 start_count;
511
512 /* Register the CP15 based counter if we have one */
Nathan Lynch423bd692014-09-29 01:50:06 +0200513 if (type & ARCH_CP15_TIMER) {
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000514 if (IS_ENABLED(CONFIG_ARM64) || arch_timer_uses_ppi == VIRT_PPI)
Sonny Rao0b46b8a2014-11-23 23:02:44 -0800515 arch_timer_read_counter = arch_counter_get_cntvct;
516 else
517 arch_timer_read_counter = arch_counter_get_cntpct;
Nathan Lynch423bd692014-09-29 01:50:06 +0200518 } else {
Stephen Boyd22006992013-07-18 16:59:32 -0700519 arch_timer_read_counter = arch_counter_get_cntvct_mem;
520
Nathan Lynch423bd692014-09-29 01:50:06 +0200521 /* If the clocksource name is "arch_sys_counter" the
522 * VDSO will attempt to read the CP15-based counter.
523 * Ensure this does not happen when CP15-based
524 * counter is not available.
525 */
526 clocksource_counter.name = "arch_mem_counter";
527 }
528
Stephen Boyd22006992013-07-18 16:59:32 -0700529 start_count = arch_timer_read_counter();
530 clocksource_register_hz(&clocksource_counter, arch_timer_rate);
531 cyclecounter.mult = clocksource_counter.mult;
532 cyclecounter.shift = clocksource_counter.shift;
Julien Grallb4d6ce92016-04-11 16:32:51 +0100533 timecounter_init(&arch_timer_kvm_info.timecounter,
534 &cyclecounter, start_count);
Thierry Reding4a7d3e82013-10-15 15:31:51 +0200535
536 /* 56 bits minimum, so we assume worst case rollover */
537 sched_clock_register(arch_timer_read_counter, 56, arch_timer_rate);
Stephen Boyd22006992013-07-18 16:59:32 -0700538}
539
Paul Gortmaker8c37bb32013-06-19 11:32:08 -0400540static void arch_timer_stop(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000541{
542 pr_debug("arch_timer_teardown disable IRQ%d cpu #%d\n",
543 clk->irq, smp_processor_id());
544
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000545 disable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi]);
546 if (arch_timer_has_nonsecure_ppi())
547 disable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI]);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000548
Viresh Kumar46c5bfd2015-06-12 13:30:12 +0530549 clk->set_state_shutdown(clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000550}
551
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000552static int arch_timer_dying_cpu(unsigned int cpu)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000553{
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000554 struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000555
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000556 arch_timer_stop(clk);
557 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000558}
559
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100560#ifdef CONFIG_CPU_PM
561static unsigned int saved_cntkctl;
562static int arch_timer_cpu_pm_notify(struct notifier_block *self,
563 unsigned long action, void *hcpu)
564{
565 if (action == CPU_PM_ENTER)
566 saved_cntkctl = arch_timer_get_cntkctl();
567 else if (action == CPU_PM_ENTER_FAILED || action == CPU_PM_EXIT)
568 arch_timer_set_cntkctl(saved_cntkctl);
569 return NOTIFY_OK;
570}
571
572static struct notifier_block arch_timer_cpu_pm_notifier = {
573 .notifier_call = arch_timer_cpu_pm_notify,
574};
575
576static int __init arch_timer_cpu_pm_init(void)
577{
578 return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier);
579}
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000580
581static void __init arch_timer_cpu_pm_deinit(void)
582{
583 WARN_ON(cpu_pm_unregister_notifier(&arch_timer_cpu_pm_notifier));
584}
585
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100586#else
587static int __init arch_timer_cpu_pm_init(void)
588{
589 return 0;
590}
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000591
592static void __init arch_timer_cpu_pm_deinit(void)
593{
594}
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100595#endif
596
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000597static int __init arch_timer_register(void)
598{
599 int err;
600 int ppi;
601
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000602 arch_timer_evt = alloc_percpu(struct clock_event_device);
603 if (!arch_timer_evt) {
604 err = -ENOMEM;
605 goto out;
606 }
607
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000608 ppi = arch_timer_ppi[arch_timer_uses_ppi];
609 switch (arch_timer_uses_ppi) {
610 case VIRT_PPI:
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000611 err = request_percpu_irq(ppi, arch_timer_handler_virt,
612 "arch_timer", arch_timer_evt);
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000613 break;
614 case PHYS_SECURE_PPI:
615 case PHYS_NONSECURE_PPI:
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000616 err = request_percpu_irq(ppi, arch_timer_handler_phys,
617 "arch_timer", arch_timer_evt);
618 if (!err && arch_timer_ppi[PHYS_NONSECURE_PPI]) {
619 ppi = arch_timer_ppi[PHYS_NONSECURE_PPI];
620 err = request_percpu_irq(ppi, arch_timer_handler_phys,
621 "arch_timer", arch_timer_evt);
622 if (err)
623 free_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI],
624 arch_timer_evt);
625 }
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000626 break;
627 case HYP_PPI:
628 err = request_percpu_irq(ppi, arch_timer_handler_phys,
629 "arch_timer", arch_timer_evt);
630 break;
631 default:
632 BUG();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000633 }
634
635 if (err) {
636 pr_err("arch_timer: can't register interrupt %d (%d)\n",
637 ppi, err);
638 goto out_free;
639 }
640
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100641 err = arch_timer_cpu_pm_init();
642 if (err)
643 goto out_unreg_notify;
644
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000645
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000646 /* Register and immediately configure the timer on the boot CPU */
647 err = cpuhp_setup_state(CPUHP_AP_ARM_ARCH_TIMER_STARTING,
648 "AP_ARM_ARCH_TIMER_STARTING",
649 arch_timer_starting_cpu, arch_timer_dying_cpu);
650 if (err)
651 goto out_unreg_cpupm;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000652 return 0;
653
Richard Cochran7e86e8b2016-07-13 17:16:39 +0000654out_unreg_cpupm:
655 arch_timer_cpu_pm_deinit();
656
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100657out_unreg_notify:
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000658 free_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], arch_timer_evt);
659 if (arch_timer_has_nonsecure_ppi())
660 free_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI],
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000661 arch_timer_evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000662
663out_free:
664 free_percpu(arch_timer_evt);
665out:
666 return err;
667}
668
Stephen Boyd22006992013-07-18 16:59:32 -0700669static int __init arch_timer_mem_register(void __iomem *base, unsigned int irq)
670{
671 int ret;
672 irq_handler_t func;
673 struct arch_timer *t;
674
675 t = kzalloc(sizeof(*t), GFP_KERNEL);
676 if (!t)
677 return -ENOMEM;
678
679 t->base = base;
680 t->evt.irq = irq;
681 __arch_timer_setup(ARCH_MEM_TIMER, &t->evt);
682
683 if (arch_timer_mem_use_virtual)
684 func = arch_timer_handler_virt_mem;
685 else
686 func = arch_timer_handler_phys_mem;
687
688 ret = request_irq(irq, func, IRQF_TIMER, "arch_mem_timer", &t->evt);
689 if (ret) {
690 pr_err("arch_timer: Failed to request mem timer irq\n");
691 kfree(t);
692 }
693
694 return ret;
695}
696
697static const struct of_device_id arch_timer_of_match[] __initconst = {
698 { .compatible = "arm,armv7-timer", },
699 { .compatible = "arm,armv8-timer", },
700 {},
701};
702
703static const struct of_device_id arch_timer_mem_of_match[] __initconst = {
704 { .compatible = "arm,armv7-timer-mem", },
705 {},
706};
707
Sudeep Hollac387f072014-09-29 01:50:05 +0200708static bool __init
Laurent Pinchart566e6df2015-03-31 12:12:22 +0200709arch_timer_needs_probing(int type, const struct of_device_id *matches)
Sudeep Hollac387f072014-09-29 01:50:05 +0200710{
711 struct device_node *dn;
Laurent Pinchart566e6df2015-03-31 12:12:22 +0200712 bool needs_probing = false;
Sudeep Hollac387f072014-09-29 01:50:05 +0200713
714 dn = of_find_matching_node(NULL, matches);
Marc Zyngier59aa8962014-10-15 16:06:20 +0100715 if (dn && of_device_is_available(dn) && !(arch_timers_present & type))
Laurent Pinchart566e6df2015-03-31 12:12:22 +0200716 needs_probing = true;
Sudeep Hollac387f072014-09-29 01:50:05 +0200717 of_node_put(dn);
718
Laurent Pinchart566e6df2015-03-31 12:12:22 +0200719 return needs_probing;
Sudeep Hollac387f072014-09-29 01:50:05 +0200720}
721
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200722static int __init arch_timer_common_init(void)
Stephen Boyd22006992013-07-18 16:59:32 -0700723{
724 unsigned mask = ARCH_CP15_TIMER | ARCH_MEM_TIMER;
725
726 /* Wait until both nodes are probed if we have two timers */
727 if ((arch_timers_present & mask) != mask) {
Laurent Pinchart566e6df2015-03-31 12:12:22 +0200728 if (arch_timer_needs_probing(ARCH_MEM_TIMER, arch_timer_mem_of_match))
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200729 return 0;
Laurent Pinchart566e6df2015-03-31 12:12:22 +0200730 if (arch_timer_needs_probing(ARCH_CP15_TIMER, arch_timer_of_match))
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200731 return 0;
Stephen Boyd22006992013-07-18 16:59:32 -0700732 }
733
734 arch_timer_banner(arch_timers_present);
735 arch_counter_register(arch_timers_present);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200736 return arch_timer_arch_init();
Stephen Boyd22006992013-07-18 16:59:32 -0700737}
738
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200739static int __init arch_timer_init(void)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000740{
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200741 int ret;
Doug Anderson65b57322014-10-08 00:33:47 -0700742 /*
Marc Zyngier82668912013-01-10 11:13:07 +0000743 * If HYP mode is available, we know that the physical timer
744 * has been configured to be accessible from PL1. Use it, so
745 * that a guest can use the virtual timer instead.
746 *
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000747 * If no interrupt provided for virtual timer, we'll have to
748 * stick to the physical timer. It'd better be accessible...
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000749 *
750 * On ARMv8.1 with VH extensions, the kernel runs in HYP. VHE
751 * accesses to CNTP_*_EL1 registers are silently redirected to
752 * their CNTHP_*_EL2 counterparts, and use a different PPI
753 * number.
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000754 */
Marc Zyngier82668912013-01-10 11:13:07 +0000755 if (is_hyp_mode_available() || !arch_timer_ppi[VIRT_PPI]) {
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000756 bool has_ppi;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000757
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000758 if (is_kernel_in_hyp_mode()) {
759 arch_timer_uses_ppi = HYP_PPI;
760 has_ppi = !!arch_timer_ppi[HYP_PPI];
761 } else {
762 arch_timer_uses_ppi = PHYS_SECURE_PPI;
763 has_ppi = (!!arch_timer_ppi[PHYS_SECURE_PPI] ||
764 !!arch_timer_ppi[PHYS_NONSECURE_PPI]);
765 }
766
767 if (!has_ppi) {
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000768 pr_warn("arch_timer: No interrupt available, giving up\n");
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200769 return -EINVAL;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000770 }
771 }
772
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200773 ret = arch_timer_register();
774 if (ret)
775 return ret;
776
777 ret = arch_timer_common_init();
778 if (ret)
779 return ret;
Julien Gralld9b5e412016-04-11 16:32:52 +0100780
781 arch_timer_kvm_info.virtual_irq = arch_timer_ppi[VIRT_PPI];
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200782
783 return 0;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000784}
Hanjun Guob09ca1e2015-03-24 14:02:50 +0000785
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200786static int __init arch_timer_of_init(struct device_node *np)
Hanjun Guob09ca1e2015-03-24 14:02:50 +0000787{
788 int i;
789
790 if (arch_timers_present & ARCH_CP15_TIMER) {
791 pr_warn("arch_timer: multiple nodes in dt, skipping\n");
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200792 return 0;
Hanjun Guob09ca1e2015-03-24 14:02:50 +0000793 }
794
795 arch_timers_present |= ARCH_CP15_TIMER;
796 for (i = PHYS_SECURE_PPI; i < MAX_TIMER_PPI; i++)
797 arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
798
799 arch_timer_detect_rate(NULL, np);
800
801 arch_timer_c3stop = !of_property_read_bool(np, "always-on");
802
803 /*
804 * If we cannot rely on firmware initializing the timer registers then
805 * we should use the physical timers instead.
806 */
807 if (IS_ENABLED(CONFIG_ARM) &&
808 of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
Marc Zyngierf81f03f2014-02-20 15:21:23 +0000809 arch_timer_uses_ppi = PHYS_SECURE_PPI;
Hanjun Guob09ca1e2015-03-24 14:02:50 +0000810
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200811 return arch_timer_init();
Hanjun Guob09ca1e2015-03-24 14:02:50 +0000812}
Daniel Lezcano177cf6e2016-06-07 00:27:44 +0200813CLOCKSOURCE_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_of_init);
814CLOCKSOURCE_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_of_init);
Stephen Boyd22006992013-07-18 16:59:32 -0700815
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200816static int __init arch_timer_mem_init(struct device_node *np)
Stephen Boyd22006992013-07-18 16:59:32 -0700817{
818 struct device_node *frame, *best_frame = NULL;
819 void __iomem *cntctlbase, *base;
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200820 unsigned int irq, ret = -EINVAL;
Stephen Boyd22006992013-07-18 16:59:32 -0700821 u32 cnttidr;
822
823 arch_timers_present |= ARCH_MEM_TIMER;
824 cntctlbase = of_iomap(np, 0);
825 if (!cntctlbase) {
826 pr_err("arch_timer: Can't find CNTCTLBase\n");
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200827 return -ENXIO;
Stephen Boyd22006992013-07-18 16:59:32 -0700828 }
829
830 cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
Stephen Boyd22006992013-07-18 16:59:32 -0700831
832 /*
833 * Try to find a virtual capable frame. Otherwise fall back to a
834 * physical capable frame.
835 */
836 for_each_available_child_of_node(np, frame) {
837 int n;
Robin Murphye392d602016-02-01 12:00:48 +0000838 u32 cntacr;
Stephen Boyd22006992013-07-18 16:59:32 -0700839
840 if (of_property_read_u32(frame, "frame-number", &n)) {
841 pr_err("arch_timer: Missing frame-number\n");
Stephen Boyd22006992013-07-18 16:59:32 -0700842 of_node_put(frame);
Robin Murphye392d602016-02-01 12:00:48 +0000843 goto out;
Stephen Boyd22006992013-07-18 16:59:32 -0700844 }
845
Robin Murphye392d602016-02-01 12:00:48 +0000846 /* Try enabling everything, and see what sticks */
847 cntacr = CNTACR_RFRQ | CNTACR_RWPT | CNTACR_RPCT |
848 CNTACR_RWVT | CNTACR_RVOFF | CNTACR_RVCT;
849 writel_relaxed(cntacr, cntctlbase + CNTACR(n));
850 cntacr = readl_relaxed(cntctlbase + CNTACR(n));
851
852 if ((cnttidr & CNTTIDR_VIRT(n)) &&
853 !(~cntacr & (CNTACR_RWVT | CNTACR_RVCT))) {
Stephen Boyd22006992013-07-18 16:59:32 -0700854 of_node_put(best_frame);
855 best_frame = frame;
856 arch_timer_mem_use_virtual = true;
857 break;
858 }
Robin Murphye392d602016-02-01 12:00:48 +0000859
860 if (~cntacr & (CNTACR_RWPT | CNTACR_RPCT))
861 continue;
862
Stephen Boyd22006992013-07-18 16:59:32 -0700863 of_node_put(best_frame);
864 best_frame = of_node_get(frame);
865 }
866
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200867 ret= -ENXIO;
Stephen Boyd22006992013-07-18 16:59:32 -0700868 base = arch_counter_base = of_iomap(best_frame, 0);
869 if (!base) {
870 pr_err("arch_timer: Can't map frame's registers\n");
Robin Murphye392d602016-02-01 12:00:48 +0000871 goto out;
Stephen Boyd22006992013-07-18 16:59:32 -0700872 }
873
874 if (arch_timer_mem_use_virtual)
875 irq = irq_of_parse_and_map(best_frame, 1);
876 else
877 irq = irq_of_parse_and_map(best_frame, 0);
Robin Murphye392d602016-02-01 12:00:48 +0000878
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200879 ret = -EINVAL;
Stephen Boyd22006992013-07-18 16:59:32 -0700880 if (!irq) {
881 pr_err("arch_timer: Frame missing %s irq",
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200882 arch_timer_mem_use_virtual ? "virt" : "phys");
Robin Murphye392d602016-02-01 12:00:48 +0000883 goto out;
Stephen Boyd22006992013-07-18 16:59:32 -0700884 }
885
886 arch_timer_detect_rate(base, np);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200887 ret = arch_timer_mem_register(base, irq);
888 if (ret)
889 goto out;
890
891 return arch_timer_common_init();
Robin Murphye392d602016-02-01 12:00:48 +0000892out:
893 iounmap(cntctlbase);
894 of_node_put(best_frame);
Daniel Lezcano3c0731d2016-06-06 17:55:40 +0200895 return ret;
Stephen Boyd22006992013-07-18 16:59:32 -0700896}
Daniel Lezcano177cf6e2016-06-07 00:27:44 +0200897CLOCKSOURCE_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem",
Stephen Boyd22006992013-07-18 16:59:32 -0700898 arch_timer_mem_init);
Hanjun Guob09ca1e2015-03-24 14:02:50 +0000899
900#ifdef CONFIG_ACPI
901static int __init map_generic_timer_interrupt(u32 interrupt, u32 flags)
902{
903 int trigger, polarity;
904
905 if (!interrupt)
906 return 0;
907
908 trigger = (flags & ACPI_GTDT_INTERRUPT_MODE) ? ACPI_EDGE_SENSITIVE
909 : ACPI_LEVEL_SENSITIVE;
910
911 polarity = (flags & ACPI_GTDT_INTERRUPT_POLARITY) ? ACPI_ACTIVE_LOW
912 : ACPI_ACTIVE_HIGH;
913
914 return acpi_register_gsi(NULL, interrupt, trigger, polarity);
915}
916
917/* Initialize per-processor generic timer */
918static int __init arch_timer_acpi_init(struct acpi_table_header *table)
919{
920 struct acpi_table_gtdt *gtdt;
921
922 if (arch_timers_present & ARCH_CP15_TIMER) {
923 pr_warn("arch_timer: already initialized, skipping\n");
924 return -EINVAL;
925 }
926
927 gtdt = container_of(table, struct acpi_table_gtdt, header);
928
929 arch_timers_present |= ARCH_CP15_TIMER;
930
931 arch_timer_ppi[PHYS_SECURE_PPI] =
932 map_generic_timer_interrupt(gtdt->secure_el1_interrupt,
933 gtdt->secure_el1_flags);
934
935 arch_timer_ppi[PHYS_NONSECURE_PPI] =
936 map_generic_timer_interrupt(gtdt->non_secure_el1_interrupt,
937 gtdt->non_secure_el1_flags);
938
939 arch_timer_ppi[VIRT_PPI] =
940 map_generic_timer_interrupt(gtdt->virtual_timer_interrupt,
941 gtdt->virtual_timer_flags);
942
943 arch_timer_ppi[HYP_PPI] =
944 map_generic_timer_interrupt(gtdt->non_secure_el2_interrupt,
945 gtdt->non_secure_el2_flags);
946
947 /* Get the frequency from CNTFRQ */
948 arch_timer_detect_rate(NULL, NULL);
949
950 /* Always-on capability */
951 arch_timer_c3stop = !(gtdt->non_secure_el1_flags & ACPI_GTDT_ALWAYS_ON);
952
953 arch_timer_init();
954 return 0;
955}
Marc Zyngierae281cb2015-09-28 15:49:17 +0100956CLOCKSOURCE_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
Hanjun Guob09ca1e2015-03-24 14:02:50 +0000957#endif