blob: e49306822acd348d5432fa9e49b1abb1c21748cf [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
AlexeiFedorovddcf3362025-06-02 17:36:20 +010054/* Non-coherent device memory attributes, Outer Shareable */
55#define S2TTE_TEST_DEV_ATTRS S2TTE_DEV_NCOH_ATTRS
56#define S2TTE_TEST_DEV_SH S2TTE_SH_OS
57
Javier Almansa Sobrino4389c342024-04-10 12:12:13 +010058/*
59 * Function to setup the environment for the tests specifying
60 * whether FEAT_LPA2 is supported or not.
61 */
62void s2tt_test_helpers_setup(bool lpa2);
63
64/* Get the PA mapped into a specific S2TTE */
65unsigned long s2tt_test_helpers_s2tte_to_pa(unsigned long s2tte, long level);
66
67/* Map a PA into a specific S2TTE */
68unsigned long s2tt_test_helpers_pa_to_s2tte(unsigned long pa, long level);
69
70/* Get the PA mask for a given level */
Javier Almansa Sobrino9810a8e2024-06-03 15:17:25 +010071unsigned long s2tt_test_helpers_lvl_mask(long level);
Javier Almansa Sobrino4389c342024-04-10 12:12:13 +010072
73/* Get the S2TTE PA mask */
74unsigned long s2tt_test_helpers_s2tte_oa_mask(void);
75
76/*
Javier Almansa Sobrino9810a8e2024-06-03 15:17:25 +010077 * Generates an IPA aligned @level
Javier Almansa Sobrino4389c342024-04-10 12:12:13 +010078 * Params:
79 * - level: Level for which the address will be generated. This will
80 * define the minimum value for the address.
81 * - aligned: If 'true' the address will be aligned to 'level'. If 'false'
82 * the address might or might not be aligned.
83 */
Javier Almansa Sobrino9810a8e2024-06-03 15:17:25 +010084unsigned long s2tt_test_helpers_gen_addr(long level, bool aligned);
Javier Almansa Sobrino4389c342024-04-10 12:12:13 +010085
86/* Retrieve the attributes for a given tte */
87unsigned long s2tt_test_helpers_s2tte_to_attrs(unsigned long tte, bool ns);
88
89/*
90 * Generate random attributes for a NS-TTE
91 * Params:
92 * - host: If 'true', it generates a random set of attributes
93 * controlled by the host. If 'false;, it generates
94 * a random set of attibutes controlled by RMM.
95 * - reserved: If host == 'true', this flag determines whether
96 * the HS or the MEMATTR fields on the host attributes
97 * will be set to RESERVED or not. If 'true' either one
98 * or both of the fields can be set to RESERVED, which
99 * will make the descriptor invalid.
100 */
101unsigned long s2tt_test_helpers_gen_ns_attrs(bool host, bool reserved);
102
103/*
104 * Generate attributes for a secure TTE
105 * Params:
106 * - invalid: If 'true', it will generate a valid TTE (e.g. the
107 * descriptor type will be other than INVALID). When
108 * 'false, it will generate a valid TTE.
109 * - level: Level of the TTE when 'invalid' == 'false'.
110 */
111unsigned long s2tt_test_helpers_gen_attrs(bool invalid, long level);
112
113/* Get the minimum table level */
114long s2tt_test_helpers_min_table_lvl(void);
115
116/* Get the minimum block level */
117long s2tt_test_helpers_min_block_lvl(void);
118
AlexeiFedorovddcf3362025-06-02 17:36:20 +0100119/* Get the minimum device memory block level */
120long s2tt_test_helpers_min_dev_block_lvl(void);
121
Javier Almansa Sobrino4389c342024-04-10 12:12:13 +0100122/* For a given level return the VA space size of an S2TTE entry at such level */
123unsigned long s2tt_test_helpers_get_entry_va_space_size(long level);
124
125/* For a given address and level, return the index @level table for 'addr' */
126unsigned long s2tt_test_helpers_get_idx_from_addr(unsigned long addr,
127 long level);
128
129/* Helper to know whether LPA2 is enabled or not for the current test */
130bool s2tt_test_helpers_lpa2_enabled(void);
131
132/* Helper to create an assigned S2TTE as per the passed parameters */
133unsigned long s2tt_test_create_assigned(const struct s2tt_context *s2tt_ctx,
134 unsigned long pa, long level,
135 unsigned long ripas);
136
AlexeiFedorovddcf3362025-06-02 17:36:20 +0100137/* Helper to create an assigned_dev S2TTE as per the passed parameters */
138unsigned long s2tt_test_create_assigned_dev(const struct s2tt_context *s2tt_ctx,
139 unsigned long pa, long level,
140 unsigned long ripas);
141
142/* Helper to create an assigned_dev_dev S2TTE as per the passed parameters */
143unsigned long s2tt_test_create_assigned_dev_dev(const struct s2tt_context *s2tt_ctx,
144 unsigned long pa, long level);
145
146/* Helper to init an assigned_dev_dev S2TTE as per the passed parameters */
147void s2tt_test_init_assigned_dev_dev(const struct s2tt_context *s2tt_ctx,
148 unsigned long *s2tt,
149 unsigned long pa, long level);
150
Javier Almansa Sobrino4389c342024-04-10 12:12:13 +0100151/* Helper to create an unassigned S2TTE as per the passed parameters */
152unsigned long s2tt_test_create_unassigned(const struct s2tt_context *s2tt_ctx,
153 unsigned long ripas);
154
Javier Almansa Sobrino9810a8e2024-06-03 15:17:25 +0100155/* Helper to modify the state of a granule without making any pre-checks */
156static inline void s2tt_test_granule_set_state(struct granule *g,
157 unsigned int state)
158{
159 g->descriptor = (g->descriptor & ~STATE_MASK) | INPLACE(GRN_STATE, state);
160}
161
162/*
163 * Helper to modify the locked state of a granule without making any pre-checks
164 */
165static inline void s2tt_test_granule_set_lock(struct granule *g,
166 bool locked)
167{
168 g->descriptor = (locked) ?
169 (g->descriptor | GRN_LOCK_BIT) : (g->descriptor & ~GRN_LOCK_BIT);
170}
171
Javier Almansa Sobrino4389c342024-04-10 12:12:13 +0100172#endif /* XLAT_TEST_HELPERS_H */