xref: /qemu/hw/vfio-user/pci.c (revision 36227628d824f563fda95f9344176ca7263c7eaf)
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 <sys/ioctl.h>
10 #include "qemu/osdep.h"
11 #include "qapi-visit-sockets.h"
12 
13 #include "hw/qdev-properties.h"
14 #include "hw/vfio/pci.h"
15 #include "hw/vfio-user/proxy.h"
16 
17 #define TYPE_VFIO_USER_PCI "vfio-user-pci"
18 OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserPCIDevice, VFIO_USER_PCI)
19 
20 struct VFIOUserPCIDevice {
21     VFIOPCIDevice device;
22     SocketAddress *socket;
23     bool send_queued;   /* all sends are queued */
24 };
25 
26 /*
27  * Incoming request message callback.
28  *
29  * Runs off main loop, so BQL held.
30  */
31 static void vfio_user_pci_process_req(void *opaque, VFIOUserMsg *msg)
32 {
33 
34 }
35 
36 /*
37  * Emulated devices don't use host hot reset
38  */
39 static void vfio_user_compute_needs_reset(VFIODevice *vbasedev)
40 {
41     vbasedev->needs_reset = false;
42 }
43 
44 static Object *vfio_user_pci_get_object(VFIODevice *vbasedev)
45 {
46     VFIOUserPCIDevice *vdev = container_of(vbasedev, VFIOUserPCIDevice,
47                                            device.vbasedev);
48 
49     return OBJECT(vdev);
50 }
51 
52 static VFIODeviceOps vfio_user_pci_ops = {
53     .vfio_compute_needs_reset = vfio_user_compute_needs_reset,
54     .vfio_eoi = vfio_pci_intx_eoi,
55     .vfio_get_object = vfio_user_pci_get_object,
56     /* No live migration support yet. */
57     .vfio_save_config = NULL,
58     .vfio_load_config = NULL,
59 };
60 
61 static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)
62 {
63     ERRP_GUARD();
64     VFIOUserPCIDevice *udev = VFIO_USER_PCI(pdev);
65     VFIOPCIDevice *vdev = VFIO_PCI_BASE(pdev);
66     VFIODevice *vbasedev = &vdev->vbasedev;
67     const char *sock_name;
68     AddressSpace *as;
69     SocketAddress addr;
70     VFIOUserProxy *proxy;
71 
72     if (!udev->socket) {
73         error_setg(errp, "No socket specified");
74         error_append_hint(errp, "e.g. -device '{"
75             "\"driver\":\"vfio-user-pci\", "
76             "\"socket\": {\"path\": \"/tmp/vfio-user.sock\", "
77             "\"type\": \"unix\"}'"
78             "}'\n");
79         return;
80     }
81 
82     sock_name = udev->socket->u.q_unix.path;
83 
84     vbasedev->name = g_strdup_printf("vfio-user:%s", sock_name);
85 
86     memset(&addr, 0, sizeof(addr));
87     addr.type = SOCKET_ADDRESS_TYPE_UNIX;
88     addr.u.q_unix.path = (char *)sock_name;
89     proxy = vfio_user_connect_dev(&addr, errp);
90     if (!proxy) {
91         return;
92     }
93     vbasedev->proxy = proxy;
94     vfio_user_set_handler(vbasedev, vfio_user_pci_process_req, vdev);
95 
96     vbasedev->name = g_strdup_printf("vfio-user:%s", sock_name);
97 
98     if (udev->send_queued) {
99         proxy->flags |= VFIO_PROXY_FORCE_QUEUED;
100     }
101 
102     if (!vfio_user_validate_version(proxy, errp)) {
103         goto error;
104     }
105 
106     /*
107      * vfio-user devices are effectively mdevs (don't use a host iommu).
108      */
109     vbasedev->mdev = true;
110 
111     as = pci_device_iommu_address_space(pdev);
112     if (!vfio_device_attach_by_iommu_type(TYPE_VFIO_IOMMU_USER,
113                                           vbasedev->name, vbasedev,
114                                           as, errp)) {
115         goto error;
116     }
117 
118     return;
119 
120 error:
121     error_prepend(errp, VFIO_MSG_PREFIX, vdev->vbasedev.name);
122 }
123 
124 static void vfio_user_instance_init(Object *obj)
125 {
126     PCIDevice *pci_dev = PCI_DEVICE(obj);
127     VFIOPCIDevice *vdev = VFIO_PCI_BASE(obj);
128     VFIODevice *vbasedev = &vdev->vbasedev;
129 
130     device_add_bootindex_property(obj, &vdev->bootindex,
131                                   "bootindex", NULL,
132                                   &pci_dev->qdev);
133     vdev->host.domain = ~0U;
134     vdev->host.bus = ~0U;
135     vdev->host.slot = ~0U;
136     vdev->host.function = ~0U;
137 
138     vfio_device_init(vbasedev, VFIO_DEVICE_TYPE_PCI, &vfio_user_pci_ops,
139                      DEVICE(vdev), false);
140 
141     vdev->nv_gpudirect_clique = 0xFF;
142 
143     /*
144      * QEMU_PCI_CAP_EXPRESS initialization does not depend on QEMU command
145      * line, therefore, no need to wait to realize like other devices.
146      */
147     pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
148 }
149 
150 static void vfio_user_instance_finalize(Object *obj)
151 {
152     VFIOPCIDevice *vdev = VFIO_PCI_BASE(obj);
153     VFIODevice *vbasedev = &vdev->vbasedev;
154 
155     vfio_pci_put_device(vdev);
156 
157     if (vbasedev->proxy != NULL) {
158         vfio_user_disconnect(vbasedev->proxy);
159     }
160 }
161 
162 static const Property vfio_user_pci_dev_properties[] = {
163     DEFINE_PROP_UINT32("x-pci-vendor-id", VFIOPCIDevice,
164                        vendor_id, PCI_ANY_ID),
165     DEFINE_PROP_UINT32("x-pci-device-id", VFIOPCIDevice,
166                        device_id, PCI_ANY_ID),
167     DEFINE_PROP_UINT32("x-pci-sub-vendor-id", VFIOPCIDevice,
168                        sub_vendor_id, PCI_ANY_ID),
169     DEFINE_PROP_UINT32("x-pci-sub-device-id", VFIOPCIDevice,
170                        sub_device_id, PCI_ANY_ID),
171     DEFINE_PROP_BOOL("x-send-queued", VFIOUserPCIDevice, send_queued, false),
172 };
173 
174 static void vfio_user_pci_set_socket(Object *obj, Visitor *v, const char *name,
175                                      void *opaque, Error **errp)
176 {
177     VFIOUserPCIDevice *udev = VFIO_USER_PCI(obj);
178     bool success;
179 
180     if (udev->device.vbasedev.proxy) {
181         error_setg(errp, "Proxy is connected");
182         return;
183     }
184 
185     qapi_free_SocketAddress(udev->socket);
186 
187     udev->socket = NULL;
188 
189     success = visit_type_SocketAddress(v, name, &udev->socket, errp);
190 
191     if (!success) {
192         return;
193     }
194 
195     if (udev->socket->type != SOCKET_ADDRESS_TYPE_UNIX) {
196         error_setg(errp, "Unsupported socket type %s",
197                    SocketAddressType_str(udev->socket->type));
198         qapi_free_SocketAddress(udev->socket);
199         udev->socket = NULL;
200         return;
201     }
202 }
203 
204 static void vfio_user_pci_dev_class_init(ObjectClass *klass, const void *data)
205 {
206     DeviceClass *dc = DEVICE_CLASS(klass);
207     PCIDeviceClass *pdc = PCI_DEVICE_CLASS(klass);
208 
209     device_class_set_props(dc, vfio_user_pci_dev_properties);
210 
211     object_class_property_add(klass, "socket", "SocketAddress", NULL,
212                               vfio_user_pci_set_socket, NULL, NULL);
213     object_class_property_set_description(klass, "socket",
214                                           "SocketAddress (UNIX sockets only)");
215 
216     dc->desc = "VFIO over socket PCI device assignment";
217     pdc->realize = vfio_user_pci_realize;
218 }
219 
220 static const TypeInfo vfio_user_pci_dev_info = {
221     .name = TYPE_VFIO_USER_PCI,
222     .parent = TYPE_VFIO_PCI_BASE,
223     .instance_size = sizeof(VFIOUserPCIDevice),
224     .class_init = vfio_user_pci_dev_class_init,
225     .instance_init = vfio_user_instance_init,
226     .instance_finalize = vfio_user_instance_finalize,
227 };
228 
229 static void register_vfio_user_dev_type(void)
230 {
231     type_register_static(&vfio_user_pci_dev_info);
232 }
233 
234  type_init(register_vfio_user_dev_type)
235