gpu: nova-core: Boot GSP to RISC-V active
Subsystems (4)
cover gpu: nova-core: Boot GSP to RISC-V active
This series builds on top of Alex's series[1] to continue initialising the GSP into a state where it becomes active and it starts communicating with the host.
It includes patches to initialise several important data structures required to boot the GSP. The biggest change is the implementation of the command/message circular queue used to establish communication between GSP and host.
This is required to configure and boot the GSP. However this series does not get the GSP to a fully active state. Instead it gets it to a state where the GSP sends a message to the host with a sequence of instructions which need running to get to the active state. A subsequent series will implement processing of this message and allow the GSP to get to the fully active state.
A full tree including the prerequisites for this patch series is available at
https://github.com/apopple-nvidia/linux/tree/nova-core-for-upstream.
[1] - https://lore.kernel.org/rust-for-linux/[email protected]/T/
To: [email protected]
To: Danilo Krummrich <[email protected]>
To: Alexandre Courbot <[email protected]>
Cc: Miguel Ojeda <[email protected]>
Cc: Alex Gaynor <[email protected]>
Cc: Boqun Feng <[email protected]>
Cc: Gary Guo <[email protected]>
Cc: Björn Roy Baron <[email protected]>
Cc: Benno Lossin <[email protected]>
Cc: Andreas Hindborg <[email protected]>
Cc: Alice Ryhl <[email protected]>
Cc: Trevor Gross <[email protected]>
Cc: David Airlie <[email protected]>
Cc: Simona Vetter <[email protected]>
Cc: Maarten Lankhorst <[email protected]>
Cc: Maxime Ripard <[email protected]>
Cc: Thomas Zimmermann <[email protected]>
Cc: John Hubbard <[email protected]>
Cc: Joel Fernandes <[email protected]>
Cc: Timur Tabi <[email protected]>
Cc: [email protected]
Cc: [email protected]
Alistair Popple (7):
gpu: nova-core: Set correct DMA mask
gpu: nova-core: Create initial GspSharedMemObjects
gpu: nova-core: gsp: Create wpr metadata
gpu: nova-core: gsp: Add GSP command queue handling
gpu: nova-core: gsp: Create rmargs
gpu: nova-core: gsp: Create RM registry and sysinfo commands
gpu: nova-core: gsp: Boot GSP
Joel Fernandes (3):
gpu: nova-core: Add a slice-buffer (sbuffer) datastructure
gpu: nova-core: falcon: Add support to check if RISC-V is active
gpu: nova-core: falcon: Add support to write firmware version
drivers/gpu/nova-core/driver.rs | 8 +- drivers/gpu/nova-core/falcon.rs | 16 + drivers/gpu/nova-core/fb.rs | 1 - drivers/gpu/nova-core/firmware.rs | 6 +- drivers/gpu/nova-core/firmware/gsp.rs | 1 - drivers/gpu/nova-core/firmware/riscv.rs | 9 +- drivers/gpu/nova-core/gpu.rs | 60 +- drivers/gpu/nova-core/gsp.rs | 237 +++++- drivers/gpu/nova-core/gsp/cmdq.rs | 701 ++++++++++++++++++ drivers/gpu/nova-core/gsp/commands.rs | 201 +++++ drivers/gpu/nova-core/nova_core.rs | 1 + drivers/gpu/nova-core/nvfw.rs | 59 ++ .../gpu/nova-core/nvfw/r570_144_bindings.rs | 501 +++++++++++++ drivers/gpu/nova-core/regs.rs | 15 + drivers/gpu/nova-core/sbuffer.rs | 188 +++++ 15 files changed, 1991 insertions(+), 13 deletions(-) create mode 100644 drivers/gpu/nova-core/gsp/cmdq.rs create mode 100644 drivers/gpu/nova-core/gsp/commands.rs create mode 100644 drivers/gpu/nova-core/sbuffer.rs
Signature · 1 line
2.47.2
PATCH 1/10 gpu: nova-core: Set correct DMA mask
Set the correct DMA mask. Without this DMA will fail on some setups.
--- drivers/gpu/nova-core/driver.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/nova-core/driver.rs +7 -1
@@ -1,6 +1,9 @@// SPDX-License-Identifier: GPL-2.0 -use kernel::{auxiliary, bindings, c_str, device::Core , pci, prelude::*, sizes::SZ_16M , sync::Arc }; +use kernel::{ + auxiliary, bindings, c_str, device::Core , dma::Device , dma::DmaMask , pci, prelude::*, + sizes::SZ_16M , sync::Arc , +};use crate ::gpu::Gpu ;
@@ -34,6 +37,9 @@ fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> Result<Pin<KBox<Self pdev.enable_device_mem ()?; pdev.set_master (); +// SAFETY: No DMA allocations have been made yet +unsafe { pdev.dma_set_mask_and_coherent (DmaMask ::new ::<48 >())? }; +let bar =Arc ::pin_init ( pdev.iomap_region_sized ::<BAR0_SIZE >(0 ,c_str! ("nova-core/bar0" )), GFP_KERNEL,
Signature · 1 line
2.47.2
Re: [PATCH 01/10] gpu: nova-core: Set correct DMA mask
Quoted 23 lines · Alistair Popple
Set the correct DMA mask. Without this DMA will fail on some setups.
Signed-off-by Alistair Popple <[email protected]>--- drivers/gpu/nova-core/driver.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)drivers/gpu/nova-core/driver.rs +4
-1@@ -1,6 +1,9 @@// SPDX-License-Identifier: GPL-2.0 -use kernel::{auxiliary, bindings, c_str, device::Core , pci, prelude::*, sizes::SZ_16M , sync::Arc }; +use kernel::{ + auxiliary, bindings, c_str, device::Core , dma::Device , dma::DmaMask , pci, prelude::*, + sizes::SZ_16M , sync::Arc , +};use crate ::gpu::Gpu ;
drivers/gpu/nova-core/driver.rs fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> Result<Pin<KBox<Self · line 41
Quoted 5 lines
drivers/gpu/nova-core/driver.rs +2
-0@@ -34,6 +37,9 @@ fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> Result<Pin<KBox<Self pdev.enable_device_mem ()?; pdev.set_master (); +// SAFETY: No DMA allocations have been made yet +unsafe { pdev.dma_set_mask_and_coherent (DmaMask ::new ::<48 >())? };
Eventually, should be 52 bits wide, rather than 48. Or so I believe from looking at various drivers, including Nouveau (which uses 52-bit for Blackwell) and mlx* (which use a 64-bit mask).
However, it works for the current series, because this series only supports Ampere GPUs, and 48 bits suffices for those.
So, you could leave this patch as-is, and I'll change 48 --> 52 in the upcoming Hopper/Blackwell series. Or you can change it now.
Either way is fine, so:
thanks,
Signature · 1 line
John Hubbard
Quoted 4 lines
drivers/gpu/nova-core/driver.rs +1
-0+let bar =Arc ::pin_init ( pdev.iomap_region_sized ::<BAR0_SIZE >(0 ,c_str! ("nova-core/bar0" )), GFP_KERNEL,
Re: [PATCH 01/10] gpu: nova-core: Set correct DMA mask
On 2025-08-30 at 09:55 +1000, John Hubbard <[email protected]> wrote...
Quoted 34 lines · Alistair Popple
Quoted 23 lines
Set the correct DMA mask. Without this DMA will fail on some setups.
Signed-off-by Alistair Popple <[email protected]>--- drivers/gpu/nova-core/driver.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)drivers/gpu/nova-core/driver.rs +4
-1@@ -1,6 +1,9 @@// SPDX-License-Identifier: GPL-2.0 -use kernel::{auxiliary, bindings, c_str, device::Core , pci, prelude::*, sizes::SZ_16M , sync::Arc }; +use kernel::{ + auxiliary, bindings, c_str, device::Core , dma::Device , dma::DmaMask , pci, prelude::*, + sizes::SZ_16M , sync::Arc , +};use crate ::gpu::Gpu ;drivers/gpu/nova-core/driver.rs fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> Result<Pin<KBox<Self · line 41
Quoted 5 lines
drivers/gpu/nova-core/driver.rs +2
-0@@ -34,6 +37,9 @@ fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> Result<Pin<KBox<Self pdev.enable_device_mem ()?; pdev.set_master (); +// SAFETY: No DMA allocations have been made yet +unsafe { pdev.dma_set_mask_and_coherent (DmaMask ::new ::<48 >())? };Eventually, should be 52 bits wide, rather than 48. Or so I believe from looking at various drivers, including Nouveau (which uses 52-bit for Blackwell) and mlx* (which use a 64-bit mask).
Quoted 2 lines
However, it works for the current series, because this series only supports Ampere GPUs, and 48 bits suffices for those.
Actually based on both Nouveau and our internal docs this should be 47-bits. I suspect I just chose 48 during initial bring-up because that's what most CPUs support but neglected to add the TODO to actually go and check this. So will fix for v2.
Quoted 2 lines
So, you could leave this patch as-is, and I'll change 48 --> 52 in the upcoming Hopper/Blackwell series. Or you can change it now.
We can't of course just change this to 52 bits because this needs to reflect what the GPU HW supports. So ideally this needs to come from the HAL. I left this hard-coded because in the short-term leaving it as 47 bits even for Blackwell won't cause any issues. It may force usage of an IOMMU to address physical addresses greater than 47-bits when it otherwise wouldn't for Hopper/Blackwell (it would always have to for Ampere/Turing), but short-term I doubt many systems actually have physical memory above 47-bits anyway.
In other words you could leave this as 47 bits in the upcoming Hopper/Blackwell series or use the HAL we have come up with (if that is available) to obtain the optimal value.
Quoted 4 lines
Either way is fine, so:
Reviewed-by John Hubbard <[email protected]>
Thanks.
Quoted 10 lines
thanks,
Signature · 1 line
John HubbardQuoted 4 lines
drivers/gpu/nova-core/driver.rs +1
-0+let bar =Arc ::pin_init ( pdev.iomap_region_sized ::<BAR0_SIZE >(0 ,c_str! ("nova-core/bar0" )), GFP_KERNEL,
Re: [PATCH 01/10] gpu: nova-core: Set correct DMA mask
Quoted 2 lines · Alistair Popple
On 2025-08-30 at 09:55 +1000, John Hubbard <[email protected]> wrote...
Quoted 1 line · Alistair Popple
...
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
drivers/gpu/nova-core/driver.rs +2
-0+// SAFETY: No DMA allocations have been made yet +unsafe { pdev.dma_set_mask_and_coherent (DmaMask ::new ::<48 >())? };
Quoted 8 lines
Quoted 6 lines
Eventually, should be 52 bits wide, rather than 48. Or so I believe from looking at various drivers, including Nouveau (which uses 52-bit for Blackwell) and mlx* (which use a 64-bit mask).
However, it works for the current series, because this series only supports Ampere GPUs, and 48 bits suffices for those.
Actually based on both Nouveau and our internal docs this should be 47-bits. I
Yes. Which is why I wrote "48 bits suffices".
Quoted 9 lines
suspect I just chose 48 during initial bring-up because that's what most CPUs support but neglected to add the TODO to actually go and check this. So will fix for v2.
Quoted 2 lines
So, you could leave this patch as-is, and I'll change 48 --> 52 in the upcoming Hopper/Blackwell series. Or you can change it now.
We can't of course just change this to 52 bits because this needs to reflect what the GPU HW supports. So ideally this needs to come from the HAL. I left
I should have been more precise. I meant, "use 52 bits, via HAL, just like Nouveau does".
Quoted 9 lines
this hard-coded because in the short-term leaving it as 47 bits even for Blackwell won't cause any issues. It may force usage of an IOMMU to address physical addresses greater than 47-bits when it otherwise wouldn't for Hopper/Blackwell (it would always have to for Ampere/Turing), but short-term I doubt many systems actually have physical memory above 47-bits anyway.
In other words you could leave this as 47 bits in the upcoming Hopper/Blackwell series or use the HAL we have come up with (if that is available) to obtain the optimal value.
Yes. I'm planning to match Nouveau's HAL approach for this, in the upcoming Hopper/Blackwell series.
thanks,
Signature · 1 line
John Hubbard
Re: [PATCH 01/10] gpu: nova-core: Set correct DMA mask
On 2025-09-04 at 05:45 +1000, John Hubbard <[email protected]> wrote...
Quoted 11 lines · Alistair Popple
Quoted 2 lines
On 2025-08-30 at 09:55 +1000, John Hubbard <[email protected]> wrote...
Quoted 1 line · Alistair Popple
...
Quoted 7 lines
Quoted 7 lines
drivers/gpu/nova-core/driver.rs fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> Result<Pin<KBox<Self · line 41
Quoted 2 lines
drivers/gpu/nova-core/driver.rs +2
-0+// SAFETY: No DMA allocations have been made yet +unsafe { pdev.dma_set_mask_and_coherent (DmaMask ::new ::<48 >())? };Eventually, should be 52 bits wide, rather than 48. Or so I believe from looking at various drivers, including Nouveau (which uses 52-bit for Blackwell) and mlx* (which use a 64-bit mask).
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
However, it works for the current series, because this series only supports Ampere GPUs, and 48 bits suffices for those.
Actually based on both Nouveau and our internal docs this should be 47-bits. I
Yes. Which is why I wrote "48 bits suffices".
Sufficies most of the time, but is wrong none the less! Knowing my luck I would run into just such a unicorn system where this would cause problems :-) So thanks for looking at this.
Quoted 4 lines
Quoted 4 lines
suspect I just chose 48 during initial bring-up because that's what most CPUs support but neglected to add the TODO to actually go and check this. So will fix for v2.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
So, you could leave this patch as-is, and I'll change 48 --> 52 in the upcoming Hopper/Blackwell series. Or you can change it now.
We can't of course just change this to 52 bits because this needs to reflect what the GPU HW supports. So ideally this needs to come from the HAL. I left
I should have been more precise. I meant, "use 52 bits, via HAL, just like Nouveau does".
Aha. I wasn't sure so thanks for the clarification.
Quoted 6 lines
Quoted 6 lines
this hard-coded because in the short-term leaving it as 47 bits even for Blackwell won't cause any issues. It may force usage of an IOMMU to address physical addresses greater than 47-bits when it otherwise wouldn't for Hopper/Blackwell (it would always have to for Ampere/Turing), but short-term I doubt many systems actually have physical memory above 47-bits anyway.
Quoted 6 lines
Quoted 3 lines
In other words you could leave this as 47 bits in the upcoming Hopper/Blackwell series or use the HAL we have come up with (if that is available) to obtain the optimal value.
Yes. I'm planning to match Nouveau's HAL approach for this, in the upcoming Hopper/Blackwell series.
Excellent, that sounds perfect.
Quoted 5 lines
thanks,
Signature · 1 line
John Hubbard
PATCH 2/10 gpu: nova-core: Create initial GspSharedMemObjects
In reply to Alistair PoppleThe GSP requires several areas of memory to operate. Each of these have their own simple embedded page tables. Set these up and map them for DMA to/from GSP using CoherentAllocation's. Return the DMA handle describing where each of these regions are for future use when booting GSP.
--- drivers/gpu/nova-core/gpu.rs | 6 + drivers/gpu/nova-core/gsp.rs | 113 ++++++++++++++++++ drivers/gpu/nova-core/nvfw.rs | 7 ++ .../gpu/nova-core/nvfw/r570_144_bindings.rs | 19 +++ 4 files changed, 145 insertions(+)
drivers/gpu/nova-core/gpu.rs +6 -0
@@ -9,6 +9,7 @@use crate ::firmware::fwsec::{FwsecCommand ,FwsecFirmware };use crate ::firmware::{Firmware ,FIRMWARE_VERSION };use crate ::gfw; +use crate ::gsp;use crate ::regs;use crate ::util;use crate ::vbios::Vbios ;
@@ -172,6 +173,7 @@ pub(crate) struct Gpu {/// System memory page required for flushing all pending GPU-side memory writes done through /// PCIE into system memory, via sysmembar (A GPU-initiated HW memory-barrier operation). sysmem_flush: SysmemFlush, + libos: gsp::GspMemObjects, }#[pinned_drop]
@@ -309,11 +311,15 @@ pub(crate) fn new(Self ::run_fwsec_frts (pdev.as_ref (), &gsp_falcon, bar, &bios, &fb_layout)?; +let libos = gsp::GspMemObjects ::new (pdev)?; +let _libos_handle = libos.libos_dma_handle (); +Ok (pin_init! (Self { spec, bar: devres_bar, fw, sysmem_flush, + libos, })) } }
drivers/gpu/nova-core/gsp.rs +113 -0
120 lines not on this page · raw message
drivers/gpu/nova-core/nvfw.rs +7 -0
@@ -40,3 +40,10 @@ pub(crate) struct LibosParams {/// Structure passed to the GSP bootloader, containing the framebuffer layout as well as the DMA /// addresses of the GSP bootloader and firmware. pub (crate )use r570_144::GspFwWprMeta ; + +pub (crate )use r570_144::{ +// LibOS memory structures +LibosMemoryRegionInitArgument , +LibosMemoryRegionKind_LIBOS_MEMORY_REGION_CONTIGUOUS , +LibosMemoryRegionLoc_LIBOS_MEMORY_REGION_LOC_SYSMEM , +};
drivers/gpu/nova-core/nvfw/r570_144_bindings.rs +19 -0
@@ -124,3 +124,22 @@ fn default() -> Self { } } } +pub type LibosAddress =u64_ ; +pub const LibosMemoryRegionKind_LIBOS_MEMORY_REGION_NONE :LibosMemoryRegionKind =0 ; +pub const LibosMemoryRegionKind_LIBOS_MEMORY_REGION_CONTIGUOUS :LibosMemoryRegionKind =1 ; +pub const LibosMemoryRegionKind_LIBOS_MEMORY_REGION_RADIX3 :LibosMemoryRegionKind =2 ; +pub type LibosMemoryRegionKind = ffi::c_uint ; +pub const LibosMemoryRegionLoc_LIBOS_MEMORY_REGION_LOC_NONE :LibosMemoryRegionLoc =0 ; +pub const LibosMemoryRegionLoc_LIBOS_MEMORY_REGION_LOC_SYSMEM :LibosMemoryRegionLoc =1 ; +pub const LibosMemoryRegionLoc_LIBOS_MEMORY_REGION_LOC_FB :LibosMemoryRegionLoc =2 ; +pub type LibosMemoryRegionLoc = ffi::c_uint ; +#[repr( C )] +#[derive( Debug , Default , Copy , Clone )] +pub struct LibosMemoryRegionInitArgument { +pub id8:LibosAddress , +pub pa:LibosAddress , +pub size:LibosAddress , +pub kind:u8_ , +pub loc:u8_ , +pub __bindgen_padding_0: [u8 ;6usize ], +}
Signature · 1 line
2.47.2
PATCH 3/10 gpu: nova-core: gsp: Create wpr metadata
In reply to Alistair PoppleThe GSP requires some pieces of metadata to boot. These are passed in a struct which the GSP transfers via DMA. Create this struct and get a handle to it for future use when booting the GSP.
--- drivers/gpu/nova-core/fb.rs | 1 - drivers/gpu/nova-core/firmware.rs | 2 +- drivers/gpu/nova-core/firmware/gsp.rs | 1 - drivers/gpu/nova-core/firmware/riscv.rs | 6 +- drivers/gpu/nova-core/gpu.rs | 4 +- drivers/gpu/nova-core/gsp.rs | 77 ++++++++++++++++++- drivers/gpu/nova-core/nvfw.rs | 7 ++ .../gpu/nova-core/nvfw/r570_144_bindings.rs | 2 + 8 files changed, 89 insertions(+), 11 deletions(-)
drivers/gpu/nova-core/fb.rs +0 -1
@@ -130,7 +130,6 @@ pub(crate) fn wpr_heap_size(&self, fb_size: u64) -> Result<u64> {/// /// Contains ranges of GPU memory reserved for a given purpose during the GSP boot process. #[derive( Debug )] -#[expect(dead_code)] pub (crate )struct FbLayout {/// Range of the framebuffer. Starts at `0`. pub (crate ) fb:Range <u64 >,
drivers/gpu/nova-core/firmware.rs +1 -1
@@ -126,7 +126,7 @@ pub(crate) struct Firmware {/// GSP firmware. pub gsp: Pin<KBox<GspFirmware>>,/// GSP signatures, to be passed as parameter to the bootloader for validation. - gsp_sigs: DmaObject, + pub gsp_sigs: DmaObject, }impl Firmware {
drivers/gpu/nova-core/firmware/gsp.rs +0 -1
@@ -94,7 +94,6 @@ pub(crate) fn new<'a>( }) } -#[expect(unused)] /// Returns the DMA handle of the level 0 page table. pub (crate )fn lvl0_dma_handle (&self) ->DmaAddress { self.lvl0.dma_handle ()
drivers/gpu/nova-core/firmware/riscv.rs +3 -3
@@ -53,11 +53,11 @@ fn new(bin_fw: &BinFirmware<'_>) -> Result<Self> {#[expect(unused)] pub (crate )struct RiscvFirmware {/// Offset at which the code starts in the firmware image. - code_offset:u32 , +pub code_offset:u32 ,/// Offset at which the data starts in the firmware image. - data_offset:u32 , +pub data_offset:u32 ,/// Offset at which the manifest starts in the firmware image. - manifest_offset:u32 , +pub manifest_offset:u32 ,/// Application version. app_version:u32 ,/// Device-mapped firmware image.
drivers/gpu/nova-core/gpu.rs +3 -1
@@ -13,6 +13,7 @@use crate ::regs;use crate ::util;use crate ::vbios::Vbios ; +use core::fmt;macro_rules! define_chipset {
@@ -311,8 +312,9 @@ pub(crate) fn new(Self ::run_fwsec_frts (pdev.as_ref (), &gsp_falcon, bar, &bios, &fb_layout)?; -let libos = gsp::GspMemObjects ::new (pdev)?; +let libos = gsp::GspMemObjects ::new (pdev, &fw, &fb_layout)?;let _libos_handle = libos.libos_dma_handle (); +let _wpr_handle = libos.wpr_meta.dma_handle (); Ok(pin_init!(Self { spec,
drivers/gpu/nova-core/gsp.rs +73 -4
115 lines not on this page · raw message
drivers/gpu/nova-core/nvfw.rs +7 -0
@@ -46,4 +46,11 @@ pub(crate) struct LibosParams { LibosMemoryRegionInitArgument, LibosMemoryRegionKind_LIBOS_MEMORY_REGION_CONTIGUOUS, LibosMemoryRegionLoc_LIBOS_MEMORY_REGION_LOC_SYSMEM, + +// GSP firmware constants + GSP_FW_WPR_META_MAGIC, + GSP_FW_WPR_META_REVISION, }; + +pub (crate )type GspFwWprMetaBootResumeInfo = r570_144::GspFwWprMeta__bindgen_ty_1 ; +pub (crate )type GspFwWprMetaBootInfo = r570_144::GspFwWprMeta__bindgen_ty_1__bindgen_ty_1 ;
drivers/gpu/nova-core/nvfw/r570_144_bindings.rs +2 -0
@@ -9,6 +9,8 @@pub const GSP_FW_HEAP_SIZE_OVERRIDE_LIBOS2_MAX_MB :u32 =256 ;pub const GSP_FW_HEAP_SIZE_OVERRIDE_LIBOS3_BAREMETAL_MIN_MB :u32 =88 ;pub const GSP_FW_HEAP_SIZE_OVERRIDE_LIBOS3_BAREMETAL_MAX_MB :u32 =280 ; +pub const GSP_FW_WPR_META_REVISION :u32 =1 ; +pub const GSP_FW_WPR_META_MAGIC :i64 = -2577556379034558285 ;pub type __u8 = ffi::c_uchar ;pub type __u16 = ffi::c_ushort ;pub type __u32 = ffi::c_uint ;
Signature · 1 line
2.47.2
Alexandre Courbot
maintainerRe: [PATCH 03/10] gpu: nova-core: gsp: Create wpr metadata
Hi Alistair,
On Wed Aug 27, 2025 at 5:20 PM JST, Alistair Popple wrote:
<snip>
Quoted 33 lines
index
161c057350622..1f51e354b9569100644drivers/gpu/nova-core/gsp.rs +13
-3@@ -6,12 +6,17 @@use kernel::dma_write;use kernel::pci;use kernel::prelude::*; -use kernel::ptr::Alignment ; +use kernel::ptr::{Alignable ,Alignment }; +use kernel::sizes::SZ_128K ;use kernel::transmute::{AsBytes ,FromBytes }; +use crate ::fb::FbLayout ; +use crate ::firmware::Firmware ;use crate ::nvfw::{ -LibosMemoryRegionInitArgument ,LibosMemoryRegionKind_LIBOS_MEMORY_REGION_CONTIGUOUS , -LibosMemoryRegionLoc_LIBOS_MEMORY_REGION_LOC_SYSMEM , +GspFwWprMeta ,GspFwWprMetaBootInfo ,GspFwWprMetaBootResumeInfo ,LibosMemoryRegionInitArgument , +LibosMemoryRegionKind_LIBOS_MEMORY_REGION_CONTIGUOUS , +LibosMemoryRegionLoc_LIBOS_MEMORY_REGION_LOC_SYSMEM ,GSP_FW_WPR_META_MAGIC , +GSP_FW_WPR_META_REVISION , };pub (crate )const GSP_PAGE_SHIFT :usize =12 ;@@ -25,12 +30,69 @@ unsafe impl AsBytes for LibosMemoryRegionInitArgument {}// are valid. unsafe impl FromBytes for LibosMemoryRegionInitArgument {} +// SAFETY: Padding is explicit and will not contain uninitialized data. +unsafe impl AsBytes for GspFwWprMeta {} + +// SAFETY: This struct only contains integer types for which all bit patterns +// are valid.
drivers/gpu/nova-core/gsp.rs unsafe impl AsBytes for LibosMemoryRegionInitArgument {} · line 47
Quoted 10 lines
drivers/gpu/nova-core/gsp.rs +4
-0+unsafe impl FromBytes for GspFwWprMeta {} +#[allow(unused)] pub (crate )struct GspMemObjects { libos:CoherentAllocation <LibosMemoryRegionInitArgument >,pub loginit:CoherentAllocation <u8 >,pub logintr:CoherentAllocation <u8 >,pub logrm:CoherentAllocation <u8 >, +pub wpr_meta:CoherentAllocation <GspFwWprMeta >, +}
I think wpr_meta is a bit out-of-place in this structure. There are several reason for this:
- All the other members of this structure (including cmdq which is added later) are referenced by libos and constitute the GSP runtime:
they are used as long as the GSP is active. wpr_meta, OTOH, does not reference any of the other objects, nor is it referenced by them.
- wpr_meta is never used by the GSP, but needed as a parameter of Booter on SEC2 to load the GSP firmware. It can actually be discarded once this step is completed. This is very different from the rest of this structure, which is used by the GSP.
So I think it doesn't really belong here, and would probably fit better in Firmware. Now the fault lies in my own series, which doesn't let you build wpr_meta easily from there. I'll try to fix that in the v3.
And with the removal of wpr_meta, this structure ends up strictly containing the GSP runtime, including the command queue... Maybe it can simply be named Gsp then? It is even already in the right module! :)
Loosely related, but looking at this series made me realize there is a very logical split of our firmware into two "bundles":
- The GSP bundle includes the GSP runtime data, which is this GspMemObjects structure minus wpr_meta. We pass it as an input parameter to the GSP firmware using the GSP's falcon mbox registers. It must live as long as the GSP is running.
- The SEC2 bundle includes Booter, wpr_meta, the GSP firmware binary, bootloader and its signatures (which are all referenced by wpr_meta). All this data is consumed by SEC2, and crucially can be dropped once the GSP is booted.
This separation is important as currently we are stuffing anything firmware-related into the Firmware struct and keep it there forever, consuming dozens of megabytes of host memory that we could free. Booting the GSP is typically a one-time operation in the life of the GPU device, and even if we ever need to do it again, we can very well build the SEC2 bundle from scratch again.
I will try to reflect the separation better in the v3 of my patchset - then we can just build wpr_meta as a local variable of the method that runs Booter, and drop it (alongside the rest of the SEC2 bundle) upon return.
Quoted 38 lines
drivers/gpu/nova-core/gsp.rs +38
-0+ +pub (crate )fn build_wpr_meta ( + dev: &device::Device <device::Bound >, + fw: &Firmware , + fb_layout: &FbLayout , +) ->Result <CoherentAllocation <GspFwWprMeta >> { +let wpr_meta = +CoherentAllocation ::<GspFwWprMeta >::alloc_coherent (dev,1 ,GFP_KERNEL | __GFP_ZERO)?; +dma_write! ( + wpr_meta[0 ] =GspFwWprMeta { + magic:GSP_FW_WPR_META_MAGIC as u64 , + revision:u64 ::from (GSP_FW_WPR_META_REVISION ), + sysmemAddrOfRadix3Elf: fw.gsp.lvl0_dma_handle (), + sizeOfRadix3Elf: fw.gsp.sizeas u64 , + sysmemAddrOfBootloader: fw.gsp_bootloader.ucode.dma_handle (), + sizeOfBootloader: fw.gsp_bootloader.ucode.size ()as u64 , + bootloaderCodeOffset:u64 ::from (fw.gsp_bootloader.code_offset), + bootloaderDataOffset:u64 ::from (fw.gsp_bootloader.data_offset), + bootloaderManifestOffset:u64 ::from (fw.gsp_bootloader.manifest_offset), + __bindgen_anon_1:GspFwWprMetaBootResumeInfo { + __bindgen_anon_1:GspFwWprMetaBootInfo { + sysmemAddrOfSignature: fw.gsp_sigs.dma_handle (), + sizeOfSignature: fw.gsp_sigs.size ()as u64 , + } + }, + gspFwRsvdStart: fb_layout.heap.start, + nonWprHeapOffset: fb_layout.heap.start, + nonWprHeapSize: fb_layout.heap.end - fb_layout.heap.start, + gspFwWprStart: fb_layout.wpr2.start, + gspFwHeapOffset: fb_layout.wpr2_heap.start, + gspFwHeapSize: fb_layout.wpr2_heap.end - fb_layout.wpr2_heap.start, + gspFwOffset: fb_layout.elf.start, + bootBinOffset: fb_layout.boot.start, + frtsOffset: fb_layout.frts.start, + frtsSize: fb_layout.frts.end - fb_layout.frts.start, + gspFwWprEnd: fb_layout + .vga_workspace + .start
drivers/gpu/nova-core/gsp.rs unsafe impl AsBytes for LibosMemoryRegionInitArgument {} · line 95
Quoted 10 lines
drivers/gpu/nova-core/gsp.rs +10
-0+ .align_down(Alignment::new(SZ_128K)), + gspFwHeapVfPartitionCount: fb_layout.vf_partition_count, + fbSize: fb_layout.fb.end - fb_layout.fb.start, + vgaWorkspaceOffset: fb_layout.vga_workspace.start, + vgaWorkspaceSize: fb_layout.vga_workspace.end - fb_layout.vga_workspace.start, + ..Default ::default () + } + )?; + +Ok (wpr_meta)
I've discussed the bindings abstractions with Danilo last week. We agreed that no layout information should ever escape the nvfw module. I.e. the fields of GspFwWprMeta should not even be visible here.
Instead, GspFwWprMeta should be wrapped privately into another structure inside nvfw:
/// Structure passed to the GSP bootloader, containing the framebuffer layout as well as the DMA /// addresses of the GSP bootloader and firmware.
#[repr(transparent)]
pub(crate) struct GspFwWprMeta(r570_144::GspFwWprMeta);
All its implementations should also be there:
// SAFETY: Padding is explicit and will not contain uninitialized data. unsafe impl AsBytes for GspFwWprMeta {}
// SAFETY: This struct only contains integer types for which all bit patterns // are valid.
unsafe impl FromBytes for GspFwWprMeta {}
And lastly, this new method can also be moved into nvfw, as an impl block for the wrapping GspFwWprMeta type. That way no layout detail escapes that module, and it will be easier to adapt the code to potential layout chances with new firmware versions.
(note that my series is the one carelessly re-exporting GspFwWprMeta as-is - I'll fix that too in v3)
The same applies to LibosMemoryRegionInitArgument of the previous patch, and other types introduced in subsequent patches. Usually there is little more work to do than moving the implentations into nvfw as everything is already abstracted correctly - just not where we eventually want it.
Re: [PATCH 03/10] gpu: nova-core: gsp: Create wpr metadata
On 2025-09-01 at 17:46 +1000, Alexandre Courbot <[email protected]> wrote...
Quoted 51 lines
Hi Alistair,
On Wed Aug 27, 2025 at 5:20 PM JST, Alistair Popple wrote:
<snip>Quoted 33 lines
index
161c057350622..1f51e354b9569100644drivers/gpu/nova-core/gsp.rs +13
-3@@ -6,12 +6,17 @@use kernel::dma_write;use kernel::pci;use kernel::prelude::*; -use kernel::ptr::Alignment ; +use kernel::ptr::{Alignable ,Alignment }; +use kernel::sizes::SZ_128K ;use kernel::transmute::{AsBytes ,FromBytes }; +use crate ::fb::FbLayout ; +use crate ::firmware::Firmware ;use crate ::nvfw::{ -LibosMemoryRegionInitArgument ,LibosMemoryRegionKind_LIBOS_MEMORY_REGION_CONTIGUOUS , -LibosMemoryRegionLoc_LIBOS_MEMORY_REGION_LOC_SYSMEM , +GspFwWprMeta ,GspFwWprMetaBootInfo ,GspFwWprMetaBootResumeInfo ,LibosMemoryRegionInitArgument , +LibosMemoryRegionKind_LIBOS_MEMORY_REGION_CONTIGUOUS , +LibosMemoryRegionLoc_LIBOS_MEMORY_REGION_LOC_SYSMEM ,GSP_FW_WPR_META_MAGIC , +GSP_FW_WPR_META_REVISION , };pub (crate )const GSP_PAGE_SHIFT :usize =12 ;@@ -25,12 +30,69 @@ unsafe impl AsBytes for LibosMemoryRegionInitArgument {}// are valid. unsafe impl FromBytes for LibosMemoryRegionInitArgument {} +// SAFETY: Padding is explicit and will not contain uninitialized data. +unsafe impl AsBytes for GspFwWprMeta {} + +// SAFETY: This struct only contains integer types for which all bit patterns +// are valid. drivers/gpu/nova-core/gsp.rs unsafe impl AsBytes for LibosMemoryRegionInitArgument {} · line 47
Quoted 10 lines
drivers/gpu/nova-core/gsp.rs +4
-0+unsafe impl FromBytes for GspFwWprMeta {} +#[allow(unused)] pub (crate )struct GspMemObjects { libos:CoherentAllocation <LibosMemoryRegionInitArgument >,pub loginit:CoherentAllocation <u8 >,pub logintr:CoherentAllocation <u8 >,pub logrm:CoherentAllocation <u8 >, +pub wpr_meta:CoherentAllocation <GspFwWprMeta >, +}I think
wpr_metais a bit out-of-place in this structure. There are several reason for this:
Quoted 8 lines
- All the other members of this structure (including
cmdqwhich is added later) are referenced bylibosand constitute the GSP runtime:
they are used as long as the GSP is active.wpr_meta, OTOH, does not reference any of the other objects, nor is it referenced by them.
-wpr_metais never used by the GSP, but needed as a parameter of Booter on SEC2 to load the GSP firmware. It can actually be discarded once this step is completed. This is very different from the rest of this structure, which is used by the GSP.
Yes, I had noticed that too and had tried to remove it previously. But as you mention below that was a little bit tricky but if you fix it for v3 I think this all makes perfect sense.
Quoted 7 lines
So I think it doesn't really belong here, and would probably fit better in
Firmware. Now the fault lies in my own series, which doesn't let you buildwpr_metaeasily from there. I'll try to fix that in the v3.And with the removal of
wpr_meta, this structure ends up strictly containing the GSP runtime, including the command queue... Maybe it can simply be namedGspthen? It is even already in the right module! :)
Agreed - I noticed this right after I renamed this struct last time so wanted to let things settle down a bit before doing another rename. But I think Gsp makes a whole lot more sense, especially if we remove the wpr_meta data.
Quoted 77 lines
Loosely related, but looking at this series made me realize there is a very logical split of our firmware into two "bundles":
- The GSP bundle includes the GSP runtime data, which is this
GspMemObjectsstructure minuswpr_meta. We pass it as an input parameter to the GSP firmware using the GSP's falcon mbox registers. It must live as long as the GSP is running.
- The SEC2 bundle includes Booter,wpr_meta, the GSP firmware binary, bootloader and its signatures (which are all referenced bywpr_meta). All this data is consumed by SEC2, and crucially can be dropped once the GSP is booted.This separation is important as currently we are stuffing anything firmware-related into the
Firmwarestruct and keep it there forever, consuming dozens of megabytes of host memory that we could free. Booting the GSP is typically a one-time operation in the life of the GPU device, and even if we ever need to do it again, we can very well build the SEC2 bundle from scratch again.I will try to reflect the separation better in the v3 of my patchset - then we can just build
wpr_metaas a local variable of the method that runsBooter, and drop it (alongside the rest of the SEC2 bundle) upon return.Quoted 38 lines
drivers/gpu/nova-core/gsp.rs +38
-0+ +pub (crate )fn build_wpr_meta ( + dev: &device::Device <device::Bound >, + fw: &Firmware , + fb_layout: &FbLayout , +) ->Result <CoherentAllocation <GspFwWprMeta >> { +let wpr_meta = +CoherentAllocation ::<GspFwWprMeta >::alloc_coherent (dev,1 ,GFP_KERNEL | __GFP_ZERO)?; +dma_write! ( + wpr_meta[0 ] =GspFwWprMeta { + magic:GSP_FW_WPR_META_MAGIC as u64 , + revision:u64 ::from (GSP_FW_WPR_META_REVISION ), + sysmemAddrOfRadix3Elf: fw.gsp.lvl0_dma_handle (), + sizeOfRadix3Elf: fw.gsp.sizeas u64 , + sysmemAddrOfBootloader: fw.gsp_bootloader.ucode.dma_handle (), + sizeOfBootloader: fw.gsp_bootloader.ucode.size ()as u64 , + bootloaderCodeOffset:u64 ::from (fw.gsp_bootloader.code_offset), + bootloaderDataOffset:u64 ::from (fw.gsp_bootloader.data_offset), + bootloaderManifestOffset:u64 ::from (fw.gsp_bootloader.manifest_offset), + __bindgen_anon_1:GspFwWprMetaBootResumeInfo { + __bindgen_anon_1:GspFwWprMetaBootInfo { + sysmemAddrOfSignature: fw.gsp_sigs.dma_handle (), + sizeOfSignature: fw.gsp_sigs.size ()as u64 , + } + }, + gspFwRsvdStart: fb_layout.heap.start, + nonWprHeapOffset: fb_layout.heap.start, + nonWprHeapSize: fb_layout.heap.end - fb_layout.heap.start, + gspFwWprStart: fb_layout.wpr2.start, + gspFwHeapOffset: fb_layout.wpr2_heap.start, + gspFwHeapSize: fb_layout.wpr2_heap.end - fb_layout.wpr2_heap.start, + gspFwOffset: fb_layout.elf.start, + bootBinOffset: fb_layout.boot.start, + frtsOffset: fb_layout.frts.start, + frtsSize: fb_layout.frts.end - fb_layout.frts.start, + gspFwWprEnd: fb_layout + .vga_workspace + .startdrivers/gpu/nova-core/gsp.rs unsafe impl AsBytes for LibosMemoryRegionInitArgument {} · line 95
Quoted 10 lines
drivers/gpu/nova-core/gsp.rs +10
-0+ .align_down(Alignment::new(SZ_128K)), + gspFwHeapVfPartitionCount: fb_layout.vf_partition_count, + fbSize: fb_layout.fb.end - fb_layout.fb.start, + vgaWorkspaceOffset: fb_layout.vga_workspace.start, + vgaWorkspaceSize: fb_layout.vga_workspace.end - fb_layout.vga_workspace.start, + ..Default ::default () + } + )?; + +Ok (wpr_meta)I've discussed the bindings abstractions with Danilo last week. We agreed that no layout information should ever escape the
nvfwmodule. I.e. the fields ofGspFwWprMetashould not even be visible here.
Quoted 7 lines
Instead,
GspFwWprMetashould be wrapped privately into another structure insidenvfw:/// Structure passed to the GSP bootloader, containing the framebuffer layout as well as the DMA /// addresses of the GSP bootloader and firmware.
#[repr(transparent)]
pub(crate) structGspFwWprMeta(r570_144::GspFwWprMeta);
I'm a little bit unsure what the advantage of this is? Admittedly I'm not sure I've seen the discussion from last week so I may have missed something but it's not obvious how creating another layer of abstraction is better. How would it help contain any layout changes to nvfw? Supporting any new struct fields for example would almost certainly still require code changes outside nvfw.
My thinking here was that the bindings (at least for GSP) probably want to live in the Gsp crate/module, and the rest of the driver would be isolated from Gsp changes by the public API provided by the Gsp crate/module rather than trying to do that at the binding level. For example the get_gsp_info() command implemented in [1] provides a separate public struct representing what the rest of the driver needs, thus ensuring the implementation specific details of Gsp (such as struct layout) do not leak into the wider nova-core driver.
Quoted 8 lines
All its implementations should also be there:
// SAFETY: Padding is explicit and will not contain uninitialized data. unsafe impl AsBytes for GspFwWprMeta {}
// SAFETY: This struct only contains integer types for which all bit patterns // are valid.
unsafe impl FromBytes for GspFwWprMeta {}
Makes sense.
Quoted 5 lines
And lastly, this
newmethod can also be moved intonvfw, as an impl block for the wrappingGspFwWprMetatype. That way no layout detail escapes that module, and it will be easier to adapt the code to potential layout chances with new firmware versions.
Quoted 8 lines
(note that my series is the one carelessly re-exporting
GspFwWprMetaas-is - I'll fix that too in v3)The same applies to
LibosMemoryRegionInitArgumentof the previous patch, and other types introduced in subsequent patches. Usually there is little more work to do than moving the implentations intonvfwas everything is already abstracted correctly - just not where we eventually want it.
This is where I get a little bit uncomfortable - this doesn't feel right to me. It seems to me moving all these implementations to the bindings would just end up with a significant amount of Gsp code in nvfw.rs rather than in the places that actually use it, making nvfw.rs large and unwieldy and the code more distributed and harder to follow.
And it's all tightly coupled anyway - for example the Gsp boot arguments require some command queue offsets which are all pretty specific to the Gsp implementation. Ie. we can't define some nice public API in the Gsp crate for "getting arguments required for booting Gsp" without that just being "here is a struct containing all the fields that must be packed into the Gsp arguments for this version", which at that point may as well just be the actual struct itself right?
- Alistair
[1] - https://lore.kernel.org/rust-for-linux/[email protected]/
Alexandre Courbot
maintainerRe: [PATCH 03/10] gpu: nova-core: gsp: Create wpr metadata
On Wed Sep 3, 2025 at 5:57 PM JST, Alistair Popple wrote:
<snip>
Quoted 12 lines
Quoted 11 lines
I've discussed the bindings abstractions with Danilo last week. We agreed that no layout information should ever escape the
nvfwmodule. I.e. the fields ofGspFwWprMetashould not even be visible here.Instead,
GspFwWprMetashould be wrapped privately into another structure insidenvfw:/// Structure passed to the GSP bootloader, containing the framebuffer layout as well as the DMA /// addresses of the GSP bootloader and firmware.
#[repr(transparent)]
pub(crate) structGspFwWprMeta(r570_144::GspFwWprMeta);
Quoted 5 lines
I'm a little bit unsure what the advantage of this is? Admittedly I'm not sure I've seen the discussion from last week so I may have missed something but it's not obvious how creating another layer of abstraction is better. How would it help contain any layout changes to nvfw? Supporting any new struct fields for example would almost certainly still require code changes outside nvfw.
It is not as much creating a new abstraction layer as it is controlling where it resides - nicely contained in nvfw or all over the place. This is particularly relevant if we consider that binding abstractions are more likely to require unsafe code, that we will then be able to confine to the nvfw module. As I got reminded in my own series, we don't want unsafe code in regular driver modules.
Even if a new field is added to GspFwWprMeta, there is a good chance that the parameters of its current constructor will cover what we need to initialize it, so the calling code outside of nvfw won't need to change. Of course we cannot guarantee this will be true all the time, but it still covers us better than the alternative.
And then there is the question of if/when we need to support several firmware versions. If we start having code in gsp that is specific to a given firmware version, this is already a non-starter. Whereas having all the abstractions in a single module leaves us in a better position to use trait objects and virtual calls, or apply proc-macro magic.
Quoted 34 lines
My thinking here was that the bindings (at least for GSP) probably want to live in the Gsp crate/module, and the rest of the driver would be isolated from Gsp changes by the public API provided by the Gsp crate/module rather than trying to do that at the binding level. For example the
get_gsp_info()command implemented in [1] provides a separate public struct representing what the rest of the driver needs, thus ensuring the implementation specific details of Gsp (such as struct layout) do not leak into the wider nova-core driver.Quoted 8 lines
All its implementations should also be there:
// SAFETY: Padding is explicit and will not contain uninitialized data. unsafe impl AsBytes for GspFwWprMeta {}
// SAFETY: This struct only contains integer types for which all bit patterns // are valid.
unsafe impl FromBytes for GspFwWprMeta {}Makes sense.
Quoted 5 lines
And lastly, this
newmethod can also be moved intonvfw, as an impl block for the wrappingGspFwWprMetatype. That way no layout detail escapes that module, and it will be easier to adapt the code to potential layout chances with new firmware versions.Quoted 8 lines
(note that my series is the one carelessly re-exporting
GspFwWprMetaas-is - I'll fix that too in v3)The same applies to
LibosMemoryRegionInitArgumentof the previous patch, and other types introduced in subsequent patches. Usually there is little more work to do than moving the implentations intonvfwas everything is already abstracted correctly - just not where we eventually want it.
Quoted 5 lines
This is where I get a little bit uncomfortable - this doesn't feel right to me. It seems to me moving all these implementations to the bindings would just end up with a significant amount of Gsp code in
nvfw.rsrather than in the places that actually use it, makingnvfw.rslarge and unwieldy and the code more distributed and harder to follow.
If we want to split things more logically, I think it's perfectly fine to have e.g. a nvfw/gsp module that contains all the GSP abstractions, another one for the sequencer, etc. As long as all the version-specific bits are contained below nvfw.
Quoted 7 lines
And it's all tightly coupled anyway - for example the Gsp boot arguments require some command queue offsets which are all pretty specific to the Gsp implementation. Ie. we can't define some nice public API in the Gsp crate for "getting arguments required for booting Gsp" without that just being "here is a struct containing all the fields that must be packed into the Gsp arguments for this version", which at that point may as well just be the actual struct itself right?
Which particular structure are you refering to?
Alexandre Courbot
maintainerRe: [PATCH 03/10] gpu: nova-core: gsp: Create wpr metadata
Quoted 8 lines · Alexandre Courbot
Quoted 6 lines
And it's all tightly coupled anyway - for example the Gsp boot arguments require some command queue offsets which are all pretty specific to the Gsp implementation. Ie. we can't define some nice public API in the Gsp crate for "getting arguments required for booting Gsp" without that just being "here is a struct containing all the fields that must be packed into the Gsp arguments for this version", which at that point may as well just be the actual struct itself right?
Which particular structure are you refering to?
Ah, I guess that was about GspArgumentsCached and the message queue's get_cmdq_offsets method. For this I guess we can just have a
fn new(cmdq: &GspCmdq) -> Self
constructor for GspArgumentsCached which grabs the information it needs from the queue and initializes itself. You would need to have this code anyway, it's just a matter of where we put it - inside a function of gsp.rs, or as a reusable (and easily replacable) constructor.
PATCH 4/10 gpu: nova-core: Add a slice-buffer (sbuffer) datastructure
In reply to Alistair PoppleFrom: Joel Fernandes <[email protected]>
A data structure that can be used to write across multiple slices which may be out of order in memory. This lets SBuffer user correctly and safely write out of memory order, without error-prone tracking of pointers/offsets.
let mut buf1 = [0u8; 3];
let mut buf2 = [0u8; 5];
let mut sbuffer = SBuffer::new([&mut buf1[..], &mut buf2[..]]);
let data = b"hellowo";
let result = sbuffer.write(data);
An internal conversion of gsp.rs to use this resulted in a nice -ve delta:gsp.rs: 37 insertions(+), 99 deletions(-)
--- drivers/gpu/nova-core/nova_core.rs | 1 + drivers/gpu/nova-core/sbuffer.rs | 188 +++++++++++++++++++++++++++++ 2 files changed, 189 insertions(+) create mode 100644 drivers/gpu/nova-core/sbuffer.rs
drivers/gpu/nova-core/nova_core.rs +1 -0
@@ -12,6 +12,7 @@mod gsp;mod nvfw;mod regs; +mod sbuffer;mod util;mod vbios;
drivers/gpu/nova-core/sbuffer.rs new file +188 -0
188 lines not on this page · raw message
Signature · 1 line
2.47.2
Alice Ryhl
maintainerRe: [PATCH 04/10] gpu: nova-core: Add a slice-buffer (sbuffer) datastructure
Quoted 14 lines · Alistair Popple
From: Joel Fernandes <[email protected]>
A data structure that can be used to write across multiple slices which may be out of order in memory. This lets SBuffer user correctly and safely write out of memory order, without error-prone tracking of pointers/offsets.
let mut buf1 = [0u8; 3];
let mut buf2 = [0u8; 5];
let mut sbuffer =SBuffer::new([&mut buf1[..], &mut buf2[..]]);let data = b"hellowo";
let result =sbuffer.write(data);
Quoted 6 lines
An internal conversion of
gsp.rsto use this resulted in a nice -ve delta:gsp.rs: 37 insertions(+), 99 deletions(-)Co-developed-by Alistair Popple <[email protected]>Signed-off-by Alistair Popple <[email protected]>Signed-off-by Joel Fernandes <[email protected]>
This seems like duplication of the logic in rust/kernel/iov_iter.rs [1].
Alice
Re: [PATCH 04/10] gpu: nova-core: Add a slice-buffer (sbuffer) datastructure
On 2025-09-07 at 20:54 +1000, Alice Ryhl <[email protected]> wrote...
Quoted 15 lines · Alistair Popple
Quoted 14 lines
From: Joel Fernandes <[email protected]>
A data structure that can be used to write across multiple slices which may be out of order in memory. This lets SBuffer user correctly and safely write out of memory order, without error-prone tracking of pointers/offsets.
let mut buf1 = [0u8; 3];
let mut buf2 = [0u8; 5];
let mut sbuffer =SBuffer::new([&mut buf1[..], &mut buf2[..]]);let data = b"hellowo";
let result =sbuffer.write(data);
Quoted 8 lines
Quoted 6 lines
An internal conversion of
gsp.rsto use this resulted in a nice -ve delta:gsp.rs: 37 insertions(+), 99 deletions(-)Co-developed-by Alistair Popple <[email protected]>Signed-off-by Alistair Popple <[email protected]>Signed-off-by Joel Fernandes <[email protected]>This seems like duplication of the logic in
rust/kernel/iov_iter.rs[1].
Conceptually I guess there is some overlap. The thing that's different here is we don't have any C version of the iovec struct or iov_iter, and AFAICT [1] doesn't provide any way of creating one from within Rust code.
Quoted 3 lines
Alice
Alexandre Courbot
maintainerRe: [PATCH 04/10] gpu: nova-core: Add a slice-buffer (sbuffer) datastructure
Quoted 23 lines · Alistair Popple
On 2025-09-07 at 20:54 +1000, Alice Ryhl <[email protected]> wrote...
Quoted 22 lines · Alistair Popple
Quoted 20 lines
From: Joel Fernandes <[email protected]>
A data structure that can be used to write across multiple slices which may be out of order in memory. This lets SBuffer user correctly and safely write out of memory order, without error-prone tracking of pointers/offsets.
let mut buf1 = [0u8; 3];
let mut buf2 = [0u8; 5];
let mut sbuffer =SBuffer::new([&mut buf1[..], &mut buf2[..]]);let data = b"hellowo";
let result =sbuffer.write(data);An internal conversion of
gsp.rsto use this resulted in a nice -ve delta:gsp.rs: 37 insertions(+), 99 deletions(-)Co-developed-by Alistair Popple <[email protected]>Signed-off-by Alistair Popple <[email protected]>Signed-off-by Joel Fernandes <[email protected]>
Quoted 5 lines
Quoted 1 line
This seems like duplication of the logic in
rust/kernel/iov_iter.rs[1].Conceptually I guess there is some overlap. The thing that's different here is we don't have any C version of the iovec struct or
iov_iter, and AFAICT [1] doesn't provide any way of creating one from within Rust code.
Yup, I was about to ask as well - I am not familiar with the C API, but how can we use it from Rust, using e.g. a pair of slices as the data source/destination? I see that struct iovec also has __user marker for its base, which hints to me that it is not designed to work with kernel data?
Re: [PATCH 04/10] gpu: nova-core: Add a slice-buffer (sbuffer) datastructure
On 2025-09-08 at 21:42 +1000, Alexandre Courbot <[email protected]> wrote...
Quoted 30 lines · Alistair Popple
Quoted 23 lines
On 2025-09-07 at 20:54 +1000, Alice Ryhl <[email protected]> wrote...
Quoted 22 lines · Alistair Popple
Quoted 20 lines
From: Joel Fernandes <[email protected]>
A data structure that can be used to write across multiple slices which may be out of order in memory. This lets SBuffer user correctly and safely write out of memory order, without error-prone tracking of pointers/offsets.
let mut buf1 = [0u8; 3];
let mut buf2 = [0u8; 5];
let mut sbuffer =SBuffer::new([&mut buf1[..], &mut buf2[..]]);let data = b"hellowo";
let result =sbuffer.write(data);An internal conversion of
gsp.rsto use this resulted in a nice -ve delta:gsp.rs: 37 insertions(+), 99 deletions(-)Co-developed-by Alistair Popple <[email protected]>Signed-off-by Alistair Popple <[email protected]>Signed-off-by Joel Fernandes <[email protected]>Quoted 5 lines
Quoted 1 line
This seems like duplication of the logic in
rust/kernel/iov_iter.rs[1].Conceptually I guess there is some overlap. The thing that's different here is we don't have any C version of the iovec struct or
iov_iter, and AFAICT [1] doesn't provide any way of creating one from within Rust code.
Quoted 5 lines
Yup, I was about to ask as well - I am not familiar with the C API, but how can we use it from Rust, using e.g. a pair of slices as the data source/destination? I see that
struct iovecalso has__usermarker for its base, which hints to me that it is not designed to work with kernel data?
There are various flavours of iovec which you can iterate over. For example there is kvec[1] which doesn't have the __user marker. But none of these are what we want because we're not interacting with C code at all here.
[1] - https://elixir.bootlin.com/linux/v6.16.5/source/include/linux/uio.h#L18
PATCH 5/10 gpu: nova-core: gsp: Add GSP command queue handling
In reply to Alistair PoppleThis commit introduces core infrastructure for handling GSP command and message queues in the nova-core driver. The command queue system enables bidirectional communication between the host driver and GSP firmware through a remote message passing interface.
The interface is based on passing serialised data structures over a ring buffer with separate transmit and receive queues. Commands are sent by writing to the CPU transmit queue and waiting for completion via the receive queue.
To ensure safety mutable or immutable (depending on whether it is a send or receive operation) references are taken on the command queue when allocating the message to write/read to. This ensures message memory remains valid and the command queue can't be mutated whilst an operation is in progress.
Currently this is only used by the probe() routine and therefore can only used by a single thread of execution. Locking to enable safe access from multiple threads will be introduced in a future series when that becomes necessary.
--- drivers/gpu/nova-core/gsp.rs | 20 +- drivers/gpu/nova-core/gsp/cmdq.rs | 695 ++++++++++++++++++ drivers/gpu/nova-core/nvfw.rs | 31 + .../gpu/nova-core/nvfw/r570_144_bindings.rs | 268 +++++++ drivers/gpu/nova-core/regs.rs | 4 + 5 files changed, 1012 insertions(+), 6 deletions(-) create mode 100644 drivers/gpu/nova-core/gsp/cmdq.rs
drivers/gpu/nova-core/gsp.rs +14 -6
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 +use kernel::alloc::flags::GFP_KERNEL ;use kernel::bindings;use kernel::device;use kernel::dma::CoherentAllocation ;
@@ -12,6 +13,7 @@use crate ::fb::FbLayout ;use crate ::firmware::Firmware ; +use crate ::gsp::cmdq::GspCmdq ;use crate ::nvfw::{GspFwWprMeta ,GspFwWprMetaBootInfo ,GspFwWprMetaBootResumeInfo ,LibosMemoryRegionInitArgument ,LibosMemoryRegionKind_LIBOS_MEMORY_REGION_CONTIGUOUS ,
@@ -19,6 +21,8 @@ GSP_FW_WPR_META_REVISION, }; +pub (crate )mod cmdq; +pub (crate )const GSP_PAGE_SHIFT :usize =12 ;pub (crate )const GSP_PAGE_SIZE :usize =1 <<GSP_PAGE_SHIFT ;pub (crate )const GSP_HEAP_ALIGNMENT :Alignment =Alignment ::new (1 <<20 );
@@ -44,6 +48,7 @@ pub(crate) struct GspMemObjects { pub logintr: CoherentAllocation<u8>, pub logrm: CoherentAllocation<u8>, pub wpr_meta: CoherentAllocation<GspFwWprMeta>, + pub cmdq: GspCmdq, } pub(crate) fn build_wpr_meta(
@@ -107,7 +112,7 @@ fn id8(name: &str) -> u64 { }/// Creates a self-mapping page table for `obj` at its beginning. -fn create_pte_array (obj: &mut CoherentAllocation <u8 >) { +fn create_pte_array <T :AsBytes +FromBytes >(obj: &mut CoherentAllocation <T >, skip:usize ) {let num_pages = obj.size ().div_ceil (GSP_PAGE_SIZE );let handle = obj.dma_handle ();
@@ -119,7 +124,7 @@ fn create_pte_array(obj: &mut CoherentAllocation<u8>) {// - The allocation size is at least as long as 8 * num_pages as // GSP_PAGE_SIZE is larger than 8 bytes. let ptes =unsafe { -let ptr = obj.start_ptr_mut ().cast ::<u64 >().add (1 ); +let ptr = obj.start_ptr_mut ().cast ::<u64 >().add (skip); core::slice::from_raw_parts_mut (ptr, num_pages) };
@@ -166,20 +171,23 @@ pub(crate) fn new( GFP_KERNEL | __GFP_ZERO, )?;let mut loginit =create_coherent_dma_object ::<u8 >(dev,"LOGINIT" ,0x10000 , &mut libos,0 )?; -create_pte_array (&mut loginit); +create_pte_array (&mut loginit,1 );let mut logintr =create_coherent_dma_object ::<u8 >(dev,"LOGINTR" ,0x10000 , &mut libos,1 )?; -create_pte_array (&mut logintr); +create_pte_array (&mut logintr,1 );let mut logrm =create_coherent_dma_object ::<u8 >(dev,"LOGRM" ,0x10000 , &mut libos,2 )?; -create_pte_array (&mut logrm); - +create_pte_array (&mut logrm,1 );let wpr_meta =build_wpr_meta (dev, fw, fb_layout)?; +// Creates its own PTE array +let cmdq =GspCmdq ::new (dev)?; +Ok (GspMemObjects { libos, loginit, logintr, logrm, wpr_meta, + cmdq, }) }
drivers/gpu/nova-core/gsp/cmdq.rs new file +695 -0
695 lines not on this page · raw message
drivers/gpu/nova-core/nvfw.rs +31 -0
@@ -50,6 +50,37 @@ pub(crate) struct LibosParams {// GSP firmware constants GSP_FW_WPR_META_MAGIC, GSP_FW_WPR_META_REVISION, + +// GSP events + NV_VGPU_MSG_EVENT_GSP_INIT_DONE, + NV_VGPU_MSG_EVENT_GSP_LOCKDOWN_NOTICE, + NV_VGPU_MSG_EVENT_GSP_POST_NOCAT_RECORD, + NV_VGPU_MSG_EVENT_GSP_RUN_CPU_SEQUENCER, + NV_VGPU_MSG_EVENT_MMU_FAULT_QUEUED, + NV_VGPU_MSG_EVENT_OS_ERROR_LOG, + NV_VGPU_MSG_EVENT_POST_EVENT, + NV_VGPU_MSG_EVENT_RC_TRIGGERED, + NV_VGPU_MSG_EVENT_UCODE_LIBOS_PRINT, + +// GSP function calls + NV_VGPU_MSG_FUNCTION_ALLOC_CHANNEL_DMA, + NV_VGPU_MSG_FUNCTION_ALLOC_CTX_DMA, + NV_VGPU_MSG_FUNCTION_ALLOC_DEVICE, + NV_VGPU_MSG_FUNCTION_ALLOC_MEMORY, + NV_VGPU_MSG_FUNCTION_ALLOC_OBJECT, + NV_VGPU_MSG_FUNCTION_ALLOC_ROOT, + NV_VGPU_MSG_FUNCTION_BIND_CTX_DMA, + NV_VGPU_MSG_FUNCTION_FREE, + NV_VGPU_MSG_FUNCTION_GET_GSP_STATIC_INFO, + NV_VGPU_MSG_FUNCTION_GET_STATIC_INFO, + NV_VGPU_MSG_FUNCTION_GSP_INIT_POST_OBJGPU, + NV_VGPU_MSG_FUNCTION_GSP_RM_CONTROL, + NV_VGPU_MSG_FUNCTION_GSP_SET_SYSTEM_INFO, + NV_VGPU_MSG_FUNCTION_LOG, + NV_VGPU_MSG_FUNCTION_MAP_MEMORY, + NV_VGPU_MSG_FUNCTION_NOP, + NV_VGPU_MSG_FUNCTION_SET_GUEST_SYSTEM_INFO, + NV_VGPU_MSG_FUNCTION_SET_REGISTRY, };pub (crate )type GspFwWprMetaBootResumeInfo = r570_144::GspFwWprMeta__bindgen_ty_1 ;
drivers/gpu/nova-core/nvfw/r570_144_bindings.rs +268 -0
274 lines not on this page · raw message
drivers/gpu/nova-core/regs.rs +4 -0
@@ -71,6 +71,10 @@ pub(crate) fn chipset(self) -> Result<Chipset> { 30:30 ecc_mode_enabled as bool; }); +register! (NV_PGSP_QUEUE_HEAD @0x00110c00 { +31 :0 addressas u32 ; +}); +impl NV_PFB_PRI_MMU_LOCAL_MEMORY_RANGE {/// Returns the usable framebuffer size, in bytes. pub (crate )fn usable_fb_size (self) ->u64 {
Signature · 1 line
2.47.2
Re: [PATCH 05/10] gpu: nova-core: gsp: Add GSP command queue handling
On 8/27/25 1:20 AM, Alistair Popple wrote:
...
Hi Alistair,
Not a real review yet, but one thing I noticed on a quick first pass:
Quoted 9 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +9
-0+pub (crate )fn send_cmd_to_gsp (cmd:GspQueueCommand <'_>, bar: &Bar0 ) ->Result { +// Find the start of the message. We could also re-read the HW pointer. +// SAFETY: The command was previously allocated and initialised on the +// queue and is therefore not-NULL and aligned. +let slice_1: &[u8 ] =unsafe { + core::slice::from_raw_parts ( + ptr::from_ref (cmd.msg_header).cast ::<u8 >(), +size_of ::<GspMsgHeader >() +size_of ::<GspRpcHeader >() + cmd.slice_1.len (), + )
drivers/gpu/nova-core/gsp/cmdq.rs line 469
Quoted 10 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +10
-0+ }; + +dev_info! ( + &cmd.cmdq.dev, +"GSP RPC: send: seq# {}, function=0x{:x} ({}), length=0x{:x}\n" , + cmd.cmdq.seq -1 , + cmd.rpc_header.function, +decode_gsp_function (cmd.rpc_header.function), + cmd.rpc_header.length, + );
Let's please make this (and the corresponding receive) a dev_dbg!(). Otherwise the driver is too chatty at INFO log levels.
I suspect that I'm to blame here, because I recall pretty-ing up the output of these, and I probably set dev_info!() at the same time. doh!
...
drivers/gpu/nova-core/gsp/cmdq.rs line 588
Quoted 9 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +9
-0+// Log RPC receive with message type decoding +dev_info! ( + self.dev, +"GSP RPC: receive: seq# {}, function=0x{:x} ({}), length=0x{:x}\n" , + rpc_header.sequence, + rpc_header.function, +decode_gsp_function (rpc_header.function), + rpc_header.length, + );
Here also: please use dev_dbg!() for this one.
thanks,
Signature · 1 line
John Hubbard
Re: [PATCH 05/10] gpu: nova-core: gsp: Add GSP command queue handling
On 2025-08-28 at 06:35 +1000, John Hubbard <[email protected]> wrote...
Quoted 27 lines · Alistair Popple
...
Hi Alistair,
Not a real review yet, but one thing I noticed on a quick first pass:
Quoted 9 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +9
-0+pub (crate )fn send_cmd_to_gsp (cmd:GspQueueCommand <'_>, bar: &Bar0 ) ->Result { +// Find the start of the message. We could also re-read the HW pointer. +// SAFETY: The command was previously allocated and initialised on the +// queue and is therefore not-NULL and aligned. +let slice_1: &[u8 ] =unsafe { + core::slice::from_raw_parts ( + ptr::from_ref (cmd.msg_header).cast ::<u8 >(), +size_of ::<GspMsgHeader >() +size_of ::<GspRpcHeader >() + cmd.slice_1.len (), + )drivers/gpu/nova-core/gsp/cmdq.rs line 469
Quoted 10 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +10
-0+ }; + +dev_info! ( + &cmd.cmdq.dev, +"GSP RPC: send: seq# {}, function=0x{:x} ({}), length=0x{:x}\n" , + cmd.cmdq.seq -1 , + cmd.rpc_header.function, +decode_gsp_function (cmd.rpc_header.function), + cmd.rpc_header.length, + );
Quoted 5 lines
Let's please make this (and the corresponding receive) a dev_dbg!(). Otherwise the driver is too chatty at INFO log levels.
I suspect that I'm to blame here, because I recall pretty-ing up the output of these, and I probably set dev_info!() at the same time. doh!
You probably took "inspiration" from my original pr_info though! So all good, I'm sure there will be a v2 so will clean these up then.
Quoted 18 lines
...
drivers/gpu/nova-core/gsp/cmdq.rs line 588
Quoted 9 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +9
-0+// Log RPC receive with message type decoding +dev_info! ( + self.dev, +"GSP RPC: receive: seq# {}, function=0x{:x} ({}), length=0x{:x}\n" , + rpc_header.sequence, + rpc_header.function, +decode_gsp_function (rpc_header.function), + rpc_header.length, + );Here also: please use dev_dbg!() for this one.
thanks,
Signature · 1 line
John Hubbard
Alexandre Courbot
maintainerRe: [PATCH 05/10] gpu: nova-core: gsp: Add GSP command queue handling
In reply to Alistair PoppleHi Alistair,
Making a pass about the bindings only - I will check the command-queue logic in another one.
Quoted 68 lines · Alistair Popple
This commit introduces core infrastructure for handling GSP command and message queues in the nova-core driver. The command queue system enables bidirectional communication between the host driver and GSP firmware through a remote message passing interface.
The interface is based on passing serialised data structures over a ring buffer with separate transmit and receive queues. Commands are sent by writing to the CPU transmit queue and waiting for completion via the receive queue.
To ensure safety mutable or immutable (depending on whether it is a send or receive operation) references are taken on the command queue when allocating the message to write/read to. This ensures message memory remains valid and the command queue can't be mutated whilst an operation is in progress.
Currently this is only used by the
probe()routine and therefore can only used by a single thread of execution. Locking to enable safe access from multiple threads will be introduced in a future series when that becomes necessary.Signed-off-by Alistair Popple <[email protected]>--- drivers/gpu/nova-core/gsp.rs | 20 +- drivers/gpu/nova-core/gsp/cmdq.rs | 695 ++++++++++++++++++ drivers/gpu/nova-core/nvfw.rs | 31 + .../gpu/nova-core/nvfw/r570_144_bindings.rs | 268 +++++++ drivers/gpu/nova-core/regs.rs | 4 + 5 files changed, 1012 insertions(+), 6 deletions(-) create mode 100644 drivers/gpu/nova-core/gsp/cmdq.rsdrivers/gpu/nova-core/gsp.rs +5
-0@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 +use kernel::alloc::flags::GFP_KERNEL ;use kernel::bindings;use kernel::device;use kernel::dma::CoherentAllocation ;@@ -12,6 +13,7 @@use crate ::fb::FbLayout ;use crate ::firmware::Firmware ; +use crate ::gsp::cmdq::GspCmdq ;use crate ::nvfw::{GspFwWprMeta ,GspFwWprMetaBootInfo ,GspFwWprMetaBootResumeInfo ,LibosMemoryRegionInitArgument ,LibosMemoryRegionKind_LIBOS_MEMORY_REGION_CONTIGUOUS ,@@ -19,6 +21,8 @@ GSP_FW_WPR_META_REVISION, }; +pub (crate )mod cmdq; +pub (crate )const GSP_PAGE_SHIFT :usize =12 ;pub (crate )const GSP_PAGE_SIZE :usize =1 <<GSP_PAGE_SHIFT ;pub (crate )const GSP_HEAP_ALIGNMENT :Alignment =Alignment ::new (1 <<20 );@@ -44,6 +48,7 @@ pub(crate) struct GspMemObjects { pub logintr: CoherentAllocation<u8>, pub logrm: CoherentAllocation<u8>, pub wpr_meta: CoherentAllocation<GspFwWprMeta>, + pub cmdq: GspCmdq, } pub(crate) fn build_wpr_meta(
drivers/gpu/nova-core/gsp.rs fn id8(name: &str) -> u64 { · line 115
Quoted 5 lines
drivers/gpu/nova-core/gsp.rs +1
-1@@ -107,7 +112,7 @@ fn id8(name: &str) -> u64 { }/// Creates a self-mapping page table for `obj` at its beginning. -fn create_pte_array (obj: &mut CoherentAllocation <u8 >) { +fn create_pte_array <T :AsBytes +FromBytes >(obj: &mut CoherentAllocation <T >, skip:usize ) {
I'd move this extra argument to the patch that introduced this function, that way we don't need to update the existing callers in this patch.
Quoted 67 lines
drivers/gpu/nova-core/gsp.rs +8
-5let num_pages = obj.size ().div_ceil (GSP_PAGE_SIZE );let handle = obj.dma_handle ();@@ -119,7 +124,7 @@ fn create_pte_array(obj: &mut CoherentAllocation<u8>) {// - The allocation size is at least as long as 8 * num_pages as // GSP_PAGE_SIZE is larger than 8 bytes. let ptes =unsafe { -let ptr = obj.start_ptr_mut ().cast ::<u64 >().add (1 ); +let ptr = obj.start_ptr_mut ().cast ::<u64 >().add (skip); core::slice::from_raw_parts_mut (ptr, num_pages) };@@ -166,20 +171,23 @@ pub(crate) fn new( GFP_KERNEL | __GFP_ZERO, )?;let mut loginit =create_coherent_dma_object ::<u8 >(dev,"LOGINIT" ,0x10000 , &mut libos,0 )?; -create_pte_array (&mut loginit); +create_pte_array (&mut loginit,1 );let mut logintr =create_coherent_dma_object ::<u8 >(dev,"LOGINTR" ,0x10000 , &mut libos,1 )?; -create_pte_array (&mut logintr); +create_pte_array (&mut logintr,1 );let mut logrm =create_coherent_dma_object ::<u8 >(dev,"LOGRM" ,0x10000 , &mut libos,2 )?; -create_pte_array (&mut logrm); - +create_pte_array (&mut logrm,1 );let wpr_meta =build_wpr_meta (dev, fw, fb_layout)?; +// Creates its own PTE array +let cmdq =GspCmdq ::new (dev)?; +Ok (GspMemObjects { libos, loginit, logintr, logrm, wpr_meta, + cmdq, }) }drivers/gpu/nova-core/gsp/cmdq.rs new file +21
-0@@ -0,0 +1,695 @@ +// SPDX-License-Identifier: GPL-2.0 +use core::mem::offset_of; +use core::ptr; +use core::sync::atomic::{fence,Ordering }; + +use kernel::alloc::flags::GFP_KERNEL ; +use kernel::device; +use kernel::dma::CoherentAllocation ; +use kernel::prelude::*; +use kernel::sync::aref::ARef ; +use kernel::time::Delta ; +use kernel::transmute::{AsBytes ,FromBytes }; +use kernel::{dma_read, dma_write}; + +use crate ::driver::Bar0 ; +use crate ::gsp::create_pte_array; +use crate ::gsp::{GSP_PAGE_SHIFT ,GSP_PAGE_SIZE }; +use crate ::nvfw::{ +NV_VGPU_MSG_EVENT_GSP_INIT_DONE ,NV_VGPU_MSG_EVENT_GSP_LOCKDOWN_NOTICE , +NV_VGPU_MSG_EVENT_GSP_POST_NOCAT_RECORD ,NV_VGPU_MSG_EVENT_GSP_RUN_CPU_SEQUENCER , +NV_VGPU_MSG_EVENT_MMU_FAULT_QUEUED ,NV_VGPU_MSG_EVENT_OS_ERROR_LOG ,
drivers/gpu/nova-core/gsp/cmdq.rs line 31
Quoted 10 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +10
-0+ NV_VGPU_MSG_EVENT_POST_EVENT, NV_VGPU_MSG_EVENT_RC_TRIGGERED, + NV_VGPU_MSG_EVENT_UCODE_LIBOS_PRINT, NV_VGPU_MSG_FUNCTION_ALLOC_CHANNEL_DMA, + NV_VGPU_MSG_FUNCTION_ALLOC_CTX_DMA, NV_VGPU_MSG_FUNCTION_ALLOC_DEVICE, + NV_VGPU_MSG_FUNCTION_ALLOC_MEMORY, NV_VGPU_MSG_FUNCTION_ALLOC_OBJECT, + NV_VGPU_MSG_FUNCTION_ALLOC_ROOT, NV_VGPU_MSG_FUNCTION_BIND_CTX_DMA, NV_VGPU_MSG_FUNCTION_FREE, + NV_VGPU_MSG_FUNCTION_GET_GSP_STATIC_INFO, NV_VGPU_MSG_FUNCTION_GET_STATIC_INFO, + NV_VGPU_MSG_FUNCTION_GSP_INIT_POST_OBJGPU, NV_VGPU_MSG_FUNCTION_GSP_RM_CONTROL, + NV_VGPU_MSG_FUNCTION_GSP_SET_SYSTEM_INFO, NV_VGPU_MSG_FUNCTION_LOG, + NV_VGPU_MSG_FUNCTION_MAP_MEMORY, NV_VGPU_MSG_FUNCTION_NOP, + NV_VGPU_MSG_FUNCTION_SET_GUEST_SYSTEM_INFO, NV_VGPU_MSG_FUNCTION_SET_REGISTRY,
We will want to declare these as a set of enums. :) It will make the import easier (only one type to import), and we can benefit from the goodies that come with having a dedicated type (more on that below).
drivers/gpu/nova-core/gsp/cmdq.rs line 40
Quoted 9 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +9
-0+}; +use crate ::regs::NV_PGSP_QUEUE_HEAD ; +use crate ::sbuffer::SBuffer ; +use crate ::util::wait_on; + +const GSP_COMMAND_TIMEOUT :i64 =5 ; + +pub (crate )trait GspCommandToGsp :Sized { +const FUNCTION :u32 ;
This then could become the right enum type, constraining the values it can take.
Let's also add short doccomments for this new trait.
drivers/gpu/nova-core/gsp/cmdq.rs line 44
Quoted 4 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +4
-0+} + +pub (crate )trait GspMessageFromGsp :Sized { +const FUNCTION :u32 ;
Same here.
Quoted 11 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +11
-0+} + +// This next section contains constants and structures hand-coded from the GSP +// headers We could replace these with bindgen versions, but that's a bit of a +// pain because they basically end up pulling in the world (ie. definitions for +// every rpc method). So for now the hand-coded ones are fine. They are just +// structs so we can easily move to bindgen generated ones if/when we want to. + +// A GSP RPC header +#[repr( C )] +#[derive( Debug , Clone )]
drivers/gpu/nova-core/gsp/cmdq.rs line 65
Quoted 10 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +10
-0+struct GspRpcHeader { + header_version:u32 , + signature:u32 , + length:u32 , + function:u32 , + rpc_result:u32 , + rpc_result_private:u32 , + sequence:u32 , + cpu_rm_gfid:u32 , +}
This is the equivalent of rpc_message_header_v03_00 in OpenRM. The fact it is versioned makes me a bit nervous. :) If the layout change somehow, we are in for a fun night of debugging. This is where having an opaque abstraction built on top of a bindgen-generated type would be
handy: if the layout changes in an incompatible way, when the abstraction would break at compile-time.
I've experimented a bit and we can generate this type while avoiding pulling the world if we just define rpc_generic_union to e.g. u8. I'll share how I did this with you.
drivers/gpu/nova-core/gsp/cmdq.rs line 68
Quoted 3 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +3
-0+ +// SAFETY: These structs don't meet the no-padding requirements of AsBytes but +// that is not a problem because they are not used outside the kernel.
Doesn't it? I don't see any implicit padding in this struct, it is just a series of u32s.
Quoted 7 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +7
-0+unsafe impl AsBytes for GspRpcHeader {} + +// SAFETY: These structs don't meet the no-padding requirements of FromBytes but +// that is not a problem because they are not used outside the kernel. +unsafe impl FromBytes for GspRpcHeader {} + +// A GSP message element header
drivers/gpu/nova-core/gsp/cmdq.rs line 85
Quoted 10 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +10
-0+#[repr( C )] +#[derive( Debug , Clone )] +struct GspMsgHeader { + auth_tag_buffer: [u8 ;16 ], + aad_buffer: [u8 ;16 ], + checksum:u32 , + sequence:u32 , + elem_count:u32 , + pad:u32 , +}
This one is GSP_MSG_QUEUE_ELEMENT in OpenRM - it has a macro and might be a bit trickier to export, but if we can I think we want to do it the same reasons as GspRpcHeader.
Quoted 13 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +13
-0+ +// SAFETY: These structs don't meet the no-padding requirements of AsBytes but +// that is not a problem because they are not used outside the kernel. +unsafe impl AsBytes for GspMsgHeader {} + +// SAFETY: These structs don't meet the no-padding requirements of FromBytes but +// that is not a problem because they are not used outside the kernel. +unsafe impl FromBytes for GspMsgHeader {} + +// These next two structs come from msgq_priv.h. Hopefully the will never +// need updating once the ABI is stabalised. +#[repr( C )] +#[derive( Debug )]
drivers/gpu/nova-core/gsp/cmdq.rs line 108
Quoted 10 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +10
-0+struct MsgqTxHeader { + version:u32 ,// queue version + size:u32 ,// bytes, page aligned + msg_size:u32 ,// entry size, bytes, must be power-of-2, 16 is minimum + msg_count:u32 ,// number of entries in queue + write_ptr:u32 ,// message id of next slot + flags:u32 ,// if set it means "i want to swap RX" + rx_hdr_off:u32 ,// Offset of msgqRxHeader from start of backing store + entry_off:u32 ,// Offset of entries from start of backing store +}
This is OpenRM's msgqTxHeader. It's declaration doesn't look too funny, and the only useful member on the driver side (outside of construction) is write_ptr, so it wraps pretty well into an opaque type that only exposes a couple of methods to read and set write_ptr.
Doing so is valuable for clarity as well as future compatibility, as it clearly shows in a single page of code how the header is used. Here is all the code operating on it, in a single block instead of being spread through this file:
impl MsgqTxHeader {
pub(crate) fn new(msgq_size: u32, msg_count: u32, rx_hdr_offset: u32) -> Self {
Self(bindings::msgqTxHeader {
version: 0,
size: msgq_size,
msgSize: GSP_PAGE_SIZE as u32,
msgCount: msg_count as u32,
writePtr: 0,
flags: 1,
rxHdrOff: rx_hdr_offset,
entryOff: GSP_PAGE_SIZE as u32,
})
}
pub(crate) fn write_ptr(&self) -> u32 {
let ptr = (&self.0.writePtr) as *const u32;
unsafe { ptr.read_volatile() }
}
pub(crate) fn set_write_ptr(&mut self, val: u32) {
let ptr = (&mut self.0.writePtr) as *mut u32;
unsafe { ptr.write_volatile(val) }
}
}drivers/gpu/nova-core/gsp/cmdq.rs line 118
Quoted 10 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +10
-0+ +// SAFETY: These structs don't meet the no-padding requirements of AsBytes but +// that is not a problem because they are not used outside the kernel. +unsafe impl AsBytes for MsgqTxHeader {} + +#[repr( C )] +#[derive( Debug )] +struct MsgqRxHeader { + read_ptr:u32 ,// message id of last message read +}
This is is even simpler than MsgqTxHeader, and can be abstracted equally well.
Quoted 22 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +22
-0+ +/// Number of GSP pages making the Msgq. +const MSGQ_NUM_PAGES :usize =0x3f ; + +#[repr( C , align( 0x1000 ))] +#[derive( Debug )] +struct MsgqData { + data: [[u8 ;GSP_PAGE_SIZE ];MSGQ_NUM_PAGES ], +} + +// Annoyingly there is no real equivalent of #define so we're forced to use a +// literal to specify the alignment above. So check that against the actual GSP +// page size here. +static_assert! (align_of ::<MsgqData >() ==GSP_PAGE_SIZE ); + +// There is no struct defined for this in the open-gpu-kernel-source headers. +// Instead it is defined by code in GspMsgQueuesInit(). +#[repr( C )] +#[derive( Debug )] +struct Msgq { + tx:MsgqTxHeader , + rx:MsgqRxHeader ,
drivers/gpu/nova-core/gsp/cmdq.rs line 150
Quoted 10 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +10
-0+ msgq: MsgqData, +} + +#[repr( C )] +#[derive( Debug )] +struct GspMem { + ptes: [u8 ;GSP_PAGE_SIZE ], + cpuq:Msgq , + gspq:Msgq , +}
... and here is probably where we want to draw the line with generated bindings. I suspect there are definitions for these types in OpenRM, but if we generate bindings for them we won't be able to take advantage of the abstractions we defined before, since bindgen won't know about them.
They also seem very unlikely to change, and we can probably negotiate a stability guarantee for them.
<snip>
Quoted 27 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +27
-0+fn decode_gsp_function (function:u32 ) -> &'staticstr { +match function { +// Common function codes +NV_VGPU_MSG_FUNCTION_NOP =>"NOP" , +NV_VGPU_MSG_FUNCTION_SET_GUEST_SYSTEM_INFO =>"SET_GUEST_SYSTEM_INFO" , +NV_VGPU_MSG_FUNCTION_ALLOC_ROOT =>"ALLOC_ROOT" , +NV_VGPU_MSG_FUNCTION_ALLOC_DEVICE =>"ALLOC_DEVICE" , +NV_VGPU_MSG_FUNCTION_ALLOC_MEMORY =>"ALLOC_MEMORY" , +NV_VGPU_MSG_FUNCTION_ALLOC_CTX_DMA =>"ALLOC_CTX_DMA" , +NV_VGPU_MSG_FUNCTION_ALLOC_CHANNEL_DMA =>"ALLOC_CHANNEL_DMA" , +NV_VGPU_MSG_FUNCTION_MAP_MEMORY =>"MAP_MEMORY" , +NV_VGPU_MSG_FUNCTION_BIND_CTX_DMA =>"BIND_CTX_DMA" , +NV_VGPU_MSG_FUNCTION_ALLOC_OBJECT =>"ALLOC_OBJECT" , +NV_VGPU_MSG_FUNCTION_FREE =>"FREE" , +NV_VGPU_MSG_FUNCTION_LOG =>"LOG" , +NV_VGPU_MSG_FUNCTION_GET_GSP_STATIC_INFO =>"GET_GSP_STATIC_INFO" , +NV_VGPU_MSG_FUNCTION_SET_REGISTRY =>"SET_REGISTRY" , +NV_VGPU_MSG_FUNCTION_GSP_SET_SYSTEM_INFO =>"GSP_SET_SYSTEM_INFO" , +NV_VGPU_MSG_FUNCTION_GSP_INIT_POST_OBJGPU =>"GSP_INIT_POST_OBJGPU" , +NV_VGPU_MSG_FUNCTION_GSP_RM_CONTROL =>"GSP_RM_CONTROL" , +NV_VGPU_MSG_FUNCTION_GET_STATIC_INFO =>"GET_STATIC_INFO" , + +// Event codes +NV_VGPU_MSG_EVENT_GSP_INIT_DONE =>"INIT_DONE" , +NV_VGPU_MSG_EVENT_GSP_RUN_CPU_SEQUENCER =>"RUN_CPU_SEQUENCER" , +NV_VGPU_MSG_EVENT_POST_EVENT =>"POST_EVENT" , +NV_VGPU_MSG_EVENT_RC_TRIGGERED =>"RC_TRIGGERED" ,
drivers/gpu/nova-core/gsp/cmdq.rs line 695
Quoted 10 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +10
-0+ NV_VGPU_MSG_EVENT_MMU_FAULT_QUEUED => "MMU_FAULT_QUEUED", + NV_VGPU_MSG_EVENT_OS_ERROR_LOG => "OS_ERROR_LOG", + NV_VGPU_MSG_EVENT_GSP_POST_NOCAT_RECORD => "NOCAT", + NV_VGPU_MSG_EVENT_GSP_LOCKDOWN_NOTICE => "LOCKDOWN_NOTICE", + NV_VGPU_MSG_EVENT_UCODE_LIBOS_PRINT => "LIBOS_PRINT", + + // Default for unknown codes + _ => "UNKNOWN", + } +}
This can probably be implemented as a as_str method for the enum types discussed above.
Re: [PATCH 05/10] gpu: nova-core: gsp: Add GSP command queue handling
On 2025-09-04 at 14:12 +1000, Alexandre Courbot <[email protected]> wrote...
Quoted 76 lines
Hi Alistair,
Making a pass about the bindings only - I will check the command-queue logic in another one.
Quoted 70 lines · Alistair Popple
This commit introduces core infrastructure for handling GSP command and message queues in the nova-core driver. The command queue system enables bidirectional communication between the host driver and GSP firmware through a remote message passing interface.
The interface is based on passing serialised data structures over a ring buffer with separate transmit and receive queues. Commands are sent by writing to the CPU transmit queue and waiting for completion via the receive queue.
To ensure safety mutable or immutable (depending on whether it is a send or receive operation) references are taken on the command queue when allocating the message to write/read to. This ensures message memory remains valid and the command queue can't be mutated whilst an operation is in progress.
Currently this is only used by the
probe()routine and therefore can only used by a single thread of execution. Locking to enable safe access from multiple threads will be introduced in a future series when that becomes necessary.Signed-off-by Alistair Popple <[email protected]>--- drivers/gpu/nova-core/gsp.rs | 20 +- drivers/gpu/nova-core/gsp/cmdq.rs | 695 ++++++++++++++++++ drivers/gpu/nova-core/nvfw.rs | 31 + .../gpu/nova-core/nvfw/r570_144_bindings.rs | 268 +++++++ drivers/gpu/nova-core/regs.rs | 4 + 5 files changed, 1012 insertions(+), 6 deletions(-) create mode 100644 drivers/gpu/nova-core/gsp/cmdq.rsdrivers/gpu/nova-core/gsp.rs +5
-0@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 +use kernel::alloc::flags::GFP_KERNEL ;use kernel::bindings;use kernel::device;use kernel::dma::CoherentAllocation ;@@ -12,6 +13,7 @@use crate ::fb::FbLayout ;use crate ::firmware::Firmware ; +use crate ::gsp::cmdq::GspCmdq ;use crate ::nvfw::{GspFwWprMeta ,GspFwWprMetaBootInfo ,GspFwWprMetaBootResumeInfo ,LibosMemoryRegionInitArgument ,LibosMemoryRegionKind_LIBOS_MEMORY_REGION_CONTIGUOUS ,@@ -19,6 +21,8 @@ GSP_FW_WPR_META_REVISION, }; +pub (crate )mod cmdq; +pub (crate )const GSP_PAGE_SHIFT :usize =12 ;pub (crate )const GSP_PAGE_SIZE :usize =1 <<GSP_PAGE_SHIFT ;pub (crate )const GSP_HEAP_ALIGNMENT :Alignment =Alignment ::new (1 <<20 );@@ -44,6 +48,7 @@ pub(crate) struct GspMemObjects { pub logintr: CoherentAllocation<u8>, pub logrm: CoherentAllocation<u8>, pub wpr_meta: CoherentAllocation<GspFwWprMeta>, + pub cmdq: GspCmdq, } pub(crate) fn build_wpr_meta(@@ -107,7 +112,7 @@ fn id8(name: &str) -> u64 { }
Quoted 6 lines
drivers/gpu/nova-core/gsp.rs fn id8(name: &str) -> u64 { · line 115
Quoted 3 lines
drivers/gpu/nova-core/gsp.rs +1
-1/// Creates a self-mapping page table for `obj` at its beginning. -fn create_pte_array (obj: &mut CoherentAllocation <u8 >) { +fn create_pte_array <T :AsBytes +FromBytes >(obj: &mut CoherentAllocation <T >, skip:usize ) {I'd move this extra argument to the patch that introduced this function, that way we don't need to update the existing callers in this patch.
Yep, sounds good.
Quoted 73 lines
Quoted 73 lines
drivers/gpu/nova-core/gsp.rs +8
-5let num_pages = obj.size ().div_ceil (GSP_PAGE_SIZE );let handle = obj.dma_handle ();@@ -119,7 +124,7 @@ fn create_pte_array(obj: &mut CoherentAllocation<u8>) {// - The allocation size is at least as long as 8 * num_pages as // GSP_PAGE_SIZE is larger than 8 bytes. let ptes =unsafe { -let ptr = obj.start_ptr_mut ().cast ::<u64 >().add (1 ); +let ptr = obj.start_ptr_mut ().cast ::<u64 >().add (skip); core::slice::from_raw_parts_mut (ptr, num_pages) };@@ -166,20 +171,23 @@ pub(crate) fn new( GFP_KERNEL | __GFP_ZERO, )?;let mut loginit =create_coherent_dma_object ::<u8 >(dev,"LOGINIT" ,0x10000 , &mut libos,0 )?; -create_pte_array (&mut loginit); +create_pte_array (&mut loginit,1 );let mut logintr =create_coherent_dma_object ::<u8 >(dev,"LOGINTR" ,0x10000 , &mut libos,1 )?; -create_pte_array (&mut logintr); +create_pte_array (&mut logintr,1 );let mut logrm =create_coherent_dma_object ::<u8 >(dev,"LOGRM" ,0x10000 , &mut libos,2 )?; -create_pte_array (&mut logrm); - +create_pte_array (&mut logrm,1 );let wpr_meta =build_wpr_meta (dev, fw, fb_layout)?; +// Creates its own PTE array +let cmdq =GspCmdq ::new (dev)?; +Ok (GspMemObjects { libos, loginit, logintr, logrm, wpr_meta, + cmdq, }) }drivers/gpu/nova-core/gsp/cmdq.rs new file +27
-0@@ -0,0 +1,695 @@ +// SPDX-License-Identifier: GPL-2.0 +use core::mem::offset_of; +use core::ptr; +use core::sync::atomic::{fence,Ordering }; + +use kernel::alloc::flags::GFP_KERNEL ; +use kernel::device; +use kernel::dma::CoherentAllocation ; +use kernel::prelude::*; +use kernel::sync::aref::ARef ; +use kernel::time::Delta ; +use kernel::transmute::{AsBytes ,FromBytes }; +use kernel::{dma_read, dma_write}; + +use crate ::driver::Bar0 ; +use crate ::gsp::create_pte_array; +use crate ::gsp::{GSP_PAGE_SHIFT ,GSP_PAGE_SIZE }; +use crate ::nvfw::{ +NV_VGPU_MSG_EVENT_GSP_INIT_DONE ,NV_VGPU_MSG_EVENT_GSP_LOCKDOWN_NOTICE , +NV_VGPU_MSG_EVENT_GSP_POST_NOCAT_RECORD ,NV_VGPU_MSG_EVENT_GSP_RUN_CPU_SEQUENCER , +NV_VGPU_MSG_EVENT_MMU_FAULT_QUEUED ,NV_VGPU_MSG_EVENT_OS_ERROR_LOG , +NV_VGPU_MSG_EVENT_POST_EVENT ,NV_VGPU_MSG_EVENT_RC_TRIGGERED , +NV_VGPU_MSG_EVENT_UCODE_LIBOS_PRINT ,NV_VGPU_MSG_FUNCTION_ALLOC_CHANNEL_DMA , +NV_VGPU_MSG_FUNCTION_ALLOC_CTX_DMA ,NV_VGPU_MSG_FUNCTION_ALLOC_DEVICE , +NV_VGPU_MSG_FUNCTION_ALLOC_MEMORY ,NV_VGPU_MSG_FUNCTION_ALLOC_OBJECT , +NV_VGPU_MSG_FUNCTION_ALLOC_ROOT ,NV_VGPU_MSG_FUNCTION_BIND_CTX_DMA ,NV_VGPU_MSG_FUNCTION_FREE , +NV_VGPU_MSG_FUNCTION_GET_GSP_STATIC_INFO ,NV_VGPU_MSG_FUNCTION_GET_STATIC_INFO ,
Quoted 8 lines
drivers/gpu/nova-core/gsp/cmdq.rs line 31
Quoted 4 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +4
-0+ NV_VGPU_MSG_FUNCTION_GSP_INIT_POST_OBJGPU, NV_VGPU_MSG_FUNCTION_GSP_RM_CONTROL, + NV_VGPU_MSG_FUNCTION_GSP_SET_SYSTEM_INFO, NV_VGPU_MSG_FUNCTION_LOG, + NV_VGPU_MSG_FUNCTION_MAP_MEMORY, NV_VGPU_MSG_FUNCTION_NOP, + NV_VGPU_MSG_FUNCTION_SET_GUEST_SYSTEM_INFO, NV_VGPU_MSG_FUNCTION_SET_REGISTRY,We will want to declare these as a set of enums. :) It will make the import easier (only one type to import), and we can benefit from the goodies that come with having a dedicated type (more on that below).
Yes, previously these were all just imported but an enum would make sense.
Quoted 4 lines
Quoted 4 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +4
-0+}; +use crate ::regs::NV_PGSP_QUEUE_HEAD ; +use crate ::sbuffer::SBuffer ; +use crate ::util::wait_on;
Quoted 8 lines
drivers/gpu/nova-core/gsp/cmdq.rs line 40
Quoted 5 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +5
-0+ +const GSP_COMMAND_TIMEOUT :i64 =5 ; + +pub (crate )trait GspCommandToGsp :Sized { +const FUNCTION :u32 ;This then could become the right enum type, constraining the values it can take.
Makes sense.
Quoted 29 lines
Let's also add short doccomments for this new trait.
drivers/gpu/nova-core/gsp/cmdq.rs line 44
Quoted 4 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +4
-0+} + +pub (crate )trait GspMessageFromGsp :Sized { +const FUNCTION :u32 ;Same here.
Quoted 20 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +20
-0+} + +// This next section contains constants and structures hand-coded from the GSP +// headers We could replace these with bindgen versions, but that's a bit of a +// pain because they basically end up pulling in the world (ie. definitions for +// every rpc method). So for now the hand-coded ones are fine. They are just +// structs so we can easily move to bindgen generated ones if/when we want to. + +// A GSP RPC header +#[repr( C )] +#[derive( Debug , Clone )] +struct GspRpcHeader { + header_version:u32 , + signature:u32 , + length:u32 , + function:u32 , + rpc_result:u32 , + rpc_result_private:u32 , + sequence:u32 , + cpu_rm_gfid:u32 ,
Quoted 8 lines
drivers/gpu/nova-core/gsp/cmdq.rs line 65
Quoted 1 line
drivers/gpu/nova-core/gsp/cmdq.rs new file +1
-0+}This is the equivalent of
rpc_message_header_v03_00in OpenRM. The fact it is versioned makes me a bit nervous. :) If the layout change somehow, we are in for a fun night of debugging. This is where having an opaque abstraction built on top of a bindgen-generated type would be
handy: if the layout changes in an incompatible way, when the abstraction would break at compile-time.
Argh! I guess I wrote that before we were generating the headers and I never thought to go back and change that. Will fix these to use the generated binding.
I will sync up with you offline but I'm not really understanding the point here. If a bindgen generated type changes in some incompatible way wouldn't we already get a compile-time error? And if the bindgen generated type changes, what's to say the rest of the logic hasn't also changed?
Whilst I'm not totally opposed to something like this it just seems premature
- the ABI is supposed to be stabalising and in practice none of the structures we care about appear to have changed in the 3 years since 525.53 was released. So IHMO it would be better to just deal with these changes if (not when) they happen rather than try and create an abstraction now, especially as this is only supposed to become more stable.
Quoted 10 lines
I've experimented a bit and we can generate this type while avoiding pulling the world if we just define
rpc_generic_unionto e.g.u8. I'll share how I did this with you.drivers/gpu/nova-core/gsp/cmdq.rs line 68
Quoted 3 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +3
-0+ +// SAFETY: These structs don't meet the no-padding requirements of AsBytes but +// that is not a problem because they are not used outside the kernel. Doesn't it? I don't see any implicit padding in this struct, it is just a series of u32s.
These safety comments are all wrong and need updating. I switched to using explicit padding (not that it's required here) to ensure we did meet the requirements for AsBytes. Will update for the next version.
Quoted 22 lines
Quoted 7 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +7
-0+unsafe impl AsBytes for GspRpcHeader {} + +// SAFETY: These structs don't meet the no-padding requirements of FromBytes but +// that is not a problem because they are not used outside the kernel. +unsafe impl FromBytes for GspRpcHeader {} + +// A GSP message element header drivers/gpu/nova-core/gsp/cmdq.rs line 85
Quoted 10 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +10
-0+#[repr( C )] +#[derive( Debug , Clone )] +struct GspMsgHeader { + auth_tag_buffer: [u8 ;16 ], + aad_buffer: [u8 ;16 ], + checksum:u32 , + sequence:u32 , + elem_count:u32 , + pad:u32 , +}This one is
GSP_MSG_QUEUE_ELEMENTin OpenRM - it has a macro and might be a bit trickier to export, but if we can I think we want to do it the same reasons asGspRpcHeader.
Quoted 4 lines
drivers/gpu/nova-core/gsp/cmdq.rs line 89
Quoted 4 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +4
-0+ +// SAFETY: These structs don't meet the no-padding requirements of AsBytes but +// that is not a problem because they are not used outside the kernel. +unsafe impl AsBytes for GspMsgHeader {}
These also need fixing up.
Quoted 15 lines
Quoted 15 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +15
-0+// SAFETY: These structs don't meet the no-padding requirements of FromBytes but +// that is not a problem because they are not used outside the kernel. +unsafe impl FromBytes for GspMsgHeader {} + +// These next two structs come from msgq_priv.h. Hopefully the will never +// need updating once the ABI is stabalised. +#[repr( C )] +#[derive( Debug )] +struct MsgqTxHeader { + version:u32 ,// queue version + size:u32 ,// bytes, page aligned + msg_size:u32 ,// entry size, bytes, must be power-of-2, 16 is minimum + msg_count:u32 ,// number of entries in queue + write_ptr:u32 ,// message id of next slot + flags:u32 ,// if set it means "i want to swap RX"
Quoted 8 lines
drivers/gpu/nova-core/gsp/cmdq.rs line 108
Quoted 3 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +3
-0+ rx_hdr_off: u32, // Offset of msgqRxHeader from start of backing store + entry_off: u32, // Offset of entries from start of backing store +}This is OpenRM's
msgqTxHeader. It's declaration doesn't look too funny, and the only useful member on the driver side (outside of construction) iswrite_ptr, so it wraps pretty well into an opaque type that only exposes a couple of methods to read and setwrite_ptr.
Huh. I guess these snuck in for the same reason as the previous ones. Clearly I didn't properly review why I was defining these by hand.
Quoted 24 lines
Doing so is valuable for clarity as well as future compatibility, as it clearly shows in a single page of code how the header is used. Here is all the code operating on it, in a single block instead of being spread through this file:
impl MsgqTxHeader { pub(crate) fn new(msgq_size: u32, msg_count: u32, rx_hdr_offset: u32) -> Self { Self(bindings::msgqTxHeader { version: 0, size: msgq_size, msgSize: GSP_PAGE_SIZE as u32, msgCount: msg_count as u32, writePtr: 0, flags: 1, rxHdrOff: rx_hdr_offset, entryOff: GSP_PAGE_SIZE as u32, }) } pub(crate) fn write_ptr(&self) -> u32 { let ptr = (&self.0.writePtr) as *const u32; unsafe { ptr.read_volatile() }
Quoted 8 lines
}
pub(crate) fn set_write_ptr(&mut self, val: u32) {
let ptr = (&mut self.0.writePtr) as *mut u32;
unsafe { ptr.write_volatile(val) }
}
}Yes, this makes a lot of sense although I'm still not seeing the value of the [repr(transparent)] representation. Hopefully you can explain during out sync up ;)
Quoted 4 lines
drivers/gpu/nova-core/gsp/cmdq.rs line 112
Quoted 4 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +4
-0+ +// SAFETY: These structs don't meet the no-padding requirements of AsBytes but +// that is not a problem because they are not used outside the kernel. +unsafe impl AsBytes for MsgqTxHeader {}
Also needs fixing.
Quoted 38 lines
drivers/gpu/nova-core/gsp/cmdq.rs line 118
Quoted 5 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +5
-0+#[repr( C )] +#[derive( Debug )] +struct MsgqRxHeader { + read_ptr:u32 ,// message id of last message read +}This is is even simpler than
MsgqTxHeader, and can be abstracted equally well.Quoted 29 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +29
-0+ +/// Number of GSP pages making the Msgq. +const MSGQ_NUM_PAGES :usize =0x3f ; + +#[repr( C , align( 0x1000 ))] +#[derive( Debug )] +struct MsgqData { + data: [[u8 ;GSP_PAGE_SIZE ];MSGQ_NUM_PAGES ], +} + +// Annoyingly there is no real equivalent of #define so we're forced to use a +// literal to specify the alignment above. So check that against the actual GSP +// page size here. +static_assert! (align_of ::<MsgqData >() ==GSP_PAGE_SIZE ); + +// There is no struct defined for this in the open-gpu-kernel-source headers. +// Instead it is defined by code in GspMsgQueuesInit(). +#[repr( C )] +#[derive( Debug )] +struct Msgq { + tx:MsgqTxHeader , + rx:MsgqRxHeader , + msgq:MsgqData , +} + +#[repr( C )] +#[derive( Debug )] +struct GspMem { + ptes: [u8 ;GSP_PAGE_SIZE ],
Quoted 8 lines
drivers/gpu/nova-core/gsp/cmdq.rs line 150
Quoted 3 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +3
-0+ cpuq: Msgq, + gspq: Msgq, +}... and here is probably where we want to draw the line with generated bindings. I suspect there are definitions for these types in OpenRM, but if we generate bindings for them we won't be able to take advantage of the abstractions we defined before, since bindgen won't know about them.
Actually I'm pretty sure this was just hardcoded in OpenRM, but agree it's unlikely to change.
Quoted 36 lines
They also seem very unlikely to change, and we can probably negotiate a stability guarantee for them.
<snip>
Quoted 32 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +32
-0+fn decode_gsp_function (function:u32 ) -> &'staticstr { +match function { +// Common function codes +NV_VGPU_MSG_FUNCTION_NOP =>"NOP" , +NV_VGPU_MSG_FUNCTION_SET_GUEST_SYSTEM_INFO =>"SET_GUEST_SYSTEM_INFO" , +NV_VGPU_MSG_FUNCTION_ALLOC_ROOT =>"ALLOC_ROOT" , +NV_VGPU_MSG_FUNCTION_ALLOC_DEVICE =>"ALLOC_DEVICE" , +NV_VGPU_MSG_FUNCTION_ALLOC_MEMORY =>"ALLOC_MEMORY" , +NV_VGPU_MSG_FUNCTION_ALLOC_CTX_DMA =>"ALLOC_CTX_DMA" , +NV_VGPU_MSG_FUNCTION_ALLOC_CHANNEL_DMA =>"ALLOC_CHANNEL_DMA" , +NV_VGPU_MSG_FUNCTION_MAP_MEMORY =>"MAP_MEMORY" , +NV_VGPU_MSG_FUNCTION_BIND_CTX_DMA =>"BIND_CTX_DMA" , +NV_VGPU_MSG_FUNCTION_ALLOC_OBJECT =>"ALLOC_OBJECT" , +NV_VGPU_MSG_FUNCTION_FREE =>"FREE" , +NV_VGPU_MSG_FUNCTION_LOG =>"LOG" , +NV_VGPU_MSG_FUNCTION_GET_GSP_STATIC_INFO =>"GET_GSP_STATIC_INFO" , +NV_VGPU_MSG_FUNCTION_SET_REGISTRY =>"SET_REGISTRY" , +NV_VGPU_MSG_FUNCTION_GSP_SET_SYSTEM_INFO =>"GSP_SET_SYSTEM_INFO" , +NV_VGPU_MSG_FUNCTION_GSP_INIT_POST_OBJGPU =>"GSP_INIT_POST_OBJGPU" , +NV_VGPU_MSG_FUNCTION_GSP_RM_CONTROL =>"GSP_RM_CONTROL" , +NV_VGPU_MSG_FUNCTION_GET_STATIC_INFO =>"GET_STATIC_INFO" , + +// Event codes +NV_VGPU_MSG_EVENT_GSP_INIT_DONE =>"INIT_DONE" , +NV_VGPU_MSG_EVENT_GSP_RUN_CPU_SEQUENCER =>"RUN_CPU_SEQUENCER" , +NV_VGPU_MSG_EVENT_POST_EVENT =>"POST_EVENT" , +NV_VGPU_MSG_EVENT_RC_TRIGGERED =>"RC_TRIGGERED" , +NV_VGPU_MSG_EVENT_MMU_FAULT_QUEUED =>"MMU_FAULT_QUEUED" , +NV_VGPU_MSG_EVENT_OS_ERROR_LOG =>"OS_ERROR_LOG" , +NV_VGPU_MSG_EVENT_GSP_POST_NOCAT_RECORD =>"NOCAT" , +NV_VGPU_MSG_EVENT_GSP_LOCKDOWN_NOTICE =>"LOCKDOWN_NOTICE" , +NV_VGPU_MSG_EVENT_UCODE_LIBOS_PRINT =>"LIBOS_PRINT" ,
Quoted 8 lines
drivers/gpu/nova-core/gsp/cmdq.rs line 695
Quoted 5 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +5
-0+ +// Default for unknown codes + _ => "UNKNOWN", + } +}This can probably be implemented as a
as_strmethod for the enum types discussed above.
Sounds good.
Alexandre Courbot
maintainerRe: [PATCH 05/10] gpu: nova-core: gsp: Add GSP command queue handling
On Thu Sep 4, 2025 at 3:57 PM JST, Alistair Popple wrote:
<snip>
Quoted 37 lines
Quoted 20 lines
Quoted 20 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +20
-0+} + +// This next section contains constants and structures hand-coded from the GSP +// headers We could replace these with bindgen versions, but that's a bit of a +// pain because they basically end up pulling in the world (ie. definitions for +// every rpc method). So for now the hand-coded ones are fine. They are just +// structs so we can easily move to bindgen generated ones if/when we want to. + +// A GSP RPC header +#[repr( C )] +#[derive( Debug , Clone )] +struct GspRpcHeader { + header_version:u32 , + signature:u32 , + length:u32 , + function:u32 , + rpc_result:u32 , + rpc_result_private:u32 , + sequence:u32 , + cpu_rm_gfid:u32 ,Quoted 8 lines
Quoted 1 line
+}
This is the equivalent of
rpc_message_header_v03_00in OpenRM. The fact it is versioned makes me a bit nervous. :) If the layout change somehow, we are in for a fun night of debugging. This is where having an opaque abstraction built on top of a bindgen-generated type would be
handy: if the layout changes in an incompatible way, when the abstraction would break at compile-time.Argh! I guess I wrote that before we were generating the headers and I never thought to go back and change that. Will fix these to use the generated binding.
I will sync up with you offline but I'm not really understanding the point here. If a bindgen generated type changes in some incompatible way wouldn't we already get a compile-time error? And if the bindgen generated type changes, what's to say the rest of the logic hasn't also changed?
Quoted 6 lines
Whilst I'm not totally opposed to something like this it just seems premature
- the ABI is supposed to be stabalising and in practice none of the structures we care about appear to have changed in the 3 years since 525.53 was released. So IHMO it would be better to just deal with these changes if (not when) they happen rather than try and create an abstraction now, especially as this is only supposed to become more stable.
While I also hope we will achieve some level of ABI stability, I want to provision a bit just in case this goal turns out to be too idealistic.
At the moment we are touching bindings internals a bit everywhere in the gsp module. As the driver matures, that trend can only continue - if one day we realize that we need a firmware version abstraction after all, it will get increasingly difficult (and time-consuming) to shoehorn back as time goes. It is much easier to do this from the start.
Also, having a proper abstraction objectively results in better code. I will share the bits I have written with you for testing purposes, but I think you will agree that this makes the GSP module much cleaner, focused on the higher-level operations, while the lower-level code is divided into easy-to-understand methods, right next to the type they manipulate instead of being inlined as part of the sub-logic of another function. Even without the prospect of multiple firmware versions, it is worth doing.
<snip>
Quoted 28 lines
Quoted 28 lines
Doing so is valuable for clarity as well as future compatibility, as it clearly shows in a single page of code how the header is used. Here is all the code operating on it, in a single block instead of being spread through this file:
impl MsgqTxHeader { pub(crate) fn new(msgq_size: u32, msg_count: u32, rx_hdr_offset: u32) -> Self { Self(bindings::msgqTxHeader { version: 0, size: msgq_size, msgSize: GSP_PAGE_SIZE as u32, msgCount: msg_count as u32, writePtr: 0, flags: 1, rxHdrOff: rx_hdr_offset, entryOff: GSP_PAGE_SIZE as u32, }) } pub(crate) fn write_ptr(&self) -> u32 { let ptr = (&self.0.writePtr) as *const u32; unsafe { ptr.read_volatile() } } pub(crate) fn set_write_ptr(&mut self, val: u32) { let ptr = (&mut self.0.writePtr) as *mut u32;
Quoted 7 lines
Quoted 3 lines
unsafe { ptr.write_volatile(val) } } }Yes, this makes a lot of sense although I'm still not seeing the value of the [repr(transparent)] representation. Hopefully you can explain during out sync up ;)
This type is shared with the GSP, so we must ensure that its layout is exactly the same as the C structure it wraps.
Alexandre Courbot
maintainerRe: [PATCH 05/10] gpu: nova-core: gsp: Add GSP command queue handling
In reply to Alistair PoppleHi Alistair,
Here is a second pass on things not directly related to bindings.
One general comment is that we will want more documentation about how the command queue operates; without it it is a bit difficult to understand how things run and who can read or write what. I hope we can improve the last point through the introduction of a few more types/abstraction.
Another thing we need to do is reduce the number of unsafe statements and justify them more strongly. I've seen 22 new unsafe statements in this patch that are not related to the implementation of FromBytes or AsBytes. I'm pretty sure that with the right abstractions in place we can remove most of them, and feel more confident about those that remain.
On Wed Aug 27, 2025 at 5:20 PM JST, Alistair Popple wrote:
<snip>
Quoted 18 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +18
-0+/// Number of GSP pages making the Msgq. +const MSGQ_NUM_PAGES :usize =0x3f ; + +#[repr( C , align( 0x1000 ))] +#[derive( Debug )] +struct MsgqData { + data: [[u8 ;GSP_PAGE_SIZE ];MSGQ_NUM_PAGES ], +} + +// Annoyingly there is no real equivalent of #define so we're forced to use a +// literal to specify the alignment above. So check that against the actual GSP +// page size here. +static_assert! (align_of ::<MsgqData >() ==GSP_PAGE_SIZE ); + +// There is no struct defined for this in the open-gpu-kernel-source headers. +// Instead it is defined by code in GspMsgQueuesInit(). +#[repr( C )] +#[derive( Debug )]
drivers/gpu/nova-core/gsp/cmdq.rs line 147
Quoted 10 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +10
-0+struct Msgq { + tx:MsgqTxHeader , + rx:MsgqRxHeader , + msgq:MsgqData , +} + +#[repr( C )] +#[derive( Debug )] +struct GspMem { + ptes: [u8 ;GSP_PAGE_SIZE ],
This member appeared unused, but then I understand that it is filled by create_pte_array. I'd suggest to change that function so it can operate directly on pte, and also introduce a similar member to the buffer types that also use it so we know what data this operates on.
drivers/gpu/nova-core/gsp/cmdq.rs line 150
Quoted 3 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +3
-0+ cpuq: Msgq, + gspq: Msgq, +}
This is one of the structures that would benefit from being more documented. :) For instance, the usage of cpuq and gspq was a bit different from what I expected.
drivers/gpu/nova-core/gsp/cmdq.rs line 163
Quoted 13 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +13
-0+ +// SAFETY: These structs don't meet the no-padding requirements of AsBytes but +// that is not a problem because they are not used outside the kernel. +unsafe impl AsBytes for GspMem {} + +// SAFETY: These structs don't meet the no-padding requirements of FromBytes but +// that is not a problem because they are not used outside the kernel. +unsafe impl FromBytes for GspMem {} + +pub (crate )struct GspCmdq { + dev:ARef <device::Device >, + msg_count:u32 , + seq:u32 ,
Here as well, some doccomments would be useful. I still don't understand what purpose seq is supposed to fulfil.
Quoted 32 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +32
-0+ gsp_mem: CoherentAllocation<GspMem>, + pub _nr_ptes: u32, +} + +// A reference to a message currently sitting in the GSP command queue. May +// contain two slices as the command queue is a circular buffer which may have +// wrapped. +// +// INVARIANT: The underlying message data cannot change because the struct holds +// a reference to the command queue which prevents command queue manipulation +// until the GspQueueMessage is dropped. +pub (crate )struct GspQueueMessage <'a> { + cmdq: &'amut GspCmdq , + rpc_header: &'aGspRpcHeader , + slice_1: &'a [u8 ], + slice_2:Option <&'a [u8 ]>, +} + +type GspQueueMessageData <'a,M > = (&'aM ,Option <SBuffer <core::array::IntoIter <&'a [u8 ],2 >>>); + +impl <'a>GspQueueMessage <'a> { +#[expect(unused)] +pub (crate )fn try_as <M :GspMessageFromGsp >(&'a self) ->Result <GspQueueMessageData <'a,M >> { +if self.rpc_header.function !=M ::FUNCTION { +return Err (ERANGE ); + } + +// SAFETY: The slice references the cmdq message memory which is +// guaranteed to outlive the returned GspQueueMessageData by the +// invariants of GspQueueMessage and the lifetime 'a. +let msg =unsafe { &*(self.slice_1.as_ptr ().cast ::<M >()) }; +let data = &self.slice_1[size_of ::<M >()..];
drivers/gpu/nova-core/gsp/cmdq.rs line 205
Quoted 10 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +10
-0+let data_size = + self.rpc_header.lengthas usize -size_of ::<GspRpcHeader >() -size_of ::<M >(); +let sbuf =if data_size >0 { +Some (SBuffer ::new_reader ([data, self.slice_2.unwrap_or (&[])])) + }else { +None + }; + +Ok ((msg, sbuf)) + }
For the message queue I think this approach mostly work (we might also want to add a way to match against the messages enum once we have it - this can be a future patch though).
Quoted 70 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +70
-0+ +#[expect(unused)] +pub (crate )fn ack (self) ->Result { + self.cmdq.ack_msg (self.rpc_header.length)?; + +Ok (()) + } +} + +// The same as GspQueueMessage except the fields are mutable for constructing a +// message to the GSP. +pub (crate )struct GspQueueCommand <'a> { + cmdq: &'amut GspCmdq , + msg_header: &'amut GspMsgHeader , + rpc_header: &'amut GspRpcHeader , + slice_1: &'amut [u8 ], + slice_2: &'amut [u8 ], +} + +type GspQueueCommandData <'a,M > = ( + &'amut M , +Option <SBuffer <core::array::IntoIter <&'amut [u8 ],2 >>>, +); + +impl <'a>GspQueueCommand <'a> { +#[expect(unused)] +pub (crate )fn try_as <'b,M :GspCommandToGsp >(&'bmut self) ->GspQueueCommandData <'b,M > { +// SAFETY: The slice references the cmdq message memory which is +// guaranteed to outlive the returned GspQueueCommandData by the +// invariants of GspQueueCommand and the lifetime 'a. +let msg =unsafe { &mut *(self.slice_1.as_mut_ptr ().cast ::<M >()) }; +let data = &mut self.slice_1[size_of ::<M >()..]; +let data_size = + self.rpc_header.lengthas usize -size_of ::<GspRpcHeader >() -size_of ::<M >(); +let sbuf =if data_size >0 { +Some (SBuffer ::new_writer ([data, self.slice_2])) + }else { +None + }; + self.rpc_header.function =M ::FUNCTION ; + + (msg, sbuf) + } + +#[expect(unused)] +pub (crate )fn send_to_gsp (self, bar: &Bar0 ) ->Result { + self.cmdq.wait_for_free_cmd_to_gsp ( +Delta ::from_secs (GSP_COMMAND_TIMEOUT ), + self.rpc_header.lengthas usize +size_of ::<GspMsgHeader >(), + )?; +GspCmdq ::send_cmd_to_gsp (self, bar)?; +Ok (()) + } +} + +impl GspCmdq { +pub (crate )fn new (dev: &device::Device <device::Bound >) ->Result <GspCmdq > { +let mut gsp_mem = +CoherentAllocation ::<GspMem >::alloc_coherent (dev,1 ,GFP_KERNEL | __GFP_ZERO)?; + +let nr_ptes =size_of ::<GspMem >() >>GSP_PAGE_SHIFT ; +build_assert! (nr_ptes *size_of ::<u64 >() <=GSP_PAGE_SIZE ); + +create_pte_array (&mut gsp_mem,0 ); + +const MSGQ_SIZE :u32 =size_of ::<Msgq >()as u32 ; +const MSG_COUNT :u32 = ((MSGQ_SIZE as usize -GSP_PAGE_SIZE ) /GSP_PAGE_SIZE )as u32 ; +const RX_HDR_OFF :u32 =offset_of! (Msgq , rx)as u32 ; +dma_write! ( + gsp_mem[0 ].cpuq.tx =MsgqTxHeader {
drivers/gpu/nova-core/gsp/cmdq.rs line 285
Quoted 10 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +10
-0+ version: 0, + size: MSGQ_SIZE, + entry_off: GSP_PAGE_SIZE as u32, + msg_size: GSP_PAGE_SIZE as u32, + msg_count: MSG_COUNT, + write_ptr: 0, + flags: 1, + rx_hdr_off: RX_HDR_OFF, + } + )?;
I don't see gspq being initialized, is this on purpose? If it is all zeroes, how does the GSP know its size? Or does it assume the same parameters are the cpuq?
Quoted 23 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +23
-0+ +Ok (GspCmdq { + dev: dev.into (), + msg_count:MSG_COUNT , + seq:0 , + gsp_mem, + _nr_ptes: nr_ptesas u32 , + }) + } + +fn cpu_wptr (&self) ->u32 { +// SAFETY: index `0` is valid as `gsp_mem` has been allocated accordingly, thus the access +// cannot fail. +unsafe {dma_read! (self.gsp_mem[0 ].cpuq.tx.write_ptr).unwrap_unchecked () } + } + +fn gsp_rptr (&self) ->u32 { +// SAFETY: index `0` is valid as `gsp_mem` has been allocated accordingly, thus the access +// cannot fail. +unsafe {dma_read! (self.gsp_mem[0 ].gspq.rx.read_ptr).unwrap_unchecked () } + } + +fn cpu_rptr (&self) ->u32 {
drivers/gpu/nova-core/gsp/cmdq.rs line 318
Quoted 10 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +10
-0+// SAFETY: index `0` is valid as `gsp_mem` has been allocated accordingly, thus the access +// cannot fail. +unsafe {dma_read! (self.gsp_mem[0 ].cpuq.rx.read_ptr).unwrap_unchecked () } + } + +fn gsp_wptr (&self) ->u32 { +// SAFETY: index `0` is valid as `gsp_mem` has been allocated accordingly, thus the access +// cannot fail. +unsafe {dma_read! (self.gsp_mem[0 ].gspq.tx.write_ptr).unwrap_unchecked () } + }
Here is an easy trick to reduce the number of unsafe statements: have a method that returns a reference to the gsp_mem (which contains the unsafe part), and have these 4 methods call into it. And voilà, 3 unsafes gone. :)
Quoted 40 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +40
-0+ +// Returns the numbers of pages free for sending an RPC to GSP. +fn free_tx_pages (&self) ->u32 { +let wptr = self.cpu_wptr (); +let rptr = self.gsp_rptr (); +let mut free = rptr + self.msg_count - wptr -1 ; + +if free >= self.msg_count { + free -= self.msg_count; + } + + free + } + +// Returns the number of pages the GSP has written to the queue. +fn used_rx_pages (&self) ->u32 { +let rptr = self.cpu_rptr (); +let wptr = self.gsp_wptr (); +let mut used = wptr + self.msg_count - rptr; +if used >= self.msg_count { + used -= self.msg_count; + } + + used + } + +fn calculate_checksum <T :Iterator <Item =u8 >>(it:T ) ->u32 { +let sum64 = it + .enumerate () + .map (|(idx, byte)| (((idx %8 ) *8 )as u32 , byte)) + .fold (0 , |acc, (rol, byte)| acc ^ u64::from (byte).rotate_left (rol)); + + ((sum64 >>32 )as u32 ) ^ (sum64as u32 ) + } + +pub (crate )fn wait_for_free_cmd_to_gsp (&self, timeout:Delta , size:usize ) ->Result { +wait_on (timeout, || { +if self.free_tx_pages () < size.div_ceil (GSP_PAGE_SIZE )as u32 { +None + }else {
drivers/gpu/nova-core/gsp/cmdq.rs line 368
Quoted 10 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +10
-0+Some (()) + } + }) + } + +#[expect(unused)] +pub (crate )fn alloc_gsp_queue_command <'a>( + &'amut self, + cmd_size:usize , + ) ->Result <GspQueueCommand <'a>> {
The command submission appears to be modeled after the message reception, but I don't think this works very well in this case.
First, the returned GspQueueCommand does not have its function set until try_as is called. But meanwhile it can very well be given to send_cmd_to_gsp, skipping an important step in the submission process. So at the very least I think we should merge both functions, as it doesn't make sense to have one step without the other anyway (and they are always called one after the other).
But maybe we can be even more radical and do the command building and submission in a single function, and remove GspQueueCommand altogether. What I have in mind is a function that reserves the required space in the queue, creates the required mutable reference to the command and (optionally) the writable SBuffer, and then call the passed closure to initialize these two before sending the command. It would be invoked something like this:
send_cmd::<CommandType>(|command, sbuffer| {
// initialize `command` and write required data into `sbuffer`
}, bar)?;This would ensure that no step is left undone, and also match how users will use the command queue anyway, as I don't think we want to reserve space for a command we are not intent on sending. :)
Quoted 54 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +54
-0+const HEADER_SIZE :usize =size_of ::<GspMsgHeader >() +size_of ::<GspRpcHeader >(); +let msg_size =size_of ::<GspMsgHeader >() +size_of ::<GspRpcHeader >() + cmd_size; +if self.free_tx_pages () < msg_size.div_ceil (GSP_PAGE_SIZE )as u32 { +return Err (EAGAIN ); + } +let wptr = self.cpu_wptr ()as usize ; + +// SAFETY: By the invariants of CoherentAllocation gsp_mem.start_ptr_mut() is valid. +let ptr =unsafe { + core::ptr::addr_of_mut!((*self.gsp_mem.start_ptr_mut ()).cpuq.msgq.data[wptr]) + }; + +// SAFETY: ptr points to at least one GSP_PAGE_SIZE bytes of contiguous +// memory which is larger than GspMsgHeader. +let msg_header_slice: &mut [u8 ] = +unsafe { core::slice::from_raw_parts_mut (ptr.cast ::<u8 >(),size_of ::<GspMsgHeader >()) }; + msg_header_slice.fill (0 ); +let msg_header =GspMsgHeader ::from_bytes_mut (msg_header_slice).ok_or (EINVAL )?; + msg_header.auth_tag_buffer = [0 ;16 ]; + msg_header.aad_buffer = [0 ;16 ]; + msg_header.checksum =0 ; + msg_header.sequence = self.seq; + msg_header.elem_count = (HEADER_SIZE + cmd_size).div_ceil (GSP_PAGE_SIZE )as u32 ; + msg_header.pad =0 ; + self.seq +=1 ; + +// SAFETY: ptr points to GSP_PAGE_SIZE bytes of memory which is larger +// than both GspMsgHeader and GspRpcHeader combined. +let rpc_header_slice: &mut [u8 ] =unsafe { + core::slice::from_raw_parts_mut ( + ptr.cast ::<u8 >().add (size_of ::<GspMsgHeader >()), +size_of ::<GspRpcHeader >(), + ) + }; + rpc_header_slice.fill (0 ); +let rpc_header =GspRpcHeader ::from_bytes_mut (rpc_header_slice).ok_or (EINVAL )?; + rpc_header.header_version =0x03000000 ; + rpc_header.signature =0x43505256 ; + rpc_header.length = (size_of ::<GspRpcHeader >() + cmd_size)as u32 ; + rpc_header.rpc_result =0xffffffff ; + rpc_header.rpc_result_private =0xffffffff ; + rpc_header.sequence =0 ; + rpc_header.cpu_rm_gfid =0 ; + +// Number of bytes left before we have to wrap the buffer +let remaining = ((self.msg_countas usize - wptr) <<GSP_PAGE_SHIFT ) -HEADER_SIZE ; + +let (slice_1, slice_2) =if cmd_size <= remaining { +// SAFETY: ptr points to a region of contiguous memory at least +// cmd_size + HEADER_SIZE long. +let slice_1: &mut [u8 ] =unsafe { + core::slice::from_raw_parts_mut (ptr.cast ::<u8 >().add (HEADER_SIZE ), cmd_size) + }; + slice_1.fill (0 );
drivers/gpu/nova-core/gsp/cmdq.rs line 432
Quoted 10 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +10
-0+ (slice_1, &mut []as &mut [u8 ]) + }else { +// SAFETY: ptr points to a region of contiguous memory remaining + +// HEADER_SIZE bytes long. +let slice_1: &mut [u8 ] =unsafe { + core::slice::from_raw_parts_mut (ptr.cast ::<u8 >().add (HEADER_SIZE ), remaining) + }; +// SAFETY: By the invariants of CoherentAllocation gsp_mem.start_ptr_mut() is valid. +let ptr =unsafe { + core::ptr::addr_of_mut!((*self.gsp_mem.start_ptr_mut ()).gspq.msgq.data[0 ])
I'm almost sure this should be cpuq instead of gspq.
drivers/gpu/nova-core/gsp/cmdq.rs line 438
Quoted 6 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +6
-0+ }; +// SAFETY: ptr points to a region of contiguous memory +// self.msg_count GSP_PAGE_SIZE pages long. +let slice_2: &mut [u8 ] = +unsafe { core::slice::from_raw_parts_mut (ptr.cast ::<u8 >(), remaining - cmd_size) }; + slice_1.fill (0 );
... and that you wanted to do slice_2.fill(0) here.
Review
Quoted 2 lines
(quoted diff) +2
-0+ (slice_1, slice_2) + };
Overall the code of this method is quite difficult to follow, and its safety statements almost impossible to verify due to its complexity. It manipulates many things at the same time and this makes typos like the two I highlighted above easy to slip in.
The problems start with the creation of ptr:
let ptr = unsafe {
core::ptr::addr_of_mut!((*self.gsp_mem.start_ptr_mut()).cpuq.msgq.data[wptr])
};For instance, we don't know for sure that the value of wptr is going to be within bounds. We also don't want to check this at runtime every time. I can track where wptr comes from and where it is updated, and indeed if I do that it looks like it is clamped to not go beyond MSGQ_NUM_PAGES, but verifying this is quite some labor and some careless future patch might change that fact, especially since this invariant is not documented.
So we want the range of wptr to be defined as an invariant, and we want that invariant to be easy to verify, and hard to break. This can be done by wrapping the TX header inside a type that hides its members so the write pointer cannot be accidentally changed, and providing methods to control how it is updated:
/// TX header for setting up a command queue with the GSP.
///
/// # Invariants
///
/// [Self::write_ptr] is guaranteed to return a value within the range 0..MSGQ_NUM_PAGES. #[repr(transparent)]
#[derive(Debug)]
struct MsgqTxHeader(nvfw::MsgqTxHeader);
/// SAFETY: MsgqTxHeader does not contain uninitialized bytes and does not have interior mutability. unsafe impl AsBytes for MsgqTxHeader {}
impl MsgqTxHeader {
...
/// # Invariants
///
/// The returned value is contained within the range `0..MSG_NUM_PAGES`.
fn write_ptr(&self) -> u32 {
self.0.write_ptr()
}
/// Advance the write pointer by `elem_count` units, wrapping around the ring buffer if
/// necessary.
fn advance_write_ptr(&mut self, elem_count: u32) {
// INVARIANT: the write pointer is within the range `0..MSGQ_NUM_PAGES`.
let wptr = self.write_ptr().wrapping_add(elem_count) % MSGQ_NUM_PAGES;
self.0.set_write_ptr(wptr);
fence(Ordering::SeqCst);
}
}Here all possible direct accesses to the write pointer are contained within a page of code, and the invariant clearly documents what we can expect for its value. So we can add a BOUNDS: statement to the initialization of ptr invoking that invariant to justify that the access will always be within bounds.
Another problem with ptr is that we create a mutable pointer over the whole memory area shared with the GSP, and then write into it after more casts and pointer arithmetic. How do we convince the reader that the GSP cannot possibly be accessing the memory we are writing into at the same time? I mean, after careful review I don't think the current code does, but how do we demonstrate this simply?
Here again we need to rely on smaller, simple methods and precise invariants.
The ring buffer of a queue is always split into two parts at any time:
one that is owned by the driver (for read or write access depending on the type of the queue), and another one by the GSP. These areas are delimited by the read and write pointers. In the case of the tx queue, the driver has write-only access over the region is owns, and the GSP can only grow that region by moving its own read pointer - so any driver-owned region that we acquire cannot suddenly become invalid due to GSP action.
So our first building block should be a method (of GspMem?) that returns two mutable slices, covering the area of the TX ring buffer currently owned by the driver. This method can rely on that invariant to satisfy the safety requirement of CoherentAllocation::as_slice_mut that no device access is taking place at the same time; that way, it can confidently return clean mutable slices for the callers to use as they want, and that should be the only unsafe statement needed to send a message.
Callers can then check the available space on these slices, call slice::split_at_mut and FromBytes::from_bytes_mut to split the slice further and cast it to the header or command type they need, all that without unsafe operations. And all made possible because we confined the only unsafe operation to a small method which safety is easy to verify.
I'll stop here for now as that's already quite a bit of work. :) But these are the essential points to fix, basically reducing the number of unsafe statements and building our safety context from small, easy to verify methods. Most types declared in cmdq should have at least an impl block defining their interface in simple terms, rather that having their fields manipulated by the types that contain them.
Alexandre Courbot
maintainerRe: [PATCH 05/10] gpu: nova-core: gsp: Add GSP command queue handling
Quoted 30 lines · Alexandre Courbot
Quoted 30 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +30
-0+ +Ok (GspCmdq { + dev: dev.into (), + msg_count:MSG_COUNT , + seq:0 , + gsp_mem, + _nr_ptes: nr_ptesas u32 , + }) + } + +fn cpu_wptr (&self) ->u32 { +// SAFETY: index `0` is valid as `gsp_mem` has been allocated accordingly, thus the access +// cannot fail. +unsafe {dma_read! (self.gsp_mem[0 ].cpuq.tx.write_ptr).unwrap_unchecked () } + } + +fn gsp_rptr (&self) ->u32 { +// SAFETY: index `0` is valid as `gsp_mem` has been allocated accordingly, thus the access +// cannot fail. +unsafe {dma_read! (self.gsp_mem[0 ].gspq.rx.read_ptr).unwrap_unchecked () } + } + +fn cpu_rptr (&self) ->u32 { +// SAFETY: index `0` is valid as `gsp_mem` has been allocated accordingly, thus the access +// cannot fail. +unsafe {dma_read! (self.gsp_mem[0 ].cpuq.rx.read_ptr).unwrap_unchecked () } + } + +fn gsp_wptr (&self) ->u32 { +// SAFETY: index `0` is valid as `gsp_mem` has been allocated accordingly, thus the access
Quoted 8 lines
drivers/gpu/nova-core/gsp/cmdq.rs line 318
Quoted 3 lines
drivers/gpu/nova-core/gsp/cmdq.rs new file +3
-0+// cannot fail. +unsafe {dma_read! (self.gsp_mem[0 ].gspq.tx.write_ptr).unwrap_unchecked () } + }Here is an easy trick to reduce the number of unsafe statements: have a method that returns a reference to the
gsp_mem(which contains the unsafe part), and have these 4 methods call into it. And voilà, 3 unsafes gone. :)
That sentence of mine is so wrong that I feel obligated to come back and fix it before someone thinks this is an acceptable pattern. >_<
A method returning a reference to gsp_mem would still need to be unsafe itself. Some areas of gsp_mem can be modified by the GSP at any time, so it would be the responsibility of the caller to make sure that it doesn't access any area that is currently under GSP ownership.
A better way to do this would be a have methods (safe, this time) that provide references to the areas that are owned by the driver at the time of calling.
PATCH 6/10 gpu: nova-core: gsp: Create rmargs
In reply to Alistair PoppleInitialise the GSP resource manager arguments (rmargs) which provide initialisation parameters to the GSP firmware during boot. The rmargs structure contains arguments to configure the GSP message/command queue location.
These are mapped for coherent DMA and added to the libos data structure for access when booting GSP.
--- drivers/gpu/nova-core/gsp.rs | 44 +++++++++++++++++-- drivers/gpu/nova-core/gsp/cmdq.rs | 13 +++++- drivers/gpu/nova-core/nvfw.rs | 6 +++ .../gpu/nova-core/nvfw/r570_144_bindings.rs | 33 ++++++++++++++ 4 files changed, 91 insertions(+), 5 deletions(-)
drivers/gpu/nova-core/gsp.rs +41 -3
@@ -17,8 +17,9 @@use crate ::nvfw::{GspFwWprMeta ,GspFwWprMetaBootInfo ,GspFwWprMetaBootResumeInfo ,LibosMemoryRegionInitArgument ,LibosMemoryRegionKind_LIBOS_MEMORY_REGION_CONTIGUOUS , -LibosMemoryRegionLoc_LIBOS_MEMORY_REGION_LOC_SYSMEM ,GSP_FW_WPR_META_MAGIC , -GSP_FW_WPR_META_REVISION , +LibosMemoryRegionLoc_LIBOS_MEMORY_REGION_LOC_SYSMEM ,GSP_ARGUMENTS_CACHED , +GSP_FW_WPR_META_MAGIC ,GSP_FW_WPR_META_REVISION ,GSP_SR_INIT_ARGUMENTS , +MESSAGE_QUEUE_INIT_ARGUMENTS , };pub (crate )mod cmdq;
@@ -41,6 +42,19 @@ unsafe impl AsBytes for GspFwWprMeta {}// are valid. unsafe impl FromBytes for GspFwWprMeta {} +// SAFETY: Padding is explicit and will not contain uninitialized data. +unsafe impl AsBytes for GSP_ARGUMENTS_CACHED {} + +// SAFETY: This struct only contains integer types for which all bit patterns +// are valid. +unsafe impl FromBytes for GSP_ARGUMENTS_CACHED {} + +// SAFETY: Padding is explicit and will not contain uninitialized data. +unsafe impl AsBytes for MESSAGE_QUEUE_INIT_ARGUMENTS {} + +// SAFETY: Padding is explicit and will not contain uninitialized data. +unsafe impl AsBytes for GSP_SR_INIT_ARGUMENTS {} +#[allow(unused)] pub (crate )struct GspMemObjects { libos:CoherentAllocation <LibosMemoryRegionInitArgument >,
@@ -49,6 +63,7 @@ pub(crate) struct GspMemObjects { pub logrm: CoherentAllocation<u8>, pub wpr_meta: CoherentAllocation<GspFwWprMeta>, pub cmdq: GspCmdq, + rmargs: CoherentAllocation<GSP_ARGUMENTS_CACHED>, } pub(crate) fn build_wpr_meta(
@@ -179,13 +194,36 @@ pub(crate) fn new(let wpr_meta =build_wpr_meta (dev, fw, fb_layout)?;// Creates its own PTE array -let cmdq =GspCmdq ::new (dev)?; +let mut cmdq =GspCmdq ::new (dev)?; +let rmargs = +create_coherent_dma_object ::<GSP_ARGUMENTS_CACHED >(dev,"RMARGS" ,1 , &mut libos,3 )?; +let (shared_mem_phys_addr, cmd_queue_offset, stat_queue_offset) = cmdq.get_cmdq_offsets (); + +dma_write! ( + rmargs[0 ].messageQueueInitArguments =MESSAGE_QUEUE_INIT_ARGUMENTS { + sharedMemPhysAddr: shared_mem_phys_addr, + pageTableEntryCount: cmdq.nr_ptes, + cmdQueueOffset: cmd_queue_offset, + statQueueOffset: stat_queue_offset, + ..Default ::default () + } + )?; +dma_write! ( + rmargs[0 ].srInitArguments =GSP_SR_INIT_ARGUMENTS { + oldLevel:0 , + flags:0 , + bInPMTransition:0 , + ..Default ::default () + } + )?; +dma_write! (rmargs[0 ].bDmemStack =1 )?;Ok (GspMemObjects { libos, loginit, logintr, logrm, + rmargs, wpr_meta, cmdq, })
drivers/gpu/nova-core/gsp/cmdq.rs +11 -2
@@ -162,7 +162,7 @@ pub(crate) struct GspCmdq { msg_count: u32, seq: u32, gsp_mem: CoherentAllocation<GspMem>, - pub _nr_ptes: u32, + pub nr_ptes: u32, }// A reference to a message currently sitting in the GSP command queue. May
@@ -289,7 +289,7 @@ pub(crate) fn new(dev: &device::Device<device::Bound>) -> Result<GspCmdq> { msg_count: MSG_COUNT, seq: 0, gsp_mem, - _nr_ptes: nr_ptes as u32, + nr_ptes: nr_ptes as u32, }) }
@@ -639,6 +639,15 @@ pub(crate) fn receive_msg_from_gsp<'a>(&'a mut self) -> Result<GspQueueMessage<'Ok (gspq_msg) } +pub (crate )fn get_cmdq_offsets (&self) -> (u64 ,u64 ,u64 ) { + ( + self.gsp_mem.dma_handle (), + core::mem::offset_of!(Msgq , msgq)as u64 , + (core::mem::offset_of!(GspMem , gspq) - core::mem::offset_of!(GspMem , cpuq) + + core::mem::offset_of!(Msgq , msgq))as u64 , + ) + } +fn ack_msg (&mut self, length:u32 ) ->Result {const HEADER_SIZE :u32 = (size_of ::<GspMsgHeader >() +size_of ::<GspRpcHeader >())as u32 ;let mut rptr = self.cpu_rptr ();
drivers/gpu/nova-core/nvfw.rs +6 -0
@@ -47,9 +47,15 @@ pub(crate) struct LibosParams { LibosMemoryRegionKind_LIBOS_MEMORY_REGION_CONTIGUOUS, LibosMemoryRegionLoc_LIBOS_MEMORY_REGION_LOC_SYSMEM, + GSP_ARGUMENTS_CACHED, +// GSP firmware constants GSP_FW_WPR_META_MAGIC, GSP_FW_WPR_META_REVISION, + GSP_SR_INIT_ARGUMENTS, + +// RM message queue parameters + MESSAGE_QUEUE_INIT_ARGUMENTS,// GSP events NV_VGPU_MSG_EVENT_GSP_INIT_DONE,
drivers/gpu/nova-core/nvfw/r570_144_bindings.rs +33 -0
@@ -288,6 +288,39 @@pub const NV_VGPU_MSG_EVENT_NUM_EVENTS :_bindgen_ty_3 =4131 ;pub type _bindgen_ty_3 = ffi::c_uint ;#[repr( C )] +#[derive( Debug , Default , Copy , Clone )] +pub struct MESSAGE_QUEUE_INIT_ARGUMENTS { +pub sharedMemPhysAddr:u64_ , +pub pageTableEntryCount:u32_ , +pub __bindgen_padding_0: [u8 ;4usize ], +pub cmdQueueOffset:u64_ , +pub statQueueOffset:u64_ , +} +#[repr( C )] +#[derive( Debug , Default , Copy , Clone )] +pub struct GSP_SR_INIT_ARGUMENTS { +pub oldLevel:u32_ , +pub flags:u32_ , +pub bInPMTransition:u8_ , +pub __bindgen_padding_0: [u8 ;3usize ], +} +#[repr( C )] +#[derive( Debug , Default , Copy , Clone )] +pub struct GSP_ARGUMENTS_CACHED { +pub messageQueueInitArguments:MESSAGE_QUEUE_INIT_ARGUMENTS , +pub srInitArguments:GSP_SR_INIT_ARGUMENTS , +pub gpuInstance:u32_ , +pub bDmemStack:u8_ , +pub __bindgen_padding_0: [u8 ;7usize ], +pub profilerArgs:GSP_ARGUMENTS_CACHED__bindgen_ty_1 , +} +#[repr( C )] +#[derive( Debug , Default , Copy , Clone )] +pub struct GSP_ARGUMENTS_CACHED__bindgen_ty_1 { +pub pa:u64_ , +pub size:u64_ , +} +#[repr( C )] #[derive( Copy , Clone )] pub struct GspFwWprMeta {pub magic:u64_ ,
Signature · 1 line
2.47.2
PATCH 7/10 gpu: nova-core: gsp: Create RM registry and sysinfo commands
In reply to Alistair PoppleAdd the RM registry and system information commands that enable the host driver to configure GSP firmware parameters during initialization.
The RM registry is serialized into a packed format and sent via the command queue. For now only two parameters which are required to boot GSP are hardcoded. In future a kernel module parameter will be added to enable other parameters to be added.
Also add the system info command, which provides required hardware information to the GSP. These commands use the GSP command queue infrastructure to issue commands to the GSP which is read during GSP boot.
--- drivers/gpu/nova-core/gpu.rs | 2 +- drivers/gpu/nova-core/gsp.rs | 7 + drivers/gpu/nova-core/gsp/cmdq.rs | 3 - drivers/gpu/nova-core/gsp/commands.rs | 201 ++++++++++++++++++ drivers/gpu/nova-core/nvfw.rs | 8 + .../gpu/nova-core/nvfw/r570_144_bindings.rs | 179 ++++++++++++++++ 6 files changed, 396 insertions(+), 4 deletions(-) create mode 100644 drivers/gpu/nova-core/gsp/commands.rs
drivers/gpu/nova-core/gpu.rs +1 -1
@@ -312,7 +312,7 @@ pub(crate) fn new(Self ::run_fwsec_frts (pdev.as_ref (), &gsp_falcon, bar, &bios, &fb_layout)?; -let libos = gsp::GspMemObjects ::new (pdev, &fw, &fb_layout)?; +let libos = gsp::GspMemObjects ::new (pdev, bar, &fw, &fb_layout)?;let _libos_handle = libos.libos_dma_handle ();let _wpr_handle = libos.wpr_meta.dma_handle ();
drivers/gpu/nova-core/gsp.rs +7 -0
@@ -11,9 +11,11 @@use kernel::sizes::SZ_128K ;use kernel::transmute::{AsBytes ,FromBytes }; +use crate ::driver::Bar0 ;use crate ::fb::FbLayout ;use crate ::firmware::Firmware ;use crate ::gsp::cmdq::GspCmdq ; +use crate ::gsp::commands::{build_registry, set_system_info};use crate ::nvfw::{GspFwWprMeta ,GspFwWprMetaBootInfo ,GspFwWprMetaBootResumeInfo ,LibosMemoryRegionInitArgument ,LibosMemoryRegionKind_LIBOS_MEMORY_REGION_CONTIGUOUS ,
@@ -23,6 +25,7 @@ };pub (crate )mod cmdq; +pub (crate )mod commands;pub (crate )const GSP_PAGE_SHIFT :usize =12 ;pub (crate )const GSP_PAGE_SIZE :usize =1 <<GSP_PAGE_SHIFT ;
@@ -176,6 +179,7 @@ fn create_coherent_dma_object<A: AsBytes + FromBytes>(impl GspMemObjects {pub (crate )fn new ( pdev: &pci::Device <device::Bound >, + bar: &Bar0 , fw: &Firmware , fb_layout: &FbLayout , ) ->Result <Self > {
@@ -218,6 +222,9 @@ pub(crate) fn new( )?;dma_write! (rmargs[0 ].bDmemStack =1 )?; +set_system_info (&mut cmdq, pdev, bar)?; +build_registry (&mut cmdq, bar)?; +Ok (GspMemObjects { libos, loginit,
drivers/gpu/nova-core/gsp/cmdq.rs +0 -3
@@ -228,7 +228,6 @@ pub(crate) struct GspQueueCommand<'a> { );impl <'a>GspQueueCommand <'a> { -#[expect(unused)] pub (crate )fn try_as <'b,M :GspCommandToGsp >(&'bmut self) ->GspQueueCommandData <'b,M > {// SAFETY: The slice references the cmdq message memory which is // guaranteed to outlive the returned GspQueueCommandData by the
@@ -247,7 +246,6 @@ pub(crate) fn try_as<'b, M: GspCommandToGsp>(&'b mut self) -> GspQueueCommandDat (msg, sbuf) } -#[expect(unused)] pub (crate )fn send_to_gsp (self, bar: &Bar0 ) ->Result { self.cmdq.wait_for_free_cmd_to_gsp ( Delta::from_secs(GSP_COMMAND_TIMEOUT),
@@ -361,7 +359,6 @@ pub(crate) fn wait_for_free_cmd_to_gsp(&self, timeout: Delta, size: usize) -> Re }) } -#[expect(unused)] pub (crate )fn alloc_gsp_queue_command <'a>( &'amut self, cmd_size: usize,
drivers/gpu/nova-core/gsp/commands.rs new file +201 -0
201 lines not on this page · raw message
drivers/gpu/nova-core/nvfw.rs +8 -0
@@ -42,6 +42,9 @@ pub(crate) struct LibosParams {pub (crate )use r570_144::GspFwWprMeta ;pub (crate )use r570_144::{ +// Core GSP structures +GspSystemInfo , +// LibOS memory structures LibosMemoryRegionInitArgument ,LibosMemoryRegionKind_LIBOS_MEMORY_REGION_CONTIGUOUS ,
@@ -87,6 +90,11 @@ pub(crate) struct LibosParams { NV_VGPU_MSG_FUNCTION_NOP, NV_VGPU_MSG_FUNCTION_SET_GUEST_SYSTEM_INFO, NV_VGPU_MSG_FUNCTION_SET_REGISTRY, + +// RM registry structures + PACKED_REGISTRY_ENTRY, + PACKED_REGISTRY_TABLE, + REGISTRY_TABLE_ENTRY_TYPE_DWORD, };pub (crate )type GspFwWprMetaBootResumeInfo = r570_144::GspFwWprMeta__bindgen_ty_1 ;
drivers/gpu/nova-core/nvfw/r570_144_bindings.rs +179 -0
199 lines not on this page · raw message
Signature · 1 line
2.47.2
Re: [PATCH 07/10] gpu: nova-core: gsp: Create RM registry and sysinfo commands
On 2025-08-27 at 18:20 +1000, Alistair Popple <[email protected]> wrote...
Quoted 126 lines
Add the RM registry and system information commands that enable the host driver to configure GSP firmware parameters during initialization.
The RM registry is serialized into a packed format and sent via the command queue. For now only two parameters which are required to boot GSP are hardcoded. In future a kernel module parameter will be added to enable other parameters to be added.
Also add the system info command, which provides required hardware information to the GSP. These commands use the GSP command queue infrastructure to issue commands to the GSP which is read during GSP boot.
Signed-off-by Alistair Popple <[email protected]>--- drivers/gpu/nova-core/gpu.rs | 2 +- drivers/gpu/nova-core/gsp.rs | 7 + drivers/gpu/nova-core/gsp/cmdq.rs | 3 - drivers/gpu/nova-core/gsp/commands.rs | 201 ++++++++++++++++++ drivers/gpu/nova-core/nvfw.rs | 8 + .../gpu/nova-core/nvfw/r570_144_bindings.rs | 179 ++++++++++++++++ 6 files changed, 396 insertions(+), 4 deletions(-) create mode 100644 drivers/gpu/nova-core/gsp/commands.rsdrivers/gpu/nova-core/gpu.rs +1
-1@@ -312,7 +312,7 @@ pub(crate) fn new(Self ::run_fwsec_frts (pdev.as_ref (), &gsp_falcon, bar, &bios, &fb_layout)?; -let libos = gsp::GspMemObjects ::new (pdev, &fw, &fb_layout)?; +let libos = gsp::GspMemObjects ::new (pdev, bar, &fw, &fb_layout)?;let _libos_handle = libos.libos_dma_handle ();let _wpr_handle = libos.wpr_meta.dma_handle ();drivers/gpu/nova-core/gsp.rs +7
-0@@ -11,9 +11,11 @@use kernel::sizes::SZ_128K ;use kernel::transmute::{AsBytes ,FromBytes }; +use crate ::driver::Bar0 ;use crate ::fb::FbLayout ;use crate ::firmware::Firmware ;use crate ::gsp::cmdq::GspCmdq ; +use crate ::gsp::commands::{build_registry, set_system_info};use crate ::nvfw::{GspFwWprMeta ,GspFwWprMetaBootInfo ,GspFwWprMetaBootResumeInfo ,LibosMemoryRegionInitArgument ,LibosMemoryRegionKind_LIBOS_MEMORY_REGION_CONTIGUOUS ,@@ -23,6 +25,7 @@ };pub (crate )mod cmdq; +pub (crate )mod commands;pub (crate )const GSP_PAGE_SHIFT :usize =12 ;pub (crate )const GSP_PAGE_SIZE :usize =1 <<GSP_PAGE_SHIFT ;@@ -176,6 +179,7 @@ fn create_coherent_dma_object<A: AsBytes + FromBytes>(impl GspMemObjects {pub (crate )fn new ( pdev: &pci::Device <device::Bound >, + bar: &Bar0 , fw: &Firmware , fb_layout: &FbLayout , ) ->Result <Self > {@@ -218,6 +222,9 @@ pub(crate) fn new( )?;dma_write! (rmargs[0 ].bDmemStack =1 )?; +set_system_info (&mut cmdq, pdev, bar)?; +build_registry (&mut cmdq, bar)?; +Ok (GspMemObjects { libos, loginit,drivers/gpu/nova-core/gsp/cmdq.rs +0
-3@@ -228,7 +228,6 @@ pub(crate) struct GspQueueCommand<'a> { );impl <'a>GspQueueCommand <'a> { -#[expect(unused)] pub (crate )fn try_as <'b,M :GspCommandToGsp >(&'bmut self) ->GspQueueCommandData <'b,M > {// SAFETY: The slice references the cmdq message memory which is // guaranteed to outlive the returned GspQueueCommandData by the @@ -247,7 +246,6 @@ pub(crate) fn try_as<'b, M: GspCommandToGsp>(&'b mut self) -> GspQueueCommandDat (msg, sbuf) } -#[expect(unused)] pub (crate )fn send_to_gsp (self, bar: &Bar0 ) ->Result { self.cmdq.wait_for_free_cmd_to_gsp ( Delta::from_secs(GSP_COMMAND_TIMEOUT),@@ -361,7 +359,6 @@ pub(crate) fn wait_for_free_cmd_to_gsp(&self, timeout: Delta, size: usize) -> Re }) } -#[expect(unused)] pub (crate )fn alloc_gsp_queue_command <'a>( &'amut self, cmd_size: usize,drivers/gpu/nova-core/gsp/commands.rs new file +13
-0@@ -0,0 +1,201 @@ +// SPDX-License-Identifier: GPL-2.0 + +use core::alloc::Layout ; + +use kernel::alloc::allocator::Kmalloc ; +use kernel::alloc::Allocator ; +use kernel::build_assert; +use kernel::device; +use kernel::pci; +use kernel::prelude::*; +use kernel::transmute::{AsBytes ,FromBytes }; + +use crate ::driver::Bar0 ;
drivers/gpu/nova-core/gsp/commands.rs line 23
Quoted 10 lines
drivers/gpu/nova-core/gsp/commands.rs new file +10
-0+use crate ::gsp::cmdq::GspCommandToGsp ; +use crate ::gsp::cmdq::GspCmdq ; +use crate ::gsp::GSP_PAGE_SIZE ; +use crate ::nvfw::{ +NV_VGPU_MSG_FUNCTION_SET_REGISTRY , +NV_VGPU_MSG_FUNCTION_GSP_SET_SYSTEM_INFO , +GspSystemInfo , +PACKED_REGISTRY_TABLE , +PACKED_REGISTRY_ENTRY , +REGISTRY_TABLE_ENTRY_TYPE_DWORD ,
I see I must have somehow missed running rustfmt on this file (probably due to resolving a merge conflict). I will make sure that happens for v2.
Quoted 414 lines
drivers/gpu/nova-core/gsp/commands.rs new file +178
-0+}; +use crate ::sbuffer::SBuffer ; + +// SAFETY: These structs don't meet the no-padding requirements of AsBytes but +// that is not a problem because they are not used outside the kernel. +unsafe impl AsBytes for GspSystemInfo {} + +// SAFETY: These structs don't meet the no-padding requirements of FromBytes but +// that is not a problem because they are not used outside the kernel. +unsafe impl FromBytes for GspSystemInfo {} + +const GSP_REGISTRY_NUM_ENTRIES :usize =2 ; +struct RegistryEntry { + key: &'staticstr , + value:u32 , +} + +struct RegistryTable { + entries: [RegistryEntry ;GSP_REGISTRY_NUM_ENTRIES ], +} + +struct GspRegistryTable ; +impl GspCommandToGsp for GspRegistryTable { +const FUNCTION :u32 =NV_VGPU_MSG_FUNCTION_SET_REGISTRY ; +} + +impl RegistryTable { +fn serialize_registry_table (&self) ->Result <KVec <u8 >> { +let entries = &self.entries; +let total_size = self.size (); +let align = core::mem::align_of ::<PACKED_REGISTRY_TABLE >(); +let layout =Layout ::from_size_align (total_size, align).map_err (|_|ENOMEM )?; +debug_assert_eq! (layout.size (), total_size); +let mut string_data_offset =size_of ::<PACKED_REGISTRY_TABLE >() + +GSP_REGISTRY_NUM_ENTRIES *size_of ::<PACKED_REGISTRY_ENTRY >(); +let allocation =Kmalloc ::alloc (layout,GFP_KERNEL )?; +let ptr = allocation.as_ptr ().cast ::<u8 >(); + +// We allocate the memory for the vector ourselves to ensure it has the +// correct layout to cast to a PACKED_REGISTRY_TABLE and subsequent +// fw:PACKED_REGISTRY_ENTRIES. +// +// SAFETY: +// - ptr was allocated with Kmalloc as required for KVec. +// - ptr trivally meets the alignment requirements for u8. +// - No elements have been initialised so this is zero length. +// - The capacity matches the total size of the allocation. +let mut table_vec =unsafe {KVec ::<u8 >::from_raw_parts (ptr,0 , layout.size ()) }; +let table_slice = table_vec.spare_capacity_mut (); +let table = table_slice.as_mut_ptr ().cast ::<PACKED_REGISTRY_TABLE >(); + +// SAFETY: We ensured the alignment was correct when allocating the vector. +unsafe { +// Set the table header + (*table).numEntries =GSP_REGISTRY_NUM_ENTRIES as u32 ; + (*table).size = total_sizeas u32 ; + } + +for (i, entry)in entries.iter ().enumerate ().take (GSP_REGISTRY_NUM_ENTRIES ) { +// SAFETY: The allocation meets the alignment requirements for +// fw::PACKED_REGISTRY_TABLE which includes a zero length array for the entries. +unsafe { +let entry_ptr = table_slice + .as_mut_ptr () + .add ( +size_of ::<PACKED_REGISTRY_TABLE >() + + i *size_of ::<PACKED_REGISTRY_ENTRY >(), + ) + .cast ::<PACKED_REGISTRY_ENTRY >(); + +// Set entry metadata + (*entry_ptr).nameOffset = string_data_offsetas u32 ; + (*entry_ptr).type_ =REGISTRY_TABLE_ENTRY_TYPE_DWORD as u8 ; + (*entry_ptr).data = entry.value; + (*entry_ptr).length =0 ; + } + +let key_bytes = entry.key.as_bytes (); +let string_dest_slice = + &mut table_slice[string_data_offset..string_data_offset + key_bytes.len () +1 ]; + +// Can't use copy_from_slice() because string_dest_slice is MaybeUninit<u8>. +for (i, &byte)in key_bytes.iter ().enumerate () { + string_dest_slice[i].write (byte); + } + +// Add null terminator + string_dest_slice[key_bytes.len ()].write (0 ); + +// Update offset for next string + string_data_offset += string_dest_slice.len (); + } + +debug_assert_eq! (total_size, string_data_offset); + +// SAFETY: All data has been written to as asserted above and the +// capacity matches the original allocation. +unsafe { table_vec.inc_len (layout.size ()) }; + +Ok (table_vec) + } + +fn copy_to_sbuf_iter (&self,mut sbuf:SBuffer <core::array::IntoIter <&mut [u8 ],2 >>) ->Result { +let table_vec = self.serialize_registry_table ()?; + sbuf.write_all (&table_vec)?; +Ok (()) + } + +fn size (&self) ->usize { +let mut key_size =0 ; +for iin 0 ..GSP_REGISTRY_NUM_ENTRIES { + key_size += self.entries[i].key.len () +1 ;// +1 for NULL terminator + } +size_of ::<PACKED_REGISTRY_TABLE >() + +GSP_REGISTRY_NUM_ENTRIES *size_of ::<PACKED_REGISTRY_ENTRY >() + + key_size + } +} + +pub (crate )fn build_registry (cmdq: &mut GspCmdq , bar: &Bar0 ) ->Result { +let registry =RegistryTable { + entries: [ +RegistryEntry { + key:"RMSecBusResetEnable" , + value:1 , + }, +RegistryEntry { + key:"RMForcePcieConfigSave" , + value:1 , + }, + ], + }; +let mut msg = cmdq.alloc_gsp_queue_command (registry.size ())?; + { +let (_, some_sbuf) = msg.try_as ::<GspRegistryTable >(); +let sbuf = some_sbuf.ok_or (ENOMEM )?; + registry.copy_to_sbuf_iter (sbuf)?; + } + msg.send_to_gsp (bar)?; + +Ok (()) +} + +impl GspCommandToGsp for GspSystemInfo { +const FUNCTION :u32 =NV_VGPU_MSG_FUNCTION_GSP_SET_SYSTEM_INFO ; +} + +pub (crate )fn set_system_info ( + cmdq: &mut GspCmdq , + dev: &pci::Device <device::Bound >, + bar: &Bar0 , +) ->Result { +build_assert! (size_of ::<GspSystemInfo >() <GSP_PAGE_SIZE ); +let mut msg = cmdq.alloc_gsp_queue_command (size_of ::<GspSystemInfo >())?; + { +let (info, _) = msg.try_as ::<GspSystemInfo >(); + + info.gpuPhysAddr = dev.resource_start (0 )?; + info.gpuPhysFbAddr = dev.resource_start (1 )?; + info.gpuPhysInstAddr = dev.resource_start (3 )?; + info.nvDomainBusDeviceFunc = u64::from (dev.dev_id ()); + +// Using TASK_SIZE in r535_gsp_rpc_set_system_info() seems wrong because +// TASK_SIZE is per-task. That's probably a design issue in GSP-RM though. + info.maxUserVa = (1 <<47 ) -4096 ; + info.pciConfigMirrorBase =0x088000 ; + info.pciConfigMirrorSize =0x001000 ; + + info.PCIDeviceID = (u32::from (dev.device_id ()) <<16 ) | u32::from (dev.vendor_id ()); + info.PCISubDeviceID = + (u32::from (dev.subsystem_device_id ()) <<16 ) | u32::from (dev.subsystem_vendor_id ()); + info.PCIRevisionID = u32::from (dev.revision_id ()); + info.bIsPrimary =0 ; + info.bPreserveVideoMemoryAllocations =0 ; + } + msg.send_to_gsp (bar)?; +Ok (()) +}drivers/gpu/nova-core/nvfw.rs +8
-0@@ -42,6 +42,9 @@ pub(crate) struct LibosParams {pub (crate )use r570_144::GspFwWprMeta ;pub (crate )use r570_144::{ +// Core GSP structures +GspSystemInfo , +// LibOS memory structures LibosMemoryRegionInitArgument ,LibosMemoryRegionKind_LIBOS_MEMORY_REGION_CONTIGUOUS ,@@ -87,6 +90,11 @@ pub(crate) struct LibosParams { NV_VGPU_MSG_FUNCTION_NOP, NV_VGPU_MSG_FUNCTION_SET_GUEST_SYSTEM_INFO, NV_VGPU_MSG_FUNCTION_SET_REGISTRY, + +// RM registry structures + PACKED_REGISTRY_ENTRY, + PACKED_REGISTRY_TABLE, + REGISTRY_TABLE_ENTRY_TYPE_DWORD, };pub (crate )type GspFwWprMetaBootResumeInfo = r570_144::GspFwWprMeta__bindgen_ty_1 ;drivers/gpu/nova-core/nvfw/r570_144_bindings.rs +179
-0@@ -1,5 +1,35 @@// SPDX-License-Identifier: GPL-2.0 +#[repr( C )] +#[derive( Default )] +pub struct __IncompleteArrayField <T >(::core::marker::PhantomData <T >, [T ;0 ]); +impl <T >__IncompleteArrayField <T > { +#[inline] +pub const fn new () ->Self { +__IncompleteArrayField (::core::marker::PhantomData , []) + } +#[inline] +pub fn as_ptr (&self) -> *const T { + selfas *const _ as *const T + } +#[inline] +pub fn as_mut_ptr (&mut self) -> *mut T { + selfas *mut _ as *mut T + } +#[inline] +pub unsafe fn as_slice (&self, len:usize ) -> &[T ] { + ::core::slice::from_raw_parts (self.as_ptr (), len) + } +#[inline] +pub unsafe fn as_mut_slice (&mut self, len:usize ) -> &mut [T ] { + ::core::slice::from_raw_parts_mut (self.as_mut_ptr (), len) + } +} +impl <T > ::core::fmt::Debug for __IncompleteArrayField <T > { +fn fmt (&self, fmt: &mut ::core::fmt::Formatter <'_>) -> ::core::fmt::Result { + fmt.write_str ("__IncompleteArrayField" ) + } +}pub const GSP_FW_HEAP_PARAM_OS_SIZE_LIBOS2 :u32 =0 ;pub const GSP_FW_HEAP_PARAM_OS_SIZE_LIBOS3_BAREMETAL :u32 =23068672 ;pub const GSP_FW_HEAP_PARAM_BASE_RM_SIZE_TU10X :u32 =8388608 ;@@ -11,6 +41,7 @@pub const GSP_FW_HEAP_SIZE_OVERRIDE_LIBOS3_BAREMETAL_MAX_MB :u32 =280 ;pub const GSP_FW_WPR_META_REVISION :u32 =1 ;pub const GSP_FW_WPR_META_MAGIC :i64 = -2577556379034558285 ; +pub const REGISTRY_TABLE_ENTRY_TYPE_DWORD :u32 =1 ;pub type __u8 = ffi::c_uchar ;pub type __u16 = ffi::c_ushort ;pub type __u32 = ffi::c_uint ;@@ -289,6 +320,138 @@pub type _bindgen_ty_3 = ffi::c_uint ;#[repr( C )] #[derive( Debug , Default , Copy , Clone )] +pub struct DOD_METHOD_DATA { +pub status:u32_ , +pub acpiIdListLen:u32_ , +pub acpiIdList: [u32_ ;16usize ], +} +#[repr( C )] +#[derive( Debug , Default , Copy , Clone )] +pub struct JT_METHOD_DATA { +pub status:u32_ , +pub jtCaps:u32_ , +pub jtRevId:u16_ , +pub bSBIOSCaps:u8_ , +pub __bindgen_padding_0:u8 , +} +#[repr( C )] +#[derive( Debug , Default , Copy , Clone )] +pub struct MUX_METHOD_DATA_ELEMENT { +pub acpiId:u32_ , +pub mode:u32_ , +pub status:u32_ , +} +#[repr( C )] +#[derive( Debug , Default , Copy , Clone )] +pub struct MUX_METHOD_DATA { +pub tableLen:u32_ , +pub acpiIdMuxModeTable: [MUX_METHOD_DATA_ELEMENT ;16usize ], +pub acpiIdMuxPartTable: [MUX_METHOD_DATA_ELEMENT ;16usize ], +pub acpiIdMuxStateTable: [MUX_METHOD_DATA_ELEMENT ;16usize ], +} +#[repr( C )] +#[derive( Debug , Default , Copy , Clone )] +pub struct CAPS_METHOD_DATA { +pub status:u32_ , +pub optimusCaps:u32_ , +} +#[repr( C )] +#[derive( Debug , Default , Copy , Clone )] +pub struct ACPI_METHOD_DATA { +pub bValid:u8_ , +pub __bindgen_padding_0: [u8 ;3usize ], +pub dodMethodData:DOD_METHOD_DATA , +pub jtMethodData:JT_METHOD_DATA , +pub muxMethodData:MUX_METHOD_DATA , +pub capsMethodData:CAPS_METHOD_DATA , +} +#[repr( C )] +#[derive( Debug , Default , Copy , Clone )] +pub struct BUSINFO { +pub deviceID:u16_ , +pub vendorID:u16_ , +pub subdeviceID:u16_ , +pub subvendorID:u16_ , +pub revisionID:u8_ , +pub __bindgen_padding_0:u8 , +} +#[repr( C )] +#[derive( Debug , Default , Copy , Clone )] +pub struct GSP_VF_INFO { +pub totalVFs:u32_ , +pub firstVFOffset:u32_ , +pub FirstVFBar0Address:u64_ , +pub FirstVFBar1Address:u64_ , +pub FirstVFBar2Address:u64_ , +pub b64bitBar0:u8_ , +pub b64bitBar1:u8_ , +pub b64bitBar2:u8_ , +pub __bindgen_padding_0: [u8 ;5usize ], +} +#[repr( C )] +#[derive( Debug , Default , Copy , Clone )] +pub struct GSP_PCIE_CONFIG_REG { +pub linkCap:u32_ , +} +#[repr( C )] +#[derive( Debug , Default , Copy , Clone )] +pub struct GspSystemInfo { +pub gpuPhysAddr:u64_ , +pub gpuPhysFbAddr:u64_ , +pub gpuPhysInstAddr:u64_ , +pub gpuPhysIoAddr:u64_ , +pub nvDomainBusDeviceFunc:u64_ , +pub simAccessBufPhysAddr:u64_ , +pub notifyOpSharedSurfacePhysAddr:u64_ , +pub pcieAtomicsOpMask:u64_ , +pub consoleMemSize:u64_ , +pub maxUserVa:u64_ , +pub pciConfigMirrorBase:u32_ , +pub pciConfigMirrorSize:u32_ , +pub PCIDeviceID:u32_ , +pub PCISubDeviceID:u32_ , +pub PCIRevisionID:u32_ , +pub pcieAtomicsCplDeviceCapMask:u32_ , +pub oorArch:u8_ , +pub __bindgen_padding_0: [u8 ;7usize ], +pub clPdbProperties:u64_ , +pub Chipset:u32_ , +pub bGpuBehindBridge:u8_ , +pub bFlrSupported:u8_ , +pub b64bBar0Supported:u8_ , +pub bMnocAvailable:u8_ , +pub chipsetL1ssEnable:u32_ , +pub bUpstreamL0sUnsupported:u8_ , +pub bUpstreamL1Unsupported:u8_ , +pub bUpstreamL1PorSupported:u8_ , +pub bUpstreamL1PorMobileOnly:u8_ , +pub bSystemHasMux:u8_ , +pub upstreamAddressValid:u8_ , +pub FHBBusInfo:BUSINFO , +pub chipsetIDInfo:BUSINFO , +pub __bindgen_padding_1: [u8 ;2usize ], +pub acpiMethodData:ACPI_METHOD_DATA , +pub hypervisorType:u32_ , +pub bIsPassthru:u8_ , +pub __bindgen_padding_2: [u8 ;7usize ], +pub sysTimerOffsetNs:u64_ , +pub gspVFInfo:GSP_VF_INFO , +pub bIsPrimary:u8_ , +pub isGridBuild:u8_ , +pub __bindgen_padding_3: [u8 ;2usize ], +pub pcieConfigReg:GSP_PCIE_CONFIG_REG , +pub gridBuildCsp:u32_ , +pub bPreserveVideoMemoryAllocations:u8_ , +pub bTdrEventSupported:u8_ , +pub bFeatureStretchVblankCapable:u8_ , +pub bEnableDynamicGranularityPageArrays:u8_ , +pub bClockBoostSupported:u8_ , +pub bRouteDispIntrsToCPU:u8_ , +pub __bindgen_padding_4: [u8 ;6usize ], +pub hostPageSize:u64_ , +} +#[repr( C )] +#[derive( Debug , Default , Copy , Clone )] pub struct MESSAGE_QUEUE_INIT_ARGUMENTS {pub sharedMemPhysAddr:u64_ ,pub pageTableEntryCount:u32_ ,@@ -446,3 +609,19 @@ pub struct LibosMemoryRegionInitArgument { pub loc: u8_, pub __bindgen_padding_0: [u8; 6usize], } +#[repr( C )] +#[derive( Debug , Default , Copy , Clone )] +pub struct PACKED_REGISTRY_ENTRY { +pub nameOffset:u32_ , +pub type_:u8_ , +pub __bindgen_padding_0: [u8 ;3usize ], +pub data:u32_ , +pub length:u32_ , +} +#[repr( C )] +#[derive( Debug , Default )] +pub struct PACKED_REGISTRY_TABLE { +pub size:u32_ , +pub numEntries:u32_ , +pub entries:__IncompleteArrayField <PACKED_REGISTRY_ENTRY >, +}Signature · 1 line
2.47.2
PATCH 8/10 gpu: nova-core: falcon: Add support to check if RISC-V is active
In reply to Alistair PoppleFrom: Joel Fernandes <[email protected]>
Add definition for RISCV_CPUCTL register and use it in a new falcon API to check if the RISC-V core of a Falcon is active. It is required by the sequencer to know if the GSP's RISCV processor is active.
--- drivers/gpu/nova-core/falcon.rs | 8 ++++++++ drivers/gpu/nova-core/regs.rs | 5 +++++ 2 files changed, 13 insertions(+)
drivers/gpu/nova-core/falcon.rs +8 -0
@@ -610,4 +610,12 @@ pub(crate) fn signature_reg_fuse_version( self.hal .signature_reg_fuse_version (self, bar, engine_id_mask, ucode_id) } + +/// Check if the RISC-V core is active. +/// +/// Returns `true` if the RISC-V core is active, `false` otherwise. +pub (crate )fn is_riscv_active (&self, bar: &Bar0 ) ->Result <bool > { +let cpuctl = regs::NV_PRISCV_RISCV_CPUCTL ::read (bar, &E ::ID ); +Ok (cpuctl.active_stat ()) + } }
drivers/gpu/nova-core/regs.rs +5 -0
@@ -324,6 +324,11 @@ pub(crate) fn mem_scrubbing_done(self) -> bool {// PRISCV +register! (NV_PRISCV_RISCV_CPUCTL @PFalconBase [0x00001388 ] { +7 :7 active_statas bool ; +0 :0 haltedas bool ; +}); +register! (NV_PRISCV_RISCV_BCR_CTRL @PFalconBase [0x00001668 ] {0 :0 validas bool ;4 :4 core_selectas bool =>PeregrineCoreSelect ;
Signature · 1 line
2.47.2
Re: [PATCH 08/10] gpu: nova-core: falcon: Add support to check if RISC-V is active
Quoted 4 lines · Alistair Popple
+ pub(crate) fn
is_riscv_active(&self, bar: &Bar0)-> Result<bool> { + let cpuctl =regs::NV_PRISCV_RISCV_CPUCTL::read(bar, &E::ID); + Ok(cpuctl.active_stat())
+ }
I think this should return just bool instead of Result<bool>
Re: [PATCH 08/10] gpu: nova-core: falcon: Add support to check if RISC-V is active
On 2025-08-30 at 04:48 +1000, Timur Tabi <[email protected]> wrote...
Quoted 7 lines · Alistair Popple
Quoted 4 lines
+ pub(crate) fn
is_riscv_active(&self, bar: &Bar0)-> Result<bool> { + let cpuctl =regs::NV_PRISCV_RISCV_CPUCTL::read(bar, &E::ID); + Ok(cpuctl.active_stat())
+ }I think this should return just bool instead of Result<bool>
Agreed. And looking at some of the other sequencer/falcon code in Joel's series I believe similar comments apply there. I assume something changed with the register reading code to make this possible.
Re: [PATCH 08/10] gpu: nova-core: falcon: Add support to check if RISC-V is active
In reply to Alistair Poppledrivers/gpu/nova-core/regs.rs pub(crate) fn mem_scrubbing_done(self) -> bool { · line 330
Quoted 5 lines · Alistair Popple
drivers/gpu/nova-core/regs.rs +4
-0+register! (NV_PRISCV_RISCV_CPUCTL @PFalconBase [0x00001388 ] { +7 :7 active_statas bool ; +0 :0 haltedas bool ; +});
Two more things I've noticed:
1) I think the convention is to list the bits in increase position. That is, 'active_stat' should be on the line below 'halted''
2) I think that this should actually be PFalcon2Base[0x00000388]
PATCH 9/10 gpu: nova-core: falcon: Add support to write firmware version
In reply to Alistair PoppleFrom: Joel Fernandes <[email protected]>
This will be needed by both the GSP boot code as well as GSP resume code in the sequencer.
--- drivers/gpu/nova-core/falcon.rs | 9 +++++++++ drivers/gpu/nova-core/regs.rs | 6 ++++++ 2 files changed, 15 insertions(+)
drivers/gpu/nova-core/falcon.rs +9 -0
@@ -618,4 +618,13 @@ pub(crate) fn is_riscv_active(&self, bar: &Bar0) -> Result<bool> {let cpuctl = regs::NV_PRISCV_RISCV_CPUCTL ::read (bar, &E ::ID );Ok (cpuctl.active_stat ()) } + +/// Write the application version to the OS register. +#[expect(dead_code)] +pub (crate )fn write_os_version (&self, bar: &Bar0 , app_version:u32 ) ->Result <()> { + regs::NV_PFALCON_FALCON_OS ::default () + .set_value (app_version) + .write (bar, &E ::ID ); +Ok (()) + } }
drivers/gpu/nova-core/regs.rs +6 -0
@@ -215,6 +215,12 @@ pub(crate) fn vga_workspace_addr(self) -> Option<u64> { 31:0 value as u32; }); +// Used to store version information about the firmware running +// on the Falcon processor. +register! (NV_PFALCON_FALCON_OS @PFalconBase [0x00000080 ] { +31 :0 valueas u32 ; +}); +register! (NV_PFALCON_FALCON_RM @PFalconBase [0x00000084 ] {31 :0 valueas u32 ; });
Signature · 1 line
2.47.2
PATCH 10/10 gpu: nova-core: gsp: Boot GSP
In reply to Alistair PoppleBoot the GSP to the RISC-V active state. Completing the boot requires running the CPU sequencer which will be added in a future commit.
--- drivers/gpu/nova-core/falcon.rs | 1 - drivers/gpu/nova-core/firmware.rs | 4 +- drivers/gpu/nova-core/firmware/riscv.rs | 3 +- drivers/gpu/nova-core/gpu.rs | 56 +++++++++++++++++++++++-- 4 files changed, 56 insertions(+), 8 deletions(-)
drivers/gpu/nova-core/falcon.rs +0 -1
@@ -620,7 +620,6 @@ pub(crate) fn is_riscv_active(&self, bar: &Bar0) -> Result<bool> { }/// Write the application version to the OS register. -#[expect(dead_code)] pub (crate )fn write_os_version (&self, bar: &Bar0 , app_version:u32 ) ->Result <()> { regs::NV_PFALCON_FALCON_OS ::default () .set_value (app_version)
drivers/gpu/nova-core/firmware.rs +2 -2
@@ -115,11 +115,11 @@ pub(super) fn elf64_section<'a, 'b>(elf: &'a [u8], name: &'b str) -> Option<&'a }/// Structure encapsulating the firmware blobs required for the GPU to operate. -#[expect(dead_code)] pub (crate )struct Firmware {/// Runs on the sec2 falcon engine to load and start the GSP bootloader. - booter_loader:BooterFirmware , +pub booter_loader:BooterFirmware ,/// Runs on the sec2 falcon engine to stop and unload a running GSP firmware. +#[expect(unused)] booter_unloader:BooterFirmware ,/// GSP bootloader, verifies the GSP firmware before loading and running it. pub gsp_bootloader:RiscvFirmware ,
drivers/gpu/nova-core/firmware/riscv.rs +1 -2
@@ -50,7 +50,6 @@ fn new(bin_fw: &BinFirmware<'_>) -> Result<Self> { }/// A parsed firmware for a RISC-V core, ready to be loaded and run. -#[expect(unused)] pub (crate )struct RiscvFirmware {/// Offset at which the code starts in the firmware image. pub code_offset:u32 ,
@@ -59,7 +58,7 @@ pub(crate) struct RiscvFirmware {/// Offset at which the manifest starts in the firmware image. pub manifest_offset: u32,/// Application version. - app_version: u32, + pub app_version: u32,/// Device-mapped firmware image. pub ucode: DmaObject, }
drivers/gpu/nova-core/gpu.rs +53 -3
67 lines not on this page · raw message
Signature · 1 line
2.47.2
Quoted 3 lines · On Wed, 27 Aug 2025 18:19:57 +1000 Alistair Popple <[email protected]> wrote:
This series builds on top of Alex's series[1] to continue initialising the GSP into a state where it becomes active and it starts communicating with the host.
No big deal, but in case it helps since probably it was not intentional given the rest of the people is there: the rust-for-linux Cc is missing.
Cheers,
Miguel
Alexandre Courbot
maintainerQuoted 7 lines · Miguel Ojeda
Quoted 3 lines · On Wed, 27 Aug 2025 18:19:57 +1000 Alistair Popple <[email protected]> wrote:
This series builds on top of Alex's series[1] to continue initialising the GSP into a state where it becomes active and it starts communicating with the host.
No big deal, but in case it helps since probably it was not intentional given the rest of the people is there: the rust-for-linux Cc is missing.
This did happen to me once as well - should we add the rust-for-linux list to the Nova MAINTAINERS entry to protect against this?
Danilo Krummrich
maintainerQuoted 11 lines · Alexandre Courbot
Quoted 7 lines · Miguel Ojeda
Quoted 3 lines · On Wed, 27 Aug 2025 18:19:57 +1000 Alistair Popple <[email protected]> wrote:
This series builds on top of Alex's series[1] to continue initialising the GSP into a state where it becomes active and it starts communicating with the host.
No big deal, but in case it helps since probably it was not intentional given the rest of the people is there: the rust-for-linux Cc is missing.
This did happen to me once as well - should we add the rust-for-linux list to the Nova
MAINTAINERSentry to protect against this?
I'm happy about every potential additional reviewer for Nova, but I'm not sure it scales very well for the rust-for-linux if we get more drivers. :)
Quoted 3 lines · Danilo Krummrich
I'm happy about every potential additional reviewer for Nova, but I'm not sure it scales very well for the rust-for-linux if we get more drivers. :)
Yeah, it is an informal rule I/we added back then so that people interested in Rust in the early days can follow everything in that list.
The expectation was that eventually it would go away organically as time passed if Rust grew -- I mentioned it here since I saw people mentioned explicitly otherwise.
@Alexandre As for your MAINTAINERS suggestion, up to all of you -- other entries do that currently, but it is not a requirement, and you are big enough already (have several people looking at patches etc.).
Thanks!
Cheers,
Miguel
Alexandre Courbot
maintainerQuoted 9 lines · Miguel Ojeda
Quoted 3 lines · Danilo Krummrich
I'm happy about every potential additional reviewer for Nova, but I'm not sure it scales very well for the rust-for-linux if we get more drivers. :)
Yeah, it is an informal rule I/we added back then so that people interested in Rust in the early days can follow everything in that list.
Quoted 7 lines
The expectation was that eventually it would go away organically as time passed if Rust grew -- I mentioned it here since I saw people mentioned explicitly otherwise.
@Alexandre As for your
MAINTAINERSsuggestion, up to all of you -- other entries do that currently, but it is not a requirement, and you are big enough already (have several people looking at patches etc.).
No, Danilo is right, it might somehow make sense now that the driver is still basic, but this probably won't scale too well after the basic bring up and not everyone on R4L is necessarily interested in GPU drivers.
We'll add "don't forget rust-for-linux" to our individual pre-send checklists for the time being. ;)