4 * Copyright(c) 2013 Intel Corporation.
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.
13 #include <asm-generic/sections.h>
15 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/types.h>
20 #define SELFTEST KBUILD_MODNAME ": "
22 * imr_self_test_result - Print result string for self test.
24 * @res: result code - true if test passed false otherwise.
25 * @fmt: format string.
26 * ... variadic argument list.
28 static void __init imr_self_test_result(int res, const char *fmt, ...)
32 /* Print pass/fail. */
34 pr_info(SELFTEST "pass ");
36 pr_info(SELFTEST "fail ");
38 /* Print variable string. */
43 /* Optional warning. */
44 WARN(res == 0, "test failed");
51 * Verify IMR self_test with some simple tests to verify overlap,
52 * zero sized allocations and 1 KiB sized areas.
55 static void __init imr_self_test(void)
57 phys_addr_t base = virt_to_phys(&_text);
58 size_t size = virt_to_phys(&__end_rodata) - base;
59 const char *fmt_over = "overlapped IMR @ (0x%08lx - 0x%08lx)\n";
63 ret = imr_add_range(0, 0, 0, 0, false);
64 imr_self_test_result(ret < 0, "zero sized IMR\n");
66 /* Test exact overlap. */
67 ret = imr_add_range(base, size, IMR_CPU, IMR_CPU, false);
68 imr_self_test_result(ret < 0, fmt_over, __va(base), __va(base + size));
70 /* Test overlap with base inside of existing. */
71 base += size - IMR_ALIGN;
72 ret = imr_add_range(base, size, IMR_CPU, IMR_CPU, false);
73 imr_self_test_result(ret < 0, fmt_over, __va(base), __va(base + size));
75 /* Test overlap with end inside of existing. */
76 base -= size + IMR_ALIGN * 2;
77 ret = imr_add_range(base, size, IMR_CPU, IMR_CPU, false);
78 imr_self_test_result(ret < 0, fmt_over, __va(base), __va(base + size));
80 /* Test that a 1 KiB IMR @ zero with read/write all will bomb out. */
81 ret = imr_add_range(0, IMR_ALIGN, IMR_READ_ACCESS_ALL,
82 IMR_WRITE_ACCESS_ALL, false);
83 imr_self_test_result(ret < 0, "1KiB IMR @ 0x00000000 - access-all\n");
85 /* Test that a 1 KiB IMR @ zero with CPU only will work. */
86 ret = imr_add_range(0, IMR_ALIGN, IMR_CPU, IMR_CPU, false);
87 imr_self_test_result(ret >= 0, "1KiB IMR @ 0x00000000 - cpu-access\n");
89 ret = imr_remove_range(0, IMR_ALIGN);
90 imr_self_test_result(ret == 0, "teardown - cpu-access\n");
93 /* Test 2 KiB works. */
95 ret = imr_add_range(0, size, IMR_READ_ACCESS_ALL,
96 IMR_WRITE_ACCESS_ALL, false);
97 imr_self_test_result(ret >= 0, "2KiB IMR @ 0x00000000\n");
99 ret = imr_remove_range(0, size);
100 imr_self_test_result(ret == 0, "teardown 2KiB\n");
105 * imr_self_test_init - entry point for IMR driver.
107 * return: -ENODEV for no IMR support 0 if good to go.
109 static int __init imr_self_test_init(void)
116 * imr_self_test_exit - exit point for IMR code.
120 static void __exit imr_self_test_exit(void)
124 module_init(imr_self_test_init);
125 module_exit(imr_self_test_exit);
128 MODULE_DESCRIPTION("Intel Isolated Memory Region self-test driver");
129 MODULE_LICENSE("Dual BSD/GPL");