iova.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright © 2006-2009, Intel Corporation.
  4. *
  5. * Author: Anil S Keshavamurthy <[email protected]>
  6. */
  7. #include <linux/iova.h>
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <linux/smp.h>
  11. #include <linux/bitops.h>
  12. #include <linux/cpu.h>
  13. #include <trace/hooks/iommu.h>
  14. /* The anchor node sits above the top of the usable address space */
  15. #define IOVA_ANCHOR ~0UL
  16. #define IOVA_RANGE_CACHE_MAX_SIZE 6 /* log of max cached IOVA range size (in pages) */
  17. static bool iova_rcache_insert(struct iova_domain *iovad,
  18. unsigned long pfn,
  19. unsigned long size);
  20. static unsigned long iova_rcache_get(struct iova_domain *iovad,
  21. unsigned long size,
  22. unsigned long limit_pfn);
  23. static void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad);
  24. static void free_iova_rcaches(struct iova_domain *iovad);
  25. unsigned long iova_rcache_range(void)
  26. {
  27. return PAGE_SIZE << (IOVA_RANGE_CACHE_MAX_SIZE - 1);
  28. }
  29. static int iova_cpuhp_dead(unsigned int cpu, struct hlist_node *node)
  30. {
  31. struct iova_domain *iovad;
  32. iovad = hlist_entry_safe(node, struct iova_domain, cpuhp_dead);
  33. free_cpu_cached_iovas(cpu, iovad);
  34. return 0;
  35. }
  36. static void free_global_cached_iovas(struct iova_domain *iovad);
  37. static struct iova *to_iova(struct rb_node *node)
  38. {
  39. return rb_entry(node, struct iova, node);
  40. }
  41. void
  42. init_iova_domain(struct iova_domain *iovad, unsigned long granule,
  43. unsigned long start_pfn)
  44. {
  45. /*
  46. * IOVA granularity will normally be equal to the smallest
  47. * supported IOMMU page size; both *must* be capable of
  48. * representing individual CPU pages exactly.
  49. */
  50. BUG_ON((granule > PAGE_SIZE) || !is_power_of_2(granule));
  51. spin_lock_init(&iovad->iova_rbtree_lock);
  52. iovad->rbroot = RB_ROOT;
  53. iovad->cached_node = &iovad->anchor.node;
  54. iovad->cached32_node = &iovad->anchor.node;
  55. iovad->granule = granule;
  56. iovad->start_pfn = start_pfn;
  57. iovad->dma_32bit_pfn = 1UL << (32 - iova_shift(iovad));
  58. iovad->max32_alloc_size = iovad->dma_32bit_pfn;
  59. iovad->anchor.pfn_lo = iovad->anchor.pfn_hi = IOVA_ANCHOR;
  60. rb_link_node(&iovad->anchor.node, NULL, &iovad->rbroot.rb_node);
  61. rb_insert_color(&iovad->anchor.node, &iovad->rbroot);
  62. android_init_vendor_data(iovad, 1);
  63. }
  64. EXPORT_SYMBOL_GPL(init_iova_domain);
  65. static struct rb_node *
  66. __get_cached_rbnode(struct iova_domain *iovad, unsigned long limit_pfn)
  67. {
  68. if (limit_pfn <= iovad->dma_32bit_pfn)
  69. return iovad->cached32_node;
  70. return iovad->cached_node;
  71. }
  72. static void
  73. __cached_rbnode_insert_update(struct iova_domain *iovad, struct iova *new)
  74. {
  75. if (new->pfn_hi < iovad->dma_32bit_pfn)
  76. iovad->cached32_node = &new->node;
  77. else
  78. iovad->cached_node = &new->node;
  79. }
  80. static void
  81. __cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
  82. {
  83. struct iova *cached_iova;
  84. cached_iova = to_iova(iovad->cached32_node);
  85. if (free == cached_iova ||
  86. (free->pfn_hi < iovad->dma_32bit_pfn &&
  87. free->pfn_lo >= cached_iova->pfn_lo))
  88. iovad->cached32_node = rb_next(&free->node);
  89. if (free->pfn_lo < iovad->dma_32bit_pfn)
  90. iovad->max32_alloc_size = iovad->dma_32bit_pfn;
  91. cached_iova = to_iova(iovad->cached_node);
  92. if (free->pfn_lo >= cached_iova->pfn_lo)
  93. iovad->cached_node = rb_next(&free->node);
  94. }
  95. static struct rb_node *iova_find_limit(struct iova_domain *iovad, unsigned long limit_pfn)
  96. {
  97. struct rb_node *node, *next;
  98. /*
  99. * Ideally what we'd like to judge here is whether limit_pfn is close
  100. * enough to the highest-allocated IOVA that starting the allocation
  101. * walk from the anchor node will be quicker than this initial work to
  102. * find an exact starting point (especially if that ends up being the
  103. * anchor node anyway). This is an incredibly crude approximation which
  104. * only really helps the most likely case, but is at least trivially easy.
  105. */
  106. if (limit_pfn > iovad->dma_32bit_pfn)
  107. return &iovad->anchor.node;
  108. node = iovad->rbroot.rb_node;
  109. while (to_iova(node)->pfn_hi < limit_pfn)
  110. node = node->rb_right;
  111. search_left:
  112. while (node->rb_left && to_iova(node->rb_left)->pfn_lo >= limit_pfn)
  113. node = node->rb_left;
  114. if (!node->rb_left)
  115. return node;
  116. next = node->rb_left;
  117. while (next->rb_right) {
  118. next = next->rb_right;
  119. if (to_iova(next)->pfn_lo >= limit_pfn) {
  120. node = next;
  121. goto search_left;
  122. }
  123. }
  124. return node;
  125. }
  126. /* Insert the iova into domain rbtree by holding writer lock */
  127. static void
  128. iova_insert_rbtree(struct rb_root *root, struct iova *iova,
  129. struct rb_node *start)
  130. {
  131. struct rb_node **new, *parent = NULL;
  132. new = (start) ? &start : &(root->rb_node);
  133. /* Figure out where to put new node */
  134. while (*new) {
  135. struct iova *this = to_iova(*new);
  136. parent = *new;
  137. if (iova->pfn_lo < this->pfn_lo)
  138. new = &((*new)->rb_left);
  139. else if (iova->pfn_lo > this->pfn_lo)
  140. new = &((*new)->rb_right);
  141. else {
  142. WARN_ON(1); /* this should not happen */
  143. return;
  144. }
  145. }
  146. /* Add new node and rebalance tree. */
  147. rb_link_node(&iova->node, parent, new);
  148. rb_insert_color(&iova->node, root);
  149. }
  150. static int __alloc_and_insert_iova_range(struct iova_domain *iovad,
  151. unsigned long size, unsigned long limit_pfn,
  152. struct iova *new, bool size_aligned)
  153. {
  154. struct rb_node *curr, *prev;
  155. struct iova *curr_iova;
  156. unsigned long flags;
  157. unsigned long new_pfn, retry_pfn;
  158. unsigned long align_mask = ~0UL;
  159. unsigned long high_pfn = limit_pfn, low_pfn = iovad->start_pfn;
  160. if (size_aligned) {
  161. unsigned long shift = fls_long(size - 1);
  162. trace_android_rvh_iommu_limit_align_shift(iovad, size, &shift);
  163. align_mask <<= shift;
  164. }
  165. /* Walk the tree backwards */
  166. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  167. if (limit_pfn <= iovad->dma_32bit_pfn &&
  168. size >= iovad->max32_alloc_size)
  169. goto iova32_full;
  170. curr = __get_cached_rbnode(iovad, limit_pfn);
  171. curr_iova = to_iova(curr);
  172. retry_pfn = curr_iova->pfn_hi;
  173. retry:
  174. do {
  175. high_pfn = min(high_pfn, curr_iova->pfn_lo);
  176. new_pfn = (high_pfn - size) & align_mask;
  177. prev = curr;
  178. curr = rb_prev(curr);
  179. curr_iova = to_iova(curr);
  180. } while (curr && new_pfn <= curr_iova->pfn_hi && new_pfn >= low_pfn);
  181. if (high_pfn < size || new_pfn < low_pfn) {
  182. if (low_pfn == iovad->start_pfn && retry_pfn < limit_pfn) {
  183. high_pfn = limit_pfn;
  184. low_pfn = retry_pfn + 1;
  185. curr = iova_find_limit(iovad, limit_pfn);
  186. curr_iova = to_iova(curr);
  187. goto retry;
  188. }
  189. iovad->max32_alloc_size = size;
  190. goto iova32_full;
  191. }
  192. /* pfn_lo will point to size aligned address if size_aligned is set */
  193. new->pfn_lo = new_pfn;
  194. new->pfn_hi = new->pfn_lo + size - 1;
  195. /* If we have 'prev', it's a valid place to start the insertion. */
  196. iova_insert_rbtree(&iovad->rbroot, new, prev);
  197. __cached_rbnode_insert_update(iovad, new);
  198. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  199. return 0;
  200. iova32_full:
  201. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  202. return -ENOMEM;
  203. }
  204. static struct kmem_cache *iova_cache;
  205. static unsigned int iova_cache_users;
  206. static DEFINE_MUTEX(iova_cache_mutex);
  207. static struct iova *alloc_iova_mem(void)
  208. {
  209. return kmem_cache_zalloc(iova_cache, GFP_ATOMIC | __GFP_NOWARN);
  210. }
  211. static void free_iova_mem(struct iova *iova)
  212. {
  213. if (iova->pfn_lo != IOVA_ANCHOR)
  214. kmem_cache_free(iova_cache, iova);
  215. }
  216. int iova_cache_get(void)
  217. {
  218. mutex_lock(&iova_cache_mutex);
  219. if (!iova_cache_users) {
  220. int ret;
  221. ret = cpuhp_setup_state_multi(CPUHP_IOMMU_IOVA_DEAD, "iommu/iova:dead", NULL,
  222. iova_cpuhp_dead);
  223. if (ret) {
  224. mutex_unlock(&iova_cache_mutex);
  225. pr_err("Couldn't register cpuhp handler\n");
  226. return ret;
  227. }
  228. iova_cache = kmem_cache_create(
  229. "iommu_iova", sizeof(struct iova), 0,
  230. SLAB_HWCACHE_ALIGN, NULL);
  231. if (!iova_cache) {
  232. cpuhp_remove_multi_state(CPUHP_IOMMU_IOVA_DEAD);
  233. mutex_unlock(&iova_cache_mutex);
  234. pr_err("Couldn't create iova cache\n");
  235. return -ENOMEM;
  236. }
  237. }
  238. iova_cache_users++;
  239. mutex_unlock(&iova_cache_mutex);
  240. return 0;
  241. }
  242. EXPORT_SYMBOL_GPL(iova_cache_get);
  243. void iova_cache_put(void)
  244. {
  245. mutex_lock(&iova_cache_mutex);
  246. if (WARN_ON(!iova_cache_users)) {
  247. mutex_unlock(&iova_cache_mutex);
  248. return;
  249. }
  250. iova_cache_users--;
  251. if (!iova_cache_users) {
  252. cpuhp_remove_multi_state(CPUHP_IOMMU_IOVA_DEAD);
  253. kmem_cache_destroy(iova_cache);
  254. }
  255. mutex_unlock(&iova_cache_mutex);
  256. }
  257. EXPORT_SYMBOL_GPL(iova_cache_put);
  258. /**
  259. * alloc_iova - allocates an iova
  260. * @iovad: - iova domain in question
  261. * @size: - size of page frames to allocate
  262. * @limit_pfn: - max limit address
  263. * @size_aligned: - set if size_aligned address range is required
  264. * This function allocates an iova in the range iovad->start_pfn to limit_pfn,
  265. * searching top-down from limit_pfn to iovad->start_pfn. If the size_aligned
  266. * flag is set then the allocated address iova->pfn_lo will be naturally
  267. * aligned on roundup_power_of_two(size).
  268. */
  269. struct iova *
  270. alloc_iova(struct iova_domain *iovad, unsigned long size,
  271. unsigned long limit_pfn,
  272. bool size_aligned)
  273. {
  274. struct iova *new_iova;
  275. int ret = -1;
  276. new_iova = alloc_iova_mem();
  277. if (!new_iova)
  278. return NULL;
  279. trace_android_rvh_iommu_alloc_insert_iova(iovad, size, limit_pfn + 1,
  280. new_iova, size_aligned, &ret);
  281. if (ret) {
  282. ret = __alloc_and_insert_iova_range(iovad, size,
  283. limit_pfn + 1, new_iova, size_aligned);
  284. }
  285. if (ret) {
  286. free_iova_mem(new_iova);
  287. return NULL;
  288. }
  289. return new_iova;
  290. }
  291. EXPORT_SYMBOL_GPL(alloc_iova);
  292. static struct iova *
  293. private_find_iova(struct iova_domain *iovad, unsigned long pfn)
  294. {
  295. struct rb_node *node = iovad->rbroot.rb_node;
  296. assert_spin_locked(&iovad->iova_rbtree_lock);
  297. while (node) {
  298. struct iova *iova = to_iova(node);
  299. if (pfn < iova->pfn_lo)
  300. node = node->rb_left;
  301. else if (pfn > iova->pfn_hi)
  302. node = node->rb_right;
  303. else
  304. return iova; /* pfn falls within iova's range */
  305. }
  306. return NULL;
  307. }
  308. static void remove_iova(struct iova_domain *iovad, struct iova *iova)
  309. {
  310. assert_spin_locked(&iovad->iova_rbtree_lock);
  311. __cached_rbnode_delete_update(iovad, iova);
  312. rb_erase(&iova->node, &iovad->rbroot);
  313. }
  314. /**
  315. * find_iova - finds an iova for a given pfn
  316. * @iovad: - iova domain in question.
  317. * @pfn: - page frame number
  318. * This function finds and returns an iova belonging to the
  319. * given domain which matches the given pfn.
  320. */
  321. struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn)
  322. {
  323. unsigned long flags;
  324. struct iova *iova;
  325. /* Take the lock so that no other thread is manipulating the rbtree */
  326. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  327. iova = private_find_iova(iovad, pfn);
  328. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  329. return iova;
  330. }
  331. EXPORT_SYMBOL_GPL(find_iova);
  332. /**
  333. * __free_iova - frees the given iova
  334. * @iovad: iova domain in question.
  335. * @iova: iova in question.
  336. * Frees the given iova belonging to the giving domain
  337. */
  338. void
  339. __free_iova(struct iova_domain *iovad, struct iova *iova)
  340. {
  341. unsigned long flags;
  342. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  343. remove_iova(iovad, iova);
  344. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  345. free_iova_mem(iova);
  346. }
  347. EXPORT_SYMBOL_GPL(__free_iova);
  348. /**
  349. * free_iova - finds and frees the iova for a given pfn
  350. * @iovad: - iova domain in question.
  351. * @pfn: - pfn that is allocated previously
  352. * This functions finds an iova for a given pfn and then
  353. * frees the iova from that domain.
  354. */
  355. void
  356. free_iova(struct iova_domain *iovad, unsigned long pfn)
  357. {
  358. unsigned long flags;
  359. struct iova *iova;
  360. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  361. iova = private_find_iova(iovad, pfn);
  362. if (!iova) {
  363. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  364. return;
  365. }
  366. remove_iova(iovad, iova);
  367. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  368. free_iova_mem(iova);
  369. }
  370. EXPORT_SYMBOL_GPL(free_iova);
  371. /**
  372. * alloc_iova_fast - allocates an iova from rcache
  373. * @iovad: - iova domain in question
  374. * @size: - size of page frames to allocate
  375. * @limit_pfn: - max limit address
  376. * @flush_rcache: - set to flush rcache on regular allocation failure
  377. * This function tries to satisfy an iova allocation from the rcache,
  378. * and falls back to regular allocation on failure. If regular allocation
  379. * fails too and the flush_rcache flag is set then the rcache will be flushed.
  380. */
  381. unsigned long
  382. alloc_iova_fast(struct iova_domain *iovad, unsigned long size,
  383. unsigned long limit_pfn, bool flush_rcache)
  384. {
  385. unsigned long iova_pfn;
  386. struct iova *new_iova;
  387. /*
  388. * Freeing non-power-of-two-sized allocations back into the IOVA caches
  389. * will come back to bite us badly, so we have to waste a bit of space
  390. * rounding up anything cacheable to make sure that can't happen. The
  391. * order of the unadjusted size will still match upon freeing.
  392. */
  393. if (size < (1 << (IOVA_RANGE_CACHE_MAX_SIZE - 1)))
  394. size = roundup_pow_of_two(size);
  395. iova_pfn = iova_rcache_get(iovad, size, limit_pfn + 1);
  396. if (iova_pfn)
  397. return iova_pfn;
  398. retry:
  399. new_iova = alloc_iova(iovad, size, limit_pfn, true);
  400. if (!new_iova) {
  401. unsigned int cpu;
  402. if (!flush_rcache)
  403. return 0;
  404. /* Try replenishing IOVAs by flushing rcache. */
  405. flush_rcache = false;
  406. for_each_online_cpu(cpu)
  407. free_cpu_cached_iovas(cpu, iovad);
  408. free_global_cached_iovas(iovad);
  409. goto retry;
  410. }
  411. return new_iova->pfn_lo;
  412. }
  413. EXPORT_SYMBOL_GPL(alloc_iova_fast);
  414. /**
  415. * free_iova_fast - free iova pfn range into rcache
  416. * @iovad: - iova domain in question.
  417. * @pfn: - pfn that is allocated previously
  418. * @size: - # of pages in range
  419. * This functions frees an iova range by trying to put it into the rcache,
  420. * falling back to regular iova deallocation via free_iova() if this fails.
  421. */
  422. void
  423. free_iova_fast(struct iova_domain *iovad, unsigned long pfn, unsigned long size)
  424. {
  425. if (iova_rcache_insert(iovad, pfn, size))
  426. return;
  427. free_iova(iovad, pfn);
  428. }
  429. EXPORT_SYMBOL_GPL(free_iova_fast);
  430. static void iova_domain_free_rcaches(struct iova_domain *iovad)
  431. {
  432. cpuhp_state_remove_instance_nocalls(CPUHP_IOMMU_IOVA_DEAD,
  433. &iovad->cpuhp_dead);
  434. free_iova_rcaches(iovad);
  435. }
  436. /**
  437. * put_iova_domain - destroys the iova domain
  438. * @iovad: - iova domain in question.
  439. * All the iova's in that domain are destroyed.
  440. */
  441. void put_iova_domain(struct iova_domain *iovad)
  442. {
  443. struct iova *iova, *tmp;
  444. if (iovad->rcaches)
  445. iova_domain_free_rcaches(iovad);
  446. rbtree_postorder_for_each_entry_safe(iova, tmp, &iovad->rbroot, node)
  447. free_iova_mem(iova);
  448. }
  449. EXPORT_SYMBOL_GPL(put_iova_domain);
  450. static int
  451. __is_range_overlap(struct rb_node *node,
  452. unsigned long pfn_lo, unsigned long pfn_hi)
  453. {
  454. struct iova *iova = to_iova(node);
  455. if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
  456. return 1;
  457. return 0;
  458. }
  459. static inline struct iova *
  460. alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi)
  461. {
  462. struct iova *iova;
  463. iova = alloc_iova_mem();
  464. if (iova) {
  465. iova->pfn_lo = pfn_lo;
  466. iova->pfn_hi = pfn_hi;
  467. }
  468. return iova;
  469. }
  470. static struct iova *
  471. __insert_new_range(struct iova_domain *iovad,
  472. unsigned long pfn_lo, unsigned long pfn_hi)
  473. {
  474. struct iova *iova;
  475. iova = alloc_and_init_iova(pfn_lo, pfn_hi);
  476. if (iova)
  477. iova_insert_rbtree(&iovad->rbroot, iova, NULL);
  478. return iova;
  479. }
  480. static void
  481. __adjust_overlap_range(struct iova *iova,
  482. unsigned long *pfn_lo, unsigned long *pfn_hi)
  483. {
  484. if (*pfn_lo < iova->pfn_lo)
  485. iova->pfn_lo = *pfn_lo;
  486. if (*pfn_hi > iova->pfn_hi)
  487. *pfn_lo = iova->pfn_hi + 1;
  488. }
  489. /**
  490. * reserve_iova - reserves an iova in the given range
  491. * @iovad: - iova domain pointer
  492. * @pfn_lo: - lower page frame address
  493. * @pfn_hi:- higher pfn adderss
  494. * This function allocates reserves the address range from pfn_lo to pfn_hi so
  495. * that this address is not dished out as part of alloc_iova.
  496. */
  497. struct iova *
  498. reserve_iova(struct iova_domain *iovad,
  499. unsigned long pfn_lo, unsigned long pfn_hi)
  500. {
  501. struct rb_node *node;
  502. unsigned long flags;
  503. struct iova *iova;
  504. unsigned int overlap = 0;
  505. /* Don't allow nonsensical pfns */
  506. if (WARN_ON((pfn_hi | pfn_lo) > (ULLONG_MAX >> iova_shift(iovad))))
  507. return NULL;
  508. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  509. for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
  510. if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
  511. iova = to_iova(node);
  512. __adjust_overlap_range(iova, &pfn_lo, &pfn_hi);
  513. if ((pfn_lo >= iova->pfn_lo) &&
  514. (pfn_hi <= iova->pfn_hi))
  515. goto finish;
  516. overlap = 1;
  517. } else if (overlap)
  518. break;
  519. }
  520. /* We are here either because this is the first reserver node
  521. * or need to insert remaining non overlap addr range
  522. */
  523. iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
  524. finish:
  525. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  526. return iova;
  527. }
  528. EXPORT_SYMBOL_GPL(reserve_iova);
  529. /*
  530. * Magazine caches for IOVA ranges. For an introduction to magazines,
  531. * see the USENIX 2001 paper "Magazines and Vmem: Extending the Slab
  532. * Allocator to Many CPUs and Arbitrary Resources" by Bonwick and Adams.
  533. * For simplicity, we use a static magazine size and don't implement the
  534. * dynamic size tuning described in the paper.
  535. */
  536. /*
  537. * As kmalloc's buffer size is fixed to power of 2, 127 is chosen to
  538. * assure size of 'iova_magazine' to be 1024 bytes, so that no memory
  539. * will be wasted.
  540. */
  541. #define IOVA_MAG_SIZE 127
  542. #define MAX_GLOBAL_MAGS 32 /* magazines per bin */
  543. struct iova_magazine {
  544. unsigned long size;
  545. unsigned long pfns[IOVA_MAG_SIZE];
  546. };
  547. struct iova_cpu_rcache {
  548. spinlock_t lock;
  549. struct iova_magazine *loaded;
  550. struct iova_magazine *prev;
  551. };
  552. struct iova_rcache {
  553. spinlock_t lock;
  554. unsigned long depot_size;
  555. struct iova_magazine *depot[MAX_GLOBAL_MAGS];
  556. struct iova_cpu_rcache __percpu *cpu_rcaches;
  557. };
  558. static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
  559. {
  560. return kzalloc(sizeof(struct iova_magazine), flags);
  561. }
  562. static void iova_magazine_free(struct iova_magazine *mag)
  563. {
  564. kfree(mag);
  565. }
  566. static void
  567. iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad)
  568. {
  569. unsigned long flags;
  570. int i;
  571. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  572. for (i = 0 ; i < mag->size; ++i) {
  573. struct iova *iova = private_find_iova(iovad, mag->pfns[i]);
  574. if (WARN_ON(!iova))
  575. continue;
  576. remove_iova(iovad, iova);
  577. free_iova_mem(iova);
  578. }
  579. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  580. mag->size = 0;
  581. }
  582. static bool iova_magazine_full(struct iova_magazine *mag)
  583. {
  584. return mag->size == IOVA_MAG_SIZE;
  585. }
  586. static bool iova_magazine_empty(struct iova_magazine *mag)
  587. {
  588. return mag->size == 0;
  589. }
  590. static unsigned long iova_magazine_pop(struct iova_magazine *mag,
  591. unsigned long limit_pfn)
  592. {
  593. int i;
  594. unsigned long pfn;
  595. /* Only fall back to the rbtree if we have no suitable pfns at all */
  596. for (i = mag->size - 1; mag->pfns[i] > limit_pfn; i--)
  597. if (i == 0)
  598. return 0;
  599. /* Swap it to pop it */
  600. pfn = mag->pfns[i];
  601. mag->pfns[i] = mag->pfns[--mag->size];
  602. return pfn;
  603. }
  604. static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn)
  605. {
  606. mag->pfns[mag->size++] = pfn;
  607. }
  608. int iova_domain_init_rcaches(struct iova_domain *iovad)
  609. {
  610. unsigned int cpu;
  611. int i, ret;
  612. iovad->rcaches = kcalloc(IOVA_RANGE_CACHE_MAX_SIZE,
  613. sizeof(struct iova_rcache),
  614. GFP_KERNEL);
  615. if (!iovad->rcaches)
  616. return -ENOMEM;
  617. for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
  618. struct iova_cpu_rcache *cpu_rcache;
  619. struct iova_rcache *rcache;
  620. rcache = &iovad->rcaches[i];
  621. spin_lock_init(&rcache->lock);
  622. rcache->depot_size = 0;
  623. rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache),
  624. cache_line_size());
  625. if (!rcache->cpu_rcaches) {
  626. ret = -ENOMEM;
  627. goto out_err;
  628. }
  629. for_each_possible_cpu(cpu) {
  630. cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
  631. spin_lock_init(&cpu_rcache->lock);
  632. cpu_rcache->loaded = iova_magazine_alloc(GFP_KERNEL);
  633. cpu_rcache->prev = iova_magazine_alloc(GFP_KERNEL);
  634. if (!cpu_rcache->loaded || !cpu_rcache->prev) {
  635. ret = -ENOMEM;
  636. goto out_err;
  637. }
  638. }
  639. }
  640. ret = cpuhp_state_add_instance_nocalls(CPUHP_IOMMU_IOVA_DEAD,
  641. &iovad->cpuhp_dead);
  642. if (ret)
  643. goto out_err;
  644. return 0;
  645. out_err:
  646. free_iova_rcaches(iovad);
  647. return ret;
  648. }
  649. EXPORT_SYMBOL_GPL(iova_domain_init_rcaches);
  650. /*
  651. * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and
  652. * return true on success. Can fail if rcache is full and we can't free
  653. * space, and free_iova() (our only caller) will then return the IOVA
  654. * range to the rbtree instead.
  655. */
  656. static bool __iova_rcache_insert(struct iova_domain *iovad,
  657. struct iova_rcache *rcache,
  658. unsigned long iova_pfn)
  659. {
  660. struct iova_magazine *mag_to_free = NULL;
  661. struct iova_cpu_rcache *cpu_rcache;
  662. bool can_insert = false;
  663. unsigned long flags;
  664. cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
  665. spin_lock_irqsave(&cpu_rcache->lock, flags);
  666. if (!iova_magazine_full(cpu_rcache->loaded)) {
  667. can_insert = true;
  668. } else if (!iova_magazine_full(cpu_rcache->prev)) {
  669. swap(cpu_rcache->prev, cpu_rcache->loaded);
  670. can_insert = true;
  671. } else {
  672. struct iova_magazine *new_mag = iova_magazine_alloc(GFP_ATOMIC);
  673. if (new_mag) {
  674. spin_lock(&rcache->lock);
  675. if (rcache->depot_size < MAX_GLOBAL_MAGS) {
  676. rcache->depot[rcache->depot_size++] =
  677. cpu_rcache->loaded;
  678. } else {
  679. mag_to_free = cpu_rcache->loaded;
  680. }
  681. spin_unlock(&rcache->lock);
  682. cpu_rcache->loaded = new_mag;
  683. can_insert = true;
  684. }
  685. }
  686. if (can_insert)
  687. iova_magazine_push(cpu_rcache->loaded, iova_pfn);
  688. spin_unlock_irqrestore(&cpu_rcache->lock, flags);
  689. if (mag_to_free) {
  690. iova_magazine_free_pfns(mag_to_free, iovad);
  691. iova_magazine_free(mag_to_free);
  692. }
  693. return can_insert;
  694. }
  695. static bool iova_rcache_insert(struct iova_domain *iovad, unsigned long pfn,
  696. unsigned long size)
  697. {
  698. unsigned int log_size = order_base_2(size);
  699. if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
  700. return false;
  701. return __iova_rcache_insert(iovad, &iovad->rcaches[log_size], pfn);
  702. }
  703. /*
  704. * Caller wants to allocate a new IOVA range from 'rcache'. If we can
  705. * satisfy the request, return a matching non-NULL range and remove
  706. * it from the 'rcache'.
  707. */
  708. static unsigned long __iova_rcache_get(struct iova_rcache *rcache,
  709. unsigned long limit_pfn)
  710. {
  711. struct iova_cpu_rcache *cpu_rcache;
  712. unsigned long iova_pfn = 0;
  713. bool has_pfn = false;
  714. unsigned long flags;
  715. cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
  716. spin_lock_irqsave(&cpu_rcache->lock, flags);
  717. if (!iova_magazine_empty(cpu_rcache->loaded)) {
  718. has_pfn = true;
  719. } else if (!iova_magazine_empty(cpu_rcache->prev)) {
  720. swap(cpu_rcache->prev, cpu_rcache->loaded);
  721. has_pfn = true;
  722. } else {
  723. spin_lock(&rcache->lock);
  724. if (rcache->depot_size > 0) {
  725. iova_magazine_free(cpu_rcache->loaded);
  726. cpu_rcache->loaded = rcache->depot[--rcache->depot_size];
  727. has_pfn = true;
  728. }
  729. spin_unlock(&rcache->lock);
  730. }
  731. if (has_pfn)
  732. iova_pfn = iova_magazine_pop(cpu_rcache->loaded, limit_pfn);
  733. spin_unlock_irqrestore(&cpu_rcache->lock, flags);
  734. return iova_pfn;
  735. }
  736. /*
  737. * Try to satisfy IOVA allocation range from rcache. Fail if requested
  738. * size is too big or the DMA limit we are given isn't satisfied by the
  739. * top element in the magazine.
  740. */
  741. static unsigned long iova_rcache_get(struct iova_domain *iovad,
  742. unsigned long size,
  743. unsigned long limit_pfn)
  744. {
  745. unsigned int log_size = order_base_2(size);
  746. if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
  747. return 0;
  748. return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn - size);
  749. }
  750. /*
  751. * free rcache data structures.
  752. */
  753. static void free_iova_rcaches(struct iova_domain *iovad)
  754. {
  755. struct iova_rcache *rcache;
  756. struct iova_cpu_rcache *cpu_rcache;
  757. unsigned int cpu;
  758. int i, j;
  759. for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
  760. rcache = &iovad->rcaches[i];
  761. if (!rcache->cpu_rcaches)
  762. break;
  763. for_each_possible_cpu(cpu) {
  764. cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
  765. iova_magazine_free(cpu_rcache->loaded);
  766. iova_magazine_free(cpu_rcache->prev);
  767. }
  768. free_percpu(rcache->cpu_rcaches);
  769. for (j = 0; j < rcache->depot_size; ++j)
  770. iova_magazine_free(rcache->depot[j]);
  771. }
  772. kfree(iovad->rcaches);
  773. iovad->rcaches = NULL;
  774. }
  775. /*
  776. * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
  777. */
  778. static void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad)
  779. {
  780. struct iova_cpu_rcache *cpu_rcache;
  781. struct iova_rcache *rcache;
  782. unsigned long flags;
  783. int i;
  784. for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
  785. rcache = &iovad->rcaches[i];
  786. cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
  787. spin_lock_irqsave(&cpu_rcache->lock, flags);
  788. iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
  789. iova_magazine_free_pfns(cpu_rcache->prev, iovad);
  790. spin_unlock_irqrestore(&cpu_rcache->lock, flags);
  791. }
  792. }
  793. /*
  794. * free all the IOVA ranges of global cache
  795. */
  796. static void free_global_cached_iovas(struct iova_domain *iovad)
  797. {
  798. struct iova_rcache *rcache;
  799. unsigned long flags;
  800. int i, j;
  801. for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
  802. rcache = &iovad->rcaches[i];
  803. spin_lock_irqsave(&rcache->lock, flags);
  804. for (j = 0; j < rcache->depot_size; ++j) {
  805. iova_magazine_free_pfns(rcache->depot[j], iovad);
  806. iova_magazine_free(rcache->depot[j]);
  807. }
  808. rcache->depot_size = 0;
  809. spin_unlock_irqrestore(&rcache->lock, flags);
  810. }
  811. }
  812. MODULE_AUTHOR("Anil S Keshavamurthy <[email protected]>");
  813. MODULE_LICENSE("GPL");