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/module.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/msi.h>
17 #include <linux/irq.h>
18 #include <linux/init.h>
20 #include <asm/uaccess.h>
22 #include <asm/pgtable.h>
28 unsigned long pci_memspace_mask = 0xffffffffUL;
31 /* A "nop" PCI implementation. */
32 asmlinkage int sys_pciconfig_read(unsigned long bus, unsigned long dfn,
33 unsigned long off, unsigned long len,
38 asmlinkage int sys_pciconfig_write(unsigned long bus, unsigned long dfn,
39 unsigned long off, unsigned long len,
46 /* List of all PCI controllers found in the system. */
47 struct pci_controller_info *pci_controller_root = NULL;
49 /* Each PCI controller found gets a unique index. */
50 int pci_num_controllers = 0;
52 volatile int pci_poke_in_progress;
53 volatile int pci_poke_cpu = -1;
54 volatile int pci_poke_faulted;
56 static DEFINE_SPINLOCK(pci_poke_lock);
58 void pci_config_read8(u8 *addr, u8 *ret)
63 spin_lock_irqsave(&pci_poke_lock, flags);
64 pci_poke_cpu = smp_processor_id();
65 pci_poke_in_progress = 1;
67 __asm__ __volatile__("membar #Sync\n\t"
68 "lduba [%1] %2, %0\n\t"
71 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
73 pci_poke_in_progress = 0;
75 if (!pci_poke_faulted)
77 spin_unlock_irqrestore(&pci_poke_lock, flags);
80 void pci_config_read16(u16 *addr, u16 *ret)
85 spin_lock_irqsave(&pci_poke_lock, flags);
86 pci_poke_cpu = smp_processor_id();
87 pci_poke_in_progress = 1;
89 __asm__ __volatile__("membar #Sync\n\t"
90 "lduha [%1] %2, %0\n\t"
93 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
95 pci_poke_in_progress = 0;
97 if (!pci_poke_faulted)
99 spin_unlock_irqrestore(&pci_poke_lock, flags);
102 void pci_config_read32(u32 *addr, u32 *ret)
107 spin_lock_irqsave(&pci_poke_lock, flags);
108 pci_poke_cpu = smp_processor_id();
109 pci_poke_in_progress = 1;
110 pci_poke_faulted = 0;
111 __asm__ __volatile__("membar #Sync\n\t"
112 "lduwa [%1] %2, %0\n\t"
115 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
117 pci_poke_in_progress = 0;
119 if (!pci_poke_faulted)
121 spin_unlock_irqrestore(&pci_poke_lock, flags);
124 void pci_config_write8(u8 *addr, u8 val)
128 spin_lock_irqsave(&pci_poke_lock, flags);
129 pci_poke_cpu = smp_processor_id();
130 pci_poke_in_progress = 1;
131 pci_poke_faulted = 0;
132 __asm__ __volatile__("membar #Sync\n\t"
133 "stba %0, [%1] %2\n\t"
136 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
138 pci_poke_in_progress = 0;
140 spin_unlock_irqrestore(&pci_poke_lock, flags);
143 void pci_config_write16(u16 *addr, u16 val)
147 spin_lock_irqsave(&pci_poke_lock, flags);
148 pci_poke_cpu = smp_processor_id();
149 pci_poke_in_progress = 1;
150 pci_poke_faulted = 0;
151 __asm__ __volatile__("membar #Sync\n\t"
152 "stha %0, [%1] %2\n\t"
155 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
157 pci_poke_in_progress = 0;
159 spin_unlock_irqrestore(&pci_poke_lock, flags);
162 void pci_config_write32(u32 *addr, u32 val)
166 spin_lock_irqsave(&pci_poke_lock, flags);
167 pci_poke_cpu = smp_processor_id();
168 pci_poke_in_progress = 1;
169 pci_poke_faulted = 0;
170 __asm__ __volatile__("membar #Sync\n\t"
171 "stwa %0, [%1] %2\n\t"
174 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
176 pci_poke_in_progress = 0;
178 spin_unlock_irqrestore(&pci_poke_lock, flags);
181 /* Probe for all PCI controllers in the system. */
182 extern void sabre_init(struct device_node *, const char *);
183 extern void psycho_init(struct device_node *, const char *);
184 extern void schizo_init(struct device_node *, const char *);
185 extern void schizo_plus_init(struct device_node *, const char *);
186 extern void tomatillo_init(struct device_node *, const char *);
187 extern void sun4v_pci_init(struct device_node *, const char *);
191 void (*init)(struct device_node *, const char *);
192 } pci_controller_table[] __initdata = {
193 { "SUNW,sabre", sabre_init },
194 { "pci108e,a000", sabre_init },
195 { "pci108e,a001", sabre_init },
196 { "SUNW,psycho", psycho_init },
197 { "pci108e,8000", psycho_init },
198 { "SUNW,schizo", schizo_init },
199 { "pci108e,8001", schizo_init },
200 { "SUNW,schizo+", schizo_plus_init },
201 { "pci108e,8002", schizo_plus_init },
202 { "SUNW,tomatillo", tomatillo_init },
203 { "pci108e,a801", tomatillo_init },
204 { "SUNW,sun4v-pci", sun4v_pci_init },
206 #define PCI_NUM_CONTROLLER_TYPES (sizeof(pci_controller_table) / \
207 sizeof(pci_controller_table[0]))
209 static int __init pci_controller_init(const char *model_name, int namelen, struct device_node *dp)
213 for (i = 0; i < PCI_NUM_CONTROLLER_TYPES; i++) {
214 if (!strncmp(model_name,
215 pci_controller_table[i].model_name,
217 pci_controller_table[i].init(dp, model_name);
225 static int __init pci_is_controller(const char *model_name, int namelen, struct device_node *dp)
229 for (i = 0; i < PCI_NUM_CONTROLLER_TYPES; i++) {
230 if (!strncmp(model_name,
231 pci_controller_table[i].model_name,
239 static int __init pci_controller_scan(int (*handler)(const char *, int, struct device_node *))
241 struct device_node *dp;
244 for_each_node_by_name(dp, "pci") {
245 struct property *prop;
248 prop = of_find_property(dp, "model", &len);
250 prop = of_find_property(dp, "compatible", &len);
253 const char *model = prop->value;
256 /* Our value may be a multi-valued string in the
257 * case of some compatible properties. For sanity,
258 * only try the first one.
260 while (model[item_len] && len) {
265 if (handler(model, item_len, dp))
274 /* Is there some PCI controller in the system? */
275 int __init pcic_present(void)
277 return pci_controller_scan(pci_is_controller);
280 struct pci_iommu_ops *pci_iommu_ops;
281 EXPORT_SYMBOL(pci_iommu_ops);
283 extern struct pci_iommu_ops pci_sun4u_iommu_ops,
286 /* Find each controller in the system, attach and initialize
287 * software state structure for each and link into the
288 * pci_controller_root. Setup the controller enough such
289 * that bus scanning can be done.
291 static void __init pci_controller_probe(void)
293 if (tlb_type == hypervisor)
294 pci_iommu_ops = &pci_sun4v_iommu_ops;
296 pci_iommu_ops = &pci_sun4u_iommu_ops;
298 printk("PCI: Probing for controllers.\n");
300 pci_controller_scan(pci_controller_init);
303 static void __init pci_scan_each_controller_bus(void)
305 struct pci_controller_info *p;
307 for (p = pci_controller_root; p; p = p->next)
311 extern void power_init(void);
313 static int __init pcibios_init(void)
315 pci_controller_probe();
316 if (pci_controller_root == NULL)
319 pci_scan_each_controller_bus();
328 subsys_initcall(pcibios_init);
330 void pcibios_fixup_bus(struct pci_bus *pbus)
332 struct pci_pbm_info *pbm = pbus->sysdata;
334 /* Generic PCI bus probing sets these to point at
335 * &io{port,mem}_resouce which is wrong for us.
337 pbus->resource[0] = &pbm->io_space;
338 pbus->resource[1] = &pbm->mem_space;
341 struct resource *pcibios_select_root(struct pci_dev *pdev, struct resource *r)
343 struct pci_pbm_info *pbm = pdev->bus->sysdata;
344 struct resource *root = NULL;
346 if (r->flags & IORESOURCE_IO)
347 root = &pbm->io_space;
348 if (r->flags & IORESOURCE_MEM)
349 root = &pbm->mem_space;
354 void pcibios_update_irq(struct pci_dev *pdev, int irq)
358 void pcibios_align_resource(void *data, struct resource *res,
359 resource_size_t size, resource_size_t align)
363 int pcibios_enable_device(struct pci_dev *pdev, int mask)
368 void pcibios_resource_to_bus(struct pci_dev *pdev, struct pci_bus_region *region,
369 struct resource *res)
371 struct pci_pbm_info *pbm = pdev->bus->sysdata;
372 struct resource zero_res, *root;
376 zero_res.flags = res->flags;
378 if (res->flags & IORESOURCE_IO)
379 root = &pbm->io_space;
381 root = &pbm->mem_space;
383 pbm->parent->resource_adjust(pdev, &zero_res, root);
385 region->start = res->start - zero_res.start;
386 region->end = res->end - zero_res.start;
388 EXPORT_SYMBOL(pcibios_resource_to_bus);
390 void pcibios_bus_to_resource(struct pci_dev *pdev, struct resource *res,
391 struct pci_bus_region *region)
393 struct pci_pbm_info *pbm = pdev->bus->sysdata;
394 struct resource *root;
396 res->start = region->start;
397 res->end = region->end;
399 if (res->flags & IORESOURCE_IO)
400 root = &pbm->io_space;
402 root = &pbm->mem_space;
404 pbm->parent->resource_adjust(pdev, res, root);
406 EXPORT_SYMBOL(pcibios_bus_to_resource);
408 char * __init pcibios_setup(char *str)
413 /* Platform support for /proc/bus/pci/X/Y mmap()s. */
415 /* If the user uses a host-bridge as the PCI device, he may use
416 * this to perform a raw mmap() of the I/O or MEM space behind
419 * This can be useful for execution of x86 PCI bios initialization code
420 * on a PCI card, like the xfree86 int10 stuff does.
422 static int __pci_mmap_make_offset_bus(struct pci_dev *pdev, struct vm_area_struct *vma,
423 enum pci_mmap_state mmap_state)
425 struct pcidev_cookie *pcp = pdev->sysdata;
426 struct pci_pbm_info *pbm;
427 struct pci_controller_info *p;
428 unsigned long space_size, user_offset, user_size;
437 if (p->pbms_same_domain) {
438 unsigned long lowest, highest;
440 lowest = ~0UL; highest = 0UL;
441 if (mmap_state == pci_mmap_io) {
442 if (p->pbm_A.io_space.flags) {
443 lowest = p->pbm_A.io_space.start;
444 highest = p->pbm_A.io_space.end + 1;
446 if (p->pbm_B.io_space.flags) {
447 if (lowest > p->pbm_B.io_space.start)
448 lowest = p->pbm_B.io_space.start;
449 if (highest < p->pbm_B.io_space.end + 1)
450 highest = p->pbm_B.io_space.end + 1;
452 space_size = highest - lowest;
454 if (p->pbm_A.mem_space.flags) {
455 lowest = p->pbm_A.mem_space.start;
456 highest = p->pbm_A.mem_space.end + 1;
458 if (p->pbm_B.mem_space.flags) {
459 if (lowest > p->pbm_B.mem_space.start)
460 lowest = p->pbm_B.mem_space.start;
461 if (highest < p->pbm_B.mem_space.end + 1)
462 highest = p->pbm_B.mem_space.end + 1;
464 space_size = highest - lowest;
467 if (mmap_state == pci_mmap_io) {
468 space_size = (pbm->io_space.end -
469 pbm->io_space.start) + 1;
471 space_size = (pbm->mem_space.end -
472 pbm->mem_space.start) + 1;
476 /* Make sure the request is in range. */
477 user_offset = vma->vm_pgoff << PAGE_SHIFT;
478 user_size = vma->vm_end - vma->vm_start;
480 if (user_offset >= space_size ||
481 (user_offset + user_size) > space_size)
484 if (p->pbms_same_domain) {
485 unsigned long lowest = ~0UL;
487 if (mmap_state == pci_mmap_io) {
488 if (p->pbm_A.io_space.flags)
489 lowest = p->pbm_A.io_space.start;
490 if (p->pbm_B.io_space.flags &&
491 lowest > p->pbm_B.io_space.start)
492 lowest = p->pbm_B.io_space.start;
494 if (p->pbm_A.mem_space.flags)
495 lowest = p->pbm_A.mem_space.start;
496 if (p->pbm_B.mem_space.flags &&
497 lowest > p->pbm_B.mem_space.start)
498 lowest = p->pbm_B.mem_space.start;
500 vma->vm_pgoff = (lowest + user_offset) >> PAGE_SHIFT;
502 if (mmap_state == pci_mmap_io) {
503 vma->vm_pgoff = (pbm->io_space.start +
504 user_offset) >> PAGE_SHIFT;
506 vma->vm_pgoff = (pbm->mem_space.start +
507 user_offset) >> PAGE_SHIFT;
514 /* Adjust vm_pgoff of VMA such that it is the physical page offset corresponding
515 * to the 32-bit pci bus offset for DEV requested by the user.
517 * Basically, the user finds the base address for his device which he wishes
518 * to mmap. They read the 32-bit value from the config space base register,
519 * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
520 * offset parameter of mmap on /proc/bus/pci/XXX for that device.
522 * Returns negative error code on failure, zero on success.
524 static int __pci_mmap_make_offset(struct pci_dev *dev, struct vm_area_struct *vma,
525 enum pci_mmap_state mmap_state)
527 unsigned long user_offset = vma->vm_pgoff << PAGE_SHIFT;
528 unsigned long user32 = user_offset & pci_memspace_mask;
529 unsigned long largest_base, this_base, addr32;
532 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
533 return __pci_mmap_make_offset_bus(dev, vma, mmap_state);
535 /* Figure out which base address this is for. */
537 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
538 struct resource *rp = &dev->resource[i];
545 if (i == PCI_ROM_RESOURCE) {
546 if (mmap_state != pci_mmap_mem)
549 if ((mmap_state == pci_mmap_io &&
550 (rp->flags & IORESOURCE_IO) == 0) ||
551 (mmap_state == pci_mmap_mem &&
552 (rp->flags & IORESOURCE_MEM) == 0))
556 this_base = rp->start;
558 addr32 = (this_base & PAGE_MASK) & pci_memspace_mask;
560 if (mmap_state == pci_mmap_io)
563 if (addr32 <= user32 && this_base > largest_base)
564 largest_base = this_base;
567 if (largest_base == 0UL)
570 /* Now construct the final physical address. */
571 if (mmap_state == pci_mmap_io)
572 vma->vm_pgoff = (((largest_base & ~0xffffffUL) | user32) >> PAGE_SHIFT);
574 vma->vm_pgoff = (((largest_base & ~(pci_memspace_mask)) | user32) >> PAGE_SHIFT);
579 /* Set vm_flags of VMA, as appropriate for this architecture, for a pci device
582 static void __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
583 enum pci_mmap_state mmap_state)
585 vma->vm_flags |= (VM_IO | VM_RESERVED);
588 /* Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
591 static void __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
592 enum pci_mmap_state mmap_state)
594 /* Our io_remap_pfn_range takes care of this, do nothing. */
597 /* Perform the actual remap of the pages for a PCI device mapping, as appropriate
598 * for this architecture. The region in the process to map is described by vm_start
599 * and vm_end members of VMA, the base physical address is found in vm_pgoff.
600 * The pci device structure is provided so that architectures may make mapping
601 * decisions on a per-device or per-bus basis.
603 * Returns a negative error code on failure, zero on success.
605 int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
606 enum pci_mmap_state mmap_state,
611 ret = __pci_mmap_make_offset(dev, vma, mmap_state);
615 __pci_mmap_set_flags(dev, vma, mmap_state);
616 __pci_mmap_set_pgprot(dev, vma, mmap_state);
618 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
619 ret = io_remap_pfn_range(vma, vma->vm_start,
621 vma->vm_end - vma->vm_start,
629 /* Return the domain nuber for this pci bus */
631 int pci_domain_nr(struct pci_bus *pbus)
633 struct pci_pbm_info *pbm = pbus->sysdata;
636 if (pbm == NULL || pbm->parent == NULL) {
639 struct pci_controller_info *p = pbm->parent;
642 if (p->pbms_same_domain == 0)
644 ((pbm == &pbm->parent->pbm_B) ? 1 : 0));
649 EXPORT_SYMBOL(pci_domain_nr);
651 #ifdef CONFIG_PCI_MSI
652 int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
654 struct pcidev_cookie *pcp = pdev->sysdata;
655 struct pci_pbm_info *pbm = pcp->pbm;
656 struct pci_controller_info *p = pbm->parent;
659 if (!pbm->msi_num || !p->setup_msi_irq)
662 err = p->setup_msi_irq(&virt_irq, pdev, desc);
669 void arch_teardown_msi_irq(unsigned int virt_irq)
671 struct msi_desc *entry = get_irq_data(virt_irq);
672 struct pci_dev *pdev = entry->dev;
673 struct pcidev_cookie *pcp = pdev->sysdata;
674 struct pci_pbm_info *pbm = pcp->pbm;
675 struct pci_controller_info *p = pbm->parent;
677 if (!pbm->msi_num || !p->setup_msi_irq)
680 return p->teardown_msi_irq(virt_irq, pdev);
682 #endif /* !(CONFIG_PCI_MSI) */
684 #endif /* !(CONFIG_PCI) */