dma.rs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // SPDX-License-Identifier: GPL-2.0
  2. //! Simple DMA object wrapper.
  3. use core::ops::{
  4. Deref,
  5. DerefMut, //
  6. };
  7. use kernel::{
  8. device,
  9. dma::CoherentAllocation,
  10. page::PAGE_SIZE,
  11. prelude::*, //
  12. };
  13. pub(crate) struct DmaObject {
  14. dma: CoherentAllocation<u8>,
  15. }
  16. impl DmaObject {
  17. pub(crate) fn new(dev: &device::Device<device::Bound>, len: usize) -> Result<Self> {
  18. let len = core::alloc::Layout::from_size_align(len, PAGE_SIZE)
  19. .map_err(|_| EINVAL)?
  20. .pad_to_align()
  21. .size();
  22. let dma = CoherentAllocation::alloc_coherent(dev, len, GFP_KERNEL | __GFP_ZERO)?;
  23. Ok(Self { dma })
  24. }
  25. pub(crate) fn from_data(dev: &device::Device<device::Bound>, data: &[u8]) -> Result<Self> {
  26. Self::new(dev, data.len()).and_then(|mut dma_obj| {
  27. // SAFETY: We have just allocated the DMA memory, we are the only users and
  28. // we haven't made the device aware of the handle yet.
  29. unsafe { dma_obj.write(data, 0)? }
  30. Ok(dma_obj)
  31. })
  32. }
  33. }
  34. impl Deref for DmaObject {
  35. type Target = CoherentAllocation<u8>;
  36. fn deref(&self) -> &Self::Target {
  37. &self.dma
  38. }
  39. }
  40. impl DerefMut for DmaObject {
  41. fn deref_mut(&mut self) -> &mut Self::Target {
  42. &mut self.dma
  43. }
  44. }