1 /*
2  * Host IOMMU device abstract declaration
3  *
4  * Copyright (C) 2024 Intel Corporation.
5  *
6  * Authors: Zhenzhong Duan <zhenzhong.duan@intel.com>
7  *
8  * This work is licensed under the terms of the GNU GPL, version 2.  See
9  * the COPYING file in the top-level directory.
10  */
11 
12 #ifndef HOST_IOMMU_DEVICE_H
13 #define HOST_IOMMU_DEVICE_H
14 
15 #include "qom/object.h"
16 #include "qapi/error.h"
17 
18 /**
19  * struct HostIOMMUDeviceCaps - Define host IOMMU device capabilities.
20  *
21  * @type: host platform IOMMU type.
22  */
23 typedef struct HostIOMMUDeviceCaps {
24     uint32_t type;
25 } HostIOMMUDeviceCaps;
26 
27 #define TYPE_HOST_IOMMU_DEVICE "host-iommu-device"
28 OBJECT_DECLARE_TYPE(HostIOMMUDevice, HostIOMMUDeviceClass, HOST_IOMMU_DEVICE)
29 
30 struct HostIOMMUDevice {
31     Object parent_obj;
32 
33     char *name;
34     void *agent; /* pointer to agent device, ie. VFIO or VDPA device */
35     PCIBus *aliased_bus;
36     int aliased_devfn;
37     HostIOMMUDeviceCaps caps;
38 };
39 
40 /**
41  * struct HostIOMMUDeviceClass - The base class for all host IOMMU devices.
42  *
43  * Different types of host devices (e.g., VFIO or VDPA device) or devices
44  * with different backend (e.g., VFIO legacy container or IOMMUFD backend)
45  * will have different implementations of the HostIOMMUDeviceClass.
46  */
47 struct HostIOMMUDeviceClass {
48     ObjectClass parent_class;
49 
50     /**
51      * @realize: initialize host IOMMU device instance further.
52      *
53      * Mandatory callback.
54      *
55      * @hiod: pointer to a host IOMMU device instance.
56      *
57      * @opaque: pointer to agent device of this host IOMMU device,
58      *          e.g., VFIO base device or VDPA device.
59      *
60      * @errp: pass an Error out when realize fails.
61      *
62      * Returns: true on success, false on failure.
63      */
64     bool (*realize)(HostIOMMUDevice *hiod, void *opaque, Error **errp);
65     /**
66      * @get_cap: check if a host IOMMU device capability is supported.
67      *
68      * Optional callback, if not implemented, hint not supporting query
69      * of @cap.
70      *
71      * @hiod: pointer to a host IOMMU device instance.
72      *
73      * @cap: capability to check.
74      *
75      * @errp: pass an Error out when fails to query capability.
76      *
77      * Returns: <0 on failure, 0 if a @cap is unsupported, or else
78      * 1 or some positive value for some special @cap,
79      * i.e., HOST_IOMMU_DEVICE_CAP_AW_BITS.
80      */
81     int (*get_cap)(HostIOMMUDevice *hiod, int cap, Error **errp);
82     /**
83      * @get_iova_ranges: Return the list of usable iova_ranges along with
84      * @hiod Host IOMMU device
85      *
86      * @hiod: handle to the host IOMMU device
87      */
88     GList* (*get_iova_ranges)(HostIOMMUDevice *hiod);
89     /**
90      *
91      * @get_page_size_mask: Return the page size mask supported along this
92      * @hiod Host IOMMU device
93      *
94      * @hiod: handle to the host IOMMU device
95      */
96     uint64_t (*get_page_size_mask)(HostIOMMUDevice *hiod);
97 };
98 
99 /*
100  * Host IOMMU device capability list.
101  */
102 #define HOST_IOMMU_DEVICE_CAP_IOMMU_TYPE        0
103 #define HOST_IOMMU_DEVICE_CAP_AW_BITS           1
104 
105 #define HOST_IOMMU_DEVICE_CAP_AW_BITS_MAX       64
106 #endif
107