From nobody Tue Feb 10 03:20:17 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=1766827172; cv=none; d=zohomail.com; s=zohoarc; b=aRi54pA4s3+edRGZiLV/dxGtEXA0FkOnEdwGJ/2bvaNUukhHoQNbJsYjLxESh31JiygQcYrxZ7ZlpsvSvCIYnsqRnCbTQI9wQVhMP1IcrRIfozxNKv1eiDoF4MGEDmk2mvQOOvBUe7axoEjYEsY1s8sj1J43xt6gXNMevWpgQ/Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1766827172; 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=jkC95+QEJXnEvaNYkvGGuoEWgNXpEhyP0YGy73T68So=; b=f34ID+QfaYhXzPXXWuFgNXnTBi8z/yexLmqhTJiVaRr0XNGummFkfoVAX/XKDcnMSI1Imum2YkGP5NZYwwkawXj3t+wnIzN77vuvcG9/bMsyCswoNzEYzCzTYPzD0wWCpI1Iz/cjYrW4LC9iI6829iCD9abNrJBL0h02RCXoo6o= 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 1766827172354762.5489222095176; Sat, 27 Dec 2025 01:19:32 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vZQRa-0007KF-4V; Sat, 27 Dec 2025 04:18:38 -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 1vZQRI-0006oB-Qe for qemu-devel@nongnu.org; Sat, 27 Dec 2025 04:18:22 -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 1vZQRG-0003gP-UD for qemu-devel@nongnu.org; Sat, 27 Dec 2025 04:18:20 -0500 Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-496-SZ6Wppi9NKCQc6bkiuuIag-1; Sat, 27 Dec 2025 04:18:16 -0500 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-430fc83f58dso4347628f8f.2 for ; Sat, 27 Dec 2025 01:18:15 -0800 (PST) Received: from [192.168.10.48] ([151.95.145.106]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4324eaa08efsm50446158f8f.29.2025.12.27.01.18.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 27 Dec 2025 01:18:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1766827098; 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=jkC95+QEJXnEvaNYkvGGuoEWgNXpEhyP0YGy73T68So=; b=bp/WZAo5+QwNKcHYB7SptOtVRHhdyWyq9zJ8wR2pj5K1vG4NCiqFQ2E3NcnS5/I7FINV6H DZz38gX4umZ+PrpSTiko0jJ/T158203PYV/ASlyRKFvgKaWrFVF5x/UiTD0zXlJfo1j+lv EK6byUg0dovyUI/xDyceXfNIC/jCYmk= X-MC-Unique: SZ6Wppi9NKCQc6bkiuuIag-1 X-Mimecast-MFC-AGG-ID: SZ6Wppi9NKCQc6bkiuuIag_1766827095 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1766827094; x=1767431894; 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=jkC95+QEJXnEvaNYkvGGuoEWgNXpEhyP0YGy73T68So=; b=rUp02qTZlUTnM1PRd8NbZ+BzewvCwqOy9mEiDKrO/sRKUi9WxIkuYUYgsfHKPC31bp bWShAqRaLgaiozTqADEWgmU8QtU5EeqsndEax5sIvgt7mYarnuHi3I9RbCFIRBMkP8Kv Co+7ZZVI31QCRjKLss+LyXq+/6UeyeeAlOYy1BiZksn/kvKgfgSMnMnotSkgritfvVR0 lHxKSRV9PfH5NC95RyvGdJj/KTyhGJswfVXc5Jeb0ZyM3tUDIchoGXXQsOo9ndT+n2Ta OLGfCZpetpt8Cf9L0E6hG9TMIMiAeaA1kxlmL58nG2lWcwKV7u3qbDJmPdxwmUPQFbZ3 4TUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766827094; x=1767431894; 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=jkC95+QEJXnEvaNYkvGGuoEWgNXpEhyP0YGy73T68So=; b=tqgPT8Kd4slhyjcwcYfdrRcpMdgg7lv5Hg45D0RSi8+TYUhYL/FGWPbWak9CJOan8f zw0RThzK48iRL1zALFor3vaZArB451nhw5owNi99nbYEwlLisWIockpdZDbu2O/ZX1I6 NyC6QIS0ZSVAuHpT+v4IDABRcQ1b1MBFZ5G2ycKtTWJTQ5NEc4lsGkYIdXaNpByCBlYz kPV7D1rUGR8cGeUVoaYmmTKwkaabJI3XOEYl1vNK3813yo8n+4SM8yO539NLSEZVaRHP KQgFgdtiELRKusQ5JDmocp2nfiifpzie32KQsNXZo5945Y31gEF+cHY+emt7r23gL0xA F9XA== X-Gm-Message-State: AOJu0YweetGgPnCji2nDbBLireuOsWwom5nB8ujdOujP0DcX6BpKbN8X yysV4Pn6wNxudHgc/pkpqOJ98dXlN0x39RWQhlxfZ1+PZ3ngOMfCe1VDiZ/PqnNrWJwxAhzmZ9a 8QgHWDT5yQkk5K/x6Rp+OOsGHaHzLpfWrs2JU/eHjAYIR3lYsdWUQ7EWzh46iy2uH6mT3WNYSQ+ XR87TJeJiTqojCcpCaBD2i23Bay9WprNFF95mE1sVJ X-Gm-Gg: AY/fxX4Rpn9PeYxW/CMSVPveNmw4HVl0+8TQFDT4X0gYs6ZO0DJv1uKVfRsKRC0ldhd Vl/3f3vrYH9UCcr67pIz1D2ByANZaftXqMxHzdZNIsRZfNVGk2WAsJIUH1iuUUJS2FYoroZv92F t+TjrnqPk958JzsUYc6sMDM14OMAQDjVlT6TAR8CCcW44togarnmDaZqVWeamTaFM9V/W5Wiiot nLGU3dueOxbIeDax/TSAJNfdcyNF3rscUA/Vtm/Akg72Hh7zXelwHAimkF5MoX90Xu2z8kVSsbG PCoyQTUi6Vld6Jd5njgaGsPFjMd8/LtyPnDZ32jRivbxnr2+DhVLJVi2r//9nnjHBc5FTwlkGNj +oatjmwhgQA0kwTCxvRteda3n/PCunxddyx/xl8E6bcI/W3yEomQE10hOxAoOq2lgqYghatwpsx BIG7fc06m9LGOiTrE= X-Received: by 2002:a05:6000:25c8:b0:430:fdc8:8be3 with SMTP id ffacd0b85a97d-4324e4f938fmr30133575f8f.29.1766827093796; Sat, 27 Dec 2025 01:18:13 -0800 (PST) X-Google-Smtp-Source: AGHT+IGzo3YNrl9fvsEexixfKvLP8kTB+O1gGU0JaiOoOOwBNUBxPnNeEofUdeB8W2QAZqdXXD2y3A== X-Received: by 2002:a05:6000:25c8:b0:430:fdc8:8be3 with SMTP id ffacd0b85a97d-4324e4f938fmr30133543f8f.29.1766827093273; Sat, 27 Dec 2025 01:18:13 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: Zhao Liu Subject: [PULL 057/153] rust/hpet: Make global register accessors as methods of HPETRegisters Date: Sat, 27 Dec 2025 10:14:44 +0100 Message-ID: <20251227091622.20725-58-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: 1766827173565158500 Content-Type: text/plain; charset="utf-8" From: Zhao Liu Implement helper accessors as methods of HPETRegisters. Then HPETRegisters can be accessed without going through HPETState. In subsequent refactoring, coarser-grained BQL lock protection will be implemented. Specifically, BqlRefCell will be borrowed only once during MMIO accesses, and the scope of borrowed `regs` will be extended to cover the entire MMIO access. Consequently, repeated borrow() attempts within function calls will no longer be allowed. Therefore, refactor the accessors of HPETRegisters to bypass HPETState, which help to reduce borrow() in deep function calls. Signed-off-by: Zhao Liu Link: https://lore.kernel.org/r/20251113051937.4017675-14-zhao1.liu@intel.c= om Signed-off-by: Paolo Bonzini --- rust/hw/timer/hpet/src/device.rs | 60 ++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/rust/hw/timer/hpet/src/device.rs b/rust/hw/timer/hpet/src/devi= ce.rs index bf9f4936718..da938f356e9 100644 --- a/rust/hw/timer/hpet/src/device.rs +++ b/rust/hw/timer/hpet/src/device.rs @@ -280,7 +280,10 @@ fn get_state(&self) -> &HPETState { } =20 fn is_int_active(&self) -> bool { - self.get_state().is_timer_int_active(self.index.into()) + self.get_state() + .regs + .borrow() + .is_timer_int_active(self.index.into()) } =20 /// calculate next value of the general counter that matches the @@ -299,7 +302,7 @@ fn calculate_cmp64(&self, cur_tick: u64, target: u64) -= > u64 { } =20 fn get_int_route(&self) -> usize { - if self.index <=3D 1 && self.get_state().is_legacy_mode() { + if self.index <=3D 1 && self.get_state().regs.borrow().is_legacy_m= ode() { // If LegacyReplacement Route bit is set, HPET specification r= equires // timer0 be routed to IRQ0 in NON-APIC or IRQ2 in the I/O API= C, // timer1 be routed to IRQ8 in NON-APIC or IRQ8 in the I/O API= C. @@ -326,7 +329,7 @@ fn get_int_route(&self) -> usize { fn set_irq(&self, set: bool) { let route =3D self.get_int_route(); =20 - if set && self.regs.is_int_enabled() && self.get_state().is_hpet_e= nabled() { + if set && self.regs.is_int_enabled() && self.get_state().regs.borr= ow().is_hpet_enabled() { if self.regs.is_fsb_route_enabled() { // SAFETY: // the parameters are valid. @@ -425,7 +428,7 @@ fn set_tn_cfg_reg(&mut self, shift: u32, len: u32, val:= u64) { self.period =3D u64::from(self.period as u32); // truncate! } =20 - if self.get_state().is_hpet_enabled() { + if self.get_state().regs.borrow().is_hpet_enabled() { self.set_timer(); } } @@ -456,7 +459,7 @@ fn set_tn_cmp_reg(&mut self, shift: u32, len: u32, val:= u64) { } =20 self.regs.clear_valset(); - if self.get_state().is_hpet_enabled() { + if self.get_state().regs.borrow().is_hpet_enabled() { self.set_timer(); } } @@ -540,6 +543,20 @@ pub struct HPETRegisters { counter: u64, } =20 +impl HPETRegisters { + fn is_legacy_mode(&self) -> bool { + self.config & (1 << HPET_CFG_LEG_RT_SHIFT) !=3D 0 + } + + fn is_hpet_enabled(&self) -> bool { + self.config & (1 << HPET_CFG_ENABLE_SHIFT) !=3D 0 + } + + fn is_timer_int_active(&self, index: usize) -> bool { + self.int_status & (1 << index) !=3D 0 + } +} + /// HPET Event Timer Block Abstraction #[repr(C)] #[derive(qom::Object, hwcore::Device)] @@ -587,18 +604,6 @@ const fn has_msi_flag(&self) -> bool { self.flags & (1 << HPET_FLAG_MSI_SUPPORT_SHIFT) !=3D 0 } =20 - fn is_legacy_mode(&self) -> bool { - self.regs.borrow().config & (1 << HPET_CFG_LEG_RT_SHIFT) !=3D 0 - } - - fn is_hpet_enabled(&self) -> bool { - self.regs.borrow().config & (1 << HPET_CFG_ENABLE_SHIFT) !=3D 0 - } - - fn is_timer_int_active(&self, index: usize) -> bool { - self.regs.borrow().int_status & (1 << index) !=3D 0 - } - fn get_ticks(&self) -> u64 { ns_to_ticks(CLOCK_VIRTUAL.get_ns() + self.hpet_offset.get()) } @@ -608,13 +613,14 @@ fn get_ns(&self, tick: u64) -> u64 { } =20 fn handle_legacy_irq(&self, irq: u32, level: u32) { + let regs =3D self.regs.borrow(); if irq =3D=3D HPET_LEGACY_PIT_INT { - if !self.is_legacy_mode() { + if !regs.is_legacy_mode() { self.irqs[0].set(level !=3D 0); } } else { self.rtc_irq_level.set(level); - if !self.is_legacy_mode() { + if !regs.is_legacy_mode() { self.irqs[RTC_ISA_IRQ].set(level !=3D 0); } } @@ -697,7 +703,8 @@ fn set_int_status_reg(&self, shift: u32, _len: u32, val= : u64) { =20 /// Main Counter Value Register fn set_counter_reg(&self, shift: u32, len: u32, val: u64) { - if self.is_hpet_enabled() { + let mut regs =3D self.regs.borrow_mut(); + if regs.is_hpet_enabled() { // HPET spec says that writes to this register should only be // done while the counter is halted. So this is an undefined // behavior. There's no need to forbid it, but when HPET is @@ -706,7 +713,6 @@ fn set_counter_reg(&self, shift: u32, len: u32, val: u6= 4) { // not be changed as well). trace::trace_hpet_ram_write_counter_write_while_enabled(); } - let mut regs =3D self.regs.borrow_mut(); regs.counter =3D regs.counter.deposit(shift, len, val); } =20 @@ -825,10 +831,11 @@ fn read(&self, addr: hwaddr, size: u32) -> u64 { Global(CFG) =3D> self.regs.borrow().config, Global(INT_STATUS) =3D> self.regs.borrow().int_status, Global(COUNTER) =3D> { - let cur_tick =3D if self.is_hpet_enabled() { + let regs =3D self.regs.borrow(); + let cur_tick =3D if regs.is_hpet_enabled() { self.get_ticks() } else { - self.regs.borrow().counter + regs.counter }; =20 trace::trace_hpet_ram_read_reading_counter((addr & 4) as u= 8, cur_tick); @@ -860,8 +867,9 @@ fn write(&self, addr: hwaddr, value: u64, size: u32) { } =20 fn pre_save(&self) -> Result<(), migration::Infallible> { - if self.is_hpet_enabled() { - self.regs.borrow_mut().counter =3D self.get_ticks(); + let mut regs =3D self.regs.borrow_mut(); + if regs.is_hpet_enabled() { + regs.counter =3D self.get_ticks(); } =20 /* @@ -896,7 +904,7 @@ fn is_rtc_irq_level_needed(&self) -> bool { } =20 fn is_offset_needed(&self) -> bool { - self.is_hpet_enabled() && self.hpet_offset_saved + self.regs.borrow().is_hpet_enabled() && self.hpet_offset_saved } =20 fn validate_num_timers(&self, _version_id: u8) -> bool { --=20 2.52.0