intel-svm.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright © 2015 Intel Corporation.
  3. *
  4. * Authors: David Woodhouse <David.Woodhouse@intel.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. */
  15. #ifndef __INTEL_SVM_H__
  16. #define __INTEL_SVM_H__
  17. struct device;
  18. struct svm_dev_ops {
  19. void (*fault_cb)(struct device *dev, int pasid, u64 address,
  20. u32 private, int rwxp, int response);
  21. };
  22. /* Values for rxwp in fault_cb callback */
  23. #define SVM_REQ_READ (1<<3)
  24. #define SVM_REQ_WRITE (1<<2)
  25. #define SVM_REQ_EXEC (1<<1)
  26. #define SVM_REQ_PRIV (1<<0)
  27. /*
  28. * The SVM_FLAG_PRIVATE_PASID flag requests a PASID which is *not* the "main"
  29. * PASID for the current process. Even if a PASID already exists, a new one
  30. * will be allocated. And the PASID allocated with SVM_FLAG_PRIVATE_PASID
  31. * will not be given to subsequent callers. This facility allows a driver to
  32. * disambiguate between multiple device contexts which access the same MM,
  33. * if there is no other way to do so. It should be used sparingly, if at all.
  34. */
  35. #define SVM_FLAG_PRIVATE_PASID (1<<0)
  36. /*
  37. * The SVM_FLAG_SUPERVISOR_MODE flag requests a PASID which can be used only
  38. * for access to kernel addresses. No IOTLB flushes are automatically done
  39. * for kernel mappings; it is valid only for access to the kernel's static
  40. * 1:1 mapping of physical memory — not to vmalloc or even module mappings.
  41. * A future API addition may permit the use of such ranges, by means of an
  42. * explicit IOTLB flush call (akin to the DMA API's unmap method).
  43. *
  44. * It is unlikely that we will ever hook into flush_tlb_kernel_range() to
  45. * do such IOTLB flushes automatically.
  46. */
  47. #define SVM_FLAG_SUPERVISOR_MODE (1<<1)
  48. #ifdef CONFIG_INTEL_IOMMU_SVM
  49. /**
  50. * intel_svm_bind_mm() - Bind the current process to a PASID
  51. * @dev: Device to be granted acccess
  52. * @pasid: Address for allocated PASID
  53. * @flags: Flags. Later for requesting supervisor mode, etc.
  54. * @ops: Callbacks to device driver
  55. *
  56. * This function attempts to enable PASID support for the given device.
  57. * If the @pasid argument is non-%NULL, a PASID is allocated for access
  58. * to the MM of the current process.
  59. *
  60. * By using a %NULL value for the @pasid argument, this function can
  61. * be used to simply validate that PASID support is available for the
  62. * given device — i.e. that it is behind an IOMMU which has the
  63. * requisite support, and is enabled.
  64. *
  65. * Page faults are handled transparently by the IOMMU code, and there
  66. * should be no need for the device driver to be involved. If a page
  67. * fault cannot be handled (i.e. is an invalid address rather than
  68. * just needs paging in), then the page request will be completed by
  69. * the core IOMMU code with appropriate status, and the device itself
  70. * can then report the resulting fault to its driver via whatever
  71. * mechanism is appropriate.
  72. *
  73. * Multiple calls from the same process may result in the same PASID
  74. * being re-used. A reference count is kept.
  75. */
  76. extern int intel_svm_bind_mm(struct device *dev, int *pasid, int flags,
  77. struct svm_dev_ops *ops);
  78. /**
  79. * intel_svm_unbind_mm() - Unbind a specified PASID
  80. * @dev: Device for which PASID was allocated
  81. * @pasid: PASID value to be unbound
  82. *
  83. * This function allows a PASID to be retired when the device no
  84. * longer requires access to the address space of a given process.
  85. *
  86. * If the use count for the PASID in question reaches zero, the
  87. * PASID is revoked and may no longer be used by hardware.
  88. *
  89. * Device drivers are required to ensure that no access (including
  90. * page requests) is currently outstanding for the PASID in question,
  91. * before calling this function.
  92. */
  93. extern int intel_svm_unbind_mm(struct device *dev, int pasid);
  94. #else /* CONFIG_INTEL_IOMMU_SVM */
  95. static inline int intel_svm_bind_mm(struct device *dev, int *pasid,
  96. int flags, struct svm_dev_ops *ops)
  97. {
  98. return -ENOSYS;
  99. }
  100. static inline int intel_svm_unbind_mm(struct device *dev, int pasid)
  101. {
  102. BUG();
  103. }
  104. #endif /* CONFIG_INTEL_IOMMU_SVM */
  105. #define intel_svm_available(dev) (!intel_svm_bind_mm((dev), NULL, 0, NULL))
  106. #endif /* __INTEL_SVM_H__ */