Skip to content

Commit c03d361

Browse files
committed
Merge branch 'pci/resource'
- Add resource_set_size() to set resource size when start has already been set (Ilpo Järvinen) - Add resource_set_range() helper to set both resource start and size (Ilpo Järvinen) - Use IS_ALIGNED() and resource_size() in quirk_s3_64M() instead of open-coding them (Ilpo Järvinen) - Add ALIGN_DOWN_IF_NONZERO() to avoid code duplication when distributing resources across devices (Ilpo Järvinen) - Improve pdev_sort_resources() warning message to be more specific (Ilpo Järvinen) * pci/resource: PCI: Improve pdev_sort_resources() warning message PCI: Add ALIGN_DOWN_IF_NONZERO() helper PCI: Use align and resource helpers, and SZ_* in quirk_s3_64M() PCI: Use resource_set_{range,size}() helpers resource: Add resource set range and size helpers
2 parents d985e2b + 19f73e9 commit c03d361

File tree

9 files changed

+72
-48
lines changed

9 files changed

+72
-48
lines changed

drivers/pci/controller/pci-tegra.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1460,7 +1460,7 @@ static int tegra_pcie_get_resources(struct tegra_pcie *pcie)
14601460
pcie->cs = *res;
14611461

14621462
/* constrain configuration space to 4 KiB */
1463-
pcie->cs.end = pcie->cs.start + SZ_4K - 1;
1463+
resource_set_size(&pcie->cs, SZ_4K);
14641464

14651465
pcie->cfg = devm_ioremap_resource(dev, &pcie->cs);
14661466
if (IS_ERR(pcie->cfg)) {

drivers/pci/controller/pci-thunder-pem.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,9 @@ static int thunder_pem_acpi_init(struct pci_config_window *cfg)
400400
* Reserve 64K size PEM specific resources. The full 16M range
401401
* size is required for thunder_pem_init() call.
402402
*/
403-
res_pem->end = res_pem->start + SZ_64K - 1;
403+
resource_set_size(res_pem, SZ_64K);
404404
thunder_pem_reserve_range(dev, root->segment, res_pem);
405-
res_pem->end = res_pem->start + SZ_16M - 1;
405+
resource_set_size(res_pem, SZ_16M);
406406

407407
/* Reserve PCI configuration space as well. */
408408
thunder_pem_reserve_range(dev, root->segment, &cfg->res);

drivers/pci/ecam.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ struct pci_config_window *pci_ecam_create(struct device *dev,
5555
bus_range_max = resource_size(cfgres) >> bus_shift;
5656
if (bus_range > bus_range_max) {
5757
bus_range = bus_range_max;
58-
cfg->busr.end = busr->start + bus_range - 1;
58+
resource_set_size(&cfg->busr, bus_range);
5959
dev_warn(dev, "ECAM area %pR can only accommodate %pR (reduced from %pR desired)\n",
6060
cfgres, &cfg->busr, busr);
6161
}

drivers/pci/iov.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,8 @@ int pci_iov_add_virtfn(struct pci_dev *dev, int id)
327327
virtfn->resource[i].name = pci_name(virtfn);
328328
virtfn->resource[i].flags = res->flags;
329329
size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
330-
virtfn->resource[i].start = res->start + size * id;
331-
virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
330+
resource_set_range(&virtfn->resource[i],
331+
res->start + size * id, size);
332332
rc = request_resource(res, &virtfn->resource[i]);
333333
BUG_ON(rc);
334334
}
@@ -804,7 +804,7 @@ static int sriov_init(struct pci_dev *dev, int pos)
804804
goto failed;
805805
}
806806
iov->barsz[i] = resource_size(res);
807-
res->end = res->start + resource_size(res) * total - 1;
807+
resource_set_size(res, resource_size(res) * total);
808808
pci_info(dev, "%s %pR: contains BAR %d for %d VFs\n",
809809
res_name, res, i, total);
810810
i += bar64;

drivers/pci/pci.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6679,8 +6679,7 @@ static void pci_request_resource_alignment(struct pci_dev *dev, int bar,
66796679
} else {
66806680
r->flags &= ~IORESOURCE_SIZEALIGN;
66816681
r->flags |= IORESOURCE_STARTALIGN;
6682-
r->start = align;
6683-
r->end = r->start + size - 1;
6682+
resource_set_range(r, align, size);
66846683
}
66856684
r->flags |= IORESOURCE_UNSET;
66866685
}

drivers/pci/quirks.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* file, where their drivers can use them.
1313
*/
1414

