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/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>
16 #include <linux/pci.h>
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) \
32 __asm__ __volatile__("ldxa [%1] %2, %0" \
34 : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
38 #define iommu_write(__reg, __val) \
39 __asm__ __volatile__("stxa %0, [%1] %2" \
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);
53 tag = iommu->iommu_tags;
54 for (entry = 0; entry < 16; entry++) {
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);
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,
97 unsigned long *handle)
99 unsigned long n, end, start, limit, boundary_size;
100 struct iommu_arena *arena = &iommu->arena;
103 /* This allocator was derived from x86_64's bit string search */
106 if (unlikely(npages == 0)) {
107 if (printk_ratelimit())
109 return DMA_ERROR_CODE;
112 if (handle && *handle)
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) {
125 if (iommu->flush_all)
126 iommu->flush_all(iommu);
132 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
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);
141 if (likely(pass < 1)) {
142 /* First failure, rescan from the beginning. */
144 if (iommu->flush_all)
145 iommu->flush_all(iommu);
149 /* Second failure, give up */
150 return DMA_ERROR_CODE;
158 /* Update handle for SG allocations */
165 void iommu_range_free(struct iommu *iommu, dma_addr_t dma_addr, unsigned long npages)
167 struct iommu_arena *arena = &iommu->arena;
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,
179 unsigned long i, order, sz, num_tsb_entries;
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");
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);
209 printk(KERN_ERR "IOMMU: Error, gfp(dummy_page) failed.\n");
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);
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]);
231 free_page(iommu->dummy_page);
232 iommu->dummy_page = 0UL;
235 kfree(iommu->arena.map);
236 iommu->arena.map = NULL;
241 static inline iopte_t *alloc_npages(struct device *dev, struct iommu *iommu,
242 unsigned long npages)
246 entry = iommu_range_alloc(dev, iommu, npages, NULL);
247 if (unlikely(entry == DMA_ERROR_CODE))
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");
267 __set_bit(n, iommu->ctx_bitmap);
272 static inline void iommu_free_ctx(struct iommu *iommu, int 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;
291 size = IO_PAGE_ALIGN(size);
292 order = get_order(size);
296 nid = dev->archdata.numa_node;
297 page = alloc_pages_node(nid, gfp, order);
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);
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);
321 iopte_val(*iopte) = (IOPTE_CONSISTENT(0UL) |
323 (first_page & IOPTE_PAGE));
325 first_page += IO_PAGE_SIZE;
331 static void dma_4u_free_coherent(struct device *dev, size_t size,
332 void *cpu, dma_addr_t dvma)
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);
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)
359 struct strbuf *strbuf;
361 unsigned long flags, npages, oaddr;
362 unsigned long i, base_paddr, ctx;
364 unsigned long iopte_protection;
366 iommu = dev->archdata.iommu;
367 strbuf = dev->archdata.stc;
369 if (unlikely(direction == DMA_NONE))
372 oaddr = (unsigned long)(page_address(page) + offset);
373 npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
374 npages >>= IO_PAGE_SHIFT;
376 spin_lock_irqsave(&iommu->lock, flags);
377 base = alloc_npages(dev, iommu, npages);
379 if (iommu->iommu_ctxflush)
380 ctx = iommu_alloc_ctx(iommu);
381 spin_unlock_irqrestore(&iommu->lock, flags);
386 bus_addr = (iommu->page_table_map_base +
387 ((base - iommu->page_table) << IO_PAGE_SHIFT));
388 ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
389 base_paddr = __pa(oaddr & IO_PAGE_MASK);
390 if (strbuf->strbuf_enabled)
391 iopte_protection = IOPTE_STREAMING(ctx);
393 iopte_protection = IOPTE_CONSISTENT(ctx);
394 if (direction != DMA_TO_DEVICE)
395 iopte_protection |= IOPTE_WRITE;
397 for (i = 0; i < npages; i++, base++, base_paddr += IO_PAGE_SIZE)
398 iopte_val(*base) = iopte_protection | base_paddr;
403 iommu_free_ctx(iommu, ctx);
405 if (printk_ratelimit())
407 return DMA_ERROR_CODE;
410 static void strbuf_flush(struct strbuf *strbuf, struct iommu *iommu,
411 u32 vaddr, unsigned long ctx, unsigned long npages,
412 enum dma_data_direction direction)
416 if (strbuf->strbuf_ctxflush &&
417 iommu->iommu_ctxflush) {
418 unsigned long matchreg, flushreg;
421 flushreg = strbuf->strbuf_ctxflush;
422 matchreg = STC_CTXMATCH_ADDR(strbuf, ctx);
424 iommu_write(flushreg, ctx);
425 val = iommu_read(matchreg);
432 iommu_write(flushreg, ctx);
435 val = iommu_read(matchreg);
437 printk(KERN_WARNING "strbuf_flush: ctx flush "
438 "timeout matchreg[%llx] ctx[%lx]\n",
446 for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE)
447 iommu_write(strbuf->strbuf_pflush, vaddr);
451 /* If the device could not have possibly put dirty data into
452 * the streaming cache, no flush-flag synchronization needs
455 if (direction == DMA_TO_DEVICE)
458 STC_FLUSHFLAG_INIT(strbuf);
459 iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
460 (void) iommu_read(iommu->write_complete_reg);
463 while (!STC_FLUSHFLAG_SET(strbuf)) {
471 printk(KERN_WARNING "strbuf_flush: flushflag timeout "
472 "vaddr[%08x] ctx[%lx] npages[%ld]\n",
476 static void dma_4u_unmap_page(struct device *dev, dma_addr_t bus_addr,
477 size_t sz, enum dma_data_direction direction)
480 struct strbuf *strbuf;
482 unsigned long flags, npages, ctx, i;
484 if (unlikely(direction == DMA_NONE)) {
485 if (printk_ratelimit())
490 iommu = dev->archdata.iommu;
491 strbuf = dev->archdata.stc;
493 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
494 npages >>= IO_PAGE_SHIFT;
495 base = iommu->page_table +
496 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
497 bus_addr &= IO_PAGE_MASK;
499 spin_lock_irqsave(&iommu->lock, flags);
501 /* Record the context, if any. */
503 if (iommu->iommu_ctxflush)
504 ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
506 /* Step 1: Kick data out of streaming buffers if necessary. */
507 if (strbuf->strbuf_enabled)
508 strbuf_flush(strbuf, iommu, bus_addr, ctx,
511 /* Step 2: Clear out TSB entries. */
512 for (i = 0; i < npages; i++)
513 iopte_make_dummy(iommu, base + i);
515 iommu_range_free(iommu, bus_addr, npages);
517 iommu_free_ctx(iommu, ctx);
519 spin_unlock_irqrestore(&iommu->lock, flags);
522 static int dma_4u_map_sg(struct device *dev, struct scatterlist *sglist,
523 int nelems, enum dma_data_direction direction)
525 struct scatterlist *s, *outs, *segstart;
526 unsigned long flags, handle, prot, ctx;
527 dma_addr_t dma_next = 0, dma_addr;
528 unsigned int max_seg_size;
529 unsigned long seg_boundary_size;
530 int outcount, incount, i;
531 struct strbuf *strbuf;
533 unsigned long base_shift;
535 BUG_ON(direction == DMA_NONE);
537 iommu = dev->archdata.iommu;
538 strbuf = dev->archdata.stc;
539 if (nelems == 0 || !iommu)
542 spin_lock_irqsave(&iommu->lock, flags);
545 if (iommu->iommu_ctxflush)
546 ctx = iommu_alloc_ctx(iommu);
548 if (strbuf->strbuf_enabled)
549 prot = IOPTE_STREAMING(ctx);
551 prot = IOPTE_CONSISTENT(ctx);
552 if (direction != DMA_TO_DEVICE)
555 outs = s = segstart = &sglist[0];
560 /* Init first segment length for backout at failure */
561 outs->dma_length = 0;
563 max_seg_size = dma_get_max_seg_size(dev);
564 seg_boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
565 IO_PAGE_SIZE) >> IO_PAGE_SHIFT;
566 base_shift = iommu->page_table_map_base >> IO_PAGE_SHIFT;
567 for_each_sg(sglist, s, nelems, i) {
568 unsigned long paddr, npages, entry, out_entry = 0, slen;
577 /* Allocate iommu entries for that segment */
578 paddr = (unsigned long) SG_ENT_PHYS_ADDRESS(s);
579 npages = iommu_num_pages(paddr, slen, IO_PAGE_SIZE);
580 entry = iommu_range_alloc(dev, iommu, npages, &handle);
583 if (unlikely(entry == DMA_ERROR_CODE)) {
584 if (printk_ratelimit())
585 printk(KERN_INFO "iommu_alloc failed, iommu %p paddr %lx"
586 " npages %lx\n", iommu, paddr, npages);
587 goto iommu_map_failed;
590 base = iommu->page_table + entry;
592 /* Convert entry to a dma_addr_t */
593 dma_addr = iommu->page_table_map_base +
594 (entry << IO_PAGE_SHIFT);
595 dma_addr |= (s->offset & ~IO_PAGE_MASK);
597 /* Insert into HW table */
598 paddr &= IO_PAGE_MASK;
600 iopte_val(*base) = prot | paddr;
602 paddr += IO_PAGE_SIZE;
605 /* If we are in an open segment, try merging */
607 /* We cannot merge if:
608 * - allocated dma_addr isn't contiguous to previous allocation
610 if ((dma_addr != dma_next) ||
611 (outs->dma_length + s->length > max_seg_size) ||
612 (is_span_boundary(out_entry, base_shift,
613 seg_boundary_size, outs, s))) {
614 /* Can't merge: create a new segment */
617 outs = sg_next(outs);
619 outs->dma_length += s->length;
624 /* This is a new segment, fill entries */
625 outs->dma_address = dma_addr;
626 outs->dma_length = slen;
630 /* Calculate next page pointer for contiguous check */
631 dma_next = dma_addr + slen;
634 spin_unlock_irqrestore(&iommu->lock, flags);
636 if (outcount < incount) {
637 outs = sg_next(outs);
638 outs->dma_address = DMA_ERROR_CODE;
639 outs->dma_length = 0;
645 for_each_sg(sglist, s, nelems, i) {
646 if (s->dma_length != 0) {
647 unsigned long vaddr, npages, entry, j;
650 vaddr = s->dma_address & IO_PAGE_MASK;
651 npages = iommu_num_pages(s->dma_address, s->dma_length,
653 iommu_range_free(iommu, vaddr, npages);
655 entry = (vaddr - iommu->page_table_map_base)
657 base = iommu->page_table + entry;
659 for (j = 0; j < npages; j++)
660 iopte_make_dummy(iommu, base + j);
662 s->dma_address = DMA_ERROR_CODE;
668 spin_unlock_irqrestore(&iommu->lock, flags);
673 /* If contexts are being used, they are the same in all of the mappings
674 * we make for a particular SG.
676 static unsigned long fetch_sg_ctx(struct iommu *iommu, struct scatterlist *sg)
678 unsigned long ctx = 0;
680 if (iommu->iommu_ctxflush) {
684 bus_addr = sg->dma_address & IO_PAGE_MASK;
685 base = iommu->page_table +
686 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
688 ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
693 static void dma_4u_unmap_sg(struct device *dev, struct scatterlist *sglist,
694 int nelems, enum dma_data_direction direction)
696 unsigned long flags, ctx;
697 struct scatterlist *sg;
698 struct strbuf *strbuf;
701 BUG_ON(direction == DMA_NONE);
703 iommu = dev->archdata.iommu;
704 strbuf = dev->archdata.stc;
706 ctx = fetch_sg_ctx(iommu, sglist);
708 spin_lock_irqsave(&iommu->lock, flags);
712 dma_addr_t dma_handle = sg->dma_address;
713 unsigned int len = sg->dma_length;
714 unsigned long npages, entry;
720 npages = iommu_num_pages(dma_handle, len, IO_PAGE_SIZE);
721 iommu_range_free(iommu, dma_handle, npages);
723 entry = ((dma_handle - iommu->page_table_map_base)
725 base = iommu->page_table + entry;
727 dma_handle &= IO_PAGE_MASK;
728 if (strbuf->strbuf_enabled)
729 strbuf_flush(strbuf, iommu, dma_handle, ctx,
732 for (i = 0; i < npages; i++)
733 iopte_make_dummy(iommu, base + i);
738 iommu_free_ctx(iommu, ctx);
740 spin_unlock_irqrestore(&iommu->lock, flags);
743 static void dma_4u_sync_single_for_cpu(struct device *dev,
744 dma_addr_t bus_addr, size_t sz,
745 enum dma_data_direction direction)
748 struct strbuf *strbuf;
749 unsigned long flags, ctx, npages;
751 iommu = dev->archdata.iommu;
752 strbuf = dev->archdata.stc;
754 if (!strbuf->strbuf_enabled)
757 spin_lock_irqsave(&iommu->lock, flags);
759 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
760 npages >>= IO_PAGE_SHIFT;
761 bus_addr &= IO_PAGE_MASK;
763 /* Step 1: Record the context, if any. */
765 if (iommu->iommu_ctxflush &&
766 strbuf->strbuf_ctxflush) {
769 iopte = iommu->page_table +
770 ((bus_addr - iommu->page_table_map_base)>>IO_PAGE_SHIFT);
771 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
774 /* Step 2: Kick data out of streaming buffers. */
775 strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
777 spin_unlock_irqrestore(&iommu->lock, flags);
780 static void dma_4u_sync_sg_for_cpu(struct device *dev,
781 struct scatterlist *sglist, int nelems,
782 enum dma_data_direction direction)
785 struct strbuf *strbuf;
786 unsigned long flags, ctx, npages, i;
787 struct scatterlist *sg, *sgprv;
790 iommu = dev->archdata.iommu;
791 strbuf = dev->archdata.stc;
793 if (!strbuf->strbuf_enabled)
796 spin_lock_irqsave(&iommu->lock, flags);
798 /* Step 1: Record the context, if any. */
800 if (iommu->iommu_ctxflush &&
801 strbuf->strbuf_ctxflush) {
804 iopte = iommu->page_table +
805 ((sglist[0].dma_address - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
806 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
809 /* Step 2: Kick data out of streaming buffers. */
810 bus_addr = sglist[0].dma_address & IO_PAGE_MASK;
812 for_each_sg(sglist, sg, nelems, i) {
813 if (sg->dma_length == 0)
818 npages = (IO_PAGE_ALIGN(sgprv->dma_address + sgprv->dma_length)
819 - bus_addr) >> IO_PAGE_SHIFT;
820 strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
822 spin_unlock_irqrestore(&iommu->lock, flags);
825 static const struct dma_ops sun4u_dma_ops = {
826 .alloc_coherent = dma_4u_alloc_coherent,
827 .free_coherent = dma_4u_free_coherent,
828 .map_page = dma_4u_map_page,
829 .unmap_page = dma_4u_unmap_page,
830 .map_sg = dma_4u_map_sg,
831 .unmap_sg = dma_4u_unmap_sg,
832 .sync_single_for_cpu = dma_4u_sync_single_for_cpu,
833 .sync_sg_for_cpu = dma_4u_sync_sg_for_cpu,
836 const struct dma_ops *dma_ops = &sun4u_dma_ops;
837 EXPORT_SYMBOL(dma_ops);
839 int dma_supported(struct device *dev, u64 device_mask)
841 struct iommu *iommu = dev->archdata.iommu;
842 u64 dma_addr_mask = iommu->dma_addr_mask;
844 if (device_mask >= (1UL << 32UL))
847 if ((device_mask & dma_addr_mask) == dma_addr_mask)
851 if (dev->bus == &pci_bus_type)
852 return pci_dma_supported(to_pci_dev(dev), device_mask);
857 EXPORT_SYMBOL(dma_supported);
859 int dma_set_mask(struct device *dev, u64 dma_mask)
862 if (dev->bus == &pci_bus_type)
863 return pci_set_dma_mask(to_pci_dev(dev), dma_mask);
867 EXPORT_SYMBOL(dma_set_mask);