iommu/omap: Integrate omap-iommu-debug into omap-iommu
[deliverable/linux.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/debugfs.h>
18 #include <linux/platform_data/iommu-omap.h>
19
20 #include "omap-iopgtable.h"
21 #include "omap-iommu.h"
22
23 static DEFINE_MUTEX(iommu_debug_lock);
24
25 static struct dentry *iommu_debug_root;
26
27 static ssize_t debug_read_regs(struct file *file, char __user *userbuf,
28 size_t count, loff_t *ppos)
29 {
30 struct omap_iommu *obj = file->private_data;
31 char *p, *buf;
32 ssize_t bytes;
33
34 buf = kmalloc(count, GFP_KERNEL);
35 if (!buf)
36 return -ENOMEM;
37 p = buf;
38
39 mutex_lock(&iommu_debug_lock);
40
41 bytes = omap_iommu_dump_ctx(obj, p, count);
42 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, bytes);
43
44 mutex_unlock(&iommu_debug_lock);
45 kfree(buf);
46
47 return bytes;
48 }
49
50 static ssize_t debug_read_tlb(struct file *file, char __user *userbuf,
51 size_t count, loff_t *ppos)
52 {
53 struct omap_iommu *obj = file->private_data;
54 char *p, *buf;
55 ssize_t bytes, rest;
56
57 buf = kmalloc(count, GFP_KERNEL);
58 if (!buf)
59 return -ENOMEM;
60 p = buf;
61
62 mutex_lock(&iommu_debug_lock);
63
64 p += sprintf(p, "%8s %8s\n", "cam:", "ram:");
65 p += sprintf(p, "-----------------------------------------\n");
66 rest = count - (p - buf);
67 p += omap_dump_tlb_entries(obj, p, rest);
68
69 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
70
71 mutex_unlock(&iommu_debug_lock);
72 kfree(buf);
73
74 return bytes;
75 }
76
77 #define dump_ioptable_entry_one(lv, da, val) \
78 ({ \
79 int __err = 0; \
80 ssize_t bytes; \
81 const int maxcol = 22; \
82 const char *str = "%d: %08x %08x\n"; \
83 bytes = snprintf(p, maxcol, str, lv, da, val); \
84 p += bytes; \
85 len -= bytes; \
86 if (len < maxcol) \
87 __err = -ENOMEM; \
88 __err; \
89 })
90
91 static ssize_t dump_ioptable(struct omap_iommu *obj, char *buf, ssize_t len)
92 {
93 int i;
94 u32 *iopgd;
95 char *p = buf;
96
97 spin_lock(&obj->page_table_lock);
98
99 iopgd = iopgd_offset(obj, 0);
100 for (i = 0; i < PTRS_PER_IOPGD; i++, iopgd++) {
101 int j, err;
102 u32 *iopte;
103 u32 da;
104
105 if (!*iopgd)
106 continue;
107
108 if (!(*iopgd & IOPGD_TABLE)) {
109 da = i << IOPGD_SHIFT;
110
111 err = dump_ioptable_entry_one(1, da, *iopgd);
112 if (err)
113 goto out;
114 continue;
115 }
116
117 iopte = iopte_offset(iopgd, 0);
118
119 for (j = 0; j < PTRS_PER_IOPTE; j++, iopte++) {
120 if (!*iopte)
121 continue;
122
123 da = (i << IOPGD_SHIFT) + (j << IOPTE_SHIFT);
124 err = dump_ioptable_entry_one(2, da, *iopgd);
125 if (err)
126 goto out;
127 }
128 }
129 out:
130 spin_unlock(&obj->page_table_lock);
131
132 return p - buf;
133 }
134
135 static ssize_t debug_read_pagetable(struct file *file, char __user *userbuf,
136 size_t count, loff_t *ppos)
137 {
138 struct omap_iommu *obj = file->private_data;
139 char *p, *buf;
140 size_t bytes;
141
142 buf = (char *)__get_free_page(GFP_KERNEL);
143 if (!buf)
144 return -ENOMEM;
145 p = buf;
146
147 p += sprintf(p, "L: %8s %8s\n", "da:", "pa:");
148 p += sprintf(p, "-----------------------------------------\n");
149
150 mutex_lock(&iommu_debug_lock);
151
152 bytes = PAGE_SIZE - (p - buf);
153 p += dump_ioptable(obj, p, bytes);
154
155 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
156
157 mutex_unlock(&iommu_debug_lock);
158 free_page((unsigned long)buf);
159
160 return bytes;
161 }
162
163 #define DEBUG_FOPS_RO(name) \
164 static const struct file_operations debug_##name##_fops = { \
165 .open = simple_open, \
166 .read = debug_read_##name, \
167 .llseek = generic_file_llseek, \
168 };
169
170 DEBUG_FOPS_RO(regs);
171 DEBUG_FOPS_RO(tlb);
172 DEBUG_FOPS_RO(pagetable);
173
174 #define __DEBUG_ADD_FILE(attr, mode) \
175 { \
176 struct dentry *dent; \
177 dent = debugfs_create_file(#attr, mode, obj->debug_dir, \
178 obj, &debug_##attr##_fops); \
179 if (!dent) \
180 goto err; \
181 }
182
183 #define DEBUG_ADD_FILE_RO(name) __DEBUG_ADD_FILE(name, 0400)
184
185 void omap_iommu_debugfs_add(struct omap_iommu *obj)
186 {
187 struct dentry *d;
188
189 if (!iommu_debug_root)
190 return;
191
192 obj->debug_dir = debugfs_create_dir(obj->name, iommu_debug_root);
193 if (!obj->debug_dir)
194 return;
195
196 d = debugfs_create_u8("nr_tlb_entries", 0400, obj->debug_dir,
197 (u8 *)&obj->nr_tlb_entries);
198 if (!d)
199 return;
200
201 DEBUG_ADD_FILE_RO(regs);
202 DEBUG_ADD_FILE_RO(tlb);
203 DEBUG_ADD_FILE_RO(pagetable);
204
205 return;
206
207 err:
208 debugfs_remove_recursive(obj->debug_dir);
209 }
210
211 void omap_iommu_debugfs_remove(struct omap_iommu *obj)
212 {
213 if (!obj->debug_dir)
214 return;
215
216 debugfs_remove_recursive(obj->debug_dir);
217 }
218
219 void __init omap_iommu_debugfs_init(void)
220 {
221 iommu_debug_root = debugfs_create_dir("omap_iommu", NULL);
222 if (!iommu_debug_root)
223 pr_err("can't create debugfs dir\n");
224 }
225
226 void __exit omap_iommu_debugfs_exit(void)
227 {
228 debugfs_remove(iommu_debug_root);
229 }
This page took 0.04228 seconds and 5 git commands to generate.