]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
PCI/sysfs: Add static PCI resource attribute macros
authorKrzysztof Wilczyński <kwilczynski@kernel.org>
Fri, 8 May 2026 04:35:25 +0000 (04:35 +0000)
committerBjorn Helgaas <bhelgaas@google.com>
Fri, 8 May 2026 22:58:35 +0000 (17:58 -0500)
Add three macros for declaring static binary attributes for PCI resource
files:

  - pci_dev_resource_io_attr(), for I/O BAR resources (read/write)
  - pci_dev_resource_uc_attr(), for memory BAR resources (mmap uncached)
  - pci_dev_resource_wc_attr(), for write-combine resources (mmap WC)

Each macro only sets the callbacks its resource type needs.  The I/O macro
conditionally includes mmap support via __PCI_RESOURCE_IO_MMAP_ATTRS on
architectures where arch_can_pci_mmap_io() is true at compile time (such as
PowerPC, SPARC, and Xtensa).

Signed-off-by: Krzysztof Wilczyński <kwilczynski@kernel.org>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Tested-by: Shivaprasad G Bhat <sbhat@linux.ibm.com>
Link: https://patch.msgid.link/20260508043543.217179-7-kwilczynski@kernel.org
drivers/pci/pci-sysfs.c

index dac780597727d47c9895bc1b79db21a98da17c6b..a74eca96918bd7aae42aee805ae7cae482f731e4 100644 (file)
@@ -1197,6 +1197,47 @@ static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj,
        return pci_resource_io(filp, kobj, attr, buf, off, count, true);
 }
 
+/*
+ * generic_file_llseek() consults f_mapping->host to determine
+ * the file size. As iomem_inode knows nothing about the
+ * attribute, it's not going to work, so override it as well.
+ */
+#if arch_can_pci_mmap_io()
+# define __PCI_RESOURCE_IO_MMAP_ATTRS          \
+       .f_mapping = iomem_get_mapping,         \
+       .llseek = pci_llseek_resource,          \
+       .mmap = pci_mmap_resource_uc,
+#else
+# define __PCI_RESOURCE_IO_MMAP_ATTRS
+#endif
+
+#define pci_dev_resource_io_attr(_bar)                                 \
+static const struct bin_attribute dev_resource##_bar##_io_attr = {     \
+       .attr = { .name = "resource" __stringify(_bar), .mode = 0600 }, \
+       .private = (void *)(unsigned long)(_bar),                       \
+       .read = pci_read_resource_io,                                   \
+       .write = pci_write_resource_io,                                 \
+       __PCI_RESOURCE_IO_MMAP_ATTRS                                    \
+}
+
+#define pci_dev_resource_uc_attr(_bar)                                 \
+static const struct bin_attribute dev_resource##_bar##_uc_attr = {     \
+       .attr = { .name = "resource" __stringify(_bar), .mode = 0600 }, \
+       .private = (void *)(unsigned long)(_bar),                       \
+       .f_mapping = iomem_get_mapping,                                 \
+       .llseek = pci_llseek_resource,                                  \
+       .mmap = pci_mmap_resource_uc,                                   \
+}
+
+#define pci_dev_resource_wc_attr(_bar)                                       \
+static const struct bin_attribute dev_resource##_bar##_wc_attr = {           \
+       .attr = { .name = "resource" __stringify(_bar) "_wc", .mode = 0600 }, \
+       .private = (void *)(unsigned long)(_bar),                             \
+       .f_mapping = iomem_get_mapping,                                       \
+       .llseek = pci_llseek_resource,                                        \
+       .mmap = pci_mmap_resource_wc,                                         \
+}
+
 /**
  * pci_remove_resource_files - cleanup resource files
  * @pdev: dev to cleanup