xref: /rk3399_ARM-atf/lib/gpt_rme/gpt_rme.c (revision e50fedbc869341d044d4cb3479a0ab3d4edaf225)
1f19dc624Sjohpow01 /*
26a00e9b0SRobert Wakim  * Copyright (c) 2022, Arm Limited. All rights reserved.
3f19dc624Sjohpow01  *
4f19dc624Sjohpow01  * SPDX-License-Identifier: BSD-3-Clause
5f19dc624Sjohpow01  */
6f19dc624Sjohpow01 
7f19dc624Sjohpow01 #include <assert.h>
8f19dc624Sjohpow01 #include <errno.h>
92461bd3aSManish Pandey #include <inttypes.h>
10f19dc624Sjohpow01 #include <limits.h>
11f19dc624Sjohpow01 #include <stdint.h>
12f19dc624Sjohpow01 
13f19dc624Sjohpow01 #include <arch.h>
14f19dc624Sjohpow01 #include <arch_helpers.h>
15f19dc624Sjohpow01 #include <common/debug.h>
16f19dc624Sjohpow01 #include "gpt_rme_private.h"
17f19dc624Sjohpow01 #include <lib/gpt_rme/gpt_rme.h>
18f19dc624Sjohpow01 #include <lib/smccc.h>
19f19dc624Sjohpow01 #include <lib/spinlock.h>
20f19dc624Sjohpow01 #include <lib/xlat_tables/xlat_tables_v2.h>
21f19dc624Sjohpow01 
22f19dc624Sjohpow01 #if !ENABLE_RME
23f19dc624Sjohpow01 #error "ENABLE_RME must be enabled to use the GPT library."
24f19dc624Sjohpow01 #endif
25f19dc624Sjohpow01 
26f19dc624Sjohpow01 /*
27f19dc624Sjohpow01  * Lookup T from PPS
28f19dc624Sjohpow01  *
29f19dc624Sjohpow01  *   PPS    Size    T
30f19dc624Sjohpow01  *   0b000  4GB     32
31f19dc624Sjohpow01  *   0b001  64GB    36
32f19dc624Sjohpow01  *   0b010  1TB     40
33f19dc624Sjohpow01  *   0b011  4TB     42
34f19dc624Sjohpow01  *   0b100  16TB    44
35f19dc624Sjohpow01  *   0b101  256TB   48
36f19dc624Sjohpow01  *   0b110  4PB     52
37f19dc624Sjohpow01  *
38f19dc624Sjohpow01  * See section 15.1.27 of the RME specification.
39f19dc624Sjohpow01  */
40f19dc624Sjohpow01 static const gpt_t_val_e gpt_t_lookup[] = {PPS_4GB_T, PPS_64GB_T,
41f19dc624Sjohpow01 					   PPS_1TB_T, PPS_4TB_T,
42f19dc624Sjohpow01 					   PPS_16TB_T, PPS_256TB_T,
43f19dc624Sjohpow01 					   PPS_4PB_T};
44f19dc624Sjohpow01 
45f19dc624Sjohpow01 /*
46f19dc624Sjohpow01  * Lookup P from PGS
47f19dc624Sjohpow01  *
48f19dc624Sjohpow01  *   PGS    Size    P
49f19dc624Sjohpow01  *   0b00   4KB     12
50f19dc624Sjohpow01  *   0b10   16KB    14
51f19dc624Sjohpow01  *   0b01   64KB    16
52f19dc624Sjohpow01  *
53f19dc624Sjohpow01  * Note that pgs=0b10 is 16KB and pgs=0b01 is 64KB, this is not a typo.
54f19dc624Sjohpow01  *
55f19dc624Sjohpow01  * See section 15.1.27 of the RME specification.
56f19dc624Sjohpow01  */
57f19dc624Sjohpow01 static const gpt_p_val_e gpt_p_lookup[] = {PGS_4KB_P, PGS_64KB_P, PGS_16KB_P};
58f19dc624Sjohpow01 
59f19dc624Sjohpow01 /*
60f19dc624Sjohpow01  * This structure contains GPT configuration data.
61f19dc624Sjohpow01  */
62f19dc624Sjohpow01 typedef struct {
63f19dc624Sjohpow01 	uintptr_t plat_gpt_l0_base;
64f19dc624Sjohpow01 	gpccr_pps_e pps;
65f19dc624Sjohpow01 	gpt_t_val_e t;
66f19dc624Sjohpow01 	gpccr_pgs_e pgs;
67f19dc624Sjohpow01 	gpt_p_val_e p;
68f19dc624Sjohpow01 } gpt_config_t;
69f19dc624Sjohpow01 
70f19dc624Sjohpow01 static gpt_config_t gpt_config;
71f19dc624Sjohpow01 
72f19dc624Sjohpow01 /* These variables are used during initialization of the L1 tables. */
73f19dc624Sjohpow01 static unsigned int gpt_next_l1_tbl_idx;
74f19dc624Sjohpow01 static uintptr_t gpt_l1_tbl;
75f19dc624Sjohpow01 
76f19dc624Sjohpow01 /*
77f19dc624Sjohpow01  * This function checks to see if a GPI value is valid.
78f19dc624Sjohpow01  *
79f19dc624Sjohpow01  * These are valid GPI values.
80f19dc624Sjohpow01  *   GPT_GPI_NO_ACCESS   U(0x0)
81f19dc624Sjohpow01  *   GPT_GPI_SECURE      U(0x8)
82f19dc624Sjohpow01  *   GPT_GPI_NS          U(0x9)
83f19dc624Sjohpow01  *   GPT_GPI_ROOT        U(0xA)
84f19dc624Sjohpow01  *   GPT_GPI_REALM       U(0xB)
85f19dc624Sjohpow01  *   GPT_GPI_ANY         U(0xF)
86f19dc624Sjohpow01  *
87f19dc624Sjohpow01  * Parameters
88f19dc624Sjohpow01  *   gpi		GPI to check for validity.
89f19dc624Sjohpow01  *
90f19dc624Sjohpow01  * Return
91f19dc624Sjohpow01  *   true for a valid GPI, false for an invalid one.
92f19dc624Sjohpow01  */
93f19dc624Sjohpow01 static bool gpt_is_gpi_valid(unsigned int gpi)
94f19dc624Sjohpow01 {
95f19dc624Sjohpow01 	if ((gpi == GPT_GPI_NO_ACCESS) || (gpi == GPT_GPI_ANY) ||
96f19dc624Sjohpow01 	    ((gpi >= GPT_GPI_SECURE) && (gpi <= GPT_GPI_REALM))) {
97f19dc624Sjohpow01 		return true;
98f19dc624Sjohpow01 	}
996a00e9b0SRobert Wakim 	return false;
100f19dc624Sjohpow01 }
101f19dc624Sjohpow01 
102f19dc624Sjohpow01 /*
103f19dc624Sjohpow01  * This function checks to see if two PAS regions overlap.
104f19dc624Sjohpow01  *
105f19dc624Sjohpow01  * Parameters
106f19dc624Sjohpow01  *   base_1: base address of first PAS
107f19dc624Sjohpow01  *   size_1: size of first PAS
108f19dc624Sjohpow01  *   base_2: base address of second PAS
109f19dc624Sjohpow01  *   size_2: size of second PAS
110f19dc624Sjohpow01  *
111f19dc624Sjohpow01  * Return
112f19dc624Sjohpow01  *   True if PAS regions overlap, false if they do not.
113f19dc624Sjohpow01  */
114f19dc624Sjohpow01 static bool gpt_check_pas_overlap(uintptr_t base_1, size_t size_1,
115f19dc624Sjohpow01 				  uintptr_t base_2, size_t size_2)
116f19dc624Sjohpow01 {
117f19dc624Sjohpow01 	if (((base_1 + size_1) > base_2) && ((base_2 + size_2) > base_1)) {
118f19dc624Sjohpow01 		return true;
119f19dc624Sjohpow01 	}
1206a00e9b0SRobert Wakim 	return false;
121f19dc624Sjohpow01 }
122f19dc624Sjohpow01 
123f19dc624Sjohpow01 /*
124f19dc624Sjohpow01  * This helper function checks to see if a PAS region from index 0 to
125f19dc624Sjohpow01  * (pas_idx - 1) occupies the L0 region at index l0_idx in the L0 table.
126f19dc624Sjohpow01  *
127f19dc624Sjohpow01  * Parameters
128f19dc624Sjohpow01  *   l0_idx:      Index of the L0 entry to check
129f19dc624Sjohpow01  *   pas_regions: PAS region array
130f19dc624Sjohpow01  *   pas_idx:     Upper bound of the PAS array index.
131f19dc624Sjohpow01  *
132f19dc624Sjohpow01  * Return
133f19dc624Sjohpow01  *   True if a PAS region occupies the L0 region in question, false if not.
134f19dc624Sjohpow01  */
135f19dc624Sjohpow01 static bool gpt_does_previous_pas_exist_here(unsigned int l0_idx,
136f19dc624Sjohpow01 					     pas_region_t *pas_regions,
137f19dc624Sjohpow01 					     unsigned int pas_idx)
138f19dc624Sjohpow01 {
139f19dc624Sjohpow01 	/* Iterate over PAS regions up to pas_idx. */
140f19dc624Sjohpow01 	for (unsigned int i = 0U; i < pas_idx; i++) {
141f19dc624Sjohpow01 		if (gpt_check_pas_overlap((GPT_L0GPTSZ_ACTUAL_SIZE * l0_idx),
142f19dc624Sjohpow01 		    GPT_L0GPTSZ_ACTUAL_SIZE,
143f19dc624Sjohpow01 		    pas_regions[i].base_pa, pas_regions[i].size)) {
144f19dc624Sjohpow01 			return true;
145f19dc624Sjohpow01 		}
146f19dc624Sjohpow01 	}
147f19dc624Sjohpow01 	return false;
148f19dc624Sjohpow01 }
149f19dc624Sjohpow01 
150f19dc624Sjohpow01 /*
151f19dc624Sjohpow01  * This function iterates over all of the PAS regions and checks them to ensure
152f19dc624Sjohpow01  * proper alignment of base and size, that the GPI is valid, and that no regions
153f19dc624Sjohpow01  * overlap. As a part of the overlap checks, this function checks existing L0
154f19dc624Sjohpow01  * mappings against the new PAS regions in the event that gpt_init_pas_l1_tables
155f19dc624Sjohpow01  * is called multiple times to place L1 tables in different areas of memory. It
156f19dc624Sjohpow01  * also counts the number of L1 tables needed and returns it on success.
157f19dc624Sjohpow01  *
158f19dc624Sjohpow01  * Parameters
159f19dc624Sjohpow01  *   *pas_regions	Pointer to array of PAS region structures.
160f19dc624Sjohpow01  *   pas_region_cnt	Total number of PAS regions in the array.
161f19dc624Sjohpow01  *
162f19dc624Sjohpow01  * Return
163f19dc624Sjohpow01  *   Negative Linux error code in the event of a failure, number of L1 regions
164f19dc624Sjohpow01  *   required when successful.
165f19dc624Sjohpow01  */
166f19dc624Sjohpow01 static int gpt_validate_pas_mappings(pas_region_t *pas_regions,
167f19dc624Sjohpow01 				     unsigned int pas_region_cnt)
168f19dc624Sjohpow01 {
169f19dc624Sjohpow01 	unsigned int idx;
170f19dc624Sjohpow01 	unsigned int l1_cnt = 0U;
171f19dc624Sjohpow01 	unsigned int pas_l1_cnt;
172f19dc624Sjohpow01 	uint64_t *l0_desc = (uint64_t *)gpt_config.plat_gpt_l0_base;
173f19dc624Sjohpow01 
174f19dc624Sjohpow01 	assert(pas_regions != NULL);
175f19dc624Sjohpow01 	assert(pas_region_cnt != 0U);
176f19dc624Sjohpow01 
177f19dc624Sjohpow01 	for (idx = 0U; idx < pas_region_cnt; idx++) {
178f19dc624Sjohpow01 		/* Check for arithmetic overflow in region. */
179f19dc624Sjohpow01 		if ((ULONG_MAX - pas_regions[idx].base_pa) <
180f19dc624Sjohpow01 		    pas_regions[idx].size) {
181f19dc624Sjohpow01 			ERROR("[GPT] Address overflow in PAS[%u]!\n", idx);
182f19dc624Sjohpow01 			return -EOVERFLOW;
183f19dc624Sjohpow01 		}
184f19dc624Sjohpow01 
185f19dc624Sjohpow01 		/* Initial checks for PAS validity. */
186f19dc624Sjohpow01 		if (((pas_regions[idx].base_pa + pas_regions[idx].size) >
187f19dc624Sjohpow01 		    GPT_PPS_ACTUAL_SIZE(gpt_config.t)) ||
188f19dc624Sjohpow01 		    !gpt_is_gpi_valid(GPT_PAS_ATTR_GPI(pas_regions[idx].attrs))) {
189f19dc624Sjohpow01 			ERROR("[GPT] PAS[%u] is invalid!\n", idx);
190f19dc624Sjohpow01 			return -EFAULT;
191f19dc624Sjohpow01 		}
192f19dc624Sjohpow01 
193f19dc624Sjohpow01 		/*
194f19dc624Sjohpow01 		 * Make sure this PAS does not overlap with another one. We
195f19dc624Sjohpow01 		 * start from idx + 1 instead of 0 since prior PAS mappings will
196f19dc624Sjohpow01 		 * have already checked themselves against this one.
197f19dc624Sjohpow01 		 */
198f19dc624Sjohpow01 		for (unsigned int i = idx + 1; i < pas_region_cnt; i++) {
199f19dc624Sjohpow01 			if (gpt_check_pas_overlap(pas_regions[idx].base_pa,
200f19dc624Sjohpow01 			    pas_regions[idx].size,
201f19dc624Sjohpow01 			    pas_regions[i].base_pa,
202f19dc624Sjohpow01 			    pas_regions[i].size)) {
203f19dc624Sjohpow01 				ERROR("[GPT] PAS[%u] overlaps with PAS[%u]\n",
204f19dc624Sjohpow01 					i, idx);
205f19dc624Sjohpow01 				return -EFAULT;
206f19dc624Sjohpow01 			}
207f19dc624Sjohpow01 		}
208f19dc624Sjohpow01 
209f19dc624Sjohpow01 		/*
210f19dc624Sjohpow01 		 * Since this function can be called multiple times with
211f19dc624Sjohpow01 		 * separate L1 tables we need to check the existing L0 mapping
212f19dc624Sjohpow01 		 * to see if this PAS would fall into one that has already been
213f19dc624Sjohpow01 		 * initialized.
214f19dc624Sjohpow01 		 */
215f19dc624Sjohpow01 		for (unsigned int i = GPT_L0_IDX(pas_regions[idx].base_pa);
216f19dc624Sjohpow01 		     i <= GPT_L0_IDX(pas_regions[idx].base_pa + pas_regions[idx].size - 1);
217f19dc624Sjohpow01 		     i++) {
218f19dc624Sjohpow01 			if ((GPT_L0_TYPE(l0_desc[i]) == GPT_L0_TYPE_BLK_DESC) &&
219f19dc624Sjohpow01 			    (GPT_L0_BLKD_GPI(l0_desc[i]) == GPT_GPI_ANY)) {
220f19dc624Sjohpow01 				/* This descriptor is unused so continue. */
221f19dc624Sjohpow01 				continue;
222f19dc624Sjohpow01 			}
223f19dc624Sjohpow01 
224f19dc624Sjohpow01 			/*
225f19dc624Sjohpow01 			 * This descriptor has been initialized in a previous
226f19dc624Sjohpow01 			 * call to this function so cannot be initialized again.
227f19dc624Sjohpow01 			 */
228f19dc624Sjohpow01 			ERROR("[GPT] PAS[%u] overlaps with previous L0[%d]!\n",
229f19dc624Sjohpow01 			      idx, i);
230f19dc624Sjohpow01 			return -EFAULT;
231f19dc624Sjohpow01 		}
232f19dc624Sjohpow01 
233f19dc624Sjohpow01 		/* Check for block mapping (L0) type. */
234f19dc624Sjohpow01 		if (GPT_PAS_ATTR_MAP_TYPE(pas_regions[idx].attrs) ==
235f19dc624Sjohpow01 		    GPT_PAS_ATTR_MAP_TYPE_BLOCK) {
236f19dc624Sjohpow01 			/* Make sure base and size are block-aligned. */
237f19dc624Sjohpow01 			if (!GPT_IS_L0_ALIGNED(pas_regions[idx].base_pa) ||
238f19dc624Sjohpow01 			    !GPT_IS_L0_ALIGNED(pas_regions[idx].size)) {
239f19dc624Sjohpow01 				ERROR("[GPT] PAS[%u] is not block-aligned!\n",
240f19dc624Sjohpow01 				      idx);
241f19dc624Sjohpow01 				return -EFAULT;
242f19dc624Sjohpow01 			}
243f19dc624Sjohpow01 
244f19dc624Sjohpow01 			continue;
245f19dc624Sjohpow01 		}
246f19dc624Sjohpow01 
247f19dc624Sjohpow01 		/* Check for granule mapping (L1) type. */
248f19dc624Sjohpow01 		if (GPT_PAS_ATTR_MAP_TYPE(pas_regions[idx].attrs) ==
249f19dc624Sjohpow01 		    GPT_PAS_ATTR_MAP_TYPE_GRANULE) {
250f19dc624Sjohpow01 			/* Make sure base and size are granule-aligned. */
251f19dc624Sjohpow01 			if (!GPT_IS_L1_ALIGNED(gpt_config.p, pas_regions[idx].base_pa) ||
252f19dc624Sjohpow01 			    !GPT_IS_L1_ALIGNED(gpt_config.p, pas_regions[idx].size)) {
253f19dc624Sjohpow01 				ERROR("[GPT] PAS[%u] is not granule-aligned!\n",
254f19dc624Sjohpow01 				      idx);
255f19dc624Sjohpow01 				return -EFAULT;
256f19dc624Sjohpow01 			}
257f19dc624Sjohpow01 
258f19dc624Sjohpow01 			/* Find how many L1 tables this PAS occupies. */
259f19dc624Sjohpow01 			pas_l1_cnt = (GPT_L0_IDX(pas_regions[idx].base_pa +
260f19dc624Sjohpow01 				     pas_regions[idx].size - 1) -
261f19dc624Sjohpow01 				     GPT_L0_IDX(pas_regions[idx].base_pa) + 1);
262f19dc624Sjohpow01 
263f19dc624Sjohpow01 			/*
264f19dc624Sjohpow01 			 * This creates a situation where, if multiple PAS
265f19dc624Sjohpow01 			 * regions occupy the same table descriptor, we can get
266f19dc624Sjohpow01 			 * an artificially high total L1 table count. The way we
267f19dc624Sjohpow01 			 * handle this is by checking each PAS against those
268f19dc624Sjohpow01 			 * before it in the array, and if they both occupy the
269f19dc624Sjohpow01 			 * same PAS we subtract from pas_l1_cnt and only the
270f19dc624Sjohpow01 			 * first PAS in the array gets to count it.
271f19dc624Sjohpow01 			 */
272f19dc624Sjohpow01 
273f19dc624Sjohpow01 			/*
274f19dc624Sjohpow01 			 * If L1 count is greater than 1 we know the start and
275f19dc624Sjohpow01 			 * end PAs are in different L0 regions so we must check
276f19dc624Sjohpow01 			 * both for overlap against other PAS.
277f19dc624Sjohpow01 			 */
278f19dc624Sjohpow01 			if (pas_l1_cnt > 1) {
279f19dc624Sjohpow01 				if (gpt_does_previous_pas_exist_here(
280f19dc624Sjohpow01 				    GPT_L0_IDX(pas_regions[idx].base_pa +
281f19dc624Sjohpow01 				    pas_regions[idx].size - 1),
282f19dc624Sjohpow01 				    pas_regions, idx)) {
283f19dc624Sjohpow01 					pas_l1_cnt = pas_l1_cnt - 1;
284f19dc624Sjohpow01 				}
285f19dc624Sjohpow01 			}
286f19dc624Sjohpow01 
287f19dc624Sjohpow01 			if (gpt_does_previous_pas_exist_here(
288f19dc624Sjohpow01 			    GPT_L0_IDX(pas_regions[idx].base_pa),
289f19dc624Sjohpow01 			    pas_regions, idx)) {
290f19dc624Sjohpow01 				pas_l1_cnt = pas_l1_cnt - 1;
291f19dc624Sjohpow01 			}
292f19dc624Sjohpow01 
293f19dc624Sjohpow01 			l1_cnt += pas_l1_cnt;
294f19dc624Sjohpow01 			continue;
295f19dc624Sjohpow01 		}
296f19dc624Sjohpow01 
297f19dc624Sjohpow01 		/* If execution reaches this point, mapping type is invalid. */
298f19dc624Sjohpow01 		ERROR("[GPT] PAS[%u] has invalid mapping type 0x%x.\n", idx,
299f19dc624Sjohpow01 		      GPT_PAS_ATTR_MAP_TYPE(pas_regions[idx].attrs));
300f19dc624Sjohpow01 		return -EINVAL;
301f19dc624Sjohpow01 	}
302f19dc624Sjohpow01 
303f19dc624Sjohpow01 	return l1_cnt;
304f19dc624Sjohpow01 }
305f19dc624Sjohpow01 
306f19dc624Sjohpow01 /*
307f19dc624Sjohpow01  * This function validates L0 initialization parameters.
308f19dc624Sjohpow01  *
309f19dc624Sjohpow01  * Parameters
310f19dc624Sjohpow01  *   l0_mem_base	Base address of memory used for L0 tables.
311f19dc624Sjohpow01  *   l1_mem_size	Size of memory available for L0 tables.
312f19dc624Sjohpow01  *
313f19dc624Sjohpow01  * Return
314f19dc624Sjohpow01  *   Negative Linux error code in the event of a failure, 0 for success.
315f19dc624Sjohpow01  */
316f19dc624Sjohpow01 static int gpt_validate_l0_params(gpccr_pps_e pps, uintptr_t l0_mem_base,
317f19dc624Sjohpow01 				  size_t l0_mem_size)
318f19dc624Sjohpow01 {
319f19dc624Sjohpow01 	size_t l0_alignment;
320f19dc624Sjohpow01 
321f19dc624Sjohpow01 	/*
322f19dc624Sjohpow01 	 * Make sure PPS is valid and then store it since macros need this value
323f19dc624Sjohpow01 	 * to work.
324f19dc624Sjohpow01 	 */
325f19dc624Sjohpow01 	if (pps > GPT_PPS_MAX) {
326f19dc624Sjohpow01 		ERROR("[GPT] Invalid PPS: 0x%x\n", pps);
327f19dc624Sjohpow01 		return -EINVAL;
328f19dc624Sjohpow01 	}
329f19dc624Sjohpow01 	gpt_config.pps = pps;
330f19dc624Sjohpow01 	gpt_config.t = gpt_t_lookup[pps];
331f19dc624Sjohpow01 
332f19dc624Sjohpow01 	/* Alignment must be the greater of 4k or l0 table size. */
333f19dc624Sjohpow01 	l0_alignment = PAGE_SIZE_4KB;
334f19dc624Sjohpow01 	if (l0_alignment < GPT_L0_TABLE_SIZE(gpt_config.t)) {
335f19dc624Sjohpow01 		l0_alignment = GPT_L0_TABLE_SIZE(gpt_config.t);
336f19dc624Sjohpow01 	}
337f19dc624Sjohpow01 
338f19dc624Sjohpow01 	/* Check base address. */
339f19dc624Sjohpow01 	if ((l0_mem_base == 0U) || ((l0_mem_base & (l0_alignment - 1)) != 0U)) {
340f19dc624Sjohpow01 		ERROR("[GPT] Invalid L0 base address: 0x%lx\n", l0_mem_base);
341f19dc624Sjohpow01 		return -EFAULT;
342f19dc624Sjohpow01 	}
343f19dc624Sjohpow01 
344f19dc624Sjohpow01 	/* Check size. */
345f19dc624Sjohpow01 	if (l0_mem_size < GPT_L0_TABLE_SIZE(gpt_config.t)) {
346f19dc624Sjohpow01 		ERROR("[GPT] Inadequate L0 memory: need 0x%lx, have 0x%lx)\n",
347f19dc624Sjohpow01 		      GPT_L0_TABLE_SIZE(gpt_config.t),
348f19dc624Sjohpow01 		      l0_mem_size);
349f19dc624Sjohpow01 		return -ENOMEM;
350f19dc624Sjohpow01 	}
351f19dc624Sjohpow01 
352f19dc624Sjohpow01 	return 0;
353f19dc624Sjohpow01 }
354f19dc624Sjohpow01 
355f19dc624Sjohpow01 /*
356f19dc624Sjohpow01  * In the event that L1 tables are needed, this function validates
357f19dc624Sjohpow01  * the L1 table generation parameters.
358f19dc624Sjohpow01  *
359f19dc624Sjohpow01  * Parameters
360f19dc624Sjohpow01  *   l1_mem_base	Base address of memory used for L1 table allocation.
361f19dc624Sjohpow01  *   l1_mem_size	Total size of memory available for L1 tables.
362f19dc624Sjohpow01  *   l1_gpt_cnt		Number of L1 tables needed.
363f19dc624Sjohpow01  *
364f19dc624Sjohpow01  * Return
365f19dc624Sjohpow01  *   Negative Linux error code in the event of a failure, 0 for success.
366f19dc624Sjohpow01  */
367f19dc624Sjohpow01 static int gpt_validate_l1_params(uintptr_t l1_mem_base, size_t l1_mem_size,
368f19dc624Sjohpow01 				  unsigned int l1_gpt_cnt)
369f19dc624Sjohpow01 {
370f19dc624Sjohpow01 	size_t l1_gpt_mem_sz;
371f19dc624Sjohpow01 
372f19dc624Sjohpow01 	/* Check if the granularity is supported */
373f19dc624Sjohpow01 	if (!xlat_arch_is_granule_size_supported(
374f19dc624Sjohpow01 	    GPT_PGS_ACTUAL_SIZE(gpt_config.p))) {
375f19dc624Sjohpow01 		return -EPERM;
376f19dc624Sjohpow01 	}
377f19dc624Sjohpow01 
378f19dc624Sjohpow01 	/* Make sure L1 tables are aligned to their size. */
379f19dc624Sjohpow01 	if ((l1_mem_base & (GPT_L1_TABLE_SIZE(gpt_config.p) - 1)) != 0U) {
380f19dc624Sjohpow01 		ERROR("[GPT] Unaligned L1 GPT base address: 0x%lx\n",
381f19dc624Sjohpow01 		      l1_mem_base);
382f19dc624Sjohpow01 		return -EFAULT;
383f19dc624Sjohpow01 	}
384f19dc624Sjohpow01 
385f19dc624Sjohpow01 	/* Get total memory needed for L1 tables. */
386f19dc624Sjohpow01 	l1_gpt_mem_sz = l1_gpt_cnt * GPT_L1_TABLE_SIZE(gpt_config.p);
387f19dc624Sjohpow01 
388f19dc624Sjohpow01 	/* Check for overflow. */
389f19dc624Sjohpow01 	if ((l1_gpt_mem_sz / GPT_L1_TABLE_SIZE(gpt_config.p)) != l1_gpt_cnt) {
390f19dc624Sjohpow01 		ERROR("[GPT] Overflow calculating L1 memory size.\n");
391f19dc624Sjohpow01 		return -ENOMEM;
392f19dc624Sjohpow01 	}
393f19dc624Sjohpow01 
394f19dc624Sjohpow01 	/* Make sure enough space was supplied. */
395f19dc624Sjohpow01 	if (l1_mem_size < l1_gpt_mem_sz) {
396f19dc624Sjohpow01 		ERROR("[GPT] Inadequate memory for L1 GPTs. ");
397f19dc624Sjohpow01 		ERROR("      Expected 0x%lx bytes. Got 0x%lx bytes\n",
398f19dc624Sjohpow01 		      l1_gpt_mem_sz, l1_mem_size);
399f19dc624Sjohpow01 		return -ENOMEM;
400f19dc624Sjohpow01 	}
401f19dc624Sjohpow01 
402f19dc624Sjohpow01 	VERBOSE("[GPT] Requested 0x%lx bytes for L1 GPTs.\n", l1_gpt_mem_sz);
403f19dc624Sjohpow01 	return 0;
404f19dc624Sjohpow01 }
405f19dc624Sjohpow01 
406f19dc624Sjohpow01 /*
407f19dc624Sjohpow01  * This function initializes L0 block descriptors (regions that cannot be
408f19dc624Sjohpow01  * transitioned at the granule level) according to the provided PAS.
409f19dc624Sjohpow01  *
410f19dc624Sjohpow01  * Parameters
411f19dc624Sjohpow01  *   *pas		Pointer to the structure defining the PAS region to
412f19dc624Sjohpow01  *			initialize.
413f19dc624Sjohpow01  */
414f19dc624Sjohpow01 static void gpt_generate_l0_blk_desc(pas_region_t *pas)
415f19dc624Sjohpow01 {
416f19dc624Sjohpow01 	uint64_t gpt_desc;
417f19dc624Sjohpow01 	unsigned int end_idx;
418f19dc624Sjohpow01 	unsigned int idx;
419f19dc624Sjohpow01 	uint64_t *l0_gpt_arr;
420f19dc624Sjohpow01 
421f19dc624Sjohpow01 	assert(gpt_config.plat_gpt_l0_base != 0U);
422f19dc624Sjohpow01 	assert(pas != NULL);
423f19dc624Sjohpow01 
424f19dc624Sjohpow01 	/*
425f19dc624Sjohpow01 	 * Checking of PAS parameters has already been done in
426f19dc624Sjohpow01 	 * gpt_validate_pas_mappings so no need to check the same things again.
427f19dc624Sjohpow01 	 */
428f19dc624Sjohpow01 
429f19dc624Sjohpow01 	l0_gpt_arr = (uint64_t *)gpt_config.plat_gpt_l0_base;
430f19dc624Sjohpow01 
431f19dc624Sjohpow01 	/* Create the GPT Block descriptor for this PAS region */
432f19dc624Sjohpow01 	gpt_desc = GPT_L0_BLK_DESC(GPT_PAS_ATTR_GPI(pas->attrs));
433f19dc624Sjohpow01 
434f19dc624Sjohpow01 	/* Start index of this region in L0 GPTs */
4356a00e9b0SRobert Wakim 	idx = GPT_L0_IDX(pas->base_pa);
436f19dc624Sjohpow01 
437f19dc624Sjohpow01 	/*
438f19dc624Sjohpow01 	 * Determine number of L0 GPT descriptors covered by
439f19dc624Sjohpow01 	 * this PAS region and use the count to populate these
440f19dc624Sjohpow01 	 * descriptors.
441f19dc624Sjohpow01 	 */
4426a00e9b0SRobert Wakim 	end_idx = GPT_L0_IDX(pas->base_pa + pas->size);
443f19dc624Sjohpow01 
444f19dc624Sjohpow01 	/* Generate the needed block descriptors. */
445f19dc624Sjohpow01 	for (; idx < end_idx; idx++) {
446f19dc624Sjohpow01 		l0_gpt_arr[idx] = gpt_desc;
4472461bd3aSManish Pandey 		VERBOSE("[GPT] L0 entry (BLOCK) index %u [%p]: GPI = 0x%" PRIx64 " (0x%" PRIx64 ")\n",
448f19dc624Sjohpow01 			idx, &l0_gpt_arr[idx],
449f19dc624Sjohpow01 			(gpt_desc >> GPT_L0_BLK_DESC_GPI_SHIFT) &
450f19dc624Sjohpow01 			GPT_L0_BLK_DESC_GPI_MASK, l0_gpt_arr[idx]);
451f19dc624Sjohpow01 	}
452f19dc624Sjohpow01 }
453f19dc624Sjohpow01 
454f19dc624Sjohpow01 /*
455f19dc624Sjohpow01  * Helper function to determine if the end physical address lies in the same L0
456f19dc624Sjohpow01  * region as the current physical address. If true, the end physical address is
457f19dc624Sjohpow01  * returned else, the start address of the next region is returned.
458f19dc624Sjohpow01  *
459f19dc624Sjohpow01  * Parameters
460f19dc624Sjohpow01  *   cur_pa		Physical address of the current PA in the loop through
461f19dc624Sjohpow01  *			the range.
462f19dc624Sjohpow01  *   end_pa		Physical address of the end PA in a PAS range.
463f19dc624Sjohpow01  *
464f19dc624Sjohpow01  * Return
465f19dc624Sjohpow01  *   The PA of the end of the current range.
466f19dc624Sjohpow01  */
467f19dc624Sjohpow01 static uintptr_t gpt_get_l1_end_pa(uintptr_t cur_pa, uintptr_t end_pa)
468f19dc624Sjohpow01 {
469f19dc624Sjohpow01 	uintptr_t cur_idx;
470f19dc624Sjohpow01 	uintptr_t end_idx;
471f19dc624Sjohpow01 
4726a00e9b0SRobert Wakim 	cur_idx = GPT_L0_IDX(cur_pa);
4736a00e9b0SRobert Wakim 	end_idx = GPT_L0_IDX(end_pa);
474f19dc624Sjohpow01 
475f19dc624Sjohpow01 	assert(cur_idx <= end_idx);
476f19dc624Sjohpow01 
477f19dc624Sjohpow01 	if (cur_idx == end_idx) {
478f19dc624Sjohpow01 		return end_pa;
479f19dc624Sjohpow01 	}
480f19dc624Sjohpow01 
481f19dc624Sjohpow01 	return (cur_idx + 1U) << GPT_L0_IDX_SHIFT;
482f19dc624Sjohpow01 }
483f19dc624Sjohpow01 
484f19dc624Sjohpow01 /*
485f19dc624Sjohpow01  * Helper function to fill out GPI entries in a single L1 table. This function
486f19dc624Sjohpow01  * fills out entire L1 descriptors at a time to save memory writes.
487f19dc624Sjohpow01  *
488f19dc624Sjohpow01  * Parameters
489f19dc624Sjohpow01  *   gpi		GPI to set this range to
490f19dc624Sjohpow01  *   l1			Pointer to L1 table to fill out
491f19dc624Sjohpow01  *   first		Address of first granule in range.
492f19dc624Sjohpow01  *   last		Address of last granule in range (inclusive).
493f19dc624Sjohpow01  */
494f19dc624Sjohpow01 static void gpt_fill_l1_tbl(uint64_t gpi, uint64_t *l1, uintptr_t first,
495f19dc624Sjohpow01 			    uintptr_t last)
496f19dc624Sjohpow01 {
497f19dc624Sjohpow01 	uint64_t gpi_field = GPT_BUILD_L1_DESC(gpi);
498f19dc624Sjohpow01 	uint64_t gpi_mask = 0xFFFFFFFFFFFFFFFF;
499f19dc624Sjohpow01 
500f19dc624Sjohpow01 	assert(first <= last);
501f19dc624Sjohpow01 	assert((first & (GPT_PGS_ACTUAL_SIZE(gpt_config.p) - 1)) == 0U);
502f19dc624Sjohpow01 	assert((last & (GPT_PGS_ACTUAL_SIZE(gpt_config.p) - 1)) == 0U);
503f19dc624Sjohpow01 	assert(GPT_L0_IDX(first) == GPT_L0_IDX(last));
504f19dc624Sjohpow01 	assert(l1 != NULL);
505f19dc624Sjohpow01 
506f19dc624Sjohpow01 	/* Shift the mask if we're starting in the middle of an L1 entry. */
507f19dc624Sjohpow01 	gpi_mask = gpi_mask << (GPT_L1_GPI_IDX(gpt_config.p, first) << 2);
508f19dc624Sjohpow01 
509f19dc624Sjohpow01 	/* Fill out each L1 entry for this region. */
510f19dc624Sjohpow01 	for (unsigned int i = GPT_L1_IDX(gpt_config.p, first);
511f19dc624Sjohpow01 	     i <= GPT_L1_IDX(gpt_config.p, last); i++) {
512f19dc624Sjohpow01 		/* Account for stopping in the middle of an L1 entry. */
513f19dc624Sjohpow01 		if (i == GPT_L1_IDX(gpt_config.p, last)) {
514f19dc624Sjohpow01 			gpi_mask &= (gpi_mask >> ((15 -
515f19dc624Sjohpow01 				    GPT_L1_GPI_IDX(gpt_config.p, last)) << 2));
516f19dc624Sjohpow01 		}
517f19dc624Sjohpow01 
518f19dc624Sjohpow01 		/* Write GPI values. */
519f19dc624Sjohpow01 		assert((l1[i] & gpi_mask) ==
520f19dc624Sjohpow01 		       (GPT_BUILD_L1_DESC(GPT_GPI_ANY) & gpi_mask));
521f19dc624Sjohpow01 		l1[i] = (l1[i] & ~gpi_mask) | (gpi_mask & gpi_field);
522f19dc624Sjohpow01 
523f19dc624Sjohpow01 		/* Reset mask. */
524f19dc624Sjohpow01 		gpi_mask = 0xFFFFFFFFFFFFFFFF;
525f19dc624Sjohpow01 	}
526f19dc624Sjohpow01 }
527f19dc624Sjohpow01 
528f19dc624Sjohpow01 /*
529f19dc624Sjohpow01  * This function finds the next available unused L1 table and initializes all
530f19dc624Sjohpow01  * granules descriptor entries to GPI_ANY. This ensures that there are no chunks
531f19dc624Sjohpow01  * of GPI_NO_ACCESS (0b0000) memory floating around in the system in the
532f19dc624Sjohpow01  * event that a PAS region stops midway through an L1 table, thus guaranteeing
533f19dc624Sjohpow01  * that all memory not explicitly assigned is GPI_ANY. This function does not
534f19dc624Sjohpow01  * check for overflow conditions, that should be done by the caller.
535f19dc624Sjohpow01  *
536f19dc624Sjohpow01  * Return
537f19dc624Sjohpow01  *   Pointer to the next available L1 table.
538f19dc624Sjohpow01  */
539f19dc624Sjohpow01 static uint64_t *gpt_get_new_l1_tbl(void)
540f19dc624Sjohpow01 {
541f19dc624Sjohpow01 	/* Retrieve the next L1 table. */
542f19dc624Sjohpow01 	uint64_t *l1 = (uint64_t *)((uint64_t)(gpt_l1_tbl) +
543f19dc624Sjohpow01 		       (GPT_L1_TABLE_SIZE(gpt_config.p) *
544f19dc624Sjohpow01 		       gpt_next_l1_tbl_idx));
545f19dc624Sjohpow01 
546f19dc624Sjohpow01 	/* Increment L1 counter. */
547f19dc624Sjohpow01 	gpt_next_l1_tbl_idx++;
548f19dc624Sjohpow01 
549f19dc624Sjohpow01 	/* Initialize all GPIs to GPT_GPI_ANY */
550f19dc624Sjohpow01 	for (unsigned int i = 0U; i < GPT_L1_ENTRY_COUNT(gpt_config.p); i++) {
551f19dc624Sjohpow01 		l1[i] = GPT_BUILD_L1_DESC(GPT_GPI_ANY);
552f19dc624Sjohpow01 	}
553f19dc624Sjohpow01 
554f19dc624Sjohpow01 	return l1;
555f19dc624Sjohpow01 }
556f19dc624Sjohpow01 
557f19dc624Sjohpow01 /*
558f19dc624Sjohpow01  * When L1 tables are needed, this function creates the necessary L0 table
559f19dc624Sjohpow01  * descriptors and fills out the L1 table entries according to the supplied
560f19dc624Sjohpow01  * PAS range.
561f19dc624Sjohpow01  *
562f19dc624Sjohpow01  * Parameters
563f19dc624Sjohpow01  *   *pas		Pointer to the structure defining the PAS region.
564f19dc624Sjohpow01  */
565f19dc624Sjohpow01 static void gpt_generate_l0_tbl_desc(pas_region_t *pas)
566f19dc624Sjohpow01 {
567f19dc624Sjohpow01 	uintptr_t end_pa;
568f19dc624Sjohpow01 	uintptr_t cur_pa;
569f19dc624Sjohpow01 	uintptr_t last_gran_pa;
570f19dc624Sjohpow01 	uint64_t *l0_gpt_base;
571f19dc624Sjohpow01 	uint64_t *l1_gpt_arr;
572f19dc624Sjohpow01 	unsigned int l0_idx;
573f19dc624Sjohpow01 
574f19dc624Sjohpow01 	assert(gpt_config.plat_gpt_l0_base != 0U);
575f19dc624Sjohpow01 	assert(pas != NULL);
576f19dc624Sjohpow01 
577f19dc624Sjohpow01 	/*
578f19dc624Sjohpow01 	 * Checking of PAS parameters has already been done in
579f19dc624Sjohpow01 	 * gpt_validate_pas_mappings so no need to check the same things again.
580f19dc624Sjohpow01 	 */
581f19dc624Sjohpow01 
582f19dc624Sjohpow01 	end_pa = pas->base_pa + pas->size;
583f19dc624Sjohpow01 	l0_gpt_base = (uint64_t *)gpt_config.plat_gpt_l0_base;
584f19dc624Sjohpow01 
585f19dc624Sjohpow01 	/* We start working from the granule at base PA */
586f19dc624Sjohpow01 	cur_pa = pas->base_pa;
587f19dc624Sjohpow01 
588f19dc624Sjohpow01 	/* Iterate over each L0 region in this memory range. */
589f19dc624Sjohpow01 	for (l0_idx = GPT_L0_IDX(pas->base_pa);
590f19dc624Sjohpow01 	     l0_idx <= GPT_L0_IDX(end_pa - 1U);
591f19dc624Sjohpow01 	     l0_idx++) {
592f19dc624Sjohpow01 
593f19dc624Sjohpow01 		/*
594f19dc624Sjohpow01 		 * See if the L0 entry is already a table descriptor or if we
595f19dc624Sjohpow01 		 * need to create one.
596f19dc624Sjohpow01 		 */
597f19dc624Sjohpow01 		if (GPT_L0_TYPE(l0_gpt_base[l0_idx]) == GPT_L0_TYPE_TBL_DESC) {
598f19dc624Sjohpow01 			/* Get the L1 array from the L0 entry. */
599f19dc624Sjohpow01 			l1_gpt_arr = GPT_L0_TBLD_ADDR(l0_gpt_base[l0_idx]);
600f19dc624Sjohpow01 		} else {
601f19dc624Sjohpow01 			/* Get a new L1 table from the L1 memory space. */
602f19dc624Sjohpow01 			l1_gpt_arr = gpt_get_new_l1_tbl();
603f19dc624Sjohpow01 
604f19dc624Sjohpow01 			/* Fill out the L0 descriptor and flush it. */
605f19dc624Sjohpow01 			l0_gpt_base[l0_idx] = GPT_L0_TBL_DESC(l1_gpt_arr);
606f19dc624Sjohpow01 		}
607f19dc624Sjohpow01 
6082461bd3aSManish Pandey 		VERBOSE("[GPT] L0 entry (TABLE) index %u [%p] ==> L1 Addr 0x%llx (0x%" PRIx64 ")\n",
609f19dc624Sjohpow01 			l0_idx, &l0_gpt_base[l0_idx],
610f19dc624Sjohpow01 			(unsigned long long)(l1_gpt_arr),
611f19dc624Sjohpow01 			l0_gpt_base[l0_idx]);
612f19dc624Sjohpow01 
613f19dc624Sjohpow01 		/*
614f19dc624Sjohpow01 		 * Determine the PA of the last granule in this L0 descriptor.
615f19dc624Sjohpow01 		 */
616f19dc624Sjohpow01 		last_gran_pa = gpt_get_l1_end_pa(cur_pa, end_pa) -
617f19dc624Sjohpow01 			       GPT_PGS_ACTUAL_SIZE(gpt_config.p);
618f19dc624Sjohpow01 
619f19dc624Sjohpow01 		/*
620f19dc624Sjohpow01 		 * Fill up L1 GPT entries between these two addresses. This
621f19dc624Sjohpow01 		 * function needs the addresses of the first granule and last
622f19dc624Sjohpow01 		 * granule in the range.
623f19dc624Sjohpow01 		 */
624f19dc624Sjohpow01 		gpt_fill_l1_tbl(GPT_PAS_ATTR_GPI(pas->attrs), l1_gpt_arr,
625f19dc624Sjohpow01 				cur_pa, last_gran_pa);
626f19dc624Sjohpow01 
627f19dc624Sjohpow01 		/* Advance cur_pa to first granule in next L0 region. */
628f19dc624Sjohpow01 		cur_pa = gpt_get_l1_end_pa(cur_pa, end_pa);
629f19dc624Sjohpow01 	}
630f19dc624Sjohpow01 }
631f19dc624Sjohpow01 
632f19dc624Sjohpow01 /*
633f19dc624Sjohpow01  * This function flushes a range of L0 descriptors used by a given PAS region
634f19dc624Sjohpow01  * array. There is a chance that some unmodified L0 descriptors would be flushed
635f19dc624Sjohpow01  * in the case that there are "holes" in an array of PAS regions but overall
636f19dc624Sjohpow01  * this should be faster than individually flushing each modified L0 descriptor
637f19dc624Sjohpow01  * as they are created.
638f19dc624Sjohpow01  *
639f19dc624Sjohpow01  * Parameters
640f19dc624Sjohpow01  *   *pas		Pointer to an array of PAS regions.
641f19dc624Sjohpow01  *   pas_count		Number of entries in the PAS array.
642f19dc624Sjohpow01  */
643f19dc624Sjohpow01 static void flush_l0_for_pas_array(pas_region_t *pas, unsigned int pas_count)
644f19dc624Sjohpow01 {
645f19dc624Sjohpow01 	unsigned int idx;
646f19dc624Sjohpow01 	unsigned int start_idx;
647f19dc624Sjohpow01 	unsigned int end_idx;
648f19dc624Sjohpow01 	uint64_t *l0 = (uint64_t *)gpt_config.plat_gpt_l0_base;
649f19dc624Sjohpow01 
650f19dc624Sjohpow01 	assert(pas != NULL);
651f19dc624Sjohpow01 	assert(pas_count > 0);
652f19dc624Sjohpow01 
653f19dc624Sjohpow01 	/* Initial start and end values. */
654f19dc624Sjohpow01 	start_idx = GPT_L0_IDX(pas[0].base_pa);
655f19dc624Sjohpow01 	end_idx = GPT_L0_IDX(pas[0].base_pa + pas[0].size - 1);
656f19dc624Sjohpow01 
657f19dc624Sjohpow01 	/* Find lowest and highest L0 indices used in this PAS array. */
658f19dc624Sjohpow01 	for (idx = 1; idx < pas_count; idx++) {
659f19dc624Sjohpow01 		if (GPT_L0_IDX(pas[idx].base_pa) < start_idx) {
660f19dc624Sjohpow01 			start_idx = GPT_L0_IDX(pas[idx].base_pa);
661f19dc624Sjohpow01 		}
662f19dc624Sjohpow01 		if (GPT_L0_IDX(pas[idx].base_pa + pas[idx].size - 1) > end_idx) {
663f19dc624Sjohpow01 			end_idx = GPT_L0_IDX(pas[idx].base_pa + pas[idx].size - 1);
664f19dc624Sjohpow01 		}
665f19dc624Sjohpow01 	}
666f19dc624Sjohpow01 
667f19dc624Sjohpow01 	/*
668f19dc624Sjohpow01 	 * Flush all covered L0 descriptors, add 1 because we need to include
669f19dc624Sjohpow01 	 * the end index value.
670f19dc624Sjohpow01 	 */
671f19dc624Sjohpow01 	flush_dcache_range((uintptr_t)&l0[start_idx],
672f19dc624Sjohpow01 			   ((end_idx + 1) - start_idx) * sizeof(uint64_t));
673f19dc624Sjohpow01 }
674f19dc624Sjohpow01 
675f19dc624Sjohpow01 /*
676f19dc624Sjohpow01  * Public API to enable granule protection checks once the tables have all been
677f19dc624Sjohpow01  * initialized. This function is called at first initialization and then again
678f19dc624Sjohpow01  * later during warm boots of CPU cores.
679f19dc624Sjohpow01  *
680f19dc624Sjohpow01  * Return
681f19dc624Sjohpow01  *   Negative Linux error code in the event of a failure, 0 for success.
682f19dc624Sjohpow01  */
683f19dc624Sjohpow01 int gpt_enable(void)
684f19dc624Sjohpow01 {
685f19dc624Sjohpow01 	u_register_t gpccr_el3;
686f19dc624Sjohpow01 
687f19dc624Sjohpow01 	/*
688f19dc624Sjohpow01 	 * Granule tables must be initialised before enabling
689f19dc624Sjohpow01 	 * granule protection.
690f19dc624Sjohpow01 	 */
691f19dc624Sjohpow01 	if (gpt_config.plat_gpt_l0_base == 0U) {
692f19dc624Sjohpow01 		ERROR("[GPT] Tables have not been initialized!\n");
693f19dc624Sjohpow01 		return -EPERM;
694f19dc624Sjohpow01 	}
695f19dc624Sjohpow01 
696f19dc624Sjohpow01 	/* Invalidate any stale TLB entries */
697f19dc624Sjohpow01 	tlbipaallos();
698f19dc624Sjohpow01 	dsb();
699f19dc624Sjohpow01 
700f19dc624Sjohpow01 	/* Write the base address of the L0 tables into GPTBR */
701f19dc624Sjohpow01 	write_gptbr_el3(((gpt_config.plat_gpt_l0_base >> GPTBR_BADDR_VAL_SHIFT)
702f19dc624Sjohpow01 			>> GPTBR_BADDR_SHIFT) & GPTBR_BADDR_MASK);
703f19dc624Sjohpow01 
704f19dc624Sjohpow01 	/* GPCCR_EL3.PPS */
705f19dc624Sjohpow01 	gpccr_el3 = SET_GPCCR_PPS(gpt_config.pps);
706f19dc624Sjohpow01 
707f19dc624Sjohpow01 	/* GPCCR_EL3.PGS */
708f19dc624Sjohpow01 	gpccr_el3 |= SET_GPCCR_PGS(gpt_config.pgs);
709f19dc624Sjohpow01 
71077612b90SSoby Mathew 	/*
71177612b90SSoby Mathew 	 * Since EL3 maps the L1 region as Inner shareable, use the same
71277612b90SSoby Mathew 	 * shareability attribute for GPC as well so that
71377612b90SSoby Mathew 	 * GPC fetches are visible to PEs
71477612b90SSoby Mathew 	 */
71577612b90SSoby Mathew 	gpccr_el3 |= SET_GPCCR_SH(GPCCR_SH_IS);
716f19dc624Sjohpow01 
717f19dc624Sjohpow01 	/* Outer and Inner cacheability set to Normal memory, WB, RA, WA. */
718f19dc624Sjohpow01 	gpccr_el3 |= SET_GPCCR_ORGN(GPCCR_ORGN_WB_RA_WA);
719f19dc624Sjohpow01 	gpccr_el3 |= SET_GPCCR_IRGN(GPCCR_IRGN_WB_RA_WA);
720f19dc624Sjohpow01 
721f19dc624Sjohpow01 	/* Enable GPT */
722f19dc624Sjohpow01 	gpccr_el3 |= GPCCR_GPC_BIT;
723f19dc624Sjohpow01 
724f19dc624Sjohpow01 	/* TODO: Configure GPCCR_EL3_GPCP for Fault control. */
725f19dc624Sjohpow01 	write_gpccr_el3(gpccr_el3);
72677612b90SSoby Mathew 	isb();
727f19dc624Sjohpow01 	tlbipaallos();
728f19dc624Sjohpow01 	dsb();
729f19dc624Sjohpow01 	isb();
730f19dc624Sjohpow01 
731f19dc624Sjohpow01 	return 0;
732f19dc624Sjohpow01 }
733f19dc624Sjohpow01 
734f19dc624Sjohpow01 /*
735f19dc624Sjohpow01  * Public API to disable granule protection checks.
736f19dc624Sjohpow01  */
737f19dc624Sjohpow01 void gpt_disable(void)
738f19dc624Sjohpow01 {
739f19dc624Sjohpow01 	u_register_t gpccr_el3 = read_gpccr_el3();
740f19dc624Sjohpow01 
741f19dc624Sjohpow01 	write_gpccr_el3(gpccr_el3 & ~GPCCR_GPC_BIT);
742f19dc624Sjohpow01 	dsbsy();
743f19dc624Sjohpow01 	isb();
744f19dc624Sjohpow01 }
745f19dc624Sjohpow01 
746f19dc624Sjohpow01 /*
747f19dc624Sjohpow01  * Public API that initializes the entire protected space to GPT_GPI_ANY using
748f19dc624Sjohpow01  * the L0 tables (block descriptors). Ideally, this function is invoked prior
749f19dc624Sjohpow01  * to DDR discovery and initialization. The MMU must be initialized before
750f19dc624Sjohpow01  * calling this function.
751f19dc624Sjohpow01  *
752f19dc624Sjohpow01  * Parameters
753f19dc624Sjohpow01  *   pps		PPS value to use for table generation
754f19dc624Sjohpow01  *   l0_mem_base	Base address of L0 tables in memory.
755f19dc624Sjohpow01  *   l0_mem_size	Total size of memory available for L0 tables.
756f19dc624Sjohpow01  *
757f19dc624Sjohpow01  * Return
758f19dc624Sjohpow01  *   Negative Linux error code in the event of a failure, 0 for success.
759f19dc624Sjohpow01  */
760f19dc624Sjohpow01 int gpt_init_l0_tables(unsigned int pps, uintptr_t l0_mem_base,
761f19dc624Sjohpow01 		       size_t l0_mem_size)
762f19dc624Sjohpow01 {
763f19dc624Sjohpow01 	int ret;
764f19dc624Sjohpow01 	uint64_t gpt_desc;
765f19dc624Sjohpow01 
76677612b90SSoby Mathew 	/* Ensure that MMU and Data caches are enabled. */
767f19dc624Sjohpow01 	assert((read_sctlr_el3() & SCTLR_C_BIT) != 0U);
768f19dc624Sjohpow01 
769f19dc624Sjohpow01 	/* Validate other parameters. */
770f19dc624Sjohpow01 	ret = gpt_validate_l0_params(pps, l0_mem_base, l0_mem_size);
7716a00e9b0SRobert Wakim 	if (ret != 0) {
772f19dc624Sjohpow01 		return ret;
773f19dc624Sjohpow01 	}
774f19dc624Sjohpow01 
775f19dc624Sjohpow01 	/* Create the descriptor to initialize L0 entries with. */
776f19dc624Sjohpow01 	gpt_desc = GPT_L0_BLK_DESC(GPT_GPI_ANY);
777f19dc624Sjohpow01 
778f19dc624Sjohpow01 	/* Iterate through all L0 entries */
779f19dc624Sjohpow01 	for (unsigned int i = 0U; i < GPT_L0_REGION_COUNT(gpt_config.t); i++) {
780f19dc624Sjohpow01 		((uint64_t *)l0_mem_base)[i] = gpt_desc;
781f19dc624Sjohpow01 	}
782f19dc624Sjohpow01 
783f19dc624Sjohpow01 	/* Flush updated L0 tables to memory. */
784f19dc624Sjohpow01 	flush_dcache_range((uintptr_t)l0_mem_base,
785f19dc624Sjohpow01 			   (size_t)GPT_L0_TABLE_SIZE(gpt_config.t));
786f19dc624Sjohpow01 
787f19dc624Sjohpow01 	/* Stash the L0 base address once initial setup is complete. */
788f19dc624Sjohpow01 	gpt_config.plat_gpt_l0_base = l0_mem_base;
789f19dc624Sjohpow01 
790f19dc624Sjohpow01 	return 0;
791f19dc624Sjohpow01 }
792f19dc624Sjohpow01 
793f19dc624Sjohpow01 /*
794f19dc624Sjohpow01  * Public API that carves out PAS regions from the L0 tables and builds any L1
795f19dc624Sjohpow01  * tables that are needed. This function ideally is run after DDR discovery and
796f19dc624Sjohpow01  * initialization. The L0 tables must have already been initialized to GPI_ANY
797f19dc624Sjohpow01  * when this function is called.
798f19dc624Sjohpow01  *
799f19dc624Sjohpow01  * This function can be called multiple times with different L1 memory ranges
800f19dc624Sjohpow01  * and PAS regions if it is desirable to place L1 tables in different locations
801f19dc624Sjohpow01  * in memory. (ex: you have multiple DDR banks and want to place the L1 tables
802f19dc624Sjohpow01  * in the DDR bank that they control)
803f19dc624Sjohpow01  *
804f19dc624Sjohpow01  * Parameters
805f19dc624Sjohpow01  *   pgs		PGS value to use for table generation.
806f19dc624Sjohpow01  *   l1_mem_base	Base address of memory used for L1 tables.
807f19dc624Sjohpow01  *   l1_mem_size	Total size of memory available for L1 tables.
808f19dc624Sjohpow01  *   *pas_regions	Pointer to PAS regions structure array.
809f19dc624Sjohpow01  *   pas_count		Total number of PAS regions.
810f19dc624Sjohpow01  *
811f19dc624Sjohpow01  * Return
812f19dc624Sjohpow01  *   Negative Linux error code in the event of a failure, 0 for success.
813f19dc624Sjohpow01  */
814f19dc624Sjohpow01 int gpt_init_pas_l1_tables(gpccr_pgs_e pgs, uintptr_t l1_mem_base,
815f19dc624Sjohpow01 			   size_t l1_mem_size, pas_region_t *pas_regions,
816f19dc624Sjohpow01 			   unsigned int pas_count)
817f19dc624Sjohpow01 {
818f19dc624Sjohpow01 	int ret;
819f19dc624Sjohpow01 	int l1_gpt_cnt;
820f19dc624Sjohpow01 
82177612b90SSoby Mathew 	/* Ensure that MMU and Data caches are enabled. */
822f19dc624Sjohpow01 	assert((read_sctlr_el3() & SCTLR_C_BIT) != 0U);
823f19dc624Sjohpow01 
824f19dc624Sjohpow01 	/* PGS is needed for gpt_validate_pas_mappings so check it now. */
825f19dc624Sjohpow01 	if (pgs > GPT_PGS_MAX) {
826f19dc624Sjohpow01 		ERROR("[GPT] Invalid PGS: 0x%x\n", pgs);
827f19dc624Sjohpow01 		return -EINVAL;
828f19dc624Sjohpow01 	}
829f19dc624Sjohpow01 	gpt_config.pgs = pgs;
830f19dc624Sjohpow01 	gpt_config.p = gpt_p_lookup[pgs];
831f19dc624Sjohpow01 
832f19dc624Sjohpow01 	/* Make sure L0 tables have been initialized. */
833f19dc624Sjohpow01 	if (gpt_config.plat_gpt_l0_base == 0U) {
834f19dc624Sjohpow01 		ERROR("[GPT] L0 tables must be initialized first!\n");
835f19dc624Sjohpow01 		return -EPERM;
836f19dc624Sjohpow01 	}
837f19dc624Sjohpow01 
838f19dc624Sjohpow01 	/* Check if L1 GPTs are required and how many. */
839f19dc624Sjohpow01 	l1_gpt_cnt = gpt_validate_pas_mappings(pas_regions, pas_count);
840f19dc624Sjohpow01 	if (l1_gpt_cnt < 0) {
841f19dc624Sjohpow01 		return l1_gpt_cnt;
842f19dc624Sjohpow01 	}
843f19dc624Sjohpow01 
844f19dc624Sjohpow01 	VERBOSE("[GPT] %u L1 GPTs requested.\n", l1_gpt_cnt);
845f19dc624Sjohpow01 
846f19dc624Sjohpow01 	/* If L1 tables are needed then validate the L1 parameters. */
847f19dc624Sjohpow01 	if (l1_gpt_cnt > 0) {
848f19dc624Sjohpow01 		ret = gpt_validate_l1_params(l1_mem_base, l1_mem_size,
849f19dc624Sjohpow01 		      l1_gpt_cnt);
8506a00e9b0SRobert Wakim 		if (ret != 0) {
851f19dc624Sjohpow01 			return ret;
852f19dc624Sjohpow01 		}
853f19dc624Sjohpow01 
854f19dc624Sjohpow01 		/* Set up parameters for L1 table generation. */
855f19dc624Sjohpow01 		gpt_l1_tbl = l1_mem_base;
856f19dc624Sjohpow01 		gpt_next_l1_tbl_idx = 0U;
857f19dc624Sjohpow01 	}
858f19dc624Sjohpow01 
859f19dc624Sjohpow01 	INFO("[GPT] Boot Configuration\n");
860f19dc624Sjohpow01 	INFO("  PPS/T:     0x%x/%u\n", gpt_config.pps, gpt_config.t);
861f19dc624Sjohpow01 	INFO("  PGS/P:     0x%x/%u\n", gpt_config.pgs, gpt_config.p);
862f19dc624Sjohpow01 	INFO("  L0GPTSZ/S: 0x%x/%u\n", GPT_L0GPTSZ, GPT_S_VAL);
863f19dc624Sjohpow01 	INFO("  PAS count: 0x%x\n", pas_count);
864f19dc624Sjohpow01 	INFO("  L0 base:   0x%lx\n", gpt_config.plat_gpt_l0_base);
865f19dc624Sjohpow01 
866f19dc624Sjohpow01 	/* Generate the tables in memory. */
867f19dc624Sjohpow01 	for (unsigned int idx = 0U; idx < pas_count; idx++) {
868f19dc624Sjohpow01 		INFO("[GPT] PAS[%u]: base 0x%lx, size 0x%lx, GPI 0x%x, type 0x%x\n",
869f19dc624Sjohpow01 		     idx, pas_regions[idx].base_pa, pas_regions[idx].size,
870f19dc624Sjohpow01 		     GPT_PAS_ATTR_GPI(pas_regions[idx].attrs),
871f19dc624Sjohpow01 		     GPT_PAS_ATTR_MAP_TYPE(pas_regions[idx].attrs));
872f19dc624Sjohpow01 
873f19dc624Sjohpow01 		/* Check if a block or table descriptor is required */
874f19dc624Sjohpow01 		if (GPT_PAS_ATTR_MAP_TYPE(pas_regions[idx].attrs) ==
875f19dc624Sjohpow01 		    GPT_PAS_ATTR_MAP_TYPE_BLOCK) {
876f19dc624Sjohpow01 			gpt_generate_l0_blk_desc(&pas_regions[idx]);
877f19dc624Sjohpow01 
878f19dc624Sjohpow01 		} else {
879f19dc624Sjohpow01 			gpt_generate_l0_tbl_desc(&pas_regions[idx]);
880f19dc624Sjohpow01 		}
881f19dc624Sjohpow01 	}
882f19dc624Sjohpow01 
883f19dc624Sjohpow01 	/* Flush modified L0 tables. */
884f19dc624Sjohpow01 	flush_l0_for_pas_array(pas_regions, pas_count);
885f19dc624Sjohpow01 
886f19dc624Sjohpow01 	/* Flush L1 tables if needed. */
887f19dc624Sjohpow01 	if (l1_gpt_cnt > 0) {
888f19dc624Sjohpow01 		flush_dcache_range(l1_mem_base,
889f19dc624Sjohpow01 				   GPT_L1_TABLE_SIZE(gpt_config.p) *
890f19dc624Sjohpow01 				   l1_gpt_cnt);
891f19dc624Sjohpow01 	}
892f19dc624Sjohpow01 
893f19dc624Sjohpow01 	/* Make sure that all the entries are written to the memory. */
894f19dc624Sjohpow01 	dsbishst();
89577612b90SSoby Mathew 	tlbipaallos();
89677612b90SSoby Mathew 	dsb();
89777612b90SSoby Mathew 	isb();
898f19dc624Sjohpow01 
899f19dc624Sjohpow01 	return 0;
900f19dc624Sjohpow01 }
901f19dc624Sjohpow01 
902f19dc624Sjohpow01 /*
903f19dc624Sjohpow01  * Public API to initialize the runtime gpt_config structure based on the values
904f19dc624Sjohpow01  * present in the GPTBR_EL3 and GPCCR_EL3 registers. GPT initialization
905f19dc624Sjohpow01  * typically happens in a bootloader stage prior to setting up the EL3 runtime
906f19dc624Sjohpow01  * environment for the granule transition service so this function detects the
907f19dc624Sjohpow01  * initialization from a previous stage. Granule protection checks must be
908f19dc624Sjohpow01  * enabled already or this function will return an error.
909f19dc624Sjohpow01  *
910f19dc624Sjohpow01  * Return
911f19dc624Sjohpow01  *   Negative Linux error code in the event of a failure, 0 for success.
912f19dc624Sjohpow01  */
913f19dc624Sjohpow01 int gpt_runtime_init(void)
914f19dc624Sjohpow01 {
915f19dc624Sjohpow01 	u_register_t reg;
916f19dc624Sjohpow01 
91777612b90SSoby Mathew 	/* Ensure that MMU and Data caches are enabled. */
918f19dc624Sjohpow01 	assert((read_sctlr_el3() & SCTLR_C_BIT) != 0U);
919f19dc624Sjohpow01 
920f19dc624Sjohpow01 	/* Ensure GPC are already enabled. */
921f19dc624Sjohpow01 	if ((read_gpccr_el3() & GPCCR_GPC_BIT) == 0U) {
922f19dc624Sjohpow01 		ERROR("[GPT] Granule protection checks are not enabled!\n");
923f19dc624Sjohpow01 		return -EPERM;
924f19dc624Sjohpow01 	}
925f19dc624Sjohpow01 
926f19dc624Sjohpow01 	/*
927f19dc624Sjohpow01 	 * Read the L0 table address from GPTBR, we don't need the L1 base
928f19dc624Sjohpow01 	 * address since those are included in the L0 tables as needed.
929f19dc624Sjohpow01 	 */
930f19dc624Sjohpow01 	reg = read_gptbr_el3();
931f19dc624Sjohpow01 	gpt_config.plat_gpt_l0_base = ((reg >> GPTBR_BADDR_SHIFT) &
932f19dc624Sjohpow01 				      GPTBR_BADDR_MASK) <<
933f19dc624Sjohpow01 				      GPTBR_BADDR_VAL_SHIFT;
934f19dc624Sjohpow01 
935f19dc624Sjohpow01 	/* Read GPCCR to get PGS and PPS values. */
936f19dc624Sjohpow01 	reg = read_gpccr_el3();
937f19dc624Sjohpow01 	gpt_config.pps = (reg >> GPCCR_PPS_SHIFT) & GPCCR_PPS_MASK;
938f19dc624Sjohpow01 	gpt_config.t = gpt_t_lookup[gpt_config.pps];
939f19dc624Sjohpow01 	gpt_config.pgs = (reg >> GPCCR_PGS_SHIFT) & GPCCR_PGS_MASK;
940f19dc624Sjohpow01 	gpt_config.p = gpt_p_lookup[gpt_config.pgs];
941f19dc624Sjohpow01 
942f19dc624Sjohpow01 	VERBOSE("[GPT] Runtime Configuration\n");
943f19dc624Sjohpow01 	VERBOSE("  PPS/T:     0x%x/%u\n", gpt_config.pps, gpt_config.t);
944f19dc624Sjohpow01 	VERBOSE("  PGS/P:     0x%x/%u\n", gpt_config.pgs, gpt_config.p);
945f19dc624Sjohpow01 	VERBOSE("  L0GPTSZ/S: 0x%x/%u\n", GPT_L0GPTSZ, GPT_S_VAL);
946f19dc624Sjohpow01 	VERBOSE("  L0 base:   0x%lx\n", gpt_config.plat_gpt_l0_base);
947f19dc624Sjohpow01 
948f19dc624Sjohpow01 	return 0;
949f19dc624Sjohpow01 }
950f19dc624Sjohpow01 
951f19dc624Sjohpow01 /*
952f19dc624Sjohpow01  * The L1 descriptors are protected by a spinlock to ensure that multiple
953f19dc624Sjohpow01  * CPUs do not attempt to change the descriptors at once. In the future it
954f19dc624Sjohpow01  * would be better to have separate spinlocks for each L1 descriptor.
955f19dc624Sjohpow01  */
956f19dc624Sjohpow01 static spinlock_t gpt_lock;
957f19dc624Sjohpow01 
958f19dc624Sjohpow01 /*
9596a00e9b0SRobert Wakim  * A helper to write the value (target_pas << gpi_shift) to the index of
9606a00e9b0SRobert Wakim  * the gpt_l1_addr
9616a00e9b0SRobert Wakim  */
9626a00e9b0SRobert Wakim static inline void write_gpt(uint64_t *gpt_l1_desc, uint64_t *gpt_l1_addr,
9636a00e9b0SRobert Wakim 			     unsigned int gpi_shift, unsigned int idx,
9646a00e9b0SRobert Wakim 			     unsigned int target_pas)
9656a00e9b0SRobert Wakim {
9666a00e9b0SRobert Wakim 	*gpt_l1_desc &= ~(GPT_L1_GRAN_DESC_GPI_MASK << gpi_shift);
9676a00e9b0SRobert Wakim 	*gpt_l1_desc |= ((uint64_t)target_pas << gpi_shift);
9686a00e9b0SRobert Wakim 	gpt_l1_addr[idx] = *gpt_l1_desc;
9696a00e9b0SRobert Wakim }
9706a00e9b0SRobert Wakim 
9716a00e9b0SRobert Wakim /*
9726a00e9b0SRobert Wakim  * Helper to retrieve the gpt_l1_* information from the base address
9736a00e9b0SRobert Wakim  * returned in gpi_info
9746a00e9b0SRobert Wakim  */
9756a00e9b0SRobert Wakim static int get_gpi_params(uint64_t base, gpi_info_t *gpi_info)
9766a00e9b0SRobert Wakim {
9776a00e9b0SRobert Wakim 	uint64_t gpt_l0_desc, *gpt_l0_base;
9786a00e9b0SRobert Wakim 
9796a00e9b0SRobert Wakim 	gpt_l0_base = (uint64_t *)gpt_config.plat_gpt_l0_base;
9806a00e9b0SRobert Wakim 	gpt_l0_desc = gpt_l0_base[GPT_L0_IDX(base)];
9816a00e9b0SRobert Wakim 	if (GPT_L0_TYPE(gpt_l0_desc) != GPT_L0_TYPE_TBL_DESC) {
9826a00e9b0SRobert Wakim 		VERBOSE("[GPT] Granule is not covered by a table descriptor!\n");
9836a00e9b0SRobert Wakim 		VERBOSE("      Base=0x%" PRIx64 "\n", base);
9846a00e9b0SRobert Wakim 		return -EINVAL;
9856a00e9b0SRobert Wakim 	}
9866a00e9b0SRobert Wakim 
9876a00e9b0SRobert Wakim 	/* Get the table index and GPI shift from PA. */
9886a00e9b0SRobert Wakim 	gpi_info->gpt_l1_addr = GPT_L0_TBLD_ADDR(gpt_l0_desc);
9896a00e9b0SRobert Wakim 	gpi_info->idx = GPT_L1_IDX(gpt_config.p, base);
9906a00e9b0SRobert Wakim 	gpi_info->gpi_shift = GPT_L1_GPI_IDX(gpt_config.p, base) << 2;
9916a00e9b0SRobert Wakim 
9926a00e9b0SRobert Wakim 	gpi_info->gpt_l1_desc = (gpi_info->gpt_l1_addr)[gpi_info->idx];
9936a00e9b0SRobert Wakim 	gpi_info->gpi = (gpi_info->gpt_l1_desc >> gpi_info->gpi_shift) &
9946a00e9b0SRobert Wakim 		GPT_L1_GRAN_DESC_GPI_MASK;
9956a00e9b0SRobert Wakim 	return 0;
9966a00e9b0SRobert Wakim }
9976a00e9b0SRobert Wakim 
9986a00e9b0SRobert Wakim /*
9996a00e9b0SRobert Wakim  * This function is the granule transition delegate service. When a granule
10006a00e9b0SRobert Wakim  * transition request occurs it is routed to this function to have the request,
10016a00e9b0SRobert Wakim  * if valid, fulfilled following A1.1.1 Delegate of RME supplement
1002f19dc624Sjohpow01  *
10036a00e9b0SRobert Wakim  * TODO: implement support for transitioning multiple granules at once.
1004f19dc624Sjohpow01  *
1005f19dc624Sjohpow01  * Parameters
10066a00e9b0SRobert Wakim  *   base		Base address of the region to transition, must be
10076a00e9b0SRobert Wakim  *			aligned to granule size.
10086a00e9b0SRobert Wakim  *   size		Size of region to transition, must be aligned to granule
10096a00e9b0SRobert Wakim  *			size.
1010f19dc624Sjohpow01  *   src_sec_state	Security state of the caller.
1011f19dc624Sjohpow01  *
1012f19dc624Sjohpow01  * Return
1013f19dc624Sjohpow01  *   Negative Linux error code in the event of a failure, 0 for success.
1014f19dc624Sjohpow01  */
10156a00e9b0SRobert Wakim int gpt_delegate_pas(uint64_t base, size_t size, unsigned int src_sec_state)
1016f19dc624Sjohpow01 {
10176a00e9b0SRobert Wakim 	gpi_info_t gpi_info;
10186a00e9b0SRobert Wakim 	uint64_t nse;
10196a00e9b0SRobert Wakim 	int res;
10206a00e9b0SRobert Wakim 	unsigned int target_pas;
1021f19dc624Sjohpow01 
10226a00e9b0SRobert Wakim 	/* Ensure that the tables have been set up before taking requests. */
10236a00e9b0SRobert Wakim 	assert(gpt_config.plat_gpt_l0_base != 0UL);
10246a00e9b0SRobert Wakim 
10256a00e9b0SRobert Wakim 	/* Ensure that caches are enabled. */
10266a00e9b0SRobert Wakim 	assert((read_sctlr_el3() & SCTLR_C_BIT) != 0UL);
10276a00e9b0SRobert Wakim 
10286a00e9b0SRobert Wakim 	/* Delegate request can only come from REALM or SECURE */
10296a00e9b0SRobert Wakim 	assert(src_sec_state == SMC_FROM_REALM ||
10306a00e9b0SRobert Wakim 	       src_sec_state == SMC_FROM_SECURE);
10316a00e9b0SRobert Wakim 
10326a00e9b0SRobert Wakim 	/* See if this is a single or a range of granule transition. */
10336a00e9b0SRobert Wakim 	if (size != GPT_PGS_ACTUAL_SIZE(gpt_config.p)) {
1034f19dc624Sjohpow01 		return -EINVAL;
1035f19dc624Sjohpow01 	}
1036f19dc624Sjohpow01 
10376a00e9b0SRobert Wakim 	/* Check that base and size are valid */
10386a00e9b0SRobert Wakim 	if ((ULONG_MAX - base) < size) {
10396a00e9b0SRobert Wakim 		VERBOSE("[GPT] Transition request address overflow!\n");
10406a00e9b0SRobert Wakim 		VERBOSE("      Base=0x%" PRIx64 "\n", base);
10416a00e9b0SRobert Wakim 		VERBOSE("      Size=0x%lx\n", size);
10426a00e9b0SRobert Wakim 		return -EINVAL;
10436a00e9b0SRobert Wakim 	}
10446a00e9b0SRobert Wakim 
10456a00e9b0SRobert Wakim 	/* Make sure base and size are valid. */
10466a00e9b0SRobert Wakim 	if (((base & (GPT_PGS_ACTUAL_SIZE(gpt_config.p) - 1)) != 0UL) ||
10476a00e9b0SRobert Wakim 	    ((size & (GPT_PGS_ACTUAL_SIZE(gpt_config.p) - 1)) != 0UL) ||
10486a00e9b0SRobert Wakim 	    (size == 0UL) ||
10496a00e9b0SRobert Wakim 	    ((base + size) >= GPT_PPS_ACTUAL_SIZE(gpt_config.t))) {
10506a00e9b0SRobert Wakim 		VERBOSE("[GPT] Invalid granule transition address range!\n");
10516a00e9b0SRobert Wakim 		VERBOSE("      Base=0x%" PRIx64 "\n", base);
10526a00e9b0SRobert Wakim 		VERBOSE("      Size=0x%lx\n", size);
10536a00e9b0SRobert Wakim 		return -EINVAL;
10546a00e9b0SRobert Wakim 	}
10556a00e9b0SRobert Wakim 
10566a00e9b0SRobert Wakim 	target_pas = GPT_GPI_REALM;
10576a00e9b0SRobert Wakim 	if (src_sec_state == SMC_FROM_SECURE) {
10586a00e9b0SRobert Wakim 		target_pas = GPT_GPI_SECURE;
10596a00e9b0SRobert Wakim 	}
10606a00e9b0SRobert Wakim 
10616a00e9b0SRobert Wakim 	/*
10626a00e9b0SRobert Wakim 	 * Access to L1 tables is controlled by a global lock to ensure
10636a00e9b0SRobert Wakim 	 * that no more than one CPU is allowed to make changes at any
10646a00e9b0SRobert Wakim 	 * given time.
10656a00e9b0SRobert Wakim 	 */
10666a00e9b0SRobert Wakim 	spin_lock(&gpt_lock);
10676a00e9b0SRobert Wakim 	res = get_gpi_params(base, &gpi_info);
10686a00e9b0SRobert Wakim 	if (res != 0) {
10696a00e9b0SRobert Wakim 		spin_unlock(&gpt_lock);
10706a00e9b0SRobert Wakim 		return res;
10716a00e9b0SRobert Wakim 	}
10726a00e9b0SRobert Wakim 
10736a00e9b0SRobert Wakim 	/* Check that the current address is in NS state */
10746a00e9b0SRobert Wakim 	if (gpi_info.gpi != GPT_GPI_NS) {
10756a00e9b0SRobert Wakim 		VERBOSE("[GPT] Only Granule in NS state can be delegated.\n");
10766a00e9b0SRobert Wakim 		VERBOSE("      Caller: %u, Current GPI: %u\n", src_sec_state,
10776a00e9b0SRobert Wakim 			gpi_info.gpi);
10786a00e9b0SRobert Wakim 		spin_unlock(&gpt_lock);
1079*e50fedbcSJavier Almansa Sobrino 		return -EPERM;
10806a00e9b0SRobert Wakim 	}
10816a00e9b0SRobert Wakim 
10826a00e9b0SRobert Wakim 	if (src_sec_state == SMC_FROM_SECURE) {
10836a00e9b0SRobert Wakim 		nse = (uint64_t)GPT_NSE_SECURE << GPT_NSE_SHIFT;
1084f19dc624Sjohpow01 	} else {
10856a00e9b0SRobert Wakim 		nse = (uint64_t)GPT_NSE_REALM << GPT_NSE_SHIFT;
1086f19dc624Sjohpow01 	}
1087f19dc624Sjohpow01 
10886a00e9b0SRobert Wakim 	/*
10896a00e9b0SRobert Wakim 	 * In order to maintain mutual distrust between Realm and Secure
10906a00e9b0SRobert Wakim 	 * states, remove any data speculatively fetched into the target
10916a00e9b0SRobert Wakim 	 * physical address space. Issue DC CIPAPA over address range
10926a00e9b0SRobert Wakim 	 */
10936a00e9b0SRobert Wakim 	flush_dcache_to_popa_range(nse | base,
10946a00e9b0SRobert Wakim 				   GPT_PGS_ACTUAL_SIZE(gpt_config.p));
10956a00e9b0SRobert Wakim 
10966a00e9b0SRobert Wakim 	write_gpt(&gpi_info.gpt_l1_desc, gpi_info.gpt_l1_addr,
10976a00e9b0SRobert Wakim 		  gpi_info.gpi_shift, gpi_info.idx, target_pas);
10986a00e9b0SRobert Wakim 	dsboshst();
10996a00e9b0SRobert Wakim 
11006a00e9b0SRobert Wakim 	gpt_tlbi_by_pa_ll(base, GPT_PGS_ACTUAL_SIZE(gpt_config.p));
11016a00e9b0SRobert Wakim 	dsbosh();
11026a00e9b0SRobert Wakim 
11036a00e9b0SRobert Wakim 	nse = (uint64_t)GPT_NSE_NS << GPT_NSE_SHIFT;
11046a00e9b0SRobert Wakim 
11056a00e9b0SRobert Wakim 	flush_dcache_to_popa_range(nse | base,
11066a00e9b0SRobert Wakim 				   GPT_PGS_ACTUAL_SIZE(gpt_config.p));
11076a00e9b0SRobert Wakim 
11086a00e9b0SRobert Wakim 	/* Unlock access to the L1 tables. */
11096a00e9b0SRobert Wakim 	spin_unlock(&gpt_lock);
11106a00e9b0SRobert Wakim 
11116a00e9b0SRobert Wakim 	/*
11126a00e9b0SRobert Wakim 	 * The isb() will be done as part of context
11136a00e9b0SRobert Wakim 	 * synchronization when returning to lower EL
11146a00e9b0SRobert Wakim 	 */
11156a00e9b0SRobert Wakim 	VERBOSE("[GPT] Granule 0x%" PRIx64 ", GPI 0x%x->0x%x\n",
11166a00e9b0SRobert Wakim 		base, gpi_info.gpi, target_pas);
1117f19dc624Sjohpow01 
1118f19dc624Sjohpow01 	return 0;
1119f19dc624Sjohpow01 }
1120f19dc624Sjohpow01 
1121f19dc624Sjohpow01 /*
11226a00e9b0SRobert Wakim  * This function is the granule transition undelegate service. When a granule
1123f19dc624Sjohpow01  * transition request occurs it is routed to this function where the request is
1124f19dc624Sjohpow01  * validated then fulfilled if possible.
1125f19dc624Sjohpow01  *
1126f19dc624Sjohpow01  * TODO: implement support for transitioning multiple granules at once.
1127f19dc624Sjohpow01  *
1128f19dc624Sjohpow01  * Parameters
1129f19dc624Sjohpow01  *   base		Base address of the region to transition, must be
1130f19dc624Sjohpow01  *			aligned to granule size.
1131f19dc624Sjohpow01  *   size		Size of region to transition, must be aligned to granule
1132f19dc624Sjohpow01  *			size.
1133f19dc624Sjohpow01  *   src_sec_state	Security state of the caller.
1134f19dc624Sjohpow01  *
1135f19dc624Sjohpow01  * Return
1136f19dc624Sjohpow01  *    Negative Linux error code in the event of a failure, 0 for success.
1137f19dc624Sjohpow01  */
11386a00e9b0SRobert Wakim int gpt_undelegate_pas(uint64_t base, size_t size, unsigned int src_sec_state)
1139f19dc624Sjohpow01 {
11406a00e9b0SRobert Wakim 	gpi_info_t gpi_info;
11416a00e9b0SRobert Wakim 	uint64_t nse;
11426a00e9b0SRobert Wakim 	int res;
1143f19dc624Sjohpow01 
1144f19dc624Sjohpow01 	/* Ensure that the tables have been set up before taking requests. */
11456a00e9b0SRobert Wakim 	assert(gpt_config.plat_gpt_l0_base != 0UL);
1146f19dc624Sjohpow01 
11476a00e9b0SRobert Wakim 	/* Ensure that MMU and caches are enabled. */
11486a00e9b0SRobert Wakim 	assert((read_sctlr_el3() & SCTLR_C_BIT) != 0UL);
114977612b90SSoby Mathew 
11506a00e9b0SRobert Wakim 	/* Delegate request can only come from REALM or SECURE */
11516a00e9b0SRobert Wakim 	assert(src_sec_state == SMC_FROM_REALM ||
11526a00e9b0SRobert Wakim 	       src_sec_state == SMC_FROM_SECURE);
11536a00e9b0SRobert Wakim 
11546a00e9b0SRobert Wakim 	/* See if this is a single or a range of granule transition. */
11556a00e9b0SRobert Wakim 	if (size != GPT_PGS_ACTUAL_SIZE(gpt_config.p)) {
11566a00e9b0SRobert Wakim 		return -EINVAL;
11576a00e9b0SRobert Wakim 	}
11586a00e9b0SRobert Wakim 
11596a00e9b0SRobert Wakim 	/* Check that base and size are valid */
1160f19dc624Sjohpow01 	if ((ULONG_MAX - base) < size) {
1161f19dc624Sjohpow01 		VERBOSE("[GPT] Transition request address overflow!\n");
11622461bd3aSManish Pandey 		VERBOSE("      Base=0x%" PRIx64 "\n", base);
1163f19dc624Sjohpow01 		VERBOSE("      Size=0x%lx\n", size);
1164f19dc624Sjohpow01 		return -EINVAL;
1165f19dc624Sjohpow01 	}
1166f19dc624Sjohpow01 
1167f19dc624Sjohpow01 	/* Make sure base and size are valid. */
11686a00e9b0SRobert Wakim 	if (((base & (GPT_PGS_ACTUAL_SIZE(gpt_config.p) - 1)) != 0UL) ||
11696a00e9b0SRobert Wakim 	    ((size & (GPT_PGS_ACTUAL_SIZE(gpt_config.p) - 1)) != 0UL) ||
11706a00e9b0SRobert Wakim 	    (size == 0UL) ||
1171f19dc624Sjohpow01 	    ((base + size) >= GPT_PPS_ACTUAL_SIZE(gpt_config.t))) {
1172f19dc624Sjohpow01 		VERBOSE("[GPT] Invalid granule transition address range!\n");
11732461bd3aSManish Pandey 		VERBOSE("      Base=0x%" PRIx64 "\n", base);
1174f19dc624Sjohpow01 		VERBOSE("      Size=0x%lx\n", size);
1175f19dc624Sjohpow01 		return -EINVAL;
1176f19dc624Sjohpow01 	}
1177f19dc624Sjohpow01 
1178f19dc624Sjohpow01 	/*
1179f19dc624Sjohpow01 	 * Access to L1 tables is controlled by a global lock to ensure
1180f19dc624Sjohpow01 	 * that no more than one CPU is allowed to make changes at any
1181f19dc624Sjohpow01 	 * given time.
1182f19dc624Sjohpow01 	 */
1183f19dc624Sjohpow01 	spin_lock(&gpt_lock);
1184f19dc624Sjohpow01 
11856a00e9b0SRobert Wakim 	res = get_gpi_params(base, &gpi_info);
11866a00e9b0SRobert Wakim 	if (res != 0) {
1187f19dc624Sjohpow01 		spin_unlock(&gpt_lock);
11886a00e9b0SRobert Wakim 		return res;
1189f19dc624Sjohpow01 	}
1190f19dc624Sjohpow01 
11916a00e9b0SRobert Wakim 	/* Check that the current address is in the delegated state */
11926a00e9b0SRobert Wakim 	if ((src_sec_state == SMC_FROM_REALM  &&
11936a00e9b0SRobert Wakim 	     gpi_info.gpi != GPT_GPI_REALM) ||
11946a00e9b0SRobert Wakim 	    (src_sec_state == SMC_FROM_SECURE &&
11956a00e9b0SRobert Wakim 	     gpi_info.gpi != GPT_GPI_SECURE)) {
11966a00e9b0SRobert Wakim 		VERBOSE("[GPT] Only Granule in REALM or SECURE state can be undelegated.\n");
11976a00e9b0SRobert Wakim 		VERBOSE("      Caller: %u, Current GPI: %u\n", src_sec_state,
11986a00e9b0SRobert Wakim 			gpi_info.gpi);
11996a00e9b0SRobert Wakim 		spin_unlock(&gpt_lock);
1200*e50fedbcSJavier Almansa Sobrino 		return -EPERM;
12016a00e9b0SRobert Wakim 	}
1202f19dc624Sjohpow01 
12036a00e9b0SRobert Wakim 
12046a00e9b0SRobert Wakim 	/* In order to maintain mutual distrust between Realm and Secure
12056a00e9b0SRobert Wakim 	 * states, remove access now, in order to guarantee that writes
12066a00e9b0SRobert Wakim 	 * to the currently-accessible physical address space will not
12076a00e9b0SRobert Wakim 	 * later become observable.
12086a00e9b0SRobert Wakim 	 */
12096a00e9b0SRobert Wakim 	write_gpt(&gpi_info.gpt_l1_desc, gpi_info.gpt_l1_addr,
12106a00e9b0SRobert Wakim 		  gpi_info.gpi_shift, gpi_info.idx, GPT_GPI_NO_ACCESS);
12116a00e9b0SRobert Wakim 	dsboshst();
12126a00e9b0SRobert Wakim 
12136a00e9b0SRobert Wakim 	gpt_tlbi_by_pa_ll(base, GPT_PGS_ACTUAL_SIZE(gpt_config.p));
12146a00e9b0SRobert Wakim 	dsbosh();
12156a00e9b0SRobert Wakim 
12166a00e9b0SRobert Wakim 	if (src_sec_state == SMC_FROM_SECURE) {
12176a00e9b0SRobert Wakim 		nse = (uint64_t)GPT_NSE_SECURE << GPT_NSE_SHIFT;
12186a00e9b0SRobert Wakim 	} else {
12196a00e9b0SRobert Wakim 		nse = (uint64_t)GPT_NSE_REALM << GPT_NSE_SHIFT;
12206a00e9b0SRobert Wakim 	}
12216a00e9b0SRobert Wakim 
12226a00e9b0SRobert Wakim 	/* Ensure that the scrubbed data has made it past the PoPA */
12236a00e9b0SRobert Wakim 	flush_dcache_to_popa_range(nse | base,
12246a00e9b0SRobert Wakim 				   GPT_PGS_ACTUAL_SIZE(gpt_config.p));
12256a00e9b0SRobert Wakim 
12266a00e9b0SRobert Wakim 	/*
12276a00e9b0SRobert Wakim 	 * Remove any data loaded speculatively
12286a00e9b0SRobert Wakim 	 * in NS space from before the scrubbing
12296a00e9b0SRobert Wakim 	 */
12306a00e9b0SRobert Wakim 	nse = (uint64_t)GPT_NSE_NS << GPT_NSE_SHIFT;
12316a00e9b0SRobert Wakim 
12326a00e9b0SRobert Wakim 	flush_dcache_to_popa_range(nse | base,
12336a00e9b0SRobert Wakim 				   GPT_PGS_ACTUAL_SIZE(gpt_config.p));
12346a00e9b0SRobert Wakim 
12356a00e9b0SRobert Wakim 	/* Clear existing GPI encoding and transition granule. */
12366a00e9b0SRobert Wakim 	write_gpt(&gpi_info.gpt_l1_desc, gpi_info.gpt_l1_addr,
12376a00e9b0SRobert Wakim 		  gpi_info.gpi_shift, gpi_info.idx, GPT_GPI_NS);
12386a00e9b0SRobert Wakim 	dsboshst();
12396a00e9b0SRobert Wakim 
12406a00e9b0SRobert Wakim 	/* Ensure that all agents observe the new NS configuration */
12416a00e9b0SRobert Wakim 	gpt_tlbi_by_pa_ll(base, GPT_PGS_ACTUAL_SIZE(gpt_config.p));
12426a00e9b0SRobert Wakim 	dsbosh();
1243f19dc624Sjohpow01 
1244f19dc624Sjohpow01 	/* Unlock access to the L1 tables. */
1245f19dc624Sjohpow01 	spin_unlock(&gpt_lock);
1246f19dc624Sjohpow01 
124777612b90SSoby Mathew 	/*
124877612b90SSoby Mathew 	 * The isb() will be done as part of context
124977612b90SSoby Mathew 	 * synchronization when returning to lower EL
125077612b90SSoby Mathew 	 */
12516a00e9b0SRobert Wakim 	VERBOSE("[GPT] Granule 0x%" PRIx64 ", GPI 0x%x->0x%x\n",
12526a00e9b0SRobert Wakim 		base, gpi_info.gpi, GPT_GPI_NS);
1253f19dc624Sjohpow01 
1254f19dc624Sjohpow01 	return 0;
1255f19dc624Sjohpow01 }
1256