aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2015-04-18 18:01:29 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2015-04-18 18:01:29 -0400
commit64fb1d0e975e92e012802d371e417266d6531676 (patch)
treeeb3fba9d0fddc47e29699e689bb45e79a22d1116 /lib
parentdba94f2155f581395ef9608418778e3491b3d470 (diff)
parentccb301862aa51ea7c10c10b440f3e8bbeac5b720 (diff)
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc
Pull sparc fixes from David Miller "Unfortunately, I brown paper bagged the generic iommu pool allocator by applying the wrong revision of the patch series. This reverts the bad one, and puts the right one in" * git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc: iommu-common: Fix PARISC compile-time warnings sparc: Make LDC use common iommu poll management functions sparc: Make sparc64 use scalable lib/iommu-common.c functions Break up monolithic iommu table/lock into finer graularity pools and lock sparc: Revert generic IOMMU allocator.
Diffstat (limited to 'lib')
-rw-r--r--lib/iommu-common.c190
1 files changed, 118 insertions, 72 deletions
diff --git a/lib/iommu-common.c b/lib/iommu-common.c
index fac4f35250c9..a1a517cba7ec 100644
--- a/lib/iommu-common.c
+++ b/lib/iommu-common.c
@@ -9,37 +9,72 @@
9#include <linux/iommu-helper.h> 9#include <linux/iommu-helper.h>
10#include <linux/iommu-common.h> 10#include <linux/iommu-common.h>
11#include <linux/dma-mapping.h> 11#include <linux/dma-mapping.h>
12#include <linux/hash.h>
12 13
13#ifndef DMA_ERROR_CODE 14#ifndef DMA_ERROR_CODE
14#define DMA_ERROR_CODE (~(dma_addr_t)0x0) 15#define DMA_ERROR_CODE (~(dma_addr_t)0x0)
15#endif 16#endif
16 17
17#define IOMMU_LARGE_ALLOC 15 18unsigned long iommu_large_alloc = 15;
19
20static DEFINE_PER_CPU(unsigned int, iommu_pool_hash);
21
22static inline bool need_flush(struct iommu_map_table *iommu)
23{
24 return (iommu->lazy_flush != NULL &&
25 (iommu->flags & IOMMU_NEED_FLUSH) != 0);
26}
27
28static inline void set_flush(struct iommu_map_table *iommu)
29{
30 iommu->flags |= IOMMU_NEED_FLUSH;
31}
32
33static inline void clear_flush(struct iommu_map_table *iommu)
34{
35 iommu->flags &= ~IOMMU_NEED_FLUSH;
36}
37
38static void setup_iommu_pool_hash(void)
39{
40 unsigned int i;
41 static bool do_once;
42
43 if (do_once)
44 return;
45 do_once = true;
46 for_each_possible_cpu(i)
47 per_cpu(iommu_pool_hash, i) = hash_32(i, IOMMU_POOL_HASHBITS);
48}
18 49
19/* 50/*
20 * Initialize iommu_pool entries for the iommu_table. `num_entries' 51 * Initialize iommu_pool entries for the iommu_map_table. `num_entries'
21 * is the number of table entries. If `large_pool' is set to true, 52 * is the number of table entries. If `large_pool' is set to true,
22 * the top 1/4 of the table will be set aside for pool allocations 53 * the top 1/4 of the table will be set aside for pool allocations
23 * of more than IOMMU_LARGE_ALLOC pages. 54 * of more than iommu_large_alloc pages.
24 */ 55 */
25extern void iommu_tbl_pool_init(struct iommu_table *iommu, 56extern void iommu_tbl_pool_init(struct iommu_map_table *iommu,
26 unsigned long num_entries, 57 unsigned long num_entries,
27 u32 page_table_shift, 58 u32 table_shift,
28 const struct iommu_tbl_ops *iommu_tbl_ops, 59 void (*lazy_flush)(struct iommu_map_table *),
29 bool large_pool, u32 npools) 60 bool large_pool, u32 npools,
61 bool skip_span_boundary_check)
30{ 62{
31 unsigned int start, i; 63 unsigned int start, i;
32 struct iommu_pool *p = &(iommu->large_pool); 64 struct iommu_pool *p = &(iommu->large_pool);
33 65
66 setup_iommu_pool_hash();
34 if (npools == 0) 67 if (npools == 0)
35 iommu->nr_pools = IOMMU_NR_POOLS; 68 iommu->nr_pools = IOMMU_NR_POOLS;
36 else 69 else
37 iommu->nr_pools = npools; 70 iommu->nr_pools = npools;
38 BUG_ON(npools > IOMMU_NR_POOLS); 71 BUG_ON(npools > IOMMU_NR_POOLS);
39 72
40 iommu->page_table_shift = page_table_shift; 73 iommu->table_shift = table_shift;
41 iommu->iommu_tbl_ops = iommu_tbl_ops; 74 iommu->lazy_flush = lazy_flush;
42 start = 0; 75 start = 0;
76 if (skip_span_boundary_check)
77 iommu->flags |= IOMMU_NO_SPAN_BOUND;
43 if (large_pool) 78 if (large_pool)
44 iommu->flags |= IOMMU_HAS_LARGE_POOL; 79 iommu->flags |= IOMMU_HAS_LARGE_POOL;
45 80
@@ -48,11 +83,11 @@ extern void iommu_tbl_pool_init(struct iommu_table *iommu,
48 else 83 else
49 iommu->poolsize = (num_entries * 3 / 4)/iommu->nr_pools; 84 iommu->poolsize = (num_entries * 3 / 4)/iommu->nr_pools;
50 for (i = 0; i < iommu->nr_pools; i++) { 85 for (i = 0; i < iommu->nr_pools; i++) {
51 spin_lock_init(&(iommu->arena_pool[i].lock)); 86 spin_lock_init(&(iommu->pools[i].lock));
52 iommu->arena_pool[i].start = start; 87 iommu->pools[i].start = start;
53 iommu->arena_pool[i].hint = start; 88 iommu->pools[i].hint = start;
54 start += iommu->poolsize; /* start for next pool */ 89 start += iommu->poolsize; /* start for next pool */
55 iommu->arena_pool[i].end = start - 1; 90 iommu->pools[i].end = start - 1;
56 } 91 }
57 if (!large_pool) 92 if (!large_pool)
58 return; 93 return;
@@ -65,121 +100,136 @@ extern void iommu_tbl_pool_init(struct iommu_table *iommu,
65EXPORT_SYMBOL(iommu_tbl_pool_init); 100EXPORT_SYMBOL(iommu_tbl_pool_init);
66 101
67unsigned long iommu_tbl_range_alloc(struct device *dev, 102unsigned long iommu_tbl_range_alloc(struct device *dev,
68 struct iommu_table *iommu, 103 struct iommu_map_table *iommu,
69 unsigned long npages, 104 unsigned long npages,
70 unsigned long *handle, 105 unsigned long *handle,
71 unsigned int pool_hash) 106 unsigned long mask,
107 unsigned int align_order)
72{ 108{
109 unsigned int pool_hash = __this_cpu_read(iommu_pool_hash);
73 unsigned long n, end, start, limit, boundary_size; 110 unsigned long n, end, start, limit, boundary_size;
74 struct iommu_pool *arena; 111 struct iommu_pool *pool;
75 int pass = 0; 112 int pass = 0;
76 unsigned int pool_nr; 113 unsigned int pool_nr;
77 unsigned int npools = iommu->nr_pools; 114 unsigned int npools = iommu->nr_pools;
78 unsigned long flags; 115 unsigned long flags;
79 bool large_pool = ((iommu->flags & IOMMU_HAS_LARGE_POOL) != 0); 116 bool large_pool = ((iommu->flags & IOMMU_HAS_LARGE_POOL) != 0);
80 bool largealloc = (large_pool && npages > IOMMU_LARGE_ALLOC); 117 bool largealloc = (large_pool && npages > iommu_large_alloc);
81 unsigned long shift; 118 unsigned long shift;
119 unsigned long align_mask = 0;
120
121 if (align_order > 0)
122 align_mask = 0xffffffffffffffffl >> (64 - align_order);
82 123
83 /* Sanity check */ 124 /* Sanity check */
84 if (unlikely(npages == 0)) { 125 if (unlikely(npages == 0)) {
85 printk_ratelimited("npages == 0\n"); 126 WARN_ON_ONCE(1);
86 return DMA_ERROR_CODE; 127 return DMA_ERROR_CODE;
87 } 128 }
88 129
89 if (largealloc) { 130 if (largealloc) {
90 arena = &(iommu->large_pool); 131 pool = &(iommu->large_pool);
91 spin_lock_irqsave(&arena->lock, flags);
92 pool_nr = 0; /* to keep compiler happy */ 132 pool_nr = 0; /* to keep compiler happy */
93 } else { 133 } else {
94 /* pick out pool_nr */ 134 /* pick out pool_nr */
95 pool_nr = pool_hash & (npools - 1); 135 pool_nr = pool_hash & (npools - 1);
96 arena = &(iommu->arena_pool[pool_nr]); 136 pool = &(iommu->pools[pool_nr]);
97
98 /* find first available unlocked pool */
99 while (!spin_trylock_irqsave(&(arena->lock), flags)) {
100 pool_nr = (pool_nr + 1) & (iommu->nr_pools - 1);
101 arena = &(iommu->arena_pool[pool_nr]);
102 }
103 } 137 }
138 spin_lock_irqsave(&pool->lock, flags);
104 139
105 again: 140 again:
106 if (pass == 0 && handle && *handle && 141 if (pass == 0 && handle && *handle &&
107 (*handle >= arena->start) && (*handle < arena->end)) 142 (*handle >= pool->start) && (*handle < pool->end))
108 start = *handle; 143 start = *handle;
109 else 144 else
110 start = arena->hint; 145 start = pool->hint;
111 146
112 limit = arena->end; 147 limit = pool->end;
113 148
114 /* The case below can happen if we have a small segment appended 149 /* The case below can happen if we have a small segment appended
115 * to a large, or when the previous alloc was at the very end of 150 * to a large, or when the previous alloc was at the very end of
116 * the available space. If so, go back to the beginning and flush. 151 * the available space. If so, go back to the beginning. If a
152 * flush is needed, it will get done based on the return value
153 * from iommu_area_alloc() below.
117 */ 154 */
118 if (start >= limit) { 155 if (start >= limit)
119 start = arena->start; 156 start = pool->start;
120 if (iommu->iommu_tbl_ops->reset != NULL) 157 shift = iommu->table_map_base >> iommu->table_shift;
121 iommu->iommu_tbl_ops->reset(iommu); 158 if (limit + shift > mask) {
159 limit = mask - shift + 1;
160 /* If we're constrained on address range, first try
161 * at the masked hint to avoid O(n) search complexity,
162 * but on second pass, start at 0 in pool 0.
163 */
164 if ((start & mask) >= limit || pass > 0) {
165 spin_unlock(&(pool->lock));
166 pool = &(iommu->pools[0]);
167 spin_lock(&(pool->lock));
168 start = pool->start;
169 } else {
170 start &= mask;
171 }
122 } 172 }
123 173
124 if (dev) 174 if (dev)
125 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1, 175 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
126 1 << iommu->page_table_shift); 176 1 << iommu->table_shift);
127 else 177 else
128 boundary_size = ALIGN(1ULL << 32, 1 << iommu->page_table_shift); 178 boundary_size = ALIGN(1ULL << 32, 1 << iommu->table_shift);
129 179
130 shift = iommu->page_table_map_base >> iommu->page_table_shift; 180 boundary_size = boundary_size >> iommu->table_shift;
131 boundary_size = boundary_size >> iommu->page_table_shift;
132 /* 181 /*
133 * if the iommu has a non-trivial cookie <-> index mapping, we set 182 * if the skip_span_boundary_check had been set during init, we set
134 * things up so that iommu_is_span_boundary() merely checks if the 183 * things up so that iommu_is_span_boundary() merely checks if the
135 * (index + npages) < num_tsb_entries 184 * (index + npages) < num_tsb_entries
136 */ 185 */
137 if (iommu->iommu_tbl_ops->cookie_to_index != NULL) { 186 if ((iommu->flags & IOMMU_NO_SPAN_BOUND) != 0) {
138 shift = 0; 187 shift = 0;
139 boundary_size = iommu->poolsize * iommu->nr_pools; 188 boundary_size = iommu->poolsize * iommu->nr_pools;
140 } 189 }
141 n = iommu_area_alloc(iommu->map, limit, start, npages, shift, 190 n = iommu_area_alloc(iommu->map, limit, start, npages, shift,
142 boundary_size, 0); 191 boundary_size, align_mask);
143 if (n == -1) { 192 if (n == -1) {
144 if (likely(pass == 0)) { 193 if (likely(pass == 0)) {
145 /* First failure, rescan from the beginning. */ 194 /* First failure, rescan from the beginning. */
146 arena->hint = arena->start; 195 pool->hint = pool->start;
147 if (iommu->iommu_tbl_ops->reset != NULL) 196 set_flush(iommu);
148 iommu->iommu_tbl_ops->reset(iommu);
149 pass++; 197 pass++;
150 goto again; 198 goto again;
151 } else if (!largealloc && pass <= iommu->nr_pools) { 199 } else if (!largealloc && pass <= iommu->nr_pools) {
152 spin_unlock(&(arena->lock)); 200 spin_unlock(&(pool->lock));
153 pool_nr = (pool_nr + 1) & (iommu->nr_pools - 1); 201 pool_nr = (pool_nr + 1) & (iommu->nr_pools - 1);
154 arena = &(iommu->arena_pool[pool_nr]); 202 pool = &(iommu->pools[pool_nr]);
155 while (!spin_trylock(&(arena->lock))) { 203 spin_lock(&(pool->lock));
156 pool_nr = (pool_nr + 1) & (iommu->nr_pools - 1); 204 pool->hint = pool->start;
157 arena = &(iommu->arena_pool[pool_nr]); 205 set_flush(iommu);
158 }
159 arena->hint = arena->start;
160 pass++; 206 pass++;
161 goto again; 207 goto again;
162 } else { 208 } else {
163 /* give up */ 209 /* give up */
164 spin_unlock_irqrestore(&(arena->lock), flags); 210 n = DMA_ERROR_CODE;
165 return DMA_ERROR_CODE; 211 goto bail;
166 } 212 }
167 } 213 }
214 if (n < pool->hint || need_flush(iommu)) {
215 clear_flush(iommu);
216 iommu->lazy_flush(iommu);
217 }
168 218
169 end = n + npages; 219 end = n + npages;
170 220 pool->hint = end;
171 arena->hint = end;
172 221
173 /* Update handle for SG allocations */ 222 /* Update handle for SG allocations */
174 if (handle) 223 if (handle)
175 *handle = end; 224 *handle = end;
176 spin_unlock_irqrestore(&(arena->lock), flags); 225bail:
226 spin_unlock_irqrestore(&(pool->lock), flags);
177 227
178 return n; 228 return n;
179} 229}
180EXPORT_SYMBOL(iommu_tbl_range_alloc); 230EXPORT_SYMBOL(iommu_tbl_range_alloc);
181 231
182static struct iommu_pool *get_pool(struct iommu_table *tbl, 232static struct iommu_pool *get_pool(struct iommu_map_table *tbl,
183 unsigned long entry) 233 unsigned long entry)
184{ 234{
185 struct iommu_pool *p; 235 struct iommu_pool *p;
@@ -193,31 +243,27 @@ static struct iommu_pool *get_pool(struct iommu_table *tbl,
193 unsigned int pool_nr = entry / tbl->poolsize; 243 unsigned int pool_nr = entry / tbl->poolsize;
194 244
195 BUG_ON(pool_nr >= tbl->nr_pools); 245 BUG_ON(pool_nr >= tbl->nr_pools);
196 p = &tbl->arena_pool[pool_nr]; 246 p = &tbl->pools[pool_nr];
197 } 247 }
198 return p; 248 return p;
199} 249}
200 250
201void iommu_tbl_range_free(struct iommu_table *iommu, u64 dma_addr, 251/* Caller supplies the index of the entry into the iommu map table
202 unsigned long npages, bool do_demap, void *demap_arg) 252 * itself when the mapping from dma_addr to the entry is not the
253 * default addr->entry mapping below.
254 */
255void iommu_tbl_range_free(struct iommu_map_table *iommu, u64 dma_addr,
256 unsigned long npages, unsigned long entry)
203{ 257{
204 unsigned long entry;
205 struct iommu_pool *pool; 258 struct iommu_pool *pool;
206 unsigned long flags; 259 unsigned long flags;
207 unsigned long shift = iommu->page_table_shift; 260 unsigned long shift = iommu->table_shift;
208 261
209 if (iommu->iommu_tbl_ops->cookie_to_index != NULL) { 262 if (entry == DMA_ERROR_CODE) /* use default addr->entry mapping */
210 entry = (*iommu->iommu_tbl_ops->cookie_to_index)(dma_addr, 263 entry = (dma_addr - iommu->table_map_base) >> shift;
211 demap_arg);
212 } else {
213 entry = (dma_addr - iommu->page_table_map_base) >> shift;
214 }
215 pool = get_pool(iommu, entry); 264 pool = get_pool(iommu, entry);
216 265
217 spin_lock_irqsave(&(pool->lock), flags); 266 spin_lock_irqsave(&(pool->lock), flags);
218 if (do_demap && iommu->iommu_tbl_ops->demap != NULL)
219 (*iommu->iommu_tbl_ops->demap)(demap_arg, entry, npages);
220
221 bitmap_clear(iommu->map, entry, npages); 267 bitmap_clear(iommu->map, entry, npages);
222 spin_unlock_irqrestore(&(pool->lock), flags); 268 spin_unlock_irqrestore(&(pool->lock), flags);
223} 269}