intel-iommu-debugfs.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright © 2018 Intel Corporation.
  4. *
  5. * Authors: Gayatri Kammela <gayatri.kammela@intel.com>
  6. * Sohil Mehta <sohil.mehta@intel.com>
  7. * Jacob Pan <jacob.jun.pan@linux.intel.com>
  8. */
  9. #include <linux/debugfs.h>
  10. #include <linux/dmar.h>
  11. #include <linux/intel-iommu.h>
  12. #include <linux/pci.h>
  13. #include <asm/irq_remapping.h>
  14. struct iommu_regset {
  15. int offset;
  16. const char *regs;
  17. };
  18. #define IOMMU_REGSET_ENTRY(_reg_) \
  19. { DMAR_##_reg_##_REG, __stringify(_reg_) }
  20. static const struct iommu_regset iommu_regs[] = {
  21. IOMMU_REGSET_ENTRY(VER),
  22. IOMMU_REGSET_ENTRY(CAP),
  23. IOMMU_REGSET_ENTRY(ECAP),
  24. IOMMU_REGSET_ENTRY(GCMD),
  25. IOMMU_REGSET_ENTRY(GSTS),
  26. IOMMU_REGSET_ENTRY(RTADDR),
  27. IOMMU_REGSET_ENTRY(CCMD),
  28. IOMMU_REGSET_ENTRY(FSTS),
  29. IOMMU_REGSET_ENTRY(FECTL),
  30. IOMMU_REGSET_ENTRY(FEDATA),
  31. IOMMU_REGSET_ENTRY(FEADDR),
  32. IOMMU_REGSET_ENTRY(FEUADDR),
  33. IOMMU_REGSET_ENTRY(AFLOG),
  34. IOMMU_REGSET_ENTRY(PMEN),
  35. IOMMU_REGSET_ENTRY(PLMBASE),
  36. IOMMU_REGSET_ENTRY(PLMLIMIT),
  37. IOMMU_REGSET_ENTRY(PHMBASE),
  38. IOMMU_REGSET_ENTRY(PHMLIMIT),
  39. IOMMU_REGSET_ENTRY(IQH),
  40. IOMMU_REGSET_ENTRY(IQT),
  41. IOMMU_REGSET_ENTRY(IQA),
  42. IOMMU_REGSET_ENTRY(ICS),
  43. IOMMU_REGSET_ENTRY(IRTA),
  44. IOMMU_REGSET_ENTRY(PQH),
  45. IOMMU_REGSET_ENTRY(PQT),
  46. IOMMU_REGSET_ENTRY(PQA),
  47. IOMMU_REGSET_ENTRY(PRS),
  48. IOMMU_REGSET_ENTRY(PECTL),
  49. IOMMU_REGSET_ENTRY(PEDATA),
  50. IOMMU_REGSET_ENTRY(PEADDR),
  51. IOMMU_REGSET_ENTRY(PEUADDR),
  52. IOMMU_REGSET_ENTRY(MTRRCAP),
  53. IOMMU_REGSET_ENTRY(MTRRDEF),
  54. IOMMU_REGSET_ENTRY(MTRR_FIX64K_00000),
  55. IOMMU_REGSET_ENTRY(MTRR_FIX16K_80000),
  56. IOMMU_REGSET_ENTRY(MTRR_FIX16K_A0000),
  57. IOMMU_REGSET_ENTRY(MTRR_FIX4K_C0000),
  58. IOMMU_REGSET_ENTRY(MTRR_FIX4K_C8000),
  59. IOMMU_REGSET_ENTRY(MTRR_FIX4K_D0000),
  60. IOMMU_REGSET_ENTRY(MTRR_FIX4K_D8000),
  61. IOMMU_REGSET_ENTRY(MTRR_FIX4K_E0000),
  62. IOMMU_REGSET_ENTRY(MTRR_FIX4K_E8000),
  63. IOMMU_REGSET_ENTRY(MTRR_FIX4K_F0000),
  64. IOMMU_REGSET_ENTRY(MTRR_FIX4K_F8000),
  65. IOMMU_REGSET_ENTRY(MTRR_PHYSBASE0),
  66. IOMMU_REGSET_ENTRY(MTRR_PHYSMASK0),
  67. IOMMU_REGSET_ENTRY(MTRR_PHYSBASE1),
  68. IOMMU_REGSET_ENTRY(MTRR_PHYSMASK1),
  69. IOMMU_REGSET_ENTRY(MTRR_PHYSBASE2),
  70. IOMMU_REGSET_ENTRY(MTRR_PHYSMASK2),
  71. IOMMU_REGSET_ENTRY(MTRR_PHYSBASE3),
  72. IOMMU_REGSET_ENTRY(MTRR_PHYSMASK3),
  73. IOMMU_REGSET_ENTRY(MTRR_PHYSBASE4),
  74. IOMMU_REGSET_ENTRY(MTRR_PHYSMASK4),
  75. IOMMU_REGSET_ENTRY(MTRR_PHYSBASE5),
  76. IOMMU_REGSET_ENTRY(MTRR_PHYSMASK5),
  77. IOMMU_REGSET_ENTRY(MTRR_PHYSBASE6),
  78. IOMMU_REGSET_ENTRY(MTRR_PHYSMASK6),
  79. IOMMU_REGSET_ENTRY(MTRR_PHYSBASE7),
  80. IOMMU_REGSET_ENTRY(MTRR_PHYSMASK7),
  81. IOMMU_REGSET_ENTRY(MTRR_PHYSBASE8),
  82. IOMMU_REGSET_ENTRY(MTRR_PHYSMASK8),
  83. IOMMU_REGSET_ENTRY(MTRR_PHYSBASE9),
  84. IOMMU_REGSET_ENTRY(MTRR_PHYSMASK9),
  85. IOMMU_REGSET_ENTRY(VCCAP),
  86. IOMMU_REGSET_ENTRY(VCMD),
  87. IOMMU_REGSET_ENTRY(VCRSP),
  88. };
  89. static int iommu_regset_show(struct seq_file *m, void *unused)
  90. {
  91. struct dmar_drhd_unit *drhd;
  92. struct intel_iommu *iommu;
  93. unsigned long flag;
  94. int i, ret = 0;
  95. u64 value;
  96. rcu_read_lock();
  97. for_each_active_iommu(iommu, drhd) {
  98. if (!drhd->reg_base_addr) {
  99. seq_puts(m, "IOMMU: Invalid base address\n");
  100. ret = -EINVAL;
  101. goto out;
  102. }
  103. seq_printf(m, "IOMMU: %s Register Base Address: %llx\n",
  104. iommu->name, drhd->reg_base_addr);
  105. seq_puts(m, "Name\t\t\tOffset\t\tContents\n");
  106. /*
  107. * Publish the contents of the 64-bit hardware registers
  108. * by adding the offset to the pointer (virtual address).
  109. */
  110. raw_spin_lock_irqsave(&iommu->register_lock, flag);
  111. for (i = 0 ; i < ARRAY_SIZE(iommu_regs); i++) {
  112. value = dmar_readq(iommu->reg + iommu_regs[i].offset);
  113. seq_printf(m, "%-16s\t0x%02x\t\t0x%016llx\n",
  114. iommu_regs[i].regs, iommu_regs[i].offset,
  115. value);
  116. }
  117. raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
  118. seq_putc(m, '\n');
  119. }
  120. out:
  121. rcu_read_unlock();
  122. return ret;
  123. }
  124. DEFINE_SHOW_ATTRIBUTE(iommu_regset);
  125. static void ctx_tbl_entry_show(struct seq_file *m, struct intel_iommu *iommu,
  126. int bus)
  127. {
  128. struct context_entry *context;
  129. int devfn;
  130. seq_printf(m, " Context Table Entries for Bus: %d\n", bus);
  131. seq_puts(m, " Entry\tB:D.F\tHigh\tLow\n");
  132. for (devfn = 0; devfn < 256; devfn++) {
  133. context = iommu_context_addr(iommu, bus, devfn, 0);
  134. if (!context)
  135. return;
  136. if (!context_present(context))
  137. continue;
  138. seq_printf(m, " %-5d\t%02x:%02x.%x\t%-6llx\t%llx\n", devfn,
  139. bus, PCI_SLOT(devfn), PCI_FUNC(devfn),
  140. context[0].hi, context[0].lo);
  141. }
  142. }
  143. static void root_tbl_entry_show(struct seq_file *m, struct intel_iommu *iommu)
  144. {
  145. unsigned long flags;
  146. int bus;
  147. spin_lock_irqsave(&iommu->lock, flags);
  148. seq_printf(m, "IOMMU %s: Root Table Address:%llx\n", iommu->name,
  149. (u64)virt_to_phys(iommu->root_entry));
  150. seq_puts(m, "Root Table Entries:\n");
  151. for (bus = 0; bus < 256; bus++) {
  152. if (!(iommu->root_entry[bus].lo & 1))
  153. continue;
  154. seq_printf(m, " Bus: %d H: %llx L: %llx\n", bus,
  155. iommu->root_entry[bus].hi,
  156. iommu->root_entry[bus].lo);
  157. ctx_tbl_entry_show(m, iommu, bus);
  158. seq_putc(m, '\n');
  159. }
  160. spin_unlock_irqrestore(&iommu->lock, flags);
  161. }
  162. static int dmar_translation_struct_show(struct seq_file *m, void *unused)
  163. {
  164. struct dmar_drhd_unit *drhd;
  165. struct intel_iommu *iommu;
  166. rcu_read_lock();
  167. for_each_active_iommu(iommu, drhd) {
  168. root_tbl_entry_show(m, iommu);
  169. seq_putc(m, '\n');
  170. }
  171. rcu_read_unlock();
  172. return 0;
  173. }
  174. DEFINE_SHOW_ATTRIBUTE(dmar_translation_struct);
  175. #ifdef CONFIG_IRQ_REMAP
  176. static void ir_tbl_remap_entry_show(struct seq_file *m,
  177. struct intel_iommu *iommu)
  178. {
  179. struct irte *ri_entry;
  180. unsigned long flags;
  181. int idx;
  182. seq_puts(m, " Entry SrcID DstID Vct IRTE_high\t\tIRTE_low\n");
  183. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  184. for (idx = 0; idx < INTR_REMAP_TABLE_ENTRIES; idx++) {
  185. ri_entry = &iommu->ir_table->base[idx];
  186. if (!ri_entry->present || ri_entry->p_pst)
  187. continue;
  188. seq_printf(m, " %-5d %02x:%02x.%01x %08x %02x %016llx\t%016llx\n",
  189. idx, PCI_BUS_NUM(ri_entry->sid),
  190. PCI_SLOT(ri_entry->sid), PCI_FUNC(ri_entry->sid),
  191. ri_entry->dest_id, ri_entry->vector,
  192. ri_entry->high, ri_entry->low);
  193. }
  194. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  195. }
  196. static void ir_tbl_posted_entry_show(struct seq_file *m,
  197. struct intel_iommu *iommu)
  198. {
  199. struct irte *pi_entry;
  200. unsigned long flags;
  201. int idx;
  202. seq_puts(m, " Entry SrcID PDA_high PDA_low Vct IRTE_high\t\tIRTE_low\n");
  203. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  204. for (idx = 0; idx < INTR_REMAP_TABLE_ENTRIES; idx++) {
  205. pi_entry = &iommu->ir_table->base[idx];
  206. if (!pi_entry->present || !pi_entry->p_pst)
  207. continue;
  208. seq_printf(m, " %-5d %02x:%02x.%01x %08x %08x %02x %016llx\t%016llx\n",
  209. idx, PCI_BUS_NUM(pi_entry->sid),
  210. PCI_SLOT(pi_entry->sid), PCI_FUNC(pi_entry->sid),
  211. pi_entry->pda_h, pi_entry->pda_l << 6,
  212. pi_entry->vector, pi_entry->high,
  213. pi_entry->low);
  214. }
  215. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  216. }
  217. /*
  218. * For active IOMMUs go through the Interrupt remapping
  219. * table and print valid entries in a table format for
  220. * Remapped and Posted Interrupts.
  221. */
  222. static int ir_translation_struct_show(struct seq_file *m, void *unused)
  223. {
  224. struct dmar_drhd_unit *drhd;
  225. struct intel_iommu *iommu;
  226. u64 irta;
  227. rcu_read_lock();
  228. for_each_active_iommu(iommu, drhd) {
  229. if (!ecap_ir_support(iommu->ecap))
  230. continue;
  231. seq_printf(m, "Remapped Interrupt supported on IOMMU: %s\n",
  232. iommu->name);
  233. if (iommu->ir_table) {
  234. irta = virt_to_phys(iommu->ir_table->base);
  235. seq_printf(m, " IR table address:%llx\n", irta);
  236. ir_tbl_remap_entry_show(m, iommu);
  237. } else {
  238. seq_puts(m, "Interrupt Remapping is not enabled\n");
  239. }
  240. seq_putc(m, '\n');
  241. }
  242. seq_puts(m, "****\n\n");
  243. for_each_active_iommu(iommu, drhd) {
  244. if (!cap_pi_support(iommu->cap))
  245. continue;
  246. seq_printf(m, "Posted Interrupt supported on IOMMU: %s\n",
  247. iommu->name);
  248. if (iommu->ir_table) {
  249. irta = virt_to_phys(iommu->ir_table->base);
  250. seq_printf(m, " IR table address:%llx\n", irta);
  251. ir_tbl_posted_entry_show(m, iommu);
  252. } else {
  253. seq_puts(m, "Interrupt Remapping is not enabled\n");
  254. }
  255. seq_putc(m, '\n');
  256. }
  257. rcu_read_unlock();
  258. return 0;
  259. }
  260. DEFINE_SHOW_ATTRIBUTE(ir_translation_struct);
  261. #endif
  262. void __init intel_iommu_debugfs_init(void)
  263. {
  264. struct dentry *intel_iommu_debug = debugfs_create_dir("intel",
  265. iommu_debugfs_dir);
  266. debugfs_create_file("iommu_regset", 0444, intel_iommu_debug, NULL,
  267. &iommu_regset_fops);
  268. debugfs_create_file("dmar_translation_struct", 0444, intel_iommu_debug,
  269. NULL, &dmar_translation_struct_fops);
  270. #ifdef CONFIG_IRQ_REMAP
  271. debugfs_create_file("ir_translation_struct", 0444, intel_iommu_debug,
  272. NULL, &ir_translation_struct_fops);
  273. #endif
  274. }