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