blob: 2ec040b314d4765b258d83145fd035f5850474c1 [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
35#ifdef DEBUG
Michael Ellermanf9e4ec52005-11-15 15:16:38 +110036#include <asm/udbg.h>
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +110037#define DBG(fmt...) printk(fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#else
39#define DBG(fmt...)
40#endif
41
42unsigned long pci_probe_only = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/* pci_io_base -- the base address from which io bars are offsets.
45 * This is the lowest I/O base address (so bar values are always positive),
46 * and it *must* be the start of ISA space if an ISA bus exists because
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +100047 * ISA drivers use hard coded offsets. If no ISA bus exists nothing
48 * is mapped on the first 64K of IO space
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 */
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +100050unsigned long pci_io_base = ISA_IO_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051EXPORT_SYMBOL(pci_io_base);
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053LIST_HEAD(hose_list);
54
Stephen Rothwell57190702007-03-04 17:02:41 +110055static struct dma_mapping_ops *pci_dma_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Stephen Rothwell98747772007-03-04 16:58:39 +110057void set_pci_dma_ops(struct dma_mapping_ops *dma_ops)
58{
59 pci_dma_ops = dma_ops;
60}
61
Stephen Rothwell57190702007-03-04 17:02:41 +110062struct dma_mapping_ops *get_pci_dma_ops(void)
63{
64 return pci_dma_ops;
65}
66EXPORT_SYMBOL(get_pci_dma_ops);
67
Michael Ellerman84631f32007-12-17 17:35:53 +110068
69int pci_set_dma_mask(struct pci_dev *dev, u64 mask)
70{
71 return dma_set_mask(&dev->dev, mask);
72}
73
74int pci_set_consistent_dma_mask(struct pci_dev *dev, u64 mask)
75{
76 int rc;
77
78 rc = dma_set_mask(&dev->dev, mask);
79 dev->dev.coherent_dma_mask = dev->dma_mask;
80
81 return rc;
82}
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static void fixup_broken_pcnet32(struct pci_dev* dev)
85{
86 if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
87 dev->vendor = PCI_VENDOR_ID_AMD;
88 pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
90}
91DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Paul Mackerras42672922005-09-12 17:17:36 +100094static u32 get_int_prop(struct device_node *np, const char *name, u32 def)
95{
Jeremy Kerra7f67bd2006-07-12 15:35:54 +100096 const u32 *prop;
Paul Mackerras42672922005-09-12 17:17:36 +100097 int len;
98
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100099 prop = of_get_property(np, name, &len);
Paul Mackerras42672922005-09-12 17:17:36 +1000100 if (prop && len >= 4)
101 return *prop;
102 return def;
103}
104
105static unsigned int pci_parse_of_flags(u32 addr0)
106{
107 unsigned int flags = 0;
108
109 if (addr0 & 0x02000000) {
Paul Mackerrasd79e7432005-09-21 14:14:22 +1000110 flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
111 flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
112 flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
Paul Mackerras42672922005-09-12 17:17:36 +1000113 if (addr0 & 0x40000000)
Paul Mackerrasd79e7432005-09-21 14:14:22 +1000114 flags |= IORESOURCE_PREFETCH
115 | PCI_BASE_ADDRESS_MEM_PREFETCH;
Paul Mackerras42672922005-09-12 17:17:36 +1000116 } else if (addr0 & 0x01000000)
Paul Mackerrasd79e7432005-09-21 14:14:22 +1000117 flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
Paul Mackerras42672922005-09-12 17:17:36 +1000118 return flags;
119}
120
Paul Mackerras42672922005-09-12 17:17:36 +1000121
122static void pci_parse_of_addrs(struct device_node *node, struct pci_dev *dev)
123{
124 u64 base, size;
125 unsigned int flags;
126 struct resource *res;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000127 const u32 *addrs;
128 u32 i;
Paul Mackerras42672922005-09-12 17:17:36 +1000129 int proplen;
130
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000131 addrs = of_get_property(node, "assigned-addresses", &proplen);
Paul Mackerras42672922005-09-12 17:17:36 +1000132 if (!addrs)
133 return;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100134 DBG(" parse addresses (%d bytes) @ %p\n", proplen, addrs);
Paul Mackerras42672922005-09-12 17:17:36 +1000135 for (; proplen >= 20; proplen -= 20, addrs += 5) {
136 flags = pci_parse_of_flags(addrs[0]);
137 if (!flags)
138 continue;
Jon Loeliger327e22d2007-06-04 14:28:44 -0500139 base = of_read_number(&addrs[1], 2);
140 size = of_read_number(&addrs[3], 2);
Paul Mackerras42672922005-09-12 17:17:36 +1000141 if (!size)
142 continue;
143 i = addrs[0] & 0xff;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100144 DBG(" base: %llx, size: %llx, i: %x\n",
145 (unsigned long long)base, (unsigned long long)size, i);
146
Paul Mackerras42672922005-09-12 17:17:36 +1000147 if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
148 res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
149 } else if (i == dev->rom_base_reg) {
150 res = &dev->resource[PCI_ROM_RESOURCE];
151 flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
152 } else {
153 printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
154 continue;
155 }
156 res->start = base;
157 res->end = base + size - 1;
158 res->flags = flags;
159 res->name = pci_name(dev);
Paul Mackerras42672922005-09-12 17:17:36 +1000160 }
161}
162
John Roseead83712005-11-04 15:30:56 -0600163struct pci_dev *of_create_pci_dev(struct device_node *node,
164 struct pci_bus *bus, int devfn)
Paul Mackerras42672922005-09-12 17:17:36 +1000165{
166 struct pci_dev *dev;
167 const char *type;
168
Michael Ellermanbab41e92007-04-05 17:19:09 +1000169 dev = alloc_pci_dev();
Paul Mackerras42672922005-09-12 17:17:36 +1000170 if (!dev)
171 return NULL;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000172 type = of_get_property(node, "device_type", NULL);
Paul Mackerras42672922005-09-12 17:17:36 +1000173 if (type == NULL)
174 type = "";
175
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100176 DBG(" create device, devfn: %x, type: %s\n", devfn, type);
177
Paul Mackerras42672922005-09-12 17:17:36 +1000178 dev->bus = bus;
179 dev->sysdata = node;
180 dev->dev.parent = bus->bridge;
181 dev->dev.bus = &pci_bus_type;
182 dev->devfn = devfn;
183 dev->multifunction = 0; /* maybe a lie? */
184
185 dev->vendor = get_int_prop(node, "vendor-id", 0xffff);
186 dev->device = get_int_prop(node, "device-id", 0xffff);
187 dev->subsystem_vendor = get_int_prop(node, "subsystem-vendor-id", 0);
188 dev->subsystem_device = get_int_prop(node, "subsystem-id", 0);
189
Benjamin Herrenschmidt9d17a5c2006-01-10 14:50:37 +1100190 dev->cfg_size = pci_cfg_space_size(dev);
Paul Mackerras42672922005-09-12 17:17:36 +1000191
192 sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus),
193 dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
194 dev->class = get_int_prop(node, "class-code", 0);
Auke Kokb8a3a522007-06-08 15:46:30 -0700195 dev->revision = get_int_prop(node, "revision-id", 0);
Paul Mackerras42672922005-09-12 17:17:36 +1000196
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100197 DBG(" class: 0x%x\n", dev->class);
Auke Kokb8a3a522007-06-08 15:46:30 -0700198 DBG(" revision: 0x%x\n", dev->revision);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100199
Paul Mackerras42672922005-09-12 17:17:36 +1000200 dev->current_state = 4; /* unknown power state */
Linas Vepstasbb63ab12006-12-19 14:00:34 -0600201 dev->error_state = pci_channel_io_normal;
Benjamin Herrenschmidt8f2ea1f2007-08-07 08:05:10 +1000202 dev->dma_mask = 0xffffffff;
Paul Mackerras42672922005-09-12 17:17:36 +1000203
Jake Moilanenbb53bb32006-06-07 16:05:46 -0500204 if (!strcmp(type, "pci") || !strcmp(type, "pciex")) {
Paul Mackerras42672922005-09-12 17:17:36 +1000205 /* a PCI-PCI bridge */
206 dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
207 dev->rom_base_reg = PCI_ROM_ADDRESS1;
208 } else if (!strcmp(type, "cardbus")) {
209 dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
210 } else {
211 dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
212 dev->rom_base_reg = PCI_ROM_ADDRESS;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000213 /* Maybe do a default OF mapping here */
Paul Mackerras42672922005-09-12 17:17:36 +1000214 dev->irq = NO_IRQ;
Paul Mackerras42672922005-09-12 17:17:36 +1000215 }
216
217 pci_parse_of_addrs(node, dev);
218
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100219 DBG(" adding to system ...\n");
220
Paul Mackerras42672922005-09-12 17:17:36 +1000221 pci_device_add(dev, bus);
222
Paul Mackerras42672922005-09-12 17:17:36 +1000223 return dev;
224}
John Roseead83712005-11-04 15:30:56 -0600225EXPORT_SYMBOL(of_create_pci_dev);
Paul Mackerras42672922005-09-12 17:17:36 +1000226
John Roseead83712005-11-04 15:30:56 -0600227void __devinit of_scan_bus(struct device_node *node,
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +1100228 struct pci_bus *bus)
Paul Mackerras42672922005-09-12 17:17:36 +1000229{
230 struct device_node *child = NULL;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000231 const u32 *reg;
Paul Mackerras42672922005-09-12 17:17:36 +1000232 int reglen, devfn;
233 struct pci_dev *dev;
234
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100235 DBG("of_scan_bus(%s) bus no %d... \n", node->full_name, bus->number);
236
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +1100237 /* Scan direct children */
Paul Mackerras42672922005-09-12 17:17:36 +1000238 while ((child = of_get_next_child(node, child)) != NULL) {
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100239 DBG(" * %s\n", child->full_name);
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000240 reg = of_get_property(child, "reg", &reglen);
Paul Mackerras42672922005-09-12 17:17:36 +1000241 if (reg == NULL || reglen < 20)
242 continue;
243 devfn = (reg[0] >> 8) & 0xff;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100244
Paul Mackerras42672922005-09-12 17:17:36 +1000245 /* create a new pci_dev for this device */
246 dev = of_create_pci_dev(child, bus, devfn);
247 if (!dev)
248 continue;
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +1100249 DBG(" dev header type: %x\n", dev->hdr_type);
Paul Mackerras42672922005-09-12 17:17:36 +1000250 }
251
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +1100252 /* Ally all fixups */
253 pcibios_fixup_of_probed_bus(bus);
254
255 /* Now scan child busses */
256 list_for_each_entry(dev, &bus->devices, bus_list) {
257 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
258 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
259 struct device_node *child = pci_device_to_OF_node(dev);
260 if (dev)
261 of_scan_pci_bridge(child, dev);
262 }
263 }
Paul Mackerras42672922005-09-12 17:17:36 +1000264}
John Roseead83712005-11-04 15:30:56 -0600265EXPORT_SYMBOL(of_scan_bus);
Paul Mackerras42672922005-09-12 17:17:36 +1000266
John Roseead83712005-11-04 15:30:56 -0600267void __devinit of_scan_pci_bridge(struct device_node *node,
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +1100268 struct pci_dev *dev)
Paul Mackerras42672922005-09-12 17:17:36 +1000269{
270 struct pci_bus *bus;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000271 const u32 *busrange, *ranges;
Paul Mackerras42672922005-09-12 17:17:36 +1000272 int len, i, mode;
273 struct resource *res;
274 unsigned int flags;
275 u64 size;
276
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100277 DBG("of_scan_pci_bridge(%s)\n", node->full_name);
278
Paul Mackerras42672922005-09-12 17:17:36 +1000279 /* parse bus-range property */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000280 busrange = of_get_property(node, "bus-range", &len);
Paul Mackerras42672922005-09-12 17:17:36 +1000281 if (busrange == NULL || len != 8) {
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100282 printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n",
Paul Mackerras42672922005-09-12 17:17:36 +1000283 node->full_name);
284 return;
285 }
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000286 ranges = of_get_property(node, "ranges", &len);
Paul Mackerras42672922005-09-12 17:17:36 +1000287 if (ranges == NULL) {
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100288 printk(KERN_DEBUG "Can't get ranges for PCI-PCI bridge %s\n",
Paul Mackerras42672922005-09-12 17:17:36 +1000289 node->full_name);
290 return;
291 }
292
293 bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
294 if (!bus) {
295 printk(KERN_ERR "Failed to create pci bus for %s\n",
296 node->full_name);
297 return;
298 }
299
300 bus->primary = dev->bus->number;
301 bus->subordinate = busrange[1];
302 bus->bridge_ctl = 0;
303 bus->sysdata = node;
304
305 /* parse ranges property */
306 /* PCI #address-cells == 3 and #size-cells == 2 always */
307 res = &dev->resource[PCI_BRIDGE_RESOURCES];
308 for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
309 res->flags = 0;
310 bus->resource[i] = res;
311 ++res;
312 }
313 i = 1;
314 for (; len >= 32; len -= 32, ranges += 8) {
315 flags = pci_parse_of_flags(ranges[0]);
Jon Loeliger327e22d2007-06-04 14:28:44 -0500316 size = of_read_number(&ranges[6], 2);
Paul Mackerras42672922005-09-12 17:17:36 +1000317 if (flags == 0 || size == 0)
318 continue;
319 if (flags & IORESOURCE_IO) {
320 res = bus->resource[0];
321 if (res->flags) {
322 printk(KERN_ERR "PCI: ignoring extra I/O range"
323 " for bridge %s\n", node->full_name);
324 continue;
325 }
326 } else {
327 if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
328 printk(KERN_ERR "PCI: too many memory ranges"
329 " for bridge %s\n", node->full_name);
330 continue;
331 }
332 res = bus->resource[i];
333 ++i;
334 }
Jon Loeliger327e22d2007-06-04 14:28:44 -0500335 res->start = of_read_number(&ranges[1], 2);
Paul Mackerras42672922005-09-12 17:17:36 +1000336 res->end = res->start + size - 1;
337 res->flags = flags;
Paul Mackerras42672922005-09-12 17:17:36 +1000338 }
339 sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
340 bus->number);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100341 DBG(" bus name: %s\n", bus->name);
Paul Mackerras42672922005-09-12 17:17:36 +1000342
343 mode = PCI_PROBE_NORMAL;
344 if (ppc_md.pci_probe_mode)
345 mode = ppc_md.pci_probe_mode(bus);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100346 DBG(" probe mode: %d\n", mode);
347
Paul Mackerras42672922005-09-12 17:17:36 +1000348 if (mode == PCI_PROBE_DEVTREE)
349 of_scan_bus(node, bus);
350 else if (mode == PCI_PROBE_NORMAL)
351 pci_scan_child_bus(bus);
352}
John Roseead83712005-11-04 15:30:56 -0600353EXPORT_SYMBOL(of_scan_pci_bridge);
Paul Mackerras42672922005-09-12 17:17:36 +1000354
John Roseead83712005-11-04 15:30:56 -0600355void __devinit scan_phb(struct pci_controller *hose)
Paul Mackerras42672922005-09-12 17:17:36 +1000356{
357 struct pci_bus *bus;
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100358 struct device_node *node = hose->dn;
Paul Mackerras42672922005-09-12 17:17:36 +1000359 int i, mode;
360 struct resource *res;
361
Benjamin Herrenschmidt50c9bc22007-12-20 14:54:55 +1100362 DBG("PCI: Scanning PHB %s\n", node ? node->full_name : "<NO NAME>");
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100363
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +1100364 /* Create an empty bus for the toplevel */
Benjamin Herrenschmidt803d4572006-11-11 17:25:07 +1100365 bus = pci_create_bus(hose->parent, hose->first_busno, hose->ops, node);
Paul Mackerras42672922005-09-12 17:17:36 +1000366 if (bus == NULL) {
367 printk(KERN_ERR "Failed to create bus for PCI domain %04x\n",
368 hose->global_number);
369 return;
370 }
371 bus->secondary = hose->first_busno;
372 hose->bus = bus;
373
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +1100374 /* Get some IO space for the new PHB */
Stephen Rothwell9ccc4fd2007-12-07 02:03:23 +1100375 pcibios_map_io_space(bus);
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000376
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +1100377 /* Wire up PHB bus resources */
Benjamin Herrenschmidt50c9bc22007-12-20 14:54:55 +1100378 if (hose->io_resource.flags) {
379 DBG("PCI: PHB IO resource = %016lx-%016lx [%lx]\n",
380 hose->io_resource.start, hose->io_resource.end,
381 hose->io_resource.flags);
382 bus->resource[0] = res = &hose->io_resource;
383 }
384 for (i = 0; i < 3; ++i) {
385 DBG("PCI: PHB MEM resource %d = %016lx-%016lx [%lx]\n", i,
386 hose->mem_resources[i].start,
387 hose->mem_resources[i].end,
388 hose->mem_resources[i].flags);
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +1100389 bus->resource[i+1] = &hose->mem_resources[i];
Benjamin Herrenschmidt50c9bc22007-12-20 14:54:55 +1100390 }
391 DBG("PCI: PHB MEM offset = %016lx\n", hose->pci_mem_offset);
392 DBG("PCI: PHB IO offset = %08lx\n",
393 (unsigned long)hose->io_base_virt - _IO_BASE);
Paul Mackerras42672922005-09-12 17:17:36 +1000394
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +1100395 /* Get probe mode and perform scan */
Paul Mackerras42672922005-09-12 17:17:36 +1000396 mode = PCI_PROBE_NORMAL;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100397 if (node && ppc_md.pci_probe_mode)
Paul Mackerras42672922005-09-12 17:17:36 +1000398 mode = ppc_md.pci_probe_mode(bus);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100399 DBG(" probe mode: %d\n", mode);
Paul Mackerras42672922005-09-12 17:17:36 +1000400 if (mode == PCI_PROBE_DEVTREE) {
401 bus->subordinate = hose->last_busno;
402 of_scan_bus(node, bus);
403 }
s.hauer@pengutronix.de99a565b2006-11-02 13:56:06 +0100404
Paul Mackerras42672922005-09-12 17:17:36 +1000405 if (mode == PCI_PROBE_NORMAL)
406 hose->last_busno = bus->subordinate = pci_scan_child_bus(bus);
Paul Mackerras42672922005-09-12 17:17:36 +1000407}
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409static int __init pcibios_init(void)
410{
411 struct pci_controller *hose, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +1100413 printk(KERN_INFO "PCI: Probing PCI hardware\n");
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /* For now, override phys_mem_access_prot. If we need it,
416 * later, we may move that initialization to each ppc_md
417 */
418 ppc_md.phys_mem_access_prot = pci_phys_mem_access_prot;
419
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +1100420 if (pci_probe_only)
421 ppc_pci_flags |= PPC_PCI_PROBE_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 /* Scan all of the recorded PCI controllers. */
John Rose92eb4602006-03-14 17:46:45 -0600424 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Paul Mackerras42672922005-09-12 17:17:36 +1000425 scan_phb(hose);
John Rose92eb4602006-03-14 17:46:45 -0600426 pci_bus_add_devices(hose->bus);
427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +1100429 /* Call common code to handle resource allocation */
430 pcibios_resource_survey();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Olof Johanssone884e9c2006-04-12 15:26:59 -0500432 printk(KERN_DEBUG "PCI: Probing PCI hardware done\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 return 0;
435}
436
437subsys_initcall(pcibios_init);
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439int pcibios_enable_device(struct pci_dev *dev, int mask)
440{
441 u16 cmd, oldcmd;
442 int i;
443
444 pci_read_config_word(dev, PCI_COMMAND, &cmd);
445 oldcmd = cmd;
446
447 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
448 struct resource *res = &dev->resource[i];
449
450 /* Only set up the requested stuff */
451 if (!(mask & (1<<i)))
452 continue;
453
454 if (res->flags & IORESOURCE_IO)
455 cmd |= PCI_COMMAND_IO;
456 if (res->flags & IORESOURCE_MEM)
457 cmd |= PCI_COMMAND_MEMORY;
458 }
459
460 if (cmd != oldcmd) {
461 printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
462 pci_name(dev), cmd);
463 /* Enable the appropriate bits in the PCI command register. */
464 pci_write_config_word(dev, PCI_COMMAND, cmd);
465 }
466 return 0;
467}
468
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000469#ifdef CONFIG_HOTPLUG
470
471int pcibios_unmap_io_space(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000473 struct pci_controller *hose;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000475 WARN_ON(bus == NULL);
Benjamin Herrenschmidtde821202007-05-15 16:19:36 +1000476
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000477 /* If this is not a PHB, we only flush the hash table over
478 * the area mapped by this bridge. We don't play with the PTE
479 * mappings since we might have to deal with sub-page alignemnts
480 * so flushing the hash table is the only sane way to make sure
481 * that no hash entries are covering that removed bridge area
482 * while still allowing other busses overlapping those pages
Benjamin Herrenschmidtde821202007-05-15 16:19:36 +1000483 */
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000484 if (bus->self) {
485 struct resource *res = bus->resource[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000487 DBG("IO unmapping for PCI-PCI bridge %s\n",
488 pci_name(bus->self));
489
490 __flush_hash_table_range(&init_mm, res->start + _IO_BASE,
491 res->end - res->start + 1);
492 return 0;
493 }
494
495 /* Get the host bridge */
496 hose = pci_bus_to_host(bus);
497
498 /* Check if we have IOs allocated */
499 if (hose->io_base_alloc == 0)
500 return 0;
501
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100502 DBG("IO unmapping for PHB %s\n", hose->dn->full_name);
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000503 DBG(" alloc=0x%p\n", hose->io_base_alloc);
504
505 /* This is a PHB, we fully unmap the IO area */
506 vunmap(hose->io_base_alloc);
507
508 return 0;
509}
510EXPORT_SYMBOL_GPL(pcibios_unmap_io_space);
511
512#endif /* CONFIG_HOTPLUG */
513
514int __devinit pcibios_map_io_space(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000516 struct vm_struct *area;
517 unsigned long phys_page;
518 unsigned long size_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 unsigned long io_virt_offset;
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000520 struct pci_controller *hose;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000522 WARN_ON(bus == NULL);
Benjamin Herrenschmidtde821202007-05-15 16:19:36 +1000523
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000524 /* If this not a PHB, nothing to do, page tables still exist and
525 * thus HPTEs will be faulted in when needed
526 */
527 if (bus->self) {
528 DBG("IO mapping for PCI-PCI bridge %s\n",
529 pci_name(bus->self));
530 DBG(" virt=0x%016lx...0x%016lx\n",
531 bus->resource[0]->start + _IO_BASE,
532 bus->resource[0]->end + _IO_BASE);
533 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000536 /* Get the host bridge */
537 hose = pci_bus_to_host(bus);
538 phys_page = _ALIGN_DOWN(hose->io_base_phys, PAGE_SIZE);
539 size_page = _ALIGN_UP(hose->pci_io_size, PAGE_SIZE);
540
541 /* Make sure IO area address is clear */
542 hose->io_base_alloc = NULL;
543
544 /* If there's no IO to map on that bus, get away too */
545 if (hose->pci_io_size == 0 || hose->io_base_phys == 0)
546 return 0;
547
548 /* Let's allocate some IO space for that guy. We don't pass
549 * VM_IOREMAP because we don't care about alignment tricks that
550 * the core does in that case. Maybe we should due to stupid card
551 * with incomplete address decoding but I'd rather not deal with
552 * those outside of the reserved 64K legacy region.
553 */
554 area = __get_vm_area(size_page, 0, PHB_IO_BASE, PHB_IO_END);
555 if (area == NULL)
556 return -ENOMEM;
557 hose->io_base_alloc = area->addr;
558 hose->io_base_virt = (void __iomem *)(area->addr +
559 hose->io_base_phys - phys_page);
560
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100561 DBG("IO mapping for PHB %s\n", hose->dn->full_name);
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000562 DBG(" phys=0x%016lx, virt=0x%p (alloc=0x%p)\n",
563 hose->io_base_phys, hose->io_base_virt, hose->io_base_alloc);
564 DBG(" size=0x%016lx (alloc=0x%016lx)\n",
565 hose->pci_io_size, size_page);
566
567 /* Establish the mapping */
568 if (__ioremap_at(phys_page, area->addr, size_page,
569 _PAGE_NO_CACHE | _PAGE_GUARDED) == NULL)
570 return -ENOMEM;
571
572 /* Fixup hose IO resource */
573 io_virt_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
574 hose->io_resource.start += io_virt_offset;
575 hose->io_resource.end += io_virt_offset;
576
577 DBG(" hose->io_resource=0x%016lx...0x%016lx\n",
578 hose->io_resource.start, hose->io_resource.end);
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return 0;
581}
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000582EXPORT_SYMBOL_GPL(pcibios_map_io_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100584void __devinit pcibios_setup_new_device(struct pci_dev *dev)
585{
586 struct dev_archdata *sd = &dev->dev.archdata;
587
588 sd->of_node = pci_device_to_OF_node(dev);
589
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +1100590 DBG("PCI: device %s OF node: %s\n", pci_name(dev),
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100591 sd->of_node ? sd->of_node->full_name : "<none>");
592
593 sd->dma_ops = pci_dma_ops;
594#ifdef CONFIG_NUMA
595 sd->numa_node = pcibus_to_node(dev->bus);
596#else
597 sd->numa_node = -1;
598#endif
599 if (ppc_md.pci_dma_dev_setup)
600 ppc_md.pci_dma_dev_setup(dev);
601}
602EXPORT_SYMBOL(pcibios_setup_new_device);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100603
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +1100604void __devinit pcibios_do_bus_setup(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Paul Mackerras42672922005-09-12 17:17:36 +1000606 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100608 if (ppc_md.pci_dma_bus_setup)
609 ppc_md.pci_dma_bus_setup(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 list_for_each_entry(dev, &bus->devices, bus_list)
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100612 pcibios_setup_new_device(dev);
Paul Mackerras42672922005-09-12 17:17:36 +1000613}
614
Benjamin Herrenschmidtf2c45832005-12-15 15:00:57 +1100615unsigned long pci_address_to_pio(phys_addr_t address)
Stephen Rothwelld4e4b352005-12-07 13:01:05 +1100616{
617 struct pci_controller *hose, *tmp;
618
619 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
620 if (address >= hose->io_base_phys &&
Benjamin Herrenschmidtf2c45832005-12-15 15:00:57 +1100621 address < (hose->io_base_phys + hose->pci_io_size)) {
622 unsigned long base =
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000623 (unsigned long)hose->io_base_virt - _IO_BASE;
Benjamin Herrenschmidtf2c45832005-12-15 15:00:57 +1100624 return base + (address - hose->io_base_phys);
625 }
Stephen Rothwelld4e4b352005-12-07 13:01:05 +1100626 }
627 return (unsigned int)-1;
628}
629EXPORT_SYMBOL_GPL(pci_address_to_pio);
630
Paul Mackerrasb2ad7b52005-09-09 23:02:36 +1000631
632#define IOBASE_BRIDGE_NUMBER 0
633#define IOBASE_MEMORY 1
634#define IOBASE_IO 2
635#define IOBASE_ISA_IO 3
636#define IOBASE_ISA_MEM 4
637
638long sys_pciconfig_iobase(long which, unsigned long in_bus,
639 unsigned long in_devfn)
640{
641 struct pci_controller* hose;
642 struct list_head *ln;
643 struct pci_bus *bus = NULL;
644 struct device_node *hose_node;
645
646 /* Argh ! Please forgive me for that hack, but that's the
647 * simplest way to get existing XFree to not lockup on some
648 * G5 machines... So when something asks for bus 0 io base
649 * (bus 0 is HT root), we return the AGP one instead.
650 */
Paul Mackerras799d6042005-11-10 13:37:51 +1100651 if (machine_is_compatible("MacRISC4"))
Paul Mackerrasb2ad7b52005-09-09 23:02:36 +1000652 if (in_bus == 0)
653 in_bus = 0xf0;
Paul Mackerrasb2ad7b52005-09-09 23:02:36 +1000654
655 /* That syscall isn't quite compatible with PCI domains, but it's
656 * used on pre-domains setup. We return the first match
657 */
658
659 for (ln = pci_root_buses.next; ln != &pci_root_buses; ln = ln->next) {
660 bus = pci_bus_b(ln);
Benjamin Herrenschmidt545da942007-01-28 07:45:53 +1100661 if (in_bus >= bus->number && in_bus <= bus->subordinate)
Paul Mackerrasb2ad7b52005-09-09 23:02:36 +1000662 break;
663 bus = NULL;
664 }
665 if (bus == NULL || bus->sysdata == NULL)
666 return -ENODEV;
667
668 hose_node = (struct device_node *)bus->sysdata;
669 hose = PCI_DN(hose_node)->phb;
670
671 switch (which) {
672 case IOBASE_BRIDGE_NUMBER:
673 return (long)hose->first_busno;
674 case IOBASE_MEMORY:
675 return (long)hose->pci_mem_offset;
676 case IOBASE_IO:
677 return (long)hose->io_base_phys;
678 case IOBASE_ISA_IO:
679 return (long)isa_io_base;
680 case IOBASE_ISA_MEM:
681 return -EINVAL;
682 }
683
684 return -EOPNOTSUPP;
685}
Anton Blanchard357518f2006-06-10 20:53:06 +1000686
687#ifdef CONFIG_NUMA
688int pcibus_to_node(struct pci_bus *bus)
689{
690 struct pci_controller *phb = pci_bus_to_host(bus);
691 return phb->node;
692}
693EXPORT_SYMBOL(pcibus_to_node);
694#endif