Skip to content

Commit b8de187

Browse files
committed
Merge branch 'pci/sysfs'
- Compile pci-sysfs.c only if CONFIG_SYSFS=y, which reduces kernel size by ~120KB when it's disabled (Lukas Wunner) - Remove obsolete pci_cleanup_rom() declaration (Lukas Wunner) - Rework pci_dev_resource_resize_attr(n) macros to call a function instead of duplicating most of the body, which saves about 2.5KB of text (Ilpo Järvinen) * pci/sysfs: PCI/sysfs: Demacrofy pci_dev_resource_resize_attr(n) functions PCI: Remove obsolete pci_cleanup_rom() declaration PCI/sysfs: Compile pci-sysfs.c only if CONFIG_SYSFS=y # Conflicts: # drivers/pci/Makefile
2 parents 598b08b + f6c7399 commit b8de187

File tree

5 files changed

+124
-99
lines changed

5 files changed

+124
-99
lines changed

drivers/pci/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
obj-$(CONFIG_PCI) += access.o bus.o probe.o host-bridge.o \
66
remove.o pci.o pci-driver.o search.o \
7-
pci-sysfs.o rom.o setup-res.o irq.o vpd.o \
7+
rom.o setup-res.o irq.o vpd.o \
88
setup-bus.o vc.o mmap.o devres.o
99

1010
obj-$(CONFIG_PCI) += msi/
1111
obj-$(CONFIG_PCI) += pcie/
1212

1313
ifdef CONFIG_PCI
1414
obj-$(CONFIG_PROC_FS) += proc.o
15-
obj-$(CONFIG_SYSFS) += slot.o
15+
obj-$(CONFIG_SYSFS) += pci-sysfs.o slot.o
1616
obj-$(CONFIG_ACPI) += pci-acpi.o
1717
obj-$(CONFIG_GENERIC_PCI_IOMAP) += iomap.o
1818
endif

drivers/pci/mmap.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <linux/mm.h>
1212
#include <linux/pci.h>
1313

14+
#include "pci.h"
15+
1416
#ifdef ARCH_GENERIC_PCI_MMAP_RESOURCE
1517

1618
static const struct vm_operations_struct pci_phys_vm_ops = {
@@ -50,3 +52,30 @@ int pci_mmap_resource_range(struct pci_dev *pdev, int bar,
5052
}
5153

5254
#endif
55+
56+
#if (defined(CONFIG_SYSFS) || defined(CONFIG_PROC_FS)) && \
57+
(defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE))
58+
59+
int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
60+
enum pci_mmap_api mmap_api)
61+
{
62+
resource_size_t pci_start = 0, pci_end;
63+
unsigned long nr, start, size;
64+
65+
if (pci_resource_len(pdev, resno) == 0)
66+
return 0;
67+
nr = vma_pages(vma);
68+
start = vma->vm_pgoff;
69+
size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
70+
if (mmap_api == PCI_MMAP_PROCFS) {
71+
pci_resource_to_user(pdev, resno, &pdev->resource[resno],
72+
&pci_start, &pci_end);
73+
pci_start >>= PAGE_SHIFT;
74+
}
75+
if (start >= pci_start && start < pci_start + size &&
76+
start + nr <= pci_start + size)
77+
return 1;
78+
return 0;
79+
}
80+
81+
#endif

drivers/pci/pci-sysfs.c

Lines changed: 75 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,29 +1022,6 @@ void pci_remove_legacy_files(struct pci_bus *b)
10221022
#endif /* HAVE_PCI_LEGACY */
10231023

