1 /** @file
2 *
3 *  Copyright (c) 2011-2013, ARM Limited. All rights reserved.
4 *
5 *  This program and the accompanying materials
6 *  are licensed and made available under the terms and conditions of the BSD License
7 *  which accompanies this distribution.  The full text of the license may be found at
8 *  http://opensource.org/licenses/bsd-license.php
9 *
10 *  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 *  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 *
13 **/
14 
15 #ifndef __ARMV7_MMU_H_
16 #define __ARMV7_MMU_H_
17 
18 #define TTBR_NOT_OUTER_SHAREABLE             BIT5
19 #define TTBR_RGN_OUTER_NON_CACHEABLE         0
20 #define TTBR_RGN_OUTER_WRITE_BACK_ALLOC      BIT3
21 #define TTBR_RGN_OUTER_WRITE_THROUGH         BIT4
22 #define TTBR_RGN_OUTER_WRITE_BACK_NO_ALLOC   (BIT3|BIT4)
23 #define TTBR_SHAREABLE                       BIT1
24 #define TTBR_NON_SHAREABLE                   0
25 #define TTBR_INNER_CACHEABLE                 BIT0
26 #define TTBR_INNER_NON_CACHEABLE             0
27 #define TTBR_RGN_INNER_NON_CACHEABLE         0
28 #define TTBR_RGN_INNER_WRITE_BACK_ALLOC      BIT6
29 #define TTBR_RGN_INNER_WRITE_THROUGH         BIT0
30 #define TTBR_RGN_INNER_WRITE_BACK_NO_ALLOC   (BIT0|BIT6)
31 
32 #define TTBR_WRITE_THROUGH              ( TTBR_RGN_OUTER_WRITE_THROUGH | TTBR_INNER_CACHEABLE | TTBR_SHAREABLE)
33 #define TTBR_WRITE_BACK_NO_ALLOC        ( TTBR_RGN_OUTER_WRITE_BACK_NO_ALLOC | TTBR_INNER_CACHEABLE | TTBR_SHAREABLE)
34 #define TTBR_NON_CACHEABLE              ( TTBR_RGN_OUTER_NON_CACHEABLE | TTBR_INNER_NON_CACHEABLE )
35 #define TTBR_WRITE_BACK_ALLOC           ( TTBR_RGN_OUTER_WRITE_BACK_ALLOC | TTBR_INNER_CACHEABLE | TTBR_SHAREABLE)
36 
37 #define TTBR_MP_WRITE_THROUGH           ( TTBR_RGN_OUTER_WRITE_THROUGH | TTBR_RGN_INNER_WRITE_THROUGH | TTBR_SHAREABLE)
38 #define TTBR_MP_WRITE_BACK_NO_ALLOC     ( TTBR_RGN_OUTER_WRITE_BACK_NO_ALLOC | TTBR_RGN_INNER_WRITE_BACK_NO_ALLOC | TTBR_SHAREABLE)
39 #define TTBR_MP_NON_CACHEABLE           ( TTBR_RGN_OUTER_NON_CACHEABLE | TTBR_RGN_INNER_NON_CACHEABLE )
40 #define TTBR_MP_WRITE_BACK_ALLOC        ( TTBR_RGN_OUTER_WRITE_BACK_ALLOC | TTBR_RGN_INNER_WRITE_BACK_ALLOC | TTBR_SHAREABLE)
41 
42 
43 #define TRANSLATION_TABLE_SECTION_COUNT                 4096
44 #define TRANSLATION_TABLE_SECTION_SIZE                  (sizeof(UINT32) * TRANSLATION_TABLE_SECTION_COUNT)
45 #define TRANSLATION_TABLE_SECTION_ALIGNMENT             (sizeof(UINT32) * TRANSLATION_TABLE_SECTION_COUNT)
46 #define TRANSLATION_TABLE_SECTION_ALIGNMENT_MASK        (TRANSLATION_TABLE_SECTION_ALIGNMENT - 1)
47 
48 #define TRANSLATION_TABLE_PAGE_COUNT                 256
49 #define TRANSLATION_TABLE_PAGE_SIZE                  (sizeof(UINT32) * TRANSLATION_TABLE_PAGE_COUNT)
50 #define TRANSLATION_TABLE_PAGE_ALIGNMENT             (sizeof(UINT32) * TRANSLATION_TABLE_PAGE_COUNT)
51 #define TRANSLATION_TABLE_PAGE_ALIGNMENT_MASK        (TRANSLATION_TABLE_PAGE_ALIGNMENT - 1)
52 
53 #define TRANSLATION_TABLE_ENTRY_FOR_VIRTUAL_ADDRESS(table, address) ((UINT32 *)(table) + (((UINTN)(address)) >> 20))
54 
55 // Translation table descriptor types
56 #define TT_DESCRIPTOR_SECTION_TYPE_MASK         ((1UL << 18) | (3UL << 0))
57 #define TT_DESCRIPTOR_SECTION_TYPE_FAULT        (0UL << 0)
58 #define TT_DESCRIPTOR_SECTION_TYPE_PAGE_TABLE   (1UL << 0)
59 #define TT_DESCRIPTOR_SECTION_TYPE_SECTION      ((0UL << 18) | (2UL << 0))
60 #define TT_DESCRIPTOR_SECTION_TYPE_SUPERSECTION ((1UL << 18) | (2UL << 0))
61 #define TT_DESCRIPTOR_SECTION_TYPE_IS_PAGE_TABLE(Desc) (((Desc) & 3UL) == TT_DESCRIPTOR_SECTION_TYPE_PAGE_TABLE)
62 
63 // Translation table descriptor types
64 #define TT_DESCRIPTOR_PAGE_TYPE_MASK         (3UL << 0)
65 #define TT_DESCRIPTOR_PAGE_TYPE_FAULT        (0UL << 0)
66 #define TT_DESCRIPTOR_PAGE_TYPE_PAGE         (2UL << 0)
67 #define TT_DESCRIPTOR_PAGE_TYPE_PAGE_XN      (3UL << 0)
68 #define TT_DESCRIPTOR_PAGE_TYPE_LARGEPAGE    (1UL << 0)
69 
70 // Section descriptor definitions
71 #define TT_DESCRIPTOR_SECTION_SIZE                              (0x00100000)
72 
73 #define TT_DESCRIPTOR_SECTION_NS_MASK                           (1UL << 19)
74 #define TT_DESCRIPTOR_SECTION_NS                                (1UL << 19)
75 
76 #define TT_DESCRIPTOR_SECTION_NG_MASK                           (1UL << 17)
77 #define TT_DESCRIPTOR_SECTION_NG_GLOBAL                         (0UL << 17)
78 #define TT_DESCRIPTOR_SECTION_NG_LOCAL                          (1UL << 17)
79 
80 #define TT_DESCRIPTOR_PAGE_NG_MASK                              (1UL << 11)
81 #define TT_DESCRIPTOR_PAGE_NG_GLOBAL                            (0UL << 11)
82 #define TT_DESCRIPTOR_PAGE_NG_LOCAL                             (1UL << 11)
83 
84 #define TT_DESCRIPTOR_SECTION_S_MASK                            (1UL << 16)
85 #define TT_DESCRIPTOR_SECTION_S_NOT_SHARED                      (0UL << 16)
86 #define TT_DESCRIPTOR_SECTION_S_SHARED                          (1UL << 16)
87 
88 #define TT_DESCRIPTOR_PAGE_S_MASK                               (1UL << 10)
89 #define TT_DESCRIPTOR_PAGE_S_NOT_SHARED                         (0UL << 10)
90 #define TT_DESCRIPTOR_PAGE_S_SHARED                             (1UL << 10)
91 
92 #define TT_DESCRIPTOR_SECTION_AP_MASK                           ((1UL << 15) | (3UL << 10))
93 #define TT_DESCRIPTOR_SECTION_AP_NO_NO                          ((0UL << 15) | (0UL << 10))
94 #define TT_DESCRIPTOR_SECTION_AP_RW_NO                          ((0UL << 15) | (1UL << 10))
95 #define TT_DESCRIPTOR_SECTION_AP_RW_RO                          ((0UL << 15) | (2UL << 10))
96 #define TT_DESCRIPTOR_SECTION_AP_RW_RW                          ((0UL << 15) | (3UL << 10))
97 #define TT_DESCRIPTOR_SECTION_AP_RO_NO                          ((1UL << 15) | (1UL << 10))
98 #define TT_DESCRIPTOR_SECTION_AP_RO_RO                          ((1UL << 15) | (3UL << 10))
99 
100 #define TT_DESCRIPTOR_PAGE_AP_MASK                              ((1UL << 9) | (3UL << 4))
101 #define TT_DESCRIPTOR_PAGE_AP_NO_NO                             ((0UL << 9) | (0UL << 4))
102 #define TT_DESCRIPTOR_PAGE_AP_RW_NO                             ((0UL << 9) | (1UL << 4))
103 #define TT_DESCRIPTOR_PAGE_AP_RW_RO                             ((0UL << 9) | (2UL << 4))
104 #define TT_DESCRIPTOR_PAGE_AP_RW_RW                             ((0UL << 9) | (3UL << 4))
105 #define TT_DESCRIPTOR_PAGE_AP_RO_NO                             ((1UL << 9) | (1UL << 4))
106 #define TT_DESCRIPTOR_PAGE_AP_RO_RO                             ((1UL << 9) | (3UL << 4))
107 
108 #define TT_DESCRIPTOR_SECTION_XN_MASK                           (0x1UL << 4)
109 #define TT_DESCRIPTOR_PAGE_XN_MASK                              (0x1UL << 0)
110 #define TT_DESCRIPTOR_LARGEPAGE_XN_MASK                         (0x1UL << 15)
111 
112 #define TT_DESCRIPTOR_SECTION_CACHE_POLICY_MASK                   ((3UL << 12) | (1UL << 3) | (1UL << 2))
113 #define TT_DESCRIPTOR_SECTION_CACHEABLE_MASK                      (1UL << 3)
114 #define TT_DESCRIPTOR_SECTION_CACHE_POLICY_STRONGLY_ORDERED       ((0UL << 12) | (0UL << 3) | (0UL << 2))
115 #define TT_DESCRIPTOR_SECTION_CACHE_POLICY_SHAREABLE_DEVICE       ((0UL << 12) | (0UL << 3) | (1UL << 2))
116 #define TT_DESCRIPTOR_SECTION_CACHE_POLICY_WRITE_THROUGH_NO_ALLOC ((0UL << 12) | (1UL << 3) | (0UL << 2))
117 #define TT_DESCRIPTOR_SECTION_CACHE_POLICY_WRITE_BACK_NO_ALLOC    ((0UL << 12) | (1UL << 3) | (1UL << 2))
118 #define TT_DESCRIPTOR_SECTION_CACHE_POLICY_NON_CACHEABLE          ((1UL << 12) | (0UL << 3) | (0UL << 2))
119 #define TT_DESCRIPTOR_SECTION_CACHE_POLICY_WRITE_BACK_ALLOC       ((1UL << 12) | (1UL << 3) | (1UL << 2))
120 #define TT_DESCRIPTOR_SECTION_CACHE_POLICY_NON_SHAREABLE_DEVICE   ((2UL << 12) | (0UL << 3) | (0UL << 2))
121 
122 #define TT_DESCRIPTOR_PAGE_SIZE                               (0x00001000)
123 
124 #define TT_DESCRIPTOR_PAGE_CACHE_POLICY_MASK                   ((3UL << 6) | (1UL << 3) | (1UL << 2))
125 #define TT_DESCRIPTOR_PAGE_CACHEABLE_MASK                       (1UL << 3)
126 #define TT_DESCRIPTOR_PAGE_CACHE_POLICY_STRONGLY_ORDERED       ((0UL << 6) | (0UL << 3) | (0UL << 2))
127 #define TT_DESCRIPTOR_PAGE_CACHE_POLICY_SHAREABLE_DEVICE       ((0UL << 6) | (0UL << 3) | (1UL << 2))
128 #define TT_DESCRIPTOR_PAGE_CACHE_POLICY_WRITE_THROUGH_NO_ALLOC ((0UL << 6) | (1UL << 3) | (0UL << 2))
129 #define TT_DESCRIPTOR_PAGE_CACHE_POLICY_WRITE_BACK_NO_ALLOC    ((0UL << 6) | (1UL << 3) | (1UL << 2))
130 #define TT_DESCRIPTOR_PAGE_CACHE_POLICY_NON_CACHEABLE          ((1UL << 6) | (0UL << 3) | (0UL << 2))
131 #define TT_DESCRIPTOR_PAGE_CACHE_POLICY_WRITE_BACK_ALLOC       ((1UL << 6) | (1UL << 3) | (1UL << 2))
132 #define TT_DESCRIPTOR_PAGE_CACHE_POLICY_NON_SHAREABLE_DEVICE   ((2UL << 6) | (0UL << 3) | (0UL << 2))
133 
134 #define TT_DESCRIPTOR_LARGEPAGE_CACHE_POLICY_MASK                   ((3UL << 12) | (1UL << 3) | (1UL << 2))
135 #define TT_DESCRIPTOR_LARGEPAGE_CACHE_POLICY_STRONGLY_ORDERED       ((0UL << 12) | (0UL << 3) | (0UL << 2))
136 #define TT_DESCRIPTOR_LARGEPAGE_CACHE_POLICY_SHAREABLE_DEVICE       ((0UL << 12) | (0UL << 3) | (1UL << 2))
137 #define TT_DESCRIPTOR_LARGEPAGE_CACHE_POLICY_WRITE_THROUGH_NO_ALLOC ((0UL << 12) | (1UL << 3) | (0UL << 2))
138 #define TT_DESCRIPTOR_LARGEPAGE_CACHE_POLICY_WRITE_BACK_NO_ALLOC    ((0UL << 12) | (1UL << 3) | (1UL << 2))
139 #define TT_DESCRIPTOR_LARGEPAGE_CACHE_POLICY_NON_CACHEABLE          ((1UL << 12) | (0UL << 3) | (0UL << 2))
140 #define TT_DESCRIPTOR_LARGEPAGE_CACHE_POLICY_WRITE_BACK_ALLOC       ((1UL << 12) | (1UL << 3) | (1UL << 2))
141 #define TT_DESCRIPTOR_LARGEPAGE_CACHE_POLICY_NON_SHAREABLE_DEVICE   ((2UL << 12) | (0UL << 3) | (0UL << 2))
142 
143 #define TT_DESCRIPTOR_CONVERT_TO_PAGE_AP(Desc)                  ((((Desc) & TT_DESCRIPTOR_SECTION_AP_MASK) >> 6) & TT_DESCRIPTOR_PAGE_AP_MASK)
144 #define TT_DESCRIPTOR_CONVERT_TO_PAGE_NG(Desc)                  ((((Desc) & TT_DESCRIPTOR_SECTION_NG_MASK) >> 6) & TT_DESCRIPTOR_PAGE_NG_MASK)
145 #define TT_DESCRIPTOR_CONVERT_TO_PAGE_S(Desc)                  ((((Desc) & TT_DESCRIPTOR_SECTION_S_MASK) >> 6) & TT_DESCRIPTOR_PAGE_S_MASK)
146 #define TT_DESCRIPTOR_CONVERT_TO_PAGE_XN(Desc,IsLargePage)      ((IsLargePage)? \
147                                                                     ((((Desc) & TT_DESCRIPTOR_SECTION_XN_MASK) << 11) & TT_DESCRIPTOR_LARGEPAGE_XN_MASK):    \
148                                                                     ((((Desc) & TT_DESCRIPTOR_SECTION_XN_MASK) >> 4) & TT_DESCRIPTOR_PAGE_XN_MASK))
149 #define TT_DESCRIPTOR_CONVERT_TO_PAGE_CACHE_POLICY(Desc,IsLargePage)      (IsLargePage? \
150                                                                     (((Desc) & TT_DESCRIPTOR_SECTION_CACHE_POLICY_MASK) & TT_DESCRIPTOR_LARGEPAGE_CACHE_POLICY_MASK): \
151                                                                     (((((Desc) & (0x3 << 12)) >> 6) | (Desc & (0x3 << 2)))))
152 
153 #define TT_DESCRIPTOR_CONVERT_TO_SECTION_AP(Desc)                  ((((Desc) & TT_DESCRIPTOR_PAGE_AP_MASK) << 6) & TT_DESCRIPTOR_SECTION_AP_MASK)
154 
155 #define TT_DESCRIPTOR_CONVERT_TO_SECTION_CACHE_POLICY(Desc,IsLargePage)      (IsLargePage? \
156                                                                     (((Desc) & TT_DESCRIPTOR_LARGEPAGE_CACHE_POLICY_MASK) & TT_DESCRIPTOR_SECTION_CACHE_POLICY_MASK): \
157                                                                     (((((Desc) & (0x3 << 6)) << 6) | (Desc & (0x3 << 2)))))
158 
159 #define TT_DESCRIPTOR_SECTION_ATTRIBUTE_MASK                (TT_DESCRIPTOR_SECTION_NS_MASK | TT_DESCRIPTOR_SECTION_NG_MASK | \
160                                                              TT_DESCRIPTOR_SECTION_S_MASK | TT_DESCRIPTOR_SECTION_AP_MASK | \
161                                                              TT_DESCRIPTOR_SECTION_XN_MASK | TT_DESCRIPTOR_SECTION_CACHE_POLICY_MASK)
162 
163 #define TT_DESCRIPTOR_PAGE_ATTRIBUTE_MASK                   (TT_DESCRIPTOR_PAGE_NG_MASK | TT_DESCRIPTOR_PAGE_S_MASK | \
164                                                              TT_DESCRIPTOR_PAGE_AP_MASK | TT_DESCRIPTOR_PAGE_XN_MASK | \
165                                                              TT_DESCRIPTOR_PAGE_CACHE_POLICY_MASK)
166 
167 #define TT_DESCRIPTOR_SECTION_DOMAIN_MASK                       (0x0FUL << 5)
168 #define TT_DESCRIPTOR_SECTION_DOMAIN(a)                         (((a) & 0x0FUL) << 5)
169 
170 #define TT_DESCRIPTOR_SECTION_BASE_ADDRESS_MASK                 (0xFFF00000)
171 #define TT_DESCRIPTOR_SECTION_PAGETABLE_ADDRESS_MASK            (0xFFFFFC00)
172 #define TT_DESCRIPTOR_SECTION_BASE_ADDRESS(a)                   ((a) & TT_DESCRIPTOR_SECTION_BASE_ADDRESS_MASK)
173 #define TT_DESCRIPTOR_SECTION_BASE_SHIFT                        20
174 
175 #define TT_DESCRIPTOR_PAGE_BASE_ADDRESS_MASK                 (0xFFFFF000)
176 #define TT_DESCRIPTOR_PAGE_INDEX_MASK                        (0x000FF000)
177 #define TT_DESCRIPTOR_PAGE_BASE_ADDRESS(a)                   ((a) & TT_DESCRIPTOR_PAGE_BASE_ADDRESS_MASK)
178 #define TT_DESCRIPTOR_PAGE_BASE_SHIFT                        12
179 
180 #define TT_DESCRIPTOR_SECTION_WRITE_BACK(NonSecure)         (TT_DESCRIPTOR_SECTION_TYPE_SECTION                                                           | \
181                                                             ((NonSecure) ?  TT_DESCRIPTOR_SECTION_NS : 0)    | \
182                                                             TT_DESCRIPTOR_SECTION_NG_GLOBAL                         | \
183                                                             TT_DESCRIPTOR_SECTION_S_SHARED                          | \
184                                                             TT_DESCRIPTOR_SECTION_DOMAIN(0)                         | \
185                                                             TT_DESCRIPTOR_SECTION_AP_RW_RW                          | \
186                                                             TT_DESCRIPTOR_SECTION_CACHE_POLICY_WRITE_BACK_ALLOC)
187 #define TT_DESCRIPTOR_SECTION_WRITE_THROUGH(NonSecure)      (TT_DESCRIPTOR_SECTION_TYPE_SECTION                                                           | \
188                                                             ((NonSecure) ?  TT_DESCRIPTOR_SECTION_NS : 0)    | \
189                                                             TT_DESCRIPTOR_SECTION_NG_GLOBAL                         | \
190                                                             TT_DESCRIPTOR_SECTION_S_SHARED                          | \
191                                                             TT_DESCRIPTOR_SECTION_DOMAIN(0)                         | \
192                                                             TT_DESCRIPTOR_SECTION_AP_RW_RW                          | \
193                                                             TT_DESCRIPTOR_SECTION_CACHE_POLICY_WRITE_THROUGH_NO_ALLOC)
194 #define TT_DESCRIPTOR_SECTION_DEVICE(NonSecure)             (TT_DESCRIPTOR_SECTION_TYPE_SECTION                                                           | \
195                                                             ((NonSecure) ?  TT_DESCRIPTOR_SECTION_NS : 0)    | \
196                                                             TT_DESCRIPTOR_SECTION_NG_GLOBAL                         | \
197                                                             TT_DESCRIPTOR_SECTION_S_NOT_SHARED                      | \
198                                                             TT_DESCRIPTOR_SECTION_DOMAIN(0)                         | \
199                                                             TT_DESCRIPTOR_SECTION_AP_RW_RW                          | \
200                                                             TT_DESCRIPTOR_SECTION_XN_MASK                           | \
201                                                             TT_DESCRIPTOR_SECTION_CACHE_POLICY_SHAREABLE_DEVICE)
202 #define TT_DESCRIPTOR_SECTION_UNCACHED(NonSecure)          (TT_DESCRIPTOR_SECTION_TYPE_SECTION                                                           | \
203                                                            ((NonSecure) ?  TT_DESCRIPTOR_SECTION_NS : 0)    | \
204                                                            TT_DESCRIPTOR_SECTION_NG_GLOBAL                         | \
205                                                            TT_DESCRIPTOR_SECTION_S_NOT_SHARED                      | \
206                                                            TT_DESCRIPTOR_SECTION_DOMAIN(0)                         | \
207                                                            TT_DESCRIPTOR_SECTION_AP_RW_RW                          | \
208                                                            TT_DESCRIPTOR_SECTION_CACHE_POLICY_NON_CACHEABLE)
209 
210 #define TT_DESCRIPTOR_PAGE_WRITE_BACK              (TT_DESCRIPTOR_PAGE_TYPE_PAGE                                                           | \
211                                                         TT_DESCRIPTOR_PAGE_NG_GLOBAL                                                      | \
212                                                         TT_DESCRIPTOR_PAGE_S_SHARED                                                       | \
213                                                         TT_DESCRIPTOR_PAGE_AP_RW_RW                                                       | \
214                                                         TT_DESCRIPTOR_PAGE_CACHE_POLICY_WRITE_BACK_ALLOC)
215 #define TT_DESCRIPTOR_PAGE_WRITE_THROUGH           (TT_DESCRIPTOR_PAGE_TYPE_PAGE                                                           | \
216                                                         TT_DESCRIPTOR_PAGE_NG_GLOBAL                                                      | \
217                                                         TT_DESCRIPTOR_PAGE_S_SHARED                                                       | \
218                                                         TT_DESCRIPTOR_PAGE_AP_RW_RW                                                       | \
219                                                         TT_DESCRIPTOR_PAGE_CACHE_POLICY_WRITE_THROUGH_NO_ALLOC)
220 #define TT_DESCRIPTOR_PAGE_DEVICE                  (TT_DESCRIPTOR_PAGE_TYPE_PAGE                                                           | \
221                                                         TT_DESCRIPTOR_PAGE_NG_GLOBAL                                                      | \
222                                                         TT_DESCRIPTOR_PAGE_S_NOT_SHARED                                                   | \
223                                                         TT_DESCRIPTOR_PAGE_AP_RW_RW                                                       | \
224                                                         TT_DESCRIPTOR_PAGE_XN_MASK                                                        | \
225                                                         TT_DESCRIPTOR_PAGE_CACHE_POLICY_SHAREABLE_DEVICE)
226 #define TT_DESCRIPTOR_PAGE_UNCACHED                (TT_DESCRIPTOR_PAGE_TYPE_PAGE                                                           | \
227                                                         TT_DESCRIPTOR_PAGE_NG_GLOBAL                                                      | \
228                                                         TT_DESCRIPTOR_PAGE_S_NOT_SHARED                                                   | \
229                                                         TT_DESCRIPTOR_PAGE_AP_RW_RW                                                       | \
230                                                         TT_DESCRIPTOR_PAGE_CACHE_POLICY_NON_CACHEABLE)
231 
232 UINT32
233 ConvertSectionAttributesToPageAttributes (
234   IN UINT32   SectionAttributes,
235   IN BOOLEAN  IsLargePage
236   );
237 
238 #endif
239