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 (sizeof(pci_controller_table) / \
211 sizeof(pci_controller_table[0]))
213 static int __init
pci_controller_init(const char *model_name
, int namelen
, struct device_node
*dp
)
217 for (i
= 0; i
< PCI_NUM_CONTROLLER_TYPES
; i
++) {
218 if (!strncmp(model_name
,
219 pci_controller_table
[i
].model_name
,
221 pci_controller_table
[i
].init(dp
, model_name
);
229 static int __init
pci_is_controller(const char *model_name
, int namelen
, struct device_node
*dp
)
233 for (i
= 0; i
< PCI_NUM_CONTROLLER_TYPES
; i
++) {
234 if (!strncmp(model_name
,
235 pci_controller_table
[i
].model_name
,
243 static int __init
pci_controller_scan(int (*handler
)(const char *, int, struct device_node
*))
245 struct device_node
*dp
;
248 for_each_node_by_name(dp
, "pci") {
249 struct property
*prop
;
252 prop
= of_find_property(dp
, "model", &len
);
254 prop
= of_find_property(dp
, "compatible", &len
);
257 const char *model
= prop
->value
;
260 /* Our value may be a multi-valued string in the
261 * case of some compatible properties. For sanity,
262 * only try the first one.
264 while (model
[item_len
] && len
) {
269 if (handler(model
, item_len
, dp
))
278 /* Is there some PCI controller in the system? */
279 int __init
pcic_present(void)
281 return pci_controller_scan(pci_is_controller
);
284 /* Find each controller in the system, attach and initialize
285 * software state structure for each and link into the
286 * pci_pbm_root. Setup the controller enough such
287 * that bus scanning can be done.
289 static void __init
pci_controller_probe(void)
291 printk("PCI: Probing for controllers.\n");
293 pci_controller_scan(pci_controller_init
);
296 static int ofpci_verbose
;
298 static int __init
ofpci_debug(char *str
)
302 get_option(&str
, &val
);
308 __setup("ofpci_debug=", ofpci_debug
);
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
);
342 printk(" parse addresses (%d bytes) @ %p\n",
344 op_res
= &op
->resource
[0];
345 for (; proplen
>= 20; proplen
-= 20, addrs
+= 5, op_res
++) {
346 struct resource
*res
;
350 flags
= pci_parse_of_flags(addrs
[0]);
355 printk(" start: %lx, end: %lx, i: %x\n",
356 op_res
->start
, op_res
->end
, i
);
358 if (PCI_BASE_ADDRESS_0
<= i
&& i
<= PCI_BASE_ADDRESS_5
) {
359 res
= &dev
->resource
[(i
- PCI_BASE_ADDRESS_0
) >> 2];
360 } else if (i
== dev
->rom_base_reg
) {
361 res
= &dev
->resource
[PCI_ROM_RESOURCE
];
362 flags
|= IORESOURCE_READONLY
| IORESOURCE_CACHEABLE
;
364 printk(KERN_ERR
"PCI: bad cfg reg num 0x%x\n", i
);
367 res
->start
= op_res
->start
;
368 res
->end
= op_res
->end
;
370 res
->name
= pci_name(dev
);
374 struct pci_dev
*of_create_pci_dev(struct pci_pbm_info
*pbm
,
375 struct device_node
*node
,
376 struct pci_bus
*bus
, int devfn
,
379 struct dev_archdata
*sd
;
384 dev
= alloc_pci_dev();
388 sd
= &dev
->dev
.archdata
;
389 sd
->iommu
= pbm
->iommu
;
391 sd
->host_controller
= pbm
;
392 sd
->prom_node
= node
;
393 sd
->op
= of_find_device_by_node(node
);
395 sd
= &sd
->op
->dev
.archdata
;
396 sd
->iommu
= pbm
->iommu
;
399 type
= of_get_property(node
, "device_type", NULL
);
404 printk(" create device, devfn: %x, type: %s\n",
409 dev
->dev
.parent
= bus
->bridge
;
410 dev
->dev
.bus
= &pci_bus_type
;
412 dev
->multifunction
= 0; /* maybe a lie? */
414 if (host_controller
) {
415 if (tlb_type
!= hypervisor
) {
416 pci_read_config_word(dev
, PCI_VENDOR_ID
,
418 pci_read_config_word(dev
, PCI_DEVICE_ID
,
421 dev
->vendor
= PCI_VENDOR_ID_SUN
;
422 dev
->device
= 0x80f0;
425 dev
->class = PCI_CLASS_BRIDGE_HOST
<< 8;
426 sprintf(pci_name(dev
), "%04x:%02x:%02x.%d", pci_domain_nr(bus
),
427 0x00, PCI_SLOT(devfn
), PCI_FUNC(devfn
));
429 dev
->vendor
= of_getintprop_default(node
, "vendor-id", 0xffff);
430 dev
->device
= of_getintprop_default(node
, "device-id", 0xffff);
431 dev
->subsystem_vendor
=
432 of_getintprop_default(node
, "subsystem-vendor-id", 0);
433 dev
->subsystem_device
=
434 of_getintprop_default(node
, "subsystem-id", 0);
436 dev
->cfg_size
= pci_cfg_space_size(dev
);
438 /* We can't actually use the firmware value, we have
439 * to read what is in the register right now. One
440 * reason is that in the case of IDE interfaces the
441 * firmware can sample the value before the the IDE
442 * interface is programmed into native mode.
444 pci_read_config_dword(dev
, PCI_CLASS_REVISION
, &class);
445 dev
->class = class >> 8;
446 dev
->revision
= class & 0xff;
448 sprintf(pci_name(dev
), "%04x:%02x:%02x.%d", pci_domain_nr(bus
),
449 dev
->bus
->number
, PCI_SLOT(devfn
), PCI_FUNC(devfn
));
452 printk(" class: 0x%x device name: %s\n",
453 dev
->class, pci_name(dev
));
455 /* I have seen IDE devices which will not respond to
456 * the bmdma simplex check reads if bus mastering is
459 if ((dev
->class >> 8) == PCI_CLASS_STORAGE_IDE
)
462 dev
->current_state
= 4; /* unknown power state */
463 dev
->error_state
= pci_channel_io_normal
;
465 if (host_controller
) {
466 dev
->hdr_type
= PCI_HEADER_TYPE_BRIDGE
;
467 dev
->rom_base_reg
= PCI_ROM_ADDRESS1
;
468 dev
->irq
= PCI_IRQ_NONE
;
470 if (!strcmp(type
, "pci") || !strcmp(type
, "pciex")) {
471 /* a PCI-PCI bridge */
472 dev
->hdr_type
= PCI_HEADER_TYPE_BRIDGE
;
473 dev
->rom_base_reg
= PCI_ROM_ADDRESS1
;
474 } else if (!strcmp(type
, "cardbus")) {
475 dev
->hdr_type
= PCI_HEADER_TYPE_CARDBUS
;
477 dev
->hdr_type
= PCI_HEADER_TYPE_NORMAL
;
478 dev
->rom_base_reg
= PCI_ROM_ADDRESS
;
480 dev
->irq
= sd
->op
->irqs
[0];
481 if (dev
->irq
== 0xffffffff)
482 dev
->irq
= PCI_IRQ_NONE
;
485 pci_parse_of_addrs(sd
->op
, node
, dev
);
488 printk(" adding to system ...\n");
490 pci_device_add(dev
, bus
);
495 static void __devinit
apb_calc_first_last(u8 map
, u32
*first_p
, u32
*last_p
)
497 u32 idx
, first
, last
;
501 for (idx
= 0; idx
< 8; idx
++) {
502 if ((map
& (1 << idx
)) != 0) {
514 static void pci_resource_adjust(struct resource
*res
,
515 struct resource
*root
)
517 res
->start
+= root
->start
;
518 res
->end
+= root
->start
;
521 /* For PCI bus devices which lack a 'ranges' property we interrogate
522 * the config space values to set the resources, just like the generic
523 * Linux PCI probing code does.
525 static void __devinit
pci_cfg_fake_ranges(struct pci_dev
*dev
,
527 struct pci_pbm_info
*pbm
)
529 struct resource
*res
;
530 u8 io_base_lo
, io_limit_lo
;
531 u16 mem_base_lo
, mem_limit_lo
;
532 unsigned long base
, limit
;
534 pci_read_config_byte(dev
, PCI_IO_BASE
, &io_base_lo
);
535 pci_read_config_byte(dev
, PCI_IO_LIMIT
, &io_limit_lo
);
536 base
= (io_base_lo
& PCI_IO_RANGE_MASK
) << 8;
537 limit
= (io_limit_lo
& PCI_IO_RANGE_MASK
) << 8;
539 if ((io_base_lo
& PCI_IO_RANGE_TYPE_MASK
) == PCI_IO_RANGE_TYPE_32
) {
540 u16 io_base_hi
, io_limit_hi
;
542 pci_read_config_word(dev
, PCI_IO_BASE_UPPER16
, &io_base_hi
);
543 pci_read_config_word(dev
, PCI_IO_LIMIT_UPPER16
, &io_limit_hi
);
544 base
|= (io_base_hi
<< 16);
545 limit
|= (io_limit_hi
<< 16);
548 res
= bus
->resource
[0];
550 res
->flags
= (io_base_lo
& PCI_IO_RANGE_TYPE_MASK
) | IORESOURCE_IO
;
554 res
->end
= limit
+ 0xfff;
555 pci_resource_adjust(res
, &pbm
->io_space
);
558 pci_read_config_word(dev
, PCI_MEMORY_BASE
, &mem_base_lo
);
559 pci_read_config_word(dev
, PCI_MEMORY_LIMIT
, &mem_limit_lo
);
560 base
= (mem_base_lo
& PCI_MEMORY_RANGE_MASK
) << 16;
561 limit
= (mem_limit_lo
& PCI_MEMORY_RANGE_MASK
) << 16;
563 res
= bus
->resource
[1];
565 res
->flags
= ((mem_base_lo
& PCI_MEMORY_RANGE_TYPE_MASK
) |
568 res
->end
= limit
+ 0xfffff;
569 pci_resource_adjust(res
, &pbm
->mem_space
);
572 pci_read_config_word(dev
, PCI_PREF_MEMORY_BASE
, &mem_base_lo
);
573 pci_read_config_word(dev
, PCI_PREF_MEMORY_LIMIT
, &mem_limit_lo
);
574 base
= (mem_base_lo
& PCI_PREF_RANGE_MASK
) << 16;
575 limit
= (mem_limit_lo
& PCI_PREF_RANGE_MASK
) << 16;
577 if ((mem_base_lo
& PCI_PREF_RANGE_TYPE_MASK
) == PCI_PREF_RANGE_TYPE_64
) {
578 u32 mem_base_hi
, mem_limit_hi
;
580 pci_read_config_dword(dev
, PCI_PREF_BASE_UPPER32
, &mem_base_hi
);
581 pci_read_config_dword(dev
, PCI_PREF_LIMIT_UPPER32
, &mem_limit_hi
);
584 * Some bridges set the base > limit by default, and some
585 * (broken) BIOSes do not initialize them. If we find
586 * this, just assume they are not being used.
588 if (mem_base_hi
<= mem_limit_hi
) {
589 base
|= ((long) mem_base_hi
) << 32;
590 limit
|= ((long) mem_limit_hi
) << 32;
594 res
= bus
->resource
[2];
596 res
->flags
= ((mem_base_lo
& PCI_MEMORY_RANGE_TYPE_MASK
) |
597 IORESOURCE_MEM
| IORESOURCE_PREFETCH
);
599 res
->end
= limit
+ 0xfffff;
600 pci_resource_adjust(res
, &pbm
->mem_space
);
604 /* Cook up fake bus resources for SUNW,simba PCI bridges which lack
605 * a proper 'ranges' property.
607 static void __devinit
apb_fake_ranges(struct pci_dev
*dev
,
609 struct pci_pbm_info
*pbm
)
611 struct resource
*res
;
615 pci_read_config_byte(dev
, APB_IO_ADDRESS_MAP
, &map
);
616 apb_calc_first_last(map
, &first
, &last
);
617 res
= bus
->resource
[0];
618 res
->start
= (first
<< 21);
619 res
->end
= (last
<< 21) + ((1 << 21) - 1);
620 res
->flags
= IORESOURCE_IO
;
621 pci_resource_adjust(res
, &pbm
->io_space
);
623 pci_read_config_byte(dev
, APB_MEM_ADDRESS_MAP
, &map
);
624 apb_calc_first_last(map
, &first
, &last
);
625 res
= bus
->resource
[1];
626 res
->start
= (first
<< 21);
627 res
->end
= (last
<< 21) + ((1 << 21) - 1);
628 res
->flags
= IORESOURCE_MEM
;
629 pci_resource_adjust(res
, &pbm
->mem_space
);
632 static void __devinit
pci_of_scan_bus(struct pci_pbm_info
*pbm
,
633 struct device_node
*node
,
634 struct pci_bus
*bus
);
636 #define GET_64BIT(prop, i) ((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1])
638 static void __devinit
of_scan_pci_bridge(struct pci_pbm_info
*pbm
,
639 struct device_node
*node
,
643 const u32
*busrange
, *ranges
;
645 struct resource
*res
;
650 printk("of_scan_pci_bridge(%s)\n", node
->full_name
);
652 /* parse bus-range property */
653 busrange
= of_get_property(node
, "bus-range", &len
);
654 if (busrange
== NULL
|| len
!= 8) {
655 printk(KERN_DEBUG
"Can't get bus-range for PCI-PCI bridge %s\n",
659 ranges
= of_get_property(node
, "ranges", &len
);
661 if (ranges
== NULL
) {
662 const char *model
= of_get_property(node
, "model", NULL
);
663 if (model
&& !strcmp(model
, "SUNW,simba"))
667 bus
= pci_add_new_bus(dev
->bus
, dev
, busrange
[0]);
669 printk(KERN_ERR
"Failed to create pci bus for %s\n",
674 bus
->primary
= dev
->bus
->number
;
675 bus
->subordinate
= busrange
[1];
678 /* parse ranges property, or cook one up by hand for Simba */
679 /* PCI #address-cells == 3 and #size-cells == 2 always */
680 res
= &dev
->resource
[PCI_BRIDGE_RESOURCES
];
681 for (i
= 0; i
< PCI_NUM_RESOURCES
- PCI_BRIDGE_RESOURCES
; ++i
) {
683 bus
->resource
[i
] = res
;
687 apb_fake_ranges(dev
, bus
, pbm
);
689 } else if (ranges
== NULL
) {
690 pci_cfg_fake_ranges(dev
, bus
, pbm
);
694 for (; len
>= 32; len
-= 32, ranges
+= 8) {
695 struct resource
*root
;
697 flags
= pci_parse_of_flags(ranges
[0]);
698 size
= GET_64BIT(ranges
, 6);
699 if (flags
== 0 || size
== 0)
701 if (flags
& IORESOURCE_IO
) {
702 res
= bus
->resource
[0];
704 printk(KERN_ERR
"PCI: ignoring extra I/O range"
705 " for bridge %s\n", node
->full_name
);
708 root
= &pbm
->io_space
;
710 if (i
>= PCI_NUM_RESOURCES
- PCI_BRIDGE_RESOURCES
) {
711 printk(KERN_ERR
"PCI: too many memory ranges"
712 " for bridge %s\n", node
->full_name
);
715 res
= bus
->resource
[i
];
717 root
= &pbm
->mem_space
;
720 res
->start
= GET_64BIT(ranges
, 1);
721 res
->end
= res
->start
+ size
- 1;
724 /* Another way to implement this would be to add an of_device
725 * layer routine that can calculate a resource for a given
726 * range property value in a PCI device.
728 pci_resource_adjust(res
, root
);
731 sprintf(bus
->name
, "PCI Bus %04x:%02x", pci_domain_nr(bus
),
734 printk(" bus name: %s\n", bus
->name
);
736 pci_of_scan_bus(pbm
, node
, bus
);
739 static void __devinit
pci_of_scan_bus(struct pci_pbm_info
*pbm
,
740 struct device_node
*node
,
743 struct device_node
*child
;
745 int reglen
, devfn
, prev_devfn
;
749 printk("PCI: scan_bus[%s] bus no %d\n",
750 node
->full_name
, bus
->number
);
754 while ((child
= of_get_next_child(node
, child
)) != NULL
) {
756 printk(" * %s\n", child
->full_name
);
757 reg
= of_get_property(child
, "reg", ®len
);
758 if (reg
== NULL
|| reglen
< 20)
761 devfn
= (reg
[0] >> 8) & 0xff;
763 /* This is a workaround for some device trees
764 * which list PCI devices twice. On the V100
765 * for example, device number 3 is listed twice.
766 * Once as "pm" and once again as "lomp".
768 if (devfn
== prev_devfn
)
772 /* create a new pci_dev for this device */
773 dev
= of_create_pci_dev(pbm
, child
, bus
, devfn
, 0);
777 printk("PCI: dev header type: %x\n",
780 if (dev
->hdr_type
== PCI_HEADER_TYPE_BRIDGE
||
781 dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
)
782 of_scan_pci_bridge(pbm
, child
, dev
);
787 show_pciobppath_attr(struct device
* dev
, struct device_attribute
* attr
, char * buf
)
789 struct pci_dev
*pdev
;
790 struct device_node
*dp
;
792 pdev
= to_pci_dev(dev
);
793 dp
= pdev
->dev
.archdata
.prom_node
;
795 return snprintf (buf
, PAGE_SIZE
, "%s\n", dp
->full_name
);
798 static DEVICE_ATTR(obppath
, S_IRUSR
| S_IRGRP
| S_IROTH
, show_pciobppath_attr
, NULL
);
800 static void __devinit
pci_bus_register_of_sysfs(struct pci_bus
*bus
)
803 struct pci_bus
*child_bus
;
806 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
807 /* we don't really care if we can create this file or
808 * not, but we need to assign the result of the call
809 * or the world will fall under alien invasion and
810 * everybody will be frozen on a spaceship ready to be
811 * eaten on alpha centauri by some green and jelly
814 err
= sysfs_create_file(&dev
->dev
.kobj
, &dev_attr_obppath
.attr
);
816 list_for_each_entry(child_bus
, &bus
->children
, node
)
817 pci_bus_register_of_sysfs(child_bus
);
820 int pci_host_bridge_read_pci_cfg(struct pci_bus
*bus_dev
,
825 static u8 fake_pci_config
[] = {
826 0x8e, 0x10, /* Vendor: 0x108e (Sun) */
827 0xf0, 0x80, /* Device: 0x80f0 (Fire) */
828 0x46, 0x01, /* Command: 0x0146 (SERR, PARITY, MASTER, MEM) */
829 0xa0, 0x22, /* Status: 0x02a0 (DEVSEL_MED, FB2B, 66MHZ) */
830 0x00, 0x00, 0x00, 0x06, /* Class: 0x06000000 host bridge */
831 0x00, /* Cacheline: 0x00 */
832 0x40, /* Latency: 0x40 */
833 0x00, /* Header-Type: 0x00 normal */
837 if (where
>= 0 && where
< sizeof(fake_pci_config
) &&
838 (where
+ size
) >= 0 &&
839 (where
+ size
) < sizeof(fake_pci_config
) &&
840 size
<= sizeof(u32
)) {
843 *value
|= fake_pci_config
[where
+ size
];
847 return PCIBIOS_SUCCESSFUL
;
850 int pci_host_bridge_write_pci_cfg(struct pci_bus
*bus_dev
,
855 return PCIBIOS_SUCCESSFUL
;
858 struct pci_bus
* __devinit
pci_scan_one_pbm(struct pci_pbm_info
*pbm
)
860 struct device_node
*node
= pbm
->prom_node
;
861 struct pci_dev
*host_pdev
;
864 printk("PCI: Scanning PBM %s\n", node
->full_name
);
866 /* XXX parent device? XXX */
867 bus
= pci_create_bus(NULL
, pbm
->pci_first_busno
, pbm
->pci_ops
, pbm
);
869 printk(KERN_ERR
"Failed to create bus for %s\n",
873 bus
->secondary
= pbm
->pci_first_busno
;
874 bus
->subordinate
= pbm
->pci_last_busno
;
876 bus
->resource
[0] = &pbm
->io_space
;
877 bus
->resource
[1] = &pbm
->mem_space
;
879 /* Create the dummy host bridge and link it in. */
880 host_pdev
= of_create_pci_dev(pbm
, node
, bus
, 0x00, 1);
881 bus
->self
= host_pdev
;
883 pci_of_scan_bus(pbm
, node
, bus
);
884 pci_bus_add_devices(bus
);
885 pci_bus_register_of_sysfs(bus
);
890 static void __init
pci_scan_each_controller_bus(void)
892 struct pci_pbm_info
*pbm
;
894 for (pbm
= pci_pbm_root
; pbm
; pbm
= pbm
->next
)
898 extern void power_init(void);
900 static int __init
pcibios_init(void)
902 pci_controller_probe();
903 if (pci_pbm_root
== NULL
)
906 pci_scan_each_controller_bus();
915 subsys_initcall(pcibios_init
);
917 void __devinit
pcibios_fixup_bus(struct pci_bus
*pbus
)
919 struct pci_pbm_info
*pbm
= pbus
->sysdata
;
921 /* Generic PCI bus probing sets these to point at
922 * &io{port,mem}_resouce which is wrong for us.
924 pbus
->resource
[0] = &pbm
->io_space
;
925 pbus
->resource
[1] = &pbm
->mem_space
;
928 struct resource
*pcibios_select_root(struct pci_dev
*pdev
, struct resource
*r
)
930 struct pci_pbm_info
*pbm
= pdev
->bus
->sysdata
;
931 struct resource
*root
= NULL
;
933 if (r
->flags
& IORESOURCE_IO
)
934 root
= &pbm
->io_space
;
935 if (r
->flags
& IORESOURCE_MEM
)
936 root
= &pbm
->mem_space
;
941 void pcibios_update_irq(struct pci_dev
*pdev
, int irq
)
945 void pcibios_align_resource(void *data
, struct resource
*res
,
946 resource_size_t size
, resource_size_t align
)
950 int pcibios_enable_device(struct pci_dev
*dev
, int mask
)
955 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
958 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
959 struct resource
*res
= &dev
->resource
[i
];
961 /* Only set up the requested stuff */
962 if (!(mask
& (1<<i
)))
965 if (res
->flags
& IORESOURCE_IO
)
966 cmd
|= PCI_COMMAND_IO
;
967 if (res
->flags
& IORESOURCE_MEM
)
968 cmd
|= PCI_COMMAND_MEMORY
;
972 printk(KERN_DEBUG
"PCI: Enabling device: (%s), cmd %x\n",
974 /* Enable the appropriate bits in the PCI command register. */
975 pci_write_config_word(dev
, PCI_COMMAND
, cmd
);
980 void pcibios_resource_to_bus(struct pci_dev
*pdev
, struct pci_bus_region
*region
,
981 struct resource
*res
)
983 struct pci_pbm_info
*pbm
= pdev
->bus
->sysdata
;
984 struct resource zero_res
, *root
;
988 zero_res
.flags
= res
->flags
;
990 if (res
->flags
& IORESOURCE_IO
)
991 root
= &pbm
->io_space
;
993 root
= &pbm
->mem_space
;
995 pci_resource_adjust(&zero_res
, root
);
997 region
->start
= res
->start
- zero_res
.start
;
998 region
->end
= res
->end
- zero_res
.start
;
1000 EXPORT_SYMBOL(pcibios_resource_to_bus
);
1002 void pcibios_bus_to_resource(struct pci_dev
*pdev
, struct resource
*res
,
1003 struct pci_bus_region
*region
)
1005 struct pci_pbm_info
*pbm
= pdev
->bus
->sysdata
;
1006 struct resource
*root
;
1008 res
->start
= region
->start
;
1009 res
->end
= region
->end
;
1011 if (res
->flags
& IORESOURCE_IO
)
1012 root
= &pbm
->io_space
;
1014 root
= &pbm
->mem_space
;
1016 pci_resource_adjust(res
, root
);
1018 EXPORT_SYMBOL(pcibios_bus_to_resource
);
1020 char * __devinit
pcibios_setup(char *str
)
1025 /* Platform support for /proc/bus/pci/X/Y mmap()s. */
1027 /* If the user uses a host-bridge as the PCI device, he may use
1028 * this to perform a raw mmap() of the I/O or MEM space behind
1031 * This can be useful for execution of x86 PCI bios initialization code
1032 * on a PCI card, like the xfree86 int10 stuff does.
1034 static int __pci_mmap_make_offset_bus(struct pci_dev
*pdev
, struct vm_area_struct
*vma
,
1035 enum pci_mmap_state mmap_state
)
1037 struct pci_pbm_info
*pbm
= pdev
->dev
.archdata
.host_controller
;
1038 unsigned long space_size
, user_offset
, user_size
;
1040 if (mmap_state
== pci_mmap_io
) {
1041 space_size
= (pbm
->io_space
.end
-
1042 pbm
->io_space
.start
) + 1;
1044 space_size
= (pbm
->mem_space
.end
-
1045 pbm
->mem_space
.start
) + 1;
1048 /* Make sure the request is in range. */
1049 user_offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
1050 user_size
= vma
->vm_end
- vma
->vm_start
;
1052 if (user_offset
>= space_size
||
1053 (user_offset
+ user_size
) > space_size
)
1056 if (mmap_state
== pci_mmap_io
) {
1057 vma
->vm_pgoff
= (pbm
->io_space
.start
+
1058 user_offset
) >> PAGE_SHIFT
;
1060 vma
->vm_pgoff
= (pbm
->mem_space
.start
+
1061 user_offset
) >> PAGE_SHIFT
;
1067 /* Adjust vm_pgoff of VMA such that it is the physical page offset
1068 * corresponding to the 32-bit pci bus offset for DEV requested by the user.
1070 * Basically, the user finds the base address for his device which he wishes
1071 * to mmap. They read the 32-bit value from the config space base register,
1072 * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
1073 * offset parameter of mmap on /proc/bus/pci/XXX for that device.
1075 * Returns negative error code on failure, zero on success.
1077 static int __pci_mmap_make_offset(struct pci_dev
*pdev
,
1078 struct vm_area_struct
*vma
,
1079 enum pci_mmap_state mmap_state
)
1081 unsigned long user_paddr
, user_size
;
1084 /* First compute the physical address in vma->vm_pgoff,
1085 * making sure the user offset is within range in the
1086 * appropriate PCI space.
1088 err
= __pci_mmap_make_offset_bus(pdev
, vma
, mmap_state
);
1092 /* If this is a mapping on a host bridge, any address
1095 if ((pdev
->class >> 8) == PCI_CLASS_BRIDGE_HOST
)
1098 /* Otherwise make sure it's in the range for one of the
1099 * device's resources.
1101 user_paddr
= vma
->vm_pgoff
<< PAGE_SHIFT
;
1102 user_size
= vma
->vm_end
- vma
->vm_start
;
1104 for (i
= 0; i
<= PCI_ROM_RESOURCE
; i
++) {
1105 struct resource
*rp
= &pdev
->resource
[i
];
1112 if (i
== PCI_ROM_RESOURCE
) {
1113 if (mmap_state
!= pci_mmap_mem
)
1116 if ((mmap_state
== pci_mmap_io
&&
1117 (rp
->flags
& IORESOURCE_IO
) == 0) ||
1118 (mmap_state
== pci_mmap_mem
&&
1119 (rp
->flags
& IORESOURCE_MEM
) == 0))
1123 if ((rp
->start
<= user_paddr
) &&
1124 (user_paddr
+ user_size
) <= (rp
->end
+ 1UL))
1128 if (i
> PCI_ROM_RESOURCE
)
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) */