blob: 4813d3a71a001e197dde3cc5101ee5a2bdff623e [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
13#include <linux/err.h>
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070014#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070016#include <linux/uaccess.h>
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070017#include <linux/debugfs.h>
Tony Lindgren2ab7c842012-11-02 12:24:14 -070018#include <linux/platform_data/iommu-omap.h>
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070019
Ido Yariv2f7702a2012-11-02 12:24:00 -070020#include "omap-iopgtable.h"
Tony Lindgrened1c7de2012-11-02 12:24:06 -070021#include "omap-iommu.h"
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070022
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070023static DEFINE_MUTEX(iommu_debug_lock);
24
25static struct dentry *iommu_debug_root;
26
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070027static ssize_t debug_read_regs(struct file *file, char __user *userbuf,
28 size_t count, loff_t *ppos)
29{
Suman Anna61c75352014-10-22 17:22:30 -050030 struct omap_iommu *obj = file->private_data;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070031 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
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030041 bytes = omap_iommu_dump_ctx(obj, p, count);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070042 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
50static ssize_t debug_read_tlb(struct file *file, char __user *userbuf,
51 size_t count, loff_t *ppos)
52{
Suman Anna61c75352014-10-22 17:22:30 -050053 struct omap_iommu *obj = file->private_data;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070054 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);
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030067 p += omap_dump_tlb_entries(obj, p, rest);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070068
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
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070077#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
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030091static ssize_t dump_ioptable(struct omap_iommu *obj, char *buf, ssize_t len)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070092{
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 }
129out:
130 spin_unlock(&obj->page_table_lock);
131
132 return p - buf;
133}
134
135static ssize_t debug_read_pagetable(struct file *file, char __user *userbuf,
136 size_t count, loff_t *ppos)
137{
Suman Anna61c75352014-10-22 17:22:30 -0500138 struct omap_iommu *obj = file->private_data;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700139 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
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700163#define DEBUG_FOPS_RO(name) \
164 static const struct file_operations debug_##name##_fops = { \
Stephen Boyd234e3402012-04-05 14:25:11 -0700165 .open = simple_open, \
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700166 .read = debug_read_##name, \
Arnd Bergmannc0b0aca2010-07-06 19:16:33 +0200167 .llseek = generic_file_llseek, \
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700168 };
169
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700170DEBUG_FOPS_RO(regs);
171DEBUG_FOPS_RO(tlb);
Suman Anna3ca5db072014-10-22 17:22:29 -0500172DEBUG_FOPS_RO(pagetable);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700173
174#define __DEBUG_ADD_FILE(attr, mode) \
175 { \
176 struct dentry *dent; \
Suman Anna61c75352014-10-22 17:22:30 -0500177 dent = debugfs_create_file(#attr, mode, obj->debug_dir, \
178 obj, &debug_##attr##_fops); \
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700179 if (!dent) \
Suman Anna61c75352014-10-22 17:22:30 -0500180 goto err; \
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700181 }
182
Joe Perchesff3a2b72014-02-25 15:01:40 -0800183#define DEBUG_ADD_FILE_RO(name) __DEBUG_ADD_FILE(name, 0400)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700184
Suman Anna61c75352014-10-22 17:22:30 -0500185void omap_iommu_debugfs_add(struct omap_iommu *obj)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700186{
Suman Anna61c75352014-10-22 17:22:30 -0500187 struct dentry *d;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700188
Suman Anna61c75352014-10-22 17:22:30 -0500189 if (!iommu_debug_root)
190 return;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700191
Suman Anna61c75352014-10-22 17:22:30 -0500192 obj->debug_dir = debugfs_create_dir(obj->name, iommu_debug_root);
193 if (!obj->debug_dir)
194 return;
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200195
Suman Anna61c75352014-10-22 17:22:30 -0500196 d = debugfs_create_u8("nr_tlb_entries", 0400, obj->debug_dir,
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700197 (u8 *)&obj->nr_tlb_entries);
198 if (!d)
Suman Anna61c75352014-10-22 17:22:30 -0500199 return;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700200
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700201 DEBUG_ADD_FILE_RO(regs);
202 DEBUG_ADD_FILE_RO(tlb);
Suman Anna3ca5db072014-10-22 17:22:29 -0500203 DEBUG_ADD_FILE_RO(pagetable);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700204
Suman Anna61c75352014-10-22 17:22:30 -0500205 return;
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200206
Suman Anna61c75352014-10-22 17:22:30 -0500207err:
208 debugfs_remove_recursive(obj->debug_dir);
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200209}
210
Suman Anna61c75352014-10-22 17:22:30 -0500211void omap_iommu_debugfs_remove(struct omap_iommu *obj)
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200212{
Suman Anna61c75352014-10-22 17:22:30 -0500213 if (!obj->debug_dir)
214 return;
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200215
Suman Anna61c75352014-10-22 17:22:30 -0500216 debugfs_remove_recursive(obj->debug_dir);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700217}
218
Suman Anna61c75352014-10-22 17:22:30 -0500219void __init omap_iommu_debugfs_init(void)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700220{
Suman Anna61c75352014-10-22 17:22:30 -0500221 iommu_debug_root = debugfs_create_dir("omap_iommu", NULL);
222 if (!iommu_debug_root)
223 pr_err("can't create debugfs dir\n");
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700224}
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700225
Suman Anna61c75352014-10-22 17:22:30 -0500226void __exit omap_iommu_debugfs_exit(void)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700227{
Suman Anna61c75352014-10-22 17:22:30 -0500228 debugfs_remove(iommu_debug_root);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700229}