1 /* iommu.c: Generic sparc64 IOMMU support.
3 * Copyright (C) 1999, 2007, 2008 David S. Miller (davem@davemloft.net)
4 * Copyright (C) 1999, 2000 Jakub Jelinek (jakub@redhat.com)
7 #include <linux/kernel.h>
8 #include <linux/export.h>
9 #include <linux/slab.h>
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/errno.h>
14 #include <linux/iommu-helper.h>
15 #include <linux/bitmap.h>
18 #include <linux/pci.h>
21 #include <asm/iommu.h>
23 #include "iommu_common.h"
26 #define STC_CTXMATCH_ADDR(STC, CTX) \
27 ((STC)->strbuf_ctxmatch_base + ((CTX) << 3))
28 #define STC_FLUSHFLAG_INIT(STC) \
29 (*((STC)->strbuf_flushflag) = 0UL)
30 #define STC_FLUSHFLAG_SET(STC) \
31 (*((STC)->strbuf_flushflag) != 0UL)
33 #define iommu_read(__reg) \
35 __asm__ __volatile__("ldxa [%1] %2, %0" \
37 : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
41 #define iommu_write(__reg, __val) \
42 __asm__ __volatile__("stxa %0, [%1] %2" \
44 : "r" (__val), "r" (__reg), \
45 "i" (ASI_PHYS_BYPASS_EC_E))
47 /* Must be invoked under the IOMMU lock. */
48 static void iommu_flushall(struct iommu
*iommu
)
50 if (iommu
->iommu_flushinv
) {
51 iommu_write(iommu
->iommu_flushinv
, ~(u64
)0);
56 tag
= iommu
->iommu_tags
;
57 for (entry
= 0; entry
< 16; entry
++) {
62 /* Ensure completion of previous PIO writes. */
63 (void) iommu_read(iommu
->write_complete_reg
);
67 #define IOPTE_CONSISTENT(CTX) \
68 (IOPTE_VALID | IOPTE_CACHE | \
69 (((CTX) << 47) & IOPTE_CONTEXT))
71 #define IOPTE_STREAMING(CTX) \
72 (IOPTE_CONSISTENT(CTX) | IOPTE_STBUF)
74 /* Existing mappings are never marked invalid, instead they
75 * are pointed to a dummy page.
77 #define IOPTE_IS_DUMMY(iommu, iopte) \
78 ((iopte_val(*iopte) & IOPTE_PAGE) == (iommu)->dummy_page_pa)
80 static inline void iopte_make_dummy(struct iommu
*iommu
, iopte_t
*iopte
)
82 unsigned long val
= iopte_val(*iopte
);
85 val
|= iommu
->dummy_page_pa
;
87 iopte_val(*iopte
) = val
;
90 /* Based almost entirely upon the ppc64 iommu allocator. If you use the 'handle'
91 * facility it must all be done in one pass while under the iommu lock.
93 * On sun4u platforms, we only flush the IOMMU once every time we've passed
94 * over the entire page table doing allocations. Therefore we only ever advance
95 * the hint and cannot backtrack it.
97 unsigned long iommu_range_alloc(struct device
*dev
,
100 unsigned long *handle
)
102 unsigned long n
, end
, start
, limit
, boundary_size
;
103 struct iommu_arena
*arena
= &iommu
->arena
;
106 /* This allocator was derived from x86_64's bit string search */
109 if (unlikely(npages
== 0)) {
110 if (printk_ratelimit())
112 return DMA_ERROR_CODE
;
115 if (handle
&& *handle
)
120 limit
= arena
->limit
;
122 /* The case below can happen if we have a small segment appended
123 * to a large, or when the previous alloc was at the very end of
124 * the available space. If so, go back to the beginning and flush.
126 if (start
>= limit
) {
128 if (iommu
->flush_all
)
129 iommu
->flush_all(iommu
);
135 boundary_size
= ALIGN(dma_get_seg_boundary(dev
) + 1,
138 boundary_size
= ALIGN(1UL << 32, 1 << IO_PAGE_SHIFT
);
140 n
= iommu_area_alloc(arena
->map
, limit
, start
, npages
,
141 iommu
->page_table_map_base
>> IO_PAGE_SHIFT
,
142 boundary_size
>> IO_PAGE_SHIFT
, 0);
144 if (likely(pass
< 1)) {
145 /* First failure, rescan from the beginning. */
147 if (iommu
->flush_all
)
148 iommu
->flush_all(iommu
);
152 /* Second failure, give up */
153 return DMA_ERROR_CODE
;
161 /* Update handle for SG allocations */
168 void iommu_range_free(struct iommu
*iommu
, dma_addr_t dma_addr
, unsigned long npages
)
170 struct iommu_arena
*arena
= &iommu
->arena
;
173 entry
= (dma_addr
- iommu
->page_table_map_base
) >> IO_PAGE_SHIFT
;
175 bitmap_clear(arena
->map
, entry
, npages
);
178 int iommu_table_init(struct iommu
*iommu
, int tsbsize
,
179 u32 dma_offset
, u32 dma_addr_mask
,
182 unsigned long i
, order
, sz
, num_tsb_entries
;
185 num_tsb_entries
= tsbsize
/ sizeof(iopte_t
);
187 /* Setup initial software IOMMU state. */
188 spin_lock_init(&iommu
->lock
);
189 iommu
->ctx_lowest_free
= 1;
190 iommu
->page_table_map_base
= dma_offset
;
191 iommu
->dma_addr_mask
= dma_addr_mask
;
193 /* Allocate and initialize the free area map. */
194 sz
= num_tsb_entries
/ 8;
195 sz
= (sz
+ 7UL) & ~7UL;
196 iommu
->arena
.map
= kmalloc_node(sz
, GFP_KERNEL
, numa_node
);
197 if (!iommu
->arena
.map
) {
198 printk(KERN_ERR
"IOMMU: Error, kmalloc(arena.map) failed.\n");
201 memset(iommu
->arena
.map
, 0, sz
);
202 iommu
->arena
.limit
= num_tsb_entries
;
204 if (tlb_type
!= hypervisor
)
205 iommu
->flush_all
= iommu_flushall
;
207 /* Allocate and initialize the dummy page which we
208 * set inactive IO PTEs to point to.
210 page
= alloc_pages_node(numa_node
, GFP_KERNEL
, 0);
212 printk(KERN_ERR
"IOMMU: Error, gfp(dummy_page) failed.\n");
215 iommu
->dummy_page
= (unsigned long) page_address(page
);
216 memset((void *)iommu
->dummy_page
, 0, PAGE_SIZE
);
217 iommu
->dummy_page_pa
= (unsigned long) __pa(iommu
->dummy_page
);
219 /* Now allocate and setup the IOMMU page table itself. */
220 order
= get_order(tsbsize
);
221 page
= alloc_pages_node(numa_node
, GFP_KERNEL
, order
);
223 printk(KERN_ERR
"IOMMU: Error, gfp(tsb) failed.\n");
224 goto out_free_dummy_page
;
226 iommu
->page_table
= (iopte_t
*)page_address(page
);
228 for (i
= 0; i
< num_tsb_entries
; i
++)
229 iopte_make_dummy(iommu
, &iommu
->page_table
[i
]);
234 free_page(iommu
->dummy_page
);
235 iommu
->dummy_page
= 0UL;
238 kfree(iommu
->arena
.map
);
239 iommu
->arena
.map
= NULL
;
244 static inline iopte_t
*alloc_npages(struct device
*dev
, struct iommu
*iommu
,
245 unsigned long npages
)
249 entry
= iommu_range_alloc(dev
, iommu
, npages
, NULL
);
250 if (unlikely(entry
== DMA_ERROR_CODE
))
253 return iommu
->page_table
+ entry
;
256 static int iommu_alloc_ctx(struct iommu
*iommu
)
258 int lowest
= iommu
->ctx_lowest_free
;
259 int n
= find_next_zero_bit(iommu
->ctx_bitmap
, IOMMU_NUM_CTXS
, lowest
);
261 if (unlikely(n
== IOMMU_NUM_CTXS
)) {
262 n
= find_next_zero_bit(iommu
->ctx_bitmap
, lowest
, 1);
263 if (unlikely(n
== lowest
)) {
264 printk(KERN_WARNING
"IOMMU: Ran out of contexts.\n");
269 __set_bit(n
, iommu
->ctx_bitmap
);
274 static inline void iommu_free_ctx(struct iommu
*iommu
, int ctx
)
277 __clear_bit(ctx
, iommu
->ctx_bitmap
);
278 if (ctx
< iommu
->ctx_lowest_free
)
279 iommu
->ctx_lowest_free
= ctx
;
283 static void *dma_4u_alloc_coherent(struct device
*dev
, size_t size
,
284 dma_addr_t
*dma_addrp
, gfp_t gfp
,
285 struct dma_attrs
*attrs
)
287 unsigned long flags
, order
, first_page
;
294 size
= IO_PAGE_ALIGN(size
);
295 order
= get_order(size
);
299 nid
= dev
->archdata
.numa_node
;
300 page
= alloc_pages_node(nid
, gfp
, order
);
304 first_page
= (unsigned long) page_address(page
);
305 memset((char *)first_page
, 0, PAGE_SIZE
<< order
);
307 iommu
= dev
->archdata
.iommu
;
309 spin_lock_irqsave(&iommu
->lock
, flags
);
310 iopte
= alloc_npages(dev
, iommu
, size
>> IO_PAGE_SHIFT
);
311 spin_unlock_irqrestore(&iommu
->lock
, flags
);
313 if (unlikely(iopte
== NULL
)) {
314 free_pages(first_page
, order
);
318 *dma_addrp
= (iommu
->page_table_map_base
+
319 ((iopte
- iommu
->page_table
) << IO_PAGE_SHIFT
));
320 ret
= (void *) first_page
;
321 npages
= size
>> IO_PAGE_SHIFT
;
322 first_page
= __pa(first_page
);
324 iopte_val(*iopte
) = (IOPTE_CONSISTENT(0UL) |
326 (first_page
& IOPTE_PAGE
));
328 first_page
+= IO_PAGE_SIZE
;
334 static void dma_4u_free_coherent(struct device
*dev
, size_t size
,
335 void *cpu
, dma_addr_t dvma
,
336 struct dma_attrs
*attrs
)
339 unsigned long flags
, order
, npages
;
341 npages
= IO_PAGE_ALIGN(size
) >> IO_PAGE_SHIFT
;
342 iommu
= dev
->archdata
.iommu
;
344 spin_lock_irqsave(&iommu
->lock
, flags
);
346 iommu_range_free(iommu
, dvma
, npages
);
348 spin_unlock_irqrestore(&iommu
->lock
, flags
);
350 order
= get_order(size
);
352 free_pages((unsigned long)cpu
, order
);
355 static dma_addr_t
dma_4u_map_page(struct device
*dev
, struct page
*page
,
356 unsigned long offset
, size_t sz
,
357 enum dma_data_direction direction
,
358 struct dma_attrs
*attrs
)
361 struct strbuf
*strbuf
;
363 unsigned long flags
, npages
, oaddr
;
364 unsigned long i
, base_paddr
, ctx
;
366 unsigned long iopte_protection
;
368 iommu
= dev
->archdata
.iommu
;
369 strbuf
= dev
->archdata
.stc
;
371 if (unlikely(direction
== DMA_NONE
))
374 oaddr
= (unsigned long)(page_address(page
) + offset
);
375 npages
= IO_PAGE_ALIGN(oaddr
+ sz
) - (oaddr
& IO_PAGE_MASK
);
376 npages
>>= IO_PAGE_SHIFT
;
378 spin_lock_irqsave(&iommu
->lock
, flags
);
379 base
= alloc_npages(dev
, iommu
, npages
);
381 if (iommu
->iommu_ctxflush
)
382 ctx
= iommu_alloc_ctx(iommu
);
383 spin_unlock_irqrestore(&iommu
->lock
, flags
);
388 bus_addr
= (iommu
->page_table_map_base
+
389 ((base
- iommu
->page_table
) << IO_PAGE_SHIFT
));
390 ret
= bus_addr
| (oaddr
& ~IO_PAGE_MASK
);
391 base_paddr
= __pa(oaddr
& IO_PAGE_MASK
);
392 if (strbuf
->strbuf_enabled
)
393 iopte_protection
= IOPTE_STREAMING(ctx
);
395 iopte_protection
= IOPTE_CONSISTENT(ctx
);
396 if (direction
!= DMA_TO_DEVICE
)
397 iopte_protection
|= IOPTE_WRITE
;
399 for (i
= 0; i
< npages
; i
++, base
++, base_paddr
+= IO_PAGE_SIZE
)
400 iopte_val(*base
) = iopte_protection
| base_paddr
;
405 iommu_free_ctx(iommu
, ctx
);
407 if (printk_ratelimit())
409 return DMA_ERROR_CODE
;
412 static void strbuf_flush(struct strbuf
*strbuf
, struct iommu
*iommu
,
413 u32 vaddr
, unsigned long ctx
, unsigned long npages
,
414 enum dma_data_direction direction
)
418 if (strbuf
->strbuf_ctxflush
&&
419 iommu
->iommu_ctxflush
) {
420 unsigned long matchreg
, flushreg
;
423 flushreg
= strbuf
->strbuf_ctxflush
;
424 matchreg
= STC_CTXMATCH_ADDR(strbuf
, ctx
);
426 iommu_write(flushreg
, ctx
);
427 val
= iommu_read(matchreg
);
434 iommu_write(flushreg
, ctx
);
437 val
= iommu_read(matchreg
);
439 printk(KERN_WARNING
"strbuf_flush: ctx flush "
440 "timeout matchreg[%llx] ctx[%lx]\n",
448 for (i
= 0; i
< npages
; i
++, vaddr
+= IO_PAGE_SIZE
)
449 iommu_write(strbuf
->strbuf_pflush
, vaddr
);
453 /* If the device could not have possibly put dirty data into
454 * the streaming cache, no flush-flag synchronization needs
457 if (direction
== DMA_TO_DEVICE
)
460 STC_FLUSHFLAG_INIT(strbuf
);
461 iommu_write(strbuf
->strbuf_fsync
, strbuf
->strbuf_flushflag_pa
);
462 (void) iommu_read(iommu
->write_complete_reg
);
465 while (!STC_FLUSHFLAG_SET(strbuf
)) {
473 printk(KERN_WARNING
"strbuf_flush: flushflag timeout "
474 "vaddr[%08x] ctx[%lx] npages[%ld]\n",
478 static void dma_4u_unmap_page(struct device
*dev
, dma_addr_t bus_addr
,
479 size_t sz
, enum dma_data_direction direction
,
480 struct dma_attrs
*attrs
)
483 struct strbuf
*strbuf
;
485 unsigned long flags
, npages
, ctx
, i
;
487 if (unlikely(direction
== DMA_NONE
)) {
488 if (printk_ratelimit())
493 iommu
= dev
->archdata
.iommu
;
494 strbuf
= dev
->archdata
.stc
;
496 npages
= IO_PAGE_ALIGN(bus_addr
+ sz
) - (bus_addr
& IO_PAGE_MASK
);
497 npages
>>= IO_PAGE_SHIFT
;
498 base
= iommu
->page_table
+
499 ((bus_addr
- iommu
->page_table_map_base
) >> IO_PAGE_SHIFT
);
500 bus_addr
&= IO_PAGE_MASK
;
502 spin_lock_irqsave(&iommu
->lock
, flags
);
504 /* Record the context, if any. */
506 if (iommu
->iommu_ctxflush
)
507 ctx
= (iopte_val(*base
) & IOPTE_CONTEXT
) >> 47UL;
509 /* Step 1: Kick data out of streaming buffers if necessary. */
510 if (strbuf
->strbuf_enabled
)
511 strbuf_flush(strbuf
, iommu
, bus_addr
, ctx
,
514 /* Step 2: Clear out TSB entries. */
515 for (i
= 0; i
< npages
; i
++)
516 iopte_make_dummy(iommu
, base
+ i
);
518 iommu_range_free(iommu
, bus_addr
, npages
);
520 iommu_free_ctx(iommu
, ctx
);
522 spin_unlock_irqrestore(&iommu
->lock
, flags
);
525 static int dma_4u_map_sg(struct device
*dev
, struct scatterlist
*sglist
,
526 int nelems
, enum dma_data_direction direction
,
527 struct dma_attrs
*attrs
)
529 struct scatterlist
*s
, *outs
, *segstart
;
530 unsigned long flags
, handle
, prot
, ctx
;
531 dma_addr_t dma_next
= 0, dma_addr
;
532 unsigned int max_seg_size
;
533 unsigned long seg_boundary_size
;
534 int outcount
, incount
, i
;
535 struct strbuf
*strbuf
;
537 unsigned long base_shift
;
539 BUG_ON(direction
== DMA_NONE
);
541 iommu
= dev
->archdata
.iommu
;
542 strbuf
= dev
->archdata
.stc
;
543 if (nelems
== 0 || !iommu
)
546 spin_lock_irqsave(&iommu
->lock
, flags
);
549 if (iommu
->iommu_ctxflush
)
550 ctx
= iommu_alloc_ctx(iommu
);
552 if (strbuf
->strbuf_enabled
)
553 prot
= IOPTE_STREAMING(ctx
);
555 prot
= IOPTE_CONSISTENT(ctx
);
556 if (direction
!= DMA_TO_DEVICE
)
559 outs
= s
= segstart
= &sglist
[0];
564 /* Init first segment length for backout at failure */
565 outs
->dma_length
= 0;
567 max_seg_size
= dma_get_max_seg_size(dev
);
568 seg_boundary_size
= ALIGN(dma_get_seg_boundary(dev
) + 1,
569 IO_PAGE_SIZE
) >> IO_PAGE_SHIFT
;
570 base_shift
= iommu
->page_table_map_base
>> IO_PAGE_SHIFT
;
571 for_each_sg(sglist
, s
, nelems
, i
) {
572 unsigned long paddr
, npages
, entry
, out_entry
= 0, slen
;
581 /* Allocate iommu entries for that segment */
582 paddr
= (unsigned long) SG_ENT_PHYS_ADDRESS(s
);
583 npages
= iommu_num_pages(paddr
, slen
, IO_PAGE_SIZE
);
584 entry
= iommu_range_alloc(dev
, iommu
, npages
, &handle
);
587 if (unlikely(entry
== DMA_ERROR_CODE
)) {
588 if (printk_ratelimit())
589 printk(KERN_INFO
"iommu_alloc failed, iommu %p paddr %lx"
590 " npages %lx\n", iommu
, paddr
, npages
);
591 goto iommu_map_failed
;
594 base
= iommu
->page_table
+ entry
;
596 /* Convert entry to a dma_addr_t */
597 dma_addr
= iommu
->page_table_map_base
+
598 (entry
<< IO_PAGE_SHIFT
);
599 dma_addr
|= (s
->offset
& ~IO_PAGE_MASK
);
601 /* Insert into HW table */
602 paddr
&= IO_PAGE_MASK
;
604 iopte_val(*base
) = prot
| paddr
;
606 paddr
+= IO_PAGE_SIZE
;
609 /* If we are in an open segment, try merging */
611 /* We cannot merge if:
612 * - allocated dma_addr isn't contiguous to previous allocation
614 if ((dma_addr
!= dma_next
) ||
615 (outs
->dma_length
+ s
->length
> max_seg_size
) ||
616 (is_span_boundary(out_entry
, base_shift
,
617 seg_boundary_size
, outs
, s
))) {
618 /* Can't merge: create a new segment */
621 outs
= sg_next(outs
);
623 outs
->dma_length
+= s
->length
;
628 /* This is a new segment, fill entries */
629 outs
->dma_address
= dma_addr
;
630 outs
->dma_length
= slen
;
634 /* Calculate next page pointer for contiguous check */
635 dma_next
= dma_addr
+ slen
;
638 spin_unlock_irqrestore(&iommu
->lock
, flags
);
640 if (outcount
< incount
) {
641 outs
= sg_next(outs
);
642 outs
->dma_address
= DMA_ERROR_CODE
;
643 outs
->dma_length
= 0;
649 for_each_sg(sglist
, s
, nelems
, i
) {
650 if (s
->dma_length
!= 0) {
651 unsigned long vaddr
, npages
, entry
, j
;
654 vaddr
= s
->dma_address
& IO_PAGE_MASK
;
655 npages
= iommu_num_pages(s
->dma_address
, s
->dma_length
,
657 iommu_range_free(iommu
, vaddr
, npages
);
659 entry
= (vaddr
- iommu
->page_table_map_base
)
661 base
= iommu
->page_table
+ entry
;
663 for (j
= 0; j
< npages
; j
++)
664 iopte_make_dummy(iommu
, base
+ j
);
666 s
->dma_address
= DMA_ERROR_CODE
;
672 spin_unlock_irqrestore(&iommu
->lock
, flags
);
677 /* If contexts are being used, they are the same in all of the mappings
678 * we make for a particular SG.
680 static unsigned long fetch_sg_ctx(struct iommu
*iommu
, struct scatterlist
*sg
)
682 unsigned long ctx
= 0;
684 if (iommu
->iommu_ctxflush
) {
688 bus_addr
= sg
->dma_address
& IO_PAGE_MASK
;
689 base
= iommu
->page_table
+
690 ((bus_addr
- iommu
->page_table_map_base
) >> IO_PAGE_SHIFT
);
692 ctx
= (iopte_val(*base
) & IOPTE_CONTEXT
) >> 47UL;
697 static void dma_4u_unmap_sg(struct device
*dev
, struct scatterlist
*sglist
,
698 int nelems
, enum dma_data_direction direction
,
699 struct dma_attrs
*attrs
)
701 unsigned long flags
, ctx
;
702 struct scatterlist
*sg
;
703 struct strbuf
*strbuf
;
706 BUG_ON(direction
== DMA_NONE
);
708 iommu
= dev
->archdata
.iommu
;
709 strbuf
= dev
->archdata
.stc
;
711 ctx
= fetch_sg_ctx(iommu
, sglist
);
713 spin_lock_irqsave(&iommu
->lock
, flags
);
717 dma_addr_t dma_handle
= sg
->dma_address
;
718 unsigned int len
= sg
->dma_length
;
719 unsigned long npages
, entry
;
725 npages
= iommu_num_pages(dma_handle
, len
, IO_PAGE_SIZE
);
726 iommu_range_free(iommu
, dma_handle
, npages
);
728 entry
= ((dma_handle
- iommu
->page_table_map_base
)
730 base
= iommu
->page_table
+ entry
;
732 dma_handle
&= IO_PAGE_MASK
;
733 if (strbuf
->strbuf_enabled
)
734 strbuf_flush(strbuf
, iommu
, dma_handle
, ctx
,
737 for (i
= 0; i
< npages
; i
++)
738 iopte_make_dummy(iommu
, base
+ i
);
743 iommu_free_ctx(iommu
, ctx
);
745 spin_unlock_irqrestore(&iommu
->lock
, flags
);
748 static void dma_4u_sync_single_for_cpu(struct device
*dev
,
749 dma_addr_t bus_addr
, size_t sz
,
750 enum dma_data_direction direction
)
753 struct strbuf
*strbuf
;
754 unsigned long flags
, ctx
, npages
;
756 iommu
= dev
->archdata
.iommu
;
757 strbuf
= dev
->archdata
.stc
;
759 if (!strbuf
->strbuf_enabled
)
762 spin_lock_irqsave(&iommu
->lock
, flags
);
764 npages
= IO_PAGE_ALIGN(bus_addr
+ sz
) - (bus_addr
& IO_PAGE_MASK
);
765 npages
>>= IO_PAGE_SHIFT
;
766 bus_addr
&= IO_PAGE_MASK
;
768 /* Step 1: Record the context, if any. */
770 if (iommu
->iommu_ctxflush
&&
771 strbuf
->strbuf_ctxflush
) {
774 iopte
= iommu
->page_table
+
775 ((bus_addr
- iommu
->page_table_map_base
)>>IO_PAGE_SHIFT
);
776 ctx
= (iopte_val(*iopte
) & IOPTE_CONTEXT
) >> 47UL;
779 /* Step 2: Kick data out of streaming buffers. */
780 strbuf_flush(strbuf
, iommu
, bus_addr
, ctx
, npages
, direction
);
782 spin_unlock_irqrestore(&iommu
->lock
, flags
);
785 static void dma_4u_sync_sg_for_cpu(struct device
*dev
,
786 struct scatterlist
*sglist
, int nelems
,
787 enum dma_data_direction direction
)
790 struct strbuf
*strbuf
;
791 unsigned long flags
, ctx
, npages
, i
;
792 struct scatterlist
*sg
, *sgprv
;
795 iommu
= dev
->archdata
.iommu
;
796 strbuf
= dev
->archdata
.stc
;
798 if (!strbuf
->strbuf_enabled
)
801 spin_lock_irqsave(&iommu
->lock
, flags
);
803 /* Step 1: Record the context, if any. */
805 if (iommu
->iommu_ctxflush
&&
806 strbuf
->strbuf_ctxflush
) {
809 iopte
= iommu
->page_table
+
810 ((sglist
[0].dma_address
- iommu
->page_table_map_base
) >> IO_PAGE_SHIFT
);
811 ctx
= (iopte_val(*iopte
) & IOPTE_CONTEXT
) >> 47UL;
814 /* Step 2: Kick data out of streaming buffers. */
815 bus_addr
= sglist
[0].dma_address
& IO_PAGE_MASK
;
817 for_each_sg(sglist
, sg
, nelems
, i
) {
818 if (sg
->dma_length
== 0)
823 npages
= (IO_PAGE_ALIGN(sgprv
->dma_address
+ sgprv
->dma_length
)
824 - bus_addr
) >> IO_PAGE_SHIFT
;
825 strbuf_flush(strbuf
, iommu
, bus_addr
, ctx
, npages
, direction
);
827 spin_unlock_irqrestore(&iommu
->lock
, flags
);
830 static struct dma_map_ops sun4u_dma_ops
= {
831 .alloc
= dma_4u_alloc_coherent
,
832 .free
= dma_4u_free_coherent
,
833 .map_page
= dma_4u_map_page
,
834 .unmap_page
= dma_4u_unmap_page
,
835 .map_sg
= dma_4u_map_sg
,
836 .unmap_sg
= dma_4u_unmap_sg
,
837 .sync_single_for_cpu
= dma_4u_sync_single_for_cpu
,
838 .sync_sg_for_cpu
= dma_4u_sync_sg_for_cpu
,
841 struct dma_map_ops
*dma_ops
= &sun4u_dma_ops
;
842 EXPORT_SYMBOL(dma_ops
);
844 int dma_supported(struct device
*dev
, u64 device_mask
)
846 struct iommu
*iommu
= dev
->archdata
.iommu
;
847 u64 dma_addr_mask
= iommu
->dma_addr_mask
;
849 if (device_mask
>= (1UL << 32UL))
852 if ((device_mask
& dma_addr_mask
) == dma_addr_mask
)
857 return pci64_dma_supported(to_pci_dev(dev
), device_mask
);
862 EXPORT_SYMBOL(dma_supported
);