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/smp_lock.h>
18 #include <linux/msi.h>
19 #include <linux/irq.h>
20 #include <linux/init.h>
22 #include <asm/uaccess.h>
24 #include <asm/pgtable.h>
33 unsigned long pci_memspace_mask = 0xffffffffUL;
36 /* A "nop" PCI implementation. */
37 asmlinkage int sys_pciconfig_read(unsigned long bus, unsigned long dfn,
38 unsigned long off, unsigned long len,
43 asmlinkage int sys_pciconfig_write(unsigned long bus, unsigned long dfn,
44 unsigned long off, unsigned long len,
51 /* List of all PCI controllers found in the system. */
52 struct pci_controller_info *pci_controller_root = NULL;
54 /* Each PCI controller found gets a unique index. */
55 int pci_num_controllers = 0;
57 volatile int pci_poke_in_progress;
58 volatile int pci_poke_cpu = -1;
59 volatile int pci_poke_faulted;
61 static DEFINE_SPINLOCK(pci_poke_lock);
63 void pci_config_read8(u8 *addr, u8 *ret)
68 spin_lock_irqsave(&pci_poke_lock, flags);
69 pci_poke_cpu = smp_processor_id();
70 pci_poke_in_progress = 1;
72 __asm__ __volatile__("membar #Sync\n\t"
73 "lduba [%1] %2, %0\n\t"
76 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
78 pci_poke_in_progress = 0;
80 if (!pci_poke_faulted)
82 spin_unlock_irqrestore(&pci_poke_lock, flags);
85 void pci_config_read16(u16 *addr, u16 *ret)
90 spin_lock_irqsave(&pci_poke_lock, flags);
91 pci_poke_cpu = smp_processor_id();
92 pci_poke_in_progress = 1;
94 __asm__ __volatile__("membar #Sync\n\t"
95 "lduha [%1] %2, %0\n\t"
98 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
100 pci_poke_in_progress = 0;
102 if (!pci_poke_faulted)
104 spin_unlock_irqrestore(&pci_poke_lock, flags);
107 void pci_config_read32(u32 *addr, u32 *ret)
112 spin_lock_irqsave(&pci_poke_lock, flags);
113 pci_poke_cpu = smp_processor_id();
114 pci_poke_in_progress = 1;
115 pci_poke_faulted = 0;
116 __asm__ __volatile__("membar #Sync\n\t"
117 "lduwa [%1] %2, %0\n\t"
120 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
122 pci_poke_in_progress = 0;
124 if (!pci_poke_faulted)
126 spin_unlock_irqrestore(&pci_poke_lock, flags);
129 void pci_config_write8(u8 *addr, u8 val)
133 spin_lock_irqsave(&pci_poke_lock, flags);
134 pci_poke_cpu = smp_processor_id();
135 pci_poke_in_progress = 1;
136 pci_poke_faulted = 0;
137 __asm__ __volatile__("membar #Sync\n\t"
138 "stba %0, [%1] %2\n\t"
141 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
143 pci_poke_in_progress = 0;
145 spin_unlock_irqrestore(&pci_poke_lock, flags);
148 void pci_config_write16(u16 *addr, u16 val)
152 spin_lock_irqsave(&pci_poke_lock, flags);
153 pci_poke_cpu = smp_processor_id();
154 pci_poke_in_progress = 1;
155 pci_poke_faulted = 0;
156 __asm__ __volatile__("membar #Sync\n\t"
157 "stha %0, [%1] %2\n\t"
160 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
162 pci_poke_in_progress = 0;
164 spin_unlock_irqrestore(&pci_poke_lock, flags);
167 void pci_config_write32(u32 *addr, u32 val)
171 spin_lock_irqsave(&pci_poke_lock, flags);
172 pci_poke_cpu = smp_processor_id();
173 pci_poke_in_progress = 1;
174 pci_poke_faulted = 0;
175 __asm__ __volatile__("membar #Sync\n\t"
176 "stwa %0, [%1] %2\n\t"
179 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
181 pci_poke_in_progress = 0;
183 spin_unlock_irqrestore(&pci_poke_lock, flags);
186 /* Probe for all PCI controllers in the system. */
187 extern void sabre_init(struct device_node *, const char *);
188 extern void psycho_init(struct device_node *, const char *);
189 extern void schizo_init(struct device_node *, const char *);
190 extern void schizo_plus_init(struct device_node *, const char *);
191 extern void tomatillo_init(struct device_node *, const char *);
192 extern void sun4v_pci_init(struct device_node *, const char *);
193 extern void fire_pci_init(struct device_node *, const char *);
197 void (*init)(struct device_node *, const char *);
198 } pci_controller_table[] __initdata = {
199 { "SUNW,sabre", sabre_init },
200 { "pci108e,a000", sabre_init },
201 { "pci108e,a001", sabre_init },
202 { "SUNW,psycho", psycho_init },
203 { "pci108e,8000", psycho_init },
204 { "SUNW,schizo", schizo_init },
205 { "pci108e,8001", schizo_init },
206 { "SUNW,schizo+", schizo_plus_init },
207 { "pci108e,8002", schizo_plus_init },
208 { "SUNW,tomatillo", tomatillo_init },
209 { "pci108e,a801", tomatillo_init },
210 { "SUNW,sun4v-pci", sun4v_pci_init },
211 { "pciex108e,80f0", fire_pci_init },
213 #define PCI_NUM_CONTROLLER_TYPES (sizeof(pci_controller_table) / \
214 sizeof(pci_controller_table[0]))
216 static int __init pci_controller_init(const char *model_name, int namelen, struct device_node *dp)
220 for (i = 0; i < PCI_NUM_CONTROLLER_TYPES; i++) {
221 if (!strncmp(model_name,
222 pci_controller_table[i].model_name,
224 pci_controller_table[i].init(dp, model_name);
232 static int __init pci_is_controller(const char *model_name, int namelen, struct device_node *dp)
236 for (i = 0; i < PCI_NUM_CONTROLLER_TYPES; i++) {
237 if (!strncmp(model_name,
238 pci_controller_table[i].model_name,
246 static int __init pci_controller_scan(int (*handler)(const char *, int, struct device_node *))
248 struct device_node *dp;
251 for_each_node_by_name(dp, "pci") {
252 struct property *prop;
255 prop = of_find_property(dp, "model", &len);
257 prop = of_find_property(dp, "compatible", &len);
260 const char *model = prop->value;
263 /* Our value may be a multi-valued string in the
264 * case of some compatible properties. For sanity,
265 * only try the first one.
267 while (model[item_len] && len) {
272 if (handler(model, item_len, dp))
281 /* Is there some PCI controller in the system? */
282 int __init pcic_present(void)
284 return pci_controller_scan(pci_is_controller);
287 const struct pci_iommu_ops *pci_iommu_ops;
288 EXPORT_SYMBOL(pci_iommu_ops);
290 extern const struct pci_iommu_ops pci_sun4u_iommu_ops,
293 /* Find each controller in the system, attach and initialize
294 * software state structure for each and link into the
295 * pci_controller_root. Setup the controller enough such
296 * that bus scanning can be done.
298 static void __init pci_controller_probe(void)
300 if (tlb_type == hypervisor)
301 pci_iommu_ops = &pci_sun4v_iommu_ops;
303 pci_iommu_ops = &pci_sun4u_iommu_ops;
305 printk("PCI: Probing for controllers.\n");
307 pci_controller_scan(pci_controller_init);
310 static unsigned long pci_parse_of_flags(u32 addr0)
312 unsigned long flags = 0;
314 if (addr0 & 0x02000000) {
315 flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
316 flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
317 flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
318 if (addr0 & 0x40000000)
319 flags |= IORESOURCE_PREFETCH
320 | PCI_BASE_ADDRESS_MEM_PREFETCH;
321 } else if (addr0 & 0x01000000)
322 flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
326 /* The of_device layer has translated all of the assigned-address properties
327 * into physical address resources, we only have to figure out the register
330 static void pci_parse_of_addrs(struct of_device *op,
331 struct device_node *node,
334 struct resource *op_res;
338 addrs = of_get_property(node, "assigned-addresses", &proplen);
341 printk(" parse addresses (%d bytes) @ %p\n", proplen, addrs);
342 op_res = &op->resource[0];
343 for (; proplen >= 20; proplen -= 20, addrs += 5, op_res++) {
344 struct resource *res;
348 flags = pci_parse_of_flags(addrs[0]);
352 printk(" start: %lx, end: %lx, i: %x\n",
353 op_res->start, op_res->end, i);
355 if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
356 res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
357 } else if (i == dev->rom_base_reg) {
358 res = &dev->resource[PCI_ROM_RESOURCE];
359 flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
361 printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
364 res->start = op_res->start;
365 res->end = op_res->end;
367 res->name = pci_name(dev);
371 struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
372 struct device_node *node,
373 struct pci_bus *bus, int devfn,
376 struct dev_archdata *sd;
381 dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
385 sd = &dev->dev.archdata;
386 sd->iommu = pbm->iommu;
388 sd->host_controller = pbm;
389 sd->prom_node = node;
390 sd->op = of_find_device_by_node(node);
391 sd->msi_num = 0xffffffff;
393 type = of_get_property(node, "device_type", NULL);
397 printk(" create device, devfn: %x, type: %s hostcontroller(%d)\n",
398 devfn, type, host_controller);
402 dev->dev.parent = bus->bridge;
403 dev->dev.bus = &pci_bus_type;
405 dev->multifunction = 0; /* maybe a lie? */
407 if (host_controller) {
408 dev->vendor = 0x108e;
409 dev->device = 0x8000;
410 dev->subsystem_vendor = 0x0000;
411 dev->subsystem_device = 0x0000;
413 dev->class = PCI_CLASS_BRIDGE_HOST << 8;
414 sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus),
415 0x00, PCI_SLOT(devfn), PCI_FUNC(devfn));
417 dev->vendor = of_getintprop_default(node, "vendor-id", 0xffff);
418 dev->device = of_getintprop_default(node, "device-id", 0xffff);
419 dev->subsystem_vendor =
420 of_getintprop_default(node, "subsystem-vendor-id", 0);
421 dev->subsystem_device =
422 of_getintprop_default(node, "subsystem-id", 0);
424 dev->cfg_size = pci_cfg_space_size(dev);
426 /* We can't actually use the firmware value, we have
427 * to read what is in the register right now. One
428 * reason is that in the case of IDE interfaces the
429 * firmware can sample the value before the the IDE
430 * interface is programmed into native mode.
432 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
433 dev->class = class >> 8;
435 sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus),
436 dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
438 printk(" class: 0x%x device name: %s\n",
439 dev->class, pci_name(dev));
441 /* I have seen IDE devices which will not respond to
442 * the bmdma simplex check reads if bus mastering is
445 if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE)
448 dev->current_state = 4; /* unknown power state */
449 dev->error_state = pci_channel_io_normal;
451 if (host_controller) {
452 dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
453 dev->rom_base_reg = PCI_ROM_ADDRESS1;
454 dev->irq = PCI_IRQ_NONE;
456 if (!strcmp(type, "pci") || !strcmp(type, "pciex")) {
457 /* a PCI-PCI bridge */
458 dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
459 dev->rom_base_reg = PCI_ROM_ADDRESS1;
460 } else if (!strcmp(type, "cardbus")) {
461 dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
463 dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
464 dev->rom_base_reg = PCI_ROM_ADDRESS;
466 dev->irq = sd->op->irqs[0];
467 if (dev->irq == 0xffffffff)
468 dev->irq = PCI_IRQ_NONE;
471 pci_parse_of_addrs(sd->op, node, dev);
473 printk(" adding to system ...\n");
475 pci_device_add(dev, bus);
480 static void __devinit apb_calc_first_last(u8 map, u32 *first_p, u32 *last_p)
482 u32 idx, first, last;
486 for (idx = 0; idx < 8; idx++) {
487 if ((map & (1 << idx)) != 0) {
499 static void __init pci_resource_adjust(struct resource *res,
500 struct resource *root)
502 res->start += root->start;
503 res->end += root->start;
506 /* Cook up fake bus resources for SUNW,simba PCI bridges which lack
507 * a proper 'ranges' property.
509 static void __devinit apb_fake_ranges(struct pci_dev *dev,
511 struct pci_pbm_info *pbm)
513 struct resource *res;
517 pci_read_config_byte(dev, APB_IO_ADDRESS_MAP, &map);
518 apb_calc_first_last(map, &first, &last);
519 res = bus->resource[0];
520 res->start = (first << 21);
521 res->end = (last << 21) + ((1 << 21) - 1);
522 res->flags = IORESOURCE_IO;
523 pci_resource_adjust(res, &pbm->io_space);
525 pci_read_config_byte(dev, APB_MEM_ADDRESS_MAP, &map);
526 apb_calc_first_last(map, &first, &last);
527 res = bus->resource[1];
528 res->start = (first << 21);
529 res->end = (last << 21) + ((1 << 21) - 1);
530 res->flags = IORESOURCE_MEM;
531 pci_resource_adjust(res, &pbm->mem_space);
534 static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
535 struct device_node *node,
536 struct pci_bus *bus);
538 #define GET_64BIT(prop, i) ((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1])
540 static void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm,
541 struct device_node *node,
545 const u32 *busrange, *ranges;
547 struct resource *res;
551 printk("of_scan_pci_bridge(%s)\n", node->full_name);
553 /* parse bus-range property */
554 busrange = of_get_property(node, "bus-range", &len);
555 if (busrange == NULL || len != 8) {
556 printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n",
560 ranges = of_get_property(node, "ranges", &len);
562 if (ranges == NULL) {
563 const char *model = of_get_property(node, "model", NULL);
564 if (model && !strcmp(model, "SUNW,simba")) {
567 printk(KERN_DEBUG "Can't get ranges for PCI-PCI bridge %s\n",
573 bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
575 printk(KERN_ERR "Failed to create pci bus for %s\n",
580 bus->primary = dev->bus->number;
581 bus->subordinate = busrange[1];
584 /* parse ranges property, or cook one up by hand for Simba */
585 /* PCI #address-cells == 3 and #size-cells == 2 always */
586 res = &dev->resource[PCI_BRIDGE_RESOURCES];
587 for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
589 bus->resource[i] = res;
593 apb_fake_ranges(dev, bus, pbm);
597 for (; len >= 32; len -= 32, ranges += 8) {
598 struct resource *root;
600 flags = pci_parse_of_flags(ranges[0]);
601 size = GET_64BIT(ranges, 6);
602 if (flags == 0 || size == 0)
604 if (flags & IORESOURCE_IO) {
605 res = bus->resource[0];
607 printk(KERN_ERR "PCI: ignoring extra I/O range"
608 " for bridge %s\n", node->full_name);
611 root = &pbm->io_space;
613 if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
614 printk(KERN_ERR "PCI: too many memory ranges"
615 " for bridge %s\n", node->full_name);
618 res = bus->resource[i];
620 root = &pbm->mem_space;
623 res->start = GET_64BIT(ranges, 1);
624 res->end = res->start + size - 1;
627 /* Another way to implement this would be to add an of_device
628 * layer routine that can calculate a resource for a given
629 * range property value in a PCI device.
631 pci_resource_adjust(res, root);
634 sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
636 printk(" bus name: %s\n", bus->name);
638 pci_of_scan_bus(pbm, node, bus);
641 static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
642 struct device_node *node,
645 struct device_node *child;
650 printk("PCI: scan_bus[%s] bus no %d\n",
651 node->full_name, bus->number);
654 while ((child = of_get_next_child(node, child)) != NULL) {
655 printk(" * %s\n", child->full_name);
656 reg = of_get_property(child, "reg", ®len);
657 if (reg == NULL || reglen < 20)
659 devfn = (reg[0] >> 8) & 0xff;
661 /* create a new pci_dev for this device */
662 dev = of_create_pci_dev(pbm, child, bus, devfn, 0);
665 printk("PCI: dev header type: %x\n", dev->hdr_type);
667 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
668 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
669 of_scan_pci_bridge(pbm, child, dev);
674 show_pciobppath_attr(struct device * dev, struct device_attribute * attr, char * buf)
676 struct pci_dev *pdev;
677 struct device_node *dp;
679 pdev = to_pci_dev(dev);
680 dp = pdev->dev.archdata.prom_node;
682 return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name);
685 static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_pciobppath_attr, NULL);
687 static void __devinit pci_bus_register_of_sysfs(struct pci_bus *bus)
690 struct pci_bus *child_bus;
693 list_for_each_entry(dev, &bus->devices, bus_list) {
694 /* we don't really care if we can create this file or
695 * not, but we need to assign the result of the call
696 * or the world will fall under alien invasion and
697 * everybody will be frozen on a spaceship ready to be
698 * eaten on alpha centauri by some green and jelly
701 err = sysfs_create_file(&dev->dev.kobj, &dev_attr_obppath.attr);
703 list_for_each_entry(child_bus, &bus->children, node)
704 pci_bus_register_of_sysfs(child_bus);
707 int pci_host_bridge_read_pci_cfg(struct pci_bus *bus_dev,
712 static u8 fake_pci_config[] = {
713 0x8e, 0x10, /* Vendor: 0x108e (Sun) */
714 0x00, 0x80, /* Device: 0x8000 (PBM) */
715 0x46, 0x01, /* Command: 0x0146 (SERR, PARITY, MASTER, MEM) */
716 0xa0, 0x22, /* Status: 0x02a0 (DEVSEL_MED, FB2B, 66MHZ) */
717 0x00, 0x00, 0x00, 0x06, /* Class: 0x06000000 host bridge */
718 0x00, /* Cacheline: 0x00 */
719 0x40, /* Latency: 0x40 */
720 0x00, /* Header-Type: 0x00 normal */
724 if (where >= 0 && where < sizeof(fake_pci_config) &&
725 (where + size) >= 0 &&
726 (where + size) < sizeof(fake_pci_config) &&
727 size <= sizeof(u32)) {
730 *value |= fake_pci_config[where + size];
734 return PCIBIOS_SUCCESSFUL;
737 int pci_host_bridge_write_pci_cfg(struct pci_bus *bus_dev,
742 return PCIBIOS_SUCCESSFUL;
745 struct pci_bus * __devinit pci_scan_one_pbm(struct pci_pbm_info *pbm)
747 struct pci_controller_info *p = pbm->parent;
748 struct device_node *node = pbm->prom_node;
749 struct pci_dev *host_pdev;
752 printk("PCI: Scanning PBM %s\n", node->full_name);
754 /* XXX parent device? XXX */
755 bus = pci_create_bus(NULL, pbm->pci_first_busno, p->pci_ops, pbm);
757 printk(KERN_ERR "Failed to create bus for %s\n",
761 bus->secondary = pbm->pci_first_busno;
762 bus->subordinate = pbm->pci_last_busno;
764 bus->resource[0] = &pbm->io_space;
765 bus->resource[1] = &pbm->mem_space;
767 /* Create the dummy host bridge and link it in. */
768 host_pdev = of_create_pci_dev(pbm, node, bus, 0x00, 1);
769 bus->self = host_pdev;
771 pci_of_scan_bus(pbm, node, bus);
772 pci_bus_add_devices(bus);
773 pci_bus_register_of_sysfs(bus);
778 static void __init pci_scan_each_controller_bus(void)
780 struct pci_controller_info *p;
782 for (p = pci_controller_root; p; p = p->next)
786 extern void power_init(void);
788 static int __init pcibios_init(void)
790 pci_controller_probe();
791 if (pci_controller_root == NULL)
794 pci_scan_each_controller_bus();
803 subsys_initcall(pcibios_init);
805 void __devinit pcibios_fixup_bus(struct pci_bus *pbus)
807 struct pci_pbm_info *pbm = pbus->sysdata;
809 /* Generic PCI bus probing sets these to point at
810 * &io{port,mem}_resouce which is wrong for us.
812 pbus->resource[0] = &pbm->io_space;
813 pbus->resource[1] = &pbm->mem_space;
816 struct resource *pcibios_select_root(struct pci_dev *pdev, struct resource *r)
818 struct pci_pbm_info *pbm = pdev->bus->sysdata;
819 struct resource *root = NULL;
821 if (r->flags & IORESOURCE_IO)
822 root = &pbm->io_space;
823 if (r->flags & IORESOURCE_MEM)
824 root = &pbm->mem_space;
829 void pcibios_update_irq(struct pci_dev *pdev, int irq)
833 void pcibios_align_resource(void *data, struct resource *res,
834 resource_size_t size, resource_size_t align)
838 int pcibios_enable_device(struct pci_dev *dev, int mask)
843 pci_read_config_word(dev, PCI_COMMAND, &cmd);
846 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
847 struct resource *res = &dev->resource[i];
849 /* Only set up the requested stuff */
850 if (!(mask & (1<<i)))
853 if (res->flags & IORESOURCE_IO)
854 cmd |= PCI_COMMAND_IO;
855 if (res->flags & IORESOURCE_MEM)
856 cmd |= PCI_COMMAND_MEMORY;
860 printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
862 /* Enable the appropriate bits in the PCI command register. */
863 pci_write_config_word(dev, PCI_COMMAND, cmd);
868 void pcibios_resource_to_bus(struct pci_dev *pdev, struct pci_bus_region *region,
869 struct resource *res)
871 struct pci_pbm_info *pbm = pdev->bus->sysdata;
872 struct resource zero_res, *root;
876 zero_res.flags = res->flags;
878 if (res->flags & IORESOURCE_IO)
879 root = &pbm->io_space;
881 root = &pbm->mem_space;
883 pci_resource_adjust(&zero_res, root);
885 region->start = res->start - zero_res.start;
886 region->end = res->end - zero_res.start;
888 EXPORT_SYMBOL(pcibios_resource_to_bus);
890 void pcibios_bus_to_resource(struct pci_dev *pdev, struct resource *res,
891 struct pci_bus_region *region)
893 struct pci_pbm_info *pbm = pdev->bus->sysdata;
894 struct resource *root;
896 res->start = region->start;
897 res->end = region->end;
899 if (res->flags & IORESOURCE_IO)
900 root = &pbm->io_space;
902 root = &pbm->mem_space;
904 pci_resource_adjust(res, root);
906 EXPORT_SYMBOL(pcibios_bus_to_resource);
908 char * __devinit pcibios_setup(char *str)
913 /* Platform support for /proc/bus/pci/X/Y mmap()s. */
915 /* If the user uses a host-bridge as the PCI device, he may use
916 * this to perform a raw mmap() of the I/O or MEM space behind
919 * This can be useful for execution of x86 PCI bios initialization code
920 * on a PCI card, like the xfree86 int10 stuff does.
922 static int __pci_mmap_make_offset_bus(struct pci_dev *pdev, struct vm_area_struct *vma,
923 enum pci_mmap_state mmap_state)
925 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
926 struct pci_controller_info *p;
927 unsigned long space_size, user_offset, user_size;
930 if (mmap_state == pci_mmap_io) {
931 space_size = (pbm->io_space.end -
932 pbm->io_space.start) + 1;
934 space_size = (pbm->mem_space.end -
935 pbm->mem_space.start) + 1;
938 /* Make sure the request is in range. */
939 user_offset = vma->vm_pgoff << PAGE_SHIFT;
940 user_size = vma->vm_end - vma->vm_start;
942 if (user_offset >= space_size ||
943 (user_offset + user_size) > space_size)
946 if (mmap_state == pci_mmap_io) {
947 vma->vm_pgoff = (pbm->io_space.start +
948 user_offset) >> PAGE_SHIFT;
950 vma->vm_pgoff = (pbm->mem_space.start +
951 user_offset) >> PAGE_SHIFT;
957 /* Adjust vm_pgoff of VMA such that it is the physical page offset corresponding
958 * to the 32-bit pci bus offset for DEV requested by the user.
960 * Basically, the user finds the base address for his device which he wishes
961 * to mmap. They read the 32-bit value from the config space base register,
962 * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
963 * offset parameter of mmap on /proc/bus/pci/XXX for that device.
965 * Returns negative error code on failure, zero on success.
967 static int __pci_mmap_make_offset(struct pci_dev *dev, struct vm_area_struct *vma,
968 enum pci_mmap_state mmap_state)
970 unsigned long user_offset = vma->vm_pgoff << PAGE_SHIFT;
971 unsigned long user32 = user_offset & pci_memspace_mask;
972 unsigned long largest_base, this_base, addr32;
975 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
976 return __pci_mmap_make_offset_bus(dev, vma, mmap_state);
978 /* Figure out which base address this is for. */
980 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
981 struct resource *rp = &dev->resource[i];
988 if (i == PCI_ROM_RESOURCE) {
989 if (mmap_state != pci_mmap_mem)
992 if ((mmap_state == pci_mmap_io &&
993 (rp->flags & IORESOURCE_IO) == 0) ||
994 (mmap_state == pci_mmap_mem &&
995 (rp->flags & IORESOURCE_MEM) == 0))
999 this_base = rp->start;
1001 addr32 = (this_base & PAGE_MASK) & pci_memspace_mask;
1003 if (mmap_state == pci_mmap_io)
1006 if (addr32 <= user32 && this_base > largest_base)
1007 largest_base = this_base;
1010 if (largest_base == 0UL)
1013 /* Now construct the final physical address. */
1014 if (mmap_state == pci_mmap_io)
1015 vma->vm_pgoff = (((largest_base & ~0xffffffUL) | user32) >> PAGE_SHIFT);
1017 vma->vm_pgoff = (((largest_base & ~(pci_memspace_mask)) | user32) >> PAGE_SHIFT);
1022 /* Set vm_flags of VMA, as appropriate for this architecture, for a pci device
1025 static void __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
1026 enum pci_mmap_state mmap_state)
1028 vma->vm_flags |= (VM_IO | VM_RESERVED);
1031 /* Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
1034 static void __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
1035 enum pci_mmap_state mmap_state)
1037 /* Our io_remap_pfn_range takes care of this, do nothing. */
1040 /* Perform the actual remap of the pages for a PCI device mapping, as appropriate
1041 * for this architecture. The region in the process to map is described by vm_start
1042 * and vm_end members of VMA, the base physical address is found in vm_pgoff.
1043 * The pci device structure is provided so that architectures may make mapping
1044 * decisions on a per-device or per-bus basis.
1046 * Returns a negative error code on failure, zero on success.
1048 int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
1049 enum pci_mmap_state mmap_state,
1054 ret = __pci_mmap_make_offset(dev, vma, mmap_state);
1058 __pci_mmap_set_flags(dev, vma, mmap_state);
1059 __pci_mmap_set_pgprot(dev, vma, mmap_state);
1061 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1062 ret = io_remap_pfn_range(vma, vma->vm_start,
1064 vma->vm_end - vma->vm_start,
1072 /* Return the domain nuber for this pci bus */
1074 int pci_domain_nr(struct pci_bus *pbus)
1076 struct pci_pbm_info *pbm = pbus->sysdata;
1079 if (pbm == NULL || pbm->parent == NULL) {
1082 struct pci_controller_info *p = pbm->parent;
1086 ((pbm == &pbm->parent->pbm_B) ? 1 : 0));
1091 EXPORT_SYMBOL(pci_domain_nr);
1093 #ifdef CONFIG_PCI_MSI
1094 int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
1096 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
1097 struct pci_controller_info *p = pbm->parent;
1100 if (!pbm->msi_num || !p->setup_msi_irq)
1103 err = p->setup_msi_irq(&virt_irq, pdev, desc);
1110 void arch_teardown_msi_irq(unsigned int virt_irq)
1112 struct msi_desc *entry = get_irq_msi(virt_irq);
1113 struct pci_dev *pdev = entry->dev;
1114 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
1115 struct pci_controller_info *p = pbm->parent;
1117 if (!pbm->msi_num || !p->setup_msi_irq)
1120 return p->teardown_msi_irq(virt_irq, pdev);
1122 #endif /* !(CONFIG_PCI_MSI) */
1124 struct device_node *pci_device_to_OF_node(struct pci_dev *pdev)
1126 return pdev->dev.archdata.prom_node;
1128 EXPORT_SYMBOL(pci_device_to_OF_node);
1130 #endif /* !(CONFIG_PCI) */