xref: /rk3399_ARM-atf/include/lib/xlat_tables/xlat_tables_v2.h (revision 82cb2c1ad9897473743f08437d0a3995bed561b9)
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		0x7
31 #define MT_TYPE(_attr)		((_attr) & MT_TYPE_MASK)
32 /* Access permissions (RO/RW) */
33 #define MT_PERM_SHIFT		3
34 /* Security state (SECURE/NS) */
35 #define MT_SEC_SHIFT		4
36 /* Access permissions for instruction execution (EXECUTE/EXECUTE_NEVER) */
37 #define MT_EXECUTE_SHIFT	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		= 0 << MT_PERM_SHIFT,
56 	MT_RW		= 1 << MT_PERM_SHIFT,
57 
58 	MT_SECURE	= 0 << MT_SEC_SHIFT,
59 	MT_NS		= 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		= 0 << MT_EXECUTE_SHIFT,
69 	MT_EXECUTE_NEVER	= 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 void init_xlat_tables(void);
87 
88 /*
89  * Add a region with defined base PA and base VA. This type of region can only
90  * be added before initializing the MMU and cannot be removed later.
91  */
92 void mmap_add_region(unsigned long long base_pa, uintptr_t base_va,
93 				size_t size, mmap_attr_t attr);
94 
95 /*
96  * Add a region with defined base PA and base VA. This type of region can be
97  * added and removed even if the MMU is enabled.
98  *
99  * Returns:
100  *        0: Success.
101  *   EINVAL: Invalid values were used as arguments.
102  *   ERANGE: Memory limits were surpassed.
103  *   ENOMEM: Not enough space in the mmap array or not enough free xlat tables.
104  *    EPERM: It overlaps another region in an invalid way.
105  */
106 int mmap_add_dynamic_region(unsigned long long base_pa, uintptr_t base_va,
107 				size_t size, mmap_attr_t attr);
108 
109 /*
110  * Add an array of static regions with defined base PA and base VA. This type
111  * of region can only be added before initializing the MMU and cannot be
112  * removed later.
113  */
114 void mmap_add(const mmap_region_t *mm);
115 
116 /*
117  * Remove a region with the specified base VA and size. Only dynamic regions can
118  * be removed, and they can be removed even if the MMU is enabled.
119  *
120  * Returns:
121  *        0: Success.
122  *   EINVAL: The specified region wasn't found.
123  *    EPERM: Trying to remove a static region.
124  */
125 int mmap_remove_dynamic_region(uintptr_t base_va, size_t size);
126 
127 #endif /*__ASSEMBLY__*/
128 #endif /* __XLAT_TABLES_V2_H__ */
129