1 /* pci_sun4v.c: SUN4V specific PCI controller support.
3 * Copyright (C) 2006 David S. Miller (davem@davemloft.net)
6 #include <linux/kernel.h>
7 #include <linux/types.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/interrupt.h>
12 #include <linux/percpu.h>
15 #include <asm/iommu.h>
18 #include <asm/pstate.h>
19 #include <asm/oplib.h>
20 #include <asm/hypervisor.h>
23 #include "iommu_common.h"
25 #include "pci_sun4v.h"
27 #define PGLIST_NENTS (PAGE_SIZE / sizeof(u64))
33 static DEFINE_PER_CPU(struct sun4v_pglist, iommu_pglists);
35 static long pci_arena_alloc(struct pci_iommu_arena *arena, unsigned long npages)
37 unsigned long n, i, start, end, limit;
45 n = find_next_zero_bit(arena->map, limit, start);
47 if (unlikely(end >= limit)) {
48 if (likely(pass < 1)) {
54 /* Scanned the whole thing, give up. */
59 for (i = n; i < end; i++) {
60 if (test_bit(i, arena->map)) {
66 for (i = n; i < end; i++)
67 __set_bit(i, arena->map);
74 static void pci_arena_free(struct pci_iommu_arena *arena, unsigned long base, unsigned long npages)
78 for (i = base; i < (base + npages); i++)
79 __clear_bit(i, arena->map);
82 static void *pci_4v_alloc_consistent(struct pci_dev *pdev, size_t size, dma_addr_t *dma_addrp)
84 struct pcidev_cookie *pcp;
85 struct pci_iommu *iommu;
86 unsigned long flags, order, first_page, npages, n;
93 size = IO_PAGE_ALIGN(size);
94 order = get_order(size);
95 if (order >= MAX_ORDER)
98 npages = size >> IO_PAGE_SHIFT;
99 if (npages > PGLIST_NENTS)
102 first_page = __get_free_pages(GFP_ATOMIC, order);
103 if (first_page == 0UL)
105 memset((char *)first_page, 0, PAGE_SIZE << order);
108 devhandle = pcp->pbm->devhandle;
109 iommu = pcp->pbm->iommu;
111 spin_lock_irqsave(&iommu->lock, flags);
112 entry = pci_arena_alloc(&iommu->arena, npages);
113 spin_unlock_irqrestore(&iommu->lock, flags);
115 if (unlikely(entry < 0L)) {
116 free_pages(first_page, order);
120 *dma_addrp = (iommu->page_table_map_base +
121 (entry << IO_PAGE_SHIFT));
122 ret = (void *) first_page;
123 first_page = __pa(first_page);
127 pglist = __get_cpu_var(iommu_pglists).pglist;
128 for (n = 0; n < npages; n++)
129 pglist[n] = first_page + (n * PAGE_SIZE);
134 num = pci_sun4v_iommu_map(devhandle, HV_PCI_TSBID(0, entry),
136 (HV_PCI_MAP_ATTR_READ |
137 HV_PCI_MAP_ATTR_WRITE),
142 } while (npages != 0);
149 static void pci_4v_free_consistent(struct pci_dev *pdev, size_t size, void *cpu, dma_addr_t dvma)
151 struct pcidev_cookie *pcp;
152 struct pci_iommu *iommu;
153 unsigned long flags, order, npages, entry;
156 npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
158 iommu = pcp->pbm->iommu;
159 devhandle = pcp->pbm->devhandle;
160 entry = ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
162 spin_lock_irqsave(&iommu->lock, flags);
164 pci_arena_free(&iommu->arena, entry, npages);
169 num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
173 } while (npages != 0);
175 spin_unlock_irqrestore(&iommu->lock, flags);
177 order = get_order(size);
179 free_pages((unsigned long)cpu, order);
182 static dma_addr_t pci_4v_map_single(struct pci_dev *pdev, void *ptr, size_t sz, int direction)
184 struct pcidev_cookie *pcp;
185 struct pci_iommu *iommu;
186 unsigned long flags, npages, oaddr;
187 unsigned long i, base_paddr;
188 u32 devhandle, bus_addr, ret;
195 iommu = pcp->pbm->iommu;
196 devhandle = pcp->pbm->devhandle;
198 if (unlikely(direction == PCI_DMA_NONE))
201 oaddr = (unsigned long)ptr;
202 npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
203 npages >>= IO_PAGE_SHIFT;
204 if (unlikely(npages > PGLIST_NENTS))
207 spin_lock_irqsave(&iommu->lock, flags);
208 entry = pci_arena_alloc(&iommu->arena, npages);
209 spin_unlock_irqrestore(&iommu->lock, flags);
211 if (unlikely(entry < 0L))
214 bus_addr = (iommu->page_table_map_base +
215 (entry << IO_PAGE_SHIFT));
216 ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
217 base_paddr = __pa(oaddr & IO_PAGE_MASK);
218 prot = HV_PCI_MAP_ATTR_READ;
219 if (direction != PCI_DMA_TODEVICE)
220 prot |= HV_PCI_MAP_ATTR_WRITE;
224 pglist = __get_cpu_var(iommu_pglists).pglist;
225 for (i = 0; i < npages; i++, base_paddr += IO_PAGE_SIZE)
226 pglist[i] = base_paddr;
231 num = pci_sun4v_iommu_map(devhandle, HV_PCI_TSBID(0, entry),
237 } while (npages != 0);
244 if (printk_ratelimit())
246 return PCI_DMA_ERROR_CODE;
249 static void pci_4v_unmap_single(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
251 struct pcidev_cookie *pcp;
252 struct pci_iommu *iommu;
253 unsigned long flags, npages;
257 if (unlikely(direction == PCI_DMA_NONE)) {
258 if (printk_ratelimit())
264 iommu = pcp->pbm->iommu;
265 devhandle = pcp->pbm->devhandle;
267 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
268 npages >>= IO_PAGE_SHIFT;
269 bus_addr &= IO_PAGE_MASK;
271 spin_lock_irqsave(&iommu->lock, flags);
273 entry = (bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT;
274 pci_arena_free(&iommu->arena, entry, npages);
279 num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
283 } while (npages != 0);
285 spin_unlock_irqrestore(&iommu->lock, flags);
288 #define SG_ENT_PHYS_ADDRESS(SG) \
289 (__pa(page_address((SG)->page)) + (SG)->offset)
291 static inline void fill_sg(long entry, u32 devhandle,
292 struct scatterlist *sg,
293 int nused, int nelems, unsigned long prot)
295 struct scatterlist *dma_sg = sg;
296 struct scatterlist *sg_end = sg + nelems;
297 int i, cpu, pglist_ent;
301 pglist = __get_cpu_var(iommu_pglists).pglist;
303 for (i = 0; i < nused; i++) {
304 unsigned long pteval = ~0UL;
307 dma_npages = ((dma_sg->dma_address & (IO_PAGE_SIZE - 1UL)) +
309 ((IO_PAGE_SIZE - 1UL))) >> IO_PAGE_SHIFT;
311 unsigned long offset;
314 /* If we are here, we know we have at least one
315 * more page to map. So walk forward until we
316 * hit a page crossing, and begin creating new
317 * mappings from that spot.
322 tmp = SG_ENT_PHYS_ADDRESS(sg);
324 if (((tmp ^ pteval) >> IO_PAGE_SHIFT) != 0UL) {
325 pteval = tmp & IO_PAGE_MASK;
326 offset = tmp & (IO_PAGE_SIZE - 1UL);
329 if (((tmp ^ (tmp + len - 1UL)) >> IO_PAGE_SHIFT) != 0UL) {
330 pteval = (tmp + IO_PAGE_SIZE) & IO_PAGE_MASK;
332 len -= (IO_PAGE_SIZE - (tmp & (IO_PAGE_SIZE - 1UL)));
338 pteval = (pteval & IOPTE_PAGE);
340 pglist[pglist_ent++] = pteval;
341 pteval += IO_PAGE_SIZE;
342 len -= (IO_PAGE_SIZE - offset);
347 pteval = (pteval & IOPTE_PAGE) + len;
350 /* Skip over any tail mappings we've fully mapped,
351 * adjusting pteval along the way. Stop when we
352 * detect a page crossing event.
354 while (sg < sg_end &&
355 (pteval << (64 - IO_PAGE_SHIFT)) != 0UL &&
356 (pteval == SG_ENT_PHYS_ADDRESS(sg)) &&
358 (SG_ENT_PHYS_ADDRESS(sg) + sg->length - 1UL)) >> IO_PAGE_SHIFT) == 0UL) {
359 pteval += sg->length;
362 if ((pteval << (64 - IO_PAGE_SHIFT)) == 0UL)
364 } while (dma_npages != 0);
368 BUG_ON(pglist_ent == 0);
373 num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
377 } while (pglist_ent != 0);
382 static int pci_4v_map_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
384 struct pcidev_cookie *pcp;
385 struct pci_iommu *iommu;
386 unsigned long flags, npages, prot;
387 u32 devhandle, dma_base;
388 struct scatterlist *sgtmp;
392 /* Fast path single entry scatterlists. */
394 sglist->dma_address =
395 pci_4v_map_single(pdev,
396 (page_address(sglist->page) + sglist->offset),
397 sglist->length, direction);
398 if (unlikely(sglist->dma_address == PCI_DMA_ERROR_CODE))
400 sglist->dma_length = sglist->length;
405 iommu = pcp->pbm->iommu;
406 devhandle = pcp->pbm->devhandle;
408 if (unlikely(direction == PCI_DMA_NONE))
411 /* Step 1: Prepare scatter list. */
412 npages = prepare_sg(sglist, nelems);
413 if (unlikely(npages > PGLIST_NENTS))
416 /* Step 2: Allocate a cluster and context, if necessary. */
417 spin_lock_irqsave(&iommu->lock, flags);
418 entry = pci_arena_alloc(&iommu->arena, npages);
419 spin_unlock_irqrestore(&iommu->lock, flags);
421 if (unlikely(entry < 0L))
424 dma_base = iommu->page_table_map_base +
425 (entry << IO_PAGE_SHIFT);
427 /* Step 3: Normalize DMA addresses. */
431 while (used && sgtmp->dma_length) {
432 sgtmp->dma_address += dma_base;
436 used = nelems - used;
438 /* Step 4: Create the mappings. */
439 prot = HV_PCI_MAP_ATTR_READ;
440 if (direction != PCI_DMA_TODEVICE)
441 prot |= HV_PCI_MAP_ATTR_WRITE;
443 fill_sg(entry, devhandle, sglist, used, nelems, prot);
448 if (printk_ratelimit())
453 static void pci_4v_unmap_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
455 struct pcidev_cookie *pcp;
456 struct pci_iommu *iommu;
457 unsigned long flags, i, npages;
459 u32 devhandle, bus_addr;
461 if (unlikely(direction == PCI_DMA_NONE)) {
462 if (printk_ratelimit())
467 iommu = pcp->pbm->iommu;
468 devhandle = pcp->pbm->devhandle;
470 bus_addr = sglist->dma_address & IO_PAGE_MASK;
472 for (i = 1; i < nelems; i++)
473 if (sglist[i].dma_length == 0)
476 npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length) -
477 bus_addr) >> IO_PAGE_SHIFT;
479 entry = ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
481 spin_lock_irqsave(&iommu->lock, flags);
483 pci_arena_free(&iommu->arena, entry, npages);
488 num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
492 } while (npages != 0);
494 spin_unlock_irqrestore(&iommu->lock, flags);
497 static void pci_4v_dma_sync_single_for_cpu(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
499 /* Nothing to do... */
502 static void pci_4v_dma_sync_sg_for_cpu(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
504 /* Nothing to do... */
507 struct pci_iommu_ops pci_sun4v_iommu_ops = {
508 .alloc_consistent = pci_4v_alloc_consistent,
509 .free_consistent = pci_4v_free_consistent,
510 .map_single = pci_4v_map_single,
511 .unmap_single = pci_4v_unmap_single,
512 .map_sg = pci_4v_map_sg,
513 .unmap_sg = pci_4v_unmap_sg,
514 .dma_sync_single_for_cpu = pci_4v_dma_sync_single_for_cpu,
515 .dma_sync_sg_for_cpu = pci_4v_dma_sync_sg_for_cpu,
518 /* SUN4V PCI configuration space accessors. */
520 static inline int pci_sun4v_out_of_range(struct pci_pbm_info *pbm, unsigned int bus)
522 if (bus < pbm->pci_first_busno ||
523 bus > pbm->pci_last_busno)
528 static int pci_sun4v_read_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
529 int where, int size, u32 *value)
531 struct pci_pbm_info *pbm = bus_dev->sysdata;
532 u32 devhandle = pbm->devhandle;
533 unsigned int bus = bus_dev->number;
534 unsigned int device = PCI_SLOT(devfn);
535 unsigned int func = PCI_FUNC(devfn);
538 if (pci_sun4v_out_of_range(pbm, bus)) {
541 ret = pci_sun4v_config_get(devhandle,
542 HV_PCI_DEVICE_BUILD(bus, device, func),
545 printk("read_pci_cfg: devh[%x] device[%08x] where[%x] sz[%d] "
547 devhandle, HV_PCI_DEVICE_BUILD(bus, device, func),
556 *value = ret & 0xffff;
559 *value = ret & 0xffffffff;
564 return PCIBIOS_SUCCESSFUL;
567 static int pci_sun4v_write_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
568 int where, int size, u32 value)
570 struct pci_pbm_info *pbm = bus_dev->sysdata;
571 u32 devhandle = pbm->devhandle;
572 unsigned int bus = bus_dev->number;
573 unsigned int device = PCI_SLOT(devfn);
574 unsigned int func = PCI_FUNC(devfn);
577 if (pci_sun4v_out_of_range(pbm, bus)) {
580 ret = pci_sun4v_config_put(devhandle,
581 HV_PCI_DEVICE_BUILD(bus, device, func),
584 printk("write_pci_cfg: devh[%x] device[%08x] where[%x] sz[%d] "
585 "val[%08x] == [%016lx]\n",
586 devhandle, HV_PCI_DEVICE_BUILD(bus, device, func),
587 where, size, value, ret);
590 return PCIBIOS_SUCCESSFUL;
593 static struct pci_ops pci_sun4v_ops = {
594 .read = pci_sun4v_read_pci_cfg,
595 .write = pci_sun4v_write_pci_cfg,
599 static void pbm_scan_bus(struct pci_controller_info *p,
600 struct pci_pbm_info *pbm)
602 struct pcidev_cookie *cookie = kmalloc(sizeof(*cookie), GFP_KERNEL);
605 prom_printf("%s: Critical allocation failure.\n", pbm->name);
609 /* All we care about is the PBM. */
610 memset(cookie, 0, sizeof(*cookie));
613 pbm->pci_bus = pci_scan_bus(pbm->pci_first_busno,
617 pci_fixup_host_bridge_self(pbm->pci_bus);
618 pbm->pci_bus->self->sysdata = cookie;
621 pci_fill_in_pbm_cookies(pbm->pci_bus, pbm,
622 prom_getchild(pbm->prom_node));
623 pci_record_assignments(pbm, pbm->pci_bus);
624 pci_assign_unassigned(pbm, pbm->pci_bus);
625 pci_fixup_irq(pbm, pbm->pci_bus);
626 pci_determine_66mhz_disposition(pbm, pbm->pci_bus);
627 pci_setup_busmastering(pbm, pbm->pci_bus);
630 static void pci_sun4v_scan_bus(struct pci_controller_info *p)
632 if (p->pbm_A.prom_node) {
633 p->pbm_A.is_66mhz_capable =
634 prom_getbool(p->pbm_A.prom_node, "66mhz-capable");
636 pbm_scan_bus(p, &p->pbm_A);
638 if (p->pbm_B.prom_node) {
639 p->pbm_B.is_66mhz_capable =
640 prom_getbool(p->pbm_B.prom_node, "66mhz-capable");
642 pbm_scan_bus(p, &p->pbm_B);
645 /* XXX register error interrupt handlers XXX */
648 static unsigned int pci_sun4v_irq_build(struct pci_pbm_info *pbm,
649 struct pci_dev *pdev,
652 u32 devhandle = pbm->devhandle;
657 switch ((pdev->class >> 16) & 0xff) {
658 case PCI_BASE_CLASS_STORAGE:
662 case PCI_BASE_CLASS_NETWORK:
666 case PCI_BASE_CLASS_DISPLAY:
670 case PCI_BASE_CLASS_MULTIMEDIA:
671 case PCI_BASE_CLASS_MEMORY:
672 case PCI_BASE_CLASS_BRIDGE:
673 case PCI_BASE_CLASS_SERIAL:
682 BUG_ON(PIL_RESERVED(pil));
684 return sun4v_build_irq(devhandle, devino, pil, IBF_PCI);
687 static void pci_sun4v_base_address_update(struct pci_dev *pdev, int resource)
689 struct pcidev_cookie *pcp = pdev->sysdata;
690 struct pci_pbm_info *pbm = pcp->pbm;
691 struct resource *res, *root;
693 int where, size, is_64bit;
695 res = &pdev->resource[resource];
697 where = PCI_BASE_ADDRESS_0 + (resource * 4);
698 } else if (resource == PCI_ROM_RESOURCE) {
699 where = pdev->rom_base_reg;
701 /* Somebody might have asked allocation of a non-standard resource */
705 /* XXX 64-bit MEM handling is not %100 correct... XXX */
707 if (res->flags & IORESOURCE_IO)
708 root = &pbm->io_space;
710 root = &pbm->mem_space;
711 if ((res->flags & PCI_BASE_ADDRESS_MEM_TYPE_MASK)
712 == PCI_BASE_ADDRESS_MEM_TYPE_64)
716 size = res->end - res->start;
717 pci_read_config_dword(pdev, where, ®);
718 reg = ((reg & size) |
719 (((u32)(res->start - root->start)) & ~size));
720 if (resource == PCI_ROM_RESOURCE) {
721 reg |= PCI_ROM_ADDRESS_ENABLE;
722 res->flags |= IORESOURCE_ROM_ENABLE;
724 pci_write_config_dword(pdev, where, reg);
726 /* This knows that the upper 32-bits of the address
727 * must be zero. Our PCI common layer enforces this.
730 pci_write_config_dword(pdev, where + 4, 0);
733 static void pci_sun4v_resource_adjust(struct pci_dev *pdev,
734 struct resource *res,
735 struct resource *root)
737 res->start += root->start;
738 res->end += root->start;
741 /* Use ranges property to determine where PCI MEM, I/O, and Config
742 * space are for this PCI bus module.
744 static void pci_sun4v_determine_mem_io_space(struct pci_pbm_info *pbm)
746 int i, saw_mem, saw_io;
748 saw_mem = saw_io = 0;
749 for (i = 0; i < pbm->num_pbm_ranges; i++) {
750 struct linux_prom_pci_ranges *pr = &pbm->pbm_ranges[i];
754 type = (pr->child_phys_hi >> 24) & 0x3;
755 a = (((unsigned long)pr->parent_phys_hi << 32UL) |
756 ((unsigned long)pr->parent_phys_lo << 0UL));
760 /* 16-bit IO space, 16MB */
761 pbm->io_space.start = a;
762 pbm->io_space.end = a + ((16UL*1024UL*1024UL) - 1UL);
763 pbm->io_space.flags = IORESOURCE_IO;
768 /* 32-bit MEM space, 2GB */
769 pbm->mem_space.start = a;
770 pbm->mem_space.end = a + (0x80000000UL - 1UL);
771 pbm->mem_space.flags = IORESOURCE_MEM;
776 /* XXX 64-bit MEM handling XXX */
783 if (!saw_io || !saw_mem) {
784 prom_printf("%s: Fatal error, missing %s PBM range.\n",
786 (!saw_io ? "IO" : "MEM"));
790 printk("%s: PCI IO[%lx] MEM[%lx]\n",
793 pbm->mem_space.start);
796 static void pbm_register_toplevel_resources(struct pci_controller_info *p,
797 struct pci_pbm_info *pbm)
799 pbm->io_space.name = pbm->mem_space.name = pbm->name;
801 request_resource(&ioport_resource, &pbm->io_space);
802 request_resource(&iomem_resource, &pbm->mem_space);
803 pci_register_legacy_regions(&pbm->io_space,
807 static void probe_existing_entries(struct pci_pbm_info *pbm,
808 struct pci_iommu *iommu)
810 struct pci_iommu_arena *arena = &iommu->arena;
814 devhandle = pbm->devhandle;
815 for (i = 0; i < arena->limit; i++) {
816 unsigned long ret, io_attrs, ra;
818 ret = pci_sun4v_iommu_getmap(devhandle,
822 __set_bit(i, arena->map);
826 static void pci_sun4v_iommu_init(struct pci_pbm_info *pbm)
828 struct pci_iommu *iommu = pbm->iommu;
829 unsigned long num_tsb_entries, sz;
830 u32 vdma[2], dma_mask, dma_offset;
833 err = prom_getproperty(pbm->prom_node, "virtual-dma",
834 (char *)&vdma[0], sizeof(vdma));
835 if (err == 0 || err == -1) {
836 /* No property, use default values. */
837 vdma[0] = 0x80000000;
838 vdma[1] = 0x80000000;
844 dma_mask |= 0x1fffffff;
849 dma_mask |= 0x3fffffff;
854 dma_mask |= 0x7fffffff;
859 prom_printf("PCI-SUN4V: strange virtual-dma size.\n");
863 num_tsb_entries = tsbsize / sizeof(iopte_t);
865 dma_offset = vdma[0];
867 /* Setup initial software IOMMU state. */
868 spin_lock_init(&iommu->lock);
869 iommu->ctx_lowest_free = 1;
870 iommu->page_table_map_base = dma_offset;
871 iommu->dma_addr_mask = dma_mask;
873 /* Allocate and initialize the free area map. */
874 sz = num_tsb_entries / 8;
875 sz = (sz + 7UL) & ~7UL;
876 iommu->arena.map = kmalloc(sz, GFP_KERNEL);
877 if (!iommu->arena.map) {
878 prom_printf("PCI_IOMMU: Error, kmalloc(arena.map) failed.\n");
881 memset(iommu->arena.map, 0, sz);
882 iommu->arena.limit = num_tsb_entries;
884 probe_existing_entries(pbm, iommu);
887 /* Don't get this from the root nexus, get it from the "pci@0" node below. */
888 static void pci_sun4v_get_bus_range(struct pci_pbm_info *pbm)
890 unsigned int busrange[2];
891 int prom_node = pbm->prom_node;
894 prom_node = prom_getchild(prom_node);
895 if (prom_node == 0) {
896 prom_printf("%s: Fatal error, no child OBP node.\n", pbm->name);
900 err = prom_getproperty(prom_node, "bus-range",
901 (char *)&busrange[0],
903 if (err == 0 || err == -1) {
904 prom_printf("%s: Fatal error, no bus-range.\n", pbm->name);
908 pbm->pci_first_busno = busrange[0];
909 pbm->pci_last_busno = busrange[1];
913 static void pci_sun4v_pbm_init(struct pci_controller_info *p, int prom_node, u32 devhandle)
915 struct pci_pbm_info *pbm;
918 if (devhandle & 0x40)
924 pbm->prom_node = prom_node;
925 pbm->pci_first_slot = 1;
927 pbm->devhandle = devhandle;
929 sprintf(pbm->name, "SUN4V-PCI%d PBM%c",
930 p->index, (pbm == &p->pbm_A ? 'A' : 'B'));
932 printk("%s: devhandle[%x]\n", pbm->name, pbm->devhandle);
934 prom_getstring(prom_node, "name",
935 pbm->prom_name, sizeof(pbm->prom_name));
937 err = prom_getproperty(prom_node, "ranges",
938 (char *) pbm->pbm_ranges,
939 sizeof(pbm->pbm_ranges));
940 if (err == 0 || err == -1) {
941 prom_printf("%s: Fatal error, no ranges property.\n",
946 pbm->num_pbm_ranges =
947 (err / sizeof(struct linux_prom_pci_ranges));
949 /* Mask out the top 8 bits of the ranges, leaving the real
952 for (i = 0; i < pbm->num_pbm_ranges; i++)
953 pbm->pbm_ranges[i].parent_phys_hi &= 0x0fffffff;
955 pci_sun4v_determine_mem_io_space(pbm);
956 pbm_register_toplevel_resources(p, pbm);
958 err = prom_getproperty(prom_node, "interrupt-map",
959 (char *)pbm->pbm_intmap,
960 sizeof(pbm->pbm_intmap));
962 pbm->num_pbm_intmap = (err / sizeof(struct linux_prom_pci_intmap));
963 err = prom_getproperty(prom_node, "interrupt-map-mask",
964 (char *)&pbm->pbm_intmask,
965 sizeof(pbm->pbm_intmask));
967 prom_printf("%s: Fatal error, no "
968 "interrupt-map-mask.\n", pbm->name);
972 pbm->num_pbm_intmap = 0;
973 memset(&pbm->pbm_intmask, 0, sizeof(pbm->pbm_intmask));
976 pci_sun4v_get_bus_range(pbm);
977 pci_sun4v_iommu_init(pbm);
980 void sun4v_pci_init(int node, char *model_name)
982 struct pci_controller_info *p;
983 struct pci_iommu *iommu;
984 struct linux_prom64_registers regs;
988 prom_getproperty(node, "reg", (char *)®s, sizeof(regs));
989 devhandle = (regs.phys_addr >> 32UL) & 0x0fffffff;
991 for (p = pci_controller_root; p; p = p->next) {
992 struct pci_pbm_info *pbm;
994 if (p->pbm_A.prom_node && p->pbm_B.prom_node)
997 pbm = (p->pbm_A.prom_node ?
1001 if (pbm->devhandle == (devhandle ^ 0x40)) {
1002 pci_sun4v_pbm_init(p, node, devhandle);
1007 for (i = 0; i < NR_CPUS; i++) {
1008 unsigned long page = get_zeroed_page(GFP_ATOMIC);
1011 goto fatal_memory_error;
1013 per_cpu(iommu_pglists, i).pglist = (u64 *) page;
1016 p = kmalloc(sizeof(struct pci_controller_info), GFP_ATOMIC);
1018 goto fatal_memory_error;
1020 memset(p, 0, sizeof(*p));
1022 iommu = kmalloc(sizeof(struct pci_iommu), GFP_ATOMIC);
1024 goto fatal_memory_error;
1026 memset(iommu, 0, sizeof(*iommu));
1027 p->pbm_A.iommu = iommu;
1029 iommu = kmalloc(sizeof(struct pci_iommu), GFP_ATOMIC);
1031 goto fatal_memory_error;
1033 memset(iommu, 0, sizeof(*iommu));
1034 p->pbm_B.iommu = iommu;
1036 p->next = pci_controller_root;
1037 pci_controller_root = p;
1039 p->index = pci_num_controllers++;
1040 p->pbms_same_domain = 0;
1042 p->scan_bus = pci_sun4v_scan_bus;
1043 p->irq_build = pci_sun4v_irq_build;
1044 p->base_address_update = pci_sun4v_base_address_update;
1045 p->resource_adjust = pci_sun4v_resource_adjust;
1046 p->pci_ops = &pci_sun4v_ops;
1048 /* Like PSYCHO and SCHIZO we have a 2GB aligned area
1051 pci_memspace_mask = 0x7fffffffUL;
1053 pci_sun4v_pbm_init(p, node, devhandle);
1057 prom_printf("SUN4V_PCI: Fatal memory allocation error.\n");