amd-iommu.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
  4. * Author: Joerg Roedel <[email protected]>
  5. * Leo Duran <[email protected]>
  6. */
  7. #ifndef _ASM_X86_AMD_IOMMU_H
  8. #define _ASM_X86_AMD_IOMMU_H
  9. #include <linux/types.h>
  10. struct amd_iommu;
  11. /*
  12. * This is mainly used to communicate information back-and-forth
  13. * between SVM and IOMMU for setting up and tearing down posted
  14. * interrupt
  15. */
  16. struct amd_iommu_pi_data {
  17. u32 ga_tag;
  18. u32 prev_ga_tag;
  19. u64 base;
  20. bool is_guest_mode;
  21. struct vcpu_data *vcpu_data;
  22. void *ir_data;
  23. };
  24. #ifdef CONFIG_AMD_IOMMU
  25. struct task_struct;
  26. struct pci_dev;
  27. extern int amd_iommu_detect(void);
  28. extern int amd_iommu_init_hardware(void);
  29. /**
  30. * amd_iommu_init_device() - Init device for use with IOMMUv2 driver
  31. * @pdev: The PCI device to initialize
  32. * @pasids: Number of PASIDs to support for this device
  33. *
  34. * This function does all setup for the device pdev so that it can be
  35. * used with IOMMUv2.
  36. * Returns 0 on success or negative value on error.
  37. */
  38. extern int amd_iommu_init_device(struct pci_dev *pdev, int pasids);
  39. /**
  40. * amd_iommu_free_device() - Free all IOMMUv2 related device resources
  41. * and disable IOMMUv2 usage for this device
  42. * @pdev: The PCI device to disable IOMMUv2 usage for'
  43. */
  44. extern void amd_iommu_free_device(struct pci_dev *pdev);
  45. /**
  46. * amd_iommu_bind_pasid() - Bind a given task to a PASID on a device
  47. * @pdev: The PCI device to bind the task to
  48. * @pasid: The PASID on the device the task should be bound to
  49. * @task: the task to bind
  50. *
  51. * The function returns 0 on success or a negative value on error.
  52. */
  53. extern int amd_iommu_bind_pasid(struct pci_dev *pdev, u32 pasid,
  54. struct task_struct *task);
  55. /**
  56. * amd_iommu_unbind_pasid() - Unbind a PASID from its task on
  57. * a device
  58. * @pdev: The device of the PASID
  59. * @pasid: The PASID to unbind
  60. *
  61. * When this function returns the device is no longer using the PASID
  62. * and the PASID is no longer bound to its task.
  63. */
  64. extern void amd_iommu_unbind_pasid(struct pci_dev *pdev, u32 pasid);
  65. /**
  66. * amd_iommu_set_invalid_ppr_cb() - Register a call-back for failed
  67. * PRI requests
  68. * @pdev: The PCI device the call-back should be registered for
  69. * @cb: The call-back function
  70. *
  71. * The IOMMUv2 driver invokes this call-back when it is unable to
  72. * successfully handle a PRI request. The device driver can then decide
  73. * which PRI response the device should see. Possible return values for
  74. * the call-back are:
  75. *
  76. * - AMD_IOMMU_INV_PRI_RSP_SUCCESS - Send SUCCESS back to the device
  77. * - AMD_IOMMU_INV_PRI_RSP_INVALID - Send INVALID back to the device
  78. * - AMD_IOMMU_INV_PRI_RSP_FAIL - Send Failure back to the device,
  79. * the device is required to disable
  80. * PRI when it receives this response
  81. *
  82. * The function returns 0 on success or negative value on error.
  83. */
  84. #define AMD_IOMMU_INV_PRI_RSP_SUCCESS 0
  85. #define AMD_IOMMU_INV_PRI_RSP_INVALID 1
  86. #define AMD_IOMMU_INV_PRI_RSP_FAIL 2
  87. typedef int (*amd_iommu_invalid_ppr_cb)(struct pci_dev *pdev,
  88. u32 pasid,
  89. unsigned long address,
  90. u16);
  91. extern int amd_iommu_set_invalid_ppr_cb(struct pci_dev *pdev,
  92. amd_iommu_invalid_ppr_cb cb);
  93. #define PPR_FAULT_EXEC (1 << 1)
  94. #define PPR_FAULT_READ (1 << 2)
  95. #define PPR_FAULT_WRITE (1 << 5)
  96. #define PPR_FAULT_USER (1 << 6)
  97. #define PPR_FAULT_RSVD (1 << 7)
  98. #define PPR_FAULT_GN (1 << 8)
  99. /**
  100. * amd_iommu_device_info() - Get information about IOMMUv2 support of a
  101. * PCI device
  102. * @pdev: PCI device to query information from
  103. * @info: A pointer to an amd_iommu_device_info structure which will contain
  104. * the information about the PCI device
  105. *
  106. * Returns 0 on success, negative value on error
  107. */
  108. #define AMD_IOMMU_DEVICE_FLAG_ATS_SUP 0x1 /* ATS feature supported */
  109. #define AMD_IOMMU_DEVICE_FLAG_PRI_SUP 0x2 /* PRI feature supported */
  110. #define AMD_IOMMU_DEVICE_FLAG_PASID_SUP 0x4 /* PASID context supported */
  111. #define AMD_IOMMU_DEVICE_FLAG_EXEC_SUP 0x8 /* Device may request execution
  112. on memory pages */
  113. #define AMD_IOMMU_DEVICE_FLAG_PRIV_SUP 0x10 /* Device may request
  114. super-user privileges */
  115. struct amd_iommu_device_info {
  116. int max_pasids;
  117. u32 flags;
  118. };
  119. extern int amd_iommu_device_info(struct pci_dev *pdev,
  120. struct amd_iommu_device_info *info);
  121. /**
  122. * amd_iommu_set_invalidate_ctx_cb() - Register a call-back for invalidating
  123. * a pasid context. This call-back is
  124. * invoked when the IOMMUv2 driver needs to
  125. * invalidate a PASID context, for example
  126. * because the task that is bound to that
  127. * context is about to exit.
  128. *
  129. * @pdev: The PCI device the call-back should be registered for
  130. * @cb: The call-back function
  131. */
  132. typedef void (*amd_iommu_invalidate_ctx)(struct pci_dev *pdev, u32 pasid);
  133. extern int amd_iommu_set_invalidate_ctx_cb(struct pci_dev *pdev,
  134. amd_iommu_invalidate_ctx cb);
  135. #else /* CONFIG_AMD_IOMMU */
  136. static inline int amd_iommu_detect(void) { return -ENODEV; }
  137. #endif /* CONFIG_AMD_IOMMU */
  138. #if defined(CONFIG_AMD_IOMMU) && defined(CONFIG_IRQ_REMAP)
  139. /* IOMMU AVIC Function */
  140. extern int amd_iommu_register_ga_log_notifier(int (*notifier)(u32));
  141. extern int
  142. amd_iommu_update_ga(int cpu, bool is_run, void *data);
  143. extern int amd_iommu_activate_guest_mode(void *data);
  144. extern int amd_iommu_deactivate_guest_mode(void *data);
  145. #else /* defined(CONFIG_AMD_IOMMU) && defined(CONFIG_IRQ_REMAP) */
  146. static inline int
  147. amd_iommu_register_ga_log_notifier(int (*notifier)(u32))
  148. {
  149. return 0;
  150. }
  151. static inline int
  152. amd_iommu_update_ga(int cpu, bool is_run, void *data)
  153. {
  154. return 0;
  155. }
  156. static inline int amd_iommu_activate_guest_mode(void *data)
  157. {
  158. return 0;
  159. }
  160. static inline int amd_iommu_deactivate_guest_mode(void *data)
  161. {
  162. return 0;
  163. }
  164. #endif /* defined(CONFIG_AMD_IOMMU) && defined(CONFIG_IRQ_REMAP) */
  165. int amd_iommu_get_num_iommus(void);
  166. bool amd_iommu_pc_supported(void);
  167. u8 amd_iommu_pc_get_max_banks(unsigned int idx);
  168. u8 amd_iommu_pc_get_max_counters(unsigned int idx);
  169. int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn,
  170. u64 *value);
  171. int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn,
  172. u64 *value);
  173. struct amd_iommu *get_amd_iommu(unsigned int idx);
  174. #ifdef CONFIG_AMD_MEM_ENCRYPT
  175. int amd_iommu_snp_enable(void);
  176. #endif
  177. #endif /* _ASM_X86_AMD_IOMMU_H */