omap-iommu-debug.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * omap iommu: debugfs interface
  4. *
  5. * Copyright (C) 2008-2009 Nokia Corporation
  6. *
  7. * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
  8. */
  9. #include <linux/err.h>
  10. #include <linux/io.h>
  11. #include <linux/slab.h>
  12. #include <linux/uaccess.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/debugfs.h>
  15. #include <linux/platform_data/iommu-omap.h>
  16. #include "omap-iopgtable.h"
  17. #include "omap-iommu.h"
  18. static DEFINE_MUTEX(iommu_debug_lock);
  19. static struct dentry *iommu_debug_root;
  20. static inline bool is_omap_iommu_detached(struct omap_iommu *obj)
  21. {
  22. return !obj->domain;
  23. }
  24. #define pr_reg(name) \
  25. do { \
  26. ssize_t bytes; \
  27. const char *str = "%20s: %08x\n"; \
  28. const int maxcol = 32; \
  29. bytes = snprintf(p, maxcol, str, __stringify(name), \
  30. iommu_read_reg(obj, MMU_##name)); \
  31. p += bytes; \
  32. len -= bytes; \
  33. if (len < maxcol) \
  34. goto out; \
  35. } while (0)
  36. static ssize_t
  37. omap2_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t len)
  38. {
  39. char *p = buf;
  40. pr_reg(REVISION);
  41. pr_reg(IRQSTATUS);
  42. pr_reg(IRQENABLE);
  43. pr_reg(WALKING_ST);
  44. pr_reg(CNTL);
  45. pr_reg(FAULT_AD);
  46. pr_reg(TTB);
  47. pr_reg(LOCK);
  48. pr_reg(LD_TLB);
  49. pr_reg(CAM);
  50. pr_reg(RAM);
  51. pr_reg(GFLUSH);
  52. pr_reg(FLUSH_ENTRY);
  53. pr_reg(READ_CAM);
  54. pr_reg(READ_RAM);
  55. pr_reg(EMU_FAULT_AD);
  56. out:
  57. return p - buf;
  58. }
  59. static ssize_t omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf,
  60. ssize_t bytes)
  61. {
  62. if (!obj || !buf)
  63. return -EINVAL;
  64. pm_runtime_get_sync(obj->dev);
  65. bytes = omap2_iommu_dump_ctx(obj, buf, bytes);
  66. pm_runtime_put_sync(obj->dev);
  67. return bytes;
  68. }
  69. static ssize_t debug_read_regs(struct file *file, char __user *userbuf,
  70. size_t count, loff_t *ppos)
  71. {
  72. struct omap_iommu *obj = file->private_data;
  73. char *p, *buf;
  74. ssize_t bytes;
  75. if (is_omap_iommu_detached(obj))
  76. return -EPERM;
  77. buf = kmalloc(count, GFP_KERNEL);
  78. if (!buf)
  79. return -ENOMEM;
  80. p = buf;
  81. mutex_lock(&iommu_debug_lock);
  82. bytes = omap_iommu_dump_ctx(obj, p, count);
  83. if (bytes < 0)
  84. goto err;
  85. bytes = simple_read_from_buffer(userbuf, count, ppos, buf, bytes);
  86. err:
  87. mutex_unlock(&iommu_debug_lock);
  88. kfree(buf);
  89. return bytes;
  90. }
  91. static int
  92. __dump_tlb_entries(struct omap_iommu *obj, struct cr_regs *crs, int num)
  93. {
  94. int i;
  95. struct iotlb_lock saved;
  96. struct cr_regs tmp;
  97. struct cr_regs *p = crs;
  98. pm_runtime_get_sync(obj->dev);
  99. iotlb_lock_get(obj, &saved);
  100. for_each_iotlb_cr(obj, num, i, tmp) {
  101. if (!iotlb_cr_valid(&tmp))
  102. continue;
  103. *p++ = tmp;
  104. }
  105. iotlb_lock_set(obj, &saved);
  106. pm_runtime_put_sync(obj->dev);
  107. return p - crs;
  108. }
  109. static ssize_t iotlb_dump_cr(struct omap_iommu *obj, struct cr_regs *cr,
  110. struct seq_file *s)
  111. {
  112. seq_printf(s, "%08x %08x %01x\n", cr->cam, cr->ram,
  113. (cr->cam & MMU_CAM_P) ? 1 : 0);
  114. return 0;
  115. }
  116. static size_t omap_dump_tlb_entries(struct omap_iommu *obj, struct seq_file *s)
  117. {
  118. int i, num;
  119. struct cr_regs *cr;
  120. num = obj->nr_tlb_entries;
  121. cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
  122. if (!cr)
  123. return 0;
  124. num = __dump_tlb_entries(obj, cr, num);
  125. for (i = 0; i < num; i++)
  126. iotlb_dump_cr(obj, cr + i, s);
  127. kfree(cr);
  128. return 0;
  129. }
  130. static int tlb_show(struct seq_file *s, void *data)
  131. {
  132. struct omap_iommu *obj = s->private;
  133. if (is_omap_iommu_detached(obj))
  134. return -EPERM;
  135. mutex_lock(&iommu_debug_lock);
  136. seq_printf(s, "%8s %8s\n", "cam:", "ram:");
  137. seq_puts(s, "-----------------------------------------\n");
  138. omap_dump_tlb_entries(obj, s);
  139. mutex_unlock(&iommu_debug_lock);
  140. return 0;
  141. }
  142. static void dump_ioptable(struct seq_file *s)
  143. {
  144. int i, j;
  145. u32 da;
  146. u32 *iopgd, *iopte;
  147. struct omap_iommu *obj = s->private;
  148. spin_lock(&obj->page_table_lock);
  149. iopgd = iopgd_offset(obj, 0);
  150. for (i = 0; i < PTRS_PER_IOPGD; i++, iopgd++) {
  151. if (!*iopgd)
  152. continue;
  153. if (!(*iopgd & IOPGD_TABLE)) {
  154. da = i << IOPGD_SHIFT;
  155. seq_printf(s, "1: 0x%08x 0x%08x\n", da, *iopgd);
  156. continue;
  157. }
  158. iopte = iopte_offset(iopgd, 0);
  159. for (j = 0; j < PTRS_PER_IOPTE; j++, iopte++) {
  160. if (!*iopte)
  161. continue;
  162. da = (i << IOPGD_SHIFT) + (j << IOPTE_SHIFT);
  163. seq_printf(s, "2: 0x%08x 0x%08x\n", da, *iopte);
  164. }
  165. }
  166. spin_unlock(&obj->page_table_lock);
  167. }
  168. static int pagetable_show(struct seq_file *s, void *data)
  169. {
  170. struct omap_iommu *obj = s->private;
  171. if (is_omap_iommu_detached(obj))
  172. return -EPERM;
  173. mutex_lock(&iommu_debug_lock);
  174. seq_printf(s, "L: %8s %8s\n", "da:", "pte:");
  175. seq_puts(s, "--------------------------\n");
  176. dump_ioptable(s);
  177. mutex_unlock(&iommu_debug_lock);
  178. return 0;
  179. }
  180. #define DEBUG_FOPS_RO(name) \
  181. static const struct file_operations name##_fops = { \
  182. .open = simple_open, \
  183. .read = debug_read_##name, \
  184. .llseek = generic_file_llseek, \
  185. }
  186. DEBUG_FOPS_RO(regs);
  187. DEFINE_SHOW_ATTRIBUTE(tlb);
  188. DEFINE_SHOW_ATTRIBUTE(pagetable);
  189. void omap_iommu_debugfs_add(struct omap_iommu *obj)
  190. {
  191. struct dentry *d;
  192. if (!iommu_debug_root)
  193. return;
  194. d = debugfs_create_dir(obj->name, iommu_debug_root);
  195. obj->debug_dir = d;
  196. debugfs_create_u32("nr_tlb_entries", 0400, d, &obj->nr_tlb_entries);
  197. debugfs_create_file("regs", 0400, d, obj, &regs_fops);
  198. debugfs_create_file("tlb", 0400, d, obj, &tlb_fops);
  199. debugfs_create_file("pagetable", 0400, d, obj, &pagetable_fops);
  200. }
  201. void omap_iommu_debugfs_remove(struct omap_iommu *obj)
  202. {
  203. if (!obj->debug_dir)
  204. return;
  205. debugfs_remove_recursive(obj->debug_dir);
  206. }
  207. void __init omap_iommu_debugfs_init(void)
  208. {
  209. iommu_debug_root = debugfs_create_dir("omap_iommu", NULL);
  210. }
  211. void __exit omap_iommu_debugfs_exit(void)
  212. {
  213. debugfs_remove(iommu_debug_root);
  214. }