tzc400.c 8.25 KB
Newer Older
Harry Liebel's avatar
Harry Liebel committed
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
/*
 * Copyright (c) 2014, ARM Limited and Contributors. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of ARM nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific
 * prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <assert.h>
32
#include <debug.h>
33
34
35
#include <mmio.h>
#include <stddef.h>
#include <tzc400.h>
Harry Liebel's avatar
Harry Liebel committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

static uint32_t tzc_read_build_config(uint64_t base)
{
	return mmio_read_32(base + BUILD_CONFIG_OFF);
}

static uint32_t tzc_read_gate_keeper(uint64_t base)
{
	return mmio_read_32(base + GATE_KEEPER_OFF);
}

static void tzc_write_gate_keeper(uint64_t base, uint32_t val)
{
	mmio_write_32(base + GATE_KEEPER_OFF, val);
}

52
static void tzc_write_action(uint64_t base, tzc_action_t action)
Harry Liebel's avatar
Harry Liebel committed
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
{
	mmio_write_32(base + ACTION_OFF, action);
}

static void tzc_write_region_base_low(uint64_t base, uint32_t region, uint32_t val)
{
	mmio_write_32(base + REGION_BASE_LOW_OFF + REGION_NUM_OFF(region), val);
}

static void tzc_write_region_base_high(uint64_t base, uint32_t region, uint32_t val)
{
	mmio_write_32(base + REGION_BASE_HIGH_OFF + REGION_NUM_OFF(region), val);
}

static void tzc_write_region_top_low(uint64_t base, uint32_t region, uint32_t val)
{
	mmio_write_32(base + REGION_TOP_LOW_OFF + REGION_NUM_OFF(region), val);
}

static void tzc_write_region_top_high(uint64_t base, uint32_t region, uint32_t val)
{
	mmio_write_32(base + REGION_TOP_HIGH_OFF + REGION_NUM_OFF(region), val);
}

static void tzc_write_region_attributes(uint64_t base, uint32_t region, uint32_t val)
{
	mmio_write_32(base + REGION_ATTRIBUTES_OFF + REGION_NUM_OFF(region), val);
}

static void tzc_write_region_id_access(uint64_t base, uint32_t region, uint32_t val)
{
	mmio_write_32(base + REGION_ID_ACCESS_OFF + REGION_NUM_OFF(region), val);
}

static uint32_t tzc_read_component_id(uint64_t base)
{
	uint32_t id;

	id = mmio_read_8(base + CID0_OFF);
	id |= (mmio_read_8(base + CID1_OFF) << 8);
	id |= (mmio_read_8(base + CID2_OFF) << 16);
	id |= (mmio_read_8(base + CID3_OFF) << 24);

	return id;
}

static uint32_t tzc_get_gate_keeper(uint64_t base, uint8_t filter)
{
	uint32_t tmp;

	tmp = (tzc_read_gate_keeper(base) >> GATE_KEEPER_OS_SHIFT) &
		GATE_KEEPER_OS_MASK;

106
	return (tmp >> filter) & GATE_KEEPER_FILTER_MASK;
Harry Liebel's avatar
Harry Liebel committed
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
}

/* This function is not MP safe. */
static void tzc_set_gate_keeper(uint64_t base, uint8_t filter, uint32_t val)
{
	uint32_t tmp;

	/* Upper half is current state. Lower half is requested state. */
	tmp = (tzc_read_gate_keeper(base) >> GATE_KEEPER_OS_SHIFT) &
		GATE_KEEPER_OS_MASK;

	if (val)
		tmp |=  (1 << filter);
	else
		tmp &= ~(1 << filter);

	tzc_write_gate_keeper(base, (tmp & GATE_KEEPER_OR_MASK) <<
			      GATE_KEEPER_OR_SHIFT);

	/* Wait here until we see the change reflected in the TZC status. */
	while (((tzc_read_gate_keeper(base) >> GATE_KEEPER_OS_SHIFT) &
		GATE_KEEPER_OS_MASK) != tmp)
	  ;
}


