1 /* $Id: pci.c,v 1.39 2002/01/05 01:13:43 davem Exp $
2 * pci.c: UltraSparc PCI controller support.
4 * Copyright (C) 1997, 1998, 1999 David S. Miller (davem@redhat.com)
5 * Copyright (C) 1998, 1999 Eddie C. Dost (ecd@skynet.be)
6 * Copyright (C) 1999 Jakub Jelinek (jj@ultra.linux.cz)
9 #include <linux/config.h>
10 #include <linux/kernel.h>
11 #include <linux/string.h>
12 #include <linux/sched.h>
13 #include <linux/capability.h>
14 #include <linux/errno.h>
15 #include <linux/smp_lock.h>
16 #include <linux/init.h>
18 #include <asm/uaccess.h>
24 unsigned long pci_memspace_mask = 0xffffffffUL;
27 /* A "nop" PCI implementation. */
28 asmlinkage int sys_pciconfig_read(unsigned long bus, unsigned long dfn,
29 unsigned long off, unsigned long len,
34 asmlinkage int sys_pciconfig_write(unsigned long bus, unsigned long dfn,
35 unsigned long off, unsigned long len,
42 /* List of all PCI controllers found in the system. */
43 spinlock_t pci_controller_lock = SPIN_LOCK_UNLOCKED;
44 struct pci_controller_info *pci_controller_root = NULL;
46 /* Each PCI controller found gets a unique index. */
47 int pci_num_controllers = 0;
49 /* At boot time the user can give the kernel a command
50 * line option which controls if and how PCI devices
51 * are reordered at PCI bus probing time.
53 int pci_device_reorder = 0;
55 volatile int pci_poke_in_progress;
56 volatile int pci_poke_cpu = -1;
57 volatile int pci_poke_faulted;
59 static spinlock_t pci_poke_lock = SPIN_LOCK_UNLOCKED;
61 void pci_config_read8(u8 *addr, u8 *ret)
66 spin_lock_irqsave(&pci_poke_lock, flags);
67 pci_poke_cpu = smp_processor_id();
68 pci_poke_in_progress = 1;
70 __asm__ __volatile__("membar #Sync\n\t"
71 "lduba [%1] %2, %0\n\t"
74 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
76 pci_poke_in_progress = 0;
78 if (!pci_poke_faulted)
80 spin_unlock_irqrestore(&pci_poke_lock, flags);
83 void pci_config_read16(u16 *addr, u16 *ret)
88 spin_lock_irqsave(&pci_poke_lock, flags);
89 pci_poke_cpu = smp_processor_id();
90 pci_poke_in_progress = 1;
92 __asm__ __volatile__("membar #Sync\n\t"
93 "lduha [%1] %2, %0\n\t"
96 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
98 pci_poke_in_progress = 0;
100 if (!pci_poke_faulted)
102 spin_unlock_irqrestore(&pci_poke_lock, flags);
105 void pci_config_read32(u32 *addr, u32 *ret)
110 spin_lock_irqsave(&pci_poke_lock, flags);
111 pci_poke_cpu = smp_processor_id();
112 pci_poke_in_progress = 1;
113 pci_poke_faulted = 0;
114 __asm__ __volatile__("membar #Sync\n\t"
115 "lduwa [%1] %2, %0\n\t"
118 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
120 pci_poke_in_progress = 0;
122 if (!pci_poke_faulted)
124 spin_unlock_irqrestore(&pci_poke_lock, flags);
127 void pci_config_write8(u8 *addr, u8 val)
131 spin_lock_irqsave(&pci_poke_lock, flags);
132 pci_poke_cpu = smp_processor_id();
133 pci_poke_in_progress = 1;
134 pci_poke_faulted = 0;
135 __asm__ __volatile__("membar #Sync\n\t"
136 "stba %0, [%1] %2\n\t"
139 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
141 pci_poke_in_progress = 0;
143 spin_unlock_irqrestore(&pci_poke_lock, flags);
146 void pci_config_write16(u16 *addr, u16 val)
150 spin_lock_irqsave(&pci_poke_lock, flags);
151 pci_poke_cpu = smp_processor_id();
152 pci_poke_in_progress = 1;
153 pci_poke_faulted = 0;
154 __asm__ __volatile__("membar #Sync\n\t"
155 "stha %0, [%1] %2\n\t"
158 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
160 pci_poke_in_progress = 0;
162 spin_unlock_irqrestore(&pci_poke_lock, flags);
165 void pci_config_write32(u32 *addr, u32 val)
169 spin_lock_irqsave(&pci_poke_lock, flags);
170 pci_poke_cpu = smp_processor_id();
171 pci_poke_in_progress = 1;
172 pci_poke_faulted = 0;
173 __asm__ __volatile__("membar #Sync\n\t"
174 "stwa %0, [%1] %2\n\t"
177 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
179 pci_poke_in_progress = 0;
181 spin_unlock_irqrestore(&pci_poke_lock, flags);
184 /* Probe for all PCI controllers in the system. */
185 extern void sabre_init(int, char *);
186 extern void psycho_init(int, char *);
187 extern void schizo_init(int, char *);
188 extern void schizo_plus_init(int, char *);
189 extern void tomatillo_init(int, char *);
193 void (*init)(int, char *);
194 } pci_controller_table[] __initdata = {
195 { "SUNW,sabre", sabre_init },
196 { "pci108e,a000", sabre_init },
197 { "pci108e,a001", sabre_init },
198 { "SUNW,psycho", psycho_init },
199 { "pci108e,8000", psycho_init },
200 { "SUNW,schizo", schizo_init },
201 { "pci108e,8001", schizo_init },
202 { "SUNW,schizo+", schizo_plus_init },
203 { "pci108e,8002", schizo_plus_init },
204 { "SUNW,tomatillo", tomatillo_init },
205 { "pci108e,a801", tomatillo_init },
207 #define PCI_NUM_CONTROLLER_TYPES (sizeof(pci_controller_table) / \
208 sizeof(pci_controller_table[0]))
210 static int __init pci_controller_init(char *model_name, int namelen, int node)
214 for (i = 0; i < PCI_NUM_CONTROLLER_TYPES; i++) {
215 if (!strncmp(model_name,
216 pci_controller_table[i].model_name,
218 pci_controller_table[i].init(node, model_name);
222 printk("PCI: Warning unknown controller, model name [%s]\n",
224 printk("PCI: Ignoring controller...\n");
229 static int __init pci_is_controller(char *model_name, int namelen, int node)
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)(char *, int, int))
249 node = prom_getchild(prom_root_node);
250 while ((node = prom_searchsiblings(node, "pci")) != 0) {
253 if ((len = prom_getproperty(node, "model", namebuf, sizeof(namebuf))) > 0 ||
254 (len = prom_getproperty(node, "compatible", namebuf, sizeof(namebuf))) > 0) {
257 /* Our value may be a multi-valued string in the
258 * case of some compatible properties. For sanity,
259 * only try the first one. */
261 while (namebuf[item_len] && len) {
266 if (handler(namebuf, item_len, node))
270 node = prom_getsibling(node);
279 /* Is there some PCI controller in the system? */
280 int __init pcic_present(void)
282 return pci_controller_scan(pci_is_controller);
285 /* Find each controller in the system, attach and initialize
286 * software state structure for each and link into the
287 * pci_controller_root. Setup the controller enough such
288 * that bus scanning can be done.
290 static void __init pci_controller_probe(void)
292 printk("PCI: Probing for controllers.\n");
294 pci_controller_scan(pci_controller_init);
297 static void __init pci_scan_each_controller_bus(void)
299 struct pci_controller_info *p;
302 spin_lock_irqsave(&pci_controller_lock, flags);
303 for (p = pci_controller_root; p; p = p->next)
305 spin_unlock_irqrestore(&pci_controller_lock, flags);
308 /* Reorder the pci_dev chain, so that onboard devices come first
309 * and then come the pluggable cards.
311 static void __init pci_reorder_devs(void)
313 struct list_head *pci_onboard = &pci_devices;
314 struct list_head *walk = pci_onboard->next;
316 while (walk != pci_onboard) {
317 struct pci_dev *pdev = pci_dev_g(walk);
318 struct list_head *walk_next = walk->next;
320 if (pdev->irq && (__irq_ino(pdev->irq) & 0x20)) {
322 list_add(walk, pci_onboard);
329 extern void clock_probe(void);
330 extern void power_init(void);
332 static int __init pcibios_init(void)
334 pci_controller_probe();
335 if (pci_controller_root == NULL)
338 pci_scan_each_controller_bus();
340 if (pci_device_reorder)
351 subsys_initcall(pcibios_init);
353 struct pci_fixup pcibios_fixups[] = {
357 void pcibios_fixup_bus(struct pci_bus *pbus)
359 struct pci_pbm_info *pbm = pbus->sysdata;
361 /* Generic PCI bus probing sets these to point at
362 * &io{port,mem}_resouce which is wrong for us.
364 pbus->resource[0] = &pbm->io_space;
365 pbus->resource[1] = &pbm->mem_space;
368 int pci_claim_resource(struct pci_dev *pdev, int resource)
370 struct pci_pbm_info *pbm = pdev->bus->sysdata;
371 struct resource *res = &pdev->resource[resource];
372 struct resource *root;
377 if (res->flags & IORESOURCE_IO)
378 root = &pbm->io_space;
380 root = &pbm->mem_space;
382 pbm->parent->resource_adjust(pdev, res, root);
384 return request_resource(root, res);
388 * Given the PCI bus a device resides on, try to
389 * find an acceptable resource allocation for a
390 * specific device resource..
392 static int pci_assign_bus_resource(const struct pci_bus *bus,
394 struct resource *res,
399 unsigned int type_mask;
402 type_mask = IORESOURCE_IO | IORESOURCE_MEM;
403 for (i = 0 ; i < 4; i++) {
404 struct resource *r = bus->resource[i];
408 /* type_mask must match */
409 if ((res->flags ^ r->flags) & type_mask)
412 /* Ok, try it out.. */
413 if (allocate_resource(r, res, size, min, -1, size, NULL, NULL) < 0)
416 /* PCI config space updated by caller. */
422 int pci_assign_resource(struct pci_dev *pdev, int resource)
424 struct pcidev_cookie *pcp = pdev->sysdata;
425 struct pci_pbm_info *pbm = pcp->pbm;
426 struct resource *res = &pdev->resource[resource];
427 unsigned long min, size;
430 if (res->flags & IORESOURCE_IO)
431 min = pbm->io_space.start + 0x400UL;
433 min = pbm->mem_space.start;
435 size = res->end - res->start + 1;
437 err = pci_assign_bus_resource(pdev->bus, pdev, res, size, min, resource);
440 printk("PCI: Failed to allocate resource %d for %s\n",
441 resource, pci_name(pdev));
443 /* Update PCI config space. */
444 pbm->parent->base_address_update(pdev, resource);
450 /* Sort resources by alignment */
451 void pdev_sort_resources(struct pci_dev *dev, struct resource_list *head)
455 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
457 struct resource_list *list, *tmp;
458 unsigned long r_align;
460 r = &dev->resource[i];
461 r_align = r->end - r->start;
463 if (!(r->flags) || r->parent)
466 printk(KERN_WARNING "PCI: Ignore bogus resource %d "
468 i, r->start, r->end, pci_name(dev));
471 r_align = (i < PCI_BRIDGE_RESOURCES) ? r_align + 1 : r->start;
472 for (list = head; ; list = list->next) {
473 unsigned long align = 0;
474 struct resource_list *ln = list->next;
478 idx = ln->res - &ln->dev->resource[0];
479 align = (idx < PCI_BRIDGE_RESOURCES) ?
480 ln->res->end - ln->res->start + 1 :
483 if (r_align > align) {
484 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
486 panic("pdev_sort_resources(): "
487 "kmalloc() failed!\n");
498 void pcibios_update_irq(struct pci_dev *pdev, int irq)
502 void pcibios_align_resource(void *data, struct resource *res,
503 unsigned long size, unsigned long align)
507 int pcibios_enable_device(struct pci_dev *pdev, int mask)
512 void pcibios_resource_to_bus(struct pci_dev *pdev, struct pci_bus_region *region,
513 struct resource *res)
515 struct pci_pbm_info *pbm = pdev->bus->sysdata;
516 struct resource zero_res, *root;
520 zero_res.flags = res->flags;
522 if (res->flags & IORESOURCE_IO)
523 root = &pbm->io_space;
525 root = &pbm->mem_space;
527 pbm->parent->resource_adjust(pdev, &zero_res, root);
529 region->start = res->start - zero_res.start;
530 region->end = res->end - zero_res.start;
533 void pcibios_bus_to_resource(struct pci_dev *pdev, struct resource *res,
534 struct pci_bus_region *region)
536 struct pci_pbm_info *pbm = pdev->bus->sysdata;
537 struct resource *root;
539 res->start = region->start;
540 res->end = region->end;
542 if (res->flags & IORESOURCE_IO)
543 root = &pbm->io_space;
545 root = &pbm->mem_space;
547 pbm->parent->resource_adjust(pdev, res, root);
550 char * __init pcibios_setup(char *str)
552 if (!strcmp(str, "onboardfirst")) {
553 pci_device_reorder = 1;
556 if (!strcmp(str, "noreorder")) {
557 pci_device_reorder = 0;
563 /* Platform support for /proc/bus/pci/X/Y mmap()s. */
565 /* If the user uses a host-bridge as the PCI device, he may use
566 * this to perform a raw mmap() of the I/O or MEM space behind
569 * This can be useful for execution of x86 PCI bios initialization code
570 * on a PCI card, like the xfree86 int10 stuff does.
572 static int __pci_mmap_make_offset_bus(struct pci_dev *pdev, struct vm_area_struct *vma,
573 enum pci_mmap_state mmap_state)
575 struct pcidev_cookie *pcp = pdev->sysdata;
576 struct pci_pbm_info *pbm;
577 struct pci_controller_info *p;
578 unsigned long space_size, user_offset, user_size;
587 if (p->pbms_same_domain) {
588 unsigned long lowest, highest;
590 lowest = ~0UL; highest = 0UL;
591 if (mmap_state == pci_mmap_io) {
592 if (p->pbm_A.io_space.flags) {
593 lowest = p->pbm_A.io_space.start;
594 highest = p->pbm_A.io_space.end + 1;
596 if (p->pbm_B.io_space.flags) {
597 if (lowest > p->pbm_B.io_space.start)
598 lowest = p->pbm_B.io_space.start;
599 if (highest < p->pbm_B.io_space.end + 1)
600 highest = p->pbm_B.io_space.end + 1;
602 space_size = highest - lowest;
604 if (p->pbm_A.mem_space.flags) {
605 lowest = p->pbm_A.mem_space.start;
606 highest = p->pbm_A.mem_space.end + 1;
608 if (p->pbm_B.mem_space.flags) {
609 if (lowest > p->pbm_B.mem_space.start)
610 lowest = p->pbm_B.mem_space.start;
611 if (highest < p->pbm_B.mem_space.end + 1)
612 highest = p->pbm_B.mem_space.end + 1;
614 space_size = highest - lowest;
617 if (mmap_state == pci_mmap_io) {
618 space_size = (pbm->io_space.end -
619 pbm->io_space.start) + 1;
621 space_size = (pbm->mem_space.end -
622 pbm->mem_space.start) + 1;
626 /* Make sure the request is in range. */
627 user_offset = vma->vm_pgoff << PAGE_SHIFT;
628 user_size = vma->vm_end - vma->vm_start;
630 if (user_offset >= space_size ||
631 (user_offset + user_size) > space_size)
634 if (p->pbms_same_domain) {
635 unsigned long lowest = ~0UL;
637 if (mmap_state == pci_mmap_io) {
638 if (p->pbm_A.io_space.flags)
639 lowest = p->pbm_A.io_space.start;
640 if (p->pbm_B.io_space.flags &&
641 lowest > p->pbm_B.io_space.start)
642 lowest = p->pbm_B.io_space.start;
644 if (p->pbm_A.mem_space.flags)
645 lowest = p->pbm_A.mem_space.start;
646 if (p->pbm_B.mem_space.flags &&
647 lowest > p->pbm_B.mem_space.start)
648 lowest = p->pbm_B.mem_space.start;
650 vma->vm_pgoff = (lowest + user_offset) >> PAGE_SHIFT;
652 if (mmap_state == pci_mmap_io) {
653 vma->vm_pgoff = (pbm->io_space.start +
654 user_offset) >> PAGE_SHIFT;
656 vma->vm_pgoff = (pbm->mem_space.start +
657 user_offset) >> PAGE_SHIFT;
664 /* Adjust vm_pgoff of VMA such that it is the physical page offset corresponding
665 * to the 32-bit pci bus offset for DEV requested by the user.
667 * Basically, the user finds the base address for his device which he wishes
668 * to mmap. They read the 32-bit value from the config space base register,
669 * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
670 * offset parameter of mmap on /proc/bus/pci/XXX for that device.
672 * Returns negative error code on failure, zero on success.
674 static int __pci_mmap_make_offset(struct pci_dev *dev, struct vm_area_struct *vma,
675 enum pci_mmap_state mmap_state)
677 unsigned long user_offset = vma->vm_pgoff << PAGE_SHIFT;
678 unsigned long user32 = user_offset & pci_memspace_mask;
679 unsigned long largest_base, this_base, addr32;
682 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
683 return __pci_mmap_make_offset_bus(dev, vma, mmap_state);
685 /* Figure out which base address this is for. */
687 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
688 struct resource *rp = &dev->resource[i];
695 if (i == PCI_ROM_RESOURCE) {
696 if (mmap_state != pci_mmap_mem)
699 if ((mmap_state == pci_mmap_io &&
700 (rp->flags & IORESOURCE_IO) == 0) ||
701 (mmap_state == pci_mmap_mem &&
702 (rp->flags & IORESOURCE_MEM) == 0))
706 this_base = rp->start;
708 addr32 = (this_base & PAGE_MASK) & pci_memspace_mask;
710 if (mmap_state == pci_mmap_io)
713 if (addr32 <= user32 && this_base > largest_base)
714 largest_base = this_base;
717 if (largest_base == 0UL)
720 /* Now construct the final physical address. */
721 if (mmap_state == pci_mmap_io)
722 vma->vm_pgoff = (((largest_base & ~0xffffffUL) | user32) >> PAGE_SHIFT);
724 vma->vm_pgoff = (((largest_base & ~(pci_memspace_mask)) | user32) >> PAGE_SHIFT);
729 /* Set vm_flags of VMA, as appropriate for this architecture, for a pci device
732 static void __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
733 enum pci_mmap_state mmap_state)
735 vma->vm_flags |= (VM_SHM | VM_LOCKED);
738 /* Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
741 static void __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
742 enum pci_mmap_state mmap_state)
744 /* Our io_remap_page_range takes care of this, do nothing. */
747 extern int io_remap_page_range(struct vm_area_struct *vma, unsigned long from, unsigned long offset,
748 unsigned long size, pgprot_t prot, int space);
750 /* Perform the actual remap of the pages for a PCI device mapping, as appropriate
751 * for this architecture. The region in the process to map is described by vm_start
752 * and vm_end members of VMA, the base physical address is found in vm_pgoff.
753 * The pci device structure is provided so that architectures may make mapping
754 * decisions on a per-device or per-bus basis.
756 * Returns a negative error code on failure, zero on success.
758 int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
759 enum pci_mmap_state mmap_state,
764 ret = __pci_mmap_make_offset(dev, vma, mmap_state);
768 __pci_mmap_set_flags(dev, vma, mmap_state);
769 __pci_mmap_set_pgprot(dev, vma, mmap_state);
771 ret = io_remap_page_range(vma, vma->vm_start,
772 (vma->vm_pgoff << PAGE_SHIFT |
773 (write_combine ? 0x1UL : 0x0UL)),
774 vma->vm_end - vma->vm_start, vma->vm_page_prot, 0);
778 vma->vm_flags |= VM_IO;
782 /* Return the domain nuber for this pci bus */
784 int pci_domain_nr(struct pci_bus *pbus)
786 struct pci_pbm_info *pbm = pbus->sysdata;
789 if (pbm == NULL || pbm->parent == NULL) {
792 struct pci_controller_info *p = pbm->parent;
795 if (p->pbms_same_domain == 0)
797 ((pbm == &pbm->parent->pbm_B) ? 1 : 0));
803 int pci_name_bus(char *name, struct pci_bus *bus)
805 sprintf(name, "%04x:%02x", pci_domain_nr(bus), bus->number);
809 int pcibios_prep_mwi(struct pci_dev *dev)
811 /* We set correct PCI_CACHE_LINE_SIZE register values for every
812 * device probed on this platform. So there is nothing to check
813 * and this always succeeds.
818 #endif /* !(CONFIG_PCI) */