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