blob: 940e00e0cd67721511723f388491f383d09a3d0a [file] [log] [blame]
Harry Liebelafd1ec72014-04-01 19:19:22 +01001/*
Dan Handley76076762015-03-19 19:26:52 +00002 * Copyright (c) 2014-2015, ARM Limited and Contributors. All rights reserved.
Harry Liebelafd1ec72014-04-01 19:19:22 +01003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of ARM nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <assert.h>
Dan Handley714a0d22014-04-09 13:13:04 +010032#include <debug.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010033#include <mmio.h>
34#include <stddef.h>
35#include <tzc400.h>
Harry Liebelafd1ec72014-04-01 19:19:22 +010036
Dan Handley53c843a2014-08-04 19:53:05 +010037/*
38 * Implementation defined values used to validate inputs later.
39 * Filters : max of 4 ; 0 to 3
40 * Regions : max of 9 ; 0 to 8
41 * Address width : Values between 32 to 64
42 */
43typedef struct tzc_instance {
Juan Castillo7f1f0622014-09-09 09:49:23 +010044 uintptr_t base;
Dan Handley53c843a2014-08-04 19:53:05 +010045 uint8_t addr_width;
46 uint8_t num_filters;
47 uint8_t num_regions;
48} tzc_instance_t;
49
50tzc_instance_t tzc;
51
52
Juan Castillo7f1f0622014-09-09 09:49:23 +010053static inline uint32_t tzc_read_build_config(uintptr_t base)
Harry Liebelafd1ec72014-04-01 19:19:22 +010054{
55 return mmio_read_32(base + BUILD_CONFIG_OFF);
56}
57
Juan Castillo7f1f0622014-09-09 09:49:23 +010058static inline uint32_t tzc_read_gate_keeper(uintptr_t base)
Harry Liebelafd1ec72014-04-01 19:19:22 +010059{
60 return mmio_read_32(base + GATE_KEEPER_OFF);
61}
62
Juan Castillo7f1f0622014-09-09 09:49:23 +010063static inline void tzc_write_gate_keeper(uintptr_t base, uint32_t val)
Harry Liebelafd1ec72014-04-01 19:19:22 +010064{
65 mmio_write_32(base + GATE_KEEPER_OFF, val);
66}
67
Juan Castillo7f1f0622014-09-09 09:49:23 +010068static inline void tzc_write_action(uintptr_t base, tzc_action_t action)
Harry Liebelafd1ec72014-04-01 19:19:22 +010069{
70 mmio_write_32(base + ACTION_OFF, action);
71}
72
Juan Castillo7f1f0622014-09-09 09:49:23 +010073static inline void tzc_write_region_base_low(uintptr_t base,
Dan Handley53c843a2014-08-04 19:53:05 +010074 uint32_t region,
75 uint32_t val)
Harry Liebelafd1ec72014-04-01 19:19:22 +010076{
Dan Handley53c843a2014-08-04 19:53:05 +010077 mmio_write_32(base + REGION_BASE_LOW_OFF +
78 REGION_NUM_OFF(region), val);
Harry Liebelafd1ec72014-04-01 19:19:22 +010079}
80
Juan Castillo7f1f0622014-09-09 09:49:23 +010081static inline void tzc_write_region_base_high(uintptr_t base,
Dan Handley53c843a2014-08-04 19:53:05 +010082 uint32_t region,
83 uint32_t val)
Harry Liebelafd1ec72014-04-01 19:19:22 +010084{
Dan Handley53c843a2014-08-04 19:53:05 +010085 mmio_write_32(base + REGION_BASE_HIGH_OFF +
86 REGION_NUM_OFF(region), val);
Harry Liebelafd1ec72014-04-01 19:19:22 +010087}
88
Juan Castillo7f1f0622014-09-09 09:49:23 +010089static inline void tzc_write_region_top_low(uintptr_t base,
Dan Handley53c843a2014-08-04 19:53:05 +010090 uint32_t region,
91 uint32_t val)
Harry Liebelafd1ec72014-04-01 19:19:22 +010092{
Dan Handley53c843a2014-08-04 19:53:05 +010093 mmio_write_32(base + REGION_TOP_LOW_OFF +
94 REGION_NUM_OFF(region), val);
Harry Liebelafd1ec72014-04-01 19:19:22 +010095}
96
Juan Castillo7f1f0622014-09-09 09:49:23 +010097static inline void tzc_write_region_top_high(uintptr_t base,
Dan Handley53c843a2014-08-04 19:53:05 +010098 uint32_t region,
99 uint32_t val)
Harry Liebelafd1ec72014-04-01 19:19:22 +0100100{
Dan Handley53c843a2014-08-04 19:53:05 +0100101 mmio_write_32(base + REGION_TOP_HIGH_OFF +
102 REGION_NUM_OFF(region), val);
Harry Liebelafd1ec72014-04-01 19:19:22 +0100103}
104
Juan Castillo7f1f0622014-09-09 09:49:23 +0100105static inline void tzc_write_region_attributes(uintptr_t base,
Dan Handley53c843a2014-08-04 19:53:05 +0100106 uint32_t region,
107 uint32_t val)
Harry Liebelafd1ec72014-04-01 19:19:22 +0100108{
Dan Handley53c843a2014-08-04 19:53:05 +0100109 mmio_write_32(base + REGION_ATTRIBUTES_OFF +
110 REGION_NUM_OFF(region), val);
Harry Liebelafd1ec72014-04-01 19:19:22 +0100111}
112
Juan Castillo7f1f0622014-09-09 09:49:23 +0100113static inline void tzc_write_region_id_access(uintptr_t base,
Dan Handley53c843a2014-08-04 19:53:05 +0100114 uint32_t region,
115 uint32_t val)
Harry Liebelafd1ec72014-04-01 19:19:22 +0100116{
Dan Handley53c843a2014-08-04 19:53:05 +0100117 mmio_write_32(base + REGION_ID_ACCESS_OFF +
118 REGION_NUM_OFF(region), val);
Harry Liebelafd1ec72014-04-01 19:19:22 +0100119}
120
Vikram Kanigirid6028732015-10-20 16:55:26 +0100121static unsigned int tzc_read_peripheral_id(uintptr_t base)
Harry Liebelafd1ec72014-04-01 19:19:22 +0100122{
Vikram Kanigirid6028732015-10-20 16:55:26 +0100123 unsigned int id;
Harry Liebelafd1ec72014-04-01 19:19:22 +0100124
Vikram Kanigirid6028732015-10-20 16:55:26 +0100125 id = mmio_read_8(base + PID0_OFF);
126 /* Masks jep106_id_3_0 part in PID1 */
127 id |= ((mmio_read_8(base + PID1_OFF) & 0xF) << 8);
Harry Liebelafd1ec72014-04-01 19:19:22 +0100128
129 return id;
130}
131
Juan Castillo7f1f0622014-09-09 09:49:23 +0100132static uint32_t tzc_get_gate_keeper(uintptr_t base, uint8_t filter)
Harry Liebelafd1ec72014-04-01 19:19:22 +0100133{
134 uint32_t tmp;
135
136 tmp = (tzc_read_gate_keeper(base) >> GATE_KEEPER_OS_SHIFT) &
137 GATE_KEEPER_OS_MASK;
138
Juan Castillof558cac2014-06-05 09:45:36 +0100139 return (tmp >> filter) & GATE_KEEPER_FILTER_MASK;
Harry Liebelafd1ec72014-04-01 19:19:22 +0100140}
141
142/* This function is not MP safe. */
Juan Castillo7f1f0622014-09-09 09:49:23 +0100143static void tzc_set_gate_keeper(uintptr_t base, uint8_t filter, uint32_t val)
Harry Liebelafd1ec72014-04-01 19:19:22 +0100144{
145 uint32_t tmp;
146
147 /* Upper half is current state. Lower half is requested state. */
148 tmp = (tzc_read_gate_keeper(base) >> GATE_KEEPER_OS_SHIFT) &
149 GATE_KEEPER_OS_MASK;
150
151 if (val)
152 tmp |= (1 << filter);
153 else
154 tmp &= ~(1 << filter);
155
156 tzc_write_gate_keeper(base, (tmp & GATE_KEEPER_OR_MASK) <<
157 GATE_KEEPER_OR_SHIFT);
158
159 /* Wait here until we see the change reflected in the TZC status. */
160 while (((tzc_read_gate_keeper(base) >> GATE_KEEPER_OS_SHIFT) &
161 GATE_KEEPER_OS_MASK) != tmp)
162 ;
163}
164
165
Juan Castillo7f1f0622014-09-09 09:49:23 +0100166void tzc_init(uintptr_t base)
Harry Liebelafd1ec72014-04-01 19:19:22 +0100167{
Vikram Kanigirid6028732015-10-20 16:55:26 +0100168 unsigned int tzc_id;
169 unsigned int tzc_build;
Harry Liebelafd1ec72014-04-01 19:19:22 +0100170
Dan Handley53c843a2014-08-04 19:53:05 +0100171 assert(base);
Vikram Kanigirid6028732015-10-20 16:55:26 +0100172
173 /* Assert if already initialised */
174 assert(!tzc.base);
175
Dan Handley53c843a2014-08-04 19:53:05 +0100176 tzc.base = base;
Harry Liebelafd1ec72014-04-01 19:19:22 +0100177
178 /*
Vikram Kanigirid6028732015-10-20 16:55:26 +0100179 * We expect to see a tzc400. Check peripheral ID.
Harry Liebelafd1ec72014-04-01 19:19:22 +0100180 */
Vikram Kanigirid6028732015-10-20 16:55:26 +0100181 tzc_id = tzc_read_peripheral_id(tzc.base);
182 if (tzc_id != TZC400_PERIPHERAL_ID) {
Harry Liebelafd1ec72014-04-01 19:19:22 +0100183 ERROR("TZC : Wrong device ID (0x%x).\n", tzc_id);
184 panic();
185 }
186
187 /* Save values we will use later. */
Dan Handley53c843a2014-08-04 19:53:05 +0100188 tzc_build = tzc_read_build_config(tzc.base);
189 tzc.num_filters = ((tzc_build >> BUILD_CONFIG_NF_SHIFT) &
Harry Liebelafd1ec72014-04-01 19:19:22 +0100190 BUILD_CONFIG_NF_MASK) + 1;
Dan Handley53c843a2014-08-04 19:53:05 +0100191 tzc.addr_width = ((tzc_build >> BUILD_CONFIG_AW_SHIFT) &
Harry Liebelafd1ec72014-04-01 19:19:22 +0100192 BUILD_CONFIG_AW_MASK) + 1;
Dan Handley53c843a2014-08-04 19:53:05 +0100193 tzc.num_regions = ((tzc_build >> BUILD_CONFIG_NR_SHIFT) &
Harry Liebelafd1ec72014-04-01 19:19:22 +0100194 BUILD_CONFIG_NR_MASK) + 1;
195}
196
Dan Handley76076762015-03-19 19:26:52 +0000197/*
198 * `tzc_configure_region0` is used to program region 0 into the TrustZone
199 * controller. Region 0 covers the whole address space that is not mapped
200 * to any other region, and is enabled on all filters; this cannot be
201 * changed. This function only changes the access permissions.
202 */
203void tzc_configure_region0(tzc_region_attributes_t sec_attr,
204 uint32_t ns_device_access)
205{
206 assert(tzc.base);
207
208 VERBOSE("TZC : Configuring region 0 (sec_attr=0x%x, ns_devs=0x%x)\n",
209 sec_attr, ns_device_access);
210
211 assert(sec_attr <= TZC_REGION_S_RDWR);
212
213 /* Set secure attributes on region 0 */
214 tzc_write_region_attributes(tzc.base, 0,
215 sec_attr << REG_ATTR_SEC_SHIFT);
216
217 /*
218 * Specify which non-secure devices have permission to access
219 * region 0.
220 */
221 tzc_write_region_id_access(tzc.base, 0, ns_device_access);
222}
223
Harry Liebelafd1ec72014-04-01 19:19:22 +0100224
225/*
226 * `tzc_configure_region` is used to program regions into the TrustZone
227 * controller. A region can be associated with more than one filter. The
228 * associated filters are passed in as a bitmap (bit0 = filter0).
229 * NOTE:
Dan Handley76076762015-03-19 19:26:52 +0000230 * Region 0 is special; it is preferable to use tzc_configure_region0
231 * for this region (see comment for that function).
Harry Liebelafd1ec72014-04-01 19:19:22 +0100232 */
Dan Handley53c843a2014-08-04 19:53:05 +0100233void tzc_configure_region(uint32_t filters,
Harry Liebelafd1ec72014-04-01 19:19:22 +0100234 uint8_t region,
235 uint64_t region_base,
236 uint64_t region_top,
Dan Handleye2712bc2014-04-10 15:37:22 +0100237 tzc_region_attributes_t sec_attr,
Harry Liebelafd1ec72014-04-01 19:19:22 +0100238 uint32_t ns_device_access)
239{
Dan Handley53c843a2014-08-04 19:53:05 +0100240 assert(tzc.base);
Harry Liebelafd1ec72014-04-01 19:19:22 +0100241
Dan Handley76076762015-03-19 19:26:52 +0000242 VERBOSE("TZC : Configuring region (filters=0x%x, region=%d, ...\n",
243 filters, region);
244 VERBOSE("TZC : ... base=0x%lx, top=0x%lx, ...\n",
245 region_base, region_top);
246 VERBOSE("TZC : ... sec_attr=0x%x, ns_devs=0x%x)\n",
247 sec_attr, ns_device_access);
248
Harry Liebelafd1ec72014-04-01 19:19:22 +0100249 /* Do range checks on filters and regions. */
Dan Handley53c843a2014-08-04 19:53:05 +0100250 assert(((filters >> tzc.num_filters) == 0) &&
251 (region < tzc.num_regions));
Harry Liebelafd1ec72014-04-01 19:19:22 +0100252
253 /*
254 * Do address range check based on TZC configuration. A 64bit address is
255 * the max and expected case.
256 */
Dan Handley53c843a2014-08-04 19:53:05 +0100257 assert(((region_top <= (UINT64_MAX >> (64 - tzc.addr_width))) &&
258 (region_base < region_top)));
Harry Liebelafd1ec72014-04-01 19:19:22 +0100259
260 /* region_base and (region_top + 1) must be 4KB aligned */
261 assert(((region_base | (region_top + 1)) & (4096 - 1)) == 0);
262
263 assert(sec_attr <= TZC_REGION_S_RDWR);
264
265 /*
266 * Inputs look ok, start programming registers.
267 * All the address registers are 32 bits wide and have a LOW and HIGH
268 * component used to construct a up to a 64bit address.
269 */
Dan Handley53c843a2014-08-04 19:53:05 +0100270 tzc_write_region_base_low(tzc.base, region,
271 (uint32_t)(region_base));
272 tzc_write_region_base_high(tzc.base, region,
273 (uint32_t)(region_base >> 32));
Harry Liebelafd1ec72014-04-01 19:19:22 +0100274
Dan Handley53c843a2014-08-04 19:53:05 +0100275 tzc_write_region_top_low(tzc.base, region,
276 (uint32_t)(region_top));
277 tzc_write_region_top_high(tzc.base, region,
278 (uint32_t)(region_top >> 32));
Harry Liebelafd1ec72014-04-01 19:19:22 +0100279
280 /* Assign the region to a filter and set secure attributes */
Dan Handley53c843a2014-08-04 19:53:05 +0100281 tzc_write_region_attributes(tzc.base, region,
Juan Castillo921b8772014-09-05 17:29:38 +0100282 (sec_attr << REG_ATTR_SEC_SHIFT) | filters);
Harry Liebelafd1ec72014-04-01 19:19:22 +0100283
284 /*
285 * Specify which non-secure devices have permission to access this
286 * region.
287 */
Dan Handley53c843a2014-08-04 19:53:05 +0100288 tzc_write_region_id_access(tzc.base, region, ns_device_access);
Harry Liebelafd1ec72014-04-01 19:19:22 +0100289}
290
291
Dan Handley53c843a2014-08-04 19:53:05 +0100292void tzc_set_action(tzc_action_t action)
Harry Liebelafd1ec72014-04-01 19:19:22 +0100293{
Dan Handley53c843a2014-08-04 19:53:05 +0100294 assert(tzc.base);
Harry Liebelafd1ec72014-04-01 19:19:22 +0100295
296 /*
297 * - Currently no handler is provided to trap an error via interrupt
298 * or exception.
299 * - The interrupt action has not been tested.
300 */
Dan Handley53c843a2014-08-04 19:53:05 +0100301 tzc_write_action(tzc.base, action);
Harry Liebelafd1ec72014-04-01 19:19:22 +0100302}
303
304
Dan Handley53c843a2014-08-04 19:53:05 +0100305void tzc_enable_filters(void)
Harry Liebelafd1ec72014-04-01 19:19:22 +0100306{
307 uint32_t state;
308 uint32_t filter;
309
Dan Handley53c843a2014-08-04 19:53:05 +0100310 assert(tzc.base);
Harry Liebelafd1ec72014-04-01 19:19:22 +0100311
Dan Handley53c843a2014-08-04 19:53:05 +0100312 for (filter = 0; filter < tzc.num_filters; filter++) {
313 state = tzc_get_gate_keeper(tzc.base, filter);
Harry Liebelafd1ec72014-04-01 19:19:22 +0100314 if (state) {
Juan Castillof558cac2014-06-05 09:45:36 +0100315 /* The TZC filter is already configured. Changing the
316 * programmer's view in an active system can cause
317 * unpredictable behavior therefore panic for now rather
318 * than try to determine whether this is safe in this
319 * instance. See:
320 * http://infocenter.arm.com/help/index.jsp?\
321 * topic=/com.arm.doc.ddi0504c/CJHHECBF.html */
Harry Liebelafd1ec72014-04-01 19:19:22 +0100322 ERROR("TZC : Filter %d Gatekeeper already enabled.\n",
323 filter);
324 panic();
325 }
Dan Handley53c843a2014-08-04 19:53:05 +0100326 tzc_set_gate_keeper(tzc.base, filter, 1);
Harry Liebelafd1ec72014-04-01 19:19:22 +0100327 }
328}
329
330
Dan Handley53c843a2014-08-04 19:53:05 +0100331void tzc_disable_filters(void)
Harry Liebelafd1ec72014-04-01 19:19:22 +0100332{
333 uint32_t filter;
334
Dan Handley53c843a2014-08-04 19:53:05 +0100335 assert(tzc.base);
Harry Liebelafd1ec72014-04-01 19:19:22 +0100336
337 /*
338 * We don't do the same state check as above as the Gatekeepers are
339 * disabled after reset.
340 */
Dan Handley53c843a2014-08-04 19:53:05 +0100341 for (filter = 0; filter < tzc.num_filters; filter++)
342 tzc_set_gate_keeper(tzc.base, filter, 0);
Harry Liebelafd1ec72014-04-01 19:19:22 +0100343}