@@ -35,27 +35,27 @@ impl fmt::Display for CpuFrequencySources {
35
35
}
36
36
37
37
struct CpuFrequency {
38
- hz : u32 ,
38
+ khz : u32 ,
39
39
source : CpuFrequencySources ,
40
40
}
41
41
42
42
impl CpuFrequency {
43
43
const fn new ( ) -> Self {
44
44
CpuFrequency {
45
- hz : 0 ,
45
+ khz : 0 ,
46
46
source : CpuFrequencySources :: Invalid ,
47
47
}
48
48
}
49
49
50
50
fn set_detected_cpu_frequency (
51
51
& mut self ,
52
- hz : u32 ,
52
+ khz : u32 ,
53
53
source : CpuFrequencySources ,
54
54
) -> Result < ( ) , ( ) > {
55
55
//The clock frequency must never be set to zero, otherwise a division by zero will
56
56
//occur during runtime
57
- if hz > 0 {
58
- self . hz = hz ;
57
+ if khz > 0 {
58
+ self . khz = khz ;
59
59
self . source = source;
60
60
Ok ( ( ) )
61
61
} else {
@@ -65,15 +65,12 @@ impl CpuFrequency {
65
65
66
66
unsafe fn detect_from_cmdline ( & mut self ) -> Result < ( ) , ( ) > {
67
67
let mhz = env:: freq ( ) . ok_or ( ( ) ) ?;
68
- self . set_detected_cpu_frequency (
69
- u32:: from ( mhz) * 1_000_000 ,
70
- CpuFrequencySources :: CommandLine ,
71
- )
68
+ self . set_detected_cpu_frequency ( u32:: from ( mhz) * 1000 , CpuFrequencySources :: CommandLine )
72
69
}
73
70
74
71
unsafe fn detect_from_register ( & mut self ) -> Result < ( ) , ( ) > {
75
- let hz = CNTFRQ_EL0 . get ( ) & 0xffff_ffff ;
76
- self . set_detected_cpu_frequency ( hz . try_into ( ) . unwrap ( ) , CpuFrequencySources :: Register )
72
+ let khz = ( CNTFRQ_EL0 . get ( ) & 0xffff_ffff ) / 1000 ;
73
+ self . set_detected_cpu_frequency ( khz . try_into ( ) . unwrap ( ) , CpuFrequencySources :: Register )
77
74
}
78
75
79
76
unsafe fn detect ( & mut self ) {
@@ -85,13 +82,13 @@ impl CpuFrequency {
85
82
}
86
83
87
84
fn get ( & self ) -> u32 {
88
- self . hz
85
+ self . khz
89
86
}
90
87
}
91
88
92
89
impl fmt:: Display for CpuFrequency {
93
90
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
94
- write ! ( f, "{} Hz (from {})" , self . hz , self . source)
91
+ write ! ( f, "{} KHz (from {})" , self . khz , self . source)
95
92
}
96
93
}
97
94
@@ -137,13 +134,12 @@ pub fn shutdown(error_code: i32) -> ! {
137
134
pub fn get_timer_ticks ( ) -> u64 {
138
135
// We simulate a timer with a 1 microsecond resolution by taking the CPU timestamp
139
136
// and dividing it by the CPU frequency in MHz.
140
- let ticks = 1_000_000 * u128:: from ( get_timestamp ( ) ) / u128:: from ( CPU_FREQUENCY . get ( ) ) ;
141
- u64:: try_from ( ticks) . unwrap ( )
137
+ get_timestamp ( ) / u64:: from ( get_frequency ( ) )
142
138
}
143
139
144
140
#[ inline]
145
141
pub fn get_frequency ( ) -> u16 {
146
- ( CPU_FREQUENCY . get ( ) / 1_000_000 ) . try_into ( ) . unwrap ( )
142
+ ( CPU_FREQUENCY . get ( ) / 1_000 ) . try_into ( ) . unwrap ( )
147
143
}
148
144
149
145
#[ inline]
@@ -221,8 +217,7 @@ pub fn detect_frequency() {
221
217
fn __set_oneshot_timer ( wakeup_time : Option < u64 > ) {
222
218
if let Some ( wt) = wakeup_time {
223
219
// wt is the absolute wakeup time in microseconds based on processor::get_timer_ticks.
224
- let deadline = u128:: from ( wt) * u128:: from ( CPU_FREQUENCY . get ( ) ) / 1_000_000 ;
225
- let deadline = u64:: try_from ( deadline) . unwrap ( ) ;
220
+ let deadline = ( wt * u64:: from ( get_frequency ( ) ) ) / 1000 ;
226
221
227
222
unsafe {
228
223
asm ! (
0 commit comments