sparc: Use dma_map_ops struct
[linux-2.6/x86.git] / arch / sparc / kernel / iommu.c
bloba9f0ad955186e5e800141f76c4ff4160ace57e99
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)
5 */
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/errno.h>
13 #include <linux/iommu-helper.h>
15 #ifdef CONFIG_PCI
16 #include <linux/pci.h>
17 #endif
19 #include <asm/iommu.h>
21 #include "iommu_common.h"
23 #define STC_CTXMATCH_ADDR(STC, CTX) \
24 ((STC)->strbuf_ctxmatch_base + ((CTX) << 3))
25 #define STC_FLUSHFLAG_INIT(STC) \
26 (*((STC)->strbuf_flushflag) = 0UL)
27 #define STC_FLUSHFLAG_SET(STC) \
28 (*((STC)->strbuf_flushflag) != 0UL)
30 #define iommu_read(__reg) \
31 ({ u64 __ret; \
32 __asm__ __volatile__("ldxa [%1] %2, %0" \
33 : "=r" (__ret) \
34 : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
35 : "memory"); \
36 __ret; \
38 #define iommu_write(__reg, __val) \
39 __asm__ __volatile__("stxa %0, [%1] %2" \
40 : /* no outputs */ \
41 : "r" (__val), "r" (__reg), \
42 "i" (ASI_PHYS_BYPASS_EC_E))
44 /* Must be invoked under the IOMMU lock. */
45 static void iommu_flushall(struct iommu *iommu)
47 if (iommu->iommu_flushinv) {
48 iommu_write(iommu->iommu_flushinv, ~(u64)0);
49 } else {
50 unsigned long tag;
51 int entry;
53 tag = iommu->iommu_tags;
54 for (entry = 0; entry < 16; entry++) {
55 iommu_write(tag, 0);
56 tag += 8;
59 /* Ensure completion of previous PIO writes. */
60 (void) iommu_read(iommu->write_complete_reg);
64 #define IOPTE_CONSISTENT(CTX) \
65 (IOPTE_VALID | IOPTE_CACHE | \
66 (((CTX) << 47) & IOPTE_CONTEXT))
68 #define IOPTE_STREAMING(CTX) \
69 (IOPTE_CONSISTENT(CTX) | IOPTE_STBUF)
71 /* Existing mappings are never marked invalid, instead they
72 * are pointed to a dummy page.
74 #define IOPTE_IS_DUMMY(iommu, iopte) \
75 ((iopte_val(*iopte) & IOPTE_PAGE) == (iommu)->dummy_page_pa)
77 static inline void iopte_make_dummy(struct iommu *iommu, iopte_t *iopte)
79 unsigned long val = iopte_val(*iopte);
81 val &= ~IOPTE_PAGE;
82 val |= iommu->dummy_page_pa;
84 iopte_val(*iopte) = val;
87 /* Based almost entirely upon the ppc64 iommu allocator. If you use the 'handle'
88 * facility it must all be done in one pass while under the iommu lock.
90 * On sun4u platforms, we only flush the IOMMU once every time we've passed
91 * over the entire page table doing allocations. Therefore we only ever advance
92 * the hint and cannot backtrack it.
94 unsigned long iommu_range_alloc(struct device *dev,
95 struct iommu *iommu,
96 unsigned long npages,
97 unsigned long *handle)
99 unsigned long n, end, start, limit, boundary_size;
100 struct iommu_arena *arena = &iommu->arena;
101 int pass = 0;
103 /* This allocator was derived from x86_64's bit string search */
105 /* Sanity check */
106 if (unlikely(npages == 0)) {
107 if (printk_ratelimit())
108 WARN_ON(1);
109 return DMA_ERROR_CODE;
112 if (handle && *handle)
113 start = *handle;
114 else
115 start = arena->hint;
117 limit = arena->limit;
119 /* The case below can happen if we have a small segment appended
120 * to a large, or when the previous alloc was at the very end of
121 * the available space. If so, go back to the beginning and flush.
123 if (start >= limit) {
124 start = 0;
125 if (iommu->flush_all)
126 iommu->flush_all(iommu);
129 again:
131 if (dev)
132 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
133 1 << IO_PAGE_SHIFT);
134 else
135 boundary_size = ALIGN(1UL << 32, 1 << IO_PAGE_SHIFT);
137 n = iommu_area_alloc(arena->map, limit, start, npages,
138 iommu->page_table_map_base >> IO_PAGE_SHIFT,
139 boundary_size >> IO_PAGE_SHIFT, 0);
140 if (n == -1) {
141 if (likely(pass < 1)) {
142 /* First failure, rescan from the beginning. */
143 start = 0;
144 if (iommu->flush_all)
145 iommu->flush_all(iommu);
146 pass++;
147 goto again;
148 } else {
149 /* Second failure, give up */
150 return DMA_ERROR_CODE;
154 end = n + npages;
156 arena->hint = end;
158 /* Update handle for SG allocations */
159 if (handle)
160 *handle = end;
162 return n;
165 void iommu_range_free(struct iommu *iommu, dma_addr_t dma_addr, unsigned long npages)
167 struct iommu_arena *arena = &iommu->arena;
168 unsigned long entry;
170 entry = (dma_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT;
172 iommu_area_free(arena->map, entry, npages);
175 int iommu_table_init(struct iommu *iommu, int tsbsize,
176 u32 dma_offset, u32 dma_addr_mask,
177 int numa_node)
179 unsigned long i, order, sz, num_tsb_entries;
180 struct page *page;
182 num_tsb_entries = tsbsize / sizeof(iopte_t);
184 /* Setup initial software IOMMU state. */
185 spin_lock_init(&iommu->lock);
186 iommu->ctx_lowest_free = 1;
187 iommu->page_table_map_base = dma_offset;
188 iommu->dma_addr_mask = dma_addr_mask;
190 /* Allocate and initialize the free area map. */
191 sz = num_tsb_entries / 8;
192 sz = (sz + 7UL) & ~7UL;
193 iommu->arena.map = kmalloc_node(sz, GFP_KERNEL, numa_node);
194 if (!iommu->arena.map) {
195 printk(KERN_ERR "IOMMU: Error, kmalloc(arena.map) failed.\n");
196 return -ENOMEM;
198 memset(iommu->arena.map, 0, sz);
199 iommu->arena.limit = num_tsb_entries;
201 if (tlb_type != hypervisor)
202 iommu->flush_all = iommu_flushall;
204 /* Allocate and initialize the dummy page which we
205 * set inactive IO PTEs to point to.
207 page = alloc_pages_node(numa_node, GFP_KERNEL, 0);
208 if (!page) {
209 printk(KERN_ERR "IOMMU: Error, gfp(dummy_page) failed.\n");
210 goto out_free_map;
212 iommu->dummy_page = (unsigned long) page_address(page);
213 memset((void *)iommu->dummy_page, 0, PAGE_SIZE);
214 iommu->dummy_page_pa = (unsigned long) __pa(iommu->dummy_page);
216 /* Now allocate and setup the IOMMU page table itself. */
217 order = get_order(tsbsize);
218 page = alloc_pages_node(numa_node, GFP_KERNEL, order);
219 if (!page) {
220 printk(KERN_ERR "IOMMU: Error, gfp(tsb) failed.\n");
221 goto out_free_dummy_page;
223 iommu->page_table = (iopte_t *)page_address(page);
225 for (i = 0; i < num_tsb_entries; i++)
226 iopte_make_dummy(iommu, &iommu->page_table[i]);
228 return 0;
230 out_free_dummy_page:
231 free_page(iommu->dummy_page);
232 iommu->dummy_page = 0UL;
234 out_free_map:
235 kfree(iommu->arena.map);
236 iommu->arena.map = NULL;
238 return -ENOMEM;
241 static inline iopte_t *alloc_npages(struct device *dev, struct iommu *iommu,
242 unsigned long npages)
244 unsigned long entry;
246 entry = iommu_range_alloc(dev, iommu, npages, NULL);
247 if (unlikely(entry == DMA_ERROR_CODE))
248 return NULL;
250 return iommu->page_table + entry;
253 static int iommu_alloc_ctx(struct iommu *iommu)
255 int lowest = iommu->ctx_lowest_free;
256 int sz = IOMMU_NUM_CTXS - lowest;
257 int n = find_next_zero_bit(iommu->ctx_bitmap, sz, lowest);
259 if (unlikely(n == sz)) {
260 n = find_next_zero_bit(iommu->ctx_bitmap, lowest, 1);
261 if (unlikely(n == lowest)) {
262 printk(KERN_WARNING "IOMMU: Ran out of contexts.\n");
263 n = 0;
266 if (n)
267 __set_bit(n, iommu->ctx_bitmap);
269 return n;
272 static inline void iommu_free_ctx(struct iommu *iommu, int ctx)
274 if (likely(ctx)) {
275 __clear_bit(ctx, iommu->ctx_bitmap);
276 if (ctx < iommu->ctx_lowest_free)
277 iommu->ctx_lowest_free = ctx;
281 static void *dma_4u_alloc_coherent(struct device *dev, size_t size,
282 dma_addr_t *dma_addrp, gfp_t gfp)
284 unsigned long flags, order, first_page;
285 struct iommu *iommu;
286 struct page *page;
287 int npages, nid;
288 iopte_t *iopte;
289 void *ret;
291 size = IO_PAGE_ALIGN(size);
292 order = get_order(size);
293 if (order >= 10)
294 return NULL;
296 nid = dev->archdata.numa_node;
297 page = alloc_pages_node(nid, gfp, order);
298 if (unlikely(!page))
299 return NULL;
301 first_page = (unsigned long) page_address(page);
302 memset((char *)first_page, 0, PAGE_SIZE << order);
304 iommu = dev->archdata.iommu;
306 spin_lock_irqsave(&iommu->lock, flags);
307 iopte = alloc_npages(dev, iommu, size >> IO_PAGE_SHIFT);
308 spin_unlock_irqrestore(&iommu->lock, flags);
310 if (unlikely(iopte == NULL)) {
311 free_pages(first_page, order);
312 return NULL;
315 *dma_addrp = (iommu->page_table_map_base +
316 ((iopte - iommu->page_table) << IO_PAGE_SHIFT));
317 ret = (void *) first_page;
318 npages = size >> IO_PAGE_SHIFT;
319 first_page = __pa(first_page);
320 while (npages--) {
321 iopte_val(*iopte) = (IOPTE_CONSISTENT(0UL) |
322 IOPTE_WRITE |
323 (first_page & IOPTE_PAGE));
324 iopte++;
325 first_page += IO_PAGE_SIZE;
328 return ret;
331 static void dma_4u_free_coherent(struct device *dev, size_t size,
332 void *cpu, dma_addr_t dvma)
334 struct iommu *iommu;
335 iopte_t *iopte;
336 unsigned long flags, order, npages;
338 npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
339 iommu = dev->archdata.iommu;
340 iopte = iommu->page_table +
341 ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
343 spin_lock_irqsave(&iommu->lock, flags);
345 iommu_range_free(iommu, dvma, npages);
347 spin_unlock_irqrestore(&iommu->lock, flags);
349 order = get_order(size);
350 if (order < 10)
351 free_pages((unsigned long)cpu, order);
354 static dma_addr_t dma_4u_map_page(struct device *dev, struct page *page,
355 unsigned long offset, size_t sz,
356 enum dma_data_direction direction,
357 struct dma_attrs *attrs)
359 struct iommu *iommu;
360 struct strbuf *strbuf;
361 iopte_t *base;
362 unsigned long flags, npages, oaddr;
363 unsigned long i, base_paddr, ctx;
364 u32 bus_addr, ret;
365 unsigned long iopte_protection;
367 iommu = dev->archdata.iommu;
368 strbuf = dev->archdata.stc;
370 if (unlikely(direction == DMA_NONE))
371 goto bad_no_ctx;
373 oaddr = (unsigned long)(page_address(page) + offset);
374 npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
375 npages >>= IO_PAGE_SHIFT;
377 spin_lock_irqsave(&iommu->lock, flags);
378 base = alloc_npages(dev, iommu, npages);
379 ctx = 0;
380 if (iommu->iommu_ctxflush)
381 ctx = iommu_alloc_ctx(iommu);
382 spin_unlock_irqrestore(&iommu->lock, flags);
384 if (unlikely(!base))
385 goto bad;
387 bus_addr = (iommu->page_table_map_base +
388 ((base - iommu->page_table) << IO_PAGE_SHIFT));
389 ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
390 base_paddr = __pa(oaddr & IO_PAGE_MASK);
391 if (strbuf->strbuf_enabled)
392 iopte_protection = IOPTE_STREAMING(ctx);
393 else
394 iopte_protection = IOPTE_CONSISTENT(ctx);
395 if (direction != DMA_TO_DEVICE)
396 iopte_protection |= IOPTE_WRITE;
398 for (i = 0; i < npages; i++, base++, base_paddr += IO_PAGE_SIZE)
399 iopte_val(*base) = iopte_protection | base_paddr;
401 return ret;
403 bad:
404 iommu_free_ctx(iommu, ctx);
405 bad_no_ctx:
406 if (printk_ratelimit())
407 WARN_ON(1);
408 return DMA_ERROR_CODE;
411 static void strbuf_flush(struct strbuf *strbuf, struct iommu *iommu,
412 u32 vaddr, unsigned long ctx, unsigned long npages,
413 enum dma_data_direction direction)
415 int limit;
417 if (strbuf->strbuf_ctxflush &&
418 iommu->iommu_ctxflush) {
419 unsigned long matchreg, flushreg;
420 u64 val;
422 flushreg = strbuf->strbuf_ctxflush;
423 matchreg = STC_CTXMATCH_ADDR(strbuf, ctx);
425 iommu_write(flushreg, ctx);
426 val = iommu_read(matchreg);
427 val &= 0xffff;
428 if (!val)
429 goto do_flush_sync;
431 while (val) {
432 if (val & 0x1)
433 iommu_write(flushreg, ctx);
434 val >>= 1;
436 val = iommu_read(matchreg);
437 if (unlikely(val)) {
438 printk(KERN_WARNING "strbuf_flush: ctx flush "
439 "timeout matchreg[%llx] ctx[%lx]\n",
440 val, ctx);
441 goto do_page_flush;
443 } else {
444 unsigned long i;
446 do_page_flush:
447 for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE)
448 iommu_write(strbuf->strbuf_pflush, vaddr);
451 do_flush_sync:
452 /* If the device could not have possibly put dirty data into
453 * the streaming cache, no flush-flag synchronization needs
454 * to be performed.
456 if (direction == DMA_TO_DEVICE)
457 return;
459 STC_FLUSHFLAG_INIT(strbuf);
460 iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
461 (void) iommu_read(iommu->write_complete_reg);
463 limit = 100000;
464 while (!STC_FLUSHFLAG_SET(strbuf)) {
465 limit--;
466 if (!limit)
467 break;
468 udelay(1);
469 rmb();
471 if (!limit)
472 printk(KERN_WARNING "strbuf_flush: flushflag timeout "
473 "vaddr[%08x] ctx[%lx] npages[%ld]\n",
474 vaddr, ctx, npages);
477 static void dma_4u_unmap_page(struct device *dev, dma_addr_t bus_addr,
478 size_t sz, enum dma_data_direction direction,
479 struct dma_attrs *attrs)
481 struct iommu *iommu;
482 struct strbuf *strbuf;
483 iopte_t *base;
484 unsigned long flags, npages, ctx, i;
486 if (unlikely(direction == DMA_NONE)) {
487 if (printk_ratelimit())
488 WARN_ON(1);
489 return;
492 iommu = dev->archdata.iommu;
493 strbuf = dev->archdata.stc;
495 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
496 npages >>= IO_PAGE_SHIFT;
497 base = iommu->page_table +
498 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
499 bus_addr &= IO_PAGE_MASK;
501 spin_lock_irqsave(&iommu->lock, flags);
503 /* Record the context, if any. */
504 ctx = 0;
505 if (iommu->iommu_ctxflush)
506 ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
508 /* Step 1: Kick data out of streaming buffers if necessary. */
509 if (strbuf->strbuf_enabled)
510 strbuf_flush(strbuf, iommu, bus_addr, ctx,
511 npages, direction);
513 /* Step 2: Clear out TSB entries. */
514 for (i = 0; i < npages; i++)
515 iopte_make_dummy(iommu, base + i);
517 iommu_range_free(iommu, bus_addr, npages);
519 iommu_free_ctx(iommu, ctx);
521 spin_unlock_irqrestore(&iommu->lock, flags);
524 static int dma_4u_map_sg(struct device *dev, struct scatterlist *sglist,
525 int nelems, enum dma_data_direction direction,
526 struct dma_attrs *attrs)
528 struct scatterlist *s, *outs, *segstart;
529 unsigned long flags, handle, prot, ctx;
530 dma_addr_t dma_next = 0, dma_addr;
531 unsigned int max_seg_size;
532 unsigned long seg_boundary_size;
533 int outcount, incount, i;
534 struct strbuf *strbuf;
535 struct iommu *iommu;
536 unsigned long base_shift;
538 BUG_ON(direction == DMA_NONE);
540 iommu = dev->archdata.iommu;
541 strbuf = dev->archdata.stc;
542 if (nelems == 0 || !iommu)
543 return 0;
545 spin_lock_irqsave(&iommu->lock, flags);
547 ctx = 0;
548 if (iommu->iommu_ctxflush)
549 ctx = iommu_alloc_ctx(iommu);
551 if (strbuf->strbuf_enabled)
552 prot = IOPTE_STREAMING(ctx);
553 else
554 prot = IOPTE_CONSISTENT(ctx);
555 if (direction != DMA_TO_DEVICE)
556 prot |= IOPTE_WRITE;
558 outs = s = segstart = &sglist[0];
559 outcount = 1;
560 incount = nelems;
561 handle = 0;
563 /* Init first segment length for backout at failure */
564 outs->dma_length = 0;
566 max_seg_size = dma_get_max_seg_size(dev);
567 seg_boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
568 IO_PAGE_SIZE) >> IO_PAGE_SHIFT;
569 base_shift = iommu->page_table_map_base >> IO_PAGE_SHIFT;
570 for_each_sg(sglist, s, nelems, i) {
571 unsigned long paddr, npages, entry, out_entry = 0, slen;
572 iopte_t *base;
574 slen = s->length;
575 /* Sanity check */
576 if (slen == 0) {
577 dma_next = 0;
578 continue;
580 /* Allocate iommu entries for that segment */
581 paddr = (unsigned long) SG_ENT_PHYS_ADDRESS(s);
582 npages = iommu_num_pages(paddr, slen, IO_PAGE_SIZE);
583 entry = iommu_range_alloc(dev, iommu, npages, &handle);
585 /* Handle failure */
586 if (unlikely(entry == DMA_ERROR_CODE)) {
587 if (printk_ratelimit())
588 printk(KERN_INFO "iommu_alloc failed, iommu %p paddr %lx"
589 " npages %lx\n", iommu, paddr, npages);
590 goto iommu_map_failed;
593 base = iommu->page_table + entry;
595 /* Convert entry to a dma_addr_t */
596 dma_addr = iommu->page_table_map_base +
597 (entry << IO_PAGE_SHIFT);
598 dma_addr |= (s->offset & ~IO_PAGE_MASK);
600 /* Insert into HW table */
601 paddr &= IO_PAGE_MASK;
602 while (npages--) {
603 iopte_val(*base) = prot | paddr;
604 base++;
605 paddr += IO_PAGE_SIZE;
608 /* If we are in an open segment, try merging */
609 if (segstart != s) {
610 /* We cannot merge if:
611 * - allocated dma_addr isn't contiguous to previous allocation
613 if ((dma_addr != dma_next) ||
614 (outs->dma_length + s->length > max_seg_size) ||
615 (is_span_boundary(out_entry, base_shift,
616 seg_boundary_size, outs, s))) {
617 /* Can't merge: create a new segment */
618 segstart = s;
619 outcount++;
620 outs = sg_next(outs);
621 } else {
622 outs->dma_length += s->length;
626 if (segstart == s) {
627 /* This is a new segment, fill entries */
628 outs->dma_address = dma_addr;
629 outs->dma_length = slen;
630 out_entry = entry;
633 /* Calculate next page pointer for contiguous check */
634 dma_next = dma_addr + slen;
637 spin_unlock_irqrestore(&iommu->lock, flags);
639 if (outcount < incount) {
640 outs = sg_next(outs);
641 outs->dma_address = DMA_ERROR_CODE;
642 outs->dma_length = 0;
645 return outcount;
647 iommu_map_failed:
648 for_each_sg(sglist, s, nelems, i) {
649 if (s->dma_length != 0) {
650 unsigned long vaddr, npages, entry, j;
651 iopte_t *base;
653 vaddr = s->dma_address & IO_PAGE_MASK;
654 npages = iommu_num_pages(s->dma_address, s->dma_length,
655 IO_PAGE_SIZE);
656 iommu_range_free(iommu, vaddr, npages);
658 entry = (vaddr - iommu->page_table_map_base)
659 >> IO_PAGE_SHIFT;
660 base = iommu->page_table + entry;
662 for (j = 0; j < npages; j++)
663 iopte_make_dummy(iommu, base + j);
665 s->dma_address = DMA_ERROR_CODE;
666 s->dma_length = 0;
668 if (s == outs)
669 break;
671 spin_unlock_irqrestore(&iommu->lock, flags);
673 return 0;
676 /* If contexts are being used, they are the same in all of the mappings
677 * we make for a particular SG.
679 static unsigned long fetch_sg_ctx(struct iommu *iommu, struct scatterlist *sg)
681 unsigned long ctx = 0;
683 if (iommu->iommu_ctxflush) {
684 iopte_t *base;
685 u32 bus_addr;
687 bus_addr = sg->dma_address & IO_PAGE_MASK;
688 base = iommu->page_table +
689 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
691 ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
693 return ctx;
696 static void dma_4u_unmap_sg(struct device *dev, struct scatterlist *sglist,
697 int nelems, enum dma_data_direction direction,
698 struct dma_attrs *attrs)
700 unsigned long flags, ctx;
701 struct scatterlist *sg;
702 struct strbuf *strbuf;
703 struct iommu *iommu;
705 BUG_ON(direction == DMA_NONE);
707 iommu = dev->archdata.iommu;
708 strbuf = dev->archdata.stc;
710 ctx = fetch_sg_ctx(iommu, sglist);
712 spin_lock_irqsave(&iommu->lock, flags);
714 sg = sglist;
715 while (nelems--) {
716 dma_addr_t dma_handle = sg->dma_address;
717 unsigned int len = sg->dma_length;
718 unsigned long npages, entry;
719 iopte_t *base;
720 int i;
722 if (!len)
723 break;
724 npages = iommu_num_pages(dma_handle, len, IO_PAGE_SIZE);
725 iommu_range_free(iommu, dma_handle, npages);
727 entry = ((dma_handle - iommu->page_table_map_base)
728 >> IO_PAGE_SHIFT);
729 base = iommu->page_table + entry;
731 dma_handle &= IO_PAGE_MASK;
732 if (strbuf->strbuf_enabled)
733 strbuf_flush(strbuf, iommu, dma_handle, ctx,
734 npages, direction);
736 for (i = 0; i < npages; i++)
737 iopte_make_dummy(iommu, base + i);
739 sg = sg_next(sg);
742 iommu_free_ctx(iommu, ctx);
744 spin_unlock_irqrestore(&iommu->lock, flags);
747 static void dma_4u_sync_single_for_cpu(struct device *dev,
748 dma_addr_t bus_addr, size_t sz,
749 enum dma_data_direction direction)
751 struct iommu *iommu;
752 struct strbuf *strbuf;
753 unsigned long flags, ctx, npages;
755 iommu = dev->archdata.iommu;
756 strbuf = dev->archdata.stc;
758 if (!strbuf->strbuf_enabled)
759 return;
761 spin_lock_irqsave(&iommu->lock, flags);
763 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
764 npages >>= IO_PAGE_SHIFT;
765 bus_addr &= IO_PAGE_MASK;
767 /* Step 1: Record the context, if any. */
768 ctx = 0;
769 if (iommu->iommu_ctxflush &&
770 strbuf->strbuf_ctxflush) {
771 iopte_t *iopte;
773 iopte = iommu->page_table +
774 ((bus_addr - iommu->page_table_map_base)>>IO_PAGE_SHIFT);
775 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
778 /* Step 2: Kick data out of streaming buffers. */
779 strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
781 spin_unlock_irqrestore(&iommu->lock, flags);
784 static void dma_4u_sync_sg_for_cpu(struct device *dev,
785 struct scatterlist *sglist, int nelems,
786 enum dma_data_direction direction)
788 struct iommu *iommu;
789 struct strbuf *strbuf;
790 unsigned long flags, ctx, npages, i;
791 struct scatterlist *sg, *sgprv;
792 u32 bus_addr;
794 iommu = dev->archdata.iommu;
795 strbuf = dev->archdata.stc;
797 if (!strbuf->strbuf_enabled)
798 return;
800 spin_lock_irqsave(&iommu->lock, flags);
802 /* Step 1: Record the context, if any. */
803 ctx = 0;
804 if (iommu->iommu_ctxflush &&
805 strbuf->strbuf_ctxflush) {
806 iopte_t *iopte;
808 iopte = iommu->page_table +
809 ((sglist[0].dma_address - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
810 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
813 /* Step 2: Kick data out of streaming buffers. */
814 bus_addr = sglist[0].dma_address & IO_PAGE_MASK;
815 sgprv = NULL;
816 for_each_sg(sglist, sg, nelems, i) {
817 if (sg->dma_length == 0)
818 break;
819 sgprv = sg;
822 npages = (IO_PAGE_ALIGN(sgprv->dma_address + sgprv->dma_length)
823 - bus_addr) >> IO_PAGE_SHIFT;
824 strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
826 spin_unlock_irqrestore(&iommu->lock, flags);
829 static const struct dma_map_ops sun4u_dma_ops = {
830 .alloc_coherent = dma_4u_alloc_coherent,
831 .free_coherent = dma_4u_free_coherent,
832 .map_page = dma_4u_map_page,
833 .unmap_page = dma_4u_unmap_page,
834 .map_sg = dma_4u_map_sg,
835 .unmap_sg = dma_4u_unmap_sg,
836 .sync_single_for_cpu = dma_4u_sync_single_for_cpu,
837 .sync_sg_for_cpu = dma_4u_sync_sg_for_cpu,
840 const struct dma_map_ops *dma_ops = &sun4u_dma_ops;
841 EXPORT_SYMBOL(dma_ops);
843 int dma_supported(struct device *dev, u64 device_mask)
845 struct iommu *iommu = dev->archdata.iommu;
846 u64 dma_addr_mask = iommu->dma_addr_mask;
848 if (device_mask >= (1UL << 32UL))
849 return 0;
851 if ((device_mask & dma_addr_mask) == dma_addr_mask)
852 return 1;
854 #ifdef CONFIG_PCI
855 if (dev->bus == &pci_bus_type)
856 return pci_dma_supported(to_pci_dev(dev), device_mask);
857 #endif
859 return 0;
861 EXPORT_SYMBOL(dma_supported);
863 int dma_set_mask(struct device *dev, u64 dma_mask)
865 #ifdef CONFIG_PCI
866 if (dev->bus == &pci_bus_type)
867 return pci_set_dma_mask(to_pci_dev(dev), dma_mask);
868 #endif
869 return -EINVAL;
871 EXPORT_SYMBOL(dma_set_mask);