testmmiotrace.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Written by Pekka Paalanen, 2008-2009 <pq@iki.fi>
  3. */
  4. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  5. #include <linux/module.h>
  6. #include <linux/io.h>
  7. #include <linux/mmiotrace.h>
  8. static unsigned long mmio_address;
  9. module_param(mmio_address, ulong, 0);
  10. MODULE_PARM_DESC(mmio_address, " Start address of the mapping of 16 kB "
  11. "(or 8 MB if read_far is non-zero).");
  12. static unsigned long read_far = 0x400100;
  13. module_param(read_far, ulong, 0);
  14. MODULE_PARM_DESC(read_far, " Offset of a 32-bit read within 8 MB "
  15. "(default: 0x400100).");
  16. static unsigned v16(unsigned i)
  17. {
  18. return i * 12 + 7;
  19. }
  20. static unsigned v32(unsigned i)
  21. {
  22. return i * 212371 + 13;
  23. }
  24. static void do_write_test(void __iomem *p)
  25. {
  26. unsigned int i;
  27. pr_info("write test.\n");
  28. mmiotrace_printk("Write test.\n");
  29. for (i = 0; i < 256; i++)
  30. iowrite8(i, p + i);
  31. for (i = 1024; i < (5 * 1024); i += 2)
  32. iowrite16(v16(i), p + i);
  33. for (i = (5 * 1024); i < (16 * 1024); i += 4)
  34. iowrite32(v32(i), p + i);
  35. }
  36. static void do_read_test(void __iomem *p)
  37. {
  38. unsigned int i;
  39. unsigned errs[3] = { 0 };
  40. pr_info("read test.\n");
  41. mmiotrace_printk("Read test.\n");
  42. for (i = 0; i < 256; i++)
  43. if (ioread8(p + i) != i)
  44. ++errs[0];
  45. for (i = 1024; i < (5 * 1024); i += 2)
  46. if (ioread16(p + i) != v16(i))
  47. ++errs[1];
  48. for (i = (5 * 1024); i < (16 * 1024); i += 4)
  49. if (ioread32(p + i) != v32(i))
  50. ++errs[2];
  51. mmiotrace_printk("Read errors: 8-bit %d, 16-bit %d, 32-bit %d.\n",
  52. errs[0], errs[1], errs[2]);
  53. }
  54. static void do_read_far_test(void __iomem *p)
  55. {
  56. pr_info("read far test.\n");
  57. mmiotrace_printk("Read far test.\n");
  58. ioread32(p + read_far);
  59. }
  60. static void do_test(unsigned long size)
  61. {
  62. void __iomem *p = ioremap_nocache(mmio_address, size);
  63. if (!p) {
  64. pr_err("could not ioremap, aborting.\n");
  65. return;
  66. }
  67. mmiotrace_printk("ioremap returned %p.\n", p);
  68. do_write_test(p);
  69. do_read_test(p);
  70. if (read_far && read_far < size - 4)
  71. do_read_far_test(p);
  72. iounmap(p);
  73. }
  74. /*
  75. * Tests how mmiotrace behaves in face of multiple ioremap / iounmaps in
  76. * a short time. We had a bug in deferred freeing procedure which tried
  77. * to free this region multiple times (ioremap can reuse the same address
  78. * for many mappings).
  79. */
  80. static void do_test_bulk_ioremapping(void)
  81. {
  82. void __iomem *p;
  83. int i;
  84. for (i = 0; i < 10; ++i) {
  85. p = ioremap_nocache(mmio_address, PAGE_SIZE);
  86. if (p)
  87. iounmap(p);
  88. }
  89. /* Force freeing. If it will crash we will know why. */
  90. synchronize_rcu();
  91. }
  92. static int __init init(void)
  93. {
  94. unsigned long size = (read_far) ? (8 << 20) : (16 << 10);
  95. if (mmio_address == 0) {
  96. pr_err("you have to use the module argument mmio_address.\n");
  97. pr_err("DO NOT LOAD THIS MODULE UNLESS YOU REALLY KNOW WHAT YOU ARE DOING!\n");
  98. return -ENXIO;
  99. }
  100. pr_warning("WARNING: mapping %lu kB @ 0x%08lx in PCI address space, "
  101. "and writing 16 kB of rubbish in there.\n",
  102. size >> 10, mmio_address);
  103. do_test(size);
  104. do_test_bulk_ioremapping();
  105. pr_info("All done.\n");
  106. return 0;
  107. }
  108. static void __exit cleanup(void)
  109. {
  110. pr_debug("unloaded.\n");
  111. }
  112. module_init(init);
  113. module_exit(cleanup);
  114. MODULE_LICENSE("GPL");