imr_selftest.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * imr_selftest.c
  3. *
  4. * Copyright(c) 2013 Intel Corporation.
  5. * Copyright(c) 2015 Bryan O'Donoghue <pure.logic@nexus-software.ie>
  6. *
  7. * IMR self test. The purpose of this module is to run a set of tests on the
  8. * IMR API to validate it's sanity. We check for overlapping, reserved
  9. * addresses and setup/teardown sanity.
  10. *
  11. */
  12. #include <asm-generic/sections.h>
  13. #include <asm/cpu_device_id.h>
  14. #include <asm/imr.h>
  15. #include <linux/init.h>
  16. #include <linux/mm.h>
  17. #include <linux/module.h>
  18. #include <linux/types.h>
  19. #define SELFTEST KBUILD_MODNAME ": "
  20. /**
  21. * imr_self_test_result - Print result string for self test.
  22. *
  23. * @res: result code - true if test passed false otherwise.
  24. * @fmt: format string.
  25. * ... variadic argument list.
  26. */
  27. static void __init imr_self_test_result(int res, const char *fmt, ...)
  28. {
  29. va_list vlist;
  30. /* Print pass/fail. */
  31. if (res)
  32. pr_info(SELFTEST "pass ");
  33. else
  34. pr_info(SELFTEST "fail ");
  35. /* Print variable string. */
  36. va_start(vlist, fmt);
  37. vprintk(fmt, vlist);
  38. va_end(vlist);
  39. /* Optional warning. */
  40. WARN(res == 0, "test failed");
  41. }
  42. #undef SELFTEST
  43. /**
  44. * imr_self_test
  45. *
  46. * Verify IMR self_test with some simple tests to verify overlap,
  47. * zero sized allocations and 1 KiB sized areas.
  48. *
  49. */
  50. static void __init imr_self_test(void)
  51. {
  52. phys_addr_t base = virt_to_phys(&_text);
  53. size_t size = virt_to_phys(&__end_rodata) - base;
  54. const char *fmt_over = "overlapped IMR @ (0x%08lx - 0x%08lx)\n";
  55. int ret;
  56. /* Test zero zero. */
  57. ret = imr_add_range(0, 0, 0, 0, false);
  58. imr_self_test_result(ret < 0, "zero sized IMR\n");
  59. /* Test exact overlap. */
  60. ret = imr_add_range(base, size, IMR_CPU, IMR_CPU, false);
  61. imr_self_test_result(ret < 0, fmt_over, __va(base), __va(base + size));
  62. /* Test overlap with base inside of existing. */
  63. base += size - IMR_ALIGN;
  64. ret = imr_add_range(base, size, IMR_CPU, IMR_CPU, false);
  65. imr_self_test_result(ret < 0, fmt_over, __va(base), __va(base + size));
  66. /* Test overlap with end inside of existing. */
  67. base -= size + IMR_ALIGN * 2;
  68. ret = imr_add_range(base, size, IMR_CPU, IMR_CPU, false);
  69. imr_self_test_result(ret < 0, fmt_over, __va(base), __va(base + size));
  70. /* Test that a 1 KiB IMR @ zero with read/write all will bomb out. */
  71. ret = imr_add_range(0, IMR_ALIGN, IMR_READ_ACCESS_ALL,
  72. IMR_WRITE_ACCESS_ALL, false);
  73. imr_self_test_result(ret < 0, "1KiB IMR @ 0x00000000 - access-all\n");
  74. /* Test that a 1 KiB IMR @ zero with CPU only will work. */
  75. ret = imr_add_range(0, IMR_ALIGN, IMR_CPU, IMR_CPU, false);
  76. imr_self_test_result(ret >= 0, "1KiB IMR @ 0x00000000 - cpu-access\n");
  77. if (ret >= 0) {
  78. ret = imr_remove_range(0, IMR_ALIGN);
  79. imr_self_test_result(ret == 0, "teardown - cpu-access\n");
  80. }
  81. /* Test 2 KiB works. */
  82. size = IMR_ALIGN * 2;
  83. ret = imr_add_range(0, size, IMR_READ_ACCESS_ALL,
  84. IMR_WRITE_ACCESS_ALL, false);
  85. imr_self_test_result(ret >= 0, "2KiB IMR @ 0x00000000\n");
  86. if (ret >= 0) {
  87. ret = imr_remove_range(0, size);
  88. imr_self_test_result(ret == 0, "teardown 2KiB\n");
  89. }
  90. }
  91. static const struct x86_cpu_id imr_ids[] __initconst = {
  92. { X86_VENDOR_INTEL, 5, 9 }, /* Intel Quark SoC X1000. */
  93. {}
  94. };
  95. MODULE_DEVICE_TABLE(x86cpu, imr_ids);
  96. /**
  97. * imr_self_test_init - entry point for IMR driver.
  98. *
  99. * return: -ENODEV for no IMR support 0 if good to go.
  100. */
  101. static int __init imr_self_test_init(void)
  102. {
  103. if (x86_match_cpu(imr_ids))
  104. imr_self_test();
  105. return 0;
  106. }
  107. /**
  108. * imr_self_test_exit - exit point for IMR code.
  109. *
  110. * return:
  111. */
  112. static void __exit imr_self_test_exit(void)
  113. {
  114. }
  115. module_init(imr_self_test_init);
  116. module_exit(imr_self_test_exit);
  117. MODULE_AUTHOR("Bryan O'Donoghue <pure.logic@nexus-software.ie>");
  118. MODULE_DESCRIPTION("Intel Isolated Memory Region self-test driver");
  119. MODULE_LICENSE("Dual BSD/GPL");