Skip to content

Commit 783602c

Browse files
ij-intelbjorn-helgaas
authored andcommitted
PCI: Use resource_set_{range,size}() helpers
Convert open-coded resource size calculations to use resource_set_{range,size}() helpers. While at it, use SZ_* for size parameter where appropriate which makes the intent of code more obvious. Also, cast sizes to resource_size_t, not u64. Link: https://lore.kernel.org/r/20240614100606.15830-3-ilpo.jarvinen@linux.intel.com Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com> Signed-off-by: Bjorn Helgaas <bhelgaas@google.com> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent 9fb6fef commit 783602c

File tree

8 files changed

+34
-45
lines changed

8 files changed

+34
-45
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
@@ -6649,8 +6649,7 @@ static void pci_request_resource_alignment(struct pci_dev *dev, int bar,
66496649
} else {
66506650
r->flags &= ~IORESOURCE_SIZEALIGN;
66516651
r->flags |= IORESOURCE_STARTALIGN;
6652-
r->start = align;
6653-
r->end = r->start + size - 1;
6652+
resource_set_range(r, align, size);
66546653
}
66556654
r->flags |= IORESOURCE_UNSET;
66566655
}

drivers/pci/quirks.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <linux/nvme.h>
3030
#include <linux/platform_data/x86/apple.h>
3131
#include <linux/pm_runtime.h>
32+
#include <linux/sizes.h>
3233
#include <linux/suspend.h>
3334
#include <linux/switchtec.h>
3435
#include "pci.h"
@@ -586,8 +587,7 @@ static void quirk_extend_bar_to_page(struct pci_dev *dev)
586587
const char *r_name = pci_resource_name(dev, i);
587588

588589
if (r->flags & IORESOURCE_MEM && resource_size(r) < PAGE_SIZE) {
589-
r->end = PAGE_SIZE - 1;
590-
r->start = 0;
590+
resource_set_range(r, 0, PAGE_SIZE);
591591
r->flags |= IORESOURCE_UNSET;
592592
pci_info(dev, "%s %pR: expanded to page size\n",
593593
r_name, r);
@@ -606,8 +606,7 @@ static void quirk_s3_64M(struct pci_dev *dev)
606606

607607
if ((r->start & 0x3ffffff) || r->end != r->start + 0x3ffffff) {
608608
r->flags |= IORESOURCE_UNSET;
609-
r->start = 0;
610-
r->end = 0x3ffffff;
609+
resource_set_range(r, 0, SZ_64M);
611610
}
612611
}
613612
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_S3, PCI_DEVICE_ID_S3_868, quirk_s3_64M);
@@ -1342,8 +1341,7 @@ static void quirk_dunord(struct pci_dev *dev)
13421341
struct resource *r = &dev->resource[1];
13431342

13441343
r->flags |= IORESOURCE_UNSET;
1345-
r->start = 0;
1346-
r->end = 0xffffff;
1344+
resource_set_range(r, 0, SZ_16M);
13471345
}
13481346
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_DUNORD, PCI_DEVICE_ID_DUNORD_I3000, quirk_dunord);
13491347

@@ -2340,8 +2338,7 @@ static void quirk_tc86c001_ide(struct pci_dev *dev)
23402338

23412339
if (r->start & 0x8) {
23422340
r->flags |= IORESOURCE_UNSET;
2343-
r->start = 0;
2344-
r->end = 0xf;
2341+
resource_set_range(r, 0, SZ_16);
23452342
}
23462343
}
23472344
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TOSHIBA_2,
@@ -2369,8 +2366,7 @@ static void quirk_plx_pci9050(struct pci_dev *dev)
23692366
pci_info(dev, "Re-allocating PLX PCI 9050 BAR %u to length 256 to avoid bit 7 bug\n",
23702367
bar);
23712368
r->flags |= IORESOURCE_UNSET;
2372-
r->start = 0;
2373-
r->end = 0xff;
2369+
resource_set_range(r, 0, SZ_256);
23742370
}
23752371
}
23762372
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
@@ -3522,13 +3518,13 @@ static void quirk_intel_ntb(struct pci_dev *dev)
35223518
if (rc)
35233519
return;
35243520

3525-
dev->resource[2].end = dev->resource[2].start + ((u64) 1 << val) - 1;
3521+
resource_set_size(&dev->resource[2], (resource_size_t)1 << val);
35263522

35273523
rc = pci_read_config_byte(dev, 0x00D1, &val);
35283524
if (rc)
35293525
return;
35303526

3531-
dev->resource[4].end = dev->resource[4].start + ((u64) 1 << val) - 1;
3527+
resource_set_size(&dev->resource[4], (resource_size_t)1 << val);
35323528
}
35333529
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0e08, quirk_intel_ntb);
35343530
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0e0d, quirk_intel_ntb);

