omap-iommu-debug.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 device *dev = file->private_data;
  38. struct omap_iommu *obj = dev_to_omap_iommu(dev);
  39. char *p, *buf;
  40. ssize_t bytes;
  41. buf = kmalloc(count, GFP_KERNEL);
  42. if (!buf)
  43. return -ENOMEM;
  44. p = buf;
  45. mutex_lock(&iommu_debug_lock);
  46. bytes = omap_iommu_dump_ctx(obj, p, count);
  47. bytes = simple_read_from_buffer(userbuf, count, ppos, buf, bytes);
  48. mutex_unlock(&iommu_debug_lock);
  49. kfree(buf);
  50. return bytes;
  51. }
  52. static ssize_t debug_read_tlb(struct file *file, char __user *userbuf,
  53. size_t count, loff_t *ppos)
  54. {
  55. struct device *dev = file->private_data;
  56. struct omap_iommu *obj = dev_to_omap_iommu(dev);
  57. char *p, *buf;
  58. ssize_t bytes, rest;
  59. buf = kmalloc(count, GFP_KERNEL);
  60. if (!buf)
  61. return -ENOMEM;
  62. p = buf;
  63. mutex_lock(&iommu_debug_lock);
  64. p += sprintf(p, "%8s %8s\n", "cam:", "ram:");
  65. p += sprintf(p, "-----------------------------------------\n");
  66. rest = count - (p - buf);
  67. p += omap_dump_tlb_entries(obj, p, rest);
  68. bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
  69. mutex_unlock(&iommu_debug_lock);
  70. kfree(buf);
  71. return bytes;
  72. }
  73. static ssize_t debug_write_pagetable(struct file *file,
  74. const char __user *userbuf, size_t count, loff_t *ppos)
  75. {
  76. struct iotlb_entry e;
  77. struct cr_regs cr;
  78. int err;
  79. struct device *dev = file->private_data;
  80. struct omap_iommu *obj = dev_to_omap_iommu(dev);
  81. char buf[MAXCOLUMN], *p = buf;
  82. count = min(count, sizeof(buf));
  83. mutex_lock(&iommu_debug_lock);
  84. if (copy_from_user(p, userbuf, count)) {
  85. mutex_unlock(&iommu_debug_lock);
  86. return -EFAULT;
  87. }
  88. sscanf(p, "%x %x", &cr.cam, &cr.ram);
  89. if (!cr.cam || !cr.ram) {
  90. mutex_unlock(&iommu_debug_lock);
  91. return -EINVAL;
  92. }
  93. omap_iotlb_cr_to_e(&cr, &e);
  94. err = omap_iopgtable_store_entry(obj, &e);
  95. if (err)
  96. dev_err(obj->dev, "%s: fail to store cr\n", __func__);
  97. mutex_unlock(&iommu_debug_lock);
  98. return count;
  99. }
  100. #define dump_ioptable_entry_one(lv, da, val) \
  101. ({ \
  102. int __err = 0; \
  103. ssize_t bytes; \
  104. const int maxcol = 22; \
  105. const char *str = "%d: %08x %08x\n"; \
  106. bytes = snprintf(p, maxcol, str, lv, da, val); \
  107. p += bytes; \
  108. len -= bytes; \
  109. if (len < maxcol) \
  110. __err = -ENOMEM; \
  111. __err; \
  112. })
  113. static ssize_t dump_ioptable(struct omap_iommu *obj, char *buf, ssize_t len)
  114. {
  115. int i;
  116. u32 *iopgd;
  117. char *p = buf;
  118. spin_lock(&obj->page_table_lock);
  119. iopgd = iopgd_offset(obj, 0);
  120. for (i = 0; i < PTRS_PER_IOPGD; i++, iopgd++) {
  121. int j, err;
  122. u32 *iopte;
  123. u32 da;
  124. if (!*iopgd)
  125. continue;
  126. if (!(*iopgd & IOPGD_TABLE)) {
  127. da = i << IOPGD_SHIFT;
  128. err = dump_ioptable_entry_one(1, da, *iopgd);
  129. if (err)
  130. goto out;
  131. continue;
  132. }
  133. iopte = iopte_offset(iopgd, 0);
  134. for (j = 0; j < PTRS_PER_IOPTE; j++, iopte++) {
  135. if (!*iopte)
  136. continue;
  137. da = (i << IOPGD_SHIFT) + (j << IOPTE_SHIFT);
  138. err = dump_ioptable_entry_one(2, da, *iopgd);
  139. if (err)
  140. goto out;
  141. }
  142. }
  143. out:
  144. spin_unlock(&obj->page_table_lock);
  145. return p - buf;
  146. }
  147. static ssize_t debug_read_pagetable(struct file *file, char __user *userbuf,
  148. size_t count, loff_t *ppos)
  149. {
  150. struct device *dev = file->private_data;
  151. struct omap_iommu *obj = dev_to_omap_iommu(dev);
  152. char *p, *buf;
  153. size_t bytes;
  154. buf = (char *)__get_free_page(GFP_KERNEL);
  155. if (!buf)
  156. return -ENOMEM;
  157. p = buf;
  158. p += sprintf(p, "L: %8s %8s\n", "da:", "pa:");
  159. p += sprintf(p, "-----------------------------------------\n");
  160. mutex_lock(&iommu_debug_lock);
  161. bytes = PAGE_SIZE - (p - buf);
  162. p += dump_ioptable(obj, p, bytes);
  163. bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
  164. mutex_unlock(&iommu_debug_lock);
  165. free_page((unsigned long)buf);
  166. return bytes;
  167. }
  168. static ssize_t debug_read_mmap(struct file *file, char __user *userbuf,
  169. size_t count, loff_t *ppos)
  170. {
  171. struct device *dev = file->private_data;
  172. struct omap_iommu *obj = dev_to_omap_iommu(dev);
  173. char *p, *buf;
  174. struct iovm_struct *tmp;
  175. int uninitialized_var(i);
  176. ssize_t bytes;
  177. buf = (char *)__get_free_page(GFP_KERNEL);
  178. if (!buf)
  179. return -ENOMEM;
  180. p = buf;
  181. p += sprintf(p, "%-3s %-8s %-8s %6s %8s\n",
  182. "No", "start", "end", "size", "flags");
  183. p += sprintf(p, "-------------------------------------------------\n");
  184. mutex_lock(&iommu_debug_lock);
  185. list_for_each_entry(tmp, &obj->mmap, list) {
  186. size_t len;
  187. const char *str = "%3d %08x-%08x %6x %8x\n";
  188. const int maxcol = 39;
  189. len = tmp->da_end - tmp->da_start;
  190. p += snprintf(p, maxcol, str,
  191. i, tmp->da_start, tmp->da_end, len, tmp->flags);
  192. if (PAGE_SIZE - (p - buf) < maxcol)
  193. break;
  194. i++;
  195. }
  196. bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
  197. mutex_unlock(&iommu_debug_lock);
  198. free_page((unsigned long)buf);
  199. return bytes;
  200. }
  201. static ssize_t debug_read_mem(struct file *file, char __user *userbuf,
  202. size_t count, loff_t *ppos)
  203. {
  204. struct device *dev = file->private_data;
  205. char *p, *buf;
  206. struct iovm_struct *area;
  207. ssize_t bytes;
  208. count = min_t(ssize_t, count, PAGE_SIZE);
  209. buf = (char *)__get_free_page(GFP_KERNEL);
  210. if (!buf)
  211. return -ENOMEM;
  212. p = buf;
  213. mutex_lock(&iommu_debug_lock);
  214. area = omap_find_iovm_area(dev, (u32)ppos);
  215. if (!area) {
  216. bytes = -EINVAL;
  217. goto err_out;
  218. }
  219. memcpy(p, area->va, count);
  220. p += count;
  221. bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
  222. err_out:
  223. mutex_unlock(&iommu_debug_lock);
  224. free_page((unsigned long)buf);
  225. return bytes;
  226. }
  227. static ssize_t debug_write_mem(struct file *file, const char __user *userbuf,
  228. size_t count, loff_t *ppos)
  229. {
  230. struct device *dev = file->private_data;
  231. struct iovm_struct *area;
  232. char *p, *buf;
  233. count = min_t(size_t, count, PAGE_SIZE);
  234. buf = (char *)__get_free_page(GFP_KERNEL);
  235. if (!buf)
  236. return -ENOMEM;
  237. p = buf;
  238. mutex_lock(&iommu_debug_lock);
  239. if (copy_from_user(p, userbuf, count)) {
  240. count = -EFAULT;
  241. goto err_out;
  242. }
  243. area = omap_find_iovm_area(dev, (u32)ppos);
  244. if (!area) {
  245. count = -EINVAL;
  246. goto err_out;
  247. }
  248. memcpy(area->va, p, count);
  249. err_out:
  250. mutex_unlock(&iommu_debug_lock);
  251. free_page((unsigned long)buf);
  252. return count;
  253. }
  254. #define DEBUG_FOPS(name) \
  255. static const struct file_operations debug_##name##_fops = { \
  256. .open = simple_open, \
  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 = simple_open, \
  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. dev, &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 omap_iommu_arch_data *arch_data;
  288. struct dentry *d, *parent;
  289. if (!obj || !obj->dev)
  290. return -EINVAL;
  291. arch_data = kzalloc(sizeof(*arch_data), GFP_KERNEL);
  292. if (!arch_data)
  293. return -ENOMEM;
  294. arch_data->iommu_dev = obj;
  295. dev->archdata.iommu = arch_data;
  296. d = debugfs_create_dir(obj->name, iommu_debug_root);
  297. if (!d)
  298. goto nomem;
  299. parent = d;
  300. d = debugfs_create_u8("nr_tlb_entries", 400, parent,
  301. (u8 *)&obj->nr_tlb_entries);
  302. if (!d)
  303. goto nomem;
  304. DEBUG_ADD_FILE_RO(ver);
  305. DEBUG_ADD_FILE_RO(regs);
  306. DEBUG_ADD_FILE_RO(tlb);
  307. DEBUG_ADD_FILE(pagetable);
  308. DEBUG_ADD_FILE_RO(mmap);
  309. DEBUG_ADD_FILE(mem);
  310. return 0;
  311. nomem:
  312. kfree(arch_data);
  313. return -ENOMEM;
  314. }
  315. static int iommu_debug_unregister(struct device *dev, void *data)
  316. {
  317. if (!dev->archdata.iommu)
  318. return 0;
  319. kfree(dev->archdata.iommu);
  320. dev->archdata.iommu = NULL;
  321. return 0;
  322. }
  323. static int __init iommu_debug_init(void)
  324. {
  325. struct dentry *d;
  326. int err;
  327. d = debugfs_create_dir("iommu", NULL);
  328. if (!d)
  329. return -ENOMEM;
  330. iommu_debug_root = d;
  331. err = omap_foreach_iommu_device(d, iommu_debug_register);
  332. if (err)
  333. goto err_out;
  334. return 0;
  335. err_out:
  336. debugfs_remove_recursive(iommu_debug_root);
  337. return err;
  338. }
  339. module_init(iommu_debug_init)
  340. static void __exit iommu_debugfs_exit(void)
  341. {
  342. debugfs_remove_recursive(iommu_debug_root);
  343. omap_foreach_iommu_device(NULL, iommu_debug_unregister);
  344. }
  345. module_exit(iommu_debugfs_exit)
  346. MODULE_DESCRIPTION("omap iommu: debugfs interface");
  347. MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>");
  348. MODULE_LICENSE("GPL v2");