History

PCI/sysfs: Convert legacy I/O and memory attributes to static definitions

Currently, legacy_io and legacy_mem are dynamically allocated and created by pci_create_legacy_files(), with pci_adjust_legacy_attr() updating the attributes at runtime on Alpha to rename them and shift the size for sparse addressing. Convert to four static const attributes (legacy_io, legacy_io_sparse, legacy_mem, legacy_mem_sparse) with .is_bin_visible() callbacks that use pci_legacy_has_sparse() to select the appropriate variant per bus. The sizes are compile-time constants and .size is set directly on each attribute. Register the groups in pcibus_groups[] under a HAVE_PCI_LEGACY guard so the driver model handles creation and removal automatically. Stub out pci_create_legacy_files() and pci_remove_legacy_files() as the dynamic creation is no longer needed. Remove the __weak pci_adjust_legacy_attr(), Alpha's override, and its declaration from both Alpha and PowerPC asm/pci.h headers.

Krzysztof Wilczyński committed 6 months ago

Commit 4e14b961 parent 574b5474 files · +139 −132Browse files

Unified · Split

4 files
  1. arch/alpha/include/asm/pci.h +0 −2
  2. arch/alpha/kernel/pci-sysfs.c +7 −31
  3. arch/powerpc/include/asm/pci.h +0 −2
  4. drivers/pci/pci-sysfs.c +132 −97
