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 device
*dev
; /* 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
, iommu_batch
);
45 /* Interrupts must be disabled. */
46 static inline void iommu_batch_start(struct device
*dev
, unsigned long prot
, unsigned long entry
)
48 struct iommu_batch
*p
= &__get_cpu_var(iommu_batch
);
56 /* Interrupts must be disabled. */
57 static long iommu_batch_flush(struct iommu_batch
*p
)
59 struct pci_pbm_info
*pbm
= p
->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("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 iommu_batch_add(u64 phys_page
)
95 struct iommu_batch
*p
= &__get_cpu_var(iommu_batch
);
97 BUG_ON(p
->npages
>= PGLIST_NENTS
);
99 p
->pglist
[p
->npages
++] = phys_page
;
100 if (p
->npages
== PGLIST_NENTS
)
101 return iommu_batch_flush(p
);
106 /* Interrupts must be disabled. */
107 static inline long iommu_batch_end(void)
109 struct iommu_batch
*p
= &__get_cpu_var(iommu_batch
);
111 BUG_ON(p
->npages
>= PGLIST_NENTS
);
113 return iommu_batch_flush(p
);
116 static long 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 arena_free(struct iommu_arena
*arena
, unsigned long base
,
156 unsigned long npages
)
160 for (i
= base
; i
< (base
+ npages
); i
++)
161 __clear_bit(i
, arena
->map
);
164 static void *dma_4v_alloc_coherent(struct device
*dev
, size_t size
,
165 dma_addr_t
*dma_addrp
, gfp_t gfp
)
168 unsigned long flags
, order
, first_page
, npages
, n
;
172 size
= IO_PAGE_ALIGN(size
);
173 order
= get_order(size
);
174 if (unlikely(order
>= MAX_ORDER
))
177 npages
= size
>> IO_PAGE_SHIFT
;
179 first_page
= __get_free_pages(gfp
, order
);
180 if (unlikely(first_page
== 0UL))
183 memset((char *)first_page
, 0, PAGE_SIZE
<< order
);
185 iommu
= dev
->archdata
.iommu
;
187 spin_lock_irqsave(&iommu
->lock
, flags
);
188 entry
= arena_alloc(&iommu
->arena
, npages
);
189 spin_unlock_irqrestore(&iommu
->lock
, flags
);
191 if (unlikely(entry
< 0L))
192 goto arena_alloc_fail
;
194 *dma_addrp
= (iommu
->page_table_map_base
+
195 (entry
<< IO_PAGE_SHIFT
));
196 ret
= (void *) first_page
;
197 first_page
= __pa(first_page
);
199 local_irq_save(flags
);
201 iommu_batch_start(dev
,
202 (HV_PCI_MAP_ATTR_READ
|
203 HV_PCI_MAP_ATTR_WRITE
),
206 for (n
= 0; n
< npages
; n
++) {
207 long err
= iommu_batch_add(first_page
+ (n
* PAGE_SIZE
));
208 if (unlikely(err
< 0L))
212 if (unlikely(iommu_batch_end() < 0L))
215 local_irq_restore(flags
);
220 /* Interrupts are disabled. */
221 spin_lock(&iommu
->lock
);
222 arena_free(&iommu
->arena
, entry
, npages
);
223 spin_unlock_irqrestore(&iommu
->lock
, flags
);
226 free_pages(first_page
, order
);
230 static void dma_4v_free_coherent(struct device
*dev
, size_t size
, void *cpu
,
233 struct pci_pbm_info
*pbm
;
235 unsigned long flags
, order
, npages
, entry
;
238 npages
= IO_PAGE_ALIGN(size
) >> IO_PAGE_SHIFT
;
239 iommu
= dev
->archdata
.iommu
;
240 pbm
= dev
->archdata
.host_controller
;
241 devhandle
= pbm
->devhandle
;
242 entry
= ((dvma
- iommu
->page_table_map_base
) >> IO_PAGE_SHIFT
);
244 spin_lock_irqsave(&iommu
->lock
, flags
);
246 arena_free(&iommu
->arena
, entry
, npages
);
251 num
= pci_sun4v_iommu_demap(devhandle
, HV_PCI_TSBID(0, entry
),
255 } while (npages
!= 0);
257 spin_unlock_irqrestore(&iommu
->lock
, flags
);
259 order
= get_order(size
);
261 free_pages((unsigned long)cpu
, order
);
264 static dma_addr_t
dma_4v_map_single(struct device
*dev
, void *ptr
, size_t sz
,
265 enum dma_data_direction direction
)
268 unsigned long flags
, npages
, oaddr
;
269 unsigned long i
, base_paddr
;
274 iommu
= dev
->archdata
.iommu
;
276 if (unlikely(direction
== DMA_NONE
))
279 oaddr
= (unsigned long)ptr
;
280 npages
= IO_PAGE_ALIGN(oaddr
+ sz
) - (oaddr
& IO_PAGE_MASK
);
281 npages
>>= IO_PAGE_SHIFT
;
283 spin_lock_irqsave(&iommu
->lock
, flags
);
284 entry
= arena_alloc(&iommu
->arena
, npages
);
285 spin_unlock_irqrestore(&iommu
->lock
, flags
);
287 if (unlikely(entry
< 0L))
290 bus_addr
= (iommu
->page_table_map_base
+
291 (entry
<< IO_PAGE_SHIFT
));
292 ret
= bus_addr
| (oaddr
& ~IO_PAGE_MASK
);
293 base_paddr
= __pa(oaddr
& IO_PAGE_MASK
);
294 prot
= HV_PCI_MAP_ATTR_READ
;
295 if (direction
!= DMA_TO_DEVICE
)
296 prot
|= HV_PCI_MAP_ATTR_WRITE
;
298 local_irq_save(flags
);
300 iommu_batch_start(dev
, prot
, entry
);
302 for (i
= 0; i
< npages
; i
++, base_paddr
+= IO_PAGE_SIZE
) {
303 long err
= iommu_batch_add(base_paddr
);
304 if (unlikely(err
< 0L))
307 if (unlikely(iommu_batch_end() < 0L))
310 local_irq_restore(flags
);
315 if (printk_ratelimit())
317 return DMA_ERROR_CODE
;
320 /* Interrupts are disabled. */
321 spin_lock(&iommu
->lock
);
322 arena_free(&iommu
->arena
, entry
, npages
);
323 spin_unlock_irqrestore(&iommu
->lock
, flags
);
325 return DMA_ERROR_CODE
;
328 static void dma_4v_unmap_single(struct device
*dev
, dma_addr_t bus_addr
,
329 size_t sz
, enum dma_data_direction direction
)
331 struct pci_pbm_info
*pbm
;
333 unsigned long flags
, npages
;
337 if (unlikely(direction
== DMA_NONE
)) {
338 if (printk_ratelimit())
343 iommu
= dev
->archdata
.iommu
;
344 pbm
= dev
->archdata
.host_controller
;
345 devhandle
= pbm
->devhandle
;
347 npages
= IO_PAGE_ALIGN(bus_addr
+ sz
) - (bus_addr
& IO_PAGE_MASK
);
348 npages
>>= IO_PAGE_SHIFT
;
349 bus_addr
&= IO_PAGE_MASK
;
351 spin_lock_irqsave(&iommu
->lock
, flags
);
353 entry
= (bus_addr
- iommu
->page_table_map_base
) >> IO_PAGE_SHIFT
;
354 arena_free(&iommu
->arena
, entry
, npages
);
359 num
= pci_sun4v_iommu_demap(devhandle
, HV_PCI_TSBID(0, entry
),
363 } while (npages
!= 0);
365 spin_unlock_irqrestore(&iommu
->lock
, flags
);
368 #define SG_ENT_PHYS_ADDRESS(SG) (__pa(sg_virt((SG))))
370 static long fill_sg(long entry
, struct device
*dev
,
371 struct scatterlist
*sg
,
372 int nused
, int nelems
, unsigned long prot
)
374 struct scatterlist
*dma_sg
= sg
;
378 local_irq_save(flags
);
380 iommu_batch_start(dev
, prot
, entry
);
382 for (i
= 0; i
< nused
; i
++) {
383 unsigned long pteval
= ~0UL;
386 dma_npages
= ((dma_sg
->dma_address
& (IO_PAGE_SIZE
- 1UL)) +
388 ((IO_PAGE_SIZE
- 1UL))) >> IO_PAGE_SHIFT
;
390 unsigned long offset
;
393 /* If we are here, we know we have at least one
394 * more page to map. So walk forward until we
395 * hit a page crossing, and begin creating new
396 * mappings from that spot.
401 tmp
= SG_ENT_PHYS_ADDRESS(sg
);
403 if (((tmp
^ pteval
) >> IO_PAGE_SHIFT
) != 0UL) {
404 pteval
= tmp
& IO_PAGE_MASK
;
405 offset
= tmp
& (IO_PAGE_SIZE
- 1UL);
408 if (((tmp
^ (tmp
+ len
- 1UL)) >> IO_PAGE_SHIFT
) != 0UL) {
409 pteval
= (tmp
+ IO_PAGE_SIZE
) & IO_PAGE_MASK
;
411 len
-= (IO_PAGE_SIZE
- (tmp
& (IO_PAGE_SIZE
- 1UL)));
418 pteval
= (pteval
& IOPTE_PAGE
);
422 err
= iommu_batch_add(pteval
);
423 if (unlikely(err
< 0L))
424 goto iommu_map_failed
;
426 pteval
+= IO_PAGE_SIZE
;
427 len
-= (IO_PAGE_SIZE
- offset
);
432 pteval
= (pteval
& IOPTE_PAGE
) + len
;
436 /* Skip over any tail mappings we've fully mapped,
437 * adjusting pteval along the way. Stop when we
438 * detect a page crossing event.
441 (pteval
<< (64 - IO_PAGE_SHIFT
)) != 0UL &&
442 (pteval
== SG_ENT_PHYS_ADDRESS(sg
)) &&
444 (SG_ENT_PHYS_ADDRESS(sg
) + sg
->length
- 1UL)) >> IO_PAGE_SHIFT
) == 0UL) {
445 pteval
+= sg
->length
;
449 if ((pteval
<< (64 - IO_PAGE_SHIFT
)) == 0UL)
451 } while (dma_npages
!= 0);
452 dma_sg
= sg_next(dma_sg
);
455 if (unlikely(iommu_batch_end() < 0L))
456 goto iommu_map_failed
;
458 local_irq_restore(flags
);
462 local_irq_restore(flags
);
466 static int dma_4v_map_sg(struct device
*dev
, struct scatterlist
*sglist
,
467 int nelems
, enum dma_data_direction direction
)
470 unsigned long flags
, npages
, prot
;
472 struct scatterlist
*sgtmp
;
476 /* Fast path single entry scatterlists. */
478 sglist
->dma_address
=
479 dma_4v_map_single(dev
, sg_virt(sglist
),
480 sglist
->length
, direction
);
481 if (unlikely(sglist
->dma_address
== DMA_ERROR_CODE
))
483 sglist
->dma_length
= sglist
->length
;
487 iommu
= dev
->archdata
.iommu
;
489 if (unlikely(direction
== DMA_NONE
))
492 /* Step 1: Prepare scatter list. */
493 npages
= prepare_sg(sglist
, nelems
);
495 /* Step 2: Allocate a cluster and context, if necessary. */
496 spin_lock_irqsave(&iommu
->lock
, flags
);
497 entry
= arena_alloc(&iommu
->arena
, npages
);
498 spin_unlock_irqrestore(&iommu
->lock
, flags
);
500 if (unlikely(entry
< 0L))
503 dma_base
= iommu
->page_table_map_base
+
504 (entry
<< IO_PAGE_SHIFT
);
506 /* Step 3: Normalize DMA addresses. */
510 while (used
&& sgtmp
->dma_length
) {
511 sgtmp
->dma_address
+= dma_base
;
512 sgtmp
= sg_next(sgtmp
);
515 used
= nelems
- used
;
517 /* Step 4: Create the mappings. */
518 prot
= HV_PCI_MAP_ATTR_READ
;
519 if (direction
!= DMA_TO_DEVICE
)
520 prot
|= HV_PCI_MAP_ATTR_WRITE
;
522 err
= fill_sg(entry
, dev
, sglist
, used
, nelems
, prot
);
523 if (unlikely(err
< 0L))
524 goto iommu_map_failed
;
529 if (printk_ratelimit())
534 spin_lock_irqsave(&iommu
->lock
, flags
);
535 arena_free(&iommu
->arena
, entry
, npages
);
536 spin_unlock_irqrestore(&iommu
->lock
, flags
);
541 static void dma_4v_unmap_sg(struct device
*dev
, struct scatterlist
*sglist
,
542 int nelems
, enum dma_data_direction direction
)
544 struct pci_pbm_info
*pbm
;
546 unsigned long flags
, i
, npages
;
547 struct scatterlist
*sg
, *sgprv
;
549 u32 devhandle
, bus_addr
;
551 if (unlikely(direction
== DMA_NONE
)) {
552 if (printk_ratelimit())
556 iommu
= dev
->archdata
.iommu
;
557 pbm
= dev
->archdata
.host_controller
;
558 devhandle
= pbm
->devhandle
;
560 bus_addr
= sglist
->dma_address
& IO_PAGE_MASK
;
562 for_each_sg(sglist
, sg
, nelems
, i
) {
563 if (sg
->dma_length
== 0)
569 npages
= (IO_PAGE_ALIGN(sgprv
->dma_address
+ sgprv
->dma_length
) -
570 bus_addr
) >> IO_PAGE_SHIFT
;
572 entry
= ((bus_addr
- iommu
->page_table_map_base
) >> IO_PAGE_SHIFT
);
574 spin_lock_irqsave(&iommu
->lock
, flags
);
576 arena_free(&iommu
->arena
, entry
, npages
);
581 num
= pci_sun4v_iommu_demap(devhandle
, HV_PCI_TSBID(0, entry
),
585 } while (npages
!= 0);
587 spin_unlock_irqrestore(&iommu
->lock
, flags
);
590 static void dma_4v_sync_single_for_cpu(struct device
*dev
,
591 dma_addr_t bus_addr
, size_t sz
,
592 enum dma_data_direction direction
)
594 /* Nothing to do... */
597 static void dma_4v_sync_sg_for_cpu(struct device
*dev
,
598 struct scatterlist
*sglist
, int nelems
,
599 enum dma_data_direction direction
)
601 /* Nothing to do... */
604 const struct dma_ops sun4v_dma_ops
= {
605 .alloc_coherent
= dma_4v_alloc_coherent
,
606 .free_coherent
= dma_4v_free_coherent
,
607 .map_single
= dma_4v_map_single
,
608 .unmap_single
= dma_4v_unmap_single
,
609 .map_sg
= dma_4v_map_sg
,
610 .unmap_sg
= dma_4v_unmap_sg
,
611 .sync_single_for_cpu
= dma_4v_sync_single_for_cpu
,
612 .sync_sg_for_cpu
= dma_4v_sync_sg_for_cpu
,
615 static void __init
pci_sun4v_scan_bus(struct pci_pbm_info
*pbm
)
617 struct property
*prop
;
618 struct device_node
*dp
;
621 prop
= of_find_property(dp
, "66mhz-capable", NULL
);
622 pbm
->is_66mhz_capable
= (prop
!= NULL
);
623 pbm
->pci_bus
= pci_scan_one_pbm(pbm
);
625 /* XXX register error interrupt handlers XXX */
628 static unsigned long probe_existing_entries(struct pci_pbm_info
*pbm
,
631 struct iommu_arena
*arena
= &iommu
->arena
;
632 unsigned long i
, cnt
= 0;
635 devhandle
= pbm
->devhandle
;
636 for (i
= 0; i
< arena
->limit
; i
++) {
637 unsigned long ret
, io_attrs
, ra
;
639 ret
= pci_sun4v_iommu_getmap(devhandle
,
643 if (page_in_phys_avail(ra
)) {
644 pci_sun4v_iommu_demap(devhandle
,
645 HV_PCI_TSBID(0, i
), 1);
648 __set_bit(i
, arena
->map
);
656 static void pci_sun4v_iommu_init(struct pci_pbm_info
*pbm
)
658 struct iommu
*iommu
= pbm
->iommu
;
659 struct property
*prop
;
660 unsigned long num_tsb_entries
, sz
, tsbsize
;
661 u32 vdma
[2], dma_mask
, dma_offset
;
663 prop
= of_find_property(pbm
->prom_node
, "virtual-dma", NULL
);
665 u32
*val
= prop
->value
;
670 /* No property, use default values. */
671 vdma
[0] = 0x80000000;
672 vdma
[1] = 0x80000000;
675 if ((vdma
[0] | vdma
[1]) & ~IO_PAGE_MASK
) {
676 prom_printf("PCI-SUN4V: strange virtual-dma[%08x:%08x].\n",
681 dma_mask
= (roundup_pow_of_two(vdma
[1]) - 1UL);
682 num_tsb_entries
= vdma
[1] / IO_PAGE_SIZE
;
683 tsbsize
= num_tsb_entries
* sizeof(iopte_t
);
685 dma_offset
= vdma
[0];
687 /* Setup initial software IOMMU state. */
688 spin_lock_init(&iommu
->lock
);
689 iommu
->ctx_lowest_free
= 1;
690 iommu
->page_table_map_base
= dma_offset
;
691 iommu
->dma_addr_mask
= dma_mask
;
693 /* Allocate and initialize the free area map. */
694 sz
= (num_tsb_entries
+ 7) / 8;
695 sz
= (sz
+ 7UL) & ~7UL;
696 iommu
->arena
.map
= kzalloc(sz
, GFP_KERNEL
);
697 if (!iommu
->arena
.map
) {
698 prom_printf("PCI_IOMMU: Error, kmalloc(arena.map) failed.\n");
701 iommu
->arena
.limit
= num_tsb_entries
;
703 sz
= probe_existing_entries(pbm
, iommu
);
705 printk("%s: Imported %lu TSB entries from OBP\n",
709 #ifdef CONFIG_PCI_MSI
710 struct pci_sun4v_msiq_entry
{
712 #define MSIQ_VERSION_MASK 0xffffffff00000000UL
713 #define MSIQ_VERSION_SHIFT 32
714 #define MSIQ_TYPE_MASK 0x00000000000000ffUL
715 #define MSIQ_TYPE_SHIFT 0
716 #define MSIQ_TYPE_NONE 0x00
717 #define MSIQ_TYPE_MSG 0x01
718 #define MSIQ_TYPE_MSI32 0x02
719 #define MSIQ_TYPE_MSI64 0x03
720 #define MSIQ_TYPE_INTX 0x08
721 #define MSIQ_TYPE_NONE2 0xff
726 u64 req_id
; /* bus/device/func */
727 #define MSIQ_REQID_BUS_MASK 0xff00UL
728 #define MSIQ_REQID_BUS_SHIFT 8
729 #define MSIQ_REQID_DEVICE_MASK 0x00f8UL
730 #define MSIQ_REQID_DEVICE_SHIFT 3
731 #define MSIQ_REQID_FUNC_MASK 0x0007UL
732 #define MSIQ_REQID_FUNC_SHIFT 0
736 /* The format of this value is message type dependent.
737 * For MSI bits 15:0 are the data from the MSI packet.
738 * For MSI-X bits 31:0 are the data from the MSI packet.
739 * For MSG, the message code and message routing code where:
740 * bits 39:32 is the bus/device/fn of the msg target-id
741 * bits 18:16 is the message routing code
742 * bits 7:0 is the message code
743 * For INTx the low order 2-bits are:
754 static int pci_sun4v_get_head(struct pci_pbm_info
*pbm
, unsigned long msiqid
,
757 unsigned long err
, limit
;
759 err
= pci_sun4v_msiq_gethead(pbm
->devhandle
, msiqid
, head
);
763 limit
= pbm
->msiq_ent_count
* sizeof(struct pci_sun4v_msiq_entry
);
764 if (unlikely(*head
>= limit
))
770 static int pci_sun4v_dequeue_msi(struct pci_pbm_info
*pbm
,
771 unsigned long msiqid
, unsigned long *head
,
774 struct pci_sun4v_msiq_entry
*ep
;
775 unsigned long err
, type
;
777 /* Note: void pointer arithmetic, 'head' is a byte offset */
778 ep
= (pbm
->msi_queues
+ ((msiqid
- pbm
->msiq_first
) *
779 (pbm
->msiq_ent_count
*
780 sizeof(struct pci_sun4v_msiq_entry
))) +
783 if ((ep
->version_type
& MSIQ_TYPE_MASK
) == 0)
786 type
= (ep
->version_type
& MSIQ_TYPE_MASK
) >> MSIQ_TYPE_SHIFT
;
787 if (unlikely(type
!= MSIQ_TYPE_MSI32
&&
788 type
!= MSIQ_TYPE_MSI64
))
793 err
= pci_sun4v_msi_setstate(pbm
->devhandle
,
794 ep
->msi_data
/* msi_num */,
799 /* Clear the entry. */
800 ep
->version_type
&= ~MSIQ_TYPE_MASK
;
802 (*head
) += sizeof(struct pci_sun4v_msiq_entry
);
804 (pbm
->msiq_ent_count
* sizeof(struct pci_sun4v_msiq_entry
)))
810 static int pci_sun4v_set_head(struct pci_pbm_info
*pbm
, unsigned long msiqid
,
815 err
= pci_sun4v_msiq_sethead(pbm
->devhandle
, msiqid
, head
);
822 static int pci_sun4v_msi_setup(struct pci_pbm_info
*pbm
, unsigned long msiqid
,
823 unsigned long msi
, int is_msi64
)
825 if (pci_sun4v_msi_setmsiq(pbm
->devhandle
, msi
, msiqid
,
827 HV_MSITYPE_MSI64
: HV_MSITYPE_MSI32
)))
829 if (pci_sun4v_msi_setstate(pbm
->devhandle
, msi
, HV_MSISTATE_IDLE
))
831 if (pci_sun4v_msi_setvalid(pbm
->devhandle
, msi
, HV_MSIVALID_VALID
))
836 static int pci_sun4v_msi_teardown(struct pci_pbm_info
*pbm
, unsigned long msi
)
838 unsigned long err
, msiqid
;
840 err
= pci_sun4v_msi_getmsiq(pbm
->devhandle
, msi
, &msiqid
);
844 pci_sun4v_msi_setvalid(pbm
->devhandle
, msi
, HV_MSIVALID_INVALID
);
849 static int pci_sun4v_msiq_alloc(struct pci_pbm_info
*pbm
)
851 unsigned long q_size
, alloc_size
, pages
, order
;
854 q_size
= pbm
->msiq_ent_count
* sizeof(struct pci_sun4v_msiq_entry
);
855 alloc_size
= (pbm
->msiq_num
* q_size
);
856 order
= get_order(alloc_size
);
857 pages
= __get_free_pages(GFP_KERNEL
| __GFP_COMP
, order
);
859 printk(KERN_ERR
"MSI: Cannot allocate MSI queues (o=%lu).\n",
863 memset((char *)pages
, 0, PAGE_SIZE
<< order
);
864 pbm
->msi_queues
= (void *) pages
;
866 for (i
= 0; i
< pbm
->msiq_num
; i
++) {
867 unsigned long err
, base
= __pa(pages
+ (i
* q_size
));
868 unsigned long ret1
, ret2
;
870 err
= pci_sun4v_msiq_conf(pbm
->devhandle
,
872 base
, pbm
->msiq_ent_count
);
874 printk(KERN_ERR
"MSI: msiq register fails (err=%lu)\n",
879 err
= pci_sun4v_msiq_info(pbm
->devhandle
,
883 printk(KERN_ERR
"MSI: Cannot read msiq (err=%lu)\n",
887 if (ret1
!= base
|| ret2
!= pbm
->msiq_ent_count
) {
888 printk(KERN_ERR
"MSI: Bogus qconf "
889 "expected[%lx:%x] got[%lx:%lx]\n",
890 base
, pbm
->msiq_ent_count
,
899 free_pages(pages
, order
);
903 static void pci_sun4v_msiq_free(struct pci_pbm_info
*pbm
)
905 unsigned long q_size
, alloc_size
, pages
, order
;
908 for (i
= 0; i
< pbm
->msiq_num
; i
++) {
909 unsigned long msiqid
= pbm
->msiq_first
+ i
;
911 (void) pci_sun4v_msiq_conf(pbm
->devhandle
, msiqid
, 0UL, 0);
914 q_size
= pbm
->msiq_ent_count
* sizeof(struct pci_sun4v_msiq_entry
);
915 alloc_size
= (pbm
->msiq_num
* q_size
);
916 order
= get_order(alloc_size
);
918 pages
= (unsigned long) pbm
->msi_queues
;
920 free_pages(pages
, order
);
922 pbm
->msi_queues
= NULL
;
925 static int pci_sun4v_msiq_build_irq(struct pci_pbm_info
*pbm
,
926 unsigned long msiqid
,
927 unsigned long devino
)
929 unsigned int virt_irq
= sun4v_build_irq(pbm
->devhandle
, devino
);
934 if (pci_sun4v_msiq_setstate(pbm
->devhandle
, msiqid
, HV_MSIQSTATE_IDLE
))
936 if (pci_sun4v_msiq_setvalid(pbm
->devhandle
, msiqid
, HV_MSIQ_VALID
))
942 static const struct sparc64_msiq_ops pci_sun4v_msiq_ops
= {
943 .get_head
= pci_sun4v_get_head
,
944 .dequeue_msi
= pci_sun4v_dequeue_msi
,
945 .set_head
= pci_sun4v_set_head
,
946 .msi_setup
= pci_sun4v_msi_setup
,
947 .msi_teardown
= pci_sun4v_msi_teardown
,
948 .msiq_alloc
= pci_sun4v_msiq_alloc
,
949 .msiq_free
= pci_sun4v_msiq_free
,
950 .msiq_build_irq
= pci_sun4v_msiq_build_irq
,
953 static void pci_sun4v_msi_init(struct pci_pbm_info
*pbm
)
955 sparc64_pbm_msi_init(pbm
, &pci_sun4v_msiq_ops
);
957 #else /* CONFIG_PCI_MSI */
958 static void pci_sun4v_msi_init(struct pci_pbm_info
*pbm
)
961 #endif /* !(CONFIG_PCI_MSI) */
963 static void __init
pci_sun4v_pbm_init(struct pci_controller_info
*p
,
964 struct device_node
*dp
, u32 devhandle
)
966 struct pci_pbm_info
*pbm
;
968 if (devhandle
& 0x40)
973 pbm
->next
= pci_pbm_root
;
976 pbm
->scan_bus
= pci_sun4v_scan_bus
;
977 pbm
->pci_ops
= &sun4v_pci_ops
;
978 pbm
->config_space_reg_bits
= 12;
980 pbm
->index
= pci_num_pbms
++;
985 pbm
->devhandle
= devhandle
;
987 pbm
->name
= dp
->full_name
;
989 printk("%s: SUN4V PCI Bus Module\n", pbm
->name
);
991 pci_determine_mem_io_space(pbm
);
993 pci_get_pbm_props(pbm
);
994 pci_sun4v_iommu_init(pbm
);
995 pci_sun4v_msi_init(pbm
);
998 void __init
sun4v_pci_init(struct device_node
*dp
, char *model_name
)
1000 static int hvapi_negotiated
= 0;
1001 struct pci_controller_info
*p
;
1002 struct pci_pbm_info
*pbm
;
1003 struct iommu
*iommu
;
1004 struct property
*prop
;
1005 struct linux_prom64_registers
*regs
;
1009 if (!hvapi_negotiated
++) {
1010 int err
= sun4v_hvapi_register(HV_GRP_PCI
,
1015 prom_printf("SUN4V_PCI: Could not register hvapi, "
1019 printk("SUN4V_PCI: Registered hvapi major[%lu] minor[%lu]\n",
1020 vpci_major
, vpci_minor
);
1022 dma_ops
= &sun4v_dma_ops
;
1025 prop
= of_find_property(dp
, "reg", NULL
);
1027 prom_printf("SUN4V_PCI: Could not find config registers\n");
1032 devhandle
= (regs
->phys_addr
>> 32UL) & 0x0fffffff;
1034 for (pbm
= pci_pbm_root
; pbm
; pbm
= pbm
->next
) {
1035 if (pbm
->devhandle
== (devhandle
^ 0x40)) {
1036 pci_sun4v_pbm_init(pbm
->parent
, dp
, devhandle
);
1041 for_each_possible_cpu(i
) {
1042 unsigned long page
= get_zeroed_page(GFP_ATOMIC
);
1045 goto fatal_memory_error
;
1047 per_cpu(iommu_batch
, i
).pglist
= (u64
*) page
;
1050 p
= kzalloc(sizeof(struct pci_controller_info
), GFP_ATOMIC
);
1052 goto fatal_memory_error
;
1054 iommu
= kzalloc(sizeof(struct iommu
), GFP_ATOMIC
);
1056 goto fatal_memory_error
;
1058 p
->pbm_A
.iommu
= iommu
;
1060 iommu
= kzalloc(sizeof(struct iommu
), GFP_ATOMIC
);
1062 goto fatal_memory_error
;
1064 p
->pbm_B
.iommu
= iommu
;
1066 pci_sun4v_pbm_init(p
, dp
, devhandle
);
1070 prom_printf("SUN4V_PCI: Fatal memory allocation error.\n");