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>
32 unsigned long pci_memspace_mask
= 0xffffffffUL
;
35 /* A "nop" PCI implementation. */
36 asmlinkage
int sys_pciconfig_read(unsigned long bus
, unsigned long dfn
,
37 unsigned long off
, unsigned long len
,
42 asmlinkage
int sys_pciconfig_write(unsigned long bus
, unsigned long dfn
,
43 unsigned long off
, unsigned long len
,
50 /* List of all PCI controllers found in the system. */
51 struct pci_pbm_info
*pci_pbm_root
= NULL
;
53 /* Each PBM found gets a unique index. */
56 volatile int pci_poke_in_progress
;
57 volatile int pci_poke_cpu
= -1;
58 volatile int pci_poke_faulted
;
60 static DEFINE_SPINLOCK(pci_poke_lock
);
62 void pci_config_read8(u8
*addr
, u8
*ret
)
67 spin_lock_irqsave(&pci_poke_lock
, flags
);
68 pci_poke_cpu
= smp_processor_id();
69 pci_poke_in_progress
= 1;
71 __asm__
__volatile__("membar #Sync\n\t"
72 "lduba [%1] %2, %0\n\t"
75 : "r" (addr
), "i" (ASI_PHYS_BYPASS_EC_E_L
)
77 pci_poke_in_progress
= 0;
79 if (!pci_poke_faulted
)
81 spin_unlock_irqrestore(&pci_poke_lock
, flags
);
84 void pci_config_read16(u16
*addr
, u16
*ret
)
89 spin_lock_irqsave(&pci_poke_lock
, flags
);
90 pci_poke_cpu
= smp_processor_id();
91 pci_poke_in_progress
= 1;
93 __asm__
__volatile__("membar #Sync\n\t"
94 "lduha [%1] %2, %0\n\t"
97 : "r" (addr
), "i" (ASI_PHYS_BYPASS_EC_E_L
)
99 pci_poke_in_progress
= 0;
101 if (!pci_poke_faulted
)
103 spin_unlock_irqrestore(&pci_poke_lock
, flags
);
106 void pci_config_read32(u32
*addr
, u32
*ret
)
111 spin_lock_irqsave(&pci_poke_lock
, flags
);
112 pci_poke_cpu
= smp_processor_id();
113 pci_poke_in_progress
= 1;
114 pci_poke_faulted
= 0;
115 __asm__
__volatile__("membar #Sync\n\t"
116 "lduwa [%1] %2, %0\n\t"
119 : "r" (addr
), "i" (ASI_PHYS_BYPASS_EC_E_L
)
121 pci_poke_in_progress
= 0;
123 if (!pci_poke_faulted
)
125 spin_unlock_irqrestore(&pci_poke_lock
, flags
);
128 void pci_config_write8(u8
*addr
, u8 val
)
132 spin_lock_irqsave(&pci_poke_lock
, flags
);
133 pci_poke_cpu
= smp_processor_id();
134 pci_poke_in_progress
= 1;
135 pci_poke_faulted
= 0;
136 __asm__
__volatile__("membar #Sync\n\t"
137 "stba %0, [%1] %2\n\t"
140 : "r" (val
), "r" (addr
), "i" (ASI_PHYS_BYPASS_EC_E_L
)
142 pci_poke_in_progress
= 0;
144 spin_unlock_irqrestore(&pci_poke_lock
, flags
);
147 void pci_config_write16(u16
*addr
, u16 val
)
151 spin_lock_irqsave(&pci_poke_lock
, flags
);
152 pci_poke_cpu
= smp_processor_id();
153 pci_poke_in_progress
= 1;
154 pci_poke_faulted
= 0;
155 __asm__
__volatile__("membar #Sync\n\t"
156 "stha %0, [%1] %2\n\t"
159 : "r" (val
), "r" (addr
), "i" (ASI_PHYS_BYPASS_EC_E_L
)
161 pci_poke_in_progress
= 0;
163 spin_unlock_irqrestore(&pci_poke_lock
, flags
);
166 void pci_config_write32(u32
*addr
, u32 val
)
170 spin_lock_irqsave(&pci_poke_lock
, flags
);
171 pci_poke_cpu
= smp_processor_id();
172 pci_poke_in_progress
= 1;
173 pci_poke_faulted
= 0;
174 __asm__
__volatile__("membar #Sync\n\t"
175 "stwa %0, [%1] %2\n\t"
178 : "r" (val
), "r" (addr
), "i" (ASI_PHYS_BYPASS_EC_E_L
)
180 pci_poke_in_progress
= 0;
182 spin_unlock_irqrestore(&pci_poke_lock
, flags
);
185 /* Probe for all PCI controllers in the system. */
186 extern void sabre_init(struct device_node
*, const char *);
187 extern void psycho_init(struct device_node
*, const char *);
188 extern void schizo_init(struct device_node
*, const char *);
189 extern void schizo_plus_init(struct device_node
*, const char *);
190 extern void tomatillo_init(struct device_node
*, const char *);
191 extern void sun4v_pci_init(struct device_node
*, const char *);
192 extern void fire_pci_init(struct device_node
*, const char *);
196 void (*init
)(struct device_node
*, const char *);
197 } pci_controller_table
[] __initdata
= {
198 { "SUNW,sabre", sabre_init
},
199 { "pci108e,a000", sabre_init
},
200 { "pci108e,a001", sabre_init
},
201 { "SUNW,psycho", psycho_init
},
202 { "pci108e,8000", psycho_init
},
203 { "SUNW,schizo", schizo_init
},
204 { "pci108e,8001", schizo_init
},
205 { "SUNW,schizo+", schizo_plus_init
},
206 { "pci108e,8002", schizo_plus_init
},
207 { "SUNW,tomatillo", tomatillo_init
},
208 { "pci108e,a801", tomatillo_init
},
209 { "SUNW,sun4v-pci", sun4v_pci_init
},
210 { "pciex108e,80f0", fire_pci_init
},
212 #define PCI_NUM_CONTROLLER_TYPES (sizeof(pci_controller_table) / \
213 sizeof(pci_controller_table[0]))
215 static int __init
pci_controller_init(const char *model_name
, int namelen
, struct device_node
*dp
)
219 for (i
= 0; i
< PCI_NUM_CONTROLLER_TYPES
; i
++) {
220 if (!strncmp(model_name
,
221 pci_controller_table
[i
].model_name
,
223 pci_controller_table
[i
].init(dp
, model_name
);
231 static int __init
pci_is_controller(const char *model_name
, int namelen
, struct device_node
*dp
)
235 for (i
= 0; i
< PCI_NUM_CONTROLLER_TYPES
; i
++) {
236 if (!strncmp(model_name
,
237 pci_controller_table
[i
].model_name
,
245 static int __init
pci_controller_scan(int (*handler
)(const char *, int, struct device_node
*))
247 struct device_node
*dp
;
250 for_each_node_by_name(dp
, "pci") {
251 struct property
*prop
;
254 prop
= of_find_property(dp
, "model", &len
);
256 prop
= of_find_property(dp
, "compatible", &len
);
259 const char *model
= prop
->value
;
262 /* Our value may be a multi-valued string in the
263 * case of some compatible properties. For sanity,
264 * only try the first one.
266 while (model
[item_len
] && len
) {
271 if (handler(model
, item_len
, dp
))
280 /* Is there some PCI controller in the system? */
281 int __init
pcic_present(void)
283 return pci_controller_scan(pci_is_controller
);
286 /* Find each controller in the system, attach and initialize
287 * software state structure for each and link into the
288 * pci_pbm_root. Setup the controller enough such
289 * that bus scanning can be done.
291 static void __init
pci_controller_probe(void)
293 printk("PCI: Probing for controllers.\n");
295 pci_controller_scan(pci_controller_init
);
298 static int ofpci_verbose
;
300 static int __init
ofpci_debug(char *str
)
304 get_option(&str
, &val
);
310 __setup("ofpci_debug=", ofpci_debug
);
312 static unsigned long pci_parse_of_flags(u32 addr0
)
314 unsigned long flags
= 0;
316 if (addr0
& 0x02000000) {
317 flags
= IORESOURCE_MEM
| PCI_BASE_ADDRESS_SPACE_MEMORY
;
318 flags
|= (addr0
>> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64
;
319 flags
|= (addr0
>> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M
;
320 if (addr0
& 0x40000000)
321 flags
|= IORESOURCE_PREFETCH
322 | PCI_BASE_ADDRESS_MEM_PREFETCH
;
323 } else if (addr0
& 0x01000000)
324 flags
= IORESOURCE_IO
| PCI_BASE_ADDRESS_SPACE_IO
;
328 /* The of_device layer has translated all of the assigned-address properties
329 * into physical address resources, we only have to figure out the register
332 static void pci_parse_of_addrs(struct of_device
*op
,
333 struct device_node
*node
,
336 struct resource
*op_res
;
340 addrs
= of_get_property(node
, "assigned-addresses", &proplen
);
344 printk(" parse addresses (%d bytes) @ %p\n",
346 op_res
= &op
->resource
[0];
347 for (; proplen
>= 20; proplen
-= 20, addrs
+= 5, op_res
++) {
348 struct resource
*res
;
352 flags
= pci_parse_of_flags(addrs
[0]);
357 printk(" start: %lx, end: %lx, i: %x\n",
358 op_res
->start
, op_res
->end
, i
);
360 if (PCI_BASE_ADDRESS_0
<= i
&& i
<= PCI_BASE_ADDRESS_5
) {
361 res
= &dev
->resource
[(i
- PCI_BASE_ADDRESS_0
) >> 2];
362 } else if (i
== dev
->rom_base_reg
) {
363 res
= &dev
->resource
[PCI_ROM_RESOURCE
];
364 flags
|= IORESOURCE_READONLY
| IORESOURCE_CACHEABLE
;
366 printk(KERN_ERR
"PCI: bad cfg reg num 0x%x\n", i
);
369 res
->start
= op_res
->start
;
370 res
->end
= op_res
->end
;
372 res
->name
= pci_name(dev
);
376 struct pci_dev
*of_create_pci_dev(struct pci_pbm_info
*pbm
,
377 struct device_node
*node
,
378 struct pci_bus
*bus
, int devfn
,
381 struct dev_archdata
*sd
;
386 dev
= alloc_pci_dev();
390 sd
= &dev
->dev
.archdata
;
391 sd
->iommu
= pbm
->iommu
;
393 sd
->host_controller
= pbm
;
394 sd
->prom_node
= node
;
395 sd
->op
= of_find_device_by_node(node
);
397 sd
= &sd
->op
->dev
.archdata
;
398 sd
->iommu
= pbm
->iommu
;
401 type
= of_get_property(node
, "device_type", NULL
);
406 printk(" create device, devfn: %x, type: %s\n",
411 dev
->dev
.parent
= bus
->bridge
;
412 dev
->dev
.bus
= &pci_bus_type
;
414 dev
->multifunction
= 0; /* maybe a lie? */
416 if (host_controller
) {
417 if (tlb_type
!= hypervisor
) {
418 pci_read_config_word(dev
, PCI_VENDOR_ID
,
420 pci_read_config_word(dev
, PCI_DEVICE_ID
,
423 dev
->vendor
= PCI_VENDOR_ID_SUN
;
424 dev
->device
= 0x80f0;
427 dev
->class = PCI_CLASS_BRIDGE_HOST
<< 8;
428 sprintf(pci_name(dev
), "%04x:%02x:%02x.%d", pci_domain_nr(bus
),
429 0x00, PCI_SLOT(devfn
), PCI_FUNC(devfn
));
431 dev
->vendor
= of_getintprop_default(node
, "vendor-id", 0xffff);
432 dev
->device
= of_getintprop_default(node
, "device-id", 0xffff);
433 dev
->subsystem_vendor
=
434 of_getintprop_default(node
, "subsystem-vendor-id", 0);
435 dev
->subsystem_device
=
436 of_getintprop_default(node
, "subsystem-id", 0);
438 dev
->cfg_size
= pci_cfg_space_size(dev
);
440 /* We can't actually use the firmware value, we have
441 * to read what is in the register right now. One
442 * reason is that in the case of IDE interfaces the
443 * firmware can sample the value before the the IDE
444 * interface is programmed into native mode.
446 pci_read_config_dword(dev
, PCI_CLASS_REVISION
, &class);
447 dev
->class = class >> 8;
448 dev
->revision
= class & 0xff;
450 sprintf(pci_name(dev
), "%04x:%02x:%02x.%d", pci_domain_nr(bus
),
451 dev
->bus
->number
, PCI_SLOT(devfn
), PCI_FUNC(devfn
));
454 printk(" class: 0x%x device name: %s\n",
455 dev
->class, pci_name(dev
));
457 /* I have seen IDE devices which will not respond to
458 * the bmdma simplex check reads if bus mastering is
461 if ((dev
->class >> 8) == PCI_CLASS_STORAGE_IDE
)
464 dev
->current_state
= 4; /* unknown power state */
465 dev
->error_state
= pci_channel_io_normal
;
467 if (host_controller
) {
468 dev
->hdr_type
= PCI_HEADER_TYPE_BRIDGE
;
469 dev
->rom_base_reg
= PCI_ROM_ADDRESS1
;
470 dev
->irq
= PCI_IRQ_NONE
;
472 if (!strcmp(type
, "pci") || !strcmp(type
, "pciex")) {
473 /* a PCI-PCI bridge */
474 dev
->hdr_type
= PCI_HEADER_TYPE_BRIDGE
;
475 dev
->rom_base_reg
= PCI_ROM_ADDRESS1
;
476 } else if (!strcmp(type
, "cardbus")) {
477 dev
->hdr_type
= PCI_HEADER_TYPE_CARDBUS
;
479 dev
->hdr_type
= PCI_HEADER_TYPE_NORMAL
;
480 dev
->rom_base_reg
= PCI_ROM_ADDRESS
;
482 dev
->irq
= sd
->op
->irqs
[0];
483 if (dev
->irq
== 0xffffffff)
484 dev
->irq
= PCI_IRQ_NONE
;
487 pci_parse_of_addrs(sd
->op
, node
, dev
);
490 printk(" adding to system ...\n");
492 pci_device_add(dev
, bus
);
497 static void __devinit
apb_calc_first_last(u8 map
, u32
*first_p
, u32
*last_p
)
499 u32 idx
, first
, last
;
503 for (idx
= 0; idx
< 8; idx
++) {
504 if ((map
& (1 << idx
)) != 0) {
516 static void pci_resource_adjust(struct resource
*res
,
517 struct resource
*root
)
519 res
->start
+= root
->start
;
520 res
->end
+= root
->start
;
523 /* For PCI bus devices which lack a 'ranges' property we interrogate
524 * the config space values to set the resources, just like the generic
525 * Linux PCI probing code does.
527 static void __devinit
pci_cfg_fake_ranges(struct pci_dev
*dev
,
529 struct pci_pbm_info
*pbm
)
531 struct resource
*res
;
532 u8 io_base_lo
, io_limit_lo
;
533 u16 mem_base_lo
, mem_limit_lo
;
534 unsigned long base
, limit
;
536 pci_read_config_byte(dev
, PCI_IO_BASE
, &io_base_lo
);
537 pci_read_config_byte(dev
, PCI_IO_LIMIT
, &io_limit_lo
);
538 base
= (io_base_lo
& PCI_IO_RANGE_MASK
) << 8;
539 limit
= (io_limit_lo
& PCI_IO_RANGE_MASK
) << 8;
541 if ((io_base_lo
& PCI_IO_RANGE_TYPE_MASK
) == PCI_IO_RANGE_TYPE_32
) {
542 u16 io_base_hi
, io_limit_hi
;
544 pci_read_config_word(dev
, PCI_IO_BASE_UPPER16
, &io_base_hi
);
545 pci_read_config_word(dev
, PCI_IO_LIMIT_UPPER16
, &io_limit_hi
);
546 base
|= (io_base_hi
<< 16);
547 limit
|= (io_limit_hi
<< 16);
550 res
= bus
->resource
[0];
552 res
->flags
= (io_base_lo
& PCI_IO_RANGE_TYPE_MASK
) | IORESOURCE_IO
;
556 res
->end
= limit
+ 0xfff;
557 pci_resource_adjust(res
, &pbm
->io_space
);
560 pci_read_config_word(dev
, PCI_MEMORY_BASE
, &mem_base_lo
);
561 pci_read_config_word(dev
, PCI_MEMORY_LIMIT
, &mem_limit_lo
);
562 base
= (mem_base_lo
& PCI_MEMORY_RANGE_MASK
) << 16;
563 limit
= (mem_limit_lo
& PCI_MEMORY_RANGE_MASK
) << 16;
565 res
= bus
->resource
[1];
567 res
->flags
= ((mem_base_lo
& PCI_MEMORY_RANGE_TYPE_MASK
) |
570 res
->end
= limit
+ 0xfffff;
571 pci_resource_adjust(res
, &pbm
->mem_space
);
574 pci_read_config_word(dev
, PCI_PREF_MEMORY_BASE
, &mem_base_lo
);
575 pci_read_config_word(dev
, PCI_PREF_MEMORY_LIMIT
, &mem_limit_lo
);
576 base
= (mem_base_lo
& PCI_PREF_RANGE_MASK
) << 16;
577 limit
= (mem_limit_lo
& PCI_PREF_RANGE_MASK
) << 16;
579 if ((mem_base_lo
& PCI_PREF_RANGE_TYPE_MASK
) == PCI_PREF_RANGE_TYPE_64
) {
580 u32 mem_base_hi
, mem_limit_hi
;
582 pci_read_config_dword(dev
, PCI_PREF_BASE_UPPER32
, &mem_base_hi
);
583 pci_read_config_dword(dev
, PCI_PREF_LIMIT_UPPER32
, &mem_limit_hi
);
586 * Some bridges set the base > limit by default, and some
587 * (broken) BIOSes do not initialize them. If we find
588 * this, just assume they are not being used.
590 if (mem_base_hi
<= mem_limit_hi
) {
591 base
|= ((long) mem_base_hi
) << 32;
592 limit
|= ((long) mem_limit_hi
) << 32;
596 res
= bus
->resource
[2];
598 res
->flags
= ((mem_base_lo
& PCI_MEMORY_RANGE_TYPE_MASK
) |
599 IORESOURCE_MEM
| IORESOURCE_PREFETCH
);
601 res
->end
= limit
+ 0xfffff;
602 pci_resource_adjust(res
, &pbm
->mem_space
);
606 /* Cook up fake bus resources for SUNW,simba PCI bridges which lack
607 * a proper 'ranges' property.
609 static void __devinit
apb_fake_ranges(struct pci_dev
*dev
,
611 struct pci_pbm_info
*pbm
)
613 struct resource
*res
;
617 pci_read_config_byte(dev
, APB_IO_ADDRESS_MAP
, &map
);
618 apb_calc_first_last(map
, &first
, &last
);
619 res
= bus
->resource
[0];
620 res
->start
= (first
<< 21);
621 res
->end
= (last
<< 21) + ((1 << 21) - 1);
622 res
->flags
= IORESOURCE_IO
;
623 pci_resource_adjust(res
, &pbm
->io_space
);
625 pci_read_config_byte(dev
, APB_MEM_ADDRESS_MAP
, &map
);
626 apb_calc_first_last(map
, &first
, &last
);
627 res
= bus
->resource
[1];
628 res
->start
= (first
<< 21);
629 res
->end
= (last
<< 21) + ((1 << 21) - 1);
630 res
->flags
= IORESOURCE_MEM
;
631 pci_resource_adjust(res
, &pbm
->mem_space
);
634 static void __devinit
pci_of_scan_bus(struct pci_pbm_info
*pbm
,
635 struct device_node
*node
,
636 struct pci_bus
*bus
);
638 #define GET_64BIT(prop, i) ((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1])
640 static void __devinit
of_scan_pci_bridge(struct pci_pbm_info
*pbm
,
641 struct device_node
*node
,
645 const u32
*busrange
, *ranges
;
647 struct resource
*res
;
652 printk("of_scan_pci_bridge(%s)\n", node
->full_name
);
654 /* parse bus-range property */
655 busrange
= of_get_property(node
, "bus-range", &len
);
656 if (busrange
== NULL
|| len
!= 8) {
657 printk(KERN_DEBUG
"Can't get bus-range for PCI-PCI bridge %s\n",
661 ranges
= of_get_property(node
, "ranges", &len
);
663 if (ranges
== NULL
) {
664 const char *model
= of_get_property(node
, "model", NULL
);
665 if (model
&& !strcmp(model
, "SUNW,simba"))
669 bus
= pci_add_new_bus(dev
->bus
, dev
, busrange
[0]);
671 printk(KERN_ERR
"Failed to create pci bus for %s\n",
676 bus
->primary
= dev
->bus
->number
;
677 bus
->subordinate
= busrange
[1];
680 /* parse ranges property, or cook one up by hand for Simba */
681 /* PCI #address-cells == 3 and #size-cells == 2 always */
682 res
= &dev
->resource
[PCI_BRIDGE_RESOURCES
];
683 for (i
= 0; i
< PCI_NUM_RESOURCES
- PCI_BRIDGE_RESOURCES
; ++i
) {
685 bus
->resource
[i
] = res
;
689 apb_fake_ranges(dev
, bus
, pbm
);
691 } else if (ranges
== NULL
) {
692 pci_cfg_fake_ranges(dev
, bus
, pbm
);
696 for (; len
>= 32; len
-= 32, ranges
+= 8) {
697 struct resource
*root
;
699 flags
= pci_parse_of_flags(ranges
[0]);
700 size
= GET_64BIT(ranges
, 6);
701 if (flags
== 0 || size
== 0)
703 if (flags
& IORESOURCE_IO
) {
704 res
= bus
->resource
[0];
706 printk(KERN_ERR
"PCI: ignoring extra I/O range"
707 " for bridge %s\n", node
->full_name
);
710 root
= &pbm
->io_space
;
712 if (i
>= PCI_NUM_RESOURCES
- PCI_BRIDGE_RESOURCES
) {
713 printk(KERN_ERR
"PCI: too many memory ranges"
714 " for bridge %s\n", node
->full_name
);
717 res
= bus
->resource
[i
];
719 root
= &pbm
->mem_space
;
722 res
->start
= GET_64BIT(ranges
, 1);
723 res
->end
= res
->start
+ size
- 1;
726 /* Another way to implement this would be to add an of_device
727 * layer routine that can calculate a resource for a given
728 * range property value in a PCI device.
730 pci_resource_adjust(res
, root
);
733 sprintf(bus
->name
, "PCI Bus %04x:%02x", pci_domain_nr(bus
),
736 printk(" bus name: %s\n", bus
->name
);
738 pci_of_scan_bus(pbm
, node
, bus
);
741 static void __devinit
pci_of_scan_bus(struct pci_pbm_info
*pbm
,
742 struct device_node
*node
,
745 struct device_node
*child
;
747 int reglen
, devfn
, prev_devfn
;
751 printk("PCI: scan_bus[%s] bus no %d\n",
752 node
->full_name
, bus
->number
);
756 while ((child
= of_get_next_child(node
, child
)) != NULL
) {
758 printk(" * %s\n", child
->full_name
);
759 reg
= of_get_property(child
, "reg", ®len
);
760 if (reg
== NULL
|| reglen
< 20)
763 devfn
= (reg
[0] >> 8) & 0xff;
765 /* This is a workaround for some device trees
766 * which list PCI devices twice. On the V100
767 * for example, device number 3 is listed twice.
768 * Once as "pm" and once again as "lomp".
770 if (devfn
== prev_devfn
)
774 /* create a new pci_dev for this device */
775 dev
= of_create_pci_dev(pbm
, child
, bus
, devfn
, 0);
779 printk("PCI: dev header type: %x\n",
782 if (dev
->hdr_type
== PCI_HEADER_TYPE_BRIDGE
||
783 dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
)
784 of_scan_pci_bridge(pbm
, child
, dev
);
789 show_pciobppath_attr(struct device
* dev
, struct device_attribute
* attr
, char * buf
)
791 struct pci_dev
*pdev
;
792 struct device_node
*dp
;
794 pdev
= to_pci_dev(dev
);
795 dp
= pdev
->dev
.archdata
.prom_node
;
797 return snprintf (buf
, PAGE_SIZE
, "%s\n", dp
->full_name
);
800 static DEVICE_ATTR(obppath
, S_IRUSR
| S_IRGRP
| S_IROTH
, show_pciobppath_attr
, NULL
);
802 static void __devinit
pci_bus_register_of_sysfs(struct pci_bus
*bus
)
805 struct pci_bus
*child_bus
;
808 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
809 /* we don't really care if we can create this file or
810 * not, but we need to assign the result of the call
811 * or the world will fall under alien invasion and
812 * everybody will be frozen on a spaceship ready to be
813 * eaten on alpha centauri by some green and jelly
816 err
= sysfs_create_file(&dev
->dev
.kobj
, &dev_attr_obppath
.attr
);
818 list_for_each_entry(child_bus
, &bus
->children
, node
)
819 pci_bus_register_of_sysfs(child_bus
);
822 int pci_host_bridge_read_pci_cfg(struct pci_bus
*bus_dev
,
827 static u8 fake_pci_config
[] = {
828 0x8e, 0x10, /* Vendor: 0x108e (Sun) */
829 0xf0, 0x80, /* Device: 0x80f0 (Fire) */
830 0x46, 0x01, /* Command: 0x0146 (SERR, PARITY, MASTER, MEM) */
831 0xa0, 0x22, /* Status: 0x02a0 (DEVSEL_MED, FB2B, 66MHZ) */
832 0x00, 0x00, 0x00, 0x06, /* Class: 0x06000000 host bridge */
833 0x00, /* Cacheline: 0x00 */
834 0x40, /* Latency: 0x40 */
835 0x00, /* Header-Type: 0x00 normal */
839 if (where
>= 0 && where
< sizeof(fake_pci_config
) &&
840 (where
+ size
) >= 0 &&
841 (where
+ size
) < sizeof(fake_pci_config
) &&
842 size
<= sizeof(u32
)) {
845 *value
|= fake_pci_config
[where
+ size
];
849 return PCIBIOS_SUCCESSFUL
;
852 int pci_host_bridge_write_pci_cfg(struct pci_bus
*bus_dev
,
857 return PCIBIOS_SUCCESSFUL
;
860 struct pci_bus
* __devinit
pci_scan_one_pbm(struct pci_pbm_info
*pbm
)
862 struct device_node
*node
= pbm
->prom_node
;
863 struct pci_dev
*host_pdev
;
866 printk("PCI: Scanning PBM %s\n", node
->full_name
);
868 /* XXX parent device? XXX */
869 bus
= pci_create_bus(NULL
, pbm
->pci_first_busno
, pbm
->pci_ops
, pbm
);
871 printk(KERN_ERR
"Failed to create bus for %s\n",
875 bus
->secondary
= pbm
->pci_first_busno
;
876 bus
->subordinate
= pbm
->pci_last_busno
;
878 bus
->resource
[0] = &pbm
->io_space
;
879 bus
->resource
[1] = &pbm
->mem_space
;
881 /* Create the dummy host bridge and link it in. */
882 host_pdev
= of_create_pci_dev(pbm
, node
, bus
, 0x00, 1);
883 bus
->self
= host_pdev
;
885 pci_of_scan_bus(pbm
, node
, bus
);
886 pci_bus_add_devices(bus
);
887 pci_bus_register_of_sysfs(bus
);
892 static void __init
pci_scan_each_controller_bus(void)
894 struct pci_pbm_info
*pbm
;
896 for (pbm
= pci_pbm_root
; pbm
; pbm
= pbm
->next
)
900 extern void power_init(void);
902 static int __init
pcibios_init(void)
904 pci_controller_probe();
905 if (pci_pbm_root
== NULL
)
908 pci_scan_each_controller_bus();
917 subsys_initcall(pcibios_init
);
919 void __devinit
pcibios_fixup_bus(struct pci_bus
*pbus
)
921 struct pci_pbm_info
*pbm
= pbus
->sysdata
;
923 /* Generic PCI bus probing sets these to point at
924 * &io{port,mem}_resouce which is wrong for us.
926 pbus
->resource
[0] = &pbm
->io_space
;
927 pbus
->resource
[1] = &pbm
->mem_space
;
930 struct resource
*pcibios_select_root(struct pci_dev
*pdev
, struct resource
*r
)
932 struct pci_pbm_info
*pbm
= pdev
->bus
->sysdata
;
933 struct resource
*root
= NULL
;
935 if (r
->flags
& IORESOURCE_IO
)
936 root
= &pbm
->io_space
;
937 if (r
->flags
& IORESOURCE_MEM
)
938 root
= &pbm
->mem_space
;
943 void pcibios_update_irq(struct pci_dev
*pdev
, int irq
)
947 void pcibios_align_resource(void *data
, struct resource
*res
,
948 resource_size_t size
, resource_size_t align
)
952 int pcibios_enable_device(struct pci_dev
*dev
, int mask
)
957 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
960 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
961 struct resource
*res
= &dev
->resource
[i
];
963 /* Only set up the requested stuff */
964 if (!(mask
& (1<<i
)))
967 if (res
->flags
& IORESOURCE_IO
)
968 cmd
|= PCI_COMMAND_IO
;
969 if (res
->flags
& IORESOURCE_MEM
)
970 cmd
|= PCI_COMMAND_MEMORY
;
974 printk(KERN_DEBUG
"PCI: Enabling device: (%s), cmd %x\n",
976 /* Enable the appropriate bits in the PCI command register. */
977 pci_write_config_word(dev
, PCI_COMMAND
, cmd
);
982 void pcibios_resource_to_bus(struct pci_dev
*pdev
, struct pci_bus_region
*region
,
983 struct resource
*res
)
985 struct pci_pbm_info
*pbm
= pdev
->bus
->sysdata
;
986 struct resource zero_res
, *root
;
990 zero_res
.flags
= res
->flags
;
992 if (res
->flags
& IORESOURCE_IO
)
993 root
= &pbm
->io_space
;
995 root
= &pbm
->mem_space
;
997 pci_resource_adjust(&zero_res
, root
);
999 region
->start
= res
->start
- zero_res
.start
;
1000 region
->end
= res
->end
- zero_res
.start
;
1002 EXPORT_SYMBOL(pcibios_resource_to_bus
);
1004 void pcibios_bus_to_resource(struct pci_dev
*pdev
, struct resource
*res
,
1005 struct pci_bus_region
*region
)
1007 struct pci_pbm_info
*pbm
= pdev
->bus
->sysdata
;
1008 struct resource
*root
;
1010 res
->start
= region
->start
;
1011 res
->end
= region
->end
;
1013 if (res
->flags
& IORESOURCE_IO
)
1014 root
= &pbm
->io_space
;
1016 root
= &pbm
->mem_space
;
1018 pci_resource_adjust(res
, root
);
1020 EXPORT_SYMBOL(pcibios_bus_to_resource
);
1022 char * __devinit
pcibios_setup(char *str
)
1027 /* Platform support for /proc/bus/pci/X/Y mmap()s. */
1029 /* If the user uses a host-bridge as the PCI device, he may use
1030 * this to perform a raw mmap() of the I/O or MEM space behind
1033 * This can be useful for execution of x86 PCI bios initialization code
1034 * on a PCI card, like the xfree86 int10 stuff does.
1036 static int __pci_mmap_make_offset_bus(struct pci_dev
*pdev
, struct vm_area_struct
*vma
,
1037 enum pci_mmap_state mmap_state
)
1039 struct pci_pbm_info
*pbm
= pdev
->dev
.archdata
.host_controller
;
1040 unsigned long space_size
, user_offset
, user_size
;
1042 if (mmap_state
== pci_mmap_io
) {
1043 space_size
= (pbm
->io_space
.end
-
1044 pbm
->io_space
.start
) + 1;
1046 space_size
= (pbm
->mem_space
.end
-
1047 pbm
->mem_space
.start
) + 1;
1050 /* Make sure the request is in range. */
1051 user_offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
1052 user_size
= vma
->vm_end
- vma
->vm_start
;
1054 if (user_offset
>= space_size
||
1055 (user_offset
+ user_size
) > space_size
)
1058 if (mmap_state
== pci_mmap_io
) {
1059 vma
->vm_pgoff
= (pbm
->io_space
.start
+
1060 user_offset
) >> PAGE_SHIFT
;
1062 vma
->vm_pgoff
= (pbm
->mem_space
.start
+
1063 user_offset
) >> PAGE_SHIFT
;
1069 /* Adjust vm_pgoff of VMA such that it is the physical page offset corresponding
1070 * to the 32-bit pci bus offset for DEV requested by the user.
1072 * Basically, the user finds the base address for his device which he wishes
1073 * to mmap. They read the 32-bit value from the config space base register,
1074 * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
1075 * offset parameter of mmap on /proc/bus/pci/XXX for that device.
1077 * Returns negative error code on failure, zero on success.
1079 static int __pci_mmap_make_offset(struct pci_dev
*dev
, struct vm_area_struct
*vma
,
1080 enum pci_mmap_state mmap_state
)
1082 unsigned long user_offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
1083 unsigned long user32
= user_offset
& pci_memspace_mask
;
1084 unsigned long largest_base
, this_base
, addr32
;
1087 if ((dev
->class >> 8) == PCI_CLASS_BRIDGE_HOST
)
1088 return __pci_mmap_make_offset_bus(dev
, vma
, mmap_state
);
1090 /* Figure out which base address this is for. */
1092 for (i
= 0; i
<= PCI_ROM_RESOURCE
; i
++) {
1093 struct resource
*rp
= &dev
->resource
[i
];
1100 if (i
== PCI_ROM_RESOURCE
) {
1101 if (mmap_state
!= pci_mmap_mem
)
1104 if ((mmap_state
== pci_mmap_io
&&
1105 (rp
->flags
& IORESOURCE_IO
) == 0) ||
1106 (mmap_state
== pci_mmap_mem
&&
1107 (rp
->flags
& IORESOURCE_MEM
) == 0))
1111 this_base
= rp
->start
;
1113 addr32
= (this_base
& PAGE_MASK
) & pci_memspace_mask
;
1115 if (mmap_state
== pci_mmap_io
)
1118 if (addr32
<= user32
&& this_base
> largest_base
)
1119 largest_base
= this_base
;
1122 if (largest_base
== 0UL)
1125 /* Now construct the final physical address. */
1126 if (mmap_state
== pci_mmap_io
)
1127 vma
->vm_pgoff
= (((largest_base
& ~0xffffffUL
) | user32
) >> PAGE_SHIFT
);
1129 vma
->vm_pgoff
= (((largest_base
& ~(pci_memspace_mask
)) | user32
) >> PAGE_SHIFT
);
1134 /* Set vm_flags of VMA, as appropriate for this architecture, for a pci device
1137 static void __pci_mmap_set_flags(struct pci_dev
*dev
, struct vm_area_struct
*vma
,
1138 enum pci_mmap_state mmap_state
)
1140 vma
->vm_flags
|= (VM_IO
| VM_RESERVED
);
1143 /* Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
1146 static void __pci_mmap_set_pgprot(struct pci_dev
*dev
, struct vm_area_struct
*vma
,
1147 enum pci_mmap_state mmap_state
)
1149 /* Our io_remap_pfn_range takes care of this, do nothing. */
1152 /* Perform the actual remap of the pages for a PCI device mapping, as appropriate
1153 * for this architecture. The region in the process to map is described by vm_start
1154 * and vm_end members of VMA, the base physical address is found in vm_pgoff.
1155 * The pci device structure is provided so that architectures may make mapping
1156 * decisions on a per-device or per-bus basis.
1158 * Returns a negative error code on failure, zero on success.
1160 int pci_mmap_page_range(struct pci_dev
*dev
, struct vm_area_struct
*vma
,
1161 enum pci_mmap_state mmap_state
,
1166 ret
= __pci_mmap_make_offset(dev
, vma
, mmap_state
);
1170 __pci_mmap_set_flags(dev
, vma
, mmap_state
);
1171 __pci_mmap_set_pgprot(dev
, vma
, mmap_state
);
1173 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
1174 ret
= io_remap_pfn_range(vma
, vma
->vm_start
,
1176 vma
->vm_end
- vma
->vm_start
,
1184 /* Return the domain nuber for this pci bus */
1186 int pci_domain_nr(struct pci_bus
*pbus
)
1188 struct pci_pbm_info
*pbm
= pbus
->sysdata
;
1191 if (pbm
== NULL
|| pbm
->parent
== NULL
) {
1199 EXPORT_SYMBOL(pci_domain_nr
);
1201 #ifdef CONFIG_PCI_MSI
1202 int arch_setup_msi_irq(struct pci_dev
*pdev
, struct msi_desc
*desc
)
1204 struct pci_pbm_info
*pbm
= pdev
->dev
.archdata
.host_controller
;
1207 if (!pbm
->setup_msi_irq
)
1210 return pbm
->setup_msi_irq(&virt_irq
, pdev
, desc
);
1213 void arch_teardown_msi_irq(unsigned int virt_irq
)
1215 struct msi_desc
*entry
= get_irq_msi(virt_irq
);
1216 struct pci_dev
*pdev
= entry
->dev
;
1217 struct pci_pbm_info
*pbm
= pdev
->dev
.archdata
.host_controller
;
1219 if (!pbm
->teardown_msi_irq
)
1222 return pbm
->teardown_msi_irq(virt_irq
, pdev
);
1224 #endif /* !(CONFIG_PCI_MSI) */
1226 struct device_node
*pci_device_to_OF_node(struct pci_dev
*pdev
)
1228 return pdev
->dev
.archdata
.prom_node
;
1230 EXPORT_SYMBOL(pci_device_to_OF_node
);
1232 static void ali_sound_dma_hack(struct pci_dev
*pdev
, int set_bit
)
1234 struct pci_dev
*ali_isa_bridge
;
1237 /* ALI sound chips generate 31-bits of DMA, a special register
1238 * determines what bit 31 is emitted as.
1240 ali_isa_bridge
= pci_get_device(PCI_VENDOR_ID_AL
,
1241 PCI_DEVICE_ID_AL_M1533
,
1244 pci_read_config_byte(ali_isa_bridge
, 0x7e, &val
);
1249 pci_write_config_byte(ali_isa_bridge
, 0x7e, val
);
1250 pci_dev_put(ali_isa_bridge
);
1253 int pci_dma_supported(struct pci_dev
*pdev
, u64 device_mask
)
1258 dma_addr_mask
= 0xffffffff;
1260 struct iommu
*iommu
= pdev
->dev
.archdata
.iommu
;
1262 dma_addr_mask
= iommu
->dma_addr_mask
;
1264 if (pdev
->vendor
== PCI_VENDOR_ID_AL
&&
1265 pdev
->device
== PCI_DEVICE_ID_AL_M5451
&&
1266 device_mask
== 0x7fffffff) {
1267 ali_sound_dma_hack(pdev
,
1268 (dma_addr_mask
& 0x80000000) != 0);
1273 if (device_mask
>= (1UL << 32UL))
1276 return (device_mask
& dma_addr_mask
) == dma_addr_mask
;
1279 #endif /* !(CONFIG_PCI) */