]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/BaseXApicLib/BaseXApicLib.c
UefiCpuPkg: CpuDxe: Update GDT to be consistent with DxeIplPeim
[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
566/**\r
567 Programming Virtual Wire Mode.\r
568\r
569 This function programs the local APIC for virtual wire mode following\r
570 the example described in chapter A.3 of the MP 1.4 spec.\r
571\r
572 IOxAPIC is not involved in this type of virtual wire mode.\r
573**/\r
574VOID\r
575EFIAPI\r
576ProgramVirtualWireMode (\r
577 VOID\r
578 )\r
579{\r
580 LOCAL_APIC_SVR Svr;\r
581 LOCAL_APIC_LVT_LINT Lint;\r
582\r
583 //\r
584 // Enable the APIC via SVR and set the spurious interrupt to use Int 00F.\r
585 //\r
586 Svr.Uint32 = ReadLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET);\r
587 Svr.Bits.SpuriousVector = 0xf;\r
588 Svr.Bits.SoftwareEnable = 1;\r
589 WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET, Svr.Uint32);\r
590\r
591 //\r
592 // Program the LINT0 vector entry as ExtInt. Not masked, edge, active high.\r
593 //\r
ae40aef1 594 Lint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT0_OFFSET);\r
bf73cc4b 595 Lint.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_EXTINT;\r
596 Lint.Bits.InputPinPolarity = 0;\r
597 Lint.Bits.TriggerMode = 0;\r
598 Lint.Bits.Mask = 0;\r
ae40aef1 599 WriteLocalApicReg (XAPIC_LVT_LINT0_OFFSET, Lint.Uint32);\r
bf73cc4b 600\r
601 //\r
602 // Program the LINT0 vector entry as NMI. Not masked, edge, active high.\r
603 //\r
ae40aef1 604 Lint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT1_OFFSET);\r
bf73cc4b 605 Lint.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_NMI;\r
606 Lint.Bits.InputPinPolarity = 0;\r
607 Lint.Bits.TriggerMode = 0;\r
608 Lint.Bits.Mask = 0;\r
ae40aef1 609 WriteLocalApicReg (XAPIC_LVT_LINT1_OFFSET, Lint.Uint32);\r
bf73cc4b 610}\r
611\r
b1b8c631 612/**\r
613 Disable LINT0 & LINT1 interrupts.\r
614\r
615 This function sets the mask flag in the LVT LINT0 & LINT1 registers.\r
616**/\r
617VOID\r
618EFIAPI\r
619DisableLvtInterrupts (\r
620 VOID\r
621 )\r
622{\r
623 LOCAL_APIC_LVT_LINT LvtLint;\r
624\r
625 LvtLint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT0_OFFSET);\r
626 LvtLint.Bits.Mask = 1;\r
627 WriteLocalApicReg (XAPIC_LVT_LINT0_OFFSET, LvtLint.Uint32);\r
628\r
629 LvtLint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT1_OFFSET);\r
630 LvtLint.Bits.Mask = 1;\r
631 WriteLocalApicReg (XAPIC_LVT_LINT1_OFFSET, LvtLint.Uint32);\r
632}\r
633\r
bf73cc4b 634/**\r
635 Read the initial count value from the init-count register.\r
636\r
637 @return The initial count value read from the init-count register.\r
638**/\r
639UINT32\r
640EFIAPI\r
641GetApicTimerInitCount (\r
642 VOID\r
643 )\r
644{\r
645 return ReadLocalApicReg (XAPIC_TIMER_INIT_COUNT_OFFSET);\r
646}\r
647\r
648/**\r
649 Read the current count value from the current-count register.\r
650\r
651 @return The current count value read from the current-count register.\r
652**/\r
653UINT32\r
654EFIAPI\r
655GetApicTimerCurrentCount (\r
656 VOID\r
657 )\r
658{\r
659 return ReadLocalApicReg (XAPIC_TIMER_CURRENT_COUNT_OFFSET);\r
660}\r
661\r
662/**\r
663 Initialize the local APIC timer.\r
664\r
665 The local APIC timer is initialized and enabled.\r
666\r
667 @param DivideValue The divide value for the DCR. It is one of 1,2,4,8,16,32,64,128.\r
668 If it is 0, then use the current divide value in the DCR.\r
669 @param InitCount The initial count value.\r
670 @param PeriodicMode If TRUE, timer mode is peridoic. Othewise, timer mode is one-shot.\r
671 @param Vector The timer interrupt vector number.\r
672**/\r
673VOID\r
674EFIAPI\r
675InitializeApicTimer (\r
676 IN UINTN DivideValue,\r
677 IN UINT32 InitCount,\r
678 IN BOOLEAN PeriodicMode,\r
679 IN UINT8 Vector\r
680 )\r
681{\r
682 LOCAL_APIC_SVR Svr;\r
683 LOCAL_APIC_DCR Dcr;\r
684 LOCAL_APIC_LVT_TIMER LvtTimer;\r
685 UINT32 Divisor;\r
686\r
687 //\r
688 // Ensure local APIC is in software-enabled state.\r
689 //\r
690 Svr.Uint32 = ReadLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET);\r
691 Svr.Bits.SoftwareEnable = 1;\r
692 WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET, Svr.Uint32);\r
693\r
694 //\r
695 // Program init-count register.\r
696 //\r
697 WriteLocalApicReg (XAPIC_TIMER_INIT_COUNT_OFFSET, InitCount);\r
698\r
699 if (DivideValue != 0) {\r
700 ASSERT (DivideValue <= 128);\r
701 ASSERT (DivideValue == GetPowerOfTwo32((UINT32)DivideValue));\r
702 Divisor = (UINT32)((HighBitSet32 ((UINT32)DivideValue) - 1) & 0x7);\r
703\r
704 Dcr.Uint32 = ReadLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET);\r
705 Dcr.Bits.DivideValue1 = (Divisor & 0x3);\r
706 Dcr.Bits.DivideValue2 = (Divisor >> 2);\r
707 WriteLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET, Dcr.Uint32); \r
708 }\r
709\r
710 //\r
711 // Enable APIC timer interrupt with specified timer mode.\r
712 //\r
713 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
714 if (PeriodicMode) {\r
715 LvtTimer.Bits.TimerMode = 1;\r
716 } else {\r
717 LvtTimer.Bits.TimerMode = 0;\r
718 }\r
719 LvtTimer.Bits.Mask = 0;\r
720 LvtTimer.Bits.Vector = Vector;\r
721 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET, LvtTimer.Uint32);\r
722}\r
723\r
ae40aef1 724/**\r
725 Get the state of the local APIC timer.\r
726\r
6d72ff7d
HW
727 This function will ASSERT if the local APIC is not software enabled.\r
728\r
ae40aef1 729 @param DivideValue Return the divide value for the DCR. It is one of 1,2,4,8,16,32,64,128.\r
730 @param PeriodicMode Return the timer mode. If TRUE, timer mode is peridoic. Othewise, timer mode is one-shot.\r
731 @param Vector Return the timer interrupt vector number.\r
732**/\r
733VOID\r
734EFIAPI\r
735GetApicTimerState (\r
736 OUT UINTN *DivideValue OPTIONAL,\r
737 OUT BOOLEAN *PeriodicMode OPTIONAL,\r
738 OUT UINT8 *Vector OPTIONAL\r
739 )\r
740{\r
741 UINT32 Divisor;\r
742 LOCAL_APIC_DCR Dcr;\r
743 LOCAL_APIC_LVT_TIMER LvtTimer;\r
744\r
6d72ff7d
HW
745 //\r
746 // Check the APIC Software Enable/Disable bit (bit 8) in Spurious-Interrupt\r
747 // Vector Register.\r
748 // This bit will be 1, if local APIC is software enabled.\r
749 //\r
750 ASSERT ((ReadLocalApicReg(XAPIC_SPURIOUS_VECTOR_OFFSET) & BIT8) != 0);\r
751\r
ae40aef1 752 if (DivideValue != NULL) {\r
753 Dcr.Uint32 = ReadLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET);\r
754 Divisor = Dcr.Bits.DivideValue1 | (Dcr.Bits.DivideValue2 << 2);\r
755 Divisor = (Divisor + 1) & 0x7;\r
756 *DivideValue = ((UINTN)1) << Divisor;\r
757 }\r
758\r
759 if (PeriodicMode != NULL || Vector != NULL) {\r
760 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
761 if (PeriodicMode != NULL) {\r
762 if (LvtTimer.Bits.TimerMode == 1) {\r
763 *PeriodicMode = TRUE;\r
764 } else {\r
765 *PeriodicMode = FALSE;\r
766 }\r
767 }\r
768 if (Vector != NULL) {\r
769 *Vector = (UINT8) LvtTimer.Bits.Vector;\r
770 }\r
771 }\r
772}\r
773\r
bf73cc4b 774/**\r
775 Enable the local APIC timer interrupt.\r
776**/\r
777VOID\r
778EFIAPI\r
779EnableApicTimerInterrupt (\r
780 VOID\r
781 )\r
782{\r
783 LOCAL_APIC_LVT_TIMER LvtTimer;\r
784\r
785 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
786 LvtTimer.Bits.Mask = 0;\r
787 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET, LvtTimer.Uint32);\r
788}\r
789\r
790/**\r
791 Disable the local APIC timer interrupt.\r
792**/\r
793VOID\r
794EFIAPI\r
795DisableApicTimerInterrupt (\r
796 VOID\r
797 )\r
798{\r
799 LOCAL_APIC_LVT_TIMER LvtTimer;\r
800\r
801 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
802 LvtTimer.Bits.Mask = 1;\r
803 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET, LvtTimer.Uint32);\r
804}\r
805\r
806/**\r
807 Get the local APIC timer interrupt state.\r
808\r
809 @retval TRUE The local APIC timer interrupt is enabled.\r
810 @retval FALSE The local APIC timer interrupt is disabled.\r
811**/\r
812BOOLEAN\r
813EFIAPI\r
814GetApicTimerInterruptState (\r
815 VOID\r
816 )\r
817{\r
818 LOCAL_APIC_LVT_TIMER LvtTimer;\r
819\r
820 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
821 return (BOOLEAN)(LvtTimer.Bits.Mask == 0);\r
822}\r
823\r
824/**\r
825 Send EOI to the local APIC.\r
826**/\r
827VOID\r
828EFIAPI\r
829SendApicEoi (\r
830 VOID\r
831 )\r
832{\r
833 WriteLocalApicReg (XAPIC_EOI_OFFSET, 0);\r
834}\r
835\r
5f867ad0 836/**\r
837 Get the 32-bit address that a device should use to send a Message Signaled \r
838 Interrupt (MSI) to the Local APIC of the currently executing processor.\r
839\r
840 @return 32-bit address used to send an MSI to the Local APIC.\r
841**/\r
842UINT32\r
843EFIAPI \r
844GetApicMsiAddress (\r
845 VOID\r
846 )\r
847{\r
848 LOCAL_APIC_MSI_ADDRESS MsiAddress;\r
849\r
850 //\r
851 // Return address for an MSI interrupt to be delivered only to the APIC ID \r
852 // of the currently executing processor.\r
853 //\r
854 MsiAddress.Uint32 = 0;\r
855 MsiAddress.Bits.BaseAddress = 0xFEE;\r
856 MsiAddress.Bits.DestinationId = GetApicId ();\r
857 return MsiAddress.Uint32;\r
858}\r
859 \r
860/**\r
861 Get the 64-bit data value that a device should use to send a Message Signaled \r
862 Interrupt (MSI) to the Local APIC of the currently executing processor.\r
863\r
864 If Vector is not in range 0x10..0xFE, then ASSERT().\r
865 If DeliveryMode is not supported, then ASSERT().\r
866 \r
867 @param Vector The 8-bit interrupt vector associated with the MSI. \r
868 Must be in the range 0x10..0xFE\r
869 @param DeliveryMode A 3-bit value that specifies how the recept of the MSI \r
870 is handled. The only supported values are:\r
871 0: LOCAL_APIC_DELIVERY_MODE_FIXED\r
872 1: LOCAL_APIC_DELIVERY_MODE_LOWEST_PRIORITY\r
873 2: LOCAL_APIC_DELIVERY_MODE_SMI\r
874 4: LOCAL_APIC_DELIVERY_MODE_NMI\r
875 5: LOCAL_APIC_DELIVERY_MODE_INIT\r
876 7: LOCAL_APIC_DELIVERY_MODE_EXTINT\r
877 \r
878 @param LevelTriggered TRUE specifies a level triggered interrupt. \r
879 FALSE specifies an edge triggered interrupt.\r
880 @param AssertionLevel Ignored if LevelTriggered is FALSE.\r
881 TRUE specifies a level triggered interrupt that active \r
882 when the interrupt line is asserted.\r
883 FALSE specifies a level triggered interrupt that active \r
884 when the interrupt line is deasserted.\r
885\r
886 @return 64-bit data value used to send an MSI to the Local APIC.\r
887**/\r
888UINT64\r
889EFIAPI \r
890GetApicMsiValue (\r
891 IN UINT8 Vector,\r
892 IN UINTN DeliveryMode,\r
893 IN BOOLEAN LevelTriggered,\r
894 IN BOOLEAN AssertionLevel\r
895 )\r
896{\r
897 LOCAL_APIC_MSI_DATA MsiData;\r
898\r
899 ASSERT (Vector >= 0x10 && Vector <= 0xFE);\r
900 ASSERT (DeliveryMode < 8 && DeliveryMode != 6 && DeliveryMode != 3);\r
901 \r
902 MsiData.Uint64 = 0;\r
903 MsiData.Bits.Vector = Vector;\r
904 MsiData.Bits.DeliveryMode = (UINT32)DeliveryMode;\r
905 if (LevelTriggered) {\r
906 MsiData.Bits.TriggerMode = 1;\r
907 if (AssertionLevel) {\r
908 MsiData.Bits.Level = 1;\r
909 }\r
910 }\r
911 return MsiData.Uint64;\r
912}\r