iova.c 23 KB

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