blob: 41b09a1d78ea01ef8c635471e4d17cd68f26af83 [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
Suman Annac5cf5c52014-10-22 17:22:34 -050027static inline bool is_omap_iommu_detached(struct omap_iommu *obj)
28{
29 return !obj->domain;
30}
31
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070032static ssize_t debug_read_regs(struct file *file, char __user *userbuf,
33 size_t count, loff_t *ppos)
34{
Suman Anna61c75352014-10-22 17:22:30 -050035 struct omap_iommu *obj = file->private_data;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070036 char *p, *buf;
37 ssize_t bytes;
38
Suman Annac5cf5c52014-10-22 17:22:34 -050039 if (is_omap_iommu_detached(obj))
40 return -EPERM;
41
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070042 buf = kmalloc(count, GFP_KERNEL);
43 if (!buf)
44 return -ENOMEM;
45 p = buf;
46
47 mutex_lock(&iommu_debug_lock);
48
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030049 bytes = omap_iommu_dump_ctx(obj, p, count);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070050 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, bytes);
51
52 mutex_unlock(&iommu_debug_lock);
53 kfree(buf);
54
55 return bytes;
56}
57
58static ssize_t debug_read_tlb(struct file *file, char __user *userbuf,
59 size_t count, loff_t *ppos)
60{
Suman Anna61c75352014-10-22 17:22:30 -050061 struct omap_iommu *obj = file->private_data;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070062 char *p, *buf;
63 ssize_t bytes, rest;
64
Suman Annac5cf5c52014-10-22 17:22:34 -050065 if (is_omap_iommu_detached(obj))
66 return -EPERM;
67
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070068 buf = kmalloc(count, GFP_KERNEL);
69 if (!buf)
70 return -ENOMEM;
71 p = buf;
72
73 mutex_lock(&iommu_debug_lock);
74
75 p += sprintf(p, "%8s %8s\n", "cam:", "ram:");
76 p += sprintf(p, "-----------------------------------------\n");
77 rest = count - (p - buf);
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030078 p += omap_dump_tlb_entries(obj, p, rest);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070079
80 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
81
82 mutex_unlock(&iommu_debug_lock);
83 kfree(buf);
84
85 return bytes;
86}
87
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070088#define dump_ioptable_entry_one(lv, da, val) \
89 ({ \
90 int __err = 0; \
91 ssize_t bytes; \
92 const int maxcol = 22; \
93 const char *str = "%d: %08x %08x\n"; \
94 bytes = snprintf(p, maxcol, str, lv, da, val); \
95 p += bytes; \
96 len -= bytes; \
97 if (len < maxcol) \
98 __err = -ENOMEM; \
99 __err; \
100 })
101
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300102static ssize_t dump_ioptable(struct omap_iommu *obj, char *buf, ssize_t len)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700103{
104 int i;
105 u32 *iopgd;
106 char *p = buf;
107
108 spin_lock(&obj->page_table_lock);
109
110 iopgd = iopgd_offset(obj, 0);
111 for (i = 0; i < PTRS_PER_IOPGD; i++, iopgd++) {
112 int j, err;
113 u32 *iopte;
114 u32 da;
115
116 if (!*iopgd)
117 continue;
118
119 if (!(*iopgd & IOPGD_TABLE)) {
120 da = i << IOPGD_SHIFT;
121
122 err = dump_ioptable_entry_one(1, da, *iopgd);
123 if (err)
124 goto out;
125 continue;
126 }
127
128 iopte = iopte_offset(iopgd, 0);
129
130 for (j = 0; j < PTRS_PER_IOPTE; j++, iopte++) {
131 if (!*iopte)
132 continue;
133
134 da = (i << IOPGD_SHIFT) + (j << IOPTE_SHIFT);
135 err = dump_ioptable_entry_one(2, da, *iopgd);
136 if (err)
137 goto out;
138 }
139 }
140out:
141 spin_unlock(&obj->page_table_lock);
142
143 return p - buf;
144}
145
146static ssize_t debug_read_pagetable(struct file *file, char __user *userbuf,
147 size_t count, loff_t *ppos)
148{
Suman Anna61c75352014-10-22 17:22:30 -0500149 struct omap_iommu *obj = file->private_data;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700150 char *p, *buf;
151 size_t bytes;
152
Suman Annac5cf5c52014-10-22 17:22:34 -0500153 if (is_omap_iommu_detached(obj))
154 return -EPERM;
155
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700156 buf = (char *)__get_free_page(GFP_KERNEL);
157 if (!buf)
158 return -ENOMEM;
159 p = buf;
160
161 p += sprintf(p, "L: %8s %8s\n", "da:", "pa:");
162 p += sprintf(p, "-----------------------------------------\n");
163
164 mutex_lock(&iommu_debug_lock);
165
166 bytes = PAGE_SIZE - (p - buf);
167 p += dump_ioptable(obj, p, bytes);
168
169 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
170
171 mutex_unlock(&iommu_debug_lock);
172 free_page((unsigned long)buf);
173
174 return bytes;
175}
176
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700177#define DEBUG_FOPS_RO(name) \
178 static const struct file_operations debug_##name##_fops = { \
Stephen Boyd234e3402012-04-05 14:25:11 -0700179 .open = simple_open, \
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700180 .read = debug_read_##name, \
Arnd Bergmannc0b0aca2010-07-06 19:16:33 +0200181 .llseek = generic_file_llseek, \
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700182 };
183
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700184DEBUG_FOPS_RO(regs);
185DEBUG_FOPS_RO(tlb);
Suman Anna3ca5db072014-10-22 17:22:29 -0500186DEBUG_FOPS_RO(pagetable);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700187
188#define __DEBUG_ADD_FILE(attr, mode) \
189 { \
190 struct dentry *dent; \
Suman Anna61c75352014-10-22 17:22:30 -0500191 dent = debugfs_create_file(#attr, mode, obj->debug_dir, \
192 obj, &debug_##attr##_fops); \
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700193 if (!dent) \
Suman Anna61c75352014-10-22 17:22:30 -0500194 goto err; \
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700195 }
196
Joe Perchesff3a2b72014-02-25 15:01:40 -0800197#define DEBUG_ADD_FILE_RO(name) __DEBUG_ADD_FILE(name, 0400)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700198
Suman Anna61c75352014-10-22 17:22:30 -0500199void omap_iommu_debugfs_add(struct omap_iommu *obj)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700200{
Suman Anna61c75352014-10-22 17:22:30 -0500201 struct dentry *d;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700202
Suman Anna61c75352014-10-22 17:22:30 -0500203 if (!iommu_debug_root)
204 return;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700205
Suman Anna61c75352014-10-22 17:22:30 -0500206 obj->debug_dir = debugfs_create_dir(obj->name, iommu_debug_root);
207 if (!obj->debug_dir)
208 return;
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200209
Suman Anna61c75352014-10-22 17:22:30 -0500210 d = debugfs_create_u8("nr_tlb_entries", 0400, obj->debug_dir,
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700211 (u8 *)&obj->nr_tlb_entries);
212 if (!d)
Suman Anna61c75352014-10-22 17:22:30 -0500213 return;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700214
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700215 DEBUG_ADD_FILE_RO(regs);
216 DEBUG_ADD_FILE_RO(tlb);
Suman Anna3ca5db072014-10-22 17:22:29 -0500217 DEBUG_ADD_FILE_RO(pagetable);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700218
Suman Anna61c75352014-10-22 17:22:30 -0500219 return;
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200220
Suman Anna61c75352014-10-22 17:22:30 -0500221err:
222 debugfs_remove_recursive(obj->debug_dir);
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200223}
224
Suman Anna61c75352014-10-22 17:22:30 -0500225void omap_iommu_debugfs_remove(struct omap_iommu *obj)
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200226{
Suman Anna61c75352014-10-22 17:22:30 -0500227 if (!obj->debug_dir)
228 return;
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200229
Suman Anna61c75352014-10-22 17:22:30 -0500230 debugfs_remove_recursive(obj->debug_dir);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700231}
232
Suman Anna61c75352014-10-22 17:22:30 -0500233void __init omap_iommu_debugfs_init(void)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700234{
Suman Anna61c75352014-10-22 17:22:30 -0500235 iommu_debug_root = debugfs_create_dir("omap_iommu", NULL);
236 if (!iommu_debug_root)
237 pr_err("can't create debugfs dir\n");
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700238}
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700239
Suman Anna61c75352014-10-22 17:22:30 -0500240void __exit omap_iommu_debugfs_exit(void)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700241{
Suman Anna61c75352014-10-22 17:22:30 -0500242 debugfs_remove(iommu_debug_root);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700243}