Skip to content

Commit 9bcc046

Browse files
GnurouDanilo Krummrich
authored andcommitted
gpu: nova-core: add DMA object struct
Since we will need to allocate lots of distinct memory chunks to be shared between GPU and CPU, introduce a type dedicated to that. It is a light wrapper around CoherentAllocation. Reviewed-by: Lyude Paul <lyude@redhat.com> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com> Link: https://lore.kernel.org/r/20250619-nova-frts-v6-13-ecf41ef99252@nvidia.com Signed-off-by: Danilo Krummrich <dakr@kernel.org>
1 parent bbe5db7 commit 9bcc046

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

drivers/gpu/nova-core/dma.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Simple DMA object wrapper.
4+
5+
// To be removed when all code is used.
6+
#![expect(dead_code)]
7+
8+
use core::ops::{Deref, DerefMut};
9+
10+
use kernel::device;
11+
use kernel::dma::CoherentAllocation;
12+
use kernel::page::PAGE_SIZE;
13+
use kernel::prelude::*;
14+
15+
pub(crate) struct DmaObject {
16+
dma: CoherentAllocation<u8>,
17+
}
18+
19+
impl DmaObject {
20+
pub(crate) fn new(dev: &device::Device<device::Bound>, len: usize) -> Result<Self> {
21+
let len = core::alloc::Layout::from_size_align(len, PAGE_SIZE)
22+
.map_err(|_| EINVAL)?
23+
.pad_to_align()
24+
.size();
25+
let dma = CoherentAllocation::alloc_coherent(dev, len, GFP_KERNEL | __GFP_ZERO)?;
26+
27+
Ok(Self { dma })
28+
}
29+
30+
pub(crate) fn from_data(dev: &device::Device<device::Bound>, data: &[u8]) -> Result<Self> {
31+
Self::new(dev, data.len()).map(|mut dma_obj| {
32+
// TODO: replace with `CoherentAllocation::write()` once available.
33+
// SAFETY:
34+
// - `dma_obj`'s size is at least `data.len()`.
35+
// - We have just created this object and there is no other user at this stage.
36+
unsafe {
37+
core::ptr::copy_nonoverlapping(
38+
data.as_ptr(),
39+
dma_obj.dma.start_ptr_mut(),
40+
data.len(),
41+
);
42+
}
43+
44+
dma_obj
45+
})
46+
}
47+
}
48+
49+
impl Deref for DmaObject {
50+
type Target = CoherentAllocation<u8>;
51+
52+
fn deref(&self) -> &Self::Target {
53+
&self.dma
54+
}
55+
}
56+
57+
impl DerefMut for DmaObject {
58+
fn deref_mut(&mut self) -> &mut Self::Target {
59+
&mut self.dma
60+
}
61+
}

drivers/gpu/nova-core/nova_core.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
//! Nova Core GPU Driver
44
5+
mod dma;
56
mod driver;
67
mod firmware;
78
mod gfw;

0 commit comments

Comments
 (0)