Skip to contentSkip to conversation

rust: pci: add resource_flags accessor

PATCH Applied Eliot Courtney 4 participants 6 messages Sep 8 – Oct 26

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: pci: add resource_flags accessor

From: Joel Fernandes <[email protected]>

Add a Device::resource_flags() method to the PCI Rust abstraction, wrapping the C-side static inline pci_resource_flags().

The flags returned correspond to the IORESOURCE bitmask carried by a PCI BAR's struct resource.

The immediate motivation is BAR layout discovery on NVIDIA GPUs: a 64-bit BAR consumes two consecutive Linux PCI resource slots (the lower 32 bits at index N and the upper 32 bits at index N+1, with the latter having no flags or size of its own).

---
This is patch 1 of Joel's nova-core mm prerequisites series [1], reposted with no code changes. The rest of that series was folded into the PRAMIN series [2], which is now in drm-rust-next and didn't carry this patch. The upcoming nova-core memory management changes still need resource_flags().

This is based on drm-rust-next.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

---
 rust/helpers/pci.c         |  6 ++++++
 rust/kernel/io/resource.rs |  8 ++++++++
 rust/kernel/pci.rs         | 14 ++++++++++++++
 3 files changed, 28 insertions(+)
rust/helpers/pci.c +6 -0
index a714cc2bfb7a..3686e405160d 100644
@@ -19,6 +19,12 @@ __rust_helper resource_size_t rust_helper_pci_resource_len(struct pci_dev *pdev,
 	return pci_resource_len(pdev, bar);
 }
 
+__rust_helper unsigned long rust_helper_pci_resource_flags(const struct pci_dev *pdev,
+							   int bar)
+{
+	return pci_resource_flags(pdev, bar);
+}
+
 __rust_helper bool rust_helper_dev_is_pci(const struct device *dev)
 {
 	return dev_is_pci(dev);
rust/kernel/io/resource.rs +8 -0
index 17b0c174cfc5..0d3b34f83334 100644
@@ -226,10 +226,18 @@ impl Flags {
     /// Resource represents a memory region that must be ioremaped using `ioremap_np`.
     pub const IORESOURCE_MEM_NONPOSTED: Flags = Flags::new(bindings::IORESOURCE_MEM_NONPOSTED);
 
+    /// Memory region uses a 64-bit address (consumes two consecutive PCI resource slots).
+    pub const IORESOURCE_MEM_64: Flags = Flags::new(bindings::IORESOURCE_MEM_64);
+
     // Always inline to optimize out error path of `build_assert`.
     #[inline(always)]
     const fn new(value: u32) -> Self {
         build_assert!(value as u64 <= c_ulong::MAX as u64);
         Flags(value as c_ulong)
     }
+
+    /// Wrap a raw `c_ulong` value returned by a C API into [`Flags`].
+    pub(crate) const fn from_raw(value: c_ulong) -> Self {
+        Flags(value)
+    }
 }
rust/kernel/pci.rs +14 -0
index 3ec897709e89..19a219847c17 100644
@@ -17,6 +17,7 @@
         from_result,
         to_result, //
     },
+    io::resource,
     prelude::*,
     str::CStr,
     types::Opaque,
@@ -439,6 +440,19 @@ pub fn resource_len(&self, bar: u32) -> Result<bindings::resource_size_t> {
         Ok(unsafe { bindings::pci_resource_len(self.as_raw(), bar.try_into()?) })
     }
 
