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
/*
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
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,
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 */
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)) {
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;
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
}
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