1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * nested.c - nested mode translation support 4 * 5 * Copyright (C) 2023 Intel Corporation 6 * 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> 10 */ 11 12 #define pr_fmt(fmt) "DMAR: " fmt 13 14 #include <linux/iommu.h> 15 #include <linux/pci.h> 16 #include <linux/pci-ats.h> 17 18 #include "iommu.h" 19 #include "pasid.h" 20 21 static int intel_nested_attach_dev(struct iommu_domain *domain, 22 struct device *dev, struct iommu_domain *old) 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 device_block_translation(dev); 31 32 /* 33 * Stage-1 domain cannot work alone, it is nested on a s2_domain. 34 * The s2_domain will be used in nested translation, hence needs 35 * to ensure the s2_domain is compatible with this IOMMU. 36 */ 37 ret = paging_domain_compatible(&dmar_domain->s2_domain->domain, dev); 38 if (ret) { 39 dev_err_ratelimited(dev, "s2 domain is not compatible\n"); 40 return ret; 41 } 42 43 ret = domain_attach_iommu(dmar_domain, iommu); 44 if (ret) { 45 dev_err_ratelimited(dev, "Failed to attach domain to iommu\n"); 46 return ret; 47 } 48 49 ret = cache_tag_assign_domain(dmar_domain, dev, IOMMU_NO_PASID); 50 if (ret) 51 goto detach_iommu; 52 53 ret = iopf_for_domain_set(domain, dev); 54 if (ret) 55 goto unassign_tag; 56 57 ret = intel_pasid_setup_nested(iommu, dev, 58 IOMMU_NO_PASID, dmar_domain); 59 if (ret) 60 goto disable_iopf; 61 62 info->domain = dmar_domain; 63 info->domain_attached = true; 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 disable_iopf: 70 iopf_for_domain_remove(domain, dev); 71 unassign_tag: 72 cache_tag_unassign_domain(dmar_domain, dev, IOMMU_NO_PASID); 73 detach_iommu: 74 domain_detach_iommu(dmar_domain, iommu); 75 76 return ret; 77 } 78 79 static void intel_nested_domain_free(struct iommu_domain *domain) 80 { 81 struct dmar_domain *dmar_domain = to_dmar_domain(domain); 82 struct dmar_domain *s2_domain = dmar_domain->s2_domain; 83 84 spin_lock(&s2_domain->s1_lock); 85 list_del(&dmar_domain->s2_link); 86 spin_unlock(&s2_domain->s1_lock); 87 kfree(dmar_domain->qi_batch); 88 kfree(dmar_domain); 89 } 90 91 static int intel_nested_cache_invalidate_user(struct iommu_domain *domain, 92 struct iommu_user_data_array *array) 93 { 94 struct dmar_domain *dmar_domain = to_dmar_domain(domain); 95 struct iommu_hwpt_vtd_s1_invalidate inv_entry; 96 u32 processed = 0; 97 int ret = 0; 98 99 if (array->type != IOMMU_HWPT_INVALIDATE_DATA_VTD_S1) { 100 ret = -EINVAL; 101 goto out; 102 } 103 104 /* 105 * The core re-invokes this op for the remaining requests, so handle one 106 * request per call. A zero-length array only probes the type, validated 107 * above. 108 */ 109 if (!array->entry_num) 110 return 0; 111 112 ret = iommu_copy_struct_from_user_array( 113 &inv_entry, array, IOMMU_HWPT_INVALIDATE_DATA_VTD_S1, 0, 114 __reserved); 115 if (ret) 116 goto out; 117 118 if ((inv_entry.flags & ~IOMMU_VTD_INV_FLAGS_LEAF) || 119 inv_entry.__reserved) { 120 ret = -EOPNOTSUPP; 121 goto out; 122 } 123 124 if (!IS_ALIGNED(inv_entry.addr, VTD_PAGE_SIZE) || 125 (inv_entry.npages == U64_MAX && inv_entry.addr)) { 126 ret = -EINVAL; 127 goto out; 128 } 129 130 cache_tag_flush_range(dmar_domain, inv_entry.addr, 131 inv_entry.addr + 132 nrpages_to_size(inv_entry.npages) - 1, 133 inv_entry.flags & IOMMU_VTD_INV_FLAGS_LEAF); 134 processed = 1; 135 out: 136 array->entry_num = processed; 137 return ret; 138 } 139 140 static int domain_setup_nested(struct intel_iommu *iommu, 141 struct dmar_domain *domain, 142 struct device *dev, ioasid_t pasid, 143 struct iommu_domain *old) 144 { 145 if (old) 146 intel_pasid_tear_down_entry(iommu, dev, pasid, false); 147 148 return intel_pasid_setup_nested(iommu, dev, pasid, domain); 149 } 150 151 static int intel_nested_set_dev_pasid(struct iommu_domain *domain, 152 struct device *dev, ioasid_t pasid, 153 struct iommu_domain *old) 154 { 155 struct device_domain_info *info = dev_iommu_priv_get(dev); 156 struct dmar_domain *dmar_domain = to_dmar_domain(domain); 157 struct intel_iommu *iommu = info->iommu; 158 struct dev_pasid_info *dev_pasid; 159 int ret; 160 161 if (!pasid_supported(iommu) || dev_is_real_dma_subdevice(dev)) 162 return -EOPNOTSUPP; 163 164 if (context_copied(iommu, info->bus, info->devfn)) 165 return -EBUSY; 166 167 ret = paging_domain_compatible(&dmar_domain->s2_domain->domain, dev); 168 if (ret) 169 return ret; 170 171 dev_pasid = domain_add_dev_pasid(domain, dev, pasid); 172 if (IS_ERR(dev_pasid)) 173 return PTR_ERR(dev_pasid); 174 175 ret = iopf_for_domain_replace(domain, old, dev); 176 if (ret) 177 goto out_remove_dev_pasid; 178 179 ret = domain_setup_nested(iommu, dmar_domain, dev, pasid, old); 180 if (ret) 181 goto out_unwind_iopf; 182 183 domain_remove_dev_pasid(old, dev, pasid); 184 185 return 0; 186 187 out_unwind_iopf: 188 iopf_for_domain_replace(old, domain, dev); 189 out_remove_dev_pasid: 190 domain_remove_dev_pasid(domain, dev, pasid); 191 return ret; 192 } 193 194 static const struct iommu_domain_ops intel_nested_domain_ops = { 195 .attach_dev = intel_nested_attach_dev, 196 .set_dev_pasid = intel_nested_set_dev_pasid, 197 .free = intel_nested_domain_free, 198 .cache_invalidate_user = intel_nested_cache_invalidate_user, 199 }; 200 201 struct iommu_domain * 202 intel_iommu_domain_alloc_nested(struct device *dev, struct iommu_domain *parent, 203 u32 flags, 204 const struct iommu_user_data *user_data) 205 { 206 struct device_domain_info *info = dev_iommu_priv_get(dev); 207 struct dmar_domain *s2_domain = to_dmar_domain(parent); 208 struct intel_iommu *iommu = info->iommu; 209 struct iommu_hwpt_vtd_s1 vtd; 210 struct dmar_domain *domain; 211 int ret; 212 213 if (!nested_supported(iommu) || flags & ~IOMMU_HWPT_ALLOC_PASID) 214 return ERR_PTR(-EOPNOTSUPP); 215 216 /* Must be nested domain */ 217 if (user_data->type != IOMMU_HWPT_DATA_VTD_S1) 218 return ERR_PTR(-EOPNOTSUPP); 219 if (!intel_domain_is_ss_paging(s2_domain) || !s2_domain->nested_parent) 220 return ERR_PTR(-EINVAL); 221 222 ret = iommu_copy_struct_from_user(&vtd, user_data, 223 IOMMU_HWPT_DATA_VTD_S1, __reserved); 224 if (ret) 225 return ERR_PTR(ret); 226 227 domain = kzalloc_obj(*domain, GFP_KERNEL_ACCOUNT); 228 if (!domain) 229 return ERR_PTR(-ENOMEM); 230 231 domain->s2_domain = s2_domain; 232 domain->s1_cfg = vtd; 233 domain->domain.ops = &intel_nested_domain_ops; 234 domain->domain.type = IOMMU_DOMAIN_NESTED; 235 INIT_LIST_HEAD(&domain->devices); 236 INIT_LIST_HEAD(&domain->dev_pasids); 237 INIT_LIST_HEAD(&domain->cache_tags); 238 spin_lock_init(&domain->lock); 239 spin_lock_init(&domain->cache_lock); 240 xa_init(&domain->iommu_array); 241 242 spin_lock(&s2_domain->s1_lock); 243 list_add(&domain->s2_link, &s2_domain->s1_domains); 244 spin_unlock(&s2_domain->s1_lock); 245 246 return &domain->domain; 247 } 248