omap-iommu-debug.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/debugfs.h>
  17. #include <linux/platform_data/iommu-omap.h>
  18. #include "omap-iopgtable.h"
  19. #include "omap-iommu.h"
  20. static DEFINE_MUTEX(iommu_debug_lock);
  21. static struct dentry *iommu_debug_root;
  22. static inline bool is_omap_iommu_detached(struct omap_iommu *obj)
  23. {
  24. return !obj->domain;
  25. }
  26. static ssize_t debug_read_regs(struct file *file, char __user *userbuf,
  27. size_t count, loff_t *ppos)
  28. {
  29. struct omap_iommu *obj = file->private_data;
  30. char *p, *buf;
  31. ssize_t bytes;
  32. if (is_omap_iommu_detached(obj))
  33. return -EPERM;
  34. buf = kmalloc(count, GFP_KERNEL);
  35. if (!buf)
  36. return -ENOMEM;
  37. p = buf;
  38. mutex_lock(&iommu_debug_lock);
  39. bytes = omap_iommu_dump_ctx(obj, p, count);
  40. bytes = simple_read_from_buffer(userbuf, count, ppos, buf, bytes);
  41. mutex_unlock(&iommu_debug_lock);
  42. kfree(buf);
  43. return bytes;
  44. }
  45. static ssize_t debug_read_tlb(struct file *file, char __user *userbuf,
  46. size_t count, loff_t *ppos)
  47. {
  48. struct omap_iommu *obj = file->private_data;
  49. char *p, *buf;
  50. ssize_t bytes, rest;
  51. if (is_omap_iommu_detached(obj))
  52. return -EPERM;
  53. buf = kmalloc(count, GFP_KERNEL);
  54. if (!buf)
  55. return -ENOMEM;
  56. p = buf;
  57. mutex_lock(&iommu_debug_lock);
  58. p += sprintf(p, "%8s %8s\n", "cam:", "ram:");
  59. p += sprintf(p, "-----------------------------------------\n");
  60. rest = count - (p - buf);
  61. p += omap_dump_tlb_entries(obj, p, rest);
  62. bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
  63. mutex_unlock(&iommu_debug_lock);
  64. kfree(buf);
  65. return bytes;
  66. }
  67. static void dump_ioptable(struct seq_file *s)
  68. {
  69. int i, j;
  70. u32 da;
  71. u32 *iopgd, *iopte;
  72. struct omap_iommu *obj = s->private;
  73. spin_lock(&obj->page_table_lock);
  74. iopgd = iopgd_offset(obj, 0);
  75. for (i = 0; i < PTRS_PER_IOPGD; i++, iopgd++) {
  76. if (!*iopgd)
  77. continue;
  78. if (!(*iopgd & IOPGD_TABLE)) {
  79. da = i << IOPGD_SHIFT;
  80. seq_printf(s, "1: 0x%08x 0x%08x\n", da, *iopgd);
  81. continue;
  82. }
  83. iopte = iopte_offset(iopgd, 0);
  84. for (j = 0; j < PTRS_PER_IOPTE; j++, iopte++) {
  85. if (!*iopte)
  86. continue;
  87. da = (i << IOPGD_SHIFT) + (j << IOPTE_SHIFT);
  88. seq_printf(s, "2: 0x%08x 0x%08x\n", da, *iopte);
  89. }
  90. }
  91. spin_unlock(&obj->page_table_lock);
  92. }
  93. static int debug_read_pagetable(struct seq_file *s, void *data)
  94. {
  95. struct omap_iommu *obj = s->private;
  96. if (is_omap_iommu_detached(obj))
  97. return -EPERM;
  98. mutex_lock(&iommu_debug_lock);
  99. seq_printf(s, "L: %8s %8s\n", "da:", "pte:");
  100. seq_puts(s, "--------------------------\n");
  101. dump_ioptable(s);
  102. mutex_unlock(&iommu_debug_lock);
  103. return 0;
  104. }
  105. #define DEBUG_SEQ_FOPS_RO(name) \
  106. static int debug_open_##name(struct inode *inode, struct file *file) \
  107. { \
  108. return single_open(file, debug_read_##name, inode->i_private); \
  109. } \
  110. \
  111. static const struct file_operations debug_##name##_fops = { \
  112. .open = debug_open_##name, \
  113. .read = seq_read, \
  114. .llseek = seq_lseek, \
  115. .release = single_release, \
  116. }
  117. #define DEBUG_FOPS_RO(name) \
  118. static const struct file_operations debug_##name##_fops = { \
  119. .open = simple_open, \
  120. .read = debug_read_##name, \
  121. .llseek = generic_file_llseek, \
  122. };
  123. DEBUG_FOPS_RO(regs);
  124. DEBUG_FOPS_RO(tlb);
  125. DEBUG_SEQ_FOPS_RO(pagetable);
  126. #define __DEBUG_ADD_FILE(attr, mode) \
  127. { \
  128. struct dentry *dent; \
  129. dent = debugfs_create_file(#attr, mode, obj->debug_dir, \
  130. obj, &debug_##attr##_fops); \
  131. if (!dent) \
  132. goto err; \
  133. }
  134. #define DEBUG_ADD_FILE_RO(name) __DEBUG_ADD_FILE(name, 0400)
  135. void omap_iommu_debugfs_add(struct omap_iommu *obj)
  136. {
  137. struct dentry *d;
  138. if (!iommu_debug_root)
  139. return;
  140. obj->debug_dir = debugfs_create_dir(obj->name, iommu_debug_root);
  141. if (!obj->debug_dir)
  142. return;
  143. d = debugfs_create_u8("nr_tlb_entries", 0400, obj->debug_dir,
  144. (u8 *)&obj->nr_tlb_entries);
  145. if (!d)
  146. return;
  147. DEBUG_ADD_FILE_RO(regs);
  148. DEBUG_ADD_FILE_RO(tlb);
  149. DEBUG_ADD_FILE_RO(pagetable);
  150. return;
  151. err:
  152. debugfs_remove_recursive(obj->debug_dir);
  153. }
  154. void omap_iommu_debugfs_remove(struct omap_iommu *obj)
  155. {
  156. if (!obj->debug_dir)
  157. return;
  158. debugfs_remove_recursive(obj->debug_dir);
  159. }
  160. void __init omap_iommu_debugfs_init(void)
  161. {
  162. iommu_debug_root = debugfs_create_dir("omap_iommu", NULL);
  163. if (!iommu_debug_root)
  164. pr_err("can't create debugfs dir\n");
  165. }
  166. void __exit omap_iommu_debugfs_exit(void)
  167. {
  168. debugfs_remove(iommu_debug_root);
  169. }