| 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 "system/ram_addr.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 | /* |
| 42 | * Virtual IOMMU object that represents physical IOMMU's virtualization |
| 43 | * support |
| 44 | */ |
| 45 | typedef struct IOMMUFDViommu { |
| 46 | IOMMUFDBackend *iommufd; |
| 47 | uint32_t s2_hwpt_id; /* ID of stage 2 HWPT */ |
| 48 | uint32_t viommu_id; /* virtual IOMMU ID of allocated object */ |
| 49 | } IOMMUFDViommu; |
| 50 | |
| 51 | /* |
| 52 | * Virtual device object for a physical device bind to a vIOMMU. |
| 53 | */ |
| 54 | typedef struct IOMMUFDVdev { |
| 55 | uint32_t vdevice_id; /* object handle for vDevice */ |
| 56 | uint32_t virt_id; /* virtual device ID */ |
| 57 | } IOMMUFDVdev; |
| 58 | |
| 59 | /* Virtual event queue interface for a vIOMMU */ |
| 60 | typedef struct IOMMUFDVeventq { |
| 61 | uint32_t veventq_id; |
| 62 | uint32_t veventq_fd; |
| 63 | uint32_t last_event_seq; /* Sequence number of last processed event */ |
| 64 | bool event_start; /* True after first valid event; cleared on overflow */ |
| 65 | } IOMMUFDVeventq; |
| 66 | |
| 67 | /* HW queue object for a vIOMMU-specific HW-accelerated queue */ |
| 68 | typedef struct IOMMUFDHWqueue { |
| 69 | IOMMUFDViommu *viommu; |
| 70 | uint32_t hw_queue_id; |
| 71 | } IOMMUFDHWqueue; |
| 72 | |
| 73 | bool iommufd_backend_connect(IOMMUFDBackend *be, Error **errp); |
| 74 | void iommufd_backend_disconnect(IOMMUFDBackend *be); |
| 75 | |
| 76 | bool iommufd_backend_alloc_ioas(IOMMUFDBackend *be, uint32_t *ioas_id, |
| 77 | Error **errp); |
| 78 | void iommufd_backend_free_id(IOMMUFDBackend *be, uint32_t id); |
| 79 | int iommufd_backend_map_file_dma(IOMMUFDBackend *be, uint32_t ioas_id, |
| 80 | hwaddr iova, uint64_t size, int fd, |
| 81 | unsigned long start, bool readonly); |
| 82 | int iommufd_backend_map_dma(IOMMUFDBackend *be, uint32_t ioas_id, hwaddr iova, |
| 83 | uint64_t size, void *vaddr, bool readonly); |
| 84 | int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id, |
| 85 | hwaddr iova, uint64_t size); |
| 86 | bool iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid, |
| 87 | uint32_t *type, void *data, uint32_t len, |
| 88 | uint64_t *caps, uint8_t *max_pasid_log2, |
| 89 | Error **errp); |
| 90 | bool iommufd_backend_alloc_hwpt(IOMMUFDBackend *be, uint32_t dev_id, |
| 91 | uint32_t pt_id, uint32_t flags, |
| 92 | uint32_t data_type, uint32_t data_len, |
| 93 | void *data_ptr, uint32_t *out_hwpt, |
| 94 | Error **errp); |
| 95 | bool iommufd_backend_alloc_viommu(IOMMUFDBackend *be, uint32_t dev_id, |
| 96 | uint32_t viommu_type, uint32_t hwpt_id, |
| 97 | void *data_ptr, uint32_t data_len, |
| 98 | uint32_t *out_hwpt, Error **errp); |
| 99 | |
| 100 | bool iommufd_backend_alloc_vdev(IOMMUFDBackend *be, uint32_t dev_id, |
| 101 | uint32_t viommu_id, uint64_t virt_id, |
| 102 | uint32_t *out_vdev_id, Error **errp); |
| 103 | |
| 104 | bool iommufd_backend_alloc_veventq(IOMMUFDBackend *be, uint32_t viommu_id, |
| 105 | uint32_t type, uint32_t depth, |
| 106 | uint32_t *out_veventq_id, |
| 107 | uint32_t *out_veventq_fd, Error **errp); |
| 108 | |
| 109 | bool iommufd_backend_alloc_hw_queue(IOMMUFDBackend *be, uint32_t viommu_id, |
| 110 | uint32_t queue_type, uint32_t index, |
| 111 | uint64_t addr, uint64_t length, |
| 112 | uint32_t *out_hw_queue_id, Error **errp); |
| 113 | |
| 114 | bool iommufd_backend_viommu_mmap(IOMMUFDBackend *be, uint32_t viommu_id, |
| 115 | uint64_t size, off_t offset, void **out_ptr, |
| 116 | Error **errp); |
| 117 | |
| 118 | bool iommufd_backend_set_dirty_tracking(IOMMUFDBackend *be, uint32_t hwpt_id, |
| 119 | bool start, Error **errp); |
| 120 | bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be, uint32_t hwpt_id, |
| 121 | uint64_t iova, ram_addr_t size, |
| 122 | uint64_t page_size, uint64_t *data, |
| 123 | uint64_t flags, Error **errp); |
| 124 | bool iommufd_backend_invalidate_cache(IOMMUFDBackend *be, uint32_t id, |
| 125 | uint32_t data_type, uint32_t entry_len, |
| 126 | uint32_t *entry_num, void *data, |
| 127 | Error **errp); |
| 128 | |
| 129 | bool iommufd_change_process_capable(IOMMUFDBackend *be); |
| 130 | bool iommufd_change_process(IOMMUFDBackend *be, Error **errp); |
| 131 | |
| 132 | #define TYPE_HOST_IOMMU_DEVICE_IOMMUFD TYPE_HOST_IOMMU_DEVICE "-iommufd" |
| 133 | OBJECT_DECLARE_TYPE(HostIOMMUDeviceIOMMUFD, HostIOMMUDeviceIOMMUFDClass, |
| 134 | HOST_IOMMU_DEVICE_IOMMUFD) |
| 135 | |
| 136 | /* Overload of the host IOMMU device for the iommufd backend */ |
| 137 | struct HostIOMMUDeviceIOMMUFD { |
| 138 | HostIOMMUDevice parent_obj; |
| 139 | |
| 140 | IOMMUFDBackend *iommufd; |
| 141 | uint32_t devid; |
| 142 | uint32_t hwpt_id; |
| 143 | }; |
| 144 | |
| 145 | struct HostIOMMUDeviceIOMMUFDClass { |
| 146 | HostIOMMUDeviceClass parent_class; |
| 147 | |
| 148 | /** |
| 149 | * @attach_hwpt: attach host IOMMU device to IOMMUFD hardware page table. |
| 150 | * VFIO and VDPA device can have different implementation. |
| 151 | * |
| 152 | * Mandatory callback. |
| 153 | * |
| 154 | * @hiodi: host IOMMU device backed by IOMMUFD backend. |
| 155 | * |
| 156 | * @pasid: target pasid of the device to be attached. |
| 157 | * |
| 158 | * @hwpt_id: ID of IOMMUFD hardware page table. |
| 159 | * |
| 160 | * @errp: pass an Error out when attachment fails. |
| 161 | * |
| 162 | * Returns: true on success, false on failure. |
| 163 | */ |
| 164 | bool (*attach_hwpt)(HostIOMMUDeviceIOMMUFD *hiodi, uint32_t pasid, |
| 165 | uint32_t hwpt_id, Error **errp); |
| 166 | /** |
| 167 | * @detach_hwpt: detach host IOMMU device from IOMMUFD hardware page table. |
| 168 | * VFIO and VDPA device can have different implementation. |
| 169 | * |
| 170 | * Mandatory callback. |
| 171 | * |
| 172 | * @hiodi: host IOMMU device backed by IOMMUFD backend. |
| 173 | * |
| 174 | * @pasid: target pasid of the device to be detached. |
| 175 | * |
| 176 | * @errp: pass an Error out when detachment fails. |
| 177 | * |
| 178 | * Returns: true on success, false on failure. |
| 179 | */ |
| 180 | bool (*detach_hwpt)(HostIOMMUDeviceIOMMUFD *hiodi, uint32_t pasid, |
| 181 | Error **errp); |
| 182 | }; |
| 183 | |
| 184 | bool host_iommu_device_iommufd_attach_hwpt(HostIOMMUDeviceIOMMUFD *hiodi, |
| 185 | uint32_t pasid, uint32_t hwpt_id, |
| 186 | Error **errp); |
| 187 | bool host_iommu_device_iommufd_detach_hwpt(HostIOMMUDeviceIOMMUFD *hiodi, |
| 188 | uint32_t pasid, Error **errp); |
| 189 | #endif |