iova.c 14 KB

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