1 /* pci.c: UltraSparc PCI controller support.
3 * Copyright (C) 1997, 1998, 1999 David S. Miller (davem@redhat.com)
4 * Copyright (C) 1998, 1999 Eddie C. Dost (ecd@skynet.be)
5 * Copyright (C) 1999 Jakub Jelinek (jj@ultra.linux.cz)
7 * OF tree based PCI bus probing taken from the PowerPC port
8 * with minor modifications, see there for credits.
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/string.h>
14 #include <linux/sched.h>
15 #include <linux/capability.h>
16 #include <linux/errno.h>
17 #include <linux/pci.h>
18 #include <linux/msi.h>
19 #include <linux/irq.h>
20 #include <linux/init.h>
22 #include <asm/uaccess.h>
23 #include <asm/pgtable.h>
33 /* A "nop" PCI implementation. */
34 asmlinkage int sys_pciconfig_read(unsigned long bus, unsigned long dfn,
35 unsigned long off, unsigned long len,
40 asmlinkage int sys_pciconfig_write(unsigned long bus, unsigned long dfn,
41 unsigned long off, unsigned long len,
48 /* List of all PCI controllers found in the system. */
49 struct pci_pbm_info *pci_pbm_root = NULL;
51 /* Each PBM found gets a unique index. */
54 volatile int pci_poke_in_progress;
55 volatile int pci_poke_cpu = -1;
56 volatile int pci_poke_faulted;
58 static DEFINE_SPINLOCK(pci_poke_lock);
60 void pci_config_read8(u8 *addr, u8 *ret)
65 spin_lock_irqsave(&pci_poke_lock, flags);
66 pci_poke_cpu = smp_processor_id();
67 pci_poke_in_progress = 1;
69 __asm__ __volatile__("membar #Sync\n\t"
70 "lduba [%1] %2, %0\n\t"
73 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
75 pci_poke_in_progress = 0;
77 if (!pci_poke_faulted)
79 spin_unlock_irqrestore(&pci_poke_lock, flags);
82 void pci_config_read16(u16 *addr, u16 *ret)
87 spin_lock_irqsave(&pci_poke_lock, flags);
88 pci_poke_cpu = smp_processor_id();
89 pci_poke_in_progress = 1;
91 __asm__ __volatile__("membar #Sync\n\t"
92 "lduha [%1] %2, %0\n\t"
95 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
97 pci_poke_in_progress = 0;
99 if (!pci_poke_faulted)
101 spin_unlock_irqrestore(&pci_poke_lock, flags);
104 void pci_config_read32(u32 *addr, u32 *ret)
109 spin_lock_irqsave(&pci_poke_lock, flags);
110 pci_poke_cpu = smp_processor_id();
111 pci_poke_in_progress = 1;
112 pci_poke_faulted = 0;
113 __asm__ __volatile__("membar #Sync\n\t"
114 "lduwa [%1] %2, %0\n\t"
117 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
119 pci_poke_in_progress = 0;
121 if (!pci_poke_faulted)
123 spin_unlock_irqrestore(&pci_poke_lock, flags);
126 void pci_config_write8(u8 *addr, u8 val)
130 spin_lock_irqsave(&pci_poke_lock, flags);
131 pci_poke_cpu = smp_processor_id();
132 pci_poke_in_progress = 1;
133 pci_poke_faulted = 0;
134 __asm__ __volatile__("membar #Sync\n\t"
135 "stba %0, [%1] %2\n\t"
138 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
140 pci_poke_in_progress = 0;
142 spin_unlock_irqrestore(&pci_poke_lock, flags);
145 void pci_config_write16(u16 *addr, u16 val)
149 spin_lock_irqsave(&pci_poke_lock, flags);
150 pci_poke_cpu = smp_processor_id();
151 pci_poke_in_progress = 1;
152 pci_poke_faulted = 0;
153 __asm__ __volatile__("membar #Sync\n\t"
154 "stha %0, [%1] %2\n\t"
157 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
159 pci_poke_in_progress = 0;
161 spin_unlock_irqrestore(&pci_poke_lock, flags);
164 void pci_config_write32(u32 *addr, u32 val)
168 spin_lock_irqsave(&pci_poke_lock, flags);
169 pci_poke_cpu = smp_processor_id();
170 pci_poke_in_progress = 1;
171 pci_poke_faulted = 0;
172 __asm__ __volatile__("membar #Sync\n\t"
173 "stwa %0, [%1] %2\n\t"
176 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
178 pci_poke_in_progress = 0;
180 spin_unlock_irqrestore(&pci_poke_lock, flags);
183 /* Probe for all PCI controllers in the system. */
184 extern void sabre_init(struct device_node *, const char *);
185 extern void psycho_init(struct device_node *, const char *);
186 extern void schizo_init(struct device_node *, const char *);
187 extern void schizo_plus_init(struct device_node *, const char *);
188 extern void tomatillo_init(struct device_node *, const char *);
189 extern void sun4v_pci_init(struct device_node *, const char *);
190 extern void fire_pci_init(struct device_node *, const char *);
194 void (*init)(struct device_node *, const char *);
195 } pci_controller_table[] __initdata = {
196 { "SUNW,sabre", sabre_init },
197 { "pci108e,a000", sabre_init },
198 { "pci108e,a001", sabre_init },
199 { "SUNW,psycho", psycho_init },
200 { "pci108e,8000", psycho_init },
201 { "SUNW,schizo", schizo_init },
202 { "pci108e,8001", schizo_init },
203 { "SUNW,schizo+", schizo_plus_init },
204 { "pci108e,8002", schizo_plus_init },
205 { "SUNW,tomatillo", tomatillo_init },
206 { "pci108e,a801", tomatillo_init },
207 { "SUNW,sun4v-pci", sun4v_pci_init },
208 { "pciex108e,80f0", fire_pci_init },
210 #define PCI_NUM_CONTROLLER_TYPES ARRAY_SIZE(pci_controller_table)
212 static int __init pci_controller_init(const char *model_name, int namelen, struct device_node *dp)
216 for (i = 0; i < PCI_NUM_CONTROLLER_TYPES; i++) {
217 if (!strncmp(model_name,
218 pci_controller_table[i].model_name,
220 pci_controller_table[i].init(dp, model_name);
228 static int __init pci_controller_scan(int (*handler)(const char *, int, struct device_node *))
230 struct device_node *dp;
233 for_each_node_by_name(dp, "pci") {
234 struct property *prop;
237 prop = of_find_property(dp, "model", &len);
239 prop = of_find_property(dp, "compatible", &len);
242 const char *model = prop->value;
245 /* Our value may be a multi-valued string in the
246 * case of some compatible properties. For sanity,
247 * only try the first one.
249 while (model[item_len] && len) {
254 if (handler(model, item_len, dp))
262 /* Find each controller in the system, attach and initialize
263 * software state structure for each and link into the
264 * pci_pbm_root. Setup the controller enough such
265 * that bus scanning can be done.
267 static void __init pci_controller_probe(void)
269 printk("PCI: Probing for controllers.\n");
271 pci_controller_scan(pci_controller_init);
274 static int ofpci_verbose;
276 static int __init ofpci_debug(char *str)
280 get_option(&str, &val);
286 __setup("ofpci_debug=", ofpci_debug);
288 static unsigned long pci_parse_of_flags(u32 addr0)
290 unsigned long flags = 0;
292 if (addr0 & 0x02000000) {
293 flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
294 flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
295 flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
296 if (addr0 & 0x40000000)
297 flags |= IORESOURCE_PREFETCH
298 | PCI_BASE_ADDRESS_MEM_PREFETCH;
299 } else if (addr0 & 0x01000000)
300 flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
304 /* The of_device layer has translated all of the assigned-address properties
305 * into physical address resources, we only have to figure out the register
308 static void pci_parse_of_addrs(struct of_device *op,
309 struct device_node *node,
312 struct resource *op_res;
316 addrs = of_get_property(node, "assigned-addresses", &proplen);
320 printk(" parse addresses (%d bytes) @ %p\n",
322 op_res = &op->resource[0];
323 for (; proplen >= 20; proplen -= 20, addrs += 5, op_res++) {
324 struct resource *res;
328 flags = pci_parse_of_flags(addrs[0]);
333 printk(" start: %lx, end: %lx, i: %x\n",
334 op_res->start, op_res->end, i);
336 if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
337 res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
338 } else if (i == dev->rom_base_reg) {
339 res = &dev->resource[PCI_ROM_RESOURCE];
340 flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
342 printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
345 res->start = op_res->start;
346 res->end = op_res->end;
348 res->name = pci_name(dev);
352 struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
353 struct device_node *node,
354 struct pci_bus *bus, int devfn,
357 struct dev_archdata *sd;
362 dev = alloc_pci_dev();
366 sd = &dev->dev.archdata;
367 sd->iommu = pbm->iommu;
369 sd->host_controller = pbm;
370 sd->prom_node = node;
371 sd->op = of_find_device_by_node(node);
372 sd->numa_node = pbm->numa_node;
374 sd = &sd->op->dev.archdata;
375 sd->iommu = pbm->iommu;
377 sd->numa_node = pbm->numa_node;
379 type = of_get_property(node, "device_type", NULL);
384 printk(" create device, devfn: %x, type: %s\n",
389 dev->dev.parent = bus->bridge;
390 dev->dev.bus = &pci_bus_type;
392 dev->multifunction = 0; /* maybe a lie? */
394 if (host_controller) {
395 if (tlb_type != hypervisor) {
396 pci_read_config_word(dev, PCI_VENDOR_ID,
398 pci_read_config_word(dev, PCI_DEVICE_ID,
401 dev->vendor = PCI_VENDOR_ID_SUN;
402 dev->device = 0x80f0;
405 dev->class = PCI_CLASS_BRIDGE_HOST << 8;
406 sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus),
407 0x00, PCI_SLOT(devfn), PCI_FUNC(devfn));
409 dev->vendor = of_getintprop_default(node, "vendor-id", 0xffff);
410 dev->device = of_getintprop_default(node, "device-id", 0xffff);
411 dev->subsystem_vendor =
412 of_getintprop_default(node, "subsystem-vendor-id", 0);
413 dev->subsystem_device =
414 of_getintprop_default(node, "subsystem-id", 0);
416 dev->cfg_size = pci_cfg_space_size(dev);
418 /* We can't actually use the firmware value, we have
419 * to read what is in the register right now. One
420 * reason is that in the case of IDE interfaces the
421 * firmware can sample the value before the the IDE
422 * interface is programmed into native mode.
424 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
425 dev->class = class >> 8;
426 dev->revision = class & 0xff;
428 sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus),
429 dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
432 printk(" class: 0x%x device name: %s\n",
433 dev->class, pci_name(dev));
435 /* I have seen IDE devices which will not respond to
436 * the bmdma simplex check reads if bus mastering is
439 if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE)
442 dev->current_state = 4; /* unknown power state */
443 dev->error_state = pci_channel_io_normal;
445 if (host_controller) {
446 dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
447 dev->rom_base_reg = PCI_ROM_ADDRESS1;
448 dev->irq = PCI_IRQ_NONE;
450 if (!strcmp(type, "pci") || !strcmp(type, "pciex")) {
451 /* a PCI-PCI bridge */
452 dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
453 dev->rom_base_reg = PCI_ROM_ADDRESS1;
454 } else if (!strcmp(type, "cardbus")) {
455 dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
457 dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
458 dev->rom_base_reg = PCI_ROM_ADDRESS;
460 dev->irq = sd->op->irqs[0];
461 if (dev->irq == 0xffffffff)
462 dev->irq = PCI_IRQ_NONE;
465 pci_parse_of_addrs(sd->op, node, dev);
468 printk(" adding to system ...\n");
470 pci_device_add(dev, bus);
475 static void __devinit apb_calc_first_last(u8 map, u32 *first_p, u32 *last_p)
477 u32 idx, first, last;
481 for (idx = 0; idx < 8; idx++) {
482 if ((map & (1 << idx)) != 0) {
494 static void pci_resource_adjust(struct resource *res,
495 struct resource *root)
497 res->start += root->start;
498 res->end += root->start;
501 /* For PCI bus devices which lack a 'ranges' property we interrogate
502 * the config space values to set the resources, just like the generic
503 * Linux PCI probing code does.
505 static void __devinit pci_cfg_fake_ranges(struct pci_dev *dev,
507 struct pci_pbm_info *pbm)
509 struct resource *res;
510 u8 io_base_lo, io_limit_lo;
511 u16 mem_base_lo, mem_limit_lo;
512 unsigned long base, limit;
514 pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
515 pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
516 base = (io_base_lo & PCI_IO_RANGE_MASK) << 8;
517 limit = (io_limit_lo & PCI_IO_RANGE_MASK) << 8;
519 if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
520 u16 io_base_hi, io_limit_hi;
522 pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
523 pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
524 base |= (io_base_hi << 16);
525 limit |= (io_limit_hi << 16);
528 res = bus->resource[0];
530 res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
534 res->end = limit + 0xfff;
535 pci_resource_adjust(res, &pbm->io_space);
538 pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
539 pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
540 base = (mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
541 limit = (mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
543 res = bus->resource[1];
545 res->flags = ((mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) |
548 res->end = limit + 0xfffff;
549 pci_resource_adjust(res, &pbm->mem_space);
552 pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
553 pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
554 base = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
555 limit = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
557 if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
558 u32 mem_base_hi, mem_limit_hi;
560 pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
561 pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
564 * Some bridges set the base > limit by default, and some
565 * (broken) BIOSes do not initialize them. If we find
566 * this, just assume they are not being used.
568 if (mem_base_hi <= mem_limit_hi) {
569 base |= ((long) mem_base_hi) << 32;
570 limit |= ((long) mem_limit_hi) << 32;
574 res = bus->resource[2];
576 res->flags = ((mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) |
577 IORESOURCE_MEM | IORESOURCE_PREFETCH);
579 res->end = limit + 0xfffff;
580 pci_resource_adjust(res, &pbm->mem_space);
584 /* Cook up fake bus resources for SUNW,simba PCI bridges which lack
585 * a proper 'ranges' property.
587 static void __devinit apb_fake_ranges(struct pci_dev *dev,
589 struct pci_pbm_info *pbm)
591 struct resource *res;
595 pci_read_config_byte(dev, APB_IO_ADDRESS_MAP, &map);
596 apb_calc_first_last(map, &first, &last);
597 res = bus->resource[0];
598 res->start = (first << 21);
599 res->end = (last << 21) + ((1 << 21) - 1);
600 res->flags = IORESOURCE_IO;
601 pci_resource_adjust(res, &pbm->io_space);
603 pci_read_config_byte(dev, APB_MEM_ADDRESS_MAP, &map);
604 apb_calc_first_last(map, &first, &last);
605 res = bus->resource[1];
606 res->start = (first << 21);
607 res->end = (last << 21) + ((1 << 21) - 1);
608 res->flags = IORESOURCE_MEM;
609 pci_resource_adjust(res, &pbm->mem_space);
612 static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
613 struct device_node *node,
614 struct pci_bus *bus);
616 #define GET_64BIT(prop, i) ((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1])
618 static void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm,
619 struct device_node *node,
623 const u32 *busrange, *ranges;
625 struct resource *res;
630 printk("of_scan_pci_bridge(%s)\n", node->full_name);
632 /* parse bus-range property */
633 busrange = of_get_property(node, "bus-range", &len);
634 if (busrange == NULL || len != 8) {
635 printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n",
639 ranges = of_get_property(node, "ranges", &len);
641 if (ranges == NULL) {
642 const char *model = of_get_property(node, "model", NULL);
643 if (model && !strcmp(model, "SUNW,simba"))
647 bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
649 printk(KERN_ERR "Failed to create pci bus for %s\n",
654 bus->primary = dev->bus->number;
655 bus->subordinate = busrange[1];
658 /* parse ranges property, or cook one up by hand for Simba */
659 /* PCI #address-cells == 3 and #size-cells == 2 always */
660 res = &dev->resource[PCI_BRIDGE_RESOURCES];
661 for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
663 bus->resource[i] = res;
667 apb_fake_ranges(dev, bus, pbm);
669 } else if (ranges == NULL) {
670 pci_cfg_fake_ranges(dev, bus, pbm);
674 for (; len >= 32; len -= 32, ranges += 8) {
675 struct resource *root;
677 flags = pci_parse_of_flags(ranges[0]);
678 size = GET_64BIT(ranges, 6);
679 if (flags == 0 || size == 0)
681 if (flags & IORESOURCE_IO) {
682 res = bus->resource[0];
684 printk(KERN_ERR "PCI: ignoring extra I/O range"
685 " for bridge %s\n", node->full_name);
688 root = &pbm->io_space;
690 if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
691 printk(KERN_ERR "PCI: too many memory ranges"
692 " for bridge %s\n", node->full_name);
695 res = bus->resource[i];
697 root = &pbm->mem_space;
700 res->start = GET_64BIT(ranges, 1);
701 res->end = res->start + size - 1;
704 /* Another way to implement this would be to add an of_device
705 * layer routine that can calculate a resource for a given
706 * range property value in a PCI device.
708 pci_resource_adjust(res, root);
711 sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
714 printk(" bus name: %s\n", bus->name);
716 pci_of_scan_bus(pbm, node, bus);
719 static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
720 struct device_node *node,
723 struct device_node *child;
725 int reglen, devfn, prev_devfn;
729 printk("PCI: scan_bus[%s] bus no %d\n",
730 node->full_name, bus->number);
734 while ((child = of_get_next_child(node, child)) != NULL) {
736 printk(" * %s\n", child->full_name);
737 reg = of_get_property(child, "reg", ®len);
738 if (reg == NULL || reglen < 20)
741 devfn = (reg[0] >> 8) & 0xff;
743 /* This is a workaround for some device trees
744 * which list PCI devices twice. On the V100
745 * for example, device number 3 is listed twice.
746 * Once as "pm" and once again as "lomp".
748 if (devfn == prev_devfn)
752 /* create a new pci_dev for this device */
753 dev = of_create_pci_dev(pbm, child, bus, devfn, 0);
757 printk("PCI: dev header type: %x\n",
760 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
761 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
762 of_scan_pci_bridge(pbm, child, dev);
767 show_pciobppath_attr(struct device * dev, struct device_attribute * attr, char * buf)
769 struct pci_dev *pdev;
770 struct device_node *dp;
772 pdev = to_pci_dev(dev);
773 dp = pdev->dev.archdata.prom_node;
775 return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name);
778 static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_pciobppath_attr, NULL);
780 static void __devinit pci_bus_register_of_sysfs(struct pci_bus *bus)
783 struct pci_bus *child_bus;
786 list_for_each_entry(dev, &bus->devices, bus_list) {
787 /* we don't really care if we can create this file or
788 * not, but we need to assign the result of the call
789 * or the world will fall under alien invasion and
790 * everybody will be frozen on a spaceship ready to be
791 * eaten on alpha centauri by some green and jelly
794 err = sysfs_create_file(&dev->dev.kobj, &dev_attr_obppath.attr);
796 list_for_each_entry(child_bus, &bus->children, node)
797 pci_bus_register_of_sysfs(child_bus);
800 int pci_host_bridge_read_pci_cfg(struct pci_bus *bus_dev,
805 static u8 fake_pci_config[] = {
806 0x8e, 0x10, /* Vendor: 0x108e (Sun) */
807 0xf0, 0x80, /* Device: 0x80f0 (Fire) */
808 0x46, 0x01, /* Command: 0x0146 (SERR, PARITY, MASTER, MEM) */
809 0xa0, 0x22, /* Status: 0x02a0 (DEVSEL_MED, FB2B, 66MHZ) */
810 0x00, 0x00, 0x00, 0x06, /* Class: 0x06000000 host bridge */
811 0x00, /* Cacheline: 0x00 */
812 0x40, /* Latency: 0x40 */
813 0x00, /* Header-Type: 0x00 normal */
817 if (where >= 0 && where < sizeof(fake_pci_config) &&
818 (where + size) >= 0 &&
819 (where + size) < sizeof(fake_pci_config) &&
820 size <= sizeof(u32)) {
823 *value |= fake_pci_config[where + size];
827 return PCIBIOS_SUCCESSFUL;
830 int pci_host_bridge_write_pci_cfg(struct pci_bus *bus_dev,
835 return PCIBIOS_SUCCESSFUL;
838 struct pci_bus * __devinit pci_scan_one_pbm(struct pci_pbm_info *pbm)
840 struct device_node *node = pbm->prom_node;
841 struct pci_dev *host_pdev;
844 printk("PCI: Scanning PBM %s\n", node->full_name);
846 /* XXX parent device? XXX */
847 bus = pci_create_bus(NULL, pbm->pci_first_busno, pbm->pci_ops, pbm);
849 printk(KERN_ERR "Failed to create bus for %s\n",
853 bus->secondary = pbm->pci_first_busno;
854 bus->subordinate = pbm->pci_last_busno;
856 bus->resource[0] = &pbm->io_space;
857 bus->resource[1] = &pbm->mem_space;
859 /* Create the dummy host bridge and link it in. */
860 host_pdev = of_create_pci_dev(pbm, node, bus, 0x00, 1);
861 bus->self = host_pdev;
863 pci_of_scan_bus(pbm, node, bus);
864 pci_bus_add_devices(bus);
865 pci_bus_register_of_sysfs(bus);
870 static void __init pci_scan_each_controller_bus(void)
872 struct pci_pbm_info *pbm;
874 for (pbm = pci_pbm_root; pbm; pbm = pbm->next)
878 extern void power_init(void);
880 static int __init pcibios_init(void)
882 pci_controller_probe();
883 if (pci_pbm_root == NULL)
886 pci_scan_each_controller_bus();
895 subsys_initcall(pcibios_init);
897 void __devinit pcibios_fixup_bus(struct pci_bus *pbus)
899 struct pci_pbm_info *pbm = pbus->sysdata;
901 /* Generic PCI bus probing sets these to point at
902 * &io{port,mem}_resouce which is wrong for us.
904 pbus->resource[0] = &pbm->io_space;
905 pbus->resource[1] = &pbm->mem_space;
908 struct resource *pcibios_select_root(struct pci_dev *pdev, struct resource *r)
910 struct pci_pbm_info *pbm = pdev->bus->sysdata;
911 struct resource *root = NULL;
913 if (r->flags & IORESOURCE_IO)
914 root = &pbm->io_space;
915 if (r->flags & IORESOURCE_MEM)
916 root = &pbm->mem_space;
921 void pcibios_update_irq(struct pci_dev *pdev, int irq)
925 void pcibios_align_resource(void *data, struct resource *res,
926 resource_size_t size, resource_size_t align)
930 int pcibios_enable_device(struct pci_dev *dev, int mask)
935 pci_read_config_word(dev, PCI_COMMAND, &cmd);
938 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
939 struct resource *res = &dev->resource[i];
941 /* Only set up the requested stuff */
942 if (!(mask & (1<<i)))
945 if (res->flags & IORESOURCE_IO)
946 cmd |= PCI_COMMAND_IO;
947 if (res->flags & IORESOURCE_MEM)
948 cmd |= PCI_COMMAND_MEMORY;
952 printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
954 /* Enable the appropriate bits in the PCI command register. */
955 pci_write_config_word(dev, PCI_COMMAND, cmd);
960 void pcibios_resource_to_bus(struct pci_dev *pdev, struct pci_bus_region *region,
961 struct resource *res)
963 struct pci_pbm_info *pbm = pdev->bus->sysdata;
964 struct resource zero_res, *root;
968 zero_res.flags = res->flags;
970 if (res->flags & IORESOURCE_IO)
971 root = &pbm->io_space;
973 root = &pbm->mem_space;
975 pci_resource_adjust(&zero_res, root);
977 region->start = res->start - zero_res.start;
978 region->end = res->end - zero_res.start;
980 EXPORT_SYMBOL(pcibios_resource_to_bus);
982 void pcibios_bus_to_resource(struct pci_dev *pdev, struct resource *res,
983 struct pci_bus_region *region)
985 struct pci_pbm_info *pbm = pdev->bus->sysdata;
986 struct resource *root;
988 res->start = region->start;
989 res->end = region->end;
991 if (res->flags & IORESOURCE_IO)
992 root = &pbm->io_space;
994 root = &pbm->mem_space;
996 pci_resource_adjust(res, root);
998 EXPORT_SYMBOL(pcibios_bus_to_resource);
1000 char * __devinit pcibios_setup(char *str)
1005 /* Platform support for /proc/bus/pci/X/Y mmap()s. */
1007 /* If the user uses a host-bridge as the PCI device, he may use
1008 * this to perform a raw mmap() of the I/O or MEM space behind
1011 * This can be useful for execution of x86 PCI bios initialization code
1012 * on a PCI card, like the xfree86 int10 stuff does.
1014 static int __pci_mmap_make_offset_bus(struct pci_dev *pdev, struct vm_area_struct *vma,
1015 enum pci_mmap_state mmap_state)
1017 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
1018 unsigned long space_size, user_offset, user_size;
1020 if (mmap_state == pci_mmap_io) {
1021 space_size = (pbm->io_space.end -
1022 pbm->io_space.start) + 1;
1024 space_size = (pbm->mem_space.end -
1025 pbm->mem_space.start) + 1;
1028 /* Make sure the request is in range. */
1029 user_offset = vma->vm_pgoff << PAGE_SHIFT;
1030 user_size = vma->vm_end - vma->vm_start;
1032 if (user_offset >= space_size ||
1033 (user_offset + user_size) > space_size)
1036 if (mmap_state == pci_mmap_io) {
1037 vma->vm_pgoff = (pbm->io_space.start +
1038 user_offset) >> PAGE_SHIFT;
1040 vma->vm_pgoff = (pbm->mem_space.start +
1041 user_offset) >> PAGE_SHIFT;
1047 /* Adjust vm_pgoff of VMA such that it is the physical page offset
1048 * corresponding to the 32-bit pci bus offset for DEV requested by the user.
1050 * Basically, the user finds the base address for his device which he wishes
1051 * to mmap. They read the 32-bit value from the config space base register,
1052 * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
1053 * offset parameter of mmap on /proc/bus/pci/XXX for that device.
1055 * Returns negative error code on failure, zero on success.
1057 static int __pci_mmap_make_offset(struct pci_dev *pdev,
1058 struct vm_area_struct *vma,
1059 enum pci_mmap_state mmap_state)
1061 unsigned long user_paddr, user_size;
1064 /* First compute the physical address in vma->vm_pgoff,
1065 * making sure the user offset is within range in the
1066 * appropriate PCI space.
1068 err = __pci_mmap_make_offset_bus(pdev, vma, mmap_state);
1072 /* If this is a mapping on a host bridge, any address
1075 if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
1078 /* Otherwise make sure it's in the range for one of the
1079 * device's resources.
1081 user_paddr = vma->vm_pgoff << PAGE_SHIFT;
1082 user_size = vma->vm_end - vma->vm_start;
1084 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
1085 struct resource *rp = &pdev->resource[i];
1092 if (i == PCI_ROM_RESOURCE) {
1093 if (mmap_state != pci_mmap_mem)
1096 if ((mmap_state == pci_mmap_io &&
1097 (rp->flags & IORESOURCE_IO) == 0) ||
1098 (mmap_state == pci_mmap_mem &&
1099 (rp->flags & IORESOURCE_MEM) == 0))
1103 if ((rp->start <= user_paddr) &&
1104 (user_paddr + user_size) <= (rp->end + 1UL))
1108 if (i > PCI_ROM_RESOURCE)
1114 /* Set vm_flags of VMA, as appropriate for this architecture, for a pci device
1117 static void __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
1118 enum pci_mmap_state mmap_state)
1120 vma->vm_flags |= (VM_IO | VM_RESERVED);
1123 /* Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
1126 static void __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
1127 enum pci_mmap_state mmap_state)
1129 /* Our io_remap_pfn_range takes care of this, do nothing. */
1132 /* Perform the actual remap of the pages for a PCI device mapping, as appropriate
1133 * for this architecture. The region in the process to map is described by vm_start
1134 * and vm_end members of VMA, the base physical address is found in vm_pgoff.
1135 * The pci device structure is provided so that architectures may make mapping
1136 * decisions on a per-device or per-bus basis.
1138 * Returns a negative error code on failure, zero on success.
1140 int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
1141 enum pci_mmap_state mmap_state,
1146 ret = __pci_mmap_make_offset(dev, vma, mmap_state);
1150 __pci_mmap_set_flags(dev, vma, mmap_state);
1151 __pci_mmap_set_pgprot(dev, vma, mmap_state);
1153 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1154 ret = io_remap_pfn_range(vma, vma->vm_start,
1156 vma->vm_end - vma->vm_start,
1165 int pcibus_to_node(struct pci_bus *pbus)
1167 struct pci_pbm_info *pbm = pbus->sysdata;
1169 return pbm->numa_node;
1171 EXPORT_SYMBOL(pcibus_to_node);
1174 /* Return the domain nuber for this pci bus */
1176 int pci_domain_nr(struct pci_bus *pbus)
1178 struct pci_pbm_info *pbm = pbus->sysdata;
1181 if (pbm == NULL || pbm->parent == NULL) {
1189 EXPORT_SYMBOL(pci_domain_nr);
1191 #ifdef CONFIG_PCI_MSI
1192 int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
1194 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
1197 if (!pbm->setup_msi_irq)
1200 return pbm->setup_msi_irq(&virt_irq, pdev, desc);
1203 void arch_teardown_msi_irq(unsigned int virt_irq)
1205 struct msi_desc *entry = get_irq_msi(virt_irq);
1206 struct pci_dev *pdev = entry->dev;
1207 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
1209 if (!pbm->teardown_msi_irq)
1212 return pbm->teardown_msi_irq(virt_irq, pdev);
1214 #endif /* !(CONFIG_PCI_MSI) */
1216 struct device_node *pci_device_to_OF_node(struct pci_dev *pdev)
1218 return pdev->dev.archdata.prom_node;
1220 EXPORT_SYMBOL(pci_device_to_OF_node);
1222 static void ali_sound_dma_hack(struct pci_dev *pdev, int set_bit)
1224 struct pci_dev *ali_isa_bridge;
1227 /* ALI sound chips generate 31-bits of DMA, a special register
1228 * determines what bit 31 is emitted as.
1230 ali_isa_bridge = pci_get_device(PCI_VENDOR_ID_AL,
1231 PCI_DEVICE_ID_AL_M1533,
1234 pci_read_config_byte(ali_isa_bridge, 0x7e, &val);
1239 pci_write_config_byte(ali_isa_bridge, 0x7e, val);
1240 pci_dev_put(ali_isa_bridge);
1243 int pci_dma_supported(struct pci_dev *pdev, u64 device_mask)
1248 dma_addr_mask = 0xffffffff;
1250 struct iommu *iommu = pdev->dev.archdata.iommu;
1252 dma_addr_mask = iommu->dma_addr_mask;
1254 if (pdev->vendor == PCI_VENDOR_ID_AL &&
1255 pdev->device == PCI_DEVICE_ID_AL_M5451 &&
1256 device_mask == 0x7fffffff) {
1257 ali_sound_dma_hack(pdev,
1258 (dma_addr_mask & 0x80000000) != 0);
1263 if (device_mask >= (1UL << 32UL))
1266 return (device_mask & dma_addr_mask) == dma_addr_mask;
1269 void pci_resource_to_user(const struct pci_dev *pdev, int bar,
1270 const struct resource *rp, resource_size_t *start,
1271 resource_size_t *end)
1273 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
1274 unsigned long offset;
1276 if (rp->flags & IORESOURCE_IO)
1277 offset = pbm->io_space.start;
1279 offset = pbm->mem_space.start;
1281 *start = rp->start - offset;
1282 *end = rp->end - offset;
1285 #endif /* !(CONFIG_PCI) */