]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Library/PciHostBridgeLib/PciHostBridgeLib.c
OvmfPkg: Make more use of ARRAY_SIZE()
[mirror_edk2.git] / OvmfPkg / Library / PciHostBridgeLib / PciHostBridgeLib.c
CommitLineData
d85861d7
LE
1/** @file\r
2 OVMF's instance of the PCI Host Bridge Library.\r
3\r
4 Copyright (C) 2016, Red Hat, Inc.\r
5 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>\r
6\r
7 This program and the accompanying materials are licensed and made available\r
8 under the terms and conditions of the BSD License which accompanies this\r
9 distribution. The full text of the license may be found at\r
10 http://opensource.org/licenses/bsd-license.php.\r
11\r
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r
13 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15**/\r
16#include <PiDxe.h>\r
46e46eaf
LE
17\r
18#include <IndustryStandard/Pci.h>\r
7daf2401 19#include <IndustryStandard/Q35MchIch9.h>\r
46e46eaf 20\r
0dcd1b1b 21#include <Protocol/PciHostBridgeResourceAllocation.h>\r
a5ece62d
LE
22#include <Protocol/PciRootBridgeIo.h>\r
23\r
65de2ef5 24#include <Library/BaseMemoryLib.h>\r
d85861d7 25#include <Library/DebugLib.h>\r
1f4e2299 26#include <Library/DevicePathLib.h>\r
46e46eaf
LE
27#include <Library/MemoryAllocationLib.h>\r
28#include <Library/PciHostBridgeLib.h>\r
29#include <Library/PciLib.h>\r
30#include <Library/QemuFwCfgLib.h>\r
49effaf2 31#include "PciHostBridge.h"\r
46e46eaf 32\r
d85861d7 33\r
1f4e2299
LE
34#pragma pack(1)\r
35typedef struct {\r
36 ACPI_HID_DEVICE_PATH AcpiDevicePath;\r
37 EFI_DEVICE_PATH_PROTOCOL EndDevicePath;\r
38} OVMF_PCI_ROOT_BRIDGE_DEVICE_PATH;\r
39#pragma pack ()\r
40\r
41\r
d85861d7
LE
42GLOBAL_REMOVE_IF_UNREFERENCED\r
43CHAR16 *mPciHostBridgeLibAcpiAddressSpaceTypeStr[] = {\r
44 L"Mem", L"I/O", L"Bus"\r
45};\r
46\r
46e46eaf 47\r
1f4e2299
LE
48STATIC\r
49CONST\r
50OVMF_PCI_ROOT_BRIDGE_DEVICE_PATH mRootBridgeDevicePathTemplate = {\r
51 {\r
52 {\r
53 ACPI_DEVICE_PATH,\r
54 ACPI_DP,\r
55 {\r
56 (UINT8) (sizeof(ACPI_HID_DEVICE_PATH)),\r
57 (UINT8) ((sizeof(ACPI_HID_DEVICE_PATH)) >> 8)\r
58 }\r
59 },\r
60 EISA_PNP_ID(0x0A03), // HID\r
61 0 // UID\r
62 },\r
63\r
64 {\r
65 END_DEVICE_PATH_TYPE,\r
66 END_ENTIRE_DEVICE_PATH_SUBTYPE,\r
67 {\r
68 END_DEVICE_PATH_LENGTH,\r
69 0\r
70 }\r
71 }\r
72};\r
73\r
c0a2591b 74STATIC PCI_ROOT_BRIDGE_APERTURE mNonExistAperture = { MAX_UINT64, 0 };\r
1f4e2299 75\r
46e46eaf
LE
76/**\r
77 Initialize a PCI_ROOT_BRIDGE structure.\r
78\r
c0a2591b 79 @param[in] Supports Supported attributes.\r
46e46eaf 80\r
c0a2591b
RN
81 @param[in] Attributes Initial attributes.\r
82\r
83 @param[in] AllocAttributes Allocation attributes.\r
84\r
85 @param[in] RootBusNumber The bus number to store in RootBus.\r
86\r
87 @param[in] MaxSubBusNumber The inclusive maximum bus number that can be\r
46e46eaf
LE
88 assigned to any subordinate bus found behind any\r
89 PCI bridge hanging off this root bus.\r
90\r
91 The caller is repsonsible for ensuring that\r
92 RootBusNumber <= MaxSubBusNumber. If\r
93 RootBusNumber equals MaxSubBusNumber, then the\r
94 root bus has no room for subordinate buses.\r
95\r
c0a2591b
RN
96 @param[in] Io IO aperture.\r
97\r
98 @param[in] Mem MMIO aperture.\r
99\r
100 @param[in] MemAbove4G MMIO aperture above 4G.\r
101\r
102 @param[in] PMem Prefetchable MMIO aperture.\r
103\r
104 @param[in] PMemAbove4G Prefetchable MMIO aperture above 4G.\r
105\r
106 @param[out] RootBus The PCI_ROOT_BRIDGE structure (allocated by the\r
46e46eaf
LE
107 caller) that should be filled in by this\r
108 function.\r
109\r
110 @retval EFI_SUCCESS Initialization successful. A device path\r
111 consisting of an ACPI device path node, with\r
112 UID = RootBusNumber, has been allocated and\r
113 linked into RootBus.\r
114\r
115 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.\r
116**/\r
46e46eaf
LE
117EFI_STATUS\r
118InitRootBridge (\r
c0a2591b
RN
119 IN UINT64 Supports,\r
120 IN UINT64 Attributes,\r
121 IN UINT64 AllocAttributes,\r
122 IN UINT8 RootBusNumber,\r
123 IN UINT8 MaxSubBusNumber,\r
124 IN PCI_ROOT_BRIDGE_APERTURE *Io,\r
125 IN PCI_ROOT_BRIDGE_APERTURE *Mem,\r
126 IN PCI_ROOT_BRIDGE_APERTURE *MemAbove4G,\r
127 IN PCI_ROOT_BRIDGE_APERTURE *PMem,\r
128 IN PCI_ROOT_BRIDGE_APERTURE *PMemAbove4G,\r
129 OUT PCI_ROOT_BRIDGE *RootBus\r
46e46eaf
LE
130 )\r
131{\r
1f4e2299
LE
132 OVMF_PCI_ROOT_BRIDGE_DEVICE_PATH *DevicePath;\r
133\r
65de2ef5
LE
134 //\r
135 // Be safe if other fields are added to PCI_ROOT_BRIDGE later.\r
136 //\r
137 ZeroMem (RootBus, sizeof *RootBus);\r
138\r
139 RootBus->Segment = 0;\r
140\r
c0a2591b
RN
141 RootBus->Supports = Supports;\r
142 RootBus->Attributes = Attributes;\r
a5ece62d 143\r
c789d61d
LE
144 RootBus->DmaAbove4G = FALSE;\r
145\r
c0a2591b 146 RootBus->AllocationAttributes = AllocAttributes;\r
390951c2
LE
147 RootBus->Bus.Base = RootBusNumber;\r
148 RootBus->Bus.Limit = MaxSubBusNumber;\r
c0a2591b
RN
149 CopyMem (&RootBus->Io, Io, sizeof (*Io));\r
150 CopyMem (&RootBus->Mem, Mem, sizeof (*Mem));\r
151 CopyMem (&RootBus->MemAbove4G, MemAbove4G, sizeof (*MemAbove4G));\r
152 CopyMem (&RootBus->PMem, PMem, sizeof (*PMem));\r
153 CopyMem (&RootBus->PMemAbove4G, PMemAbove4G, sizeof (*PMemAbove4G));\r
390951c2 154\r
7daf2401
LE
155 RootBus->NoExtendedConfigSpace = (PcdGet16 (PcdOvmfHostBridgePciDevId) !=\r
156 INTEL_Q35_MCH_DEVICE_ID);\r
cb81595b 157\r
1f4e2299
LE
158 DevicePath = AllocateCopyPool (sizeof mRootBridgeDevicePathTemplate,\r
159 &mRootBridgeDevicePathTemplate);\r
160 if (DevicePath == NULL) {\r
161 DEBUG ((EFI_D_ERROR, "%a: %r\n", __FUNCTION__, EFI_OUT_OF_RESOURCES));\r
162 return EFI_OUT_OF_RESOURCES;\r
163 }\r
164 DevicePath->AcpiDevicePath.UID = RootBusNumber;\r
165 RootBus->DevicePath = (EFI_DEVICE_PATH_PROTOCOL *)DevicePath;\r
166\r
167 DEBUG ((EFI_D_INFO,\r
168 "%a: populated root bus %d, with room for %d subordinate bus(es)\n",\r
169 __FUNCTION__, RootBusNumber, MaxSubBusNumber - RootBusNumber));\r
170 return EFI_SUCCESS;\r
46e46eaf
LE
171}\r
172\r
173\r
174/**\r
175 Uninitialize a PCI_ROOT_BRIDGE structure set up with InitRootBridge().\r
176\r
177 param[in] RootBus The PCI_ROOT_BRIDGE structure, allocated by the caller and\r
178 initialized with InitRootBridge(), that should be\r
179 uninitialized. This function doesn't free RootBus.\r
180**/\r
181STATIC\r
182VOID\r
183UninitRootBridge (\r
184 IN PCI_ROOT_BRIDGE *RootBus\r
185 )\r
186{\r
1f4e2299 187 FreePool (RootBus->DevicePath);\r
46e46eaf
LE
188}\r
189\r
190\r
d85861d7
LE
191/**\r
192 Return all the root bridge instances in an array.\r
193\r
194 @param Count Return the count of root bridge instances.\r
195\r
196 @return All the root bridge instances in an array.\r
197 The array should be passed into PciHostBridgeFreeRootBridges()\r
198 when it's not used.\r
199**/\r
200PCI_ROOT_BRIDGE *\r
201EFIAPI\r
202PciHostBridgeGetRootBridges (\r
203 UINTN *Count\r
204 )\r
205{\r
46e46eaf
LE
206 EFI_STATUS Status;\r
207 FIRMWARE_CONFIG_ITEM FwCfgItem;\r
208 UINTN FwCfgSize;\r
209 UINT64 ExtraRootBridges;\r
210 PCI_ROOT_BRIDGE *Bridges;\r
211 UINTN Initialized;\r
212 UINTN LastRootBridgeNumber;\r
213 UINTN RootBridgeNumber;\r
c0a2591b
RN
214 UINT64 Attributes;\r
215 UINT64 AllocationAttributes;\r
216 PCI_ROOT_BRIDGE_APERTURE Io;\r
217 PCI_ROOT_BRIDGE_APERTURE Mem;\r
218 PCI_ROOT_BRIDGE_APERTURE MemAbove4G;\r
219\r
49effaf2
RN
220 if (PcdGetBool (PcdPciDisableBusEnumeration)) {\r
221 return ScanForRootBridges (Count);\r
222 }\r
223\r
c0a2591b
RN
224 Attributes = EFI_PCI_ATTRIBUTE_IDE_PRIMARY_IO |\r
225 EFI_PCI_ATTRIBUTE_IDE_SECONDARY_IO |\r
226 EFI_PCI_ATTRIBUTE_ISA_IO_16 |\r
227 EFI_PCI_ATTRIBUTE_ISA_MOTHERBOARD_IO |\r
228 EFI_PCI_ATTRIBUTE_VGA_MEMORY |\r
229 EFI_PCI_ATTRIBUTE_VGA_IO_16 |\r
230 EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO_16;\r
231\r
232 AllocationAttributes = EFI_PCI_HOST_BRIDGE_COMBINE_MEM_PMEM;\r
233 if (PcdGet64 (PcdPciMmio64Size) > 0) {\r
234 AllocationAttributes |= EFI_PCI_HOST_BRIDGE_MEM64_DECODE;\r
235 MemAbove4G.Base = PcdGet64 (PcdPciMmio64Base);\r
236 MemAbove4G.Limit = PcdGet64 (PcdPciMmio64Base) +\r
237 PcdGet64 (PcdPciMmio64Size) - 1;\r
238 } else {\r
239 CopyMem (&MemAbove4G, &mNonExistAperture, sizeof (mNonExistAperture));\r
240 }\r
241\r
242 Io.Base = PcdGet64 (PcdPciIoBase);\r
243 Io.Limit = PcdGet64 (PcdPciIoBase) + (PcdGet64 (PcdPciIoSize) - 1);\r
244 Mem.Base = PcdGet64 (PcdPciMmio32Base);\r
245 Mem.Limit = PcdGet64 (PcdPciMmio32Base) + (PcdGet64 (PcdPciMmio32Size) - 1);\r
46e46eaf 246\r
d85861d7 247 *Count = 0;\r
46e46eaf
LE
248\r
249 //\r
250 // QEMU provides the number of extra root buses, shortening the exhaustive\r
251 // search below. If there is no hint, the feature is missing.\r
252 //\r
253 Status = QemuFwCfgFindFile ("etc/extra-pci-roots", &FwCfgItem, &FwCfgSize);\r
254 if (EFI_ERROR (Status) || FwCfgSize != sizeof ExtraRootBridges) {\r
255 ExtraRootBridges = 0;\r
256 } else {\r
257 QemuFwCfgSelectItem (FwCfgItem);\r
258 QemuFwCfgReadBytes (FwCfgSize, &ExtraRootBridges);\r
259\r
260 if (ExtraRootBridges > PCI_MAX_BUS) {\r
261 DEBUG ((EFI_D_ERROR, "%a: invalid count of extra root buses (%Lu) "\r
262 "reported by QEMU\n", __FUNCTION__, ExtraRootBridges));\r
263 return NULL;\r
264 }\r
265 DEBUG ((EFI_D_INFO, "%a: %Lu extra root buses reported by QEMU\n",\r
266 __FUNCTION__, ExtraRootBridges));\r
267 }\r
268\r
269 //\r
270 // Allocate the "main" root bridge, and any extra root bridges.\r
271 //\r
272 Bridges = AllocatePool ((1 + (UINTN)ExtraRootBridges) * sizeof *Bridges);\r
273 if (Bridges == NULL) {\r
274 DEBUG ((EFI_D_ERROR, "%a: %r\n", __FUNCTION__, EFI_OUT_OF_RESOURCES));\r
275 return NULL;\r
276 }\r
277 Initialized = 0;\r
278\r
279 //\r
280 // The "main" root bus is always there.\r
281 //\r
282 LastRootBridgeNumber = 0;\r
283\r
284 //\r
285 // Scan all other root buses. If function 0 of any device on a bus returns a\r
286 // VendorId register value different from all-bits-one, then that bus is\r
287 // alive.\r
288 //\r
289 for (RootBridgeNumber = 1;\r
290 RootBridgeNumber <= PCI_MAX_BUS && Initialized < ExtraRootBridges;\r
291 ++RootBridgeNumber) {\r
292 UINTN Device;\r
293\r
294 for (Device = 0; Device <= PCI_MAX_DEVICE; ++Device) {\r
295 if (PciRead16 (PCI_LIB_ADDRESS (RootBridgeNumber, Device, 0,\r
296 PCI_VENDOR_ID_OFFSET)) != MAX_UINT16) {\r
297 break;\r
298 }\r
299 }\r
300 if (Device <= PCI_MAX_DEVICE) {\r
301 //\r
302 // Found the next root bus. We can now install the *previous* one,\r
303 // because now we know how big a bus number range *that* one has, for any\r
304 // subordinate buses that might exist behind PCI bridges hanging off it.\r
305 //\r
c0a2591b
RN
306 Status = InitRootBridge (\r
307 Attributes,\r
308 Attributes,\r
309 AllocationAttributes,\r
310 (UINT8) LastRootBridgeNumber,\r
311 (UINT8) (RootBridgeNumber - 1),\r
312 &Io,\r
313 &Mem,\r
314 &MemAbove4G,\r
315 &mNonExistAperture,\r
316 &mNonExistAperture,\r
317 &Bridges[Initialized]\r
318 );\r
46e46eaf
LE
319 if (EFI_ERROR (Status)) {\r
320 goto FreeBridges;\r
321 }\r
322 ++Initialized;\r
323 LastRootBridgeNumber = RootBridgeNumber;\r
324 }\r
325 }\r
326\r
327 //\r
328 // Install the last root bus (which might be the only, ie. main, root bus, if\r
329 // we've found no extra root buses).\r
330 //\r
c0a2591b
RN
331 Status = InitRootBridge (\r
332 Attributes,\r
333 Attributes,\r
334 AllocationAttributes,\r
335 (UINT8) LastRootBridgeNumber,\r
336 PCI_MAX_BUS,\r
337 &Io,\r
338 &Mem,\r
339 &MemAbove4G,\r
340 &mNonExistAperture,\r
341 &mNonExistAperture,\r
342 &Bridges[Initialized]\r
343 );\r
46e46eaf
LE
344 if (EFI_ERROR (Status)) {\r
345 goto FreeBridges;\r
346 }\r
347 ++Initialized;\r
348\r
349 *Count = Initialized;\r
350 return Bridges;\r
351\r
352FreeBridges:\r
353 while (Initialized > 0) {\r
354 --Initialized;\r
355 UninitRootBridge (&Bridges[Initialized]);\r
356 }\r
357\r
358 FreePool (Bridges);\r
d85861d7
LE
359 return NULL;\r
360}\r
361\r
46e46eaf 362\r
d85861d7
LE
363/**\r
364 Free the root bridge instances array returned from\r
365 PciHostBridgeGetRootBridges().\r
366\r
367 @param The root bridge instances array.\r
368 @param The count of the array.\r
369**/\r
370VOID\r
371EFIAPI\r
372PciHostBridgeFreeRootBridges (\r
373 PCI_ROOT_BRIDGE *Bridges,\r
374 UINTN Count\r
375 )\r
376{\r
dc4d6467
LE
377 if (Bridges == NULL && Count == 0) {\r
378 return;\r
379 }\r
380 ASSERT (Bridges != NULL && Count > 0);\r
381\r
382 do {\r
383 --Count;\r
384 UninitRootBridge (&Bridges[Count]);\r
385 } while (Count > 0);\r
386\r
387 FreePool (Bridges);\r
d85861d7
LE
388}\r
389\r
46e46eaf 390\r
d85861d7
LE
391/**\r
392 Inform the platform that the resource conflict happens.\r
393\r
394 @param HostBridgeHandle Handle of the Host Bridge.\r
395 @param Configuration Pointer to PCI I/O and PCI memory resource\r
396 descriptors. The Configuration contains the resources\r
397 for all the root bridges. The resource for each root\r
398 bridge is terminated with END descriptor and an\r
399 additional END is appended indicating the end of the\r
400 entire resources. The resource descriptor field\r
401 values follow the description in\r
402 EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL\r
403 .SubmitResources().\r
404**/\r
405VOID\r
406EFIAPI\r
407PciHostBridgeResourceConflict (\r
408 EFI_HANDLE HostBridgeHandle,\r
409 VOID *Configuration\r
410 )\r
411{\r
412 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptor;\r
413 UINTN RootBridgeIndex;\r
414 DEBUG ((EFI_D_ERROR, "PciHostBridge: Resource conflict happens!\n"));\r
415\r
416 RootBridgeIndex = 0;\r
417 Descriptor = (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *) Configuration;\r
418 while (Descriptor->Desc == ACPI_ADDRESS_SPACE_DESCRIPTOR) {\r
419 DEBUG ((EFI_D_ERROR, "RootBridge[%d]:\n", RootBridgeIndex++));\r
420 for (; Descriptor->Desc == ACPI_ADDRESS_SPACE_DESCRIPTOR; Descriptor++) {\r
421 ASSERT (Descriptor->ResType <\r
5a5025e4 422 ARRAY_SIZE (mPciHostBridgeLibAcpiAddressSpaceTypeStr)\r
d85861d7
LE
423 );\r
424 DEBUG ((EFI_D_ERROR, " %s: Length/Alignment = 0x%lx / 0x%lx\n",\r
425 mPciHostBridgeLibAcpiAddressSpaceTypeStr[Descriptor->ResType],\r
426 Descriptor->AddrLen, Descriptor->AddrRangeMax\r
427 ));\r
428 if (Descriptor->ResType == ACPI_ADDRESS_SPACE_TYPE_MEM) {\r
429 DEBUG ((EFI_D_ERROR, " Granularity/SpecificFlag = %ld / %02x%s\n",\r
430 Descriptor->AddrSpaceGranularity, Descriptor->SpecificFlag,\r
431 ((Descriptor->SpecificFlag &\r
432 EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE\r
433 ) != 0) ? L" (Prefetchable)" : L""\r
434 ));\r
435 }\r
436 }\r
437 //\r
438 // Skip the END descriptor for root bridge\r
439 //\r
440 ASSERT (Descriptor->Desc == ACPI_END_TAG_DESCRIPTOR);\r
441 Descriptor = (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *)(\r
442 (EFI_ACPI_END_TAG_DESCRIPTOR *)Descriptor + 1\r
443 );\r
444 }\r
445}\r