xref: /linux/arch/arm/include/asm/arch_timer.h (revision 1e8d929231cf7b397101c5e6aaaa3d9bc9832f10)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2022c03a2SMarc Zyngier #ifndef __ASMARM_ARCH_TIMER_H
3022c03a2SMarc Zyngier #define __ASMARM_ARCH_TIMER_H
4022c03a2SMarc Zyngier 
5ec944c93SMark Rutland #include <asm/barrier.h>
6923df96bSWill Deacon #include <asm/errno.h>
75a354412SAndrew Murray #include <asm/hwcap.h>
8a1b2dde7SMarc Zyngier #include <linux/clocksource.h>
98a4da6e3SMark Rutland #include <linux/init.h>
10ec944c93SMark Rutland #include <linux/types.h>
11923df96bSWill Deacon 
128a4da6e3SMark Rutland #include <clocksource/arm_arch_timer.h>
138a4da6e3SMark Rutland 
14022c03a2SMarc Zyngier #ifdef CONFIG_ARM_ARCH_TIMER
155ef19a16SMarc Zyngier /* 32bit ARM doesn't know anything about timer errata... */
165ef19a16SMarc Zyngier #define has_erratum_handler(h)		(false)
175ef19a16SMarc Zyngier #define erratum_handler(h)		(arch_timer_##h)
185ef19a16SMarc Zyngier 
190583fe47SRob Herring int arch_timer_arch_init(void);
20ec944c93SMark Rutland 
21ec944c93SMark Rutland /*
22ec944c93SMark Rutland  * These register accessors are marked inline so the compiler can
23ec944c93SMark Rutland  * nicely work out which register we want, and chuck away the rest of
24ec944c93SMark Rutland  * the code. At least it does so with a recent GCC (4.6.3).
25ec944c93SMark Rutland  */
26e09f3cc0SStephen Boyd static __always_inline
27*1e8d9292SMarc Zyngier void arch_timer_reg_write_cp15(int access, enum arch_timer_reg reg, u64 val)
28ec944c93SMark Rutland {
29ec944c93SMark Rutland 	if (access == ARCH_TIMER_PHYS_ACCESS) {
30ec944c93SMark Rutland 		switch (reg) {
31ec944c93SMark Rutland 		case ARCH_TIMER_REG_CTRL:
32*1e8d9292SMarc Zyngier 			asm volatile("mcr p15, 0, %0, c14, c2, 1" : : "r" ((u32)val));
33ec944c93SMark Rutland 			break;
34ec944c93SMark Rutland 		case ARCH_TIMER_REG_TVAL:
35*1e8d9292SMarc Zyngier 			asm volatile("mcr p15, 0, %0, c14, c2, 0" : : "r" ((u32)val));
36ec944c93SMark Rutland 			break;
374775bc63SMarc Zyngier 		default:
384775bc63SMarc Zyngier 			BUILD_BUG();
39ec944c93SMark Rutland 		}
40e09f3cc0SStephen Boyd 	} else if (access == ARCH_TIMER_VIRT_ACCESS) {
41ec944c93SMark Rutland 		switch (reg) {
42ec944c93SMark Rutland 		case ARCH_TIMER_REG_CTRL:
43*1e8d9292SMarc Zyngier 			asm volatile("mcr p15, 0, %0, c14, c3, 1" : : "r" ((u32)val));
44ec944c93SMark Rutland 			break;
45ec944c93SMark Rutland 		case ARCH_TIMER_REG_TVAL:
46*1e8d9292SMarc Zyngier 			asm volatile("mcr p15, 0, %0, c14, c3, 0" : : "r" ((u32)val));
47ec944c93SMark Rutland 			break;
484775bc63SMarc Zyngier 		default:
494775bc63SMarc Zyngier 			BUILD_BUG();
50ec944c93SMark Rutland 		}
514775bc63SMarc Zyngier 	} else {
524775bc63SMarc Zyngier 		BUILD_BUG();
53ec944c93SMark Rutland 	}
5445801042SMark Rutland 
5545801042SMark Rutland 	isb();
56ec944c93SMark Rutland }
57ec944c93SMark Rutland 
58e09f3cc0SStephen Boyd static __always_inline
5960faddf6SStephen Boyd u32 arch_timer_reg_read_cp15(int access, enum arch_timer_reg reg)
60ec944c93SMark Rutland {
61ec944c93SMark Rutland 	u32 val = 0;
62ec944c93SMark Rutland 
63ec944c93SMark Rutland 	if (access == ARCH_TIMER_PHYS_ACCESS) {
64ec944c93SMark Rutland 		switch (reg) {
65ec944c93SMark Rutland 		case ARCH_TIMER_REG_CTRL:
66ec944c93SMark Rutland 			asm volatile("mrc p15, 0, %0, c14, c2, 1" : "=r" (val));
67ec944c93SMark Rutland 			break;
684775bc63SMarc Zyngier 		default:
694775bc63SMarc Zyngier 			BUILD_BUG();
70ec944c93SMark Rutland 		}
71e09f3cc0SStephen Boyd 	} else if (access == ARCH_TIMER_VIRT_ACCESS) {
72ec944c93SMark Rutland 		switch (reg) {
73ec944c93SMark Rutland 		case ARCH_TIMER_REG_CTRL:
74ec944c93SMark Rutland 			asm volatile("mrc p15, 0, %0, c14, c3, 1" : "=r" (val));
75ec944c93SMark Rutland 			break;
764775bc63SMarc Zyngier 		default:
774775bc63SMarc Zyngier 			BUILD_BUG();
78ec944c93SMark Rutland 		}
794775bc63SMarc Zyngier 	} else {
804775bc63SMarc Zyngier 		BUILD_BUG();
81ec944c93SMark Rutland 	}
82ec944c93SMark Rutland 
83ec944c93SMark Rutland 	return val;
84ec944c93SMark Rutland }
85ec944c93SMark Rutland 
86ec944c93SMark Rutland static inline u32 arch_timer_get_cntfrq(void)
87ec944c93SMark Rutland {
88ec944c93SMark Rutland 	u32 val;
89ec944c93SMark Rutland 	asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (val));
90ec944c93SMark Rutland 	return val;
91ec944c93SMark Rutland }
92ec944c93SMark Rutland 
930ea41539SMarc Zyngier static inline u64 __arch_counter_get_cntpct(void)
940b46b8a7SSonny Rao {
950b46b8a7SSonny Rao 	u64 cval;
960b46b8a7SSonny Rao 
970b46b8a7SSonny Rao 	isb();
980b46b8a7SSonny Rao 	asm volatile("mrrc p15, 0, %Q0, %R0, c14" : "=r" (cval));
990b46b8a7SSonny Rao 	return cval;
1000b46b8a7SSonny Rao }
1010b46b8a7SSonny Rao 
1020ea41539SMarc Zyngier static inline u64 __arch_counter_get_cntpct_stable(void)
1030ea41539SMarc Zyngier {
1040ea41539SMarc Zyngier 	return __arch_counter_get_cntpct();
1050ea41539SMarc Zyngier }
1060ea41539SMarc Zyngier 
1070ea41539SMarc Zyngier static inline u64 __arch_counter_get_cntvct(void)
108ec944c93SMark Rutland {
109ec944c93SMark Rutland 	u64 cval;
110ec944c93SMark Rutland 
11145801042SMark Rutland 	isb();
112ec944c93SMark Rutland 	asm volatile("mrrc p15, 1, %Q0, %R0, c14" : "=r" (cval));
113ec944c93SMark Rutland 	return cval;
114ec944c93SMark Rutland }
115b2deabe3SMark Rutland 
1160ea41539SMarc Zyngier static inline u64 __arch_counter_get_cntvct_stable(void)
1170ea41539SMarc Zyngier {
1180ea41539SMarc Zyngier 	return __arch_counter_get_cntvct();
1190ea41539SMarc Zyngier }
1200ea41539SMarc Zyngier 
121e9faebc6SSudeep KarkadaNagesha static inline u32 arch_timer_get_cntkctl(void)
122b2deabe3SMark Rutland {
123b2deabe3SMark Rutland 	u32 cntkctl;
124b2deabe3SMark Rutland 	asm volatile("mrc p15, 0, %0, c14, c1, 0" : "=r" (cntkctl));
125e9faebc6SSudeep KarkadaNagesha 	return cntkctl;
126e9faebc6SSudeep KarkadaNagesha }
127e9faebc6SSudeep KarkadaNagesha 
128e9faebc6SSudeep KarkadaNagesha static inline void arch_timer_set_cntkctl(u32 cntkctl)
129e9faebc6SSudeep KarkadaNagesha {
130e9faebc6SSudeep KarkadaNagesha 	asm volatile("mcr p15, 0, %0, c14, c1, 0" : : "r" (cntkctl));
131ec5c8e42SJulien Thierry 	isb();
132e9faebc6SSudeep KarkadaNagesha }
133e9faebc6SSudeep KarkadaNagesha 
1345a354412SAndrew Murray static inline void arch_timer_set_evtstrm_feature(void)
1355a354412SAndrew Murray {
1365a354412SAndrew Murray 	elf_hwcap |= HWCAP_EVTSTRM;
1375a354412SAndrew Murray }
1385a354412SAndrew Murray 
1395a354412SAndrew Murray static inline bool arch_timer_have_evtstrm_feature(void)
1405a354412SAndrew Murray {
1415a354412SAndrew Murray 	return elf_hwcap & HWCAP_EVTSTRM;
1425a354412SAndrew Murray }
143022c03a2SMarc Zyngier #endif
144022c03a2SMarc Zyngier 
145022c03a2SMarc Zyngier #endif
146