iova.c 11 KB

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