iova-tree.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * IOVA tree implementation based on GTree.
  3. *
  4. * Copyright 2018 Red Hat, Inc.
  5. *
  6. * Authors:
  7. * Peter Xu <peterx@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. */
  11. #include "qemu/osdep.h"
  12. #include "qemu/iova-tree.h"
  13. struct IOVATree {
  14. GTree *tree;
  15. };
  16. /* Args to pass to iova_tree_alloc foreach function. */
  17. struct IOVATreeAllocArgs {
  18. /* Size of the desired allocation */
  19. size_t new_size;
  20. /* The minimum address allowed in the allocation */
  21. hwaddr iova_begin;
  22. /* Map at the left of the hole, can be NULL if "this" is first one */
  23. const DMAMap *prev;
  24. /* Map at the right of the hole, can be NULL if "prev" is the last one */
  25. const DMAMap *this;
  26. /* If found, we fill in the IOVA here */
  27. hwaddr iova_result;
  28. /* Whether have we found a valid IOVA */
  29. bool iova_found;
  30. };
  31. typedef struct IOVATreeFindIOVAArgs {
  32. const DMAMap *needle;
  33. const DMAMap *result;
  34. } IOVATreeFindIOVAArgs;
  35. /**
  36. * Iterate args to the next hole
  37. *
  38. * @args: The alloc arguments
  39. * @next: The next mapping in the tree. Can be NULL to signal the last one
  40. */
  41. static void iova_tree_alloc_args_iterate(struct IOVATreeAllocArgs *args,
  42. const DMAMap *next)
  43. {
  44. args->prev = args->this;
  45. args->this = next;
  46. }
  47. static int iova_tree_compare(gconstpointer a, gconstpointer b, gpointer data)
  48. {
  49. const DMAMap *m1 = a, *m2 = b;
  50. if (m1->iova > m2->iova + m2->size) {
  51. return 1;
  52. }
  53. if (m1->iova + m1->size < m2->iova) {
  54. return -1;
  55. }
  56. /* Overlapped */
  57. return 0;
  58. }
  59. IOVATree *iova_tree_new(void)
  60. {
  61. IOVATree *iova_tree = g_new0(IOVATree, 1);
  62. /* We don't have values actually, no need to free */
  63. iova_tree->tree = g_tree_new_full(iova_tree_compare, NULL, g_free, NULL);
  64. return iova_tree;
  65. }
  66. const DMAMap *iova_tree_find(const IOVATree *tree, const DMAMap *map)
  67. {
  68. return g_tree_lookup(tree->tree, map);
  69. }
  70. static gboolean iova_tree_find_address_iterator(gpointer key, gpointer value,
  71. gpointer data)
  72. {
  73. const DMAMap *map = key;
  74. IOVATreeFindIOVAArgs *args = data;
  75. const DMAMap *needle;
  76. g_assert(key == value);
  77. needle = args->needle;
  78. if (map->translated_addr + map->size < needle->translated_addr ||
  79. needle->translated_addr + needle->size < map->translated_addr) {
  80. return false;
  81. }
  82. args->result = map;
  83. return true;
  84. }
  85. const DMAMap *iova_tree_find_iova(const IOVATree *tree, const DMAMap *map)
  86. {
  87. IOVATreeFindIOVAArgs args = {
  88. .needle = map,
  89. };
  90. g_tree_foreach(tree->tree, iova_tree_find_address_iterator, &args);
  91. return args.result;
  92. }
  93. const DMAMap *iova_tree_find_address(const IOVATree *tree, hwaddr iova)
  94. {
  95. const DMAMap map = { .iova = iova, .size = 0 };
  96. return iova_tree_find(tree, &map);
  97. }
  98. static inline void iova_tree_insert_internal(GTree *gtree, DMAMap *range)
  99. {
  100. /* Key and value are sharing the same range data */
  101. g_tree_insert(gtree, range, range);
  102. }
  103. int iova_tree_insert(IOVATree *tree, const DMAMap *map)
  104. {
  105. DMAMap *new;
  106. if (map->iova + map->size < map->iova || map->perm == IOMMU_NONE) {
  107. return IOVA_ERR_INVALID;
  108. }
  109. /* We don't allow to insert range that overlaps with existings */
  110. if (iova_tree_find(tree, map)) {
  111. return IOVA_ERR_OVERLAP;
  112. }
  113. new = g_new0(DMAMap, 1);
  114. memcpy(new, map, sizeof(*new));
  115. iova_tree_insert_internal(tree->tree, new);
  116. return IOVA_OK;
  117. }
  118. static gboolean iova_tree_traverse(gpointer key, gpointer value,
  119. gpointer data)
  120. {
  121. iova_tree_iterator iterator = data;
  122. DMAMap *map = key;
  123. g_assert(key == value);
  124. return iterator(map);
  125. }
  126. void iova_tree_foreach(IOVATree *tree, iova_tree_iterator iterator)
  127. {
  128. g_tree_foreach(tree->tree, iova_tree_traverse, iterator);
  129. }
  130. void iova_tree_remove(IOVATree *tree, DMAMap map)
  131. {
  132. const DMAMap *overlap;
  133. while ((overlap = iova_tree_find(tree, &map))) {
  134. g_tree_remove(tree->tree, overlap);
  135. }
  136. }
  137. /**
  138. * Try to find an unallocated IOVA range between prev and this elements.
  139. *
  140. * @args: Arguments to allocation
  141. *
  142. * Cases:
  143. *
  144. * (1) !prev, !this: No entries allocated, always succeed
  145. *
  146. * (2) !prev, this: We're iterating at the 1st element.
  147. *
  148. * (3) prev, !this: We're iterating at the last element.
  149. *
  150. * (4) prev, this: this is the most common case, we'll try to find a hole
  151. * between "prev" and "this" mapping.
  152. *
  153. * Note that this function assumes the last valid iova is HWADDR_MAX, but it
  154. * searches linearly so it's easy to discard the result if it's not the case.
  155. */
  156. static void iova_tree_alloc_map_in_hole(struct IOVATreeAllocArgs *args)
  157. {
  158. const DMAMap *prev = args->prev, *this = args->this;
  159. uint64_t hole_start, hole_last;
  160. if (this && this->iova + this->size < args->iova_begin) {
  161. return;
  162. }
  163. hole_start = MAX(prev ? prev->iova + prev->size + 1 : 0, args->iova_begin);
  164. hole_last = this ? this->iova : HWADDR_MAX;
  165. if (hole_last - hole_start > args->new_size) {
  166. args->iova_result = hole_start;
  167. args->iova_found = true;
  168. }
  169. }
  170. /**
  171. * Foreach dma node in the tree, compare if there is a hole with its previous
  172. * node (or minimum iova address allowed) and the node.
  173. *
  174. * @key: Node iterating
  175. * @value: Node iterating
  176. * @pargs: Struct to communicate with the outside world
  177. *
  178. * Return: false to keep iterating, true if needs break.
  179. */
  180. static gboolean iova_tree_alloc_traverse(gpointer key, gpointer value,
  181. gpointer pargs)
  182. {
  183. struct IOVATreeAllocArgs *args = pargs;
  184. DMAMap *node = value;
  185. assert(key == value);
  186. iova_tree_alloc_args_iterate(args, node);
  187. iova_tree_alloc_map_in_hole(args);
  188. return args->iova_found;
  189. }
  190. int iova_tree_alloc_map(IOVATree *tree, DMAMap *map, hwaddr iova_begin,
  191. hwaddr iova_last)
  192. {
  193. struct IOVATreeAllocArgs args = {
  194. .new_size = map->size,
  195. .iova_begin = iova_begin,
  196. };
  197. if (unlikely(iova_last < iova_begin)) {
  198. return IOVA_ERR_INVALID;
  199. }
  200. /*
  201. * Find a valid hole for the mapping
  202. *
  203. * Assuming low iova_begin, so no need to do a binary search to
  204. * locate the first node.
  205. *
  206. * TODO: Replace all this with g_tree_node_first/next/last when available
  207. * (from glib since 2.68). To do it with g_tree_foreach complicates the
  208. * code a lot.
  209. *
  210. */
  211. g_tree_foreach(tree->tree, iova_tree_alloc_traverse, &args);
  212. if (!args.iova_found) {
  213. /*
  214. * Either tree is empty or the last hole is still not checked.
  215. * g_tree_foreach does not compare (last, iova_last] range, so we check
  216. * it here.
  217. */
  218. iova_tree_alloc_args_iterate(&args, NULL);
  219. iova_tree_alloc_map_in_hole(&args);
  220. }
  221. if (!args.iova_found || args.iova_result + map->size > iova_last) {
  222. return IOVA_ERR_NOMEM;
  223. }
  224. map->iova = args.iova_result;
  225. return iova_tree_insert(tree, map);
  226. }
  227. void iova_tree_destroy(IOVATree *tree)
  228. {
  229. g_tree_destroy(tree->tree);
  230. g_free(tree);
  231. }