blob: 288da5c1499d5432c14b2410136dfc05f82d18ff [file] [log] [blame]
Hiroshi DOYU14e0e672009-08-28 10:54:41 -07001/*
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
Ming Lei08f2e632011-11-08 18:29:15 +080013#include <linux/module.h>
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070014#include <linux/err.h>
15#include <linux/clk.h>
16#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070018#include <linux/uaccess.h>
19#include <linux/platform_device.h>
20#include <linux/debugfs.h>
21
Tony Lindgrence491cf2009-10-20 09:40:47 -070022#include <plat/iommu.h>
23#include <plat/iovmm.h>
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070024
Ohad Ben-Cohenfcf3a6e2011-08-15 23:21:41 +030025#include <plat/iopgtable.h>
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070026
27#define MAXCOLUMN 100 /* for short messages */
28
29static DEFINE_MUTEX(iommu_debug_lock);
30
31static struct dentry *iommu_debug_root;
32
33static ssize_t debug_read_ver(struct file *file, char __user *userbuf,
34 size_t count, loff_t *ppos)
35{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030036 u32 ver = omap_iommu_arch_version();
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070037 char buf[MAXCOLUMN], *p = buf;
38
39 p += sprintf(p, "H/W version: %d.%d\n", (ver >> 4) & 0xf , ver & 0xf);
40
41 return simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
42}
43
44static ssize_t debug_read_regs(struct file *file, char __user *userbuf,
45 size_t count, loff_t *ppos)
46{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030047 struct omap_iommu *obj = file->private_data;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070048 char *p, *buf;
49 ssize_t bytes;
50
51 buf = kmalloc(count, GFP_KERNEL);
52 if (!buf)
53 return -ENOMEM;
54 p = buf;
55
56 mutex_lock(&iommu_debug_lock);
57
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030058 bytes = omap_iommu_dump_ctx(obj, p, count);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070059 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, bytes);
60
61 mutex_unlock(&iommu_debug_lock);
62 kfree(buf);
63
64 return bytes;
65}
66
67static ssize_t debug_read_tlb(struct file *file, char __user *userbuf,
68 size_t count, loff_t *ppos)
69{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030070 struct omap_iommu *obj = file->private_data;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070071 char *p, *buf;
72 ssize_t bytes, rest;
73
74 buf = kmalloc(count, GFP_KERNEL);
75 if (!buf)
76 return -ENOMEM;
77 p = buf;
78
79 mutex_lock(&iommu_debug_lock);
80
81 p += sprintf(p, "%8s %8s\n", "cam:", "ram:");
82 p += sprintf(p, "-----------------------------------------\n");
83 rest = count - (p - buf);
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030084 p += omap_dump_tlb_entries(obj, p, rest);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070085
86 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
87
88 mutex_unlock(&iommu_debug_lock);
89 kfree(buf);
90
91 return bytes;
92}
93
94static ssize_t debug_write_pagetable(struct file *file,
95 const char __user *userbuf, size_t count, loff_t *ppos)
96{
97 struct iotlb_entry e;
98 struct cr_regs cr;
99 int err;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300100 struct omap_iommu *obj = file->private_data;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700101 char buf[MAXCOLUMN], *p = buf;
102
103 count = min(count, sizeof(buf));
104
105 mutex_lock(&iommu_debug_lock);
106 if (copy_from_user(p, userbuf, count)) {
107 mutex_unlock(&iommu_debug_lock);
108 return -EFAULT;
109 }
110
111 sscanf(p, "%x %x", &cr.cam, &cr.ram);
112 if (!cr.cam || !cr.ram) {
113 mutex_unlock(&iommu_debug_lock);
114 return -EINVAL;
115 }
116
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300117 omap_iotlb_cr_to_e(&cr, &e);
118 err = omap_iopgtable_store_entry(obj, &e);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700119 if (err)
120 dev_err(obj->dev, "%s: fail to store cr\n", __func__);
121
122 mutex_unlock(&iommu_debug_lock);
123 return count;
124}
125
126#define dump_ioptable_entry_one(lv, da, val) \
127 ({ \
128 int __err = 0; \
129 ssize_t bytes; \
130 const int maxcol = 22; \
131 const char *str = "%d: %08x %08x\n"; \
132 bytes = snprintf(p, maxcol, str, lv, da, val); \
133 p += bytes; \
134 len -= bytes; \
135 if (len < maxcol) \
136 __err = -ENOMEM; \
137 __err; \
138 })
139
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300140static ssize_t dump_ioptable(struct omap_iommu *obj, char *buf, ssize_t len)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700141{
142 int i;
143 u32 *iopgd;
144 char *p = buf;
145
146 spin_lock(&obj->page_table_lock);
147
148 iopgd = iopgd_offset(obj, 0);
149 for (i = 0; i < PTRS_PER_IOPGD; i++, iopgd++) {
150 int j, err;
151 u32 *iopte;
152 u32 da;
153
154 if (!*iopgd)
155 continue;
156
157 if (!(*iopgd & IOPGD_TABLE)) {
158 da = i << IOPGD_SHIFT;
159
160 err = dump_ioptable_entry_one(1, da, *iopgd);
161 if (err)
162 goto out;
163 continue;
164 }
165
166 iopte = iopte_offset(iopgd, 0);
167
168 for (j = 0; j < PTRS_PER_IOPTE; j++, iopte++) {
169 if (!*iopte)
170 continue;
171
172 da = (i << IOPGD_SHIFT) + (j << IOPTE_SHIFT);
173 err = dump_ioptable_entry_one(2, da, *iopgd);
174 if (err)
175 goto out;
176 }
177 }
178out:
179 spin_unlock(&obj->page_table_lock);
180
181 return p - buf;
182}
183
184static ssize_t debug_read_pagetable(struct file *file, char __user *userbuf,
185 size_t count, loff_t *ppos)
186{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300187 struct omap_iommu *obj = file->private_data;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700188 char *p, *buf;
189 size_t bytes;
190
191 buf = (char *)__get_free_page(GFP_KERNEL);
192 if (!buf)
193 return -ENOMEM;
194 p = buf;
195
196 p += sprintf(p, "L: %8s %8s\n", "da:", "pa:");
197 p += sprintf(p, "-----------------------------------------\n");
198
199 mutex_lock(&iommu_debug_lock);
200
201 bytes = PAGE_SIZE - (p - buf);
202 p += dump_ioptable(obj, p, bytes);
203
204 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
205
206 mutex_unlock(&iommu_debug_lock);
207 free_page((unsigned long)buf);
208
209 return bytes;
210}
211
212static ssize_t debug_read_mmap(struct file *file, char __user *userbuf,
213 size_t count, loff_t *ppos)
214{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300215 struct omap_iommu *obj = file->private_data;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700216 char *p, *buf;
217 struct iovm_struct *tmp;
218 int uninitialized_var(i);
219 ssize_t bytes;
220
221 buf = (char *)__get_free_page(GFP_KERNEL);
222 if (!buf)
223 return -ENOMEM;
224 p = buf;
225
226 p += sprintf(p, "%-3s %-8s %-8s %6s %8s\n",
227 "No", "start", "end", "size", "flags");
228 p += sprintf(p, "-------------------------------------------------\n");
229
230 mutex_lock(&iommu_debug_lock);
231
232 list_for_each_entry(tmp, &obj->mmap, list) {
233 size_t len;
234 const char *str = "%3d %08x-%08x %6x %8x\n";
235 const int maxcol = 39;
236
237 len = tmp->da_end - tmp->da_start;
238 p += snprintf(p, maxcol, str,
239 i, tmp->da_start, tmp->da_end, len, tmp->flags);
240
241 if (PAGE_SIZE - (p - buf) < maxcol)
242 break;
243 i++;
244 }
245
246 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
247
248 mutex_unlock(&iommu_debug_lock);
249 free_page((unsigned long)buf);
250
251 return bytes;
252}
253
254static ssize_t debug_read_mem(struct file *file, char __user *userbuf,
255 size_t count, loff_t *ppos)
256{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300257 struct omap_iommu *obj = file->private_data;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700258 char *p, *buf;
259 struct iovm_struct *area;
260 ssize_t bytes;
261
262 count = min_t(ssize_t, count, PAGE_SIZE);
263
264 buf = (char *)__get_free_page(GFP_KERNEL);
265 if (!buf)
266 return -ENOMEM;
267 p = buf;
268
269 mutex_lock(&iommu_debug_lock);
270
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300271 area = omap_find_iovm_area(obj, (u32)ppos);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700272 if (IS_ERR(area)) {
273 bytes = -EINVAL;
274 goto err_out;
275 }
276 memcpy(p, area->va, count);
277 p += count;
278
279 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
280err_out:
281 mutex_unlock(&iommu_debug_lock);
282 free_page((unsigned long)buf);
283
284 return bytes;
285}
286
287static ssize_t debug_write_mem(struct file *file, const char __user *userbuf,
288 size_t count, loff_t *ppos)
289{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300290 struct omap_iommu *obj = file->private_data;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700291 struct iovm_struct *area;
292 char *p, *buf;
293
294 count = min_t(size_t, count, PAGE_SIZE);
295
296 buf = (char *)__get_free_page(GFP_KERNEL);
297 if (!buf)
298 return -ENOMEM;
299 p = buf;
300
301 mutex_lock(&iommu_debug_lock);
302
303 if (copy_from_user(p, userbuf, count)) {
304 count = -EFAULT;
305 goto err_out;
306 }
307
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300308 area = omap_find_iovm_area(obj, (u32)ppos);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700309 if (IS_ERR(area)) {
310 count = -EINVAL;
311 goto err_out;
312 }
313 memcpy(area->va, p, count);
314err_out:
315 mutex_unlock(&iommu_debug_lock);
316 free_page((unsigned long)buf);
317
318 return count;
319}
320
321static int debug_open_generic(struct inode *inode, struct file *file)
322{
323 file->private_data = inode->i_private;
324 return 0;
325}
326
327#define DEBUG_FOPS(name) \
328 static const struct file_operations debug_##name##_fops = { \
329 .open = debug_open_generic, \
330 .read = debug_read_##name, \
331 .write = debug_write_##name, \
Arnd Bergmannc0b0aca2010-07-06 19:16:33 +0200332 .llseek = generic_file_llseek, \
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700333 };
334
335#define DEBUG_FOPS_RO(name) \
336 static const struct file_operations debug_##name##_fops = { \
337 .open = debug_open_generic, \
338 .read = debug_read_##name, \
Arnd Bergmannc0b0aca2010-07-06 19:16:33 +0200339 .llseek = generic_file_llseek, \
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700340 };
341
342DEBUG_FOPS_RO(ver);
343DEBUG_FOPS_RO(regs);
344DEBUG_FOPS_RO(tlb);
345DEBUG_FOPS(pagetable);
346DEBUG_FOPS_RO(mmap);
347DEBUG_FOPS(mem);
348
349#define __DEBUG_ADD_FILE(attr, mode) \
350 { \
351 struct dentry *dent; \
352 dent = debugfs_create_file(#attr, mode, parent, \
353 obj, &debug_##attr##_fops); \
354 if (!dent) \
355 return -ENOMEM; \
356 }
357
358#define DEBUG_ADD_FILE(name) __DEBUG_ADD_FILE(name, 600)
359#define DEBUG_ADD_FILE_RO(name) __DEBUG_ADD_FILE(name, 400)
360
361static int iommu_debug_register(struct device *dev, void *data)
362{
363 struct platform_device *pdev = to_platform_device(dev);
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300364 struct omap_iommu *obj = platform_get_drvdata(pdev);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700365 struct dentry *d, *parent;
366
367 if (!obj || !obj->dev)
368 return -EINVAL;
369
370 d = debugfs_create_dir(obj->name, iommu_debug_root);
371 if (!d)
372 return -ENOMEM;
373 parent = d;
374
375 d = debugfs_create_u8("nr_tlb_entries", 400, parent,
376 (u8 *)&obj->nr_tlb_entries);
377 if (!d)
378 return -ENOMEM;
379
380 DEBUG_ADD_FILE_RO(ver);
381 DEBUG_ADD_FILE_RO(regs);
382 DEBUG_ADD_FILE_RO(tlb);
383 DEBUG_ADD_FILE(pagetable);
384 DEBUG_ADD_FILE_RO(mmap);
385 DEBUG_ADD_FILE(mem);
386
387 return 0;
388}
389
390static int __init iommu_debug_init(void)
391{
392 struct dentry *d;
393 int err;
394
395 d = debugfs_create_dir("iommu", NULL);
396 if (!d)
397 return -ENOMEM;
398 iommu_debug_root = d;
399
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300400 err = omap_foreach_iommu_device(d, iommu_debug_register);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700401 if (err)
402 goto err_out;
403 return 0;
404
405err_out:
406 debugfs_remove_recursive(iommu_debug_root);
407 return err;
408}
409module_init(iommu_debug_init)
410
411static void __exit iommu_debugfs_exit(void)
412{
413 debugfs_remove_recursive(iommu_debug_root);
414}
415module_exit(iommu_debugfs_exit)
416
417MODULE_DESCRIPTION("omap iommu: debugfs interface");
418MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>");
419MODULE_LICENSE("GPL v2");