blob: 24191ea2d9a78393d9f7ad26da0daabc0eb91ca1 [file] [log] [blame]
Grant Likelyfbe65442009-08-25 20:07:11 +00001/*
2 * Helper routines to scan the device tree for PCI devices and busses
3 *
4 * Migrated out of PowerPC architecture pci_64.c file by Grant Likely
5 * <grant.likely@secretlab.ca> so that these routines are available for
6 * 32 bit also.
7 *
8 * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
9 * Rework, based on alpha PCI code.
10 * Copyright (c) 2009 Secret Lab Technologies Ltd.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * version 2 as published by the Free Software Foundation.
15 */
16
17#include <linux/pci.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040018#include <linux/export.h>
Grant Likelyfbe65442009-08-25 20:07:11 +000019#include <asm/pci-bridge.h>
20#include <asm/prom.h>
21
22/**
23 * get_int_prop - Decode a u32 from a device tree property
24 */
25static u32 get_int_prop(struct device_node *np, const char *name, u32 def)
26{
Anton Blancharda795dc52013-08-07 02:01:41 +100027 const __be32 *prop;
Grant Likelyfbe65442009-08-25 20:07:11 +000028 int len;
29
30 prop = of_get_property(np, name, &len);
31 if (prop && len >= 4)
Anton Blancharda795dc52013-08-07 02:01:41 +100032 return of_read_number(prop, 1);
Grant Likelyfbe65442009-08-25 20:07:11 +000033 return def;
34}
35
36/**
37 * pci_parse_of_flags - Parse the flags cell of a device tree PCI address
38 * @addr0: value of 1st cell of a device tree PCI address.
39 * @bridge: Set this flag if the address is from a bridge 'ranges' property
40 */
Bryant G. Lyfc5f6222018-01-05 10:45:52 -060041unsigned int pci_parse_of_flags(u32 addr0, int bridge)
Grant Likelyfbe65442009-08-25 20:07:11 +000042{
43 unsigned int flags = 0;
44
45 if (addr0 & 0x02000000) {
46 flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
47 flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
48 flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
49 if (addr0 & 0x40000000)
50 flags |= IORESOURCE_PREFETCH
51 | PCI_BASE_ADDRESS_MEM_PREFETCH;
52 /* Note: We don't know whether the ROM has been left enabled
53 * by the firmware or not. We mark it as disabled (ie, we do
54 * not set the IORESOURCE_ROM_ENABLE flag) for now rather than
55 * do a config space read, it will be force-enabled if needed
56 */
57 if (!bridge && (addr0 & 0xff) == 0x30)
58 flags |= IORESOURCE_READONLY;
59 } else if (addr0 & 0x01000000)
60 flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
61 if (flags)
62 flags |= IORESOURCE_SIZEALIGN;
63 return flags;
64}
65
66/**
67 * of_pci_parse_addrs - Parse PCI addresses assigned in the device tree node
68 * @node: device tree node for the PCI device
69 * @dev: pci_dev structure for the device
70 *
71 * This function parses the 'assigned-addresses' property of a PCI devices'
72 * device tree node and writes them into the associated pci_dev structure.
73 */
74static void of_pci_parse_addrs(struct device_node *node, struct pci_dev *dev)
75{
76 u64 base, size;
77 unsigned int flags;
Bjorn Helgaas39aa1462012-03-16 17:48:14 -060078 struct pci_bus_region region;
Grant Likelyfbe65442009-08-25 20:07:11 +000079 struct resource *res;
Anton Blancharda795dc52013-08-07 02:01:41 +100080 const __be32 *addrs;
Grant Likelyfbe65442009-08-25 20:07:11 +000081 u32 i;
82 int proplen;
83
84 addrs = of_get_property(node, "assigned-addresses", &proplen);
85 if (!addrs)
86 return;
87 pr_debug(" parse addresses (%d bytes) @ %p\n", proplen, addrs);
88 for (; proplen >= 20; proplen -= 20, addrs += 5) {
Anton Blancharda795dc52013-08-07 02:01:41 +100089 flags = pci_parse_of_flags(of_read_number(addrs, 1), 0);
Grant Likelyfbe65442009-08-25 20:07:11 +000090 if (!flags)
91 continue;
92 base = of_read_number(&addrs[1], 2);
93 size = of_read_number(&addrs[3], 2);
94 if (!size)
95 continue;
Anton Blancharda795dc52013-08-07 02:01:41 +100096 i = of_read_number(addrs, 1) & 0xff;
Grant Likelyfbe65442009-08-25 20:07:11 +000097 pr_debug(" base: %llx, size: %llx, i: %x\n",
98 (unsigned long long)base,
99 (unsigned long long)size, i);
100
101 if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
102 res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
103 } else if (i == dev->rom_base_reg) {
104 res = &dev->resource[PCI_ROM_RESOURCE];
Dan Williams92b19ff2015-08-10 23:07:06 -0400105 flags |= IORESOURCE_READONLY;
Grant Likelyfbe65442009-08-25 20:07:11 +0000106 } else {
107 printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
108 continue;
109 }
Grant Likelyfbe65442009-08-25 20:07:11 +0000110 res->flags = flags;
111 res->name = pci_name(dev);
Bjorn Helgaas39aa1462012-03-16 17:48:14 -0600112 region.start = base;
113 region.end = base + size - 1;
Yinghai Lufc279852013-12-09 22:54:40 -0800114 pcibios_bus_to_resource(dev->bus, res, &region);
Grant Likelyfbe65442009-08-25 20:07:11 +0000115 }
116}
117
118/**
119 * of_create_pci_dev - Given a device tree node on a pci bus, create a pci_dev
120 * @node: device tree node pointer
121 * @bus: bus the device is sitting on
122 * @devfn: PCI function number, extracted from device tree by caller.
123 */
124struct pci_dev *of_create_pci_dev(struct device_node *node,
125 struct pci_bus *bus, int devfn)
126{
127 struct pci_dev *dev;
Grant Likelyfbe65442009-08-25 20:07:11 +0000128
Gu Zheng8b1fce02013-05-25 21:48:31 +0800129 dev = pci_alloc_dev(bus);
Grant Likelyfbe65442009-08-25 20:07:11 +0000130 if (!dev)
131 return NULL;
Grant Likelyfbe65442009-08-25 20:07:11 +0000132
Rob Herringe5480bd2018-11-16 16:11:00 -0600133 pr_debug(" create device, devfn: %x, type: %s\n", devfn,
134 of_node_get_device_type(node));
Grant Likelyfbe65442009-08-25 20:07:11 +0000135
Grant Likelyb5d937d2011-02-04 11:24:11 -0700136 dev->dev.of_node = of_node_get(node);
Grant Likelyfbe65442009-08-25 20:07:11 +0000137 dev->dev.parent = bus->bridge;
138 dev->dev.bus = &pci_bus_type;
139 dev->devfn = devfn;
140 dev->multifunction = 0; /* maybe a lie? */
Linus Torvalds4406c562009-09-16 07:49:54 -0700141 dev->needs_freset = 0; /* pcie fundamental reset required */
Benjamin Herrenschmidtbb209c82010-01-26 17:10:03 +0000142 set_pcie_port_type(dev);
Grant Likelyfbe65442009-08-25 20:07:11 +0000143
Yijing Wang017ffe62015-07-17 17:16:32 +0800144 pci_dev_assign_slot(dev);
Grant Likelyfbe65442009-08-25 20:07:11 +0000145 dev->vendor = get_int_prop(node, "vendor-id", 0xffff);
146 dev->device = get_int_prop(node, "device-id", 0xffff);
147 dev->subsystem_vendor = get_int_prop(node, "subsystem-vendor-id", 0);
148 dev->subsystem_device = get_int_prop(node, "subsystem-id", 0);
149
150 dev->cfg_size = pci_cfg_space_size(dev);
151
152 dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(bus),
153 dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
154 dev->class = get_int_prop(node, "class-code", 0);
155 dev->revision = get_int_prop(node, "revision-id", 0);
156
157 pr_debug(" class: 0x%x\n", dev->class);
158 pr_debug(" revision: 0x%x\n", dev->revision);
159
Bjorn Helgaasc2defb52013-05-17 15:08:50 -0600160 dev->current_state = PCI_UNKNOWN; /* unknown power state */
Grant Likelyfbe65442009-08-25 20:07:11 +0000161 dev->error_state = pci_channel_io_normal;
162 dev->dma_mask = 0xffffffff;
163
Benjamin Herrenschmidt94afc002010-01-26 17:10:05 +0000164 /* Early fixups, before probing the BARs */
165 pci_fixup_device(pci_fixup_early, dev);
166
Rob Herringe5480bd2018-11-16 16:11:00 -0600167 if (of_node_is_type(node, "pci") || of_node_is_type(node, "pciex")) {
Grant Likelyfbe65442009-08-25 20:07:11 +0000168 /* a PCI-PCI bridge */
169 dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
170 dev->rom_base_reg = PCI_ROM_ADDRESS1;
Benjamin Herrenschmidtbb209c82010-01-26 17:10:03 +0000171 set_pcie_hotplug_bridge(dev);
Rob Herringe5480bd2018-11-16 16:11:00 -0600172 } else if (of_node_is_type(node, "cardbus")) {
Grant Likelyfbe65442009-08-25 20:07:11 +0000173 dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
174 } else {
175 dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
176 dev->rom_base_reg = PCI_ROM_ADDRESS;
177 /* Maybe do a default OF mapping here */
Michael Ellermanef24ba72016-09-06 21:53:24 +1000178 dev->irq = 0;
Grant Likelyfbe65442009-08-25 20:07:11 +0000179 }
180
181 of_pci_parse_addrs(node, dev);
182
183 pr_debug(" adding to system ...\n");
184
185 pci_device_add(dev, bus);
186
187 return dev;
188}
189EXPORT_SYMBOL(of_create_pci_dev);
190
191/**
192 * of_scan_pci_bridge - Set up a PCI bridge and scan for child nodes
Grant Likelyfbe65442009-08-25 20:07:11 +0000193 * @dev: pci_dev structure for the bridge
194 *
195 * of_scan_bus() calls this routine for each PCI bridge that it finds, and
196 * this routine in turn call of_scan_bus() recusively to scan for more child
197 * devices.
198 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800199void of_scan_pci_bridge(struct pci_dev *dev)
Grant Likelyfbe65442009-08-25 20:07:11 +0000200{
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +1000201 struct device_node *node = dev->dev.of_node;
Grant Likelyfbe65442009-08-25 20:07:11 +0000202 struct pci_bus *bus;
Daniel Axtens467efc22015-03-31 16:00:56 +1100203 struct pci_controller *phb;
Anton Blancharda795dc52013-08-07 02:01:41 +1000204 const __be32 *busrange, *ranges;
Grant Likelyfbe65442009-08-25 20:07:11 +0000205 int len, i, mode;
Bjorn Helgaas39aa1462012-03-16 17:48:14 -0600206 struct pci_bus_region region;
Grant Likelyfbe65442009-08-25 20:07:11 +0000207 struct resource *res;
208 unsigned int flags;
209 u64 size;
210
Rob Herringb7c670d2017-08-21 10:16:47 -0500211 pr_debug("of_scan_pci_bridge(%pOF)\n", node);
Grant Likelyfbe65442009-08-25 20:07:11 +0000212
213 /* parse bus-range property */
214 busrange = of_get_property(node, "bus-range", &len);
215 if (busrange == NULL || len != 8) {
Rob Herringb7c670d2017-08-21 10:16:47 -0500216 printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %pOF\n",
217 node);
Grant Likelyfbe65442009-08-25 20:07:11 +0000218 return;
219 }
220 ranges = of_get_property(node, "ranges", &len);
221 if (ranges == NULL) {
Rob Herringb7c670d2017-08-21 10:16:47 -0500222 printk(KERN_DEBUG "Can't get ranges for PCI-PCI bridge %pOF\n",
223 node);
Grant Likelyfbe65442009-08-25 20:07:11 +0000224 return;
225 }
226
Anton Blancharda795dc52013-08-07 02:01:41 +1000227 bus = pci_find_bus(pci_domain_nr(dev->bus),
228 of_read_number(busrange, 1));
Grant Likelyfbe65442009-08-25 20:07:11 +0000229 if (!bus) {
Anton Blancharda795dc52013-08-07 02:01:41 +1000230 bus = pci_add_new_bus(dev->bus, dev,
231 of_read_number(busrange, 1));
Gavin Shanab444ec2013-07-24 10:24:57 +0800232 if (!bus) {
Rob Herringb7c670d2017-08-21 10:16:47 -0500233 printk(KERN_ERR "Failed to create pci bus for %pOF\n",
234 node);
Gavin Shanab444ec2013-07-24 10:24:57 +0800235 return;
236 }
Grant Likelyfbe65442009-08-25 20:07:11 +0000237 }
238
239 bus->primary = dev->bus->number;
Anton Blancharda795dc52013-08-07 02:01:41 +1000240 pci_bus_insert_busn_res(bus, of_read_number(busrange, 1),
241 of_read_number(busrange+1, 1));
Grant Likelyfbe65442009-08-25 20:07:11 +0000242 bus->bridge_ctl = 0;
Grant Likelyfbe65442009-08-25 20:07:11 +0000243
244 /* parse ranges property */
245 /* PCI #address-cells == 3 and #size-cells == 2 always */
246 res = &dev->resource[PCI_BRIDGE_RESOURCES];
247 for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
248 res->flags = 0;
249 bus->resource[i] = res;
250 ++res;
251 }
252 i = 1;
253 for (; len >= 32; len -= 32, ranges += 8) {
Anton Blancharda795dc52013-08-07 02:01:41 +1000254 flags = pci_parse_of_flags(of_read_number(ranges, 1), 1);
Grant Likelyfbe65442009-08-25 20:07:11 +0000255 size = of_read_number(&ranges[6], 2);
256 if (flags == 0 || size == 0)
257 continue;
258 if (flags & IORESOURCE_IO) {
259 res = bus->resource[0];
260 if (res->flags) {
261 printk(KERN_ERR "PCI: ignoring extra I/O range"
Rob Herringb7c670d2017-08-21 10:16:47 -0500262 " for bridge %pOF\n", node);
Grant Likelyfbe65442009-08-25 20:07:11 +0000263 continue;
264 }
265 } else {
266 if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
267 printk(KERN_ERR "PCI: too many memory ranges"
Rob Herringb7c670d2017-08-21 10:16:47 -0500268 " for bridge %pOF\n", node);
Grant Likelyfbe65442009-08-25 20:07:11 +0000269 continue;
270 }
271 res = bus->resource[i];
272 ++i;
273 }
Grant Likelyfbe65442009-08-25 20:07:11 +0000274 res->flags = flags;
Bjorn Helgaas39aa1462012-03-16 17:48:14 -0600275 region.start = of_read_number(&ranges[1], 2);
276 region.end = region.start + size - 1;
Yinghai Lufc279852013-12-09 22:54:40 -0800277 pcibios_bus_to_resource(dev->bus, res, &region);
Grant Likelyfbe65442009-08-25 20:07:11 +0000278 }
279 sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
280 bus->number);
281 pr_debug(" bus name: %s\n", bus->name);
282
Daniel Axtens467efc22015-03-31 16:00:56 +1100283 phb = pci_bus_to_host(bus);
284
Grant Likelyfbe65442009-08-25 20:07:11 +0000285 mode = PCI_PROBE_NORMAL;
Daniel Axtens467efc22015-03-31 16:00:56 +1100286 if (phb->controller_ops.probe_mode)
287 mode = phb->controller_ops.probe_mode(bus);
Grant Likelyfbe65442009-08-25 20:07:11 +0000288 pr_debug(" probe mode: %d\n", mode);
289
290 if (mode == PCI_PROBE_DEVTREE)
291 of_scan_bus(node, bus);
292 else if (mode == PCI_PROBE_NORMAL)
293 pci_scan_child_bus(bus);
294}
295EXPORT_SYMBOL(of_scan_pci_bridge);
296
Gavin Shanab444ec2013-07-24 10:24:57 +0800297static struct pci_dev *of_scan_pci_dev(struct pci_bus *bus,
298 struct device_node *dn)
299{
300 struct pci_dev *dev = NULL;
Anton Blanchard3e7cec62013-10-17 23:19:43 +1100301 const __be32 *reg;
Gavin Shanab444ec2013-07-24 10:24:57 +0800302 int reglen, devfn;
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000303#ifdef CONFIG_EEH
Gavin Shanc6406d82015-03-17 16:15:08 +1100304 struct eeh_dev *edev = pdn_to_eeh_dev(PCI_DN(dn));
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000305#endif
Gavin Shanab444ec2013-07-24 10:24:57 +0800306
Rob Herringb7c670d2017-08-21 10:16:47 -0500307 pr_debug(" * %pOF\n", dn);
Gavin Shanab444ec2013-07-24 10:24:57 +0800308 if (!of_device_is_available(dn))
309 return NULL;
310
311 reg = of_get_property(dn, "reg", &reglen);
312 if (reg == NULL || reglen < 20)
313 return NULL;
Anton Blanchard3e7cec62013-10-17 23:19:43 +1100314 devfn = (of_read_number(reg, 1) >> 8) & 0xff;
Gavin Shanab444ec2013-07-24 10:24:57 +0800315
316 /* Check if the PCI device is already there */
317 dev = pci_get_slot(bus, devfn);
318 if (dev) {
319 pci_dev_put(dev);
320 return dev;
321 }
322
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000323 /* Device removed permanently ? */
324#ifdef CONFIG_EEH
325 if (edev && (edev->mode & EEH_DEV_REMOVED))
326 return NULL;
327#endif
328
Gavin Shanab444ec2013-07-24 10:24:57 +0800329 /* create a new pci_dev for this device */
330 dev = of_create_pci_dev(dn, bus, devfn);
331 if (!dev)
332 return NULL;
333
334 pr_debug(" dev header type: %x\n", dev->hdr_type);
335 return dev;
336}
337
Grant Likelyfbe65442009-08-25 20:07:11 +0000338/**
339 * __of_scan_bus - given a PCI bus node, setup bus and scan for child devices
340 * @node: device tree node for the PCI bus
341 * @bus: pci_bus structure for the PCI bus
342 * @rescan_existing: Flag indicating bus has already been set up
343 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800344static void __of_scan_bus(struct device_node *node, struct pci_bus *bus,
345 int rescan_existing)
Grant Likelyfbe65442009-08-25 20:07:11 +0000346{
347 struct device_node *child;
Grant Likelyfbe65442009-08-25 20:07:11 +0000348 struct pci_dev *dev;
349
Rob Herringb7c670d2017-08-21 10:16:47 -0500350 pr_debug("of_scan_bus(%pOF) bus no %d...\n",
351 node, bus->number);
Grant Likelyfbe65442009-08-25 20:07:11 +0000352
353 /* Scan direct children */
354 for_each_child_of_node(node, child) {
Gavin Shanab444ec2013-07-24 10:24:57 +0800355 dev = of_scan_pci_dev(bus, child);
Grant Likelyfbe65442009-08-25 20:07:11 +0000356 if (!dev)
357 continue;
358 pr_debug(" dev header type: %x\n", dev->hdr_type);
359 }
360
361 /* Apply all fixups necessary. We don't fixup the bus "self"
362 * for an existing bridge that is being rescanned
363 */
364 if (!rescan_existing)
365 pcibios_setup_bus_self(bus);
366 pcibios_setup_bus_devices(bus);
367
368 /* Now scan child busses */
Andy Shevchenkodd1ea572017-11-10 19:52:29 +0200369 for_each_pci_bridge(dev, bus)
370 of_scan_pci_bridge(dev);
Grant Likelyfbe65442009-08-25 20:07:11 +0000371}
372
373/**
374 * of_scan_bus - given a PCI bus node, setup bus and scan for child devices
375 * @node: device tree node for the PCI bus
376 * @bus: pci_bus structure for the PCI bus
377 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800378void of_scan_bus(struct device_node *node, struct pci_bus *bus)
Grant Likelyfbe65442009-08-25 20:07:11 +0000379{
380 __of_scan_bus(node, bus, 0);
381}
382EXPORT_SYMBOL_GPL(of_scan_bus);
383
384/**
385 * of_rescan_bus - given a PCI bus node, scan for child devices
386 * @node: device tree node for the PCI bus
387 * @bus: pci_bus structure for the PCI bus
388 *
389 * Same as of_scan_bus, but for a pci_bus structure that has already been
390 * setup.
391 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800392void of_rescan_bus(struct device_node *node, struct pci_bus *bus)
Grant Likelyfbe65442009-08-25 20:07:11 +0000393{
394 __of_scan_bus(node, bus, 1);
395}
396EXPORT_SYMBOL_GPL(of_rescan_bus);
397