xref: /aosp_15_r20/external/arm-trusted-firmware/lib/gpt_rme/gpt_rme.c (revision 54fd6939e177f8ff529b10183254802c76df6d08)
1*54fd6939SJiyong Park /*
2*54fd6939SJiyong Park  * Copyright (c) 2021, Arm Limited. All rights reserved.
3*54fd6939SJiyong Park  *
4*54fd6939SJiyong Park  * SPDX-License-Identifier: BSD-3-Clause
5*54fd6939SJiyong Park  */
6*54fd6939SJiyong Park 
7*54fd6939SJiyong Park #include <assert.h>
8*54fd6939SJiyong Park #include <errno.h>
9*54fd6939SJiyong Park #include <inttypes.h>
10*54fd6939SJiyong Park #include <limits.h>
11*54fd6939SJiyong Park #include <stdint.h>
12*54fd6939SJiyong Park 
13*54fd6939SJiyong Park #include <arch.h>
14*54fd6939SJiyong Park #include <arch_helpers.h>
15*54fd6939SJiyong Park #include <common/debug.h>
16*54fd6939SJiyong Park #include "gpt_rme_private.h"
17*54fd6939SJiyong Park #include <lib/gpt_rme/gpt_rme.h>
18*54fd6939SJiyong Park #include <lib/smccc.h>
19*54fd6939SJiyong Park #include <lib/spinlock.h>
20*54fd6939SJiyong Park #include <lib/xlat_tables/xlat_tables_v2.h>
21*54fd6939SJiyong Park 
22*54fd6939SJiyong Park #if !ENABLE_RME
23*54fd6939SJiyong Park #error "ENABLE_RME must be enabled to use the GPT library."
24*54fd6939SJiyong Park #endif
25*54fd6939SJiyong Park 
26*54fd6939SJiyong Park /*
27*54fd6939SJiyong Park  * Lookup T from PPS
28*54fd6939SJiyong Park  *
29*54fd6939SJiyong Park  *   PPS    Size    T
30*54fd6939SJiyong Park  *   0b000  4GB     32
31*54fd6939SJiyong Park  *   0b001  64GB    36
32*54fd6939SJiyong Park  *   0b010  1TB     40
33*54fd6939SJiyong Park  *   0b011  4TB     42
34*54fd6939SJiyong Park  *   0b100  16TB    44
35*54fd6939SJiyong Park  *   0b101  256TB   48
36*54fd6939SJiyong Park  *   0b110  4PB     52
37*54fd6939SJiyong Park  *
38*54fd6939SJiyong Park  * See section 15.1.27 of the RME specification.
39*54fd6939SJiyong Park  */
40*54fd6939SJiyong Park static const gpt_t_val_e gpt_t_lookup[] = {PPS_4GB_T, PPS_64GB_T,
41*54fd6939SJiyong Park 					   PPS_1TB_T, PPS_4TB_T,
42*54fd6939SJiyong Park 					   PPS_16TB_T, PPS_256TB_T,
43*54fd6939SJiyong Park 					   PPS_4PB_T};
44*54fd6939SJiyong Park 
45*54fd6939SJiyong Park /*
46*54fd6939SJiyong Park  * Lookup P from PGS
47*54fd6939SJiyong Park  *
48*54fd6939SJiyong Park  *   PGS    Size    P
49*54fd6939SJiyong Park  *   0b00   4KB     12
50*54fd6939SJiyong Park  *   0b10   16KB    14
51*54fd6939SJiyong Park  *   0b01   64KB    16
52*54fd6939SJiyong Park  *
53*54fd6939SJiyong Park  * Note that pgs=0b10 is 16KB and pgs=0b01 is 64KB, this is not a typo.
54*54fd6939SJiyong Park  *
55*54fd6939SJiyong Park  * See section 15.1.27 of the RME specification.
56*54fd6939SJiyong Park  */
57*54fd6939SJiyong Park static const gpt_p_val_e gpt_p_lookup[] = {PGS_4KB_P, PGS_64KB_P, PGS_16KB_P};
58*54fd6939SJiyong Park 
59*54fd6939SJiyong Park /*
60*54fd6939SJiyong Park  * This structure contains GPT configuration data.
61*54fd6939SJiyong Park  */
62*54fd6939SJiyong Park typedef struct {
63*54fd6939SJiyong Park 	uintptr_t plat_gpt_l0_base;
64*54fd6939SJiyong Park 	gpccr_pps_e pps;
65*54fd6939SJiyong Park 	gpt_t_val_e t;
66*54fd6939SJiyong Park 	gpccr_pgs_e pgs;
67*54fd6939SJiyong Park 	gpt_p_val_e p;
68*54fd6939SJiyong Park } gpt_config_t;
69*54fd6939SJiyong Park 
70*54fd6939SJiyong Park static gpt_config_t gpt_config;
71*54fd6939SJiyong Park 
72*54fd6939SJiyong Park /* These variables are used during initialization of the L1 tables. */
73*54fd6939SJiyong Park static unsigned int gpt_next_l1_tbl_idx;
74*54fd6939SJiyong Park static uintptr_t gpt_l1_tbl;
75*54fd6939SJiyong Park 
76*54fd6939SJiyong Park /*
77*54fd6939SJiyong Park  * This function checks to see if a GPI value is valid.
78*54fd6939SJiyong Park  *
79*54fd6939SJiyong Park  * These are valid GPI values.
80*54fd6939SJiyong Park  *   GPT_GPI_NO_ACCESS   U(0x0)
81*54fd6939SJiyong Park  *   GPT_GPI_SECURE      U(0x8)
82*54fd6939SJiyong Park  *   GPT_GPI_NS          U(0x9)
83*54fd6939SJiyong Park  *   GPT_GPI_ROOT        U(0xA)
84*54fd6939SJiyong Park  *   GPT_GPI_REALM       U(0xB)
85*54fd6939SJiyong Park  *   GPT_GPI_ANY         U(0xF)
86*54fd6939SJiyong Park  *
87*54fd6939SJiyong Park  * Parameters
88*54fd6939SJiyong Park  *   gpi		GPI to check for validity.
89*54fd6939SJiyong Park  *
90*54fd6939SJiyong Park  * Return
91*54fd6939SJiyong Park  *   true for a valid GPI, false for an invalid one.
92*54fd6939SJiyong Park  */
gpt_is_gpi_valid(unsigned int gpi)93*54fd6939SJiyong Park static bool gpt_is_gpi_valid(unsigned int gpi)
94*54fd6939SJiyong Park {
95*54fd6939SJiyong Park 	if ((gpi == GPT_GPI_NO_ACCESS) || (gpi == GPT_GPI_ANY) ||
96*54fd6939SJiyong Park 	    ((gpi >= GPT_GPI_SECURE) && (gpi <= GPT_GPI_REALM))) {
97*54fd6939SJiyong Park 		return true;
98*54fd6939SJiyong Park 	} else {
99*54fd6939SJiyong Park 		return false;
100*54fd6939SJiyong Park 	}
101*54fd6939SJiyong Park }
102*54fd6939SJiyong Park 
103*54fd6939SJiyong Park /*
104*54fd6939SJiyong Park  * This function checks to see if two PAS regions overlap.
105*54fd6939SJiyong Park  *
106*54fd6939SJiyong Park  * Parameters
107*54fd6939SJiyong Park  *   base_1: base address of first PAS
108*54fd6939SJiyong Park  *   size_1: size of first PAS
109*54fd6939SJiyong Park  *   base_2: base address of second PAS
110*54fd6939SJiyong Park  *   size_2: size of second PAS
111*54fd6939SJiyong Park  *
112*54fd6939SJiyong Park  * Return
113*54fd6939SJiyong Park  *   True if PAS regions overlap, false if they do not.
114*54fd6939SJiyong Park  */
gpt_check_pas_overlap(uintptr_t base_1,size_t size_1,uintptr_t base_2,size_t size_2)115*54fd6939SJiyong Park static bool gpt_check_pas_overlap(uintptr_t base_1, size_t size_1,
116*54fd6939SJiyong Park 				  uintptr_t base_2, size_t size_2)
117*54fd6939SJiyong Park {
118*54fd6939SJiyong Park 	if (((base_1 + size_1) > base_2) && ((base_2 + size_2) > base_1)) {
119*54fd6939SJiyong Park 		return true;
120*54fd6939SJiyong Park 	} else {
121*54fd6939SJiyong Park 		return false;
122*54fd6939SJiyong Park 	}
123*54fd6939SJiyong Park }
124*54fd6939SJiyong Park 
125*54fd6939SJiyong Park /*
126*54fd6939SJiyong Park  * This helper function checks to see if a PAS region from index 0 to
127*54fd6939SJiyong Park  * (pas_idx - 1) occupies the L0 region at index l0_idx in the L0 table.
128*54fd6939SJiyong Park  *
129*54fd6939SJiyong Park  * Parameters
130*54fd6939SJiyong Park  *   l0_idx:      Index of the L0 entry to check
131*54fd6939SJiyong Park  *   pas_regions: PAS region array
132*54fd6939SJiyong Park  *   pas_idx:     Upper bound of the PAS array index.
133*54fd6939SJiyong Park  *
134*54fd6939SJiyong Park  * Return
135*54fd6939SJiyong Park  *   True if a PAS region occupies the L0 region in question, false if not.
136*54fd6939SJiyong Park  */
gpt_does_previous_pas_exist_here(unsigned int l0_idx,pas_region_t * pas_regions,unsigned int pas_idx)137*54fd6939SJiyong Park static bool gpt_does_previous_pas_exist_here(unsigned int l0_idx,
138*54fd6939SJiyong Park 					     pas_region_t *pas_regions,
139*54fd6939SJiyong Park 					     unsigned int pas_idx)
140*54fd6939SJiyong Park {
141*54fd6939SJiyong Park 	/* Iterate over PAS regions up to pas_idx. */
142*54fd6939SJiyong Park 	for (unsigned int i = 0U; i < pas_idx; i++) {
143*54fd6939SJiyong Park 		if (gpt_check_pas_overlap((GPT_L0GPTSZ_ACTUAL_SIZE * l0_idx),
144*54fd6939SJiyong Park 		    GPT_L0GPTSZ_ACTUAL_SIZE,
145*54fd6939SJiyong Park 		    pas_regions[i].base_pa, pas_regions[i].size)) {
146*54fd6939SJiyong Park 			return true;
147*54fd6939SJiyong Park 		}
148*54fd6939SJiyong Park 	}
149*54fd6939SJiyong Park 	return false;
150*54fd6939SJiyong Park }
151*54fd6939SJiyong Park 
152*54fd6939SJiyong Park /*
153*54fd6939SJiyong Park  * This function iterates over all of the PAS regions and checks them to ensure
154*54fd6939SJiyong Park  * proper alignment of base and size, that the GPI is valid, and that no regions
155*54fd6939SJiyong Park  * overlap. As a part of the overlap checks, this function checks existing L0
156*54fd6939SJiyong Park  * mappings against the new PAS regions in the event that gpt_init_pas_l1_tables
157*54fd6939SJiyong Park  * is called multiple times to place L1 tables in different areas of memory. It
158*54fd6939SJiyong Park  * also counts the number of L1 tables needed and returns it on success.
159*54fd6939SJiyong Park  *
160*54fd6939SJiyong Park  * Parameters
161*54fd6939SJiyong Park  *   *pas_regions	Pointer to array of PAS region structures.
162*54fd6939SJiyong Park  *   pas_region_cnt	Total number of PAS regions in the array.
163*54fd6939SJiyong Park  *
164*54fd6939SJiyong Park  * Return
165*54fd6939SJiyong Park  *   Negative Linux error code in the event of a failure, number of L1 regions
166*54fd6939SJiyong Park  *   required when successful.
167*54fd6939SJiyong Park  */
gpt_validate_pas_mappings(pas_region_t * pas_regions,unsigned int pas_region_cnt)168*54fd6939SJiyong Park static int gpt_validate_pas_mappings(pas_region_t *pas_regions,
169*54fd6939SJiyong Park 				     unsigned int pas_region_cnt)
170*54fd6939SJiyong Park {
171*54fd6939SJiyong Park 	unsigned int idx;
172*54fd6939SJiyong Park 	unsigned int l1_cnt = 0U;
173*54fd6939SJiyong Park 	unsigned int pas_l1_cnt;
174*54fd6939SJiyong Park 	uint64_t *l0_desc = (uint64_t *)gpt_config.plat_gpt_l0_base;
175*54fd6939SJiyong Park 
176*54fd6939SJiyong Park 	assert(pas_regions != NULL);
177*54fd6939SJiyong Park 	assert(pas_region_cnt != 0U);
178*54fd6939SJiyong Park 
179*54fd6939SJiyong Park 	for (idx = 0U; idx < pas_region_cnt; idx++) {
180*54fd6939SJiyong Park 		/* Check for arithmetic overflow in region. */
181*54fd6939SJiyong Park 		if ((ULONG_MAX - pas_regions[idx].base_pa) <
182*54fd6939SJiyong Park 		    pas_regions[idx].size) {
183*54fd6939SJiyong Park 			ERROR("[GPT] Address overflow in PAS[%u]!\n", idx);
184*54fd6939SJiyong Park 			return -EOVERFLOW;
185*54fd6939SJiyong Park 		}
186*54fd6939SJiyong Park 
187*54fd6939SJiyong Park 		/* Initial checks for PAS validity. */
188*54fd6939SJiyong Park 		if (((pas_regions[idx].base_pa + pas_regions[idx].size) >
189*54fd6939SJiyong Park 		    GPT_PPS_ACTUAL_SIZE(gpt_config.t)) ||
190*54fd6939SJiyong Park 		    !gpt_is_gpi_valid(GPT_PAS_ATTR_GPI(pas_regions[idx].attrs))) {
191*54fd6939SJiyong Park 			ERROR("[GPT] PAS[%u] is invalid!\n", idx);
192*54fd6939SJiyong Park 			return -EFAULT;
193*54fd6939SJiyong Park 		}
194*54fd6939SJiyong Park 
195*54fd6939SJiyong Park 		/*
196*54fd6939SJiyong Park 		 * Make sure this PAS does not overlap with another one. We
197*54fd6939SJiyong Park 		 * start from idx + 1 instead of 0 since prior PAS mappings will
198*54fd6939SJiyong Park 		 * have already checked themselves against this one.
199*54fd6939SJiyong Park 		 */
200*54fd6939SJiyong Park 		for (unsigned int i = idx + 1; i < pas_region_cnt; i++) {
201*54fd6939SJiyong Park 			if (gpt_check_pas_overlap(pas_regions[idx].base_pa,
202*54fd6939SJiyong Park 			    pas_regions[idx].size,
203*54fd6939SJiyong Park 			    pas_regions[i].base_pa,
204*54fd6939SJiyong Park 			    pas_regions[i].size)) {
205*54fd6939SJiyong Park 				ERROR("[GPT] PAS[%u] overlaps with PAS[%u]\n",
206*54fd6939SJiyong Park 					i, idx);
207*54fd6939SJiyong Park 				return -EFAULT;
208*54fd6939SJiyong Park 			}
209*54fd6939SJiyong Park 		}
210*54fd6939SJiyong Park 
211*54fd6939SJiyong Park 		/*
212*54fd6939SJiyong Park 		 * Since this function can be called multiple times with
213*54fd6939SJiyong Park 		 * separate L1 tables we need to check the existing L0 mapping
214*54fd6939SJiyong Park 		 * to see if this PAS would fall into one that has already been
215*54fd6939SJiyong Park 		 * initialized.
216*54fd6939SJiyong Park 		 */
217*54fd6939SJiyong Park 		for (unsigned int i = GPT_L0_IDX(pas_regions[idx].base_pa);
218*54fd6939SJiyong Park 		     i <= GPT_L0_IDX(pas_regions[idx].base_pa + pas_regions[idx].size - 1);
219*54fd6939SJiyong Park 		     i++) {
220*54fd6939SJiyong Park 			if ((GPT_L0_TYPE(l0_desc[i]) == GPT_L0_TYPE_BLK_DESC) &&
221*54fd6939SJiyong Park 			    (GPT_L0_BLKD_GPI(l0_desc[i]) == GPT_GPI_ANY)) {
222*54fd6939SJiyong Park 				/* This descriptor is unused so continue. */
223*54fd6939SJiyong Park 				continue;
224*54fd6939SJiyong Park 			}
225*54fd6939SJiyong Park 
226*54fd6939SJiyong Park 			/*
227*54fd6939SJiyong Park 			 * This descriptor has been initialized in a previous
228*54fd6939SJiyong Park 			 * call to this function so cannot be initialized again.
229*54fd6939SJiyong Park 			 */
230*54fd6939SJiyong Park 			ERROR("[GPT] PAS[%u] overlaps with previous L0[%d]!\n",
231*54fd6939SJiyong Park 			      idx, i);
232*54fd6939SJiyong Park 			return -EFAULT;
233*54fd6939SJiyong Park 		}
234*54fd6939SJiyong Park 
235*54fd6939SJiyong Park 		/* Check for block mapping (L0) type. */
236*54fd6939SJiyong Park 		if (GPT_PAS_ATTR_MAP_TYPE(pas_regions[idx].attrs) ==
237*54fd6939SJiyong Park 		    GPT_PAS_ATTR_MAP_TYPE_BLOCK) {
238*54fd6939SJiyong Park 			/* Make sure base and size are block-aligned. */
239*54fd6939SJiyong Park 			if (!GPT_IS_L0_ALIGNED(pas_regions[idx].base_pa) ||
240*54fd6939SJiyong Park 			    !GPT_IS_L0_ALIGNED(pas_regions[idx].size)) {
241*54fd6939SJiyong Park 				ERROR("[GPT] PAS[%u] is not block-aligned!\n",
242*54fd6939SJiyong Park 				      idx);
243*54fd6939SJiyong Park 				return -EFAULT;
244*54fd6939SJiyong Park 			}
245*54fd6939SJiyong Park 
246*54fd6939SJiyong Park 			continue;
247*54fd6939SJiyong Park 		}
248*54fd6939SJiyong Park 
249*54fd6939SJiyong Park 		/* Check for granule mapping (L1) type. */
250*54fd6939SJiyong Park 		if (GPT_PAS_ATTR_MAP_TYPE(pas_regions[idx].attrs) ==
251*54fd6939SJiyong Park 		    GPT_PAS_ATTR_MAP_TYPE_GRANULE) {
252*54fd6939SJiyong Park 			/* Make sure base and size are granule-aligned. */
253*54fd6939SJiyong Park 			if (!GPT_IS_L1_ALIGNED(gpt_config.p, pas_regions[idx].base_pa) ||
254*54fd6939SJiyong Park 			    !GPT_IS_L1_ALIGNED(gpt_config.p, pas_regions[idx].size)) {
255*54fd6939SJiyong Park 				ERROR("[GPT] PAS[%u] is not granule-aligned!\n",
256*54fd6939SJiyong Park 				      idx);
257*54fd6939SJiyong Park 				return -EFAULT;
258*54fd6939SJiyong Park 			}
259*54fd6939SJiyong Park 
260*54fd6939SJiyong Park 			/* Find how many L1 tables this PAS occupies. */
261*54fd6939SJiyong Park 			pas_l1_cnt = (GPT_L0_IDX(pas_regions[idx].base_pa +
262*54fd6939SJiyong Park 				     pas_regions[idx].size - 1) -
263*54fd6939SJiyong Park 				     GPT_L0_IDX(pas_regions[idx].base_pa) + 1);
264*54fd6939SJiyong Park 
265*54fd6939SJiyong Park 			/*
266*54fd6939SJiyong Park 			 * This creates a situation where, if multiple PAS
267*54fd6939SJiyong Park 			 * regions occupy the same table descriptor, we can get
268*54fd6939SJiyong Park 			 * an artificially high total L1 table count. The way we
269*54fd6939SJiyong Park 			 * handle this is by checking each PAS against those
270*54fd6939SJiyong Park 			 * before it in the array, and if they both occupy the
271*54fd6939SJiyong Park 			 * same PAS we subtract from pas_l1_cnt and only the
272*54fd6939SJiyong Park 			 * first PAS in the array gets to count it.
273*54fd6939SJiyong Park 			 */
274*54fd6939SJiyong Park 
275*54fd6939SJiyong Park 			/*
276*54fd6939SJiyong Park 			 * If L1 count is greater than 1 we know the start and
277*54fd6939SJiyong Park 			 * end PAs are in different L0 regions so we must check
278*54fd6939SJiyong Park 			 * both for overlap against other PAS.
279*54fd6939SJiyong Park 			 */
280*54fd6939SJiyong Park 			if (pas_l1_cnt > 1) {
281*54fd6939SJiyong Park 				if (gpt_does_previous_pas_exist_here(
282*54fd6939SJiyong Park 				    GPT_L0_IDX(pas_regions[idx].base_pa +
283*54fd6939SJiyong Park 				    pas_regions[idx].size - 1),
284*54fd6939SJiyong Park 				    pas_regions, idx)) {
285*54fd6939SJiyong Park 					pas_l1_cnt = pas_l1_cnt - 1;
286*54fd6939SJiyong Park 				}
287*54fd6939SJiyong Park 			}
288*54fd6939SJiyong Park 
289*54fd6939SJiyong Park 			if (gpt_does_previous_pas_exist_here(
290*54fd6939SJiyong Park 			    GPT_L0_IDX(pas_regions[idx].base_pa),
291*54fd6939SJiyong Park 			    pas_regions, idx)) {
292*54fd6939SJiyong Park 				pas_l1_cnt = pas_l1_cnt - 1;
293*54fd6939SJiyong Park 			}
294*54fd6939SJiyong Park 
295*54fd6939SJiyong Park 			l1_cnt += pas_l1_cnt;
296*54fd6939SJiyong Park 			continue;
297*54fd6939SJiyong Park 		}
298*54fd6939SJiyong Park 
299*54fd6939SJiyong Park 		/* If execution reaches this point, mapping type is invalid. */
300*54fd6939SJiyong Park 		ERROR("[GPT] PAS[%u] has invalid mapping type 0x%x.\n", idx,
301*54fd6939SJiyong Park 		      GPT_PAS_ATTR_MAP_TYPE(pas_regions[idx].attrs));
302*54fd6939SJiyong Park 		return -EINVAL;
303*54fd6939SJiyong Park 	}
304*54fd6939SJiyong Park 
305*54fd6939SJiyong Park 	return l1_cnt;
306*54fd6939SJiyong Park }
307*54fd6939SJiyong Park 
308*54fd6939SJiyong Park /*
309*54fd6939SJiyong Park  * This function validates L0 initialization parameters.
310*54fd6939SJiyong Park  *
311*54fd6939SJiyong Park  * Parameters
312*54fd6939SJiyong Park  *   l0_mem_base	Base address of memory used for L0 tables.
313*54fd6939SJiyong Park  *   l1_mem_size	Size of memory available for L0 tables.
314*54fd6939SJiyong Park  *
315*54fd6939SJiyong Park  * Return
316*54fd6939SJiyong Park  *   Negative Linux error code in the event of a failure, 0 for success.
317*54fd6939SJiyong Park  */
gpt_validate_l0_params(gpccr_pps_e pps,uintptr_t l0_mem_base,size_t l0_mem_size)318*54fd6939SJiyong Park static int gpt_validate_l0_params(gpccr_pps_e pps, uintptr_t l0_mem_base,
319*54fd6939SJiyong Park 				  size_t l0_mem_size)
320*54fd6939SJiyong Park {
321*54fd6939SJiyong Park 	size_t l0_alignment;
322*54fd6939SJiyong Park 
323*54fd6939SJiyong Park 	/*
324*54fd6939SJiyong Park 	 * Make sure PPS is valid and then store it since macros need this value
325*54fd6939SJiyong Park 	 * to work.
326*54fd6939SJiyong Park 	 */
327*54fd6939SJiyong Park 	if (pps > GPT_PPS_MAX) {
328*54fd6939SJiyong Park 		ERROR("[GPT] Invalid PPS: 0x%x\n", pps);
329*54fd6939SJiyong Park 		return -EINVAL;
330*54fd6939SJiyong Park 	}
331*54fd6939SJiyong Park 	gpt_config.pps = pps;
332*54fd6939SJiyong Park 	gpt_config.t = gpt_t_lookup[pps];
333*54fd6939SJiyong Park 
334*54fd6939SJiyong Park 	/* Alignment must be the greater of 4k or l0 table size. */
335*54fd6939SJiyong Park 	l0_alignment = PAGE_SIZE_4KB;
336*54fd6939SJiyong Park 	if (l0_alignment < GPT_L0_TABLE_SIZE(gpt_config.t)) {
337*54fd6939SJiyong Park 		l0_alignment = GPT_L0_TABLE_SIZE(gpt_config.t);
338*54fd6939SJiyong Park 	}
339*54fd6939SJiyong Park 
340*54fd6939SJiyong Park 	/* Check base address. */
341*54fd6939SJiyong Park 	if ((l0_mem_base == 0U) || ((l0_mem_base & (l0_alignment - 1)) != 0U)) {
342*54fd6939SJiyong Park 		ERROR("[GPT] Invalid L0 base address: 0x%lx\n", l0_mem_base);
343*54fd6939SJiyong Park 		return -EFAULT;
344*54fd6939SJiyong Park 	}
345*54fd6939SJiyong Park 
346*54fd6939SJiyong Park 	/* Check size. */
347*54fd6939SJiyong Park 	if (l0_mem_size < GPT_L0_TABLE_SIZE(gpt_config.t)) {
348*54fd6939SJiyong Park 		ERROR("[GPT] Inadequate L0 memory: need 0x%lx, have 0x%lx)\n",
349*54fd6939SJiyong Park 		      GPT_L0_TABLE_SIZE(gpt_config.t),
350*54fd6939SJiyong Park 		      l0_mem_size);
351*54fd6939SJiyong Park 		return -ENOMEM;
352*54fd6939SJiyong Park 	}
353*54fd6939SJiyong Park 
354*54fd6939SJiyong Park 	return 0;
355*54fd6939SJiyong Park }
356*54fd6939SJiyong Park 
357*54fd6939SJiyong Park /*
358*54fd6939SJiyong Park  * In the event that L1 tables are needed, this function validates
359*54fd6939SJiyong Park  * the L1 table generation parameters.
360*54fd6939SJiyong Park  *
361*54fd6939SJiyong Park  * Parameters
362*54fd6939SJiyong Park  *   l1_mem_base	Base address of memory used for L1 table allocation.
363*54fd6939SJiyong Park  *   l1_mem_size	Total size of memory available for L1 tables.
364*54fd6939SJiyong Park  *   l1_gpt_cnt		Number of L1 tables needed.
365*54fd6939SJiyong Park  *
366*54fd6939SJiyong Park  * Return
367*54fd6939SJiyong Park  *   Negative Linux error code in the event of a failure, 0 for success.
368*54fd6939SJiyong Park  */
gpt_validate_l1_params(uintptr_t l1_mem_base,size_t l1_mem_size,unsigned int l1_gpt_cnt)369*54fd6939SJiyong Park static int gpt_validate_l1_params(uintptr_t l1_mem_base, size_t l1_mem_size,
370*54fd6939SJiyong Park 				  unsigned int l1_gpt_cnt)
371*54fd6939SJiyong Park {
372*54fd6939SJiyong Park 	size_t l1_gpt_mem_sz;
373*54fd6939SJiyong Park 
374*54fd6939SJiyong Park 	/* Check if the granularity is supported */
375*54fd6939SJiyong Park 	if (!xlat_arch_is_granule_size_supported(
376*54fd6939SJiyong Park 	    GPT_PGS_ACTUAL_SIZE(gpt_config.p))) {
377*54fd6939SJiyong Park 		return -EPERM;
378*54fd6939SJiyong Park 	}
379*54fd6939SJiyong Park 
380*54fd6939SJiyong Park 	/* Make sure L1 tables are aligned to their size. */
381*54fd6939SJiyong Park 	if ((l1_mem_base & (GPT_L1_TABLE_SIZE(gpt_config.p) - 1)) != 0U) {
382*54fd6939SJiyong Park 		ERROR("[GPT] Unaligned L1 GPT base address: 0x%lx\n",
383*54fd6939SJiyong Park 		      l1_mem_base);
384*54fd6939SJiyong Park 		return -EFAULT;
385*54fd6939SJiyong Park 	}
386*54fd6939SJiyong Park 
387*54fd6939SJiyong Park 	/* Get total memory needed for L1 tables. */
388*54fd6939SJiyong Park 	l1_gpt_mem_sz = l1_gpt_cnt * GPT_L1_TABLE_SIZE(gpt_config.p);
389*54fd6939SJiyong Park 
390*54fd6939SJiyong Park 	/* Check for overflow. */
391*54fd6939SJiyong Park 	if ((l1_gpt_mem_sz / GPT_L1_TABLE_SIZE(gpt_config.p)) != l1_gpt_cnt) {
392*54fd6939SJiyong Park 		ERROR("[GPT] Overflow calculating L1 memory size.\n");
393*54fd6939SJiyong Park 		return -ENOMEM;
394*54fd6939SJiyong Park 	}
395*54fd6939SJiyong Park 
396*54fd6939SJiyong Park 	/* Make sure enough space was supplied. */
397*54fd6939SJiyong Park 	if (l1_mem_size < l1_gpt_mem_sz) {
398*54fd6939SJiyong Park 		ERROR("[GPT] Inadequate memory for L1 GPTs. ");
399*54fd6939SJiyong Park 		ERROR("      Expected 0x%lx bytes. Got 0x%lx bytes\n",
400*54fd6939SJiyong Park 		      l1_gpt_mem_sz, l1_mem_size);
401*54fd6939SJiyong Park 		return -ENOMEM;
402*54fd6939SJiyong Park 	}
403*54fd6939SJiyong Park 
404*54fd6939SJiyong Park 	VERBOSE("[GPT] Requested 0x%lx bytes for L1 GPTs.\n", l1_gpt_mem_sz);
405*54fd6939SJiyong Park 	return 0;
406*54fd6939SJiyong Park }
407*54fd6939SJiyong Park 
408*54fd6939SJiyong Park /*
409*54fd6939SJiyong Park  * This function initializes L0 block descriptors (regions that cannot be
410*54fd6939SJiyong Park  * transitioned at the granule level) according to the provided PAS.
411*54fd6939SJiyong Park  *
412*54fd6939SJiyong Park  * Parameters
413*54fd6939SJiyong Park  *   *pas		Pointer to the structure defining the PAS region to
414*54fd6939SJiyong Park  *			initialize.
415*54fd6939SJiyong Park  */
gpt_generate_l0_blk_desc(pas_region_t * pas)416*54fd6939SJiyong Park static void gpt_generate_l0_blk_desc(pas_region_t *pas)
417*54fd6939SJiyong Park {
418*54fd6939SJiyong Park 	uint64_t gpt_desc;
419*54fd6939SJiyong Park 	unsigned int end_idx;
420*54fd6939SJiyong Park 	unsigned int idx;
421*54fd6939SJiyong Park 	uint64_t *l0_gpt_arr;
422*54fd6939SJiyong Park 
423*54fd6939SJiyong Park 	assert(gpt_config.plat_gpt_l0_base != 0U);
424*54fd6939SJiyong Park 	assert(pas != NULL);
425*54fd6939SJiyong Park 
426*54fd6939SJiyong Park 	/*
427*54fd6939SJiyong Park 	 * Checking of PAS parameters has already been done in
428*54fd6939SJiyong Park 	 * gpt_validate_pas_mappings so no need to check the same things again.
429*54fd6939SJiyong Park 	 */
430*54fd6939SJiyong Park 
431*54fd6939SJiyong Park 	l0_gpt_arr = (uint64_t *)gpt_config.plat_gpt_l0_base;
432*54fd6939SJiyong Park 
433*54fd6939SJiyong Park 	/* Create the GPT Block descriptor for this PAS region */
434*54fd6939SJiyong Park 	gpt_desc = GPT_L0_BLK_DESC(GPT_PAS_ATTR_GPI(pas->attrs));
435*54fd6939SJiyong Park 
436*54fd6939SJiyong Park 	/* Start index of this region in L0 GPTs */
437*54fd6939SJiyong Park 	idx = pas->base_pa >> GPT_L0_IDX_SHIFT;
438*54fd6939SJiyong Park 
439*54fd6939SJiyong Park 	/*
440*54fd6939SJiyong Park 	 * Determine number of L0 GPT descriptors covered by
441*54fd6939SJiyong Park 	 * this PAS region and use the count to populate these
442*54fd6939SJiyong Park 	 * descriptors.
443*54fd6939SJiyong Park 	 */
444*54fd6939SJiyong Park 	end_idx = (pas->base_pa + pas->size) >> GPT_L0_IDX_SHIFT;
445*54fd6939SJiyong Park 
446*54fd6939SJiyong Park 	/* Generate the needed block descriptors. */
447*54fd6939SJiyong Park 	for (; idx < end_idx; idx++) {
448*54fd6939SJiyong Park 		l0_gpt_arr[idx] = gpt_desc;
449*54fd6939SJiyong Park 		VERBOSE("[GPT] L0 entry (BLOCK) index %u [%p]: GPI = 0x%" PRIx64 " (0x%" PRIx64 ")\n",
450*54fd6939SJiyong Park 			idx, &l0_gpt_arr[idx],
451*54fd6939SJiyong Park 			(gpt_desc >> GPT_L0_BLK_DESC_GPI_SHIFT) &
452*54fd6939SJiyong Park 			GPT_L0_BLK_DESC_GPI_MASK, l0_gpt_arr[idx]);
453*54fd6939SJiyong Park 	}
454*54fd6939SJiyong Park }
455*54fd6939SJiyong Park 
456*54fd6939SJiyong Park /*
457*54fd6939SJiyong Park  * Helper function to determine if the end physical address lies in the same L0
458*54fd6939SJiyong Park  * region as the current physical address. If true, the end physical address is
459*54fd6939SJiyong Park  * returned else, the start address of the next region is returned.
460*54fd6939SJiyong Park  *
461*54fd6939SJiyong Park  * Parameters
462*54fd6939SJiyong Park  *   cur_pa		Physical address of the current PA in the loop through
463*54fd6939SJiyong Park  *			the range.
464*54fd6939SJiyong Park  *   end_pa		Physical address of the end PA in a PAS range.
465*54fd6939SJiyong Park  *
466*54fd6939SJiyong Park  * Return
467*54fd6939SJiyong Park  *   The PA of the end of the current range.
468*54fd6939SJiyong Park  */
gpt_get_l1_end_pa(uintptr_t cur_pa,uintptr_t end_pa)469*54fd6939SJiyong Park static uintptr_t gpt_get_l1_end_pa(uintptr_t cur_pa, uintptr_t end_pa)
470*54fd6939SJiyong Park {
471*54fd6939SJiyong Park 	uintptr_t cur_idx;
472*54fd6939SJiyong Park 	uintptr_t end_idx;
473*54fd6939SJiyong Park 
474*54fd6939SJiyong Park 	cur_idx = cur_pa >> GPT_L0_IDX_SHIFT;
475*54fd6939SJiyong Park 	end_idx = end_pa >> GPT_L0_IDX_SHIFT;
476*54fd6939SJiyong Park 
477*54fd6939SJiyong Park 	assert(cur_idx <= end_idx);
478*54fd6939SJiyong Park 
479*54fd6939SJiyong Park 	if (cur_idx == end_idx) {
480*54fd6939SJiyong Park 		return end_pa;
481*54fd6939SJiyong Park 	}
482*54fd6939SJiyong Park 
483*54fd6939SJiyong Park 	return (cur_idx + 1U) << GPT_L0_IDX_SHIFT;
484*54fd6939SJiyong Park }
485*54fd6939SJiyong Park 
486*54fd6939SJiyong Park /*
487*54fd6939SJiyong Park  * Helper function to fill out GPI entries in a single L1 table. This function
488*54fd6939SJiyong Park  * fills out entire L1 descriptors at a time to save memory writes.
489*54fd6939SJiyong Park  *
490*54fd6939SJiyong Park  * Parameters
491*54fd6939SJiyong Park  *   gpi		GPI to set this range to
492*54fd6939SJiyong Park  *   l1			Pointer to L1 table to fill out
493*54fd6939SJiyong Park  *   first		Address of first granule in range.
494*54fd6939SJiyong Park  *   last		Address of last granule in range (inclusive).
495*54fd6939SJiyong Park  */
gpt_fill_l1_tbl(uint64_t gpi,uint64_t * l1,uintptr_t first,uintptr_t last)496*54fd6939SJiyong Park static void gpt_fill_l1_tbl(uint64_t gpi, uint64_t *l1, uintptr_t first,
497*54fd6939SJiyong Park 			    uintptr_t last)
498*54fd6939SJiyong Park {
499*54fd6939SJiyong Park 	uint64_t gpi_field = GPT_BUILD_L1_DESC(gpi);
500*54fd6939SJiyong Park 	uint64_t gpi_mask = 0xFFFFFFFFFFFFFFFF;
501*54fd6939SJiyong Park 
502*54fd6939SJiyong Park 	assert(first <= last);
503*54fd6939SJiyong Park 	assert((first & (GPT_PGS_ACTUAL_SIZE(gpt_config.p) - 1)) == 0U);
504*54fd6939SJiyong Park 	assert((last & (GPT_PGS_ACTUAL_SIZE(gpt_config.p) - 1)) == 0U);
505*54fd6939SJiyong Park 	assert(GPT_L0_IDX(first) == GPT_L0_IDX(last));
506*54fd6939SJiyong Park 	assert(l1 != NULL);
507*54fd6939SJiyong Park 
508*54fd6939SJiyong Park 	/* Shift the mask if we're starting in the middle of an L1 entry. */
509*54fd6939SJiyong Park 	gpi_mask = gpi_mask << (GPT_L1_GPI_IDX(gpt_config.p, first) << 2);
510*54fd6939SJiyong Park 
511*54fd6939SJiyong Park 	/* Fill out each L1 entry for this region. */
512*54fd6939SJiyong Park 	for (unsigned int i = GPT_L1_IDX(gpt_config.p, first);
513*54fd6939SJiyong Park 	     i <= GPT_L1_IDX(gpt_config.p, last); i++) {
514*54fd6939SJiyong Park 		/* Account for stopping in the middle of an L1 entry. */
515*54fd6939SJiyong Park 		if (i == GPT_L1_IDX(gpt_config.p, last)) {
516*54fd6939SJiyong Park 			gpi_mask &= (gpi_mask >> ((15 -
517*54fd6939SJiyong Park 				    GPT_L1_GPI_IDX(gpt_config.p, last)) << 2));
518*54fd6939SJiyong Park 		}
519*54fd6939SJiyong Park 
520*54fd6939SJiyong Park 		/* Write GPI values. */
521*54fd6939SJiyong Park 		assert((l1[i] & gpi_mask) ==
522*54fd6939SJiyong Park 		       (GPT_BUILD_L1_DESC(GPT_GPI_ANY) & gpi_mask));
523*54fd6939SJiyong Park 		l1[i] = (l1[i] & ~gpi_mask) | (gpi_mask & gpi_field);
524*54fd6939SJiyong Park 
525*54fd6939SJiyong Park 		/* Reset mask. */
526*54fd6939SJiyong Park 		gpi_mask = 0xFFFFFFFFFFFFFFFF;
527*54fd6939SJiyong Park 	}
528*54fd6939SJiyong Park }
529*54fd6939SJiyong Park 
530*54fd6939SJiyong Park /*
531*54fd6939SJiyong Park  * This function finds the next available unused L1 table and initializes all
532*54fd6939SJiyong Park  * granules descriptor entries to GPI_ANY. This ensures that there are no chunks
533*54fd6939SJiyong Park  * of GPI_NO_ACCESS (0b0000) memory floating around in the system in the
534*54fd6939SJiyong Park  * event that a PAS region stops midway through an L1 table, thus guaranteeing
535*54fd6939SJiyong Park  * that all memory not explicitly assigned is GPI_ANY. This function does not
536*54fd6939SJiyong Park  * check for overflow conditions, that should be done by the caller.
537*54fd6939SJiyong Park  *
538*54fd6939SJiyong Park  * Return
539*54fd6939SJiyong Park  *   Pointer to the next available L1 table.
540*54fd6939SJiyong Park  */
gpt_get_new_l1_tbl(void)541*54fd6939SJiyong Park static uint64_t *gpt_get_new_l1_tbl(void)
542*54fd6939SJiyong Park {
543*54fd6939SJiyong Park 	/* Retrieve the next L1 table. */
544*54fd6939SJiyong Park 	uint64_t *l1 = (uint64_t *)((uint64_t)(gpt_l1_tbl) +
545*54fd6939SJiyong Park 		       (GPT_L1_TABLE_SIZE(gpt_config.p) *
546*54fd6939SJiyong Park 		       gpt_next_l1_tbl_idx));
547*54fd6939SJiyong Park 
548*54fd6939SJiyong Park 	/* Increment L1 counter. */
549*54fd6939SJiyong Park 	gpt_next_l1_tbl_idx++;
550*54fd6939SJiyong Park 
551*54fd6939SJiyong Park 	/* Initialize all GPIs to GPT_GPI_ANY */
552*54fd6939SJiyong Park 	for (unsigned int i = 0U; i < GPT_L1_ENTRY_COUNT(gpt_config.p); i++) {
553*54fd6939SJiyong Park 		l1[i] = GPT_BUILD_L1_DESC(GPT_GPI_ANY);
554*54fd6939SJiyong Park 	}
555*54fd6939SJiyong Park 
556*54fd6939SJiyong Park 	return l1;
557*54fd6939SJiyong Park }
558*54fd6939SJiyong Park 
559*54fd6939SJiyong Park /*
560*54fd6939SJiyong Park  * When L1 tables are needed, this function creates the necessary L0 table
561*54fd6939SJiyong Park  * descriptors and fills out the L1 table entries according to the supplied
562*54fd6939SJiyong Park  * PAS range.
563*54fd6939SJiyong Park  *
564*54fd6939SJiyong Park  * Parameters
565*54fd6939SJiyong Park  *   *pas		Pointer to the structure defining the PAS region.
566*54fd6939SJiyong Park  */
gpt_generate_l0_tbl_desc(pas_region_t * pas)567*54fd6939SJiyong Park static void gpt_generate_l0_tbl_desc(pas_region_t *pas)
568*54fd6939SJiyong Park {
569*54fd6939SJiyong Park 	uintptr_t end_pa;
570*54fd6939SJiyong Park 	uintptr_t cur_pa;
571*54fd6939SJiyong Park 	uintptr_t last_gran_pa;
572*54fd6939SJiyong Park 	uint64_t *l0_gpt_base;
573*54fd6939SJiyong Park 	uint64_t *l1_gpt_arr;
574*54fd6939SJiyong Park 	unsigned int l0_idx;
575*54fd6939SJiyong Park 
576*54fd6939SJiyong Park 	assert(gpt_config.plat_gpt_l0_base != 0U);
577*54fd6939SJiyong Park 	assert(pas != NULL);
578*54fd6939SJiyong Park 
579*54fd6939SJiyong Park 	/*
580*54fd6939SJiyong Park 	 * Checking of PAS parameters has already been done in
581*54fd6939SJiyong Park 	 * gpt_validate_pas_mappings so no need to check the same things again.
582*54fd6939SJiyong Park 	 */
583*54fd6939SJiyong Park 
584*54fd6939SJiyong Park 	end_pa = pas->base_pa + pas->size;
585*54fd6939SJiyong Park 	l0_gpt_base = (uint64_t *)gpt_config.plat_gpt_l0_base;
586*54fd6939SJiyong Park 
587*54fd6939SJiyong Park 	/* We start working from the granule at base PA */
588*54fd6939SJiyong Park 	cur_pa = pas->base_pa;
589*54fd6939SJiyong Park 
590*54fd6939SJiyong Park 	/* Iterate over each L0 region in this memory range. */
591*54fd6939SJiyong Park 	for (l0_idx = GPT_L0_IDX(pas->base_pa);
592*54fd6939SJiyong Park 	     l0_idx <= GPT_L0_IDX(end_pa - 1U);
593*54fd6939SJiyong Park 	     l0_idx++) {
594*54fd6939SJiyong Park 
595*54fd6939SJiyong Park 		/*
596*54fd6939SJiyong Park 		 * See if the L0 entry is already a table descriptor or if we
597*54fd6939SJiyong Park 		 * need to create one.
598*54fd6939SJiyong Park 		 */
599*54fd6939SJiyong Park 		if (GPT_L0_TYPE(l0_gpt_base[l0_idx]) == GPT_L0_TYPE_TBL_DESC) {
600*54fd6939SJiyong Park 			/* Get the L1 array from the L0 entry. */
601*54fd6939SJiyong Park 			l1_gpt_arr = GPT_L0_TBLD_ADDR(l0_gpt_base[l0_idx]);
602*54fd6939SJiyong Park 		} else {
603*54fd6939SJiyong Park 			/* Get a new L1 table from the L1 memory space. */
604*54fd6939SJiyong Park 			l1_gpt_arr = gpt_get_new_l1_tbl();
605*54fd6939SJiyong Park 
606*54fd6939SJiyong Park 			/* Fill out the L0 descriptor and flush it. */
607*54fd6939SJiyong Park 			l0_gpt_base[l0_idx] = GPT_L0_TBL_DESC(l1_gpt_arr);
608*54fd6939SJiyong Park 		}
609*54fd6939SJiyong Park 
610*54fd6939SJiyong Park 		VERBOSE("[GPT] L0 entry (TABLE) index %u [%p] ==> L1 Addr 0x%llx (0x%" PRIx64 ")\n",
611*54fd6939SJiyong Park 			l0_idx, &l0_gpt_base[l0_idx],
612*54fd6939SJiyong Park 			(unsigned long long)(l1_gpt_arr),
613*54fd6939SJiyong Park 			l0_gpt_base[l0_idx]);
614*54fd6939SJiyong Park 
615*54fd6939SJiyong Park 		/*
616*54fd6939SJiyong Park 		 * Determine the PA of the last granule in this L0 descriptor.
617*54fd6939SJiyong Park 		 */
618*54fd6939SJiyong Park 		last_gran_pa = gpt_get_l1_end_pa(cur_pa, end_pa) -
619*54fd6939SJiyong Park 			       GPT_PGS_ACTUAL_SIZE(gpt_config.p);
620*54fd6939SJiyong Park 
621*54fd6939SJiyong Park 		/*
622*54fd6939SJiyong Park 		 * Fill up L1 GPT entries between these two addresses. This
623*54fd6939SJiyong Park 		 * function needs the addresses of the first granule and last
624*54fd6939SJiyong Park 		 * granule in the range.
625*54fd6939SJiyong Park 		 */
626*54fd6939SJiyong Park 		gpt_fill_l1_tbl(GPT_PAS_ATTR_GPI(pas->attrs), l1_gpt_arr,
627*54fd6939SJiyong Park 				cur_pa, last_gran_pa);
628*54fd6939SJiyong Park 
629*54fd6939SJiyong Park 		/* Advance cur_pa to first granule in next L0 region. */
630*54fd6939SJiyong Park 		cur_pa = gpt_get_l1_end_pa(cur_pa, end_pa);
631*54fd6939SJiyong Park 	}
632*54fd6939SJiyong Park }
633*54fd6939SJiyong Park 
634*54fd6939SJiyong Park /*
635*54fd6939SJiyong Park  * This function flushes a range of L0 descriptors used by a given PAS region
636*54fd6939SJiyong Park  * array. There is a chance that some unmodified L0 descriptors would be flushed
637*54fd6939SJiyong Park  * in the case that there are "holes" in an array of PAS regions but overall
638*54fd6939SJiyong Park  * this should be faster than individually flushing each modified L0 descriptor
639*54fd6939SJiyong Park  * as they are created.
640*54fd6939SJiyong Park  *
641*54fd6939SJiyong Park  * Parameters
642*54fd6939SJiyong Park  *   *pas		Pointer to an array of PAS regions.
643*54fd6939SJiyong Park  *   pas_count		Number of entries in the PAS array.
644*54fd6939SJiyong Park  */
flush_l0_for_pas_array(pas_region_t * pas,unsigned int pas_count)645*54fd6939SJiyong Park static void flush_l0_for_pas_array(pas_region_t *pas, unsigned int pas_count)
646*54fd6939SJiyong Park {
647*54fd6939SJiyong Park 	unsigned int idx;
648*54fd6939SJiyong Park 	unsigned int start_idx;
649*54fd6939SJiyong Park 	unsigned int end_idx;
650*54fd6939SJiyong Park 	uint64_t *l0 = (uint64_t *)gpt_config.plat_gpt_l0_base;
651*54fd6939SJiyong Park 
652*54fd6939SJiyong Park 	assert(pas != NULL);
653*54fd6939SJiyong Park 	assert(pas_count > 0);
654*54fd6939SJiyong Park 
655*54fd6939SJiyong Park 	/* Initial start and end values. */
656*54fd6939SJiyong Park 	start_idx = GPT_L0_IDX(pas[0].base_pa);
657*54fd6939SJiyong Park 	end_idx = GPT_L0_IDX(pas[0].base_pa + pas[0].size - 1);
658*54fd6939SJiyong Park 
659*54fd6939SJiyong Park 	/* Find lowest and highest L0 indices used in this PAS array. */
660*54fd6939SJiyong Park 	for (idx = 1; idx < pas_count; idx++) {
661*54fd6939SJiyong Park 		if (GPT_L0_IDX(pas[idx].base_pa) < start_idx) {
662*54fd6939SJiyong Park 			start_idx = GPT_L0_IDX(pas[idx].base_pa);
663*54fd6939SJiyong Park 		}
664*54fd6939SJiyong Park 		if (GPT_L0_IDX(pas[idx].base_pa + pas[idx].size - 1) > end_idx) {
665*54fd6939SJiyong Park 			end_idx = GPT_L0_IDX(pas[idx].base_pa + pas[idx].size - 1);
666*54fd6939SJiyong Park 		}
667*54fd6939SJiyong Park 	}
668*54fd6939SJiyong Park 
669*54fd6939SJiyong Park 	/*
670*54fd6939SJiyong Park 	 * Flush all covered L0 descriptors, add 1 because we need to include
671*54fd6939SJiyong Park 	 * the end index value.
672*54fd6939SJiyong Park 	 */
673*54fd6939SJiyong Park 	flush_dcache_range((uintptr_t)&l0[start_idx],
674*54fd6939SJiyong Park 			   ((end_idx + 1) - start_idx) * sizeof(uint64_t));
675*54fd6939SJiyong Park }
676*54fd6939SJiyong Park 
677*54fd6939SJiyong Park /*
678*54fd6939SJiyong Park  * Public API to enable granule protection checks once the tables have all been
679*54fd6939SJiyong Park  * initialized. This function is called at first initialization and then again
680*54fd6939SJiyong Park  * later during warm boots of CPU cores.
681*54fd6939SJiyong Park  *
682*54fd6939SJiyong Park  * Return
683*54fd6939SJiyong Park  *   Negative Linux error code in the event of a failure, 0 for success.
684*54fd6939SJiyong Park  */
gpt_enable(void)685*54fd6939SJiyong Park int gpt_enable(void)
686*54fd6939SJiyong Park {
687*54fd6939SJiyong Park 	u_register_t gpccr_el3;
688*54fd6939SJiyong Park 
689*54fd6939SJiyong Park 	/*
690*54fd6939SJiyong Park 	 * Granule tables must be initialised before enabling
691*54fd6939SJiyong Park 	 * granule protection.
692*54fd6939SJiyong Park 	 */
693*54fd6939SJiyong Park 	if (gpt_config.plat_gpt_l0_base == 0U) {
694*54fd6939SJiyong Park 		ERROR("[GPT] Tables have not been initialized!\n");
695*54fd6939SJiyong Park 		return -EPERM;
696*54fd6939SJiyong Park 	}
697*54fd6939SJiyong Park 
698*54fd6939SJiyong Park 	/* Invalidate any stale TLB entries */
699*54fd6939SJiyong Park 	tlbipaallos();
700*54fd6939SJiyong Park 	dsb();
701*54fd6939SJiyong Park 
702*54fd6939SJiyong Park 	/* Write the base address of the L0 tables into GPTBR */
703*54fd6939SJiyong Park 	write_gptbr_el3(((gpt_config.plat_gpt_l0_base >> GPTBR_BADDR_VAL_SHIFT)
704*54fd6939SJiyong Park 			>> GPTBR_BADDR_SHIFT) & GPTBR_BADDR_MASK);
705*54fd6939SJiyong Park 
706*54fd6939SJiyong Park 	/* GPCCR_EL3.PPS */
707*54fd6939SJiyong Park 	gpccr_el3 = SET_GPCCR_PPS(gpt_config.pps);
708*54fd6939SJiyong Park 
709*54fd6939SJiyong Park 	/* GPCCR_EL3.PGS */
710*54fd6939SJiyong Park 	gpccr_el3 |= SET_GPCCR_PGS(gpt_config.pgs);
711*54fd6939SJiyong Park 
712*54fd6939SJiyong Park 	/*
713*54fd6939SJiyong Park 	 * Since EL3 maps the L1 region as Inner shareable, use the same
714*54fd6939SJiyong Park 	 * shareability attribute for GPC as well so that
715*54fd6939SJiyong Park 	 * GPC fetches are visible to PEs
716*54fd6939SJiyong Park 	 */
717*54fd6939SJiyong Park 	gpccr_el3 |= SET_GPCCR_SH(GPCCR_SH_IS);
718*54fd6939SJiyong Park 
719*54fd6939SJiyong Park 	/* Outer and Inner cacheability set to Normal memory, WB, RA, WA. */
720*54fd6939SJiyong Park 	gpccr_el3 |= SET_GPCCR_ORGN(GPCCR_ORGN_WB_RA_WA);
721*54fd6939SJiyong Park 	gpccr_el3 |= SET_GPCCR_IRGN(GPCCR_IRGN_WB_RA_WA);
722*54fd6939SJiyong Park 
723*54fd6939SJiyong Park 	/* Enable GPT */
724*54fd6939SJiyong Park 	gpccr_el3 |= GPCCR_GPC_BIT;
725*54fd6939SJiyong Park 
726*54fd6939SJiyong Park 	/* TODO: Configure GPCCR_EL3_GPCP for Fault control. */
727*54fd6939SJiyong Park 	write_gpccr_el3(gpccr_el3);
728*54fd6939SJiyong Park 	isb();
729*54fd6939SJiyong Park 	tlbipaallos();
730*54fd6939SJiyong Park 	dsb();
731*54fd6939SJiyong Park 	isb();
732*54fd6939SJiyong Park 
733*54fd6939SJiyong Park 	return 0;
734*54fd6939SJiyong Park }
735*54fd6939SJiyong Park 
736*54fd6939SJiyong Park /*
737*54fd6939SJiyong Park  * Public API to disable granule protection checks.
738*54fd6939SJiyong Park  */
gpt_disable(void)739*54fd6939SJiyong Park void gpt_disable(void)
740*54fd6939SJiyong Park {
741*54fd6939SJiyong Park 	u_register_t gpccr_el3 = read_gpccr_el3();
742*54fd6939SJiyong Park 
743*54fd6939SJiyong Park 	write_gpccr_el3(gpccr_el3 & ~GPCCR_GPC_BIT);
744*54fd6939SJiyong Park 	dsbsy();
745*54fd6939SJiyong Park 	isb();
746*54fd6939SJiyong Park }
747*54fd6939SJiyong Park 
748*54fd6939SJiyong Park /*
749*54fd6939SJiyong Park  * Public API that initializes the entire protected space to GPT_GPI_ANY using
750*54fd6939SJiyong Park  * the L0 tables (block descriptors). Ideally, this function is invoked prior
751*54fd6939SJiyong Park  * to DDR discovery and initialization. The MMU must be initialized before
752*54fd6939SJiyong Park  * calling this function.
753*54fd6939SJiyong Park  *
754*54fd6939SJiyong Park  * Parameters
755*54fd6939SJiyong Park  *   pps		PPS value to use for table generation
756*54fd6939SJiyong Park  *   l0_mem_base	Base address of L0 tables in memory.
757*54fd6939SJiyong Park  *   l0_mem_size	Total size of memory available for L0 tables.
758*54fd6939SJiyong Park  *
759*54fd6939SJiyong Park  * Return
760*54fd6939SJiyong Park  *   Negative Linux error code in the event of a failure, 0 for success.
761*54fd6939SJiyong Park  */
gpt_init_l0_tables(unsigned int pps,uintptr_t l0_mem_base,size_t l0_mem_size)762*54fd6939SJiyong Park int gpt_init_l0_tables(unsigned int pps, uintptr_t l0_mem_base,
763*54fd6939SJiyong Park 		       size_t l0_mem_size)
764*54fd6939SJiyong Park {
765*54fd6939SJiyong Park 	int ret;
766*54fd6939SJiyong Park 	uint64_t gpt_desc;
767*54fd6939SJiyong Park 
768*54fd6939SJiyong Park 	/* Ensure that MMU and Data caches are enabled. */
769*54fd6939SJiyong Park 	assert((read_sctlr_el3() & SCTLR_C_BIT) != 0U);
770*54fd6939SJiyong Park 
771*54fd6939SJiyong Park 	/* Validate other parameters. */
772*54fd6939SJiyong Park 	ret = gpt_validate_l0_params(pps, l0_mem_base, l0_mem_size);
773*54fd6939SJiyong Park 	if (ret < 0) {
774*54fd6939SJiyong Park 		return ret;
775*54fd6939SJiyong Park 	}
776*54fd6939SJiyong Park 
777*54fd6939SJiyong Park 	/* Create the descriptor to initialize L0 entries with. */
778*54fd6939SJiyong Park 	gpt_desc = GPT_L0_BLK_DESC(GPT_GPI_ANY);
779*54fd6939SJiyong Park 
780*54fd6939SJiyong Park 	/* Iterate through all L0 entries */
781*54fd6939SJiyong Park 	for (unsigned int i = 0U; i < GPT_L0_REGION_COUNT(gpt_config.t); i++) {
782*54fd6939SJiyong Park 		((uint64_t *)l0_mem_base)[i] = gpt_desc;
783*54fd6939SJiyong Park 	}
784*54fd6939SJiyong Park 
785*54fd6939SJiyong Park 	/* Flush updated L0 tables to memory. */
786*54fd6939SJiyong Park 	flush_dcache_range((uintptr_t)l0_mem_base,
787*54fd6939SJiyong Park 			   (size_t)GPT_L0_TABLE_SIZE(gpt_config.t));
788*54fd6939SJiyong Park 
789*54fd6939SJiyong Park 	/* Stash the L0 base address once initial setup is complete. */
790*54fd6939SJiyong Park 	gpt_config.plat_gpt_l0_base = l0_mem_base;
791*54fd6939SJiyong Park 
792*54fd6939SJiyong Park 	return 0;
793*54fd6939SJiyong Park }
794*54fd6939SJiyong Park 
795*54fd6939SJiyong Park /*
796*54fd6939SJiyong Park  * Public API that carves out PAS regions from the L0 tables and builds any L1
797*54fd6939SJiyong Park  * tables that are needed. This function ideally is run after DDR discovery and
798*54fd6939SJiyong Park  * initialization. The L0 tables must have already been initialized to GPI_ANY
799*54fd6939SJiyong Park  * when this function is called.
800*54fd6939SJiyong Park  *
801*54fd6939SJiyong Park  * This function can be called multiple times with different L1 memory ranges
802*54fd6939SJiyong Park  * and PAS regions if it is desirable to place L1 tables in different locations
803*54fd6939SJiyong Park  * in memory. (ex: you have multiple DDR banks and want to place the L1 tables
804*54fd6939SJiyong Park  * in the DDR bank that they control)
805*54fd6939SJiyong Park  *
806*54fd6939SJiyong Park  * Parameters
807*54fd6939SJiyong Park  *   pgs		PGS value to use for table generation.
808*54fd6939SJiyong Park  *   l1_mem_base	Base address of memory used for L1 tables.
809*54fd6939SJiyong Park  *   l1_mem_size	Total size of memory available for L1 tables.
810*54fd6939SJiyong Park  *   *pas_regions	Pointer to PAS regions structure array.
811*54fd6939SJiyong Park  *   pas_count		Total number of PAS regions.
812*54fd6939SJiyong Park  *
813*54fd6939SJiyong Park  * Return
814*54fd6939SJiyong Park  *   Negative Linux error code in the event of a failure, 0 for success.
815*54fd6939SJiyong Park  */
gpt_init_pas_l1_tables(gpccr_pgs_e pgs,uintptr_t l1_mem_base,size_t l1_mem_size,pas_region_t * pas_regions,unsigned int pas_count)816*54fd6939SJiyong Park int gpt_init_pas_l1_tables(gpccr_pgs_e pgs, uintptr_t l1_mem_base,
817*54fd6939SJiyong Park 			   size_t l1_mem_size, pas_region_t *pas_regions,
818*54fd6939SJiyong Park 			   unsigned int pas_count)
819*54fd6939SJiyong Park {
820*54fd6939SJiyong Park 	int ret;
821*54fd6939SJiyong Park 	int l1_gpt_cnt;
822*54fd6939SJiyong Park 
823*54fd6939SJiyong Park 	/* Ensure that MMU and Data caches are enabled. */
824*54fd6939SJiyong Park 	assert((read_sctlr_el3() & SCTLR_C_BIT) != 0U);
825*54fd6939SJiyong Park 
826*54fd6939SJiyong Park 	/* PGS is needed for gpt_validate_pas_mappings so check it now. */
827*54fd6939SJiyong Park 	if (pgs > GPT_PGS_MAX) {
828*54fd6939SJiyong Park 		ERROR("[GPT] Invalid PGS: 0x%x\n", pgs);
829*54fd6939SJiyong Park 		return -EINVAL;
830*54fd6939SJiyong Park 	}
831*54fd6939SJiyong Park 	gpt_config.pgs = pgs;
832*54fd6939SJiyong Park 	gpt_config.p = gpt_p_lookup[pgs];
833*54fd6939SJiyong Park 
834*54fd6939SJiyong Park 	/* Make sure L0 tables have been initialized. */
835*54fd6939SJiyong Park 	if (gpt_config.plat_gpt_l0_base == 0U) {
836*54fd6939SJiyong Park 		ERROR("[GPT] L0 tables must be initialized first!\n");
837*54fd6939SJiyong Park 		return -EPERM;
838*54fd6939SJiyong Park 	}
839*54fd6939SJiyong Park 
840*54fd6939SJiyong Park 	/* Check if L1 GPTs are required and how many. */
841*54fd6939SJiyong Park 	l1_gpt_cnt = gpt_validate_pas_mappings(pas_regions, pas_count);
842*54fd6939SJiyong Park 	if (l1_gpt_cnt < 0) {
843*54fd6939SJiyong Park 		return l1_gpt_cnt;
844*54fd6939SJiyong Park 	}
845*54fd6939SJiyong Park 
846*54fd6939SJiyong Park 	VERBOSE("[GPT] %u L1 GPTs requested.\n", l1_gpt_cnt);
847*54fd6939SJiyong Park 
848*54fd6939SJiyong Park 	/* If L1 tables are needed then validate the L1 parameters. */
849*54fd6939SJiyong Park 	if (l1_gpt_cnt > 0) {
850*54fd6939SJiyong Park 		ret = gpt_validate_l1_params(l1_mem_base, l1_mem_size,
851*54fd6939SJiyong Park 		      l1_gpt_cnt);
852*54fd6939SJiyong Park 		if (ret < 0) {
853*54fd6939SJiyong Park 			return ret;
854*54fd6939SJiyong Park 		}
855*54fd6939SJiyong Park 
856*54fd6939SJiyong Park 		/* Set up parameters for L1 table generation. */
857*54fd6939SJiyong Park 		gpt_l1_tbl = l1_mem_base;
858*54fd6939SJiyong Park 		gpt_next_l1_tbl_idx = 0U;
859*54fd6939SJiyong Park 	}
860*54fd6939SJiyong Park 
861*54fd6939SJiyong Park 	INFO("[GPT] Boot Configuration\n");
862*54fd6939SJiyong Park 	INFO("  PPS/T:     0x%x/%u\n", gpt_config.pps, gpt_config.t);
863*54fd6939SJiyong Park 	INFO("  PGS/P:     0x%x/%u\n", gpt_config.pgs, gpt_config.p);
864*54fd6939SJiyong Park 	INFO("  L0GPTSZ/S: 0x%x/%u\n", GPT_L0GPTSZ, GPT_S_VAL);
865*54fd6939SJiyong Park 	INFO("  PAS count: 0x%x\n", pas_count);
866*54fd6939SJiyong Park 	INFO("  L0 base:   0x%lx\n", gpt_config.plat_gpt_l0_base);
867*54fd6939SJiyong Park 
868*54fd6939SJiyong Park 	/* Generate the tables in memory. */
869*54fd6939SJiyong Park 	for (unsigned int idx = 0U; idx < pas_count; idx++) {
870*54fd6939SJiyong Park 		INFO("[GPT] PAS[%u]: base 0x%lx, size 0x%lx, GPI 0x%x, type 0x%x\n",
871*54fd6939SJiyong Park 		     idx, pas_regions[idx].base_pa, pas_regions[idx].size,
872*54fd6939SJiyong Park 		     GPT_PAS_ATTR_GPI(pas_regions[idx].attrs),
873*54fd6939SJiyong Park 		     GPT_PAS_ATTR_MAP_TYPE(pas_regions[idx].attrs));
874*54fd6939SJiyong Park 
875*54fd6939SJiyong Park 		/* Check if a block or table descriptor is required */
876*54fd6939SJiyong Park 		if (GPT_PAS_ATTR_MAP_TYPE(pas_regions[idx].attrs) ==
877*54fd6939SJiyong Park 		    GPT_PAS_ATTR_MAP_TYPE_BLOCK) {
878*54fd6939SJiyong Park 			gpt_generate_l0_blk_desc(&pas_regions[idx]);
879*54fd6939SJiyong Park 
880*54fd6939SJiyong Park 		} else {
881*54fd6939SJiyong Park 			gpt_generate_l0_tbl_desc(&pas_regions[idx]);
882*54fd6939SJiyong Park 		}
883*54fd6939SJiyong Park 	}
884*54fd6939SJiyong Park 
885*54fd6939SJiyong Park 	/* Flush modified L0 tables. */
886*54fd6939SJiyong Park 	flush_l0_for_pas_array(pas_regions, pas_count);
887*54fd6939SJiyong Park 
888*54fd6939SJiyong Park 	/* Flush L1 tables if needed. */
889*54fd6939SJiyong Park 	if (l1_gpt_cnt > 0) {
890*54fd6939SJiyong Park 		flush_dcache_range(l1_mem_base,
891*54fd6939SJiyong Park 				   GPT_L1_TABLE_SIZE(gpt_config.p) *
892*54fd6939SJiyong Park 				   l1_gpt_cnt);
893*54fd6939SJiyong Park 	}
894*54fd6939SJiyong Park 
895*54fd6939SJiyong Park 	/* Make sure that all the entries are written to the memory. */
896*54fd6939SJiyong Park 	dsbishst();
897*54fd6939SJiyong Park 	tlbipaallos();
898*54fd6939SJiyong Park 	dsb();
899*54fd6939SJiyong Park 	isb();
900*54fd6939SJiyong Park 
901*54fd6939SJiyong Park 	return 0;
902*54fd6939SJiyong Park }
903*54fd6939SJiyong Park 
904*54fd6939SJiyong Park /*
905*54fd6939SJiyong Park  * Public API to initialize the runtime gpt_config structure based on the values
906*54fd6939SJiyong Park  * present in the GPTBR_EL3 and GPCCR_EL3 registers. GPT initialization
907*54fd6939SJiyong Park  * typically happens in a bootloader stage prior to setting up the EL3 runtime
908*54fd6939SJiyong Park  * environment for the granule transition service so this function detects the
909*54fd6939SJiyong Park  * initialization from a previous stage. Granule protection checks must be
910*54fd6939SJiyong Park  * enabled already or this function will return an error.
911*54fd6939SJiyong Park  *
912*54fd6939SJiyong Park  * Return
913*54fd6939SJiyong Park  *   Negative Linux error code in the event of a failure, 0 for success.
914*54fd6939SJiyong Park  */
gpt_runtime_init(void)915*54fd6939SJiyong Park int gpt_runtime_init(void)
916*54fd6939SJiyong Park {
917*54fd6939SJiyong Park 	u_register_t reg;
918*54fd6939SJiyong Park 
919*54fd6939SJiyong Park 	/* Ensure that MMU and Data caches are enabled. */
920*54fd6939SJiyong Park 	assert((read_sctlr_el3() & SCTLR_C_BIT) != 0U);
921*54fd6939SJiyong Park 
922*54fd6939SJiyong Park 	/* Ensure GPC are already enabled. */
923*54fd6939SJiyong Park 	if ((read_gpccr_el3() & GPCCR_GPC_BIT) == 0U) {
924*54fd6939SJiyong Park 		ERROR("[GPT] Granule protection checks are not enabled!\n");
925*54fd6939SJiyong Park 		return -EPERM;
926*54fd6939SJiyong Park 	}
927*54fd6939SJiyong Park 
928*54fd6939SJiyong Park 	/*
929*54fd6939SJiyong Park 	 * Read the L0 table address from GPTBR, we don't need the L1 base
930*54fd6939SJiyong Park 	 * address since those are included in the L0 tables as needed.
931*54fd6939SJiyong Park 	 */
932*54fd6939SJiyong Park 	reg = read_gptbr_el3();
933*54fd6939SJiyong Park 	gpt_config.plat_gpt_l0_base = ((reg >> GPTBR_BADDR_SHIFT) &
934*54fd6939SJiyong Park 				      GPTBR_BADDR_MASK) <<
935*54fd6939SJiyong Park 				      GPTBR_BADDR_VAL_SHIFT;
936*54fd6939SJiyong Park 
937*54fd6939SJiyong Park 	/* Read GPCCR to get PGS and PPS values. */
938*54fd6939SJiyong Park 	reg = read_gpccr_el3();
939*54fd6939SJiyong Park 	gpt_config.pps = (reg >> GPCCR_PPS_SHIFT) & GPCCR_PPS_MASK;
940*54fd6939SJiyong Park 	gpt_config.t = gpt_t_lookup[gpt_config.pps];
941*54fd6939SJiyong Park 	gpt_config.pgs = (reg >> GPCCR_PGS_SHIFT) & GPCCR_PGS_MASK;
942*54fd6939SJiyong Park 	gpt_config.p = gpt_p_lookup[gpt_config.pgs];
943*54fd6939SJiyong Park 
944*54fd6939SJiyong Park 	VERBOSE("[GPT] Runtime Configuration\n");
945*54fd6939SJiyong Park 	VERBOSE("  PPS/T:     0x%x/%u\n", gpt_config.pps, gpt_config.t);
946*54fd6939SJiyong Park 	VERBOSE("  PGS/P:     0x%x/%u\n", gpt_config.pgs, gpt_config.p);
947*54fd6939SJiyong Park 	VERBOSE("  L0GPTSZ/S: 0x%x/%u\n", GPT_L0GPTSZ, GPT_S_VAL);
948*54fd6939SJiyong Park 	VERBOSE("  L0 base:   0x%lx\n", gpt_config.plat_gpt_l0_base);
949*54fd6939SJiyong Park 
950*54fd6939SJiyong Park 	return 0;
951*54fd6939SJiyong Park }
952*54fd6939SJiyong Park 
953*54fd6939SJiyong Park /*
954*54fd6939SJiyong Park  * The L1 descriptors are protected by a spinlock to ensure that multiple
955*54fd6939SJiyong Park  * CPUs do not attempt to change the descriptors at once. In the future it
956*54fd6939SJiyong Park  * would be better to have separate spinlocks for each L1 descriptor.
957*54fd6939SJiyong Park  */
958*54fd6939SJiyong Park static spinlock_t gpt_lock;
959*54fd6939SJiyong Park 
960*54fd6939SJiyong Park /*
961*54fd6939SJiyong Park  * Check if caller is allowed to transition a PAS.
962*54fd6939SJiyong Park  *
963*54fd6939SJiyong Park  * - Secure world caller can only request S <-> NS transitions on a
964*54fd6939SJiyong Park  *   granule that is already in either S or NS PAS.
965*54fd6939SJiyong Park  *
966*54fd6939SJiyong Park  * - Realm world caller can only request R <-> NS transitions on a
967*54fd6939SJiyong Park  *   granule that is already in either R or NS PAS.
968*54fd6939SJiyong Park  *
969*54fd6939SJiyong Park  * Parameters
970*54fd6939SJiyong Park  *   src_sec_state	Security state of the caller.
971*54fd6939SJiyong Park  *   current_gpi	Current GPI of the granule.
972*54fd6939SJiyong Park  *   target_gpi		Requested new GPI for the granule.
973*54fd6939SJiyong Park  *
974*54fd6939SJiyong Park  * Return
975*54fd6939SJiyong Park  *   Negative Linux error code in the event of a failure, 0 for success.
976*54fd6939SJiyong Park  */
gpt_check_transition_gpi(unsigned int src_sec_state,unsigned int current_gpi,unsigned int target_gpi)977*54fd6939SJiyong Park static int gpt_check_transition_gpi(unsigned int src_sec_state,
978*54fd6939SJiyong Park 				    unsigned int current_gpi,
979*54fd6939SJiyong Park 				    unsigned int target_gpi)
980*54fd6939SJiyong Park {
981*54fd6939SJiyong Park 	unsigned int check_gpi;
982*54fd6939SJiyong Park 
983*54fd6939SJiyong Park 	/* Cannot transition a granule to the state it is already in. */
984*54fd6939SJiyong Park 	if (current_gpi == target_gpi) {
985*54fd6939SJiyong Park 		return -EINVAL;
986*54fd6939SJiyong Park 	}
987*54fd6939SJiyong Park 
988*54fd6939SJiyong Park 	/* Check security state, only secure and realm can transition. */
989*54fd6939SJiyong Park 	if (src_sec_state == SMC_FROM_REALM) {
990*54fd6939SJiyong Park 		check_gpi = GPT_GPI_REALM;
991*54fd6939SJiyong Park 	} else if (src_sec_state == SMC_FROM_SECURE) {
992*54fd6939SJiyong Park 		check_gpi = GPT_GPI_SECURE;
993*54fd6939SJiyong Park 	} else {
994*54fd6939SJiyong Park 		return -EINVAL;
995*54fd6939SJiyong Park 	}
996*54fd6939SJiyong Park 
997*54fd6939SJiyong Park 	/* Make sure security state is allowed to make the transition. */
998*54fd6939SJiyong Park 	if ((target_gpi != check_gpi) && (target_gpi != GPT_GPI_NS)) {
999*54fd6939SJiyong Park 		return -EINVAL;
1000*54fd6939SJiyong Park 	}
1001*54fd6939SJiyong Park 	if ((current_gpi != check_gpi) && (current_gpi != GPT_GPI_NS)) {
1002*54fd6939SJiyong Park 		return -EINVAL;
1003*54fd6939SJiyong Park 	}
1004*54fd6939SJiyong Park 
1005*54fd6939SJiyong Park 	return 0;
1006*54fd6939SJiyong Park }
1007*54fd6939SJiyong Park 
1008*54fd6939SJiyong Park /*
1009*54fd6939SJiyong Park  * This function is the core of the granule transition service. When a granule
1010*54fd6939SJiyong Park  * transition request occurs it is routed to this function where the request is
1011*54fd6939SJiyong Park  * validated then fulfilled if possible.
1012*54fd6939SJiyong Park  *
1013*54fd6939SJiyong Park  * TODO: implement support for transitioning multiple granules at once.
1014*54fd6939SJiyong Park  *
1015*54fd6939SJiyong Park  * Parameters
1016*54fd6939SJiyong Park  *   base		Base address of the region to transition, must be
1017*54fd6939SJiyong Park  *			aligned to granule size.
1018*54fd6939SJiyong Park  *   size		Size of region to transition, must be aligned to granule
1019*54fd6939SJiyong Park  *			size.
1020*54fd6939SJiyong Park  *   src_sec_state	Security state of the caller.
1021*54fd6939SJiyong Park  *   target_pas		Target PAS of the specified memory region.
1022*54fd6939SJiyong Park  *
1023*54fd6939SJiyong Park  * Return
1024*54fd6939SJiyong Park  *    Negative Linux error code in the event of a failure, 0 for success.
1025*54fd6939SJiyong Park  */
gpt_transition_pas(uint64_t base,size_t size,unsigned int src_sec_state,unsigned int target_pas)1026*54fd6939SJiyong Park int gpt_transition_pas(uint64_t base, size_t size, unsigned int src_sec_state,
1027*54fd6939SJiyong Park 	unsigned int target_pas)
1028*54fd6939SJiyong Park {
1029*54fd6939SJiyong Park 	int idx;
1030*54fd6939SJiyong Park 	unsigned int gpi_shift;
1031*54fd6939SJiyong Park 	unsigned int gpi;
1032*54fd6939SJiyong Park 	uint64_t gpt_l0_desc;
1033*54fd6939SJiyong Park 	uint64_t gpt_l1_desc;
1034*54fd6939SJiyong Park 	uint64_t *gpt_l1_addr;
1035*54fd6939SJiyong Park 	uint64_t *gpt_l0_base;
1036*54fd6939SJiyong Park 
1037*54fd6939SJiyong Park 	/* Ensure that the tables have been set up before taking requests. */
1038*54fd6939SJiyong Park 	assert(gpt_config.plat_gpt_l0_base != 0U);
1039*54fd6939SJiyong Park 
1040*54fd6939SJiyong Park 	/* Ensure that MMU and data caches are enabled. */
1041*54fd6939SJiyong Park 	assert((read_sctlr_el3() & SCTLR_C_BIT) != 0U);
1042*54fd6939SJiyong Park 
1043*54fd6939SJiyong Park 	/* Check for address range overflow. */
1044*54fd6939SJiyong Park 	if ((ULONG_MAX - base) < size) {
1045*54fd6939SJiyong Park 		VERBOSE("[GPT] Transition request address overflow!\n");
1046*54fd6939SJiyong Park 		VERBOSE("      Base=0x%" PRIx64 "\n", base);
1047*54fd6939SJiyong Park 		VERBOSE("      Size=0x%lx\n", size);
1048*54fd6939SJiyong Park 		return -EINVAL;
1049*54fd6939SJiyong Park 	}
1050*54fd6939SJiyong Park 
1051*54fd6939SJiyong Park 	/* Make sure base and size are valid. */
1052*54fd6939SJiyong Park 	if (((base & (GPT_PGS_ACTUAL_SIZE(gpt_config.p) - 1)) != 0U) ||
1053*54fd6939SJiyong Park 	    ((size & (GPT_PGS_ACTUAL_SIZE(gpt_config.p) - 1)) != 0U) ||
1054*54fd6939SJiyong Park 	    (size == 0U) ||
1055*54fd6939SJiyong Park 	    ((base + size) >= GPT_PPS_ACTUAL_SIZE(gpt_config.t))) {
1056*54fd6939SJiyong Park 		VERBOSE("[GPT] Invalid granule transition address range!\n");
1057*54fd6939SJiyong Park 		VERBOSE("      Base=0x%" PRIx64 "\n", base);
1058*54fd6939SJiyong Park 		VERBOSE("      Size=0x%lx\n", size);
1059*54fd6939SJiyong Park 		return -EINVAL;
1060*54fd6939SJiyong Park 	}
1061*54fd6939SJiyong Park 
1062*54fd6939SJiyong Park 	/* See if this is a single granule transition or a range of granules. */
1063*54fd6939SJiyong Park 	if (size != GPT_PGS_ACTUAL_SIZE(gpt_config.p)) {
1064*54fd6939SJiyong Park 		/*
1065*54fd6939SJiyong Park 		 * TODO: Add support for transitioning multiple granules with a
1066*54fd6939SJiyong Park 		 * single call to this function.
1067*54fd6939SJiyong Park 		 */
1068*54fd6939SJiyong Park 		panic();
1069*54fd6939SJiyong Park 	}
1070*54fd6939SJiyong Park 
1071*54fd6939SJiyong Park 	/* Get the L0 descriptor and make sure it is for a table. */
1072*54fd6939SJiyong Park 	gpt_l0_base = (uint64_t *)gpt_config.plat_gpt_l0_base;
1073*54fd6939SJiyong Park 	gpt_l0_desc = gpt_l0_base[GPT_L0_IDX(base)];
1074*54fd6939SJiyong Park 	if (GPT_L0_TYPE(gpt_l0_desc) != GPT_L0_TYPE_TBL_DESC) {
1075*54fd6939SJiyong Park 		VERBOSE("[GPT] Granule is not covered by a table descriptor!\n");
1076*54fd6939SJiyong Park 		VERBOSE("      Base=0x%" PRIx64 "\n", base);
1077*54fd6939SJiyong Park 		return -EINVAL;
1078*54fd6939SJiyong Park 	}
1079*54fd6939SJiyong Park 
1080*54fd6939SJiyong Park 	/* Get the table index and GPI shift from PA. */
1081*54fd6939SJiyong Park 	gpt_l1_addr = GPT_L0_TBLD_ADDR(gpt_l0_desc);
1082*54fd6939SJiyong Park 	idx = GPT_L1_IDX(gpt_config.p, base);
1083*54fd6939SJiyong Park 	gpi_shift = GPT_L1_GPI_IDX(gpt_config.p, base) << 2;
1084*54fd6939SJiyong Park 
1085*54fd6939SJiyong Park 	/*
1086*54fd6939SJiyong Park 	 * Access to L1 tables is controlled by a global lock to ensure
1087*54fd6939SJiyong Park 	 * that no more than one CPU is allowed to make changes at any
1088*54fd6939SJiyong Park 	 * given time.
1089*54fd6939SJiyong Park 	 */
1090*54fd6939SJiyong Park 	spin_lock(&gpt_lock);
1091*54fd6939SJiyong Park 	gpt_l1_desc = gpt_l1_addr[idx];
1092*54fd6939SJiyong Park 	gpi = (gpt_l1_desc >> gpi_shift) & GPT_L1_GRAN_DESC_GPI_MASK;
1093*54fd6939SJiyong Park 
1094*54fd6939SJiyong Park 	/* Make sure caller state and source/target PAS are allowed. */
1095*54fd6939SJiyong Park 	if (gpt_check_transition_gpi(src_sec_state, gpi, target_pas) < 0) {
1096*54fd6939SJiyong Park 		spin_unlock(&gpt_lock);
1097*54fd6939SJiyong Park 			VERBOSE("[GPT] Invalid caller state and PAS combo!\n");
1098*54fd6939SJiyong Park 		VERBOSE("      Caller: %u, Current GPI: %u, Target GPI: %u\n",
1099*54fd6939SJiyong Park 			src_sec_state, gpi, target_pas);
1100*54fd6939SJiyong Park 		return -EPERM;
1101*54fd6939SJiyong Park 	}
1102*54fd6939SJiyong Park 
1103*54fd6939SJiyong Park 	/* Clear existing GPI encoding and transition granule. */
1104*54fd6939SJiyong Park 	gpt_l1_desc &= ~(GPT_L1_GRAN_DESC_GPI_MASK << gpi_shift);
1105*54fd6939SJiyong Park 	gpt_l1_desc |= ((uint64_t)target_pas << gpi_shift);
1106*54fd6939SJiyong Park 	gpt_l1_addr[idx] = gpt_l1_desc;
1107*54fd6939SJiyong Park 
1108*54fd6939SJiyong Park 	/* Ensure that the write operation will be observed by GPC */
1109*54fd6939SJiyong Park 	dsbishst();
1110*54fd6939SJiyong Park 
1111*54fd6939SJiyong Park 	/* Unlock access to the L1 tables. */
1112*54fd6939SJiyong Park 	spin_unlock(&gpt_lock);
1113*54fd6939SJiyong Park 
1114*54fd6939SJiyong Park 	gpt_tlbi_by_pa(base, GPT_PGS_ACTUAL_SIZE(gpt_config.p));
1115*54fd6939SJiyong Park 	dsbishst();
1116*54fd6939SJiyong Park 	/*
1117*54fd6939SJiyong Park 	 * The isb() will be done as part of context
1118*54fd6939SJiyong Park 	 * synchronization when returning to lower EL
1119*54fd6939SJiyong Park 	 */
1120*54fd6939SJiyong Park 	VERBOSE("[GPT] Granule 0x%" PRIx64 ", GPI 0x%x->0x%x\n", base, gpi,
1121*54fd6939SJiyong Park 		target_pas);
1122*54fd6939SJiyong Park 
1123*54fd6939SJiyong Park 	return 0;
1124*54fd6939SJiyong Park }
1125