tzc400.c 6.49 KB
Newer Older
1
/*
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
2
 * Copyright (c) 2016-2018, ARM Limited and Contributors. All rights reserved.
3
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
5
6
7
8
9
10
11
 */

#include <assert.h>
#include <debug.h>
#include <mmio.h>
#include <stddef.h>
#include <tzc400.h>
12
#include "tzc_common_private.h"
13
14
15
16

/*
 * Macros which will be used by common core functions.
 */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
17
18
19
20
21
22
#define TZC_400_REGION_BASE_LOW_0_OFFSET	U(0x100)
#define TZC_400_REGION_BASE_HIGH_0_OFFSET	U(0x104)
#define TZC_400_REGION_TOP_LOW_0_OFFSET		U(0x108)
#define TZC_400_REGION_TOP_HIGH_0_OFFSET	U(0x10c)
#define TZC_400_REGION_ATTR_0_OFFSET		U(0x110)
#define TZC_400_REGION_ID_ACCESS_0_OFFSET	U(0x114)
23
24
25
26
27
28
29
30
31
32
33
34
35
36

/*
 * Implementation defined values used to validate inputs later.
 * Filters : max of 4 ; 0 to 3
 * Regions : max of 9 ; 0 to 8
 * Address width : Values between 32 to 64
 */
typedef struct tzc400_instance {
	uintptr_t base;
	uint8_t addr_width;
	uint8_t num_filters;
	uint8_t num_regions;
} tzc400_instance_t;

Roberto Vargas's avatar
Roberto Vargas committed
37
static tzc400_instance_t tzc400;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

static inline unsigned int _tzc400_read_build_config(uintptr_t base)
{
	return mmio_read_32(base + BUILD_CONFIG_OFF);
}

static inline unsigned int _tzc400_read_gate_keeper(uintptr_t base)
{
	return mmio_read_32(base + GATE_KEEPER_OFF);
}

static inline void _tzc400_write_gate_keeper(uintptr_t base, unsigned int val)
{
	mmio_write_32(base + GATE_KEEPER_OFF, val);
}

/*
 * Get the open status information for all filter units.
 */
Daniel Boulby's avatar
Daniel Boulby committed
57
#define get_gate_keeper_os(_base)	((_tzc400_read_gate_keeper(_base) >>  \
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
					GATE_KEEPER_OS_SHIFT) &		\
					GATE_KEEPER_OS_MASK)


/* Define common core functions used across different TZC peripherals. */
DEFINE_TZC_COMMON_WRITE_ACTION(400, 400)
DEFINE_TZC_COMMON_WRITE_REGION_BASE(400, 400)
DEFINE_TZC_COMMON_WRITE_REGION_TOP(400, 400)
DEFINE_TZC_COMMON_WRITE_REGION_ATTRIBUTES(400, 400)
DEFINE_TZC_COMMON_WRITE_REGION_ID_ACCESS(400, 400)
DEFINE_TZC_COMMON_CONFIGURE_REGION0(400)
DEFINE_TZC_COMMON_CONFIGURE_REGION(400)

static unsigned int _tzc400_get_gate_keeper(uintptr_t base,
				unsigned int filter)
{
	unsigned int open_status;

	open_status = get_gate_keeper_os(base);

	return (open_status >> filter) & GATE_KEEPER_FILTER_MASK;
}

/* This function is not MP safe. */
static void _tzc400_set_gate_keeper(uintptr_t base,
				unsigned int filter,
				int val)
{
	unsigned int open_status;

	/* Upper half is current state. Lower half is requested state. */
	open_status = get_gate_keeper_os(base);

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
91
92
	if (val != 0)
		open_status |=  (1U << filter);
93
	else
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
94
		open_status &= ~(1U << filter);
95
96
97
98
99
100
101
102
103

	_tzc400_write_gate_keeper(base, (open_status & GATE_KEEPER_OR_MASK) <<
			      GATE_KEEPER_OR_SHIFT);

	/* Wait here until we see the change reflected in the TZC status. */
	while ((get_gate_keeper_os(base)) != open_status)
		;
}

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
104
void tzc400_set_action(unsigned int action)
105
{
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
106
	assert(tzc400.base != 0U);
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
	assert(action <= TZC_ACTION_ERR_INT);

	/*
	 * - Currently no handler is provided to trap an error via interrupt
	 *   or exception.
	 * - The interrupt action has not been tested.
	 */
	_tzc400_write_action(tzc400.base, action);
}

