blob: e0648a09d9c8c3da4fe53c3fbe4360a3e8fbf1b6 [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 */
Anton Blancharde51df2c2014-08-20 08:55:18 +100041static unsigned 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;
Alexey Kardashevskiy514670a2019-06-05 13:38:14 +100048 if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64)
49 flags |= IORESOURCE_MEM_64;
Grant Likelyfbe65442009-08-25 20:07:11 +000050 flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
51 if (addr0 & 0x40000000)
52 flags |= IORESOURCE_PREFETCH
53 | PCI_BASE_ADDRESS_MEM_PREFETCH;
54 /* Note: We don't know whether the ROM has been left enabled
55 * by the firmware or not. We mark it as disabled (ie, we do
56 * not set the IORESOURCE_ROM_ENABLE flag) for now rather than
57 * do a config space read, it will be force-enabled if needed
58 */
59 if (!bridge && (addr0 & 0xff) == 0x30)
60 flags |= IORESOURCE_READONLY;
61 } else if (addr0 & 0x01000000)
62 flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
63 if (flags)
64 flags |= IORESOURCE_SIZEALIGN;
65 return flags;
66}
67
68/**
69 * of_pci_parse_addrs - Parse PCI addresses assigned in the device tree node
70 * @node: device tree node for the PCI device
71 * @dev: pci_dev structure for the device
72 *
73 * This function parses the 'assigned-addresses' property of a PCI devices'
74 * device tree node and writes them into the associated pci_dev structure.
75 */
76static void of_pci_parse_addrs(struct device_node *node, struct pci_dev *dev)
77{
78 u64 base, size;
79 unsigned int flags;
Bjorn Helgaas39aa1462012-03-16 17:48:14 -060080 struct pci_bus_region region;
Grant Likelyfbe65442009-08-25 20:07:11 +000081 struct resource *res;
Anton Blancharda795dc52013-08-07 02:01:41 +100082 const __be32 *addrs;
Grant Likelyfbe65442009-08-25 20:07:11 +000083 u32 i;
84 int proplen;
85
86 addrs = of_get_property(node, "assigned-addresses", &proplen);
87 if (!addrs)
88 return;
89 pr_debug(" parse addresses (%d bytes) @ %p\n", proplen, addrs);
90 for (; proplen >= 20; proplen -= 20, addrs += 5) {
Anton Blancharda795dc52013-08-07 02:01:41 +100091 flags = pci_parse_of_flags(of_read_number(addrs, 1), 0);
Grant Likelyfbe65442009-08-25 20:07:11 +000092 if (!flags)
93 continue;
94 base = of_read_number(&addrs[1], 2);
95 size = of_read_number(&addrs[3], 2);
96 if (!size)
97 continue;
Anton Blancharda795dc52013-08-07 02:01:41 +100098 i = of_read_number(addrs, 1) & 0xff;
Grant Likelyfbe65442009-08-25 20:07:11 +000099 pr_debug(" base: %llx, size: %llx, i: %x\n",
100 (unsigned long long)base,
101 (unsigned long long)size, i);
102
103 if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
104 res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
105 } else if (i == dev->rom_base_reg) {
106 res = &dev->resource[PCI_ROM_RESOURCE];
Dan Williams92b19ff2015-08-10 23:07:06 -0400107 flags |= IORESOURCE_READONLY;
Grant Likelyfbe65442009-08-25 20:07:11 +0000108 } else {
109 printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
110 continue;
111 }
Grant Likelyfbe65442009-08-25 20:07:11 +0000112 res->flags = flags;
113 res->name = pci_name(dev);
Bjorn Helgaas39aa1462012-03-16 17:48:14 -0600114 region.start = base;
115 region.end = base + size - 1;
Yinghai Lufc279852013-12-09 22:54:40 -0800116 pcibios_bus_to_resource(dev->bus, res, &region);
Grant Likelyfbe65442009-08-25 20:07:11 +0000117 }
118}
119
120/**
121 * of_create_pci_dev - Given a device tree node on a pci bus, create a pci_dev
122 * @node: device tree node pointer
123 * @bus: bus the device is sitting on
124 * @devfn: PCI function number, extracted from device tree by caller.
125 */
126struct pci_dev *of_create_pci_dev(struct device_node *node,
127 struct pci_bus *bus, int devfn)
128{
129 struct pci_dev *dev;
130 const char *type;
131
Gu Zheng8b1fce02013-05-25 21:48:31 +0800132 dev = pci_alloc_dev(bus);
Grant Likelyfbe65442009-08-25 20:07:11 +0000133 if (!dev)
134 return NULL;
135 type = of_get_property(node, "device_type", NULL);
136 if (type == NULL)
137 type = "";
138
139 pr_debug(" create device, devfn: %x, type: %s\n", devfn, type);
140
Grant Likelyb5d937d2011-02-04 11:24:11 -0700141 dev->dev.of_node = of_node_get(node);
Grant Likelyfbe65442009-08-25 20:07:11 +0000142 dev->dev.parent = bus->bridge;
143 dev->dev.bus = &pci_bus_type;
144 dev->devfn = devfn;
145 dev->multifunction = 0; /* maybe a lie? */
Linus Torvalds4406c562009-09-16 07:49:54 -0700146 dev->needs_freset = 0; /* pcie fundamental reset required */
Benjamin Herrenschmidtbb209c82010-01-26 17:10:03 +0000147 set_pcie_port_type(dev);
Grant Likelyfbe65442009-08-25 20:07:11 +0000148
Yijing Wang017ffe62015-07-17 17:16:32 +0800149 pci_dev_assign_slot(dev);
Grant Likelyfbe65442009-08-25 20:07:11 +0000150 dev->vendor = get_int_prop(node, "vendor-id", 0xffff);
151 dev->device = get_int_prop(node, "device-id", 0xffff);
152 dev->subsystem_vendor = get_int_prop(node, "subsystem-vendor-id", 0);
153 dev->subsystem_device = get_int_prop(node, "subsystem-id", 0);
154
155 dev->cfg_size = pci_cfg_space_size(dev);
156
157 dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(bus),
158 dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
159 dev->class = get_int_prop(node, "class-code", 0);
160 dev->revision = get_int_prop(node, "revision-id", 0);
161
162 pr_debug(" class: 0x%x\n", dev->class);
163 pr_debug(" revision: 0x%x\n", dev->revision);
164
Bjorn Helgaasc2defb52013-05-17 15:08:50 -0600165 dev->current_state = PCI_UNKNOWN; /* unknown power state */
Grant Likelyfbe65442009-08-25 20:07:11 +0000166 dev->error_state = pci_channel_io_normal;
167 dev->dma_mask = 0xffffffff;
168
Benjamin Herrenschmidt94afc002010-01-26 17:10:05 +0000169 /* Early fixups, before probing the BARs */
170 pci_fixup_device(pci_fixup_early, dev);
171
Grant Likelyfbe65442009-08-25 20:07:11 +0000172 if (!strcmp(type, "pci") || !strcmp(type, "pciex")) {
173 /* a PCI-PCI bridge */
174 dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
175 dev->rom_base_reg = PCI_ROM_ADDRESS1;
Benjamin Herrenschmidtbb209c82010-01-26 17:10:03 +0000176 set_pcie_hotplug_bridge(dev);
Grant Likelyfbe65442009-08-25 20:07:11 +0000177 } else if (!strcmp(type, "cardbus")) {
178 dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
179 } else {
180 dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
181 dev->rom_base_reg = PCI_ROM_ADDRESS;
182 /* Maybe do a default OF mapping here */
Michael Ellermanef24ba72016-09-06 21:53:24 +1000183 dev->irq = 0;
Grant Likelyfbe65442009-08-25 20:07:11 +0000184 }
185
186 of_pci_parse_addrs(node, dev);
187
188 pr_debug(" adding to system ...\n");
189
190 pci_device_add(dev, bus);
191
192 return dev;
193}
194EXPORT_SYMBOL(of_create_pci_dev);
195
196/**
197 * of_scan_pci_bridge - Set up a PCI bridge and scan for child nodes
Grant Likelyfbe65442009-08-25 20:07:11 +0000198 * @dev: pci_dev structure for the bridge
199 *
200 * of_scan_bus() calls this routine for each PCI bridge that it finds, and
201 * this routine in turn call of_scan_bus() recusively to scan for more child
202 * devices.
203 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800204void of_scan_pci_bridge(struct pci_dev *dev)
Grant Likelyfbe65442009-08-25 20:07:11 +0000205{
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +1000206 struct device_node *node = dev->dev.of_node;
Grant Likelyfbe65442009-08-25 20:07:11 +0000207 struct pci_bus *bus;
Daniel Axtens467efc22015-03-31 16:00:56 +1100208 struct pci_controller *phb;
Anton Blancharda795dc52013-08-07 02:01:41 +1000209 const __be32 *busrange, *ranges;
Grant Likelyfbe65442009-08-25 20:07:11 +0000210 int len, i, mode;
Bjorn Helgaas39aa1462012-03-16 17:48:14 -0600211 struct pci_bus_region region;
Grant Likelyfbe65442009-08-25 20:07:11 +0000212 struct resource *res;
213 unsigned int flags;
214 u64 size;
215
216 pr_debug("of_scan_pci_bridge(%s)\n", node->full_name);
217
218 /* parse bus-range property */
219 busrange = of_get_property(node, "bus-range", &len);
220 if (busrange == NULL || len != 8) {
221 printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n",
222 node->full_name);
223 return;
224 }
225 ranges = of_get_property(node, "ranges", &len);
226 if (ranges == NULL) {
227 printk(KERN_DEBUG "Can't get ranges for PCI-PCI bridge %s\n",
228 node->full_name);
229 return;
230 }
231
Anton Blancharda795dc52013-08-07 02:01:41 +1000232 bus = pci_find_bus(pci_domain_nr(dev->bus),
233 of_read_number(busrange, 1));
Grant Likelyfbe65442009-08-25 20:07:11 +0000234 if (!bus) {
Anton Blancharda795dc52013-08-07 02:01:41 +1000235 bus = pci_add_new_bus(dev->bus, dev,
236 of_read_number(busrange, 1));
Gavin Shanab444ec2013-07-24 10:24:57 +0800237 if (!bus) {
238 printk(KERN_ERR "Failed to create pci bus for %s\n",
239 node->full_name);
240 return;
241 }
Grant Likelyfbe65442009-08-25 20:07:11 +0000242 }
243
244 bus->primary = dev->bus->number;
Anton Blancharda795dc52013-08-07 02:01:41 +1000245 pci_bus_insert_busn_res(bus, of_read_number(busrange, 1),
246 of_read_number(busrange+1, 1));
Grant Likelyfbe65442009-08-25 20:07:11 +0000247 bus->bridge_ctl = 0;
Grant Likelyfbe65442009-08-25 20:07:11 +0000248
249 /* parse ranges property */
250 /* PCI #address-cells == 3 and #size-cells == 2 always */
251 res = &dev->resource[PCI_BRIDGE_RESOURCES];
252 for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
253 res->flags = 0;
254 bus->resource[i] = res;
255 ++res;
256 }
257 i = 1;
258 for (; len >= 32; len -= 32, ranges += 8) {
Anton Blancharda795dc52013-08-07 02:01:41 +1000259 flags = pci_parse_of_flags(of_read_number(ranges, 1), 1);
Grant Likelyfbe65442009-08-25 20:07:11 +0000260 size = of_read_number(&ranges[6], 2);
261 if (flags == 0 || size == 0)
262 continue;
263 if (flags & IORESOURCE_IO) {
264 res = bus->resource[0];
265 if (res->flags) {
266 printk(KERN_ERR "PCI: ignoring extra I/O range"
267 " for bridge %s\n", node->full_name);
268 continue;
269 }
270 } else {
271 if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
272 printk(KERN_ERR "PCI: too many memory ranges"
273 " for bridge %s\n", node->full_name);
274 continue;
275 }
276 res = bus->resource[i];
277 ++i;
278 }
Grant Likelyfbe65442009-08-25 20:07:11 +0000279 res->flags = flags;
Bjorn Helgaas39aa1462012-03-16 17:48:14 -0600280 region.start = of_read_number(&ranges[1], 2);
281 region.end = region.start + size - 1;
Yinghai Lufc279852013-12-09 22:54:40 -0800282 pcibios_bus_to_resource(dev->bus, res, &region);
Grant Likelyfbe65442009-08-25 20:07:11 +0000283 }
284 sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
285 bus->number);
286 pr_debug(" bus name: %s\n", bus->name);
287
Daniel Axtens467efc22015-03-31 16:00:56 +1100288 phb = pci_bus_to_host(bus);
289
Grant Likelyfbe65442009-08-25 20:07:11 +0000290 mode = PCI_PROBE_NORMAL;
Daniel Axtens467efc22015-03-31 16:00:56 +1100291 if (phb->controller_ops.probe_mode)
292 mode = phb->controller_ops.probe_mode(bus);
Grant Likelyfbe65442009-08-25 20:07:11 +0000293 pr_debug(" probe mode: %d\n", mode);
294
295 if (mode == PCI_PROBE_DEVTREE)
296 of_scan_bus(node, bus);
297 else if (mode == PCI_PROBE_NORMAL)
298 pci_scan_child_bus(bus);
299}
300EXPORT_SYMBOL(of_scan_pci_bridge);
301
Gavin Shanab444ec2013-07-24 10:24:57 +0800302static struct pci_dev *of_scan_pci_dev(struct pci_bus *bus,
303 struct device_node *dn)
304{
305 struct pci_dev *dev = NULL;
Anton Blanchard3e7cec62013-10-17 23:19:43 +1100306 const __be32 *reg;
Gavin Shanab444ec2013-07-24 10:24:57 +0800307 int reglen, devfn;
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000308#ifdef CONFIG_EEH
Gavin Shanc6406d82015-03-17 16:15:08 +1100309 struct eeh_dev *edev = pdn_to_eeh_dev(PCI_DN(dn));
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000310#endif
Gavin Shanab444ec2013-07-24 10:24:57 +0800311
312 pr_debug(" * %s\n", dn->full_name);
313 if (!of_device_is_available(dn))
314 return NULL;
315
316 reg = of_get_property(dn, "reg", &reglen);
317 if (reg == NULL || reglen < 20)
318 return NULL;
Anton Blanchard3e7cec62013-10-17 23:19:43 +1100319 devfn = (of_read_number(reg, 1) >> 8) & 0xff;
Gavin Shanab444ec2013-07-24 10:24:57 +0800320
321 /* Check if the PCI device is already there */
322 dev = pci_get_slot(bus, devfn);
323 if (dev) {
324 pci_dev_put(dev);
325 return dev;
326 }
327
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000328 /* Device removed permanently ? */
329#ifdef CONFIG_EEH
330 if (edev && (edev->mode & EEH_DEV_REMOVED))
331 return NULL;
332#endif
333
Gavin Shanab444ec2013-07-24 10:24:57 +0800334 /* create a new pci_dev for this device */
335 dev = of_create_pci_dev(dn, bus, devfn);
336 if (!dev)
337 return NULL;
338
339 pr_debug(" dev header type: %x\n", dev->hdr_type);
340 return dev;
341}
342
Grant Likelyfbe65442009-08-25 20:07:11 +0000343/**
344 * __of_scan_bus - given a PCI bus node, setup bus and scan for child devices
345 * @node: device tree node for the PCI bus
346 * @bus: pci_bus structure for the PCI bus
347 * @rescan_existing: Flag indicating bus has already been set up
348 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800349static void __of_scan_bus(struct device_node *node, struct pci_bus *bus,
350 int rescan_existing)
Grant Likelyfbe65442009-08-25 20:07:11 +0000351{
352 struct device_node *child;
Grant Likelyfbe65442009-08-25 20:07:11 +0000353 struct pci_dev *dev;
354
Frans Pop8354be92010-02-06 07:47:20 +0000355 pr_debug("of_scan_bus(%s) bus no %d...\n",
Grant Likelyfbe65442009-08-25 20:07:11 +0000356 node->full_name, bus->number);
357
358 /* Scan direct children */
359 for_each_child_of_node(node, child) {
Gavin Shanab444ec2013-07-24 10:24:57 +0800360 dev = of_scan_pci_dev(bus, child);
Grant Likelyfbe65442009-08-25 20:07:11 +0000361 if (!dev)
362 continue;
363 pr_debug(" dev header type: %x\n", dev->hdr_type);
364 }
365
366 /* Apply all fixups necessary. We don't fixup the bus "self"
367 * for an existing bridge that is being rescanned
368 */
369 if (!rescan_existing)
370 pcibios_setup_bus_self(bus);
371 pcibios_setup_bus_devices(bus);
372
373 /* Now scan child busses */
374 list_for_each_entry(dev, &bus->devices, bus_list) {
Yijing Wangc8887702014-05-04 12:23:41 +0800375 if (pci_is_bridge(dev)) {
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +1000376 of_scan_pci_bridge(dev);
Grant Likelyfbe65442009-08-25 20:07:11 +0000377 }
378 }
379}
380
381/**
382 * of_scan_bus - given a PCI bus node, setup bus and scan for child devices
383 * @node: device tree node for the PCI bus
384 * @bus: pci_bus structure for the PCI bus
385 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800386void of_scan_bus(struct device_node *node, struct pci_bus *bus)
Grant Likelyfbe65442009-08-25 20:07:11 +0000387{
388 __of_scan_bus(node, bus, 0);
389}
390EXPORT_SYMBOL_GPL(of_scan_bus);
391
392/**
393 * of_rescan_bus - given a PCI bus node, scan for child devices
394 * @node: device tree node for the PCI bus
395 * @bus: pci_bus structure for the PCI bus
396 *
397 * Same as of_scan_bus, but for a pci_bus structure that has already been
398 * setup.
399 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800400void of_rescan_bus(struct device_node *node, struct pci_bus *bus)
Grant Likelyfbe65442009-08-25 20:07:11 +0000401{
402 __of_scan_bus(node, bus, 1);
403}
404EXPORT_SYMBOL_GPL(of_rescan_bus);
405