blob: 96edb6f8babb161c4d3d4b8b41b5bac7d999867c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Port for PPC64 David Engebretsen, IBM Corp.
3 * Contains common pci routines for ppc64 platform, pSeries and iSeries brands.
4 *
5 * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
6 * Rework, based on alpha PCI code.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
14#undef DEBUG
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/kernel.h>
17#include <linux/pci.h>
18#include <linux/string.h>
19#include <linux/init.h>
20#include <linux/bootmem.h>
21#include <linux/mm.h>
22#include <linux/list.h>
Paul Mackerrasb2ad7b52005-09-09 23:02:36 +100023#include <linux/syscalls.h>
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -070024#include <linux/irq.h>
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +100025#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <asm/processor.h>
28#include <asm/io.h>
29#include <asm/prom.h>
30#include <asm/pci-bridge.h>
31#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/machdep.h>
Stephen Rothwelld3878992005-09-28 02:50:25 +100033#include <asm/ppc-pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035unsigned long pci_probe_only = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/* pci_io_base -- the base address from which io bars are offsets.
38 * This is the lowest I/O base address (so bar values are always positive),
39 * and it *must* be the start of ISA space if an ISA bus exists because
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +100040 * ISA drivers use hard coded offsets. If no ISA bus exists nothing
41 * is mapped on the first 64K of IO space
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 */
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +100043unsigned long pci_io_base = ISA_IO_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044EXPORT_SYMBOL(pci_io_base);
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static void fixup_broken_pcnet32(struct pci_dev* dev)
47{
48 if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
49 dev->vendor = PCI_VENDOR_ID_AMD;
50 pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 }
52}
53DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Paul Mackerras42672922005-09-12 17:17:36 +100056static u32 get_int_prop(struct device_node *np, const char *name, u32 def)
57{
Jeremy Kerra7f67bd2006-07-12 15:35:54 +100058 const u32 *prop;
Paul Mackerras42672922005-09-12 17:17:36 +100059 int len;
60
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100061 prop = of_get_property(np, name, &len);
Paul Mackerras42672922005-09-12 17:17:36 +100062 if (prop && len >= 4)
63 return *prop;
64 return def;
65}
66
Benjamin Herrenschmidtad892a62009-05-14 20:16:47 +000067static unsigned int pci_parse_of_flags(u32 addr0, int bridge)
Paul Mackerras42672922005-09-12 17:17:36 +100068{
69 unsigned int flags = 0;
70
71 if (addr0 & 0x02000000) {
Paul Mackerrasd79e7432005-09-21 14:14:22 +100072 flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
73 flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
74 flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
Paul Mackerras42672922005-09-12 17:17:36 +100075 if (addr0 & 0x40000000)
Paul Mackerrasd79e7432005-09-21 14:14:22 +100076 flags |= IORESOURCE_PREFETCH
77 | PCI_BASE_ADDRESS_MEM_PREFETCH;
Benjamin Herrenschmidtad892a62009-05-14 20:16:47 +000078 /* Note: We don't know whether the ROM has been left enabled
79 * by the firmware or not. We mark it as disabled (ie, we do
80 * not set the IORESOURCE_ROM_ENABLE flag) for now rather than
81 * do a config space read, it will be force-enabled if needed
82 */
83 if (!bridge && (addr0 & 0xff) == 0x30)
84 flags |= IORESOURCE_READONLY;
Paul Mackerras42672922005-09-12 17:17:36 +100085 } else if (addr0 & 0x01000000)
Paul Mackerrasd79e7432005-09-21 14:14:22 +100086 flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
Benjamin Herrenschmidtad892a62009-05-14 20:16:47 +000087 if (flags)
88 flags |= IORESOURCE_SIZEALIGN;
Paul Mackerras42672922005-09-12 17:17:36 +100089 return flags;
90}
91
Paul Mackerras42672922005-09-12 17:17:36 +100092
93static void pci_parse_of_addrs(struct device_node *node, struct pci_dev *dev)
94{
95 u64 base, size;
96 unsigned int flags;
97 struct resource *res;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +100098 const u32 *addrs;
99 u32 i;
Paul Mackerras42672922005-09-12 17:17:36 +1000100 int proplen;
101
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000102 addrs = of_get_property(node, "assigned-addresses", &proplen);
Paul Mackerras42672922005-09-12 17:17:36 +1000103 if (!addrs)
104 return;
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000105 pr_debug(" parse addresses (%d bytes) @ %p\n", proplen, addrs);
Paul Mackerras42672922005-09-12 17:17:36 +1000106 for (; proplen >= 20; proplen -= 20, addrs += 5) {
Benjamin Herrenschmidtad892a62009-05-14 20:16:47 +0000107 flags = pci_parse_of_flags(addrs[0], 0);
Paul Mackerras42672922005-09-12 17:17:36 +1000108 if (!flags)
109 continue;
Jon Loeliger327e22d2007-06-04 14:28:44 -0500110 base = of_read_number(&addrs[1], 2);
111 size = of_read_number(&addrs[3], 2);
Paul Mackerras42672922005-09-12 17:17:36 +1000112 if (!size)
113 continue;
114 i = addrs[0] & 0xff;
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000115 pr_debug(" base: %llx, size: %llx, i: %x\n",
116 (unsigned long long)base,
117 (unsigned long long)size, i);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100118
Paul Mackerras42672922005-09-12 17:17:36 +1000119 if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
120 res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
121 } else if (i == dev->rom_base_reg) {
122 res = &dev->resource[PCI_ROM_RESOURCE];
123 flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
124 } else {
125 printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
126 continue;
127 }
128 res->start = base;
129 res->end = base + size - 1;
130 res->flags = flags;
131 res->name = pci_name(dev);
Paul Mackerras42672922005-09-12 17:17:36 +1000132 }
133}
134
John Roseead83712005-11-04 15:30:56 -0600135struct pci_dev *of_create_pci_dev(struct device_node *node,
136 struct pci_bus *bus, int devfn)
Paul Mackerras42672922005-09-12 17:17:36 +1000137{
138 struct pci_dev *dev;
139 const char *type;
140
Michael Ellermanbab41e92007-04-05 17:19:09 +1000141 dev = alloc_pci_dev();
Paul Mackerras42672922005-09-12 17:17:36 +1000142 if (!dev)
143 return NULL;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000144 type = of_get_property(node, "device_type", NULL);
Paul Mackerras42672922005-09-12 17:17:36 +1000145 if (type == NULL)
146 type = "";
147
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000148 pr_debug(" create device, devfn: %x, type: %s\n", devfn, type);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100149
Paul Mackerras42672922005-09-12 17:17:36 +1000150 dev->bus = bus;
151 dev->sysdata = node;
152 dev->dev.parent = bus->bridge;
153 dev->dev.bus = &pci_bus_type;
154 dev->devfn = devfn;
155 dev->multifunction = 0; /* maybe a lie? */
156
157 dev->vendor = get_int_prop(node, "vendor-id", 0xffff);
158 dev->device = get_int_prop(node, "device-id", 0xffff);
159 dev->subsystem_vendor = get_int_prop(node, "subsystem-vendor-id", 0);
160 dev->subsystem_device = get_int_prop(node, "subsystem-id", 0);
161
Benjamin Herrenschmidt9d17a5c2006-01-10 14:50:37 +1100162 dev->cfg_size = pci_cfg_space_size(dev);
Paul Mackerras42672922005-09-12 17:17:36 +1000163
Stephen Rothwell420b5ee2008-06-03 13:36:11 +1000164 dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(bus),
Paul Mackerras42672922005-09-12 17:17:36 +1000165 dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
166 dev->class = get_int_prop(node, "class-code", 0);
Auke Kokb8a3a522007-06-08 15:46:30 -0700167 dev->revision = get_int_prop(node, "revision-id", 0);
Paul Mackerras42672922005-09-12 17:17:36 +1000168
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000169 pr_debug(" class: 0x%x\n", dev->class);
170 pr_debug(" revision: 0x%x\n", dev->revision);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100171
Paul Mackerras42672922005-09-12 17:17:36 +1000172 dev->current_state = 4; /* unknown power state */
Linas Vepstasbb63ab12006-12-19 14:00:34 -0600173 dev->error_state = pci_channel_io_normal;
Benjamin Herrenschmidt8f2ea1f2007-08-07 08:05:10 +1000174 dev->dma_mask = 0xffffffff;
Paul Mackerras42672922005-09-12 17:17:36 +1000175
Jake Moilanenbb53bb32006-06-07 16:05:46 -0500176 if (!strcmp(type, "pci") || !strcmp(type, "pciex")) {
Paul Mackerras42672922005-09-12 17:17:36 +1000177 /* a PCI-PCI bridge */
178 dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
179 dev->rom_base_reg = PCI_ROM_ADDRESS1;
180 } else if (!strcmp(type, "cardbus")) {
181 dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
182 } else {
183 dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
184 dev->rom_base_reg = PCI_ROM_ADDRESS;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000185 /* Maybe do a default OF mapping here */
Paul Mackerras42672922005-09-12 17:17:36 +1000186 dev->irq = NO_IRQ;
Paul Mackerras42672922005-09-12 17:17:36 +1000187 }
188
189 pci_parse_of_addrs(node, dev);
190
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000191 pr_debug(" adding to system ...\n");
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100192
Paul Mackerras42672922005-09-12 17:17:36 +1000193 pci_device_add(dev, bus);
194
Paul Mackerras42672922005-09-12 17:17:36 +1000195 return dev;
196}
John Roseead83712005-11-04 15:30:56 -0600197EXPORT_SYMBOL(of_create_pci_dev);
Paul Mackerras42672922005-09-12 17:17:36 +1000198
Benjamin Herrenschmidt8b8da352008-10-27 19:48:37 +0000199static void __devinit __of_scan_bus(struct device_node *node,
200 struct pci_bus *bus, int rescan_existing)
Paul Mackerras42672922005-09-12 17:17:36 +1000201{
Stephen Rothwell85e99b92008-01-03 15:13:37 +1100202 struct device_node *child;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000203 const u32 *reg;
Paul Mackerras42672922005-09-12 17:17:36 +1000204 int reglen, devfn;
205 struct pci_dev *dev;
206
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000207 pr_debug("of_scan_bus(%s) bus no %d... \n",
208 node->full_name, bus->number);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100209
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +1100210 /* Scan direct children */
Stephen Rothwell85e99b92008-01-03 15:13:37 +1100211 for_each_child_of_node(node, child) {
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000212 pr_debug(" * %s\n", child->full_name);
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000213 reg = of_get_property(child, "reg", &reglen);
Paul Mackerras42672922005-09-12 17:17:36 +1000214 if (reg == NULL || reglen < 20)
215 continue;
216 devfn = (reg[0] >> 8) & 0xff;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100217
Paul Mackerras42672922005-09-12 17:17:36 +1000218 /* create a new pci_dev for this device */
219 dev = of_create_pci_dev(child, bus, devfn);
220 if (!dev)
221 continue;
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000222 pr_debug(" dev header type: %x\n", dev->hdr_type);
Paul Mackerras42672922005-09-12 17:17:36 +1000223 }
224
Benjamin Herrenschmidt8b8da352008-10-27 19:48:37 +0000225 /* Apply all fixups necessary. We don't fixup the bus "self"
226 * for an existing bridge that is being rescanned
227 */
228 if (!rescan_existing)
229 pcibios_setup_bus_self(bus);
230 pcibios_setup_bus_devices(bus);
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +1100231
232 /* Now scan child busses */
233 list_for_each_entry(dev, &bus->devices, bus_list) {
234 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
235 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
236 struct device_node *child = pci_device_to_OF_node(dev);
237 if (dev)
238 of_scan_pci_bridge(child, dev);
239 }
240 }
Paul Mackerras42672922005-09-12 17:17:36 +1000241}
Benjamin Herrenschmidt8b8da352008-10-27 19:48:37 +0000242
243void __devinit of_scan_bus(struct device_node *node,
244 struct pci_bus *bus)
245{
246 __of_scan_bus(node, bus, 0);
247}
248EXPORT_SYMBOL_GPL(of_scan_bus);
249
250void __devinit of_rescan_bus(struct device_node *node,
251 struct pci_bus *bus)
252{
253 __of_scan_bus(node, bus, 1);
254}
255EXPORT_SYMBOL_GPL(of_rescan_bus);
Paul Mackerras42672922005-09-12 17:17:36 +1000256
John Roseead83712005-11-04 15:30:56 -0600257void __devinit of_scan_pci_bridge(struct device_node *node,
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +1100258 struct pci_dev *dev)
Paul Mackerras42672922005-09-12 17:17:36 +1000259{
260 struct pci_bus *bus;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000261 const u32 *busrange, *ranges;
Paul Mackerras42672922005-09-12 17:17:36 +1000262 int len, i, mode;
263 struct resource *res;
264 unsigned int flags;
265 u64 size;
266
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000267 pr_debug("of_scan_pci_bridge(%s)\n", node->full_name);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100268
Paul Mackerras42672922005-09-12 17:17:36 +1000269 /* parse bus-range property */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000270 busrange = of_get_property(node, "bus-range", &len);
Paul Mackerras42672922005-09-12 17:17:36 +1000271 if (busrange == NULL || len != 8) {
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100272 printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n",
Paul Mackerras42672922005-09-12 17:17:36 +1000273 node->full_name);
274 return;
275 }
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000276 ranges = of_get_property(node, "ranges", &len);
Paul Mackerras42672922005-09-12 17:17:36 +1000277 if (ranges == NULL) {
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100278 printk(KERN_DEBUG "Can't get ranges for PCI-PCI bridge %s\n",
Paul Mackerras42672922005-09-12 17:17:36 +1000279 node->full_name);
280 return;
281 }
282
283 bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
284 if (!bus) {
285 printk(KERN_ERR "Failed to create pci bus for %s\n",
286 node->full_name);
287 return;
288 }
289
290 bus->primary = dev->bus->number;
291 bus->subordinate = busrange[1];
292 bus->bridge_ctl = 0;
293 bus->sysdata = node;
294
295 /* parse ranges property */
296 /* PCI #address-cells == 3 and #size-cells == 2 always */
297 res = &dev->resource[PCI_BRIDGE_RESOURCES];
298 for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
299 res->flags = 0;
300 bus->resource[i] = res;
301 ++res;
302 }
303 i = 1;
304 for (; len >= 32; len -= 32, ranges += 8) {
Benjamin Herrenschmidtad892a62009-05-14 20:16:47 +0000305 flags = pci_parse_of_flags(ranges[0], 1);
Jon Loeliger327e22d2007-06-04 14:28:44 -0500306 size = of_read_number(&ranges[6], 2);
Paul Mackerras42672922005-09-12 17:17:36 +1000307 if (flags == 0 || size == 0)
308 continue;
309 if (flags & IORESOURCE_IO) {
310 res = bus->resource[0];
311 if (res->flags) {
312 printk(KERN_ERR "PCI: ignoring extra I/O range"
313 " for bridge %s\n", node->full_name);
314 continue;
315 }
316 } else {
317 if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
318 printk(KERN_ERR "PCI: too many memory ranges"
319 " for bridge %s\n", node->full_name);
320 continue;
321 }
322 res = bus->resource[i];
323 ++i;
324 }
Jon Loeliger327e22d2007-06-04 14:28:44 -0500325 res->start = of_read_number(&ranges[1], 2);
Paul Mackerras42672922005-09-12 17:17:36 +1000326 res->end = res->start + size - 1;
327 res->flags = flags;
Paul Mackerras42672922005-09-12 17:17:36 +1000328 }
329 sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
330 bus->number);
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000331 pr_debug(" bus name: %s\n", bus->name);
Paul Mackerras42672922005-09-12 17:17:36 +1000332
333 mode = PCI_PROBE_NORMAL;
334 if (ppc_md.pci_probe_mode)
335 mode = ppc_md.pci_probe_mode(bus);
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000336 pr_debug(" probe mode: %d\n", mode);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100337
Paul Mackerras42672922005-09-12 17:17:36 +1000338 if (mode == PCI_PROBE_DEVTREE)
339 of_scan_bus(node, bus);
340 else if (mode == PCI_PROBE_NORMAL)
341 pci_scan_child_bus(bus);
342}
John Roseead83712005-11-04 15:30:56 -0600343EXPORT_SYMBOL(of_scan_pci_bridge);
Paul Mackerras42672922005-09-12 17:17:36 +1000344
John Roseead83712005-11-04 15:30:56 -0600345void __devinit scan_phb(struct pci_controller *hose)
Paul Mackerras42672922005-09-12 17:17:36 +1000346{
347 struct pci_bus *bus;
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100348 struct device_node *node = hose->dn;
Benjamin Herrenschmidt53280322008-10-27 19:48:29 +0000349 int mode;
Paul Mackerras42672922005-09-12 17:17:36 +1000350
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000351 pr_debug("PCI: Scanning PHB %s\n",
352 node ? node->full_name : "<NO NAME>");
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100353
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +1100354 /* Create an empty bus for the toplevel */
Benjamin Herrenschmidt803d4572006-11-11 17:25:07 +1100355 bus = pci_create_bus(hose->parent, hose->first_busno, hose->ops, node);
Paul Mackerras42672922005-09-12 17:17:36 +1000356 if (bus == NULL) {
357 printk(KERN_ERR "Failed to create bus for PCI domain %04x\n",
358 hose->global_number);
359 return;
360 }
361 bus->secondary = hose->first_busno;
362 hose->bus = bus;
363
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +1100364 /* Get some IO space for the new PHB */
Stephen Rothwell9ccc4fd2007-12-07 02:03:23 +1100365 pcibios_map_io_space(bus);
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000366
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +1100367 /* Wire up PHB bus resources */
Benjamin Herrenschmidt53280322008-10-27 19:48:29 +0000368 pcibios_setup_phb_resources(hose);
Paul Mackerras42672922005-09-12 17:17:36 +1000369
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +1100370 /* Get probe mode and perform scan */
Paul Mackerras42672922005-09-12 17:17:36 +1000371 mode = PCI_PROBE_NORMAL;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100372 if (node && ppc_md.pci_probe_mode)
Paul Mackerras42672922005-09-12 17:17:36 +1000373 mode = ppc_md.pci_probe_mode(bus);
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000374 pr_debug(" probe mode: %d\n", mode);
Paul Mackerras42672922005-09-12 17:17:36 +1000375 if (mode == PCI_PROBE_DEVTREE) {
376 bus->subordinate = hose->last_busno;
377 of_scan_bus(node, bus);
378 }
s.hauer@pengutronix.de99a565b2006-11-02 13:56:06 +0100379
Paul Mackerras42672922005-09-12 17:17:36 +1000380 if (mode == PCI_PROBE_NORMAL)
381 hose->last_busno = bus->subordinate = pci_scan_child_bus(bus);
Paul Mackerras42672922005-09-12 17:17:36 +1000382}
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384static int __init pcibios_init(void)
385{
386 struct pci_controller *hose, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +1100388 printk(KERN_INFO "PCI: Probing PCI hardware\n");
389
Benjamin Herrenschmidt53280322008-10-27 19:48:29 +0000390 /* For now, override phys_mem_access_prot. If we need it,g
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 * later, we may move that initialization to each ppc_md
392 */
393 ppc_md.phys_mem_access_prot = pci_phys_mem_access_prot;
394
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +1100395 if (pci_probe_only)
396 ppc_pci_flags |= PPC_PCI_PROBE_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Benjamin Herrenschmidt1fd0f522008-10-02 14:12:51 +0000398 /* On ppc64, we always enable PCI domains and we keep domain 0
399 * backward compatible in /proc for video cards
400 */
401 ppc_pci_flags |= PPC_PCI_ENABLE_PROC_DOMAINS | PPC_PCI_COMPAT_DOMAIN_0;
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 /* Scan all of the recorded PCI controllers. */
John Rose92eb4602006-03-14 17:46:45 -0600404 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Paul Mackerras42672922005-09-12 17:17:36 +1000405 scan_phb(hose);
John Rose92eb4602006-03-14 17:46:45 -0600406 pci_bus_add_devices(hose->bus);
407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +1100409 /* Call common code to handle resource allocation */
410 pcibios_resource_survey();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Olof Johanssone884e9c2006-04-12 15:26:59 -0500412 printk(KERN_DEBUG "PCI: Probing PCI hardware done\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 return 0;
415}
416
417subsys_initcall(pcibios_init);
418
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000419#ifdef CONFIG_HOTPLUG
420
421int pcibios_unmap_io_space(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000423 struct pci_controller *hose;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000425 WARN_ON(bus == NULL);
Benjamin Herrenschmidtde821202007-05-15 16:19:36 +1000426
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000427 /* If this is not a PHB, we only flush the hash table over
428 * the area mapped by this bridge. We don't play with the PTE
429 * mappings since we might have to deal with sub-page alignemnts
430 * so flushing the hash table is the only sane way to make sure
431 * that no hash entries are covering that removed bridge area
432 * while still allowing other busses overlapping those pages
Benjamin Herrenschmidtde821202007-05-15 16:19:36 +1000433 */
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000434 if (bus->self) {
435 struct resource *res = bus->resource[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000437 pr_debug("IO unmapping for PCI-PCI bridge %s\n",
438 pci_name(bus->self));
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000439
440 __flush_hash_table_range(&init_mm, res->start + _IO_BASE,
Benjamin Herrenschmidtb30115e2008-10-27 19:48:47 +0000441 res->end + _IO_BASE + 1);
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000442 return 0;
443 }
444
445 /* Get the host bridge */
446 hose = pci_bus_to_host(bus);
447
448 /* Check if we have IOs allocated */
449 if (hose->io_base_alloc == 0)
450 return 0;
451
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000452 pr_debug("IO unmapping for PHB %s\n", hose->dn->full_name);
453 pr_debug(" alloc=0x%p\n", hose->io_base_alloc);
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000454
455 /* This is a PHB, we fully unmap the IO area */
456 vunmap(hose->io_base_alloc);
457
458 return 0;
459}
460EXPORT_SYMBOL_GPL(pcibios_unmap_io_space);
461
462#endif /* CONFIG_HOTPLUG */
463
464int __devinit pcibios_map_io_space(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000466 struct vm_struct *area;
467 unsigned long phys_page;
468 unsigned long size_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 unsigned long io_virt_offset;
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000470 struct pci_controller *hose;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000472 WARN_ON(bus == NULL);
Benjamin Herrenschmidtde821202007-05-15 16:19:36 +1000473
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000474 /* If this not a PHB, nothing to do, page tables still exist and
475 * thus HPTEs will be faulted in when needed
476 */
477 if (bus->self) {
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000478 pr_debug("IO mapping for PCI-PCI bridge %s\n",
479 pci_name(bus->self));
Stephen Rothwell9477e452009-01-06 14:27:38 +0000480 pr_debug(" virt=0x%016llx...0x%016llx\n",
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000481 bus->resource[0]->start + _IO_BASE,
482 bus->resource[0]->end + _IO_BASE);
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000483 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
485
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000486 /* Get the host bridge */
487 hose = pci_bus_to_host(bus);
488 phys_page = _ALIGN_DOWN(hose->io_base_phys, PAGE_SIZE);
489 size_page = _ALIGN_UP(hose->pci_io_size, PAGE_SIZE);
490
491 /* Make sure IO area address is clear */
492 hose->io_base_alloc = NULL;
493
494 /* If there's no IO to map on that bus, get away too */
495 if (hose->pci_io_size == 0 || hose->io_base_phys == 0)
496 return 0;
497
498 /* Let's allocate some IO space for that guy. We don't pass
499 * VM_IOREMAP because we don't care about alignment tricks that
500 * the core does in that case. Maybe we should due to stupid card
501 * with incomplete address decoding but I'd rather not deal with
502 * those outside of the reserved 64K legacy region.
503 */
504 area = __get_vm_area(size_page, 0, PHB_IO_BASE, PHB_IO_END);
505 if (area == NULL)
506 return -ENOMEM;
507 hose->io_base_alloc = area->addr;
508 hose->io_base_virt = (void __iomem *)(area->addr +
509 hose->io_base_phys - phys_page);
510
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000511 pr_debug("IO mapping for PHB %s\n", hose->dn->full_name);
Stephen Rothwell9477e452009-01-06 14:27:38 +0000512 pr_debug(" phys=0x%016llx, virt=0x%p (alloc=0x%p)\n",
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000513 hose->io_base_phys, hose->io_base_virt, hose->io_base_alloc);
514 pr_debug(" size=0x%016lx (alloc=0x%016lx)\n",
515 hose->pci_io_size, size_page);
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000516
517 /* Establish the mapping */
518 if (__ioremap_at(phys_page, area->addr, size_page,
519 _PAGE_NO_CACHE | _PAGE_GUARDED) == NULL)
520 return -ENOMEM;
521
522 /* Fixup hose IO resource */
523 io_virt_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
524 hose->io_resource.start += io_virt_offset;
525 hose->io_resource.end += io_virt_offset;
526
Stephen Rothwell9477e452009-01-06 14:27:38 +0000527 pr_debug(" hose->io_resource=0x%016llx...0x%016llx\n",
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000528 hose->io_resource.start, hose->io_resource.end);
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return 0;
531}
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000532EXPORT_SYMBOL_GPL(pcibios_map_io_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Paul Mackerrasb2ad7b52005-09-09 23:02:36 +1000534#define IOBASE_BRIDGE_NUMBER 0
535#define IOBASE_MEMORY 1
536#define IOBASE_IO 2
537#define IOBASE_ISA_IO 3
538#define IOBASE_ISA_MEM 4
539
540long sys_pciconfig_iobase(long which, unsigned long in_bus,
541 unsigned long in_devfn)
542{
543 struct pci_controller* hose;
544 struct list_head *ln;
545 struct pci_bus *bus = NULL;
546 struct device_node *hose_node;
547
548 /* Argh ! Please forgive me for that hack, but that's the
549 * simplest way to get existing XFree to not lockup on some
550 * G5 machines... So when something asks for bus 0 io base
551 * (bus 0 is HT root), we return the AGP one instead.
552 */
Paul Mackerras16124f12008-12-28 14:12:57 +0000553 if (in_bus == 0 && machine_is_compatible("MacRISC4")) {
554 struct device_node *agp;
555
556 agp = of_find_compatible_node(NULL, NULL, "u3-agp");
557 if (agp)
Paul Mackerrasb2ad7b52005-09-09 23:02:36 +1000558 in_bus = 0xf0;
Paul Mackerras16124f12008-12-28 14:12:57 +0000559 of_node_put(agp);
560 }
Paul Mackerrasb2ad7b52005-09-09 23:02:36 +1000561
562 /* That syscall isn't quite compatible with PCI domains, but it's
563 * used on pre-domains setup. We return the first match
564 */
565
566 for (ln = pci_root_buses.next; ln != &pci_root_buses; ln = ln->next) {
567 bus = pci_bus_b(ln);
Benjamin Herrenschmidt545da942007-01-28 07:45:53 +1100568 if (in_bus >= bus->number && in_bus <= bus->subordinate)
Paul Mackerrasb2ad7b52005-09-09 23:02:36 +1000569 break;
570 bus = NULL;
571 }
572 if (bus == NULL || bus->sysdata == NULL)
573 return -ENODEV;
574
575 hose_node = (struct device_node *)bus->sysdata;
576 hose = PCI_DN(hose_node)->phb;
577
578 switch (which) {
579 case IOBASE_BRIDGE_NUMBER:
580 return (long)hose->first_busno;
581 case IOBASE_MEMORY:
582 return (long)hose->pci_mem_offset;
583 case IOBASE_IO:
584 return (long)hose->io_base_phys;
585 case IOBASE_ISA_IO:
586 return (long)isa_io_base;
587 case IOBASE_ISA_MEM:
588 return -EINVAL;
589 }
590
591 return -EOPNOTSUPP;
592}
Anton Blanchard357518f2006-06-10 20:53:06 +1000593
594#ifdef CONFIG_NUMA
595int pcibus_to_node(struct pci_bus *bus)
596{
597 struct pci_controller *phb = pci_bus_to_host(bus);
598 return phb->node;
599}
600EXPORT_SYMBOL(pcibus_to_node);
601#endif