dmar.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2006, Intel Corporation.
  4. *
  5. * Copyright (C) Ashok Raj <[email protected]>
  6. * Copyright (C) Shaohua Li <[email protected]>
  7. */
  8. #ifndef __DMAR_H__
  9. #define __DMAR_H__
  10. #include <linux/acpi.h>
  11. #include <linux/types.h>
  12. #include <linux/msi.h>
  13. #include <linux/irqreturn.h>
  14. #include <linux/rwsem.h>
  15. #include <linux/rculist.h>
  16. struct acpi_dmar_header;
  17. #define DMAR_UNITS_SUPPORTED 1024
  18. /* DMAR Flags */
  19. #define DMAR_INTR_REMAP 0x1
  20. #define DMAR_X2APIC_OPT_OUT 0x2
  21. #define DMAR_PLATFORM_OPT_IN 0x4
  22. struct intel_iommu;
  23. struct dmar_dev_scope {
  24. struct device __rcu *dev;
  25. u8 bus;
  26. u8 devfn;
  27. };
  28. #ifdef CONFIG_DMAR_TABLE
  29. extern struct acpi_table_header *dmar_tbl;
  30. struct dmar_drhd_unit {
  31. struct list_head list; /* list of drhd units */
  32. struct acpi_dmar_header *hdr; /* ACPI header */
  33. u64 reg_base_addr; /* register base address*/
  34. struct dmar_dev_scope *devices;/* target device array */
  35. int devices_cnt; /* target device count */
  36. u16 segment; /* PCI domain */
  37. u8 ignored:1; /* ignore drhd */
  38. u8 include_all:1;
  39. u8 gfx_dedicated:1; /* graphic dedicated */
  40. struct intel_iommu *iommu;
  41. };
  42. struct dmar_pci_path {
  43. u8 bus;
  44. u8 device;
  45. u8 function;
  46. };
  47. struct dmar_pci_notify_info {
  48. struct pci_dev *dev;
  49. unsigned long event;
  50. int bus;
  51. u16 seg;
  52. u16 level;
  53. struct dmar_pci_path path[];
  54. } __attribute__((packed));
  55. extern struct rw_semaphore dmar_global_lock;
  56. extern struct list_head dmar_drhd_units;
  57. #define for_each_drhd_unit(drhd) \
  58. list_for_each_entry_rcu(drhd, &dmar_drhd_units, list, \
  59. dmar_rcu_check())
  60. #define for_each_active_drhd_unit(drhd) \
  61. list_for_each_entry_rcu(drhd, &dmar_drhd_units, list, \
  62. dmar_rcu_check()) \
  63. if (drhd->ignored) {} else
  64. #define for_each_active_iommu(i, drhd) \
  65. list_for_each_entry_rcu(drhd, &dmar_drhd_units, list, \
  66. dmar_rcu_check()) \
  67. if (i=drhd->iommu, drhd->ignored) {} else
  68. #define for_each_iommu(i, drhd) \
  69. list_for_each_entry_rcu(drhd, &dmar_drhd_units, list, \
  70. dmar_rcu_check()) \
  71. if (i=drhd->iommu, 0) {} else
  72. static inline bool dmar_rcu_check(void)
  73. {
  74. return rwsem_is_locked(&dmar_global_lock) ||
  75. system_state == SYSTEM_BOOTING;
  76. }
  77. #define dmar_rcu_dereference(p) rcu_dereference_check((p), dmar_rcu_check())
  78. #define for_each_dev_scope(devs, cnt, i, tmp) \
  79. for ((i) = 0; ((tmp) = (i) < (cnt) ? \
  80. dmar_rcu_dereference((devs)[(i)].dev) : NULL, (i) < (cnt)); \
  81. (i)++)
  82. #define for_each_active_dev_scope(devs, cnt, i, tmp) \
  83. for_each_dev_scope((devs), (cnt), (i), (tmp)) \
  84. if (!(tmp)) { continue; } else
  85. extern int dmar_table_init(void);
  86. extern int dmar_dev_scope_init(void);
  87. extern void dmar_register_bus_notifier(void);
  88. extern int dmar_parse_dev_scope(void *start, void *end, int *cnt,
  89. struct dmar_dev_scope **devices, u16 segment);
  90. extern void *dmar_alloc_dev_scope(void *start, void *end, int *cnt);
  91. extern void dmar_free_dev_scope(struct dmar_dev_scope **devices, int *cnt);
  92. extern int dmar_insert_dev_scope(struct dmar_pci_notify_info *info,
  93. void *start, void*end, u16 segment,
  94. struct dmar_dev_scope *devices,
  95. int devices_cnt);
  96. extern int dmar_remove_dev_scope(struct dmar_pci_notify_info *info,
  97. u16 segment, struct dmar_dev_scope *devices,
  98. int count);
  99. /* Intel IOMMU detection */
  100. void detect_intel_iommu(void);
  101. extern int enable_drhd_fault_handling(void);
  102. extern int dmar_device_add(acpi_handle handle);
  103. extern int dmar_device_remove(acpi_handle handle);
  104. static inline int dmar_res_noop(struct acpi_dmar_header *hdr, void *arg)
  105. {
  106. return 0;
  107. }
  108. #ifdef CONFIG_DMAR_DEBUG
  109. void dmar_fault_dump_ptes(struct intel_iommu *iommu, u16 source_id,
  110. unsigned long long addr, u32 pasid);
  111. #else
  112. static inline void dmar_fault_dump_ptes(struct intel_iommu *iommu, u16 source_id,
  113. unsigned long long addr, u32 pasid) {}
  114. #endif
  115. #ifdef CONFIG_INTEL_IOMMU
  116. extern int iommu_detected, no_iommu;
  117. extern int intel_iommu_init(void);
  118. extern void intel_iommu_shutdown(void);
  119. extern int dmar_parse_one_rmrr(struct acpi_dmar_header *header, void *arg);
  120. extern int dmar_parse_one_atsr(struct acpi_dmar_header *header, void *arg);
  121. extern int dmar_check_one_atsr(struct acpi_dmar_header *hdr, void *arg);
  122. extern int dmar_parse_one_satc(struct acpi_dmar_header *hdr, void *arg);
  123. extern int dmar_release_one_atsr(struct acpi_dmar_header *hdr, void *arg);
  124. extern int dmar_iommu_hotplug(struct dmar_drhd_unit *dmaru, bool insert);
  125. extern int dmar_iommu_notify_scope_dev(struct dmar_pci_notify_info *info);
  126. #else /* !CONFIG_INTEL_IOMMU: */
  127. static inline int intel_iommu_init(void) { return -ENODEV; }
  128. static inline void intel_iommu_shutdown(void) { }
  129. #define dmar_parse_one_rmrr dmar_res_noop
  130. #define dmar_parse_one_atsr dmar_res_noop
  131. #define dmar_check_one_atsr dmar_res_noop
  132. #define dmar_release_one_atsr dmar_res_noop
  133. #define dmar_parse_one_satc dmar_res_noop
  134. static inline int dmar_iommu_notify_scope_dev(struct dmar_pci_notify_info *info)
  135. {
  136. return 0;
  137. }
  138. static inline int dmar_iommu_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
  139. {
  140. return 0;
  141. }
  142. #endif /* CONFIG_INTEL_IOMMU */
  143. #ifdef CONFIG_IRQ_REMAP
  144. extern int dmar_ir_hotplug(struct dmar_drhd_unit *dmaru, bool insert);
  145. #else /* CONFIG_IRQ_REMAP */
  146. static inline int dmar_ir_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
  147. { return 0; }
  148. #endif /* CONFIG_IRQ_REMAP */
  149. extern bool dmar_platform_optin(void);
  150. #else /* CONFIG_DMAR_TABLE */
  151. static inline int dmar_device_add(void *handle)
  152. {
  153. return 0;
  154. }
  155. static inline int dmar_device_remove(void *handle)
  156. {
  157. return 0;
  158. }
  159. static inline bool dmar_platform_optin(void)
  160. {
  161. return false;
  162. }
  163. static inline void detect_intel_iommu(void)
  164. {
  165. }
  166. #endif /* CONFIG_DMAR_TABLE */
  167. struct irte {
  168. union {
  169. /* Shared between remapped and posted mode*/
  170. struct {
  171. __u64 present : 1, /* 0 */
  172. fpd : 1, /* 1 */
  173. __res0 : 6, /* 2 - 6 */
  174. avail : 4, /* 8 - 11 */
  175. __res1 : 3, /* 12 - 14 */
  176. pst : 1, /* 15 */
  177. vector : 8, /* 16 - 23 */
  178. __res2 : 40; /* 24 - 63 */
  179. };
  180. /* Remapped mode */
  181. struct {
  182. __u64 r_present : 1, /* 0 */
  183. r_fpd : 1, /* 1 */
  184. dst_mode : 1, /* 2 */
  185. redir_hint : 1, /* 3 */
  186. trigger_mode : 1, /* 4 */
  187. dlvry_mode : 3, /* 5 - 7 */
  188. r_avail : 4, /* 8 - 11 */
  189. r_res0 : 4, /* 12 - 15 */
  190. r_vector : 8, /* 16 - 23 */
  191. r_res1 : 8, /* 24 - 31 */
  192. dest_id : 32; /* 32 - 63 */
  193. };
  194. /* Posted mode */
  195. struct {
  196. __u64 p_present : 1, /* 0 */
  197. p_fpd : 1, /* 1 */
  198. p_res0 : 6, /* 2 - 7 */
  199. p_avail : 4, /* 8 - 11 */
  200. p_res1 : 2, /* 12 - 13 */
  201. p_urgent : 1, /* 14 */
  202. p_pst : 1, /* 15 */
  203. p_vector : 8, /* 16 - 23 */
  204. p_res2 : 14, /* 24 - 37 */
  205. pda_l : 26; /* 38 - 63 */
  206. };
  207. __u64 low;
  208. };
  209. union {
  210. /* Shared between remapped and posted mode*/
  211. struct {
  212. __u64 sid : 16, /* 64 - 79 */
  213. sq : 2, /* 80 - 81 */
  214. svt : 2, /* 82 - 83 */
  215. __res3 : 44; /* 84 - 127 */
  216. };
  217. /* Posted mode*/
  218. struct {
  219. __u64 p_sid : 16, /* 64 - 79 */
  220. p_sq : 2, /* 80 - 81 */
  221. p_svt : 2, /* 82 - 83 */
  222. p_res3 : 12, /* 84 - 95 */
  223. pda_h : 32; /* 96 - 127 */
  224. };
  225. __u64 high;
  226. };
  227. };
  228. static inline void dmar_copy_shared_irte(struct irte *dst, struct irte *src)
  229. {
  230. dst->present = src->present;
  231. dst->fpd = src->fpd;
  232. dst->avail = src->avail;
  233. dst->pst = src->pst;
  234. dst->vector = src->vector;
  235. dst->sid = src->sid;
  236. dst->sq = src->sq;
  237. dst->svt = src->svt;
  238. }
  239. #define PDA_LOW_BIT 26
  240. #define PDA_HIGH_BIT 32
  241. /* Can't use the common MSI interrupt functions
  242. * since DMAR is not a pci device
  243. */
  244. struct irq_data;
  245. extern void dmar_msi_unmask(struct irq_data *data);
  246. extern void dmar_msi_mask(struct irq_data *data);
  247. extern void dmar_msi_read(int irq, struct msi_msg *msg);
  248. extern void dmar_msi_write(int irq, struct msi_msg *msg);
  249. extern int dmar_set_interrupt(struct intel_iommu *iommu);
  250. extern irqreturn_t dmar_fault(int irq, void *dev_id);
  251. extern int dmar_alloc_hwirq(int id, int node, void *arg);
  252. extern void dmar_free_hwirq(int irq);
  253. #endif /* __DMAR_H__ */