1/**
2 * imr_selftest.c
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
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/module.h>
19#include <linux/types.h>
20
21#define SELFTEST KBUILD_MODNAME ": "
22/**
23 * imr_self_test_result - Print result string for self test.
24 *
25 * @res:	result code - true if test passed false otherwise.
26 * @fmt:	format string.
27 * ...		variadic argument list.
28 */
29static void __init imr_self_test_result(int res, const char *fmt, ...)
30{
31	va_list vlist;
32
33	/* Print pass/fail. */
34	if (res)
35		pr_info(SELFTEST "pass ");
36	else
37		pr_info(SELFTEST "fail ");
38
39	/* Print variable string. */
40	va_start(vlist, fmt);
41	vprintk(fmt, vlist);
42	va_end(vlist);
43
44	/* Optional warning. */
45	WARN(res == 0, "test failed");
46}
47#undef SELFTEST
48
49/**
50 * imr_self_test
51 *
52 * Verify IMR self_test with some simple tests to verify overlap,
53 * zero sized allocations and 1 KiB sized areas.
54 *
55 */
56static void __init imr_self_test(void)
57{
58	phys_addr_t base  = virt_to_phys(&_text);
59	size_t size = virt_to_phys(&__end_rodata) - base;
60	const char *fmt_over = "overlapped IMR @ (0x%08lx - 0x%08lx)\n";
61	int ret;
62
63	/* Test zero zero. */
64	ret = imr_add_range(0, 0, 0, 0, false);
65	imr_self_test_result(ret < 0, "zero sized IMR\n");
66
67	/* Test exact overlap. */
68	ret = imr_add_range(base, size, IMR_CPU, IMR_CPU, false);
69	imr_self_test_result(ret < 0, fmt_over, __va(base), __va(base + size));
70
71	/* Test overlap with base inside of existing. */
72	base += size - IMR_ALIGN;
73	ret = imr_add_range(base, size, IMR_CPU, IMR_CPU, false);
74	imr_self_test_result(ret < 0, fmt_over, __va(base), __va(base + size));
75
76	/* Test overlap with end inside of existing. */
77	base -= size + IMR_ALIGN * 2;
78	ret = imr_add_range(base, size, IMR_CPU, IMR_CPU, false);
79	imr_self_test_result(ret < 0, fmt_over, __va(base), __va(base + size));
80
81	/* Test that a 1 KiB IMR @ zero with read/write all will bomb out. */
82	ret = imr_add_range(0, IMR_ALIGN, IMR_READ_ACCESS_ALL,
83			    IMR_WRITE_ACCESS_ALL, false);
84	imr_self_test_result(ret < 0, "1KiB IMR @ 0x00000000 - access-all\n");
85
86	/* Test that a 1 KiB IMR @ zero with CPU only will work. */
87	ret = imr_add_range(0, IMR_ALIGN, IMR_CPU, IMR_CPU, false);
88	imr_self_test_result(ret >= 0, "1KiB IMR @ 0x00000000 - cpu-access\n");
89	if (ret >= 0) {
90		ret = imr_remove_range(0, IMR_ALIGN);
91		imr_self_test_result(ret == 0, "teardown - cpu-access\n");
92	}
93
94	/* Test 2 KiB works. */
95	size = IMR_ALIGN * 2;
96	ret = imr_add_range(0, size, IMR_READ_ACCESS_ALL,
97			    IMR_WRITE_ACCESS_ALL, false);
98	imr_self_test_result(ret >= 0, "2KiB IMR @ 0x00000000\n");
99	if (ret >= 0) {
100		ret = imr_remove_range(0, size);
101		imr_self_test_result(ret == 0, "teardown 2KiB\n");
102	}
103}
104
105static const struct x86_cpu_id imr_ids[] __initconst = {
106	{ X86_VENDOR_INTEL, 5, 9 },	/* Intel Quark SoC X1000. */
107	{}
108};
109MODULE_DEVICE_TABLE(x86cpu, imr_ids);
110
111/**
112 * imr_self_test_init - entry point for IMR driver.
113 *
114 * return: -ENODEV for no IMR support 0 if good to go.
115 */
116static int __init imr_self_test_init(void)
117{
118	if (x86_match_cpu(imr_ids))
119		imr_self_test();
120	return 0;
121}
122
123/**
124 * imr_self_test_exit - exit point for IMR code.
125 *
126 * return:
127 */
128static void __exit imr_self_test_exit(void)
129{
130}
131
132module_init(imr_self_test_init);
133module_exit(imr_self_test_exit);
134
135MODULE_AUTHOR("Bryan O'Donoghue <pure.logic@nexus-software.ie>");
136MODULE_DESCRIPTION("Intel Isolated Memory Region self-test driver");
137MODULE_LICENSE("Dual BSD/GPL");
138