]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Library/PciHostBridgeLib/PciHostBridgeLib.c
OvmfPkg: PciHostBridgeLib: set bus, IO and 32-bit MMIO windows in RootBus
[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
19\r
0dcd1b1b 20#include <Protocol/PciHostBridgeResourceAllocation.h>\r
a5ece62d
LE
21#include <Protocol/PciRootBridgeIo.h>\r
22\r
65de2ef5 23#include <Library/BaseMemoryLib.h>\r
d85861d7 24#include <Library/DebugLib.h>\r
46e46eaf
LE
25#include <Library/MemoryAllocationLib.h>\r
26#include <Library/PciHostBridgeLib.h>\r
27#include <Library/PciLib.h>\r
28#include <Library/QemuFwCfgLib.h>\r
29\r
d85861d7
LE
30\r
31GLOBAL_REMOVE_IF_UNREFERENCED\r
32CHAR16 *mPciHostBridgeLibAcpiAddressSpaceTypeStr[] = {\r
33 L"Mem", L"I/O", L"Bus"\r
34};\r
35\r
46e46eaf
LE
36\r
37/**\r
38 Initialize a PCI_ROOT_BRIDGE structure.\r
39\r
40 param[in] RootBusNumber The bus number to store in RootBus.\r
41\r
42 param[in] MaxSubBusNumber The inclusive maximum bus number that can be\r
43 assigned to any subordinate bus found behind any\r
44 PCI bridge hanging off this root bus.\r
45\r
46 The caller is repsonsible for ensuring that\r
47 RootBusNumber <= MaxSubBusNumber. If\r
48 RootBusNumber equals MaxSubBusNumber, then the\r
49 root bus has no room for subordinate buses.\r
50\r
51 param[out] RootBus The PCI_ROOT_BRIDGE structure (allocated by the\r
52 caller) that should be filled in by this\r
53 function.\r
54\r
55 @retval EFI_SUCCESS Initialization successful. A device path\r
56 consisting of an ACPI device path node, with\r
57 UID = RootBusNumber, has been allocated and\r
58 linked into RootBus.\r
59\r
60 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.\r
61**/\r
62STATIC\r
63EFI_STATUS\r
64InitRootBridge (\r
65 IN UINT8 RootBusNumber,\r
66 IN UINT8 MaxSubBusNumber,\r
67 OUT PCI_ROOT_BRIDGE *RootBus\r
68 )\r
69{\r
65de2ef5
LE
70 //\r
71 // Be safe if other fields are added to PCI_ROOT_BRIDGE later.\r
72 //\r
73 ZeroMem (RootBus, sizeof *RootBus);\r
74\r
75 RootBus->Segment = 0;\r
76\r
a5ece62d
LE
77 RootBus->Supports = EFI_PCI_ATTRIBUTE_IDE_PRIMARY_IO |\r
78 EFI_PCI_ATTRIBUTE_IDE_SECONDARY_IO |\r
79 EFI_PCI_ATTRIBUTE_ISA_IO_16 |\r
80 EFI_PCI_ATTRIBUTE_ISA_MOTHERBOARD_IO |\r
81 EFI_PCI_ATTRIBUTE_VGA_MEMORY |\r
82 EFI_PCI_ATTRIBUTE_VGA_IO_16 |\r
83 EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO_16;\r
84 RootBus->Attributes = RootBus->Supports;\r
85\r
c789d61d
LE
86 RootBus->DmaAbove4G = FALSE;\r
87\r
0dcd1b1b
LE
88 RootBus->AllocationAttributes = EFI_PCI_HOST_BRIDGE_COMBINE_MEM_PMEM;\r
89 RootBus->PMem.Base = 0;\r
90 RootBus->PMem.Limit = 0;\r
91 RootBus->PMemAbove4G.Base = 0;\r
92 RootBus->PMemAbove4G.Limit = 0;\r
93 RootBus->MemAbove4G.Base = 0;\r
94 RootBus->MemAbove4G.Limit = 0;\r
95\r
390951c2
LE
96 RootBus->Bus.Base = RootBusNumber;\r
97 RootBus->Bus.Limit = MaxSubBusNumber;\r
98 RootBus->Io.Base = PcdGet64 (PcdPciIoBase);\r
99 RootBus->Io.Limit = PcdGet64 (PcdPciIoBase) + (PcdGet64 (PcdPciIoSize) - 1);\r
100 RootBus->Mem.Base = PcdGet64 (PcdPciMmio32Base);\r
101 RootBus->Mem.Limit = PcdGet64 (PcdPciMmio32Base) +\r
102 (PcdGet64 (PcdPciMmio32Size) - 1);\r
103\r
46e46eaf
LE
104 return EFI_OUT_OF_RESOURCES;\r
105}\r
106\r
107\r
108/**\r
109 Uninitialize a PCI_ROOT_BRIDGE structure set up with InitRootBridge().\r
110\r
111 param[in] RootBus The PCI_ROOT_BRIDGE structure, allocated by the caller and\r
112 initialized with InitRootBridge(), that should be\r
113 uninitialized. This function doesn't free RootBus.\r
114**/\r
115STATIC\r
116VOID\r
117UninitRootBridge (\r
118 IN PCI_ROOT_BRIDGE *RootBus\r
119 )\r
120{\r
121}\r
122\r
123\r
d85861d7
LE
124/**\r
125 Return all the root bridge instances in an array.\r
126\r
127 @param Count Return the count of root bridge instances.\r
128\r
129 @return All the root bridge instances in an array.\r
130 The array should be passed into PciHostBridgeFreeRootBridges()\r
131 when it's not used.\r
132**/\r
133PCI_ROOT_BRIDGE *\r
134EFIAPI\r
135PciHostBridgeGetRootBridges (\r
136 UINTN *Count\r
137 )\r
138{\r
46e46eaf
LE
139 EFI_STATUS Status;\r
140 FIRMWARE_CONFIG_ITEM FwCfgItem;\r
141 UINTN FwCfgSize;\r
142 UINT64 ExtraRootBridges;\r
143 PCI_ROOT_BRIDGE *Bridges;\r
144 UINTN Initialized;\r
145 UINTN LastRootBridgeNumber;\r
146 UINTN RootBridgeNumber;\r
147\r
d85861d7 148 *Count = 0;\r
46e46eaf
LE
149\r
150 //\r
151 // QEMU provides the number of extra root buses, shortening the exhaustive\r
152 // search below. If there is no hint, the feature is missing.\r
153 //\r
154 Status = QemuFwCfgFindFile ("etc/extra-pci-roots", &FwCfgItem, &FwCfgSize);\r
155 if (EFI_ERROR (Status) || FwCfgSize != sizeof ExtraRootBridges) {\r
156 ExtraRootBridges = 0;\r
157 } else {\r
158 QemuFwCfgSelectItem (FwCfgItem);\r
159 QemuFwCfgReadBytes (FwCfgSize, &ExtraRootBridges);\r
160\r
161 if (ExtraRootBridges > PCI_MAX_BUS) {\r
162 DEBUG ((EFI_D_ERROR, "%a: invalid count of extra root buses (%Lu) "\r
163 "reported by QEMU\n", __FUNCTION__, ExtraRootBridges));\r
164 return NULL;\r
165 }\r
166 DEBUG ((EFI_D_INFO, "%a: %Lu extra root buses reported by QEMU\n",\r
167 __FUNCTION__, ExtraRootBridges));\r
168 }\r
169\r
170 //\r
171 // Allocate the "main" root bridge, and any extra root bridges.\r
172 //\r
173 Bridges = AllocatePool ((1 + (UINTN)ExtraRootBridges) * sizeof *Bridges);\r
174 if (Bridges == NULL) {\r
175 DEBUG ((EFI_D_ERROR, "%a: %r\n", __FUNCTION__, EFI_OUT_OF_RESOURCES));\r
176 return NULL;\r
177 }\r
178 Initialized = 0;\r
179\r
180 //\r
181 // The "main" root bus is always there.\r
182 //\r
183 LastRootBridgeNumber = 0;\r
184\r
185 //\r
186 // Scan all other root buses. If function 0 of any device on a bus returns a\r
187 // VendorId register value different from all-bits-one, then that bus is\r
188 // alive.\r
189 //\r
190 for (RootBridgeNumber = 1;\r
191 RootBridgeNumber <= PCI_MAX_BUS && Initialized < ExtraRootBridges;\r
192 ++RootBridgeNumber) {\r
193 UINTN Device;\r
194\r
195 for (Device = 0; Device <= PCI_MAX_DEVICE; ++Device) {\r
196 if (PciRead16 (PCI_LIB_ADDRESS (RootBridgeNumber, Device, 0,\r
197 PCI_VENDOR_ID_OFFSET)) != MAX_UINT16) {\r
198 break;\r
199 }\r
200 }\r
201 if (Device <= PCI_MAX_DEVICE) {\r
202 //\r
203 // Found the next root bus. We can now install the *previous* one,\r
204 // because now we know how big a bus number range *that* one has, for any\r
205 // subordinate buses that might exist behind PCI bridges hanging off it.\r
206 //\r
207 Status = InitRootBridge ((UINT8)LastRootBridgeNumber,\r
208 (UINT8)(RootBridgeNumber - 1), &Bridges[Initialized]);\r
209 if (EFI_ERROR (Status)) {\r
210 goto FreeBridges;\r
211 }\r
212 ++Initialized;\r
213 LastRootBridgeNumber = RootBridgeNumber;\r
214 }\r
215 }\r
216\r
217 //\r
218 // Install the last root bus (which might be the only, ie. main, root bus, if\r
219 // we've found no extra root buses).\r
220 //\r
221 Status = InitRootBridge ((UINT8)LastRootBridgeNumber, PCI_MAX_BUS,\r
222 &Bridges[Initialized]);\r
223 if (EFI_ERROR (Status)) {\r
224 goto FreeBridges;\r
225 }\r
226 ++Initialized;\r
227\r
228 *Count = Initialized;\r
229 return Bridges;\r
230\r
231FreeBridges:\r
232 while (Initialized > 0) {\r
233 --Initialized;\r
234 UninitRootBridge (&Bridges[Initialized]);\r
235 }\r
236\r
237 FreePool (Bridges);\r
d85861d7
LE
238 return NULL;\r
239}\r
240\r
46e46eaf 241\r
d85861d7
LE
242/**\r
243 Free the root bridge instances array returned from\r
244 PciHostBridgeGetRootBridges().\r
245\r
246 @param The root bridge instances array.\r
247 @param The count of the array.\r
248**/\r
249VOID\r
250EFIAPI\r
251PciHostBridgeFreeRootBridges (\r
252 PCI_ROOT_BRIDGE *Bridges,\r
253 UINTN Count\r
254 )\r
255{\r
dc4d6467
LE
256 if (Bridges == NULL && Count == 0) {\r
257 return;\r
258 }\r
259 ASSERT (Bridges != NULL && Count > 0);\r
260\r
261 do {\r
262 --Count;\r
263 UninitRootBridge (&Bridges[Count]);\r
264 } while (Count > 0);\r
265\r
266 FreePool (Bridges);\r
d85861d7
LE
267}\r
268\r
46e46eaf 269\r
d85861d7
LE
270/**\r
271 Inform the platform that the resource conflict happens.\r
272\r
273 @param HostBridgeHandle Handle of the Host Bridge.\r
274 @param Configuration Pointer to PCI I/O and PCI memory resource\r
275 descriptors. The Configuration contains the resources\r
276 for all the root bridges. The resource for each root\r
277 bridge is terminated with END descriptor and an\r
278 additional END is appended indicating the end of the\r
279 entire resources. The resource descriptor field\r
280 values follow the description in\r
281 EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL\r
282 .SubmitResources().\r
283**/\r
284VOID\r
285EFIAPI\r
286PciHostBridgeResourceConflict (\r
287 EFI_HANDLE HostBridgeHandle,\r
288 VOID *Configuration\r
289 )\r
290{\r
291 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptor;\r
292 UINTN RootBridgeIndex;\r
293 DEBUG ((EFI_D_ERROR, "PciHostBridge: Resource conflict happens!\n"));\r
294\r
295 RootBridgeIndex = 0;\r
296 Descriptor = (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *) Configuration;\r
297 while (Descriptor->Desc == ACPI_ADDRESS_SPACE_DESCRIPTOR) {\r
298 DEBUG ((EFI_D_ERROR, "RootBridge[%d]:\n", RootBridgeIndex++));\r
299 for (; Descriptor->Desc == ACPI_ADDRESS_SPACE_DESCRIPTOR; Descriptor++) {\r
300 ASSERT (Descriptor->ResType <\r
301 (sizeof (mPciHostBridgeLibAcpiAddressSpaceTypeStr) /\r
302 sizeof (mPciHostBridgeLibAcpiAddressSpaceTypeStr[0])\r
303 )\r
304 );\r
305 DEBUG ((EFI_D_ERROR, " %s: Length/Alignment = 0x%lx / 0x%lx\n",\r
306 mPciHostBridgeLibAcpiAddressSpaceTypeStr[Descriptor->ResType],\r
307 Descriptor->AddrLen, Descriptor->AddrRangeMax\r
308 ));\r
309 if (Descriptor->ResType == ACPI_ADDRESS_SPACE_TYPE_MEM) {\r
310 DEBUG ((EFI_D_ERROR, " Granularity/SpecificFlag = %ld / %02x%s\n",\r
311 Descriptor->AddrSpaceGranularity, Descriptor->SpecificFlag,\r
312 ((Descriptor->SpecificFlag &\r
313 EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE\r
314 ) != 0) ? L" (Prefetchable)" : L""\r
315 ));\r
316 }\r
317 }\r
318 //\r
319 // Skip the END descriptor for root bridge\r
320 //\r
321 ASSERT (Descriptor->Desc == ACPI_END_TAG_DESCRIPTOR);\r
322 Descriptor = (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *)(\r
323 (EFI_ACPI_END_TAG_DESCRIPTOR *)Descriptor + 1\r
324 );\r
325 }\r
326}\r