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