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