GNU Linux-libre 4.4.288-gnu1
[releases.git] / drivers / iommu / omap-iommu-debug.c
1 /*
2  * omap iommu: debugfs interface
3  *
4  * Copyright (C) 2008-2009 Nokia Corporation
5  *
6  * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/err.h>
14 #include <linux/io.h>
15 #include <linux/slab.h>
16 #include <linux/uaccess.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/debugfs.h>
19 #include <linux/platform_data/iommu-omap.h>
20
21 #include "omap-iopgtable.h"
22 #include "omap-iommu.h"
23
24 static DEFINE_MUTEX(iommu_debug_lock);
25
26 static struct dentry *iommu_debug_root;
27
28 static inline bool is_omap_iommu_detached(struct omap_iommu *obj)
29 {
30         return !obj->domain;
31 }
32
33 #define pr_reg(name)                                                    \
34         do {                                                            \
35                 ssize_t bytes;                                          \
36                 const char *str = "%20s: %08x\n";                       \
37                 const int maxcol = 32;                                  \
38                 bytes = snprintf(p, maxcol, str, __stringify(name),     \
39                                  iommu_read_reg(obj, MMU_##name));      \
40                 p += bytes;                                             \
41                 len -= bytes;                                           \
42                 if (len < maxcol)                                       \
43                         goto out;                                       \
44         } while (0)
45
46 static ssize_t
47 omap2_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t len)
48 {
49         char *p = buf;
50
51         pr_reg(REVISION);
52         pr_reg(IRQSTATUS);
53         pr_reg(IRQENABLE);
54         pr_reg(WALKING_ST);
55         pr_reg(CNTL);
56         pr_reg(FAULT_AD);
57         pr_reg(TTB);
58         pr_reg(LOCK);
59         pr_reg(LD_TLB);
60         pr_reg(CAM);
61         pr_reg(RAM);
62         pr_reg(GFLUSH);
63         pr_reg(FLUSH_ENTRY);
64         pr_reg(READ_CAM);
65         pr_reg(READ_RAM);
66         pr_reg(EMU_FAULT_AD);
67 out:
68         return p - buf;
69 }
70
71 static ssize_t omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf,
72                                    ssize_t bytes)
73 {
74         if (!obj || !buf)
75                 return -EINVAL;
76
77         pm_runtime_get_sync(obj->dev);
78
79         bytes = omap2_iommu_dump_ctx(obj, buf, bytes);
80
81         pm_runtime_put_sync(obj->dev);
82
83         return bytes;
84 }
85
86 static ssize_t debug_read_regs(struct file *file, char __user *userbuf,
87                                size_t count, loff_t *ppos)
88 {
89         struct omap_iommu *obj = file->private_data;
90         char *p, *buf;
91         ssize_t bytes;
92
93         if (is_omap_iommu_detached(obj))
94                 return -EPERM;
95
96         buf = kmalloc(count, GFP_KERNEL);
97         if (!buf)
98                 return -ENOMEM;
99         p = buf;
100
101         mutex_lock(&iommu_debug_lock);
102
103         bytes = omap_iommu_dump_ctx(obj, p, count);
104         if (bytes < 0)
105                 goto err;
106         bytes = simple_read_from_buffer(userbuf, count, ppos, buf, bytes);
107
108 err:
109         mutex_unlock(&iommu_debug_lock);
110         kfree(buf);
111
112         return bytes;
113 }
114
115 static int
116 __dump_tlb_entries(struct omap_iommu *obj, struct cr_regs *crs, int num)
117 {
118         int i;
119         struct iotlb_lock saved;
120         struct cr_regs tmp;
121         struct cr_regs *p = crs;
122
123         pm_runtime_get_sync(obj->dev);
124         iotlb_lock_get(obj, &saved);
125
126         for_each_iotlb_cr(obj, num, i, tmp) {
127                 if (!iotlb_cr_valid(&tmp))
128                         continue;
129                 *p++ = tmp;
130         }
131
132         iotlb_lock_set(obj, &saved);
133         pm_runtime_put_sync(obj->dev);
134
135         return  p - crs;
136 }
137
138 static ssize_t iotlb_dump_cr(struct omap_iommu *obj, struct cr_regs *cr,
139                              struct seq_file *s)
140 {
141         seq_printf(s, "%08x %08x %01x\n", cr->cam, cr->ram,
142                           (cr->cam & MMU_CAM_P) ? 1 : 0);
143         return 0;
144 }
145
146 static size_t omap_dump_tlb_entries(struct omap_iommu *obj, struct seq_file *s)
147 {
148         int i, num;
149         struct cr_regs *cr;
150
151         num = obj->nr_tlb_entries;
152
153         cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
154         if (!cr)
155                 return 0;
156
157         num = __dump_tlb_entries(obj, cr, num);
158         for (i = 0; i < num; i++)
159                 iotlb_dump_cr(obj, cr + i, s);
160         kfree(cr);
161
162         return 0;
163 }
164
165 static int debug_read_tlb(struct seq_file *s, void *data)
166 {
167         struct omap_iommu *obj = s->private;
168
169         if (is_omap_iommu_detached(obj))
170                 return -EPERM;
171
172         mutex_lock(&iommu_debug_lock);
173
174         seq_printf(s, "%8s %8s\n", "cam:", "ram:");
175         seq_puts(s, "-----------------------------------------\n");
176         omap_dump_tlb_entries(obj, s);
177
178         mutex_unlock(&iommu_debug_lock);
179
180         return 0;
181 }
182
183 static void dump_ioptable(struct seq_file *s)
184 {
185         int i, j;
186         u32 da;
187         u32 *iopgd, *iopte;
188         struct omap_iommu *obj = s->private;
189
190         spin_lock(&obj->page_table_lock);
191
192         iopgd = iopgd_offset(obj, 0);
193         for (i = 0; i < PTRS_PER_IOPGD; i++, iopgd++) {
194                 if (!*iopgd)
195                         continue;
196
197                 if (!(*iopgd & IOPGD_TABLE)) {
198                         da = i << IOPGD_SHIFT;
199                         seq_printf(s, "1: 0x%08x 0x%08x\n", da, *iopgd);
200                         continue;
201                 }
202
203                 iopte = iopte_offset(iopgd, 0);
204                 for (j = 0; j < PTRS_PER_IOPTE; j++, iopte++) {
205                         if (!*iopte)
206                                 continue;
207
208                         da = (i << IOPGD_SHIFT) + (j << IOPTE_SHIFT);
209                         seq_printf(s, "2: 0x%08x 0x%08x\n", da, *iopte);
210                 }
211         }
212
213         spin_unlock(&obj->page_table_lock);
214 }
215
216 static int debug_read_pagetable(struct seq_file *s, void *data)
217 {
218         struct omap_iommu *obj = s->private;
219
220         if (is_omap_iommu_detached(obj))
221                 return -EPERM;
222
223         mutex_lock(&iommu_debug_lock);
224
225         seq_printf(s, "L: %8s %8s\n", "da:", "pte:");
226         seq_puts(s, "--------------------------\n");
227         dump_ioptable(s);
228
229         mutex_unlock(&iommu_debug_lock);
230
231         return 0;
232 }
233
234 #define DEBUG_SEQ_FOPS_RO(name)                                                \
235         static int debug_open_##name(struct inode *inode, struct file *file)   \
236         {                                                                      \
237                 return single_open(file, debug_read_##name, inode->i_private); \
238         }                                                                      \
239                                                                                \
240         static const struct file_operations debug_##name##_fops = {            \
241                 .open           = debug_open_##name,                           \
242                 .read           = seq_read,                                    \
243                 .llseek         = seq_lseek,                                   \
244                 .release        = single_release,                              \
245         }
246
247 #define DEBUG_FOPS_RO(name)                                             \
248         static const struct file_operations debug_##name##_fops = {     \
249                 .open = simple_open,                                    \
250                 .read = debug_read_##name,                              \
251                 .llseek = generic_file_llseek,                          \
252         }
253
254 DEBUG_FOPS_RO(regs);
255 DEBUG_SEQ_FOPS_RO(tlb);
256 DEBUG_SEQ_FOPS_RO(pagetable);
257
258 #define __DEBUG_ADD_FILE(attr, mode)                                    \
259         {                                                               \
260                 struct dentry *dent;                                    \
261                 dent = debugfs_create_file(#attr, mode, obj->debug_dir, \
262                                            obj, &debug_##attr##_fops);  \
263                 if (!dent)                                              \
264                         goto err;                                       \
265         }
266
267 #define DEBUG_ADD_FILE_RO(name) __DEBUG_ADD_FILE(name, 0400)
268
269 void omap_iommu_debugfs_add(struct omap_iommu *obj)
270 {
271         struct dentry *d;
272
273         if (!iommu_debug_root)
274                 return;
275
276         obj->debug_dir = debugfs_create_dir(obj->name, iommu_debug_root);
277         if (!obj->debug_dir)
278                 return;
279
280         d = debugfs_create_u8("nr_tlb_entries", 0400, obj->debug_dir,
281                               (u8 *)&obj->nr_tlb_entries);
282         if (!d)
283                 return;
284
285         DEBUG_ADD_FILE_RO(regs);
286         DEBUG_ADD_FILE_RO(tlb);
287         DEBUG_ADD_FILE_RO(pagetable);
288
289         return;
290
291 err:
292         debugfs_remove_recursive(obj->debug_dir);
293 }
294
295 void omap_iommu_debugfs_remove(struct omap_iommu *obj)
296 {
297         if (!obj->debug_dir)
298                 return;
299
300         debugfs_remove_recursive(obj->debug_dir);
301 }
302
303 void __init omap_iommu_debugfs_init(void)
304 {
305         iommu_debug_root = debugfs_create_dir("omap_iommu", NULL);
306         if (!iommu_debug_root)
307                 pr_err("can't create debugfs dir\n");
308 }
309
310 void __exit omap_iommu_debugfs_exit(void)
311 {
312         debugfs_remove(iommu_debug_root);
313 }