Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
14e0e679 HD |
2 | /* |
3 | * omap iommu: debugfs interface | |
4 | * | |
5 | * Copyright (C) 2008-2009 Nokia Corporation | |
6 | * | |
7 | * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com> | |
14e0e679 HD |
8 | */ |
9 | ||
10 | #include <linux/err.h> | |
14e0e679 | 11 | #include <linux/io.h> |
5a0e3ad6 | 12 | #include <linux/slab.h> |
14e0e679 | 13 | #include <linux/uaccess.h> |
69c2c196 | 14 | #include <linux/pm_runtime.h> |
14e0e679 | 15 | #include <linux/debugfs.h> |
2ab7c848 | 16 | #include <linux/platform_data/iommu-omap.h> |
14e0e679 | 17 | |
2f7702af | 18 | #include "omap-iopgtable.h" |
ed1c7de2 | 19 | #include "omap-iommu.h" |
14e0e679 | 20 | |
14e0e679 HD |
21 | static DEFINE_MUTEX(iommu_debug_lock); |
22 | ||
23 | static struct dentry *iommu_debug_root; | |
24 | ||
c5cf5c53 SA |
25 | static inline bool is_omap_iommu_detached(struct omap_iommu *obj) |
26 | { | |
27 | return !obj->domain; | |
28 | } | |
29 | ||
69c2c196 SA |
30 | #define pr_reg(name) \ |
31 | do { \ | |
32 | ssize_t bytes; \ | |
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)); \ | |
37 | p += bytes; \ | |
38 | len -= bytes; \ | |
39 | if (len < maxcol) \ | |
40 | goto out; \ | |
41 | } while (0) | |
42 | ||
43 | static ssize_t | |
44 | omap2_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t len) | |
45 | { | |
46 | char *p = buf; | |
47 | ||
48 | pr_reg(REVISION); | |
49 | pr_reg(IRQSTATUS); | |
50 | pr_reg(IRQENABLE); | |
51 | pr_reg(WALKING_ST); | |
52 | pr_reg(CNTL); | |
53 | pr_reg(FAULT_AD); | |
54 | pr_reg(TTB); | |
55 | pr_reg(LOCK); | |
56 | pr_reg(LD_TLB); | |
57 | pr_reg(CAM); | |
58 | pr_reg(RAM); | |
59 | pr_reg(GFLUSH); | |
60 | pr_reg(FLUSH_ENTRY); | |
61 | pr_reg(READ_CAM); | |
62 | pr_reg(READ_RAM); | |
63 | pr_reg(EMU_FAULT_AD); | |
64 | out: | |
65 | return p - buf; | |
66 | } | |
67 | ||
68 | static ssize_t omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf, | |
69 | ssize_t bytes) | |
70 | { | |
71 | if (!obj || !buf) | |
72 | return -EINVAL; | |
73 | ||
74 | pm_runtime_get_sync(obj->dev); | |
75 | ||
76 | bytes = omap2_iommu_dump_ctx(obj, buf, bytes); | |
77 | ||
78 | pm_runtime_put_sync(obj->dev); | |
79 | ||
80 | return bytes; | |
81 | } | |
82 | ||
14e0e679 HD |
83 | static ssize_t debug_read_regs(struct file *file, char __user *userbuf, |
84 | size_t count, loff_t *ppos) | |
85 | { | |
61c75352 | 86 | struct omap_iommu *obj = file->private_data; |
14e0e679 HD |
87 | char *p, *buf; |
88 | ssize_t bytes; | |
89 | ||
c5cf5c53 SA |
90 | if (is_omap_iommu_detached(obj)) |
91 | return -EPERM; | |
92 | ||
14e0e679 HD |
93 | buf = kmalloc(count, GFP_KERNEL); |
94 | if (!buf) | |
95 | return -ENOMEM; | |
96 | p = buf; | |
97 | ||
98 | mutex_lock(&iommu_debug_lock); | |
99 | ||
6c32df43 | 100 | bytes = omap_iommu_dump_ctx(obj, p, count); |
dee9d154 CIK |
101 | if (bytes < 0) |
102 | goto err; | |
14e0e679 HD |
103 | bytes = simple_read_from_buffer(userbuf, count, ppos, buf, bytes); |
104 | ||
dee9d154 | 105 | err: |
14e0e679 HD |
106 | mutex_unlock(&iommu_debug_lock); |
107 | kfree(buf); | |
108 | ||
109 | return bytes; | |
110 | } | |
111 | ||
69c2c196 SA |
112 | static int |
113 | __dump_tlb_entries(struct omap_iommu *obj, struct cr_regs *crs, int num) | |
114 | { | |
115 | int i; | |
116 | struct iotlb_lock saved; | |
117 | struct cr_regs tmp; | |
118 | struct cr_regs *p = crs; | |
119 | ||
120 | pm_runtime_get_sync(obj->dev); | |
121 | iotlb_lock_get(obj, &saved); | |
122 | ||
123 | for_each_iotlb_cr(obj, num, i, tmp) { | |
124 | if (!iotlb_cr_valid(&tmp)) | |
125 | continue; | |
126 | *p++ = tmp; | |
127 | } | |
128 | ||
129 | iotlb_lock_set(obj, &saved); | |
130 | pm_runtime_put_sync(obj->dev); | |
131 | ||
132 | return p - crs; | |
133 | } | |
134 | ||
135 | static ssize_t iotlb_dump_cr(struct omap_iommu *obj, struct cr_regs *cr, | |
e203db29 | 136 | struct seq_file *s) |
69c2c196 | 137 | { |
6798a8ca | 138 | seq_printf(s, "%08x %08x %01x\n", cr->cam, cr->ram, |
a5c0e0b4 | 139 | (cr->cam & MMU_CAM_P) ? 1 : 0); |
6798a8ca | 140 | return 0; |
69c2c196 SA |
141 | } |
142 | ||
e203db29 | 143 | static size_t omap_dump_tlb_entries(struct omap_iommu *obj, struct seq_file *s) |
69c2c196 SA |
144 | { |
145 | int i, num; | |
146 | struct cr_regs *cr; | |
69c2c196 | 147 | |
e203db29 | 148 | num = obj->nr_tlb_entries; |
69c2c196 SA |
149 | |
150 | cr = kcalloc(num, sizeof(*cr), GFP_KERNEL); | |
151 | if (!cr) | |
152 | return 0; | |
153 | ||
154 | num = __dump_tlb_entries(obj, cr, num); | |
155 | for (i = 0; i < num; i++) | |
e203db29 | 156 | iotlb_dump_cr(obj, cr + i, s); |
69c2c196 SA |
157 | kfree(cr); |
158 | ||
e203db29 | 159 | return 0; |
69c2c196 SA |
160 | } |
161 | ||
a6906a8b | 162 | static int tlb_show(struct seq_file *s, void *data) |
14e0e679 | 163 | { |
e203db29 | 164 | struct omap_iommu *obj = s->private; |
14e0e679 | 165 | |
c5cf5c53 SA |
166 | if (is_omap_iommu_detached(obj)) |
167 | return -EPERM; | |
168 | ||
14e0e679 HD |
169 | mutex_lock(&iommu_debug_lock); |
170 | ||
e203db29 SP |
171 | seq_printf(s, "%8s %8s\n", "cam:", "ram:"); |
172 | seq_puts(s, "-----------------------------------------\n"); | |
173 | omap_dump_tlb_entries(obj, s); | |
14e0e679 HD |
174 | |
175 | mutex_unlock(&iommu_debug_lock); | |
14e0e679 | 176 | |
e203db29 | 177 | return 0; |
14e0e679 HD |
178 | } |
179 | ||
9c83e9f3 | 180 | static void dump_ioptable(struct seq_file *s) |
14e0e679 | 181 | { |
9c83e9f3 SA |
182 | int i, j; |
183 | u32 da; | |
184 | u32 *iopgd, *iopte; | |
185 | struct omap_iommu *obj = s->private; | |
14e0e679 HD |
186 | |
187 | spin_lock(&obj->page_table_lock); | |
188 | ||
189 | iopgd = iopgd_offset(obj, 0); | |
190 | for (i = 0; i < PTRS_PER_IOPGD; i++, iopgd++) { | |
14e0e679 HD |
191 | if (!*iopgd) |
192 | continue; | |
193 | ||
194 | if (!(*iopgd & IOPGD_TABLE)) { | |
195 | da = i << IOPGD_SHIFT; | |
9c83e9f3 | 196 | seq_printf(s, "1: 0x%08x 0x%08x\n", da, *iopgd); |
14e0e679 HD |
197 | continue; |
198 | } | |
199 | ||
200 | iopte = iopte_offset(iopgd, 0); | |
14e0e679 HD |
201 | for (j = 0; j < PTRS_PER_IOPTE; j++, iopte++) { |
202 | if (!*iopte) | |
203 | continue; | |
204 | ||
205 | da = (i << IOPGD_SHIFT) + (j << IOPTE_SHIFT); | |
9c83e9f3 | 206 | seq_printf(s, "2: 0x%08x 0x%08x\n", da, *iopte); |
14e0e679 HD |
207 | } |
208 | } | |
14e0e679 | 209 | |
9c83e9f3 | 210 | spin_unlock(&obj->page_table_lock); |
14e0e679 HD |
211 | } |
212 | ||
a6906a8b | 213 | static int pagetable_show(struct seq_file *s, void *data) |
14e0e679 | 214 | { |
9c83e9f3 | 215 | struct omap_iommu *obj = s->private; |
14e0e679 | 216 | |
c5cf5c53 SA |
217 | if (is_omap_iommu_detached(obj)) |
218 | return -EPERM; | |
219 | ||
14e0e679 HD |
220 | mutex_lock(&iommu_debug_lock); |
221 | ||
9c83e9f3 SA |
222 | seq_printf(s, "L: %8s %8s\n", "da:", "pte:"); |
223 | seq_puts(s, "--------------------------\n"); | |
224 | dump_ioptable(s); | |
14e0e679 HD |
225 | |
226 | mutex_unlock(&iommu_debug_lock); | |
14e0e679 | 227 | |
9c83e9f3 | 228 | return 0; |
14e0e679 HD |
229 | } |
230 | ||
14e0e679 | 231 | #define DEBUG_FOPS_RO(name) \ |
a6906a8b | 232 | static const struct file_operations name##_fops = { \ |
234e3405 | 233 | .open = simple_open, \ |
14e0e679 | 234 | .read = debug_read_##name, \ |
c0b0aca0 | 235 | .llseek = generic_file_llseek, \ |
5b39a37a | 236 | } |
14e0e679 | 237 | |
14e0e679 | 238 | DEBUG_FOPS_RO(regs); |
a6906a8b YL |
239 | DEFINE_SHOW_ATTRIBUTE(tlb); |
240 | DEFINE_SHOW_ATTRIBUTE(pagetable); | |
14e0e679 | 241 | |
61c75352 | 242 | void omap_iommu_debugfs_add(struct omap_iommu *obj) |
14e0e679 | 243 | { |
61c75352 | 244 | struct dentry *d; |
46451d62 | 245 | |
61c75352 SA |
246 | if (!iommu_debug_root) |
247 | return; | |
46451d62 | 248 | |
9378bfea GKH |
249 | d = debugfs_create_dir(obj->name, iommu_debug_root); |
250 | obj->debug_dir = d; | |
14e0e679 | 251 | |
9378bfea GKH |
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); | |
46451d62 OBC |
256 | } |
257 | ||
61c75352 | 258 | void omap_iommu_debugfs_remove(struct omap_iommu *obj) |
46451d62 | 259 | { |
61c75352 SA |
260 | if (!obj->debug_dir) |
261 | return; | |
46451d62 | 262 | |
61c75352 | 263 | debugfs_remove_recursive(obj->debug_dir); |
14e0e679 HD |
264 | } |
265 | ||
61c75352 | 266 | void __init omap_iommu_debugfs_init(void) |
14e0e679 | 267 | { |
61c75352 | 268 | iommu_debug_root = debugfs_create_dir("omap_iommu", NULL); |
14e0e679 | 269 | } |
14e0e679 | 270 | |
61c75352 | 271 | void __exit omap_iommu_debugfs_exit(void) |
14e0e679 | 272 | { |
61c75352 | 273 | debugfs_remove(iommu_debug_root); |
14e0e679 | 274 | } |