2 This driver implements EFI_PCI_HOT_PLUG_INIT_PROTOCOL, providing the PCI bus
3 driver with resource padding information, for PCIe hotplug purposes.
5 Copyright (C) 2016, Red Hat, Inc.
7 This program and the accompanying materials are licensed and made available
8 under the terms and conditions of the BSD License which accompanies this
9 distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
13 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <IndustryStandard/Acpi10.h>
18 #include <Library/BaseLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/DevicePathLib.h>
22 #include <Library/MemoryAllocationLib.h>
23 #include <Library/UefiBootServicesTableLib.h>
25 #include <Protocol/PciHotPlugInit.h>
26 #include <Protocol/PciRootBridgeIo.h>
29 // The protocol interface this driver produces.
31 // Refer to 12.6 "PCI Hot Plug PCI Initialization Protocol" in the Platform
32 // Init 1.4a Spec, Volume 5.
34 STATIC EFI_PCI_HOT_PLUG_INIT_PROTOCOL mPciHotPlugInit
;
38 // Resource padding template for the GetResourcePadding() protocol member
41 // Refer to Table 8 "ACPI 2.0 & 3.0 QWORD Address Space Descriptor Usage" in
42 // the Platform Init 1.4a Spec, Volume 5.
44 // This structure is interpreted by the ApplyResourcePadding() function in the
45 // edk2 PCI Bus UEFI_DRIVER.
47 // We can request padding for at most four resource types, each of which is
48 // optional, independently of the others:
51 // (c) non-prefetchable MMIO space (32-bit only),
52 // (d) prefetchable MMIO space (either 32-bit or 64-bit, never both).
56 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR Padding
[4];
57 EFI_ACPI_END_TAG_DESCRIPTOR EndDesc
;
63 Initialize a RESOURCE_PADDING object.
65 @param[out] ResourcePadding The caller-allocated RESOURCE_PADDING object to
70 InitializeResourcePadding (
71 OUT RESOURCE_PADDING
*ResourcePadding
76 ZeroMem (ResourcePadding
, sizeof *ResourcePadding
);
79 // Fill in the Padding fields that don't vary across resource types.
81 for (Index
= 0; Index
< ARRAY_SIZE (ResourcePadding
->Padding
); ++Index
) {
82 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptor
;
84 Descriptor
= ResourcePadding
->Padding
+ Index
;
85 Descriptor
->Desc
= ACPI_ADDRESS_SPACE_DESCRIPTOR
;
86 Descriptor
->Len
= (UINT16
)(
87 sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) -
89 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
,
96 // Fill in the End Tag.
98 ResourcePadding
->EndDesc
.Desc
= ACPI_END_TAG_DESCRIPTOR
;
103 Set up a descriptor entry for reserving IO space.
105 @param[in,out] Descriptor The descriptor to configure. The caller shall have
106 initialized Descriptor earlier, with
107 InitializeResourcePadding().
109 @param[in] SizeExponent The size and natural alignment of the reservation
110 are determined by raising two to this power.
115 IN OUT EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptor
,
116 IN UINTN SizeExponent
119 Descriptor
->ResType
= ACPI_ADDRESS_SPACE_TYPE_IO
;
120 Descriptor
->AddrLen
= LShiftU64 (1, SizeExponent
);
121 Descriptor
->AddrRangeMax
= Descriptor
->AddrLen
- 1;
126 Set up a descriptor entry for reserving MMIO space.
128 @param[in,out] Descriptor The descriptor to configure. The caller shall
129 have initialized Descriptor earlier, with
130 InitializeResourcePadding().
132 @param[in] Prefetchable TRUE if the descriptor should reserve
133 prefetchable MMIO space. Pass FALSE for
134 reserving non-prefetchable MMIO space.
136 @param[in] ThirtyTwoBitOnly TRUE if the reservation should be limited to
137 32-bit address space. FALSE if the reservation
138 can be satisfied from 64-bit address space.
139 ThirtyTwoBitOnly is ignored if Prefetchable is
140 FALSE; in that case ThirtyTwoBitOnly is always
143 @param[in] SizeExponent The size and natural alignment of the
144 reservation are determined by raising two to
150 IN OUT EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptor
,
151 IN BOOLEAN Prefetchable
,
152 IN BOOLEAN ThirtyTwoBitOnly
,
153 IN UINTN SizeExponent
156 Descriptor
->ResType
= ACPI_ADDRESS_SPACE_TYPE_MEM
;
158 Descriptor
->SpecificFlag
=
159 EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE
;
160 Descriptor
->AddrSpaceGranularity
= ThirtyTwoBitOnly
? 32 : 64;
162 Descriptor
->SpecificFlag
=
163 EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_NON_CACHEABLE
;
164 Descriptor
->AddrSpaceGranularity
= 32;
166 Descriptor
->AddrLen
= LShiftU64 (1, SizeExponent
);
167 Descriptor
->AddrRangeMax
= Descriptor
->AddrLen
- 1;
172 Round up a positive 32-bit value to the next whole power of two, and return
173 the bit position of the highest bit set in the result. Equivalent to
176 @param[in] Operand The 32-bit operand to evaluate.
178 @retval -1 Operand is zero.
180 @retval -1 Operand is positive, not a whole power of two, and rounding it
181 up to the next power of two does not fit into 32 bits.
183 @retval 0..31 Otherwise, return ceil(log2(Value)).
187 HighBitSetRoundUp32 (
193 HighBit
= HighBitSet32 (Operand
);
200 if ((Operand
& (Operand
- 1)) != 0) {
202 // Operand is not a whole power of two.
206 return (HighBit
< 32) ? HighBit
: -1;
211 Round up a positive 64-bit value to the next whole power of two, and return
212 the bit position of the highest bit set in the result. Equivalent to
215 @param[in] Operand The 64-bit operand to evaluate.
217 @retval -1 Operand is zero.
219 @retval -1 Operand is positive, not a whole power of two, and rounding it
220 up to the next power of two does not fit into 64 bits.
222 @retval 0..63 Otherwise, return ceil(log2(Value)).
226 HighBitSetRoundUp64 (
232 HighBit
= HighBitSet64 (Operand
);
239 if ((Operand
& (Operand
- 1)) != 0) {
241 // Operand is not a whole power of two.
245 return (HighBit
< 64) ? HighBit
: -1;
250 Returns a list of root Hot Plug Controllers (HPCs) that require
251 initialization during the boot process.
253 This procedure returns a list of root HPCs. The PCI bus driver must
254 initialize these controllers during the boot process. The PCI bus driver may
255 or may not be able to detect these HPCs. If the platform includes a
256 PCI-to-CardBus bridge, it can be included in this list if it requires
257 initialization. The HpcList must be self consistent. An HPC cannot control
258 any of its parent buses. Only one HPC can control a PCI bus. Because this
259 list includes only root HPCs, no HPC in the list can be a child of another
260 HPC. This policy must be enforced by the EFI_PCI_HOT_PLUG_INIT_PROTOCOL.
261 The PCI bus driver may not check for such invalid conditions. The callee
262 allocates the buffer HpcList
264 @param[in] This Pointer to the EFI_PCI_HOT_PLUG_INIT_PROTOCOL
266 @param[out] HpcCount The number of root HPCs that were returned.
267 @param[out] HpcList The list of root HPCs. HpcCount defines the number of
268 elements in this list.
270 @retval EFI_SUCCESS HpcList was returned.
271 @retval EFI_OUT_OF_RESOURCES HpcList was not returned due to insufficient
273 @retval EFI_INVALID_PARAMETER HpcCount is NULL or HpcList is NULL.
279 IN EFI_PCI_HOT_PLUG_INIT_PROTOCOL
*This
,
281 OUT EFI_HPC_LOCATION
**HpcList
284 if (HpcCount
== NULL
|| HpcList
== NULL
) {
285 return EFI_INVALID_PARAMETER
;
289 // There are no top-level (i.e., un-enumerable) hot-plug controllers in QEMU
290 // that would require special initialization.
299 Initializes one root Hot Plug Controller (HPC). This process may causes
300 initialization of its subordinate buses.
302 This function initializes the specified HPC. At the end of initialization,
303 the hot-plug slots or sockets (controlled by this HPC) are powered and are
304 connected to the bus. All the necessary registers in the HPC are set up. For
305 a Standard (PCI) Hot Plug Controller (SHPC), the registers that must be set
306 up are defined in the PCI Standard Hot Plug Controller and Subsystem
309 @param[in] This Pointer to the EFI_PCI_HOT_PLUG_INIT_PROTOCOL
311 @param[in] HpcDevicePath The device path to the HPC that is being
313 @param[in] HpcPciAddress The address of the HPC function on the PCI bus.
314 @param[in] Event The event that should be signaled when the HPC
315 initialization is complete. Set to NULL if the
316 caller wants to wait until the entire
317 initialization process is complete.
318 @param[out] HpcState The state of the HPC hardware. The state is
319 EFI_HPC_STATE_INITIALIZED or
320 EFI_HPC_STATE_ENABLED.
322 @retval EFI_SUCCESS If Event is NULL, the specific HPC was
323 successfully initialized. If Event is not
324 NULL, Event will be signaled at a later time
325 when initialization is complete.
326 @retval EFI_UNSUPPORTED This instance of
327 EFI_PCI_HOT_PLUG_INIT_PROTOCOL does not
328 support the specified HPC.
329 @retval EFI_OUT_OF_RESOURCES Initialization failed due to insufficient
331 @retval EFI_INVALID_PARAMETER HpcState is NULL.
337 IN EFI_PCI_HOT_PLUG_INIT_PROTOCOL
*This
,
338 IN EFI_DEVICE_PATH_PROTOCOL
*HpcDevicePath
,
339 IN UINT64 HpcPciAddress
,
340 IN EFI_EVENT Event
, OPTIONAL
341 OUT EFI_HPC_STATE
*HpcState
345 // This function should never be called, due to the information returned by
350 if (HpcState
== NULL
) {
351 return EFI_INVALID_PARAMETER
;
353 return EFI_UNSUPPORTED
;
358 Returns the resource padding that is required by the PCI bus that is
359 controlled by the specified Hot Plug Controller (HPC).
361 This function returns the resource padding that is required by the PCI bus
362 that is controlled by the specified HPC. This member function is called for
363 all the root HPCs and nonroot HPCs that are detected by the PCI bus
364 enumerator. This function will be called before PCI resource allocation is
365 completed. This function must be called after all the root HPCs, with the
366 possible exception of a PCI-to-CardBus bridge, have completed
369 @param[in] This Pointer to the EFI_PCI_HOT_PLUG_INIT_PROTOCOL
371 @param[in] HpcDevicePath The device path to the HPC.
372 @param[in] HpcPciAddress The address of the HPC function on the PCI bus.
373 @param[in] HpcState The state of the HPC hardware.
374 @param[out] Padding The amount of resource padding that is required
375 by the PCI bus under the control of the specified
377 @param[out] Attributes Describes how padding is accounted for. The
378 padding is returned in the form of ACPI 2.0
379 resource descriptors.
381 @retval EFI_SUCCESS The resource padding was successfully
383 @retval EFI_UNSUPPORTED This instance of the
384 EFI_PCI_HOT_PLUG_INIT_PROTOCOL does not
385 support the specified HPC.
386 @retval EFI_NOT_READY This function was called before HPC
387 initialization is complete.
388 @retval EFI_INVALID_PARAMETER HpcState or Padding or Attributes is NULL.
389 @retval EFI_OUT_OF_RESOURCES ACPI 2.0 resource descriptors for Padding
390 cannot be allocated due to insufficient
397 IN EFI_PCI_HOT_PLUG_INIT_PROTOCOL
*This
,
398 IN EFI_DEVICE_PATH_PROTOCOL
*HpcDevicePath
,
399 IN UINT64 HpcPciAddress
,
400 OUT EFI_HPC_STATE
*HpcState
,
402 OUT EFI_HPC_PADDING_ATTRIBUTES
*Attributes
407 RESOURCE_PADDING ReservationRequest
;
408 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*FirstResource
;
411 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS
*Address
;
412 CHAR16
*DevicePathString
;
414 Address
= (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS
*)&HpcPciAddress
;
415 DevicePathString
= ConvertDevicePathToText (HpcDevicePath
, FALSE
, FALSE
);
417 DEBUG ((EFI_D_VERBOSE
, "%a: Address=%02x:%02x.%x DevicePath=%s\n",
418 __FUNCTION__
, Address
->Bus
, Address
->Device
, Address
->Function
,
419 (DevicePathString
== NULL
) ? L
"<unavailable>" : DevicePathString
));
421 if (DevicePathString
!= NULL
) {
422 FreePool (DevicePathString
);
426 if (HpcState
== NULL
|| Padding
== NULL
|| Attributes
== NULL
) {
427 return EFI_INVALID_PARAMETER
;
434 // Init ReservationRequest, and point FirstResource one past the last
435 // descriptor entry. We're going to build the entries backwards from
436 // ReservationRequest.EndDesc.
438 InitializeResourcePadding (&ReservationRequest
);
439 FirstResource
= ReservationRequest
.Padding
+
440 ARRAY_SIZE (ReservationRequest
.Padding
);
443 // (b) Reserve IO space.
449 SetIoPadding (--FirstResource
, (UINTN
)HighBitSetRoundUp64 (512));
453 // (c) Reserve non-prefetchable MMIO space (32-bit only).
463 (UINTN
)HighBitSetRoundUp32 (SIZE_2MB
)
468 // Output a copy of ReservationRequest from the lowest-address populated
469 // entry until the end of the structure (including
470 // ReservationRequest.EndDesc). If no reservations are necessary, we'll only
471 // output the End Tag.
473 *Padding
= AllocateCopyPool (
474 (UINT8
*)(&ReservationRequest
+ 1) - (UINT8
*)FirstResource
,
477 if (*Padding
== NULL
) {
478 return EFI_OUT_OF_RESOURCES
;
482 // Resource padding is required.
484 *HpcState
= EFI_HPC_STATE_INITIALIZED
| EFI_HPC_STATE_ENABLED
;
487 // The padding should be applied at PCI bus level, and considered by upstream
488 // bridges, recursively.
490 *Attributes
= EfiPaddingPciBus
;
496 Entry point for this driver.
498 @param[in] ImageHandle Image handle of this driver.
499 @param[in] SystemTable Pointer to SystemTable.
501 @retval EFI_SUCESS Driver has loaded successfully.
502 @return Error codes from lower level functions.
508 IN EFI_HANDLE ImageHandle
,
509 IN EFI_SYSTEM_TABLE
*SystemTable
514 mPciHotPlugInit
.GetRootHpcList
= GetRootHpcList
;
515 mPciHotPlugInit
.InitializeRootHpc
= InitializeRootHpc
;
516 mPciHotPlugInit
.GetResourcePadding
= GetResourcePadding
;
517 Status
= gBS
->InstallMultipleProtocolInterfaces (&ImageHandle
,
518 &gEfiPciHotPlugInitProtocolGuid
, &mPciHotPlugInit
, NULL
);