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