iova.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. void
  21. init_iova_domain(struct iova_domain *iovad, unsigned long pfn_32bit)
  22. {
  23. spin_lock_init(&iovad->iova_rbtree_lock);
  24. iovad->rbroot = RB_ROOT;
  25. iovad->cached32_node = NULL;
  26. iovad->dma_32bit_pfn = pfn_32bit;
  27. }
  28. static struct rb_node *
  29. __get_cached_rbnode(struct iova_domain *iovad, unsigned long *limit_pfn)
  30. {
  31. if ((*limit_pfn != iovad->dma_32bit_pfn) ||
  32. (iovad->cached32_node == NULL))
  33. return rb_last(&iovad->rbroot);
  34. else {
  35. struct rb_node *prev_node = rb_prev(iovad->cached32_node);
  36. struct iova *curr_iova =
  37. container_of(iovad->cached32_node, struct iova, node);
  38. *limit_pfn = curr_iova->pfn_lo - 1;
  39. return prev_node;
  40. }
  41. }
  42. static void
  43. __cached_rbnode_insert_update(struct iova_domain *iovad,
  44. unsigned long limit_pfn, struct iova *new)
  45. {
  46. if (limit_pfn != iovad->dma_32bit_pfn)
  47. return;
  48. iovad->cached32_node = &new->node;
  49. }
  50. static void
  51. __cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
  52. {
  53. struct iova *cached_iova;
  54. struct rb_node *curr;
  55. if (!iovad->cached32_node)
  56. return;
  57. curr = iovad->cached32_node;
  58. cached_iova = container_of(curr, struct iova, node);
  59. if (free->pfn_lo >= cached_iova->pfn_lo) {
  60. struct rb_node *node = rb_next(&free->node);
  61. struct iova *iova = container_of(node, struct iova, node);
  62. /* only cache if it's below 32bit pfn */
  63. if (node && iova->pfn_lo < iovad->dma_32bit_pfn)
  64. iovad->cached32_node = node;
  65. else
  66. iovad->cached32_node = NULL;
  67. }
  68. }
  69. /* Computes the padding size required, to make the
  70. * the start address naturally aligned on its size
  71. */
  72. static int
  73. iova_get_pad_size(int size, unsigned int limit_pfn)
  74. {
  75. unsigned int pad_size = 0;
  76. unsigned int order = ilog2(size);
  77. if (order)
  78. pad_size = (limit_pfn + 1) % (1 << order);
  79. return pad_size;
  80. }
  81. static int __alloc_and_insert_iova_range(struct iova_domain *iovad,
  82. unsigned long size, unsigned long limit_pfn,
  83. struct iova *new, bool size_aligned)
  84. {
  85. struct rb_node *prev, *curr = NULL;
  86. unsigned long flags;
  87. unsigned long saved_pfn;
  88. unsigned int pad_size = 0;
  89. /* Walk the tree backwards */
  90. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  91. saved_pfn = limit_pfn;
  92. curr = __get_cached_rbnode(iovad, &limit_pfn);
  93. prev = curr;
  94. while (curr) {
  95. struct iova *curr_iova = container_of(curr, struct iova, node);
  96. if (limit_pfn < curr_iova->pfn_lo)
  97. goto move_left;
  98. else if (limit_pfn < curr_iova->pfn_hi)
  99. goto adjust_limit_pfn;
  100. else {
  101. if (size_aligned)
  102. pad_size = iova_get_pad_size(size, limit_pfn);
  103. if ((curr_iova->pfn_hi + size + pad_size) <= limit_pfn)
  104. break; /* found a free slot */
  105. }
  106. adjust_limit_pfn:
  107. limit_pfn = curr_iova->pfn_lo - 1;
  108. move_left:
  109. prev = curr;
  110. curr = rb_prev(curr);
  111. }
  112. if (!curr) {
  113. if (size_aligned)
  114. pad_size = iova_get_pad_size(size, limit_pfn);
  115. if ((IOVA_START_PFN + size + pad_size) > limit_pfn) {
  116. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  117. return -ENOMEM;
  118. }
  119. }
  120. /* pfn_lo will point to size aligned address if size_aligned is set */
  121. new->pfn_lo = limit_pfn - (size + pad_size) + 1;
  122. new->pfn_hi = new->pfn_lo + size - 1;
  123. /* Insert the new_iova into domain rbtree by holding writer lock */
  124. /* Add new node and rebalance tree. */
  125. {
  126. struct rb_node **entry, *parent = NULL;
  127. /* If we have 'prev', it's a valid place to start the
  128. insertion. Otherwise, start from the root. */
  129. if (prev)
  130. entry = &prev;
  131. else
  132. entry = &iovad->rbroot.rb_node;
  133. /* Figure out where to put new node */
  134. while (*entry) {
  135. struct iova *this = container_of(*entry,
  136. struct iova, node);
  137. parent = *entry;
  138. if (new->pfn_lo < this->pfn_lo)
  139. entry = &((*entry)->rb_left);
  140. else if (new->pfn_lo > this->pfn_lo)
  141. entry = &((*entry)->rb_right);
  142. else
  143. BUG(); /* this should not happen */
  144. }
  145. /* Add new node and rebalance tree. */
  146. rb_link_node(&new->node, parent, entry);
  147. rb_insert_color(&new->node, &iovad->rbroot);
  148. }
  149. __cached_rbnode_insert_update(iovad, saved_pfn, new);
  150. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  151. return 0;
  152. }
  153. static void
  154. iova_insert_rbtree(struct rb_root *root, struct iova *iova)
  155. {
  156. struct rb_node **new = &(root->rb_node), *parent = NULL;
  157. /* Figure out where to put new node */
  158. while (*new) {
  159. struct iova *this = container_of(*new, struct iova, node);
  160. parent = *new;
  161. if (iova->pfn_lo < this->pfn_lo)
  162. new = &((*new)->rb_left);
  163. else if (iova->pfn_lo > this->pfn_lo)
  164. new = &((*new)->rb_right);
  165. else
  166. BUG(); /* this should not happen */
  167. }
  168. /* Add new node and rebalance tree. */
  169. rb_link_node(&iova->node, parent, new);
  170. rb_insert_color(&iova->node, root);
  171. }
  172. /**
  173. * alloc_iova - allocates an iova
  174. * @iovad - iova domain in question
  175. * @size - size of page frames to allocate
  176. * @limit_pfn - max limit address
  177. * @size_aligned - set if size_aligned address range is required
  178. * This function allocates an iova in the range limit_pfn to IOVA_START_PFN
  179. * looking from limit_pfn instead from IOVA_START_PFN. If the size_aligned
  180. * flag is set then the allocated address iova->pfn_lo will be naturally
  181. * aligned on roundup_power_of_two(size).
  182. */
  183. struct iova *
  184. alloc_iova(struct iova_domain *iovad, unsigned long size,
  185. unsigned long limit_pfn,
  186. bool size_aligned)
  187. {
  188. struct iova *new_iova;
  189. int ret;
  190. new_iova = alloc_iova_mem();
  191. if (!new_iova)
  192. return NULL;
  193. /* If size aligned is set then round the size to
  194. * to next power of two.
  195. */
  196. if (size_aligned)
  197. size = __roundup_pow_of_two(size);
  198. ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn,
  199. new_iova, size_aligned);
  200. if (ret) {
  201. free_iova_mem(new_iova);
  202. return NULL;
  203. }
  204. return new_iova;
  205. }
  206. /**
  207. * find_iova - find's an iova for a given pfn
  208. * @iovad - iova domain in question.
  209. * pfn - page frame number
  210. * This function finds and returns an iova belonging to the
  211. * given doamin which matches the given pfn.
  212. */
  213. struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn)
  214. {
  215. unsigned long flags;
  216. struct rb_node *node;
  217. /* Take the lock so that no other thread is manipulating the rbtree */
  218. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  219. node = iovad->rbroot.rb_node;
  220. while (node) {
  221. struct iova *iova = container_of(node, struct iova, node);
  222. /* If pfn falls within iova's range, return iova */
  223. if ((pfn >= iova->pfn_lo) && (pfn <= iova->pfn_hi)) {
  224. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  225. /* We are not holding the lock while this iova
  226. * is referenced by the caller as the same thread
  227. * which called this function also calls __free_iova()
  228. * and it is by desing that only one thread can possibly
  229. * reference a particular iova and hence no conflict.
  230. */
  231. return iova;
  232. }
  233. if (pfn < iova->pfn_lo)
  234. node = node->rb_left;
  235. else if (pfn > iova->pfn_lo)
  236. node = node->rb_right;
  237. }
  238. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  239. return NULL;
  240. }
  241. /**
  242. * __free_iova - frees the given iova
  243. * @iovad: iova domain in question.
  244. * @iova: iova in question.
  245. * Frees the given iova belonging to the giving domain
  246. */
  247. void
  248. __free_iova(struct iova_domain *iovad, struct iova *iova)
  249. {
  250. unsigned long flags;
  251. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  252. __cached_rbnode_delete_update(iovad, iova);
  253. rb_erase(&iova->node, &iovad->rbroot);
  254. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  255. free_iova_mem(iova);
  256. }
  257. /**
  258. * free_iova - finds and frees the iova for a given pfn
  259. * @iovad: - iova domain in question.
  260. * @pfn: - pfn that is allocated previously
  261. * This functions finds an iova for a given pfn and then
  262. * frees the iova from that domain.
  263. */
  264. void
  265. free_iova(struct iova_domain *iovad, unsigned long pfn)
  266. {
  267. struct iova *iova = find_iova(iovad, pfn);
  268. if (iova)
  269. __free_iova(iovad, iova);
  270. }
  271. /**
  272. * put_iova_domain - destroys the iova doamin
  273. * @iovad: - iova domain in question.
  274. * All the iova's in that domain are destroyed.
  275. */
  276. void put_iova_domain(struct iova_domain *iovad)
  277. {
  278. struct rb_node *node;
  279. unsigned long flags;
  280. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  281. node = rb_first(&iovad->rbroot);
  282. while (node) {
  283. struct iova *iova = container_of(node, struct iova, node);
  284. rb_erase(node, &iovad->rbroot);
  285. free_iova_mem(iova);
  286. node = rb_first(&iovad->rbroot);
  287. }
  288. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  289. }
  290. static int
  291. __is_range_overlap(struct rb_node *node,
  292. unsigned long pfn_lo, unsigned long pfn_hi)
  293. {
  294. struct iova *iova = container_of(node, struct iova, node);
  295. if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
  296. return 1;
  297. return 0;
  298. }
  299. static struct iova *
  300. __insert_new_range(struct iova_domain *iovad,
  301. unsigned long pfn_lo, unsigned long pfn_hi)
  302. {
  303. struct iova *iova;
  304. iova = alloc_iova_mem();
  305. if (!iova)
  306. return iova;
  307. iova->pfn_hi = pfn_hi;
  308. iova->pfn_lo = pfn_lo;
  309. iova_insert_rbtree(&iovad->rbroot, iova);
  310. return iova;
  311. }
  312. static void
  313. __adjust_overlap_range(struct iova *iova,
  314. unsigned long *pfn_lo, unsigned long *pfn_hi)
  315. {
  316. if (*pfn_lo < iova->pfn_lo)
  317. iova->pfn_lo = *pfn_lo;
  318. if (*pfn_hi > iova->pfn_hi)
  319. *pfn_lo = iova->pfn_hi + 1;
  320. }
  321. /**
  322. * reserve_iova - reserves an iova in the given range
  323. * @iovad: - iova domain pointer
  324. * @pfn_lo: - lower page frame address
  325. * @pfn_hi:- higher pfn adderss
  326. * This function allocates reserves the address range from pfn_lo to pfn_hi so
  327. * that this address is not dished out as part of alloc_iova.
  328. */
  329. struct iova *
  330. reserve_iova(struct iova_domain *iovad,
  331. unsigned long pfn_lo, unsigned long pfn_hi)
  332. {
  333. struct rb_node *node;
  334. unsigned long flags;
  335. struct iova *iova;
  336. unsigned int overlap = 0;
  337. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  338. for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
  339. if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
  340. iova = container_of(node, struct iova, node);
  341. __adjust_overlap_range(iova, &pfn_lo, &pfn_hi);
  342. if ((pfn_lo >= iova->pfn_lo) &&
  343. (pfn_hi <= iova->pfn_hi))
  344. goto finish;
  345. overlap = 1;
  346. } else if (overlap)
  347. break;
  348. }
  349. /* We are here either because this is the first reserver node
  350. * or need to insert remaining non overlap addr range
  351. */
  352. iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
  353. finish:
  354. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  355. return iova;
  356. }
  357. /**
  358. * copy_reserved_iova - copies the reserved between domains
  359. * @from: - source doamin from where to copy
  360. * @to: - destination domin where to copy
  361. * This function copies reserved iova's from one doamin to
  362. * other.
  363. */
  364. void
  365. copy_reserved_iova(struct iova_domain *from, struct iova_domain *to)
  366. {
  367. unsigned long flags;
  368. struct rb_node *node;
  369. spin_lock_irqsave(&from->iova_rbtree_lock, flags);
  370. for (node = rb_first(&from->rbroot); node; node = rb_next(node)) {
  371. struct iova *iova = container_of(node, struct iova, node);
  372. struct iova *new_iova;
  373. new_iova = reserve_iova(to, iova->pfn_lo, iova->pfn_hi);
  374. if (!new_iova)
  375. printk(KERN_ERR "Reserve iova range %lx@%lx failed\n",
  376. iova->pfn_lo, iova->pfn_lo);
  377. }
  378. spin_unlock_irqrestore(&from->iova_rbtree_lock, flags);
  379. }