iova.c 24 KB

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