]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Drivers/ArmGic/GicV3/ArmGicV3Dxe.c
OvmfPkg/PciHotPlugInitDxe: translate QEMU's resource reservation hints
[mirror_edk2.git] / ArmPkg / Drivers / ArmGic / GicV3 / ArmGicV3Dxe.c
CommitLineData
5f81082e
OM
1/** @file\r
2*\r
7989300d 3* Copyright (c) 2011-2016, 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
bce29e30
AB
15#include <Library/ArmGicLib.h>\r
16\r
5f81082e 17#include "ArmGicDxe.h"\r
5f81082e
OM
18\r
19#define ARM_GIC_DEFAULT_PRIORITY 0x80\r
20\r
21extern EFI_HARDWARE_INTERRUPT_PROTOCOL gHardwareInterruptV3Protocol;\r
22\r
23STATIC UINTN mGicDistributorBase;\r
919697ae 24STATIC UINTN mGicRedistributorsBase;\r
5f81082e
OM
25\r
26/**\r
27 Enable interrupt source Source.\r
28\r
29 @param This Instance pointer for this protocol\r
30 @param Source Hardware source of the interrupt\r
31\r
32 @retval EFI_SUCCESS Source interrupt enabled.\r
33 @retval EFI_DEVICE_ERROR Hardware could not be programmed.\r
34\r
35**/\r
36EFI_STATUS\r
37EFIAPI\r
38GicV3EnableInterruptSource (\r
39 IN EFI_HARDWARE_INTERRUPT_PROTOCOL *This,\r
40 IN HARDWARE_INTERRUPT_SOURCE Source\r
41 )\r
42{\r
599f004b 43 if (Source >= mGicNumInterrupts) {\r
5f81082e
OM
44 ASSERT(FALSE);\r
45 return EFI_UNSUPPORTED;\r
46 }\r
47\r
41fb5d46 48 ArmGicEnableInterrupt (mGicDistributorBase, mGicRedistributorsBase, Source);\r
5f81082e
OM
49\r
50 return EFI_SUCCESS;\r
51}\r
52\r
53/**\r
54 Disable interrupt source Source.\r
55\r
56 @param This Instance pointer for this protocol\r
57 @param Source Hardware source of the interrupt\r
58\r
59 @retval EFI_SUCCESS Source interrupt disabled.\r
60 @retval EFI_DEVICE_ERROR Hardware could not be programmed.\r
61\r
62**/\r
63EFI_STATUS\r
64EFIAPI\r
65GicV3DisableInterruptSource (\r
66 IN EFI_HARDWARE_INTERRUPT_PROTOCOL *This,\r
67 IN HARDWARE_INTERRUPT_SOURCE Source\r
68 )\r
69{\r
599f004b 70 if (Source >= mGicNumInterrupts) {\r
5f81082e
OM
71 ASSERT(FALSE);\r
72 return EFI_UNSUPPORTED;\r
73 }\r
74\r
41fb5d46 75 ArmGicDisableInterrupt (mGicDistributorBase, mGicRedistributorsBase, Source);\r
5f81082e
OM
76\r
77 return EFI_SUCCESS;\r
78}\r
79\r
80/**\r
81 Return current state of interrupt source Source.\r
82\r
83 @param This Instance pointer for this protocol\r
84 @param Source Hardware source of the interrupt\r
85 @param InterruptState TRUE: source enabled, FALSE: source disabled.\r
86\r
87 @retval EFI_SUCCESS InterruptState is valid\r
88 @retval EFI_DEVICE_ERROR InterruptState is not valid\r
89\r
90**/\r
91EFI_STATUS\r
92EFIAPI\r
93GicV3GetInterruptSourceState (\r
94 IN EFI_HARDWARE_INTERRUPT_PROTOCOL *This,\r
95 IN HARDWARE_INTERRUPT_SOURCE Source,\r
96 IN BOOLEAN *InterruptState\r
97 )\r
98{\r
599f004b 99 if (Source >= mGicNumInterrupts) {\r
5f81082e
OM
100 ASSERT(FALSE);\r
101 return EFI_UNSUPPORTED;\r
102 }\r
103\r
41fb5d46 104 *InterruptState = ArmGicIsInterruptEnabled (mGicDistributorBase, mGicRedistributorsBase, Source);\r
5f81082e
OM
105\r
106 return EFI_SUCCESS;\r
107}\r
108\r
109/**\r
110 Signal to the hardware that the End Of Interrupt state\r
111 has been reached.\r
112\r
113 @param This Instance pointer for this protocol\r
114 @param Source Hardware source of the interrupt\r
115\r
116 @retval EFI_SUCCESS Source interrupt EOI'ed.\r
117 @retval EFI_DEVICE_ERROR Hardware could not be programmed.\r
118\r
119**/\r
120EFI_STATUS\r
121EFIAPI\r
122GicV3EndOfInterrupt (\r
123 IN EFI_HARDWARE_INTERRUPT_PROTOCOL *This,\r
124 IN HARDWARE_INTERRUPT_SOURCE Source\r
125 )\r
126{\r
599f004b 127 if (Source >= mGicNumInterrupts) {\r
5f81082e
OM
128 ASSERT(FALSE);\r
129 return EFI_UNSUPPORTED;\r
130 }\r
131\r
132 ArmGicV3EndOfInterrupt (Source);\r
133 return EFI_SUCCESS;\r
134}\r
135\r
136/**\r
137 EFI_CPU_INTERRUPT_HANDLER that is called when a processor interrupt occurs.\r
138\r
139 @param InterruptType Defines the type of interrupt or exception that\r
140 occurred on the processor.This parameter is processor architecture specific.\r
141 @param SystemContext A pointer to the processor context when\r
142 the interrupt occurred on the processor.\r
143\r
144 @return None\r
145\r
146**/\r
147VOID\r
148EFIAPI\r
149GicV3IrqInterruptHandler (\r
150 IN EFI_EXCEPTION_TYPE InterruptType,\r
151 IN EFI_SYSTEM_CONTEXT SystemContext\r
152 )\r
153{\r
154 UINT32 GicInterrupt;\r
155 HARDWARE_INTERRUPT_HANDLER InterruptHandler;\r
156\r
157 GicInterrupt = ArmGicV3AcknowledgeInterrupt ();\r
158\r
159 // Special Interrupts (ID1020-ID1023) have an Interrupt ID greater than the\r
160 // number of interrupt (ie: Spurious interrupt).\r
161 if ((GicInterrupt & ARM_GIC_ICCIAR_ACKINTID) >= mGicNumInterrupts) {\r
162 // The special interrupt do not need to be acknowledge\r
163 return;\r
164 }\r
165\r
166 InterruptHandler = gRegisteredInterruptHandlers[GicInterrupt];\r
167 if (InterruptHandler != NULL) {\r
168 // Call the registered interrupt handler.\r
169 InterruptHandler (GicInterrupt, SystemContext);\r
170 } else {\r
171 DEBUG ((EFI_D_ERROR, "Spurious GIC interrupt: 0x%x\n", GicInterrupt));\r
7989300d 172 GicV3EndOfInterrupt (&gHardwareInterruptV3Protocol, GicInterrupt);\r
5f81082e 173 }\r
5f81082e
OM
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
8a1f2378
DC
248 mGicDistributorBase = PcdGet64 (PcdGicDistributorBase);\r
249 mGicRedistributorsBase = PcdGet64 (PcdGicRedistributorsBase);\r
919697ae 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
c7fefb69
AB
299 if ((MmioRead32 (mGicDistributorBase + ARM_GIC_ICDDCR) & ARM_GIC_ICDDCR_DS) != 0) {\r
300 //\r
301 // If the Disable Security (DS) control bit is set, we are dealing with a\r
302 // GIC that has only one security state. In this case, let's assume we are\r
303 // executing in non-secure state (which is appropriate for DXE modules)\r
304 // and that no other firmware has performed any configuration on the GIC.\r
305 // This means we need to reconfigure all interrupts to non-secure Group 1\r
306 // first.\r
307 //\r
308 MmioWrite32 (mGicRedistributorsBase + ARM_GICR_CTLR_FRAME_SIZE + ARM_GIC_ICDISR, 0xffffffff);\r
309\r
310 for (Index = 32; Index < mGicNumInterrupts; Index += 32) {\r
311 MmioWrite32 (mGicDistributorBase + ARM_GIC_ICDISR + Index / 8, 0xffffffff);\r
312 }\r
313 }\r
314\r
152ac489
OM
315 // Route the SPIs to the primary CPU. SPIs start at the INTID 32\r
316 for (Index = 0; Index < (mGicNumInterrupts - 32); Index++) {\r
317 MmioWrite32 (mGicDistributorBase + ARM_GICD_IROUTER + (Index * 8), CpuTarget | ARM_GICD_IROUTER_IRM);\r
318 }\r
5f81082e
OM
319 }\r
320\r
5f81082e
OM
321 // Set binary point reg to 0x7 (no preemption)\r
322 ArmGicV3SetBinaryPointer (0x7);\r
323\r
324 // Set priority mask reg to 0xff to allow all priorities through\r
325 ArmGicV3SetPriorityMask (0xff);\r
326\r
327 // Enable gic cpu interface\r
328 ArmGicV3EnableInterruptInterface ();\r
329\r
330 // Enable gic distributor\r
331 ArmGicEnableDistributor (mGicDistributorBase);\r
332\r
333 Status = InstallAndRegisterInterruptService (\r
334 &gHardwareInterruptV3Protocol, GicV3IrqInterruptHandler, GicV3ExitBootServicesEvent);\r
335\r
336 return Status;\r
337}\r