]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Drivers/ArmGic/GicV3/ArmGicV3Dxe.c
ArmPkg/ArmGic: Added GICv3 specific definitions
[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
47 ArmGicEnableInterrupt (mGicDistributorBase, Source);\r
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
74 ArmGicDisableInterrupt (mGicDistributorBase, Source);\r
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
103 *InterruptState = ArmGicIsInterruptEnabled (mGicDistributorBase, Source);\r
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
242 UINT32 CpuTarget;\r
243\r
244 // Make sure the Interrupt Controller Protocol is not already installed in the system.\r
245 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gHardwareInterruptProtocolGuid);\r
246\r
919697ae
OM
247 mGicDistributorBase = PcdGet32 (PcdGicDistributorBase);\r
248 mGicRedistributorsBase = PcdGet32 (PcdGicRedistributorsBase);\r
249 mGicNumInterrupts = ArmGicGetMaxNumInterrupts (mGicDistributorBase);\r
5f81082e
OM
250\r
251 for (Index = 0; Index < mGicNumInterrupts; Index++) {\r
252 GicV3DisableInterruptSource (&gHardwareInterruptV3Protocol, Index);\r
253\r
254 // Set Priority\r
255 RegOffset = Index / 4;\r
256 RegShift = (Index % 4) * 8;\r
257 MmioAndThenOr32 (\r
258 mGicDistributorBase + ARM_GIC_ICDIPR + (4 * RegOffset),\r
259 ~(0xff << RegShift),\r
260 ARM_GIC_DEFAULT_PRIORITY << RegShift\r
261 );\r
262 }\r
263\r
264 //\r
265 // Targets the interrupts to the Primary Cpu\r
266 //\r
267\r
268 // Only Primary CPU will run this code. We can identify our GIC CPU ID by reading\r
269 // the GIC Distributor Target register. The 8 first GICD_ITARGETSRn are banked to each\r
270 // connected CPU. These 8 registers hold the CPU targets fields for interrupts 0-31.\r
271 // More Info in the GIC Specification about "Interrupt Processor Targets Registers"\r
272 //\r
273 // Read the first Interrupt Processor Targets Register (that corresponds to the 4\r
274 // first SGIs)\r
275 CpuTarget = MmioRead32 (mGicDistributorBase + ARM_GIC_ICDIPTR);\r
276\r
277 // The CPU target is a bit field mapping each CPU to a GIC CPU Interface. This value\r
278 // is 0 when we run on a uniprocessor platform.\r
279 if (CpuTarget != 0) {\r
280 // The 8 first Interrupt Processor Targets Registers are read-only\r
281 for (Index = 8; Index < (mGicNumInterrupts / 4); Index++) {\r
282 MmioWrite32 (mGicDistributorBase + ARM_GIC_ICDIPTR + (Index * 4), CpuTarget);\r
283 }\r
284 }\r
285\r
5f81082e
OM
286 // Set binary point reg to 0x7 (no preemption)\r
287 ArmGicV3SetBinaryPointer (0x7);\r
288\r
289 // Set priority mask reg to 0xff to allow all priorities through\r
290 ArmGicV3SetPriorityMask (0xff);\r
291\r
292 // Enable gic cpu interface\r
293 ArmGicV3EnableInterruptInterface ();\r
294\r
295 // Enable gic distributor\r
296 ArmGicEnableDistributor (mGicDistributorBase);\r
297\r
298 Status = InstallAndRegisterInterruptService (\r
299 &gHardwareInterruptV3Protocol, GicV3IrqInterruptHandler, GicV3ExitBootServicesEvent);\r
300\r
301 return Status;\r
302}\r