rust/hpet: Drop BqlCell wrapper for num_timers

Now that the num_timers field is initialized as a property, someone may
change its default value using qdev_prop_set_uint8(), but the value is
fixed after the Rust code sees it first.  Since there is no need to modify
it after realize(), it is not to be necessary to have a BqlCell wrapper.

Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
Link: https://lore.kernel.org/r/20250520152750.2542612-4-zhao1.liu@intel.com
[Remove .into() as well. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Zhao Liu 2025-05-26 13:54:21 +02:00 committed by Paolo Bonzini
parent 4d2fec89cb
commit 869b0afa4f

View file

@ -562,7 +562,7 @@ pub struct HPETState {
/// HPET timer array managed by this timer block. /// HPET timer array managed by this timer block.
#[doc(alias = "timer")] #[doc(alias = "timer")]
timers: [BqlRefCell<HPETTimer>; HPET_MAX_TIMERS], timers: [BqlRefCell<HPETTimer>; HPET_MAX_TIMERS],
num_timers: BqlCell<usize>, num_timers: usize,
num_timers_save: BqlCell<u8>, num_timers_save: BqlCell<u8>,
/// Instance id (HPET timer block ID). /// Instance id (HPET timer block ID).
@ -570,11 +570,6 @@ pub struct HPETState {
} }
impl HPETState { impl HPETState {
// Get num_timers with `usize` type, which is useful to play with array index.
fn get_num_timers(&self) -> usize {
self.num_timers.get()
}
const fn has_msi_flag(&self) -> bool { const fn has_msi_flag(&self) -> bool {
self.flags & (1 << HPET_FLAG_MSI_SUPPORT_SHIFT) != 0 self.flags & (1 << HPET_FLAG_MSI_SUPPORT_SHIFT) != 0
} }
@ -636,7 +631,7 @@ impl HPETState {
self.hpet_offset self.hpet_offset
.set(ticks_to_ns(self.counter.get()) - CLOCK_VIRTUAL.get_ns()); .set(ticks_to_ns(self.counter.get()) - CLOCK_VIRTUAL.get_ns());
for timer in self.timers.iter().take(self.get_num_timers()) { for timer in self.timers.iter().take(self.num_timers) {
let mut t = timer.borrow_mut(); let mut t = timer.borrow_mut();
if t.is_int_enabled() && t.is_int_active() { if t.is_int_enabled() && t.is_int_active() {
@ -648,7 +643,7 @@ impl HPETState {
// Halt main counter and disable interrupt generation. // Halt main counter and disable interrupt generation.
self.counter.set(self.get_ticks()); self.counter.set(self.get_ticks());
for timer in self.timers.iter().take(self.get_num_timers()) { for timer in self.timers.iter().take(self.num_timers) {
timer.borrow_mut().del_timer(); timer.borrow_mut().del_timer();
} }
} }
@ -671,7 +666,7 @@ impl HPETState {
let new_val = val << shift; let new_val = val << shift;
let cleared = new_val & self.int_status.get(); let cleared = new_val & self.int_status.get();
for (index, timer) in self.timers.iter().take(self.get_num_timers()).enumerate() { for (index, timer) in self.timers.iter().take(self.num_timers).enumerate() {
if cleared & (1 << index) != 0 { if cleared & (1 << index) != 0 {
timer.borrow_mut().update_irq(false); timer.borrow_mut().update_irq(false);
} }
@ -725,7 +720,7 @@ impl HPETState {
} }
fn realize(&self) -> qemu_api::Result<()> { fn realize(&self) -> qemu_api::Result<()> {
if self.num_timers.get() < HPET_MIN_TIMERS || self.num_timers.get() > HPET_MAX_TIMERS { if self.num_timers < HPET_MIN_TIMERS || self.num_timers > HPET_MAX_TIMERS {
Err(format!( Err(format!(
"hpet.num_timers must be between {HPET_MIN_TIMERS} and {HPET_MAX_TIMERS}" "hpet.num_timers must be between {HPET_MIN_TIMERS} and {HPET_MAX_TIMERS}"
))?; ))?;
@ -743,7 +738,7 @@ impl HPETState {
1 << HPET_CAP_COUNT_SIZE_CAP_SHIFT | 1 << HPET_CAP_COUNT_SIZE_CAP_SHIFT |
1 << HPET_CAP_LEG_RT_CAP_SHIFT | 1 << HPET_CAP_LEG_RT_CAP_SHIFT |
HPET_CAP_VENDER_ID_VALUE << HPET_CAP_VENDER_ID_SHIFT | HPET_CAP_VENDER_ID_VALUE << HPET_CAP_VENDER_ID_SHIFT |
((self.get_num_timers() - 1) as u64) << HPET_CAP_NUM_TIM_SHIFT | // indicate the last timer ((self.num_timers - 1) as u64) << HPET_CAP_NUM_TIM_SHIFT | // indicate the last timer
(HPET_CLK_PERIOD * FS_PER_NS) << HPET_CAP_CNT_CLK_PERIOD_SHIFT, // 10 ns (HPET_CLK_PERIOD * FS_PER_NS) << HPET_CAP_CNT_CLK_PERIOD_SHIFT, // 10 ns
); );
@ -753,7 +748,7 @@ impl HPETState {
} }
fn reset_hold(&self, _type: ResetType) { fn reset_hold(&self, _type: ResetType) {
for timer in self.timers.iter().take(self.get_num_timers()) { for timer in self.timers.iter().take(self.num_timers) {
timer.borrow_mut().reset(); timer.borrow_mut().reset();
} }
@ -781,7 +776,7 @@ impl HPETState {
GlobalRegister::try_from(addr).map(HPETRegister::Global) GlobalRegister::try_from(addr).map(HPETRegister::Global)
} else { } else {
let timer_id: usize = ((addr - 0x100) / 0x20) as usize; let timer_id: usize = ((addr - 0x100) / 0x20) as usize;
if timer_id <= self.get_num_timers() { if timer_id < self.num_timers {
// TODO: Add trace point - trace_hpet_ram_[read|write]_timer_id(timer_id) // TODO: Add trace point - trace_hpet_ram_[read|write]_timer_id(timer_id)
TimerRegister::try_from(addr & 0x18) TimerRegister::try_from(addr & 0x18)
.map(|reg| HPETRegister::Timer(&self.timers[timer_id], reg)) .map(|reg| HPETRegister::Timer(&self.timers[timer_id], reg))
@ -852,12 +847,12 @@ impl HPETState {
* also added to the migration stream. Check that it matches the value * also added to the migration stream. Check that it matches the value
* that was configured. * that was configured.
*/ */
self.num_timers_save.set(self.num_timers.get() as u8); self.num_timers_save.set(self.num_timers as u8);
0 0
} }
fn post_load(&self, _version_id: u8) -> i32 { fn post_load(&self, _version_id: u8) -> i32 {
for timer in self.timers.iter().take(self.get_num_timers()) { for timer in self.timers.iter().take(self.num_timers) {
let mut t = timer.borrow_mut(); let mut t = timer.borrow_mut();
t.cmp64 = t.calculate_cmp64(t.get_state().counter.get(), t.cmp); t.cmp64 = t.calculate_cmp64(t.get_state().counter.get(), t.cmp);
@ -882,7 +877,7 @@ impl HPETState {
} }
fn validate_num_timers(&self, _version_id: u8) -> bool { fn validate_num_timers(&self, _version_id: u8) -> bool {
self.num_timers.get() == self.num_timers_save.get().into() self.num_timers == self.num_timers_save.get().into()
} }
} }