blob: 2090944a75ac1f5548c949ffb73754102b5d594c [file] [log] [blame]
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +00001/*
Lionel Debieve7bd23362020-09-27 20:48:30 +02002 * Copyright (c) 2016-2021, ARM Limited and Contributors. All rights reserved.
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +00003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +00005 */
6
Antonio Nino Diaz5f475792018-10-15 14:58:11 +01007#ifndef TZC_COMMON_PRIVATE_H
8#define TZC_COMMON_PRIVATE_H
Antonio Nino Diaz0ffc4492017-02-28 10:58:25 +00009
Soby Mathewd6452322016-05-05 13:59:07 +010010#include <arch.h>
11#include <arch_helpers.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012#include <drivers/arm/tzc_common.h>
13#include <lib/mmio.h>
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +000014
15#define DEFINE_TZC_COMMON_WRITE_ACTION(fn_name, macro_name) \
16 static inline void _tzc##fn_name##_write_action( \
17 uintptr_t base, \
Antonio Nino Diaz5f475792018-10-15 14:58:11 +010018 unsigned int action) \
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +000019 { \
20 mmio_write_32(base + TZC_##macro_name##_ACTION_OFF, \
21 action); \
22 }
23
24#define DEFINE_TZC_COMMON_WRITE_REGION_BASE(fn_name, macro_name) \
25 static inline void _tzc##fn_name##_write_region_base( \
Yatharth Kocharfc719752016-04-08 14:40:44 +010026 uintptr_t base, \
Antonio Nino Diaz5f475792018-10-15 14:58:11 +010027 unsigned int region_no, \
Yatharth Kocharfc719752016-04-08 14:40:44 +010028 unsigned long long region_base) \
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +000029 { \
30 mmio_write_32(base + \
31 TZC_REGION_OFFSET( \
32 TZC_##macro_name##_REGION_SIZE, \
Jimmy Brissoned202072020-08-04 16:18:52 -050033 (u_register_t)region_no) + \
Yatharth Kocharfc719752016-04-08 14:40:44 +010034 TZC_##macro_name##_REGION_BASE_LOW_0_OFFSET, \
35 (uint32_t)region_base); \
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +000036 mmio_write_32(base + \
37 TZC_REGION_OFFSET( \
38 TZC_##macro_name##_REGION_SIZE, \
Jimmy Brissoned202072020-08-04 16:18:52 -050039 (u_register_t)region_no) + \
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +000040 TZC_##macro_name##_REGION_BASE_HIGH_0_OFFSET, \
Yatharth Kocharfc719752016-04-08 14:40:44 +010041 (uint32_t)(region_base >> 32)); \
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +000042 }
43
44#define DEFINE_TZC_COMMON_WRITE_REGION_TOP(fn_name, macro_name) \
45 static inline void _tzc##fn_name##_write_region_top( \
Yatharth Kocharfc719752016-04-08 14:40:44 +010046 uintptr_t base, \
Antonio Nino Diaz5f475792018-10-15 14:58:11 +010047 unsigned int region_no, \
Yatharth Kocharfc719752016-04-08 14:40:44 +010048 unsigned long long region_top) \
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +000049 { \
50 mmio_write_32(base + \
Jimmy Brissoned202072020-08-04 16:18:52 -050051 TZC_REGION_OFFSET( \
52 TZC_##macro_name##_REGION_SIZE, \
53 (u_register_t)region_no) + \
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +000054 TZC_##macro_name##_REGION_TOP_LOW_0_OFFSET, \
Antonio Nino Diaz5f475792018-10-15 14:58:11 +010055 (uint32_t)region_top); \
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +000056 mmio_write_32(base + \
57 TZC_REGION_OFFSET( \
58 TZC_##macro_name##_REGION_SIZE, \
Jimmy Brissoned202072020-08-04 16:18:52 -050059 (u_register_t)region_no) + \
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +000060 TZC_##macro_name##_REGION_TOP_HIGH_0_OFFSET, \
Antonio Nino Diaz5f475792018-10-15 14:58:11 +010061 (uint32_t)(region_top >> 32)); \
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +000062 }
63
64#define DEFINE_TZC_COMMON_WRITE_REGION_ATTRIBUTES(fn_name, macro_name) \
65 static inline void _tzc##fn_name##_write_region_attributes( \
66 uintptr_t base, \
Antonio Nino Diaz5f475792018-10-15 14:58:11 +010067 unsigned int region_no, \
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +000068 unsigned int attr) \
69 { \
70 mmio_write_32(base + \
71 TZC_REGION_OFFSET( \
72 TZC_##macro_name##_REGION_SIZE, \
Jimmy Brissoned202072020-08-04 16:18:52 -050073 (u_register_t)region_no) + \
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +000074 TZC_##macro_name##_REGION_ATTR_0_OFFSET, \
75 attr); \
76 }
77
78#define DEFINE_TZC_COMMON_WRITE_REGION_ID_ACCESS(fn_name, macro_name) \
79 static inline void _tzc##fn_name##_write_region_id_access( \
80 uintptr_t base, \
Antonio Nino Diaz5f475792018-10-15 14:58:11 +010081 unsigned int region_no, \
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +000082 unsigned int val) \
83 { \
84 mmio_write_32(base + \
85 TZC_REGION_OFFSET( \
86 TZC_##macro_name##_REGION_SIZE, \
Jimmy Brissoned202072020-08-04 16:18:52 -050087 (u_register_t)region_no) + \
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +000088 TZC_##macro_name##_REGION_ID_ACCESS_0_OFFSET, \
89 val); \
90 }
91
92/*
Lionel Debieve7bd23362020-09-27 20:48:30 +020093 * It is used to modify the filters status for a defined region.
94 */
95#define DEFINE_TZC_COMMON_UPDATE_FILTERS(fn_name, macro_name) \
96 static inline void _tzc##fn_name##_update_filters( \
97 uintptr_t base, \
98 unsigned int region_no, \
99 unsigned int nbfilters, \
100 unsigned int filters) \
101 { \
102 uint32_t filters_mask = GENMASK(nbfilters - 1U, 0); \
103 \
104 mmio_clrsetbits_32(base + \
105 TZC_REGION_OFFSET( \
106 TZC_##macro_name##_REGION_SIZE, \
107 region_no) + \
108 TZC_##macro_name##_REGION_ATTR_0_OFFSET, \
109 filters_mask << TZC_REGION_ATTR_F_EN_SHIFT, \
110 filters << TZC_REGION_ATTR_F_EN_SHIFT); \
111 }
112
113/*
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +0000114 * It is used to program region 0 ATTRIBUTES and ACCESS register.
115 */
116#define DEFINE_TZC_COMMON_CONFIGURE_REGION0(fn_name) \
Roberto Vargas2ca18d92018-02-12 12:36:17 +0000117 static void _tzc##fn_name##_configure_region0(uintptr_t base, \
Antonio Nino Diaz5f475792018-10-15 14:58:11 +0100118 unsigned int sec_attr, \
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +0000119 unsigned int ns_device_access) \
120 { \
Antonio Nino Diaz5f475792018-10-15 14:58:11 +0100121 assert(base != 0U); \
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +0000122 VERBOSE("TrustZone : Configuring region 0 " \
Antonio Nino Diaz5f475792018-10-15 14:58:11 +0100123 "(TZC Interface Base=0x%lx sec_attr=0x%x," \
124 " ns_devs=0x%x)\n", base, \
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +0000125 sec_attr, ns_device_access); \
126 \
127 /* Set secure attributes on region 0 */ \
128 _tzc##fn_name##_write_region_attributes(base, 0, \
129 sec_attr << TZC_REGION_ATTR_SEC_SHIFT); \
130 \
131 /***************************************************/ \
132 /* Specify which non-secure devices have permission*/ \
133 /* to access region 0. */ \
134 /***************************************************/ \
135 _tzc##fn_name##_write_region_id_access(base, \
136 0, \
137 ns_device_access); \
138 }
139
140/*
141 * It is used to program a region from 1 to 8 in the TrustZone controller.
142 * NOTE:
143 * Region 0 is special; it is preferable to use
144 * ##fn_name##_configure_region0 for this region (see comment for
145 * that function).
146 */
147#define DEFINE_TZC_COMMON_CONFIGURE_REGION(fn_name) \
Roberto Vargas2ca18d92018-02-12 12:36:17 +0000148 static void _tzc##fn_name##_configure_region(uintptr_t base, \
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +0000149 unsigned int filters, \
Antonio Nino Diaz5f475792018-10-15 14:58:11 +0100150 unsigned int region_no, \
Yatharth Kocharfc719752016-04-08 14:40:44 +0100151 unsigned long long region_base, \
152 unsigned long long region_top, \
Antonio Nino Diaz5f475792018-10-15 14:58:11 +0100153 unsigned int sec_attr, \
154 unsigned int nsaid_permissions) \
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +0000155 { \
Antonio Nino Diaz5f475792018-10-15 14:58:11 +0100156 assert(base != 0U); \
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +0000157 VERBOSE("TrustZone : Configuring region " \
Antonio Nino Diaz5f475792018-10-15 14:58:11 +0100158 "(TZC Interface Base: 0x%lx, region_no = %u)" \
159 "...\n", base, region_no); \
Yatharth Kocharfc719752016-04-08 14:40:44 +0100160 VERBOSE("TrustZone : ... base = %llx, top = %llx," \
Antonio Nino Diaz5f475792018-10-15 14:58:11 +0100161 "\n", region_base, region_top); \
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +0000162 VERBOSE("TrustZone : ... sec_attr = 0x%x," \
163 " ns_devs = 0x%x)\n", \
164 sec_attr, nsaid_permissions); \
165 \
166 /***************************************************/ \
167 /* Inputs look ok, start programming registers. */ \
168 /* All the address registers are 32 bits wide and */ \
169 /* have a LOW and HIGH */ \
170 /* component used to construct an address up to a */ \
171 /* 64bit. */ \
172 /***************************************************/ \
173 _tzc##fn_name##_write_region_base(base, \
174 region_no, region_base); \
175 _tzc##fn_name##_write_region_top(base, \
176 region_no, region_top); \
177 \
178 /* Enable filter to the region and set secure attributes */\
179 _tzc##fn_name##_write_region_attributes(base, \
180 region_no, \
181 (sec_attr << TZC_REGION_ATTR_SEC_SHIFT) |\
182 (filters << TZC_REGION_ATTR_F_EN_SHIFT));\
183 \
184 /***************************************************/ \
185 /* Specify which non-secure devices have permission*/ \
186 /* to access this region. */ \
187 /***************************************************/ \
188 _tzc##fn_name##_write_region_id_access(base, \
189 region_no, \
190 nsaid_permissions); \
191 }
192
Antonio Nino Diaz0ffc4492017-02-28 10:58:25 +0000193static inline unsigned int _tzc_read_peripheral_id(uintptr_t base)
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +0000194{
195 unsigned int id;
196
197 id = mmio_read_32(base + PID0_OFF);
198 /* Masks DESC part in PID1 */
Antonio Nino Diaz5f475792018-10-15 14:58:11 +0100199 id |= ((mmio_read_32(base + PID1_OFF) & 0xFU) << 8U);
Vikram Kanigiri1eabdbc2016-01-28 17:22:16 +0000200
201 return id;
202}
Soby Mathewd6452322016-05-05 13:59:07 +0100203
Antonio Nino Diaz5f475792018-10-15 14:58:11 +0100204#endif /* TZC_COMMON_PRIVATE_H */