]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/PciHotPlugInitDxe/PciHotPlugInit.c
MdeModulePkg/Include: Add IOMMU protocol definition.
[mirror_edk2.git] / OvmfPkg / PciHotPlugInitDxe / PciHotPlugInit.c
CommitLineData
8aba40b7
LE
1/** @file\r
2 This driver implements EFI_PCI_HOT_PLUG_INIT_PROTOCOL, providing the PCI bus\r
3 driver with resource padding information, for PCIe hotplug purposes.\r
4\r
5 Copyright (C) 2016, Red Hat, Inc.\r
6\r
7 This program and the accompanying materials are licensed and made available\r
8 under the terms and conditions of the BSD License which accompanies this\r
9 distribution. The full text of the license may be found at\r
10 http://opensource.org/licenses/bsd-license.php\r
11\r
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r
13 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14**/\r
15\r
16#include <IndustryStandard/Acpi10.h>\r
17\r
18#include <Library/DebugLib.h>\r
19#include <Library/DevicePathLib.h>\r
20#include <Library/MemoryAllocationLib.h>\r
21#include <Library/UefiBootServicesTableLib.h>\r
22\r
23#include <Protocol/PciHotPlugInit.h>\r
24#include <Protocol/PciRootBridgeIo.h>\r
25\r
26//\r
27// The protocol interface this driver produces.\r
28//\r
29// Refer to 12.6 "PCI Hot Plug PCI Initialization Protocol" in the Platform\r
30// Init 1.4a Spec, Volume 5.\r
31//\r
32STATIC EFI_PCI_HOT_PLUG_INIT_PROTOCOL mPciHotPlugInit;\r
33\r
34\r
35//\r
36// Resource padding template for the GetResourcePadding() protocol member\r
37// function.\r
38//\r
39// Refer to Table 8 "ACPI 2.0 & 3.0 QWORD Address Space Descriptor Usage" in\r
40// the Platform Init 1.4a Spec, Volume 5.\r
41//\r
42// This structure is interpreted by the ApplyResourcePadding() function in the\r
43// edk2 PCI Bus UEFI_DRIVER.\r
44//\r
45#pragma pack (1)\r
46typedef struct {\r
47 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR MmioPadding;\r
48 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR IoPadding;\r
49 EFI_ACPI_END_TAG_DESCRIPTOR EndDesc;\r
50} RESOURCE_PADDING;\r
51#pragma pack ()\r
52\r
53STATIC CONST RESOURCE_PADDING mPadding = {\r
54 //\r
55 // MmioPadding\r
56 //\r
57 {\r
58 ACPI_ADDRESS_SPACE_DESCRIPTOR, // Desc\r
59 (UINT16)( // Len\r
60 sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) -\r
61 OFFSET_OF (\r
62 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR,\r
63 ResType\r
64 )\r
65 ),\r
66 ACPI_ADDRESS_SPACE_TYPE_MEM, // ResType\r
67 0, // GenFlag:\r
68 // ignored\r
69 0, // SpecificFlag:\r
70 // non-prefetchable\r
71 64, // AddrSpaceGranularity:\r
72 // reserve 64-bit aperture\r
73 0, // AddrRangeMin:\r
74 // ignored\r
75 SIZE_2MB - 1, // AddrRangeMax:\r
76 // align at 2MB\r
77 0, // AddrTranslationOffset:\r
78 // ignored\r
79 SIZE_2MB // AddrLen:\r
80 // 2MB padding\r
81 },\r
82\r
83 //\r
84 // IoPadding\r
85 //\r
86 {\r
87 ACPI_ADDRESS_SPACE_DESCRIPTOR, // Desc\r
88 (UINT16)( // Len\r
89 sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) -\r
90 OFFSET_OF (\r
91 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR,\r
92 ResType\r
93 )\r
94 ),\r
95 ACPI_ADDRESS_SPACE_TYPE_IO,// ResType\r
96 0, // GenFlag:\r
97 // ignored\r
98 0, // SpecificFlag:\r
99 // ignored\r
100 0, // AddrSpaceGranularity:\r
101 // ignored\r
102 0, // AddrRangeMin:\r
103 // ignored\r
104 512 - 1, // AddrRangeMax:\r
105 // align at 512 IO ports\r
106 0, // AddrTranslationOffset:\r
107 // ignored\r
108 512 // AddrLen:\r
109 // 512 IO ports\r
110 },\r
111\r
112 //\r
113 // EndDesc\r
114 //\r
115 {\r
116 ACPI_END_TAG_DESCRIPTOR, // Desc\r
117 0 // Checksum: to be ignored\r
118 }\r
119};\r
120\r
121\r
122/**\r
123 Returns a list of root Hot Plug Controllers (HPCs) that require\r
124 initialization during the boot process.\r
125\r
126 This procedure returns a list of root HPCs. The PCI bus driver must\r
127 initialize these controllers during the boot process. The PCI bus driver may\r
128 or may not be able to detect these HPCs. If the platform includes a\r
129 PCI-to-CardBus bridge, it can be included in this list if it requires\r
130 initialization. The HpcList must be self consistent. An HPC cannot control\r
131 any of its parent buses. Only one HPC can control a PCI bus. Because this\r
132 list includes only root HPCs, no HPC in the list can be a child of another\r
133 HPC. This policy must be enforced by the EFI_PCI_HOT_PLUG_INIT_PROTOCOL.\r
134 The PCI bus driver may not check for such invalid conditions. The callee\r
135 allocates the buffer HpcList\r
136\r
137 @param[in] This Pointer to the EFI_PCI_HOT_PLUG_INIT_PROTOCOL\r
138 instance.\r
139 @param[out] HpcCount The number of root HPCs that were returned.\r
140 @param[out] HpcList The list of root HPCs. HpcCount defines the number of\r
141 elements in this list.\r
142\r
143 @retval EFI_SUCCESS HpcList was returned.\r
144 @retval EFI_OUT_OF_RESOURCES HpcList was not returned due to insufficient\r
145 resources.\r
146 @retval EFI_INVALID_PARAMETER HpcCount is NULL or HpcList is NULL.\r
147**/\r
148STATIC\r
149EFI_STATUS\r
150EFIAPI\r
151GetRootHpcList (\r
152 IN EFI_PCI_HOT_PLUG_INIT_PROTOCOL *This,\r
153 OUT UINTN *HpcCount,\r
154 OUT EFI_HPC_LOCATION **HpcList\r
155 )\r
156{\r
157 if (HpcCount == NULL || HpcList == NULL) {\r
158 return EFI_INVALID_PARAMETER;\r
159 }\r
160\r
161 //\r
162 // There are no top-level (i.e., un-enumerable) hot-plug controllers in QEMU\r
163 // that would require special initialization.\r
164 //\r
165 *HpcCount = 0;\r
166 *HpcList = NULL;\r
167 return EFI_SUCCESS;\r
168}\r
169\r
170\r
171/**\r
172 Initializes one root Hot Plug Controller (HPC). This process may causes\r
173 initialization of its subordinate buses.\r
174\r
175 This function initializes the specified HPC. At the end of initialization,\r
176 the hot-plug slots or sockets (controlled by this HPC) are powered and are\r
177 connected to the bus. All the necessary registers in the HPC are set up. For\r
178 a Standard (PCI) Hot Plug Controller (SHPC), the registers that must be set\r
179 up are defined in the PCI Standard Hot Plug Controller and Subsystem\r
180 Specification.\r
181\r
182 @param[in] This Pointer to the EFI_PCI_HOT_PLUG_INIT_PROTOCOL\r
183 instance.\r
184 @param[in] HpcDevicePath The device path to the HPC that is being\r
185 initialized.\r
186 @param[in] HpcPciAddress The address of the HPC function on the PCI bus.\r
187 @param[in] Event The event that should be signaled when the HPC\r
188 initialization is complete. Set to NULL if the\r
189 caller wants to wait until the entire\r
190 initialization process is complete.\r
191 @param[out] HpcState The state of the HPC hardware. The state is\r
192 EFI_HPC_STATE_INITIALIZED or\r
193 EFI_HPC_STATE_ENABLED.\r
194\r
195 @retval EFI_SUCCESS If Event is NULL, the specific HPC was\r
196 successfully initialized. If Event is not\r
197 NULL, Event will be signaled at a later time\r
198 when initialization is complete.\r
199 @retval EFI_UNSUPPORTED This instance of\r
200 EFI_PCI_HOT_PLUG_INIT_PROTOCOL does not\r
201 support the specified HPC.\r
202 @retval EFI_OUT_OF_RESOURCES Initialization failed due to insufficient\r
203 resources.\r
204 @retval EFI_INVALID_PARAMETER HpcState is NULL.\r
205**/\r
206STATIC\r
207EFI_STATUS\r
208EFIAPI\r
209InitializeRootHpc (\r
210 IN EFI_PCI_HOT_PLUG_INIT_PROTOCOL *This,\r
211 IN EFI_DEVICE_PATH_PROTOCOL *HpcDevicePath,\r
212 IN UINT64 HpcPciAddress,\r
213 IN EFI_EVENT Event, OPTIONAL\r
214 OUT EFI_HPC_STATE *HpcState\r
215 )\r
216{\r
217 //\r
218 // This function should never be called, due to the information returned by\r
219 // GetRootHpcList().\r
220 //\r
221 ASSERT (FALSE);\r
222\r
223 if (HpcState == NULL) {\r
224 return EFI_INVALID_PARAMETER;\r
225 }\r
226 return EFI_UNSUPPORTED;\r
227}\r
228\r
229\r
230/**\r
231 Returns the resource padding that is required by the PCI bus that is\r
232 controlled by the specified Hot Plug Controller (HPC).\r
233\r
234 This function returns the resource padding that is required by the PCI bus\r
235 that is controlled by the specified HPC. This member function is called for\r
236 all the root HPCs and nonroot HPCs that are detected by the PCI bus\r
237 enumerator. This function will be called before PCI resource allocation is\r
238 completed. This function must be called after all the root HPCs, with the\r
239 possible exception of a PCI-to-CardBus bridge, have completed\r
240 initialization.\r
241\r
242 @param[in] This Pointer to the EFI_PCI_HOT_PLUG_INIT_PROTOCOL\r
243 instance.\r
244 @param[in] HpcDevicePath The device path to the HPC.\r
245 @param[in] HpcPciAddress The address of the HPC function on the PCI bus.\r
246 @param[in] HpcState The state of the HPC hardware.\r
247 @param[out] Padding The amount of resource padding that is required\r
248 by the PCI bus under the control of the specified\r
249 HPC.\r
250 @param[out] Attributes Describes how padding is accounted for. The\r
251 padding is returned in the form of ACPI 2.0\r
252 resource descriptors.\r
253\r
254 @retval EFI_SUCCESS The resource padding was successfully\r
255 returned.\r
256 @retval EFI_UNSUPPORTED This instance of the\r
257 EFI_PCI_HOT_PLUG_INIT_PROTOCOL does not\r
258 support the specified HPC.\r
259 @retval EFI_NOT_READY This function was called before HPC\r
260 initialization is complete.\r
261 @retval EFI_INVALID_PARAMETER HpcState or Padding or Attributes is NULL.\r
262 @retval EFI_OUT_OF_RESOURCES ACPI 2.0 resource descriptors for Padding\r
263 cannot be allocated due to insufficient\r
264 resources.\r
265**/\r
266STATIC\r
267EFI_STATUS\r
268EFIAPI\r
269GetResourcePadding (\r
270 IN EFI_PCI_HOT_PLUG_INIT_PROTOCOL *This,\r
271 IN EFI_DEVICE_PATH_PROTOCOL *HpcDevicePath,\r
272 IN UINT64 HpcPciAddress,\r
273 OUT EFI_HPC_STATE *HpcState,\r
274 OUT VOID **Padding,\r
275 OUT EFI_HPC_PADDING_ATTRIBUTES *Attributes\r
276 )\r
277{\r
278 DEBUG_CODE (\r
279 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS *Address;\r
280 CHAR16 *DevicePathString;\r
281\r
282 Address = (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS *)&HpcPciAddress;\r
283 DevicePathString = ConvertDevicePathToText (HpcDevicePath, FALSE, FALSE);\r
284\r
285 DEBUG ((EFI_D_VERBOSE, "%a: Address=%02x:%02x.%x DevicePath=%s\n",\r
286 __FUNCTION__, Address->Bus, Address->Device, Address->Function,\r
287 (DevicePathString == NULL) ? L"<unavailable>" : DevicePathString));\r
288\r
289 if (DevicePathString != NULL) {\r
290 FreePool (DevicePathString);\r
291 }\r
292 );\r
293\r
294 if (HpcState == NULL || Padding == NULL || Attributes == NULL) {\r
295 return EFI_INVALID_PARAMETER;\r
296 }\r
297\r
298 *Padding = AllocateCopyPool (sizeof mPadding, &mPadding);\r
299 if (*Padding == NULL) {\r
300 return EFI_OUT_OF_RESOURCES;\r
301 }\r
302\r
303 //\r
304 // Resource padding is required.\r
305 //\r
306 *HpcState = EFI_HPC_STATE_INITIALIZED | EFI_HPC_STATE_ENABLED;\r
307\r
308 //\r
309 // The padding should be applied at PCI bus level, and considered by upstream\r
310 // bridges, recursively.\r
311 //\r
312 *Attributes = EfiPaddingPciBus;\r
313 return EFI_SUCCESS;\r
314}\r
315\r
316\r
317/**\r
318 Entry point for this driver.\r
319\r
320 @param[in] ImageHandle Image handle of this driver.\r
321 @param[in] SystemTable Pointer to SystemTable.\r
322\r
323 @retval EFI_SUCESS Driver has loaded successfully.\r
324 @return Error codes from lower level functions.\r
325\r
326**/\r
327EFI_STATUS\r
328EFIAPI\r
329DriverInitialize (\r
330 IN EFI_HANDLE ImageHandle,\r
331 IN EFI_SYSTEM_TABLE *SystemTable\r
332 )\r
333{\r
334 EFI_STATUS Status;\r
335\r
336 mPciHotPlugInit.GetRootHpcList = GetRootHpcList;\r
337 mPciHotPlugInit.InitializeRootHpc = InitializeRootHpc;\r
338 mPciHotPlugInit.GetResourcePadding = GetResourcePadding;\r
339 Status = gBS->InstallMultipleProtocolInterfaces (&ImageHandle,\r
340 &gEfiPciHotPlugInitProtocolGuid, &mPciHotPlugInit, NULL);\r
341 return Status;\r
342}\r