10241024
#if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
1025-
1026-
int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
1027-
enum pci_mmap_api mmap_api)
1028-
{
1029-
unsigned long nr, start, size;
1030-
resource_size_t pci_start = 0, pci_end;
1031-
1032-
if (pci_resource_len(pdev, resno) == 0)
1033-
return 0;
1034-
nr = vma_pages(vma);
1035-
start = vma->vm_pgoff;
1036-
size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
1037-
if (mmap_api == PCI_MMAP_PROCFS) {
1038-
pci_resource_to_user(pdev, resno, &pdev->resource[resno],
1039-
&pci_start, &pci_end);
1040-
pci_start >>= PAGE_SHIFT;
1041-
}
1042-
if (start >= pci_start && start < pci_start + size &&
1043-
start + nr <= pci_start + size)
1044-
return 1;
1045-
return 0;
1046-
}
1047-
10481025
/**
10491026
* pci_mmap_resource - map a PCI resource into user memory space
10501027
* @kobj: kobject for mapping
@@ -1410,79 +1387,89 @@ static const struct attribute_group pci_dev_reset_attr_group = {
14101387
.is_visible = pci_dev_reset_attr_is_visible,
14111388
};
14121389

1390+
static ssize_t __resource_resize_show(struct device *dev, int n, char *buf)
1391+
{
1392+
struct pci_dev *pdev = to_pci_dev(dev);
1393+
ssize_t ret;
1394+
1395+
pci_config_pm_runtime_get(pdev);
1396+
1397+
ret = sysfs_emit(buf, "%016llx\n",
1398+
(u64)pci_rebar_get_possible_sizes(pdev, n));
1399+
1400+
pci_config_pm_runtime_put(pdev);
1401+
1402+
return ret;
1403+
}
1404+
1405+
static ssize_t __resource_resize_store(struct device *dev, int n,
1406+
const char *buf, size_t count)
1407+
{
1408+
struct pci_dev *pdev = to_pci_dev(dev);
1409+
unsigned long size, flags;
1410+
int ret, i;
1411+
u16 cmd;
1412+
1413+
if (kstrtoul(buf, 0, &size) < 0)
1414+
return -EINVAL;
1415+
1416+
device_lock(dev);
1417+
if (dev->driver) {
1418+
ret = -EBUSY;
1419+
goto unlock;
1420+
}
1421+
1422+
pci_config_pm_runtime_get(pdev);
1423+
1424+
if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
1425+
ret = aperture_remove_conflicting_pci_devices(pdev,
1426+
"resourceN_resize");
1427+
if (ret)
1428+
goto pm_put;
1429+
}
1430+
1431+
pci_read_config_word(pdev, PCI_COMMAND, &cmd);
1432+
pci_write_config_word(pdev, PCI_COMMAND,
1433+
cmd & ~PCI_COMMAND_MEMORY);
1434+
1435+
flags = pci_resource_flags(pdev, n);
1436+
1437+
pci_remove_resource_files(pdev);
1438+
1439+
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
1440+
if (pci_resource_len(pdev, i) &&
1441+
pci_resource_flags(pdev, i) == flags)
1442+
pci_release_resource(pdev, i);
1443+
}
1444+
1445+
ret = pci_resize_resource(pdev, n, size);
1446+
1447+
pci_assign_unassigned_bus_resources(pdev->bus);
1448+
1449+
if (pci_create_resource_files(pdev))
1450+
pci_warn(pdev, "Failed to recreate resource files after BAR resizing\n");
1451+
1452+
pci_write_config_word(pdev, PCI_COMMAND, cmd);
1453+
pm_put:
1454+
pci_config_pm_runtime_put(pdev);
1455+
unlock:
1456+
device_unlock(dev);
1457+
1458+
return ret ? ret : count;
1459+
}
1460+
14131461
#define pci_dev_resource_resize_attr(n) \
14141462
static ssize_t resource##n##_resize_show(struct device *dev, \
14151463
struct device_attribute *attr, \
1416-
char * buf) \
1464+
char *buf) \
14171465
{ \
1418-
struct pci_dev *pdev = to_pci_dev(dev); \
1419-
ssize_t ret; \
1420-
\
1421-
pci_config_pm_runtime_get(pdev); \
1422-
\
1423-
ret = sysfs_emit(buf, "%016llx\n", \
1424-
(u64)pci_rebar_get_possible_sizes(pdev, n)); \
1425-
\
1426-
pci_config_pm_runtime_put(pdev); \
1427-
\
1428-
return ret; \
1466+
return __resource_resize_show(dev, n, buf); \
14291467
} \
1430-
\
14311468
static ssize_t resource##n##_resize_store(struct device *dev, \
14321469
struct device_attribute *attr,\
14331470
const char *buf, size_t count)\
14341471
{ \
1435-
struct pci_dev *pdev = to_pci_dev(dev); \
1436-
unsigned long size, flags; \
1437-
int ret, i; \
1438-
u16 cmd; \
1439-
\
1440-
if (kstrtoul(buf, 0, &size) < 0) \
1441-
return -EINVAL; \
1442-
\
1443-
device_lock(dev); \
1444-
if (dev->driver) { \
1445-
ret = -EBUSY; \
1446-
goto unlock; \
1447-
} \
1448-
\
1449-
pci_config_pm_runtime_get(pdev); \
1450-
\
1451-
if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) { \
1452-
ret = aperture_remove_conflicting_pci_devices(pdev, \
1453-
"resourceN_resize"); \
1454-
if (ret) \
1455-
goto pm_put; \
1456-
} \
1457-
\
1458-
pci_read_config_word(pdev, PCI_COMMAND, &cmd); \
1459-
pci_write_config_word(pdev, PCI_COMMAND, \
1460-
cmd & ~PCI_COMMAND_MEMORY); \
1461-
\
1462-
flags = pci_resource_flags(pdev, n); \
1463-
\
1464-
pci_remove_resource_files(pdev); \
1465-
\
1466-
for (i = 0; i < PCI_STD_NUM_BARS; i++) { \
1467-
if (pci_resource_len(pdev, i) && \
1468-
pci_resource_flags(pdev, i) == flags) \
1469-
pci_release_resource(pdev, i); \
1470-
} \
1471-
\
1472-
ret = pci_resize_resource(pdev, n, size); \
1473-
\
1474-
pci_assign_unassigned_bus_resources(pdev->bus); \
1475-
\
1476-
if (pci_create_resource_files(pdev)) \
1477-
pci_warn(pdev, "Failed to recreate resource files after BAR resizing\n");\
1478-
\
1479-
pci_write_config_word(pdev, PCI_COMMAND, cmd); \
1480-
pm_put: \
1481-
pci_config_pm_runtime_put(pdev); \
1482-
unlock: \
1483-
device_unlock(dev); \
1484-
\
1485-
return ret ? ret : count; \
1472+
return __resource_resize_store(dev, n, buf, count); \
14861473
} \
14871474
static DEVICE_ATTR_RW(resource##n##_resize)
14881475

@@ -1660,7 +1647,7 @@ static const struct attribute_group pcie_dev_attr_group = {
16601647
.is_visible = pcie_dev_attrs_are_visible,
16611648
};
16621649

1663-
static const struct attribute_group *pci_dev_attr_groups[] = {
1650+
const struct attribute_group *pci_dev_attr_groups[] = {
16641651
&pci_dev_attr_group,
16651652
&pci_dev_hp_attr_group,
16661653
#ifdef CONFIG_PCI_IOV
@@ -1677,7 +1664,3 @@ static const struct attribute_group *pci_dev_attr_groups[] = {
16771664
#endif
16781665
NULL,
16791666
};
1680-
1681-
const struct device_type pci_dev_type = {
1682-
.groups = pci_dev_attr_groups,
1683-
};

drivers/pci/pci.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ bool pcie_cap_has_rtctl(const struct pci_dev *dev);
3131

3232
/* Functions internal to the PCI core code */
3333

