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>
22 #include <linux/platform_data/iommu-omap.h>
24 #include "omap-iopgtable.h"
25 #include "omap-iommu.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 device *dev = file->private_data;
48 struct omap_iommu *obj = dev_to_omap_iommu(dev);
52 buf = kmalloc(count, GFP_KERNEL);
57 mutex_lock(&iommu_debug_lock);
59 bytes = omap_iommu_dump_ctx(obj, p, count);
60 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, bytes);
62 mutex_unlock(&iommu_debug_lock);
68 static ssize_t debug_read_tlb(struct file *file, char __user *userbuf,
69 size_t count, loff_t *ppos)
71 struct device *dev = file->private_data;
72 struct omap_iommu *obj = dev_to_omap_iommu(dev);
76 buf = kmalloc(count, GFP_KERNEL);
81 mutex_lock(&iommu_debug_lock);
83 p += sprintf(p, "%8s %8s\n", "cam:", "ram:");
84 p += sprintf(p, "-----------------------------------------\n");
85 rest = count - (p - buf);
86 p += omap_dump_tlb_entries(obj, p, rest);
88 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
90 mutex_unlock(&iommu_debug_lock);
96 static ssize_t debug_write_pagetable(struct file *file,
97 const char __user *userbuf, size_t count, loff_t *ppos)
102 struct device *dev = file->private_data;
103 struct omap_iommu *obj = dev_to_omap_iommu(dev);
104 char buf[MAXCOLUMN], *p = buf;
106 count = min(count, sizeof(buf));
108 mutex_lock(&iommu_debug_lock);
109 if (copy_from_user(p, userbuf, count)) {
110 mutex_unlock(&iommu_debug_lock);
114 sscanf(p, "%x %x", &cr.cam, &cr.ram);
115 if (!cr.cam || !cr.ram) {
116 mutex_unlock(&iommu_debug_lock);
120 omap_iotlb_cr_to_e(&cr, &e);
121 err = omap_iopgtable_store_entry(obj, &e);
123 dev_err(obj->dev, "%s: fail to store cr\n", __func__);
125 mutex_unlock(&iommu_debug_lock);
129 #define dump_ioptable_entry_one(lv, da, val) \
133 const int maxcol = 22; \
134 const char *str = "%d: %08x %08x\n"; \
135 bytes = snprintf(p, maxcol, str, lv, da, val); \
143 static ssize_t dump_ioptable(struct omap_iommu *obj, char *buf, ssize_t len)
149 spin_lock(&obj->page_table_lock);
151 iopgd = iopgd_offset(obj, 0);
152 for (i = 0; i < PTRS_PER_IOPGD; i++, iopgd++) {
160 if (!(*iopgd & IOPGD_TABLE)) {
161 da = i << IOPGD_SHIFT;
163 err = dump_ioptable_entry_one(1, da, *iopgd);
169 iopte = iopte_offset(iopgd, 0);
171 for (j = 0; j < PTRS_PER_IOPTE; j++, iopte++) {
175 da = (i << IOPGD_SHIFT) + (j << IOPTE_SHIFT);
176 err = dump_ioptable_entry_one(2, da, *iopgd);
182 spin_unlock(&obj->page_table_lock);
187 static ssize_t debug_read_pagetable(struct file *file, char __user *userbuf,
188 size_t count, loff_t *ppos)
190 struct device *dev = file->private_data;
191 struct omap_iommu *obj = dev_to_omap_iommu(dev);
195 buf = (char *)__get_free_page(GFP_KERNEL);
200 p += sprintf(p, "L: %8s %8s\n", "da:", "pa:");
201 p += sprintf(p, "-----------------------------------------\n");
203 mutex_lock(&iommu_debug_lock);
205 bytes = PAGE_SIZE - (p - buf);
206 p += dump_ioptable(obj, p, bytes);
208 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
210 mutex_unlock(&iommu_debug_lock);
211 free_page((unsigned long)buf);
216 static ssize_t debug_read_mmap(struct file *file, char __user *userbuf,
217 size_t count, loff_t *ppos)
219 struct device *dev = file->private_data;
220 struct omap_iommu *obj = dev_to_omap_iommu(dev);
222 struct iovm_struct *tmp;
223 int uninitialized_var(i);
226 buf = (char *)__get_free_page(GFP_KERNEL);
231 p += sprintf(p, "%-3s %-8s %-8s %6s %8s\n",
232 "No", "start", "end", "size", "flags");
233 p += sprintf(p, "-------------------------------------------------\n");
235 mutex_lock(&iommu_debug_lock);
237 list_for_each_entry(tmp, &obj->mmap, list) {
239 const char *str = "%3d %08x-%08x %6x %8x\n";
240 const int maxcol = 39;
242 len = tmp->da_end - tmp->da_start;
243 p += snprintf(p, maxcol, str,
244 i, tmp->da_start, tmp->da_end, len, tmp->flags);
246 if (PAGE_SIZE - (p - buf) < maxcol)
251 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
253 mutex_unlock(&iommu_debug_lock);
254 free_page((unsigned long)buf);
259 static ssize_t debug_read_mem(struct file *file, char __user *userbuf,
260 size_t count, loff_t *ppos)
262 struct device *dev = file->private_data;
264 struct iovm_struct *area;
267 count = min_t(ssize_t, count, PAGE_SIZE);
269 buf = (char *)__get_free_page(GFP_KERNEL);
274 mutex_lock(&iommu_debug_lock);
276 area = omap_find_iovm_area(dev, (u32)ppos);
281 memcpy(p, area->va, count);
284 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
286 mutex_unlock(&iommu_debug_lock);
287 free_page((unsigned long)buf);
292 static ssize_t debug_write_mem(struct file *file, const char __user *userbuf,
293 size_t count, loff_t *ppos)
295 struct device *dev = file->private_data;
296 struct iovm_struct *area;
299 count = min_t(size_t, count, PAGE_SIZE);
301 buf = (char *)__get_free_page(GFP_KERNEL);
306 mutex_lock(&iommu_debug_lock);
308 if (copy_from_user(p, userbuf, count)) {
313 area = omap_find_iovm_area(dev, (u32)ppos);
318 memcpy(area->va, p, count);
320 mutex_unlock(&iommu_debug_lock);
321 free_page((unsigned long)buf);
326 #define DEBUG_FOPS(name) \
327 static const struct file_operations debug_##name##_fops = { \
328 .open = simple_open, \
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 = simple_open, \
337 .read = debug_read_##name, \
338 .llseek = generic_file_llseek, \
344 DEBUG_FOPS(pagetable);
348 #define __DEBUG_ADD_FILE(attr, mode) \
350 struct dentry *dent; \
351 dent = debugfs_create_file(#attr, mode, parent, \
352 dev, &debug_##attr##_fops); \
357 #define DEBUG_ADD_FILE(name) __DEBUG_ADD_FILE(name, 0600)
358 #define DEBUG_ADD_FILE_RO(name) __DEBUG_ADD_FILE(name, 0400)
360 static int iommu_debug_register(struct device *dev, void *data)
362 struct platform_device *pdev = to_platform_device(dev);
363 struct omap_iommu *obj = platform_get_drvdata(pdev);
364 struct omap_iommu_arch_data *arch_data;
365 struct dentry *d, *parent;
367 if (!obj || !obj->dev)
370 arch_data = kzalloc(sizeof(*arch_data), GFP_KERNEL);
374 arch_data->iommu_dev = obj;
376 dev->archdata.iommu = arch_data;
378 d = debugfs_create_dir(obj->name, iommu_debug_root);
383 d = debugfs_create_u8("nr_tlb_entries", 400, parent,
384 (u8 *)&obj->nr_tlb_entries);
388 DEBUG_ADD_FILE_RO(ver);
389 DEBUG_ADD_FILE_RO(regs);
390 DEBUG_ADD_FILE_RO(tlb);
391 DEBUG_ADD_FILE(pagetable);
392 DEBUG_ADD_FILE_RO(mmap);
402 static int iommu_debug_unregister(struct device *dev, void *data)
404 if (!dev->archdata.iommu)
407 kfree(dev->archdata.iommu);
409 dev->archdata.iommu = NULL;
414 static int __init iommu_debug_init(void)
419 d = debugfs_create_dir("iommu", NULL);
422 iommu_debug_root = d;
424 err = omap_foreach_iommu_device(d, iommu_debug_register);
430 debugfs_remove_recursive(iommu_debug_root);
433 module_init(iommu_debug_init)
435 static void __exit iommu_debugfs_exit(void)
437 debugfs_remove_recursive(iommu_debug_root);
438 omap_foreach_iommu_device(NULL, iommu_debug_unregister);
440 module_exit(iommu_debugfs_exit)
442 MODULE_DESCRIPTION("omap iommu: debugfs interface");
443 MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>");
444 MODULE_LICENSE("GPL v2");