]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/CpuDxe/CpuDxe.c
UefiCpuPkg/CpuDxe: Remove PcdCpuMaxLogicalProcessorNumber consuming
[mirror_edk2.git] / UefiCpuPkg / CpuDxe / CpuDxe.c
CommitLineData
a47463f2 1/** @file\r
7fadaacd 2 CPU DXE Module to produce CPU ARCH Protocol.\r
a47463f2 3\r
afa7b971 4 Copyright (c) 2008 - 2016, Intel Corporation. All rights reserved.<BR>\r
01a1c0fc 5 This program and the accompanying materials\r
a47463f2 6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "CpuDxe.h"\r
6022e28c 16#include "CpuMp.h"\r
a47463f2 17\r
18//\r
19// Global Variables\r
20//\r
a47463f2 21BOOLEAN InterruptState = FALSE;\r
22EFI_HANDLE mCpuHandle = NULL;\r
23BOOLEAN mIsFlushingGCD;\r
a47463f2 24UINT64 mValidMtrrAddressMask = MTRR_LIB_CACHE_VALID_ADDRESS;\r
25UINT64 mValidMtrrBitsMask = MTRR_LIB_MSR_VALID_MASK;\r
26\r
27FIXED_MTRR mFixedMtrrTable[] = {\r
28 {\r
29 MTRR_LIB_IA32_MTRR_FIX64K_00000,\r
30 0,\r
31 0x10000\r
32 },\r
33 {\r
34 MTRR_LIB_IA32_MTRR_FIX16K_80000,\r
35 0x80000,\r
36 0x4000\r
37 },\r
38 {\r
39 MTRR_LIB_IA32_MTRR_FIX16K_A0000,\r
40 0xA0000,\r
41 0x4000\r
42 },\r
43 {\r
44 MTRR_LIB_IA32_MTRR_FIX4K_C0000,\r
45 0xC0000,\r
46 0x1000\r
47 },\r
48 {\r
49 MTRR_LIB_IA32_MTRR_FIX4K_C8000,\r
50 0xC8000,\r
51 0x1000\r
52 },\r
53 {\r
54 MTRR_LIB_IA32_MTRR_FIX4K_D0000,\r
55 0xD0000,\r
56 0x1000\r
57 },\r
58 {\r
59 MTRR_LIB_IA32_MTRR_FIX4K_D8000,\r
60 0xD8000,\r
61 0x1000\r
62 },\r
63 {\r
64 MTRR_LIB_IA32_MTRR_FIX4K_E0000,\r
65 0xE0000,\r
66 0x1000\r
67 },\r
68 {\r
69 MTRR_LIB_IA32_MTRR_FIX4K_E8000,\r
70 0xE8000,\r
71 0x1000\r
72 },\r
73 {\r
74 MTRR_LIB_IA32_MTRR_FIX4K_F0000,\r
75 0xF0000,\r
76 0x1000\r
77 },\r
78 {\r
79 MTRR_LIB_IA32_MTRR_FIX4K_F8000,\r
80 0xF8000,\r
81 0x1000\r
82 },\r
83};\r
84\r
85\r
86EFI_CPU_ARCH_PROTOCOL gCpu = {\r
87 CpuFlushCpuDataCache,\r
88 CpuEnableInterrupt,\r
89 CpuDisableInterrupt,\r
90 CpuGetInterruptState,\r
91 CpuInit,\r
92 CpuRegisterInterruptHandler,\r
93 CpuGetTimerValue,\r
94 CpuSetMemoryAttributes,\r
95 1, // NumberOfTimers\r
96 4 // DmaBufferAlignment\r
97};\r
98\r
a47463f2 99//\r
100// CPU Arch Protocol Functions\r
101//\r
102\r
a47463f2 103/**\r
104 Flush CPU data cache. If the instruction cache is fully coherent\r
105 with all DMA operations then function can just return EFI_SUCCESS.\r
106\r
107 @param This Protocol instance structure\r
108 @param Start Physical address to start flushing from.\r
109 @param Length Number of bytes to flush. Round up to chipset\r
110 granularity.\r
111 @param FlushType Specifies the type of flush operation to perform.\r
112\r
113 @retval EFI_SUCCESS If cache was flushed\r
114 @retval EFI_UNSUPPORTED If flush type is not supported.\r
115 @retval EFI_DEVICE_ERROR If requested range could not be flushed.\r
116\r
117**/\r
118EFI_STATUS\r
119EFIAPI\r
120CpuFlushCpuDataCache (\r
121 IN EFI_CPU_ARCH_PROTOCOL *This,\r
122 IN EFI_PHYSICAL_ADDRESS Start,\r
123 IN UINT64 Length,\r
124 IN EFI_CPU_FLUSH_TYPE FlushType\r
125 )\r
126{\r
127 if (FlushType == EfiCpuFlushTypeWriteBackInvalidate) {\r
128 AsmWbinvd ();\r
129 return EFI_SUCCESS;\r
130 } else if (FlushType == EfiCpuFlushTypeInvalidate) {\r
131 AsmInvd ();\r
132 return EFI_SUCCESS;\r
133 } else {\r
134 return EFI_UNSUPPORTED;\r
135 }\r
136}\r
137\r
138\r
139/**\r
140 Enables CPU interrupts.\r
141\r
142 @param This Protocol instance structure\r
143\r
144 @retval EFI_SUCCESS If interrupts were enabled in the CPU\r
145 @retval EFI_DEVICE_ERROR If interrupts could not be enabled on the CPU.\r
146\r
147**/\r
148EFI_STATUS\r
149EFIAPI\r
150CpuEnableInterrupt (\r
151 IN EFI_CPU_ARCH_PROTOCOL *This\r
152 )\r
153{\r
154 EnableInterrupts ();\r
155\r
156 InterruptState = TRUE;\r
157 return EFI_SUCCESS;\r
158}\r
159\r
160\r
161/**\r
162 Disables CPU interrupts.\r
163\r
164 @param This Protocol instance structure\r
165\r
166 @retval EFI_SUCCESS If interrupts were disabled in the CPU.\r
167 @retval EFI_DEVICE_ERROR If interrupts could not be disabled on the CPU.\r
168\r
169**/\r
170EFI_STATUS\r
171EFIAPI\r
172CpuDisableInterrupt (\r
173 IN EFI_CPU_ARCH_PROTOCOL *This\r
174 )\r
175{\r
176 DisableInterrupts ();\r
177\r
178 InterruptState = FALSE;\r
179 return EFI_SUCCESS;\r
180}\r
181\r
182\r
183/**\r
184 Return the state of interrupts.\r
185\r
186 @param This Protocol instance structure\r
187 @param State Pointer to the CPU's current interrupt state\r
188\r
189 @retval EFI_SUCCESS If interrupts were disabled in the CPU.\r
190 @retval EFI_INVALID_PARAMETER State is NULL.\r
191\r
192**/\r
193EFI_STATUS\r
194EFIAPI\r
195CpuGetInterruptState (\r
196 IN EFI_CPU_ARCH_PROTOCOL *This,\r
197 OUT BOOLEAN *State\r
198 )\r
199{\r
200 if (State == NULL) {\r
201 return EFI_INVALID_PARAMETER;\r
202 }\r
203\r
204 *State = InterruptState;\r
205 return EFI_SUCCESS;\r
206}\r
207\r
208\r
209/**\r
210 Generates an INIT to the CPU.\r
211\r
212 @param This Protocol instance structure\r
213 @param InitType Type of CPU INIT to perform\r
214\r
215 @retval EFI_SUCCESS If CPU INIT occurred. This value should never be\r
216 seen.\r
217 @retval EFI_DEVICE_ERROR If CPU INIT failed.\r
218 @retval EFI_UNSUPPORTED Requested type of CPU INIT not supported.\r
219\r
220**/\r
221EFI_STATUS\r
222EFIAPI\r
223CpuInit (\r
224 IN EFI_CPU_ARCH_PROTOCOL *This,\r
225 IN EFI_CPU_INIT_TYPE InitType\r
226 )\r
227{\r
228 return EFI_UNSUPPORTED;\r
229}\r
230\r
231\r
232/**\r
233 Registers a function to be called from the CPU interrupt handler.\r
234\r
235 @param This Protocol instance structure\r
236 @param InterruptType Defines which interrupt to hook. IA-32\r
237 valid range is 0x00 through 0xFF\r
238 @param InterruptHandler A pointer to a function of type\r
239 EFI_CPU_INTERRUPT_HANDLER that is called\r
240 when a processor interrupt occurs. A null\r
241 pointer is an error condition.\r
242\r
243 @retval EFI_SUCCESS If handler installed or uninstalled.\r
244 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler\r
245 for InterruptType was previously installed.\r
246 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for\r
247 InterruptType was not previously installed.\r
248 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType\r
249 is not supported.\r
250\r
251**/\r
252EFI_STATUS\r
253EFIAPI\r
254CpuRegisterInterruptHandler (\r
255 IN EFI_CPU_ARCH_PROTOCOL *This,\r
256 IN EFI_EXCEPTION_TYPE InterruptType,\r
257 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r
258 )\r
259{\r
e41aad15 260 return RegisterCpuInterruptHandler (InterruptType, InterruptHandler);\r
a47463f2 261}\r
262\r
263\r
264/**\r
265 Returns a timer value from one of the CPU's internal timers. There is no\r
266 inherent time interval between ticks but is a function of the CPU frequency.\r
267\r
268 @param This - Protocol instance structure.\r
269 @param TimerIndex - Specifies which CPU timer is requested.\r
270 @param TimerValue - Pointer to the returned timer value.\r
271 @param TimerPeriod - A pointer to the amount of time that passes\r
272 in femtoseconds (10-15) for each increment\r
273 of TimerValue. If TimerValue does not\r
274 increment at a predictable rate, then 0 is\r
275 returned. The amount of time that has\r
276 passed between two calls to GetTimerValue()\r
277 can be calculated with the formula\r
278 (TimerValue2 - TimerValue1) * TimerPeriod.\r
279 This parameter is optional and may be NULL.\r
280\r
281 @retval EFI_SUCCESS - If the CPU timer count was returned.\r
282 @retval EFI_UNSUPPORTED - If the CPU does not have any readable timers.\r
283 @retval EFI_DEVICE_ERROR - If an error occurred while reading the timer.\r
284 @retval EFI_INVALID_PARAMETER - TimerIndex is not valid or TimerValue is NULL.\r
285\r
286**/\r
287EFI_STATUS\r
288EFIAPI\r
289CpuGetTimerValue (\r
290 IN EFI_CPU_ARCH_PROTOCOL *This,\r
291 IN UINT32 TimerIndex,\r
292 OUT UINT64 *TimerValue,\r
293 OUT UINT64 *TimerPeriod OPTIONAL\r
294 )\r
295{\r
296 if (TimerValue == NULL) {\r
297 return EFI_INVALID_PARAMETER;\r
298 }\r
299\r
300 if (TimerIndex != 0) {\r
301 return EFI_INVALID_PARAMETER;\r
302 }\r
303\r
304 *TimerValue = AsmReadTsc ();\r
305\r
306 if (TimerPeriod != NULL) {\r
307 //\r
308 // BugBug: Hard coded. Don't know how to do this generically\r
309 //\r
310 *TimerPeriod = 1000000000;\r
311 }\r
312\r
313 return EFI_SUCCESS;\r
314}\r
315\r
0b9f0dd6
JF
316/**\r
317 A minimal wrapper function that allows MtrrSetAllMtrrs() to be passed to\r
318 EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() as Procedure.\r
319\r
320 @param[in] Buffer Pointer to an MTRR_SETTINGS object, to be passed to\r
321 MtrrSetAllMtrrs().\r
322**/\r
323VOID\r
324EFIAPI\r
325SetMtrrsFromBuffer (\r
326 IN VOID *Buffer\r
327 )\r
328{\r
329 MtrrSetAllMtrrs (Buffer);\r
330}\r
a47463f2 331\r
332/**\r
4ec21e8b 333 Implementation of SetMemoryAttributes() service of CPU Architecture Protocol.\r
334\r
335 This function modifies the attributes for the memory region specified by BaseAddress and\r
336 Length from their current attributes to the attributes specified by Attributes.\r
337\r
338 @param This The EFI_CPU_ARCH_PROTOCOL instance.\r
339 @param BaseAddress The physical address that is the start address of a memory region.\r
340 @param Length The size in bytes of the memory region.\r
341 @param Attributes The bit mask of attributes to set for the memory region.\r
342\r
343 @retval EFI_SUCCESS The attributes were set for the memory region.\r
344 @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by\r
345 BaseAddress and Length cannot be modified.\r
346 @retval EFI_INVALID_PARAMETER Length is zero.\r
347 Attributes specified an illegal combination of attributes that\r
348 cannot be set together.\r
349 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r
350 the memory resource range.\r
351 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory\r
352 resource range specified by BaseAddress and Length.\r
353 The bit mask of attributes is not support for the memory resource\r
354 range specified by BaseAddress and Length.\r
a47463f2 355\r
356**/\r
357EFI_STATUS\r
358EFIAPI\r
359CpuSetMemoryAttributes (\r
360 IN EFI_CPU_ARCH_PROTOCOL *This,\r
361 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
362 IN UINT64 Length,\r
363 IN UINT64 Attributes\r
364 )\r
365{\r
366 RETURN_STATUS Status;\r
367 MTRR_MEMORY_CACHE_TYPE CacheType;\r
94941c88
LE
368 EFI_STATUS MpStatus;\r
369 EFI_MP_SERVICES_PROTOCOL *MpService;\r
370 MTRR_SETTINGS MtrrSettings;\r
a47463f2 371\r
947a573a 372 if (!IsMtrrSupported ()) {\r
373 return EFI_UNSUPPORTED;\r
374 }\r
375\r
a47463f2 376 //\r
377 // If this function is called because GCD SetMemorySpaceAttributes () is called\r
378 // by RefreshGcdMemoryAttributes (), then we are just synchronzing GCD memory\r
379 // map with MTRR values. So there is no need to modify MTRRs, just return immediately\r
380 // to avoid unnecessary computing.\r
381 //\r
382 if (mIsFlushingGCD) {\r
79aca636
SEHM
383 DEBUG((EFI_D_INFO, " Flushing GCD\n"));\r
384 return EFI_SUCCESS;\r
385 }\r
a47463f2 386\r
387 switch (Attributes) {\r
388 case EFI_MEMORY_UC:\r
389 CacheType = CacheUncacheable;\r
390 break;\r
391\r
392 case EFI_MEMORY_WC:\r
393 CacheType = CacheWriteCombining;\r
394 break;\r
395\r
396 case EFI_MEMORY_WT:\r
397 CacheType = CacheWriteThrough;\r
398 break;\r
399\r
400 case EFI_MEMORY_WP:\r
401 CacheType = CacheWriteProtected;\r
402 break;\r
403\r
404 case EFI_MEMORY_WB:\r
405 CacheType = CacheWriteBack;\r
406 break;\r
407\r
4ec21e8b 408 case EFI_MEMORY_UCE:\r
409 case EFI_MEMORY_RP:\r
410 case EFI_MEMORY_XP:\r
411 case EFI_MEMORY_RUNTIME:\r
a47463f2 412 return EFI_UNSUPPORTED;\r
4ec21e8b 413\r
414 default:\r
415 return EFI_INVALID_PARAMETER;\r
a47463f2 416 }\r
417 //\r
418 // call MTRR libary function\r
419 //\r
f877f300 420 Status = MtrrSetMemoryAttribute (\r
a47463f2 421 BaseAddress,\r
422 Length,\r
423 CacheType\r
424 );\r
425\r
94941c88
LE
426 if (!RETURN_ERROR (Status)) {\r
427 MpStatus = gBS->LocateProtocol (\r
428 &gEfiMpServiceProtocolGuid,\r
429 NULL,\r
430 (VOID **)&MpService\r
431 );\r
432 //\r
433 // Synchronize the update with all APs\r
434 //\r
435 if (!EFI_ERROR (MpStatus)) {\r
436 MtrrGetAllMtrrs (&MtrrSettings);\r
437 MpStatus = MpService->StartupAllAPs (\r
438 MpService, // This\r
439 SetMtrrsFromBuffer, // Procedure\r
afa7b971 440 FALSE, // SingleThread\r
94941c88
LE
441 NULL, // WaitEvent\r
442 0, // TimeoutInMicrosecsond\r
443 &MtrrSettings, // ProcedureArgument\r
444 NULL // FailedCpuList\r
445 );\r
446 ASSERT (MpStatus == EFI_SUCCESS || MpStatus == EFI_NOT_STARTED);\r
447 }\r
448 }\r
a47463f2 449 return (EFI_STATUS) Status;\r
450}\r
451\r
452/**\r
453 Initializes the valid bits mask and valid address mask for MTRRs.\r
454\r
455 This function initializes the valid bits mask and valid address mask for MTRRs.\r
456\r
457**/\r
458VOID\r
459InitializeMtrrMask (\r
460 VOID\r
461 )\r
462{\r
463 UINT32 RegEax;\r
464 UINT8 PhysicalAddressBits;\r
465\r
466 AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);\r
467\r
468 if (RegEax >= 0x80000008) {\r
469 AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);\r
470\r
471 PhysicalAddressBits = (UINT8) RegEax;\r
472\r
473 mValidMtrrBitsMask = LShiftU64 (1, PhysicalAddressBits) - 1;\r
474 mValidMtrrAddressMask = mValidMtrrBitsMask & 0xfffffffffffff000ULL;\r
475 } else {\r
476 mValidMtrrBitsMask = MTRR_LIB_MSR_VALID_MASK;\r
477 mValidMtrrAddressMask = MTRR_LIB_CACHE_VALID_ADDRESS;\r
478 }\r
479}\r
480\r
481/**\r
430fbbe0 482 Gets GCD Mem Space type from MTRR Type.\r
a47463f2 483\r
430fbbe0 484 This function gets GCD Mem Space type from MTRR Type.\r
a47463f2 485\r
430fbbe0 486 @param MtrrAttributes MTRR memory type\r
a47463f2 487\r
488 @return GCD Mem Space type\r
489\r
490**/\r
491UINT64\r
492GetMemorySpaceAttributeFromMtrrType (\r
2c4b1bdc 493 IN UINT8 MtrrAttributes\r
a47463f2 494 )\r
495{\r
496 switch (MtrrAttributes) {\r
497 case MTRR_CACHE_UNCACHEABLE:\r
498 return EFI_MEMORY_UC;\r
499 case MTRR_CACHE_WRITE_COMBINING:\r
500 return EFI_MEMORY_WC;\r
501 case MTRR_CACHE_WRITE_THROUGH:\r
502 return EFI_MEMORY_WT;\r
503 case MTRR_CACHE_WRITE_PROTECTED:\r
504 return EFI_MEMORY_WP;\r
505 case MTRR_CACHE_WRITE_BACK:\r
506 return EFI_MEMORY_WB;\r
507 default:\r
508 return 0;\r
509 }\r
510}\r
511\r
512/**\r
513 Searches memory descriptors covered by given memory range.\r
514\r
515 This function searches into the Gcd Memory Space for descriptors\r
516 (from StartIndex to EndIndex) that contains the memory range\r
517 specified by BaseAddress and Length.\r
518\r
519 @param MemorySpaceMap Gcd Memory Space Map as array.\r
520 @param NumberOfDescriptors Number of descriptors in map.\r
521 @param BaseAddress BaseAddress for the requested range.\r
522 @param Length Length for the requested range.\r
523 @param StartIndex Start index into the Gcd Memory Space Map.\r
524 @param EndIndex End index into the Gcd Memory Space Map.\r
525\r
526 @retval EFI_SUCCESS Search successfully.\r
527 @retval EFI_NOT_FOUND The requested descriptors does not exist.\r
528\r
529**/\r
530EFI_STATUS\r
531SearchGcdMemorySpaces (\r
532 IN EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap,\r
533 IN UINTN NumberOfDescriptors,\r
534 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
535 IN UINT64 Length,\r
536 OUT UINTN *StartIndex,\r
537 OUT UINTN *EndIndex\r
538 )\r
539{\r
540 UINTN Index;\r
541\r
542 *StartIndex = 0;\r
543 *EndIndex = 0;\r
544 for (Index = 0; Index < NumberOfDescriptors; Index++) {\r
545 if (BaseAddress >= MemorySpaceMap[Index].BaseAddress &&\r
546 BaseAddress < MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length) {\r
547 *StartIndex = Index;\r
548 }\r
549 if (BaseAddress + Length - 1 >= MemorySpaceMap[Index].BaseAddress &&\r
550 BaseAddress + Length - 1 < MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length) {\r
551 *EndIndex = Index;\r
552 return EFI_SUCCESS;\r
553 }\r
554 }\r
555 return EFI_NOT_FOUND;\r
556}\r
557\r
558/**\r
559 Sets the attributes for a specified range in Gcd Memory Space Map.\r
560\r
561 This function sets the attributes for a specified range in\r
562 Gcd Memory Space Map.\r
563\r
564 @param MemorySpaceMap Gcd Memory Space Map as array\r
565 @param NumberOfDescriptors Number of descriptors in map\r
566 @param BaseAddress BaseAddress for the range\r
567 @param Length Length for the range\r
568 @param Attributes Attributes to set\r
569\r
570 @retval EFI_SUCCESS Memory attributes set successfully\r
571 @retval EFI_NOT_FOUND The specified range does not exist in Gcd Memory Space\r
572\r
573**/\r
574EFI_STATUS\r
575SetGcdMemorySpaceAttributes (\r
576 IN EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap,\r
577 IN UINTN NumberOfDescriptors,\r
578 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
579 IN UINT64 Length,\r
580 IN UINT64 Attributes\r
581 )\r
582{\r
583 EFI_STATUS Status;\r
584 UINTN Index;\r
585 UINTN StartIndex;\r
586 UINTN EndIndex;\r
587 EFI_PHYSICAL_ADDRESS RegionStart;\r
588 UINT64 RegionLength;\r
589\r
590 //\r
591 // Get all memory descriptors covered by the memory range\r
592 //\r
593 Status = SearchGcdMemorySpaces (\r
594 MemorySpaceMap,\r
595 NumberOfDescriptors,\r
596 BaseAddress,\r
597 Length,\r
598 &StartIndex,\r
599 &EndIndex\r
600 );\r
601 if (EFI_ERROR (Status)) {\r
602 return Status;\r
603 }\r
604\r
605 //\r
606 // Go through all related descriptors and set attributes accordingly\r
607 //\r
608 for (Index = StartIndex; Index <= EndIndex; Index++) {\r
609 if (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeNonExistent) {\r
610 continue;\r
611 }\r
612 //\r
613 // Calculate the start and end address of the overlapping range\r
614 //\r
615 if (BaseAddress >= MemorySpaceMap[Index].BaseAddress) {\r
616 RegionStart = BaseAddress;\r
617 } else {\r
618 RegionStart = MemorySpaceMap[Index].BaseAddress;\r
619 }\r
620 if (BaseAddress + Length - 1 < MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length) {\r
621 RegionLength = BaseAddress + Length - RegionStart;\r
622 } else {\r
623 RegionLength = MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length - RegionStart;\r
624 }\r
625 //\r
626 // Set memory attributes according to MTRR attribute and the original attribute of descriptor\r
627 //\r
628 gDS->SetMemorySpaceAttributes (\r
629 RegionStart,\r
630 RegionLength,\r
631 (MemorySpaceMap[Index].Attributes & ~EFI_MEMORY_CACHETYPE_MASK) | (MemorySpaceMap[Index].Capabilities & Attributes)\r
632 );\r
633 }\r
634\r
635 return EFI_SUCCESS;\r
636}\r
637\r
638\r
639/**\r
640 Refreshes the GCD Memory Space attributes according to MTRRs.\r
641\r
642 This function refreshes the GCD Memory Space attributes according to MTRRs.\r
643\r
644**/\r
645VOID\r
646RefreshGcdMemoryAttributes (\r
647 VOID\r
648 )\r
649{\r
650 EFI_STATUS Status;\r
651 UINTN Index;\r
652 UINTN SubIndex;\r
653 UINT64 RegValue;\r
654 EFI_PHYSICAL_ADDRESS BaseAddress;\r
655 UINT64 Length;\r
656 UINT64 Attributes;\r
657 UINT64 CurrentAttributes;\r
2c4b1bdc 658 UINT8 MtrrType;\r
a47463f2 659 UINTN NumberOfDescriptors;\r
660 EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap;\r
661 UINT64 DefaultAttributes;\r
6640eb36 662 VARIABLE_MTRR VariableMtrr[MTRR_NUMBER_OF_VARIABLE_MTRR];\r
a47463f2 663 MTRR_FIXED_SETTINGS MtrrFixedSettings;\r
3b9be416 664 UINT32 FirmwareVariableMtrrCount;\r
2c4b1bdc 665 UINT8 DefaultMemoryType;\r
3b9be416 666\r
947a573a 667 if (!IsMtrrSupported ()) {\r
668 return;\r
669 }\r
670\r
3b9be416 671 FirmwareVariableMtrrCount = GetFirmwareVariableMtrrCount ();\r
5bdfa4e5 672 ASSERT (FirmwareVariableMtrrCount <= MTRR_NUMBER_OF_VARIABLE_MTRR);\r
a47463f2 673\r
91ec7824 674 mIsFlushingGCD = TRUE;\r
a47463f2 675 MemorySpaceMap = NULL;\r
676\r
677 //\r
678 // Initialize the valid bits mask and valid address mask for MTRRs\r
679 //\r
680 InitializeMtrrMask ();\r
681\r
682 //\r
683 // Get the memory attribute of variable MTRRs\r
684 //\r
685 MtrrGetMemoryAttributeInVariableMtrr (\r
686 mValidMtrrBitsMask,\r
687 mValidMtrrAddressMask,\r
688 VariableMtrr\r
689 );\r
690\r
691 //\r
692 // Get the memory space map from GCD\r
693 //\r
694 Status = gDS->GetMemorySpaceMap (\r
695 &NumberOfDescriptors,\r
696 &MemorySpaceMap\r
697 );\r
698 ASSERT_EFI_ERROR (Status);\r
699\r
2c4b1bdc 700 DefaultMemoryType = (UINT8) MtrrGetDefaultMemoryType ();\r
91ec7824 701 DefaultAttributes = GetMemorySpaceAttributeFromMtrrType (DefaultMemoryType);\r
a47463f2 702\r
703 //\r
704 // Set default attributes to all spaces.\r
705 //\r
706 for (Index = 0; Index < NumberOfDescriptors; Index++) {\r
707 if (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeNonExistent) {\r
708 continue;\r
709 }\r
710 gDS->SetMemorySpaceAttributes (\r
711 MemorySpaceMap[Index].BaseAddress,\r
712 MemorySpaceMap[Index].Length,\r
713 (MemorySpaceMap[Index].Attributes & ~EFI_MEMORY_CACHETYPE_MASK) |\r
714 (MemorySpaceMap[Index].Capabilities & DefaultAttributes)\r
715 );\r
716 }\r
717\r
718 //\r
719 // Go for variable MTRRs with WB attribute\r
720 //\r
3b9be416 721 for (Index = 0; Index < FirmwareVariableMtrrCount; Index++) {\r
a47463f2 722 if (VariableMtrr[Index].Valid &&\r
723 VariableMtrr[Index].Type == MTRR_CACHE_WRITE_BACK) {\r
724 SetGcdMemorySpaceAttributes (\r
725 MemorySpaceMap,\r
726 NumberOfDescriptors,\r
727 VariableMtrr[Index].BaseAddress,\r
728 VariableMtrr[Index].Length,\r
729 EFI_MEMORY_WB\r
730 );\r
731 }\r
732 }\r
91ec7824 733\r
a47463f2 734 //\r
91ec7824 735 // Go for variable MTRRs with the attribute except for WB and UC attributes\r
a47463f2 736 //\r
3b9be416 737 for (Index = 0; Index < FirmwareVariableMtrrCount; Index++) {\r
d4605c23 738 if (VariableMtrr[Index].Valid &&\r
91ec7824 739 VariableMtrr[Index].Type != MTRR_CACHE_WRITE_BACK &&\r
740 VariableMtrr[Index].Type != MTRR_CACHE_UNCACHEABLE) {\r
2c4b1bdc 741 Attributes = GetMemorySpaceAttributeFromMtrrType ((UINT8) VariableMtrr[Index].Type);\r
a47463f2 742 SetGcdMemorySpaceAttributes (\r
743 MemorySpaceMap,\r
744 NumberOfDescriptors,\r
745 VariableMtrr[Index].BaseAddress,\r
746 VariableMtrr[Index].Length,\r
747 Attributes\r
748 );\r
749 }\r
750 }\r
751\r
91ec7824 752 //\r
753 // Go for variable MTRRs with UC attribute\r
754 //\r
755 for (Index = 0; Index < FirmwareVariableMtrrCount; Index++) {\r
756 if (VariableMtrr[Index].Valid &&\r
757 VariableMtrr[Index].Type == MTRR_CACHE_UNCACHEABLE) {\r
758 SetGcdMemorySpaceAttributes (\r
759 MemorySpaceMap,\r
760 NumberOfDescriptors,\r
761 VariableMtrr[Index].BaseAddress,\r
762 VariableMtrr[Index].Length,\r
763 EFI_MEMORY_UC\r
764 );\r
765 }\r
766 }\r
767\r
a47463f2 768 //\r
769 // Go for fixed MTRRs\r
770 //\r
771 Attributes = 0;\r
772 BaseAddress = 0;\r
773 Length = 0;\r
774 MtrrGetFixedMtrr (&MtrrFixedSettings);\r
775 for (Index = 0; Index < MTRR_NUMBER_OF_FIXED_MTRR; Index++) {\r
776 RegValue = MtrrFixedSettings.Mtrr[Index];\r
777 //\r
778 // Check for continuous fixed MTRR sections\r
779 //\r
780 for (SubIndex = 0; SubIndex < 8; SubIndex++) {\r
2c4b1bdc 781 MtrrType = (UINT8) RShiftU64 (RegValue, SubIndex * 8);\r
a47463f2 782 CurrentAttributes = GetMemorySpaceAttributeFromMtrrType (MtrrType);\r
783 if (Length == 0) {\r
784 //\r
785 // A new MTRR attribute begins\r
786 //\r
787 Attributes = CurrentAttributes;\r
788 } else {\r
789 //\r
790 // If fixed MTRR attribute changed, then set memory attribute for previous atrribute\r
791 //\r
792 if (CurrentAttributes != Attributes) {\r
793 SetGcdMemorySpaceAttributes (\r
794 MemorySpaceMap,\r
795 NumberOfDescriptors,\r
796 BaseAddress,\r
797 Length,\r
798 Attributes\r
799 );\r
800 BaseAddress = mFixedMtrrTable[Index].BaseAddress + mFixedMtrrTable[Index].Length * SubIndex;\r
801 Length = 0;\r
802 Attributes = CurrentAttributes;\r
803 }\r
804 }\r
805 Length += mFixedMtrrTable[Index].Length;\r
806 }\r
807 }\r
808 //\r
809 // Handle the last fixed MTRR region\r
810 //\r
811 SetGcdMemorySpaceAttributes (\r
812 MemorySpaceMap,\r
813 NumberOfDescriptors,\r
814 BaseAddress,\r
815 Length,\r
816 Attributes\r
817 );\r
818\r
819 //\r
820 // Free memory space map allocated by GCD service GetMemorySpaceMap ()\r
821 //\r
822 if (MemorySpaceMap != NULL) {\r
823 FreePool (MemorySpaceMap);\r
824 }\r
825\r
826 mIsFlushingGCD = FALSE;\r
827}\r
828\r
a47463f2 829/**\r
830 Initialize Interrupt Descriptor Table for interrupt handling.\r
831\r
832**/\r
a47463f2 833VOID\r
834InitInterruptDescriptorTable (\r
835 VOID\r
836 )\r
837{\r
e41aad15
JF
838 EFI_STATUS Status;\r
839 EFI_VECTOR_HANDOFF_INFO *VectorInfoList;\r
840 EFI_VECTOR_HANDOFF_INFO *VectorInfo;\r
841\r
842 VectorInfo = NULL;\r
843 Status = EfiGetSystemConfigurationTable (&gEfiVectorHandoffTableGuid, (VOID **) &VectorInfoList);\r
844 if (Status == EFI_SUCCESS && VectorInfoList != NULL) {\r
845 VectorInfo = VectorInfoList;\r
a47463f2 846 }\r
e41aad15
JF
847 Status = InitializeCpuInterruptHandlers (VectorInfo);\r
848 ASSERT_EFI_ERROR (Status);\r
a47463f2 849}\r
850\r
851\r
32394027 852/**\r
853 Callback function for idle events.\r
d4605c23 854\r
32394027 855 @param Event Event whose notification function is being invoked.\r
856 @param Context The pointer to the notification function's context,\r
857 which is implementation-dependent.\r
858\r
859**/\r
860VOID\r
861EFIAPI\r
862IdleLoopEventCallback (\r
863 IN EFI_EVENT Event,\r
864 IN VOID *Context\r
865 )\r
866{\r
867 CpuSleep ();\r
868}\r
869\r
870\r
a47463f2 871/**\r
872 Initialize the state information for the CPU Architectural Protocol.\r
873\r
874 @param ImageHandle Image handle this driver.\r
875 @param SystemTable Pointer to the System Table.\r
876\r
877 @retval EFI_SUCCESS Thread can be successfully created\r
878 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure\r
879 @retval EFI_DEVICE_ERROR Cannot create the thread\r
880\r
881**/\r
882EFI_STATUS\r
883EFIAPI\r
884InitializeCpu (\r
885 IN EFI_HANDLE ImageHandle,\r
886 IN EFI_SYSTEM_TABLE *SystemTable\r
887 )\r
888{\r
889 EFI_STATUS Status;\r
32394027 890 EFI_EVENT IdleLoopEvent;\r
a47463f2 891\r
661cab5d 892 InitializeFloatingPointUnits ();\r
893\r
a47463f2 894 //\r
895 // Make sure interrupts are disabled\r
896 //\r
897 DisableInterrupts ();\r
898\r
899 //\r
900 // Init GDT for DXE\r
901 //\r
902 InitGlobalDescriptorTable ();\r
903\r
904 //\r
905 // Setup IDT pointer, IDT and interrupt entry points\r
906 //\r
907 InitInterruptDescriptorTable ();\r
908\r
d4605c23
EB
909 //\r
910 // Enable the local APIC for Virtual Wire Mode.\r
911 //\r
912 ProgramVirtualWireMode ();\r
913\r
a47463f2 914 //\r
915 // Install CPU Architectural Protocol\r
916 //\r
917 Status = gBS->InstallMultipleProtocolInterfaces (\r
918 &mCpuHandle,\r
919 &gEfiCpuArchProtocolGuid, &gCpu,\r
920 NULL\r
921 );\r
922 ASSERT_EFI_ERROR (Status);\r
923\r
924 //\r
925 // Refresh GCD memory space map according to MTRR value.\r
926 //\r
927 RefreshGcdMemoryAttributes ();\r
928\r
32394027 929 //\r
930 // Setup a callback for idle events\r
931 //\r
932 Status = gBS->CreateEventEx (\r
933 EVT_NOTIFY_SIGNAL,\r
934 TPL_NOTIFY,\r
935 IdleLoopEventCallback,\r
936 NULL,\r
937 &gIdleLoopEventGuid,\r
938 &IdleLoopEvent\r
939 );\r
940 ASSERT_EFI_ERROR (Status);\r
941\r
6022e28c
JJ
942 InitializeMpSupport ();\r
943\r
a47463f2 944 return Status;\r
945}\r
946\r