Skip to contentSkip to conversation

nova-core Improve pin initializer code

PATCH Applied Danilo Krummrich 2 participants 7 messages Dec 18, 2025 – Dec 29, 2025

Subsystems (4)
lore
Share & tools
lore

Danilo Krummrich

author maintainer
#0 lore raw

cover nova-core Improve pin initializer code

This is a minor series to improve some of the pin initializer code to:

1. Reduce redundancy caused by Result<impl PinInit<T, Error>> return values with pin_init_scope().

2. Relocate code that technically fits in the pin initializer into the initializer itself.

While, thanks to pin_init_scope(), it is also possible to keep it as is, moving appropriate code into the initializer has the advantage that it structures the dependencies of fields naturally.

For instance, intermediate data that is only needed for a single field goes into the initializer block of this field, making it obvious that it is not needed by anything else.

On the other hand, intermediate data that is needed for multiple fields to initialize remains above the initializer, naturally indicating that it is needed my multiple fields.

Danilo Krummrich (4):
gpu: nova-core: fw: get rid of redundant Result in GspFirmware::new()
gpu: nova-core: fw: move appropriate code into pin initializer
gpu: nova-core: gsp: get rid of redundant Result in Gsp::new()
gpu: nova-core: gsp: move appropriate code into pin initializer

 drivers/gpu/nova-core/firmware/gsp.rs | 134 +++++++++++++-------------
 drivers/gpu/nova-core/gpu.rs          |   2 +-
 drivers/gpu/nova-core/gsp.rs          |  73 +++++++-------
 drivers/gpu/nova-core/gsp/boot.rs     |   5 +-
 4 files changed, 106 insertions(+), 108 deletions(-)

base-commit: 97cf6bc0abd381fd84e5d8e978322a62a58fb00e

Signature · 1 line
2.52.0
author maintainer
#1 lore raw

PATCH 1/4 gpu: nova-core: fw: get rid of redundant Result in GspFirmware::new()

In GspFirmware::new(), utilize pin_init_scope() to get rid of the Result in the returned

Result<impl PinInit<T, Error>>

which is unnecessarily redundant.

---
 drivers/gpu/nova-core/firmware/gsp.rs | 132 +++++++++++++-------------
 drivers/gpu/nova-core/gsp/boot.rs     |   5 +-
 2 files changed, 68 insertions(+), 69 deletions(-)
