nested.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. #define pr_fmt(fmt) "DMAR: " fmt
  12. #include <linux/iommu.h>
  13. #include <linux/pci.h>
  14. #include <linux/pci-ats.h>
  15. #include "iommu.h"
  16. #include "pasid.h"
  17. static int intel_nested_attach_dev(struct iommu_domain *domain,
  18. struct device *dev, struct iommu_domain *old)
  19. {
  20. struct device_domain_info *info = dev_iommu_priv_get(dev);
  21. struct dmar_domain *dmar_domain = to_dmar_domain(domain);
  22. struct intel_iommu *iommu = info->iommu;
  23. unsigned long flags;
  24. int ret = 0;
  25. device_block_translation(dev);
  26. /*
  27. * Stage-1 domain cannot work alone, it is nested on a s2_domain.
  28. * The s2_domain will be used in nested translation, hence needs
  29. * to ensure the s2_domain is compatible with this IOMMU.
  30. */
  31. ret = paging_domain_compatible(&dmar_domain->s2_domain->domain, dev);
  32. if (ret) {
  33. dev_err_ratelimited(dev, "s2 domain is not compatible\n");
  34. return ret;
  35. }
  36. ret = domain_attach_iommu(dmar_domain, iommu);
  37. if (ret) {
  38. dev_err_ratelimited(dev, "Failed to attach domain to iommu\n");
  39. return ret;
  40. }
  41. ret = cache_tag_assign_domain(dmar_domain, dev, IOMMU_NO_PASID);
  42. if (ret)
  43. goto detach_iommu;
  44. ret = iopf_for_domain_set(domain, dev);
  45. if (ret)
  46. goto unassign_tag;
  47. ret = intel_pasid_setup_nested(iommu, dev,
  48. IOMMU_NO_PASID, dmar_domain);
  49. if (ret)
  50. goto disable_iopf;
  51. info->domain = dmar_domain;
  52. info->domain_attached = true;
  53. spin_lock_irqsave(&dmar_domain->lock, flags);
  54. list_add(&info->link, &dmar_domain->devices);
  55. spin_unlock_irqrestore(&dmar_domain->lock, flags);
  56. return 0;
  57. disable_iopf:
  58. iopf_for_domain_remove(domain, dev);
  59. unassign_tag:
  60. cache_tag_unassign_domain(dmar_domain, dev, IOMMU_NO_PASID);
  61. detach_iommu:
  62. domain_detach_iommu(dmar_domain, iommu);
  63. return ret;
  64. }
  65. static void intel_nested_domain_free(struct iommu_domain *domain)
  66. {
  67. struct dmar_domain *dmar_domain = to_dmar_domain(domain);
  68. struct dmar_domain *s2_domain = dmar_domain->s2_domain;
  69. spin_lock(&s2_domain->s1_lock);
  70. list_del(&dmar_domain->s2_link);
  71. spin_unlock(&s2_domain->s1_lock);
  72. kfree(dmar_domain->qi_batch);
  73. kfree(dmar_domain);
  74. }
  75. static int intel_nested_cache_invalidate_user(struct iommu_domain *domain,
  76. struct iommu_user_data_array *array)
  77. {
  78. struct dmar_domain *dmar_domain = to_dmar_domain(domain);
  79. struct iommu_hwpt_vtd_s1_invalidate inv_entry;
  80. u32 index, processed = 0;
  81. int ret = 0;
  82. if (array->type != IOMMU_HWPT_INVALIDATE_DATA_VTD_S1) {
  83. ret = -EINVAL;
  84. goto out;
  85. }
  86. for (index = 0; index < array->entry_num; index++) {
  87. ret = iommu_copy_struct_from_user_array(&inv_entry, array,
  88. IOMMU_HWPT_INVALIDATE_DATA_VTD_S1,
  89. index, __reserved);
  90. if (ret)
  91. break;
  92. if ((inv_entry.flags & ~IOMMU_VTD_INV_FLAGS_LEAF) ||
  93. inv_entry.__reserved) {
  94. ret = -EOPNOTSUPP;
  95. break;
  96. }
  97. if (!IS_ALIGNED(inv_entry.addr, VTD_PAGE_SIZE) ||
  98. ((inv_entry.npages == U64_MAX) && inv_entry.addr)) {
  99. ret = -EINVAL;
  100. break;
  101. }
  102. cache_tag_flush_range(dmar_domain, inv_entry.addr,
  103. inv_entry.addr + nrpages_to_size(inv_entry.npages) - 1,
  104. inv_entry.flags & IOMMU_VTD_INV_FLAGS_LEAF);
  105. processed++;
  106. }
  107. out:
  108. array->entry_num = processed;
  109. return ret;
  110. }
  111. static int domain_setup_nested(struct intel_iommu *iommu,
  112. struct dmar_domain *domain,
  113. struct device *dev, ioasid_t pasid,
  114. struct iommu_domain *old)
  115. {
  116. if (old)
  117. intel_pasid_tear_down_entry(iommu, dev, pasid, false);
  118. return intel_pasid_setup_nested(iommu, dev, pasid, domain);
  119. }
  120. static int intel_nested_set_dev_pasid(struct iommu_domain *domain,
  121. struct device *dev, ioasid_t pasid,
  122. struct iommu_domain *old)
  123. {
  124. struct device_domain_info *info = dev_iommu_priv_get(dev);
  125. struct dmar_domain *dmar_domain = to_dmar_domain(domain);
  126. struct intel_iommu *iommu = info->iommu;
  127. struct dev_pasid_info *dev_pasid;
  128. int ret;
  129. if (!pasid_supported(iommu) || dev_is_real_dma_subdevice(dev))
  130. return -EOPNOTSUPP;
  131. if (context_copied(iommu, info->bus, info->devfn))
  132. return -EBUSY;
  133. ret = paging_domain_compatible(&dmar_domain->s2_domain->domain, dev);
  134. if (ret)
  135. return ret;
  136. dev_pasid = domain_add_dev_pasid(domain, dev, pasid);
  137. if (IS_ERR(dev_pasid))
  138. return PTR_ERR(dev_pasid);
  139. ret = iopf_for_domain_replace(domain, old, dev);
  140. if (ret)
  141. goto out_remove_dev_pasid;
  142. ret = domain_setup_nested(iommu, dmar_domain, dev, pasid, old);
  143. if (ret)
  144. goto out_unwind_iopf;
  145. domain_remove_dev_pasid(old, dev, pasid);
  146. return 0;
  147. out_unwind_iopf:
  148. iopf_for_domain_replace(old, domain, dev);
  149. out_remove_dev_pasid:
  150. domain_remove_dev_pasid(domain, dev, pasid);
  151. return ret;
  152. }
  153. static const struct iommu_domain_ops intel_nested_domain_ops = {
  154. .attach_dev = intel_nested_attach_dev,
  155. .set_dev_pasid = intel_nested_set_dev_pasid,
  156. .free = intel_nested_domain_free,
  157. .cache_invalidate_user = intel_nested_cache_invalidate_user,
  158. };
  159. struct iommu_domain *
  160. intel_iommu_domain_alloc_nested(struct device *dev, struct iommu_domain *parent,
  161. u32 flags,
  162. const struct iommu_user_data *user_data)
  163. {
  164. struct device_domain_info *info = dev_iommu_priv_get(dev);
  165. struct dmar_domain *s2_domain = to_dmar_domain(parent);
  166. struct intel_iommu *iommu = info->iommu;
  167. struct iommu_hwpt_vtd_s1 vtd;
  168. struct dmar_domain *domain;
  169. int ret;
  170. if (!nested_supported(iommu) || flags & ~IOMMU_HWPT_ALLOC_PASID)
  171. return ERR_PTR(-EOPNOTSUPP);
  172. /* Must be nested domain */
  173. if (user_data->type != IOMMU_HWPT_DATA_VTD_S1)
  174. return ERR_PTR(-EOPNOTSUPP);
  175. if (!intel_domain_is_ss_paging(s2_domain) || !s2_domain->nested_parent)
  176. return ERR_PTR(-EINVAL);
  177. ret = iommu_copy_struct_from_user(&vtd, user_data,
  178. IOMMU_HWPT_DATA_VTD_S1, __reserved);
  179. if (ret)
  180. return ERR_PTR(ret);
  181. domain = kzalloc_obj(*domain, GFP_KERNEL_ACCOUNT);
  182. if (!domain)
  183. return ERR_PTR(-ENOMEM);
  184. domain->s2_domain = s2_domain;
  185. domain->s1_cfg = vtd;
  186. domain->domain.ops = &intel_nested_domain_ops;
  187. domain->domain.type = IOMMU_DOMAIN_NESTED;
  188. INIT_LIST_HEAD(&domain->devices);
  189. INIT_LIST_HEAD(&domain->dev_pasids);
  190. INIT_LIST_HEAD(&domain->cache_tags);
  191. spin_lock_init(&domain->lock);
  192. spin_lock_init(&domain->cache_lock);
  193. xa_init(&domain->iommu_array);
  194. spin_lock(&s2_domain->s1_lock);
  195. list_add(&domain->s2_link, &s2_domain->s1_domains);
  196. spin_unlock(&s2_domain->s1_lock);
  197. return &domain->domain;
  198. }