blob: 007057fd38cfbe7c689a3d45b93050075087125d [file] [log] [blame]
Soby Mathewb4c6df42022-11-09 11:13:29 +00001/*
2 * SPDX-License-Identifier: BSD-3-Clause
3 * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
4 * SPDX-FileCopyrightText: Copyright Arm Limited and Contributors.
5 */
6
7/* This file is derived from xlat_table_v2 library in TF-A project */
8
9#ifndef XLAT_CONTEXTS_H
10#define XLAT_CONTEXTS_H
11
12#ifndef __ASSEMBLER__
13
Soby Mathewb4c6df42022-11-09 11:13:29 +000014#include <stdbool.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000015#include <xlat_defs.h>
16
Soby Mathewb4c6df42022-11-09 11:13:29 +000017/* Enumerator to identify the right address space within a context */
18typedef enum xlat_addr_region_id {
19 VA_LOW_REGION = 0,
20 VA_HIGH_REGION,
21 VA_REGIONS
22} xlat_addr_region_id_t;
23
24/*
25 * Structure to hold all the xlat tables and related information within a
26 * context. This allows to reuse the same xlat_ctx_cfg part of the context
27 * on several PEs that share the same memory map region whilst keeping
28 * private tables for each PE.
29 */
30struct xlat_ctx_tbls {
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +000031 /* Array of translation tables. */
32 uint64_t *tables;
Soby Mathewb4c6df42022-11-09 11:13:29 +000033 unsigned int tables_num;
34 unsigned int next_table;
35
Soby Mathewb4c6df42022-11-09 11:13:29 +000036 /* Set to true when the translation tables are initialized. */
37 bool initialized;
38};
39
40/* Struct that holds the context configuration */
41struct xlat_ctx_cfg {
42 /*
43 * Maximum size allowed for the VA space handled by the context.
44 */
45 uintptr_t max_va_size;
46
47 /*
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +000048 * Pointer to an array with all the memory regions stored in order
49 * of ascending base_va.
Soby Mathewb4c6df42022-11-09 11:13:29 +000050 */
51 struct xlat_mmap_region *mmap;
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +000052
53 /*
54 * Number of regions stored in the mmap array.
55 */
56 unsigned int mmap_regions;
Soby Mathewb4c6df42022-11-09 11:13:29 +000057
58 /*
59 * Base address for the virtual space on this context.
60 */
61 uintptr_t base_va;
62
63 /*
64 * Max Physical and Virtual addresses currently in use by the
65 * current context. These will get updated as we map memory
66 * regions but it will never go beyond the maximum physical address
67 * or max_va_size respectively.
68 *
69 * max_mapped_pa is an absolute Physical Address.
70 */
71 uintptr_t max_mapped_pa;
72 uintptr_t max_mapped_va_offset;
73
74 /* Level of the base translation table. */
75 unsigned int base_level;
76
77 /*
78 * Virtual address region handled by this context.
79 */
80 xlat_addr_region_id_t region;
81
82 bool initialized;
83};
84
85/*
86 * Struct that holds the context itself, composed of
87 * a pointer to the context config and a pointer to the
88 * translation tables associated to it.
89 */
90struct xlat_ctx {
91 struct xlat_ctx_cfg *cfg;
92 struct xlat_ctx_tbls *tbls;
93};
94
95/*
96 * The translation tables are aligned to their size. For 4KB graularity, this
97 * is aligned to 4KB as well.
98 */
99#define XLAT_TABLES_ALIGNMENT XLAT_TABLE_SIZE
100
101/*
Soby Mathewb4c6df42022-11-09 11:13:29 +0000102 * Compute the number of entries required at the initial lookup level to
103 * address the whole virtual address space.
104 */
105#define GET_NUM_BASE_LEVEL_ENTRIES(addr_space_size) \
106 ((addr_space_size) >> \
107 XLAT_ADDR_SHIFT(GET_XLAT_TABLE_LEVEL_BASE(addr_space_size)))
108
109/*
110 * Macro to check if the xlat_ctx_cfg part of a context is valid.
111 */
112#define XLAT_TABLES_CTX_CFG_VALID(_ctx) ((_ctx)->cfg != NULL)
113
114/*
115 * Macro to check if the xlat_ctx_tbls part of a context is valid.
116 */
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +0000117#define XLAT_TABLES_CTX_TBL_VALID(_ctx) ((_ctx)->tbls != NULL)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000118
119/*
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +0000120 * Function to initialize the configuration structure for a
121 * translation context. This function must be called before
122 * the MMU is enabled.
123 *
124 * Arguments:
125 * - cfg: Pointer to a xlat_ctx_cfg structure to initialize.
126 * - region: xlat_addr_region_id_t descriptor indicating the memory
127 * region for the configured context.
128 * - mm: List of memory map regions to add to the
129 * context configuration.
130 * - mm_regions: Number of memory regions in the mm array.
131 * - va_size: Size of the VA space for the current context.
132 *
133 * Return:
134 * - 0 on success or a negative POSIX error otherwise.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000135 */
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +0000136int xlat_ctx_cfg_init(struct xlat_ctx_cfg *cfg,
137 xlat_addr_region_id_t region,
138 struct xlat_mmap_region *mm,
139 unsigned int mm_regions,
140 size_t va_size);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000141
142/*
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +0000143 * Initializes the translation context (xlat_ctx) and the xlat_ctx_tbls with
144 * the given xlat_ctx_cfg. The tables are created according to the memory
145 * map description available in the latter and stored in the tables area
146 * pointed by `tables_ptr`.
147 * Must be called before the MMU is enabled.
148 *
149 * Arguments:
150 * - ctx: Pointer to the xlat_ctx structure to initialize.
151 * - cfg: Pointer to the structure containing the context configuration.
152 * This must have already been initialized via xlat_ctx_cfg_init().
153 * - tbls_ctx: Pointer to a xlat_ctx_tbls structure to configure the
154 * associated table data for the translation context.
155 * - tables_ptr: Pointer to the memory for the translation tables,
156 * the memory provided must be page aligned and multiple
157 * of page size.
158 * - ntables: Number of pages passed in the `tables_ptr`.
159 *
160 * Return:
161 * - 0 on success.
162 * - -EALREADY if tbls_ctx is already initialized.
163 * - Negative POSIX error codes on all other errors.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000164 */
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +0000165int xlat_ctx_init(struct xlat_ctx *ctx,
166 struct xlat_ctx_cfg *cfg,
167 struct xlat_ctx_tbls *tbls_ctx,
168 uint64_t *tables_ptr,
169 unsigned int ntables);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000170
171#endif /*__ASSEMBLER__*/
Soby Mathewb4c6df42022-11-09 11:13:29 +0000172#endif /* XLAT_CONTEXTS_H */