iova.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. void
  23. init_iova_domain(struct iova_domain *iovad, unsigned long granule,
  24. unsigned long start_pfn, unsigned long pfn_32bit)
  25. {
  26. /*
  27. * IOVA granularity will normally be equal to the smallest
  28. * supported IOMMU page size; both *must* be capable of
  29. * representing individual CPU pages exactly.
  30. */
  31. BUG_ON((granule > PAGE_SIZE) || !is_power_of_2(granule));
  32. spin_lock_init(&iovad->iova_rbtree_lock);
  33. iovad->rbroot = RB_ROOT;
  34. iovad->cached32_node = NULL;
  35. iovad->granule = granule;
  36. iovad->start_pfn = start_pfn;
  37. iovad->dma_32bit_pfn = pfn_32bit;
  38. }
  39. EXPORT_SYMBOL_GPL(init_iova_domain);
  40. static struct rb_node *
  41. __get_cached_rbnode(struct iova_domain *iovad, unsigned long *limit_pfn)
  42. {
  43. if ((*limit_pfn != iovad->dma_32bit_pfn) ||
  44. (iovad->cached32_node == NULL))
  45. return rb_last(&iovad->rbroot);
  46. else {
  47. struct rb_node *prev_node = rb_prev(iovad->cached32_node);
  48. struct iova *curr_iova =
  49. container_of(iovad->cached32_node, struct iova, node);
  50. *limit_pfn = curr_iova->pfn_lo - 1;
  51. return prev_node;
  52. }
  53. }
  54. static void
  55. __cached_rbnode_insert_update(struct iova_domain *iovad,
  56. unsigned long limit_pfn, struct iova *new)
  57. {
  58. if (limit_pfn != iovad->dma_32bit_pfn)
  59. return;
  60. iovad->cached32_node = &new->node;
  61. }
  62. static void
  63. __cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
  64. {
  65. struct iova *cached_iova;
  66. struct rb_node *curr;
  67. if (!iovad->cached32_node)
  68. return;
  69. curr = iovad->cached32_node;
  70. cached_iova = container_of(curr, struct iova, node);
  71. if (free->pfn_lo >= cached_iova->pfn_lo) {
  72. struct rb_node *node = rb_next(&free->node);
  73. struct iova *iova = container_of(node, struct iova, node);
  74. /* only cache if it's below 32bit pfn */
  75. if (node && iova->pfn_lo < iovad->dma_32bit_pfn)
  76. iovad->cached32_node = node;
  77. else
  78. iovad->cached32_node = NULL;
  79. }
  80. }
  81. /*
  82. * Computes the padding size required, to make the start address
  83. * naturally aligned on the power-of-two order of its size
  84. */
  85. static unsigned int
  86. iova_get_pad_size(unsigned int size, unsigned int limit_pfn)
  87. {
  88. return (limit_pfn + 1 - size) & (__roundup_pow_of_two(size) - 1);
  89. }
  90. static int __alloc_and_insert_iova_range(struct iova_domain *iovad,
  91. unsigned long size, unsigned long limit_pfn,
  92. struct iova *new, bool size_aligned)
  93. {
  94. struct rb_node *prev, *curr = NULL;
  95. unsigned long flags;
  96. unsigned long saved_pfn;
  97. unsigned int pad_size = 0;
  98. /* Walk the tree backwards */
  99. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  100. saved_pfn = limit_pfn;
  101. curr = __get_cached_rbnode(iovad, &limit_pfn);
  102. prev = curr;
  103. while (curr) {
  104. struct iova *curr_iova = container_of(curr, struct iova, node);
  105. if (limit_pfn < curr_iova->pfn_lo)
  106. goto move_left;
  107. else if (limit_pfn < curr_iova->pfn_hi)
  108. goto adjust_limit_pfn;
  109. else {
  110. if (size_aligned)
  111. pad_size = iova_get_pad_size(size, limit_pfn);
  112. if ((curr_iova->pfn_hi + size + pad_size) <= limit_pfn)
  113. break; /* found a free slot */
  114. }
  115. adjust_limit_pfn:
  116. limit_pfn = curr_iova->pfn_lo ? (curr_iova->pfn_lo - 1) : 0;
  117. move_left:
  118. prev = curr;
  119. curr = rb_prev(curr);
  120. }
  121. if (!curr) {
  122. if (size_aligned)
  123. pad_size = iova_get_pad_size(size, limit_pfn);
  124. if ((iovad->start_pfn + size + pad_size) > limit_pfn) {
  125. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  126. return -ENOMEM;
  127. }
  128. }
  129. /* pfn_lo will point to size aligned address if size_aligned is set */
  130. new->pfn_lo = limit_pfn - (size + pad_size) + 1;
  131. new->pfn_hi = new->pfn_lo + size - 1;
  132. /* Insert the new_iova into domain rbtree by holding writer lock */
  133. /* Add new node and rebalance tree. */
  134. {
  135. struct rb_node **entry, *parent = NULL;
  136. /* If we have 'prev', it's a valid place to start the
  137. insertion. Otherwise, start from the root. */
  138. if (prev)
  139. entry = &prev;
  140. else
  141. entry = &iovad->rbroot.rb_node;
  142. /* Figure out where to put new node */
  143. while (*entry) {
  144. struct iova *this = container_of(*entry,
  145. struct iova, node);
  146. parent = *entry;
  147. if (new->pfn_lo < this->pfn_lo)
  148. entry = &((*entry)->rb_left);
  149. else if (new->pfn_lo > this->pfn_lo)
  150. entry = &((*entry)->rb_right);
  151. else
  152. BUG(); /* this should not happen */
  153. }
  154. /* Add new node and rebalance tree. */
  155. rb_link_node(&new->node, parent, entry);
  156. rb_insert_color(&new->node, &iovad->rbroot);
  157. }
  158. __cached_rbnode_insert_update(iovad, saved_pfn, new);
  159. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  160. return 0;
  161. }
  162. static void
  163. iova_insert_rbtree(struct rb_root *root, struct iova *iova)
  164. {
  165. struct rb_node **new = &(root->rb_node), *parent = NULL;
  166. /* Figure out where to put new node */
  167. while (*new) {
  168. struct iova *this = container_of(*new, struct iova, node);
  169. parent = *new;
  170. if (iova->pfn_lo < this->pfn_lo)
  171. new = &((*new)->rb_left);
  172. else if (iova->pfn_lo > this->pfn_lo)
  173. new = &((*new)->rb_right);
  174. else
  175. BUG(); /* this should not happen */
  176. }
  177. /* Add new node and rebalance tree. */
  178. rb_link_node(&iova->node, parent, new);
  179. rb_insert_color(&iova->node, root);
  180. }
  181. static struct kmem_cache *iova_cache;
  182. static unsigned int iova_cache_users;
  183. static DEFINE_MUTEX(iova_cache_mutex);
  184. struct iova *alloc_iova_mem(void)
  185. {
  186. return kmem_cache_alloc(iova_cache, GFP_ATOMIC);
  187. }
  188. EXPORT_SYMBOL(alloc_iova_mem);
  189. void free_iova_mem(struct iova *iova)
  190. {
  191. kmem_cache_free(iova_cache, iova);
  192. }
  193. EXPORT_SYMBOL(free_iova_mem);
  194. int iova_cache_get(void)
  195. {
  196. mutex_lock(&iova_cache_mutex);
  197. if (!iova_cache_users) {
  198. iova_cache = kmem_cache_create(
  199. "iommu_iova", sizeof(struct iova), 0,
  200. SLAB_HWCACHE_ALIGN, NULL);
  201. if (!iova_cache) {
  202. mutex_unlock(&iova_cache_mutex);
  203. printk(KERN_ERR "Couldn't create iova cache\n");
  204. return -ENOMEM;
  205. }
  206. }
  207. iova_cache_users++;
  208. mutex_unlock(&iova_cache_mutex);
  209. return 0;
  210. }
  211. EXPORT_SYMBOL_GPL(iova_cache_get);
  212. void iova_cache_put(void)
  213. {
  214. mutex_lock(&iova_cache_mutex);
  215. if (WARN_ON(!iova_cache_users)) {
  216. mutex_unlock(&iova_cache_mutex);
  217. return;
  218. }
  219. iova_cache_users--;
  220. if (!iova_cache_users)
  221. kmem_cache_destroy(iova_cache);
  222. mutex_unlock(&iova_cache_mutex);
  223. }
  224. EXPORT_SYMBOL_GPL(iova_cache_put);
  225. /**
  226. * alloc_iova - allocates an iova
  227. * @iovad: - iova domain in question
  228. * @size: - size of page frames to allocate
  229. * @limit_pfn: - max limit address
  230. * @size_aligned: - set if size_aligned address range is required
  231. * This function allocates an iova in the range iovad->start_pfn to limit_pfn,
  232. * searching top-down from limit_pfn to iovad->start_pfn. If the size_aligned
  233. * flag is set then the allocated address iova->pfn_lo will be naturally
  234. * aligned on roundup_power_of_two(size).
  235. */
  236. struct iova *
  237. alloc_iova(struct iova_domain *iovad, unsigned long size,
  238. unsigned long limit_pfn,
  239. bool size_aligned)
  240. {
  241. struct iova *new_iova;
  242. int ret;
  243. new_iova = alloc_iova_mem();
  244. if (!new_iova)
  245. return NULL;
  246. ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn,
  247. new_iova, size_aligned);
  248. if (ret) {
  249. free_iova_mem(new_iova);
  250. return NULL;
  251. }
  252. return new_iova;
  253. }
  254. EXPORT_SYMBOL_GPL(alloc_iova);
  255. /**
  256. * find_iova - find's an iova for a given pfn
  257. * @iovad: - iova domain in question.
  258. * @pfn: - page frame number
  259. * This function finds and returns an iova belonging to the
  260. * given doamin which matches the given pfn.
  261. */
  262. struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn)
  263. {
  264. unsigned long flags;
  265. struct rb_node *node;
  266. /* Take the lock so that no other thread is manipulating the rbtree */
  267. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  268. node = iovad->rbroot.rb_node;
  269. while (node) {
  270. struct iova *iova = container_of(node, struct iova, node);
  271. /* If pfn falls within iova's range, return iova */
  272. if ((pfn >= iova->pfn_lo) && (pfn <= iova->pfn_hi)) {
  273. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  274. /* We are not holding the lock while this iova
  275. * is referenced by the caller as the same thread
  276. * which called this function also calls __free_iova()
  277. * and it is by design that only one thread can possibly
  278. * reference a particular iova and hence no conflict.
  279. */
  280. return iova;
  281. }
  282. if (pfn < iova->pfn_lo)
  283. node = node->rb_left;
  284. else if (pfn > iova->pfn_lo)
  285. node = node->rb_right;
  286. }
  287. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  288. return NULL;
  289. }
  290. EXPORT_SYMBOL_GPL(find_iova);
  291. /**
  292. * __free_iova - frees the given iova
  293. * @iovad: iova domain in question.
  294. * @iova: iova in question.
  295. * Frees the given iova belonging to the giving domain
  296. */
  297. void
  298. __free_iova(struct iova_domain *iovad, struct iova *iova)
  299. {
  300. unsigned long flags;
  301. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  302. __cached_rbnode_delete_update(iovad, iova);
  303. rb_erase(&iova->node, &iovad->rbroot);
  304. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  305. free_iova_mem(iova);
  306. }
  307. EXPORT_SYMBOL_GPL(__free_iova);
  308. /**
  309. * free_iova - finds and frees the iova for a given pfn
  310. * @iovad: - iova domain in question.
  311. * @pfn: - pfn that is allocated previously
  312. * This functions finds an iova for a given pfn and then
  313. * frees the iova from that domain.
  314. */
  315. void
  316. free_iova(struct iova_domain *iovad, unsigned long pfn)
  317. {
  318. struct iova *iova = find_iova(iovad, pfn);
  319. if (iova)
  320. __free_iova(iovad, iova);
  321. }
  322. EXPORT_SYMBOL_GPL(free_iova);
  323. /**
  324. * put_iova_domain - destroys the iova doamin
  325. * @iovad: - iova domain in question.
  326. * All the iova's in that domain are destroyed.
  327. */
  328. void put_iova_domain(struct iova_domain *iovad)
  329. {
  330. struct rb_node *node;
  331. unsigned long flags;
  332. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  333. node = rb_first(&iovad->rbroot);
  334. while (node) {
  335. struct iova *iova = container_of(node, struct iova, node);
  336. rb_erase(node, &iovad->rbroot);
  337. free_iova_mem(iova);
  338. node = rb_first(&iovad->rbroot);
  339. }
  340. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  341. }
  342. EXPORT_SYMBOL_GPL(put_iova_domain);
  343. static int
  344. __is_range_overlap(struct rb_node *node,
  345. unsigned long pfn_lo, unsigned long pfn_hi)
  346. {
  347. struct iova *iova = container_of(node, struct iova, node);
  348. if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
  349. return 1;
  350. return 0;
  351. }
  352. static inline struct iova *
  353. alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi)
  354. {
  355. struct iova *iova;
  356. iova = alloc_iova_mem();
  357. if (iova) {
  358. iova->pfn_lo = pfn_lo;
  359. iova->pfn_hi = pfn_hi;
  360. }
  361. return iova;
  362. }
  363. static struct iova *
  364. __insert_new_range(struct iova_domain *iovad,
  365. unsigned long pfn_lo, unsigned long pfn_hi)
  366. {
  367. struct iova *iova;
  368. iova = alloc_and_init_iova(pfn_lo, pfn_hi);
  369. if (iova)
  370. iova_insert_rbtree(&iovad->rbroot, iova);
  371. return iova;
  372. }
  373. static void
  374. __adjust_overlap_range(struct iova *iova,
  375. unsigned long *pfn_lo, unsigned long *pfn_hi)
  376. {
  377. if (*pfn_lo < iova->pfn_lo)
  378. iova->pfn_lo = *pfn_lo;
  379. if (*pfn_hi > iova->pfn_hi)
  380. *pfn_lo = iova->pfn_hi + 1;
  381. }
  382. /**
  383. * reserve_iova - reserves an iova in the given range
  384. * @iovad: - iova domain pointer
  385. * @pfn_lo: - lower page frame address
  386. * @pfn_hi:- higher pfn adderss
  387. * This function allocates reserves the address range from pfn_lo to pfn_hi so
  388. * that this address is not dished out as part of alloc_iova.
  389. */
  390. struct iova *
  391. reserve_iova(struct iova_domain *iovad,
  392. unsigned long pfn_lo, unsigned long pfn_hi)
  393. {
  394. struct rb_node *node;
  395. unsigned long flags;
  396. struct iova *iova;
  397. unsigned int overlap = 0;
  398. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  399. for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
  400. if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
  401. iova = container_of(node, struct iova, node);
  402. __adjust_overlap_range(iova, &pfn_lo, &pfn_hi);
  403. if ((pfn_lo >= iova->pfn_lo) &&
  404. (pfn_hi <= iova->pfn_hi))
  405. goto finish;
  406. overlap = 1;
  407. } else if (overlap)
  408. break;
  409. }
  410. /* We are here either because this is the first reserver node
  411. * or need to insert remaining non overlap addr range
  412. */
  413. iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
  414. finish:
  415. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  416. return iova;
  417. }
  418. EXPORT_SYMBOL_GPL(reserve_iova);
  419. /**
  420. * copy_reserved_iova - copies the reserved between domains
  421. * @from: - source doamin from where to copy
  422. * @to: - destination domin where to copy
  423. * This function copies reserved iova's from one doamin to
  424. * other.
  425. */
  426. void
  427. copy_reserved_iova(struct iova_domain *from, struct iova_domain *to)
  428. {
  429. unsigned long flags;
  430. struct rb_node *node;
  431. spin_lock_irqsave(&from->iova_rbtree_lock, flags);
  432. for (node = rb_first(&from->rbroot); node; node = rb_next(node)) {
  433. struct iova *iova = container_of(node, struct iova, node);
  434. struct iova *new_iova;
  435. new_iova = reserve_iova(to, iova->pfn_lo, iova->pfn_hi);
  436. if (!new_iova)
  437. printk(KERN_ERR "Reserve iova range %lx@%lx failed\n",
  438. iova->pfn_lo, iova->pfn_lo);
  439. }
  440. spin_unlock_irqrestore(&from->iova_rbtree_lock, flags);
  441. }
  442. EXPORT_SYMBOL_GPL(copy_reserved_iova);
  443. struct iova *
  444. split_and_remove_iova(struct iova_domain *iovad, struct iova *iova,
  445. unsigned long pfn_lo, unsigned long pfn_hi)
  446. {
  447. unsigned long flags;
  448. struct iova *prev = NULL, *next = NULL;
  449. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  450. if (iova->pfn_lo < pfn_lo) {
  451. prev = alloc_and_init_iova(iova->pfn_lo, pfn_lo - 1);
  452. if (prev == NULL)
  453. goto error;
  454. }
  455. if (iova->pfn_hi > pfn_hi) {
  456. next = alloc_and_init_iova(pfn_hi + 1, iova->pfn_hi);
  457. if (next == NULL)
  458. goto error;
  459. }
  460. __cached_rbnode_delete_update(iovad, iova);
  461. rb_erase(&iova->node, &iovad->rbroot);
  462. if (prev) {
  463. iova_insert_rbtree(&iovad->rbroot, prev);
  464. iova->pfn_lo = pfn_lo;
  465. }
  466. if (next) {
  467. iova_insert_rbtree(&iovad->rbroot, next);
  468. iova->pfn_hi = pfn_hi;
  469. }
  470. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  471. return iova;
  472. error:
  473. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  474. if (prev)
  475. free_iova_mem(prev);
  476. return NULL;
  477. }
  478. MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
  479. MODULE_LICENSE("GPL");