From nobody Mon Feb 9 11:03:54 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1766827229; cv=none; d=zohomail.com; s=zohoarc; b=Yszu+CPabWXhMhekBF/j3jl/w4bx99PBCV915UqkZuOZ/+FadSnL9DyxQ4+WIo96XZWkyeV505QuzvO53+use98EYcribIX9oDYh7fIz3KXnIPsP0MY+B3gxm9umcTpLxT5MV4Nx4fiivLWlwLrjidJ1p7d7VWvhET8fFcwpvPo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1766827229; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=xJg73JCP5bk90I6eF87pOlmwqjr/KR3OYSz/2lMy7Lo=; b=gKOrlAJmIkBoqtVcTpVzSGA6GnJvf6vcBtarBxVyb/9fURJ358IhE6uP8PtX9Z8x6Ppk9NpvJ4Rm3htFwZhVklgsLELWl6t1JHm3JgkHwMLYtOSt2T9AfB9ZcI/VaV9B/A696KMs0ZOok0C7yyEz3QaZdp0XHn3KvMPny5lob0I= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 17668272295221008.2350228001667; Sat, 27 Dec 2025 01:20:29 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vZQRk-0008RR-VF; Sat, 27 Dec 2025 04:18:49 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vZQRb-0007PD-3F for qemu-devel@nongnu.org; Sat, 27 Dec 2025 04:18:39 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vZQRZ-0004Bh-4G for qemu-devel@nongnu.org; Sat, 27 Dec 2025 04:18:38 -0500 Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-84--IIT5waaMqWW5_FA3yzWwA-1; Sat, 27 Dec 2025 04:18:33 -0500 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-4792bd2c290so73398945e9.1 for ; Sat, 27 Dec 2025 01:18:33 -0800 (PST) Received: from [192.168.10.48] ([151.95.145.106]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-47d193621c8sm429301655e9.7.2025.12.27.01.18.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 27 Dec 2025 01:18:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1766827116; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xJg73JCP5bk90I6eF87pOlmwqjr/KR3OYSz/2lMy7Lo=; b=A1jRbETd/SILeAf+9rl+Lm03V0iZUvplZs/vlbfpb5UQ2wb3gKe1RgdFfiUmfrDb8LEJyN RgRHjHCdsMpY9IAjgkJLUWBiuQWGy8ET14VsEyrBsCqGp4prLy3XT6NkesFdG0Fwur/ZWc GKD+duX7T6mE40IwbcCFIV5VORW+v8M= X-MC-Unique: -IIT5waaMqWW5_FA3yzWwA-1 X-Mimecast-MFC-AGG-ID: -IIT5waaMqWW5_FA3yzWwA_1766827113 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1766827112; x=1767431912; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=xJg73JCP5bk90I6eF87pOlmwqjr/KR3OYSz/2lMy7Lo=; b=KY9tad2y4nmqWF4zgMWb7VMJBeWiMIP3nQ+IGgCLyXrGB8giHY7Ga7R4eAxnCnKESb oQ09ZzSJet9SBCbH9vHtkXfGpiz7CRhn5KJcjXH98hsXPlFIMHp9vyIulIHKDxd2Z2WO kxVZ5RpPkRQ/OmstUS4SAxfWxlyxSaTBsU0VpnogUieOpE0Mut6aXZG+yk93Eb3cDLig dysxZXQoqX1AM8kZm3zSNTJ0gsueFACr96lYKfmMbThrNkdb6XwHeACndS5Jc5rhBNVZ v5UGKiKAsd/Tj2AJSCidn2UTeqaptQqIUWyioGkaW9RdqNGOeg5MgUnpO+uyYO03LK5K o5ag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766827112; x=1767431912; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=xJg73JCP5bk90I6eF87pOlmwqjr/KR3OYSz/2lMy7Lo=; b=YJWBRnPR8MpxGsuTy1ahjUYwep2XCC9b3OL2dgUTbbOKVkxNugogP13uOBSPvLSCac OhzzPMi5oQTOIQ6P0+ilYlcnP+DKYH2e6VqmAhNnooJabXmczW3fLUqajPsGk8vcsJ6x fiShrLKUUqrEqOnaY3/GlQyeJdPIy/OfpHd0xTIkOicn3vNfJGMHnKKVIUPRB+BuiMFN gl1Wf3kboGXflQSivTgGDARVp2ZRQTE/blniBzI0EjBom5/j9LVtWc7NS2C7biVqtt9v a5ae9YvXxmuu1WR7jPgDRfHsLYoYZdVhg2ZYZm+76CAzZADrnrkNNlt2g9EeBqgAk7kV Z2Tw== X-Gm-Message-State: AOJu0YxxTOueZkmCejK9gqB9t+t5uwTgFWmjrC3/2P4xcymjcrASn/Iz oQwyqu88Z4HrNjGFkW3O7K/K02swERAJjB9urXlmJjYllw6BzXNuO119JkNSL+N3BSwxUJ6QB3W lFiWRZlheI8p1e0i02/JohNFdB0cvi3zvSjPxerxzlHXJpLGgnLc3ZXQpcj16nOH3TVv4JtZlHp VdswxkbEDja2r2P7rsL9A84dpMQDo3AaTrV2TGUQ8b X-Gm-Gg: AY/fxX5h4WbgS0zyTwYP4JpakVkqraXnl41Gzo+ezc7YgcYGn5RYGiYnWj46jQretGu 0+PukbeOW9Yr84CJxZoNvwfy8X3l6Ix1AAGtCy5xsbky0yVI2UioG2AgaaFAgc8g56WgHAkEzc5 cIMMzuRLhbX+hsAP5BjKJzUp7Ie9ymuYrAXJahWDUu4VpFdmTwnJ+esQIbOA75Nom9CvNvRSeoX luuSKa2mC2RnZmVeeruVEbL4g+Qjbtr8J5rQCil4IHGl87ElomqCmCvC8MEQVvPIKQWjGfGzpuq /8MjpTON0PnqGSAx+e2QClfAM1rqjFItNjXPUjo53MMAg+YrUg7hYeBtHSkFs+35afPeVRrUhOb hKZ7RaQpBUghHUUdarNIxbzB8kQzkE9NU1jkzxpoZMCPEyPSZzFIczIyO0e2l/2uK7atSUIYXpc +yeVCg5KFsztkKzXA= X-Received: by 2002:a05:600c:1385:b0:477:7b16:5fb1 with SMTP id 5b1f17b1804b1-47d3a5f47b3mr133615155e9.7.1766827111893; Sat, 27 Dec 2025 01:18:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IH16AAAEs8WOB3izfhztSHCIKz03DbffUWBXBoaojP8vqKu2zBDnjrrWhqPVWUjKhV2d8RZmw== X-Received: by 2002:a05:600c:1385:b0:477:7b16:5fb1 with SMTP id 5b1f17b1804b1-47d3a5f47b3mr133614875e9.7.1766827111446; Sat, 27 Dec 2025 01:18:31 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: Zhao Liu Subject: [PULL 065/153] rust/hpet: move hpet_offset to HPETRegisters Date: Sat, 27 Dec 2025 10:14:52 +0100 Message-ID: <20251227091622.20725-66-pbonzini@redhat.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20251227091622.20725-1-pbonzini@redhat.com> References: <20251227091622.20725-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1766827229806158500 Content-Type: text/plain; charset="utf-8" Likewise, do not separate hpet_offset from the other registers. However, because it is migrated in a subsection it is necessary to copy it out of HPETRegisters and into a BqlCell<>. Reviewed-by: Zhao Liu Signed-off-by: Paolo Bonzini --- rust/hw/timer/hpet/src/device.rs | 69 ++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 29 deletions(-) diff --git a/rust/hw/timer/hpet/src/device.rs b/rust/hw/timer/hpet/src/devi= ce.rs index 0dbfc5b3bab..41d5c8fea0e 100644 --- a/rust/hw/timer/hpet/src/device.rs +++ b/rust/hw/timer/hpet/src/device.rs @@ -362,8 +362,9 @@ fn update_irq(&self, regs: &mut HPETRegisters, set: boo= l) { self.set_irq(regs, set); } =20 - fn arm_timer(&self, tn_regs: &mut HPETTimerRegisters, tick: u64) { - let mut ns =3D self.get_state().get_ns(tick); + fn arm_timer(&self, regs: &mut HPETRegisters, tick: u64) { + let mut ns =3D regs.get_ns(tick); + let tn_regs =3D &mut regs.tn_regs[self.index as usize]; =20 // Clamp period to reasonable min value (1 us) if tn_regs.is_periodic() && ns - tn_regs.last < 1000 { @@ -375,21 +376,22 @@ fn arm_timer(&self, tn_regs: &mut HPETTimerRegisters,= tick: u64) { } =20 fn set_timer(&self, regs: &mut HPETRegisters) { + let cur_tick: u64 =3D regs.get_ticks(); let tn_regs =3D &mut regs.tn_regs[self.index as usize]; - let cur_tick: u64 =3D self.get_state().get_ticks(); =20 tn_regs.wrap_flag =3D 0; tn_regs.update_cmp64(cur_tick); + + let mut next_tick: u64 =3D tn_regs.cmp64; if tn_regs.is_32bit_mod() { // HPET spec says in one-shot 32-bit mode, generate an interru= pt when // counter wraps in addition to an interrupt with comparator m= atch. if !tn_regs.is_periodic() && tn_regs.cmp64 > hpet_next_wrap(cu= r_tick) { tn_regs.wrap_flag =3D 1; - self.arm_timer(tn_regs, hpet_next_wrap(cur_tick)); - return; + next_tick =3D hpet_next_wrap(cur_tick); } } - self.arm_timer(tn_regs, tn_regs.cmp64); + self.arm_timer(regs, next_tick); } =20 fn del_timer(&self, regs: &mut HPETRegisters) { @@ -506,10 +508,10 @@ fn reset(&self, regs: &mut HPETRegisters) { =20 /// timer expiration callback fn callback(&self, regs: &mut HPETRegisters) { + let cur_tick: u64 =3D regs.get_ticks(); let tn_regs =3D &mut regs.tn_regs[self.index as usize]; - let cur_tick: u64 =3D self.get_state().get_ticks(); =20 - if tn_regs.is_periodic() && tn_regs.period !=3D 0 { + let next_tick =3D if tn_regs.is_periodic() && tn_regs.period !=3D = 0 { while hpet_time_after(cur_tick, tn_regs.cmp64) { tn_regs.cmp64 +=3D tn_regs.period; } @@ -518,10 +520,16 @@ fn callback(&self, regs: &mut HPETRegisters) { } else { tn_regs.cmp =3D tn_regs.cmp64; } - self.arm_timer(tn_regs, tn_regs.cmp64); + Some(tn_regs.cmp64) } else if tn_regs.wrap_flag !=3D 0 { tn_regs.wrap_flag =3D 0; - self.arm_timer(tn_regs, tn_regs.cmp64); + Some(tn_regs.cmp64) + } else { + None + }; + + if let Some(tick) =3D next_tick { + self.arm_timer(regs, tick); } self.update_irq(regs, true); } @@ -573,9 +581,20 @@ pub struct HPETRegisters { =20 /// HPET Timer N Registers tn_regs: [HPETTimerRegisters; HPET_MAX_TIMERS], + + /// Offset of main counter relative to qemu clock. + pub hpet_offset: u64, } =20 impl HPETRegisters { + fn get_ticks(&self) -> u64 { + ns_to_ticks(CLOCK_VIRTUAL.get_ns() + self.hpet_offset) + } + + fn get_ns(&self, tick: u64) -> u64 { + ticks_to_ns(tick) - self.hpet_offset + } + fn is_legacy_mode(&self) -> bool { self.config & (1 << HPET_CFG_LEG_RT_SHIFT) !=3D 0 } @@ -603,8 +622,7 @@ pub struct HPETState { #[property(rename =3D "msi", bit =3D HPET_FLAG_MSI_SUPPORT_SHIFT, defa= ult =3D false)] flags: u32, =20 - /// Offset of main counter relative to qemu clock. - hpet_offset: BqlCell, + hpet_offset_migration: BqlCell, #[property(rename =3D "hpet-offset-saved", default =3D true)] hpet_offset_saved: bool, =20 @@ -636,14 +654,6 @@ const fn has_msi_flag(&self) -> bool { self.flags & (1 << HPET_FLAG_MSI_SUPPORT_SHIFT) !=3D 0 } =20 - fn get_ticks(&self) -> u64 { - ns_to_ticks(CLOCK_VIRTUAL.get_ns() + self.hpet_offset.get()) - } - - fn get_ns(&self, tick: u64) -> u64 { - ticks_to_ns(tick) - self.hpet_offset.get() - } - fn handle_legacy_irq(&self, irq: u32, level: u32) { let regs =3D self.regs.borrow(); if irq =3D=3D HPET_LEGACY_PIT_INT { @@ -682,8 +692,7 @@ fn set_cfg_reg(&self, regs: &mut HPETRegisters, shift: = u32, len: u32, val: u64) =20 if activating_bit(old_val, new_val, HPET_CFG_ENABLE_SHIFT) { // Enable main counter and interrupt generation. - self.hpet_offset - .set(ticks_to_ns(regs.counter) - CLOCK_VIRTUAL.get_ns()); + regs.hpet_offset =3D ticks_to_ns(regs.counter) - CLOCK_VIRTUAL= .get_ns(); =20 for t in self.timers.iter().take(self.num_timers) { let id =3D t.index as usize; @@ -696,7 +705,7 @@ fn set_cfg_reg(&self, regs: &mut HPETRegisters, shift: = u32, len: u32, val: u64) } } else if deactivating_bit(old_val, new_val, HPET_CFG_ENABLE_SHIFT= ) { // Halt main counter and disable interrupt generation. - regs.counter =3D self.get_ticks(); + regs.counter =3D regs.get_ticks(); =20 for t in self.timers.iter().take(self.num_timers) { t.del_timer(regs); @@ -763,7 +772,7 @@ unsafe fn init(mut this: ParentInit) { // initialized memory to all zeros - simple types (bool/u32/usize)= can // rely on this without explicit initialization. uninit_field_mut!(*this, regs).write(Default::default()); - uninit_field_mut!(*this, hpet_offset).write(Default::default()); + uninit_field_mut!(*this, hpet_offset_migration).write(Default::def= ault()); // Set null_mut for now and post_init() will fill it. uninit_field_mut!(*this, irqs).write(Default::default()); uninit_field_mut!(*this, rtc_irq_level).write(Default::default()); @@ -814,6 +823,7 @@ fn reset_hold(&self, _type: ResetType) { =20 regs.counter =3D 0; regs.config =3D 0; + regs.hpet_offset =3D 0; HPETFwConfig::update_hpet_cfg( self.hpet_id.get(), regs.capability as u32, @@ -826,7 +836,6 @@ fn reset_hold(&self, _type: ResetType) { drop(regs); =20 self.pit_enabled.set(true); - self.hpet_offset.set(0); =20 // to document that the RTC lowers its output on reset as well self.rtc_irq_level.set(0); @@ -871,7 +880,7 @@ fn read(&self, addr: hwaddr, size: u32) -> u64 { Global(INT_STATUS) =3D> regs.int_status, Global(COUNTER) =3D> { let cur_tick =3D if regs.is_hpet_enabled() { - self.get_ticks() + regs.get_ticks() } else { regs.counter }; @@ -907,8 +916,9 @@ fn write(&self, addr: hwaddr, value: u64, size: u32) { =20 fn pre_save(&self) -> Result<(), migration::Infallible> { let mut regs =3D self.regs.borrow_mut(); + self.hpet_offset_migration.set(regs.hpet_offset); if regs.is_hpet_enabled() { - regs.counter =3D self.get_ticks(); + regs.counter =3D regs.get_ticks(); } =20 /* @@ -933,9 +943,10 @@ fn post_load(&self, _version_id: u8) -> Result<(), mig= ration::Infallible> { =20 // Recalculate the offset between the main counter and guest time if !self.hpet_offset_saved { - self.hpet_offset + self.hpet_offset_migration .set(ticks_to_ns(regs.counter) - CLOCK_VIRTUAL.get_ns()); } + regs.hpet_offset =3D self.hpet_offset_migration.get(); =20 Ok(()) } @@ -987,7 +998,7 @@ impl ObjectImpl for HPETState { .minimum_version_id(1) .needed(&HPETState::is_offset_needed) .fields(vmstate_fields! { - vmstate_of!(HPETState, hpet_offset), + vmstate_of!(HPETState, hpet_offset_migration), }) .build(); =20 --=20 2.52.0