]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/Gcd/Gcd.c
Update to fix minor coding style issues.
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Gcd / Gcd.c
1 /** @file
2 The file contains the GCD related services in the EFI Boot Services Table.
3 The GCD services are used to manage the memory and I/O regions that
4 are accessible to the CPU that is executing the DXE core.
5
6 Copyright (c) 2006 - 2008, Intel Corporation. <BR>
7 All rights reserved. This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 #include <DxeMain.h>
18
19 #define MINIMUM_INITIAL_MEMORY_SIZE 0x10000
20
21 #define MEMORY_ATTRIBUTE_MASK (EFI_RESOURCE_ATTRIBUTE_PRESENT | \
22 EFI_RESOURCE_ATTRIBUTE_INITIALIZED | \
23 EFI_RESOURCE_ATTRIBUTE_TESTED | \
24 EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED | \
25 EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED | \
26 EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED | \
27 EFI_RESOURCE_ATTRIBUTE_16_BIT_IO | \
28 EFI_RESOURCE_ATTRIBUTE_32_BIT_IO | \
29 EFI_RESOURCE_ATTRIBUTE_64_BIT_IO )
30
31 #define TESTED_MEMORY_ATTRIBUTES (EFI_RESOURCE_ATTRIBUTE_PRESENT | \
32 EFI_RESOURCE_ATTRIBUTE_INITIALIZED | \
33 EFI_RESOURCE_ATTRIBUTE_TESTED )
34
35 #define INITIALIZED_MEMORY_ATTRIBUTES (EFI_RESOURCE_ATTRIBUTE_PRESENT | \
36 EFI_RESOURCE_ATTRIBUTE_INITIALIZED )
37
38 #define PRESENT_MEMORY_ATTRIBUTES (EFI_RESOURCE_ATTRIBUTE_PRESENT)
39
40 #define INVALID_CPU_ARCH_ATTRIBUTES 0xffffffff
41
42 //
43 // Module Variables
44 //
45 EFI_LOCK mGcdMemorySpaceLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY);
46 EFI_LOCK mGcdIoSpaceLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY);
47 LIST_ENTRY mGcdMemorySpaceMap = INITIALIZE_LIST_HEAD_VARIABLE (mGcdMemorySpaceMap);
48 LIST_ENTRY mGcdIoSpaceMap = INITIALIZE_LIST_HEAD_VARIABLE (mGcdIoSpaceMap);
49
50 EFI_GCD_MAP_ENTRY mGcdMemorySpaceMapEntryTemplate = {
51 EFI_GCD_MAP_SIGNATURE,
52 { NULL, NULL },
53 0,
54 0,
55 0,
56 0,
57 EfiGcdMemoryTypeNonExistent,
58 (EFI_GCD_IO_TYPE) 0,
59 NULL,
60 NULL
61 };
62
63 EFI_GCD_MAP_ENTRY mGcdIoSpaceMapEntryTemplate = {
64 EFI_GCD_MAP_SIGNATURE,
65 { NULL, NULL },
66 0,
67 0,
68 0,
69 0,
70 (EFI_GCD_MEMORY_TYPE) 0,
71 EfiGcdIoTypeNonExistent,
72 NULL,
73 NULL
74 };
75
76 GCD_ATTRIBUTE_CONVERSION_ENTRY mAttributeConversionTable[] = {
77 { EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE, EFI_MEMORY_UC, TRUE },
78 { EFI_RESOURCE_ATTRIBUTE_UNCACHED_EXPORTED, EFI_MEMORY_UCE, TRUE },
79 { EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE, EFI_MEMORY_WC, TRUE },
80 { EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE, EFI_MEMORY_WT, TRUE },
81 { EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE, EFI_MEMORY_WB, TRUE },
82 { EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED, EFI_MEMORY_RP, TRUE },
83 { EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED, EFI_MEMORY_WP, TRUE },
84 { EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED, EFI_MEMORY_XP, TRUE },
85 { EFI_RESOURCE_ATTRIBUTE_PRESENT, EFI_MEMORY_PRESENT, FALSE },
86 { EFI_RESOURCE_ATTRIBUTE_INITIALIZED, EFI_MEMORY_INITIALIZED, FALSE },
87 { EFI_RESOURCE_ATTRIBUTE_TESTED, EFI_MEMORY_TESTED, FALSE },
88 { 0, 0, FALSE }
89 };
90
91
92 /**
93 Acquire memory lock on mGcdMemorySpaceLock.
94
95 **/
96 VOID
97 CoreAcquireGcdMemoryLock (
98 VOID
99 )
100 {
101 CoreAcquireLock (&mGcdMemorySpaceLock);
102 }
103
104
105
106 /**
107 Release memory lock on mGcdMemorySpaceLock.
108
109 **/
110 VOID
111 CoreReleaseGcdMemoryLock (
112 VOID
113 )
114 {
115 CoreReleaseLock (&mGcdMemorySpaceLock);
116 }
117
118
119
120 /**
121 Acquire memory lock on mGcdIoSpaceLock.
122
123 **/
124 VOID
125 CoreAcquireGcdIoLock (
126 VOID
127 )
128 {
129 CoreAcquireLock (&mGcdIoSpaceLock);
130 }
131
132
133 /**
134 Release memory lock on mGcdIoSpaceLock.
135
136 **/
137 VOID
138 CoreReleaseGcdIoLock (
139 VOID
140 )
141 {
142 CoreReleaseLock (&mGcdIoSpaceLock);
143 }
144
145
146
147 //
148 // GCD Initialization Worker Functions
149 //
150
151 /**
152 Aligns a value to the specified boundary.
153
154 @param Value 64 bit value to align
155 @param Alignment Log base 2 of the boundary to align Value to
156 @param RoundUp TRUE if Value is to be rounded up to the nearest
157 aligned boundary. FALSE is Value is to be
158 rounded down to the nearest aligned boundary.
159
160 @return A 64 bit value is the aligned to the value nearest Value with an alignment by Alignment.
161
162 **/
163 UINT64
164 AlignValue (
165 IN UINT64 Value,
166 IN UINTN Alignment,
167 IN BOOLEAN RoundUp
168 )
169 {
170 UINT64 AlignmentMask;
171
172 AlignmentMask = LShiftU64 (1, Alignment) - 1;
173 if (RoundUp) {
174 Value += AlignmentMask;
175 }
176 return Value & (~AlignmentMask);
177 }
178
179
180 /**
181 Aligns address to the page boundary.
182
183 @param Value 64 bit address to align
184
185 @return A 64 bit value is the aligned to the value nearest Value with an alignment by Alignment.
186
187 **/
188 UINT64
189 PageAlignAddress (
190 IN UINT64 Value
191 )
192 {
193 return AlignValue (Value, EFI_PAGE_SHIFT, TRUE);
194 }
195
196
197 /**
198 Aligns length to the page boundary.
199
200 @param Value 64 bit length to align
201
202 @return A 64 bit value is the aligned to the value nearest Value with an alignment by Alignment.
203
204 **/
205 UINT64
206 PageAlignLength (
207 IN UINT64 Value
208 )
209 {
210 return AlignValue (Value, EFI_PAGE_SHIFT, FALSE);
211 }
212
213 //
214 // GCD Memory Space Worker Functions
215 //
216
217 /**
218 Allocate pool for two entries.
219
220 @param TopEntry An entry of GCD map
221 @param BottomEntry An entry of GCD map
222
223 @retval EFI_OUT_OF_RESOURCES No enough buffer to be allocated.
224 @retval EFI_SUCCESS Both entries successfully allocated.
225
226 **/
227 EFI_STATUS
228 CoreAllocateGcdMapEntry (
229 IN OUT EFI_GCD_MAP_ENTRY **TopEntry,
230 IN OUT EFI_GCD_MAP_ENTRY **BottomEntry
231 )
232 {
233 *TopEntry = CoreAllocateZeroBootServicesPool (sizeof (EFI_GCD_MAP_ENTRY));
234 if (*TopEntry == NULL) {
235 return EFI_OUT_OF_RESOURCES;
236 }
237
238 *BottomEntry = CoreAllocateZeroBootServicesPool (sizeof (EFI_GCD_MAP_ENTRY));
239 if (*BottomEntry == NULL) {
240 CoreFreePool (*TopEntry);
241 return EFI_OUT_OF_RESOURCES;
242 }
243
244 return EFI_SUCCESS;
245 }
246
247
248 /**
249 Internal function. Inserts a new descriptor into a sorted list
250
251 @param Link The linked list to insert the range BaseAddress
252 and Length into
253 @param Entry A pointer to the entry that is inserted
254 @param BaseAddress The base address of the new range
255 @param Length The length of the new range in bytes
256 @param TopEntry Top pad entry to insert if needed.
257 @param BottomEntry Bottom pad entry to insert if needed.
258
259 @retval EFI_SUCCESS The new range was inserted into the linked list
260
261 **/
262 EFI_STATUS
263 CoreInsertGcdMapEntry (
264 IN LIST_ENTRY *Link,
265 IN EFI_GCD_MAP_ENTRY *Entry,
266 IN EFI_PHYSICAL_ADDRESS BaseAddress,
267 IN UINT64 Length,
268 IN EFI_GCD_MAP_ENTRY *TopEntry,
269 IN EFI_GCD_MAP_ENTRY *BottomEntry
270 )
271 {
272 ASSERT (Length != 0);
273 ASSERT (TopEntry->Signature == 0);
274 ASSERT (BottomEntry->Signature == 0);
275
276 if (BaseAddress > Entry->BaseAddress) {
277 CopyMem (BottomEntry, Entry, sizeof (EFI_GCD_MAP_ENTRY));
278 Entry->BaseAddress = BaseAddress;
279 BottomEntry->EndAddress = BaseAddress - 1;
280 InsertTailList (Link, &BottomEntry->Link);
281 }
282
283 if ((BaseAddress + Length - 1) < Entry->EndAddress) {
284 CopyMem (TopEntry, Entry, sizeof (EFI_GCD_MAP_ENTRY));
285 TopEntry->BaseAddress = BaseAddress + Length;
286 Entry->EndAddress = BaseAddress + Length - 1;
287 InsertHeadList (Link, &TopEntry->Link);
288 }
289
290 return EFI_SUCCESS;
291 }
292
293
294 /**
295 Merge the Gcd region specified by Link and its adjacent entry
296
297 @param Link Specify the entry to be merged (with its
298 adjacent entry).
299 @param Forward Direction (forward or backward).
300 @param Map Boundary.
301
302 @retval EFI_SUCCESS Successfully returned.
303 @retval EFI_UNSUPPORTED These adjacent regions could not merge.
304
305 **/
306 EFI_STATUS
307 CoreMergeGcdMapEntry (
308 IN LIST_ENTRY *Link,
309 IN BOOLEAN Forward,
310 IN LIST_ENTRY *Map
311 )
312 {
313 LIST_ENTRY *AdjacentLink;
314 EFI_GCD_MAP_ENTRY *Entry;
315 EFI_GCD_MAP_ENTRY *AdjacentEntry;
316
317 //
318 // Get adjacent entry
319 //
320 if (Forward) {
321 AdjacentLink = Link->ForwardLink;
322 } else {
323 AdjacentLink = Link->BackLink;
324 }
325
326 //
327 // If AdjacentLink is the head of the list, then no merge can be performed
328 //
329 if (AdjacentLink == Map) {
330 return EFI_SUCCESS;
331 }
332
333 Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
334 AdjacentEntry = CR (AdjacentLink, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
335
336 if (Entry->Capabilities != AdjacentEntry->Capabilities) {
337 return EFI_UNSUPPORTED;
338 }
339 if (Entry->Attributes != AdjacentEntry->Attributes) {
340 return EFI_UNSUPPORTED;
341 }
342 if (Entry->GcdMemoryType != AdjacentEntry->GcdMemoryType) {
343 return EFI_UNSUPPORTED;
344 }
345 if (Entry->GcdIoType != AdjacentEntry->GcdIoType) {
346 return EFI_UNSUPPORTED;
347 }
348 if (Entry->ImageHandle != AdjacentEntry->ImageHandle) {
349 return EFI_UNSUPPORTED;
350 }
351 if (Entry->DeviceHandle != AdjacentEntry->DeviceHandle) {
352 return EFI_UNSUPPORTED;
353 }
354
355 if (Forward) {
356 Entry->EndAddress = AdjacentEntry->EndAddress;
357 } else {
358 Entry->BaseAddress = AdjacentEntry->BaseAddress;
359 }
360 RemoveEntryList (AdjacentLink);
361 CoreFreePool (AdjacentEntry);
362
363 return EFI_SUCCESS;
364 }
365
366
367 /**
368 Merge adjacent entries on total chain.
369
370 @param TopEntry Top entry of GCD map.
371 @param BottomEntry Bottom entry of GCD map.
372 @param StartLink Start link of the list for this loop.
373 @param EndLink End link of the list for this loop.
374 @param Map Boundary.
375
376 @retval EFI_SUCCESS GCD map successfully cleaned up.
377
378 **/
379 EFI_STATUS
380 CoreCleanupGcdMapEntry (
381 IN EFI_GCD_MAP_ENTRY *TopEntry,
382 IN EFI_GCD_MAP_ENTRY *BottomEntry,
383 IN LIST_ENTRY *StartLink,
384 IN LIST_ENTRY *EndLink,
385 IN LIST_ENTRY *Map
386 )
387 {
388 LIST_ENTRY *Link;
389
390 if (TopEntry->Signature == 0) {
391 CoreFreePool (TopEntry);
392 }
393 if (BottomEntry->Signature == 0) {
394 CoreFreePool (BottomEntry);
395 }
396
397 Link = StartLink;
398 while (Link != EndLink->ForwardLink) {
399 CoreMergeGcdMapEntry (Link, FALSE, Map);
400 Link = Link->ForwardLink;
401 }
402 CoreMergeGcdMapEntry (EndLink, TRUE, Map);
403
404 return EFI_SUCCESS;
405 }
406
407
408 /**
409 Search a segment of memory space in GCD map. The result is a range of GCD entry list.
410
411 @param BaseAddress The start address of the segment.
412 @param Length The length of the segment.
413 @param StartLink The first GCD entry involves this segment of
414 memory space.
415 @param EndLink The first GCD entry involves this segment of
416 memory space.
417 @param Map Points to the start entry to search.
418
419 @retval EFI_SUCCESS Successfully found the entry.
420 @retval EFI_NOT_FOUND Not found.
421
422 **/
423 EFI_STATUS
424 CoreSearchGcdMapEntry (
425 IN EFI_PHYSICAL_ADDRESS BaseAddress,
426 IN UINT64 Length,
427 OUT LIST_ENTRY **StartLink,
428 OUT LIST_ENTRY **EndLink,
429 IN LIST_ENTRY *Map
430 )
431 {
432 LIST_ENTRY *Link;
433 EFI_GCD_MAP_ENTRY *Entry;
434
435 ASSERT (Length != 0);
436
437 *StartLink = NULL;
438 *EndLink = NULL;
439
440 Link = Map->ForwardLink;
441 while (Link != Map) {
442 Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
443 if (BaseAddress >= Entry->BaseAddress && BaseAddress <= Entry->EndAddress) {
444 *StartLink = Link;
445 }
446 if (*StartLink != NULL) {
447 if ((BaseAddress + Length - 1) >= Entry->BaseAddress &&
448 (BaseAddress + Length - 1) <= Entry->EndAddress ) {
449 *EndLink = Link;
450 return EFI_SUCCESS;
451 }
452 }
453 Link = Link->ForwardLink;
454 }
455 return EFI_NOT_FOUND;
456 }
457
458
459 /**
460 Count the amount of GCD map entries.
461
462 @param Map Points to the start entry to do the count loop.
463
464 @return The count.
465
466 **/
467 UINTN
468 CoreCountGcdMapEntry (
469 IN LIST_ENTRY *Map
470 )
471 {
472 UINTN Count;
473 LIST_ENTRY *Link;
474
475 Count = 0;
476 Link = Map->ForwardLink;
477 while (Link != Map) {
478 Count++;
479 Link = Link->ForwardLink;
480 }
481 return Count;
482 }
483
484
485
486 /**
487 Return the memory attribute specified by Attributes
488
489 @param Attributes A num with some attribute bits on.
490
491 @return The enum value of memory attribute.
492
493 **/
494 UINT64
495 ConverToCpuArchAttributes (
496 UINT64 Attributes
497 )
498 {
499 if ( (Attributes & EFI_MEMORY_UC) == EFI_MEMORY_UC) {
500 return EFI_MEMORY_UC;
501 }
502
503 if ( (Attributes & EFI_MEMORY_WC ) == EFI_MEMORY_WC) {
504 return EFI_MEMORY_WC;
505 }
506
507 if ( (Attributes & EFI_MEMORY_WT ) == EFI_MEMORY_WT) {
508 return EFI_MEMORY_WT;
509 }
510
511 if ( (Attributes & EFI_MEMORY_WB) == EFI_MEMORY_WB) {
512 return EFI_MEMORY_WB;
513 }
514
515 if ( (Attributes & EFI_MEMORY_WP) == EFI_MEMORY_WP) {
516 return EFI_MEMORY_WP;
517 }
518
519 return INVALID_CPU_ARCH_ATTRIBUTES;
520
521 }
522
523
524 /**
525 Do operation on a segment of memory space specified (add, free, remove, change attribute ...).
526
527 @param Operation The type of the operation
528 @param GcdMemoryType Additional information for the operation
529 @param GcdIoType Additional information for the operation
530 @param BaseAddress Start address of the segment
531 @param Length length of the segment
532 @param Capabilities The alterable attributes of a newly added entry
533 @param Attributes The attributes needs to be set
534
535 @retval EFI_INVALID_PARAMETER Length is 0 or address (length) not aligned when
536 setting attribute.
537 @retval EFI_SUCCESS Action successfully done.
538 @retval EFI_UNSUPPORTED Could not find the proper descriptor on this
539 segment or set an upsupported attribute.
540 @retval EFI_ACCESS_DENIED Operate on an space non-exist or is used for an
541 image.
542 @retval EFI_NOT_FOUND Free a non-using space or remove a non-exist
543 space, and so on.
544 @retval EFI_OUT_OF_RESOURCES No buffer could be allocated.
545
546 **/
547 EFI_STATUS
548 CoreConvertSpace (
549 IN UINTN Operation,
550 IN EFI_GCD_MEMORY_TYPE GcdMemoryType,
551 IN EFI_GCD_IO_TYPE GcdIoType,
552 IN EFI_PHYSICAL_ADDRESS BaseAddress,
553 IN UINT64 Length,
554 IN UINT64 Capabilities,
555 IN UINT64 Attributes
556 )
557 {
558 EFI_STATUS Status;
559 LIST_ENTRY *Map;
560 LIST_ENTRY *Link;
561 EFI_GCD_MAP_ENTRY *Entry;
562 EFI_GCD_MAP_ENTRY *TopEntry;
563 EFI_GCD_MAP_ENTRY *BottomEntry;
564 LIST_ENTRY *StartLink;
565 LIST_ENTRY *EndLink;
566
567 EFI_CPU_ARCH_PROTOCOL *CpuArch;
568 UINT64 CpuArchAttributes;
569
570 if (Length == 0) {
571 return EFI_INVALID_PARAMETER;
572 }
573
574 Map = NULL;
575 if ((Operation & GCD_MEMORY_SPACE_OPERATION) != 0) {
576 CoreAcquireGcdMemoryLock ();
577 Map = &mGcdMemorySpaceMap;
578 }
579 if ((Operation & GCD_IO_SPACE_OPERATION) != 0) {
580 CoreAcquireGcdIoLock ();
581 Map = &mGcdIoSpaceMap;
582 }
583
584 //
585 // Search for the list of descriptors that cover the range BaseAddress to BaseAddress+Length
586 //
587 Status = CoreSearchGcdMapEntry (BaseAddress, Length, &StartLink, &EndLink, Map);
588 if (EFI_ERROR (Status)) {
589 Status = EFI_UNSUPPORTED;
590
591 goto Done;
592 }
593
594 //
595 // Verify that the list of descriptors are unallocated non-existent memory.
596 //
597 Link = StartLink;
598 while (Link != EndLink->ForwardLink) {
599 Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
600 switch (Operation) {
601 //
602 // Add operations
603 //
604 case GCD_ADD_MEMORY_OPERATION:
605 if (Entry->GcdMemoryType != EfiGcdMemoryTypeNonExistent ||
606 Entry->ImageHandle != NULL ) {
607 Status = EFI_ACCESS_DENIED;
608 goto Done;
609 }
610 break;
611 case GCD_ADD_IO_OPERATION:
612 if (Entry->GcdIoType != EfiGcdIoTypeNonExistent ||
613 Entry->ImageHandle != NULL ) {
614 Status = EFI_ACCESS_DENIED;
615 goto Done;
616 }
617 break;
618 //
619 // Free operations
620 //
621 case GCD_FREE_MEMORY_OPERATION:
622 case GCD_FREE_IO_OPERATION:
623 if (Entry->ImageHandle == NULL) {
624 Status = EFI_NOT_FOUND;
625 goto Done;
626 }
627 break;
628 //
629 // Remove operations
630 //
631 case GCD_REMOVE_MEMORY_OPERATION:
632 if (Entry->GcdMemoryType == EfiGcdMemoryTypeNonExistent) {
633 Status = EFI_NOT_FOUND;
634 goto Done;
635 }
636 if (Entry->ImageHandle != NULL) {
637 Status = EFI_ACCESS_DENIED;
638 goto Done;
639 }
640 break;
641 case GCD_REMOVE_IO_OPERATION:
642 if (Entry->GcdIoType == EfiGcdIoTypeNonExistent) {
643 Status = EFI_NOT_FOUND;
644 goto Done;
645 }
646 if (Entry->ImageHandle != NULL) {
647 Status = EFI_ACCESS_DENIED;
648 goto Done;
649 }
650 break;
651 //
652 // Set attribute operations
653 //
654 case GCD_SET_ATTRIBUTES_MEMORY_OPERATION:
655 if ((Attributes & EFI_MEMORY_RUNTIME) != 0) {
656 if ((BaseAddress & EFI_PAGE_MASK) != 0 || (Length & EFI_PAGE_MASK) != 0) {
657 Status = EFI_INVALID_PARAMETER;
658
659 goto Done;
660 }
661 }
662 if ((Entry->Capabilities & Attributes) != Attributes) {
663 Status = EFI_UNSUPPORTED;
664 goto Done;
665 }
666 break;
667 }
668 Link = Link->ForwardLink;
669 }
670
671 //
672 // Allocate work space to perform this operation
673 //
674 Status = CoreAllocateGcdMapEntry (&TopEntry, &BottomEntry);
675 if (EFI_ERROR (Status)) {
676 Status = EFI_OUT_OF_RESOURCES;
677 goto Done;
678 }
679
680 //
681 //
682 //
683 if (Operation == GCD_SET_ATTRIBUTES_MEMORY_OPERATION) {
684 //
685 // Call CPU Arch Protocol to attempt to set attributes on the range
686 //
687 CpuArchAttributes = ConverToCpuArchAttributes (Attributes);
688 if ( CpuArchAttributes != INVALID_CPU_ARCH_ATTRIBUTES ) {
689 Status = CoreLocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&CpuArch);
690 if (EFI_ERROR (Status)) {
691 Status = EFI_ACCESS_DENIED;
692 goto Done;
693 }
694
695 Status = CpuArch->SetMemoryAttributes (
696 CpuArch,
697 BaseAddress,
698 Length,
699 CpuArchAttributes
700 );
701 if (EFI_ERROR (Status)) {
702 goto Done;
703 }
704 }
705
706 }
707
708 //
709 // Convert/Insert the list of descriptors from StartLink to EndLink
710 //
711 Link = StartLink;
712 while (Link != EndLink->ForwardLink) {
713 Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
714 CoreInsertGcdMapEntry (Link, Entry, BaseAddress, Length, TopEntry, BottomEntry);
715 switch (Operation) {
716 //
717 // Add operations
718 //
719 case GCD_ADD_MEMORY_OPERATION:
720 Entry->GcdMemoryType = GcdMemoryType;
721 if (GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) {
722 Entry->Capabilities = Capabilities | EFI_MEMORY_RUNTIME | EFI_MEMORY_PORT_IO;
723 } else {
724 Entry->Capabilities = Capabilities | EFI_MEMORY_RUNTIME;
725 }
726 break;
727 case GCD_ADD_IO_OPERATION:
728 Entry->GcdIoType = GcdIoType;
729 break;
730 //
731 // Free operations
732 //
733 case GCD_FREE_MEMORY_OPERATION:
734 case GCD_FREE_IO_OPERATION:
735 Entry->ImageHandle = NULL;
736 Entry->DeviceHandle = NULL;
737 break;
738 //
739 // Remove operations
740 //
741 case GCD_REMOVE_MEMORY_OPERATION:
742 Entry->GcdMemoryType = EfiGcdMemoryTypeNonExistent;
743 Entry->Capabilities = 0;
744 break;
745 case GCD_REMOVE_IO_OPERATION:
746 Entry->GcdIoType = EfiGcdIoTypeNonExistent;
747 break;
748 //
749 // Set attribute operations
750 //
751 case GCD_SET_ATTRIBUTES_MEMORY_OPERATION:
752 Entry->Attributes = Attributes;
753 break;
754 }
755 Link = Link->ForwardLink;
756 }
757
758 //
759 // Cleanup
760 //
761 Status = CoreCleanupGcdMapEntry (TopEntry, BottomEntry, StartLink, EndLink, Map);
762
763 Done:
764 if ((Operation & GCD_MEMORY_SPACE_OPERATION) != 0) {
765 CoreReleaseGcdMemoryLock ();
766 }
767 if ((Operation & GCD_IO_SPACE_OPERATION) != 0) {
768 CoreReleaseGcdIoLock ();
769 }
770
771 return Status;
772 }
773
774
775 /**
776 Check whether an entry could be used to allocate space.
777
778 @param Operation Allocate memory or IO
779 @param Entry The entry to be tested
780 @param GcdMemoryType The desired memory type
781 @param GcdIoType The desired IO type
782
783 @retval EFI_NOT_FOUND The memory type does not match or there's an
784 image handle on the entry.
785 @retval EFI_UNSUPPORTED The operation unsupported.
786 @retval EFI_SUCCESS It's ok for this entry to be used to allocate
787 space.
788
789 **/
790 EFI_STATUS
791 CoreAllocateSpaceCheckEntry (
792 IN UINTN Operation,
793 IN EFI_GCD_MAP_ENTRY *Entry,
794 IN EFI_GCD_MEMORY_TYPE GcdMemoryType,
795 IN EFI_GCD_IO_TYPE GcdIoType
796 )
797 {
798 if (Entry->ImageHandle != NULL) {
799 return EFI_NOT_FOUND;
800 }
801 switch (Operation) {
802 case GCD_ALLOCATE_MEMORY_OPERATION:
803 if (Entry->GcdMemoryType != GcdMemoryType) {
804 return EFI_NOT_FOUND;
805 }
806 break;
807 case GCD_ALLOCATE_IO_OPERATION:
808 if (Entry->GcdIoType != GcdIoType) {
809 return EFI_NOT_FOUND;
810 }
811 break;
812 default:
813 return EFI_UNSUPPORTED;
814 }
815 return EFI_SUCCESS;
816 }
817
818
819 /**
820 Allocate space on specified address and length.
821
822 @param Operation The type of operation (memory or IO)
823 @param GcdAllocateType The type of allocate operation
824 @param GcdMemoryType The desired memory type
825 @param GcdIoType The desired IO type
826 @param Alignment Align with 2^Alignment
827 @param Length Length to allocate
828 @param BaseAddress Base address to allocate
829 @param ImageHandle The image handle consume the allocated space.
830 @param DeviceHandle The device handle consume the allocated space.
831
832 @retval EFI_INVALID_PARAMETER Invalid parameter.
833 @retval EFI_NOT_FOUND No descriptor for the desired space exists.
834 @retval EFI_SUCCESS Space successfully allocated.
835
836 **/
837 EFI_STATUS
838 CoreAllocateSpace (
839 IN UINTN Operation,
840 IN EFI_GCD_ALLOCATE_TYPE GcdAllocateType,
841 IN EFI_GCD_MEMORY_TYPE GcdMemoryType,
842 IN EFI_GCD_IO_TYPE GcdIoType,
843 IN UINTN Alignment,
844 IN UINT64 Length,
845 IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress,
846 IN EFI_HANDLE ImageHandle,
847 IN EFI_HANDLE DeviceHandle OPTIONAL
848 )
849 {
850 EFI_STATUS Status;
851 EFI_PHYSICAL_ADDRESS AlignmentMask;
852 EFI_PHYSICAL_ADDRESS MaxAddress;
853 LIST_ENTRY *Map;
854 LIST_ENTRY *Link;
855 LIST_ENTRY *SubLink;
856 EFI_GCD_MAP_ENTRY *Entry;
857 EFI_GCD_MAP_ENTRY *TopEntry;
858 EFI_GCD_MAP_ENTRY *BottomEntry;
859 LIST_ENTRY *StartLink;
860 LIST_ENTRY *EndLink;
861 BOOLEAN Found;
862
863 //
864 // Make sure parameters are valid
865 //
866 if (GcdAllocateType < 0 || GcdAllocateType >= EfiGcdMaxAllocateType) {
867 return EFI_INVALID_PARAMETER;
868 }
869 if (GcdMemoryType < 0 || GcdMemoryType >= EfiGcdMemoryTypeMaximum) {
870 return EFI_INVALID_PARAMETER;
871 }
872 if (GcdIoType < 0 || GcdIoType >= EfiGcdIoTypeMaximum) {
873 return EFI_INVALID_PARAMETER;
874 }
875 if (BaseAddress == NULL) {
876 return EFI_INVALID_PARAMETER;
877 }
878 if (ImageHandle == NULL) {
879 return EFI_INVALID_PARAMETER;
880 }
881 if (Alignment >= 64) {
882 return EFI_NOT_FOUND;
883 }
884 if (Length == 0) {
885 return EFI_INVALID_PARAMETER;
886 }
887
888 Map = NULL;
889 if ((Operation & GCD_MEMORY_SPACE_OPERATION) != 0) {
890 CoreAcquireGcdMemoryLock ();
891 Map = &mGcdMemorySpaceMap;
892 }
893 if ((Operation & GCD_IO_SPACE_OPERATION) != 0) {
894 CoreAcquireGcdIoLock ();
895 Map = &mGcdIoSpaceMap;
896 }
897
898 Found = FALSE;
899 StartLink = NULL;
900 EndLink = NULL;
901 //
902 // Compute alignment bit mask
903 //
904 AlignmentMask = LShiftU64 (1, Alignment) - 1;
905
906 if (GcdAllocateType == EfiGcdAllocateAddress) {
907 //
908 // Verify that the BaseAddress passed in is aligned correctly
909 //
910 if ((*BaseAddress & AlignmentMask) != 0) {
911 Status = EFI_NOT_FOUND;
912 goto Done;
913 }
914
915 //
916 // Search for the list of descriptors that cover the range BaseAddress to BaseAddress+Length
917 //
918 Status = CoreSearchGcdMapEntry (*BaseAddress, Length, &StartLink, &EndLink, Map);
919 if (EFI_ERROR (Status)) {
920 Status = EFI_NOT_FOUND;
921 goto Done;
922 }
923
924 //
925 // Verify that the list of descriptors are unallocated memory matching GcdMemoryType.
926 //
927 Link = StartLink;
928 while (Link != EndLink->ForwardLink) {
929 Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
930 Link = Link->ForwardLink;
931 Status = CoreAllocateSpaceCheckEntry (Operation, Entry, GcdMemoryType, GcdIoType);
932 if (EFI_ERROR (Status)) {
933 goto Done;
934 }
935 }
936 Found = TRUE;
937 } else {
938
939 Entry = CR (Map->BackLink, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
940
941 //
942 // Compute the maximum address to use in the search algorithm
943 //
944 if (GcdAllocateType == EfiGcdAllocateMaxAddressSearchBottomUp ||
945 GcdAllocateType == EfiGcdAllocateMaxAddressSearchTopDown ) {
946 MaxAddress = *BaseAddress;
947 } else {
948 MaxAddress = Entry->EndAddress;
949 }
950
951 //
952 // Verify that the list of descriptors are unallocated memory matching GcdMemoryType.
953 //
954 if (GcdAllocateType == EfiGcdAllocateMaxAddressSearchTopDown ||
955 GcdAllocateType == EfiGcdAllocateAnySearchTopDown ) {
956 Link = Map->BackLink;
957 } else {
958 Link = Map->ForwardLink;
959 }
960 while (Link != Map) {
961 Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
962
963 if (GcdAllocateType == EfiGcdAllocateMaxAddressSearchTopDown ||
964 GcdAllocateType == EfiGcdAllocateAnySearchTopDown ) {
965 Link = Link->BackLink;
966 } else {
967 Link = Link->ForwardLink;
968 }
969
970 Status = CoreAllocateSpaceCheckEntry (Operation, Entry, GcdMemoryType, GcdIoType);
971 if (EFI_ERROR (Status)) {
972 continue;
973 }
974
975 if (GcdAllocateType == EfiGcdAllocateMaxAddressSearchTopDown ||
976 GcdAllocateType == EfiGcdAllocateAnySearchTopDown ) {
977 if ((Entry->BaseAddress + Length) > MaxAddress) {
978 continue;
979 }
980 if (Length > (Entry->EndAddress + 1)) {
981 Status = EFI_NOT_FOUND;
982 goto Done;
983 }
984 if (Entry->EndAddress > MaxAddress) {
985 *BaseAddress = MaxAddress;
986 } else {
987 *BaseAddress = Entry->EndAddress;
988 }
989 *BaseAddress = (*BaseAddress + 1 - Length) & (~AlignmentMask);
990 } else {
991 *BaseAddress = (Entry->BaseAddress + AlignmentMask) & (~AlignmentMask);
992 if ((*BaseAddress + Length - 1) > MaxAddress) {
993 Status = EFI_NOT_FOUND;
994 goto Done;
995 }
996 }
997
998 //
999 // Search for the list of descriptors that cover the range BaseAddress to BaseAddress+Length
1000 //
1001 Status = CoreSearchGcdMapEntry (*BaseAddress, Length, &StartLink, &EndLink, Map);
1002 if (EFI_ERROR (Status)) {
1003 Status = EFI_NOT_FOUND;
1004 goto Done;
1005 }
1006
1007 Link = StartLink;
1008 //
1009 // Verify that the list of descriptors are unallocated memory matching GcdMemoryType.
1010 //
1011 Found = TRUE;
1012 SubLink = StartLink;
1013 while (SubLink != EndLink->ForwardLink) {
1014 Entry = CR (SubLink, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
1015 Status = CoreAllocateSpaceCheckEntry (Operation, Entry, GcdMemoryType, GcdIoType);
1016 if (EFI_ERROR (Status)) {
1017 Link = SubLink;
1018 Found = FALSE;
1019 break;
1020 }
1021 SubLink = SubLink->ForwardLink;
1022 }
1023 if (Found) {
1024 break;
1025 }
1026 }
1027 }
1028 if (!Found) {
1029 Status = EFI_NOT_FOUND;
1030 goto Done;
1031 }
1032
1033 //
1034 // Allocate work space to perform this operation
1035 //
1036 Status = CoreAllocateGcdMapEntry (&TopEntry, &BottomEntry);
1037 if (EFI_ERROR (Status)) {
1038 Status = EFI_OUT_OF_RESOURCES;
1039 goto Done;
1040 }
1041
1042 //
1043 // Convert/Insert the list of descriptors from StartLink to EndLink
1044 //
1045 Link = StartLink;
1046 while (Link != EndLink->ForwardLink) {
1047 Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
1048 CoreInsertGcdMapEntry (Link, Entry, *BaseAddress, Length, TopEntry, BottomEntry);
1049 Entry->ImageHandle = ImageHandle;
1050 Entry->DeviceHandle = DeviceHandle;
1051 Link = Link->ForwardLink;
1052 }
1053
1054 //
1055 // Cleanup
1056 //
1057 Status = CoreCleanupGcdMapEntry (TopEntry, BottomEntry, StartLink, EndLink, Map);
1058
1059 Done:
1060 if ((Operation & GCD_MEMORY_SPACE_OPERATION) != 0) {
1061 CoreReleaseGcdMemoryLock ();
1062 }
1063 if ((Operation & GCD_IO_SPACE_OPERATION) !=0) {
1064 CoreReleaseGcdIoLock ();
1065 }
1066
1067 return Status;
1068 }
1069
1070
1071 /**
1072 Add a segment of memory to GCD map.
1073
1074 @param GcdMemoryType Memory type of the segment.
1075 @param BaseAddress Base address of the segment.
1076 @param Length Length of the segment.
1077 @param Capabilities alterable attributes of the segment.
1078
1079 @retval EFI_INVALID_PARAMETER Invalid parameters.
1080 @retval EFI_SUCCESS Successfully add a segment of memory space.
1081
1082 **/
1083 EFI_STATUS
1084 CoreInternalAddMemorySpace (
1085 IN EFI_GCD_MEMORY_TYPE GcdMemoryType,
1086 IN EFI_PHYSICAL_ADDRESS BaseAddress,
1087 IN UINT64 Length,
1088 IN UINT64 Capabilities
1089 )
1090 {
1091 //
1092 // Make sure parameters are valid
1093 //
1094 if (GcdMemoryType <= EfiGcdMemoryTypeNonExistent || GcdMemoryType >= EfiGcdMemoryTypeMaximum) {
1095 return EFI_INVALID_PARAMETER;
1096 }
1097
1098 return CoreConvertSpace (GCD_ADD_MEMORY_OPERATION, GcdMemoryType, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, Capabilities, 0);
1099 }
1100
1101 //
1102 // GCD Core Services
1103 //
1104
1105 /**
1106 Allocates nonexistent memory, reserved memory, system memory, or memorymapped
1107 I/O resources from the global coherency domain of the processor.
1108
1109 @param GcdAllocateType The type of allocate operation
1110 @param GcdMemoryType The desired memory type
1111 @param Alignment Align with 2^Alignment
1112 @param Length Length to allocate
1113 @param BaseAddress Base address to allocate
1114 @param ImageHandle The image handle consume the allocated space.
1115 @param DeviceHandle The device handle consume the allocated space.
1116
1117 @retval EFI_INVALID_PARAMETER Invalid parameter.
1118 @retval EFI_NOT_FOUND No descriptor contains the desired space.
1119 @retval EFI_SUCCESS Memory space successfully allocated.
1120
1121 **/
1122 EFI_STATUS
1123 CoreAllocateMemorySpace (
1124 IN EFI_GCD_ALLOCATE_TYPE GcdAllocateType,
1125 IN EFI_GCD_MEMORY_TYPE GcdMemoryType,
1126 IN UINTN Alignment,
1127 IN UINT64 Length,
1128 IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress,
1129 IN EFI_HANDLE ImageHandle,
1130 IN EFI_HANDLE DeviceHandle OPTIONAL
1131 )
1132 {
1133 return CoreAllocateSpace (
1134 GCD_ALLOCATE_MEMORY_OPERATION,
1135 GcdAllocateType,
1136 GcdMemoryType,
1137 (EFI_GCD_IO_TYPE) 0,
1138 Alignment,
1139 Length,
1140 BaseAddress,
1141 ImageHandle,
1142 DeviceHandle
1143 );
1144 }
1145
1146
1147 /**
1148 Adds reserved memory, system memory, or memory-mapped I/O resources to the
1149 global coherency domain of the processor.
1150
1151 @param GcdMemoryType Memory type of the memory space.
1152 @param BaseAddress Base address of the memory space.
1153 @param Length Length of the memory space.
1154 @param Capabilities alterable attributes of the memory space.
1155
1156 @retval EFI_SUCCESS Merged this memory space into GCD map.
1157
1158 **/
1159 EFI_STATUS
1160 CoreAddMemorySpace (
1161 IN EFI_GCD_MEMORY_TYPE GcdMemoryType,
1162 IN EFI_PHYSICAL_ADDRESS BaseAddress,
1163 IN UINT64 Length,
1164 IN UINT64 Capabilities
1165 )
1166 {
1167 EFI_STATUS Status;
1168 EFI_PHYSICAL_ADDRESS PageBaseAddress;
1169 UINT64 PageLength;
1170
1171 Status = CoreInternalAddMemorySpace (GcdMemoryType, BaseAddress, Length, Capabilities);
1172
1173 if (!EFI_ERROR (Status) && GcdMemoryType == EfiGcdMemoryTypeSystemMemory) {
1174
1175 PageBaseAddress = PageAlignLength (BaseAddress);
1176 PageLength = PageAlignLength (BaseAddress + Length - PageBaseAddress);
1177
1178 Status = CoreAllocateMemorySpace (
1179 EfiGcdAllocateAddress,
1180 GcdMemoryType,
1181 EFI_PAGE_SHIFT,
1182 PageLength,
1183 &PageBaseAddress,
1184 gDxeCoreImageHandle,
1185 NULL
1186 );
1187
1188 if (!EFI_ERROR (Status)) {
1189 CoreAddMemoryDescriptor (
1190 EfiConventionalMemory,
1191 PageBaseAddress,
1192 RShiftU64 (PageLength, EFI_PAGE_SHIFT),
1193 Capabilities
1194 );
1195 } else {
1196 for (; PageLength != 0; PageLength -= EFI_PAGE_SIZE, PageBaseAddress += EFI_PAGE_SIZE) {
1197 Status = CoreAllocateMemorySpace (
1198 EfiGcdAllocateAddress,
1199 GcdMemoryType,
1200 EFI_PAGE_SHIFT,
1201 EFI_PAGE_SIZE,
1202 &PageBaseAddress,
1203 gDxeCoreImageHandle,
1204 NULL
1205 );
1206
1207 if (!EFI_ERROR (Status)) {
1208 CoreAddMemoryDescriptor (
1209 EfiConventionalMemory,
1210 PageBaseAddress,
1211 1,
1212 Capabilities
1213 );
1214 }
1215 }
1216 }
1217 }
1218 return Status;
1219 }
1220
1221
1222 /**
1223 Frees nonexistent memory, reserved memory, system memory, or memory-mapped
1224 I/O resources from the global coherency domain of the processor.
1225
1226 @param BaseAddress Base address of the memory space.
1227 @param Length Length of the memory space.
1228
1229 @retval EFI_SUCCESS Space successfully freed.
1230
1231 **/
1232 EFI_STATUS
1233 CoreFreeMemorySpace (
1234 IN EFI_PHYSICAL_ADDRESS BaseAddress,
1235 IN UINT64 Length
1236 )
1237 {
1238 return CoreConvertSpace (GCD_FREE_MEMORY_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, 0, 0);
1239 }
1240
1241
1242 /**
1243 Removes reserved memory, system memory, or memory-mapped I/O resources from
1244 the global coherency domain of the processor.
1245
1246 @param BaseAddress Base address of the memory space.
1247 @param Length Length of the memory space.
1248
1249 @retval EFI_SUCCESS Successfully remove a segment of memory space.
1250
1251 **/
1252 EFI_STATUS
1253 CoreRemoveMemorySpace (
1254 IN EFI_PHYSICAL_ADDRESS BaseAddress,
1255 IN UINT64 Length
1256 )
1257 {
1258 return CoreConvertSpace (GCD_REMOVE_MEMORY_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, 0, 0);
1259 }
1260
1261
1262 /**
1263 Build a memory descriptor according to an entry.
1264
1265 @param Descriptor The descriptor to be built
1266 @param Entry According to this entry
1267
1268 **/
1269 VOID
1270 BuildMemoryDescriptor (
1271 IN OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Descriptor,
1272 IN EFI_GCD_MAP_ENTRY *Entry
1273 )
1274 {
1275 Descriptor->BaseAddress = Entry->BaseAddress;
1276 Descriptor->Length = Entry->EndAddress - Entry->BaseAddress + 1;
1277 Descriptor->Capabilities = Entry->Capabilities;
1278 Descriptor->Attributes = Entry->Attributes;
1279 Descriptor->GcdMemoryType = Entry->GcdMemoryType;
1280 Descriptor->ImageHandle = Entry->ImageHandle;
1281 Descriptor->DeviceHandle = Entry->DeviceHandle;
1282 }
1283
1284
1285 /**
1286 Retrieves the descriptor for a memory region containing a specified address.
1287
1288 @param BaseAddress Specified start address
1289 @param Descriptor Specified length
1290
1291 @retval EFI_INVALID_PARAMETER Invalid parameter
1292 @retval EFI_SUCCESS Successfully get memory space descriptor.
1293
1294 **/
1295 EFI_STATUS
1296 CoreGetMemorySpaceDescriptor (
1297 IN EFI_PHYSICAL_ADDRESS BaseAddress,
1298 OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Descriptor
1299 )
1300 {
1301 EFI_STATUS Status;
1302 LIST_ENTRY *StartLink;
1303 LIST_ENTRY *EndLink;
1304 EFI_GCD_MAP_ENTRY *Entry;
1305
1306 //
1307 // Make sure parameters are valid
1308 //
1309 if (Descriptor == NULL) {
1310 return EFI_INVALID_PARAMETER;
1311 }
1312
1313 CoreAcquireGcdMemoryLock ();
1314
1315 //
1316 // Search for the list of descriptors that contain BaseAddress
1317 //
1318 Status = CoreSearchGcdMapEntry (BaseAddress, 1, &StartLink, &EndLink, &mGcdMemorySpaceMap);
1319 if (EFI_ERROR (Status)) {
1320 Status = EFI_NOT_FOUND;
1321 } else {
1322 //
1323 // Copy the contents of the found descriptor into Descriptor
1324 //
1325 Entry = CR (StartLink, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
1326 BuildMemoryDescriptor (Descriptor, Entry);
1327 }
1328
1329 CoreReleaseGcdMemoryLock ();
1330
1331 return Status;
1332 }
1333
1334
1335 /**
1336 Modifies the attributes for a memory region in the global coherency domain of the
1337 processor.
1338
1339 @param BaseAddress Specified start address
1340 @param Length Specified length
1341 @param Attributes Specified attributes
1342
1343 @retval EFI_SUCCESS Successfully set attribute of a segment of
1344 memory space.
1345
1346 **/
1347 EFI_STATUS
1348 CoreSetMemorySpaceAttributes (
1349 IN EFI_PHYSICAL_ADDRESS BaseAddress,
1350 IN UINT64 Length,
1351 IN UINT64 Attributes
1352 )
1353 {
1354 return CoreConvertSpace (GCD_SET_ATTRIBUTES_MEMORY_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, 0, Attributes);
1355 }
1356
1357
1358 /**
1359 Returns a map of the memory resources in the global coherency domain of the
1360 processor.
1361
1362 @param NumberOfDescriptors Number of descriptors.
1363 @param MemorySpaceMap Descriptor array
1364
1365 @retval EFI_INVALID_PARAMETER Invalid parameter
1366 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1367 @retval EFI_SUCCESS Successfully get memory space map.
1368
1369 **/
1370 EFI_STATUS
1371 CoreGetMemorySpaceMap (
1372 OUT UINTN *NumberOfDescriptors,
1373 OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR **MemorySpaceMap
1374 )
1375 {
1376 EFI_STATUS Status;
1377 LIST_ENTRY *Link;
1378 EFI_GCD_MAP_ENTRY *Entry;
1379 EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Descriptor;
1380
1381 //
1382 // Make sure parameters are valid
1383 //
1384 if (NumberOfDescriptors == NULL) {
1385 return EFI_INVALID_PARAMETER;
1386 }
1387 if (MemorySpaceMap == NULL) {
1388 return EFI_INVALID_PARAMETER;
1389 }
1390
1391 CoreAcquireGcdMemoryLock ();
1392
1393 //
1394 // Count the number of descriptors
1395 //
1396 *NumberOfDescriptors = CoreCountGcdMapEntry (&mGcdMemorySpaceMap);
1397
1398 //
1399 // Allocate the MemorySpaceMap
1400 //
1401 *MemorySpaceMap = CoreAllocateBootServicesPool (*NumberOfDescriptors * sizeof (EFI_GCD_MEMORY_SPACE_DESCRIPTOR));
1402 if (*MemorySpaceMap == NULL) {
1403 Status = EFI_OUT_OF_RESOURCES;
1404 goto Done;
1405 }
1406
1407 //
1408 // Fill in the MemorySpaceMap
1409 //
1410 Descriptor = *MemorySpaceMap;
1411 Link = mGcdMemorySpaceMap.ForwardLink;
1412 while (Link != &mGcdMemorySpaceMap) {
1413 Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
1414 BuildMemoryDescriptor (Descriptor, Entry);
1415 Descriptor++;
1416 Link = Link->ForwardLink;
1417 }
1418 Status = EFI_SUCCESS;
1419
1420 Done:
1421 CoreReleaseGcdMemoryLock ();
1422 return Status;
1423 }
1424
1425
1426 /**
1427 Adds reserved I/O or I/O resources to the global coherency domain of the processor.
1428
1429 @param GcdIoType IO type of the segment.
1430 @param BaseAddress Base address of the segment.
1431 @param Length Length of the segment.
1432
1433 @retval EFI_SUCCESS Merged this segment into GCD map.
1434 @retval EFI_INVALID_PARAMETER Parameter not valid
1435
1436 **/
1437 EFI_STATUS
1438 CoreAddIoSpace (
1439 IN EFI_GCD_IO_TYPE GcdIoType,
1440 IN EFI_PHYSICAL_ADDRESS BaseAddress,
1441 IN UINT64 Length
1442 )
1443 {
1444 //
1445 // Make sure parameters are valid
1446 //
1447 if (GcdIoType <= EfiGcdIoTypeNonExistent || GcdIoType >= EfiGcdIoTypeMaximum) {
1448 return EFI_INVALID_PARAMETER;
1449 }
1450 return CoreConvertSpace (GCD_ADD_IO_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, GcdIoType, BaseAddress, Length, 0, 0);
1451 }
1452
1453
1454 /**
1455 Allocates nonexistent I/O, reserved I/O, or I/O resources from the global coherency
1456 domain of the processor.
1457
1458 @param GcdAllocateType The type of allocate operation
1459 @param GcdIoType The desired IO type
1460 @param Alignment Align with 2^Alignment
1461 @param Length Length to allocate
1462 @param BaseAddress Base address to allocate
1463 @param ImageHandle The image handle consume the allocated space.
1464 @param DeviceHandle The device handle consume the allocated space.
1465
1466 @retval EFI_INVALID_PARAMETER Invalid parameter.
1467 @retval EFI_NOT_FOUND No descriptor contains the desired space.
1468 @retval EFI_SUCCESS IO space successfully allocated.
1469
1470 **/
1471 EFI_STATUS
1472 CoreAllocateIoSpace (
1473 IN EFI_GCD_ALLOCATE_TYPE GcdAllocateType,
1474 IN EFI_GCD_IO_TYPE GcdIoType,
1475 IN UINTN Alignment,
1476 IN UINT64 Length,
1477 IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress,
1478 IN EFI_HANDLE ImageHandle,
1479 IN EFI_HANDLE DeviceHandle OPTIONAL
1480 )
1481 {
1482 return CoreAllocateSpace (
1483 GCD_ALLOCATE_IO_OPERATION,
1484 GcdAllocateType,
1485 (EFI_GCD_MEMORY_TYPE) 0,
1486 GcdIoType,
1487 Alignment,
1488 Length,
1489 BaseAddress,
1490 ImageHandle,
1491 DeviceHandle
1492 );
1493 }
1494
1495
1496 /**
1497 Frees nonexistent I/O, reserved I/O, or I/O resources from the global coherency
1498 domain of the processor.
1499
1500 @param BaseAddress Base address of the segment.
1501 @param Length Length of the segment.
1502
1503 @retval EFI_SUCCESS Space successfully freed.
1504
1505 **/
1506 EFI_STATUS
1507 CoreFreeIoSpace (
1508 IN EFI_PHYSICAL_ADDRESS BaseAddress,
1509 IN UINT64 Length
1510 )
1511 {
1512 return CoreConvertSpace (GCD_FREE_IO_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, 0, 0);
1513 }
1514
1515
1516 /**
1517 Removes reserved I/O or I/O resources from the global coherency domain of the
1518 processor.
1519
1520 @param BaseAddress Base address of the segment.
1521 @param Length Length of the segment.
1522
1523 @retval EFI_SUCCESS Successfully removed a segment of IO space.
1524
1525 **/
1526 EFI_STATUS
1527 CoreRemoveIoSpace (
1528 IN EFI_PHYSICAL_ADDRESS BaseAddress,
1529 IN UINT64 Length
1530 )
1531 {
1532 return CoreConvertSpace (GCD_REMOVE_IO_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, 0, 0);
1533 }
1534
1535
1536 /**
1537 Build a IO descriptor according to an entry.
1538
1539 @param Descriptor The descriptor to be built
1540 @param Entry According to this entry
1541
1542 **/
1543 VOID
1544 BuildIoDescriptor (
1545 IN EFI_GCD_IO_SPACE_DESCRIPTOR *Descriptor,
1546 IN EFI_GCD_MAP_ENTRY *Entry
1547 )
1548 {
1549 Descriptor->BaseAddress = Entry->BaseAddress;
1550 Descriptor->Length = Entry->EndAddress - Entry->BaseAddress + 1;
1551 Descriptor->GcdIoType = Entry->GcdIoType;
1552 Descriptor->ImageHandle = Entry->ImageHandle;
1553 Descriptor->DeviceHandle = Entry->DeviceHandle;
1554 }
1555
1556
1557 /**
1558 Retrieves the descriptor for an I/O region containing a specified address.
1559
1560 @param BaseAddress Specified start address
1561 @param Descriptor Specified length
1562
1563 @retval EFI_INVALID_PARAMETER Descriptor is NULL.
1564 @retval EFI_SUCCESS Successfully get the IO space descriptor.
1565
1566 **/
1567 EFI_STATUS
1568 CoreGetIoSpaceDescriptor (
1569 IN EFI_PHYSICAL_ADDRESS BaseAddress,
1570 OUT EFI_GCD_IO_SPACE_DESCRIPTOR *Descriptor
1571 )
1572 {
1573 EFI_STATUS Status;
1574 LIST_ENTRY *StartLink;
1575 LIST_ENTRY *EndLink;
1576 EFI_GCD_MAP_ENTRY *Entry;
1577
1578 //
1579 // Make sure parameters are valid
1580 //
1581 if (Descriptor == NULL) {
1582 return EFI_INVALID_PARAMETER;
1583 }
1584
1585 CoreAcquireGcdIoLock ();
1586
1587 //
1588 // Search for the list of descriptors that contain BaseAddress
1589 //
1590 Status = CoreSearchGcdMapEntry (BaseAddress, 1, &StartLink, &EndLink, &mGcdIoSpaceMap);
1591 if (EFI_ERROR (Status)) {
1592 Status = EFI_NOT_FOUND;
1593 } else {
1594 //
1595 // Copy the contents of the found descriptor into Descriptor
1596 //
1597 Entry = CR (StartLink, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
1598 BuildIoDescriptor (Descriptor, Entry);
1599 }
1600
1601 CoreReleaseGcdIoLock ();
1602
1603 return Status;
1604 }
1605
1606
1607 /**
1608 Returns a map of the I/O resources in the global coherency domain of the processor.
1609
1610 @param NumberOfDescriptors Number of descriptors.
1611 @param IoSpaceMap Descriptor array
1612
1613 @retval EFI_INVALID_PARAMETER Invalid parameter
1614 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1615 @retval EFI_SUCCESS Successfully get IO space map.
1616
1617 **/
1618 EFI_STATUS
1619 CoreGetIoSpaceMap (
1620 OUT UINTN *NumberOfDescriptors,
1621 OUT EFI_GCD_IO_SPACE_DESCRIPTOR **IoSpaceMap
1622 )
1623 {
1624 EFI_STATUS Status;
1625 LIST_ENTRY *Link;
1626 EFI_GCD_MAP_ENTRY *Entry;
1627 EFI_GCD_IO_SPACE_DESCRIPTOR *Descriptor;
1628
1629 //
1630 // Make sure parameters are valid
1631 //
1632 if (NumberOfDescriptors == NULL) {
1633 return EFI_INVALID_PARAMETER;
1634 }
1635 if (IoSpaceMap == NULL) {
1636 return EFI_INVALID_PARAMETER;
1637 }
1638
1639 CoreAcquireGcdIoLock ();
1640
1641 //
1642 // Count the number of descriptors
1643 //
1644 *NumberOfDescriptors = CoreCountGcdMapEntry (&mGcdIoSpaceMap);
1645
1646 //
1647 // Allocate the IoSpaceMap
1648 //
1649 *IoSpaceMap = CoreAllocateBootServicesPool (*NumberOfDescriptors * sizeof (EFI_GCD_IO_SPACE_DESCRIPTOR));
1650 if (*IoSpaceMap == NULL) {
1651 Status = EFI_OUT_OF_RESOURCES;
1652 goto Done;
1653 }
1654
1655 //
1656 // Fill in the IoSpaceMap
1657 //
1658 Descriptor = *IoSpaceMap;
1659 Link = mGcdIoSpaceMap.ForwardLink;
1660 while (Link != &mGcdIoSpaceMap) {
1661 Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
1662 BuildIoDescriptor (Descriptor, Entry);
1663 Descriptor++;
1664 Link = Link->ForwardLink;
1665 }
1666 Status = EFI_SUCCESS;
1667
1668 Done:
1669 CoreReleaseGcdIoLock ();
1670 return Status;
1671 }
1672
1673
1674 /**
1675 Converts a Resource Descriptor HOB attributes mask to an EFI Memory Descriptor
1676 capabilities mask
1677
1678 @param GcdMemoryType Type of resource in the GCD memory map.
1679 @param Attributes The attribute mask in the Resource Descriptor
1680 HOB.
1681
1682 @return The capabilities mask for an EFI Memory Descriptor.
1683
1684 **/
1685 UINT64
1686 CoreConvertResourceDescriptorHobAttributesToCapabilities (
1687 EFI_GCD_MEMORY_TYPE GcdMemoryType,
1688 UINT64 Attributes
1689 )
1690 {
1691 UINT64 Capabilities;
1692 GCD_ATTRIBUTE_CONVERSION_ENTRY *Conversion;
1693
1694 //
1695 // Convert the Resource HOB Attributes to an EFI Memory Capabilities mask
1696 //
1697 for (Capabilities = 0, Conversion = mAttributeConversionTable; Conversion->Attribute != 0; Conversion++) {
1698 if (Conversion->Memory || (GcdMemoryType != EfiGcdMemoryTypeSystemMemory)) {
1699 if (Attributes & Conversion->Attribute) {
1700 Capabilities |= Conversion->Capability;
1701 }
1702 }
1703 }
1704
1705 return Capabilities;
1706 }
1707
1708
1709 /**
1710 External function. Initializes the GCD and memory services based on the memory
1711 descriptor HOBs. This function is responsible for priming the GCD map and the
1712 memory map, so memory allocations and resource allocations can be made. The first
1713 part of this function can not depend on any memory services until at least one
1714 memory descriptor is provided to the memory services. Then the memory services
1715 can be used to intialize the GCD map.
1716
1717 @param HobStart The start address of the HOB.
1718 @param MemoryBaseAddress Start address of memory region found to init DXE
1719 core.
1720 @param MemoryLength Length of memory region found to init DXE core.
1721
1722 @retval EFI_SUCCESS Memory services successfully initialized.
1723
1724 **/
1725 EFI_STATUS
1726 CoreInitializeMemoryServices (
1727 IN VOID **HobStart,
1728 OUT EFI_PHYSICAL_ADDRESS *MemoryBaseAddress,
1729 OUT UINT64 *MemoryLength
1730 )
1731 {
1732 EFI_PEI_HOB_POINTERS Hob;
1733 EFI_MEMORY_TYPE_INFORMATION *EfiMemoryTypeInformation;
1734 UINTN DataSize;
1735 BOOLEAN Found;
1736 EFI_HOB_HANDOFF_INFO_TABLE *PhitHob;
1737 EFI_HOB_RESOURCE_DESCRIPTOR *ResourceHob;
1738 EFI_HOB_RESOURCE_DESCRIPTOR *PhitResourceHob;
1739 EFI_PHYSICAL_ADDRESS BaseAddress;
1740 UINT64 Length;
1741 UINT64 Attributes;
1742 UINT64 Capabilities;
1743 EFI_PHYSICAL_ADDRESS MaxMemoryBaseAddress;
1744 UINT64 MaxMemoryLength;
1745 UINT64 MaxMemoryAttributes;
1746 EFI_PHYSICAL_ADDRESS MaxAddress;
1747 EFI_PHYSICAL_ADDRESS HighAddress;
1748 EFI_HOB_RESOURCE_DESCRIPTOR *MaxResourceHob;
1749 EFI_HOB_GUID_TYPE *GuidHob;
1750
1751 //
1752 // Point at the first HOB. This must be the PHIT HOB.
1753 //
1754 Hob.Raw = *HobStart;
1755 ASSERT (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_HANDOFF);
1756
1757 //
1758 // Initialize the spin locks and maps in the memory services.
1759 // Also fill in the memory services into the EFI Boot Services Table
1760 //
1761 CoreInitializePool ();
1762
1763 //
1764 // Initialize Local Variables
1765 //
1766 PhitResourceHob = NULL;
1767 MaxResourceHob = NULL;
1768 ResourceHob = NULL;
1769 BaseAddress = 0;
1770 Length = 0;
1771 Attributes = 0;
1772 MaxMemoryBaseAddress = 0;
1773 MaxMemoryLength = 0;
1774 MaxMemoryAttributes = 0;
1775
1776 //
1777 // Cache the PHIT HOB for later use
1778 //
1779 PhitHob = Hob.HandoffInformationTable;
1780
1781 //
1782 // See if a Memory Type Information HOB is available
1783 //
1784 GuidHob = GetFirstGuidHob (&gEfiMemoryTypeInformationGuid);
1785 if (GuidHob != NULL) {
1786 EfiMemoryTypeInformation = GET_GUID_HOB_DATA (GuidHob);
1787 DataSize = GET_GUID_HOB_DATA_SIZE (GuidHob);
1788 if (EfiMemoryTypeInformation != NULL && DataSize > 0 && DataSize <= (EfiMaxMemoryType + 1) * sizeof (EFI_MEMORY_TYPE_INFORMATION)) {
1789 CopyMem (&gMemoryTypeInformation, EfiMemoryTypeInformation, DataSize);
1790 }
1791 }
1792
1793 //
1794 // Find the Resource Descriptor HOB that contains range FreeMemoryBaseAddress..FreeMemoryLength
1795 //
1796 Length = 0;
1797 Found = FALSE;
1798 for (Hob.Raw = *HobStart; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {
1799
1800 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {
1801
1802 ResourceHob = Hob.ResourceDescriptor;
1803
1804 if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&
1805 (ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) == TESTED_MEMORY_ATTRIBUTES ) {
1806
1807 if (PhitHob->EfiFreeMemoryBottom >= ResourceHob->PhysicalStart &&
1808 PhitHob->EfiFreeMemoryTop <= (ResourceHob->PhysicalStart + ResourceHob->ResourceLength) ) {
1809
1810 //
1811 // Cache the resource descriptor HOB for the memory region described by the PHIT HOB
1812 //
1813 PhitResourceHob = ResourceHob;
1814 Found = TRUE;
1815
1816 Attributes = PhitResourceHob->ResourceAttribute;
1817 BaseAddress = PageAlignAddress (PhitHob->EfiMemoryTop);
1818 Length = PageAlignLength (ResourceHob->PhysicalStart + ResourceHob->ResourceLength - BaseAddress);
1819 if (Length < MINIMUM_INITIAL_MEMORY_SIZE) {
1820 BaseAddress = PageAlignAddress (PhitHob->EfiFreeMemoryBottom);
1821 Length = PageAlignLength (PhitHob->EfiFreeMemoryTop - BaseAddress);
1822 if (Length < MINIMUM_INITIAL_MEMORY_SIZE) {
1823 BaseAddress = PageAlignAddress (ResourceHob->PhysicalStart);
1824 Length = PageAlignLength ((UINT64)((UINTN)*HobStart - BaseAddress));
1825 }
1826 }
1827 break;
1828 }
1829 }
1830 }
1831 }
1832
1833 //
1834 // Assert if a resource descriptor HOB for the memory region described by the PHIT was not found
1835 //
1836 ASSERT (Found);
1837
1838 //
1839 // Search all the resource descriptor HOBs from the highest possible addresses down for a memory
1840 // region that is big enough to initialize the DXE core. Always skip the PHIT Resource HOB.
1841 // The max address must be within the physically addressible range for the processor.
1842 //
1843 MaxMemoryLength = 0;
1844 MaxAddress = EFI_MAX_ADDRESS;
1845 do {
1846 HighAddress = 0;
1847 Found = FALSE;
1848 //
1849 // Search for a tested memory region that is below MaxAddress
1850 //
1851 for (Hob.Raw = *HobStart; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {
1852
1853 //
1854 // See if this is a resource descriptor HOB that does not contain the PHIT.
1855 //
1856 if (Hob.ResourceDescriptor != PhitResourceHob && GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {
1857
1858 ResourceHob = Hob.ResourceDescriptor;
1859 //
1860 // See if this resource descrior HOB describes tested system memory below MaxAddress
1861 //
1862 if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&
1863 (ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) == TESTED_MEMORY_ATTRIBUTES &&
1864 ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MaxAddress ) {
1865
1866 //
1867 // See if this is the highest tested system memory region below MaxAddress
1868 //
1869 if (ResourceHob->PhysicalStart > HighAddress) {
1870
1871 MaxResourceHob = ResourceHob;
1872 HighAddress = MaxResourceHob->PhysicalStart;
1873 Found = TRUE;
1874 }
1875 }
1876 }
1877 }
1878 if (Found) {
1879 //
1880 // Compute the size of the tested memory region below MaxAddrees
1881 //
1882 MaxMemoryBaseAddress = PageAlignAddress (MaxResourceHob->PhysicalStart);
1883 MaxMemoryLength = PageAlignLength (MaxResourceHob->PhysicalStart + MaxResourceHob->ResourceLength - MaxMemoryBaseAddress);
1884 MaxMemoryAttributes = MaxResourceHob->ResourceAttribute;
1885 }
1886 MaxAddress = ResourceHob->PhysicalStart;
1887 } while (Found && MaxMemoryLength < MINIMUM_INITIAL_MEMORY_SIZE);
1888
1889 //
1890 //
1891 //
1892 if ((Length < MINIMUM_INITIAL_MEMORY_SIZE) ||
1893 (MaxMemoryBaseAddress > BaseAddress && MaxMemoryLength >= MINIMUM_INITIAL_MEMORY_SIZE) ) {
1894 BaseAddress = MaxMemoryBaseAddress;
1895 Length = MaxMemoryLength;
1896 Attributes = MaxMemoryAttributes;
1897 }
1898
1899 //
1900 // If no memory regions are found that are big enough to initialize the DXE core, then ASSERT().
1901 //
1902 ASSERT (Length >= MINIMUM_INITIAL_MEMORY_SIZE);
1903
1904 //
1905 // Convert the Resource HOB Attributes to an EFI Memory Capabilities mask
1906 //
1907 Capabilities = CoreConvertResourceDescriptorHobAttributesToCapabilities (EfiGcdMemoryTypeSystemMemory, Attributes);
1908
1909 //
1910 // Declare the very first memory region, so the EFI Memory Services are available.
1911 //
1912 CoreAddMemoryDescriptor (
1913 EfiConventionalMemory,
1914 BaseAddress,
1915 RShiftU64 (Length, EFI_PAGE_SHIFT),
1916 Capabilities
1917 );
1918
1919 *MemoryBaseAddress = BaseAddress;
1920 *MemoryLength = Length;
1921
1922 return EFI_SUCCESS;
1923 }
1924
1925
1926 /**
1927 External function. Initializes the GCD and memory services based on the memory
1928 descriptor HOBs. This function is responsible for priming the GCD map and the
1929 memory map, so memory allocations and resource allocations can be made. The first
1930 part of this function can not depend on any memory services until at least one
1931 memory descriptor is provided to the memory services. Then the memory services
1932 can be used to intialize the GCD map. The HobStart will be relocated to a pool
1933 buffer.
1934
1935 @param HobStart The start address of the HOB
1936 @param MemoryBaseAddress Start address of memory region found to init DXE
1937 core.
1938 @param MemoryLength Length of memory region found to init DXE core.
1939
1940 @retval EFI_SUCCESS GCD services successfully initialized.
1941
1942 **/
1943 EFI_STATUS
1944 CoreInitializeGcdServices (
1945 IN OUT VOID **HobStart,
1946 IN EFI_PHYSICAL_ADDRESS MemoryBaseAddress,
1947 IN UINT64 MemoryLength
1948 )
1949 {
1950 EFI_PEI_HOB_POINTERS Hob;
1951 VOID *NewHobList;
1952 EFI_HOB_HANDOFF_INFO_TABLE *PhitHob;
1953 UINT8 SizeOfMemorySpace;
1954 UINT8 SizeOfIoSpace;
1955 EFI_HOB_RESOURCE_DESCRIPTOR *ResourceHob;
1956 EFI_PHYSICAL_ADDRESS BaseAddress;
1957 UINT64 Length;
1958 EFI_STATUS Status;
1959 EFI_GCD_MAP_ENTRY *Entry;
1960 EFI_GCD_MEMORY_TYPE GcdMemoryType;
1961 EFI_GCD_IO_TYPE GcdIoType;
1962 EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor;
1963 EFI_HOB_MEMORY_ALLOCATION *MemoryHob;
1964 EFI_HOB_FIRMWARE_VOLUME *FirmwareVolumeHob;
1965 UINTN NumberOfDescriptors;
1966 EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap;
1967 UINTN Index;
1968 UINT64 Capabilities;
1969 EFI_HOB_CPU * CpuHob;
1970 //
1971 // Cache the PHIT HOB for later use
1972 //
1973 PhitHob = (EFI_HOB_HANDOFF_INFO_TABLE *)(*HobStart);
1974
1975 //
1976 // Get the number of address lines in the I/O and Memory space for the CPU
1977 //
1978 CpuHob = GetFirstHob (EFI_HOB_TYPE_CPU);
1979 ASSERT (CpuHob != NULL);
1980 SizeOfMemorySpace = CpuHob->SizeOfMemorySpace;
1981 SizeOfIoSpace = CpuHob->SizeOfIoSpace;
1982
1983 //
1984 // Initialize the GCD Memory Space Map
1985 //
1986 Entry = CoreAllocateCopyPool (sizeof (EFI_GCD_MAP_ENTRY), &mGcdMemorySpaceMapEntryTemplate);
1987 ASSERT (Entry != NULL);
1988
1989 Entry->EndAddress = LShiftU64 (1, SizeOfMemorySpace) - 1;
1990
1991 InsertHeadList (&mGcdMemorySpaceMap, &Entry->Link);
1992
1993 //
1994 // Initialize the GCD I/O Space Map
1995 //
1996 Entry = CoreAllocateCopyPool (sizeof (EFI_GCD_MAP_ENTRY), &mGcdIoSpaceMapEntryTemplate);
1997 ASSERT (Entry != NULL);
1998
1999 Entry->EndAddress = LShiftU64 (1, SizeOfIoSpace) - 1;
2000
2001 InsertHeadList (&mGcdIoSpaceMap, &Entry->Link);
2002
2003 //
2004 // Walk the HOB list and add all resource descriptors to the GCD
2005 //
2006 for (Hob.Raw = *HobStart; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {
2007
2008 GcdMemoryType = EfiGcdMemoryTypeNonExistent;
2009 GcdIoType = EfiGcdIoTypeNonExistent;
2010
2011 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {
2012
2013 ResourceHob = Hob.ResourceDescriptor;
2014
2015 switch (ResourceHob->ResourceType) {
2016 case EFI_RESOURCE_SYSTEM_MEMORY:
2017 if ((ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) == TESTED_MEMORY_ATTRIBUTES) {
2018 GcdMemoryType = EfiGcdMemoryTypeSystemMemory;
2019 }
2020 if ((ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) == INITIALIZED_MEMORY_ATTRIBUTES) {
2021 GcdMemoryType = EfiGcdMemoryTypeReserved;
2022 }
2023 if ((ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) == PRESENT_MEMORY_ATTRIBUTES) {
2024 GcdMemoryType = EfiGcdMemoryTypeReserved;
2025 }
2026 break;
2027 case EFI_RESOURCE_MEMORY_MAPPED_IO:
2028 case EFI_RESOURCE_FIRMWARE_DEVICE:
2029 GcdMemoryType = EfiGcdMemoryTypeMemoryMappedIo;
2030 break;
2031 case EFI_RESOURCE_MEMORY_MAPPED_IO_PORT:
2032 case EFI_RESOURCE_MEMORY_RESERVED:
2033 GcdMemoryType = EfiGcdMemoryTypeReserved;
2034 break;
2035 case EFI_RESOURCE_IO:
2036 GcdIoType = EfiGcdIoTypeIo;
2037 break;
2038 case EFI_RESOURCE_IO_RESERVED:
2039 GcdIoType = EfiGcdIoTypeReserved;
2040 break;
2041 }
2042
2043 if (GcdMemoryType != EfiGcdMemoryTypeNonExistent) {
2044
2045 //
2046 // Convert the Resource HOB Attributes to an EFI Memory Capabilities mask
2047 //
2048 Capabilities = CoreConvertResourceDescriptorHobAttributesToCapabilities (
2049 GcdMemoryType,
2050 ResourceHob->ResourceAttribute
2051 );
2052
2053 Status = CoreInternalAddMemorySpace (
2054 GcdMemoryType,
2055 ResourceHob->PhysicalStart,
2056 ResourceHob->ResourceLength,
2057 Capabilities
2058 );
2059 }
2060
2061 if (GcdIoType != EfiGcdIoTypeNonExistent) {
2062 Status = CoreAddIoSpace (
2063 GcdIoType,
2064 ResourceHob->PhysicalStart,
2065 ResourceHob->ResourceLength
2066 );
2067 }
2068 }
2069 }
2070
2071 //
2072 // Allocate first memory region from the GCD by the DXE core
2073 //
2074 Status = CoreAllocateMemorySpace (
2075 EfiGcdAllocateAddress,
2076 EfiGcdMemoryTypeSystemMemory,
2077 0,
2078 MemoryLength,
2079 &MemoryBaseAddress,
2080 gDxeCoreImageHandle,
2081 NULL
2082 );
2083
2084 //
2085 // Walk the HOB list and allocate all memory space that is consumed by memory allocation HOBs,
2086 // and Firmware Volume HOBs. Also update the EFI Memory Map with the memory allocation HOBs.
2087 //
2088 for (Hob.Raw = *HobStart; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {
2089 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_MEMORY_ALLOCATION) {
2090 MemoryHob = Hob.MemoryAllocation;
2091 BaseAddress = MemoryHob->AllocDescriptor.MemoryBaseAddress;
2092 Status = CoreGetMemorySpaceDescriptor (BaseAddress, &Descriptor);
2093 if (!EFI_ERROR (Status)) {
2094 Status = CoreAllocateMemorySpace (
2095 EfiGcdAllocateAddress,
2096 Descriptor.GcdMemoryType,
2097 0,
2098 MemoryHob->AllocDescriptor.MemoryLength,
2099 &BaseAddress,
2100 gDxeCoreImageHandle,
2101 NULL
2102 );
2103 if (!EFI_ERROR (Status) && Descriptor.GcdMemoryType == EfiGcdMemoryTypeSystemMemory) {
2104 CoreAddMemoryDescriptor (
2105 MemoryHob->AllocDescriptor.MemoryType,
2106 MemoryHob->AllocDescriptor.MemoryBaseAddress,
2107 RShiftU64 (MemoryHob->AllocDescriptor.MemoryLength, EFI_PAGE_SHIFT),
2108 Descriptor.Capabilities & (~EFI_MEMORY_RUNTIME)
2109 );
2110 }
2111 }
2112 }
2113
2114 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_FV) {
2115 FirmwareVolumeHob = Hob.FirmwareVolume;
2116 BaseAddress = FirmwareVolumeHob->BaseAddress;
2117 Status = CoreAllocateMemorySpace (
2118 EfiGcdAllocateAddress,
2119 EfiGcdMemoryTypeMemoryMappedIo,
2120 0,
2121 FirmwareVolumeHob->Length,
2122 &BaseAddress,
2123 gDxeCoreImageHandle,
2124 NULL
2125 );
2126 }
2127 }
2128
2129 //
2130 // Relocate HOB List to an allocated pool buffer.
2131 //
2132 NewHobList = CoreAllocateCopyPool (
2133 (UINTN)PhitHob->EfiFreeMemoryBottom - (UINTN)(*HobStart),
2134 *HobStart
2135 );
2136 ASSERT (NewHobList != NULL);
2137
2138 *HobStart = NewHobList;
2139 gHobList = NewHobList;
2140
2141 //
2142 // Add and allocate the remaining unallocated system memory to the memory services.
2143 //
2144 Status = CoreGetMemorySpaceMap (&NumberOfDescriptors, &MemorySpaceMap);
2145 for (Index = 0; Index < NumberOfDescriptors; Index++) {
2146 if (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeSystemMemory) {
2147 if (MemorySpaceMap[Index].ImageHandle == NULL) {
2148 BaseAddress = PageAlignAddress (MemorySpaceMap[Index].BaseAddress);
2149 Length = PageAlignLength (MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length - BaseAddress);
2150 if (Length == 0 || MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length < BaseAddress) {
2151 continue;
2152 }
2153 CoreAddMemoryDescriptor (
2154 EfiConventionalMemory,
2155 BaseAddress,
2156 RShiftU64 (Length, EFI_PAGE_SHIFT),
2157 MemorySpaceMap[Index].Capabilities & (~EFI_MEMORY_RUNTIME)
2158 );
2159 Status = CoreAllocateMemorySpace (
2160 EfiGcdAllocateAddress,
2161 EfiGcdMemoryTypeSystemMemory,
2162 0,
2163 Length,
2164 &BaseAddress,
2165 gDxeCoreImageHandle,
2166 NULL
2167 );
2168 }
2169 }
2170 }
2171 CoreFreePool (MemorySpaceMap);
2172
2173 return EFI_SUCCESS;
2174 }