Lines Matching +full:valid +full:- +full:mask

1 // SPDX-License-Identifier: GPL-2.0
5 //! C header: [`include/linux/dma-mapping.h`](srctree/include/linux/dma-mapping.h)
23 /// Note that this may be `u64` even on 32-bit architectures.
41 unsafe fn dma_set_mask(&self, mask: DmaMask) -> Result {
43 // - By the type invariant of `device::Device`, `self.as_ref().as_raw()` is valid.
44 // - The safety requirement of this function guarantees that there are no concurrent calls
45 // to DMA allocation and mapping primitives using this mask.
46 to_result(unsafe { bindings::dma_set_mask(self.as_ref().as_raw(), mask.value()) })
58 unsafe fn dma_set_coherent_mask(&self, mask: DmaMask) -> Result {
60 // - By the type invariant of `device::Device`, `self.as_ref().as_raw()` is valid.
61 // - The safety requirement of this function guarantees that there are no concurrent calls
62 // to DMA allocation and mapping primitives using this mask.
63 to_result(unsafe { bindings::dma_set_coherent_mask(self.as_ref().as_raw(), mask.value()) })
77 unsafe fn dma_set_mask_and_coherent(&self, mask: DmaMask) -> Result {
79 // - By the type invariant of `device::Device`, `self.as_ref().as_raw()` is valid.
80 // - The safety requirement of this function guarantees that there are no concurrent calls
81 // to DMA allocation and mapping primitives using this mask.
83 bindings::dma_set_mask_and_coherent(self.as_ref().as_raw(), mask.value())
88 /// A DMA mask that holds a bitmask with the lowest `n` bits set.
121 pub const fn new<const N: u32>() -> Self {
122 let Ok(mask) = Self::try_new(N) else {
123 build_error!("Invalid DMA Mask.");
126 mask
153 pub const fn try_new(n: u32) -> Result<Self> {
156 1..=64 => u64::MAX >> (64 - n),
163 pub const fn value(&self) -> u64 {
180 /// # fn test(dev: &Device<Bound>) -> Result {
192 pub(crate) fn as_raw(self) -> crate::ffi::c_ulong {
197 pub fn contains(self, flags: Attrs) -> bool {
204 fn bitor(self, rhs: Self) -> Self::Output {
211 fn bitand(self, rhs: Self) -> Self::Output {
218 fn not(self) -> Self::Output {
244 /// Hints DMA-mapping subsystem that it's probably not worth the time to try
248 /// This tells the DMA-mapping subsystem to suppress allocation failure reports (similarly to
253 /// ideally inaccessible or at least read-only at lesser-privileged levels).
264 /// [`enum dma_data_direction`]: srctree/include/linux/dma-direction.h
294 /// Casts the bindgen-generated enum type to a `u32` at compile time.
296 /// This function will cause a compile-time error if the underlying value of the
298 const fn const_cast(val: bindings::dma_data_direction) -> u32 {
304 // Check if the value is outside the valid range for the target type `u32`.
307 // Trigger a compile-time error in a const context.
311 // CAST: This cast is valid because the check above guarantees that `wide_val`
319 fn from(direction: DataDirection) -> Self {
322 // with the enum variants of `DataDirection`, which is a valid assumption given our
323 // compile-time checks.
340 /// - For the lifetime of an instance of [`CoherentAllocation`], the `cpu_addr` is a valid pointer
343 /// - The size in bytes of the allocation is equal to `size_of::<T> * count`.
344 /// - `size_of::<T> * count` fits into a `usize`.
374 /// # fn test(dev: &Device<Bound>) -> Result {
384 ) -> Result<CoherentAllocation<T>> {
394 // SAFETY: Device pointer is guaranteed as valid by the type invariant on `Device`.
408 // - We just successfully allocated a coherent region which is accessible for
409 // `count` elements, hence the cpu address is valid. We also hold a refcounted reference
411 // - The allocated `size` is equal to `size_of::<T> * count`.
412 // - The allocated `size` fits into a `usize`.
428 ) -> Result<CoherentAllocation<T>> {
436 pub fn count(&self) -> usize {
441 pub fn size(&self) -> usize {
448 pub fn start_ptr(&self) -> *const T {
454 pub fn start_ptr_mut(&mut self) -> *mut T {
460 pub fn dma_handle(&self) -> DmaAddress {
468 pub fn dma_handle_with_offset(&self, offset: usize) -> Result<DmaAddress> {
480 fn validate_range(&self, offset: usize, count: usize) -> Result {
490 /// For ringbuffer type of r/w access or use-cases where the pointer to the live data is needed,
500 pub unsafe fn as_slice(&self, offset: usize, count: usize) -> Result<&[T]> {
503 // - The pointer is valid due to type invariant on `CoherentAllocation`,
506 // - `offset + count` can't overflow since it is smaller than `self.count` and we've checked
520 pub unsafe fn as_slice_mut(&mut self, offset: usize, count: usize) -> Result<&mut [T]> {
523 // - The pointer is valid due to type invariant on `CoherentAllocation`,
526 // - `offset + count` can't overflow since it is smaller than `self.count` and we've checked
544 /// # fn test(alloc: &mut kernel::dma::CoherentAllocation<u8>) -> Result {
552 pub unsafe fn write(&mut self, src: &[T], offset: usize) -> Result {
555 // - The pointer is valid due to type invariant on `CoherentAllocation`
557 // - `offset + count` can't overflow since it is smaller than `self.count` and we've checked
570 pub fn item_from_index(&self, offset: usize) -> Result<*mut T> {
575 // - The pointer is valid due to type invariant on `CoherentAllocation`
577 // - `offset` can't overflow since it is smaller than `self.count` and we've checked
591 pub unsafe fn field_read<F: FromBytes>(&self, field: *const F) -> F {
593 // - By the safety requirements field is valid.
594 // - Using read_volatile() here is not sound as per the usual rules, the usage here is
596 // race from a hardware or code outside kernel (e.g. user-space program), we need that
597 // read on a valid memory is not UB. Currently read_volatile() is used for this, and the
616 // - By the safety requirements field is valid.
617 // - Using write_volatile() here is not sound as per the usual rules, the usage here is
619 // race from a hardware or code outside kernel (e.g. user-space program), we need that
620 // write on a valid memory is not UB. Currently write_volatile() is used for this, and the
633 // SAFETY: Device pointer is guaranteed as valid by the type invariant on `Device`.
634 // The cpu address, and the dma handle are valid due to the type invariants on
667 /// # fn test(alloc: &kernel::dma::CoherentAllocation<MyStruct>) -> Result {
675 (|| -> ::core::result::Result<_, $crate::error::Error> {
677 // SAFETY: `item_from_index` ensures that `item` is always a valid pointer and can be
711 /// # fn test(alloc: &kernel::dma::CoherentAllocation<MyStruct>) -> Result {
725 (|| -> ::core::result::Result<_, $crate::error::Error> {
727 // SAFETY: `item_from_index` ensures that `item` is always a valid item.
733 (|| -> ::core::result::Result<_, $crate::error::Error> {
735 // SAFETY: `item_from_index` ensures that `item` is always a valid pointer and can be