1 /* pci_sun4v.c: SUN4V specific PCI controller support.
3 * Copyright (C) 2006, 2007 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>
13 #include <linux/irq.h>
14 #include <linux/msi.h>
15 #include <linux/log2.h>
17 #include <asm/iommu.h>
20 #include <asm/pstate.h>
21 #include <asm/oplib.h>
22 #include <asm/hypervisor.h>
26 #include "iommu_common.h"
28 #include "pci_sun4v.h"
30 static unsigned long vpci_major = 1;
31 static unsigned long vpci_minor = 1;
33 #define PGLIST_NENTS (PAGE_SIZE / sizeof(u64))
36 struct pci_dev *pdev; /* Device mapping is for. */
37 unsigned long prot; /* IOMMU page protections */
38 unsigned long entry; /* Index into IOTSB. */
39 u64 *pglist; /* List of physical pages */
40 unsigned long npages; /* Number of pages in list. */
43 static DEFINE_PER_CPU(struct iommu_batch, pci_iommu_batch);
45 /* Interrupts must be disabled. */
46 static inline void pci_iommu_batch_start(struct pci_dev *pdev, unsigned long prot, unsigned long entry)
48 struct iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
56 /* Interrupts must be disabled. */
57 static long pci_iommu_batch_flush(struct iommu_batch *p)
59 struct pci_pbm_info *pbm = p->pdev->dev.archdata.host_controller;
60 unsigned long devhandle = pbm->devhandle;
61 unsigned long prot = p->prot;
62 unsigned long entry = p->entry;
63 u64 *pglist = p->pglist;
64 unsigned long npages = p->npages;
69 num = pci_sun4v_iommu_map(devhandle, HV_PCI_TSBID(0, entry),
70 npages, prot, __pa(pglist));
71 if (unlikely(num < 0)) {
72 if (printk_ratelimit())
73 printk("pci_iommu_batch_flush: IOMMU map of "
74 "[%08lx:%08lx:%lx:%lx:%lx] failed with "
76 devhandle, HV_PCI_TSBID(0, entry),
77 npages, prot, __pa(pglist), num);
92 /* Interrupts must be disabled. */
93 static inline long pci_iommu_batch_add(u64 phys_page)
95 struct iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
97 BUG_ON(p->npages >= PGLIST_NENTS);
99 p->pglist[p->npages++] = phys_page;
100 if (p->npages == PGLIST_NENTS)
101 return pci_iommu_batch_flush(p);
106 /* Interrupts must be disabled. */
107 static inline long pci_iommu_batch_end(void)
109 struct iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
111 BUG_ON(p->npages >= PGLIST_NENTS);
113 return pci_iommu_batch_flush(p);
116 static long pci_arena_alloc(struct iommu_arena *arena, unsigned long npages)
118 unsigned long n, i, start, end, limit;
121 limit = arena->limit;
126 n = find_next_zero_bit(arena->map, limit, start);
128 if (unlikely(end >= limit)) {
129 if (likely(pass < 1)) {
135 /* Scanned the whole thing, give up. */
140 for (i = n; i < end; i++) {
141 if (test_bit(i, arena->map)) {
147 for (i = n; i < end; i++)
148 __set_bit(i, arena->map);
155 static void pci_arena_free(struct iommu_arena *arena, unsigned long base, unsigned long npages)
159 for (i = base; i < (base + npages); i++)
160 __clear_bit(i, arena->map);
163 static void *pci_4v_alloc_consistent(struct pci_dev *pdev, size_t size, dma_addr_t *dma_addrp, gfp_t gfp)
166 unsigned long flags, order, first_page, npages, n;
170 size = IO_PAGE_ALIGN(size);
171 order = get_order(size);
172 if (unlikely(order >= MAX_ORDER))
175 npages = size >> IO_PAGE_SHIFT;
177 first_page = __get_free_pages(gfp, order);
178 if (unlikely(first_page == 0UL))
181 memset((char *)first_page, 0, PAGE_SIZE << order);
183 iommu = pdev->dev.archdata.iommu;
185 spin_lock_irqsave(&iommu->lock, flags);
186 entry = pci_arena_alloc(&iommu->arena, npages);
187 spin_unlock_irqrestore(&iommu->lock, flags);
189 if (unlikely(entry < 0L))
190 goto arena_alloc_fail;
192 *dma_addrp = (iommu->page_table_map_base +
193 (entry << IO_PAGE_SHIFT));
194 ret = (void *) first_page;
195 first_page = __pa(first_page);
197 local_irq_save(flags);
199 pci_iommu_batch_start(pdev,
200 (HV_PCI_MAP_ATTR_READ |
201 HV_PCI_MAP_ATTR_WRITE),
204 for (n = 0; n < npages; n++) {
205 long err = pci_iommu_batch_add(first_page + (n * PAGE_SIZE));
206 if (unlikely(err < 0L))
210 if (unlikely(pci_iommu_batch_end() < 0L))
213 local_irq_restore(flags);
218 /* Interrupts are disabled. */
219 spin_lock(&iommu->lock);
220 pci_arena_free(&iommu->arena, entry, npages);
221 spin_unlock_irqrestore(&iommu->lock, flags);
224 free_pages(first_page, order);
228 static void pci_4v_free_consistent(struct pci_dev *pdev, size_t size, void *cpu, dma_addr_t dvma)
230 struct pci_pbm_info *pbm;
232 unsigned long flags, order, npages, entry;
235 npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
236 iommu = pdev->dev.archdata.iommu;
237 pbm = pdev->dev.archdata.host_controller;
238 devhandle = pbm->devhandle;
239 entry = ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
241 spin_lock_irqsave(&iommu->lock, flags);
243 pci_arena_free(&iommu->arena, entry, npages);
248 num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
252 } while (npages != 0);
254 spin_unlock_irqrestore(&iommu->lock, flags);
256 order = get_order(size);
258 free_pages((unsigned long)cpu, order);
261 static dma_addr_t pci_4v_map_single(struct pci_dev *pdev, void *ptr, size_t sz, int direction)
264 unsigned long flags, npages, oaddr;
265 unsigned long i, base_paddr;
270 iommu = pdev->dev.archdata.iommu;
272 if (unlikely(direction == PCI_DMA_NONE))
275 oaddr = (unsigned long)ptr;
276 npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
277 npages >>= IO_PAGE_SHIFT;
279 spin_lock_irqsave(&iommu->lock, flags);
280 entry = pci_arena_alloc(&iommu->arena, npages);
281 spin_unlock_irqrestore(&iommu->lock, flags);
283 if (unlikely(entry < 0L))
286 bus_addr = (iommu->page_table_map_base +
287 (entry << IO_PAGE_SHIFT));
288 ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
289 base_paddr = __pa(oaddr & IO_PAGE_MASK);
290 prot = HV_PCI_MAP_ATTR_READ;
291 if (direction != PCI_DMA_TODEVICE)
292 prot |= HV_PCI_MAP_ATTR_WRITE;
294 local_irq_save(flags);
296 pci_iommu_batch_start(pdev, prot, entry);
298 for (i = 0; i < npages; i++, base_paddr += IO_PAGE_SIZE) {
299 long err = pci_iommu_batch_add(base_paddr);
300 if (unlikely(err < 0L))
303 if (unlikely(pci_iommu_batch_end() < 0L))
306 local_irq_restore(flags);
311 if (printk_ratelimit())
313 return PCI_DMA_ERROR_CODE;
316 /* Interrupts are disabled. */
317 spin_lock(&iommu->lock);
318 pci_arena_free(&iommu->arena, entry, npages);
319 spin_unlock_irqrestore(&iommu->lock, flags);
321 return PCI_DMA_ERROR_CODE;
324 static void pci_4v_unmap_single(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
326 struct pci_pbm_info *pbm;
328 unsigned long flags, npages;
332 if (unlikely(direction == PCI_DMA_NONE)) {
333 if (printk_ratelimit())
338 iommu = pdev->dev.archdata.iommu;
339 pbm = pdev->dev.archdata.host_controller;
340 devhandle = pbm->devhandle;
342 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
343 npages >>= IO_PAGE_SHIFT;
344 bus_addr &= IO_PAGE_MASK;
346 spin_lock_irqsave(&iommu->lock, flags);
348 entry = (bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT;
349 pci_arena_free(&iommu->arena, entry, npages);
354 num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
358 } while (npages != 0);
360 spin_unlock_irqrestore(&iommu->lock, flags);
363 #define SG_ENT_PHYS_ADDRESS(SG) \
364 (__pa(page_address((SG)->page)) + (SG)->offset)
366 static inline long fill_sg(long entry, struct pci_dev *pdev,
367 struct scatterlist *sg,
368 int nused, int nelems, unsigned long prot)
370 struct scatterlist *dma_sg = sg;
371 struct scatterlist *sg_end = sg + nelems;
375 local_irq_save(flags);
377 pci_iommu_batch_start(pdev, prot, entry);
379 for (i = 0; i < nused; i++) {
380 unsigned long pteval = ~0UL;
383 dma_npages = ((dma_sg->dma_address & (IO_PAGE_SIZE - 1UL)) +
385 ((IO_PAGE_SIZE - 1UL))) >> IO_PAGE_SHIFT;
387 unsigned long offset;
390 /* If we are here, we know we have at least one
391 * more page to map. So walk forward until we
392 * hit a page crossing, and begin creating new
393 * mappings from that spot.
398 tmp = SG_ENT_PHYS_ADDRESS(sg);
400 if (((tmp ^ pteval) >> IO_PAGE_SHIFT) != 0UL) {
401 pteval = tmp & IO_PAGE_MASK;
402 offset = tmp & (IO_PAGE_SIZE - 1UL);
405 if (((tmp ^ (tmp + len - 1UL)) >> IO_PAGE_SHIFT) != 0UL) {
406 pteval = (tmp + IO_PAGE_SIZE) & IO_PAGE_MASK;
408 len -= (IO_PAGE_SIZE - (tmp & (IO_PAGE_SIZE - 1UL)));
414 pteval = (pteval & IOPTE_PAGE);
418 err = pci_iommu_batch_add(pteval);
419 if (unlikely(err < 0L))
420 goto iommu_map_failed;
422 pteval += IO_PAGE_SIZE;
423 len -= (IO_PAGE_SIZE - offset);
428 pteval = (pteval & IOPTE_PAGE) + len;
431 /* Skip over any tail mappings we've fully mapped,
432 * adjusting pteval along the way. Stop when we
433 * detect a page crossing event.
435 while (sg < sg_end &&
436 (pteval << (64 - IO_PAGE_SHIFT)) != 0UL &&
437 (pteval == SG_ENT_PHYS_ADDRESS(sg)) &&
439 (SG_ENT_PHYS_ADDRESS(sg) + sg->length - 1UL)) >> IO_PAGE_SHIFT) == 0UL) {
440 pteval += sg->length;
443 if ((pteval << (64 - IO_PAGE_SHIFT)) == 0UL)
445 } while (dma_npages != 0);
449 if (unlikely(pci_iommu_batch_end() < 0L))
450 goto iommu_map_failed;
452 local_irq_restore(flags);
456 local_irq_restore(flags);
460 static int pci_4v_map_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
463 unsigned long flags, npages, prot;
465 struct scatterlist *sgtmp;
469 /* Fast path single entry scatterlists. */
471 sglist->dma_address =
472 pci_4v_map_single(pdev,
473 (page_address(sglist->page) + sglist->offset),
474 sglist->length, direction);
475 if (unlikely(sglist->dma_address == PCI_DMA_ERROR_CODE))
477 sglist->dma_length = sglist->length;
481 iommu = pdev->dev.archdata.iommu;
483 if (unlikely(direction == PCI_DMA_NONE))
486 /* Step 1: Prepare scatter list. */
487 npages = prepare_sg(sglist, nelems);
489 /* Step 2: Allocate a cluster and context, if necessary. */
490 spin_lock_irqsave(&iommu->lock, flags);
491 entry = pci_arena_alloc(&iommu->arena, npages);
492 spin_unlock_irqrestore(&iommu->lock, flags);
494 if (unlikely(entry < 0L))
497 dma_base = iommu->page_table_map_base +
498 (entry << IO_PAGE_SHIFT);
500 /* Step 3: Normalize DMA addresses. */
504 while (used && sgtmp->dma_length) {
505 sgtmp->dma_address += dma_base;
509 used = nelems - used;
511 /* Step 4: Create the mappings. */
512 prot = HV_PCI_MAP_ATTR_READ;
513 if (direction != PCI_DMA_TODEVICE)
514 prot |= HV_PCI_MAP_ATTR_WRITE;
516 err = fill_sg(entry, pdev, sglist, used, nelems, prot);
517 if (unlikely(err < 0L))
518 goto iommu_map_failed;
523 if (printk_ratelimit())
528 spin_lock_irqsave(&iommu->lock, flags);
529 pci_arena_free(&iommu->arena, entry, npages);
530 spin_unlock_irqrestore(&iommu->lock, flags);
535 static void pci_4v_unmap_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
537 struct pci_pbm_info *pbm;
539 unsigned long flags, i, npages;
541 u32 devhandle, bus_addr;
543 if (unlikely(direction == PCI_DMA_NONE)) {
544 if (printk_ratelimit())
548 iommu = pdev->dev.archdata.iommu;
549 pbm = pdev->dev.archdata.host_controller;
550 devhandle = pbm->devhandle;
552 bus_addr = sglist->dma_address & IO_PAGE_MASK;
554 for (i = 1; i < nelems; i++)
555 if (sglist[i].dma_length == 0)
558 npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length) -
559 bus_addr) >> IO_PAGE_SHIFT;
561 entry = ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
563 spin_lock_irqsave(&iommu->lock, flags);
565 pci_arena_free(&iommu->arena, entry, npages);
570 num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
574 } while (npages != 0);
576 spin_unlock_irqrestore(&iommu->lock, flags);
579 static void pci_4v_dma_sync_single_for_cpu(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
581 /* Nothing to do... */
584 static void pci_4v_dma_sync_sg_for_cpu(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
586 /* Nothing to do... */
589 const struct pci_iommu_ops pci_sun4v_iommu_ops = {
590 .alloc_consistent = pci_4v_alloc_consistent,
591 .free_consistent = pci_4v_free_consistent,
592 .map_single = pci_4v_map_single,
593 .unmap_single = pci_4v_unmap_single,
594 .map_sg = pci_4v_map_sg,
595 .unmap_sg = pci_4v_unmap_sg,
596 .dma_sync_single_for_cpu = pci_4v_dma_sync_single_for_cpu,
597 .dma_sync_sg_for_cpu = pci_4v_dma_sync_sg_for_cpu,
600 static void pci_sun4v_scan_bus(struct pci_pbm_info *pbm)
602 struct property *prop;
603 struct device_node *dp;
606 prop = of_find_property(dp, "66mhz-capable", NULL);
607 pbm->is_66mhz_capable = (prop != NULL);
608 pbm->pci_bus = pci_scan_one_pbm(pbm);
610 /* XXX register error interrupt handlers XXX */
613 static unsigned long probe_existing_entries(struct pci_pbm_info *pbm,
616 struct iommu_arena *arena = &iommu->arena;
617 unsigned long i, cnt = 0;
620 devhandle = pbm->devhandle;
621 for (i = 0; i < arena->limit; i++) {
622 unsigned long ret, io_attrs, ra;
624 ret = pci_sun4v_iommu_getmap(devhandle,
628 if (page_in_phys_avail(ra)) {
629 pci_sun4v_iommu_demap(devhandle,
630 HV_PCI_TSBID(0, i), 1);
633 __set_bit(i, arena->map);
641 static void pci_sun4v_iommu_init(struct pci_pbm_info *pbm)
643 struct iommu *iommu = pbm->iommu;
644 struct property *prop;
645 unsigned long num_tsb_entries, sz, tsbsize;
646 u32 vdma[2], dma_mask, dma_offset;
648 prop = of_find_property(pbm->prom_node, "virtual-dma", NULL);
650 u32 *val = prop->value;
655 /* No property, use default values. */
656 vdma[0] = 0x80000000;
657 vdma[1] = 0x80000000;
660 if ((vdma[0] | vdma[1]) & ~IO_PAGE_MASK) {
661 prom_printf("PCI-SUN4V: strange virtual-dma[%08x:%08x].\n",
666 dma_mask = (roundup_pow_of_two(vdma[1]) - 1UL);
667 num_tsb_entries = vdma[1] / IO_PAGE_SIZE;
668 tsbsize = num_tsb_entries * sizeof(iopte_t);
670 dma_offset = vdma[0];
672 /* Setup initial software IOMMU state. */
673 spin_lock_init(&iommu->lock);
674 iommu->ctx_lowest_free = 1;
675 iommu->page_table_map_base = dma_offset;
676 iommu->dma_addr_mask = dma_mask;
678 /* Allocate and initialize the free area map. */
679 sz = (num_tsb_entries + 7) / 8;
680 sz = (sz + 7UL) & ~7UL;
681 iommu->arena.map = kzalloc(sz, GFP_KERNEL);
682 if (!iommu->arena.map) {
683 prom_printf("PCI_IOMMU: Error, kmalloc(arena.map) failed.\n");
686 iommu->arena.limit = num_tsb_entries;
688 sz = probe_existing_entries(pbm, iommu);
690 printk("%s: Imported %lu TSB entries from OBP\n",
694 #ifdef CONFIG_PCI_MSI
695 struct pci_sun4v_msiq_entry {
697 #define MSIQ_VERSION_MASK 0xffffffff00000000UL
698 #define MSIQ_VERSION_SHIFT 32
699 #define MSIQ_TYPE_MASK 0x00000000000000ffUL
700 #define MSIQ_TYPE_SHIFT 0
701 #define MSIQ_TYPE_NONE 0x00
702 #define MSIQ_TYPE_MSG 0x01
703 #define MSIQ_TYPE_MSI32 0x02
704 #define MSIQ_TYPE_MSI64 0x03
705 #define MSIQ_TYPE_INTX 0x08
706 #define MSIQ_TYPE_NONE2 0xff
711 u64 req_id; /* bus/device/func */
712 #define MSIQ_REQID_BUS_MASK 0xff00UL
713 #define MSIQ_REQID_BUS_SHIFT 8
714 #define MSIQ_REQID_DEVICE_MASK 0x00f8UL
715 #define MSIQ_REQID_DEVICE_SHIFT 3
716 #define MSIQ_REQID_FUNC_MASK 0x0007UL
717 #define MSIQ_REQID_FUNC_SHIFT 0
721 /* The format of this value is message type dependent.
722 * For MSI bits 15:0 are the data from the MSI packet.
723 * For MSI-X bits 31:0 are the data from the MSI packet.
724 * For MSG, the message code and message routing code where:
725 * bits 39:32 is the bus/device/fn of the msg target-id
726 * bits 18:16 is the message routing code
727 * bits 7:0 is the message code
728 * For INTx the low order 2-bits are:
739 /* For now this just runs as a pre-handler for the real interrupt handler.
740 * So we just walk through the queue and ACK all the entries, update the
741 * head pointer, and return.
743 * In the longer term it would be nice to do something more integrated
744 * wherein we can pass in some of this MSI info to the drivers. This
745 * would be most useful for PCIe fabric error messages, although we could
746 * invoke those directly from the loop here in order to pass the info around.
748 static void pci_sun4v_msi_prehandler(unsigned int ino, void *data1, void *data2)
750 struct pci_pbm_info *pbm = data1;
751 struct pci_sun4v_msiq_entry *base, *ep;
752 unsigned long msiqid, orig_head, head, type, err;
754 msiqid = (unsigned long) data2;
757 err = pci_sun4v_msiq_gethead(pbm->devhandle, msiqid, &head);
761 if (unlikely(head >= (pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry))))
764 head /= sizeof(struct pci_sun4v_msiq_entry);
766 base = (pbm->msi_queues + ((msiqid - pbm->msiq_first) *
767 (pbm->msiq_ent_count *
768 sizeof(struct pci_sun4v_msiq_entry))));
770 while ((ep->version_type & MSIQ_TYPE_MASK) != 0) {
771 type = (ep->version_type & MSIQ_TYPE_MASK) >> MSIQ_TYPE_SHIFT;
772 if (unlikely(type != MSIQ_TYPE_MSI32 &&
773 type != MSIQ_TYPE_MSI64))
776 pci_sun4v_msi_setstate(pbm->devhandle,
777 ep->msi_data /* msi_num */,
780 /* Clear the entry. */
781 ep->version_type &= ~MSIQ_TYPE_MASK;
783 /* Go to next entry in ring. */
785 if (head >= pbm->msiq_ent_count)
790 if (likely(head != orig_head)) {
791 /* ACK entries by updating head pointer. */
792 head *= sizeof(struct pci_sun4v_msiq_entry);
793 err = pci_sun4v_msiq_sethead(pbm->devhandle, msiqid, head);
800 printk(KERN_EMERG "MSI: Hypervisor set head gives error %lu\n", err);
804 printk(KERN_EMERG "MSI: Hypervisor get head gives error %lu\n", err);
807 printk(KERN_EMERG "MSI: devhandle[%x] msiqid[%lx] head[%lu]\n",
808 pbm->devhandle, msiqid, head);
812 printk(KERN_EMERG "MSI: Hypervisor gives bad offset %lx max(%lx)\n",
813 head, pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry));
817 printk(KERN_EMERG "MSI: Entry has bad type %lx\n", type);
821 static int msi_bitmap_alloc(struct pci_pbm_info *pbm)
823 unsigned long size, bits_per_ulong;
825 bits_per_ulong = sizeof(unsigned long) * 8;
826 size = (pbm->msi_num + (bits_per_ulong - 1)) & ~(bits_per_ulong - 1);
828 BUG_ON(size % sizeof(unsigned long));
830 pbm->msi_bitmap = kzalloc(size, GFP_KERNEL);
831 if (!pbm->msi_bitmap)
837 static void msi_bitmap_free(struct pci_pbm_info *pbm)
839 kfree(pbm->msi_bitmap);
840 pbm->msi_bitmap = NULL;
843 static int msi_queue_alloc(struct pci_pbm_info *pbm)
845 unsigned long q_size, alloc_size, pages, order;
848 q_size = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
849 alloc_size = (pbm->msiq_num * q_size);
850 order = get_order(alloc_size);
851 pages = __get_free_pages(GFP_KERNEL | __GFP_COMP, order);
853 printk(KERN_ERR "MSI: Cannot allocate MSI queues (o=%lu).\n",
857 memset((char *)pages, 0, PAGE_SIZE << order);
858 pbm->msi_queues = (void *) pages;
860 for (i = 0; i < pbm->msiq_num; i++) {
861 unsigned long err, base = __pa(pages + (i * q_size));
862 unsigned long ret1, ret2;
864 err = pci_sun4v_msiq_conf(pbm->devhandle,
866 base, pbm->msiq_ent_count);
868 printk(KERN_ERR "MSI: msiq register fails (err=%lu)\n",
873 err = pci_sun4v_msiq_info(pbm->devhandle,
877 printk(KERN_ERR "MSI: Cannot read msiq (err=%lu)\n",
881 if (ret1 != base || ret2 != pbm->msiq_ent_count) {
882 printk(KERN_ERR "MSI: Bogus qconf "
883 "expected[%lx:%x] got[%lx:%lx]\n",
884 base, pbm->msiq_ent_count,
893 free_pages(pages, order);
898 static int alloc_msi(struct pci_pbm_info *pbm)
902 for (i = 0; i < pbm->msi_num; i++) {
903 if (!test_and_set_bit(i, pbm->msi_bitmap))
904 return i + pbm->msi_first;
910 static void free_msi(struct pci_pbm_info *pbm, int msi_num)
912 msi_num -= pbm->msi_first;
913 clear_bit(msi_num, pbm->msi_bitmap);
916 static int pci_sun4v_setup_msi_irq(unsigned int *virt_irq_p,
917 struct pci_dev *pdev,
918 struct msi_desc *entry)
920 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
921 unsigned long devino, msiqid;
927 msi_num = alloc_msi(pbm);
931 devino = sun4v_build_msi(pbm->devhandle, virt_irq_p,
932 pbm->msiq_first_devino,
933 (pbm->msiq_first_devino +
939 msiqid = ((devino - pbm->msiq_first_devino) +
943 if (pci_sun4v_msiq_setstate(pbm->devhandle, msiqid, HV_MSIQSTATE_IDLE))
947 if (pci_sun4v_msiq_setvalid(pbm->devhandle, msiqid, HV_MSIQ_VALID))
950 if (pci_sun4v_msi_setmsiq(pbm->devhandle,
952 (entry->msi_attrib.is_64 ?
953 HV_MSITYPE_MSI64 : HV_MSITYPE_MSI32)))
956 if (pci_sun4v_msi_setstate(pbm->devhandle, msi_num, HV_MSISTATE_IDLE))
959 if (pci_sun4v_msi_setvalid(pbm->devhandle, msi_num, HV_MSIVALID_VALID))
962 pdev->dev.archdata.msi_num = msi_num;
964 if (entry->msi_attrib.is_64) {
965 msg.address_hi = pbm->msi64_start >> 32;
966 msg.address_lo = pbm->msi64_start & 0xffffffff;
969 msg.address_lo = pbm->msi32_start;
973 set_irq_msi(*virt_irq_p, entry);
974 write_msi_msg(*virt_irq_p, &msg);
976 irq_install_pre_handler(*virt_irq_p,
977 pci_sun4v_msi_prehandler,
978 pbm, (void *) msiqid);
983 free_msi(pbm, msi_num);
984 sun4v_destroy_msi(*virt_irq_p);
990 static void pci_sun4v_teardown_msi_irq(unsigned int virt_irq,
991 struct pci_dev *pdev)
993 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
994 unsigned long msiqid, err;
995 unsigned int msi_num;
997 msi_num = pdev->dev.archdata.msi_num;
998 err = pci_sun4v_msi_getmsiq(pbm->devhandle, msi_num, &msiqid);
1000 printk(KERN_ERR "%s: getmsiq gives error %lu\n",
1005 pci_sun4v_msi_setvalid(pbm->devhandle, msi_num, HV_MSIVALID_INVALID);
1006 pci_sun4v_msiq_setvalid(pbm->devhandle, msiqid, HV_MSIQ_INVALID);
1008 free_msi(pbm, msi_num);
1010 /* The sun4v_destroy_msi() will liberate the devino and thus the MSIQ
1013 sun4v_destroy_msi(virt_irq);
1016 static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
1021 val = of_get_property(pbm->prom_node, "#msi-eqs", &len);
1022 if (!val || len != 4)
1024 pbm->msiq_num = *val;
1025 if (pbm->msiq_num) {
1026 const struct msiq_prop {
1031 const struct msi_range_prop {
1035 const struct addr_range_prop {
1044 val = of_get_property(pbm->prom_node, "msi-eq-size", &len);
1045 if (!val || len != 4)
1048 pbm->msiq_ent_count = *val;
1050 mqp = of_get_property(pbm->prom_node,
1051 "msi-eq-to-devino", &len);
1052 if (!mqp || len != sizeof(struct msiq_prop))
1055 pbm->msiq_first = mqp->first_msiq;
1056 pbm->msiq_first_devino = mqp->first_devino;
1058 val = of_get_property(pbm->prom_node, "#msi", &len);
1059 if (!val || len != 4)
1061 pbm->msi_num = *val;
1063 mrng = of_get_property(pbm->prom_node, "msi-ranges", &len);
1064 if (!mrng || len != sizeof(struct msi_range_prop))
1066 pbm->msi_first = mrng->first_msi;
1068 val = of_get_property(pbm->prom_node, "msi-data-mask", &len);
1069 if (!val || len != 4)
1071 pbm->msi_data_mask = *val;
1073 val = of_get_property(pbm->prom_node, "msix-data-width", &len);
1074 if (!val || len != 4)
1076 pbm->msix_data_width = *val;
1078 arng = of_get_property(pbm->prom_node, "msi-address-ranges",
1080 if (!arng || len != sizeof(struct addr_range_prop))
1082 pbm->msi32_start = ((u64)arng->msi32_high << 32) |
1083 (u64) arng->msi32_low;
1084 pbm->msi64_start = ((u64)arng->msi64_high << 32) |
1085 (u64) arng->msi64_low;
1086 pbm->msi32_len = arng->msi32_len;
1087 pbm->msi64_len = arng->msi64_len;
1089 if (msi_bitmap_alloc(pbm))
1092 if (msi_queue_alloc(pbm)) {
1093 msi_bitmap_free(pbm);
1097 printk(KERN_INFO "%s: MSI Queue first[%u] num[%u] count[%u] "
1100 pbm->msiq_first, pbm->msiq_num,
1101 pbm->msiq_ent_count,
1102 pbm->msiq_first_devino);
1103 printk(KERN_INFO "%s: MSI first[%u] num[%u] mask[0x%x] "
1106 pbm->msi_first, pbm->msi_num, pbm->msi_data_mask,
1107 pbm->msix_data_width);
1108 printk(KERN_INFO "%s: MSI addr32[0x%lx:0x%x] "
1109 "addr64[0x%lx:0x%x]\n",
1111 pbm->msi32_start, pbm->msi32_len,
1112 pbm->msi64_start, pbm->msi64_len);
1113 printk(KERN_INFO "%s: MSI queues at RA [%p]\n",
1117 pbm->setup_msi_irq = pci_sun4v_setup_msi_irq;
1118 pbm->teardown_msi_irq = pci_sun4v_teardown_msi_irq;
1124 printk(KERN_INFO "%s: No MSI support.\n", pbm->name);
1126 #else /* CONFIG_PCI_MSI */
1127 static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
1130 #endif /* !(CONFIG_PCI_MSI) */
1132 static void __init pci_sun4v_pbm_init(struct pci_controller_info *p, struct device_node *dp, u32 devhandle)
1134 struct pci_pbm_info *pbm;
1136 if (devhandle & 0x40)
1141 pbm->next = pci_pbm_root;
1144 pbm->scan_bus = pci_sun4v_scan_bus;
1145 pbm->pci_ops = &sun4v_pci_ops;
1146 pbm->config_space_reg_bits = 12;
1148 pbm->index = pci_num_pbms++;
1151 pbm->prom_node = dp;
1153 pbm->devhandle = devhandle;
1155 pbm->name = dp->full_name;
1157 printk("%s: SUN4V PCI Bus Module\n", pbm->name);
1159 pci_determine_mem_io_space(pbm);
1161 pci_get_pbm_props(pbm);
1162 pci_sun4v_iommu_init(pbm);
1163 pci_sun4v_msi_init(pbm);
1166 void __init sun4v_pci_init(struct device_node *dp, char *model_name)
1168 static int hvapi_negotiated = 0;
1169 struct pci_controller_info *p;
1170 struct pci_pbm_info *pbm;
1171 struct iommu *iommu;
1172 struct property *prop;
1173 struct linux_prom64_registers *regs;
1177 if (!hvapi_negotiated++) {
1178 int err = sun4v_hvapi_register(HV_GRP_PCI,
1183 prom_printf("SUN4V_PCI: Could not register hvapi, "
1187 printk("SUN4V_PCI: Registered hvapi major[%lu] minor[%lu]\n",
1188 vpci_major, vpci_minor);
1191 prop = of_find_property(dp, "reg", NULL);
1194 devhandle = (regs->phys_addr >> 32UL) & 0x0fffffff;
1196 for (pbm = pci_pbm_root; pbm; pbm = pbm->next) {
1197 if (pbm->devhandle == (devhandle ^ 0x40)) {
1198 pci_sun4v_pbm_init(pbm->parent, dp, devhandle);
1203 for_each_possible_cpu(i) {
1204 unsigned long page = get_zeroed_page(GFP_ATOMIC);
1207 goto fatal_memory_error;
1209 per_cpu(pci_iommu_batch, i).pglist = (u64 *) page;
1212 p = kzalloc(sizeof(struct pci_controller_info), GFP_ATOMIC);
1214 goto fatal_memory_error;
1216 iommu = kzalloc(sizeof(struct iommu), GFP_ATOMIC);
1218 goto fatal_memory_error;
1220 p->pbm_A.iommu = iommu;
1222 iommu = kzalloc(sizeof(struct iommu), GFP_ATOMIC);
1224 goto fatal_memory_error;
1226 p->pbm_B.iommu = iommu;
1228 /* Like PSYCHO and SCHIZO we have a 2GB aligned area
1231 pci_memspace_mask = 0x7fffffffUL;
1233 pci_sun4v_pbm_init(p, dp, devhandle);
1237 prom_printf("SUN4V_PCI: Fatal memory allocation error.\n");