1 /* 2 * Copyright 2019 The Hafnium Authors. 3 * 4 * Use of this source code is governed by a BSD-style 5 * license that can be found in the LICENSE file or at 6 * https://opensource.org/licenses/BSD-3-Clause. 7 */ 8 9 #pragma once 10 11 #include <stdint.h> 12 13 #include "../msr.h" 14 #include "../sysregs.h" 15 16 #define NANOS_PER_UNIT 1000000000 17 18 /* Generic timer control interface. */ 19 #define CNTx_CTL_ENABLE_MASK (UINT32_C(1) << 0) 20 #define CNTx_CTL_IMASK_MASK (UINT32_C(1) << 1) 21 #define CNTx_CTL_ISTS_MASK (UINT32_C(1) << 2) 22 timer_set(uint32_t ticks)23static inline void timer_set(uint32_t ticks) 24 { 25 has_vhe_support() ? write_msr(MSR_CNTV_TVAL_EL02, ticks) 26 : write_msr(cntv_tval_el0, ticks); 27 } 28 timer_start(void)29static inline void timer_start(void) 30 { 31 has_vhe_support() ? write_msr(MSR_CNTV_CTL_EL02, 0x00000001) 32 : write_msr(cntv_ctl_el0, 0x00000001); 33 } 34 35 /** 36 * Converts a number of nanoseconds to the equivalent number of timer ticks. 37 */ ns_to_ticks(uint64_t ns)38static inline uint64_t ns_to_ticks(uint64_t ns) 39 { 40 return ns * read_msr(cntfrq_el0) / NANOS_PER_UNIT; 41 } 42