omap-iommu-debug.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. return seq_printf(s, "%08x %08x %01x\n", cr->cam, cr->ram,
  113. (cr->cam & MMU_CAM_P) ? 1 : 0);
  114. }
  115. static size_t omap_dump_tlb_entries(struct omap_iommu *obj, struct seq_file *s)
  116. {
  117. int i, num;
  118. struct cr_regs *cr;
  119. num = obj->nr_tlb_entries;
  120. cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
  121. if (!cr)
  122. return 0;
  123. num = __dump_tlb_entries(obj, cr, num);
  124. for (i = 0; i < num; i++)
  125. iotlb_dump_cr(obj, cr + i, s);
  126. kfree(cr);
  127. return 0;
  128. }
  129. static int debug_read_tlb(struct seq_file *s, void *data)
  130. {
  131. struct omap_iommu *obj = s->private;
  132. if (is_omap_iommu_detached(obj))
  133. return -EPERM;
  134. mutex_lock(&iommu_debug_lock);
  135. seq_printf(s, "%8s %8s\n", "cam:", "ram:");
  136. seq_puts(s, "-----------------------------------------\n");
  137. omap_dump_tlb_entries(obj, s);
  138. mutex_unlock(&iommu_debug_lock);
  139. return 0;
  140. }
  141. static void dump_ioptable(struct seq_file *s)
  142. {
  143. int i, j;
  144. u32 da;
  145. u32 *iopgd, *iopte;
  146. struct omap_iommu *obj = s->private;
  147. spin_lock(&obj->page_table_lock);
  148. iopgd = iopgd_offset(obj, 0);
  149. for (i = 0; i < PTRS_PER_IOPGD; i++, iopgd++) {
  150. if (!*iopgd)
  151. continue;
  152. if (!(*iopgd & IOPGD_TABLE)) {
  153. da = i << IOPGD_SHIFT;
  154. seq_printf(s, "1: 0x%08x 0x%08x\n", da, *iopgd);
  155. continue;
  156. }
  157. iopte = iopte_offset(iopgd, 0);
  158. for (j = 0; j < PTRS_PER_IOPTE; j++, iopte++) {
  159. if (!*iopte)
  160. continue;
  161. da = (i << IOPGD_SHIFT) + (j << IOPTE_SHIFT);
  162. seq_printf(s, "2: 0x%08x 0x%08x\n", da, *iopte);
  163. }
  164. }
  165. spin_unlock(&obj->page_table_lock);
  166. }
  167. static int debug_read_pagetable(struct seq_file *s, void *data)
  168. {
  169. struct omap_iommu *obj = s->private;
  170. if (is_omap_iommu_detached(obj))
  171. return -EPERM;
  172. mutex_lock(&iommu_debug_lock);
  173. seq_printf(s, "L: %8s %8s\n", "da:", "pte:");
  174. seq_puts(s, "--------------------------\n");
  175. dump_ioptable(s);
  176. mutex_unlock(&iommu_debug_lock);
  177. return 0;
  178. }
  179. #define DEBUG_SEQ_FOPS_RO(name) \
  180. static int debug_open_##name(struct inode *inode, struct file *file) \
  181. { \
  182. return single_open(file, debug_read_##name, inode->i_private); \
  183. } \
  184. \
  185. static const struct file_operations debug_##name##_fops = { \
  186. .open = debug_open_##name, \
  187. .read = seq_read, \
  188. .llseek = seq_lseek, \
  189. .release = single_release, \
  190. }
  191. #define DEBUG_FOPS_RO(name) \
  192. static const struct file_operations debug_##name##_fops = { \
  193. .open = simple_open, \
  194. .read = debug_read_##name, \
  195. .llseek = generic_file_llseek, \
  196. }
  197. DEBUG_FOPS_RO(regs);
  198. DEBUG_SEQ_FOPS_RO(tlb);
  199. DEBUG_SEQ_FOPS_RO(pagetable);
  200. #define __DEBUG_ADD_FILE(attr, mode) \
  201. { \
  202. struct dentry *dent; \
  203. dent = debugfs_create_file(#attr, mode, obj->debug_dir, \
  204. obj, &debug_##attr##_fops); \
  205. if (!dent) \
  206. goto err; \
  207. }
  208. #define DEBUG_ADD_FILE_RO(name) __DEBUG_ADD_FILE(name, 0400)
  209. void omap_iommu_debugfs_add(struct omap_iommu *obj)
  210. {
  211. struct dentry *d;
  212. if (!iommu_debug_root)
  213. return;
  214. obj->debug_dir = debugfs_create_dir(obj->name, iommu_debug_root);
  215. if (!obj->debug_dir)
  216. return;
  217. d = debugfs_create_u8("nr_tlb_entries", 0400, obj->debug_dir,
  218. (u8 *)&obj->nr_tlb_entries);
  219. if (!d)
  220. return;
  221. DEBUG_ADD_FILE_RO(regs);
  222. DEBUG_ADD_FILE_RO(tlb);
  223. DEBUG_ADD_FILE_RO(pagetable);
  224. return;
  225. err:
  226. debugfs_remove_recursive(obj->debug_dir);
  227. }
  228. void omap_iommu_debugfs_remove(struct omap_iommu *obj)
  229. {
  230. if (!obj->debug_dir)
  231. return;
  232. debugfs_remove_recursive(obj->debug_dir);
  233. }
  234. void __init omap_iommu_debugfs_init(void)
  235. {
  236. iommu_debug_root = debugfs_create_dir("omap_iommu", NULL);
  237. if (!iommu_debug_root)
  238. pr_err("can't create debugfs dir\n");
  239. }
  240. void __exit omap_iommu_debugfs_exit(void)
  241. {
  242. debugfs_remove(iommu_debug_root);
  243. }