1 // SPDX-License-Identifier: GPL-2.0-only
3 * omap iommu: debugfs interface
5 * Copyright (C) 2008-2009 Nokia Corporation
7 * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
10 #include <linux/err.h>
12 #include <linux/slab.h>
13 #include <linux/uaccess.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/debugfs.h>
16 #include <linux/platform_data/iommu-omap.h>
18 #include "omap-iopgtable.h"
19 #include "omap-iommu.h"
21 static DEFINE_MUTEX(iommu_debug_lock
);
23 static struct dentry
*iommu_debug_root
;
25 static inline bool is_omap_iommu_detached(struct omap_iommu
*obj
)
30 #define pr_reg(name) \
33 const char *str = "%20s: %08x\n"; \
34 const int maxcol = 32; \
35 bytes = snprintf(p, maxcol, str, __stringify(name), \
36 iommu_read_reg(obj, MMU_##name)); \
44 omap2_iommu_dump_ctx(struct omap_iommu
*obj
, char *buf
, ssize_t len
)
68 static ssize_t
omap_iommu_dump_ctx(struct omap_iommu
*obj
, char *buf
,
74 pm_runtime_get_sync(obj
->dev
);
76 bytes
= omap2_iommu_dump_ctx(obj
, buf
, bytes
);
78 pm_runtime_put_sync(obj
->dev
);
83 static ssize_t
debug_read_regs(struct file
*file
, char __user
*userbuf
,
84 size_t count
, loff_t
*ppos
)
86 struct omap_iommu
*obj
= file
->private_data
;
90 if (is_omap_iommu_detached(obj
))
93 buf
= kmalloc(count
, GFP_KERNEL
);
98 mutex_lock(&iommu_debug_lock
);
100 bytes
= omap_iommu_dump_ctx(obj
, p
, count
);
103 bytes
= simple_read_from_buffer(userbuf
, count
, ppos
, buf
, bytes
);
106 mutex_unlock(&iommu_debug_lock
);
113 __dump_tlb_entries(struct omap_iommu
*obj
, struct cr_regs
*crs
, int num
)
116 struct iotlb_lock saved
;
118 struct cr_regs
*p
= crs
;
120 pm_runtime_get_sync(obj
->dev
);
121 iotlb_lock_get(obj
, &saved
);
123 for_each_iotlb_cr(obj
, num
, i
, tmp
) {
124 if (!iotlb_cr_valid(&tmp
))
129 iotlb_lock_set(obj
, &saved
);
130 pm_runtime_put_sync(obj
->dev
);
135 static ssize_t
iotlb_dump_cr(struct omap_iommu
*obj
, struct cr_regs
*cr
,
138 seq_printf(s
, "%08x %08x %01x\n", cr
->cam
, cr
->ram
,
139 (cr
->cam
& MMU_CAM_P
) ? 1 : 0);
143 static size_t omap_dump_tlb_entries(struct omap_iommu
*obj
, struct seq_file
*s
)
148 num
= obj
->nr_tlb_entries
;
150 cr
= kcalloc(num
, sizeof(*cr
), GFP_KERNEL
);
154 num
= __dump_tlb_entries(obj
, cr
, num
);
155 for (i
= 0; i
< num
; i
++)
156 iotlb_dump_cr(obj
, cr
+ i
, s
);
162 static int tlb_show(struct seq_file
*s
, void *data
)
164 struct omap_iommu
*obj
= s
->private;
166 if (is_omap_iommu_detached(obj
))
169 mutex_lock(&iommu_debug_lock
);
171 seq_printf(s
, "%8s %8s\n", "cam:", "ram:");
172 seq_puts(s
, "-----------------------------------------\n");
173 omap_dump_tlb_entries(obj
, s
);
175 mutex_unlock(&iommu_debug_lock
);
180 static void dump_ioptable(struct seq_file
*s
)
185 struct omap_iommu
*obj
= s
->private;
187 spin_lock(&obj
->page_table_lock
);
189 iopgd
= iopgd_offset(obj
, 0);
190 for (i
= 0; i
< PTRS_PER_IOPGD
; i
++, iopgd
++) {
194 if (!(*iopgd
& IOPGD_TABLE
)) {
195 da
= i
<< IOPGD_SHIFT
;
196 seq_printf(s
, "1: 0x%08x 0x%08x\n", da
, *iopgd
);
200 iopte
= iopte_offset(iopgd
, 0);
201 for (j
= 0; j
< PTRS_PER_IOPTE
; j
++, iopte
++) {
205 da
= (i
<< IOPGD_SHIFT
) + (j
<< IOPTE_SHIFT
);
206 seq_printf(s
, "2: 0x%08x 0x%08x\n", da
, *iopte
);
210 spin_unlock(&obj
->page_table_lock
);
213 static int pagetable_show(struct seq_file
*s
, void *data
)
215 struct omap_iommu
*obj
= s
->private;
217 if (is_omap_iommu_detached(obj
))
220 mutex_lock(&iommu_debug_lock
);
222 seq_printf(s
, "L: %8s %8s\n", "da:", "pte:");
223 seq_puts(s
, "--------------------------\n");
226 mutex_unlock(&iommu_debug_lock
);
231 #define DEBUG_FOPS_RO(name) \
232 static const struct file_operations name##_fops = { \
233 .open = simple_open, \
234 .read = debug_read_##name, \
235 .llseek = generic_file_llseek, \
239 DEFINE_SHOW_ATTRIBUTE(tlb
);
240 DEFINE_SHOW_ATTRIBUTE(pagetable
);
242 void omap_iommu_debugfs_add(struct omap_iommu
*obj
)
246 if (!iommu_debug_root
)
249 d
= debugfs_create_dir(obj
->name
, iommu_debug_root
);
252 debugfs_create_u32("nr_tlb_entries", 0400, d
, &obj
->nr_tlb_entries
);
253 debugfs_create_file("regs", 0400, d
, obj
, ®s_fops
);
254 debugfs_create_file("tlb", 0400, d
, obj
, &tlb_fops
);
255 debugfs_create_file("pagetable", 0400, d
, obj
, &pagetable_fops
);
258 void omap_iommu_debugfs_remove(struct omap_iommu
*obj
)
263 debugfs_remove_recursive(obj
->debug_dir
);
266 void __init
omap_iommu_debugfs_init(void)
268 iommu_debug_root
= debugfs_create_dir("omap_iommu", NULL
);
271 void __exit
omap_iommu_debugfs_exit(void)
273 debugfs_remove(iommu_debug_root
);