omap-iommu-debug.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  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. #include <linux/err.h>
  13. #include <linux/io.h>
  14. #include <linux/slab.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/platform_data/iommu-omap.h>
  19. #include "omap-iopgtable.h"
  20. #include "omap-iommu.h"
  21. static DEFINE_MUTEX(iommu_debug_lock);
  22. static struct dentry *iommu_debug_root;
  23. static inline bool is_omap_iommu_detached(struct omap_iommu *obj)
  24. {
  25. return !obj->domain;
  26. }
  27. #define pr_reg(name) \
  28. do { \
  29. ssize_t bytes; \
  30. const char *str = "%20s: %08x\n"; \
  31. const int maxcol = 32; \
  32. bytes = snprintf(p, maxcol, str, __stringify(name), \
  33. iommu_read_reg(obj, MMU_##name)); \
  34. p += bytes; \
  35. len -= bytes; \
  36. if (len < maxcol) \
  37. goto out; \
  38. } while (0)
  39. static ssize_t
  40. omap2_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t len)
  41. {
  42. char *p = buf;
  43. pr_reg(REVISION);
  44. pr_reg(IRQSTATUS);
  45. pr_reg(IRQENABLE);
  46. pr_reg(WALKING_ST);
  47. pr_reg(CNTL);
  48. pr_reg(FAULT_AD);
  49. pr_reg(TTB);
  50. pr_reg(LOCK);
  51. pr_reg(LD_TLB);
  52. pr_reg(CAM);
  53. pr_reg(RAM);
  54. pr_reg(GFLUSH);
  55. pr_reg(FLUSH_ENTRY);
  56. pr_reg(READ_CAM);
  57. pr_reg(READ_RAM);
  58. pr_reg(EMU_FAULT_AD);
  59. out:
  60. return p - buf;
  61. }
  62. static ssize_t omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf,
  63. ssize_t bytes)
  64. {
  65. if (!obj || !buf)
  66. return -EINVAL;
  67. pm_runtime_get_sync(obj->dev);
  68. bytes = omap2_iommu_dump_ctx(obj, buf, bytes);
  69. pm_runtime_put_sync(obj->dev);
  70. return bytes;
  71. }
  72. static ssize_t debug_read_regs(struct file *file, char __user *userbuf,
  73. size_t count, loff_t *ppos)
  74. {
  75. struct omap_iommu *obj = file->private_data;
  76. char *p, *buf;
  77. ssize_t bytes;
  78. if (is_omap_iommu_detached(obj))
  79. return -EPERM;
  80. buf = kmalloc(count, GFP_KERNEL);
  81. if (!buf)
  82. return -ENOMEM;
  83. p = buf;
  84. mutex_lock(&iommu_debug_lock);
  85. bytes = omap_iommu_dump_ctx(obj, p, count);
  86. bytes = simple_read_from_buffer(userbuf, count, ppos, buf, bytes);
  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 debug_read_tlb(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 debug_read_pagetable(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_SEQ_FOPS_RO(name) \
  181. static int debug_open_##name(struct inode *inode, struct file *file) \
  182. { \
  183. return single_open(file, debug_read_##name, inode->i_private); \
  184. } \
  185. \
  186. static const struct file_operations debug_##name##_fops = { \
  187. .open = debug_open_##name, \
  188. .read = seq_read, \
  189. .llseek = seq_lseek, \
  190. .release = single_release, \
  191. }
  192. #define DEBUG_FOPS_RO(name) \
  193. static const struct file_operations debug_##name##_fops = { \
  194. .open = simple_open, \
  195. .read = debug_read_##name, \
  196. .llseek = generic_file_llseek, \
  197. }
  198. DEBUG_FOPS_RO(regs);
  199. DEBUG_SEQ_FOPS_RO(tlb);
  200. DEBUG_SEQ_FOPS_RO(pagetable);
  201. #define __DEBUG_ADD_FILE(attr, mode) \
  202. { \
  203. struct dentry *dent; \
  204. dent = debugfs_create_file(#attr, mode, obj->debug_dir, \
  205. obj, &debug_##attr##_fops); \
  206. if (!dent) \
  207. goto err; \
  208. }
  209. #define DEBUG_ADD_FILE_RO(name) __DEBUG_ADD_FILE(name, 0400)
  210. void omap_iommu_debugfs_add(struct omap_iommu *obj)
  211. {
  212. struct dentry *d;
  213. if (!iommu_debug_root)
  214. return;
  215. obj->debug_dir = debugfs_create_dir(obj->name, iommu_debug_root);
  216. if (!obj->debug_dir)
  217. return;
  218. d = debugfs_create_u8("nr_tlb_entries", 0400, obj->debug_dir,
  219. (u8 *)&obj->nr_tlb_entries);
  220. if (!d)
  221. return;
  222. DEBUG_ADD_FILE_RO(regs);
  223. DEBUG_ADD_FILE_RO(tlb);
  224. DEBUG_ADD_FILE_RO(pagetable);
  225. return;
  226. err:
  227. debugfs_remove_recursive(obj->debug_dir);
  228. }
  229. void omap_iommu_debugfs_remove(struct omap_iommu *obj)
  230. {
  231. if (!obj->debug_dir)
  232. return;
  233. debugfs_remove_recursive(obj->debug_dir);
  234. }
  235. void __init omap_iommu_debugfs_init(void)
  236. {
  237. iommu_debug_root = debugfs_create_dir("omap_iommu", NULL);
  238. if (!iommu_debug_root)
  239. pr_err("can't create debugfs dir\n");
  240. }
  241. void __exit omap_iommu_debugfs_exit(void)
  242. {
  243. debugfs_remove(iommu_debug_root);
  244. }