tzc_common_private.h 6.67 KB
Newer Older
1
/*
Roberto Vargas's avatar
Roberto Vargas 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
 */

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
7
8
#ifndef TZC_COMMON_PRIVATE_H
#define TZC_COMMON_PRIVATE_H
9

10
11
#include <arch.h>
#include <arch_helpers.h>
12
13
14
15
16
17
#include <mmio.h>
#include <tzc_common.h>

#define DEFINE_TZC_COMMON_WRITE_ACTION(fn_name, macro_name)		\
	static inline void _tzc##fn_name##_write_action(		\
					uintptr_t base,			\
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
18
					unsigned int action)		\
19
20
21
22
23
24
25
	{								\
		mmio_write_32(base + TZC_##macro_name##_ACTION_OFF,	\
			action);					\
	}

#define DEFINE_TZC_COMMON_WRITE_REGION_BASE(fn_name, macro_name)	\
	static inline void _tzc##fn_name##_write_region_base(		\
26
					uintptr_t base,			\
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
27
					unsigned int region_no,		\
28
					unsigned long long region_base)	\
29
30
31
32
33
	{								\
		mmio_write_32(base +					\
			TZC_REGION_OFFSET(				\
				TZC_##macro_name##_REGION_SIZE,		\
				region_no) +				\
34
35
			TZC_##macro_name##_REGION_BASE_LOW_0_OFFSET,	\
			(uint32_t)region_base);				\
36
37
38
39
40
		mmio_write_32(base +					\
			TZC_REGION_OFFSET(				\
				TZC_##macro_name##_REGION_SIZE,		\
				region_no) +				\
			TZC_##macro_name##_REGION_BASE_HIGH_0_OFFSET,	\
41
			(uint32_t)(region_base >> 32));			\
42
43
44
45
	}

#define DEFINE_TZC_COMMON_WRITE_REGION_TOP(fn_name, macro_name)		\
	static inline void _tzc##fn_name##_write_region_top(		\
46
					uintptr_t base,			\
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
47
					unsigned int region_no,		\
48
					unsigned long long region_top)	\
49
50
51
52
53
54
	{								\
		mmio_write_32(base +					\
			TZC_REGION_OFFSET				\
				(TZC_##macro_name##_REGION_SIZE,	\
				region_no) +				\
			TZC_##macro_name##_REGION_TOP_LOW_0_OFFSET,	\
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
55
			(uint32_t)region_top);				\
56
57
58
59
60
		mmio_write_32(base +					\
			TZC_REGION_OFFSET(				\
				TZC_##macro_name##_REGION_SIZE,		\
				region_no) +				\
			TZC_##macro_name##_REGION_TOP_HIGH_0_OFFSET,	\
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
61
			(uint32_t)(region_top >> 32));			\
62
63
64
65
66
	}

#define DEFINE_TZC_COMMON_WRITE_REGION_ATTRIBUTES(fn_name, macro_name)	\
	static inline void _tzc##fn_name##_write_region_attributes(	\
						uintptr_t base,		\
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
67
						unsigned int region_no,	\
68
69
70
71
72
73
74
75
76
77
78
79
80
						unsigned int attr)	\
	{								\
		mmio_write_32(base +					\
			TZC_REGION_OFFSET(				\
				TZC_##macro_name##_REGION_SIZE,		\
				region_no) +				\
			TZC_##macro_name##_REGION_ATTR_0_OFFSET,	\
			attr);						\
	}

#define DEFINE_TZC_COMMON_WRITE_REGION_ID_ACCESS(fn_name, macro_name)	\
	static inline void _tzc##fn_name##_write_region_id_access(	\
						uintptr_t base,		\
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
81
						unsigned int region_no,	\
82
83
84
85
86
87
88
89
90
91
92
93
94
95
						unsigned int val)	\
	{								\
		mmio_write_32(base +					\
			TZC_REGION_OFFSET(				\
				TZC_##macro_name##_REGION_SIZE,		\
				region_no) +				\
			TZC_##macro_name##_REGION_ID_ACCESS_0_OFFSET,	\
			val);						\
	}

/*
 * It is used to program region 0 ATTRIBUTES and ACCESS register.
 */
#define DEFINE_TZC_COMMON_CONFIGURE_REGION0(fn_name)			\
Roberto Vargas's avatar
Roberto Vargas committed
96
	static void _tzc##fn_name##_configure_region0(uintptr_t base,	\
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
97
			   unsigned int sec_attr,			\
