xref: /rk3399_ARM-atf/include/lib/xlat_tables/xlat_tables_v2.h (revision a52662edf3435a92438ddffe031d0fe35523fcf4)
17bb01fb2SAntonio Nino Diaz /*
2*9bfe78c2SLevi Yun  * Copyright (c) 2017-2025, Arm Limited and Contributors. All rights reserved.
37bb01fb2SAntonio Nino Diaz  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
57bb01fb2SAntonio Nino Diaz  */
67bb01fb2SAntonio Nino Diaz 
7e7b9886cSAntonio Nino Diaz #ifndef XLAT_TABLES_V2_H
8e7b9886cSAntonio Nino Diaz #define XLAT_TABLES_V2_H
97bb01fb2SAntonio Nino Diaz 
1009d40e0eSAntonio Nino Diaz #include <lib/xlat_tables/xlat_tables_defs.h>
1109d40e0eSAntonio Nino Diaz #include <lib/xlat_tables/xlat_tables_v2_helpers.h>
127bb01fb2SAntonio Nino Diaz 
13d5dfdeb6SJulius Werner #ifndef __ASSEMBLER__
147bb01fb2SAntonio Nino Diaz #include <stddef.h>
157bb01fb2SAntonio Nino Diaz #include <stdint.h>
1609d40e0eSAntonio Nino Diaz 
1709d40e0eSAntonio Nino Diaz #include <lib/xlat_tables/xlat_mmu_helpers.h>
187bb01fb2SAntonio Nino Diaz 
19fdb1964cSSandrine Bailleux /*
20fdb1964cSSandrine Bailleux  * Default granularity size for an mmap_region_t.
21fdb1964cSSandrine Bailleux  * Useful when no specific granularity is required.
22fdb1964cSSandrine Bailleux  *
23fdb1964cSSandrine Bailleux  * By default, choose the biggest possible block size allowed by the
24fdb1964cSSandrine Bailleux  * architectural state and granule size in order to minimize the number of page
25fdb1964cSSandrine Bailleux  * tables required for the mapping.
267bb01fb2SAntonio Nino Diaz  */
27fdb1964cSSandrine Bailleux #define REGION_DEFAULT_GRANULARITY	XLAT_BLOCK_SIZE(MIN_LVL_BLOCK_DESC)
287bb01fb2SAntonio Nino Diaz 
29fdb1964cSSandrine Bailleux /* Helper macro to define an mmap_region_t. */
30fdb1964cSSandrine Bailleux #define MAP_REGION(_pa, _va, _sz, _attr)	\
31e7b9886cSAntonio Nino Diaz 	MAP_REGION_FULL_SPEC(_pa, _va, _sz, _attr, REGION_DEFAULT_GRANULARITY)
32fdb1964cSSandrine Bailleux 
33fdb1964cSSandrine Bailleux /* Helper macro to define an mmap_region_t with an identity mapping. */
34fdb1964cSSandrine Bailleux #define MAP_REGION_FLAT(_adr, _sz, _attr)			\
35fdb1964cSSandrine Bailleux 	MAP_REGION(_adr, _adr, _sz, _attr)
36fdb1964cSSandrine Bailleux 
37fdb1964cSSandrine Bailleux /*
389056f108SAntonio Nino Diaz  * Helper macro to define entries for mmap_region_t. It allows to define 'pa'
399056f108SAntonio Nino Diaz  * and sets 'va' to 0 for each region. To be used with mmap_add_alloc_va().
409056f108SAntonio Nino Diaz  */
419056f108SAntonio Nino Diaz #define MAP_REGION_ALLOC_VA(pa, sz, attr)	MAP_REGION(pa, 0, sz, attr)
429056f108SAntonio Nino Diaz 
439056f108SAntonio Nino Diaz /*
44fdb1964cSSandrine Bailleux  * Helper macro to define an mmap_region_t to map with the desired granularity
45fdb1964cSSandrine Bailleux  * of translation tables.
46fdb1964cSSandrine Bailleux  *
47fdb1964cSSandrine Bailleux  * The granularity value passed to this macro must be a valid block or page
48fdb1964cSSandrine Bailleux  * size. When using a 4KB translation granule, this might be 4KB, 2MB or 1GB.
49fdb1964cSSandrine Bailleux  * Passing REGION_DEFAULT_GRANULARITY is also allowed and means that the library
50fdb1964cSSandrine Bailleux  * is free to choose the granularity for this region. In this case, it is
51fdb1964cSSandrine Bailleux  * equivalent to the MAP_REGION() macro.
527bb01fb2SAntonio Nino Diaz  */
53fdb1964cSSandrine Bailleux #define MAP_REGION2(_pa, _va, _sz, _attr, _gr)			\
54e7b9886cSAntonio Nino Diaz 	MAP_REGION_FULL_SPEC(_pa, _va, _sz, _attr, _gr)
557bb01fb2SAntonio Nino Diaz 
567bb01fb2SAntonio Nino Diaz /*
573a1b7b10SAntonio Nino Diaz  * Shifts and masks to access fields of an mmap attribute
587bb01fb2SAntonio Nino Diaz  */
59030567e6SVarun Wadekar #define MT_TYPE_MASK		U(0x7)
607bb01fb2SAntonio Nino Diaz #define MT_TYPE(_attr)		((_attr) & MT_TYPE_MASK)
617bb01fb2SAntonio Nino Diaz /* Access permissions (RO/RW) */
62030567e6SVarun Wadekar #define MT_PERM_SHIFT		U(3)
6336218238SZelalem Aweke 
6436218238SZelalem Aweke /* Physical address space (SECURE/NS/Root/Realm) */
6536218238SZelalem Aweke #define	MT_PAS_SHIFT		U(4)
6636218238SZelalem Aweke #define MT_PAS_MASK		(U(3) << MT_PAS_SHIFT)
6736218238SZelalem Aweke #define MT_PAS(_attr)		((_attr) & MT_PAS_MASK)
6836218238SZelalem Aweke 
697bb01fb2SAntonio Nino Diaz /* Access permissions for instruction execution (EXECUTE/EXECUTE_NEVER) */
7036218238SZelalem Aweke #define MT_EXECUTE_SHIFT	U(6)
713a1b7b10SAntonio Nino Diaz /* In the EL1&0 translation regime, User (EL0) or Privileged (EL1). */
7236218238SZelalem Aweke #define MT_USER_SHIFT		U(7)
7306aca857SPramod Kumar 
7406aca857SPramod Kumar /* Shareability attribute for the memory region */
7536218238SZelalem Aweke #define MT_SHAREABILITY_SHIFT	U(8)
7606aca857SPramod Kumar #define MT_SHAREABILITY_MASK	(U(3) << MT_SHAREABILITY_SHIFT)
7706aca857SPramod Kumar #define MT_SHAREABILITY(_attr)	((_attr) & MT_SHAREABILITY_MASK)
7836218238SZelalem Aweke 
790b64f4efSAntonio Nino Diaz /* All other bits are reserved */
807bb01fb2SAntonio Nino Diaz 
817bb01fb2SAntonio Nino Diaz /*
827bb01fb2SAntonio Nino Diaz  * Memory mapping attributes
837bb01fb2SAntonio Nino Diaz  */
843a1b7b10SAntonio Nino Diaz 
857bb01fb2SAntonio Nino Diaz /*
867bb01fb2SAntonio Nino Diaz  * Memory types supported.
873a1b7b10SAntonio Nino Diaz  * These are organised so that, going down the list, the memory types are
883a1b7b10SAntonio Nino Diaz  * getting weaker; conversely going up the list the memory types are getting
893a1b7b10SAntonio Nino Diaz  * stronger.
907bb01fb2SAntonio Nino Diaz  */
913a1b7b10SAntonio Nino Diaz #define MT_DEVICE		U(0)
923a1b7b10SAntonio Nino Diaz #define MT_NON_CACHEABLE	U(1)
933a1b7b10SAntonio Nino Diaz #define MT_MEMORY		U(2)
947bb01fb2SAntonio Nino Diaz /* Values up to 7 are reserved to add new memory types in the future */
957bb01fb2SAntonio Nino Diaz 
963a1b7b10SAntonio Nino Diaz #define MT_RO			(U(0) << MT_PERM_SHIFT)
973a1b7b10SAntonio Nino Diaz #define MT_RW			(U(1) << MT_PERM_SHIFT)
987bb01fb2SAntonio Nino Diaz 
9936218238SZelalem Aweke #define MT_SECURE		(U(0) << MT_PAS_SHIFT)
10036218238SZelalem Aweke #define MT_NS			(U(1) << MT_PAS_SHIFT)
10136218238SZelalem Aweke #define MT_ROOT			(U(2) << MT_PAS_SHIFT)
10236218238SZelalem Aweke #define MT_REALM		(U(3) << MT_PAS_SHIFT)
1037bb01fb2SAntonio Nino Diaz 
1047bb01fb2SAntonio Nino Diaz /*
1053a1b7b10SAntonio Nino Diaz  * Access permissions for instruction execution are only relevant for normal
1063a1b7b10SAntonio Nino Diaz  * read-only memory, i.e. MT_MEMORY | MT_RO. They are ignored (and potentially
1073a1b7b10SAntonio Nino Diaz  * overridden) otherwise:
1087bb01fb2SAntonio Nino Diaz  *  - Device memory is always marked as execute-never.
1097bb01fb2SAntonio Nino Diaz  *  - Read-write normal memory is always marked as execute-never.
1107bb01fb2SAntonio Nino Diaz  */
1113a1b7b10SAntonio Nino Diaz #define MT_EXECUTE		(U(0) << MT_EXECUTE_SHIFT)
1123a1b7b10SAntonio Nino Diaz #define MT_EXECUTE_NEVER	(U(1) << MT_EXECUTE_SHIFT)
113609c9191SAntonio Nino Diaz 
114609c9191SAntonio Nino Diaz /*
1153a1b7b10SAntonio Nino Diaz  * When mapping a region at EL0 or EL1, this attribute will be used to determine
1163a1b7b10SAntonio Nino Diaz  * if a User mapping (EL0) will be created or a Privileged mapping (EL1).
117609c9191SAntonio Nino Diaz  */
1183a1b7b10SAntonio Nino Diaz #define MT_USER			(U(1) << MT_USER_SHIFT)
1193a1b7b10SAntonio Nino Diaz #define MT_PRIVILEGED		(U(0) << MT_USER_SHIFT)
1207bb01fb2SAntonio Nino Diaz 
12106aca857SPramod Kumar /*
12206aca857SPramod Kumar  * Shareability defines the visibility of any cache changes to
12306aca857SPramod Kumar  * all masters belonging to a shareable domain.
12406aca857SPramod Kumar  *
12506aca857SPramod Kumar  * MT_SHAREABILITY_ISH: For inner shareable domain
12606aca857SPramod Kumar  * MT_SHAREABILITY_OSH: For outer shareable domain
12706aca857SPramod Kumar  * MT_SHAREABILITY_NSH: For non shareable domain
12806aca857SPramod Kumar  */
12906aca857SPramod Kumar #define MT_SHAREABILITY_ISH	(U(1) << MT_SHAREABILITY_SHIFT)
13006aca857SPramod Kumar #define MT_SHAREABILITY_OSH	(U(2) << MT_SHAREABILITY_SHIFT)
13106aca857SPramod Kumar #define MT_SHAREABILITY_NSH	(U(3) << MT_SHAREABILITY_SHIFT)
13206aca857SPramod Kumar 
133609c9191SAntonio Nino Diaz /* Compound attributes for most common usages */
1347bb01fb2SAntonio Nino Diaz #define MT_CODE			(MT_MEMORY | MT_RO | MT_EXECUTE)
1357bb01fb2SAntonio Nino Diaz #define MT_RO_DATA		(MT_MEMORY | MT_RO | MT_EXECUTE_NEVER)
136609c9191SAntonio Nino Diaz #define MT_RW_DATA		(MT_MEMORY | MT_RW | MT_EXECUTE_NEVER)
1377bb01fb2SAntonio Nino Diaz 
1387bb01fb2SAntonio Nino Diaz /*
1397bb01fb2SAntonio Nino Diaz  * Structure for specifying a single region of memory.
1407bb01fb2SAntonio Nino Diaz  */
1417bb01fb2SAntonio Nino Diaz typedef struct mmap_region {
1427bb01fb2SAntonio Nino Diaz 	unsigned long long	base_pa;
1437bb01fb2SAntonio Nino Diaz 	uintptr_t		base_va;
1447bb01fb2SAntonio Nino Diaz 	size_t			size;
1453a1b7b10SAntonio Nino Diaz 	unsigned int		attr;
146fdb1964cSSandrine Bailleux 	/* Desired granularity. See the MAP_REGION2() macro for more details. */
147fdb1964cSSandrine Bailleux 	size_t			granularity;
1487bb01fb2SAntonio Nino Diaz } mmap_region_t;
1497bb01fb2SAntonio Nino Diaz 
15055c84964SSandrine Bailleux /*
151aa1d5f60SAntonio Nino Diaz  * Translation regimes supported by this library. EL_REGIME_INVALID tells the
152aa1d5f60SAntonio Nino Diaz  * library to detect it at runtime.
153b4ae615bSDouglas Raillard  */
154fd2299e6SAntonio Nino Diaz #define EL1_EL0_REGIME		1
1551a92a0e0SAntonio Nino Diaz #define EL2_REGIME		2
156fd2299e6SAntonio Nino Diaz #define EL3_REGIME		3
157aa1d5f60SAntonio Nino Diaz #define EL_REGIME_INVALID	-1
158b4ae615bSDouglas Raillard 
15936218238SZelalem Aweke /* Memory type for EL3 regions. With RME, EL3 is in ROOT PAS */
16036218238SZelalem Aweke #if ENABLE_RME
16136218238SZelalem Aweke #define EL3_PAS			MT_ROOT
16236218238SZelalem Aweke #else
16336218238SZelalem Aweke #define EL3_PAS			MT_SECURE
16436218238SZelalem Aweke #endif /* ENABLE_RME */
16536218238SZelalem Aweke 
166b4ae615bSDouglas Raillard /*
16755c84964SSandrine Bailleux  * Declare the translation context type.
16855c84964SSandrine Bailleux  * Its definition is private.
16955c84964SSandrine Bailleux  */
17055c84964SSandrine Bailleux typedef struct xlat_ctx xlat_ctx_t;
17155c84964SSandrine Bailleux 
17255c84964SSandrine Bailleux /*
17355c84964SSandrine Bailleux  * Statically allocate a translation context and associated structures. Also
17455c84964SSandrine Bailleux  * initialize them.
17555c84964SSandrine Bailleux  *
17655c84964SSandrine Bailleux  * _ctx_name:
17755c84964SSandrine Bailleux  *   Prefix for the translation context variable.
17855c84964SSandrine Bailleux  *   E.g. If _ctx_name is 'foo', the variable will be called 'foo_xlat_ctx'.
17955c84964SSandrine Bailleux  *   Useful to distinguish multiple contexts from one another.
18055c84964SSandrine Bailleux  *
18155c84964SSandrine Bailleux  * _mmap_count:
18255c84964SSandrine Bailleux  *   Number of mmap_region_t to allocate.
18355c84964SSandrine Bailleux  *   Would typically be MAX_MMAP_REGIONS for the translation context describing
18455c84964SSandrine Bailleux  *   the BL image currently executing.
18555c84964SSandrine Bailleux  *
18655c84964SSandrine Bailleux  * _xlat_tables_count:
18755c84964SSandrine Bailleux  *   Number of sub-translation tables to allocate.
18855c84964SSandrine Bailleux  *   Would typically be MAX_XLAT_TABLES for the translation context describing
18955c84964SSandrine Bailleux  *   the BL image currently executing.
19055c84964SSandrine Bailleux  *   Note that this is only for sub-tables ; at the initial lookup level, there
19155c84964SSandrine Bailleux  *   is always a single table.
19255c84964SSandrine Bailleux  *
19355c84964SSandrine Bailleux  * _virt_addr_space_size, _phy_addr_space_size:
19455c84964SSandrine Bailleux  *   Size (in bytes) of the virtual (resp. physical) address space.
19555c84964SSandrine Bailleux  *   Would typically be PLAT_VIRT_ADDR_SPACE_SIZE
19655c84964SSandrine Bailleux  *   (resp. PLAT_PHY_ADDR_SPACE_SIZE) for the translation context describing the
19755c84964SSandrine Bailleux  *   BL image currently executing.
19855c84964SSandrine Bailleux  */
19955c84964SSandrine Bailleux #define REGISTER_XLAT_CONTEXT(_ctx_name, _mmap_count, _xlat_tables_count, \
200268131c2SMasahiro Yamada 			      _virt_addr_space_size, _phy_addr_space_size) \
201e7b9886cSAntonio Nino Diaz 	REGISTER_XLAT_CONTEXT_FULL_SPEC(_ctx_name, (_mmap_count),	\
202fd2299e6SAntonio Nino Diaz 					 (_xlat_tables_count),		\
203fd2299e6SAntonio Nino Diaz 					 (_virt_addr_space_size),	\
204fd2299e6SAntonio Nino Diaz 					 (_phy_addr_space_size),	\
205363830dfSMasahiro Yamada 					 EL_REGIME_INVALID,		\
206da04341eSChris Kay 					 ".xlat_table", ".base_xlat_table")
207609c9191SAntonio Nino Diaz 
208609c9191SAntonio Nino Diaz /*
20945d640f0SAntonio Nino Diaz  * Same as REGISTER_XLAT_CONTEXT plus the additional parameters:
21045d640f0SAntonio Nino Diaz  *
21145d640f0SAntonio Nino Diaz  * _xlat_regime:
21245d640f0SAntonio Nino Diaz  *   Specify the translation regime managed by this xlat_ctx_t instance. The
213fd2299e6SAntonio Nino Diaz  *   values are the one from the EL*_REGIME definitions.
21445d640f0SAntonio Nino Diaz  *
21545d640f0SAntonio Nino Diaz  * _section_name:
21645d640f0SAntonio Nino Diaz  *   Specify the name of the section where the translation tables have to be
21745d640f0SAntonio Nino Diaz  *   placed by the linker.
2180922e481SMasahisa Kojima  *
2190922e481SMasahisa Kojima  * _base_table_section_name:
2200922e481SMasahisa Kojima  *   Specify the name of the section where the base translation tables have to
2210922e481SMasahisa Kojima  *   be placed by the linker.
222609c9191SAntonio Nino Diaz  */
223609c9191SAntonio Nino Diaz #define REGISTER_XLAT_CONTEXT2(_ctx_name, _mmap_count, _xlat_tables_count, \
224609c9191SAntonio Nino Diaz 			_virt_addr_space_size, _phy_addr_space_size,	\
2250922e481SMasahisa Kojima 			_xlat_regime, _section_name, _base_table_section_name) \
226e7b9886cSAntonio Nino Diaz 	REGISTER_XLAT_CONTEXT_FULL_SPEC(_ctx_name, (_mmap_count),	\
227fd2299e6SAntonio Nino Diaz 					 (_xlat_tables_count),		\
228fd2299e6SAntonio Nino Diaz 					 (_virt_addr_space_size),	\
229fd2299e6SAntonio Nino Diaz 					 (_phy_addr_space_size),	\
230363830dfSMasahiro Yamada 					 (_xlat_regime),		\
2310922e481SMasahisa Kojima 					 (_section_name), (_base_table_section_name) \
232363830dfSMasahiro Yamada )
23355c84964SSandrine Bailleux 
234a9ad848cSSandrine Bailleux /******************************************************************************
235a9ad848cSSandrine Bailleux  * Generic translation table APIs.
236a9ad848cSSandrine Bailleux  * Each API comes in 2 variants:
237a9ad848cSSandrine Bailleux  * - one that acts on the current translation context for this BL image
238a9ad848cSSandrine Bailleux  * - another that acts on the given translation context instead. This variant
239a9ad848cSSandrine Bailleux  *   is named after the 1st version, with an additional '_ctx' suffix.
240a9ad848cSSandrine Bailleux  *****************************************************************************/
241e769db3eSAntonio Nino Diaz 
242e769db3eSAntonio Nino Diaz /*
243e769db3eSAntonio Nino Diaz  * Initialize translation tables from the current list of mmap regions. Calling
244e769db3eSAntonio Nino Diaz  * this function marks the transition point after which static regions can no
245e769db3eSAntonio Nino Diaz  * longer be added.
246e769db3eSAntonio Nino Diaz  */
2477bb01fb2SAntonio Nino Diaz void init_xlat_tables(void);
248a9ad848cSSandrine Bailleux void init_xlat_tables_ctx(xlat_ctx_t *ctx);
2497bb01fb2SAntonio Nino Diaz 
2507bb01fb2SAntonio Nino Diaz /*
251bbc81007SAntonio Nino Diaz  * Fill all fields of a dynamic translation tables context. It must be done
252bbc81007SAntonio Nino Diaz  * either statically with REGISTER_XLAT_CONTEXT() or at runtime with this
253bbc81007SAntonio Nino Diaz  * function.
254bbc81007SAntonio Nino Diaz  */
255bbc81007SAntonio Nino Diaz void xlat_setup_dynamic_ctx(xlat_ctx_t *ctx, unsigned long long pa_max,
256bbc81007SAntonio Nino Diaz 			    uintptr_t va_max, struct mmap_region *mmap,
257bbc81007SAntonio Nino Diaz 			    unsigned int mmap_num, uint64_t **tables,
258bbc81007SAntonio Nino Diaz 			    unsigned int tables_num, uint64_t *base_table,
259bbc81007SAntonio Nino Diaz 			    int xlat_regime, int *mapped_regions);
260bbc81007SAntonio Nino Diaz 
261bbc81007SAntonio Nino Diaz /*
262e769db3eSAntonio Nino Diaz  * Add a static region with defined base PA and base VA. This function can only
263e769db3eSAntonio Nino Diaz  * be used before initializing the translation tables. The region cannot be
264e769db3eSAntonio Nino Diaz  * removed afterwards.
2657bb01fb2SAntonio Nino Diaz  */
2667bb01fb2SAntonio Nino Diaz void mmap_add_region(unsigned long long base_pa, uintptr_t base_va,
2673a1b7b10SAntonio Nino Diaz 		     size_t size, unsigned int attr);
268a9ad848cSSandrine Bailleux void mmap_add_region_ctx(xlat_ctx_t *ctx, const mmap_region_t *mm);
2697bb01fb2SAntonio Nino Diaz 
2707bb01fb2SAntonio Nino Diaz /*
271a9ad848cSSandrine Bailleux  * Add an array of static regions with defined base PA and base VA. This
272a9ad848cSSandrine Bailleux  * function can only be used before initializing the translation tables. The
273a9ad848cSSandrine Bailleux  * regions cannot be removed afterwards.
274a9ad848cSSandrine Bailleux  */
275a9ad848cSSandrine Bailleux void mmap_add(const mmap_region_t *mm);
276a9ad848cSSandrine Bailleux void mmap_add_ctx(xlat_ctx_t *ctx, const mmap_region_t *mm);
277a9ad848cSSandrine Bailleux 
2789056f108SAntonio Nino Diaz /*
2799056f108SAntonio Nino Diaz  * Add a region with defined base PA. Returns base VA calculated using the
2809056f108SAntonio Nino Diaz  * highest existing region in the mmap array even if it fails to allocate the
2819056f108SAntonio Nino Diaz  * region.
2829056f108SAntonio Nino Diaz  */
2839056f108SAntonio Nino Diaz void mmap_add_region_alloc_va(unsigned long long base_pa, uintptr_t *base_va,
2849056f108SAntonio Nino Diaz 			      size_t size, unsigned int attr);
2859056f108SAntonio Nino Diaz void mmap_add_region_alloc_va_ctx(xlat_ctx_t *ctx, mmap_region_t *mm);
2869056f108SAntonio Nino Diaz 
2879056f108SAntonio Nino Diaz /*
2889056f108SAntonio Nino Diaz  * Add an array of static regions with defined base PA, and fill the base VA
2899056f108SAntonio Nino Diaz  * field on the array of structs. This function can only be used before
2909056f108SAntonio Nino Diaz  * initializing the translation tables. The regions cannot be removed afterwards.
2919056f108SAntonio Nino Diaz  */
2929056f108SAntonio Nino Diaz void mmap_add_alloc_va(mmap_region_t *mm);
293a9ad848cSSandrine Bailleux 
294a9ad848cSSandrine Bailleux #if PLAT_XLAT_TABLES_DYNAMIC
295a9ad848cSSandrine Bailleux /*
296e769db3eSAntonio Nino Diaz  * Add a dynamic region with defined base PA and base VA. This type of region
297e769db3eSAntonio Nino Diaz  * can be added and removed even after the translation tables are initialized.
2980b64f4efSAntonio Nino Diaz  *
2990b64f4efSAntonio Nino Diaz  * Returns:
3000b64f4efSAntonio Nino Diaz  *        0: Success.
3010b64f4efSAntonio Nino Diaz  *   EINVAL: Invalid values were used as arguments.
3020b64f4efSAntonio Nino Diaz  *   ERANGE: Memory limits were surpassed.
3030b64f4efSAntonio Nino Diaz  *   ENOMEM: Not enough space in the mmap array or not enough free xlat tables.
3040b64f4efSAntonio Nino Diaz  *    EPERM: It overlaps another region in an invalid way.
3050b64f4efSAntonio Nino Diaz  */
3060b64f4efSAntonio Nino Diaz int mmap_add_dynamic_region(unsigned long long base_pa, uintptr_t base_va,
3073a1b7b10SAntonio Nino Diaz 			    size_t size, unsigned int attr);
308a9ad848cSSandrine Bailleux int mmap_add_dynamic_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm);
3097bb01fb2SAntonio Nino Diaz 
3100b64f4efSAntonio Nino Diaz /*
3119056f108SAntonio Nino Diaz  * Add a dynamic region with defined base PA. Returns base VA calculated using
3129056f108SAntonio Nino Diaz  * the highest existing region in the mmap array even if it fails to allocate
3139056f108SAntonio Nino Diaz  * the region.
3149056f108SAntonio Nino Diaz  *
3159056f108SAntonio Nino Diaz  * mmap_add_dynamic_region_alloc_va() returns the allocated VA in 'base_va'.
3169056f108SAntonio Nino Diaz  * mmap_add_dynamic_region_alloc_va_ctx() returns it in 'mm->base_va'.
3179056f108SAntonio Nino Diaz  *
3189056f108SAntonio Nino Diaz  * It returns the same error values as mmap_add_dynamic_region().
3199056f108SAntonio Nino Diaz  */
3209056f108SAntonio Nino Diaz int mmap_add_dynamic_region_alloc_va(unsigned long long base_pa,
3219056f108SAntonio Nino Diaz 				     uintptr_t *base_va,
3229056f108SAntonio Nino Diaz 				     size_t size, unsigned int attr);
3239056f108SAntonio Nino Diaz int mmap_add_dynamic_region_alloc_va_ctx(xlat_ctx_t *ctx, mmap_region_t *mm);
3249056f108SAntonio Nino Diaz 
3259056f108SAntonio Nino Diaz /*
3260b64f4efSAntonio Nino Diaz  * Remove a region with the specified base VA and size. Only dynamic regions can
327e769db3eSAntonio Nino Diaz  * be removed, and they can be removed even if the translation tables are
328e769db3eSAntonio Nino Diaz  * initialized.
3290b64f4efSAntonio Nino Diaz  *
3300b64f4efSAntonio Nino Diaz  * Returns:
3310b64f4efSAntonio Nino Diaz  *        0: Success.
3320b64f4efSAntonio Nino Diaz  *   EINVAL: The specified region wasn't found.
3330b64f4efSAntonio Nino Diaz  *    EPERM: Trying to remove a static region.
3340b64f4efSAntonio Nino Diaz  */
3350b64f4efSAntonio Nino Diaz int mmap_remove_dynamic_region(uintptr_t base_va, size_t size);
336a9ad848cSSandrine Bailleux int mmap_remove_dynamic_region_ctx(xlat_ctx_t *ctx,
337a9ad848cSSandrine Bailleux 				uintptr_t base_va,
338a9ad848cSSandrine Bailleux 				size_t size);
339a9ad848cSSandrine Bailleux 
340a9ad848cSSandrine Bailleux #endif /* PLAT_XLAT_TABLES_DYNAMIC */
3410b64f4efSAntonio Nino Diaz 
3421be910bbSSandrine Bailleux /*
343996d6b39SSandrine Bailleux  * Change the memory attributes of the memory region starting from a given
344996d6b39SSandrine Bailleux  * virtual address in a set of translation tables.
345996d6b39SSandrine Bailleux  *
346996d6b39SSandrine Bailleux  * This function can only be used after the translation tables have been
347996d6b39SSandrine Bailleux  * initialized.
348996d6b39SSandrine Bailleux  *
349996d6b39SSandrine Bailleux  * The base address of the memory region must be aligned on a page boundary.
350996d6b39SSandrine Bailleux  * The size of this memory region must be a multiple of a page size.
351996d6b39SSandrine Bailleux  * The memory region must be already mapped by the given translation tables
352996d6b39SSandrine Bailleux  * and it must be mapped at the granularity of a page.
353996d6b39SSandrine Bailleux  *
354996d6b39SSandrine Bailleux  * Return 0 on success, a negative value on error.
355996d6b39SSandrine Bailleux  *
356996d6b39SSandrine Bailleux  * In case of error, the memory attributes remain unchanged and this function
357996d6b39SSandrine Bailleux  * has no effect.
358996d6b39SSandrine Bailleux  *
359996d6b39SSandrine Bailleux  * ctx
360996d6b39SSandrine Bailleux  *   Translation context to work on.
361996d6b39SSandrine Bailleux  * base_va:
362996d6b39SSandrine Bailleux  *   Virtual address of the 1st page to change the attributes of.
363996d6b39SSandrine Bailleux  * size:
364996d6b39SSandrine Bailleux  *   Size in bytes of the memory region.
365996d6b39SSandrine Bailleux  * attr:
366996d6b39SSandrine Bailleux  *   New attributes of the page tables. The attributes that can be changed are
367996d6b39SSandrine Bailleux  *   data access (MT_RO/MT_RW), instruction access (MT_EXECUTE_NEVER/MT_EXECUTE)
368996d6b39SSandrine Bailleux  *   and user/privileged access (MT_USER/MT_PRIVILEGED) in the case of contexts
369996d6b39SSandrine Bailleux  *   that are used in the EL1&0 translation regime. Also, note that this
370996d6b39SSandrine Bailleux  *   function doesn't allow to remap a region as RW and executable, or to remap
371996d6b39SSandrine Bailleux  *   device memory as executable.
372996d6b39SSandrine Bailleux  *
373996d6b39SSandrine Bailleux  * NOTE: The caller of this function must be able to write to the translation
374996d6b39SSandrine Bailleux  * tables, i.e. the memory where they are stored must be mapped with read-write
375996d6b39SSandrine Bailleux  * access permissions. This function assumes it is the case. If this is not
376996d6b39SSandrine Bailleux  * the case then this function might trigger a data abort exception.
377996d6b39SSandrine Bailleux  *
378996d6b39SSandrine Bailleux  * NOTE2: The caller is responsible for making sure that the targeted
379996d6b39SSandrine Bailleux  * translation tables are not modified by any other code while this function is
380996d6b39SSandrine Bailleux  * executing.
381996d6b39SSandrine Bailleux  */
382e5d59519SAntonio Nino Diaz int xlat_change_mem_attributes_ctx(const xlat_ctx_t *ctx, uintptr_t base_va,
383e5d59519SAntonio Nino Diaz 				   size_t size, uint32_t attr);
384e5d59519SAntonio Nino Diaz int xlat_change_mem_attributes(uintptr_t base_va, size_t size, uint32_t attr);
385996d6b39SSandrine Bailleux 
38660e8f3cfSPetre-Ionut Tudor #if PLAT_RO_XLAT_TABLES
38760e8f3cfSPetre-Ionut Tudor /*
38860e8f3cfSPetre-Ionut Tudor  * Change the memory attributes of the memory region encompassing the higher
38960e8f3cfSPetre-Ionut Tudor  * level translation tables to secure read-only data.
39060e8f3cfSPetre-Ionut Tudor  *
39160e8f3cfSPetre-Ionut Tudor  * Return 0 on success, a negative error code on error.
39260e8f3cfSPetre-Ionut Tudor  */
39360e8f3cfSPetre-Ionut Tudor int xlat_make_tables_readonly(void);
39460e8f3cfSPetre-Ionut Tudor #endif
39560e8f3cfSPetre-Ionut Tudor 
396996d6b39SSandrine Bailleux /*
3971be910bbSSandrine Bailleux  * Query the memory attributes of a memory page in a set of translation tables.
3981be910bbSSandrine Bailleux  *
3991be910bbSSandrine Bailleux  * Return 0 on success, a negative error code on error.
400e5d59519SAntonio Nino Diaz  * On success, the attributes are stored into *attr.
4011be910bbSSandrine Bailleux  *
4021be910bbSSandrine Bailleux  * ctx
4031be910bbSSandrine Bailleux  *   Translation context to work on.
4041be910bbSSandrine Bailleux  * base_va
4051be910bbSSandrine Bailleux  *   Virtual address of the page to get the attributes of.
4061be910bbSSandrine Bailleux  *   There are no alignment restrictions on this address. The attributes of the
4071be910bbSSandrine Bailleux  *   memory page it lies within are returned.
408e5d59519SAntonio Nino Diaz  * attr
4091be910bbSSandrine Bailleux  *   Output parameter where to store the attributes of the targeted memory page.
410*9bfe78c2SLevi Yun  * table_level
411*9bfe78c2SLevi Yun  *   Output parameter where to store base_va's table level
4121be910bbSSandrine Bailleux  */
413e5d59519SAntonio Nino Diaz int xlat_get_mem_attributes_ctx(const xlat_ctx_t *ctx, uintptr_t base_va,
414*9bfe78c2SLevi Yun 				uint32_t *attr, unsigned int *table_level);
415e5d59519SAntonio Nino Diaz int xlat_get_mem_attributes(uintptr_t base_va, uint32_t *attr);
4161be910bbSSandrine Bailleux 
417d5dfdeb6SJulius Werner #endif /*__ASSEMBLER__*/
418e7b9886cSAntonio Nino Diaz #endif /* XLAT_TABLES_V2_H */
419