Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-btrfs-devel.git] / arch / arm / plat-omap / iommu-debug.c
blobf07cf2f08e09479045d3b7e3c8c6b1c0458c59f9
1 /*
2 * omap iommu: debugfs interface
4 * Copyright (C) 2008-2009 Nokia Corporation
6 * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
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.
13 #include <linux/err.h>
14 #include <linux/clk.h>
15 #include <linux/io.h>
16 #include <linux/slab.h>
17 #include <linux/uaccess.h>
18 #include <linux/platform_device.h>
19 #include <linux/debugfs.h>
21 #include <plat/iommu.h>
22 #include <plat/iovmm.h>
24 #include "iopgtable.h"
26 #define MAXCOLUMN 100 /* for short messages */
28 static DEFINE_MUTEX(iommu_debug_lock);
30 static struct dentry *iommu_debug_root;
32 static ssize_t debug_read_ver(struct file *file, char __user *userbuf,
33 size_t count, loff_t *ppos)
35 u32 ver = iommu_arch_version();
36 char buf[MAXCOLUMN], *p = buf;
38 p += sprintf(p, "H/W version: %d.%d\n", (ver >> 4) & 0xf , ver & 0xf);
40 return simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
43 static ssize_t debug_read_regs(struct file *file, char __user *userbuf,
44 size_t count, loff_t *ppos)
46 struct iommu *obj = file->private_data;
47 char *p, *buf;
48 ssize_t bytes;
50 buf = kmalloc(count, GFP_KERNEL);
51 if (!buf)
52 return -ENOMEM;
53 p = buf;
55 mutex_lock(&iommu_debug_lock);
57 bytes = iommu_dump_ctx(obj, p, count);
58 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, bytes);
60 mutex_unlock(&iommu_debug_lock);
61 kfree(buf);
63 return bytes;
66 static ssize_t debug_read_tlb(struct file *file, char __user *userbuf,
67 size_t count, loff_t *ppos)
69 struct iommu *obj = file->private_data;
70 char *p, *buf;
71 ssize_t bytes, rest;
73 buf = kmalloc(count, GFP_KERNEL);
74 if (!buf)
75 return -ENOMEM;
76 p = buf;
78 mutex_lock(&iommu_debug_lock);
80 p += sprintf(p, "%8s %8s\n", "cam:", "ram:");
81 p += sprintf(p, "-----------------------------------------\n");
82 rest = count - (p - buf);
83 p += dump_tlb_entries(obj, p, rest);
85 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
87 mutex_unlock(&iommu_debug_lock);
88 kfree(buf);
90 return bytes;
93 static ssize_t debug_write_pagetable(struct file *file,
94 const char __user *userbuf, size_t count, loff_t *ppos)
96 struct iotlb_entry e;
97 struct cr_regs cr;
98 int err;
99 struct iommu *obj = file->private_data;
100 char buf[MAXCOLUMN], *p = buf;
102 count = min(count, sizeof(buf));
104 mutex_lock(&iommu_debug_lock);
105 if (copy_from_user(p, userbuf, count)) {
106 mutex_unlock(&iommu_debug_lock);
107 return -EFAULT;
110 sscanf(p, "%x %x", &cr.cam, &cr.ram);
111 if (!cr.cam || !cr.ram) {
112 mutex_unlock(&iommu_debug_lock);
113 return -EINVAL;
116 iotlb_cr_to_e(&cr, &e);
117 err = iopgtable_store_entry(obj, &e);
118 if (err)
119 dev_err(obj->dev, "%s: fail to store cr\n", __func__);
121 mutex_unlock(&iommu_debug_lock);
122 return count;
125 #define dump_ioptable_entry_one(lv, da, val) \
126 ({ \
127 int __err = 0; \
128 ssize_t bytes; \
129 const int maxcol = 22; \
130 const char *str = "%d: %08x %08x\n"; \
131 bytes = snprintf(p, maxcol, str, lv, da, val); \
132 p += bytes; \
133 len -= bytes; \
134 if (len < maxcol) \
135 __err = -ENOMEM; \
136 __err; \
139 static ssize_t dump_ioptable(struct iommu *obj, char *buf, ssize_t len)
141 int i;
142 u32 *iopgd;
143 char *p = buf;
145 spin_lock(&obj->page_table_lock);
147 iopgd = iopgd_offset(obj, 0);
148 for (i = 0; i < PTRS_PER_IOPGD; i++, iopgd++) {
149 int j, err;
150 u32 *iopte;
151 u32 da;
153 if (!*iopgd)
154 continue;
156 if (!(*iopgd & IOPGD_TABLE)) {
157 da = i << IOPGD_SHIFT;
159 err = dump_ioptable_entry_one(1, da, *iopgd);
160 if (err)
161 goto out;
162 continue;
165 iopte = iopte_offset(iopgd, 0);
167 for (j = 0; j < PTRS_PER_IOPTE; j++, iopte++) {
168 if (!*iopte)
169 continue;
171 da = (i << IOPGD_SHIFT) + (j << IOPTE_SHIFT);
172 err = dump_ioptable_entry_one(2, da, *iopgd);
173 if (err)
174 goto out;
177 out:
178 spin_unlock(&obj->page_table_lock);
180 return p - buf;
183 static ssize_t debug_read_pagetable(struct file *file, char __user *userbuf,
184 size_t count, loff_t *ppos)
186 struct iommu *obj = file->private_data;
187 char *p, *buf;
188 size_t bytes;
190 buf = (char *)__get_free_page(GFP_KERNEL);
191 if (!buf)
192 return -ENOMEM;
193 p = buf;
195 p += sprintf(p, "L: %8s %8s\n", "da:", "pa:");
196 p += sprintf(p, "-----------------------------------------\n");
198 mutex_lock(&iommu_debug_lock);
200 bytes = PAGE_SIZE - (p - buf);
201 p += dump_ioptable(obj, p, bytes);
203 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
205 mutex_unlock(&iommu_debug_lock);
206 free_page((unsigned long)buf);
208 return bytes;
211 static ssize_t debug_read_mmap(struct file *file, char __user *userbuf,
212 size_t count, loff_t *ppos)
214 struct iommu *obj = file->private_data;
215 char *p, *buf;
216 struct iovm_struct *tmp;
217 int uninitialized_var(i);
218 ssize_t bytes;
220 buf = (char *)__get_free_page(GFP_KERNEL);
221 if (!buf)
222 return -ENOMEM;
223 p = buf;
225 p += sprintf(p, "%-3s %-8s %-8s %6s %8s\n",
226 "No", "start", "end", "size", "flags");
227 p += sprintf(p, "-------------------------------------------------\n");
229 mutex_lock(&iommu_debug_lock);
231 list_for_each_entry(tmp, &obj->mmap, list) {
232 size_t len;
233 const char *str = "%3d %08x-%08x %6x %8x\n";
234 const int maxcol = 39;
236 len = tmp->da_end - tmp->da_start;
237 p += snprintf(p, maxcol, str,
238 i, tmp->da_start, tmp->da_end, len, tmp->flags);
240 if (PAGE_SIZE - (p - buf) < maxcol)
241 break;
242 i++;
245 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
247 mutex_unlock(&iommu_debug_lock);
248 free_page((unsigned long)buf);
250 return bytes;
253 static ssize_t debug_read_mem(struct file *file, char __user *userbuf,
254 size_t count, loff_t *ppos)
256 struct iommu *obj = file->private_data;
257 char *p, *buf;
258 struct iovm_struct *area;
259 ssize_t bytes;
261 count = min_t(ssize_t, count, PAGE_SIZE);
263 buf = (char *)__get_free_page(GFP_KERNEL);
264 if (!buf)
265 return -ENOMEM;
266 p = buf;
268 mutex_lock(&iommu_debug_lock);
270 area = find_iovm_area(obj, (u32)ppos);
271 if (IS_ERR(area)) {
272 bytes = -EINVAL;
273 goto err_out;
275 memcpy(p, area->va, count);
276 p += count;
278 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
279 err_out:
280 mutex_unlock(&iommu_debug_lock);
281 free_page((unsigned long)buf);
283 return bytes;
286 static ssize_t debug_write_mem(struct file *file, const char __user *userbuf,
287 size_t count, loff_t *ppos)
289 struct iommu *obj = file->private_data;
290 struct iovm_struct *area;
291 char *p, *buf;
293 count = min_t(size_t, count, PAGE_SIZE);
295 buf = (char *)__get_free_page(GFP_KERNEL);
296 if (!buf)
297 return -ENOMEM;
298 p = buf;
300 mutex_lock(&iommu_debug_lock);
302 if (copy_from_user(p, userbuf, count)) {
303 count = -EFAULT;
304 goto err_out;
307 area = find_iovm_area(obj, (u32)ppos);
308 if (IS_ERR(area)) {
309 count = -EINVAL;
310 goto err_out;
312 memcpy(area->va, p, count);
313 err_out:
314 mutex_unlock(&iommu_debug_lock);
315 free_page((unsigned long)buf);
317 return count;
320 static int debug_open_generic(struct inode *inode, struct file *file)
322 file->private_data = inode->i_private;
323 return 0;
326 #define DEBUG_FOPS(name) \
327 static const struct file_operations debug_##name##_fops = { \
328 .open = debug_open_generic, \
329 .read = debug_read_##name, \
330 .write = debug_write_##name, \
331 .llseek = generic_file_llseek, \
334 #define DEBUG_FOPS_RO(name) \
335 static const struct file_operations debug_##name##_fops = { \
336 .open = debug_open_generic, \
337 .read = debug_read_##name, \
338 .llseek = generic_file_llseek, \
341 DEBUG_FOPS_RO(ver);
342 DEBUG_FOPS_RO(regs);
343 DEBUG_FOPS_RO(tlb);
344 DEBUG_FOPS(pagetable);
345 DEBUG_FOPS_RO(mmap);
346 DEBUG_FOPS(mem);
348 #define __DEBUG_ADD_FILE(attr, mode) \
350 struct dentry *dent; \
351 dent = debugfs_create_file(#attr, mode, parent, \
352 obj, &debug_##attr##_fops); \
353 if (!dent) \
354 return -ENOMEM; \
357 #define DEBUG_ADD_FILE(name) __DEBUG_ADD_FILE(name, 600)
358 #define DEBUG_ADD_FILE_RO(name) __DEBUG_ADD_FILE(name, 400)
360 static int iommu_debug_register(struct device *dev, void *data)
362 struct platform_device *pdev = to_platform_device(dev);
363 struct iommu *obj = platform_get_drvdata(pdev);
364 struct dentry *d, *parent;
366 if (!obj || !obj->dev)
367 return -EINVAL;
369 d = debugfs_create_dir(obj->name, iommu_debug_root);
370 if (!d)
371 return -ENOMEM;
372 parent = d;
374 d = debugfs_create_u8("nr_tlb_entries", 400, parent,
375 (u8 *)&obj->nr_tlb_entries);
376 if (!d)
377 return -ENOMEM;
379 DEBUG_ADD_FILE_RO(ver);
380 DEBUG_ADD_FILE_RO(regs);
381 DEBUG_ADD_FILE_RO(tlb);
382 DEBUG_ADD_FILE(pagetable);
383 DEBUG_ADD_FILE_RO(mmap);
384 DEBUG_ADD_FILE(mem);
386 return 0;
389 static int __init iommu_debug_init(void)
391 struct dentry *d;
392 int err;
394 d = debugfs_create_dir("iommu", NULL);
395 if (!d)
396 return -ENOMEM;
397 iommu_debug_root = d;
399 err = foreach_iommu_device(d, iommu_debug_register);
400 if (err)
401 goto err_out;
402 return 0;
404 err_out:
405 debugfs_remove_recursive(iommu_debug_root);
406 return err;
408 module_init(iommu_debug_init)
410 static void __exit iommu_debugfs_exit(void)
412 debugfs_remove_recursive(iommu_debug_root);
414 module_exit(iommu_debugfs_exit)
416 MODULE_DESCRIPTION("omap iommu: debugfs interface");
417 MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>");
418 MODULE_LICENSE("GPL v2");