]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/PciHotPlugInitDxe/PciHotPlugInit.c
OvmfPkg/PciHotPlugInitDxe: add helper functions for setting up paddings
[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
4776d5cb 18#include <Library/BaseLib.h>\r
a9803247 19#include <Library/BaseMemoryLib.h>\r
8aba40b7
LE
20#include <Library/DebugLib.h>\r
21#include <Library/DevicePathLib.h>\r
22#include <Library/MemoryAllocationLib.h>\r
23#include <Library/UefiBootServicesTableLib.h>\r
24\r
25#include <Protocol/PciHotPlugInit.h>\r
26#include <Protocol/PciRootBridgeIo.h>\r
27\r
28//\r
29// The protocol interface this driver produces.\r
30//\r
31// Refer to 12.6 "PCI Hot Plug PCI Initialization Protocol" in the Platform\r
32// Init 1.4a Spec, Volume 5.\r
33//\r
34STATIC EFI_PCI_HOT_PLUG_INIT_PROTOCOL mPciHotPlugInit;\r
35\r
36\r
37//\r
38// Resource padding template for the GetResourcePadding() protocol member\r
39// function.\r
40//\r
41// Refer to Table 8 "ACPI 2.0 & 3.0 QWORD Address Space Descriptor Usage" in\r
42// the Platform Init 1.4a Spec, Volume 5.\r
43//\r
44// This structure is interpreted by the ApplyResourcePadding() function in the\r
45// edk2 PCI Bus UEFI_DRIVER.\r
46//\r
a9803247
LE
47// We can request padding for at most four resource types, each of which is\r
48// optional, independently of the others:\r
49// (a) bus numbers,\r
50// (b) IO space,\r
51// (c) non-prefetchable MMIO space (32-bit only),\r
52// (d) prefetchable MMIO space (either 32-bit or 64-bit, never both).\r
53//\r
8aba40b7
LE
54#pragma pack (1)\r
55typedef struct {\r
a9803247 56 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR Padding[4];\r
8aba40b7
LE
57 EFI_ACPI_END_TAG_DESCRIPTOR EndDesc;\r
58} RESOURCE_PADDING;\r
59#pragma pack ()\r
60\r
a9803247
LE
61\r
62/**\r
63 Initialize a RESOURCE_PADDING object.\r
64\r
65 @param[out] ResourcePadding The caller-allocated RESOURCE_PADDING object to\r
66 initialize.\r
67**/\r
68STATIC\r
69VOID\r
70InitializeResourcePadding (\r
71 OUT RESOURCE_PADDING *ResourcePadding\r
72 )\r
73{\r
74 UINTN Index;\r
75\r
76 ZeroMem (ResourcePadding, sizeof *ResourcePadding);\r
8aba40b7
LE
77\r
78 //\r
a9803247 79 // Fill in the Padding fields that don't vary across resource types.\r
8aba40b7 80 //\r
a9803247
LE
81 for (Index = 0; Index < ARRAY_SIZE (ResourcePadding->Padding); ++Index) {\r
82 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptor;\r
83\r
84 Descriptor = ResourcePadding->Padding + Index;\r
85 Descriptor->Desc = ACPI_ADDRESS_SPACE_DESCRIPTOR;\r
86 Descriptor->Len = (UINT16)(\r
87 sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) -\r
88 OFFSET_OF (\r
89 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR,\r
90 ResType\r
91 )\r
92 );\r
93 }\r
8aba40b7
LE
94\r
95 //\r
a9803247 96 // Fill in the End Tag.\r
8aba40b7 97 //\r
a9803247
LE
98 ResourcePadding->EndDesc.Desc = ACPI_END_TAG_DESCRIPTOR;\r
99}\r
8aba40b7
LE
100\r
101\r
4776d5cb
LE
102/**\r
103 Set up a descriptor entry for reserving IO space.\r
104\r
105 @param[in,out] Descriptor The descriptor to configure. The caller shall have\r
106 initialized Descriptor earlier, with\r
107 InitializeResourcePadding().\r
108\r
109 @param[in] SizeExponent The size and natural alignment of the reservation\r
110 are determined by raising two to this power.\r
111**/\r
112STATIC\r
113VOID\r
114SetIoPadding (\r
115 IN OUT EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptor,\r
116 IN UINTN SizeExponent\r
117 )\r
118{\r
119 Descriptor->ResType = ACPI_ADDRESS_SPACE_TYPE_IO;\r
120 Descriptor->AddrLen = LShiftU64 (1, SizeExponent);\r
121 Descriptor->AddrRangeMax = Descriptor->AddrLen - 1;\r
122}\r
123\r
124\r
125/**\r
126 Set up a descriptor entry for reserving MMIO space.\r
127\r
128 @param[in,out] Descriptor The descriptor to configure. The caller shall\r
129 have initialized Descriptor earlier, with\r
130 InitializeResourcePadding().\r
131\r
132 @param[in] Prefetchable TRUE if the descriptor should reserve\r
133 prefetchable MMIO space. Pass FALSE for\r
134 reserving non-prefetchable MMIO space.\r
135\r
136 @param[in] ThirtyTwoBitOnly TRUE if the reservation should be limited to\r
137 32-bit address space. FALSE if the reservation\r
138 can be satisfied from 64-bit address space.\r
139 ThirtyTwoBitOnly is ignored if Prefetchable is\r
140 FALSE; in that case ThirtyTwoBitOnly is always\r
141 considered TRUE.\r
142\r
143 @param[in] SizeExponent The size and natural alignment of the\r
144 reservation are determined by raising two to\r
145 this power.\r
146**/\r
147STATIC\r
148VOID\r
149SetMmioPadding (\r
150 IN OUT EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptor,\r
151 IN BOOLEAN Prefetchable,\r
152 IN BOOLEAN ThirtyTwoBitOnly,\r
153 IN UINTN SizeExponent\r
154 )\r
155{\r
156 Descriptor->ResType = ACPI_ADDRESS_SPACE_TYPE_MEM;\r
157 if (Prefetchable) {\r
158 Descriptor->SpecificFlag =\r
159 EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE;\r
160 Descriptor->AddrSpaceGranularity = ThirtyTwoBitOnly ? 32 : 64;\r
161 } else {\r
162 Descriptor->SpecificFlag =\r
163 EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_NON_CACHEABLE;\r
164 Descriptor->AddrSpaceGranularity = 32;\r
165 }\r
166 Descriptor->AddrLen = LShiftU64 (1, SizeExponent);\r
167 Descriptor->AddrRangeMax = Descriptor->AddrLen - 1;\r
168}\r
169\r
170\r
171/**\r
172 Round up a positive 32-bit value to the next whole power of two, and return\r
173 the bit position of the highest bit set in the result. Equivalent to\r
174 ceil(log2(x)).\r
175\r
176 @param[in] Operand The 32-bit operand to evaluate.\r
177\r
178 @retval -1 Operand is zero.\r
179\r
180 @retval -1 Operand is positive, not a whole power of two, and rounding it\r
181 up to the next power of two does not fit into 32 bits.\r
182\r
183 @retval 0..31 Otherwise, return ceil(log2(Value)).\r
184**/\r
185STATIC\r
186INTN\r
187HighBitSetRoundUp32 (\r
188 IN UINT32 Operand\r
189 )\r
190{\r
191 INTN HighBit;\r
192\r
193 HighBit = HighBitSet32 (Operand);\r
194 if (HighBit == -1) {\r
195 //\r
196 // Operand is zero.\r
197 //\r
198 return HighBit;\r
199 }\r
200 if ((Operand & (Operand - 1)) != 0) {\r
201 //\r
202 // Operand is not a whole power of two.\r
203 //\r
204 ++HighBit;\r
205 }\r
206 return (HighBit < 32) ? HighBit : -1;\r
207}\r
208\r
209\r
210/**\r
211 Round up a positive 64-bit value to the next whole power of two, and return\r
212 the bit position of the highest bit set in the result. Equivalent to\r
213 ceil(log2(x)).\r
214\r
215 @param[in] Operand The 64-bit operand to evaluate.\r
216\r
217 @retval -1 Operand is zero.\r
218\r
219 @retval -1 Operand is positive, not a whole power of two, and rounding it\r
220 up to the next power of two does not fit into 64 bits.\r
221\r
222 @retval 0..63 Otherwise, return ceil(log2(Value)).\r
223**/\r
224STATIC\r
225INTN\r
226HighBitSetRoundUp64 (\r
227 IN UINT64 Operand\r
228 )\r
229{\r
230 INTN HighBit;\r
231\r
232 HighBit = HighBitSet64 (Operand);\r
233 if (HighBit == -1) {\r
234 //\r
235 // Operand is zero.\r
236 //\r
237 return HighBit;\r
238 }\r
239 if ((Operand & (Operand - 1)) != 0) {\r
240 //\r
241 // Operand is not a whole power of two.\r
242 //\r
243 ++HighBit;\r
244 }\r
245 return (HighBit < 64) ? HighBit : -1;\r
246}\r
247\r
248\r
8aba40b7
LE
249/**\r
250 Returns a list of root Hot Plug Controllers (HPCs) that require\r
251 initialization during the boot process.\r
252\r
253 This procedure returns a list of root HPCs. The PCI bus driver must\r
254 initialize these controllers during the boot process. The PCI bus driver may\r
255 or may not be able to detect these HPCs. If the platform includes a\r
256 PCI-to-CardBus bridge, it can be included in this list if it requires\r
257 initialization. The HpcList must be self consistent. An HPC cannot control\r
258 any of its parent buses. Only one HPC can control a PCI bus. Because this\r
259 list includes only root HPCs, no HPC in the list can be a child of another\r
260 HPC. This policy must be enforced by the EFI_PCI_HOT_PLUG_INIT_PROTOCOL.\r
261 The PCI bus driver may not check for such invalid conditions. The callee\r
262 allocates the buffer HpcList\r
263\r
264 @param[in] This Pointer to the EFI_PCI_HOT_PLUG_INIT_PROTOCOL\r
265 instance.\r
266 @param[out] HpcCount The number of root HPCs that were returned.\r
267 @param[out] HpcList The list of root HPCs. HpcCount defines the number of\r
268 elements in this list.\r
269\r
270 @retval EFI_SUCCESS HpcList was returned.\r
271 @retval EFI_OUT_OF_RESOURCES HpcList was not returned due to insufficient\r
272 resources.\r
273 @retval EFI_INVALID_PARAMETER HpcCount is NULL or HpcList is NULL.\r
274**/\r
275STATIC\r
276EFI_STATUS\r
277EFIAPI\r
278GetRootHpcList (\r
279 IN EFI_PCI_HOT_PLUG_INIT_PROTOCOL *This,\r
280 OUT UINTN *HpcCount,\r
281 OUT EFI_HPC_LOCATION **HpcList\r
282 )\r
283{\r
284 if (HpcCount == NULL || HpcList == NULL) {\r
285 return EFI_INVALID_PARAMETER;\r
286 }\r
287\r
288 //\r
289 // There are no top-level (i.e., un-enumerable) hot-plug controllers in QEMU\r
290 // that would require special initialization.\r
291 //\r
292 *HpcCount = 0;\r
293 *HpcList = NULL;\r
294 return EFI_SUCCESS;\r
295}\r
296\r
297\r
298/**\r
299 Initializes one root Hot Plug Controller (HPC). This process may causes\r
300 initialization of its subordinate buses.\r
301\r
302 This function initializes the specified HPC. At the end of initialization,\r
303 the hot-plug slots or sockets (controlled by this HPC) are powered and are\r
304 connected to the bus. All the necessary registers in the HPC are set up. For\r
305 a Standard (PCI) Hot Plug Controller (SHPC), the registers that must be set\r
306 up are defined in the PCI Standard Hot Plug Controller and Subsystem\r
307 Specification.\r
308\r
309 @param[in] This Pointer to the EFI_PCI_HOT_PLUG_INIT_PROTOCOL\r
310 instance.\r
311 @param[in] HpcDevicePath The device path to the HPC that is being\r
312 initialized.\r
313 @param[in] HpcPciAddress The address of the HPC function on the PCI bus.\r
314 @param[in] Event The event that should be signaled when the HPC\r
315 initialization is complete. Set to NULL if the\r
316 caller wants to wait until the entire\r
317 initialization process is complete.\r
318 @param[out] HpcState The state of the HPC hardware. The state is\r
319 EFI_HPC_STATE_INITIALIZED or\r
320 EFI_HPC_STATE_ENABLED.\r
321\r
322 @retval EFI_SUCCESS If Event is NULL, the specific HPC was\r
323 successfully initialized. If Event is not\r
324 NULL, Event will be signaled at a later time\r
325 when initialization is complete.\r
326 @retval EFI_UNSUPPORTED This instance of\r
327 EFI_PCI_HOT_PLUG_INIT_PROTOCOL does not\r
328 support the specified HPC.\r
329 @retval EFI_OUT_OF_RESOURCES Initialization failed due to insufficient\r
330 resources.\r
331 @retval EFI_INVALID_PARAMETER HpcState is NULL.\r
332**/\r
333STATIC\r
334EFI_STATUS\r
335EFIAPI\r
336InitializeRootHpc (\r
337 IN EFI_PCI_HOT_PLUG_INIT_PROTOCOL *This,\r
338 IN EFI_DEVICE_PATH_PROTOCOL *HpcDevicePath,\r
339 IN UINT64 HpcPciAddress,\r
340 IN EFI_EVENT Event, OPTIONAL\r
341 OUT EFI_HPC_STATE *HpcState\r
342 )\r
343{\r
344 //\r
345 // This function should never be called, due to the information returned by\r
346 // GetRootHpcList().\r
347 //\r
348 ASSERT (FALSE);\r
349\r
350 if (HpcState == NULL) {\r
351 return EFI_INVALID_PARAMETER;\r
352 }\r
353 return EFI_UNSUPPORTED;\r
354}\r
355\r
356\r
357/**\r
358 Returns the resource padding that is required by the PCI bus that is\r
359 controlled by the specified Hot Plug Controller (HPC).\r
360\r
361 This function returns the resource padding that is required by the PCI bus\r
362 that is controlled by the specified HPC. This member function is called for\r
363 all the root HPCs and nonroot HPCs that are detected by the PCI bus\r
364 enumerator. This function will be called before PCI resource allocation is\r
365 completed. This function must be called after all the root HPCs, with the\r
366 possible exception of a PCI-to-CardBus bridge, have completed\r
367 initialization.\r
368\r
369 @param[in] This Pointer to the EFI_PCI_HOT_PLUG_INIT_PROTOCOL\r
370 instance.\r
371 @param[in] HpcDevicePath The device path to the HPC.\r
372 @param[in] HpcPciAddress The address of the HPC function on the PCI bus.\r
373 @param[in] HpcState The state of the HPC hardware.\r
374 @param[out] Padding The amount of resource padding that is required\r
375 by the PCI bus under the control of the specified\r
376 HPC.\r
377 @param[out] Attributes Describes how padding is accounted for. The\r
378 padding is returned in the form of ACPI 2.0\r
379 resource descriptors.\r
380\r
381 @retval EFI_SUCCESS The resource padding was successfully\r
382 returned.\r
383 @retval EFI_UNSUPPORTED This instance of the\r
384 EFI_PCI_HOT_PLUG_INIT_PROTOCOL does not\r
385 support the specified HPC.\r
386 @retval EFI_NOT_READY This function was called before HPC\r
387 initialization is complete.\r
388 @retval EFI_INVALID_PARAMETER HpcState or Padding or Attributes is NULL.\r
389 @retval EFI_OUT_OF_RESOURCES ACPI 2.0 resource descriptors for Padding\r
390 cannot be allocated due to insufficient\r
391 resources.\r
392**/\r
393STATIC\r
394EFI_STATUS\r
395EFIAPI\r
396GetResourcePadding (\r
397 IN EFI_PCI_HOT_PLUG_INIT_PROTOCOL *This,\r
398 IN EFI_DEVICE_PATH_PROTOCOL *HpcDevicePath,\r
399 IN UINT64 HpcPciAddress,\r
400 OUT EFI_HPC_STATE *HpcState,\r
401 OUT VOID **Padding,\r
402 OUT EFI_HPC_PADDING_ATTRIBUTES *Attributes\r
403 )\r
404{\r
a9803247
LE
405 BOOLEAN DefaultIo;\r
406 BOOLEAN DefaultMmio;\r
407 RESOURCE_PADDING ReservationRequest;\r
408 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *FirstResource;\r
409\r
8aba40b7
LE
410 DEBUG_CODE (\r
411 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS *Address;\r
412 CHAR16 *DevicePathString;\r
413\r
414 Address = (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS *)&HpcPciAddress;\r
415 DevicePathString = ConvertDevicePathToText (HpcDevicePath, FALSE, FALSE);\r
416\r
417 DEBUG ((EFI_D_VERBOSE, "%a: Address=%02x:%02x.%x DevicePath=%s\n",\r
418 __FUNCTION__, Address->Bus, Address->Device, Address->Function,\r
419 (DevicePathString == NULL) ? L"<unavailable>" : DevicePathString));\r
420\r
421 if (DevicePathString != NULL) {\r
422 FreePool (DevicePathString);\r
423 }\r
424 );\r
425\r
426 if (HpcState == NULL || Padding == NULL || Attributes == NULL) {\r
427 return EFI_INVALID_PARAMETER;\r
428 }\r
429\r
a9803247
LE
430 DefaultIo = TRUE;\r
431 DefaultMmio = TRUE;\r
432\r
433 //\r
434 // Init ReservationRequest, and point FirstResource one past the last\r
435 // descriptor entry. We're going to build the entries backwards from\r
436 // ReservationRequest.EndDesc.\r
437 //\r
438 InitializeResourcePadding (&ReservationRequest);\r
439 FirstResource = ReservationRequest.Padding +\r
440 ARRAY_SIZE (ReservationRequest.Padding);\r
441\r
442 //\r
443 // (b) Reserve IO space.\r
444 //\r
445 if (DefaultIo) {\r
446 //\r
447 // Request defaults.\r
448 //\r
4776d5cb 449 SetIoPadding (--FirstResource, (UINTN)HighBitSetRoundUp64 (512));\r
a9803247
LE
450 }\r
451\r
452 //\r
453 // (c) Reserve non-prefetchable MMIO space (32-bit only).\r
454 //\r
455 if (DefaultMmio) {\r
456 //\r
457 // Request defaults.\r
458 //\r
4776d5cb
LE
459 SetMmioPadding (\r
460 --FirstResource,\r
461 FALSE,\r
462 TRUE,\r
463 (UINTN)HighBitSetRoundUp32 (SIZE_2MB)\r
464 );\r
a9803247
LE
465 }\r
466\r
467 //\r
468 // Output a copy of ReservationRequest from the lowest-address populated\r
469 // entry until the end of the structure (including\r
470 // ReservationRequest.EndDesc). If no reservations are necessary, we'll only\r
471 // output the End Tag.\r
472 //\r
473 *Padding = AllocateCopyPool (\r
474 (UINT8 *)(&ReservationRequest + 1) - (UINT8 *)FirstResource,\r
475 FirstResource\r
476 );\r
8aba40b7
LE
477 if (*Padding == NULL) {\r
478 return EFI_OUT_OF_RESOURCES;\r
479 }\r
480\r
481 //\r
482 // Resource padding is required.\r
483 //\r
484 *HpcState = EFI_HPC_STATE_INITIALIZED | EFI_HPC_STATE_ENABLED;\r
485\r
486 //\r
487 // The padding should be applied at PCI bus level, and considered by upstream\r
488 // bridges, recursively.\r
489 //\r
490 *Attributes = EfiPaddingPciBus;\r
491 return EFI_SUCCESS;\r
492}\r
493\r
494\r
495/**\r
496 Entry point for this driver.\r
497\r
498 @param[in] ImageHandle Image handle of this driver.\r
499 @param[in] SystemTable Pointer to SystemTable.\r
500\r
501 @retval EFI_SUCESS Driver has loaded successfully.\r
502 @return Error codes from lower level functions.\r
503\r
504**/\r
505EFI_STATUS\r
506EFIAPI\r
507DriverInitialize (\r
508 IN EFI_HANDLE ImageHandle,\r
509 IN EFI_SYSTEM_TABLE *SystemTable\r
510 )\r
511{\r
512 EFI_STATUS Status;\r
513\r
514 mPciHotPlugInit.GetRootHpcList = GetRootHpcList;\r
515 mPciHotPlugInit.InitializeRootHpc = InitializeRootHpc;\r
516 mPciHotPlugInit.GetResourcePadding = GetResourcePadding;\r
517 Status = gBS->InstallMultipleProtocolInterfaces (&ImageHandle,\r
518 &gEfiPciHotPlugInitProtocolGuid, &mPciHotPlugInit, NULL);\r
519 return Status;\r
520}\r