From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from CO1PR03CU002.outbound.protection.outlook.com (mail-westus2azon11010063.outbound.protection.outlook.com [52.101.46.63]) (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 C14A838AC8B; Wed, 15 Apr 2026 21:06:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.46.63 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776287178; cv=fail; b=p65NqdNMM6SGFNHbPtE2eV1XZLSo/qt+dUjll1fJgf/C/mTA3axCgtz0Ykao1FpHXTIuOkb+BJnKweOiv9P2eq+DIQVIisH/m4l8d1rCYrIk9HSJyw56Lo5siANImamziRMdb34glYPREaxXQRIDVAkc2Hb8RY563ZdofcAy1Uw= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776287178; c=relaxed/simple; bh=R2EqN5iZoPqA9E4WGMnKqBVHYtcCGyyZRQ2ag75N7sw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: Content-Type:MIME-Version; b=ksN097I5FZRfWMpd6Ybs7kZLDBhhSmMghzMBH8JYZjx5Fct/Lu2cuqOqtvZK3JiKnZaUtWoPZz1KGv81GA/YmEv4vTPHC+NPWuHHrrkpCD0FH73ZZ+GsanDWLJ2rHe3+xXkQ8hgy0AxgG/qL8UYbKhZfQzMTBsg136abs0BgHbk= 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=aMb64ZDX; arc=fail smtp.client-ip=52.101.46.63 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="aMb64ZDX" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=l8dDkFTMVPJAPIT4VobD9jCRecnDJ/vQRVBLQnao/uFEo+DuQ8bn5ethW3SZFu0/E8P4orJ0yFEv6X6dSJaqGk+Pp9y+aUzrcK+7Fu6rbIvIJtY8NE94kdJsAsTp1sG7EGPjRAf6bslaqLk84NWKgoBd32klIVHgnEPNBM+bTqks6ywfqg3Fug4wcLK9Un2MxEmFOD2cwYk7EJcZ9I90hZyiMPrmxUFswtR1G7Y9a6iBKaOwsyOqRWTmH6Sb5aiJL0p+fCWUIdlMSWELW8blcRd9aGnxuYHP7aGQDafxd45bX+qsI4I6tDzEghRGzoMAto4i/mZtCPU8MC1FFfZegA== 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=jm2eQZVxn+9vxbpNpR4SSwBuG9mTPoY7cqYU2TKfQUQ=; b=H9vhDZF0+3+6SDw8LHNBKGhew+QOD40RBFXh33iL+WBMX2agupQjXHsJ1Rz2H4Akv18ebhdazIVNrX0h66b+6dRic2Gw+n8qSoh0WEzitWbNYQekAdn1VxajX8egk/MZ1IJwymuonqvXYjq8/nNAFtnH9JF6MCLdAj/B60hxusY5S5lFMZHcwh88cxEEG6iJ42wWn6UEqklC26RJuVdgSwShITN1xFoFRH53RvYu7WLw2LheKtkWKrrmlaOvjMSjfGXp/aPJ3v2yFzRmtlUyHwbpR8tT79YSeiop8k+CIX/VgtSgJior0iUjccQgB1u+MaMwgMV+bBKV4zON3dCmBw== 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=jm2eQZVxn+9vxbpNpR4SSwBuG9mTPoY7cqYU2TKfQUQ=; b=aMb64ZDXUgDA5XLyUmGPXhRV3IBgSwPZOJwIe97ZEK/5oh50muN+/LH8zlDAQw9u5YBknbTZtUoIj4wWcEZwtYTb9Zrxbcnq5su8SxwwrCR+XnmaxLsavEShFbd9YlXMtQ+RIY8xwySzo/ZwiARi1vAbnOokl4ERU5IwWpWj1HFihcF7j+8Ohj4OsLoI8qlW5JnNMWyYK6PWOByiVBFE6zDPDW1Io1tU8qG4Nswwev9PX1dm0fTQLlwr9UfxD4qi+ZBLJGY9T9xZom9JxHTS5MGulLMqbYhOu2I0jzCEgGyqaTNkJ46n2sje8n/FWDsPJ9BMd0yCz+F0SaaW/76uxw== 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 SA3PR12MB9177.namprd12.prod.outlook.com (2603:10b6:806:39d::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9818.20; Wed, 15 Apr 2026 21:06:05 +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.9818.017; Wed, 15 Apr 2026 21:06:05 +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 , 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 v11 04/20] gpu: nova-core: mm: Add support to use PRAMIN windows to write to VRAM Date: Wed, 15 Apr 2026 17:05:31 -0400 Message-Id: <20260415210548.3776595-4-joelagnelf@nvidia.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260415210548.3776595-1-joelagnelf@nvidia.com> References: <20260415210548.3776595-1-joelagnelf@nvidia.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: BL0PR02CA0135.namprd02.prod.outlook.com (2603:10b6:208:35::40) To DS0PR12MB6486.namprd12.prod.outlook.com (2603:10b6:8:c5::21) Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS0PR12MB6486:EE_|SA3PR12MB9177:EE_ X-MS-Office365-Filtering-Correlation-Id: 9988d79f-1aa5-4cfe-3539-08de9b32ce7a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|376014|7416014|22082099003|18002099003|11006099003|56012099003; X-Microsoft-Antispam-Message-Info: Yq9UEBs6w5nZgokMMQtPzgZoEc0Qp47bAbjMiPXEjVxZYWNL3h1J5q/71uEO42K8d/XXUVnxh/vwvvPKMAcPaQJKa2PvIu037RwilaKrVJV13Uz/0Fu3rQZFyLmynv0EWYETcHSq7Fyi95mHnkXNgZZwrPy44W5zGYtRnHDx8vC3X2fipwSoHsDaRNAnyAzFnpCxtMxscos9wbuDXDXPDtnZvRkuXxrLNUbCkRucElSykYly98LKSamqMtZ1WolkDLnIMVIIV6ScLb5nY17oMUxxrCXOmxXzQnndmH01eOULqvMO6RLQD2P4aaImwGSo1Mk52boMURAswdg4qEpDKnHaboxY1x8XbNN4XV6eNG+PVbLwR7JZzIDTX8KVj/M7c/V3dd8z+Efq4ztsFK3uPydpBvcoRd7ihrhu8HwYz/CgIZPmD6+VDqoQ3cvzxuSiiT4IsmjzCVQJ8LSlpAC7GBlob8WIuYX2XxFoKeygjbSD9TaP3ND05j0FDCTMZyJ0sYXx+l4WxaZILKcl9td8OUjsk+9DxQkJmUQaimNQXrD2V9Ct5DuwQuz+9sFVnZBciHpvKpb1ZuWwvVQMisPqAGL6aZyVqEsvYToddgJMbWs6f28UrCCIQJT2GL8p4apTa/1oCcWRygagHlk0m1JfGl5meWtgNmr5RVLGobM6oTsBB6jfzyBW2qGj1XdubUKLCm7BDznR49nLQBPgEoVbRcm+qAEtPR0yr83Ggmx1Krk= 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)(366016)(376014)(7416014)(22082099003)(18002099003)(11006099003)(56012099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?yaRASBwkJlM4pa9g/RX3d707e+Y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X-MS-Exchange-CrossTenant-Network-Message-Id: 9988d79f-1aa5-4cfe-3539-08de9b32ce7a X-MS-Exchange-CrossTenant-AuthSource: DS0PR12MB6486.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Apr 2026 21:06:04.9805 (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: jMcI4gxZOOL5TMLZczpGAATUBS7FB9ZvMcpykYBKPebJOlGNFh8Dr289fQFK0wGLwDdXVJkmX7bFmQh1e89rlA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA3PR12MB9177 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..91a0957b2f92 --- /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 synchronization lock. +//! - [`PraminWindow`]: A guard object that holds exclusive PRAMIN access for its lifetime. +//! +//! The PRAMIN aperture is a 1MB region at a fixed offset from BAR0. The window base is +//! controlled by an architecture-specific 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::Range) -> Result<()> { +//! let pramin = Arc::pin_init(pramin::Pramin::new(devres_bar, vram_region)?, GFP_KERNEL)?; +//! let mut window = pramin.get_window()?; +//! +//! // Write and read back. +//! window.try_write32(0x100, 0xDEADBEEF)?; +//! let val = 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::Range) -> Result<()> { +//! let pramin = Arc::pin_init(pramin::Pramin::new(devres_bar, vram_region)?, GFP_KERNEL)?; +//! let mut window = 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 = 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 = 0, + } +} + +/// PRAMIN aperture base offset in BAR0. +const PRAMIN_BASE: usize = 0x700000; + +/// PRAMIN aperture size (1MB). +const PRAMIN_SIZE: usize = SZ_1M; + +/// Generate a PRAMIN read accessor. +macro_rules! define_pramin_read { + ($name:ident, $ty:ty) => { + #[doc = 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) = + self.compute_window(vram_offset, ::core::mem::size_of::<$ty>())?; + + if let Some(base) = new_base { + Self::write_window_base(&self.bar, base)?; + *self.state = base; + } + self.bar.$name(bar_offset) + } + }; +} + +/// Generate a PRAMIN write accessor. +macro_rules! define_pramin_write { + ($name:ident, $ty:ty) => { + #[doc = 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) = + self.compute_window(vram_offset, ::core::mem::size_of::<$ty>())?; + + if let Some(base) = new_base { + Self::write_window_base(&self.bar, base)?; + *self.state = 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 / allocations + /// (`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 = bar.try_access().ok_or(ENODEV)?; + let current_base = 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 accessors. + /// The [`PraminWindow`] is exclusive and only one can exist at a time. + pub(crate) fn get_window(&self) -> Result> { + let bar = self.bar.try_access().ok_or(ENODEV)?; + let state = 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 = 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 auto-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 = (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` otherwise. + 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 = vram_offset as u64; + let end_addr = vram_addr.checked_add(access_size as u64).ok_or(EINVAL)?; + if vram_addr < self.vram_region.start || end_addr > self.vram_region.end { + return Err(EINVAL); + } + + // Check if access fits within the current 1MB window. + let current_base = *self.state; + if vram_addr >= current_base { + let offset_in_window: usize = (vram_addr - current_base).into_safe_cast(); + if offset_in_window + access_size <= 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 = vram_addr & !(SZ_64K as u64 - 1); + let offset_in_window: usize = (vram_addr - needed_base).into_safe_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/nova_core.rs index 3a0c45481a92..d087354f03b9 100644 --- a/drivers/gpu/nova-core/nova_core.rs +++ b/drivers/gpu/nova-core/nova_core.rs @@ -17,6 +17,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, }; @@ -115,6 +116,15 @@ fn fmt(&self, f: &mut kernel::fmt::Formatter<'_>) -> kernel::fmt::Result { } } +register! { + /// BAR0 window control for PRAMIN access. + pub(crate) NV_PBUS_BAR0_WINDOW(u32) @ 0x00001700 { + 25:24 target ?=> Bar0WindowTarget; + /// Window base address (bits 39:16 of FB addr). + 23:0 window_base; + } +} + // PFB register! { -- 2.34.1