target/mips/mips-defs: Rename ISA_MIPS32R5 as ISA_MIPS_R5
[qemu/kevin.git] / hw / virtio / vhost-backend.c
blob222bbcc62de5717491de9ce0d2cb2907a11b5471
1 /*
2 * vhost-backend
4 * Copyright (c) 2013 Virtual Open Systems Sarl.
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
9 */
11 #include "qemu/osdep.h"
12 #include "hw/virtio/vhost.h"
13 #include "hw/virtio/vhost-backend.h"
14 #include "qemu/error-report.h"
15 #include "qemu/main-loop.h"
16 #include "standard-headers/linux/vhost_types.h"
18 #include "hw/virtio/vhost-vdpa.h"
19 #ifdef CONFIG_VHOST_KERNEL
20 #include <linux/vhost.h>
21 #include <sys/ioctl.h>
23 static int vhost_kernel_call(struct vhost_dev *dev, unsigned long int request,
24 void *arg)
26 int fd = (uintptr_t) dev->opaque;
28 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_KERNEL);
30 return ioctl(fd, request, arg);
33 static int vhost_kernel_init(struct vhost_dev *dev, void *opaque)
35 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_KERNEL);
37 dev->opaque = opaque;
39 return 0;
42 static int vhost_kernel_cleanup(struct vhost_dev *dev)
44 int fd = (uintptr_t) dev->opaque;
46 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_KERNEL);
48 return close(fd);
51 static int vhost_kernel_memslots_limit(struct vhost_dev *dev)
53 int limit = 64;
54 char *s;
56 if (g_file_get_contents("/sys/module/vhost/parameters/max_mem_regions",
57 &s, NULL, NULL)) {
58 uint64_t val = g_ascii_strtoull(s, NULL, 10);
59 if (!((val == G_MAXUINT64 || !val) && errno)) {
60 g_free(s);
61 return val;
63 error_report("ignoring invalid max_mem_regions value in vhost module:"
64 " %s", s);
66 g_free(s);
67 return limit;
70 static int vhost_kernel_net_set_backend(struct vhost_dev *dev,
71 struct vhost_vring_file *file)
73 return vhost_kernel_call(dev, VHOST_NET_SET_BACKEND, file);
76 static int vhost_kernel_scsi_set_endpoint(struct vhost_dev *dev,
77 struct vhost_scsi_target *target)
79 return vhost_kernel_call(dev, VHOST_SCSI_SET_ENDPOINT, target);
82 static int vhost_kernel_scsi_clear_endpoint(struct vhost_dev *dev,
83 struct vhost_scsi_target *target)
85 return vhost_kernel_call(dev, VHOST_SCSI_CLEAR_ENDPOINT, target);
88 static int vhost_kernel_scsi_get_abi_version(struct vhost_dev *dev, int *version)
90 return vhost_kernel_call(dev, VHOST_SCSI_GET_ABI_VERSION, version);
93 static int vhost_kernel_set_log_base(struct vhost_dev *dev, uint64_t base,
94 struct vhost_log *log)
96 return vhost_kernel_call(dev, VHOST_SET_LOG_BASE, &base);
99 static int vhost_kernel_set_mem_table(struct vhost_dev *dev,
100 struct vhost_memory *mem)
102 return vhost_kernel_call(dev, VHOST_SET_MEM_TABLE, mem);
105 static int vhost_kernel_set_vring_addr(struct vhost_dev *dev,
106 struct vhost_vring_addr *addr)
108 return vhost_kernel_call(dev, VHOST_SET_VRING_ADDR, addr);
111 static int vhost_kernel_set_vring_endian(struct vhost_dev *dev,
112 struct vhost_vring_state *ring)
114 return vhost_kernel_call(dev, VHOST_SET_VRING_ENDIAN, ring);
117 static int vhost_kernel_set_vring_num(struct vhost_dev *dev,
118 struct vhost_vring_state *ring)
120 return vhost_kernel_call(dev, VHOST_SET_VRING_NUM, ring);
123 static int vhost_kernel_set_vring_base(struct vhost_dev *dev,
124 struct vhost_vring_state *ring)
126 return vhost_kernel_call(dev, VHOST_SET_VRING_BASE, ring);
129 static int vhost_kernel_get_vring_base(struct vhost_dev *dev,
130 struct vhost_vring_state *ring)
132 return vhost_kernel_call(dev, VHOST_GET_VRING_BASE, ring);
135 static int vhost_kernel_set_vring_kick(struct vhost_dev *dev,
136 struct vhost_vring_file *file)
138 return vhost_kernel_call(dev, VHOST_SET_VRING_KICK, file);
141 static int vhost_kernel_set_vring_call(struct vhost_dev *dev,
142 struct vhost_vring_file *file)
144 return vhost_kernel_call(dev, VHOST_SET_VRING_CALL, file);
147 static int vhost_kernel_set_vring_busyloop_timeout(struct vhost_dev *dev,
148 struct vhost_vring_state *s)
150 return vhost_kernel_call(dev, VHOST_SET_VRING_BUSYLOOP_TIMEOUT, s);
153 static int vhost_kernel_set_features(struct vhost_dev *dev,
154 uint64_t features)
156 return vhost_kernel_call(dev, VHOST_SET_FEATURES, &features);
159 static int vhost_kernel_set_backend_cap(struct vhost_dev *dev)
161 uint64_t features;
162 uint64_t f = 0x1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2;
163 int r;
165 if (vhost_kernel_call(dev, VHOST_GET_BACKEND_FEATURES, &features)) {
166 return 0;
169 features &= f;
170 r = vhost_kernel_call(dev, VHOST_SET_BACKEND_FEATURES,
171 &features);
172 if (r) {
173 return 0;
176 dev->backend_cap = features;
178 return 0;
181 static int vhost_kernel_get_features(struct vhost_dev *dev,
182 uint64_t *features)
184 return vhost_kernel_call(dev, VHOST_GET_FEATURES, features);
187 static int vhost_kernel_set_owner(struct vhost_dev *dev)
189 return vhost_kernel_call(dev, VHOST_SET_OWNER, NULL);
192 static int vhost_kernel_reset_device(struct vhost_dev *dev)
194 return vhost_kernel_call(dev, VHOST_RESET_OWNER, NULL);
197 static int vhost_kernel_get_vq_index(struct vhost_dev *dev, int idx)
199 assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs);
201 return idx - dev->vq_index;
204 #ifdef CONFIG_VHOST_VSOCK
205 static int vhost_kernel_vsock_set_guest_cid(struct vhost_dev *dev,
206 uint64_t guest_cid)
208 return vhost_kernel_call(dev, VHOST_VSOCK_SET_GUEST_CID, &guest_cid);
211 static int vhost_kernel_vsock_set_running(struct vhost_dev *dev, int start)
213 return vhost_kernel_call(dev, VHOST_VSOCK_SET_RUNNING, &start);
215 #endif /* CONFIG_VHOST_VSOCK */
217 static void vhost_kernel_iotlb_read(void *opaque)
219 struct vhost_dev *dev = opaque;
220 ssize_t len;
222 if (dev->backend_cap &
223 (0x1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2)) {
224 struct vhost_msg_v2 msg;
226 while ((len = read((uintptr_t)dev->opaque, &msg, sizeof msg)) > 0) {
227 if (len < sizeof msg) {
228 error_report("Wrong vhost message len: %d", (int)len);
229 break;
231 if (msg.type != VHOST_IOTLB_MSG_V2) {
232 error_report("Unknown vhost iotlb message type");
233 break;
236 vhost_backend_handle_iotlb_msg(dev, &msg.iotlb);
238 } else {
239 struct vhost_msg msg;
241 while ((len = read((uintptr_t)dev->opaque, &msg, sizeof msg)) > 0) {
242 if (len < sizeof msg) {
243 error_report("Wrong vhost message len: %d", (int)len);
244 break;
246 if (msg.type != VHOST_IOTLB_MSG) {
247 error_report("Unknown vhost iotlb message type");
248 break;
251 vhost_backend_handle_iotlb_msg(dev, &msg.iotlb);
256 static int vhost_kernel_send_device_iotlb_msg(struct vhost_dev *dev,
257 struct vhost_iotlb_msg *imsg)
259 if (dev->backend_cap & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2)) {
260 struct vhost_msg_v2 msg = {};
262 msg.type = VHOST_IOTLB_MSG_V2;
263 msg.iotlb = *imsg;
265 if (write((uintptr_t)dev->opaque, &msg, sizeof msg) != sizeof msg) {
266 error_report("Fail to update device iotlb");
267 return -EFAULT;
269 } else {
270 struct vhost_msg msg = {};
272 msg.type = VHOST_IOTLB_MSG;
273 msg.iotlb = *imsg;
275 if (write((uintptr_t)dev->opaque, &msg, sizeof msg) != sizeof msg) {
276 error_report("Fail to update device iotlb");
277 return -EFAULT;
281 return 0;
284 static void vhost_kernel_set_iotlb_callback(struct vhost_dev *dev,
285 int enabled)
287 if (enabled)
288 qemu_set_fd_handler((uintptr_t)dev->opaque,
289 vhost_kernel_iotlb_read, NULL, dev);
290 else
291 qemu_set_fd_handler((uintptr_t)dev->opaque, NULL, NULL, NULL);
294 static const VhostOps kernel_ops = {
295 .backend_type = VHOST_BACKEND_TYPE_KERNEL,
296 .vhost_backend_init = vhost_kernel_init,
297 .vhost_backend_cleanup = vhost_kernel_cleanup,
298 .vhost_backend_memslots_limit = vhost_kernel_memslots_limit,
299 .vhost_net_set_backend = vhost_kernel_net_set_backend,
300 .vhost_scsi_set_endpoint = vhost_kernel_scsi_set_endpoint,
301 .vhost_scsi_clear_endpoint = vhost_kernel_scsi_clear_endpoint,
302 .vhost_scsi_get_abi_version = vhost_kernel_scsi_get_abi_version,
303 .vhost_set_log_base = vhost_kernel_set_log_base,
304 .vhost_set_mem_table = vhost_kernel_set_mem_table,
305 .vhost_set_vring_addr = vhost_kernel_set_vring_addr,
306 .vhost_set_vring_endian = vhost_kernel_set_vring_endian,
307 .vhost_set_vring_num = vhost_kernel_set_vring_num,
308 .vhost_set_vring_base = vhost_kernel_set_vring_base,
309 .vhost_get_vring_base = vhost_kernel_get_vring_base,
310 .vhost_set_vring_kick = vhost_kernel_set_vring_kick,
311 .vhost_set_vring_call = vhost_kernel_set_vring_call,
312 .vhost_set_vring_busyloop_timeout =
313 vhost_kernel_set_vring_busyloop_timeout,
314 .vhost_set_features = vhost_kernel_set_features,
315 .vhost_get_features = vhost_kernel_get_features,
316 .vhost_set_backend_cap = vhost_kernel_set_backend_cap,
317 .vhost_set_owner = vhost_kernel_set_owner,
318 .vhost_reset_device = vhost_kernel_reset_device,
319 .vhost_get_vq_index = vhost_kernel_get_vq_index,
320 #ifdef CONFIG_VHOST_VSOCK
321 .vhost_vsock_set_guest_cid = vhost_kernel_vsock_set_guest_cid,
322 .vhost_vsock_set_running = vhost_kernel_vsock_set_running,
323 #endif /* CONFIG_VHOST_VSOCK */
324 .vhost_set_iotlb_callback = vhost_kernel_set_iotlb_callback,
325 .vhost_send_device_iotlb_msg = vhost_kernel_send_device_iotlb_msg,
327 #endif
329 int vhost_set_backend_type(struct vhost_dev *dev, VhostBackendType backend_type)
331 int r = 0;
333 switch (backend_type) {
334 #ifdef CONFIG_VHOST_KERNEL
335 case VHOST_BACKEND_TYPE_KERNEL:
336 dev->vhost_ops = &kernel_ops;
337 break;
338 #endif
339 #ifdef CONFIG_VHOST_USER
340 case VHOST_BACKEND_TYPE_USER:
341 dev->vhost_ops = &user_ops;
342 break;
343 #endif
344 #ifdef CONFIG_VHOST_VDPA
345 case VHOST_BACKEND_TYPE_VDPA:
346 dev->vhost_ops = &vdpa_ops;
347 break;
348 #endif
349 default:
350 error_report("Unknown vhost backend type");
351 r = -1;
354 return r;
357 int vhost_backend_update_device_iotlb(struct vhost_dev *dev,
358 uint64_t iova, uint64_t uaddr,
359 uint64_t len,
360 IOMMUAccessFlags perm)
362 struct vhost_iotlb_msg imsg;
364 imsg.iova = iova;
365 imsg.uaddr = uaddr;
366 imsg.size = len;
367 imsg.type = VHOST_IOTLB_UPDATE;
369 switch (perm) {
370 case IOMMU_RO:
371 imsg.perm = VHOST_ACCESS_RO;
372 break;
373 case IOMMU_WO:
374 imsg.perm = VHOST_ACCESS_WO;
375 break;
376 case IOMMU_RW:
377 imsg.perm = VHOST_ACCESS_RW;
378 break;
379 default:
380 return -EINVAL;
383 if (dev->vhost_ops && dev->vhost_ops->vhost_send_device_iotlb_msg)
384 return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg);
386 return -ENODEV;
389 int vhost_backend_invalidate_device_iotlb(struct vhost_dev *dev,
390 uint64_t iova, uint64_t len)
392 struct vhost_iotlb_msg imsg;
394 imsg.iova = iova;
395 imsg.size = len;
396 imsg.type = VHOST_IOTLB_INVALIDATE;
398 if (dev->vhost_ops && dev->vhost_ops->vhost_send_device_iotlb_msg)
399 return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg);
401 return -ENODEV;
404 int vhost_backend_handle_iotlb_msg(struct vhost_dev *dev,
405 struct vhost_iotlb_msg *imsg)
407 int ret = 0;
409 switch (imsg->type) {
410 case VHOST_IOTLB_MISS:
411 ret = vhost_device_iotlb_miss(dev, imsg->iova,
412 imsg->perm != VHOST_ACCESS_RO);
413 break;
414 case VHOST_IOTLB_ACCESS_FAIL:
415 /* FIXME: report device iotlb error */
416 error_report("Access failure IOTLB message type not supported");
417 ret = -ENOTSUP;
418 break;
419 case VHOST_IOTLB_UPDATE:
420 case VHOST_IOTLB_INVALIDATE:
421 default:
422 error_report("Unexpected IOTLB message type");
423 ret = -EINVAL;
424 break;
427 return ret;