98
99
			   unsigned int ns_device_access)		\
	{								\
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
100
		assert(base != 0U);					\
101
		VERBOSE("TrustZone : Configuring region 0 "		\
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
102
103
			"(TZC Interface Base=0x%lx sec_attr=0x%x,"	\
			" ns_devs=0x%x)\n", base,			\
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
			sec_attr, ns_device_access);			\
									\
		/* Set secure attributes on region 0 */			\
		_tzc##fn_name##_write_region_attributes(base, 0,	\
			sec_attr << TZC_REGION_ATTR_SEC_SHIFT);		\
									\
		/***************************************************/	\
		/* Specify which non-secure devices have permission*/	\
		/* to access region 0.				   */	\
		/***************************************************/	\
		_tzc##fn_name##_write_region_id_access(base,		\
						0,			\
						ns_device_access);	\
	}

/*
 * It is used to program a region from 1 to 8 in the TrustZone controller.
 * NOTE:
 * Region 0 is special; it is preferable to use
 * ##fn_name##_configure_region0 for this region (see comment for
 * that function).
 */
#define DEFINE_TZC_COMMON_CONFIGURE_REGION(fn_name)			\
Roberto Vargas's avatar
Roberto Vargas committed
127
	static void _tzc##fn_name##_configure_region(uintptr_t base,	\
128
				unsigned int filters,			\
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
129
				unsigned int region_no,			\
130
131
				unsigned long long region_base,		\
				unsigned long long region_top,		\
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
132
133
				unsigned int sec_attr,			\
				unsigned int nsaid_permissions)		\
134
	{								\
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
135
		assert(base != 0U);					\
136
		VERBOSE("TrustZone : Configuring region "		\
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
137
138
			"(TZC Interface Base: 0x%lx, region_no = %u)"	\
			"...\n", base, region_no);			\
139
		VERBOSE("TrustZone : ... base = %llx, top = %llx,"	\
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
140
			"\n", region_base, region_top);			\
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
169
170
171
		VERBOSE("TrustZone : ... sec_attr = 0x%x,"		\
			" ns_devs = 0x%x)\n",				\
			sec_attr, nsaid_permissions);			\
									\
		/***************************************************/	\
		/* Inputs look ok, start programming registers.    */	\
		/* All the address registers are 32 bits wide and  */	\
		/* have a LOW and HIGH				   */	\
		/* component used to construct an address up to a  */	\
		/* 64bit.					   */	\
		/***************************************************/	\
		_tzc##fn_name##_write_region_base(base,			\
					region_no, region_base);	\
		_tzc##fn_name##_write_region_top(base,			\
					region_no, region_top);		\
									\
		/* Enable filter to the region and set secure attributes */\
		_tzc##fn_name##_write_region_attributes(base,		\
				region_no,				\
				(sec_attr << TZC_REGION_ATTR_SEC_SHIFT) |\
				(filters << TZC_REGION_ATTR_F_EN_SHIFT));\
									\
		/***************************************************/	\
		/* Specify which non-secure devices have permission*/	\
		/* to access this region.			   */	\
		/***************************************************/	\
		_tzc##fn_name##_write_region_id_access(base,		\
						region_no,		\
						nsaid_permissions);	\
	}

172
static inline unsigned int _tzc_read_peripheral_id(uintptr_t base)
173
174
175
176
177
{
	unsigned int id;

	id = mmio_read_32(base + PID0_OFF);
	/* Masks DESC part in PID1 */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
178
	id |= ((mmio_read_32(base + PID1_OFF) & 0xFU) << 8U);
179
180
181

	return id;
}
182

183
#if ENABLE_ASSERTIONS
184
#ifdef AARCH32
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
185
static inline unsigned long long _tzc_get_max_top_addr(unsigned int addr_width)
186
187
188
189
190
191
{
	/*
	 * Assume at least 32 bit wide address and initialize the max.
	 * This function doesn't use 64-bit integer arithmetic to avoid
	 * having to implement additional compiler library functions.
	 */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
192
	unsigned long long addr_mask = 0xFFFFFFFFU;
193
194
	uint32_t *addr_ptr = (uint32_t *)&addr_mask;

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
195
	assert(addr_width >= 32U);
196
197

	/* This logic works only on little - endian platforms */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
198
	assert((read_sctlr() & SCTLR_EE_BIT) == 0U);
199
200
201
202
203

	/*
	 * If required address width is greater than 32, populate the higher
	 * 32 bits of the 64 bit field with the max address.
	 */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
204
205
	if (addr_width > 32U)
		*(addr_ptr + 1U) = ((1U << (addr_width - 32U)) - 1U);
206
207
208
209

	return addr_mask;
}
#else
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
210
211
212
213
static inline unsigned long long _tzc_get_max_top_addr(unsigned int addr_width)
{
	return UINT64_MAX >> (64U - addr_width);
}
214
215
#endif /* AARCH32 */

216
#endif /* ENABLE_ASSERTIONS */
217

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
218
#endif /* TZC_COMMON_PRIVATE_H */