1/* 2 * drivers/pci/setup-bus.c 3 * 4 * Extruded from code written by 5 * Dave Rusling (david.rusling@reo.mts.dec.com) 6 * David Mosberger (davidm@cs.arizona.edu) 7 * David Miller (davem@redhat.com) 8 * 9 * Support routines for initializing a PCI subsystem. 10 */ 11 12/* 13 * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru> 14 * PCI-PCI bridges cleanup, sorted resource allocation. 15 * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru> 16 * Converted to allocation in 3 passes, which gives 17 * tighter packing. Prefetchable range support. 18 */ 19 20#include <linux/init.h> 21#include <linux/kernel.h> 22#include <linux/module.h> 23#include <linux/pci.h> 24#include <linux/errno.h> 25#include <linux/ioport.h> 26#include <linux/cache.h> 27#include <linux/slab.h> 28 29 30#define DEBUG_CONFIG 1 31#if DEBUG_CONFIG 32#define DBG(x...) printk(x) 33#else 34#define DBG(x...) 35#endif 36 37#define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1)) 38 39static void pbus_assign_resources_sorted(struct pci_bus *bus) 40{ 41 struct pci_dev *dev; 42 struct resource *res; 43 struct resource_list head, *list, *tmp; 44 int idx; 45 46 head.next = NULL; 47 list_for_each_entry(dev, &bus->devices, bus_list) { 48 u16 class = dev->class >> 8; 49 50 /* Don't touch classless devices or host bridges or ioapics. */ 51 if (class == PCI_CLASS_NOT_DEFINED || 52 class == PCI_CLASS_BRIDGE_HOST) 53 continue; 54 55 /* Don't touch ioapic devices already enabled by firmware */ 56 if (class == PCI_CLASS_SYSTEM_PIC) { 57 u16 command; 58 pci_read_config_word(dev, PCI_COMMAND, &command); 59 if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) 60 continue; 61 } 62 63 pdev_sort_resources(dev, &head); 64 } 65 66 for (list = head.next; list;) { 67 res = list->res; 68 idx = res - &list->dev->resource[0]; 69 if (pci_assign_resource(list->dev, idx)) { 70 res->start = 0; 71 res->end = 0; 72 res->flags = 0; 73 } 74 tmp = list; 75 list = list->next; 76 kfree(tmp); 77 } 78} 79 80void pci_setup_cardbus(struct pci_bus *bus) 81{ 82 struct pci_dev *bridge = bus->self; 83 struct pci_bus_region region; 84 85 printk("PCI: Bus %d, cardbus bridge: %s\n", 86 bus->number, pci_name(bridge)); 87 88 pcibios_resource_to_bus(bridge, ®ion, bus->resource[0]); 89 if (bus->resource[0]->flags & IORESOURCE_IO) { 90 /* 91 * The IO resource is allocated a range twice as large as it 92 * would normally need. This allows us to set both IO regs. 93 */ 94 printk(" IO window: %08lx-%08lx\n", 95 region.start, region.end); 96 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0, 97 region.start); 98 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0, 99 region.end); 100 } 101 102 pcibios_resource_to_bus(bridge, ®ion, bus->resource[1]); 103 if (bus->resource[1]->flags & IORESOURCE_IO) { 104 printk(" IO window: %08lx-%08lx\n", 105 region.start, region.end); 106 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1, 107 region.start); 108 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1, 109 region.end); 110 } 111 112 pcibios_resource_to_bus(bridge, ®ion, bus->resource[2]); 113 if (bus->resource[2]->flags & IORESOURCE_MEM) { 114 printk(" PREFETCH window: %08lx-%08lx\n", 115 region.start, region.end); 116 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0, 117 region.start); 118 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0, 119 region.end); 120 } 121 122 pcibios_resource_to_bus(bridge, ®ion, bus->resource[3]); 123 if (bus->resource[3]->flags & IORESOURCE_MEM) { 124 printk(" MEM window: %08lx-%08lx\n", 125 region.start, region.end); 126 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1, 127 region.start); 128 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1, 129 region.end); 130 } 131} 132EXPORT_SYMBOL(pci_setup_cardbus); 133 134/* Initialize bridges with base/limit values we have collected. 135 PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998) 136 requires that if there is no I/O ports or memory behind the 137 bridge, corresponding range must be turned off by writing base 138 value greater than limit to the bridge's base/limit registers. 139 140 Note: care must be taken when updating I/O base/limit registers 141 of bridges which support 32-bit I/O. This update requires two 142 config space writes, so it's quite possible that an I/O window of 143 the bridge will have some undesirable address (e.g. 0) after the 144 first write. Ditto 64-bit prefetchable MMIO. */ 145static void __devinit 146pci_setup_bridge(struct pci_bus *bus) 147{ 148 struct pci_dev *bridge = bus->self; 149 struct pci_bus_region region; 150 u32 l, io_upper16; 151 152 DBG(KERN_INFO "PCI: Bridge: %s\n", pci_name(bridge)); 153 154 /* Set up the top and bottom of the PCI I/O segment for this bus. */ 155 pcibios_resource_to_bus(bridge, ®ion, bus->resource[0]); 156 if (bus->resource[0]->flags & IORESOURCE_IO) { 157 pci_read_config_dword(bridge, PCI_IO_BASE, &l); 158 l &= 0xffff0000; 159 l |= (region.start >> 8) & 0x00f0; 160 l |= region.end & 0xf000; 161 /* Set up upper 16 bits of I/O base/limit. */ 162 io_upper16 = (region.end & 0xffff0000) | (region.start >> 16); 163 DBG(KERN_INFO " IO window: %04lx-%04lx\n", 164 region.start, region.end); 165 } 166 else { 167 /* Clear upper 16 bits of I/O base/limit. */ 168 io_upper16 = 0; 169 l = 0x00f0; 170 DBG(KERN_INFO " IO window: disabled.\n"); 171 } 172 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */ 173 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff); 174 /* Update lower 16 bits of I/O base/limit. */ 175 pci_write_config_dword(bridge, PCI_IO_BASE, l); 176 /* Update upper 16 bits of I/O base/limit. */ 177 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16); 178 179 /* Set up the top and bottom of the PCI Memory segment 180 for this bus. */ 181 pcibios_resource_to_bus(bridge, ®ion, bus->resource[1]); 182 if (bus->resource[1]->flags & IORESOURCE_MEM) { 183 l = (region.start >> 16) & 0xfff0; 184 l |= region.end & 0xfff00000; 185 DBG(KERN_INFO " MEM window: %08lx-%08lx\n", 186 region.start, region.end); 187 } 188 else { 189 l = 0x0000fff0; 190 DBG(KERN_INFO " MEM window: disabled.\n"); 191 } 192 pci_write_config_dword(bridge, PCI_MEMORY_BASE, l); 193 194 /* Clear out the upper 32 bits of PREF limit. 195 If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily 196 disables PREF range, which is ok. */ 197 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0); 198 199 /* Set up PREF base/limit. */ 200 pcibios_resource_to_bus(bridge, ®ion, bus->resource[2]); 201 if (bus->resource[2]->flags & IORESOURCE_PREFETCH) { 202 l = (region.start >> 16) & 0xfff0; 203 l |= region.end & 0xfff00000; 204 DBG(KERN_INFO " PREFETCH window: %08lx-%08lx\n", 205 region.start, region.end); 206 } 207 else { 208 l = 0x0000fff0; 209 DBG(KERN_INFO " PREFETCH window: disabled.\n"); 210 } 211 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l); 212 213 /* Clear out the upper 32 bits of PREF base. */ 214 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, 0); 215 216 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl); 217} 218 219/* Check whether the bridge supports optional I/O and 220 prefetchable memory ranges. If not, the respective 221 base/limit registers must be read-only and read as 0. */ 222static void pci_bridge_check_ranges(struct pci_bus *bus) 223{ 224 u16 io; 225 u32 pmem; 226 struct pci_dev *bridge = bus->self; 227 struct resource *b_res; 228 229 b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; 230 b_res[1].flags |= IORESOURCE_MEM; 231 232 pci_read_config_word(bridge, PCI_IO_BASE, &io); 233 if (!io) { 234 pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0); 235 pci_read_config_word(bridge, PCI_IO_BASE, &io); 236 pci_write_config_word(bridge, PCI_IO_BASE, 0x0); 237 } 238 if (io) 239 b_res[0].flags |= IORESOURCE_IO; 240 if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001) 241 return; 242 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); 243 if (!pmem) { 244 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 245 0xfff0fff0); 246 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); 247 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0); 248 } 249 if (pmem) 250 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH; 251} 252 253/* Helper function for sizing routines: find first available 254 bus resource of a given type. Note: we intentionally skip 255 the bus resources which have already been assigned (that is, 256 have non-NULL parent resource). */ 257static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type) 258{ 259 int i; 260 struct resource *r; 261 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 262 IORESOURCE_PREFETCH; 263 264 for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) { 265 r = bus->resource[i]; 266 if (r == &ioport_resource || r == &iomem_resource) 267 continue; 268 if (r && (r->flags & type_mask) == type && !r->parent) 269 return r; 270 } 271 return NULL; 272} 273 274/* Sizing the IO windows of the PCI-PCI bridge is trivial, 275 since these windows have 4K granularity and the IO ranges 276 of non-bridge PCI devices are limited to 256 bytes. 277 We must be careful with the ISA aliasing though. */ 278static void pbus_size_io(struct pci_bus *bus) 279{ 280 struct pci_dev *dev; 281 struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO); 282 unsigned long size = 0, size1 = 0; 283 284 if (!b_res) 285 return; 286 287 list_for_each_entry(dev, &bus->devices, bus_list) { 288 int i; 289 290 for (i = 0; i < PCI_NUM_RESOURCES; i++) { 291 struct resource *r = &dev->resource[i]; 292 unsigned long r_size; 293 294 if (r->parent || !(r->flags & IORESOURCE_IO)) 295 continue; 296 r_size = r->end - r->start + 1; 297 298 if (r_size < 0x400) 299 /* Might be re-aligned for ISA */ 300 size += r_size; 301 else 302 size1 += r_size; 303 } 304 } 305/* To be fixed in 2.5: we should have sort of HAVE_ISA 306 flag in the struct pci_bus. */ 307#if defined(CONFIG_ISA) || defined(CONFIG_EISA) 308 size = (size & 0xff) + ((size & ~0xffUL) << 2); 309#endif 310 size = ROUND_UP(size + size1, 4096); 311 if (!size) { 312 b_res->flags = 0; 313 return; 314 } 315 /* Alignment of the IO window is always 4K */ 316 b_res->start = 4096; 317 b_res->end = b_res->start + size - 1; 318} 319 320/* Calculate the size of the bus and minimal alignment which 321 guarantees that all child resources fit in this size. */ 322static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, unsigned long type) 323{ 324 struct pci_dev *dev; 325 unsigned long min_align, align, size; 326 unsigned long aligns[12]; /* Alignments from 1Mb to 2Gb */ 327 int order, max_order; 328 struct resource *b_res = find_free_bus_resource(bus, type); 329 330 if (!b_res) 331 return 0; 332 333 memset(aligns, 0, sizeof(aligns)); 334 max_order = 0; 335 size = 0; 336 337 list_for_each_entry(dev, &bus->devices, bus_list) { 338 int i; 339 340 for (i = 0; i < PCI_NUM_RESOURCES; i++) { 341 struct resource *r = &dev->resource[i]; 342 unsigned long r_size; 343 344 if (r->parent || (r->flags & mask) != type) 345 continue; 346 r_size = r->end - r->start + 1; 347 /* For bridges size != alignment */ 348 align = (i < PCI_BRIDGE_RESOURCES) ? r_size : r->start; 349 order = __ffs(align) - 20; 350 if (order > 11) { 351 printk(KERN_WARNING "PCI: region %s/%d " 352 "too large: %llx-%llx\n", 353 pci_name(dev), i, 354 (unsigned long long)r->start, 355 (unsigned long long)r->end); 356 r->flags = 0; 357 continue; 358 } 359 size += r_size; 360 if (order < 0) 361 order = 0; 362 /* Exclude ranges with size > align from 363 calculation of the alignment. */ 364 if (r_size == align) 365 aligns[order] += align; 366 if (order > max_order) 367 max_order = order; 368 } 369 } 370 371 align = 0; 372 min_align = 0; 373 for (order = 0; order <= max_order; order++) { 374 unsigned long align1 = 1UL << (order + 20); 375 376 if (!align) 377 min_align = align1; 378 else if (ROUND_UP(align + min_align, min_align) < align1) 379 min_align = align1 >> 1; 380 align += aligns[order]; 381 } 382 size = ROUND_UP(size, min_align); 383 if (!size) { 384 b_res->flags = 0; 385 return 1; 386 } 387 b_res->start = min_align; 388 b_res->end = size + min_align - 1; 389 return 1; 390} 391 392static void __devinit 393pci_bus_size_cardbus(struct pci_bus *bus) 394{ 395 struct pci_dev *bridge = bus->self; 396 struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; 397 u16 ctrl; 398 399 /* 400 * Reserve some resources for CardBus. We reserve 401 * a fixed amount of bus space for CardBus bridges. 402 */ 403 b_res[0].start = pci_cardbus_io_size; 404 b_res[0].end = b_res[0].start + pci_cardbus_io_size - 1; 405 b_res[0].flags |= IORESOURCE_IO; 406 407 b_res[1].start = pci_cardbus_io_size; 408 b_res[1].end = b_res[1].start + pci_cardbus_io_size - 1; 409 b_res[1].flags |= IORESOURCE_IO; 410 411 /* 412 * Check whether prefetchable memory is supported 413 * by this bridge. 414 */ 415 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 416 if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) { 417 ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0; 418 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); 419 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 420 } 421 422 /* 423 * If we have prefetchable memory support, allocate 424 * two regions. Otherwise, allocate one region of 425 * twice the size. 426 */ 427 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) { 428 b_res[2].start = pci_cardbus_mem_size; 429 b_res[2].end = b_res[2].start + pci_cardbus_mem_size - 1; 430 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH; 431 432 b_res[3].start = pci_cardbus_mem_size; 433 b_res[3].end = b_res[3].start + pci_cardbus_mem_size - 1; 434 b_res[3].flags |= IORESOURCE_MEM; 435 } else { 436 b_res[3].start = pci_cardbus_mem_size * 2; 437 b_res[3].end = b_res[3].start + pci_cardbus_mem_size * 2 - 1; 438 b_res[3].flags |= IORESOURCE_MEM; 439 } 440} 441 442void pci_bus_size_bridges(struct pci_bus *bus) 443{ 444 struct pci_dev *dev; 445 unsigned long mask, prefmask; 446 447 list_for_each_entry(dev, &bus->devices, bus_list) { 448 struct pci_bus *b = dev->subordinate; 449 if (!b) 450 continue; 451 452 switch (dev->class >> 8) { 453 case PCI_CLASS_BRIDGE_CARDBUS: 454 pci_bus_size_cardbus(b); 455 break; 456 457 case PCI_CLASS_BRIDGE_PCI: 458 default: 459 pci_bus_size_bridges(b); 460 break; 461 } 462 } 463 464 /* The root bus? */ 465 if (!bus->self) 466 return; 467 468 switch (bus->self->class >> 8) { 469 case PCI_CLASS_BRIDGE_CARDBUS: 470 /* don't size cardbuses yet. */ 471 break; 472 473 case PCI_CLASS_BRIDGE_PCI: 474 pci_bridge_check_ranges(bus); 475 default: 476 pbus_size_io(bus); 477 /* If the bridge supports prefetchable range, size it 478 separately. If it doesn't, or its prefetchable window 479 has already been allocated by arch code, try 480 non-prefetchable range for both types of PCI memory 481 resources. */ 482 mask = IORESOURCE_MEM; 483 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH; 484 if (pbus_size_mem(bus, prefmask, prefmask)) 485 mask = prefmask; /* Success, size non-prefetch only. */ 486 pbus_size_mem(bus, mask, IORESOURCE_MEM); 487 break; 488 } 489} 490EXPORT_SYMBOL(pci_bus_size_bridges); 491 492void pci_bus_assign_resources(struct pci_bus *bus) 493{ 494 struct pci_bus *b; 495 struct pci_dev *dev; 496 497 pbus_assign_resources_sorted(bus); 498 499 list_for_each_entry(dev, &bus->devices, bus_list) { 500 b = dev->subordinate; 501 if (!b) 502 continue; 503 504 pci_bus_assign_resources(b); 505 506 switch (dev->class >> 8) { 507 case PCI_CLASS_BRIDGE_PCI: 508 pci_setup_bridge(b); 509 break; 510 511 case PCI_CLASS_BRIDGE_CARDBUS: 512 pci_setup_cardbus(b); 513 break; 514 515 default: 516 printk(KERN_INFO "PCI: not setting up bridge %s " 517 "for bus %d\n", pci_name(dev), b->number); 518 break; 519 } 520 } 521} 522EXPORT_SYMBOL(pci_bus_assign_resources); 523 524void __init 525pci_assign_unassigned_resources(void) 526{ 527 struct pci_bus *bus; 528 529 /* Depth first, calculate sizes and alignments of all 530 subordinate buses. */ 531 list_for_each_entry(bus, &pci_root_buses, node) { 532 pci_bus_size_bridges(bus); 533 } 534 /* Depth last, allocate resources and update the hardware. */ 535 list_for_each_entry(bus, &pci_root_buses, node) { 536 pci_bus_assign_resources(bus); 537 pci_enable_bridges(bus); 538 } 539} 540