blob: 0717aa96ce39bd22d1b4fe9e3a82242fa1e5403e [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>
Suman Anna69c2c192015-07-20 17:33:25 -050017#include <linux/pm_runtime.h>
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070018#include <linux/debugfs.h>
Tony Lindgren2ab7c842012-11-02 12:24:14 -070019#include <linux/platform_data/iommu-omap.h>
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070020
Ido Yariv2f7702a2012-11-02 12:24:00 -070021#include "omap-iopgtable.h"
Tony Lindgrened1c7de2012-11-02 12:24:06 -070022#include "omap-iommu.h"
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070023
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070024static DEFINE_MUTEX(iommu_debug_lock);
25
26static struct dentry *iommu_debug_root;
27
Suman Annac5cf5c52014-10-22 17:22:34 -050028static inline bool is_omap_iommu_detached(struct omap_iommu *obj)
29{
30 return !obj->domain;
31}
32
Suman Anna69c2c192015-07-20 17:33:25 -050033#define pr_reg(name) \
34 do { \
35 ssize_t bytes; \
36 const char *str = "%20s: %08x\n"; \
37 const int maxcol = 32; \
38 bytes = snprintf(p, maxcol, str, __stringify(name), \
39 iommu_read_reg(obj, MMU_##name)); \
40 p += bytes; \
41 len -= bytes; \
42 if (len < maxcol) \
43 goto out; \
44 } while (0)
45
46static ssize_t
47omap2_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t len)
48{
49 char *p = buf;
50
51 pr_reg(REVISION);
52 pr_reg(IRQSTATUS);
53 pr_reg(IRQENABLE);
54 pr_reg(WALKING_ST);
55 pr_reg(CNTL);
56 pr_reg(FAULT_AD);
57 pr_reg(TTB);
58 pr_reg(LOCK);
59 pr_reg(LD_TLB);
60 pr_reg(CAM);
61 pr_reg(RAM);
62 pr_reg(GFLUSH);
63 pr_reg(FLUSH_ENTRY);
64 pr_reg(READ_CAM);
65 pr_reg(READ_RAM);
66 pr_reg(EMU_FAULT_AD);
67out:
68 return p - buf;
69}
70
71static ssize_t omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf,
72 ssize_t bytes)
73{
74 if (!obj || !buf)
75 return -EINVAL;
76
77 pm_runtime_get_sync(obj->dev);
78
79 bytes = omap2_iommu_dump_ctx(obj, buf, bytes);
80
81 pm_runtime_put_sync(obj->dev);
82
83 return bytes;
84}
85
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070086static ssize_t debug_read_regs(struct file *file, char __user *userbuf,
87 size_t count, loff_t *ppos)
88{
Suman Anna61c75352014-10-22 17:22:30 -050089 struct omap_iommu *obj = file->private_data;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070090 char *p, *buf;
91 ssize_t bytes;
92
Suman Annac5cf5c52014-10-22 17:22:34 -050093 if (is_omap_iommu_detached(obj))
94 return -EPERM;
95
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070096 buf = kmalloc(count, GFP_KERNEL);
97 if (!buf)
98 return -ENOMEM;
99 p = buf;
100
101 mutex_lock(&iommu_debug_lock);
102
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300103 bytes = omap_iommu_dump_ctx(obj, p, count);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700104 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, bytes);
105
106 mutex_unlock(&iommu_debug_lock);
107 kfree(buf);
108
109 return bytes;
110}
111
Suman Anna69c2c192015-07-20 17:33:25 -0500112static int
113__dump_tlb_entries(struct omap_iommu *obj, struct cr_regs *crs, int num)
114{
115 int i;
116 struct iotlb_lock saved;
117 struct cr_regs tmp;
118 struct cr_regs *p = crs;
119
120 pm_runtime_get_sync(obj->dev);
121 iotlb_lock_get(obj, &saved);
122
123 for_each_iotlb_cr(obj, num, i, tmp) {
124 if (!iotlb_cr_valid(&tmp))
125 continue;
126 *p++ = tmp;
127 }
128
129 iotlb_lock_set(obj, &saved);
130 pm_runtime_put_sync(obj->dev);
131
132 return p - crs;
133}
134
135static ssize_t iotlb_dump_cr(struct omap_iommu *obj, struct cr_regs *cr,
Salva Peiróe203db22015-07-23 14:26:19 +0200136 struct seq_file *s)
Suman Anna69c2c192015-07-20 17:33:25 -0500137{
Salva Peiróe203db22015-07-23 14:26:19 +0200138 return seq_printf(s, "%08x %08x %01x\n", cr->cam, cr->ram,
139 (cr->cam & MMU_CAM_P) ? 1 : 0);
Suman Anna69c2c192015-07-20 17:33:25 -0500140}
141
Salva Peiróe203db22015-07-23 14:26:19 +0200142static size_t omap_dump_tlb_entries(struct omap_iommu *obj, struct seq_file *s)
Suman Anna69c2c192015-07-20 17:33:25 -0500143{
144 int i, num;
145 struct cr_regs *cr;
Suman Anna69c2c192015-07-20 17:33:25 -0500146
Salva Peiróe203db22015-07-23 14:26:19 +0200147 num = obj->nr_tlb_entries;
Suman Anna69c2c192015-07-20 17:33:25 -0500148
149 cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
150 if (!cr)
151 return 0;
152
153 num = __dump_tlb_entries(obj, cr, num);
154 for (i = 0; i < num; i++)
Salva Peiróe203db22015-07-23 14:26:19 +0200155 iotlb_dump_cr(obj, cr + i, s);
Suman Anna69c2c192015-07-20 17:33:25 -0500156 kfree(cr);
157
Salva Peiróe203db22015-07-23 14:26:19 +0200158 return 0;
Suman Anna69c2c192015-07-20 17:33:25 -0500159}
160
Salva Peiróe203db22015-07-23 14:26:19 +0200161static int debug_read_tlb(struct seq_file *s, void *data)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700162{
Salva Peiróe203db22015-07-23 14:26:19 +0200163 struct omap_iommu *obj = s->private;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700164
Suman Annac5cf5c52014-10-22 17:22:34 -0500165 if (is_omap_iommu_detached(obj))
166 return -EPERM;
167
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700168 mutex_lock(&iommu_debug_lock);
169
Salva Peiróe203db22015-07-23 14:26:19 +0200170 seq_printf(s, "%8s %8s\n", "cam:", "ram:");
171 seq_puts(s, "-----------------------------------------\n");
172 omap_dump_tlb_entries(obj, s);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700173
174 mutex_unlock(&iommu_debug_lock);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700175
Salva Peiróe203db22015-07-23 14:26:19 +0200176 return 0;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700177}
178
Suman Anna9c83e9f2014-10-22 17:22:35 -0500179static void dump_ioptable(struct seq_file *s)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700180{
Suman Anna9c83e9f2014-10-22 17:22:35 -0500181 int i, j;
182 u32 da;
183 u32 *iopgd, *iopte;
184 struct omap_iommu *obj = s->private;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700185
186 spin_lock(&obj->page_table_lock);
187
188 iopgd = iopgd_offset(obj, 0);
189 for (i = 0; i < PTRS_PER_IOPGD; i++, iopgd++) {
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700190 if (!*iopgd)
191 continue;
192
193 if (!(*iopgd & IOPGD_TABLE)) {
194 da = i << IOPGD_SHIFT;
Suman Anna9c83e9f2014-10-22 17:22:35 -0500195 seq_printf(s, "1: 0x%08x 0x%08x\n", da, *iopgd);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700196 continue;
197 }
198
199 iopte = iopte_offset(iopgd, 0);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700200 for (j = 0; j < PTRS_PER_IOPTE; j++, iopte++) {
201 if (!*iopte)
202 continue;
203
204 da = (i << IOPGD_SHIFT) + (j << IOPTE_SHIFT);
Suman Anna9c83e9f2014-10-22 17:22:35 -0500205 seq_printf(s, "2: 0x%08x 0x%08x\n", da, *iopte);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700206 }
207 }
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700208
Suman Anna9c83e9f2014-10-22 17:22:35 -0500209 spin_unlock(&obj->page_table_lock);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700210}
211
Suman Anna9c83e9f2014-10-22 17:22:35 -0500212static int debug_read_pagetable(struct seq_file *s, void *data)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700213{
Suman Anna9c83e9f2014-10-22 17:22:35 -0500214 struct omap_iommu *obj = s->private;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700215
Suman Annac5cf5c52014-10-22 17:22:34 -0500216 if (is_omap_iommu_detached(obj))
217 return -EPERM;
218
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700219 mutex_lock(&iommu_debug_lock);
220
Suman Anna9c83e9f2014-10-22 17:22:35 -0500221 seq_printf(s, "L: %8s %8s\n", "da:", "pte:");
222 seq_puts(s, "--------------------------\n");
223 dump_ioptable(s);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700224
225 mutex_unlock(&iommu_debug_lock);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700226
Suman Anna9c83e9f2014-10-22 17:22:35 -0500227 return 0;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700228}
229
Suman Anna9c83e9f2014-10-22 17:22:35 -0500230#define DEBUG_SEQ_FOPS_RO(name) \
231 static int debug_open_##name(struct inode *inode, struct file *file) \
232 { \
233 return single_open(file, debug_read_##name, inode->i_private); \
234 } \
235 \
236 static const struct file_operations debug_##name##_fops = { \
237 .open = debug_open_##name, \
238 .read = seq_read, \
239 .llseek = seq_lseek, \
240 .release = single_release, \
241 }
242
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700243#define DEBUG_FOPS_RO(name) \
244 static const struct file_operations debug_##name##_fops = { \
Stephen Boyd234e3402012-04-05 14:25:11 -0700245 .open = simple_open, \
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700246 .read = debug_read_##name, \
Arnd Bergmannc0b0aca2010-07-06 19:16:33 +0200247 .llseek = generic_file_llseek, \
Suman Anna5b39a372015-07-20 17:33:28 -0500248 }
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700249
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700250DEBUG_FOPS_RO(regs);
Salva Peiróe203db22015-07-23 14:26:19 +0200251DEBUG_SEQ_FOPS_RO(tlb);
Suman Anna9c83e9f2014-10-22 17:22:35 -0500252DEBUG_SEQ_FOPS_RO(pagetable);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700253
254#define __DEBUG_ADD_FILE(attr, mode) \
255 { \
256 struct dentry *dent; \
Suman Anna61c75352014-10-22 17:22:30 -0500257 dent = debugfs_create_file(#attr, mode, obj->debug_dir, \
258 obj, &debug_##attr##_fops); \
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700259 if (!dent) \
Suman Anna61c75352014-10-22 17:22:30 -0500260 goto err; \
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700261 }
262
Joe Perchesff3a2b72014-02-25 15:01:40 -0800263#define DEBUG_ADD_FILE_RO(name) __DEBUG_ADD_FILE(name, 0400)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700264
Suman Anna61c75352014-10-22 17:22:30 -0500265void omap_iommu_debugfs_add(struct omap_iommu *obj)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700266{
Suman Anna61c75352014-10-22 17:22:30 -0500267 struct dentry *d;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700268
Suman Anna61c75352014-10-22 17:22:30 -0500269 if (!iommu_debug_root)
270 return;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700271
Suman Anna61c75352014-10-22 17:22:30 -0500272 obj->debug_dir = debugfs_create_dir(obj->name, iommu_debug_root);
273 if (!obj->debug_dir)
274 return;
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200275
Suman Anna61c75352014-10-22 17:22:30 -0500276 d = debugfs_create_u8("nr_tlb_entries", 0400, obj->debug_dir,
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700277 (u8 *)&obj->nr_tlb_entries);
278 if (!d)
Suman Anna61c75352014-10-22 17:22:30 -0500279 return;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700280
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700281 DEBUG_ADD_FILE_RO(regs);
282 DEBUG_ADD_FILE_RO(tlb);
Suman Anna3ca5db072014-10-22 17:22:29 -0500283 DEBUG_ADD_FILE_RO(pagetable);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700284
Suman Anna61c75352014-10-22 17:22:30 -0500285 return;
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200286
Suman Anna61c75352014-10-22 17:22:30 -0500287err:
288 debugfs_remove_recursive(obj->debug_dir);
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200289}
290
Suman Anna61c75352014-10-22 17:22:30 -0500291void omap_iommu_debugfs_remove(struct omap_iommu *obj)
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200292{
Suman Anna61c75352014-10-22 17:22:30 -0500293 if (!obj->debug_dir)
294 return;
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200295
Suman Anna61c75352014-10-22 17:22:30 -0500296 debugfs_remove_recursive(obj->debug_dir);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700297}
298
Suman Anna61c75352014-10-22 17:22:30 -0500299void __init omap_iommu_debugfs_init(void)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700300{
Suman Anna61c75352014-10-22 17:22:30 -0500301 iommu_debug_root = debugfs_create_dir("omap_iommu", NULL);
302 if (!iommu_debug_root)
303 pr_err("can't create debugfs dir\n");
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700304}
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700305
Suman Anna61c75352014-10-22 17:22:30 -0500306void __exit omap_iommu_debugfs_exit(void)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700307{
Suman Anna61c75352014-10-22 17:22:30 -0500308 debugfs_remove(iommu_debug_root);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700309}