2 EFI PCI IO protocol functions implementation for PCI Bus module.
4 Copyright (c) 2006 - 2009, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 // Pci Io Protocol Interface
20 EFI_PCI_IO_PROTOCOL mPciIoInterface
= {
43 PciIoGetBarAttributes
,
44 PciIoSetBarAttributes
,
50 Report a error Status code of PCI bus driver controller.
52 @param PciIoDevice Pci device instance.
53 @param Code Status code value.
57 ReportErrorStatusCode (
58 IN PCI_IO_DEVICE
*PciIoDevice
,
59 IN EFI_STATUS_CODE_VALUE Code
62 return REPORT_STATUS_CODE_WITH_DEVICE_PATH (
63 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
65 PciIoDevice
->DevicePath
70 Initializes a PCI I/O Instance.
72 @param PciIoDevice Pci device instance.
76 InitializePciIoInstance (
77 IN PCI_IO_DEVICE
*PciIoDevice
80 CopyMem (&PciIoDevice
->PciIo
, &mPciIoInterface
, sizeof (EFI_PCI_IO_PROTOCOL
));
84 Verifies access to a PCI Base Address Register (BAR).
86 @param PciIoDevice Pci device instance.
87 @param BarIndex The BAR index of the standard PCI Configuration header to use as the
88 base address for the memory or I/O operation to perform.
89 @param Type Operation type could be memory or I/O.
90 @param Width Signifies the width of the memory or I/O operations.
91 @param Count The number of memory or I/O operations to perform.
92 @param Offset The offset within the PCI configuration space for the PCI controller.
94 @retval EFI_INVALID_PARAMETER Invalid Width/BarIndex or Bar type.
95 @retval EFI_SUCCESS Successfully verified.
99 PciIoVerifyBarAccess (
100 IN PCI_IO_DEVICE
*PciIoDevice
,
102 IN PCI_BAR_TYPE Type
,
103 IN IN EFI_PCI_IO_PROTOCOL_WIDTH Width
,
108 if (Width
< 0 || Width
>= EfiPciIoWidthMaximum
) {
109 return EFI_INVALID_PARAMETER
;
112 if (BarIndex
== EFI_PCI_IO_PASS_THROUGH_BAR
) {
117 // BarIndex 0-5 is legal
119 if (BarIndex
>= PCI_MAX_BAR
) {
120 return EFI_INVALID_PARAMETER
;
123 if (!CheckBarType (PciIoDevice
, BarIndex
, Type
)) {
124 return EFI_INVALID_PARAMETER
;
128 // If Width is EfiPciIoWidthFifoUintX then convert to EfiPciIoWidthUintX
129 // If Width is EfiPciIoWidthFillUintX then convert to EfiPciIoWidthUintX
131 if (Width
>= EfiPciIoWidthFifoUint8
&& Width
<= EfiPciIoWidthFifoUint64
) {
135 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) (Width
& 0x03);
137 if ((*Offset
+ Count
* (UINTN
)(1 << Width
)) - 1 >= PciIoDevice
->PciBar
[BarIndex
].Length
) {
138 return EFI_INVALID_PARAMETER
;
141 *Offset
= *Offset
+ PciIoDevice
->PciBar
[BarIndex
].BaseAddress
;
147 Verifies access to a PCI Configuration Header.
149 @param PciIoDevice Pci device instance.
150 @param Width Signifies the width of the memory or I/O operations.
151 @param Count The number of memory or I/O operations to perform.
152 @param Offset The offset within the PCI configuration space for the PCI controller.
154 @retval EFI_INVALID_PARAMETER Invalid Width
155 @retval EFI_UNSUPPORTED Offset overflowed.
156 @retval EFI_SUCCESS Successfully verified.
160 PciIoVerifyConfigAccess (
161 IN PCI_IO_DEVICE
*PciIoDevice
,
162 IN EFI_PCI_IO_PROTOCOL_WIDTH Width
,
169 if (Width
< 0 || Width
>= EfiPciIoWidthMaximum
) {
170 return EFI_INVALID_PARAMETER
;
174 // If Width is EfiPciIoWidthFillUintX then convert to EfiPciIoWidthUintX
176 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) (Width
& 0x03);
178 if (PciIoDevice
->IsPciExp
) {
179 if ((*Offset
+ Count
* (UINTN
)(1 << Width
)) - 1 >= PCI_EXP_MAX_CONFIG_OFFSET
) {
180 return EFI_UNSUPPORTED
;
183 ExtendOffset
= LShiftU64 (*Offset
, 32);
184 *Offset
= EFI_PCI_ADDRESS (PciIoDevice
->BusNumber
, PciIoDevice
->DeviceNumber
, PciIoDevice
->FunctionNumber
, 0);
185 *Offset
= (*Offset
) | ExtendOffset
;
188 if ((*Offset
+ Count
* (UINTN
)(1 << Width
)) - 1 >= PCI_MAX_CONFIG_OFFSET
) {
189 return EFI_UNSUPPORTED
;
192 *Offset
= EFI_PCI_ADDRESS (PciIoDevice
->BusNumber
, PciIoDevice
->DeviceNumber
, PciIoDevice
->FunctionNumber
, *Offset
);
199 Reads from the memory space of a PCI controller. Returns either when the polling exit criteria is
200 satisfied or after a defined duration.
202 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
203 @param Width Signifies the width of the memory or I/O operations.
204 @param BarIndex The BAR index of the standard PCI Configuration header to use as the
205 base address for the memory operation to perform.
206 @param Offset The offset within the selected BAR to start the memory operation.
207 @param Mask Mask used for the polling criteria.
208 @param Value The comparison value used for the polling exit criteria.
209 @param Delay The number of 100 ns units to poll.
210 @param Result Pointer to the last value read from the memory location.
212 @retval EFI_SUCCESS The last data returned from the access matched the poll exit criteria.
213 @retval EFI_UNSUPPORTED BarIndex not valid for this PCI controller.
214 @retval EFI_UNSUPPORTED Offset is not valid for the BarIndex of this PCI controller.
215 @retval EFI_TIMEOUT Delay expired before a match occurred.
216 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
217 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
223 IN EFI_PCI_IO_PROTOCOL
*This
,
224 IN EFI_PCI_IO_PROTOCOL_WIDTH Width
,
234 PCI_IO_DEVICE
*PciIoDevice
;
236 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
238 if (Width
< 0 || Width
>= EfiPciIoWidthMaximum
) {
239 return EFI_INVALID_PARAMETER
;
242 Status
= PciIoVerifyBarAccess (PciIoDevice
, BarIndex
, PciBarTypeMem
, Width
, 1, &Offset
);
243 if (EFI_ERROR (Status
)) {
244 return EFI_UNSUPPORTED
;
247 if (Width
> EfiPciIoWidthUint64
) {
248 return EFI_INVALID_PARAMETER
;
252 // If request is not aligned, then convert request to EfiPciIoWithXXXUint8
254 if (FeaturePcdGet (PcdUnalignedPciIoEnable
)) {
255 if ((Offset
& ((1 << (Width
& 0x03)) - 1)) != 0) {
256 Status
= PciIoMemRead (This
, Width
, BarIndex
, Offset
, 1, Result
);
257 if (EFI_ERROR (Status
)) {
260 if ((*Result
& Mask
) == Value
|| Delay
== 0) {
265 // Stall 10 us = 100 * 100ns
269 Status
= PciIoMemRead (This
, Width
, BarIndex
, Offset
, 1, Result
);
270 if (EFI_ERROR (Status
)) {
273 if ((*Result
& Mask
) == Value
) {
284 Status
= PciIoDevice
->PciRootBridgeIo
->PollMem (
285 PciIoDevice
->PciRootBridgeIo
,
286 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH
) Width
,
294 if (EFI_ERROR (Status
)) {
295 ReportErrorStatusCode (PciIoDevice
, EFI_IO_BUS_PCI
| EFI_IOB_EC_CONTROLLER_ERROR
);
302 Reads from the memory space of a PCI controller. Returns either when the polling exit criteria is
303 satisfied or after a defined duration.
305 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
306 @param Width Signifies the width of the memory or I/O operations.
307 @param BarIndex The BAR index of the standard PCI Configuration header to use as the
308 base address for the memory operation to perform.
309 @param Offset The offset within the selected BAR to start the memory operation.
310 @param Mask Mask used for the polling criteria.
311 @param Value The comparison value used for the polling exit criteria.
312 @param Delay The number of 100 ns units to poll.
313 @param Result Pointer to the last value read from the memory location.
315 @retval EFI_SUCCESS The last data returned from the access matched the poll exit criteria.
316 @retval EFI_UNSUPPORTED BarIndex not valid for this PCI controller.
317 @retval EFI_UNSUPPORTED Offset is not valid for the BarIndex of this PCI controller.
318 @retval EFI_TIMEOUT Delay expired before a match occurred.
319 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
320 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
326 IN EFI_PCI_IO_PROTOCOL
*This
,
327 IN EFI_PCI_IO_PROTOCOL_WIDTH Width
,
337 PCI_IO_DEVICE
*PciIoDevice
;
339 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
341 if (Width
< 0 || Width
> EfiPciIoWidthUint64
) {
342 return EFI_INVALID_PARAMETER
;
345 Status
= PciIoVerifyBarAccess (PciIoDevice
, BarIndex
, PciBarTypeIo
, Width
, 1, &Offset
);
346 if (EFI_ERROR (Status
)) {
347 return EFI_UNSUPPORTED
;
351 // If request is not aligned, then convert request to EfiPciIoWithXXXUint8
353 if (FeaturePcdGet (PcdUnalignedPciIoEnable
)) {
354 if ((Offset
& ((1 << (Width
& 0x03)) - 1)) != 0) {
355 Status
= PciIoIoRead (This
, Width
, BarIndex
, Offset
, 1, Result
);
356 if (EFI_ERROR (Status
)) {
359 if ((*Result
& Mask
) == Value
|| Delay
== 0) {
364 // Stall 10 us = 100 * 100ns
368 Status
= PciIoIoRead (This
, Width
, BarIndex
, Offset
, 1, Result
);
369 if (EFI_ERROR (Status
)) {
372 if ((*Result
& Mask
) == Value
) {
383 Status
= PciIoDevice
->PciRootBridgeIo
->PollIo (
384 PciIoDevice
->PciRootBridgeIo
,
385 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH
) Width
,
393 if (EFI_ERROR (Status
)) {
394 ReportErrorStatusCode (PciIoDevice
, EFI_IO_BUS_PCI
| EFI_IOB_EC_CONTROLLER_ERROR
);
401 Enable a PCI driver to access PCI controller registers in the PCI memory or I/O space.
403 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
404 @param Width Signifies the width of the memory or I/O operations.
405 @param BarIndex The BAR index of the standard PCI Configuration header to use as the
406 base address for the memory or I/O operation to perform.
407 @param Offset The offset within the selected BAR to start the memory or I/O operation.
408 @param Count The number of memory or I/O operations to perform.
409 @param Buffer For read operations, the destination buffer to store the results. For write
410 operations, the source buffer to write data from.
412 @retval EFI_SUCCESS The data was read from or written to the PCI controller.
413 @retval EFI_UNSUPPORTED BarIndex not valid for this PCI controller.
414 @retval EFI_UNSUPPORTED The address range specified by Offset, Width, and Count is not
415 valid for the PCI BAR specified by BarIndex.
416 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
417 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
423 IN EFI_PCI_IO_PROTOCOL
*This
,
424 IN EFI_PCI_IO_PROTOCOL_WIDTH Width
,
432 PCI_IO_DEVICE
*PciIoDevice
;
434 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
436 if (Width
< 0 || Width
>= EfiPciIoWidthMaximum
) {
437 return EFI_INVALID_PARAMETER
;
440 if (Buffer
== NULL
) {
441 return EFI_INVALID_PARAMETER
;
444 Status
= PciIoVerifyBarAccess (PciIoDevice
, BarIndex
, PciBarTypeMem
, Width
, Count
, &Offset
);
445 if (EFI_ERROR (Status
)) {
446 return EFI_UNSUPPORTED
;
450 // If request is not aligned, then convert request to EfiPciIoWithXXXUint8
452 if (FeaturePcdGet (PcdUnalignedPciIoEnable
)) {
453 if ((Offset
& ((1 << (Width
& 0x03)) - 1)) != 0) {
454 Count
*= (UINTN
)(1 << (Width
& 0x03));
455 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) (Width
& (~0x03));
460 Status
= PciIoDevice
->PciRootBridgeIo
->Mem
.Read (
461 PciIoDevice
->PciRootBridgeIo
,
462 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH
) Width
,
468 if (EFI_ERROR (Status
)) {
469 ReportErrorStatusCode (PciIoDevice
, EFI_IO_BUS_PCI
| EFI_IOB_EC_READ_ERROR
);
476 Enable a PCI driver to access PCI controller registers in the PCI memory or I/O space.
478 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
479 @param Width Signifies the width of the memory or I/O operations.
480 @param BarIndex The BAR index of the standard PCI Configuration header to use as the
481 base address for the memory or I/O operation to perform.
482 @param Offset The offset within the selected BAR to start the memory or I/O operation.
483 @param Count The number of memory or I/O operations to perform.
484 @param Buffer For read operations, the destination buffer to store the results. For write
485 operations, the source buffer to write data from.
487 @retval EFI_SUCCESS The data was read from or written to the PCI controller.
488 @retval EFI_UNSUPPORTED BarIndex not valid for this PCI controller.
489 @retval EFI_UNSUPPORTED The address range specified by Offset, Width, and Count is not
490 valid for the PCI BAR specified by BarIndex.
491 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
492 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
498 IN EFI_PCI_IO_PROTOCOL
*This
,
499 IN EFI_PCI_IO_PROTOCOL_WIDTH Width
,
507 PCI_IO_DEVICE
*PciIoDevice
;
509 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
511 if (Width
< 0 || Width
>= EfiPciIoWidthMaximum
) {
512 return EFI_INVALID_PARAMETER
;
515 if (Buffer
== NULL
) {
516 return EFI_INVALID_PARAMETER
;
519 Status
= PciIoVerifyBarAccess (PciIoDevice
, BarIndex
, PciBarTypeMem
, Width
, Count
, &Offset
);
520 if (EFI_ERROR (Status
)) {
521 return EFI_UNSUPPORTED
;
525 // If request is not aligned, then convert request to EfiPciIoWithXXXUint8
527 if (FeaturePcdGet (PcdUnalignedPciIoEnable
)) {
528 if ((Offset
& ((1 << (Width
& 0x03)) - 1)) != 0) {
529 Count
*= (UINTN
)(1 << (Width
& 0x03));
530 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) (Width
& (~0x03));
534 Status
= PciIoDevice
->PciRootBridgeIo
->Mem
.Write (
535 PciIoDevice
->PciRootBridgeIo
,
536 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH
) Width
,
542 if (EFI_ERROR (Status
)) {
543 ReportErrorStatusCode (PciIoDevice
, EFI_IO_BUS_PCI
| EFI_IOB_EC_WRITE_ERROR
);
550 Enable a PCI driver to access PCI controller registers in the PCI memory or I/O space.
552 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
553 @param Width Signifies the width of the memory or I/O operations.
554 @param BarIndex The BAR index of the standard PCI Configuration header to use as the
555 base address for the memory or I/O operation to perform.
556 @param Offset The offset within the selected BAR to start the memory or I/O operation.
557 @param Count The number of memory or I/O operations to perform.
558 @param Buffer For read operations, the destination buffer to store the results. For write
559 operations, the source buffer to write data from.
561 @retval EFI_SUCCESS The data was read from or written to the PCI controller.
562 @retval EFI_UNSUPPORTED BarIndex not valid for this PCI controller.
563 @retval EFI_UNSUPPORTED The address range specified by Offset, Width, and Count is not
564 valid for the PCI BAR specified by BarIndex.
565 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
566 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
572 IN EFI_PCI_IO_PROTOCOL
*This
,
573 IN EFI_PCI_IO_PROTOCOL_WIDTH Width
,
581 PCI_IO_DEVICE
*PciIoDevice
;
583 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
585 if (Width
< 0 || Width
>= EfiPciIoWidthMaximum
) {
586 return EFI_INVALID_PARAMETER
;
589 if (Buffer
== NULL
) {
590 return EFI_INVALID_PARAMETER
;
593 Status
= PciIoVerifyBarAccess (PciIoDevice
, BarIndex
, PciBarTypeIo
, Width
, Count
, &Offset
);
594 if (EFI_ERROR (Status
)) {
595 return EFI_UNSUPPORTED
;
599 // If request is not aligned, then convert request to EfiPciIoWithXXXUint8
601 if (FeaturePcdGet (PcdUnalignedPciIoEnable
)) {
602 if ((Offset
& ((1 << (Width
& 0x03)) - 1)) != 0) {
603 Count
*= (UINTN
)(1 << (Width
& 0x03));
604 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) (Width
& (~0x03));
608 Status
= PciIoDevice
->PciRootBridgeIo
->Io
.Read (
609 PciIoDevice
->PciRootBridgeIo
,
610 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH
) Width
,
616 if (EFI_ERROR (Status
)) {
617 ReportErrorStatusCode (PciIoDevice
, EFI_IO_BUS_PCI
| EFI_IOB_EC_READ_ERROR
);
624 Enable a PCI driver to access PCI controller registers in the PCI memory or I/O space.
626 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
627 @param Width Signifies the width of the memory or I/O operations.
628 @param BarIndex The BAR index of the standard PCI Configuration header to use as the
629 base address for the memory or I/O operation to perform.
630 @param Offset The offset within the selected BAR to start the memory or I/O operation.
631 @param Count The number of memory or I/O operations to perform.
632 @param Buffer For read operations, the destination buffer to store the results. For write
633 operations, the source buffer to write data from.
635 @retval EFI_SUCCESS The data was read from or written to the PCI controller.
636 @retval EFI_UNSUPPORTED BarIndex not valid for this PCI controller.
637 @retval EFI_UNSUPPORTED The address range specified by Offset, Width, and Count is not
638 valid for the PCI BAR specified by BarIndex.
639 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
640 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
646 IN EFI_PCI_IO_PROTOCOL
*This
,
647 IN EFI_PCI_IO_PROTOCOL_WIDTH Width
,
655 PCI_IO_DEVICE
*PciIoDevice
;
657 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
659 if (Width
< 0 || Width
>= EfiPciIoWidthMaximum
) {
660 return EFI_INVALID_PARAMETER
;
663 if (Buffer
== NULL
) {
664 return EFI_INVALID_PARAMETER
;
667 Status
= PciIoVerifyBarAccess (PciIoDevice
, BarIndex
, PciBarTypeIo
, Width
, Count
, &Offset
);
668 if (EFI_ERROR (Status
)) {
669 return EFI_UNSUPPORTED
;
673 // If request is not aligned, then convert request to EfiPciIoWithXXXUint8
675 if (FeaturePcdGet (PcdUnalignedPciIoEnable
)) {
676 if ((Offset
& ((1 << (Width
& 0x03)) - 1)) != 0) {
677 Count
*= (UINTN
)(1 << (Width
& 0x03));
678 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) (Width
& (~0x03));
682 Status
= PciIoDevice
->PciRootBridgeIo
->Io
.Write (
683 PciIoDevice
->PciRootBridgeIo
,
684 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH
) Width
,
690 if (EFI_ERROR (Status
)) {
691 ReportErrorStatusCode (PciIoDevice
, EFI_IO_BUS_PCI
| EFI_IOB_EC_WRITE_ERROR
);
698 Enable a PCI driver to access PCI controller registers in PCI configuration space.
700 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
701 @param Width Signifies the width of the memory operations.
702 @param Offset The offset within the PCI configuration space for the PCI controller.
703 @param Count The number of PCI configuration operations to perform.
704 @param Buffer For read operations, the destination buffer to store the results. For write
705 operations, the source buffer to write data from.
708 @retval EFI_SUCCESS The data was read from or written to the PCI controller.
709 @retval EFI_UNSUPPORTED The address range specified by Offset, Width, and Count is not
710 valid for the PCI configuration header of the PCI controller.
711 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
712 @retval EFI_INVALID_PARAMETER Buffer is NULL or Width is invalid.
718 IN EFI_PCI_IO_PROTOCOL
*This
,
719 IN EFI_PCI_IO_PROTOCOL_WIDTH Width
,
726 PCI_IO_DEVICE
*PciIoDevice
;
729 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
732 Status
= PciIoVerifyConfigAccess (PciIoDevice
, Width
, Count
, &Address
);
733 if (EFI_ERROR (Status
)) {
738 // If request is not aligned, then convert request to EfiPciIoWithXXXUint8
740 if (FeaturePcdGet (PcdUnalignedPciIoEnable
)) {
741 if ((Offset
& ((1 << (Width
& 0x03)) - 1)) != 0) {
742 Count
*= (UINTN
)(1 << (Width
& 0x03));
743 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) (Width
& (~0x03));
747 Status
= PciIoDevice
->PciRootBridgeIo
->Pci
.Read (
748 PciIoDevice
->PciRootBridgeIo
,
749 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH
) Width
,
755 if (EFI_ERROR (Status
)) {
756 ReportErrorStatusCode (PciIoDevice
, EFI_IO_BUS_PCI
| EFI_IOB_EC_READ_ERROR
);
763 Enable a PCI driver to access PCI controller registers in PCI configuration space.
765 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
766 @param Width Signifies the width of the memory operations.
767 @param Offset The offset within the PCI configuration space for the PCI controller.
768 @param Count The number of PCI configuration operations to perform.
769 @param Buffer For read operations, the destination buffer to store the results. For write
770 operations, the source buffer to write data from.
773 @retval EFI_SUCCESS The data was read from or written to the PCI controller.
774 @retval EFI_UNSUPPORTED The address range specified by Offset, Width, and Count is not
775 valid for the PCI configuration header of the PCI controller.
776 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
777 @retval EFI_INVALID_PARAMETER Buffer is NULL or Width is invalid.
783 IN EFI_PCI_IO_PROTOCOL
*This
,
784 IN EFI_PCI_IO_PROTOCOL_WIDTH Width
,
791 PCI_IO_DEVICE
*PciIoDevice
;
794 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
797 Status
= PciIoVerifyConfigAccess (PciIoDevice
, Width
, Count
, &Address
);
798 if (EFI_ERROR (Status
)) {
803 // If request is not aligned, then convert request to EfiPciIoWithXXXUint8
805 if (FeaturePcdGet (PcdUnalignedPciIoEnable
)) {
806 if ((Offset
& ((1 << (Width
& 0x03)) - 1)) != 0) {
807 Count
*= (UINTN
)(1 << (Width
& 0x03));
808 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) (Width
& (~0x03));
812 Status
= PciIoDevice
->PciRootBridgeIo
->Pci
.Write (
813 PciIoDevice
->PciRootBridgeIo
,
814 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH
) Width
,
820 if (EFI_ERROR (Status
)) {
821 ReportErrorStatusCode (PciIoDevice
, EFI_IO_BUS_PCI
| EFI_IOB_EC_WRITE_ERROR
);
828 Enables a PCI driver to copy one region of PCI memory space to another region of PCI
831 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
832 @param Width Signifies the width of the memory operations.
833 @param DestBarIndex The BAR index in the standard PCI Configuration header to use as the
834 base address for the memory operation to perform.
835 @param DestOffset The destination offset within the BAR specified by DestBarIndex to
836 start the memory writes for the copy operation.
837 @param SrcBarIndex The BAR index in the standard PCI Configuration header to use as the
838 base address for the memory operation to perform.
839 @param SrcOffset The source offset within the BAR specified by SrcBarIndex to start
840 the memory reads for the copy operation.
841 @param Count The number of memory operations to perform. Bytes moved is Width
842 size * Count, starting at DestOffset and SrcOffset.
844 @retval EFI_SUCCESS The data was copied from one memory region to another memory region.
845 @retval EFI_UNSUPPORTED DestBarIndex not valid for this PCI controller.
846 @retval EFI_UNSUPPORTED SrcBarIndex not valid for this PCI controller.
847 @retval EFI_UNSUPPORTED The address range specified by DestOffset, Width, and Count
848 is not valid for the PCI BAR specified by DestBarIndex.
849 @retval EFI_UNSUPPORTED The address range specified by SrcOffset, Width, and Count is
850 not valid for the PCI BAR specified by SrcBarIndex.
851 @retval EFI_INVALID_PARAMETER Width is invalid.
852 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
858 IN EFI_PCI_IO_PROTOCOL
*This
,
859 IN EFI_PCI_IO_PROTOCOL_WIDTH Width
,
860 IN UINT8 DestBarIndex
,
861 IN UINT64 DestOffset
,
862 IN UINT8 SrcBarIndex
,
868 PCI_IO_DEVICE
*PciIoDevice
;
870 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
872 if (Width
< 0 || Width
>= EfiPciIoWidthMaximum
) {
873 return EFI_INVALID_PARAMETER
;
876 if (Width
== EfiPciIoWidthFifoUint8
||
877 Width
== EfiPciIoWidthFifoUint16
||
878 Width
== EfiPciIoWidthFifoUint32
||
879 Width
== EfiPciIoWidthFifoUint64
||
880 Width
== EfiPciIoWidthFillUint8
||
881 Width
== EfiPciIoWidthFillUint16
||
882 Width
== EfiPciIoWidthFillUint32
||
883 Width
== EfiPciIoWidthFillUint64
) {
884 return EFI_INVALID_PARAMETER
;
887 Status
= PciIoVerifyBarAccess (PciIoDevice
, DestBarIndex
, PciBarTypeMem
, Width
, Count
, &DestOffset
);
888 if (EFI_ERROR (Status
)) {
889 return EFI_UNSUPPORTED
;
892 Status
= PciIoVerifyBarAccess (PciIoDevice
, SrcBarIndex
, PciBarTypeMem
, Width
, Count
, &SrcOffset
);
893 if (EFI_ERROR (Status
)) {
894 return EFI_UNSUPPORTED
;
898 // If request is not aligned, then convert request to EfiPciIoWithXXXUint8
900 if (FeaturePcdGet (PcdUnalignedPciIoEnable
)) {
901 if ((SrcOffset
& ((1 << (Width
& 0x03)) - 1)) != 0 || (DestOffset
& ((1 << (Width
& 0x03)) - 1)) != 0) {
902 Count
*= (UINTN
)(1 << (Width
& 0x03));
903 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) (Width
& (~0x03));
907 Status
= PciIoDevice
->PciRootBridgeIo
->CopyMem (
908 PciIoDevice
->PciRootBridgeIo
,
909 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH
) Width
,
915 if (EFI_ERROR (Status
)) {
916 ReportErrorStatusCode (PciIoDevice
, EFI_IO_BUS_PCI
| EFI_IOB_EC_CONTROLLER_ERROR
);
923 Provides the PCI controller-specific addresses needed to access system memory.
925 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
926 @param Operation Indicates if the bus master is going to read or write to system memory.
927 @param HostAddress The system memory address to map to the PCI controller.
928 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes
930 @param DeviceAddress The resulting map address for the bus master PCI controller to use to
931 access the hosts HostAddress.
932 @param Mapping A resulting value to pass to Unmap().
934 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
935 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
936 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
937 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
938 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
944 IN EFI_PCI_IO_PROTOCOL
*This
,
945 IN EFI_PCI_IO_PROTOCOL_OPERATION Operation
,
946 IN VOID
*HostAddress
,
947 IN OUT UINTN
*NumberOfBytes
,
948 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
953 PCI_IO_DEVICE
*PciIoDevice
;
955 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
957 if (Operation
< 0 || Operation
>= EfiPciIoOperationMaximum
) {
958 return EFI_INVALID_PARAMETER
;
961 if (HostAddress
== NULL
|| NumberOfBytes
== NULL
|| DeviceAddress
== NULL
|| Mapping
== NULL
) {
962 return EFI_INVALID_PARAMETER
;
965 if ((PciIoDevice
->Attributes
& EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE
) != 0) {
966 Operation
= (EFI_PCI_IO_PROTOCOL_OPERATION
) (Operation
+ EfiPciOperationBusMasterRead64
);
969 Status
= PciIoDevice
->PciRootBridgeIo
->Map (
970 PciIoDevice
->PciRootBridgeIo
,
971 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_OPERATION
) Operation
,
978 if (EFI_ERROR (Status
)) {
979 ReportErrorStatusCode (PciIoDevice
, EFI_IO_BUS_PCI
| EFI_IOB_EC_CONTROLLER_ERROR
);
986 Completes the Map() operation and releases any corresponding resources.
988 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
989 @param Mapping The mapping value returned from Map().
991 @retval EFI_SUCCESS The range was unmapped.
992 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
998 IN EFI_PCI_IO_PROTOCOL
*This
,
1003 PCI_IO_DEVICE
*PciIoDevice
;
1005 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
1007 Status
= PciIoDevice
->PciRootBridgeIo
->Unmap (
1008 PciIoDevice
->PciRootBridgeIo
,
1012 if (EFI_ERROR (Status
)) {
1013 ReportErrorStatusCode (PciIoDevice
, EFI_IO_BUS_PCI
| EFI_IOB_EC_CONTROLLER_ERROR
);
1020 Allocates pages that are suitable for an EfiPciIoOperationBusMasterCommonBuffer
1023 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
1024 @param Type This parameter is not used and must be ignored.
1025 @param MemoryType The type of memory to allocate, EfiBootServicesData or
1026 EfiRuntimeServicesData.
1027 @param Pages The number of pages to allocate.
1028 @param HostAddress A pointer to store the base system memory address of the
1030 @param Attributes The requested bit mask of attributes for the allocated range.
1032 @retval EFI_SUCCESS The requested memory pages were allocated.
1033 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
1034 MEMORY_WRITE_COMBINE and MEMORY_CACHED.
1035 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1036 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
1041 PciIoAllocateBuffer (
1042 IN EFI_PCI_IO_PROTOCOL
*This
,
1043 IN EFI_ALLOCATE_TYPE Type
,
1044 IN EFI_MEMORY_TYPE MemoryType
,
1046 OUT VOID
**HostAddress
,
1047 IN UINT64 Attributes
1051 PCI_IO_DEVICE
*PciIoDevice
;
1054 (~(EFI_PCI_ATTRIBUTE_MEMORY_WRITE_COMBINE
| EFI_PCI_ATTRIBUTE_MEMORY_CACHED
))) != 0){
1055 return EFI_UNSUPPORTED
;
1058 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
1060 if ((PciIoDevice
->Attributes
& EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE
) != 0) {
1061 Attributes
|= EFI_PCI_ATTRIBUTE_DUAL_ADDRESS_CYCLE
;
1064 Status
= PciIoDevice
->PciRootBridgeIo
->AllocateBuffer (
1065 PciIoDevice
->PciRootBridgeIo
,
1073 if (EFI_ERROR (Status
)) {
1074 ReportErrorStatusCode (PciIoDevice
, EFI_IO_BUS_PCI
| EFI_IOB_EC_CONTROLLER_ERROR
);
1081 Frees memory that was allocated with AllocateBuffer().
1083 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
1084 @param Pages The number of pages to free.
1085 @param HostAddress The base system memory address of the allocated range.
1087 @retval EFI_SUCCESS The requested memory pages were freed.
1088 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages
1089 was not allocated with AllocateBuffer().
1095 IN EFI_PCI_IO_PROTOCOL
*This
,
1097 IN VOID
*HostAddress
1101 PCI_IO_DEVICE
*PciIoDevice
;
1103 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
1105 Status
= PciIoDevice
->PciRootBridgeIo
->FreeBuffer (
1106 PciIoDevice
->PciRootBridgeIo
,
1111 if (EFI_ERROR (Status
)) {
1112 ReportErrorStatusCode (PciIoDevice
, EFI_IO_BUS_PCI
| EFI_IOB_EC_CONTROLLER_ERROR
);
1119 Flushes all PCI posted write transactions from a PCI host bridge to system memory.
1121 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
1123 @retval EFI_SUCCESS The PCI posted write transactions were flushed from the PCI host
1124 bridge to system memory.
1125 @retval EFI_DEVICE_ERROR The PCI posted write transactions were not flushed from the PCI
1126 host bridge due to a hardware error.
1132 IN EFI_PCI_IO_PROTOCOL
*This
1136 PCI_IO_DEVICE
*PciIoDevice
;
1138 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
1140 Status
= PciIoDevice
->PciRootBridgeIo
->Flush (
1141 PciIoDevice
->PciRootBridgeIo
1143 if (EFI_ERROR (Status
)) {
1144 ReportErrorStatusCode (PciIoDevice
, EFI_IO_BUS_PCI
| EFI_IOB_EC_CONTROLLER_ERROR
);
1151 Retrieves this PCI controller's current PCI bus number, device number, and function number.
1153 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
1154 @param SegmentNumber The PCI controller's current PCI segment number.
1155 @param BusNumber The PCI controller's current PCI bus number.
1156 @param DeviceNumber The PCI controller's current PCI device number.
1157 @param FunctionNumber The PCI controller's current PCI function number.
1159 @retval EFI_SUCCESS The PCI controller location was returned.
1160 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1166 IN EFI_PCI_IO_PROTOCOL
*This
,
1173 PCI_IO_DEVICE
*PciIoDevice
;
1175 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
1177 if (Segment
== NULL
|| Bus
== NULL
|| Device
== NULL
|| Function
== NULL
) {
1178 return EFI_INVALID_PARAMETER
;
1181 *Segment
= PciIoDevice
->PciRootBridgeIo
->SegmentNumber
;
1182 *Bus
= PciIoDevice
->BusNumber
;
1183 *Device
= PciIoDevice
->DeviceNumber
;
1184 *Function
= PciIoDevice
->FunctionNumber
;
1190 Check BAR type for PCI resource.
1192 @param PciIoDevice PCI device instance.
1193 @param BarIndex The BAR index of the standard PCI Configuration header to use as the
1194 base address for the memory or I/O operation to perform.
1195 @param BarType Memory or I/O.
1197 @retval TRUE Pci device's bar type is same with input BarType.
1198 @retval TRUE Pci device's bar type is not same with input BarType.
1203 IN PCI_IO_DEVICE
*PciIoDevice
,
1205 IN PCI_BAR_TYPE BarType
1212 if (PciIoDevice
->PciBar
[BarIndex
].BarType
!= PciBarTypeMem32
&&
1213 PciIoDevice
->PciBar
[BarIndex
].BarType
!= PciBarTypePMem32
&&
1214 PciIoDevice
->PciBar
[BarIndex
].BarType
!= PciBarTypePMem64
&&
1215 PciIoDevice
->PciBar
[BarIndex
].BarType
!= PciBarTypeMem64
) {
1222 if (PciIoDevice
->PciBar
[BarIndex
].BarType
!= PciBarTypeIo32
&&
1223 PciIoDevice
->PciBar
[BarIndex
].BarType
!= PciBarTypeIo16
){
1237 Set/Disable new attributes to a Root Bridge.
1239 @param PciIoDevice Pci device instance.
1240 @param Attributes New attribute want to be set.
1241 @param Operation Set or Disable.
1243 @retval EFI_UNSUPPORTED If root bridge does not support change attribute.
1244 @retval EFI_SUCCESS Successfully set new attributs.
1248 ModifyRootBridgeAttributes (
1249 IN PCI_IO_DEVICE
*PciIoDevice
,
1250 IN UINT64 Attributes
,
1251 IN EFI_PCI_IO_PROTOCOL_ATTRIBUTE_OPERATION Operation
1254 UINT64 PciRootBridgeSupports
;
1255 UINT64 PciRootBridgeAttributes
;
1256 UINT64 NewPciRootBridgeAttributes
;
1260 // Get the current attributes of this PCI device's PCI Root Bridge
1262 Status
= PciIoDevice
->PciRootBridgeIo
->GetAttributes (
1263 PciIoDevice
->PciRootBridgeIo
,
1264 &PciRootBridgeSupports
,
1265 &PciRootBridgeAttributes
1267 if (EFI_ERROR (Status
)) {
1268 return EFI_UNSUPPORTED
;
1272 // Mask off EFI_PCI_IO_ATTRIBUTE_EMBEDDED_DEVICE &
1273 // EFI_PCI_IO_ATTRIBUTE_EMBEDDED_ROM, which are not supported by PCI root bridge.
1275 Attributes
&= ~(UINT64
)(EFI_PCI_IO_ATTRIBUTE_EMBEDDED_DEVICE
|
1276 EFI_PCI_IO_ATTRIBUTE_EMBEDDED_ROM
);
1279 // Record the new attribute of the Root Bridge
1281 if (Operation
== EfiPciIoAttributeOperationEnable
) {
1282 NewPciRootBridgeAttributes
= PciRootBridgeAttributes
| Attributes
;
1284 NewPciRootBridgeAttributes
= PciRootBridgeAttributes
& (~Attributes
);
1288 // Call the PCI Root Bridge to attempt to modify the attributes
1290 if ((NewPciRootBridgeAttributes
^ PciRootBridgeAttributes
) != 0) {
1292 Status
= PciIoDevice
->PciRootBridgeIo
->SetAttributes (
1293 PciIoDevice
->PciRootBridgeIo
,
1294 NewPciRootBridgeAttributes
,
1298 if (EFI_ERROR (Status
)) {
1300 // The PCI Root Bridge could not modify the attributes, so return the error.
1302 return EFI_UNSUPPORTED
;
1307 // Also update the attributes for this Root Bridge structure
1309 PciIoDevice
->Attributes
= NewPciRootBridgeAttributes
;
1315 Check whether this device can be enable/disable to snoop.
1317 @param PciIoDevice Pci device instance.
1318 @param Operation Enable/Disable.
1320 @retval EFI_UNSUPPORTED Pci device is not GFX device or not support snoop.
1321 @retval EFI_SUCCESS Snoop can be supported.
1325 SupportPaletteSnoopAttributes (
1326 IN PCI_IO_DEVICE
*PciIoDevice
,
1327 IN EFI_PCI_IO_PROTOCOL_ATTRIBUTE_OPERATION Operation
1330 PCI_IO_DEVICE
*Temp
;
1334 // Snoop attribute can be only modified by GFX
1336 if (!IS_PCI_GFX (&PciIoDevice
->Pci
)) {
1337 return EFI_UNSUPPORTED
;
1341 // Get the boot VGA on the same segement
1343 Temp
= ActiveVGADeviceOnTheSameSegment (PciIoDevice
);
1347 // If there is no VGA device on the segement, set
1348 // this graphics card to decode the palette range
1354 // Check these two agents are on the same path
1356 if (!PciDevicesOnTheSamePath (Temp
, PciIoDevice
)) {
1358 // they are not on the same path, so snoop can be enabled or disabled
1363 // Check if they are on the same bus
1365 if (Temp
->Parent
== PciIoDevice
->Parent
) {
1367 PCI_READ_COMMAND_REGISTER (Temp
, &VGACommand
);
1370 // If they are on the same bus, either one can
1371 // be set to snoop, the other set to decode
1373 if ((VGACommand
& EFI_PCI_COMMAND_VGA_PALETTE_SNOOP
) != 0) {
1375 // VGA has set to snoop, so GFX can be only set to disable snoop
1377 if (Operation
== EfiPciIoAttributeOperationEnable
) {
1378 return EFI_UNSUPPORTED
;
1382 // VGA has disabled to snoop, so GFX can be only enabled
1384 if (Operation
== EfiPciIoAttributeOperationDisable
) {
1385 return EFI_UNSUPPORTED
;
1393 // If they are on the same path but on the different bus
1394 // The first agent is set to snoop, the second one set to
1398 if (Temp
->BusNumber
< PciIoDevice
->BusNumber
) {
1400 // GFX should be set to decode
1402 if (Operation
== EfiPciIoAttributeOperationDisable
) {
1403 PCI_ENABLE_COMMAND_REGISTER (Temp
, EFI_PCI_COMMAND_VGA_PALETTE_SNOOP
);
1404 Temp
->Attributes
|= EFI_PCI_COMMAND_VGA_PALETTE_SNOOP
;
1406 return EFI_UNSUPPORTED
;
1411 // GFX should be set to snoop
1413 if (Operation
== EfiPciIoAttributeOperationEnable
) {
1414 PCI_DISABLE_COMMAND_REGISTER (Temp
, EFI_PCI_COMMAND_VGA_PALETTE_SNOOP
);
1415 Temp
->Attributes
&= (~EFI_PCI_COMMAND_VGA_PALETTE_SNOOP
);
1417 return EFI_UNSUPPORTED
;
1426 Performs an operation on the attributes that this PCI controller supports. The operations include
1427 getting the set of supported attributes, retrieving the current attributes, setting the current
1428 attributes, enabling attributes, and disabling attributes.
1430 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
1431 @param Operation The operation to perform on the attributes for this PCI controller.
1432 @param Attributes The mask of attributes that are used for Set, Enable, and Disable
1434 @param Result A pointer to the result mask of attributes that are returned for the Get
1435 and Supported operations.
1437 @retval EFI_SUCCESS The operation on the PCI controller's attributes was completed.
1438 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1439 @retval EFI_UNSUPPORTED one or more of the bits set in
1440 Attributes are not supported by this PCI controller or one of
1441 its parent bridges when Operation is Set, Enable or Disable.
1447 IN EFI_PCI_IO_PROTOCOL
* This
,
1448 IN EFI_PCI_IO_PROTOCOL_ATTRIBUTE_OPERATION Operation
,
1449 IN UINT64 Attributes
,
1450 OUT UINT64
*Result OPTIONAL
1455 PCI_IO_DEVICE
*PciIoDevice
;
1456 PCI_IO_DEVICE
*UpStreamBridge
;
1457 PCI_IO_DEVICE
*Temp
;
1460 UINT64 UpStreamAttributes
;
1461 UINT16 BridgeControl
;
1464 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
1466 switch (Operation
) {
1467 case EfiPciIoAttributeOperationGet
:
1468 if (Result
== NULL
) {
1469 return EFI_INVALID_PARAMETER
;
1472 *Result
= PciIoDevice
->Attributes
;
1475 case EfiPciIoAttributeOperationSupported
:
1476 if (Result
== NULL
) {
1477 return EFI_INVALID_PARAMETER
;
1480 *Result
= PciIoDevice
->Supports
;
1483 case EfiPciIoAttributeOperationSet
:
1484 Status
= PciIoDevice
->PciIo
.Attributes (
1485 &(PciIoDevice
->PciIo
),
1486 EfiPciIoAttributeOperationEnable
,
1490 if (EFI_ERROR (Status
)) {
1491 return EFI_UNSUPPORTED
;
1494 Status
= PciIoDevice
->PciIo
.Attributes (
1495 &(PciIoDevice
->PciIo
),
1496 EfiPciIoAttributeOperationDisable
,
1497 (~Attributes
) & (PciIoDevice
->Supports
),
1500 if (EFI_ERROR (Status
)) {
1501 return EFI_UNSUPPORTED
;
1506 case EfiPciIoAttributeOperationEnable
:
1507 case EfiPciIoAttributeOperationDisable
:
1511 return EFI_INVALID_PARAMETER
;
1514 // Just a trick for ENABLE attribute
1515 // EFI_PCI_DEVICE_ENABLE is not defined in UEFI spec, which is the internal usage.
1516 // So, this logic doesn't confrom to UEFI spec, which should be removed.
1517 // But this trick logic is still kept for some binary drivers that depend on it.
1519 if ((Attributes
& EFI_PCI_DEVICE_ENABLE
) == EFI_PCI_DEVICE_ENABLE
) {
1520 Attributes
&= (PciIoDevice
->Supports
);
1523 // Raise the EFI_P_PC_ENABLE Status code
1525 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1527 EFI_IO_BUS_PCI
| EFI_P_PC_ENABLE
,
1528 PciIoDevice
->DevicePath
1533 // Check VGA and VGA16, they can not be set at the same time
1535 if (((Attributes
& EFI_PCI_IO_ATTRIBUTE_VGA_IO
) != 0 &&
1536 (Attributes
& EFI_PCI_IO_ATTRIBUTE_VGA_IO_16
) != 0) ||
1537 ((Attributes
& EFI_PCI_IO_ATTRIBUTE_VGA_IO
) != 0 &&
1538 (Attributes
& EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16
) != 0) ||
1539 ((Attributes
& EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO
) != 0 &&
1540 (Attributes
& EFI_PCI_IO_ATTRIBUTE_VGA_IO_16
) != 0) ||
1541 ((Attributes
& EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO
) != 0 &&
1542 (Attributes
& EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16
) != 0) ) {
1543 return EFI_UNSUPPORTED
;
1547 // workaround for PCI drivers which always set ISA_IO or VGA_IO attribute without detecting support of
1548 // ISA_IO/ISA_IO_16 or VGA_IO/VGA_IO_16 to maintain backward-compatibility.
1550 if (((Attributes
& EFI_PCI_IO_ATTRIBUTE_VGA_IO
) != 0) &&
1551 ((PciIoDevice
->Supports
& (EFI_PCI_IO_ATTRIBUTE_VGA_IO
| EFI_PCI_IO_ATTRIBUTE_VGA_IO_16
)) \
1552 == EFI_PCI_IO_ATTRIBUTE_VGA_IO_16
)) {
1553 Attributes
&= ~(UINT64
)EFI_PCI_IO_ATTRIBUTE_VGA_IO
;
1554 Attributes
|= EFI_PCI_IO_ATTRIBUTE_VGA_IO_16
;
1557 if (((Attributes
& EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO
) != 0) &&
1558 ((PciIoDevice
->Supports
& (EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO
| EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16
)) \
1559 == EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16
)) {
1560 Attributes
&= ~(UINT64
)EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO
;
1561 Attributes
|= EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16
;
1565 // If no attributes can be supported, then return.
1566 // Otherwise, set the attributes that it can support.
1568 Supports
= (PciIoDevice
->Supports
) & Attributes
;
1569 if (Supports
!= Attributes
) {
1570 return EFI_UNSUPPORTED
;
1574 // For Root Bridge, just call RootBridgeIo to set attributes;
1576 if (PciIoDevice
->Parent
== NULL
) {
1577 Status
= ModifyRootBridgeAttributes (PciIoDevice
, Attributes
, Operation
);
1585 // For PPB & P2C, set relevant attribute bits
1587 if (IS_PCI_BRIDGE (&PciIoDevice
->Pci
) || IS_CARDBUS_BRIDGE (&PciIoDevice
->Pci
)) {
1589 if ((Attributes
& (EFI_PCI_IO_ATTRIBUTE_VGA_IO
| EFI_PCI_IO_ATTRIBUTE_VGA_IO_16
)) != 0) {
1590 BridgeControl
|= EFI_PCI_BRIDGE_CONTROL_VGA
;
1593 if ((Attributes
& EFI_PCI_IO_ATTRIBUTE_ISA_IO
) != 0) {
1594 BridgeControl
|= EFI_PCI_BRIDGE_CONTROL_ISA
;
1597 if ((Attributes
& (EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO
| EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16
)) != 0) {
1598 Command
|= EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO
;
1601 if ((Attributes
& (EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16
| EFI_PCI_IO_ATTRIBUTE_VGA_IO_16
)) != 0) {
1602 BridgeControl
|= EFI_PCI_BRIDGE_CONTROL_VGA_16
;
1607 // Do with the attributes on VGA
1608 // Only for VGA's legacy resource, we just can enable once.
1611 (EFI_PCI_IO_ATTRIBUTE_VGA_IO
|
1612 EFI_PCI_IO_ATTRIBUTE_VGA_IO_16
|
1613 EFI_PCI_IO_ATTRIBUTE_VGA_MEMORY
)) != 0) {
1615 // Check if a VGA has been enabled before enabling a new one
1617 if (Operation
== EfiPciIoAttributeOperationEnable
) {
1619 // Check if there have been an active VGA device on the same segment
1621 Temp
= ActiveVGADeviceOnTheSameSegment (PciIoDevice
);
1622 if (Temp
!= NULL
&& Temp
!= PciIoDevice
) {
1624 // An active VGA has been detected, so can not enable another
1626 return EFI_UNSUPPORTED
;
1632 // Do with the attributes on GFX
1634 if ((Attributes
& (EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO
| EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16
)) != 0) {
1636 if (Operation
== EfiPciIoAttributeOperationEnable
) {
1638 // Check if snoop can be enabled in current configuration
1640 Status
= SupportPaletteSnoopAttributes (PciIoDevice
, Operation
);
1642 if (EFI_ERROR (Status
)) {
1645 // Enable operation is forbidden, so mask the bit in attributes
1646 // so as to keep consistent with the actual Status
1648 // Attributes &= (~EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO);
1652 return EFI_UNSUPPORTED
;
1658 // It can be supported, so get ready to set the bit
1660 Command
|= EFI_PCI_COMMAND_VGA_PALETTE_SNOOP
;
1664 if ((Attributes
& EFI_PCI_IO_ATTRIBUTE_IO
) != 0) {
1665 Command
|= EFI_PCI_COMMAND_IO_SPACE
;
1668 if ((Attributes
& EFI_PCI_IO_ATTRIBUTE_MEMORY
) != 0) {
1669 Command
|= EFI_PCI_COMMAND_MEMORY_SPACE
;
1672 if ((Attributes
& EFI_PCI_IO_ATTRIBUTE_BUS_MASTER
) != 0) {
1673 Command
|= EFI_PCI_COMMAND_BUS_MASTER
;
1676 // The upstream bridge should be also set to revelant attribute
1677 // expect for IO, Mem and BusMaster
1679 UpStreamAttributes
= Attributes
&
1680 (~(EFI_PCI_IO_ATTRIBUTE_IO
|
1681 EFI_PCI_IO_ATTRIBUTE_MEMORY
|
1682 EFI_PCI_IO_ATTRIBUTE_BUS_MASTER
1685 UpStreamBridge
= PciIoDevice
->Parent
;
1687 if (Operation
== EfiPciIoAttributeOperationEnable
) {
1689 // Enable relevant attributes to command register and bridge control register
1691 Status
= PCI_ENABLE_COMMAND_REGISTER (PciIoDevice
, Command
);
1692 if (BridgeControl
!= 0) {
1693 Status
= PCI_ENABLE_BRIDGE_CONTROL_REGISTER (PciIoDevice
, BridgeControl
);
1696 PciIoDevice
->Attributes
|= Attributes
;
1699 // Enable attributes of the upstream bridge
1701 Status
= UpStreamBridge
->PciIo
.Attributes (
1702 &(UpStreamBridge
->PciIo
),
1703 EfiPciIoAttributeOperationEnable
,
1710 // Disable relevant attributes to command register and bridge control register
1712 Status
= PCI_DISABLE_COMMAND_REGISTER (PciIoDevice
, Command
);
1713 if (BridgeControl
!= 0) {
1714 Status
= PCI_DISABLE_BRIDGE_CONTROL_REGISTER (PciIoDevice
, BridgeControl
);
1717 PciIoDevice
->Attributes
&= (~Attributes
);
1718 Status
= EFI_SUCCESS
;
1722 if (EFI_ERROR (Status
)) {
1723 ReportErrorStatusCode (PciIoDevice
, EFI_IO_BUS_PCI
| EFI_IOB_EC_CONTROLLER_ERROR
);
1730 Gets the attributes that this PCI controller supports setting on a BAR using
1731 SetBarAttributes(), and retrieves the list of resource descriptors for a BAR.
1733 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
1734 @param BarIndex The BAR index of the standard PCI Configuration header to use as the
1735 base address for resource range. The legal range for this field is 0..5.
1736 @param Supports A pointer to the mask of attributes that this PCI controller supports
1737 setting for this BAR with SetBarAttributes().
1738 @param Resources A pointer to the ACPI 2.0 resource descriptors that describe the current
1739 configuration of this BAR of the PCI controller.
1741 @retval EFI_SUCCESS If Supports is not NULL, then the attributes that the PCI
1742 controller supports are returned in Supports. If Resources
1743 is not NULL, then the ACPI 2.0 resource descriptors that the PCI
1744 controller is currently using are returned in Resources.
1745 @retval EFI_INVALID_PARAMETER Both Supports and Attributes are NULL.
1746 @retval EFI_UNSUPPORTED BarIndex not valid for this PCI controller.
1747 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to allocate
1753 PciIoGetBarAttributes (
1754 IN EFI_PCI_IO_PROTOCOL
* This
,
1756 OUT UINT64
*Supports
, OPTIONAL
1757 OUT VOID
**Resources OPTIONAL
1761 UINT8
*Configuration
;
1763 PCI_IO_DEVICE
*PciIoDevice
;
1764 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Ptr
;
1765 EFI_ACPI_END_TAG_DESCRIPTOR
*PtrEnd
;
1769 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
1771 if (Supports
== NULL
&& Resources
== NULL
) {
1772 return EFI_INVALID_PARAMETER
;
1775 if (BarIndex
>= PCI_MAX_BAR
) {
1776 return EFI_UNSUPPORTED
;
1780 // This driver does not support modifications to the WRITE_COMBINE or
1781 // CACHED attributes for BAR ranges.
1783 if (Supports
!= NULL
) {
1784 *Supports
= PciIoDevice
->Supports
& EFI_PCI_IO_ATTRIBUTE_MEMORY_CACHED
& EFI_PCI_IO_ATTRIBUTE_MEMORY_WRITE_COMBINE
;
1787 if (Resources
!= NULL
) {
1789 if (PciIoDevice
->PciBar
[BarIndex
].BarType
!= PciBarTypeUnknown
) {
1793 Configuration
= AllocateZeroPool (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) * NumConfig
+ sizeof (EFI_ACPI_END_TAG_DESCRIPTOR
));
1794 if (Configuration
== NULL
) {
1795 return EFI_OUT_OF_RESOURCES
;
1798 Ptr
= (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*) Configuration
;
1800 if (NumConfig
== 1) {
1801 Ptr
->Desc
= ACPI_ADDRESS_SPACE_DESCRIPTOR
;
1802 Ptr
->Len
= (UINT16
) (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) - 3);
1804 Ptr
->AddrRangeMin
= PciIoDevice
->PciBar
[BarIndex
].BaseAddress
;
1805 Ptr
->AddrLen
= PciIoDevice
->PciBar
[BarIndex
].Length
;
1806 Ptr
->AddrRangeMax
= PciIoDevice
->PciBar
[BarIndex
].Alignment
;
1808 switch (PciIoDevice
->PciBar
[BarIndex
].BarType
) {
1809 case PciBarTypeIo16
:
1810 case PciBarTypeIo32
:
1814 Ptr
->ResType
= ACPI_ADDRESS_SPACE_TYPE_IO
;
1817 case PciBarTypeMem32
:
1821 Ptr
->ResType
= ACPI_ADDRESS_SPACE_TYPE_MEM
;
1825 Ptr
->AddrSpaceGranularity
= 32;
1828 case PciBarTypePMem32
:
1832 Ptr
->ResType
= ACPI_ADDRESS_SPACE_TYPE_MEM
;
1836 Ptr
->SpecificFlag
= 0x6;
1840 Ptr
->AddrSpaceGranularity
= 32;
1843 case PciBarTypeMem64
:
1847 Ptr
->ResType
= ACPI_ADDRESS_SPACE_TYPE_MEM
;
1851 Ptr
->AddrSpaceGranularity
= 64;
1854 case PciBarTypePMem64
:
1858 Ptr
->ResType
= ACPI_ADDRESS_SPACE_TYPE_MEM
;
1862 Ptr
->SpecificFlag
= 0x6;
1866 Ptr
->AddrSpaceGranularity
= 64;
1873 Ptr
= (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*) ((UINT8
*) Ptr
+ sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
));
1879 PtrEnd
= (EFI_ACPI_END_TAG_DESCRIPTOR
*) ((UINT8
*) Ptr
);
1880 PtrEnd
->Desc
= ACPI_END_TAG_DESCRIPTOR
;
1881 PtrEnd
->Checksum
= 0;
1883 *Resources
= Configuration
;
1890 Sets the attributes for a range of a BAR on a PCI controller.
1892 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
1893 @param Attributes The mask of attributes to set for the resource range specified by
1894 BarIndex, Offset, and Length.
1895 @param BarIndex The BAR index of the standard PCI Configuration header to use as the
1896 base address for resource range. The legal range for this field is 0..5.
1897 @param Offset A pointer to the BAR relative base address of the resource range to be
1898 modified by the attributes specified by Attributes.
1899 @param Length A pointer to the length of the resource range to be modified by the
1900 attributes specified by Attributes.
1902 @retval EFI_SUCCESS The set of attributes specified by Attributes for the resource
1903 range specified by BarIndex, Offset, and Length were
1904 set on the PCI controller, and the actual resource range is returned
1905 in Offset and Length.
1906 @retval EFI_INVALID_PARAMETER Offset or Length is NULL.
1907 @retval EFI_UNSUPPORTED BarIndex not valid for this PCI controller.
1908 @retval EFI_OUT_OF_RESOURCES There are not enough resources to set the attributes on the
1909 resource range specified by BarIndex, Offset, and
1915 PciIoSetBarAttributes (
1916 IN EFI_PCI_IO_PROTOCOL
*This
,
1917 IN UINT64 Attributes
,
1919 IN OUT UINT64
*Offset
,
1920 IN OUT UINT64
*Length
1924 PCI_IO_DEVICE
*PciIoDevice
;
1925 UINT64 NonRelativeOffset
;
1928 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
1931 // Make sure Offset and Length are not NULL
1933 if (Offset
== NULL
|| Length
== NULL
) {
1934 return EFI_INVALID_PARAMETER
;
1937 if (PciIoDevice
->PciBar
[BarIndex
].BarType
== PciBarTypeUnknown
) {
1938 return EFI_UNSUPPORTED
;
1941 // This driver does not support setting the WRITE_COMBINE or the CACHED attributes.
1942 // If Attributes is not 0, then return EFI_UNSUPPORTED.
1944 Supports
= PciIoDevice
->Supports
& EFI_PCI_IO_ATTRIBUTE_MEMORY_CACHED
& EFI_PCI_IO_ATTRIBUTE_MEMORY_WRITE_COMBINE
;
1946 if (Attributes
!= (Attributes
& Supports
)) {
1947 return EFI_UNSUPPORTED
;
1950 // Attributes must be supported. Make sure the BAR range describd by BarIndex, Offset, and
1951 // Length are valid for this PCI device.
1953 NonRelativeOffset
= *Offset
;
1954 Status
= PciIoVerifyBarAccess (
1962 if (EFI_ERROR (Status
)) {
1963 return EFI_UNSUPPORTED
;
1970 Program parent bridge's attribute recurrently.
1972 @param PciIoDevice Child Pci device instance
1973 @param Operation The operation to perform on the attributes for this PCI controller.
1974 @param Attributes The mask of attributes that are used for Set, Enable, and Disable
1977 @retval EFI_SUCCESS The operation on the PCI controller's attributes was completed.
1978 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1979 @retval EFI_UNSUPPORTED one or more of the bits set in
1980 Attributes are not supported by this PCI controller or one of
1981 its parent bridges when Operation is Set, Enable or Disable.
1985 UpStreamBridgesAttributes (
1986 IN PCI_IO_DEVICE
*PciIoDevice
,
1987 IN EFI_PCI_IO_PROTOCOL_ATTRIBUTE_OPERATION Operation
,
1988 IN UINT64 Attributes
1991 PCI_IO_DEVICE
*Parent
;
1992 EFI_PCI_IO_PROTOCOL
*PciIo
;
1994 Parent
= PciIoDevice
->Parent
;
1996 while (Parent
!= NULL
&& IS_PCI_BRIDGE (&Parent
->Pci
)) {
1999 // Get the PciIo Protocol
2001 PciIo
= &Parent
->PciIo
;
2003 PciIo
->Attributes (PciIo
, Operation
, Attributes
, NULL
);
2005 Parent
= Parent
->Parent
;
2012 Test whether two Pci devices has same parent bridge.
2014 @param PciDevice1 The first pci device for testing.
2015 @param PciDevice2 The second pci device for testing.
2017 @retval TRUE Two Pci device has the same parent bridge.
2018 @retval FALSE Two Pci device has not the same parent bridge.
2022 PciDevicesOnTheSamePath (
2023 IN PCI_IO_DEVICE
*PciDevice1
,
2024 IN PCI_IO_DEVICE
*PciDevice2
2030 if (PciDevice1
->Parent
== PciDevice2
->Parent
) {
2034 Existed1
= PciDeviceExisted (PciDevice1
->Parent
, PciDevice2
);
2035 Existed2
= PciDeviceExisted (PciDevice2
->Parent
, PciDevice1
);
2037 return (BOOLEAN
) (Existed1
|| Existed2
);