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; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 35 | unsigned long pci_dram_offset = 0; |
| 36 | int pcibios_assign_bus_offset = 1; |
| 37 | |
Benjamin Herrenschmidt | fc3fb71 | 2007-12-20 14:54:46 +1100 | [diff] [blame^] | 38 | /* Default PCI flags is 0 */ |
| 39 | unsigned int ppc_pci_flags; |
| 40 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 41 | void pcibios_make_OF_bus_map(void); |
| 42 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 43 | static void pcibios_fixup_resources(struct pci_dev* dev); |
| 44 | static void fixup_broken_pcnet32(struct pci_dev* dev); |
| 45 | static int reparent_resources(struct resource *parent, struct resource *res); |
| 46 | static void fixup_cpc710_pci64(struct pci_dev* dev); |
| 47 | #ifdef CONFIG_PPC_OF |
| 48 | static u8* pci_to_OF_bus_map; |
| 49 | #endif |
| 50 | |
| 51 | /* By default, we don't re-assign bus numbers. We do this only on |
| 52 | * some pmacs |
| 53 | */ |
Benjamin Herrenschmidt | fc3fb71 | 2007-12-20 14:54:46 +1100 | [diff] [blame^] | 54 | static int pci_assign_all_buses; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 55 | |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame] | 56 | LIST_HEAD(hose_list); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 57 | |
| 58 | static int pci_bus_count; |
| 59 | |
| 60 | static void |
Kumar Gala | 2052d6d | 2007-07-25 00:44:11 -0500 | [diff] [blame] | 61 | fixup_hide_host_resource_fsl(struct pci_dev* dev) |
| 62 | { |
| 63 | int i, class = dev->class >> 8; |
| 64 | |
| 65 | if ((class == PCI_CLASS_PROCESSOR_POWERPC) && |
| 66 | (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) && |
| 67 | (dev->bus->parent == NULL)) { |
| 68 | for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { |
| 69 | dev->resource[i].start = 0; |
| 70 | dev->resource[i].end = 0; |
| 71 | dev->resource[i].flags = 0; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MOTOROLA, PCI_ANY_ID, fixup_hide_host_resource_fsl); |
| 76 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, fixup_hide_host_resource_fsl); |
| 77 | |
| 78 | static void |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 79 | fixup_broken_pcnet32(struct pci_dev* dev) |
| 80 | { |
| 81 | if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) { |
| 82 | dev->vendor = PCI_VENDOR_ID_AMD; |
| 83 | pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD); |
| 84 | } |
| 85 | } |
| 86 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32); |
| 87 | |
| 88 | static void |
| 89 | fixup_cpc710_pci64(struct pci_dev* dev) |
| 90 | { |
| 91 | /* Hide the PCI64 BARs from the kernel as their content doesn't |
| 92 | * fit well in the resource management |
| 93 | */ |
| 94 | dev->resource[0].start = dev->resource[0].end = 0; |
| 95 | dev->resource[0].flags = 0; |
| 96 | dev->resource[1].start = dev->resource[1].end = 0; |
| 97 | dev->resource[1].flags = 0; |
| 98 | } |
| 99 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64); |
| 100 | |
| 101 | static void |
| 102 | pcibios_fixup_resources(struct pci_dev *dev) |
| 103 | { |
| 104 | struct pci_controller* hose = (struct pci_controller *)dev->sysdata; |
| 105 | int i; |
Benjamin Herrenschmidt | 05d3957 | 2007-12-11 14:48:20 +1100 | [diff] [blame] | 106 | resource_size_t offset, mask; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 107 | |
| 108 | if (!hose) { |
| 109 | printk(KERN_ERR "No hose for PCI dev %s!\n", pci_name(dev)); |
| 110 | return; |
| 111 | } |
| 112 | for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { |
| 113 | struct resource *res = dev->resource + i; |
| 114 | if (!res->flags) |
| 115 | continue; |
| 116 | if (res->end == 0xffffffff) { |
Greg Kroah-Hartman | 685143a | 2006-06-12 15:18:31 -0700 | [diff] [blame] | 117 | DBG("PCI:%s Resource %d [%016llx-%016llx] is unassigned\n", |
Sergei Shtylyov | 872455e | 2006-12-03 20:52:27 +0300 | [diff] [blame] | 118 | pci_name(dev), i, (u64)res->start, (u64)res->end); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 119 | res->end -= res->start; |
| 120 | res->start = 0; |
| 121 | res->flags |= IORESOURCE_UNSET; |
| 122 | continue; |
| 123 | } |
| 124 | offset = 0; |
Benjamin Herrenschmidt | 05d3957 | 2007-12-11 14:48:20 +1100 | [diff] [blame] | 125 | mask = (resource_size_t)-1; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 126 | if (res->flags & IORESOURCE_MEM) { |
| 127 | offset = hose->pci_mem_offset; |
| 128 | } else if (res->flags & IORESOURCE_IO) { |
| 129 | offset = (unsigned long) hose->io_base_virt |
| 130 | - isa_io_base; |
Benjamin Herrenschmidt | 05d3957 | 2007-12-11 14:48:20 +1100 | [diff] [blame] | 131 | mask = 0xffffffffu; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 132 | } |
| 133 | if (offset != 0) { |
Benjamin Herrenschmidt | 05d3957 | 2007-12-11 14:48:20 +1100 | [diff] [blame] | 134 | res->start = (res->start + offset) & mask; |
| 135 | res->end = (res->end + offset) & mask; |
Benjamin Herrenschmidt | 0ec6b5c | 2007-12-20 14:54:43 +1100 | [diff] [blame] | 136 | DBG("PCI: Fixup res %d (0x%lx) of dev %s: %llx -> %llx\n", |
Sergei Shtylyov | 872455e | 2006-12-03 20:52:27 +0300 | [diff] [blame] | 137 | i, res->flags, pci_name(dev), |
| 138 | (u64)res->start - offset, (u64)res->start); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | /* Call machine specific resource fixup */ |
| 143 | if (ppc_md.pcibios_fixup_resources) |
| 144 | ppc_md.pcibios_fixup_resources(dev); |
| 145 | } |
| 146 | DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources); |
| 147 | |
| 148 | void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region, |
| 149 | struct resource *res) |
| 150 | { |
Benjamin Herrenschmidt | 05d3957 | 2007-12-11 14:48:20 +1100 | [diff] [blame] | 151 | resource_size_t offset = 0, mask = (resource_size_t)-1; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 152 | struct pci_controller *hose = dev->sysdata; |
| 153 | |
Benjamin Herrenschmidt | 05d3957 | 2007-12-11 14:48:20 +1100 | [diff] [blame] | 154 | if (hose && res->flags & IORESOURCE_IO) { |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 155 | offset = (unsigned long)hose->io_base_virt - isa_io_base; |
Benjamin Herrenschmidt | 05d3957 | 2007-12-11 14:48:20 +1100 | [diff] [blame] | 156 | mask = 0xffffffffu; |
| 157 | } else if (hose && res->flags & IORESOURCE_MEM) |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 158 | offset = hose->pci_mem_offset; |
Benjamin Herrenschmidt | 05d3957 | 2007-12-11 14:48:20 +1100 | [diff] [blame] | 159 | region->start = (res->start - offset) & mask; |
| 160 | region->end = (res->end - offset) & mask; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 161 | } |
| 162 | EXPORT_SYMBOL(pcibios_resource_to_bus); |
| 163 | |
| 164 | void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res, |
| 165 | struct pci_bus_region *region) |
| 166 | { |
Benjamin Herrenschmidt | 05d3957 | 2007-12-11 14:48:20 +1100 | [diff] [blame] | 167 | resource_size_t offset = 0, mask = (resource_size_t)-1; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 168 | struct pci_controller *hose = dev->sysdata; |
| 169 | |
Benjamin Herrenschmidt | 05d3957 | 2007-12-11 14:48:20 +1100 | [diff] [blame] | 170 | if (hose && res->flags & IORESOURCE_IO) { |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 171 | offset = (unsigned long)hose->io_base_virt - isa_io_base; |
Benjamin Herrenschmidt | 05d3957 | 2007-12-11 14:48:20 +1100 | [diff] [blame] | 172 | mask = 0xffffffffu; |
| 173 | } else if (hose && res->flags & IORESOURCE_MEM) |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 174 | offset = hose->pci_mem_offset; |
Benjamin Herrenschmidt | 05d3957 | 2007-12-11 14:48:20 +1100 | [diff] [blame] | 175 | res->start = (region->start + offset) & mask; |
| 176 | res->end = (region->end + offset) & mask; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 177 | } |
| 178 | EXPORT_SYMBOL(pcibios_bus_to_resource); |
| 179 | |
Benjamin Herrenschmidt | fc3fb71 | 2007-12-20 14:54:46 +1100 | [diff] [blame^] | 180 | static int skip_isa_ioresource_align(struct pci_dev *dev) |
| 181 | { |
| 182 | if ((ppc_pci_flags & PPC_PCI_CAN_SKIP_ISA_ALIGN) && |
| 183 | !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA)) |
| 184 | return 1; |
| 185 | return 0; |
| 186 | } |
| 187 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 188 | /* |
| 189 | * We need to avoid collisions with `mirrored' VGA ports |
| 190 | * and other strange ISA hardware, so we always want the |
| 191 | * addresses to be allocated in the 0x000-0x0ff region |
| 192 | * modulo 0x400. |
| 193 | * |
| 194 | * Why? Because some silly external IO cards only decode |
| 195 | * the low 10 bits of the IO address. The 0x00-0xff region |
| 196 | * is reserved for motherboard devices that decode all 16 |
| 197 | * bits, so it's ok to allocate at, say, 0x2800-0x28ff, |
| 198 | * but we want to try to avoid allocating at 0x2900-0x2bff |
| 199 | * which might have be mirrored at 0x0100-0x03ff.. |
| 200 | */ |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 201 | void pcibios_align_resource(void *data, struct resource *res, |
| 202 | resource_size_t size, resource_size_t align) |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 203 | { |
| 204 | struct pci_dev *dev = data; |
| 205 | |
| 206 | if (res->flags & IORESOURCE_IO) { |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 207 | resource_size_t start = res->start; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 208 | |
Benjamin Herrenschmidt | fc3fb71 | 2007-12-20 14:54:46 +1100 | [diff] [blame^] | 209 | if (skip_isa_ioresource_align(dev)) |
| 210 | return; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 211 | if (start & 0x300) { |
| 212 | start = (start + 0x3ff) & ~0x3ff; |
| 213 | res->start = start; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | EXPORT_SYMBOL(pcibios_align_resource); |
| 218 | |
| 219 | /* |
| 220 | * Handle resources of PCI devices. If the world were perfect, we could |
| 221 | * just allocate all the resource regions and do nothing more. It isn't. |
| 222 | * On the other hand, we cannot just re-allocate all devices, as it would |
| 223 | * require us to know lots of host bridge internals. So we attempt to |
| 224 | * keep as much of the original configuration as possible, but tweak it |
| 225 | * when it's found to be wrong. |
| 226 | * |
| 227 | * Known BIOS problems we have to work around: |
| 228 | * - I/O or memory regions not configured |
| 229 | * - regions configured, but not enabled in the command register |
| 230 | * - bogus I/O addresses above 64K used |
| 231 | * - expansion ROMs left enabled (this may sound harmless, but given |
| 232 | * the fact the PCI specs explicitly allow address decoders to be |
| 233 | * shared between expansion ROMs and other resource regions, it's |
| 234 | * at least dangerous) |
| 235 | * |
| 236 | * Our solution: |
| 237 | * (1) Allocate resources for all buses behind PCI-to-PCI bridges. |
| 238 | * This gives us fixed barriers on where we can allocate. |
| 239 | * (2) Allocate resources for all enabled devices. If there is |
| 240 | * a collision, just mark the resource as unallocated. Also |
| 241 | * disable expansion ROMs during this step. |
| 242 | * (3) Try to allocate resources for disabled devices. If the |
| 243 | * resources were assigned correctly, everything goes well, |
| 244 | * if they weren't, they won't disturb allocation of other |
| 245 | * resources. |
| 246 | * (4) Assign new addresses to resources which were either |
| 247 | * not configured at all or misconfigured. If explicitly |
| 248 | * requested by the user, configure expansion ROM address |
| 249 | * as well. |
| 250 | */ |
| 251 | |
| 252 | static void __init |
| 253 | pcibios_allocate_bus_resources(struct list_head *bus_list) |
| 254 | { |
| 255 | struct pci_bus *bus; |
| 256 | int i; |
| 257 | struct resource *res, *pr; |
| 258 | |
| 259 | /* Depth-First Search on bus tree */ |
| 260 | list_for_each_entry(bus, bus_list, node) { |
| 261 | for (i = 0; i < 4; ++i) { |
| 262 | if ((res = bus->resource[i]) == NULL || !res->flags |
| 263 | || res->start > res->end) |
| 264 | continue; |
| 265 | if (bus->parent == NULL) |
| 266 | pr = (res->flags & IORESOURCE_IO)? |
Benjamin Herrenschmidt | fc3fb71 | 2007-12-20 14:54:46 +1100 | [diff] [blame^] | 267 | &ioport_resource : &iomem_resource; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 268 | else { |
Benjamin Herrenschmidt | fc3fb71 | 2007-12-20 14:54:46 +1100 | [diff] [blame^] | 269 | /* Don't bother with non-root busses when |
| 270 | * re-assigning all resources. |
| 271 | */ |
| 272 | if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_RSRC) |
| 273 | continue; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 274 | pr = pci_find_parent_resource(bus->self, res); |
| 275 | if (pr == res) { |
| 276 | /* this happens when the generic PCI |
| 277 | * code (wrongly) decides that this |
| 278 | * bridge is transparent -- paulus |
| 279 | */ |
| 280 | continue; |
| 281 | } |
| 282 | } |
| 283 | |
Benjamin Herrenschmidt | 0ec6b5c | 2007-12-20 14:54:43 +1100 | [diff] [blame] | 284 | DBG("PCI: dev %s (bus 0x%02x) bridge rsrc %d: %016llx..%016llx " |
| 285 | "(f:0x%08lx), parent %p\n", |
| 286 | bus->self ? pci_name(bus->self) : "PHB", bus->number, i, |
Sergei Shtylyov | 872455e | 2006-12-03 20:52:27 +0300 | [diff] [blame] | 287 | (u64)res->start, (u64)res->end, res->flags, pr); |
Benjamin Herrenschmidt | 0ec6b5c | 2007-12-20 14:54:43 +1100 | [diff] [blame] | 288 | |
| 289 | if (pr && !(pr->flags & IORESOURCE_UNSET)) { |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 290 | if (request_resource(pr, res) == 0) |
| 291 | continue; |
| 292 | /* |
| 293 | * Must be a conflict with an existing entry. |
| 294 | * Move that entry (or entries) under the |
| 295 | * bridge resource and try again. |
| 296 | */ |
| 297 | if (reparent_resources(pr, res) == 0) |
| 298 | continue; |
| 299 | } |
Benjamin Herrenschmidt | 0ec6b5c | 2007-12-20 14:54:43 +1100 | [diff] [blame] | 300 | printk(KERN_WARNING |
| 301 | "PCI: Cannot allocate resource region " |
| 302 | "%d of PCI bridge %d, will remap\n", |
| 303 | i, bus->number); |
| 304 | res->flags |= IORESOURCE_UNSET; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 305 | } |
| 306 | pcibios_allocate_bus_resources(&bus->children); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /* |
| 311 | * Reparent resource children of pr that conflict with res |
| 312 | * under res, and make res replace those children. |
| 313 | */ |
| 314 | static int __init |
| 315 | reparent_resources(struct resource *parent, struct resource *res) |
| 316 | { |
| 317 | struct resource *p, **pp; |
| 318 | struct resource **firstpp = NULL; |
| 319 | |
| 320 | for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) { |
| 321 | if (p->end < res->start) |
| 322 | continue; |
| 323 | if (res->end < p->start) |
| 324 | break; |
| 325 | if (p->start < res->start || p->end > res->end) |
| 326 | return -1; /* not completely contained */ |
| 327 | if (firstpp == NULL) |
| 328 | firstpp = pp; |
| 329 | } |
| 330 | if (firstpp == NULL) |
| 331 | return -1; /* didn't find any conflicting entries? */ |
| 332 | res->parent = parent; |
| 333 | res->child = *firstpp; |
| 334 | res->sibling = *pp; |
| 335 | *firstpp = res; |
| 336 | *pp = NULL; |
| 337 | for (p = res->child; p != NULL; p = p->sibling) { |
| 338 | p->parent = res; |
Greg Kroah-Hartman | 685143a | 2006-06-12 15:18:31 -0700 | [diff] [blame] | 339 | DBG(KERN_INFO "PCI: reparented %s [%llx..%llx] under %s\n", |
Sergei Shtylyov | 872455e | 2006-12-03 20:52:27 +0300 | [diff] [blame] | 340 | p->name, (u64)p->start, (u64)p->end, res->name); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 341 | } |
| 342 | return 0; |
| 343 | } |
| 344 | |
Kumar Gala | f64fddb | 2007-07-20 13:35:34 -0500 | [diff] [blame] | 345 | void __init |
| 346 | update_bridge_resource(struct pci_dev *dev, struct resource *res) |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 347 | { |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 348 | u8 io_base_lo, io_limit_lo; |
| 349 | u16 mem_base, mem_limit; |
| 350 | u16 cmd; |
Benjamin Herrenschmidt | 05d3957 | 2007-12-11 14:48:20 +1100 | [diff] [blame] | 351 | resource_size_t start, end, off; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 352 | struct pci_controller *hose = dev->sysdata; |
| 353 | |
| 354 | if (!hose) { |
| 355 | printk("update_bridge_base: no hose?\n"); |
| 356 | return; |
| 357 | } |
| 358 | pci_read_config_word(dev, PCI_COMMAND, &cmd); |
| 359 | pci_write_config_word(dev, PCI_COMMAND, |
| 360 | cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)); |
| 361 | if (res->flags & IORESOURCE_IO) { |
| 362 | off = (unsigned long) hose->io_base_virt - isa_io_base; |
| 363 | start = res->start - off; |
| 364 | end = res->end - off; |
| 365 | io_base_lo = (start >> 8) & PCI_IO_RANGE_MASK; |
| 366 | io_limit_lo = (end >> 8) & PCI_IO_RANGE_MASK; |
Randy Vinson | 60b2a46 | 2006-10-12 13:36:23 -0700 | [diff] [blame] | 367 | if (end > 0xffff) |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 368 | io_base_lo |= PCI_IO_RANGE_TYPE_32; |
Randy Vinson | 60b2a46 | 2006-10-12 13:36:23 -0700 | [diff] [blame] | 369 | else |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 370 | io_base_lo |= PCI_IO_RANGE_TYPE_16; |
Randy Vinson | 60b2a46 | 2006-10-12 13:36:23 -0700 | [diff] [blame] | 371 | pci_write_config_word(dev, PCI_IO_BASE_UPPER16, |
| 372 | start >> 16); |
| 373 | pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16, |
| 374 | end >> 16); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 375 | pci_write_config_byte(dev, PCI_IO_BASE, io_base_lo); |
| 376 | pci_write_config_byte(dev, PCI_IO_LIMIT, io_limit_lo); |
| 377 | |
| 378 | } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH)) |
| 379 | == IORESOURCE_MEM) { |
| 380 | off = hose->pci_mem_offset; |
| 381 | mem_base = ((res->start - off) >> 16) & PCI_MEMORY_RANGE_MASK; |
| 382 | mem_limit = ((res->end - off) >> 16) & PCI_MEMORY_RANGE_MASK; |
| 383 | pci_write_config_word(dev, PCI_MEMORY_BASE, mem_base); |
| 384 | pci_write_config_word(dev, PCI_MEMORY_LIMIT, mem_limit); |
| 385 | |
| 386 | } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH)) |
| 387 | == (IORESOURCE_MEM | IORESOURCE_PREFETCH)) { |
| 388 | off = hose->pci_mem_offset; |
| 389 | mem_base = ((res->start - off) >> 16) & PCI_PREF_RANGE_MASK; |
| 390 | mem_limit = ((res->end - off) >> 16) & PCI_PREF_RANGE_MASK; |
| 391 | pci_write_config_word(dev, PCI_PREF_MEMORY_BASE, mem_base); |
| 392 | pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, mem_limit); |
| 393 | |
| 394 | } else { |
Kumar Gala | f64fddb | 2007-07-20 13:35:34 -0500 | [diff] [blame] | 395 | DBG(KERN_ERR "PCI: ugh, bridge %s res has flags=%lx\n", |
| 396 | pci_name(dev), res->flags); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 397 | } |
| 398 | pci_write_config_word(dev, PCI_COMMAND, cmd); |
| 399 | } |
| 400 | |
| 401 | static inline void alloc_resource(struct pci_dev *dev, int idx) |
| 402 | { |
| 403 | struct resource *pr, *r = &dev->resource[idx]; |
| 404 | |
Benjamin Herrenschmidt | 0ec6b5c | 2007-12-20 14:54:43 +1100 | [diff] [blame] | 405 | DBG("PCI: Allocating %s: Resource %d: %016llx..%016llx (f=%lx)\n", |
Sergei Shtylyov | 872455e | 2006-12-03 20:52:27 +0300 | [diff] [blame] | 406 | pci_name(dev), idx, (u64)r->start, (u64)r->end, r->flags); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 407 | pr = pci_find_parent_resource(dev, r); |
Benjamin Herrenschmidt | 0ec6b5c | 2007-12-20 14:54:43 +1100 | [diff] [blame] | 408 | if (!pr || (pr->flags & IORESOURCE_UNSET) || request_resource(pr, r) < 0) { |
| 409 | printk(KERN_WARNING "PCI: Cannot allocate resource region %d" |
| 410 | " of device %s, will remap\n", idx, pci_name(dev)); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 411 | if (pr) |
Greg Kroah-Hartman | 685143a | 2006-06-12 15:18:31 -0700 | [diff] [blame] | 412 | DBG("PCI: parent is %p: %016llx-%016llx (f=%lx)\n", |
Sergei Shtylyov | 872455e | 2006-12-03 20:52:27 +0300 | [diff] [blame] | 413 | pr, (u64)pr->start, (u64)pr->end, pr->flags); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 414 | /* We'll assign a new address later */ |
| 415 | r->flags |= IORESOURCE_UNSET; |
| 416 | r->end -= r->start; |
| 417 | r->start = 0; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | static void __init |
| 422 | pcibios_allocate_resources(int pass) |
| 423 | { |
| 424 | struct pci_dev *dev = NULL; |
| 425 | int idx, disabled; |
| 426 | u16 command; |
| 427 | struct resource *r; |
| 428 | |
| 429 | for_each_pci_dev(dev) { |
| 430 | pci_read_config_word(dev, PCI_COMMAND, &command); |
| 431 | for (idx = 0; idx < 6; idx++) { |
| 432 | r = &dev->resource[idx]; |
| 433 | if (r->parent) /* Already allocated */ |
| 434 | continue; |
| 435 | if (!r->flags || (r->flags & IORESOURCE_UNSET)) |
| 436 | continue; /* Not assigned at all */ |
| 437 | if (r->flags & IORESOURCE_IO) |
| 438 | disabled = !(command & PCI_COMMAND_IO); |
| 439 | else |
| 440 | disabled = !(command & PCI_COMMAND_MEMORY); |
| 441 | if (pass == disabled) |
| 442 | alloc_resource(dev, idx); |
| 443 | } |
| 444 | if (pass) |
| 445 | continue; |
| 446 | r = &dev->resource[PCI_ROM_RESOURCE]; |
| 447 | if (r->flags & IORESOURCE_ROM_ENABLE) { |
| 448 | /* Turn the ROM off, leave the resource region, but keep it unregistered. */ |
| 449 | u32 reg; |
| 450 | DBG("PCI: Switching off ROM of %s\n", pci_name(dev)); |
| 451 | r->flags &= ~IORESOURCE_ROM_ENABLE; |
| 452 | pci_read_config_dword(dev, dev->rom_base_reg, ®); |
| 453 | pci_write_config_dword(dev, dev->rom_base_reg, |
| 454 | reg & ~PCI_ROM_ADDRESS_ENABLE); |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 459 | #ifdef CONFIG_PPC_OF |
| 460 | /* |
| 461 | * Functions below are used on OpenFirmware machines. |
| 462 | */ |
| 463 | static void |
| 464 | make_one_node_map(struct device_node* node, u8 pci_bus) |
| 465 | { |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 466 | const int *bus_range; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 467 | int len; |
| 468 | |
| 469 | if (pci_bus >= pci_bus_count) |
| 470 | return; |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 471 | bus_range = of_get_property(node, "bus-range", &len); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 472 | if (bus_range == NULL || len < 2 * sizeof(int)) { |
| 473 | printk(KERN_WARNING "Can't get bus-range for %s, " |
| 474 | "assuming it starts at 0\n", node->full_name); |
| 475 | pci_to_OF_bus_map[pci_bus] = 0; |
| 476 | } else |
| 477 | pci_to_OF_bus_map[pci_bus] = bus_range[0]; |
| 478 | |
| 479 | for (node=node->child; node != 0;node = node->sibling) { |
| 480 | struct pci_dev* dev; |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 481 | const unsigned int *class_code, *reg; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 482 | |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 483 | class_code = of_get_property(node, "class-code", NULL); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 484 | if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI && |
| 485 | (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) |
| 486 | continue; |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 487 | reg = of_get_property(node, "reg", NULL); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 488 | if (!reg) |
| 489 | continue; |
Alan Cox | ab46276 | 2007-04-23 14:47:59 +0100 | [diff] [blame] | 490 | dev = pci_get_bus_and_slot(pci_bus, ((reg[0] >> 8) & 0xff)); |
| 491 | if (!dev || !dev->subordinate) { |
| 492 | pci_dev_put(dev); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 493 | continue; |
Alan Cox | ab46276 | 2007-04-23 14:47:59 +0100 | [diff] [blame] | 494 | } |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 495 | make_one_node_map(node, dev->subordinate->number); |
Alan Cox | ab46276 | 2007-04-23 14:47:59 +0100 | [diff] [blame] | 496 | pci_dev_put(dev); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
| 500 | void |
| 501 | pcibios_make_OF_bus_map(void) |
| 502 | { |
| 503 | int i; |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame] | 504 | struct pci_controller *hose, *tmp; |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 505 | struct property *map_prop; |
Stephen Rothwell | 8c8dc32 | 2007-04-24 13:50:55 +1000 | [diff] [blame] | 506 | struct device_node *dn; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 507 | |
Robert P. J. Day | 5cbded5 | 2006-12-13 00:35:56 -0800 | [diff] [blame] | 508 | pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 509 | if (!pci_to_OF_bus_map) { |
| 510 | printk(KERN_ERR "Can't allocate OF bus map !\n"); |
| 511 | return; |
| 512 | } |
| 513 | |
| 514 | /* We fill the bus map with invalid values, that helps |
| 515 | * debugging. |
| 516 | */ |
| 517 | for (i=0; i<pci_bus_count; i++) |
| 518 | pci_to_OF_bus_map[i] = 0xff; |
| 519 | |
| 520 | /* For each hose, we begin searching bridges */ |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame] | 521 | list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { |
Stephen Rothwell | 44ef339 | 2007-12-10 14:33:21 +1100 | [diff] [blame] | 522 | struct device_node* node = hose->dn; |
| 523 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 524 | if (!node) |
| 525 | continue; |
| 526 | make_one_node_map(node, hose->first_busno); |
| 527 | } |
Stephen Rothwell | 8c8dc32 | 2007-04-24 13:50:55 +1000 | [diff] [blame] | 528 | dn = of_find_node_by_path("/"); |
| 529 | map_prop = of_find_property(dn, "pci-OF-bus-map", NULL); |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 530 | if (map_prop) { |
| 531 | BUG_ON(pci_bus_count > map_prop->length); |
| 532 | memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count); |
| 533 | } |
Stephen Rothwell | 8c8dc32 | 2007-04-24 13:50:55 +1000 | [diff] [blame] | 534 | of_node_put(dn); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 535 | #ifdef DEBUG |
| 536 | printk("PCI->OF bus map:\n"); |
| 537 | for (i=0; i<pci_bus_count; i++) { |
| 538 | if (pci_to_OF_bus_map[i] == 0xff) |
| 539 | continue; |
| 540 | printk("%d -> %d\n", i, pci_to_OF_bus_map[i]); |
| 541 | } |
| 542 | #endif |
| 543 | } |
| 544 | |
| 545 | typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data); |
| 546 | |
| 547 | static struct device_node* |
| 548 | scan_OF_pci_childs(struct device_node* node, pci_OF_scan_iterator filter, void* data) |
| 549 | { |
| 550 | struct device_node* sub_node; |
| 551 | |
| 552 | for (; node != 0;node = node->sibling) { |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 553 | const unsigned int *class_code; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 554 | |
| 555 | if (filter(node, data)) |
| 556 | return node; |
| 557 | |
| 558 | /* For PCI<->PCI bridges or CardBus bridges, we go down |
| 559 | * Note: some OFs create a parent node "multifunc-device" as |
| 560 | * a fake root for all functions of a multi-function device, |
| 561 | * we go down them as well. |
| 562 | */ |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 563 | class_code = of_get_property(node, "class-code", NULL); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 564 | if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI && |
| 565 | (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) && |
| 566 | strcmp(node->name, "multifunc-device")) |
| 567 | continue; |
| 568 | sub_node = scan_OF_pci_childs(node->child, filter, data); |
| 569 | if (sub_node) |
| 570 | return sub_node; |
| 571 | } |
| 572 | return NULL; |
| 573 | } |
| 574 | |
Benjamin Herrenschmidt | dae4828 | 2006-12-11 14:09:07 +1100 | [diff] [blame] | 575 | static struct device_node *scan_OF_for_pci_dev(struct device_node *parent, |
| 576 | unsigned int devfn) |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 577 | { |
Benjamin Herrenschmidt | dae4828 | 2006-12-11 14:09:07 +1100 | [diff] [blame] | 578 | struct device_node *np = NULL; |
| 579 | const u32 *reg; |
| 580 | unsigned int psize; |
| 581 | |
| 582 | while ((np = of_get_next_child(parent, np)) != NULL) { |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 583 | reg = of_get_property(np, "reg", &psize); |
Benjamin Herrenschmidt | dae4828 | 2006-12-11 14:09:07 +1100 | [diff] [blame] | 584 | if (reg == NULL || psize < 4) |
| 585 | continue; |
| 586 | if (((reg[0] >> 8) & 0xff) == devfn) |
| 587 | return np; |
| 588 | } |
| 589 | return NULL; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 590 | } |
| 591 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 592 | |
Benjamin Herrenschmidt | dae4828 | 2006-12-11 14:09:07 +1100 | [diff] [blame] | 593 | static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus) |
| 594 | { |
| 595 | struct device_node *parent, *np; |
| 596 | |
| 597 | /* Are we a root bus ? */ |
| 598 | if (bus->self == NULL || bus->parent == NULL) { |
Kumar Gala | 0b1d40c | 2007-06-27 10:27:33 -0500 | [diff] [blame] | 599 | struct pci_controller *hose = pci_bus_to_host(bus); |
Benjamin Herrenschmidt | dae4828 | 2006-12-11 14:09:07 +1100 | [diff] [blame] | 600 | if (hose == NULL) |
| 601 | return NULL; |
Stephen Rothwell | 44ef339 | 2007-12-10 14:33:21 +1100 | [diff] [blame] | 602 | return of_node_get(hose->dn); |
Benjamin Herrenschmidt | dae4828 | 2006-12-11 14:09:07 +1100 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /* not a root bus, we need to get our parent */ |
| 606 | parent = scan_OF_for_pci_bus(bus->parent); |
| 607 | if (parent == NULL) |
| 608 | return NULL; |
| 609 | |
| 610 | /* now iterate for children for a match */ |
| 611 | np = scan_OF_for_pci_dev(parent, bus->self->devfn); |
| 612 | of_node_put(parent); |
| 613 | |
Benjamin Herrenschmidt | dae4828 | 2006-12-11 14:09:07 +1100 | [diff] [blame] | 614 | return np; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | /* |
| 618 | * Scans the OF tree for a device node matching a PCI device |
| 619 | */ |
| 620 | struct device_node * |
| 621 | pci_busdev_to_OF_node(struct pci_bus *bus, int devfn) |
| 622 | { |
Benjamin Herrenschmidt | dae4828 | 2006-12-11 14:09:07 +1100 | [diff] [blame] | 623 | struct device_node *parent, *np; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 624 | |
| 625 | if (!have_of) |
| 626 | return NULL; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 627 | |
Benjamin Herrenschmidt | dae4828 | 2006-12-11 14:09:07 +1100 | [diff] [blame] | 628 | DBG("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn); |
| 629 | parent = scan_OF_for_pci_bus(bus); |
| 630 | if (parent == NULL) |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 631 | return NULL; |
Benjamin Herrenschmidt | dae4828 | 2006-12-11 14:09:07 +1100 | [diff] [blame] | 632 | DBG(" parent is %s\n", parent ? parent->full_name : "<NULL>"); |
| 633 | np = scan_OF_for_pci_dev(parent, devfn); |
| 634 | of_node_put(parent); |
| 635 | DBG(" result is %s\n", np ? np->full_name : "<NULL>"); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 636 | |
Benjamin Herrenschmidt | dae4828 | 2006-12-11 14:09:07 +1100 | [diff] [blame] | 637 | /* XXX most callers don't release the returned node |
| 638 | * mostly because ppc64 doesn't increase the refcount, |
| 639 | * we need to fix that. |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 640 | */ |
Benjamin Herrenschmidt | dae4828 | 2006-12-11 14:09:07 +1100 | [diff] [blame] | 641 | return np; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 642 | } |
| 643 | EXPORT_SYMBOL(pci_busdev_to_OF_node); |
| 644 | |
| 645 | struct device_node* |
| 646 | pci_device_to_OF_node(struct pci_dev *dev) |
| 647 | { |
| 648 | return pci_busdev_to_OF_node(dev->bus, dev->devfn); |
| 649 | } |
| 650 | EXPORT_SYMBOL(pci_device_to_OF_node); |
| 651 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 652 | static int |
| 653 | find_OF_pci_device_filter(struct device_node* node, void* data) |
| 654 | { |
| 655 | return ((void *)node == data); |
| 656 | } |
| 657 | |
| 658 | /* |
| 659 | * Returns the PCI device matching a given OF node |
| 660 | */ |
| 661 | int |
| 662 | pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn) |
| 663 | { |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 664 | const unsigned int *reg; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 665 | struct pci_controller* hose; |
| 666 | struct pci_dev* dev = NULL; |
| 667 | |
| 668 | if (!have_of) |
| 669 | return -ENODEV; |
| 670 | /* Make sure it's really a PCI device */ |
| 671 | hose = pci_find_hose_for_OF_device(node); |
Stephen Rothwell | 44ef339 | 2007-12-10 14:33:21 +1100 | [diff] [blame] | 672 | if (!hose || !hose->dn) |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 673 | return -ENODEV; |
Stephen Rothwell | 44ef339 | 2007-12-10 14:33:21 +1100 | [diff] [blame] | 674 | if (!scan_OF_pci_childs(hose->dn->child, |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 675 | find_OF_pci_device_filter, (void *)node)) |
| 676 | return -ENODEV; |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 677 | reg = of_get_property(node, "reg", NULL); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 678 | if (!reg) |
| 679 | return -ENODEV; |
| 680 | *bus = (reg[0] >> 16) & 0xff; |
| 681 | *devfn = ((reg[0] >> 8) & 0xff); |
| 682 | |
| 683 | /* Ok, here we need some tweak. If we have already renumbered |
| 684 | * all busses, we can't rely on the OF bus number any more. |
| 685 | * the pci_to_OF_bus_map is not enough as several PCI busses |
| 686 | * may match the same OF bus number. |
| 687 | */ |
| 688 | if (!pci_to_OF_bus_map) |
| 689 | return 0; |
| 690 | |
| 691 | for_each_pci_dev(dev) |
| 692 | if (pci_to_OF_bus_map[dev->bus->number] == *bus && |
| 693 | dev->devfn == *devfn) { |
| 694 | *bus = dev->bus->number; |
| 695 | pci_dev_put(dev); |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | return -ENODEV; |
| 700 | } |
| 701 | EXPORT_SYMBOL(pci_device_from_OF_node); |
| 702 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 703 | /* We create the "pci-OF-bus-map" property now so it appears in the |
| 704 | * /proc device tree |
| 705 | */ |
| 706 | void __init |
| 707 | pci_create_OF_bus_map(void) |
| 708 | { |
| 709 | struct property* of_prop; |
Stephen Rothwell | 8c8dc32 | 2007-04-24 13:50:55 +1000 | [diff] [blame] | 710 | struct device_node *dn; |
| 711 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 712 | of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256); |
Stephen Rothwell | 8c8dc32 | 2007-04-24 13:50:55 +1000 | [diff] [blame] | 713 | if (!of_prop) |
| 714 | return; |
| 715 | dn = of_find_node_by_path("/"); |
| 716 | if (dn) { |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 717 | memset(of_prop, -1, sizeof(struct property) + 256); |
| 718 | of_prop->name = "pci-OF-bus-map"; |
| 719 | of_prop->length = 256; |
Stephen Rothwell | 1a38147 | 2007-04-03 10:58:52 +1000 | [diff] [blame] | 720 | of_prop->value = &of_prop[1]; |
Stephen Rothwell | 8c8dc32 | 2007-04-24 13:50:55 +1000 | [diff] [blame] | 721 | prom_add_property(dn, of_prop); |
| 722 | of_node_put(dn); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 726 | #else /* CONFIG_PPC_OF */ |
| 727 | void pcibios_make_OF_bus_map(void) |
| 728 | { |
| 729 | } |
| 730 | #endif /* CONFIG_PPC_OF */ |
| 731 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 732 | static int __init |
| 733 | pcibios_init(void) |
| 734 | { |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame] | 735 | struct pci_controller *hose, *tmp; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 736 | struct pci_bus *bus; |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame] | 737 | int next_busno = 0; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 738 | |
| 739 | printk(KERN_INFO "PCI: Probing PCI hardware\n"); |
| 740 | |
Benjamin Herrenschmidt | fc3fb71 | 2007-12-20 14:54:46 +1100 | [diff] [blame^] | 741 | if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_BUS) |
| 742 | pci_assign_all_buses = 1; |
| 743 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 744 | /* Scan all of the recorded PCI controllers. */ |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame] | 745 | list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 746 | if (pci_assign_all_buses) |
| 747 | hose->first_busno = next_busno; |
| 748 | hose->last_busno = 0xff; |
Benjamin Herrenschmidt | 803d457 | 2006-11-11 17:25:07 +1100 | [diff] [blame] | 749 | bus = pci_scan_bus_parented(hose->parent, hose->first_busno, |
| 750 | hose->ops, hose); |
| 751 | if (bus) |
| 752 | pci_bus_add_devices(bus); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 753 | hose->last_busno = bus->subordinate; |
| 754 | if (pci_assign_all_buses || next_busno <= hose->last_busno) |
| 755 | next_busno = hose->last_busno + pcibios_assign_bus_offset; |
| 756 | } |
| 757 | pci_bus_count = next_busno; |
| 758 | |
| 759 | /* OpenFirmware based machines need a map of OF bus |
| 760 | * numbers vs. kernel bus numbers since we may have to |
| 761 | * remap them. |
| 762 | */ |
| 763 | if (pci_assign_all_buses && have_of) |
| 764 | pcibios_make_OF_bus_map(); |
| 765 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 766 | /* Call machine dependent fixup */ |
| 767 | if (ppc_md.pcibios_fixup) |
| 768 | ppc_md.pcibios_fixup(); |
| 769 | |
Benjamin Herrenschmidt | fc3fb71 | 2007-12-20 14:54:46 +1100 | [diff] [blame^] | 770 | /* Allocate and assign resources. If we re-assign everything, then |
| 771 | * we skip the allocate phase |
| 772 | */ |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 773 | pcibios_allocate_bus_resources(&pci_root_buses); |
Benjamin Herrenschmidt | fc3fb71 | 2007-12-20 14:54:46 +1100 | [diff] [blame^] | 774 | if (!(ppc_pci_flags & PPC_PCI_REASSIGN_ALL_RSRC)) { |
| 775 | pcibios_allocate_resources(0); |
| 776 | pcibios_allocate_resources(1); |
| 777 | } |
| 778 | if (!(ppc_pci_flags & PPC_PCI_PROBE_ONLY)) { |
| 779 | DBG("PCI: Assigning unassigned resouces...\n"); |
| 780 | pci_assign_unassigned_resources(); |
| 781 | } |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 782 | |
| 783 | /* Call machine dependent post-init code */ |
| 784 | if (ppc_md.pcibios_after_init) |
| 785 | ppc_md.pcibios_after_init(); |
| 786 | |
| 787 | return 0; |
| 788 | } |
| 789 | |
| 790 | subsys_initcall(pcibios_init); |
| 791 | |
Kumar Gala | 282045b | 2007-07-26 00:16:05 -0500 | [diff] [blame] | 792 | void pcibios_fixup_bus(struct pci_bus *bus) |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 793 | { |
| 794 | struct pci_controller *hose = (struct pci_controller *) bus->sysdata; |
| 795 | unsigned long io_offset; |
| 796 | struct resource *res; |
Benjamin Herrenschmidt | f90bb15 | 2006-11-11 17:24:51 +1100 | [diff] [blame] | 797 | struct pci_dev *dev; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 798 | int i; |
| 799 | |
| 800 | io_offset = (unsigned long)hose->io_base_virt - isa_io_base; |
| 801 | if (bus->parent == NULL) { |
| 802 | /* This is a host bridge - fill in its resources */ |
| 803 | hose->bus = bus; |
| 804 | |
| 805 | bus->resource[0] = res = &hose->io_resource; |
| 806 | if (!res->flags) { |
| 807 | if (io_offset) |
| 808 | printk(KERN_ERR "I/O resource not set for host" |
Kumar Gala | 5516b54 | 2007-06-27 01:17:57 -0500 | [diff] [blame] | 809 | " bridge %d\n", hose->global_number); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 810 | res->start = 0; |
| 811 | res->end = IO_SPACE_LIMIT; |
| 812 | res->flags = IORESOURCE_IO; |
| 813 | } |
Benjamin Herrenschmidt | 05d3957 | 2007-12-11 14:48:20 +1100 | [diff] [blame] | 814 | res->start = (res->start + io_offset) & 0xffffffffu; |
| 815 | res->end = (res->end + io_offset) & 0xffffffffu; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 816 | |
| 817 | for (i = 0; i < 3; ++i) { |
| 818 | res = &hose->mem_resources[i]; |
| 819 | if (!res->flags) { |
| 820 | if (i > 0) |
| 821 | continue; |
| 822 | printk(KERN_ERR "Memory resource not set for " |
Kumar Gala | 5516b54 | 2007-06-27 01:17:57 -0500 | [diff] [blame] | 823 | "host bridge %d\n", hose->global_number); |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 824 | res->start = hose->pci_mem_offset; |
| 825 | res->end = ~0U; |
| 826 | res->flags = IORESOURCE_MEM; |
| 827 | } |
| 828 | bus->resource[i+1] = res; |
| 829 | } |
| 830 | } else { |
| 831 | /* This is a subordinate bridge */ |
| 832 | pci_read_bridge_bases(bus); |
| 833 | |
| 834 | for (i = 0; i < 4; ++i) { |
| 835 | if ((res = bus->resource[i]) == NULL) |
| 836 | continue; |
York Sun | 6d8ff10 | 2007-06-04 11:56:42 -0500 | [diff] [blame] | 837 | if (!res->flags || bus->self->transparent) |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 838 | continue; |
| 839 | if (io_offset && (res->flags & IORESOURCE_IO)) { |
Benjamin Herrenschmidt | 05d3957 | 2007-12-11 14:48:20 +1100 | [diff] [blame] | 840 | res->start = (res->start + io_offset) & |
| 841 | 0xffffffffu; |
| 842 | res->end = (res->end + io_offset) & |
| 843 | 0xffffffffu; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 844 | } else if (hose->pci_mem_offset |
| 845 | && (res->flags & IORESOURCE_MEM)) { |
| 846 | res->start += hose->pci_mem_offset; |
| 847 | res->end += hose->pci_mem_offset; |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | |
Benjamin Herrenschmidt | f90bb15 | 2006-11-11 17:24:51 +1100 | [diff] [blame] | 852 | /* Platform specific bus fixups */ |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 853 | if (ppc_md.pcibios_fixup_bus) |
| 854 | ppc_md.pcibios_fixup_bus(bus); |
Benjamin Herrenschmidt | f90bb15 | 2006-11-11 17:24:51 +1100 | [diff] [blame] | 855 | |
| 856 | /* Read default IRQs and fixup if necessary */ |
| 857 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 858 | pci_read_irq_line(dev); |
| 859 | if (ppc_md.pci_irq_fixup) |
| 860 | ppc_md.pci_irq_fixup(dev); |
| 861 | } |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 862 | } |
| 863 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 864 | /* the next one is stolen from the alpha port... */ |
| 865 | void __init |
| 866 | pcibios_update_irq(struct pci_dev *dev, int irq) |
| 867 | { |
| 868 | pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq); |
| 869 | /* XXX FIXME - update OF device tree node interrupt property */ |
| 870 | } |
| 871 | |
| 872 | int pcibios_enable_device(struct pci_dev *dev, int mask) |
| 873 | { |
| 874 | u16 cmd, old_cmd; |
| 875 | int idx; |
| 876 | struct resource *r; |
| 877 | |
| 878 | if (ppc_md.pcibios_enable_device_hook) |
| 879 | if (ppc_md.pcibios_enable_device_hook(dev, 0)) |
| 880 | return -EINVAL; |
| 881 | |
| 882 | pci_read_config_word(dev, PCI_COMMAND, &cmd); |
| 883 | old_cmd = cmd; |
| 884 | for (idx=0; idx<6; idx++) { |
| 885 | r = &dev->resource[idx]; |
| 886 | if (r->flags & IORESOURCE_UNSET) { |
| 887 | printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev)); |
| 888 | return -EINVAL; |
| 889 | } |
| 890 | if (r->flags & IORESOURCE_IO) |
| 891 | cmd |= PCI_COMMAND_IO; |
| 892 | if (r->flags & IORESOURCE_MEM) |
| 893 | cmd |= PCI_COMMAND_MEMORY; |
| 894 | } |
| 895 | if (cmd != old_cmd) { |
| 896 | printk("PCI: Enabling device %s (%04x -> %04x)\n", |
| 897 | pci_name(dev), old_cmd, cmd); |
| 898 | pci_write_config_word(dev, PCI_COMMAND, cmd); |
| 899 | } |
| 900 | return 0; |
| 901 | } |
| 902 | |
Kumar Gala | 0b1d40c | 2007-06-27 10:27:33 -0500 | [diff] [blame] | 903 | static struct pci_controller* |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 904 | pci_bus_to_hose(int bus) |
| 905 | { |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame] | 906 | struct pci_controller *hose, *tmp; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 907 | |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame] | 908 | list_for_each_entry_safe(hose, tmp, &hose_list, list_node) |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 909 | if (bus >= hose->first_busno && bus <= hose->last_busno) |
| 910 | return hose; |
| 911 | return NULL; |
| 912 | } |
| 913 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 914 | /* Provide information on locations of various I/O regions in physical |
| 915 | * memory. Do this on a per-card basis so that we choose the right |
| 916 | * root bridge. |
| 917 | * Note that the returned IO or memory base is a physical address |
| 918 | */ |
| 919 | |
| 920 | long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn) |
| 921 | { |
| 922 | struct pci_controller* hose; |
| 923 | long result = -EOPNOTSUPP; |
| 924 | |
| 925 | /* Argh ! Please forgive me for that hack, but that's the |
| 926 | * simplest way to get existing XFree to not lockup on some |
| 927 | * G5 machines... So when something asks for bus 0 io base |
| 928 | * (bus 0 is HT root), we return the AGP one instead. |
| 929 | */ |
| 930 | #ifdef CONFIG_PPC_PMAC |
Benjamin Herrenschmidt | e822250 | 2006-03-28 23:15:54 +1100 | [diff] [blame] | 931 | if (machine_is(powermac) && machine_is_compatible("MacRISC4")) |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 932 | if (bus == 0) |
| 933 | bus = 0xf0; |
| 934 | #endif /* CONFIG_PPC_PMAC */ |
| 935 | |
| 936 | hose = pci_bus_to_hose(bus); |
| 937 | if (!hose) |
| 938 | return -ENODEV; |
| 939 | |
| 940 | switch (which) { |
| 941 | case IOBASE_BRIDGE_NUMBER: |
| 942 | return (long)hose->first_busno; |
| 943 | case IOBASE_MEMORY: |
| 944 | return (long)hose->pci_mem_offset; |
| 945 | case IOBASE_IO: |
| 946 | return (long)hose->io_base_phys; |
| 947 | case IOBASE_ISA_IO: |
| 948 | return (long)isa_io_base; |
| 949 | case IOBASE_ISA_MEM: |
| 950 | return (long)isa_mem_base; |
| 951 | } |
| 952 | |
| 953 | return result; |
| 954 | } |
| 955 | |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 956 | unsigned long pci_address_to_pio(phys_addr_t address) |
| 957 | { |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame] | 958 | struct pci_controller *hose, *tmp; |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 959 | |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame] | 960 | list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 961 | unsigned int size = hose->io_resource.end - |
| 962 | hose->io_resource.start + 1; |
| 963 | if (address >= hose->io_base_phys && |
| 964 | address < (hose->io_base_phys + size)) { |
| 965 | unsigned long base = |
| 966 | (unsigned long)hose->io_base_virt - _IO_BASE; |
| 967 | return base + (address - hose->io_base_phys); |
| 968 | } |
| 969 | } |
| 970 | return (unsigned int)-1; |
| 971 | } |
| 972 | EXPORT_SYMBOL(pci_address_to_pio); |
| 973 | |
| 974 | /* |
| 975 | * Null PCI config access functions, for the case when we can't |
| 976 | * find a hose. |
| 977 | */ |
| 978 | #define NULL_PCI_OP(rw, size, type) \ |
| 979 | static int \ |
| 980 | null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \ |
| 981 | { \ |
| 982 | return PCIBIOS_DEVICE_NOT_FOUND; \ |
| 983 | } |
| 984 | |
| 985 | static int |
| 986 | null_read_config(struct pci_bus *bus, unsigned int devfn, int offset, |
| 987 | int len, u32 *val) |
| 988 | { |
| 989 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 990 | } |
| 991 | |
| 992 | static int |
| 993 | null_write_config(struct pci_bus *bus, unsigned int devfn, int offset, |
| 994 | int len, u32 val) |
| 995 | { |
| 996 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 997 | } |
| 998 | |
| 999 | static struct pci_ops null_pci_ops = |
| 1000 | { |
Nathan Lynch | 6127d1c | 2007-08-10 05:18:42 +1000 | [diff] [blame] | 1001 | .read = null_read_config, |
| 1002 | .write = null_write_config, |
Paul Mackerras | e05b3b4 | 2006-01-15 22:05:47 +1100 | [diff] [blame] | 1003 | }; |
| 1004 | |
| 1005 | /* |
| 1006 | * These functions are used early on before PCI scanning is done |
| 1007 | * and all of the pci_dev and pci_bus structures have been created. |
| 1008 | */ |
| 1009 | static struct pci_bus * |
| 1010 | fake_pci_bus(struct pci_controller *hose, int busnr) |
| 1011 | { |
| 1012 | static struct pci_bus bus; |
| 1013 | |
| 1014 | if (hose == 0) { |
| 1015 | hose = pci_bus_to_hose(busnr); |
| 1016 | if (hose == 0) |
| 1017 | printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr); |
| 1018 | } |
| 1019 | bus.number = busnr; |
| 1020 | bus.sysdata = hose; |
| 1021 | bus.ops = hose? hose->ops: &null_pci_ops; |
| 1022 | return &bus; |
| 1023 | } |
| 1024 | |
| 1025 | #define EARLY_PCI_OP(rw, size, type) \ |
| 1026 | int early_##rw##_config_##size(struct pci_controller *hose, int bus, \ |
| 1027 | int devfn, int offset, type value) \ |
| 1028 | { \ |
| 1029 | return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \ |
| 1030 | devfn, offset, value); \ |
| 1031 | } |
| 1032 | |
| 1033 | EARLY_PCI_OP(read, byte, u8 *) |
| 1034 | EARLY_PCI_OP(read, word, u16 *) |
| 1035 | EARLY_PCI_OP(read, dword, u32 *) |
| 1036 | EARLY_PCI_OP(write, byte, u8) |
| 1037 | EARLY_PCI_OP(write, word, u16) |
| 1038 | EARLY_PCI_OP(write, dword, u32) |
Kumar Gala | 38805e5 | 2007-07-10 23:37:45 -0500 | [diff] [blame] | 1039 | |
| 1040 | extern int pci_bus_find_capability (struct pci_bus *bus, unsigned int devfn, int cap); |
| 1041 | int early_find_capability(struct pci_controller *hose, int bus, int devfn, |
| 1042 | int cap) |
| 1043 | { |
| 1044 | return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap); |
| 1045 | } |