34-
int pci_create_sysfs_dev_files(struct pci_dev *pdev);
35-
void pci_remove_sysfs_dev_files(struct pci_dev *pdev);
36-
void pci_cleanup_rom(struct pci_dev *dev);
3734
#ifdef CONFIG_DMI
3835
extern const struct attribute_group pci_dev_smbios_attr_group;
3936
#endif
@@ -151,7 +148,7 @@ static inline int pci_proc_detach_bus(struct pci_bus *bus) { return 0; }
151148
/* Functions for PCI Hotplug drivers to use */
152149
int pci_hp_add_bridge(struct pci_dev *dev);
153150

154-
#ifdef HAVE_PCI_LEGACY
151+
#if defined(CONFIG_SYSFS) && defined(HAVE_PCI_LEGACY)
155152
void pci_create_legacy_files(struct pci_bus *bus);
156153
void pci_remove_legacy_files(struct pci_bus *bus);
157154
#else
@@ -184,10 +181,22 @@ static inline int pci_no_d1d2(struct pci_dev *dev)
184181
return (dev->no_d1d2 || parent_dstates);
185182

186183
}
184+
185+
#ifdef CONFIG_SYSFS
186+
int pci_create_sysfs_dev_files(struct pci_dev *pdev);
187+
void pci_remove_sysfs_dev_files(struct pci_dev *pdev);
187188
extern const struct attribute_group *pci_dev_groups[];
189+
extern const struct attribute_group *pci_dev_attr_groups[];
188190
extern const struct attribute_group *pcibus_groups[];
189-
extern const struct device_type pci_dev_type;
190191
extern const struct attribute_group *pci_bus_groups[];
192+
#else
193+
static inline int pci_create_sysfs_dev_files(struct pci_dev *pdev) { return 0; }
194+
static inline void pci_remove_sysfs_dev_files(struct pci_dev *pdev) { }
195+
#define pci_dev_groups NULL
196+
#define pci_dev_attr_groups NULL
197+
#define pcibus_groups NULL
198+
#define pci_bus_groups NULL
199+
#endif
191200

192201
extern unsigned long pci_hotplug_io_size;
193202
extern unsigned long pci_hotplug_mmio_size;

drivers/pci/probe.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2297,6 +2297,10 @@ static void pci_release_dev(struct device *dev)
22972297
kfree(pci_dev);
22982298
}
22992299

2300+
static const struct device_type pci_dev_type = {
2301+
.groups = pci_dev_attr_groups,
2302+
};
2303+
23002304
struct pci_dev *pci_alloc_dev(struct pci_bus *bus)
23012305
{
23022306
struct pci_dev *dev;

0 commit comments

Comments
 (0)