Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 2 | /* |
| 3 | * omap iommu: debugfs interface |
| 4 | * |
| 5 | * Copyright (C) 2008-2009 Nokia Corporation |
| 6 | * |
| 7 | * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com> |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/err.h> |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 11 | #include <linux/io.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 13 | #include <linux/uaccess.h> |
Suman Anna | 69c2c19 | 2015-07-20 17:33:25 -0500 | [diff] [blame] | 14 | #include <linux/pm_runtime.h> |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 15 | #include <linux/debugfs.h> |
Tony Lindgren | 2ab7c84 | 2012-11-02 12:24:14 -0700 | [diff] [blame] | 16 | #include <linux/platform_data/iommu-omap.h> |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 17 | |
Ido Yariv | 2f7702a | 2012-11-02 12:24:00 -0700 | [diff] [blame] | 18 | #include "omap-iopgtable.h" |
Tony Lindgren | ed1c7de | 2012-11-02 12:24:06 -0700 | [diff] [blame] | 19 | #include "omap-iommu.h" |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 20 | |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 21 | static DEFINE_MUTEX(iommu_debug_lock); |
| 22 | |
| 23 | static struct dentry *iommu_debug_root; |
| 24 | |
Suman Anna | c5cf5c5 | 2014-10-22 17:22:34 -0500 | [diff] [blame] | 25 | static inline bool is_omap_iommu_detached(struct omap_iommu *obj) |
| 26 | { |
| 27 | return !obj->domain; |
| 28 | } |
| 29 | |
Suman Anna | 69c2c19 | 2015-07-20 17:33:25 -0500 | [diff] [blame] | 30 | #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 | |
| 43 | static ssize_t |
| 44 | omap2_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); |
| 64 | out: |
| 65 | return p - buf; |
| 66 | } |
| 67 | |
| 68 | static 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 DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 83 | static ssize_t debug_read_regs(struct file *file, char __user *userbuf, |
| 84 | size_t count, loff_t *ppos) |
| 85 | { |
Suman Anna | 61c7535 | 2014-10-22 17:22:30 -0500 | [diff] [blame] | 86 | struct omap_iommu *obj = file->private_data; |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 87 | char *p, *buf; |
| 88 | ssize_t bytes; |
| 89 | |
Suman Anna | c5cf5c5 | 2014-10-22 17:22:34 -0500 | [diff] [blame] | 90 | if (is_omap_iommu_detached(obj)) |
| 91 | return -EPERM; |
| 92 | |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 93 | buf = kmalloc(count, GFP_KERNEL); |
| 94 | if (!buf) |
| 95 | return -ENOMEM; |
| 96 | p = buf; |
| 97 | |
| 98 | mutex_lock(&iommu_debug_lock); |
| 99 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 100 | bytes = omap_iommu_dump_ctx(obj, p, count); |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 101 | bytes = simple_read_from_buffer(userbuf, count, ppos, buf, bytes); |
| 102 | |
| 103 | mutex_unlock(&iommu_debug_lock); |
| 104 | kfree(buf); |
| 105 | |
| 106 | return bytes; |
| 107 | } |
| 108 | |
Suman Anna | 69c2c19 | 2015-07-20 17:33:25 -0500 | [diff] [blame] | 109 | static int |
| 110 | __dump_tlb_entries(struct omap_iommu *obj, struct cr_regs *crs, int num) |
| 111 | { |
| 112 | int i; |
| 113 | struct iotlb_lock saved; |
| 114 | struct cr_regs tmp; |
| 115 | struct cr_regs *p = crs; |
| 116 | |
| 117 | pm_runtime_get_sync(obj->dev); |
| 118 | iotlb_lock_get(obj, &saved); |
| 119 | |
| 120 | for_each_iotlb_cr(obj, num, i, tmp) { |
| 121 | if (!iotlb_cr_valid(&tmp)) |
| 122 | continue; |
| 123 | *p++ = tmp; |
| 124 | } |
| 125 | |
| 126 | iotlb_lock_set(obj, &saved); |
| 127 | pm_runtime_put_sync(obj->dev); |
| 128 | |
| 129 | return p - crs; |
| 130 | } |
| 131 | |
| 132 | static ssize_t iotlb_dump_cr(struct omap_iommu *obj, struct cr_regs *cr, |
Salva Peiró | e203db2 | 2015-07-23 14:26:19 +0200 | [diff] [blame] | 133 | struct seq_file *s) |
Suman Anna | 69c2c19 | 2015-07-20 17:33:25 -0500 | [diff] [blame] | 134 | { |
Joe Perches | 6798a8c | 2015-09-11 13:07:48 -0700 | [diff] [blame] | 135 | seq_printf(s, "%08x %08x %01x\n", cr->cam, cr->ram, |
Suman Anna | a5c0e0b | 2016-04-04 17:46:21 -0500 | [diff] [blame] | 136 | (cr->cam & MMU_CAM_P) ? 1 : 0); |
Joe Perches | 6798a8c | 2015-09-11 13:07:48 -0700 | [diff] [blame] | 137 | return 0; |
Suman Anna | 69c2c19 | 2015-07-20 17:33:25 -0500 | [diff] [blame] | 138 | } |
| 139 | |
Salva Peiró | e203db2 | 2015-07-23 14:26:19 +0200 | [diff] [blame] | 140 | static size_t omap_dump_tlb_entries(struct omap_iommu *obj, struct seq_file *s) |
Suman Anna | 69c2c19 | 2015-07-20 17:33:25 -0500 | [diff] [blame] | 141 | { |
| 142 | int i, num; |
| 143 | struct cr_regs *cr; |
Suman Anna | 69c2c19 | 2015-07-20 17:33:25 -0500 | [diff] [blame] | 144 | |
Salva Peiró | e203db2 | 2015-07-23 14:26:19 +0200 | [diff] [blame] | 145 | num = obj->nr_tlb_entries; |
Suman Anna | 69c2c19 | 2015-07-20 17:33:25 -0500 | [diff] [blame] | 146 | |
| 147 | cr = kcalloc(num, sizeof(*cr), GFP_KERNEL); |
| 148 | if (!cr) |
| 149 | return 0; |
| 150 | |
| 151 | num = __dump_tlb_entries(obj, cr, num); |
| 152 | for (i = 0; i < num; i++) |
Salva Peiró | e203db2 | 2015-07-23 14:26:19 +0200 | [diff] [blame] | 153 | iotlb_dump_cr(obj, cr + i, s); |
Suman Anna | 69c2c19 | 2015-07-20 17:33:25 -0500 | [diff] [blame] | 154 | kfree(cr); |
| 155 | |
Salva Peiró | e203db2 | 2015-07-23 14:26:19 +0200 | [diff] [blame] | 156 | return 0; |
Suman Anna | 69c2c19 | 2015-07-20 17:33:25 -0500 | [diff] [blame] | 157 | } |
| 158 | |
Yangtao Li | a6906a8 | 2018-11-22 08:54:43 -0500 | [diff] [blame] | 159 | static int tlb_show(struct seq_file *s, void *data) |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 160 | { |
Salva Peiró | e203db2 | 2015-07-23 14:26:19 +0200 | [diff] [blame] | 161 | struct omap_iommu *obj = s->private; |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 162 | |
Suman Anna | c5cf5c5 | 2014-10-22 17:22:34 -0500 | [diff] [blame] | 163 | if (is_omap_iommu_detached(obj)) |
| 164 | return -EPERM; |
| 165 | |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 166 | mutex_lock(&iommu_debug_lock); |
| 167 | |
Salva Peiró | e203db2 | 2015-07-23 14:26:19 +0200 | [diff] [blame] | 168 | seq_printf(s, "%8s %8s\n", "cam:", "ram:"); |
| 169 | seq_puts(s, "-----------------------------------------\n"); |
| 170 | omap_dump_tlb_entries(obj, s); |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 171 | |
| 172 | mutex_unlock(&iommu_debug_lock); |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 173 | |
Salva Peiró | e203db2 | 2015-07-23 14:26:19 +0200 | [diff] [blame] | 174 | return 0; |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Suman Anna | 9c83e9f | 2014-10-22 17:22:35 -0500 | [diff] [blame] | 177 | static void dump_ioptable(struct seq_file *s) |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 178 | { |
Suman Anna | 9c83e9f | 2014-10-22 17:22:35 -0500 | [diff] [blame] | 179 | int i, j; |
| 180 | u32 da; |
| 181 | u32 *iopgd, *iopte; |
| 182 | struct omap_iommu *obj = s->private; |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 183 | |
| 184 | spin_lock(&obj->page_table_lock); |
| 185 | |
| 186 | iopgd = iopgd_offset(obj, 0); |
| 187 | for (i = 0; i < PTRS_PER_IOPGD; i++, iopgd++) { |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 188 | if (!*iopgd) |
| 189 | continue; |
| 190 | |
| 191 | if (!(*iopgd & IOPGD_TABLE)) { |
| 192 | da = i << IOPGD_SHIFT; |
Suman Anna | 9c83e9f | 2014-10-22 17:22:35 -0500 | [diff] [blame] | 193 | seq_printf(s, "1: 0x%08x 0x%08x\n", da, *iopgd); |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 194 | continue; |
| 195 | } |
| 196 | |
| 197 | iopte = iopte_offset(iopgd, 0); |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 198 | for (j = 0; j < PTRS_PER_IOPTE; j++, iopte++) { |
| 199 | if (!*iopte) |
| 200 | continue; |
| 201 | |
| 202 | da = (i << IOPGD_SHIFT) + (j << IOPTE_SHIFT); |
Suman Anna | 9c83e9f | 2014-10-22 17:22:35 -0500 | [diff] [blame] | 203 | seq_printf(s, "2: 0x%08x 0x%08x\n", da, *iopte); |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 206 | |
Suman Anna | 9c83e9f | 2014-10-22 17:22:35 -0500 | [diff] [blame] | 207 | spin_unlock(&obj->page_table_lock); |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Yangtao Li | a6906a8 | 2018-11-22 08:54:43 -0500 | [diff] [blame] | 210 | static int pagetable_show(struct seq_file *s, void *data) |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 211 | { |
Suman Anna | 9c83e9f | 2014-10-22 17:22:35 -0500 | [diff] [blame] | 212 | struct omap_iommu *obj = s->private; |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 213 | |
Suman Anna | c5cf5c5 | 2014-10-22 17:22:34 -0500 | [diff] [blame] | 214 | if (is_omap_iommu_detached(obj)) |
| 215 | return -EPERM; |
| 216 | |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 217 | mutex_lock(&iommu_debug_lock); |
| 218 | |
Suman Anna | 9c83e9f | 2014-10-22 17:22:35 -0500 | [diff] [blame] | 219 | seq_printf(s, "L: %8s %8s\n", "da:", "pte:"); |
| 220 | seq_puts(s, "--------------------------\n"); |
| 221 | dump_ioptable(s); |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 222 | |
| 223 | mutex_unlock(&iommu_debug_lock); |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 224 | |
Suman Anna | 9c83e9f | 2014-10-22 17:22:35 -0500 | [diff] [blame] | 225 | return 0; |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 228 | #define DEBUG_FOPS_RO(name) \ |
Yangtao Li | a6906a8 | 2018-11-22 08:54:43 -0500 | [diff] [blame] | 229 | static const struct file_operations name##_fops = { \ |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 230 | .open = simple_open, \ |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 231 | .read = debug_read_##name, \ |
Arnd Bergmann | c0b0aca | 2010-07-06 19:16:33 +0200 | [diff] [blame] | 232 | .llseek = generic_file_llseek, \ |
Suman Anna | 5b39a37 | 2015-07-20 17:33:28 -0500 | [diff] [blame] | 233 | } |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 234 | |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 235 | DEBUG_FOPS_RO(regs); |
Yangtao Li | a6906a8 | 2018-11-22 08:54:43 -0500 | [diff] [blame] | 236 | DEFINE_SHOW_ATTRIBUTE(tlb); |
| 237 | DEFINE_SHOW_ATTRIBUTE(pagetable); |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 238 | |
Suman Anna | 61c7535 | 2014-10-22 17:22:30 -0500 | [diff] [blame] | 239 | void omap_iommu_debugfs_add(struct omap_iommu *obj) |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 240 | { |
Suman Anna | 61c7535 | 2014-10-22 17:22:30 -0500 | [diff] [blame] | 241 | struct dentry *d; |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 242 | |
Suman Anna | 61c7535 | 2014-10-22 17:22:30 -0500 | [diff] [blame] | 243 | if (!iommu_debug_root) |
| 244 | return; |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 245 | |
Greg Kroah-Hartman | 9378bfe | 2019-07-04 16:36:49 +0200 | [diff] [blame] | 246 | d = debugfs_create_dir(obj->name, iommu_debug_root); |
| 247 | obj->debug_dir = d; |
Ohad Ben-Cohen | 46451d6 | 2012-02-22 10:52:51 +0200 | [diff] [blame] | 248 | |
Greg Kroah-Hartman | 9378bfe | 2019-07-04 16:36:49 +0200 | [diff] [blame] | 249 | debugfs_create_u32("nr_tlb_entries", 0400, d, &obj->nr_tlb_entries); |
| 250 | debugfs_create_file("regs", 0400, d, obj, ®s_fops); |
| 251 | debugfs_create_file("tlb", 0400, d, obj, &tlb_fops); |
| 252 | debugfs_create_file("pagetable", 0400, d, obj, &pagetable_fops); |
Ohad Ben-Cohen | 46451d6 | 2012-02-22 10:52:51 +0200 | [diff] [blame] | 253 | } |
| 254 | |
Suman Anna | 61c7535 | 2014-10-22 17:22:30 -0500 | [diff] [blame] | 255 | void omap_iommu_debugfs_remove(struct omap_iommu *obj) |
Ohad Ben-Cohen | 46451d6 | 2012-02-22 10:52:51 +0200 | [diff] [blame] | 256 | { |
Suman Anna | 61c7535 | 2014-10-22 17:22:30 -0500 | [diff] [blame] | 257 | if (!obj->debug_dir) |
| 258 | return; |
Ohad Ben-Cohen | 46451d6 | 2012-02-22 10:52:51 +0200 | [diff] [blame] | 259 | |
Suman Anna | 61c7535 | 2014-10-22 17:22:30 -0500 | [diff] [blame] | 260 | debugfs_remove_recursive(obj->debug_dir); |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Suman Anna | 61c7535 | 2014-10-22 17:22:30 -0500 | [diff] [blame] | 263 | void __init omap_iommu_debugfs_init(void) |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 264 | { |
Suman Anna | 61c7535 | 2014-10-22 17:22:30 -0500 | [diff] [blame] | 265 | iommu_debug_root = debugfs_create_dir("omap_iommu", NULL); |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 266 | } |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 267 | |
Suman Anna | 61c7535 | 2014-10-22 17:22:30 -0500 | [diff] [blame] | 268 | void __exit omap_iommu_debugfs_exit(void) |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 269 | { |
Suman Anna | 61c7535 | 2014-10-22 17:22:30 -0500 | [diff] [blame] | 270 | debugfs_remove(iommu_debug_root); |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 271 | } |