OvmfPkg/PciHostBridgeLib: clear PCI aperture vars for (re)init
[mirror_edk2.git] / OvmfPkg / Library / PciHostBridgeLib / XenSupport.c
CommitLineData
49effaf2
RN
1/** @file\r
2 Scan the entire PCI bus for root bridges to support OVMF above Xen.\r
3\r
4 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>\r
5\r
6 This program and the accompanying materials are licensed and made available\r
7 under the terms and conditions of the BSD License which accompanies this\r
8 distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php.\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r
12 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15#include <PiDxe.h>\r
16\r
17#include <IndustryStandard/Pci.h>\r
18#include <IndustryStandard/Q35MchIch9.h>\r
19\r
20#include <Protocol/PciHostBridgeResourceAllocation.h>\r
21#include <Protocol/PciRootBridgeIo.h>\r
22\r
23#include <Library/BaseMemoryLib.h>\r
24#include <Library/DebugLib.h>\r
25#include <Library/MemoryAllocationLib.h>\r
26#include <Library/PciHostBridgeLib.h>\r
27#include <Library/PciLib.h>\r
28#include "PciHostBridge.h"\r
29\r
30STATIC\r
31VOID\r
32PcatPciRootBridgeBarExisted (\r
f982bb8d 33 IN UINTN Address,\r
49effaf2
RN
34 OUT UINT32 *OriginalValue,\r
35 OUT UINT32 *Value\r
36 )\r
37{\r
38 //\r
39 // Preserve the original value\r
40 //\r
41 *OriginalValue = PciRead32 (Address);\r
42\r
43 //\r
44 // Disable timer interrupt while the BAR is probed\r
45 //\r
46 DisableInterrupts ();\r
47\r
48 PciWrite32 (Address, 0xFFFFFFFF);\r
49 *Value = PciRead32 (Address);\r
50 PciWrite32 (Address, *OriginalValue);\r
51\r
52 //\r
53 // Enable interrupt\r
54 //\r
55 EnableInterrupts ();\r
56}\r
57\r
58STATIC\r
59VOID\r
60PcatPciRootBridgeParseBars (\r
61 IN UINT16 Command,\r
62 IN UINTN Bus,\r
63 IN UINTN Device,\r
64 IN UINTN Function,\r
65 IN UINTN BarOffsetBase,\r
66 IN UINTN BarOffsetEnd,\r
67 IN PCI_ROOT_BRIDGE_APERTURE *Io,\r
68 IN PCI_ROOT_BRIDGE_APERTURE *Mem,\r
69 IN PCI_ROOT_BRIDGE_APERTURE *MemAbove4G,\r
70 IN PCI_ROOT_BRIDGE_APERTURE *PMem,\r
71 IN PCI_ROOT_BRIDGE_APERTURE *PMemAbove4G\r
72\r
73)\r
74{\r
75 UINT32 OriginalValue;\r
76 UINT32 Value;\r
77 UINT32 OriginalUpperValue;\r
78 UINT32 UpperValue;\r
79 UINT64 Mask;\r
80 UINTN Offset;\r
81 UINT64 Base;\r
82 UINT64 Length;\r
83 UINT64 Limit;\r
84 PCI_ROOT_BRIDGE_APERTURE *MemAperture;\r
85\r
86 for (Offset = BarOffsetBase; Offset < BarOffsetEnd; Offset += sizeof (UINT32)) {\r
87 PcatPciRootBridgeBarExisted (\r
88 PCI_LIB_ADDRESS (Bus, Device, Function, Offset),\r
89 &OriginalValue, &Value\r
90 );\r
91 if (Value == 0) {\r
92 continue;\r
93 }\r
94 if ((Value & BIT0) == BIT0) {\r
95 //\r
96 // IO Bar\r
97 //\r
98 if (Command & EFI_PCI_COMMAND_IO_SPACE) {\r
99 Mask = 0xfffffffc;\r
100 Base = OriginalValue & Mask;\r
101 Length = ((~(Value & Mask)) & Mask) + 0x04;\r
102 if (!(Value & 0xFFFF0000)) {\r
103 Length &= 0x0000FFFF;\r
104 }\r
105 Limit = Base + Length - 1;\r
106\r
107 if (Base < Limit) {\r
108 if (Io->Base > Base) {\r
109 Io->Base = Base;\r
110 }\r
111 if (Io->Limit < Limit) {\r
112 Io->Limit = Limit;\r
113 }\r
114 }\r
115 }\r
116 } else {\r
117 //\r
118 // Mem Bar\r
119 //\r
120 if (Command & EFI_PCI_COMMAND_MEMORY_SPACE) {\r
121\r
122 Mask = 0xfffffff0;\r
123 Base = OriginalValue & Mask;\r
124 Length = Value & Mask;\r
125\r
126 if ((Value & (BIT1 | BIT2)) == 0) {\r
127 //\r
128 // 32bit\r
129 //\r
130 Length = ((~Length) + 1) & 0xffffffff;\r
131\r
132 if ((Value & BIT3) == BIT3) {\r
133 MemAperture = PMem;\r
134 } else {\r
135 MemAperture = Mem;\r
136 }\r
137 } else {\r
138 //\r
139 // 64bit\r
140 //\r
141 Offset += 4;\r
142 PcatPciRootBridgeBarExisted (\r
143 PCI_LIB_ADDRESS (Bus, Device, Function, Offset),\r
144 &OriginalUpperValue,\r
145 &UpperValue\r
146 );\r
147\r
148 Base = Base | LShiftU64 ((UINT64) OriginalUpperValue, 32);\r
149 Length = Length | LShiftU64 ((UINT64) UpperValue, 32);\r
150 Length = (~Length) + 1;\r
151\r
152 if ((Value & BIT3) == BIT3) {\r
153 MemAperture = PMemAbove4G;\r
154 } else {\r
155 MemAperture = MemAbove4G;\r
156 }\r
157 }\r
158\r
159 Limit = Base + Length - 1;\r
160 if (Base < Limit) {\r
161 if (MemAperture->Base > Base) {\r
162 MemAperture->Base = Base;\r
163 }\r
164 if (MemAperture->Limit < Limit) {\r
165 MemAperture->Limit = Limit;\r
166 }\r
167 }\r
168 }\r
169 }\r
170 }\r
171}\r
172\r
173PCI_ROOT_BRIDGE *\r
174ScanForRootBridges (\r
175 UINTN *NumberOfRootBridges\r
176 )\r
177{\r
178 UINTN PrimaryBus;\r
179 UINTN SubBus;\r
180 UINT8 Device;\r
181 UINT8 Function;\r
182 UINTN NumberOfDevices;\r
f982bb8d 183 UINTN Address;\r
49effaf2
RN
184 PCI_TYPE01 Pci;\r
185 UINT64 Attributes;\r
186 UINT64 Base;\r
187 UINT64 Limit;\r
188 UINT64 Value;\r
189 PCI_ROOT_BRIDGE_APERTURE Io, Mem, MemAbove4G, PMem, PMemAbove4G, *MemAperture;\r
190 PCI_ROOT_BRIDGE *RootBridges;\r
191 UINTN BarOffsetEnd;\r
192\r
193\r
194 *NumberOfRootBridges = 0;\r
195 RootBridges = NULL;\r
196\r
197 //\r
198 // After scanning all the PCI devices on the PCI root bridge's primary bus,\r
199 // update the Primary Bus Number for the next PCI root bridge to be this PCI\r
200 // root bridge's subordinate bus number + 1.\r
201 //\r
202 for (PrimaryBus = 0; PrimaryBus <= PCI_MAX_BUS; PrimaryBus = SubBus + 1) {\r
203 SubBus = PrimaryBus;\r
204 Attributes = 0;\r
e33305ea
HG
205\r
206 ZeroMem (&Io, sizeof (Io));\r
207 ZeroMem (&Mem, sizeof (Mem));\r
208 ZeroMem (&MemAbove4G, sizeof (MemAbove4G));\r
209 ZeroMem (&PMem, sizeof (PMem));\r
210 ZeroMem (&PMemAbove4G, sizeof (PMemAbove4G));\r
49effaf2 211 Io.Base = Mem.Base = MemAbove4G.Base = PMem.Base = PMemAbove4G.Base = MAX_UINT64;\r
49effaf2
RN
212 //\r
213 // Scan all the PCI devices on the primary bus of the PCI root bridge\r
214 //\r
215 for (Device = 0, NumberOfDevices = 0; Device <= PCI_MAX_DEVICE; Device++) {\r
216\r
217 for (Function = 0; Function <= PCI_MAX_FUNC; Function++) {\r
218\r
219 //\r
220 // Compute the PCI configuration address of the PCI device to probe\r
221 //\r
222 Address = PCI_LIB_ADDRESS (PrimaryBus, Device, Function, 0);\r
223\r
224 //\r
225 // Read the Vendor ID from the PCI Configuration Header\r
226 //\r
227 if (PciRead16 (Address) == MAX_UINT16) {\r
228 if (Function == 0) {\r
229 //\r
230 // If the PCI Configuration Read fails, or a PCI device does not\r
231 // exist, then skip this entire PCI device\r
232 //\r
233 break;\r
234 } else {\r
235 //\r
236 // If PCI function != 0, VendorId == 0xFFFF, we continue to search\r
237 // PCI function.\r
238 //\r
239 continue;\r
240 }\r
241 }\r
242\r
243 //\r
244 // Read the entire PCI Configuration Header\r
245 //\r
246 PciReadBuffer (Address, sizeof (Pci), &Pci);\r
247\r
248 //\r
249 // Increment the number of PCI device found on the primary bus of the\r
250 // PCI root bridge\r
251 //\r
252 NumberOfDevices++;\r
253\r
254 //\r
255 // Look for devices with the VGA Palette Snoop enabled in the COMMAND\r
256 // register of the PCI Config Header\r
257 //\r
258 if ((Pci.Hdr.Command & EFI_PCI_COMMAND_VGA_PALETTE_SNOOP) != 0) {\r
259 Attributes |= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO;\r
260 Attributes |= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO_16;\r
261 }\r
262\r
263 BarOffsetEnd = 0;\r
264\r
265 //\r
266 // PCI-PCI Bridge\r
267 //\r
268 if (IS_PCI_BRIDGE (&Pci)) {\r
269 //\r
270 // Get the Bus range that the PPB is decoding\r
271 //\r
272 if (Pci.Bridge.SubordinateBus > SubBus) {\r
273 //\r
274 // If the suborinate bus number of the PCI-PCI bridge is greater\r
275 // than the PCI root bridge's current subordinate bus number,\r
276 // then update the PCI root bridge's subordinate bus number\r
277 //\r
278 SubBus = Pci.Bridge.SubordinateBus;\r
279 }\r
280\r
281 //\r
282 // Get the I/O range that the PPB is decoding\r
283 //\r
284 Value = Pci.Bridge.IoBase & 0x0f;\r
285 Base = ((UINT32) Pci.Bridge.IoBase & 0xf0) << 8;\r
286 Limit = (((UINT32) Pci.Bridge.IoLimit & 0xf0) << 8) | 0x0fff;\r
287 if (Value == BIT0) {\r
288 Base |= ((UINT32) Pci.Bridge.IoBaseUpper16 << 16);\r
289 Limit |= ((UINT32) Pci.Bridge.IoLimitUpper16 << 16);\r
290 }\r
291 if (Base < Limit) {\r
292 if (Io.Base > Base) {\r
293 Io.Base = Base;\r
294 }\r
295 if (Io.Limit < Limit) {\r
296 Io.Limit = Limit;\r
297 }\r
298 }\r
299\r
300 //\r
301 // Get the Memory range that the PPB is decoding\r
302 //\r
303 Base = ((UINT32) Pci.Bridge.MemoryBase & 0xfff0) << 16;\r
304 Limit = (((UINT32) Pci.Bridge.MemoryLimit & 0xfff0) << 16) | 0xfffff;\r
305 if (Base < Limit) {\r
306 if (Mem.Base > Base) {\r
307 Mem.Base = Base;\r
308 }\r
309 if (Mem.Limit < Limit) {\r
310 Mem.Limit = Limit;\r
311 }\r
312 }\r
313\r
314 //\r
315 // Get the Prefetchable Memory range that the PPB is decoding\r
316 //\r
317 Value = Pci.Bridge.PrefetchableMemoryBase & 0x0f;\r
318 Base = ((UINT32) Pci.Bridge.PrefetchableMemoryBase & 0xfff0) << 16;\r
319 Limit = (((UINT32) Pci.Bridge.PrefetchableMemoryLimit & 0xfff0)\r
320 << 16) | 0xfffff;\r
321 MemAperture = &PMem;\r
322 if (Value == BIT0) {\r
323 Base |= LShiftU64 (Pci.Bridge.PrefetchableBaseUpper32, 32);\r
324 Limit |= LShiftU64 (Pci.Bridge.PrefetchableLimitUpper32, 32);\r
325 MemAperture = &PMemAbove4G;\r
326 }\r
327 if (Base < Limit) {\r
328 if (MemAperture->Base > Base) {\r
329 MemAperture->Base = Base;\r
330 }\r
331 if (MemAperture->Limit < Limit) {\r
332 MemAperture->Limit = Limit;\r
333 }\r
334 }\r
335\r
336 //\r
337 // Look at the PPB Configuration for legacy decoding attributes\r
338 //\r
339 if ((Pci.Bridge.BridgeControl & EFI_PCI_BRIDGE_CONTROL_ISA)\r
340 == EFI_PCI_BRIDGE_CONTROL_ISA) {\r
341 Attributes |= EFI_PCI_ATTRIBUTE_ISA_IO;\r
342 Attributes |= EFI_PCI_ATTRIBUTE_ISA_IO_16;\r
343 Attributes |= EFI_PCI_ATTRIBUTE_ISA_MOTHERBOARD_IO;\r
344 }\r
345 if ((Pci.Bridge.BridgeControl & EFI_PCI_BRIDGE_CONTROL_VGA)\r
346 == EFI_PCI_BRIDGE_CONTROL_VGA) {\r
347 Attributes |= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO;\r
348 Attributes |= EFI_PCI_ATTRIBUTE_VGA_MEMORY;\r
349 Attributes |= EFI_PCI_ATTRIBUTE_VGA_IO;\r
350 if ((Pci.Bridge.BridgeControl & EFI_PCI_BRIDGE_CONTROL_VGA_16)\r
351 != 0) {\r
352 Attributes |= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO_16;\r
353 Attributes |= EFI_PCI_ATTRIBUTE_VGA_IO_16;\r
354 }\r
355 }\r
356\r
357 BarOffsetEnd = OFFSET_OF (PCI_TYPE01, Bridge.Bar[2]);\r
358 } else {\r
359 //\r
360 // Parse the BARs of the PCI device to get what I/O Ranges, Memory\r
361 // Ranges, and Prefetchable Memory Ranges the device is decoding\r
362 //\r
363 if ((Pci.Hdr.HeaderType & HEADER_LAYOUT_CODE) == HEADER_TYPE_DEVICE) {\r
364 BarOffsetEnd = OFFSET_OF (PCI_TYPE00, Device.Bar[6]);\r
365 }\r
366 }\r
367\r
368 PcatPciRootBridgeParseBars (\r
369 Pci.Hdr.Command,\r
370 PrimaryBus,\r
371 Device,\r
372 Function,\r
373 OFFSET_OF (PCI_TYPE00, Device.Bar),\r
374 BarOffsetEnd,\r
375 &Io,\r
376 &Mem, &MemAbove4G,\r
377 &PMem, &PMemAbove4G\r
378 );\r
379\r
380 //\r
381 // See if the PCI device is an IDE controller\r
382 //\r
383 if (IS_CLASS2 (&Pci, PCI_CLASS_MASS_STORAGE,\r
384 PCI_CLASS_MASS_STORAGE_IDE)) {\r
385 if (Pci.Hdr.ClassCode[0] & 0x80) {\r
386 Attributes |= EFI_PCI_ATTRIBUTE_IDE_PRIMARY_IO;\r
387 Attributes |= EFI_PCI_ATTRIBUTE_IDE_SECONDARY_IO;\r
388 }\r
389 if (Pci.Hdr.ClassCode[0] & 0x01) {\r
390 Attributes |= EFI_PCI_ATTRIBUTE_IDE_PRIMARY_IO;\r
391 }\r
392 if (Pci.Hdr.ClassCode[0] & 0x04) {\r
393 Attributes |= EFI_PCI_ATTRIBUTE_IDE_SECONDARY_IO;\r
394 }\r
395 }\r
396\r
397 //\r
398 // See if the PCI device is a legacy VGA controller or\r
399 // a standard VGA controller\r
400 //\r
401 if (IS_CLASS2 (&Pci, PCI_CLASS_OLD, PCI_CLASS_OLD_VGA) ||\r
402 IS_CLASS2 (&Pci, PCI_CLASS_DISPLAY, PCI_CLASS_DISPLAY_VGA)\r
403 ) {\r
404 Attributes |= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO;\r
405 Attributes |= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO_16;\r
406 Attributes |= EFI_PCI_ATTRIBUTE_VGA_MEMORY;\r
407 Attributes |= EFI_PCI_ATTRIBUTE_VGA_IO;\r
408 Attributes |= EFI_PCI_ATTRIBUTE_VGA_IO_16;\r
409 }\r
410\r
411 //\r
412 // See if the PCI Device is a PCI - ISA or PCI - EISA\r
413 // or ISA_POSITIVIE_DECODE Bridge device\r
414 //\r
415 if (Pci.Hdr.ClassCode[2] == PCI_CLASS_BRIDGE) {\r
416 if (Pci.Hdr.ClassCode[1] == PCI_CLASS_BRIDGE_ISA ||\r
417 Pci.Hdr.ClassCode[1] == PCI_CLASS_BRIDGE_EISA ||\r
418 Pci.Hdr.ClassCode[1] == PCI_CLASS_BRIDGE_ISA_PDECODE) {\r
419 Attributes |= EFI_PCI_ATTRIBUTE_ISA_IO;\r
420 Attributes |= EFI_PCI_ATTRIBUTE_ISA_IO_16;\r
421 Attributes |= EFI_PCI_ATTRIBUTE_ISA_MOTHERBOARD_IO;\r
422 }\r
423 }\r
424\r
425 //\r
426 // If this device is not a multi function device, then skip the rest\r
427 // of this PCI device\r
428 //\r
429 if (Function == 0 && !IS_PCI_MULTI_FUNC (&Pci)) {\r
430 break;\r
431 }\r
432 }\r
433 }\r
434\r
435 //\r
436 // If at least one PCI device was found on the primary bus of this PCI\r
437 // root bridge, then the PCI root bridge exists.\r
438 //\r
439 if (NumberOfDevices > 0) {\r
440 RootBridges = ReallocatePool (\r
441 (*NumberOfRootBridges) * sizeof (PCI_ROOT_BRIDGE),\r
442 (*NumberOfRootBridges + 1) * sizeof (PCI_ROOT_BRIDGE),\r
443 RootBridges\r
444 );\r
445 ASSERT (RootBridges != NULL);\r
446 InitRootBridge (\r
447 Attributes, Attributes, 0,\r
448 (UINT8) PrimaryBus, (UINT8) SubBus,\r
449 &Io, &Mem, &MemAbove4G, &PMem, &PMemAbove4G,\r
450 &RootBridges[*NumberOfRootBridges]\r
451 );\r
452 RootBridges[*NumberOfRootBridges].ResourceAssigned = TRUE;\r
453 //\r
454 // Increment the index for the next PCI Root Bridge\r
455 //\r
456 (*NumberOfRootBridges)++;\r
457 }\r
458 }\r
459\r
460 return RootBridges;\r
461}\r