iova.c 12 KB

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