svm.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright © 2015 Intel Corporation.
  4. *
  5. * Authors: David Woodhouse <dwmw2@infradead.org>
  6. */
  7. #include <linux/mmu_notifier.h>
  8. #include <linux/sched.h>
  9. #include <linux/sched/mm.h>
  10. #include <linux/slab.h>
  11. #include <linux/rculist.h>
  12. #include <linux/pci.h>
  13. #include <linux/pci-ats.h>
  14. #include <linux/dmar.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/mm_types.h>
  17. #include <linux/xarray.h>
  18. #include <asm/page.h>
  19. #include <asm/fpu/api.h>
  20. #include "iommu.h"
  21. #include "pasid.h"
  22. #include "perf.h"
  23. #include "../iommu-pages.h"
  24. #include "trace.h"
  25. void intel_svm_check(struct intel_iommu *iommu)
  26. {
  27. if (!pasid_supported(iommu))
  28. return;
  29. if (cpu_feature_enabled(X86_FEATURE_GBPAGES) &&
  30. !cap_fl1gp_support(iommu->cap)) {
  31. pr_err("%s SVM disabled, incompatible 1GB page capability\n",
  32. iommu->name);
  33. return;
  34. }
  35. if (cpu_feature_enabled(X86_FEATURE_LA57) &&
  36. !cap_fl5lp_support(iommu->cap)) {
  37. pr_err("%s SVM disabled, incompatible paging mode\n",
  38. iommu->name);
  39. return;
  40. }
  41. iommu->flags |= VTD_FLAG_SVM_CAPABLE;
  42. }
  43. /* Pages have been freed at this point */
  44. static void intel_arch_invalidate_secondary_tlbs(struct mmu_notifier *mn,
  45. struct mm_struct *mm,
  46. unsigned long start, unsigned long end)
  47. {
  48. struct dmar_domain *domain = container_of(mn, struct dmar_domain, notifier);
  49. if (start == 0 && end == ULONG_MAX) {
  50. cache_tag_flush_all(domain);
  51. return;
  52. }
  53. /*
  54. * The mm_types defines vm_end as the first byte after the end address,
  55. * different from IOMMU subsystem using the last address of an address
  56. * range.
  57. */
  58. cache_tag_flush_range(domain, start, end - 1, 0);
  59. }
  60. static void intel_mm_release(struct mmu_notifier *mn, struct mm_struct *mm)
  61. {
  62. struct dmar_domain *domain = container_of(mn, struct dmar_domain, notifier);
  63. struct dev_pasid_info *dev_pasid;
  64. struct device_domain_info *info;
  65. unsigned long flags;
  66. /* This might end up being called from exit_mmap(), *before* the page
  67. * tables are cleared. And __mmu_notifier_release() will delete us from
  68. * the list of notifiers so that our invalidate_range() callback doesn't
  69. * get called when the page tables are cleared. So we need to protect
  70. * against hardware accessing those page tables.
  71. *
  72. * We do it by clearing the entry in the PASID table and then flushing
  73. * the IOTLB and the PASID table caches. This might upset hardware;
  74. * perhaps we'll want to point the PASID to a dummy PGD (like the zero
  75. * page) so that we end up taking a fault that the hardware really
  76. * *has* to handle gracefully without affecting other processes.
  77. */
  78. spin_lock_irqsave(&domain->lock, flags);
  79. list_for_each_entry(dev_pasid, &domain->dev_pasids, link_domain) {
  80. info = dev_iommu_priv_get(dev_pasid->dev);
  81. intel_pasid_tear_down_entry(info->iommu, dev_pasid->dev,
  82. dev_pasid->pasid, true);
  83. }
  84. spin_unlock_irqrestore(&domain->lock, flags);
  85. }
  86. static void intel_mm_free_notifier(struct mmu_notifier *mn)
  87. {
  88. struct dmar_domain *domain = container_of(mn, struct dmar_domain, notifier);
  89. kfree(domain->qi_batch);
  90. kfree(domain);
  91. }
  92. static const struct mmu_notifier_ops intel_mmuops = {
  93. .release = intel_mm_release,
  94. .arch_invalidate_secondary_tlbs = intel_arch_invalidate_secondary_tlbs,
  95. .free_notifier = intel_mm_free_notifier,
  96. };
  97. static int intel_iommu_sva_supported(struct device *dev)
  98. {
  99. struct device_domain_info *info = dev_iommu_priv_get(dev);
  100. struct intel_iommu *iommu;
  101. if (!info || dmar_disabled)
  102. return -EINVAL;
  103. iommu = info->iommu;
  104. if (!iommu)
  105. return -EINVAL;
  106. if (!(iommu->flags & VTD_FLAG_SVM_CAPABLE))
  107. return -ENODEV;
  108. if (!info->pasid_enabled || !info->ats_enabled)
  109. return -EINVAL;
  110. /*
  111. * Devices having device-specific I/O fault handling should not
  112. * support PCI/PRI. The IOMMU side has no means to check the
  113. * capability of device-specific IOPF. Therefore, IOMMU can only
  114. * default that if the device driver enables SVA on a non-PRI
  115. * device, it will handle IOPF in its own way.
  116. */
  117. if (!info->pri_supported)
  118. return 0;
  119. /* Devices supporting PRI should have it enabled. */
  120. if (!info->pri_enabled)
  121. return -EINVAL;
  122. return 0;
  123. }
  124. static int intel_svm_set_dev_pasid(struct iommu_domain *domain,
  125. struct device *dev, ioasid_t pasid,
  126. struct iommu_domain *old)
  127. {
  128. struct device_domain_info *info = dev_iommu_priv_get(dev);
  129. struct intel_iommu *iommu = info->iommu;
  130. struct mm_struct *mm = domain->mm;
  131. struct dev_pasid_info *dev_pasid;
  132. unsigned long sflags;
  133. int ret = 0;
  134. ret = intel_iommu_sva_supported(dev);
  135. if (ret)
  136. return ret;
  137. dev_pasid = domain_add_dev_pasid(domain, dev, pasid);
  138. if (IS_ERR(dev_pasid))
  139. return PTR_ERR(dev_pasid);
  140. /* SVA with non-IOMMU/PRI IOPF handling is allowed. */
  141. if (info->pri_supported) {
  142. ret = iopf_for_domain_replace(domain, old, dev);
  143. if (ret)
  144. goto out_remove_dev_pasid;
  145. }
  146. /* Setup the pasid table: */
  147. sflags = cpu_feature_enabled(X86_FEATURE_LA57) ? PASID_FLAG_FL5LP : 0;
  148. sflags |= PASID_FLAG_PWSNP;
  149. ret = __domain_setup_first_level(iommu, dev, pasid,
  150. FLPT_DEFAULT_DID, __pa(mm->pgd),
  151. sflags, old);
  152. if (ret)
  153. goto out_unwind_iopf;
  154. domain_remove_dev_pasid(old, dev, pasid);
  155. return 0;
  156. out_unwind_iopf:
  157. if (info->pri_supported)
  158. iopf_for_domain_replace(old, domain, dev);
  159. out_remove_dev_pasid:
  160. domain_remove_dev_pasid(domain, dev, pasid);
  161. return ret;
  162. }
  163. static void intel_svm_domain_free(struct iommu_domain *domain)
  164. {
  165. struct dmar_domain *dmar_domain = to_dmar_domain(domain);
  166. /* dmar_domain free is deferred to the mmu free_notifier callback. */
  167. mmu_notifier_put(&dmar_domain->notifier);
  168. }
  169. static const struct iommu_domain_ops intel_svm_domain_ops = {
  170. .set_dev_pasid = intel_svm_set_dev_pasid,
  171. .free = intel_svm_domain_free
  172. };
  173. struct iommu_domain *intel_svm_domain_alloc(struct device *dev,
  174. struct mm_struct *mm)
  175. {
  176. struct dmar_domain *domain;
  177. int ret;
  178. ret = intel_iommu_sva_supported(dev);
  179. if (ret)
  180. return ERR_PTR(ret);
  181. domain = kzalloc_obj(*domain);
  182. if (!domain)
  183. return ERR_PTR(-ENOMEM);
  184. domain->domain.ops = &intel_svm_domain_ops;
  185. INIT_LIST_HEAD(&domain->dev_pasids);
  186. INIT_LIST_HEAD(&domain->cache_tags);
  187. spin_lock_init(&domain->cache_lock);
  188. spin_lock_init(&domain->lock);
  189. domain->notifier.ops = &intel_mmuops;
  190. ret = mmu_notifier_register(&domain->notifier, mm);
  191. if (ret) {
  192. kfree(domain);
  193. return ERR_PTR(ret);
  194. }
  195. return &domain->domain;
  196. }