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>
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 <plat/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
= omap_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 omap_iommu
*obj
= file
->private_data
;
50 buf
= kmalloc(count
, GFP_KERNEL
);
55 mutex_lock(&iommu_debug_lock
);
57 bytes
= omap_iommu_dump_ctx(obj
, p
, count
);
58 bytes
= simple_read_from_buffer(userbuf
, count
, ppos
, buf
, bytes
);
60 mutex_unlock(&iommu_debug_lock
);
66 static ssize_t
debug_read_tlb(struct file
*file
, char __user
*userbuf
,
67 size_t count
, loff_t
*ppos
)
69 struct omap_iommu
*obj
= file
->private_data
;
73 buf
= kmalloc(count
, GFP_KERNEL
);
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
+= omap_dump_tlb_entries(obj
, p
, rest
);
85 bytes
= simple_read_from_buffer(userbuf
, count
, ppos
, buf
, p
- buf
);
87 mutex_unlock(&iommu_debug_lock
);
93 static ssize_t
debug_write_pagetable(struct file
*file
,
94 const char __user
*userbuf
, size_t count
, loff_t
*ppos
)
99 struct omap_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
);
110 sscanf(p
, "%x %x", &cr
.cam
, &cr
.ram
);
111 if (!cr
.cam
|| !cr
.ram
) {
112 mutex_unlock(&iommu_debug_lock
);
116 omap_iotlb_cr_to_e(&cr
, &e
);
117 err
= omap_iopgtable_store_entry(obj
, &e
);
119 dev_err(obj
->dev
, "%s: fail to store cr\n", __func__
);
121 mutex_unlock(&iommu_debug_lock
);
125 #define dump_ioptable_entry_one(lv, da, val) \
129 const int maxcol = 22; \
130 const char *str = "%d: %08x %08x\n"; \
131 bytes = snprintf(p, maxcol, str, lv, da, val); \
139 static ssize_t
dump_ioptable(struct omap_iommu
*obj
, char *buf
, ssize_t len
)
145 spin_lock(&obj
->page_table_lock
);
147 iopgd
= iopgd_offset(obj
, 0);
148 for (i
= 0; i
< PTRS_PER_IOPGD
; i
++, iopgd
++) {
156 if (!(*iopgd
& IOPGD_TABLE
)) {
157 da
= i
<< IOPGD_SHIFT
;
159 err
= dump_ioptable_entry_one(1, da
, *iopgd
);
165 iopte
= iopte_offset(iopgd
, 0);
167 for (j
= 0; j
< PTRS_PER_IOPTE
; j
++, iopte
++) {
171 da
= (i
<< IOPGD_SHIFT
) + (j
<< IOPTE_SHIFT
);
172 err
= dump_ioptable_entry_one(2, da
, *iopgd
);
178 spin_unlock(&obj
->page_table_lock
);
183 static ssize_t
debug_read_pagetable(struct file
*file
, char __user
*userbuf
,
184 size_t count
, loff_t
*ppos
)
186 struct omap_iommu
*obj
= file
->private_data
;
190 buf
= (char *)__get_free_page(GFP_KERNEL
);
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
);
211 static ssize_t
debug_read_mmap(struct file
*file
, char __user
*userbuf
,
212 size_t count
, loff_t
*ppos
)
214 struct omap_iommu
*obj
= file
->private_data
;
216 struct iovm_struct
*tmp
;
217 int uninitialized_var(i
);
220 buf
= (char *)__get_free_page(GFP_KERNEL
);
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
) {
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
)
245 bytes
= simple_read_from_buffer(userbuf
, count
, ppos
, buf
, p
- buf
);
247 mutex_unlock(&iommu_debug_lock
);
248 free_page((unsigned long)buf
);
253 static ssize_t
debug_read_mem(struct file
*file
, char __user
*userbuf
,
254 size_t count
, loff_t
*ppos
)
256 struct omap_iommu
*obj
= file
->private_data
;
258 struct iovm_struct
*area
;
261 count
= min_t(ssize_t
, count
, PAGE_SIZE
);
263 buf
= (char *)__get_free_page(GFP_KERNEL
);
268 mutex_lock(&iommu_debug_lock
);
270 area
= omap_find_iovm_area(obj
, (u32
)ppos
);
275 memcpy(p
, area
->va
, count
);
278 bytes
= simple_read_from_buffer(userbuf
, count
, ppos
, buf
, p
- buf
);
280 mutex_unlock(&iommu_debug_lock
);
281 free_page((unsigned long)buf
);
286 static ssize_t
debug_write_mem(struct file
*file
, const char __user
*userbuf
,
287 size_t count
, loff_t
*ppos
)
289 struct omap_iommu
*obj
= file
->private_data
;
290 struct iovm_struct
*area
;
293 count
= min_t(size_t, count
, PAGE_SIZE
);
295 buf
= (char *)__get_free_page(GFP_KERNEL
);
300 mutex_lock(&iommu_debug_lock
);
302 if (copy_from_user(p
, userbuf
, count
)) {
307 area
= omap_find_iovm_area(obj
, (u32
)ppos
);
312 memcpy(area
->va
, p
, count
);
314 mutex_unlock(&iommu_debug_lock
);
315 free_page((unsigned long)buf
);
320 static int debug_open_generic(struct inode
*inode
, struct file
*file
)
322 file
->private_data
= inode
->i_private
;
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, \
344 DEBUG_FOPS(pagetable
);
348 #define __DEBUG_ADD_FILE(attr, mode) \
350 struct dentry *dent; \
351 dent = debugfs_create_file(#attr, mode, parent, \
352 obj, &debug_##attr##_fops); \
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 omap_iommu
*obj
= platform_get_drvdata(pdev
);
364 struct dentry
*d
, *parent
;
366 if (!obj
|| !obj
->dev
)
369 d
= debugfs_create_dir(obj
->name
, iommu_debug_root
);
374 d
= debugfs_create_u8("nr_tlb_entries", 400, parent
,
375 (u8
*)&obj
->nr_tlb_entries
);
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
);
389 static int __init
iommu_debug_init(void)
394 d
= debugfs_create_dir("iommu", NULL
);
397 iommu_debug_root
= d
;
399 err
= omap_foreach_iommu_device(d
, iommu_debug_register
);
405 debugfs_remove_recursive(iommu_debug_root
);
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");