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