blob: 4755518a450cf624189503f0fc068ca534cc2ae5 [file] [log] [blame]
Javier Almansa Sobrino4389c342024-04-10 12:12:13 +01001/*
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 Sobrino9810a8e2024-06-03 15:17:25 +010010#include <granule_types.h>
Javier Almansa Sobrino4389c342024-04-10 12:12:13 +010011#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 */
58void s2tt_test_helpers_setup(bool lpa2);
59
60/* Get the PA mapped into a specific S2TTE */
61unsigned long s2tt_test_helpers_s2tte_to_pa(unsigned long s2tte, long level);
62
63/* Map a PA into a specific S2TTE */
64unsigned long s2tt_test_helpers_pa_to_s2tte(unsigned long pa, long level);
65
66/* Get the PA mask for a given level */
Javier Almansa Sobrino9810a8e2024-06-03 15:17:25 +010067unsigned long s2tt_test_helpers_lvl_mask(long level);
Javier Almansa Sobrino4389c342024-04-10 12:12:13 +010068
69/* Get the S2TTE PA mask */
70unsigned long s2tt_test_helpers_s2tte_oa_mask(void);
71
72/*
Javier Almansa Sobrino9810a8e2024-06-03 15:17:25 +010073 * Generates an IPA aligned @level
Javier Almansa Sobrino4389c342024-04-10 12:12:13 +010074 * 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 Sobrino9810a8e2024-06-03 15:17:25 +010080unsigned long s2tt_test_helpers_gen_addr(long level, bool aligned);
Javier Almansa Sobrino4389c342024-04-10 12:12:13 +010081
82/* Retrieve the attributes for a given tte */
83unsigned 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 */
97unsigned 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 */
107unsigned long s2tt_test_helpers_gen_attrs(bool invalid, long level);
108
109/* Get the minimum table level */
110long s2tt_test_helpers_min_table_lvl(void);
111
112/* Get the minimum block level */
113long 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 */
116unsigned 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' */
119unsigned 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 */
123bool s2tt_test_helpers_lpa2_enabled(void);
124
125/* Helper to create an assigned S2TTE as per the passed parameters */
126unsigned 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 */
131unsigned long s2tt_test_create_unassigned(const struct s2tt_context *s2tt_ctx,
132 unsigned long ripas);
133
Javier Almansa Sobrino9810a8e2024-06-03 15:17:25 +0100134/* Helper to modify the state of a granule without making any pre-checks */
135static 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 */
144static 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 Sobrino4389c342024-04-10 12:12:13 +0100151#endif /* XLAT_TEST_HELPERS_H */