rust_dma.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // SPDX-License-Identifier: GPL-2.0
  2. //! Rust DMA api test (based on QEMU's `pci-testdev`).
  3. //!
  4. //! To make this driver probe, QEMU must be run with `-device pci-testdev`.
  5. use kernel::{
  6. device::Core,
  7. dma::{CoherentAllocation, DataDirection, Device, DmaMask},
  8. page, pci,
  9. prelude::*,
  10. scatterlist::{Owned, SGTable},
  11. sync::aref::ARef,
  12. };
  13. #[pin_data(PinnedDrop)]
  14. struct DmaSampleDriver {
  15. pdev: ARef<pci::Device>,
  16. ca: CoherentAllocation<MyStruct>,
  17. #[pin]
  18. sgt: SGTable<Owned<VVec<u8>>>,
  19. }
  20. const TEST_VALUES: [(u32, u32); 5] = [
  21. (0xa, 0xb),
  22. (0xc, 0xd),
  23. (0xe, 0xf),
  24. (0xab, 0xba),
  25. (0xcd, 0xef),
  26. ];
  27. struct MyStruct {
  28. h: u32,
  29. b: u32,
  30. }
  31. impl MyStruct {
  32. fn new(h: u32, b: u32) -> Self {
  33. Self { h, b }
  34. }
  35. }
  36. // SAFETY: All bit patterns are acceptable values for `MyStruct`.
  37. unsafe impl kernel::transmute::AsBytes for MyStruct {}
  38. // SAFETY: Instances of `MyStruct` have no uninitialized portions.
  39. unsafe impl kernel::transmute::FromBytes for MyStruct {}
  40. kernel::pci_device_table!(
  41. PCI_TABLE,
  42. MODULE_PCI_TABLE,
  43. <DmaSampleDriver as pci::Driver>::IdInfo,
  44. [(pci::DeviceId::from_id(pci::Vendor::REDHAT, 0x5), ())]
  45. );
  46. impl pci::Driver for DmaSampleDriver {
  47. type IdInfo = ();
  48. const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
  49. fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> impl PinInit<Self, Error> {
  50. pin_init::pin_init_scope(move || {
  51. dev_info!(pdev, "Probe DMA test driver.\n");
  52. let mask = DmaMask::new::<64>();
  53. // SAFETY: There are no concurrent calls to DMA allocation and mapping primitives.
  54. unsafe { pdev.dma_set_mask_and_coherent(mask)? };
  55. let ca: CoherentAllocation<MyStruct> =
  56. CoherentAllocation::alloc_coherent(pdev.as_ref(), TEST_VALUES.len(), GFP_KERNEL)?;
  57. for (i, value) in TEST_VALUES.into_iter().enumerate() {
  58. kernel::dma_write!(ca, [i]?, MyStruct::new(value.0, value.1));
  59. }
  60. let size = 4 * page::PAGE_SIZE;
  61. let pages = VVec::with_capacity(size, GFP_KERNEL)?;
  62. let sgt = SGTable::new(pdev.as_ref(), pages, DataDirection::ToDevice, GFP_KERNEL);
  63. Ok(try_pin_init!(Self {
  64. pdev: pdev.into(),
  65. ca,
  66. sgt <- sgt,
  67. }))
  68. })
  69. }
  70. }
  71. impl DmaSampleDriver {
  72. fn check_dma(&self) -> Result {
  73. for (i, value) in TEST_VALUES.into_iter().enumerate() {
  74. let val0 = kernel::dma_read!(self.ca, [i]?.h);
  75. let val1 = kernel::dma_read!(self.ca, [i]?.b);
  76. assert_eq!(val0, value.0);
  77. assert_eq!(val1, value.1);
  78. }
  79. Ok(())
  80. }
  81. }
  82. #[pinned_drop]
  83. impl PinnedDrop for DmaSampleDriver {
  84. fn drop(self: Pin<&mut Self>) {
  85. dev_info!(self.pdev, "Unload DMA test driver.\n");
  86. assert!(self.check_dma().is_ok());
  87. for (i, entry) in self.sgt.iter().enumerate() {
  88. dev_info!(
  89. self.pdev,
  90. "Entry[{}]: DMA address: {:#x}",
  91. i,
  92. entry.dma_address(),
  93. );
  94. }
  95. }
  96. }
  97. kernel::module_pci_driver! {
  98. type: DmaSampleDriver,
  99. name: "rust_dma",
  100. authors: ["Abdiel Janulgue"],
  101. description: "Rust DMA test",
  102. license: "GPL v2",
  103. }