+    /// Returns the resource flags (`IORESOURCE_*`) of the given PCI BAR.
+    pub fn resource_flags(&self, bar: u32) -> Result<resource::Flags> {
+        if !Bar::index_is_valid(bar) {
+            return Err(EINVAL);
+        }
+
+        // SAFETY:
+        // - `bar` is a valid bar number, as guaranteed by the above call to `Bar::index_is_valid`,
+        // - by its type invariant `self.as_raw` is always a valid pointer to a `struct pci_dev`.
+        let raw = unsafe { bindings::pci_resource_flags(self.as_raw(), bar.try_into()?) };
+        Ok(resource::Flags::from_raw(raw))
+    }
+
     /// Returns the PCI class as a `Class` struct.
     #[inline]
     pub fn pci_class(&self) -> Class {

---
base-commit: e6a2c988ed96a5a3af51ed97ecba980521bf2fc0
change-id: 20260907-pci-resource-flags-b7addba0b34f

Best regards,

Signature · 1 line
Eliot Courtney <[email protected]>
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 15 lines · Eliot Courtney

From: Joel Fernandes <[email protected]>

Add a Device::resource_flags() method to the PCI Rust abstraction, wrapping the C-side static inline pci_resource_flags().

The flags returned correspond to the IORESOURCE bitmask carried by a PCI BAR's struct resource.

The immediate motivation is BAR layout discovery on NVIDIA GPUs: a 64-bit BAR consumes two consecutive Linux PCI resource slots (the lower 32 bits at index N and the upper 32 bits at index N+1, with the latter having no flags or size of its own).

Quoted 8 lines

---
This is patch 1 of Joel's nova-core mm prerequisites series [1], reposted with no code changes. The rest of that series was folded into the PRAMIN series [2], which is now in drm-rust-next and didn't carry this patch. The upcoming nova-core memory management changes still need resource_flags().

This is based on drm-rust-next.

maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Eliot Courtney
Quoted 8 lines · Eliot Courtney

From: Joel Fernandes <[email protected]>

Add a Device::resource_flags() method to the PCI Rust abstraction, wrapping the C-side static inline pci_resource_flags().

The flags returned correspond to the IORESOURCE bitmask carried by a PCI BAR's struct resource.

Quoted 7 lines

The immediate motivation is BAR layout discovery on NVIDIA GPUs: a 64-bit BAR consumes two consecutive Linux PCI resource slots (the lower 32 bits at index N and the upper 32 bits at index N+1, with the latter having no flags or size of its own).

author Applied
#3 lore raw
In reply to Eliot Courtney
Quoted 22 lines · Eliot Courtney

From: Joel Fernandes <[email protected]>

Add a Device::resource_flags() method to the PCI Rust abstraction, wrapping the C-side static inline pci_resource_flags().

The flags returned correspond to the IORESOURCE bitmask carried by a PCI BAR's struct resource.

The immediate motivation is BAR layout discovery on NVIDIA GPUs: a 64-bit BAR consumes two consecutive Linux PCI resource slots (the lower 32 bits at index N and the upper 32 bits at index N+1, with the latter having no flags or size of its own).

---
This is patch 1 of Joel's nova-core mm prerequisites series [1], reposted with no code changes. The rest of that series was folded into the PRAMIN series [2], which is now in drm-rust-next and didn't carry this patch. The upcoming nova-core memory management changes still need resource_flags().

Quoted 5 lines

This is based on drm-rust-next.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
---

Applied to drm-rust-next, thanks!

[1/1] rust: pci: add resource_flags accessor

      commit: 3f1e12fce16f6e88fbaab3404df33f65c8f4df87
maintainer
#4 lore raw
In reply to Eliot Courtney

rust/kernel/io/resource.rs impl Flags { · line 242

Quoted 5 lines · On Tue, 08 Sep 2026 16:51:48 +0900 Eliot Courtney <[email protected]> wrote:
rust/kernel/io/resource.rs +4 -0
+    /// Wrap a raw `c_ulong` value returned by a C API into [`Flags`].
+    pub(crate) const fn from_raw(value: c_ulong) -> Self {
+        Flags(value)
+    }

In linux-next (next-20261022), on UML x86_64, I am seeing:

    error: associated function `from_raw` is never used
       --> rust/kernel/io/resource.rs:240:25
        |
    216 | impl Flags {
        | ---------- associated function in this implementation
    ...
    240 |     pub(crate) const fn from_raw(value: c_ulong) -> Self {
        |                         ^^^^^^^^
        |
        = note: `-D dead-code` implied by `-D warnings`
        = help: to override `-D warnings` add `#[allow(dead_code)]`

kernel::io::resource is always built, but the only user of from_raw is under CONFIG_PCI.

I hope that helps.

Cheers,
Miguel

#5 lore raw
Quoted 21 lines · Miguel Ojeda

rust/kernel/io/resource.rs impl Flags { · line 242

Quoted 5 lines · On Tue, 08 Sep 2026 16:51:48 +0900 Eliot Courtney <[email protected]> wrote:
rust/kernel/io/resource.rs +4 -0
+    /// Wrap a raw `c_ulong` value returned by a C API into [`Flags`].
+    pub(crate) const fn from_raw(value: c_ulong) -> Self {
+        Flags(value)
+    }

In linux-next (next-20261022), on UML x86_64, I am seeing:

    error: associated function `from_raw` is never used
       --> rust/kernel/io/resource.rs:240:25
        |
    216 | impl Flags {
        | ---------- associated function in this implementation
    ...
    240 |     pub(crate) const fn from_raw(value: c_ulong) -> Self {
        |                         ^^^^^^^^
        |
        = note: `-D dead-code` implied by `-D warnings`
        = help: to override `-D warnings` add `#[allow(dead_code)]`
Quoted 7 lines

kernel::io::resource is always built, but the only user of from_raw is under CONFIG_PCI.

I hope that helps.

Cheers,
Miguel

Thanks Miguel! I've sent a fix [1].

[1]: https://lore.kernel.org/all/[email protected]/