arch/alpha/include/asm/pci.h+0−2
@@ -84,8 +84,6 @@ extern int pci_legacy_write(struct pci_bus *bus, loff_t port, u32 val, · 83 unmodified lines
8484 extern int pci_mmap_legacy_page_range(struct pci_bus *bus,
8585 struct vm_area_struct *vma,
8686 enum pci_mmap_state mmap_state);
87−extern void pci_adjust_legacy_attr(struct pci_bus *bus,
88− enum pci_mmap_state mmap_type);
8987 extern bool pci_legacy_has_sparse(struct pci_bus *bus,
9088 enum pci_mmap_state type);
9189 #define HAVE_PCI_LEGACY 1
arch/alpha/kernel/pci-sysfs.c+7−31
@@ -191,30 +191,6 @@ bool pci_legacy_has_sparse(struct pci_bus *bus, enum pci_mmap_state type) · 190 unmodified lines
191191 return has_sparse(hose, type);
192192 }
193193
194−/**
195− * pci_adjust_legacy_attr - adjustment of legacy file attributes
196− * @bus: bus to create files under
197− * @mmap_type: I/O port or memory
198− *
199− * Adjust file name and size for sparse mappings.
200− */
201−void pci_adjust_legacy_attr(struct pci_bus *bus, enum pci_mmap_state mmap_type)
202−{
203− struct pci_controller *hose = bus->sysdata;
204−
205− if (!has_sparse(hose, mmap_type))
206− return;
207−
208− if (mmap_type == pci_mmap_mem) {
209− bus->legacy_mem->attr.name = "legacy_mem_sparse";
210− bus->legacy_mem->size <<= 5;
211− } else {
212− bus->legacy_io->attr.name = "legacy_io_sparse";
213− bus->legacy_io->size <<= 5;
214− }
215− return;
216−}
217−
218194 /* Legacy I/O bus read/write functions */
219195 int pci_legacy_read(struct pci_bus *bus, loff_t port, u32 *val, size_t size)
220196 {
@@ -291,9 +267,9 @@ static inline enum pci_mmap_state pci_bar_mmap_type(struct pci_dev *pdev, · 70 unmodified lines
291267 return pci_resource_is_mem(pdev, bar) ? pci_mmap_mem : pci_mmap_io;
292268 }
293269
294−static inline umode_t __pci_dev_resource_is_visible(struct kobject *kobj,
295− const struct bin_attribute *a,
296− int bar)
270+static inline umode_t __pci_resource_attr_is_visible(struct kobject *kobj,
271+ const struct bin_attribute *a,
272+ int bar)
297273 {
298274 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
299275
@@ -313,7 +289,7 @@ static umode_t pci_dev_resource_is_visible(struct kobject *kobj, · 13 unmodified lines
313289 if (has_sparse(hose, pci_bar_mmap_type(pdev, bar)))
314290 return 0;
315291
316− return __pci_dev_resource_is_visible(kobj, a, bar);
292+ return __pci_resource_attr_is_visible(kobj, a, bar);
317293 }
318294
319295 static umode_t pci_dev_resource_sparse_is_visible(struct kobject *kobj,
@@ -330,7 +306,7 @@ static umode_t pci_dev_resource_sparse_is_visible(struct kobject *kobj, · 10 unmodified lines
330306 if (type == pci_mmap_mem && !sparse_mem_mmap_fits(pdev, bar))
331307 return 0;
332308
333− return __pci_dev_resource_is_visible(kobj, a, bar);
309+ return __pci_resource_attr_is_visible(kobj, a, bar);
334310 }
335311
336312 static umode_t pci_dev_resource_dense_is_visible(struct kobject *kobj,
@@ -346,14 +322,14 @@ static umode_t pci_dev_resource_dense_is_visible(struct kobject *kobj, · 9 unmodified lines
346322 return 0;
347323
348324 if (type == pci_mmap_mem && !sparse_mem_mmap_fits(pdev, bar))
349− return __pci_dev_resource_is_visible(kobj, a, bar);
325+ return __pci_resource_attr_is_visible(kobj, a, bar);
350326
351327 dense_base = (type == pci_mmap_mem) ? hose->dense_mem_base :
352328 hose->dense_io_base;
353329 if (!dense_base)
354330 return 0;
355331
356− return __pci_dev_resource_is_visible(kobj, a, bar);
332+ return __pci_resource_attr_is_visible(kobj, a, bar);
357333 }
358334
359335 static inline size_t __pci_dev_resource_bin_size(struct kobject *kobj,
arch/powerpc/include/asm/pci.h+0−2
@@ -82,8 +82,6 @@ extern int pci_legacy_write(struct pci_bus *bus, loff_t port, u32 val, · 81 unmodified lines
8282 extern int pci_mmap_legacy_page_range(struct pci_bus *bus,
8383 struct vm_area_struct *vma,
8484 enum pci_mmap_state mmap_state);
85−extern void pci_adjust_legacy_attr(struct pci_bus *bus,
86− enum pci_mmap_state mmap_type);
8785 #define HAVE_PCI_LEGACY 1
8886
8987 extern void pcibios_claim_one_bus(struct pci_bus *b);
drivers/pci/pci-sysfs.c+132−97
@@ -676,11 +676,6 @@ static const struct attribute_group pcibus_group = { · 675 unmodified lines
676676 .attrs = pcibus_attrs,
677677 };
678678
679−const struct attribute_group *pcibus_groups[] = {
680− &pcibus_group,
681− NULL,
682−};
683−
684679 static ssize_t boot_vga_show(struct device *dev, struct device_attribute *attr,
685680 char *buf)
686681 {
@@ -879,19 +874,6 @@ static const struct attribute_group pci_dev_config_attr_group = { · 192 unmodified lines
879874 .bin_size = pci_dev_config_attr_bin_size,
880875 };
881876
882−/*
883− * llseek operation for mmappable PCI resources.
884− * May be left unused if the arch doesn't provide them.
885− */
886−static __maybe_unused loff_t
887−pci_llseek_resource_legacy(struct file *filep,
888− struct kobject *kobj __always_unused,
889− const struct bin_attribute *attr,
890− loff_t offset, int whence)
891−{
892− return fixed_size_llseek(filep, offset, whence, attr->size);
893−}
894−
895877 #ifdef HAVE_PCI_LEGACY
896878 /**
897879 * pci_read_legacy_io - read byte(s) from legacy I/O port space
@@ -989,91 +971,144 @@ bool __weak pci_legacy_has_sparse(struct pci_bus *bus, · 91 unmodified lines
989971 return false;
990972 }
991973
992−/**
993− * pci_adjust_legacy_attr - adjustment of legacy file attributes
994− * @b: bus to create files under
995− * @mmap_type: I/O port or memory
996− *
997− * Stub implementation. Can be overridden by arch if necessary.
998− */
999−void __weak pci_adjust_legacy_attr(struct pci_bus *b,
1000− enum pci_mmap_state mmap_type)
974+static inline umode_t __pci_legacy_is_visible(struct kobject *kobj,
975+ const struct bin_attribute *a,
976+ enum pci_mmap_state type,
977+ bool sparse)
978+{
979+ struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
980+
981+ if (pci_legacy_has_sparse(bus, type) != sparse)
982+ return 0;
983+
984+ return a->attr.mode;
985+}
986+
987+static umode_t pci_legacy_io_is_visible(struct kobject *kobj,
988+ const struct bin_attribute *a, int n)
1001989 {
990+ return __pci_legacy_is_visible(kobj, a, pci_mmap_io, false);
1002991 }
1003992
1004−/**
1005− * pci_create_legacy_files - create legacy I/O port and memory files
1006− * @b: bus to create files under
1007− *
1008− * Some platforms allow access to legacy I/O port and ISA memory space on
1009− * a per-bus basis. This routine creates the files and ties them into
1010− * their associated read, write and mmap files from pci-sysfs.c
1011− *
1012− * On error unwind, but don't propagate the error to the caller
1013− * as it is ok to set up the PCI bus without these files.
1014− */
1015−void pci_create_legacy_files(struct pci_bus *b)
1016−{
1017− int error;
1018−
1019− if (!sysfs_initialized)
1020− return;
1021−
1022− b->legacy_io = kzalloc_objs(struct bin_attribute, 2, GFP_ATOMIC);
1023− if (!b->legacy_io)
1024− goto kzalloc_err;
1025−
1026− sysfs_bin_attr_init(b->legacy_io);
1027− b->legacy_io->attr.name = "legacy_io";
1028− b->legacy_io->size = PCI_LEGACY_IO_SIZE;
1029− b->legacy_io->attr.mode = 0600;
1030− b->legacy_io->read = pci_read_legacy_io;
1031− b->legacy_io->write = pci_write_legacy_io;
1032− /* See pci_create_attr() for motivation */
1033− b->legacy_io->llseek = pci_llseek_resource_legacy;
1034− b->legacy_io->mmap = pci_mmap_legacy_io;
1035− b->legacy_io->f_mapping = iomem_get_mapping;
1036− pci_adjust_legacy_attr(b, pci_mmap_io);
1037− error = device_create_bin_file(&b->dev, b->legacy_io);
1038− if (error)
1039− goto legacy_io_err;
1040−
1041− /* Allocated above after the legacy_io struct */
1042− b->legacy_mem = b->legacy_io + 1;
1043− sysfs_bin_attr_init(b->legacy_mem);
1044− b->legacy_mem->attr.name = "legacy_mem";
1045− b->legacy_mem->size = PCI_LEGACY_MEM_SIZE;
1046− b->legacy_mem->attr.mode = 0600;
1047− b->legacy_mem->mmap = pci_mmap_legacy_mem;
1048− /* See pci_create_attr() for motivation */
1049− b->legacy_mem->llseek = pci_llseek_resource_legacy;
1050− b->legacy_mem->f_mapping = iomem_get_mapping;
1051− pci_adjust_legacy_attr(b, pci_mmap_mem);
1052− error = device_create_bin_file(&b->dev, b->legacy_mem);
1053− if (error)
1054− goto legacy_mem_err;
1055−
1056− return;
1057−
1058−legacy_mem_err:
1059− device_remove_bin_file(&b->dev, b->legacy_io);
1060−legacy_io_err:
1061− kfree(b->legacy_io);
1062− b->legacy_io = NULL;
1063−kzalloc_err:
1064− dev_warn(&b->dev, "could not create legacy I/O port and ISA memory resources in sysfs\n");
1065−}
1066−
1067−void pci_remove_legacy_files(struct pci_bus *b)
1068−{
1069− if (b->legacy_io) {
1070− device_remove_bin_file(&b->dev, b->legacy_io);
1071− device_remove_bin_file(&b->dev, b->legacy_mem);
1072− kfree(b->legacy_io); /* both are allocated here */
1073− }
993+static umode_t pci_legacy_io_sparse_is_visible(struct kobject *kobj,
994+ const struct bin_attribute *a,
995+ int n)
996+{
997+ return __pci_legacy_is_visible(kobj, a, pci_mmap_io, true);
998+}
999+
1000+static umode_t pci_legacy_mem_is_visible(struct kobject *kobj,
1001+ const struct bin_attribute *a, int n)
1002+{
1003+ return __pci_legacy_is_visible(kobj, a, pci_mmap_mem, false);
1004+}
1005+
1006+static umode_t pci_legacy_mem_sparse_is_visible(struct kobject *kobj,
1007+ const struct bin_attribute *a,
1008+ int n)
1009+{
1010+ return __pci_legacy_is_visible(kobj, a, pci_mmap_mem, true);
1011+}
1012+
1013+static loff_t pci_llseek_resource_legacy(struct file *filep,
1014+ struct kobject *kobj __always_unused,
1015+ const struct bin_attribute *attr,
1016+ loff_t offset, int whence)
1017+{
1018+ return fixed_size_llseek(filep, offset, whence, attr->size);
10741019 }
1020+
1021+static const struct bin_attribute pci_legacy_io_attr = {
1022+ .attr = { .name = "legacy_io", .mode = 0600 },
1023+ .size = PCI_LEGACY_IO_SIZE,
1024+ .read = pci_read_legacy_io,
1025+ .write = pci_write_legacy_io,
1026+ .mmap = pci_mmap_legacy_io,
1027+ .llseek = pci_llseek_resource_legacy,
1028+ .f_mapping = iomem_get_mapping,
1029+};
1030+
1031+static const struct bin_attribute pci_legacy_io_sparse_attr = {
1032+ .attr = { .name = "legacy_io_sparse", .mode = 0600 },
1033+ .size = PCI_LEGACY_IO_SIZE << 5,
1034+ .read = pci_read_legacy_io,
1035+ .write = pci_write_legacy_io,
1036+ .mmap = pci_mmap_legacy_io,
1037+ .llseek = pci_llseek_resource_legacy,
1038+ .f_mapping = iomem_get_mapping,
1039+};
1040+
1041+static const struct bin_attribute pci_legacy_mem_attr = {
1042+ .attr = { .name = "legacy_mem", .mode = 0600 },
1043+ .size = PCI_LEGACY_MEM_SIZE,
1044+ .mmap = pci_mmap_legacy_mem,
1045+ .llseek = pci_llseek_resource_legacy,
1046+ .f_mapping = iomem_get_mapping,
1047+};
1048+
1049+static const struct bin_attribute pci_legacy_mem_sparse_attr = {
1050+ .attr = { .name = "legacy_mem_sparse", .mode = 0600 },
1051+ .size = PCI_LEGACY_MEM_SIZE << 5,
1052+ .mmap = pci_mmap_legacy_mem,
1053+ .llseek = pci_llseek_resource_legacy,
1054+ .f_mapping = iomem_get_mapping,
1055+};
1056+
1057+static const struct bin_attribute *const pci_legacy_io_attrs[] = {
1058+ &pci_legacy_io_attr,
1059+ NULL,
1060+};
1061+
1062+static const struct bin_attribute *const pci_legacy_io_sparse_attrs[] = {
1063+ &pci_legacy_io_sparse_attr,
1064+ NULL,
1065+};
1066+
1067+static const struct bin_attribute *const pci_legacy_mem_attrs[] = {
1068+ &pci_legacy_mem_attr,
1069+ NULL,
1070+};
1071+
1072+static const struct bin_attribute *const pci_legacy_mem_sparse_attrs[] = {
1073+ &pci_legacy_mem_sparse_attr,
1074+ NULL,
1075+};
1076+
1077+static const struct attribute_group pci_legacy_io_group = {
1078+ .bin_attrs = pci_legacy_io_attrs,
1079+ .is_bin_visible = pci_legacy_io_is_visible,
1080+};
1081+
1082+static const struct attribute_group pci_legacy_io_sparse_group = {
1083+ .bin_attrs = pci_legacy_io_sparse_attrs,
1084+ .is_bin_visible = pci_legacy_io_sparse_is_visible,
1085+};
1086+
1087+static const struct attribute_group pci_legacy_mem_group = {
1088+ .bin_attrs = pci_legacy_mem_attrs,
1089+ .is_bin_visible = pci_legacy_mem_is_visible,
1090+};
1091+
1092+static const struct attribute_group pci_legacy_mem_sparse_group = {
1093+ .bin_attrs = pci_legacy_mem_sparse_attrs,
1094+ .is_bin_visible = pci_legacy_mem_sparse_is_visible,
1095+};
1096+
1097+void pci_create_legacy_files(struct pci_bus *b) { }
1098+void pci_remove_legacy_files(struct pci_bus *b) { }
10751099 #endif /* HAVE_PCI_LEGACY */
10761100
1101+const struct attribute_group *pcibus_groups[] = {
1102+ &pcibus_group,
1103+#ifdef HAVE_PCI_LEGACY
1104+ &pci_legacy_io_group,
1105+ &pci_legacy_io_sparse_group,
1106+ &pci_legacy_mem_group,
1107+ &pci_legacy_mem_sparse_group,
1108+#endif
1109+ NULL,
1110+};
1111+
10771112 #if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
10781113 /**
10791114 * pci_mmap_resource - map a PCI resource into user memory space

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t