xref: /rk3399_ARM-atf/include/lib/xlat_tables/xlat_tables_v2.h (revision 51faada71a219a8b94cd8d8e423f0f22e9da4d8f)
1 /*
2  * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of ARM nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without specific
16  * prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef __XLAT_TABLES_V2_H__
32 #define __XLAT_TABLES_V2_H__
33 
34 #include <xlat_tables_defs.h>
35 
36 #ifndef __ASSEMBLY__
37 #include <stddef.h>
38 #include <stdint.h>
39 #include <xlat_mmu_helpers.h>
40 
41 /* Helper macro to define entries for mmap_region_t. It creates
42  * identity mappings for each region.
43  */
44 #define MAP_REGION_FLAT(adr, sz, attr) MAP_REGION(adr, adr, sz, attr)
45 
46 /* Helper macro to define entries for mmap_region_t. It allows to
47  * re-map address mappings from 'pa' to 'va' for each region.
48  */
49 #define MAP_REGION(pa, va, sz, attr) {(pa), (va), (sz), (attr)}
50 
51 /*
52  * Shifts and masks to access fields of an mmap_attr_t
53  */
54 #define MT_TYPE_MASK		0x7
55 #define MT_TYPE(_attr)		((_attr) & MT_TYPE_MASK)
56 /* Access permissions (RO/RW) */
57 #define MT_PERM_SHIFT		3
58 /* Security state (SECURE/NS) */
59 #define MT_SEC_SHIFT		4
60 /* Access permissions for instruction execution (EXECUTE/EXECUTE_NEVER) */
61 #define MT_EXECUTE_SHIFT	5
62 /* All other bits are reserved */
63 
64 /*
65  * Memory mapping attributes
66  */
67 typedef enum  {
68 	/*
69 	 * Memory types supported.
70 	 * These are organised so that, going down the list, the memory types
71 	 * are getting weaker; conversely going up the list the memory types are
72 	 * getting stronger.
73 	 */
74 	MT_DEVICE,
75 	MT_NON_CACHEABLE,
76 	MT_MEMORY,
77 	/* Values up to 7 are reserved to add new memory types in the future */
78 
79 	MT_RO		= 0 << MT_PERM_SHIFT,
80 	MT_RW		= 1 << MT_PERM_SHIFT,
81 
82 	MT_SECURE	= 0 << MT_SEC_SHIFT,
83 	MT_NS		= 1 << MT_SEC_SHIFT,
84 
85 	/*
86 	 * Access permissions for instruction execution are only relevant for
87 	 * normal read-only memory, i.e. MT_MEMORY | MT_RO. They are ignored
88 	 * (and potentially overridden) otherwise:
89 	 *  - Device memory is always marked as execute-never.
90 	 *  - Read-write normal memory is always marked as execute-never.
91 	 */
92 	MT_EXECUTE		= 0 << MT_EXECUTE_SHIFT,
93 	MT_EXECUTE_NEVER	= 1 << MT_EXECUTE_SHIFT,
94 } mmap_attr_t;
95 
96 #define MT_CODE		(MT_MEMORY | MT_RO | MT_EXECUTE)
97 #define MT_RO_DATA	(MT_MEMORY | MT_RO | MT_EXECUTE_NEVER)
98 
99 /*
100  * Structure for specifying a single region of memory.
101  */
102 typedef struct mmap_region {
103 	unsigned long long	base_pa;
104 	uintptr_t		base_va;
105 	size_t			size;
106 	mmap_attr_t		attr;
107 } mmap_region_t;
108 
109 /* Generic translation table APIs */
110 void init_xlat_tables(void);
111 
112 /*
113  * Add a region with defined base PA and base VA. This type of region can only
114  * be added before initializing the MMU and cannot be removed later.
115  */
116 void mmap_add_region(unsigned long long base_pa, uintptr_t base_va,
117 				size_t size, unsigned int attr);
118 
119 /*
120  * Add a region with defined base PA and base VA. This type of region can be
121  * added and removed even if the MMU is enabled.
122  *
123  * Returns:
124  *        0: Success.
125  *   EINVAL: Invalid values were used as arguments.
126  *   ERANGE: Memory limits were surpassed.
127  *   ENOMEM: Not enough space in the mmap array or not enough free xlat tables.
128  *    EPERM: It overlaps another region in an invalid way.
129  */
130 int mmap_add_dynamic_region(unsigned long long base_pa, uintptr_t base_va,
131 				size_t size, unsigned int attr);
132 
133 /*
134  * Add an array of static regions with defined base PA and base VA. This type
135  * of region can only be added before initializing the MMU and cannot be
136  * removed later.
137  */
138 void mmap_add(const mmap_region_t *mm);
139 
140 /*
141  * Remove a region with the specified base VA and size. Only dynamic regions can
142  * be removed, and they can be removed even if the MMU is enabled.
143  *
144  * Returns:
145  *        0: Success.
146  *   EINVAL: The specified region wasn't found.
147  *    EPERM: Trying to remove a static region.
148  */
149 int mmap_remove_dynamic_region(uintptr_t base_va, size_t size);
150 
151 #endif /*__ASSEMBLY__*/
152 #endif /* __XLAT_TABLES_V2_H__ */
153