aboutsummaryrefslogtreecommitdiff
path: root/tftf/tests/plat/nvidia/common/test_sip.c
blob: 0fb86faa8ca9fd88d42712253766c40e5e765a16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <arch_helpers.h>
#include <debug.h>
#include <tftf_lib.h>
#include <xlat_tables_v2.h>

#include <platform_def.h>

/*******************************************************************************
 * Common Tegra SiP SMCs
 ******************************************************************************/
#define TEGRA_SIP_NEW_VIDEOMEM_REGION		0x82000003ULL

/*
 * @Test_Aim@ Test to issue VideoMem SiP SMC function IDs.
 *
 * This test runs on the lead CPU and issues TEGRA_SIP_NEW_VIDEOMEM_REGION
 * SMC to resize the memory region.
 */
test_result_t test_sip_videomem_resize(void)
{
	uint32_t size_in_bytes = (4U << 20);
	uint32_t offset = (8U << 20);
	uint64_t vidmem_base = DRAM_END, mem;
	uint64_t buf[] = { 0xCAFEBABE, 0xCAFEBABE, 0xCAFEBABE, 0xCAFEBABE };
	smc_args args = { TEGRA_SIP_NEW_VIDEOMEM_REGION, vidmem_base, size_in_bytes };
	smc_ret_values ret;
	test_result_t result = TEST_RESULT_SUCCESS;
	int err;

	/* Map dummy memory region for the test */
	err = mmap_add_dynamic_region(vidmem_base, vidmem_base, size_in_bytes << 2,
		MT_DEVICE | MT_RW | MT_NS | MT_EXECUTE_NEVER);
	if (err != 0) {
		tftf_testcase_printf("%s: could not map memory (%d)\n", __func__, err);
		return TEST_RESULT_FAIL;
	}

	/* copy sample data before setting up the memory protections */
	memcpy((char *)vidmem_base, (void *)buf, sizeof(buf));
	flush_dcache_range(vidmem_base, sizeof(buf));

	/* Issue the SMC to program videomem and expect success */
	ret = tftf_smc(&args);
	if (ret.ret0 != 0UL) {
		tftf_testcase_printf("%s failed. Expected 0, received %ld\n",
			__func__, (long int)ret.ret0);
		result = TEST_RESULT_FAIL;
		goto exit;
	}

	/* copy sample data before setting up the memory protections */
	memcpy((char *)vidmem_base + offset, (void *)buf, sizeof(buf));
	flush_dcache_range(vidmem_base + offset, sizeof(buf));

	/* Issue request to "move" the protected memory region */
	args.arg1 = vidmem_base + offset;
	args.arg2 = (u_register_t)size_in_bytes;
	ret = tftf_smc(&args);
	if (ret.ret0 != 0UL) {
		tftf_testcase_printf("%s failed. Expected 0, received %ld\n",
			__func__, (long int)ret.ret0);
		result = TEST_RESULT_FAIL;
		goto exit;
	}

	/* Verify that memory in the open has been cleared */
	mem = vidmem_base;
	for (unsigned int i = 0U; i < (size_in_bytes / 8); i++, mem += 8) {
		if (*(uint64_t *)(void *)mem != 0ULL) {
			tftf_testcase_printf("%s failed. Memory is non-zero (%llx:%llx)\n",
				__func__, mem, *(uint64_t *)(void *)mem);
			result = TEST_RESULT_FAIL;
			goto exit;
		}
	}

	/* copy sample data before setting up the memory protections */
	memcpy((char *)vidmem_base, (void *)buf, sizeof(buf));
	flush_dcache_range(vidmem_base, sizeof(buf));

	/* Issue request to "move" the protected memory region */
	args.arg1 = vidmem_base;
	args.arg2 = (u_register_t)size_in_bytes;
	ret = tftf_smc(&args);
	if (ret.ret0 != 0UL) {
		tftf_testcase_printf("%s failed. Expected 0, received %ld\n",
			__func__, (long int)ret.ret0);
		result = TEST_RESULT_FAIL;
		goto exit;
	}

	/* Verify that memory in the open has been cleared */
	mem = vidmem_base + offset;
	for (unsigned int i = 0U; i < (size_in_bytes / 8); i++, mem += 8) {
		if (*(uint64_t *)(void *)mem != 0U) {
			tftf_testcase_printf("%s failed. Memory is non-zero (%llx:%llx)\n",
				__func__, mem, *(uint64_t *)(void *)mem);
			result = TEST_RESULT_FAIL;
			goto exit;
		}
	}

exit:
	/* unmap dummy memory region */
	err = mmap_remove_dynamic_region(vidmem_base, size_in_bytes << 2);
	if (err != 0) {
		tftf_testcase_printf("%s: could not unmap memory (%d)\n",
			__func__, err);
		result = TEST_RESULT_FAIL;
	}

	return result;
}

/*
 * @Test_Aim@ Test to issue common SiP SMC function IDs.
 *
 * This test runs on the lead CPU and issues TEGRA_SIP_NEW_VIDEOMEM_REGION
 * and tests positive and negative scenarios.
 */
test_result_t test_sip_videomem_incorrect_inputs(void)
{
	smc_args args = { TEGRA_SIP_NEW_VIDEOMEM_REGION };
	smc_ret_values ret;

	/* Issue the SMC with no input parameters and expect error */
	ret = tftf_smc(&args);
	if (ret.ret0 == 0UL) {
		tftf_testcase_printf("%s failed. Expected -1, received %ld\n",
			__func__, (long int)ret.ret0);
		return TEST_RESULT_FAIL;
	}

	/* Issue the SMC with incorrect parameters and expect error */
	args.arg1 = 0x10000000ULL;
	args.arg2 = 4ULL << 20;

	ret = tftf_smc(&args);
	if (ret.ret0 == 0UL) {
		tftf_testcase_printf("%s failed. Expected -1, received %ld\n",
			__func__, (long int)ret.ret0);
		return TEST_RESULT_FAIL;
	}

	/* Issue the SMC with incorrect parameters and expect error */
	args.arg1 = 0x40000000ULL;
	args.arg2 = 4ULL << 20;

	ret = tftf_smc(&args);
	if (ret.ret0 == 0UL) {
		tftf_testcase_printf("%s failed. Expected -1, received %ld\n",
			__func__, (long int)ret.ret0);
		return TEST_RESULT_FAIL;
	}

	/* Issue the SMC with incorrect parameters and expect error */
	args.arg1 = DRAM_END - (4U << 20);
	args.arg2 = 0x100ULL;

	ret = tftf_smc(&args);
	if (ret.ret0 == 0UL) {
		tftf_testcase_printf("%s failed. Expected -1, received %ld\n",
			__func__, (long int)ret.ret0);
		return TEST_RESULT_FAIL;
	}

	/* Issue the SMC with incorrect parameters and expect error */
	args.arg1 = DRAM_END - (4U << 20);
	args.arg2 = 0ULL;

	ret = tftf_smc(&args);
	if (ret.ret0 == 0UL) {
		tftf_testcase_printf("%s failed. Expected -1, received %ld\n",
			__func__, (long int)ret.ret0);
		return TEST_RESULT_FAIL;
	}

	/* Issue the SMC with incorrect parameters and expect error */
	args.arg1 = 0ULL;
	args.arg2 = 4ULL << 20;

	ret = tftf_smc(&args);
	if (ret.ret0 == 0UL) {
		tftf_testcase_printf("%s failed. Expected -1, received %ld\n",
			__func__, (long int)ret.ret0);
		return TEST_RESULT_FAIL;
	}

	return TEST_RESULT_SUCCESS;
}