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 <linux/of_device.h>
24 #include <asm/uaccess.h>
25 #include <asm/pgtable.h>
32 /* List of all PCI controllers found in the system. */
33 struct pci_pbm_info
*pci_pbm_root
= NULL
;
35 /* Each PBM found gets a unique index. */
38 volatile int pci_poke_in_progress
;
39 volatile int pci_poke_cpu
= -1;
40 volatile int pci_poke_faulted
;
42 static DEFINE_SPINLOCK(pci_poke_lock
);
44 void pci_config_read8(u8
*addr
, u8
*ret
)
49 spin_lock_irqsave(&pci_poke_lock
, flags
);
50 pci_poke_cpu
= smp_processor_id();
51 pci_poke_in_progress
= 1;
53 __asm__
__volatile__("membar #Sync\n\t"
54 "lduba [%1] %2, %0\n\t"
57 : "r" (addr
), "i" (ASI_PHYS_BYPASS_EC_E_L
)
59 pci_poke_in_progress
= 0;
61 if (!pci_poke_faulted
)
63 spin_unlock_irqrestore(&pci_poke_lock
, flags
);
66 void pci_config_read16(u16
*addr
, u16
*ret
)
71 spin_lock_irqsave(&pci_poke_lock
, flags
);
72 pci_poke_cpu
= smp_processor_id();
73 pci_poke_in_progress
= 1;
75 __asm__
__volatile__("membar #Sync\n\t"
76 "lduha [%1] %2, %0\n\t"
79 : "r" (addr
), "i" (ASI_PHYS_BYPASS_EC_E_L
)
81 pci_poke_in_progress
= 0;
83 if (!pci_poke_faulted
)
85 spin_unlock_irqrestore(&pci_poke_lock
, flags
);
88 void pci_config_read32(u32
*addr
, u32
*ret
)
93 spin_lock_irqsave(&pci_poke_lock
, flags
);
94 pci_poke_cpu
= smp_processor_id();
95 pci_poke_in_progress
= 1;
97 __asm__
__volatile__("membar #Sync\n\t"
98 "lduwa [%1] %2, %0\n\t"
101 : "r" (addr
), "i" (ASI_PHYS_BYPASS_EC_E_L
)
103 pci_poke_in_progress
= 0;
105 if (!pci_poke_faulted
)
107 spin_unlock_irqrestore(&pci_poke_lock
, flags
);
110 void pci_config_write8(u8
*addr
, u8 val
)
114 spin_lock_irqsave(&pci_poke_lock
, flags
);
115 pci_poke_cpu
= smp_processor_id();
116 pci_poke_in_progress
= 1;
117 pci_poke_faulted
= 0;
118 __asm__
__volatile__("membar #Sync\n\t"
119 "stba %0, [%1] %2\n\t"
122 : "r" (val
), "r" (addr
), "i" (ASI_PHYS_BYPASS_EC_E_L
)
124 pci_poke_in_progress
= 0;
126 spin_unlock_irqrestore(&pci_poke_lock
, flags
);
129 void pci_config_write16(u16
*addr
, u16 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 "stha %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_write32(u32
*addr
, u32 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 "stwa %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 /* Probe for all PCI controllers in the system. */
168 extern void sabre_init(struct device_node
*, const char *);
169 extern void psycho_init(struct device_node
*, const char *);
170 extern void fire_pci_init(struct device_node
*, const char *);
174 void (*init
)(struct device_node
*, const char *);
175 } pci_controller_table
[] __initdata
= {
176 { "SUNW,sabre", sabre_init
},
177 { "pci108e,a000", sabre_init
},
178 { "pci108e,a001", sabre_init
},
179 { "SUNW,psycho", psycho_init
},
180 { "pci108e,8000", psycho_init
},
181 { "pciex108e,80f0", fire_pci_init
},
183 #define PCI_NUM_CONTROLLER_TYPES ARRAY_SIZE(pci_controller_table)
185 static int __init
pci_controller_init(const char *model_name
, int namelen
, struct device_node
*dp
)
189 for (i
= 0; i
< PCI_NUM_CONTROLLER_TYPES
; i
++) {
190 if (!strncmp(model_name
,
191 pci_controller_table
[i
].model_name
,
193 pci_controller_table
[i
].init(dp
, model_name
);
201 static int __init
pci_controller_scan(int (*handler
)(const char *, int, struct device_node
*))
203 struct device_node
*dp
;
206 for_each_node_by_name(dp
, "pci") {
207 struct property
*prop
;
210 prop
= of_find_property(dp
, "model", &len
);
212 prop
= of_find_property(dp
, "compatible", &len
);
215 const char *model
= prop
->value
;
218 /* Our value may be a multi-valued string in the
219 * case of some compatible properties. For sanity,
220 * only try the first one.
222 while (model
[item_len
] && len
) {
227 if (handler(model
, item_len
, dp
))
235 /* Find each controller in the system, attach and initialize
236 * software state structure for each and link into the
237 * pci_pbm_root. Setup the controller enough such
238 * that bus scanning can be done.
240 static void __init
pci_controller_probe(void)
242 printk("PCI: Probing for controllers.\n");
244 pci_controller_scan(pci_controller_init
);
247 static int ofpci_verbose
;
249 static int __init
ofpci_debug(char *str
)
253 get_option(&str
, &val
);
259 __setup("ofpci_debug=", ofpci_debug
);
261 static unsigned long pci_parse_of_flags(u32 addr0
)
263 unsigned long flags
= 0;
265 if (addr0
& 0x02000000) {
266 flags
= IORESOURCE_MEM
| PCI_BASE_ADDRESS_SPACE_MEMORY
;
267 flags
|= (addr0
>> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64
;
268 flags
|= (addr0
>> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M
;
269 if (addr0
& 0x40000000)
270 flags
|= IORESOURCE_PREFETCH
271 | PCI_BASE_ADDRESS_MEM_PREFETCH
;
272 } else if (addr0
& 0x01000000)
273 flags
= IORESOURCE_IO
| PCI_BASE_ADDRESS_SPACE_IO
;
277 /* The of_device layer has translated all of the assigned-address properties
278 * into physical address resources, we only have to figure out the register
281 static void pci_parse_of_addrs(struct of_device
*op
,
282 struct device_node
*node
,
285 struct resource
*op_res
;
289 addrs
= of_get_property(node
, "assigned-addresses", &proplen
);
293 printk(" parse addresses (%d bytes) @ %p\n",
295 op_res
= &op
->resource
[0];
296 for (; proplen
>= 20; proplen
-= 20, addrs
+= 5, op_res
++) {
297 struct resource
*res
;
301 flags
= pci_parse_of_flags(addrs
[0]);
306 printk(" start: %lx, end: %lx, i: %x\n",
307 op_res
->start
, op_res
->end
, i
);
309 if (PCI_BASE_ADDRESS_0
<= i
&& i
<= PCI_BASE_ADDRESS_5
) {
310 res
= &dev
->resource
[(i
- PCI_BASE_ADDRESS_0
) >> 2];
311 } else if (i
== dev
->rom_base_reg
) {
312 res
= &dev
->resource
[PCI_ROM_RESOURCE
];
313 flags
|= IORESOURCE_READONLY
| IORESOURCE_CACHEABLE
;
315 printk(KERN_ERR
"PCI: bad cfg reg num 0x%x\n", i
);
318 res
->start
= op_res
->start
;
319 res
->end
= op_res
->end
;
321 res
->name
= pci_name(dev
);
325 struct pci_dev
*of_create_pci_dev(struct pci_pbm_info
*pbm
,
326 struct device_node
*node
,
327 struct pci_bus
*bus
, int devfn
)
329 struct dev_archdata
*sd
;
330 struct of_device
*op
;
335 dev
= alloc_pci_dev();
339 sd
= &dev
->dev
.archdata
;
340 sd
->iommu
= pbm
->iommu
;
342 sd
->host_controller
= pbm
;
343 sd
->prom_node
= node
;
344 sd
->op
= op
= of_find_device_by_node(node
);
345 sd
->numa_node
= pbm
->numa_node
;
347 sd
= &op
->dev
.archdata
;
348 sd
->iommu
= pbm
->iommu
;
350 sd
->numa_node
= pbm
->numa_node
;
352 if (!strcmp(node
->name
, "ebus"))
353 of_propagate_archdata(op
);
355 type
= of_get_property(node
, "device_type", NULL
);
360 printk(" create device, devfn: %x, type: %s\n",
365 dev
->dev
.parent
= bus
->bridge
;
366 dev
->dev
.bus
= &pci_bus_type
;
368 dev
->multifunction
= 0; /* maybe a lie? */
370 dev
->vendor
= of_getintprop_default(node
, "vendor-id", 0xffff);
371 dev
->device
= of_getintprop_default(node
, "device-id", 0xffff);
372 dev
->subsystem_vendor
=
373 of_getintprop_default(node
, "subsystem-vendor-id", 0);
374 dev
->subsystem_device
=
375 of_getintprop_default(node
, "subsystem-id", 0);
377 dev
->cfg_size
= pci_cfg_space_size(dev
);
379 /* We can't actually use the firmware value, we have
380 * to read what is in the register right now. One
381 * reason is that in the case of IDE interfaces the
382 * firmware can sample the value before the the IDE
383 * interface is programmed into native mode.
385 pci_read_config_dword(dev
, PCI_CLASS_REVISION
, &class);
386 dev
->class = class >> 8;
387 dev
->revision
= class & 0xff;
389 dev_set_name(&dev
->dev
, "%04x:%02x:%02x.%d", pci_domain_nr(bus
),
390 dev
->bus
->number
, PCI_SLOT(devfn
), PCI_FUNC(devfn
));
393 printk(" class: 0x%x device name: %s\n",
394 dev
->class, pci_name(dev
));
396 /* I have seen IDE devices which will not respond to
397 * the bmdma simplex check reads if bus mastering is
400 if ((dev
->class >> 8) == PCI_CLASS_STORAGE_IDE
)
403 dev
->current_state
= 4; /* unknown power state */
404 dev
->error_state
= pci_channel_io_normal
;
406 if (!strcmp(type
, "pci") || !strcmp(type
, "pciex")) {
407 /* a PCI-PCI bridge */
408 dev
->hdr_type
= PCI_HEADER_TYPE_BRIDGE
;
409 dev
->rom_base_reg
= PCI_ROM_ADDRESS1
;
410 } else if (!strcmp(type
, "cardbus")) {
411 dev
->hdr_type
= PCI_HEADER_TYPE_CARDBUS
;
413 dev
->hdr_type
= PCI_HEADER_TYPE_NORMAL
;
414 dev
->rom_base_reg
= PCI_ROM_ADDRESS
;
416 dev
->irq
= sd
->op
->irqs
[0];
417 if (dev
->irq
== 0xffffffff)
418 dev
->irq
= PCI_IRQ_NONE
;
421 pci_parse_of_addrs(sd
->op
, node
, dev
);
424 printk(" adding to system ...\n");
426 pci_device_add(dev
, bus
);
431 static void __devinit
apb_calc_first_last(u8 map
, u32
*first_p
, u32
*last_p
)
433 u32 idx
, first
, last
;
437 for (idx
= 0; idx
< 8; idx
++) {
438 if ((map
& (1 << idx
)) != 0) {
450 static void pci_resource_adjust(struct resource
*res
,
451 struct resource
*root
)
453 res
->start
+= root
->start
;
454 res
->end
+= root
->start
;
457 /* For PCI bus devices which lack a 'ranges' property we interrogate
458 * the config space values to set the resources, just like the generic
459 * Linux PCI probing code does.
461 static void __devinit
pci_cfg_fake_ranges(struct pci_dev
*dev
,
463 struct pci_pbm_info
*pbm
)
465 struct resource
*res
;
466 u8 io_base_lo
, io_limit_lo
;
467 u16 mem_base_lo
, mem_limit_lo
;
468 unsigned long base
, limit
;
470 pci_read_config_byte(dev
, PCI_IO_BASE
, &io_base_lo
);
471 pci_read_config_byte(dev
, PCI_IO_LIMIT
, &io_limit_lo
);
472 base
= (io_base_lo
& PCI_IO_RANGE_MASK
) << 8;
473 limit
= (io_limit_lo
& PCI_IO_RANGE_MASK
) << 8;
475 if ((io_base_lo
& PCI_IO_RANGE_TYPE_MASK
) == PCI_IO_RANGE_TYPE_32
) {
476 u16 io_base_hi
, io_limit_hi
;
478 pci_read_config_word(dev
, PCI_IO_BASE_UPPER16
, &io_base_hi
);
479 pci_read_config_word(dev
, PCI_IO_LIMIT_UPPER16
, &io_limit_hi
);
480 base
|= (io_base_hi
<< 16);
481 limit
|= (io_limit_hi
<< 16);
484 res
= bus
->resource
[0];
486 res
->flags
= (io_base_lo
& PCI_IO_RANGE_TYPE_MASK
) | IORESOURCE_IO
;
490 res
->end
= limit
+ 0xfff;
491 pci_resource_adjust(res
, &pbm
->io_space
);
494 pci_read_config_word(dev
, PCI_MEMORY_BASE
, &mem_base_lo
);
495 pci_read_config_word(dev
, PCI_MEMORY_LIMIT
, &mem_limit_lo
);
496 base
= (mem_base_lo
& PCI_MEMORY_RANGE_MASK
) << 16;
497 limit
= (mem_limit_lo
& PCI_MEMORY_RANGE_MASK
) << 16;
499 res
= bus
->resource
[1];
501 res
->flags
= ((mem_base_lo
& PCI_MEMORY_RANGE_TYPE_MASK
) |
504 res
->end
= limit
+ 0xfffff;
505 pci_resource_adjust(res
, &pbm
->mem_space
);
508 pci_read_config_word(dev
, PCI_PREF_MEMORY_BASE
, &mem_base_lo
);
509 pci_read_config_word(dev
, PCI_PREF_MEMORY_LIMIT
, &mem_limit_lo
);
510 base
= (mem_base_lo
& PCI_PREF_RANGE_MASK
) << 16;
511 limit
= (mem_limit_lo
& PCI_PREF_RANGE_MASK
) << 16;
513 if ((mem_base_lo
& PCI_PREF_RANGE_TYPE_MASK
) == PCI_PREF_RANGE_TYPE_64
) {
514 u32 mem_base_hi
, mem_limit_hi
;
516 pci_read_config_dword(dev
, PCI_PREF_BASE_UPPER32
, &mem_base_hi
);
517 pci_read_config_dword(dev
, PCI_PREF_LIMIT_UPPER32
, &mem_limit_hi
);
520 * Some bridges set the base > limit by default, and some
521 * (broken) BIOSes do not initialize them. If we find
522 * this, just assume they are not being used.
524 if (mem_base_hi
<= mem_limit_hi
) {
525 base
|= ((long) mem_base_hi
) << 32;
526 limit
|= ((long) mem_limit_hi
) << 32;
530 res
= bus
->resource
[2];
532 res
->flags
= ((mem_base_lo
& PCI_MEMORY_RANGE_TYPE_MASK
) |
533 IORESOURCE_MEM
| IORESOURCE_PREFETCH
);
535 res
->end
= limit
+ 0xfffff;
536 pci_resource_adjust(res
, &pbm
->mem_space
);
540 /* Cook up fake bus resources for SUNW,simba PCI bridges which lack
541 * a proper 'ranges' property.
543 static void __devinit
apb_fake_ranges(struct pci_dev
*dev
,
545 struct pci_pbm_info
*pbm
)
547 struct resource
*res
;
551 pci_read_config_byte(dev
, APB_IO_ADDRESS_MAP
, &map
);
552 apb_calc_first_last(map
, &first
, &last
);
553 res
= bus
->resource
[0];
554 res
->start
= (first
<< 21);
555 res
->end
= (last
<< 21) + ((1 << 21) - 1);
556 res
->flags
= IORESOURCE_IO
;
557 pci_resource_adjust(res
, &pbm
->io_space
);
559 pci_read_config_byte(dev
, APB_MEM_ADDRESS_MAP
, &map
);
560 apb_calc_first_last(map
, &first
, &last
);
561 res
= bus
->resource
[1];
562 res
->start
= (first
<< 21);
563 res
->end
= (last
<< 21) + ((1 << 21) - 1);
564 res
->flags
= IORESOURCE_MEM
;
565 pci_resource_adjust(res
, &pbm
->mem_space
);
568 static void __devinit
pci_of_scan_bus(struct pci_pbm_info
*pbm
,
569 struct device_node
*node
,
570 struct pci_bus
*bus
);
572 #define GET_64BIT(prop, i) ((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1])
574 static void __devinit
of_scan_pci_bridge(struct pci_pbm_info
*pbm
,
575 struct device_node
*node
,
579 const u32
*busrange
, *ranges
;
581 struct resource
*res
;
586 printk("of_scan_pci_bridge(%s)\n", node
->full_name
);
588 /* parse bus-range property */
589 busrange
= of_get_property(node
, "bus-range", &len
);
590 if (busrange
== NULL
|| len
!= 8) {
591 printk(KERN_DEBUG
"Can't get bus-range for PCI-PCI bridge %s\n",
595 ranges
= of_get_property(node
, "ranges", &len
);
597 if (ranges
== NULL
) {
598 const char *model
= of_get_property(node
, "model", NULL
);
599 if (model
&& !strcmp(model
, "SUNW,simba"))
603 bus
= pci_add_new_bus(dev
->bus
, dev
, busrange
[0]);
605 printk(KERN_ERR
"Failed to create pci bus for %s\n",
610 bus
->primary
= dev
->bus
->number
;
611 bus
->subordinate
= busrange
[1];
614 /* parse ranges property, or cook one up by hand for Simba */
615 /* PCI #address-cells == 3 and #size-cells == 2 always */
616 res
= &dev
->resource
[PCI_BRIDGE_RESOURCES
];
617 for (i
= 0; i
< PCI_NUM_RESOURCES
- PCI_BRIDGE_RESOURCES
; ++i
) {
619 bus
->resource
[i
] = res
;
623 apb_fake_ranges(dev
, bus
, pbm
);
625 } else if (ranges
== NULL
) {
626 pci_cfg_fake_ranges(dev
, bus
, pbm
);
630 for (; len
>= 32; len
-= 32, ranges
+= 8) {
631 struct resource
*root
;
633 flags
= pci_parse_of_flags(ranges
[0]);
634 size
= GET_64BIT(ranges
, 6);
635 if (flags
== 0 || size
== 0)
637 if (flags
& IORESOURCE_IO
) {
638 res
= bus
->resource
[0];
640 printk(KERN_ERR
"PCI: ignoring extra I/O range"
641 " for bridge %s\n", node
->full_name
);
644 root
= &pbm
->io_space
;
646 if (i
>= PCI_NUM_RESOURCES
- PCI_BRIDGE_RESOURCES
) {
647 printk(KERN_ERR
"PCI: too many memory ranges"
648 " for bridge %s\n", node
->full_name
);
651 res
= bus
->resource
[i
];
653 root
= &pbm
->mem_space
;
656 res
->start
= GET_64BIT(ranges
, 1);
657 res
->end
= res
->start
+ size
- 1;
660 /* Another way to implement this would be to add an of_device
661 * layer routine that can calculate a resource for a given
662 * range property value in a PCI device.
664 pci_resource_adjust(res
, root
);
667 sprintf(bus
->name
, "PCI Bus %04x:%02x", pci_domain_nr(bus
),
670 printk(" bus name: %s\n", bus
->name
);
672 pci_of_scan_bus(pbm
, node
, bus
);
675 static void __devinit
pci_of_scan_bus(struct pci_pbm_info
*pbm
,
676 struct device_node
*node
,
679 struct device_node
*child
;
681 int reglen
, devfn
, prev_devfn
;
685 printk("PCI: scan_bus[%s] bus no %d\n",
686 node
->full_name
, bus
->number
);
690 while ((child
= of_get_next_child(node
, child
)) != NULL
) {
692 printk(" * %s\n", child
->full_name
);
693 reg
= of_get_property(child
, "reg", ®len
);
694 if (reg
== NULL
|| reglen
< 20)
697 devfn
= (reg
[0] >> 8) & 0xff;
699 /* This is a workaround for some device trees
700 * which list PCI devices twice. On the V100
701 * for example, device number 3 is listed twice.
702 * Once as "pm" and once again as "lomp".
704 if (devfn
== prev_devfn
)
708 /* create a new pci_dev for this device */
709 dev
= of_create_pci_dev(pbm
, child
, bus
, devfn
);
713 printk("PCI: dev header type: %x\n",
716 if (dev
->hdr_type
== PCI_HEADER_TYPE_BRIDGE
||
717 dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
)
718 of_scan_pci_bridge(pbm
, child
, dev
);
723 show_pciobppath_attr(struct device
* dev
, struct device_attribute
* attr
, char * buf
)
725 struct pci_dev
*pdev
;
726 struct device_node
*dp
;
728 pdev
= to_pci_dev(dev
);
729 dp
= pdev
->dev
.archdata
.prom_node
;
731 return snprintf (buf
, PAGE_SIZE
, "%s\n", dp
->full_name
);
734 static DEVICE_ATTR(obppath
, S_IRUSR
| S_IRGRP
| S_IROTH
, show_pciobppath_attr
, NULL
);
736 static void __devinit
pci_bus_register_of_sysfs(struct pci_bus
*bus
)
739 struct pci_bus
*child_bus
;
742 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
743 /* we don't really care if we can create this file or
744 * not, but we need to assign the result of the call
745 * or the world will fall under alien invasion and
746 * everybody will be frozen on a spaceship ready to be
747 * eaten on alpha centauri by some green and jelly
750 err
= sysfs_create_file(&dev
->dev
.kobj
, &dev_attr_obppath
.attr
);
752 list_for_each_entry(child_bus
, &bus
->children
, node
)
753 pci_bus_register_of_sysfs(child_bus
);
756 struct pci_bus
* __devinit
pci_scan_one_pbm(struct pci_pbm_info
*pbm
)
758 struct device_node
*node
= pbm
->prom_node
;
761 printk("PCI: Scanning PBM %s\n", node
->full_name
);
763 /* XXX parent device? XXX */
764 bus
= pci_create_bus(NULL
, pbm
->pci_first_busno
, pbm
->pci_ops
, pbm
);
766 printk(KERN_ERR
"Failed to create bus for %s\n",
770 bus
->secondary
= pbm
->pci_first_busno
;
771 bus
->subordinate
= pbm
->pci_last_busno
;
773 bus
->resource
[0] = &pbm
->io_space
;
774 bus
->resource
[1] = &pbm
->mem_space
;
776 pci_of_scan_bus(pbm
, node
, bus
);
777 pci_bus_add_devices(bus
);
778 pci_bus_register_of_sysfs(bus
);
783 static void __init
pci_scan_each_controller_bus(void)
785 struct pci_pbm_info
*pbm
;
787 for (pbm
= pci_pbm_root
; pbm
; pbm
= pbm
->next
) {
793 static int __init
pcibios_init(void)
795 pci_controller_probe();
796 if (pci_pbm_root
== NULL
)
799 pci_scan_each_controller_bus();
804 subsys_initcall(pcibios_init
);
806 void __devinit
pcibios_fixup_bus(struct pci_bus
*pbus
)
808 struct pci_pbm_info
*pbm
= pbus
->sysdata
;
810 /* Generic PCI bus probing sets these to point at
811 * &io{port,mem}_resouce which is wrong for us.
813 pbus
->resource
[0] = &pbm
->io_space
;
814 pbus
->resource
[1] = &pbm
->mem_space
;
817 struct resource
*pcibios_select_root(struct pci_dev
*pdev
, struct resource
*r
)
819 struct pci_pbm_info
*pbm
= pdev
->bus
->sysdata
;
820 struct resource
*root
= NULL
;
822 if (r
->flags
& IORESOURCE_IO
)
823 root
= &pbm
->io_space
;
824 if (r
->flags
& IORESOURCE_MEM
)
825 root
= &pbm
->mem_space
;
830 void pcibios_update_irq(struct pci_dev
*pdev
, int irq
)
834 void pcibios_align_resource(void *data
, struct resource
*res
,
835 resource_size_t size
, resource_size_t align
)
839 int pcibios_enable_device(struct pci_dev
*dev
, int mask
)
844 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
847 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
848 struct resource
*res
= &dev
->resource
[i
];
850 /* Only set up the requested stuff */
851 if (!(mask
& (1<<i
)))
854 if (res
->flags
& IORESOURCE_IO
)
855 cmd
|= PCI_COMMAND_IO
;
856 if (res
->flags
& IORESOURCE_MEM
)
857 cmd
|= PCI_COMMAND_MEMORY
;
861 printk(KERN_DEBUG
"PCI: Enabling device: (%s), cmd %x\n",
863 /* Enable the appropriate bits in the PCI command register. */
864 pci_write_config_word(dev
, PCI_COMMAND
, cmd
);
869 void pcibios_resource_to_bus(struct pci_dev
*pdev
, struct pci_bus_region
*region
,
870 struct resource
*res
)
872 struct pci_pbm_info
*pbm
= pdev
->bus
->sysdata
;
873 struct resource zero_res
, *root
;
877 zero_res
.flags
= res
->flags
;
879 if (res
->flags
& IORESOURCE_IO
)
880 root
= &pbm
->io_space
;
882 root
= &pbm
->mem_space
;
884 pci_resource_adjust(&zero_res
, root
);
886 region
->start
= res
->start
- zero_res
.start
;
887 region
->end
= res
->end
- zero_res
.start
;
889 EXPORT_SYMBOL(pcibios_resource_to_bus
);
891 void pcibios_bus_to_resource(struct pci_dev
*pdev
, struct resource
*res
,
892 struct pci_bus_region
*region
)
894 struct pci_pbm_info
*pbm
= pdev
->bus
->sysdata
;
895 struct resource
*root
;
897 res
->start
= region
->start
;
898 res
->end
= region
->end
;
900 if (res
->flags
& IORESOURCE_IO
)
901 root
= &pbm
->io_space
;
903 root
= &pbm
->mem_space
;
905 pci_resource_adjust(res
, root
);
907 EXPORT_SYMBOL(pcibios_bus_to_resource
);
909 char * __devinit
pcibios_setup(char *str
)
914 /* Platform support for /proc/bus/pci/X/Y mmap()s. */
916 /* If the user uses a host-bridge as the PCI device, he may use
917 * this to perform a raw mmap() of the I/O or MEM space behind
920 * This can be useful for execution of x86 PCI bios initialization code
921 * on a PCI card, like the xfree86 int10 stuff does.
923 static int __pci_mmap_make_offset_bus(struct pci_dev
*pdev
, struct vm_area_struct
*vma
,
924 enum pci_mmap_state mmap_state
)
926 struct pci_pbm_info
*pbm
= pdev
->dev
.archdata
.host_controller
;
927 unsigned long space_size
, user_offset
, user_size
;
929 if (mmap_state
== pci_mmap_io
) {
930 space_size
= (pbm
->io_space
.end
-
931 pbm
->io_space
.start
) + 1;
933 space_size
= (pbm
->mem_space
.end
-
934 pbm
->mem_space
.start
) + 1;
937 /* Make sure the request is in range. */
938 user_offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
939 user_size
= vma
->vm_end
- vma
->vm_start
;
941 if (user_offset
>= space_size
||
942 (user_offset
+ user_size
) > space_size
)
945 if (mmap_state
== pci_mmap_io
) {
946 vma
->vm_pgoff
= (pbm
->io_space
.start
+
947 user_offset
) >> PAGE_SHIFT
;
949 vma
->vm_pgoff
= (pbm
->mem_space
.start
+
950 user_offset
) >> PAGE_SHIFT
;
956 /* Adjust vm_pgoff of VMA such that it is the physical page offset
957 * corresponding to the 32-bit pci bus offset for DEV requested by the user.
959 * Basically, the user finds the base address for his device which he wishes
960 * to mmap. They read the 32-bit value from the config space base register,
961 * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
962 * offset parameter of mmap on /proc/bus/pci/XXX for that device.
964 * Returns negative error code on failure, zero on success.
966 static int __pci_mmap_make_offset(struct pci_dev
*pdev
,
967 struct vm_area_struct
*vma
,
968 enum pci_mmap_state mmap_state
)
970 unsigned long user_paddr
, user_size
;
973 /* First compute the physical address in vma->vm_pgoff,
974 * making sure the user offset is within range in the
975 * appropriate PCI space.
977 err
= __pci_mmap_make_offset_bus(pdev
, vma
, mmap_state
);
981 /* If this is a mapping on a host bridge, any address
984 if ((pdev
->class >> 8) == PCI_CLASS_BRIDGE_HOST
)
987 /* Otherwise make sure it's in the range for one of the
988 * device's resources.
990 user_paddr
= vma
->vm_pgoff
<< PAGE_SHIFT
;
991 user_size
= vma
->vm_end
- vma
->vm_start
;
993 for (i
= 0; i
<= PCI_ROM_RESOURCE
; i
++) {
994 struct resource
*rp
= &pdev
->resource
[i
];
1001 if (i
== PCI_ROM_RESOURCE
) {
1002 if (mmap_state
!= pci_mmap_mem
)
1005 if ((mmap_state
== pci_mmap_io
&&
1006 (rp
->flags
& IORESOURCE_IO
) == 0) ||
1007 (mmap_state
== pci_mmap_mem
&&
1008 (rp
->flags
& IORESOURCE_MEM
) == 0))
1012 if ((rp
->start
<= user_paddr
) &&
1013 (user_paddr
+ user_size
) <= (rp
->end
+ 1UL))
1017 if (i
> PCI_ROM_RESOURCE
)
1023 /* Set vm_flags of VMA, as appropriate for this architecture, for a pci device
1026 static void __pci_mmap_set_flags(struct pci_dev
*dev
, struct vm_area_struct
*vma
,
1027 enum pci_mmap_state mmap_state
)
1029 vma
->vm_flags
|= (VM_IO
| VM_RESERVED
);
1032 /* Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
1035 static void __pci_mmap_set_pgprot(struct pci_dev
*dev
, struct vm_area_struct
*vma
,
1036 enum pci_mmap_state mmap_state
)
1038 /* Our io_remap_pfn_range takes care of this, do nothing. */
1041 /* Perform the actual remap of the pages for a PCI device mapping, as appropriate
1042 * for this architecture. The region in the process to map is described by vm_start
1043 * and vm_end members of VMA, the base physical address is found in vm_pgoff.
1044 * The pci device structure is provided so that architectures may make mapping
1045 * decisions on a per-device or per-bus basis.
1047 * Returns a negative error code on failure, zero on success.
1049 int pci_mmap_page_range(struct pci_dev
*dev
, struct vm_area_struct
*vma
,
1050 enum pci_mmap_state mmap_state
,
1055 ret
= __pci_mmap_make_offset(dev
, vma
, mmap_state
);
1059 __pci_mmap_set_flags(dev
, vma
, mmap_state
);
1060 __pci_mmap_set_pgprot(dev
, vma
, mmap_state
);
1062 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
1063 ret
= io_remap_pfn_range(vma
, vma
->vm_start
,
1065 vma
->vm_end
- vma
->vm_start
,
1074 int pcibus_to_node(struct pci_bus
*pbus
)
1076 struct pci_pbm_info
*pbm
= pbus
->sysdata
;
1078 return pbm
->numa_node
;
1080 EXPORT_SYMBOL(pcibus_to_node
);
1083 /* Return the domain nuber for this pci bus */
1085 int pci_domain_nr(struct pci_bus
*pbus
)
1087 struct pci_pbm_info
*pbm
= pbus
->sysdata
;
1090 if (pbm
== NULL
|| pbm
->parent
== NULL
) {
1098 EXPORT_SYMBOL(pci_domain_nr
);
1100 #ifdef CONFIG_PCI_MSI
1101 int arch_setup_msi_irq(struct pci_dev
*pdev
, struct msi_desc
*desc
)
1103 struct pci_pbm_info
*pbm
= pdev
->dev
.archdata
.host_controller
;
1106 if (!pbm
->setup_msi_irq
)
1109 return pbm
->setup_msi_irq(&virt_irq
, pdev
, desc
);
1112 void arch_teardown_msi_irq(unsigned int virt_irq
)
1114 struct msi_desc
*entry
= get_irq_msi(virt_irq
);
1115 struct pci_dev
*pdev
= entry
->dev
;
1116 struct pci_pbm_info
*pbm
= pdev
->dev
.archdata
.host_controller
;
1118 if (!pbm
->teardown_msi_irq
)
1121 return pbm
->teardown_msi_irq(virt_irq
, pdev
);
1123 #endif /* !(CONFIG_PCI_MSI) */
1125 struct device_node
*pci_device_to_OF_node(struct pci_dev
*pdev
)
1127 return pdev
->dev
.archdata
.prom_node
;
1129 EXPORT_SYMBOL(pci_device_to_OF_node
);
1131 static void ali_sound_dma_hack(struct pci_dev
*pdev
, int set_bit
)
1133 struct pci_dev
*ali_isa_bridge
;
1136 /* ALI sound chips generate 31-bits of DMA, a special register
1137 * determines what bit 31 is emitted as.
1139 ali_isa_bridge
= pci_get_device(PCI_VENDOR_ID_AL
,
1140 PCI_DEVICE_ID_AL_M1533
,
1143 pci_read_config_byte(ali_isa_bridge
, 0x7e, &val
);
1148 pci_write_config_byte(ali_isa_bridge
, 0x7e, val
);
1149 pci_dev_put(ali_isa_bridge
);
1152 int pci_dma_supported(struct pci_dev
*pdev
, u64 device_mask
)
1157 dma_addr_mask
= 0xffffffff;
1159 struct iommu
*iommu
= pdev
->dev
.archdata
.iommu
;
1161 dma_addr_mask
= iommu
->dma_addr_mask
;
1163 if (pdev
->vendor
== PCI_VENDOR_ID_AL
&&
1164 pdev
->device
== PCI_DEVICE_ID_AL_M5451
&&
1165 device_mask
== 0x7fffffff) {
1166 ali_sound_dma_hack(pdev
,
1167 (dma_addr_mask
& 0x80000000) != 0);
1172 if (device_mask
>= (1UL << 32UL))
1175 return (device_mask
& dma_addr_mask
) == dma_addr_mask
;
1178 void pci_resource_to_user(const struct pci_dev
*pdev
, int bar
,
1179 const struct resource
*rp
, resource_size_t
*start
,
1180 resource_size_t
*end
)
1182 struct pci_pbm_info
*pbm
= pdev
->dev
.archdata
.host_controller
;
1183 unsigned long offset
;
1185 if (rp
->flags
& IORESOURCE_IO
)
1186 offset
= pbm
->io_space
.start
;
1188 offset
= pbm
->mem_space
.start
;
1190 *start
= rp
->start
- offset
;
1191 *end
= rp
->end
- offset
;