From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: qemu-rust@nongnu.org, zhao1.liu@intel.com
Subject: [PATCH 3/5] rust/hpet: remove BqlRefCell around HPETTimer
Date: Mon, 17 Nov 2025 09:47:50 +0100 [thread overview]
Message-ID: <20251117084752.203219-4-pbonzini@redhat.com> (raw)
In-Reply-To: <20251117084752.203219-1-pbonzini@redhat.com>
HPETTimer now has all of its state stored in HPETRegisters, so it does not
need its own BqlRefCell anymore.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
rust/hw/timer/hpet/src/device.rs | 53 ++++++++++++++++----------------
rust/util/src/timer.rs | 12 +++++---
2 files changed, 34 insertions(+), 31 deletions(-)
diff --git a/rust/hw/timer/hpet/src/device.rs b/rust/hw/timer/hpet/src/device.rs
index 19676af74bc..5bcf151a680 100644
--- a/rust/hw/timer/hpet/src/device.rs
+++ b/rust/hw/timer/hpet/src/device.rs
@@ -126,7 +126,7 @@ enum DecodedRegister<'a> {
Global(GlobalRegister),
/// Register in the timer block `0x100`...`0x3ff`
- Timer(&'a BqlRefCell<HPETTimer>, TimerRegister),
+ Timer(&'a HPETTimer, TimerRegister),
/// Invalid address
#[allow(dead_code)]
@@ -170,8 +170,7 @@ const fn deactivating_bit(old: u64, new: u64, shift: usize) -> bool {
(old & mask != 0) && (new & mask == 0)
}
-fn timer_handler(timer_cell: &BqlRefCell<HPETTimer>) {
- let t = timer_cell.borrow();
+fn timer_handler(t: &HPETTimer) {
// SFAETY: state field is valid after timer initialization.
let regs = &mut unsafe { t.state.as_ref() }.regs.borrow_mut();
t.callback(regs)
@@ -277,12 +276,16 @@ fn new(index: u8, state: *const HPETState) -> HPETTimer {
}
}
- fn init_timer_with_cell(cell: &BqlRefCell<Self>) {
- let mut timer = cell.borrow_mut();
- // SAFETY: HPETTimer is only used as part of HPETState, which is
- // always pinned.
- let qemu_timer = unsafe { Pin::new_unchecked(&mut timer.qemu_timer) };
- qemu_timer.init_full(None, CLOCK_VIRTUAL, Timer::NS, 0, timer_handler, cell);
+ fn init_timer(timer: Pin<&mut Self>) {
+ Timer::init_full(
+ timer,
+ None,
+ CLOCK_VIRTUAL,
+ Timer::NS,
+ 0,
+ timer_handler,
+ |t| &mut t.qemu_timer,
+ );
}
fn get_state(&self) -> &HPETState {
@@ -726,7 +729,7 @@ pub struct HPETState {
/// HPET timer array managed by this timer block.
#[doc(alias = "timer")]
- timers: [BqlRefCell<HPETTimer>; HPET_MAX_TIMERS],
+ timers: [HPETTimer; HPET_MAX_TIMERS],
#[property(rename = "timers", default = HPET_MIN_TIMERS)]
num_timers: usize,
num_timers_save: BqlCell<u8>,
@@ -761,11 +764,10 @@ fn init_timers(this: &mut MaybeUninit<Self>) {
// Initialize in two steps, to avoid calling Timer::init_full on a
// temporary that can be moved.
- let timer = timer.write(BqlRefCell::new(HPETTimer::new(
- index.try_into().unwrap(),
- state,
- )));
- HPETTimer::init_timer_with_cell(timer);
+ let timer = timer.write(HPETTimer::new(index.try_into().unwrap(), state));
+ // SAFETY: HPETState is pinned
+ let timer = unsafe { Pin::new_unchecked(timer) };
+ HPETTimer::init_timer(timer);
}
}
@@ -787,8 +789,7 @@ fn set_cfg_reg(&self, regs: &mut HPETRegisters, shift: u32, len: u32, val: u64)
// Enable main counter and interrupt generation.
regs.hpet_offset = ticks_to_ns(regs.counter) - CLOCK_VIRTUAL.get_ns();
- for timer in self.timers.iter().take(self.num_timers) {
- let t = timer.borrow();
+ for t in self.timers.iter().take(self.num_timers) {
let id = t.index as usize;
let tn_regs = ®s.tn_regs[id];
@@ -801,8 +802,8 @@ fn set_cfg_reg(&self, regs: &mut HPETRegisters, shift: u32, len: u32, val: u64)
// Halt main counter and disable interrupt generation.
regs.counter = regs.get_ticks();
- for timer in self.timers.iter().take(self.num_timers) {
- timer.borrow().del_timer(regs);
+ for t in self.timers.iter().take(self.num_timers) {
+ t.del_timer(regs);
}
}
@@ -830,9 +831,9 @@ fn set_int_status_reg(&self, regs: &mut HPETRegisters, shift: u32, _len: u32, va
let new_val = val << shift;
let cleared = new_val & regs.int_status;
- for (index, timer) in self.timers.iter().take(self.num_timers).enumerate() {
- if cleared & (1 << index) != 0 {
- timer.borrow().update_irq(regs, false);
+ for t in self.timers.iter().take(self.num_timers) {
+ if cleared & (1 << t.index) != 0 {
+ t.update_irq(regs, false);
}
}
}
@@ -928,8 +929,8 @@ fn reset_hold(&self, _type: ResetType) {
{
let mut regs = self.regs.borrow_mut();
- for timer in self.timers.iter().take(self.num_timers) {
- timer.borrow().reset(&mut regs);
+ for t in self.timers.iter().take(self.num_timers) {
+ t.reset(&mut regs);
}
regs.counter = 0;
@@ -981,7 +982,7 @@ fn read(&self, addr: hwaddr, size: u32) -> u64 {
use DecodedRegister::*;
use GlobalRegister::*;
(match target {
- Timer(timer, tn_target) => timer.borrow().read(tn_target, regs),
+ Timer(t, tn_target) => t.read(tn_target, regs),
Global(CAP) => regs.capability, /* including HPET_PERIOD 0x004 */
Global(CFG) => regs.config,
Global(INT_STATUS) => regs.int_status,
@@ -1012,7 +1013,7 @@ fn write(&self, addr: hwaddr, value: u64, size: u32) {
use DecodedRegister::*;
use GlobalRegister::*;
match target {
- Timer(timer, tn_target) => timer.borrow().write(tn_target, regs, value, shift, len),
+ Timer(t, tn_target) => t.write(tn_target, regs, value, shift, len),
Global(CAP) => {} // General Capabilities and ID Register: Read Only
Global(CFG) => self.set_cfg_reg(regs, shift, len, value),
Global(INT_STATUS) => self.set_int_status_reg(regs, shift, len, value),
diff --git a/rust/util/src/timer.rs b/rust/util/src/timer.rs
index c6b3e4088ec..829f52d111e 100644
--- a/rust/util/src/timer.rs
+++ b/rust/util/src/timer.rs
@@ -45,14 +45,14 @@ impl Timer {
}
/// Create a new timer with the given attributes.
- pub fn init_full<'timer, 'opaque: 'timer, T, F>(
- self: Pin<&'timer mut Self>,
+ pub fn init_full<T, F>(
+ opaque: Pin<&mut T>,
timer_list_group: Option<&TimerListGroup>,
clk_type: ClockType,
scale: u32,
attributes: u32,
_cb: F,
- opaque: &'opaque T,
+ field: impl FnOnce(&mut T) -> &mut Self,
) where
F: for<'a> FnCall<(&'a T,)>,
{
@@ -70,8 +70,10 @@ pub fn init_full<'timer, 'opaque: 'timer, T, F>(
// SAFETY: the opaque outlives the timer
unsafe {
+ let opaque = Pin::into_inner_unchecked(opaque);
+ let timer = field(opaque).as_mut_ptr();
timer_init_full(
- self.as_mut_ptr(),
+ timer,
if let Some(g) = timer_list_group {
g as *const TimerListGroup as *mut _
} else {
@@ -81,7 +83,7 @@ pub fn init_full<'timer, 'opaque: 'timer, T, F>(
scale as c_int,
attributes as c_int,
Some(timer_cb),
- (opaque as *const T).cast::<c_void>().cast_mut(),
+ (opaque as *mut T).cast::<c_void>(),
)
}
}
--
2.51.1
next prev parent reply other threads:[~2025-11-17 8:49 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-17 8:47 [PATCH 0/5] rust/hpet: complete moving state out of HPETTimer Paolo Bonzini
2025-11-17 8:47 ` [PATCH 1/5] rust/hpet: move hidden registers to HPETTimerRegisters Paolo Bonzini
2025-11-18 8:35 ` Zhao Liu
2025-11-17 8:47 ` [PATCH 2/5] rust/hpet: move hpet_offset to HPETRegisters Paolo Bonzini
2025-11-18 13:54 ` Zhao Liu
2025-11-17 8:47 ` Paolo Bonzini [this message]
2025-11-19 15:17 ` [PATCH 3/5] rust/hpet: remove BqlRefCell around HPETTimer Zhao Liu
2025-11-19 22:28 ` Paolo Bonzini
2025-11-17 8:47 ` [PATCH 4/5] rust: migration: implement ToMigrationState for Timer Paolo Bonzini
2025-11-20 14:31 ` Zhao Liu
2025-11-17 8:47 ` [PATCH 5/5] rust/hpet: Apply Migratable<> wrapper and ToMigrationState Paolo Bonzini
2025-11-19 15:31 ` Zhao Liu
2025-11-19 15:59 ` [PATCH 0/5] rust/hpet: complete moving state out of HPETTimer Zhao Liu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251117084752.203219-4-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=qemu-rust@nongnu.org \
--cc=zhao1.liu@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).