18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  linux/drivers/clocksource/arm_arch_timer.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) 2011 ARM Ltd.
68c2ecf20Sopenharmony_ci *  All Rights Reserved
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#define pr_fmt(fmt) 	"arch_timer: " fmt
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/init.h>
128c2ecf20Sopenharmony_ci#include <linux/kernel.h>
138c2ecf20Sopenharmony_ci#include <linux/device.h>
148c2ecf20Sopenharmony_ci#include <linux/smp.h>
158c2ecf20Sopenharmony_ci#include <linux/cpu.h>
168c2ecf20Sopenharmony_ci#include <linux/cpu_pm.h>
178c2ecf20Sopenharmony_ci#include <linux/clockchips.h>
188c2ecf20Sopenharmony_ci#include <linux/clocksource.h>
198c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
208c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
218c2ecf20Sopenharmony_ci#include <linux/of_address.h>
228c2ecf20Sopenharmony_ci#include <linux/io.h>
238c2ecf20Sopenharmony_ci#include <linux/slab.h>
248c2ecf20Sopenharmony_ci#include <linux/sched/clock.h>
258c2ecf20Sopenharmony_ci#include <linux/sched_clock.h>
268c2ecf20Sopenharmony_ci#include <linux/acpi.h>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#include <asm/arch_timer.h>
298c2ecf20Sopenharmony_ci#include <asm/virt.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include <clocksource/arm_arch_timer.h>
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#define CNTTIDR		0x08
348c2ecf20Sopenharmony_ci#define CNTTIDR_VIRT(n)	(BIT(1) << ((n) * 4))
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define CNTACR(n)	(0x40 + ((n) * 4))
378c2ecf20Sopenharmony_ci#define CNTACR_RPCT	BIT(0)
388c2ecf20Sopenharmony_ci#define CNTACR_RVCT	BIT(1)
398c2ecf20Sopenharmony_ci#define CNTACR_RFRQ	BIT(2)
408c2ecf20Sopenharmony_ci#define CNTACR_RVOFF	BIT(3)
418c2ecf20Sopenharmony_ci#define CNTACR_RWVT	BIT(4)
428c2ecf20Sopenharmony_ci#define CNTACR_RWPT	BIT(5)
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define CNTVCT_LO	0x08
458c2ecf20Sopenharmony_ci#define CNTVCT_HI	0x0c
468c2ecf20Sopenharmony_ci#define CNTFRQ		0x10
478c2ecf20Sopenharmony_ci#define CNTP_TVAL	0x28
488c2ecf20Sopenharmony_ci#define CNTP_CTL	0x2c
498c2ecf20Sopenharmony_ci#define CNTV_TVAL	0x38
508c2ecf20Sopenharmony_ci#define CNTV_CTL	0x3c
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic unsigned arch_timers_present __initdata;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic void __iomem *arch_counter_base;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistruct arch_timer {
578c2ecf20Sopenharmony_ci	void __iomem *base;
588c2ecf20Sopenharmony_ci	struct clock_event_device evt;
598c2ecf20Sopenharmony_ci};
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#define to_arch_timer(e) container_of(e, struct arch_timer, evt)
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic u32 arch_timer_rate;
648c2ecf20Sopenharmony_cistatic int arch_timer_ppi[ARCH_TIMER_MAX_TIMER_PPI];
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistatic struct clock_event_device __percpu *arch_timer_evt;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic enum arch_timer_ppi_nr arch_timer_uses_ppi = ARCH_TIMER_VIRT_PPI;
698c2ecf20Sopenharmony_cistatic bool arch_timer_c3stop;
708c2ecf20Sopenharmony_cistatic bool arch_timer_mem_use_virtual;
718c2ecf20Sopenharmony_cistatic bool arch_counter_suspend_stop;
728c2ecf20Sopenharmony_ci#ifdef CONFIG_GENERIC_GETTIMEOFDAY
738c2ecf20Sopenharmony_cistatic enum vdso_clock_mode vdso_default = VDSO_CLOCKMODE_ARCHTIMER;
748c2ecf20Sopenharmony_ci#else
758c2ecf20Sopenharmony_cistatic enum vdso_clock_mode vdso_default = VDSO_CLOCKMODE_NONE;
768c2ecf20Sopenharmony_ci#endif /* CONFIG_GENERIC_GETTIMEOFDAY */
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic cpumask_t evtstrm_available = CPU_MASK_NONE;
798c2ecf20Sopenharmony_cistatic bool evtstrm_enable = IS_ENABLED(CONFIG_ARM_ARCH_TIMER_EVTSTREAM);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistatic int __init early_evtstrm_cfg(char *buf)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	return strtobool(buf, &evtstrm_enable);
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ciearly_param("clocksource.arm_arch_timer.evtstrm", early_evtstrm_cfg);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci/*
888c2ecf20Sopenharmony_ci * Architected system timer support.
898c2ecf20Sopenharmony_ci */
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic __always_inline
928c2ecf20Sopenharmony_civoid arch_timer_reg_write(int access, enum arch_timer_reg reg, u32 val,
938c2ecf20Sopenharmony_ci			  struct clock_event_device *clk)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
968c2ecf20Sopenharmony_ci		struct arch_timer *timer = to_arch_timer(clk);
978c2ecf20Sopenharmony_ci		switch (reg) {
988c2ecf20Sopenharmony_ci		case ARCH_TIMER_REG_CTRL:
998c2ecf20Sopenharmony_ci			writel_relaxed(val, timer->base + CNTP_CTL);
1008c2ecf20Sopenharmony_ci			break;
1018c2ecf20Sopenharmony_ci		case ARCH_TIMER_REG_TVAL:
1028c2ecf20Sopenharmony_ci			writel_relaxed(val, timer->base + CNTP_TVAL);
1038c2ecf20Sopenharmony_ci			break;
1048c2ecf20Sopenharmony_ci		}
1058c2ecf20Sopenharmony_ci	} else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
1068c2ecf20Sopenharmony_ci		struct arch_timer *timer = to_arch_timer(clk);
1078c2ecf20Sopenharmony_ci		switch (reg) {
1088c2ecf20Sopenharmony_ci		case ARCH_TIMER_REG_CTRL:
1098c2ecf20Sopenharmony_ci			writel_relaxed(val, timer->base + CNTV_CTL);
1108c2ecf20Sopenharmony_ci			break;
1118c2ecf20Sopenharmony_ci		case ARCH_TIMER_REG_TVAL:
1128c2ecf20Sopenharmony_ci			writel_relaxed(val, timer->base + CNTV_TVAL);
1138c2ecf20Sopenharmony_ci			break;
1148c2ecf20Sopenharmony_ci		}
1158c2ecf20Sopenharmony_ci	} else {
1168c2ecf20Sopenharmony_ci		arch_timer_reg_write_cp15(access, reg, val);
1178c2ecf20Sopenharmony_ci	}
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic __always_inline
1218c2ecf20Sopenharmony_ciu32 arch_timer_reg_read(int access, enum arch_timer_reg reg,
1228c2ecf20Sopenharmony_ci			struct clock_event_device *clk)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	u32 val;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
1278c2ecf20Sopenharmony_ci		struct arch_timer *timer = to_arch_timer(clk);
1288c2ecf20Sopenharmony_ci		switch (reg) {
1298c2ecf20Sopenharmony_ci		case ARCH_TIMER_REG_CTRL:
1308c2ecf20Sopenharmony_ci			val = readl_relaxed(timer->base + CNTP_CTL);
1318c2ecf20Sopenharmony_ci			break;
1328c2ecf20Sopenharmony_ci		case ARCH_TIMER_REG_TVAL:
1338c2ecf20Sopenharmony_ci			val = readl_relaxed(timer->base + CNTP_TVAL);
1348c2ecf20Sopenharmony_ci			break;
1358c2ecf20Sopenharmony_ci		}
1368c2ecf20Sopenharmony_ci	} else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
1378c2ecf20Sopenharmony_ci		struct arch_timer *timer = to_arch_timer(clk);
1388c2ecf20Sopenharmony_ci		switch (reg) {
1398c2ecf20Sopenharmony_ci		case ARCH_TIMER_REG_CTRL:
1408c2ecf20Sopenharmony_ci			val = readl_relaxed(timer->base + CNTV_CTL);
1418c2ecf20Sopenharmony_ci			break;
1428c2ecf20Sopenharmony_ci		case ARCH_TIMER_REG_TVAL:
1438c2ecf20Sopenharmony_ci			val = readl_relaxed(timer->base + CNTV_TVAL);
1448c2ecf20Sopenharmony_ci			break;
1458c2ecf20Sopenharmony_ci		}
1468c2ecf20Sopenharmony_ci	} else {
1478c2ecf20Sopenharmony_ci		val = arch_timer_reg_read_cp15(access, reg);
1488c2ecf20Sopenharmony_ci	}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	return val;
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistatic notrace u64 arch_counter_get_cntpct_stable(void)
1548c2ecf20Sopenharmony_ci{
1558c2ecf20Sopenharmony_ci	return __arch_counter_get_cntpct_stable();
1568c2ecf20Sopenharmony_ci}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistatic notrace u64 arch_counter_get_cntpct(void)
1598c2ecf20Sopenharmony_ci{
1608c2ecf20Sopenharmony_ci	return __arch_counter_get_cntpct();
1618c2ecf20Sopenharmony_ci}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_cistatic notrace u64 arch_counter_get_cntvct_stable(void)
1648c2ecf20Sopenharmony_ci{
1658c2ecf20Sopenharmony_ci	return __arch_counter_get_cntvct_stable();
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_cistatic notrace u64 arch_counter_get_cntvct(void)
1698c2ecf20Sopenharmony_ci{
1708c2ecf20Sopenharmony_ci	return __arch_counter_get_cntvct();
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci/*
1748c2ecf20Sopenharmony_ci * Default to cp15 based access because arm64 uses this function for
1758c2ecf20Sopenharmony_ci * sched_clock() before DT is probed and the cp15 method is guaranteed
1768c2ecf20Sopenharmony_ci * to exist on arm64. arm doesn't use this before DT is probed so even
1778c2ecf20Sopenharmony_ci * if we don't have the cp15 accessors we won't have a problem.
1788c2ecf20Sopenharmony_ci */
1798c2ecf20Sopenharmony_ciu64 (*arch_timer_read_counter)(void) = arch_counter_get_cntvct;
1808c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(arch_timer_read_counter);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_cistatic u64 arch_counter_read(struct clocksource *cs)
1838c2ecf20Sopenharmony_ci{
1848c2ecf20Sopenharmony_ci	return arch_timer_read_counter();
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic u64 arch_counter_read_cc(const struct cyclecounter *cc)
1888c2ecf20Sopenharmony_ci{
1898c2ecf20Sopenharmony_ci	return arch_timer_read_counter();
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistatic struct clocksource clocksource_counter = {
1938c2ecf20Sopenharmony_ci	.name	= "arch_sys_counter",
1948c2ecf20Sopenharmony_ci	.rating	= 400,
1958c2ecf20Sopenharmony_ci	.read	= arch_counter_read,
1968c2ecf20Sopenharmony_ci	.mask	= CLOCKSOURCE_MASK(56),
1978c2ecf20Sopenharmony_ci	.flags	= CLOCK_SOURCE_IS_CONTINUOUS,
1988c2ecf20Sopenharmony_ci};
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistatic struct cyclecounter cyclecounter __ro_after_init = {
2018c2ecf20Sopenharmony_ci	.read	= arch_counter_read_cc,
2028c2ecf20Sopenharmony_ci	.mask	= CLOCKSOURCE_MASK(56),
2038c2ecf20Sopenharmony_ci};
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_cistruct ate_acpi_oem_info {
2068c2ecf20Sopenharmony_ci	char oem_id[ACPI_OEM_ID_SIZE + 1];
2078c2ecf20Sopenharmony_ci	char oem_table_id[ACPI_OEM_TABLE_ID_SIZE + 1];
2088c2ecf20Sopenharmony_ci	u32 oem_revision;
2098c2ecf20Sopenharmony_ci};
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci#ifdef CONFIG_FSL_ERRATUM_A008585
2128c2ecf20Sopenharmony_ci/*
2138c2ecf20Sopenharmony_ci * The number of retries is an arbitrary value well beyond the highest number
2148c2ecf20Sopenharmony_ci * of iterations the loop has been observed to take.
2158c2ecf20Sopenharmony_ci */
2168c2ecf20Sopenharmony_ci#define __fsl_a008585_read_reg(reg) ({			\
2178c2ecf20Sopenharmony_ci	u64 _old, _new;					\
2188c2ecf20Sopenharmony_ci	int _retries = 200;				\
2198c2ecf20Sopenharmony_ci							\
2208c2ecf20Sopenharmony_ci	do {						\
2218c2ecf20Sopenharmony_ci		_old = read_sysreg(reg);		\
2228c2ecf20Sopenharmony_ci		_new = read_sysreg(reg);		\
2238c2ecf20Sopenharmony_ci		_retries--;				\
2248c2ecf20Sopenharmony_ci	} while (unlikely(_old != _new) && _retries);	\
2258c2ecf20Sopenharmony_ci							\
2268c2ecf20Sopenharmony_ci	WARN_ON_ONCE(!_retries);			\
2278c2ecf20Sopenharmony_ci	_new;						\
2288c2ecf20Sopenharmony_ci})
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_cistatic u32 notrace fsl_a008585_read_cntp_tval_el0(void)
2318c2ecf20Sopenharmony_ci{
2328c2ecf20Sopenharmony_ci	return __fsl_a008585_read_reg(cntp_tval_el0);
2338c2ecf20Sopenharmony_ci}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_cistatic u32 notrace fsl_a008585_read_cntv_tval_el0(void)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	return __fsl_a008585_read_reg(cntv_tval_el0);
2388c2ecf20Sopenharmony_ci}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_cistatic u64 notrace fsl_a008585_read_cntpct_el0(void)
2418c2ecf20Sopenharmony_ci{
2428c2ecf20Sopenharmony_ci	return __fsl_a008585_read_reg(cntpct_el0);
2438c2ecf20Sopenharmony_ci}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_cistatic u64 notrace fsl_a008585_read_cntvct_el0(void)
2468c2ecf20Sopenharmony_ci{
2478c2ecf20Sopenharmony_ci	return __fsl_a008585_read_reg(cntvct_el0);
2488c2ecf20Sopenharmony_ci}
2498c2ecf20Sopenharmony_ci#endif
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci#ifdef CONFIG_HISILICON_ERRATUM_161010101
2528c2ecf20Sopenharmony_ci/*
2538c2ecf20Sopenharmony_ci * Verify whether the value of the second read is larger than the first by
2548c2ecf20Sopenharmony_ci * less than 32 is the only way to confirm the value is correct, so clear the
2558c2ecf20Sopenharmony_ci * lower 5 bits to check whether the difference is greater than 32 or not.
2568c2ecf20Sopenharmony_ci * Theoretically the erratum should not occur more than twice in succession
2578c2ecf20Sopenharmony_ci * when reading the system counter, but it is possible that some interrupts
2588c2ecf20Sopenharmony_ci * may lead to more than twice read errors, triggering the warning, so setting
2598c2ecf20Sopenharmony_ci * the number of retries far beyond the number of iterations the loop has been
2608c2ecf20Sopenharmony_ci * observed to take.
2618c2ecf20Sopenharmony_ci */
2628c2ecf20Sopenharmony_ci#define __hisi_161010101_read_reg(reg) ({				\
2638c2ecf20Sopenharmony_ci	u64 _old, _new;						\
2648c2ecf20Sopenharmony_ci	int _retries = 50;					\
2658c2ecf20Sopenharmony_ci								\
2668c2ecf20Sopenharmony_ci	do {							\
2678c2ecf20Sopenharmony_ci		_old = read_sysreg(reg);			\
2688c2ecf20Sopenharmony_ci		_new = read_sysreg(reg);			\
2698c2ecf20Sopenharmony_ci		_retries--;					\
2708c2ecf20Sopenharmony_ci	} while (unlikely((_new - _old) >> 5) && _retries);	\
2718c2ecf20Sopenharmony_ci								\
2728c2ecf20Sopenharmony_ci	WARN_ON_ONCE(!_retries);				\
2738c2ecf20Sopenharmony_ci	_new;							\
2748c2ecf20Sopenharmony_ci})
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_cistatic u32 notrace hisi_161010101_read_cntp_tval_el0(void)
2778c2ecf20Sopenharmony_ci{
2788c2ecf20Sopenharmony_ci	return __hisi_161010101_read_reg(cntp_tval_el0);
2798c2ecf20Sopenharmony_ci}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_cistatic u32 notrace hisi_161010101_read_cntv_tval_el0(void)
2828c2ecf20Sopenharmony_ci{
2838c2ecf20Sopenharmony_ci	return __hisi_161010101_read_reg(cntv_tval_el0);
2848c2ecf20Sopenharmony_ci}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_cistatic u64 notrace hisi_161010101_read_cntpct_el0(void)
2878c2ecf20Sopenharmony_ci{
2888c2ecf20Sopenharmony_ci	return __hisi_161010101_read_reg(cntpct_el0);
2898c2ecf20Sopenharmony_ci}
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_cistatic u64 notrace hisi_161010101_read_cntvct_el0(void)
2928c2ecf20Sopenharmony_ci{
2938c2ecf20Sopenharmony_ci	return __hisi_161010101_read_reg(cntvct_el0);
2948c2ecf20Sopenharmony_ci}
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_cistatic struct ate_acpi_oem_info hisi_161010101_oem_info[] = {
2978c2ecf20Sopenharmony_ci	/*
2988c2ecf20Sopenharmony_ci	 * Note that trailing spaces are required to properly match
2998c2ecf20Sopenharmony_ci	 * the OEM table information.
3008c2ecf20Sopenharmony_ci	 */
3018c2ecf20Sopenharmony_ci	{
3028c2ecf20Sopenharmony_ci		.oem_id		= "HISI  ",
3038c2ecf20Sopenharmony_ci		.oem_table_id	= "HIP05   ",
3048c2ecf20Sopenharmony_ci		.oem_revision	= 0,
3058c2ecf20Sopenharmony_ci	},
3068c2ecf20Sopenharmony_ci	{
3078c2ecf20Sopenharmony_ci		.oem_id		= "HISI  ",
3088c2ecf20Sopenharmony_ci		.oem_table_id	= "HIP06   ",
3098c2ecf20Sopenharmony_ci		.oem_revision	= 0,
3108c2ecf20Sopenharmony_ci	},
3118c2ecf20Sopenharmony_ci	{
3128c2ecf20Sopenharmony_ci		.oem_id		= "HISI  ",
3138c2ecf20Sopenharmony_ci		.oem_table_id	= "HIP07   ",
3148c2ecf20Sopenharmony_ci		.oem_revision	= 0,
3158c2ecf20Sopenharmony_ci	},
3168c2ecf20Sopenharmony_ci	{ /* Sentinel indicating the end of the OEM array */ },
3178c2ecf20Sopenharmony_ci};
3188c2ecf20Sopenharmony_ci#endif
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci#ifdef CONFIG_ARM64_ERRATUM_858921
3218c2ecf20Sopenharmony_cistatic u64 notrace arm64_858921_read_cntpct_el0(void)
3228c2ecf20Sopenharmony_ci{
3238c2ecf20Sopenharmony_ci	u64 old, new;
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	old = read_sysreg(cntpct_el0);
3268c2ecf20Sopenharmony_ci	new = read_sysreg(cntpct_el0);
3278c2ecf20Sopenharmony_ci	return (((old ^ new) >> 32) & 1) ? old : new;
3288c2ecf20Sopenharmony_ci}
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_cistatic u64 notrace arm64_858921_read_cntvct_el0(void)
3318c2ecf20Sopenharmony_ci{
3328c2ecf20Sopenharmony_ci	u64 old, new;
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	old = read_sysreg(cntvct_el0);
3358c2ecf20Sopenharmony_ci	new = read_sysreg(cntvct_el0);
3368c2ecf20Sopenharmony_ci	return (((old ^ new) >> 32) & 1) ? old : new;
3378c2ecf20Sopenharmony_ci}
3388c2ecf20Sopenharmony_ci#endif
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci#ifdef CONFIG_SUN50I_ERRATUM_UNKNOWN1
3418c2ecf20Sopenharmony_ci/*
3428c2ecf20Sopenharmony_ci * The low bits of the counter registers are indeterminate while bit 10 or
3438c2ecf20Sopenharmony_ci * greater is rolling over. Since the counter value can jump both backward
3448c2ecf20Sopenharmony_ci * (7ff -> 000 -> 800) and forward (7ff -> fff -> 800), ignore register values
3458c2ecf20Sopenharmony_ci * with all ones or all zeros in the low bits. Bound the loop by the maximum
3468c2ecf20Sopenharmony_ci * number of CPU cycles in 3 consecutive 24 MHz counter periods.
3478c2ecf20Sopenharmony_ci */
3488c2ecf20Sopenharmony_ci#define __sun50i_a64_read_reg(reg) ({					\
3498c2ecf20Sopenharmony_ci	u64 _val;							\
3508c2ecf20Sopenharmony_ci	int _retries = 150;						\
3518c2ecf20Sopenharmony_ci									\
3528c2ecf20Sopenharmony_ci	do {								\
3538c2ecf20Sopenharmony_ci		_val = read_sysreg(reg);				\
3548c2ecf20Sopenharmony_ci		_retries--;						\
3558c2ecf20Sopenharmony_ci	} while (((_val + 1) & GENMASK(8, 0)) <= 1 && _retries);	\
3568c2ecf20Sopenharmony_ci									\
3578c2ecf20Sopenharmony_ci	WARN_ON_ONCE(!_retries);					\
3588c2ecf20Sopenharmony_ci	_val;								\
3598c2ecf20Sopenharmony_ci})
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_cistatic u64 notrace sun50i_a64_read_cntpct_el0(void)
3628c2ecf20Sopenharmony_ci{
3638c2ecf20Sopenharmony_ci	return __sun50i_a64_read_reg(cntpct_el0);
3648c2ecf20Sopenharmony_ci}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_cistatic u64 notrace sun50i_a64_read_cntvct_el0(void)
3678c2ecf20Sopenharmony_ci{
3688c2ecf20Sopenharmony_ci	return __sun50i_a64_read_reg(cntvct_el0);
3698c2ecf20Sopenharmony_ci}
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_cistatic u32 notrace sun50i_a64_read_cntp_tval_el0(void)
3728c2ecf20Sopenharmony_ci{
3738c2ecf20Sopenharmony_ci	return read_sysreg(cntp_cval_el0) - sun50i_a64_read_cntpct_el0();
3748c2ecf20Sopenharmony_ci}
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_cistatic u32 notrace sun50i_a64_read_cntv_tval_el0(void)
3778c2ecf20Sopenharmony_ci{
3788c2ecf20Sopenharmony_ci	return read_sysreg(cntv_cval_el0) - sun50i_a64_read_cntvct_el0();
3798c2ecf20Sopenharmony_ci}
3808c2ecf20Sopenharmony_ci#endif
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci#ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
3838c2ecf20Sopenharmony_ciDEFINE_PER_CPU(const struct arch_timer_erratum_workaround *, timer_unstable_counter_workaround);
3848c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(timer_unstable_counter_workaround);
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_cistatic atomic_t timer_unstable_counter_workaround_in_use = ATOMIC_INIT(0);
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_cistatic void erratum_set_next_event_tval_generic(const int access, unsigned long evt,
3898c2ecf20Sopenharmony_ci						struct clock_event_device *clk)
3908c2ecf20Sopenharmony_ci{
3918c2ecf20Sopenharmony_ci	unsigned long ctrl;
3928c2ecf20Sopenharmony_ci	u64 cval;
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
3958c2ecf20Sopenharmony_ci	ctrl |= ARCH_TIMER_CTRL_ENABLE;
3968c2ecf20Sopenharmony_ci	ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	if (access == ARCH_TIMER_PHYS_ACCESS) {
3998c2ecf20Sopenharmony_ci		cval = evt + arch_counter_get_cntpct_stable();
4008c2ecf20Sopenharmony_ci		write_sysreg(cval, cntp_cval_el0);
4018c2ecf20Sopenharmony_ci	} else {
4028c2ecf20Sopenharmony_ci		cval = evt + arch_counter_get_cntvct_stable();
4038c2ecf20Sopenharmony_ci		write_sysreg(cval, cntv_cval_el0);
4048c2ecf20Sopenharmony_ci	}
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
4078c2ecf20Sopenharmony_ci}
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_cistatic __maybe_unused int erratum_set_next_event_tval_virt(unsigned long evt,
4108c2ecf20Sopenharmony_ci					    struct clock_event_device *clk)
4118c2ecf20Sopenharmony_ci{
4128c2ecf20Sopenharmony_ci	erratum_set_next_event_tval_generic(ARCH_TIMER_VIRT_ACCESS, evt, clk);
4138c2ecf20Sopenharmony_ci	return 0;
4148c2ecf20Sopenharmony_ci}
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_cistatic __maybe_unused int erratum_set_next_event_tval_phys(unsigned long evt,
4178c2ecf20Sopenharmony_ci					    struct clock_event_device *clk)
4188c2ecf20Sopenharmony_ci{
4198c2ecf20Sopenharmony_ci	erratum_set_next_event_tval_generic(ARCH_TIMER_PHYS_ACCESS, evt, clk);
4208c2ecf20Sopenharmony_ci	return 0;
4218c2ecf20Sopenharmony_ci}
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_cistatic const struct arch_timer_erratum_workaround ool_workarounds[] = {
4248c2ecf20Sopenharmony_ci#ifdef CONFIG_FSL_ERRATUM_A008585
4258c2ecf20Sopenharmony_ci	{
4268c2ecf20Sopenharmony_ci		.match_type = ate_match_dt,
4278c2ecf20Sopenharmony_ci		.id = "fsl,erratum-a008585",
4288c2ecf20Sopenharmony_ci		.desc = "Freescale erratum a005858",
4298c2ecf20Sopenharmony_ci		.read_cntp_tval_el0 = fsl_a008585_read_cntp_tval_el0,
4308c2ecf20Sopenharmony_ci		.read_cntv_tval_el0 = fsl_a008585_read_cntv_tval_el0,
4318c2ecf20Sopenharmony_ci		.read_cntpct_el0 = fsl_a008585_read_cntpct_el0,
4328c2ecf20Sopenharmony_ci		.read_cntvct_el0 = fsl_a008585_read_cntvct_el0,
4338c2ecf20Sopenharmony_ci		.set_next_event_phys = erratum_set_next_event_tval_phys,
4348c2ecf20Sopenharmony_ci		.set_next_event_virt = erratum_set_next_event_tval_virt,
4358c2ecf20Sopenharmony_ci	},
4368c2ecf20Sopenharmony_ci#endif
4378c2ecf20Sopenharmony_ci#ifdef CONFIG_HISILICON_ERRATUM_161010101
4388c2ecf20Sopenharmony_ci	{
4398c2ecf20Sopenharmony_ci		.match_type = ate_match_dt,
4408c2ecf20Sopenharmony_ci		.id = "hisilicon,erratum-161010101",
4418c2ecf20Sopenharmony_ci		.desc = "HiSilicon erratum 161010101",
4428c2ecf20Sopenharmony_ci		.read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
4438c2ecf20Sopenharmony_ci		.read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
4448c2ecf20Sopenharmony_ci		.read_cntpct_el0 = hisi_161010101_read_cntpct_el0,
4458c2ecf20Sopenharmony_ci		.read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
4468c2ecf20Sopenharmony_ci		.set_next_event_phys = erratum_set_next_event_tval_phys,
4478c2ecf20Sopenharmony_ci		.set_next_event_virt = erratum_set_next_event_tval_virt,
4488c2ecf20Sopenharmony_ci	},
4498c2ecf20Sopenharmony_ci	{
4508c2ecf20Sopenharmony_ci		.match_type = ate_match_acpi_oem_info,
4518c2ecf20Sopenharmony_ci		.id = hisi_161010101_oem_info,
4528c2ecf20Sopenharmony_ci		.desc = "HiSilicon erratum 161010101",
4538c2ecf20Sopenharmony_ci		.read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
4548c2ecf20Sopenharmony_ci		.read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
4558c2ecf20Sopenharmony_ci		.read_cntpct_el0 = hisi_161010101_read_cntpct_el0,
4568c2ecf20Sopenharmony_ci		.read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
4578c2ecf20Sopenharmony_ci		.set_next_event_phys = erratum_set_next_event_tval_phys,
4588c2ecf20Sopenharmony_ci		.set_next_event_virt = erratum_set_next_event_tval_virt,
4598c2ecf20Sopenharmony_ci	},
4608c2ecf20Sopenharmony_ci#endif
4618c2ecf20Sopenharmony_ci#ifdef CONFIG_ARM64_ERRATUM_858921
4628c2ecf20Sopenharmony_ci	{
4638c2ecf20Sopenharmony_ci		.match_type = ate_match_local_cap_id,
4648c2ecf20Sopenharmony_ci		.id = (void *)ARM64_WORKAROUND_858921,
4658c2ecf20Sopenharmony_ci		.desc = "ARM erratum 858921",
4668c2ecf20Sopenharmony_ci		.read_cntpct_el0 = arm64_858921_read_cntpct_el0,
4678c2ecf20Sopenharmony_ci		.read_cntvct_el0 = arm64_858921_read_cntvct_el0,
4688c2ecf20Sopenharmony_ci	},
4698c2ecf20Sopenharmony_ci#endif
4708c2ecf20Sopenharmony_ci#ifdef CONFIG_SUN50I_ERRATUM_UNKNOWN1
4718c2ecf20Sopenharmony_ci	{
4728c2ecf20Sopenharmony_ci		.match_type = ate_match_dt,
4738c2ecf20Sopenharmony_ci		.id = "allwinner,erratum-unknown1",
4748c2ecf20Sopenharmony_ci		.desc = "Allwinner erratum UNKNOWN1",
4758c2ecf20Sopenharmony_ci		.read_cntp_tval_el0 = sun50i_a64_read_cntp_tval_el0,
4768c2ecf20Sopenharmony_ci		.read_cntv_tval_el0 = sun50i_a64_read_cntv_tval_el0,
4778c2ecf20Sopenharmony_ci		.read_cntpct_el0 = sun50i_a64_read_cntpct_el0,
4788c2ecf20Sopenharmony_ci		.read_cntvct_el0 = sun50i_a64_read_cntvct_el0,
4798c2ecf20Sopenharmony_ci		.set_next_event_phys = erratum_set_next_event_tval_phys,
4808c2ecf20Sopenharmony_ci		.set_next_event_virt = erratum_set_next_event_tval_virt,
4818c2ecf20Sopenharmony_ci	},
4828c2ecf20Sopenharmony_ci#endif
4838c2ecf20Sopenharmony_ci#ifdef CONFIG_ARM64_ERRATUM_1418040
4848c2ecf20Sopenharmony_ci	{
4858c2ecf20Sopenharmony_ci		.match_type = ate_match_local_cap_id,
4868c2ecf20Sopenharmony_ci		.id = (void *)ARM64_WORKAROUND_1418040,
4878c2ecf20Sopenharmony_ci		.desc = "ARM erratum 1418040",
4888c2ecf20Sopenharmony_ci		.disable_compat_vdso = true,
4898c2ecf20Sopenharmony_ci	},
4908c2ecf20Sopenharmony_ci#endif
4918c2ecf20Sopenharmony_ci};
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_citypedef bool (*ate_match_fn_t)(const struct arch_timer_erratum_workaround *,
4948c2ecf20Sopenharmony_ci			       const void *);
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_cistatic
4978c2ecf20Sopenharmony_cibool arch_timer_check_dt_erratum(const struct arch_timer_erratum_workaround *wa,
4988c2ecf20Sopenharmony_ci				 const void *arg)
4998c2ecf20Sopenharmony_ci{
5008c2ecf20Sopenharmony_ci	const struct device_node *np = arg;
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	return of_property_read_bool(np, wa->id);
5038c2ecf20Sopenharmony_ci}
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_cistatic
5068c2ecf20Sopenharmony_cibool arch_timer_check_local_cap_erratum(const struct arch_timer_erratum_workaround *wa,
5078c2ecf20Sopenharmony_ci					const void *arg)
5088c2ecf20Sopenharmony_ci{
5098c2ecf20Sopenharmony_ci	return this_cpu_has_cap((uintptr_t)wa->id);
5108c2ecf20Sopenharmony_ci}
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_cistatic
5148c2ecf20Sopenharmony_cibool arch_timer_check_acpi_oem_erratum(const struct arch_timer_erratum_workaround *wa,
5158c2ecf20Sopenharmony_ci				       const void *arg)
5168c2ecf20Sopenharmony_ci{
5178c2ecf20Sopenharmony_ci	static const struct ate_acpi_oem_info empty_oem_info = {};
5188c2ecf20Sopenharmony_ci	const struct ate_acpi_oem_info *info = wa->id;
5198c2ecf20Sopenharmony_ci	const struct acpi_table_header *table = arg;
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	/* Iterate over the ACPI OEM info array, looking for a match */
5228c2ecf20Sopenharmony_ci	while (memcmp(info, &empty_oem_info, sizeof(*info))) {
5238c2ecf20Sopenharmony_ci		if (!memcmp(info->oem_id, table->oem_id, ACPI_OEM_ID_SIZE) &&
5248c2ecf20Sopenharmony_ci		    !memcmp(info->oem_table_id, table->oem_table_id, ACPI_OEM_TABLE_ID_SIZE) &&
5258c2ecf20Sopenharmony_ci		    info->oem_revision == table->oem_revision)
5268c2ecf20Sopenharmony_ci			return true;
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci		info++;
5298c2ecf20Sopenharmony_ci	}
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci	return false;
5328c2ecf20Sopenharmony_ci}
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_cistatic const struct arch_timer_erratum_workaround *
5358c2ecf20Sopenharmony_ciarch_timer_iterate_errata(enum arch_timer_erratum_match_type type,
5368c2ecf20Sopenharmony_ci			  ate_match_fn_t match_fn,
5378c2ecf20Sopenharmony_ci			  void *arg)
5388c2ecf20Sopenharmony_ci{
5398c2ecf20Sopenharmony_ci	int i;
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(ool_workarounds); i++) {
5428c2ecf20Sopenharmony_ci		if (ool_workarounds[i].match_type != type)
5438c2ecf20Sopenharmony_ci			continue;
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci		if (match_fn(&ool_workarounds[i], arg))
5468c2ecf20Sopenharmony_ci			return &ool_workarounds[i];
5478c2ecf20Sopenharmony_ci	}
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	return NULL;
5508c2ecf20Sopenharmony_ci}
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_cistatic
5538c2ecf20Sopenharmony_civoid arch_timer_enable_workaround(const struct arch_timer_erratum_workaround *wa,
5548c2ecf20Sopenharmony_ci				  bool local)
5558c2ecf20Sopenharmony_ci{
5568c2ecf20Sopenharmony_ci	int i;
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	if (local) {
5598c2ecf20Sopenharmony_ci		__this_cpu_write(timer_unstable_counter_workaround, wa);
5608c2ecf20Sopenharmony_ci	} else {
5618c2ecf20Sopenharmony_ci		for_each_possible_cpu(i)
5628c2ecf20Sopenharmony_ci			per_cpu(timer_unstable_counter_workaround, i) = wa;
5638c2ecf20Sopenharmony_ci	}
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	if (wa->read_cntvct_el0 || wa->read_cntpct_el0)
5668c2ecf20Sopenharmony_ci		atomic_set(&timer_unstable_counter_workaround_in_use, 1);
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	/*
5698c2ecf20Sopenharmony_ci	 * Don't use the vdso fastpath if errata require using the
5708c2ecf20Sopenharmony_ci	 * out-of-line counter accessor. We may change our mind pretty
5718c2ecf20Sopenharmony_ci	 * late in the game (with a per-CPU erratum, for example), so
5728c2ecf20Sopenharmony_ci	 * change both the default value and the vdso itself.
5738c2ecf20Sopenharmony_ci	 */
5748c2ecf20Sopenharmony_ci	if (wa->read_cntvct_el0) {
5758c2ecf20Sopenharmony_ci		clocksource_counter.vdso_clock_mode = VDSO_CLOCKMODE_NONE;
5768c2ecf20Sopenharmony_ci		vdso_default = VDSO_CLOCKMODE_NONE;
5778c2ecf20Sopenharmony_ci	} else if (wa->disable_compat_vdso && vdso_default != VDSO_CLOCKMODE_NONE) {
5788c2ecf20Sopenharmony_ci		vdso_default = VDSO_CLOCKMODE_ARCHTIMER_NOCOMPAT;
5798c2ecf20Sopenharmony_ci		clocksource_counter.vdso_clock_mode = vdso_default;
5808c2ecf20Sopenharmony_ci	}
5818c2ecf20Sopenharmony_ci}
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_cistatic void arch_timer_check_ool_workaround(enum arch_timer_erratum_match_type type,
5848c2ecf20Sopenharmony_ci					    void *arg)
5858c2ecf20Sopenharmony_ci{
5868c2ecf20Sopenharmony_ci	const struct arch_timer_erratum_workaround *wa, *__wa;
5878c2ecf20Sopenharmony_ci	ate_match_fn_t match_fn = NULL;
5888c2ecf20Sopenharmony_ci	bool local = false;
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	switch (type) {
5918c2ecf20Sopenharmony_ci	case ate_match_dt:
5928c2ecf20Sopenharmony_ci		match_fn = arch_timer_check_dt_erratum;
5938c2ecf20Sopenharmony_ci		break;
5948c2ecf20Sopenharmony_ci	case ate_match_local_cap_id:
5958c2ecf20Sopenharmony_ci		match_fn = arch_timer_check_local_cap_erratum;
5968c2ecf20Sopenharmony_ci		local = true;
5978c2ecf20Sopenharmony_ci		break;
5988c2ecf20Sopenharmony_ci	case ate_match_acpi_oem_info:
5998c2ecf20Sopenharmony_ci		match_fn = arch_timer_check_acpi_oem_erratum;
6008c2ecf20Sopenharmony_ci		break;
6018c2ecf20Sopenharmony_ci	default:
6028c2ecf20Sopenharmony_ci		WARN_ON(1);
6038c2ecf20Sopenharmony_ci		return;
6048c2ecf20Sopenharmony_ci	}
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	wa = arch_timer_iterate_errata(type, match_fn, arg);
6078c2ecf20Sopenharmony_ci	if (!wa)
6088c2ecf20Sopenharmony_ci		return;
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	__wa = __this_cpu_read(timer_unstable_counter_workaround);
6118c2ecf20Sopenharmony_ci	if (__wa && wa != __wa)
6128c2ecf20Sopenharmony_ci		pr_warn("Can't enable workaround for %s (clashes with %s\n)",
6138c2ecf20Sopenharmony_ci			wa->desc, __wa->desc);
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	if (__wa)
6168c2ecf20Sopenharmony_ci		return;
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	arch_timer_enable_workaround(wa, local);
6198c2ecf20Sopenharmony_ci	pr_info("Enabling %s workaround for %s\n",
6208c2ecf20Sopenharmony_ci		local ? "local" : "global", wa->desc);
6218c2ecf20Sopenharmony_ci}
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_cistatic bool arch_timer_this_cpu_has_cntvct_wa(void)
6248c2ecf20Sopenharmony_ci{
6258c2ecf20Sopenharmony_ci	return has_erratum_handler(read_cntvct_el0);
6268c2ecf20Sopenharmony_ci}
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_cistatic bool arch_timer_counter_has_wa(void)
6298c2ecf20Sopenharmony_ci{
6308c2ecf20Sopenharmony_ci	return atomic_read(&timer_unstable_counter_workaround_in_use);
6318c2ecf20Sopenharmony_ci}
6328c2ecf20Sopenharmony_ci#else
6338c2ecf20Sopenharmony_ci#define arch_timer_check_ool_workaround(t,a)		do { } while(0)
6348c2ecf20Sopenharmony_ci#define arch_timer_this_cpu_has_cntvct_wa()		({false;})
6358c2ecf20Sopenharmony_ci#define arch_timer_counter_has_wa()			({false;})
6368c2ecf20Sopenharmony_ci#endif /* CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND */
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_cistatic __always_inline irqreturn_t timer_handler(const int access,
6398c2ecf20Sopenharmony_ci					struct clock_event_device *evt)
6408c2ecf20Sopenharmony_ci{
6418c2ecf20Sopenharmony_ci	unsigned long ctrl;
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci	ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, evt);
6448c2ecf20Sopenharmony_ci	if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
6458c2ecf20Sopenharmony_ci		ctrl |= ARCH_TIMER_CTRL_IT_MASK;
6468c2ecf20Sopenharmony_ci		arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, evt);
6478c2ecf20Sopenharmony_ci		evt->event_handler(evt);
6488c2ecf20Sopenharmony_ci		return IRQ_HANDLED;
6498c2ecf20Sopenharmony_ci	}
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	return IRQ_NONE;
6528c2ecf20Sopenharmony_ci}
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_cistatic irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
6558c2ecf20Sopenharmony_ci{
6568c2ecf20Sopenharmony_ci	struct clock_event_device *evt = dev_id;
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci	return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
6598c2ecf20Sopenharmony_ci}
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_cistatic irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
6628c2ecf20Sopenharmony_ci{
6638c2ecf20Sopenharmony_ci	struct clock_event_device *evt = dev_id;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
6668c2ecf20Sopenharmony_ci}
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_cistatic irqreturn_t arch_timer_handler_phys_mem(int irq, void *dev_id)
6698c2ecf20Sopenharmony_ci{
6708c2ecf20Sopenharmony_ci	struct clock_event_device *evt = dev_id;
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci	return timer_handler(ARCH_TIMER_MEM_PHYS_ACCESS, evt);
6738c2ecf20Sopenharmony_ci}
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_cistatic irqreturn_t arch_timer_handler_virt_mem(int irq, void *dev_id)
6768c2ecf20Sopenharmony_ci{
6778c2ecf20Sopenharmony_ci	struct clock_event_device *evt = dev_id;
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	return timer_handler(ARCH_TIMER_MEM_VIRT_ACCESS, evt);
6808c2ecf20Sopenharmony_ci}
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_cistatic __always_inline int timer_shutdown(const int access,
6838c2ecf20Sopenharmony_ci					  struct clock_event_device *clk)
6848c2ecf20Sopenharmony_ci{
6858c2ecf20Sopenharmony_ci	unsigned long ctrl;
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci	ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
6888c2ecf20Sopenharmony_ci	ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
6898c2ecf20Sopenharmony_ci	arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	return 0;
6928c2ecf20Sopenharmony_ci}
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_cistatic int arch_timer_shutdown_virt(struct clock_event_device *clk)
6958c2ecf20Sopenharmony_ci{
6968c2ecf20Sopenharmony_ci	return timer_shutdown(ARCH_TIMER_VIRT_ACCESS, clk);
6978c2ecf20Sopenharmony_ci}
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_cistatic int arch_timer_shutdown_phys(struct clock_event_device *clk)
7008c2ecf20Sopenharmony_ci{
7018c2ecf20Sopenharmony_ci	return timer_shutdown(ARCH_TIMER_PHYS_ACCESS, clk);
7028c2ecf20Sopenharmony_ci}
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_cistatic int arch_timer_shutdown_virt_mem(struct clock_event_device *clk)
7058c2ecf20Sopenharmony_ci{
7068c2ecf20Sopenharmony_ci	return timer_shutdown(ARCH_TIMER_MEM_VIRT_ACCESS, clk);
7078c2ecf20Sopenharmony_ci}
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_cistatic int arch_timer_shutdown_phys_mem(struct clock_event_device *clk)
7108c2ecf20Sopenharmony_ci{
7118c2ecf20Sopenharmony_ci	return timer_shutdown(ARCH_TIMER_MEM_PHYS_ACCESS, clk);
7128c2ecf20Sopenharmony_ci}
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_cistatic __always_inline void set_next_event(const int access, unsigned long evt,
7158c2ecf20Sopenharmony_ci					   struct clock_event_device *clk)
7168c2ecf20Sopenharmony_ci{
7178c2ecf20Sopenharmony_ci	unsigned long ctrl;
7188c2ecf20Sopenharmony_ci	ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
7198c2ecf20Sopenharmony_ci	ctrl |= ARCH_TIMER_CTRL_ENABLE;
7208c2ecf20Sopenharmony_ci	ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
7218c2ecf20Sopenharmony_ci	arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt, clk);
7228c2ecf20Sopenharmony_ci	arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
7238c2ecf20Sopenharmony_ci}
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_cistatic int arch_timer_set_next_event_virt(unsigned long evt,
7268c2ecf20Sopenharmony_ci					  struct clock_event_device *clk)
7278c2ecf20Sopenharmony_ci{
7288c2ecf20Sopenharmony_ci	set_next_event(ARCH_TIMER_VIRT_ACCESS, evt, clk);
7298c2ecf20Sopenharmony_ci	return 0;
7308c2ecf20Sopenharmony_ci}
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_cistatic int arch_timer_set_next_event_phys(unsigned long evt,
7338c2ecf20Sopenharmony_ci					  struct clock_event_device *clk)
7348c2ecf20Sopenharmony_ci{
7358c2ecf20Sopenharmony_ci	set_next_event(ARCH_TIMER_PHYS_ACCESS, evt, clk);
7368c2ecf20Sopenharmony_ci	return 0;
7378c2ecf20Sopenharmony_ci}
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_cistatic int arch_timer_set_next_event_virt_mem(unsigned long evt,
7408c2ecf20Sopenharmony_ci					      struct clock_event_device *clk)
7418c2ecf20Sopenharmony_ci{
7428c2ecf20Sopenharmony_ci	set_next_event(ARCH_TIMER_MEM_VIRT_ACCESS, evt, clk);
7438c2ecf20Sopenharmony_ci	return 0;
7448c2ecf20Sopenharmony_ci}
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_cistatic int arch_timer_set_next_event_phys_mem(unsigned long evt,
7478c2ecf20Sopenharmony_ci					      struct clock_event_device *clk)
7488c2ecf20Sopenharmony_ci{
7498c2ecf20Sopenharmony_ci	set_next_event(ARCH_TIMER_MEM_PHYS_ACCESS, evt, clk);
7508c2ecf20Sopenharmony_ci	return 0;
7518c2ecf20Sopenharmony_ci}
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_cistatic void __arch_timer_setup(unsigned type,
7548c2ecf20Sopenharmony_ci			       struct clock_event_device *clk)
7558c2ecf20Sopenharmony_ci{
7568c2ecf20Sopenharmony_ci	clk->features = CLOCK_EVT_FEAT_ONESHOT;
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	if (type == ARCH_TIMER_TYPE_CP15) {
7598c2ecf20Sopenharmony_ci		typeof(clk->set_next_event) sne;
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci		arch_timer_check_ool_workaround(ate_match_local_cap_id, NULL);
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci		if (arch_timer_c3stop)
7648c2ecf20Sopenharmony_ci			clk->features |= CLOCK_EVT_FEAT_C3STOP;
7658c2ecf20Sopenharmony_ci		clk->name = "arch_sys_timer";
7668c2ecf20Sopenharmony_ci		clk->rating = 450;
7678c2ecf20Sopenharmony_ci		clk->cpumask = cpumask_of(smp_processor_id());
7688c2ecf20Sopenharmony_ci		clk->irq = arch_timer_ppi[arch_timer_uses_ppi];
7698c2ecf20Sopenharmony_ci		switch (arch_timer_uses_ppi) {
7708c2ecf20Sopenharmony_ci		case ARCH_TIMER_VIRT_PPI:
7718c2ecf20Sopenharmony_ci			clk->set_state_shutdown = arch_timer_shutdown_virt;
7728c2ecf20Sopenharmony_ci			clk->set_state_oneshot_stopped = arch_timer_shutdown_virt;
7738c2ecf20Sopenharmony_ci			sne = erratum_handler(set_next_event_virt);
7748c2ecf20Sopenharmony_ci			break;
7758c2ecf20Sopenharmony_ci		case ARCH_TIMER_PHYS_SECURE_PPI:
7768c2ecf20Sopenharmony_ci		case ARCH_TIMER_PHYS_NONSECURE_PPI:
7778c2ecf20Sopenharmony_ci		case ARCH_TIMER_HYP_PPI:
7788c2ecf20Sopenharmony_ci			clk->set_state_shutdown = arch_timer_shutdown_phys;
7798c2ecf20Sopenharmony_ci			clk->set_state_oneshot_stopped = arch_timer_shutdown_phys;
7808c2ecf20Sopenharmony_ci			sne = erratum_handler(set_next_event_phys);
7818c2ecf20Sopenharmony_ci			break;
7828c2ecf20Sopenharmony_ci		default:
7838c2ecf20Sopenharmony_ci			BUG();
7848c2ecf20Sopenharmony_ci		}
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci		clk->set_next_event = sne;
7878c2ecf20Sopenharmony_ci	} else {
7888c2ecf20Sopenharmony_ci		clk->features |= CLOCK_EVT_FEAT_DYNIRQ;
7898c2ecf20Sopenharmony_ci		clk->name = "arch_mem_timer";
7908c2ecf20Sopenharmony_ci		clk->rating = 400;
7918c2ecf20Sopenharmony_ci		clk->cpumask = cpu_possible_mask;
7928c2ecf20Sopenharmony_ci		if (arch_timer_mem_use_virtual) {
7938c2ecf20Sopenharmony_ci			clk->set_state_shutdown = arch_timer_shutdown_virt_mem;
7948c2ecf20Sopenharmony_ci			clk->set_state_oneshot_stopped = arch_timer_shutdown_virt_mem;
7958c2ecf20Sopenharmony_ci			clk->set_next_event =
7968c2ecf20Sopenharmony_ci				arch_timer_set_next_event_virt_mem;
7978c2ecf20Sopenharmony_ci		} else {
7988c2ecf20Sopenharmony_ci			clk->set_state_shutdown = arch_timer_shutdown_phys_mem;
7998c2ecf20Sopenharmony_ci			clk->set_state_oneshot_stopped = arch_timer_shutdown_phys_mem;
8008c2ecf20Sopenharmony_ci			clk->set_next_event =
8018c2ecf20Sopenharmony_ci				arch_timer_set_next_event_phys_mem;
8028c2ecf20Sopenharmony_ci		}
8038c2ecf20Sopenharmony_ci	}
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_ci	clk->set_state_shutdown(clk);
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci	clockevents_config_and_register(clk, arch_timer_rate, 0xf, 0x7fffffff);
8088c2ecf20Sopenharmony_ci}
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_cistatic void arch_timer_evtstrm_enable(int divider)
8118c2ecf20Sopenharmony_ci{
8128c2ecf20Sopenharmony_ci	u32 cntkctl = arch_timer_get_cntkctl();
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	cntkctl &= ~ARCH_TIMER_EVT_TRIGGER_MASK;
8158c2ecf20Sopenharmony_ci	/* Set the divider and enable virtual event stream */
8168c2ecf20Sopenharmony_ci	cntkctl |= (divider << ARCH_TIMER_EVT_TRIGGER_SHIFT)
8178c2ecf20Sopenharmony_ci			| ARCH_TIMER_VIRT_EVT_EN;
8188c2ecf20Sopenharmony_ci	arch_timer_set_cntkctl(cntkctl);
8198c2ecf20Sopenharmony_ci	arch_timer_set_evtstrm_feature();
8208c2ecf20Sopenharmony_ci	cpumask_set_cpu(smp_processor_id(), &evtstrm_available);
8218c2ecf20Sopenharmony_ci}
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_cistatic void arch_timer_configure_evtstream(void)
8248c2ecf20Sopenharmony_ci{
8258c2ecf20Sopenharmony_ci	int evt_stream_div, lsb;
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci	/*
8288c2ecf20Sopenharmony_ci	 * As the event stream can at most be generated at half the frequency
8298c2ecf20Sopenharmony_ci	 * of the counter, use half the frequency when computing the divider.
8308c2ecf20Sopenharmony_ci	 */
8318c2ecf20Sopenharmony_ci	evt_stream_div = arch_timer_rate / ARCH_TIMER_EVT_STREAM_FREQ / 2;
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci	/*
8348c2ecf20Sopenharmony_ci	 * Find the closest power of two to the divisor. If the adjacent bit
8358c2ecf20Sopenharmony_ci	 * of lsb (last set bit, starts from 0) is set, then we use (lsb + 1).
8368c2ecf20Sopenharmony_ci	 */
8378c2ecf20Sopenharmony_ci	lsb = fls(evt_stream_div) - 1;
8388c2ecf20Sopenharmony_ci	if (lsb > 0 && (evt_stream_div & BIT(lsb - 1)))
8398c2ecf20Sopenharmony_ci		lsb++;
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci	/* enable event stream */
8428c2ecf20Sopenharmony_ci	arch_timer_evtstrm_enable(max(0, min(lsb, 15)));
8438c2ecf20Sopenharmony_ci}
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_cistatic void arch_counter_set_user_access(void)
8468c2ecf20Sopenharmony_ci{
8478c2ecf20Sopenharmony_ci	u32 cntkctl = arch_timer_get_cntkctl();
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci	/* Disable user access to the timers and both counters */
8508c2ecf20Sopenharmony_ci	/* Also disable virtual event stream */
8518c2ecf20Sopenharmony_ci	cntkctl &= ~(ARCH_TIMER_USR_PT_ACCESS_EN
8528c2ecf20Sopenharmony_ci			| ARCH_TIMER_USR_VT_ACCESS_EN
8538c2ecf20Sopenharmony_ci		        | ARCH_TIMER_USR_VCT_ACCESS_EN
8548c2ecf20Sopenharmony_ci			| ARCH_TIMER_VIRT_EVT_EN
8558c2ecf20Sopenharmony_ci			| ARCH_TIMER_USR_PCT_ACCESS_EN);
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci	/*
8588c2ecf20Sopenharmony_ci	 * Enable user access to the virtual counter if it doesn't
8598c2ecf20Sopenharmony_ci	 * need to be workaround. The vdso may have been already
8608c2ecf20Sopenharmony_ci	 * disabled though.
8618c2ecf20Sopenharmony_ci	 */
8628c2ecf20Sopenharmony_ci	if (arch_timer_this_cpu_has_cntvct_wa())
8638c2ecf20Sopenharmony_ci		pr_info("CPU%d: Trapping CNTVCT access\n", smp_processor_id());
8648c2ecf20Sopenharmony_ci	else
8658c2ecf20Sopenharmony_ci		cntkctl |= ARCH_TIMER_USR_VCT_ACCESS_EN;
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_ci	arch_timer_set_cntkctl(cntkctl);
8688c2ecf20Sopenharmony_ci}
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_cistatic bool arch_timer_has_nonsecure_ppi(void)
8718c2ecf20Sopenharmony_ci{
8728c2ecf20Sopenharmony_ci	return (arch_timer_uses_ppi == ARCH_TIMER_PHYS_SECURE_PPI &&
8738c2ecf20Sopenharmony_ci		arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
8748c2ecf20Sopenharmony_ci}
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_cistatic u32 check_ppi_trigger(int irq)
8778c2ecf20Sopenharmony_ci{
8788c2ecf20Sopenharmony_ci	u32 flags = irq_get_trigger_type(irq);
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_ci	if (flags != IRQF_TRIGGER_HIGH && flags != IRQF_TRIGGER_LOW) {
8818c2ecf20Sopenharmony_ci		pr_warn("WARNING: Invalid trigger for IRQ%d, assuming level low\n", irq);
8828c2ecf20Sopenharmony_ci		pr_warn("WARNING: Please fix your firmware\n");
8838c2ecf20Sopenharmony_ci		flags = IRQF_TRIGGER_LOW;
8848c2ecf20Sopenharmony_ci	}
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	return flags;
8878c2ecf20Sopenharmony_ci}
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_cistatic int arch_timer_starting_cpu(unsigned int cpu)
8908c2ecf20Sopenharmony_ci{
8918c2ecf20Sopenharmony_ci	struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
8928c2ecf20Sopenharmony_ci	u32 flags;
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci	__arch_timer_setup(ARCH_TIMER_TYPE_CP15, clk);
8958c2ecf20Sopenharmony_ci
8968c2ecf20Sopenharmony_ci	flags = check_ppi_trigger(arch_timer_ppi[arch_timer_uses_ppi]);
8978c2ecf20Sopenharmony_ci	enable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], flags);
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci	if (arch_timer_has_nonsecure_ppi()) {
9008c2ecf20Sopenharmony_ci		flags = check_ppi_trigger(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
9018c2ecf20Sopenharmony_ci		enable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
9028c2ecf20Sopenharmony_ci				  flags);
9038c2ecf20Sopenharmony_ci	}
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci	arch_counter_set_user_access();
9068c2ecf20Sopenharmony_ci	if (evtstrm_enable)
9078c2ecf20Sopenharmony_ci		arch_timer_configure_evtstream();
9088c2ecf20Sopenharmony_ci
9098c2ecf20Sopenharmony_ci	return 0;
9108c2ecf20Sopenharmony_ci}
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_cistatic int validate_timer_rate(void)
9138c2ecf20Sopenharmony_ci{
9148c2ecf20Sopenharmony_ci	if (!arch_timer_rate)
9158c2ecf20Sopenharmony_ci		return -EINVAL;
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_ci	/* Arch timer frequency < 1MHz can cause trouble */
9188c2ecf20Sopenharmony_ci	WARN_ON(arch_timer_rate < 1000000);
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci	return 0;
9218c2ecf20Sopenharmony_ci}
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci/*
9248c2ecf20Sopenharmony_ci * For historical reasons, when probing with DT we use whichever (non-zero)
9258c2ecf20Sopenharmony_ci * rate was probed first, and don't verify that others match. If the first node
9268c2ecf20Sopenharmony_ci * probed has a clock-frequency property, this overrides the HW register.
9278c2ecf20Sopenharmony_ci */
9288c2ecf20Sopenharmony_cistatic void arch_timer_of_configure_rate(u32 rate, struct device_node *np)
9298c2ecf20Sopenharmony_ci{
9308c2ecf20Sopenharmony_ci	/* Who has more than one independent system counter? */
9318c2ecf20Sopenharmony_ci	if (arch_timer_rate)
9328c2ecf20Sopenharmony_ci		return;
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_ci	if (of_property_read_u32(np, "clock-frequency", &arch_timer_rate))
9358c2ecf20Sopenharmony_ci		arch_timer_rate = rate;
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_ci	/* Check the timer frequency. */
9388c2ecf20Sopenharmony_ci	if (validate_timer_rate())
9398c2ecf20Sopenharmony_ci		pr_warn("frequency not available\n");
9408c2ecf20Sopenharmony_ci}
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_cistatic void arch_timer_banner(unsigned type)
9438c2ecf20Sopenharmony_ci{
9448c2ecf20Sopenharmony_ci	pr_info("%s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
9458c2ecf20Sopenharmony_ci		type & ARCH_TIMER_TYPE_CP15 ? "cp15" : "",
9468c2ecf20Sopenharmony_ci		type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ?
9478c2ecf20Sopenharmony_ci			" and " : "",
9488c2ecf20Sopenharmony_ci		type & ARCH_TIMER_TYPE_MEM ? "mmio" : "",
9498c2ecf20Sopenharmony_ci		(unsigned long)arch_timer_rate / 1000000,
9508c2ecf20Sopenharmony_ci		(unsigned long)(arch_timer_rate / 10000) % 100,
9518c2ecf20Sopenharmony_ci		type & ARCH_TIMER_TYPE_CP15 ?
9528c2ecf20Sopenharmony_ci			(arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI) ? "virt" : "phys" :
9538c2ecf20Sopenharmony_ci			"",
9548c2ecf20Sopenharmony_ci		type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ? "/" : "",
9558c2ecf20Sopenharmony_ci		type & ARCH_TIMER_TYPE_MEM ?
9568c2ecf20Sopenharmony_ci			arch_timer_mem_use_virtual ? "virt" : "phys" :
9578c2ecf20Sopenharmony_ci			"");
9588c2ecf20Sopenharmony_ci}
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_ciu32 arch_timer_get_rate(void)
9618c2ecf20Sopenharmony_ci{
9628c2ecf20Sopenharmony_ci	return arch_timer_rate;
9638c2ecf20Sopenharmony_ci}
9648c2ecf20Sopenharmony_ci
9658c2ecf20Sopenharmony_cibool arch_timer_evtstrm_available(void)
9668c2ecf20Sopenharmony_ci{
9678c2ecf20Sopenharmony_ci	/*
9688c2ecf20Sopenharmony_ci	 * We might get called from a preemptible context. This is fine
9698c2ecf20Sopenharmony_ci	 * because availability of the event stream should be always the same
9708c2ecf20Sopenharmony_ci	 * for a preemptible context and context where we might resume a task.
9718c2ecf20Sopenharmony_ci	 */
9728c2ecf20Sopenharmony_ci	return cpumask_test_cpu(raw_smp_processor_id(), &evtstrm_available);
9738c2ecf20Sopenharmony_ci}
9748c2ecf20Sopenharmony_ci
9758c2ecf20Sopenharmony_cistatic u64 arch_counter_get_cntvct_mem(void)
9768c2ecf20Sopenharmony_ci{
9778c2ecf20Sopenharmony_ci	u32 vct_lo, vct_hi, tmp_hi;
9788c2ecf20Sopenharmony_ci
9798c2ecf20Sopenharmony_ci	do {
9808c2ecf20Sopenharmony_ci		vct_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
9818c2ecf20Sopenharmony_ci		vct_lo = readl_relaxed(arch_counter_base + CNTVCT_LO);
9828c2ecf20Sopenharmony_ci		tmp_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
9838c2ecf20Sopenharmony_ci	} while (vct_hi != tmp_hi);
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci	return ((u64) vct_hi << 32) | vct_lo;
9868c2ecf20Sopenharmony_ci}
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_cistatic struct arch_timer_kvm_info arch_timer_kvm_info;
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_cistruct arch_timer_kvm_info *arch_timer_get_kvm_info(void)
9918c2ecf20Sopenharmony_ci{
9928c2ecf20Sopenharmony_ci	return &arch_timer_kvm_info;
9938c2ecf20Sopenharmony_ci}
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_cistatic void __init arch_counter_register(unsigned type)
9968c2ecf20Sopenharmony_ci{
9978c2ecf20Sopenharmony_ci	u64 start_count;
9988c2ecf20Sopenharmony_ci
9998c2ecf20Sopenharmony_ci	/* Register the CP15 based counter if we have one */
10008c2ecf20Sopenharmony_ci	if (type & ARCH_TIMER_TYPE_CP15) {
10018c2ecf20Sopenharmony_ci		u64 (*rd)(void);
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_ci		if ((IS_ENABLED(CONFIG_ARM64) && !is_hyp_mode_available()) ||
10048c2ecf20Sopenharmony_ci		    arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI) {
10058c2ecf20Sopenharmony_ci			if (arch_timer_counter_has_wa())
10068c2ecf20Sopenharmony_ci				rd = arch_counter_get_cntvct_stable;
10078c2ecf20Sopenharmony_ci			else
10088c2ecf20Sopenharmony_ci				rd = arch_counter_get_cntvct;
10098c2ecf20Sopenharmony_ci		} else {
10108c2ecf20Sopenharmony_ci			if (arch_timer_counter_has_wa())
10118c2ecf20Sopenharmony_ci				rd = arch_counter_get_cntpct_stable;
10128c2ecf20Sopenharmony_ci			else
10138c2ecf20Sopenharmony_ci				rd = arch_counter_get_cntpct;
10148c2ecf20Sopenharmony_ci		}
10158c2ecf20Sopenharmony_ci
10168c2ecf20Sopenharmony_ci		arch_timer_read_counter = rd;
10178c2ecf20Sopenharmony_ci		clocksource_counter.vdso_clock_mode = vdso_default;
10188c2ecf20Sopenharmony_ci	} else {
10198c2ecf20Sopenharmony_ci		arch_timer_read_counter = arch_counter_get_cntvct_mem;
10208c2ecf20Sopenharmony_ci	}
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci	if (!arch_counter_suspend_stop)
10238c2ecf20Sopenharmony_ci		clocksource_counter.flags |= CLOCK_SOURCE_SUSPEND_NONSTOP;
10248c2ecf20Sopenharmony_ci	start_count = arch_timer_read_counter();
10258c2ecf20Sopenharmony_ci	clocksource_register_hz(&clocksource_counter, arch_timer_rate);
10268c2ecf20Sopenharmony_ci	cyclecounter.mult = clocksource_counter.mult;
10278c2ecf20Sopenharmony_ci	cyclecounter.shift = clocksource_counter.shift;
10288c2ecf20Sopenharmony_ci	timecounter_init(&arch_timer_kvm_info.timecounter,
10298c2ecf20Sopenharmony_ci			 &cyclecounter, start_count);
10308c2ecf20Sopenharmony_ci
10318c2ecf20Sopenharmony_ci	/* 56 bits minimum, so we assume worst case rollover */
10328c2ecf20Sopenharmony_ci	sched_clock_register(arch_timer_read_counter, 56, arch_timer_rate);
10338c2ecf20Sopenharmony_ci}
10348c2ecf20Sopenharmony_ci
10358c2ecf20Sopenharmony_cistatic void arch_timer_stop(struct clock_event_device *clk)
10368c2ecf20Sopenharmony_ci{
10378c2ecf20Sopenharmony_ci	pr_debug("disable IRQ%d cpu #%d\n", clk->irq, smp_processor_id());
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_ci	disable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi]);
10408c2ecf20Sopenharmony_ci	if (arch_timer_has_nonsecure_ppi())
10418c2ecf20Sopenharmony_ci		disable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
10428c2ecf20Sopenharmony_ci
10438c2ecf20Sopenharmony_ci	clk->set_state_shutdown(clk);
10448c2ecf20Sopenharmony_ci}
10458c2ecf20Sopenharmony_ci
10468c2ecf20Sopenharmony_cistatic int arch_timer_dying_cpu(unsigned int cpu)
10478c2ecf20Sopenharmony_ci{
10488c2ecf20Sopenharmony_ci	struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
10498c2ecf20Sopenharmony_ci
10508c2ecf20Sopenharmony_ci	cpumask_clear_cpu(smp_processor_id(), &evtstrm_available);
10518c2ecf20Sopenharmony_ci
10528c2ecf20Sopenharmony_ci	arch_timer_stop(clk);
10538c2ecf20Sopenharmony_ci	return 0;
10548c2ecf20Sopenharmony_ci}
10558c2ecf20Sopenharmony_ci
10568c2ecf20Sopenharmony_ci#ifdef CONFIG_CPU_PM
10578c2ecf20Sopenharmony_cistatic DEFINE_PER_CPU(unsigned long, saved_cntkctl);
10588c2ecf20Sopenharmony_cistatic int arch_timer_cpu_pm_notify(struct notifier_block *self,
10598c2ecf20Sopenharmony_ci				    unsigned long action, void *hcpu)
10608c2ecf20Sopenharmony_ci{
10618c2ecf20Sopenharmony_ci	if (action == CPU_PM_ENTER) {
10628c2ecf20Sopenharmony_ci		__this_cpu_write(saved_cntkctl, arch_timer_get_cntkctl());
10638c2ecf20Sopenharmony_ci
10648c2ecf20Sopenharmony_ci		cpumask_clear_cpu(smp_processor_id(), &evtstrm_available);
10658c2ecf20Sopenharmony_ci	} else if (action == CPU_PM_ENTER_FAILED || action == CPU_PM_EXIT) {
10668c2ecf20Sopenharmony_ci		arch_timer_set_cntkctl(__this_cpu_read(saved_cntkctl));
10678c2ecf20Sopenharmony_ci
10688c2ecf20Sopenharmony_ci		if (arch_timer_have_evtstrm_feature())
10698c2ecf20Sopenharmony_ci			cpumask_set_cpu(smp_processor_id(), &evtstrm_available);
10708c2ecf20Sopenharmony_ci	}
10718c2ecf20Sopenharmony_ci	return NOTIFY_OK;
10728c2ecf20Sopenharmony_ci}
10738c2ecf20Sopenharmony_ci
10748c2ecf20Sopenharmony_cistatic struct notifier_block arch_timer_cpu_pm_notifier = {
10758c2ecf20Sopenharmony_ci	.notifier_call = arch_timer_cpu_pm_notify,
10768c2ecf20Sopenharmony_ci};
10778c2ecf20Sopenharmony_ci
10788c2ecf20Sopenharmony_cistatic int __init arch_timer_cpu_pm_init(void)
10798c2ecf20Sopenharmony_ci{
10808c2ecf20Sopenharmony_ci	return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier);
10818c2ecf20Sopenharmony_ci}
10828c2ecf20Sopenharmony_ci
10838c2ecf20Sopenharmony_cistatic void __init arch_timer_cpu_pm_deinit(void)
10848c2ecf20Sopenharmony_ci{
10858c2ecf20Sopenharmony_ci	WARN_ON(cpu_pm_unregister_notifier(&arch_timer_cpu_pm_notifier));
10868c2ecf20Sopenharmony_ci}
10878c2ecf20Sopenharmony_ci
10888c2ecf20Sopenharmony_ci#else
10898c2ecf20Sopenharmony_cistatic int __init arch_timer_cpu_pm_init(void)
10908c2ecf20Sopenharmony_ci{
10918c2ecf20Sopenharmony_ci	return 0;
10928c2ecf20Sopenharmony_ci}
10938c2ecf20Sopenharmony_ci
10948c2ecf20Sopenharmony_cistatic void __init arch_timer_cpu_pm_deinit(void)
10958c2ecf20Sopenharmony_ci{
10968c2ecf20Sopenharmony_ci}
10978c2ecf20Sopenharmony_ci#endif
10988c2ecf20Sopenharmony_ci
10998c2ecf20Sopenharmony_cistatic int __init arch_timer_register(void)
11008c2ecf20Sopenharmony_ci{
11018c2ecf20Sopenharmony_ci	int err;
11028c2ecf20Sopenharmony_ci	int ppi;
11038c2ecf20Sopenharmony_ci
11048c2ecf20Sopenharmony_ci	arch_timer_evt = alloc_percpu(struct clock_event_device);
11058c2ecf20Sopenharmony_ci	if (!arch_timer_evt) {
11068c2ecf20Sopenharmony_ci		err = -ENOMEM;
11078c2ecf20Sopenharmony_ci		goto out;
11088c2ecf20Sopenharmony_ci	}
11098c2ecf20Sopenharmony_ci
11108c2ecf20Sopenharmony_ci	ppi = arch_timer_ppi[arch_timer_uses_ppi];
11118c2ecf20Sopenharmony_ci	switch (arch_timer_uses_ppi) {
11128c2ecf20Sopenharmony_ci	case ARCH_TIMER_VIRT_PPI:
11138c2ecf20Sopenharmony_ci		err = request_percpu_irq(ppi, arch_timer_handler_virt,
11148c2ecf20Sopenharmony_ci					 "arch_timer", arch_timer_evt);
11158c2ecf20Sopenharmony_ci		break;
11168c2ecf20Sopenharmony_ci	case ARCH_TIMER_PHYS_SECURE_PPI:
11178c2ecf20Sopenharmony_ci	case ARCH_TIMER_PHYS_NONSECURE_PPI:
11188c2ecf20Sopenharmony_ci		err = request_percpu_irq(ppi, arch_timer_handler_phys,
11198c2ecf20Sopenharmony_ci					 "arch_timer", arch_timer_evt);
11208c2ecf20Sopenharmony_ci		if (!err && arch_timer_has_nonsecure_ppi()) {
11218c2ecf20Sopenharmony_ci			ppi = arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI];
11228c2ecf20Sopenharmony_ci			err = request_percpu_irq(ppi, arch_timer_handler_phys,
11238c2ecf20Sopenharmony_ci						 "arch_timer", arch_timer_evt);
11248c2ecf20Sopenharmony_ci			if (err)
11258c2ecf20Sopenharmony_ci				free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_SECURE_PPI],
11268c2ecf20Sopenharmony_ci						arch_timer_evt);
11278c2ecf20Sopenharmony_ci		}
11288c2ecf20Sopenharmony_ci		break;
11298c2ecf20Sopenharmony_ci	case ARCH_TIMER_HYP_PPI:
11308c2ecf20Sopenharmony_ci		err = request_percpu_irq(ppi, arch_timer_handler_phys,
11318c2ecf20Sopenharmony_ci					 "arch_timer", arch_timer_evt);
11328c2ecf20Sopenharmony_ci		break;
11338c2ecf20Sopenharmony_ci	default:
11348c2ecf20Sopenharmony_ci		BUG();
11358c2ecf20Sopenharmony_ci	}
11368c2ecf20Sopenharmony_ci
11378c2ecf20Sopenharmony_ci	if (err) {
11388c2ecf20Sopenharmony_ci		pr_err("can't register interrupt %d (%d)\n", ppi, err);
11398c2ecf20Sopenharmony_ci		goto out_free;
11408c2ecf20Sopenharmony_ci	}
11418c2ecf20Sopenharmony_ci
11428c2ecf20Sopenharmony_ci	err = arch_timer_cpu_pm_init();
11438c2ecf20Sopenharmony_ci	if (err)
11448c2ecf20Sopenharmony_ci		goto out_unreg_notify;
11458c2ecf20Sopenharmony_ci
11468c2ecf20Sopenharmony_ci	/* Register and immediately configure the timer on the boot CPU */
11478c2ecf20Sopenharmony_ci	err = cpuhp_setup_state(CPUHP_AP_ARM_ARCH_TIMER_STARTING,
11488c2ecf20Sopenharmony_ci				"clockevents/arm/arch_timer:starting",
11498c2ecf20Sopenharmony_ci				arch_timer_starting_cpu, arch_timer_dying_cpu);
11508c2ecf20Sopenharmony_ci	if (err)
11518c2ecf20Sopenharmony_ci		goto out_unreg_cpupm;
11528c2ecf20Sopenharmony_ci	return 0;
11538c2ecf20Sopenharmony_ci
11548c2ecf20Sopenharmony_ciout_unreg_cpupm:
11558c2ecf20Sopenharmony_ci	arch_timer_cpu_pm_deinit();
11568c2ecf20Sopenharmony_ci
11578c2ecf20Sopenharmony_ciout_unreg_notify:
11588c2ecf20Sopenharmony_ci	free_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], arch_timer_evt);
11598c2ecf20Sopenharmony_ci	if (arch_timer_has_nonsecure_ppi())
11608c2ecf20Sopenharmony_ci		free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
11618c2ecf20Sopenharmony_ci				arch_timer_evt);
11628c2ecf20Sopenharmony_ci
11638c2ecf20Sopenharmony_ciout_free:
11648c2ecf20Sopenharmony_ci	free_percpu(arch_timer_evt);
11658c2ecf20Sopenharmony_ciout:
11668c2ecf20Sopenharmony_ci	return err;
11678c2ecf20Sopenharmony_ci}
11688c2ecf20Sopenharmony_ci
11698c2ecf20Sopenharmony_cistatic int __init arch_timer_mem_register(void __iomem *base, unsigned int irq)
11708c2ecf20Sopenharmony_ci{
11718c2ecf20Sopenharmony_ci	int ret;
11728c2ecf20Sopenharmony_ci	irq_handler_t func;
11738c2ecf20Sopenharmony_ci	struct arch_timer *t;
11748c2ecf20Sopenharmony_ci
11758c2ecf20Sopenharmony_ci	t = kzalloc(sizeof(*t), GFP_KERNEL);
11768c2ecf20Sopenharmony_ci	if (!t)
11778c2ecf20Sopenharmony_ci		return -ENOMEM;
11788c2ecf20Sopenharmony_ci
11798c2ecf20Sopenharmony_ci	t->base = base;
11808c2ecf20Sopenharmony_ci	t->evt.irq = irq;
11818c2ecf20Sopenharmony_ci	__arch_timer_setup(ARCH_TIMER_TYPE_MEM, &t->evt);
11828c2ecf20Sopenharmony_ci
11838c2ecf20Sopenharmony_ci	if (arch_timer_mem_use_virtual)
11848c2ecf20Sopenharmony_ci		func = arch_timer_handler_virt_mem;
11858c2ecf20Sopenharmony_ci	else
11868c2ecf20Sopenharmony_ci		func = arch_timer_handler_phys_mem;
11878c2ecf20Sopenharmony_ci
11888c2ecf20Sopenharmony_ci	ret = request_irq(irq, func, IRQF_TIMER, "arch_mem_timer", &t->evt);
11898c2ecf20Sopenharmony_ci	if (ret) {
11908c2ecf20Sopenharmony_ci		pr_err("Failed to request mem timer irq\n");
11918c2ecf20Sopenharmony_ci		kfree(t);
11928c2ecf20Sopenharmony_ci	}
11938c2ecf20Sopenharmony_ci
11948c2ecf20Sopenharmony_ci	return ret;
11958c2ecf20Sopenharmony_ci}
11968c2ecf20Sopenharmony_ci
11978c2ecf20Sopenharmony_cistatic const struct of_device_id arch_timer_of_match[] __initconst = {
11988c2ecf20Sopenharmony_ci	{ .compatible   = "arm,armv7-timer",    },
11998c2ecf20Sopenharmony_ci	{ .compatible   = "arm,armv8-timer",    },
12008c2ecf20Sopenharmony_ci	{},
12018c2ecf20Sopenharmony_ci};
12028c2ecf20Sopenharmony_ci
12038c2ecf20Sopenharmony_cistatic const struct of_device_id arch_timer_mem_of_match[] __initconst = {
12048c2ecf20Sopenharmony_ci	{ .compatible   = "arm,armv7-timer-mem", },
12058c2ecf20Sopenharmony_ci	{},
12068c2ecf20Sopenharmony_ci};
12078c2ecf20Sopenharmony_ci
12088c2ecf20Sopenharmony_cistatic bool __init arch_timer_needs_of_probing(void)
12098c2ecf20Sopenharmony_ci{
12108c2ecf20Sopenharmony_ci	struct device_node *dn;
12118c2ecf20Sopenharmony_ci	bool needs_probing = false;
12128c2ecf20Sopenharmony_ci	unsigned int mask = ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM;
12138c2ecf20Sopenharmony_ci
12148c2ecf20Sopenharmony_ci	/* We have two timers, and both device-tree nodes are probed. */
12158c2ecf20Sopenharmony_ci	if ((arch_timers_present & mask) == mask)
12168c2ecf20Sopenharmony_ci		return false;
12178c2ecf20Sopenharmony_ci
12188c2ecf20Sopenharmony_ci	/*
12198c2ecf20Sopenharmony_ci	 * Only one type of timer is probed,
12208c2ecf20Sopenharmony_ci	 * check if we have another type of timer node in device-tree.
12218c2ecf20Sopenharmony_ci	 */
12228c2ecf20Sopenharmony_ci	if (arch_timers_present & ARCH_TIMER_TYPE_CP15)
12238c2ecf20Sopenharmony_ci		dn = of_find_matching_node(NULL, arch_timer_mem_of_match);
12248c2ecf20Sopenharmony_ci	else
12258c2ecf20Sopenharmony_ci		dn = of_find_matching_node(NULL, arch_timer_of_match);
12268c2ecf20Sopenharmony_ci
12278c2ecf20Sopenharmony_ci	if (dn && of_device_is_available(dn))
12288c2ecf20Sopenharmony_ci		needs_probing = true;
12298c2ecf20Sopenharmony_ci
12308c2ecf20Sopenharmony_ci	of_node_put(dn);
12318c2ecf20Sopenharmony_ci
12328c2ecf20Sopenharmony_ci	return needs_probing;
12338c2ecf20Sopenharmony_ci}
12348c2ecf20Sopenharmony_ci
12358c2ecf20Sopenharmony_cistatic int __init arch_timer_common_init(void)
12368c2ecf20Sopenharmony_ci{
12378c2ecf20Sopenharmony_ci	arch_timer_banner(arch_timers_present);
12388c2ecf20Sopenharmony_ci	arch_counter_register(arch_timers_present);
12398c2ecf20Sopenharmony_ci	return arch_timer_arch_init();
12408c2ecf20Sopenharmony_ci}
12418c2ecf20Sopenharmony_ci
12428c2ecf20Sopenharmony_ci/**
12438c2ecf20Sopenharmony_ci * arch_timer_select_ppi() - Select suitable PPI for the current system.
12448c2ecf20Sopenharmony_ci *
12458c2ecf20Sopenharmony_ci * If HYP mode is available, we know that the physical timer
12468c2ecf20Sopenharmony_ci * has been configured to be accessible from PL1. Use it, so
12478c2ecf20Sopenharmony_ci * that a guest can use the virtual timer instead.
12488c2ecf20Sopenharmony_ci *
12498c2ecf20Sopenharmony_ci * On ARMv8.1 with VH extensions, the kernel runs in HYP. VHE
12508c2ecf20Sopenharmony_ci * accesses to CNTP_*_EL1 registers are silently redirected to
12518c2ecf20Sopenharmony_ci * their CNTHP_*_EL2 counterparts, and use a different PPI
12528c2ecf20Sopenharmony_ci * number.
12538c2ecf20Sopenharmony_ci *
12548c2ecf20Sopenharmony_ci * If no interrupt provided for virtual timer, we'll have to
12558c2ecf20Sopenharmony_ci * stick to the physical timer. It'd better be accessible...
12568c2ecf20Sopenharmony_ci * For arm64 we never use the secure interrupt.
12578c2ecf20Sopenharmony_ci *
12588c2ecf20Sopenharmony_ci * Return: a suitable PPI type for the current system.
12598c2ecf20Sopenharmony_ci */
12608c2ecf20Sopenharmony_cistatic enum arch_timer_ppi_nr __init arch_timer_select_ppi(void)
12618c2ecf20Sopenharmony_ci{
12628c2ecf20Sopenharmony_ci	if (is_kernel_in_hyp_mode())
12638c2ecf20Sopenharmony_ci		return ARCH_TIMER_HYP_PPI;
12648c2ecf20Sopenharmony_ci
12658c2ecf20Sopenharmony_ci	if (!is_hyp_mode_available() && arch_timer_ppi[ARCH_TIMER_VIRT_PPI])
12668c2ecf20Sopenharmony_ci		return ARCH_TIMER_VIRT_PPI;
12678c2ecf20Sopenharmony_ci
12688c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_ARM64))
12698c2ecf20Sopenharmony_ci		return ARCH_TIMER_PHYS_NONSECURE_PPI;
12708c2ecf20Sopenharmony_ci
12718c2ecf20Sopenharmony_ci	return ARCH_TIMER_PHYS_SECURE_PPI;
12728c2ecf20Sopenharmony_ci}
12738c2ecf20Sopenharmony_ci
12748c2ecf20Sopenharmony_cistatic void __init arch_timer_populate_kvm_info(void)
12758c2ecf20Sopenharmony_ci{
12768c2ecf20Sopenharmony_ci	arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI];
12778c2ecf20Sopenharmony_ci	if (is_kernel_in_hyp_mode())
12788c2ecf20Sopenharmony_ci		arch_timer_kvm_info.physical_irq = arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI];
12798c2ecf20Sopenharmony_ci}
12808c2ecf20Sopenharmony_ci
12818c2ecf20Sopenharmony_cistatic int __init arch_timer_of_init(struct device_node *np)
12828c2ecf20Sopenharmony_ci{
12838c2ecf20Sopenharmony_ci	int i, ret;
12848c2ecf20Sopenharmony_ci	u32 rate;
12858c2ecf20Sopenharmony_ci
12868c2ecf20Sopenharmony_ci	if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
12878c2ecf20Sopenharmony_ci		pr_warn("multiple nodes in dt, skipping\n");
12888c2ecf20Sopenharmony_ci		return 0;
12898c2ecf20Sopenharmony_ci	}
12908c2ecf20Sopenharmony_ci
12918c2ecf20Sopenharmony_ci	arch_timers_present |= ARCH_TIMER_TYPE_CP15;
12928c2ecf20Sopenharmony_ci	for (i = ARCH_TIMER_PHYS_SECURE_PPI; i < ARCH_TIMER_MAX_TIMER_PPI; i++)
12938c2ecf20Sopenharmony_ci		arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
12948c2ecf20Sopenharmony_ci
12958c2ecf20Sopenharmony_ci	arch_timer_populate_kvm_info();
12968c2ecf20Sopenharmony_ci
12978c2ecf20Sopenharmony_ci	rate = arch_timer_get_cntfrq();
12988c2ecf20Sopenharmony_ci	arch_timer_of_configure_rate(rate, np);
12998c2ecf20Sopenharmony_ci
13008c2ecf20Sopenharmony_ci	arch_timer_c3stop = !of_property_read_bool(np, "always-on");
13018c2ecf20Sopenharmony_ci
13028c2ecf20Sopenharmony_ci	/* Check for globally applicable workarounds */
13038c2ecf20Sopenharmony_ci	arch_timer_check_ool_workaround(ate_match_dt, np);
13048c2ecf20Sopenharmony_ci
13058c2ecf20Sopenharmony_ci	/*
13068c2ecf20Sopenharmony_ci	 * If we cannot rely on firmware initializing the timer registers then
13078c2ecf20Sopenharmony_ci	 * we should use the physical timers instead.
13088c2ecf20Sopenharmony_ci	 */
13098c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_ARM) &&
13108c2ecf20Sopenharmony_ci	    of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
13118c2ecf20Sopenharmony_ci		arch_timer_uses_ppi = ARCH_TIMER_PHYS_SECURE_PPI;
13128c2ecf20Sopenharmony_ci	else
13138c2ecf20Sopenharmony_ci		arch_timer_uses_ppi = arch_timer_select_ppi();
13148c2ecf20Sopenharmony_ci
13158c2ecf20Sopenharmony_ci	if (!arch_timer_ppi[arch_timer_uses_ppi]) {
13168c2ecf20Sopenharmony_ci		pr_err("No interrupt available, giving up\n");
13178c2ecf20Sopenharmony_ci		return -EINVAL;
13188c2ecf20Sopenharmony_ci	}
13198c2ecf20Sopenharmony_ci
13208c2ecf20Sopenharmony_ci	/* On some systems, the counter stops ticking when in suspend. */
13218c2ecf20Sopenharmony_ci	arch_counter_suspend_stop = of_property_read_bool(np,
13228c2ecf20Sopenharmony_ci							 "arm,no-tick-in-suspend");
13238c2ecf20Sopenharmony_ci
13248c2ecf20Sopenharmony_ci	ret = arch_timer_register();
13258c2ecf20Sopenharmony_ci	if (ret)
13268c2ecf20Sopenharmony_ci		return ret;
13278c2ecf20Sopenharmony_ci
13288c2ecf20Sopenharmony_ci	if (arch_timer_needs_of_probing())
13298c2ecf20Sopenharmony_ci		return 0;
13308c2ecf20Sopenharmony_ci
13318c2ecf20Sopenharmony_ci	return arch_timer_common_init();
13328c2ecf20Sopenharmony_ci}
13338c2ecf20Sopenharmony_ciTIMER_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_of_init);
13348c2ecf20Sopenharmony_ciTIMER_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_of_init);
13358c2ecf20Sopenharmony_ci
13368c2ecf20Sopenharmony_cistatic u32 __init
13378c2ecf20Sopenharmony_ciarch_timer_mem_frame_get_cntfrq(struct arch_timer_mem_frame *frame)
13388c2ecf20Sopenharmony_ci{
13398c2ecf20Sopenharmony_ci	void __iomem *base;
13408c2ecf20Sopenharmony_ci	u32 rate;
13418c2ecf20Sopenharmony_ci
13428c2ecf20Sopenharmony_ci	base = ioremap(frame->cntbase, frame->size);
13438c2ecf20Sopenharmony_ci	if (!base) {
13448c2ecf20Sopenharmony_ci		pr_err("Unable to map frame @ %pa\n", &frame->cntbase);
13458c2ecf20Sopenharmony_ci		return 0;
13468c2ecf20Sopenharmony_ci	}
13478c2ecf20Sopenharmony_ci
13488c2ecf20Sopenharmony_ci	rate = readl_relaxed(base + CNTFRQ);
13498c2ecf20Sopenharmony_ci
13508c2ecf20Sopenharmony_ci	iounmap(base);
13518c2ecf20Sopenharmony_ci
13528c2ecf20Sopenharmony_ci	return rate;
13538c2ecf20Sopenharmony_ci}
13548c2ecf20Sopenharmony_ci
13558c2ecf20Sopenharmony_cistatic struct arch_timer_mem_frame * __init
13568c2ecf20Sopenharmony_ciarch_timer_mem_find_best_frame(struct arch_timer_mem *timer_mem)
13578c2ecf20Sopenharmony_ci{
13588c2ecf20Sopenharmony_ci	struct arch_timer_mem_frame *frame, *best_frame = NULL;
13598c2ecf20Sopenharmony_ci	void __iomem *cntctlbase;
13608c2ecf20Sopenharmony_ci	u32 cnttidr;
13618c2ecf20Sopenharmony_ci	int i;
13628c2ecf20Sopenharmony_ci
13638c2ecf20Sopenharmony_ci	cntctlbase = ioremap(timer_mem->cntctlbase, timer_mem->size);
13648c2ecf20Sopenharmony_ci	if (!cntctlbase) {
13658c2ecf20Sopenharmony_ci		pr_err("Can't map CNTCTLBase @ %pa\n",
13668c2ecf20Sopenharmony_ci			&timer_mem->cntctlbase);
13678c2ecf20Sopenharmony_ci		return NULL;
13688c2ecf20Sopenharmony_ci	}
13698c2ecf20Sopenharmony_ci
13708c2ecf20Sopenharmony_ci	cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
13718c2ecf20Sopenharmony_ci
13728c2ecf20Sopenharmony_ci	/*
13738c2ecf20Sopenharmony_ci	 * Try to find a virtual capable frame. Otherwise fall back to a
13748c2ecf20Sopenharmony_ci	 * physical capable frame.
13758c2ecf20Sopenharmony_ci	 */
13768c2ecf20Sopenharmony_ci	for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
13778c2ecf20Sopenharmony_ci		u32 cntacr = CNTACR_RFRQ | CNTACR_RWPT | CNTACR_RPCT |
13788c2ecf20Sopenharmony_ci			     CNTACR_RWVT | CNTACR_RVOFF | CNTACR_RVCT;
13798c2ecf20Sopenharmony_ci
13808c2ecf20Sopenharmony_ci		frame = &timer_mem->frame[i];
13818c2ecf20Sopenharmony_ci		if (!frame->valid)
13828c2ecf20Sopenharmony_ci			continue;
13838c2ecf20Sopenharmony_ci
13848c2ecf20Sopenharmony_ci		/* Try enabling everything, and see what sticks */
13858c2ecf20Sopenharmony_ci		writel_relaxed(cntacr, cntctlbase + CNTACR(i));
13868c2ecf20Sopenharmony_ci		cntacr = readl_relaxed(cntctlbase + CNTACR(i));
13878c2ecf20Sopenharmony_ci
13888c2ecf20Sopenharmony_ci		if ((cnttidr & CNTTIDR_VIRT(i)) &&
13898c2ecf20Sopenharmony_ci		    !(~cntacr & (CNTACR_RWVT | CNTACR_RVCT))) {
13908c2ecf20Sopenharmony_ci			best_frame = frame;
13918c2ecf20Sopenharmony_ci			arch_timer_mem_use_virtual = true;
13928c2ecf20Sopenharmony_ci			break;
13938c2ecf20Sopenharmony_ci		}
13948c2ecf20Sopenharmony_ci
13958c2ecf20Sopenharmony_ci		if (~cntacr & (CNTACR_RWPT | CNTACR_RPCT))
13968c2ecf20Sopenharmony_ci			continue;
13978c2ecf20Sopenharmony_ci
13988c2ecf20Sopenharmony_ci		best_frame = frame;
13998c2ecf20Sopenharmony_ci	}
14008c2ecf20Sopenharmony_ci
14018c2ecf20Sopenharmony_ci	iounmap(cntctlbase);
14028c2ecf20Sopenharmony_ci
14038c2ecf20Sopenharmony_ci	return best_frame;
14048c2ecf20Sopenharmony_ci}
14058c2ecf20Sopenharmony_ci
14068c2ecf20Sopenharmony_cistatic int __init
14078c2ecf20Sopenharmony_ciarch_timer_mem_frame_register(struct arch_timer_mem_frame *frame)
14088c2ecf20Sopenharmony_ci{
14098c2ecf20Sopenharmony_ci	void __iomem *base;
14108c2ecf20Sopenharmony_ci	int ret, irq = 0;
14118c2ecf20Sopenharmony_ci
14128c2ecf20Sopenharmony_ci	if (arch_timer_mem_use_virtual)
14138c2ecf20Sopenharmony_ci		irq = frame->virt_irq;
14148c2ecf20Sopenharmony_ci	else
14158c2ecf20Sopenharmony_ci		irq = frame->phys_irq;
14168c2ecf20Sopenharmony_ci
14178c2ecf20Sopenharmony_ci	if (!irq) {
14188c2ecf20Sopenharmony_ci		pr_err("Frame missing %s irq.\n",
14198c2ecf20Sopenharmony_ci		       arch_timer_mem_use_virtual ? "virt" : "phys");
14208c2ecf20Sopenharmony_ci		return -EINVAL;
14218c2ecf20Sopenharmony_ci	}
14228c2ecf20Sopenharmony_ci
14238c2ecf20Sopenharmony_ci	if (!request_mem_region(frame->cntbase, frame->size,
14248c2ecf20Sopenharmony_ci				"arch_mem_timer"))
14258c2ecf20Sopenharmony_ci		return -EBUSY;
14268c2ecf20Sopenharmony_ci
14278c2ecf20Sopenharmony_ci	base = ioremap(frame->cntbase, frame->size);
14288c2ecf20Sopenharmony_ci	if (!base) {
14298c2ecf20Sopenharmony_ci		pr_err("Can't map frame's registers\n");
14308c2ecf20Sopenharmony_ci		return -ENXIO;
14318c2ecf20Sopenharmony_ci	}
14328c2ecf20Sopenharmony_ci
14338c2ecf20Sopenharmony_ci	ret = arch_timer_mem_register(base, irq);
14348c2ecf20Sopenharmony_ci	if (ret) {
14358c2ecf20Sopenharmony_ci		iounmap(base);
14368c2ecf20Sopenharmony_ci		return ret;
14378c2ecf20Sopenharmony_ci	}
14388c2ecf20Sopenharmony_ci
14398c2ecf20Sopenharmony_ci	arch_counter_base = base;
14408c2ecf20Sopenharmony_ci	arch_timers_present |= ARCH_TIMER_TYPE_MEM;
14418c2ecf20Sopenharmony_ci
14428c2ecf20Sopenharmony_ci	return 0;
14438c2ecf20Sopenharmony_ci}
14448c2ecf20Sopenharmony_ci
14458c2ecf20Sopenharmony_cistatic int __init arch_timer_mem_of_init(struct device_node *np)
14468c2ecf20Sopenharmony_ci{
14478c2ecf20Sopenharmony_ci	struct arch_timer_mem *timer_mem;
14488c2ecf20Sopenharmony_ci	struct arch_timer_mem_frame *frame;
14498c2ecf20Sopenharmony_ci	struct device_node *frame_node;
14508c2ecf20Sopenharmony_ci	struct resource res;
14518c2ecf20Sopenharmony_ci	int ret = -EINVAL;
14528c2ecf20Sopenharmony_ci	u32 rate;
14538c2ecf20Sopenharmony_ci
14548c2ecf20Sopenharmony_ci	timer_mem = kzalloc(sizeof(*timer_mem), GFP_KERNEL);
14558c2ecf20Sopenharmony_ci	if (!timer_mem)
14568c2ecf20Sopenharmony_ci		return -ENOMEM;
14578c2ecf20Sopenharmony_ci
14588c2ecf20Sopenharmony_ci	if (of_address_to_resource(np, 0, &res))
14598c2ecf20Sopenharmony_ci		goto out;
14608c2ecf20Sopenharmony_ci	timer_mem->cntctlbase = res.start;
14618c2ecf20Sopenharmony_ci	timer_mem->size = resource_size(&res);
14628c2ecf20Sopenharmony_ci
14638c2ecf20Sopenharmony_ci	for_each_available_child_of_node(np, frame_node) {
14648c2ecf20Sopenharmony_ci		u32 n;
14658c2ecf20Sopenharmony_ci		struct arch_timer_mem_frame *frame;
14668c2ecf20Sopenharmony_ci
14678c2ecf20Sopenharmony_ci		if (of_property_read_u32(frame_node, "frame-number", &n)) {
14688c2ecf20Sopenharmony_ci			pr_err(FW_BUG "Missing frame-number.\n");
14698c2ecf20Sopenharmony_ci			of_node_put(frame_node);
14708c2ecf20Sopenharmony_ci			goto out;
14718c2ecf20Sopenharmony_ci		}
14728c2ecf20Sopenharmony_ci		if (n >= ARCH_TIMER_MEM_MAX_FRAMES) {
14738c2ecf20Sopenharmony_ci			pr_err(FW_BUG "Wrong frame-number, only 0-%u are permitted.\n",
14748c2ecf20Sopenharmony_ci			       ARCH_TIMER_MEM_MAX_FRAMES - 1);
14758c2ecf20Sopenharmony_ci			of_node_put(frame_node);
14768c2ecf20Sopenharmony_ci			goto out;
14778c2ecf20Sopenharmony_ci		}
14788c2ecf20Sopenharmony_ci		frame = &timer_mem->frame[n];
14798c2ecf20Sopenharmony_ci
14808c2ecf20Sopenharmony_ci		if (frame->valid) {
14818c2ecf20Sopenharmony_ci			pr_err(FW_BUG "Duplicated frame-number.\n");
14828c2ecf20Sopenharmony_ci			of_node_put(frame_node);
14838c2ecf20Sopenharmony_ci			goto out;
14848c2ecf20Sopenharmony_ci		}
14858c2ecf20Sopenharmony_ci
14868c2ecf20Sopenharmony_ci		if (of_address_to_resource(frame_node, 0, &res)) {
14878c2ecf20Sopenharmony_ci			of_node_put(frame_node);
14888c2ecf20Sopenharmony_ci			goto out;
14898c2ecf20Sopenharmony_ci		}
14908c2ecf20Sopenharmony_ci		frame->cntbase = res.start;
14918c2ecf20Sopenharmony_ci		frame->size = resource_size(&res);
14928c2ecf20Sopenharmony_ci
14938c2ecf20Sopenharmony_ci		frame->virt_irq = irq_of_parse_and_map(frame_node,
14948c2ecf20Sopenharmony_ci						       ARCH_TIMER_VIRT_SPI);
14958c2ecf20Sopenharmony_ci		frame->phys_irq = irq_of_parse_and_map(frame_node,
14968c2ecf20Sopenharmony_ci						       ARCH_TIMER_PHYS_SPI);
14978c2ecf20Sopenharmony_ci
14988c2ecf20Sopenharmony_ci		frame->valid = true;
14998c2ecf20Sopenharmony_ci	}
15008c2ecf20Sopenharmony_ci
15018c2ecf20Sopenharmony_ci	frame = arch_timer_mem_find_best_frame(timer_mem);
15028c2ecf20Sopenharmony_ci	if (!frame) {
15038c2ecf20Sopenharmony_ci		pr_err("Unable to find a suitable frame in timer @ %pa\n",
15048c2ecf20Sopenharmony_ci			&timer_mem->cntctlbase);
15058c2ecf20Sopenharmony_ci		ret = -EINVAL;
15068c2ecf20Sopenharmony_ci		goto out;
15078c2ecf20Sopenharmony_ci	}
15088c2ecf20Sopenharmony_ci
15098c2ecf20Sopenharmony_ci	rate = arch_timer_mem_frame_get_cntfrq(frame);
15108c2ecf20Sopenharmony_ci	arch_timer_of_configure_rate(rate, np);
15118c2ecf20Sopenharmony_ci
15128c2ecf20Sopenharmony_ci	ret = arch_timer_mem_frame_register(frame);
15138c2ecf20Sopenharmony_ci	if (!ret && !arch_timer_needs_of_probing())
15148c2ecf20Sopenharmony_ci		ret = arch_timer_common_init();
15158c2ecf20Sopenharmony_ciout:
15168c2ecf20Sopenharmony_ci	kfree(timer_mem);
15178c2ecf20Sopenharmony_ci	return ret;
15188c2ecf20Sopenharmony_ci}
15198c2ecf20Sopenharmony_ciTIMER_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem",
15208c2ecf20Sopenharmony_ci		       arch_timer_mem_of_init);
15218c2ecf20Sopenharmony_ci
15228c2ecf20Sopenharmony_ci#ifdef CONFIG_ACPI_GTDT
15238c2ecf20Sopenharmony_cistatic int __init
15248c2ecf20Sopenharmony_ciarch_timer_mem_verify_cntfrq(struct arch_timer_mem *timer_mem)
15258c2ecf20Sopenharmony_ci{
15268c2ecf20Sopenharmony_ci	struct arch_timer_mem_frame *frame;
15278c2ecf20Sopenharmony_ci	u32 rate;
15288c2ecf20Sopenharmony_ci	int i;
15298c2ecf20Sopenharmony_ci
15308c2ecf20Sopenharmony_ci	for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
15318c2ecf20Sopenharmony_ci		frame = &timer_mem->frame[i];
15328c2ecf20Sopenharmony_ci
15338c2ecf20Sopenharmony_ci		if (!frame->valid)
15348c2ecf20Sopenharmony_ci			continue;
15358c2ecf20Sopenharmony_ci
15368c2ecf20Sopenharmony_ci		rate = arch_timer_mem_frame_get_cntfrq(frame);
15378c2ecf20Sopenharmony_ci		if (rate == arch_timer_rate)
15388c2ecf20Sopenharmony_ci			continue;
15398c2ecf20Sopenharmony_ci
15408c2ecf20Sopenharmony_ci		pr_err(FW_BUG "CNTFRQ mismatch: frame @ %pa: (0x%08lx), CPU: (0x%08lx)\n",
15418c2ecf20Sopenharmony_ci			&frame->cntbase,
15428c2ecf20Sopenharmony_ci			(unsigned long)rate, (unsigned long)arch_timer_rate);
15438c2ecf20Sopenharmony_ci
15448c2ecf20Sopenharmony_ci		return -EINVAL;
15458c2ecf20Sopenharmony_ci	}
15468c2ecf20Sopenharmony_ci
15478c2ecf20Sopenharmony_ci	return 0;
15488c2ecf20Sopenharmony_ci}
15498c2ecf20Sopenharmony_ci
15508c2ecf20Sopenharmony_cistatic int __init arch_timer_mem_acpi_init(int platform_timer_count)
15518c2ecf20Sopenharmony_ci{
15528c2ecf20Sopenharmony_ci	struct arch_timer_mem *timers, *timer;
15538c2ecf20Sopenharmony_ci	struct arch_timer_mem_frame *frame, *best_frame = NULL;
15548c2ecf20Sopenharmony_ci	int timer_count, i, ret = 0;
15558c2ecf20Sopenharmony_ci
15568c2ecf20Sopenharmony_ci	timers = kcalloc(platform_timer_count, sizeof(*timers),
15578c2ecf20Sopenharmony_ci			    GFP_KERNEL);
15588c2ecf20Sopenharmony_ci	if (!timers)
15598c2ecf20Sopenharmony_ci		return -ENOMEM;
15608c2ecf20Sopenharmony_ci
15618c2ecf20Sopenharmony_ci	ret = acpi_arch_timer_mem_init(timers, &timer_count);
15628c2ecf20Sopenharmony_ci	if (ret || !timer_count)
15638c2ecf20Sopenharmony_ci		goto out;
15648c2ecf20Sopenharmony_ci
15658c2ecf20Sopenharmony_ci	/*
15668c2ecf20Sopenharmony_ci	 * While unlikely, it's theoretically possible that none of the frames
15678c2ecf20Sopenharmony_ci	 * in a timer expose the combination of feature we want.
15688c2ecf20Sopenharmony_ci	 */
15698c2ecf20Sopenharmony_ci	for (i = 0; i < timer_count; i++) {
15708c2ecf20Sopenharmony_ci		timer = &timers[i];
15718c2ecf20Sopenharmony_ci
15728c2ecf20Sopenharmony_ci		frame = arch_timer_mem_find_best_frame(timer);
15738c2ecf20Sopenharmony_ci		if (!best_frame)
15748c2ecf20Sopenharmony_ci			best_frame = frame;
15758c2ecf20Sopenharmony_ci
15768c2ecf20Sopenharmony_ci		ret = arch_timer_mem_verify_cntfrq(timer);
15778c2ecf20Sopenharmony_ci		if (ret) {
15788c2ecf20Sopenharmony_ci			pr_err("Disabling MMIO timers due to CNTFRQ mismatch\n");
15798c2ecf20Sopenharmony_ci			goto out;
15808c2ecf20Sopenharmony_ci		}
15818c2ecf20Sopenharmony_ci
15828c2ecf20Sopenharmony_ci		if (!best_frame) /* implies !frame */
15838c2ecf20Sopenharmony_ci			/*
15848c2ecf20Sopenharmony_ci			 * Only complain about missing suitable frames if we
15858c2ecf20Sopenharmony_ci			 * haven't already found one in a previous iteration.
15868c2ecf20Sopenharmony_ci			 */
15878c2ecf20Sopenharmony_ci			pr_err("Unable to find a suitable frame in timer @ %pa\n",
15888c2ecf20Sopenharmony_ci				&timer->cntctlbase);
15898c2ecf20Sopenharmony_ci	}
15908c2ecf20Sopenharmony_ci
15918c2ecf20Sopenharmony_ci	if (best_frame)
15928c2ecf20Sopenharmony_ci		ret = arch_timer_mem_frame_register(best_frame);
15938c2ecf20Sopenharmony_ciout:
15948c2ecf20Sopenharmony_ci	kfree(timers);
15958c2ecf20Sopenharmony_ci	return ret;
15968c2ecf20Sopenharmony_ci}
15978c2ecf20Sopenharmony_ci
15988c2ecf20Sopenharmony_ci/* Initialize per-processor generic timer and memory-mapped timer(if present) */
15998c2ecf20Sopenharmony_cistatic int __init arch_timer_acpi_init(struct acpi_table_header *table)
16008c2ecf20Sopenharmony_ci{
16018c2ecf20Sopenharmony_ci	int ret, platform_timer_count;
16028c2ecf20Sopenharmony_ci
16038c2ecf20Sopenharmony_ci	if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
16048c2ecf20Sopenharmony_ci		pr_warn("already initialized, skipping\n");
16058c2ecf20Sopenharmony_ci		return -EINVAL;
16068c2ecf20Sopenharmony_ci	}
16078c2ecf20Sopenharmony_ci
16088c2ecf20Sopenharmony_ci	arch_timers_present |= ARCH_TIMER_TYPE_CP15;
16098c2ecf20Sopenharmony_ci
16108c2ecf20Sopenharmony_ci	ret = acpi_gtdt_init(table, &platform_timer_count);
16118c2ecf20Sopenharmony_ci	if (ret)
16128c2ecf20Sopenharmony_ci		return ret;
16138c2ecf20Sopenharmony_ci
16148c2ecf20Sopenharmony_ci	arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI] =
16158c2ecf20Sopenharmony_ci		acpi_gtdt_map_ppi(ARCH_TIMER_PHYS_NONSECURE_PPI);
16168c2ecf20Sopenharmony_ci
16178c2ecf20Sopenharmony_ci	arch_timer_ppi[ARCH_TIMER_VIRT_PPI] =
16188c2ecf20Sopenharmony_ci		acpi_gtdt_map_ppi(ARCH_TIMER_VIRT_PPI);
16198c2ecf20Sopenharmony_ci
16208c2ecf20Sopenharmony_ci	arch_timer_ppi[ARCH_TIMER_HYP_PPI] =
16218c2ecf20Sopenharmony_ci		acpi_gtdt_map_ppi(ARCH_TIMER_HYP_PPI);
16228c2ecf20Sopenharmony_ci
16238c2ecf20Sopenharmony_ci	arch_timer_populate_kvm_info();
16248c2ecf20Sopenharmony_ci
16258c2ecf20Sopenharmony_ci	/*
16268c2ecf20Sopenharmony_ci	 * When probing via ACPI, we have no mechanism to override the sysreg
16278c2ecf20Sopenharmony_ci	 * CNTFRQ value. This *must* be correct.
16288c2ecf20Sopenharmony_ci	 */
16298c2ecf20Sopenharmony_ci	arch_timer_rate = arch_timer_get_cntfrq();
16308c2ecf20Sopenharmony_ci	ret = validate_timer_rate();
16318c2ecf20Sopenharmony_ci	if (ret) {
16328c2ecf20Sopenharmony_ci		pr_err(FW_BUG "frequency not available.\n");
16338c2ecf20Sopenharmony_ci		return ret;
16348c2ecf20Sopenharmony_ci	}
16358c2ecf20Sopenharmony_ci
16368c2ecf20Sopenharmony_ci	arch_timer_uses_ppi = arch_timer_select_ppi();
16378c2ecf20Sopenharmony_ci	if (!arch_timer_ppi[arch_timer_uses_ppi]) {
16388c2ecf20Sopenharmony_ci		pr_err("No interrupt available, giving up\n");
16398c2ecf20Sopenharmony_ci		return -EINVAL;
16408c2ecf20Sopenharmony_ci	}
16418c2ecf20Sopenharmony_ci
16428c2ecf20Sopenharmony_ci	/* Always-on capability */
16438c2ecf20Sopenharmony_ci	arch_timer_c3stop = acpi_gtdt_c3stop(arch_timer_uses_ppi);
16448c2ecf20Sopenharmony_ci
16458c2ecf20Sopenharmony_ci	/* Check for globally applicable workarounds */
16468c2ecf20Sopenharmony_ci	arch_timer_check_ool_workaround(ate_match_acpi_oem_info, table);
16478c2ecf20Sopenharmony_ci
16488c2ecf20Sopenharmony_ci	ret = arch_timer_register();
16498c2ecf20Sopenharmony_ci	if (ret)
16508c2ecf20Sopenharmony_ci		return ret;
16518c2ecf20Sopenharmony_ci
16528c2ecf20Sopenharmony_ci	if (platform_timer_count &&
16538c2ecf20Sopenharmony_ci	    arch_timer_mem_acpi_init(platform_timer_count))
16548c2ecf20Sopenharmony_ci		pr_err("Failed to initialize memory-mapped timer.\n");
16558c2ecf20Sopenharmony_ci
16568c2ecf20Sopenharmony_ci	return arch_timer_common_init();
16578c2ecf20Sopenharmony_ci}
16588c2ecf20Sopenharmony_ciTIMER_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
16598c2ecf20Sopenharmony_ci#endif
1660