00001
00002
00003
00004
00005 #include <linux/kernel.h>
00006 #include <linux/delay.h>
00007 #include <linux/device.h>
00008 #include <linux/init.h>
00009 #include <linux/pci.h>
00010 #include <linux/slab.h>
00011 #include <linux/module.h>
00012 #include <linux/cpumask.h>
00013 #include <linux/pci-aspm.h>
00014 #include "pci.h"
00015
00016 #define CARDBUS_LATENCY_TIMER 176
00017 #define CARDBUS_RESERVE_BUSNR 3
00018
00019
00020 LIST_HEAD(pci_root_buses);
00021 EXPORT_SYMBOL(pci_root_buses);
00022
00023 #ifdef DDE_LINUX
00024 #include "local.h"
00025 #endif
00026
00027 static int find_anything(struct device *dev, void *data)
00028 {
00029 return 1;
00030 }
00031
00032
00033
00034
00035
00036
00037 int no_pci_devices(void)
00038 {
00039 struct device *dev;
00040 int no_devices;
00041
00042 dev = bus_find_device(&pci_bus_type, NULL, NULL, find_anything);
00043 no_devices = (dev == NULL);
00044 put_device(dev);
00045 return no_devices;
00046 }
00047 EXPORT_SYMBOL(no_pci_devices);
00048
00049
00050
00051
00052 static ssize_t pci_bus_show_cpuaffinity(struct device *dev,
00053 int type,
00054 struct device_attribute *attr,
00055 char *buf)
00056 {
00057 int ret;
00058 const struct cpumask *cpumask;
00059
00060 cpumask = cpumask_of_pcibus(to_pci_bus(dev));
00061 ret = type?
00062 cpulist_scnprintf(buf, PAGE_SIZE-2, cpumask) :
00063 cpumask_scnprintf(buf, PAGE_SIZE-2, cpumask);
00064 buf[ret++] = '\n';
00065 buf[ret] = '\0';
00066 return ret;
00067 }
00068
00069 static ssize_t inline pci_bus_show_cpumaskaffinity(struct device *dev,
00070 struct device_attribute *attr,
00071 char *buf)
00072 {
00073 return pci_bus_show_cpuaffinity(dev, 0, attr, buf);
00074 }
00075
00076 static ssize_t inline pci_bus_show_cpulistaffinity(struct device *dev,
00077 struct device_attribute *attr,
00078 char *buf)
00079 {
00080 return pci_bus_show_cpuaffinity(dev, 1, attr, buf);
00081 }
00082
00083 DEVICE_ATTR(cpuaffinity, S_IRUGO, pci_bus_show_cpumaskaffinity, NULL);
00084 DEVICE_ATTR(cpulistaffinity, S_IRUGO, pci_bus_show_cpulistaffinity, NULL);
00085
00086
00087
00088
00089 static void release_pcibus_dev(struct device *dev)
00090 {
00091 struct pci_bus *pci_bus = to_pci_bus(dev);
00092
00093 if (pci_bus->bridge)
00094 put_device(pci_bus->bridge);
00095 kfree(pci_bus);
00096 }
00097
00098 static struct class pcibus_class = {
00099 .name = "pci_bus",
00100 .dev_release = &release_pcibus_dev,
00101 };
00102
00103 static int __init pcibus_class_init(void)
00104 {
00105 return class_register(&pcibus_class);
00106 }
00107 postcore_initcall(pcibus_class_init);
00108
00109
00110
00111
00112
00113 static inline unsigned int pci_calc_resource_flags(unsigned int flags)
00114 {
00115 if (flags & PCI_BASE_ADDRESS_SPACE_IO)
00116 return IORESOURCE_IO;
00117
00118 if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH)
00119 return IORESOURCE_MEM | IORESOURCE_PREFETCH;
00120
00121 return IORESOURCE_MEM;
00122 }
00123
00124 static u64 pci_size(u64 base, u64 maxbase, u64 mask)
00125 {
00126 u64 size = mask & maxbase;
00127 if (!size)
00128 return 0;
00129
00130
00131
00132 size = (size & ~(size-1)) - 1;
00133
00134
00135
00136 if (base == maxbase && ((base | size) & mask) != mask)
00137 return 0;
00138
00139 return size;
00140 }
00141
00142 static inline enum pci_bar_type decode_bar(struct resource *res, u32 bar)
00143 {
00144 if ((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
00145 res->flags = bar & ~PCI_BASE_ADDRESS_IO_MASK;
00146 return pci_bar_io;
00147 }
00148
00149 res->flags = bar & ~PCI_BASE_ADDRESS_MEM_MASK;
00150
00151 if (res->flags & PCI_BASE_ADDRESS_MEM_TYPE_64)
00152 return pci_bar_mem64;
00153 return pci_bar_mem32;
00154 }
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
00166 struct resource *res, unsigned int pos)
00167 {
00168 u32 l, sz, mask;
00169
00170 mask = type ? ~PCI_ROM_ADDRESS_ENABLE : ~0;
00171
00172 res->name = pci_name(dev);
00173
00174 pci_read_config_dword(dev, pos, &l);
00175 pci_write_config_dword(dev, pos, mask);
00176 pci_read_config_dword(dev, pos, &sz);
00177 pci_write_config_dword(dev, pos, l);
00178
00179
00180
00181
00182
00183
00184
00185 if (!sz || sz == 0xffffffff)
00186 goto fail;
00187
00188
00189
00190
00191
00192 if (l == 0xffffffff)
00193 l = 0;
00194
00195 if (type == pci_bar_unknown) {
00196 type = decode_bar(res, l);
00197 res->flags |= pci_calc_resource_flags(l) | IORESOURCE_SIZEALIGN;
00198 if (type == pci_bar_io) {
00199 l &= PCI_BASE_ADDRESS_IO_MASK;
00200 mask = PCI_BASE_ADDRESS_IO_MASK & 0xffff;
00201 } else {
00202 l &= PCI_BASE_ADDRESS_MEM_MASK;
00203 mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
00204 }
00205 } else {
00206 res->flags |= (l & IORESOURCE_ROM_ENABLE);
00207 l &= PCI_ROM_ADDRESS_MASK;
00208 mask = (u32)PCI_ROM_ADDRESS_MASK;
00209 }
00210
00211 if (type == pci_bar_mem64) {
00212 u64 l64 = l;
00213 u64 sz64 = sz;
00214 u64 mask64 = mask | (u64)~0 << 32;
00215
00216 pci_read_config_dword(dev, pos + 4, &l);
00217 pci_write_config_dword(dev, pos + 4, ~0);
00218 pci_read_config_dword(dev, pos + 4, &sz);
00219 pci_write_config_dword(dev, pos + 4, l);
00220
00221 l64 |= ((u64)l << 32);
00222 sz64 |= ((u64)sz << 32);
00223
00224 sz64 = pci_size(l64, sz64, mask64);
00225
00226 if (!sz64)
00227 goto fail;
00228
00229 if ((sizeof(resource_size_t) < 8) && (sz64 > 0x100000000ULL)) {
00230 dev_err(&dev->dev, "can't handle 64-bit BAR\n");
00231 goto fail;
00232 } else if ((sizeof(resource_size_t) < 8) && l) {
00233
00234 pci_write_config_dword(dev, pos, 0);
00235 pci_write_config_dword(dev, pos + 4, 0);
00236 res->start = 0;
00237 res->end = sz64;
00238 } else {
00239 res->start = l64;
00240 res->end = l64 + sz64;
00241 dev_printk(KERN_DEBUG, &dev->dev,
00242 "reg %x 64bit mmio: %pR\n", pos, res);
00243 }
00244 } else {
00245 sz = pci_size(l, sz, mask);
00246
00247 if (!sz)
00248 goto fail;
00249
00250 res->start = l;
00251 res->end = l + sz;
00252
00253 dev_printk(KERN_DEBUG, &dev->dev, "reg %x %s: %pR\n", pos,
00254 (res->flags & IORESOURCE_IO) ? "io port" : "32bit mmio",
00255 res);
00256 }
00257
00258 out:
00259 return (type == pci_bar_mem64) ? 1 : 0;
00260 fail:
00261 res->flags = 0;
00262 goto out;
00263 }
00264
00265 static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
00266 {
00267 unsigned int pos, reg;
00268
00269 for (pos = 0; pos < howmany; pos++) {
00270 struct resource *res = &dev->resource[pos];
00271 reg = PCI_BASE_ADDRESS_0 + (pos << 2);
00272 pos += __pci_read_base(dev, pci_bar_unknown, res, reg);
00273 }
00274
00275 if (rom) {
00276 struct resource *res = &dev->resource[PCI_ROM_RESOURCE];
00277 dev->rom_base_reg = rom;
00278 res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH |
00279 IORESOURCE_READONLY | IORESOURCE_CACHEABLE |
00280 IORESOURCE_SIZEALIGN;
00281 __pci_read_base(dev, pci_bar_mem32, res, rom);
00282 }
00283 }
00284
00285 void __devinit pci_read_bridge_bases(struct pci_bus *child)
00286 {
00287 struct pci_dev *dev = child->self;
00288 u8 io_base_lo, io_limit_lo;
00289 u16 mem_base_lo, mem_limit_lo;
00290 unsigned long base, limit;
00291 struct resource *res;
00292 int i;
00293
00294 if (!dev)
00295 return;
00296
00297 if (dev->transparent) {
00298 dev_info(&dev->dev, "transparent bridge\n");
00299 for(i = 3; i < PCI_BUS_NUM_RESOURCES; i++)
00300 child->resource[i] = child->parent->resource[i - 3];
00301 }
00302
00303 res = child->resource[0];
00304 pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
00305 pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
00306 base = (io_base_lo & PCI_IO_RANGE_MASK) << 8;
00307 limit = (io_limit_lo & PCI_IO_RANGE_MASK) << 8;
00308
00309 if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
00310 u16 io_base_hi, io_limit_hi;
00311 pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
00312 pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
00313 base |= (io_base_hi << 16);
00314 limit |= (io_limit_hi << 16);
00315 }
00316
00317 if (base <= limit) {
00318 res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
00319 if (!res->start)
00320 res->start = base;
00321 if (!res->end)
00322 res->end = limit + 0xfff;
00323 dev_printk(KERN_DEBUG, &dev->dev, "bridge io port: %pR\n", res);
00324 }
00325
00326 res = child->resource[1];
00327 pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
00328 pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
00329 base = (mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
00330 limit = (mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
00331 if (base <= limit) {
00332 res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM;
00333 res->start = base;
00334 res->end = limit + 0xfffff;
00335 dev_printk(KERN_DEBUG, &dev->dev, "bridge 32bit mmio: %pR\n",
00336 res);
00337 }
00338
00339 res = child->resource[2];
00340 pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
00341 pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
00342 base = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
00343 limit = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
00344
00345 if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
00346 u32 mem_base_hi, mem_limit_hi;
00347 pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
00348 pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
00349
00350
00351
00352
00353
00354
00355 if (mem_base_hi <= mem_limit_hi) {
00356 #if BITS_PER_LONG == 64
00357 base |= ((long) mem_base_hi) << 32;
00358 limit |= ((long) mem_limit_hi) << 32;
00359 #else
00360 if (mem_base_hi || mem_limit_hi) {
00361 dev_err(&dev->dev, "can't handle 64-bit "
00362 "address space for bridge\n");
00363 return;
00364 }
00365 #endif
00366 }
00367 }
00368 if (base <= limit) {
00369 res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM | IORESOURCE_PREFETCH;
00370 res->start = base;
00371 res->end = limit + 0xfffff;
00372 dev_printk(KERN_DEBUG, &dev->dev, "bridge %sbit mmio pref: %pR\n",
00373 (res->flags & PCI_PREF_RANGE_TYPE_64) ? "64" : "32",
00374 res);
00375 }
00376 }
00377
00378 static struct pci_bus * pci_alloc_bus(void)
00379 {
00380 struct pci_bus *b;
00381
00382 b = kzalloc(sizeof(*b), GFP_KERNEL);
00383 if (b) {
00384 INIT_LIST_HEAD(&b->node);
00385 INIT_LIST_HEAD(&b->children);
00386 INIT_LIST_HEAD(&b->devices);
00387 INIT_LIST_HEAD(&b->slots);
00388 }
00389 return b;
00390 }
00391
00392 static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
00393 struct pci_dev *bridge, int busnr)
00394 {
00395 struct pci_bus *child;
00396 int i;
00397
00398
00399
00400
00401 child = pci_alloc_bus();
00402 if (!child)
00403 return NULL;
00404
00405 child->parent = parent;
00406 child->ops = parent->ops;
00407 child->sysdata = parent->sysdata;
00408 child->bus_flags = parent->bus_flags;
00409
00410
00411
00412
00413
00414 child->dev.class = &pcibus_class;
00415 dev_set_name(&child->dev, "%04x:%02x", pci_domain_nr(child), busnr);
00416
00417
00418
00419
00420
00421 child->number = child->secondary = busnr;
00422 child->primary = parent->secondary;
00423 child->subordinate = 0xff;
00424
00425 if (!bridge)
00426 return child;
00427
00428 child->self = bridge;
00429 child->bridge = get_device(&bridge->dev);
00430
00431
00432 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
00433 child->resource[i] = &bridge->resource[PCI_BRIDGE_RESOURCES+i];
00434 child->resource[i]->name = child->name;
00435 }
00436 bridge->subordinate = child;
00437
00438 return child;
00439 }
00440
00441 struct pci_bus *__ref pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev, int busnr)
00442 {
00443 struct pci_bus *child;
00444
00445 child = pci_alloc_child_bus(parent, dev, busnr);
00446 if (child) {
00447 down_write(&pci_bus_sem);
00448 list_add_tail(&child->node, &parent->children);
00449 up_write(&pci_bus_sem);
00450 }
00451 return child;
00452 }
00453
00454 static void pci_fixup_parent_subordinate_busnr(struct pci_bus *child, int max)
00455 {
00456 struct pci_bus *parent = child->parent;
00457
00458 #ifndef DDE_LINUX
00459
00460
00461 if (!pcibios_assign_all_busses())
00462 return;
00463 #endif
00464
00465 while (parent->parent && parent->subordinate < max) {
00466 parent->subordinate = max;
00467 pci_write_config_byte(parent->self, PCI_SUBORDINATE_BUS, max);
00468 parent = parent->parent;
00469 }
00470 }
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482 int __devinit pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max, int pass)
00483 {
00484 struct pci_bus *child;
00485 int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);
00486 u32 buses, i, j = 0;
00487 u16 bctl;
00488 int broken = 0;
00489
00490 pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses);
00491
00492 dev_dbg(&dev->dev, "scanning behind bridge, config %06x, pass %d\n",
00493 buses & 0xffffff, pass);
00494
00495
00496 if (!pass &&
00497 ((buses & 0xff) != bus->number || ((buses >> 8) & 0xff) <= bus->number)) {
00498 dev_dbg(&dev->dev, "bus configuration invalid, reconfiguring\n");
00499 broken = 1;
00500 }
00501
00502
00503
00504 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bctl);
00505 pci_write_config_word(dev, PCI_BRIDGE_CONTROL,
00506 bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT);
00507
00508 if ((buses & 0xffff00) && !pcibios_assign_all_busses() && !is_cardbus && !broken) {
00509 unsigned int cmax, busnr;
00510
00511
00512
00513
00514 if (pass)
00515 goto out;
00516 busnr = (buses >> 8) & 0xFF;
00517
00518
00519
00520
00521
00522 if (pci_find_bus(pci_domain_nr(bus), busnr)) {
00523 dev_info(&dev->dev, "bus %04x:%02x already known\n",
00524 pci_domain_nr(bus), busnr);
00525 goto out;
00526 }
00527
00528 child = pci_add_new_bus(bus, dev, busnr);
00529 if (!child)
00530 goto out;
00531 child->primary = buses & 0xFF;
00532 child->subordinate = (buses >> 16) & 0xFF;
00533 child->bridge_ctl = bctl;
00534
00535 cmax = pci_scan_child_bus(child);
00536 if (cmax > max)
00537 max = cmax;
00538 if (child->subordinate > max)
00539 max = child->subordinate;
00540 } else {
00541 #ifndef DDE_LINUX
00542
00543
00544
00545
00546 if (!pass) {
00547 if (pcibios_assign_all_busses() || broken)
00548
00549
00550
00551
00552
00553
00554 pci_write_config_dword(dev, PCI_PRIMARY_BUS,
00555 buses & ~0xffffff);
00556 goto out;
00557 }
00558 #endif
00559
00560
00561 pci_write_config_word(dev, PCI_STATUS, 0xffff);
00562
00563
00564
00565 if (pci_find_bus(pci_domain_nr(bus), max+1))
00566 goto out;
00567 child = pci_add_new_bus(bus, dev, ++max);
00568 buses = (buses & 0xff000000)
00569 | ((unsigned int)(child->primary) << 0)
00570 | ((unsigned int)(child->secondary) << 8)
00571 | ((unsigned int)(child->subordinate) << 16);
00572
00573
00574
00575
00576
00577 if (is_cardbus) {
00578 buses &= ~0xff000000;
00579 buses |= CARDBUS_LATENCY_TIMER << 24;
00580 }
00581
00582
00583
00584
00585 pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses);
00586
00587 if (!is_cardbus) {
00588 child->bridge_ctl = bctl;
00589
00590
00591
00592
00593
00594
00595 pci_fixup_parent_subordinate_busnr(child, max);
00596
00597 max = pci_scan_child_bus(child);
00598
00599
00600
00601
00602 pci_fixup_parent_subordinate_busnr(child, max);
00603 } else {
00604
00605
00606
00607
00608
00609 for (i=0; i<CARDBUS_RESERVE_BUSNR; i++) {
00610 struct pci_bus *parent = bus;
00611 if (pci_find_bus(pci_domain_nr(bus),
00612 max+i+1))
00613 break;
00614 while (parent->parent) {
00615 if ((!pcibios_assign_all_busses()) &&
00616 (parent->subordinate > max) &&
00617 (parent->subordinate <= max+i)) {
00618 j = 1;
00619 }
00620 parent = parent->parent;
00621 }
00622 if (j) {
00623
00624
00625
00626
00627
00628 i /= 2;
00629 break;
00630 }
00631 }
00632 max += i;
00633 pci_fixup_parent_subordinate_busnr(child, max);
00634 }
00635
00636
00637
00638 child->subordinate = max;
00639 pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max);
00640 }
00641
00642 sprintf(child->name,
00643 (is_cardbus ? "PCI CardBus %04x:%02x" : "PCI Bus %04x:%02x"),
00644 pci_domain_nr(bus), child->number);
00645
00646
00647 while (bus->parent) {
00648 if ((child->subordinate > bus->subordinate) ||
00649 (child->number > bus->subordinate) ||
00650 (child->number < bus->number) ||
00651 (child->subordinate < bus->number)) {
00652 pr_debug("PCI: Bus #%02x (-#%02x) is %s "
00653 "hidden behind%s bridge #%02x (-#%02x)\n",
00654 child->number, child->subordinate,
00655 (bus->number > child->subordinate &&
00656 bus->subordinate < child->number) ?
00657 "wholly" : "partially",
00658 bus->self->transparent ? " transparent" : "",
00659 bus->number, bus->subordinate);
00660 }
00661 bus = bus->parent;
00662 }
00663
00664 out:
00665 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl);
00666
00667 return max;
00668 }
00669
00670
00671
00672
00673
00674 static void pci_read_irq(struct pci_dev *dev)
00675 {
00676 unsigned char irq;
00677
00678 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq);
00679 dev->pin = irq;
00680 if (irq)
00681 pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
00682 dev->irq = irq;
00683 }
00684
00685 #define LEGACY_IO_RESOURCE (IORESOURCE_IO | IORESOURCE_PCI_FIXED)
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697 static int pci_setup_device(struct pci_dev * dev)
00698 {
00699 u32 class;
00700
00701 dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus),
00702 dev->bus->number, PCI_SLOT(dev->devfn),
00703 PCI_FUNC(dev->devfn));
00704
00705 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
00706 dev->revision = class & 0xff;
00707 class >>= 8;
00708 dev->class = class;
00709 class >>= 8;
00710
00711 dev_dbg(&dev->dev, "found [%04x:%04x] class %06x header type %02x\n",
00712 dev->vendor, dev->device, class, dev->hdr_type);
00713
00714
00715 dev->current_state = PCI_UNKNOWN;
00716
00717
00718 pci_fixup_device(pci_fixup_early, dev);
00719 class = dev->class >> 8;
00720
00721 switch (dev->hdr_type) {
00722 case PCI_HEADER_TYPE_NORMAL:
00723 if (class == PCI_CLASS_BRIDGE_PCI)
00724 goto bad;
00725 pci_read_irq(dev);
00726 pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
00727 pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
00728 pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &dev->subsystem_device);
00729
00730
00731
00732
00733
00734
00735
00736 if (class == PCI_CLASS_STORAGE_IDE) {
00737 u8 progif;
00738 pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
00739 if ((progif & 1) == 0) {
00740 dev->resource[0].start = 0x1F0;
00741 dev->resource[0].end = 0x1F7;
00742 dev->resource[0].flags = LEGACY_IO_RESOURCE;
00743 dev->resource[1].start = 0x3F6;
00744 dev->resource[1].end = 0x3F6;
00745 dev->resource[1].flags = LEGACY_IO_RESOURCE;
00746 }
00747 if ((progif & 4) == 0) {
00748 dev->resource[2].start = 0x170;
00749 dev->resource[2].end = 0x177;
00750 dev->resource[2].flags = LEGACY_IO_RESOURCE;
00751 dev->resource[3].start = 0x376;
00752 dev->resource[3].end = 0x376;
00753 dev->resource[3].flags = LEGACY_IO_RESOURCE;
00754 }
00755 }
00756 break;
00757
00758 case PCI_HEADER_TYPE_BRIDGE:
00759 if (class != PCI_CLASS_BRIDGE_PCI)
00760 goto bad;
00761
00762
00763
00764 pci_read_irq(dev);
00765 dev->transparent = ((dev->class & 0xff) == 1);
00766 pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
00767 break;
00768
00769 case PCI_HEADER_TYPE_CARDBUS:
00770 if (class != PCI_CLASS_BRIDGE_CARDBUS)
00771 goto bad;
00772 pci_read_irq(dev);
00773 pci_read_bases(dev, 1, 0);
00774 pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
00775 pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device);
00776 break;
00777
00778 default:
00779 dev_err(&dev->dev, "unknown header type %02x, "
00780 "ignoring device\n", dev->hdr_type);
00781 return -1;
00782
00783 bad:
00784 dev_err(&dev->dev, "ignoring class %02x (doesn't match header "
00785 "type %02x)\n", class, dev->hdr_type);
00786 dev->class = PCI_CLASS_NOT_DEFINED;
00787 }
00788
00789
00790 return 0;
00791 }
00792
00793 static void pci_release_capabilities(struct pci_dev *dev)
00794 {
00795 pci_vpd_release(dev);
00796 }
00797
00798
00799
00800
00801
00802
00803
00804
00805 static void pci_release_dev(struct device *dev)
00806 {
00807 struct pci_dev *pci_dev;
00808
00809 pci_dev = to_pci_dev(dev);
00810 pci_release_capabilities(pci_dev);
00811 kfree(pci_dev);
00812 }
00813
00814 static void set_pcie_port_type(struct pci_dev *pdev)
00815 {
00816 int pos;
00817 u16 reg16;
00818
00819 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
00820 if (!pos)
00821 return;
00822 pdev->is_pcie = 1;
00823 pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, ®16);
00824 pdev->pcie_type = (reg16 & PCI_EXP_FLAGS_TYPE) >> 4;
00825 }
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838 int pci_cfg_space_size_ext(struct pci_dev *dev)
00839 {
00840 u32 status;
00841 int pos = PCI_CFG_SPACE_SIZE;
00842
00843 if (pci_read_config_dword(dev, pos, &status) != PCIBIOS_SUCCESSFUL)
00844 goto fail;
00845 if (status == 0xffffffff)
00846 goto fail;
00847
00848 return PCI_CFG_SPACE_EXP_SIZE;
00849
00850 fail:
00851 return PCI_CFG_SPACE_SIZE;
00852 }
00853
00854 int pci_cfg_space_size(struct pci_dev *dev)
00855 {
00856 int pos;
00857 u32 status;
00858
00859 pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
00860 if (!pos) {
00861 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
00862 if (!pos)
00863 goto fail;
00864
00865 pci_read_config_dword(dev, pos + PCI_X_STATUS, &status);
00866 if (!(status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ)))
00867 goto fail;
00868 }
00869
00870 return pci_cfg_space_size_ext(dev);
00871
00872 fail:
00873 return PCI_CFG_SPACE_SIZE;
00874 }
00875
00876 static void pci_release_bus_bridge_dev(struct device *dev)
00877 {
00878 kfree(dev);
00879 }
00880
00881 struct pci_dev *alloc_pci_dev(void)
00882 {
00883 struct pci_dev *dev;
00884
00885 dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
00886 if (!dev)
00887 return NULL;
00888
00889 INIT_LIST_HEAD(&dev->bus_list);
00890
00891 return dev;
00892 }
00893 EXPORT_SYMBOL(alloc_pci_dev);
00894
00895
00896
00897
00898
00899 static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn)
00900 {
00901 struct pci_dev *dev;
00902 struct pci_slot *slot;
00903 u32 l;
00904 u8 hdr_type;
00905 int delay = 1;
00906
00907 if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, &l))
00908 return NULL;
00909
00910
00911 if (l == 0xffffffff || l == 0x00000000 ||
00912 l == 0x0000ffff || l == 0xffff0000)
00913 return NULL;
00914
00915
00916 while (l == 0xffff0001) {
00917 msleep(delay);
00918 delay *= 2;
00919 if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, &l))
00920 return NULL;
00921
00922 if (delay > 60 * 1000) {
00923 printk(KERN_WARNING "pci %04x:%02x:%02x.%d: not "
00924 "responding\n", pci_domain_nr(bus),
00925 bus->number, PCI_SLOT(devfn),
00926 PCI_FUNC(devfn));
00927 return NULL;
00928 }
00929 }
00930
00931 if (pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type))
00932 return NULL;
00933
00934 dev = alloc_pci_dev();
00935 if (!dev)
00936 return NULL;
00937
00938 dev->bus = bus;
00939 dev->sysdata = bus->sysdata;
00940 dev->dev.parent = bus->bridge;
00941 dev->dev.bus = &pci_bus_type;
00942 dev->devfn = devfn;
00943 dev->hdr_type = hdr_type & 0x7f;
00944 dev->multifunction = !!(hdr_type & 0x80);
00945 dev->vendor = l & 0xffff;
00946 dev->device = (l >> 16) & 0xffff;
00947 dev->cfg_size = pci_cfg_space_size(dev);
00948 dev->error_state = pci_channel_io_normal;
00949 set_pcie_port_type(dev);
00950
00951 list_for_each_entry(slot, &bus->slots, list)
00952 if (PCI_SLOT(devfn) == slot->number)
00953 dev->slot = slot;
00954
00955
00956
00957 dev->dma_mask = 0xffffffff;
00958 if (pci_setup_device(dev) < 0) {
00959 kfree(dev);
00960 return NULL;
00961 }
00962
00963 return dev;
00964 }
00965
00966 static void pci_init_capabilities(struct pci_dev *dev)
00967 {
00968
00969 pci_msi_init_pci_dev(dev);
00970
00971
00972 pci_allocate_cap_save_buffers(dev);
00973
00974
00975 pci_pm_init(dev);
00976 platform_pci_wakeup_init(dev);
00977
00978
00979 pci_vpd_pci22_init(dev);
00980
00981
00982 pci_enable_ari(dev);
00983 }
00984
00985 void pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
00986 {
00987 device_initialize(&dev->dev);
00988 dev->dev.release = pci_release_dev;
00989 pci_dev_get(dev);
00990
00991 dev->dev.dma_mask = &dev->dma_mask;
00992 dev->dev.dma_parms = &dev->dma_parms;
00993 dev->dev.coherent_dma_mask = 0xffffffffull;
00994
00995 pci_set_dma_max_seg_size(dev, 65536);
00996 pci_set_dma_seg_boundary(dev, 0xffffffff);
00997
00998
00999 pci_fixup_device(pci_fixup_header, dev);
01000
01001
01002 pci_init_capabilities(dev);
01003
01004
01005
01006
01007
01008 down_write(&pci_bus_sem);
01009 list_add_tail(&dev->bus_list, &bus->devices);
01010 up_write(&pci_bus_sem);
01011 }
01012
01013 struct pci_dev *__ref pci_scan_single_device(struct pci_bus *bus, int devfn)
01014 {
01015 struct pci_dev *dev;
01016
01017 dev = pci_scan_device(bus, devfn);
01018 if (!dev)
01019 return NULL;
01020
01021 pci_device_add(dev, bus);
01022
01023 return dev;
01024 }
01025 EXPORT_SYMBOL(pci_scan_single_device);
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036 int pci_scan_slot(struct pci_bus *bus, int devfn)
01037 {
01038 int func, nr = 0;
01039 int scan_all_fns;
01040
01041 scan_all_fns = pcibios_scan_all_fns(bus, devfn);
01042
01043 for (func = 0; func < 8; func++, devfn++) {
01044 struct pci_dev *dev;
01045
01046 dev = pci_scan_single_device(bus, devfn);
01047 if (dev) {
01048 nr++;
01049
01050
01051
01052
01053
01054 if (!dev->multifunction) {
01055 if (func > 0) {
01056 dev->multifunction = 1;
01057 } else {
01058 break;
01059 }
01060 }
01061 } else {
01062 if (func == 0 && !scan_all_fns)
01063 break;
01064 }
01065 }
01066
01067
01068 if (bus->self && nr)
01069 pcie_aspm_init_link_state(bus->self);
01070
01071 return nr;
01072 }
01073
01074 unsigned int __devinit pci_scan_child_bus(struct pci_bus *bus)
01075 {
01076 unsigned int devfn, pass, max = bus->secondary;
01077 struct pci_dev *dev;
01078
01079 pr_debug("PCI: Scanning bus %04x:%02x\n", pci_domain_nr(bus), bus->number);
01080
01081
01082 for (devfn = 0; devfn < 0x100; devfn += 8)
01083 pci_scan_slot(bus, devfn);
01084
01085 #ifndef DDE_LINUX
01086
01087
01088
01089
01090 pr_debug("PCI: Fixups for bus %04x:%02x\n", pci_domain_nr(bus), bus->number);
01091 pcibios_fixup_bus(bus);
01092 for (pass=0; pass < 2; pass++)
01093 list_for_each_entry(dev, &bus->devices, bus_list) {
01094 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
01095 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
01096 max = pci_scan_bridge(bus, dev, max, pass);
01097 }
01098 #endif
01099
01100
01101
01102
01103
01104
01105
01106
01107 pr_debug("PCI: Bus scan for %04x:%02x returning with max=%02x\n",
01108 pci_domain_nr(bus), bus->number, max);
01109 return max;
01110 }
01111
01112 void __attribute__((weak)) set_pci_bus_resources_arch_default(struct pci_bus *b)
01113 {
01114 }
01115
01116 struct pci_bus * pci_create_bus(struct device *parent,
01117 int bus, struct pci_ops *ops, void *sysdata)
01118 {
01119 int error;
01120 struct pci_bus *b;
01121 struct device *dev;
01122
01123 b = pci_alloc_bus();
01124 if (!b)
01125 return NULL;
01126
01127 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
01128 if (!dev){
01129 kfree(b);
01130 return NULL;
01131 }
01132
01133 b->sysdata = sysdata;
01134 b->ops = ops;
01135
01136 if (pci_find_bus(pci_domain_nr(b), bus)) {
01137
01138 pr_debug("PCI: Bus %04x:%02x already known\n", pci_domain_nr(b), bus);
01139 goto err_out;
01140 }
01141
01142 down_write(&pci_bus_sem);
01143 list_add_tail(&b->node, &pci_root_buses);
01144 up_write(&pci_bus_sem);
01145
01146 memset(dev, 0, sizeof(*dev));
01147 dev->parent = parent;
01148 dev->release = pci_release_bus_bridge_dev;
01149 dev_set_name(dev, "pci%04x:%02x", pci_domain_nr(b), bus);
01150 error = device_register(dev);
01151 if (error)
01152 goto dev_reg_err;
01153 b->bridge = get_device(dev);
01154
01155 if (!parent)
01156 set_dev_node(b->bridge, pcibus_to_node(b));
01157
01158 b->dev.class = &pcibus_class;
01159 b->dev.parent = b->bridge;
01160 dev_set_name(&b->dev, "%04x:%02x", pci_domain_nr(b), bus);
01161 error = device_register(&b->dev);
01162 if (error)
01163 goto class_dev_reg_err;
01164 error = device_create_file(&b->dev, &dev_attr_cpuaffinity);
01165 if (error)
01166 goto dev_create_file_err;
01167
01168
01169 pci_create_legacy_files(b);
01170
01171 b->number = b->secondary = bus;
01172 b->resource[0] = &ioport_resource;
01173 b->resource[1] = &iomem_resource;
01174
01175 set_pci_bus_resources_arch_default(b);
01176
01177 return b;
01178
01179 dev_create_file_err:
01180 device_unregister(&b->dev);
01181 class_dev_reg_err:
01182 device_unregister(dev);
01183 dev_reg_err:
01184 down_write(&pci_bus_sem);
01185 list_del(&b->node);
01186 up_write(&pci_bus_sem);
01187 err_out:
01188 kfree(dev);
01189 kfree(b);
01190 return NULL;
01191 }
01192
01193 struct pci_bus * __devinit pci_scan_bus_parented(struct device *parent,
01194 int bus, struct pci_ops *ops, void *sysdata)
01195 {
01196 struct pci_bus *b;
01197
01198 b = pci_create_bus(parent, bus, ops, sysdata);
01199 if (b)
01200 b->subordinate = pci_scan_child_bus(b);
01201 return b;
01202 }
01203 EXPORT_SYMBOL(pci_scan_bus_parented);
01204
01205 #ifdef CONFIG_HOTPLUG
01206 EXPORT_SYMBOL(pci_add_new_bus);
01207 EXPORT_SYMBOL(pci_scan_slot);
01208 EXPORT_SYMBOL(pci_scan_bridge);
01209 EXPORT_SYMBOL_GPL(pci_scan_child_bus);
01210 #endif
01211
01212 static int __init pci_sort_bf_cmp(const struct device *d_a, const struct device *d_b)
01213 {
01214 const struct pci_dev *a = to_pci_dev(d_a);
01215 const struct pci_dev *b = to_pci_dev(d_b);
01216
01217 if (pci_domain_nr(a->bus) < pci_domain_nr(b->bus)) return -1;
01218 else if (pci_domain_nr(a->bus) > pci_domain_nr(b->bus)) return 1;
01219
01220 if (a->bus->number < b->bus->number) return -1;
01221 else if (a->bus->number > b->bus->number) return 1;
01222
01223 if (a->devfn < b->devfn) return -1;
01224 else if (a->devfn > b->devfn) return 1;
01225
01226 return 0;
01227 }
01228
01229 void __init pci_sort_breadthfirst(void)
01230 {
01231 bus_sort_breadthfirst(&pci_bus_type, &pci_sort_bf_cmp);
01232 }