1 /* pci_sun4v.c: SUN4V specific PCI controller support.
3 * Copyright (C) 2006, 2007, 2008 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 static inline void iommu_batch_new_entry(unsigned long entry
)
94 struct iommu_batch
*p
= &__get_cpu_var(iommu_batch
);
96 if (p
->entry
+ p
->npages
== entry
)
103 /* Interrupts must be disabled. */
104 static inline long iommu_batch_add(u64 phys_page
)
106 struct iommu_batch
*p
= &__get_cpu_var(iommu_batch
);
108 BUG_ON(p
->npages
>= PGLIST_NENTS
);
110 p
->pglist
[p
->npages
++] = phys_page
;
111 if (p
->npages
== PGLIST_NENTS
)
112 return iommu_batch_flush(p
);
117 /* Interrupts must be disabled. */
118 static inline long iommu_batch_end(void)
120 struct iommu_batch
*p
= &__get_cpu_var(iommu_batch
);
122 BUG_ON(p
->npages
>= PGLIST_NENTS
);
124 return iommu_batch_flush(p
);
127 static void *dma_4v_alloc_coherent(struct device
*dev
, size_t size
,
128 dma_addr_t
*dma_addrp
, gfp_t gfp
)
131 unsigned long flags
, order
, first_page
, npages
, n
;
135 size
= IO_PAGE_ALIGN(size
);
136 order
= get_order(size
);
137 if (unlikely(order
>= MAX_ORDER
))
140 npages
= size
>> IO_PAGE_SHIFT
;
142 first_page
= __get_free_pages(gfp
, order
);
143 if (unlikely(first_page
== 0UL))
146 memset((char *)first_page
, 0, PAGE_SIZE
<< order
);
148 iommu
= dev
->archdata
.iommu
;
150 spin_lock_irqsave(&iommu
->lock
, flags
);
151 entry
= iommu_range_alloc(dev
, iommu
, npages
, NULL
);
152 spin_unlock_irqrestore(&iommu
->lock
, flags
);
154 if (unlikely(entry
== DMA_ERROR_CODE
))
155 goto range_alloc_fail
;
157 *dma_addrp
= (iommu
->page_table_map_base
+
158 (entry
<< IO_PAGE_SHIFT
));
159 ret
= (void *) first_page
;
160 first_page
= __pa(first_page
);
162 local_irq_save(flags
);
164 iommu_batch_start(dev
,
165 (HV_PCI_MAP_ATTR_READ
|
166 HV_PCI_MAP_ATTR_WRITE
),
169 for (n
= 0; n
< npages
; n
++) {
170 long err
= iommu_batch_add(first_page
+ (n
* PAGE_SIZE
));
171 if (unlikely(err
< 0L))
175 if (unlikely(iommu_batch_end() < 0L))
178 local_irq_restore(flags
);
183 /* Interrupts are disabled. */
184 spin_lock(&iommu
->lock
);
185 iommu_range_free(iommu
, *dma_addrp
, npages
);
186 spin_unlock_irqrestore(&iommu
->lock
, flags
);
189 free_pages(first_page
, order
);
193 static void dma_4v_free_coherent(struct device
*dev
, size_t size
, void *cpu
,
196 struct pci_pbm_info
*pbm
;
198 unsigned long flags
, order
, npages
, entry
;
201 npages
= IO_PAGE_ALIGN(size
) >> IO_PAGE_SHIFT
;
202 iommu
= dev
->archdata
.iommu
;
203 pbm
= dev
->archdata
.host_controller
;
204 devhandle
= pbm
->devhandle
;
205 entry
= ((dvma
- iommu
->page_table_map_base
) >> IO_PAGE_SHIFT
);
207 spin_lock_irqsave(&iommu
->lock
, flags
);
209 iommu_range_free(iommu
, dvma
, npages
);
214 num
= pci_sun4v_iommu_demap(devhandle
, HV_PCI_TSBID(0, entry
),
218 } while (npages
!= 0);
220 spin_unlock_irqrestore(&iommu
->lock
, flags
);
222 order
= get_order(size
);
224 free_pages((unsigned long)cpu
, order
);
227 static dma_addr_t
dma_4v_map_single(struct device
*dev
, void *ptr
, size_t sz
,
228 enum dma_data_direction direction
)
231 unsigned long flags
, npages
, oaddr
;
232 unsigned long i
, base_paddr
;
237 iommu
= dev
->archdata
.iommu
;
239 if (unlikely(direction
== DMA_NONE
))
242 oaddr
= (unsigned long)ptr
;
243 npages
= IO_PAGE_ALIGN(oaddr
+ sz
) - (oaddr
& IO_PAGE_MASK
);
244 npages
>>= IO_PAGE_SHIFT
;
246 spin_lock_irqsave(&iommu
->lock
, flags
);
247 entry
= iommu_range_alloc(dev
, iommu
, npages
, NULL
);
248 spin_unlock_irqrestore(&iommu
->lock
, flags
);
250 if (unlikely(entry
== DMA_ERROR_CODE
))
253 bus_addr
= (iommu
->page_table_map_base
+
254 (entry
<< IO_PAGE_SHIFT
));
255 ret
= bus_addr
| (oaddr
& ~IO_PAGE_MASK
);
256 base_paddr
= __pa(oaddr
& IO_PAGE_MASK
);
257 prot
= HV_PCI_MAP_ATTR_READ
;
258 if (direction
!= DMA_TO_DEVICE
)
259 prot
|= HV_PCI_MAP_ATTR_WRITE
;
261 local_irq_save(flags
);
263 iommu_batch_start(dev
, prot
, entry
);
265 for (i
= 0; i
< npages
; i
++, base_paddr
+= IO_PAGE_SIZE
) {
266 long err
= iommu_batch_add(base_paddr
);
267 if (unlikely(err
< 0L))
270 if (unlikely(iommu_batch_end() < 0L))
273 local_irq_restore(flags
);
278 if (printk_ratelimit())
280 return DMA_ERROR_CODE
;
283 /* Interrupts are disabled. */
284 spin_lock(&iommu
->lock
);
285 iommu_range_free(iommu
, bus_addr
, npages
);
286 spin_unlock_irqrestore(&iommu
->lock
, flags
);
288 return DMA_ERROR_CODE
;
291 static void dma_4v_unmap_single(struct device
*dev
, dma_addr_t bus_addr
,
292 size_t sz
, enum dma_data_direction direction
)
294 struct pci_pbm_info
*pbm
;
296 unsigned long flags
, npages
;
300 if (unlikely(direction
== DMA_NONE
)) {
301 if (printk_ratelimit())
306 iommu
= dev
->archdata
.iommu
;
307 pbm
= dev
->archdata
.host_controller
;
308 devhandle
= pbm
->devhandle
;
310 npages
= IO_PAGE_ALIGN(bus_addr
+ sz
) - (bus_addr
& IO_PAGE_MASK
);
311 npages
>>= IO_PAGE_SHIFT
;
312 bus_addr
&= IO_PAGE_MASK
;
314 spin_lock_irqsave(&iommu
->lock
, flags
);
316 iommu_range_free(iommu
, bus_addr
, npages
);
318 entry
= (bus_addr
- iommu
->page_table_map_base
) >> IO_PAGE_SHIFT
;
322 num
= pci_sun4v_iommu_demap(devhandle
, HV_PCI_TSBID(0, entry
),
326 } while (npages
!= 0);
328 spin_unlock_irqrestore(&iommu
->lock
, flags
);
331 static int dma_4v_map_sg(struct device
*dev
, struct scatterlist
*sglist
,
332 int nelems
, enum dma_data_direction direction
)
334 struct scatterlist
*s
, *outs
, *segstart
;
335 unsigned long flags
, handle
, prot
;
336 dma_addr_t dma_next
= 0, dma_addr
;
337 unsigned int max_seg_size
;
338 int outcount
, incount
, i
;
342 BUG_ON(direction
== DMA_NONE
);
344 iommu
= dev
->archdata
.iommu
;
345 if (nelems
== 0 || !iommu
)
348 prot
= HV_PCI_MAP_ATTR_READ
;
349 if (direction
!= DMA_TO_DEVICE
)
350 prot
|= HV_PCI_MAP_ATTR_WRITE
;
352 outs
= s
= segstart
= &sglist
[0];
357 /* Init first segment length for backout at failure */
358 outs
->dma_length
= 0;
360 spin_lock_irqsave(&iommu
->lock
, flags
);
362 iommu_batch_start(dev
, prot
, ~0UL);
364 max_seg_size
= dma_get_max_seg_size(dev
);
365 for_each_sg(sglist
, s
, nelems
, i
) {
366 unsigned long paddr
, npages
, entry
, slen
;
374 /* Allocate iommu entries for that segment */
375 paddr
= (unsigned long) SG_ENT_PHYS_ADDRESS(s
);
376 npages
= iommu_num_pages(paddr
, slen
);
377 entry
= iommu_range_alloc(dev
, iommu
, npages
, &handle
);
380 if (unlikely(entry
== DMA_ERROR_CODE
)) {
381 if (printk_ratelimit())
382 printk(KERN_INFO
"iommu_alloc failed, iommu %p paddr %lx"
383 " npages %lx\n", iommu
, paddr
, npages
);
384 goto iommu_map_failed
;
387 iommu_batch_new_entry(entry
);
389 /* Convert entry to a dma_addr_t */
390 dma_addr
= iommu
->page_table_map_base
+
391 (entry
<< IO_PAGE_SHIFT
);
392 dma_addr
|= (s
->offset
& ~IO_PAGE_MASK
);
394 /* Insert into HW table */
395 paddr
&= IO_PAGE_MASK
;
397 err
= iommu_batch_add(paddr
);
398 if (unlikely(err
< 0L))
399 goto iommu_map_failed
;
400 paddr
+= IO_PAGE_SIZE
;
403 /* If we are in an open segment, try merging */
405 /* We cannot merge if:
406 * - allocated dma_addr isn't contiguous to previous allocation
408 if ((dma_addr
!= dma_next
) ||
409 (outs
->dma_length
+ s
->length
> max_seg_size
)) {
410 /* Can't merge: create a new segment */
413 outs
= sg_next(outs
);
415 outs
->dma_length
+= s
->length
;
420 /* This is a new segment, fill entries */
421 outs
->dma_address
= dma_addr
;
422 outs
->dma_length
= slen
;
425 /* Calculate next page pointer for contiguous check */
426 dma_next
= dma_addr
+ slen
;
429 err
= iommu_batch_end();
431 if (unlikely(err
< 0L))
432 goto iommu_map_failed
;
434 spin_unlock_irqrestore(&iommu
->lock
, flags
);
436 if (outcount
< incount
) {
437 outs
= sg_next(outs
);
438 outs
->dma_address
= DMA_ERROR_CODE
;
439 outs
->dma_length
= 0;
445 for_each_sg(sglist
, s
, nelems
, i
) {
446 if (s
->dma_length
!= 0) {
447 unsigned long vaddr
, npages
;
449 vaddr
= s
->dma_address
& IO_PAGE_MASK
;
450 npages
= iommu_num_pages(s
->dma_address
, s
->dma_length
);
451 iommu_range_free(iommu
, vaddr
, npages
);
453 s
->dma_address
= DMA_ERROR_CODE
;
459 spin_unlock_irqrestore(&iommu
->lock
, flags
);
464 static void dma_4v_unmap_sg(struct device
*dev
, struct scatterlist
*sglist
,
465 int nelems
, enum dma_data_direction direction
)
467 struct pci_pbm_info
*pbm
;
468 struct scatterlist
*sg
;
473 BUG_ON(direction
== DMA_NONE
);
475 iommu
= dev
->archdata
.iommu
;
476 pbm
= dev
->archdata
.host_controller
;
477 devhandle
= pbm
->devhandle
;
479 spin_lock_irqsave(&iommu
->lock
, flags
);
483 dma_addr_t dma_handle
= sg
->dma_address
;
484 unsigned int len
= sg
->dma_length
;
485 unsigned long npages
, entry
;
489 npages
= iommu_num_pages(dma_handle
, len
);
490 iommu_range_free(iommu
, dma_handle
, npages
);
492 entry
= ((dma_handle
- iommu
->page_table_map_base
) >> IO_PAGE_SHIFT
);
496 num
= pci_sun4v_iommu_demap(devhandle
, HV_PCI_TSBID(0, entry
),
505 spin_unlock_irqrestore(&iommu
->lock
, flags
);
508 static void dma_4v_sync_single_for_cpu(struct device
*dev
,
509 dma_addr_t bus_addr
, size_t sz
,
510 enum dma_data_direction direction
)
512 /* Nothing to do... */
515 static void dma_4v_sync_sg_for_cpu(struct device
*dev
,
516 struct scatterlist
*sglist
, int nelems
,
517 enum dma_data_direction direction
)
519 /* Nothing to do... */
522 const struct dma_ops sun4v_dma_ops
= {
523 .alloc_coherent
= dma_4v_alloc_coherent
,
524 .free_coherent
= dma_4v_free_coherent
,
525 .map_single
= dma_4v_map_single
,
526 .unmap_single
= dma_4v_unmap_single
,
527 .map_sg
= dma_4v_map_sg
,
528 .unmap_sg
= dma_4v_unmap_sg
,
529 .sync_single_for_cpu
= dma_4v_sync_single_for_cpu
,
530 .sync_sg_for_cpu
= dma_4v_sync_sg_for_cpu
,
533 static void __init
pci_sun4v_scan_bus(struct pci_pbm_info
*pbm
)
535 struct property
*prop
;
536 struct device_node
*dp
;
539 prop
= of_find_property(dp
, "66mhz-capable", NULL
);
540 pbm
->is_66mhz_capable
= (prop
!= NULL
);
541 pbm
->pci_bus
= pci_scan_one_pbm(pbm
);
543 /* XXX register error interrupt handlers XXX */
546 static unsigned long __init
probe_existing_entries(struct pci_pbm_info
*pbm
,
549 struct iommu_arena
*arena
= &iommu
->arena
;
550 unsigned long i
, cnt
= 0;
553 devhandle
= pbm
->devhandle
;
554 for (i
= 0; i
< arena
->limit
; i
++) {
555 unsigned long ret
, io_attrs
, ra
;
557 ret
= pci_sun4v_iommu_getmap(devhandle
,
561 if (page_in_phys_avail(ra
)) {
562 pci_sun4v_iommu_demap(devhandle
,
563 HV_PCI_TSBID(0, i
), 1);
566 __set_bit(i
, arena
->map
);
574 static void __init
pci_sun4v_iommu_init(struct pci_pbm_info
*pbm
)
576 struct iommu
*iommu
= pbm
->iommu
;
577 struct property
*prop
;
578 unsigned long num_tsb_entries
, sz
, tsbsize
;
579 u32 vdma
[2], dma_mask
, dma_offset
;
581 prop
= of_find_property(pbm
->prom_node
, "virtual-dma", NULL
);
583 u32
*val
= prop
->value
;
588 /* No property, use default values. */
589 vdma
[0] = 0x80000000;
590 vdma
[1] = 0x80000000;
593 if ((vdma
[0] | vdma
[1]) & ~IO_PAGE_MASK
) {
594 prom_printf("PCI-SUN4V: strange virtual-dma[%08x:%08x].\n",
599 dma_mask
= (roundup_pow_of_two(vdma
[1]) - 1UL);
600 num_tsb_entries
= vdma
[1] / IO_PAGE_SIZE
;
601 tsbsize
= num_tsb_entries
* sizeof(iopte_t
);
603 dma_offset
= vdma
[0];
605 /* Setup initial software IOMMU state. */
606 spin_lock_init(&iommu
->lock
);
607 iommu
->ctx_lowest_free
= 1;
608 iommu
->page_table_map_base
= dma_offset
;
609 iommu
->dma_addr_mask
= dma_mask
;
611 /* Allocate and initialize the free area map. */
612 sz
= (num_tsb_entries
+ 7) / 8;
613 sz
= (sz
+ 7UL) & ~7UL;
614 iommu
->arena
.map
= kzalloc(sz
, GFP_KERNEL
);
615 if (!iommu
->arena
.map
) {
616 prom_printf("PCI_IOMMU: Error, kmalloc(arena.map) failed.\n");
619 iommu
->arena
.limit
= num_tsb_entries
;
621 sz
= probe_existing_entries(pbm
, iommu
);
623 printk("%s: Imported %lu TSB entries from OBP\n",
627 #ifdef CONFIG_PCI_MSI
628 struct pci_sun4v_msiq_entry
{
630 #define MSIQ_VERSION_MASK 0xffffffff00000000UL
631 #define MSIQ_VERSION_SHIFT 32
632 #define MSIQ_TYPE_MASK 0x00000000000000ffUL
633 #define MSIQ_TYPE_SHIFT 0
634 #define MSIQ_TYPE_NONE 0x00
635 #define MSIQ_TYPE_MSG 0x01
636 #define MSIQ_TYPE_MSI32 0x02
637 #define MSIQ_TYPE_MSI64 0x03
638 #define MSIQ_TYPE_INTX 0x08
639 #define MSIQ_TYPE_NONE2 0xff
644 u64 req_id
; /* bus/device/func */
645 #define MSIQ_REQID_BUS_MASK 0xff00UL
646 #define MSIQ_REQID_BUS_SHIFT 8
647 #define MSIQ_REQID_DEVICE_MASK 0x00f8UL
648 #define MSIQ_REQID_DEVICE_SHIFT 3
649 #define MSIQ_REQID_FUNC_MASK 0x0007UL
650 #define MSIQ_REQID_FUNC_SHIFT 0
654 /* The format of this value is message type dependent.
655 * For MSI bits 15:0 are the data from the MSI packet.
656 * For MSI-X bits 31:0 are the data from the MSI packet.
657 * For MSG, the message code and message routing code where:
658 * bits 39:32 is the bus/device/fn of the msg target-id
659 * bits 18:16 is the message routing code
660 * bits 7:0 is the message code
661 * For INTx the low order 2-bits are:
672 static int pci_sun4v_get_head(struct pci_pbm_info
*pbm
, unsigned long msiqid
,
675 unsigned long err
, limit
;
677 err
= pci_sun4v_msiq_gethead(pbm
->devhandle
, msiqid
, head
);
681 limit
= pbm
->msiq_ent_count
* sizeof(struct pci_sun4v_msiq_entry
);
682 if (unlikely(*head
>= limit
))
688 static int pci_sun4v_dequeue_msi(struct pci_pbm_info
*pbm
,
689 unsigned long msiqid
, unsigned long *head
,
692 struct pci_sun4v_msiq_entry
*ep
;
693 unsigned long err
, type
;
695 /* Note: void pointer arithmetic, 'head' is a byte offset */
696 ep
= (pbm
->msi_queues
+ ((msiqid
- pbm
->msiq_first
) *
697 (pbm
->msiq_ent_count
*
698 sizeof(struct pci_sun4v_msiq_entry
))) +
701 if ((ep
->version_type
& MSIQ_TYPE_MASK
) == 0)
704 type
= (ep
->version_type
& MSIQ_TYPE_MASK
) >> MSIQ_TYPE_SHIFT
;
705 if (unlikely(type
!= MSIQ_TYPE_MSI32
&&
706 type
!= MSIQ_TYPE_MSI64
))
711 err
= pci_sun4v_msi_setstate(pbm
->devhandle
,
712 ep
->msi_data
/* msi_num */,
717 /* Clear the entry. */
718 ep
->version_type
&= ~MSIQ_TYPE_MASK
;
720 (*head
) += sizeof(struct pci_sun4v_msiq_entry
);
722 (pbm
->msiq_ent_count
* sizeof(struct pci_sun4v_msiq_entry
)))
728 static int pci_sun4v_set_head(struct pci_pbm_info
*pbm
, unsigned long msiqid
,
733 err
= pci_sun4v_msiq_sethead(pbm
->devhandle
, msiqid
, head
);
740 static int pci_sun4v_msi_setup(struct pci_pbm_info
*pbm
, unsigned long msiqid
,
741 unsigned long msi
, int is_msi64
)
743 if (pci_sun4v_msi_setmsiq(pbm
->devhandle
, msi
, msiqid
,
745 HV_MSITYPE_MSI64
: HV_MSITYPE_MSI32
)))
747 if (pci_sun4v_msi_setstate(pbm
->devhandle
, msi
, HV_MSISTATE_IDLE
))
749 if (pci_sun4v_msi_setvalid(pbm
->devhandle
, msi
, HV_MSIVALID_VALID
))
754 static int pci_sun4v_msi_teardown(struct pci_pbm_info
*pbm
, unsigned long msi
)
756 unsigned long err
, msiqid
;
758 err
= pci_sun4v_msi_getmsiq(pbm
->devhandle
, msi
, &msiqid
);
762 pci_sun4v_msi_setvalid(pbm
->devhandle
, msi
, HV_MSIVALID_INVALID
);
767 static int pci_sun4v_msiq_alloc(struct pci_pbm_info
*pbm
)
769 unsigned long q_size
, alloc_size
, pages
, order
;
772 q_size
= pbm
->msiq_ent_count
* sizeof(struct pci_sun4v_msiq_entry
);
773 alloc_size
= (pbm
->msiq_num
* q_size
);
774 order
= get_order(alloc_size
);
775 pages
= __get_free_pages(GFP_KERNEL
| __GFP_COMP
, order
);
777 printk(KERN_ERR
"MSI: Cannot allocate MSI queues (o=%lu).\n",
781 memset((char *)pages
, 0, PAGE_SIZE
<< order
);
782 pbm
->msi_queues
= (void *) pages
;
784 for (i
= 0; i
< pbm
->msiq_num
; i
++) {
785 unsigned long err
, base
= __pa(pages
+ (i
* q_size
));
786 unsigned long ret1
, ret2
;
788 err
= pci_sun4v_msiq_conf(pbm
->devhandle
,
790 base
, pbm
->msiq_ent_count
);
792 printk(KERN_ERR
"MSI: msiq register fails (err=%lu)\n",
797 err
= pci_sun4v_msiq_info(pbm
->devhandle
,
801 printk(KERN_ERR
"MSI: Cannot read msiq (err=%lu)\n",
805 if (ret1
!= base
|| ret2
!= pbm
->msiq_ent_count
) {
806 printk(KERN_ERR
"MSI: Bogus qconf "
807 "expected[%lx:%x] got[%lx:%lx]\n",
808 base
, pbm
->msiq_ent_count
,
817 free_pages(pages
, order
);
821 static void pci_sun4v_msiq_free(struct pci_pbm_info
*pbm
)
823 unsigned long q_size
, alloc_size
, pages
, order
;
826 for (i
= 0; i
< pbm
->msiq_num
; i
++) {
827 unsigned long msiqid
= pbm
->msiq_first
+ i
;
829 (void) pci_sun4v_msiq_conf(pbm
->devhandle
, msiqid
, 0UL, 0);
832 q_size
= pbm
->msiq_ent_count
* sizeof(struct pci_sun4v_msiq_entry
);
833 alloc_size
= (pbm
->msiq_num
* q_size
);
834 order
= get_order(alloc_size
);
836 pages
= (unsigned long) pbm
->msi_queues
;
838 free_pages(pages
, order
);
840 pbm
->msi_queues
= NULL
;
843 static int pci_sun4v_msiq_build_irq(struct pci_pbm_info
*pbm
,
844 unsigned long msiqid
,
845 unsigned long devino
)
847 unsigned int virt_irq
= sun4v_build_irq(pbm
->devhandle
, devino
);
852 if (pci_sun4v_msiq_setstate(pbm
->devhandle
, msiqid
, HV_MSIQSTATE_IDLE
))
854 if (pci_sun4v_msiq_setvalid(pbm
->devhandle
, msiqid
, HV_MSIQ_VALID
))
860 static const struct sparc64_msiq_ops pci_sun4v_msiq_ops
= {
861 .get_head
= pci_sun4v_get_head
,
862 .dequeue_msi
= pci_sun4v_dequeue_msi
,
863 .set_head
= pci_sun4v_set_head
,
864 .msi_setup
= pci_sun4v_msi_setup
,
865 .msi_teardown
= pci_sun4v_msi_teardown
,
866 .msiq_alloc
= pci_sun4v_msiq_alloc
,
867 .msiq_free
= pci_sun4v_msiq_free
,
868 .msiq_build_irq
= pci_sun4v_msiq_build_irq
,
871 static void pci_sun4v_msi_init(struct pci_pbm_info
*pbm
)
873 sparc64_pbm_msi_init(pbm
, &pci_sun4v_msiq_ops
);
875 #else /* CONFIG_PCI_MSI */
876 static void pci_sun4v_msi_init(struct pci_pbm_info
*pbm
)
879 #endif /* !(CONFIG_PCI_MSI) */
881 static void __init
pci_sun4v_pbm_init(struct pci_controller_info
*p
,
882 struct device_node
*dp
, u32 devhandle
)
884 struct pci_pbm_info
*pbm
;
886 if (devhandle
& 0x40)
891 pbm
->next
= pci_pbm_root
;
894 pbm
->scan_bus
= pci_sun4v_scan_bus
;
895 pbm
->pci_ops
= &sun4v_pci_ops
;
896 pbm
->config_space_reg_bits
= 12;
898 pbm
->index
= pci_num_pbms
++;
903 pbm
->devhandle
= devhandle
;
905 pbm
->name
= dp
->full_name
;
907 printk("%s: SUN4V PCI Bus Module\n", pbm
->name
);
909 pci_determine_mem_io_space(pbm
);
911 pci_get_pbm_props(pbm
);
912 pci_sun4v_iommu_init(pbm
);
913 pci_sun4v_msi_init(pbm
);
916 void __init
sun4v_pci_init(struct device_node
*dp
, char *model_name
)
918 static int hvapi_negotiated
= 0;
919 struct pci_controller_info
*p
;
920 struct pci_pbm_info
*pbm
;
922 struct property
*prop
;
923 struct linux_prom64_registers
*regs
;
927 if (!hvapi_negotiated
++) {
928 int err
= sun4v_hvapi_register(HV_GRP_PCI
,
933 prom_printf("SUN4V_PCI: Could not register hvapi, "
937 printk("SUN4V_PCI: Registered hvapi major[%lu] minor[%lu]\n",
938 vpci_major
, vpci_minor
);
940 dma_ops
= &sun4v_dma_ops
;
943 prop
= of_find_property(dp
, "reg", NULL
);
945 prom_printf("SUN4V_PCI: Could not find config registers\n");
950 devhandle
= (regs
->phys_addr
>> 32UL) & 0x0fffffff;
952 for (pbm
= pci_pbm_root
; pbm
; pbm
= pbm
->next
) {
953 if (pbm
->devhandle
== (devhandle
^ 0x40)) {
954 pci_sun4v_pbm_init(pbm
->parent
, dp
, devhandle
);
959 for_each_possible_cpu(i
) {
960 unsigned long page
= get_zeroed_page(GFP_ATOMIC
);
963 goto fatal_memory_error
;
965 per_cpu(iommu_batch
, i
).pglist
= (u64
*) page
;
968 p
= kzalloc(sizeof(struct pci_controller_info
), GFP_ATOMIC
);
970 goto fatal_memory_error
;
972 iommu
= kzalloc(sizeof(struct iommu
), GFP_ATOMIC
);
974 goto fatal_memory_error
;
976 p
->pbm_A
.iommu
= iommu
;
978 iommu
= kzalloc(sizeof(struct iommu
), GFP_ATOMIC
);
980 goto fatal_memory_error
;
982 p
->pbm_B
.iommu
= iommu
;
984 pci_sun4v_pbm_init(p
, dp
, devhandle
);
988 prom_printf("SUN4V_PCI: Fatal memory allocation error.\n");