7 #include <linux/kernel.h>
8 #include <linux/export.h>
9 #include <linux/slab.h>
11 #include <linux/device.h>
13 #include <linux/errno.h>
15 #include <linux/bitmap.h>
18 #include <linux/pci.h>
21 #include <asm/iommu.h>
25 #define STC_CTXMATCH_ADDR(STC, CTX) \
26 ((STC)->strbuf_ctxmatch_base + ((CTX) << 3))
27 #define STC_FLUSHFLAG_INIT(STC) \
28 (*((STC)->strbuf_flushflag) = 0UL)
29 #define STC_FLUSHFLAG_SET(STC) \
30 (*((STC)->strbuf_flushflag) != 0UL)
32 #define iommu_read(__reg) \
34 __asm__ __volatile__("ldxa [%1] %2, %0" \
36 : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
40 #define iommu_write(__reg, __val) \
41 __asm__ __volatile__("stxa %0, [%1] %2" \
43 : "r" (__val), "r" (__reg), \
44 "i" (ASI_PHYS_BYPASS_EC_E))
47 static void iommu_flushall(
struct iommu *
iommu)
56 for (entry = 0; entry < 16; entry++) {
66 #define IOPTE_CONSISTENT(CTX) \
67 (IOPTE_VALID | IOPTE_CACHE | \
68 (((CTX) << 47) & IOPTE_CONTEXT))
70 #define IOPTE_STREAMING(CTX) \
71 (IOPTE_CONSISTENT(CTX) | IOPTE_STBUF)
76 #define IOPTE_IS_DUMMY(iommu, iopte) \
77 ((iopte_val(*iopte) & IOPTE_PAGE) == (iommu)->dummy_page_pa)
109 if (printk_ratelimit())
114 if (handle && *handle)
119 limit = arena->
limit;
125 if (start >= limit) {
134 boundary_size =
ALIGN(dma_get_seg_boundary(dev) + 1,
178 u32 dma_offset,
u32 dma_addr_mask,
181 unsigned long i,
order, sz, num_tsb_entries;
184 num_tsb_entries = tsbsize /
sizeof(
iopte_t);
193 sz = num_tsb_entries / 8;
194 sz = (sz + 7
UL) & ~7
UL;
196 if (!iommu->
arena.map) {
201 iommu->
arena.limit = num_tsb_entries;
209 page = alloc_pages_node(numa_node,
GFP_KERNEL, 0);
220 page = alloc_pages_node(numa_node,
GFP_KERNEL, order);
223 goto out_free_dummy_page;
227 for (i = 0; i < num_tsb_entries; i++)
228 iopte_make_dummy(iommu, &iommu->
page_table[i]);
244 unsigned long npages)
255 static int iommu_alloc_ctx(
struct iommu *iommu)
273 static inline void iommu_free_ctx(
struct iommu *iommu,
int ctx)
277 if (ctx < iommu->ctx_lowest_free)
282 static void *dma_4u_alloc_coherent(
struct device *dev,
size_t size,
299 page = alloc_pages_node(nid, gfp, order);
310 spin_unlock_irqrestore(&iommu->
lock, flags);
319 ret = (
void *) first_page;
321 first_page =
__pa(first_page);
333 static void dma_4u_free_coherent(
struct device *dev,
size_t size,
347 spin_unlock_irqrestore(&iommu->
lock, flags);
355 unsigned long offset,
size_t sz,
362 unsigned long flags, npages, oaddr;
363 unsigned long i, base_paddr,
ctx;
365 unsigned long iopte_protection;
378 base = alloc_npages(dev, iommu, npages);
381 ctx = iommu_alloc_ctx(iommu);
382 spin_unlock_irqrestore(&iommu->
lock, flags);
388 ((base - iommu->
page_table) << IO_PAGE_SHIFT));
398 for (i = 0; i < npages; i++, base++, base_paddr +=
IO_PAGE_SIZE)
399 iopte_val(*base) = iopte_protection | base_paddr;
404 iommu_free_ctx(iommu, ctx);
406 if (printk_ratelimit())
411 static void strbuf_flush(
struct strbuf *strbuf,
struct iommu *iommu,
412 u32 vaddr,
unsigned long ctx,
unsigned long npages,
419 unsigned long matchreg, flushreg;
439 "timeout matchreg[%llx] ctx[%lx]\n",
473 "vaddr[%08x] ctx[%lx] npages[%ld]\n",
482 struct strbuf *strbuf;
487 if (printk_ratelimit())
510 strbuf_flush(strbuf, iommu, bus_addr, ctx,
514 for (i = 0; i < npages; i++)
515 iopte_make_dummy(iommu, base + i);
519 iommu_free_ctx(iommu, ctx);
521 spin_unlock_irqrestore(&iommu->
lock, flags);
531 unsigned int max_seg_size;
532 unsigned long seg_boundary_size;
533 int outcount, incount,
i;
534 struct strbuf *strbuf;
536 unsigned long base_shift;
542 if (nelems == 0 || !iommu)
549 ctx = iommu_alloc_ctx(iommu);
558 outs = s = segstart = &sglist[0];
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,
571 unsigned long paddr, npages,
entry, out_entry = 0, slen;
587 if (printk_ratelimit())
589 " npages %lx\n", iommu, paddr, npages);
590 goto iommu_map_failed;
614 (outs->dma_length + s->
length > max_seg_size) ||
615 (is_span_boundary(out_entry, base_shift,
616 seg_boundary_size, outs, s))) {
622 outs->dma_length += s->
length;
629 outs->dma_length = slen;
637 spin_unlock_irqrestore(&iommu->
lock, flags);
639 if (outcount < incount) {
642 outs->dma_length = 0;
649 if (s->dma_length != 0) {
654 npages = iommu_num_pages(s->
dma_address, s->dma_length,
662 for (j = 0; j < npages; j++)
663 iopte_make_dummy(iommu, base + j);
671 spin_unlock_irqrestore(&iommu->
lock, flags);
679 static unsigned long fetch_sg_ctx(
struct iommu *iommu,
struct scatterlist *
sg)
681 unsigned long ctx = 0;
702 struct strbuf *strbuf;
710 ctx = fetch_sg_ctx(iommu, sglist);
717 unsigned int len = sg->dma_length;
718 unsigned long npages,
entry;
724 npages = iommu_num_pages(dma_handle, len,
IO_PAGE_SIZE);
733 strbuf_flush(strbuf, iommu, dma_handle, ctx,
736 for (i = 0; i < npages; i++)
737 iopte_make_dummy(iommu, base + i);
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,
752 struct strbuf *strbuf;
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,
789 struct strbuf *strbuf;
817 if (sg->dma_length == 0)
823 - bus_addr) >> IO_PAGE_SHIFT;
824 strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
826 spin_unlock_irqrestore(&iommu->
lock, flags);
830 .alloc = dma_4u_alloc_coherent,
831 .free = 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,
847 struct iommu *iommu = dev->
archdata.iommu;
850 if (device_mask >= (1
UL << 32
UL))
853 if ((device_mask & dma_addr_mask) == dma_addr_mask)