]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Library/ArmLib/AArch64/AArch64Mmu.c
ArmPkg/ArmLib: mark all cached mappings as (inner) shareable
[mirror_edk2.git] / ArmPkg / Library / ArmLib / AArch64 / AArch64Mmu.c
CommitLineData
25402f5d
HL
1/** @file\r
2* File managing the MMU for ARMv8 architecture\r
3*\r
19dc108b 4* Copyright (c) 2011-2014, ARM Limited. All rights reserved.\r
25402f5d
HL
5*\r
6* This program and the accompanying materials\r
7* are licensed and made available under the terms and conditions of the BSD License\r
8* which accompanies this distribution. The full text of the license may be found at\r
9* http://opensource.org/licenses/bsd-license.php\r
10*\r
11* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13*\r
14**/\r
15\r
16#include <Uefi.h>\r
17#include <Chipset/AArch64.h>\r
18#include <Library/BaseMemoryLib.h>\r
19#include <Library/MemoryAllocationLib.h>\r
20#include <Library/ArmLib.h>\r
21#include <Library/BaseLib.h>\r
22#include <Library/DebugLib.h>\r
23#include "AArch64Lib.h"\r
24#include "ArmLibPrivate.h"\r
25\r
26// We use this index definition to define an invalid block entry\r
27#define TT_ATTR_INDX_INVALID ((UINT32)~0)\r
28\r
29STATIC\r
30UINT64\r
31ArmMemoryAttributeToPageAttribute (\r
32 IN ARM_MEMORY_REGION_ATTRIBUTES Attributes\r
33 )\r
34{\r
35 switch (Attributes) {\r
36 case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK:\r
25402f5d 37 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_BACK:\r
0c9a522f
AB
38 return TT_ATTR_INDX_MEMORY_WRITE_BACK | TT_SH_INNER_SHAREABLE;\r
39\r
40 case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_THROUGH:\r
25402f5d 41 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_THROUGH:\r
0c9a522f
AB
42 return TT_ATTR_INDX_MEMORY_WRITE_THROUGH | TT_SH_INNER_SHAREABLE;\r
43\r
44 // Uncached and device mappings are treated as outer shareable by default,\r
45 case ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED:\r
25402f5d
HL
46 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_UNCACHED_UNBUFFERED:\r
47 return TT_ATTR_INDX_MEMORY_NON_CACHEABLE;\r
0c9a522f 48\r
25402f5d
HL
49 default:\r
50 ASSERT(0);\r
0c9a522f
AB
51 case ARM_MEMORY_REGION_ATTRIBUTE_DEVICE:\r
52 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_DEVICE:\r
25402f5d
HL
53 return TT_ATTR_INDX_DEVICE_MEMORY;\r
54 }\r
55}\r
56\r
57UINT64\r
58PageAttributeToGcdAttribute (\r
59 IN UINT64 PageAttributes\r
60 )\r
61{\r
62 UINT64 GcdAttributes;\r
63\r
64 switch (PageAttributes & TT_ATTR_INDX_MASK) {\r
65 case TT_ATTR_INDX_DEVICE_MEMORY:\r
66 GcdAttributes = EFI_MEMORY_UC;\r
67 break;\r
68 case TT_ATTR_INDX_MEMORY_NON_CACHEABLE:\r
69 GcdAttributes = EFI_MEMORY_WC;\r
70 break;\r
71 case TT_ATTR_INDX_MEMORY_WRITE_THROUGH:\r
72 GcdAttributes = EFI_MEMORY_WT;\r
73 break;\r
74 case TT_ATTR_INDX_MEMORY_WRITE_BACK:\r
75 GcdAttributes = EFI_MEMORY_WB;\r
76 break;\r
77 default:\r
78 DEBUG ((EFI_D_ERROR, "PageAttributeToGcdAttribute: PageAttributes:0x%lX not supported.\n", PageAttributes));\r
79 ASSERT (0);\r
80 // The Global Coherency Domain (GCD) value is defined as a bit set.\r
81 // Returning 0 means no attribute has been set.\r
82 GcdAttributes = 0;\r
83 }\r
84\r
85 // Determine protection attributes\r
86 if (((PageAttributes & TT_AP_MASK) == TT_AP_NO_RO) || ((PageAttributes & TT_AP_MASK) == TT_AP_RO_RO)) {\r
87 // Read only cases map to write-protect\r
88 GcdAttributes |= EFI_MEMORY_WP;\r
89 }\r
90\r
91 // Process eXecute Never attribute\r
92 if ((PageAttributes & (TT_PXN_MASK | TT_UXN_MASK)) != 0 ) {\r
93 GcdAttributes |= EFI_MEMORY_XP;\r
94 }\r
95\r
96 return GcdAttributes;\r
97}\r
98\r
99UINT64\r
100GcdAttributeToPageAttribute (\r
101 IN UINT64 GcdAttributes\r
102 )\r
103{\r
104 UINT64 PageAttributes;\r
105\r
106 switch (GcdAttributes & 0xFF) {\r
107 case EFI_MEMORY_UC:\r
108 PageAttributes = TT_ATTR_INDX_DEVICE_MEMORY;\r
109 break;\r
110 case EFI_MEMORY_WC:\r
111 PageAttributes = TT_ATTR_INDX_MEMORY_NON_CACHEABLE;\r
112 break;\r
113 case EFI_MEMORY_WT:\r
114 PageAttributes = TT_ATTR_INDX_MEMORY_WRITE_THROUGH;\r
115 break;\r
116 case EFI_MEMORY_WB:\r
117 PageAttributes = TT_ATTR_INDX_MEMORY_WRITE_BACK;\r
118 break;\r
119 default:\r
120 DEBUG ((EFI_D_ERROR, "GcdAttributeToPageAttribute: 0x%X attributes is not supported.\n", GcdAttributes));\r
121 ASSERT (0);\r
122 // If no match has been found then we mark the memory as device memory.\r
123 // The only side effect of using device memory should be a slow down in the performance.\r
124 PageAttributes = TT_ATTR_INDX_DEVICE_MEMORY;\r
125 }\r
126\r
127 // Determine protection attributes\r
128 if (GcdAttributes & EFI_MEMORY_WP) {\r
129 // Read only cases map to write-protect\r
130 PageAttributes |= TT_AP_RO_RO;\r
131 }\r
132\r
133 // Process eXecute Never attribute\r
134 if (GcdAttributes & EFI_MEMORY_XP) {\r
135 PageAttributes |= (TT_PXN_MASK | TT_UXN_MASK);\r
136 }\r
137\r
138 return PageAttributes;\r
139}\r
140\r
141ARM_MEMORY_REGION_ATTRIBUTES\r
142GcdAttributeToArmAttribute (\r
143 IN UINT64 GcdAttributes\r
144 )\r
145{\r
146 switch (GcdAttributes & 0xFF) {\r
147 case EFI_MEMORY_UC:\r
148 return ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;\r
149 case EFI_MEMORY_WC:\r
150 return ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED;\r
151 case EFI_MEMORY_WT:\r
152 return ARM_MEMORY_REGION_ATTRIBUTE_WRITE_THROUGH;\r
153 case EFI_MEMORY_WB:\r
154 return ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK;\r
155 default:\r
156 DEBUG ((EFI_D_ERROR, "GcdAttributeToArmAttribute: 0x%lX attributes is not supported.\n", GcdAttributes));\r
157 ASSERT (0);\r
158 return ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;\r
159 }\r
160}\r
161\r
162// Describe the T0SZ values for each translation table level\r
163typedef struct {\r
164 UINTN MinT0SZ;\r
165 UINTN MaxT0SZ;\r
166 UINTN LargestT0SZ; // Generally (MaxT0SZ == LargestT0SZ) but at the Level3 Table\r
167 // the MaxT0SZ is not at the boundary of the table\r
168} T0SZ_DESCRIPTION_PER_LEVEL;\r
169\r
170// Map table for the corresponding Level of Table\r
171STATIC CONST T0SZ_DESCRIPTION_PER_LEVEL T0SZPerTableLevel[] = {\r
172 { 16, 24, 24 }, // Table Level 0\r
173 { 25, 33, 33 }, // Table Level 1\r
174 { 34, 39, 42 } // Table Level 2\r
175};\r
176\r
177VOID\r
178GetRootTranslationTableInfo (\r
179 IN UINTN T0SZ,\r
180 OUT UINTN *TableLevel,\r
181 OUT UINTN *TableEntryCount\r
182 )\r
183{\r
184 UINTN Index;\r
185\r
186 // Identify the level of the root table from the given T0SZ\r
187 for (Index = 0; Index < sizeof (T0SZPerTableLevel) / sizeof (T0SZ_DESCRIPTION_PER_LEVEL); Index++) {\r
188 if (T0SZ <= T0SZPerTableLevel[Index].MaxT0SZ) {\r
189 break;\r
190 }\r
191 }\r
192\r
193 // If we have not found the corresponding maximum T0SZ then we use the last one\r
194 if (Index == sizeof (T0SZPerTableLevel) / sizeof (T0SZ_DESCRIPTION_PER_LEVEL)) {\r
195 Index--;\r
196 }\r
197\r
198 // Get the level of the root table\r
199 if (TableLevel) {\r
200 *TableLevel = Index;\r
201 }\r
202\r
203 // The Size of the Table is 2^(T0SZ-LargestT0SZ)\r
204 if (TableEntryCount) {\r
205 *TableEntryCount = 1 << (T0SZPerTableLevel[Index].LargestT0SZ - T0SZ + 1);\r
206 }\r
207}\r
208\r
209STATIC\r
210VOID\r
211LookupAddresstoRootTable (\r
212 IN UINT64 MaxAddress,\r
213 OUT UINTN *T0SZ,\r
214 OUT UINTN *TableEntryCount\r
215 )\r
216{\r
217 UINTN TopBit;\r
218\r
219 // Check the parameters are not NULL\r
220 ASSERT ((T0SZ != NULL) && (TableEntryCount != NULL));\r
221\r
222 // Look for the highest bit set in MaxAddress\r
223 for (TopBit = 63; TopBit != 0; TopBit--) {\r
224 if ((1ULL << TopBit) & MaxAddress) {\r
225 // MaxAddress top bit is found\r
226 TopBit = TopBit + 1;\r
227 break;\r
228 }\r
229 }\r
230 ASSERT (TopBit != 0);\r
231\r
232 // Calculate T0SZ from the top bit of the MaxAddress\r
233 *T0SZ = 64 - TopBit;\r
234\r
235 // Get the Table info from T0SZ\r
236 GetRootTranslationTableInfo (*T0SZ, NULL, TableEntryCount);\r
237}\r
238\r
239STATIC\r
240UINT64*\r
241GetBlockEntryListFromAddress (\r
242 IN UINT64 *RootTable,\r
243 IN UINT64 RegionStart,\r
244 OUT UINTN *TableLevel,\r
245 IN OUT UINT64 *BlockEntrySize,\r
edff645f 246 OUT UINT64 **LastBlockEntry\r
25402f5d
HL
247 )\r
248{\r
249 UINTN RootTableLevel;\r
250 UINTN RootTableEntryCount;\r
251 UINT64 *TranslationTable;\r
252 UINT64 *BlockEntry;\r
ebb92353 253 UINT64 *SubTableBlockEntry;\r
25402f5d
HL
254 UINT64 BlockEntryAddress;\r
255 UINTN BaseAddressAlignment;\r
256 UINTN PageLevel;\r
257 UINTN Index;\r
258 UINTN IndexLevel;\r
259 UINTN T0SZ;\r
260 UINT64 Attributes;\r
261 UINT64 TableAttributes;\r
262\r
263 // Initialize variable\r
264 BlockEntry = NULL;\r
265\r
266 // Ensure the parameters are valid\r
19dc108b
OM
267 if (!(TableLevel && BlockEntrySize && LastBlockEntry)) {\r
268 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER);\r
269 return NULL;\r
270 }\r
25402f5d
HL
271\r
272 // Ensure the Region is aligned on 4KB boundary\r
19dc108b
OM
273 if ((RegionStart & (SIZE_4KB - 1)) != 0) {\r
274 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER);\r
275 return NULL;\r
276 }\r
25402f5d 277\r
41f89016
HG
278 // Ensure the required size is aligned on 4KB boundary and not 0\r
279 if ((*BlockEntrySize & (SIZE_4KB - 1)) != 0 || *BlockEntrySize == 0) {\r
19dc108b
OM
280 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER);\r
281 return NULL;\r
282 }\r
25402f5d 283\r
25402f5d
HL
284 T0SZ = ArmGetTCR () & TCR_T0SZ_MASK;\r
285 // Get the Table info from T0SZ\r
286 GetRootTranslationTableInfo (T0SZ, &RootTableLevel, &RootTableEntryCount);\r
25402f5d
HL
287\r
288 // If the start address is 0x0 then we use the size of the region to identify the alignment\r
289 if (RegionStart == 0) {\r
290 // Identify the highest possible alignment for the Region Size\r
41f89016 291 BaseAddressAlignment = LowBitSet64 (*BlockEntrySize);\r
25402f5d
HL
292 } else {\r
293 // Identify the highest possible alignment for the Base Address\r
41f89016 294 BaseAddressAlignment = LowBitSet64 (RegionStart);\r
25402f5d
HL
295 }\r
296\r
54d8d4dc
AB
297 // Identify the Page Level the RegionStart must belong to. Note that PageLevel\r
298 // should be at least 1 since block translations are not supported at level 0\r
299 PageLevel = MAX (3 - ((BaseAddressAlignment - 12) / 9), 1);\r
25402f5d 300\r
6ea162c2
OM
301 // If the required size is smaller than the current block size then we need to go to the page below.\r
302 // The PageLevel was calculated on the Base Address alignment but did not take in account the alignment\r
303 // of the allocation size\r
946067bf 304 while (*BlockEntrySize < TT_BLOCK_ENTRY_SIZE_AT_LEVEL (PageLevel)) {\r
25402f5d
HL
305 // It does not fit so we need to go a page level above\r
306 PageLevel++;\r
307 }\r
308\r
25402f5d
HL
309 //\r
310 // Get the Table Descriptor for the corresponding PageLevel. We need to decompose RegionStart to get appropriate entries\r
311 //\r
312\r
313 TranslationTable = RootTable;\r
314 for (IndexLevel = RootTableLevel; IndexLevel <= PageLevel; IndexLevel++) {\r
315 BlockEntry = (UINT64*)TT_GET_ENTRY_FOR_ADDRESS (TranslationTable, IndexLevel, RegionStart);\r
316\r
317 if ((IndexLevel != 3) && ((*BlockEntry & TT_TYPE_MASK) == TT_TYPE_TABLE_ENTRY)) {\r
318 // Go to the next table\r
319 TranslationTable = (UINT64*)(*BlockEntry & TT_ADDRESS_MASK_DESCRIPTION_TABLE);\r
320\r
edff645f 321 // If we are at the last level then update the last level to next level\r
25402f5d 322 if (IndexLevel == PageLevel) {\r
edff645f
HG
323 // Enter the next level\r
324 PageLevel++;\r
25402f5d
HL
325 }\r
326 } else if ((*BlockEntry & TT_TYPE_MASK) == TT_TYPE_BLOCK_ENTRY) {\r
327 // If we are not at the last level then we need to split this BlockEntry\r
328 if (IndexLevel != PageLevel) {\r
329 // Retrieve the attributes from the block entry\r
330 Attributes = *BlockEntry & TT_ATTRIBUTES_MASK;\r
331\r
332 // Convert the block entry attributes into Table descriptor attributes\r
333 TableAttributes = TT_TABLE_AP_NO_PERMISSION;\r
334 if (Attributes & TT_PXN_MASK) {\r
335 TableAttributes = TT_TABLE_PXN;\r
336 }\r
2afeabd1
AB
337 // XN maps to UXN in the EL1&0 translation regime\r
338 if (Attributes & TT_XN_MASK) {\r
25402f5d
HL
339 TableAttributes = TT_TABLE_XN;\r
340 }\r
341 if (Attributes & TT_NS) {\r
342 TableAttributes = TT_TABLE_NS;\r
343 }\r
344\r
345 // Get the address corresponding at this entry\r
346 BlockEntryAddress = RegionStart;\r
347 BlockEntryAddress = BlockEntryAddress >> TT_ADDRESS_OFFSET_AT_LEVEL(IndexLevel);\r
348 // Shift back to right to set zero before the effective address\r
349 BlockEntryAddress = BlockEntryAddress << TT_ADDRESS_OFFSET_AT_LEVEL(IndexLevel);\r
350\r
6ea162c2
OM
351 // Set the correct entry type for the next page level\r
352 if ((IndexLevel + 1) == 3) {\r
25402f5d
HL
353 Attributes |= TT_TYPE_BLOCK_ENTRY_LEVEL3;\r
354 } else {\r
355 Attributes |= TT_TYPE_BLOCK_ENTRY;\r
356 }\r
357\r
358 // Create a new translation table\r
7d189f99 359 TranslationTable = (UINT64*)AllocateAlignedPages (EFI_SIZE_TO_PAGES(TT_ENTRY_COUNT * sizeof(UINT64)), TT_ALIGNMENT_DESCRIPTION_TABLE);\r
25402f5d
HL
360 if (TranslationTable == NULL) {\r
361 return NULL;\r
362 }\r
25402f5d 363\r
ebb92353
OM
364 // Populate the newly created lower level table\r
365 SubTableBlockEntry = TranslationTable;\r
366 for (Index = 0; Index < TT_ENTRY_COUNT; Index++) {\r
367 *SubTableBlockEntry = Attributes | (BlockEntryAddress + (Index << TT_ADDRESS_OFFSET_AT_LEVEL(IndexLevel + 1)));\r
368 SubTableBlockEntry++;\r
369 }\r
370\r
6ea162c2 371 // Fill the BlockEntry with the new TranslationTable\r
25402f5d 372 *BlockEntry = ((UINTN)TranslationTable & TT_ADDRESS_MASK_DESCRIPTION_TABLE) | TableAttributes | TT_TYPE_TABLE_ENTRY;\r
25402f5d
HL
373 }\r
374 } else {\r
25402f5d 375 if (IndexLevel != PageLevel) {\r
8bb7f03a
OM
376 //\r
377 // Case when we have an Invalid Entry and we are at a page level above of the one targetted.\r
378 //\r
379\r
25402f5d 380 // Create a new translation table\r
7d189f99 381 TranslationTable = (UINT64*)AllocateAlignedPages (EFI_SIZE_TO_PAGES(TT_ENTRY_COUNT * sizeof(UINT64)), TT_ALIGNMENT_DESCRIPTION_TABLE);\r
25402f5d
HL
382 if (TranslationTable == NULL) {\r
383 return NULL;\r
384 }\r
25402f5d
HL
385\r
386 ZeroMem (TranslationTable, TT_ENTRY_COUNT * sizeof(UINT64));\r
387\r
388 // Fill the new BlockEntry with the TranslationTable\r
389 *BlockEntry = ((UINTN)TranslationTable & TT_ADDRESS_MASK_DESCRIPTION_TABLE) | TT_TYPE_TABLE_ENTRY;\r
390 }\r
391 }\r
392 }\r
393\r
edff645f
HG
394 // Expose the found PageLevel to the caller\r
395 *TableLevel = PageLevel;\r
396\r
397 // Now, we have the Table Level we can get the Block Size associated to this table\r
398 *BlockEntrySize = TT_BLOCK_ENTRY_SIZE_AT_LEVEL (PageLevel);\r
399\r
400 // The last block of the root table depends on the number of entry in this table,\r
401 // otherwise it is always the (TT_ENTRY_COUNT - 1)th entry in the table.\r
402 *LastBlockEntry = TT_LAST_BLOCK_ADDRESS(TranslationTable,\r
403 (PageLevel == RootTableLevel) ? RootTableEntryCount : TT_ENTRY_COUNT);\r
404\r
25402f5d
HL
405 return BlockEntry;\r
406}\r
407\r
408STATIC\r
409RETURN_STATUS\r
5ab77c66
AB
410UpdateRegionMapping (\r
411 IN UINT64 *RootTable,\r
412 IN UINT64 RegionStart,\r
413 IN UINT64 RegionLength,\r
414 IN UINT64 Attributes,\r
415 IN UINT64 BlockEntryMask\r
25402f5d
HL
416 )\r
417{\r
25402f5d 418 UINT32 Type;\r
5ab77c66
AB
419 UINT64 *BlockEntry;\r
420 UINT64 *LastBlockEntry;\r
25402f5d
HL
421 UINT64 BlockEntrySize;\r
422 UINTN TableLevel;\r
423\r
424 // Ensure the Length is aligned on 4KB boundary\r
5ab77c66 425 if ((RegionLength == 0) || ((RegionLength & (SIZE_4KB - 1)) != 0)) {\r
19dc108b
OM
426 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER);\r
427 return RETURN_INVALID_PARAMETER;\r
428 }\r
25402f5d 429\r
25402f5d
HL
430 do {\r
431 // Get the first Block Entry that matches the Virtual Address and also the information on the Table Descriptor\r
432 // such as the the size of the Block Entry and the address of the last BlockEntry of the Table Descriptor\r
5ab77c66 433 BlockEntrySize = RegionLength;\r
25402f5d
HL
434 BlockEntry = GetBlockEntryListFromAddress (RootTable, RegionStart, &TableLevel, &BlockEntrySize, &LastBlockEntry);\r
435 if (BlockEntry == NULL) {\r
436 // GetBlockEntryListFromAddress() return NULL when it fails to allocate new pages from the Translation Tables\r
437 return RETURN_OUT_OF_RESOURCES;\r
438 }\r
439\r
440 if (TableLevel != 3) {\r
441 Type = TT_TYPE_BLOCK_ENTRY;\r
442 } else {\r
443 Type = TT_TYPE_BLOCK_ENTRY_LEVEL3;\r
444 }\r
445\r
446 do {\r
447 // Fill the Block Entry with attribute and output block address\r
5ab77c66
AB
448 *BlockEntry &= BlockEntryMask;\r
449 *BlockEntry |= (RegionStart & TT_ADDRESS_MASK_BLOCK_ENTRY) | Attributes | Type;\r
25402f5d
HL
450\r
451 // Go to the next BlockEntry\r
452 RegionStart += BlockEntrySize;\r
5ab77c66 453 RegionLength -= BlockEntrySize;\r
25402f5d 454 BlockEntry++;\r
84836814
HG
455\r
456 // Break the inner loop when next block is a table\r
457 // Rerun GetBlockEntryListFromAddress to avoid page table memory leak\r
458 if (TableLevel != 3 &&\r
459 (*BlockEntry & TT_TYPE_MASK) == TT_TYPE_TABLE_ENTRY) {\r
460 break;\r
461 }\r
5ab77c66
AB
462 } while ((RegionLength >= BlockEntrySize) && (BlockEntry <= LastBlockEntry));\r
463 } while (RegionLength != 0);\r
25402f5d
HL
464\r
465 return RETURN_SUCCESS;\r
466}\r
467\r
5ab77c66
AB
468STATIC\r
469RETURN_STATUS\r
470FillTranslationTable (\r
471 IN UINT64 *RootTable,\r
472 IN ARM_MEMORY_REGION_DESCRIPTOR *MemoryRegion\r
473 )\r
474{\r
475 return UpdateRegionMapping (\r
476 RootTable,\r
477 MemoryRegion->VirtualBase,\r
478 MemoryRegion->Length,\r
479 ArmMemoryAttributeToPageAttribute (MemoryRegion->Attributes) | TT_AF,\r
480 0\r
481 );\r
482}\r
483\r
25402f5d
HL
484RETURN_STATUS\r
485SetMemoryAttributes (\r
486 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
487 IN UINT64 Length,\r
488 IN UINT64 Attributes,\r
489 IN EFI_PHYSICAL_ADDRESS VirtualMask\r
490 )\r
e6f3ed43 491{\r
25402f5d
HL
492 RETURN_STATUS Status;\r
493 ARM_MEMORY_REGION_DESCRIPTOR MemoryRegion;\r
494 UINT64 *TranslationTable;\r
495\r
496 MemoryRegion.PhysicalBase = BaseAddress;\r
497 MemoryRegion.VirtualBase = BaseAddress;\r
498 MemoryRegion.Length = Length;\r
499 MemoryRegion.Attributes = GcdAttributeToArmAttribute (Attributes);\r
500\r
501 TranslationTable = ArmGetTTBR0BaseAddress ();\r
502\r
e6f3ed43
LL
503 Status = FillTranslationTable (TranslationTable, &MemoryRegion);\r
504 if (RETURN_ERROR (Status)) {\r
505 return Status;\r
25402f5d
HL
506 }\r
507\r
25402f5d
HL
508 // Invalidate all TLB entries so changes are synced\r
509 ArmInvalidateTlb ();\r
510\r
511 return RETURN_SUCCESS;\r
512}\r
513\r
4d9a4f62
AB
514STATIC\r
515RETURN_STATUS\r
516SetMemoryRegionAttribute (\r
517 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
518 IN UINT64 Length,\r
519 IN UINT64 Attributes,\r
520 IN UINT64 BlockEntryMask\r
521 )\r
522{\r
523 RETURN_STATUS Status;\r
524 UINT64 *RootTable;\r
525\r
526 RootTable = ArmGetTTBR0BaseAddress ();\r
527\r
528 Status = UpdateRegionMapping (RootTable, BaseAddress, Length, Attributes, BlockEntryMask);\r
529 if (RETURN_ERROR (Status)) {\r
530 return Status;\r
531 }\r
532\r
533 // Invalidate all TLB entries so changes are synced\r
534 ArmInvalidateTlb ();\r
535\r
536 return RETURN_SUCCESS;\r
537}\r
538\r
539RETURN_STATUS\r
540ArmSetMemoryRegionNoExec (\r
541 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
542 IN UINT64 Length\r
543 )\r
544{\r
545 UINT64 Val;\r
546\r
547 if (ArmReadCurrentEL () == AARCH64_EL1) {\r
548 Val = TT_PXN_MASK | TT_UXN_MASK;\r
549 } else {\r
550 Val = TT_XN_MASK;\r
551 }\r
552\r
553 return SetMemoryRegionAttribute (\r
554 BaseAddress,\r
555 Length,\r
556 Val,\r
557 ~TT_ADDRESS_MASK_BLOCK_ENTRY);\r
558}\r
559\r
560RETURN_STATUS\r
561ArmClearMemoryRegionNoExec (\r
562 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
563 IN UINT64 Length\r
564 )\r
565{\r
566 UINT64 Mask;\r
567\r
568 // XN maps to UXN in the EL1&0 translation regime\r
569 Mask = ~(TT_ADDRESS_MASK_BLOCK_ENTRY | TT_PXN_MASK | TT_XN_MASK);\r
570\r
571 return SetMemoryRegionAttribute (\r
572 BaseAddress,\r
573 Length,\r
574 0,\r
575 Mask);\r
576}\r
577\r
578RETURN_STATUS\r
579ArmSetMemoryRegionReadOnly (\r
580 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
581 IN UINT64 Length\r
582 )\r
583{\r
584 return SetMemoryRegionAttribute (\r
585 BaseAddress,\r
586 Length,\r
587 TT_AP_RO_RO,\r
588 ~TT_ADDRESS_MASK_BLOCK_ENTRY);\r
589}\r
590\r
591RETURN_STATUS\r
592ArmClearMemoryRegionReadOnly (\r
593 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
594 IN UINT64 Length\r
595 )\r
596{\r
597 return SetMemoryRegionAttribute (\r
598 BaseAddress,\r
599 Length,\r
600 TT_AP_NO_RO,\r
601 ~(TT_ADDRESS_MASK_BLOCK_ENTRY | TT_AP_MASK));\r
602}\r
603\r
25402f5d
HL
604RETURN_STATUS\r
605EFIAPI\r
606ArmConfigureMmu (\r
607 IN ARM_MEMORY_REGION_DESCRIPTOR *MemoryTable,\r
608 OUT VOID **TranslationTableBase OPTIONAL,\r
609 OUT UINTN *TranslationTableSize OPTIONAL\r
610 )\r
611{\r
612 VOID* TranslationTable;\r
613 UINTN TranslationTablePageCount;\r
614 UINT32 TranslationTableAttribute;\r
615 ARM_MEMORY_REGION_DESCRIPTOR *MemoryTableEntry;\r
616 UINT64 MaxAddress;\r
617 UINT64 TopAddress;\r
618 UINTN T0SZ;\r
619 UINTN RootTableEntryCount;\r
620 UINT64 TCR;\r
621 RETURN_STATUS Status;\r
622\r
8bb7f03a 623 if(MemoryTable == NULL) {\r
19dc108b
OM
624 ASSERT (MemoryTable != NULL);\r
625 return RETURN_INVALID_PARAMETER;\r
626 }\r
25402f5d
HL
627\r
628 // Identify the highest address of the memory table\r
629 MaxAddress = MemoryTable->PhysicalBase + MemoryTable->Length - 1;\r
630 MemoryTableEntry = MemoryTable;\r
631 while (MemoryTableEntry->Length != 0) {\r
632 TopAddress = MemoryTableEntry->PhysicalBase + MemoryTableEntry->Length - 1;\r
633 if (TopAddress > MaxAddress) {\r
634 MaxAddress = TopAddress;\r
635 }\r
636 MemoryTableEntry++;\r
637 }\r
638\r
639 // Lookup the Table Level to get the information\r
640 LookupAddresstoRootTable (MaxAddress, &T0SZ, &RootTableEntryCount);\r
641\r
642 //\r
643 // Set TCR that allows us to retrieve T0SZ in the subsequent functions\r
644 //\r
e21227c6
OM
645 // Ideally we will be running at EL2, but should support EL1 as well.\r
646 // UEFI should not run at EL3.\r
647 if (ArmReadCurrentEL () == AARCH64_EL2) {\r
648 //Note: Bits 23 and 31 are reserved(RES1) bits in TCR_EL2\r
25402f5d
HL
649 TCR = T0SZ | (1UL << 31) | (1UL << 23) | TCR_TG0_4KB;\r
650\r
651 // Set the Physical Address Size using MaxAddress\r
652 if (MaxAddress < SIZE_4GB) {\r
653 TCR |= TCR_PS_4GB;\r
654 } else if (MaxAddress < SIZE_64GB) {\r
655 TCR |= TCR_PS_64GB;\r
656 } else if (MaxAddress < SIZE_1TB) {\r
657 TCR |= TCR_PS_1TB;\r
658 } else if (MaxAddress < SIZE_4TB) {\r
659 TCR |= TCR_PS_4TB;\r
660 } else if (MaxAddress < SIZE_16TB) {\r
661 TCR |= TCR_PS_16TB;\r
662 } else if (MaxAddress < SIZE_256TB) {\r
663 TCR |= TCR_PS_256TB;\r
664 } else {\r
e21227c6
OM
665 DEBUG ((EFI_D_ERROR, "ArmConfigureMmu: The MaxAddress 0x%lX is not supported by this MMU configuration.\n", MaxAddress));\r
666 ASSERT (0); // Bigger than 48-bit memory space are not supported\r
667 return RETURN_UNSUPPORTED;\r
668 }\r
669 } else if (ArmReadCurrentEL () == AARCH64_EL1) {\r
670 TCR = T0SZ | TCR_TG0_4KB;\r
671\r
672 // Set the Physical Address Size using MaxAddress\r
673 if (MaxAddress < SIZE_4GB) {\r
674 TCR |= TCR_IPS_4GB;\r
675 } else if (MaxAddress < SIZE_64GB) {\r
676 TCR |= TCR_IPS_64GB;\r
677 } else if (MaxAddress < SIZE_1TB) {\r
678 TCR |= TCR_IPS_1TB;\r
679 } else if (MaxAddress < SIZE_4TB) {\r
680 TCR |= TCR_IPS_4TB;\r
681 } else if (MaxAddress < SIZE_16TB) {\r
682 TCR |= TCR_IPS_16TB;\r
683 } else if (MaxAddress < SIZE_256TB) {\r
684 TCR |= TCR_IPS_256TB;\r
685 } else {\r
686 DEBUG ((EFI_D_ERROR, "ArmConfigureMmu: The MaxAddress 0x%lX is not supported by this MMU configuration.\n", MaxAddress));\r
25402f5d
HL
687 ASSERT (0); // Bigger than 48-bit memory space are not supported\r
688 return RETURN_UNSUPPORTED;\r
689 }\r
690 } else {\r
e21227c6 691 ASSERT (0); // UEFI is only expected to run at EL2 and EL1, not EL3.\r
25402f5d
HL
692 return RETURN_UNSUPPORTED;\r
693 }\r
694\r
695 // Set TCR\r
696 ArmSetTCR (TCR);\r
697\r
698 // Allocate pages for translation table\r
7d189f99
HG
699 TranslationTablePageCount = EFI_SIZE_TO_PAGES(RootTableEntryCount * sizeof(UINT64));\r
700 TranslationTable = (UINT64*)AllocateAlignedPages (TranslationTablePageCount, TT_ALIGNMENT_DESCRIPTION_TABLE);\r
25402f5d
HL
701 if (TranslationTable == NULL) {\r
702 return RETURN_OUT_OF_RESOURCES;\r
703 }\r
25402f5d
HL
704 // We set TTBR0 just after allocating the table to retrieve its location from the subsequent\r
705 // functions without needing to pass this value across the functions. The MMU is only enabled\r
706 // after the translation tables are populated.\r
707 ArmSetTTBR0 (TranslationTable);\r
708\r
709 if (TranslationTableBase != NULL) {\r
710 *TranslationTableBase = TranslationTable;\r
711 }\r
712\r
713 if (TranslationTableSize != NULL) {\r
714 *TranslationTableSize = RootTableEntryCount * sizeof(UINT64);\r
715 }\r
716\r
717 ZeroMem (TranslationTable, RootTableEntryCount * sizeof(UINT64));\r
718\r
719 // Disable MMU and caches. ArmDisableMmu() also invalidates the TLBs\r
720 ArmDisableMmu ();\r
721 ArmDisableDataCache ();\r
722 ArmDisableInstructionCache ();\r
723\r
724 // Make sure nothing sneaked into the cache\r
725 ArmCleanInvalidateDataCache ();\r
726 ArmInvalidateInstructionCache ();\r
727\r
728 TranslationTableAttribute = TT_ATTR_INDX_INVALID;\r
729 while (MemoryTable->Length != 0) {\r
730 // Find the memory attribute for the Translation Table\r
731 if (((UINTN)TranslationTable >= MemoryTable->PhysicalBase) &&\r
732 ((UINTN)TranslationTable <= MemoryTable->PhysicalBase - 1 + MemoryTable->Length)) {\r
733 TranslationTableAttribute = MemoryTable->Attributes;\r
734 }\r
735\r
736 Status = FillTranslationTable (TranslationTable, MemoryTable);\r
737 if (RETURN_ERROR (Status)) {\r
738 goto FREE_TRANSLATION_TABLE;\r
739 }\r
740 MemoryTable++;\r
741 }\r
742\r
743 // Translate the Memory Attributes into Translation Table Register Attributes\r
744 if ((TranslationTableAttribute == ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED) ||\r
745 (TranslationTableAttribute == ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_UNCACHED_UNBUFFERED)) {\r
746 TCR |= TCR_SH_NON_SHAREABLE | TCR_RGN_OUTER_NON_CACHEABLE | TCR_RGN_INNER_NON_CACHEABLE;\r
747 } else if ((TranslationTableAttribute == ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK) ||\r
748 (TranslationTableAttribute == ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_BACK)) {\r
749 TCR |= TCR_SH_INNER_SHAREABLE | TCR_RGN_OUTER_WRITE_BACK_ALLOC | TCR_RGN_INNER_WRITE_BACK_ALLOC;\r
750 } else if ((TranslationTableAttribute == ARM_MEMORY_REGION_ATTRIBUTE_WRITE_THROUGH) ||\r
751 (TranslationTableAttribute == ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_THROUGH)) {\r
752 TCR |= TCR_SH_NON_SHAREABLE | TCR_RGN_OUTER_WRITE_THROUGH | TCR_RGN_INNER_WRITE_THROUGH;\r
753 } else {\r
754 // If we failed to find a mapping that contains the root translation table then it probably means the translation table\r
755 // is not mapped in the given memory map.\r
756 ASSERT (0);\r
757 Status = RETURN_UNSUPPORTED;\r
758 goto FREE_TRANSLATION_TABLE;\r
759 }\r
760\r
1eb5b4f2
OM
761 // Set again TCR after getting the Translation Table attributes\r
762 ArmSetTCR (TCR);\r
763\r
25402f5d
HL
764 ArmSetMAIR (MAIR_ATTR(TT_ATTR_INDX_DEVICE_MEMORY, MAIR_ATTR_DEVICE_MEMORY) | // mapped to EFI_MEMORY_UC\r
765 MAIR_ATTR(TT_ATTR_INDX_MEMORY_NON_CACHEABLE, MAIR_ATTR_NORMAL_MEMORY_NON_CACHEABLE) | // mapped to EFI_MEMORY_WC\r
766 MAIR_ATTR(TT_ATTR_INDX_MEMORY_WRITE_THROUGH, MAIR_ATTR_NORMAL_MEMORY_WRITE_THROUGH) | // mapped to EFI_MEMORY_WT\r
767 MAIR_ATTR(TT_ATTR_INDX_MEMORY_WRITE_BACK, MAIR_ATTR_NORMAL_MEMORY_WRITE_BACK)); // mapped to EFI_MEMORY_WB\r
768\r
769 ArmDisableAlignmentCheck ();\r
770 ArmEnableInstructionCache ();\r
771 ArmEnableDataCache ();\r
772\r
773 ArmEnableMmu ();\r
774 return RETURN_SUCCESS;\r
775\r
776FREE_TRANSLATION_TABLE:\r
777 FreePages (TranslationTable, TranslationTablePageCount);\r
778 return Status;\r
779}\r