| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 |
|
| 7 | use crate::{
|
| 8 | bindings,
|
| 9 | debugfs,
|
| 10 | device::{
|
| 11 | self,
|
| 12 | Bound,
|
| 13 | Core, |
| 14 | },
|
| 15 | error::to_result,
|
| 16 | fs::file,
|
| 17 | prelude::*,
|
| 18 | ptr::KnownSize,
|
| 19 | sync::aref::ARef,
|
| 20 | transmute::{
|
| 21 | AsBytes,
|
| 22 | FromBytes, |
| 23 | }, |
| 24 | uaccess::UserSliceWriter,
|
| 25 | };
|
| 26 | use core::{
|
| 27 | ops::{
|
| 28 | Deref,
|
| 29 | DerefMut, |
| 30 | },
|
| 31 | ptr::NonNull, |
| 32 | };
|
| 33 |
|
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | pub type DmaAddress = bindings::dma_addr_t;
|
| 43 |
|
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | #[cfg_attr(CONFIG_PCI, doc = "* [`pci::Device`](kernel::pci::Device)")]
|
| 49 | |
| 50 | pub trait Device<'a>: AsRef<device::Device<Core<'a>>> {
|
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | unsafe fn dma_set_mask(&self, mask: DmaMask) -> Result {
|
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | to_result(unsafe { bindings::dma_set_mask(self.as_ref().as_raw(), mask.value()) })
|
| 66 | }
|
| 67 |
|
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | unsafe fn dma_set_coherent_mask(&self, mask: DmaMask) -> Result {
|
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | to_result(unsafe { bindings::dma_set_coherent_mask(self.as_ref().as_raw(), mask.value()) })
|
| 83 | }
|
| 84 |
|
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | unsafe fn dma_set_mask_and_coherent(&self, mask: DmaMask) -> Result {
|
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | to_result(unsafe {
|
| 102 | bindings::dma_set_mask_and_coherent(self.as_ref().as_raw(), mask.value())
|
| 103 | })
|
| 104 | }
|
| 105 |
|
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | unsafe fn dma_set_max_seg_size(&self, size: u32) {
|
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | unsafe { bindings::dma_set_max_seg_size(self.as_ref().as_raw(), size) }
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | #[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
| 131 | pub struct DmaMask(u64);
|
| 132 |
|
| 133 | impl DmaMask {
|
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | #[inline]
|
| 157 | pub const fn new<const N: u32>() -> Self {
|
| 158 | let Ok(mask) = Self::try_new(N) else {
|
| 159 | build_error!("Invalid DMA Mask.");
|
| 160 | };
|
| 161 |
|
| 162 | mask
|
| 163 | }
|
| 164 |
|
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | #[inline]
|
| 189 | pub const fn try_new(n: u32) -> Result<Self> {
|
| 190 | Ok(Self(match n {
|
| 191 | 0 => 0,
|
| 192 | 1..=64 => u64::MAX >> (64 - n),
|
| 193 | _ => return Err(EINVAL),
|
| 194 | }))
|
| 195 | }
|
| 196 |
|
| 197 | |
| 198 | #[inline]
|
| 199 | pub const fn value(&self) -> u64 {
|
| 200 | self.0
|
| 201 | }
|
| 202 | }
|
| 203 |
|
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | #[derive(Clone, Copy, PartialEq)]
|
| 223 | #[repr(transparent)]
|
| 224 | pub struct Attrs(u32);
|
| 225 |
|
| 226 | impl Attrs {
|
| 227 | |
| 228 | pub(crate) fn as_raw(self) -> crate::ffi::c_ulong {
|
| 229 | self.0 as crate::ffi::c_ulong
|
| 230 | }
|
| 231 |
|
| 232 | |
| 233 | pub fn contains(self, flags: Attrs) -> bool {
|
| 234 | (self & flags) == flags
|
| 235 | }
|
| 236 | }
|
| 237 |
|
| 238 | impl core::ops::BitOr for Attrs {
|
| 239 | type Output = Self;
|
| 240 | fn bitor(self, rhs: Self) -> Self::Output {
|
| 241 | Self(self.0 | rhs.0)
|
| 242 | }
|
| 243 | }
|
| 244 |
|
| 245 | impl core::ops::BitAnd for Attrs {
|
| 246 | type Output = Self;
|
| 247 | fn bitand(self, rhs: Self) -> Self::Output {
|
| 248 | Self(self.0 & rhs.0)
|
| 249 | }
|
| 250 | }
|
| 251 |
|
| 252 | impl core::ops::Not for Attrs {
|
| 253 | type Output = Self;
|
| 254 | fn not(self) -> Self::Output {
|
| 255 | Self(!self.0)
|
| 256 | }
|
| 257 | }
|
| 258 |
|
| 259 | |
| 260 | pub mod attrs {
|
| 261 | use super::Attrs;
|
| 262 |
|
| 263 | |
| 264 | |
| 265 | pub const DMA_ATTR_WEAK_ORDERING: Attrs = Attrs(bindings::DMA_ATTR_WEAK_ORDERING);
|
| 266 |
|
| 267 | |
| 268 | pub const DMA_ATTR_WRITE_COMBINE: Attrs = Attrs(bindings::DMA_ATTR_WRITE_COMBINE);
|
| 269 |
|
| 270 | |
| 271 | |
| 272 | pub const DMA_ATTR_SKIP_CPU_SYNC: Attrs = Attrs(bindings::DMA_ATTR_SKIP_CPU_SYNC);
|
| 273 |
|
| 274 | |
| 275 | pub const DMA_ATTR_FORCE_CONTIGUOUS: Attrs = Attrs(bindings::DMA_ATTR_FORCE_CONTIGUOUS);
|
| 276 |
|
| 277 | |
| 278 | |
| 279 | pub const DMA_ATTR_ALLOC_SINGLE_PAGES: Attrs = Attrs(bindings::DMA_ATTR_ALLOC_SINGLE_PAGES);
|
| 280 |
|
| 281 | |
| 282 | |
| 283 | pub const DMA_ATTR_NO_WARN: Attrs = Attrs(bindings::DMA_ATTR_NO_WARN);
|
| 284 |
|
| 285 | |
| 286 | |
| 287 | pub const DMA_ATTR_PRIVILEGED: Attrs = Attrs(bindings::DMA_ATTR_PRIVILEGED);
|
| 288 |
|
| 289 | |
| 290 | pub const DMA_ATTR_MMIO: Attrs = Attrs(bindings::DMA_ATTR_MMIO);
|
| 291 | }
|
| 292 |
|
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | #[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
| 299 | #[repr(u32)]
|
| 300 | pub enum DataDirection {
|
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | Bidirectional = Self::const_cast(bindings::dma_data_direction_DMA_BIDIRECTIONAL),
|
| 306 |
|
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | ToDevice = Self::const_cast(bindings::dma_data_direction_DMA_TO_DEVICE),
|
| 312 |
|
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | FromDevice = Self::const_cast(bindings::dma_data_direction_DMA_FROM_DEVICE),
|
| 318 |
|
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | None = Self::const_cast(bindings::dma_data_direction_DMA_NONE),
|
| 324 | }
|
| 325 |
|
| 326 | impl DataDirection {
|
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | const fn const_cast(val: bindings::dma_data_direction) -> u32 {
|
| 332 | |
| 333 | |
| 334 | |
| 335 | let wide_val = val as i128;
|
| 336 |
|
| 337 | |
| 338 | |
| 339 | if wide_val < 0 || wide_val > u32::MAX as i128 {
|
| 340 | |
| 341 | build_error!("C enum value is out of bounds for the target type `u32`.");
|
| 342 | }
|
| 343 |
|
| 344 | |
| 345 | |
| 346 | wide_val as u32
|
| 347 | }
|
| 348 | }
|
| 349 |
|
| 350 | impl From<DataDirection> for bindings::dma_data_direction {
|
| 351 | |
| 352 | fn from(direction: DataDirection) -> Self {
|
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | direction as u32 as Self
|
| 358 | }
|
| 359 | }
|
| 360 |
|
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | pub struct CoherentBox<T: KnownSize + ?Sized>(Coherent<T>);
|
| 411 |
|
| 412 | impl<T: AsBytes + FromBytes> CoherentBox<[T]> {
|
| 413 | |
| 414 | #[inline]
|
| 415 | pub fn zeroed_slice_with_attrs(
|
| 416 | dev: &device::Device<Bound>,
|
| 417 | count: usize,
|
| 418 | gfp_flags: kernel::alloc::Flags,
|
| 419 | dma_attrs: Attrs,
|
| 420 | ) -> Result<Self> {
|
| 421 | Coherent::zeroed_slice_with_attrs(dev, count, gfp_flags, dma_attrs).map(Self)
|
| 422 | }
|
| 423 |
|
| 424 | |
| 425 | #[inline]
|
| 426 | pub fn zeroed_slice(
|
| 427 | dev: &device::Device<Bound>,
|
| 428 | count: usize,
|
| 429 | gfp_flags: kernel::alloc::Flags,
|
| 430 | ) -> Result<Self> {
|
| 431 | Self::zeroed_slice_with_attrs(dev, count, gfp_flags, Attrs(0))
|
| 432 | }
|
| 433 |
|
| 434 | |
| 435 | |
| 436 | |
| 437 | pub fn init_at<E>(&mut self, i: usize, init: impl Init<T, E>) -> Result
|
| 438 | where
|
| 439 | Error: From<E>,
|
| 440 | {
|
| 441 | if i >= self.0.len() {
|
| 442 | return Err(EINVAL);
|
| 443 | }
|
| 444 |
|
| 445 | let ptr = &raw mut self[i];
|
| 446 |
|
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | unsafe { init.__init(ptr)? };
|
| 453 |
|
| 454 | Ok(())
|
| 455 | }
|
| 456 |
|
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | pub fn from_slice_with_attrs(
|
| 482 | dev: &device::Device<Bound>,
|
| 483 | data: &[T],
|
| 484 | gfp_flags: kernel::alloc::Flags,
|
| 485 | dma_attrs: Attrs,
|
| 486 | ) -> Result<Self>
|
| 487 | where
|
| 488 | T: Copy,
|
| 489 | {
|
| 490 | let mut slice = Self(Coherent::<T>::alloc_slice_with_attrs(
|
| 491 | dev,
|
| 492 | data.len(),
|
| 493 | gfp_flags,
|
| 494 | dma_attrs,
|
| 495 | )?);
|
| 496 |
|
| 497 | |
| 498 | slice.copy_from_slice(data);
|
| 499 |
|
| 500 | Ok(slice)
|
| 501 | }
|
| 502 |
|
| 503 | |
| 504 | |
| 505 | #[inline]
|
| 506 | pub fn from_slice(
|
| 507 | dev: &device::Device<Bound>,
|
| 508 | data: &[T],
|
| 509 | gfp_flags: kernel::alloc::Flags,
|
| 510 | ) -> Result<Self>
|
| 511 | where
|
| 512 | T: Copy,
|
| 513 | {
|
| 514 | Self::from_slice_with_attrs(dev, data, gfp_flags, Attrs(0))
|
| 515 | }
|
| 516 | }
|
| 517 |
|
| 518 | impl<T: AsBytes + FromBytes> CoherentBox<T> {
|
| 519 | |
| 520 | #[inline]
|
| 521 | pub fn zeroed_with_attrs(
|
| 522 | dev: &device::Device<Bound>,
|
| 523 | gfp_flags: kernel::alloc::Flags,
|
| 524 | dma_attrs: Attrs,
|
| 525 | ) -> Result<Self> {
|
| 526 | Coherent::zeroed_with_attrs(dev, gfp_flags, dma_attrs).map(Self)
|
| 527 | }
|
| 528 |
|
| 529 | |
| 530 | #[inline]
|
| 531 | pub fn zeroed(dev: &device::Device<Bound>, gfp_flags: kernel::alloc::Flags) -> Result<Self> {
|
| 532 | Self::zeroed_with_attrs(dev, gfp_flags, Attrs(0))
|
| 533 | }
|
| 534 | }
|
| 535 |
|
| 536 | impl<T: KnownSize + ?Sized> Deref for CoherentBox<T> {
|
| 537 | type Target = T;
|
| 538 |
|
| 539 | #[inline]
|
| 540 | fn deref(&self) -> &Self::Target {
|
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | unsafe { self.0.as_ref() }
|
| 546 | }
|
| 547 | }
|
| 548 |
|
| 549 | impl<T: AsBytes + FromBytes + KnownSize + ?Sized> DerefMut for CoherentBox<T> {
|
| 550 | #[inline]
|
| 551 | fn deref_mut(&mut self) -> &mut Self::Target {
|
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | unsafe { self.0.as_mut() }
|
| 557 | }
|
| 558 | }
|
| 559 |
|
| 560 | impl<T: AsBytes + FromBytes + KnownSize + ?Sized> From<CoherentBox<T>> for Coherent<T> {
|
| 561 | #[inline]
|
| 562 | fn from(value: CoherentBox<T>) -> Self {
|
| 563 | value.0
|
| 564 | }
|
| 565 | }
|
| 566 |
|
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | pub struct Coherent<T: KnownSize + ?Sized> {
|
| 596 | dev: ARef<device::Device>,
|
| 597 | dma_handle: DmaAddress,
|
| 598 | cpu_addr: NonNull<T>,
|
| 599 | dma_attrs: Attrs,
|
| 600 | }
|
| 601 |
|
| 602 | impl<T: KnownSize + ?Sized> Coherent<T> {
|
| 603 | |
| 604 | #[inline]
|
| 605 | pub fn size(&self) -> usize {
|
| 606 | T::size(self.cpu_addr.as_ptr())
|
| 607 | }
|
| 608 |
|
| 609 | |
| 610 | #[inline]
|
| 611 | pub fn as_ptr(&self) -> *const T {
|
| 612 | self.cpu_addr.as_ptr()
|
| 613 | }
|
| 614 |
|
| 615 | |
| 616 | |
| 617 | #[inline]
|
| 618 | pub fn as_mut_ptr(&self) -> *mut T {
|
| 619 | self.cpu_addr.as_ptr()
|
| 620 | }
|
| 621 |
|
| 622 | |
| 623 | |
| 624 | #[inline]
|
| 625 | pub fn dma_handle(&self) -> DmaAddress {
|
| 626 | self.dma_handle
|
| 627 | }
|
| 628 |
|
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | #[inline]
|
| 638 | pub unsafe fn as_ref(&self) -> &T {
|
| 639 | |
| 640 | unsafe { &*self.as_ptr() }
|
| 641 | }
|
| 642 |
|
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | #[expect(clippy::mut_from_ref, reason = "unsafe to use API")]
|
| 652 | #[inline]
|
| 653 | pub unsafe fn as_mut(&self) -> &mut T {
|
| 654 | |
| 655 | unsafe { &mut *self.as_mut_ptr() }
|
| 656 | }
|
| 657 |
|
| 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | #[doc(hidden)]
|
| 667 | pub unsafe fn field_read<F: FromBytes>(&self, field: *const F) -> F {
|
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | unsafe { field.read_volatile() }
|
| 679 | }
|
| 680 |
|
| 681 | |
| 682 | |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | #[doc(hidden)]
|
| 690 | pub unsafe fn field_write<F: AsBytes>(&self, field: *mut F, val: F) {
|
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | unsafe { field.write_volatile(val) }
|
| 702 | }
|
| 703 | }
|
| 704 |
|
| 705 | impl<T: AsBytes + FromBytes> Coherent<T> {
|
| 706 | |
| 707 | fn alloc_with_attrs(
|
| 708 | dev: &device::Device<Bound>,
|
| 709 | gfp_flags: kernel::alloc::Flags,
|
| 710 | dma_attrs: Attrs,
|
| 711 | ) -> Result<Self> {
|
| 712 | const {
|
| 713 | assert!(
|
| 714 | core::mem::size_of::<T>() > 0,
|
| 715 | "It doesn't make sense for the allocated type to be a ZST"
|
| 716 | );
|
| 717 | }
|
| 718 |
|
| 719 | let mut dma_handle = 0;
|
| 720 | |
| 721 | let addr = unsafe {
|
| 722 | bindings::dma_alloc_attrs(
|
| 723 | dev.as_raw(),
|
| 724 | core::mem::size_of::<T>(),
|
| 725 | &mut dma_handle,
|
| 726 | gfp_flags.as_raw(),
|
| 727 | dma_attrs.as_raw(),
|
| 728 | )
|
| 729 | };
|
| 730 | let cpu_addr = NonNull::new(addr.cast()).ok_or(ENOMEM)?;
|
| 731 | |
| 732 | |
| 733 | |
| 734 | |
| 735 | Ok(Self {
|
| 736 | dev: dev.into(),
|
| 737 | dma_handle,
|
| 738 | cpu_addr,
|
| 739 | dma_attrs,
|
| 740 | })
|
| 741 | }
|
| 742 |
|
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | |
| 749 | |
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | |
| 760 | |
| 761 | |
| 762 | #[inline]
|
| 763 | pub fn zeroed_with_attrs(
|
| 764 | dev: &device::Device<Bound>,
|
| 765 | gfp_flags: kernel::alloc::Flags,
|
| 766 | dma_attrs: Attrs,
|
| 767 | ) -> Result<Self> {
|
| 768 | Self::alloc_with_attrs(dev, gfp_flags | __GFP_ZERO, dma_attrs)
|
| 769 | }
|
| 770 |
|
| 771 | |
| 772 | |
| 773 | #[inline]
|
| 774 | pub fn zeroed(dev: &device::Device<Bound>, gfp_flags: kernel::alloc::Flags) -> Result<Self> {
|
| 775 | Self::zeroed_with_attrs(dev, gfp_flags, Attrs(0))
|
| 776 | }
|
| 777 |
|
| 778 | |
| 779 | |
| 780 | pub fn init_with_attrs<E>(
|
| 781 | dev: &device::Device<Bound>,
|
| 782 | gfp_flags: kernel::alloc::Flags,
|
| 783 | dma_attrs: Attrs,
|
| 784 | init: impl Init<T, E>,
|
| 785 | ) -> Result<Self>
|
| 786 | where
|
| 787 | Error: From<E>,
|
| 788 | {
|
| 789 | let dmem = Self::alloc_with_attrs(dev, gfp_flags, dma_attrs)?;
|
| 790 | let ptr = dmem.as_mut_ptr();
|
| 791 |
|
| 792 | |
| 793 | |
| 794 | |
| 795 | |
| 796 | |
| 797 | unsafe { init.__init(ptr)? };
|
| 798 |
|
| 799 | Ok(dmem)
|
| 800 | }
|
| 801 |
|
| 802 | |
| 803 | |
| 804 | #[inline]
|
| 805 | pub fn init<E>(
|
| 806 | dev: &device::Device<Bound>,
|
| 807 | gfp_flags: kernel::alloc::Flags,
|
| 808 | init: impl Init<T, E>,
|
| 809 | ) -> Result<Self>
|
| 810 | where
|
| 811 | Error: From<E>,
|
| 812 | {
|
| 813 | Self::init_with_attrs(dev, gfp_flags, Attrs(0), init)
|
| 814 | }
|
| 815 |
|
| 816 | |
| 817 | fn alloc_slice_with_attrs(
|
| 818 | dev: &device::Device<Bound>,
|
| 819 | len: usize,
|
| 820 | gfp_flags: kernel::alloc::Flags,
|
| 821 | dma_attrs: Attrs,
|
| 822 | ) -> Result<Coherent<[T]>> {
|
| 823 | const {
|
| 824 | assert!(
|
| 825 | core::mem::size_of::<T>() > 0,
|
| 826 | "It doesn't make sense for the allocated type to be a ZST"
|
| 827 | );
|
| 828 | }
|
| 829 |
|
| 830 | |
| 831 | if len == 0 {
|
| 832 | Err(EINVAL)?;
|
| 833 | }
|
| 834 |
|
| 835 | let size = core::mem::size_of::<T>().checked_mul(len).ok_or(ENOMEM)?;
|
| 836 | let mut dma_handle = 0;
|
| 837 | |
| 838 | let addr = unsafe {
|
| 839 | bindings::dma_alloc_attrs(
|
| 840 | dev.as_raw(),
|
| 841 | size,
|
| 842 | &mut dma_handle,
|
| 843 | gfp_flags.as_raw(),
|
| 844 | dma_attrs.as_raw(),
|
| 845 | )
|
| 846 | };
|
| 847 | let cpu_addr = NonNull::slice_from_raw_parts(NonNull::new(addr.cast()).ok_or(ENOMEM)?, len);
|
| 848 | |
| 849 | |
| 850 | |
| 851 | |
| 852 | Ok(Coherent {
|
| 853 | dev: dev.into(),
|
| 854 | dma_handle,
|
| 855 | cpu_addr,
|
| 856 | dma_attrs,
|
| 857 | })
|
| 858 | }
|
| 859 |
|
| 860 | |
| 861 | |
| 862 | |
| 863 | |
| 864 | |
| 865 | |
| 866 | |
| 867 | |
| 868 | |
| 869 | |
| 870 | |
| 871 | |
| 872 | |
| 873 | |
| 874 | |
| 875 | |
| 876 | |
| 877 | |
| 878 | |
| 879 | |
| 880 | |
| 881 | |
| 882 | #[inline]
|
| 883 | pub fn zeroed_slice_with_attrs(
|
| 884 | dev: &device::Device<Bound>,
|
| 885 | len: usize,
|
| 886 | gfp_flags: kernel::alloc::Flags,
|
| 887 | dma_attrs: Attrs,
|
| 888 | ) -> Result<Coherent<[T]>> {
|
| 889 | Coherent::alloc_slice_with_attrs(dev, len, gfp_flags | __GFP_ZERO, dma_attrs)
|
| 890 | }
|
| 891 |
|
| 892 | |
| 893 | |
| 894 | #[inline]
|
| 895 | pub fn zeroed_slice(
|
| 896 | dev: &device::Device<Bound>,
|
| 897 | len: usize,
|
| 898 | gfp_flags: kernel::alloc::Flags,
|
| 899 | ) -> Result<Coherent<[T]>> {
|
| 900 | Self::zeroed_slice_with_attrs(dev, len, gfp_flags, Attrs(0))
|
| 901 | }
|
| 902 |
|
| 903 | |
| 904 | |
| 905 | |
| 906 | |
| 907 | |
| 908 | |
| 909 | |
| 910 | |
| 911 | |
| 912 | |
| 913 | |
| 914 | |
| 915 | |
| 916 | |
| 917 | |
| 918 | |
| 919 | |
| 920 | |
| 921 | |
| 922 | |
| 923 | #[inline]
|
| 924 | pub fn from_slice_with_attrs(
|
| 925 | dev: &device::Device<Bound>,
|
| 926 | data: &[T],
|
| 927 | gfp_flags: kernel::alloc::Flags,
|
| 928 | dma_attrs: Attrs,
|
| 929 | ) -> Result<Coherent<[T]>>
|
| 930 | where
|
| 931 | T: Copy,
|
| 932 | {
|
| 933 | CoherentBox::from_slice_with_attrs(dev, data, gfp_flags, dma_attrs).map(Into::into)
|
| 934 | }
|
| 935 |
|
| 936 | |
| 937 | |
| 938 | #[inline]
|
| 939 | pub fn from_slice(
|
| 940 | dev: &device::Device<Bound>,
|
| 941 | data: &[T],
|
| 942 | gfp_flags: kernel::alloc::Flags,
|
| 943 | ) -> Result<Coherent<[T]>>
|
| 944 | where
|
| 945 | T: Copy,
|
| 946 | {
|
| 947 | Self::from_slice_with_attrs(dev, data, gfp_flags, Attrs(0))
|
| 948 | }
|
| 949 | }
|
| 950 |
|
| 951 | impl<T> Coherent<[T]> {
|
| 952 | |
| 953 | |
| 954 | |
| 955 | |
| 956 | #[inline]
|
| 957 | #[expect(clippy::len_without_is_empty, reason = "Coherent slice is never empty")]
|
| 958 | pub fn len(&self) -> usize {
|
| 959 | self.cpu_addr.len()
|
| 960 | }
|
| 961 | }
|
| 962 |
|
| 963 | |
| 964 | impl<T: KnownSize + ?Sized> Drop for Coherent<T> {
|
| 965 | fn drop(&mut self) {
|
| 966 | let size = T::size(self.cpu_addr.as_ptr());
|
| 967 | |
| 968 | |
| 969 | |
| 970 | unsafe {
|
| 971 | bindings::dma_free_attrs(
|
| 972 | self.dev.as_raw(),
|
| 973 | size,
|
| 974 | self.cpu_addr.as_ptr().cast(),
|
| 975 | self.dma_handle,
|
| 976 | self.dma_attrs.as_raw(),
|
| 977 | )
|
| 978 | }
|
| 979 | }
|
| 980 | }
|
| 981 |
|
| 982 | |
| 983 | |
| 984 | unsafe impl<T: KnownSize + Send + ?Sized> Send for Coherent<T> {}
|
| 985 |
|
| 986 | |
| 987 | |
| 988 | |
| 989 | |
| 990 | unsafe impl<T: KnownSize + ?Sized + AsBytes + FromBytes + Sync> Sync for Coherent<T> {}
|
| 991 |
|
| 992 | impl<T: KnownSize + AsBytes + ?Sized> debugfs::BinaryWriter for Coherent<T> {
|
| 993 | fn write_to_slice(
|
| 994 | &self,
|
| 995 | writer: &mut UserSliceWriter,
|
| 996 | offset: &mut file::Offset,
|
| 997 | ) -> Result<usize> {
|
| 998 | if offset.is_negative() {
|
| 999 | return Err(EINVAL);
|
| 1000 | }
|
| 1001 |
|
| 1002 | |
| 1003 | |
| 1004 | let Ok(offset_val) = usize::try_from(*offset) else {
|
| 1005 | return Ok(0);
|
| 1006 | };
|
| 1007 |
|
| 1008 | let count = self.size().saturating_sub(offset_val).min(writer.len());
|
| 1009 |
|
| 1010 | writer.write_dma(self, offset_val, count)?;
|
| 1011 |
|
| 1012 | *offset += count as i64;
|
| 1013 | Ok(count)
|
| 1014 | }
|
| 1015 | }
|
| 1016 |
|
| 1017 | |
| 1018 | |
| 1019 | |
| 1020 | |
| 1021 | |
| 1022 | |
| 1023 | |
| 1024 | |
| 1025 | |
| 1026 | |
| 1027 | |
| 1028 | |
| 1029 | |
| 1030 | |
| 1031 | |
| 1032 | |
| 1033 | |
| 1034 | pub struct CoherentHandle {
|
| 1035 | dev: ARef<device::Device>,
|
| 1036 | dma_handle: DmaAddress,
|
| 1037 | cpu_handle: NonNull<c_void>,
|
| 1038 | size: usize,
|
| 1039 | dma_attrs: Attrs,
|
| 1040 | }
|
| 1041 |
|
| 1042 | impl CoherentHandle {
|
| 1043 | |
| 1044 | |
| 1045 | |
| 1046 | |
| 1047 | |
| 1048 | |
| 1049 | pub fn alloc_with_attrs(
|
| 1050 | dev: &device::Device<Bound>,
|
| 1051 | size: usize,
|
| 1052 | gfp_flags: kernel::alloc::Flags,
|
| 1053 | dma_attrs: Attrs,
|
| 1054 | ) -> Result<Self> {
|
| 1055 | if size == 0 {
|
| 1056 | return Err(EINVAL);
|
| 1057 | }
|
| 1058 |
|
| 1059 | let dma_attrs = dma_attrs | Attrs(bindings::DMA_ATTR_NO_KERNEL_MAPPING);
|
| 1060 | let mut dma_handle = 0;
|
| 1061 | |
| 1062 | let cpu_handle = unsafe {
|
| 1063 | bindings::dma_alloc_attrs(
|
| 1064 | dev.as_raw(),
|
| 1065 | size,
|
| 1066 | &mut dma_handle,
|
| 1067 | gfp_flags.as_raw(),
|
| 1068 | dma_attrs.as_raw(),
|
| 1069 | )
|
| 1070 | };
|
| 1071 |
|
| 1072 | let cpu_handle = NonNull::new(cpu_handle).ok_or(ENOMEM)?;
|
| 1073 |
|
| 1074 | |
| 1075 | |
| 1076 | |
| 1077 | Ok(Self {
|
| 1078 | dev: dev.into(),
|
| 1079 | dma_handle,
|
| 1080 | cpu_handle,
|
| 1081 | size,
|
| 1082 | dma_attrs,
|
| 1083 | })
|
| 1084 | }
|
| 1085 |
|
| 1086 | |
| 1087 | #[inline]
|
| 1088 | pub fn alloc(
|
| 1089 | dev: &device::Device<Bound>,
|
| 1090 | size: usize,
|
| 1091 | gfp_flags: kernel::alloc::Flags,
|
| 1092 | ) -> Result<Self> {
|
| 1093 | Self::alloc_with_attrs(dev, size, gfp_flags, Attrs(0))
|
| 1094 | }
|
| 1095 |
|
| 1096 | |
| 1097 | |
| 1098 | |
| 1099 | #[inline]
|
| 1100 | pub fn dma_handle(&self) -> DmaAddress {
|
| 1101 | self.dma_handle
|
| 1102 | }
|
| 1103 |
|
| 1104 | |
| 1105 | #[inline]
|
| 1106 | pub fn size(&self) -> usize {
|
| 1107 | self.size
|
| 1108 | }
|
| 1109 | }
|
| 1110 |
|
| 1111 | impl Drop for CoherentHandle {
|
| 1112 | fn drop(&mut self) {
|
| 1113 | |
| 1114 | |
| 1115 | unsafe {
|
| 1116 | bindings::dma_free_attrs(
|
| 1117 | self.dev.as_raw(),
|
| 1118 | self.size,
|
| 1119 | self.cpu_handle.as_ptr(),
|
| 1120 | self.dma_handle,
|
| 1121 | self.dma_attrs.as_raw(),
|
| 1122 | )
|
| 1123 | }
|
| 1124 | }
|
| 1125 | }
|
| 1126 |
|
| 1127 | |
| 1128 | |
| 1129 | unsafe impl Send for CoherentHandle {}
|
| 1130 |
|
| 1131 | |
| 1132 | |
| 1133 | |
| 1134 | unsafe impl Sync for CoherentHandle {}
|
| 1135 |
|
| 1136 | |
| 1137 | |
| 1138 | |
| 1139 | |
| 1140 | |
| 1141 | |
| 1142 | |
| 1143 | |
| 1144 | |
| 1145 | |
| 1146 | |
| 1147 | |
| 1148 | |
| 1149 | |
| 1150 | |
| 1151 | |
| 1152 | |
| 1153 | |
| 1154 | |
| 1155 | |
| 1156 | |
| 1157 | |
| 1158 | |
| 1159 | #[macro_export]
|
| 1160 | macro_rules! dma_read {
|
| 1161 | ($dma:expr, $($proj:tt)*) => {{
|
| 1162 | let dma = &$dma;
|
| 1163 | let ptr = $crate::ptr::project!(
|
| 1164 | $crate::dma::Coherent::as_ptr(dma), $($proj)*
|
| 1165 | );
|
| 1166 | |
| 1167 | unsafe { $crate::dma::Coherent::field_read(dma, ptr) }
|
| 1168 | }};
|
| 1169 | }
|
| 1170 |
|
| 1171 | |
| 1172 | |
| 1173 | |
| 1174 | |
| 1175 | |
| 1176 | |
| 1177 | |
| 1178 | |
| 1179 | |
| 1180 | |
| 1181 | |
| 1182 | |
| 1183 | |
| 1184 | |
| 1185 | |
| 1186 | |
| 1187 | |
| 1188 | |
| 1189 | |
| 1190 | |
| 1191 | |
| 1192 | |
| 1193 | |
| 1194 | |
| 1195 | |
| 1196 | #[macro_export]
|
| 1197 | macro_rules! dma_write {
|
| 1198 | (@parse [$dma:expr] [$($proj:tt)*] [, $val:expr]) => {{
|
| 1199 | let dma = &$dma;
|
| 1200 | let ptr = $crate::ptr::project!(
|
| 1201 | mut $crate::dma::Coherent::as_mut_ptr(dma), $($proj)*
|
| 1202 | );
|
| 1203 | let val = $val;
|
| 1204 | |
| 1205 | unsafe { $crate::dma::Coherent::field_write(dma, ptr, val) }
|
| 1206 | }};
|
| 1207 | (@parse [$dma:expr] [$($proj:tt)*] [.$field:tt $($rest:tt)*]) => {
|
| 1208 | $crate::dma_write!(@parse [$dma] [$($proj)* .$field] [$($rest)*])
|
| 1209 | };
|
| 1210 | (@parse [$dma:expr] [$($proj:tt)*] [[$flavor:ident: $index:expr] $($rest:tt)*]) => {
|
| 1211 | $crate::dma_write!(@parse [$dma] [$($proj)* [$flavor: $index]] [$($rest)*])
|
| 1212 | };
|
| 1213 | ($dma:expr, $($rest:tt)*) => {
|
| 1214 | $crate::dma_write!(@parse [$dma] [] [$($rest)*])
|
| 1215 | };
|
| 1216 | }
|