@samitouri / QOSamiQemu / commits / 4101df1201

intel_iommu_accel: Switch to VTDAccelPASIDCacheEntry for PASID bind/unbind and PIOTLB invalidation

This patch switches from VTDAddressSpace to VTDAccelPASIDCacheEntry for handling PASID bind/unbind operations and PIOTLB invalidations in passthrough scenarios. VTDAccelPASIDCacheEntry was introduced to cache PASID entries for passthrough devices and is now ready to propagate PASID bind/unbind operations and PIOTLB invalidations to the host. Unlike the previous approach, VTDAccelPASIDCacheEntry supports both IOMMU_NO_PASID (rid_pasid) and other valid PASIDs, so this switch drops IOMMU_NO_PASID limitations that existed in the prior PASID bind/unbind and PIOTLB invalidation path. For IOMMU_NO_PASID of passthrough devices, VTDAddressSpace continues to handle shadow page modifications to the host, but no longer manages PASID bind/unbind operations or PIOTLB invalidations for passthrough scenarios. Co-developed-by: Yi Liu <yi.l.liu@intel.com> Signed-off-by: Yi Liu <yi.l.liu@intel.com> Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com> Tested-by: Xudong Hao <xudong.hao@intel.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Message-Id: <20260527054658.1021096-16-zhenzhong.duan@intel.com>

