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