xref: /rk3399_ARM-atf/include/lib/xlat_tables/xlat_tables_v2.h (revision fdb1964c34968921379d3592e7ac6e9a685dbab1)
17bb01fb2SAntonio Nino Diaz /*
27bb01fb2SAntonio Nino Diaz  * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved.
37bb01fb2SAntonio Nino Diaz  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
57bb01fb2SAntonio Nino Diaz  */
67bb01fb2SAntonio Nino Diaz 
77bb01fb2SAntonio Nino Diaz #ifndef __XLAT_TABLES_V2_H__
87bb01fb2SAntonio Nino Diaz #define __XLAT_TABLES_V2_H__
97bb01fb2SAntonio Nino Diaz 
107bb01fb2SAntonio Nino Diaz #include <xlat_tables_defs.h>
117bb01fb2SAntonio Nino Diaz 
127bb01fb2SAntonio Nino Diaz #ifndef __ASSEMBLY__
137bb01fb2SAntonio Nino Diaz #include <stddef.h>
147bb01fb2SAntonio Nino Diaz #include <stdint.h>
157bb01fb2SAntonio Nino Diaz #include <xlat_mmu_helpers.h>
1655c84964SSandrine Bailleux #include <xlat_tables_v2_helpers.h>
177bb01fb2SAntonio Nino Diaz 
18*fdb1964cSSandrine Bailleux /*
19*fdb1964cSSandrine Bailleux  * Default granularity size for an mmap_region_t.
20*fdb1964cSSandrine Bailleux  * Useful when no specific granularity is required.
21*fdb1964cSSandrine Bailleux  *
22*fdb1964cSSandrine Bailleux  * By default, choose the biggest possible block size allowed by the
23*fdb1964cSSandrine Bailleux  * architectural state and granule size in order to minimize the number of page
24*fdb1964cSSandrine Bailleux  * tables required for the mapping.
257bb01fb2SAntonio Nino Diaz  */
26*fdb1964cSSandrine Bailleux #define REGION_DEFAULT_GRANULARITY	XLAT_BLOCK_SIZE(MIN_LVL_BLOCK_DESC)
277bb01fb2SAntonio Nino Diaz 
28*fdb1964cSSandrine Bailleux /* Helper macro to define an mmap_region_t. */
29*fdb1964cSSandrine Bailleux #define MAP_REGION(_pa, _va, _sz, _attr)	\
30*fdb1964cSSandrine Bailleux 	_MAP_REGION_FULL_SPEC(_pa, _va, _sz, _attr, REGION_DEFAULT_GRANULARITY)
31*fdb1964cSSandrine Bailleux 
32*fdb1964cSSandrine Bailleux /* Helper macro to define an mmap_region_t with an identity mapping. */
33*fdb1964cSSandrine Bailleux #define MAP_REGION_FLAT(_adr, _sz, _attr)			\
34*fdb1964cSSandrine Bailleux 	MAP_REGION(_adr, _adr, _sz, _attr)
35*fdb1964cSSandrine Bailleux 
36*fdb1964cSSandrine Bailleux /*
37*fdb1964cSSandrine Bailleux  * Helper macro to define an mmap_region_t to map with the desired granularity
38*fdb1964cSSandrine Bailleux  * of translation tables.
39*fdb1964cSSandrine Bailleux  *
40*fdb1964cSSandrine Bailleux  * The granularity value passed to this macro must be a valid block or page
41*fdb1964cSSandrine Bailleux  * size. When using a 4KB translation granule, this might be 4KB, 2MB or 1GB.
42*fdb1964cSSandrine Bailleux  * Passing REGION_DEFAULT_GRANULARITY is also allowed and means that the library
43*fdb1964cSSandrine Bailleux  * is free to choose the granularity for this region. In this case, it is
44*fdb1964cSSandrine Bailleux  * equivalent to the MAP_REGION() macro.
457bb01fb2SAntonio Nino Diaz  */
46*fdb1964cSSandrine Bailleux #define MAP_REGION2(_pa, _va, _sz, _attr, _gr)			\
47*fdb1964cSSandrine Bailleux 	_MAP_REGION_FULL_SPEC(_pa, _va, _sz, _attr, _gr)
487bb01fb2SAntonio Nino Diaz 
497bb01fb2SAntonio Nino Diaz /*
507bb01fb2SAntonio Nino Diaz  * Shifts and masks to access fields of an mmap_attr_t
517bb01fb2SAntonio Nino Diaz  */
52030567e6SVarun Wadekar #define MT_TYPE_MASK		U(0x7)
537bb01fb2SAntonio Nino Diaz #define MT_TYPE(_attr)		((_attr) & MT_TYPE_MASK)
547bb01fb2SAntonio Nino Diaz /* Access permissions (RO/RW) */
55030567e6SVarun Wadekar #define MT_PERM_SHIFT		U(3)
567bb01fb2SAntonio Nino Diaz /* Security state (SECURE/NS) */
57030567e6SVarun Wadekar #define MT_SEC_SHIFT		U(4)
587bb01fb2SAntonio Nino Diaz /* Access permissions for instruction execution (EXECUTE/EXECUTE_NEVER) */
59030567e6SVarun Wadekar #define MT_EXECUTE_SHIFT	U(5)
600b64f4efSAntonio Nino Diaz /* All other bits are reserved */
617bb01fb2SAntonio Nino Diaz 
627bb01fb2SAntonio Nino Diaz /*
637bb01fb2SAntonio Nino Diaz  * Memory mapping attributes
647bb01fb2SAntonio Nino Diaz  */
657bb01fb2SAntonio Nino Diaz typedef enum  {
667bb01fb2SAntonio Nino Diaz 	/*
677bb01fb2SAntonio Nino Diaz 	 * Memory types supported.
687bb01fb2SAntonio Nino Diaz 	 * These are organised so that, going down the list, the memory types
697bb01fb2SAntonio Nino Diaz 	 * are getting weaker; conversely going up the list the memory types are
707bb01fb2SAntonio Nino Diaz 	 * getting stronger.
717bb01fb2SAntonio Nino Diaz 	 */
727bb01fb2SAntonio Nino Diaz 	MT_DEVICE,
737bb01fb2SAntonio Nino Diaz 	MT_NON_CACHEABLE,
747bb01fb2SAntonio Nino Diaz 	MT_MEMORY,
757bb01fb2SAntonio Nino Diaz 	/* Values up to 7 are reserved to add new memory types in the future */
767bb01fb2SAntonio Nino Diaz 
77030567e6SVarun Wadekar 	MT_RO		= U(0) << MT_PERM_SHIFT,
78030567e6SVarun Wadekar 	MT_RW		= U(1) << MT_PERM_SHIFT,
797bb01fb2SAntonio Nino Diaz 
80030567e6SVarun Wadekar 	MT_SECURE	= U(0) << MT_SEC_SHIFT,
81030567e6SVarun Wadekar 	MT_NS		= U(1) << MT_SEC_SHIFT,
827bb01fb2SAntonio Nino Diaz 
837bb01fb2SAntonio Nino Diaz 	/*
847bb01fb2SAntonio Nino Diaz 	 * Access permissions for instruction execution are only relevant for
857bb01fb2SAntonio Nino Diaz 	 * normal read-only memory, i.e. MT_MEMORY | MT_RO. They are ignored
867bb01fb2SAntonio Nino Diaz 	 * (and potentially overridden) otherwise:
877bb01fb2SAntonio Nino Diaz 	 *  - Device memory is always marked as execute-never.
887bb01fb2SAntonio Nino Diaz 	 *  - Read-write normal memory is always marked as execute-never.
897bb01fb2SAntonio Nino Diaz 	 */
90030567e6SVarun Wadekar 	MT_EXECUTE		= U(0) << MT_EXECUTE_SHIFT,
91030567e6SVarun Wadekar 	MT_EXECUTE_NEVER	= U(1) << MT_EXECUTE_SHIFT,
927bb01fb2SAntonio Nino Diaz } mmap_attr_t;
937bb01fb2SAntonio Nino Diaz 
947bb01fb2SAntonio Nino Diaz #define MT_CODE		(MT_MEMORY | MT_RO | MT_EXECUTE)
957bb01fb2SAntonio Nino Diaz #define MT_RO_DATA	(MT_MEMORY | MT_RO | MT_EXECUTE_NEVER)
967bb01fb2SAntonio Nino Diaz 
977bb01fb2SAntonio Nino Diaz /*
987bb01fb2SAntonio Nino Diaz  * Structure for specifying a single region of memory.
997bb01fb2SAntonio Nino Diaz  */
1007bb01fb2SAntonio Nino Diaz typedef struct mmap_region {
1017bb01fb2SAntonio Nino Diaz 	unsigned long long	base_pa;
1027bb01fb2SAntonio Nino Diaz 	uintptr_t		base_va;
1037bb01fb2SAntonio Nino Diaz 	size_t			size;
1047bb01fb2SAntonio Nino Diaz 	mmap_attr_t		attr;
105*fdb1964cSSandrine Bailleux 	/* Desired granularity. See the MAP_REGION2() macro for more details. */
106*fdb1964cSSandrine Bailleux 	size_t			granularity;
1077bb01fb2SAntonio Nino Diaz } mmap_region_t;
1087bb01fb2SAntonio Nino Diaz 
10955c84964SSandrine Bailleux /*
11055c84964SSandrine Bailleux  * Declare the translation context type.
11155c84964SSandrine Bailleux  * Its definition is private.
11255c84964SSandrine Bailleux  */
11355c84964SSandrine Bailleux typedef struct xlat_ctx xlat_ctx_t;
11455c84964SSandrine Bailleux 
11555c84964SSandrine Bailleux /*
11655c84964SSandrine Bailleux  * Statically allocate a translation context and associated structures. Also
11755c84964SSandrine Bailleux  * initialize them.
11855c84964SSandrine Bailleux  *
11955c84964SSandrine Bailleux  * _ctx_name:
12055c84964SSandrine Bailleux  *   Prefix for the translation context variable.
12155c84964SSandrine Bailleux  *   E.g. If _ctx_name is 'foo', the variable will be called 'foo_xlat_ctx'.
12255c84964SSandrine Bailleux  *   Useful to distinguish multiple contexts from one another.
12355c84964SSandrine Bailleux  *
12455c84964SSandrine Bailleux  * _mmap_count:
12555c84964SSandrine Bailleux  *   Number of mmap_region_t to allocate.
12655c84964SSandrine Bailleux  *   Would typically be MAX_MMAP_REGIONS for the translation context describing
12755c84964SSandrine Bailleux  *   the BL image currently executing.
12855c84964SSandrine Bailleux  *
12955c84964SSandrine Bailleux  * _xlat_tables_count:
13055c84964SSandrine Bailleux  *   Number of sub-translation tables to allocate.
13155c84964SSandrine Bailleux  *   Would typically be MAX_XLAT_TABLES for the translation context describing
13255c84964SSandrine Bailleux  *   the BL image currently executing.
13355c84964SSandrine Bailleux  *   Note that this is only for sub-tables ; at the initial lookup level, there
13455c84964SSandrine Bailleux  *   is always a single table.
13555c84964SSandrine Bailleux  *
13655c84964SSandrine Bailleux  * _virt_addr_space_size, _phy_addr_space_size:
13755c84964SSandrine Bailleux  *   Size (in bytes) of the virtual (resp. physical) address space.
13855c84964SSandrine Bailleux  *   Would typically be PLAT_VIRT_ADDR_SPACE_SIZE
13955c84964SSandrine Bailleux  *   (resp. PLAT_PHY_ADDR_SPACE_SIZE) for the translation context describing the
14055c84964SSandrine Bailleux  *   BL image currently executing.
14155c84964SSandrine Bailleux  */
14255c84964SSandrine Bailleux #define REGISTER_XLAT_CONTEXT(_ctx_name, _mmap_count, _xlat_tables_count,	\
14355c84964SSandrine Bailleux 			_virt_addr_space_size, _phy_addr_space_size)		\
14455c84964SSandrine Bailleux 	_REGISTER_XLAT_CONTEXT(_ctx_name, _mmap_count, _xlat_tables_count,	\
14555c84964SSandrine Bailleux 		_virt_addr_space_size, _phy_addr_space_size)
14655c84964SSandrine Bailleux 
147a9ad848cSSandrine Bailleux /******************************************************************************
148a9ad848cSSandrine Bailleux  * Generic translation table APIs.
149a9ad848cSSandrine Bailleux  * Each API comes in 2 variants:
150a9ad848cSSandrine Bailleux  * - one that acts on the current translation context for this BL image
151a9ad848cSSandrine Bailleux  * - another that acts on the given translation context instead. This variant
152a9ad848cSSandrine Bailleux  *   is named after the 1st version, with an additional '_ctx' suffix.
153a9ad848cSSandrine Bailleux  *****************************************************************************/
154e769db3eSAntonio Nino Diaz 
155e769db3eSAntonio Nino Diaz /*
156e769db3eSAntonio Nino Diaz  * Initialize translation tables from the current list of mmap regions. Calling
157e769db3eSAntonio Nino Diaz  * this function marks the transition point after which static regions can no
158e769db3eSAntonio Nino Diaz  * longer be added.
159e769db3eSAntonio Nino Diaz  */
1607bb01fb2SAntonio Nino Diaz void init_xlat_tables(void);
161a9ad848cSSandrine Bailleux void init_xlat_tables_ctx(xlat_ctx_t *ctx);
1627bb01fb2SAntonio Nino Diaz 
1637bb01fb2SAntonio Nino Diaz /*
164e769db3eSAntonio Nino Diaz  * Add a static region with defined base PA and base VA. This function can only
165e769db3eSAntonio Nino Diaz  * be used before initializing the translation tables. The region cannot be
166e769db3eSAntonio Nino Diaz  * removed afterwards.
1677bb01fb2SAntonio Nino Diaz  */
1687bb01fb2SAntonio Nino Diaz void mmap_add_region(unsigned long long base_pa, uintptr_t base_va,
16928fa2e9eSSandrine Bailleux 				size_t size, mmap_attr_t attr);
170a9ad848cSSandrine Bailleux void mmap_add_region_ctx(xlat_ctx_t *ctx, const mmap_region_t *mm);
1717bb01fb2SAntonio Nino Diaz 
1727bb01fb2SAntonio Nino Diaz /*
173a9ad848cSSandrine Bailleux  * Add an array of static regions with defined base PA and base VA. This
174a9ad848cSSandrine Bailleux  * function can only be used before initializing the translation tables. The
175a9ad848cSSandrine Bailleux  * regions cannot be removed afterwards.
176a9ad848cSSandrine Bailleux  */
177a9ad848cSSandrine Bailleux void mmap_add(const mmap_region_t *mm);
178a9ad848cSSandrine Bailleux void mmap_add_ctx(xlat_ctx_t *ctx, const mmap_region_t *mm);
179a9ad848cSSandrine Bailleux 
180a9ad848cSSandrine Bailleux 
181a9ad848cSSandrine Bailleux #if PLAT_XLAT_TABLES_DYNAMIC
182a9ad848cSSandrine Bailleux /*
183e769db3eSAntonio Nino Diaz  * Add a dynamic region with defined base PA and base VA. This type of region
184e769db3eSAntonio Nino Diaz  * can be added and removed even after the translation tables are initialized.
1850b64f4efSAntonio Nino Diaz  *
1860b64f4efSAntonio Nino Diaz  * Returns:
1870b64f4efSAntonio Nino Diaz  *        0: Success.
1880b64f4efSAntonio Nino Diaz  *   EINVAL: Invalid values were used as arguments.
1890b64f4efSAntonio Nino Diaz  *   ERANGE: Memory limits were surpassed.
1900b64f4efSAntonio Nino Diaz  *   ENOMEM: Not enough space in the mmap array or not enough free xlat tables.
1910b64f4efSAntonio Nino Diaz  *    EPERM: It overlaps another region in an invalid way.
1920b64f4efSAntonio Nino Diaz  */
1930b64f4efSAntonio Nino Diaz int mmap_add_dynamic_region(unsigned long long base_pa, uintptr_t base_va,
19428fa2e9eSSandrine Bailleux 				size_t size, mmap_attr_t attr);
195a9ad848cSSandrine Bailleux int mmap_add_dynamic_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm);
1967bb01fb2SAntonio Nino Diaz 
1970b64f4efSAntonio Nino Diaz /*
1980b64f4efSAntonio Nino Diaz  * Remove a region with the specified base VA and size. Only dynamic regions can
199e769db3eSAntonio Nino Diaz  * be removed, and they can be removed even if the translation tables are
200e769db3eSAntonio Nino Diaz  * initialized.
2010b64f4efSAntonio Nino Diaz  *
2020b64f4efSAntonio Nino Diaz  * Returns:
2030b64f4efSAntonio Nino Diaz  *        0: Success.
2040b64f4efSAntonio Nino Diaz  *   EINVAL: The specified region wasn't found.
2050b64f4efSAntonio Nino Diaz  *    EPERM: Trying to remove a static region.
2060b64f4efSAntonio Nino Diaz  */
2070b64f4efSAntonio Nino Diaz int mmap_remove_dynamic_region(uintptr_t base_va, size_t size);
208a9ad848cSSandrine Bailleux int mmap_remove_dynamic_region_ctx(xlat_ctx_t *ctx,
209a9ad848cSSandrine Bailleux 				uintptr_t base_va,
210a9ad848cSSandrine Bailleux 				size_t size);
211a9ad848cSSandrine Bailleux 
212a9ad848cSSandrine Bailleux #endif /* PLAT_XLAT_TABLES_DYNAMIC */
2130b64f4efSAntonio Nino Diaz 
2147bb01fb2SAntonio Nino Diaz #endif /*__ASSEMBLY__*/
2157bb01fb2SAntonio Nino Diaz #endif /* __XLAT_TABLES_V2_H__ */
216