xref: /openbmc/qemu/hw/vfio/helpers.c (revision b403c8d5)
1 /*
2  * low level and IOMMU backend agnostic helpers used by VFIO devices,
3  * related to regions, interrupts, capabilities
4  *
5  * Copyright Red Hat, Inc. 2012
6  *
7  * Authors:
8  *  Alex Williamson <alex.williamson@redhat.com>
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 qemu-kvm device-assignment:
14  *  Adapted for KVM by Qumranet.
15  *  Copyright (c) 2007, Neocleus, Alex Novik (alex@neocleus.com)
16  *  Copyright (c) 2007, Neocleus, Guy Zana (guy@neocleus.com)
17  *  Copyright (C) 2008, Qumranet, Amit Shah (amit.shah@qumranet.com)
18  *  Copyright (C) 2008, Red Hat, Amit Shah (amit.shah@redhat.com)
19  *  Copyright (C) 2008, IBM, Muli Ben-Yehuda (muli@il.ibm.com)
20  */
21 
22 #include "qemu/osdep.h"
23 #include <sys/ioctl.h>
24 
25 #include "hw/vfio/vfio-common.h"
26 #include "hw/hw.h"
27 #include "trace.h"
28 #include "qapi/error.h"
29 #include "qemu/error-report.h"
30 #include "monitor/monitor.h"
31 
32 /*
33  * Common VFIO interrupt disable
34  */
35 void vfio_disable_irqindex(VFIODevice *vbasedev, int index)
36 {
37     struct vfio_irq_set irq_set = {
38         .argsz = sizeof(irq_set),
39         .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER,
40         .index = index,
41         .start = 0,
42         .count = 0,
43     };
44 
45     ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set);
46 }
47 
48 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index)
49 {
50     struct vfio_irq_set irq_set = {
51         .argsz = sizeof(irq_set),
52         .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK,
53         .index = index,
54         .start = 0,
55         .count = 1,
56     };
57 
58     ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set);
59 }
60 
61 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index)
62 {
63     struct vfio_irq_set irq_set = {
64         .argsz = sizeof(irq_set),
65         .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_MASK,
66         .index = index,
67         .start = 0,
68         .count = 1,
69     };
70 
71     ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set);
72 }
73 
74 static inline const char *action_to_str(int action)
75 {
76     switch (action) {
77     case VFIO_IRQ_SET_ACTION_MASK:
78         return "MASK";
79     case VFIO_IRQ_SET_ACTION_UNMASK:
80         return "UNMASK";
81     case VFIO_IRQ_SET_ACTION_TRIGGER:
82         return "TRIGGER";
83     default:
84         return "UNKNOWN ACTION";
85     }
86 }
87 
88 static const char *index_to_str(VFIODevice *vbasedev, int index)
89 {
90     if (vbasedev->type != VFIO_DEVICE_TYPE_PCI) {
91         return NULL;
92     }
93 
94     switch (index) {
95     case VFIO_PCI_INTX_IRQ_INDEX:
96         return "INTX";
97     case VFIO_PCI_MSI_IRQ_INDEX:
98         return "MSI";
99     case VFIO_PCI_MSIX_IRQ_INDEX:
100         return "MSIX";
101     case VFIO_PCI_ERR_IRQ_INDEX:
102         return "ERR";
103     case VFIO_PCI_REQ_IRQ_INDEX:
104         return "REQ";
105     default:
106         return NULL;
107     }
108 }
109 
110 int vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex,
111                            int action, int fd, Error **errp)
112 {
113     ERRP_GUARD();
114     struct vfio_irq_set *irq_set;
115     int argsz, ret = 0;
116     const char *name;
117     int32_t *pfd;
118 
119     argsz = sizeof(*irq_set) + sizeof(*pfd);
120 
121     irq_set = g_malloc0(argsz);
122     irq_set->argsz = argsz;
123     irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | action;
124     irq_set->index = index;
125     irq_set->start = subindex;
126     irq_set->count = 1;
127     pfd = (int32_t *)&irq_set->data;
128     *pfd = fd;
129 
130     if (ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, irq_set)) {
131         ret = -errno;
132     }
133     g_free(irq_set);
134 
135     if (!ret) {
136         return 0;
137     }
138 
139     error_setg_errno(errp, -ret, "VFIO_DEVICE_SET_IRQS failure");
140 
141     name = index_to_str(vbasedev, index);
142     if (name) {
143         error_prepend(errp, "%s-%d: ", name, subindex);
144     } else {
145         error_prepend(errp, "index %d-%d: ", index, subindex);
146     }
147     error_prepend(errp,
148                   "Failed to %s %s eventfd signaling for interrupt ",
149                   fd < 0 ? "tear down" : "set up", action_to_str(action));
150     return ret;
151 }
152 
153 /*
154  * IO Port/MMIO - Beware of the endians, VFIO is always little endian
155  */
156 void vfio_region_write(void *opaque, hwaddr addr,
157                        uint64_t data, unsigned size)
158 {
159     VFIORegion *region = opaque;
160     VFIODevice *vbasedev = region->vbasedev;
161     union {
162         uint8_t byte;
163         uint16_t word;
164         uint32_t dword;
165         uint64_t qword;
166     } buf;
167 
168     switch (size) {
169     case 1:
170         buf.byte = data;
171         break;
172     case 2:
173         buf.word = cpu_to_le16(data);
174         break;
175     case 4:
176         buf.dword = cpu_to_le32(data);
177         break;
178     case 8:
179         buf.qword = cpu_to_le64(data);
180         break;
181     default:
182         hw_error("vfio: unsupported write size, %u bytes", size);
183         break;
184     }
185 
186     if (pwrite(vbasedev->fd, &buf, size, region->fd_offset + addr) != size) {
187         error_report("%s(%s:region%d+0x%"HWADDR_PRIx", 0x%"PRIx64
188                      ",%d) failed: %m",
189                      __func__, vbasedev->name, region->nr,
190                      addr, data, size);
191     }
192 
193     trace_vfio_region_write(vbasedev->name, region->nr, addr, data, size);
194 
195     /*
196      * A read or write to a BAR always signals an INTx EOI.  This will
197      * do nothing if not pending (including not in INTx mode).  We assume
198      * that a BAR access is in response to an interrupt and that BAR
199      * accesses will service the interrupt.  Unfortunately, we don't know
200      * which access will service the interrupt, so we're potentially
201      * getting quite a few host interrupts per guest interrupt.
202      */
203     vbasedev->ops->vfio_eoi(vbasedev);
204 }
205 
206 uint64_t vfio_region_read(void *opaque,
207                           hwaddr addr, unsigned size)
208 {
209     VFIORegion *region = opaque;
210     VFIODevice *vbasedev = region->vbasedev;
211     union {
212         uint8_t byte;
213         uint16_t word;
214         uint32_t dword;
215         uint64_t qword;
216     } buf;
217     uint64_t data = 0;
218 
219     if (pread(vbasedev->fd, &buf, size, region->fd_offset + addr) != size) {
220         error_report("%s(%s:region%d+0x%"HWADDR_PRIx", %d) failed: %m",
221                      __func__, vbasedev->name, region->nr,
222                      addr, size);
223         return (uint64_t)-1;
224     }
225     switch (size) {
226     case 1:
227         data = buf.byte;
228         break;
229     case 2:
230         data = le16_to_cpu(buf.word);
231         break;
232     case 4:
233         data = le32_to_cpu(buf.dword);
234         break;
235     case 8:
236         data = le64_to_cpu(buf.qword);
237         break;
238     default:
239         hw_error("vfio: unsupported read size, %u bytes", size);
240         break;
241     }
242 
243     trace_vfio_region_read(vbasedev->name, region->nr, addr, size, data);
244 
245     /* Same as write above */
246     vbasedev->ops->vfio_eoi(vbasedev);
247 
248     return data;
249 }
250 
251 const MemoryRegionOps vfio_region_ops = {
252     .read = vfio_region_read,
253     .write = vfio_region_write,
254     .endianness = DEVICE_LITTLE_ENDIAN,
255     .valid = {
256         .min_access_size = 1,
257         .max_access_size = 8,
258     },
259     .impl = {
260         .min_access_size = 1,
261         .max_access_size = 8,
262     },
263 };
264 
265 int vfio_bitmap_alloc(VFIOBitmap *vbmap, hwaddr size)
266 {
267     vbmap->pages = REAL_HOST_PAGE_ALIGN(size) / qemu_real_host_page_size();
268     vbmap->size = ROUND_UP(vbmap->pages, sizeof(__u64) * BITS_PER_BYTE) /
269                                          BITS_PER_BYTE;
270     vbmap->bitmap = g_try_malloc0(vbmap->size);
271     if (!vbmap->bitmap) {
272         return -ENOMEM;
273     }
274 
275     return 0;
276 }
277 
278 struct vfio_info_cap_header *
279 vfio_get_cap(void *ptr, uint32_t cap_offset, uint16_t id)
280 {
281     struct vfio_info_cap_header *hdr;
282 
283     for (hdr = ptr + cap_offset; hdr != ptr; hdr = ptr + hdr->next) {
284         if (hdr->id == id) {
285             return hdr;
286         }
287     }
288 
289     return NULL;
290 }
291 
292 struct vfio_info_cap_header *
293 vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id)
294 {
295     if (!(info->flags & VFIO_REGION_INFO_FLAG_CAPS)) {
296         return NULL;
297     }
298 
299     return vfio_get_cap((void *)info, info->cap_offset, id);
300 }
301 
302 struct vfio_info_cap_header *
303 vfio_get_device_info_cap(struct vfio_device_info *info, uint16_t id)
304 {
305     if (!(info->flags & VFIO_DEVICE_FLAGS_CAPS)) {
306         return NULL;
307     }
308 
309     return vfio_get_cap((void *)info, info->cap_offset, id);
310 }
311 
312 static int vfio_setup_region_sparse_mmaps(VFIORegion *region,
313                                           struct vfio_region_info *info)
314 {
315     struct vfio_info_cap_header *hdr;
316     struct vfio_region_info_cap_sparse_mmap *sparse;
317     int i, j;
318 
319     hdr = vfio_get_region_info_cap(info, VFIO_REGION_INFO_CAP_SPARSE_MMAP);
320     if (!hdr) {
321         return -ENODEV;
322     }
323 
324     sparse = container_of(hdr, struct vfio_region_info_cap_sparse_mmap, header);
325 
326     trace_vfio_region_sparse_mmap_header(region->vbasedev->name,
327                                          region->nr, sparse->nr_areas);
328 
329     region->mmaps = g_new0(VFIOMmap, sparse->nr_areas);
330 
331     for (i = 0, j = 0; i < sparse->nr_areas; i++) {
332         if (sparse->areas[i].size) {
333             trace_vfio_region_sparse_mmap_entry(i, sparse->areas[i].offset,
334                                             sparse->areas[i].offset +
335                                             sparse->areas[i].size - 1);
336             region->mmaps[j].offset = sparse->areas[i].offset;
337             region->mmaps[j].size = sparse->areas[i].size;
338             j++;
339         }
340     }
341 
342     region->nr_mmaps = j;
343     region->mmaps = g_realloc(region->mmaps, j * sizeof(VFIOMmap));
344 
345     return 0;
346 }
347 
348 int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region,
349                       int index, const char *name)
350 {
351     struct vfio_region_info *info;
352     int ret;
353 
354     ret = vfio_get_region_info(vbasedev, index, &info);
355     if (ret) {
356         return ret;
357     }
358 
359     region->vbasedev = vbasedev;
360     region->flags = info->flags;
361     region->size = info->size;
362     region->fd_offset = info->offset;
363     region->nr = index;
364 
365     if (region->size) {
366         region->mem = g_new0(MemoryRegion, 1);
367         memory_region_init_io(region->mem, obj, &vfio_region_ops,
368                               region, name, region->size);
369 
370         if (!vbasedev->no_mmap &&
371             region->flags & VFIO_REGION_INFO_FLAG_MMAP) {
372 
373             ret = vfio_setup_region_sparse_mmaps(region, info);
374 
375             if (ret) {
376                 region->nr_mmaps = 1;
377                 region->mmaps = g_new0(VFIOMmap, region->nr_mmaps);
378                 region->mmaps[0].offset = 0;
379                 region->mmaps[0].size = region->size;
380             }
381         }
382     }
383 
384     g_free(info);
385 
386     trace_vfio_region_setup(vbasedev->name, index, name,
387                             region->flags, region->fd_offset, region->size);
388     return 0;
389 }
390 
391 static void vfio_subregion_unmap(VFIORegion *region, int index)
392 {
393     trace_vfio_region_unmap(memory_region_name(&region->mmaps[index].mem),
394                             region->mmaps[index].offset,
395                             region->mmaps[index].offset +
396                             region->mmaps[index].size - 1);
397     memory_region_del_subregion(region->mem, &region->mmaps[index].mem);
398     munmap(region->mmaps[index].mmap, region->mmaps[index].size);
399     object_unparent(OBJECT(&region->mmaps[index].mem));
400     region->mmaps[index].mmap = NULL;
401 }
402 
403 int vfio_region_mmap(VFIORegion *region)
404 {
405     int i, prot = 0;
406     char *name;
407 
408     if (!region->mem) {
409         return 0;
410     }
411 
412     prot |= region->flags & VFIO_REGION_INFO_FLAG_READ ? PROT_READ : 0;
413     prot |= region->flags & VFIO_REGION_INFO_FLAG_WRITE ? PROT_WRITE : 0;
414 
415     for (i = 0; i < region->nr_mmaps; i++) {
416         region->mmaps[i].mmap = mmap(NULL, region->mmaps[i].size, prot,
417                                      MAP_SHARED, region->vbasedev->fd,
418                                      region->fd_offset +
419                                      region->mmaps[i].offset);
420         if (region->mmaps[i].mmap == MAP_FAILED) {
421             int ret = -errno;
422 
423             trace_vfio_region_mmap_fault(memory_region_name(region->mem), i,
424                                          region->fd_offset +
425                                          region->mmaps[i].offset,
426                                          region->fd_offset +
427                                          region->mmaps[i].offset +
428                                          region->mmaps[i].size - 1, ret);
429 
430             region->mmaps[i].mmap = NULL;
431 
432             for (i--; i >= 0; i--) {
433                 vfio_subregion_unmap(region, i);
434             }
435 
436             return ret;
437         }
438 
439         name = g_strdup_printf("%s mmaps[%d]",
440                                memory_region_name(region->mem), i);
441         memory_region_init_ram_device_ptr(&region->mmaps[i].mem,
442                                           memory_region_owner(region->mem),
443                                           name, region->mmaps[i].size,
444                                           region->mmaps[i].mmap);
445         g_free(name);
446         memory_region_add_subregion(region->mem, region->mmaps[i].offset,
447                                     &region->mmaps[i].mem);
448 
449         trace_vfio_region_mmap(memory_region_name(&region->mmaps[i].mem),
450                                region->mmaps[i].offset,
451                                region->mmaps[i].offset +
452                                region->mmaps[i].size - 1);
453     }
454 
455     return 0;
456 }
457 
458 void vfio_region_unmap(VFIORegion *region)
459 {
460     int i;
461 
462     if (!region->mem) {
463         return;
464     }
465 
466     for (i = 0; i < region->nr_mmaps; i++) {
467         if (region->mmaps[i].mmap) {
468             vfio_subregion_unmap(region, i);
469         }
470     }
471 }
472 
473 void vfio_region_exit(VFIORegion *region)
474 {
475     int i;
476 
477     if (!region->mem) {
478         return;
479     }
480 
481     for (i = 0; i < region->nr_mmaps; i++) {
482         if (region->mmaps[i].mmap) {
483             memory_region_del_subregion(region->mem, &region->mmaps[i].mem);
484         }
485     }
486 
487     trace_vfio_region_exit(region->vbasedev->name, region->nr);
488 }
489 
490 void vfio_region_finalize(VFIORegion *region)
491 {
492     int i;
493 
494     if (!region->mem) {
495         return;
496     }
497 
498     for (i = 0; i < region->nr_mmaps; i++) {
499         if (region->mmaps[i].mmap) {
500             munmap(region->mmaps[i].mmap, region->mmaps[i].size);
501             object_unparent(OBJECT(&region->mmaps[i].mem));
502         }
503     }
504 
505     object_unparent(OBJECT(region->mem));
506 
507     g_free(region->mem);
508     g_free(region->mmaps);
509 
510     trace_vfio_region_finalize(region->vbasedev->name, region->nr);
511 
512     region->mem = NULL;
513     region->mmaps = NULL;
514     region->nr_mmaps = 0;
515     region->size = 0;
516     region->flags = 0;
517     region->nr = 0;
518 }
519 
520 void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled)
521 {
522     int i;
523 
524     if (!region->mem) {
525         return;
526     }
527 
528     for (i = 0; i < region->nr_mmaps; i++) {
529         if (region->mmaps[i].mmap) {
530             memory_region_set_enabled(&region->mmaps[i].mem, enabled);
531         }
532     }
533 
534     trace_vfio_region_mmaps_set_enabled(memory_region_name(region->mem),
535                                         enabled);
536 }
537 
538 int vfio_get_region_info(VFIODevice *vbasedev, int index,
539                          struct vfio_region_info **info)
540 {
541     size_t argsz = sizeof(struct vfio_region_info);
542 
543     *info = g_malloc0(argsz);
544 
545     (*info)->index = index;
546 retry:
547     (*info)->argsz = argsz;
548 
549     if (ioctl(vbasedev->fd, VFIO_DEVICE_GET_REGION_INFO, *info)) {
550         g_free(*info);
551         *info = NULL;
552         return -errno;
553     }
554 
555     if ((*info)->argsz > argsz) {
556         argsz = (*info)->argsz;
557         *info = g_realloc(*info, argsz);
558 
559         goto retry;
560     }
561 
562     return 0;
563 }
564 
565 int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type,
566                              uint32_t subtype, struct vfio_region_info **info)
567 {
568     int i;
569 
570     for (i = 0; i < vbasedev->num_regions; i++) {
571         struct vfio_info_cap_header *hdr;
572         struct vfio_region_info_cap_type *cap_type;
573 
574         if (vfio_get_region_info(vbasedev, i, info)) {
575             continue;
576         }
577 
578         hdr = vfio_get_region_info_cap(*info, VFIO_REGION_INFO_CAP_TYPE);
579         if (!hdr) {
580             g_free(*info);
581             continue;
582         }
583 
584         cap_type = container_of(hdr, struct vfio_region_info_cap_type, header);
585 
586         trace_vfio_get_dev_region(vbasedev->name, i,
587                                   cap_type->type, cap_type->subtype);
588 
589         if (cap_type->type == type && cap_type->subtype == subtype) {
590             return 0;
591         }
592 
593         g_free(*info);
594     }
595 
596     *info = NULL;
597     return -ENODEV;
598 }
599 
600 bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type)
601 {
602     struct vfio_region_info *info = NULL;
603     bool ret = false;
604 
605     if (!vfio_get_region_info(vbasedev, region, &info)) {
606         if (vfio_get_region_info_cap(info, cap_type)) {
607             ret = true;
608         }
609         g_free(info);
610     }
611 
612     return ret;
613 }
614 
615 int vfio_device_get_name(VFIODevice *vbasedev, Error **errp)
616 {
617     ERRP_GUARD();
618     struct stat st;
619 
620     if (vbasedev->fd < 0) {
621         if (stat(vbasedev->sysfsdev, &st) < 0) {
622             error_setg_errno(errp, errno, "no such host device");
623             error_prepend(errp, VFIO_MSG_PREFIX, vbasedev->sysfsdev);
624             return -errno;
625         }
626         /* User may specify a name, e.g: VFIO platform device */
627         if (!vbasedev->name) {
628             vbasedev->name = g_path_get_basename(vbasedev->sysfsdev);
629         }
630     } else {
631         if (!vbasedev->iommufd) {
632             error_setg(errp, "Use FD passing only with iommufd backend");
633             return -EINVAL;
634         }
635         /*
636          * Give a name with fd so any function printing out vbasedev->name
637          * will not break.
638          */
639         if (!vbasedev->name) {
640             vbasedev->name = g_strdup_printf("VFIO_FD%d", vbasedev->fd);
641         }
642     }
643 
644     return 0;
645 }
646 
647 void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp)
648 {
649     ERRP_GUARD();
650     int fd = monitor_fd_param(monitor_cur(), str, errp);
651 
652     if (fd < 0) {
653         error_prepend(errp, "Could not parse remote object fd %s:", str);
654         return;
655     }
656     vbasedev->fd = fd;
657 }
658 
659 void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops,
660                       DeviceState *dev, bool ram_discard)
661 {
662     vbasedev->type = type;
663     vbasedev->ops = ops;
664     vbasedev->dev = dev;
665     vbasedev->fd = -1;
666 
667     vbasedev->ram_block_discard_allowed = ram_discard;
668 }
669