1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright © 2018 Intel Corporation.
5 * Authors: Gayatri Kammela <gayatri.kammela@intel.com>
6 * Sohil Mehta <sohil.mehta@intel.com>
7 * Jacob Pan <jacob.jun.pan@linux.intel.com>
10 #include <linux/debugfs.h>
11 #include <linux/dmar.h>
12 #include <linux/intel-iommu.h>
13 #include <linux/pci.h>
15 #include <asm/irq_remapping.h>
17 #include "intel-pasid.h"
23 struct root_entry
*rt_entry
;
24 struct context_entry
*ctx_entry
;
25 struct pasid_entry
*pasid_tbl_entry
;
33 #define IOMMU_REGSET_ENTRY(_reg_) \
34 { DMAR_##_reg_##_REG, __stringify(_reg_) }
36 static const struct iommu_regset iommu_regs_32
[] = {
37 IOMMU_REGSET_ENTRY(VER
),
38 IOMMU_REGSET_ENTRY(GCMD
),
39 IOMMU_REGSET_ENTRY(GSTS
),
40 IOMMU_REGSET_ENTRY(FSTS
),
41 IOMMU_REGSET_ENTRY(FECTL
),
42 IOMMU_REGSET_ENTRY(FEDATA
),
43 IOMMU_REGSET_ENTRY(FEADDR
),
44 IOMMU_REGSET_ENTRY(FEUADDR
),
45 IOMMU_REGSET_ENTRY(PMEN
),
46 IOMMU_REGSET_ENTRY(PLMBASE
),
47 IOMMU_REGSET_ENTRY(PLMLIMIT
),
48 IOMMU_REGSET_ENTRY(ICS
),
49 IOMMU_REGSET_ENTRY(PRS
),
50 IOMMU_REGSET_ENTRY(PECTL
),
51 IOMMU_REGSET_ENTRY(PEDATA
),
52 IOMMU_REGSET_ENTRY(PEADDR
),
53 IOMMU_REGSET_ENTRY(PEUADDR
),
56 static const struct iommu_regset iommu_regs_64
[] = {
57 IOMMU_REGSET_ENTRY(CAP
),
58 IOMMU_REGSET_ENTRY(ECAP
),
59 IOMMU_REGSET_ENTRY(RTADDR
),
60 IOMMU_REGSET_ENTRY(CCMD
),
61 IOMMU_REGSET_ENTRY(AFLOG
),
62 IOMMU_REGSET_ENTRY(PHMBASE
),
63 IOMMU_REGSET_ENTRY(PHMLIMIT
),
64 IOMMU_REGSET_ENTRY(IQH
),
65 IOMMU_REGSET_ENTRY(IQT
),
66 IOMMU_REGSET_ENTRY(IQA
),
67 IOMMU_REGSET_ENTRY(IRTA
),
68 IOMMU_REGSET_ENTRY(PQH
),
69 IOMMU_REGSET_ENTRY(PQT
),
70 IOMMU_REGSET_ENTRY(PQA
),
71 IOMMU_REGSET_ENTRY(MTRRCAP
),
72 IOMMU_REGSET_ENTRY(MTRRDEF
),
73 IOMMU_REGSET_ENTRY(MTRR_FIX64K_00000
),
74 IOMMU_REGSET_ENTRY(MTRR_FIX16K_80000
),
75 IOMMU_REGSET_ENTRY(MTRR_FIX16K_A0000
),
76 IOMMU_REGSET_ENTRY(MTRR_FIX4K_C0000
),
77 IOMMU_REGSET_ENTRY(MTRR_FIX4K_C8000
),
78 IOMMU_REGSET_ENTRY(MTRR_FIX4K_D0000
),
79 IOMMU_REGSET_ENTRY(MTRR_FIX4K_D8000
),
80 IOMMU_REGSET_ENTRY(MTRR_FIX4K_E0000
),
81 IOMMU_REGSET_ENTRY(MTRR_FIX4K_E8000
),
82 IOMMU_REGSET_ENTRY(MTRR_FIX4K_F0000
),
83 IOMMU_REGSET_ENTRY(MTRR_FIX4K_F8000
),
84 IOMMU_REGSET_ENTRY(MTRR_PHYSBASE0
),
85 IOMMU_REGSET_ENTRY(MTRR_PHYSMASK0
),
86 IOMMU_REGSET_ENTRY(MTRR_PHYSBASE1
),
87 IOMMU_REGSET_ENTRY(MTRR_PHYSMASK1
),
88 IOMMU_REGSET_ENTRY(MTRR_PHYSBASE2
),
89 IOMMU_REGSET_ENTRY(MTRR_PHYSMASK2
),
90 IOMMU_REGSET_ENTRY(MTRR_PHYSBASE3
),
91 IOMMU_REGSET_ENTRY(MTRR_PHYSMASK3
),
92 IOMMU_REGSET_ENTRY(MTRR_PHYSBASE4
),
93 IOMMU_REGSET_ENTRY(MTRR_PHYSMASK4
),
94 IOMMU_REGSET_ENTRY(MTRR_PHYSBASE5
),
95 IOMMU_REGSET_ENTRY(MTRR_PHYSMASK5
),
96 IOMMU_REGSET_ENTRY(MTRR_PHYSBASE6
),
97 IOMMU_REGSET_ENTRY(MTRR_PHYSMASK6
),
98 IOMMU_REGSET_ENTRY(MTRR_PHYSBASE7
),
99 IOMMU_REGSET_ENTRY(MTRR_PHYSMASK7
),
100 IOMMU_REGSET_ENTRY(MTRR_PHYSBASE8
),
101 IOMMU_REGSET_ENTRY(MTRR_PHYSMASK8
),
102 IOMMU_REGSET_ENTRY(MTRR_PHYSBASE9
),
103 IOMMU_REGSET_ENTRY(MTRR_PHYSMASK9
),
104 IOMMU_REGSET_ENTRY(VCCAP
),
105 IOMMU_REGSET_ENTRY(VCMD
),
106 IOMMU_REGSET_ENTRY(VCRSP
),
109 static int iommu_regset_show(struct seq_file
*m
, void *unused
)
111 struct dmar_drhd_unit
*drhd
;
112 struct intel_iommu
*iommu
;
118 for_each_active_iommu(iommu
, drhd
) {
119 if (!drhd
->reg_base_addr
) {
120 seq_puts(m
, "IOMMU: Invalid base address\n");
125 seq_printf(m
, "IOMMU: %s Register Base Address: %llx\n",
126 iommu
->name
, drhd
->reg_base_addr
);
127 seq_puts(m
, "Name\t\t\tOffset\t\tContents\n");
129 * Publish the contents of the 64-bit hardware registers
130 * by adding the offset to the pointer (virtual address).
132 raw_spin_lock_irqsave(&iommu
->register_lock
, flag
);
133 for (i
= 0 ; i
< ARRAY_SIZE(iommu_regs_32
); i
++) {
134 value
= dmar_readl(iommu
->reg
+ iommu_regs_32
[i
].offset
);
135 seq_printf(m
, "%-16s\t0x%02x\t\t0x%016llx\n",
136 iommu_regs_32
[i
].regs
, iommu_regs_32
[i
].offset
,
139 for (i
= 0 ; i
< ARRAY_SIZE(iommu_regs_64
); i
++) {
140 value
= dmar_readq(iommu
->reg
+ iommu_regs_64
[i
].offset
);
141 seq_printf(m
, "%-16s\t0x%02x\t\t0x%016llx\n",
142 iommu_regs_64
[i
].regs
, iommu_regs_64
[i
].offset
,
145 raw_spin_unlock_irqrestore(&iommu
->register_lock
, flag
);
153 DEFINE_SHOW_ATTRIBUTE(iommu_regset
);
155 static inline void print_tbl_walk(struct seq_file
*m
)
157 struct tbl_walk
*tbl_wlk
= m
->private;
159 seq_printf(m
, "%02x:%02x.%x\t0x%016llx:0x%016llx\t0x%016llx:0x%016llx\t",
160 tbl_wlk
->bus
, PCI_SLOT(tbl_wlk
->devfn
),
161 PCI_FUNC(tbl_wlk
->devfn
), tbl_wlk
->rt_entry
->hi
,
162 tbl_wlk
->rt_entry
->lo
, tbl_wlk
->ctx_entry
->hi
,
163 tbl_wlk
->ctx_entry
->lo
);
166 * A legacy mode DMAR doesn't support PASID, hence default it to -1
167 * indicating that it's invalid. Also, default all PASID related fields
170 if (!tbl_wlk
->pasid_tbl_entry
)
171 seq_printf(m
, "%-6d\t0x%016llx:0x%016llx:0x%016llx\n", -1,
172 (u64
)0, (u64
)0, (u64
)0);
174 seq_printf(m
, "%-6d\t0x%016llx:0x%016llx:0x%016llx\n",
175 tbl_wlk
->pasid
, tbl_wlk
->pasid_tbl_entry
->val
[2],
176 tbl_wlk
->pasid_tbl_entry
->val
[1],
177 tbl_wlk
->pasid_tbl_entry
->val
[0]);
180 static void pasid_tbl_walk(struct seq_file
*m
, struct pasid_entry
*tbl_entry
,
183 struct tbl_walk
*tbl_wlk
= m
->private;
186 for (tbl_idx
= 0; tbl_idx
< PASID_TBL_ENTRIES
; tbl_idx
++) {
187 if (pasid_pte_is_present(tbl_entry
)) {
188 tbl_wlk
->pasid_tbl_entry
= tbl_entry
;
189 tbl_wlk
->pasid
= (dir_idx
<< PASID_PDE_SHIFT
) + tbl_idx
;
197 static void pasid_dir_walk(struct seq_file
*m
, u64 pasid_dir_ptr
,
200 struct pasid_dir_entry
*dir_entry
= phys_to_virt(pasid_dir_ptr
);
201 struct pasid_entry
*pasid_tbl
;
204 for (dir_idx
= 0; dir_idx
< pasid_dir_size
; dir_idx
++) {
205 pasid_tbl
= get_pasid_table_from_pde(dir_entry
);
207 pasid_tbl_walk(m
, pasid_tbl
, dir_idx
);
213 static void ctx_tbl_walk(struct seq_file
*m
, struct intel_iommu
*iommu
, u16 bus
)
215 struct context_entry
*context
;
216 u16 devfn
, pasid_dir_size
;
219 for (devfn
= 0; devfn
< 256; devfn
++) {
220 struct tbl_walk tbl_wlk
= {0};
223 * Scalable mode root entry points to upper scalable mode
224 * context table and lower scalable mode context table. Each
225 * scalable mode context table has 128 context entries where as
226 * legacy mode context table has 256 context entries. So in
227 * scalable mode, the context entries for former 128 devices are
228 * in the lower scalable mode context table, while the latter
229 * 128 devices are in the upper scalable mode context table.
230 * In scalable mode, when devfn > 127, iommu_context_addr()
231 * automatically refers to upper scalable mode context table and
232 * hence the caller doesn't have to worry about differences
233 * between scalable mode and non scalable mode.
235 context
= iommu_context_addr(iommu
, bus
, devfn
, 0);
239 if (!context_present(context
))
243 tbl_wlk
.devfn
= devfn
;
244 tbl_wlk
.rt_entry
= &iommu
->root_entry
[bus
];
245 tbl_wlk
.ctx_entry
= context
;
246 m
->private = &tbl_wlk
;
248 if (dmar_readq(iommu
->reg
+ DMAR_RTADDR_REG
) & DMA_RTADDR_SMT
) {
249 pasid_dir_ptr
= context
->lo
& VTD_PAGE_MASK
;
250 pasid_dir_size
= get_pasid_dir_size(context
);
251 pasid_dir_walk(m
, pasid_dir_ptr
, pasid_dir_size
);
259 static void root_tbl_walk(struct seq_file
*m
, struct intel_iommu
*iommu
)
264 spin_lock_irqsave(&iommu
->lock
, flags
);
265 seq_printf(m
, "IOMMU %s: Root Table Address: 0x%llx\n", iommu
->name
,
266 (u64
)virt_to_phys(iommu
->root_entry
));
267 seq_puts(m
, "B.D.F\tRoot_entry\t\t\t\tContext_entry\t\t\t\tPASID\tPASID_table_entry\n");
270 * No need to check if the root entry is present or not because
271 * iommu_context_addr() performs the same check before returning
274 for (bus
= 0; bus
< 256; bus
++)
275 ctx_tbl_walk(m
, iommu
, bus
);
277 spin_unlock_irqrestore(&iommu
->lock
, flags
);
280 static int dmar_translation_struct_show(struct seq_file
*m
, void *unused
)
282 struct dmar_drhd_unit
*drhd
;
283 struct intel_iommu
*iommu
;
287 for_each_active_iommu(iommu
, drhd
) {
288 sts
= dmar_readl(iommu
->reg
+ DMAR_GSTS_REG
);
289 if (!(sts
& DMA_GSTS_TES
)) {
290 seq_printf(m
, "DMA Remapping is not enabled on %s\n",
294 root_tbl_walk(m
, iommu
);
301 DEFINE_SHOW_ATTRIBUTE(dmar_translation_struct
);
303 #ifdef CONFIG_IRQ_REMAP
304 static void ir_tbl_remap_entry_show(struct seq_file
*m
,
305 struct intel_iommu
*iommu
)
307 struct irte
*ri_entry
;
311 seq_puts(m
, " Entry SrcID DstID Vct IRTE_high\t\tIRTE_low\n");
313 raw_spin_lock_irqsave(&irq_2_ir_lock
, flags
);
314 for (idx
= 0; idx
< INTR_REMAP_TABLE_ENTRIES
; idx
++) {
315 ri_entry
= &iommu
->ir_table
->base
[idx
];
316 if (!ri_entry
->present
|| ri_entry
->p_pst
)
319 seq_printf(m
, " %-5d %02x:%02x.%01x %08x %02x %016llx\t%016llx\n",
320 idx
, PCI_BUS_NUM(ri_entry
->sid
),
321 PCI_SLOT(ri_entry
->sid
), PCI_FUNC(ri_entry
->sid
),
322 ri_entry
->dest_id
, ri_entry
->vector
,
323 ri_entry
->high
, ri_entry
->low
);
325 raw_spin_unlock_irqrestore(&irq_2_ir_lock
, flags
);
328 static void ir_tbl_posted_entry_show(struct seq_file
*m
,
329 struct intel_iommu
*iommu
)
331 struct irte
*pi_entry
;
335 seq_puts(m
, " Entry SrcID PDA_high PDA_low Vct IRTE_high\t\tIRTE_low\n");
337 raw_spin_lock_irqsave(&irq_2_ir_lock
, flags
);
338 for (idx
= 0; idx
< INTR_REMAP_TABLE_ENTRIES
; idx
++) {
339 pi_entry
= &iommu
->ir_table
->base
[idx
];
340 if (!pi_entry
->present
|| !pi_entry
->p_pst
)
343 seq_printf(m
, " %-5d %02x:%02x.%01x %08x %08x %02x %016llx\t%016llx\n",
344 idx
, PCI_BUS_NUM(pi_entry
->sid
),
345 PCI_SLOT(pi_entry
->sid
), PCI_FUNC(pi_entry
->sid
),
346 pi_entry
->pda_h
, pi_entry
->pda_l
<< 6,
347 pi_entry
->vector
, pi_entry
->high
,
350 raw_spin_unlock_irqrestore(&irq_2_ir_lock
, flags
);
354 * For active IOMMUs go through the Interrupt remapping
355 * table and print valid entries in a table format for
356 * Remapped and Posted Interrupts.
358 static int ir_translation_struct_show(struct seq_file
*m
, void *unused
)
360 struct dmar_drhd_unit
*drhd
;
361 struct intel_iommu
*iommu
;
366 for_each_active_iommu(iommu
, drhd
) {
367 if (!ecap_ir_support(iommu
->ecap
))
370 seq_printf(m
, "Remapped Interrupt supported on IOMMU: %s\n",
373 sts
= dmar_readl(iommu
->reg
+ DMAR_GSTS_REG
);
374 if (iommu
->ir_table
&& (sts
& DMA_GSTS_IRES
)) {
375 irta
= virt_to_phys(iommu
->ir_table
->base
);
376 seq_printf(m
, " IR table address:%llx\n", irta
);
377 ir_tbl_remap_entry_show(m
, iommu
);
379 seq_puts(m
, "Interrupt Remapping is not enabled\n");
384 seq_puts(m
, "****\n\n");
386 for_each_active_iommu(iommu
, drhd
) {
387 if (!cap_pi_support(iommu
->cap
))
390 seq_printf(m
, "Posted Interrupt supported on IOMMU: %s\n",
393 if (iommu
->ir_table
) {
394 irta
= virt_to_phys(iommu
->ir_table
->base
);
395 seq_printf(m
, " IR table address:%llx\n", irta
);
396 ir_tbl_posted_entry_show(m
, iommu
);
398 seq_puts(m
, "Interrupt Remapping is not enabled\n");
406 DEFINE_SHOW_ATTRIBUTE(ir_translation_struct
);
409 void __init
intel_iommu_debugfs_init(void)
411 struct dentry
*intel_iommu_debug
= debugfs_create_dir("intel",
414 debugfs_create_file("iommu_regset", 0444, intel_iommu_debug
, NULL
,
416 debugfs_create_file("dmar_translation_struct", 0444, intel_iommu_debug
,
417 NULL
, &dmar_translation_struct_fops
);
418 #ifdef CONFIG_IRQ_REMAP
419 debugfs_create_file("ir_translation_struct", 0444, intel_iommu_debug
,
420 NULL
, &ir_translation_struct_fops
);