qemu_spm.c 3.97 KB
Newer Older
1
2
3
4
5
/* SPDX-License-Identifier: BSD-3-Clause
 *
 * Copyright (c) 2020, Linaro Limited and Contributors. All rights reserved.
 */

6
7
#include <libfdt.h>

8
#include <bl31/ehf.h>
9
10
11
#include <common/debug.h>
#include <common/fdt_fixup.h>
#include <common/fdt_wrappers.h>
12
13
14
15
16
17
18
19
20
21
#include <lib/xlat_tables/xlat_tables_compat.h>
#include <services/spm_mm_partition.h>

#include <platform_def.h>

/* Region equivalent to MAP_DEVICE1 suitable for mapping at EL0 */
#define MAP_DEVICE1_EL0	MAP_REGION_FLAT(DEVICE1_BASE,			\
					DEVICE1_SIZE,			\
					MT_DEVICE | MT_RW | MT_SECURE | MT_USER)

22
23
24
mmap_region_t plat_qemu_secure_partition_mmap[] = {
	QEMU_SP_IMAGE_NS_BUF_MMAP,	/* must be placed at first entry */
	MAP_DEVICE1_EL0,		/* for the UART */
25
26
27
	QEMU_SP_IMAGE_MMAP,
	QEMU_SPM_BUF_EL0_MMAP,
	QEMU_SP_IMAGE_RW_MMAP,
28
	MAP_SECURE_VARSTORE,
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
	{0}
};

/*
 * Boot information passed to a secure partition during initialisation.
 * Linear indices in MP information will be filled at runtime.
 */
static spm_mm_mp_info_t sp_mp_info[] = {
	[0] = {0x80000000, 0},
	[1] = {0x80000001, 0},
	[2] = {0x80000002, 0},
	[3] = {0x80000003, 0},
	[4] = {0x80000004, 0},
	[5] = {0x80000005, 0},
	[6] = {0x80000006, 0},
	[7] = {0x80000007, 0}
};

47
spm_mm_boot_info_t plat_qemu_secure_partition_boot_info = {
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
	.h.type              = PARAM_SP_IMAGE_BOOT_INFO,
	.h.version           = VERSION_1,
	.h.size              = sizeof(spm_mm_boot_info_t),
	.h.attr              = 0,
	.sp_mem_base         = PLAT_QEMU_SP_IMAGE_BASE,
	.sp_mem_limit        = BL32_LIMIT,
	.sp_image_base       = PLAT_QEMU_SP_IMAGE_BASE,
	.sp_stack_base       = PLAT_SP_IMAGE_STACK_BASE,
	.sp_heap_base        = PLAT_QEMU_SP_IMAGE_HEAP_BASE,
	.sp_ns_comm_buf_base = PLAT_QEMU_SP_IMAGE_NS_BUF_BASE,
	.sp_shared_buf_base  = PLAT_SPM_BUF_BASE,
	.sp_image_size       = PLAT_QEMU_SP_IMAGE_SIZE,
	.sp_pcpu_stack_size  = PLAT_SP_IMAGE_STACK_PCPU_SIZE,
	.sp_heap_size        = PLAT_QEMU_SP_IMAGE_HEAP_SIZE,
	.sp_ns_comm_buf_size = PLAT_QEMU_SP_IMAGE_NS_BUF_SIZE,
	.sp_shared_buf_size  = PLAT_SPM_BUF_SIZE,
	.num_sp_mem_regions  = PLAT_QEMU_SP_IMAGE_NUM_MEM_REGIONS,
	.num_cpus            = PLATFORM_CORE_COUNT,
	.mp_info             = sp_mp_info
};

/* Enumeration of priority levels on QEMU platforms. */
ehf_pri_desc_t qemu_exceptions[] = {
	EHF_PRI_DESC(QEMU_PRI_BITS, PLAT_SP_PRI)
};

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
int dt_add_ns_buf_node(uintptr_t *base)
{
	uintptr_t addr;
	size_t size;
	uintptr_t ns_buf_addr;
	int node;
	int err;
	void *fdt = (void *)ARM_PRELOADED_DTB_BASE;

	err = fdt_open_into(fdt, fdt, PLAT_QEMU_DT_MAX_SIZE);
	if (err < 0) {
		ERROR("Invalid Device Tree at %p: error %d\n", fdt, err);
		return err;
	}

	/*
	 * reserved-memory for standaloneMM non-secure buffer
	 * is allocated at the top of the first system memory region.
	 */
	node = fdt_path_offset(fdt, "/memory");

	err = fdt_get_reg_props_by_index(fdt, node, 0, &addr, &size);
	if (err < 0) {
		ERROR("Failed to get the memory node information\n");
		return err;
	}
	INFO("System RAM @ 0x%lx - 0x%lx\n", addr, addr + size - 1);

	ns_buf_addr = addr + (size - PLAT_QEMU_SP_IMAGE_NS_BUF_SIZE);
	INFO("reserved-memory for spm-mm @ 0x%lx - 0x%llx\n", ns_buf_addr,
	     ns_buf_addr + PLAT_QEMU_SP_IMAGE_NS_BUF_SIZE - 1);

	err = fdt_add_reserved_memory(fdt, "ns-buf-spm-mm", ns_buf_addr,
				      PLAT_QEMU_SP_IMAGE_NS_BUF_SIZE);
	if (err < 0) {
		ERROR("Failed to add the reserved-memory node\n");
		return err;
	}

	*base = ns_buf_addr;
	return 0;
}

117
118
119
120
121
122
/* Plug in QEMU exceptions to Exception Handling Framework. */
EHF_REGISTER_PRIORITIES(qemu_exceptions, ARRAY_SIZE(qemu_exceptions),
			QEMU_PRI_BITS);

const mmap_region_t *plat_get_secure_partition_mmap(void *cookie)
{
123
124
125
126
127
128
129
130
	uintptr_t ns_buf_base;

	dt_add_ns_buf_node(&ns_buf_base);

	plat_qemu_secure_partition_mmap[0].base_pa = ns_buf_base;
	plat_qemu_secure_partition_mmap[0].base_va = ns_buf_base;
	plat_qemu_secure_partition_boot_info.sp_ns_comm_buf_base = ns_buf_base;

131
132
133
134
135
136
137
138
	return plat_qemu_secure_partition_mmap;
}

const spm_mm_boot_info_t *
plat_get_secure_partition_boot_info(void *cookie)
{
	return &plat_qemu_secure_partition_boot_info;
}