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/uaccess.h>
17 #include <linux/platform_device.h>
18 #include <linux/debugfs.h>
20 #include <mach/iommu.h>
21 #include <mach/iovmm.h>
23 #include "iopgtable.h"
25 #define MAXCOLUMN 100 /* for short messages */
27 static DEFINE_MUTEX(iommu_debug_lock
);
29 static struct dentry
*iommu_debug_root
;
31 static ssize_t
debug_read_ver(struct file
*file
, char __user
*userbuf
,
32 size_t count
, loff_t
*ppos
)
34 u32 ver
= iommu_arch_version();
35 char buf
[MAXCOLUMN
], *p
= buf
;
37 p
+= sprintf(p
, "H/W version: %d.%d\n", (ver
>> 4) & 0xf , ver
& 0xf);
39 return simple_read_from_buffer(userbuf
, count
, ppos
, buf
, p
- buf
);
42 static ssize_t
debug_read_regs(struct file
*file
, char __user
*userbuf
,
43 size_t count
, loff_t
*ppos
)
45 struct iommu
*obj
= file
->private_data
;
49 buf
= kmalloc(count
, GFP_KERNEL
);
54 mutex_lock(&iommu_debug_lock
);
56 bytes
= iommu_dump_ctx(obj
, p
, count
);
57 bytes
= simple_read_from_buffer(userbuf
, count
, ppos
, buf
, bytes
);
59 mutex_unlock(&iommu_debug_lock
);
65 static ssize_t
debug_read_tlb(struct file
*file
, char __user
*userbuf
,
66 size_t count
, loff_t
*ppos
)
68 struct iommu
*obj
= file
->private_data
;
72 buf
= kmalloc(count
, GFP_KERNEL
);
77 mutex_lock(&iommu_debug_lock
);
79 p
+= sprintf(p
, "%8s %8s\n", "cam:", "ram:");
80 p
+= sprintf(p
, "-----------------------------------------\n");
81 rest
= count
- (p
- buf
);
82 p
+= dump_tlb_entries(obj
, p
, rest
);
84 bytes
= simple_read_from_buffer(userbuf
, count
, ppos
, buf
, p
- buf
);
86 mutex_unlock(&iommu_debug_lock
);
92 static ssize_t
debug_write_pagetable(struct file
*file
,
93 const char __user
*userbuf
, size_t count
, loff_t
*ppos
)
98 struct iommu
*obj
= file
->private_data
;
99 char buf
[MAXCOLUMN
], *p
= buf
;
101 count
= min(count
, sizeof(buf
));
103 mutex_lock(&iommu_debug_lock
);
104 if (copy_from_user(p
, userbuf
, count
)) {
105 mutex_unlock(&iommu_debug_lock
);
109 sscanf(p
, "%x %x", &cr
.cam
, &cr
.ram
);
110 if (!cr
.cam
|| !cr
.ram
) {
111 mutex_unlock(&iommu_debug_lock
);
115 iotlb_cr_to_e(&cr
, &e
);
116 err
= iopgtable_store_entry(obj
, &e
);
118 dev_err(obj
->dev
, "%s: fail to store cr\n", __func__
);
120 mutex_unlock(&iommu_debug_lock
);
124 #define dump_ioptable_entry_one(lv, da, val) \
128 const int maxcol = 22; \
129 const char *str = "%d: %08x %08x\n"; \
130 bytes = snprintf(p, maxcol, str, lv, da, val); \
138 static ssize_t
dump_ioptable(struct iommu
*obj
, char *buf
, ssize_t len
)
144 spin_lock(&obj
->page_table_lock
);
146 iopgd
= iopgd_offset(obj
, 0);
147 for (i
= 0; i
< PTRS_PER_IOPGD
; i
++, iopgd
++) {
155 if (!(*iopgd
& IOPGD_TABLE
)) {
156 da
= i
<< IOPGD_SHIFT
;
158 err
= dump_ioptable_entry_one(1, da
, *iopgd
);
164 iopte
= iopte_offset(iopgd
, 0);
166 for (j
= 0; j
< PTRS_PER_IOPTE
; j
++, iopte
++) {
170 da
= (i
<< IOPGD_SHIFT
) + (j
<< IOPTE_SHIFT
);
171 err
= dump_ioptable_entry_one(2, da
, *iopgd
);
177 spin_unlock(&obj
->page_table_lock
);
182 static ssize_t
debug_read_pagetable(struct file
*file
, char __user
*userbuf
,
183 size_t count
, loff_t
*ppos
)
185 struct iommu
*obj
= file
->private_data
;
189 buf
= (char *)__get_free_page(GFP_KERNEL
);
194 p
+= sprintf(p
, "L: %8s %8s\n", "da:", "pa:");
195 p
+= sprintf(p
, "-----------------------------------------\n");
197 mutex_lock(&iommu_debug_lock
);
199 bytes
= PAGE_SIZE
- (p
- buf
);
200 p
+= dump_ioptable(obj
, p
, bytes
);
202 bytes
= simple_read_from_buffer(userbuf
, count
, ppos
, buf
, p
- buf
);
204 mutex_unlock(&iommu_debug_lock
);
205 free_page((unsigned long)buf
);
210 static ssize_t
debug_read_mmap(struct file
*file
, char __user
*userbuf
,
211 size_t count
, loff_t
*ppos
)
213 struct iommu
*obj
= file
->private_data
;
215 struct iovm_struct
*tmp
;
216 int uninitialized_var(i
);
219 buf
= (char *)__get_free_page(GFP_KERNEL
);
224 p
+= sprintf(p
, "%-3s %-8s %-8s %6s %8s\n",
225 "No", "start", "end", "size", "flags");
226 p
+= sprintf(p
, "-------------------------------------------------\n");
228 mutex_lock(&iommu_debug_lock
);
230 list_for_each_entry(tmp
, &obj
->mmap
, list
) {
232 const char *str
= "%3d %08x-%08x %6x %8x\n";
233 const int maxcol
= 39;
235 len
= tmp
->da_end
- tmp
->da_start
;
236 p
+= snprintf(p
, maxcol
, str
,
237 i
, tmp
->da_start
, tmp
->da_end
, len
, tmp
->flags
);
239 if (PAGE_SIZE
- (p
- buf
) < maxcol
)
244 bytes
= simple_read_from_buffer(userbuf
, count
, ppos
, buf
, p
- buf
);
246 mutex_unlock(&iommu_debug_lock
);
247 free_page((unsigned long)buf
);
252 static ssize_t
debug_read_mem(struct file
*file
, char __user
*userbuf
,
253 size_t count
, loff_t
*ppos
)
255 struct iommu
*obj
= file
->private_data
;
257 struct iovm_struct
*area
;
260 count
= min_t(ssize_t
, count
, PAGE_SIZE
);
262 buf
= (char *)__get_free_page(GFP_KERNEL
);
267 mutex_lock(&iommu_debug_lock
);
269 area
= find_iovm_area(obj
, (u32
)ppos
);
274 memcpy(p
, area
->va
, count
);
277 bytes
= simple_read_from_buffer(userbuf
, count
, ppos
, buf
, p
- buf
);
279 mutex_unlock(&iommu_debug_lock
);
280 free_page((unsigned long)buf
);
285 static ssize_t
debug_write_mem(struct file
*file
, const char __user
*userbuf
,
286 size_t count
, loff_t
*ppos
)
288 struct iommu
*obj
= file
->private_data
;
289 struct iovm_struct
*area
;
292 count
= min_t(size_t, count
, PAGE_SIZE
);
294 buf
= (char *)__get_free_page(GFP_KERNEL
);
299 mutex_lock(&iommu_debug_lock
);
301 if (copy_from_user(p
, userbuf
, count
)) {
306 area
= find_iovm_area(obj
, (u32
)ppos
);
311 memcpy(area
->va
, p
, count
);
313 mutex_unlock(&iommu_debug_lock
);
314 free_page((unsigned long)buf
);
319 static int debug_open_generic(struct inode
*inode
, struct file
*file
)
321 file
->private_data
= inode
->i_private
;
325 #define DEBUG_FOPS(name) \
326 static const struct file_operations debug_##name##_fops = { \
327 .open = debug_open_generic, \
328 .read = debug_read_##name, \
329 .write = debug_write_##name, \
332 #define DEBUG_FOPS_RO(name) \
333 static const struct file_operations debug_##name##_fops = { \
334 .open = debug_open_generic, \
335 .read = debug_read_##name, \
341 DEBUG_FOPS(pagetable
);
345 #define __DEBUG_ADD_FILE(attr, mode) \
347 struct dentry *dent; \
348 dent = debugfs_create_file(#attr, mode, parent, \
349 obj, &debug_##attr##_fops); \
354 #define DEBUG_ADD_FILE(name) __DEBUG_ADD_FILE(name, 600)
355 #define DEBUG_ADD_FILE_RO(name) __DEBUG_ADD_FILE(name, 400)
357 static int iommu_debug_register(struct device
*dev
, void *data
)
359 struct platform_device
*pdev
= to_platform_device(dev
);
360 struct iommu
*obj
= platform_get_drvdata(pdev
);
361 struct dentry
*d
, *parent
;
363 if (!obj
|| !obj
->dev
)
366 d
= debugfs_create_dir(obj
->name
, iommu_debug_root
);
371 d
= debugfs_create_u8("nr_tlb_entries", 400, parent
,
372 (u8
*)&obj
->nr_tlb_entries
);
376 DEBUG_ADD_FILE_RO(ver
);
377 DEBUG_ADD_FILE_RO(regs
);
378 DEBUG_ADD_FILE_RO(tlb
);
379 DEBUG_ADD_FILE(pagetable
);
380 DEBUG_ADD_FILE_RO(mmap
);
386 static int __init
iommu_debug_init(void)
391 d
= debugfs_create_dir("iommu", NULL
);
394 iommu_debug_root
= d
;
396 err
= foreach_iommu_device(d
, iommu_debug_register
);
402 debugfs_remove_recursive(iommu_debug_root
);
405 module_init(iommu_debug_init
)
407 static void __exit
iommu_debugfs_exit(void)
409 debugfs_remove_recursive(iommu_debug_root
);
411 module_exit(iommu_debugfs_exit
)
413 MODULE_DESCRIPTION("omap iommu: debugfs interface");
414 MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>");
415 MODULE_LICENSE("GPL v2");