debugfs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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. * Lu Baolu <baolu.lu@linux.intel.com>
  9. */
  10. #include <linux/debugfs.h>
  11. #include <linux/dmar.h>
  12. #include <linux/pci.h>
  13. #include <asm/irq_remapping.h>
  14. #include "iommu.h"
  15. #include "pasid.h"
  16. #include "perf.h"
  17. struct tbl_walk {
  18. u16 bus;
  19. u16 devfn;
  20. u32 pasid;
  21. struct root_entry *rt_entry;
  22. struct context_entry *ctx_entry;
  23. struct pasid_entry *pasid_tbl_entry;
  24. };
  25. struct iommu_regset {
  26. int offset;
  27. const char *regs;
  28. };
  29. #define DEBUG_BUFFER_SIZE 1024
  30. static char debug_buf[DEBUG_BUFFER_SIZE];
  31. #define IOMMU_REGSET_ENTRY(_reg_) \
  32. { DMAR_##_reg_##_REG, __stringify(_reg_) }
  33. static const struct iommu_regset iommu_regs_32[] = {
  34. IOMMU_REGSET_ENTRY(VER),
  35. IOMMU_REGSET_ENTRY(GCMD),
  36. IOMMU_REGSET_ENTRY(GSTS),
  37. IOMMU_REGSET_ENTRY(FSTS),
  38. IOMMU_REGSET_ENTRY(FECTL),
  39. IOMMU_REGSET_ENTRY(FEDATA),
  40. IOMMU_REGSET_ENTRY(FEADDR),
  41. IOMMU_REGSET_ENTRY(FEUADDR),
  42. IOMMU_REGSET_ENTRY(PMEN),
  43. IOMMU_REGSET_ENTRY(PLMBASE),
  44. IOMMU_REGSET_ENTRY(PLMLIMIT),
  45. IOMMU_REGSET_ENTRY(ICS),
  46. IOMMU_REGSET_ENTRY(PRS),
  47. IOMMU_REGSET_ENTRY(PECTL),
  48. IOMMU_REGSET_ENTRY(PEDATA),
  49. IOMMU_REGSET_ENTRY(PEADDR),
  50. IOMMU_REGSET_ENTRY(PEUADDR),
  51. };
  52. static const struct iommu_regset iommu_regs_64[] = {
  53. IOMMU_REGSET_ENTRY(CAP),
  54. IOMMU_REGSET_ENTRY(ECAP),
  55. IOMMU_REGSET_ENTRY(RTADDR),
  56. IOMMU_REGSET_ENTRY(CCMD),
  57. IOMMU_REGSET_ENTRY(AFLOG),
  58. IOMMU_REGSET_ENTRY(PHMBASE),
  59. IOMMU_REGSET_ENTRY(PHMLIMIT),
  60. IOMMU_REGSET_ENTRY(IQH),
  61. IOMMU_REGSET_ENTRY(IQT),
  62. IOMMU_REGSET_ENTRY(IQA),
  63. IOMMU_REGSET_ENTRY(IRTA),
  64. IOMMU_REGSET_ENTRY(PQH),
  65. IOMMU_REGSET_ENTRY(PQT),
  66. IOMMU_REGSET_ENTRY(PQA),
  67. IOMMU_REGSET_ENTRY(MTRRCAP),
  68. IOMMU_REGSET_ENTRY(MTRRDEF),
  69. IOMMU_REGSET_ENTRY(MTRR_FIX64K_00000),
  70. IOMMU_REGSET_ENTRY(MTRR_FIX16K_80000),
  71. IOMMU_REGSET_ENTRY(MTRR_FIX16K_A0000),
  72. IOMMU_REGSET_ENTRY(MTRR_FIX4K_C0000),
  73. IOMMU_REGSET_ENTRY(MTRR_FIX4K_C8000),
  74. IOMMU_REGSET_ENTRY(MTRR_FIX4K_D0000),
  75. IOMMU_REGSET_ENTRY(MTRR_FIX4K_D8000),
  76. IOMMU_REGSET_ENTRY(MTRR_FIX4K_E0000),
  77. IOMMU_REGSET_ENTRY(MTRR_FIX4K_E8000),
  78. IOMMU_REGSET_ENTRY(MTRR_FIX4K_F0000),
  79. IOMMU_REGSET_ENTRY(MTRR_FIX4K_F8000),
  80. IOMMU_REGSET_ENTRY(MTRR_PHYSBASE0),
  81. IOMMU_REGSET_ENTRY(MTRR_PHYSMASK0),
  82. IOMMU_REGSET_ENTRY(MTRR_PHYSBASE1),
  83. IOMMU_REGSET_ENTRY(MTRR_PHYSMASK1),
  84. IOMMU_REGSET_ENTRY(MTRR_PHYSBASE2),
  85. IOMMU_REGSET_ENTRY(MTRR_PHYSMASK2),
  86. IOMMU_REGSET_ENTRY(MTRR_PHYSBASE3),
  87. IOMMU_REGSET_ENTRY(MTRR_PHYSMASK3),
  88. IOMMU_REGSET_ENTRY(MTRR_PHYSBASE4),
  89. IOMMU_REGSET_ENTRY(MTRR_PHYSMASK4),
  90. IOMMU_REGSET_ENTRY(MTRR_PHYSBASE5),
  91. IOMMU_REGSET_ENTRY(MTRR_PHYSMASK5),
  92. IOMMU_REGSET_ENTRY(MTRR_PHYSBASE6),
  93. IOMMU_REGSET_ENTRY(MTRR_PHYSMASK6),
  94. IOMMU_REGSET_ENTRY(MTRR_PHYSBASE7),
  95. IOMMU_REGSET_ENTRY(MTRR_PHYSMASK7),
  96. IOMMU_REGSET_ENTRY(MTRR_PHYSBASE8),
  97. IOMMU_REGSET_ENTRY(MTRR_PHYSMASK8),
  98. IOMMU_REGSET_ENTRY(MTRR_PHYSBASE9),
  99. IOMMU_REGSET_ENTRY(MTRR_PHYSMASK9),
  100. };
  101. static struct dentry *intel_iommu_debug;
  102. static int iommu_regset_show(struct seq_file *m, void *unused)
  103. {
  104. struct dmar_drhd_unit *drhd;
  105. struct intel_iommu *iommu;
  106. unsigned long flag;
  107. int i, ret = 0;
  108. u64 value;
  109. rcu_read_lock();
  110. for_each_active_iommu(iommu, drhd) {
  111. if (!drhd->reg_base_addr) {
  112. seq_puts(m, "IOMMU: Invalid base address\n");
  113. ret = -EINVAL;
  114. goto out;
  115. }
  116. seq_printf(m, "IOMMU: %s Register Base Address: %llx\n",
  117. iommu->name, drhd->reg_base_addr);
  118. seq_puts(m, "Name\t\t\tOffset\t\tContents\n");
  119. /*
  120. * Publish the contents of the 64-bit hardware registers
  121. * by adding the offset to the pointer (virtual address).
  122. */
  123. raw_spin_lock_irqsave(&iommu->register_lock, flag);
  124. for (i = 0 ; i < ARRAY_SIZE(iommu_regs_32); i++) {
  125. value = dmar_readl(iommu->reg + iommu_regs_32[i].offset);
  126. seq_printf(m, "%-16s\t0x%02x\t\t0x%016llx\n",
  127. iommu_regs_32[i].regs, iommu_regs_32[i].offset,
  128. value);
  129. }
  130. for (i = 0 ; i < ARRAY_SIZE(iommu_regs_64); i++) {
  131. value = dmar_readq(iommu->reg + iommu_regs_64[i].offset);
  132. seq_printf(m, "%-16s\t0x%02x\t\t0x%016llx\n",
  133. iommu_regs_64[i].regs, iommu_regs_64[i].offset,
  134. value);
  135. }
  136. raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
  137. seq_putc(m, '\n');
  138. }
  139. out:
  140. rcu_read_unlock();
  141. return ret;
  142. }
  143. DEFINE_SHOW_ATTRIBUTE(iommu_regset);
  144. static inline void print_tbl_walk(struct seq_file *m)
  145. {
  146. struct tbl_walk *tbl_wlk = m->private;
  147. seq_printf(m, "%02x:%02x.%x\t0x%016llx:0x%016llx\t0x%016llx:0x%016llx\t",
  148. tbl_wlk->bus, PCI_SLOT(tbl_wlk->devfn),
  149. PCI_FUNC(tbl_wlk->devfn), tbl_wlk->rt_entry->hi,
  150. tbl_wlk->rt_entry->lo, tbl_wlk->ctx_entry->hi,
  151. tbl_wlk->ctx_entry->lo);
  152. /*
  153. * A legacy mode DMAR doesn't support PASID, hence default it to -1
  154. * indicating that it's invalid. Also, default all PASID related fields
  155. * to 0.
  156. */
  157. if (!tbl_wlk->pasid_tbl_entry)
  158. seq_printf(m, "%-6d\t0x%016llx:0x%016llx:0x%016llx\n", -1,
  159. (u64)0, (u64)0, (u64)0);
  160. else
  161. seq_printf(m, "%-6d\t0x%016llx:0x%016llx:0x%016llx\n",
  162. tbl_wlk->pasid, tbl_wlk->pasid_tbl_entry->val[2],
  163. tbl_wlk->pasid_tbl_entry->val[1],
  164. tbl_wlk->pasid_tbl_entry->val[0]);
  165. }
  166. static void pasid_tbl_walk(struct seq_file *m, struct pasid_entry *tbl_entry,
  167. u16 dir_idx)
  168. {
  169. struct tbl_walk *tbl_wlk = m->private;
  170. u8 tbl_idx;
  171. for (tbl_idx = 0; tbl_idx < PASID_TBL_ENTRIES; tbl_idx++) {
  172. if (pasid_pte_is_present(tbl_entry)) {
  173. tbl_wlk->pasid_tbl_entry = tbl_entry;
  174. tbl_wlk->pasid = (dir_idx << PASID_PDE_SHIFT) + tbl_idx;
  175. print_tbl_walk(m);
  176. }
  177. tbl_entry++;
  178. }
  179. }
  180. static void pasid_dir_walk(struct seq_file *m, u64 pasid_dir_ptr,
  181. u16 pasid_dir_size)
  182. {
  183. struct pasid_dir_entry *dir_entry = phys_to_virt(pasid_dir_ptr);
  184. struct pasid_entry *pasid_tbl;
  185. u16 dir_idx;
  186. for (dir_idx = 0; dir_idx < pasid_dir_size; dir_idx++) {
  187. pasid_tbl = get_pasid_table_from_pde(dir_entry);
  188. if (pasid_tbl)
  189. pasid_tbl_walk(m, pasid_tbl, dir_idx);
  190. dir_entry++;
  191. }
  192. }
  193. static void ctx_tbl_walk(struct seq_file *m, struct intel_iommu *iommu, u16 bus)
  194. {
  195. struct context_entry *context;
  196. u16 devfn, pasid_dir_size;
  197. u64 pasid_dir_ptr;
  198. for (devfn = 0; devfn < 256; devfn++) {
  199. struct tbl_walk tbl_wlk = {0};
  200. /*
  201. * Scalable mode root entry points to upper scalable mode
  202. * context table and lower scalable mode context table. Each
  203. * scalable mode context table has 128 context entries where as
  204. * legacy mode context table has 256 context entries. So in
  205. * scalable mode, the context entries for former 128 devices are
  206. * in the lower scalable mode context table, while the latter
  207. * 128 devices are in the upper scalable mode context table.
  208. * In scalable mode, when devfn > 127, iommu_context_addr()
  209. * automatically refers to upper scalable mode context table and
  210. * hence the caller doesn't have to worry about differences
  211. * between scalable mode and non scalable mode.
  212. */
  213. context = iommu_context_addr(iommu, bus, devfn, 0);
  214. if (!context)
  215. return;
  216. if (!context_present(context))
  217. continue;
  218. tbl_wlk.bus = bus;
  219. tbl_wlk.devfn = devfn;
  220. tbl_wlk.rt_entry = &iommu->root_entry[bus];
  221. tbl_wlk.ctx_entry = context;
  222. m->private = &tbl_wlk;
  223. if (dmar_readq(iommu->reg + DMAR_RTADDR_REG) & DMA_RTADDR_SMT) {
  224. pasid_dir_ptr = context->lo & VTD_PAGE_MASK;
  225. pasid_dir_size = get_pasid_dir_size(context);
  226. pasid_dir_walk(m, pasid_dir_ptr, pasid_dir_size);
  227. continue;
  228. }
  229. print_tbl_walk(m);
  230. }
  231. }
  232. static void root_tbl_walk(struct seq_file *m, struct intel_iommu *iommu)
  233. {
  234. u16 bus;
  235. spin_lock(&iommu->lock);
  236. seq_printf(m, "IOMMU %s: Root Table Address: 0x%llx\n", iommu->name,
  237. (u64)virt_to_phys(iommu->root_entry));
  238. seq_puts(m, "B.D.F\tRoot_entry\t\t\t\tContext_entry\t\t\t\tPASID\tPASID_table_entry\n");
  239. /*
  240. * No need to check if the root entry is present or not because
  241. * iommu_context_addr() performs the same check before returning
  242. * context entry.
  243. */
  244. for (bus = 0; bus < 256; bus++)
  245. ctx_tbl_walk(m, iommu, bus);
  246. spin_unlock(&iommu->lock);
  247. }
  248. static int dmar_translation_struct_show(struct seq_file *m, void *unused)
  249. {
  250. struct dmar_drhd_unit *drhd;
  251. struct intel_iommu *iommu;
  252. u32 sts;
  253. rcu_read_lock();
  254. for_each_active_iommu(iommu, drhd) {
  255. sts = dmar_readl(iommu->reg + DMAR_GSTS_REG);
  256. if (!(sts & DMA_GSTS_TES)) {
  257. seq_printf(m, "DMA Remapping is not enabled on %s\n",
  258. iommu->name);
  259. continue;
  260. }
  261. root_tbl_walk(m, iommu);
  262. seq_putc(m, '\n');
  263. }
  264. rcu_read_unlock();
  265. return 0;
  266. }
  267. DEFINE_SHOW_ATTRIBUTE(dmar_translation_struct);
  268. static inline unsigned long level_to_directory_size(int level)
  269. {
  270. return BIT_ULL(VTD_PAGE_SHIFT + VTD_STRIDE_SHIFT * (level - 1));
  271. }
  272. static inline void
  273. dump_page_info(struct seq_file *m, unsigned long iova, u64 *path)
  274. {
  275. seq_printf(m, "0x%013lx |\t0x%016llx\t0x%016llx\t0x%016llx",
  276. iova >> VTD_PAGE_SHIFT, path[5], path[4], path[3]);
  277. if (path[2]) {
  278. seq_printf(m, "\t0x%016llx", path[2]);
  279. if (path[1])
  280. seq_printf(m, "\t0x%016llx", path[1]);
  281. }
  282. seq_putc(m, '\n');
  283. }
  284. static void pgtable_walk_level(struct seq_file *m, struct dma_pte *pde,
  285. int level, unsigned long start,
  286. u64 *path)
  287. {
  288. int i;
  289. if (level > 5 || level < 1)
  290. return;
  291. for (i = 0; i < BIT_ULL(VTD_STRIDE_SHIFT);
  292. i++, pde++, start += level_to_directory_size(level)) {
  293. if (!dma_pte_present(pde))
  294. continue;
  295. path[level] = pde->val;
  296. if (dma_pte_superpage(pde) || level == 1)
  297. dump_page_info(m, start, path);
  298. else
  299. pgtable_walk_level(m, phys_to_virt(dma_pte_addr(pde)),
  300. level - 1, start, path);
  301. path[level] = 0;
  302. }
  303. }
  304. static int domain_translation_struct_show(struct seq_file *m,
  305. struct device_domain_info *info,
  306. ioasid_t pasid)
  307. {
  308. bool scalable, found = false;
  309. struct dmar_drhd_unit *drhd;
  310. struct intel_iommu *iommu;
  311. u16 devfn, bus, seg;
  312. bus = info->bus;
  313. devfn = info->devfn;
  314. seg = info->segment;
  315. rcu_read_lock();
  316. for_each_active_iommu(iommu, drhd) {
  317. struct context_entry *context;
  318. u64 pgd, path[6] = { 0 };
  319. u32 sts, agaw;
  320. if (seg != iommu->segment)
  321. continue;
  322. sts = dmar_readl(iommu->reg + DMAR_GSTS_REG);
  323. if (!(sts & DMA_GSTS_TES)) {
  324. seq_printf(m, "DMA Remapping is not enabled on %s\n",
  325. iommu->name);
  326. continue;
  327. }
  328. if (dmar_readq(iommu->reg + DMAR_RTADDR_REG) & DMA_RTADDR_SMT)
  329. scalable = true;
  330. else
  331. scalable = false;
  332. /*
  333. * The iommu->lock is held across the callback, which will
  334. * block calls to domain_attach/domain_detach. Hence,
  335. * the domain of the device will not change during traversal.
  336. *
  337. * Traversing page table possibly races with the iommu_unmap()
  338. * interface. This could be solved by RCU-freeing the page
  339. * table pages in the iommu_unmap() path.
  340. */
  341. spin_lock(&iommu->lock);
  342. context = iommu_context_addr(iommu, bus, devfn, 0);
  343. if (!context || !context_present(context))
  344. goto iommu_unlock;
  345. if (scalable) { /* scalable mode */
  346. struct pasid_entry *pasid_tbl, *pasid_tbl_entry;
  347. struct pasid_dir_entry *dir_tbl, *dir_entry;
  348. u16 dir_idx, tbl_idx, pgtt;
  349. u64 pasid_dir_ptr;
  350. pasid_dir_ptr = context->lo & VTD_PAGE_MASK;
  351. /* Dump specified device domain mappings with PASID. */
  352. dir_idx = pasid >> PASID_PDE_SHIFT;
  353. tbl_idx = pasid & PASID_PTE_MASK;
  354. dir_tbl = phys_to_virt(pasid_dir_ptr);
  355. dir_entry = &dir_tbl[dir_idx];
  356. pasid_tbl = get_pasid_table_from_pde(dir_entry);
  357. if (!pasid_tbl)
  358. goto iommu_unlock;
  359. pasid_tbl_entry = &pasid_tbl[tbl_idx];
  360. if (!pasid_pte_is_present(pasid_tbl_entry))
  361. goto iommu_unlock;
  362. /*
  363. * According to PASID Granular Translation Type(PGTT),
  364. * get the page table pointer.
  365. */
  366. pgtt = (u16)(pasid_tbl_entry->val[0] & GENMASK_ULL(8, 6)) >> 6;
  367. agaw = (u8)(pasid_tbl_entry->val[0] & GENMASK_ULL(4, 2)) >> 2;
  368. switch (pgtt) {
  369. case PASID_ENTRY_PGTT_FL_ONLY:
  370. pgd = pasid_tbl_entry->val[2];
  371. break;
  372. case PASID_ENTRY_PGTT_SL_ONLY:
  373. case PASID_ENTRY_PGTT_NESTED:
  374. pgd = pasid_tbl_entry->val[0];
  375. break;
  376. default:
  377. goto iommu_unlock;
  378. }
  379. pgd &= VTD_PAGE_MASK;
  380. } else { /* legacy mode */
  381. u8 tt = (u8)(context->lo & GENMASK_ULL(3, 2)) >> 2;
  382. /*
  383. * According to Translation Type(TT),
  384. * get the page table pointer(SSPTPTR).
  385. */
  386. switch (tt) {
  387. case CONTEXT_TT_MULTI_LEVEL:
  388. case CONTEXT_TT_DEV_IOTLB:
  389. pgd = context->lo & VTD_PAGE_MASK;
  390. agaw = context->hi & 7;
  391. break;
  392. default:
  393. goto iommu_unlock;
  394. }
  395. }
  396. seq_printf(m, "Device %04x:%02x:%02x.%x ",
  397. iommu->segment, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
  398. if (scalable)
  399. seq_printf(m, "with pasid %x @0x%llx\n", pasid, pgd);
  400. else
  401. seq_printf(m, "@0x%llx\n", pgd);
  402. seq_printf(m, "%-17s\t%-18s\t%-18s\t%-18s\t%-18s\t%-s\n",
  403. "IOVA_PFN", "PML5E", "PML4E", "PDPE", "PDE", "PTE");
  404. pgtable_walk_level(m, phys_to_virt(pgd), agaw + 2, 0, path);
  405. found = true;
  406. iommu_unlock:
  407. spin_unlock(&iommu->lock);
  408. if (found)
  409. break;
  410. }
  411. rcu_read_unlock();
  412. return 0;
  413. }
  414. static int dev_domain_translation_struct_show(struct seq_file *m, void *unused)
  415. {
  416. struct device_domain_info *info = (struct device_domain_info *)m->private;
  417. return domain_translation_struct_show(m, info, IOMMU_NO_PASID);
  418. }
  419. DEFINE_SHOW_ATTRIBUTE(dev_domain_translation_struct);
  420. static int pasid_domain_translation_struct_show(struct seq_file *m, void *unused)
  421. {
  422. struct dev_pasid_info *dev_pasid = (struct dev_pasid_info *)m->private;
  423. struct device_domain_info *info = dev_iommu_priv_get(dev_pasid->dev);
  424. return domain_translation_struct_show(m, info, dev_pasid->pasid);
  425. }
  426. DEFINE_SHOW_ATTRIBUTE(pasid_domain_translation_struct);
  427. static void invalidation_queue_entry_show(struct seq_file *m,
  428. struct intel_iommu *iommu)
  429. {
  430. int index, shift = qi_shift(iommu);
  431. struct qi_desc *desc;
  432. int offset;
  433. if (ecap_smts(iommu->ecap))
  434. seq_puts(m, "Index\t\tqw0\t\t\tqw1\t\t\tqw2\t\t\tqw3\t\t\tstatus\n");
  435. else
  436. seq_puts(m, "Index\t\tqw0\t\t\tqw1\t\t\tstatus\n");
  437. for (index = 0; index < QI_LENGTH; index++) {
  438. offset = index << shift;
  439. desc = iommu->qi->desc + offset;
  440. if (ecap_smts(iommu->ecap))
  441. seq_printf(m, "%5d\t%016llx\t%016llx\t%016llx\t%016llx\t%016x\n",
  442. index, desc->qw0, desc->qw1,
  443. desc->qw2, desc->qw3,
  444. iommu->qi->desc_status[index]);
  445. else
  446. seq_printf(m, "%5d\t%016llx\t%016llx\t%016x\n",
  447. index, desc->qw0, desc->qw1,
  448. iommu->qi->desc_status[index]);
  449. }
  450. }
  451. static int invalidation_queue_show(struct seq_file *m, void *unused)
  452. {
  453. struct dmar_drhd_unit *drhd;
  454. struct intel_iommu *iommu;
  455. unsigned long flags;
  456. struct q_inval *qi;
  457. int shift;
  458. rcu_read_lock();
  459. for_each_active_iommu(iommu, drhd) {
  460. qi = iommu->qi;
  461. shift = qi_shift(iommu);
  462. if (!qi || !ecap_qis(iommu->ecap))
  463. continue;
  464. seq_printf(m, "Invalidation queue on IOMMU: %s\n", iommu->name);
  465. raw_spin_lock_irqsave(&qi->q_lock, flags);
  466. seq_printf(m, " Base: 0x%llx\tHead: %lld\tTail: %lld\n",
  467. (u64)virt_to_phys(qi->desc),
  468. dmar_readq(iommu->reg + DMAR_IQH_REG) >> shift,
  469. dmar_readq(iommu->reg + DMAR_IQT_REG) >> shift);
  470. invalidation_queue_entry_show(m, iommu);
  471. raw_spin_unlock_irqrestore(&qi->q_lock, flags);
  472. seq_putc(m, '\n');
  473. }
  474. rcu_read_unlock();
  475. return 0;
  476. }
  477. DEFINE_SHOW_ATTRIBUTE(invalidation_queue);
  478. #ifdef CONFIG_IRQ_REMAP
  479. static void ir_tbl_remap_entry_show(struct seq_file *m,
  480. struct intel_iommu *iommu)
  481. {
  482. struct irte *ri_entry;
  483. unsigned long flags;
  484. int idx;
  485. seq_puts(m, " Entry SrcID DstID Vct IRTE_high\t\tIRTE_low\n");
  486. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  487. for (idx = 0; idx < INTR_REMAP_TABLE_ENTRIES; idx++) {
  488. ri_entry = &iommu->ir_table->base[idx];
  489. if (!ri_entry->present || ri_entry->p_pst)
  490. continue;
  491. seq_printf(m, " %-5d %02x:%02x.%01x %08x %02x %016llx\t%016llx\n",
  492. idx, PCI_BUS_NUM(ri_entry->sid),
  493. PCI_SLOT(ri_entry->sid), PCI_FUNC(ri_entry->sid),
  494. ri_entry->dest_id, ri_entry->vector,
  495. ri_entry->high, ri_entry->low);
  496. }
  497. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  498. }
  499. static void ir_tbl_posted_entry_show(struct seq_file *m,
  500. struct intel_iommu *iommu)
  501. {
  502. struct irte *pi_entry;
  503. unsigned long flags;
  504. int idx;
  505. seq_puts(m, " Entry SrcID PDA_high PDA_low Vct IRTE_high\t\tIRTE_low\n");
  506. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  507. for (idx = 0; idx < INTR_REMAP_TABLE_ENTRIES; idx++) {
  508. pi_entry = &iommu->ir_table->base[idx];
  509. if (!pi_entry->present || !pi_entry->p_pst)
  510. continue;
  511. seq_printf(m, " %-5d %02x:%02x.%01x %08x %08x %02x %016llx\t%016llx\n",
  512. idx, PCI_BUS_NUM(pi_entry->sid),
  513. PCI_SLOT(pi_entry->sid), PCI_FUNC(pi_entry->sid),
  514. pi_entry->pda_h, pi_entry->pda_l << 6,
  515. pi_entry->vector, pi_entry->high,
  516. pi_entry->low);
  517. }
  518. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  519. }
  520. /*
  521. * For active IOMMUs go through the Interrupt remapping
  522. * table and print valid entries in a table format for
  523. * Remapped and Posted Interrupts.
  524. */
  525. static int ir_translation_struct_show(struct seq_file *m, void *unused)
  526. {
  527. struct dmar_drhd_unit *drhd;
  528. struct intel_iommu *iommu;
  529. u64 irta;
  530. u32 sts;
  531. rcu_read_lock();
  532. for_each_active_iommu(iommu, drhd) {
  533. if (!ecap_ir_support(iommu->ecap))
  534. continue;
  535. seq_printf(m, "Remapped Interrupt supported on IOMMU: %s\n",
  536. iommu->name);
  537. sts = dmar_readl(iommu->reg + DMAR_GSTS_REG);
  538. if (iommu->ir_table && (sts & DMA_GSTS_IRES)) {
  539. irta = virt_to_phys(iommu->ir_table->base);
  540. seq_printf(m, " IR table address:%llx\n", irta);
  541. ir_tbl_remap_entry_show(m, iommu);
  542. } else {
  543. seq_puts(m, "Interrupt Remapping is not enabled\n");
  544. }
  545. seq_putc(m, '\n');
  546. }
  547. seq_puts(m, "****\n\n");
  548. for_each_active_iommu(iommu, drhd) {
  549. if (!cap_pi_support(iommu->cap))
  550. continue;
  551. seq_printf(m, "Posted Interrupt supported on IOMMU: %s\n",
  552. iommu->name);
  553. if (iommu->ir_table) {
  554. irta = virt_to_phys(iommu->ir_table->base);
  555. seq_printf(m, " IR table address:%llx\n", irta);
  556. ir_tbl_posted_entry_show(m, iommu);
  557. } else {
  558. seq_puts(m, "Interrupt Remapping is not enabled\n");
  559. }
  560. seq_putc(m, '\n');
  561. }
  562. rcu_read_unlock();
  563. return 0;
  564. }
  565. DEFINE_SHOW_ATTRIBUTE(ir_translation_struct);
  566. #endif
  567. static void latency_show_one(struct seq_file *m, struct intel_iommu *iommu,
  568. struct dmar_drhd_unit *drhd)
  569. {
  570. int ret;
  571. seq_printf(m, "IOMMU: %s Register Base Address: %llx\n",
  572. iommu->name, drhd->reg_base_addr);
  573. ret = dmar_latency_snapshot(iommu, debug_buf, DEBUG_BUFFER_SIZE);
  574. if (ret < 0)
  575. seq_puts(m, "Failed to get latency snapshot");
  576. else
  577. seq_puts(m, debug_buf);
  578. seq_puts(m, "\n");
  579. }
  580. static int latency_show(struct seq_file *m, void *v)
  581. {
  582. struct dmar_drhd_unit *drhd;
  583. struct intel_iommu *iommu;
  584. rcu_read_lock();
  585. for_each_active_iommu(iommu, drhd)
  586. latency_show_one(m, iommu, drhd);
  587. rcu_read_unlock();
  588. return 0;
  589. }
  590. static int dmar_perf_latency_open(struct inode *inode, struct file *filp)
  591. {
  592. return single_open(filp, latency_show, NULL);
  593. }
  594. static ssize_t dmar_perf_latency_write(struct file *filp,
  595. const char __user *ubuf,
  596. size_t cnt, loff_t *ppos)
  597. {
  598. struct dmar_drhd_unit *drhd;
  599. struct intel_iommu *iommu;
  600. int counting;
  601. char buf[64];
  602. if (cnt > 63)
  603. cnt = 63;
  604. if (copy_from_user(&buf, ubuf, cnt))
  605. return -EFAULT;
  606. buf[cnt] = 0;
  607. if (kstrtoint(buf, 0, &counting))
  608. return -EINVAL;
  609. switch (counting) {
  610. case 0:
  611. rcu_read_lock();
  612. for_each_active_iommu(iommu, drhd) {
  613. dmar_latency_disable(iommu, DMAR_LATENCY_INV_IOTLB);
  614. dmar_latency_disable(iommu, DMAR_LATENCY_INV_DEVTLB);
  615. dmar_latency_disable(iommu, DMAR_LATENCY_INV_IEC);
  616. }
  617. rcu_read_unlock();
  618. break;
  619. case 1:
  620. rcu_read_lock();
  621. for_each_active_iommu(iommu, drhd)
  622. dmar_latency_enable(iommu, DMAR_LATENCY_INV_IOTLB);
  623. rcu_read_unlock();
  624. break;
  625. case 2:
  626. rcu_read_lock();
  627. for_each_active_iommu(iommu, drhd)
  628. dmar_latency_enable(iommu, DMAR_LATENCY_INV_DEVTLB);
  629. rcu_read_unlock();
  630. break;
  631. case 3:
  632. rcu_read_lock();
  633. for_each_active_iommu(iommu, drhd)
  634. dmar_latency_enable(iommu, DMAR_LATENCY_INV_IEC);
  635. rcu_read_unlock();
  636. break;
  637. default:
  638. return -EINVAL;
  639. }
  640. *ppos += cnt;
  641. return cnt;
  642. }
  643. static const struct file_operations dmar_perf_latency_fops = {
  644. .open = dmar_perf_latency_open,
  645. .write = dmar_perf_latency_write,
  646. .read = seq_read,
  647. .llseek = seq_lseek,
  648. .release = single_release,
  649. };
  650. void __init intel_iommu_debugfs_init(void)
  651. {
  652. intel_iommu_debug = debugfs_create_dir("intel", iommu_debugfs_dir);
  653. debugfs_create_file("iommu_regset", 0444, intel_iommu_debug, NULL,
  654. &iommu_regset_fops);
  655. debugfs_create_file("dmar_translation_struct", 0444, intel_iommu_debug,
  656. NULL, &dmar_translation_struct_fops);
  657. debugfs_create_file("invalidation_queue", 0444, intel_iommu_debug,
  658. NULL, &invalidation_queue_fops);
  659. #ifdef CONFIG_IRQ_REMAP
  660. debugfs_create_file("ir_translation_struct", 0444, intel_iommu_debug,
  661. NULL, &ir_translation_struct_fops);
  662. #endif
  663. debugfs_create_file("dmar_perf_latency", 0644, intel_iommu_debug,
  664. NULL, &dmar_perf_latency_fops);
  665. }
  666. /*
  667. * Create a debugfs directory for each device, and then create a
  668. * debugfs file in this directory for users to dump the page table
  669. * of the default domain. e.g.
  670. * /sys/kernel/debug/iommu/intel/0000:00:01.0/domain_translation_struct
  671. */
  672. void intel_iommu_debugfs_create_dev(struct device_domain_info *info)
  673. {
  674. info->debugfs_dentry = debugfs_create_dir(dev_name(info->dev), intel_iommu_debug);
  675. debugfs_create_file("domain_translation_struct", 0444, info->debugfs_dentry,
  676. info, &dev_domain_translation_struct_fops);
  677. }
  678. /* Remove the device debugfs directory. */
  679. void intel_iommu_debugfs_remove_dev(struct device_domain_info *info)
  680. {
  681. debugfs_remove_recursive(info->debugfs_dentry);
  682. }
  683. /*
  684. * Create a debugfs directory per pair of {device, pasid}, then create the
  685. * corresponding debugfs file in this directory for users to dump its page
  686. * table. e.g.
  687. * /sys/kernel/debug/iommu/intel/0000:00:01.0/1/domain_translation_struct
  688. *
  689. * The debugfs only dumps the page tables whose mappings are created and
  690. * destroyed by the iommu_map/unmap() interfaces. Check the mapping type
  691. * of the domain before creating debugfs directory.
  692. */
  693. void intel_iommu_debugfs_create_dev_pasid(struct dev_pasid_info *dev_pasid)
  694. {
  695. struct device_domain_info *info = dev_iommu_priv_get(dev_pasid->dev);
  696. char dir_name[10];
  697. sprintf(dir_name, "%x", dev_pasid->pasid);
  698. dev_pasid->debugfs_dentry = debugfs_create_dir(dir_name, info->debugfs_dentry);
  699. debugfs_create_file("domain_translation_struct", 0444, dev_pasid->debugfs_dentry,
  700. dev_pasid, &pasid_domain_translation_struct_fops);
  701. }
  702. /* Remove the device pasid debugfs directory. */
  703. void intel_iommu_debugfs_remove_dev_pasid(struct dev_pasid_info *dev_pasid)
  704. {
  705. debugfs_remove_recursive(dev_pasid->debugfs_dentry);
  706. }