3 Copyright (c) 2005 - 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 EFI PC AT PCI Root Bridge Io Protocol
23 #include "PcatPciRootBridge.h"
25 static BOOLEAN mPciOptionRomTableInstalled
= FALSE
;
26 static EFI_PCI_OPTION_ROM_TABLE mPciOptionRomTable
= {0, NULL
};
30 PcatRootBridgeIoIoRead (
31 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*This
,
32 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width
,
33 IN UINT64 UserAddress
,
35 IN OUT VOID
*UserBuffer
38 return gCpuIo
->Io
.Read (
40 (EFI_CPU_IO_PROTOCOL_WIDTH
) Width
,
49 PcatRootBridgeIoIoWrite (
50 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*This
,
51 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width
,
52 IN UINT64 UserAddress
,
54 IN OUT VOID
*UserBuffer
57 return gCpuIo
->Io
.Write (
59 (EFI_CPU_IO_PROTOCOL_WIDTH
) Width
,
68 PcatRootBridgeIoGetIoPortMapping (
69 OUT EFI_PHYSICAL_ADDRESS
*IoPortMapping
,
70 OUT EFI_PHYSICAL_ADDRESS
*MemoryPortMapping
74 Get the IO Port Mapping. For IA-32 it is always 0.
79 *MemoryPortMapping
= 0;
85 PcatRootBridgeIoPciRW (
86 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*This
,
88 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width
,
89 IN UINT64 UserAddress
,
91 IN OUT VOID
*UserBuffer
94 PCI_CONFIG_ACCESS_CF8 Pci
;
95 PCI_CONFIG_ACCESS_CF8 PciAligned
;
100 PCAT_PCI_ROOT_BRIDGE_INSTANCE
*PrivateData
;
101 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS PciAddress
;
102 UINT64 PciExpressRegAddr
;
103 BOOLEAN UsePciExpressAccess
;
105 if (Width
< 0 || Width
>= EfiPciWidthMaximum
) {
106 return EFI_INVALID_PARAMETER
;
109 if ((Width
& 0x03) >= EfiPciWidthUint64
) {
110 return EFI_INVALID_PARAMETER
;
113 PrivateData
= DRIVER_INSTANCE_FROM_PCI_ROOT_BRIDGE_IO_THIS(This
);
115 InStride
= 1 << (Width
& 0x03);
116 OutStride
= InStride
;
117 if (Width
>= EfiCpuIoWidthFifoUint8
&& Width
<= EfiCpuIoWidthFifoUint64
) {
121 if (Width
>= EfiCpuIoWidthFillUint8
&& Width
<= EfiCpuIoWidthFillUint64
) {
125 UsePciExpressAccess
= FALSE
;
127 CopyMem (&PciAddress
, &UserAddress
, sizeof(UINT64
));
129 if (PciAddress
.ExtendedRegister
> 0xFF) {
131 // Check PciExpressBaseAddress
133 if ((PrivateData
->PciExpressBaseAddress
== 0) ||
134 (PrivateData
->PciExpressBaseAddress
>= EFI_MAX_ADDRESS
)) {
135 return EFI_UNSUPPORTED
;
137 UsePciExpressAccess
= TRUE
;
140 if (PciAddress
.ExtendedRegister
!= 0) {
141 Pci
.Bits
.Reg
= PciAddress
.ExtendedRegister
& 0xFF;
143 Pci
.Bits
.Reg
= PciAddress
.Register
;
146 // Note: We can also use PciExpress access here, if wanted.
150 if (!UsePciExpressAccess
) {
151 Pci
.Bits
.Func
= PciAddress
.Function
;
152 Pci
.Bits
.Dev
= PciAddress
.Device
;
153 Pci
.Bits
.Bus
= PciAddress
.Bus
;
154 Pci
.Bits
.Reserved
= 0;
158 // PCI Config access are all 32-bit alligned, but by accessing the
159 // CONFIG_DATA_REGISTER (0xcfc) with different widths more cycle types
160 // are possible on PCI.
162 // To read a byte of PCI config space you load 0xcf8 and
163 // read 0xcfc, 0xcfd, 0xcfe, 0xcff
165 PciDataStride
= Pci
.Bits
.Reg
& 0x03;
169 PciAligned
.Bits
.Reg
&= 0xfc;
170 PciData
= (UINTN
)PrivateData
->PciData
+ PciDataStride
;
171 EfiAcquireLock(&PrivateData
->PciLock
);
172 This
->Io
.Write (This
, EfiPciWidthUint32
, PrivateData
->PciAddress
, 1, &PciAligned
);
174 This
->Io
.Write (This
, Width
, PciData
, 1, UserBuffer
);
176 This
->Io
.Read (This
, Width
, PciData
, 1, UserBuffer
);
178 EfiReleaseLock(&PrivateData
->PciLock
);
179 UserBuffer
= ((UINT8
*)UserBuffer
) + OutStride
;
180 PciDataStride
= (PciDataStride
+ InStride
) % 4;
181 Pci
.Bits
.Reg
+= InStride
;
186 // Access PCI-Express space by using memory mapped method.
188 PciExpressRegAddr
= (PrivateData
->PciExpressBaseAddress
) |
189 (PciAddress
.Bus
<< 20) |
190 (PciAddress
.Device
<< 15) |
191 (PciAddress
.Function
<< 12);
192 if (PciAddress
.ExtendedRegister
!= 0) {
193 PciExpressRegAddr
+= PciAddress
.ExtendedRegister
;
195 PciExpressRegAddr
+= PciAddress
.Register
;
199 This
->Mem
.Write (This
, Width
, (UINTN
) PciExpressRegAddr
, 1, UserBuffer
);
201 This
->Mem
.Read (This
, Width
, (UINTN
) PciExpressRegAddr
, 1, UserBuffer
);
204 UserBuffer
= ((UINT8
*) UserBuffer
) + OutStride
;
205 PciExpressRegAddr
+= InStride
;
216 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*IoDev
,
223 EFI_PCI_BUS_SCAN_CALLBACK Callback
,
232 PCI_TYPE00 PciHeader
;
235 // Loop through all busses
237 for (Bus
= MinBus
; Bus
<= MaxBus
; Bus
++) {
239 // Loop 32 devices per bus
241 for (Device
= MinDevice
; Device
<= MaxDevice
; Device
++) {
243 // Loop through 8 functions per device
245 for (Func
= MinFunc
; Func
<= MaxFunc
; Func
++) {
248 // Compute the EFI Address required to access the PCI Configuration Header of this PCI Device
250 Address
= EFI_PCI_ADDRESS (Bus
, Device
, Func
, 0);
253 // Read the VendorID from this PCI Device's Confioguration Header
255 IoDev
->Pci
.Read (IoDev
, EfiPciWidthUint16
, Address
, 1, &PciHeader
.Hdr
.VendorId
);
258 // If VendorId = 0xffff, there does not exist a device at this
259 // location. For each device, if there is any function on it,
260 // there must be 1 function at Function 0. So if Func = 0, there
261 // will be no more functions in the same device, so we can break
262 // loop to deal with the next device.
264 if (PciHeader
.Hdr
.VendorId
== 0xffff && Func
== 0) {
268 if (PciHeader
.Hdr
.VendorId
!= 0xffff) {
271 // Read the HeaderType to determine if this is a multi-function device
273 IoDev
->Pci
.Read (IoDev
, EfiPciWidthUint8
, Address
+ 0x0e, 1, &PciHeader
.Hdr
.HeaderType
);
276 // Call the callback function for the device that was found
281 MinDevice
, MaxDevice
,
290 // If this is not a multi-function device, we can leave the loop
291 // to deal with the next device.
293 if ((PciHeader
.Hdr
.HeaderType
& HEADER_TYPE_MULTI_FUNCTION
) == 0x00 && Func
== 0) {
305 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*IoDev
,
319 PCAT_PCI_ROOT_BRIDGE_SCAN_FOR_ROM_CONTEXT
*Context
;
321 PCI_TYPE00 PciHeader
;
322 PCI_TYPE01
*PciBridgeHeader
;
326 EFI_PHYSICAL_ADDRESS RomBuffer
;
328 EFI_PCI_EXPANSION_ROM_HEADER EfiRomHeader
;
329 PCI_DATA_STRUCTURE Pcir
;
330 EFI_PCI_OPTION_ROM_DESCRIPTOR
*TempPciOptionRomDescriptors
;
333 Context
= (PCAT_PCI_ROOT_BRIDGE_SCAN_FOR_ROM_CONTEXT
*)VoidContext
;
335 Address
= EFI_PCI_ADDRESS (Bus
, Device
, Func
, 0);
338 // Save the contents of the PCI Configuration Header
340 IoDev
->Pci
.Read (IoDev
, EfiPciWidthUint32
, Address
, sizeof(PciHeader
)/sizeof(UINT32
), &PciHeader
);
342 if (IS_PCI_BRIDGE(&PciHeader
)) {
344 PciBridgeHeader
= (PCI_TYPE01
*)(&PciHeader
);
347 // See if the PCI-PCI Bridge has its secondary interface enabled.
349 if (PciBridgeHeader
->Bridge
.SubordinateBus
>= PciBridgeHeader
->Bridge
.SecondaryBus
) {
352 // Disable the Prefetchable Memory Window
354 Register
= 0x00000000;
355 IoDev
->Pci
.Write (IoDev
, EfiPciWidthUint16
, Address
+ 0x26, 1, &Register
);
356 IoDev
->Pci
.Write (IoDev
, EfiPciWidthUint32
, Address
+ 0x2c, 1, &Register
);
357 Register
= 0xffffffff;
358 IoDev
->Pci
.Write (IoDev
, EfiPciWidthUint16
, Address
+ 0x24, 1, &Register
);
359 IoDev
->Pci
.Write (IoDev
, EfiPciWidthUint16
, Address
+ 0x28, 1, &Register
);
362 // Program Memory Window to the PCI Root Bridge Memory Window
364 IoDev
->Pci
.Write (IoDev
, EfiPciWidthUint16
, Address
+ 0x20, 4, &Context
->PpbMemoryWindow
);
367 // Enable the Memory decode for the PCI-PCI Bridge
369 IoDev
->Pci
.Read (IoDev
, EfiPciWidthUint16
, Address
+ 4, 1, &Register
);
371 IoDev
->Pci
.Write (IoDev
, EfiPciWidthUint16
, Address
+ 4, 1, &Register
);
374 // Recurse on the Secondary Bus Number
378 PciBridgeHeader
->Bridge
.SecondaryBus
, PciBridgeHeader
->Bridge
.SecondaryBus
,
387 // Check if an Option ROM Register is present and save the Option ROM Window Register
390 IoDev
->Pci
.Write (IoDev
, EfiPciWidthUint32
, Address
+ 0x30, 1, &RomBar
);
391 IoDev
->Pci
.Read (IoDev
, EfiPciWidthUint32
, Address
+ 0x30, 1, &RomBar
);
393 RomBarSize
= (~(RomBar
& 0xfffff800)) + 1;
396 // Make sure the size of the ROM is between 0 and 16 MB
398 if (RomBarSize
> 0 && RomBarSize
<= 0x01000000) {
401 // Program Option ROM Window Register to the PCI Root Bridge Window and Enable the Option ROM Window
403 RomBar
= (Context
->PpbMemoryWindow
& 0xffff) << 16;
404 RomBar
= ((RomBar
- 1) & (~(RomBarSize
- 1))) + RomBarSize
;
405 if (RomBar
< (Context
->PpbMemoryWindow
& 0xffff0000)) {
406 MaxRomSize
= (Context
->PpbMemoryWindow
& 0xffff0000) - RomBar
;
408 IoDev
->Pci
.Write (IoDev
, EfiPciWidthUint32
, Address
+ 0x30, 1, &RomBar
);
409 IoDev
->Pci
.Read (IoDev
, EfiPciWidthUint32
, Address
+ 0x30, 1, &RomBar
);
413 // Enable the Memory decode for the PCI Device
415 IoDev
->Pci
.Read (IoDev
, EfiPciWidthUint16
, Address
+ 4, 1, &Register
);
417 IoDev
->Pci
.Write (IoDev
, EfiPciWidthUint16
, Address
+ 4, 1, &Register
);
420 // Follow the chain of images to determine the size of the Option ROM present
421 // Keep going until the last image is found by looking at the Indicator field
422 // or the size of an image is 0, or the size of all the images is bigger than the
423 // size of the window programmed into the PPB.
430 ZeroMem (&EfiRomHeader
, sizeof(EfiRomHeader
));
435 sizeof(EfiRomHeader
),
439 Pcir
.ImageLength
= 0;
441 if (EfiRomHeader
.Signature
== 0xaa55) {
443 ZeroMem (&Pcir
, sizeof(Pcir
));
447 RomBar
+ RomBarSize
+ EfiRomHeader
.PcirOffset
,
452 if ((Pcir
.Indicator
& 0x80) == 0x00) {
456 RomBarSize
+= Pcir
.ImageLength
* 512;
458 } while (!LastImage
&& RomBarSize
< MaxRomSize
&& Pcir
.ImageLength
!=0);
460 if (RomBarSize
> 0) {
463 // Allocate a memory buffer for the Option ROM contents.
465 Status
= gBS
->AllocatePages(
468 EFI_SIZE_TO_PAGES(RomBarSize
),
472 if (!EFI_ERROR (Status
)) {
475 // Copy the contents of the Option ROM to the memory buffer
477 IoDev
->Mem
.Read (IoDev
, EfiPciWidthUint32
, RomBar
, RomBarSize
/ sizeof(UINT32
), (VOID
*)(UINTN
)RomBuffer
);
479 Status
= gBS
->AllocatePool(
481 ((UINT32
)mPciOptionRomTable
.PciOptionRomCount
+ 1) * sizeof(EFI_PCI_OPTION_ROM_DESCRIPTOR
),
482 (VOID
*)&TempPciOptionRomDescriptors
484 if (mPciOptionRomTable
.PciOptionRomCount
> 0) {
486 TempPciOptionRomDescriptors
,
487 mPciOptionRomTable
.PciOptionRomDescriptors
,
488 (UINT32
)mPciOptionRomTable
.PciOptionRomCount
* sizeof(EFI_PCI_OPTION_ROM_DESCRIPTOR
)
491 gBS
->FreePool(mPciOptionRomTable
.PciOptionRomDescriptors
);
494 mPciOptionRomTable
.PciOptionRomDescriptors
= TempPciOptionRomDescriptors
;
496 TempPciOptionRomDescriptors
= &(mPciOptionRomTable
.PciOptionRomDescriptors
[(UINT32
)mPciOptionRomTable
.PciOptionRomCount
]);
498 TempPciOptionRomDescriptors
->RomAddress
= RomBuffer
;
499 TempPciOptionRomDescriptors
->MemoryType
= EfiBootServicesData
;
500 TempPciOptionRomDescriptors
->RomLength
= RomBarSize
;
501 TempPciOptionRomDescriptors
->Seg
= (UINT32
)IoDev
->SegmentNumber
;
502 TempPciOptionRomDescriptors
->Bus
= (UINT8
)Bus
;
503 TempPciOptionRomDescriptors
->Dev
= (UINT8
)Device
;
504 TempPciOptionRomDescriptors
->Func
= (UINT8
)Func
;
505 TempPciOptionRomDescriptors
->ExecutedLegacyBiosImage
= TRUE
;
506 TempPciOptionRomDescriptors
->DontLoadEfiRom
= FALSE
;
508 mPciOptionRomTable
.PciOptionRomCount
++;
513 // Disable the Memory decode for the PCI-PCI Bridge
515 IoDev
->Pci
.Read (IoDev
, EfiPciWidthUint16
, Address
+ 4, 1, &Register
);
517 IoDev
->Pci
.Write (IoDev
, EfiPciWidthUint16
, Address
+ 4, 1, &Register
);
523 // Restore the PCI Configuration Header
525 IoDev
->Pci
.Write (IoDev
, EfiPciWidthUint32
, Address
, sizeof(PciHeader
)/sizeof(UINT32
), &PciHeader
);
530 SaveCommandRegister (
531 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*IoDev
,
545 PCAT_PCI_ROOT_BRIDGE_SCAN_FOR_ROM_CONTEXT
*Context
;
550 Context
= (PCAT_PCI_ROOT_BRIDGE_SCAN_FOR_ROM_CONTEXT
*)VoidContext
;
552 Address
= EFI_PCI_ADDRESS (Bus
, Device
, Func
, 4);
554 Index
= (Bus
- MinBus
) * (PCI_MAX_DEVICE
+1) * (PCI_MAX_FUNC
+1) + Device
* (PCI_MAX_FUNC
+1) + Func
;
556 IoDev
->Pci
.Read (IoDev
, EfiPciWidthUint16
, Address
, 1, &Context
->CommandRegisterBuffer
[Index
]);
559 // Clear the memory enable bit
561 Command
= Context
->CommandRegisterBuffer
[Index
] & (~0x02);
563 IoDev
->Pci
.Write (IoDev
, EfiPciWidthUint16
, Address
, 1, &Command
);
568 RestoreCommandRegister (
569 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*IoDev
,
583 PCAT_PCI_ROOT_BRIDGE_SCAN_FOR_ROM_CONTEXT
*Context
;
587 Context
= (PCAT_PCI_ROOT_BRIDGE_SCAN_FOR_ROM_CONTEXT
*)VoidContext
;
589 Address
= EFI_PCI_ADDRESS (Bus
, Device
, Func
, 4);
591 Index
= (Bus
- MinBus
) * (PCI_MAX_DEVICE
+1) * (PCI_MAX_FUNC
+1) + Device
* (PCI_MAX_FUNC
+1) + Func
;
593 IoDev
->Pci
.Write (IoDev
, EfiPciWidthUint16
, Address
, 1, &Context
->CommandRegisterBuffer
[Index
]);
597 ScanPciRootBridgeForRoms(
598 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*IoDev
603 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptors
;
606 UINT64 RootWindowBase
;
607 UINT64 RootWindowLimit
;
608 PCAT_PCI_ROOT_BRIDGE_SCAN_FOR_ROM_CONTEXT Context
;
610 if (mPciOptionRomTableInstalled
== FALSE
) {
611 gBS
->InstallConfigurationTable(&gEfiPciOptionRomTableGuid
, &mPciOptionRomTable
);
612 mPciOptionRomTableInstalled
= TRUE
;
615 Status
= IoDev
->Configuration(IoDev
, &Descriptors
);
616 if (EFI_ERROR (Status
) || Descriptors
== NULL
) {
617 return EFI_NOT_FOUND
;
624 while (Descriptors
->Desc
!= ACPI_END_TAG_DESCRIPTOR
) {
628 if (Descriptors
->ResType
== ACPI_ADDRESS_SPACE_TYPE_BUS
) {
629 MinBus
= (UINT16
)Descriptors
->AddrRangeMin
;
630 MaxBus
= (UINT16
)Descriptors
->AddrRangeMax
;
633 // Find memory descriptors that are not prefetchable
635 if (Descriptors
->ResType
== ACPI_ADDRESS_SPACE_TYPE_MEM
&& Descriptors
->SpecificFlag
== 0) {
637 // Find Memory Descriptors that are less than 4GB, so the PPB Memory Window can be used for downstream devices
639 if (Descriptors
->AddrRangeMax
< 0x100000000) {
641 // Find the largest Non-Prefetchable Memory Descriptor that is less than 4GB
643 if ((Descriptors
->AddrRangeMax
- Descriptors
->AddrRangeMin
) > (RootWindowLimit
- RootWindowBase
)) {
644 RootWindowBase
= Descriptors
->AddrRangeMin
;
645 RootWindowLimit
= Descriptors
->AddrRangeMax
;
653 // Make sure a bus range was found
655 if (MinBus
== 0xffff || MaxBus
== 0xffff) {
656 return EFI_NOT_FOUND
;
660 // Make sure a non-prefetchable memory region was found
662 if (RootWindowBase
== 0 && RootWindowLimit
== 0) {
663 return EFI_NOT_FOUND
;
667 // Round the Base and Limit values to 1 MB boudaries
669 RootWindowBase
= ((RootWindowBase
- 1) & 0xfff00000) + 0x00100000;
670 RootWindowLimit
= ((RootWindowLimit
+ 1) & 0xfff00000) - 1;
673 // Make sure that the size of the rounded window is greater than zero
675 if (RootWindowLimit
<= RootWindowBase
) {
676 return EFI_NOT_FOUND
;
680 // Allocate buffer to save the Command register from all the PCI devices
682 Context
.CommandRegisterBuffer
= NULL
;
683 Status
= gBS
->AllocatePool(
685 sizeof(UINT16
) * (MaxBus
- MinBus
+ 1) * (PCI_MAX_DEVICE
+1) * (PCI_MAX_FUNC
+1),
686 &Context
.CommandRegisterBuffer
689 if (EFI_ERROR (Status
)) {
693 Context
.PpbMemoryWindow
= (((UINT32
)RootWindowBase
) >> 16) | ((UINT32
)RootWindowLimit
& 0xffff0000);
696 // Save the Command register from all the PCI devices, and disable the I/O, Mem, and BusMaster bits
703 SaveCommandRegister
, &Context
707 // Recursively scan all the busses for PCI Option ROMs
714 CheckForRom
, &Context
718 // Restore the Command register in all the PCI devices
725 RestoreCommandRegister
, &Context
729 // Free the buffer used to save all the Command register values
731 gBS
->FreePool(Context
.CommandRegisterBuffer
);