blob: 9338f1ad61007353a876919d92ea0d4398685574 [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 */
11#include <linux/init.h>
12#include <linux/kernel.h>
13#include <linux/device.h>
14#include <linux/smp.h>
15#include <linux/cpu.h>
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +010016#include <linux/cpu_pm.h>
Mark Rutland8a4da6e2012-11-12 14:33:44 +000017#include <linux/clockchips.h>
18#include <linux/interrupt.h>
19#include <linux/of_irq.h>
Stephen Boyd22006992013-07-18 16:59:32 -070020#include <linux/of_address.h>
Mark Rutland8a4da6e2012-11-12 14:33:44 +000021#include <linux/io.h>
Stephen Boyd22006992013-07-18 16:59:32 -070022#include <linux/slab.h>
Mark Rutland8a4da6e2012-11-12 14:33:44 +000023
24#include <asm/arch_timer.h>
Marc Zyngier82668912013-01-10 11:13:07 +000025#include <asm/virt.h>
Mark Rutland8a4da6e2012-11-12 14:33:44 +000026
27#include <clocksource/arm_arch_timer.h>
28
Stephen Boyd22006992013-07-18 16:59:32 -070029#define CNTTIDR 0x08
30#define CNTTIDR_VIRT(n) (BIT(1) << ((n) * 4))
31
32#define CNTVCT_LO 0x08
33#define CNTVCT_HI 0x0c
34#define CNTFRQ 0x10
35#define CNTP_TVAL 0x28
36#define CNTP_CTL 0x2c
37#define CNTV_TVAL 0x38
38#define CNTV_CTL 0x3c
39
40#define ARCH_CP15_TIMER BIT(0)
41#define ARCH_MEM_TIMER BIT(1)
42static unsigned arch_timers_present __initdata;
43
44static void __iomem *arch_counter_base;
45
46struct arch_timer {
47 void __iomem *base;
48 struct clock_event_device evt;
49};
50
51#define to_arch_timer(e) container_of(e, struct arch_timer, evt)
52
Mark Rutland8a4da6e2012-11-12 14:33:44 +000053static u32 arch_timer_rate;
54
55enum ppi_nr {
56 PHYS_SECURE_PPI,
57 PHYS_NONSECURE_PPI,
58 VIRT_PPI,
59 HYP_PPI,
60 MAX_TIMER_PPI
61};
62
63static int arch_timer_ppi[MAX_TIMER_PPI];
64
65static struct clock_event_device __percpu *arch_timer_evt;
66
67static bool arch_timer_use_virtual = true;
Stephen Boyd22006992013-07-18 16:59:32 -070068static bool arch_timer_mem_use_virtual;
Mark Rutland8a4da6e2012-11-12 14:33:44 +000069
70/*
71 * Architected system timer support.
72 */
73
Stephen Boyd60faddf2013-07-18 16:59:31 -070074static __always_inline
75void arch_timer_reg_write(int access, enum arch_timer_reg reg, u32 val,
Thomas Gleixnercfb6d652013-08-21 14:59:23 +020076 struct clock_event_device *clk)
Stephen Boyd60faddf2013-07-18 16:59:31 -070077{
Stephen Boyd22006992013-07-18 16:59:32 -070078 if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
79 struct arch_timer *timer = to_arch_timer(clk);
80 switch (reg) {
81 case ARCH_TIMER_REG_CTRL:
82 writel_relaxed(val, timer->base + CNTP_CTL);
83 break;
84 case ARCH_TIMER_REG_TVAL:
85 writel_relaxed(val, timer->base + CNTP_TVAL);
86 break;
87 }
88 } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
89 struct arch_timer *timer = to_arch_timer(clk);
90 switch (reg) {
91 case ARCH_TIMER_REG_CTRL:
92 writel_relaxed(val, timer->base + CNTV_CTL);
93 break;
94 case ARCH_TIMER_REG_TVAL:
95 writel_relaxed(val, timer->base + CNTV_TVAL);
96 break;
97 }
98 } else {
99 arch_timer_reg_write_cp15(access, reg, val);
100 }
Stephen Boyd60faddf2013-07-18 16:59:31 -0700101}
102
103static __always_inline
104u32 arch_timer_reg_read(int access, enum arch_timer_reg reg,
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200105 struct clock_event_device *clk)
Stephen Boyd60faddf2013-07-18 16:59:31 -0700106{
Stephen Boyd22006992013-07-18 16:59:32 -0700107 u32 val;
108
109 if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
110 struct arch_timer *timer = to_arch_timer(clk);
111 switch (reg) {
112 case ARCH_TIMER_REG_CTRL:
113 val = readl_relaxed(timer->base + CNTP_CTL);
114 break;
115 case ARCH_TIMER_REG_TVAL:
116 val = readl_relaxed(timer->base + CNTP_TVAL);
117 break;
118 }
119 } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
120 struct arch_timer *timer = to_arch_timer(clk);
121 switch (reg) {
122 case ARCH_TIMER_REG_CTRL:
123 val = readl_relaxed(timer->base + CNTV_CTL);
124 break;
125 case ARCH_TIMER_REG_TVAL:
126 val = readl_relaxed(timer->base + CNTV_TVAL);
127 break;
128 }
129 } else {
130 val = arch_timer_reg_read_cp15(access, reg);
131 }
132
133 return val;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700134}
135
Stephen Boyde09f3cc2013-07-18 16:59:28 -0700136static __always_inline irqreturn_t timer_handler(const int access,
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000137 struct clock_event_device *evt)
138{
139 unsigned long ctrl;
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200140
Stephen Boyd60faddf2013-07-18 16:59:31 -0700141 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000142 if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
143 ctrl |= ARCH_TIMER_CTRL_IT_MASK;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700144 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000145 evt->event_handler(evt);
146 return IRQ_HANDLED;
147 }
148
149 return IRQ_NONE;
150}
151
152static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
153{
154 struct clock_event_device *evt = dev_id;
155
156 return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
157}
158
159static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
160{
161 struct clock_event_device *evt = dev_id;
162
163 return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
164}
165
Stephen Boyd22006992013-07-18 16:59:32 -0700166static irqreturn_t arch_timer_handler_phys_mem(int irq, void *dev_id)
167{
168 struct clock_event_device *evt = dev_id;
169
170 return timer_handler(ARCH_TIMER_MEM_PHYS_ACCESS, evt);
171}
172
173static irqreturn_t arch_timer_handler_virt_mem(int irq, void *dev_id)
174{
175 struct clock_event_device *evt = dev_id;
176
177 return timer_handler(ARCH_TIMER_MEM_VIRT_ACCESS, evt);
178}
179
Stephen Boyd60faddf2013-07-18 16:59:31 -0700180static __always_inline void timer_set_mode(const int access, int mode,
181 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000182{
183 unsigned long ctrl;
184 switch (mode) {
185 case CLOCK_EVT_MODE_UNUSED:
186 case CLOCK_EVT_MODE_SHUTDOWN:
Stephen Boyd60faddf2013-07-18 16:59:31 -0700187 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000188 ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700189 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000190 break;
191 default:
192 break;
193 }
194}
195
196static void arch_timer_set_mode_virt(enum clock_event_mode mode,
197 struct clock_event_device *clk)
198{
Stephen Boyd60faddf2013-07-18 16:59:31 -0700199 timer_set_mode(ARCH_TIMER_VIRT_ACCESS, mode, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000200}
201
202static void arch_timer_set_mode_phys(enum clock_event_mode mode,
203 struct clock_event_device *clk)
204{
Stephen Boyd60faddf2013-07-18 16:59:31 -0700205 timer_set_mode(ARCH_TIMER_PHYS_ACCESS, mode, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000206}
207
Stephen Boyd22006992013-07-18 16:59:32 -0700208static void arch_timer_set_mode_virt_mem(enum clock_event_mode mode,
209 struct clock_event_device *clk)
210{
211 timer_set_mode(ARCH_TIMER_MEM_VIRT_ACCESS, mode, clk);
212}
213
214static void arch_timer_set_mode_phys_mem(enum clock_event_mode mode,
215 struct clock_event_device *clk)
216{
217 timer_set_mode(ARCH_TIMER_MEM_PHYS_ACCESS, mode, clk);
218}
219
Stephen Boyd60faddf2013-07-18 16:59:31 -0700220static __always_inline void set_next_event(const int access, unsigned long evt,
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200221 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000222{
223 unsigned long ctrl;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700224 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000225 ctrl |= ARCH_TIMER_CTRL_ENABLE;
226 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
Stephen Boyd60faddf2013-07-18 16:59:31 -0700227 arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt, clk);
228 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000229}
230
231static int arch_timer_set_next_event_virt(unsigned long evt,
Stephen Boyd60faddf2013-07-18 16:59:31 -0700232 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000233{
Stephen Boyd60faddf2013-07-18 16:59:31 -0700234 set_next_event(ARCH_TIMER_VIRT_ACCESS, evt, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000235 return 0;
236}
237
238static int arch_timer_set_next_event_phys(unsigned long evt,
Stephen Boyd60faddf2013-07-18 16:59:31 -0700239 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000240{
Stephen Boyd60faddf2013-07-18 16:59:31 -0700241 set_next_event(ARCH_TIMER_PHYS_ACCESS, evt, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000242 return 0;
243}
244
Stephen Boyd22006992013-07-18 16:59:32 -0700245static int arch_timer_set_next_event_virt_mem(unsigned long evt,
246 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000247{
Stephen Boyd22006992013-07-18 16:59:32 -0700248 set_next_event(ARCH_TIMER_MEM_VIRT_ACCESS, evt, clk);
249 return 0;
250}
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000251
Stephen Boyd22006992013-07-18 16:59:32 -0700252static int arch_timer_set_next_event_phys_mem(unsigned long evt,
253 struct clock_event_device *clk)
254{
255 set_next_event(ARCH_TIMER_MEM_PHYS_ACCESS, evt, clk);
256 return 0;
257}
258
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200259static void __arch_timer_setup(unsigned type,
260 struct clock_event_device *clk)
Stephen Boyd22006992013-07-18 16:59:32 -0700261{
262 clk->features = CLOCK_EVT_FEAT_ONESHOT;
263
264 if (type == ARCH_CP15_TIMER) {
265 clk->features |= CLOCK_EVT_FEAT_C3STOP;
266 clk->name = "arch_sys_timer";
267 clk->rating = 450;
268 clk->cpumask = cpumask_of(smp_processor_id());
269 if (arch_timer_use_virtual) {
270 clk->irq = arch_timer_ppi[VIRT_PPI];
271 clk->set_mode = arch_timer_set_mode_virt;
272 clk->set_next_event = arch_timer_set_next_event_virt;
273 } else {
274 clk->irq = arch_timer_ppi[PHYS_SECURE_PPI];
275 clk->set_mode = arch_timer_set_mode_phys;
276 clk->set_next_event = arch_timer_set_next_event_phys;
277 }
278 } else {
279 clk->name = "arch_mem_timer";
280 clk->rating = 400;
281 clk->cpumask = cpu_all_mask;
282 if (arch_timer_mem_use_virtual) {
283 clk->set_mode = arch_timer_set_mode_virt_mem;
284 clk->set_next_event =
285 arch_timer_set_next_event_virt_mem;
286 } else {
287 clk->set_mode = arch_timer_set_mode_phys_mem;
288 clk->set_next_event =
289 arch_timer_set_next_event_phys_mem;
290 }
291 }
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000292
Stephen Boyd1ff99ea2013-07-18 16:59:30 -0700293 clk->set_mode(CLOCK_EVT_MODE_SHUTDOWN, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000294
Stephen Boyd22006992013-07-18 16:59:32 -0700295 clockevents_config_and_register(clk, arch_timer_rate, 0xf, 0x7fffffff);
296}
297
Will Deacon037f6372013-08-23 15:32:29 +0100298static void arch_timer_configure_evtstream(void)
299{
300 int evt_stream_div, pos;
301
302 /* Find the closest power of two to the divisor */
303 evt_stream_div = arch_timer_rate / ARCH_TIMER_EVT_STREAM_FREQ;
304 pos = fls(evt_stream_div);
305 if (pos > 1 && !(evt_stream_div & (1 << (pos - 2))))
306 pos--;
307 /* enable event stream */
308 arch_timer_evtstrm_enable(min(pos, 15));
309}
310
Paul Gortmaker8c37bb32013-06-19 11:32:08 -0400311static int arch_timer_setup(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000312{
Stephen Boyd22006992013-07-18 16:59:32 -0700313 __arch_timer_setup(ARCH_CP15_TIMER, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000314
315 if (arch_timer_use_virtual)
316 enable_percpu_irq(arch_timer_ppi[VIRT_PPI], 0);
317 else {
318 enable_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI], 0);
319 if (arch_timer_ppi[PHYS_NONSECURE_PPI])
320 enable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI], 0);
321 }
322
323 arch_counter_set_user_access();
Will Deacon037f6372013-08-23 15:32:29 +0100324 if (IS_ENABLED(CONFIG_ARM_ARCH_TIMER_EVTSTREAM))
325 arch_timer_configure_evtstream();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000326
327 return 0;
328}
329
Stephen Boyd22006992013-07-18 16:59:32 -0700330static void
331arch_timer_detect_rate(void __iomem *cntbase, struct device_node *np)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000332{
Stephen Boyd22006992013-07-18 16:59:32 -0700333 /* Who has more than one independent system counter? */
334 if (arch_timer_rate)
335 return;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000336
Stephen Boyd22006992013-07-18 16:59:32 -0700337 /* Try to determine the frequency from the device tree or CNTFRQ */
338 if (of_property_read_u32(np, "clock-frequency", &arch_timer_rate)) {
339 if (cntbase)
340 arch_timer_rate = readl_relaxed(cntbase + CNTFRQ);
341 else
342 arch_timer_rate = arch_timer_get_cntfrq();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000343 }
344
Stephen Boyd22006992013-07-18 16:59:32 -0700345 /* Check the timer frequency. */
346 if (arch_timer_rate == 0)
347 pr_warn("Architected timer frequency not available\n");
348}
349
350static void arch_timer_banner(unsigned type)
351{
352 pr_info("Architected %s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
353 type & ARCH_CP15_TIMER ? "cp15" : "",
354 type == (ARCH_CP15_TIMER | ARCH_MEM_TIMER) ? " and " : "",
355 type & ARCH_MEM_TIMER ? "mmio" : "",
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000356 (unsigned long)arch_timer_rate / 1000000,
357 (unsigned long)(arch_timer_rate / 10000) % 100,
Stephen Boyd22006992013-07-18 16:59:32 -0700358 type & ARCH_CP15_TIMER ?
359 arch_timer_use_virtual ? "virt" : "phys" :
360 "",
361 type == (ARCH_CP15_TIMER | ARCH_MEM_TIMER) ? "/" : "",
362 type & ARCH_MEM_TIMER ?
363 arch_timer_mem_use_virtual ? "virt" : "phys" :
364 "");
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000365}
366
367u32 arch_timer_get_rate(void)
368{
369 return arch_timer_rate;
370}
371
Stephen Boyd22006992013-07-18 16:59:32 -0700372static u64 arch_counter_get_cntvct_mem(void)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000373{
Stephen Boyd22006992013-07-18 16:59:32 -0700374 u32 vct_lo, vct_hi, tmp_hi;
375
376 do {
377 vct_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
378 vct_lo = readl_relaxed(arch_counter_base + CNTVCT_LO);
379 tmp_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
380 } while (vct_hi != tmp_hi);
381
382 return ((u64) vct_hi << 32) | vct_lo;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000383}
384
Stephen Boyd22006992013-07-18 16:59:32 -0700385/*
386 * Default to cp15 based access because arm64 uses this function for
387 * sched_clock() before DT is probed and the cp15 method is guaranteed
388 * to exist on arm64. arm doesn't use this before DT is probed so even
389 * if we don't have the cp15 accessors we won't have a problem.
390 */
391u64 (*arch_timer_read_counter)(void) = arch_counter_get_cntvct;
392
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000393static cycle_t arch_counter_read(struct clocksource *cs)
394{
Stephen Boyd22006992013-07-18 16:59:32 -0700395 return arch_timer_read_counter();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000396}
397
398static cycle_t arch_counter_read_cc(const struct cyclecounter *cc)
399{
Stephen Boyd22006992013-07-18 16:59:32 -0700400 return arch_timer_read_counter();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000401}
402
403static struct clocksource clocksource_counter = {
404 .name = "arch_sys_counter",
405 .rating = 400,
406 .read = arch_counter_read,
407 .mask = CLOCKSOURCE_MASK(56),
408 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
409};
410
411static struct cyclecounter cyclecounter = {
412 .read = arch_counter_read_cc,
413 .mask = CLOCKSOURCE_MASK(56),
414};
415
416static struct timecounter timecounter;
417
418struct timecounter *arch_timer_get_timecounter(void)
419{
420 return &timecounter;
421}
422
Stephen Boyd22006992013-07-18 16:59:32 -0700423static void __init arch_counter_register(unsigned type)
424{
425 u64 start_count;
426
427 /* Register the CP15 based counter if we have one */
428 if (type & ARCH_CP15_TIMER)
429 arch_timer_read_counter = arch_counter_get_cntvct;
430 else
431 arch_timer_read_counter = arch_counter_get_cntvct_mem;
432
433 start_count = arch_timer_read_counter();
434 clocksource_register_hz(&clocksource_counter, arch_timer_rate);
435 cyclecounter.mult = clocksource_counter.mult;
436 cyclecounter.shift = clocksource_counter.shift;
437 timecounter_init(&timecounter, &cyclecounter, start_count);
438}
439
Paul Gortmaker8c37bb32013-06-19 11:32:08 -0400440static void arch_timer_stop(struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000441{
442 pr_debug("arch_timer_teardown disable IRQ%d cpu #%d\n",
443 clk->irq, smp_processor_id());
444
445 if (arch_timer_use_virtual)
446 disable_percpu_irq(arch_timer_ppi[VIRT_PPI]);
447 else {
448 disable_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI]);
449 if (arch_timer_ppi[PHYS_NONSECURE_PPI])
450 disable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI]);
451 }
452
453 clk->set_mode(CLOCK_EVT_MODE_UNUSED, clk);
454}
455
Paul Gortmaker8c37bb32013-06-19 11:32:08 -0400456static int arch_timer_cpu_notify(struct notifier_block *self,
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000457 unsigned long action, void *hcpu)
458{
Stephen Boydf31c2f12013-04-17 16:26:18 -0700459 /*
460 * Grab cpu pointer in each case to avoid spurious
461 * preemptible warnings
462 */
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000463 switch (action & ~CPU_TASKS_FROZEN) {
464 case CPU_STARTING:
Stephen Boydf31c2f12013-04-17 16:26:18 -0700465 arch_timer_setup(this_cpu_ptr(arch_timer_evt));
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000466 break;
467 case CPU_DYING:
Stephen Boydf31c2f12013-04-17 16:26:18 -0700468 arch_timer_stop(this_cpu_ptr(arch_timer_evt));
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000469 break;
470 }
471
472 return NOTIFY_OK;
473}
474
Paul Gortmaker8c37bb32013-06-19 11:32:08 -0400475static struct notifier_block arch_timer_cpu_nb = {
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000476 .notifier_call = arch_timer_cpu_notify,
477};
478
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100479#ifdef CONFIG_CPU_PM
480static unsigned int saved_cntkctl;
481static int arch_timer_cpu_pm_notify(struct notifier_block *self,
482 unsigned long action, void *hcpu)
483{
484 if (action == CPU_PM_ENTER)
485 saved_cntkctl = arch_timer_get_cntkctl();
486 else if (action == CPU_PM_ENTER_FAILED || action == CPU_PM_EXIT)
487 arch_timer_set_cntkctl(saved_cntkctl);
488 return NOTIFY_OK;
489}
490
491static struct notifier_block arch_timer_cpu_pm_notifier = {
492 .notifier_call = arch_timer_cpu_pm_notify,
493};
494
495static int __init arch_timer_cpu_pm_init(void)
496{
497 return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier);
498}
499#else
500static int __init arch_timer_cpu_pm_init(void)
501{
502 return 0;
503}
504#endif
505
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000506static int __init arch_timer_register(void)
507{
508 int err;
509 int ppi;
510
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000511 arch_timer_evt = alloc_percpu(struct clock_event_device);
512 if (!arch_timer_evt) {
513 err = -ENOMEM;
514 goto out;
515 }
516
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000517 if (arch_timer_use_virtual) {
518 ppi = arch_timer_ppi[VIRT_PPI];
519 err = request_percpu_irq(ppi, arch_timer_handler_virt,
520 "arch_timer", arch_timer_evt);
521 } else {
522 ppi = arch_timer_ppi[PHYS_SECURE_PPI];
523 err = request_percpu_irq(ppi, arch_timer_handler_phys,
524 "arch_timer", arch_timer_evt);
525 if (!err && arch_timer_ppi[PHYS_NONSECURE_PPI]) {
526 ppi = arch_timer_ppi[PHYS_NONSECURE_PPI];
527 err = request_percpu_irq(ppi, arch_timer_handler_phys,
528 "arch_timer", arch_timer_evt);
529 if (err)
530 free_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI],
531 arch_timer_evt);
532 }
533 }
534
535 if (err) {
536 pr_err("arch_timer: can't register interrupt %d (%d)\n",
537 ppi, err);
538 goto out_free;
539 }
540
541 err = register_cpu_notifier(&arch_timer_cpu_nb);
542 if (err)
543 goto out_free_irq;
544
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100545 err = arch_timer_cpu_pm_init();
546 if (err)
547 goto out_unreg_notify;
548
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000549 /* Immediately configure the timer on the boot CPU */
550 arch_timer_setup(this_cpu_ptr(arch_timer_evt));
551
552 return 0;
553
Sudeep KarkadaNagesha346e7482013-08-23 15:53:15 +0100554out_unreg_notify:
555 unregister_cpu_notifier(&arch_timer_cpu_nb);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000556out_free_irq:
557 if (arch_timer_use_virtual)
558 free_percpu_irq(arch_timer_ppi[VIRT_PPI], arch_timer_evt);
559 else {
560 free_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI],
561 arch_timer_evt);
562 if (arch_timer_ppi[PHYS_NONSECURE_PPI])
563 free_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI],
564 arch_timer_evt);
565 }
566
567out_free:
568 free_percpu(arch_timer_evt);
569out:
570 return err;
571}
572
Stephen Boyd22006992013-07-18 16:59:32 -0700573static int __init arch_timer_mem_register(void __iomem *base, unsigned int irq)
574{
575 int ret;
576 irq_handler_t func;
577 struct arch_timer *t;
578
579 t = kzalloc(sizeof(*t), GFP_KERNEL);
580 if (!t)
581 return -ENOMEM;
582
583 t->base = base;
584 t->evt.irq = irq;
585 __arch_timer_setup(ARCH_MEM_TIMER, &t->evt);
586
587 if (arch_timer_mem_use_virtual)
588 func = arch_timer_handler_virt_mem;
589 else
590 func = arch_timer_handler_phys_mem;
591
592 ret = request_irq(irq, func, IRQF_TIMER, "arch_mem_timer", &t->evt);
593 if (ret) {
594 pr_err("arch_timer: Failed to request mem timer irq\n");
595 kfree(t);
596 }
597
598 return ret;
599}
600
601static const struct of_device_id arch_timer_of_match[] __initconst = {
602 { .compatible = "arm,armv7-timer", },
603 { .compatible = "arm,armv8-timer", },
604 {},
605};
606
607static const struct of_device_id arch_timer_mem_of_match[] __initconst = {
608 { .compatible = "arm,armv7-timer-mem", },
609 {},
610};
611
612static void __init arch_timer_common_init(void)
613{
614 unsigned mask = ARCH_CP15_TIMER | ARCH_MEM_TIMER;
615
616 /* Wait until both nodes are probed if we have two timers */
617 if ((arch_timers_present & mask) != mask) {
618 if (of_find_matching_node(NULL, arch_timer_mem_of_match) &&
619 !(arch_timers_present & ARCH_MEM_TIMER))
620 return;
621 if (of_find_matching_node(NULL, arch_timer_of_match) &&
622 !(arch_timers_present & ARCH_CP15_TIMER))
623 return;
624 }
625
626 arch_timer_banner(arch_timers_present);
627 arch_counter_register(arch_timers_present);
628 arch_timer_arch_init();
629}
630
Rob Herring0583fe42013-04-10 18:27:51 -0500631static void __init arch_timer_init(struct device_node *np)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000632{
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000633 int i;
634
Stephen Boyd22006992013-07-18 16:59:32 -0700635 if (arch_timers_present & ARCH_CP15_TIMER) {
Rob Herring0583fe42013-04-10 18:27:51 -0500636 pr_warn("arch_timer: multiple nodes in dt, skipping\n");
637 return;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000638 }
639
Stephen Boyd22006992013-07-18 16:59:32 -0700640 arch_timers_present |= ARCH_CP15_TIMER;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000641 for (i = PHYS_SECURE_PPI; i < MAX_TIMER_PPI; i++)
642 arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
Stephen Boyd22006992013-07-18 16:59:32 -0700643 arch_timer_detect_rate(NULL, np);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000644
645 /*
Marc Zyngier82668912013-01-10 11:13:07 +0000646 * If HYP mode is available, we know that the physical timer
647 * has been configured to be accessible from PL1. Use it, so
648 * that a guest can use the virtual timer instead.
649 *
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000650 * If no interrupt provided for virtual timer, we'll have to
651 * stick to the physical timer. It'd better be accessible...
652 */
Marc Zyngier82668912013-01-10 11:13:07 +0000653 if (is_hyp_mode_available() || !arch_timer_ppi[VIRT_PPI]) {
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000654 arch_timer_use_virtual = false;
655
656 if (!arch_timer_ppi[PHYS_SECURE_PPI] ||
657 !arch_timer_ppi[PHYS_NONSECURE_PPI]) {
658 pr_warn("arch_timer: No interrupt available, giving up\n");
Rob Herring0583fe42013-04-10 18:27:51 -0500659 return;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000660 }
661 }
662
Rob Herring0583fe42013-04-10 18:27:51 -0500663 arch_timer_register();
Stephen Boyd22006992013-07-18 16:59:32 -0700664 arch_timer_common_init();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000665}
Rob Herring0583fe42013-04-10 18:27:51 -0500666CLOCKSOURCE_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_init);
667CLOCKSOURCE_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_init);
Stephen Boyd22006992013-07-18 16:59:32 -0700668
669static void __init arch_timer_mem_init(struct device_node *np)
670{
671 struct device_node *frame, *best_frame = NULL;
672 void __iomem *cntctlbase, *base;
673 unsigned int irq;
674 u32 cnttidr;
675
676 arch_timers_present |= ARCH_MEM_TIMER;
677 cntctlbase = of_iomap(np, 0);
678 if (!cntctlbase) {
679 pr_err("arch_timer: Can't find CNTCTLBase\n");
680 return;
681 }
682
683 cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
684 iounmap(cntctlbase);
685
686 /*
687 * Try to find a virtual capable frame. Otherwise fall back to a
688 * physical capable frame.
689 */
690 for_each_available_child_of_node(np, frame) {
691 int n;
692
693 if (of_property_read_u32(frame, "frame-number", &n)) {
694 pr_err("arch_timer: Missing frame-number\n");
695 of_node_put(best_frame);
696 of_node_put(frame);
697 return;
698 }
699
700 if (cnttidr & CNTTIDR_VIRT(n)) {
701 of_node_put(best_frame);
702 best_frame = frame;
703 arch_timer_mem_use_virtual = true;
704 break;
705 }
706 of_node_put(best_frame);
707 best_frame = of_node_get(frame);
708 }
709
710 base = arch_counter_base = of_iomap(best_frame, 0);
711 if (!base) {
712 pr_err("arch_timer: Can't map frame's registers\n");
713 of_node_put(best_frame);
714 return;
715 }
716
717 if (arch_timer_mem_use_virtual)
718 irq = irq_of_parse_and_map(best_frame, 1);
719 else
720 irq = irq_of_parse_and_map(best_frame, 0);
721 of_node_put(best_frame);
722 if (!irq) {
723 pr_err("arch_timer: Frame missing %s irq",
Thomas Gleixnercfb6d652013-08-21 14:59:23 +0200724 arch_timer_mem_use_virtual ? "virt" : "phys");
Stephen Boyd22006992013-07-18 16:59:32 -0700725 return;
726 }
727
728 arch_timer_detect_rate(base, np);
729 arch_timer_mem_register(base, irq);
730 arch_timer_common_init();
731}
732CLOCKSOURCE_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem",
733 arch_timer_mem_init);