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