3 * Copyright (c) 2011-2014, ARM Limited. All rights reserved.
5 * This program and the accompanying materials
6 * are licensed and made available under the terms and conditions of the BSD License
7 * which accompanies this distribution. The full text of the license may be found at
8 * http://opensource.org/licenses/bsd-license.php
10 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "ArmGicDxe.h"
16 #include "GicV3/ArmGicV3Lib.h"
18 #define ARM_GIC_DEFAULT_PRIORITY 0x80
20 extern EFI_HARDWARE_INTERRUPT_PROTOCOL gHardwareInterruptV3Protocol
;
22 STATIC UINTN mGicDistributorBase
;
25 Enable interrupt source Source.
27 @param This Instance pointer for this protocol
28 @param Source Hardware source of the interrupt
30 @retval EFI_SUCCESS Source interrupt enabled.
31 @retval EFI_DEVICE_ERROR Hardware could not be programmed.
36 GicV3EnableInterruptSource (
37 IN EFI_HARDWARE_INTERRUPT_PROTOCOL
*This
,
38 IN HARDWARE_INTERRUPT_SOURCE Source
41 if (Source
> mGicNumInterrupts
) {
43 return EFI_UNSUPPORTED
;
46 ArmGicEnableInterrupt (mGicDistributorBase
, Source
);
52 Disable interrupt source Source.
54 @param This Instance pointer for this protocol
55 @param Source Hardware source of the interrupt
57 @retval EFI_SUCCESS Source interrupt disabled.
58 @retval EFI_DEVICE_ERROR Hardware could not be programmed.
63 GicV3DisableInterruptSource (
64 IN EFI_HARDWARE_INTERRUPT_PROTOCOL
*This
,
65 IN HARDWARE_INTERRUPT_SOURCE Source
68 if (Source
> mGicNumInterrupts
) {
70 return EFI_UNSUPPORTED
;
73 ArmGicDisableInterrupt (mGicDistributorBase
, Source
);
79 Return current state of interrupt source Source.
81 @param This Instance pointer for this protocol
82 @param Source Hardware source of the interrupt
83 @param InterruptState TRUE: source enabled, FALSE: source disabled.
85 @retval EFI_SUCCESS InterruptState is valid
86 @retval EFI_DEVICE_ERROR InterruptState is not valid
91 GicV3GetInterruptSourceState (
92 IN EFI_HARDWARE_INTERRUPT_PROTOCOL
*This
,
93 IN HARDWARE_INTERRUPT_SOURCE Source
,
94 IN BOOLEAN
*InterruptState
97 if (Source
> mGicNumInterrupts
) {
99 return EFI_UNSUPPORTED
;
102 *InterruptState
= ArmGicIsInterruptEnabled (mGicDistributorBase
, Source
);
108 Signal to the hardware that the End Of Interrupt state
111 @param This Instance pointer for this protocol
112 @param Source Hardware source of the interrupt
114 @retval EFI_SUCCESS Source interrupt EOI'ed.
115 @retval EFI_DEVICE_ERROR Hardware could not be programmed.
120 GicV3EndOfInterrupt (
121 IN EFI_HARDWARE_INTERRUPT_PROTOCOL
*This
,
122 IN HARDWARE_INTERRUPT_SOURCE Source
125 if (Source
> mGicNumInterrupts
) {
127 return EFI_UNSUPPORTED
;
130 ArmGicV3EndOfInterrupt (Source
);
135 EFI_CPU_INTERRUPT_HANDLER that is called when a processor interrupt occurs.
137 @param InterruptType Defines the type of interrupt or exception that
138 occurred on the processor.This parameter is processor architecture specific.
139 @param SystemContext A pointer to the processor context when
140 the interrupt occurred on the processor.
147 GicV3IrqInterruptHandler (
148 IN EFI_EXCEPTION_TYPE InterruptType
,
149 IN EFI_SYSTEM_CONTEXT SystemContext
153 HARDWARE_INTERRUPT_HANDLER InterruptHandler
;
155 GicInterrupt
= ArmGicV3AcknowledgeInterrupt ();
157 // Special Interrupts (ID1020-ID1023) have an Interrupt ID greater than the
158 // number of interrupt (ie: Spurious interrupt).
159 if ((GicInterrupt
& ARM_GIC_ICCIAR_ACKINTID
) >= mGicNumInterrupts
) {
160 // The special interrupt do not need to be acknowledge
164 InterruptHandler
= gRegisteredInterruptHandlers
[GicInterrupt
];
165 if (InterruptHandler
!= NULL
) {
166 // Call the registered interrupt handler.
167 InterruptHandler (GicInterrupt
, SystemContext
);
169 DEBUG ((EFI_D_ERROR
, "Spurious GIC interrupt: 0x%x\n", GicInterrupt
));
172 GicV3EndOfInterrupt (&gHardwareInterruptV3Protocol
, GicInterrupt
);
176 // The protocol instance produced by this driver
178 EFI_HARDWARE_INTERRUPT_PROTOCOL gHardwareInterruptV3Protocol
= {
179 RegisterInterruptSource
,
180 GicV3EnableInterruptSource
,
181 GicV3DisableInterruptSource
,
182 GicV3GetInterruptSourceState
,
187 Shutdown our hardware
189 DXE Core will disable interrupts and turn off the timer and disable interrupts
190 after all the event handlers have run.
192 @param[in] Event The Event that is being processed
193 @param[in] Context Event Context
197 GicV3ExitBootServicesEvent (
204 // Acknowledge all pending interrupts
205 for (Index
= 0; Index
< mGicNumInterrupts
; Index
++) {
206 GicV3DisableInterruptSource (&gHardwareInterruptV3Protocol
, Index
);
209 for (Index
= 0; Index
< mGicNumInterrupts
; Index
++) {
210 GicV3EndOfInterrupt (&gHardwareInterruptV3Protocol
, Index
);
213 // Disable Gic Interface
214 ArmGicV3DisableInterruptInterface ();
216 // Disable Gic Distributor
217 ArmGicDisableDistributor (mGicDistributorBase
);
221 Initialize the state information for the CPU Architectural Protocol
223 @param ImageHandle of the loaded driver
224 @param SystemTable Pointer to the System Table
226 @retval EFI_SUCCESS Protocol registered
227 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
228 @retval EFI_DEVICE_ERROR Hardware problems
233 IN EFI_HANDLE ImageHandle
,
234 IN EFI_SYSTEM_TABLE
*SystemTable
243 // Make sure the Interrupt Controller Protocol is not already installed in the system.
244 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gHardwareInterruptProtocolGuid
);
246 mGicDistributorBase
= PcdGet32 (PcdGicDistributorBase
);
247 mGicNumInterrupts
= ArmGicGetMaxNumInterrupts (mGicDistributorBase
);
249 for (Index
= 0; Index
< mGicNumInterrupts
; Index
++) {
250 GicV3DisableInterruptSource (&gHardwareInterruptV3Protocol
, Index
);
253 RegOffset
= Index
/ 4;
254 RegShift
= (Index
% 4) * 8;
256 mGicDistributorBase
+ ARM_GIC_ICDIPR
+ (4 * RegOffset
),
258 ARM_GIC_DEFAULT_PRIORITY
<< RegShift
263 // Targets the interrupts to the Primary Cpu
266 // Only Primary CPU will run this code. We can identify our GIC CPU ID by reading
267 // the GIC Distributor Target register. The 8 first GICD_ITARGETSRn are banked to each
268 // connected CPU. These 8 registers hold the CPU targets fields for interrupts 0-31.
269 // More Info in the GIC Specification about "Interrupt Processor Targets Registers"
271 // Read the first Interrupt Processor Targets Register (that corresponds to the 4
273 CpuTarget
= MmioRead32 (mGicDistributorBase
+ ARM_GIC_ICDIPTR
);
275 // The CPU target is a bit field mapping each CPU to a GIC CPU Interface. This value
276 // is 0 when we run on a uniprocessor platform.
277 if (CpuTarget
!= 0) {
278 // The 8 first Interrupt Processor Targets Registers are read-only
279 for (Index
= 8; Index
< (mGicNumInterrupts
/ 4); Index
++) {
280 MmioWrite32 (mGicDistributorBase
+ ARM_GIC_ICDIPTR
+ (Index
* 4), CpuTarget
);
284 // Set binary point reg to 0x7 (no preemption)
285 ArmGicV3SetBinaryPointer (0x7);
287 // Set priority mask reg to 0xff to allow all priorities through
288 ArmGicV3SetPriorityMask (0xff);
290 // Enable gic cpu interface
291 ArmGicV3EnableInterruptInterface ();
293 // Enable gic distributor
294 ArmGicEnableDistributor (mGicDistributorBase
);
296 Status
= InstallAndRegisterInterruptService (
297 &gHardwareInterruptV3Protocol
, GicV3IrqInterruptHandler
, GicV3ExitBootServicesEvent
);