15+
#include <linux/align.h>
1516
#include <linux/bitfield.h>
1617
#include <linux/types.h>
1718
#include <linux/kernel.h>
@@ -29,6 +30,7 @@
2930
#include <linux/nvme.h>
3031
#include <linux/platform_data/x86/apple.h>
3132
#include <linux/pm_runtime.h>
33+
#include <linux/sizes.h>
3234
#include <linux/suspend.h>
3335
#include <linux/switchtec.h>
3436
#include "pci.h"
@@ -588,8 +590,7 @@ static void quirk_extend_bar_to_page(struct pci_dev *dev)
588590
const char *r_name = pci_resource_name(dev, i);
589591

590592
if (r->flags & IORESOURCE_MEM && resource_size(r) < PAGE_SIZE) {
591-
r->end = PAGE_SIZE - 1;
592-
r->start = 0;
593+
resource_set_range(r, 0, PAGE_SIZE);
593594
r->flags |= IORESOURCE_UNSET;
594595
pci_info(dev, "%s %pR: expanded to page size\n",
595596
r_name, r);
@@ -606,10 +607,9 @@ static void quirk_s3_64M(struct pci_dev *dev)
606607
{
607608
struct resource *r = &dev->resource[0];
608609

609-
if ((r->start & 0x3ffffff) || r->end != r->start + 0x3ffffff) {
610+
if (!IS_ALIGNED(r->start, SZ_64M) || resource_size(r) != SZ_64M) {
610611
r->flags |= IORESOURCE_UNSET;
611-
r->start = 0;
612-
r->end = 0x3ffffff;
612+
resource_set_range(r, 0, SZ_64M);
613613
}
614614
}
615615
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_S3, PCI_DEVICE_ID_S3_868, quirk_s3_64M);
@@ -1344,8 +1344,7 @@ static void quirk_dunord(struct pci_dev *dev)
13441344
struct resource *r = &dev->resource[1];
13451345

13461346
r->flags |= IORESOURCE_UNSET;
1347-
r->start = 0;
1348-
r->end = 0xffffff;
1347+
resource_set_range(r, 0, SZ_16M);
13491348
}
13501349
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_DUNORD, PCI_DEVICE_ID_DUNORD_I3000, quirk_dunord);
13511350

@@ -2342,8 +2341,7 @@ static void quirk_tc86c001_ide(struct pci_dev *dev)
23422341

23432342
if (r->start & 0x8) {
23442343
r->flags |= IORESOURCE_UNSET;
2345-
r->start = 0;
2346-
r->end = 0xf;
2344+
resource_set_range(r, 0, SZ_16);
23472345
}
23482346
}
23492347
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TOSHIBA_2,
@@ -2371,8 +2369,7 @@ static void quirk_plx_pci9050(struct pci_dev *dev)
23712369
pci_info(dev, "Re-allocating PLX PCI 9050 BAR %u to length 256 to avoid bit 7 bug\n",
23722370
bar);
23732371
r->flags |= IORESOURCE_UNSET;
2374-
r->start = 0;
2375-
r->end = 0xff;
2372+
resource_set_range(r, 0, SZ_256);
23762373
}
23772374
}
23782375
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
@@ -3524,13 +3521,13 @@ static void quirk_intel_ntb(struct pci_dev *dev)
35243521
if (rc)
35253522
return;
35263523

3527-
dev->resource[2].end = dev->resource[2].start + ((u64) 1 << val) - 1;
3524+
resource_set_size(&dev->resource[2], (resource_size_t)1 << val);
35283525

35293526
rc = pci_read_config_byte(dev, 0x00D1, &val);
35303527
if (rc)
35313528
return;
35323529

3533-
dev->resource[4].end = dev->resource[4].start + ((u64) 1 << val) - 1;
3530+
resource_set_size(&dev->resource[4], (resource_size_t)1 << val);
35343531
}
35353532
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0e08, quirk_intel_ntb);
35363533
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0e0d, quirk_intel_ntb);

drivers/pci/setup-bus.c

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
134134
int i;
135135

