1 // SPDX-License-Identifier: GPL-2.0
3 * nested.c - nested mode translation support
5 * Copyright (C) 2023 Intel Corporation
7 * Author: Lu Baolu <baolu.lu@linux.intel.com>
8 * Jacob Pan <jacob.jun.pan@linux.intel.com>
9 * Yi Liu <yi.l.liu@intel.com>
12 #define pr_fmt(fmt) "DMAR: " fmt
14 #include <linux/iommu.h>
15 #include <linux/pci.h>
16 #include <linux/pci-ats.h>
21 static int intel_nested_attach_dev(struct iommu_domain *domain,
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;
31 device_block_translation(dev);
33 if (iommu->agaw < dmar_domain->s2_domain->agaw) {
34 dev_err_ratelimited(dev, "Adjusted guest address width not compatible\n");
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.
43 ret = prepare_domain_attach_device(&dmar_domain->s2_domain->domain, dev);
45 dev_err_ratelimited(dev, "s2 domain is not compatible\n");
49 ret = domain_attach_iommu(dmar_domain, iommu);
51 dev_err_ratelimited(dev, "Failed to attach domain to iommu\n");
55 ret = intel_pasid_setup_nested(iommu, dev,
56 IOMMU_NO_PASID, dmar_domain);
58 domain_detach_iommu(dmar_domain, iommu);
59 dev_err_ratelimited(dev, "Failed to setup pasid entry\n");
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);
68 domain_update_iotlb(dmar_domain);
73 static void intel_nested_domain_free(struct iommu_domain *domain)
75 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
76 struct dmar_domain *s2_domain = dmar_domain->s2_domain;
78 spin_lock(&s2_domain->s1_lock);
79 list_del(&dmar_domain->s2_link);
80 spin_unlock(&s2_domain->s1_lock);
84 static void nested_flush_dev_iotlb(struct dmar_domain *domain, u64 addr,
87 struct device_domain_info *info;
91 spin_lock_irqsave(&domain->lock, flags);
92 list_for_each_entry(info, &domain->devices, link) {
93 if (!info->ats_enabled)
95 sid = info->bus << 8 | info->devfn;
96 qdep = info->ats_qdep;
97 qi_flush_dev_iotlb(info->iommu, sid, info->pfsid,
99 quirk_extra_dev_tlb_flush(info, addr, mask,
100 IOMMU_NO_PASID, qdep);
102 spin_unlock_irqrestore(&domain->lock, flags);
105 static void intel_nested_flush_cache(struct dmar_domain *domain, u64 addr,
108 struct iommu_domain_info *info;
112 xa_for_each(&domain->iommu_array, i, info)
113 qi_flush_piotlb(info->iommu,
114 domain_id_iommu(domain, info->iommu),
115 IOMMU_NO_PASID, addr, npages, ih);
117 if (!domain->has_iotlb_device)
120 if (npages == U64_MAX)
121 mask = 64 - VTD_PAGE_SHIFT;
123 mask = ilog2(__roundup_pow_of_two(npages));
125 nested_flush_dev_iotlb(domain, addr, mask);
128 static int intel_nested_cache_invalidate_user(struct iommu_domain *domain,
129 struct iommu_user_data_array *array)
131 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
132 struct iommu_hwpt_vtd_s1_invalidate inv_entry;
133 u32 index, processed = 0;
136 if (array->type != IOMMU_HWPT_INVALIDATE_DATA_VTD_S1) {
141 for (index = 0; index < array->entry_num; index++) {
142 ret = iommu_copy_struct_from_user_array(&inv_entry, array,
143 IOMMU_HWPT_INVALIDATE_DATA_VTD_S1,
148 if ((inv_entry.flags & ~IOMMU_VTD_INV_FLAGS_LEAF) ||
149 inv_entry.__reserved) {
154 if (!IS_ALIGNED(inv_entry.addr, VTD_PAGE_SIZE) ||
155 ((inv_entry.npages == U64_MAX) && inv_entry.addr)) {
160 intel_nested_flush_cache(dmar_domain, inv_entry.addr,
162 inv_entry.flags & IOMMU_VTD_INV_FLAGS_LEAF);
167 array->entry_num = processed;
171 static const struct iommu_domain_ops intel_nested_domain_ops = {
172 .attach_dev = intel_nested_attach_dev,
173 .free = intel_nested_domain_free,
174 .cache_invalidate_user = intel_nested_cache_invalidate_user,
177 struct iommu_domain *intel_nested_domain_alloc(struct iommu_domain *parent,
178 const struct iommu_user_data *user_data)
180 struct dmar_domain *s2_domain = to_dmar_domain(parent);
181 struct iommu_hwpt_vtd_s1 vtd;
182 struct dmar_domain *domain;
185 /* Must be nested domain */
186 if (user_data->type != IOMMU_HWPT_DATA_VTD_S1)
187 return ERR_PTR(-EOPNOTSUPP);
188 if (parent->ops != intel_iommu_ops.default_domain_ops ||
189 !s2_domain->nested_parent)
190 return ERR_PTR(-EINVAL);
192 ret = iommu_copy_struct_from_user(&vtd, user_data,
193 IOMMU_HWPT_DATA_VTD_S1, __reserved);
197 domain = kzalloc(sizeof(*domain), GFP_KERNEL_ACCOUNT);
199 return ERR_PTR(-ENOMEM);
201 domain->use_first_level = true;
202 domain->s2_domain = s2_domain;
203 domain->s1_pgtbl = vtd.pgtbl_addr;
204 domain->s1_cfg = vtd;
205 domain->domain.ops = &intel_nested_domain_ops;
206 domain->domain.type = IOMMU_DOMAIN_NESTED;
207 INIT_LIST_HEAD(&domain->devices);
208 INIT_LIST_HEAD(&domain->dev_pasids);
209 spin_lock_init(&domain->lock);
210 xa_init(&domain->iommu_array);
212 spin_lock(&s2_domain->s1_lock);
213 list_add(&domain->s2_link, &s2_domain->s1_domains);
214 spin_unlock(&s2_domain->s1_lock);
216 return &domain->domain;