void tzc400_init(uintptr_t base)
{
#if DEBUG
	unsigned int tzc400_id;
#endif
	unsigned int tzc400_build;

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
124
	assert(base != 0U);
125
126
127
128
129
130
131
132
133
134
135
136
	tzc400.base = base;

#if DEBUG
	tzc400_id = _tzc_read_peripheral_id(base);
	if (tzc400_id != TZC_400_PERIPHERAL_ID) {
		ERROR("TZC-400 : Wrong device ID (0x%x).\n", tzc400_id);
		panic();
	}
#endif

	/* Save values we will use later. */
	tzc400_build = _tzc400_read_build_config(tzc400.base);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
137
138
139
140
141
142
	tzc400.num_filters = (uint8_t)((tzc400_build >> BUILD_CONFIG_NF_SHIFT) &
					BUILD_CONFIG_NF_MASK) + 1U;
	tzc400.addr_width  = (uint8_t)((tzc400_build >> BUILD_CONFIG_AW_SHIFT) &
					BUILD_CONFIG_AW_MASK) + 1U;
	tzc400.num_regions = (uint8_t)((tzc400_build >> BUILD_CONFIG_NR_SHIFT) &
					BUILD_CONFIG_NR_MASK) + 1U;
143
144
145
146
147
148
149
150
}

/*
 * `tzc400_configure_region0` is used to program region 0 into the TrustZone
 * controller. Region 0 covers the whole address space that is not mapped
 * to any other region, and is enabled on all filters; this cannot be
 * changed. This function only changes the access permissions.
 */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
151
void tzc400_configure_region0(unsigned int sec_attr,
152
153
			   unsigned int ns_device_access)
{
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
154
	assert(tzc400.base != 0U);
155
156
157
158
159
160
161
162
163
164
165
166
167
168
	assert(sec_attr <= TZC_REGION_S_RDWR);

	_tzc400_configure_region0(tzc400.base, sec_attr, ns_device_access);
}

/*
 * `tzc400_configure_region` is used to program regions into the TrustZone
 * controller. A region can be associated with more than one filter. The
 * associated filters are passed in as a bitmap (bit0 = filter0).
 * NOTE:
 * Region 0 is special; it is preferable to use tzc400_configure_region0
 * for this region (see comment for that function).
 */
void tzc400_configure_region(unsigned int filters,
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
169
			  unsigned int region,
170
171
			  unsigned long long region_base,
			  unsigned long long region_top,
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
172
			  unsigned int sec_attr,
173
174
			  unsigned int nsaid_permissions)
{
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
175
	assert(tzc400.base != 0U);
176
177

	/* Do range checks on filters and regions. */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
178
179
	assert(((filters >> tzc400.num_filters) == 0U) &&
	       (region < tzc400.num_regions));
180
181
182
183
184

	/*
	 * Do address range check based on TZC configuration. A 64bit address is
	 * the max and expected case.
	 */
185
	assert(((region_top <= _tzc_get_max_top_addr(tzc400.addr_width)) &&
186
187
188
		(region_base < region_top)));

	/* region_base and (region_top + 1) must be 4KB aligned */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
189
	assert(((region_base | (region_top + 1U)) & (4096U - 1U)) == 0U);
190
191
192
193
194
195
196
197
198
199
200
201
202

	assert(sec_attr <= TZC_REGION_S_RDWR);

	_tzc400_configure_region(tzc400.base, filters, region, region_base,
						region_top,
						sec_attr, nsaid_permissions);
}

void tzc400_enable_filters(void)
{
	unsigned int state;
	unsigned int filter;

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
203
	assert(tzc400.base != 0U);
204

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
205
	for (filter = 0U; filter < tzc400.num_filters; filter++) {
206
		state = _tzc400_get_gate_keeper(tzc400.base, filter);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
207
		if (state != 0U) {
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
208
209
			/*
			 * The TZC filter is already configured. Changing the
210
211
212
			 * programmer's view in an active system can cause
			 * unpredictable behavior therefore panic for now rather
			 * than try to determine whether this is safe in this
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
213
214
215
216
217
			 * instance.
			 *
			 * See the 'ARM (R) CoreLink TM TZC-400 TrustZone (R)
			 * Address Space Controller' Technical Reference Manual.
			 */
218
219
220
221
222
223
224
225
226
227
228
229
			ERROR("TZC-400 : Filter %d Gatekeeper already"
				" enabled.\n", filter);
			panic();
		}
		_tzc400_set_gate_keeper(tzc400.base, filter, 1);
	}
}

void tzc400_disable_filters(void)
{
	unsigned int filter;

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
230
	assert(tzc400.base != 0U);
231
232
233
234
235
236
237
238

	/*
	 * We don't do the same state check as above as the Gatekeepers are
	 * disabled after reset.
	 */
	for (filter = 0; filter < tzc400.num_filters; filter++)
		_tzc400_set_gate_keeper(tzc400.base, filter, 0);
}