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