USB: usbsevseg: fix max length
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / iommu / omap-iommu-debug.c
blob288da5c1499d5432c14b2410136dfc05f82d18ff
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/module.h>
14 #include <linux/err.h>
15 #include <linux/clk.h>
16 #include <linux/io.h>
17 #include <linux/slab.h>
18 #include <linux/uaccess.h>
19 #include <linux/platform_device.h>
20 #include <linux/debugfs.h>
22 #include <plat/iommu.h>
23 #include <plat/iovmm.h>
25 #include <plat/iopgtable.h>
27 #define MAXCOLUMN 100 /* for short messages */
29 static DEFINE_MUTEX(iommu_debug_lock);
31 static struct dentry *iommu_debug_root;
33 static ssize_t debug_read_ver(struct file *file, char __user *userbuf,
34 size_t count, loff_t *ppos)
36 u32 ver = omap_iommu_arch_version();
37 char buf[MAXCOLUMN], *p = buf;
39 p += sprintf(p, "H/W version: %d.%d\n", (ver >> 4) & 0xf , ver & 0xf);
41 return simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
44 static ssize_t debug_read_regs(struct file *file, char __user *userbuf,
45 size_t count, loff_t *ppos)
47 struct omap_iommu *obj = file->private_data;
48 char *p, *buf;
49 ssize_t bytes;
51 buf = kmalloc(count, GFP_KERNEL);
52 if (!buf)
53 return -ENOMEM;
54 p = buf;
56 mutex_lock(&iommu_debug_lock);
58 bytes = omap_iommu_dump_ctx(obj, p, count);
59 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, bytes);
61 mutex_unlock(&iommu_debug_lock);
62 kfree(buf);
64 return bytes;
67 static ssize_t debug_read_tlb(struct file *file, char __user *userbuf,
68 size_t count, loff_t *ppos)
70 struct omap_iommu *obj = file->private_data;
71 char *p, *buf;
72 ssize_t bytes, rest;
74 buf = kmalloc(count, GFP_KERNEL);
75 if (!buf)
76 return -ENOMEM;
77 p = buf;
79 mutex_lock(&iommu_debug_lock);
81 p += sprintf(p, "%8s %8s\n", "cam:", "ram:");
82 p += sprintf(p, "-----------------------------------------\n");
83 rest = count - (p - buf);
84 p += omap_dump_tlb_entries(obj, p, rest);
86 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
88 mutex_unlock(&iommu_debug_lock);
89 kfree(buf);
91 return bytes;
94 static ssize_t debug_write_pagetable(struct file *file,
95 const char __user *userbuf, size_t count, loff_t *ppos)
97 struct iotlb_entry e;
98 struct cr_regs cr;
99 int err;
100 struct omap_iommu *obj = file->private_data;
101 char buf[MAXCOLUMN], *p = buf;
103 count = min(count, sizeof(buf));
105 mutex_lock(&iommu_debug_lock);
106 if (copy_from_user(p, userbuf, count)) {
107 mutex_unlock(&iommu_debug_lock);
108 return -EFAULT;
111 sscanf(p, "%x %x", &cr.cam, &cr.ram);
112 if (!cr.cam || !cr.ram) {
113 mutex_unlock(&iommu_debug_lock);
114 return -EINVAL;
117 omap_iotlb_cr_to_e(&cr, &e);
118 err = omap_iopgtable_store_entry(obj, &e);
119 if (err)
120 dev_err(obj->dev, "%s: fail to store cr\n", __func__);
122 mutex_unlock(&iommu_debug_lock);
123 return count;
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; \
140 static ssize_t dump_ioptable(struct omap_iommu *obj, char *buf, ssize_t len)
142 int i;
143 u32 *iopgd;
144 char *p = buf;
146 spin_lock(&obj->page_table_lock);
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;
154 if (!*iopgd)
155 continue;
157 if (!(*iopgd & IOPGD_TABLE)) {
158 da = i << IOPGD_SHIFT;
160 err = dump_ioptable_entry_one(1, da, *iopgd);
161 if (err)
162 goto out;
163 continue;
166 iopte = iopte_offset(iopgd, 0);
168 for (j = 0; j < PTRS_PER_IOPTE; j++, iopte++) {
169 if (!*iopte)
170 continue;
172 da = (i << IOPGD_SHIFT) + (j << IOPTE_SHIFT);
173 err = dump_ioptable_entry_one(2, da, *iopgd);
174 if (err)
175 goto out;
178 out:
179 spin_unlock(&obj->page_table_lock);
181 return p - buf;
184 static ssize_t debug_read_pagetable(struct file *file, char __user *userbuf,
185 size_t count, loff_t *ppos)
187 struct omap_iommu *obj = file->private_data;
188 char *p, *buf;
189 size_t bytes;
191 buf = (char *)__get_free_page(GFP_KERNEL);
192 if (!buf)
193 return -ENOMEM;
194 p = buf;
196 p += sprintf(p, "L: %8s %8s\n", "da:", "pa:");
197 p += sprintf(p, "-----------------------------------------\n");
199 mutex_lock(&iommu_debug_lock);
201 bytes = PAGE_SIZE - (p - buf);
202 p += dump_ioptable(obj, p, bytes);
204 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
206 mutex_unlock(&iommu_debug_lock);
207 free_page((unsigned long)buf);
209 return bytes;
212 static ssize_t debug_read_mmap(struct file *file, char __user *userbuf,
213 size_t count, loff_t *ppos)
215 struct omap_iommu *obj = file->private_data;
216 char *p, *buf;
217 struct iovm_struct *tmp;
218 int uninitialized_var(i);
219 ssize_t bytes;
221 buf = (char *)__get_free_page(GFP_KERNEL);
222 if (!buf)
223 return -ENOMEM;
224 p = buf;
226 p += sprintf(p, "%-3s %-8s %-8s %6s %8s\n",
227 "No", "start", "end", "size", "flags");
228 p += sprintf(p, "-------------------------------------------------\n");
230 mutex_lock(&iommu_debug_lock);
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;
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);
241 if (PAGE_SIZE - (p - buf) < maxcol)
242 break;
243 i++;
246 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
248 mutex_unlock(&iommu_debug_lock);
249 free_page((unsigned long)buf);
251 return bytes;
254 static ssize_t debug_read_mem(struct file *file, char __user *userbuf,
255 size_t count, loff_t *ppos)
257 struct omap_iommu *obj = file->private_data;
258 char *p, *buf;
259 struct iovm_struct *area;
260 ssize_t bytes;
262 count = min_t(ssize_t, count, PAGE_SIZE);
264 buf = (char *)__get_free_page(GFP_KERNEL);
265 if (!buf)
266 return -ENOMEM;
267 p = buf;
269 mutex_lock(&iommu_debug_lock);
271 area = omap_find_iovm_area(obj, (u32)ppos);
272 if (IS_ERR(area)) {
273 bytes = -EINVAL;
274 goto err_out;
276 memcpy(p, area->va, count);
277 p += count;
279 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
280 err_out:
281 mutex_unlock(&iommu_debug_lock);
282 free_page((unsigned long)buf);
284 return bytes;
287 static ssize_t debug_write_mem(struct file *file, const char __user *userbuf,
288 size_t count, loff_t *ppos)
290 struct omap_iommu *obj = file->private_data;
291 struct iovm_struct *area;
292 char *p, *buf;
294 count = min_t(size_t, count, PAGE_SIZE);
296 buf = (char *)__get_free_page(GFP_KERNEL);
297 if (!buf)
298 return -ENOMEM;
299 p = buf;
301 mutex_lock(&iommu_debug_lock);
303 if (copy_from_user(p, userbuf, count)) {
304 count = -EFAULT;
305 goto err_out;
308 area = omap_find_iovm_area(obj, (u32)ppos);
309 if (IS_ERR(area)) {
310 count = -EINVAL;
311 goto err_out;
313 memcpy(area->va, p, count);
314 err_out:
315 mutex_unlock(&iommu_debug_lock);
316 free_page((unsigned long)buf);
318 return count;
321 static int debug_open_generic(struct inode *inode, struct file *file)
323 file->private_data = inode->i_private;
324 return 0;
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, \
332 .llseek = generic_file_llseek, \
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, \
339 .llseek = generic_file_llseek, \
342 DEBUG_FOPS_RO(ver);
343 DEBUG_FOPS_RO(regs);
344 DEBUG_FOPS_RO(tlb);
345 DEBUG_FOPS(pagetable);
346 DEBUG_FOPS_RO(mmap);
347 DEBUG_FOPS(mem);
349 #define __DEBUG_ADD_FILE(attr, mode) \
351 struct dentry *dent; \
352 dent = debugfs_create_file(#attr, mode, parent, \
353 obj, &debug_##attr##_fops); \
354 if (!dent) \
355 return -ENOMEM; \
358 #define DEBUG_ADD_FILE(name) __DEBUG_ADD_FILE(name, 600)
359 #define DEBUG_ADD_FILE_RO(name) __DEBUG_ADD_FILE(name, 400)
361 static int iommu_debug_register(struct device *dev, void *data)
363 struct platform_device *pdev = to_platform_device(dev);
364 struct omap_iommu *obj = platform_get_drvdata(pdev);
365 struct dentry *d, *parent;
367 if (!obj || !obj->dev)
368 return -EINVAL;
370 d = debugfs_create_dir(obj->name, iommu_debug_root);
371 if (!d)
372 return -ENOMEM;
373 parent = d;
375 d = debugfs_create_u8("nr_tlb_entries", 400, parent,
376 (u8 *)&obj->nr_tlb_entries);
377 if (!d)
378 return -ENOMEM;
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);
387 return 0;
390 static int __init iommu_debug_init(void)
392 struct dentry *d;
393 int err;
395 d = debugfs_create_dir("iommu", NULL);
396 if (!d)
397 return -ENOMEM;
398 iommu_debug_root = d;
400 err = omap_foreach_iommu_device(d, iommu_debug_register);
401 if (err)
402 goto err_out;
403 return 0;
405 err_out:
406 debugfs_remove_recursive(iommu_debug_root);
407 return err;
409 module_init(iommu_debug_init)
411 static void __exit iommu_debugfs_exit(void)
413 debugfs_remove_recursive(iommu_debug_root);
415 module_exit(iommu_debugfs_exit)
417 MODULE_DESCRIPTION("omap iommu: debugfs interface");
418 MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>");
419 MODULE_LICENSE("GPL v2");