2 * IOMMU mmap management and range allocation functions.
3 * Based almost entirely upon the powerpc iommu allocator.
6 #include <linux/export.h>
7 #include <linux/bitmap.h>
9 #include <linux/iommu-helper.h>
10 #include <linux/iommu-common.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/hash.h>
14 #ifndef DMA_ERROR_CODE
15 #define DMA_ERROR_CODE (~(dma_addr_t)0x0)
18 static unsigned long iommu_large_alloc
= 15;
20 static DEFINE_PER_CPU(unsigned int, iommu_hash_common
);
22 static inline bool need_flush(struct iommu_map_table
*iommu
)
24 return ((iommu
->flags
& IOMMU_NEED_FLUSH
) != 0);
27 static inline void set_flush(struct iommu_map_table
*iommu
)
29 iommu
->flags
|= IOMMU_NEED_FLUSH
;
32 static inline void clear_flush(struct iommu_map_table
*iommu
)
34 iommu
->flags
&= ~IOMMU_NEED_FLUSH
;
37 static void setup_iommu_pool_hash(void)
45 for_each_possible_cpu(i
)
46 per_cpu(iommu_hash_common
, i
) = hash_32(i
, IOMMU_POOL_HASHBITS
);
50 * Initialize iommu_pool entries for the iommu_map_table. `num_entries'
51 * is the number of table entries. If `large_pool' is set to true,
52 * the top 1/4 of the table will be set aside for pool allocations
53 * of more than iommu_large_alloc pages.
55 void iommu_tbl_pool_init(struct iommu_map_table
*iommu
,
56 unsigned long num_entries
,
58 void (*lazy_flush
)(struct iommu_map_table
*),
59 bool large_pool
, u32 npools
,
60 bool skip_span_boundary_check
)
62 unsigned int start
, i
;
63 struct iommu_pool
*p
= &(iommu
->large_pool
);
65 setup_iommu_pool_hash();
67 iommu
->nr_pools
= IOMMU_NR_POOLS
;
69 iommu
->nr_pools
= npools
;
70 BUG_ON(npools
> IOMMU_NR_POOLS
);
72 iommu
->table_shift
= table_shift
;
73 iommu
->lazy_flush
= lazy_flush
;
75 if (skip_span_boundary_check
)
76 iommu
->flags
|= IOMMU_NO_SPAN_BOUND
;
78 iommu
->flags
|= IOMMU_HAS_LARGE_POOL
;
81 iommu
->poolsize
= num_entries
/iommu
->nr_pools
;
83 iommu
->poolsize
= (num_entries
* 3 / 4)/iommu
->nr_pools
;
84 for (i
= 0; i
< iommu
->nr_pools
; i
++) {
85 spin_lock_init(&(iommu
->pools
[i
].lock
));
86 iommu
->pools
[i
].start
= start
;
87 iommu
->pools
[i
].hint
= start
;
88 start
+= iommu
->poolsize
; /* start for next pool */
89 iommu
->pools
[i
].end
= start
- 1;
93 /* initialize large_pool */
94 spin_lock_init(&(p
->lock
));
99 EXPORT_SYMBOL(iommu_tbl_pool_init
);
101 unsigned long iommu_tbl_range_alloc(struct device
*dev
,
102 struct iommu_map_table
*iommu
,
103 unsigned long npages
,
104 unsigned long *handle
,
106 unsigned int align_order
)
108 unsigned int pool_hash
= __this_cpu_read(iommu_hash_common
);
109 unsigned long n
, end
, start
, limit
, boundary_size
;
110 struct iommu_pool
*pool
;
112 unsigned int pool_nr
;
113 unsigned int npools
= iommu
->nr_pools
;
115 bool large_pool
= ((iommu
->flags
& IOMMU_HAS_LARGE_POOL
) != 0);
116 bool largealloc
= (large_pool
&& npages
> iommu_large_alloc
);
118 unsigned long align_mask
= 0;
121 align_mask
= ~0ul >> (BITS_PER_LONG
- align_order
);
124 if (unlikely(npages
== 0)) {
126 return DMA_ERROR_CODE
;
130 pool
= &(iommu
->large_pool
);
131 pool_nr
= 0; /* to keep compiler happy */
133 /* pick out pool_nr */
134 pool_nr
= pool_hash
& (npools
- 1);
135 pool
= &(iommu
->pools
[pool_nr
]);
137 spin_lock_irqsave(&pool
->lock
, flags
);
140 if (pass
== 0 && handle
&& *handle
&&
141 (*handle
>= pool
->start
) && (*handle
< pool
->end
))
148 /* The case below can happen if we have a small segment appended
149 * to a large, or when the previous alloc was at the very end of
150 * the available space. If so, go back to the beginning. If a
151 * flush is needed, it will get done based on the return value
152 * from iommu_area_alloc() below.
156 shift
= iommu
->table_map_base
>> iommu
->table_shift
;
157 if (limit
+ shift
> mask
) {
158 limit
= mask
- shift
+ 1;
159 /* If we're constrained on address range, first try
160 * at the masked hint to avoid O(n) search complexity,
161 * but on second pass, start at 0 in pool 0.
163 if ((start
& mask
) >= limit
|| pass
> 0) {
164 spin_unlock(&(pool
->lock
));
165 pool
= &(iommu
->pools
[0]);
166 spin_lock(&(pool
->lock
));
174 boundary_size
= ALIGN(dma_get_seg_boundary(dev
) + 1,
175 1 << iommu
->table_shift
);
177 boundary_size
= ALIGN(1ULL << 32, 1 << iommu
->table_shift
);
179 boundary_size
= boundary_size
>> iommu
->table_shift
;
181 * if the skip_span_boundary_check had been set during init, we set
182 * things up so that iommu_is_span_boundary() merely checks if the
183 * (index + npages) < num_tsb_entries
185 if ((iommu
->flags
& IOMMU_NO_SPAN_BOUND
) != 0) {
187 boundary_size
= iommu
->poolsize
* iommu
->nr_pools
;
189 n
= iommu_area_alloc(iommu
->map
, limit
, start
, npages
, shift
,
190 boundary_size
, align_mask
);
192 if (likely(pass
== 0)) {
193 /* First failure, rescan from the beginning. */
194 pool
->hint
= pool
->start
;
198 } else if (!largealloc
&& pass
<= iommu
->nr_pools
) {
199 spin_unlock(&(pool
->lock
));
200 pool_nr
= (pool_nr
+ 1) & (iommu
->nr_pools
- 1);
201 pool
= &(iommu
->pools
[pool_nr
]);
202 spin_lock(&(pool
->lock
));
203 pool
->hint
= pool
->start
;
213 if (iommu
->lazy_flush
&&
214 (n
< pool
->hint
|| need_flush(iommu
))) {
216 iommu
->lazy_flush(iommu
);
222 /* Update handle for SG allocations */
226 spin_unlock_irqrestore(&(pool
->lock
), flags
);
230 EXPORT_SYMBOL(iommu_tbl_range_alloc
);
232 static struct iommu_pool
*get_pool(struct iommu_map_table
*tbl
,
235 struct iommu_pool
*p
;
236 unsigned long largepool_start
= tbl
->large_pool
.start
;
237 bool large_pool
= ((tbl
->flags
& IOMMU_HAS_LARGE_POOL
) != 0);
239 /* The large pool is the last pool at the top of the table */
240 if (large_pool
&& entry
>= largepool_start
) {
241 p
= &tbl
->large_pool
;
243 unsigned int pool_nr
= entry
/ tbl
->poolsize
;
245 BUG_ON(pool_nr
>= tbl
->nr_pools
);
246 p
= &tbl
->pools
[pool_nr
];
251 /* Caller supplies the index of the entry into the iommu map table
252 * itself when the mapping from dma_addr to the entry is not the
253 * default addr->entry mapping below.
255 void iommu_tbl_range_free(struct iommu_map_table
*iommu
, u64 dma_addr
,
256 unsigned long npages
, unsigned long entry
)
258 struct iommu_pool
*pool
;
260 unsigned long shift
= iommu
->table_shift
;
262 if (entry
== DMA_ERROR_CODE
) /* use default addr->entry mapping */
263 entry
= (dma_addr
- iommu
->table_map_base
) >> shift
;
264 pool
= get_pool(iommu
, entry
);
266 spin_lock_irqsave(&(pool
->lock
), flags
);
267 bitmap_clear(iommu
->map
, entry
, npages
);
268 spin_unlock_irqrestore(&(pool
->lock
), flags
);
270 EXPORT_SYMBOL(iommu_tbl_range_free
);