Commit | Line | Data |
---|---|---|
14e0e679 HD |
1 | /* |
2 | * omap iommu: debugfs interface | |
3 | * | |
4 | * Copyright (C) 2008-2009 Nokia Corporation | |
5 | * | |
6 | * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com> | |
7 | * | |
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. | |
11 | */ | |
12 | ||
13 | #include <linux/err.h> | |
14e0e679 | 14 | #include <linux/io.h> |
5a0e3ad6 | 15 | #include <linux/slab.h> |
14e0e679 | 16 | #include <linux/uaccess.h> |
14e0e679 | 17 | #include <linux/debugfs.h> |
2ab7c848 | 18 | #include <linux/platform_data/iommu-omap.h> |
14e0e679 | 19 | |
2f7702af | 20 | #include "omap-iopgtable.h" |
ed1c7de2 | 21 | #include "omap-iommu.h" |
14e0e679 | 22 | |
14e0e679 HD |
23 | static DEFINE_MUTEX(iommu_debug_lock); |
24 | ||
25 | static struct dentry *iommu_debug_root; | |
26 | ||
14e0e679 HD |
27 | static ssize_t debug_read_regs(struct file *file, char __user *userbuf, |
28 | size_t count, loff_t *ppos) | |
29 | { | |
61c75352 | 30 | struct omap_iommu *obj = file->private_data; |
14e0e679 HD |
31 | char *p, *buf; |
32 | ssize_t bytes; | |
33 | ||
34 | buf = kmalloc(count, GFP_KERNEL); | |
35 | if (!buf) | |
36 | return -ENOMEM; | |
37 | p = buf; | |
38 | ||
39 | mutex_lock(&iommu_debug_lock); | |
40 | ||
6c32df43 | 41 | bytes = omap_iommu_dump_ctx(obj, p, count); |
14e0e679 HD |
42 | bytes = simple_read_from_buffer(userbuf, count, ppos, buf, bytes); |
43 | ||
44 | mutex_unlock(&iommu_debug_lock); | |
45 | kfree(buf); | |
46 | ||
47 | return bytes; | |
48 | } | |
49 | ||
50 | static ssize_t debug_read_tlb(struct file *file, char __user *userbuf, | |
51 | size_t count, loff_t *ppos) | |
52 | { | |
61c75352 | 53 | struct omap_iommu *obj = file->private_data; |
14e0e679 HD |
54 | char *p, *buf; |
55 | ssize_t bytes, rest; | |
56 | ||
57 | buf = kmalloc(count, GFP_KERNEL); | |
58 | if (!buf) | |
59 | return -ENOMEM; | |
60 | p = buf; | |
61 | ||
62 | mutex_lock(&iommu_debug_lock); | |
63 | ||
64 | p += sprintf(p, "%8s %8s\n", "cam:", "ram:"); | |
65 | p += sprintf(p, "-----------------------------------------\n"); | |
66 | rest = count - (p - buf); | |
6c32df43 | 67 | p += omap_dump_tlb_entries(obj, p, rest); |
14e0e679 HD |
68 | |
69 | bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf); | |
70 | ||
71 | mutex_unlock(&iommu_debug_lock); | |
72 | kfree(buf); | |
73 | ||
74 | return bytes; | |
75 | } | |
76 | ||
14e0e679 HD |
77 | #define dump_ioptable_entry_one(lv, da, val) \ |
78 | ({ \ | |
79 | int __err = 0; \ | |
80 | ssize_t bytes; \ | |
81 | const int maxcol = 22; \ | |
82 | const char *str = "%d: %08x %08x\n"; \ | |
83 | bytes = snprintf(p, maxcol, str, lv, da, val); \ | |
84 | p += bytes; \ | |
85 | len -= bytes; \ | |
86 | if (len < maxcol) \ | |
87 | __err = -ENOMEM; \ | |
88 | __err; \ | |
89 | }) | |
90 | ||
6c32df43 | 91 | static ssize_t dump_ioptable(struct omap_iommu *obj, char *buf, ssize_t len) |
14e0e679 HD |
92 | { |
93 | int i; | |
94 | u32 *iopgd; | |
95 | char *p = buf; | |
96 | ||
97 | spin_lock(&obj->page_table_lock); | |
98 | ||
99 | iopgd = iopgd_offset(obj, 0); | |
100 | for (i = 0; i < PTRS_PER_IOPGD; i++, iopgd++) { | |
101 | int j, err; | |
102 | u32 *iopte; | |
103 | u32 da; | |
104 | ||
105 | if (!*iopgd) | |
106 | continue; | |
107 | ||
108 | if (!(*iopgd & IOPGD_TABLE)) { | |
109 | da = i << IOPGD_SHIFT; | |
110 | ||
111 | err = dump_ioptable_entry_one(1, da, *iopgd); | |
112 | if (err) | |
113 | goto out; | |
114 | continue; | |
115 | } | |
116 | ||
117 | iopte = iopte_offset(iopgd, 0); | |
118 | ||
119 | for (j = 0; j < PTRS_PER_IOPTE; j++, iopte++) { | |
120 | if (!*iopte) | |
121 | continue; | |
122 | ||
123 | da = (i << IOPGD_SHIFT) + (j << IOPTE_SHIFT); | |
124 | err = dump_ioptable_entry_one(2, da, *iopgd); | |
125 | if (err) | |
126 | goto out; | |
127 | } | |
128 | } | |
129 | out: | |
130 | spin_unlock(&obj->page_table_lock); | |
131 | ||
132 | return p - buf; | |
133 | } | |
134 | ||
135 | static ssize_t debug_read_pagetable(struct file *file, char __user *userbuf, | |
136 | size_t count, loff_t *ppos) | |
137 | { | |
61c75352 | 138 | struct omap_iommu *obj = file->private_data; |
14e0e679 HD |
139 | char *p, *buf; |
140 | size_t bytes; | |
141 | ||
142 | buf = (char *)__get_free_page(GFP_KERNEL); | |
143 | if (!buf) | |
144 | return -ENOMEM; | |
145 | p = buf; | |
146 | ||
147 | p += sprintf(p, "L: %8s %8s\n", "da:", "pa:"); | |
148 | p += sprintf(p, "-----------------------------------------\n"); | |
149 | ||
150 | mutex_lock(&iommu_debug_lock); | |
151 | ||
152 | bytes = PAGE_SIZE - (p - buf); | |
153 | p += dump_ioptable(obj, p, bytes); | |
154 | ||
155 | bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf); | |
156 | ||
157 | mutex_unlock(&iommu_debug_lock); | |
158 | free_page((unsigned long)buf); | |
159 | ||
160 | return bytes; | |
161 | } | |
162 | ||
14e0e679 HD |
163 | #define DEBUG_FOPS_RO(name) \ |
164 | static const struct file_operations debug_##name##_fops = { \ | |
234e3405 | 165 | .open = simple_open, \ |
14e0e679 | 166 | .read = debug_read_##name, \ |
c0b0aca0 | 167 | .llseek = generic_file_llseek, \ |
14e0e679 HD |
168 | }; |
169 | ||
14e0e679 HD |
170 | DEBUG_FOPS_RO(regs); |
171 | DEBUG_FOPS_RO(tlb); | |
3ca5db07 | 172 | DEBUG_FOPS_RO(pagetable); |
14e0e679 HD |
173 | |
174 | #define __DEBUG_ADD_FILE(attr, mode) \ | |
175 | { \ | |
176 | struct dentry *dent; \ | |
61c75352 SA |
177 | dent = debugfs_create_file(#attr, mode, obj->debug_dir, \ |
178 | obj, &debug_##attr##_fops); \ | |
14e0e679 | 179 | if (!dent) \ |
61c75352 | 180 | goto err; \ |
14e0e679 HD |
181 | } |
182 | ||
ff3a2b73 | 183 | #define DEBUG_ADD_FILE_RO(name) __DEBUG_ADD_FILE(name, 0400) |
14e0e679 | 184 | |
61c75352 | 185 | void omap_iommu_debugfs_add(struct omap_iommu *obj) |
14e0e679 | 186 | { |
61c75352 | 187 | struct dentry *d; |
46451d62 | 188 | |
61c75352 SA |
189 | if (!iommu_debug_root) |
190 | return; | |
46451d62 | 191 | |
61c75352 SA |
192 | obj->debug_dir = debugfs_create_dir(obj->name, iommu_debug_root); |
193 | if (!obj->debug_dir) | |
194 | return; | |
14e0e679 | 195 | |
61c75352 | 196 | d = debugfs_create_u8("nr_tlb_entries", 0400, obj->debug_dir, |
14e0e679 HD |
197 | (u8 *)&obj->nr_tlb_entries); |
198 | if (!d) | |
61c75352 | 199 | return; |
14e0e679 | 200 | |
14e0e679 HD |
201 | DEBUG_ADD_FILE_RO(regs); |
202 | DEBUG_ADD_FILE_RO(tlb); | |
3ca5db07 | 203 | DEBUG_ADD_FILE_RO(pagetable); |
14e0e679 | 204 | |
61c75352 | 205 | return; |
46451d62 | 206 | |
61c75352 SA |
207 | err: |
208 | debugfs_remove_recursive(obj->debug_dir); | |
46451d62 OBC |
209 | } |
210 | ||
61c75352 | 211 | void omap_iommu_debugfs_remove(struct omap_iommu *obj) |
46451d62 | 212 | { |
61c75352 SA |
213 | if (!obj->debug_dir) |
214 | return; | |
46451d62 | 215 | |
61c75352 | 216 | debugfs_remove_recursive(obj->debug_dir); |
14e0e679 HD |
217 | } |
218 | ||
61c75352 | 219 | void __init omap_iommu_debugfs_init(void) |
14e0e679 | 220 | { |
61c75352 SA |
221 | iommu_debug_root = debugfs_create_dir("omap_iommu", NULL); |
222 | if (!iommu_debug_root) | |
223 | pr_err("can't create debugfs dir\n"); | |
14e0e679 | 224 | } |
14e0e679 | 225 | |
61c75352 | 226 | void __exit omap_iommu_debugfs_exit(void) |
14e0e679 | 227 | { |
61c75352 | 228 | debugfs_remove(iommu_debug_root); |
14e0e679 | 229 | } |