[PATCH 4/7] gpu: nova-core: falcon: separate staging data for DMA from its location

From: Eliot Courtney

Date: Thu Nov 26 2026 - 03:21:12 EST


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@xxxxxxxxxx>
---
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