mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Eliot Courtney <ecourtney@nvidia.com>
To: Danilo Krummrich <dakr@kernel.org>,
	Alice Ryhl <aliceryhl@google.com>,
	 Alexandre Courbot <acourbot@nvidia.com>,
	David Airlie <airlied@gmail.com>,
	 Simona Vetter <simona@ffwll.ch>
Cc: John Hubbard <jhubbard@nvidia.com>,
	 Alistair Popple <apopple@nvidia.com>,
	Timur Tabi <ttabi@nvidia.com>,
	 nova-gpu@lists.linux.dev, dri-devel@lists.freedesktop.org,
	 linux-kernel@vger.kernel.org,
	Eliot Courtney <ecourtney@nvidia.com>
Subject: [PATCH 4/7] gpu: nova-core: falcon: separate staging data for DMA from its location
Date: Thu, 26 Nov 2026 17:20:13 +0900	[thread overview]
Message-ID: <20261126-boot-rework2-v1-4-99a1dc6c7cc4@nvidia.com> (raw)
In-Reply-To: <20261126-boot-rework2-v1-0-99a1dc6c7cc4@nvidia.com>

Introduce a new enum `FalconSegmentSource` that holds the source of
each segment to copy to a falcon. This replaces the existing
`FalconDmaSource` enum which mixed the jobs of knowing where the bytes
to copy to the falcon live with how they are copied (DMA).

Since `FalconDmaSource` is removed (to separate how data is sent to a
falcon from where it lives), merge the staging of the data that gets
DMA'd to a falcon with actually performing the copy. This separates the
decision to stage it for DMA from where the data is living.

This is useful for allowing DMA from framebuffer memory and sysmem to
falcon in a unified way.

Signed-off-by: Eliot Courtney <ecourtney@nvidia.com>
---
 drivers/gpu/nova-core/falcon.rs | 126 ++++++++++++++++++++++++----------------
 1 file changed, 75 insertions(+), 51 deletions(-)

