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>
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
;
51 buf
= kmalloc(count
, GFP_KERNEL
);
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
);
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
;
74 buf
= kmalloc(count
, GFP_KERNEL
);
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
);
94 static ssize_t
debug_write_pagetable(struct file
*file
,
95 const char __user
*userbuf
, size_t count
, loff_t
*ppos
)
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
);
111 sscanf(p
, "%x %x", &cr
.cam
, &cr
.ram
);
112 if (!cr
.cam
|| !cr
.ram
) {
113 mutex_unlock(&iommu_debug_lock
);
117 omap_iotlb_cr_to_e(&cr
, &e
);
118 err
= omap_iopgtable_store_entry(obj
, &e
);
120 dev_err(obj
->dev
, "%s: fail to store cr\n", __func__
);
122 mutex_unlock(&iommu_debug_lock
);
126 #define dump_ioptable_entry_one(lv, da, val) \
130 const int maxcol = 22; \
131 const char *str = "%d: %08x %08x\n"; \
132 bytes = snprintf(p, maxcol, str, lv, da, val); \
140 static ssize_t
dump_ioptable(struct omap_iommu
*obj
, char *buf
, ssize_t len
)
146 spin_lock(&obj
->page_table_lock
);
148 iopgd
= iopgd_offset(obj
, 0);
149 for (i
= 0; i
< PTRS_PER_IOPGD
; i
++, iopgd
++) {
157 if (!(*iopgd
& IOPGD_TABLE
)) {
158 da
= i
<< IOPGD_SHIFT
;
160 err
= dump_ioptable_entry_one(1, da
, *iopgd
);
166 iopte
= iopte_offset(iopgd
, 0);
168 for (j
= 0; j
< PTRS_PER_IOPTE
; j
++, iopte
++) {
172 da
= (i
<< IOPGD_SHIFT
) + (j
<< IOPTE_SHIFT
);
173 err
= dump_ioptable_entry_one(2, da
, *iopgd
);
179 spin_unlock(&obj
->page_table_lock
);
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
;
191 buf
= (char *)__get_free_page(GFP_KERNEL
);
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
);
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
;
217 struct iovm_struct
*tmp
;
218 int uninitialized_var(i
);
221 buf
= (char *)__get_free_page(GFP_KERNEL
);
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
) {
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
)
246 bytes
= simple_read_from_buffer(userbuf
, count
, ppos
, buf
, p
- buf
);
248 mutex_unlock(&iommu_debug_lock
);
249 free_page((unsigned long)buf
);
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
;
259 struct iovm_struct
*area
;
262 count
= min_t(ssize_t
, count
, PAGE_SIZE
);
264 buf
= (char *)__get_free_page(GFP_KERNEL
);
269 mutex_lock(&iommu_debug_lock
);
271 area
= omap_find_iovm_area(obj
, (u32
)ppos
);
276 memcpy(p
, area
->va
, count
);
279 bytes
= simple_read_from_buffer(userbuf
, count
, ppos
, buf
, p
- buf
);
281 mutex_unlock(&iommu_debug_lock
);
282 free_page((unsigned long)buf
);
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
;
294 count
= min_t(size_t, count
, PAGE_SIZE
);
296 buf
= (char *)__get_free_page(GFP_KERNEL
);
301 mutex_lock(&iommu_debug_lock
);
303 if (copy_from_user(p
, userbuf
, count
)) {
308 area
= omap_find_iovm_area(obj
, (u32
)ppos
);
313 memcpy(area
->va
, p
, count
);
315 mutex_unlock(&iommu_debug_lock
);
316 free_page((unsigned long)buf
);
321 static int debug_open_generic(struct inode
*inode
, struct file
*file
)
323 file
->private_data
= inode
->i_private
;
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, \
345 DEBUG_FOPS(pagetable
);
349 #define __DEBUG_ADD_FILE(attr, mode) \
351 struct dentry *dent; \
352 dent = debugfs_create_file(#attr, mode, parent, \
353 obj, &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 dentry
*d
, *parent
;
367 if (!obj
|| !obj
->dev
)
370 d
= debugfs_create_dir(obj
->name
, iommu_debug_root
);
375 d
= debugfs_create_u8("nr_tlb_entries", 400, parent
,
376 (u8
*)&obj
->nr_tlb_entries
);
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
);
390 static int __init
iommu_debug_init(void)
395 d
= debugfs_create_dir("iommu", NULL
);
398 iommu_debug_root
= d
;
400 err
= omap_foreach_iommu_device(d
, iommu_debug_register
);
406 debugfs_remove_recursive(iommu_debug_root
);
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");