blob: a99afb5d9011c5375905d54520e9851048bfbc5a [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Hiroshi DOYU14e0e672009-08-28 10:54:41 -07002/*
3 * omap iommu: debugfs interface
4 *
5 * Copyright (C) 2008-2009 Nokia Corporation
6 *
7 * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
Hiroshi DOYU14e0e672009-08-28 10:54:41 -07008 */
9
10#include <linux/err.h>
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070011#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070013#include <linux/uaccess.h>
Suman Anna69c2c192015-07-20 17:33:25 -050014#include <linux/pm_runtime.h>
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070015#include <linux/debugfs.h>
Tony Lindgren2ab7c842012-11-02 12:24:14 -070016#include <linux/platform_data/iommu-omap.h>
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070017
Ido Yariv2f7702a2012-11-02 12:24:00 -070018#include "omap-iopgtable.h"
Tony Lindgrened1c7de2012-11-02 12:24:06 -070019#include "omap-iommu.h"
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070020
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070021static DEFINE_MUTEX(iommu_debug_lock);
22
23static struct dentry *iommu_debug_root;
24
Suman Annac5cf5c52014-10-22 17:22:34 -050025static inline bool is_omap_iommu_detached(struct omap_iommu *obj)
26{
27 return !obj->domain;
28}
29
Suman Anna69c2c192015-07-20 17:33:25 -050030#define pr_reg(name) \
31 do { \
32 ssize_t bytes; \
33 const char *str = "%20s: %08x\n"; \
34 const int maxcol = 32; \
35 bytes = snprintf(p, maxcol, str, __stringify(name), \
36 iommu_read_reg(obj, MMU_##name)); \
37 p += bytes; \
38 len -= bytes; \
39 if (len < maxcol) \
40 goto out; \
41 } while (0)
42
43static ssize_t
44omap2_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t len)
45{
46 char *p = buf;
47
48 pr_reg(REVISION);
49 pr_reg(IRQSTATUS);
50 pr_reg(IRQENABLE);
51 pr_reg(WALKING_ST);
52 pr_reg(CNTL);
53 pr_reg(FAULT_AD);
54 pr_reg(TTB);
55 pr_reg(LOCK);
56 pr_reg(LD_TLB);
57 pr_reg(CAM);
58 pr_reg(RAM);
59 pr_reg(GFLUSH);
60 pr_reg(FLUSH_ENTRY);
61 pr_reg(READ_CAM);
62 pr_reg(READ_RAM);
63 pr_reg(EMU_FAULT_AD);
64out:
65 return p - buf;
66}
67
68static ssize_t omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf,
69 ssize_t bytes)
70{
71 if (!obj || !buf)
72 return -EINVAL;
73
74 pm_runtime_get_sync(obj->dev);
75
76 bytes = omap2_iommu_dump_ctx(obj, buf, bytes);
77
78 pm_runtime_put_sync(obj->dev);
79
80 return bytes;
81}
82
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070083static ssize_t debug_read_regs(struct file *file, char __user *userbuf,
84 size_t count, loff_t *ppos)
85{
Suman Anna61c75352014-10-22 17:22:30 -050086 struct omap_iommu *obj = file->private_data;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070087 char *p, *buf;
88 ssize_t bytes;
89
Suman Annac5cf5c52014-10-22 17:22:34 -050090 if (is_omap_iommu_detached(obj))
91 return -EPERM;
92
Hiroshi DOYU14e0e672009-08-28 10:54:41 -070093 buf = kmalloc(count, GFP_KERNEL);
94 if (!buf)
95 return -ENOMEM;
96 p = buf;
97
98 mutex_lock(&iommu_debug_lock);
99
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300100 bytes = omap_iommu_dump_ctx(obj, p, count);
Colin Ian Kingdee9d152020-07-14 20:22:11 +0100101 if (bytes < 0)
102 goto err;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700103 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, bytes);
104
Colin Ian Kingdee9d152020-07-14 20:22:11 +0100105err:
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700106 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{
Joe Perches6798a8c2015-09-11 13:07:48 -0700138 seq_printf(s, "%08x %08x %01x\n", cr->cam, cr->ram,
Suman Annaa5c0e0b2016-04-04 17:46:21 -0500139 (cr->cam & MMU_CAM_P) ? 1 : 0);
Joe Perches6798a8c2015-09-11 13:07:48 -0700140 return 0;
Suman Anna69c2c192015-07-20 17:33:25 -0500141}
142
Salva Peiróe203db22015-07-23 14:26:19 +0200143static size_t omap_dump_tlb_entries(struct omap_iommu *obj, struct seq_file *s)
Suman Anna69c2c192015-07-20 17:33:25 -0500144{
145 int i, num;
146 struct cr_regs *cr;
Suman Anna69c2c192015-07-20 17:33:25 -0500147
Salva Peiróe203db22015-07-23 14:26:19 +0200148 num = obj->nr_tlb_entries;
Suman Anna69c2c192015-07-20 17:33:25 -0500149
150 cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
151 if (!cr)
152 return 0;
153
154 num = __dump_tlb_entries(obj, cr, num);
155 for (i = 0; i < num; i++)
Salva Peiróe203db22015-07-23 14:26:19 +0200156 iotlb_dump_cr(obj, cr + i, s);
Suman Anna69c2c192015-07-20 17:33:25 -0500157 kfree(cr);
158
Salva Peiróe203db22015-07-23 14:26:19 +0200159 return 0;
Suman Anna69c2c192015-07-20 17:33:25 -0500160}
161
Yangtao Lia6906a82018-11-22 08:54:43 -0500162static int tlb_show(struct seq_file *s, void *data)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700163{
Salva Peiróe203db22015-07-23 14:26:19 +0200164 struct omap_iommu *obj = s->private;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700165
Suman Annac5cf5c52014-10-22 17:22:34 -0500166 if (is_omap_iommu_detached(obj))
167 return -EPERM;
168
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700169 mutex_lock(&iommu_debug_lock);
170
Salva Peiróe203db22015-07-23 14:26:19 +0200171 seq_printf(s, "%8s %8s\n", "cam:", "ram:");
172 seq_puts(s, "-----------------------------------------\n");
173 omap_dump_tlb_entries(obj, s);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700174
175 mutex_unlock(&iommu_debug_lock);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700176
Salva Peiróe203db22015-07-23 14:26:19 +0200177 return 0;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700178}
179
Suman Anna9c83e9f2014-10-22 17:22:35 -0500180static void dump_ioptable(struct seq_file *s)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700181{
Suman Anna9c83e9f2014-10-22 17:22:35 -0500182 int i, j;
183 u32 da;
184 u32 *iopgd, *iopte;
185 struct omap_iommu *obj = s->private;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700186
187 spin_lock(&obj->page_table_lock);
188
189 iopgd = iopgd_offset(obj, 0);
190 for (i = 0; i < PTRS_PER_IOPGD; i++, iopgd++) {
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700191 if (!*iopgd)
192 continue;
193
194 if (!(*iopgd & IOPGD_TABLE)) {
195 da = i << IOPGD_SHIFT;
Suman Anna9c83e9f2014-10-22 17:22:35 -0500196 seq_printf(s, "1: 0x%08x 0x%08x\n", da, *iopgd);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700197 continue;
198 }
199
200 iopte = iopte_offset(iopgd, 0);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700201 for (j = 0; j < PTRS_PER_IOPTE; j++, iopte++) {
202 if (!*iopte)
203 continue;
204
205 da = (i << IOPGD_SHIFT) + (j << IOPTE_SHIFT);
Suman Anna9c83e9f2014-10-22 17:22:35 -0500206 seq_printf(s, "2: 0x%08x 0x%08x\n", da, *iopte);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700207 }
208 }
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700209
Suman Anna9c83e9f2014-10-22 17:22:35 -0500210 spin_unlock(&obj->page_table_lock);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700211}
212
Yangtao Lia6906a82018-11-22 08:54:43 -0500213static int pagetable_show(struct seq_file *s, void *data)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700214{
Suman Anna9c83e9f2014-10-22 17:22:35 -0500215 struct omap_iommu *obj = s->private;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700216
Suman Annac5cf5c52014-10-22 17:22:34 -0500217 if (is_omap_iommu_detached(obj))
218 return -EPERM;
219
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700220 mutex_lock(&iommu_debug_lock);
221
Suman Anna9c83e9f2014-10-22 17:22:35 -0500222 seq_printf(s, "L: %8s %8s\n", "da:", "pte:");
223 seq_puts(s, "--------------------------\n");
224 dump_ioptable(s);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700225
226 mutex_unlock(&iommu_debug_lock);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700227
Suman Anna9c83e9f2014-10-22 17:22:35 -0500228 return 0;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700229}
230
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700231#define DEBUG_FOPS_RO(name) \
Yangtao Lia6906a82018-11-22 08:54:43 -0500232 static const struct file_operations name##_fops = { \
Stephen Boyd234e3402012-04-05 14:25:11 -0700233 .open = simple_open, \
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700234 .read = debug_read_##name, \
Arnd Bergmannc0b0aca2010-07-06 19:16:33 +0200235 .llseek = generic_file_llseek, \
Suman Anna5b39a372015-07-20 17:33:28 -0500236 }
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700237
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700238DEBUG_FOPS_RO(regs);
Yangtao Lia6906a82018-11-22 08:54:43 -0500239DEFINE_SHOW_ATTRIBUTE(tlb);
240DEFINE_SHOW_ATTRIBUTE(pagetable);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700241
Suman Anna61c75352014-10-22 17:22:30 -0500242void omap_iommu_debugfs_add(struct omap_iommu *obj)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700243{
Suman Anna61c75352014-10-22 17:22:30 -0500244 struct dentry *d;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700245
Suman Anna61c75352014-10-22 17:22:30 -0500246 if (!iommu_debug_root)
247 return;
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700248
Greg Kroah-Hartman9378bfe2019-07-04 16:36:49 +0200249 d = debugfs_create_dir(obj->name, iommu_debug_root);
250 obj->debug_dir = d;
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200251
Greg Kroah-Hartman9378bfe2019-07-04 16:36:49 +0200252 debugfs_create_u32("nr_tlb_entries", 0400, d, &obj->nr_tlb_entries);
253 debugfs_create_file("regs", 0400, d, obj, &regs_fops);
254 debugfs_create_file("tlb", 0400, d, obj, &tlb_fops);
255 debugfs_create_file("pagetable", 0400, d, obj, &pagetable_fops);
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200256}
257
Suman Anna61c75352014-10-22 17:22:30 -0500258void omap_iommu_debugfs_remove(struct omap_iommu *obj)
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200259{
Suman Anna61c75352014-10-22 17:22:30 -0500260 if (!obj->debug_dir)
261 return;
Ohad Ben-Cohen46451d62012-02-22 10:52:51 +0200262
Suman Anna61c75352014-10-22 17:22:30 -0500263 debugfs_remove_recursive(obj->debug_dir);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700264}
265
Suman Anna61c75352014-10-22 17:22:30 -0500266void __init omap_iommu_debugfs_init(void)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700267{
Suman Anna61c75352014-10-22 17:22:30 -0500268 iommu_debug_root = debugfs_create_dir("omap_iommu", NULL);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700269}
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700270
Suman Anna61c75352014-10-22 17:22:30 -0500271void __exit omap_iommu_debugfs_exit(void)
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700272{
Suman Anna61c75352014-10-22 17:22:30 -0500273 debugfs_remove(iommu_debug_root);
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700274}