iommu-debug.c 8.9 KB

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