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