xref: /qemu/include/system/iommufd.h (revision 563ac3d18129a2770a285cc16c20ad50c8adc7c0)
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     Error *cpr_blocker;/* set if be does not support CPR */
36     uint32_t users;
37 
38     /*< public >*/
39 };
40 
41 bool iommufd_backend_connect(IOMMUFDBackend *be, Error **errp);
42 void iommufd_backend_disconnect(IOMMUFDBackend *be);
43 
44 bool iommufd_backend_alloc_ioas(IOMMUFDBackend *be, uint32_t *ioas_id,
45                                 Error **errp);
46 void iommufd_backend_free_id(IOMMUFDBackend *be, uint32_t id);
47 int iommufd_backend_map_file_dma(IOMMUFDBackend *be, uint32_t ioas_id,
48                                  hwaddr iova, ram_addr_t size, int fd,
49                                  unsigned long start, bool readonly);
50 int iommufd_backend_map_dma(IOMMUFDBackend *be, uint32_t ioas_id, hwaddr iova,
51                             ram_addr_t size, void *vaddr, bool readonly);
52 int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id,
53                               hwaddr iova, ram_addr_t size);
54 bool iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,
55                                      uint32_t *type, void *data, uint32_t len,
56                                      uint64_t *caps, Error **errp);
57 bool iommufd_backend_alloc_hwpt(IOMMUFDBackend *be, uint32_t dev_id,
58                                 uint32_t pt_id, uint32_t flags,
59                                 uint32_t data_type, uint32_t data_len,
60                                 void *data_ptr, uint32_t *out_hwpt,
61                                 Error **errp);
62 bool iommufd_backend_set_dirty_tracking(IOMMUFDBackend *be, uint32_t hwpt_id,
63                                         bool start, Error **errp);
64 bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be, uint32_t hwpt_id,
65                                       uint64_t iova, ram_addr_t size,
66                                       uint64_t page_size, uint64_t *data,
67                                       Error **errp);
68 bool iommufd_backend_invalidate_cache(IOMMUFDBackend *be, uint32_t id,
69                                       uint32_t data_type, uint32_t entry_len,
70                                       uint32_t *entry_num, void *data,
71                                       Error **errp);
72 
73 bool iommufd_change_process_capable(IOMMUFDBackend *be);
74 bool iommufd_change_process(IOMMUFDBackend *be, Error **errp);
75 
76 #define TYPE_HOST_IOMMU_DEVICE_IOMMUFD TYPE_HOST_IOMMU_DEVICE "-iommufd"
77 OBJECT_DECLARE_TYPE(HostIOMMUDeviceIOMMUFD, HostIOMMUDeviceIOMMUFDClass,
78                     HOST_IOMMU_DEVICE_IOMMUFD)
79 
80 /* Overload of the host IOMMU device for the iommufd backend */
81 struct HostIOMMUDeviceIOMMUFD {
82     HostIOMMUDevice parent_obj;
83 
84     IOMMUFDBackend *iommufd;
85     uint32_t devid;
86     uint32_t hwpt_id;
87 };
88 
89 struct HostIOMMUDeviceIOMMUFDClass {
90     HostIOMMUDeviceClass parent_class;
91 
92     /**
93      * @attach_hwpt: attach host IOMMU device to IOMMUFD hardware page table.
94      * VFIO and VDPA device can have different implementation.
95      *
96      * Mandatory callback.
97      *
98      * @idev: host IOMMU device backed by IOMMUFD backend.
99      *
100      * @hwpt_id: ID of IOMMUFD hardware page table.
101      *
102      * @errp: pass an Error out when attachment fails.
103      *
104      * Returns: true on success, false on failure.
105      */
106     bool (*attach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, uint32_t hwpt_id,
107                         Error **errp);
108     /**
109      * @detach_hwpt: detach host IOMMU device from IOMMUFD hardware page table.
110      * VFIO and VDPA device can have different implementation.
111      *
112      * Mandatory callback.
113      *
114      * @idev: host IOMMU device backed by IOMMUFD backend.
115      *
116      * @errp: pass an Error out when attachment fails.
117      *
118      * Returns: true on success, false on failure.
119      */
120     bool (*detach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, Error **errp);
121 };
122 
123 bool host_iommu_device_iommufd_attach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
124                                            uint32_t hwpt_id, Error **errp);
125 bool host_iommu_device_iommufd_detach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
126                                            Error **errp);
127 #endif
128