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 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <IndustryStandard/Acpi10.h>
11 #include <IndustryStandard/Q35MchIch9.h>
12 #include <IndustryStandard/QemuPciBridgeCapabilities.h>
14 #include <Library/BaseLib.h>
15 #include <Library/BaseMemoryLib.h>
16 #include <Library/DebugLib.h>
17 #include <Library/DevicePathLib.h>
18 #include <Library/MemoryAllocationLib.h>
19 #include <Library/PciCapLib.h>
20 #include <Library/PciCapPciSegmentLib.h>
21 #include <Library/PciLib.h>
22 #include <Library/UefiBootServicesTableLib.h>
24 #include <Protocol/PciHotPlugInit.h>
25 #include <Protocol/PciRootBridgeIo.h>
28 // TRUE if the PCI platform supports extended config space, FALSE otherwise.
30 STATIC BOOLEAN mPciExtConfSpaceSupported
;
33 // The protocol interface this driver produces.
35 // Refer to 12.6 "PCI Hot Plug PCI Initialization Protocol" in the Platform
36 // Init 1.4a Spec, Volume 5.
38 STATIC EFI_PCI_HOT_PLUG_INIT_PROTOCOL mPciHotPlugInit
;
41 // Resource padding template for the GetResourcePadding() protocol member
44 // Refer to Table 8 "ACPI 2.0 & 3.0 QWORD Address Space Descriptor Usage" in
45 // the Platform Init 1.4a Spec, Volume 5.
47 // This structure is interpreted by the ApplyResourcePadding() function in the
48 // edk2 PCI Bus UEFI_DRIVER.
50 // We can request padding for at most four resource types, each of which is
51 // optional, independently of the others:
54 // (c) non-prefetchable MMIO space (32-bit only),
55 // (d) prefetchable MMIO space (either 32-bit or 64-bit, never both).
59 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR Padding
[4];
60 EFI_ACPI_END_TAG_DESCRIPTOR EndDesc
;
65 Initialize a RESOURCE_PADDING object.
67 @param[out] ResourcePadding The caller-allocated RESOURCE_PADDING object to
72 InitializeResourcePadding (
73 OUT RESOURCE_PADDING
*ResourcePadding
78 ZeroMem (ResourcePadding
, sizeof *ResourcePadding
);
81 // Fill in the Padding fields that don't vary across resource types.
83 for (Index
= 0; Index
< ARRAY_SIZE (ResourcePadding
->Padding
); ++Index
) {
84 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptor
;
86 Descriptor
= ResourcePadding
->Padding
+ Index
;
87 Descriptor
->Desc
= ACPI_ADDRESS_SPACE_DESCRIPTOR
;
88 Descriptor
->Len
= (UINT16
)(
89 sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) -
91 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
,
98 // Fill in the End Tag.
100 ResourcePadding
->EndDesc
.Desc
= ACPI_END_TAG_DESCRIPTOR
;
104 Set up a descriptor entry for reserving IO space.
106 @param[in,out] Descriptor The descriptor to configure. The caller shall have
107 initialized Descriptor earlier, with
108 InitializeResourcePadding().
110 @param[in] SizeExponent The size and natural alignment of the reservation
111 are determined by raising two to this power.
116 IN OUT EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptor
,
117 IN UINTN SizeExponent
120 Descriptor
->ResType
= ACPI_ADDRESS_SPACE_TYPE_IO
;
121 Descriptor
->AddrLen
= LShiftU64 (1, SizeExponent
);
122 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;
167 Descriptor
->AddrLen
= LShiftU64 (1, SizeExponent
);
168 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
);
201 if ((Operand
& (Operand
- 1)) != 0) {
203 // Operand is not a whole power of two.
208 return (HighBit
< 32) ? HighBit
: -1;
212 Round up a positive 64-bit value to the next whole power of two, and return
213 the bit position of the highest bit set in the result. Equivalent to
216 @param[in] Operand The 64-bit operand to evaluate.
218 @retval -1 Operand is zero.
220 @retval -1 Operand is positive, not a whole power of two, and rounding it
221 up to the next power of two does not fit into 64 bits.
223 @retval 0..63 Otherwise, return ceil(log2(Value)).
227 HighBitSetRoundUp64 (
233 HighBit
= HighBitSet64 (Operand
);
241 if ((Operand
& (Operand
- 1)) != 0) {
243 // Operand is not a whole power of two.
248 return (HighBit
< 64) ? HighBit
: -1;
252 Look up the QEMU-specific Resource Reservation capability in the conventional
253 config space of a Hotplug Controller (that is, PCI Bridge).
255 On error, the contents of ReservationHint are indeterminate.
257 @param[in] HpcPciAddress The address of the PCI Bridge -- Bus, Device,
258 Function -- in UEFI (not PciLib) encoding.
260 @param[out] ReservationHint The caller-allocated capability structure to
261 populate from the PCI Bridge's config space.
263 @retval EFI_SUCCESS The capability has been found, ReservationHint has
266 @retval EFI_NOT_FOUND The capability is missing.
268 @return Error codes from PciCapPciSegmentLib and PciCapLib.
272 QueryReservationHint (
273 IN CONST EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS
*HpcPciAddress
,
274 OUT QEMU_PCI_BRIDGE_CAPABILITY_RESOURCE_RESERVATION
*ReservationHint
279 PCI_CAP_DEV
*PciDevice
;
280 PCI_CAP_LIST
*CapList
;
281 UINT16 VendorInstance
;
285 // Check the vendor identifier.
287 PciVendorId
= PciRead16 (
290 HpcPciAddress
->Device
,
291 HpcPciAddress
->Function
,
295 if (PciVendorId
!= QEMU_PCI_BRIDGE_VENDOR_ID_REDHAT
) {
296 return EFI_NOT_FOUND
;
300 // Parse the capabilities lists.
302 Status
= PciCapPciSegmentDeviceInit (
303 mPciExtConfSpaceSupported
? PciCapExtended
: PciCapNormal
,
306 HpcPciAddress
->Device
,
307 HpcPciAddress
->Function
,
310 if (EFI_ERROR (Status
)) {
314 Status
= PciCapListInit (PciDevice
, &CapList
);
315 if (EFI_ERROR (Status
)) {
316 goto UninitPciDevice
;
320 // Scan the vendor capability instances for the Resource Reservation
328 Status
= PciCapListFindCap (
331 EFI_PCI_CAPABILITY_ID_VENDOR
,
335 if (EFI_ERROR (Status
)) {
340 // Check the vendor capability length.
342 Status
= PciCapRead (
345 OFFSET_OF (EFI_PCI_CAPABILITY_VENDOR_HDR
, Length
),
349 if (EFI_ERROR (Status
)) {
353 if (VendorLength
!= sizeof *ReservationHint
) {
358 // Check the vendor bridge capability type.
360 Status
= PciCapRead (
363 OFFSET_OF (QEMU_PCI_BRIDGE_CAPABILITY_HDR
, Type
),
367 if (EFI_ERROR (Status
)) {
372 QEMU_PCI_BRIDGE_CAPABILITY_TYPE_RESOURCE_RESERVATION
)
382 // Populate ReservationHint.
384 Status
= PciCapRead (
387 0, // SourceOffsetInCap
389 sizeof *ReservationHint
393 PciCapListUninit (CapList
);
396 PciCapPciSegmentDeviceUninit (PciDevice
);
402 Returns a list of root Hot Plug Controllers (HPCs) that require
403 initialization during the boot process.
405 This procedure returns a list of root HPCs. The PCI bus driver must
406 initialize these controllers during the boot process. The PCI bus driver may
407 or may not be able to detect these HPCs. If the platform includes a
408 PCI-to-CardBus bridge, it can be included in this list if it requires
409 initialization. The HpcList must be self consistent. An HPC cannot control
410 any of its parent buses. Only one HPC can control a PCI bus. Because this
411 list includes only root HPCs, no HPC in the list can be a child of another
412 HPC. This policy must be enforced by the EFI_PCI_HOT_PLUG_INIT_PROTOCOL.
413 The PCI bus driver may not check for such invalid conditions. The callee
414 allocates the buffer HpcList
416 @param[in] This Pointer to the EFI_PCI_HOT_PLUG_INIT_PROTOCOL
418 @param[out] HpcCount The number of root HPCs that were returned.
419 @param[out] HpcList The list of root HPCs. HpcCount defines the number of
420 elements in this list.
422 @retval EFI_SUCCESS HpcList was returned.
423 @retval EFI_OUT_OF_RESOURCES HpcList was not returned due to insufficient
425 @retval EFI_INVALID_PARAMETER HpcCount is NULL or HpcList is NULL.
431 IN EFI_PCI_HOT_PLUG_INIT_PROTOCOL
*This
,
433 OUT EFI_HPC_LOCATION
**HpcList
436 if ((HpcCount
== NULL
) || (HpcList
== NULL
)) {
437 return EFI_INVALID_PARAMETER
;
441 // There are no top-level (i.e., un-enumerable) hot-plug controllers in QEMU
442 // that would require special initialization.
450 Initializes one root Hot Plug Controller (HPC). This process may causes
451 initialization of its subordinate buses.
453 This function initializes the specified HPC. At the end of initialization,
454 the hot-plug slots or sockets (controlled by this HPC) are powered and are
455 connected to the bus. All the necessary registers in the HPC are set up. For
456 a Standard (PCI) Hot Plug Controller (SHPC), the registers that must be set
457 up are defined in the PCI Standard Hot Plug Controller and Subsystem
460 @param[in] This Pointer to the EFI_PCI_HOT_PLUG_INIT_PROTOCOL
462 @param[in] HpcDevicePath The device path to the HPC that is being
464 @param[in] HpcPciAddress The address of the HPC function on the PCI bus.
465 @param[in] Event The event that should be signaled when the HPC
466 initialization is complete. Set to NULL if the
467 caller wants to wait until the entire
468 initialization process is complete.
469 @param[out] HpcState The state of the HPC hardware. The state is
470 EFI_HPC_STATE_INITIALIZED or
471 EFI_HPC_STATE_ENABLED.
473 @retval EFI_SUCCESS If Event is NULL, the specific HPC was
474 successfully initialized. If Event is not
475 NULL, Event will be signaled at a later time
476 when initialization is complete.
477 @retval EFI_UNSUPPORTED This instance of
478 EFI_PCI_HOT_PLUG_INIT_PROTOCOL does not
479 support the specified HPC.
480 @retval EFI_OUT_OF_RESOURCES Initialization failed due to insufficient
482 @retval EFI_INVALID_PARAMETER HpcState is NULL.
488 IN EFI_PCI_HOT_PLUG_INIT_PROTOCOL
*This
,
489 IN EFI_DEVICE_PATH_PROTOCOL
*HpcDevicePath
,
490 IN UINT64 HpcPciAddress
,
491 IN EFI_EVENT Event OPTIONAL
,
492 OUT EFI_HPC_STATE
*HpcState
496 // This function should never be called, due to the information returned by
501 if (HpcState
== NULL
) {
502 return EFI_INVALID_PARAMETER
;
505 return EFI_UNSUPPORTED
;
509 Returns the resource padding that is required by the PCI bus that is
510 controlled by the specified Hot Plug Controller (HPC).
512 This function returns the resource padding that is required by the PCI bus
513 that is controlled by the specified HPC. This member function is called for
514 all the root HPCs and nonroot HPCs that are detected by the PCI bus
515 enumerator. This function will be called before PCI resource allocation is
516 completed. This function must be called after all the root HPCs, with the
517 possible exception of a PCI-to-CardBus bridge, have completed
520 @param[in] This Pointer to the EFI_PCI_HOT_PLUG_INIT_PROTOCOL
522 @param[in] HpcDevicePath The device path to the HPC.
523 @param[in] HpcPciAddress The address of the HPC function on the PCI bus.
524 @param[in] HpcState The state of the HPC hardware.
525 @param[out] Padding The amount of resource padding that is required
526 by the PCI bus under the control of the specified
528 @param[out] Attributes Describes how padding is accounted for. The
529 padding is returned in the form of ACPI 2.0
530 resource descriptors.
532 @retval EFI_SUCCESS The resource padding was successfully
534 @retval EFI_UNSUPPORTED This instance of the
535 EFI_PCI_HOT_PLUG_INIT_PROTOCOL does not
536 support the specified HPC.
537 @retval EFI_NOT_READY This function was called before HPC
538 initialization is complete.
539 @retval EFI_INVALID_PARAMETER HpcState or Padding or Attributes is NULL.
540 @retval EFI_OUT_OF_RESOURCES ACPI 2.0 resource descriptors for Padding
541 cannot be allocated due to insufficient
548 IN EFI_PCI_HOT_PLUG_INIT_PROTOCOL
*This
,
549 IN EFI_DEVICE_PATH_PROTOCOL
*HpcDevicePath
,
550 IN UINT64 HpcPciAddress
,
551 OUT EFI_HPC_STATE
*HpcState
,
553 OUT EFI_HPC_PADDING_ATTRIBUTES
*Attributes
556 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS
*Address
;
559 RESOURCE_PADDING ReservationRequest
;
560 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*FirstResource
;
561 EFI_STATUS ReservationHintStatus
;
562 QEMU_PCI_BRIDGE_CAPABILITY_RESOURCE_RESERVATION ReservationHint
;
564 Address
= (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS
*)&HpcPciAddress
;
567 CHAR16
*DevicePathString
;
569 DevicePathString
= ConvertDevicePathToText (HpcDevicePath
, FALSE
, FALSE
);
573 "%a: Address=%02x:%02x.%x DevicePath=%s\n",
578 (DevicePathString
== NULL
) ? L
"<unavailable>" : DevicePathString
581 if (DevicePathString
!= NULL
) {
582 FreePool (DevicePathString
);
587 if ((HpcState
== NULL
) || (Padding
== NULL
) || (Attributes
== NULL
)) {
588 return EFI_INVALID_PARAMETER
;
595 // Init ReservationRequest, and point FirstResource one past the last
596 // descriptor entry. We're going to build the entries backwards from
597 // ReservationRequest.EndDesc.
599 InitializeResourcePadding (&ReservationRequest
);
600 FirstResource
= ReservationRequest
.Padding
+
601 ARRAY_SIZE (ReservationRequest
.Padding
);
604 // Try to get the QEMU-specific Resource Reservation capability.
606 ReservationHintStatus
= QueryReservationHint (Address
, &ReservationHint
);
607 if (!EFI_ERROR (ReservationHintStatus
)) {
612 "%a: BusNumbers=0x%x Io=0x%Lx NonPrefetchable32BitMmio=0x%x\n"
613 "%a: Prefetchable32BitMmio=0x%x Prefetchable64BitMmio=0x%Lx\n",
615 ReservationHint
.BusNumbers
,
617 ReservationHint
.NonPrefetchable32BitMmio
,
619 ReservationHint
.Prefetchable32BitMmio
,
620 ReservationHint
.Prefetchable64BitMmio
624 // (a) Reserve bus numbers.
626 switch (ReservationHint
.BusNumbers
) {
629 // No reservation needed.
634 // Firmware default (unspecified). Treat it as "no reservation needed".
639 // Request the specified amount.
642 FirstResource
->ResType
= ACPI_ADDRESS_SPACE_TYPE_BUS
;
643 FirstResource
->AddrLen
= ReservationHint
.BusNumbers
;
648 // (b) Reserve IO space.
650 switch (ReservationHint
.Io
) {
653 // No reservation needed, disable our built-in.
659 // Firmware default (unspecified). Stick with our built-in.
664 // Round the specified amount up to the next power of two. If rounding is
665 // successful, reserve the rounded value. Fall back to the default
668 HighBit
= HighBitSetRoundUp64 (ReservationHint
.Io
);
670 SetIoPadding (--FirstResource
, (UINTN
)HighBit
);
678 // (c) Reserve non-prefetchable MMIO space (32-bit only).
680 switch (ReservationHint
.NonPrefetchable32BitMmio
) {
683 // No reservation needed, disable our built-in.
689 // Firmware default (unspecified). Stick with our built-in.
694 // Round the specified amount up to the next power of two. If rounding is
695 // successful, reserve the rounded value. Fall back to the default
698 HighBit
= HighBitSetRoundUp32 (ReservationHint
.NonPrefetchable32BitMmio
);
700 SetMmioPadding (--FirstResource
, FALSE
, TRUE
, (UINTN
)HighBit
);
708 // (d) Reserve prefetchable MMIO space (either 32-bit or 64-bit, never
711 // For either space, we treat 0 as "no reservation needed", and the maximum
712 // value as "firmware default". The latter is unspecified, and we interpret
715 // Otherwise, round the specified amount up to the next power of two. If
716 // rounding is successful, reserve the rounded value. Do not reserve
717 // prefetchable MMIO space otherwise.
719 if ((ReservationHint
.Prefetchable32BitMmio
> 0) &&
720 (ReservationHint
.Prefetchable32BitMmio
< MAX_UINT32
))
722 HighBit
= HighBitSetRoundUp32 (ReservationHint
.Prefetchable32BitMmio
);
724 SetMmioPadding (--FirstResource
, TRUE
, TRUE
, (UINTN
)HighBit
);
726 } else if ((ReservationHint
.Prefetchable64BitMmio
> 0) &&
727 (ReservationHint
.Prefetchable64BitMmio
< MAX_UINT64
))
729 HighBit
= HighBitSetRoundUp64 (ReservationHint
.Prefetchable64BitMmio
);
731 SetMmioPadding (--FirstResource
, TRUE
, FALSE
, (UINTN
)HighBit
);
740 SetIoPadding (--FirstResource
, (UINTN
)HighBitSetRoundUp64 (512));
751 (UINTN
)HighBitSetRoundUp32 (SIZE_2MB
)
756 // Output a copy of ReservationRequest from the lowest-address populated
757 // entry until the end of the structure (including
758 // ReservationRequest.EndDesc). If no reservations are necessary, we'll only
759 // output the End Tag.
761 *Padding
= AllocateCopyPool (
762 (UINT8
*)(&ReservationRequest
+ 1) - (UINT8
*)FirstResource
,
765 if (*Padding
== NULL
) {
766 return EFI_OUT_OF_RESOURCES
;
770 // Resource padding is required.
772 *HpcState
= EFI_HPC_STATE_INITIALIZED
| EFI_HPC_STATE_ENABLED
;
775 // The padding should be applied at PCI bus level, and considered by upstream
776 // bridges, recursively.
778 *Attributes
= EfiPaddingPciBus
;
783 Entry point for this driver.
785 @param[in] ImageHandle Image handle of this driver.
786 @param[in] SystemTable Pointer to SystemTable.
788 @retval EFI_SUCESS Driver has loaded successfully.
789 @return Error codes from lower level functions.
795 IN EFI_HANDLE ImageHandle
,
796 IN EFI_SYSTEM_TABLE
*SystemTable
801 mPciExtConfSpaceSupported
= (PcdGet16 (PcdOvmfHostBridgePciDevId
) ==
802 INTEL_Q35_MCH_DEVICE_ID
);
803 mPciHotPlugInit
.GetRootHpcList
= GetRootHpcList
;
804 mPciHotPlugInit
.InitializeRootHpc
= InitializeRootHpc
;
805 mPciHotPlugInit
.GetResourcePadding
= GetResourcePadding
;
806 Status
= gBS
->InstallMultipleProtocolInterfaces (
808 &gEfiPciHotPlugInitProtocolGuid
,