debugfs.c 22 KB

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