1 /*
2  * VFIO BASE CONTAINER
3  *
4  * Copyright (C) 2023 Intel Corporation.
5  * Copyright Red Hat, Inc. 2023
6  *
7  * Authors: Yi Liu <yi.l.liu@intel.com>
8  *          Eric Auger <eric.auger@redhat.com>
9  *
10  * SPDX-License-Identifier: GPL-2.0-or-later
11  */
12 
13 #ifndef HW_VFIO_VFIO_CONTAINER_BASE_H
14 #define HW_VFIO_VFIO_CONTAINER_BASE_H
15 
16 #include "exec/memory.h"
17 
18 typedef struct VFIODevice VFIODevice;
19 typedef struct VFIOIOMMUOps VFIOIOMMUOps;
20 
21 typedef struct {
22     unsigned long *bitmap;
23     hwaddr size;
24     hwaddr pages;
25 } VFIOBitmap;
26 
27 /*
28  * This is the base object for vfio container backends
29  */
30 typedef struct VFIOContainerBase {
31     const VFIOIOMMUOps *ops;
32     QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
33 } VFIOContainerBase;
34 
35 typedef struct VFIOGuestIOMMU {
36     VFIOContainerBase *bcontainer;
37     IOMMUMemoryRegion *iommu_mr;
38     hwaddr iommu_offset;
39     IOMMUNotifier n;
40     QLIST_ENTRY(VFIOGuestIOMMU) giommu_next;
41 } VFIOGuestIOMMU;
42 
43 int vfio_container_dma_map(VFIOContainerBase *bcontainer,
44                            hwaddr iova, ram_addr_t size,
45                            void *vaddr, bool readonly);
46 int vfio_container_dma_unmap(VFIOContainerBase *bcontainer,
47                              hwaddr iova, ram_addr_t size,
48                              IOMMUTLBEntry *iotlb);
49 
50 void vfio_container_init(VFIOContainerBase *bcontainer,
51                          const VFIOIOMMUOps *ops);
52 void vfio_container_destroy(VFIOContainerBase *bcontainer);
53 
54 struct VFIOIOMMUOps {
55     /* basic feature */
56     int (*dma_map)(VFIOContainerBase *bcontainer,
57                    hwaddr iova, ram_addr_t size,
58                    void *vaddr, bool readonly);
59     int (*dma_unmap)(VFIOContainerBase *bcontainer,
60                      hwaddr iova, ram_addr_t size,
61                      IOMMUTLBEntry *iotlb);
62     int (*attach_device)(const char *name, VFIODevice *vbasedev,
63                          AddressSpace *as, Error **errp);
64     void (*detach_device)(VFIODevice *vbasedev);
65     /* migration feature */
66     int (*set_dirty_page_tracking)(VFIOContainerBase *bcontainer, bool start);
67     int (*query_dirty_bitmap)(VFIOContainerBase *bcontainer, VFIOBitmap *vbmap,
68                               hwaddr iova, hwaddr size);
69 };
70 #endif /* HW_VFIO_VFIO_CONTAINER_BASE_H */
71