master
c 492 lines 13.8 KB
Raw
1 /*
2 * vfio PCI device over a UNIX socket.
3 *
4 * Copyright © 2018, 2021 Oracle and/or its affiliates.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #include "qemu/osdep.h"
10 #include <sys/ioctl.h>
11 #include "qapi-visit-sockets.h"
12 #include "qemu/error-report.h"
13
14 #include "hw/core/qdev-properties.h"
15 #include "hw/vfio/pci.h"
16 #include "hw/vfio-user/device.h"
17 #include "hw/vfio-user/proxy.h"
18
19 #define TYPE_VFIO_USER_PCI "vfio-user-pci"
20 OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserPCIDevice, VFIO_USER_PCI)
21
22 struct VFIOUserPCIDevice {
23 VFIOPCIDevice parent_obj;
24
25 SocketAddress *socket;
26 bool send_queued; /* all sends are queued */
27 uint32_t wait_time; /* timeout for message replies */
28 bool no_post; /* all region writes are sync */
29 };
30
31 /*
32 * The server maintains the device's pending interrupts,
33 * via its MSIX table and PBA, so we treat these accesses
34 * like PCI config space and forward them.
35 */
36 static uint64_t vfio_user_pba_read(void *opaque, hwaddr addr,
37 unsigned size)
38 {
39 VFIOPCIDevice *vdev = opaque;
40 VFIORegion *region = &vdev->bars[vdev->msix->pba_bar].region;
41 uint64_t data;
42
43 /* server copy is what matters */
44 data = vfio_region_read(region, addr + vdev->msix->pba_offset, size);
45 return data;
46 }
47
48 static void vfio_user_pba_write(void *opaque, hwaddr addr,
49 uint64_t data, unsigned size)
50 {
51 /* dropped */
52 }
53
54 static const MemoryRegionOps vfio_user_pba_ops = {
55 .read = vfio_user_pba_read,
56 .write = vfio_user_pba_write,
57 .endianness = DEVICE_LITTLE_ENDIAN,
58 };
59
60 static void vfio_user_msix_setup(VFIOPCIDevice *vdev)
61 {
62 MemoryRegion *vfio_reg, *msix_reg, *pba_reg;
63
64 pba_reg = g_new0(MemoryRegion, 1);
65 vdev->msix->pba_region = pba_reg;
66
67 vfio_reg = vdev->bars[vdev->msix->pba_bar].mr;
68 msix_reg = &PCI_DEVICE(vdev)->msix_pba_mmio;
69 memory_region_init_io(pba_reg, OBJECT(vdev), &vfio_user_pba_ops, vdev,
70 "VFIO MSIX PBA", int128_get64(msix_reg->size));
71 memory_region_add_subregion_overlap(vfio_reg, vdev->msix->pba_offset,
72 pba_reg, 1);
73 }
74
75 static void vfio_user_msix_teardown(VFIOPCIDevice *vdev)
76 {
77 MemoryRegion *mr, *sub;
78
79 mr = vdev->bars[vdev->msix->pba_bar].mr;
80 sub = vdev->msix->pba_region;
81 memory_region_del_subregion(mr, sub);
82
83 g_free(vdev->msix->pba_region);
84 vdev->msix->pba_region = NULL;
85 }
86
87 static void vfio_user_dma_read(VFIOPCIDevice *vdev, VFIOUserDMARW *msg)
88 {
89 PCIDevice *pdev = PCI_DEVICE(vdev);
90 VFIOUserProxy *proxy = vdev->vbasedev.proxy;
91 VFIOUserDMARW *res;
92 MemTxResult r;
93 size_t size;
94
95 if (msg->hdr.size < sizeof(*msg)) {
96 vfio_user_send_error(proxy, &msg->hdr, EINVAL);
97 return;
98 }
99 if (msg->count > proxy->max_xfer_size) {
100 vfio_user_send_error(proxy, &msg->hdr, E2BIG);
101 return;
102 }
103
104 /* switch to our own message buffer */
105 size = msg->count + sizeof(VFIOUserDMARW);
106 res = g_malloc0(size);
107 memcpy(res, msg, sizeof(*res));
108 g_free(msg);
109
110 r = pci_dma_read(pdev, res->offset, &res->data, res->count);
111
112 /*
113 * pci_dma_read() doesn't support reporting short reads via the reply's
114 * count parameter; in this case, we'll reply with an error instead.
115 */
116 switch (r) {
117 case MEMTX_OK:
118 if (res->hdr.flags & VFIO_USER_NO_REPLY) {
119 g_free(res);
120 return;
121 }
122 vfio_user_send_reply(proxy, &res->hdr, size);
123 break;
124 case MEMTX_ERROR:
125 vfio_user_send_error(proxy, &res->hdr, EFAULT);
126 break;
127 case MEMTX_DECODE_ERROR:
128 vfio_user_send_error(proxy, &res->hdr, ENODEV);
129 break;
130 case MEMTX_ACCESS_ERROR:
131 vfio_user_send_error(proxy, &res->hdr, EPERM);
132 break;
133 default:
134 error_printf("vfio_user_dma_read unknown error %d\n", r);
135 vfio_user_send_error(vdev->vbasedev.proxy, &res->hdr, EINVAL);
136 }
137 }
138
139 static void vfio_user_dma_write(VFIOPCIDevice *vdev, VFIOUserDMARW *msg)
140 {
141 PCIDevice *pdev = PCI_DEVICE(vdev);
142 VFIOUserProxy *proxy = vdev->vbasedev.proxy;
143 VFIOUserDMARW *res;
144 MemTxResult r;
145
146 if (msg->hdr.size < sizeof(*msg)) {
147 vfio_user_send_error(proxy, &msg->hdr, EINVAL);
148 return;
149 }
150 /* make sure transfer count isn't larger than the message data */
151 if (msg->count > msg->hdr.size - sizeof(*msg)) {
152 vfio_user_send_error(proxy, &msg->hdr, E2BIG);
153 return;
154 }
155
156 r = pci_dma_write(pdev, msg->offset, &msg->data, msg->count);
157
158 res = g_malloc0(sizeof(*res));
159 memcpy(res, msg, sizeof(*res));
160 g_free(msg);
161
162 /*
163 * pci_dma_write() doesn't support reporting short writes via the reply's
164 * count parameter; in this case, we'll reply with an error instead.
165 */
166 switch (r) {
167 case MEMTX_OK:
168 if (res->hdr.flags & VFIO_USER_NO_REPLY) {
169 g_free(res);
170 return;
171 }
172
173 vfio_user_send_reply(proxy, &res->hdr, sizeof(*res));
174 break;
175 case MEMTX_ERROR:
176 vfio_user_send_error(proxy, &res->hdr, EFAULT);
177 break;
178 case MEMTX_DECODE_ERROR:
179 vfio_user_send_error(proxy, &res->hdr, ENODEV);
180 break;
181 case MEMTX_ACCESS_ERROR:
182 vfio_user_send_error(proxy, &res->hdr, EPERM);
183 break;
184 default:
185 error_printf("vfio_user_dma_write unknown error %d\n", r);
186 vfio_user_send_error(vdev->vbasedev.proxy, &res->hdr, EINVAL);
187 }
188 }
189
190 /*
191 * Incoming request message callback.
192 *
193 * Runs off main loop, so BQL held.
194 */
195 static void vfio_user_pci_process_req(void *opaque, VFIOUserMsg *msg)
196 {
197 VFIOPCIDevice *vdev = opaque;
198 VFIOUserHdr *hdr = msg->hdr;
199
200 /* no incoming PCI requests pass FDs */
201 if (msg->fds != NULL) {
202 vfio_user_send_error(vdev->vbasedev.proxy, hdr, EINVAL);
203 vfio_user_putfds(msg);
204 return;
205 }
206
207 switch (hdr->command) {
208 case VFIO_USER_DMA_READ:
209 vfio_user_dma_read(vdev, (VFIOUserDMARW *)hdr);
210 break;
211 case VFIO_USER_DMA_WRITE:
212 vfio_user_dma_write(vdev, (VFIOUserDMARW *)hdr);
213 break;
214 default:
215 error_printf("vfio_user_pci_process_req unknown cmd %d\n",
216 hdr->command);
217 vfio_user_send_error(vdev->vbasedev.proxy, hdr, ENOSYS);
218 }
219 }
220
221 /*
222 * Emulated devices don't use host hot reset
223 */
224 static void vfio_user_compute_needs_reset(VFIODevice *vbasedev)
225 {
226 vbasedev->needs_reset = false;
227 }
228
229 static Object *vfio_user_pci_get_object(VFIODevice *vbasedev)
230 {
231 VFIOUserPCIDevice *vdev = VFIO_USER_PCI(container_of(vbasedev,
232 VFIOPCIDevice,
233 vbasedev));
234
235 return OBJECT(vdev);
236 }
237
238 static VFIODeviceOps vfio_user_pci_ops = {
239 .vfio_compute_needs_reset = vfio_user_compute_needs_reset,
240 .vfio_eoi = vfio_pci_intx_eoi,
241 .vfio_get_object = vfio_user_pci_get_object,
242 /* No live migration support yet. */
243 .vfio_save_config = NULL,
244 .vfio_load_config = NULL,
245 };
246
247 static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)
248 {
249 ERRP_GUARD();
250 VFIOUserPCIDevice *udev = VFIO_USER_PCI(pdev);
251 VFIOPCIDevice *vdev = VFIO_PCI_DEVICE(pdev);
252 VFIODevice *vbasedev = &vdev->vbasedev;
253 const char *sock_name;
254
255 AddressSpace *as;
256 SocketAddress addr;
257 VFIOUserProxy *proxy;
258
259 if (!udev->socket) {
260 error_setg(errp, "No socket specified");
261 error_append_hint(errp, "e.g. -device '{"
262 "\"driver\":\"vfio-user-pci\", "
263 "\"socket\": {\"path\": \"/tmp/vfio-user.sock\", "
264 "\"type\": \"unix\"}'"
265 "}'\n");
266 return;
267 }
268
269 sock_name = udev->socket->u.q_unix.path;
270
271 vbasedev->name = g_strdup_printf("vfio-user:%s", sock_name);
272
273 memset(&addr, 0, sizeof(addr));
274 addr.type = SOCKET_ADDRESS_TYPE_UNIX;
275 addr.u.q_unix.path = (char *)sock_name;
276 proxy = vfio_user_connect_dev(&addr, errp);
277 if (!proxy) {
278 return;
279 }
280 vbasedev->proxy = proxy;
281 vfio_user_set_handler(vbasedev, vfio_user_pci_process_req, vdev);
282
283 if (udev->send_queued) {
284 proxy->flags |= VFIO_PROXY_FORCE_QUEUED;
285 }
286
287 if (udev->no_post) {
288 proxy->flags |= VFIO_PROXY_NO_POST;
289 }
290
291 /* user specified or 5 sec default */
292 proxy->wait_time = udev->wait_time;
293
294 if (!vfio_user_validate_version(proxy, errp)) {
295 goto error;
296 }
297
298 /*
299 * Use socket-based device I/O instead of vfio kernel driver.
300 */
301 vbasedev->io_ops = &vfio_user_device_io_ops_sock;
302
303 /*
304 * vfio-user devices are effectively mdevs (don't use a host iommu).
305 */
306 vbasedev->mdev = true;
307
308 /*
309 * Enable per-region fds.
310 */
311 vbasedev->use_region_fds = true;
312
313 as = pci_device_iommu_address_space(pdev);
314 if (!vfio_device_attach_by_iommu_type(TYPE_VFIO_IOMMU_USER,
315 vbasedev->name, vbasedev,
316 as, errp)) {
317 goto error;
318 }
319
320 if (!vfio_pci_populate_device(vdev, errp)) {
321 goto error;
322 }
323
324 if (!vfio_pci_config_setup(vdev, errp)) {
325 goto error;
326 }
327
328 /*
329 * vfio_pci_config_setup will have registered the device's BARs
330 * and setup any MSIX BARs, so errors after it succeeds must
331 * use out_teardown
332 */
333
334 if (!vfio_pci_add_capabilities(vdev, errp)) {
335 goto out_teardown;
336 }
337
338 if (vdev->msix != NULL) {
339 vfio_user_msix_setup(vdev);
340 }
341
342 if (!vfio_pci_interrupt_setup(vdev, errp)) {
343 goto out_teardown;
344 }
345
346 vfio_pci_register_err_notifier(vdev);
347 vfio_pci_register_req_notifier(vdev);
348
349 return;
350
351 out_teardown:
352 vfio_pci_teardown_msi(vdev);
353 vfio_pci_bars_exit(vdev);
354 error:
355 error_prepend(errp, VFIO_MSG_PREFIX, vdev->vbasedev.name);
356 vfio_pci_put_device(vdev);
357 }
358
359 static void vfio_user_pci_init(Object *obj)
360 {
361 PCIDevice *pci_dev = PCI_DEVICE(obj);
362 VFIOPCIDevice *vdev = VFIO_PCI_DEVICE(obj);
363 VFIODevice *vbasedev = &vdev->vbasedev;
364
365 device_add_bootindex_property(obj, &vdev->bootindex,
366 "bootindex", NULL,
367 &pci_dev->qdev);
368 vdev->host.domain = ~0U;
369 vdev->host.bus = ~0U;
370 vdev->host.slot = ~0U;
371 vdev->host.function = ~0U;
372
373 vfio_device_init(vbasedev, VFIO_DEVICE_TYPE_PCI, &vfio_user_pci_ops,
374 DEVICE(vdev), false);
375
376 vdev->nv_gpudirect_clique = 0xFF;
377
378 /*
379 * QEMU_PCI_CAP_EXPRESS initialization does not depend on QEMU command
380 * line, therefore, no need to wait to realize like other devices.
381 */
382 pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
383 }
384
385 static void vfio_user_pci_finalize(Object *obj)
386 {
387 VFIOPCIDevice *vdev = VFIO_PCI_DEVICE(obj);
388 VFIODevice *vbasedev = &vdev->vbasedev;
389
390 if (vdev->msix != NULL) {
391 vfio_user_msix_teardown(vdev);
392 }
393
394 vfio_pci_put_device(vdev);
395
396 if (vbasedev->proxy != NULL) {
397 vfio_user_disconnect(vbasedev->proxy);
398 }
399 }
400
401 static void vfio_user_pci_reset(DeviceState *dev)
402 {
403 VFIOPCIDevice *vdev = VFIO_PCI_DEVICE(dev);
404 VFIODevice *vbasedev = &vdev->vbasedev;
405
406 vfio_pci_pre_reset(vdev);
407
408 if (vbasedev->reset_works) {
409 vfio_user_device_reset(vbasedev->proxy);
410 }
411
412 vfio_pci_post_reset(vdev);
413 }
414
415 static const Property vfio_user_pci_properties[] = {
416 DEFINE_PROP_UINT32("x-pci-vendor-id", VFIOPCIDevice,
417 vendor_id, PCI_ANY_ID),
418 DEFINE_PROP_UINT32("x-pci-device-id", VFIOPCIDevice,
419 device_id, PCI_ANY_ID),
420 DEFINE_PROP_UINT32("x-pci-sub-vendor-id", VFIOPCIDevice,
421 sub_vendor_id, PCI_ANY_ID),
422 DEFINE_PROP_UINT32("x-pci-sub-device-id", VFIOPCIDevice,
423 sub_device_id, PCI_ANY_ID),
424 DEFINE_PROP_UINT32("x-pci-class-code", VFIOPCIDevice,
425 class_code, PCI_ANY_ID),
426 DEFINE_PROP_BOOL("x-send-queued", VFIOUserPCIDevice, send_queued, false),
427 DEFINE_PROP_UINT32("x-msg-timeout", VFIOUserPCIDevice, wait_time, 5000),
428 DEFINE_PROP_BOOL("x-no-posted-writes", VFIOUserPCIDevice, no_post, false),
429 };
430
431 static void vfio_user_pci_set_socket(Object *obj, Visitor *v, const char *name,
432 void *opaque, Error **errp)
433 {
434 VFIOUserPCIDevice *udev = VFIO_USER_PCI(obj);
435 bool success;
436
437 if (VFIO_PCI_DEVICE(udev)->vbasedev.proxy) {
438 error_setg(errp, "Proxy is connected");
439 return;
440 }
441
442 qapi_free_SocketAddress(udev->socket);
443
444 udev->socket = NULL;
445
446 success = visit_type_SocketAddress(v, name, &udev->socket, errp);
447
448 if (!success) {
449 return;
450 }
451
452 if (udev->socket->type != SOCKET_ADDRESS_TYPE_UNIX) {
453 error_setg(errp, "Unsupported socket type %s",
454 SocketAddressType_str(udev->socket->type));
455 qapi_free_SocketAddress(udev->socket);
456 udev->socket = NULL;
457 return;
458 }
459 }
460
461 static void vfio_user_pci_class_init(ObjectClass *klass, const void *data)
462 {
463 DeviceClass *dc = DEVICE_CLASS(klass);
464 PCIDeviceClass *pdc = PCI_DEVICE_CLASS(klass);
465
466 device_class_set_legacy_reset(dc, vfio_user_pci_reset);
467 device_class_set_props(dc, vfio_user_pci_properties);
468
469 object_class_property_add(klass, "socket", "SocketAddress", NULL,
470 vfio_user_pci_set_socket, NULL, NULL);
471 object_class_property_set_description(klass, "socket",
472 "SocketAddress (UNIX sockets only)");
473
474 dc->desc = "VFIO over socket PCI device assignment";
475 pdc->realize = vfio_user_pci_realize;
476 }
477
478 static const TypeInfo vfio_user_pci_info = {
479 .name = TYPE_VFIO_USER_PCI,
480 .parent = TYPE_VFIO_PCI_DEVICE,
481 .instance_size = sizeof(VFIOUserPCIDevice),
482 .class_init = vfio_user_pci_class_init,
483 .instance_init = vfio_user_pci_init,
484 .instance_finalize = vfio_user_pci_finalize,
485 };
486
487 static void register_vfio_user_dev_type(void)
488 {
489 type_register_static(&vfio_user_pci_info);
490 }
491
492 type_init(register_vfio_user_dev_type)