drivers/gpu/nova-core/firmware/gsp.rs +67 -65
index 0549805282ab..e034268be3c5 100644
@@ -153,82 +153,84 @@ pub(crate) struct GspFirmware {
 impl GspFirmware {
     /// Loads the GSP firmware binaries, map them into `dev`'s address-space, and creates the page
     /// tables expected by the GSP bootloader to load it.
-    pub(crate) fn new<'a, 'b>(
+    pub(crate) fn new<'a>(
         dev: &'a device::Device<device::Bound>,
         chipset: Chipset,
-        ver: &'b str,
-    ) -> Result<impl PinInit<Self, Error> + 'a> {
-        let fw = super::request_firmware(dev, chipset, "gsp", ver)?;
+        ver: &'a str,
+    ) -> impl PinInit<Self, Error> + 'a {
+        pin_init::pin_init_scope(move || {
+            let fw = super::request_firmware(dev, chipset, "gsp", ver)?;
 
-        let fw_section = elf::elf64_section(fw.data(), ".fwimage").ok_or(EINVAL)?;
+            let fw_section = elf::elf64_section(fw.data(), ".fwimage").ok_or(EINVAL)?;
 
-        let sigs_section = match chipset.arch() {
-            Architecture::Ampere => ".fwsignature_ga10x",
-            Architecture::Ada => ".fwsignature_ad10x",
-            _ => return Err(ENOTSUPP),
-        };
-        let signatures = elf::elf64_section(fw.data(), sigs_section)
-            .ok_or(EINVAL)
-            .and_then(|data| DmaObject::from_data(dev, data))?;
+            let sigs_section = match chipset.arch() {
+                Architecture::Ampere => ".fwsignature_ga10x",
+                Architecture::Ada => ".fwsignature_ad10x",
+                _ => return Err(ENOTSUPP),
+            };
+            let signatures = elf::elf64_section(fw.data(), sigs_section)
+                .ok_or(EINVAL)
+                .and_then(|data| DmaObject::from_data(dev, data))?;
 
-        let size = fw_section.len();
+            let size = fw_section.len();
 
-        // Move the firmware into a vmalloc'd vector and map it into the device address
-        // space.
-        let fw_vvec = VVec::with_capacity(fw_section.len(), GFP_KERNEL)
-            .and_then(|mut v| {
-                v.extend_from_slice(fw_section, GFP_KERNEL)?;
-                Ok(v)
-            })
-            .map_err(|_| ENOMEM)?;
+            // Move the firmware into a vmalloc'd vector and map it into the device address
+            // space.
+            let fw_vvec = VVec::with_capacity(fw_section.len(), GFP_KERNEL)
+                .and_then(|mut v| {
+                    v.extend_from_slice(fw_section, GFP_KERNEL)?;
+                    Ok(v)
+                })
+                .map_err(|_| ENOMEM)?;
 
-        let bl = super::request_firmware(dev, chipset, "bootloader", ver)?;
-        let bootloader = RiscvFirmware::new(dev, &bl)?;
+            let bl = super::request_firmware(dev, chipset, "bootloader", ver)?;
+            let bootloader = RiscvFirmware::new(dev, &bl)?;
 
-        Ok(try_pin_init!(Self {
-            fw <- SGTable::new(dev, fw_vvec, DataDirection::ToDevice, GFP_KERNEL),
-            level2 <- {
-                // Allocate the level 2 page table, map the firmware onto it, and map it into the
-                // device address space.
-                VVec::<u8>::with_capacity(
-                    fw.iter().count() * core::mem::size_of::<u64>(),
-                    GFP_KERNEL,
-                )
-                .map_err(|_| ENOMEM)
-                .and_then(|level2| map_into_lvl(&fw, level2))
-                .map(|level2| SGTable::new(dev, level2, DataDirection::ToDevice, GFP_KERNEL))?
-            },
-            level1 <- {
-                // Allocate the level 1 page table, map the level 2 page table onto it, and map it
-                // into the device address space.
-                VVec::<u8>::with_capacity(
-                    level2.iter().count() * core::mem::size_of::<u64>(),
-                    GFP_KERNEL,
-                )
-                .map_err(|_| ENOMEM)
-                .and_then(|level1| map_into_lvl(&level2, level1))
-                .map(|level1| SGTable::new(dev, level1, DataDirection::ToDevice, GFP_KERNEL))?
-            },
-            level0: {
-                // Allocate the level 0 page table as a device-visible DMA object, and map the
-                // level 1 page table onto it.
+            Ok(try_pin_init!(Self {
+                fw <- SGTable::new(dev, fw_vvec, DataDirection::ToDevice, GFP_KERNEL),
+                level2 <- {
+                    // Allocate the level 2 page table, map the firmware onto it, and map it into
+                    // the device address space.
+                    VVec::<u8>::with_capacity(
+                        fw.iter().count() * core::mem::size_of::<u64>(),
+                        GFP_KERNEL,
+                    )
+                    .map_err(|_| ENOMEM)
+                    .and_then(|level2| map_into_lvl(&fw, level2))
+                    .map(|level2| SGTable::new(dev, level2, DataDirection::ToDevice, GFP_KERNEL))?
+                },
+                level1 <- {
+                    // Allocate the level 1 page table, map the level 2 page table onto it, and map
+                    // it into the device address space.
+                    VVec::<u8>::with_capacity(
+                        level2.iter().count() * core::mem::size_of::<u64>(),
+                        GFP_KERNEL,
+                    )
+                    .map_err(|_| ENOMEM)
+                    .and_then(|level1| map_into_lvl(&level2, level1))
+                    .map(|level1| SGTable::new(dev, level1, DataDirection::ToDevice, GFP_KERNEL))?
+                },
+                level0: {
+                    // Allocate the level 0 page table as a device-visible DMA object, and map the
+                    // level 1 page table onto it.
 
-                // Level 0 page table data.
-                let mut level0_data = kvec![0u8; GSP_PAGE_SIZE]?;
+                    // Level 0 page table data.
+                    let mut level0_data = kvec![0u8; GSP_PAGE_SIZE]?;
 
-                // Fill level 1 page entry.
-                let level1_entry = level1.iter().next().ok_or(EINVAL)?;
-                let level1_entry_addr = level1_entry.dma_address();
-                let dst = &mut level0_data[..size_of_val(&level1_entry_addr)];
-                dst.copy_from_slice(&level1_entry_addr.to_le_bytes());
+                    // Fill level 1 page entry.
+                    let level1_entry = level1.iter().next().ok_or(EINVAL)?;
+                    let level1_entry_addr = level1_entry.dma_address();
+                    let dst = &mut level0_data[..size_of_val(&level1_entry_addr)];
+                    dst.copy_from_slice(&level1_entry_addr.to_le_bytes());
 
-                // Turn the level0 page table into a [`DmaObject`].
-                DmaObject::from_data(dev, &level0_data)?
-            },
-            size,
-            signatures,
-            bootloader,
-        }))
+                    // Turn the level0 page table into a [`DmaObject`].
+                    DmaObject::from_data(dev, &level0_data)?
+                },
+                size,
+                signatures,
+                bootloader,
+            }))
+        })
     }
 
     /// Returns the DMA handle of the radix3 level 0 page table.
