Skip to content

Commit 9838f2b

Browse files
yiliu1765jgunthorpe
authored andcommitted
iommu/vt-d: Set the nested domain to a device
This adds the helper for setting the nested domain to a device hence enable nested domain usage on Intel VT-d. Link: https://lore.kernel.org/r/20231026044216.64964-7-yi.l.liu@intel.com Signed-off-by: Jacob Pan <jacob.jun.pan@linux.intel.com> Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com> Signed-off-by: Yi Liu <yi.l.liu@intel.com> Reviewed-by: Kevin Tian <kevin.tian@intel.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
1 parent d86724d commit 9838f2b

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

drivers/iommu/intel/nested.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,69 @@
1212
#define pr_fmt(fmt) "DMAR: " fmt
1313

1414
#include <linux/iommu.h>
15+
#include <linux/pci.h>
16+
#include <linux/pci-ats.h>
1517

1618
#include "iommu.h"
19+
#include "pasid.h"
20+
21+
static int intel_nested_attach_dev(struct iommu_domain *domain,
22+
struct device *dev)
23+
{
24+
struct device_domain_info *info = dev_iommu_priv_get(dev);
25+
struct dmar_domain *dmar_domain = to_dmar_domain(domain);
26+
struct intel_iommu *iommu = info->iommu;
27+
unsigned long flags;
28+
int ret = 0;
29+
30+
if (info->domain)
31+
device_block_translation(dev);
32+
33+
if (iommu->agaw < dmar_domain->s2_domain->agaw) {
34+
dev_err_ratelimited(dev, "Adjusted guest address width not compatible\n");
35+
return -ENODEV;
36+
}
37+
38+
/*
39+
* Stage-1 domain cannot work alone, it is nested on a s2_domain.
40+
* The s2_domain will be used in nested translation, hence needs
41+
* to ensure the s2_domain is compatible with this IOMMU.
42+
*/
43+
ret = prepare_domain_attach_device(&dmar_domain->s2_domain->domain, dev);
44+
if (ret) {
45+
dev_err_ratelimited(dev, "s2 domain is not compatible\n");
46+
return ret;
47+
}
48+
49+
ret = domain_attach_iommu(dmar_domain, iommu);
50+
if (ret) {
51+
dev_err_ratelimited(dev, "Failed to attach domain to iommu\n");
52+
return ret;
53+
}
54+
55+
ret = intel_pasid_setup_nested(iommu, dev,
56+
IOMMU_NO_PASID, dmar_domain);
57+
if (ret) {
58+
domain_detach_iommu(dmar_domain, iommu);
59+
dev_err_ratelimited(dev, "Failed to setup pasid entry\n");
60+
return ret;
61+
}
62+
63+
info->domain = dmar_domain;
64+
spin_lock_irqsave(&dmar_domain->lock, flags);
65+
list_add(&info->link, &dmar_domain->devices);
66+
spin_unlock_irqrestore(&dmar_domain->lock, flags);
67+
68+
return 0;
69+
}
1770

1871
static void intel_nested_domain_free(struct iommu_domain *domain)
1972
{
2073
kfree(to_dmar_domain(domain));
2174
}
2275

2376
static const struct iommu_domain_ops intel_nested_domain_ops = {
77+
.attach_dev = intel_nested_attach_dev,
2478
.free = intel_nested_domain_free,
2579
};
2680

0 commit comments

Comments
 (0)