Skip to content

Commit b3e37ad

Browse files
committed
handle the counter frequency in khz
Hereby, the usage of u128 can be prevented
1 parent 60e7106 commit b3e37ad

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

src/arch/aarch64/kernel/processor.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,27 @@ impl fmt::Display for CpuFrequencySources {
3535
}
3636

3737
struct CpuFrequency {
38-
hz: u32,
38+
khz: u32,
3939
source: CpuFrequencySources,
4040
}
4141

4242
impl CpuFrequency {
4343
const fn new() -> Self {
4444
CpuFrequency {
45-
hz: 0,
45+
khz: 0,
4646
source: CpuFrequencySources::Invalid,
4747
}
4848
}
4949

5050
fn set_detected_cpu_frequency(
5151
&mut self,
52-
hz: u32,
52+
khz: u32,
5353
source: CpuFrequencySources,
5454
) -> Result<(), ()> {
5555
//The clock frequency must never be set to zero, otherwise a division by zero will
5656
//occur during runtime
57-
if hz > 0 {
58-
self.hz = hz;
57+
if khz > 0 {
58+
self.khz = khz;
5959
self.source = source;
6060
Ok(())
6161
} else {
@@ -65,15 +65,12 @@ impl CpuFrequency {
6565

6666
unsafe fn detect_from_cmdline(&mut self) -> Result<(), ()> {
6767
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)
7269
}
7370

7471
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)
7774
}
7875

7976
unsafe fn detect(&mut self) {
@@ -85,13 +82,13 @@ impl CpuFrequency {
8582
}
8683

8784
fn get(&self) -> u32 {
88-
self.hz
85+
self.khz
8986
}
9087
}
9188

9289
impl fmt::Display for CpuFrequency {
9390
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)
9592
}
9693
}
9794

@@ -137,13 +134,12 @@ pub fn shutdown(error_code: i32) -> ! {
137134
pub fn get_timer_ticks() -> u64 {
138135
// We simulate a timer with a 1 microsecond resolution by taking the CPU timestamp
139136
// 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())
142138
}
143139

144140
#[inline]
145141
pub fn get_frequency() -> u16 {
146-
(CPU_FREQUENCY.get() / 1_000_000).try_into().unwrap()
142+
(CPU_FREQUENCY.get() / 1_000).try_into().unwrap()
147143
}
148144

149145
#[inline]
@@ -221,8 +217,7 @@ pub fn detect_frequency() {
221217
fn __set_oneshot_timer(wakeup_time: Option<u64>) {
222218
if let Some(wt) = wakeup_time {
223219
// 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;
226221

227222
unsafe {
228223
asm!(

0 commit comments

Comments
 (0)