Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #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 Mackerras | b2ad7b5 | 2005-09-09 23:02:36 +1000 | [diff] [blame] | 23 | #include <linux/syscalls.h> |
Benjamin Herrenschmidt | 6e99e45 | 2006-07-10 04:44:42 -0700 | [diff] [blame] | 24 | #include <linux/irq.h> |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 25 | #include <linux/vmalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <asm/machdep.h> |
Stephen Rothwell | d387899 | 2005-09-28 02:50:25 +1000 | [diff] [blame] | 33 | #include <asm/ppc-pci.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | #ifdef DEBUG |
Michael Ellerman | f9e4ec5 | 2005-11-15 15:16:38 +1100 | [diff] [blame] | 36 | #include <asm/udbg.h> |
Benjamin Herrenschmidt | 1beb6a7 | 2005-12-14 13:10:10 +1100 | [diff] [blame] | 37 | #define DBG(fmt...) printk(fmt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #else |
| 39 | #define DBG(fmt...) |
| 40 | #endif |
| 41 | |
| 42 | unsigned long pci_probe_only = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | /* 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 Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 47 | * ISA drivers use hard coded offsets. If no ISA bus exists nothing |
| 48 | * is mapped on the first 64K of IO space |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | */ |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 50 | unsigned long pci_io_base = ISA_IO_BASE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | EXPORT_SYMBOL(pci_io_base); |
| 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | LIST_HEAD(hose_list); |
| 54 | |
Stephen Rothwell | 5719070 | 2007-03-04 17:02:41 +1100 | [diff] [blame] | 55 | static struct dma_mapping_ops *pci_dma_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Stephen Rothwell | 9874777 | 2007-03-04 16:58:39 +1100 | [diff] [blame] | 57 | void set_pci_dma_ops(struct dma_mapping_ops *dma_ops) |
| 58 | { |
| 59 | pci_dma_ops = dma_ops; |
| 60 | } |
| 61 | |
Stephen Rothwell | 5719070 | 2007-03-04 17:02:41 +1100 | [diff] [blame] | 62 | struct dma_mapping_ops *get_pci_dma_ops(void) |
| 63 | { |
| 64 | return pci_dma_ops; |
| 65 | } |
| 66 | EXPORT_SYMBOL(get_pci_dma_ops); |
| 67 | |
Michael Ellerman | 84631f3 | 2007-12-17 17:35:53 +1100 | [diff] [blame] | 68 | |
| 69 | int pci_set_dma_mask(struct pci_dev *dev, u64 mask) |
| 70 | { |
| 71 | return dma_set_mask(&dev->dev, mask); |
| 72 | } |
| 73 | |
| 74 | int 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32); |
| 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | * We need to avoid collisions with `mirrored' VGA ports |
| 96 | * and other strange ISA hardware, so we always want the |
| 97 | * addresses to be allocated in the 0x000-0x0ff region |
| 98 | * modulo 0x400. |
| 99 | * |
| 100 | * Why? Because some silly external IO cards only decode |
| 101 | * the low 10 bits of the IO address. The 0x00-0xff region |
| 102 | * is reserved for motherboard devices that decode all 16 |
| 103 | * bits, so it's ok to allocate at, say, 0x2800-0x28ff, |
| 104 | * but we want to try to avoid allocating at 0x2900-0x2bff |
| 105 | * which might have be mirrored at 0x0100-0x03ff.. |
| 106 | */ |
| 107 | void pcibios_align_resource(void *data, struct resource *res, |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 108 | resource_size_t size, resource_size_t align) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
| 110 | struct pci_dev *dev = data; |
| 111 | struct pci_controller *hose = pci_bus_to_host(dev->bus); |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 112 | resource_size_t start = res->start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | unsigned long alignto; |
| 114 | |
| 115 | if (res->flags & IORESOURCE_IO) { |
| 116 | unsigned long offset = (unsigned long)hose->io_base_virt - |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 117 | _IO_BASE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | /* Make sure we start at our min on all hoses */ |
| 119 | if (start - offset < PCIBIOS_MIN_IO) |
| 120 | start = PCIBIOS_MIN_IO + offset; |
| 121 | |
| 122 | /* |
| 123 | * Put everything into 0x00-0xff region modulo 0x400 |
| 124 | */ |
| 125 | if (start & 0x300) |
| 126 | start = (start + 0x3ff) & ~0x3ff; |
| 127 | |
| 128 | } else if (res->flags & IORESOURCE_MEM) { |
| 129 | /* Make sure we start at our min on all hoses */ |
| 130 | if (start - hose->pci_mem_offset < PCIBIOS_MIN_MEM) |
| 131 | start = PCIBIOS_MIN_MEM + hose->pci_mem_offset; |
| 132 | |
| 133 | /* Align to multiple of size of minimum base. */ |
| 134 | alignto = max(0x1000UL, align); |
| 135 | start = ALIGN(start, alignto); |
| 136 | } |
| 137 | |
| 138 | res->start = start; |
| 139 | } |
| 140 | |
Linas Vepstas | facf078 | 2005-11-03 18:52:01 -0600 | [diff] [blame] | 141 | void __devinit pcibios_claim_one_bus(struct pci_bus *b) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
| 143 | struct pci_dev *dev; |
| 144 | struct pci_bus *child_bus; |
| 145 | |
| 146 | list_for_each_entry(dev, &b->devices, bus_list) { |
| 147 | int i; |
| 148 | |
| 149 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { |
| 150 | struct resource *r = &dev->resource[i]; |
| 151 | |
| 152 | if (r->parent || !r->start || !r->flags) |
| 153 | continue; |
| 154 | pci_claim_resource(dev, i); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | list_for_each_entry(child_bus, &b->children, node) |
| 159 | pcibios_claim_one_bus(child_bus); |
| 160 | } |
linas | af9deab | 2006-01-10 15:18:16 -0600 | [diff] [blame] | 161 | #ifdef CONFIG_HOTPLUG |
| 162 | EXPORT_SYMBOL_GPL(pcibios_claim_one_bus); |
| 163 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | static void __init pcibios_claim_of_setup(void) |
| 166 | { |
| 167 | struct pci_bus *b; |
| 168 | |
| 169 | list_for_each_entry(b, &pci_root_buses, node) |
| 170 | pcibios_claim_one_bus(b); |
| 171 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 173 | static u32 get_int_prop(struct device_node *np, const char *name, u32 def) |
| 174 | { |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 175 | const u32 *prop; |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 176 | int len; |
| 177 | |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 178 | prop = of_get_property(np, name, &len); |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 179 | if (prop && len >= 4) |
| 180 | return *prop; |
| 181 | return def; |
| 182 | } |
| 183 | |
| 184 | static unsigned int pci_parse_of_flags(u32 addr0) |
| 185 | { |
| 186 | unsigned int flags = 0; |
| 187 | |
| 188 | if (addr0 & 0x02000000) { |
Paul Mackerras | d79e743 | 2005-09-21 14:14:22 +1000 | [diff] [blame] | 189 | flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY; |
| 190 | flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64; |
| 191 | flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M; |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 192 | if (addr0 & 0x40000000) |
Paul Mackerras | d79e743 | 2005-09-21 14:14:22 +1000 | [diff] [blame] | 193 | flags |= IORESOURCE_PREFETCH |
| 194 | | PCI_BASE_ADDRESS_MEM_PREFETCH; |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 195 | } else if (addr0 & 0x01000000) |
Paul Mackerras | d79e743 | 2005-09-21 14:14:22 +1000 | [diff] [blame] | 196 | flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO; |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 197 | return flags; |
| 198 | } |
| 199 | |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 200 | |
| 201 | static void pci_parse_of_addrs(struct device_node *node, struct pci_dev *dev) |
| 202 | { |
| 203 | u64 base, size; |
| 204 | unsigned int flags; |
| 205 | struct resource *res; |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 206 | const u32 *addrs; |
| 207 | u32 i; |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 208 | int proplen; |
| 209 | |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 210 | addrs = of_get_property(node, "assigned-addresses", &proplen); |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 211 | if (!addrs) |
| 212 | return; |
Benjamin Herrenschmidt | 1beb6a7 | 2005-12-14 13:10:10 +1100 | [diff] [blame] | 213 | DBG(" parse addresses (%d bytes) @ %p\n", proplen, addrs); |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 214 | for (; proplen >= 20; proplen -= 20, addrs += 5) { |
| 215 | flags = pci_parse_of_flags(addrs[0]); |
| 216 | if (!flags) |
| 217 | continue; |
Jon Loeliger | 327e22d | 2007-06-04 14:28:44 -0500 | [diff] [blame] | 218 | base = of_read_number(&addrs[1], 2); |
| 219 | size = of_read_number(&addrs[3], 2); |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 220 | if (!size) |
| 221 | continue; |
| 222 | i = addrs[0] & 0xff; |
Benjamin Herrenschmidt | 1beb6a7 | 2005-12-14 13:10:10 +1100 | [diff] [blame] | 223 | DBG(" base: %llx, size: %llx, i: %x\n", |
| 224 | (unsigned long long)base, (unsigned long long)size, i); |
| 225 | |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 226 | if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) { |
| 227 | res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2]; |
| 228 | } else if (i == dev->rom_base_reg) { |
| 229 | res = &dev->resource[PCI_ROM_RESOURCE]; |
| 230 | flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE; |
| 231 | } else { |
| 232 | printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i); |
| 233 | continue; |
| 234 | } |
| 235 | res->start = base; |
| 236 | res->end = base + size - 1; |
| 237 | res->flags = flags; |
| 238 | res->name = pci_name(dev); |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
John Rose | ead8371 | 2005-11-04 15:30:56 -0600 | [diff] [blame] | 242 | struct pci_dev *of_create_pci_dev(struct device_node *node, |
| 243 | struct pci_bus *bus, int devfn) |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 244 | { |
| 245 | struct pci_dev *dev; |
| 246 | const char *type; |
| 247 | |
Michael Ellerman | bab41e9 | 2007-04-05 17:19:09 +1000 | [diff] [blame] | 248 | dev = alloc_pci_dev(); |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 249 | if (!dev) |
| 250 | return NULL; |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 251 | type = of_get_property(node, "device_type", NULL); |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 252 | if (type == NULL) |
| 253 | type = ""; |
| 254 | |
Benjamin Herrenschmidt | 1beb6a7 | 2005-12-14 13:10:10 +1100 | [diff] [blame] | 255 | DBG(" create device, devfn: %x, type: %s\n", devfn, type); |
| 256 | |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 257 | dev->bus = bus; |
| 258 | dev->sysdata = node; |
| 259 | dev->dev.parent = bus->bridge; |
| 260 | dev->dev.bus = &pci_bus_type; |
| 261 | dev->devfn = devfn; |
| 262 | dev->multifunction = 0; /* maybe a lie? */ |
| 263 | |
| 264 | dev->vendor = get_int_prop(node, "vendor-id", 0xffff); |
| 265 | dev->device = get_int_prop(node, "device-id", 0xffff); |
| 266 | dev->subsystem_vendor = get_int_prop(node, "subsystem-vendor-id", 0); |
| 267 | dev->subsystem_device = get_int_prop(node, "subsystem-id", 0); |
| 268 | |
Benjamin Herrenschmidt | 9d17a5c | 2006-01-10 14:50:37 +1100 | [diff] [blame] | 269 | dev->cfg_size = pci_cfg_space_size(dev); |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 270 | |
| 271 | sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus), |
| 272 | dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn)); |
| 273 | dev->class = get_int_prop(node, "class-code", 0); |
Auke Kok | b8a3a52 | 2007-06-08 15:46:30 -0700 | [diff] [blame] | 274 | dev->revision = get_int_prop(node, "revision-id", 0); |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 275 | |
Benjamin Herrenschmidt | 1beb6a7 | 2005-12-14 13:10:10 +1100 | [diff] [blame] | 276 | DBG(" class: 0x%x\n", dev->class); |
Auke Kok | b8a3a52 | 2007-06-08 15:46:30 -0700 | [diff] [blame] | 277 | DBG(" revision: 0x%x\n", dev->revision); |
Benjamin Herrenschmidt | 1beb6a7 | 2005-12-14 13:10:10 +1100 | [diff] [blame] | 278 | |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 279 | dev->current_state = 4; /* unknown power state */ |
Linas Vepstas | bb63ab1 | 2006-12-19 14:00:34 -0600 | [diff] [blame] | 280 | dev->error_state = pci_channel_io_normal; |
Benjamin Herrenschmidt | 8f2ea1f | 2007-08-07 08:05:10 +1000 | [diff] [blame] | 281 | dev->dma_mask = 0xffffffff; |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 282 | |
Jake Moilanen | bb53bb3 | 2006-06-07 16:05:46 -0500 | [diff] [blame] | 283 | if (!strcmp(type, "pci") || !strcmp(type, "pciex")) { |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 284 | /* a PCI-PCI bridge */ |
| 285 | dev->hdr_type = PCI_HEADER_TYPE_BRIDGE; |
| 286 | dev->rom_base_reg = PCI_ROM_ADDRESS1; |
| 287 | } else if (!strcmp(type, "cardbus")) { |
| 288 | dev->hdr_type = PCI_HEADER_TYPE_CARDBUS; |
| 289 | } else { |
| 290 | dev->hdr_type = PCI_HEADER_TYPE_NORMAL; |
| 291 | dev->rom_base_reg = PCI_ROM_ADDRESS; |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 292 | /* Maybe do a default OF mapping here */ |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 293 | dev->irq = NO_IRQ; |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | pci_parse_of_addrs(node, dev); |
| 297 | |
Benjamin Herrenschmidt | 1beb6a7 | 2005-12-14 13:10:10 +1100 | [diff] [blame] | 298 | DBG(" adding to system ...\n"); |
| 299 | |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 300 | pci_device_add(dev, bus); |
| 301 | |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 302 | return dev; |
| 303 | } |
John Rose | ead8371 | 2005-11-04 15:30:56 -0600 | [diff] [blame] | 304 | EXPORT_SYMBOL(of_create_pci_dev); |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 305 | |
John Rose | ead8371 | 2005-11-04 15:30:56 -0600 | [diff] [blame] | 306 | void __devinit of_scan_bus(struct device_node *node, |
Benjamin Herrenschmidt | bf5e2ba | 2007-12-20 14:54:51 +1100 | [diff] [blame^] | 307 | struct pci_bus *bus) |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 308 | { |
| 309 | struct device_node *child = NULL; |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 310 | const u32 *reg; |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 311 | int reglen, devfn; |
| 312 | struct pci_dev *dev; |
| 313 | |
Benjamin Herrenschmidt | 1beb6a7 | 2005-12-14 13:10:10 +1100 | [diff] [blame] | 314 | DBG("of_scan_bus(%s) bus no %d... \n", node->full_name, bus->number); |
| 315 | |
Benjamin Herrenschmidt | bf5e2ba | 2007-12-20 14:54:51 +1100 | [diff] [blame^] | 316 | /* Scan direct children */ |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 317 | while ((child = of_get_next_child(node, child)) != NULL) { |
Benjamin Herrenschmidt | 1beb6a7 | 2005-12-14 13:10:10 +1100 | [diff] [blame] | 318 | DBG(" * %s\n", child->full_name); |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 319 | reg = of_get_property(child, "reg", ®len); |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 320 | if (reg == NULL || reglen < 20) |
| 321 | continue; |
| 322 | devfn = (reg[0] >> 8) & 0xff; |
Benjamin Herrenschmidt | 1beb6a7 | 2005-12-14 13:10:10 +1100 | [diff] [blame] | 323 | |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 324 | /* create a new pci_dev for this device */ |
| 325 | dev = of_create_pci_dev(child, bus, devfn); |
| 326 | if (!dev) |
| 327 | continue; |
Benjamin Herrenschmidt | bf5e2ba | 2007-12-20 14:54:51 +1100 | [diff] [blame^] | 328 | DBG(" dev header type: %x\n", dev->hdr_type); |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 329 | } |
| 330 | |
Benjamin Herrenschmidt | bf5e2ba | 2007-12-20 14:54:51 +1100 | [diff] [blame^] | 331 | /* Ally all fixups */ |
| 332 | pcibios_fixup_of_probed_bus(bus); |
| 333 | |
| 334 | /* Now scan child busses */ |
| 335 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 336 | if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE || |
| 337 | dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) { |
| 338 | struct device_node *child = pci_device_to_OF_node(dev); |
| 339 | if (dev) |
| 340 | of_scan_pci_bridge(child, dev); |
| 341 | } |
| 342 | } |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 343 | } |
John Rose | ead8371 | 2005-11-04 15:30:56 -0600 | [diff] [blame] | 344 | EXPORT_SYMBOL(of_scan_bus); |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 345 | |
John Rose | ead8371 | 2005-11-04 15:30:56 -0600 | [diff] [blame] | 346 | void __devinit of_scan_pci_bridge(struct device_node *node, |
Benjamin Herrenschmidt | bf5e2ba | 2007-12-20 14:54:51 +1100 | [diff] [blame^] | 347 | struct pci_dev *dev) |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 348 | { |
| 349 | struct pci_bus *bus; |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 350 | const u32 *busrange, *ranges; |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 351 | int len, i, mode; |
| 352 | struct resource *res; |
| 353 | unsigned int flags; |
| 354 | u64 size; |
| 355 | |
Benjamin Herrenschmidt | 1beb6a7 | 2005-12-14 13:10:10 +1100 | [diff] [blame] | 356 | DBG("of_scan_pci_bridge(%s)\n", node->full_name); |
| 357 | |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 358 | /* parse bus-range property */ |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 359 | busrange = of_get_property(node, "bus-range", &len); |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 360 | if (busrange == NULL || len != 8) { |
Benjamin Herrenschmidt | 1beb6a7 | 2005-12-14 13:10:10 +1100 | [diff] [blame] | 361 | printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n", |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 362 | node->full_name); |
| 363 | return; |
| 364 | } |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 365 | ranges = of_get_property(node, "ranges", &len); |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 366 | if (ranges == NULL) { |
Benjamin Herrenschmidt | 1beb6a7 | 2005-12-14 13:10:10 +1100 | [diff] [blame] | 367 | printk(KERN_DEBUG "Can't get ranges for PCI-PCI bridge %s\n", |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 368 | node->full_name); |
| 369 | return; |
| 370 | } |
| 371 | |
| 372 | bus = pci_add_new_bus(dev->bus, dev, busrange[0]); |
| 373 | if (!bus) { |
| 374 | printk(KERN_ERR "Failed to create pci bus for %s\n", |
| 375 | node->full_name); |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | bus->primary = dev->bus->number; |
| 380 | bus->subordinate = busrange[1]; |
| 381 | bus->bridge_ctl = 0; |
| 382 | bus->sysdata = node; |
| 383 | |
| 384 | /* parse ranges property */ |
| 385 | /* PCI #address-cells == 3 and #size-cells == 2 always */ |
| 386 | res = &dev->resource[PCI_BRIDGE_RESOURCES]; |
| 387 | for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) { |
| 388 | res->flags = 0; |
| 389 | bus->resource[i] = res; |
| 390 | ++res; |
| 391 | } |
| 392 | i = 1; |
| 393 | for (; len >= 32; len -= 32, ranges += 8) { |
| 394 | flags = pci_parse_of_flags(ranges[0]); |
Jon Loeliger | 327e22d | 2007-06-04 14:28:44 -0500 | [diff] [blame] | 395 | size = of_read_number(&ranges[6], 2); |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 396 | if (flags == 0 || size == 0) |
| 397 | continue; |
| 398 | if (flags & IORESOURCE_IO) { |
| 399 | res = bus->resource[0]; |
| 400 | if (res->flags) { |
| 401 | printk(KERN_ERR "PCI: ignoring extra I/O range" |
| 402 | " for bridge %s\n", node->full_name); |
| 403 | continue; |
| 404 | } |
| 405 | } else { |
| 406 | if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) { |
| 407 | printk(KERN_ERR "PCI: too many memory ranges" |
| 408 | " for bridge %s\n", node->full_name); |
| 409 | continue; |
| 410 | } |
| 411 | res = bus->resource[i]; |
| 412 | ++i; |
| 413 | } |
Jon Loeliger | 327e22d | 2007-06-04 14:28:44 -0500 | [diff] [blame] | 414 | res->start = of_read_number(&ranges[1], 2); |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 415 | res->end = res->start + size - 1; |
| 416 | res->flags = flags; |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 417 | } |
| 418 | sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus), |
| 419 | bus->number); |
Benjamin Herrenschmidt | 1beb6a7 | 2005-12-14 13:10:10 +1100 | [diff] [blame] | 420 | DBG(" bus name: %s\n", bus->name); |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 421 | |
| 422 | mode = PCI_PROBE_NORMAL; |
| 423 | if (ppc_md.pci_probe_mode) |
| 424 | mode = ppc_md.pci_probe_mode(bus); |
Benjamin Herrenschmidt | 1beb6a7 | 2005-12-14 13:10:10 +1100 | [diff] [blame] | 425 | DBG(" probe mode: %d\n", mode); |
| 426 | |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 427 | if (mode == PCI_PROBE_DEVTREE) |
| 428 | of_scan_bus(node, bus); |
| 429 | else if (mode == PCI_PROBE_NORMAL) |
| 430 | pci_scan_child_bus(bus); |
| 431 | } |
John Rose | ead8371 | 2005-11-04 15:30:56 -0600 | [diff] [blame] | 432 | EXPORT_SYMBOL(of_scan_pci_bridge); |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 433 | |
John Rose | ead8371 | 2005-11-04 15:30:56 -0600 | [diff] [blame] | 434 | void __devinit scan_phb(struct pci_controller *hose) |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 435 | { |
| 436 | struct pci_bus *bus; |
Stephen Rothwell | 44ef339 | 2007-12-10 14:33:21 +1100 | [diff] [blame] | 437 | struct device_node *node = hose->dn; |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 438 | int i, mode; |
| 439 | struct resource *res; |
| 440 | |
Benjamin Herrenschmidt | 1beb6a7 | 2005-12-14 13:10:10 +1100 | [diff] [blame] | 441 | DBG("Scanning PHB %s\n", node ? node->full_name : "<NO NAME>"); |
| 442 | |
Benjamin Herrenschmidt | 803d457 | 2006-11-11 17:25:07 +1100 | [diff] [blame] | 443 | bus = pci_create_bus(hose->parent, hose->first_busno, hose->ops, node); |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 444 | if (bus == NULL) { |
| 445 | printk(KERN_ERR "Failed to create bus for PCI domain %04x\n", |
| 446 | hose->global_number); |
| 447 | return; |
| 448 | } |
| 449 | bus->secondary = hose->first_busno; |
| 450 | hose->bus = bus; |
| 451 | |
Stephen Rothwell | 9ccc4fd | 2007-12-07 02:03:23 +1100 | [diff] [blame] | 452 | pcibios_map_io_space(bus); |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 453 | |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 454 | bus->resource[0] = res = &hose->io_resource; |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 455 | if (res->flags && request_resource(&ioport_resource, res)) { |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 456 | printk(KERN_ERR "Failed to request PCI IO region " |
| 457 | "on PCI domain %04x\n", hose->global_number); |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 458 | DBG("res->start = 0x%016lx, res->end = 0x%016lx\n", |
| 459 | res->start, res->end); |
| 460 | } |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 461 | |
| 462 | for (i = 0; i < 3; ++i) { |
| 463 | res = &hose->mem_resources[i]; |
| 464 | bus->resource[i+1] = res; |
| 465 | if (res->flags && request_resource(&iomem_resource, res)) |
| 466 | printk(KERN_ERR "Failed to request PCI memory region " |
| 467 | "on PCI domain %04x\n", hose->global_number); |
| 468 | } |
| 469 | |
| 470 | mode = PCI_PROBE_NORMAL; |
s.hauer@pengutronix.de | 99a565b | 2006-11-02 13:56:06 +0100 | [diff] [blame] | 471 | |
Benjamin Herrenschmidt | 1beb6a7 | 2005-12-14 13:10:10 +1100 | [diff] [blame] | 472 | if (node && ppc_md.pci_probe_mode) |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 473 | mode = ppc_md.pci_probe_mode(bus); |
Benjamin Herrenschmidt | 1beb6a7 | 2005-12-14 13:10:10 +1100 | [diff] [blame] | 474 | DBG(" probe mode: %d\n", mode); |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 475 | if (mode == PCI_PROBE_DEVTREE) { |
| 476 | bus->subordinate = hose->last_busno; |
| 477 | of_scan_bus(node, bus); |
| 478 | } |
s.hauer@pengutronix.de | 99a565b | 2006-11-02 13:56:06 +0100 | [diff] [blame] | 479 | |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 480 | if (mode == PCI_PROBE_NORMAL) |
| 481 | hose->last_busno = bus->subordinate = pci_scan_child_bus(bus); |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 482 | } |
| 483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | static int __init pcibios_init(void) |
| 485 | { |
| 486 | struct pci_controller *hose, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
| 488 | /* For now, override phys_mem_access_prot. If we need it, |
| 489 | * later, we may move that initialization to each ppc_md |
| 490 | */ |
| 491 | ppc_md.phys_mem_access_prot = pci_phys_mem_access_prot; |
| 492 | |
Olof Johansson | e884e9c | 2006-04-12 15:26:59 -0500 | [diff] [blame] | 493 | printk(KERN_DEBUG "PCI: Probing PCI hardware\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | |
| 495 | /* Scan all of the recorded PCI controllers. */ |
John Rose | 92eb460 | 2006-03-14 17:46:45 -0600 | [diff] [blame] | 496 | list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 497 | scan_phb(hose); |
John Rose | 92eb460 | 2006-03-14 17:46:45 -0600 | [diff] [blame] | 498 | pci_bus_add_devices(hose->bus); |
| 499 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
Stephen Rothwell | 9ccc4fd | 2007-12-07 02:03:23 +1100 | [diff] [blame] | 501 | if (pci_probe_only) |
| 502 | pcibios_claim_of_setup(); |
| 503 | else |
| 504 | /* FIXME: `else' will be removed when |
| 505 | pci_assign_unassigned_resources() is able to work |
| 506 | correctly with [partially] allocated PCI tree. */ |
| 507 | pci_assign_unassigned_resources(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | |
| 509 | /* Call machine dependent final fixup */ |
| 510 | if (ppc_md.pcibios_fixup) |
| 511 | ppc_md.pcibios_fixup(); |
| 512 | |
Olof Johansson | e884e9c | 2006-04-12 15:26:59 -0500 | [diff] [blame] | 513 | printk(KERN_DEBUG "PCI: Probing PCI hardware done\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | subsys_initcall(pcibios_init); |
| 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | int pcibios_enable_device(struct pci_dev *dev, int mask) |
| 521 | { |
| 522 | u16 cmd, oldcmd; |
| 523 | int i; |
| 524 | |
| 525 | pci_read_config_word(dev, PCI_COMMAND, &cmd); |
| 526 | oldcmd = cmd; |
| 527 | |
| 528 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { |
| 529 | struct resource *res = &dev->resource[i]; |
| 530 | |
| 531 | /* Only set up the requested stuff */ |
| 532 | if (!(mask & (1<<i))) |
| 533 | continue; |
| 534 | |
| 535 | if (res->flags & IORESOURCE_IO) |
| 536 | cmd |= PCI_COMMAND_IO; |
| 537 | if (res->flags & IORESOURCE_MEM) |
| 538 | cmd |= PCI_COMMAND_MEMORY; |
| 539 | } |
| 540 | |
| 541 | if (cmd != oldcmd) { |
| 542 | printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n", |
| 543 | pci_name(dev), cmd); |
| 544 | /* Enable the appropriate bits in the PCI command register. */ |
| 545 | pci_write_config_word(dev, PCI_COMMAND, cmd); |
| 546 | } |
| 547 | return 0; |
| 548 | } |
| 549 | |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 550 | #ifdef CONFIG_HOTPLUG |
| 551 | |
| 552 | int pcibios_unmap_io_space(struct pci_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | { |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 554 | struct pci_controller *hose; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 556 | WARN_ON(bus == NULL); |
Benjamin Herrenschmidt | de82120 | 2007-05-15 16:19:36 +1000 | [diff] [blame] | 557 | |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 558 | /* If this is not a PHB, we only flush the hash table over |
| 559 | * the area mapped by this bridge. We don't play with the PTE |
| 560 | * mappings since we might have to deal with sub-page alignemnts |
| 561 | * so flushing the hash table is the only sane way to make sure |
| 562 | * that no hash entries are covering that removed bridge area |
| 563 | * while still allowing other busses overlapping those pages |
Benjamin Herrenschmidt | de82120 | 2007-05-15 16:19:36 +1000 | [diff] [blame] | 564 | */ |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 565 | if (bus->self) { |
| 566 | struct resource *res = bus->resource[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 568 | DBG("IO unmapping for PCI-PCI bridge %s\n", |
| 569 | pci_name(bus->self)); |
| 570 | |
| 571 | __flush_hash_table_range(&init_mm, res->start + _IO_BASE, |
| 572 | res->end - res->start + 1); |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | /* Get the host bridge */ |
| 577 | hose = pci_bus_to_host(bus); |
| 578 | |
| 579 | /* Check if we have IOs allocated */ |
| 580 | if (hose->io_base_alloc == 0) |
| 581 | return 0; |
| 582 | |
Stephen Rothwell | 44ef339 | 2007-12-10 14:33:21 +1100 | [diff] [blame] | 583 | DBG("IO unmapping for PHB %s\n", hose->dn->full_name); |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 584 | DBG(" alloc=0x%p\n", hose->io_base_alloc); |
| 585 | |
| 586 | /* This is a PHB, we fully unmap the IO area */ |
| 587 | vunmap(hose->io_base_alloc); |
| 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | EXPORT_SYMBOL_GPL(pcibios_unmap_io_space); |
| 592 | |
| 593 | #endif /* CONFIG_HOTPLUG */ |
| 594 | |
| 595 | int __devinit pcibios_map_io_space(struct pci_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | { |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 597 | struct vm_struct *area; |
| 598 | unsigned long phys_page; |
| 599 | unsigned long size_page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | unsigned long io_virt_offset; |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 601 | struct pci_controller *hose; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 603 | WARN_ON(bus == NULL); |
Benjamin Herrenschmidt | de82120 | 2007-05-15 16:19:36 +1000 | [diff] [blame] | 604 | |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 605 | /* If this not a PHB, nothing to do, page tables still exist and |
| 606 | * thus HPTEs will be faulted in when needed |
| 607 | */ |
| 608 | if (bus->self) { |
| 609 | DBG("IO mapping for PCI-PCI bridge %s\n", |
| 610 | pci_name(bus->self)); |
| 611 | DBG(" virt=0x%016lx...0x%016lx\n", |
| 612 | bus->resource[0]->start + _IO_BASE, |
| 613 | bus->resource[0]->end + _IO_BASE); |
| 614 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 617 | /* Get the host bridge */ |
| 618 | hose = pci_bus_to_host(bus); |
| 619 | phys_page = _ALIGN_DOWN(hose->io_base_phys, PAGE_SIZE); |
| 620 | size_page = _ALIGN_UP(hose->pci_io_size, PAGE_SIZE); |
| 621 | |
| 622 | /* Make sure IO area address is clear */ |
| 623 | hose->io_base_alloc = NULL; |
| 624 | |
| 625 | /* If there's no IO to map on that bus, get away too */ |
| 626 | if (hose->pci_io_size == 0 || hose->io_base_phys == 0) |
| 627 | return 0; |
| 628 | |
| 629 | /* Let's allocate some IO space for that guy. We don't pass |
| 630 | * VM_IOREMAP because we don't care about alignment tricks that |
| 631 | * the core does in that case. Maybe we should due to stupid card |
| 632 | * with incomplete address decoding but I'd rather not deal with |
| 633 | * those outside of the reserved 64K legacy region. |
| 634 | */ |
| 635 | area = __get_vm_area(size_page, 0, PHB_IO_BASE, PHB_IO_END); |
| 636 | if (area == NULL) |
| 637 | return -ENOMEM; |
| 638 | hose->io_base_alloc = area->addr; |
| 639 | hose->io_base_virt = (void __iomem *)(area->addr + |
| 640 | hose->io_base_phys - phys_page); |
| 641 | |
Stephen Rothwell | 44ef339 | 2007-12-10 14:33:21 +1100 | [diff] [blame] | 642 | DBG("IO mapping for PHB %s\n", hose->dn->full_name); |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 643 | DBG(" phys=0x%016lx, virt=0x%p (alloc=0x%p)\n", |
| 644 | hose->io_base_phys, hose->io_base_virt, hose->io_base_alloc); |
| 645 | DBG(" size=0x%016lx (alloc=0x%016lx)\n", |
| 646 | hose->pci_io_size, size_page); |
| 647 | |
| 648 | /* Establish the mapping */ |
| 649 | if (__ioremap_at(phys_page, area->addr, size_page, |
| 650 | _PAGE_NO_CACHE | _PAGE_GUARDED) == NULL) |
| 651 | return -ENOMEM; |
| 652 | |
| 653 | /* Fixup hose IO resource */ |
| 654 | io_virt_offset = (unsigned long)hose->io_base_virt - _IO_BASE; |
| 655 | hose->io_resource.start += io_virt_offset; |
| 656 | hose->io_resource.end += io_virt_offset; |
| 657 | |
| 658 | DBG(" hose->io_resource=0x%016lx...0x%016lx\n", |
| 659 | hose->io_resource.start, hose->io_resource.end); |
| 660 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | return 0; |
| 662 | } |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 663 | EXPORT_SYMBOL_GPL(pcibios_map_io_space); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 665 | void __devinit pcibios_setup_new_device(struct pci_dev *dev) |
| 666 | { |
| 667 | struct dev_archdata *sd = &dev->dev.archdata; |
| 668 | |
| 669 | sd->of_node = pci_device_to_OF_node(dev); |
| 670 | |
Benjamin Herrenschmidt | bf5e2ba | 2007-12-20 14:54:51 +1100 | [diff] [blame^] | 671 | DBG("PCI: device %s OF node: %s\n", pci_name(dev), |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 672 | sd->of_node ? sd->of_node->full_name : "<none>"); |
| 673 | |
| 674 | sd->dma_ops = pci_dma_ops; |
| 675 | #ifdef CONFIG_NUMA |
| 676 | sd->numa_node = pcibus_to_node(dev->bus); |
| 677 | #else |
| 678 | sd->numa_node = -1; |
| 679 | #endif |
| 680 | if (ppc_md.pci_dma_dev_setup) |
| 681 | ppc_md.pci_dma_dev_setup(dev); |
| 682 | } |
| 683 | EXPORT_SYMBOL(pcibios_setup_new_device); |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 684 | |
Benjamin Herrenschmidt | bf5e2ba | 2007-12-20 14:54:51 +1100 | [diff] [blame^] | 685 | void __devinit pcibios_do_bus_setup(struct pci_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | { |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 687 | struct pci_dev *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 689 | if (ppc_md.pci_dma_bus_setup) |
| 690 | ppc_md.pci_dma_bus_setup(bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | |
| 692 | list_for_each_entry(dev, &bus->devices, bus_list) |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 693 | pcibios_setup_new_device(dev); |
Paul Mackerras | 4267292 | 2005-09-12 17:17:36 +1000 | [diff] [blame] | 694 | } |
| 695 | |
Benjamin Herrenschmidt | f2c4583 | 2005-12-15 15:00:57 +1100 | [diff] [blame] | 696 | unsigned long pci_address_to_pio(phys_addr_t address) |
Stephen Rothwell | d4e4b35 | 2005-12-07 13:01:05 +1100 | [diff] [blame] | 697 | { |
| 698 | struct pci_controller *hose, *tmp; |
| 699 | |
| 700 | list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { |
| 701 | if (address >= hose->io_base_phys && |
Benjamin Herrenschmidt | f2c4583 | 2005-12-15 15:00:57 +1100 | [diff] [blame] | 702 | address < (hose->io_base_phys + hose->pci_io_size)) { |
| 703 | unsigned long base = |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 704 | (unsigned long)hose->io_base_virt - _IO_BASE; |
Benjamin Herrenschmidt | f2c4583 | 2005-12-15 15:00:57 +1100 | [diff] [blame] | 705 | return base + (address - hose->io_base_phys); |
| 706 | } |
Stephen Rothwell | d4e4b35 | 2005-12-07 13:01:05 +1100 | [diff] [blame] | 707 | } |
| 708 | return (unsigned int)-1; |
| 709 | } |
| 710 | EXPORT_SYMBOL_GPL(pci_address_to_pio); |
| 711 | |
Paul Mackerras | b2ad7b5 | 2005-09-09 23:02:36 +1000 | [diff] [blame] | 712 | |
| 713 | #define IOBASE_BRIDGE_NUMBER 0 |
| 714 | #define IOBASE_MEMORY 1 |
| 715 | #define IOBASE_IO 2 |
| 716 | #define IOBASE_ISA_IO 3 |
| 717 | #define IOBASE_ISA_MEM 4 |
| 718 | |
| 719 | long sys_pciconfig_iobase(long which, unsigned long in_bus, |
| 720 | unsigned long in_devfn) |
| 721 | { |
| 722 | struct pci_controller* hose; |
| 723 | struct list_head *ln; |
| 724 | struct pci_bus *bus = NULL; |
| 725 | struct device_node *hose_node; |
| 726 | |
| 727 | /* Argh ! Please forgive me for that hack, but that's the |
| 728 | * simplest way to get existing XFree to not lockup on some |
| 729 | * G5 machines... So when something asks for bus 0 io base |
| 730 | * (bus 0 is HT root), we return the AGP one instead. |
| 731 | */ |
Paul Mackerras | 799d604 | 2005-11-10 13:37:51 +1100 | [diff] [blame] | 732 | if (machine_is_compatible("MacRISC4")) |
Paul Mackerras | b2ad7b5 | 2005-09-09 23:02:36 +1000 | [diff] [blame] | 733 | if (in_bus == 0) |
| 734 | in_bus = 0xf0; |
Paul Mackerras | b2ad7b5 | 2005-09-09 23:02:36 +1000 | [diff] [blame] | 735 | |
| 736 | /* That syscall isn't quite compatible with PCI domains, but it's |
| 737 | * used on pre-domains setup. We return the first match |
| 738 | */ |
| 739 | |
| 740 | for (ln = pci_root_buses.next; ln != &pci_root_buses; ln = ln->next) { |
| 741 | bus = pci_bus_b(ln); |
Benjamin Herrenschmidt | 545da94 | 2007-01-28 07:45:53 +1100 | [diff] [blame] | 742 | if (in_bus >= bus->number && in_bus <= bus->subordinate) |
Paul Mackerras | b2ad7b5 | 2005-09-09 23:02:36 +1000 | [diff] [blame] | 743 | break; |
| 744 | bus = NULL; |
| 745 | } |
| 746 | if (bus == NULL || bus->sysdata == NULL) |
| 747 | return -ENODEV; |
| 748 | |
| 749 | hose_node = (struct device_node *)bus->sysdata; |
| 750 | hose = PCI_DN(hose_node)->phb; |
| 751 | |
| 752 | switch (which) { |
| 753 | case IOBASE_BRIDGE_NUMBER: |
| 754 | return (long)hose->first_busno; |
| 755 | case IOBASE_MEMORY: |
| 756 | return (long)hose->pci_mem_offset; |
| 757 | case IOBASE_IO: |
| 758 | return (long)hose->io_base_phys; |
| 759 | case IOBASE_ISA_IO: |
| 760 | return (long)isa_io_base; |
| 761 | case IOBASE_ISA_MEM: |
| 762 | return -EINVAL; |
| 763 | } |
| 764 | |
| 765 | return -EOPNOTSUPP; |
| 766 | } |
Anton Blanchard | 357518f | 2006-06-10 20:53:06 +1000 | [diff] [blame] | 767 | |
| 768 | #ifdef CONFIG_NUMA |
| 769 | int pcibus_to_node(struct pci_bus *bus) |
| 770 | { |
| 771 | struct pci_controller *phb = pci_bus_to_host(bus); |
| 772 | return phb->node; |
| 773 | } |
| 774 | EXPORT_SYMBOL(pcibus_to_node); |
| 775 | #endif |