0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/err.h>
0011 #include <linux/io.h>
0012 #include <linux/slab.h>
0013 #include <linux/uaccess.h>
0014 #include <linux/pm_runtime.h>
0015 #include <linux/debugfs.h>
0016 #include <linux/platform_data/iommu-omap.h>
0017
0018 #include "omap-iopgtable.h"
0019 #include "omap-iommu.h"
0020
0021 static DEFINE_MUTEX(iommu_debug_lock);
0022
0023 static struct dentry *iommu_debug_root;
0024
0025 static inline bool is_omap_iommu_detached(struct omap_iommu *obj)
0026 {
0027 return !obj->domain;
0028 }
0029
0030 #define pr_reg(name) \
0031 do { \
0032 ssize_t bytes; \
0033 const char *str = "%20s: %08x\n"; \
0034 const int maxcol = 32; \
0035 bytes = snprintf(p, maxcol, str, __stringify(name), \
0036 iommu_read_reg(obj, MMU_##name)); \
0037 p += bytes; \
0038 len -= bytes; \
0039 if (len < maxcol) \
0040 goto out; \
0041 } while (0)
0042
0043 static ssize_t
0044 omap2_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t len)
0045 {
0046 char *p = buf;
0047
0048 pr_reg(REVISION);
0049 pr_reg(IRQSTATUS);
0050 pr_reg(IRQENABLE);
0051 pr_reg(WALKING_ST);
0052 pr_reg(CNTL);
0053 pr_reg(FAULT_AD);
0054 pr_reg(TTB);
0055 pr_reg(LOCK);
0056 pr_reg(LD_TLB);
0057 pr_reg(CAM);
0058 pr_reg(RAM);
0059 pr_reg(GFLUSH);
0060 pr_reg(FLUSH_ENTRY);
0061 pr_reg(READ_CAM);
0062 pr_reg(READ_RAM);
0063 pr_reg(EMU_FAULT_AD);
0064 out:
0065 return p - buf;
0066 }
0067
0068 static ssize_t omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf,
0069 ssize_t bytes)
0070 {
0071 if (!obj || !buf)
0072 return -EINVAL;
0073
0074 pm_runtime_get_sync(obj->dev);
0075
0076 bytes = omap2_iommu_dump_ctx(obj, buf, bytes);
0077
0078 pm_runtime_put_sync(obj->dev);
0079
0080 return bytes;
0081 }
0082
0083 static ssize_t debug_read_regs(struct file *file, char __user *userbuf,
0084 size_t count, loff_t *ppos)
0085 {
0086 struct omap_iommu *obj = file->private_data;
0087 char *p, *buf;
0088 ssize_t bytes;
0089
0090 if (is_omap_iommu_detached(obj))
0091 return -EPERM;
0092
0093 buf = kmalloc(count, GFP_KERNEL);
0094 if (!buf)
0095 return -ENOMEM;
0096 p = buf;
0097
0098 mutex_lock(&iommu_debug_lock);
0099
0100 bytes = omap_iommu_dump_ctx(obj, p, count);
0101 if (bytes < 0)
0102 goto err;
0103 bytes = simple_read_from_buffer(userbuf, count, ppos, buf, bytes);
0104
0105 err:
0106 mutex_unlock(&iommu_debug_lock);
0107 kfree(buf);
0108
0109 return bytes;
0110 }
0111
0112 static int
0113 __dump_tlb_entries(struct omap_iommu *obj, struct cr_regs *crs, int num)
0114 {
0115 int i;
0116 struct iotlb_lock saved;
0117 struct cr_regs tmp;
0118 struct cr_regs *p = crs;
0119
0120 pm_runtime_get_sync(obj->dev);
0121 iotlb_lock_get(obj, &saved);
0122
0123 for_each_iotlb_cr(obj, num, i, tmp) {
0124 if (!iotlb_cr_valid(&tmp))
0125 continue;
0126 *p++ = tmp;
0127 }
0128
0129 iotlb_lock_set(obj, &saved);
0130 pm_runtime_put_sync(obj->dev);
0131
0132 return p - crs;
0133 }
0134
0135 static ssize_t iotlb_dump_cr(struct omap_iommu *obj, struct cr_regs *cr,
0136 struct seq_file *s)
0137 {
0138 seq_printf(s, "%08x %08x %01x\n", cr->cam, cr->ram,
0139 (cr->cam & MMU_CAM_P) ? 1 : 0);
0140 return 0;
0141 }
0142
0143 static size_t omap_dump_tlb_entries(struct omap_iommu *obj, struct seq_file *s)
0144 {
0145 int i, num;
0146 struct cr_regs *cr;
0147
0148 num = obj->nr_tlb_entries;
0149
0150 cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
0151 if (!cr)
0152 return 0;
0153
0154 num = __dump_tlb_entries(obj, cr, num);
0155 for (i = 0; i < num; i++)
0156 iotlb_dump_cr(obj, cr + i, s);
0157 kfree(cr);
0158
0159 return 0;
0160 }
0161
0162 static int tlb_show(struct seq_file *s, void *data)
0163 {
0164 struct omap_iommu *obj = s->private;
0165
0166 if (is_omap_iommu_detached(obj))
0167 return -EPERM;
0168
0169 mutex_lock(&iommu_debug_lock);
0170
0171 seq_printf(s, "%8s %8s\n", "cam:", "ram:");
0172 seq_puts(s, "-----------------------------------------\n");
0173 omap_dump_tlb_entries(obj, s);
0174
0175 mutex_unlock(&iommu_debug_lock);
0176
0177 return 0;
0178 }
0179
0180 static void dump_ioptable(struct seq_file *s)
0181 {
0182 int i, j;
0183 u32 da;
0184 u32 *iopgd, *iopte;
0185 struct omap_iommu *obj = s->private;
0186
0187 spin_lock(&obj->page_table_lock);
0188
0189 iopgd = iopgd_offset(obj, 0);
0190 for (i = 0; i < PTRS_PER_IOPGD; i++, iopgd++) {
0191 if (!*iopgd)
0192 continue;
0193
0194 if (!(*iopgd & IOPGD_TABLE)) {
0195 da = i << IOPGD_SHIFT;
0196 seq_printf(s, "1: 0x%08x 0x%08x\n", da, *iopgd);
0197 continue;
0198 }
0199
0200 iopte = iopte_offset(iopgd, 0);
0201 for (j = 0; j < PTRS_PER_IOPTE; j++, iopte++) {
0202 if (!*iopte)
0203 continue;
0204
0205 da = (i << IOPGD_SHIFT) + (j << IOPTE_SHIFT);
0206 seq_printf(s, "2: 0x%08x 0x%08x\n", da, *iopte);
0207 }
0208 }
0209
0210 spin_unlock(&obj->page_table_lock);
0211 }
0212
0213 static int pagetable_show(struct seq_file *s, void *data)
0214 {
0215 struct omap_iommu *obj = s->private;
0216
0217 if (is_omap_iommu_detached(obj))
0218 return -EPERM;
0219
0220 mutex_lock(&iommu_debug_lock);
0221
0222 seq_printf(s, "L: %8s %8s\n", "da:", "pte:");
0223 seq_puts(s, "--------------------------\n");
0224 dump_ioptable(s);
0225
0226 mutex_unlock(&iommu_debug_lock);
0227
0228 return 0;
0229 }
0230
0231 #define DEBUG_FOPS_RO(name) \
0232 static const struct file_operations name##_fops = { \
0233 .open = simple_open, \
0234 .read = debug_read_##name, \
0235 .llseek = generic_file_llseek, \
0236 }
0237
0238 DEBUG_FOPS_RO(regs);
0239 DEFINE_SHOW_ATTRIBUTE(tlb);
0240 DEFINE_SHOW_ATTRIBUTE(pagetable);
0241
0242 void omap_iommu_debugfs_add(struct omap_iommu *obj)
0243 {
0244 struct dentry *d;
0245
0246 if (!iommu_debug_root)
0247 return;
0248
0249 d = debugfs_create_dir(obj->name, iommu_debug_root);
0250 obj->debug_dir = d;
0251
0252 debugfs_create_u32("nr_tlb_entries", 0400, d, &obj->nr_tlb_entries);
0253 debugfs_create_file("regs", 0400, d, obj, ®s_fops);
0254 debugfs_create_file("tlb", 0400, d, obj, &tlb_fops);
0255 debugfs_create_file("pagetable", 0400, d, obj, &pagetable_fops);
0256 }
0257
0258 void omap_iommu_debugfs_remove(struct omap_iommu *obj)
0259 {
0260 if (!obj->debug_dir)
0261 return;
0262
0263 debugfs_remove_recursive(obj->debug_dir);
0264 }
0265
0266 void __init omap_iommu_debugfs_init(void)
0267 {
0268 iommu_debug_root = debugfs_create_dir("omap_iommu", NULL);
0269 }
0270
0271 void __exit omap_iommu_debugfs_exit(void)
0272 {
0273 debugfs_remove(iommu_debug_root);
0274 }