]> git.proxmox.com Git - mirror_edk2.git/blame - UefiPayloadPkg/Library/PciHostBridgeLib/PciHostBridgeLib.c
UefiPayloadPkg: Replace MEMROY_ENTRY by MEMORY_ENTRY
[mirror_edk2.git] / UefiPayloadPkg / Library / PciHostBridgeLib / PciHostBridgeLib.c
CommitLineData
04af8bf2
DG
1/** @file\r
2 Library instance of PciHostBridgeLib library class for coreboot.\r
3\r
4 Copyright (C) 2016, Red Hat, Inc.\r
99de2e7e 5 Copyright (c) 2016 - 2021, Intel Corporation. All rights reserved.<BR>\r
04af8bf2
DG
6\r
7 SPDX-License-Identifier: BSD-2-Clause-Patent\r
8\r
9**/\r
10#include <PiDxe.h>\r
11\r
12#include <IndustryStandard/Pci.h>\r
13#include <Protocol/PciHostBridgeResourceAllocation.h>\r
14#include <Protocol/PciRootBridgeIo.h>\r
15\r
16#include <Library/BaseMemoryLib.h>\r
17#include <Library/DebugLib.h>\r
18#include <Library/DevicePathLib.h>\r
19#include <Library/MemoryAllocationLib.h>\r
20#include <Library/PciHostBridgeLib.h>\r
21#include <Library/PciLib.h>\r
99de2e7e 22#include <Library/HobLib.h>\r
04af8bf2
DG
23\r
24#include "PciHostBridge.h"\r
25\r
26STATIC\r
27CONST\r
28CB_PCI_ROOT_BRIDGE_DEVICE_PATH mRootBridgeDevicePathTemplate = {\r
29 {\r
30 {\r
31 ACPI_DEVICE_PATH,\r
32 ACPI_DP,\r
33 {\r
34 (UINT8) (sizeof(ACPI_HID_DEVICE_PATH)),\r
35 (UINT8) ((sizeof(ACPI_HID_DEVICE_PATH)) >> 8)\r
36 }\r
37 },\r
38 EISA_PNP_ID(0x0A03), // HID\r
39 0 // UID\r
40 },\r
41\r
42 {\r
43 END_DEVICE_PATH_TYPE,\r
44 END_ENTIRE_DEVICE_PATH_SUBTYPE,\r
45 {\r
46 END_DEVICE_PATH_LENGTH,\r
47 0\r
48 }\r
49 }\r
50};\r
51\r
04af8bf2
DG
52/**\r
53 Initialize a PCI_ROOT_BRIDGE structure.\r
54\r
55 @param[in] Supports Supported attributes.\r
56\r
57 @param[in] Attributes Initial attributes.\r
58\r
59 @param[in] AllocAttributes Allocation attributes.\r
60\r
61 @param[in] RootBusNumber The bus number to store in RootBus.\r
62\r
63 @param[in] MaxSubBusNumber The inclusive maximum bus number that can be\r
64 assigned to any subordinate bus found behind any\r
65 PCI bridge hanging off this root bus.\r
66\r
67 The caller is responsible for ensuring that\r
68 RootBusNumber <= MaxSubBusNumber. If\r
69 RootBusNumber equals MaxSubBusNumber, then the\r
70 root bus has no room for subordinate buses.\r
71\r
72 @param[in] Io IO aperture.\r
73\r
74 @param[in] Mem MMIO aperture.\r
75\r
76 @param[in] MemAbove4G MMIO aperture above 4G.\r
77\r
78 @param[in] PMem Prefetchable MMIO aperture.\r
79\r
80 @param[in] PMemAbove4G Prefetchable MMIO aperture above 4G.\r
81\r
82 @param[out] RootBus The PCI_ROOT_BRIDGE structure (allocated by the\r
83 caller) that should be filled in by this\r
84 function.\r
85\r
86 @retval EFI_SUCCESS Initialization successful. A device path\r
87 consisting of an ACPI device path node, with\r
88 UID = RootBusNumber, has been allocated and\r
89 linked into RootBus.\r
90\r
91 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.\r
92**/\r
93EFI_STATUS\r
94InitRootBridge (\r
95 IN UINT64 Supports,\r
96 IN UINT64 Attributes,\r
97 IN UINT64 AllocAttributes,\r
98 IN UINT8 RootBusNumber,\r
99 IN UINT8 MaxSubBusNumber,\r
100 IN PCI_ROOT_BRIDGE_APERTURE *Io,\r
101 IN PCI_ROOT_BRIDGE_APERTURE *Mem,\r
102 IN PCI_ROOT_BRIDGE_APERTURE *MemAbove4G,\r
103 IN PCI_ROOT_BRIDGE_APERTURE *PMem,\r
104 IN PCI_ROOT_BRIDGE_APERTURE *PMemAbove4G,\r
105 OUT PCI_ROOT_BRIDGE *RootBus\r
106)\r
107{\r
108 CB_PCI_ROOT_BRIDGE_DEVICE_PATH *DevicePath;\r
109\r
110 //\r
111 // Be safe if other fields are added to PCI_ROOT_BRIDGE later.\r
112 //\r
113 ZeroMem (RootBus, sizeof *RootBus);\r
114\r
115 RootBus->Segment = 0;\r
116\r
117 RootBus->Supports = Supports;\r
118 RootBus->Attributes = Attributes;\r
119\r
120 RootBus->DmaAbove4G = FALSE;\r
121\r
122 RootBus->AllocationAttributes = AllocAttributes;\r
123 RootBus->Bus.Base = RootBusNumber;\r
124 RootBus->Bus.Limit = MaxSubBusNumber;\r
125 CopyMem (&RootBus->Io, Io, sizeof (*Io));\r
126 CopyMem (&RootBus->Mem, Mem, sizeof (*Mem));\r
127 CopyMem (&RootBus->MemAbove4G, MemAbove4G, sizeof (*MemAbove4G));\r
128 CopyMem (&RootBus->PMem, PMem, sizeof (*PMem));\r
129 CopyMem (&RootBus->PMemAbove4G, PMemAbove4G, sizeof (*PMemAbove4G));\r
130\r
131 RootBus->NoExtendedConfigSpace = FALSE;\r
132\r
133 DevicePath = AllocateCopyPool (sizeof (mRootBridgeDevicePathTemplate),\r
134 &mRootBridgeDevicePathTemplate);\r
135 if (DevicePath == NULL) {\r
136 DEBUG ((DEBUG_ERROR, "%a: %r\n", __FUNCTION__, EFI_OUT_OF_RESOURCES));\r
137 return EFI_OUT_OF_RESOURCES;\r
138 }\r
139 DevicePath->AcpiDevicePath.UID = RootBusNumber;\r
140 RootBus->DevicePath = (EFI_DEVICE_PATH_PROTOCOL *)DevicePath;\r
141\r
142 DEBUG ((DEBUG_INFO,\r
143 "%a: populated root bus %d, with room for %d subordinate bus(es)\n",\r
144 __FUNCTION__, RootBusNumber, MaxSubBusNumber - RootBusNumber));\r
145 return EFI_SUCCESS;\r
146}\r
147\r
99de2e7e
ZL
148/**\r
149 Initialize DevicePath for a PCI_ROOT_BRIDGE.\r
150 @param[in] HID HID for device path\r
151 @param[in] UID UID for device path\r
152\r
153 @retval A pointer to the new created device patch.\r
154**/\r
155EFI_DEVICE_PATH_PROTOCOL *\r
156CreateRootBridgeDevicePath (\r
157 IN UINT32 HID,\r
158 IN UINT32 UID\r
159)\r
160{\r
161 CB_PCI_ROOT_BRIDGE_DEVICE_PATH *DevicePath;\r
162 DevicePath = AllocateCopyPool (sizeof (mRootBridgeDevicePathTemplate),\r
163 &mRootBridgeDevicePathTemplate);\r
164 ASSERT (DevicePath != NULL);\r
165 DevicePath->AcpiDevicePath.HID = HID;\r
166 DevicePath->AcpiDevicePath.UID = UID;\r
167 return (EFI_DEVICE_PATH_PROTOCOL *)DevicePath;\r
168}\r
04af8bf2
DG
169\r
170/**\r
171 Return all the root bridge instances in an array.\r
172\r
173 @param Count Return the count of root bridge instances.\r
174\r
175 @return All the root bridge instances in an array.\r
176 The array should be passed into PciHostBridgeFreeRootBridges()\r
177 when it's not used.\r
178**/\r
179PCI_ROOT_BRIDGE *\r
180EFIAPI\r
181PciHostBridgeGetRootBridges (\r
182 UINTN *Count\r
183)\r
184{\r
99de2e7e
ZL
185 UNIVERSAL_PAYLOAD_PCI_ROOT_BRIDGES *PciRootBridgeInfo;\r
186 EFI_HOB_GUID_TYPE *GuidHob;\r
187 UNIVERSAL_PAYLOAD_GENERIC_HEADER *GenericHeader;\r
188 //\r
189 // Find Universal Payload PCI Root Bridge Info hob\r
190 //\r
191 GuidHob = GetFirstGuidHob (&gUniversalPayloadPciRootBridgeInfoGuid);\r
192 if (GuidHob != NULL) {\r
193 GenericHeader = (UNIVERSAL_PAYLOAD_GENERIC_HEADER *) GET_GUID_HOB_DATA (GuidHob);\r
194 if ((sizeof(UNIVERSAL_PAYLOAD_GENERIC_HEADER) <= GET_GUID_HOB_DATA_SIZE (GuidHob)) && (GenericHeader->Length <= GET_GUID_HOB_DATA_SIZE (GuidHob))) {\r
195 if ((GenericHeader->Revision == UNIVERSAL_PAYLOAD_PCI_ROOT_BRIDGES_REVISION) && (GenericHeader->Length >= sizeof (UNIVERSAL_PAYLOAD_PCI_ROOT_BRIDGES))) {\r
196 //\r
197 // UNIVERSAL_PAYLOAD_PCI_ROOT_BRIDGES structure is used when Revision equals to UNIVERSAL_PAYLOAD_PCI_ROOT_BRIDGES_REVISION\r
198 //\r
199 PciRootBridgeInfo = (UNIVERSAL_PAYLOAD_PCI_ROOT_BRIDGES *) GET_GUID_HOB_DATA (GuidHob);\r
200 if (PciRootBridgeInfo->Count <= (GET_GUID_HOB_DATA_SIZE (GuidHob) - sizeof(UNIVERSAL_PAYLOAD_PCI_ROOT_BRIDGES)) / sizeof(UNIVERSAL_PAYLOAD_PCI_ROOT_BRIDGE)) {\r
201 return RetrieveRootBridgeInfoFromHob (PciRootBridgeInfo, Count);\r
202 }\r
203 }\r
204 }\r
205 }\r
04af8bf2
DG
206 return ScanForRootBridges (Count);\r
207}\r
208\r
04af8bf2
DG
209/**\r
210 Free the root bridge instances array returned from\r
211 PciHostBridgeGetRootBridges().\r
212\r
213 @param The root bridge instances array.\r
214 @param The count of the array.\r
215**/\r
216VOID\r
217EFIAPI\r
218PciHostBridgeFreeRootBridges (\r
219 PCI_ROOT_BRIDGE *Bridges,\r
220 UINTN Count\r
221)\r
222{\r
223 if (Bridges == NULL && Count == 0) {\r
224 return;\r
225 }\r
226 ASSERT (Bridges != NULL && Count > 0);\r
227\r
228 do {\r
229 --Count;\r
230 FreePool (Bridges[Count].DevicePath);\r
231 } while (Count > 0);\r
232\r
233 FreePool (Bridges);\r
234}\r
235\r
236\r
237/**\r
238 Inform the platform that the resource conflict happens.\r
239\r
240 @param HostBridgeHandle Handle of the Host Bridge.\r
241 @param Configuration Pointer to PCI I/O and PCI memory resource\r
242 descriptors. The Configuration contains the resources\r
243 for all the root bridges. The resource for each root\r
244 bridge is terminated with END descriptor and an\r
245 additional END is appended indicating the end of the\r
246 entire resources. The resource descriptor field\r
247 values follow the description in\r
248 EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL\r
249 .SubmitResources().\r
250**/\r
251VOID\r
252EFIAPI\r
253PciHostBridgeResourceConflict (\r
254 EFI_HANDLE HostBridgeHandle,\r
255 VOID *Configuration\r
256)\r
257{\r
258 //\r
259 // coreboot UEFI Payload does not do PCI enumeration and should not call this\r
260 // library interface.\r
261 //\r
262 ASSERT (FALSE);\r
263}\r