aboutsummaryrefslogtreecommitdiffstats
path: root/arch/sparc/kernel/iommu.c
diff options
context:
space:
mode:
authorSowmini Varadhan <sowmini.varadhan@oracle.com>2015-04-09 15:33:31 -0400
committerDavid S. Miller <davem@davemloft.net>2015-04-18 15:32:59 -0400
commitbb620c3d3925aec0ed4f21010c86df08ec18a8c7 (patch)
treef3adfa12c95f9aa6dd844fd47fd486332bbd0a2e /arch/sparc/kernel/iommu.c
parentff7d37a502022149655c18035b99a53391be0383 (diff)
sparc: Make sparc64 use scalable lib/iommu-common.c functions
In iperf experiments running linux as the Tx side (TCP client) with 10 threads results in a severe performance drop when TSO is disabled, indicating a weakness in the software that can be avoided by using the scalable IOMMU arena DMA allocation. Baseline numbers before this patch: with default settings (TSO enabled) : 9-9.5 Gbps Disable TSO using ethtool- drops badly: 2-3 Gbps. After this patch, iperf client with 10 threads, can give a throughput of at least 8.5 Gbps, even when TSO is disabled. Signed-off-by: Sowmini Varadhan <sowmini.varadhan@oracle.com> Acked-by: Benjamin Herrenschmidt <benh@kernel.crashing.org> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'arch/sparc/kernel/iommu.c')
-rw-r--r--arch/sparc/kernel/iommu.c172
1 files changed, 43 insertions, 129 deletions
diff --git a/arch/sparc/kernel/iommu.c b/arch/sparc/kernel/iommu.c
index bfa4d0c2df42..5320689c06e9 100644
--- a/arch/sparc/kernel/iommu.c
+++ b/arch/sparc/kernel/iommu.c
@@ -13,6 +13,7 @@
13#include <linux/errno.h> 13#include <linux/errno.h>
14#include <linux/iommu-helper.h> 14#include <linux/iommu-helper.h>
15#include <linux/bitmap.h> 15#include <linux/bitmap.h>
16#include <linux/iommu-common.h>
16 17
17#ifdef CONFIG_PCI 18#ifdef CONFIG_PCI
18#include <linux/pci.h> 19#include <linux/pci.h>
@@ -45,8 +46,9 @@
45 "i" (ASI_PHYS_BYPASS_EC_E)) 46 "i" (ASI_PHYS_BYPASS_EC_E))
46 47
47/* Must be invoked under the IOMMU lock. */ 48/* Must be invoked under the IOMMU lock. */
48static void iommu_flushall(struct iommu *iommu) 49static void iommu_flushall(struct iommu_map_table *iommu_map_table)
49{ 50{
51 struct iommu *iommu = container_of(iommu_map_table, struct iommu, tbl);
50 if (iommu->iommu_flushinv) { 52 if (iommu->iommu_flushinv) {
51 iommu_write(iommu->iommu_flushinv, ~(u64)0); 53 iommu_write(iommu->iommu_flushinv, ~(u64)0);
52 } else { 54 } else {
@@ -87,94 +89,6 @@ static inline void iopte_make_dummy(struct iommu *iommu, iopte_t *iopte)
87 iopte_val(*iopte) = val; 89 iopte_val(*iopte) = val;
88} 90}
89 91
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.
92 *
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.
96 */
97unsigned long iommu_range_alloc(struct device *dev,
98 struct iommu *iommu,
99 unsigned long npages,
100 unsigned long *handle)
101{
102 unsigned long n, end, start, limit, boundary_size;
103 struct iommu_arena *arena = &iommu->arena;
104 int pass = 0;
105
106 /* This allocator was derived from x86_64's bit string search */
107
108 /* Sanity check */
109 if (unlikely(npages == 0)) {
110 if (printk_ratelimit())
111 WARN_ON(1);
112 return DMA_ERROR_CODE;
113 }
114
115 if (handle && *handle)
116 start = *handle;
117 else
118 start = arena->hint;
119
120 limit = arena->limit;
121
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.
125 */
126 if (start >= limit) {
127 start = 0;
128 if (iommu->flush_all)
129 iommu->flush_all(iommu);
130 }
131
132 again:
133
134 if (dev)
135 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
136 1 << IO_PAGE_SHIFT);
137 else
138 boundary_size = ALIGN(1UL << 32, 1 << IO_PAGE_SHIFT);
139
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);
143 if (n == -1) {
144 if (likely(pass < 1)) {
145 /* First failure, rescan from the beginning. */
146 start = 0;
147 if (iommu->flush_all)
148 iommu->flush_all(iommu);
149 pass++;
150 goto again;
151 } else {
152 /* Second failure, give up */
153 return DMA_ERROR_CODE;
154 }
155 }
156
157 end = n + npages;
158
159 arena->hint = end;
160
161 /* Update handle for SG allocations */
162 if (handle)
163 *handle = end;
164
165 return n;
166}
167
168void iommu_range_free(struct iommu *iommu, dma_addr_t dma_addr, unsigned long npages)
169{
170 struct iommu_arena *arena = &iommu->arena;
171 unsigned long entry;
172
173 entry = (dma_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT;
174
175 bitmap_clear(arena->map, entry, npages);
176}
177
178int iommu_table_init(struct iommu *iommu, int tsbsize, 92int iommu_table_init(struct iommu *iommu, int tsbsize,
179 u32 dma_offset, u32 dma_addr_mask, 93 u32 dma_offset, u32 dma_addr_mask,
180 int numa_node) 94 int numa_node)
@@ -187,22 +101,20 @@ int iommu_table_init(struct iommu *iommu, int tsbsize,
187 /* Setup initial software IOMMU state. */ 101 /* Setup initial software IOMMU state. */
188 spin_lock_init(&iommu->lock); 102 spin_lock_init(&iommu->lock);
189 iommu->ctx_lowest_free = 1; 103 iommu->ctx_lowest_free = 1;
190 iommu->page_table_map_base = dma_offset; 104 iommu->tbl.table_map_base = dma_offset;
191 iommu->dma_addr_mask = dma_addr_mask; 105 iommu->dma_addr_mask = dma_addr_mask;
192 106
193 /* Allocate and initialize the free area map. */ 107 /* Allocate and initialize the free area map. */
194 sz = num_tsb_entries / 8; 108 sz = num_tsb_entries / 8;
195 sz = (sz + 7UL) & ~7UL; 109 sz = (sz + 7UL) & ~7UL;
196 iommu->arena.map = kmalloc_node(sz, GFP_KERNEL, numa_node); 110 iommu->tbl.map = kmalloc_node(sz, GFP_KERNEL, numa_node);
197 if (!iommu->arena.map) { 111 if (!iommu->tbl.map)
198 printk(KERN_ERR "IOMMU: Error, kmalloc(arena.map) failed.\n");
199 return -ENOMEM; 112 return -ENOMEM;
200 } 113 memset(iommu->tbl.map, 0, sz);
201 memset(iommu->arena.map, 0, sz);
202 iommu->arena.limit = num_tsb_entries;
203 114
204 if (tlb_type != hypervisor) 115 iommu_tbl_pool_init(&iommu->tbl, num_tsb_entries, IO_PAGE_SHIFT,
205 iommu->flush_all = iommu_flushall; 116 (tlb_type != hypervisor ? iommu_flushall : NULL),
117 false, 1, false);
206 118
207 /* Allocate and initialize the dummy page which we 119 /* Allocate and initialize the dummy page which we
208 * set inactive IO PTEs to point to. 120 * set inactive IO PTEs to point to.
@@ -235,18 +147,20 @@ out_free_dummy_page:
235 iommu->dummy_page = 0UL; 147 iommu->dummy_page = 0UL;
236 148
237out_free_map: 149out_free_map:
238 kfree(iommu->arena.map); 150 kfree(iommu->tbl.map);
239 iommu->arena.map = NULL; 151 iommu->tbl.map = NULL;
240 152
241 return -ENOMEM; 153 return -ENOMEM;
242} 154}
243 155
244static inline iopte_t *alloc_npages(struct device *dev, struct iommu *iommu, 156static inline iopte_t *alloc_npages(struct device *dev,
157 struct iommu *iommu,
245 unsigned long npages) 158 unsigned long npages)
246{ 159{
247 unsigned long entry; 160 unsigned long entry;
248 161
249 entry = iommu_range_alloc(dev, iommu, npages, NULL); 162 entry = iommu_tbl_range_alloc(dev, &iommu->tbl, npages, NULL,
163 (unsigned long)(-1), 0);
250 if (unlikely(entry == DMA_ERROR_CODE)) 164 if (unlikely(entry == DMA_ERROR_CODE))
251 return NULL; 165 return NULL;
252 166
@@ -284,7 +198,7 @@ static void *dma_4u_alloc_coherent(struct device *dev, size_t size,
284 dma_addr_t *dma_addrp, gfp_t gfp, 198 dma_addr_t *dma_addrp, gfp_t gfp,
285 struct dma_attrs *attrs) 199 struct dma_attrs *attrs)
286{ 200{
287 unsigned long flags, order, first_page; 201 unsigned long order, first_page;
288 struct iommu *iommu; 202 struct iommu *iommu;
289 struct page *page; 203 struct page *page;
290 int npages, nid; 204 int npages, nid;
@@ -306,16 +220,14 @@ static void *dma_4u_alloc_coherent(struct device *dev, size_t size,
306 220
307 iommu = dev->archdata.iommu; 221 iommu = dev->archdata.iommu;
308 222
309 spin_lock_irqsave(&iommu->lock, flags);
310 iopte = alloc_npages(dev, iommu, size >> IO_PAGE_SHIFT); 223 iopte = alloc_npages(dev, iommu, size >> IO_PAGE_SHIFT);
311 spin_unlock_irqrestore(&iommu->lock, flags);
312 224
313 if (unlikely(iopte == NULL)) { 225 if (unlikely(iopte == NULL)) {
314 free_pages(first_page, order); 226 free_pages(first_page, order);
315 return NULL; 227 return NULL;
316 } 228 }
317 229
318 *dma_addrp = (iommu->page_table_map_base + 230 *dma_addrp = (iommu->tbl.table_map_base +
319 ((iopte - iommu->page_table) << IO_PAGE_SHIFT)); 231 ((iopte - iommu->page_table) << IO_PAGE_SHIFT));
320 ret = (void *) first_page; 232 ret = (void *) first_page;
321 npages = size >> IO_PAGE_SHIFT; 233 npages = size >> IO_PAGE_SHIFT;
@@ -336,16 +248,12 @@ static void dma_4u_free_coherent(struct device *dev, size_t size,
336 struct dma_attrs *attrs) 248 struct dma_attrs *attrs)
337{ 249{
338 struct iommu *iommu; 250 struct iommu *iommu;
339 unsigned long flags, order, npages; 251 unsigned long order, npages;
340 252
341 npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT; 253 npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
342 iommu = dev->archdata.iommu; 254 iommu = dev->archdata.iommu;
343 255
344 spin_lock_irqsave(&iommu->lock, flags); 256 iommu_tbl_range_free(&iommu->tbl, dvma, npages, DMA_ERROR_CODE);
345
346 iommu_range_free(iommu, dvma, npages);
347
348 spin_unlock_irqrestore(&iommu->lock, flags);
349 257
350 order = get_order(size); 258 order = get_order(size);
351 if (order < 10) 259 if (order < 10)
@@ -375,8 +283,8 @@ static dma_addr_t dma_4u_map_page(struct device *dev, struct page *page,
375 npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK); 283 npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
376 npages >>= IO_PAGE_SHIFT; 284 npages >>= IO_PAGE_SHIFT;
377 285
378 spin_lock_irqsave(&iommu->lock, flags);
379 base = alloc_npages(dev, iommu, npages); 286 base = alloc_npages(dev, iommu, npages);
287 spin_lock_irqsave(&iommu->lock, flags);
380 ctx = 0; 288 ctx = 0;
381 if (iommu->iommu_ctxflush) 289 if (iommu->iommu_ctxflush)
382 ctx = iommu_alloc_ctx(iommu); 290 ctx = iommu_alloc_ctx(iommu);
@@ -385,7 +293,7 @@ static dma_addr_t dma_4u_map_page(struct device *dev, struct page *page,
385 if (unlikely(!base)) 293 if (unlikely(!base))
386 goto bad; 294 goto bad;
387 295
388 bus_addr = (iommu->page_table_map_base + 296 bus_addr = (iommu->tbl.table_map_base +
389 ((base - iommu->page_table) << IO_PAGE_SHIFT)); 297 ((base - iommu->page_table) << IO_PAGE_SHIFT));
390 ret = bus_addr | (oaddr & ~IO_PAGE_MASK); 298 ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
391 base_paddr = __pa(oaddr & IO_PAGE_MASK); 299 base_paddr = __pa(oaddr & IO_PAGE_MASK);
@@ -496,7 +404,7 @@ static void dma_4u_unmap_page(struct device *dev, dma_addr_t bus_addr,
496 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK); 404 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
497 npages >>= IO_PAGE_SHIFT; 405 npages >>= IO_PAGE_SHIFT;
498 base = iommu->page_table + 406 base = iommu->page_table +
499 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT); 407 ((bus_addr - iommu->tbl.table_map_base) >> IO_PAGE_SHIFT);
500 bus_addr &= IO_PAGE_MASK; 408 bus_addr &= IO_PAGE_MASK;
501 409
502 spin_lock_irqsave(&iommu->lock, flags); 410 spin_lock_irqsave(&iommu->lock, flags);
@@ -515,11 +423,10 @@ static void dma_4u_unmap_page(struct device *dev, dma_addr_t bus_addr,
515 for (i = 0; i < npages; i++) 423 for (i = 0; i < npages; i++)
516 iopte_make_dummy(iommu, base + i); 424 iopte_make_dummy(iommu, base + i);
517 425
518 iommu_range_free(iommu, bus_addr, npages);
519
520 iommu_free_ctx(iommu, ctx); 426 iommu_free_ctx(iommu, ctx);
521
522 spin_unlock_irqrestore(&iommu->lock, flags); 427 spin_unlock_irqrestore(&iommu->lock, flags);
428
429 iommu_tbl_range_free(&iommu->tbl, bus_addr, npages, DMA_ERROR_CODE);
523} 430}
524 431
525static int dma_4u_map_sg(struct device *dev, struct scatterlist *sglist, 432static int dma_4u_map_sg(struct device *dev, struct scatterlist *sglist,
@@ -567,7 +474,7 @@ static int dma_4u_map_sg(struct device *dev, struct scatterlist *sglist,
567 max_seg_size = dma_get_max_seg_size(dev); 474 max_seg_size = dma_get_max_seg_size(dev);
568 seg_boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1, 475 seg_boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
569 IO_PAGE_SIZE) >> IO_PAGE_SHIFT; 476 IO_PAGE_SIZE) >> IO_PAGE_SHIFT;
570 base_shift = iommu->page_table_map_base >> IO_PAGE_SHIFT; 477 base_shift = iommu->tbl.table_map_base >> IO_PAGE_SHIFT;
571 for_each_sg(sglist, s, nelems, i) { 478 for_each_sg(sglist, s, nelems, i) {
572 unsigned long paddr, npages, entry, out_entry = 0, slen; 479 unsigned long paddr, npages, entry, out_entry = 0, slen;
573 iopte_t *base; 480 iopte_t *base;
@@ -581,7 +488,8 @@ static int dma_4u_map_sg(struct device *dev, struct scatterlist *sglist,
581 /* Allocate iommu entries for that segment */ 488 /* Allocate iommu entries for that segment */
582 paddr = (unsigned long) SG_ENT_PHYS_ADDRESS(s); 489 paddr = (unsigned long) SG_ENT_PHYS_ADDRESS(s);
583 npages = iommu_num_pages(paddr, slen, IO_PAGE_SIZE); 490 npages = iommu_num_pages(paddr, slen, IO_PAGE_SIZE);
584 entry = iommu_range_alloc(dev, iommu, npages, &handle); 491 entry = iommu_tbl_range_alloc(dev, &iommu->tbl, npages,
492 &handle, (unsigned long)(-1), 0);
585 493
586 /* Handle failure */ 494 /* Handle failure */
587 if (unlikely(entry == DMA_ERROR_CODE)) { 495 if (unlikely(entry == DMA_ERROR_CODE)) {
@@ -594,7 +502,7 @@ static int dma_4u_map_sg(struct device *dev, struct scatterlist *sglist,
594 base = iommu->page_table + entry; 502 base = iommu->page_table + entry;
595 503
596 /* Convert entry to a dma_addr_t */ 504 /* Convert entry to a dma_addr_t */
597 dma_addr = iommu->page_table_map_base + 505 dma_addr = iommu->tbl.table_map_base +
598 (entry << IO_PAGE_SHIFT); 506 (entry << IO_PAGE_SHIFT);
599 dma_addr |= (s->offset & ~IO_PAGE_MASK); 507 dma_addr |= (s->offset & ~IO_PAGE_MASK);
600 508
@@ -654,15 +562,17 @@ iommu_map_failed:
654 vaddr = s->dma_address & IO_PAGE_MASK; 562 vaddr = s->dma_address & IO_PAGE_MASK;
655 npages = iommu_num_pages(s->dma_address, s->dma_length, 563 npages = iommu_num_pages(s->dma_address, s->dma_length,
656 IO_PAGE_SIZE); 564 IO_PAGE_SIZE);
657 iommu_range_free(iommu, vaddr, npages);
658 565
659 entry = (vaddr - iommu->page_table_map_base) 566 entry = (vaddr - iommu->tbl.table_map_base)
660 >> IO_PAGE_SHIFT; 567 >> IO_PAGE_SHIFT;
661 base = iommu->page_table + entry; 568 base = iommu->page_table + entry;
662 569
663 for (j = 0; j < npages; j++) 570 for (j = 0; j < npages; j++)
664 iopte_make_dummy(iommu, base + j); 571 iopte_make_dummy(iommu, base + j);
665 572
573 iommu_tbl_range_free(&iommu->tbl, vaddr, npages,
574 DMA_ERROR_CODE);
575
666 s->dma_address = DMA_ERROR_CODE; 576 s->dma_address = DMA_ERROR_CODE;
667 s->dma_length = 0; 577 s->dma_length = 0;
668 } 578 }
@@ -684,10 +594,11 @@ static unsigned long fetch_sg_ctx(struct iommu *iommu, struct scatterlist *sg)
684 if (iommu->iommu_ctxflush) { 594 if (iommu->iommu_ctxflush) {
685 iopte_t *base; 595 iopte_t *base;
686 u32 bus_addr; 596 u32 bus_addr;
597 struct iommu_map_table *tbl = &iommu->tbl;
687 598
688 bus_addr = sg->dma_address & IO_PAGE_MASK; 599 bus_addr = sg->dma_address & IO_PAGE_MASK;
689 base = iommu->page_table + 600 base = iommu->page_table +
690 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT); 601 ((bus_addr - tbl->table_map_base) >> IO_PAGE_SHIFT);
691 602
692 ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL; 603 ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
693 } 604 }
@@ -723,9 +634,8 @@ static void dma_4u_unmap_sg(struct device *dev, struct scatterlist *sglist,
723 if (!len) 634 if (!len)
724 break; 635 break;
725 npages = iommu_num_pages(dma_handle, len, IO_PAGE_SIZE); 636 npages = iommu_num_pages(dma_handle, len, IO_PAGE_SIZE);
726 iommu_range_free(iommu, dma_handle, npages);
727 637
728 entry = ((dma_handle - iommu->page_table_map_base) 638 entry = ((dma_handle - iommu->tbl.table_map_base)
729 >> IO_PAGE_SHIFT); 639 >> IO_PAGE_SHIFT);
730 base = iommu->page_table + entry; 640 base = iommu->page_table + entry;
731 641
@@ -737,6 +647,8 @@ static void dma_4u_unmap_sg(struct device *dev, struct scatterlist *sglist,
737 for (i = 0; i < npages; i++) 647 for (i = 0; i < npages; i++)
738 iopte_make_dummy(iommu, base + i); 648 iopte_make_dummy(iommu, base + i);
739 649
650 iommu_tbl_range_free(&iommu->tbl, dma_handle, npages,
651 DMA_ERROR_CODE);
740 sg = sg_next(sg); 652 sg = sg_next(sg);
741 } 653 }
742 654
@@ -770,9 +682,10 @@ static void dma_4u_sync_single_for_cpu(struct device *dev,
770 if (iommu->iommu_ctxflush && 682 if (iommu->iommu_ctxflush &&
771 strbuf->strbuf_ctxflush) { 683 strbuf->strbuf_ctxflush) {
772 iopte_t *iopte; 684 iopte_t *iopte;
685 struct iommu_map_table *tbl = &iommu->tbl;
773 686
774 iopte = iommu->page_table + 687 iopte = iommu->page_table +
775 ((bus_addr - iommu->page_table_map_base)>>IO_PAGE_SHIFT); 688 ((bus_addr - tbl->table_map_base)>>IO_PAGE_SHIFT);
776 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL; 689 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
777 } 690 }
778 691
@@ -805,9 +718,10 @@ static void dma_4u_sync_sg_for_cpu(struct device *dev,
805 if (iommu->iommu_ctxflush && 718 if (iommu->iommu_ctxflush &&
806 strbuf->strbuf_ctxflush) { 719 strbuf->strbuf_ctxflush) {
807 iopte_t *iopte; 720 iopte_t *iopte;
721 struct iommu_map_table *tbl = &iommu->tbl;
808 722
809 iopte = iommu->page_table + 723 iopte = iommu->page_table + ((sglist[0].dma_address -
810 ((sglist[0].dma_address - iommu->page_table_map_base) >> IO_PAGE_SHIFT); 724 tbl->table_map_base) >> IO_PAGE_SHIFT);
811 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL; 725 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
812 } 726 }
813 727