Javier Almansa Sobrino | 2f717dd | 2024-02-12 20:49:46 +0000 | [diff] [blame^] | 1 | /* |
| 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> |
| 8 | #include <stddef.h> |
| 9 | #include <test_helpers.h> |
| 10 | #include <xlat_tables.h> |
| 11 | |
| 12 | uintptr_t buffer_test_helpers_slot_to_pa(enum buffer_slot slot) |
| 13 | { |
| 14 | struct xlat_llt_info *entry = get_cached_llt_info(); |
| 15 | uintptr_t va = slot_to_va(slot); |
| 16 | uint64_t *desc_ptr = xlat_get_tte_ptr(entry, va); |
| 17 | uint64_t descriptor = xlat_read_tte(desc_ptr); |
| 18 | |
| 19 | return (uintptr_t)xlat_get_oa_from_tte(descriptor); |
| 20 | } |
| 21 | |
| 22 | /* |
| 23 | * Helper function to find the slot VA to which a PA is mapped to. |
| 24 | * This function is used to validate that the slot buffer library |
| 25 | * mapped the given PA to the VA that would be expected by the |
| 26 | * aarch64 VMSA. |
| 27 | */ |
| 28 | uintptr_t buffer_test_helpers_slot_va_from_pa(uintptr_t pa) |
| 29 | { |
| 30 | for (unsigned int i = 0U; i < (unsigned int)NR_CPU_SLOTS; i++) { |
| 31 | if (pa == buffer_test_helpers_slot_to_pa((enum buffer_slot)i)) { |
| 32 | /* |
| 33 | * Found a slot returning the same address, get |
| 34 | * the VA for that slot (the one that would be |
| 35 | * used by the aarch64 VMSA). |
| 36 | */ |
| 37 | return slot_to_va((enum buffer_slot)i); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | /* No buffer slot found */ |
| 42 | return (uintptr_t)NULL; |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * Callback to mock aarch64 based slot buffer mapping to the buffer tests. |
| 47 | * This function maps `addr` to the requested slot in slot buffer and returns |
| 48 | * a pointer which can be read or written to by the tests on fake_host |
| 49 | * architecture. |
| 50 | * Note that the function maps the addr in the Stage 1 xlat tables as per |
| 51 | * aarch64 VMSA and it walks the table to retrieve the PA and returns this |
| 52 | * back to the caller. |
| 53 | */ |
| 54 | void *buffer_test_cb_map_access(unsigned int slot, unsigned long addr) |
| 55 | { |
| 56 | void *va = buffer_map_internal((enum buffer_slot)slot, addr); |
| 57 | |
| 58 | if (va == NULL) { |
| 59 | return NULL; |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * Perform a table walk to get the PA mapped to `slot`. |
| 64 | * If everything went well it should return the same address as `addr`. |
| 65 | */ |
| 66 | return (void *)buffer_test_helpers_slot_to_pa((enum buffer_slot)slot); |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Callback to mock aarch64 based slot buffer ummapping to the buffer tests. |
| 71 | * The function receives a `buf` pointer mapped using |
| 72 | * buffer_test_cb_map_access(). It needs to find the VA as per aarch64 |
| 73 | * VMSA based slot buffer and then it uses this va to unmap from Stage 1 |
| 74 | * xlat tables. |
| 75 | */ |
| 76 | void buffer_test_cb_unmap_access(void *buf) |
| 77 | { |
| 78 | void *slot_va = |
| 79 | (void *)buffer_test_helpers_slot_va_from_pa((uintptr_t)buf); |
| 80 | |
| 81 | assert(slot_va != NULL); |
| 82 | |
| 83 | buffer_unmap_internal(slot_va); |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Callback to map an addr to a slot as per aarch64 VMSA. Note that the address |
| 88 | * to the buffer returned by this function cannot be read/written to from |
| 89 | * the tests. |
| 90 | */ |
| 91 | void *buffer_test_cb_map_aarch64_vmsa(unsigned int slot, unsigned long addr) |
| 92 | { |
| 93 | return buffer_map_internal((enum buffer_slot)slot, addr); |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Callback to unmap a buf mapped to a slot as per aarch64 vmsa via |
| 98 | * buffer_test_cb_map_aarch64_vmsa() callback. |
| 99 | */ |
| 100 | void buffer_test_cb_unmap_aarch64_vmsa(void *buf) |
| 101 | { |
| 102 | buffer_unmap_internal(buf); |
| 103 | } |