xref: /openbmc/linux/drivers/vhost/iotlb.c (revision 349f631d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2020 Red Hat, Inc.
3  * Author: Jason Wang <jasowang@redhat.com>
4  *
5  * IOTLB implementation for vhost.
6  */
7 #include <linux/slab.h>
8 #include <linux/vhost_iotlb.h>
9 #include <linux/module.h>
10 
11 #define MOD_VERSION  "0.1"
12 #define MOD_DESC     "VHOST IOTLB"
13 #define MOD_AUTHOR   "Jason Wang <jasowang@redhat.com>"
14 #define MOD_LICENSE  "GPL v2"
15 
16 #define START(map) ((map)->start)
17 #define LAST(map) ((map)->last)
18 
19 INTERVAL_TREE_DEFINE(struct vhost_iotlb_map,
20 		     rb, __u64, __subtree_last,
21 		     START, LAST, static inline, vhost_iotlb_itree);
22 
23 /**
24  * vhost_iotlb_map_free - remove a map node and free it
25  * @iotlb: the IOTLB
26  * @map: the map that want to be remove and freed
27  */
28 void vhost_iotlb_map_free(struct vhost_iotlb *iotlb,
29 			  struct vhost_iotlb_map *map)
30 {
31 	vhost_iotlb_itree_remove(map, &iotlb->root);
32 	list_del(&map->link);
33 	kfree(map);
34 	iotlb->nmaps--;
35 }
36 EXPORT_SYMBOL_GPL(vhost_iotlb_map_free);
37 
38 /**
39  * vhost_iotlb_add_range_ctx - add a new range to vhost IOTLB
40  * @iotlb: the IOTLB
41  * @start: start of the IOVA range
42  * @last: last of IOVA range
43  * @addr: the address that is mapped to @start
44  * @perm: access permission of this range
45  * @opaque: the opaque pointer for the new mapping
46  *
47  * Returns an error last is smaller than start or memory allocation
48  * fails
49  */
50 int vhost_iotlb_add_range_ctx(struct vhost_iotlb *iotlb,
51 			      u64 start, u64 last,
52 			      u64 addr, unsigned int perm,
53 			      void *opaque)
54 {
55 	struct vhost_iotlb_map *map;
56 
57 	if (last < start)
58 		return -EFAULT;
59 
60 	if (iotlb->limit &&
61 	    iotlb->nmaps == iotlb->limit &&
62 	    iotlb->flags & VHOST_IOTLB_FLAG_RETIRE) {
63 		map = list_first_entry(&iotlb->list, typeof(*map), link);
64 		vhost_iotlb_map_free(iotlb, map);
65 	}
66 
67 	map = kmalloc(sizeof(*map), GFP_ATOMIC);
68 	if (!map)
69 		return -ENOMEM;
70 
71 	map->start = start;
72 	map->size = last - start + 1;
73 	map->last = last;
74 	map->addr = addr;
75 	map->perm = perm;
76 	map->opaque = opaque;
77 
78 	iotlb->nmaps++;
79 	vhost_iotlb_itree_insert(map, &iotlb->root);
80 
81 	INIT_LIST_HEAD(&map->link);
82 	list_add_tail(&map->link, &iotlb->list);
83 
84 	return 0;
85 }
86 EXPORT_SYMBOL_GPL(vhost_iotlb_add_range_ctx);
87 
88 int vhost_iotlb_add_range(struct vhost_iotlb *iotlb,
89 			  u64 start, u64 last,
90 			  u64 addr, unsigned int perm)
91 {
92 	return vhost_iotlb_add_range_ctx(iotlb, start, last,
93 					 addr, perm, NULL);
94 }
95 EXPORT_SYMBOL_GPL(vhost_iotlb_add_range);
96 
97 /**
98  * vhost_iotlb_del_range - delete overlapped ranges from vhost IOTLB
99  * @iotlb: the IOTLB
100  * @start: start of the IOVA range
101  * @last: last of IOVA range
102  */
103 void vhost_iotlb_del_range(struct vhost_iotlb *iotlb, u64 start, u64 last)
104 {
105 	struct vhost_iotlb_map *map;
106 
107 	while ((map = vhost_iotlb_itree_iter_first(&iotlb->root,
108 						   start, last)))
109 		vhost_iotlb_map_free(iotlb, map);
110 }
111 EXPORT_SYMBOL_GPL(vhost_iotlb_del_range);
112 
113 /**
114  * vhost_iotlb_alloc - add a new vhost IOTLB
115  * @limit: maximum number of IOTLB entries
116  * @flags: VHOST_IOTLB_FLAG_XXX
117  *
118  * Returns an error is memory allocation fails
119  */
120 struct vhost_iotlb *vhost_iotlb_alloc(unsigned int limit, unsigned int flags)
121 {
122 	struct vhost_iotlb *iotlb = kzalloc(sizeof(*iotlb), GFP_KERNEL);
123 
124 	if (!iotlb)
125 		return NULL;
126 
127 	iotlb->root = RB_ROOT_CACHED;
128 	iotlb->limit = limit;
129 	iotlb->nmaps = 0;
130 	iotlb->flags = flags;
131 	INIT_LIST_HEAD(&iotlb->list);
132 
133 	return iotlb;
134 }
135 EXPORT_SYMBOL_GPL(vhost_iotlb_alloc);
136 
137 /**
138  * vhost_iotlb_reset - reset vhost IOTLB (free all IOTLB entries)
139  * @iotlb: the IOTLB to be reset
140  */
141 void vhost_iotlb_reset(struct vhost_iotlb *iotlb)
142 {
143 	vhost_iotlb_del_range(iotlb, 0ULL, 0ULL - 1);
144 }
145 EXPORT_SYMBOL_GPL(vhost_iotlb_reset);
146 
147 /**
148  * vhost_iotlb_free - reset and free vhost IOTLB
149  * @iotlb: the IOTLB to be freed
150  */
151 void vhost_iotlb_free(struct vhost_iotlb *iotlb)
152 {
153 	if (iotlb) {
154 		vhost_iotlb_reset(iotlb);
155 		kfree(iotlb);
156 	}
157 }
158 EXPORT_SYMBOL_GPL(vhost_iotlb_free);
159 
160 /**
161  * vhost_iotlb_itree_first - return the first overlapped range
162  * @iotlb: the IOTLB
163  * @start: start of IOVA range
164  * @last: last byte in IOVA range
165  */
166 struct vhost_iotlb_map *
167 vhost_iotlb_itree_first(struct vhost_iotlb *iotlb, u64 start, u64 last)
168 {
169 	return vhost_iotlb_itree_iter_first(&iotlb->root, start, last);
170 }
171 EXPORT_SYMBOL_GPL(vhost_iotlb_itree_first);
172 
173 /**
174  * vhost_iotlb_itree_next - return the next overlapped range
175  * @map: the starting map node
176  * @start: start of IOVA range
177  * @last: last byte IOVA range
178  */
179 struct vhost_iotlb_map *
180 vhost_iotlb_itree_next(struct vhost_iotlb_map *map, u64 start, u64 last)
181 {
182 	return vhost_iotlb_itree_iter_next(map, start, last);
183 }
184 EXPORT_SYMBOL_GPL(vhost_iotlb_itree_next);
185 
186 MODULE_VERSION(MOD_VERSION);
187 MODULE_DESCRIPTION(MOD_DESC);
188 MODULE_AUTHOR(MOD_AUTHOR);
189 MODULE_LICENSE(MOD_LICENSE);
190