omap-iommu-debug.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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/module.h>
  13. #include <linux/err.h>
  14. #include <linux/clk.h>
  15. #include <linux/io.h>
  16. #include <linux/slab.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/debugfs.h>
  20. #include <plat/iommu.h>
  21. #include <plat/iovmm.h>
  22. #include <plat/iopgtable.h>
  23. #define MAXCOLUMN 100 /* for short messages */
  24. static DEFINE_MUTEX(iommu_debug_lock);
  25. static struct dentry *iommu_debug_root;
  26. static ssize_t debug_read_ver(struct file *file, char __user *userbuf,
  27. size_t count, loff_t *ppos)
  28. {
  29. u32 ver = omap_iommu_arch_version();
  30. char buf[MAXCOLUMN], *p = buf;
  31. p += sprintf(p, "H/W version: %d.%d\n", (ver >> 4) & 0xf , ver & 0xf);
  32. return simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
  33. }
  34. static ssize_t debug_read_regs(struct file *file, char __user *userbuf,
  35. size_t count, loff_t *ppos)
  36. {
  37. struct omap_iommu *obj = file->private_data;
  38. char *p, *buf;
  39. ssize_t bytes;
  40. buf = kmalloc(count, GFP_KERNEL);
  41. if (!buf)
  42. return -ENOMEM;
  43. p = buf;
  44. mutex_lock(&iommu_debug_lock);
  45. bytes = omap_iommu_dump_ctx(obj, p, count);
  46. bytes = simple_read_from_buffer(userbuf, count, ppos, buf, bytes);
  47. mutex_unlock(&iommu_debug_lock);
  48. kfree(buf);
  49. return bytes;
  50. }
  51. static ssize_t debug_read_tlb(struct file *file, char __user *userbuf,
  52. size_t count, loff_t *ppos)
  53. {
  54. struct omap_iommu *obj = file->private_data;
  55. char *p, *buf;
  56. ssize_t bytes, rest;
  57. buf = kmalloc(count, GFP_KERNEL);
  58. if (!buf)
  59. return -ENOMEM;
  60. p = buf;
  61. mutex_lock(&iommu_debug_lock);
  62. p += sprintf(p, "%8s %8s\n", "cam:", "ram:");
  63. p += sprintf(p, "-----------------------------------------\n");
  64. rest = count - (p - buf);
  65. p += omap_dump_tlb_entries(obj, p, rest);
  66. bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
  67. mutex_unlock(&iommu_debug_lock);
  68. kfree(buf);
  69. return bytes;
  70. }
  71. static ssize_t debug_write_pagetable(struct file *file,
  72. const char __user *userbuf, size_t count, loff_t *ppos)
  73. {
  74. struct iotlb_entry e;
  75. struct cr_regs cr;
  76. int err;
  77. struct omap_iommu *obj = file->private_data;
  78. char buf[MAXCOLUMN], *p = buf;
  79. count = min(count, sizeof(buf));
  80. mutex_lock(&iommu_debug_lock);
  81. if (copy_from_user(p, userbuf, count)) {
  82. mutex_unlock(&iommu_debug_lock);
  83. return -EFAULT;
  84. }
  85. sscanf(p, "%x %x", &cr.cam, &cr.ram);
  86. if (!cr.cam || !cr.ram) {
  87. mutex_unlock(&iommu_debug_lock);
  88. return -EINVAL;
  89. }
  90. omap_iotlb_cr_to_e(&cr, &e);
  91. err = omap_iopgtable_store_entry(obj, &e);
  92. if (err)
  93. dev_err(obj->dev, "%s: fail to store cr\n", __func__);
  94. mutex_unlock(&iommu_debug_lock);
  95. return count;
  96. }
  97. #define dump_ioptable_entry_one(lv, da, val) \
  98. ({ \
  99. int __err = 0; \
  100. ssize_t bytes; \
  101. const int maxcol = 22; \
  102. const char *str = "%d: %08x %08x\n"; \
  103. bytes = snprintf(p, maxcol, str, lv, da, val); \
  104. p += bytes; \
  105. len -= bytes; \
  106. if (len < maxcol) \
  107. __err = -ENOMEM; \
  108. __err; \
  109. })
  110. static ssize_t dump_ioptable(struct omap_iommu *obj, char *buf, ssize_t len)
  111. {
  112. int i;
  113. u32 *iopgd;
  114. char *p = buf;
  115. spin_lock(&obj->page_table_lock);
  116. iopgd = iopgd_offset(obj, 0);
  117. for (i = 0; i < PTRS_PER_IOPGD; i++, iopgd++) {
  118. int j, err;
  119. u32 *iopte;
  120. u32 da;
  121. if (!*iopgd)
  122. continue;
  123. if (!(*iopgd & IOPGD_TABLE)) {
  124. da = i << IOPGD_SHIFT;
  125. err = dump_ioptable_entry_one(1, da, *iopgd);
  126. if (err)
  127. goto out;
  128. continue;
  129. }
  130. iopte = iopte_offset(iopgd, 0);
  131. for (j = 0; j < PTRS_PER_IOPTE; j++, iopte++) {
  132. if (!*iopte)
  133. continue;
  134. da = (i << IOPGD_SHIFT) + (j << IOPTE_SHIFT);
  135. err = dump_ioptable_entry_one(2, da, *iopgd);
  136. if (err)
  137. goto out;
  138. }
  139. }
  140. out:
  141. spin_unlock(&obj->page_table_lock);
  142. return p - buf;
  143. }
  144. static ssize_t debug_read_pagetable(struct file *file, char __user *userbuf,
  145. size_t count, loff_t *ppos)
  146. {
  147. struct omap_iommu *obj = file->private_data;
  148. char *p, *buf;
  149. size_t bytes;
  150. buf = (char *)__get_free_page(GFP_KERNEL);
  151. if (!buf)
  152. return -ENOMEM;
  153. p = buf;
  154. p += sprintf(p, "L: %8s %8s\n", "da:", "pa:");
  155. p += sprintf(p, "-----------------------------------------\n");
  156. mutex_lock(&iommu_debug_lock);
  157. bytes = PAGE_SIZE - (p - buf);
  158. p += dump_ioptable(obj, p, bytes);
  159. bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
  160. mutex_unlock(&iommu_debug_lock);
  161. free_page((unsigned long)buf);
  162. return bytes;
  163. }
  164. static ssize_t debug_read_mmap(struct file *file, char __user *userbuf,
  165. size_t count, loff_t *ppos)
  166. {
  167. struct omap_iommu *obj = file->private_data;
  168. char *p, *buf;
  169. struct iovm_struct *tmp;
  170. int uninitialized_var(i);
  171. ssize_t bytes;
  172. buf = (char *)__get_free_page(GFP_KERNEL);
  173. if (!buf)
  174. return -ENOMEM;
  175. p = buf;
  176. p += sprintf(p, "%-3s %-8s %-8s %6s %8s\n",
  177. "No", "start", "end", "size", "flags");
  178. p += sprintf(p, "-------------------------------------------------\n");
  179. mutex_lock(&iommu_debug_lock);
  180. list_for_each_entry(tmp, &obj->mmap, list) {
  181. size_t len;
  182. const char *str = "%3d %08x-%08x %6x %8x\n";
  183. const int maxcol = 39;
  184. len = tmp->da_end - tmp->da_start;
  185. p += snprintf(p, maxcol, str,
  186. i, tmp->da_start, tmp->da_end, len, tmp->flags);
  187. if (PAGE_SIZE - (p - buf) < maxcol)
  188. break;
  189. i++;
  190. }
  191. bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
  192. mutex_unlock(&iommu_debug_lock);
  193. free_page((unsigned long)buf);
  194. return bytes;
  195. }
  196. static ssize_t debug_read_mem(struct file *file, char __user *userbuf,
  197. size_t count, loff_t *ppos)
  198. {
  199. struct omap_iommu *obj = file->private_data;
  200. char *p, *buf;
  201. struct iovm_struct *area;
  202. ssize_t bytes;
  203. count = min_t(ssize_t, count, PAGE_SIZE);
  204. buf = (char *)__get_free_page(GFP_KERNEL);
  205. if (!buf)
  206. return -ENOMEM;
  207. p = buf;
  208. mutex_lock(&iommu_debug_lock);
  209. area = omap_find_iovm_area(obj, (u32)ppos);
  210. if (IS_ERR(area)) {
  211. bytes = -EINVAL;
  212. goto err_out;
  213. }
  214. memcpy(p, area->va, count);
  215. p += count;
  216. bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
  217. err_out:
  218. mutex_unlock(&iommu_debug_lock);
  219. free_page((unsigned long)buf);
  220. return bytes;
  221. }
  222. static ssize_t debug_write_mem(struct file *file, const char __user *userbuf,
  223. size_t count, loff_t *ppos)
  224. {
  225. struct omap_iommu *obj = file->private_data;
  226. struct iovm_struct *area;
  227. char *p, *buf;
  228. count = min_t(size_t, count, PAGE_SIZE);
  229. buf = (char *)__get_free_page(GFP_KERNEL);
  230. if (!buf)
  231. return -ENOMEM;
  232. p = buf;
  233. mutex_lock(&iommu_debug_lock);
  234. if (copy_from_user(p, userbuf, count)) {
  235. count = -EFAULT;
  236. goto err_out;
  237. }
  238. area = omap_find_iovm_area(obj, (u32)ppos);
  239. if (IS_ERR(area)) {
  240. count = -EINVAL;
  241. goto err_out;
  242. }
  243. memcpy(area->va, p, count);
  244. err_out:
  245. mutex_unlock(&iommu_debug_lock);
  246. free_page((unsigned long)buf);
  247. return count;
  248. }
  249. static int debug_open_generic(struct inode *inode, struct file *file)
  250. {
  251. file->private_data = inode->i_private;
  252. return 0;
  253. }
  254. #define DEBUG_FOPS(name) \
  255. static const struct file_operations debug_##name##_fops = { \
  256. .open = debug_open_generic, \
  257. .read = debug_read_##name, \
  258. .write = debug_write_##name, \
  259. .llseek = generic_file_llseek, \
  260. };
  261. #define DEBUG_FOPS_RO(name) \
  262. static const struct file_operations debug_##name##_fops = { \
  263. .open = debug_open_generic, \
  264. .read = debug_read_##name, \
  265. .llseek = generic_file_llseek, \
  266. };
  267. DEBUG_FOPS_RO(ver);
  268. DEBUG_FOPS_RO(regs);
  269. DEBUG_FOPS_RO(tlb);
  270. DEBUG_FOPS(pagetable);
  271. DEBUG_FOPS_RO(mmap);
  272. DEBUG_FOPS(mem);
  273. #define __DEBUG_ADD_FILE(attr, mode) \
  274. { \
  275. struct dentry *dent; \
  276. dent = debugfs_create_file(#attr, mode, parent, \
  277. obj, &debug_##attr##_fops); \
  278. if (!dent) \
  279. return -ENOMEM; \
  280. }
  281. #define DEBUG_ADD_FILE(name) __DEBUG_ADD_FILE(name, 600)
  282. #define DEBUG_ADD_FILE_RO(name) __DEBUG_ADD_FILE(name, 400)
  283. static int iommu_debug_register(struct device *dev, void *data)
  284. {
  285. struct platform_device *pdev = to_platform_device(dev);
  286. struct omap_iommu *obj = platform_get_drvdata(pdev);
  287. struct dentry *d, *parent;
  288. if (!obj || !obj->dev)
  289. return -EINVAL;
  290. d = debugfs_create_dir(obj->name, iommu_debug_root);
  291. if (!d)
  292. return -ENOMEM;
  293. parent = d;
  294. d = debugfs_create_u8("nr_tlb_entries", 400, parent,
  295. (u8 *)&obj->nr_tlb_entries);
  296. if (!d)
  297. return -ENOMEM;
  298. DEBUG_ADD_FILE_RO(ver);
  299. DEBUG_ADD_FILE_RO(regs);
  300. DEBUG_ADD_FILE_RO(tlb);
  301. DEBUG_ADD_FILE(pagetable);
  302. DEBUG_ADD_FILE_RO(mmap);
  303. DEBUG_ADD_FILE(mem);
  304. return 0;
  305. }
  306. static int __init iommu_debug_init(void)
  307. {
  308. struct dentry *d;
  309. int err;
  310. d = debugfs_create_dir("iommu", NULL);
  311. if (!d)
  312. return -ENOMEM;
  313. iommu_debug_root = d;
  314. err = omap_foreach_iommu_device(d, iommu_debug_register);
  315. if (err)
  316. goto err_out;
  317. return 0;
  318. err_out:
  319. debugfs_remove_recursive(iommu_debug_root);
  320. return err;
  321. }
  322. module_init(iommu_debug_init)
  323. static void __exit iommu_debugfs_exit(void)
  324. {
  325. debugfs_remove_recursive(iommu_debug_root);
  326. }
  327. module_exit(iommu_debugfs_exit)
  328. MODULE_DESCRIPTION("omap iommu: debugfs interface");
  329. MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>");
  330. MODULE_LICENSE("GPL v2");