iommu-common.c 7.1 KB

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