iommu/omap: Align code with open parenthesis
[deliverable/linux.git] / drivers / iommu / omap-iommu-debug.c
CommitLineData
14e0e679
HD
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>
14e0e679 14#include <linux/io.h>
5a0e3ad6 15#include <linux/slab.h>
14e0e679 16#include <linux/uaccess.h>
69c2c196 17#include <linux/pm_runtime.h>
14e0e679 18#include <linux/debugfs.h>
2ab7c848 19#include <linux/platform_data/iommu-omap.h>
14e0e679 20
2f7702af 21#include "omap-iopgtable.h"
ed1c7de2 22#include "omap-iommu.h"
14e0e679 23
14e0e679
HD
24static DEFINE_MUTEX(iommu_debug_lock);
25
26static struct dentry *iommu_debug_root;
27
c5cf5c53
SA
28static inline bool is_omap_iommu_detached(struct omap_iommu *obj)
29{
30 return !obj->domain;
31}
32
69c2c196
SA
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
46static ssize_t
47omap2_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);
67out:
68 return p - buf;
69}
70
71static 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
14e0e679
HD
86static ssize_t debug_read_regs(struct file *file, char __user *userbuf,
87 size_t count, loff_t *ppos)
88{
61c75352 89 struct omap_iommu *obj = file->private_data;
14e0e679
HD
90 char *p, *buf;
91 ssize_t bytes;
92
c5cf5c53
SA
93 if (is_omap_iommu_detached(obj))
94 return -EPERM;
95
14e0e679
HD
96 buf = kmalloc(count, GFP_KERNEL);
97 if (!buf)
98 return -ENOMEM;
99 p = buf;
100
101 mutex_lock(&iommu_debug_lock);
102
6c32df43 103 bytes = omap_iommu_dump_ctx(obj, p, count);
14e0e679
HD
104 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, bytes);
105
106 mutex_unlock(&iommu_debug_lock);
107 kfree(buf);
108
109 return bytes;
110}
111
69c2c196
SA
112static 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
135static ssize_t iotlb_dump_cr(struct omap_iommu *obj, struct cr_regs *cr,
136 char *buf)
137{
138 char *p = buf;
139
140 /* FIXME: Need more detail analysis of cam/ram */
141 p += sprintf(p, "%08x %08x %01x\n", cr->cam, cr->ram,
142 (cr->cam & MMU_CAM_P) ? 1 : 0);
143
144 return p - buf;
145}
146
147static size_t omap_dump_tlb_entries(struct omap_iommu *obj, char *buf,
148 ssize_t bytes)
149{
150 int i, num;
151 struct cr_regs *cr;
152 char *p = buf;
153
154 num = bytes / sizeof(*cr);
155 num = min(obj->nr_tlb_entries, num);
156
157 cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
158 if (!cr)
159 return 0;
160
161 num = __dump_tlb_entries(obj, cr, num);
162 for (i = 0; i < num; i++)
163 p += iotlb_dump_cr(obj, cr + i, p);
164 kfree(cr);
165
166 return p - buf;
167}
168
14e0e679
HD
169static ssize_t debug_read_tlb(struct file *file, char __user *userbuf,
170 size_t count, loff_t *ppos)
171{
61c75352 172 struct omap_iommu *obj = file->private_data;
14e0e679
HD
173 char *p, *buf;
174 ssize_t bytes, rest;
175
c5cf5c53
SA
176 if (is_omap_iommu_detached(obj))
177 return -EPERM;
178
14e0e679
HD
179 buf = kmalloc(count, GFP_KERNEL);
180 if (!buf)
181 return -ENOMEM;
182 p = buf;
183
184 mutex_lock(&iommu_debug_lock);
185
186 p += sprintf(p, "%8s %8s\n", "cam:", "ram:");
187 p += sprintf(p, "-----------------------------------------\n");
188 rest = count - (p - buf);
6c32df43 189 p += omap_dump_tlb_entries(obj, p, rest);
14e0e679
HD
190
191 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
192
193 mutex_unlock(&iommu_debug_lock);
194 kfree(buf);
195
196 return bytes;
197}
198
9c83e9f3 199static void dump_ioptable(struct seq_file *s)
14e0e679 200{
9c83e9f3
SA
201 int i, j;
202 u32 da;
203 u32 *iopgd, *iopte;
204 struct omap_iommu *obj = s->private;
14e0e679
HD
205
206 spin_lock(&obj->page_table_lock);
207
208 iopgd = iopgd_offset(obj, 0);
209 for (i = 0; i < PTRS_PER_IOPGD; i++, iopgd++) {
14e0e679
HD
210 if (!*iopgd)
211 continue;
212
213 if (!(*iopgd & IOPGD_TABLE)) {
214 da = i << IOPGD_SHIFT;
9c83e9f3 215 seq_printf(s, "1: 0x%08x 0x%08x\n", da, *iopgd);
14e0e679
HD
216 continue;
217 }
218
219 iopte = iopte_offset(iopgd, 0);
14e0e679
HD
220 for (j = 0; j < PTRS_PER_IOPTE; j++, iopte++) {
221 if (!*iopte)
222 continue;
223
224 da = (i << IOPGD_SHIFT) + (j << IOPTE_SHIFT);
9c83e9f3 225 seq_printf(s, "2: 0x%08x 0x%08x\n", da, *iopte);
14e0e679
HD
226 }
227 }
14e0e679 228
9c83e9f3 229 spin_unlock(&obj->page_table_lock);
14e0e679
HD
230}
231
9c83e9f3 232static int debug_read_pagetable(struct seq_file *s, void *data)
14e0e679 233{
9c83e9f3 234 struct omap_iommu *obj = s->private;
14e0e679 235
c5cf5c53
SA
236 if (is_omap_iommu_detached(obj))
237 return -EPERM;
238
14e0e679
HD
239 mutex_lock(&iommu_debug_lock);
240
9c83e9f3
SA
241 seq_printf(s, "L: %8s %8s\n", "da:", "pte:");
242 seq_puts(s, "--------------------------\n");
243 dump_ioptable(s);
14e0e679
HD
244
245 mutex_unlock(&iommu_debug_lock);
14e0e679 246
9c83e9f3 247 return 0;
14e0e679
HD
248}
249
9c83e9f3
SA
250#define DEBUG_SEQ_FOPS_RO(name) \
251 static int debug_open_##name(struct inode *inode, struct file *file) \
252 { \
253 return single_open(file, debug_read_##name, inode->i_private); \
254 } \
255 \
256 static const struct file_operations debug_##name##_fops = { \
257 .open = debug_open_##name, \
258 .read = seq_read, \
259 .llseek = seq_lseek, \
260 .release = single_release, \
261 }
262
14e0e679
HD
263#define DEBUG_FOPS_RO(name) \
264 static const struct file_operations debug_##name##_fops = { \
234e3405 265 .open = simple_open, \
14e0e679 266 .read = debug_read_##name, \
c0b0aca0 267 .llseek = generic_file_llseek, \
5b39a37a 268 }
14e0e679 269
14e0e679
HD
270DEBUG_FOPS_RO(regs);
271DEBUG_FOPS_RO(tlb);
9c83e9f3 272DEBUG_SEQ_FOPS_RO(pagetable);
14e0e679
HD
273
274#define __DEBUG_ADD_FILE(attr, mode) \
275 { \
276 struct dentry *dent; \
61c75352
SA
277 dent = debugfs_create_file(#attr, mode, obj->debug_dir, \
278 obj, &debug_##attr##_fops); \
14e0e679 279 if (!dent) \
61c75352 280 goto err; \
14e0e679
HD
281 }
282
ff3a2b73 283#define DEBUG_ADD_FILE_RO(name) __DEBUG_ADD_FILE(name, 0400)
14e0e679 284
61c75352 285void omap_iommu_debugfs_add(struct omap_iommu *obj)
14e0e679 286{
61c75352 287 struct dentry *d;
46451d62 288
61c75352
SA
289 if (!iommu_debug_root)
290 return;
46451d62 291
61c75352
SA
292 obj->debug_dir = debugfs_create_dir(obj->name, iommu_debug_root);
293 if (!obj->debug_dir)
294 return;
14e0e679 295
61c75352 296 d = debugfs_create_u8("nr_tlb_entries", 0400, obj->debug_dir,
14e0e679
HD
297 (u8 *)&obj->nr_tlb_entries);
298 if (!d)
61c75352 299 return;
14e0e679 300
14e0e679
HD
301 DEBUG_ADD_FILE_RO(regs);
302 DEBUG_ADD_FILE_RO(tlb);
3ca5db07 303 DEBUG_ADD_FILE_RO(pagetable);
14e0e679 304
61c75352 305 return;
46451d62 306
61c75352
SA
307err:
308 debugfs_remove_recursive(obj->debug_dir);
46451d62
OBC
309}
310
61c75352 311void omap_iommu_debugfs_remove(struct omap_iommu *obj)
46451d62 312{
61c75352
SA
313 if (!obj->debug_dir)
314 return;
46451d62 315
61c75352 316 debugfs_remove_recursive(obj->debug_dir);
14e0e679
HD
317}
318
61c75352 319void __init omap_iommu_debugfs_init(void)
14e0e679 320{
61c75352
SA
321 iommu_debug_root = debugfs_create_dir("omap_iommu", NULL);
322 if (!iommu_debug_root)
323 pr_err("can't create debugfs dir\n");
14e0e679 324}
14e0e679 325
61c75352 326void __exit omap_iommu_debugfs_exit(void)
14e0e679 327{
61c75352 328 debugfs_remove(iommu_debug_root);
14e0e679 329}
This page took 0.371789 seconds and 5 git commands to generate.