1
0
mirror of https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git synced 2026-01-12 01:20:14 +00:00
Daniel del Castillo 3577e265e4 gpu: nova-core: Simplify DmaObject::from_data in nova-core/dma.rs
This patch solves one of the existing mentions of COHA, a task
in the Nova task list about improving the `CoherentAllocation` API.
It uses the `write` method from `CoherentAllocation`.

Signed-off-by: Daniel del Castillo <delcastillodelarosadaniel@gmail.com>
[acourbot@nvidia.com: set prefix to "gpu: nova-core:".]
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
Message-ID: <20251104193756.57726-3-delcastillodelarosadaniel@gmail.com>
2025-11-07 23:10:44 +09:00

55 lines
1.3 KiB
Rust

// SPDX-License-Identifier: GPL-2.0
//! Simple DMA object wrapper.
use core::ops::{
Deref,
DerefMut, //
};
use kernel::{
device,
dma::CoherentAllocation,
page::PAGE_SIZE,
prelude::*, //
};
pub(crate) struct DmaObject {
dma: CoherentAllocation<u8>,
}
impl DmaObject {
pub(crate) fn new(dev: &device::Device<device::Bound>, len: usize) -> Result<Self> {
let len = core::alloc::Layout::from_size_align(len, PAGE_SIZE)
.map_err(|_| EINVAL)?
.pad_to_align()
.size();
let dma = CoherentAllocation::alloc_coherent(dev, len, GFP_KERNEL | __GFP_ZERO)?;
Ok(Self { dma })
}
pub(crate) fn from_data(dev: &device::Device<device::Bound>, data: &[u8]) -> Result<Self> {
Self::new(dev, data.len()).and_then(|mut dma_obj| {
// SAFETY: We have just allocated the DMA memory, we are the only users and
// we haven't made the device aware of the handle yet.
unsafe { dma_obj.write(data, 0)? }
Ok(dma_obj)
})
}
}
impl Deref for DmaObject {
type Target = CoherentAllocation<u8>;
fn deref(&self) -> &Self::Target {
&self.dma
}
}
impl DerefMut for DmaObject {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.dma
}
}