diff --git a/drivers/gpu/nova-core/falcon.rs b/drivers/gpu/nova-core/falcon.rs
index 7dfe71e4cd49..f0e6f0eea641 100644
--- a/drivers/gpu/nova-core/falcon.rs
+++ b/drivers/gpu/nova-core/falcon.rs
@@ -12,7 +12,6 @@
         DmaAddress, //
     },
     io::{
-        io_project,
         poll::read_poll_timeout,
         register::Array,
         Io,
@@ -140,15 +139,6 @@ pub(crate) enum FalconMem {
     Dmem { tag: bool },
 }
 
-/// Where a falcon DMA transfer reads from.
-pub(crate) enum FalconDmaSource<'a> {
-    /// A DMA object in system memory.
-    Sysmem(&'a Coherent<'a, [u8]>),
-    /// GPU framebuffer memory at physical address `base`.
-    #[expect(dead_code)]
-    Framebuffer { base: u64 },
-}
-
 bounded_enum! {
     /// Defines the Framebuffer Interface (FBIF) aperture type.
     /// This determines the memory type for external memory access during a DMA transfer, which is
@@ -208,6 +198,16 @@ pub(crate) struct FalconDmaLoadTarget {
     pub(crate) len: u32,
 }
 
+impl FalconDmaLoadTarget {
+    /// Returns the data that should be copied to the falcon from `blob`.
+    fn data<'a>(&self, blob: &'a [u8]) -> Result<&'a [u8]> {
+        let start = usize::from_safe_cast(self.src_start);
+        let end = start + usize::from_safe_cast(self.len);
+
+        blob.get(start..end).ok_or(EINVAL)
+    }
+}
+
 /// Parameters for the falcon boot ROM.
 #[derive(Debug, Clone)]
 pub(crate) struct FalconBromParams {
@@ -219,6 +219,21 @@ pub(crate) struct FalconBromParams {
     pub(crate) ucode_id: u8,
 }
 
+/// Denotes where the bytes of a firmware segment are.
+#[derive(Clone, Copy)]
+pub(crate) enum FalconSegmentSource<'a> {
+    /// Bytes in system memory.
+    Sysmem(&'a [u8]),
+    /// Bytes in the framebuffer.
+    #[expect(dead_code)]
+    Framebuffer {
+        /// DMA base address of the segment in the framebuffer.
+        base: u64,
+        /// Number of bytes in the segment.
+        len: u32,
+    },
+}
+
 /// Trait implemented by falcon firmwares that can be loaded using DMA.
 pub(crate) trait FalconDmaLoadable {
     /// Returns the firmware data as a slice of bytes.
@@ -545,36 +560,39 @@ pub(crate) fn with_fbif_transcfg<R>(
         Ok(result)
     }
 
-    /// Perform a DMA write according to `load_offsets` from `src` into the falcon's `target_mem`.
+    /// Perform a DMA write from `src` at `src_offset` into the falcon's `target_mem` at
+    /// `dst_start`.
     fn dma_wr(
         &self,
-        src: FalconDmaSource<'_>,
+        src: FalconSegmentSource<'_>,
         target_mem: FalconMem,
-        load_offsets: FalconDmaLoadTarget,
+        src_offset: u32,
+        dst_start: u32,
     ) -> Result {
         const DMA_LEN: u32 = cv!(MEM_BLOCK_ALIGNMENT);
 
-        // DMA transfers can only be done in units of 256 bytes. Compute how many such transfers we
-        // need to perform.
-        let num_transfers = load_offsets.len.div_ceil(DMA_LEN);
-
-        let (dma_base, fbif_target) = match src {
-            FalconDmaSource::Sysmem(dma_obj) => {
-                // Upper limit of transfer is `(num_transfers * DMA_LEN) + load_offsets.src_start`.
-                let end_offset = num_transfers
-                    .checked_mul(DMA_LEN)
-                    .and_then(|size| size.checked_add(load_offsets.src_start))
-                    .map(usize::from_safe_cast)
-                    .ok_or(EOVERFLOW)?;
+        // Declare `dma_obj` here so it lives long enough for transfers to
+        // complete.
+        let dma_obj;
+        let (dma_base, fbif_target, len) = match src {
+            FalconSegmentSource::Sysmem(data) => {
+                dma_obj = self.dma_stage(data, src_offset)?;
 
                 (
-                    io_project!(dma_obj, [try: 0..end_offset]).dma_address(),
+                    dma_obj.dma_address(),
                     FalconFbifTarget::CoherentSysmem,
+                    u32::try_from(data.len())?,
                 )
             }
-            FalconDmaSource::Framebuffer { base } => (base, FalconFbifTarget::LocalFb),
+            FalconSegmentSource::Framebuffer { base, len } => {
+                (base, FalconFbifTarget::LocalFb, len)
+            }
         };
 
+        // DMA transfers can only be done in units of 256 bytes. Compute how many such transfers we
+        // need to perform.
+        let num_transfers = len.div_ceil(DMA_LEN);
+
         if dma_base % DmaAddress::from(DMA_LEN) > 0 {
             dev_err!(
                 self.dev,
@@ -610,12 +628,12 @@ fn dma_wr(
             // Perform a transfer of size `DMA_LEN`.
             self.pfalcon.write_reg(
                 regs::NV_PFALCON_FALCON_DMATRFMOFFS::zeroed()
-                    .try_with_offs(load_offsets.dst_start.checked_add(pos).ok_or(EOVERFLOW)?)?,
+                    .try_with_offs(dst_start.checked_add(pos).ok_or(EOVERFLOW)?)?,
             );
             // The source offset doubles as the virtual address tag for IMEM and tagged DMEM.
             self.pfalcon.write_reg(
                 regs::NV_PFALCON_FALCON_DMATRFFBOFFS::zeroed()
-                    .with_offs(load_offsets.src_start.checked_add(pos).ok_or(EOVERFLOW)?),
+                    .with_offs(src_offset.checked_add(pos).ok_or(EOVERFLOW)?),
             );
 
             self.pfalcon.write_reg(cmd);
@@ -634,38 +652,44 @@ fn dma_wr(
         Ok(())
     }
 
+    /// Returns a DMA object with `data` at `offset`, padded with zeros up to
+    /// the next multiple of `MEM_BLOCK_ALIGNMENT`.
+    fn dma_stage(&self, data: &[u8], offset: u32) -> Result<Coherent<'a, [u8]>> {
+        let offset = usize::from_safe_cast(offset);
+
+        // DMA copies are done in chunks of `MEM_BLOCK_ALIGNMENT`, so pad the length
+        // accordingly and fill with `0`.
+        let mut dma_obj = CoherentBox::zeroed_slice(
+            self.dev,
+            offset + data.len().next_multiple_of(MEM_BLOCK_ALIGNMENT),
+            GFP_KERNEL,
+        )?;
+
+        // PANIC: `dma_obj` has at least `offset + data.len()` bytes.
+        dma_obj[offset..offset + data.len()].copy_from_slice(data);
+
+        Ok(dma_obj.into())
+    }
+
     /// Perform a DMA load into `IMEM` and `DMEM` of `fw`.
     fn dma_load<F: FalconFirmware<Target = E> + FalconDmaLoadable>(&self, fw: &F) -> Result {
-        // DMA object with firmware content as the source of the DMA engine.
-        let dma_obj = {
-            let fw_slice = fw.as_slice();
-
-            // DMA copies are done in chunks of `MEM_BLOCK_ALIGNMENT`, so pad the length
-            // accordingly and fill with `0`.
-            let mut dma_obj = CoherentBox::zeroed_slice(
-                self.dev,
-                fw_slice.len().next_multiple_of(MEM_BLOCK_ALIGNMENT),
-                GFP_KERNEL,
-            )?;
-
-            // PANIC: `dma_obj` has been created with a length equal to or larger than
-            // `fw_slice.len()`, so the range `..fw_slice.len()` is valid.
-            dma_obj[..fw_slice.len()].copy_from_slice(fw_slice);
-
-            dma_obj.into()
-        };
+        let blob = fw.as_slice();
+        let imem_sec = fw.imem_sec_load_params();
+        let dmem = fw.dmem_load_params();
 
         self.dma_reset();
 
         self.dma_wr(
-            FalconDmaSource::Sysmem(&dma_obj),
+            FalconSegmentSource::Sysmem(imem_sec.data(blob)?),
             FalconMem::ImemSecure,
-            fw.imem_sec_load_params(),
+            imem_sec.src_start,
+            imem_sec.dst_start,
         )?;
         self.dma_wr(
-            FalconDmaSource::Sysmem(&dma_obj),
+            FalconSegmentSource::Sysmem(dmem.data(blob)?),
             FalconMem::Dmem { tag: false },
-            fw.dmem_load_params(),
+            0,
+            dmem.dst_start,
         )
     }
 

-- 
2.56.1


  parent reply	other threads:[~2026-11-26  8:20 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  8:20 [PATCH 0/7] gpu: nova-core: merge `load` and `boot` for falcons Eliot Courtney
2026-11-26  8:20 ` [PATCH 1/7] gpu: nova-core: falcon: make pfalcon private Eliot Courtney
2026-11-26  8:20 ` [PATCH 2/7] gpu: nova-core: gsp: remove unnecessary start of `gsp_falcon` Eliot Courtney
2026-11-26  8:20 ` [PATCH 3/7] gpu: nova-core: falcon: program BROM and BOOTVEC in `boot` Eliot Courtney
2026-11-26  8:20 ` Eliot Courtney [this message]
2026-11-26  8:20 ` [PATCH 5/7] gpu: nova-core: falcon: add `FalconImage` for `FalconFirmware` trait Eliot Courtney
2026-11-26  8:20 ` [PATCH 6/7] gpu: nova-core: falcon: rename FalconDmaLoadTarget to FalconLoadTarget Eliot Courtney
2026-11-26  8:20 ` [PATCH 7/7] gpu: nova-core: falcon: load the firmware image in Falcon::boot Eliot Courtney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261126-boot-rework2-v1-4-99a1dc6c7cc4@nvidia.com \
    --to=ecourtney@nvidia.com \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=apopple@nvidia.com \
    --cc=dakr@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=jhubbard@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nova-gpu@lists.linux.dev \
    --cc=simona@ffwll.ch \
    --cc=ttabi@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®