blob: f3d20a2039d20417093fd70c6192b905e7c3ccf8 [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
Suman Anna9c83e9f2014-10-22 17:22:35 -050088static void dump_ioptable(struct seq_file *s)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070089{
Suman Anna9c83e9f2014-10-22 17:22:35 -050090 int i, j;
91 u32 da;
92 u32 *iopgd, *iopte;
93 struct omap_iommu *obj = s->private;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070094
95 spin_lock(&obj->page_table_lock);
96
97 iopgd = iopgd_offset(obj, 0);
98 for (i = 0; i < PTRS_PER_IOPGD; i++, iopgd++) {
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070099 if (!*iopgd)
100 continue;
101
102 if (!(*iopgd & IOPGD_TABLE)) {
103 da = i << IOPGD_SHIFT;
Suman Anna9c83e9f2014-10-22 17:22:35 -0500104 seq_printf(s, "1: 0x%08x 0x%08x\n", da, *iopgd);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700105 continue;
106 }
107
108 iopte = iopte_offset(iopgd, 0);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700109 for (j = 0; j < PTRS_PER_IOPTE; j++, iopte++) {
110 if (!*iopte)
111 continue;
112
113 da = (i << IOPGD_SHIFT) + (j << IOPTE_SHIFT);
Suman Anna9c83e9f2014-10-22 17:22:35 -0500114 seq_printf(s, "2: 0x%08x 0x%08x\n", da, *iopte);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700115 }
116 }
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700117
Suman Anna9c83e9f2014-10-22 17:22:35 -0500118 spin_unlock(&obj->page_table_lock);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700119}
120
Suman Anna9c83e9f2014-10-22 17:22:35 -0500121static int debug_read_pagetable(struct seq_file *s, void *data)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700122{
Suman Anna9c83e9f2014-10-22 17:22:35 -0500123 struct omap_iommu *obj = s->private;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700124
Suman Annac5cf5c52014-10-22 17:22:34 -0500125 if (is_omap_iommu_detached(obj))
126 return -EPERM;
127
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700128 mutex_lock(&iommu_debug_lock);
129
Suman Anna9c83e9f2014-10-22 17:22:35 -0500130 seq_printf(s, "L: %8s %8s\n", "da:", "pte:");
131 seq_puts(s, "--------------------------\n");
132 dump_ioptable(s);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700133
134 mutex_unlock(&iommu_debug_lock);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700135
Suman Anna9c83e9f2014-10-22 17:22:35 -0500136 return 0;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700137}
138
Suman Anna9c83e9f2014-10-22 17:22:35 -0500139#define DEBUG_SEQ_FOPS_RO(name) \
140 static int debug_open_##name(struct inode *inode, struct file *file) \
141 { \
142 return single_open(file, debug_read_##name, inode->i_private); \
143 } \
144 \
145 static const struct file_operations debug_##name##_fops = { \
146 .open = debug_open_##name, \
147 .read = seq_read, \
148 .llseek = seq_lseek, \
149 .release = single_release, \
150 }
151
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700152#define DEBUG_FOPS_RO(name) \
153 static const struct file_operations debug_##name##_fops = { \
Stephen Boyd234e3402012-04-05 14:25:11 -0700154 .open = simple_open, \
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700155 .read = debug_read_##name, \
Arnd Bergmannc0b0aca2010-07-06 19:16:33 +0200156 .llseek = generic_file_llseek, \
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700157 };
158
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700159DEBUG_FOPS_RO(regs);
160DEBUG_FOPS_RO(tlb);
Suman Anna9c83e9f2014-10-22 17:22:35 -0500161DEBUG_SEQ_FOPS_RO(pagetable);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700162
163#define __DEBUG_ADD_FILE(attr, mode) \
164 { \
165 struct dentry *dent; \
Suman Anna61c75352014-10-22 17:22:30 -0500166 dent = debugfs_create_file(#attr, mode, obj->debug_dir, \
167 obj, &debug_##attr##_fops); \
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700168 if (!dent) \
Suman Anna61c75352014-10-22 17:22:30 -0500169 goto err; \
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700170 }
171
Joe Perchesff3a2b72014-02-25 15:01:40 -0800172#define DEBUG_ADD_FILE_RO(name) __DEBUG_ADD_FILE(name, 0400)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700173
Suman Anna61c75352014-10-22 17:22:30 -0500174void omap_iommu_debugfs_add(struct omap_iommu *obj)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700175{
Suman Anna61c75352014-10-22 17:22:30 -0500176 struct dentry *d;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700177
Suman Anna61c75352014-10-22 17:22:30 -0500178 if (!iommu_debug_root)
179 return;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700180
Suman Anna61c75352014-10-22 17:22:30 -0500181 obj->debug_dir = debugfs_create_dir(obj->name, iommu_debug_root);
182 if (!obj->debug_dir)
183 return;
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200184
Suman Anna61c75352014-10-22 17:22:30 -0500185 d = debugfs_create_u8("nr_tlb_entries", 0400, obj->debug_dir,
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700186 (u8 *)&obj->nr_tlb_entries);
187 if (!d)
Suman Anna61c75352014-10-22 17:22:30 -0500188 return;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700189
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700190 DEBUG_ADD_FILE_RO(regs);
191 DEBUG_ADD_FILE_RO(tlb);
Suman Anna3ca5db02014-10-22 17:22:29 -0500192 DEBUG_ADD_FILE_RO(pagetable);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700193
Suman Anna61c75352014-10-22 17:22:30 -0500194 return;
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200195
Suman Anna61c75352014-10-22 17:22:30 -0500196err:
197 debugfs_remove_recursive(obj->debug_dir);
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200198}
199
Suman Anna61c75352014-10-22 17:22:30 -0500200void omap_iommu_debugfs_remove(struct omap_iommu *obj)
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200201{
Suman Anna61c75352014-10-22 17:22:30 -0500202 if (!obj->debug_dir)
203 return;
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200204
Suman Anna61c75352014-10-22 17:22:30 -0500205 debugfs_remove_recursive(obj->debug_dir);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700206}
207
Suman Anna61c75352014-10-22 17:22:30 -0500208void __init omap_iommu_debugfs_init(void)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700209{
Suman Anna61c75352014-10-22 17:22:30 -0500210 iommu_debug_root = debugfs_create_dir("omap_iommu", NULL);
211 if (!iommu_debug_root)
212 pr_err("can't create debugfs dir\n");
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700213}
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700214
Suman Anna61c75352014-10-22 17:22:30 -0500215void __exit omap_iommu_debugfs_exit(void)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700216{
Suman Anna61c75352014-10-22 17:22:30 -0500217 debugfs_remove(iommu_debug_root);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700218}