18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * arch/arm64/include/asm/arch_timer.h 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2012 ARM Ltd. 68c2ecf20Sopenharmony_ci * Author: Marc Zyngier <marc.zyngier@arm.com> 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci#ifndef __ASM_ARCH_TIMER_H 98c2ecf20Sopenharmony_ci#define __ASM_ARCH_TIMER_H 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <asm/barrier.h> 128c2ecf20Sopenharmony_ci#include <asm/hwcap.h> 138c2ecf20Sopenharmony_ci#include <asm/sysreg.h> 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#include <linux/bug.h> 168c2ecf20Sopenharmony_ci#include <linux/init.h> 178c2ecf20Sopenharmony_ci#include <linux/jump_label.h> 188c2ecf20Sopenharmony_ci#include <linux/smp.h> 198c2ecf20Sopenharmony_ci#include <linux/types.h> 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci#include <clocksource/arm_arch_timer.h> 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND) 248c2ecf20Sopenharmony_ci#define has_erratum_handler(h) \ 258c2ecf20Sopenharmony_ci ({ \ 268c2ecf20Sopenharmony_ci const struct arch_timer_erratum_workaround *__wa; \ 278c2ecf20Sopenharmony_ci __wa = __this_cpu_read(timer_unstable_counter_workaround); \ 288c2ecf20Sopenharmony_ci (__wa && __wa->h); \ 298c2ecf20Sopenharmony_ci }) 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#define erratum_handler(h) \ 328c2ecf20Sopenharmony_ci ({ \ 338c2ecf20Sopenharmony_ci const struct arch_timer_erratum_workaround *__wa; \ 348c2ecf20Sopenharmony_ci __wa = __this_cpu_read(timer_unstable_counter_workaround); \ 358c2ecf20Sopenharmony_ci (__wa && __wa->h) ? __wa->h : arch_timer_##h; \ 368c2ecf20Sopenharmony_ci }) 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci#else 398c2ecf20Sopenharmony_ci#define has_erratum_handler(h) false 408c2ecf20Sopenharmony_ci#define erratum_handler(h) (arch_timer_##h) 418c2ecf20Sopenharmony_ci#endif 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_cienum arch_timer_erratum_match_type { 448c2ecf20Sopenharmony_ci ate_match_dt, 458c2ecf20Sopenharmony_ci ate_match_local_cap_id, 468c2ecf20Sopenharmony_ci ate_match_acpi_oem_info, 478c2ecf20Sopenharmony_ci}; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_cistruct clock_event_device; 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_cistruct arch_timer_erratum_workaround { 528c2ecf20Sopenharmony_ci enum arch_timer_erratum_match_type match_type; 538c2ecf20Sopenharmony_ci const void *id; 548c2ecf20Sopenharmony_ci const char *desc; 558c2ecf20Sopenharmony_ci u32 (*read_cntp_tval_el0)(void); 568c2ecf20Sopenharmony_ci u32 (*read_cntv_tval_el0)(void); 578c2ecf20Sopenharmony_ci u64 (*read_cntpct_el0)(void); 588c2ecf20Sopenharmony_ci u64 (*read_cntvct_el0)(void); 598c2ecf20Sopenharmony_ci int (*set_next_event_phys)(unsigned long, struct clock_event_device *); 608c2ecf20Sopenharmony_ci int (*set_next_event_virt)(unsigned long, struct clock_event_device *); 618c2ecf20Sopenharmony_ci bool disable_compat_vdso; 628c2ecf20Sopenharmony_ci}; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ciDECLARE_PER_CPU(const struct arch_timer_erratum_workaround *, 658c2ecf20Sopenharmony_ci timer_unstable_counter_workaround); 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci/* inline sysreg accessors that make erratum_handler() work */ 688c2ecf20Sopenharmony_cistatic inline notrace u32 arch_timer_read_cntp_tval_el0(void) 698c2ecf20Sopenharmony_ci{ 708c2ecf20Sopenharmony_ci return read_sysreg(cntp_tval_el0); 718c2ecf20Sopenharmony_ci} 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_cistatic inline notrace u32 arch_timer_read_cntv_tval_el0(void) 748c2ecf20Sopenharmony_ci{ 758c2ecf20Sopenharmony_ci return read_sysreg(cntv_tval_el0); 768c2ecf20Sopenharmony_ci} 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_cistatic inline notrace u64 arch_timer_read_cntpct_el0(void) 798c2ecf20Sopenharmony_ci{ 808c2ecf20Sopenharmony_ci return read_sysreg(cntpct_el0); 818c2ecf20Sopenharmony_ci} 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_cistatic inline notrace u64 arch_timer_read_cntvct_el0(void) 848c2ecf20Sopenharmony_ci{ 858c2ecf20Sopenharmony_ci return read_sysreg(cntvct_el0); 868c2ecf20Sopenharmony_ci} 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci#define arch_timer_reg_read_stable(reg) \ 898c2ecf20Sopenharmony_ci ({ \ 908c2ecf20Sopenharmony_ci u64 _val; \ 918c2ecf20Sopenharmony_ci \ 928c2ecf20Sopenharmony_ci preempt_disable_notrace(); \ 938c2ecf20Sopenharmony_ci _val = erratum_handler(read_ ## reg)(); \ 948c2ecf20Sopenharmony_ci preempt_enable_notrace(); \ 958c2ecf20Sopenharmony_ci \ 968c2ecf20Sopenharmony_ci _val; \ 978c2ecf20Sopenharmony_ci }) 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci/* 1008c2ecf20Sopenharmony_ci * These register accessors are marked inline so the compiler can 1018c2ecf20Sopenharmony_ci * nicely work out which register we want, and chuck away the rest of 1028c2ecf20Sopenharmony_ci * the code. 1038c2ecf20Sopenharmony_ci */ 1048c2ecf20Sopenharmony_cistatic __always_inline 1058c2ecf20Sopenharmony_civoid arch_timer_reg_write_cp15(int access, enum arch_timer_reg reg, u32 val) 1068c2ecf20Sopenharmony_ci{ 1078c2ecf20Sopenharmony_ci if (access == ARCH_TIMER_PHYS_ACCESS) { 1088c2ecf20Sopenharmony_ci switch (reg) { 1098c2ecf20Sopenharmony_ci case ARCH_TIMER_REG_CTRL: 1108c2ecf20Sopenharmony_ci write_sysreg(val, cntp_ctl_el0); 1118c2ecf20Sopenharmony_ci break; 1128c2ecf20Sopenharmony_ci case ARCH_TIMER_REG_TVAL: 1138c2ecf20Sopenharmony_ci write_sysreg(val, cntp_tval_el0); 1148c2ecf20Sopenharmony_ci break; 1158c2ecf20Sopenharmony_ci } 1168c2ecf20Sopenharmony_ci } else if (access == ARCH_TIMER_VIRT_ACCESS) { 1178c2ecf20Sopenharmony_ci switch (reg) { 1188c2ecf20Sopenharmony_ci case ARCH_TIMER_REG_CTRL: 1198c2ecf20Sopenharmony_ci write_sysreg(val, cntv_ctl_el0); 1208c2ecf20Sopenharmony_ci break; 1218c2ecf20Sopenharmony_ci case ARCH_TIMER_REG_TVAL: 1228c2ecf20Sopenharmony_ci write_sysreg(val, cntv_tval_el0); 1238c2ecf20Sopenharmony_ci break; 1248c2ecf20Sopenharmony_ci } 1258c2ecf20Sopenharmony_ci } 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci isb(); 1288c2ecf20Sopenharmony_ci} 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_cistatic __always_inline 1318c2ecf20Sopenharmony_ciu32 arch_timer_reg_read_cp15(int access, enum arch_timer_reg reg) 1328c2ecf20Sopenharmony_ci{ 1338c2ecf20Sopenharmony_ci if (access == ARCH_TIMER_PHYS_ACCESS) { 1348c2ecf20Sopenharmony_ci switch (reg) { 1358c2ecf20Sopenharmony_ci case ARCH_TIMER_REG_CTRL: 1368c2ecf20Sopenharmony_ci return read_sysreg(cntp_ctl_el0); 1378c2ecf20Sopenharmony_ci case ARCH_TIMER_REG_TVAL: 1388c2ecf20Sopenharmony_ci return arch_timer_reg_read_stable(cntp_tval_el0); 1398c2ecf20Sopenharmony_ci } 1408c2ecf20Sopenharmony_ci } else if (access == ARCH_TIMER_VIRT_ACCESS) { 1418c2ecf20Sopenharmony_ci switch (reg) { 1428c2ecf20Sopenharmony_ci case ARCH_TIMER_REG_CTRL: 1438c2ecf20Sopenharmony_ci return read_sysreg(cntv_ctl_el0); 1448c2ecf20Sopenharmony_ci case ARCH_TIMER_REG_TVAL: 1458c2ecf20Sopenharmony_ci return arch_timer_reg_read_stable(cntv_tval_el0); 1468c2ecf20Sopenharmony_ci } 1478c2ecf20Sopenharmony_ci } 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci BUG(); 1508c2ecf20Sopenharmony_ci} 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_cistatic inline u32 arch_timer_get_cntfrq(void) 1538c2ecf20Sopenharmony_ci{ 1548c2ecf20Sopenharmony_ci return read_sysreg(cntfrq_el0); 1558c2ecf20Sopenharmony_ci} 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_cistatic inline u32 arch_timer_get_cntkctl(void) 1588c2ecf20Sopenharmony_ci{ 1598c2ecf20Sopenharmony_ci return read_sysreg(cntkctl_el1); 1608c2ecf20Sopenharmony_ci} 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_cistatic inline void arch_timer_set_cntkctl(u32 cntkctl) 1638c2ecf20Sopenharmony_ci{ 1648c2ecf20Sopenharmony_ci write_sysreg(cntkctl, cntkctl_el1); 1658c2ecf20Sopenharmony_ci isb(); 1668c2ecf20Sopenharmony_ci} 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_cistatic __always_inline u64 __arch_counter_get_cntpct_stable(void) 1698c2ecf20Sopenharmony_ci{ 1708c2ecf20Sopenharmony_ci u64 cnt; 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci isb(); 1738c2ecf20Sopenharmony_ci cnt = arch_timer_reg_read_stable(cntpct_el0); 1748c2ecf20Sopenharmony_ci arch_counter_enforce_ordering(cnt); 1758c2ecf20Sopenharmony_ci return cnt; 1768c2ecf20Sopenharmony_ci} 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_cistatic __always_inline u64 __arch_counter_get_cntpct(void) 1798c2ecf20Sopenharmony_ci{ 1808c2ecf20Sopenharmony_ci u64 cnt; 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci isb(); 1838c2ecf20Sopenharmony_ci cnt = read_sysreg(cntpct_el0); 1848c2ecf20Sopenharmony_ci arch_counter_enforce_ordering(cnt); 1858c2ecf20Sopenharmony_ci return cnt; 1868c2ecf20Sopenharmony_ci} 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_cistatic __always_inline u64 __arch_counter_get_cntvct_stable(void) 1898c2ecf20Sopenharmony_ci{ 1908c2ecf20Sopenharmony_ci u64 cnt; 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci isb(); 1938c2ecf20Sopenharmony_ci cnt = arch_timer_reg_read_stable(cntvct_el0); 1948c2ecf20Sopenharmony_ci arch_counter_enforce_ordering(cnt); 1958c2ecf20Sopenharmony_ci return cnt; 1968c2ecf20Sopenharmony_ci} 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_cistatic __always_inline u64 __arch_counter_get_cntvct(void) 1998c2ecf20Sopenharmony_ci{ 2008c2ecf20Sopenharmony_ci u64 cnt; 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci isb(); 2038c2ecf20Sopenharmony_ci cnt = read_sysreg(cntvct_el0); 2048c2ecf20Sopenharmony_ci arch_counter_enforce_ordering(cnt); 2058c2ecf20Sopenharmony_ci return cnt; 2068c2ecf20Sopenharmony_ci} 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_cistatic inline int arch_timer_arch_init(void) 2098c2ecf20Sopenharmony_ci{ 2108c2ecf20Sopenharmony_ci return 0; 2118c2ecf20Sopenharmony_ci} 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_cistatic inline void arch_timer_set_evtstrm_feature(void) 2148c2ecf20Sopenharmony_ci{ 2158c2ecf20Sopenharmony_ci cpu_set_named_feature(EVTSTRM); 2168c2ecf20Sopenharmony_ci#ifdef CONFIG_COMPAT 2178c2ecf20Sopenharmony_ci compat_elf_hwcap |= COMPAT_HWCAP_EVTSTRM; 2188c2ecf20Sopenharmony_ci#endif 2198c2ecf20Sopenharmony_ci} 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_cistatic inline bool arch_timer_have_evtstrm_feature(void) 2228c2ecf20Sopenharmony_ci{ 2238c2ecf20Sopenharmony_ci return cpu_have_named_feature(EVTSTRM); 2248c2ecf20Sopenharmony_ci} 2258c2ecf20Sopenharmony_ci#endif 226