xref: /DragonOS/kernel/src/arch/x86_64/time.rs (revision 23ef2b33d1e3cfd2506eb7449a33df4ec42f11d3)
1b8ed3825SDonkey Kane use crate::time::{clocksource::HZ, TimeArch};
2fbe6becdSLoGin 
38cb2e9b3SLoGin use super::driver::tsc::TSCManager;
48cb2e9b3SLoGin 
5b8ed3825SDonkey Kane /// 这个是系统jiffies时钟源的固有频率(不是调频之后的)
6b8ed3825SDonkey Kane pub const CLOCK_TICK_RATE: u32 = HZ as u32 * 1000000;
7b8ed3825SDonkey Kane 
time_init()8*23ef2b33SLoGin pub fn time_init() {
9*23ef2b33SLoGin     // do nothing
10*23ef2b33SLoGin }
11*23ef2b33SLoGin 
12fbe6becdSLoGin pub struct X86_64TimeArch;
13fbe6becdSLoGin 
14fbe6becdSLoGin impl TimeArch for X86_64TimeArch {
15b8ed3825SDonkey Kane     #[inline(always)]
get_cycles() -> usize16fbe6becdSLoGin     fn get_cycles() -> usize {
17fbe6becdSLoGin         unsafe { x86::time::rdtsc() as usize }
18fbe6becdSLoGin     }
198cb2e9b3SLoGin 
cal_expire_cycles(ns: usize) -> usize208cb2e9b3SLoGin     fn cal_expire_cycles(ns: usize) -> usize {
218cb2e9b3SLoGin         Self::get_cycles() + ns * TSCManager::cpu_khz() as usize / 1000000
228cb2e9b3SLoGin     }
23b8ed3825SDonkey Kane 
24b8ed3825SDonkey Kane     /// 将CPU的时钟周期数转换为纳秒
25b8ed3825SDonkey Kane     #[inline(always)]
cycles2ns(cycles: usize) -> usize26b8ed3825SDonkey Kane     fn cycles2ns(cycles: usize) -> usize {
27b8ed3825SDonkey Kane         cycles * 1000000 / TSCManager::cpu_khz() as usize
28b8ed3825SDonkey Kane     }
29fbe6becdSLoGin }
30