xref: /openbmc/linux/arch/arm64/include/asm/arch_timer.h (revision 651bb2e9dca6e6dbad3fba5f6e6086a23575b8b5)
11aee5d7aSMark Rutland /*
21aee5d7aSMark Rutland  * arch/arm64/include/asm/arch_timer.h
31aee5d7aSMark Rutland  *
41aee5d7aSMark Rutland  * Copyright (C) 2012 ARM Ltd.
51aee5d7aSMark Rutland  * Author: Marc Zyngier <marc.zyngier@arm.com>
61aee5d7aSMark Rutland  *
71aee5d7aSMark Rutland  * This program is free software: you can redistribute it and/or modify
81aee5d7aSMark Rutland  * it under the terms of the GNU General Public License version 2 as
91aee5d7aSMark Rutland  * published by the Free Software Foundation.
101aee5d7aSMark Rutland  *
111aee5d7aSMark Rutland  * This program is distributed in the hope that it will be useful,
121aee5d7aSMark Rutland  * but WITHOUT ANY WARRANTY; without even the implied warranty of
131aee5d7aSMark Rutland  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
141aee5d7aSMark Rutland  * GNU General Public License for more details.
151aee5d7aSMark Rutland  *
161aee5d7aSMark Rutland  * You should have received a copy of the GNU General Public License
171aee5d7aSMark Rutland  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
181aee5d7aSMark Rutland  */
191aee5d7aSMark Rutland #ifndef __ASM_ARCH_TIMER_H
201aee5d7aSMark Rutland #define __ASM_ARCH_TIMER_H
211aee5d7aSMark Rutland 
221aee5d7aSMark Rutland #include <asm/barrier.h>
23cd5f22d7SMark Rutland #include <asm/sysreg.h>
241aee5d7aSMark Rutland 
25082471a8SPaul Walmsley #include <linux/bug.h>
261aee5d7aSMark Rutland #include <linux/init.h>
27f6dc1576SScott Wood #include <linux/jump_label.h>
281aee5d7aSMark Rutland #include <linux/types.h>
291aee5d7aSMark Rutland 
301aee5d7aSMark Rutland #include <clocksource/arm_arch_timer.h>
311aee5d7aSMark Rutland 
3216d10ef2SDing Tianhong #if IS_ENABLED(CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND)
33f6dc1576SScott Wood extern struct static_key_false arch_timer_read_ool_enabled;
3416d10ef2SDing Tianhong #define needs_unstable_timer_counter_workaround() \
35f6dc1576SScott Wood 	static_branch_unlikely(&arch_timer_read_ool_enabled)
36f6dc1576SScott Wood #else
3716d10ef2SDing Tianhong #define needs_unstable_timer_counter_workaround()  false
38f6dc1576SScott Wood #endif
39f6dc1576SScott Wood 
40*651bb2e9SMarc Zyngier enum arch_timer_erratum_match_type {
41*651bb2e9SMarc Zyngier 	ate_match_dt,
42*651bb2e9SMarc Zyngier };
43f6dc1576SScott Wood 
4416d10ef2SDing Tianhong struct arch_timer_erratum_workaround {
45*651bb2e9SMarc Zyngier 	enum arch_timer_erratum_match_type match_type;
46*651bb2e9SMarc Zyngier 	const void *id;
47*651bb2e9SMarc Zyngier 	const char *desc;
4816d10ef2SDing Tianhong 	u32 (*read_cntp_tval_el0)(void);
4916d10ef2SDing Tianhong 	u32 (*read_cntv_tval_el0)(void);
5016d10ef2SDing Tianhong 	u64 (*read_cntvct_el0)(void);
5116d10ef2SDing Tianhong };
5216d10ef2SDing Tianhong 
5316d10ef2SDing Tianhong extern const struct arch_timer_erratum_workaround *timer_unstable_counter_workaround;
54f6dc1576SScott Wood 
55f6dc1576SScott Wood #define arch_timer_reg_read_stable(reg) 		\
56f6dc1576SScott Wood ({							\
57f6dc1576SScott Wood 	u64 _val;					\
5816d10ef2SDing Tianhong 	if (needs_unstable_timer_counter_workaround())		\
5916d10ef2SDing Tianhong 		_val = timer_unstable_counter_workaround->read_##reg();\
60f6dc1576SScott Wood 	else						\
61f6dc1576SScott Wood 		_val = read_sysreg(reg);		\
62f6dc1576SScott Wood 	_val;						\
63f6dc1576SScott Wood })
64f6dc1576SScott Wood 
65e09f3cc0SStephen Boyd /*
66e09f3cc0SStephen Boyd  * These register accessors are marked inline so the compiler can
67e09f3cc0SStephen Boyd  * nicely work out which register we want, and chuck away the rest of
68e09f3cc0SStephen Boyd  * the code.
69e09f3cc0SStephen Boyd  */
70e09f3cc0SStephen Boyd static __always_inline
7160faddf6SStephen Boyd void arch_timer_reg_write_cp15(int access, enum arch_timer_reg reg, u32 val)
721aee5d7aSMark Rutland {
731aee5d7aSMark Rutland 	if (access == ARCH_TIMER_PHYS_ACCESS) {
741aee5d7aSMark Rutland 		switch (reg) {
751aee5d7aSMark Rutland 		case ARCH_TIMER_REG_CTRL:
76cd5f22d7SMark Rutland 			write_sysreg(val, cntp_ctl_el0);
771aee5d7aSMark Rutland 			break;
781aee5d7aSMark Rutland 		case ARCH_TIMER_REG_TVAL:
79cd5f22d7SMark Rutland 			write_sysreg(val, cntp_tval_el0);
801aee5d7aSMark Rutland 			break;
811aee5d7aSMark Rutland 		}
821aee5d7aSMark Rutland 	} else if (access == ARCH_TIMER_VIRT_ACCESS) {
831aee5d7aSMark Rutland 		switch (reg) {
841aee5d7aSMark Rutland 		case ARCH_TIMER_REG_CTRL:
85cd5f22d7SMark Rutland 			write_sysreg(val, cntv_ctl_el0);
861aee5d7aSMark Rutland 			break;
871aee5d7aSMark Rutland 		case ARCH_TIMER_REG_TVAL:
88cd5f22d7SMark Rutland 			write_sysreg(val, cntv_tval_el0);
891aee5d7aSMark Rutland 			break;
901aee5d7aSMark Rutland 		}
911aee5d7aSMark Rutland 	}
921aee5d7aSMark Rutland 
931aee5d7aSMark Rutland 	isb();
941aee5d7aSMark Rutland }
951aee5d7aSMark Rutland 
96e09f3cc0SStephen Boyd static __always_inline
9760faddf6SStephen Boyd u32 arch_timer_reg_read_cp15(int access, enum arch_timer_reg reg)
981aee5d7aSMark Rutland {
991aee5d7aSMark Rutland 	if (access == ARCH_TIMER_PHYS_ACCESS) {
1001aee5d7aSMark Rutland 		switch (reg) {
1011aee5d7aSMark Rutland 		case ARCH_TIMER_REG_CTRL:
102cd5f22d7SMark Rutland 			return read_sysreg(cntp_ctl_el0);
1031aee5d7aSMark Rutland 		case ARCH_TIMER_REG_TVAL:
104f6dc1576SScott Wood 			return arch_timer_reg_read_stable(cntp_tval_el0);
1051aee5d7aSMark Rutland 		}
1061aee5d7aSMark Rutland 	} else if (access == ARCH_TIMER_VIRT_ACCESS) {
1071aee5d7aSMark Rutland 		switch (reg) {
1081aee5d7aSMark Rutland 		case ARCH_TIMER_REG_CTRL:
109cd5f22d7SMark Rutland 			return read_sysreg(cntv_ctl_el0);
1101aee5d7aSMark Rutland 		case ARCH_TIMER_REG_TVAL:
111f6dc1576SScott Wood 			return arch_timer_reg_read_stable(cntv_tval_el0);
1121aee5d7aSMark Rutland 		}
1131aee5d7aSMark Rutland 	}
1141aee5d7aSMark Rutland 
115cd5f22d7SMark Rutland 	BUG();
1161aee5d7aSMark Rutland }
1171aee5d7aSMark Rutland 
1181aee5d7aSMark Rutland static inline u32 arch_timer_get_cntfrq(void)
1191aee5d7aSMark Rutland {
120cd5f22d7SMark Rutland 	return read_sysreg(cntfrq_el0);
1211aee5d7aSMark Rutland }
1221aee5d7aSMark Rutland 
12346efe547SSudeep KarkadaNagesha static inline u32 arch_timer_get_cntkctl(void)
1241aee5d7aSMark Rutland {
125cd5f22d7SMark Rutland 	return read_sysreg(cntkctl_el1);
12646efe547SSudeep KarkadaNagesha }
12746efe547SSudeep KarkadaNagesha 
12846efe547SSudeep KarkadaNagesha static inline void arch_timer_set_cntkctl(u32 cntkctl)
12946efe547SSudeep KarkadaNagesha {
130cd5f22d7SMark Rutland 	write_sysreg(cntkctl, cntkctl_el1);
13146efe547SSudeep KarkadaNagesha }
13246efe547SSudeep KarkadaNagesha 
1330b46b8a7SSonny Rao static inline u64 arch_counter_get_cntpct(void)
1340b46b8a7SSonny Rao {
1350b46b8a7SSonny Rao 	/*
1360b46b8a7SSonny Rao 	 * AArch64 kernel and user space mandate the use of CNTVCT.
1370b46b8a7SSonny Rao 	 */
1380b46b8a7SSonny Rao 	BUG();
1390b46b8a7SSonny Rao 	return 0;
1400b46b8a7SSonny Rao }
1410b46b8a7SSonny Rao 
1421aee5d7aSMark Rutland static inline u64 arch_counter_get_cntvct(void)
1431aee5d7aSMark Rutland {
1441aee5d7aSMark Rutland 	isb();
145f6dc1576SScott Wood 	return arch_timer_reg_read_stable(cntvct_el0);
1461aee5d7aSMark Rutland }
1471aee5d7aSMark Rutland 
1480583fe47SRob Herring static inline int arch_timer_arch_init(void)
1490583fe47SRob Herring {
1500583fe47SRob Herring 	return 0;
1510583fe47SRob Herring }
1520583fe47SRob Herring 
1531aee5d7aSMark Rutland #endif
154