Javier Almansa Sobrino | 4389c34 | 2024-04-10 12:12:13 +0100 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-License-Identifier: BSD-3-Clause |
| 3 | * SPDX-FileCopyrightText: Copyright TF-RMM Contributors. |
| 4 | */ |
| 5 | |
| 6 | #ifndef S2TT_TEST_HELPERS_H |
| 7 | #define S2TT_TEST_HELPERS_H |
| 8 | |
| 9 | #include <arch_helpers.h> |
Javier Almansa Sobrino | 9810a8e | 2024-06-03 15:17:25 +0100 | [diff] [blame] | 10 | #include <granule_types.h> |
Javier Almansa Sobrino | 4389c34 | 2024-04-10 12:12:13 +0100 | [diff] [blame] | 11 | #include <utils_def.h> |
| 12 | |
| 13 | /* Macros to specify LPA2 status */ |
| 14 | #define LPA2_ENABLED (true) |
| 15 | #define LPA2_DISABLED (false) |
| 16 | |
| 17 | /* |
| 18 | * Helper macro definitions. |
| 19 | * When possible, we try not to rely on the SUT own definitions, to |
| 20 | * avoid poisoning if any of them are buggy. |
| 21 | * |
| 22 | * Some essential definions which rely on build options are taken from |
| 23 | * the SUT definitions, though (e.g. {MIN, MAX}_IPA_BITS and such). |
| 24 | */ |
| 25 | #define S2TT_TEST_HELPERS_MIN_LVL (0L) |
| 26 | #define S2TT_TEST_HELPERS_MIN_LVL_LPA2 (-1L) |
| 27 | #define S2TT_TEST_HELPERS_MAX_LVL (3L) |
| 28 | #define S2TT_TEST_HELPERS_MAX_TABLE_LVL (2L) |
| 29 | |
| 30 | /* TTE type */ |
| 31 | #define S2TT_TEST_DESC_TYPE_WIDTH (2UL) |
| 32 | #define S2TT_TEST_DESC_TYPE_SHIFT (0UL) |
| 33 | #define S2TT_TEST_DESC_TYPE_MASK MASK(S2TT_TEST_DESC_TYPE) |
| 34 | #define S2TT_TEST_INVALID_DESC INPLACE(S2TT_TEST_DESC_TYPE, 0x0UL) |
| 35 | #define S2TT_TEST_BLOCK_DESC INPLACE(S2TT_TEST_DESC_TYPE, 0x1UL) |
| 36 | #define S2TT_TEST_PAGE_DESC INPLACE(S2TT_TEST_DESC_TYPE, 0x3UL) |
| 37 | #define S2TT_TEST_TABLE_DESC INPLACE(S2TT_TEST_DESC_TYPE, 0x3UL) |
| 38 | |
| 39 | /* |
| 40 | * When FEAT_LPA2 is enabled, the 2 MSB bits of the OA is not contiguous |
| 41 | * to the rest of the address in the TTE. |
| 42 | */ |
| 43 | #define S2TT_TEST_OA_MSB_SHIFT 50U |
| 44 | #define S2TT_TEST_OA_MSB_WIDTH 2U |
| 45 | |
| 46 | /* Where the 2 MSB bits of the OA are stored in the TTE */ |
| 47 | #define S2TT_TEST_MSB_SHIFT 8U |
| 48 | #define S2TT_TEST_MSB_WIDTH S2TT_TEST_OA_MSB_WIDTH |
| 49 | #define S2TT_TEST_MSB_MASK MASK(S2TT_TEST_MSB) |
| 50 | |
| 51 | /* Invalid value for the RIPAS field */ |
| 52 | #define S2TT_TEST_RIPAS_INVALID (3UL) |
| 53 | |
| 54 | /* |
| 55 | * Function to setup the environment for the tests specifying |
| 56 | * whether FEAT_LPA2 is supported or not. |
| 57 | */ |
| 58 | void s2tt_test_helpers_setup(bool lpa2); |
| 59 | |
| 60 | /* Get the PA mapped into a specific S2TTE */ |
| 61 | unsigned long s2tt_test_helpers_s2tte_to_pa(unsigned long s2tte, long level); |
| 62 | |
| 63 | /* Map a PA into a specific S2TTE */ |
| 64 | unsigned long s2tt_test_helpers_pa_to_s2tte(unsigned long pa, long level); |
| 65 | |
| 66 | /* Get the PA mask for a given level */ |
Javier Almansa Sobrino | 9810a8e | 2024-06-03 15:17:25 +0100 | [diff] [blame] | 67 | unsigned long s2tt_test_helpers_lvl_mask(long level); |
Javier Almansa Sobrino | 4389c34 | 2024-04-10 12:12:13 +0100 | [diff] [blame] | 68 | |
| 69 | /* Get the S2TTE PA mask */ |
| 70 | unsigned long s2tt_test_helpers_s2tte_oa_mask(void); |
| 71 | |
| 72 | /* |
Javier Almansa Sobrino | 9810a8e | 2024-06-03 15:17:25 +0100 | [diff] [blame] | 73 | * Generates an IPA aligned @level |
Javier Almansa Sobrino | 4389c34 | 2024-04-10 12:12:13 +0100 | [diff] [blame] | 74 | * Params: |
| 75 | * - level: Level for which the address will be generated. This will |
| 76 | * define the minimum value for the address. |
| 77 | * - aligned: If 'true' the address will be aligned to 'level'. If 'false' |
| 78 | * the address might or might not be aligned. |
| 79 | */ |
Javier Almansa Sobrino | 9810a8e | 2024-06-03 15:17:25 +0100 | [diff] [blame] | 80 | unsigned long s2tt_test_helpers_gen_addr(long level, bool aligned); |
Javier Almansa Sobrino | 4389c34 | 2024-04-10 12:12:13 +0100 | [diff] [blame] | 81 | |
| 82 | /* Retrieve the attributes for a given tte */ |
| 83 | unsigned long s2tt_test_helpers_s2tte_to_attrs(unsigned long tte, bool ns); |
| 84 | |
| 85 | /* |
| 86 | * Generate random attributes for a NS-TTE |
| 87 | * Params: |
| 88 | * - host: If 'true', it generates a random set of attributes |
| 89 | * controlled by the host. If 'false;, it generates |
| 90 | * a random set of attibutes controlled by RMM. |
| 91 | * - reserved: If host == 'true', this flag determines whether |
| 92 | * the HS or the MEMATTR fields on the host attributes |
| 93 | * will be set to RESERVED or not. If 'true' either one |
| 94 | * or both of the fields can be set to RESERVED, which |
| 95 | * will make the descriptor invalid. |
| 96 | */ |
| 97 | unsigned long s2tt_test_helpers_gen_ns_attrs(bool host, bool reserved); |
| 98 | |
| 99 | /* |
| 100 | * Generate attributes for a secure TTE |
| 101 | * Params: |
| 102 | * - invalid: If 'true', it will generate a valid TTE (e.g. the |
| 103 | * descriptor type will be other than INVALID). When |
| 104 | * 'false, it will generate a valid TTE. |
| 105 | * - level: Level of the TTE when 'invalid' == 'false'. |
| 106 | */ |
| 107 | unsigned long s2tt_test_helpers_gen_attrs(bool invalid, long level); |
| 108 | |
| 109 | /* Get the minimum table level */ |
| 110 | long s2tt_test_helpers_min_table_lvl(void); |
| 111 | |
| 112 | /* Get the minimum block level */ |
| 113 | long s2tt_test_helpers_min_block_lvl(void); |
| 114 | |
| 115 | /* For a given level return the VA space size of an S2TTE entry at such level */ |
| 116 | unsigned long s2tt_test_helpers_get_entry_va_space_size(long level); |
| 117 | |
| 118 | /* For a given address and level, return the index @level table for 'addr' */ |
| 119 | unsigned long s2tt_test_helpers_get_idx_from_addr(unsigned long addr, |
| 120 | long level); |
| 121 | |
| 122 | /* Helper to know whether LPA2 is enabled or not for the current test */ |
| 123 | bool s2tt_test_helpers_lpa2_enabled(void); |
| 124 | |
| 125 | /* Helper to create an assigned S2TTE as per the passed parameters */ |
| 126 | unsigned long s2tt_test_create_assigned(const struct s2tt_context *s2tt_ctx, |
| 127 | unsigned long pa, long level, |
| 128 | unsigned long ripas); |
| 129 | |
| 130 | /* Helper to create an unassigned S2TTE as per the passed parameters */ |
| 131 | unsigned long s2tt_test_create_unassigned(const struct s2tt_context *s2tt_ctx, |
| 132 | unsigned long ripas); |
| 133 | |
Javier Almansa Sobrino | 9810a8e | 2024-06-03 15:17:25 +0100 | [diff] [blame] | 134 | /* Helper to modify the state of a granule without making any pre-checks */ |
| 135 | static inline void s2tt_test_granule_set_state(struct granule *g, |
| 136 | unsigned int state) |
| 137 | { |
| 138 | g->descriptor = (g->descriptor & ~STATE_MASK) | INPLACE(GRN_STATE, state); |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Helper to modify the locked state of a granule without making any pre-checks |
| 143 | */ |
| 144 | static inline void s2tt_test_granule_set_lock(struct granule *g, |
| 145 | bool locked) |
| 146 | { |
| 147 | g->descriptor = (locked) ? |
| 148 | (g->descriptor | GRN_LOCK_BIT) : (g->descriptor & ~GRN_LOCK_BIT); |
| 149 | } |
| 150 | |
Javier Almansa Sobrino | 4389c34 | 2024-04-10 12:12:13 +0100 | [diff] [blame] | 151 | #endif /* XLAT_TEST_HELPERS_H */ |