xref: /qemu/include/system/iommufd.h (revision 2a53c4f5c534a1ab825ba03e0d3ec45a7c2b90d8)
1 /*
2  * iommufd container backend declaration
3  *
4  * Copyright (C) 2024 Intel Corporation.
5  * Copyright Red Hat, Inc. 2024
6  *
7  * Authors: Yi Liu <yi.l.liu@intel.com>
8  *          Eric Auger <eric.auger@redhat.com>
9  *          Zhenzhong Duan <zhenzhong.duan@intel.com>
10  *
11  * SPDX-License-Identifier: GPL-2.0-or-later
12  */
13 
14 #ifndef SYSTEM_IOMMUFD_H
15 #define SYSTEM_IOMMUFD_H
16 
17 #include "qom/object.h"
18 #include "exec/hwaddr.h"
19 #include "exec/cpu-common.h"
20 #include "system/host_iommu_device.h"
21 
22 #define TYPE_IOMMUFD_BACKEND "iommufd"
23 OBJECT_DECLARE_TYPE(IOMMUFDBackend, IOMMUFDBackendClass, IOMMUFD_BACKEND)
24 
25 struct IOMMUFDBackendClass {
26     ObjectClass parent_class;
27 };
28 
29 struct IOMMUFDBackend {
30     Object parent;
31 
32     /*< protected >*/
33     int fd;            /* /dev/iommu file descriptor */
34     bool owned;        /* is the /dev/iommu opened internally */
35     uint32_t users;
36 
37     /*< public >*/
38 };
39 
40 bool iommufd_backend_connect(IOMMUFDBackend *be, Error **errp);
41 void iommufd_backend_disconnect(IOMMUFDBackend *be);
42 
43 bool iommufd_backend_alloc_ioas(IOMMUFDBackend *be, uint32_t *ioas_id,
44                                 Error **errp);
45 void iommufd_backend_free_id(IOMMUFDBackend *be, uint32_t id);
46 int iommufd_backend_map_dma(IOMMUFDBackend *be, uint32_t ioas_id, hwaddr iova,
47                             ram_addr_t size, void *vaddr, bool readonly);
48 int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id,
49                               hwaddr iova, ram_addr_t size);
50 bool iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,
51                                      uint32_t *type, void *data, uint32_t len,
52                                      uint64_t *caps, Error **errp);
53 bool iommufd_backend_alloc_hwpt(IOMMUFDBackend *be, uint32_t dev_id,
54                                 uint32_t pt_id, uint32_t flags,
55                                 uint32_t data_type, uint32_t data_len,
56                                 void *data_ptr, uint32_t *out_hwpt,
57                                 Error **errp);
58 bool iommufd_backend_set_dirty_tracking(IOMMUFDBackend *be, uint32_t hwpt_id,
59                                         bool start, Error **errp);
60 bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be, uint32_t hwpt_id,
61                                       uint64_t iova, ram_addr_t size,
62                                       uint64_t page_size, uint64_t *data,
63                                       Error **errp);
64 bool iommufd_backend_invalidate_cache(IOMMUFDBackend *be, uint32_t id,
65                                       uint32_t data_type, uint32_t entry_len,
66                                       uint32_t *entry_num, void *data,
67                                       Error **errp);
68 
69 #define TYPE_HOST_IOMMU_DEVICE_IOMMUFD TYPE_HOST_IOMMU_DEVICE "-iommufd"
70 OBJECT_DECLARE_TYPE(HostIOMMUDeviceIOMMUFD, HostIOMMUDeviceIOMMUFDClass,
71                     HOST_IOMMU_DEVICE_IOMMUFD)
72 
73 /* Overload of the host IOMMU device for the iommufd backend */
74 struct HostIOMMUDeviceIOMMUFD {
75     HostIOMMUDevice parent_obj;
76 
77     IOMMUFDBackend *iommufd;
78     uint32_t devid;
79     uint32_t hwpt_id;
80 };
81 
82 struct HostIOMMUDeviceIOMMUFDClass {
83     HostIOMMUDeviceClass parent_class;
84 
85     /**
86      * @attach_hwpt: attach host IOMMU device to IOMMUFD hardware page table.
87      * VFIO and VDPA device can have different implementation.
88      *
89      * Mandatory callback.
90      *
91      * @idev: host IOMMU device backed by IOMMUFD backend.
92      *
93      * @hwpt_id: ID of IOMMUFD hardware page table.
94      *
95      * @errp: pass an Error out when attachment fails.
96      *
97      * Returns: true on success, false on failure.
98      */
99     bool (*attach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, uint32_t hwpt_id,
100                         Error **errp);
101     /**
102      * @detach_hwpt: detach host IOMMU device from IOMMUFD hardware page table.
103      * VFIO and VDPA device can have different implementation.
104      *
105      * Mandatory callback.
106      *
107      * @idev: host IOMMU device backed by IOMMUFD backend.
108      *
109      * @errp: pass an Error out when attachment fails.
110      *
111      * Returns: true on success, false on failure.
112      */
113     bool (*detach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, Error **errp);
114 };
115 
116 bool host_iommu_device_iommufd_attach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
117                                            uint32_t hwpt_id, Error **errp);
118 bool host_iommu_device_iommufd_detach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
119                                            Error **errp);
120 #endif
121