Zhenzhong Duan committed May 27, 2026 at 01:46 UTC 4101df1201fa60eb3a0932485881453661a2edbf
4 files changed +68 -84
hw/i386/intel_iommu.c
+1 -16
@@ -86,8 +86,6 @@ static void vtd_pasid_cache_reset_locked(IntelIOMMUState *s)
86 VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
87 if (pc_entry->valid) {
88 pc_entry->valid = false;
89 - /* It's fatal to get failure during reset */
90 - vtd_propagate_guest_pasid(vtd_as, &error_fatal);
89 }
90 }
91 }
@@ -3104,8 +3102,6 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
3102 VTDPASIDEntry pe;
3103 IOMMUNotifier *n;
3104 uint16_t did;
3107 - const char *err_prefix = "Attaching to HWPT failed: ";
3108 - Error *local_err = NULL;
3105
3106 if (vtd_dev_get_pe_from_pasid(vtd_as->iommu_state, vtd_as->bus,
3107 vtd_as->devfn, vtd_as->pasid, &pe)) {
@@ -3127,9 +3123,6 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
3123 vtd_address_space_unmap(vtd_as, n);
3124 }
3125 vtd_switch_address_space(vtd_as);
3130 -
3131 - err_prefix = "Detaching from HWPT failed: ";
3132 - goto do_bind_unbind;
3126 }
3127
3128 /*
@@ -3157,20 +3150,12 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
3150 if (!pc_entry->valid) {
3151 pc_entry->pasid_entry = pe;
3152 pc_entry->valid = true;
3160 - } else if (vtd_pasid_entry_compare(&pe, &pc_entry->pasid_entry)) {
3161 - err_prefix = "Replacing HWPT attachment failed: ";
3162 - } else {
3153 + } else if (!vtd_pasid_entry_compare(&pe, &pc_entry->pasid_entry)) {
3154 return;
3155 }
3156
3157 vtd_switch_address_space(vtd_as);
3158 vtd_address_space_sync(vtd_as);
3168 -
3169 -do_bind_unbind:
3170 - /* TODO: Fault event injection into guest, report error to QEMU for now */
3171 - if (!vtd_propagate_guest_pasid(vtd_as, &local_err)) {
3172 - error_reportf_err(local_err, "%s", err_prefix);
3173 - }
3159 }
3160
3161 static void vtd_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info)
hw/i386/intel_iommu_accel.c
+66 -65
@@ -89,24 +89,25 @@ static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,
89 fs_hwpt_id, errp);
90 }
91
92 -static void vtd_destroy_old_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,
93 - VTDAddressSpace *vtd_as)
92 +static void vtd_destroy_old_fs_hwpt(VTDAccelPASIDCacheEntry *vtd_pce)
93 {
95 - HostIOMMUDeviceIOMMUFD *hiodi = HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
94 + HostIOMMUDeviceIOMMUFD *hiodi =
95 + HOST_IOMMU_DEVICE_IOMMUFD(vtd_pce->vtd_hiod->hiod);
96
97 - if (!vtd_as->fs_hwpt_id) {
97 + if (!vtd_pce->fs_hwpt_id) {
98 return;
99 }
100 - iommufd_backend_free_id(hiodi->iommufd, vtd_as->fs_hwpt_id);
101 - vtd_as->fs_hwpt_id = 0;
100 + iommufd_backend_free_id(hiodi->iommufd, vtd_pce->fs_hwpt_id);
101 + vtd_pce->fs_hwpt_id = 0;
102 }
103
104 -static bool vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
105 - VTDAddressSpace *vtd_as, Error **errp)
104 +static bool vtd_device_attach_iommufd(VTDAccelPASIDCacheEntry *vtd_pce,
105 + Error **errp)
106 {
107 + VTDHostIOMMUDevice *vtd_hiod = vtd_pce->vtd_hiod;
108 HostIOMMUDeviceIOMMUFD *hiodi = HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
108 - VTDPASIDEntry *pe = &vtd_as->pasid_cache_entry.pasid_entry;
109 - uint32_t hwpt_id = hiodi->hwpt_id;
109 + VTDPASIDEntry *pe = &vtd_pce->pasid_entry;
110 + uint32_t hwpt_id = hiodi->hwpt_id, pasid = vtd_pce->pasid;
111 bool ret;
112
113 /*
@@ -126,14 +127,13 @@ static bool vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
127 }
128 }
129
129 - ret = host_iommu_device_iommufd_attach_hwpt(hiodi, IOMMU_NO_PASID, hwpt_id,
130 - errp);
131 - trace_vtd_device_attach_hwpt(hiodi->devid, IOMMU_NO_PASID, hwpt_id, ret);
130 + ret = host_iommu_device_iommufd_attach_hwpt(hiodi, pasid, hwpt_id, errp);
131 + trace_vtd_device_attach_hwpt(hiodi->devid, pasid, hwpt_id, ret);
132 if (ret) {
133 /* Destroy old fs_hwpt if it's a replacement */
134 - vtd_destroy_old_fs_hwpt(vtd_hiod, vtd_as);
134 + vtd_destroy_old_fs_hwpt(vtd_pce);
135 if (vtd_pe_pgtt_is_fst(pe)) {
136 - vtd_as->fs_hwpt_id = hwpt_id;
136 + vtd_pce->fs_hwpt_id = hwpt_id;
137 }
138 } else if (vtd_pe_pgtt_is_fst(pe)) {
139 iommufd_backend_free_id(hiodi->iommufd, hwpt_id);
@@ -142,17 +142,19 @@ static bool vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
142 return ret;
143 }
144
145 -static bool vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
146 - VTDAddressSpace *vtd_as, Error **errp)
145 +static bool vtd_device_detach_iommufd(VTDAccelPASIDCacheEntry *vtd_pce,
146 + Error **errp)
147 {
148 + VTDHostIOMMUDevice *vtd_hiod = vtd_pce->vtd_hiod;
149 HostIOMMUDeviceIOMMUFD *hiodi = HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
149 - IntelIOMMUState *s = vtd_as->iommu_state;
150 +
151 + IntelIOMMUState *s = vtd_hiod->iommu_state;
152 + uint32_t pasid = vtd_pce->pasid;
153 bool ret;
154
152 - if (s->dmar_enabled && s->root_scalable) {
153 - ret = host_iommu_device_iommufd_detach_hwpt(hiodi, IOMMU_NO_PASID,
154 - errp);
155 - trace_vtd_device_detach_hwpt(hiodi->devid, IOMMU_NO_PASID, ret);
155 + if (pasid != IOMMU_NO_PASID || (s->dmar_enabled && s->root_scalable)) {
156 + ret = host_iommu_device_iommufd_detach_hwpt(hiodi, pasid, errp);
157 + trace_vtd_device_detach_hwpt(hiodi->devid, pasid, ret);
158 } else {
159 /*
160 * If DMAR remapping is disabled or guest switches to legacy mode,
@@ -166,58 +168,32 @@ static bool vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
168 }
169
170 if (ret) {
169 - vtd_destroy_old_fs_hwpt(vtd_hiod, vtd_as);
171 + vtd_destroy_old_fs_hwpt(vtd_pce);
172 }
173
174 return ret;
175 }
176
175 -bool vtd_propagate_guest_pasid(VTDAddressSpace *vtd_as, Error **errp)
176 -{
177 - VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
178 - VTDHostIOMMUDevice *vtd_hiod = vtd_find_hiod_iommufd(vtd_as);
179 -
180 - /* Ignore emulated device or legacy VFIO backed device */
181 - if (!vtd_as->iommu_state->fsts || !vtd_hiod) {
182 - return true;
183 - }
184 -
185 - if (pc_entry->valid) {
186 - return vtd_device_attach_iommufd(vtd_hiod, vtd_as, errp);
187 - }
188 -
189 - return vtd_device_detach_iommufd(vtd_hiod, vtd_as, errp);
190 -}
191 -
177 /*
193 - * This function is a loop function for the s->vtd_address_spaces
194 - * list with VTDPIOTLBInvInfo as execution filter. It propagates
195 - * the piotlb invalidation to host.
178 + * This function is a loop function for the s->vtd_host_iommu_dev
179 + * and vtd_hiod->pasid_cache_list lists with VTDPIOTLBInvInfo as
180 + * execution filter. It propagates the piotlb invalidation to host.
181 */
197 -static void vtd_flush_host_piotlb_locked(gpointer key, gpointer value,
198 - gpointer user_data)
182 +static void vtd_flush_host_piotlb_locked(VTDAccelPASIDCacheEntry *vtd_pce,
183 + VTDPIOTLBInvInfo *piotlb_info)
184 {
200 - VTDPIOTLBInvInfo *piotlb_info = user_data;
201 - VTDAddressSpace *vtd_as = value;
202 - VTDHostIOMMUDevice *vtd_hiod = vtd_find_hiod_iommufd(vtd_as);
203 - VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
185 + VTDHostIOMMUDevice *vtd_hiod = vtd_pce->vtd_hiod;
186 + VTDPASIDEntry *pe = &vtd_pce->pasid_entry;
187 uint16_t did;
188
206 - if (!vtd_hiod) {
207 - return;
208 - }
209 -
210 - assert(vtd_as->pasid == IOMMU_NO_PASID);
211 -
189 /* Nothing to do if there is no first stage HWPT attached */
213 - if (!pc_entry->valid ||
214 - !vtd_pe_pgtt_is_fst(&pc_entry->pasid_entry)) {
190 + if (!vtd_pe_pgtt_is_fst(pe)) {
191 return;
192 }
193
218 - did = VTD_SM_PASID_ENTRY_DID(&pc_entry->pasid_entry);
194 + did = VTD_SM_PASID_ENTRY_DID(pe);
195
220 - if (piotlb_info->domain_id == did && piotlb_info->pasid == IOMMU_NO_PASID) {
196 + if (piotlb_info->domain_id == did && piotlb_info->pasid == vtd_pce->pasid) {
197 HostIOMMUDeviceIOMMUFD *hiodi =
198 HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
199 uint32_t entry_num = 1; /* Only implement one request for simplicity */
@@ -225,7 +201,7 @@ static void vtd_flush_host_piotlb_locked(gpointer key, gpointer value,
201 struct iommu_hwpt_vtd_s1_invalidate *cache = piotlb_info->inv_data;
202
203 if (!iommufd_backend_invalidate_cache(hiodi->iommufd,
228 - vtd_as->fs_hwpt_id,
204 + vtd_pce->fs_hwpt_id,
205 IOMMU_HWPT_INVALIDATE_DATA_VTD_S1,
206 sizeof(*cache), &entry_num, cache,
207 &local_err)) {
@@ -241,6 +217,8 @@ void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t domain_id,
217 {
218 struct iommu_hwpt_vtd_s1_invalidate cache_info = { 0 };
219 VTDPIOTLBInvInfo piotlb_info;
220 + VTDHostIOMMUDevice *vtd_hiod;
221 + GHashTableIter hiod_it;
222
223 cache_info.addr = addr;
224 cache_info.npages = npages;
@@ -251,23 +229,36 @@ void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t domain_id,
229 piotlb_info.inv_data = &cache_info;
230
231 /*
254 - * Go through each vtd_as instance in s->vtd_address_spaces, find out
255 - * affected host devices which need host piotlb invalidation. Piotlb
256 - * invalidation should check pasid cache per architecture point of view.
232 + * Go through each vtd_pce in vtd_hiod->pasid_cache_list for each host
233 + * device, find out affected host device pasid which need host piotlb
234 + * invalidation. Piotlb invalidation should check pasid cache per
235 + * architecture point of view.
236 */
258 - g_hash_table_foreach(s->vtd_address_spaces,
259 - vtd_flush_host_piotlb_locked, &piotlb_info);
237 + g_hash_table_iter_init(&hiod_it, s->vtd_host_iommu_dev);
238 + while (g_hash_table_iter_next(&hiod_it, NULL, (void **)&vtd_hiod)) {
239 + VTDAccelPASIDCacheEntry *vtd_pce;
240 +
241 + QLIST_FOREACH(vtd_pce, &vtd_hiod->pasid_cache_list, next) {
242 + vtd_flush_host_piotlb_locked(vtd_pce, &piotlb_info);
243 + }
244 + }
245 }
246
247 static void vtd_accel_fill_pc(VTDHostIOMMUDevice *vtd_hiod, uint32_t pasid,
248 VTDPASIDEntry *pe)
249 {
250 VTDAccelPASIDCacheEntry *vtd_pce;
251 + Error *local_err = NULL;
252
253 QLIST_FOREACH(vtd_pce, &vtd_hiod->pasid_cache_list, next) {
254 if (vtd_pce->pasid == pasid) {
255 if (vtd_pasid_entry_compare(pe, &vtd_pce->pasid_entry)) {
256 vtd_pce->pasid_entry = *pe;
257 +
258 + if (!vtd_device_attach_iommufd(vtd_pce, &local_err)) {
259 + error_reportf_err(local_err, "%s",
260 + "Replacing HWPT attachment failed: ");
261 + }
262 }
263 return;
264 }
@@ -278,11 +269,21 @@ static void vtd_accel_fill_pc(VTDHostIOMMUDevice *vtd_hiod, uint32_t pasid,
269 vtd_pce->pasid = pasid;
270 vtd_pce->pasid_entry = *pe;
271 QLIST_INSERT_HEAD(&vtd_hiod->pasid_cache_list, vtd_pce, next);
272 +
273 + if (!vtd_device_attach_iommufd(vtd_pce, &local_err)) {
274 + error_reportf_err(local_err, "%s", "Attaching to HWPT failed: ");
275 + }
276 }
277
278 static void vtd_accel_delete_pc(VTDAccelPASIDCacheEntry *vtd_pce,
279 VTDPASIDCacheInfo *pc_info)
280 {
281 + Error *local_err = NULL;
282 +
283 + if (!vtd_device_detach_iommufd(vtd_pce, &local_err)) {
284 + error_reportf_err(local_err, "%s", "Detaching from HWPT failed: ");
285 + }
286 +
287 QLIST_REMOVE(vtd_pce, next);
288 g_free(vtd_pce);
289
hw/i386/intel_iommu_accel.h
+1 -1
@@ -16,6 +16,7 @@ typedef struct VTDAccelPASIDCacheEntry {
16 VTDHostIOMMUDevice *vtd_hiod;
17 VTDPASIDEntry pasid_entry;
18 uint32_t pasid;
19 + uint32_t fs_hwpt_id;
20 QLIST_ENTRY(VTDAccelPASIDCacheEntry) next;
21 } VTDAccelPASIDCacheEntry;
22
@@ -23,7 +24,6 @@ typedef struct VTDAccelPASIDCacheEntry {
24 bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod,
25 Error **errp);
26 VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as);
26 -bool vtd_propagate_guest_pasid(VTDAddressSpace *vtd_as, Error **errp);
27 void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t domain_id,
28 uint32_t pasid, hwaddr addr,
29 uint64_t npages, bool ih);
include/hw/i386/intel_iommu.h
-2
@@ -154,8 +154,6 @@ struct VTDAddressSpace {
154 * with the guest IOMMU pgtables for a device.
155 */
156 IOVATree *iova_tree;
157 -
158 - uint32_t fs_hwpt_id;
157 };
158
159 struct VTDIOTLBEntry {