imr_selftest.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * imr_selftest.c -- Intel Isolated Memory Region self-test driver
  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/types.h>
  18. #define SELFTEST KBUILD_MODNAME ": "
  19. /**
  20. * imr_self_test_result - Print result string for self test.
  21. *
  22. * @res: result code - true if test passed false otherwise.
  23. * @fmt: format string.
  24. * ... variadic argument list.
  25. */
  26. static __printf(2, 3)
  27. 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);
  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);
  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);
  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);
  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);
  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);
  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, IMR_WRITE_ACCESS_ALL);
  84. imr_self_test_result(ret >= 0, "2KiB IMR @ 0x00000000\n");
  85. if (ret >= 0) {
  86. ret = imr_remove_range(0, size);
  87. imr_self_test_result(ret == 0, "teardown 2KiB\n");
  88. }
  89. }
  90. static const struct x86_cpu_id imr_ids[] __initconst = {
  91. { X86_VENDOR_INTEL, 5, 9 }, /* Intel Quark SoC X1000. */
  92. {}
  93. };
  94. /**
  95. * imr_self_test_init - entry point for IMR driver.
  96. *
  97. * return: -ENODEV for no IMR support 0 if good to go.
  98. */
  99. static int __init imr_self_test_init(void)
  100. {
  101. if (x86_match_cpu(imr_ids))
  102. imr_self_test();
  103. return 0;
  104. }
  105. /**
  106. * imr_self_test_exit - exit point for IMR code.
  107. *
  108. * return:
  109. */
  110. device_initcall(imr_self_test_init);