drivers/pci/setup-bus.c

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,7 @@ static void reassign_resources_sorted(struct list_head *realloc_head,
246246
add_size = add_res->add_size;
247247
align = add_res->min_align;
248248
if (!resource_size(res)) {
249-
res->start = align;
250-
res->end = res->start + add_size - 1;
249+
resource_set_range(res, align, add_size);
251250
if (pci_assign_resource(add_res->dev, idx))
252251
reset_resource(res);
253252
} else {
@@ -938,8 +937,7 @@ static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
938937
return;
939938
}
940939

941-
b_res->start = min_align;
942-
b_res->end = b_res->start + size0 - 1;
940+
resource_set_range(b_res, min_align, size0);
943941
b_res->flags |= IORESOURCE_STARTALIGN;
944942
if (bus->self && size1 > size0 && realloc_head) {
945943
add_to_list(realloc_head, bus->self, b_res, size1-size0,
@@ -1202,8 +1200,7 @@ static void pci_bus_size_cardbus(struct pci_bus *bus,
12021200
* Reserve some resources for CardBus. We reserve a fixed amount
12031201
* of bus space for CardBus bridges.
12041202
*/
1205-
b_res->start = pci_cardbus_io_size;
1206-
b_res->end = b_res->start + pci_cardbus_io_size - 1;
1203+
resource_set_range(b_res, pci_cardbus_io_size, pci_cardbus_io_size);
12071204
b_res->flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
12081205
if (realloc_head) {
12091206
b_res->end -= pci_cardbus_io_size;
@@ -1215,8 +1212,7 @@ static void pci_bus_size_cardbus(struct pci_bus *bus,
12151212
b_res = &bridge->resource[PCI_CB_BRIDGE_IO_1_WINDOW];
12161213
if (b_res->parent)
12171214
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;
1215+
resource_set_range(b_res, pci_cardbus_io_size, pci_cardbus_io_size);
12201216
b_res->flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
12211217
if (realloc_head) {
12221218
b_res->end -= pci_cardbus_io_size;
@@ -1249,8 +1245,8 @@ static void pci_bus_size_cardbus(struct pci_bus *bus,
12491245
* Otherwise, allocate one region of twice the size.
12501246
*/
12511247
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;
1248+
resource_set_range(b_res, pci_cardbus_mem_size,
1249+
pci_cardbus_mem_size);
12541250
b_res->flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH |
12551251
IORESOURCE_STARTALIGN;
12561252
if (realloc_head) {
@@ -1267,8 +1263,7 @@ static void pci_bus_size_cardbus(struct pci_bus *bus,
12671263
b_res = &bridge->resource[PCI_CB_BRIDGE_MEM_1_WINDOW];
12681264
if (b_res->parent)
12691265
goto handle_done;
1270-
b_res->start = pci_cardbus_mem_size;
1271-
b_res->end = b_res->start + b_res_3_size - 1;
1266+
resource_set_range(b_res, pci_cardbus_mem_size, b_res_3_size);
12721267
b_res->flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN;
12731268
if (realloc_head) {
12741269
b_res->end -= b_res_3_size;
@@ -1847,7 +1842,7 @@ static void adjust_bridge_window(struct pci_dev *bridge, struct resource *res,
18471842
return;
18481843
}
18491844

1850-
res->end = res->start + new_size - 1;
1845+
resource_set_size(res, new_size);
18511846

18521847
/* If the resource is part of the add_list, remove it now */
18531848
if (add_list)
@@ -2010,8 +2005,8 @@ static void pci_bus_distribute_available_resources(struct pci_bus *bus,
20102005
* what is available).
20112006
*/
20122007
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;
2008+
resource_set_size(&io, align ? ALIGN_DOWN(io_per_b, align)
2009+
: io_per_b);
20152010

20162011
/*
20172012
* The x_per_b holds the extra resource space that can be
@@ -2023,15 +2018,15 @@ static void pci_bus_distribute_available_resources(struct pci_bus *bus,
20232018

20242019
res = &dev->resource[PCI_BRIDGE_MEM_WINDOW];
20252020
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;
2021+
resource_set_size(&mmio, align ? ALIGN_DOWN(mmio_per_b, align)
2022+
: mmio_per_b);
20282023
mmio.start -= resource_size(res);
20292024

20302025
res = &dev->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
20312026
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;
2027+
resource_set_size(&mmio_pref,
2028+
align ? ALIGN_DOWN(mmio_pref_per_b, align)
2029+
: mmio_pref_per_b);
20352030
mmio_pref.start -= resource_size(res);
20362031

20372032
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);

0 commit comments

Comments
 (0)