1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
// SPDX-License-Identifier: GPL-2.0
/*
* AMD IOMMU driver
*
* Copyright (C) 2018 Advanced Micro Devices, Inc.
*
* Author: Gary R Hook <gary.hook@amd.com>
*/
#include <linux/debugfs.h>
#include <linux/pci.h>
#include "amd_iommu.h"
#include "../irq_remapping.h"
static struct dentry *amd_iommu_debugfs;
#define MAX_NAME_LEN 20
#define OFS_IN_SZ 8
#define DEVID_IN_SZ 16
static int sbdf = -1;
static ssize_t iommu_mmio_write(struct file *filp, const char __user *ubuf,
size_t cnt, loff_t *ppos)
{
struct seq_file *m = filp->private_data;
struct amd_iommu *iommu = m->private;
int ret;
iommu->dbg_mmio_offset = -1;
if (cnt > OFS_IN_SZ)
return -EINVAL;
ret = kstrtou32_from_user(ubuf, cnt, 0, &iommu->dbg_mmio_offset);
if (ret)
return ret;
if (iommu->dbg_mmio_offset > iommu->mmio_phys_end - 4) {
iommu->dbg_mmio_offset = -1;
return -EINVAL;
}
return cnt;
}
static int iommu_mmio_show(struct seq_file *m, void *unused)
{
struct amd_iommu *iommu = m->private;
u64 value;
if (iommu->dbg_mmio_offset < 0) {
seq_puts(m, "Please provide mmio register's offset\n");
return 0;
}
value = readq(iommu->mmio_base + iommu->dbg_mmio_offset);
seq_printf(m, "Offset:0x%x Value:0x%016llx\n", iommu->dbg_mmio_offset, value);
return 0;
}
DEFINE_SHOW_STORE_ATTRIBUTE(iommu_mmio);
static ssize_t iommu_capability_write(struct file *filp, const char __user *ubuf,
size_t cnt, loff_t *ppos)
{
struct seq_file *m = filp->private_data;
struct amd_iommu *iommu = m->private;
int ret;
iommu->dbg_cap_offset = -1;
if (cnt > OFS_IN_SZ)
return -EINVAL;
ret = kstrtou32_from_user(ubuf, cnt, 0, &iommu->dbg_cap_offset);
if (ret)
return ret;
/* Capability register at offset 0x14 is the last IOMMU capability register. */
if (iommu->dbg_cap_offset > 0x14) {
iommu->dbg_cap_offset = -1;
return -EINVAL;
}
return cnt;
}
static int iommu_capability_show(struct seq_file *m, void *unused)
{
struct amd_iommu *iommu = m->private;
u32 value;
int err;
if (iommu->dbg_cap_offset < 0) {
seq_puts(m, "Please provide capability register's offset in the range [0x00 - 0x14]\n");
return 0;
}
err = pci_read_config_dword(iommu->dev, iommu->cap_ptr + iommu->dbg_cap_offset, &value);
if (err) {
seq_printf(m, "Not able to read capability register at 0x%x\n",
iommu->dbg_cap_offset);
return 0;
}
seq_printf(m, "Offset:0x%x Value:0x%08x\n", iommu->dbg_cap_offset, value);
return 0;
}
DEFINE_SHOW_STORE_ATTRIBUTE(iommu_capability);
static int iommu_cmdbuf_show(struct seq_file *m, void *unused)
{
struct amd_iommu *iommu = m->private;
struct iommu_cmd *cmd;
unsigned long flag;
u32 head, tail;
int i;
raw_spin_lock_irqsave(&iommu->lock, flag);
head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
seq_printf(m, "CMD Buffer Head Offset:%d Tail Offset:%d\n",
(head >> 4) & 0x7fff, (tail >> 4) & 0x7fff);
for (i = 0; i < CMD_BUFFER_ENTRIES; i++) {
cmd = (struct iommu_cmd *)(iommu->cmd_buf + i * sizeof(*cmd));
seq_printf(m, "%3d: %08x %08x %08x %08x\n", i, cmd->data[0],
cmd->data[1], cmd->data[2], cmd->data[3]);
}
raw_spin_unlock_irqrestore(&iommu->lock, flag);
return 0;
}
DEFINE_SHOW_ATTRIBUTE(iommu_cmdbuf);
static ssize_t devid_write(struct file *filp, const char __user *ubuf,
size_t cnt, loff_t *ppos)
{
struct amd_iommu_pci_seg *pci_seg;
int seg, bus, slot, func;
struct amd_iommu *iommu;
char *srcid_ptr;
u16 devid;
int i;
sbdf = -1;
if (cnt >= DEVID_IN_SZ)
return -EINVAL;
srcid_ptr = memdup_user_nul(ubuf, cnt);
if (IS_ERR(srcid_ptr))
return PTR_ERR(srcid_ptr);
i = sscanf(srcid_ptr, "%x:%x:%x.%x", &seg, &bus, &slot, &func);
if (i != 4) {
i = sscanf(srcid_ptr, "%x:%x.%x", &bus, &slot, &func);
if (i != 3) {
kfree(srcid_ptr);
return -EINVAL;
}
seg = 0;
}
devid = PCI_DEVID(bus, PCI_DEVFN(slot, func));
/* Check if user device id input is a valid input */
for_each_pci_segment(pci_seg) {
if (pci_seg->id != seg)
continue;
if (devid > pci_seg->last_bdf) {
kfree(srcid_ptr);
return -EINVAL;
}
iommu = pci_seg->rlookup_table[devid];
if (!iommu) {
kfree(srcid_ptr);
return -ENODEV;
}
break;
}
if (pci_seg->id != seg) {
kfree(srcid_ptr);
return -EINVAL;
}
sbdf = PCI_SEG_DEVID_TO_SBDF(seg, devid);
kfree(srcid_ptr);
return cnt;
}
static int devid_show(struct seq_file *m, void *unused)
{
u16 devid;
if (sbdf >= 0) {
devid = PCI_SBDF_TO_DEVID(sbdf);
seq_printf(m, "%04x:%02x:%02x.%x\n", PCI_SBDF_TO_SEGID(sbdf),
PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid));
} else
seq_puts(m, "No or Invalid input provided\n");
return 0;
}
DEFINE_SHOW_STORE_ATTRIBUTE(devid);
static void dump_dte(struct seq_file *m, struct amd_iommu_pci_seg *pci_seg, u16 devid)
{
struct dev_table_entry *dev_table;
struct amd_iommu *iommu;
iommu = pci_seg->rlookup_table[devid];
if (!iommu)
return;
dev_table = get_dev_table(iommu);
if (!dev_table) {
seq_puts(m, "Device table not found");
return;
}
seq_printf(m, "%-12s %16s %16s %16s %16s iommu\n", "DeviceId",
"QWORD[3]", "QWORD[2]", "QWORD[1]", "QWORD[0]");
seq_printf(m, "%04x:%02x:%02x.%x ", pci_seg->id, PCI_BUS_NUM(devid),
PCI_SLOT(devid), PCI_FUNC(devid));
for (int i = 3; i >= 0; --i)
seq_printf(m, "%016llx ", dev_table[devid].data[i]);
seq_printf(m, "iommu%d\n", iommu->index);
}
static int iommu_devtbl_show(struct seq_file *m, void *unused)
{
struct amd_iommu_pci_seg *pci_seg;
u16 seg, devid;
if (sbdf < 0) {
seq_puts(m, "Enter a valid device ID to 'devid' file\n");
return 0;
}
seg = PCI_SBDF_TO_SEGID(sbdf);
devid = PCI_SBDF_TO_DEVID(sbdf);
for_each_pci_segment(pci_seg) {
if (pci_seg->id != seg)
continue;
dump_dte(m, pci_seg, devid);
break;
}
return 0;
}
DEFINE_SHOW_ATTRIBUTE(iommu_devtbl);
static void dump_128_irte(struct seq_file *m, struct irq_remap_table *table, u16 int_tab_len)
{
struct irte_ga *ptr, *irte;
int index;
for (index = 0; index < int_tab_len; index++) {
ptr = (struct irte_ga *)table->table;
irte = &ptr[index];
if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
!irte->lo.fields_vapic.valid)
continue;
else if (!irte->lo.fields_remap.valid)
continue;
seq_printf(m, "IRT[%04d] %016llx %016llx\n", index, irte->hi.val, irte->lo.val);
}
}
static void dump_32_irte(struct seq_file *m, struct irq_remap_table *table, u16 int_tab_len)
{
union irte *ptr, *irte;
int index;
for (index = 0; index < int_tab_len; index++) {
ptr = (union irte *)table->table;
irte = &ptr[index];
if (!irte->fields.valid)
continue;
seq_printf(m, "IRT[%04d] %08x\n", index, irte->val);
}
}
static void dump_irte(struct seq_file *m, u16 devid, struct amd_iommu_pci_seg *pci_seg)
{
struct dev_table_entry *dev_table;
struct irq_remap_table *table;
struct amd_iommu *iommu;
unsigned long flags;
u16 int_tab_len;
table = pci_seg->irq_lookup_table[devid];
if (!table) {
seq_printf(m, "IRQ lookup table not set for %04x:%02x:%02x:%x\n",
pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid));
return;
}
iommu = pci_seg->rlookup_table[devid];
if (!iommu)
return;
dev_table = get_dev_table(iommu);
if (!dev_table) {
seq_puts(m, "Device table not found");
return;
}
int_tab_len = dev_table[devid].data[2] & DTE_INTTABLEN_MASK;
if (int_tab_len != DTE_INTTABLEN_512 && int_tab_len != DTE_INTTABLEN_2K) {
seq_puts(m, "The device's DTE contains an invalid IRT length value.");
return;
}
seq_printf(m, "DeviceId %04x:%02x:%02x.%x\n", pci_seg->id, PCI_BUS_NUM(devid),
PCI_SLOT(devid), PCI_FUNC(devid));
raw_spin_lock_irqsave(&table->lock, flags);
if (AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
dump_128_irte(m, table, BIT(int_tab_len >> 1));
else
dump_32_irte(m, table, BIT(int_tab_len >> 1));
seq_puts(m, "\n");
raw_spin_unlock_irqrestore(&table->lock, flags);
}
static int iommu_irqtbl_show(struct seq_file *m, void *unused)
{
struct amd_iommu_pci_seg *pci_seg;
u16 devid, seg;
if (!irq_remapping_enabled) {
seq_puts(m, "Interrupt remapping is disabled\n");
return 0;
}
if (sbdf < 0) {
seq_puts(m, "Enter a valid device ID to 'devid' file\n");
return 0;
}
seg = PCI_SBDF_TO_SEGID(sbdf);
devid = PCI_SBDF_TO_DEVID(sbdf);
for_each_pci_segment(pci_seg) {
if (pci_seg->id != seg)
continue;
dump_irte(m, devid, pci_seg);
break;
}
return 0;
}
DEFINE_SHOW_ATTRIBUTE(iommu_irqtbl);
void amd_iommu_debugfs_setup(void)
{
struct amd_iommu *iommu;
char name[MAX_NAME_LEN + 1];
amd_iommu_debugfs = debugfs_create_dir("amd", iommu_debugfs_dir);
for_each_iommu(iommu) {
iommu->dbg_mmio_offset = -1;
iommu->dbg_cap_offset = -1;
snprintf(name, MAX_NAME_LEN, "iommu%02d", iommu->index);
iommu->debugfs = debugfs_create_dir(name, amd_iommu_debugfs);
debugfs_create_file("mmio", 0644, iommu->debugfs, iommu,
&iommu_mmio_fops);
debugfs_create_file("capability", 0644, iommu->debugfs, iommu,
&iommu_capability_fops);
debugfs_create_file("cmdbuf", 0444, iommu->debugfs, iommu,
&iommu_cmdbuf_fops);
}
debugfs_create_file("devid", 0644, amd_iommu_debugfs, NULL,
&devid_fops);
debugfs_create_file("devtbl", 0444, amd_iommu_debugfs, NULL,
&iommu_devtbl_fops);
debugfs_create_file("irqtbl", 0444, amd_iommu_debugfs, NULL,
&iommu_irqtbl_fops);
}
|