iommu-common.c 7.0 KB

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