Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Common pmac/prep/chrp pci routines. -- Cort |
| 3 | */ |
| 4 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 5 | #include <linux/kernel.h> |
| 6 | #include <linux/pci.h> |
| 7 | #include <linux/delay.h> |
| 8 | #include <linux/string.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/capability.h> |
| 11 | #include <linux/sched.h> |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/bootmem.h> |
Benjamin Herrenschmidt | 6e99e45 | 2006-07-10 04:44:42 -0700 | [diff] [blame] | 14 | #include <linux/irq.h> |
Benjamin Herrenschmidt | f90bb15 | 2006-11-11 17:24:51 +1100 | [diff] [blame] | 15 | #include <linux/list.h> |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 16 | |
| 17 | #include <asm/processor.h> |
| 18 | #include <asm/io.h> |
| 19 | #include <asm/prom.h> |
| 20 | #include <asm/sections.h> |
| 21 | #include <asm/pci-bridge.h> |
| 22 | #include <asm/byteorder.h> |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 23 | #include <asm/uaccess.h> |
| 24 | #include <asm/machdep.h> |
| 25 | |
| 26 | #undef DEBUG |
| 27 | |
| 28 | #ifdef DEBUG |
| 29 | #define DBG(x...) printk(x) |
| 30 | #else |
| 31 | #define DBG(x...) |
| 32 | #endif |
| 33 | |
| 34 | unsigned long isa_io_base = 0; |
| 35 | unsigned long isa_mem_base = 0; |
| 36 | unsigned long pci_dram_offset = 0; |
| 37 | int pcibios_assign_bus_offset = 1; |
| 38 | |
| 39 | void pcibios_make_OF_bus_map(void); |
| 40 | |
| 41 | static int pci_relocate_bridge_resource(struct pci_bus *bus, int i); |
| 42 | static int probe_resource(struct pci_bus *parent, struct resource *pr, |
| 43 | struct resource *res, struct resource **conflict); |
| 44 | static void update_bridge_base(struct pci_bus *bus, int i); |
| 45 | static void pcibios_fixup_resources(struct pci_dev* dev); |
| 46 | static void fixup_broken_pcnet32(struct pci_dev* dev); |
| 47 | static int reparent_resources(struct resource *parent, struct resource *res); |
| 48 | static void fixup_cpc710_pci64(struct pci_dev* dev); |
| 49 | #ifdef CONFIG_PPC_OF |
| 50 | static u8* pci_to_OF_bus_map; |
| 51 | #endif |
| 52 | |
| 53 | /* By default, we don't re-assign bus numbers. We do this only on |
| 54 | * some pmacs |
| 55 | */ |
| 56 | int pci_assign_all_buses; |
| 57 | |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame^] | 58 | LIST_HEAD(hose_list); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 59 | |
| 60 | static int pci_bus_count; |
| 61 | |
| 62 | static void |
| 63 | fixup_broken_pcnet32(struct pci_dev* dev) |
| 64 | { |
| 65 | if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) { |
| 66 | dev->vendor = PCI_VENDOR_ID_AMD; |
| 67 | pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD); |
| 68 | } |
| 69 | } |
| 70 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32); |
| 71 | |
| 72 | static void |
| 73 | fixup_cpc710_pci64(struct pci_dev* dev) |
| 74 | { |
| 75 | /* Hide the PCI64 BARs from the kernel as their content doesn't |
| 76 | * fit well in the resource management |
| 77 | */ |
| 78 | dev->resource[0].start = dev->resource[0].end = 0; |
| 79 | dev->resource[0].flags = 0; |
| 80 | dev->resource[1].start = dev->resource[1].end = 0; |
| 81 | dev->resource[1].flags = 0; |
| 82 | } |
| 83 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64); |
| 84 | |
| 85 | static void |
| 86 | pcibios_fixup_resources(struct pci_dev *dev) |
| 87 | { |
| 88 | struct pci_controller* hose = (struct pci_controller *)dev->sysdata; |
| 89 | int i; |
| 90 | unsigned long offset; |
| 91 | |
| 92 | if (!hose) { |
| 93 | printk(KERN_ERR "No hose for PCI dev %s!\n", pci_name(dev)); |
| 94 | return; |
| 95 | } |
| 96 | for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { |
| 97 | struct resource *res = dev->resource + i; |
| 98 | if (!res->flags) |
| 99 | continue; |
| 100 | if (res->end == 0xffffffff) { |
Greg Kroah-Hartman | 685143ac | 2006-06-12 15:18:31 -0700 | [diff] [blame] | 101 | DBG("PCI:%s Resource %d [%016llx-%016llx] is unassigned\n", |
Sergei Shtylyov | 872455e | 2006-12-03 20:52:27 +0300 | [diff] [blame] | 102 | pci_name(dev), i, (u64)res->start, (u64)res->end); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 103 | res->end -= res->start; |
| 104 | res->start = 0; |
| 105 | res->flags |= IORESOURCE_UNSET; |
| 106 | continue; |
| 107 | } |
| 108 | offset = 0; |
| 109 | if (res->flags & IORESOURCE_MEM) { |
| 110 | offset = hose->pci_mem_offset; |
| 111 | } else if (res->flags & IORESOURCE_IO) { |
| 112 | offset = (unsigned long) hose->io_base_virt |
| 113 | - isa_io_base; |
| 114 | } |
| 115 | if (offset != 0) { |
| 116 | res->start += offset; |
| 117 | res->end += offset; |
Sergei Shtylyov | 872455e | 2006-12-03 20:52:27 +0300 | [diff] [blame] | 118 | DBG("Fixup res %d (%lx) of dev %s: %llx -> %llx\n", |
| 119 | i, res->flags, pci_name(dev), |
| 120 | (u64)res->start - offset, (u64)res->start); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | /* Call machine specific resource fixup */ |
| 125 | if (ppc_md.pcibios_fixup_resources) |
| 126 | ppc_md.pcibios_fixup_resources(dev); |
| 127 | } |
| 128 | DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources); |
| 129 | |
| 130 | void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region, |
| 131 | struct resource *res) |
| 132 | { |
| 133 | unsigned long offset = 0; |
| 134 | struct pci_controller *hose = dev->sysdata; |
| 135 | |
| 136 | if (hose && res->flags & IORESOURCE_IO) |
| 137 | offset = (unsigned long)hose->io_base_virt - isa_io_base; |
| 138 | else if (hose && res->flags & IORESOURCE_MEM) |
| 139 | offset = hose->pci_mem_offset; |
| 140 | region->start = res->start - offset; |
| 141 | region->end = res->end - offset; |
| 142 | } |
| 143 | EXPORT_SYMBOL(pcibios_resource_to_bus); |
| 144 | |
| 145 | void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res, |
| 146 | struct pci_bus_region *region) |
| 147 | { |
| 148 | unsigned long offset = 0; |
| 149 | struct pci_controller *hose = dev->sysdata; |
| 150 | |
| 151 | if (hose && res->flags & IORESOURCE_IO) |
| 152 | offset = (unsigned long)hose->io_base_virt - isa_io_base; |
| 153 | else if (hose && res->flags & IORESOURCE_MEM) |
| 154 | offset = hose->pci_mem_offset; |
| 155 | res->start = region->start + offset; |
| 156 | res->end = region->end + offset; |
| 157 | } |
| 158 | EXPORT_SYMBOL(pcibios_bus_to_resource); |
| 159 | |
| 160 | /* |
| 161 | * We need to avoid collisions with `mirrored' VGA ports |
| 162 | * and other strange ISA hardware, so we always want the |
| 163 | * addresses to be allocated in the 0x000-0x0ff region |
| 164 | * modulo 0x400. |
| 165 | * |
| 166 | * Why? Because some silly external IO cards only decode |
| 167 | * the low 10 bits of the IO address. The 0x00-0xff region |
| 168 | * is reserved for motherboard devices that decode all 16 |
| 169 | * bits, so it's ok to allocate at, say, 0x2800-0x28ff, |
| 170 | * but we want to try to avoid allocating at 0x2900-0x2bff |
| 171 | * which might have be mirrored at 0x0100-0x03ff.. |
| 172 | */ |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 173 | void pcibios_align_resource(void *data, struct resource *res, |
| 174 | resource_size_t size, resource_size_t align) |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 175 | { |
| 176 | struct pci_dev *dev = data; |
| 177 | |
| 178 | if (res->flags & IORESOURCE_IO) { |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 179 | resource_size_t start = res->start; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 180 | |
| 181 | if (size > 0x100) { |
| 182 | printk(KERN_ERR "PCI: I/O Region %s/%d too large" |
Greg Kroah-Hartman | 685143ac | 2006-06-12 15:18:31 -0700 | [diff] [blame] | 183 | " (%lld bytes)\n", pci_name(dev), |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 184 | dev->resource - res, (unsigned long long)size); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | if (start & 0x300) { |
| 188 | start = (start + 0x3ff) & ~0x3ff; |
| 189 | res->start = start; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | EXPORT_SYMBOL(pcibios_align_resource); |
| 194 | |
| 195 | /* |
| 196 | * Handle resources of PCI devices. If the world were perfect, we could |
| 197 | * just allocate all the resource regions and do nothing more. It isn't. |
| 198 | * On the other hand, we cannot just re-allocate all devices, as it would |
| 199 | * require us to know lots of host bridge internals. So we attempt to |
| 200 | * keep as much of the original configuration as possible, but tweak it |
| 201 | * when it's found to be wrong. |
| 202 | * |
| 203 | * Known BIOS problems we have to work around: |
| 204 | * - I/O or memory regions not configured |
| 205 | * - regions configured, but not enabled in the command register |
| 206 | * - bogus I/O addresses above 64K used |
| 207 | * - expansion ROMs left enabled (this may sound harmless, but given |
| 208 | * the fact the PCI specs explicitly allow address decoders to be |
| 209 | * shared between expansion ROMs and other resource regions, it's |
| 210 | * at least dangerous) |
| 211 | * |
| 212 | * Our solution: |
| 213 | * (1) Allocate resources for all buses behind PCI-to-PCI bridges. |
| 214 | * This gives us fixed barriers on where we can allocate. |
| 215 | * (2) Allocate resources for all enabled devices. If there is |
| 216 | * a collision, just mark the resource as unallocated. Also |
| 217 | * disable expansion ROMs during this step. |
| 218 | * (3) Try to allocate resources for disabled devices. If the |
| 219 | * resources were assigned correctly, everything goes well, |
| 220 | * if they weren't, they won't disturb allocation of other |
| 221 | * resources. |
| 222 | * (4) Assign new addresses to resources which were either |
| 223 | * not configured at all or misconfigured. If explicitly |
| 224 | * requested by the user, configure expansion ROM address |
| 225 | * as well. |
| 226 | */ |
| 227 | |
| 228 | static void __init |
| 229 | pcibios_allocate_bus_resources(struct list_head *bus_list) |
| 230 | { |
| 231 | struct pci_bus *bus; |
| 232 | int i; |
| 233 | struct resource *res, *pr; |
| 234 | |
| 235 | /* Depth-First Search on bus tree */ |
| 236 | list_for_each_entry(bus, bus_list, node) { |
| 237 | for (i = 0; i < 4; ++i) { |
| 238 | if ((res = bus->resource[i]) == NULL || !res->flags |
| 239 | || res->start > res->end) |
| 240 | continue; |
| 241 | if (bus->parent == NULL) |
| 242 | pr = (res->flags & IORESOURCE_IO)? |
| 243 | &ioport_resource: &iomem_resource; |
| 244 | else { |
| 245 | pr = pci_find_parent_resource(bus->self, res); |
| 246 | if (pr == res) { |
| 247 | /* this happens when the generic PCI |
| 248 | * code (wrongly) decides that this |
| 249 | * bridge is transparent -- paulus |
| 250 | */ |
| 251 | continue; |
| 252 | } |
| 253 | } |
| 254 | |
Greg Kroah-Hartman | 685143ac | 2006-06-12 15:18:31 -0700 | [diff] [blame] | 255 | DBG("PCI: bridge rsrc %llx..%llx (%lx), parent %p\n", |
Sergei Shtylyov | 872455e | 2006-12-03 20:52:27 +0300 | [diff] [blame] | 256 | (u64)res->start, (u64)res->end, res->flags, pr); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 257 | if (pr) { |
| 258 | if (request_resource(pr, res) == 0) |
| 259 | continue; |
| 260 | /* |
| 261 | * Must be a conflict with an existing entry. |
| 262 | * Move that entry (or entries) under the |
| 263 | * bridge resource and try again. |
| 264 | */ |
| 265 | if (reparent_resources(pr, res) == 0) |
| 266 | continue; |
| 267 | } |
| 268 | printk(KERN_ERR "PCI: Cannot allocate resource region " |
| 269 | "%d of PCI bridge %d\n", i, bus->number); |
| 270 | if (pci_relocate_bridge_resource(bus, i)) |
| 271 | bus->resource[i] = NULL; |
| 272 | } |
| 273 | pcibios_allocate_bus_resources(&bus->children); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * Reparent resource children of pr that conflict with res |
| 279 | * under res, and make res replace those children. |
| 280 | */ |
| 281 | static int __init |
| 282 | reparent_resources(struct resource *parent, struct resource *res) |
| 283 | { |
| 284 | struct resource *p, **pp; |
| 285 | struct resource **firstpp = NULL; |
| 286 | |
| 287 | for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) { |
| 288 | if (p->end < res->start) |
| 289 | continue; |
| 290 | if (res->end < p->start) |
| 291 | break; |
| 292 | if (p->start < res->start || p->end > res->end) |
| 293 | return -1; /* not completely contained */ |
| 294 | if (firstpp == NULL) |
| 295 | firstpp = pp; |
| 296 | } |
| 297 | if (firstpp == NULL) |
| 298 | return -1; /* didn't find any conflicting entries? */ |
| 299 | res->parent = parent; |
| 300 | res->child = *firstpp; |
| 301 | res->sibling = *pp; |
| 302 | *firstpp = res; |
| 303 | *pp = NULL; |
| 304 | for (p = res->child; p != NULL; p = p->sibling) { |
| 305 | p->parent = res; |
Greg Kroah-Hartman | 685143ac | 2006-06-12 15:18:31 -0700 | [diff] [blame] | 306 | DBG(KERN_INFO "PCI: reparented %s [%llx..%llx] under %s\n", |
Sergei Shtylyov | 872455e | 2006-12-03 20:52:27 +0300 | [diff] [blame] | 307 | p->name, (u64)p->start, (u64)p->end, res->name); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 308 | } |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | * A bridge has been allocated a range which is outside the range |
| 314 | * of its parent bridge, so it needs to be moved. |
| 315 | */ |
| 316 | static int __init |
| 317 | pci_relocate_bridge_resource(struct pci_bus *bus, int i) |
| 318 | { |
| 319 | struct resource *res, *pr, *conflict; |
| 320 | unsigned long try, size; |
| 321 | int j; |
| 322 | struct pci_bus *parent = bus->parent; |
| 323 | |
| 324 | if (parent == NULL) { |
| 325 | /* shouldn't ever happen */ |
| 326 | printk(KERN_ERR "PCI: can't move host bridge resource\n"); |
| 327 | return -1; |
| 328 | } |
| 329 | res = bus->resource[i]; |
| 330 | if (res == NULL) |
| 331 | return -1; |
| 332 | pr = NULL; |
| 333 | for (j = 0; j < 4; j++) { |
| 334 | struct resource *r = parent->resource[j]; |
| 335 | if (!r) |
| 336 | continue; |
| 337 | if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM)) |
| 338 | continue; |
| 339 | if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH)) { |
| 340 | pr = r; |
| 341 | break; |
| 342 | } |
| 343 | if (res->flags & IORESOURCE_PREFETCH) |
| 344 | pr = r; |
| 345 | } |
| 346 | if (pr == NULL) |
| 347 | return -1; |
| 348 | size = res->end - res->start; |
| 349 | if (pr->start > pr->end || size > pr->end - pr->start) |
| 350 | return -1; |
| 351 | try = pr->end; |
| 352 | for (;;) { |
| 353 | res->start = try - size; |
| 354 | res->end = try; |
| 355 | if (probe_resource(bus->parent, pr, res, &conflict) == 0) |
| 356 | break; |
| 357 | if (conflict->start <= pr->start + size) |
| 358 | return -1; |
| 359 | try = conflict->start - 1; |
| 360 | } |
| 361 | if (request_resource(pr, res)) { |
Greg Kroah-Hartman | 685143ac | 2006-06-12 15:18:31 -0700 | [diff] [blame] | 362 | DBG(KERN_ERR "PCI: huh? couldn't move to %llx..%llx\n", |
Sergei Shtylyov | 872455e | 2006-12-03 20:52:27 +0300 | [diff] [blame] | 363 | (u64)res->start, (u64)res->end); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 364 | return -1; /* "can't happen" */ |
| 365 | } |
| 366 | update_bridge_base(bus, i); |
Greg Kroah-Hartman | 685143ac | 2006-06-12 15:18:31 -0700 | [diff] [blame] | 367 | printk(KERN_INFO "PCI: bridge %d resource %d moved to %llx..%llx\n", |
| 368 | bus->number, i, (unsigned long long)res->start, |
| 369 | (unsigned long long)res->end); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | static int __init |
| 374 | probe_resource(struct pci_bus *parent, struct resource *pr, |
| 375 | struct resource *res, struct resource **conflict) |
| 376 | { |
| 377 | struct pci_bus *bus; |
| 378 | struct pci_dev *dev; |
| 379 | struct resource *r; |
| 380 | int i; |
| 381 | |
| 382 | for (r = pr->child; r != NULL; r = r->sibling) { |
| 383 | if (r->end >= res->start && res->end >= r->start) { |
| 384 | *conflict = r; |
| 385 | return 1; |
| 386 | } |
| 387 | } |
| 388 | list_for_each_entry(bus, &parent->children, node) { |
| 389 | for (i = 0; i < 4; ++i) { |
| 390 | if ((r = bus->resource[i]) == NULL) |
| 391 | continue; |
| 392 | if (!r->flags || r->start > r->end || r == res) |
| 393 | continue; |
| 394 | if (pci_find_parent_resource(bus->self, r) != pr) |
| 395 | continue; |
| 396 | if (r->end >= res->start && res->end >= r->start) { |
| 397 | *conflict = r; |
| 398 | return 1; |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | list_for_each_entry(dev, &parent->devices, bus_list) { |
| 403 | for (i = 0; i < 6; ++i) { |
| 404 | r = &dev->resource[i]; |
| 405 | if (!r->flags || (r->flags & IORESOURCE_UNSET)) |
| 406 | continue; |
| 407 | if (pci_find_parent_resource(dev, r) != pr) |
| 408 | continue; |
| 409 | if (r->end >= res->start && res->end >= r->start) { |
| 410 | *conflict = r; |
| 411 | return 1; |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | static void __init |
| 419 | update_bridge_base(struct pci_bus *bus, int i) |
| 420 | { |
| 421 | struct resource *res = bus->resource[i]; |
| 422 | u8 io_base_lo, io_limit_lo; |
| 423 | u16 mem_base, mem_limit; |
| 424 | u16 cmd; |
| 425 | unsigned long start, end, off; |
| 426 | struct pci_dev *dev = bus->self; |
| 427 | struct pci_controller *hose = dev->sysdata; |
| 428 | |
| 429 | if (!hose) { |
| 430 | printk("update_bridge_base: no hose?\n"); |
| 431 | return; |
| 432 | } |
| 433 | pci_read_config_word(dev, PCI_COMMAND, &cmd); |
| 434 | pci_write_config_word(dev, PCI_COMMAND, |
| 435 | cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)); |
| 436 | if (res->flags & IORESOURCE_IO) { |
| 437 | off = (unsigned long) hose->io_base_virt - isa_io_base; |
| 438 | start = res->start - off; |
| 439 | end = res->end - off; |
| 440 | io_base_lo = (start >> 8) & PCI_IO_RANGE_MASK; |
| 441 | io_limit_lo = (end >> 8) & PCI_IO_RANGE_MASK; |
Randy Vinson | 60b2a46 | 2006-10-12 13:36:23 -0700 | [diff] [blame] | 442 | if (end > 0xffff) |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 443 | io_base_lo |= PCI_IO_RANGE_TYPE_32; |
Randy Vinson | 60b2a46 | 2006-10-12 13:36:23 -0700 | [diff] [blame] | 444 | else |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 445 | io_base_lo |= PCI_IO_RANGE_TYPE_16; |
Randy Vinson | 60b2a46 | 2006-10-12 13:36:23 -0700 | [diff] [blame] | 446 | pci_write_config_word(dev, PCI_IO_BASE_UPPER16, |
| 447 | start >> 16); |
| 448 | pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16, |
| 449 | end >> 16); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 450 | pci_write_config_byte(dev, PCI_IO_BASE, io_base_lo); |
| 451 | pci_write_config_byte(dev, PCI_IO_LIMIT, io_limit_lo); |
| 452 | |
| 453 | } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH)) |
| 454 | == IORESOURCE_MEM) { |
| 455 | off = hose->pci_mem_offset; |
| 456 | mem_base = ((res->start - off) >> 16) & PCI_MEMORY_RANGE_MASK; |
| 457 | mem_limit = ((res->end - off) >> 16) & PCI_MEMORY_RANGE_MASK; |
| 458 | pci_write_config_word(dev, PCI_MEMORY_BASE, mem_base); |
| 459 | pci_write_config_word(dev, PCI_MEMORY_LIMIT, mem_limit); |
| 460 | |
| 461 | } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH)) |
| 462 | == (IORESOURCE_MEM | IORESOURCE_PREFETCH)) { |
| 463 | off = hose->pci_mem_offset; |
| 464 | mem_base = ((res->start - off) >> 16) & PCI_PREF_RANGE_MASK; |
| 465 | mem_limit = ((res->end - off) >> 16) & PCI_PREF_RANGE_MASK; |
| 466 | pci_write_config_word(dev, PCI_PREF_MEMORY_BASE, mem_base); |
| 467 | pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, mem_limit); |
| 468 | |
| 469 | } else { |
| 470 | DBG(KERN_ERR "PCI: ugh, bridge %s res %d has flags=%lx\n", |
| 471 | pci_name(dev), i, res->flags); |
| 472 | } |
| 473 | pci_write_config_word(dev, PCI_COMMAND, cmd); |
| 474 | } |
| 475 | |
| 476 | static inline void alloc_resource(struct pci_dev *dev, int idx) |
| 477 | { |
| 478 | struct resource *pr, *r = &dev->resource[idx]; |
| 479 | |
Greg Kroah-Hartman | 685143ac | 2006-06-12 15:18:31 -0700 | [diff] [blame] | 480 | DBG("PCI:%s: Resource %d: %016llx-%016llx (f=%lx)\n", |
Sergei Shtylyov | 872455e | 2006-12-03 20:52:27 +0300 | [diff] [blame] | 481 | pci_name(dev), idx, (u64)r->start, (u64)r->end, r->flags); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 482 | pr = pci_find_parent_resource(dev, r); |
| 483 | if (!pr || request_resource(pr, r) < 0) { |
| 484 | printk(KERN_ERR "PCI: Cannot allocate resource region %d" |
| 485 | " of device %s\n", idx, pci_name(dev)); |
| 486 | if (pr) |
Greg Kroah-Hartman | 685143ac | 2006-06-12 15:18:31 -0700 | [diff] [blame] | 487 | DBG("PCI: parent is %p: %016llx-%016llx (f=%lx)\n", |
Sergei Shtylyov | 872455e | 2006-12-03 20:52:27 +0300 | [diff] [blame] | 488 | pr, (u64)pr->start, (u64)pr->end, pr->flags); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 489 | /* We'll assign a new address later */ |
| 490 | r->flags |= IORESOURCE_UNSET; |
| 491 | r->end -= r->start; |
| 492 | r->start = 0; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | static void __init |
| 497 | pcibios_allocate_resources(int pass) |
| 498 | { |
| 499 | struct pci_dev *dev = NULL; |
| 500 | int idx, disabled; |
| 501 | u16 command; |
| 502 | struct resource *r; |
| 503 | |
| 504 | for_each_pci_dev(dev) { |
| 505 | pci_read_config_word(dev, PCI_COMMAND, &command); |
| 506 | for (idx = 0; idx < 6; idx++) { |
| 507 | r = &dev->resource[idx]; |
| 508 | if (r->parent) /* Already allocated */ |
| 509 | continue; |
| 510 | if (!r->flags || (r->flags & IORESOURCE_UNSET)) |
| 511 | continue; /* Not assigned at all */ |
| 512 | if (r->flags & IORESOURCE_IO) |
| 513 | disabled = !(command & PCI_COMMAND_IO); |
| 514 | else |
| 515 | disabled = !(command & PCI_COMMAND_MEMORY); |
| 516 | if (pass == disabled) |
| 517 | alloc_resource(dev, idx); |
| 518 | } |
| 519 | if (pass) |
| 520 | continue; |
| 521 | r = &dev->resource[PCI_ROM_RESOURCE]; |
| 522 | if (r->flags & IORESOURCE_ROM_ENABLE) { |
| 523 | /* Turn the ROM off, leave the resource region, but keep it unregistered. */ |
| 524 | u32 reg; |
| 525 | DBG("PCI: Switching off ROM of %s\n", pci_name(dev)); |
| 526 | r->flags &= ~IORESOURCE_ROM_ENABLE; |
| 527 | pci_read_config_dword(dev, dev->rom_base_reg, ®); |
| 528 | pci_write_config_dword(dev, dev->rom_base_reg, |
| 529 | reg & ~PCI_ROM_ADDRESS_ENABLE); |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | static void __init |
| 535 | pcibios_assign_resources(void) |
| 536 | { |
| 537 | struct pci_dev *dev = NULL; |
| 538 | int idx; |
| 539 | struct resource *r; |
| 540 | |
| 541 | for_each_pci_dev(dev) { |
| 542 | int class = dev->class >> 8; |
| 543 | |
| 544 | /* Don't touch classless devices and host bridges */ |
| 545 | if (!class || class == PCI_CLASS_BRIDGE_HOST) |
| 546 | continue; |
| 547 | |
| 548 | for (idx = 0; idx < 6; idx++) { |
| 549 | r = &dev->resource[idx]; |
| 550 | |
| 551 | /* |
| 552 | * We shall assign a new address to this resource, |
| 553 | * either because the BIOS (sic) forgot to do so |
| 554 | * or because we have decided the old address was |
| 555 | * unusable for some reason. |
| 556 | */ |
| 557 | if ((r->flags & IORESOURCE_UNSET) && r->end && |
| 558 | (!ppc_md.pcibios_enable_device_hook || |
| 559 | !ppc_md.pcibios_enable_device_hook(dev, 1))) { |
| 560 | r->flags &= ~IORESOURCE_UNSET; |
| 561 | pci_assign_resource(dev, idx); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | #if 0 /* don't assign ROMs */ |
| 566 | r = &dev->resource[PCI_ROM_RESOURCE]; |
| 567 | r->end -= r->start; |
| 568 | r->start = 0; |
| 569 | if (r->end) |
| 570 | pci_assign_resource(dev, PCI_ROM_RESOURCE); |
| 571 | #endif |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | |
| 576 | int |
| 577 | pcibios_enable_resources(struct pci_dev *dev, int mask) |
| 578 | { |
| 579 | u16 cmd, old_cmd; |
| 580 | int idx; |
| 581 | struct resource *r; |
| 582 | |
| 583 | pci_read_config_word(dev, PCI_COMMAND, &cmd); |
| 584 | old_cmd = cmd; |
| 585 | for (idx=0; idx<6; idx++) { |
| 586 | /* Only set up the requested stuff */ |
| 587 | if (!(mask & (1<<idx))) |
| 588 | continue; |
| 589 | |
| 590 | r = &dev->resource[idx]; |
| 591 | if (r->flags & IORESOURCE_UNSET) { |
| 592 | printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev)); |
| 593 | return -EINVAL; |
| 594 | } |
| 595 | if (r->flags & IORESOURCE_IO) |
| 596 | cmd |= PCI_COMMAND_IO; |
| 597 | if (r->flags & IORESOURCE_MEM) |
| 598 | cmd |= PCI_COMMAND_MEMORY; |
| 599 | } |
| 600 | if (dev->resource[PCI_ROM_RESOURCE].start) |
| 601 | cmd |= PCI_COMMAND_MEMORY; |
| 602 | if (cmd != old_cmd) { |
| 603 | printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd); |
| 604 | pci_write_config_word(dev, PCI_COMMAND, cmd); |
| 605 | } |
| 606 | return 0; |
| 607 | } |
| 608 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 609 | #ifdef CONFIG_PPC_OF |
| 610 | /* |
| 611 | * Functions below are used on OpenFirmware machines. |
| 612 | */ |
| 613 | static void |
| 614 | make_one_node_map(struct device_node* node, u8 pci_bus) |
| 615 | { |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 616 | const int *bus_range; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 617 | int len; |
| 618 | |
| 619 | if (pci_bus >= pci_bus_count) |
| 620 | return; |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 621 | bus_range = of_get_property(node, "bus-range", &len); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 622 | if (bus_range == NULL || len < 2 * sizeof(int)) { |
| 623 | printk(KERN_WARNING "Can't get bus-range for %s, " |
| 624 | "assuming it starts at 0\n", node->full_name); |
| 625 | pci_to_OF_bus_map[pci_bus] = 0; |
| 626 | } else |
| 627 | pci_to_OF_bus_map[pci_bus] = bus_range[0]; |
| 628 | |
| 629 | for (node=node->child; node != 0;node = node->sibling) { |
| 630 | struct pci_dev* dev; |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 631 | const unsigned int *class_code, *reg; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 632 | |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 633 | class_code = of_get_property(node, "class-code", NULL); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 634 | if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI && |
| 635 | (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) |
| 636 | continue; |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 637 | reg = of_get_property(node, "reg", NULL); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 638 | if (!reg) |
| 639 | continue; |
Alan Cox | ab46276 | 2007-04-23 14:47:59 +0100 | [diff] [blame] | 640 | dev = pci_get_bus_and_slot(pci_bus, ((reg[0] >> 8) & 0xff)); |
| 641 | if (!dev || !dev->subordinate) { |
| 642 | pci_dev_put(dev); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 643 | continue; |
Alan Cox | ab46276 | 2007-04-23 14:47:59 +0100 | [diff] [blame] | 644 | } |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 645 | make_one_node_map(node, dev->subordinate->number); |
Alan Cox | ab46276 | 2007-04-23 14:47:59 +0100 | [diff] [blame] | 646 | pci_dev_put(dev); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | |
| 650 | void |
| 651 | pcibios_make_OF_bus_map(void) |
| 652 | { |
| 653 | int i; |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame^] | 654 | struct pci_controller *hose, *tmp; |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 655 | struct property *map_prop; |
Stephen Rothwell | 8c8dc32 | 2007-04-24 13:50:55 +1000 | [diff] [blame] | 656 | struct device_node *dn; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 657 | |
Robert P. J. Day | 5cbded5 | 2006-12-13 00:35:56 -0800 | [diff] [blame] | 658 | pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 659 | if (!pci_to_OF_bus_map) { |
| 660 | printk(KERN_ERR "Can't allocate OF bus map !\n"); |
| 661 | return; |
| 662 | } |
| 663 | |
| 664 | /* We fill the bus map with invalid values, that helps |
| 665 | * debugging. |
| 666 | */ |
| 667 | for (i=0; i<pci_bus_count; i++) |
| 668 | pci_to_OF_bus_map[i] = 0xff; |
| 669 | |
| 670 | /* For each hose, we begin searching bridges */ |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame^] | 671 | list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 672 | struct device_node* node; |
| 673 | node = (struct device_node *)hose->arch_data; |
| 674 | if (!node) |
| 675 | continue; |
| 676 | make_one_node_map(node, hose->first_busno); |
| 677 | } |
Stephen Rothwell | 8c8dc32 | 2007-04-24 13:50:55 +1000 | [diff] [blame] | 678 | dn = of_find_node_by_path("/"); |
| 679 | map_prop = of_find_property(dn, "pci-OF-bus-map", NULL); |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 680 | if (map_prop) { |
| 681 | BUG_ON(pci_bus_count > map_prop->length); |
| 682 | memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count); |
| 683 | } |
Stephen Rothwell | 8c8dc32 | 2007-04-24 13:50:55 +1000 | [diff] [blame] | 684 | of_node_put(dn); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 685 | #ifdef DEBUG |
| 686 | printk("PCI->OF bus map:\n"); |
| 687 | for (i=0; i<pci_bus_count; i++) { |
| 688 | if (pci_to_OF_bus_map[i] == 0xff) |
| 689 | continue; |
| 690 | printk("%d -> %d\n", i, pci_to_OF_bus_map[i]); |
| 691 | } |
| 692 | #endif |
| 693 | } |
| 694 | |
| 695 | typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data); |
| 696 | |
| 697 | static struct device_node* |
| 698 | scan_OF_pci_childs(struct device_node* node, pci_OF_scan_iterator filter, void* data) |
| 699 | { |
| 700 | struct device_node* sub_node; |
| 701 | |
| 702 | for (; node != 0;node = node->sibling) { |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 703 | const unsigned int *class_code; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 704 | |
| 705 | if (filter(node, data)) |
| 706 | return node; |
| 707 | |
| 708 | /* For PCI<->PCI bridges or CardBus bridges, we go down |
| 709 | * Note: some OFs create a parent node "multifunc-device" as |
| 710 | * a fake root for all functions of a multi-function device, |
| 711 | * we go down them as well. |
| 712 | */ |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 713 | class_code = of_get_property(node, "class-code", NULL); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 714 | if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI && |
| 715 | (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) && |
| 716 | strcmp(node->name, "multifunc-device")) |
| 717 | continue; |
| 718 | sub_node = scan_OF_pci_childs(node->child, filter, data); |
| 719 | if (sub_node) |
| 720 | return sub_node; |
| 721 | } |
| 722 | return NULL; |
| 723 | } |
| 724 | |
Benjamin Herrenschmidt | dae4828 | 2006-12-11 14:09:07 +1100 | [diff] [blame] | 725 | static struct device_node *scan_OF_for_pci_dev(struct device_node *parent, |
| 726 | unsigned int devfn) |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 727 | { |
Benjamin Herrenschmidt | dae4828 | 2006-12-11 14:09:07 +1100 | [diff] [blame] | 728 | struct device_node *np = NULL; |
| 729 | const u32 *reg; |
| 730 | unsigned int psize; |
| 731 | |
| 732 | while ((np = of_get_next_child(parent, np)) != NULL) { |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 733 | reg = of_get_property(np, "reg", &psize); |
Benjamin Herrenschmidt | dae4828 | 2006-12-11 14:09:07 +1100 | [diff] [blame] | 734 | if (reg == NULL || psize < 4) |
| 735 | continue; |
| 736 | if (((reg[0] >> 8) & 0xff) == devfn) |
| 737 | return np; |
| 738 | } |
| 739 | return NULL; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 740 | } |
| 741 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 742 | |
Benjamin Herrenschmidt | dae4828 | 2006-12-11 14:09:07 +1100 | [diff] [blame] | 743 | static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus) |
| 744 | { |
| 745 | struct device_node *parent, *np; |
| 746 | |
| 747 | /* Are we a root bus ? */ |
| 748 | if (bus->self == NULL || bus->parent == NULL) { |
Kumar Gala | 0b1d40c | 2007-06-27 10:27:33 -0500 | [diff] [blame] | 749 | struct pci_controller *hose = pci_bus_to_host(bus); |
Benjamin Herrenschmidt | dae4828 | 2006-12-11 14:09:07 +1100 | [diff] [blame] | 750 | if (hose == NULL) |
| 751 | return NULL; |
| 752 | return of_node_get(hose->arch_data); |
| 753 | } |
| 754 | |
| 755 | /* not a root bus, we need to get our parent */ |
| 756 | parent = scan_OF_for_pci_bus(bus->parent); |
| 757 | if (parent == NULL) |
| 758 | return NULL; |
| 759 | |
| 760 | /* now iterate for children for a match */ |
| 761 | np = scan_OF_for_pci_dev(parent, bus->self->devfn); |
| 762 | of_node_put(parent); |
| 763 | |
Benjamin Herrenschmidt | dae4828 | 2006-12-11 14:09:07 +1100 | [diff] [blame] | 764 | return np; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | /* |
| 768 | * Scans the OF tree for a device node matching a PCI device |
| 769 | */ |
| 770 | struct device_node * |
| 771 | pci_busdev_to_OF_node(struct pci_bus *bus, int devfn) |
| 772 | { |
Benjamin Herrenschmidt | dae4828 | 2006-12-11 14:09:07 +1100 | [diff] [blame] | 773 | struct device_node *parent, *np; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 774 | |
| 775 | if (!have_of) |
| 776 | return NULL; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 777 | |
Benjamin Herrenschmidt | dae4828 | 2006-12-11 14:09:07 +1100 | [diff] [blame] | 778 | DBG("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn); |
| 779 | parent = scan_OF_for_pci_bus(bus); |
| 780 | if (parent == NULL) |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 781 | return NULL; |
Benjamin Herrenschmidt | dae4828 | 2006-12-11 14:09:07 +1100 | [diff] [blame] | 782 | DBG(" parent is %s\n", parent ? parent->full_name : "<NULL>"); |
| 783 | np = scan_OF_for_pci_dev(parent, devfn); |
| 784 | of_node_put(parent); |
| 785 | DBG(" result is %s\n", np ? np->full_name : "<NULL>"); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 786 | |
Benjamin Herrenschmidt | dae4828 | 2006-12-11 14:09:07 +1100 | [diff] [blame] | 787 | /* XXX most callers don't release the returned node |
| 788 | * mostly because ppc64 doesn't increase the refcount, |
| 789 | * we need to fix that. |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 790 | */ |
Benjamin Herrenschmidt | dae4828 | 2006-12-11 14:09:07 +1100 | [diff] [blame] | 791 | return np; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 792 | } |
| 793 | EXPORT_SYMBOL(pci_busdev_to_OF_node); |
| 794 | |
| 795 | struct device_node* |
| 796 | pci_device_to_OF_node(struct pci_dev *dev) |
| 797 | { |
| 798 | return pci_busdev_to_OF_node(dev->bus, dev->devfn); |
| 799 | } |
| 800 | EXPORT_SYMBOL(pci_device_to_OF_node); |
| 801 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 802 | static int |
| 803 | find_OF_pci_device_filter(struct device_node* node, void* data) |
| 804 | { |
| 805 | return ((void *)node == data); |
| 806 | } |
| 807 | |
| 808 | /* |
| 809 | * Returns the PCI device matching a given OF node |
| 810 | */ |
| 811 | int |
| 812 | pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn) |
| 813 | { |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 814 | const unsigned int *reg; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 815 | struct pci_controller* hose; |
| 816 | struct pci_dev* dev = NULL; |
| 817 | |
| 818 | if (!have_of) |
| 819 | return -ENODEV; |
| 820 | /* Make sure it's really a PCI device */ |
| 821 | hose = pci_find_hose_for_OF_device(node); |
| 822 | if (!hose || !hose->arch_data) |
| 823 | return -ENODEV; |
| 824 | if (!scan_OF_pci_childs(((struct device_node*)hose->arch_data)->child, |
| 825 | find_OF_pci_device_filter, (void *)node)) |
| 826 | return -ENODEV; |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 827 | reg = of_get_property(node, "reg", NULL); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 828 | if (!reg) |
| 829 | return -ENODEV; |
| 830 | *bus = (reg[0] >> 16) & 0xff; |
| 831 | *devfn = ((reg[0] >> 8) & 0xff); |
| 832 | |
| 833 | /* Ok, here we need some tweak. If we have already renumbered |
| 834 | * all busses, we can't rely on the OF bus number any more. |
| 835 | * the pci_to_OF_bus_map is not enough as several PCI busses |
| 836 | * may match the same OF bus number. |
| 837 | */ |
| 838 | if (!pci_to_OF_bus_map) |
| 839 | return 0; |
| 840 | |
| 841 | for_each_pci_dev(dev) |
| 842 | if (pci_to_OF_bus_map[dev->bus->number] == *bus && |
| 843 | dev->devfn == *devfn) { |
| 844 | *bus = dev->bus->number; |
| 845 | pci_dev_put(dev); |
| 846 | return 0; |
| 847 | } |
| 848 | |
| 849 | return -ENODEV; |
| 850 | } |
| 851 | EXPORT_SYMBOL(pci_device_from_OF_node); |
| 852 | |
| 853 | void __init |
| 854 | pci_process_bridge_OF_ranges(struct pci_controller *hose, |
| 855 | struct device_node *dev, int primary) |
| 856 | { |
| 857 | static unsigned int static_lc_ranges[256] __initdata; |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 858 | const unsigned int *dt_ranges; |
| 859 | unsigned int *lc_ranges, *ranges, *prev, size; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 860 | int rlen = 0, orig_rlen; |
| 861 | int memno = 0; |
| 862 | struct resource *res; |
Stephen Rothwell | a8bda5d | 2007-04-03 10:56:50 +1000 | [diff] [blame] | 863 | int np, na = of_n_addr_cells(dev); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 864 | np = na + 5; |
| 865 | |
| 866 | /* First we try to merge ranges to fix a problem with some pmacs |
| 867 | * that can have more than 3 ranges, fortunately using contiguous |
| 868 | * addresses -- BenH |
| 869 | */ |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 870 | dt_ranges = of_get_property(dev, "ranges", &rlen); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 871 | if (!dt_ranges) |
| 872 | return; |
| 873 | /* Sanity check, though hopefully that never happens */ |
| 874 | if (rlen > sizeof(static_lc_ranges)) { |
| 875 | printk(KERN_WARNING "OF ranges property too large !\n"); |
| 876 | rlen = sizeof(static_lc_ranges); |
| 877 | } |
| 878 | lc_ranges = static_lc_ranges; |
| 879 | memcpy(lc_ranges, dt_ranges, rlen); |
| 880 | orig_rlen = rlen; |
| 881 | |
| 882 | /* Let's work on a copy of the "ranges" property instead of damaging |
| 883 | * the device-tree image in memory |
| 884 | */ |
| 885 | ranges = lc_ranges; |
| 886 | prev = NULL; |
| 887 | while ((rlen -= np * sizeof(unsigned int)) >= 0) { |
| 888 | if (prev) { |
| 889 | if (prev[0] == ranges[0] && prev[1] == ranges[1] && |
| 890 | (prev[2] + prev[na+4]) == ranges[2] && |
| 891 | (prev[na+2] + prev[na+4]) == ranges[na+2]) { |
| 892 | prev[na+4] += ranges[na+4]; |
| 893 | ranges[0] = 0; |
| 894 | ranges += np; |
| 895 | continue; |
| 896 | } |
| 897 | } |
| 898 | prev = ranges; |
| 899 | ranges += np; |
| 900 | } |
| 901 | |
| 902 | /* |
| 903 | * The ranges property is laid out as an array of elements, |
| 904 | * each of which comprises: |
| 905 | * cells 0 - 2: a PCI address |
| 906 | * cells 3 or 3+4: a CPU physical address |
| 907 | * (size depending on dev->n_addr_cells) |
| 908 | * cells 4+5 or 5+6: the size of the range |
| 909 | */ |
| 910 | ranges = lc_ranges; |
| 911 | rlen = orig_rlen; |
| 912 | while (ranges && (rlen -= np * sizeof(unsigned int)) >= 0) { |
| 913 | res = NULL; |
| 914 | size = ranges[na+4]; |
| 915 | switch ((ranges[0] >> 24) & 0x3) { |
| 916 | case 1: /* I/O space */ |
| 917 | if (ranges[2] != 0) |
| 918 | break; |
| 919 | hose->io_base_phys = ranges[na+2]; |
| 920 | /* limit I/O space to 16MB */ |
| 921 | if (size > 0x01000000) |
| 922 | size = 0x01000000; |
| 923 | hose->io_base_virt = ioremap(ranges[na+2], size); |
| 924 | if (primary) |
| 925 | isa_io_base = (unsigned long) hose->io_base_virt; |
| 926 | res = &hose->io_resource; |
| 927 | res->flags = IORESOURCE_IO; |
| 928 | res->start = ranges[2]; |
Greg Kroah-Hartman | 685143ac | 2006-06-12 15:18:31 -0700 | [diff] [blame] | 929 | DBG("PCI: IO 0x%llx -> 0x%llx\n", |
Sergei Shtylyov | 872455e | 2006-12-03 20:52:27 +0300 | [diff] [blame] | 930 | (u64)res->start, (u64)res->start + size - 1); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 931 | break; |
| 932 | case 2: /* memory space */ |
| 933 | memno = 0; |
| 934 | if (ranges[1] == 0 && ranges[2] == 0 |
| 935 | && ranges[na+4] <= (16 << 20)) { |
| 936 | /* 1st 16MB, i.e. ISA memory area */ |
| 937 | if (primary) |
| 938 | isa_mem_base = ranges[na+2]; |
| 939 | memno = 1; |
| 940 | } |
| 941 | while (memno < 3 && hose->mem_resources[memno].flags) |
| 942 | ++memno; |
| 943 | if (memno == 0) |
| 944 | hose->pci_mem_offset = ranges[na+2] - ranges[2]; |
| 945 | if (memno < 3) { |
| 946 | res = &hose->mem_resources[memno]; |
| 947 | res->flags = IORESOURCE_MEM; |
| 948 | if(ranges[0] & 0x40000000) |
| 949 | res->flags |= IORESOURCE_PREFETCH; |
| 950 | res->start = ranges[na+2]; |
Greg Kroah-Hartman | 685143ac | 2006-06-12 15:18:31 -0700 | [diff] [blame] | 951 | DBG("PCI: MEM[%d] 0x%llx -> 0x%llx\n", memno, |
Sergei Shtylyov | 872455e | 2006-12-03 20:52:27 +0300 | [diff] [blame] | 952 | (u64)res->start, (u64)res->start + size - 1); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 953 | } |
| 954 | break; |
| 955 | } |
| 956 | if (res != NULL) { |
| 957 | res->name = dev->full_name; |
| 958 | res->end = res->start + size - 1; |
| 959 | res->parent = NULL; |
| 960 | res->sibling = NULL; |
| 961 | res->child = NULL; |
| 962 | } |
| 963 | ranges += np; |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | /* We create the "pci-OF-bus-map" property now so it appears in the |
| 968 | * /proc device tree |
| 969 | */ |
| 970 | void __init |
| 971 | pci_create_OF_bus_map(void) |
| 972 | { |
| 973 | struct property* of_prop; |
Stephen Rothwell | 8c8dc32 | 2007-04-24 13:50:55 +1000 | [diff] [blame] | 974 | struct device_node *dn; |
| 975 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 976 | of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256); |
Stephen Rothwell | 8c8dc32 | 2007-04-24 13:50:55 +1000 | [diff] [blame] | 977 | if (!of_prop) |
| 978 | return; |
| 979 | dn = of_find_node_by_path("/"); |
| 980 | if (dn) { |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 981 | memset(of_prop, -1, sizeof(struct property) + 256); |
| 982 | of_prop->name = "pci-OF-bus-map"; |
| 983 | of_prop->length = 256; |
Stephen Rothwell | 1a38147 | 2007-04-03 10:58:52 +1000 | [diff] [blame] | 984 | of_prop->value = &of_prop[1]; |
Stephen Rothwell | 8c8dc32 | 2007-04-24 13:50:55 +1000 | [diff] [blame] | 985 | prom_add_property(dn, of_prop); |
| 986 | of_node_put(dn); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 987 | } |
| 988 | } |
| 989 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 990 | #else /* CONFIG_PPC_OF */ |
| 991 | void pcibios_make_OF_bus_map(void) |
| 992 | { |
| 993 | } |
| 994 | #endif /* CONFIG_PPC_OF */ |
| 995 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 996 | #ifdef CONFIG_PPC_PMAC |
| 997 | /* |
| 998 | * This set of routines checks for PCI<->PCI bridges that have closed |
| 999 | * IO resources and have child devices. It tries to re-open an IO |
| 1000 | * window on them. |
| 1001 | * |
| 1002 | * This is a _temporary_ fix to workaround a problem with Apple's OF |
| 1003 | * closing IO windows on P2P bridges when the OF drivers of cards |
| 1004 | * below this bridge don't claim any IO range (typically ATI or |
| 1005 | * Adaptec). |
| 1006 | * |
| 1007 | * A more complete fix would be to use drivers/pci/setup-bus.c, which |
| 1008 | * involves a working pcibios_fixup_pbus_ranges(), some more care about |
| 1009 | * ordering when creating the host bus resources, and maybe a few more |
| 1010 | * minor tweaks |
| 1011 | */ |
| 1012 | |
| 1013 | /* Initialize bridges with base/limit values we have collected */ |
| 1014 | static void __init |
| 1015 | do_update_p2p_io_resource(struct pci_bus *bus, int enable_vga) |
| 1016 | { |
| 1017 | struct pci_dev *bridge = bus->self; |
| 1018 | struct pci_controller* hose = (struct pci_controller *)bridge->sysdata; |
| 1019 | u32 l; |
| 1020 | u16 w; |
| 1021 | struct resource res; |
| 1022 | |
| 1023 | if (bus->resource[0] == NULL) |
| 1024 | return; |
| 1025 | res = *(bus->resource[0]); |
| 1026 | |
| 1027 | DBG("Remapping Bus %d, bridge: %s\n", bus->number, pci_name(bridge)); |
| 1028 | res.start -= ((unsigned long) hose->io_base_virt - isa_io_base); |
| 1029 | res.end -= ((unsigned long) hose->io_base_virt - isa_io_base); |
Greg Kroah-Hartman | 685143ac | 2006-06-12 15:18:31 -0700 | [diff] [blame] | 1030 | DBG(" IO window: %016llx-%016llx\n", res.start, res.end); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 1031 | |
| 1032 | /* Set up the top and bottom of the PCI I/O segment for this bus. */ |
| 1033 | pci_read_config_dword(bridge, PCI_IO_BASE, &l); |
| 1034 | l &= 0xffff000f; |
| 1035 | l |= (res.start >> 8) & 0x00f0; |
| 1036 | l |= res.end & 0xf000; |
| 1037 | pci_write_config_dword(bridge, PCI_IO_BASE, l); |
| 1038 | |
| 1039 | if ((l & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) { |
| 1040 | l = (res.start >> 16) | (res.end & 0xffff0000); |
| 1041 | pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, l); |
| 1042 | } |
| 1043 | |
| 1044 | pci_read_config_word(bridge, PCI_COMMAND, &w); |
| 1045 | w |= PCI_COMMAND_IO; |
| 1046 | pci_write_config_word(bridge, PCI_COMMAND, w); |
| 1047 | |
| 1048 | #if 0 /* Enabling this causes XFree 4.2.0 to hang during PCI probe */ |
| 1049 | if (enable_vga) { |
| 1050 | pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, &w); |
| 1051 | w |= PCI_BRIDGE_CTL_VGA; |
| 1052 | pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, w); |
| 1053 | } |
| 1054 | #endif |
| 1055 | } |
| 1056 | |
| 1057 | /* This function is pretty basic and actually quite broken for the |
| 1058 | * general case, it's enough for us right now though. It's supposed |
| 1059 | * to tell us if we need to open an IO range at all or not and what |
| 1060 | * size. |
| 1061 | */ |
| 1062 | static int __init |
| 1063 | check_for_io_childs(struct pci_bus *bus, struct resource* res, int *found_vga) |
| 1064 | { |
| 1065 | struct pci_dev *dev; |
| 1066 | int i; |
| 1067 | int rc = 0; |
| 1068 | |
Paul Mackerras | 0f582bc | 2006-06-15 18:03:32 +1000 | [diff] [blame] | 1069 | #define push_end(res, mask) do { \ |
| 1070 | BUG_ON((mask+1) & mask); \ |
| 1071 | res->end = (res->end + mask) | mask; \ |
| 1072 | } while (0) |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 1073 | |
| 1074 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 1075 | u16 class = dev->class >> 8; |
| 1076 | |
| 1077 | if (class == PCI_CLASS_DISPLAY_VGA || |
| 1078 | class == PCI_CLASS_NOT_DEFINED_VGA) |
| 1079 | *found_vga = 1; |
| 1080 | if (class >> 8 == PCI_BASE_CLASS_BRIDGE && dev->subordinate) |
| 1081 | rc |= check_for_io_childs(dev->subordinate, res, found_vga); |
| 1082 | if (class == PCI_CLASS_BRIDGE_CARDBUS) |
| 1083 | push_end(res, 0xfff); |
| 1084 | |
| 1085 | for (i=0; i<PCI_NUM_RESOURCES; i++) { |
| 1086 | struct resource *r; |
| 1087 | unsigned long r_size; |
| 1088 | |
| 1089 | if (dev->class >> 8 == PCI_CLASS_BRIDGE_PCI |
| 1090 | && i >= PCI_BRIDGE_RESOURCES) |
| 1091 | continue; |
| 1092 | r = &dev->resource[i]; |
| 1093 | r_size = r->end - r->start; |
| 1094 | if (r_size < 0xfff) |
| 1095 | r_size = 0xfff; |
| 1096 | if (r->flags & IORESOURCE_IO && (r_size) != 0) { |
| 1097 | rc = 1; |
| 1098 | push_end(res, r_size); |
| 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | return rc; |
| 1104 | } |
| 1105 | |
| 1106 | /* Here we scan all P2P bridges of a given level that have a closed |
| 1107 | * IO window. Note that the test for the presence of a VGA card should |
| 1108 | * be improved to take into account already configured P2P bridges, |
| 1109 | * currently, we don't see them and might end up configuring 2 bridges |
| 1110 | * with VGA pass through enabled |
| 1111 | */ |
| 1112 | static void __init |
| 1113 | do_fixup_p2p_level(struct pci_bus *bus) |
| 1114 | { |
| 1115 | struct pci_bus *b; |
| 1116 | int i, parent_io; |
| 1117 | int has_vga = 0; |
| 1118 | |
| 1119 | for (parent_io=0; parent_io<4; parent_io++) |
| 1120 | if (bus->resource[parent_io] |
| 1121 | && bus->resource[parent_io]->flags & IORESOURCE_IO) |
| 1122 | break; |
| 1123 | if (parent_io >= 4) |
| 1124 | return; |
| 1125 | |
| 1126 | list_for_each_entry(b, &bus->children, node) { |
| 1127 | struct pci_dev *d = b->self; |
| 1128 | struct pci_controller* hose = (struct pci_controller *)d->sysdata; |
| 1129 | struct resource *res = b->resource[0]; |
| 1130 | struct resource tmp_res; |
| 1131 | unsigned long max; |
| 1132 | int found_vga = 0; |
| 1133 | |
| 1134 | memset(&tmp_res, 0, sizeof(tmp_res)); |
| 1135 | tmp_res.start = bus->resource[parent_io]->start; |
| 1136 | |
| 1137 | /* We don't let low addresses go through that closed P2P bridge, well, |
| 1138 | * that may not be necessary but I feel safer that way |
| 1139 | */ |
| 1140 | if (tmp_res.start == 0) |
| 1141 | tmp_res.start = 0x1000; |
| 1142 | |
| 1143 | if (!list_empty(&b->devices) && res && res->flags == 0 && |
| 1144 | res != bus->resource[parent_io] && |
| 1145 | (d->class >> 8) == PCI_CLASS_BRIDGE_PCI && |
| 1146 | check_for_io_childs(b, &tmp_res, &found_vga)) { |
| 1147 | u8 io_base_lo; |
| 1148 | |
| 1149 | printk(KERN_INFO "Fixing up IO bus %s\n", b->name); |
| 1150 | |
| 1151 | if (found_vga) { |
| 1152 | if (has_vga) { |
| 1153 | printk(KERN_WARNING "Skipping VGA, already active" |
| 1154 | " on bus segment\n"); |
| 1155 | found_vga = 0; |
| 1156 | } else |
| 1157 | has_vga = 1; |
| 1158 | } |
| 1159 | pci_read_config_byte(d, PCI_IO_BASE, &io_base_lo); |
| 1160 | |
| 1161 | if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) |
| 1162 | max = ((unsigned long) hose->io_base_virt |
| 1163 | - isa_io_base) + 0xffffffff; |
| 1164 | else |
| 1165 | max = ((unsigned long) hose->io_base_virt |
| 1166 | - isa_io_base) + 0xffff; |
| 1167 | |
| 1168 | *res = tmp_res; |
| 1169 | res->flags = IORESOURCE_IO; |
| 1170 | res->name = b->name; |
| 1171 | |
| 1172 | /* Find a resource in the parent where we can allocate */ |
| 1173 | for (i = 0 ; i < 4; i++) { |
| 1174 | struct resource *r = bus->resource[i]; |
| 1175 | if (!r) |
| 1176 | continue; |
| 1177 | if ((r->flags & IORESOURCE_IO) == 0) |
| 1178 | continue; |
Greg Kroah-Hartman | 685143ac | 2006-06-12 15:18:31 -0700 | [diff] [blame] | 1179 | DBG("Trying to allocate from %016llx, size %016llx from parent" |
| 1180 | " res %d: %016llx -> %016llx\n", |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 1181 | res->start, res->end, i, r->start, r->end); |
| 1182 | |
| 1183 | if (allocate_resource(r, res, res->end + 1, res->start, max, |
| 1184 | res->end + 1, NULL, NULL) < 0) { |
| 1185 | DBG("Failed !\n"); |
| 1186 | continue; |
| 1187 | } |
| 1188 | do_update_p2p_io_resource(b, found_vga); |
| 1189 | break; |
| 1190 | } |
| 1191 | } |
| 1192 | do_fixup_p2p_level(b); |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | static void |
| 1197 | pcibios_fixup_p2p_bridges(void) |
| 1198 | { |
| 1199 | struct pci_bus *b; |
| 1200 | |
| 1201 | list_for_each_entry(b, &pci_root_buses, node) |
| 1202 | do_fixup_p2p_level(b); |
| 1203 | } |
| 1204 | |
| 1205 | #endif /* CONFIG_PPC_PMAC */ |
| 1206 | |
| 1207 | static int __init |
| 1208 | pcibios_init(void) |
| 1209 | { |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame^] | 1210 | struct pci_controller *hose, *tmp; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 1211 | struct pci_bus *bus; |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame^] | 1212 | int next_busno = 0; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 1213 | |
| 1214 | printk(KERN_INFO "PCI: Probing PCI hardware\n"); |
| 1215 | |
| 1216 | /* Scan all of the recorded PCI controllers. */ |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame^] | 1217 | list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 1218 | if (pci_assign_all_buses) |
| 1219 | hose->first_busno = next_busno; |
| 1220 | hose->last_busno = 0xff; |
Benjamin Herrenschmidt | 803d457 | 2006-11-11 17:25:07 +1100 | [diff] [blame] | 1221 | bus = pci_scan_bus_parented(hose->parent, hose->first_busno, |
| 1222 | hose->ops, hose); |
| 1223 | if (bus) |
| 1224 | pci_bus_add_devices(bus); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 1225 | hose->last_busno = bus->subordinate; |
| 1226 | if (pci_assign_all_buses || next_busno <= hose->last_busno) |
| 1227 | next_busno = hose->last_busno + pcibios_assign_bus_offset; |
| 1228 | } |
| 1229 | pci_bus_count = next_busno; |
| 1230 | |
| 1231 | /* OpenFirmware based machines need a map of OF bus |
| 1232 | * numbers vs. kernel bus numbers since we may have to |
| 1233 | * remap them. |
| 1234 | */ |
| 1235 | if (pci_assign_all_buses && have_of) |
| 1236 | pcibios_make_OF_bus_map(); |
| 1237 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 1238 | /* Call machine dependent fixup */ |
| 1239 | if (ppc_md.pcibios_fixup) |
| 1240 | ppc_md.pcibios_fixup(); |
| 1241 | |
| 1242 | /* Allocate and assign resources */ |
| 1243 | pcibios_allocate_bus_resources(&pci_root_buses); |
| 1244 | pcibios_allocate_resources(0); |
| 1245 | pcibios_allocate_resources(1); |
| 1246 | #ifdef CONFIG_PPC_PMAC |
| 1247 | pcibios_fixup_p2p_bridges(); |
| 1248 | #endif /* CONFIG_PPC_PMAC */ |
| 1249 | pcibios_assign_resources(); |
| 1250 | |
| 1251 | /* Call machine dependent post-init code */ |
| 1252 | if (ppc_md.pcibios_after_init) |
| 1253 | ppc_md.pcibios_after_init(); |
| 1254 | |
| 1255 | return 0; |
| 1256 | } |
| 1257 | |
| 1258 | subsys_initcall(pcibios_init); |
| 1259 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 1260 | void __init pcibios_fixup_bus(struct pci_bus *bus) |
| 1261 | { |
| 1262 | struct pci_controller *hose = (struct pci_controller *) bus->sysdata; |
| 1263 | unsigned long io_offset; |
| 1264 | struct resource *res; |
Benjamin Herrenschmidt | f90bb15 | 2006-11-11 17:24:51 +1100 | [diff] [blame] | 1265 | struct pci_dev *dev; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 1266 | int i; |
| 1267 | |
| 1268 | io_offset = (unsigned long)hose->io_base_virt - isa_io_base; |
| 1269 | if (bus->parent == NULL) { |
| 1270 | /* This is a host bridge - fill in its resources */ |
| 1271 | hose->bus = bus; |
| 1272 | |
| 1273 | bus->resource[0] = res = &hose->io_resource; |
| 1274 | if (!res->flags) { |
| 1275 | if (io_offset) |
| 1276 | printk(KERN_ERR "I/O resource not set for host" |
Kumar Gala | 5516b54 | 2007-06-27 01:17:57 -0500 | [diff] [blame] | 1277 | " bridge %d\n", hose->global_number); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 1278 | res->start = 0; |
| 1279 | res->end = IO_SPACE_LIMIT; |
| 1280 | res->flags = IORESOURCE_IO; |
| 1281 | } |
| 1282 | res->start += io_offset; |
| 1283 | res->end += io_offset; |
| 1284 | |
| 1285 | for (i = 0; i < 3; ++i) { |
| 1286 | res = &hose->mem_resources[i]; |
| 1287 | if (!res->flags) { |
| 1288 | if (i > 0) |
| 1289 | continue; |
| 1290 | printk(KERN_ERR "Memory resource not set for " |
Kumar Gala | 5516b54 | 2007-06-27 01:17:57 -0500 | [diff] [blame] | 1291 | "host bridge %d\n", hose->global_number); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 1292 | res->start = hose->pci_mem_offset; |
| 1293 | res->end = ~0U; |
| 1294 | res->flags = IORESOURCE_MEM; |
| 1295 | } |
| 1296 | bus->resource[i+1] = res; |
| 1297 | } |
| 1298 | } else { |
| 1299 | /* This is a subordinate bridge */ |
| 1300 | pci_read_bridge_bases(bus); |
| 1301 | |
| 1302 | for (i = 0; i < 4; ++i) { |
| 1303 | if ((res = bus->resource[i]) == NULL) |
| 1304 | continue; |
York Sun | 6d8ff10 | 2007-06-04 11:56:42 -0500 | [diff] [blame] | 1305 | if (!res->flags || bus->self->transparent) |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 1306 | continue; |
| 1307 | if (io_offset && (res->flags & IORESOURCE_IO)) { |
| 1308 | res->start += io_offset; |
| 1309 | res->end += io_offset; |
| 1310 | } else if (hose->pci_mem_offset |
| 1311 | && (res->flags & IORESOURCE_MEM)) { |
| 1312 | res->start += hose->pci_mem_offset; |
| 1313 | res->end += hose->pci_mem_offset; |
| 1314 | } |
| 1315 | } |
| 1316 | } |
| 1317 | |
Benjamin Herrenschmidt | f90bb15 | 2006-11-11 17:24:51 +1100 | [diff] [blame] | 1318 | /* Platform specific bus fixups */ |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 1319 | if (ppc_md.pcibios_fixup_bus) |
| 1320 | ppc_md.pcibios_fixup_bus(bus); |
Benjamin Herrenschmidt | f90bb15 | 2006-11-11 17:24:51 +1100 | [diff] [blame] | 1321 | |
| 1322 | /* Read default IRQs and fixup if necessary */ |
| 1323 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 1324 | pci_read_irq_line(dev); |
| 1325 | if (ppc_md.pci_irq_fixup) |
| 1326 | ppc_md.pci_irq_fixup(dev); |
| 1327 | } |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 1328 | } |
| 1329 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 1330 | /* the next one is stolen from the alpha port... */ |
| 1331 | void __init |
| 1332 | pcibios_update_irq(struct pci_dev *dev, int irq) |
| 1333 | { |
| 1334 | pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq); |
| 1335 | /* XXX FIXME - update OF device tree node interrupt property */ |
| 1336 | } |
| 1337 | |
| 1338 | int pcibios_enable_device(struct pci_dev *dev, int mask) |
| 1339 | { |
| 1340 | u16 cmd, old_cmd; |
| 1341 | int idx; |
| 1342 | struct resource *r; |
| 1343 | |
| 1344 | if (ppc_md.pcibios_enable_device_hook) |
| 1345 | if (ppc_md.pcibios_enable_device_hook(dev, 0)) |
| 1346 | return -EINVAL; |
| 1347 | |
| 1348 | pci_read_config_word(dev, PCI_COMMAND, &cmd); |
| 1349 | old_cmd = cmd; |
| 1350 | for (idx=0; idx<6; idx++) { |
| 1351 | r = &dev->resource[idx]; |
| 1352 | if (r->flags & IORESOURCE_UNSET) { |
| 1353 | printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev)); |
| 1354 | return -EINVAL; |
| 1355 | } |
| 1356 | if (r->flags & IORESOURCE_IO) |
| 1357 | cmd |= PCI_COMMAND_IO; |
| 1358 | if (r->flags & IORESOURCE_MEM) |
| 1359 | cmd |= PCI_COMMAND_MEMORY; |
| 1360 | } |
| 1361 | if (cmd != old_cmd) { |
| 1362 | printk("PCI: Enabling device %s (%04x -> %04x)\n", |
| 1363 | pci_name(dev), old_cmd, cmd); |
| 1364 | pci_write_config_word(dev, PCI_COMMAND, cmd); |
| 1365 | } |
| 1366 | return 0; |
| 1367 | } |
| 1368 | |
Kumar Gala | 0b1d40c | 2007-06-27 10:27:33 -0500 | [diff] [blame] | 1369 | static struct pci_controller* |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 1370 | pci_bus_to_hose(int bus) |
| 1371 | { |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame^] | 1372 | struct pci_controller *hose, *tmp; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 1373 | |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame^] | 1374 | list_for_each_entry_safe(hose, tmp, &hose_list, list_node) |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 1375 | if (bus >= hose->first_busno && bus <= hose->last_busno) |
| 1376 | return hose; |
| 1377 | return NULL; |
| 1378 | } |
| 1379 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 1380 | /* Provide information on locations of various I/O regions in physical |
| 1381 | * memory. Do this on a per-card basis so that we choose the right |
| 1382 | * root bridge. |
| 1383 | * Note that the returned IO or memory base is a physical address |
| 1384 | */ |
| 1385 | |
| 1386 | long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn) |
| 1387 | { |
| 1388 | struct pci_controller* hose; |
| 1389 | long result = -EOPNOTSUPP; |
| 1390 | |
| 1391 | /* Argh ! Please forgive me for that hack, but that's the |
| 1392 | * simplest way to get existing XFree to not lockup on some |
| 1393 | * G5 machines... So when something asks for bus 0 io base |
| 1394 | * (bus 0 is HT root), we return the AGP one instead. |
| 1395 | */ |
| 1396 | #ifdef CONFIG_PPC_PMAC |
Benjamin Herrenschmidt | e822250 | 2006-03-28 23:15:54 +1100 | [diff] [blame] | 1397 | if (machine_is(powermac) && machine_is_compatible("MacRISC4")) |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 1398 | if (bus == 0) |
| 1399 | bus = 0xf0; |
| 1400 | #endif /* CONFIG_PPC_PMAC */ |
| 1401 | |
| 1402 | hose = pci_bus_to_hose(bus); |
| 1403 | if (!hose) |
| 1404 | return -ENODEV; |
| 1405 | |
| 1406 | switch (which) { |
| 1407 | case IOBASE_BRIDGE_NUMBER: |
| 1408 | return (long)hose->first_busno; |
| 1409 | case IOBASE_MEMORY: |
| 1410 | return (long)hose->pci_mem_offset; |
| 1411 | case IOBASE_IO: |
| 1412 | return (long)hose->io_base_phys; |
| 1413 | case IOBASE_ISA_IO: |
| 1414 | return (long)isa_io_base; |
| 1415 | case IOBASE_ISA_MEM: |
| 1416 | return (long)isa_mem_base; |
| 1417 | } |
| 1418 | |
| 1419 | return result; |
| 1420 | } |
| 1421 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 1422 | unsigned long pci_address_to_pio(phys_addr_t address) |
| 1423 | { |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame^] | 1424 | struct pci_controller *hose, *tmp; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 1425 | |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame^] | 1426 | list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 1427 | unsigned int size = hose->io_resource.end - |
| 1428 | hose->io_resource.start + 1; |
| 1429 | if (address >= hose->io_base_phys && |
| 1430 | address < (hose->io_base_phys + size)) { |
| 1431 | unsigned long base = |
| 1432 | (unsigned long)hose->io_base_virt - _IO_BASE; |
| 1433 | return base + (address - hose->io_base_phys); |
| 1434 | } |
| 1435 | } |
| 1436 | return (unsigned int)-1; |
| 1437 | } |
| 1438 | EXPORT_SYMBOL(pci_address_to_pio); |
| 1439 | |
| 1440 | /* |
| 1441 | * Null PCI config access functions, for the case when we can't |
| 1442 | * find a hose. |
| 1443 | */ |
| 1444 | #define NULL_PCI_OP(rw, size, type) \ |
| 1445 | static int \ |
| 1446 | null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \ |
| 1447 | { \ |
| 1448 | return PCIBIOS_DEVICE_NOT_FOUND; \ |
| 1449 | } |
| 1450 | |
| 1451 | static int |
| 1452 | null_read_config(struct pci_bus *bus, unsigned int devfn, int offset, |
| 1453 | int len, u32 *val) |
| 1454 | { |
| 1455 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 1456 | } |
| 1457 | |
| 1458 | static int |
| 1459 | null_write_config(struct pci_bus *bus, unsigned int devfn, int offset, |
| 1460 | int len, u32 val) |
| 1461 | { |
| 1462 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 1463 | } |
| 1464 | |
| 1465 | static struct pci_ops null_pci_ops = |
| 1466 | { |
| 1467 | null_read_config, |
| 1468 | null_write_config |
| 1469 | }; |
| 1470 | |
| 1471 | /* |
| 1472 | * These functions are used early on before PCI scanning is done |
| 1473 | * and all of the pci_dev and pci_bus structures have been created. |
| 1474 | */ |
| 1475 | static struct pci_bus * |
| 1476 | fake_pci_bus(struct pci_controller *hose, int busnr) |
| 1477 | { |
| 1478 | static struct pci_bus bus; |
| 1479 | |
| 1480 | if (hose == 0) { |
| 1481 | hose = pci_bus_to_hose(busnr); |
| 1482 | if (hose == 0) |
| 1483 | printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr); |
| 1484 | } |
| 1485 | bus.number = busnr; |
| 1486 | bus.sysdata = hose; |
| 1487 | bus.ops = hose? hose->ops: &null_pci_ops; |
| 1488 | return &bus; |
| 1489 | } |
| 1490 | |
| 1491 | #define EARLY_PCI_OP(rw, size, type) \ |
| 1492 | int early_##rw##_config_##size(struct pci_controller *hose, int bus, \ |
| 1493 | int devfn, int offset, type value) \ |
| 1494 | { \ |
| 1495 | return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \ |
| 1496 | devfn, offset, value); \ |
| 1497 | } |
| 1498 | |
| 1499 | EARLY_PCI_OP(read, byte, u8 *) |
| 1500 | EARLY_PCI_OP(read, word, u16 *) |
| 1501 | EARLY_PCI_OP(read, dword, u32 *) |
| 1502 | EARLY_PCI_OP(write, byte, u8) |
| 1503 | EARLY_PCI_OP(write, word, u16) |
| 1504 | EARLY_PCI_OP(write, dword, u32) |