iommu-common.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * IOMMU mmap management and range allocation functions.
  3. * Based almost entirely upon the powerpc iommu allocator.
  4. */
  5. #include <linux/export.h>
  6. #include <linux/bitmap.h>
  7. #include <linux/bug.h>
  8. #include <linux/iommu-helper.h>
  9. #include <linux/iommu-common.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/hash.h>
  12. #ifndef DMA_ERROR_CODE
  13. #define DMA_ERROR_CODE (~(dma_addr_t)0x0)
  14. #endif
  15. static unsigned long iommu_large_alloc = 15;
  16. static DEFINE_PER_CPU(unsigned int, iommu_hash_common);
  17. static inline bool need_flush(struct iommu_map_table *iommu)
  18. {
  19. return ((iommu->flags & IOMMU_NEED_FLUSH) != 0);
  20. }
  21. static inline void set_flush(struct iommu_map_table *iommu)
  22. {
  23. iommu->flags |= IOMMU_NEED_FLUSH;
  24. }
  25. static inline void clear_flush(struct iommu_map_table *iommu)
  26. {
  27. iommu->flags &= ~IOMMU_NEED_FLUSH;
  28. }
  29. static void setup_iommu_pool_hash(void)
  30. {
  31. unsigned int i;
  32. static bool do_once;
  33. if (do_once)
  34. return;
  35. do_once = true;
  36. for_each_possible_cpu(i)
  37. per_cpu(iommu_hash_common, i) = hash_32(i, IOMMU_POOL_HASHBITS);
  38. }
  39. /*
  40. * Initialize iommu_pool entries for the iommu_map_table. `num_entries'
  41. * is the number of table entries. If `large_pool' is set to true,
  42. * the top 1/4 of the table will be set aside for pool allocations
  43. * of more than iommu_large_alloc pages.
  44. */
  45. void iommu_tbl_pool_init(struct iommu_map_table *iommu,
  46. unsigned long num_entries,
  47. u32 table_shift,
  48. void (*lazy_flush)(struct iommu_map_table *),
  49. bool large_pool, u32 npools,
  50. bool skip_span_boundary_check)
  51. {
  52. unsigned int start, i;
  53. struct iommu_pool *p = &(iommu->large_pool);
  54. setup_iommu_pool_hash();
  55. if (npools == 0)
  56. iommu->nr_pools = IOMMU_NR_POOLS;
  57. else
  58. iommu->nr_pools = npools;
  59. BUG_ON(npools > IOMMU_NR_POOLS);
  60. iommu->table_shift = table_shift;
  61. iommu->lazy_flush = lazy_flush;
  62. start = 0;
  63. if (skip_span_boundary_check)
  64. iommu->flags |= IOMMU_NO_SPAN_BOUND;
  65. if (large_pool)
  66. iommu->flags |= IOMMU_HAS_LARGE_POOL;
  67. if (!large_pool)
  68. iommu->poolsize = num_entries/iommu->nr_pools;
  69. else
  70. iommu->poolsize = (num_entries * 3 / 4)/iommu->nr_pools;
  71. for (i = 0; i < iommu->nr_pools; i++) {
  72. spin_lock_init(&(iommu->pools[i].lock));
  73. iommu->pools[i].start = start;
  74. iommu->pools[i].hint = start;
  75. start += iommu->poolsize; /* start for next pool */
  76. iommu->pools[i].end = start - 1;
  77. }
  78. if (!large_pool)
  79. return;
  80. /* initialize large_pool */
  81. spin_lock_init(&(p->lock));
  82. p->start = start;
  83. p->hint = p->start;
  84. p->end = num_entries;
  85. }
  86. EXPORT_SYMBOL(iommu_tbl_pool_init);
  87. unsigned long iommu_tbl_range_alloc(struct device *dev,
  88. struct iommu_map_table *iommu,
  89. unsigned long npages,
  90. unsigned long *handle,
  91. unsigned long mask,
  92. unsigned int align_order)
  93. {
  94. unsigned int pool_hash = __this_cpu_read(iommu_hash_common);
  95. unsigned long n, end, start, limit, boundary_size;
  96. struct iommu_pool *pool;
  97. int pass = 0;
  98. unsigned int pool_nr;
  99. unsigned int npools = iommu->nr_pools;
  100. unsigned long flags;
  101. bool large_pool = ((iommu->flags & IOMMU_HAS_LARGE_POOL) != 0);
  102. bool largealloc = (large_pool && npages > iommu_large_alloc);
  103. unsigned long shift;
  104. unsigned long align_mask = 0;
  105. if (align_order > 0)
  106. align_mask = ~0ul >> (BITS_PER_LONG - align_order);
  107. /* Sanity check */
  108. if (unlikely(npages == 0)) {
  109. WARN_ON_ONCE(1);
  110. return DMA_ERROR_CODE;
  111. }
  112. if (largealloc) {
  113. pool = &(iommu->large_pool);
  114. pool_nr = 0; /* to keep compiler happy */
  115. } else {
  116. /* pick out pool_nr */
  117. pool_nr = pool_hash & (npools - 1);
  118. pool = &(iommu->pools[pool_nr]);
  119. }
  120. spin_lock_irqsave(&pool->lock, flags);
  121. again:
  122. if (pass == 0 && handle && *handle &&
  123. (*handle >= pool->start) && (*handle < pool->end))
  124. start = *handle;
  125. else
  126. start = pool->hint;
  127. limit = pool->end;
  128. /* The case below can happen if we have a small segment appended
  129. * to a large, or when the previous alloc was at the very end of
  130. * the available space. If so, go back to the beginning. If a
  131. * flush is needed, it will get done based on the return value
  132. * from iommu_area_alloc() below.
  133. */
  134. if (start >= limit)
  135. start = pool->start;
  136. shift = iommu->table_map_base >> iommu->table_shift;
  137. if (limit + shift > mask) {
  138. limit = mask - shift + 1;
  139. /* If we're constrained on address range, first try
  140. * at the masked hint to avoid O(n) search complexity,
  141. * but on second pass, start at 0 in pool 0.
  142. */
  143. if ((start & mask) >= limit || pass > 0) {
  144. spin_unlock(&(pool->lock));
  145. pool = &(iommu->pools[0]);
  146. spin_lock(&(pool->lock));
  147. start = pool->start;
  148. } else {
  149. start &= mask;
  150. }
  151. }
  152. if (dev)
  153. boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
  154. 1 << iommu->table_shift);
  155. else
  156. boundary_size = ALIGN(1ULL << 32, 1 << iommu->table_shift);
  157. boundary_size = boundary_size >> iommu->table_shift;
  158. /*
  159. * if the skip_span_boundary_check had been set during init, we set
  160. * things up so that iommu_is_span_boundary() merely checks if the
  161. * (index + npages) < num_tsb_entries
  162. */
  163. if ((iommu->flags & IOMMU_NO_SPAN_BOUND) != 0) {
  164. shift = 0;
  165. boundary_size = iommu->poolsize * iommu->nr_pools;
  166. }
  167. n = iommu_area_alloc(iommu->map, limit, start, npages, shift,
  168. boundary_size, align_mask);
  169. if (n == -1) {
  170. if (likely(pass == 0)) {
  171. /* First failure, rescan from the beginning. */
  172. pool->hint = pool->start;
  173. set_flush(iommu);
  174. pass++;
  175. goto again;
  176. } else if (!largealloc && pass <= iommu->nr_pools) {
  177. spin_unlock(&(pool->lock));
  178. pool_nr = (pool_nr + 1) & (iommu->nr_pools - 1);
  179. pool = &(iommu->pools[pool_nr]);
  180. spin_lock(&(pool->lock));
  181. pool->hint = pool->start;
  182. set_flush(iommu);
  183. pass++;
  184. goto again;
  185. } else {
  186. /* give up */
  187. n = DMA_ERROR_CODE;
  188. goto bail;
  189. }
  190. }
  191. if (iommu->lazy_flush &&
  192. (n < pool->hint || need_flush(iommu))) {
  193. clear_flush(iommu);
  194. iommu->lazy_flush(iommu);
  195. }
  196. end = n + npages;
  197. pool->hint = end;
  198. /* Update handle for SG allocations */
  199. if (handle)
  200. *handle = end;
  201. bail:
  202. spin_unlock_irqrestore(&(pool->lock), flags);
  203. return n;
  204. }
  205. EXPORT_SYMBOL(iommu_tbl_range_alloc);
  206. static struct iommu_pool *get_pool(struct iommu_map_table *tbl,
  207. unsigned long entry)
  208. {
  209. struct iommu_pool *p;
  210. unsigned long largepool_start = tbl->large_pool.start;
  211. bool large_pool = ((tbl->flags & IOMMU_HAS_LARGE_POOL) != 0);
  212. /* The large pool is the last pool at the top of the table */
  213. if (large_pool && entry >= largepool_start) {
  214. p = &tbl->large_pool;
  215. } else {
  216. unsigned int pool_nr = entry / tbl->poolsize;
  217. BUG_ON(pool_nr >= tbl->nr_pools);
  218. p = &tbl->pools[pool_nr];
  219. }
  220. return p;
  221. }
  222. /* Caller supplies the index of the entry into the iommu map table
  223. * itself when the mapping from dma_addr to the entry is not the
  224. * default addr->entry mapping below.
  225. */
  226. void iommu_tbl_range_free(struct iommu_map_table *iommu, u64 dma_addr,
  227. unsigned long npages, unsigned long entry)
  228. {
  229. struct iommu_pool *pool;
  230. unsigned long flags;
  231. unsigned long shift = iommu->table_shift;
  232. if (entry == DMA_ERROR_CODE) /* use default addr->entry mapping */
  233. entry = (dma_addr - iommu->table_map_base) >> shift;
  234. pool = get_pool(iommu, entry);
  235. spin_lock_irqsave(&(pool->lock), flags);
  236. bitmap_clear(iommu->map, entry, npages);
  237. spin_unlock_irqrestore(&(pool->lock), flags);
  238. }
  239. EXPORT_SYMBOL(iommu_tbl_range_free);