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