drivers/gpu/nova-core/gsp/boot.rs +1 -4
index 54937606b5b0..a53d80620468 100644
@@ -139,10 +139,7 @@ pub(crate) fn boot(
 
         let bios = Vbios::new(dev, bar)?;
 
-        let gsp_fw = KBox::pin_init(
-            GspFirmware::new(dev, chipset, FIRMWARE_VERSION)?,
-            GFP_KERNEL,
-        )?;
+        let gsp_fw = KBox::pin_init(GspFirmware::new(dev, chipset, FIRMWARE_VERSION), GFP_KERNEL)?;
 
         let fb_layout = FbLayout::new(chipset, bar, &gsp_fw)?;
         dev_dbg!(dev, "{:#x?}\n", fb_layout);
Signature · 1 line
2.52.0
author maintainer
#2 lore raw

PATCH 2/4 gpu: nova-core: fw: move appropriate code into pin initializer

In reply to Danilo Krummrich

Relocate the code that technically fits in the pin initializer into the initializer itself.

While, thanks to pin_init_scope(), it is also possible to keep it as is, moving appropriate code into the initializer has the advantage that it structures the dependencies of fields naturally.

For instance, intermediate data that is only needed for a single field goes into the initializer block of this field, making it obvious that it is not needed by anything else.

On the other hand, intermediate data that is needed for multiple fields to initialize remains above the initializer, naturally indicating that it is needed my multiple fields.

---
 drivers/gpu/nova-core/firmware/gsp.rs | 34 ++++++++++++++-------------
 1 file changed, 18 insertions(+), 16 deletions(-)
drivers/gpu/nova-core/firmware/gsp.rs +18 -16
index e034268be3c5..da97814cf859 100644
@@ -159,18 +159,9 @@ pub(crate) fn new<'a>(
         ver: &'a str,
     ) -> impl PinInit<Self, Error> + 'a {
         pin_init::pin_init_scope(move || {
-            let fw = super::request_firmware(dev, chipset, "gsp", ver)?;
+            let firmware = super::request_firmware(dev, chipset, "gsp", ver)?;
 
-            let fw_section = elf::elf64_section(fw.data(), ".fwimage").ok_or(EINVAL)?;
-
-            let sigs_section = match chipset.arch() {
-                Architecture::Ampere => ".fwsignature_ga10x",
-                Architecture::Ada => ".fwsignature_ad10x",
-                _ => return Err(ENOTSUPP),
-            };
-            let signatures = elf::elf64_section(fw.data(), sigs_section)
-                .ok_or(EINVAL)
-                .and_then(|data| DmaObject::from_data(dev, data))?;
+            let fw_section = elf::elf64_section(firmware.data(), ".fwimage").ok_or(EINVAL)?;
 
             let size = fw_section.len();
 
@@ -183,9 +174,6 @@ pub(crate) fn new<'a>(
                 })
                 .map_err(|_| ENOMEM)?;
 
