xref: /rk3399_ARM-atf/include/lib/xlat_tables/xlat_tables_v2.h (revision c283e05af1b7a287cd8edee53603c6d015330aff)
1 /*
2  * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef __XLAT_TABLES_V2_H__
8 #define __XLAT_TABLES_V2_H__
9 
10 #include <xlat_tables_defs.h>
11 
12 #ifndef __ASSEMBLY__
13 #include <stddef.h>
14 #include <stdint.h>
15 #include <xlat_mmu_helpers.h>
16 
17 /* Helper macro to define entries for mmap_region_t. It creates
18  * identity mappings for each region.
19  */
20 #define MAP_REGION_FLAT(adr, sz, attr) MAP_REGION(adr, adr, sz, attr)
21 
22 /* Helper macro to define entries for mmap_region_t. It allows to
23  * re-map address mappings from 'pa' to 'va' for each region.
24  */
25 #define MAP_REGION(pa, va, sz, attr) {(pa), (va), (sz), (attr)}
26 
27 /*
28  * Shifts and masks to access fields of an mmap_attr_t
29  */
30 #define MT_TYPE_MASK		U(0x7)
31 #define MT_TYPE(_attr)		((_attr) & MT_TYPE_MASK)
32 /* Access permissions (RO/RW) */
33 #define MT_PERM_SHIFT		U(3)
34 /* Security state (SECURE/NS) */
35 #define MT_SEC_SHIFT		U(4)
36 /* Access permissions for instruction execution (EXECUTE/EXECUTE_NEVER) */
37 #define MT_EXECUTE_SHIFT	U(5)
38 /* All other bits are reserved */
39 
40 /*
41  * Memory mapping attributes
42  */
43 typedef enum  {
44 	/*
45 	 * Memory types supported.
46 	 * These are organised so that, going down the list, the memory types
47 	 * are getting weaker; conversely going up the list the memory types are
48 	 * getting stronger.
49 	 */
50 	MT_DEVICE,
51 	MT_NON_CACHEABLE,
52 	MT_MEMORY,
53 	/* Values up to 7 are reserved to add new memory types in the future */
54 
55 	MT_RO		= U(0) << MT_PERM_SHIFT,
56 	MT_RW		= U(1) << MT_PERM_SHIFT,
57 
58 	MT_SECURE	= U(0) << MT_SEC_SHIFT,
59 	MT_NS		= U(1) << MT_SEC_SHIFT,
60 
61 	/*
62 	 * Access permissions for instruction execution are only relevant for
63 	 * normal read-only memory, i.e. MT_MEMORY | MT_RO. They are ignored
64 	 * (and potentially overridden) otherwise:
65 	 *  - Device memory is always marked as execute-never.
66 	 *  - Read-write normal memory is always marked as execute-never.
67 	 */
68 	MT_EXECUTE		= U(0) << MT_EXECUTE_SHIFT,
69 	MT_EXECUTE_NEVER	= U(1) << MT_EXECUTE_SHIFT,
70 } mmap_attr_t;
71 
72 #define MT_CODE		(MT_MEMORY | MT_RO | MT_EXECUTE)
73 #define MT_RO_DATA	(MT_MEMORY | MT_RO | MT_EXECUTE_NEVER)
74 
75 /*
76  * Structure for specifying a single region of memory.
77  */
78 typedef struct mmap_region {
79 	unsigned long long	base_pa;
80 	uintptr_t		base_va;
81 	size_t			size;
82 	mmap_attr_t		attr;
83 } mmap_region_t;
84 
85 /* Generic translation table APIs */
86 
87 /*
88  * Initialize translation tables from the current list of mmap regions. Calling
89  * this function marks the transition point after which static regions can no
90  * longer be added.
91  */
92 void init_xlat_tables(void);
93 
94 /*
95  * Add a static region with defined base PA and base VA. This function can only
96  * be used before initializing the translation tables. The region cannot be
97  * removed afterwards.
98  */
99 void mmap_add_region(unsigned long long base_pa, uintptr_t base_va,
100 				size_t size, mmap_attr_t attr);
101 
102 /*
103  * Add a dynamic region with defined base PA and base VA. This type of region
104  * can be added and removed even after the translation tables are initialized.
105  *
106  * Returns:
107  *        0: Success.
108  *   EINVAL: Invalid values were used as arguments.
109  *   ERANGE: Memory limits were surpassed.
110  *   ENOMEM: Not enough space in the mmap array or not enough free xlat tables.
111  *    EPERM: It overlaps another region in an invalid way.
112  */
113 int mmap_add_dynamic_region(unsigned long long base_pa, uintptr_t base_va,
114 				size_t size, mmap_attr_t attr);
115 
116 /*
117  * Add an array of static regions with defined base PA and base VA. This
118  * function can only be used before initializing the translation tables. The
119  * regions cannot be removed afterwards.
120  */
121 void mmap_add(const mmap_region_t *mm);
122 
123 /*
124  * Remove a region with the specified base VA and size. Only dynamic regions can
125  * be removed, and they can be removed even if the translation tables are
126  * initialized.
127  *
128  * Returns:
129  *        0: Success.
130  *   EINVAL: The specified region wasn't found.
131  *    EPERM: Trying to remove a static region.
132  */
133 int mmap_remove_dynamic_region(uintptr_t base_va, size_t size);
134 
135 #endif /*__ASSEMBLY__*/
136 #endif /* __XLAT_TABLES_V2_H__ */
137