19
19
20
20
#include <linux/ratelimit.h>
21
21
#include <linux/pci.h>
22
+ #include <linux/acpi.h>
22
23
#include <linux/pci-ats.h>
23
24
#include <linux/bitmap.h>
24
25
#include <linux/slab.h>
@@ -216,13 +217,60 @@ static struct iommu_dev_data *find_dev_data(u16 devid)
216
217
return dev_data ;
217
218
}
218
219
219
- static inline u16 get_device_id (struct device * dev )
220
+ static inline int match_hid_uid (struct device * dev ,
221
+ struct acpihid_map_entry * entry )
222
+ {
223
+ const char * hid , * uid ;
224
+
225
+ hid = acpi_device_hid (ACPI_COMPANION (dev ));
226
+ uid = acpi_device_uid (ACPI_COMPANION (dev ));
227
+
228
+ if (!hid || !(* hid ))
229
+ return - ENODEV ;
230
+
231
+ if (!uid || !(* uid ))
232
+ return strcmp (hid , entry -> hid );
233
+
234
+ if (!(* entry -> uid ))
235
+ return strcmp (hid , entry -> hid );
236
+
237
+ return (strcmp (hid , entry -> hid ) || strcmp (uid , entry -> uid ));
238
+ }
239
+
240
+ static inline u16 get_pci_device_id (struct device * dev )
220
241
{
221
242
struct pci_dev * pdev = to_pci_dev (dev );
222
243
223
244
return PCI_DEVID (pdev -> bus -> number , pdev -> devfn );
224
245
}
225
246
247
+ static inline int get_acpihid_device_id (struct device * dev ,
248
+ struct acpihid_map_entry * * entry )
249
+ {
250
+ struct acpihid_map_entry * p ;
251
+
252
+ list_for_each_entry (p , & acpihid_map , list ) {
253
+ if (!match_hid_uid (dev , p )) {
254
+ if (entry )
255
+ * entry = p ;
256
+ return p -> devid ;
257
+ }
258
+ }
259
+ return - EINVAL ;
260
+ }
261
+
262
+ static inline int get_device_id (struct device * dev )
263
+ {
264
+ int devid ;
265
+
266
+ if (dev_is_pci (dev ))
267
+ devid = get_pci_device_id (dev );
268
+ else
269
+ devid = get_acpihid_device_id (dev , NULL );
270
+
271
+ return devid ;
272
+ }
273
+
226
274
static struct iommu_dev_data * get_dev_data (struct device * dev )
227
275
{
228
276
return dev -> archdata .iommu ;
@@ -303,10 +351,6 @@ static bool check_device(struct device *dev)
303
351
if (!dev || !dev -> dma_mask )
304
352
return false;
305
353
306
- /* No PCI device */
307
- if (!dev_is_pci (dev ))
308
- return false;
309
-
310
354
devid = get_device_id (dev );
311
355
if (IS_ERR_VALUE (devid ))
312
356
return false;
@@ -344,7 +388,6 @@ static void init_iommu_group(struct device *dev)
344
388
345
389
static int iommu_init_device (struct device * dev )
346
390
{
347
- struct pci_dev * pdev = to_pci_dev (dev );
348
391
struct iommu_dev_data * dev_data ;
349
392
int devid ;
350
393
@@ -359,10 +402,10 @@ static int iommu_init_device(struct device *dev)
359
402
if (!dev_data )
360
403
return - ENOMEM ;
361
404
362
- if (pci_iommuv2_capable (pdev )) {
405
+ if (dev_is_pci ( dev ) && pci_iommuv2_capable (to_pci_dev ( dev ) )) {
363
406
struct amd_iommu * iommu ;
364
407
365
- iommu = amd_iommu_rlookup_table [dev_data -> devid ];
408
+ iommu = amd_iommu_rlookup_table [dev_data -> devid ];
366
409
dev_data -> iommu_v2 = iommu -> is_iommu_v2 ;
367
410
}
368
411
@@ -2239,13 +2282,17 @@ static bool pci_pri_tlp_required(struct pci_dev *pdev)
2239
2282
static int attach_device (struct device * dev ,
2240
2283
struct protection_domain * domain )
2241
2284
{
2242
- struct pci_dev * pdev = to_pci_dev ( dev ) ;
2285
+ struct pci_dev * pdev ;
2243
2286
struct iommu_dev_data * dev_data ;
2244
2287
unsigned long flags ;
2245
2288
int ret ;
2246
2289
2247
2290
dev_data = get_dev_data (dev );
2248
2291
2292
+ if (!dev_is_pci (dev ))
2293
+ goto skip_ats_check ;
2294
+
2295
+ pdev = to_pci_dev (dev );
2249
2296
if (domain -> flags & PD_IOMMUV2_MASK ) {
2250
2297
if (!dev_data -> passthrough )
2251
2298
return - EINVAL ;
@@ -2264,6 +2311,7 @@ static int attach_device(struct device *dev,
2264
2311
dev_data -> ats .qdep = pci_ats_queue_depth (pdev );
2265
2312
}
2266
2313
2314
+ skip_ats_check :
2267
2315
write_lock_irqsave (& amd_iommu_devtable_lock , flags );
2268
2316
ret = __attach_device (dev_data , domain );
2269
2317
write_unlock_irqrestore (& amd_iommu_devtable_lock , flags );
@@ -2320,6 +2368,9 @@ static void detach_device(struct device *dev)
2320
2368
__detach_device (dev_data );
2321
2369
write_unlock_irqrestore (& amd_iommu_devtable_lock , flags );
2322
2370
2371
+ if (!dev_is_pci (dev ))
2372
+ return ;
2373
+
2323
2374
if (domain -> flags & PD_IOMMUV2_MASK && dev_data -> iommu_v2 )
2324
2375
pdev_iommuv2_disable (to_pci_dev (dev ));
2325
2376
else if (dev_data -> ats .enabled )
0 commit comments