-            let bl = super::request_firmware(dev, chipset, "bootloader", ver)?;
-            let bootloader = RiscvFirmware::new(dev, &bl)?;
-
             Ok(try_pin_init!(Self {
                 fw <- SGTable::new(dev, fw_vvec, DataDirection::ToDevice, GFP_KERNEL),
                 level2 <- {
@@ -227,8 +215,22 @@ pub(crate) fn new<'a>(
                     DmaObject::from_data(dev, &level0_data)?
                 },
                 size,
-                signatures,
-                bootloader,
+                signatures: {
+                    let sigs_section = match chipset.arch() {
+                        Architecture::Ampere => ".fwsignature_ga10x",
+                        Architecture::Ada => ".fwsignature_ad10x",
+                        _ => return Err(ENOTSUPP),
+                    };
+
+                    elf::elf64_section(firmware.data(), sigs_section)
+                        .ok_or(EINVAL)
+                        .and_then(|data| DmaObject::from_data(dev, data))?
+                },
+                bootloader: {
+                    let bl = super::request_firmware(dev, chipset, "bootloader", ver)?;
+
+                    RiscvFirmware::new(dev, &bl)?
+                },
             }))
         })
     }
Signature · 1 line
2.52.0
author maintainer
#3 lore raw

PATCH 3/4 gpu: nova-core: gsp: get rid of redundant Result in Gsp::new()

In reply to Danilo Krummrich

In Gsp::new(), utilize pin_init_scope() to get rid of the Result in the returned

Result<impl PinInit<T, Error>>

which is unnecessarily redundant.

---
 drivers/gpu/nova-core/gpu.rs |  2 +-
 drivers/gpu/nova-core/gsp.rs | 78 ++++++++++++++++++------------------
 2 files changed, 41 insertions(+), 39 deletions(-)
drivers/gpu/nova-core/gpu.rs +1 -1
index 629c9d2dc994..50d76092fbdd 100644
@@ -281,7 +281,7 @@ pub(crate) fn new<'a>(
 
             sec2_falcon: Falcon::new(pdev.as_ref(), spec.chipset)?,
 
-            gsp <- Gsp::new(pdev)?,
+            gsp <- Gsp::new(pdev),
 
             _: { gsp.boot(pdev, bar, spec.chipset, gsp_falcon, sec2_falcon)? },
 
drivers/gpu/nova-core/gsp.rs +40 -38

83 lines not on this page · raw message

Signature · 1 line
2.52.0
author maintainer
#4 lore raw

PATCH 4/4 gpu: nova-core: gsp: move appropriate code into pin initializer

In reply to Danilo Krummrich

Relocate the code that technically fits in the pin initializer into the initializer itself.

While, thanks to pin_init_scope(), it is also possible to keep it as is, moving appropriate code into the initializer has the advantage that it structures the dependencies of fields naturally.

For instance, intermediate data that is only needed for a single field goes into the initializer block of this field, making it obvious that it is not needed by anything else.

On the other hand, intermediate data that is needed for multiple fields to initialize remains above the initializer, naturally indicating that it is needed my multiple fields.

---
 drivers/gpu/nova-core/gsp.rs | 61 +++++++++++++++++-------------------
 1 file changed, 28 insertions(+), 33 deletions(-)
