]>
Commit | Line | Data |
---|---|---|
0ea2730b EA |
1 | /* |
2 | * vfio based device assignment support - platform devices | |
3 | * | |
4 | * Copyright Linaro Limited, 2014 | |
5 | * | |
6 | * Authors: | |
7 | * Kim Phillips <[email protected]> | |
8 | * Eric Auger <[email protected]> | |
9 | * | |
10 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
11 | * the COPYING file in the top-level directory. | |
12 | * | |
13 | * Based on vfio based PCI device assignment support: | |
14 | * Copyright Red Hat, Inc. 2012 | |
15 | */ | |
16 | ||
c6eacb1a | 17 | #include "qemu/osdep.h" |
da34e65c | 18 | #include "qapi/error.h" |
0ea2730b | 19 | #include <sys/ioctl.h> |
e2075277 | 20 | #include <linux/vfio.h> |
0ea2730b EA |
21 | |
22 | #include "hw/vfio/vfio-platform.h" | |
d6454270 | 23 | #include "migration/vmstate.h" |
0ea2730b | 24 | #include "qemu/error-report.h" |
db725815 | 25 | #include "qemu/main-loop.h" |
0b8fa32f | 26 | #include "qemu/module.h" |
0ea2730b EA |
27 | #include "qemu/range.h" |
28 | #include "sysemu/sysemu.h" | |
29 | #include "exec/memory.h" | |
d791937f | 30 | #include "exec/address-spaces.h" |
38559979 | 31 | #include "qemu/queue.h" |
0ea2730b EA |
32 | #include "hw/sysbus.h" |
33 | #include "trace.h" | |
64552b6b | 34 | #include "hw/irq.h" |
0ea2730b | 35 | #include "hw/platform-bus.h" |
fb5f8164 | 36 | #include "sysemu/kvm.h" |
0ea2730b | 37 | |
38559979 EA |
38 | /* |
39 | * Functions used whatever the injection method | |
40 | */ | |
41 | ||
a5b39cd3 EA |
42 | static inline bool vfio_irq_is_automasked(VFIOINTp *intp) |
43 | { | |
44 | return intp->flags & VFIO_IRQ_INFO_AUTOMASKED; | |
45 | } | |
46 | ||
38559979 EA |
47 | /** |
48 | * vfio_init_intp - allocate, initialize the IRQ struct pointer | |
49 | * and add it into the list of IRQs | |
50 | * @vbasedev: the VFIO device handle | |
51 | * @info: irq info struct retrieved from VFIO driver | |
5ff7419d | 52 | * @errp: error object |
38559979 EA |
53 | */ |
54 | static VFIOINTp *vfio_init_intp(VFIODevice *vbasedev, | |
5ff7419d | 55 | struct vfio_irq_info info, Error **errp) |
38559979 EA |
56 | { |
57 | int ret; | |
58 | VFIOPlatformDevice *vdev = | |
59 | container_of(vbasedev, VFIOPlatformDevice, vbasedev); | |
60 | SysBusDevice *sbdev = SYS_BUS_DEVICE(vdev); | |
61 | VFIOINTp *intp; | |
62 | ||
63 | intp = g_malloc0(sizeof(*intp)); | |
64 | intp->vdev = vdev; | |
65 | intp->pin = info.index; | |
66 | intp->flags = info.flags; | |
67 | intp->state = VFIO_IRQ_INACTIVE; | |
fb5f8164 | 68 | intp->kvm_accel = false; |
38559979 EA |
69 | |
70 | sysbus_init_irq(sbdev, &intp->qemuirq); | |
71 | ||
72 | /* Get an eventfd for trigger */ | |
a22313de EA |
73 | intp->interrupt = g_malloc0(sizeof(EventNotifier)); |
74 | ret = event_notifier_init(intp->interrupt, 0); | |
38559979 | 75 | if (ret) { |
a22313de | 76 | g_free(intp->interrupt); |
38559979 | 77 | g_free(intp); |
5ff7419d | 78 | error_setg_errno(errp, -ret, |
bf04ef35 | 79 | "failed to initialize trigger eventfd notifier"); |
38559979 EA |
80 | return NULL; |
81 | } | |
a5b39cd3 EA |
82 | if (vfio_irq_is_automasked(intp)) { |
83 | /* Get an eventfd for resample/unmask */ | |
84 | intp->unmask = g_malloc0(sizeof(EventNotifier)); | |
85 | ret = event_notifier_init(intp->unmask, 0); | |
86 | if (ret) { | |
87 | g_free(intp->interrupt); | |
88 | g_free(intp->unmask); | |
89 | g_free(intp); | |
5ff7419d | 90 | error_setg_errno(errp, -ret, |
bf04ef35 | 91 | "failed to initialize resample eventfd notifier"); |
a5b39cd3 EA |
92 | return NULL; |
93 | } | |
fb5f8164 | 94 | } |
38559979 EA |
95 | |
96 | QLIST_INSERT_HEAD(&vdev->intp_list, intp, next); | |
97 | return intp; | |
98 | } | |
99 | ||
100 | /** | |
101 | * vfio_set_trigger_eventfd - set VFIO eventfd handling | |
102 | * | |
103 | * @intp: IRQ struct handle | |
104 | * @handler: handler to be called on eventfd signaling | |
105 | * | |
106 | * Setup VFIO signaling and attach an optional user-side handler | |
107 | * to the eventfd | |
108 | */ | |
109 | static int vfio_set_trigger_eventfd(VFIOINTp *intp, | |
110 | eventfd_user_side_handler_t handler) | |
111 | { | |
112 | VFIODevice *vbasedev = &intp->vdev->vbasedev; | |
201a7331 EA |
113 | int32_t fd = event_notifier_get_fd(intp->interrupt); |
114 | Error *err = NULL; | |
115 | int ret; | |
116 | ||
117 | qemu_set_fd_handler(fd, (IOHandler *)handler, NULL, intp); | |
118 | ||
119 | ret = vfio_set_irq_signaling(vbasedev, intp->pin, 0, | |
120 | VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err); | |
121 | if (ret) { | |
122 | error_reportf_err(err, VFIO_MSG_PREFIX, vbasedev->name); | |
123 | qemu_set_fd_handler(fd, NULL, NULL, NULL); | |
38559979 | 124 | } |
201a7331 | 125 | |
38559979 EA |
126 | return ret; |
127 | } | |
128 | ||
129 | /* | |
130 | * Functions only used when eventfds are handled on user-side | |
131 | * ie. without irqfd | |
132 | */ | |
133 | ||
134 | /** | |
135 | * vfio_mmap_set_enabled - enable/disable the fast path mode | |
136 | * @vdev: the VFIO platform device | |
137 | * @enabled: the target mmap state | |
138 | * | |
139 | * enabled = true ~ fast path = MMIO region is mmaped (no KVM TRAP); | |
140 | * enabled = false ~ slow path = MMIO region is trapped and region callbacks | |
141 | * are called; slow path enables to trap the device IRQ status register reset | |
142 | */ | |
143 | ||
144 | static void vfio_mmap_set_enabled(VFIOPlatformDevice *vdev, bool enabled) | |
145 | { | |
146 | int i; | |
147 | ||
38559979 | 148 | for (i = 0; i < vdev->vbasedev.num_regions; i++) { |
db0da029 | 149 | vfio_region_mmaps_set_enabled(vdev->regions[i], enabled); |
38559979 EA |
150 | } |
151 | } | |
152 | ||
153 | /** | |
154 | * vfio_intp_mmap_enable - timer function, restores the fast path | |
155 | * if there is no more active IRQ | |
156 | * @opaque: actually points to the VFIO platform device | |
157 | * | |
158 | * Called on mmap timer timout, this function checks whether the | |
159 | * IRQ is still active and if not, restores the fast path. | |
160 | * by construction a single eventfd is handled at a time. | |
161 | * if the IRQ is still active, the timer is re-programmed. | |
162 | */ | |
163 | static void vfio_intp_mmap_enable(void *opaque) | |
164 | { | |
165 | VFIOINTp *tmp; | |
166 | VFIOPlatformDevice *vdev = (VFIOPlatformDevice *)opaque; | |
167 | ||
168 | qemu_mutex_lock(&vdev->intp_mutex); | |
169 | QLIST_FOREACH(tmp, &vdev->intp_list, next) { | |
170 | if (tmp->state == VFIO_IRQ_ACTIVE) { | |
171 | trace_vfio_platform_intp_mmap_enable(tmp->pin); | |
172 | /* re-program the timer to check active status later */ | |
173 | timer_mod(vdev->mmap_timer, | |
174 | qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + | |
175 | vdev->mmap_timeout); | |
176 | qemu_mutex_unlock(&vdev->intp_mutex); | |
177 | return; | |
178 | } | |
179 | } | |
180 | vfio_mmap_set_enabled(vdev, true); | |
181 | qemu_mutex_unlock(&vdev->intp_mutex); | |
182 | } | |
183 | ||
184 | /** | |
185 | * vfio_intp_inject_pending_lockheld - Injects a pending IRQ | |
186 | * @opaque: opaque pointer, in practice the VFIOINTp handle | |
187 | * | |
188 | * The function is called on a previous IRQ completion, from | |
189 | * vfio_platform_eoi, while the intp_mutex is locked. | |
190 | * Also in such situation, the slow path already is set and | |
191 | * the mmap timer was already programmed. | |
192 | */ | |
193 | static void vfio_intp_inject_pending_lockheld(VFIOINTp *intp) | |
194 | { | |
195 | trace_vfio_platform_intp_inject_pending_lockheld(intp->pin, | |
a22313de | 196 | event_notifier_get_fd(intp->interrupt)); |
38559979 EA |
197 | |
198 | intp->state = VFIO_IRQ_ACTIVE; | |
199 | ||
200 | /* trigger the virtual IRQ */ | |
201 | qemu_set_irq(intp->qemuirq, 1); | |
202 | } | |
203 | ||
204 | /** | |
205 | * vfio_intp_interrupt - The user-side eventfd handler | |
206 | * @opaque: opaque pointer which in practice is the VFIOINTp handle | |
207 | * | |
208 | * the function is entered in event handler context: | |
209 | * the vIRQ is injected into the guest if there is no other active | |
210 | * or pending IRQ. | |
211 | */ | |
212 | static void vfio_intp_interrupt(VFIOINTp *intp) | |
213 | { | |
214 | int ret; | |
215 | VFIOINTp *tmp; | |
216 | VFIOPlatformDevice *vdev = intp->vdev; | |
217 | bool delay_handling = false; | |
218 | ||
219 | qemu_mutex_lock(&vdev->intp_mutex); | |
220 | if (intp->state == VFIO_IRQ_INACTIVE) { | |
221 | QLIST_FOREACH(tmp, &vdev->intp_list, next) { | |
222 | if (tmp->state == VFIO_IRQ_ACTIVE || | |
223 | tmp->state == VFIO_IRQ_PENDING) { | |
224 | delay_handling = true; | |
225 | break; | |
226 | } | |
227 | } | |
228 | } | |
229 | if (delay_handling) { | |
230 | /* | |
231 | * the new IRQ gets a pending status and is pushed in | |
232 | * the pending queue | |
233 | */ | |
234 | intp->state = VFIO_IRQ_PENDING; | |
235 | trace_vfio_intp_interrupt_set_pending(intp->pin); | |
236 | QSIMPLEQ_INSERT_TAIL(&vdev->pending_intp_queue, | |
237 | intp, pqnext); | |
a22313de | 238 | ret = event_notifier_test_and_clear(intp->interrupt); |
38559979 EA |
239 | qemu_mutex_unlock(&vdev->intp_mutex); |
240 | return; | |
241 | } | |
242 | ||
243 | trace_vfio_platform_intp_interrupt(intp->pin, | |
a22313de | 244 | event_notifier_get_fd(intp->interrupt)); |
38559979 | 245 | |
a22313de | 246 | ret = event_notifier_test_and_clear(intp->interrupt); |
38559979 | 247 | if (!ret) { |
594fd211 | 248 | error_report("Error when clearing fd=%d (ret = %d)", |
a22313de | 249 | event_notifier_get_fd(intp->interrupt), ret); |
38559979 EA |
250 | } |
251 | ||
252 | intp->state = VFIO_IRQ_ACTIVE; | |
253 | ||
254 | /* sets slow path */ | |
255 | vfio_mmap_set_enabled(vdev, false); | |
256 | ||
257 | /* trigger the virtual IRQ */ | |
258 | qemu_set_irq(intp->qemuirq, 1); | |
259 | ||
260 | /* | |
261 | * Schedule the mmap timer which will restore fastpath when no IRQ | |
262 | * is active anymore | |
263 | */ | |
264 | if (vdev->mmap_timeout) { | |
265 | timer_mod(vdev->mmap_timer, | |
266 | qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + | |
267 | vdev->mmap_timeout); | |
268 | } | |
269 | qemu_mutex_unlock(&vdev->intp_mutex); | |
270 | } | |
271 | ||
272 | /** | |
273 | * vfio_platform_eoi - IRQ completion routine | |
274 | * @vbasedev: the VFIO device handle | |
275 | * | |
276 | * De-asserts the active virtual IRQ and unmasks the physical IRQ | |
277 | * (effective for level sensitive IRQ auto-masked by the VFIO driver). | |
278 | * Then it handles next pending IRQ if any. | |
279 | * eoi function is called on the first access to any MMIO region | |
280 | * after an IRQ was triggered, trapped since slow path was set. | |
281 | * It is assumed this access corresponds to the IRQ status | |
282 | * register reset. With such a mechanism, a single IRQ can be | |
283 | * handled at a time since there is no way to know which IRQ | |
284 | * was completed by the guest (we would need additional details | |
285 | * about the IRQ status register mask). | |
286 | */ | |
287 | static void vfio_platform_eoi(VFIODevice *vbasedev) | |
288 | { | |
289 | VFIOINTp *intp; | |
290 | VFIOPlatformDevice *vdev = | |
291 | container_of(vbasedev, VFIOPlatformDevice, vbasedev); | |
292 | ||
293 | qemu_mutex_lock(&vdev->intp_mutex); | |
294 | QLIST_FOREACH(intp, &vdev->intp_list, next) { | |
295 | if (intp->state == VFIO_IRQ_ACTIVE) { | |
296 | trace_vfio_platform_eoi(intp->pin, | |
a22313de | 297 | event_notifier_get_fd(intp->interrupt)); |
38559979 EA |
298 | intp->state = VFIO_IRQ_INACTIVE; |
299 | ||
300 | /* deassert the virtual IRQ */ | |
301 | qemu_set_irq(intp->qemuirq, 0); | |
302 | ||
a5b39cd3 | 303 | if (vfio_irq_is_automasked(intp)) { |
38559979 EA |
304 | /* unmasks the physical level-sensitive IRQ */ |
305 | vfio_unmask_single_irqindex(vbasedev, intp->pin); | |
306 | } | |
307 | ||
308 | /* a single IRQ can be active at a time */ | |
309 | break; | |
310 | } | |
311 | } | |
312 | /* in case there are pending IRQs, handle the first one */ | |
313 | if (!QSIMPLEQ_EMPTY(&vdev->pending_intp_queue)) { | |
314 | intp = QSIMPLEQ_FIRST(&vdev->pending_intp_queue); | |
315 | vfio_intp_inject_pending_lockheld(intp); | |
316 | QSIMPLEQ_REMOVE_HEAD(&vdev->pending_intp_queue, pqnext); | |
317 | } | |
318 | qemu_mutex_unlock(&vdev->intp_mutex); | |
319 | } | |
320 | ||
321 | /** | |
322 | * vfio_start_eventfd_injection - starts the virtual IRQ injection using | |
323 | * user-side handled eventfds | |
58892b44 EA |
324 | * @sbdev: the sysbus device handle |
325 | * @irq: the qemu irq handle | |
38559979 EA |
326 | */ |
327 | ||
58892b44 | 328 | static void vfio_start_eventfd_injection(SysBusDevice *sbdev, qemu_irq irq) |
38559979 | 329 | { |
58892b44 EA |
330 | VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev); |
331 | VFIOINTp *intp; | |
332 | ||
333 | QLIST_FOREACH(intp, &vdev->intp_list, next) { | |
334 | if (intp->qemuirq == irq) { | |
335 | break; | |
336 | } | |
337 | } | |
338 | assert(intp); | |
38559979 | 339 | |
201a7331 | 340 | if (vfio_set_trigger_eventfd(intp, vfio_intp_interrupt)) { |
58892b44 | 341 | abort(); |
38559979 | 342 | } |
38559979 EA |
343 | } |
344 | ||
fb5f8164 EA |
345 | /* |
346 | * Functions used for irqfd | |
347 | */ | |
348 | ||
349 | /** | |
350 | * vfio_set_resample_eventfd - sets the resamplefd for an IRQ | |
351 | * @intp: the IRQ struct handle | |
352 | * programs the VFIO driver to unmask this IRQ when the | |
353 | * intp->unmask eventfd is triggered | |
354 | */ | |
355 | static int vfio_set_resample_eventfd(VFIOINTp *intp) | |
356 | { | |
201a7331 | 357 | int32_t fd = event_notifier_get_fd(intp->unmask); |
fb5f8164 | 358 | VFIODevice *vbasedev = &intp->vdev->vbasedev; |
201a7331 EA |
359 | Error *err = NULL; |
360 | int ret; | |
361 | ||
362 | qemu_set_fd_handler(fd, NULL, NULL, NULL); | |
363 | ret = vfio_set_irq_signaling(vbasedev, intp->pin, 0, | |
364 | VFIO_IRQ_SET_ACTION_UNMASK, fd, &err); | |
365 | if (ret) { | |
366 | error_reportf_err(err, VFIO_MSG_PREFIX, vbasedev->name); | |
fb5f8164 EA |
367 | } |
368 | return ret; | |
369 | } | |
370 | ||
58892b44 EA |
371 | /** |
372 | * vfio_start_irqfd_injection - starts the virtual IRQ injection using | |
373 | * irqfd | |
374 | * | |
375 | * @sbdev: the sysbus device handle | |
376 | * @irq: the qemu irq handle | |
377 | * | |
378 | * In case the irqfd setup fails, we fallback to userspace handled eventfd | |
379 | */ | |
fb5f8164 EA |
380 | static void vfio_start_irqfd_injection(SysBusDevice *sbdev, qemu_irq irq) |
381 | { | |
382 | VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev); | |
383 | VFIOINTp *intp; | |
384 | ||
385 | if (!kvm_irqfds_enabled() || !kvm_resamplefds_enabled() || | |
386 | !vdev->irqfd_allowed) { | |
58892b44 | 387 | goto fail_irqfd; |
fb5f8164 EA |
388 | } |
389 | ||
390 | QLIST_FOREACH(intp, &vdev->intp_list, next) { | |
391 | if (intp->qemuirq == irq) { | |
392 | break; | |
393 | } | |
394 | } | |
395 | assert(intp); | |
396 | ||
a22313de EA |
397 | if (kvm_irqchip_add_irqfd_notifier(kvm_state, intp->interrupt, |
398 | intp->unmask, irq) < 0) { | |
fb5f8164 EA |
399 | goto fail_irqfd; |
400 | } | |
401 | ||
402 | if (vfio_set_trigger_eventfd(intp, NULL) < 0) { | |
403 | goto fail_vfio; | |
404 | } | |
a5b39cd3 EA |
405 | if (vfio_irq_is_automasked(intp)) { |
406 | if (vfio_set_resample_eventfd(intp) < 0) { | |
407 | goto fail_vfio; | |
408 | } | |
409 | trace_vfio_platform_start_level_irqfd_injection(intp->pin, | |
410 | event_notifier_get_fd(intp->interrupt), | |
411 | event_notifier_get_fd(intp->unmask)); | |
412 | } else { | |
413 | trace_vfio_platform_start_edge_irqfd_injection(intp->pin, | |
414 | event_notifier_get_fd(intp->interrupt)); | |
fb5f8164 EA |
415 | } |
416 | ||
fb5f8164 EA |
417 | intp->kvm_accel = true; |
418 | ||
fb5f8164 EA |
419 | return; |
420 | fail_vfio: | |
a22313de | 421 | kvm_irqchip_remove_irqfd_notifier(kvm_state, intp->interrupt, irq); |
58892b44 | 422 | abort(); |
fb5f8164 | 423 | fail_irqfd: |
58892b44 | 424 | vfio_start_eventfd_injection(sbdev, irq); |
fb5f8164 EA |
425 | return; |
426 | } | |
427 | ||
0ea2730b EA |
428 | /* VFIO skeleton */ |
429 | ||
430 | static void vfio_platform_compute_needs_reset(VFIODevice *vbasedev) | |
431 | { | |
432 | vbasedev->needs_reset = true; | |
433 | } | |
434 | ||
435 | /* not implemented yet */ | |
436 | static int vfio_platform_hot_reset_multi(VFIODevice *vbasedev) | |
437 | { | |
438 | return -1; | |
439 | } | |
440 | ||
441 | /** | |
442 | * vfio_populate_device - Allocate and populate MMIO region | |
38559979 | 443 | * and IRQ structs according to driver returned information |
0ea2730b | 444 | * @vbasedev: the VFIO device handle |
5ff7419d | 445 | * @errp: error object |
0ea2730b EA |
446 | * |
447 | */ | |
5ff7419d | 448 | static int vfio_populate_device(VFIODevice *vbasedev, Error **errp) |
0ea2730b | 449 | { |
38559979 | 450 | VFIOINTp *intp, *tmp; |
0ea2730b EA |
451 | int i, ret = -1; |
452 | VFIOPlatformDevice *vdev = | |
453 | container_of(vbasedev, VFIOPlatformDevice, vbasedev); | |
454 | ||
455 | if (!(vbasedev->flags & VFIO_DEVICE_FLAGS_PLATFORM)) { | |
5ff7419d | 456 | error_setg(errp, "this isn't a platform device"); |
0ea2730b EA |
457 | return ret; |
458 | } | |
459 | ||
0b70743d | 460 | vdev->regions = g_new0(VFIORegion *, vbasedev->num_regions); |
0ea2730b EA |
461 | |
462 | for (i = 0; i < vbasedev->num_regions; i++) { | |
db0da029 | 463 | char *name = g_strdup_printf("VFIO %s region %d\n", vbasedev->name, i); |
0ea2730b | 464 | |
bdd81add | 465 | vdev->regions[i] = g_new0(VFIORegion, 1); |
db0da029 AW |
466 | ret = vfio_region_setup(OBJECT(vdev), vbasedev, |
467 | vdev->regions[i], i, name); | |
468 | g_free(name); | |
0ea2730b | 469 | if (ret) { |
5ff7419d | 470 | error_setg_errno(errp, -ret, "failed to get region %d info", i); |
0ea2730b EA |
471 | goto reg_error; |
472 | } | |
0ea2730b EA |
473 | } |
474 | ||
38559979 EA |
475 | vdev->mmap_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, |
476 | vfio_intp_mmap_enable, vdev); | |
477 | ||
478 | QSIMPLEQ_INIT(&vdev->pending_intp_queue); | |
479 | ||
480 | for (i = 0; i < vbasedev->num_irqs; i++) { | |
481 | struct vfio_irq_info irq = { .argsz = sizeof(irq) }; | |
482 | ||
483 | irq.index = i; | |
484 | ret = ioctl(vbasedev->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq); | |
485 | if (ret) { | |
5ff7419d | 486 | error_setg_errno(errp, -ret, "failed to get device irq info"); |
38559979 EA |
487 | goto irq_err; |
488 | } else { | |
489 | trace_vfio_platform_populate_interrupts(irq.index, | |
490 | irq.count, | |
491 | irq.flags); | |
5ff7419d | 492 | intp = vfio_init_intp(vbasedev, irq, errp); |
38559979 | 493 | if (!intp) { |
0d84f47b | 494 | ret = -1; |
38559979 EA |
495 | goto irq_err; |
496 | } | |
497 | } | |
498 | } | |
0ea2730b | 499 | return 0; |
38559979 EA |
500 | irq_err: |
501 | timer_del(vdev->mmap_timer); | |
502 | QLIST_FOREACH_SAFE(intp, &vdev->intp_list, next, tmp) { | |
503 | QLIST_REMOVE(intp, next); | |
504 | g_free(intp); | |
505 | } | |
0ea2730b EA |
506 | reg_error: |
507 | for (i = 0; i < vbasedev->num_regions; i++) { | |
db0da029 AW |
508 | if (vdev->regions[i]) { |
509 | vfio_region_finalize(vdev->regions[i]); | |
510 | } | |
0ea2730b EA |
511 | g_free(vdev->regions[i]); |
512 | } | |
513 | g_free(vdev->regions); | |
514 | return ret; | |
515 | } | |
516 | ||
517 | /* specialized functions for VFIO Platform devices */ | |
518 | static VFIODeviceOps vfio_platform_ops = { | |
519 | .vfio_compute_needs_reset = vfio_platform_compute_needs_reset, | |
520 | .vfio_hot_reset_multi = vfio_platform_hot_reset_multi, | |
38559979 | 521 | .vfio_eoi = vfio_platform_eoi, |
0ea2730b EA |
522 | }; |
523 | ||
524 | /** | |
525 | * vfio_base_device_init - perform preliminary VFIO setup | |
526 | * @vbasedev: the VFIO device handle | |
9bdbfbd5 | 527 | * @errp: error object |
0ea2730b EA |
528 | * |
529 | * Implement the VFIO command sequence that allows to discover | |
530 | * assigned device resources: group extraction, device | |
531 | * fd retrieval, resource query. | |
532 | * Precondition: the device name must be initialized | |
533 | */ | |
9bdbfbd5 | 534 | static int vfio_base_device_init(VFIODevice *vbasedev, Error **errp) |
0ea2730b EA |
535 | { |
536 | VFIOGroup *group; | |
537 | VFIODevice *vbasedev_iter; | |
7df9381b | 538 | char *tmp, group_path[PATH_MAX], *group_name; |
0ea2730b EA |
539 | ssize_t len; |
540 | struct stat st; | |
541 | int groupid; | |
542 | int ret; | |
543 | ||
7df9381b AW |
544 | /* @sysfsdev takes precedence over @host */ |
545 | if (vbasedev->sysfsdev) { | |
546 | g_free(vbasedev->name); | |
3e015d81 | 547 | vbasedev->name = g_path_get_basename(vbasedev->sysfsdev); |
7df9381b AW |
548 | } else { |
549 | if (!vbasedev->name || strchr(vbasedev->name, '/')) { | |
9bdbfbd5 | 550 | error_setg(errp, "wrong host device name"); |
7df9381b AW |
551 | return -EINVAL; |
552 | } | |
0ea2730b | 553 | |
7df9381b AW |
554 | vbasedev->sysfsdev = g_strdup_printf("/sys/bus/platform/devices/%s", |
555 | vbasedev->name); | |
556 | } | |
0ea2730b | 557 | |
7df9381b | 558 | if (stat(vbasedev->sysfsdev, &st) < 0) { |
9bdbfbd5 EA |
559 | error_setg_errno(errp, errno, |
560 | "failed to get the sysfs host device file status"); | |
0ea2730b EA |
561 | return -errno; |
562 | } | |
563 | ||
7df9381b AW |
564 | tmp = g_strdup_printf("%s/iommu_group", vbasedev->sysfsdev); |
565 | len = readlink(tmp, group_path, sizeof(group_path)); | |
566 | g_free(tmp); | |
567 | ||
568 | if (len < 0 || len >= sizeof(group_path)) { | |
9bdbfbd5 EA |
569 | ret = len < 0 ? -errno : -ENAMETOOLONG; |
570 | error_setg_errno(errp, -ret, "no iommu_group found"); | |
571 | return ret; | |
0ea2730b EA |
572 | } |
573 | ||
7df9381b | 574 | group_path[len] = 0; |
0ea2730b | 575 | |
7df9381b | 576 | group_name = basename(group_path); |
0ea2730b | 577 | if (sscanf(group_name, "%d", &groupid) != 1) { |
9bdbfbd5 | 578 | error_setg_errno(errp, errno, "failed to read %s", group_path); |
0ea2730b EA |
579 | return -errno; |
580 | } | |
581 | ||
582 | trace_vfio_platform_base_device_init(vbasedev->name, groupid); | |
583 | ||
9bdbfbd5 | 584 | group = vfio_get_group(groupid, &address_space_memory, errp); |
0ea2730b | 585 | if (!group) { |
9bdbfbd5 | 586 | return -ENOENT; |
0ea2730b EA |
587 | } |
588 | ||
0ea2730b EA |
589 | QLIST_FOREACH(vbasedev_iter, &group->device_list, next) { |
590 | if (strcmp(vbasedev_iter->name, vbasedev->name) == 0) { | |
9bdbfbd5 | 591 | error_setg(errp, "device is already attached"); |
0ea2730b EA |
592 | vfio_put_group(group); |
593 | return -EBUSY; | |
594 | } | |
595 | } | |
9bdbfbd5 | 596 | ret = vfio_get_device(group, vbasedev->name, vbasedev, errp); |
0ea2730b | 597 | if (ret) { |
0ea2730b | 598 | vfio_put_group(group); |
9bdbfbd5 | 599 | return ret; |
0ea2730b EA |
600 | } |
601 | ||
9bdbfbd5 | 602 | ret = vfio_populate_device(vbasedev, errp); |
0ea2730b | 603 | if (ret) { |
0ea2730b EA |
604 | vfio_put_group(group); |
605 | } | |
606 | ||
607 | return ret; | |
608 | } | |
609 | ||
0ea2730b EA |
610 | /** |
611 | * vfio_platform_realize - the device realize function | |
612 | * @dev: device state pointer | |
613 | * @errp: error | |
614 | * | |
615 | * initialize the device, its memory regions and IRQ structures | |
616 | * IRQ are started separately | |
617 | */ | |
618 | static void vfio_platform_realize(DeviceState *dev, Error **errp) | |
619 | { | |
620 | VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(dev); | |
621 | SysBusDevice *sbdev = SYS_BUS_DEVICE(dev); | |
622 | VFIODevice *vbasedev = &vdev->vbasedev; | |
623 | int i, ret; | |
624 | ||
625 | vbasedev->type = VFIO_DEVICE_TYPE_PLATFORM; | |
7da624e2 | 626 | vbasedev->dev = dev; |
0ea2730b EA |
627 | vbasedev->ops = &vfio_platform_ops; |
628 | ||
89202c6f EA |
629 | qemu_mutex_init(&vdev->intp_mutex); |
630 | ||
7df9381b AW |
631 | trace_vfio_platform_realize(vbasedev->sysfsdev ? |
632 | vbasedev->sysfsdev : vbasedev->name, | |
633 | vdev->compat); | |
0ea2730b | 634 | |
9bdbfbd5 | 635 | ret = vfio_base_device_init(vbasedev, errp); |
0ea2730b | 636 | if (ret) { |
9bdbfbd5 | 637 | goto out; |
0ea2730b EA |
638 | } |
639 | ||
a49531eb EA |
640 | if (!vdev->compat) { |
641 | GError *gerr = NULL; | |
642 | gchar *contents; | |
643 | gsize length; | |
644 | char *path; | |
645 | ||
646 | path = g_strdup_printf("%s/of_node/compatible", vbasedev->sysfsdev); | |
647 | if (!g_file_get_contents(path, &contents, &length, &gerr)) { | |
648 | error_setg(errp, "%s", gerr->message); | |
649 | g_error_free(gerr); | |
650 | g_free(path); | |
651 | return; | |
652 | } | |
653 | g_free(path); | |
654 | vdev->compat = contents; | |
655 | for (vdev->num_compat = 0; length; vdev->num_compat++) { | |
656 | size_t skip = strlen(contents) + 1; | |
657 | contents += skip; | |
658 | length -= skip; | |
659 | } | |
660 | } | |
661 | ||
0ea2730b | 662 | for (i = 0; i < vbasedev->num_regions; i++) { |
db0da029 | 663 | if (vfio_region_mmap(vdev->regions[i])) { |
e1eb292a MA |
664 | warn_report("%s mmap unsupported, performance may be slow", |
665 | memory_region_name(vdev->regions[i]->mem)); | |
db0da029 AW |
666 | } |
667 | sysbus_init_mmio(sbdev, vdev->regions[i]->mem); | |
0ea2730b | 668 | } |
9bdbfbd5 EA |
669 | out: |
670 | if (!ret) { | |
671 | return; | |
672 | } | |
673 | ||
674 | if (vdev->vbasedev.name) { | |
c3b8e3e0 | 675 | error_prepend(errp, VFIO_MSG_PREFIX, vdev->vbasedev.name); |
9bdbfbd5 EA |
676 | } else { |
677 | error_prepend(errp, "vfio error: "); | |
678 | } | |
0ea2730b EA |
679 | } |
680 | ||
681 | static const VMStateDescription vfio_platform_vmstate = { | |
da56e330 | 682 | .name = "vfio-platform", |
0ea2730b EA |
683 | .unmigratable = 1, |
684 | }; | |
685 | ||
686 | static Property vfio_platform_dev_properties[] = { | |
687 | DEFINE_PROP_STRING("host", VFIOPlatformDevice, vbasedev.name), | |
7df9381b | 688 | DEFINE_PROP_STRING("sysfsdev", VFIOPlatformDevice, vbasedev.sysfsdev), |
5e15d79b | 689 | DEFINE_PROP_BOOL("x-no-mmap", VFIOPlatformDevice, vbasedev.no_mmap, false), |
38559979 EA |
690 | DEFINE_PROP_UINT32("mmap-timeout-ms", VFIOPlatformDevice, |
691 | mmap_timeout, 1100), | |
fb5f8164 | 692 | DEFINE_PROP_BOOL("x-irqfd", VFIOPlatformDevice, irqfd_allowed, true), |
0ea2730b EA |
693 | DEFINE_PROP_END_OF_LIST(), |
694 | }; | |
695 | ||
696 | static void vfio_platform_class_init(ObjectClass *klass, void *data) | |
697 | { | |
698 | DeviceClass *dc = DEVICE_CLASS(klass); | |
fb5f8164 | 699 | SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(klass); |
0ea2730b EA |
700 | |
701 | dc->realize = vfio_platform_realize; | |
702 | dc->props = vfio_platform_dev_properties; | |
703 | dc->vmsd = &vfio_platform_vmstate; | |
704 | dc->desc = "VFIO-based platform device assignment"; | |
fb5f8164 | 705 | sbc->connect_irq_notifier = vfio_start_irqfd_injection; |
0ea2730b | 706 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
a49531eb EA |
707 | /* Supported by TYPE_VIRT_MACHINE */ |
708 | dc->user_creatable = true; | |
0ea2730b EA |
709 | } |
710 | ||
711 | static const TypeInfo vfio_platform_dev_info = { | |
712 | .name = TYPE_VFIO_PLATFORM, | |
713 | .parent = TYPE_SYS_BUS_DEVICE, | |
714 | .instance_size = sizeof(VFIOPlatformDevice), | |
715 | .class_init = vfio_platform_class_init, | |
716 | .class_size = sizeof(VFIOPlatformDeviceClass), | |
0ea2730b EA |
717 | }; |
718 | ||
719 | static void register_vfio_platform_dev_type(void) | |
720 | { | |
721 | type_register_static(&vfio_platform_dev_info); | |
722 | } | |
723 | ||
724 | type_init(register_vfio_platform_dev_type) |