iova.c 23 KB

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