blob: 67bbbd8ae507b5e4e937abcf616288861d430182 [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 KarkadaNagesha5e3c2002013-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 Boyd2b8b6082013-07-18 16:59:32 -070020#include <linux/of_address.h>
Mark Rutland8a4da6e2012-11-12 14:33:44 +000021#include <linux/io.h>
Stephen Boyd2b8b6082013-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 Boyd2b8b6082013-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 Boyd2b8b6082013-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 Boydff527612013-07-18 16:59:31 -070074static __always_inline
75void arch_timer_reg_write(int access, enum arch_timer_reg reg, u32 val,
76 struct clock_event_device *clk)
77{
Stephen Boyd2b8b6082013-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 Boydff527612013-07-18 16:59:31 -0700101}
102
103static __always_inline
104u32 arch_timer_reg_read(int access, enum arch_timer_reg reg,
105 struct clock_event_device *clk)
106{
Stephen Boyd2b8b6082013-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 Boydff527612013-07-18 16:59:31 -0700134}
135
Stephen Boyd42705592013-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;
Stephen Boydff527612013-07-18 16:59:31 -0700140 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000141 if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
142 ctrl |= ARCH_TIMER_CTRL_IT_MASK;
Stephen Boydff527612013-07-18 16:59:31 -0700143 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, evt);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000144 evt->event_handler(evt);
145 return IRQ_HANDLED;
146 }
147
148 return IRQ_NONE;
149}
150
151static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
152{
153 struct clock_event_device *evt = dev_id;
154
155 return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
156}
157
158static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
159{
160 struct clock_event_device *evt = dev_id;
161
162 return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
163}
164
Stephen Boyd2b8b6082013-07-18 16:59:32 -0700165static irqreturn_t arch_timer_handler_phys_mem(int irq, void *dev_id)
166{
167 struct clock_event_device *evt = dev_id;
168
169 return timer_handler(ARCH_TIMER_MEM_PHYS_ACCESS, evt);
170}
171
172static irqreturn_t arch_timer_handler_virt_mem(int irq, void *dev_id)
173{
174 struct clock_event_device *evt = dev_id;
175
176 return timer_handler(ARCH_TIMER_MEM_VIRT_ACCESS, evt);
177}
178
Stephen Boydff527612013-07-18 16:59:31 -0700179static __always_inline void timer_set_mode(const int access, int mode,
180 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000181{
182 unsigned long ctrl;
183 switch (mode) {
184 case CLOCK_EVT_MODE_UNUSED:
185 case CLOCK_EVT_MODE_SHUTDOWN:
Stephen Boydff527612013-07-18 16:59:31 -0700186 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000187 ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
Stephen Boydff527612013-07-18 16:59:31 -0700188 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000189 break;
190 default:
191 break;
192 }
193}
194
195static void arch_timer_set_mode_virt(enum clock_event_mode mode,
196 struct clock_event_device *clk)
197{
Stephen Boydff527612013-07-18 16:59:31 -0700198 timer_set_mode(ARCH_TIMER_VIRT_ACCESS, mode, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000199}
200
201static void arch_timer_set_mode_phys(enum clock_event_mode mode,
202 struct clock_event_device *clk)
203{
Stephen Boydff527612013-07-18 16:59:31 -0700204 timer_set_mode(ARCH_TIMER_PHYS_ACCESS, mode, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000205}
206
Stephen Boyd2b8b6082013-07-18 16:59:32 -0700207static void arch_timer_set_mode_virt_mem(enum clock_event_mode mode,
208 struct clock_event_device *clk)
209{
210 timer_set_mode(ARCH_TIMER_MEM_VIRT_ACCESS, mode, clk);
211}
212
213static void arch_timer_set_mode_phys_mem(enum clock_event_mode mode,
214 struct clock_event_device *clk)
215{
216 timer_set_mode(ARCH_TIMER_MEM_PHYS_ACCESS, mode, clk);
217}
218
Stephen Boydff527612013-07-18 16:59:31 -0700219static __always_inline void set_next_event(const int access, unsigned long evt,
220 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000221{
222 unsigned long ctrl;
Stephen Boydff527612013-07-18 16:59:31 -0700223 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000224 ctrl |= ARCH_TIMER_CTRL_ENABLE;
225 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
Stephen Boydff527612013-07-18 16:59:31 -0700226 arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt, clk);
227 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000228}
229
230static int arch_timer_set_next_event_virt(unsigned long evt,
Stephen Boydff527612013-07-18 16:59:31 -0700231 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000232{
Stephen Boydff527612013-07-18 16:59:31 -0700233 set_next_event(ARCH_TIMER_VIRT_ACCESS, evt, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000234 return 0;
235}
236
237static int arch_timer_set_next_event_phys(unsigned long evt,
Stephen Boydff527612013-07-18 16:59:31 -0700238 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000239{
Stephen Boydff527612013-07-18 16:59:31 -0700240 set_next_event(ARCH_TIMER_PHYS_ACCESS, evt, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000241 return 0;
242}
243
Will Deacon8c06dc72013-08-23 15:32:29 +0100244static void arch_timer_configure_evtstream(void)
245{
246 int evt_stream_div, pos;
247
248 /* Find the closest power of two to the divisor */
249 evt_stream_div = arch_timer_rate / ARCH_TIMER_EVT_STREAM_FREQ;
250 pos = fls(evt_stream_div);
251 if (pos > 1 && !(evt_stream_div & (1 << (pos - 2))))
252 pos--;
253 /* enable event stream */
254 arch_timer_evtstrm_enable(min(pos, 15));
255}
256
Stephen Boyd2b8b6082013-07-18 16:59:32 -0700257static int arch_timer_set_next_event_virt_mem(unsigned long evt,
258 struct clock_event_device *clk)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000259{
Stephen Boyd2b8b6082013-07-18 16:59:32 -0700260 set_next_event(ARCH_TIMER_MEM_VIRT_ACCESS, evt, clk);
261 return 0;
262}
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000263
Stephen Boyd2b8b6082013-07-18 16:59:32 -0700264static int arch_timer_set_next_event_phys_mem(unsigned long evt,
265 struct clock_event_device *clk)
266{
267 set_next_event(ARCH_TIMER_MEM_PHYS_ACCESS, evt, clk);
268 return 0;
269}
270
271static void __cpuinit __arch_timer_setup(unsigned type,
272 struct clock_event_device *clk)
273{
274 clk->features = CLOCK_EVT_FEAT_ONESHOT;
275
276 if (type == ARCH_CP15_TIMER) {
277 clk->features |= CLOCK_EVT_FEAT_C3STOP;
278 clk->name = "arch_sys_timer";
279 clk->rating = 450;
280 clk->cpumask = cpumask_of(smp_processor_id());
281 if (arch_timer_use_virtual) {
282 clk->irq = arch_timer_ppi[VIRT_PPI];
283 clk->set_mode = arch_timer_set_mode_virt;
284 clk->set_next_event = arch_timer_set_next_event_virt;
285 } else {
286 clk->irq = arch_timer_ppi[PHYS_SECURE_PPI];
287 clk->set_mode = arch_timer_set_mode_phys;
288 clk->set_next_event = arch_timer_set_next_event_phys;
289 }
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000290 } else {
Stephen Boyd2b8b6082013-07-18 16:59:32 -0700291 clk->name = "arch_mem_timer";
292 clk->rating = 400;
293 clk->cpumask = cpu_all_mask;
294 if (arch_timer_mem_use_virtual) {
295 clk->set_mode = arch_timer_set_mode_virt_mem;
296 clk->set_next_event =
297 arch_timer_set_next_event_virt_mem;
298 } else {
299 clk->set_mode = arch_timer_set_mode_phys_mem;
300 clk->set_next_event =
301 arch_timer_set_next_event_phys_mem;
302 }
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000303 }
304
Stephen Boyd5a5f99e2013-07-18 16:59:30 -0700305 clk->set_mode(CLOCK_EVT_MODE_SHUTDOWN, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000306
Stephen Boyd2b8b6082013-07-18 16:59:32 -0700307 clockevents_config_and_register(clk, arch_timer_rate, 0xf, 0x7fffffff);
308}
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000309
Stephen Boyd2b8b6082013-07-18 16:59:32 -0700310static int __cpuinit arch_timer_setup(struct clock_event_device *clk)
311{
312 __arch_timer_setup(ARCH_CP15_TIMER, clk);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000313
314 if (arch_timer_use_virtual)
315 enable_percpu_irq(arch_timer_ppi[VIRT_PPI], 0);
316 else {
317 enable_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI], 0);
318 if (arch_timer_ppi[PHYS_NONSECURE_PPI])
319 enable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI], 0);
320 }
321
322 arch_counter_set_user_access();
Will Deacon8c06dc72013-08-23 15:32:29 +0100323 if (IS_ENABLED(CONFIG_ARM_ARCH_TIMER_EVTSTREAM))
324 arch_timer_configure_evtstream();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000325
326 return 0;
327}
328
Stephen Boyd2b8b6082013-07-18 16:59:32 -0700329static void
330arch_timer_detect_rate(void __iomem *cntbase, struct device_node *np)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000331{
Stephen Boyd2b8b6082013-07-18 16:59:32 -0700332 /* Who has more than one independent system counter? */
333 if (arch_timer_rate)
334 return;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000335
Stephen Boyd2b8b6082013-07-18 16:59:32 -0700336 /* Try to determine the frequency from the device tree or CNTFRQ */
337 if (of_property_read_u32(np, "clock-frequency", &arch_timer_rate)) {
338 if (cntbase)
339 arch_timer_rate = readl_relaxed(cntbase + CNTFRQ);
340 else
341 arch_timer_rate = arch_timer_get_cntfrq();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000342 }
343
Stephen Boyd2b8b6082013-07-18 16:59:32 -0700344 /* Check the timer frequency. */
345 if (arch_timer_rate == 0)
346 pr_warn("Architected timer frequency not available\n");
347}
348
349static void arch_timer_banner(unsigned type)
350{
351 pr_info("Architected %s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
352 type & ARCH_CP15_TIMER ? "cp15" : "",
353 type == (ARCH_CP15_TIMER | ARCH_MEM_TIMER) ? " and " : "",
354 type & ARCH_MEM_TIMER ? "mmio" : "",
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000355 (unsigned long)arch_timer_rate / 1000000,
356 (unsigned long)(arch_timer_rate / 10000) % 100,
Stephen Boyd2b8b6082013-07-18 16:59:32 -0700357 type & ARCH_CP15_TIMER ?
358 arch_timer_use_virtual ? "virt" : "phys" :
359 "",
360 type == (ARCH_CP15_TIMER | ARCH_MEM_TIMER) ? "/" : "",
361 type & ARCH_MEM_TIMER ?
362 arch_timer_mem_use_virtual ? "virt" : "phys" :
363 "");
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000364}
365
366u32 arch_timer_get_rate(void)
367{
368 return arch_timer_rate;
369}
370
Stephen Boyd2b8b6082013-07-18 16:59:32 -0700371static u64 arch_counter_get_cntvct_mem(void)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000372{
Stephen Boyd2b8b6082013-07-18 16:59:32 -0700373 u32 vct_lo, vct_hi, tmp_hi;
374
375 do {
376 vct_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
377 vct_lo = readl_relaxed(arch_counter_base + CNTVCT_LO);
378 tmp_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
379 } while (vct_hi != tmp_hi);
380
381 return ((u64) vct_hi << 32) | vct_lo;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000382}
383
Stephen Boyd2b8b6082013-07-18 16:59:32 -0700384/*
385 * Default to cp15 based access because arm64 uses this function for
386 * sched_clock() before DT is probed and the cp15 method is guaranteed
387 * to exist on arm64. arm doesn't use this before DT is probed so even
388 * if we don't have the cp15 accessors we won't have a problem.
389 */
390u64 (*arch_timer_read_counter)(void) = arch_counter_get_cntvct;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000391
392static cycle_t arch_counter_read(struct clocksource *cs)
393{
Mark Rutland714c21c2013-01-30 17:51:26 +0000394 return arch_counter_get_cntvct();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000395}
396
397static cycle_t arch_counter_read_cc(const struct cyclecounter *cc)
398{
Mark Rutland714c21c2013-01-30 17:51:26 +0000399 return arch_counter_get_cntvct();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000400}
401
402static struct clocksource clocksource_counter = {
403 .name = "arch_sys_counter",
404 .rating = 400,
405 .read = arch_counter_read,
406 .mask = CLOCKSOURCE_MASK(56),
407 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
408};
409
410static struct cyclecounter cyclecounter = {
411 .read = arch_counter_read_cc,
412 .mask = CLOCKSOURCE_MASK(56),
413};
414
415static struct timecounter timecounter;
416
417struct timecounter *arch_timer_get_timecounter(void)
418{
419 return &timecounter;
420}
421
Stephen Boyd2b8b6082013-07-18 16:59:32 -0700422static void __init arch_counter_register(unsigned type)
423{
424 u64 start_count;
425
426 /* Register the CP15 based counter if we have one */
427 if (type & ARCH_CP15_TIMER)
428 arch_timer_read_counter = arch_counter_get_cntvct;
429 else
430 arch_timer_read_counter = arch_counter_get_cntvct_mem;
431
432 start_count = arch_timer_read_counter();
433 clocksource_register_hz(&clocksource_counter, arch_timer_rate);
434 cyclecounter.mult = clocksource_counter.mult;
435 cyclecounter.shift = clocksource_counter.shift;
436 timecounter_init(&timecounter, &cyclecounter, start_count);
437}
438
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000439static void __cpuinit arch_timer_stop(struct clock_event_device *clk)
440{
441 pr_debug("arch_timer_teardown disable IRQ%d cpu #%d\n",
442 clk->irq, smp_processor_id());
443
444 if (arch_timer_use_virtual)
445 disable_percpu_irq(arch_timer_ppi[VIRT_PPI]);
446 else {
447 disable_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI]);
448 if (arch_timer_ppi[PHYS_NONSECURE_PPI])
449 disable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI]);
450 }
451
452 clk->set_mode(CLOCK_EVT_MODE_UNUSED, clk);
453}
454
455static int __cpuinit arch_timer_cpu_notify(struct notifier_block *self,
456 unsigned long action, void *hcpu)
457{
Stephen Boydf31c2f12013-04-17 16:26:18 -0700458 /*
459 * Grab cpu pointer in each case to avoid spurious
460 * preemptible warnings
461 */
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000462 switch (action & ~CPU_TASKS_FROZEN) {
463 case CPU_STARTING:
Stephen Boydf31c2f12013-04-17 16:26:18 -0700464 arch_timer_setup(this_cpu_ptr(arch_timer_evt));
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000465 break;
466 case CPU_DYING:
Stephen Boydf31c2f12013-04-17 16:26:18 -0700467 arch_timer_stop(this_cpu_ptr(arch_timer_evt));
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000468 break;
469 }
470
471 return NOTIFY_OK;
472}
473
474static struct notifier_block arch_timer_cpu_nb __cpuinitdata = {
475 .notifier_call = arch_timer_cpu_notify,
476};
477
Sudeep KarkadaNagesha5e3c2002013-08-23 15:53:15 +0100478#ifdef CONFIG_CPU_PM
479static unsigned int saved_cntkctl;
480static int arch_timer_cpu_pm_notify(struct notifier_block *self,
481 unsigned long action, void *hcpu)
482{
483 if (action == CPU_PM_ENTER)
484 saved_cntkctl = arch_timer_get_cntkctl();
485 else if (action == CPU_PM_ENTER_FAILED || action == CPU_PM_EXIT)
486 arch_timer_set_cntkctl(saved_cntkctl);
487 return NOTIFY_OK;
488}
489
490static struct notifier_block arch_timer_cpu_pm_notifier = {
491 .notifier_call = arch_timer_cpu_pm_notify,
492};
493
494static int __init arch_timer_cpu_pm_init(void)
495{
496 return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier);
497}
498#else
499static int __init arch_timer_cpu_pm_init(void)
500{
501 return 0;
502}
503#endif
504
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000505static int __init arch_timer_register(void)
506{
507 int err;
508 int ppi;
509
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000510 arch_timer_evt = alloc_percpu(struct clock_event_device);
511 if (!arch_timer_evt) {
512 err = -ENOMEM;
513 goto out;
514 }
515
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000516 if (arch_timer_use_virtual) {
517 ppi = arch_timer_ppi[VIRT_PPI];
518 err = request_percpu_irq(ppi, arch_timer_handler_virt,
519 "arch_timer", arch_timer_evt);
520 } else {
521 ppi = arch_timer_ppi[PHYS_SECURE_PPI];
522 err = request_percpu_irq(ppi, arch_timer_handler_phys,
523 "arch_timer", arch_timer_evt);
524 if (!err && arch_timer_ppi[PHYS_NONSECURE_PPI]) {
525 ppi = arch_timer_ppi[PHYS_NONSECURE_PPI];
526 err = request_percpu_irq(ppi, arch_timer_handler_phys,
527 "arch_timer", arch_timer_evt);
528 if (err)
529 free_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI],
530 arch_timer_evt);
531 }
532 }
533
534 if (err) {
535 pr_err("arch_timer: can't register interrupt %d (%d)\n",
536 ppi, err);
537 goto out_free;
538 }
539
540 err = register_cpu_notifier(&arch_timer_cpu_nb);
541 if (err)
542 goto out_free_irq;
543
Sudeep KarkadaNagesha5e3c2002013-08-23 15:53:15 +0100544 err = arch_timer_cpu_pm_init();
545 if (err)
546 goto out_unreg_notify;
547
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000548 /* Immediately configure the timer on the boot CPU */
549 arch_timer_setup(this_cpu_ptr(arch_timer_evt));
550
551 return 0;
552
Sudeep KarkadaNagesha5e3c2002013-08-23 15:53:15 +0100553out_unreg_notify:
554 unregister_cpu_notifier(&arch_timer_cpu_nb);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000555out_free_irq:
556 if (arch_timer_use_virtual)
557 free_percpu_irq(arch_timer_ppi[VIRT_PPI], arch_timer_evt);
558 else {
559 free_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI],
560 arch_timer_evt);
561 if (arch_timer_ppi[PHYS_NONSECURE_PPI])
562 free_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI],
563 arch_timer_evt);
564 }
565
566out_free:
567 free_percpu(arch_timer_evt);
568out:
569 return err;
570}
571
Stephen Boyd2b8b6082013-07-18 16:59:32 -0700572static int __init arch_timer_mem_register(void __iomem *base, unsigned int irq)
573{
574 int ret;
575 irq_handler_t func;
576 struct arch_timer *t;
577
578 t = kzalloc(sizeof(*t), GFP_KERNEL);
579 if (!t)
580 return -ENOMEM;
581
582 t->base = base;
583 t->evt.irq = irq;
584 __arch_timer_setup(ARCH_MEM_TIMER, &t->evt);
585
586 if (arch_timer_mem_use_virtual)
587 func = arch_timer_handler_virt_mem;
588 else
589 func = arch_timer_handler_phys_mem;
590
591 ret = request_irq(irq, func, IRQF_TIMER, "arch_mem_timer", &t->evt);
592 if (ret) {
593 pr_err("arch_timer: Failed to request mem timer irq\n");
594 kfree(t);
595 }
596
597 return ret;
598}
599
600static const struct of_device_id arch_timer_of_match[] __initconst = {
601 { .compatible = "arm,armv7-timer", },
602 { .compatible = "arm,armv8-timer", },
603 {},
604};
605
606static const struct of_device_id arch_timer_mem_of_match[] __initconst = {
607 { .compatible = "arm,armv7-timer-mem", },
608 {},
609};
610
611static void __init arch_timer_common_init(void)
612{
613 unsigned mask = ARCH_CP15_TIMER | ARCH_MEM_TIMER;
614
615 /* Wait until both nodes are probed if we have two timers */
616 if ((arch_timers_present & mask) != mask) {
617 if (of_find_matching_node(NULL, arch_timer_mem_of_match) &&
618 !(arch_timers_present & ARCH_MEM_TIMER))
619 return;
620 if (of_find_matching_node(NULL, arch_timer_of_match) &&
621 !(arch_timers_present & ARCH_CP15_TIMER))
622 return;
623 }
624
625 arch_timer_banner(arch_timers_present);
626 arch_counter_register(arch_timers_present);
627 arch_timer_arch_init();
628}
629
Rob Herring0583fe42013-04-10 18:27:51 -0500630static void __init arch_timer_init(struct device_node *np)
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000631{
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000632 int i;
633
Stephen Boyd2b8b6082013-07-18 16:59:32 -0700634 if (arch_timers_present & ARCH_CP15_TIMER) {
Rob Herring0583fe42013-04-10 18:27:51 -0500635 pr_warn("arch_timer: multiple nodes in dt, skipping\n");
636 return;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000637 }
638
Stephen Boyd2b8b6082013-07-18 16:59:32 -0700639 arch_timers_present |= ARCH_CP15_TIMER;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000640 for (i = PHYS_SECURE_PPI; i < MAX_TIMER_PPI; i++)
641 arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
Stephen Boyd2b8b6082013-07-18 16:59:32 -0700642 arch_timer_detect_rate(NULL, np);
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000643
644 /*
Marc Zyngier82668912013-01-10 11:13:07 +0000645 * If HYP mode is available, we know that the physical timer
646 * has been configured to be accessible from PL1. Use it, so
647 * that a guest can use the virtual timer instead.
648 *
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000649 * If no interrupt provided for virtual timer, we'll have to
650 * stick to the physical timer. It'd better be accessible...
651 */
Marc Zyngier82668912013-01-10 11:13:07 +0000652 if (is_hyp_mode_available() || !arch_timer_ppi[VIRT_PPI]) {
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000653 arch_timer_use_virtual = false;
654
655 if (!arch_timer_ppi[PHYS_SECURE_PPI] ||
656 !arch_timer_ppi[PHYS_NONSECURE_PPI]) {
657 pr_warn("arch_timer: No interrupt available, giving up\n");
Rob Herring0583fe42013-04-10 18:27:51 -0500658 return;
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000659 }
660 }
661
Rob Herring0583fe42013-04-10 18:27:51 -0500662 arch_timer_register();
Stephen Boyd2b8b6082013-07-18 16:59:32 -0700663 arch_timer_common_init();
Mark Rutland8a4da6e2012-11-12 14:33:44 +0000664}
Rob Herring0583fe42013-04-10 18:27:51 -0500665CLOCKSOURCE_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_init);
666CLOCKSOURCE_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_init);
Stephen Boyd2b8b6082013-07-18 16:59:32 -0700667
668static void __init arch_timer_mem_init(struct device_node *np)
669{
670 struct device_node *frame, *best_frame = NULL;
671 void __iomem *cntctlbase, *base;
672 unsigned int irq;
673 u32 cnttidr;
674
675 arch_timers_present |= ARCH_MEM_TIMER;
676 cntctlbase = of_iomap(np, 0);
677 if (!cntctlbase) {
678 pr_err("arch_timer: Can't find CNTCTLBase\n");
679 return;
680 }
681
682 cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
683 iounmap(cntctlbase);
684
685 /*
686 * Try to find a virtual capable frame. Otherwise fall back to a
687 * physical capable frame.
688 */
689 for_each_available_child_of_node(np, frame) {
690 int n;
691
692 if (of_property_read_u32(frame, "frame-number", &n)) {
693 pr_err("arch_timer: Missing frame-number\n");
694 of_node_put(best_frame);
695 of_node_put(frame);
696 return;
697 }
698
699 if (cnttidr & CNTTIDR_VIRT(n)) {
700 of_node_put(best_frame);
701 best_frame = frame;
702 arch_timer_mem_use_virtual = true;
703 break;
704 }
705 of_node_put(best_frame);
706 best_frame = of_node_get(frame);
707 }
708
709 base = arch_counter_base = of_iomap(best_frame, 0);
710 if (!base) {
711 pr_err("arch_timer: Can't map frame's registers\n");
712 of_node_put(best_frame);
713 return;
714 }
715
716 if (arch_timer_mem_use_virtual)
717 irq = irq_of_parse_and_map(best_frame, 1);
718 else
719 irq = irq_of_parse_and_map(best_frame, 0);
720 of_node_put(best_frame);
721 if (!irq) {
722 pr_err("arch_timer: Frame missing %s irq",
723 arch_timer_mem_use_virtual ? "virt" : "phys");
724 return;
725 }
726
727 arch_timer_detect_rate(base, np);
728 arch_timer_mem_register(base, irq);
729 arch_timer_common_init();
730}
731CLOCKSOURCE_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem",
732 arch_timer_mem_init);