2 * linux/drivers/clocksource/arm_arch_timer.c
4 * Copyright (C) 2011 ARM Ltd.
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.
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>
16 #include <linux/clockchips.h>
17 #include <linux/interrupt.h>
18 #include <linux/of_irq.h>
19 #include <linux/of_address.h>
21 #include <linux/slab.h>
22 #include <linux/sched_clock.h>
24 #include <asm/arch_timer.h>
27 #include <clocksource/arm_arch_timer.h>
30 #define CNTTIDR_VIRT(n) (BIT(1) << ((n) * 4))
32 #define CNTVCT_LO 0x08
33 #define CNTVCT_HI 0x0c
35 #define CNTP_TVAL 0x28
37 #define CNTV_TVAL 0x38
40 #define ARCH_CP15_TIMER BIT(0)
41 #define ARCH_MEM_TIMER BIT(1)
42 static unsigned arch_timers_present __initdata;
44 static void __iomem *arch_counter_base;
48 struct clock_event_device evt;
51 #define to_arch_timer(e) container_of(e, struct arch_timer, evt)
53 static u32 arch_timer_rate;
63 static int arch_timer_ppi[MAX_TIMER_PPI];
65 static struct clock_event_device __percpu *arch_timer_evt;
67 static bool arch_timer_use_virtual = true;
68 static bool arch_timer_mem_use_virtual;
71 * Architected system timer support.
74 static __always_inline
75 void arch_timer_reg_write(int access, enum arch_timer_reg reg, u32 val,
76 struct clock_event_device *clk)
78 if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
79 struct arch_timer *timer = to_arch_timer(clk);
81 case ARCH_TIMER_REG_CTRL:
82 writel_relaxed(val, timer->base + CNTP_CTL);
84 case ARCH_TIMER_REG_TVAL:
85 writel_relaxed(val, timer->base + CNTP_TVAL);
88 } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
89 struct arch_timer *timer = to_arch_timer(clk);
91 case ARCH_TIMER_REG_CTRL:
92 writel_relaxed(val, timer->base + CNTV_CTL);
94 case ARCH_TIMER_REG_TVAL:
95 writel_relaxed(val, timer->base + CNTV_TVAL);
99 arch_timer_reg_write_cp15(access, reg, val);
103 static __always_inline
104 u32 arch_timer_reg_read(int access, enum arch_timer_reg reg,
105 struct clock_event_device *clk)
109 if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
110 struct arch_timer *timer = to_arch_timer(clk);
112 case ARCH_TIMER_REG_CTRL:
113 val = readl_relaxed(timer->base + CNTP_CTL);
115 case ARCH_TIMER_REG_TVAL:
116 val = readl_relaxed(timer->base + CNTP_TVAL);
119 } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
120 struct arch_timer *timer = to_arch_timer(clk);
122 case ARCH_TIMER_REG_CTRL:
123 val = readl_relaxed(timer->base + CNTV_CTL);
125 case ARCH_TIMER_REG_TVAL:
126 val = readl_relaxed(timer->base + CNTV_TVAL);
130 val = arch_timer_reg_read_cp15(access, reg);
136 static __always_inline irqreturn_t timer_handler(const int access,
137 struct clock_event_device *evt)
141 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, evt);
142 if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
143 ctrl |= ARCH_TIMER_CTRL_IT_MASK;
144 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, evt);
145 evt->event_handler(evt);
152 static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
154 struct clock_event_device *evt = dev_id;
156 return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
159 static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
161 struct clock_event_device *evt = dev_id;
163 return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
166 static irqreturn_t arch_timer_handler_phys_mem(int irq, void *dev_id)
168 struct clock_event_device *evt = dev_id;
170 return timer_handler(ARCH_TIMER_MEM_PHYS_ACCESS, evt);
173 static irqreturn_t arch_timer_handler_virt_mem(int irq, void *dev_id)
175 struct clock_event_device *evt = dev_id;
177 return timer_handler(ARCH_TIMER_MEM_VIRT_ACCESS, evt);
180 static __always_inline void timer_set_mode(const int access, int mode,
181 struct clock_event_device *clk)
185 case CLOCK_EVT_MODE_UNUSED:
186 case CLOCK_EVT_MODE_SHUTDOWN:
187 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
188 ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
189 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
196 static void arch_timer_set_mode_virt(enum clock_event_mode mode,
197 struct clock_event_device *clk)
199 timer_set_mode(ARCH_TIMER_VIRT_ACCESS, mode, clk);
202 static void arch_timer_set_mode_phys(enum clock_event_mode mode,
203 struct clock_event_device *clk)
205 timer_set_mode(ARCH_TIMER_PHYS_ACCESS, mode, clk);
208 static void arch_timer_set_mode_virt_mem(enum clock_event_mode mode,
209 struct clock_event_device *clk)
211 timer_set_mode(ARCH_TIMER_MEM_VIRT_ACCESS, mode, clk);
214 static void arch_timer_set_mode_phys_mem(enum clock_event_mode mode,
215 struct clock_event_device *clk)
217 timer_set_mode(ARCH_TIMER_MEM_PHYS_ACCESS, mode, clk);
220 static __always_inline void set_next_event(const int access, unsigned long evt,
221 struct clock_event_device *clk)
224 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
225 ctrl |= ARCH_TIMER_CTRL_ENABLE;
226 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
227 arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt, clk);
228 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
231 static int arch_timer_set_next_event_virt(unsigned long evt,
232 struct clock_event_device *clk)
234 set_next_event(ARCH_TIMER_VIRT_ACCESS, evt, clk);
238 static int arch_timer_set_next_event_phys(unsigned long evt,
239 struct clock_event_device *clk)
241 set_next_event(ARCH_TIMER_PHYS_ACCESS, evt, clk);
245 static int arch_timer_set_next_event_virt_mem(unsigned long evt,
246 struct clock_event_device *clk)
248 set_next_event(ARCH_TIMER_MEM_VIRT_ACCESS, evt, clk);
252 static int arch_timer_set_next_event_phys_mem(unsigned long evt,
253 struct clock_event_device *clk)
255 set_next_event(ARCH_TIMER_MEM_PHYS_ACCESS, evt, clk);
259 static void __arch_timer_setup(unsigned type,
260 struct clock_event_device *clk)
262 clk->features = CLOCK_EVT_FEAT_ONESHOT;
264 if (type == ARCH_CP15_TIMER) {
265 clk->features |= CLOCK_EVT_FEAT_C3STOP;
266 clk->name = "arch_sys_timer";
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;
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;
279 clk->name = "arch_mem_timer";
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;
287 clk->set_mode = arch_timer_set_mode_phys_mem;
288 clk->set_next_event =
289 arch_timer_set_next_event_phys_mem;
293 clk->set_mode(CLOCK_EVT_MODE_SHUTDOWN, clk);
295 clockevents_config_and_register(clk, arch_timer_rate, 0xf, 0x7fffffff);
298 static int arch_timer_setup(struct clock_event_device *clk)
300 __arch_timer_setup(ARCH_CP15_TIMER, clk);
302 if (arch_timer_use_virtual)
303 enable_percpu_irq(arch_timer_ppi[VIRT_PPI], 0);
305 enable_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI], 0);
306 if (arch_timer_ppi[PHYS_NONSECURE_PPI])
307 enable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI], 0);
310 arch_counter_set_user_access();
316 arch_timer_detect_rate(void __iomem *cntbase, struct device_node *np)
318 /* Who has more than one independent system counter? */
322 /* Try to determine the frequency from the device tree or CNTFRQ */
323 if (of_property_read_u32(np, "clock-frequency", &arch_timer_rate)) {
325 arch_timer_rate = readl_relaxed(cntbase + CNTFRQ);
327 arch_timer_rate = arch_timer_get_cntfrq();
330 /* Check the timer frequency. */
331 if (arch_timer_rate == 0)
332 pr_warn("Architected timer frequency not available\n");
335 static void arch_timer_banner(unsigned type)
337 pr_info("Architected %s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
338 type & ARCH_CP15_TIMER ? "cp15" : "",
339 type == (ARCH_CP15_TIMER | ARCH_MEM_TIMER) ? " and " : "",
340 type & ARCH_MEM_TIMER ? "mmio" : "",
341 (unsigned long)arch_timer_rate / 1000000,
342 (unsigned long)(arch_timer_rate / 10000) % 100,
343 type & ARCH_CP15_TIMER ?
344 arch_timer_use_virtual ? "virt" : "phys" :
346 type == (ARCH_CP15_TIMER | ARCH_MEM_TIMER) ? "/" : "",
347 type & ARCH_MEM_TIMER ?
348 arch_timer_mem_use_virtual ? "virt" : "phys" :
352 u32 arch_timer_get_rate(void)
354 return arch_timer_rate;
357 static u64 arch_counter_get_cntvct_mem(void)
359 u32 vct_lo, vct_hi, tmp_hi;
362 vct_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
363 vct_lo = readl_relaxed(arch_counter_base + CNTVCT_LO);
364 tmp_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
365 } while (vct_hi != tmp_hi);
367 return ((u64) vct_hi << 32) | vct_lo;
371 * Default to cp15 based access because arm64 uses this function for
372 * sched_clock() before DT is probed and the cp15 method is guaranteed
373 * to exist on arm64. arm doesn't use this before DT is probed so even
374 * if we don't have the cp15 accessors we won't have a problem.
376 u64 (*arch_timer_read_counter)(void) = arch_counter_get_cntvct;
378 static cycle_t arch_counter_read(struct clocksource *cs)
380 return arch_timer_read_counter();
383 static cycle_t arch_counter_read_cc(const struct cyclecounter *cc)
385 return arch_timer_read_counter();
388 static struct clocksource clocksource_counter = {
389 .name = "arch_sys_counter",
391 .read = arch_counter_read,
392 .mask = CLOCKSOURCE_MASK(56),
393 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
396 static struct cyclecounter cyclecounter = {
397 .read = arch_counter_read_cc,
398 .mask = CLOCKSOURCE_MASK(56),
401 static struct timecounter timecounter;
403 struct timecounter *arch_timer_get_timecounter(void)
408 static void __init arch_counter_register(unsigned type)
412 /* Register the CP15 based counter if we have one */
413 if (type & ARCH_CP15_TIMER)
414 arch_timer_read_counter = arch_counter_get_cntvct;
416 arch_timer_read_counter = arch_counter_get_cntvct_mem;
418 start_count = arch_timer_read_counter();
419 clocksource_register_hz(&clocksource_counter, arch_timer_rate);
420 cyclecounter.mult = clocksource_counter.mult;
421 cyclecounter.shift = clocksource_counter.shift;
422 timecounter_init(&timecounter, &cyclecounter, start_count);
425 static void arch_timer_stop(struct clock_event_device *clk)
427 pr_debug("arch_timer_teardown disable IRQ%d cpu #%d\n",
428 clk->irq, smp_processor_id());
430 if (arch_timer_use_virtual)
431 disable_percpu_irq(arch_timer_ppi[VIRT_PPI]);
433 disable_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI]);
434 if (arch_timer_ppi[PHYS_NONSECURE_PPI])
435 disable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI]);
438 clk->set_mode(CLOCK_EVT_MODE_UNUSED, clk);
441 static int arch_timer_cpu_notify(struct notifier_block *self,
442 unsigned long action, void *hcpu)
445 * Grab cpu pointer in each case to avoid spurious
446 * preemptible warnings
448 switch (action & ~CPU_TASKS_FROZEN) {
450 arch_timer_setup(this_cpu_ptr(arch_timer_evt));
453 arch_timer_stop(this_cpu_ptr(arch_timer_evt));
460 static struct notifier_block arch_timer_cpu_nb = {
461 .notifier_call = arch_timer_cpu_notify,
464 static int __init arch_timer_register(void)
469 arch_timer_evt = alloc_percpu(struct clock_event_device);
470 if (!arch_timer_evt) {
475 clocksource_register_hz(&clocksource_counter, arch_timer_rate);
476 cyclecounter.mult = clocksource_counter.mult;
477 cyclecounter.shift = clocksource_counter.shift;
478 timecounter_init(&timecounter, &cyclecounter,
479 arch_counter_get_cntvct());
481 /* 56 bits minimum, so we assume worst case rollover */
482 sched_clock_register(arch_timer_read_counter, 56, arch_timer_rate);
484 if (arch_timer_use_virtual) {
485 ppi = arch_timer_ppi[VIRT_PPI];
486 err = request_percpu_irq(ppi, arch_timer_handler_virt,
487 "arch_timer", arch_timer_evt);
489 ppi = arch_timer_ppi[PHYS_SECURE_PPI];
490 err = request_percpu_irq(ppi, arch_timer_handler_phys,
491 "arch_timer", arch_timer_evt);
492 if (!err && arch_timer_ppi[PHYS_NONSECURE_PPI]) {
493 ppi = arch_timer_ppi[PHYS_NONSECURE_PPI];
494 err = request_percpu_irq(ppi, arch_timer_handler_phys,
495 "arch_timer", arch_timer_evt);
497 free_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI],
503 pr_err("arch_timer: can't register interrupt %d (%d)\n",
508 err = register_cpu_notifier(&arch_timer_cpu_nb);
512 /* Immediately configure the timer on the boot CPU */
513 arch_timer_setup(this_cpu_ptr(arch_timer_evt));
518 if (arch_timer_use_virtual)
519 free_percpu_irq(arch_timer_ppi[VIRT_PPI], arch_timer_evt);
521 free_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI],
523 if (arch_timer_ppi[PHYS_NONSECURE_PPI])
524 free_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI],
529 free_percpu(arch_timer_evt);
534 static int __init arch_timer_mem_register(void __iomem *base, unsigned int irq)
538 struct arch_timer *t;
540 t = kzalloc(sizeof(*t), GFP_KERNEL);
546 __arch_timer_setup(ARCH_MEM_TIMER, &t->evt);
548 if (arch_timer_mem_use_virtual)
549 func = arch_timer_handler_virt_mem;
551 func = arch_timer_handler_phys_mem;
553 ret = request_irq(irq, func, IRQF_TIMER, "arch_mem_timer", &t->evt);
555 pr_err("arch_timer: Failed to request mem timer irq\n");
562 static const struct of_device_id arch_timer_of_match[] __initconst = {
563 { .compatible = "arm,armv7-timer", },
564 { .compatible = "arm,armv8-timer", },
568 static const struct of_device_id arch_timer_mem_of_match[] __initconst = {
569 { .compatible = "arm,armv7-timer-mem", },
573 static void __init arch_timer_common_init(void)
575 unsigned mask = ARCH_CP15_TIMER | ARCH_MEM_TIMER;
577 /* Wait until both nodes are probed if we have two timers */
578 if ((arch_timers_present & mask) != mask) {
579 if (of_find_matching_node(NULL, arch_timer_mem_of_match) &&
580 !(arch_timers_present & ARCH_MEM_TIMER))
582 if (of_find_matching_node(NULL, arch_timer_of_match) &&
583 !(arch_timers_present & ARCH_CP15_TIMER))
587 arch_timer_banner(arch_timers_present);
588 arch_counter_register(arch_timers_present);
589 arch_timer_arch_init();
592 static void __init arch_timer_init(struct device_node *np)
596 if (arch_timers_present & ARCH_CP15_TIMER) {
597 pr_warn("arch_timer: multiple nodes in dt, skipping\n");
601 arch_timers_present |= ARCH_CP15_TIMER;
602 for (i = PHYS_SECURE_PPI; i < MAX_TIMER_PPI; i++)
603 arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
604 arch_timer_detect_rate(NULL, np);
607 * If HYP mode is available, we know that the physical timer
608 * has been configured to be accessible from PL1. Use it, so
609 * that a guest can use the virtual timer instead.
611 * If no interrupt provided for virtual timer, we'll have to
612 * stick to the physical timer. It'd better be accessible...
614 if (is_hyp_mode_available() || !arch_timer_ppi[VIRT_PPI]) {
615 arch_timer_use_virtual = false;
617 if (!arch_timer_ppi[PHYS_SECURE_PPI] ||
618 !arch_timer_ppi[PHYS_NONSECURE_PPI]) {
619 pr_warn("arch_timer: No interrupt available, giving up\n");
624 arch_timer_register();
625 arch_timer_common_init();
627 CLOCKSOURCE_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_init);
628 CLOCKSOURCE_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_init);
630 static void __init arch_timer_mem_init(struct device_node *np)
632 struct device_node *frame, *best_frame = NULL;
633 void __iomem *cntctlbase, *base;
637 arch_timers_present |= ARCH_MEM_TIMER;
638 cntctlbase = of_iomap(np, 0);
640 pr_err("arch_timer: Can't find CNTCTLBase\n");
644 cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
648 * Try to find a virtual capable frame. Otherwise fall back to a
649 * physical capable frame.
651 for_each_available_child_of_node(np, frame) {
654 if (of_property_read_u32(frame, "frame-number", &n)) {
655 pr_err("arch_timer: Missing frame-number\n");
656 of_node_put(best_frame);
661 if (cnttidr & CNTTIDR_VIRT(n)) {
662 of_node_put(best_frame);
664 arch_timer_mem_use_virtual = true;
667 of_node_put(best_frame);
668 best_frame = of_node_get(frame);
671 base = arch_counter_base = of_iomap(best_frame, 0);
673 pr_err("arch_timer: Can't map frame's registers\n");
674 of_node_put(best_frame);
678 if (arch_timer_mem_use_virtual)
679 irq = irq_of_parse_and_map(best_frame, 1);
681 irq = irq_of_parse_and_map(best_frame, 0);
682 of_node_put(best_frame);
684 pr_err("arch_timer: Frame missing %s irq",
685 arch_timer_mem_use_virtual ? "virt" : "phys");
689 arch_timer_detect_rate(base, np);
690 arch_timer_mem_register(base, irq);
691 arch_timer_common_init();
693 CLOCKSOURCE_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem",
694 arch_timer_mem_init);