136136
pci_dev_for_each_resource(dev, r, i) {
137+
const char *r_name = pci_resource_name(dev, i);
137138
struct pci_dev_resource *dev_res, *tmp;
138139
resource_size_t r_align;
139140
struct list_head *n;
@@ -146,8 +147,8 @@ static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
146147

147148
r_align = pci_resource_alignment(dev, r);
148149
if (!r_align) {
149-
pci_warn(dev, "BAR %d: %pR has bogus alignment\n",
150-
i, r);
150+
pci_warn(dev, "%s %pR: alignment must not be zero\n",
151+
r_name, r);
151152
continue;
152153
}
153154

@@ -246,8 +247,7 @@ static void reassign_resources_sorted(struct list_head *realloc_head,
246247
add_size = add_res->add_size;
247248
align = add_res->min_align;
248249
if (!resource_size(res)) {
249-
res->start = align;
250-
res->end = res->start + add_size - 1;
250+
resource_set_range(res, align, add_size);
251251
if (pci_assign_resource(add_res->dev, idx))
252252
reset_resource(res);
253253
} else {
@@ -938,8 +938,7 @@ static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
938938
return;
939939
}
940940

941-
b_res->start = min_align;
942-
b_res->end = b_res->start + size0 - 1;
941+
resource_set_range(b_res, min_align, size0);
943942
b_res->flags |= IORESOURCE_STARTALIGN;
944943
if (bus->self && size1 > size0 && realloc_head) {
945944
add_to_list(realloc_head, bus->self, b_res, size1-size0,
@@ -1202,8 +1201,7 @@ static void pci_bus_size_cardbus(struct pci_bus *bus,
12021201
* Reserve some resources for CardBus. We reserve a fixed amount
12031202
* of bus space for CardBus bridges.
12041203
*/
1205-
b_res->start = pci_cardbus_io_size;
1206-
b_res->end = b_res->start + pci_cardbus_io_size - 1;
1204+
resource_set_range(b_res, pci_cardbus_io_size, pci_cardbus_io_size);
12071205
b_res->flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
12081206
if (realloc_head) {
12091207
b_res->end -= pci_cardbus_io_size;
@@ -1215,8 +1213,7 @@ static void pci_bus_size_cardbus(struct pci_bus *bus,
12151213
b_res = &bridge->resource[PCI_CB_BRIDGE_IO_1_WINDOW];
12161214
if (b_res->parent)
12171215
goto handle_b_res_2;
1218-
b_res->start = pci_cardbus_io_size;
1219-
b_res->end = b_res->start + pci_cardbus_io_size - 1;
1216+
resource_set_range(b_res, pci_cardbus_io_size, pci_cardbus_io_size);
12201217
b_res->flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
12211218
if (realloc_head) {
12221219
b_res->end -= pci_cardbus_io_size;
@@ -1249,8 +1246,8 @@ static void pci_bus_size_cardbus(struct pci_bus *bus,
12491246
* Otherwise, allocate one region of twice the size.
12501247
*/
12511248
if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
1252-
b_res->start = pci_cardbus_mem_size;
1253-
b_res->end = b_res->start + pci_cardbus_mem_size - 1;
1249+
resource_set_range(b_res, pci_cardbus_mem_size,
1250+
pci_cardbus_mem_size);
12541251
b_res->flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH |
12551252
IORESOURCE_STARTALIGN;
12561253
if (realloc_head) {
@@ -1267,8 +1264,7 @@ static void pci_bus_size_cardbus(struct pci_bus *bus,
12671264
b_res = &bridge->resource[PCI_CB_BRIDGE_MEM_1_WINDOW];
12681265
if (b_res->parent)
12691266
goto handle_done;
1270-
b_res->start = pci_cardbus_mem_size;
1271-
b_res->end = b_res->start + b_res_3_size - 1;
1267+
resource_set_range(b_res, pci_cardbus_mem_size, b_res_3_size);
12721268
b_res->flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN;
12731269
if (realloc_head) {
12741270
b_res->end -= b_res_3_size;
@@ -1847,7 +1843,7 @@ static void adjust_bridge_window(struct pci_dev *bridge, struct resource *res,
18471843
return;
18481844
}
18491845

1850-
res->end = res->start + new_size - 1;
1846+
resource_set_size(res, new_size);
18511847

18521848
/* If the resource is part of the add_list, remove it now */
18531849
if (add_list)
@@ -1899,6 +1895,9 @@ static void remove_dev_resources(struct pci_dev *dev, struct resource *io,
18991895
}
19001896
}
19011897

1898+
#define ALIGN_DOWN_IF_NONZERO(addr, align) \
1899+
((align) ? ALIGN_DOWN((addr), (align)) : (addr))
1900+
19021901
/*
19031902
* io, mmio and mmio_pref contain the total amount of bridge window space
19041903
* available. This includes the minimal space needed to cover all the
@@ -2010,8 +2009,7 @@ static void pci_bus_distribute_available_resources(struct pci_bus *bus,
20102009
* what is available).
20112010
*/
20122011
align = pci_resource_alignment(dev, res);
2013-
io.end = align ? io.start + ALIGN_DOWN(io_per_b, align) - 1
2014-
: io.start + io_per_b - 1;
2012+
resource_set_size(&io, ALIGN_DOWN_IF_NONZERO(io_per_b, align));
20152013

20162014
/*
20172015
* The x_per_b holds the extra resource space that can be
@@ -2023,15 +2021,14 @@ static void pci_bus_distribute_available_resources(struct pci_bus *bus,
20232021

20242022
res = &dev->resource[PCI_BRIDGE_MEM_WINDOW];
20252023
align = pci_resource_alignment(dev, res);
2026-
mmio.end = align ? mmio.start + ALIGN_DOWN(mmio_per_b, align) - 1
2027-
: mmio.start + mmio_per_b - 1;
2024+
resource_set_size(&mmio,
2025+
ALIGN_DOWN_IF_NONZERO(mmio_per_b,align));
20282026
mmio.start -= resource_size(res);
20292027

20302028
res = &dev->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
20312029
align = pci_resource_alignment(dev, res);
2032-
mmio_pref.end = align ? mmio_pref.start +
2033-
ALIGN_DOWN(mmio_pref_per_b, align) - 1
2034-
: mmio_pref.start + mmio_pref_per_b - 1;
2030+
resource_set_size(&mmio_pref,
2031+
ALIGN_DOWN_IF_NONZERO(mmio_pref_per_b, align));
20352032
mmio_pref.start -= resource_size(res);
20362033

20372034
pci_bus_distribute_available_resources(b, add_list, io, mmio,

drivers/pci/setup-res.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,7 @@ static int pci_revert_fw_address(struct resource *res, struct pci_dev *dev,
211211

212212
start = res->start;
213213
end = res->end;
214-
res->start = fw_addr;
215-
res->end = res->start + size - 1;
214+
resource_set_range(res, fw_addr, size);
216215
res->flags &= ~IORESOURCE_UNSET;
217216

218217
root = pci_find_parent_resource(dev, res);
@@ -463,7 +462,7 @@ int pci_resize_resource(struct pci_dev *dev, int resno, int size)
463462
if (ret)
464463
return ret;
465464

466-
res->end = res->start + pci_rebar_size_to_bytes(size) - 1;
465+
resource_set_size(res, pci_rebar_size_to_bytes(size));
467466

468467
/* Check if the new config works by trying to assign everything. */
469468
if (dev->bus->self) {
@@ -475,7 +474,7 @@ int pci_resize_resource(struct pci_dev *dev, int resno, int size)
475474

476475
error_resize:
477476
pci_rebar_set_size(dev, resno, old);
478-
res->end = res->start + pci_rebar_size_to_bytes(old) - 1;
477+
resource_set_size(res, pci_rebar_size_to_bytes(old));
479478
return ret;
480479
}
481480
EXPORT_SYMBOL(pci_resize_resource);

include/linux/ioport.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,38 @@ struct resource *lookup_resource(struct resource *root, resource_size_t start);
249249
int adjust_resource(struct resource *res, resource_size_t start,
250250
resource_size_t size);
251251
resource_size_t resource_alignment(struct resource *res);
252+
253+
/**
254+
* resource_set_size - Calculate resource end address from size and start
255+
* @res: Resource descriptor
256+
* @size: Size of the resource
257+
*
258+
* Calculate the end address for @res based on @size.
259+
*
260+
* Note: The start address of @res must be set when calling this function.
261+
* Prefer resource_set_range() if setting both the start address and @size.
262+
*/
263+
static inline void resource_set_size(struct resource *res, resource_size_t size)
264+
{
265+
res->end = res->start + size - 1;
266+
}
267+
268+
/**
269+
* resource_set_range - Set resource start and end addresses
270+
* @res: Resource descriptor
271+
* @start: Start address for the resource
272+
* @size: Size of the resource
273+
*
274+
* Set @res start address and calculate the end address based on @size.
275+
*/
276+
static inline void resource_set_range(struct resource *res,
277+
resource_size_t start,
278+
resource_size_t size)
279+
{
280+
res->start = start;
281+
resource_set_size(res, size);
282+
}
283+
252284
static inline resource_size_t resource_size(const struct resource *res)
253285
{
254286
return res->end - res->start + 1;

0 commit comments

Comments
 (0)