From nobody Wed Apr 1 09:45:01 2026 Received: from DM5PR21CU001.outbound.protection.outlook.com (mail-centralusazon11011013.outbound.protection.outlook.com [52.101.62.13]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EF0D54534BA; Tue, 31 Mar 2026 21:21:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.62.13 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774992080; cv=fail; b=HnXFeYQt+4bn9t283cPc0IAO89NitXzjfe5hm6KHHyge07sSqYY45dbx1fk85sIPyVMwn4nTR9JN80ZEBYM9ULiTCIhQCWNLxbyMarmIgpDHXTCKmGp+wnljgp24nald1gtB57YVleT4WV4YRs42W6z06JAlHiz2ak8/BnjU12Q= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774992080; c=relaxed/simple; bh=hy/DWkrKifR4kjY95JazxotKUaxIjpsAwJ5RKkIoHro=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: Content-Type:MIME-Version; b=JkyqjVKuMjQkOlEZJ7IazljzPwW/DNcgVSnB6AoNRZ0pNsxOfiBnqHKTzUj7cwJUH6btnSP5yIDkvTmHeq1/2aqRUUEWt8z2ZiyVusStmNcLkNz8UyrYUCTmxvbdY4jYU3DxqytEnjTCI1bHefvBbFx8DWsG7eaLYBPp2ubAVrw= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=qQ5mCDlH; arc=fail smtp.client-ip=52.101.62.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="qQ5mCDlH" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=AwqKap7pjnoRxCHrW6ZURcK3N59RMhajYZFufWwnmzNIQeBQIdWdp3dl1JzvdoJJo/zWb7ZcFmR0E8Ojqmy9YZp0mZq1qLvQcITRfy0tWy6KLOwh/Z1NzDELiK+UQ7w3enQ5+N2aP2lUeJC+z3Y2uja5EtWv9e236m4Za5yedgLE7k/NLW9Duye314b9tJYcV3z9lEaHdQ4jO/srwrS5OhugDacopziNZYA4aU8izTDsLSAKinpjQwYMNbhrlmESkM9jceEwiDbskGbFWlPnoW1aIKnkK5t71v9VcPqIDoDOIL7u88uGRRoL5yI16F8Yw5eSQbwsYVCtvgF9SKc/6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=LDBCutnsz3JPI4JJT75zhjTl7rs9/+BdsTXMsrNf598=; b=IDjzqJZd9yVpJd21V3K9ithvYwyB32Azn9hHi2dAGRO0Txq+nMTkt+cnv7glqdX4yCHK42TWunW+acCh12yuN5R5nWH9bd7aehdpK+aOV75IbZg2XIlSbnSiO4AbKxjcpLEQTXeY7VZoSNfO+U2IwNTPzgIjJu8Bpwt4eVpgSvd2rodR+n+/bLjZyKkWmYWQ54U7qQ/CauVQ698iv6CrYUotfzn/of01ZBwaM0pmFvxOaQdSydBHQDbuTNckjDjTFcQYFS8TPeKyP0b0cC/HFcKnKO6NBoiw3ioKuTIdNOfgYt6rQg76hGQDMXXxFbXZvD6U8nxiWOPQja4hU3uMig== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=LDBCutnsz3JPI4JJT75zhjTl7rs9/+BdsTXMsrNf598=; b=qQ5mCDlH5XYXDZg4xrKavWQ/76U5ts/sttj5cuvZdj5VQB+x7QfWLn2qL78tPvGijfWmRElA+EQIRh612bK7XRsY9y9bwSpzx0jL1XJ99wJCC1A/hIz975zMjVbg33rwvXnuorX+L+MfqCbudFSNNJvjeIRh+KHT+oRL1q4qvjxSnh5nkSppHsOEUQNpxNwcDeht3CLxk8Qb+AMQL24gcahIhXWcvJQmaS3UeChx8Xc/nsgA2dvodaKtjRbtTLKBi25MtnmBnue+7MKFP7bO7YGyCp4iXU2mmDbpJYPPPnVAP7TNtl19DJOS6A3+18ZoDxtcezj9KlGVbEkJrTWhEw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DS0PR12MB6486.namprd12.prod.outlook.com (2603:10b6:8:c5::21) by BL1PR12MB5849.namprd12.prod.outlook.com (2603:10b6:208:384::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9769.8; Tue, 31 Mar 2026 21:21:11 +0000 Received: from DS0PR12MB6486.namprd12.prod.outlook.com ([fe80::88a9:f314:c95f:8b33]) by DS0PR12MB6486.namprd12.prod.outlook.com ([fe80::88a9:f314:c95f:8b33%4]) with mapi id 15.20.9769.014; Tue, 31 Mar 2026 21:21:11 +0000 From: Joel Fernandes To: linux-kernel@vger.kernel.org Cc: Miguel Ojeda , Boqun Feng , Gary Guo , Bjorn Roy Baron , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Dave Airlie , Daniel Almeida , Koen Koning , dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org, Nikola Djukic , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Jonathan Corbet , Alex Deucher , Christian Koenig , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , Huang Rui , Matthew Auld , Matthew Brost , Lucas De Marchi , Thomas Hellstrom , Helge Deller , Alex Gaynor , Boqun Feng , John Hubbard , Alistair Popple , Timur Tabi , Edwin Peer , Alexandre Courbot , Andrea Righi , Andy Ritger , Zhi Wang , Balbir Singh , Philipp Stanner , Elle Rhumsaa , alexeyi@nvidia.com, Eliot Courtney , joel@joelfernandes.org, linux-doc@vger.kernel.org, amd-gfx@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, intel-xe@lists.freedesktop.org, linux-fbdev@vger.kernel.org, Joel Fernandes Subject: [PATCH v10 04/21] gpu: nova-core: mm: Add support to use PRAMIN windows to write to VRAM Date: Tue, 31 Mar 2026 17:20:31 -0400 Message-Id: <20260331212048.2229260-5-joelagnelf@nvidia.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260331212048.2229260-1-joelagnelf@nvidia.com> References: <20260311004008.2208806-1-joelagnelf@nvidia.com> <20260331212048.2229260-1-joelagnelf@nvidia.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: BLAPR05CA0013.namprd05.prod.outlook.com (2603:10b6:208:36e::28) To DS0PR12MB6486.namprd12.prod.outlook.com (2603:10b6:8:c5::21) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS0PR12MB6486:EE_|BL1PR12MB5849:EE_ X-MS-Office365-Filtering-Correlation-Id: a042bbf4-806d-4e2d-d3ae-08de8f6b6e56 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|7416014|376014|366016|22082099003|18002099003|56012099003; X-Microsoft-Antispam-Message-Info: LyKJCwZVy8daUDuyC/eFYi14GdHOsLZfQr2AVlgb/q3CkdW90bu9Dg9PLhTlP/Lj0W3Fo51CrwNbgifOHRYyp2ZOZ0qell12S/Wq4Ahf7qiGJRZlhMeGfDXtWaJ2YvqrQ65kiLI1e/9iL6QMh2pEx8W/XDGnDjw1PGCkxgEkELnxMpwBZyMyIfNB1+ggQa7LATESm6K9Gt3QKW450++MeNwET/bLsfWWTMd/9Pz5Ok4lBHscc78tXWrPw3sKJsmLnB8CsFmHE3Q+izs9xxgn6QqIinJ0YWJSxvKvdD7cw4QdF/90FeXgO/12siEZSHtD4zIRRiy2V44OAoHfCnoxb71ZIIKtDaEcF6dSXuQFRNjgMF5bsSV/q6BjXEZbOsYG1PfGSSTyVVEiRYeEBVpJLkmxpPrqdMGqOO1/bEo2Y2Yrio0E73BeThcYzNX0JYXiu2DgAaY/ERh/FO5QQb7V/i3vKQU7atBwW6VPjiWKmmsyDMBE4GfcP5s3LTOngLBX22uB9lIWHEdW/PU7+0wI4rmY1NkLQfQ46wnrWsBiJhrVUwubIBxdDSgdPXGgBaElNm2L1mGXBIu2Qeg0oOdmLG819/NwRxkqkCpzlqSQRvcH3/BVZWBb02bnb5cLRVTt589fycMFHimpCFe5EY9YdUI8Lj2U7BAI0bys7GldvQfHQOn9T1d53yPpM+dkDThwYenaadTFlbDHmBe34C7ErDe78o8o/O9V4Tg3LrguQhA= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DS0PR12MB6486.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(7416014)(376014)(366016)(22082099003)(18002099003)(56012099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?i7z2zNIjSqDDgT+izjI96C/dCK3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idia.com X-MS-Exchange-CrossTenant-Network-Message-Id: a042bbf4-806d-4e2d-d3ae-08de8f6b6e56 X-MS-Exchange-CrossTenant-AuthSource: DS0PR12MB6486.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Mar 2026 21:21:11.0624 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: GRJfFteYh6+PZAkWnR89HvP0DzUbr6/9OL/cZInxzmw0mttuoMI6k5qUMMriwffAE83leq2mgmBcTM5qCEVxNw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5849 Content-Type: text/plain; charset="utf-8" PRAMIN apertures are a crucial mechanism to direct read/write to VRAM. Add support for the same. Cc: Nikola Djukic Signed-off-by: Joel Fernandes --- drivers/gpu/nova-core/mm.rs | 5 + drivers/gpu/nova-core/mm/pramin.rs | 280 +++++++++++++++++++++++++++++ drivers/gpu/nova-core/nova_core.rs | 1 + drivers/gpu/nova-core/regs.rs | 10 ++ 4 files changed, 296 insertions(+) create mode 100644 drivers/gpu/nova-core/mm.rs create mode 100644 drivers/gpu/nova-core/mm/pramin.rs diff --git a/drivers/gpu/nova-core/mm.rs b/drivers/gpu/nova-core/mm.rs new file mode 100644 index 000000000000..7a5dd4220c67 --- /dev/null +++ b/drivers/gpu/nova-core/mm.rs @@ -0,0 +1,5 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Memory management subsystems for nova-core. + +pub(crate) mod pramin; diff --git a/drivers/gpu/nova-core/mm/pramin.rs b/drivers/gpu/nova-core/mm/= pramin.rs new file mode 100644 index 000000000000..fde0eb30eaeb --- /dev/null +++ b/drivers/gpu/nova-core/mm/pramin.rs @@ -0,0 +1,280 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Direct VRAM access through the PRAMIN aperture. +//! +//! PRAMIN provides a 1MB sliding window into VRAM through BAR0, allowing = the CPU to access +//! video memory directly. Access is managed through a two-level API: +//! +//! - [`Pramin`]: The parent object that owns the BAR0 reference and synch= ronization lock. +//! - [`PraminWindow`]: A guard object that holds exclusive PRAMIN access = for its lifetime. +//! +//! The PRAMIN aperture is a 1MB region at BAR0 + 0x700000 for all GPUs. T= he window base is +//! controlled by the `NV_PBUS_BAR0_WINDOW` register and is 64KB aligned. +//! +//! # Examples +//! +//! ## Basic read/write +//! +//! ```no_run +//! use crate::driver::Bar0; +//! use crate::mm::pramin; +//! use kernel::devres::Devres; +//! use kernel::prelude::*; +//! use kernel::sync::Arc; +//! +//! fn example(devres_bar: Arc>, vram_region: core::ops::Rang= e) -> Result<()> { +//! let pramin =3D Arc::pin_init(pramin::Pramin::new(devres_bar, vram_= region)?, GFP_KERNEL)?; +//! let mut window =3D pramin.get_window()?; +//! +//! // Write and read back. +//! window.try_write32(0x100, 0xDEADBEEF)?; +//! let val =3D window.try_read32(0x100)?; +//! assert_eq!(val, 0xDEADBEEF); +//! +//! Ok(()) +//! } +//! ``` +//! +//! ## Auto-repositioning across VRAM regions +//! +//! ```no_run +//! use crate::driver::Bar0; +//! use crate::mm::pramin; +//! use kernel::devres::Devres; +//! use kernel::prelude::*; +//! use kernel::sync::Arc; +//! +//! fn example(devres_bar: Arc>, vram_region: core::ops::Rang= e) -> Result<()> { +//! let pramin =3D Arc::pin_init(pramin::Pramin::new(devres_bar, vram_= region)?, GFP_KERNEL)?; +//! let mut window =3D pramin.get_window()?; +//! +//! // Access first 1MB region. +//! window.try_write32(0x100, 0x11111111)?; +//! +//! // Access at 2MB - window auto-repositions. +//! window.try_write32(0x200000, 0x22222222)?; +//! +//! // Back to first region - window repositions again. +//! let val =3D window.try_read32(0x100)?; +//! assert_eq!(val, 0x11111111); +//! +//! Ok(()) +//! } +//! ``` + +#![expect(unused)] + +use core::ops::Range; + +use crate::{ + bounded_enum, + driver::Bar0, + num::IntoSafeCast, + regs, // +}; + +use kernel::{ + devres::Devres, + io::Io, + new_mutex, + num::Bounded, + prelude::*, + revocable::RevocableGuard, + sizes::{ + SZ_1M, + SZ_64K, // + }, + sync::{ + lock::mutex::MutexGuard, + Arc, + Mutex, // + }, +}; + +bounded_enum! { + /// Target memory type for the BAR0 window register. + /// + /// Only VRAM is supported; Hopper+ GPUs do not support other targets. + #[derive(Debug)] + pub(crate) enum Bar0WindowTarget with TryFrom> { + /// Video RAM (GPU framebuffer memory). + Vram =3D 0, + } +} + +/// PRAMIN aperture base offset in BAR0. +const PRAMIN_BASE: usize =3D 0x700000; + +/// PRAMIN aperture size (1MB). +const PRAMIN_SIZE: usize =3D SZ_1M; + +/// Generate a PRAMIN read accessor. +macro_rules! define_pramin_read { + ($name:ident, $ty:ty) =3D> { + #[doc =3D concat!("Read a `", stringify!($ty), "` from VRAM at the= given offset.")] + pub(crate) fn $name(&mut self, vram_offset: usize) -> Result<$ty> { + let (bar_offset, new_base) =3D + self.compute_window(vram_offset, ::core::mem::size_of::<$t= y>())?; + + if let Some(base) =3D new_base { + Self::write_window_base(&self.bar, base)?; + *self.state =3D base; + } + self.bar.$name(bar_offset) + } + }; +} + +/// Generate a PRAMIN write accessor. +macro_rules! define_pramin_write { + ($name:ident, $ty:ty) =3D> { + #[doc =3D concat!("Write a `", stringify!($ty), "` to VRAM at the = given offset.")] + pub(crate) fn $name(&mut self, vram_offset: usize, value: $ty) -> = Result { + let (bar_offset, new_base) =3D + self.compute_window(vram_offset, ::core::mem::size_of::<$t= y>())?; + + if let Some(base) =3D new_base { + Self::write_window_base(&self.bar, base)?; + *self.state =3D base; + } + self.bar.$name(value, bar_offset) + } + }; +} + +/// PRAMIN aperture manager. +/// +/// Call [`Pramin::get_window()`] to acquire exclusive PRAMIN access. +#[pin_data] +pub(crate) struct Pramin { + bar: Arc>, + /// Valid VRAM region. Accesses outside this range are rejected. + vram_region: Range, + /// PRAMIN aperture state, protected by a mutex. + /// + /// # Invariants + /// + /// This lock is acquired during the DMA fence signaling critical path. + /// It must NEVER be held across any reclaimable CPU memory / allocati= ons + /// (`GFP_KERNEL`), because the memory reclaim path can call + /// `dma_fence_wait()`, which would deadlock with this lock held. + #[pin] + state: Mutex, +} + +impl Pramin { + /// Create a pin-initializer for PRAMIN. + /// + /// `vram_region` specifies the valid VRAM address range. + pub(crate) fn new( + bar: Arc>, + vram_region: Range, + ) -> Result> { + let bar_access =3D bar.try_access().ok_or(ENODEV)?; + let current_base =3D Self::read_window_base(&bar_access); + + Ok(pin_init!(Self { + bar, + vram_region, + state <- new_mutex!(current_base, "pramin_state"), + })) + } + + /// Acquire exclusive PRAMIN access. + /// + /// Returns a [`PraminWindow`] guard that provides VRAM read/write acc= essors. + /// The [`PraminWindow`] is exclusive and only one can exist at a time. + pub(crate) fn get_window(&self) -> Result> { + let bar =3D self.bar.try_access().ok_or(ENODEV)?; + let state =3D self.state.lock(); + Ok(PraminWindow { + bar, + vram_region: self.vram_region.clone(), + state, + }) + } + + /// Read the current window base from the BAR0_WINDOW register. + fn read_window_base(bar: &Bar0) -> u64 { + let reg =3D bar.read(regs::NV_PBUS_BAR0_WINDOW); + + // TODO: Convert to Bounded when available. + u64::from(reg.window_base()) << 16 + } +} + +/// PRAMIN window guard for direct VRAM access. +/// +/// This guard holds exclusive access to the PRAMIN aperture. The window a= uto-repositions +/// when accessing VRAM offsets outside the current 1MB range. +/// +/// Only one [`PraminWindow`] can exist at a time per [`Pramin`] instance = (enforced by the +/// internal `MutexGuard`). +pub(crate) struct PraminWindow<'a> { + bar: RevocableGuard<'a, Bar0>, + vram_region: Range, + state: MutexGuard<'a, u64>, +} + +impl PraminWindow<'_> { + /// Write a new window base to the BAR0_WINDOW register. + fn write_window_base(bar: &Bar0, base: u64) -> Result { + // CAST: After >> 16, a VRAM address fits in u32. + let window_base =3D (base >> 16) as u32; + bar.write_reg( + regs::NV_PBUS_BAR0_WINDOW::zeroed() + .with_target(Bar0WindowTarget::Vram) + .try_with_window_base(window_base)?, + ); + Ok(()) + } + + /// Compute window parameters for a VRAM access. + /// + /// Returns (`bar_offset`, `new_base`) where: + /// - `bar_offset`: The BAR0 offset to use for the access. + /// - `new_base`: `Some(base)` if window needs repositioning, `None` o= therwise. + fn compute_window( + &self, + vram_offset: usize, + access_size: usize, + ) -> Result<(usize, Option)> { + // Validate VRAM offset is within the valid VRAM region. + let vram_addr =3D vram_offset as u64; + let end_addr =3D vram_addr.checked_add(access_size as u64).ok_or(E= INVAL)?; + if vram_addr < self.vram_region.start || end_addr > self.vram_regi= on.end { + return Err(EINVAL); + } + + // Check if access fits within the current 1MB window. + let current_base =3D *self.state; + if vram_addr >=3D current_base { + let offset_in_window: usize =3D (vram_addr - current_base).int= o_safe_cast(); + if offset_in_window + access_size <=3D PRAMIN_SIZE { + return Ok((PRAMIN_BASE + offset_in_window, None)); + } + } + + // Access doesn't fit in current window - reposition. + // Hardware requires 64KB alignment for the window base register. + let needed_base =3D vram_addr & !(SZ_64K as u64 - 1); + let offset_in_window: usize =3D (vram_addr - needed_base).into_saf= e_cast(); + + // Verify access fits in the 1MB window from the new base. + if offset_in_window + access_size > PRAMIN_SIZE { + return Err(EINVAL); + } + + Ok((PRAMIN_BASE + offset_in_window, Some(needed_base))) + } + + define_pramin_read!(try_read8, u8); + define_pramin_read!(try_read16, u16); + define_pramin_read!(try_read32, u32); + define_pramin_read!(try_read64, u64); + + define_pramin_write!(try_write8, u8); + define_pramin_write!(try_write16, u16); + define_pramin_write!(try_write32, u32); + define_pramin_write!(try_write64, u64); +} diff --git a/drivers/gpu/nova-core/nova_core.rs b/drivers/gpu/nova-core/nov= a_core.rs index 04a1fa6b25f8..5f716d1b8c1c 100644 --- a/drivers/gpu/nova-core/nova_core.rs +++ b/drivers/gpu/nova-core/nova_core.rs @@ -20,6 +20,7 @@ mod gfw; mod gpu; mod gsp; +mod mm; #[macro_use] mod num; mod regs; diff --git a/drivers/gpu/nova-core/regs.rs b/drivers/gpu/nova-core/regs.rs index 2f171a4ff9ba..a3ca02345e20 100644 --- a/drivers/gpu/nova-core/regs.rs +++ b/drivers/gpu/nova-core/regs.rs @@ -30,6 +30,7 @@ Architecture, Chipset, // }, + mm::pramin::Bar0WindowTarget, num::FromSafeCast, }; =20 @@ -115,6 +116,15 @@ fn fmt(&self, f: &mut kernel::fmt::Formatter<'_>) -> k= ernel::fmt::Result { } } =20 +register! { + /// BAR0 window control for PRAMIN access. + pub(crate) NV_PBUS_BAR0_WINDOW(u32) @ 0x00001700 { + 25:24 target ?=3D> Bar0WindowTarget; + /// Window base address (bits 39:16 of FB addr). + 23:0 window_base; + } +} + // PFB =20 register! { --=20 2.34.1