2 * An very simplified iova tree implementation based on GTree.
4 * Copyright 2018 Red Hat, Inc.
7 * Peter Xu <peterx@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
15 * Currently the iova tree will only allow to keep ranges
16 * information, and no extra user data is allowed for each element. A
17 * benefit is that we can merge adjacent ranges internally within the
18 * tree. It can save a lot of memory when the ranges are splitted but
21 * Note that current implementation does not provide any thread
22 * protections. Callers of the iova tree should be responsible
23 * for the thread safety issue.
26 #include "qemu/osdep.h"
27 #include "exec/memory.h"
28 #include "exec/hwaddr.h"
31 #define IOVA_ERR_INVALID (-1) /* Invalid parameters */
32 #define IOVA_ERR_OVERLAP (-2) /* IOVA range overlapped */
34 typedef struct IOVATree IOVATree
;
35 typedef struct DMAMap
{
37 hwaddr translated_addr
;
38 hwaddr size
; /* Inclusive */
39 IOMMUAccessFlags perm
;
41 typedef gboolean (*iova_tree_iterator
)(DMAMap
*map
);
46 * Create a new iova tree.
48 * Returns: the tree pointer when succeeded, or NULL if error.
50 IOVATree
*iova_tree_new(void);
55 * @tree: the iova tree to insert
56 * @map: the mapping to insert
58 * Insert an iova range to the tree. If there is overlapped
59 * ranges, IOVA_ERR_OVERLAP will be returned.
61 * Return: 0 if succeeded, or <0 if error.
63 int iova_tree_insert(IOVATree
*tree
, DMAMap
*map
);
68 * @tree: the iova tree to remove range from
69 * @map: the map range to remove
71 * Remove mappings from the tree that are covered by the map range
72 * provided. The range does not need to be exactly what has inserted,
73 * all the mappings that are included in the provided range will be
74 * removed from the tree. Here map->translated_addr is meaningless.
76 * Return: 0 if succeeded, or <0 if error.
78 int iova_tree_remove(IOVATree
*tree
, DMAMap
*map
);
83 * @tree: the iova tree to search from
84 * @map: the mapping to search
86 * Search for a mapping in the iova tree that overlaps with the
87 * mapping range specified. Only the first found mapping will be
90 * Return: DMAMap pointer if found, or NULL if not found. Note that
91 * the returned DMAMap pointer is maintained internally. User should
92 * only read the content but never modify or free the content. Also,
93 * user is responsible to make sure the pointer is valid (say, no
94 * concurrent deletion in progress).
96 DMAMap
*iova_tree_find(IOVATree
*tree
, DMAMap
*map
);
99 * iova_tree_find_address:
101 * @tree: the iova tree to search from
102 * @iova: the iova address to find
104 * Similar to iova_tree_find(), but it tries to find mapping with
105 * range iova=iova & size=0.
107 * Return: same as iova_tree_find().
109 DMAMap
*iova_tree_find_address(IOVATree
*tree
, hwaddr iova
);
114 * @tree: the iova tree to iterate on
115 * @iterator: the interator for the mappings, return true to stop
117 * Iterate over the iova tree.
119 * Return: 1 if found any overlap, 0 if not, <0 if error.
121 void iova_tree_foreach(IOVATree
*tree
, iova_tree_iterator iterator
);
126 * @tree: the iova tree to destroy
128 * Destroy an existing iova tree.
132 void iova_tree_destroy(IOVATree
*tree
);