Commit dd6ff5cf authored by Alice Ryhl's avatar Alice Ryhl Committed by Danilo Krummrich
Browse files

rust: io: add typedef for phys_addr_t



The C typedef phys_addr_t is missing an analogue in Rust, meaning that
we end up using bindings::phys_addr_t or ResourceSize as a replacement
in various places throughout the kernel. Fix that by introducing a new
typedef on the Rust side. Place it next to the existing ResourceSize
typedef since they're quite related to each other.

Cc: stable@vger.kernel.org # for v6.18 [1]
Signed-off-by: default avatarAlice Ryhl <aliceryhl@google.com>
Link: https://patch.msgid.link/20251112-resource-phys-typedefs-v2-4-538307384f82@google.com
Link: https://lore.kernel.org/all/20251112-resource-phys-typedefs-v2-0-538307384f82@google.com/

 [1]
Signed-off-by: default avatarDanilo Krummrich <dakr@kernel.org>
parent ee2776e5
Loading
Loading
Loading
Loading
+15 −3
Original line number Diff line number Diff line
@@ -52,8 +52,20 @@ struct Inner<T: Send> {
/// # Examples
///
/// ```no_run
/// # use kernel::{bindings, device::{Bound, Device}, devres::Devres, io::{Io, IoRaw}};
/// # use core::ops::Deref;
/// use kernel::{
///     bindings,
///     device::{
///         Bound,
///         Device,
///     },
///     devres::Devres,
///     io::{
///         Io,
///         IoRaw,
///         PhysAddr,
///     },
/// };
/// use core::ops::Deref;
///
/// // See also [`pci::Bar`] for a real example.
/// struct IoMem<const SIZE: usize>(IoRaw<SIZE>);
@@ -66,7 +78,7 @@ struct Inner<T: Send> {
///     unsafe fn new(paddr: usize) -> Result<Self>{
///         // SAFETY: By the safety requirements of this function [`paddr`, `paddr` + `SIZE`) is
///         // valid for `ioremap`.
///         let addr = unsafe { bindings::ioremap(paddr as bindings::phys_addr_t, SIZE) };
///         let addr = unsafe { bindings::ioremap(paddr as PhysAddr, SIZE) };
///         if addr.is_null() {
///             return Err(ENOMEM);
///         }
+17 −3
Original line number Diff line number Diff line
@@ -15,6 +15,12 @@

pub use resource::Resource;

/// Physical address type.
///
/// This is a type alias to either `u32` or `u64` depending on the config option
/// `CONFIG_PHYS_ADDR_T_64BIT`, and it can be a u64 even on 32-bit architectures.
pub type PhysAddr = bindings::phys_addr_t;

/// Resource Size type.
///
/// This is a type alias to either `u32` or `u64` depending on the config option
@@ -70,8 +76,16 @@ pub fn maxsize(&self) -> usize {
/// # Examples
///
/// ```no_run
/// # use kernel::{bindings, ffi::c_void, io::{Io, IoRaw}};
/// # use core::ops::Deref;
/// use kernel::{
///     bindings,
///     ffi::c_void,
///     io::{
///         Io,
///         IoRaw,
///         PhysAddr,
///     },
/// };
/// use core::ops::Deref;
///
/// // See also [`pci::Bar`] for a real example.
/// struct IoMem<const SIZE: usize>(IoRaw<SIZE>);
@@ -84,7 +98,7 @@ pub fn maxsize(&self) -> usize {
///     unsafe fn new(paddr: usize) -> Result<Self>{
///         // SAFETY: By the safety requirements of this function [`paddr`, `paddr` + `SIZE`) is
///         // valid for `ioremap`.
///         let addr = unsafe { bindings::ioremap(paddr as bindings::phys_addr_t, SIZE) };
///         let addr = unsafe { bindings::ioremap(paddr as PhysAddr, SIZE) };
///         if addr.is_null() {
///             return Err(ENOMEM);
///         }
+6 −3
Original line number Diff line number Diff line
@@ -16,7 +16,10 @@
    types::Opaque, //
};

pub use super::ResourceSize;
pub use super::{
    PhysAddr,
    ResourceSize, //
};

/// A region allocated from a parent [`Resource`].
///
@@ -97,7 +100,7 @@ impl Resource {
    /// the region, or a part of it, is already in use.
    pub fn request_region(
        &self,
        start: ResourceSize,
        start: PhysAddr,
        size: ResourceSize,
        name: CString,
        flags: Flags,
@@ -131,7 +134,7 @@ pub fn size(&self) -> ResourceSize {
    }

    /// Returns the start address of the resource.
    pub fn start(&self) -> ResourceSize {
    pub fn start(&self) -> PhysAddr {
        let inner = self.0.get();
        // SAFETY: Safe as per the invariants of `Resource`.
        unsafe { (*inner).start }