imr_selftest.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 its 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 <asm/io.h>
  17. #include <linux/init.h>
  18. #include <linux/mm.h>
  19. #include <linux/types.h>
  20. #define SELFTEST KBUILD_MODNAME ": "
  21. /**
  22. * imr_self_test_result - Print result string for self test.
  23. *
  24. * @res: result code - true if test passed false otherwise.
  25. * @fmt: format string.
  26. * ... variadic argument list.
  27. */
  28. static __printf(2, 3)
  29. void __init imr_self_test_result(int res, const char *fmt, ...)
  30. {
  31. va_list vlist;
  32. /* Print pass/fail. */
  33. if (res)
  34. pr_info(SELFTEST "pass ");
  35. else
  36. pr_info(SELFTEST "fail ");
  37. /* Print variable string. */
  38. va_start(vlist, fmt);
  39. vprintk(fmt, vlist);
  40. va_end(vlist);
  41. /* Optional warning. */
  42. WARN(res == 0, "test failed");
  43. }
  44. #undef SELFTEST
  45. /**
  46. * imr_self_test
  47. *
  48. * Verify IMR self_test with some simple tests to verify overlap,
  49. * zero sized allocations and 1 KiB sized areas.
  50. *
  51. */
  52. static void __init imr_self_test(void)
  53. {
  54. phys_addr_t base = virt_to_phys(&_text);
  55. size_t size = virt_to_phys(&__end_rodata) - base;
  56. const char *fmt_over = "overlapped IMR @ (0x%08lx - 0x%08lx)\n";
  57. int ret;
  58. /* Test zero zero. */
  59. ret = imr_add_range(0, 0, 0, 0);
  60. imr_self_test_result(ret < 0, "zero sized IMR\n");
  61. /* Test exact overlap. */
  62. ret = imr_add_range(base, size, IMR_CPU, IMR_CPU);
  63. imr_self_test_result(ret < 0, fmt_over, __va(base), __va(base + size));
  64. /* Test overlap with base inside of existing. */
  65. base += size - IMR_ALIGN;
  66. ret = imr_add_range(base, size, IMR_CPU, IMR_CPU);
  67. imr_self_test_result(ret < 0, fmt_over, __va(base), __va(base + size));
  68. /* Test overlap with end inside of existing. */
  69. base -= size + IMR_ALIGN * 2;
  70. ret = imr_add_range(base, size, IMR_CPU, IMR_CPU);
  71. imr_self_test_result(ret < 0, fmt_over, __va(base), __va(base + size));
  72. /* Test that a 1 KiB IMR @ zero with read/write all will bomb out. */
  73. ret = imr_add_range(0, IMR_ALIGN, IMR_READ_ACCESS_ALL,
  74. IMR_WRITE_ACCESS_ALL);
  75. imr_self_test_result(ret < 0, "1KiB IMR @ 0x00000000 - access-all\n");
  76. /* Test that a 1 KiB IMR @ zero with CPU only will work. */
  77. ret = imr_add_range(0, IMR_ALIGN, IMR_CPU, IMR_CPU);
  78. imr_self_test_result(ret >= 0, "1KiB IMR @ 0x00000000 - cpu-access\n");
  79. if (ret >= 0) {
  80. ret = imr_remove_range(0, IMR_ALIGN);
  81. imr_self_test_result(ret == 0, "teardown - cpu-access\n");
  82. }
  83. /* Test 2 KiB works. */
  84. size = IMR_ALIGN * 2;
  85. ret = imr_add_range(0, size, IMR_READ_ACCESS_ALL, IMR_WRITE_ACCESS_ALL);
  86. imr_self_test_result(ret >= 0, "2KiB IMR @ 0x00000000\n");
  87. if (ret >= 0) {
  88. ret = imr_remove_range(0, size);
  89. imr_self_test_result(ret == 0, "teardown 2KiB\n");
  90. }
  91. }
  92. static const struct x86_cpu_id imr_ids[] __initconst = {
  93. X86_MATCH_VENDOR_FAM_MODEL(INTEL, 5, INTEL_FAM5_QUARK_X1000, NULL),
  94. {}
  95. };
  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. device_initcall(imr_self_test_init);