18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/**
38c2ecf20Sopenharmony_ci * imr_selftest.c -- Intel Isolated Memory Region self-test driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright(c) 2013 Intel Corporation.
68c2ecf20Sopenharmony_ci * Copyright(c) 2015 Bryan O'Donoghue <pure.logic@nexus-software.ie>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * IMR self test. The purpose of this module is to run a set of tests on the
98c2ecf20Sopenharmony_ci * IMR API to validate it's sanity. We check for overlapping, reserved
108c2ecf20Sopenharmony_ci * addresses and setup/teardown sanity.
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <asm-generic/sections.h>
158c2ecf20Sopenharmony_ci#include <asm/cpu_device_id.h>
168c2ecf20Sopenharmony_ci#include <asm/imr.h>
178c2ecf20Sopenharmony_ci#include <asm/io.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <linux/init.h>
208c2ecf20Sopenharmony_ci#include <linux/mm.h>
218c2ecf20Sopenharmony_ci#include <linux/types.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define SELFTEST KBUILD_MODNAME ": "
248c2ecf20Sopenharmony_ci/**
258c2ecf20Sopenharmony_ci * imr_self_test_result - Print result string for self test.
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci * @res:	result code - true if test passed false otherwise.
288c2ecf20Sopenharmony_ci * @fmt:	format string.
298c2ecf20Sopenharmony_ci * ...		variadic argument list.
308c2ecf20Sopenharmony_ci */
318c2ecf20Sopenharmony_cistatic __printf(2, 3)
328c2ecf20Sopenharmony_civoid __init imr_self_test_result(int res, const char *fmt, ...)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	va_list vlist;
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	/* Print pass/fail. */
378c2ecf20Sopenharmony_ci	if (res)
388c2ecf20Sopenharmony_ci		pr_info(SELFTEST "pass ");
398c2ecf20Sopenharmony_ci	else
408c2ecf20Sopenharmony_ci		pr_info(SELFTEST "fail ");
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	/* Print variable string. */
438c2ecf20Sopenharmony_ci	va_start(vlist, fmt);
448c2ecf20Sopenharmony_ci	vprintk(fmt, vlist);
458c2ecf20Sopenharmony_ci	va_end(vlist);
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	/* Optional warning. */
488c2ecf20Sopenharmony_ci	WARN(res == 0, "test failed");
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci#undef SELFTEST
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/**
538c2ecf20Sopenharmony_ci * imr_self_test
548c2ecf20Sopenharmony_ci *
558c2ecf20Sopenharmony_ci * Verify IMR self_test with some simple tests to verify overlap,
568c2ecf20Sopenharmony_ci * zero sized allocations and 1 KiB sized areas.
578c2ecf20Sopenharmony_ci *
588c2ecf20Sopenharmony_ci */
598c2ecf20Sopenharmony_cistatic void __init imr_self_test(void)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	phys_addr_t base  = virt_to_phys(&_text);
628c2ecf20Sopenharmony_ci	size_t size = virt_to_phys(&__end_rodata) - base;
638c2ecf20Sopenharmony_ci	const char *fmt_over = "overlapped IMR @ (0x%08lx - 0x%08lx)\n";
648c2ecf20Sopenharmony_ci	int ret;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	/* Test zero zero. */
678c2ecf20Sopenharmony_ci	ret = imr_add_range(0, 0, 0, 0);
688c2ecf20Sopenharmony_ci	imr_self_test_result(ret < 0, "zero sized IMR\n");
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	/* Test exact overlap. */
718c2ecf20Sopenharmony_ci	ret = imr_add_range(base, size, IMR_CPU, IMR_CPU);
728c2ecf20Sopenharmony_ci	imr_self_test_result(ret < 0, fmt_over, __va(base), __va(base + size));
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	/* Test overlap with base inside of existing. */
758c2ecf20Sopenharmony_ci	base += size - IMR_ALIGN;
768c2ecf20Sopenharmony_ci	ret = imr_add_range(base, size, IMR_CPU, IMR_CPU);
778c2ecf20Sopenharmony_ci	imr_self_test_result(ret < 0, fmt_over, __va(base), __va(base + size));
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	/* Test overlap with end inside of existing. */
808c2ecf20Sopenharmony_ci	base -= size + IMR_ALIGN * 2;
818c2ecf20Sopenharmony_ci	ret = imr_add_range(base, size, IMR_CPU, IMR_CPU);
828c2ecf20Sopenharmony_ci	imr_self_test_result(ret < 0, fmt_over, __va(base), __va(base + size));
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	/* Test that a 1 KiB IMR @ zero with read/write all will bomb out. */
858c2ecf20Sopenharmony_ci	ret = imr_add_range(0, IMR_ALIGN, IMR_READ_ACCESS_ALL,
868c2ecf20Sopenharmony_ci			    IMR_WRITE_ACCESS_ALL);
878c2ecf20Sopenharmony_ci	imr_self_test_result(ret < 0, "1KiB IMR @ 0x00000000 - access-all\n");
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	/* Test that a 1 KiB IMR @ zero with CPU only will work. */
908c2ecf20Sopenharmony_ci	ret = imr_add_range(0, IMR_ALIGN, IMR_CPU, IMR_CPU);
918c2ecf20Sopenharmony_ci	imr_self_test_result(ret >= 0, "1KiB IMR @ 0x00000000 - cpu-access\n");
928c2ecf20Sopenharmony_ci	if (ret >= 0) {
938c2ecf20Sopenharmony_ci		ret = imr_remove_range(0, IMR_ALIGN);
948c2ecf20Sopenharmony_ci		imr_self_test_result(ret == 0, "teardown - cpu-access\n");
958c2ecf20Sopenharmony_ci	}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	/* Test 2 KiB works. */
988c2ecf20Sopenharmony_ci	size = IMR_ALIGN * 2;
998c2ecf20Sopenharmony_ci	ret = imr_add_range(0, size, IMR_READ_ACCESS_ALL, IMR_WRITE_ACCESS_ALL);
1008c2ecf20Sopenharmony_ci	imr_self_test_result(ret >= 0, "2KiB IMR @ 0x00000000\n");
1018c2ecf20Sopenharmony_ci	if (ret >= 0) {
1028c2ecf20Sopenharmony_ci		ret = imr_remove_range(0, size);
1038c2ecf20Sopenharmony_ci		imr_self_test_result(ret == 0, "teardown 2KiB\n");
1048c2ecf20Sopenharmony_ci	}
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic const struct x86_cpu_id imr_ids[] __initconst = {
1088c2ecf20Sopenharmony_ci	X86_MATCH_VENDOR_FAM_MODEL(INTEL, 5, INTEL_FAM5_QUARK_X1000, NULL),
1098c2ecf20Sopenharmony_ci	{}
1108c2ecf20Sopenharmony_ci};
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci/**
1138c2ecf20Sopenharmony_ci * imr_self_test_init - entry point for IMR driver.
1148c2ecf20Sopenharmony_ci *
1158c2ecf20Sopenharmony_ci * return: -ENODEV for no IMR support 0 if good to go.
1168c2ecf20Sopenharmony_ci */
1178c2ecf20Sopenharmony_cistatic int __init imr_self_test_init(void)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	if (x86_match_cpu(imr_ids))
1208c2ecf20Sopenharmony_ci		imr_self_test();
1218c2ecf20Sopenharmony_ci	return 0;
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci/**
1258c2ecf20Sopenharmony_ci * imr_self_test_exit - exit point for IMR code.
1268c2ecf20Sopenharmony_ci *
1278c2ecf20Sopenharmony_ci * return:
1288c2ecf20Sopenharmony_ci */
1298c2ecf20Sopenharmony_cidevice_initcall(imr_self_test_init);
130