blob: 24522aa37665331f5bf3a4d78dda00bbe31ca10f [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Grant Likelyfbe65442009-08-25 20:07:11 +00002/*
3 * Helper routines to scan the device tree for PCI devices and busses
4 *
5 * Migrated out of PowerPC architecture pci_64.c file by Grant Likely
6 * <grant.likely@secretlab.ca> so that these routines are available for
7 * 32 bit also.
8 *
9 * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
10 * Rework, based on alpha PCI code.
11 * Copyright (c) 2009 Secret Lab Technologies Ltd.
Grant Likelyfbe65442009-08-25 20:07:11 +000012 */
13
14#include <linux/pci.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040015#include <linux/export.h>
Grant Likelyfbe65442009-08-25 20:07:11 +000016#include <asm/pci-bridge.h>
17#include <asm/prom.h>
18
19/**
20 * get_int_prop - Decode a u32 from a device tree property
21 */
22static u32 get_int_prop(struct device_node *np, const char *name, u32 def)
23{
Anton Blancharda795dc52013-08-07 02:01:41 +100024 const __be32 *prop;
Grant Likelyfbe65442009-08-25 20:07:11 +000025 int len;
26
27 prop = of_get_property(np, name, &len);
28 if (prop && len >= 4)
Anton Blancharda795dc52013-08-07 02:01:41 +100029 return of_read_number(prop, 1);
Grant Likelyfbe65442009-08-25 20:07:11 +000030 return def;
31}
32
33/**
34 * pci_parse_of_flags - Parse the flags cell of a device tree PCI address
35 * @addr0: value of 1st cell of a device tree PCI address.
36 * @bridge: Set this flag if the address is from a bridge 'ranges' property
37 */
Bryant G. Lyfc5f6222018-01-05 10:45:52 -060038unsigned int pci_parse_of_flags(u32 addr0, int bridge)
Grant Likelyfbe65442009-08-25 20:07:11 +000039{
40 unsigned int flags = 0;
41
42 if (addr0 & 0x02000000) {
43 flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
44 flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
45 flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
46 if (addr0 & 0x40000000)
47 flags |= IORESOURCE_PREFETCH
48 | PCI_BASE_ADDRESS_MEM_PREFETCH;
49 /* Note: We don't know whether the ROM has been left enabled
50 * by the firmware or not. We mark it as disabled (ie, we do
51 * not set the IORESOURCE_ROM_ENABLE flag) for now rather than
52 * do a config space read, it will be force-enabled if needed
53 */
54 if (!bridge && (addr0 & 0xff) == 0x30)
55 flags |= IORESOURCE_READONLY;
56 } else if (addr0 & 0x01000000)
57 flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
58 if (flags)
59 flags |= IORESOURCE_SIZEALIGN;
60 return flags;
61}
62
63/**
64 * of_pci_parse_addrs - Parse PCI addresses assigned in the device tree node
65 * @node: device tree node for the PCI device
66 * @dev: pci_dev structure for the device
67 *
68 * This function parses the 'assigned-addresses' property of a PCI devices'
69 * device tree node and writes them into the associated pci_dev structure.
70 */
71static void of_pci_parse_addrs(struct device_node *node, struct pci_dev *dev)
72{
73 u64 base, size;
74 unsigned int flags;
Bjorn Helgaas39aa1462012-03-16 17:48:14 -060075 struct pci_bus_region region;
Grant Likelyfbe65442009-08-25 20:07:11 +000076 struct resource *res;
Anton Blancharda795dc52013-08-07 02:01:41 +100077 const __be32 *addrs;
Grant Likelyfbe65442009-08-25 20:07:11 +000078 u32 i;
79 int proplen;
80
81 addrs = of_get_property(node, "assigned-addresses", &proplen);
82 if (!addrs)
83 return;
84 pr_debug(" parse addresses (%d bytes) @ %p\n", proplen, addrs);
85 for (; proplen >= 20; proplen -= 20, addrs += 5) {
Anton Blancharda795dc52013-08-07 02:01:41 +100086 flags = pci_parse_of_flags(of_read_number(addrs, 1), 0);
Grant Likelyfbe65442009-08-25 20:07:11 +000087 if (!flags)
88 continue;
89 base = of_read_number(&addrs[1], 2);
90 size = of_read_number(&addrs[3], 2);
91 if (!size)
92 continue;
Anton Blancharda795dc52013-08-07 02:01:41 +100093 i = of_read_number(addrs, 1) & 0xff;
Grant Likelyfbe65442009-08-25 20:07:11 +000094 pr_debug(" base: %llx, size: %llx, i: %x\n",
95 (unsigned long long)base,
96 (unsigned long long)size, i);
97
98 if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
99 res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
100 } else if (i == dev->rom_base_reg) {
101 res = &dev->resource[PCI_ROM_RESOURCE];
Dan Williams92b19ff2015-08-10 23:07:06 -0400102 flags |= IORESOURCE_READONLY;
Grant Likelyfbe65442009-08-25 20:07:11 +0000103 } else {
104 printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
105 continue;
106 }
Grant Likelyfbe65442009-08-25 20:07:11 +0000107 res->flags = flags;
108 res->name = pci_name(dev);
Bjorn Helgaas39aa1462012-03-16 17:48:14 -0600109 region.start = base;
110 region.end = base + size - 1;
Yinghai Lufc279852013-12-09 22:54:40 -0800111 pcibios_bus_to_resource(dev->bus, res, &region);
Grant Likelyfbe65442009-08-25 20:07:11 +0000112 }
113}
114
115/**
116 * of_create_pci_dev - Given a device tree node on a pci bus, create a pci_dev
117 * @node: device tree node pointer
118 * @bus: bus the device is sitting on
119 * @devfn: PCI function number, extracted from device tree by caller.
120 */
121struct pci_dev *of_create_pci_dev(struct device_node *node,
122 struct pci_bus *bus, int devfn)
123{
124 struct pci_dev *dev;
Grant Likelyfbe65442009-08-25 20:07:11 +0000125
Gu Zheng8b1fce02013-05-25 21:48:31 +0800126 dev = pci_alloc_dev(bus);
Grant Likelyfbe65442009-08-25 20:07:11 +0000127 if (!dev)
128 return NULL;
Grant Likelyfbe65442009-08-25 20:07:11 +0000129
Rob Herringe5480bd2018-11-16 16:11:00 -0600130 pr_debug(" create device, devfn: %x, type: %s\n", devfn,
131 of_node_get_device_type(node));
Grant Likelyfbe65442009-08-25 20:07:11 +0000132
Grant Likelyb5d937d2011-02-04 11:24:11 -0700133 dev->dev.of_node = of_node_get(node);
Grant Likelyfbe65442009-08-25 20:07:11 +0000134 dev->dev.parent = bus->bridge;
135 dev->dev.bus = &pci_bus_type;
136 dev->devfn = devfn;
137 dev->multifunction = 0; /* maybe a lie? */
Linus Torvalds4406c562009-09-16 07:49:54 -0700138 dev->needs_freset = 0; /* pcie fundamental reset required */
Benjamin Herrenschmidtbb209c82010-01-26 17:10:03 +0000139 set_pcie_port_type(dev);
Grant Likelyfbe65442009-08-25 20:07:11 +0000140
Yijing Wang017ffe62015-07-17 17:16:32 +0800141 pci_dev_assign_slot(dev);
Grant Likelyfbe65442009-08-25 20:07:11 +0000142 dev->vendor = get_int_prop(node, "vendor-id", 0xffff);
143 dev->device = get_int_prop(node, "device-id", 0xffff);
144 dev->subsystem_vendor = get_int_prop(node, "subsystem-vendor-id", 0);
145 dev->subsystem_device = get_int_prop(node, "subsystem-id", 0);
146
147 dev->cfg_size = pci_cfg_space_size(dev);
148
149 dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(bus),
150 dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
151 dev->class = get_int_prop(node, "class-code", 0);
152 dev->revision = get_int_prop(node, "revision-id", 0);
153
154 pr_debug(" class: 0x%x\n", dev->class);
155 pr_debug(" revision: 0x%x\n", dev->revision);
156
Bjorn Helgaasc2defb52013-05-17 15:08:50 -0600157 dev->current_state = PCI_UNKNOWN; /* unknown power state */
Grant Likelyfbe65442009-08-25 20:07:11 +0000158 dev->error_state = pci_channel_io_normal;
159 dev->dma_mask = 0xffffffff;
160
Benjamin Herrenschmidt94afc002010-01-26 17:10:05 +0000161 /* Early fixups, before probing the BARs */
162 pci_fixup_device(pci_fixup_early, dev);
163
Rob Herringe5480bd2018-11-16 16:11:00 -0600164 if (of_node_is_type(node, "pci") || of_node_is_type(node, "pciex")) {
Grant Likelyfbe65442009-08-25 20:07:11 +0000165 /* a PCI-PCI bridge */
166 dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
167 dev->rom_base_reg = PCI_ROM_ADDRESS1;
Benjamin Herrenschmidtbb209c82010-01-26 17:10:03 +0000168 set_pcie_hotplug_bridge(dev);
Rob Herringe5480bd2018-11-16 16:11:00 -0600169 } else if (of_node_is_type(node, "cardbus")) {
Grant Likelyfbe65442009-08-25 20:07:11 +0000170 dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
171 } else {
172 dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
173 dev->rom_base_reg = PCI_ROM_ADDRESS;
174 /* Maybe do a default OF mapping here */
Michael Ellermanef24ba72016-09-06 21:53:24 +1000175 dev->irq = 0;
Grant Likelyfbe65442009-08-25 20:07:11 +0000176 }
177
178 of_pci_parse_addrs(node, dev);
179
180 pr_debug(" adding to system ...\n");
181
182 pci_device_add(dev, bus);
183
184 return dev;
185}
186EXPORT_SYMBOL(of_create_pci_dev);
187
188/**
189 * of_scan_pci_bridge - Set up a PCI bridge and scan for child nodes
Grant Likelyfbe65442009-08-25 20:07:11 +0000190 * @dev: pci_dev structure for the bridge
191 *
192 * of_scan_bus() calls this routine for each PCI bridge that it finds, and
193 * this routine in turn call of_scan_bus() recusively to scan for more child
194 * devices.
195 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800196void of_scan_pci_bridge(struct pci_dev *dev)
Grant Likelyfbe65442009-08-25 20:07:11 +0000197{
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +1000198 struct device_node *node = dev->dev.of_node;
Grant Likelyfbe65442009-08-25 20:07:11 +0000199 struct pci_bus *bus;
Daniel Axtens467efc22015-03-31 16:00:56 +1100200 struct pci_controller *phb;
Anton Blancharda795dc52013-08-07 02:01:41 +1000201 const __be32 *busrange, *ranges;
Grant Likelyfbe65442009-08-25 20:07:11 +0000202 int len, i, mode;
Bjorn Helgaas39aa1462012-03-16 17:48:14 -0600203 struct pci_bus_region region;
Grant Likelyfbe65442009-08-25 20:07:11 +0000204 struct resource *res;
205 unsigned int flags;
206 u64 size;
207
Rob Herringb7c670d2017-08-21 10:16:47 -0500208 pr_debug("of_scan_pci_bridge(%pOF)\n", node);
Grant Likelyfbe65442009-08-25 20:07:11 +0000209
210 /* parse bus-range property */
211 busrange = of_get_property(node, "bus-range", &len);
212 if (busrange == NULL || len != 8) {
Rob Herringb7c670d2017-08-21 10:16:47 -0500213 printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %pOF\n",
214 node);
Grant Likelyfbe65442009-08-25 20:07:11 +0000215 return;
216 }
217 ranges = of_get_property(node, "ranges", &len);
218 if (ranges == NULL) {
Rob Herringb7c670d2017-08-21 10:16:47 -0500219 printk(KERN_DEBUG "Can't get ranges for PCI-PCI bridge %pOF\n",
220 node);
Grant Likelyfbe65442009-08-25 20:07:11 +0000221 return;
222 }
223
Anton Blancharda795dc52013-08-07 02:01:41 +1000224 bus = pci_find_bus(pci_domain_nr(dev->bus),
225 of_read_number(busrange, 1));
Grant Likelyfbe65442009-08-25 20:07:11 +0000226 if (!bus) {
Anton Blancharda795dc52013-08-07 02:01:41 +1000227 bus = pci_add_new_bus(dev->bus, dev,
228 of_read_number(busrange, 1));
Gavin Shanab444ec2013-07-24 10:24:57 +0800229 if (!bus) {
Rob Herringb7c670d2017-08-21 10:16:47 -0500230 printk(KERN_ERR "Failed to create pci bus for %pOF\n",
231 node);
Gavin Shanab444ec2013-07-24 10:24:57 +0800232 return;
233 }
Grant Likelyfbe65442009-08-25 20:07:11 +0000234 }
235
236 bus->primary = dev->bus->number;
Anton Blancharda795dc52013-08-07 02:01:41 +1000237 pci_bus_insert_busn_res(bus, of_read_number(busrange, 1),
238 of_read_number(busrange+1, 1));
Grant Likelyfbe65442009-08-25 20:07:11 +0000239 bus->bridge_ctl = 0;
Grant Likelyfbe65442009-08-25 20:07:11 +0000240
241 /* parse ranges property */
242 /* PCI #address-cells == 3 and #size-cells == 2 always */
243 res = &dev->resource[PCI_BRIDGE_RESOURCES];
244 for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
245 res->flags = 0;
246 bus->resource[i] = res;
247 ++res;
248 }
249 i = 1;
250 for (; len >= 32; len -= 32, ranges += 8) {
Anton Blancharda795dc52013-08-07 02:01:41 +1000251 flags = pci_parse_of_flags(of_read_number(ranges, 1), 1);
Grant Likelyfbe65442009-08-25 20:07:11 +0000252 size = of_read_number(&ranges[6], 2);
253 if (flags == 0 || size == 0)
254 continue;
255 if (flags & IORESOURCE_IO) {
256 res = bus->resource[0];
257 if (res->flags) {
258 printk(KERN_ERR "PCI: ignoring extra I/O range"
Rob Herringb7c670d2017-08-21 10:16:47 -0500259 " for bridge %pOF\n", node);
Grant Likelyfbe65442009-08-25 20:07:11 +0000260 continue;
261 }
262 } else {
263 if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
264 printk(KERN_ERR "PCI: too many memory ranges"
Rob Herringb7c670d2017-08-21 10:16:47 -0500265 " for bridge %pOF\n", node);
Grant Likelyfbe65442009-08-25 20:07:11 +0000266 continue;
267 }
268 res = bus->resource[i];
269 ++i;
270 }
Grant Likelyfbe65442009-08-25 20:07:11 +0000271 res->flags = flags;
Bjorn Helgaas39aa1462012-03-16 17:48:14 -0600272 region.start = of_read_number(&ranges[1], 2);
273 region.end = region.start + size - 1;
Yinghai Lufc279852013-12-09 22:54:40 -0800274 pcibios_bus_to_resource(dev->bus, res, &region);
Grant Likelyfbe65442009-08-25 20:07:11 +0000275 }
276 sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
277 bus->number);
278 pr_debug(" bus name: %s\n", bus->name);
279
Daniel Axtens467efc22015-03-31 16:00:56 +1100280 phb = pci_bus_to_host(bus);
281
Grant Likelyfbe65442009-08-25 20:07:11 +0000282 mode = PCI_PROBE_NORMAL;
Daniel Axtens467efc22015-03-31 16:00:56 +1100283 if (phb->controller_ops.probe_mode)
284 mode = phb->controller_ops.probe_mode(bus);
Grant Likelyfbe65442009-08-25 20:07:11 +0000285 pr_debug(" probe mode: %d\n", mode);
286
287 if (mode == PCI_PROBE_DEVTREE)
288 of_scan_bus(node, bus);
289 else if (mode == PCI_PROBE_NORMAL)
290 pci_scan_child_bus(bus);
291}
292EXPORT_SYMBOL(of_scan_pci_bridge);
293
Gavin Shanab444ec2013-07-24 10:24:57 +0800294static struct pci_dev *of_scan_pci_dev(struct pci_bus *bus,
295 struct device_node *dn)
296{
297 struct pci_dev *dev = NULL;
Anton Blanchard3e7cec62013-10-17 23:19:43 +1100298 const __be32 *reg;
Gavin Shanab444ec2013-07-24 10:24:57 +0800299 int reglen, devfn;
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000300#ifdef CONFIG_EEH
Gavin Shanc6406d82015-03-17 16:15:08 +1100301 struct eeh_dev *edev = pdn_to_eeh_dev(PCI_DN(dn));
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000302#endif
Gavin Shanab444ec2013-07-24 10:24:57 +0800303
Rob Herringb7c670d2017-08-21 10:16:47 -0500304 pr_debug(" * %pOF\n", dn);
Gavin Shanab444ec2013-07-24 10:24:57 +0800305 if (!of_device_is_available(dn))
306 return NULL;
307
308 reg = of_get_property(dn, "reg", &reglen);
309 if (reg == NULL || reglen < 20)
310 return NULL;
Anton Blanchard3e7cec62013-10-17 23:19:43 +1100311 devfn = (of_read_number(reg, 1) >> 8) & 0xff;
Gavin Shanab444ec2013-07-24 10:24:57 +0800312
313 /* Check if the PCI device is already there */
314 dev = pci_get_slot(bus, devfn);
315 if (dev) {
316 pci_dev_put(dev);
317 return dev;
318 }
319
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000320 /* Device removed permanently ? */
321#ifdef CONFIG_EEH
322 if (edev && (edev->mode & EEH_DEV_REMOVED))
323 return NULL;
324#endif
325
Gavin Shanab444ec2013-07-24 10:24:57 +0800326 /* create a new pci_dev for this device */
327 dev = of_create_pci_dev(dn, bus, devfn);
328 if (!dev)
329 return NULL;
330
331 pr_debug(" dev header type: %x\n", dev->hdr_type);
332 return dev;
333}
334
Grant Likelyfbe65442009-08-25 20:07:11 +0000335/**
336 * __of_scan_bus - given a PCI bus node, setup bus and scan for child devices
337 * @node: device tree node for the PCI bus
338 * @bus: pci_bus structure for the PCI bus
339 * @rescan_existing: Flag indicating bus has already been set up
340 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800341static void __of_scan_bus(struct device_node *node, struct pci_bus *bus,
342 int rescan_existing)
Grant Likelyfbe65442009-08-25 20:07:11 +0000343{
344 struct device_node *child;
Grant Likelyfbe65442009-08-25 20:07:11 +0000345 struct pci_dev *dev;
346
Rob Herringb7c670d2017-08-21 10:16:47 -0500347 pr_debug("of_scan_bus(%pOF) bus no %d...\n",
348 node, bus->number);
Grant Likelyfbe65442009-08-25 20:07:11 +0000349
350 /* Scan direct children */
351 for_each_child_of_node(node, child) {
Gavin Shanab444ec2013-07-24 10:24:57 +0800352 dev = of_scan_pci_dev(bus, child);
Grant Likelyfbe65442009-08-25 20:07:11 +0000353 if (!dev)
354 continue;
355 pr_debug(" dev header type: %x\n", dev->hdr_type);
356 }
357
358 /* Apply all fixups necessary. We don't fixup the bus "self"
359 * for an existing bridge that is being rescanned
360 */
361 if (!rescan_existing)
362 pcibios_setup_bus_self(bus);
363 pcibios_setup_bus_devices(bus);
364
365 /* Now scan child busses */
Andy Shevchenkodd1ea572017-11-10 19:52:29 +0200366 for_each_pci_bridge(dev, bus)
367 of_scan_pci_bridge(dev);
Grant Likelyfbe65442009-08-25 20:07:11 +0000368}
369
370/**
371 * of_scan_bus - given a PCI bus node, setup bus and scan for child devices
372 * @node: device tree node for the PCI bus
373 * @bus: pci_bus structure for the PCI bus
374 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800375void of_scan_bus(struct device_node *node, struct pci_bus *bus)
Grant Likelyfbe65442009-08-25 20:07:11 +0000376{
377 __of_scan_bus(node, bus, 0);
378}
379EXPORT_SYMBOL_GPL(of_scan_bus);
380
381/**
382 * of_rescan_bus - given a PCI bus node, scan for child devices
383 * @node: device tree node for the PCI bus
384 * @bus: pci_bus structure for the PCI bus
385 *
386 * Same as of_scan_bus, but for a pci_bus structure that has already been
387 * setup.
388 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800389void of_rescan_bus(struct device_node *node, struct pci_bus *bus)
Grant Likelyfbe65442009-08-25 20:07:11 +0000390{
391 __of_scan_bus(node, bus, 1);
392}
393EXPORT_SYMBOL_GPL(of_rescan_bus);
394