]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Library/ArmMmuLib/Arm/ArmMmuLibCore.c
ArmPkg/ArmMmuLib ARM: fix page size granularity in initial MMU setting
[mirror_edk2.git] / ArmPkg / Library / ArmMmuLib / Arm / ArmMmuLibCore.c
CommitLineData
d7f03464
AB
1/** @file\r
2* File managing the MMU for ARMv7 architecture\r
3*\r
4* Copyright (c) 2011-2016, ARM Limited. All rights reserved.\r
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/ArmV7.h>\r
18#include <Library/BaseMemoryLib.h>\r
521f3ced 19#include <Library/CacheMaintenanceLib.h>\r
d7f03464
AB
20#include <Library/MemoryAllocationLib.h>\r
21#include <Library/ArmLib.h>\r
22#include <Library/BaseLib.h>\r
23#include <Library/DebugLib.h>\r
24#include <Library/PcdLib.h>\r
25\r
26#define ID_MMFR0_SHARELVL_SHIFT 12\r
27#define ID_MMFR0_SHARELVL_MASK 0xf\r
28#define ID_MMFR0_SHARELVL_ONE 0\r
29#define ID_MMFR0_SHARELVL_TWO 1\r
30\r
31#define ID_MMFR0_INNERSHR_SHIFT 28\r
32#define ID_MMFR0_INNERSHR_MASK 0xf\r
33#define ID_MMFR0_OUTERSHR_SHIFT 8\r
34#define ID_MMFR0_OUTERSHR_MASK 0xf\r
35\r
36#define ID_MMFR0_SHR_IMP_UNCACHED 0\r
37#define ID_MMFR0_SHR_IMP_HW_COHERENT 1\r
38#define ID_MMFR0_SHR_IGNORED 0xf\r
39\r
9f1dcbe8
AB
40#define __EFI_MEMORY_RWX 0 // no restrictions\r
41\r
521f3ced
AB
42#define CACHE_ATTRIBUTE_MASK (EFI_MEMORY_UC | \\r
43 EFI_MEMORY_WC | \\r
44 EFI_MEMORY_WT | \\r
45 EFI_MEMORY_WB | \\r
46 EFI_MEMORY_UCE | \\r
47 EFI_MEMORY_WP)\r
48\r
d7f03464
AB
49UINTN\r
50EFIAPI\r
51ArmReadIdMmfr0 (\r
52 VOID\r
53 );\r
54\r
55BOOLEAN\r
56EFIAPI\r
57ArmHasMpExtensions (\r
58 VOID\r
59 );\r
60\r
61UINT32\r
62ConvertSectionAttributesToPageAttributes (\r
63 IN UINT32 SectionAttributes,\r
64 IN BOOLEAN IsLargePage\r
65 )\r
66{\r
67 UINT32 PageAttributes;\r
68\r
69 PageAttributes = 0;\r
70 PageAttributes |= TT_DESCRIPTOR_CONVERT_TO_PAGE_CACHE_POLICY (SectionAttributes, IsLargePage);\r
71 PageAttributes |= TT_DESCRIPTOR_CONVERT_TO_PAGE_AP (SectionAttributes);\r
72 PageAttributes |= TT_DESCRIPTOR_CONVERT_TO_PAGE_XN (SectionAttributes, IsLargePage);\r
73 PageAttributes |= TT_DESCRIPTOR_CONVERT_TO_PAGE_NG (SectionAttributes);\r
74 PageAttributes |= TT_DESCRIPTOR_CONVERT_TO_PAGE_S (SectionAttributes);\r
75\r
76 return PageAttributes;\r
77}\r
78\r
79STATIC\r
80BOOLEAN\r
81PreferNonshareableMemory (\r
82 VOID\r
83 )\r
84{\r
85 UINTN Mmfr;\r
86 UINTN Val;\r
87\r
88 if (FeaturePcdGet (PcdNormalMemoryNonshareableOverride)) {\r
89 return TRUE;\r
90 }\r
91\r
92 //\r
93 // Check whether the innermost level of shareability (the level we will use\r
94 // by default to map normal memory) is implemented with hardware coherency\r
95 // support. Otherwise, revert to mapping as non-shareable.\r
96 //\r
97 Mmfr = ArmReadIdMmfr0 ();\r
98 switch ((Mmfr >> ID_MMFR0_SHARELVL_SHIFT) & ID_MMFR0_SHARELVL_MASK) {\r
99 case ID_MMFR0_SHARELVL_ONE:\r
100 // one level of shareability\r
101 Val = (Mmfr >> ID_MMFR0_OUTERSHR_SHIFT) & ID_MMFR0_OUTERSHR_MASK;\r
102 break;\r
103 case ID_MMFR0_SHARELVL_TWO:\r
104 // two levels of shareability\r
105 Val = (Mmfr >> ID_MMFR0_INNERSHR_SHIFT) & ID_MMFR0_INNERSHR_MASK;\r
106 break;\r
107 default:\r
108 // unexpected value -> shareable is the safe option\r
109 ASSERT (FALSE);\r
110 return FALSE;\r
111 }\r
112 return Val != ID_MMFR0_SHR_IMP_HW_COHERENT;\r
113}\r
114\r
115STATIC\r
116VOID\r
117PopulateLevel2PageTable (\r
118 IN UINT32 *SectionEntry,\r
119 IN UINT32 PhysicalBase,\r
120 IN UINT32 RemainLength,\r
121 IN ARM_MEMORY_REGION_ATTRIBUTES Attributes\r
122 )\r
123{\r
124 UINT32* PageEntry;\r
125 UINT32 Pages;\r
126 UINT32 Index;\r
127 UINT32 PageAttributes;\r
128 UINT32 SectionDescriptor;\r
129 UINT32 TranslationTable;\r
130 UINT32 BaseSectionAddress;\r
889c7ca1 131 UINT32 FirstPageOffset;\r
d7f03464
AB
132\r
133 switch (Attributes) {\r
134 case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK:\r
135 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_BACK:\r
136 PageAttributes = TT_DESCRIPTOR_PAGE_WRITE_BACK;\r
137 break;\r
138 case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_THROUGH:\r
139 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_THROUGH:\r
140 PageAttributes = TT_DESCRIPTOR_PAGE_WRITE_THROUGH;\r
141 break;\r
142 case ARM_MEMORY_REGION_ATTRIBUTE_DEVICE:\r
143 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_DEVICE:\r
144 PageAttributes = TT_DESCRIPTOR_PAGE_DEVICE;\r
145 break;\r
146 case ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED:\r
147 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_UNCACHED_UNBUFFERED:\r
148 PageAttributes = TT_DESCRIPTOR_PAGE_UNCACHED;\r
149 break;\r
150 default:\r
151 PageAttributes = TT_DESCRIPTOR_PAGE_UNCACHED;\r
152 break;\r
153 }\r
154\r
155 if (PreferNonshareableMemory ()) {\r
156 PageAttributes &= ~TT_DESCRIPTOR_PAGE_S_SHARED;\r
157 }\r
158\r
159 // Check if the Section Entry has already been populated. Otherwise attach a\r
160 // Level 2 Translation Table to it\r
161 if (*SectionEntry != 0) {\r
162 // The entry must be a page table. Otherwise it exists an overlapping in the memory map\r
163 if (TT_DESCRIPTOR_SECTION_TYPE_IS_PAGE_TABLE(*SectionEntry)) {\r
164 TranslationTable = *SectionEntry & TT_DESCRIPTOR_SECTION_PAGETABLE_ADDRESS_MASK;\r
165 } else if ((*SectionEntry & TT_DESCRIPTOR_SECTION_TYPE_MASK) == TT_DESCRIPTOR_SECTION_TYPE_SECTION) {\r
166 // Case where a virtual memory map descriptor overlapped a section entry\r
167\r
168 // Allocate a Level2 Page Table for this Section\r
169 TranslationTable = (UINTN)AllocatePages(EFI_SIZE_TO_PAGES(TRANSLATION_TABLE_PAGE_SIZE + TRANSLATION_TABLE_PAGE_ALIGNMENT));\r
170 TranslationTable = ((UINTN)TranslationTable + TRANSLATION_TABLE_PAGE_ALIGNMENT_MASK) & ~TRANSLATION_TABLE_PAGE_ALIGNMENT_MASK;\r
171\r
172 // Translate the Section Descriptor into Page Descriptor\r
173 SectionDescriptor = TT_DESCRIPTOR_PAGE_TYPE_PAGE | ConvertSectionAttributesToPageAttributes (*SectionEntry, FALSE);\r
174\r
175 BaseSectionAddress = TT_DESCRIPTOR_SECTION_BASE_ADDRESS(*SectionEntry);\r
176\r
177 // Populate the new Level2 Page Table for the section\r
178 PageEntry = (UINT32*)TranslationTable;\r
179 for (Index = 0; Index < TRANSLATION_TABLE_PAGE_COUNT; Index++) {\r
180 PageEntry[Index] = TT_DESCRIPTOR_PAGE_BASE_ADDRESS(BaseSectionAddress + (Index << 12)) | SectionDescriptor;\r
181 }\r
182\r
183 // Overwrite the section entry to point to the new Level2 Translation Table\r
184 *SectionEntry = (TranslationTable & TT_DESCRIPTOR_SECTION_PAGETABLE_ADDRESS_MASK) |\r
185 (IS_ARM_MEMORY_REGION_ATTRIBUTES_SECURE(Attributes) ? (1 << 3) : 0) |\r
186 TT_DESCRIPTOR_SECTION_TYPE_PAGE_TABLE;\r
187 } else {\r
188 // We do not support the other section type (16MB Section)\r
189 ASSERT(0);\r
190 return;\r
191 }\r
192 } else {\r
193 TranslationTable = (UINTN)AllocatePages(EFI_SIZE_TO_PAGES(TRANSLATION_TABLE_PAGE_SIZE + TRANSLATION_TABLE_PAGE_ALIGNMENT));\r
194 TranslationTable = ((UINTN)TranslationTable + TRANSLATION_TABLE_PAGE_ALIGNMENT_MASK) & ~TRANSLATION_TABLE_PAGE_ALIGNMENT_MASK;\r
195\r
196 ZeroMem ((VOID *)TranslationTable, TRANSLATION_TABLE_PAGE_SIZE);\r
197\r
198 *SectionEntry = (TranslationTable & TT_DESCRIPTOR_SECTION_PAGETABLE_ADDRESS_MASK) |\r
199 (IS_ARM_MEMORY_REGION_ATTRIBUTES_SECURE(Attributes) ? (1 << 3) : 0) |\r
200 TT_DESCRIPTOR_SECTION_TYPE_PAGE_TABLE;\r
201 }\r
202\r
889c7ca1
MZ
203 FirstPageOffset = (PhysicalBase & TT_DESCRIPTOR_PAGE_INDEX_MASK) >> TT_DESCRIPTOR_PAGE_BASE_SHIFT;\r
204 PageEntry = (UINT32 *)TranslationTable + FirstPageOffset;\r
d7f03464
AB
205 Pages = RemainLength / TT_DESCRIPTOR_PAGE_SIZE;\r
206\r
889c7ca1
MZ
207 ASSERT (FirstPageOffset + Pages <= TRANSLATION_TABLE_PAGE_COUNT);\r
208\r
d7f03464
AB
209 for (Index = 0; Index < Pages; Index++) {\r
210 *PageEntry++ = TT_DESCRIPTOR_PAGE_BASE_ADDRESS(PhysicalBase) | PageAttributes;\r
211 PhysicalBase += TT_DESCRIPTOR_PAGE_SIZE;\r
212 }\r
213\r
214}\r
215\r
216STATIC\r
217VOID\r
218FillTranslationTable (\r
219 IN UINT32 *TranslationTable,\r
220 IN ARM_MEMORY_REGION_DESCRIPTOR *MemoryRegion\r
221 )\r
222{\r
223 UINT32 *SectionEntry;\r
224 UINT32 Attributes;\r
225 UINT32 PhysicalBase;\r
226 UINT64 RemainLength;\r
889c7ca1 227 UINT32 PageMapLength;\r
d7f03464
AB
228\r
229 ASSERT(MemoryRegion->Length > 0);\r
230\r
231 if (MemoryRegion->PhysicalBase >= SIZE_4GB) {\r
232 return;\r
233 }\r
234\r
235 PhysicalBase = MemoryRegion->PhysicalBase;\r
236 RemainLength = MIN(MemoryRegion->Length, SIZE_4GB - PhysicalBase);\r
237\r
238 switch (MemoryRegion->Attributes) {\r
239 case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK:\r
240 Attributes = TT_DESCRIPTOR_SECTION_WRITE_BACK(0);\r
241 break;\r
242 case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_THROUGH:\r
243 Attributes = TT_DESCRIPTOR_SECTION_WRITE_THROUGH(0);\r
244 break;\r
245 case ARM_MEMORY_REGION_ATTRIBUTE_DEVICE:\r
246 Attributes = TT_DESCRIPTOR_SECTION_DEVICE(0);\r
247 break;\r
248 case ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED:\r
249 Attributes = TT_DESCRIPTOR_SECTION_UNCACHED(0);\r
250 break;\r
251 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_BACK:\r
252 Attributes = TT_DESCRIPTOR_SECTION_WRITE_BACK(1);\r
253 break;\r
254 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_THROUGH:\r
255 Attributes = TT_DESCRIPTOR_SECTION_WRITE_THROUGH(1);\r
256 break;\r
257 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_DEVICE:\r
258 Attributes = TT_DESCRIPTOR_SECTION_DEVICE(1);\r
259 break;\r
260 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_UNCACHED_UNBUFFERED:\r
261 Attributes = TT_DESCRIPTOR_SECTION_UNCACHED(1);\r
262 break;\r
263 default:\r
264 Attributes = TT_DESCRIPTOR_SECTION_UNCACHED(0);\r
265 break;\r
266 }\r
267\r
268 if (PreferNonshareableMemory ()) {\r
269 Attributes &= ~TT_DESCRIPTOR_SECTION_S_SHARED;\r
270 }\r
271\r
272 // Get the first section entry for this mapping\r
273 SectionEntry = TRANSLATION_TABLE_ENTRY_FOR_VIRTUAL_ADDRESS(TranslationTable, MemoryRegion->VirtualBase);\r
274\r
275 while (RemainLength != 0) {\r
889c7ca1
MZ
276 if (PhysicalBase % TT_DESCRIPTOR_SECTION_SIZE == 0 &&\r
277 RemainLength >= TT_DESCRIPTOR_SECTION_SIZE) {\r
278 // Case: Physical address aligned on the Section Size (1MB) && the length\r
279 // is greater than the Section Size\r
280 *SectionEntry++ = TT_DESCRIPTOR_SECTION_BASE_ADDRESS(PhysicalBase) | Attributes;\r
281 PhysicalBase += TT_DESCRIPTOR_SECTION_SIZE;\r
282 RemainLength -= TT_DESCRIPTOR_SECTION_SIZE;\r
d7f03464 283 } else {\r
889c7ca1
MZ
284 PageMapLength = MIN (RemainLength, TT_DESCRIPTOR_SECTION_SIZE) -\r
285 (PhysicalBase % TT_DESCRIPTOR_SECTION_SIZE);\r
286\r
287 // Case: Physical address aligned on the Section Size (1MB) && the length\r
288 // does not fill a section\r
d7f03464 289 // Case: Physical address NOT aligned on the Section Size (1MB)\r
889c7ca1
MZ
290 PopulateLevel2PageTable (SectionEntry++, PhysicalBase, PageMapLength,\r
291 MemoryRegion->Attributes);\r
d7f03464
AB
292\r
293 // If it is the last entry\r
294 if (RemainLength < TT_DESCRIPTOR_SECTION_SIZE) {\r
295 break;\r
296 }\r
889c7ca1
MZ
297\r
298 PhysicalBase += PageMapLength;\r
299 RemainLength -= PageMapLength;\r
d7f03464 300 }\r
d7f03464
AB
301 }\r
302}\r
303\r
304RETURN_STATUS\r
305EFIAPI\r
306ArmConfigureMmu (\r
307 IN ARM_MEMORY_REGION_DESCRIPTOR *MemoryTable,\r
308 OUT VOID **TranslationTableBase OPTIONAL,\r
309 OUT UINTN *TranslationTableSize OPTIONAL\r
310 )\r
311{\r
312 VOID* TranslationTable;\r
313 ARM_MEMORY_REGION_ATTRIBUTES TranslationTableAttribute;\r
314 UINT32 TTBRAttributes;\r
315\r
316 // Allocate pages for translation table.\r
317 TranslationTable = AllocatePages (EFI_SIZE_TO_PAGES (TRANSLATION_TABLE_SECTION_SIZE + TRANSLATION_TABLE_SECTION_ALIGNMENT));\r
318 if (TranslationTable == NULL) {\r
319 return RETURN_OUT_OF_RESOURCES;\r
320 }\r
321 TranslationTable = (VOID*)(((UINTN)TranslationTable + TRANSLATION_TABLE_SECTION_ALIGNMENT_MASK) & ~TRANSLATION_TABLE_SECTION_ALIGNMENT_MASK);\r
322\r
323 if (TranslationTableBase != NULL) {\r
324 *TranslationTableBase = TranslationTable;\r
325 }\r
326\r
327 if (TranslationTableSize != NULL) {\r
328 *TranslationTableSize = TRANSLATION_TABLE_SECTION_SIZE;\r
329 }\r
330\r
331 ZeroMem (TranslationTable, TRANSLATION_TABLE_SECTION_SIZE);\r
332\r
333 // By default, mark the translation table as belonging to a uncached region\r
334 TranslationTableAttribute = ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED;\r
335 while (MemoryTable->Length != 0) {\r
336 // Find the memory attribute for the Translation Table\r
337 if (((UINTN)TranslationTable >= MemoryTable->PhysicalBase) && ((UINTN)TranslationTable <= MemoryTable->PhysicalBase - 1 + MemoryTable->Length)) {\r
338 TranslationTableAttribute = MemoryTable->Attributes;\r
339 }\r
340\r
341 FillTranslationTable (TranslationTable, MemoryTable);\r
342 MemoryTable++;\r
343 }\r
344\r
345 // Translate the Memory Attributes into Translation Table Register Attributes\r
346 if ((TranslationTableAttribute == ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED) ||\r
347 (TranslationTableAttribute == ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_UNCACHED_UNBUFFERED)) {\r
348 TTBRAttributes = ArmHasMpExtensions () ? TTBR_MP_NON_CACHEABLE : TTBR_NON_CACHEABLE;\r
349 } else if ((TranslationTableAttribute == ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK) ||\r
350 (TranslationTableAttribute == ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_BACK)) {\r
351 TTBRAttributes = ArmHasMpExtensions () ? TTBR_MP_WRITE_BACK_ALLOC : TTBR_WRITE_BACK_ALLOC;\r
352 } else if ((TranslationTableAttribute == ARM_MEMORY_REGION_ATTRIBUTE_WRITE_THROUGH) ||\r
353 (TranslationTableAttribute == ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_THROUGH)) {\r
354 TTBRAttributes = ArmHasMpExtensions () ? TTBR_MP_WRITE_THROUGH : TTBR_WRITE_THROUGH;\r
355 } else {\r
356 ASSERT (0); // No support has been found for the attributes of the memory region that the translation table belongs to.\r
357 return RETURN_UNSUPPORTED;\r
358 }\r
359\r
360 if (TTBRAttributes & TTBR_SHAREABLE) {\r
361 if (PreferNonshareableMemory ()) {\r
362 TTBRAttributes ^= TTBR_SHAREABLE;\r
363 } else {\r
364 //\r
365 // Unlike the S bit in the short descriptors, which implies inner shareable\r
366 // on an implementation that supports two levels, the meaning of the S bit\r
367 // in the TTBR depends on the NOS bit, which defaults to Outer Shareable.\r
368 // However, we should only set this bit after we have confirmed that the\r
369 // implementation supports multiple levels, or else the NOS bit is UNK/SBZP\r
370 //\r
371 if (((ArmReadIdMmfr0 () >> 12) & 0xf) != 0) {\r
372 TTBRAttributes |= TTBR_NOT_OUTER_SHAREABLE;\r
373 }\r
374 }\r
375 }\r
376\r
377 ArmCleanInvalidateDataCache ();\r
378 ArmInvalidateInstructionCache ();\r
379\r
380 ArmDisableDataCache ();\r
381 ArmDisableInstructionCache();\r
382 // TLBs are also invalidated when calling ArmDisableMmu()\r
383 ArmDisableMmu ();\r
384\r
385 // Make sure nothing sneaked into the cache\r
386 ArmCleanInvalidateDataCache ();\r
387 ArmInvalidateInstructionCache ();\r
388\r
389 ArmSetTTBR0 ((VOID *)(UINTN)(((UINTN)TranslationTable & ~TRANSLATION_TABLE_SECTION_ALIGNMENT_MASK) | (TTBRAttributes & 0x7F)));\r
390\r
391 //\r
392 // The TTBCR register value is undefined at reset in the Non-Secure world.\r
393 // Writing 0 has the effect of:\r
394 // Clearing EAE: Use short descriptors, as mandated by specification.\r
395 // Clearing PD0 and PD1: Translation Table Walk Disable is off.\r
396 // Clearing N: Perform all translation table walks through TTBR0.\r
397 // (0 is the default reset value in systems not implementing\r
398 // the Security Extensions.)\r
399 //\r
400 ArmSetTTBCR (0);\r
401\r
402 ArmSetDomainAccessControl (DOMAIN_ACCESS_CONTROL_NONE(15) |\r
403 DOMAIN_ACCESS_CONTROL_NONE(14) |\r
404 DOMAIN_ACCESS_CONTROL_NONE(13) |\r
405 DOMAIN_ACCESS_CONTROL_NONE(12) |\r
406 DOMAIN_ACCESS_CONTROL_NONE(11) |\r
407 DOMAIN_ACCESS_CONTROL_NONE(10) |\r
408 DOMAIN_ACCESS_CONTROL_NONE( 9) |\r
409 DOMAIN_ACCESS_CONTROL_NONE( 8) |\r
410 DOMAIN_ACCESS_CONTROL_NONE( 7) |\r
411 DOMAIN_ACCESS_CONTROL_NONE( 6) |\r
412 DOMAIN_ACCESS_CONTROL_NONE( 5) |\r
413 DOMAIN_ACCESS_CONTROL_NONE( 4) |\r
414 DOMAIN_ACCESS_CONTROL_NONE( 3) |\r
415 DOMAIN_ACCESS_CONTROL_NONE( 2) |\r
416 DOMAIN_ACCESS_CONTROL_NONE( 1) |\r
417 DOMAIN_ACCESS_CONTROL_CLIENT(0));\r
418\r
419 ArmEnableInstructionCache();\r
420 ArmEnableDataCache();\r
421 ArmEnableMmu();\r
422 return RETURN_SUCCESS;\r
423}\r
424\r
521f3ced
AB
425STATIC\r
426EFI_STATUS\r
427ConvertSectionToPages (\r
428 IN EFI_PHYSICAL_ADDRESS BaseAddress\r
429 )\r
430{\r
431 UINT32 FirstLevelIdx;\r
432 UINT32 SectionDescriptor;\r
433 UINT32 PageTableDescriptor;\r
434 UINT32 PageDescriptor;\r
435 UINT32 Index;\r
436\r
437 volatile ARM_FIRST_LEVEL_DESCRIPTOR *FirstLevelTable;\r
438 volatile ARM_PAGE_TABLE_ENTRY *PageTable;\r
439\r
440 DEBUG ((EFI_D_PAGE, "Converting section at 0x%x to pages\n", (UINTN)BaseAddress));\r
441\r
442 // Obtain page table base\r
443 FirstLevelTable = (ARM_FIRST_LEVEL_DESCRIPTOR *)ArmGetTTBR0BaseAddress ();\r
444\r
445 // Calculate index into first level translation table for start of modification\r
446 FirstLevelIdx = TT_DESCRIPTOR_SECTION_BASE_ADDRESS(BaseAddress) >> TT_DESCRIPTOR_SECTION_BASE_SHIFT;\r
447 ASSERT (FirstLevelIdx < TRANSLATION_TABLE_SECTION_COUNT);\r
448\r
449 // Get section attributes and convert to page attributes\r
450 SectionDescriptor = FirstLevelTable[FirstLevelIdx];\r
451 PageDescriptor = TT_DESCRIPTOR_PAGE_TYPE_PAGE | ConvertSectionAttributesToPageAttributes (SectionDescriptor, FALSE);\r
452\r
453 // Allocate a page table for the 4KB entries (we use up a full page even though we only need 1KB)\r
454 PageTable = (volatile ARM_PAGE_TABLE_ENTRY *)AllocatePages (1);\r
455 if (PageTable == NULL) {\r
456 return EFI_OUT_OF_RESOURCES;\r
457 }\r
458\r
459 // Write the page table entries out\r
460 for (Index = 0; Index < TRANSLATION_TABLE_PAGE_COUNT; Index++) {\r
461 PageTable[Index] = TT_DESCRIPTOR_PAGE_BASE_ADDRESS(BaseAddress + (Index << 12)) | PageDescriptor;\r
462 }\r
463\r
464 // Flush d-cache so descriptors make it back to uncached memory for subsequent table walks\r
465 WriteBackInvalidateDataCacheRange ((VOID *)PageTable, TT_DESCRIPTOR_PAGE_SIZE);\r
466\r
467 // Formulate page table entry, Domain=0, NS=0\r
468 PageTableDescriptor = (((UINTN)PageTable) & TT_DESCRIPTOR_SECTION_PAGETABLE_ADDRESS_MASK) | TT_DESCRIPTOR_SECTION_TYPE_PAGE_TABLE;\r
469\r
470 // Write the page table entry out, replacing section entry\r
471 FirstLevelTable[FirstLevelIdx] = PageTableDescriptor;\r
472\r
473 return EFI_SUCCESS;\r
474}\r
475\r
476STATIC\r
477EFI_STATUS\r
478UpdatePageEntries (\r
479 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
480 IN UINT64 Length,\r
481 IN UINT64 Attributes,\r
521f3ced
AB
482 OUT BOOLEAN *FlushTlbs OPTIONAL\r
483 )\r
484{\r
485 EFI_STATUS Status;\r
486 UINT32 EntryValue;\r
487 UINT32 EntryMask;\r
488 UINT32 FirstLevelIdx;\r
489 UINT32 Offset;\r
490 UINT32 NumPageEntries;\r
491 UINT32 Descriptor;\r
492 UINT32 p;\r
493 UINT32 PageTableIndex;\r
494 UINT32 PageTableEntry;\r
495 UINT32 CurrentPageTableEntry;\r
496 VOID *Mva;\r
497\r
498 volatile ARM_FIRST_LEVEL_DESCRIPTOR *FirstLevelTable;\r
499 volatile ARM_PAGE_TABLE_ENTRY *PageTable;\r
500\r
501 Status = EFI_SUCCESS;\r
502\r
503 // EntryMask: bitmask of values to change (1 = change this value, 0 = leave alone)\r
504 // EntryValue: values at bit positions specified by EntryMask\r
505 EntryMask = TT_DESCRIPTOR_PAGE_TYPE_MASK | TT_DESCRIPTOR_PAGE_AP_MASK;\r
506 if (Attributes & EFI_MEMORY_XP) {\r
507 EntryValue = TT_DESCRIPTOR_PAGE_TYPE_PAGE_XN;\r
508 } else {\r
509 EntryValue = TT_DESCRIPTOR_PAGE_TYPE_PAGE;\r
510 }\r
511\r
512 // Although the PI spec is unclear on this, the GCD guarantees that only\r
513 // one Attribute bit is set at a time, so the order of the conditionals below\r
514 // is irrelevant. If no memory attribute is specified, we preserve whatever\r
515 // memory type is set in the page tables, and update the permission attributes\r
516 // only.\r
517 if (Attributes & EFI_MEMORY_UC) {\r
518 // modify cacheability attributes\r
519 EntryMask |= TT_DESCRIPTOR_PAGE_CACHE_POLICY_MASK;\r
520 // map to strongly ordered\r
521 EntryValue |= TT_DESCRIPTOR_PAGE_CACHE_POLICY_STRONGLY_ORDERED; // TEX[2:0] = 0, C=0, B=0\r
522 } else if (Attributes & EFI_MEMORY_WC) {\r
523 // modify cacheability attributes\r
524 EntryMask |= TT_DESCRIPTOR_PAGE_CACHE_POLICY_MASK;\r
525 // map to normal non-cachable\r
526 EntryValue |= TT_DESCRIPTOR_PAGE_CACHE_POLICY_NON_CACHEABLE; // TEX [2:0]= 001 = 0x2, B=0, C=0\r
527 } else if (Attributes & EFI_MEMORY_WT) {\r
528 // modify cacheability attributes\r
529 EntryMask |= TT_DESCRIPTOR_PAGE_CACHE_POLICY_MASK;\r
530 // write through with no-allocate\r
531 EntryValue |= TT_DESCRIPTOR_PAGE_CACHE_POLICY_WRITE_THROUGH_NO_ALLOC; // TEX [2:0] = 0, C=1, B=0\r
532 } else if (Attributes & EFI_MEMORY_WB) {\r
533 // modify cacheability attributes\r
534 EntryMask |= TT_DESCRIPTOR_PAGE_CACHE_POLICY_MASK;\r
535 // write back (with allocate)\r
536 EntryValue |= TT_DESCRIPTOR_PAGE_CACHE_POLICY_WRITE_BACK_ALLOC; // TEX [2:0] = 001, C=1, B=1\r
537 } else if (Attributes & CACHE_ATTRIBUTE_MASK) {\r
538 // catch unsupported memory type attributes\r
539 ASSERT (FALSE);\r
540 return EFI_UNSUPPORTED;\r
541 }\r
542\r
543 if (Attributes & EFI_MEMORY_RO) {\r
544 EntryValue |= TT_DESCRIPTOR_PAGE_AP_RO_RO;\r
545 } else {\r
546 EntryValue |= TT_DESCRIPTOR_PAGE_AP_RW_RW;\r
547 }\r
548\r
549 // Obtain page table base\r
550 FirstLevelTable = (ARM_FIRST_LEVEL_DESCRIPTOR *)ArmGetTTBR0BaseAddress ();\r
551\r
552 // Calculate number of 4KB page table entries to change\r
553 NumPageEntries = Length / TT_DESCRIPTOR_PAGE_SIZE;\r
554\r
555 // Iterate for the number of 4KB pages to change\r
556 Offset = 0;\r
557 for(p = 0; p < NumPageEntries; p++) {\r
558 // Calculate index into first level translation table for page table value\r
559\r
560 FirstLevelIdx = TT_DESCRIPTOR_SECTION_BASE_ADDRESS(BaseAddress + Offset) >> TT_DESCRIPTOR_SECTION_BASE_SHIFT;\r
561 ASSERT (FirstLevelIdx < TRANSLATION_TABLE_SECTION_COUNT);\r
562\r
563 // Read the descriptor from the first level page table\r
564 Descriptor = FirstLevelTable[FirstLevelIdx];\r
565\r
566 // Does this descriptor need to be converted from section entry to 4K pages?\r
567 if (!TT_DESCRIPTOR_SECTION_TYPE_IS_PAGE_TABLE(Descriptor)) {\r
568 Status = ConvertSectionToPages (FirstLevelIdx << TT_DESCRIPTOR_SECTION_BASE_SHIFT);\r
569 if (EFI_ERROR(Status)) {\r
570 // Exit for loop\r
571 break;\r
572 }\r
573\r
574 // Re-read descriptor\r
575 Descriptor = FirstLevelTable[FirstLevelIdx];\r
576 if (FlushTlbs != NULL) {\r
577 *FlushTlbs = TRUE;\r
578 }\r
579 }\r
580\r
581 // Obtain page table base address\r
582 PageTable = (ARM_PAGE_TABLE_ENTRY *)TT_DESCRIPTOR_PAGE_BASE_ADDRESS(Descriptor);\r
583\r
584 // Calculate index into the page table\r
585 PageTableIndex = ((BaseAddress + Offset) & TT_DESCRIPTOR_PAGE_INDEX_MASK) >> TT_DESCRIPTOR_PAGE_BASE_SHIFT;\r
586 ASSERT (PageTableIndex < TRANSLATION_TABLE_PAGE_COUNT);\r
587\r
588 // Get the entry\r
589 CurrentPageTableEntry = PageTable[PageTableIndex];\r
590\r
591 // Mask off appropriate fields\r
592 PageTableEntry = CurrentPageTableEntry & ~EntryMask;\r
593\r
594 // Mask in new attributes and/or permissions\r
595 PageTableEntry |= EntryValue;\r
596\r
521f3ced
AB
597 if (CurrentPageTableEntry != PageTableEntry) {\r
598 Mva = (VOID *)(UINTN)((((UINTN)FirstLevelIdx) << TT_DESCRIPTOR_SECTION_BASE_SHIFT) + (PageTableIndex << TT_DESCRIPTOR_PAGE_BASE_SHIFT));\r
599\r
600 // Clean/invalidate the cache for this page, but only\r
601 // if we are modifying the memory type attributes\r
602 if (((CurrentPageTableEntry ^ PageTableEntry) & TT_DESCRIPTOR_PAGE_CACHE_POLICY_MASK) != 0) {\r
603 WriteBackInvalidateDataCacheRange (Mva, TT_DESCRIPTOR_PAGE_SIZE);\r
604 }\r
605\r
606 // Only need to update if we are changing the entry\r
607 PageTable[PageTableIndex] = PageTableEntry;\r
608 ArmUpdateTranslationTableEntry ((VOID *)&PageTable[PageTableIndex], Mva);\r
609 }\r
610\r
611 Status = EFI_SUCCESS;\r
612 Offset += TT_DESCRIPTOR_PAGE_SIZE;\r
613\r
614 } // End first level translation table loop\r
615\r
616 return Status;\r
617}\r
618\r
619STATIC\r
620EFI_STATUS\r
621UpdateSectionEntries (\r
622 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
623 IN UINT64 Length,\r
d9c0d991 624 IN UINT64 Attributes\r
521f3ced
AB
625 )\r
626{\r
627 EFI_STATUS Status = EFI_SUCCESS;\r
628 UINT32 EntryMask;\r
629 UINT32 EntryValue;\r
630 UINT32 FirstLevelIdx;\r
631 UINT32 NumSections;\r
632 UINT32 i;\r
633 UINT32 CurrentDescriptor;\r
634 UINT32 Descriptor;\r
635 VOID *Mva;\r
636 volatile ARM_FIRST_LEVEL_DESCRIPTOR *FirstLevelTable;\r
637\r
638 // EntryMask: bitmask of values to change (1 = change this value, 0 = leave alone)\r
639 // EntryValue: values at bit positions specified by EntryMask\r
640\r
641 // Make sure we handle a section range that is unmapped\r
642 EntryMask = TT_DESCRIPTOR_SECTION_TYPE_MASK | TT_DESCRIPTOR_SECTION_XN_MASK |\r
643 TT_DESCRIPTOR_SECTION_AP_MASK;\r
644 EntryValue = TT_DESCRIPTOR_SECTION_TYPE_SECTION;\r
645\r
646 // Although the PI spec is unclear on this, the GCD guarantees that only\r
647 // one Attribute bit is set at a time, so the order of the conditionals below\r
648 // is irrelevant. If no memory attribute is specified, we preserve whatever\r
649 // memory type is set in the page tables, and update the permission attributes\r
650 // only.\r
651 if (Attributes & EFI_MEMORY_UC) {\r
652 // modify cacheability attributes\r
653 EntryMask |= TT_DESCRIPTOR_SECTION_CACHE_POLICY_MASK;\r
654 // map to strongly ordered\r
655 EntryValue |= TT_DESCRIPTOR_SECTION_CACHE_POLICY_STRONGLY_ORDERED; // TEX[2:0] = 0, C=0, B=0\r
656 } else if (Attributes & EFI_MEMORY_WC) {\r
657 // modify cacheability attributes\r
658 EntryMask |= TT_DESCRIPTOR_SECTION_CACHE_POLICY_MASK;\r
659 // map to normal non-cachable\r
660 EntryValue |= TT_DESCRIPTOR_SECTION_CACHE_POLICY_NON_CACHEABLE; // TEX [2:0]= 001 = 0x2, B=0, C=0\r
661 } else if (Attributes & EFI_MEMORY_WT) {\r
662 // modify cacheability attributes\r
663 EntryMask |= TT_DESCRIPTOR_SECTION_CACHE_POLICY_MASK;\r
664 // write through with no-allocate\r
665 EntryValue |= TT_DESCRIPTOR_SECTION_CACHE_POLICY_WRITE_THROUGH_NO_ALLOC; // TEX [2:0] = 0, C=1, B=0\r
666 } else if (Attributes & EFI_MEMORY_WB) {\r
667 // modify cacheability attributes\r
668 EntryMask |= TT_DESCRIPTOR_SECTION_CACHE_POLICY_MASK;\r
669 // write back (with allocate)\r
670 EntryValue |= TT_DESCRIPTOR_SECTION_CACHE_POLICY_WRITE_BACK_ALLOC; // TEX [2:0] = 001, C=1, B=1\r
671 } else if (Attributes & CACHE_ATTRIBUTE_MASK) {\r
672 // catch unsupported memory type attributes\r
673 ASSERT (FALSE);\r
674 return EFI_UNSUPPORTED;\r
675 }\r
676\r
677 if (Attributes & EFI_MEMORY_RO) {\r
678 EntryValue |= TT_DESCRIPTOR_SECTION_AP_RO_RO;\r
679 } else {\r
680 EntryValue |= TT_DESCRIPTOR_SECTION_AP_RW_RW;\r
681 }\r
682\r
683 if (Attributes & EFI_MEMORY_XP) {\r
684 EntryValue |= TT_DESCRIPTOR_SECTION_XN_MASK;\r
685 }\r
686\r
687 // obtain page table base\r
688 FirstLevelTable = (ARM_FIRST_LEVEL_DESCRIPTOR *)ArmGetTTBR0BaseAddress ();\r
689\r
690 // calculate index into first level translation table for start of modification\r
691 FirstLevelIdx = TT_DESCRIPTOR_SECTION_BASE_ADDRESS(BaseAddress) >> TT_DESCRIPTOR_SECTION_BASE_SHIFT;\r
692 ASSERT (FirstLevelIdx < TRANSLATION_TABLE_SECTION_COUNT);\r
693\r
694 // calculate number of 1MB first level entries this applies to\r
695 NumSections = Length / TT_DESCRIPTOR_SECTION_SIZE;\r
696\r
697 // iterate through each descriptor\r
698 for(i=0; i<NumSections; i++) {\r
699 CurrentDescriptor = FirstLevelTable[FirstLevelIdx + i];\r
700\r
701 // has this descriptor already been coverted to pages?\r
702 if (TT_DESCRIPTOR_SECTION_TYPE_IS_PAGE_TABLE(CurrentDescriptor)) {\r
703 // forward this 1MB range to page table function instead\r
704 Status = UpdatePageEntries (\r
705 (FirstLevelIdx + i) << TT_DESCRIPTOR_SECTION_BASE_SHIFT,\r
706 TT_DESCRIPTOR_SECTION_SIZE,\r
707 Attributes,\r
521f3ced
AB
708 NULL);\r
709 } else {\r
710 // still a section entry\r
711\r
712 // mask off appropriate fields\r
713 Descriptor = CurrentDescriptor & ~EntryMask;\r
714\r
715 // mask in new attributes and/or permissions\r
716 Descriptor |= EntryValue;\r
521f3ced
AB
717\r
718 if (CurrentDescriptor != Descriptor) {\r
719 Mva = (VOID *)(UINTN)(((UINTN)FirstLevelTable) << TT_DESCRIPTOR_SECTION_BASE_SHIFT);\r
720\r
721 // Clean/invalidate the cache for this section, but only\r
722 // if we are modifying the memory type attributes\r
723 if (((CurrentDescriptor ^ Descriptor) & TT_DESCRIPTOR_SECTION_CACHE_POLICY_MASK) != 0) {\r
724 WriteBackInvalidateDataCacheRange (Mva, SIZE_1MB);\r
725 }\r
726\r
727 // Only need to update if we are changing the descriptor\r
728 FirstLevelTable[FirstLevelIdx + i] = Descriptor;\r
729 ArmUpdateTranslationTableEntry ((VOID *)&FirstLevelTable[FirstLevelIdx + i], Mva);\r
730 }\r
731\r
732 Status = EFI_SUCCESS;\r
733 }\r
734 }\r
735\r
736 return Status;\r
737}\r
738\r
739EFI_STATUS\r
740ArmSetMemoryAttributes (\r
741 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
742 IN UINT64 Length,\r
d9c0d991 743 IN UINT64 Attributes\r
521f3ced
AB
744 )\r
745{\r
746 EFI_STATUS Status;\r
747 UINT64 ChunkLength;\r
748 BOOLEAN FlushTlbs;\r
749\r
750 if (Length == 0) {\r
751 return EFI_SUCCESS;\r
752 }\r
753\r
754 FlushTlbs = FALSE;\r
755 while (Length > 0) {\r
756 if ((BaseAddress % TT_DESCRIPTOR_SECTION_SIZE == 0) &&\r
757 Length >= TT_DESCRIPTOR_SECTION_SIZE) {\r
758\r
759 ChunkLength = Length - Length % TT_DESCRIPTOR_SECTION_SIZE;\r
760\r
761 DEBUG ((DEBUG_PAGE,\r
762 "SetMemoryAttributes(): MMU section 0x%lx length 0x%lx to %lx\n",\r
763 BaseAddress, ChunkLength, Attributes));\r
764\r
d9c0d991 765 Status = UpdateSectionEntries (BaseAddress, ChunkLength, Attributes);\r
521f3ced
AB
766\r
767 FlushTlbs = TRUE;\r
768 } else {\r
769\r
770 //\r
771 // Process page by page until the next section boundary, but only if\r
772 // we have more than a section's worth of area to deal with after that.\r
773 //\r
774 ChunkLength = TT_DESCRIPTOR_SECTION_SIZE -\r
775 (BaseAddress % TT_DESCRIPTOR_SECTION_SIZE);\r
776 if (ChunkLength + TT_DESCRIPTOR_SECTION_SIZE > Length) {\r
777 ChunkLength = Length;\r
778 }\r
779\r
780 DEBUG ((DEBUG_PAGE,\r
781 "SetMemoryAttributes(): MMU page 0x%lx length 0x%lx to %lx\n",\r
782 BaseAddress, ChunkLength, Attributes));\r
783\r
784 Status = UpdatePageEntries (BaseAddress, ChunkLength, Attributes,\r
d9c0d991 785 &FlushTlbs);\r
521f3ced
AB
786 }\r
787\r
788 if (EFI_ERROR (Status)) {\r
789 break;\r
790 }\r
791\r
792 BaseAddress += ChunkLength;\r
793 Length -= ChunkLength;\r
794 }\r
795\r
796 if (FlushTlbs) {\r
797 ArmInvalidateTlb ();\r
798 }\r
799 return Status;\r
800}\r
f49ea03d
AB
801\r
802EFI_STATUS\r
d7f03464
AB
803ArmSetMemoryRegionNoExec (\r
804 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
805 IN UINT64 Length\r
806 )\r
807{\r
9f1dcbe8 808 return ArmSetMemoryAttributes (BaseAddress, Length, EFI_MEMORY_XP);\r
d7f03464
AB
809}\r
810\r
f49ea03d 811EFI_STATUS\r
d7f03464
AB
812ArmClearMemoryRegionNoExec (\r
813 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
814 IN UINT64 Length\r
815 )\r
816{\r
9f1dcbe8 817 return ArmSetMemoryAttributes (BaseAddress, Length, __EFI_MEMORY_RWX);\r
d7f03464
AB
818}\r
819\r
f49ea03d 820EFI_STATUS\r
d7f03464
AB
821ArmSetMemoryRegionReadOnly (\r
822 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
823 IN UINT64 Length\r
824 )\r
825{\r
9f1dcbe8 826 return ArmSetMemoryAttributes (BaseAddress, Length, EFI_MEMORY_RO);\r
d7f03464
AB
827}\r
828\r
f49ea03d 829EFI_STATUS\r
d7f03464
AB
830ArmClearMemoryRegionReadOnly (\r
831 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
832 IN UINT64 Length\r
833 )\r
834{\r
9f1dcbe8 835 return ArmSetMemoryAttributes (BaseAddress, Length, __EFI_MEMORY_RWX);\r
d7f03464
AB
836}\r
837\r
838RETURN_STATUS\r
839EFIAPI\r
840ArmMmuBaseLibConstructor (\r
841 VOID\r
842 )\r
843{\r
844 return RETURN_SUCCESS;\r
845}\r