3 * Copyright (c) 2011-2015, 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 <Library/ArmGicLib.h>
17 #include "ArmGicDxe.h"
19 #define ARM_GIC_DEFAULT_PRIORITY 0x80
21 extern EFI_HARDWARE_INTERRUPT_PROTOCOL gHardwareInterruptV3Protocol
;
23 STATIC UINTN mGicDistributorBase
;
24 STATIC UINTN mGicRedistributorsBase
;
27 Enable interrupt source Source.
29 @param This Instance pointer for this protocol
30 @param Source Hardware source of the interrupt
32 @retval EFI_SUCCESS Source interrupt enabled.
33 @retval EFI_DEVICE_ERROR Hardware could not be programmed.
38 GicV3EnableInterruptSource (
39 IN EFI_HARDWARE_INTERRUPT_PROTOCOL
*This
,
40 IN HARDWARE_INTERRUPT_SOURCE Source
43 if (Source
> mGicNumInterrupts
) {
45 return EFI_UNSUPPORTED
;
48 ArmGicEnableInterrupt (mGicDistributorBase
, mGicRedistributorsBase
, Source
);
54 Disable interrupt source Source.
56 @param This Instance pointer for this protocol
57 @param Source Hardware source of the interrupt
59 @retval EFI_SUCCESS Source interrupt disabled.
60 @retval EFI_DEVICE_ERROR Hardware could not be programmed.
65 GicV3DisableInterruptSource (
66 IN EFI_HARDWARE_INTERRUPT_PROTOCOL
*This
,
67 IN HARDWARE_INTERRUPT_SOURCE Source
70 if (Source
> mGicNumInterrupts
) {
72 return EFI_UNSUPPORTED
;
75 ArmGicDisableInterrupt (mGicDistributorBase
, mGicRedistributorsBase
, Source
);
81 Return current state of interrupt source Source.
83 @param This Instance pointer for this protocol
84 @param Source Hardware source of the interrupt
85 @param InterruptState TRUE: source enabled, FALSE: source disabled.
87 @retval EFI_SUCCESS InterruptState is valid
88 @retval EFI_DEVICE_ERROR InterruptState is not valid
93 GicV3GetInterruptSourceState (
94 IN EFI_HARDWARE_INTERRUPT_PROTOCOL
*This
,
95 IN HARDWARE_INTERRUPT_SOURCE Source
,
96 IN BOOLEAN
*InterruptState
99 if (Source
> mGicNumInterrupts
) {
101 return EFI_UNSUPPORTED
;
104 *InterruptState
= ArmGicIsInterruptEnabled (mGicDistributorBase
, mGicRedistributorsBase
, Source
);
110 Signal to the hardware that the End Of Interrupt state
113 @param This Instance pointer for this protocol
114 @param Source Hardware source of the interrupt
116 @retval EFI_SUCCESS Source interrupt EOI'ed.
117 @retval EFI_DEVICE_ERROR Hardware could not be programmed.
122 GicV3EndOfInterrupt (
123 IN EFI_HARDWARE_INTERRUPT_PROTOCOL
*This
,
124 IN HARDWARE_INTERRUPT_SOURCE Source
127 if (Source
> mGicNumInterrupts
) {
129 return EFI_UNSUPPORTED
;
132 ArmGicV3EndOfInterrupt (Source
);
137 EFI_CPU_INTERRUPT_HANDLER that is called when a processor interrupt occurs.
139 @param InterruptType Defines the type of interrupt or exception that
140 occurred on the processor.This parameter is processor architecture specific.
141 @param SystemContext A pointer to the processor context when
142 the interrupt occurred on the processor.
149 GicV3IrqInterruptHandler (
150 IN EFI_EXCEPTION_TYPE InterruptType
,
151 IN EFI_SYSTEM_CONTEXT SystemContext
155 HARDWARE_INTERRUPT_HANDLER InterruptHandler
;
157 GicInterrupt
= ArmGicV3AcknowledgeInterrupt ();
159 // Special Interrupts (ID1020-ID1023) have an Interrupt ID greater than the
160 // number of interrupt (ie: Spurious interrupt).
161 if ((GicInterrupt
& ARM_GIC_ICCIAR_ACKINTID
) >= mGicNumInterrupts
) {
162 // The special interrupt do not need to be acknowledge
166 InterruptHandler
= gRegisteredInterruptHandlers
[GicInterrupt
];
167 if (InterruptHandler
!= NULL
) {
168 // Call the registered interrupt handler.
169 InterruptHandler (GicInterrupt
, SystemContext
);
171 DEBUG ((EFI_D_ERROR
, "Spurious GIC interrupt: 0x%x\n", GicInterrupt
));
174 GicV3EndOfInterrupt (&gHardwareInterruptV3Protocol
, GicInterrupt
);
178 // The protocol instance produced by this driver
180 EFI_HARDWARE_INTERRUPT_PROTOCOL gHardwareInterruptV3Protocol
= {
181 RegisterInterruptSource
,
182 GicV3EnableInterruptSource
,
183 GicV3DisableInterruptSource
,
184 GicV3GetInterruptSourceState
,
189 Shutdown our hardware
191 DXE Core will disable interrupts and turn off the timer and disable interrupts
192 after all the event handlers have run.
194 @param[in] Event The Event that is being processed
195 @param[in] Context Event Context
199 GicV3ExitBootServicesEvent (
206 // Acknowledge all pending interrupts
207 for (Index
= 0; Index
< mGicNumInterrupts
; Index
++) {
208 GicV3DisableInterruptSource (&gHardwareInterruptV3Protocol
, Index
);
211 for (Index
= 0; Index
< mGicNumInterrupts
; Index
++) {
212 GicV3EndOfInterrupt (&gHardwareInterruptV3Protocol
, Index
);
215 // Disable Gic Interface
216 ArmGicV3DisableInterruptInterface ();
218 // Disable Gic Distributor
219 ArmGicDisableDistributor (mGicDistributorBase
);
223 Initialize the state information for the CPU Architectural Protocol
225 @param ImageHandle of the loaded driver
226 @param SystemTable Pointer to the System Table
228 @retval EFI_SUCCESS Protocol registered
229 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
230 @retval EFI_DEVICE_ERROR Hardware problems
235 IN EFI_HANDLE ImageHandle
,
236 IN EFI_SYSTEM_TABLE
*SystemTable
246 // Make sure the Interrupt Controller Protocol is not already installed in the system.
247 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gHardwareInterruptProtocolGuid
);
249 mGicDistributorBase
= PcdGet32 (PcdGicDistributorBase
);
250 mGicRedistributorsBase
= PcdGet32 (PcdGicRedistributorsBase
);
251 mGicNumInterrupts
= ArmGicGetMaxNumInterrupts (mGicDistributorBase
);
254 // We will be driving this GIC in native v3 mode, i.e., with Affinity
255 // Routing enabled. So ensure that the ARE bit is set.
257 if (!FeaturePcdGet (PcdArmGicV3WithV2Legacy
)) {
258 MmioOr32 (mGicDistributorBase
+ ARM_GIC_ICDDCR
, ARM_GIC_ICDDCR_ARE
);
261 for (Index
= 0; Index
< mGicNumInterrupts
; Index
++) {
262 GicV3DisableInterruptSource (&gHardwareInterruptV3Protocol
, Index
);
265 RegOffset
= Index
/ 4;
266 RegShift
= (Index
% 4) * 8;
268 mGicDistributorBase
+ ARM_GIC_ICDIPR
+ (4 * RegOffset
),
270 ARM_GIC_DEFAULT_PRIORITY
<< RegShift
275 // Targets the interrupts to the Primary Cpu
278 if (FeaturePcdGet (PcdArmGicV3WithV2Legacy
)) {
279 // Only Primary CPU will run this code. We can identify our GIC CPU ID by reading
280 // the GIC Distributor Target register. The 8 first GICD_ITARGETSRn are banked to each
281 // connected CPU. These 8 registers hold the CPU targets fields for interrupts 0-31.
282 // More Info in the GIC Specification about "Interrupt Processor Targets Registers"
284 // Read the first Interrupt Processor Targets Register (that corresponds to the 4
286 CpuTarget
= MmioRead32 (mGicDistributorBase
+ ARM_GIC_ICDIPTR
);
288 // The CPU target is a bit field mapping each CPU to a GIC CPU Interface. This value
289 // is 0 when we run on a uniprocessor platform.
290 if (CpuTarget
!= 0) {
291 // The 8 first Interrupt Processor Targets Registers are read-only
292 for (Index
= 8; Index
< (mGicNumInterrupts
/ 4); Index
++) {
293 MmioWrite32 (mGicDistributorBase
+ ARM_GIC_ICDIPTR
+ (Index
* 4), CpuTarget
);
297 MpId
= ArmReadMpidr ();
298 CpuTarget
= MpId
& (ARM_CORE_AFF0
| ARM_CORE_AFF1
| ARM_CORE_AFF2
| ARM_CORE_AFF3
);
300 // Route the SPIs to the primary CPU. SPIs start at the INTID 32
301 for (Index
= 0; Index
< (mGicNumInterrupts
- 32); Index
++) {
302 MmioWrite32 (mGicDistributorBase
+ ARM_GICD_IROUTER
+ (Index
* 8), CpuTarget
| ARM_GICD_IROUTER_IRM
);
306 // Set binary point reg to 0x7 (no preemption)
307 ArmGicV3SetBinaryPointer (0x7);
309 // Set priority mask reg to 0xff to allow all priorities through
310 ArmGicV3SetPriorityMask (0xff);
312 // Enable gic cpu interface
313 ArmGicV3EnableInterruptInterface ();
315 // Enable gic distributor
316 ArmGicEnableDistributor (mGicDistributorBase
);
318 Status
= InstallAndRegisterInterruptService (
319 &gHardwareInterruptV3Protocol
, GicV3IrqInterruptHandler
, GicV3ExitBootServicesEvent
);