]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/BaseXApicLib/BaseXApicLib.c
UefiCpuPkg/LocalApicLib: fix feature test for Extended Topology CPUID leaf
[mirror_edk2.git] / UefiCpuPkg / Library / BaseXApicLib / BaseXApicLib.c
CommitLineData
bf73cc4b 1/** @file\r
2 Local APIC Library.\r
3\r
4 This local APIC library instance supports xAPIC mode only.\r
5\r
a742e186 6 Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>\r
bf73cc4b 7 This program and the accompanying materials\r
8 are licensed and made available under the terms and conditions of the BSD License\r
9 which accompanies this distribution. The full text of the license may be found at\r
10 http://opensource.org/licenses/bsd-license.php\r
11\r
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15**/\r
16\r
28a7ddf0 17#include <Register/Cpuid.h>\r
a742e186 18#include <Register/Msr.h>\r
bf73cc4b 19#include <Register/LocalApic.h>\r
20\r
21#include <Library/BaseLib.h>\r
22#include <Library/DebugLib.h>\r
23#include <Library/LocalApicLib.h>\r
24#include <Library/IoLib.h>\r
25#include <Library/TimerLib.h>\r
59d67246 26#include <Library/PcdLib.h>\r
bf73cc4b 27\r
28//\r
29// Library internal functions\r
30//\r
31\r
59d67246
MK
32/**\r
33 Determine if the CPU supports the Local APIC Base Address MSR.\r
34\r
35 @retval TRUE The CPU supports the Local APIC Base Address MSR.\r
36 @retval FALSE The CPU does not support the Local APIC Base Address MSR.\r
37\r
38**/\r
39BOOLEAN\r
40LocalApicBaseAddressMsrSupported (\r
41 VOID\r
42 )\r
43{\r
44 UINT32 RegEax;\r
45 UINTN FamilyId;\r
46 \r
47 AsmCpuid (1, &RegEax, NULL, NULL, NULL);\r
48 FamilyId = BitFieldRead32 (RegEax, 8, 11);\r
49 if (FamilyId == 0x04 || FamilyId == 0x05) {\r
50 //\r
51 // CPUs with a FamilyId of 0x04 or 0x05 do not support the \r
52 // Local APIC Base Address MSR\r
53 //\r
54 return FALSE;\r
55 }\r
56 return TRUE;\r
57}\r
58\r
a66e0c7d 59/**\r
60 Retrieve the base address of local APIC.\r
61\r
62 @return The base address of local APIC.\r
63\r
64**/\r
65UINTN\r
66EFIAPI\r
67GetLocalApicBaseAddress (\r
68 VOID\r
69 )\r
70{\r
a742e186 71 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr;\r
59d67246
MK
72\r
73 if (!LocalApicBaseAddressMsrSupported ()) {\r
74 //\r
75 // If CPU does not support Local APIC Base Address MSR, then retrieve\r
76 // Local APIC Base Address from PCD\r
77 //\r
78 return PcdGet32 (PcdCpuLocalApicBaseAddress);\r
79 }\r
80\r
a742e186 81 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
a66e0c7d 82 \r
a742e186
JF
83 return (UINTN)(LShiftU64 ((UINT64) ApicBaseMsr.Bits.ApicBaseHi, 32)) +\r
84 (((UINTN)ApicBaseMsr.Bits.ApicBase) << 12);\r
a66e0c7d 85}\r
86\r
87/**\r
88 Set the base address of local APIC.\r
89\r
90 If BaseAddress is not aligned on a 4KB boundary, then ASSERT().\r
91\r
92 @param[in] BaseAddress Local APIC base address to be set.\r
93\r
94**/\r
95VOID\r
96EFIAPI\r
97SetLocalApicBaseAddress (\r
98 IN UINTN BaseAddress\r
99 )\r
100{\r
a742e186 101 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr;\r
a66e0c7d 102\r
103 ASSERT ((BaseAddress & (SIZE_4KB - 1)) == 0);\r
104\r
59d67246
MK
105 if (!LocalApicBaseAddressMsrSupported ()) {\r
106 //\r
107 // Ignore set request if the CPU does not support APIC Base Address MSR\r
108 //\r
109 return;\r
110 }\r
111\r
a742e186 112 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
a66e0c7d 113\r
a742e186
JF
114 ApicBaseMsr.Bits.ApicBase = (UINT32) (BaseAddress >> 12);\r
115 ApicBaseMsr.Bits.ApicBaseHi = (UINT32) (RShiftU64((UINT64) BaseAddress, 32));\r
a66e0c7d 116\r
a742e186 117 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);\r
a66e0c7d 118}\r
119\r
bf73cc4b 120/**\r
121 Read from a local APIC register.\r
122\r
123 This function reads from a local APIC register either in xAPIC or x2APIC mode.\r
124 It is required that in xAPIC mode wider registers (64-bit or 256-bit) must be\r
125 accessed using multiple 32-bit loads or stores, so this function only performs\r
126 32-bit read.\r
127\r
128 @param MmioOffset The MMIO offset of the local APIC register in xAPIC mode.\r
129 It must be 16-byte aligned.\r
130\r
131 @return 32-bit Value read from the register.\r
132**/\r
133UINT32\r
134EFIAPI\r
135ReadLocalApicReg (\r
136 IN UINTN MmioOffset\r
137 )\r
138{\r
139 ASSERT ((MmioOffset & 0xf) == 0);\r
140 ASSERT (GetApicMode () == LOCAL_APIC_MODE_XAPIC);\r
141\r
a66e0c7d 142 return MmioRead32 (GetLocalApicBaseAddress() + MmioOffset);\r
bf73cc4b 143}\r
144\r
145/**\r
146 Write to a local APIC register.\r
147\r
148 This function writes to a local APIC register either in xAPIC or x2APIC mode.\r
149 It is required that in xAPIC mode wider registers (64-bit or 256-bit) must be\r
150 accessed using multiple 32-bit loads or stores, so this function only performs\r
151 32-bit write.\r
152\r
153 if the register index is invalid or unsupported in current APIC mode, then ASSERT.\r
154\r
155 @param MmioOffset The MMIO offset of the local APIC register in xAPIC mode.\r
156 It must be 16-byte aligned.\r
157 @param Value Value to be written to the register.\r
158**/\r
159VOID\r
160EFIAPI\r
161WriteLocalApicReg (\r
162 IN UINTN MmioOffset,\r
163 IN UINT32 Value\r
164 )\r
165{\r
166 ASSERT ((MmioOffset & 0xf) == 0);\r
167 ASSERT (GetApicMode () == LOCAL_APIC_MODE_XAPIC);\r
168\r
a66e0c7d 169 MmioWrite32 (GetLocalApicBaseAddress() + MmioOffset, Value);\r
bf73cc4b 170}\r
171\r
172/**\r
173 Send an IPI by writing to ICR.\r
174\r
175 This function returns after the IPI has been accepted by the target processor. \r
176\r
177 @param IcrLow 32-bit value to be written to the low half of ICR.\r
178 @param ApicId APIC ID of the target processor if this IPI is targeted for a specific processor.\r
179**/\r
180VOID\r
181SendIpi (\r
182 IN UINT32 IcrLow,\r
183 IN UINT32 ApicId\r
184 )\r
185{\r
186 LOCAL_APIC_ICR_LOW IcrLowReg;\r
9c71e1e0
JF
187 UINT32 IcrHigh;\r
188 BOOLEAN InterruptState;\r
bf73cc4b 189\r
190 ASSERT (GetApicMode () == LOCAL_APIC_MODE_XAPIC);\r
191 ASSERT (ApicId <= 0xff);\r
192\r
9c71e1e0
JF
193 InterruptState = SaveAndDisableInterrupts ();\r
194\r
195 //\r
196 // Save existing contents of ICR high 32 bits\r
197 //\r
198 IcrHigh = ReadLocalApicReg (XAPIC_ICR_HIGH_OFFSET);\r
199\r
200 //\r
201 // Wait for DeliveryStatus clear in case a previous IPI\r
202 // is still being sent\r
203 //\r
204 do {\r
205 IcrLowReg.Uint32 = ReadLocalApicReg (XAPIC_ICR_LOW_OFFSET);\r
206 } while (IcrLowReg.Bits.DeliveryStatus != 0);\r
207\r
bf73cc4b 208 //\r
209 // For xAPIC, the act of writing to the low doubleword of the ICR causes the IPI to be sent.\r
210 //\r
211 WriteLocalApicReg (XAPIC_ICR_HIGH_OFFSET, ApicId << 24);\r
212 WriteLocalApicReg (XAPIC_ICR_LOW_OFFSET, IcrLow);\r
9c71e1e0
JF
213\r
214 //\r
215 // Wait for DeliveryStatus clear again\r
216 //\r
bf73cc4b 217 do {\r
218 IcrLowReg.Uint32 = ReadLocalApicReg (XAPIC_ICR_LOW_OFFSET);\r
219 } while (IcrLowReg.Bits.DeliveryStatus != 0);\r
9c71e1e0
JF
220\r
221 //\r
222 // And restore old contents of ICR high\r
223 //\r
224 WriteLocalApicReg (XAPIC_ICR_HIGH_OFFSET, IcrHigh);\r
225\r
226 SetInterruptState (InterruptState);\r
227\r
bf73cc4b 228}\r
229\r
230//\r
231// Library API implementation functions\r
232//\r
233\r
234/**\r
235 Get the current local APIC mode.\r
236\r
237 If local APIC is disabled, then ASSERT.\r
238\r
239 @retval LOCAL_APIC_MODE_XAPIC current APIC mode is xAPIC.\r
240 @retval LOCAL_APIC_MODE_X2APIC current APIC mode is x2APIC.\r
241**/\r
242UINTN\r
243EFIAPI\r
244GetApicMode (\r
245 VOID\r
246 )\r
247{\r
248 DEBUG_CODE (\r
249 {\r
a742e186 250 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr;\r
bf73cc4b 251\r
bf73cc4b 252 //\r
59d67246 253 // Check to see if the CPU supports the APIC Base Address MSR \r
bf73cc4b 254 //\r
59d67246 255 if (LocalApicBaseAddressMsrSupported ()) {\r
a742e186 256 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
59d67246
MK
257 //\r
258 // Local APIC should have been enabled\r
259 //\r
a742e186
JF
260 ASSERT (ApicBaseMsr.Bits.EN != 0);\r
261 ASSERT (ApicBaseMsr.Bits.EXTD == 0);\r
59d67246 262 }\r
bf73cc4b 263 }\r
264 );\r
265 return LOCAL_APIC_MODE_XAPIC;\r
266}\r
267\r
268/**\r
269 Set the current local APIC mode.\r
270\r
271 If the specified local APIC mode is not valid, then ASSERT.\r
272 If the specified local APIC mode can't be set as current, then ASSERT.\r
273\r
274 @param ApicMode APIC mode to be set.\r
9c71e1e0
JF
275\r
276 @note This API must not be called from an interrupt handler or SMI handler.\r
277 It may result in unpredictable behavior.\r
bf73cc4b 278**/\r
279VOID\r
280EFIAPI\r
281SetApicMode (\r
282 IN UINTN ApicMode\r
283 )\r
284{\r
285 ASSERT (ApicMode == LOCAL_APIC_MODE_XAPIC);\r
286 ASSERT (GetApicMode () == LOCAL_APIC_MODE_XAPIC);\r
287}\r
288\r
289/**\r
290 Get the initial local APIC ID of the executing processor assigned by hardware upon power on or reset.\r
291\r
6e3e4d70 292 In xAPIC mode, the initial local APIC ID may be different from current APIC ID.\r
bf73cc4b 293 In x2APIC mode, the local APIC ID can't be changed and there is no concept of initial APIC ID. In this case, \r
294 the 32-bit local APIC ID is returned as initial APIC ID.\r
295\r
296 @return 32-bit initial local APIC ID of the executing processor.\r
297**/\r
298UINT32\r
299EFIAPI\r
300GetInitialApicId (\r
301 VOID\r
302 )\r
303{\r
6e3e4d70
JF
304 UINT32 ApicId;\r
305 UINT32 MaxCpuIdIndex;\r
bf73cc4b 306 UINT32 RegEbx;\r
307\r
308 ASSERT (GetApicMode () == LOCAL_APIC_MODE_XAPIC);\r
309\r
6e3e4d70
JF
310 //\r
311 // Get the max index of basic CPUID\r
312 //\r
313 AsmCpuid (CPUID_SIGNATURE, &MaxCpuIdIndex, NULL, NULL, NULL);\r
314\r
315 //\r
316 // If CPUID Leaf B is supported, \r
4af3ae14 317 // And CPUID.0BH:EBX[15:0] reports a non-zero value,\r
6e3e4d70
JF
318 // Then the initial 32-bit APIC ID = CPUID.0BH:EDX\r
319 // Else the initial 8-bit APIC ID = CPUID.1:EBX[31:24]\r
320 //\r
321 if (MaxCpuIdIndex >= CPUID_EXTENDED_TOPOLOGY) {\r
4af3ae14
LE
322 AsmCpuidEx (CPUID_EXTENDED_TOPOLOGY, 0, NULL, &RegEbx, NULL, &ApicId);\r
323 if ((RegEbx & (BIT16 - 1)) != 0) {\r
324 return ApicId;\r
325 }\r
6e3e4d70
JF
326 }\r
327\r
bf73cc4b 328 AsmCpuid (CPUID_VERSION_INFO, NULL, &RegEbx, NULL, NULL);\r
329 return RegEbx >> 24;\r
330}\r
331\r
332/**\r
333 Get the local APIC ID of the executing processor.\r
334\r
335 @return 32-bit local APIC ID of the executing processor.\r
336**/\r
337UINT32\r
338EFIAPI\r
339GetApicId (\r
340 VOID\r
341 )\r
342{\r
343 UINT32 ApicId;\r
344\r
345 ASSERT (GetApicMode () == LOCAL_APIC_MODE_XAPIC);\r
6e3e4d70
JF
346 \r
347 if ((ApicId = GetInitialApicId ()) < 0x100) {\r
348 //\r
349 // If the initial local APIC ID is less 0x100, read APIC ID from\r
350 // XAPIC_ID_OFFSET, otherwise return the initial local APIC ID.\r
351 //\r
352 ApicId = ReadLocalApicReg (XAPIC_ID_OFFSET);\r
353 ApicId >>= 24;\r
354 }\r
bf73cc4b 355 return ApicId;\r
356}\r
357\r
ae40aef1 358/**\r
359 Get the value of the local APIC version register.\r
360\r
361 @return the value of the local APIC version register.\r
362**/\r
363UINT32\r
364EFIAPI\r
365GetApicVersion (\r
366 VOID\r
367 )\r
368{\r
369 return ReadLocalApicReg (XAPIC_VERSION_OFFSET);\r
370}\r
371\r
372/**\r
373 Send a Fixed IPI to a specified target processor.\r
374\r
375 This function returns after the IPI has been accepted by the target processor. \r
376\r
377 @param ApicId The local APIC ID of the target processor.\r
378 @param Vector The vector number of the interrupt being sent.\r
379**/\r
380VOID\r
381EFIAPI\r
382SendFixedIpi (\r
383 IN UINT32 ApicId,\r
384 IN UINT8 Vector\r
385 )\r
386{\r
387 LOCAL_APIC_ICR_LOW IcrLow;\r
388\r
389 IcrLow.Uint32 = 0;\r
390 IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_FIXED;\r
391 IcrLow.Bits.Level = 1;\r
392 IcrLow.Bits.Vector = Vector;\r
393 SendIpi (IcrLow.Uint32, ApicId);\r
394}\r
395\r
396/**\r
397 Send a Fixed IPI to all processors excluding self.\r
398\r
399 This function returns after the IPI has been accepted by the target processors. \r
400\r
401 @param Vector The vector number of the interrupt being sent.\r
402**/\r
403VOID\r
404EFIAPI\r
405SendFixedIpiAllExcludingSelf (\r
406 IN UINT8 Vector\r
407 )\r
408{\r
409 LOCAL_APIC_ICR_LOW IcrLow;\r
410\r
411 IcrLow.Uint32 = 0;\r
412 IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_FIXED;\r
413 IcrLow.Bits.Level = 1;\r
414 IcrLow.Bits.DestinationShorthand = LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF;\r
415 IcrLow.Bits.Vector = Vector;\r
416 SendIpi (IcrLow.Uint32, 0);\r
417}\r
418\r
bf73cc4b 419/**\r
420 Send a SMI IPI to a specified target processor.\r
421\r
422 This function returns after the IPI has been accepted by the target processor. \r
423\r
424 @param ApicId Specify the local APIC ID of the target processor.\r
425**/\r
426VOID\r
427EFIAPI\r
428SendSmiIpi (\r
429 IN UINT32 ApicId\r
430 )\r
431{\r
432 LOCAL_APIC_ICR_LOW IcrLow;\r
433\r
434 IcrLow.Uint32 = 0;\r
435 IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_SMI;\r
436 IcrLow.Bits.Level = 1;\r
437 SendIpi (IcrLow.Uint32, ApicId);\r
438}\r
439\r
440/**\r
441 Send a SMI IPI to all processors excluding self.\r
442\r
443 This function returns after the IPI has been accepted by the target processors. \r
444**/\r
445VOID\r
446EFIAPI\r
447SendSmiIpiAllExcludingSelf (\r
448 VOID\r
449 )\r
450{\r
451 LOCAL_APIC_ICR_LOW IcrLow;\r
452\r
453 IcrLow.Uint32 = 0;\r
454 IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_SMI;\r
455 IcrLow.Bits.Level = 1;\r
456 IcrLow.Bits.DestinationShorthand = LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF;\r
457 SendIpi (IcrLow.Uint32, 0);\r
458}\r
459\r
460/**\r
461 Send an INIT IPI to a specified target processor.\r
462\r
463 This function returns after the IPI has been accepted by the target processor. \r
464\r
465 @param ApicId Specify the local APIC ID of the target processor.\r
466**/\r
467VOID\r
468EFIAPI\r
469SendInitIpi (\r
470 IN UINT32 ApicId\r
471 )\r
472{\r
473 LOCAL_APIC_ICR_LOW IcrLow;\r
474\r
475 IcrLow.Uint32 = 0;\r
476 IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_INIT;\r
477 IcrLow.Bits.Level = 1;\r
478 SendIpi (IcrLow.Uint32, ApicId);\r
479}\r
480\r
481/**\r
482 Send an INIT IPI to all processors excluding self.\r
483\r
484 This function returns after the IPI has been accepted by the target processors. \r
485**/\r
486VOID\r
487EFIAPI\r
488SendInitIpiAllExcludingSelf (\r
489 VOID\r
490 )\r
491{\r
492 LOCAL_APIC_ICR_LOW IcrLow;\r
493\r
494 IcrLow.Uint32 = 0;\r
495 IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_INIT;\r
496 IcrLow.Bits.Level = 1;\r
497 IcrLow.Bits.DestinationShorthand = LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF;\r
498 SendIpi (IcrLow.Uint32, 0);\r
499}\r
500\r
501/**\r
502 Send an INIT-Start-up-Start-up IPI sequence to a specified target processor.\r
503\r
504 This function returns after the IPI has been accepted by the target processor. \r
505\r
506 if StartupRoutine >= 1M, then ASSERT.\r
507 if StartupRoutine is not multiple of 4K, then ASSERT.\r
508\r
509 @param ApicId Specify the local APIC ID of the target processor.\r
510 @param StartupRoutine Points to a start-up routine which is below 1M physical\r
511 address and 4K aligned.\r
512**/\r
513VOID\r
514EFIAPI\r
515SendInitSipiSipi (\r
516 IN UINT32 ApicId,\r
517 IN UINT32 StartupRoutine\r
518 )\r
519{\r
520 LOCAL_APIC_ICR_LOW IcrLow;\r
521\r
522 ASSERT (StartupRoutine < 0x100000);\r
523 ASSERT ((StartupRoutine & 0xfff) == 0);\r
524\r
525 SendInitIpi (ApicId);\r
cf1eb6e6 526 MicroSecondDelay (PcdGet32(PcdCpuInitIpiDelayInMicroSeconds));\r
bf73cc4b 527 IcrLow.Uint32 = 0;\r
528 IcrLow.Bits.Vector = (StartupRoutine >> 12);\r
529 IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_STARTUP;\r
530 IcrLow.Bits.Level = 1;\r
531 SendIpi (IcrLow.Uint32, ApicId);\r
532 MicroSecondDelay (200);\r
533 SendIpi (IcrLow.Uint32, ApicId);\r
534}\r
535\r
536/**\r
537 Send an INIT-Start-up-Start-up IPI sequence to all processors excluding self.\r
538\r
539 This function returns after the IPI has been accepted by the target processors. \r
540\r
541 if StartupRoutine >= 1M, then ASSERT.\r
542 if StartupRoutine is not multiple of 4K, then ASSERT.\r
543\r
544 @param StartupRoutine Points to a start-up routine which is below 1M physical\r
545 address and 4K aligned.\r
546**/\r
547VOID\r
548EFIAPI\r
549SendInitSipiSipiAllExcludingSelf (\r
550 IN UINT32 StartupRoutine\r
551 )\r
552{\r
553 LOCAL_APIC_ICR_LOW IcrLow;\r
554\r
555 ASSERT (StartupRoutine < 0x100000);\r
556 ASSERT ((StartupRoutine & 0xfff) == 0);\r
557\r
558 SendInitIpiAllExcludingSelf ();\r
cf1eb6e6 559 MicroSecondDelay (PcdGet32(PcdCpuInitIpiDelayInMicroSeconds));\r
bf73cc4b 560 IcrLow.Uint32 = 0;\r
561 IcrLow.Bits.Vector = (StartupRoutine >> 12);\r
562 IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_STARTUP;\r
563 IcrLow.Bits.Level = 1;\r
564 IcrLow.Bits.DestinationShorthand = LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF;\r
565 SendIpi (IcrLow.Uint32, 0);\r
566 MicroSecondDelay (200);\r
567 SendIpi (IcrLow.Uint32, 0);\r
568}\r
569\r
14e4ca25
MK
570/**\r
571 Initialize the state of the SoftwareEnable bit in the Local APIC\r
572 Spurious Interrupt Vector register.\r
573\r
574 @param Enable If TRUE, then set SoftwareEnable to 1\r
575 If FALSE, then set SoftwareEnable to 0.\r
576\r
577**/\r
578VOID\r
579EFIAPI\r
580InitializeLocalApicSoftwareEnable (\r
581 IN BOOLEAN Enable\r
582 )\r
583{\r
584 LOCAL_APIC_SVR Svr;\r
585\r
586 //\r
587 // Set local APIC software-enabled bit.\r
588 //\r
589 Svr.Uint32 = ReadLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET);\r
590 if (Enable) {\r
591 if (Svr.Bits.SoftwareEnable == 0) {\r
592 Svr.Bits.SoftwareEnable = 1;\r
593 WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET, Svr.Uint32);\r
594 }\r
595 } else {\r
596 if (Svr.Bits.SoftwareEnable == 1) {\r
597 Svr.Bits.SoftwareEnable = 0;\r
598 WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET, Svr.Uint32);\r
599 }\r
600 }\r
601}\r
602\r
bf73cc4b 603/**\r
604 Programming Virtual Wire Mode.\r
605\r
606 This function programs the local APIC for virtual wire mode following\r
607 the example described in chapter A.3 of the MP 1.4 spec.\r
608\r
609 IOxAPIC is not involved in this type of virtual wire mode.\r
610**/\r
611VOID\r
612EFIAPI\r
613ProgramVirtualWireMode (\r
614 VOID\r
615 )\r
616{\r
617 LOCAL_APIC_SVR Svr;\r
618 LOCAL_APIC_LVT_LINT Lint;\r
619\r
620 //\r
621 // Enable the APIC via SVR and set the spurious interrupt to use Int 00F.\r
622 //\r
623 Svr.Uint32 = ReadLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET);\r
624 Svr.Bits.SpuriousVector = 0xf;\r
625 Svr.Bits.SoftwareEnable = 1;\r
626 WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET, Svr.Uint32);\r
627\r
628 //\r
629 // Program the LINT0 vector entry as ExtInt. Not masked, edge, active high.\r
630 //\r
ae40aef1 631 Lint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT0_OFFSET);\r
bf73cc4b 632 Lint.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_EXTINT;\r
633 Lint.Bits.InputPinPolarity = 0;\r
634 Lint.Bits.TriggerMode = 0;\r
635 Lint.Bits.Mask = 0;\r
ae40aef1 636 WriteLocalApicReg (XAPIC_LVT_LINT0_OFFSET, Lint.Uint32);\r
bf73cc4b 637\r
638 //\r
639 // Program the LINT0 vector entry as NMI. Not masked, edge, active high.\r
640 //\r
ae40aef1 641 Lint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT1_OFFSET);\r
bf73cc4b 642 Lint.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_NMI;\r
643 Lint.Bits.InputPinPolarity = 0;\r
644 Lint.Bits.TriggerMode = 0;\r
645 Lint.Bits.Mask = 0;\r
ae40aef1 646 WriteLocalApicReg (XAPIC_LVT_LINT1_OFFSET, Lint.Uint32);\r
bf73cc4b 647}\r
648\r
b1b8c631 649/**\r
650 Disable LINT0 & LINT1 interrupts.\r
651\r
652 This function sets the mask flag in the LVT LINT0 & LINT1 registers.\r
653**/\r
654VOID\r
655EFIAPI\r
656DisableLvtInterrupts (\r
657 VOID\r
658 )\r
659{\r
660 LOCAL_APIC_LVT_LINT LvtLint;\r
661\r
662 LvtLint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT0_OFFSET);\r
663 LvtLint.Bits.Mask = 1;\r
664 WriteLocalApicReg (XAPIC_LVT_LINT0_OFFSET, LvtLint.Uint32);\r
665\r
666 LvtLint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT1_OFFSET);\r
667 LvtLint.Bits.Mask = 1;\r
668 WriteLocalApicReg (XAPIC_LVT_LINT1_OFFSET, LvtLint.Uint32);\r
669}\r
670\r
bf73cc4b 671/**\r
672 Read the initial count value from the init-count register.\r
673\r
674 @return The initial count value read from the init-count register.\r
675**/\r
676UINT32\r
677EFIAPI\r
678GetApicTimerInitCount (\r
679 VOID\r
680 )\r
681{\r
682 return ReadLocalApicReg (XAPIC_TIMER_INIT_COUNT_OFFSET);\r
683}\r
684\r
685/**\r
686 Read the current count value from the current-count register.\r
687\r
688 @return The current count value read from the current-count register.\r
689**/\r
690UINT32\r
691EFIAPI\r
692GetApicTimerCurrentCount (\r
693 VOID\r
694 )\r
695{\r
696 return ReadLocalApicReg (XAPIC_TIMER_CURRENT_COUNT_OFFSET);\r
697}\r
698\r
699/**\r
700 Initialize the local APIC timer.\r
701\r
702 The local APIC timer is initialized and enabled.\r
703\r
704 @param DivideValue The divide value for the DCR. It is one of 1,2,4,8,16,32,64,128.\r
705 If it is 0, then use the current divide value in the DCR.\r
706 @param InitCount The initial count value.\r
707 @param PeriodicMode If TRUE, timer mode is peridoic. Othewise, timer mode is one-shot.\r
708 @param Vector The timer interrupt vector number.\r
709**/\r
710VOID\r
711EFIAPI\r
712InitializeApicTimer (\r
713 IN UINTN DivideValue,\r
714 IN UINT32 InitCount,\r
715 IN BOOLEAN PeriodicMode,\r
716 IN UINT8 Vector\r
717 )\r
718{\r
bf73cc4b 719 LOCAL_APIC_DCR Dcr;\r
720 LOCAL_APIC_LVT_TIMER LvtTimer;\r
721 UINT32 Divisor;\r
722\r
723 //\r
724 // Ensure local APIC is in software-enabled state.\r
725 //\r
14e4ca25 726 InitializeLocalApicSoftwareEnable (TRUE);\r
bf73cc4b 727\r
728 //\r
729 // Program init-count register.\r
730 //\r
731 WriteLocalApicReg (XAPIC_TIMER_INIT_COUNT_OFFSET, InitCount);\r
732\r
733 if (DivideValue != 0) {\r
734 ASSERT (DivideValue <= 128);\r
735 ASSERT (DivideValue == GetPowerOfTwo32((UINT32)DivideValue));\r
736 Divisor = (UINT32)((HighBitSet32 ((UINT32)DivideValue) - 1) & 0x7);\r
737\r
738 Dcr.Uint32 = ReadLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET);\r
739 Dcr.Bits.DivideValue1 = (Divisor & 0x3);\r
740 Dcr.Bits.DivideValue2 = (Divisor >> 2);\r
741 WriteLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET, Dcr.Uint32); \r
742 }\r
743\r
744 //\r
745 // Enable APIC timer interrupt with specified timer mode.\r
746 //\r
747 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
748 if (PeriodicMode) {\r
749 LvtTimer.Bits.TimerMode = 1;\r
750 } else {\r
751 LvtTimer.Bits.TimerMode = 0;\r
752 }\r
753 LvtTimer.Bits.Mask = 0;\r
754 LvtTimer.Bits.Vector = Vector;\r
755 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET, LvtTimer.Uint32);\r
756}\r
757\r
ae40aef1 758/**\r
759 Get the state of the local APIC timer.\r
760\r
6d72ff7d
HW
761 This function will ASSERT if the local APIC is not software enabled.\r
762\r
ae40aef1 763 @param DivideValue Return the divide value for the DCR. It is one of 1,2,4,8,16,32,64,128.\r
764 @param PeriodicMode Return the timer mode. If TRUE, timer mode is peridoic. Othewise, timer mode is one-shot.\r
765 @param Vector Return the timer interrupt vector number.\r
766**/\r
767VOID\r
768EFIAPI\r
769GetApicTimerState (\r
770 OUT UINTN *DivideValue OPTIONAL,\r
771 OUT BOOLEAN *PeriodicMode OPTIONAL,\r
772 OUT UINT8 *Vector OPTIONAL\r
773 )\r
774{\r
775 UINT32 Divisor;\r
776 LOCAL_APIC_DCR Dcr;\r
777 LOCAL_APIC_LVT_TIMER LvtTimer;\r
778\r
6d72ff7d
HW
779 //\r
780 // Check the APIC Software Enable/Disable bit (bit 8) in Spurious-Interrupt\r
781 // Vector Register.\r
782 // This bit will be 1, if local APIC is software enabled.\r
783 //\r
784 ASSERT ((ReadLocalApicReg(XAPIC_SPURIOUS_VECTOR_OFFSET) & BIT8) != 0);\r
785\r
ae40aef1 786 if (DivideValue != NULL) {\r
787 Dcr.Uint32 = ReadLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET);\r
788 Divisor = Dcr.Bits.DivideValue1 | (Dcr.Bits.DivideValue2 << 2);\r
789 Divisor = (Divisor + 1) & 0x7;\r
790 *DivideValue = ((UINTN)1) << Divisor;\r
791 }\r
792\r
793 if (PeriodicMode != NULL || Vector != NULL) {\r
794 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
795 if (PeriodicMode != NULL) {\r
796 if (LvtTimer.Bits.TimerMode == 1) {\r
797 *PeriodicMode = TRUE;\r
798 } else {\r
799 *PeriodicMode = FALSE;\r
800 }\r
801 }\r
802 if (Vector != NULL) {\r
803 *Vector = (UINT8) LvtTimer.Bits.Vector;\r
804 }\r
805 }\r
806}\r
807\r
bf73cc4b 808/**\r
809 Enable the local APIC timer interrupt.\r
810**/\r
811VOID\r
812EFIAPI\r
813EnableApicTimerInterrupt (\r
814 VOID\r
815 )\r
816{\r
817 LOCAL_APIC_LVT_TIMER LvtTimer;\r
818\r
819 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
820 LvtTimer.Bits.Mask = 0;\r
821 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET, LvtTimer.Uint32);\r
822}\r
823\r
824/**\r
825 Disable the local APIC timer interrupt.\r
826**/\r
827VOID\r
828EFIAPI\r
829DisableApicTimerInterrupt (\r
830 VOID\r
831 )\r
832{\r
833 LOCAL_APIC_LVT_TIMER LvtTimer;\r
834\r
835 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
836 LvtTimer.Bits.Mask = 1;\r
837 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET, LvtTimer.Uint32);\r
838}\r
839\r
840/**\r
841 Get the local APIC timer interrupt state.\r
842\r
843 @retval TRUE The local APIC timer interrupt is enabled.\r
844 @retval FALSE The local APIC timer interrupt is disabled.\r
845**/\r
846BOOLEAN\r
847EFIAPI\r
848GetApicTimerInterruptState (\r
849 VOID\r
850 )\r
851{\r
852 LOCAL_APIC_LVT_TIMER LvtTimer;\r
853\r
854 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
855 return (BOOLEAN)(LvtTimer.Bits.Mask == 0);\r
856}\r
857\r
858/**\r
859 Send EOI to the local APIC.\r
860**/\r
861VOID\r
862EFIAPI\r
863SendApicEoi (\r
864 VOID\r
865 )\r
866{\r
867 WriteLocalApicReg (XAPIC_EOI_OFFSET, 0);\r
868}\r
869\r
5f867ad0 870/**\r
871 Get the 32-bit address that a device should use to send a Message Signaled \r
872 Interrupt (MSI) to the Local APIC of the currently executing processor.\r
873\r
874 @return 32-bit address used to send an MSI to the Local APIC.\r
875**/\r
876UINT32\r
877EFIAPI \r
878GetApicMsiAddress (\r
879 VOID\r
880 )\r
881{\r
882 LOCAL_APIC_MSI_ADDRESS MsiAddress;\r
883\r
884 //\r
885 // Return address for an MSI interrupt to be delivered only to the APIC ID \r
886 // of the currently executing processor.\r
887 //\r
888 MsiAddress.Uint32 = 0;\r
889 MsiAddress.Bits.BaseAddress = 0xFEE;\r
890 MsiAddress.Bits.DestinationId = GetApicId ();\r
891 return MsiAddress.Uint32;\r
892}\r
893 \r
894/**\r
895 Get the 64-bit data value that a device should use to send a Message Signaled \r
896 Interrupt (MSI) to the Local APIC of the currently executing processor.\r
897\r
898 If Vector is not in range 0x10..0xFE, then ASSERT().\r
899 If DeliveryMode is not supported, then ASSERT().\r
900 \r
901 @param Vector The 8-bit interrupt vector associated with the MSI. \r
902 Must be in the range 0x10..0xFE\r
903 @param DeliveryMode A 3-bit value that specifies how the recept of the MSI \r
904 is handled. The only supported values are:\r
905 0: LOCAL_APIC_DELIVERY_MODE_FIXED\r
906 1: LOCAL_APIC_DELIVERY_MODE_LOWEST_PRIORITY\r
907 2: LOCAL_APIC_DELIVERY_MODE_SMI\r
908 4: LOCAL_APIC_DELIVERY_MODE_NMI\r
909 5: LOCAL_APIC_DELIVERY_MODE_INIT\r
910 7: LOCAL_APIC_DELIVERY_MODE_EXTINT\r
911 \r
912 @param LevelTriggered TRUE specifies a level triggered interrupt. \r
913 FALSE specifies an edge triggered interrupt.\r
914 @param AssertionLevel Ignored if LevelTriggered is FALSE.\r
915 TRUE specifies a level triggered interrupt that active \r
916 when the interrupt line is asserted.\r
917 FALSE specifies a level triggered interrupt that active \r
918 when the interrupt line is deasserted.\r
919\r
920 @return 64-bit data value used to send an MSI to the Local APIC.\r
921**/\r
922UINT64\r
923EFIAPI \r
924GetApicMsiValue (\r
925 IN UINT8 Vector,\r
926 IN UINTN DeliveryMode,\r
927 IN BOOLEAN LevelTriggered,\r
928 IN BOOLEAN AssertionLevel\r
929 )\r
930{\r
931 LOCAL_APIC_MSI_DATA MsiData;\r
932\r
933 ASSERT (Vector >= 0x10 && Vector <= 0xFE);\r
934 ASSERT (DeliveryMode < 8 && DeliveryMode != 6 && DeliveryMode != 3);\r
935 \r
936 MsiData.Uint64 = 0;\r
937 MsiData.Bits.Vector = Vector;\r
938 MsiData.Bits.DeliveryMode = (UINT32)DeliveryMode;\r
939 if (LevelTriggered) {\r
940 MsiData.Bits.TriggerMode = 1;\r
941 if (AssertionLevel) {\r
942 MsiData.Bits.Level = 1;\r
943 }\r
944 }\r
945 return MsiData.Uint64;\r
946}\r
73152f19
LD
947\r
948/**\r
949 Get Package ID/Core ID/Thread ID of a processor.\r
950\r
951 The algorithm assumes the target system has symmetry across physical\r
952 package boundaries with respect to the number of logical processors\r
953 per package, number of cores per package.\r
954\r
955 @param[in] InitialApicId Initial APIC ID of the target logical processor.\r
956 @param[out] Package Returns the processor package ID.\r
957 @param[out] Core Returns the processor core ID.\r
958 @param[out] Thread Returns the processor thread ID.\r
959**/\r
960VOID\r
1c8ca9a0 961EFIAPI\r
262128e5 962GetProcessorLocationByApicId (\r
73152f19
LD
963 IN UINT32 InitialApicId,\r
964 OUT UINT32 *Package OPTIONAL,\r
965 OUT UINT32 *Core OPTIONAL,\r
966 OUT UINT32 *Thread OPTIONAL\r
967 )\r
968{\r
969 BOOLEAN TopologyLeafSupported;\r
970 UINTN ThreadBits;\r
971 UINTN CoreBits;\r
972 CPUID_VERSION_INFO_EBX VersionInfoEbx;\r
973 CPUID_VERSION_INFO_EDX VersionInfoEdx;\r
974 CPUID_CACHE_PARAMS_EAX CacheParamsEax;\r
975 CPUID_EXTENDED_TOPOLOGY_EAX ExtendedTopologyEax;\r
976 CPUID_EXTENDED_TOPOLOGY_EBX ExtendedTopologyEbx;\r
977 CPUID_EXTENDED_TOPOLOGY_ECX ExtendedTopologyEcx;\r
978 UINT32 MaxCpuIdIndex;\r
979 UINT32 SubIndex;\r
980 UINTN LevelType;\r
981 UINT32 MaxLogicProcessorsPerPackage;\r
982 UINT32 MaxCoresPerPackage;\r
983\r
984 //\r
985 // Check if the processor is capable of supporting more than one logical processor.\r
986 //\r
987 AsmCpuid(CPUID_VERSION_INFO, NULL, NULL, NULL, &VersionInfoEdx.Uint32);\r
988 if (VersionInfoEdx.Bits.HTT == 0) {\r
989 if (Thread != NULL) {\r
990 *Thread = 0;\r
991 }\r
992 if (Core != NULL) {\r
993 *Core = 0;\r
994 }\r
995 if (Package != NULL) {\r
996 *Package = 0;\r
997 }\r
998 return;\r
999 }\r
1000\r
1001 ThreadBits = 0;\r
1002 CoreBits = 0;\r
1003\r
1004 //\r
1005 // Assume three-level mapping of APIC ID: Package:Core:SMT.\r
1006 //\r
1007 TopologyLeafSupported = FALSE;\r
1008\r
1009 //\r
1010 // Get the max index of basic CPUID\r
1011 //\r
1012 AsmCpuid(CPUID_SIGNATURE, &MaxCpuIdIndex, NULL, NULL, NULL);\r
1013\r
1014 //\r
1015 // If the extended topology enumeration leaf is available, it\r
1016 // is the preferred mechanism for enumerating topology.\r
1017 //\r
1018 if (MaxCpuIdIndex >= CPUID_EXTENDED_TOPOLOGY) {\r
1019 AsmCpuidEx(\r
1020 CPUID_EXTENDED_TOPOLOGY,\r
1021 0,\r
1022 &ExtendedTopologyEax.Uint32,\r
1023 &ExtendedTopologyEbx.Uint32,\r
1024 &ExtendedTopologyEcx.Uint32,\r
1025 NULL\r
1026 );\r
1027 //\r
1028 // If CPUID.(EAX=0BH, ECX=0H):EBX returns zero and maximum input value for\r
1029 // basic CPUID information is greater than 0BH, then CPUID.0BH leaf is not\r
1030 // supported on that processor.\r
1031 //\r
1032 if (ExtendedTopologyEbx.Uint32 != 0) {\r
1033 TopologyLeafSupported = TRUE;\r
1034\r
1035 //\r
1036 // Sub-leaf index 0 (ECX= 0 as input) provides enumeration parameters to extract\r
1037 // the SMT sub-field of x2APIC ID.\r
1038 //\r
1039 LevelType = ExtendedTopologyEcx.Bits.LevelType;\r
1040 ASSERT(LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_SMT);\r
1041 ThreadBits = ExtendedTopologyEax.Bits.ApicIdShift;\r
1042\r
1043 //\r
1044 // Software must not assume any "level type" encoding\r
1045 // value to be related to any sub-leaf index, except sub-leaf 0.\r
1046 //\r
1047 SubIndex = 1;\r
1048 do {\r
1049 AsmCpuidEx(\r
1050 CPUID_EXTENDED_TOPOLOGY,\r
1051 SubIndex,\r
1052 &ExtendedTopologyEax.Uint32,\r
1053 NULL,\r
1054 &ExtendedTopologyEcx.Uint32,\r
1055 NULL\r
1056 );\r
1057 LevelType = ExtendedTopologyEcx.Bits.LevelType;\r
1058 if (LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_CORE) {\r
1059 CoreBits = ExtendedTopologyEax.Bits.ApicIdShift - ThreadBits;\r
1060 break;\r
1061 }\r
1062 SubIndex++;\r
1063 } while (LevelType != CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_INVALID);\r
1064 }\r
1065 }\r
1066\r
1067 if (!TopologyLeafSupported) {\r
1068 AsmCpuid(CPUID_VERSION_INFO, NULL, &VersionInfoEbx.Uint32, NULL, NULL);\r
1069 MaxLogicProcessorsPerPackage = VersionInfoEbx.Bits.MaximumAddressableIdsForLogicalProcessors;\r
1070 if (MaxCpuIdIndex >= CPUID_CACHE_PARAMS) {\r
1071 AsmCpuidEx(CPUID_CACHE_PARAMS, 0, &CacheParamsEax.Uint32, NULL, NULL, NULL);\r
1072 MaxCoresPerPackage = CacheParamsEax.Bits.MaximumAddressableIdsForLogicalProcessors + 1;\r
1073 }\r
1074 else {\r
1075 //\r
1076 // Must be a single-core processor.\r
1077 //\r
1078 MaxCoresPerPackage = 1;\r
1079 }\r
1080\r
1081 ThreadBits = (UINTN)(HighBitSet32(MaxLogicProcessorsPerPackage / MaxCoresPerPackage - 1) + 1);\r
1082 CoreBits = (UINTN)(HighBitSet32(MaxCoresPerPackage - 1) + 1); }\r
1083\r
1084 if (Thread != NULL) {\r
1085 *Thread = InitialApicId & ((1 << ThreadBits) - 1);\r
1086 }\r
1087 if (Core != NULL) {\r
1088 *Core = (InitialApicId >> ThreadBits) & ((1 << CoreBits) - 1);\r
1089 }\r
1090 if (Package != NULL) {\r
1091 *Package = (InitialApicId >> (ThreadBits + CoreBits));\r
1092 }\r
1093}\r