Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / vhost / iotlb.c
blob0fd3f87e913c7f70e8d670729632f2bff33779cc
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2020 Red Hat, Inc.
3 * Author: Jason Wang <jasowang@redhat.com>
5 * IOTLB implementation for vhost.
6 */
7 #include <linux/slab.h>
8 #include <linux/vhost_iotlb.h>
9 #include <linux/module.h>
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"
16 #define START(map) ((map)->start)
17 #define LAST(map) ((map)->last)
19 INTERVAL_TREE_DEFINE(struct vhost_iotlb_map,
20 rb, __u64, __subtree_last,
21 START, LAST, static inline, vhost_iotlb_itree);
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
28 void vhost_iotlb_map_free(struct vhost_iotlb *iotlb,
29 struct vhost_iotlb_map *map)
31 vhost_iotlb_itree_remove(map, &iotlb->root);
32 list_del(&map->link);
33 kfree(map);
34 iotlb->nmaps--;
36 EXPORT_SYMBOL_GPL(vhost_iotlb_map_free);
38 /**
39 * vhost_iotlb_add_range - 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
46 * Returns an error last is smaller than start or memory allocation
47 * fails
49 int vhost_iotlb_add_range(struct vhost_iotlb *iotlb,
50 u64 start, u64 last,
51 u64 addr, unsigned int perm)
53 struct vhost_iotlb_map *map;
55 if (last < start)
56 return -EFAULT;
58 if (iotlb->limit &&
59 iotlb->nmaps == iotlb->limit &&
60 iotlb->flags & VHOST_IOTLB_FLAG_RETIRE) {
61 map = list_first_entry(&iotlb->list, typeof(*map), link);
62 vhost_iotlb_map_free(iotlb, map);
65 map = kmalloc(sizeof(*map), GFP_ATOMIC);
66 if (!map)
67 return -ENOMEM;
69 map->start = start;
70 map->size = last - start + 1;
71 map->last = last;
72 map->addr = addr;
73 map->perm = perm;
75 iotlb->nmaps++;
76 vhost_iotlb_itree_insert(map, &iotlb->root);
78 INIT_LIST_HEAD(&map->link);
79 list_add_tail(&map->link, &iotlb->list);
81 return 0;
83 EXPORT_SYMBOL_GPL(vhost_iotlb_add_range);
85 /**
86 * vring_iotlb_del_range - delete overlapped ranges from vhost IOTLB
87 * @iotlb: the IOTLB
88 * @start: start of the IOVA range
89 * @last: last of IOVA range
91 void vhost_iotlb_del_range(struct vhost_iotlb *iotlb, u64 start, u64 last)
93 struct vhost_iotlb_map *map;
95 while ((map = vhost_iotlb_itree_iter_first(&iotlb->root,
96 start, last)))
97 vhost_iotlb_map_free(iotlb, map);
99 EXPORT_SYMBOL_GPL(vhost_iotlb_del_range);
102 * vhost_iotlb_alloc - add a new vhost IOTLB
103 * @limit: maximum number of IOTLB entries
104 * @flags: VHOST_IOTLB_FLAG_XXX
106 * Returns an error is memory allocation fails
108 struct vhost_iotlb *vhost_iotlb_alloc(unsigned int limit, unsigned int flags)
110 struct vhost_iotlb *iotlb = kzalloc(sizeof(*iotlb), GFP_KERNEL);
112 if (!iotlb)
113 return NULL;
115 iotlb->root = RB_ROOT_CACHED;
116 iotlb->limit = limit;
117 iotlb->nmaps = 0;
118 iotlb->flags = flags;
119 INIT_LIST_HEAD(&iotlb->list);
121 return iotlb;
123 EXPORT_SYMBOL_GPL(vhost_iotlb_alloc);
126 * vhost_iotlb_reset - reset vhost IOTLB (free all IOTLB entries)
127 * @iotlb: the IOTLB to be reset
129 void vhost_iotlb_reset(struct vhost_iotlb *iotlb)
131 vhost_iotlb_del_range(iotlb, 0ULL, 0ULL - 1);
133 EXPORT_SYMBOL_GPL(vhost_iotlb_reset);
136 * vhost_iotlb_free - reset and free vhost IOTLB
137 * @iotlb: the IOTLB to be freed
139 void vhost_iotlb_free(struct vhost_iotlb *iotlb)
141 if (iotlb) {
142 vhost_iotlb_reset(iotlb);
143 kfree(iotlb);
146 EXPORT_SYMBOL_GPL(vhost_iotlb_free);
149 * vhost_iotlb_itree_first - return the first overlapped range
150 * @iotlb: the IOTLB
151 * @start: start of IOVA range
152 * @last: last byte in IOVA range
154 struct vhost_iotlb_map *
155 vhost_iotlb_itree_first(struct vhost_iotlb *iotlb, u64 start, u64 last)
157 return vhost_iotlb_itree_iter_first(&iotlb->root, start, last);
159 EXPORT_SYMBOL_GPL(vhost_iotlb_itree_first);
162 * vhost_iotlb_itree_next - return the next overlapped range
163 * @map: the starting map node
164 * @start: start of IOVA range
165 * @last: last byte IOVA range
167 struct vhost_iotlb_map *
168 vhost_iotlb_itree_next(struct vhost_iotlb_map *map, u64 start, u64 last)
170 return vhost_iotlb_itree_iter_next(map, start, last);
172 EXPORT_SYMBOL_GPL(vhost_iotlb_itree_next);
174 MODULE_VERSION(MOD_VERSION);
175 MODULE_DESCRIPTION(MOD_DESC);
176 MODULE_AUTHOR(MOD_AUTHOR);
177 MODULE_LICENSE(MOD_LICENSE);