iommu-debug.c 8.9 KB

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