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