blob: 0af8b47a19d1894ec09caa1e932d325c49fd2c73 [file] [log] [blame]
Javier Almansa Sobrinobb66f8a2023-01-05 16:43:43 +00001/*
2 * SPDX-License-Identifier: BSD-3-Clause
3 * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
4 */
5
6#include <buffer.h>
7#include <buffer_private.h>
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +00008#include <granule.h>
9#include <host_utils.h>
Javier Almansa Sobrinobb66f8a2023-01-05 16:43:43 +000010#include <xlat_tables.h>
11
12/*
13 * Return the PA mapped to a given slot.
14 *
15 * NOTE: This API assumes a 4KB granularity and that the architecture
16 * has a VA space of 48 bits.
17 */
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +010018uintptr_t realm_test_util_slot_to_pa(enum buffer_slot slot)
Javier Almansa Sobrinobb66f8a2023-01-05 16:43:43 +000019{
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +000020 struct xlat_tbl_info *entry = get_cached_tbl_info();
Javier Almansa Sobrinobb66f8a2023-01-05 16:43:43 +000021 uintptr_t va = slot_to_va(slot);
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +000022 uint64_t *desc_ptr = xlat_get_tte_ptr(entry, va);
23 uint64_t descriptor = xlat_read_tte(desc_ptr);
Javier Almansa Sobrinobb66f8a2023-01-05 16:43:43 +000024
25 return (uintptr_t)(descriptor & XLAT_TTE_L3_PA_MASK);
26}
27
28/*
29 * Helper function to find the slot VA to which a PA is mapped to.
30 * This function is used to validate that the slot buffer library
31 * mapped the given PA to the VA that would be expected by the
32 * aarch64 VMSA.
33 */
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +010034uintptr_t realm_test_util_slot_va_from_pa(uintptr_t pa)
Javier Almansa Sobrinobb66f8a2023-01-05 16:43:43 +000035{
36 for (unsigned int i = 0U; i < NR_CPU_SLOTS; i++) {
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +010037 if (pa == realm_test_util_slot_to_pa((enum buffer_slot)i)) {
Javier Almansa Sobrinobb66f8a2023-01-05 16:43:43 +000038 /*
39 * Found a slot returning the same address, get
40 * the VA for that slot (the one that would be
41 * used by the aarch64 VMSA).
42 */
43 return slot_to_va((enum buffer_slot)i);
44 }
45 }
46
47 /* No buffer slot found */
48 return (uintptr_t)NULL;
49}
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +000050
51/*
52 * Function to return the base pointer to granule structure.
53 * This function relies on addr_to_granule().
54 */
55struct granule *realm_test_util_granule_struct_base(void)
56{
57 return addr_to_granule(host_util_get_granule_base());
58}