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