audk/OvmfPkg/Library/PciHostBridgeLib/PciHostBridgeLib.c

133 lines
3.8 KiB
C
Raw Normal View History

/** @file
OVMF's instance of the PCI Host Bridge Library.
Copyright (C) 2016, Red Hat, Inc.
Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <PiDxe.h>
OvmfPkg: PciHostBridgeLib: convert main loop from PciHostBridgeDxe In this patch we import the scan for extra root buses from the InitializePciHostBridge() function, in file "OvmfPkg/PciHostBridgeDxe/PciHostBridge.c". For the time being, the InitRootBridge() and UninitRootBridge() functions are just placeholders. The PciHostBridgeGetRootBridges() API expects us to return the PCI_ROOT_BRIDGE structures in a contiguous array, instead of a linked list. Therefore the following bits have to be converted manually: (1) The array is allocated in advance, in a single step. (2) The calculation of the array size depends on an explicit multiplication, which we must check against overflow. Since more than 255 extra root bridges make no sense anyway, we use (1 + 255) as the limit on the main plus all extra root bridges. This also ensures that the UINTN multiplication doesn't overflow. (3) The PciHostBridgeDxe code decrements "ExtraRootBridgesLeft" to terminate the scanning early. Here we need track the increasing count of used array elements as well, so we employ "ExtraRootBridges" as a constant limit, and increment the new local variable "Initialized". (4) The prototypes of InitRootBridge() and UninitRootBridge() reflect that the PCI_ROOT_BRIDGE structure is allocated by the caller; only in-place initialization is necessary. Additionally, macros are employed for standard PCI quantities, from "MdePkg/Include/IndustryStandard/Pci22.h": - MAX_PCI_DEVICE_NUMBER (31) is replaced with PCI_MAX_DEVICE (same), - the constant 255 is replaced with PCI_MAX_BUS, - the (RootBridgeNumber < 256) condition is replaced with (RootBridgeNumber <= PCI_MAX_BUS). Cc: Jordan Justen <jordan.l.justen@intel.com> Cc: Ruiyu Ni <ruiyu.ni@intel.com> Cc: Marcel Apfelbaum <marcel@redhat.com> Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Laszlo Ersek <lersek@redhat.com> Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2016-01-25 22:47:00 +01:00
OvmfPkg: PciHostBridgeLib: set RootBus->AllocationAttributes InitRootBridge() in "OvmfPkg/PciHostBridgeDxe/PciHostBridge.c" passes the EFI_PCI_HOST_BRIDGE_COMBINE_MEM_PMEM allocation attribute to RootBridgeConstructor(); we should do the same here. From "MdePkg/Include/Protocol/PciHostBridgeResourceAllocation.h": /// If this bit is set, then the PCI Root Bridge does not support separate /// windows for Non-prefetchable and Prefetchable memory. A PCI bus driver /// needs to include requests for Prefetchable memory in the /// Non-prefetchable memory pool. Which implies that both the 32-bit and 64-bit prefetchable MMIO apertures should be marked empty. (The CreateRootBridge() function actually enforces this in "MdeModulePkg/Bus/Pci/PciHostBridgeDxe/PciRootBridgeIo.c".) Furthermore, since OvmfPkg/PciHostBridgeDxe does *not* set the EFI_PCI_HOST_BRIDGE_MEM64_DECODE allocation attribute: /// If this bit is set, then the PCI Root Bridge supports 64 bit memory /// windows. If this bit is not set, the PCI bus driver needs to include /// requests for 64 bit memory address in the corresponding 32 bit memory /// pool. we follow suit in the PciHostBridgeLib instance. In turn, the 64-bit MMIO apertures (both prefetchable and non-prefetchable) should be marked empty. MdeModulePkg/Bus/Pci/PciHostBridgeDxe enforces this too. (64-bit MMIO aperture support, based on yet more fw_cfg files, is a planned future improvement.) Cc: Jordan Justen <jordan.l.justen@intel.com> Cc: Ruiyu Ni <ruiyu.ni@intel.com> Cc: Marcel Apfelbaum <marcel@redhat.com> Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Laszlo Ersek <lersek@redhat.com> Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2016-01-26 00:11:43 +01:00
#include <Protocol/PciHostBridgeResourceAllocation.h>
#include <Protocol/PciRootBridgeIo.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
OvmfPkg: PciHostBridgeLib: convert main loop from PciHostBridgeDxe In this patch we import the scan for extra root buses from the InitializePciHostBridge() function, in file "OvmfPkg/PciHostBridgeDxe/PciHostBridge.c". For the time being, the InitRootBridge() and UninitRootBridge() functions are just placeholders. The PciHostBridgeGetRootBridges() API expects us to return the PCI_ROOT_BRIDGE structures in a contiguous array, instead of a linked list. Therefore the following bits have to be converted manually: (1) The array is allocated in advance, in a single step. (2) The calculation of the array size depends on an explicit multiplication, which we must check against overflow. Since more than 255 extra root bridges make no sense anyway, we use (1 + 255) as the limit on the main plus all extra root bridges. This also ensures that the UINTN multiplication doesn't overflow. (3) The PciHostBridgeDxe code decrements "ExtraRootBridgesLeft" to terminate the scanning early. Here we need track the increasing count of used array elements as well, so we employ "ExtraRootBridges" as a constant limit, and increment the new local variable "Initialized". (4) The prototypes of InitRootBridge() and UninitRootBridge() reflect that the PCI_ROOT_BRIDGE structure is allocated by the caller; only in-place initialization is necessary. Additionally, macros are employed for standard PCI quantities, from "MdePkg/Include/IndustryStandard/Pci22.h": - MAX_PCI_DEVICE_NUMBER (31) is replaced with PCI_MAX_DEVICE (same), - the constant 255 is replaced with PCI_MAX_BUS, - the (RootBridgeNumber < 256) condition is replaced with (RootBridgeNumber <= PCI_MAX_BUS). Cc: Jordan Justen <jordan.l.justen@intel.com> Cc: Ruiyu Ni <ruiyu.ni@intel.com> Cc: Marcel Apfelbaum <marcel@redhat.com> Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Laszlo Ersek <lersek@redhat.com> Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2016-01-25 22:47:00 +01:00
#include <Library/MemoryAllocationLib.h>
#include <Library/PcdLib.h>
OvmfPkg: PciHostBridgeLib: convert main loop from PciHostBridgeDxe In this patch we import the scan for extra root buses from the InitializePciHostBridge() function, in file "OvmfPkg/PciHostBridgeDxe/PciHostBridge.c". For the time being, the InitRootBridge() and UninitRootBridge() functions are just placeholders. The PciHostBridgeGetRootBridges() API expects us to return the PCI_ROOT_BRIDGE structures in a contiguous array, instead of a linked list. Therefore the following bits have to be converted manually: (1) The array is allocated in advance, in a single step. (2) The calculation of the array size depends on an explicit multiplication, which we must check against overflow. Since more than 255 extra root bridges make no sense anyway, we use (1 + 255) as the limit on the main plus all extra root bridges. This also ensures that the UINTN multiplication doesn't overflow. (3) The PciHostBridgeDxe code decrements "ExtraRootBridgesLeft" to terminate the scanning early. Here we need track the increasing count of used array elements as well, so we employ "ExtraRootBridges" as a constant limit, and increment the new local variable "Initialized". (4) The prototypes of InitRootBridge() and UninitRootBridge() reflect that the PCI_ROOT_BRIDGE structure is allocated by the caller; only in-place initialization is necessary. Additionally, macros are employed for standard PCI quantities, from "MdePkg/Include/IndustryStandard/Pci22.h": - MAX_PCI_DEVICE_NUMBER (31) is replaced with PCI_MAX_DEVICE (same), - the constant 255 is replaced with PCI_MAX_BUS, - the (RootBridgeNumber < 256) condition is replaced with (RootBridgeNumber <= PCI_MAX_BUS). Cc: Jordan Justen <jordan.l.justen@intel.com> Cc: Ruiyu Ni <ruiyu.ni@intel.com> Cc: Marcel Apfelbaum <marcel@redhat.com> Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Laszlo Ersek <lersek@redhat.com> Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2016-01-25 22:47:00 +01:00
#include <Library/PciHostBridgeLib.h>
#include <Library/PciHostBridgeUtilityLib.h>
#include "PciHostBridge.h"
OvmfPkg: PciHostBridgeLib: convert main loop from PciHostBridgeDxe In this patch we import the scan for extra root buses from the InitializePciHostBridge() function, in file "OvmfPkg/PciHostBridgeDxe/PciHostBridge.c". For the time being, the InitRootBridge() and UninitRootBridge() functions are just placeholders. The PciHostBridgeGetRootBridges() API expects us to return the PCI_ROOT_BRIDGE structures in a contiguous array, instead of a linked list. Therefore the following bits have to be converted manually: (1) The array is allocated in advance, in a single step. (2) The calculation of the array size depends on an explicit multiplication, which we must check against overflow. Since more than 255 extra root bridges make no sense anyway, we use (1 + 255) as the limit on the main plus all extra root bridges. This also ensures that the UINTN multiplication doesn't overflow. (3) The PciHostBridgeDxe code decrements "ExtraRootBridgesLeft" to terminate the scanning early. Here we need track the increasing count of used array elements as well, so we employ "ExtraRootBridges" as a constant limit, and increment the new local variable "Initialized". (4) The prototypes of InitRootBridge() and UninitRootBridge() reflect that the PCI_ROOT_BRIDGE structure is allocated by the caller; only in-place initialization is necessary. Additionally, macros are employed for standard PCI quantities, from "MdePkg/Include/IndustryStandard/Pci22.h": - MAX_PCI_DEVICE_NUMBER (31) is replaced with PCI_MAX_DEVICE (same), - the constant 255 is replaced with PCI_MAX_BUS, - the (RootBridgeNumber < 256) condition is replaced with (RootBridgeNumber <= PCI_MAX_BUS). Cc: Jordan Justen <jordan.l.justen@intel.com> Cc: Ruiyu Ni <ruiyu.ni@intel.com> Cc: Marcel Apfelbaum <marcel@redhat.com> Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Laszlo Ersek <lersek@redhat.com> Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2016-01-25 22:47:00 +01:00
STATIC PCI_ROOT_BRIDGE_APERTURE mNonExistAperture = { MAX_UINT64, 0 };
OvmfPkg: PciHostBridgeLib: convert main loop from PciHostBridgeDxe In this patch we import the scan for extra root buses from the InitializePciHostBridge() function, in file "OvmfPkg/PciHostBridgeDxe/PciHostBridge.c". For the time being, the InitRootBridge() and UninitRootBridge() functions are just placeholders. The PciHostBridgeGetRootBridges() API expects us to return the PCI_ROOT_BRIDGE structures in a contiguous array, instead of a linked list. Therefore the following bits have to be converted manually: (1) The array is allocated in advance, in a single step. (2) The calculation of the array size depends on an explicit multiplication, which we must check against overflow. Since more than 255 extra root bridges make no sense anyway, we use (1 + 255) as the limit on the main plus all extra root bridges. This also ensures that the UINTN multiplication doesn't overflow. (3) The PciHostBridgeDxe code decrements "ExtraRootBridgesLeft" to terminate the scanning early. Here we need track the increasing count of used array elements as well, so we employ "ExtraRootBridges" as a constant limit, and increment the new local variable "Initialized". (4) The prototypes of InitRootBridge() and UninitRootBridge() reflect that the PCI_ROOT_BRIDGE structure is allocated by the caller; only in-place initialization is necessary. Additionally, macros are employed for standard PCI quantities, from "MdePkg/Include/IndustryStandard/Pci22.h": - MAX_PCI_DEVICE_NUMBER (31) is replaced with PCI_MAX_DEVICE (same), - the constant 255 is replaced with PCI_MAX_BUS, - the (RootBridgeNumber < 256) condition is replaced with (RootBridgeNumber <= PCI_MAX_BUS). Cc: Jordan Justen <jordan.l.justen@intel.com> Cc: Ruiyu Ni <ruiyu.ni@intel.com> Cc: Marcel Apfelbaum <marcel@redhat.com> Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Laszlo Ersek <lersek@redhat.com> Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2016-01-25 22:47:00 +01:00
/**
Return all the root bridge instances in an array.
@param Count Return the count of root bridge instances.
@return All the root bridge instances in an array.
The array should be passed into PciHostBridgeFreeRootBridges()
when it's not used.
**/
PCI_ROOT_BRIDGE *
EFIAPI
PciHostBridgeGetRootBridges (
UINTN *Count
)
{
UINT64 Attributes;
UINT64 AllocationAttributes;
PCI_ROOT_BRIDGE_APERTURE Io;
PCI_ROOT_BRIDGE_APERTURE Mem;
PCI_ROOT_BRIDGE_APERTURE MemAbove4G;
if (PcdGetBool (PcdPciDisableBusEnumeration)) {
return ScanForRootBridges (Count);
}
ZeroMem (&Io, sizeof (Io));
ZeroMem (&Mem, sizeof (Mem));
ZeroMem (&MemAbove4G, sizeof (MemAbove4G));
Attributes = EFI_PCI_ATTRIBUTE_IDE_PRIMARY_IO |
EFI_PCI_ATTRIBUTE_IDE_SECONDARY_IO |
EFI_PCI_ATTRIBUTE_ISA_IO_16 |
EFI_PCI_ATTRIBUTE_ISA_MOTHERBOARD_IO |
EFI_PCI_ATTRIBUTE_VGA_MEMORY |
EFI_PCI_ATTRIBUTE_VGA_IO_16 |
EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO_16;
AllocationAttributes = EFI_PCI_HOST_BRIDGE_COMBINE_MEM_PMEM;
if (PcdGet64 (PcdPciMmio64Size) > 0) {
AllocationAttributes |= EFI_PCI_HOST_BRIDGE_MEM64_DECODE;
MemAbove4G.Base = PcdGet64 (PcdPciMmio64Base);
MemAbove4G.Limit = PcdGet64 (PcdPciMmio64Base) +
PcdGet64 (PcdPciMmio64Size) - 1;
} else {
CopyMem (&MemAbove4G, &mNonExistAperture, sizeof (mNonExistAperture));
}
Io.Base = PcdGet64 (PcdPciIoBase);
Io.Limit = PcdGet64 (PcdPciIoBase) + (PcdGet64 (PcdPciIoSize) - 1);
Mem.Base = PcdGet64 (PcdPciMmio32Base);
Mem.Limit = PcdGet64 (PcdPciMmio32Base) + (PcdGet64 (PcdPciMmio32Size) - 1);
OvmfPkg: PciHostBridgeLib: convert main loop from PciHostBridgeDxe In this patch we import the scan for extra root buses from the InitializePciHostBridge() function, in file "OvmfPkg/PciHostBridgeDxe/PciHostBridge.c". For the time being, the InitRootBridge() and UninitRootBridge() functions are just placeholders. The PciHostBridgeGetRootBridges() API expects us to return the PCI_ROOT_BRIDGE structures in a contiguous array, instead of a linked list. Therefore the following bits have to be converted manually: (1) The array is allocated in advance, in a single step. (2) The calculation of the array size depends on an explicit multiplication, which we must check against overflow. Since more than 255 extra root bridges make no sense anyway, we use (1 + 255) as the limit on the main plus all extra root bridges. This also ensures that the UINTN multiplication doesn't overflow. (3) The PciHostBridgeDxe code decrements "ExtraRootBridgesLeft" to terminate the scanning early. Here we need track the increasing count of used array elements as well, so we employ "ExtraRootBridges" as a constant limit, and increment the new local variable "Initialized". (4) The prototypes of InitRootBridge() and UninitRootBridge() reflect that the PCI_ROOT_BRIDGE structure is allocated by the caller; only in-place initialization is necessary. Additionally, macros are employed for standard PCI quantities, from "MdePkg/Include/IndustryStandard/Pci22.h": - MAX_PCI_DEVICE_NUMBER (31) is replaced with PCI_MAX_DEVICE (same), - the constant 255 is replaced with PCI_MAX_BUS, - the (RootBridgeNumber < 256) condition is replaced with (RootBridgeNumber <= PCI_MAX_BUS). Cc: Jordan Justen <jordan.l.justen@intel.com> Cc: Ruiyu Ni <ruiyu.ni@intel.com> Cc: Marcel Apfelbaum <marcel@redhat.com> Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Laszlo Ersek <lersek@redhat.com> Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2016-01-25 22:47:00 +01:00
return PciHostBridgeUtilityGetRootBridges (
Count,
Attributes,
AllocationAttributes,
&Io,
&Mem,
&MemAbove4G,
&mNonExistAperture,
&mNonExistAperture
);
}
OvmfPkg: PciHostBridgeLib: convert main loop from PciHostBridgeDxe In this patch we import the scan for extra root buses from the InitializePciHostBridge() function, in file "OvmfPkg/PciHostBridgeDxe/PciHostBridge.c". For the time being, the InitRootBridge() and UninitRootBridge() functions are just placeholders. The PciHostBridgeGetRootBridges() API expects us to return the PCI_ROOT_BRIDGE structures in a contiguous array, instead of a linked list. Therefore the following bits have to be converted manually: (1) The array is allocated in advance, in a single step. (2) The calculation of the array size depends on an explicit multiplication, which we must check against overflow. Since more than 255 extra root bridges make no sense anyway, we use (1 + 255) as the limit on the main plus all extra root bridges. This also ensures that the UINTN multiplication doesn't overflow. (3) The PciHostBridgeDxe code decrements "ExtraRootBridgesLeft" to terminate the scanning early. Here we need track the increasing count of used array elements as well, so we employ "ExtraRootBridges" as a constant limit, and increment the new local variable "Initialized". (4) The prototypes of InitRootBridge() and UninitRootBridge() reflect that the PCI_ROOT_BRIDGE structure is allocated by the caller; only in-place initialization is necessary. Additionally, macros are employed for standard PCI quantities, from "MdePkg/Include/IndustryStandard/Pci22.h": - MAX_PCI_DEVICE_NUMBER (31) is replaced with PCI_MAX_DEVICE (same), - the constant 255 is replaced with PCI_MAX_BUS, - the (RootBridgeNumber < 256) condition is replaced with (RootBridgeNumber <= PCI_MAX_BUS). Cc: Jordan Justen <jordan.l.justen@intel.com> Cc: Ruiyu Ni <ruiyu.ni@intel.com> Cc: Marcel Apfelbaum <marcel@redhat.com> Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Laszlo Ersek <lersek@redhat.com> Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2016-01-25 22:47:00 +01:00
/**
Free the root bridge instances array returned from
PciHostBridgeGetRootBridges().
@param The root bridge instances array.
@param The count of the array.
**/
VOID
EFIAPI
PciHostBridgeFreeRootBridges (
PCI_ROOT_BRIDGE *Bridges,
UINTN Count
)
{
PciHostBridgeUtilityFreeRootBridges (Bridges, Count);
}
OvmfPkg: PciHostBridgeLib: convert main loop from PciHostBridgeDxe In this patch we import the scan for extra root buses from the InitializePciHostBridge() function, in file "OvmfPkg/PciHostBridgeDxe/PciHostBridge.c". For the time being, the InitRootBridge() and UninitRootBridge() functions are just placeholders. The PciHostBridgeGetRootBridges() API expects us to return the PCI_ROOT_BRIDGE structures in a contiguous array, instead of a linked list. Therefore the following bits have to be converted manually: (1) The array is allocated in advance, in a single step. (2) The calculation of the array size depends on an explicit multiplication, which we must check against overflow. Since more than 255 extra root bridges make no sense anyway, we use (1 + 255) as the limit on the main plus all extra root bridges. This also ensures that the UINTN multiplication doesn't overflow. (3) The PciHostBridgeDxe code decrements "ExtraRootBridgesLeft" to terminate the scanning early. Here we need track the increasing count of used array elements as well, so we employ "ExtraRootBridges" as a constant limit, and increment the new local variable "Initialized". (4) The prototypes of InitRootBridge() and UninitRootBridge() reflect that the PCI_ROOT_BRIDGE structure is allocated by the caller; only in-place initialization is necessary. Additionally, macros are employed for standard PCI quantities, from "MdePkg/Include/IndustryStandard/Pci22.h": - MAX_PCI_DEVICE_NUMBER (31) is replaced with PCI_MAX_DEVICE (same), - the constant 255 is replaced with PCI_MAX_BUS, - the (RootBridgeNumber < 256) condition is replaced with (RootBridgeNumber <= PCI_MAX_BUS). Cc: Jordan Justen <jordan.l.justen@intel.com> Cc: Ruiyu Ni <ruiyu.ni@intel.com> Cc: Marcel Apfelbaum <marcel@redhat.com> Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Laszlo Ersek <lersek@redhat.com> Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2016-01-25 22:47:00 +01:00
/**
Inform the platform that the resource conflict happens.
@param HostBridgeHandle Handle of the Host Bridge.
@param Configuration Pointer to PCI I/O and PCI memory resource
descriptors. The Configuration contains the resources
for all the root bridges. The resource for each root
bridge is terminated with END descriptor and an
additional END is appended indicating the end of the
entire resources. The resource descriptor field
values follow the description in
EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
.SubmitResources().
**/
VOID
EFIAPI
PciHostBridgeResourceConflict (
EFI_HANDLE HostBridgeHandle,
VOID *Configuration
)
{
PciHostBridgeUtilityResourceConflict (Configuration);
}