blob: 28de657e38c0952544db60996fb7acf20abb932c [file] [log] [blame]
Hiroshi DOYU14e0e672009-08-28 10:54:41 -07001/*
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
Ming Lei08f2e632011-11-08 18:29:15 +080013#include <linux/module.h>
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070014#include <linux/err.h>
15#include <linux/clk.h>
16#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070018#include <linux/uaccess.h>
19#include <linux/platform_device.h>
20#include <linux/debugfs.h>
Tony Lindgrenc8d35c82012-11-02 12:24:03 -070021#include <linux/omap-iommu.h>
Tony Lindgren2ab7c842012-11-02 12:24:14 -070022#include <linux/platform_data/iommu-omap.h>
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070023
Ido Yariv2f7702a2012-11-02 12:24:00 -070024#include "omap-iopgtable.h"
Tony Lindgrened1c7de2012-11-02 12:24:06 -070025#include "omap-iommu.h"
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070026
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070027static DEFINE_MUTEX(iommu_debug_lock);
28
29static struct dentry *iommu_debug_root;
30
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070031static ssize_t debug_read_regs(struct file *file, char __user *userbuf,
32 size_t count, loff_t *ppos)
33{
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +020034 struct device *dev = file->private_data;
35 struct omap_iommu *obj = dev_to_omap_iommu(dev);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070036 char *p, *buf;
37 ssize_t bytes;
38
39 buf = kmalloc(count, GFP_KERNEL);
40 if (!buf)
41 return -ENOMEM;
42 p = buf;
43
44 mutex_lock(&iommu_debug_lock);
45
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030046 bytes = omap_iommu_dump_ctx(obj, p, count);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070047 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, bytes);
48
49 mutex_unlock(&iommu_debug_lock);
50 kfree(buf);
51
52 return bytes;
53}
54
55static ssize_t debug_read_tlb(struct file *file, char __user *userbuf,
56 size_t count, loff_t *ppos)
57{
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +020058 struct device *dev = file->private_data;
59 struct omap_iommu *obj = dev_to_omap_iommu(dev);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070060 char *p, *buf;
61 ssize_t bytes, rest;
62
63 buf = kmalloc(count, GFP_KERNEL);
64 if (!buf)
65 return -ENOMEM;
66 p = buf;
67
68 mutex_lock(&iommu_debug_lock);
69
70 p += sprintf(p, "%8s %8s\n", "cam:", "ram:");
71 p += sprintf(p, "-----------------------------------------\n");
72 rest = count - (p - buf);
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030073 p += omap_dump_tlb_entries(obj, p, rest);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070074
75 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
76
77 mutex_unlock(&iommu_debug_lock);
78 kfree(buf);
79
80 return bytes;
81}
82
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070083#define dump_ioptable_entry_one(lv, da, val) \
84 ({ \
85 int __err = 0; \
86 ssize_t bytes; \
87 const int maxcol = 22; \
88 const char *str = "%d: %08x %08x\n"; \
89 bytes = snprintf(p, maxcol, str, lv, da, val); \
90 p += bytes; \
91 len -= bytes; \
92 if (len < maxcol) \
93 __err = -ENOMEM; \
94 __err; \
95 })
96
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030097static ssize_t dump_ioptable(struct omap_iommu *obj, char *buf, ssize_t len)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070098{
99 int i;
100 u32 *iopgd;
101 char *p = buf;
102
103 spin_lock(&obj->page_table_lock);
104
105 iopgd = iopgd_offset(obj, 0);
106 for (i = 0; i < PTRS_PER_IOPGD; i++, iopgd++) {
107 int j, err;
108 u32 *iopte;
109 u32 da;
110
111 if (!*iopgd)
112 continue;
113
114 if (!(*iopgd & IOPGD_TABLE)) {
115 da = i << IOPGD_SHIFT;
116
117 err = dump_ioptable_entry_one(1, da, *iopgd);
118 if (err)
119 goto out;
120 continue;
121 }
122
123 iopte = iopte_offset(iopgd, 0);
124
125 for (j = 0; j < PTRS_PER_IOPTE; j++, iopte++) {
126 if (!*iopte)
127 continue;
128
129 da = (i << IOPGD_SHIFT) + (j << IOPTE_SHIFT);
130 err = dump_ioptable_entry_one(2, da, *iopgd);
131 if (err)
132 goto out;
133 }
134 }
135out:
136 spin_unlock(&obj->page_table_lock);
137
138 return p - buf;
139}
140
141static ssize_t debug_read_pagetable(struct file *file, char __user *userbuf,
142 size_t count, loff_t *ppos)
143{
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200144 struct device *dev = file->private_data;
145 struct omap_iommu *obj = dev_to_omap_iommu(dev);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700146 char *p, *buf;
147 size_t bytes;
148
149 buf = (char *)__get_free_page(GFP_KERNEL);
150 if (!buf)
151 return -ENOMEM;
152 p = buf;
153
154 p += sprintf(p, "L: %8s %8s\n", "da:", "pa:");
155 p += sprintf(p, "-----------------------------------------\n");
156
157 mutex_lock(&iommu_debug_lock);
158
159 bytes = PAGE_SIZE - (p - buf);
160 p += dump_ioptable(obj, p, bytes);
161
162 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
163
164 mutex_unlock(&iommu_debug_lock);
165 free_page((unsigned long)buf);
166
167 return bytes;
168}
169
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700170#define DEBUG_FOPS_RO(name) \
171 static const struct file_operations debug_##name##_fops = { \
Stephen Boyd234e3402012-04-05 14:25:11 -0700172 .open = simple_open, \
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700173 .read = debug_read_##name, \
Arnd Bergmannc0b0aca2010-07-06 19:16:33 +0200174 .llseek = generic_file_llseek, \
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700175 };
176
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700177DEBUG_FOPS_RO(regs);
178DEBUG_FOPS_RO(tlb);
Suman Anna3ca5db02014-10-22 17:22:29 -0500179DEBUG_FOPS_RO(pagetable);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700180
181#define __DEBUG_ADD_FILE(attr, mode) \
182 { \
183 struct dentry *dent; \
184 dent = debugfs_create_file(#attr, mode, parent, \
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200185 dev, &debug_##attr##_fops); \
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700186 if (!dent) \
187 return -ENOMEM; \
188 }
189
Joe Perchesff3a2b72014-02-25 15:01:40 -0800190#define DEBUG_ADD_FILE_RO(name) __DEBUG_ADD_FILE(name, 0400)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700191
192static int iommu_debug_register(struct device *dev, void *data)
193{
194 struct platform_device *pdev = to_platform_device(dev);
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300195 struct omap_iommu *obj = platform_get_drvdata(pdev);
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200196 struct omap_iommu_arch_data *arch_data;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700197 struct dentry *d, *parent;
198
199 if (!obj || !obj->dev)
200 return -EINVAL;
201
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200202 arch_data = kzalloc(sizeof(*arch_data), GFP_KERNEL);
203 if (!arch_data)
204 return -ENOMEM;
205
206 arch_data->iommu_dev = obj;
207
208 dev->archdata.iommu = arch_data;
209
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700210 d = debugfs_create_dir(obj->name, iommu_debug_root);
211 if (!d)
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200212 goto nomem;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700213 parent = d;
214
Suman Anna68570a72014-10-22 17:22:28 -0500215 d = debugfs_create_u8("nr_tlb_entries", 0400, parent,
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700216 (u8 *)&obj->nr_tlb_entries);
217 if (!d)
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200218 goto nomem;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700219
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700220 DEBUG_ADD_FILE_RO(regs);
221 DEBUG_ADD_FILE_RO(tlb);
Suman Anna3ca5db02014-10-22 17:22:29 -0500222 DEBUG_ADD_FILE_RO(pagetable);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700223
224 return 0;
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200225
226nomem:
227 kfree(arch_data);
228 return -ENOMEM;
229}
230
231static int iommu_debug_unregister(struct device *dev, void *data)
232{
233 if (!dev->archdata.iommu)
234 return 0;
235
236 kfree(dev->archdata.iommu);
237
238 dev->archdata.iommu = NULL;
239
240 return 0;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700241}
242
243static int __init iommu_debug_init(void)
244{
245 struct dentry *d;
246 int err;
247
248 d = debugfs_create_dir("iommu", NULL);
249 if (!d)
250 return -ENOMEM;
251 iommu_debug_root = d;
252
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300253 err = omap_foreach_iommu_device(d, iommu_debug_register);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700254 if (err)
255 goto err_out;
256 return 0;
257
258err_out:
259 debugfs_remove_recursive(iommu_debug_root);
260 return err;
261}
262module_init(iommu_debug_init)
263
264static void __exit iommu_debugfs_exit(void)
265{
266 debugfs_remove_recursive(iommu_debug_root);
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200267 omap_foreach_iommu_device(NULL, iommu_debug_unregister);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700268}
269module_exit(iommu_debugfs_exit)
270
271MODULE_DESCRIPTION("omap iommu: debugfs interface");
272MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>");
273MODULE_LICENSE("GPL v2");