blob: 1f90e64cfa33270f9d0935355cf4c390185f0011 [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
711 /* Set shareability attribute to Outher Shareable */
712 gpccr_el3 |= SET_GPCCR_SH(GPCCR_SH_OS);
713
714 /* Outer and Inner cacheability set to Normal memory, WB, RA, WA. */
715 gpccr_el3 |= SET_GPCCR_ORGN(GPCCR_ORGN_WB_RA_WA);
716 gpccr_el3 |= SET_GPCCR_IRGN(GPCCR_IRGN_WB_RA_WA);
717
718 /* Enable GPT */
719 gpccr_el3 |= GPCCR_GPC_BIT;
720
721 /* TODO: Configure GPCCR_EL3_GPCP for Fault control. */
722 write_gpccr_el3(gpccr_el3);
723 tlbipaallos();
724 dsb();
725 isb();
726
727 return 0;
728}
729
730/*
731 * Public API to disable granule protection checks.
732 */
733void gpt_disable(void)
734{
735 u_register_t gpccr_el3 = read_gpccr_el3();
736
737 write_gpccr_el3(gpccr_el3 & ~GPCCR_GPC_BIT);
738 dsbsy();
739 isb();
740}
741
742/*
743 * Public API that initializes the entire protected space to GPT_GPI_ANY using
744 * the L0 tables (block descriptors). Ideally, this function is invoked prior
745 * to DDR discovery and initialization. The MMU must be initialized before
746 * calling this function.
747 *
748 * Parameters
749 * pps PPS value to use for table generation
750 * l0_mem_base Base address of L0 tables in memory.
751 * l0_mem_size Total size of memory available for L0 tables.
752 *
753 * Return
754 * Negative Linux error code in the event of a failure, 0 for success.
755 */
756int gpt_init_l0_tables(unsigned int pps, uintptr_t l0_mem_base,
757 size_t l0_mem_size)
758{
759 int ret;
760 uint64_t gpt_desc;
761
762 /* Ensure that MMU and caches are enabled. */
763 assert((read_sctlr_el3() & SCTLR_C_BIT) != 0U);
764
765 /* Validate other parameters. */
766 ret = gpt_validate_l0_params(pps, l0_mem_base, l0_mem_size);
767 if (ret < 0) {
768 return ret;
769 }
770
771 /* Create the descriptor to initialize L0 entries with. */
772 gpt_desc = GPT_L0_BLK_DESC(GPT_GPI_ANY);
773
774 /* Iterate through all L0 entries */
775 for (unsigned int i = 0U; i < GPT_L0_REGION_COUNT(gpt_config.t); i++) {
776 ((uint64_t *)l0_mem_base)[i] = gpt_desc;
777 }
778
779 /* Flush updated L0 tables to memory. */
780 flush_dcache_range((uintptr_t)l0_mem_base,
781 (size_t)GPT_L0_TABLE_SIZE(gpt_config.t));
782
783 /* Stash the L0 base address once initial setup is complete. */
784 gpt_config.plat_gpt_l0_base = l0_mem_base;
785
786 return 0;
787}
788
789/*
790 * Public API that carves out PAS regions from the L0 tables and builds any L1
791 * tables that are needed. This function ideally is run after DDR discovery and
792 * initialization. The L0 tables must have already been initialized to GPI_ANY
793 * when this function is called.
794 *
795 * This function can be called multiple times with different L1 memory ranges
796 * and PAS regions if it is desirable to place L1 tables in different locations
797 * in memory. (ex: you have multiple DDR banks and want to place the L1 tables
798 * in the DDR bank that they control)
799 *
800 * Parameters
801 * pgs PGS value to use for table generation.
802 * l1_mem_base Base address of memory used for L1 tables.
803 * l1_mem_size Total size of memory available for L1 tables.
804 * *pas_regions Pointer to PAS regions structure array.
805 * pas_count Total number of PAS regions.
806 *
807 * Return
808 * Negative Linux error code in the event of a failure, 0 for success.
809 */
810int gpt_init_pas_l1_tables(gpccr_pgs_e pgs, uintptr_t l1_mem_base,
811 size_t l1_mem_size, pas_region_t *pas_regions,
812 unsigned int pas_count)
813{
814 int ret;
815 int l1_gpt_cnt;
816
817 /* Ensure that MMU and caches are enabled. */
818 assert((read_sctlr_el3() & SCTLR_C_BIT) != 0U);
819
820 /* PGS is needed for gpt_validate_pas_mappings so check it now. */
821 if (pgs > GPT_PGS_MAX) {
822 ERROR("[GPT] Invalid PGS: 0x%x\n", pgs);
823 return -EINVAL;
824 }
825 gpt_config.pgs = pgs;
826 gpt_config.p = gpt_p_lookup[pgs];
827
828 /* Make sure L0 tables have been initialized. */
829 if (gpt_config.plat_gpt_l0_base == 0U) {
830 ERROR("[GPT] L0 tables must be initialized first!\n");
831 return -EPERM;
832 }
833
834 /* Check if L1 GPTs are required and how many. */
835 l1_gpt_cnt = gpt_validate_pas_mappings(pas_regions, pas_count);
836 if (l1_gpt_cnt < 0) {
837 return l1_gpt_cnt;
838 }
839
840 VERBOSE("[GPT] %u L1 GPTs requested.\n", l1_gpt_cnt);
841
842 /* If L1 tables are needed then validate the L1 parameters. */
843 if (l1_gpt_cnt > 0) {
844 ret = gpt_validate_l1_params(l1_mem_base, l1_mem_size,
845 l1_gpt_cnt);
846 if (ret < 0) {
847 return ret;
848 }
849
850 /* Set up parameters for L1 table generation. */
851 gpt_l1_tbl = l1_mem_base;
852 gpt_next_l1_tbl_idx = 0U;
853 }
854
855 INFO("[GPT] Boot Configuration\n");
856 INFO(" PPS/T: 0x%x/%u\n", gpt_config.pps, gpt_config.t);
857 INFO(" PGS/P: 0x%x/%u\n", gpt_config.pgs, gpt_config.p);
858 INFO(" L0GPTSZ/S: 0x%x/%u\n", GPT_L0GPTSZ, GPT_S_VAL);
859 INFO(" PAS count: 0x%x\n", pas_count);
860 INFO(" L0 base: 0x%lx\n", gpt_config.plat_gpt_l0_base);
861
862 /* Generate the tables in memory. */
863 for (unsigned int idx = 0U; idx < pas_count; idx++) {
864 INFO("[GPT] PAS[%u]: base 0x%lx, size 0x%lx, GPI 0x%x, type 0x%x\n",
865 idx, pas_regions[idx].base_pa, pas_regions[idx].size,
866 GPT_PAS_ATTR_GPI(pas_regions[idx].attrs),
867 GPT_PAS_ATTR_MAP_TYPE(pas_regions[idx].attrs));
868
869 /* Check if a block or table descriptor is required */
870 if (GPT_PAS_ATTR_MAP_TYPE(pas_regions[idx].attrs) ==
871 GPT_PAS_ATTR_MAP_TYPE_BLOCK) {
872 gpt_generate_l0_blk_desc(&pas_regions[idx]);
873
874 } else {
875 gpt_generate_l0_tbl_desc(&pas_regions[idx]);
876 }
877 }
878
879 /* Flush modified L0 tables. */
880 flush_l0_for_pas_array(pas_regions, pas_count);
881
882 /* Flush L1 tables if needed. */
883 if (l1_gpt_cnt > 0) {
884 flush_dcache_range(l1_mem_base,
885 GPT_L1_TABLE_SIZE(gpt_config.p) *
886 l1_gpt_cnt);
887 }
888
889 /* Make sure that all the entries are written to the memory. */
890 dsbishst();
891
892 return 0;
893}
894
895/*
896 * Public API to initialize the runtime gpt_config structure based on the values
897 * present in the GPTBR_EL3 and GPCCR_EL3 registers. GPT initialization
898 * typically happens in a bootloader stage prior to setting up the EL3 runtime
899 * environment for the granule transition service so this function detects the
900 * initialization from a previous stage. Granule protection checks must be
901 * enabled already or this function will return an error.
902 *
903 * Return
904 * Negative Linux error code in the event of a failure, 0 for success.
905 */
906int gpt_runtime_init(void)
907{
908 u_register_t reg;
909
910 /* Ensure that MMU and caches are enabled. */
911 assert((read_sctlr_el3() & SCTLR_C_BIT) != 0U);
912
913 /* Ensure GPC are already enabled. */
914 if ((read_gpccr_el3() & GPCCR_GPC_BIT) == 0U) {
915 ERROR("[GPT] Granule protection checks are not enabled!\n");
916 return -EPERM;
917 }
918
919 /*
920 * Read the L0 table address from GPTBR, we don't need the L1 base
921 * address since those are included in the L0 tables as needed.
922 */
923 reg = read_gptbr_el3();
924 gpt_config.plat_gpt_l0_base = ((reg >> GPTBR_BADDR_SHIFT) &
925 GPTBR_BADDR_MASK) <<
926 GPTBR_BADDR_VAL_SHIFT;
927
928 /* Read GPCCR to get PGS and PPS values. */
929 reg = read_gpccr_el3();
930 gpt_config.pps = (reg >> GPCCR_PPS_SHIFT) & GPCCR_PPS_MASK;
931 gpt_config.t = gpt_t_lookup[gpt_config.pps];
932 gpt_config.pgs = (reg >> GPCCR_PGS_SHIFT) & GPCCR_PGS_MASK;
933 gpt_config.p = gpt_p_lookup[gpt_config.pgs];
934
935 VERBOSE("[GPT] Runtime Configuration\n");
936 VERBOSE(" PPS/T: 0x%x/%u\n", gpt_config.pps, gpt_config.t);
937 VERBOSE(" PGS/P: 0x%x/%u\n", gpt_config.pgs, gpt_config.p);
938 VERBOSE(" L0GPTSZ/S: 0x%x/%u\n", GPT_L0GPTSZ, GPT_S_VAL);
939 VERBOSE(" L0 base: 0x%lx\n", gpt_config.plat_gpt_l0_base);
940
941 return 0;
942}
943
944/*
945 * The L1 descriptors are protected by a spinlock to ensure that multiple
946 * CPUs do not attempt to change the descriptors at once. In the future it
947 * would be better to have separate spinlocks for each L1 descriptor.
948 */
949static spinlock_t gpt_lock;
950
951/*
952 * Check if caller is allowed to transition a PAS.
953 *
954 * - Secure world caller can only request S <-> NS transitions on a
955 * granule that is already in either S or NS PAS.
956 *
957 * - Realm world caller can only request R <-> NS transitions on a
958 * granule that is already in either R or NS PAS.
959 *
960 * Parameters
961 * src_sec_state Security state of the caller.
962 * current_gpi Current GPI of the granule.
963 * target_gpi Requested new GPI for the granule.
964 *
965 * Return
966 * Negative Linux error code in the event of a failure, 0 for success.
967 */
968static int gpt_check_transition_gpi(unsigned int src_sec_state,
969 unsigned int current_gpi,
970 unsigned int target_gpi)
971{
972 unsigned int check_gpi;
973
974 /* Cannot transition a granule to the state it is already in. */
975 if (current_gpi == target_gpi) {
976 return -EINVAL;
977 }
978
979 /* Check security state, only secure and realm can transition. */
980 if (src_sec_state == SMC_FROM_REALM) {
981 check_gpi = GPT_GPI_REALM;
982 } else if (src_sec_state == SMC_FROM_SECURE) {
983 check_gpi = GPT_GPI_SECURE;
984 } else {
985 return -EINVAL;
986 }
987
988 /* Make sure security state is allowed to make the transition. */
989 if ((target_gpi != check_gpi) && (target_gpi != GPT_GPI_NS)) {
990 return -EINVAL;
991 }
992 if ((current_gpi != check_gpi) && (current_gpi != GPT_GPI_NS)) {
993 return -EINVAL;
994 }
995
996 return 0;
997}
998
999/*
1000 * This function is the core of the granule transition service. When a granule
1001 * transition request occurs it is routed to this function where the request is
1002 * validated then fulfilled if possible.
1003 *
1004 * TODO: implement support for transitioning multiple granules at once.
1005 *
1006 * Parameters
1007 * base Base address of the region to transition, must be
1008 * aligned to granule size.
1009 * size Size of region to transition, must be aligned to granule
1010 * size.
1011 * src_sec_state Security state of the caller.
1012 * target_pas Target PAS of the specified memory region.
1013 *
1014 * Return
1015 * Negative Linux error code in the event of a failure, 0 for success.
1016 */
1017int gpt_transition_pas(uint64_t base, size_t size, unsigned int src_sec_state,
1018 unsigned int target_pas)
1019{
1020 int idx;
1021 unsigned int gpi_shift;
1022 unsigned int gpi;
1023 uint64_t gpt_l0_desc;
1024 uint64_t gpt_l1_desc;
1025 uint64_t *gpt_l1_addr;
1026 uint64_t *gpt_l0_base;
1027
1028 /* Ensure that the tables have been set up before taking requests. */
1029 assert(gpt_config.plat_gpt_l0_base != 0U);
1030
1031 /* Check for address range overflow. */
1032 if ((ULONG_MAX - base) < size) {
1033 VERBOSE("[GPT] Transition request address overflow!\n");
1034 VERBOSE(" Base=0x%llx\n", base);
1035 VERBOSE(" Size=0x%lx\n", size);
1036 return -EINVAL;
1037 }
1038
1039 /* Make sure base and size are valid. */
1040 if (((base & (GPT_PGS_ACTUAL_SIZE(gpt_config.p) - 1)) != 0U) ||
1041 ((size & (GPT_PGS_ACTUAL_SIZE(gpt_config.p) - 1)) != 0U) ||
1042 (size == 0U) ||
1043 ((base + size) >= GPT_PPS_ACTUAL_SIZE(gpt_config.t))) {
1044 VERBOSE("[GPT] Invalid granule transition address range!\n");
1045 VERBOSE(" Base=0x%llx\n", base);
1046 VERBOSE(" Size=0x%lx\n", size);
1047 return -EINVAL;
1048 }
1049
1050 /* See if this is a single granule transition or a range of granules. */
1051 if (size != GPT_PGS_ACTUAL_SIZE(gpt_config.p)) {
1052 /*
1053 * TODO: Add support for transitioning multiple granules with a
1054 * single call to this function.
1055 */
1056 panic();
1057 }
1058
1059 /* Get the L0 descriptor and make sure it is for a table. */
1060 gpt_l0_base = (uint64_t *)gpt_config.plat_gpt_l0_base;
1061 gpt_l0_desc = gpt_l0_base[GPT_L0_IDX(base)];
1062 if (GPT_L0_TYPE(gpt_l0_desc) != GPT_L0_TYPE_TBL_DESC) {
1063 VERBOSE("[GPT] Granule is not covered by a table descriptor!\n");
1064 VERBOSE(" Base=0x%llx\n", base);
1065 return -EINVAL;
1066 }
1067
1068 /* Get the table index and GPI shift from PA. */
1069 gpt_l1_addr = GPT_L0_TBLD_ADDR(gpt_l0_desc);
1070 idx = GPT_L1_IDX(gpt_config.p, base);
1071 gpi_shift = GPT_L1_GPI_IDX(gpt_config.p, base) << 2;
1072
1073 /*
1074 * Access to L1 tables is controlled by a global lock to ensure
1075 * that no more than one CPU is allowed to make changes at any
1076 * given time.
1077 */
1078 spin_lock(&gpt_lock);
1079 gpt_l1_desc = gpt_l1_addr[idx];
1080 gpi = (gpt_l1_desc >> gpi_shift) & GPT_L1_GRAN_DESC_GPI_MASK;
1081
1082 /* Make sure caller state and source/target PAS are allowed. */
1083 if (gpt_check_transition_gpi(src_sec_state, gpi, target_pas) < 0) {
1084 spin_unlock(&gpt_lock);
1085 VERBOSE("[GPT] Invalid caller state and PAS combo!\n");
1086 VERBOSE(" Caller: %u, Current GPI: %u, Target GPI: %u\n",
1087 src_sec_state, gpi, target_pas);
1088 return -EPERM;
1089 }
1090
1091 /* Clear existing GPI encoding and transition granule. */
1092 gpt_l1_desc &= ~(GPT_L1_GRAN_DESC_GPI_MASK << gpi_shift);
1093 gpt_l1_desc |= ((uint64_t)target_pas << gpi_shift);
1094 gpt_l1_addr[idx] = gpt_l1_desc;
1095
1096 /* Ensure that the write operation happens before the unlock. */
1097 dmbishst();
1098
1099 /* Unlock access to the L1 tables. */
1100 spin_unlock(&gpt_lock);
1101
1102 /* Cache maintenance. */
1103 clean_dcache_range((uintptr_t)&gpt_l1_addr[idx],
1104 sizeof(uint64_t));
1105 gpt_tlbi_by_pa(base, GPT_PGS_ACTUAL_SIZE(gpt_config.p));
1106 dsbishst();
1107
1108 VERBOSE("[GPT] Granule 0x%llx, GPI 0x%x->0x%x\n", base, gpi,
1109 target_pas);
1110
1111 return 0;
1112}