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>
17 #include <IndustryStandard/Q35MchIch9.h>
18 #include <IndustryStandard/QemuPciBridgeCapabilities.h>
20 #include <Library/BaseLib.h>
21 #include <Library/BaseMemoryLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/DevicePathLib.h>
24 #include <Library/MemoryAllocationLib.h>
25 #include <Library/PciCapLib.h>
26 #include <Library/PciCapPciSegmentLib.h>
27 #include <Library/PciLib.h>
28 #include <Library/UefiBootServicesTableLib.h>
30 #include <Protocol/PciHotPlugInit.h>
31 #include <Protocol/PciRootBridgeIo.h>
34 // TRUE if the PCI platform supports extended config space, FALSE otherwise.
36 STATIC BOOLEAN mPciExtConfSpaceSupported
;
40 // The protocol interface this driver produces.
42 // Refer to 12.6 "PCI Hot Plug PCI Initialization Protocol" in the Platform
43 // Init 1.4a Spec, Volume 5.
45 STATIC EFI_PCI_HOT_PLUG_INIT_PROTOCOL mPciHotPlugInit
;
49 // Resource padding template for the GetResourcePadding() protocol member
52 // Refer to Table 8 "ACPI 2.0 & 3.0 QWORD Address Space Descriptor Usage" in
53 // the Platform Init 1.4a Spec, Volume 5.
55 // This structure is interpreted by the ApplyResourcePadding() function in the
56 // edk2 PCI Bus UEFI_DRIVER.
58 // We can request padding for at most four resource types, each of which is
59 // optional, independently of the others:
62 // (c) non-prefetchable MMIO space (32-bit only),
63 // (d) prefetchable MMIO space (either 32-bit or 64-bit, never both).
67 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR Padding
[4];
68 EFI_ACPI_END_TAG_DESCRIPTOR EndDesc
;
74 Initialize a RESOURCE_PADDING object.
76 @param[out] ResourcePadding The caller-allocated RESOURCE_PADDING object to
81 InitializeResourcePadding (
82 OUT RESOURCE_PADDING
*ResourcePadding
87 ZeroMem (ResourcePadding
, sizeof *ResourcePadding
);
90 // Fill in the Padding fields that don't vary across resource types.
92 for (Index
= 0; Index
< ARRAY_SIZE (ResourcePadding
->Padding
); ++Index
) {
93 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptor
;
95 Descriptor
= ResourcePadding
->Padding
+ Index
;
96 Descriptor
->Desc
= ACPI_ADDRESS_SPACE_DESCRIPTOR
;
97 Descriptor
->Len
= (UINT16
)(
98 sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) -
100 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
,
107 // Fill in the End Tag.
109 ResourcePadding
->EndDesc
.Desc
= ACPI_END_TAG_DESCRIPTOR
;
114 Set up a descriptor entry for reserving IO space.
116 @param[in,out] Descriptor The descriptor to configure. The caller shall have
117 initialized Descriptor earlier, with
118 InitializeResourcePadding().
120 @param[in] SizeExponent The size and natural alignment of the reservation
121 are determined by raising two to this power.
126 IN OUT EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptor
,
127 IN UINTN SizeExponent
130 Descriptor
->ResType
= ACPI_ADDRESS_SPACE_TYPE_IO
;
131 Descriptor
->AddrLen
= LShiftU64 (1, SizeExponent
);
132 Descriptor
->AddrRangeMax
= Descriptor
->AddrLen
- 1;
137 Set up a descriptor entry for reserving MMIO space.
139 @param[in,out] Descriptor The descriptor to configure. The caller shall
140 have initialized Descriptor earlier, with
141 InitializeResourcePadding().
143 @param[in] Prefetchable TRUE if the descriptor should reserve
144 prefetchable MMIO space. Pass FALSE for
145 reserving non-prefetchable MMIO space.
147 @param[in] ThirtyTwoBitOnly TRUE if the reservation should be limited to
148 32-bit address space. FALSE if the reservation
149 can be satisfied from 64-bit address space.
150 ThirtyTwoBitOnly is ignored if Prefetchable is
151 FALSE; in that case ThirtyTwoBitOnly is always
154 @param[in] SizeExponent The size and natural alignment of the
155 reservation are determined by raising two to
161 IN OUT EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptor
,
162 IN BOOLEAN Prefetchable
,
163 IN BOOLEAN ThirtyTwoBitOnly
,
164 IN UINTN SizeExponent
167 Descriptor
->ResType
= ACPI_ADDRESS_SPACE_TYPE_MEM
;
169 Descriptor
->SpecificFlag
=
170 EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE
;
171 Descriptor
->AddrSpaceGranularity
= ThirtyTwoBitOnly
? 32 : 64;
173 Descriptor
->SpecificFlag
=
174 EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_NON_CACHEABLE
;
175 Descriptor
->AddrSpaceGranularity
= 32;
177 Descriptor
->AddrLen
= LShiftU64 (1, SizeExponent
);
178 Descriptor
->AddrRangeMax
= Descriptor
->AddrLen
- 1;
183 Round up a positive 32-bit value to the next whole power of two, and return
184 the bit position of the highest bit set in the result. Equivalent to
187 @param[in] Operand The 32-bit operand to evaluate.
189 @retval -1 Operand is zero.
191 @retval -1 Operand is positive, not a whole power of two, and rounding it
192 up to the next power of two does not fit into 32 bits.
194 @retval 0..31 Otherwise, return ceil(log2(Value)).
198 HighBitSetRoundUp32 (
204 HighBit
= HighBitSet32 (Operand
);
211 if ((Operand
& (Operand
- 1)) != 0) {
213 // Operand is not a whole power of two.
217 return (HighBit
< 32) ? HighBit
: -1;
222 Round up a positive 64-bit value to the next whole power of two, and return
223 the bit position of the highest bit set in the result. Equivalent to
226 @param[in] Operand The 64-bit operand to evaluate.
228 @retval -1 Operand is zero.
230 @retval -1 Operand is positive, not a whole power of two, and rounding it
231 up to the next power of two does not fit into 64 bits.
233 @retval 0..63 Otherwise, return ceil(log2(Value)).
237 HighBitSetRoundUp64 (
243 HighBit
= HighBitSet64 (Operand
);
250 if ((Operand
& (Operand
- 1)) != 0) {
252 // Operand is not a whole power of two.
256 return (HighBit
< 64) ? HighBit
: -1;
261 Look up the QEMU-specific Resource Reservation capability in the conventional
262 config space of a Hotplug Controller (that is, PCI Bridge).
264 On error, the contents of ReservationHint are indeterminate.
266 @param[in] HpcPciAddress The address of the PCI Bridge -- Bus, Device,
267 Function -- in UEFI (not PciLib) encoding.
269 @param[out] ReservationHint The caller-allocated capability structure to
270 populate from the PCI Bridge's config space.
272 @retval EFI_SUCCESS The capability has been found, ReservationHint has
275 @retval EFI_NOT_FOUND The capability is missing.
277 @return Error codes from PciCapPciSegmentLib and PciCapLib.
281 QueryReservationHint (
282 IN CONST EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS
*HpcPciAddress
,
283 OUT QEMU_PCI_BRIDGE_CAPABILITY_RESOURCE_RESERVATION
*ReservationHint
288 PCI_CAP_DEV
*PciDevice
;
289 PCI_CAP_LIST
*CapList
;
290 UINT16 VendorInstance
;
294 // Check the vendor identifier.
296 PciVendorId
= PciRead16 (
299 HpcPciAddress
->Device
,
300 HpcPciAddress
->Function
,
304 if (PciVendorId
!= QEMU_PCI_BRIDGE_VENDOR_ID_REDHAT
) {
305 return EFI_NOT_FOUND
;
309 // Parse the capabilities lists.
311 Status
= PciCapPciSegmentDeviceInit (
312 mPciExtConfSpaceSupported
? PciCapExtended
: PciCapNormal
,
315 HpcPciAddress
->Device
,
316 HpcPciAddress
->Function
,
319 if (EFI_ERROR (Status
)) {
322 Status
= PciCapListInit (PciDevice
, &CapList
);
323 if (EFI_ERROR (Status
)) {
324 goto UninitPciDevice
;
328 // Scan the vendor capability instances for the Resource Reservation
336 Status
= PciCapListFindCap (
339 EFI_PCI_CAPABILITY_ID_VENDOR
,
343 if (EFI_ERROR (Status
)) {
348 // Check the vendor capability length.
350 Status
= PciCapRead (
353 OFFSET_OF (EFI_PCI_CAPABILITY_VENDOR_HDR
, Length
),
357 if (EFI_ERROR (Status
)) {
360 if (VendorLength
!= sizeof *ReservationHint
) {
365 // Check the vendor bridge capability type.
367 Status
= PciCapRead (
370 OFFSET_OF (QEMU_PCI_BRIDGE_CAPABILITY_HDR
, Type
),
374 if (EFI_ERROR (Status
)) {
378 QEMU_PCI_BRIDGE_CAPABILITY_TYPE_RESOURCE_RESERVATION
) {
387 // Populate ReservationHint.
389 Status
= PciCapRead (
392 0, // SourceOffsetInCap
394 sizeof *ReservationHint
398 PciCapListUninit (CapList
);
401 PciCapPciSegmentDeviceUninit (PciDevice
);
408 Returns a list of root Hot Plug Controllers (HPCs) that require
409 initialization during the boot process.
411 This procedure returns a list of root HPCs. The PCI bus driver must
412 initialize these controllers during the boot process. The PCI bus driver may
413 or may not be able to detect these HPCs. If the platform includes a
414 PCI-to-CardBus bridge, it can be included in this list if it requires
415 initialization. The HpcList must be self consistent. An HPC cannot control
416 any of its parent buses. Only one HPC can control a PCI bus. Because this
417 list includes only root HPCs, no HPC in the list can be a child of another
418 HPC. This policy must be enforced by the EFI_PCI_HOT_PLUG_INIT_PROTOCOL.
419 The PCI bus driver may not check for such invalid conditions. The callee
420 allocates the buffer HpcList
422 @param[in] This Pointer to the EFI_PCI_HOT_PLUG_INIT_PROTOCOL
424 @param[out] HpcCount The number of root HPCs that were returned.
425 @param[out] HpcList The list of root HPCs. HpcCount defines the number of
426 elements in this list.
428 @retval EFI_SUCCESS HpcList was returned.
429 @retval EFI_OUT_OF_RESOURCES HpcList was not returned due to insufficient
431 @retval EFI_INVALID_PARAMETER HpcCount is NULL or HpcList is NULL.
437 IN EFI_PCI_HOT_PLUG_INIT_PROTOCOL
*This
,
439 OUT EFI_HPC_LOCATION
**HpcList
442 if (HpcCount
== NULL
|| HpcList
== NULL
) {
443 return EFI_INVALID_PARAMETER
;
447 // There are no top-level (i.e., un-enumerable) hot-plug controllers in QEMU
448 // that would require special initialization.
457 Initializes one root Hot Plug Controller (HPC). This process may causes
458 initialization of its subordinate buses.
460 This function initializes the specified HPC. At the end of initialization,
461 the hot-plug slots or sockets (controlled by this HPC) are powered and are
462 connected to the bus. All the necessary registers in the HPC are set up. For
463 a Standard (PCI) Hot Plug Controller (SHPC), the registers that must be set
464 up are defined in the PCI Standard Hot Plug Controller and Subsystem
467 @param[in] This Pointer to the EFI_PCI_HOT_PLUG_INIT_PROTOCOL
469 @param[in] HpcDevicePath The device path to the HPC that is being
471 @param[in] HpcPciAddress The address of the HPC function on the PCI bus.
472 @param[in] Event The event that should be signaled when the HPC
473 initialization is complete. Set to NULL if the
474 caller wants to wait until the entire
475 initialization process is complete.
476 @param[out] HpcState The state of the HPC hardware. The state is
477 EFI_HPC_STATE_INITIALIZED or
478 EFI_HPC_STATE_ENABLED.
480 @retval EFI_SUCCESS If Event is NULL, the specific HPC was
481 successfully initialized. If Event is not
482 NULL, Event will be signaled at a later time
483 when initialization is complete.
484 @retval EFI_UNSUPPORTED This instance of
485 EFI_PCI_HOT_PLUG_INIT_PROTOCOL does not
486 support the specified HPC.
487 @retval EFI_OUT_OF_RESOURCES Initialization failed due to insufficient
489 @retval EFI_INVALID_PARAMETER HpcState is NULL.
495 IN EFI_PCI_HOT_PLUG_INIT_PROTOCOL
*This
,
496 IN EFI_DEVICE_PATH_PROTOCOL
*HpcDevicePath
,
497 IN UINT64 HpcPciAddress
,
498 IN EFI_EVENT Event
, OPTIONAL
499 OUT EFI_HPC_STATE
*HpcState
503 // This function should never be called, due to the information returned by
508 if (HpcState
== NULL
) {
509 return EFI_INVALID_PARAMETER
;
511 return EFI_UNSUPPORTED
;
516 Returns the resource padding that is required by the PCI bus that is
517 controlled by the specified Hot Plug Controller (HPC).
519 This function returns the resource padding that is required by the PCI bus
520 that is controlled by the specified HPC. This member function is called for
521 all the root HPCs and nonroot HPCs that are detected by the PCI bus
522 enumerator. This function will be called before PCI resource allocation is
523 completed. This function must be called after all the root HPCs, with the
524 possible exception of a PCI-to-CardBus bridge, have completed
527 @param[in] This Pointer to the EFI_PCI_HOT_PLUG_INIT_PROTOCOL
529 @param[in] HpcDevicePath The device path to the HPC.
530 @param[in] HpcPciAddress The address of the HPC function on the PCI bus.
531 @param[in] HpcState The state of the HPC hardware.
532 @param[out] Padding The amount of resource padding that is required
533 by the PCI bus under the control of the specified
535 @param[out] Attributes Describes how padding is accounted for. The
536 padding is returned in the form of ACPI 2.0
537 resource descriptors.
539 @retval EFI_SUCCESS The resource padding was successfully
541 @retval EFI_UNSUPPORTED This instance of the
542 EFI_PCI_HOT_PLUG_INIT_PROTOCOL does not
543 support the specified HPC.
544 @retval EFI_NOT_READY This function was called before HPC
545 initialization is complete.
546 @retval EFI_INVALID_PARAMETER HpcState or Padding or Attributes is NULL.
547 @retval EFI_OUT_OF_RESOURCES ACPI 2.0 resource descriptors for Padding
548 cannot be allocated due to insufficient
555 IN EFI_PCI_HOT_PLUG_INIT_PROTOCOL
*This
,
556 IN EFI_DEVICE_PATH_PROTOCOL
*HpcDevicePath
,
557 IN UINT64 HpcPciAddress
,
558 OUT EFI_HPC_STATE
*HpcState
,
560 OUT EFI_HPC_PADDING_ATTRIBUTES
*Attributes
563 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS
*Address
;
566 RESOURCE_PADDING ReservationRequest
;
567 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*FirstResource
;
568 EFI_STATUS ReservationHintStatus
;
569 QEMU_PCI_BRIDGE_CAPABILITY_RESOURCE_RESERVATION ReservationHint
;
571 Address
= (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS
*)&HpcPciAddress
;
574 CHAR16
*DevicePathString
;
576 DevicePathString
= ConvertDevicePathToText (HpcDevicePath
, FALSE
, FALSE
);
578 DEBUG ((EFI_D_VERBOSE
, "%a: Address=%02x:%02x.%x DevicePath=%s\n",
579 __FUNCTION__
, Address
->Bus
, Address
->Device
, Address
->Function
,
580 (DevicePathString
== NULL
) ? L
"<unavailable>" : DevicePathString
));
582 if (DevicePathString
!= NULL
) {
583 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
);
677 // (c) Reserve non-prefetchable MMIO space (32-bit only).
679 switch (ReservationHint
.NonPrefetchable32BitMmio
) {
682 // No reservation needed, disable our built-in.
688 // Firmware default (unspecified). Stick with our built-in.
693 // Round the specified amount up to the next power of two. If rounding is
694 // successful, reserve the rounded value. Fall back to the default
697 HighBit
= HighBitSetRoundUp32 (ReservationHint
.NonPrefetchable32BitMmio
);
699 SetMmioPadding (--FirstResource
, FALSE
, TRUE
, (UINTN
)HighBit
);
706 // (d) Reserve prefetchable MMIO space (either 32-bit or 64-bit, never
709 // For either space, we treat 0 as "no reservation needed", and the maximum
710 // value as "firmware default". The latter is unspecified, and we interpret
713 // Otherwise, round the specified amount up to the next power of two. If
714 // rounding is successful, reserve the rounded value. Do not reserve
715 // prefetchable MMIO space otherwise.
717 if (ReservationHint
.Prefetchable32BitMmio
> 0 &&
718 ReservationHint
.Prefetchable32BitMmio
< MAX_UINT32
) {
719 HighBit
= HighBitSetRoundUp32 (ReservationHint
.Prefetchable32BitMmio
);
721 SetMmioPadding (--FirstResource
, TRUE
, TRUE
, (UINTN
)HighBit
);
723 } else if (ReservationHint
.Prefetchable64BitMmio
> 0 &&
724 ReservationHint
.Prefetchable64BitMmio
< MAX_UINT64
) {
725 HighBit
= HighBitSetRoundUp64 (ReservationHint
.Prefetchable64BitMmio
);
727 SetMmioPadding (--FirstResource
, TRUE
, FALSE
, (UINTN
)HighBit
);
736 SetIoPadding (--FirstResource
, (UINTN
)HighBitSetRoundUp64 (512));
747 (UINTN
)HighBitSetRoundUp32 (SIZE_2MB
)
752 // Output a copy of ReservationRequest from the lowest-address populated
753 // entry until the end of the structure (including
754 // ReservationRequest.EndDesc). If no reservations are necessary, we'll only
755 // output the End Tag.
757 *Padding
= AllocateCopyPool (
758 (UINT8
*)(&ReservationRequest
+ 1) - (UINT8
*)FirstResource
,
761 if (*Padding
== NULL
) {
762 return EFI_OUT_OF_RESOURCES
;
766 // Resource padding is required.
768 *HpcState
= EFI_HPC_STATE_INITIALIZED
| EFI_HPC_STATE_ENABLED
;
771 // The padding should be applied at PCI bus level, and considered by upstream
772 // bridges, recursively.
774 *Attributes
= EfiPaddingPciBus
;
780 Entry point for this driver.
782 @param[in] ImageHandle Image handle of this driver.
783 @param[in] SystemTable Pointer to SystemTable.
785 @retval EFI_SUCESS Driver has loaded successfully.
786 @return Error codes from lower level functions.
792 IN EFI_HANDLE ImageHandle
,
793 IN EFI_SYSTEM_TABLE
*SystemTable
798 mPciExtConfSpaceSupported
= (PcdGet16 (PcdOvmfHostBridgePciDevId
) ==
799 INTEL_Q35_MCH_DEVICE_ID
);
800 mPciHotPlugInit
.GetRootHpcList
= GetRootHpcList
;
801 mPciHotPlugInit
.InitializeRootHpc
= InitializeRootHpc
;
802 mPciHotPlugInit
.GetResourcePadding
= GetResourcePadding
;
803 Status
= gBS
->InstallMultipleProtocolInterfaces (&ImageHandle
,
804 &gEfiPciHotPlugInitProtocolGuid
, &mPciHotPlugInit
, NULL
);