d27b010294220d0f69aff24bacc4ac6c3ab6563b
2 * Copyright (c) 2016-2018, ARM Limited and Contributors. All rights reserved.
4 * SPDX-License-Identifier: BSD-3-Clause
12 #include "tzc_common_private.h"
15 * Macros which will be used by common core functions.
17 #define TZC_400_REGION_BASE_LOW_0_OFFSET U(0x100)
18 #define TZC_400_REGION_BASE_HIGH_0_OFFSET U(0x104)
19 #define TZC_400_REGION_TOP_LOW_0_OFFSET U(0x108)
20 #define TZC_400_REGION_TOP_HIGH_0_OFFSET U(0x10c)
21 #define TZC_400_REGION_ATTR_0_OFFSET U(0x110)
22 #define TZC_400_REGION_ID_ACCESS_0_OFFSET U(0x114)
25 * Implementation defined values used to validate inputs later.
26 * Filters : max of 4 ; 0 to 3
27 * Regions : max of 9 ; 0 to 8
28 * Address width : Values between 32 to 64
30 typedef struct tzc400_instance
{
37 static tzc400_instance_t tzc400
;
39 static inline unsigned int _tzc400_read_build_config(uintptr_t base
)
41 return mmio_read_32(base
+ BUILD_CONFIG_OFF
);
44 static inline unsigned int _tzc400_read_gate_keeper(uintptr_t base
)
46 return mmio_read_32(base
+ GATE_KEEPER_OFF
);
49 static inline void _tzc400_write_gate_keeper(uintptr_t base
, unsigned int val
)
51 mmio_write_32(base
+ GATE_KEEPER_OFF
, val
);
55 * Get the open status information for all filter units.
57 #define get_gate_keeper_os(_base) ((_tzc400_read_gate_keeper(_base) >> \
58 GATE_KEEPER_OS_SHIFT) & \
62 /* Define common core functions used across different TZC peripherals. */
63 DEFINE_TZC_COMMON_WRITE_ACTION(400, 400)
64 DEFINE_TZC_COMMON_WRITE_REGION_BASE(400, 400)
65 DEFINE_TZC_COMMON_WRITE_REGION_TOP(400, 400)
66 DEFINE_TZC_COMMON_WRITE_REGION_ATTRIBUTES(400, 400)
67 DEFINE_TZC_COMMON_WRITE_REGION_ID_ACCESS(400, 400)
68 DEFINE_TZC_COMMON_CONFIGURE_REGION0(400)
69 DEFINE_TZC_COMMON_CONFIGURE_REGION(400)
71 static unsigned int _tzc400_get_gate_keeper(uintptr_t base
,
74 unsigned int open_status
;
76 open_status
= get_gate_keeper_os(base
);
78 return (open_status
>> filter
) & GATE_KEEPER_FILTER_MASK
;
81 /* This function is not MP safe. */
82 static void _tzc400_set_gate_keeper(uintptr_t base
,
86 unsigned int open_status
;
88 /* Upper half is current state. Lower half is requested state. */
89 open_status
= get_gate_keeper_os(base
);
92 open_status
|= (1U << filter
);
94 open_status
&= ~(1U << filter
);
96 _tzc400_write_gate_keeper(base
, (open_status
& GATE_KEEPER_OR_MASK
) <<
97 GATE_KEEPER_OR_SHIFT
);
99 /* Wait here until we see the change reflected in the TZC status. */
100 while ((get_gate_keeper_os(base
)) != open_status
)
104 void tzc400_set_action(unsigned int action
)
106 assert(tzc400
.base
!= 0U);
107 assert(action
<= TZC_ACTION_ERR_INT
);
110 * - Currently no handler is provided to trap an error via interrupt
112 * - The interrupt action has not been tested.
114 _tzc400_write_action(tzc400
.base
, action
);
117 void tzc400_init(uintptr_t base
)
120 unsigned int tzc400_id
;
122 unsigned int tzc400_build
;
128 tzc400_id
= _tzc_read_peripheral_id(base
);
129 if (tzc400_id
!= TZC_400_PERIPHERAL_ID
) {
130 ERROR("TZC-400 : Wrong device ID (0x%x).\n", tzc400_id
);
135 /* Save values we will use later. */
136 tzc400_build
= _tzc400_read_build_config(tzc400
.base
);
137 tzc400
.num_filters
= (uint8_t)((tzc400_build
>> BUILD_CONFIG_NF_SHIFT
) &
138 BUILD_CONFIG_NF_MASK
) + 1U;
139 tzc400
.addr_width
= (uint8_t)((tzc400_build
>> BUILD_CONFIG_AW_SHIFT
) &
140 BUILD_CONFIG_AW_MASK
) + 1U;
141 tzc400
.num_regions
= (uint8_t)((tzc400_build
>> BUILD_CONFIG_NR_SHIFT
) &
142 BUILD_CONFIG_NR_MASK
) + 1U;
146 * `tzc400_configure_region0` is used to program region 0 into the TrustZone
147 * controller. Region 0 covers the whole address space that is not mapped
148 * to any other region, and is enabled on all filters; this cannot be
149 * changed. This function only changes the access permissions.
151 void tzc400_configure_region0(unsigned int sec_attr
,
152 unsigned int ns_device_access
)
154 assert(tzc400
.base
!= 0U);
155 assert(sec_attr
<= TZC_REGION_S_RDWR
);
157 _tzc400_configure_region0(tzc400
.base
, sec_attr
, ns_device_access
);
161 * `tzc400_configure_region` is used to program regions into the TrustZone
162 * controller. A region can be associated with more than one filter. The
163 * associated filters are passed in as a bitmap (bit0 = filter0).
165 * Region 0 is special; it is preferable to use tzc400_configure_region0
166 * for this region (see comment for that function).
168 void tzc400_configure_region(unsigned int filters
,
170 unsigned long long region_base
,
171 unsigned long long region_top
,
172 unsigned int sec_attr
,
173 unsigned int nsaid_permissions
)
175 assert(tzc400
.base
!= 0U);
177 /* Do range checks on filters and regions. */
178 assert(((filters
>> tzc400
.num_filters
) == 0U) &&
179 (region
< tzc400
.num_regions
));
182 * Do address range check based on TZC configuration. A 64bit address is
183 * the max and expected case.
185 assert((region_top
<= (UINT64_MAX
>> (64U - tzc400
.addr_width
))) &&
186 (region_base
< region_top
));
188 /* region_base and (region_top + 1) must be 4KB aligned */
189 assert(((region_base
| (region_top
+ 1U)) & (4096U - 1U)) == 0U);
191 assert(sec_attr
<= TZC_REGION_S_RDWR
);
193 _tzc400_configure_region(tzc400
.base
, filters
, region
, region_base
,
195 sec_attr
, nsaid_permissions
);
198 void tzc400_enable_filters(void)
203 assert(tzc400
.base
!= 0U);
205 for (filter
= 0U; filter
< tzc400
.num_filters
; filter
++) {
206 state
= _tzc400_get_gate_keeper(tzc400
.base
, filter
);
209 * The TZC filter is already configured. Changing the
210 * programmer's view in an active system can cause
211 * unpredictable behavior therefore panic for now rather
212 * than try to determine whether this is safe in this
215 * See the 'ARM (R) CoreLink TM TZC-400 TrustZone (R)
216 * Address Space Controller' Technical Reference Manual.
218 ERROR("TZC-400 : Filter %d Gatekeeper already"
219 " enabled.\n", filter
);
222 _tzc400_set_gate_keeper(tzc400
.base
, filter
, 1);
226 void tzc400_disable_filters(void)
230 assert(tzc400
.base
!= 0U);
233 * We don't do the same state check as above as the Gatekeepers are
234 * disabled after reset.
236 for (filter
= 0; filter
< tzc400
.num_filters
; filter
++)
237 _tzc400_set_gate_keeper(tzc400
.base
, filter
, 0);