]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Drivers/ArmGic/GicV3/ArmGicV3Dxe.c
ShellPkg: prevent Close call when Open failed
[mirror_edk2.git] / ArmPkg / Drivers / ArmGic / GicV3 / ArmGicV3Dxe.c
CommitLineData
5f81082e
OM
1/** @file\r
2*\r
919697ae 3* Copyright (c) 2011-2015, ARM Limited. All rights reserved.\r
5f81082e
OM
4*\r
5* This program and the accompanying materials\r
6* are licensed and made available under the terms and conditions of the BSD License\r
7* which accompanies this distribution. The full text of the license may be found at\r
8* http://opensource.org/licenses/bsd-license.php\r
9*\r
10* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12*\r
13**/\r
14\r
15#include "ArmGicDxe.h"\r
16#include "GicV3/ArmGicV3Lib.h"\r
17\r
18#define ARM_GIC_DEFAULT_PRIORITY 0x80\r
19\r
20extern EFI_HARDWARE_INTERRUPT_PROTOCOL gHardwareInterruptV3Protocol;\r
21\r
22STATIC UINTN mGicDistributorBase;\r
919697ae 23STATIC UINTN mGicRedistributorsBase;\r
5f81082e
OM
24\r
25/**\r
26 Enable interrupt source Source.\r
27\r
28 @param This Instance pointer for this protocol\r
29 @param Source Hardware source of the interrupt\r
30\r
31 @retval EFI_SUCCESS Source interrupt enabled.\r
32 @retval EFI_DEVICE_ERROR Hardware could not be programmed.\r
33\r
34**/\r
35EFI_STATUS\r
36EFIAPI\r
37GicV3EnableInterruptSource (\r
38 IN EFI_HARDWARE_INTERRUPT_PROTOCOL *This,\r
39 IN HARDWARE_INTERRUPT_SOURCE Source\r
40 )\r
41{\r
42 if (Source > mGicNumInterrupts) {\r
43 ASSERT(FALSE);\r
44 return EFI_UNSUPPORTED;\r
45 }\r
46\r
41fb5d46 47 ArmGicEnableInterrupt (mGicDistributorBase, mGicRedistributorsBase, Source);\r
5f81082e
OM
48\r
49 return EFI_SUCCESS;\r
50}\r
51\r
52/**\r
53 Disable interrupt source Source.\r
54\r
55 @param This Instance pointer for this protocol\r
56 @param Source Hardware source of the interrupt\r
57\r
58 @retval EFI_SUCCESS Source interrupt disabled.\r
59 @retval EFI_DEVICE_ERROR Hardware could not be programmed.\r
60\r
61**/\r
62EFI_STATUS\r
63EFIAPI\r
64GicV3DisableInterruptSource (\r
65 IN EFI_HARDWARE_INTERRUPT_PROTOCOL *This,\r
66 IN HARDWARE_INTERRUPT_SOURCE Source\r
67 )\r
68{\r
69 if (Source > mGicNumInterrupts) {\r
70 ASSERT(FALSE);\r
71 return EFI_UNSUPPORTED;\r
72 }\r
73\r
41fb5d46 74 ArmGicDisableInterrupt (mGicDistributorBase, mGicRedistributorsBase, Source);\r
5f81082e
OM
75\r
76 return EFI_SUCCESS;\r
77}\r
78\r
79/**\r
80 Return current state of interrupt source Source.\r
81\r
82 @param This Instance pointer for this protocol\r
83 @param Source Hardware source of the interrupt\r
84 @param InterruptState TRUE: source enabled, FALSE: source disabled.\r
85\r
86 @retval EFI_SUCCESS InterruptState is valid\r
87 @retval EFI_DEVICE_ERROR InterruptState is not valid\r
88\r
89**/\r
90EFI_STATUS\r
91EFIAPI\r
92GicV3GetInterruptSourceState (\r
93 IN EFI_HARDWARE_INTERRUPT_PROTOCOL *This,\r
94 IN HARDWARE_INTERRUPT_SOURCE Source,\r
95 IN BOOLEAN *InterruptState\r
96 )\r
97{\r
98 if (Source > mGicNumInterrupts) {\r
99 ASSERT(FALSE);\r
100 return EFI_UNSUPPORTED;\r
101 }\r
102\r
41fb5d46 103 *InterruptState = ArmGicIsInterruptEnabled (mGicDistributorBase, mGicRedistributorsBase, Source);\r
5f81082e
OM
104\r
105 return EFI_SUCCESS;\r
106}\r
107\r
108/**\r
109 Signal to the hardware that the End Of Interrupt state\r
110 has been reached.\r
111\r
112 @param This Instance pointer for this protocol\r
113 @param Source Hardware source of the interrupt\r
114\r
115 @retval EFI_SUCCESS Source interrupt EOI'ed.\r
116 @retval EFI_DEVICE_ERROR Hardware could not be programmed.\r
117\r
118**/\r
119EFI_STATUS\r
120EFIAPI\r
121GicV3EndOfInterrupt (\r
122 IN EFI_HARDWARE_INTERRUPT_PROTOCOL *This,\r
123 IN HARDWARE_INTERRUPT_SOURCE Source\r
124 )\r
125{\r
126 if (Source > mGicNumInterrupts) {\r
127 ASSERT(FALSE);\r
128 return EFI_UNSUPPORTED;\r
129 }\r
130\r
131 ArmGicV3EndOfInterrupt (Source);\r
132 return EFI_SUCCESS;\r
133}\r
134\r
135/**\r
136 EFI_CPU_INTERRUPT_HANDLER that is called when a processor interrupt occurs.\r
137\r
138 @param InterruptType Defines the type of interrupt or exception that\r
139 occurred on the processor.This parameter is processor architecture specific.\r
140 @param SystemContext A pointer to the processor context when\r
141 the interrupt occurred on the processor.\r
142\r
143 @return None\r
144\r
145**/\r
146VOID\r
147EFIAPI\r
148GicV3IrqInterruptHandler (\r
149 IN EFI_EXCEPTION_TYPE InterruptType,\r
150 IN EFI_SYSTEM_CONTEXT SystemContext\r
151 )\r
152{\r
153 UINT32 GicInterrupt;\r
154 HARDWARE_INTERRUPT_HANDLER InterruptHandler;\r
155\r
156 GicInterrupt = ArmGicV3AcknowledgeInterrupt ();\r
157\r
158 // Special Interrupts (ID1020-ID1023) have an Interrupt ID greater than the\r
159 // number of interrupt (ie: Spurious interrupt).\r
160 if ((GicInterrupt & ARM_GIC_ICCIAR_ACKINTID) >= mGicNumInterrupts) {\r
161 // The special interrupt do not need to be acknowledge\r
162 return;\r
163 }\r
164\r
165 InterruptHandler = gRegisteredInterruptHandlers[GicInterrupt];\r
166 if (InterruptHandler != NULL) {\r
167 // Call the registered interrupt handler.\r
168 InterruptHandler (GicInterrupt, SystemContext);\r
169 } else {\r
170 DEBUG ((EFI_D_ERROR, "Spurious GIC interrupt: 0x%x\n", GicInterrupt));\r
171 }\r
172\r
173 GicV3EndOfInterrupt (&gHardwareInterruptV3Protocol, GicInterrupt);\r
174}\r
175\r
176//\r
177// The protocol instance produced by this driver\r
178//\r
179EFI_HARDWARE_INTERRUPT_PROTOCOL gHardwareInterruptV3Protocol = {\r
180 RegisterInterruptSource,\r
181 GicV3EnableInterruptSource,\r
182 GicV3DisableInterruptSource,\r
183 GicV3GetInterruptSourceState,\r
184 GicV3EndOfInterrupt\r
185};\r
186\r
187/**\r
188 Shutdown our hardware\r
189\r
190 DXE Core will disable interrupts and turn off the timer and disable interrupts\r
191 after all the event handlers have run.\r
192\r
193 @param[in] Event The Event that is being processed\r
194 @param[in] Context Event Context\r
195**/\r
196VOID\r
197EFIAPI\r
198GicV3ExitBootServicesEvent (\r
199 IN EFI_EVENT Event,\r
200 IN VOID *Context\r
201 )\r
202{\r
203 UINTN Index;\r
204\r
205 // Acknowledge all pending interrupts\r
206 for (Index = 0; Index < mGicNumInterrupts; Index++) {\r
207 GicV3DisableInterruptSource (&gHardwareInterruptV3Protocol, Index);\r
208 }\r
209\r
210 for (Index = 0; Index < mGicNumInterrupts; Index++) {\r
211 GicV3EndOfInterrupt (&gHardwareInterruptV3Protocol, Index);\r
212 }\r
213\r
214 // Disable Gic Interface\r
215 ArmGicV3DisableInterruptInterface ();\r
216\r
217 // Disable Gic Distributor\r
218 ArmGicDisableDistributor (mGicDistributorBase);\r
219}\r
220\r
221/**\r
222 Initialize the state information for the CPU Architectural Protocol\r
223\r
224 @param ImageHandle of the loaded driver\r
225 @param SystemTable Pointer to the System Table\r
226\r
227 @retval EFI_SUCCESS Protocol registered\r
228 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure\r
229 @retval EFI_DEVICE_ERROR Hardware problems\r
230\r
231**/\r
232EFI_STATUS\r
233GicV3DxeInitialize (\r
234 IN EFI_HANDLE ImageHandle,\r
235 IN EFI_SYSTEM_TABLE *SystemTable\r
236 )\r
237{\r
238 EFI_STATUS Status;\r
239 UINTN Index;\r
240 UINT32 RegOffset;\r
241 UINTN RegShift;\r
41fb5d46
OM
242 UINT64 CpuTarget;\r
243 UINT64 MpId;\r
5f81082e
OM
244\r
245 // Make sure the Interrupt Controller Protocol is not already installed in the system.\r
246 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gHardwareInterruptProtocolGuid);\r
247\r
919697ae
OM
248 mGicDistributorBase = PcdGet32 (PcdGicDistributorBase);\r
249 mGicRedistributorsBase = PcdGet32 (PcdGicRedistributorsBase);\r
250 mGicNumInterrupts = ArmGicGetMaxNumInterrupts (mGicDistributorBase);\r
5f81082e 251\r
f6d46e29
AB
252 //\r
253 // We will be driving this GIC in native v3 mode, i.e., with Affinity\r
254 // Routing enabled. So ensure that the ARE bit is set.\r
255 //\r
256 if (!FeaturePcdGet (PcdArmGicV3WithV2Legacy)) {\r
257 MmioOr32 (mGicDistributorBase + ARM_GIC_ICDDCR, ARM_GIC_ICDDCR_ARE);\r
258 }\r
259\r
5f81082e
OM
260 for (Index = 0; Index < mGicNumInterrupts; Index++) {\r
261 GicV3DisableInterruptSource (&gHardwareInterruptV3Protocol, Index);\r
262\r
263 // Set Priority\r
264 RegOffset = Index / 4;\r
265 RegShift = (Index % 4) * 8;\r
266 MmioAndThenOr32 (\r
267 mGicDistributorBase + ARM_GIC_ICDIPR + (4 * RegOffset),\r
268 ~(0xff << RegShift),\r
269 ARM_GIC_DEFAULT_PRIORITY << RegShift\r
270 );\r
271 }\r
272\r
273 //\r
274 // Targets the interrupts to the Primary Cpu\r
275 //\r
276\r
152ac489
OM
277 if (FeaturePcdGet (PcdArmGicV3WithV2Legacy)) {\r
278 // Only Primary CPU will run this code. We can identify our GIC CPU ID by reading\r
279 // the GIC Distributor Target register. The 8 first GICD_ITARGETSRn are banked to each\r
280 // connected CPU. These 8 registers hold the CPU targets fields for interrupts 0-31.\r
281 // More Info in the GIC Specification about "Interrupt Processor Targets Registers"\r
282 //\r
283 // Read the first Interrupt Processor Targets Register (that corresponds to the 4\r
284 // first SGIs)\r
285 CpuTarget = MmioRead32 (mGicDistributorBase + ARM_GIC_ICDIPTR);\r
286\r
287 // The CPU target is a bit field mapping each CPU to a GIC CPU Interface. This value\r
288 // is 0 when we run on a uniprocessor platform.\r
289 if (CpuTarget != 0) {\r
290 // The 8 first Interrupt Processor Targets Registers are read-only\r
291 for (Index = 8; Index < (mGicNumInterrupts / 4); Index++) {\r
292 MmioWrite32 (mGicDistributorBase + ARM_GIC_ICDIPTR + (Index * 4), CpuTarget);\r
293 }\r
294 }\r
295 } else {\r
296 MpId = ArmReadMpidr ();\r
297 CpuTarget = MpId & (ARM_CORE_AFF0 | ARM_CORE_AFF1 | ARM_CORE_AFF2 | ARM_CORE_AFF3);\r
41fb5d46 298\r
152ac489
OM
299 // Route the SPIs to the primary CPU. SPIs start at the INTID 32\r
300 for (Index = 0; Index < (mGicNumInterrupts - 32); Index++) {\r
301 MmioWrite32 (mGicDistributorBase + ARM_GICD_IROUTER + (Index * 8), CpuTarget | ARM_GICD_IROUTER_IRM);\r
302 }\r
5f81082e
OM
303 }\r
304\r
5f81082e
OM
305 // Set binary point reg to 0x7 (no preemption)\r
306 ArmGicV3SetBinaryPointer (0x7);\r
307\r
308 // Set priority mask reg to 0xff to allow all priorities through\r
309 ArmGicV3SetPriorityMask (0xff);\r
310\r
311 // Enable gic cpu interface\r
312 ArmGicV3EnableInterruptInterface ();\r
313\r
314 // Enable gic distributor\r
315 ArmGicEnableDistributor (mGicDistributorBase);\r
316\r
317 Status = InstallAndRegisterInterruptService (\r
318 &gHardwareInterruptV3Protocol, GicV3IrqInterruptHandler, GicV3ExitBootServicesEvent);\r
319\r
320 return Status;\r
321}\r