1499e53ccSCédric Le Goater /*
2499e53ccSCédric Le Goater * VFIO regions
3499e53ccSCédric Le Goater *
4499e53ccSCédric Le Goater * Copyright Red Hat, Inc. 2012
5499e53ccSCédric Le Goater *
6499e53ccSCédric Le Goater * Authors:
7499e53ccSCédric Le Goater * Alex Williamson <alex.williamson@redhat.com>
8499e53ccSCédric Le Goater *
9499e53ccSCédric Le Goater * This work is licensed under the terms of the GNU GPL, version 2. See
10499e53ccSCédric Le Goater * the COPYING file in the top-level directory.
11499e53ccSCédric Le Goater *
12499e53ccSCédric Le Goater * Based on qemu-kvm device-assignment:
13499e53ccSCédric Le Goater * Adapted for KVM by Qumranet.
14499e53ccSCédric Le Goater * Copyright (c) 2007, Neocleus, Alex Novik (alex@neocleus.com)
15499e53ccSCédric Le Goater * Copyright (c) 2007, Neocleus, Guy Zana (guy@neocleus.com)
16499e53ccSCédric Le Goater * Copyright (C) 2008, Qumranet, Amit Shah (amit.shah@qumranet.com)
17499e53ccSCédric Le Goater * Copyright (C) 2008, Red Hat, Amit Shah (amit.shah@redhat.com)
18499e53ccSCédric Le Goater * Copyright (C) 2008, IBM, Muli Ben-Yehuda (muli@il.ibm.com)
19499e53ccSCédric Le Goater */
20499e53ccSCédric Le Goater
21499e53ccSCédric Le Goater #include "qemu/osdep.h"
22499e53ccSCédric Le Goater #include <sys/ioctl.h>
23499e53ccSCédric Le Goater
2411b8b9d5SCédric Le Goater #include "hw/vfio/vfio-region.h"
2511b8b9d5SCédric Le Goater #include "hw/vfio/vfio-device.h"
26499e53ccSCédric Le Goater #include "hw/hw.h"
27499e53ccSCédric Le Goater #include "trace.h"
28499e53ccSCédric Le Goater #include "qapi/error.h"
29499e53ccSCédric Le Goater #include "qemu/error-report.h"
30499e53ccSCédric Le Goater #include "qemu/units.h"
31499e53ccSCédric Le Goater #include "monitor/monitor.h"
32ac28680dSCédric Le Goater #include "vfio-helpers.h"
33499e53ccSCédric Le Goater
34499e53ccSCédric Le Goater /*
35499e53ccSCédric Le Goater * IO Port/MMIO - Beware of the endians, VFIO is always little endian
36499e53ccSCédric Le Goater */
vfio_region_write(void * opaque,hwaddr addr,uint64_t data,unsigned size)37499e53ccSCédric Le Goater void vfio_region_write(void *opaque, hwaddr addr,
38499e53ccSCédric Le Goater uint64_t data, unsigned size)
39499e53ccSCédric Le Goater {
40499e53ccSCédric Le Goater VFIORegion *region = opaque;
41499e53ccSCédric Le Goater VFIODevice *vbasedev = region->vbasedev;
42499e53ccSCédric Le Goater union {
43499e53ccSCédric Le Goater uint8_t byte;
44499e53ccSCédric Le Goater uint16_t word;
45499e53ccSCédric Le Goater uint32_t dword;
46499e53ccSCédric Le Goater uint64_t qword;
47499e53ccSCédric Le Goater } buf;
48*776066acSJohn Levon int ret;
49499e53ccSCédric Le Goater
50499e53ccSCédric Le Goater switch (size) {
51499e53ccSCédric Le Goater case 1:
52499e53ccSCédric Le Goater buf.byte = data;
53499e53ccSCédric Le Goater break;
54499e53ccSCédric Le Goater case 2:
55499e53ccSCédric Le Goater buf.word = cpu_to_le16(data);
56499e53ccSCédric Le Goater break;
57499e53ccSCédric Le Goater case 4:
58499e53ccSCédric Le Goater buf.dword = cpu_to_le32(data);
59499e53ccSCédric Le Goater break;
60499e53ccSCédric Le Goater case 8:
61499e53ccSCédric Le Goater buf.qword = cpu_to_le64(data);
62499e53ccSCédric Le Goater break;
63499e53ccSCédric Le Goater default:
64499e53ccSCédric Le Goater hw_error("vfio: unsupported write size, %u bytes", size);
65499e53ccSCédric Le Goater break;
66499e53ccSCédric Le Goater }
67499e53ccSCédric Le Goater
68*776066acSJohn Levon ret = vbasedev->io_ops->region_write(vbasedev, region->nr,
69*776066acSJohn Levon addr, size, &buf);
70*776066acSJohn Levon if (ret != size) {
71499e53ccSCédric Le Goater error_report("%s(%s:region%d+0x%"HWADDR_PRIx", 0x%"PRIx64
72*776066acSJohn Levon ",%d) failed: %s",
73499e53ccSCédric Le Goater __func__, vbasedev->name, region->nr,
74*776066acSJohn Levon addr, data, size, strwriteerror(ret));
75499e53ccSCédric Le Goater }
76499e53ccSCédric Le Goater
77499e53ccSCédric Le Goater trace_vfio_region_write(vbasedev->name, region->nr, addr, data, size);
78499e53ccSCédric Le Goater
79499e53ccSCédric Le Goater /*
80499e53ccSCédric Le Goater * A read or write to a BAR always signals an INTx EOI. This will
81499e53ccSCédric Le Goater * do nothing if not pending (including not in INTx mode). We assume
82499e53ccSCédric Le Goater * that a BAR access is in response to an interrupt and that BAR
83499e53ccSCédric Le Goater * accesses will service the interrupt. Unfortunately, we don't know
84499e53ccSCédric Le Goater * which access will service the interrupt, so we're potentially
85499e53ccSCédric Le Goater * getting quite a few host interrupts per guest interrupt.
86499e53ccSCédric Le Goater */
87499e53ccSCédric Le Goater vbasedev->ops->vfio_eoi(vbasedev);
88499e53ccSCédric Le Goater }
89499e53ccSCédric Le Goater
vfio_region_read(void * opaque,hwaddr addr,unsigned size)90499e53ccSCédric Le Goater uint64_t vfio_region_read(void *opaque,
91499e53ccSCédric Le Goater hwaddr addr, unsigned size)
92499e53ccSCédric Le Goater {
93499e53ccSCédric Le Goater VFIORegion *region = opaque;
94499e53ccSCédric Le Goater VFIODevice *vbasedev = region->vbasedev;
95499e53ccSCédric Le Goater union {
96499e53ccSCédric Le Goater uint8_t byte;
97499e53ccSCédric Le Goater uint16_t word;
98499e53ccSCédric Le Goater uint32_t dword;
99499e53ccSCédric Le Goater uint64_t qword;
100499e53ccSCédric Le Goater } buf;
101499e53ccSCédric Le Goater uint64_t data = 0;
102*776066acSJohn Levon int ret;
103499e53ccSCédric Le Goater
104*776066acSJohn Levon ret = vbasedev->io_ops->region_read(vbasedev, region->nr, addr, size, &buf);
105*776066acSJohn Levon if (ret != size) {
106*776066acSJohn Levon error_report("%s(%s:region%d+0x%"HWADDR_PRIx", %d) failed: %s",
107499e53ccSCédric Le Goater __func__, vbasedev->name, region->nr,
108*776066acSJohn Levon addr, size, strreaderror(ret));
109499e53ccSCédric Le Goater return (uint64_t)-1;
110499e53ccSCédric Le Goater }
111499e53ccSCédric Le Goater switch (size) {
112499e53ccSCédric Le Goater case 1:
113499e53ccSCédric Le Goater data = buf.byte;
114499e53ccSCédric Le Goater break;
115499e53ccSCédric Le Goater case 2:
116499e53ccSCédric Le Goater data = le16_to_cpu(buf.word);
117499e53ccSCédric Le Goater break;
118499e53ccSCédric Le Goater case 4:
119499e53ccSCédric Le Goater data = le32_to_cpu(buf.dword);
120499e53ccSCédric Le Goater break;
121499e53ccSCédric Le Goater case 8:
122499e53ccSCédric Le Goater data = le64_to_cpu(buf.qword);
123499e53ccSCédric Le Goater break;
124499e53ccSCédric Le Goater default:
125499e53ccSCédric Le Goater hw_error("vfio: unsupported read size, %u bytes", size);
126499e53ccSCédric Le Goater break;
127499e53ccSCédric Le Goater }
128499e53ccSCédric Le Goater
129499e53ccSCédric Le Goater trace_vfio_region_read(vbasedev->name, region->nr, addr, size, data);
130499e53ccSCédric Le Goater
131499e53ccSCédric Le Goater /* Same as write above */
132499e53ccSCédric Le Goater vbasedev->ops->vfio_eoi(vbasedev);
133499e53ccSCédric Le Goater
134499e53ccSCédric Le Goater return data;
135499e53ccSCédric Le Goater }
136499e53ccSCédric Le Goater
137499e53ccSCédric Le Goater static const MemoryRegionOps vfio_region_ops = {
138499e53ccSCédric Le Goater .read = vfio_region_read,
139499e53ccSCédric Le Goater .write = vfio_region_write,
140499e53ccSCédric Le Goater .endianness = DEVICE_LITTLE_ENDIAN,
141499e53ccSCédric Le Goater .valid = {
142499e53ccSCédric Le Goater .min_access_size = 1,
143499e53ccSCédric Le Goater .max_access_size = 8,
144499e53ccSCédric Le Goater },
145499e53ccSCédric Le Goater .impl = {
146499e53ccSCédric Le Goater .min_access_size = 1,
147499e53ccSCédric Le Goater .max_access_size = 8,
148499e53ccSCédric Le Goater },
149499e53ccSCédric Le Goater };
150499e53ccSCédric Le Goater
vfio_setup_region_sparse_mmaps(VFIORegion * region,struct vfio_region_info * info)151499e53ccSCédric Le Goater static int vfio_setup_region_sparse_mmaps(VFIORegion *region,
152499e53ccSCédric Le Goater struct vfio_region_info *info)
153499e53ccSCédric Le Goater {
154499e53ccSCédric Le Goater struct vfio_info_cap_header *hdr;
155499e53ccSCédric Le Goater struct vfio_region_info_cap_sparse_mmap *sparse;
156499e53ccSCédric Le Goater int i, j;
157499e53ccSCédric Le Goater
158499e53ccSCédric Le Goater hdr = vfio_get_region_info_cap(info, VFIO_REGION_INFO_CAP_SPARSE_MMAP);
159499e53ccSCédric Le Goater if (!hdr) {
160499e53ccSCédric Le Goater return -ENODEV;
161499e53ccSCédric Le Goater }
162499e53ccSCédric Le Goater
163499e53ccSCédric Le Goater sparse = container_of(hdr, struct vfio_region_info_cap_sparse_mmap, header);
164499e53ccSCédric Le Goater
165499e53ccSCédric Le Goater trace_vfio_region_sparse_mmap_header(region->vbasedev->name,
166499e53ccSCédric Le Goater region->nr, sparse->nr_areas);
167499e53ccSCédric Le Goater
168499e53ccSCédric Le Goater region->mmaps = g_new0(VFIOMmap, sparse->nr_areas);
169499e53ccSCédric Le Goater
170499e53ccSCédric Le Goater for (i = 0, j = 0; i < sparse->nr_areas; i++) {
171499e53ccSCédric Le Goater if (sparse->areas[i].size) {
172499e53ccSCédric Le Goater trace_vfio_region_sparse_mmap_entry(i, sparse->areas[i].offset,
173499e53ccSCédric Le Goater sparse->areas[i].offset +
174499e53ccSCédric Le Goater sparse->areas[i].size - 1);
175499e53ccSCédric Le Goater region->mmaps[j].offset = sparse->areas[i].offset;
176499e53ccSCédric Le Goater region->mmaps[j].size = sparse->areas[i].size;
177499e53ccSCédric Le Goater j++;
178499e53ccSCédric Le Goater }
179499e53ccSCédric Le Goater }
180499e53ccSCédric Le Goater
181499e53ccSCédric Le Goater region->nr_mmaps = j;
182499e53ccSCédric Le Goater region->mmaps = g_realloc(region->mmaps, j * sizeof(VFIOMmap));
183499e53ccSCédric Le Goater
184499e53ccSCédric Le Goater return 0;
185499e53ccSCédric Le Goater }
186499e53ccSCédric Le Goater
vfio_region_setup(Object * obj,VFIODevice * vbasedev,VFIORegion * region,int index,const char * name)187499e53ccSCédric Le Goater int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region,
188499e53ccSCédric Le Goater int index, const char *name)
189499e53ccSCédric Le Goater {
19095cdb024SJohn Levon struct vfio_region_info *info = NULL;
191499e53ccSCédric Le Goater int ret;
192499e53ccSCédric Le Goater
193e218ccf0SCédric Le Goater ret = vfio_device_get_region_info(vbasedev, index, &info);
194499e53ccSCédric Le Goater if (ret) {
195499e53ccSCédric Le Goater return ret;
196499e53ccSCédric Le Goater }
197499e53ccSCédric Le Goater
198499e53ccSCédric Le Goater region->vbasedev = vbasedev;
199499e53ccSCédric Le Goater region->flags = info->flags;
200499e53ccSCédric Le Goater region->size = info->size;
201499e53ccSCédric Le Goater region->fd_offset = info->offset;
202499e53ccSCédric Le Goater region->nr = index;
203499e53ccSCédric Le Goater
204499e53ccSCédric Le Goater if (region->size) {
205499e53ccSCédric Le Goater region->mem = g_new0(MemoryRegion, 1);
206499e53ccSCédric Le Goater memory_region_init_io(region->mem, obj, &vfio_region_ops,
207499e53ccSCédric Le Goater region, name, region->size);
208499e53ccSCédric Le Goater
209499e53ccSCédric Le Goater if (!vbasedev->no_mmap &&
210499e53ccSCédric Le Goater region->flags & VFIO_REGION_INFO_FLAG_MMAP) {
211499e53ccSCédric Le Goater
212499e53ccSCédric Le Goater ret = vfio_setup_region_sparse_mmaps(region, info);
213499e53ccSCédric Le Goater
214499e53ccSCédric Le Goater if (ret) {
215499e53ccSCédric Le Goater region->nr_mmaps = 1;
216499e53ccSCédric Le Goater region->mmaps = g_new0(VFIOMmap, region->nr_mmaps);
217499e53ccSCédric Le Goater region->mmaps[0].offset = 0;
218499e53ccSCédric Le Goater region->mmaps[0].size = region->size;
219499e53ccSCédric Le Goater }
220499e53ccSCédric Le Goater }
221499e53ccSCédric Le Goater }
222499e53ccSCédric Le Goater
223499e53ccSCédric Le Goater trace_vfio_region_setup(vbasedev->name, index, name,
224499e53ccSCédric Le Goater region->flags, region->fd_offset, region->size);
225499e53ccSCédric Le Goater return 0;
226499e53ccSCédric Le Goater }
227499e53ccSCédric Le Goater
vfio_subregion_unmap(VFIORegion * region,int index)228499e53ccSCédric Le Goater static void vfio_subregion_unmap(VFIORegion *region, int index)
229499e53ccSCédric Le Goater {
230499e53ccSCédric Le Goater trace_vfio_region_unmap(memory_region_name(®ion->mmaps[index].mem),
231499e53ccSCédric Le Goater region->mmaps[index].offset,
232499e53ccSCédric Le Goater region->mmaps[index].offset +
233499e53ccSCédric Le Goater region->mmaps[index].size - 1);
234499e53ccSCédric Le Goater memory_region_del_subregion(region->mem, ®ion->mmaps[index].mem);
235499e53ccSCédric Le Goater munmap(region->mmaps[index].mmap, region->mmaps[index].size);
236499e53ccSCédric Le Goater object_unparent(OBJECT(®ion->mmaps[index].mem));
237499e53ccSCédric Le Goater region->mmaps[index].mmap = NULL;
238499e53ccSCédric Le Goater }
239499e53ccSCédric Le Goater
vfio_region_mmap(VFIORegion * region)240499e53ccSCédric Le Goater int vfio_region_mmap(VFIORegion *region)
241499e53ccSCédric Le Goater {
242499e53ccSCédric Le Goater int i, ret, prot = 0;
243499e53ccSCédric Le Goater char *name;
244499e53ccSCédric Le Goater
245499e53ccSCédric Le Goater if (!region->mem) {
246499e53ccSCédric Le Goater return 0;
247499e53ccSCédric Le Goater }
248499e53ccSCédric Le Goater
249499e53ccSCédric Le Goater prot |= region->flags & VFIO_REGION_INFO_FLAG_READ ? PROT_READ : 0;
250499e53ccSCédric Le Goater prot |= region->flags & VFIO_REGION_INFO_FLAG_WRITE ? PROT_WRITE : 0;
251499e53ccSCédric Le Goater
252499e53ccSCédric Le Goater for (i = 0; i < region->nr_mmaps; i++) {
253499e53ccSCédric Le Goater size_t align = MIN(1ULL << ctz64(region->mmaps[i].size), 1 * GiB);
254499e53ccSCédric Le Goater void *map_base, *map_align;
255499e53ccSCédric Le Goater
256499e53ccSCédric Le Goater /*
257499e53ccSCédric Le Goater * Align the mmap for more efficient mapping in the kernel. Ideally
258499e53ccSCédric Le Goater * we'd know the PMD and PUD mapping sizes to use as discrete alignment
259499e53ccSCédric Le Goater * intervals, but we don't. As of Linux v6.12, the largest PUD size
260499e53ccSCédric Le Goater * supporting huge pfnmap is 1GiB (ARCH_SUPPORTS_PUD_PFNMAP is only set
261499e53ccSCédric Le Goater * on x86_64). Align by power-of-two size, capped at 1GiB.
262499e53ccSCédric Le Goater *
263499e53ccSCédric Le Goater * NB. qemu_memalign() and friends actually allocate memory, whereas
264499e53ccSCédric Le Goater * the region size here can exceed host memory, therefore we manually
265499e53ccSCédric Le Goater * create an oversized anonymous mapping and clean it up for alignment.
266499e53ccSCédric Le Goater */
267499e53ccSCédric Le Goater map_base = mmap(0, region->mmaps[i].size + align, PROT_NONE,
268499e53ccSCédric Le Goater MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
269499e53ccSCédric Le Goater if (map_base == MAP_FAILED) {
270499e53ccSCédric Le Goater ret = -errno;
271499e53ccSCédric Le Goater goto no_mmap;
272499e53ccSCédric Le Goater }
273499e53ccSCédric Le Goater
274499e53ccSCédric Le Goater map_align = (void *)ROUND_UP((uintptr_t)map_base, (uintptr_t)align);
275499e53ccSCédric Le Goater munmap(map_base, map_align - map_base);
276499e53ccSCédric Le Goater munmap(map_align + region->mmaps[i].size,
277499e53ccSCédric Le Goater align - (map_align - map_base));
278499e53ccSCédric Le Goater
279499e53ccSCédric Le Goater region->mmaps[i].mmap = mmap(map_align, region->mmaps[i].size, prot,
280499e53ccSCédric Le Goater MAP_SHARED | MAP_FIXED,
281499e53ccSCédric Le Goater region->vbasedev->fd,
282499e53ccSCédric Le Goater region->fd_offset +
283499e53ccSCédric Le Goater region->mmaps[i].offset);
284499e53ccSCédric Le Goater if (region->mmaps[i].mmap == MAP_FAILED) {
285499e53ccSCédric Le Goater ret = -errno;
286499e53ccSCédric Le Goater goto no_mmap;
287499e53ccSCédric Le Goater }
288499e53ccSCédric Le Goater
289499e53ccSCédric Le Goater name = g_strdup_printf("%s mmaps[%d]",
290499e53ccSCédric Le Goater memory_region_name(region->mem), i);
291499e53ccSCédric Le Goater memory_region_init_ram_device_ptr(®ion->mmaps[i].mem,
292499e53ccSCédric Le Goater memory_region_owner(region->mem),
293499e53ccSCédric Le Goater name, region->mmaps[i].size,
294499e53ccSCédric Le Goater region->mmaps[i].mmap);
295499e53ccSCédric Le Goater g_free(name);
296499e53ccSCédric Le Goater memory_region_add_subregion(region->mem, region->mmaps[i].offset,
297499e53ccSCédric Le Goater ®ion->mmaps[i].mem);
298499e53ccSCédric Le Goater
299499e53ccSCédric Le Goater trace_vfio_region_mmap(memory_region_name(®ion->mmaps[i].mem),
300499e53ccSCédric Le Goater region->mmaps[i].offset,
301499e53ccSCédric Le Goater region->mmaps[i].offset +
302499e53ccSCédric Le Goater region->mmaps[i].size - 1);
303499e53ccSCédric Le Goater }
304499e53ccSCédric Le Goater
305499e53ccSCédric Le Goater return 0;
306499e53ccSCédric Le Goater
307499e53ccSCédric Le Goater no_mmap:
308499e53ccSCédric Le Goater trace_vfio_region_mmap_fault(memory_region_name(region->mem), i,
309499e53ccSCédric Le Goater region->fd_offset + region->mmaps[i].offset,
310499e53ccSCédric Le Goater region->fd_offset + region->mmaps[i].offset +
311499e53ccSCédric Le Goater region->mmaps[i].size - 1, ret);
312499e53ccSCédric Le Goater
313499e53ccSCédric Le Goater region->mmaps[i].mmap = NULL;
314499e53ccSCédric Le Goater
315499e53ccSCédric Le Goater for (i--; i >= 0; i--) {
316499e53ccSCédric Le Goater vfio_subregion_unmap(region, i);
317499e53ccSCédric Le Goater }
318499e53ccSCédric Le Goater
319499e53ccSCédric Le Goater return ret;
320499e53ccSCédric Le Goater }
321499e53ccSCédric Le Goater
vfio_region_unmap(VFIORegion * region)322499e53ccSCédric Le Goater void vfio_region_unmap(VFIORegion *region)
323499e53ccSCédric Le Goater {
324499e53ccSCédric Le Goater int i;
325499e53ccSCédric Le Goater
326499e53ccSCédric Le Goater if (!region->mem) {
327499e53ccSCédric Le Goater return;
328499e53ccSCédric Le Goater }
329499e53ccSCédric Le Goater
330499e53ccSCédric Le Goater for (i = 0; i < region->nr_mmaps; i++) {
331499e53ccSCédric Le Goater if (region->mmaps[i].mmap) {
332499e53ccSCédric Le Goater vfio_subregion_unmap(region, i);
333499e53ccSCédric Le Goater }
334499e53ccSCédric Le Goater }
335499e53ccSCédric Le Goater }
336499e53ccSCédric Le Goater
vfio_region_exit(VFIORegion * region)337499e53ccSCédric Le Goater void vfio_region_exit(VFIORegion *region)
338499e53ccSCédric Le Goater {
339499e53ccSCédric Le Goater int i;
340499e53ccSCédric Le Goater
341499e53ccSCédric Le Goater if (!region->mem) {
342499e53ccSCédric Le Goater return;
343499e53ccSCédric Le Goater }
344499e53ccSCédric Le Goater
345499e53ccSCédric Le Goater for (i = 0; i < region->nr_mmaps; i++) {
346499e53ccSCédric Le Goater if (region->mmaps[i].mmap) {
347499e53ccSCédric Le Goater memory_region_del_subregion(region->mem, ®ion->mmaps[i].mem);
348499e53ccSCédric Le Goater }
349499e53ccSCédric Le Goater }
350499e53ccSCédric Le Goater
351499e53ccSCédric Le Goater trace_vfio_region_exit(region->vbasedev->name, region->nr);
352499e53ccSCédric Le Goater }
353499e53ccSCédric Le Goater
vfio_region_finalize(VFIORegion * region)354499e53ccSCédric Le Goater void vfio_region_finalize(VFIORegion *region)
355499e53ccSCédric Le Goater {
356499e53ccSCédric Le Goater int i;
357499e53ccSCédric Le Goater
358499e53ccSCédric Le Goater if (!region->mem) {
359499e53ccSCédric Le Goater return;
360499e53ccSCédric Le Goater }
361499e53ccSCédric Le Goater
362499e53ccSCédric Le Goater for (i = 0; i < region->nr_mmaps; i++) {
363499e53ccSCédric Le Goater if (region->mmaps[i].mmap) {
364499e53ccSCédric Le Goater munmap(region->mmaps[i].mmap, region->mmaps[i].size);
365499e53ccSCédric Le Goater object_unparent(OBJECT(®ion->mmaps[i].mem));
366499e53ccSCédric Le Goater }
367499e53ccSCédric Le Goater }
368499e53ccSCédric Le Goater
369499e53ccSCédric Le Goater object_unparent(OBJECT(region->mem));
370499e53ccSCédric Le Goater
371499e53ccSCédric Le Goater g_free(region->mem);
372499e53ccSCédric Le Goater g_free(region->mmaps);
373499e53ccSCédric Le Goater
374499e53ccSCédric Le Goater trace_vfio_region_finalize(region->vbasedev->name, region->nr);
375499e53ccSCédric Le Goater
376499e53ccSCédric Le Goater region->mem = NULL;
377499e53ccSCédric Le Goater region->mmaps = NULL;
378499e53ccSCédric Le Goater region->nr_mmaps = 0;
379499e53ccSCédric Le Goater region->size = 0;
380499e53ccSCédric Le Goater region->flags = 0;
381499e53ccSCédric Le Goater region->nr = 0;
382499e53ccSCédric Le Goater }
383499e53ccSCédric Le Goater
vfio_region_mmaps_set_enabled(VFIORegion * region,bool enabled)384499e53ccSCédric Le Goater void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled)
385499e53ccSCédric Le Goater {
386499e53ccSCédric Le Goater int i;
387499e53ccSCédric Le Goater
388499e53ccSCédric Le Goater if (!region->mem) {
389499e53ccSCédric Le Goater return;
390499e53ccSCédric Le Goater }
391499e53ccSCédric Le Goater
392499e53ccSCédric Le Goater for (i = 0; i < region->nr_mmaps; i++) {
393499e53ccSCédric Le Goater if (region->mmaps[i].mmap) {
394499e53ccSCédric Le Goater memory_region_set_enabled(®ion->mmaps[i].mem, enabled);
395499e53ccSCédric Le Goater }
396499e53ccSCédric Le Goater }
397499e53ccSCédric Le Goater
398499e53ccSCédric Le Goater trace_vfio_region_mmaps_set_enabled(memory_region_name(region->mem),
399499e53ccSCédric Le Goater enabled);
400499e53ccSCédric Le Goater }
401