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 void __init imr_self_test_result(int res, const char *fmt, ...)
  27. {
  28. va_list vlist;
  29. /* Print pass/fail. */
  30. if (res)
  31. pr_info(SELFTEST "pass ");
  32. else
  33. pr_info(SELFTEST "fail ");
  34. /* Print variable string. */
  35. va_start(vlist, fmt);
  36. vprintk(fmt, vlist);
  37. va_end(vlist);
  38. /* Optional warning. */
  39. WARN(res == 0, "test failed");
  40. }
  41. #undef SELFTEST
  42. /**
  43. * imr_self_test
  44. *
  45. * Verify IMR self_test with some simple tests to verify overlap,
  46. * zero sized allocations and 1 KiB sized areas.
  47. *
  48. */
  49. static void __init imr_self_test(void)
  50. {
  51. phys_addr_t base = virt_to_phys(&_text);
  52. size_t size = virt_to_phys(&__end_rodata) - base;
  53. const char *fmt_over = "overlapped IMR @ (0x%08lx - 0x%08lx)\n";
  54. int ret;
  55. /* Test zero zero. */
  56. ret = imr_add_range(0, 0, 0, 0);
  57. imr_self_test_result(ret < 0, "zero sized IMR\n");
  58. /* Test exact overlap. */
  59. ret = imr_add_range(base, size, IMR_CPU, IMR_CPU);
  60. imr_self_test_result(ret < 0, fmt_over, __va(base), __va(base + size));
  61. /* Test overlap with base inside of existing. */
  62. base += size - IMR_ALIGN;
  63. ret = imr_add_range(base, size, IMR_CPU, IMR_CPU);
  64. imr_self_test_result(ret < 0, fmt_over, __va(base), __va(base + size));
  65. /* Test overlap with end inside of existing. */
  66. base -= size + IMR_ALIGN * 2;
  67. ret = imr_add_range(base, size, IMR_CPU, IMR_CPU);
  68. imr_self_test_result(ret < 0, fmt_over, __va(base), __va(base + size));
  69. /* Test that a 1 KiB IMR @ zero with read/write all will bomb out. */
  70. ret = imr_add_range(0, IMR_ALIGN, IMR_READ_ACCESS_ALL,
  71. IMR_WRITE_ACCESS_ALL);
  72. imr_self_test_result(ret < 0, "1KiB IMR @ 0x00000000 - access-all\n");
  73. /* Test that a 1 KiB IMR @ zero with CPU only will work. */
  74. ret = imr_add_range(0, IMR_ALIGN, IMR_CPU, IMR_CPU);
  75. imr_self_test_result(ret >= 0, "1KiB IMR @ 0x00000000 - cpu-access\n");
  76. if (ret >= 0) {
  77. ret = imr_remove_range(0, IMR_ALIGN);
  78. imr_self_test_result(ret == 0, "teardown - cpu-access\n");
  79. }
  80. /* Test 2 KiB works. */
  81. size = IMR_ALIGN * 2;
  82. ret = imr_add_range(0, size, IMR_READ_ACCESS_ALL, IMR_WRITE_ACCESS_ALL);
  83. imr_self_test_result(ret >= 0, "2KiB IMR @ 0x00000000\n");
  84. if (ret >= 0) {
  85. ret = imr_remove_range(0, size);
  86. imr_self_test_result(ret == 0, "teardown 2KiB\n");
  87. }
  88. }
  89. static const struct x86_cpu_id imr_ids[] __initconst = {
  90. { X86_VENDOR_INTEL, 5, 9 }, /* Intel Quark SoC X1000. */
  91. {}
  92. };
  93. /**
  94. * imr_self_test_init - entry point for IMR driver.
  95. *
  96. * return: -ENODEV for no IMR support 0 if good to go.
  97. */
  98. static int __init imr_self_test_init(void)
  99. {
  100. if (x86_match_cpu(imr_ids))
  101. imr_self_test();
  102. return 0;
  103. }
  104. /**
  105. * imr_self_test_exit - exit point for IMR code.
  106. *
  107. * return:
  108. */
  109. device_initcall(imr_self_test_init);