drivers/gpu/nova-core/gsp.rs +28 -33
index 8bc86e1bcac5..766fd9905358 100644
@@ -122,41 +122,36 @@ impl Gsp {
     pub(crate) fn new(pdev: &pci::Device<device::Bound>) -> impl PinInit<Self, Error> + '_ {
         pin_init::pin_init_scope(move || {
             let dev = pdev.as_ref();
-            let libos = CoherentAllocation::<LibosMemoryRegionInitArgument>::alloc_coherent(
-                dev,
-                GSP_PAGE_SIZE / size_of::<LibosMemoryRegionInitArgument>(),
-                GFP_KERNEL | __GFP_ZERO,
-            )?;
-
-            // Initialise the logging structures. The OpenRM equivalents are in:
-            // _kgspInitLibosLoggingStructures (allocates memory for buffers)
-            // kgspSetupLibosInitArgs_IMPL (creates pLibosInitArgs[] array)
-            let loginit = LogBuffer::new(dev)?;
-            dma_write!(libos[0] = LibosMemoryRegionInitArgument::new("LOGINIT", &loginit.0))?;
-
-            let logintr = LogBuffer::new(dev)?;
-            dma_write!(libos[1] = LibosMemoryRegionInitArgument::new("LOGINTR", &logintr.0))?;
-
-            let logrm = LogBuffer::new(dev)?;
-            dma_write!(libos[2] = LibosMemoryRegionInitArgument::new("LOGRM", &logrm.0))?;
-
-            let cmdq = Cmdq::new(dev)?;
-
-            let rmargs = CoherentAllocation::<GspArgumentsCached>::alloc_coherent(
-                dev,
-                1,
-                GFP_KERNEL | __GFP_ZERO,
-            )?;
-            dma_write!(rmargs[0] = fw::GspArgumentsCached::new(&cmdq))?;
-            dma_write!(libos[3] = LibosMemoryRegionInitArgument::new("RMARGS", &rmargs))?;
 
             Ok(try_pin_init!(Self {
-                libos,
-                loginit,
-                logintr,
-                logrm,
-                rmargs,
-                cmdq,
+                libos: CoherentAllocation::<LibosMemoryRegionInitArgument>::alloc_coherent(
+                    dev,
+                    GSP_PAGE_SIZE / size_of::<LibosMemoryRegionInitArgument>(),
+                    GFP_KERNEL | __GFP_ZERO,
+                )?,
+                loginit: LogBuffer::new(dev)?,
+                logintr: LogBuffer::new(dev)?,
+                logrm: LogBuffer::new(dev)?,
+                cmdq: Cmdq::new(dev)?,
+                rmargs: CoherentAllocation::<GspArgumentsCached>::alloc_coherent(
+                    dev,
+                    1,
+                    GFP_KERNEL | __GFP_ZERO,
+                )?,
+                _: {
+                    // Initialise the logging structures. The OpenRM equivalents are in:
+                    // _kgspInitLibosLoggingStructures (allocates memory for buffers)
+                    // kgspSetupLibosInitArgs_IMPL (creates pLibosInitArgs[] array)
+                    dma_write!(
+                        libos[0] = LibosMemoryRegionInitArgument::new("LOGINIT", &loginit.0)
+                    )?;
+                    dma_write!(
+                        libos[1] = LibosMemoryRegionInitArgument::new("LOGINTR", &logintr.0)
+                    )?;
+                    dma_write!(libos[2] = LibosMemoryRegionInitArgument::new("LOGRM", &logrm.0))?;
+                    dma_write!(rmargs[0] = fw::GspArgumentsCached::new(cmdq))?;
+                    dma_write!(libos[3] = LibosMemoryRegionInitArgument::new("RMARGS", rmargs))?;
+                },
             }))
         })
     }
Signature · 1 line
2.52.0
Reviewed-by on the v1 series
#5 lore raw
In reply to Danilo Krummrich
Quoted 24 lines · Danilo Krummrich

This is a minor series to improve some of the pin initializer code to:

1. Reduce redundancy caused by Result<impl PinInit<T, Error>> return values with pin_init_scope().

2. Relocate code that technically fits in the pin initializer into the initializer itself.

While, thanks to pin_init_scope(), it is also possible to keep it as is, moving appropriate code into the initializer has the advantage that it structures the dependencies of fields naturally.

For instance, intermediate data that is only needed for a single field goes into the initializer block of this field, making it obvious that it is not needed by anything else.

On the other hand, intermediate data that is needed for multiple fields to initialize remains above the initializer, naturally indicating that it is needed my multiple fields.

Quoted 5 lines

Danilo Krummrich (4):
gpu: nova-core: fw: get rid of redundant Result in GspFirmware::new()
gpu: nova-core: fw: move appropriate code into pin initializer
gpu: nova-core: gsp: get rid of redundant Result in Gsp::new()
gpu: nova-core: gsp: move appropriate code into pin initializer

thanks,

- Joel

Quoted 12 lines
drivers/gpu/nova-core/firmware/gsp.rs | 134 +++++++++++++-------------
drivers/gpu/nova-core/gpu.rs          |   2 +-
drivers/gpu/nova-core/gsp.rs          |  73 +++++++-------
drivers/gpu/nova-core/gsp/boot.rs     |   5 +-
4 files changed, 106 insertions(+), 108 deletions(-)

base-commit: 97cf6bc0abd381fd84e5d8e978322a62a58fb00e --
2.52.0

author maintainer Applied
#6 lore raw
In reply to Danilo Krummrich
Quoted 7 lines · Danilo Krummrich

This is a minor series to improve some of the pin initializer code to:

1. Reduce redundancy caused by Result<impl PinInit<T, Error>> return values with pin_init_scope().

2. Relocate code that technically fits in the pin initializer into the initializer itself.

Applied to drm-rust-next, thanks!