iova.c 23 KB

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