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.
12 #define pr_fmt(fmt) "arm_arch_timer: " fmt
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/device.h>
17 #include <linux/smp.h>
18 #include <linux/cpu.h>
19 #include <linux/cpu_pm.h>
20 #include <linux/clockchips.h>
21 #include <linux/clocksource.h>
22 #include <linux/interrupt.h>
23 #include <linux/of_irq.h>
24 #include <linux/of_address.h>
26 #include <linux/slab.h>
27 #include <linux/sched/clock.h>
28 #include <linux/sched_clock.h>
29 #include <linux/acpi.h>
31 #include <asm/arch_timer.h>
34 #include <clocksource/arm_arch_timer.h>
37 #define pr_fmt(fmt) "arch_timer: " fmt
40 #define CNTTIDR_VIRT(n) (BIT(1) << ((n) * 4))
42 #define CNTACR(n) (0x40 + ((n) * 4))
43 #define CNTACR_RPCT BIT(0)
44 #define CNTACR_RVCT BIT(1)
45 #define CNTACR_RFRQ BIT(2)
46 #define CNTACR_RVOFF BIT(3)
47 #define CNTACR_RWVT BIT(4)
48 #define CNTACR_RWPT BIT(5)
50 #define CNTVCT_LO 0x08
51 #define CNTVCT_HI 0x0c
53 #define CNTP_TVAL 0x28
55 #define CNTV_TVAL 0x38
58 static unsigned arch_timers_present __initdata
;
60 static void __iomem
*arch_counter_base
;
64 struct clock_event_device evt
;
67 #define to_arch_timer(e) container_of(e, struct arch_timer, evt)
69 static u32 arch_timer_rate
;
70 static int arch_timer_ppi
[ARCH_TIMER_MAX_TIMER_PPI
];
72 static struct clock_event_device __percpu
*arch_timer_evt
;
74 static enum arch_timer_ppi_nr arch_timer_uses_ppi
= ARCH_TIMER_VIRT_PPI
;
75 static bool arch_timer_c3stop
;
76 static bool arch_timer_mem_use_virtual
;
77 static bool arch_counter_suspend_stop
;
78 static bool vdso_default
= true;
80 static cpumask_t evtstrm_available
= CPU_MASK_NONE
;
81 static bool evtstrm_enable
= IS_ENABLED(CONFIG_ARM_ARCH_TIMER_EVTSTREAM
);
83 static int __init
early_evtstrm_cfg(char *buf
)
85 return strtobool(buf
, &evtstrm_enable
);
87 early_param("clocksource.arm_arch_timer.evtstrm", early_evtstrm_cfg
);
90 * Architected system timer support.
93 static __always_inline
94 void arch_timer_reg_write(int access
, enum arch_timer_reg reg
, u32 val
,
95 struct clock_event_device
*clk
)
97 if (access
== ARCH_TIMER_MEM_PHYS_ACCESS
) {
98 struct arch_timer
*timer
= to_arch_timer(clk
);
100 case ARCH_TIMER_REG_CTRL
:
101 writel_relaxed(val
, timer
->base
+ CNTP_CTL
);
103 case ARCH_TIMER_REG_TVAL
:
104 writel_relaxed(val
, timer
->base
+ CNTP_TVAL
);
107 } else if (access
== ARCH_TIMER_MEM_VIRT_ACCESS
) {
108 struct arch_timer
*timer
= to_arch_timer(clk
);
110 case ARCH_TIMER_REG_CTRL
:
111 writel_relaxed(val
, timer
->base
+ CNTV_CTL
);
113 case ARCH_TIMER_REG_TVAL
:
114 writel_relaxed(val
, timer
->base
+ CNTV_TVAL
);
118 arch_timer_reg_write_cp15(access
, reg
, val
);
122 static __always_inline
123 u32
arch_timer_reg_read(int access
, enum arch_timer_reg reg
,
124 struct clock_event_device
*clk
)
128 if (access
== ARCH_TIMER_MEM_PHYS_ACCESS
) {
129 struct arch_timer
*timer
= to_arch_timer(clk
);
131 case ARCH_TIMER_REG_CTRL
:
132 val
= readl_relaxed(timer
->base
+ CNTP_CTL
);
134 case ARCH_TIMER_REG_TVAL
:
135 val
= readl_relaxed(timer
->base
+ CNTP_TVAL
);
138 } else if (access
== ARCH_TIMER_MEM_VIRT_ACCESS
) {
139 struct arch_timer
*timer
= to_arch_timer(clk
);
141 case ARCH_TIMER_REG_CTRL
:
142 val
= readl_relaxed(timer
->base
+ CNTV_CTL
);
144 case ARCH_TIMER_REG_TVAL
:
145 val
= readl_relaxed(timer
->base
+ CNTV_TVAL
);
149 val
= arch_timer_reg_read_cp15(access
, reg
);
156 * Default to cp15 based access because arm64 uses this function for
157 * sched_clock() before DT is probed and the cp15 method is guaranteed
158 * to exist on arm64. arm doesn't use this before DT is probed so even
159 * if we don't have the cp15 accessors we won't have a problem.
161 u64 (*arch_timer_read_counter
)(void) = arch_counter_get_cntvct
;
162 EXPORT_SYMBOL_GPL(arch_timer_read_counter
);
164 static u64
arch_counter_read(struct clocksource
*cs
)
166 return arch_timer_read_counter();
169 static u64
arch_counter_read_cc(const struct cyclecounter
*cc
)
171 return arch_timer_read_counter();
174 static struct clocksource clocksource_counter
= {
175 .name
= "arch_sys_counter",
177 .read
= arch_counter_read
,
178 .mask
= CLOCKSOURCE_MASK(56),
179 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
182 static struct cyclecounter cyclecounter __ro_after_init
= {
183 .read
= arch_counter_read_cc
,
184 .mask
= CLOCKSOURCE_MASK(56),
187 struct ate_acpi_oem_info
{
188 char oem_id
[ACPI_OEM_ID_SIZE
+ 1];
189 char oem_table_id
[ACPI_OEM_TABLE_ID_SIZE
+ 1];
193 #ifdef CONFIG_FSL_ERRATUM_A008585
195 * The number of retries is an arbitrary value well beyond the highest number
196 * of iterations the loop has been observed to take.
198 #define __fsl_a008585_read_reg(reg) ({ \
200 int _retries = 200; \
203 _old = read_sysreg(reg); \
204 _new = read_sysreg(reg); \
206 } while (unlikely(_old != _new) && _retries); \
208 WARN_ON_ONCE(!_retries); \
212 static u32 notrace
fsl_a008585_read_cntp_tval_el0(void)
214 return __fsl_a008585_read_reg(cntp_tval_el0
);
217 static u32 notrace
fsl_a008585_read_cntv_tval_el0(void)
219 return __fsl_a008585_read_reg(cntv_tval_el0
);
222 static u64 notrace
fsl_a008585_read_cntpct_el0(void)
224 return __fsl_a008585_read_reg(cntpct_el0
);
227 static u64 notrace
fsl_a008585_read_cntvct_el0(void)
229 return __fsl_a008585_read_reg(cntvct_el0
);
233 #ifdef CONFIG_HISILICON_ERRATUM_161010101
235 * Verify whether the value of the second read is larger than the first by
236 * less than 32 is the only way to confirm the value is correct, so clear the
237 * lower 5 bits to check whether the difference is greater than 32 or not.
238 * Theoretically the erratum should not occur more than twice in succession
239 * when reading the system counter, but it is possible that some interrupts
240 * may lead to more than twice read errors, triggering the warning, so setting
241 * the number of retries far beyond the number of iterations the loop has been
244 #define __hisi_161010101_read_reg(reg) ({ \
249 _old = read_sysreg(reg); \
250 _new = read_sysreg(reg); \
252 } while (unlikely((_new - _old) >> 5) && _retries); \
254 WARN_ON_ONCE(!_retries); \
258 static u32 notrace
hisi_161010101_read_cntp_tval_el0(void)
260 return __hisi_161010101_read_reg(cntp_tval_el0
);
263 static u32 notrace
hisi_161010101_read_cntv_tval_el0(void)
265 return __hisi_161010101_read_reg(cntv_tval_el0
);
268 static u64 notrace
hisi_161010101_read_cntpct_el0(void)
270 return __hisi_161010101_read_reg(cntpct_el0
);
273 static u64 notrace
hisi_161010101_read_cntvct_el0(void)
275 return __hisi_161010101_read_reg(cntvct_el0
);
278 static struct ate_acpi_oem_info hisi_161010101_oem_info
[] = {
280 * Note that trailing spaces are required to properly match
281 * the OEM table information.
285 .oem_table_id
= "HIP05 ",
290 .oem_table_id
= "HIP06 ",
295 .oem_table_id
= "HIP07 ",
298 { /* Sentinel indicating the end of the OEM array */ },
302 #ifdef CONFIG_ARM64_ERRATUM_858921
303 static u64 notrace
arm64_858921_read_cntpct_el0(void)
307 old
= read_sysreg(cntpct_el0
);
308 new = read_sysreg(cntpct_el0
);
309 return (((old
^ new) >> 32) & 1) ? old
: new;
312 static u64 notrace
arm64_858921_read_cntvct_el0(void)
316 old
= read_sysreg(cntvct_el0
);
317 new = read_sysreg(cntvct_el0
);
318 return (((old
^ new) >> 32) & 1) ? old
: new;
322 #ifdef CONFIG_ARM64_ERRATUM_1188873
323 static u64 notrace
arm64_1188873_read_cntvct_el0(void)
325 return read_sysreg(cntvct_el0
);
329 #ifdef CONFIG_SUN50I_ERRATUM_UNKNOWN1
331 * The low bits of the counter registers are indeterminate while bit 10 or
332 * greater is rolling over. Since the counter value can jump both backward
333 * (7ff -> 000 -> 800) and forward (7ff -> fff -> 800), ignore register values
334 * with all ones or all zeros in the low bits. Bound the loop by the maximum
335 * number of CPU cycles in 3 consecutive 24 MHz counter periods.
337 #define __sun50i_a64_read_reg(reg) ({ \
339 int _retries = 150; \
342 _val = read_sysreg(reg); \
344 } while (((_val + 1) & GENMASK(9, 0)) <= 1 && _retries); \
346 WARN_ON_ONCE(!_retries); \
350 static u64 notrace
sun50i_a64_read_cntpct_el0(void)
352 return __sun50i_a64_read_reg(cntpct_el0
);
355 static u64 notrace
sun50i_a64_read_cntvct_el0(void)
357 return __sun50i_a64_read_reg(cntvct_el0
);
360 static u32 notrace
sun50i_a64_read_cntp_tval_el0(void)
362 return read_sysreg(cntp_cval_el0
) - sun50i_a64_read_cntpct_el0();
365 static u32 notrace
sun50i_a64_read_cntv_tval_el0(void)
367 return read_sysreg(cntv_cval_el0
) - sun50i_a64_read_cntvct_el0();
371 #ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
372 DEFINE_PER_CPU(const struct arch_timer_erratum_workaround
*, timer_unstable_counter_workaround
);
373 EXPORT_SYMBOL_GPL(timer_unstable_counter_workaround
);
375 DEFINE_STATIC_KEY_FALSE(arch_timer_read_ool_enabled
);
376 EXPORT_SYMBOL_GPL(arch_timer_read_ool_enabled
);
378 static void erratum_set_next_event_tval_generic(const int access
, unsigned long evt
,
379 struct clock_event_device
*clk
)
384 ctrl
= arch_timer_reg_read(access
, ARCH_TIMER_REG_CTRL
, clk
);
385 ctrl
|= ARCH_TIMER_CTRL_ENABLE
;
386 ctrl
&= ~ARCH_TIMER_CTRL_IT_MASK
;
388 if (access
== ARCH_TIMER_PHYS_ACCESS
) {
389 cval
= evt
+ arch_counter_get_cntpct();
390 write_sysreg(cval
, cntp_cval_el0
);
392 cval
= evt
+ arch_counter_get_cntvct();
393 write_sysreg(cval
, cntv_cval_el0
);
396 arch_timer_reg_write(access
, ARCH_TIMER_REG_CTRL
, ctrl
, clk
);
399 static __maybe_unused
int erratum_set_next_event_tval_virt(unsigned long evt
,
400 struct clock_event_device
*clk
)
402 erratum_set_next_event_tval_generic(ARCH_TIMER_VIRT_ACCESS
, evt
, clk
);
406 static __maybe_unused
int erratum_set_next_event_tval_phys(unsigned long evt
,
407 struct clock_event_device
*clk
)
409 erratum_set_next_event_tval_generic(ARCH_TIMER_PHYS_ACCESS
, evt
, clk
);
413 static const struct arch_timer_erratum_workaround ool_workarounds
[] = {
414 #ifdef CONFIG_FSL_ERRATUM_A008585
416 .match_type
= ate_match_dt
,
417 .id
= "fsl,erratum-a008585",
418 .desc
= "Freescale erratum a005858",
419 .read_cntp_tval_el0
= fsl_a008585_read_cntp_tval_el0
,
420 .read_cntv_tval_el0
= fsl_a008585_read_cntv_tval_el0
,
421 .read_cntpct_el0
= fsl_a008585_read_cntpct_el0
,
422 .read_cntvct_el0
= fsl_a008585_read_cntvct_el0
,
423 .set_next_event_phys
= erratum_set_next_event_tval_phys
,
424 .set_next_event_virt
= erratum_set_next_event_tval_virt
,
427 #ifdef CONFIG_HISILICON_ERRATUM_161010101
429 .match_type
= ate_match_dt
,
430 .id
= "hisilicon,erratum-161010101",
431 .desc
= "HiSilicon erratum 161010101",
432 .read_cntp_tval_el0
= hisi_161010101_read_cntp_tval_el0
,
433 .read_cntv_tval_el0
= hisi_161010101_read_cntv_tval_el0
,
434 .read_cntpct_el0
= hisi_161010101_read_cntpct_el0
,
435 .read_cntvct_el0
= hisi_161010101_read_cntvct_el0
,
436 .set_next_event_phys
= erratum_set_next_event_tval_phys
,
437 .set_next_event_virt
= erratum_set_next_event_tval_virt
,
440 .match_type
= ate_match_acpi_oem_info
,
441 .id
= hisi_161010101_oem_info
,
442 .desc
= "HiSilicon erratum 161010101",
443 .read_cntp_tval_el0
= hisi_161010101_read_cntp_tval_el0
,
444 .read_cntv_tval_el0
= hisi_161010101_read_cntv_tval_el0
,
445 .read_cntpct_el0
= hisi_161010101_read_cntpct_el0
,
446 .read_cntvct_el0
= hisi_161010101_read_cntvct_el0
,
447 .set_next_event_phys
= erratum_set_next_event_tval_phys
,
448 .set_next_event_virt
= erratum_set_next_event_tval_virt
,
451 #ifdef CONFIG_ARM64_ERRATUM_858921
453 .match_type
= ate_match_local_cap_id
,
454 .id
= (void *)ARM64_WORKAROUND_858921
,
455 .desc
= "ARM erratum 858921",
456 .read_cntpct_el0
= arm64_858921_read_cntpct_el0
,
457 .read_cntvct_el0
= arm64_858921_read_cntvct_el0
,
460 #ifdef CONFIG_ARM64_ERRATUM_1188873
462 .match_type
= ate_match_local_cap_id
,
463 .id
= (void *)ARM64_WORKAROUND_1188873
,
464 .desc
= "ARM erratum 1188873",
465 .read_cntvct_el0
= arm64_1188873_read_cntvct_el0
,
468 #ifdef CONFIG_SUN50I_ERRATUM_UNKNOWN1
470 .match_type
= ate_match_dt
,
471 .id
= "allwinner,erratum-unknown1",
472 .desc
= "Allwinner erratum UNKNOWN1",
473 .read_cntp_tval_el0
= sun50i_a64_read_cntp_tval_el0
,
474 .read_cntv_tval_el0
= sun50i_a64_read_cntv_tval_el0
,
475 .read_cntpct_el0
= sun50i_a64_read_cntpct_el0
,
476 .read_cntvct_el0
= sun50i_a64_read_cntvct_el0
,
477 .set_next_event_phys
= erratum_set_next_event_tval_phys
,
478 .set_next_event_virt
= erratum_set_next_event_tval_virt
,
483 typedef bool (*ate_match_fn_t
)(const struct arch_timer_erratum_workaround
*,
487 bool arch_timer_check_dt_erratum(const struct arch_timer_erratum_workaround
*wa
,
490 const struct device_node
*np
= arg
;
492 return of_property_read_bool(np
, wa
->id
);
496 bool arch_timer_check_local_cap_erratum(const struct arch_timer_erratum_workaround
*wa
,
499 return this_cpu_has_cap((uintptr_t)wa
->id
);
504 bool arch_timer_check_acpi_oem_erratum(const struct arch_timer_erratum_workaround
*wa
,
507 static const struct ate_acpi_oem_info empty_oem_info
= {};
508 const struct ate_acpi_oem_info
*info
= wa
->id
;
509 const struct acpi_table_header
*table
= arg
;
511 /* Iterate over the ACPI OEM info array, looking for a match */
512 while (memcmp(info
, &empty_oem_info
, sizeof(*info
))) {
513 if (!memcmp(info
->oem_id
, table
->oem_id
, ACPI_OEM_ID_SIZE
) &&
514 !memcmp(info
->oem_table_id
, table
->oem_table_id
, ACPI_OEM_TABLE_ID_SIZE
) &&
515 info
->oem_revision
== table
->oem_revision
)
524 static const struct arch_timer_erratum_workaround
*
525 arch_timer_iterate_errata(enum arch_timer_erratum_match_type type
,
526 ate_match_fn_t match_fn
,
531 for (i
= 0; i
< ARRAY_SIZE(ool_workarounds
); i
++) {
532 if (ool_workarounds
[i
].match_type
!= type
)
535 if (match_fn(&ool_workarounds
[i
], arg
))
536 return &ool_workarounds
[i
];
543 void arch_timer_enable_workaround(const struct arch_timer_erratum_workaround
*wa
,
549 __this_cpu_write(timer_unstable_counter_workaround
, wa
);
551 for_each_possible_cpu(i
)
552 per_cpu(timer_unstable_counter_workaround
, i
) = wa
;
556 * Use the locked version, as we're called from the CPU
557 * hotplug framework. Otherwise, we end-up in deadlock-land.
559 static_branch_enable_cpuslocked(&arch_timer_read_ool_enabled
);
562 * Don't use the vdso fastpath if errata require using the
563 * out-of-line counter accessor. We may change our mind pretty
564 * late in the game (with a per-CPU erratum, for example), so
565 * change both the default value and the vdso itself.
567 if (wa
->read_cntvct_el0
) {
568 clocksource_counter
.archdata
.vdso_direct
= false;
569 vdso_default
= false;
573 static void arch_timer_check_ool_workaround(enum arch_timer_erratum_match_type type
,
576 const struct arch_timer_erratum_workaround
*wa
;
577 ate_match_fn_t match_fn
= NULL
;
582 match_fn
= arch_timer_check_dt_erratum
;
584 case ate_match_local_cap_id
:
585 match_fn
= arch_timer_check_local_cap_erratum
;
588 case ate_match_acpi_oem_info
:
589 match_fn
= arch_timer_check_acpi_oem_erratum
;
596 wa
= arch_timer_iterate_errata(type
, match_fn
, arg
);
600 if (needs_unstable_timer_counter_workaround()) {
601 const struct arch_timer_erratum_workaround
*__wa
;
602 __wa
= __this_cpu_read(timer_unstable_counter_workaround
);
603 if (__wa
&& wa
!= __wa
)
604 pr_warn("Can't enable workaround for %s (clashes with %s\n)",
605 wa
->desc
, __wa
->desc
);
611 arch_timer_enable_workaround(wa
, local
);
612 pr_info("Enabling %s workaround for %s\n",
613 local
? "local" : "global", wa
->desc
);
616 #define erratum_handler(fn, r, ...) \
619 if (needs_unstable_timer_counter_workaround()) { \
620 const struct arch_timer_erratum_workaround *__wa; \
621 __wa = __this_cpu_read(timer_unstable_counter_workaround); \
622 if (__wa && __wa->fn) { \
623 r = __wa->fn(__VA_ARGS__); \
634 static bool arch_timer_this_cpu_has_cntvct_wa(void)
636 const struct arch_timer_erratum_workaround
*wa
;
638 wa
= __this_cpu_read(timer_unstable_counter_workaround
);
639 return wa
&& wa
->read_cntvct_el0
;
642 #define arch_timer_check_ool_workaround(t,a) do { } while(0)
643 #define erratum_set_next_event_tval_virt(...) ({BUG(); 0;})
644 #define erratum_set_next_event_tval_phys(...) ({BUG(); 0;})
645 #define erratum_handler(fn, r, ...) ({false;})
646 #define arch_timer_this_cpu_has_cntvct_wa() ({false;})
647 #endif /* CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND */
649 static __always_inline irqreturn_t
timer_handler(const int access
,
650 struct clock_event_device
*evt
)
654 ctrl
= arch_timer_reg_read(access
, ARCH_TIMER_REG_CTRL
, evt
);
655 if (ctrl
& ARCH_TIMER_CTRL_IT_STAT
) {
656 ctrl
|= ARCH_TIMER_CTRL_IT_MASK
;
657 arch_timer_reg_write(access
, ARCH_TIMER_REG_CTRL
, ctrl
, evt
);
658 evt
->event_handler(evt
);
665 static irqreturn_t
arch_timer_handler_virt(int irq
, void *dev_id
)
667 struct clock_event_device
*evt
= dev_id
;
669 return timer_handler(ARCH_TIMER_VIRT_ACCESS
, evt
);
672 static irqreturn_t
arch_timer_handler_phys(int irq
, void *dev_id
)
674 struct clock_event_device
*evt
= dev_id
;
676 return timer_handler(ARCH_TIMER_PHYS_ACCESS
, evt
);
679 static irqreturn_t
arch_timer_handler_phys_mem(int irq
, void *dev_id
)
681 struct clock_event_device
*evt
= dev_id
;
683 return timer_handler(ARCH_TIMER_MEM_PHYS_ACCESS
, evt
);
686 static irqreturn_t
arch_timer_handler_virt_mem(int irq
, void *dev_id
)
688 struct clock_event_device
*evt
= dev_id
;
690 return timer_handler(ARCH_TIMER_MEM_VIRT_ACCESS
, evt
);
693 static __always_inline
int timer_shutdown(const int access
,
694 struct clock_event_device
*clk
)
698 ctrl
= arch_timer_reg_read(access
, ARCH_TIMER_REG_CTRL
, clk
);
699 ctrl
&= ~ARCH_TIMER_CTRL_ENABLE
;
700 arch_timer_reg_write(access
, ARCH_TIMER_REG_CTRL
, ctrl
, clk
);
705 static int arch_timer_shutdown_virt(struct clock_event_device
*clk
)
707 return timer_shutdown(ARCH_TIMER_VIRT_ACCESS
, clk
);
710 static int arch_timer_shutdown_phys(struct clock_event_device
*clk
)
712 return timer_shutdown(ARCH_TIMER_PHYS_ACCESS
, clk
);
715 static int arch_timer_shutdown_virt_mem(struct clock_event_device
*clk
)
717 return timer_shutdown(ARCH_TIMER_MEM_VIRT_ACCESS
, clk
);
720 static int arch_timer_shutdown_phys_mem(struct clock_event_device
*clk
)
722 return timer_shutdown(ARCH_TIMER_MEM_PHYS_ACCESS
, clk
);
725 static __always_inline
void set_next_event(const int access
, unsigned long evt
,
726 struct clock_event_device
*clk
)
729 ctrl
= arch_timer_reg_read(access
, ARCH_TIMER_REG_CTRL
, clk
);
730 ctrl
|= ARCH_TIMER_CTRL_ENABLE
;
731 ctrl
&= ~ARCH_TIMER_CTRL_IT_MASK
;
732 arch_timer_reg_write(access
, ARCH_TIMER_REG_TVAL
, evt
, clk
);
733 arch_timer_reg_write(access
, ARCH_TIMER_REG_CTRL
, ctrl
, clk
);
736 static int arch_timer_set_next_event_virt(unsigned long evt
,
737 struct clock_event_device
*clk
)
741 if (erratum_handler(set_next_event_virt
, ret
, evt
, clk
))
744 set_next_event(ARCH_TIMER_VIRT_ACCESS
, evt
, clk
);
748 static int arch_timer_set_next_event_phys(unsigned long evt
,
749 struct clock_event_device
*clk
)
753 if (erratum_handler(set_next_event_phys
, ret
, evt
, clk
))
756 set_next_event(ARCH_TIMER_PHYS_ACCESS
, evt
, clk
);
760 static int arch_timer_set_next_event_virt_mem(unsigned long evt
,
761 struct clock_event_device
*clk
)
763 set_next_event(ARCH_TIMER_MEM_VIRT_ACCESS
, evt
, clk
);
767 static int arch_timer_set_next_event_phys_mem(unsigned long evt
,
768 struct clock_event_device
*clk
)
770 set_next_event(ARCH_TIMER_MEM_PHYS_ACCESS
, evt
, clk
);
774 static void __arch_timer_setup(unsigned type
,
775 struct clock_event_device
*clk
)
777 clk
->features
= CLOCK_EVT_FEAT_ONESHOT
;
779 if (type
== ARCH_TIMER_TYPE_CP15
) {
780 if (arch_timer_c3stop
)
781 clk
->features
|= CLOCK_EVT_FEAT_C3STOP
;
782 clk
->name
= "arch_sys_timer";
784 clk
->cpumask
= cpumask_of(smp_processor_id());
785 clk
->irq
= arch_timer_ppi
[arch_timer_uses_ppi
];
786 switch (arch_timer_uses_ppi
) {
787 case ARCH_TIMER_VIRT_PPI
:
788 clk
->set_state_shutdown
= arch_timer_shutdown_virt
;
789 clk
->set_state_oneshot_stopped
= arch_timer_shutdown_virt
;
790 clk
->set_next_event
= arch_timer_set_next_event_virt
;
792 case ARCH_TIMER_PHYS_SECURE_PPI
:
793 case ARCH_TIMER_PHYS_NONSECURE_PPI
:
794 case ARCH_TIMER_HYP_PPI
:
795 clk
->set_state_shutdown
= arch_timer_shutdown_phys
;
796 clk
->set_state_oneshot_stopped
= arch_timer_shutdown_phys
;
797 clk
->set_next_event
= arch_timer_set_next_event_phys
;
803 arch_timer_check_ool_workaround(ate_match_local_cap_id
, NULL
);
805 clk
->features
|= CLOCK_EVT_FEAT_DYNIRQ
;
806 clk
->name
= "arch_mem_timer";
808 clk
->cpumask
= cpu_possible_mask
;
809 if (arch_timer_mem_use_virtual
) {
810 clk
->set_state_shutdown
= arch_timer_shutdown_virt_mem
;
811 clk
->set_state_oneshot_stopped
= arch_timer_shutdown_virt_mem
;
812 clk
->set_next_event
=
813 arch_timer_set_next_event_virt_mem
;
815 clk
->set_state_shutdown
= arch_timer_shutdown_phys_mem
;
816 clk
->set_state_oneshot_stopped
= arch_timer_shutdown_phys_mem
;
817 clk
->set_next_event
=
818 arch_timer_set_next_event_phys_mem
;
822 clk
->set_state_shutdown(clk
);
824 clockevents_config_and_register(clk
, arch_timer_rate
, 0xf, 0x7fffffff);
827 static void arch_timer_evtstrm_enable(int divider
)
829 u32 cntkctl
= arch_timer_get_cntkctl();
831 cntkctl
&= ~ARCH_TIMER_EVT_TRIGGER_MASK
;
832 /* Set the divider and enable virtual event stream */
833 cntkctl
|= (divider
<< ARCH_TIMER_EVT_TRIGGER_SHIFT
)
834 | ARCH_TIMER_VIRT_EVT_EN
;
835 arch_timer_set_cntkctl(cntkctl
);
836 elf_hwcap
|= HWCAP_EVTSTRM
;
838 compat_elf_hwcap
|= COMPAT_HWCAP_EVTSTRM
;
840 cpumask_set_cpu(smp_processor_id(), &evtstrm_available
);
843 static void arch_timer_configure_evtstream(void)
845 int evt_stream_div
, pos
;
847 /* Find the closest power of two to the divisor */
848 evt_stream_div
= arch_timer_rate
/ ARCH_TIMER_EVT_STREAM_FREQ
;
849 pos
= fls(evt_stream_div
);
850 if (pos
> 1 && !(evt_stream_div
& (1 << (pos
- 2))))
852 /* enable event stream */
853 arch_timer_evtstrm_enable(min(pos
, 15));
856 static void arch_counter_set_user_access(void)
858 u32 cntkctl
= arch_timer_get_cntkctl();
860 /* Disable user access to the timers and both counters */
861 /* Also disable virtual event stream */
862 cntkctl
&= ~(ARCH_TIMER_USR_PT_ACCESS_EN
863 | ARCH_TIMER_USR_VT_ACCESS_EN
864 | ARCH_TIMER_USR_VCT_ACCESS_EN
865 | ARCH_TIMER_VIRT_EVT_EN
866 | ARCH_TIMER_USR_PCT_ACCESS_EN
);
869 * Enable user access to the virtual counter if it doesn't
870 * need to be workaround. The vdso may have been already
873 if (arch_timer_this_cpu_has_cntvct_wa())
874 pr_info("CPU%d: Trapping CNTVCT access\n", smp_processor_id());
876 cntkctl
|= ARCH_TIMER_USR_VCT_ACCESS_EN
;
878 arch_timer_set_cntkctl(cntkctl
);
881 static bool arch_timer_has_nonsecure_ppi(void)
883 return (arch_timer_uses_ppi
== ARCH_TIMER_PHYS_SECURE_PPI
&&
884 arch_timer_ppi
[ARCH_TIMER_PHYS_NONSECURE_PPI
]);
887 static u32
check_ppi_trigger(int irq
)
889 u32 flags
= irq_get_trigger_type(irq
);
891 if (flags
!= IRQF_TRIGGER_HIGH
&& flags
!= IRQF_TRIGGER_LOW
) {
892 pr_warn("WARNING: Invalid trigger for IRQ%d, assuming level low\n", irq
);
893 pr_warn("WARNING: Please fix your firmware\n");
894 flags
= IRQF_TRIGGER_LOW
;
900 static int arch_timer_starting_cpu(unsigned int cpu
)
902 struct clock_event_device
*clk
= this_cpu_ptr(arch_timer_evt
);
905 __arch_timer_setup(ARCH_TIMER_TYPE_CP15
, clk
);
907 flags
= check_ppi_trigger(arch_timer_ppi
[arch_timer_uses_ppi
]);
908 enable_percpu_irq(arch_timer_ppi
[arch_timer_uses_ppi
], flags
);
910 if (arch_timer_has_nonsecure_ppi()) {
911 flags
= check_ppi_trigger(arch_timer_ppi
[ARCH_TIMER_PHYS_NONSECURE_PPI
]);
912 enable_percpu_irq(arch_timer_ppi
[ARCH_TIMER_PHYS_NONSECURE_PPI
],
916 arch_counter_set_user_access();
918 arch_timer_configure_evtstream();
924 * For historical reasons, when probing with DT we use whichever (non-zero)
925 * rate was probed first, and don't verify that others match. If the first node
926 * probed has a clock-frequency property, this overrides the HW register.
928 static void arch_timer_of_configure_rate(u32 rate
, struct device_node
*np
)
930 /* Who has more than one independent system counter? */
934 if (of_property_read_u32(np
, "clock-frequency", &arch_timer_rate
))
935 arch_timer_rate
= rate
;
937 /* Check the timer frequency. */
938 if (arch_timer_rate
== 0)
939 pr_warn("frequency not available\n");
942 static void arch_timer_banner(unsigned type
)
944 pr_info("%s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
945 type
& ARCH_TIMER_TYPE_CP15
? "cp15" : "",
946 type
== (ARCH_TIMER_TYPE_CP15
| ARCH_TIMER_TYPE_MEM
) ?
948 type
& ARCH_TIMER_TYPE_MEM
? "mmio" : "",
949 (unsigned long)arch_timer_rate
/ 1000000,
950 (unsigned long)(arch_timer_rate
/ 10000) % 100,
951 type
& ARCH_TIMER_TYPE_CP15
?
952 (arch_timer_uses_ppi
== ARCH_TIMER_VIRT_PPI
) ? "virt" : "phys" :
954 type
== (ARCH_TIMER_TYPE_CP15
| ARCH_TIMER_TYPE_MEM
) ? "/" : "",
955 type
& ARCH_TIMER_TYPE_MEM
?
956 arch_timer_mem_use_virtual
? "virt" : "phys" :
960 u32
arch_timer_get_rate(void)
962 return arch_timer_rate
;
965 bool arch_timer_evtstrm_available(void)
968 * We might get called from a preemptible context. This is fine
969 * because availability of the event stream should be always the same
970 * for a preemptible context and context where we might resume a task.
972 return cpumask_test_cpu(raw_smp_processor_id(), &evtstrm_available
);
975 static u64
arch_counter_get_cntvct_mem(void)
977 u32 vct_lo
, vct_hi
, tmp_hi
;
980 vct_hi
= readl_relaxed(arch_counter_base
+ CNTVCT_HI
);
981 vct_lo
= readl_relaxed(arch_counter_base
+ CNTVCT_LO
);
982 tmp_hi
= readl_relaxed(arch_counter_base
+ CNTVCT_HI
);
983 } while (vct_hi
!= tmp_hi
);
985 return ((u64
) vct_hi
<< 32) | vct_lo
;
988 static struct arch_timer_kvm_info arch_timer_kvm_info
;
990 struct arch_timer_kvm_info
*arch_timer_get_kvm_info(void)
992 return &arch_timer_kvm_info
;
995 static void __init
arch_counter_register(unsigned type
)
999 /* Register the CP15 based counter if we have one */
1000 if (type
& ARCH_TIMER_TYPE_CP15
) {
1001 if ((IS_ENABLED(CONFIG_ARM64
) && !is_hyp_mode_available()) ||
1002 arch_timer_uses_ppi
== ARCH_TIMER_VIRT_PPI
)
1003 arch_timer_read_counter
= arch_counter_get_cntvct
;
1005 arch_timer_read_counter
= arch_counter_get_cntpct
;
1007 clocksource_counter
.archdata
.vdso_direct
= vdso_default
;
1009 arch_timer_read_counter
= arch_counter_get_cntvct_mem
;
1012 if (!arch_counter_suspend_stop
)
1013 clocksource_counter
.flags
|= CLOCK_SOURCE_SUSPEND_NONSTOP
;
1014 start_count
= arch_timer_read_counter();
1015 clocksource_register_hz(&clocksource_counter
, arch_timer_rate
);
1016 cyclecounter
.mult
= clocksource_counter
.mult
;
1017 cyclecounter
.shift
= clocksource_counter
.shift
;
1018 timecounter_init(&arch_timer_kvm_info
.timecounter
,
1019 &cyclecounter
, start_count
);
1021 /* 56 bits minimum, so we assume worst case rollover */
1022 sched_clock_register(arch_timer_read_counter
, 56, arch_timer_rate
);
1025 static void arch_timer_stop(struct clock_event_device
*clk
)
1027 pr_debug("disable IRQ%d cpu #%d\n", clk
->irq
, smp_processor_id());
1029 disable_percpu_irq(arch_timer_ppi
[arch_timer_uses_ppi
]);
1030 if (arch_timer_has_nonsecure_ppi())
1031 disable_percpu_irq(arch_timer_ppi
[ARCH_TIMER_PHYS_NONSECURE_PPI
]);
1033 clk
->set_state_shutdown(clk
);
1036 static int arch_timer_dying_cpu(unsigned int cpu
)
1038 struct clock_event_device
*clk
= this_cpu_ptr(arch_timer_evt
);
1040 cpumask_clear_cpu(smp_processor_id(), &evtstrm_available
);
1042 arch_timer_stop(clk
);
1046 #ifdef CONFIG_CPU_PM
1047 static DEFINE_PER_CPU(unsigned long, saved_cntkctl
);
1048 static int arch_timer_cpu_pm_notify(struct notifier_block
*self
,
1049 unsigned long action
, void *hcpu
)
1051 if (action
== CPU_PM_ENTER
) {
1052 __this_cpu_write(saved_cntkctl
, arch_timer_get_cntkctl());
1054 cpumask_clear_cpu(smp_processor_id(), &evtstrm_available
);
1055 } else if (action
== CPU_PM_ENTER_FAILED
|| action
== CPU_PM_EXIT
) {
1056 arch_timer_set_cntkctl(__this_cpu_read(saved_cntkctl
));
1058 if (elf_hwcap
& HWCAP_EVTSTRM
)
1059 cpumask_set_cpu(smp_processor_id(), &evtstrm_available
);
1064 static struct notifier_block arch_timer_cpu_pm_notifier
= {
1065 .notifier_call
= arch_timer_cpu_pm_notify
,
1068 static int __init
arch_timer_cpu_pm_init(void)
1070 return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier
);
1073 static void __init
arch_timer_cpu_pm_deinit(void)
1075 WARN_ON(cpu_pm_unregister_notifier(&arch_timer_cpu_pm_notifier
));
1079 static int __init
arch_timer_cpu_pm_init(void)
1084 static void __init
arch_timer_cpu_pm_deinit(void)
1089 static int __init
arch_timer_register(void)
1094 arch_timer_evt
= alloc_percpu(struct clock_event_device
);
1095 if (!arch_timer_evt
) {
1100 ppi
= arch_timer_ppi
[arch_timer_uses_ppi
];
1101 switch (arch_timer_uses_ppi
) {
1102 case ARCH_TIMER_VIRT_PPI
:
1103 err
= request_percpu_irq(ppi
, arch_timer_handler_virt
,
1104 "arch_timer", arch_timer_evt
);
1106 case ARCH_TIMER_PHYS_SECURE_PPI
:
1107 case ARCH_TIMER_PHYS_NONSECURE_PPI
:
1108 err
= request_percpu_irq(ppi
, arch_timer_handler_phys
,
1109 "arch_timer", arch_timer_evt
);
1110 if (!err
&& arch_timer_has_nonsecure_ppi()) {
1111 ppi
= arch_timer_ppi
[ARCH_TIMER_PHYS_NONSECURE_PPI
];
1112 err
= request_percpu_irq(ppi
, arch_timer_handler_phys
,
1113 "arch_timer", arch_timer_evt
);
1115 free_percpu_irq(arch_timer_ppi
[ARCH_TIMER_PHYS_SECURE_PPI
],
1119 case ARCH_TIMER_HYP_PPI
:
1120 err
= request_percpu_irq(ppi
, arch_timer_handler_phys
,
1121 "arch_timer", arch_timer_evt
);
1128 pr_err("can't register interrupt %d (%d)\n", ppi
, err
);
1132 err
= arch_timer_cpu_pm_init();
1134 goto out_unreg_notify
;
1136 /* Register and immediately configure the timer on the boot CPU */
1137 err
= cpuhp_setup_state(CPUHP_AP_ARM_ARCH_TIMER_STARTING
,
1138 "clockevents/arm/arch_timer:starting",
1139 arch_timer_starting_cpu
, arch_timer_dying_cpu
);
1141 goto out_unreg_cpupm
;
1145 arch_timer_cpu_pm_deinit();
1148 free_percpu_irq(arch_timer_ppi
[arch_timer_uses_ppi
], arch_timer_evt
);
1149 if (arch_timer_has_nonsecure_ppi())
1150 free_percpu_irq(arch_timer_ppi
[ARCH_TIMER_PHYS_NONSECURE_PPI
],
1154 free_percpu(arch_timer_evt
);
1159 static int __init
arch_timer_mem_register(void __iomem
*base
, unsigned int irq
)
1163 struct arch_timer
*t
;
1165 t
= kzalloc(sizeof(*t
), GFP_KERNEL
);
1171 __arch_timer_setup(ARCH_TIMER_TYPE_MEM
, &t
->evt
);
1173 if (arch_timer_mem_use_virtual
)
1174 func
= arch_timer_handler_virt_mem
;
1176 func
= arch_timer_handler_phys_mem
;
1178 ret
= request_irq(irq
, func
, IRQF_TIMER
, "arch_mem_timer", &t
->evt
);
1180 pr_err("Failed to request mem timer irq\n");
1187 static const struct of_device_id arch_timer_of_match
[] __initconst
= {
1188 { .compatible
= "arm,armv7-timer", },
1189 { .compatible
= "arm,armv8-timer", },
1193 static const struct of_device_id arch_timer_mem_of_match
[] __initconst
= {
1194 { .compatible
= "arm,armv7-timer-mem", },
1198 static bool __init
arch_timer_needs_of_probing(void)
1200 struct device_node
*dn
;
1201 bool needs_probing
= false;
1202 unsigned int mask
= ARCH_TIMER_TYPE_CP15
| ARCH_TIMER_TYPE_MEM
;
1204 /* We have two timers, and both device-tree nodes are probed. */
1205 if ((arch_timers_present
& mask
) == mask
)
1209 * Only one type of timer is probed,
1210 * check if we have another type of timer node in device-tree.
1212 if (arch_timers_present
& ARCH_TIMER_TYPE_CP15
)
1213 dn
= of_find_matching_node(NULL
, arch_timer_mem_of_match
);
1215 dn
= of_find_matching_node(NULL
, arch_timer_of_match
);
1217 if (dn
&& of_device_is_available(dn
))
1218 needs_probing
= true;
1222 return needs_probing
;
1225 static int __init
arch_timer_common_init(void)
1227 arch_timer_banner(arch_timers_present
);
1228 arch_counter_register(arch_timers_present
);
1229 return arch_timer_arch_init();
1233 * arch_timer_select_ppi() - Select suitable PPI for the current system.
1235 * If HYP mode is available, we know that the physical timer
1236 * has been configured to be accessible from PL1. Use it, so
1237 * that a guest can use the virtual timer instead.
1239 * On ARMv8.1 with VH extensions, the kernel runs in HYP. VHE
1240 * accesses to CNTP_*_EL1 registers are silently redirected to
1241 * their CNTHP_*_EL2 counterparts, and use a different PPI
1244 * If no interrupt provided for virtual timer, we'll have to
1245 * stick to the physical timer. It'd better be accessible...
1246 * For arm64 we never use the secure interrupt.
1248 * Return: a suitable PPI type for the current system.
1250 static enum arch_timer_ppi_nr __init
arch_timer_select_ppi(void)
1252 if (is_kernel_in_hyp_mode())
1253 return ARCH_TIMER_HYP_PPI
;
1255 if (!is_hyp_mode_available() && arch_timer_ppi
[ARCH_TIMER_VIRT_PPI
])
1256 return ARCH_TIMER_VIRT_PPI
;
1258 if (IS_ENABLED(CONFIG_ARM64
))
1259 return ARCH_TIMER_PHYS_NONSECURE_PPI
;
1261 return ARCH_TIMER_PHYS_SECURE_PPI
;
1264 static void __init
arch_timer_populate_kvm_info(void)
1266 arch_timer_kvm_info
.virtual_irq
= arch_timer_ppi
[ARCH_TIMER_VIRT_PPI
];
1267 if (is_kernel_in_hyp_mode())
1268 arch_timer_kvm_info
.physical_irq
= arch_timer_ppi
[ARCH_TIMER_PHYS_NONSECURE_PPI
];
1271 static int __init
arch_timer_of_init(struct device_node
*np
)
1276 if (arch_timers_present
& ARCH_TIMER_TYPE_CP15
) {
1277 pr_warn("multiple nodes in dt, skipping\n");
1281 arch_timers_present
|= ARCH_TIMER_TYPE_CP15
;
1282 for (i
= ARCH_TIMER_PHYS_SECURE_PPI
; i
< ARCH_TIMER_MAX_TIMER_PPI
; i
++)
1283 arch_timer_ppi
[i
] = irq_of_parse_and_map(np
, i
);
1285 arch_timer_populate_kvm_info();
1287 rate
= arch_timer_get_cntfrq();
1288 arch_timer_of_configure_rate(rate
, np
);
1290 arch_timer_c3stop
= !of_property_read_bool(np
, "always-on");
1292 /* Check for globally applicable workarounds */
1293 arch_timer_check_ool_workaround(ate_match_dt
, np
);
1296 * If we cannot rely on firmware initializing the timer registers then
1297 * we should use the physical timers instead.
1299 if (IS_ENABLED(CONFIG_ARM
) &&
1300 of_property_read_bool(np
, "arm,cpu-registers-not-fw-configured"))
1301 arch_timer_uses_ppi
= ARCH_TIMER_PHYS_SECURE_PPI
;
1303 arch_timer_uses_ppi
= arch_timer_select_ppi();
1305 if (!arch_timer_ppi
[arch_timer_uses_ppi
]) {
1306 pr_err("No interrupt available, giving up\n");
1310 /* On some systems, the counter stops ticking when in suspend. */
1311 arch_counter_suspend_stop
= of_property_read_bool(np
,
1312 "arm,no-tick-in-suspend");
1314 ret
= arch_timer_register();
1318 if (arch_timer_needs_of_probing())
1321 return arch_timer_common_init();
1323 TIMER_OF_DECLARE(armv7_arch_timer
, "arm,armv7-timer", arch_timer_of_init
);
1324 TIMER_OF_DECLARE(armv8_arch_timer
, "arm,armv8-timer", arch_timer_of_init
);
1327 arch_timer_mem_frame_get_cntfrq(struct arch_timer_mem_frame
*frame
)
1332 base
= ioremap(frame
->cntbase
, frame
->size
);
1334 pr_err("Unable to map frame @ %pa\n", &frame
->cntbase
);
1338 rate
= readl_relaxed(base
+ CNTFRQ
);
1345 static struct arch_timer_mem_frame
* __init
1346 arch_timer_mem_find_best_frame(struct arch_timer_mem
*timer_mem
)
1348 struct arch_timer_mem_frame
*frame
, *best_frame
= NULL
;
1349 void __iomem
*cntctlbase
;
1353 cntctlbase
= ioremap(timer_mem
->cntctlbase
, timer_mem
->size
);
1355 pr_err("Can't map CNTCTLBase @ %pa\n",
1356 &timer_mem
->cntctlbase
);
1360 cnttidr
= readl_relaxed(cntctlbase
+ CNTTIDR
);
1363 * Try to find a virtual capable frame. Otherwise fall back to a
1364 * physical capable frame.
1366 for (i
= 0; i
< ARCH_TIMER_MEM_MAX_FRAMES
; i
++) {
1367 u32 cntacr
= CNTACR_RFRQ
| CNTACR_RWPT
| CNTACR_RPCT
|
1368 CNTACR_RWVT
| CNTACR_RVOFF
| CNTACR_RVCT
;
1370 frame
= &timer_mem
->frame
[i
];
1374 /* Try enabling everything, and see what sticks */
1375 writel_relaxed(cntacr
, cntctlbase
+ CNTACR(i
));
1376 cntacr
= readl_relaxed(cntctlbase
+ CNTACR(i
));
1378 if ((cnttidr
& CNTTIDR_VIRT(i
)) &&
1379 !(~cntacr
& (CNTACR_RWVT
| CNTACR_RVCT
))) {
1381 arch_timer_mem_use_virtual
= true;
1385 if (~cntacr
& (CNTACR_RWPT
| CNTACR_RPCT
))
1391 iounmap(cntctlbase
);
1397 arch_timer_mem_frame_register(struct arch_timer_mem_frame
*frame
)
1402 if (arch_timer_mem_use_virtual
)
1403 irq
= frame
->virt_irq
;
1405 irq
= frame
->phys_irq
;
1408 pr_err("Frame missing %s irq.\n",
1409 arch_timer_mem_use_virtual
? "virt" : "phys");
1413 if (!request_mem_region(frame
->cntbase
, frame
->size
,
1417 base
= ioremap(frame
->cntbase
, frame
->size
);
1419 pr_err("Can't map frame's registers\n");
1423 ret
= arch_timer_mem_register(base
, irq
);
1429 arch_counter_base
= base
;
1430 arch_timers_present
|= ARCH_TIMER_TYPE_MEM
;
1435 static int __init
arch_timer_mem_of_init(struct device_node
*np
)
1437 struct arch_timer_mem
*timer_mem
;
1438 struct arch_timer_mem_frame
*frame
;
1439 struct device_node
*frame_node
;
1440 struct resource res
;
1444 timer_mem
= kzalloc(sizeof(*timer_mem
), GFP_KERNEL
);
1448 if (of_address_to_resource(np
, 0, &res
))
1450 timer_mem
->cntctlbase
= res
.start
;
1451 timer_mem
->size
= resource_size(&res
);
1453 for_each_available_child_of_node(np
, frame_node
) {
1455 struct arch_timer_mem_frame
*frame
;
1457 if (of_property_read_u32(frame_node
, "frame-number", &n
)) {
1458 pr_err(FW_BUG
"Missing frame-number.\n");
1459 of_node_put(frame_node
);
1462 if (n
>= ARCH_TIMER_MEM_MAX_FRAMES
) {
1463 pr_err(FW_BUG
"Wrong frame-number, only 0-%u are permitted.\n",
1464 ARCH_TIMER_MEM_MAX_FRAMES
- 1);
1465 of_node_put(frame_node
);
1468 frame
= &timer_mem
->frame
[n
];
1471 pr_err(FW_BUG
"Duplicated frame-number.\n");
1472 of_node_put(frame_node
);
1476 if (of_address_to_resource(frame_node
, 0, &res
)) {
1477 of_node_put(frame_node
);
1480 frame
->cntbase
= res
.start
;
1481 frame
->size
= resource_size(&res
);
1483 frame
->virt_irq
= irq_of_parse_and_map(frame_node
,
1484 ARCH_TIMER_VIRT_SPI
);
1485 frame
->phys_irq
= irq_of_parse_and_map(frame_node
,
1486 ARCH_TIMER_PHYS_SPI
);
1488 frame
->valid
= true;
1491 frame
= arch_timer_mem_find_best_frame(timer_mem
);
1493 pr_err("Unable to find a suitable frame in timer @ %pa\n",
1494 &timer_mem
->cntctlbase
);
1499 rate
= arch_timer_mem_frame_get_cntfrq(frame
);
1500 arch_timer_of_configure_rate(rate
, np
);
1502 ret
= arch_timer_mem_frame_register(frame
);
1503 if (!ret
&& !arch_timer_needs_of_probing())
1504 ret
= arch_timer_common_init();
1509 TIMER_OF_DECLARE(armv7_arch_timer_mem
, "arm,armv7-timer-mem",
1510 arch_timer_mem_of_init
);
1512 #ifdef CONFIG_ACPI_GTDT
1514 arch_timer_mem_verify_cntfrq(struct arch_timer_mem
*timer_mem
)
1516 struct arch_timer_mem_frame
*frame
;
1520 for (i
= 0; i
< ARCH_TIMER_MEM_MAX_FRAMES
; i
++) {
1521 frame
= &timer_mem
->frame
[i
];
1526 rate
= arch_timer_mem_frame_get_cntfrq(frame
);
1527 if (rate
== arch_timer_rate
)
1530 pr_err(FW_BUG
"CNTFRQ mismatch: frame @ %pa: (0x%08lx), CPU: (0x%08lx)\n",
1532 (unsigned long)rate
, (unsigned long)arch_timer_rate
);
1540 static int __init
arch_timer_mem_acpi_init(int platform_timer_count
)
1542 struct arch_timer_mem
*timers
, *timer
;
1543 struct arch_timer_mem_frame
*frame
, *best_frame
= NULL
;
1544 int timer_count
, i
, ret
= 0;
1546 timers
= kcalloc(platform_timer_count
, sizeof(*timers
),
1551 ret
= acpi_arch_timer_mem_init(timers
, &timer_count
);
1552 if (ret
|| !timer_count
)
1556 * While unlikely, it's theoretically possible that none of the frames
1557 * in a timer expose the combination of feature we want.
1559 for (i
= 0; i
< timer_count
; i
++) {
1562 frame
= arch_timer_mem_find_best_frame(timer
);
1566 ret
= arch_timer_mem_verify_cntfrq(timer
);
1568 pr_err("Disabling MMIO timers due to CNTFRQ mismatch\n");
1572 if (!best_frame
) /* implies !frame */
1574 * Only complain about missing suitable frames if we
1575 * haven't already found one in a previous iteration.
1577 pr_err("Unable to find a suitable frame in timer @ %pa\n",
1578 &timer
->cntctlbase
);
1582 ret
= arch_timer_mem_frame_register(best_frame
);
1588 /* Initialize per-processor generic timer and memory-mapped timer(if present) */
1589 static int __init
arch_timer_acpi_init(struct acpi_table_header
*table
)
1591 int ret
, platform_timer_count
;
1593 if (arch_timers_present
& ARCH_TIMER_TYPE_CP15
) {
1594 pr_warn("already initialized, skipping\n");
1598 arch_timers_present
|= ARCH_TIMER_TYPE_CP15
;
1600 ret
= acpi_gtdt_init(table
, &platform_timer_count
);
1602 pr_err("Failed to init GTDT table.\n");
1606 arch_timer_ppi
[ARCH_TIMER_PHYS_NONSECURE_PPI
] =
1607 acpi_gtdt_map_ppi(ARCH_TIMER_PHYS_NONSECURE_PPI
);
1609 arch_timer_ppi
[ARCH_TIMER_VIRT_PPI
] =
1610 acpi_gtdt_map_ppi(ARCH_TIMER_VIRT_PPI
);
1612 arch_timer_ppi
[ARCH_TIMER_HYP_PPI
] =
1613 acpi_gtdt_map_ppi(ARCH_TIMER_HYP_PPI
);
1615 arch_timer_populate_kvm_info();
1618 * When probing via ACPI, we have no mechanism to override the sysreg
1619 * CNTFRQ value. This *must* be correct.
1621 arch_timer_rate
= arch_timer_get_cntfrq();
1622 if (!arch_timer_rate
) {
1623 pr_err(FW_BUG
"frequency not available.\n");
1627 arch_timer_uses_ppi
= arch_timer_select_ppi();
1628 if (!arch_timer_ppi
[arch_timer_uses_ppi
]) {
1629 pr_err("No interrupt available, giving up\n");
1633 /* Always-on capability */
1634 arch_timer_c3stop
= acpi_gtdt_c3stop(arch_timer_uses_ppi
);
1636 /* Check for globally applicable workarounds */
1637 arch_timer_check_ool_workaround(ate_match_acpi_oem_info
, table
);
1639 ret
= arch_timer_register();
1643 if (platform_timer_count
&&
1644 arch_timer_mem_acpi_init(platform_timer_count
))
1645 pr_err("Failed to initialize memory-mapped timer.\n");
1647 return arch_timer_common_init();
1649 TIMER_ACPI_DECLARE(arch_timer
, ACPI_SIG_GTDT
, arch_timer_acpi_init
);