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>
17 #include <linux/slab.h>
18 #include <linux/uaccess.h>
19 #include <linux/platform_device.h>
20 #include <linux/debugfs.h>
21 #include <linux/omap-iommu.h>
23 #include <plat/iommu.h>
25 #include "omap-iopgtable.h"
26 #include "omap-iommu.h"
28 #define MAXCOLUMN 100 /* for short messages */
30 static DEFINE_MUTEX(iommu_debug_lock);
32 static struct dentry *iommu_debug_root;
34 static ssize_t debug_read_ver(struct file *file, char __user *userbuf,
35 size_t count, loff_t *ppos)
37 u32 ver = omap_iommu_arch_version();
38 char buf[MAXCOLUMN], *p = buf;
40 p += sprintf(p, "H/W version: %d.%d\n", (ver >> 4) & 0xf , ver & 0xf);
42 return simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
45 static ssize_t debug_read_regs(struct file *file, char __user *userbuf,
46 size_t count, loff_t *ppos)
48 struct device *dev = file->private_data;
49 struct omap_iommu *obj = dev_to_omap_iommu(dev);
53 buf = kmalloc(count, GFP_KERNEL);
58 mutex_lock(&iommu_debug_lock);
60 bytes = omap_iommu_dump_ctx(obj, p, count);
61 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, bytes);
63 mutex_unlock(&iommu_debug_lock);
69 static ssize_t debug_read_tlb(struct file *file, char __user *userbuf,
70 size_t count, loff_t *ppos)
72 struct device *dev = file->private_data;
73 struct omap_iommu *obj = dev_to_omap_iommu(dev);
77 buf = kmalloc(count, GFP_KERNEL);
82 mutex_lock(&iommu_debug_lock);
84 p += sprintf(p, "%8s %8s\n", "cam:", "ram:");
85 p += sprintf(p, "-----------------------------------------\n");
86 rest = count - (p - buf);
87 p += omap_dump_tlb_entries(obj, p, rest);
89 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
91 mutex_unlock(&iommu_debug_lock);
97 static ssize_t debug_write_pagetable(struct file *file,
98 const char __user *userbuf, size_t count, loff_t *ppos)
100 struct iotlb_entry e;
103 struct device *dev = file->private_data;
104 struct omap_iommu *obj = dev_to_omap_iommu(dev);
105 char buf[MAXCOLUMN], *p = buf;
107 count = min(count, sizeof(buf));
109 mutex_lock(&iommu_debug_lock);
110 if (copy_from_user(p, userbuf, count)) {
111 mutex_unlock(&iommu_debug_lock);
115 sscanf(p, "%x %x", &cr.cam, &cr.ram);
116 if (!cr.cam || !cr.ram) {
117 mutex_unlock(&iommu_debug_lock);
121 omap_iotlb_cr_to_e(&cr, &e);
122 err = omap_iopgtable_store_entry(obj, &e);
124 dev_err(obj->dev, "%s: fail to store cr\n", __func__);
126 mutex_unlock(&iommu_debug_lock);
130 #define dump_ioptable_entry_one(lv, da, val) \
134 const int maxcol = 22; \
135 const char *str = "%d: %08x %08x\n"; \
136 bytes = snprintf(p, maxcol, str, lv, da, val); \
144 static ssize_t dump_ioptable(struct omap_iommu *obj, char *buf, ssize_t len)
150 spin_lock(&obj->page_table_lock);
152 iopgd = iopgd_offset(obj, 0);
153 for (i = 0; i < PTRS_PER_IOPGD; i++, iopgd++) {
161 if (!(*iopgd & IOPGD_TABLE)) {
162 da = i << IOPGD_SHIFT;
164 err = dump_ioptable_entry_one(1, da, *iopgd);
170 iopte = iopte_offset(iopgd, 0);
172 for (j = 0; j < PTRS_PER_IOPTE; j++, iopte++) {
176 da = (i << IOPGD_SHIFT) + (j << IOPTE_SHIFT);
177 err = dump_ioptable_entry_one(2, da, *iopgd);
183 spin_unlock(&obj->page_table_lock);
188 static ssize_t debug_read_pagetable(struct file *file, char __user *userbuf,
189 size_t count, loff_t *ppos)
191 struct device *dev = file->private_data;
192 struct omap_iommu *obj = dev_to_omap_iommu(dev);
196 buf = (char *)__get_free_page(GFP_KERNEL);
201 p += sprintf(p, "L: %8s %8s\n", "da:", "pa:");
202 p += sprintf(p, "-----------------------------------------\n");
204 mutex_lock(&iommu_debug_lock);
206 bytes = PAGE_SIZE - (p - buf);
207 p += dump_ioptable(obj, p, bytes);
209 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
211 mutex_unlock(&iommu_debug_lock);
212 free_page((unsigned long)buf);
217 static ssize_t debug_read_mmap(struct file *file, char __user *userbuf,
218 size_t count, loff_t *ppos)
220 struct device *dev = file->private_data;
221 struct omap_iommu *obj = dev_to_omap_iommu(dev);
223 struct iovm_struct *tmp;
224 int uninitialized_var(i);
227 buf = (char *)__get_free_page(GFP_KERNEL);
232 p += sprintf(p, "%-3s %-8s %-8s %6s %8s\n",
233 "No", "start", "end", "size", "flags");
234 p += sprintf(p, "-------------------------------------------------\n");
236 mutex_lock(&iommu_debug_lock);
238 list_for_each_entry(tmp, &obj->mmap, list) {
240 const char *str = "%3d %08x-%08x %6x %8x\n";
241 const int maxcol = 39;
243 len = tmp->da_end - tmp->da_start;
244 p += snprintf(p, maxcol, str,
245 i, tmp->da_start, tmp->da_end, len, tmp->flags);
247 if (PAGE_SIZE - (p - buf) < maxcol)
252 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
254 mutex_unlock(&iommu_debug_lock);
255 free_page((unsigned long)buf);
260 static ssize_t debug_read_mem(struct file *file, char __user *userbuf,
261 size_t count, loff_t *ppos)
263 struct device *dev = file->private_data;
265 struct iovm_struct *area;
268 count = min_t(ssize_t, count, PAGE_SIZE);
270 buf = (char *)__get_free_page(GFP_KERNEL);
275 mutex_lock(&iommu_debug_lock);
277 area = omap_find_iovm_area(dev, (u32)ppos);
282 memcpy(p, area->va, count);
285 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
287 mutex_unlock(&iommu_debug_lock);
288 free_page((unsigned long)buf);
293 static ssize_t debug_write_mem(struct file *file, const char __user *userbuf,
294 size_t count, loff_t *ppos)
296 struct device *dev = file->private_data;
297 struct iovm_struct *area;
300 count = min_t(size_t, count, PAGE_SIZE);
302 buf = (char *)__get_free_page(GFP_KERNEL);
307 mutex_lock(&iommu_debug_lock);
309 if (copy_from_user(p, userbuf, count)) {
314 area = omap_find_iovm_area(dev, (u32)ppos);
319 memcpy(area->va, p, count);
321 mutex_unlock(&iommu_debug_lock);
322 free_page((unsigned long)buf);
327 #define DEBUG_FOPS(name) \
328 static const struct file_operations debug_##name##_fops = { \
329 .open = simple_open, \
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 = simple_open, \
338 .read = debug_read_##name, \
339 .llseek = generic_file_llseek, \
345 DEBUG_FOPS(pagetable);
349 #define __DEBUG_ADD_FILE(attr, mode) \
351 struct dentry *dent; \
352 dent = debugfs_create_file(#attr, mode, parent, \
353 dev, &debug_##attr##_fops); \
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 omap_iommu_arch_data *arch_data;
366 struct dentry *d, *parent;
368 if (!obj || !obj->dev)
371 arch_data = kzalloc(sizeof(*arch_data), GFP_KERNEL);
375 arch_data->iommu_dev = obj;
377 dev->archdata.iommu = arch_data;
379 d = debugfs_create_dir(obj->name, iommu_debug_root);
384 d = debugfs_create_u8("nr_tlb_entries", 400, parent,
385 (u8 *)&obj->nr_tlb_entries);
389 DEBUG_ADD_FILE_RO(ver);
390 DEBUG_ADD_FILE_RO(regs);
391 DEBUG_ADD_FILE_RO(tlb);
392 DEBUG_ADD_FILE(pagetable);
393 DEBUG_ADD_FILE_RO(mmap);
403 static int iommu_debug_unregister(struct device *dev, void *data)
405 if (!dev->archdata.iommu)
408 kfree(dev->archdata.iommu);
410 dev->archdata.iommu = NULL;
415 static int __init iommu_debug_init(void)
420 d = debugfs_create_dir("iommu", NULL);
423 iommu_debug_root = d;
425 err = omap_foreach_iommu_device(d, iommu_debug_register);
431 debugfs_remove_recursive(iommu_debug_root);
434 module_init(iommu_debug_init)
436 static void __exit iommu_debugfs_exit(void)
438 debugfs_remove_recursive(iommu_debug_root);
439 omap_foreach_iommu_device(NULL, iommu_debug_unregister);
441 module_exit(iommu_debugfs_exit)
443 MODULE_DESCRIPTION("omap iommu: debugfs interface");
444 MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>");
445 MODULE_LICENSE("GPL v2");