imr_selftest.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // SPDX-License-Identifier: GPL-2.0
  2. /**
  3. * imr_selftest.c -- Intel Isolated Memory Region self-test driver
  4. *
  5. * Copyright(c) 2013 Intel Corporation.
  6. * Copyright(c) 2015 Bryan O'Donoghue <pure.logic@nexus-software.ie>
  7. *
  8. * IMR self test. The purpose of this module is to run a set of tests on the
  9. * IMR API to validate it's sanity. We check for overlapping, reserved
  10. * addresses and setup/teardown sanity.
  11. *
  12. */
  13. #include <asm-generic/sections.h>
  14. #include <asm/cpu_device_id.h>
  15. #include <asm/imr.h>
  16. #include <linux/init.h>
  17. #include <linux/mm.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 __printf(2, 3)
  28. void __init imr_self_test_result(int res, const char *fmt, ...)
  29. {
  30. va_list vlist;
  31. /* Print pass/fail. */
  32. if (res)
  33. pr_info(SELFTEST "pass ");
  34. else
  35. pr_info(SELFTEST "fail ");
  36. /* Print variable string. */
  37. va_start(vlist, fmt);
  38. vprintk(fmt, vlist);
  39. va_end(vlist);
  40. /* Optional warning. */
  41. WARN(res == 0, "test failed");
  42. }
  43. #undef SELFTEST
  44. /**
  45. * imr_self_test
  46. *
  47. * Verify IMR self_test with some simple tests to verify overlap,
  48. * zero sized allocations and 1 KiB sized areas.
  49. *
  50. */
  51. static void __init imr_self_test(void)
  52. {
  53. phys_addr_t base = virt_to_phys(&_text);
  54. size_t size = virt_to_phys(&__end_rodata) - base;
  55. const char *fmt_over = "overlapped IMR @ (0x%08lx - 0x%08lx)\n";
  56. int ret;
  57. /* Test zero zero. */
  58. ret = imr_add_range(0, 0, 0, 0);
  59. imr_self_test_result(ret < 0, "zero sized IMR\n");
  60. /* Test exact overlap. */
  61. ret = imr_add_range(base, size, IMR_CPU, IMR_CPU);
  62. imr_self_test_result(ret < 0, fmt_over, __va(base), __va(base + size));
  63. /* Test overlap with base inside of existing. */
  64. base += size - IMR_ALIGN;
  65. ret = imr_add_range(base, size, IMR_CPU, IMR_CPU);
  66. imr_self_test_result(ret < 0, fmt_over, __va(base), __va(base + size));
  67. /* Test overlap with end inside of existing. */
  68. base -= size + IMR_ALIGN * 2;
  69. ret = imr_add_range(base, size, IMR_CPU, IMR_CPU);
  70. imr_self_test_result(ret < 0, fmt_over, __va(base), __va(base + size));
  71. /* Test that a 1 KiB IMR @ zero with read/write all will bomb out. */
  72. ret = imr_add_range(0, IMR_ALIGN, IMR_READ_ACCESS_ALL,
  73. IMR_WRITE_ACCESS_ALL);
  74. imr_self_test_result(ret < 0, "1KiB IMR @ 0x00000000 - access-all\n");
  75. /* Test that a 1 KiB IMR @ zero with CPU only will work. */
  76. ret = imr_add_range(0, IMR_ALIGN, IMR_CPU, IMR_CPU);
  77. imr_self_test_result(ret >= 0, "1KiB IMR @ 0x00000000 - cpu-access\n");
  78. if (ret >= 0) {
  79. ret = imr_remove_range(0, IMR_ALIGN);
  80. imr_self_test_result(ret == 0, "teardown - cpu-access\n");
  81. }
  82. /* Test 2 KiB works. */
  83. size = IMR_ALIGN * 2;
  84. ret = imr_add_range(0, size, IMR_READ_ACCESS_ALL, IMR_WRITE_ACCESS_ALL);
  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. /**
  96. * imr_self_test_init - entry point for IMR driver.
  97. *
  98. * return: -ENODEV for no IMR support 0 if good to go.
  99. */
  100. static int __init imr_self_test_init(void)
  101. {
  102. if (x86_match_cpu(imr_ids))
  103. imr_self_test();
  104. return 0;
  105. }
  106. /**
  107. * imr_self_test_exit - exit point for IMR code.
  108. *
  109. * return:
  110. */
  111. device_initcall(imr_self_test_init);