Gpu::new() sets the DMA mask and maximum segment size from inside the
try_pin_init!() initializer, after Tree::new(). A follow-up change
replaces these setters with a DMA configuration step that returns a
handle, and that handle must be available to several fields of the
initializer.

Wrap the initializer in pin_init_scope(), compute the chip spec before
it, and set the DMA parameters right after the spec is known. This
keeps the reindentation out of the follow-up change.

No functional change, except that the DMA parameters are now set
before Tree::new() instead of after it.

Signed-off-by: Matteo Kloiber <[email protected]>
---
 drivers/gpu/nova-core/gpu.rs | 233 ++++++++++++++++++-----------------
 1 file changed, 118 insertions(+), 115 deletions(-)

diff --git a/drivers/gpu/nova-core/gpu.rs b/drivers/gpu/nova-core/gpu.rs
index 8ab1687847ff..993f1512c391 100644
--- a/drivers/gpu/nova-core/gpu.rs
+++ b/drivers/gpu/nova-core/gpu.rs
@@ -455,142 +455,145 @@ pub(crate) fn new<'a>(
     ) -> impl PinInit<Self, Error> + use<'gpu, 'a> {
         let dev = pdev.as_ref();
 
-        try_pin_init!(Self {
-            spec: Spec::new(dev, bar).inspect(|spec| {
-                dev_info!(dev,"NVIDIA ({})\n", spec);
-            })?,
+        pin_init::pin_init_scope(move || {
+            let spec = Spec::new(dev, bar).inspect(|spec| {
+                dev_info!(dev, "NVIDIA ({})\n", spec);
+            })?;
+            let dma_mask = hal::gpu_hal(spec.chipset).dma_mask();
+            // SAFETY: `Gpu` owns all DMA allocations for this device, and we 
are
+            // still constructing it, so no concurrent DMA allocations can 
exist.
+            unsafe { pdev.dma_set_mask_and_coherent(dma_mask)? };
+
+            // Nova walks SG segments to build page tables, so their length is
+            // irrelevant to the device.
+            //
+            // SAFETY: `Gpu` owns all DMA allocations for this device, and we 
are
+            // still constructing it, so no concurrent DMA allocations can 
exist.
+            unsafe { pdev.dma_set_max_seg_size(u32::MAX) };
+
+            Ok(try_pin_init!(Self {
+                spec,
+
+                tree: Tree::new(pdev, bar, spec.chipset, 
irq::gsp::GSP_SUBTREE.into())?,
+
+                _: {
+                    // We must wait for GFW_BOOT completion before doing any 
significant setup on
+                    // the GPU.
+                    Self::wait_gfw_boot_completion(dev, bar, spec.chipset)?;
+                },
 
-            tree: Tree::new(pdev, bar, spec.chipset, 
irq::gsp::GSP_SUBTREE.into())?,
+                // Initialize this early because `gsp_resources` depends on it.
+                sysmem_flush: SysmemFlush::register(dev, bar, spec.chipset)?,
 
-            _: {
-                let dma_mask = hal::gpu_hal(spec.chipset).dma_mask();
+                gsp_resources <- try_pin_init!(GspResources {
+                    device: pdev,
 
-                // SAFETY: `Gpu` owns all DMA allocations for this device, and 
we are
-                // still constructing it, so no concurrent DMA allocations can 
exist.
-                unsafe { pdev.dma_set_mask_and_coherent(dma_mask)? };
+                    spec: *spec,
 
-                // Nova walks SG segments to build page tables, so their 
length is
-                // irrelevant to the device.
-                //
-                // SAFETY: `Gpu` owns all DMA allocations for this device, and 
we are
-                // still constructing it, so no concurrent DMA allocations can 
exist.
-                unsafe { pdev.dma_set_max_seg_size(u32::MAX) };
+                    bar,
 
-                // We must wait for GFW_BOOT completion before doing any 
significant setup on the GPU.
-                Self::wait_gfw_boot_completion(dev, bar, spec.chipset)?;
-            },
+                    gsp_falcon: Falcon::new(dev, spec.chipset, bar)?,
 
-            // Initialize this early because `gsp_resources` depends on it.
-            sysmem_flush: SysmemFlush::register(dev, bar, spec.chipset)?,
+                    sec2_falcon: Falcon::new(dev, spec.chipset, bar)?,
 
-            gsp_resources <- try_pin_init!(GspResources {
-                device: pdev,
+                    fsp: Fsp::try_new(dev, bar, spec.chipset)?,
 
-                spec: *spec,
+                    timer: Timer::setup(dev, bar, spec.chipset, fsp.as_ref())?,
 
-                bar,
+                    vgpu: VgpuManager::new(pdev, spec.chipset, fsp.as_mut()),
 
-                gsp_falcon: Falcon::new(dev, spec.chipset, bar)?,
+                    gsp <- Gsp::new(pdev, bar),
 
-                sec2_falcon: Falcon::new(dev, spec.chipset, bar)?,
+                    // This member must be initialized last, so the 
`UnloadBundle` can never be
+                    // dropped from outside of the constructed `GspResources`, 
ensuring that the
+                    // unload sequence is properly run in case of failure.
+                    unload_bundle: gsp.boot(GspBootContext {
+                        pdev,
+                        bar,
+                        chipset: spec.chipset,
+                        gsp_falcon,
+                        sec2_falcon,
+                        fsp: fsp.as_mut(),
+                        vgpu,
+                    })?,
+                }),
 
-                fsp: Fsp::try_new(dev, bar, spec.chipset)?,
+                _: {
+                    irq::gsp::quiesce(tree.get_ref(), 
&gsp_resources.gsp_falcon);
+                },
 
-                timer: Timer::setup(dev, bar, spec.chipset, fsp.as_ref())?,
+                // SAFETY: The registration is owned by `Gpu`, which is 
dropped when the device is
+                // unbound, so the registration's `Drop` runs.
+                _gsp_subtree <- unsafe {
+                    GspSubtree::new(
+                        pdev,
+                        tree.get_ref(),
+                        &gsp_resources.get_ref().gsp_falcon,
+                        &gsp_resources.get_ref().gsp.cmdq,
+                    )
+                },
 
-                vgpu: VgpuManager::new(pdev, spec.chipset, fsp.as_mut()),
+                // No interrupt announces the messages that the GSP posted 
during boot, before the
+                // SWGEN0 latch was cleared.
+                _: {
+                    gsp_resources.gsp.cmdq.drain()?;
+                },
 
-                gsp <- Gsp::new(pdev, bar),
+                gsp_static_info: {
+                    // Obtain and display basic GPU information.
+                    let info = gsp_resources.gsp.get_static_info()?;
+                    match info.gpu_name() {
+                        Ok(name) => dev_info!(dev, "GPU name: {}\n", name),
+                        Err(e) => dev_warn!(dev, "GPU name unavailable: 
{:?}\n", e),
+                    }
 
-                // This member must be initialized last, so the `UnloadBundle` 
can never be dropped
-                // from outside of the constructed `GspResources`, ensuring 
that the unload sequence
-                // is properly run in case of failure.
-                unload_bundle: gsp.boot(GspBootContext {
-                    pdev,
-                    bar,
-                    chipset: spec.chipset,
-                    gsp_falcon,
-                    sec2_falcon,
-                    fsp: fsp.as_mut(),
-                    vgpu,
-                })?,
-            }),
-
-            _: {
-                irq::gsp::quiesce(tree.get_ref(), &gsp_resources.gsp_falcon);
-            },
-
-            // SAFETY: The registration is owned by `Gpu`, which is dropped 
when the device is
-            // unbound, so the registration's `Drop` runs.
-            _gsp_subtree <- unsafe {
-                GspSubtree::new(
-                    pdev,
-                    tree.get_ref(),
-                    &gsp_resources.get_ref().gsp_falcon,
-                    &gsp_resources.get_ref().gsp.cmdq,
-                )
-            },
-
-            // No interrupt announces the messages that the GSP posted during 
boot, before the
-            // SWGEN0 latch was cleared.
-            _: {
-                gsp_resources.gsp.cmdq.drain()?;
-            },
-
-            gsp_static_info: {
-                // Obtain and display basic GPU information.
-                let info = gsp_resources.gsp.get_static_info()?;
-                match info.gpu_name() {
-                    Ok(name) => dev_info!(dev, "GPU name: {}\n", name),
-                    Err(e) => dev_warn!(dev, "GPU name unavailable: {:?}\n", 
e),
-                }
+                    if !info.usable_fb_regions.is_empty() {
+                        dev_dbg!(dev, "Usable FB regions:\n");
+                        for region in &info.usable_fb_regions {
+                            dev_dbg!(dev, "  - {:#x?}\n", region);
+                        }
 
-                if !info.usable_fb_regions.is_empty() {
-                    dev_dbg!(dev, "Usable FB regions:\n");
-                    for region in &info.usable_fb_regions {
-                        dev_dbg!(dev, "  - {:#x?}\n", region);
+                        dev_dbg!(
+                            dev,
+                            "Total usable VRAM: {} MiB\n",
+                            info.vram_size() / u64::SZ_1M
+                        );
                     }
 
-                    dev_dbg!(
-                        dev,
-                        "Total usable VRAM: {} MiB\n",
-                        info.vram_size() / u64::SZ_1M
-                    );
-                }
-
-                info
-            },
+                    info
+                },
 
-            // Create GPU memory manager owning memory management resources.
-            mm: {
-                let usable_vram = 
gsp_static_info.usable_fb_regions.first().ok_or(ENODEV)?;
-                let buddy_params = GpuBuddyParams {
-                    base_offset: usable_vram.start,
-                    size: usable_vram.end - usable_vram.start,
-                    chunk_size: Alignment::SZ_4K,
-                };
+                // Create GPU memory manager owning memory management 
resources.
+                mm: {
+                    let usable_vram = 
gsp_static_info.usable_fb_regions.first().ok_or(ENODEV)?;
+                    let buddy_params = GpuBuddyParams {
+                        base_offset: usable_vram.start,
+                        size: usable_vram.end - usable_vram.start,
+                        chunk_size: Alignment::SZ_4K,
+                    };
+
+                    GpuMm::new(
+                        bar,
+                        gsp_resources.spec.chipset,
+                        buddy_params,
+                        VramAddress::from_raw(gsp_static_info.total_fb_end),
+                    )?
+                },
 
-                GpuMm::new(
-                    bar,
-                    gsp_resources.spec.chipset,
-                    buddy_params,
-                    VramAddress::from_raw(gsp_static_info.total_fb_end),
-                )?
-            },
-
-            // Create BAR1 user interface for CPU access to GPU virtual memory.
-            bar_user <- {
-                let pdb_addr = 
VramAddress::from_raw(gsp_static_info.bar1_pde_base);
-                let bar1_idx = crate::driver::bar1_resource_index(pdev)?;
-                let bar1_size = pdev.resource_len(bar1_idx)?;
-
-                BarUser::new(
-                    pdb_addr,
-                    gsp_resources.spec.chipset,
-                    bar1_size,
-                    bar1,
-                )
-            },
+                // Create BAR1 user interface for CPU access to GPU virtual 
memory.
+                bar_user <- {
+                    let pdb_addr = 
VramAddress::from_raw(gsp_static_info.bar1_pde_base);
+                    let bar1_idx = crate::driver::bar1_resource_index(pdev)?;
+                    let bar1_size = pdev.resource_len(bar1_idx)?;
+
+                    BarUser::new(
+                        pdb_addr,
+                        gsp_resources.spec.chipset,
+                        bar1_size,
+                        bar1,
+                    )
+                },
+            }))
         })
     }
 
-- 
2.51.2

Reply via email to