| 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 "exec/cpu-common.h" |
| 26 | #include "hw/vfio/vfio-device.h" |
| 27 | #include "qapi/error.h" |
| 28 | #include "vfio-helpers.h" |
| 29 | |
| 30 | int vfio_bitmap_alloc(VFIOBitmap *vbmap, hwaddr size) |
| 31 | { |
| 32 | vbmap->pages = REAL_HOST_PAGE_ALIGN(size) / qemu_real_host_page_size(); |
| 33 | vbmap->size = ROUND_UP(vbmap->pages, sizeof(__u64) * BITS_PER_BYTE) / |
| 34 | BITS_PER_BYTE; |
| 35 | vbmap->bitmap = g_try_malloc0(vbmap->size); |
| 36 | if (!vbmap->bitmap) { |
| 37 | return -ENOMEM; |
| 38 | } |
| 39 | |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | struct vfio_info_cap_header * |
| 44 | vfio_get_cap(void *ptr, uint32_t cap_offset, uint16_t id) |
| 45 | { |
| 46 | struct vfio_info_cap_header *hdr; |
| 47 | |
| 48 | for (hdr = ptr + cap_offset; hdr != ptr; hdr = ptr + hdr->next) { |
| 49 | if (hdr->id == id) { |
| 50 | return hdr; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | struct vfio_info_cap_header * |
| 58 | vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id) |
| 59 | { |
| 60 | if (!(info->flags & VFIO_REGION_INFO_FLAG_CAPS)) { |
| 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | return vfio_get_cap((void *)info, info->cap_offset, id); |
| 65 | } |
| 66 | |
| 67 | struct vfio_info_cap_header * |
| 68 | vfio_get_device_info_cap(struct vfio_device_info *info, uint16_t id) |
| 69 | { |
| 70 | if (!(info->flags & VFIO_DEVICE_FLAGS_CAPS)) { |
| 71 | return NULL; |
| 72 | } |
| 73 | |
| 74 | return vfio_get_cap((void *)info, info->cap_offset, id); |
| 75 | } |
| 76 | |
| 77 | struct vfio_info_cap_header * |
| 78 | vfio_get_iommu_type1_info_cap(struct vfio_iommu_type1_info *info, uint16_t id) |
| 79 | { |
| 80 | if (!(info->flags & VFIO_IOMMU_INFO_CAPS)) { |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | return vfio_get_cap((void *)info, info->cap_offset, id); |
| 85 | } |
| 86 | |
| 87 | bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info, |
| 88 | unsigned int *avail) |
| 89 | { |
| 90 | struct vfio_info_cap_header *hdr; |
| 91 | struct vfio_iommu_type1_info_dma_avail *cap; |
| 92 | |
| 93 | /* If the capability cannot be found, assume no DMA limiting */ |
| 94 | hdr = vfio_get_iommu_type1_info_cap(info, |
| 95 | VFIO_IOMMU_TYPE1_INFO_DMA_AVAIL); |
| 96 | if (!hdr) { |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | if (avail != NULL) { |
| 101 | cap = (void *) hdr; |
| 102 | *avail = cap->avail; |
| 103 | } |
| 104 | |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | struct vfio_device_info *vfio_get_device_info(int fd) |
| 109 | { |
| 110 | struct vfio_device_info *info; |
| 111 | uint32_t argsz = sizeof(*info); |
| 112 | |
| 113 | info = g_malloc0(argsz); |
| 114 | |
| 115 | retry: |
| 116 | info->argsz = argsz; |
| 117 | |
| 118 | if (ioctl(fd, VFIO_DEVICE_GET_INFO, info)) { |
| 119 | g_free(info); |
| 120 | return NULL; |
| 121 | } |
| 122 | |
| 123 | if (info->argsz > argsz) { |
| 124 | argsz = info->argsz; |
| 125 | info = g_realloc(info, argsz); |
| 126 | goto retry; |
| 127 | } |
| 128 | |
| 129 | return info; |
| 130 | } |