133
void tzc_init(tzc_instance_t *controller)
Harry Liebel's avatar
Harry Liebel committed
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
{
	uint32_t tzc_id, tzc_build;

	assert(controller != NULL);

	/*
	 * We expect to see a tzc400. Check component ID. The TZC-400 TRM shows
	 * component ID is expected to be "0xB105F00D".
	 */
	tzc_id = tzc_read_component_id(controller->base);
	if (tzc_id != TZC400_COMPONENT_ID) {
		ERROR("TZC : Wrong device ID (0x%x).\n", tzc_id);
		panic();
	}

	/* Save values we will use later. */
	tzc_build = tzc_read_build_config(controller->base);
	controller->num_filters = ((tzc_build >> BUILD_CONFIG_NF_SHIFT) &
			   BUILD_CONFIG_NF_MASK) + 1;
	controller->addr_width  = ((tzc_build >> BUILD_CONFIG_AW_SHIFT) &
			   BUILD_CONFIG_AW_MASK) + 1;
	controller->num_regions = ((tzc_build >> BUILD_CONFIG_NR_SHIFT) &
			   BUILD_CONFIG_NR_MASK) + 1;
}


/*
 * `tzc_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:
 * The region 0 covers the whole address space and is enabled on all filters,
 * this cannot be changed. It is, however, possible to change some region 0
 * permissions.
 */
169
void tzc_configure_region(const tzc_instance_t *controller,
Harry Liebel's avatar
Harry Liebel committed
170
171
172
173
			  uint32_t filters,
			  uint8_t  region,
			  uint64_t region_base,
			  uint64_t region_top,
174
			  tzc_region_attributes_t sec_attr,
Harry Liebel's avatar
Harry Liebel committed
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
			  uint32_t ns_device_access)
{
	uint64_t max_addr;

	assert(controller != NULL);

	/* Do range checks on filters and regions. */
	assert(((filters >> controller->num_filters) == 0) &&
	       (region < controller->num_regions));

	/*
	 * Do address range check based on TZC configuration. A 64bit address is
	 * the max and expected case.
	 */
	max_addr = UINT64_MAX >> (64 - controller->addr_width);
	if ((region_top > max_addr) || (region_base >= region_top))
		assert(0);

	/* region_base and (region_top + 1) must be 4KB aligned */
	assert(((region_base | (region_top + 1)) & (4096 - 1)) == 0);

	assert(sec_attr <= TZC_REGION_S_RDWR);

	/*
	 * Inputs look ok, start programming registers.
	 * All the address registers are 32 bits wide and have a LOW and HIGH
	 * component used to construct a up to a 64bit address.
	 */
	tzc_write_region_base_low(controller->base, region, (uint32_t)(region_base));
	tzc_write_region_base_high(controller->base, region, (uint32_t)(region_base >> 32));

	tzc_write_region_top_low(controller->base, region, (uint32_t)(region_top));
	tzc_write_region_top_high(controller->base, region, (uint32_t)(region_top >> 32));

	/* Assign the region to a filter and set secure attributes */
	tzc_write_region_attributes(controller->base, region,
		(sec_attr << REGION_ATTRIBUTES_SEC_SHIFT) | filters);

	/*
	 * Specify which non-secure devices have permission to access this
	 * region.
	 */
	tzc_write_region_id_access(controller->base, region, ns_device_access);
}


221
void tzc_set_action(const tzc_instance_t *controller, tzc_action_t action)
Harry Liebel's avatar
Harry Liebel committed
222
223
224
225
226
227
228
229
230
231
232
233
{
	assert(controller != NULL);

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


234
void tzc_enable_filters(const tzc_instance_t *controller)
Harry Liebel's avatar
Harry Liebel committed
235
236
237
238
239
240
241
242
243
{
	uint32_t state;
	uint32_t filter;

	assert(controller != NULL);

	for (filter = 0; filter < controller->num_filters; filter++) {
		state = tzc_get_gate_keeper(controller->base, filter);
		if (state) {
244
245
246
247
248
249
250
			/* The TZC filter is already configured. Changing the
			 * 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
			 * instance. See:
			 * http://infocenter.arm.com/help/index.jsp?\
			 * topic=/com.arm.doc.ddi0504c/CJHHECBF.html */
Harry Liebel's avatar
Harry Liebel committed
251
252
253
254
255
256
257
258
259
			ERROR("TZC : Filter %d Gatekeeper already enabled.\n",
				filter);
			panic();
		}
		tzc_set_gate_keeper(controller->base, filter, 1);
	}
}


260
void tzc_disable_filters(const tzc_instance_t *controller)
Harry Liebel's avatar
Harry Liebel committed
261
262
263
264
265
266
267
268
269
270
271
272
{
	uint32_t filter;

	assert(controller != NULL);

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