2 EFI PCI IO protocol functions implementation for PCI Bus module.
4 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 extern EDKII_IOMMU_PROTOCOL
*mIoMmuProtocol
;
14 // Pci Io Protocol Interface
16 EFI_PCI_IO_PROTOCOL mPciIoInterface
= {
39 PciIoGetBarAttributes
,
40 PciIoSetBarAttributes
,
46 Initializes a PCI I/O Instance.
48 @param PciIoDevice Pci device instance.
52 InitializePciIoInstance (
53 IN PCI_IO_DEVICE
*PciIoDevice
56 CopyMem (&PciIoDevice
->PciIo
, &mPciIoInterface
, sizeof (EFI_PCI_IO_PROTOCOL
));
60 Verifies access to a PCI Base Address Register (BAR).
62 @param PciIoDevice Pci device instance.
63 @param BarIndex The BAR index of the standard PCI Configuration header to use as the
64 base address for the memory or I/O operation to perform.
65 @param Type Operation type could be memory or I/O.
66 @param Width Signifies the width of the memory or I/O operations.
67 @param Count The number of memory or I/O operations to perform.
68 @param Offset The offset within the PCI configuration space for the PCI controller.
70 @retval EFI_INVALID_PARAMETER Invalid Width/BarIndex or Bar type.
71 @retval EFI_SUCCESS Successfully verified.
75 PciIoVerifyBarAccess (
76 IN PCI_IO_DEVICE
*PciIoDevice
,
79 IN IN EFI_PCI_IO_PROTOCOL_WIDTH Width
,
84 if ((UINT32
)Width
>= EfiPciIoWidthMaximum
) {
85 return EFI_INVALID_PARAMETER
;
88 if (BarIndex
== EFI_PCI_IO_PASS_THROUGH_BAR
) {
93 // BarIndex 0-5 is legal
95 if (BarIndex
>= PCI_MAX_BAR
) {
96 return EFI_INVALID_PARAMETER
;
99 if (!CheckBarType (PciIoDevice
, BarIndex
, Type
)) {
100 return EFI_INVALID_PARAMETER
;
104 // If Width is EfiPciIoWidthFifoUintX then convert to EfiPciIoWidthUintX
105 // If Width is EfiPciIoWidthFillUintX then convert to EfiPciIoWidthUintX
107 if ((Width
>= EfiPciIoWidthFifoUint8
) && (Width
<= EfiPciIoWidthFifoUint64
)) {
111 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
)(Width
& 0x03);
113 if ((*Offset
+ Count
* (UINTN
)(1 << Width
)) - 1 >= PciIoDevice
->PciBar
[BarIndex
].Length
) {
114 return EFI_INVALID_PARAMETER
;
117 *Offset
= *Offset
+ PciIoDevice
->PciBar
[BarIndex
].BaseAddress
;
123 Verifies access to a PCI Configuration Header.
125 @param PciIoDevice Pci device instance.
126 @param Width Signifies the width of the memory or I/O operations.
127 @param Count The number of memory or I/O operations to perform.
128 @param Offset The offset within the PCI configuration space for the PCI controller.
130 @retval EFI_INVALID_PARAMETER Invalid Width
131 @retval EFI_UNSUPPORTED Offset overflowed.
132 @retval EFI_SUCCESS Successfully verified.
136 PciIoVerifyConfigAccess (
137 IN PCI_IO_DEVICE
*PciIoDevice
,
138 IN EFI_PCI_IO_PROTOCOL_WIDTH Width
,
145 if ((UINT32
)Width
>= EfiPciIoWidthMaximum
) {
146 return EFI_INVALID_PARAMETER
;
150 // If Width is EfiPciIoWidthFillUintX then convert to EfiPciIoWidthUintX
152 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
)(Width
& 0x03);
154 if (PciIoDevice
->IsPciExp
) {
155 if ((*Offset
+ Count
* (UINTN
)(1 << Width
)) - 1 >= PCI_EXP_MAX_CONFIG_OFFSET
) {
156 return EFI_UNSUPPORTED
;
159 ExtendOffset
= LShiftU64 (*Offset
, 32);
160 *Offset
= EFI_PCI_ADDRESS (PciIoDevice
->BusNumber
, PciIoDevice
->DeviceNumber
, PciIoDevice
->FunctionNumber
, 0);
161 *Offset
= (*Offset
) | ExtendOffset
;
163 if ((*Offset
+ Count
* (UINTN
)(1 << Width
)) - 1 >= PCI_MAX_CONFIG_OFFSET
) {
164 return EFI_UNSUPPORTED
;
167 *Offset
= EFI_PCI_ADDRESS (PciIoDevice
->BusNumber
, PciIoDevice
->DeviceNumber
, PciIoDevice
->FunctionNumber
, *Offset
);
174 Reads from the memory space of a PCI controller. Returns either when the polling exit criteria is
175 satisfied or after a defined duration.
177 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
178 @param Width Signifies the width of the memory or I/O operations.
179 @param BarIndex The BAR index of the standard PCI Configuration header to use as the
180 base address for the memory operation to perform.
181 @param Offset The offset within the selected BAR to start the memory operation.
182 @param Mask Mask used for the polling criteria.
183 @param Value The comparison value used for the polling exit criteria.
184 @param Delay The number of 100 ns units to poll.
185 @param Result Pointer to the last value read from the memory location.
187 @retval EFI_SUCCESS The last data returned from the access matched the poll exit criteria.
188 @retval EFI_UNSUPPORTED BarIndex not valid for this PCI controller.
189 @retval EFI_UNSUPPORTED Offset is not valid for the BarIndex of this PCI controller.
190 @retval EFI_TIMEOUT Delay expired before a match occurred.
191 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
192 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
198 IN EFI_PCI_IO_PROTOCOL
*This
,
199 IN EFI_PCI_IO_PROTOCOL_WIDTH Width
,
209 PCI_IO_DEVICE
*PciIoDevice
;
211 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
213 if ((UINT32
)Width
>= EfiPciIoWidthMaximum
) {
214 return EFI_INVALID_PARAMETER
;
217 Status
= PciIoVerifyBarAccess (PciIoDevice
, BarIndex
, PciBarTypeMem
, Width
, 1, &Offset
);
218 if (EFI_ERROR (Status
)) {
219 return EFI_UNSUPPORTED
;
222 if (Width
> EfiPciIoWidthUint64
) {
223 return EFI_INVALID_PARAMETER
;
227 // If request is not aligned, then convert request to EfiPciIoWithXXXUint8
229 if (FeaturePcdGet (PcdUnalignedPciIoEnable
)) {
230 if ((Offset
& ((1 << (Width
& 0x03)) - 1)) != 0) {
231 Status
= PciIoMemRead (This
, Width
, BarIndex
, Offset
, 1, Result
);
232 if (EFI_ERROR (Status
)) {
236 if (((*Result
& Mask
) == Value
) || (Delay
== 0)) {
242 // Stall 10 us = 100 * 100ns
246 Status
= PciIoMemRead (This
, Width
, BarIndex
, Offset
, 1, Result
);
247 if (EFI_ERROR (Status
)) {
251 if ((*Result
& Mask
) == Value
) {
264 Status
= PciIoDevice
->PciRootBridgeIo
->PollMem (
265 PciIoDevice
->PciRootBridgeIo
,
266 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH
)Width
,
274 if (EFI_ERROR (Status
)) {
275 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
276 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
277 EFI_IO_BUS_PCI
| EFI_IOB_EC_CONTROLLER_ERROR
,
278 PciIoDevice
->DevicePath
286 Reads from the memory space of a PCI controller. Returns either when the polling exit criteria is
287 satisfied or after a defined duration.
289 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
290 @param Width Signifies the width of the memory or I/O operations.
291 @param BarIndex The BAR index of the standard PCI Configuration header to use as the
292 base address for the memory operation to perform.
293 @param Offset The offset within the selected BAR to start the memory operation.
294 @param Mask Mask used for the polling criteria.
295 @param Value The comparison value used for the polling exit criteria.
296 @param Delay The number of 100 ns units to poll.
297 @param Result Pointer to the last value read from the memory location.
299 @retval EFI_SUCCESS The last data returned from the access matched the poll exit criteria.
300 @retval EFI_UNSUPPORTED BarIndex not valid for this PCI controller.
301 @retval EFI_UNSUPPORTED Offset is not valid for the BarIndex of this PCI controller.
302 @retval EFI_TIMEOUT Delay expired before a match occurred.
303 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
304 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
310 IN EFI_PCI_IO_PROTOCOL
*This
,
311 IN EFI_PCI_IO_PROTOCOL_WIDTH Width
,
321 PCI_IO_DEVICE
*PciIoDevice
;
323 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
325 if ((UINT32
)Width
> EfiPciIoWidthUint64
) {
326 return EFI_INVALID_PARAMETER
;
329 Status
= PciIoVerifyBarAccess (PciIoDevice
, BarIndex
, PciBarTypeIo
, Width
, 1, &Offset
);
330 if (EFI_ERROR (Status
)) {
331 return EFI_UNSUPPORTED
;
335 // If request is not aligned, then convert request to EfiPciIoWithXXXUint8
337 if (FeaturePcdGet (PcdUnalignedPciIoEnable
)) {
338 if ((Offset
& ((1 << (Width
& 0x03)) - 1)) != 0) {
339 Status
= PciIoIoRead (This
, Width
, BarIndex
, Offset
, 1, Result
);
340 if (EFI_ERROR (Status
)) {
344 if (((*Result
& Mask
) == Value
) || (Delay
== 0)) {
350 // Stall 10 us = 100 * 100ns
354 Status
= PciIoIoRead (This
, Width
, BarIndex
, Offset
, 1, Result
);
355 if (EFI_ERROR (Status
)) {
359 if ((*Result
& Mask
) == Value
) {
372 Status
= PciIoDevice
->PciRootBridgeIo
->PollIo (
373 PciIoDevice
->PciRootBridgeIo
,
374 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH
)Width
,
382 if (EFI_ERROR (Status
)) {
383 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
384 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
385 EFI_IO_BUS_PCI
| EFI_IOB_EC_CONTROLLER_ERROR
,
386 PciIoDevice
->DevicePath
394 Enable a PCI driver to access PCI controller registers in the PCI memory or I/O space.
396 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
397 @param Width Signifies the width of the memory or I/O operations.
398 @param BarIndex The BAR index of the standard PCI Configuration header to use as the
399 base address for the memory or I/O operation to perform.
400 @param Offset The offset within the selected BAR to start the memory or I/O operation.
401 @param Count The number of memory or I/O operations to perform.
402 @param Buffer For read operations, the destination buffer to store the results. For write
403 operations, the source buffer to write data from.
405 @retval EFI_SUCCESS The data was read from or written to the PCI controller.
406 @retval EFI_UNSUPPORTED BarIndex not valid for this PCI controller.
407 @retval EFI_UNSUPPORTED The address range specified by Offset, Width, and Count is not
408 valid for the PCI BAR specified by BarIndex.
409 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
410 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
416 IN EFI_PCI_IO_PROTOCOL
*This
,
417 IN EFI_PCI_IO_PROTOCOL_WIDTH Width
,
425 PCI_IO_DEVICE
*PciIoDevice
;
427 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
429 if ((UINT32
)Width
>= EfiPciIoWidthMaximum
) {
430 return EFI_INVALID_PARAMETER
;
433 if (Buffer
== NULL
) {
434 return EFI_INVALID_PARAMETER
;
437 Status
= PciIoVerifyBarAccess (PciIoDevice
, BarIndex
, PciBarTypeMem
, Width
, Count
, &Offset
);
438 if (EFI_ERROR (Status
)) {
439 return EFI_UNSUPPORTED
;
443 // If request is not aligned, then convert request to EfiPciIoWithXXXUint8
445 if (FeaturePcdGet (PcdUnalignedPciIoEnable
)) {
446 if ((Offset
& ((1 << (Width
& 0x03)) - 1)) != 0) {
447 Count
*= (UINTN
)(1 << (Width
& 0x03));
448 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
)(Width
& (~0x03));
452 Status
= PciIoDevice
->PciRootBridgeIo
->Mem
.Read (
453 PciIoDevice
->PciRootBridgeIo
,
454 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH
)Width
,
460 if (EFI_ERROR (Status
)) {
461 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
462 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
463 EFI_IO_BUS_PCI
| EFI_IOB_EC_READ_ERROR
,
464 PciIoDevice
->DevicePath
472 Enable a PCI driver to access PCI controller registers in the PCI memory or I/O space.
474 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
475 @param Width Signifies the width of the memory or I/O operations.
476 @param BarIndex The BAR index of the standard PCI Configuration header to use as the
477 base address for the memory or I/O operation to perform.
478 @param Offset The offset within the selected BAR to start the memory or I/O operation.
479 @param Count The number of memory or I/O operations to perform.
480 @param Buffer For read operations, the destination buffer to store the results. For write
481 operations, the source buffer to write data from.
483 @retval EFI_SUCCESS The data was read from or written to the PCI controller.
484 @retval EFI_UNSUPPORTED BarIndex not valid for this PCI controller.
485 @retval EFI_UNSUPPORTED The address range specified by Offset, Width, and Count is not
486 valid for the PCI BAR specified by BarIndex.
487 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
488 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
494 IN EFI_PCI_IO_PROTOCOL
*This
,
495 IN EFI_PCI_IO_PROTOCOL_WIDTH Width
,
503 PCI_IO_DEVICE
*PciIoDevice
;
505 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
507 if ((UINT32
)Width
>= EfiPciIoWidthMaximum
) {
508 return EFI_INVALID_PARAMETER
;
511 if (Buffer
== NULL
) {
512 return EFI_INVALID_PARAMETER
;
515 Status
= PciIoVerifyBarAccess (PciIoDevice
, BarIndex
, PciBarTypeMem
, Width
, Count
, &Offset
);
516 if (EFI_ERROR (Status
)) {
517 return EFI_UNSUPPORTED
;
521 // If request is not aligned, then convert request to EfiPciIoWithXXXUint8
523 if (FeaturePcdGet (PcdUnalignedPciIoEnable
)) {
524 if ((Offset
& ((1 << (Width
& 0x03)) - 1)) != 0) {
525 Count
*= (UINTN
)(1 << (Width
& 0x03));
526 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
)(Width
& (~0x03));
530 Status
= PciIoDevice
->PciRootBridgeIo
->Mem
.Write (
531 PciIoDevice
->PciRootBridgeIo
,
532 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH
)Width
,
538 if (EFI_ERROR (Status
)) {
539 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
540 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
541 EFI_IO_BUS_PCI
| EFI_IOB_EC_WRITE_ERROR
,
542 PciIoDevice
->DevicePath
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 ((UINT32
)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 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
618 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
619 EFI_IO_BUS_PCI
| EFI_IOB_EC_READ_ERROR
,
620 PciIoDevice
->DevicePath
628 Enable a PCI driver to access PCI controller registers in the PCI memory or I/O space.
630 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
631 @param Width Signifies the width of the memory or I/O operations.
632 @param BarIndex The BAR index of the standard PCI Configuration header to use as the
633 base address for the memory or I/O operation to perform.
634 @param Offset The offset within the selected BAR to start the memory or I/O operation.
635 @param Count The number of memory or I/O operations to perform.
636 @param Buffer For read operations, the destination buffer to store the results. For write
637 operations, the source buffer to write data from.
639 @retval EFI_SUCCESS The data was read from or written to the PCI controller.
640 @retval EFI_UNSUPPORTED BarIndex not valid for this PCI controller.
641 @retval EFI_UNSUPPORTED The address range specified by Offset, Width, and Count is not
642 valid for the PCI BAR specified by BarIndex.
643 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
644 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
650 IN EFI_PCI_IO_PROTOCOL
*This
,
651 IN EFI_PCI_IO_PROTOCOL_WIDTH Width
,
659 PCI_IO_DEVICE
*PciIoDevice
;
661 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
663 if ((UINT32
)Width
>= EfiPciIoWidthMaximum
) {
664 return EFI_INVALID_PARAMETER
;
667 if (Buffer
== NULL
) {
668 return EFI_INVALID_PARAMETER
;
671 Status
= PciIoVerifyBarAccess (PciIoDevice
, BarIndex
, PciBarTypeIo
, Width
, Count
, &Offset
);
672 if (EFI_ERROR (Status
)) {
673 return EFI_UNSUPPORTED
;
677 // If request is not aligned, then convert request to EfiPciIoWithXXXUint8
679 if (FeaturePcdGet (PcdUnalignedPciIoEnable
)) {
680 if ((Offset
& ((1 << (Width
& 0x03)) - 1)) != 0) {
681 Count
*= (UINTN
)(1 << (Width
& 0x03));
682 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
)(Width
& (~0x03));
686 Status
= PciIoDevice
->PciRootBridgeIo
->Io
.Write (
687 PciIoDevice
->PciRootBridgeIo
,
688 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH
)Width
,
694 if (EFI_ERROR (Status
)) {
695 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
696 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
697 EFI_IO_BUS_PCI
| EFI_IOB_EC_WRITE_ERROR
,
698 PciIoDevice
->DevicePath
706 Enable a PCI driver to access PCI controller registers in PCI configuration space.
708 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
709 @param Width Signifies the width of the memory operations.
710 @param Offset The offset within the PCI configuration space for the PCI controller.
711 @param Count The number of PCI configuration operations to perform.
712 @param Buffer For read operations, the destination buffer to store the results. For write
713 operations, the source buffer to write data from.
716 @retval EFI_SUCCESS The data was read from or written to the PCI controller.
717 @retval EFI_UNSUPPORTED The address range specified by Offset, Width, and Count is not
718 valid for the PCI configuration header of the PCI controller.
719 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
720 @retval EFI_INVALID_PARAMETER Buffer is NULL or Width is invalid.
726 IN EFI_PCI_IO_PROTOCOL
*This
,
727 IN EFI_PCI_IO_PROTOCOL_WIDTH Width
,
734 PCI_IO_DEVICE
*PciIoDevice
;
737 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
740 Status
= PciIoVerifyConfigAccess (PciIoDevice
, Width
, Count
, &Address
);
741 if (EFI_ERROR (Status
)) {
746 // If request is not aligned, then convert request to EfiPciIoWithXXXUint8
748 if (FeaturePcdGet (PcdUnalignedPciIoEnable
)) {
749 if ((Offset
& ((1 << (Width
& 0x03)) - 1)) != 0) {
750 Count
*= (UINTN
)(1 << (Width
& 0x03));
751 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
)(Width
& (~0x03));
755 Status
= PciIoDevice
->PciRootBridgeIo
->Pci
.Read (
756 PciIoDevice
->PciRootBridgeIo
,
757 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH
)Width
,
763 if (EFI_ERROR (Status
)) {
764 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
765 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
766 EFI_IO_BUS_PCI
| EFI_IOB_EC_READ_ERROR
,
767 PciIoDevice
->DevicePath
775 Enable a PCI driver to access PCI controller registers in PCI configuration space.
777 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
778 @param Width Signifies the width of the memory operations.
779 @param Offset The offset within the PCI configuration space for the PCI controller.
780 @param Count The number of PCI configuration operations to perform.
781 @param Buffer For read operations, the destination buffer to store the results. For write
782 operations, the source buffer to write data from.
785 @retval EFI_SUCCESS The data was read from or written to the PCI controller.
786 @retval EFI_UNSUPPORTED The address range specified by Offset, Width, and Count is not
787 valid for the PCI configuration header of the PCI controller.
788 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
789 @retval EFI_INVALID_PARAMETER Buffer is NULL or Width is invalid.
795 IN EFI_PCI_IO_PROTOCOL
*This
,
796 IN EFI_PCI_IO_PROTOCOL_WIDTH Width
,
803 PCI_IO_DEVICE
*PciIoDevice
;
806 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
809 Status
= PciIoVerifyConfigAccess (PciIoDevice
, Width
, Count
, &Address
);
810 if (EFI_ERROR (Status
)) {
815 // If request is not aligned, then convert request to EfiPciIoWithXXXUint8
817 if (FeaturePcdGet (PcdUnalignedPciIoEnable
)) {
818 if ((Offset
& ((1 << (Width
& 0x03)) - 1)) != 0) {
819 Count
*= (UINTN
)(1 << (Width
& 0x03));
820 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
)(Width
& (~0x03));
824 Status
= PciIoDevice
->PciRootBridgeIo
->Pci
.Write (
825 PciIoDevice
->PciRootBridgeIo
,
826 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH
)Width
,
832 if (EFI_ERROR (Status
)) {
833 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
834 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
835 EFI_IO_BUS_PCI
| EFI_IOB_EC_WRITE_ERROR
,
836 PciIoDevice
->DevicePath
844 Enables a PCI driver to copy one region of PCI memory space to another region of PCI
847 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
848 @param Width Signifies the width of the memory operations.
849 @param DestBarIndex The BAR index in the standard PCI Configuration header to use as the
850 base address for the memory operation to perform.
851 @param DestOffset The destination offset within the BAR specified by DestBarIndex to
852 start the memory writes for the copy operation.
853 @param SrcBarIndex The BAR index in the standard PCI Configuration header to use as the
854 base address for the memory operation to perform.
855 @param SrcOffset The source offset within the BAR specified by SrcBarIndex to start
856 the memory reads for the copy operation.
857 @param Count The number of memory operations to perform. Bytes moved is Width
858 size * Count, starting at DestOffset and SrcOffset.
860 @retval EFI_SUCCESS The data was copied from one memory region to another memory region.
861 @retval EFI_UNSUPPORTED DestBarIndex not valid for this PCI controller.
862 @retval EFI_UNSUPPORTED SrcBarIndex not valid for this PCI controller.
863 @retval EFI_UNSUPPORTED The address range specified by DestOffset, Width, and Count
864 is not valid for the PCI BAR specified by DestBarIndex.
865 @retval EFI_UNSUPPORTED The address range specified by SrcOffset, Width, and Count is
866 not valid for the PCI BAR specified by SrcBarIndex.
867 @retval EFI_INVALID_PARAMETER Width is invalid.
868 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
874 IN EFI_PCI_IO_PROTOCOL
*This
,
875 IN EFI_PCI_IO_PROTOCOL_WIDTH Width
,
876 IN UINT8 DestBarIndex
,
877 IN UINT64 DestOffset
,
878 IN UINT8 SrcBarIndex
,
884 PCI_IO_DEVICE
*PciIoDevice
;
886 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
888 if ((UINT32
)Width
>= EfiPciIoWidthMaximum
) {
889 return EFI_INVALID_PARAMETER
;
892 if ((Width
== EfiPciIoWidthFifoUint8
) ||
893 (Width
== EfiPciIoWidthFifoUint16
) ||
894 (Width
== EfiPciIoWidthFifoUint32
) ||
895 (Width
== EfiPciIoWidthFifoUint64
) ||
896 (Width
== EfiPciIoWidthFillUint8
) ||
897 (Width
== EfiPciIoWidthFillUint16
) ||
898 (Width
== EfiPciIoWidthFillUint32
) ||
899 (Width
== EfiPciIoWidthFillUint64
))
901 return EFI_INVALID_PARAMETER
;
904 Status
= PciIoVerifyBarAccess (PciIoDevice
, DestBarIndex
, PciBarTypeMem
, Width
, Count
, &DestOffset
);
905 if (EFI_ERROR (Status
)) {
906 return EFI_UNSUPPORTED
;
909 Status
= PciIoVerifyBarAccess (PciIoDevice
, SrcBarIndex
, PciBarTypeMem
, Width
, Count
, &SrcOffset
);
910 if (EFI_ERROR (Status
)) {
911 return EFI_UNSUPPORTED
;
915 // If request is not aligned, then convert request to EfiPciIoWithXXXUint8
917 if (FeaturePcdGet (PcdUnalignedPciIoEnable
)) {
918 if (((SrcOffset
& ((1 << (Width
& 0x03)) - 1)) != 0) || ((DestOffset
& ((1 << (Width
& 0x03)) - 1)) != 0)) {
919 Count
*= (UINTN
)(1 << (Width
& 0x03));
920 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
)(Width
& (~0x03));
924 Status
= PciIoDevice
->PciRootBridgeIo
->CopyMem (
925 PciIoDevice
->PciRootBridgeIo
,
926 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH
)Width
,
932 if (EFI_ERROR (Status
)) {
933 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
934 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
935 EFI_IO_BUS_PCI
| EFI_IOB_EC_CONTROLLER_ERROR
,
936 PciIoDevice
->DevicePath
944 Provides the PCI controller-specific addresses needed to access system memory.
946 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
947 @param Operation Indicates if the bus master is going to read or write to system memory.
948 @param HostAddress The system memory address to map to the PCI controller.
949 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes
951 @param DeviceAddress The resulting map address for the bus master PCI controller to use to
952 access the hosts HostAddress.
953 @param Mapping A resulting value to pass to Unmap().
955 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
956 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
957 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
958 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
959 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
965 IN EFI_PCI_IO_PROTOCOL
*This
,
966 IN EFI_PCI_IO_PROTOCOL_OPERATION Operation
,
967 IN VOID
*HostAddress
,
968 IN OUT UINTN
*NumberOfBytes
,
969 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
974 PCI_IO_DEVICE
*PciIoDevice
;
975 UINT64 IoMmuAttribute
;
976 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_OPERATION RootBridgeIoOperation
;
978 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
980 if ((UINT32
)Operation
>= EfiPciIoOperationMaximum
) {
981 return EFI_INVALID_PARAMETER
;
984 if ((HostAddress
== NULL
) || (NumberOfBytes
== NULL
) || (DeviceAddress
== NULL
) || (Mapping
== NULL
)) {
985 return EFI_INVALID_PARAMETER
;
988 RootBridgeIoOperation
= (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_OPERATION
)Operation
;
989 if ((PciIoDevice
->Attributes
& EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE
) != 0) {
990 RootBridgeIoOperation
= (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_OPERATION
)(Operation
+ EfiPciOperationBusMasterRead64
);
993 Status
= PciIoDevice
->PciRootBridgeIo
->Map (
994 PciIoDevice
->PciRootBridgeIo
,
995 RootBridgeIoOperation
,
1002 if (EFI_ERROR (Status
)) {
1003 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1004 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
1005 EFI_IO_BUS_PCI
| EFI_IOB_EC_CONTROLLER_ERROR
,
1006 PciIoDevice
->DevicePath
1010 if (mIoMmuProtocol
!= NULL
) {
1011 if (!EFI_ERROR (Status
)) {
1012 switch (Operation
) {
1013 case EfiPciIoOperationBusMasterRead
:
1014 IoMmuAttribute
= EDKII_IOMMU_ACCESS_READ
;
1016 case EfiPciIoOperationBusMasterWrite
:
1017 IoMmuAttribute
= EDKII_IOMMU_ACCESS_WRITE
;
1019 case EfiPciIoOperationBusMasterCommonBuffer
:
1020 IoMmuAttribute
= EDKII_IOMMU_ACCESS_READ
| EDKII_IOMMU_ACCESS_WRITE
;
1024 return EFI_INVALID_PARAMETER
;
1027 mIoMmuProtocol
->SetAttribute (
1029 PciIoDevice
->Handle
,
1040 Completes the Map() operation and releases any corresponding resources.
1042 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
1043 @param Mapping The mapping value returned from Map().
1045 @retval EFI_SUCCESS The range was unmapped.
1046 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
1052 IN EFI_PCI_IO_PROTOCOL
*This
,
1057 PCI_IO_DEVICE
*PciIoDevice
;
1059 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
1061 if (mIoMmuProtocol
!= NULL
) {
1062 mIoMmuProtocol
->SetAttribute (
1064 PciIoDevice
->Handle
,
1070 Status
= PciIoDevice
->PciRootBridgeIo
->Unmap (
1071 PciIoDevice
->PciRootBridgeIo
,
1075 if (EFI_ERROR (Status
)) {
1076 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1077 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
1078 EFI_IO_BUS_PCI
| EFI_IOB_EC_CONTROLLER_ERROR
,
1079 PciIoDevice
->DevicePath
1087 Allocates pages that are suitable for an EfiPciIoOperationBusMasterCommonBuffer
1088 or EfiPciOperationBusMasterCommonBuffer64 mapping.
1090 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
1091 @param Type This parameter is not used and must be ignored.
1092 @param MemoryType The type of memory to allocate, EfiBootServicesData or
1093 EfiRuntimeServicesData.
1094 @param Pages The number of pages to allocate.
1095 @param HostAddress A pointer to store the base system memory address of the
1097 @param Attributes The requested bit mask of attributes for the allocated range.
1099 @retval EFI_SUCCESS The requested memory pages were allocated.
1100 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
1101 MEMORY_WRITE_COMBINE, MEMORY_CACHED and DUAL_ADDRESS_CYCLE.
1102 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1103 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
1108 PciIoAllocateBuffer (
1109 IN EFI_PCI_IO_PROTOCOL
*This
,
1110 IN EFI_ALLOCATE_TYPE Type
,
1111 IN EFI_MEMORY_TYPE MemoryType
,
1113 OUT VOID
**HostAddress
,
1114 IN UINT64 Attributes
1118 PCI_IO_DEVICE
*PciIoDevice
;
1121 (~(EFI_PCI_ATTRIBUTE_MEMORY_WRITE_COMBINE
| EFI_PCI_ATTRIBUTE_MEMORY_CACHED
))) != 0)
1123 return EFI_UNSUPPORTED
;
1126 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
1128 if ((PciIoDevice
->Attributes
& EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE
) != 0) {
1129 Attributes
|= EFI_PCI_ATTRIBUTE_DUAL_ADDRESS_CYCLE
;
1132 Status
= PciIoDevice
->PciRootBridgeIo
->AllocateBuffer (
1133 PciIoDevice
->PciRootBridgeIo
,
1141 if (EFI_ERROR (Status
)) {
1142 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1143 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
1144 EFI_IO_BUS_PCI
| EFI_IOB_EC_CONTROLLER_ERROR
,
1145 PciIoDevice
->DevicePath
1153 Frees memory that was allocated with AllocateBuffer().
1155 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
1156 @param Pages The number of pages to free.
1157 @param HostAddress The base system memory address of the allocated range.
1159 @retval EFI_SUCCESS The requested memory pages were freed.
1160 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages
1161 was not allocated with AllocateBuffer().
1167 IN EFI_PCI_IO_PROTOCOL
*This
,
1169 IN VOID
*HostAddress
1173 PCI_IO_DEVICE
*PciIoDevice
;
1175 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
1177 Status
= PciIoDevice
->PciRootBridgeIo
->FreeBuffer (
1178 PciIoDevice
->PciRootBridgeIo
,
1183 if (EFI_ERROR (Status
)) {
1184 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1185 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
1186 EFI_IO_BUS_PCI
| EFI_IOB_EC_CONTROLLER_ERROR
,
1187 PciIoDevice
->DevicePath
1195 Flushes all PCI posted write transactions from a PCI host bridge to system memory.
1197 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
1199 @retval EFI_SUCCESS The PCI posted write transactions were flushed from the PCI host
1200 bridge to system memory.
1201 @retval EFI_DEVICE_ERROR The PCI posted write transactions were not flushed from the PCI
1202 host bridge due to a hardware error.
1208 IN EFI_PCI_IO_PROTOCOL
*This
1212 PCI_IO_DEVICE
*PciIoDevice
;
1214 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
1216 Status
= PciIoDevice
->PciRootBridgeIo
->Flush (
1217 PciIoDevice
->PciRootBridgeIo
1219 if (EFI_ERROR (Status
)) {
1220 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1221 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
1222 EFI_IO_BUS_PCI
| EFI_IOB_EC_CONTROLLER_ERROR
,
1223 PciIoDevice
->DevicePath
1231 Retrieves this PCI controller's current PCI bus number, device number, and function number.
1233 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
1234 @param SegmentNumber The PCI controller's current PCI segment number.
1235 @param BusNumber The PCI controller's current PCI bus number.
1236 @param DeviceNumber The PCI controller's current PCI device number.
1237 @param FunctionNumber The PCI controller's current PCI function number.
1239 @retval EFI_SUCCESS The PCI controller location was returned.
1240 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1246 IN EFI_PCI_IO_PROTOCOL
*This
,
1253 PCI_IO_DEVICE
*PciIoDevice
;
1255 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
1257 if ((Segment
== NULL
) || (Bus
== NULL
) || (Device
== NULL
) || (Function
== NULL
)) {
1258 return EFI_INVALID_PARAMETER
;
1261 *Segment
= PciIoDevice
->PciRootBridgeIo
->SegmentNumber
;
1262 *Bus
= PciIoDevice
->BusNumber
;
1263 *Device
= PciIoDevice
->DeviceNumber
;
1264 *Function
= PciIoDevice
->FunctionNumber
;
1270 Check BAR type for PCI resource.
1272 @param PciIoDevice PCI device instance.
1273 @param BarIndex The BAR index of the standard PCI Configuration header to use as the
1274 base address for the memory or I/O operation to perform.
1275 @param BarType Memory or I/O.
1277 @retval TRUE Pci device's bar type is same with input BarType.
1278 @retval TRUE Pci device's bar type is not same with input BarType.
1283 IN PCI_IO_DEVICE
*PciIoDevice
,
1285 IN PCI_BAR_TYPE BarType
1291 if ((PciIoDevice
->PciBar
[BarIndex
].BarType
!= PciBarTypeMem32
) &&
1292 (PciIoDevice
->PciBar
[BarIndex
].BarType
!= PciBarTypePMem32
) &&
1293 (PciIoDevice
->PciBar
[BarIndex
].BarType
!= PciBarTypePMem64
) &&
1294 (PciIoDevice
->PciBar
[BarIndex
].BarType
!= PciBarTypeMem64
))
1302 if ((PciIoDevice
->PciBar
[BarIndex
].BarType
!= PciBarTypeIo32
) &&
1303 (PciIoDevice
->PciBar
[BarIndex
].BarType
!= PciBarTypeIo16
))
1318 Set/Disable new attributes to a Root Bridge.
1320 @param PciIoDevice Pci device instance.
1321 @param Attributes New attribute want to be set.
1322 @param Operation Set or Disable.
1324 @retval EFI_UNSUPPORTED If root bridge does not support change attribute.
1325 @retval EFI_SUCCESS Successfully set new attributes.
1329 ModifyRootBridgeAttributes (
1330 IN PCI_IO_DEVICE
*PciIoDevice
,
1331 IN UINT64 Attributes
,
1332 IN EFI_PCI_IO_PROTOCOL_ATTRIBUTE_OPERATION Operation
1335 UINT64 PciRootBridgeSupports
;
1336 UINT64 PciRootBridgeAttributes
;
1337 UINT64 NewPciRootBridgeAttributes
;
1341 // Get the current attributes of this PCI device's PCI Root Bridge
1343 Status
= PciIoDevice
->PciRootBridgeIo
->GetAttributes (
1344 PciIoDevice
->PciRootBridgeIo
,
1345 &PciRootBridgeSupports
,
1346 &PciRootBridgeAttributes
1348 if (EFI_ERROR (Status
)) {
1349 return EFI_UNSUPPORTED
;
1353 // Mask off attributes not supported by PCI root bridge.
1355 Attributes
&= ~(UINT64
)(EFI_PCI_IO_ATTRIBUTE_EMBEDDED_DEVICE
|
1356 EFI_PCI_IO_ATTRIBUTE_EMBEDDED_ROM
|
1357 EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE
);
1360 // Record the new attribute of the Root Bridge
1362 if (Operation
== EfiPciIoAttributeOperationEnable
) {
1363 NewPciRootBridgeAttributes
= PciRootBridgeAttributes
| Attributes
;
1365 NewPciRootBridgeAttributes
= PciRootBridgeAttributes
& (~Attributes
);
1369 // Call the PCI Root Bridge to attempt to modify the attributes
1371 if ((NewPciRootBridgeAttributes
^ PciRootBridgeAttributes
) != 0) {
1372 Status
= PciIoDevice
->PciRootBridgeIo
->SetAttributes (
1373 PciIoDevice
->PciRootBridgeIo
,
1374 NewPciRootBridgeAttributes
,
1378 if (EFI_ERROR (Status
)) {
1380 // The PCI Root Bridge could not modify the attributes, so return the error.
1382 return EFI_UNSUPPORTED
;
1387 // Also update the attributes for this Root Bridge structure
1389 PciIoDevice
->Attributes
= NewPciRootBridgeAttributes
;
1395 Check whether this device can be enable/disable to snoop.
1397 @param PciIoDevice Pci device instance.
1398 @param Operation Enable/Disable.
1400 @retval EFI_UNSUPPORTED Pci device is not GFX device or not support snoop.
1401 @retval EFI_SUCCESS Snoop can be supported.
1405 SupportPaletteSnoopAttributes (
1406 IN PCI_IO_DEVICE
*PciIoDevice
,
1407 IN EFI_PCI_IO_PROTOCOL_ATTRIBUTE_OPERATION Operation
1410 PCI_IO_DEVICE
*Temp
;
1414 // Snoop attribute can be only modified by GFX
1416 if (!IS_PCI_GFX (&PciIoDevice
->Pci
)) {
1417 return EFI_UNSUPPORTED
;
1421 // Get the boot VGA on the same Host Bridge
1423 Temp
= LocateVgaDeviceOnHostBridge (PciIoDevice
->PciRootBridgeIo
->ParentHandle
);
1427 // If there is no VGA device on the segment, set
1428 // this graphics card to decode the palette range
1434 // Check these two agents are on the same path
1436 if (!PciDevicesOnTheSamePath (Temp
, PciIoDevice
)) {
1438 // they are not on the same path, so snoop can be enabled or disabled
1444 // Check if they are on the same bus
1446 if (Temp
->Parent
== PciIoDevice
->Parent
) {
1447 PCI_READ_COMMAND_REGISTER (Temp
, &VGACommand
);
1450 // If they are on the same bus, either one can
1451 // be set to snoop, the other set to decode
1453 if ((VGACommand
& EFI_PCI_COMMAND_VGA_PALETTE_SNOOP
) != 0) {
1455 // VGA has set to snoop, so GFX can be only set to disable snoop
1457 if (Operation
== EfiPciIoAttributeOperationEnable
) {
1458 return EFI_UNSUPPORTED
;
1462 // VGA has disabled to snoop, so GFX can be only enabled
1464 if (Operation
== EfiPciIoAttributeOperationDisable
) {
1465 return EFI_UNSUPPORTED
;
1473 // If they are on the same path but on the different bus
1474 // The first agent is set to snoop, the second one set to
1478 if (Temp
->BusNumber
< PciIoDevice
->BusNumber
) {
1480 // GFX should be set to decode
1482 if (Operation
== EfiPciIoAttributeOperationDisable
) {
1483 PCI_ENABLE_COMMAND_REGISTER (Temp
, EFI_PCI_COMMAND_VGA_PALETTE_SNOOP
);
1484 Temp
->Attributes
|= EFI_PCI_COMMAND_VGA_PALETTE_SNOOP
;
1486 return EFI_UNSUPPORTED
;
1490 // GFX should be set to snoop
1492 if (Operation
== EfiPciIoAttributeOperationEnable
) {
1493 PCI_DISABLE_COMMAND_REGISTER (Temp
, EFI_PCI_COMMAND_VGA_PALETTE_SNOOP
);
1494 Temp
->Attributes
&= (~(UINT64
)EFI_PCI_COMMAND_VGA_PALETTE_SNOOP
);
1496 return EFI_UNSUPPORTED
;
1504 Performs an operation on the attributes that this PCI controller supports. The operations include
1505 getting the set of supported attributes, retrieving the current attributes, setting the current
1506 attributes, enabling attributes, and disabling attributes.
1508 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
1509 @param Operation The operation to perform on the attributes for this PCI controller.
1510 @param Attributes The mask of attributes that are used for Set, Enable, and Disable
1512 @param Result A pointer to the result mask of attributes that are returned for the Get
1513 and Supported operations.
1515 @retval EFI_SUCCESS The operation on the PCI controller's attributes was completed.
1516 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1517 @retval EFI_UNSUPPORTED one or more of the bits set in
1518 Attributes are not supported by this PCI controller or one of
1519 its parent bridges when Operation is Set, Enable or Disable.
1525 IN EFI_PCI_IO_PROTOCOL
*This
,
1526 IN EFI_PCI_IO_PROTOCOL_ATTRIBUTE_OPERATION Operation
,
1527 IN UINT64 Attributes
,
1528 OUT UINT64
*Result OPTIONAL
1533 PCI_IO_DEVICE
*PciIoDevice
;
1534 PCI_IO_DEVICE
*UpStreamBridge
;
1535 PCI_IO_DEVICE
*Temp
;
1538 UINT64 UpStreamAttributes
;
1539 UINT16 BridgeControl
;
1542 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
1544 switch (Operation
) {
1545 case EfiPciIoAttributeOperationGet
:
1546 if (Result
== NULL
) {
1547 return EFI_INVALID_PARAMETER
;
1550 *Result
= PciIoDevice
->Attributes
;
1553 case EfiPciIoAttributeOperationSupported
:
1554 if (Result
== NULL
) {
1555 return EFI_INVALID_PARAMETER
;
1558 *Result
= PciIoDevice
->Supports
;
1561 case EfiPciIoAttributeOperationSet
:
1562 Status
= PciIoDevice
->PciIo
.Attributes (
1563 &(PciIoDevice
->PciIo
),
1564 EfiPciIoAttributeOperationEnable
,
1568 if (EFI_ERROR (Status
)) {
1569 return EFI_UNSUPPORTED
;
1572 Status
= PciIoDevice
->PciIo
.Attributes (
1573 &(PciIoDevice
->PciIo
),
1574 EfiPciIoAttributeOperationDisable
,
1575 (~Attributes
) & (PciIoDevice
->Supports
),
1578 if (EFI_ERROR (Status
)) {
1579 return EFI_UNSUPPORTED
;
1584 case EfiPciIoAttributeOperationEnable
:
1585 case EfiPciIoAttributeOperationDisable
:
1589 return EFI_INVALID_PARAMETER
;
1593 // Just a trick for ENABLE attribute
1594 // EFI_PCI_DEVICE_ENABLE is not defined in UEFI spec, which is the internal usage.
1595 // So, this logic doesn't conform to UEFI spec, which should be removed.
1596 // But this trick logic is still kept for some binary drivers that depend on it.
1598 if ((Attributes
& EFI_PCI_DEVICE_ENABLE
) == EFI_PCI_DEVICE_ENABLE
) {
1599 Attributes
&= (PciIoDevice
->Supports
);
1602 // Raise the EFI_P_PC_ENABLE Status code
1604 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1606 EFI_IO_BUS_PCI
| EFI_P_PC_ENABLE
,
1607 PciIoDevice
->DevicePath
1612 // Check VGA and VGA16, they can not be set at the same time
1614 if ((Attributes
& (EFI_PCI_IO_ATTRIBUTE_VGA_IO
| EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO
)) != 0) {
1615 if ((Attributes
& (EFI_PCI_IO_ATTRIBUTE_VGA_IO_16
| EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16
)) != 0) {
1616 return EFI_UNSUPPORTED
;
1621 // If no attributes can be supported, then return.
1622 // Otherwise, set the attributes that it can support.
1624 Supports
= (PciIoDevice
->Supports
) & Attributes
;
1625 if (Supports
!= Attributes
) {
1626 return EFI_UNSUPPORTED
;
1630 // For Root Bridge, just call RootBridgeIo to set attributes;
1632 if (PciIoDevice
->Parent
== NULL
) {
1633 Status
= ModifyRootBridgeAttributes (PciIoDevice
, Attributes
, Operation
);
1641 // For PPB & P2C, set relevant attribute bits
1643 if (IS_PCI_BRIDGE (&PciIoDevice
->Pci
) || IS_CARDBUS_BRIDGE (&PciIoDevice
->Pci
)) {
1644 if ((Attributes
& (EFI_PCI_IO_ATTRIBUTE_VGA_IO
| EFI_PCI_IO_ATTRIBUTE_VGA_IO_16
)) != 0) {
1645 BridgeControl
|= EFI_PCI_BRIDGE_CONTROL_VGA
;
1648 if ((Attributes
& EFI_PCI_IO_ATTRIBUTE_ISA_IO
) != 0) {
1649 BridgeControl
|= EFI_PCI_BRIDGE_CONTROL_ISA
;
1652 if ((Attributes
& (EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO
| EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16
)) != 0) {
1653 Command
|= EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO
;
1656 if ((Attributes
& (EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16
| EFI_PCI_IO_ATTRIBUTE_VGA_IO_16
)) != 0) {
1657 BridgeControl
|= EFI_PCI_BRIDGE_CONTROL_VGA_16
;
1661 // Do with the attributes on VGA
1662 // Only for VGA's legacy resource, we just can enable once.
1665 (EFI_PCI_IO_ATTRIBUTE_VGA_IO
|
1666 EFI_PCI_IO_ATTRIBUTE_VGA_IO_16
|
1667 EFI_PCI_IO_ATTRIBUTE_VGA_MEMORY
)) != 0)
1670 // Check if a VGA has been enabled before enabling a new one
1672 if (Operation
== EfiPciIoAttributeOperationEnable
) {
1674 // Check if there have been an active VGA device on the same Host Bridge
1676 Temp
= LocateVgaDeviceOnHostBridge (PciIoDevice
->PciRootBridgeIo
->ParentHandle
);
1677 if ((Temp
!= NULL
) && (Temp
!= PciIoDevice
)) {
1679 // An active VGA has been detected, so can not enable another
1681 return EFI_UNSUPPORTED
;
1687 // Do with the attributes on GFX
1689 if ((Attributes
& (EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO
| EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16
)) != 0) {
1690 if (Operation
== EfiPciIoAttributeOperationEnable
) {
1692 // Check if snoop can be enabled in current configuration
1694 Status
= SupportPaletteSnoopAttributes (PciIoDevice
, Operation
);
1696 if (EFI_ERROR (Status
)) {
1698 // Enable operation is forbidden, so mask the bit in attributes
1699 // so as to keep consistent with the actual Status
1701 // Attributes &= (~EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO);
1705 return EFI_UNSUPPORTED
;
1710 // It can be supported, so get ready to set the bit
1712 Command
|= EFI_PCI_COMMAND_VGA_PALETTE_SNOOP
;
1716 if ((Attributes
& EFI_PCI_IO_ATTRIBUTE_IO
) != 0) {
1717 Command
|= EFI_PCI_COMMAND_IO_SPACE
;
1720 if ((Attributes
& EFI_PCI_IO_ATTRIBUTE_MEMORY
) != 0) {
1721 Command
|= EFI_PCI_COMMAND_MEMORY_SPACE
;
1724 if ((Attributes
& EFI_PCI_IO_ATTRIBUTE_BUS_MASTER
) != 0) {
1725 Command
|= EFI_PCI_COMMAND_BUS_MASTER
;
1729 // The upstream bridge should be also set to relevant attribute
1730 // expect for IO, Mem and BusMaster
1732 UpStreamAttributes
= Attributes
&
1733 (~(EFI_PCI_IO_ATTRIBUTE_IO
|
1734 EFI_PCI_IO_ATTRIBUTE_MEMORY
|
1735 EFI_PCI_IO_ATTRIBUTE_BUS_MASTER
1738 UpStreamBridge
= PciIoDevice
->Parent
;
1740 if (Operation
== EfiPciIoAttributeOperationEnable
) {
1742 // Enable relevant attributes to command register and bridge control register
1744 Status
= PCI_ENABLE_COMMAND_REGISTER (PciIoDevice
, Command
);
1745 if (BridgeControl
!= 0) {
1746 Status
= PCI_ENABLE_BRIDGE_CONTROL_REGISTER (PciIoDevice
, BridgeControl
);
1749 PciIoDevice
->Attributes
|= Attributes
;
1752 // Enable attributes of the upstream bridge
1754 Status
= UpStreamBridge
->PciIo
.Attributes (
1755 &(UpStreamBridge
->PciIo
),
1756 EfiPciIoAttributeOperationEnable
,
1762 // Disable relevant attributes to command register and bridge control register
1764 Status
= PCI_DISABLE_COMMAND_REGISTER (PciIoDevice
, Command
);
1765 if (BridgeControl
!= 0) {
1766 Status
= PCI_DISABLE_BRIDGE_CONTROL_REGISTER (PciIoDevice
, BridgeControl
);
1769 PciIoDevice
->Attributes
&= (~Attributes
);
1770 Status
= EFI_SUCCESS
;
1773 if (EFI_ERROR (Status
)) {
1774 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1775 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
1776 EFI_IO_BUS_PCI
| EFI_IOB_EC_CONTROLLER_ERROR
,
1777 PciIoDevice
->DevicePath
1785 Retrieve the AddrTranslationOffset from RootBridgeIo for the
1788 @param RootBridgeIo Root Bridge IO instance.
1789 @param AddrRangeMin The base address of the MMIO.
1790 @param AddrLen The length of the MMIO.
1792 @retval The AddrTranslationOffset from RootBridgeIo for the
1793 specified range, or (UINT64) -1 if the range is not
1794 found in RootBridgeIo.
1797 GetMmioAddressTranslationOffset (
1798 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*RootBridgeIo
,
1799 UINT64 AddrRangeMin
,
1804 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Configuration
;
1806 Status
= RootBridgeIo
->Configuration (
1808 (VOID
**)&Configuration
1810 if (EFI_ERROR (Status
)) {
1814 // According to UEFI 2.7, EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL::Configuration()
1815 // returns host address instead of device address, while AddrTranslationOffset
1816 // is not zero, and device address = host address + AddrTranslationOffset, so
1817 // we convert host address to device address for range compare.
1818 while (Configuration
->Desc
== ACPI_ADDRESS_SPACE_DESCRIPTOR
) {
1819 if ((Configuration
->ResType
== ACPI_ADDRESS_SPACE_TYPE_MEM
) &&
1820 (Configuration
->AddrRangeMin
+ Configuration
->AddrTranslationOffset
<= AddrRangeMin
) &&
1821 (Configuration
->AddrRangeMin
+ Configuration
->AddrLen
+ Configuration
->AddrTranslationOffset
>= AddrRangeMin
+ AddrLen
)
1824 return Configuration
->AddrTranslationOffset
;
1831 // The resource occupied by BAR should be in the range reported by RootBridge.
1838 Gets the attributes that this PCI controller supports setting on a BAR using
1839 SetBarAttributes(), and retrieves the list of resource descriptors for a BAR.
1841 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
1842 @param BarIndex The BAR index of the standard PCI Configuration header to use as the
1843 base address for resource range. The legal range for this field is 0..5.
1844 @param Supports A pointer to the mask of attributes that this PCI controller supports
1845 setting for this BAR with SetBarAttributes().
1846 @param Resources A pointer to the resource descriptors that describe the current
1847 configuration of this BAR of the PCI controller.
1849 @retval EFI_SUCCESS If Supports is not NULL, then the attributes that the PCI
1850 controller supports are returned in Supports. If Resources
1851 is not NULL, then the resource descriptors that the PCI
1852 controller is currently using are returned in Resources.
1853 @retval EFI_INVALID_PARAMETER Both Supports and Attributes are NULL.
1854 @retval EFI_UNSUPPORTED BarIndex not valid for this PCI controller.
1855 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to allocate
1861 PciIoGetBarAttributes (
1862 IN EFI_PCI_IO_PROTOCOL
*This
,
1864 OUT UINT64
*Supports OPTIONAL
,
1865 OUT VOID
**Resources OPTIONAL
1868 PCI_IO_DEVICE
*PciIoDevice
;
1869 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptor
;
1870 EFI_ACPI_END_TAG_DESCRIPTOR
*End
;
1872 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
1874 if ((Supports
== NULL
) && (Resources
== NULL
)) {
1875 return EFI_INVALID_PARAMETER
;
1878 if ((BarIndex
>= PCI_MAX_BAR
) || (PciIoDevice
->PciBar
[BarIndex
].BarType
== PciBarTypeUnknown
)) {
1879 return EFI_UNSUPPORTED
;
1883 // This driver does not support modifications to the WRITE_COMBINE or
1884 // CACHED attributes for BAR ranges.
1886 if (Supports
!= NULL
) {
1887 *Supports
= PciIoDevice
->Supports
& EFI_PCI_IO_ATTRIBUTE_MEMORY_CACHED
& EFI_PCI_IO_ATTRIBUTE_MEMORY_WRITE_COMBINE
;
1890 if (Resources
!= NULL
) {
1891 Descriptor
= AllocateZeroPool (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) + sizeof (EFI_ACPI_END_TAG_DESCRIPTOR
));
1892 if (Descriptor
== NULL
) {
1893 return EFI_OUT_OF_RESOURCES
;
1896 *Resources
= Descriptor
;
1898 Descriptor
->Desc
= ACPI_ADDRESS_SPACE_DESCRIPTOR
;
1899 Descriptor
->Len
= (UINT16
)(sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) - 3);
1900 Descriptor
->AddrRangeMin
= PciIoDevice
->PciBar
[BarIndex
].BaseAddress
;
1901 Descriptor
->AddrLen
= PciIoDevice
->PciBar
[BarIndex
].Length
;
1902 Descriptor
->AddrRangeMax
= PciIoDevice
->PciBar
[BarIndex
].Alignment
;
1904 switch (PciIoDevice
->PciBar
[BarIndex
].BarType
) {
1905 case PciBarTypeIo16
:
1906 case PciBarTypeIo32
:
1910 Descriptor
->ResType
= ACPI_ADDRESS_SPACE_TYPE_IO
;
1913 case PciBarTypePMem32
:
1917 Descriptor
->SpecificFlag
= EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE
;
1921 case PciBarTypeMem32
:
1925 Descriptor
->ResType
= ACPI_ADDRESS_SPACE_TYPE_MEM
;
1929 Descriptor
->AddrSpaceGranularity
= 32;
1932 case PciBarTypePMem64
:
1936 Descriptor
->SpecificFlag
= EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE
;
1940 case PciBarTypeMem64
:
1944 Descriptor
->ResType
= ACPI_ADDRESS_SPACE_TYPE_MEM
;
1948 Descriptor
->AddrSpaceGranularity
= 64;
1958 End
= (EFI_ACPI_END_TAG_DESCRIPTOR
*)(Descriptor
+ 1);
1959 End
->Desc
= ACPI_END_TAG_DESCRIPTOR
;
1963 // Get the Address Translation Offset
1965 if (Descriptor
->ResType
== ACPI_ADDRESS_SPACE_TYPE_MEM
) {
1966 Descriptor
->AddrTranslationOffset
= GetMmioAddressTranslationOffset (
1967 PciIoDevice
->PciRootBridgeIo
,
1968 Descriptor
->AddrRangeMin
,
1971 if (Descriptor
->AddrTranslationOffset
== (UINT64
)-1) {
1972 FreePool (Descriptor
);
1973 return EFI_UNSUPPORTED
;
1977 // According to UEFI spec 2.7, we need return host address for
1978 // PciIo->GetBarAttributes, and host address = device address - translation.
1979 Descriptor
->AddrRangeMin
-= Descriptor
->AddrTranslationOffset
;
1986 Sets the attributes for a range of a BAR on a PCI controller.
1988 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.
1989 @param Attributes The mask of attributes to set for the resource range specified by
1990 BarIndex, Offset, and Length.
1991 @param BarIndex The BAR index of the standard PCI Configuration header to use as the
1992 base address for resource range. The legal range for this field is 0..5.
1993 @param Offset A pointer to the BAR relative base address of the resource range to be
1994 modified by the attributes specified by Attributes.
1995 @param Length A pointer to the length of the resource range to be modified by the
1996 attributes specified by Attributes.
1998 @retval EFI_SUCCESS The set of attributes specified by Attributes for the resource
1999 range specified by BarIndex, Offset, and Length were
2000 set on the PCI controller, and the actual resource range is returned
2001 in Offset and Length.
2002 @retval EFI_INVALID_PARAMETER Offset or Length is NULL.
2003 @retval EFI_UNSUPPORTED BarIndex not valid for this PCI controller.
2004 @retval EFI_OUT_OF_RESOURCES There are not enough resources to set the attributes on the
2005 resource range specified by BarIndex, Offset, and
2011 PciIoSetBarAttributes (
2012 IN EFI_PCI_IO_PROTOCOL
*This
,
2013 IN UINT64 Attributes
,
2015 IN OUT UINT64
*Offset
,
2016 IN OUT UINT64
*Length
2020 PCI_IO_DEVICE
*PciIoDevice
;
2021 UINT64 NonRelativeOffset
;
2024 PciIoDevice
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (This
);
2027 // Make sure Offset and Length are not NULL
2029 if ((Offset
== NULL
) || (Length
== NULL
)) {
2030 return EFI_INVALID_PARAMETER
;
2033 if (PciIoDevice
->PciBar
[BarIndex
].BarType
== PciBarTypeUnknown
) {
2034 return EFI_UNSUPPORTED
;
2038 // This driver does not support setting the WRITE_COMBINE or the CACHED attributes.
2039 // If Attributes is not 0, then return EFI_UNSUPPORTED.
2041 Supports
= PciIoDevice
->Supports
& EFI_PCI_IO_ATTRIBUTE_MEMORY_CACHED
& EFI_PCI_IO_ATTRIBUTE_MEMORY_WRITE_COMBINE
;
2043 if (Attributes
!= (Attributes
& Supports
)) {
2044 return EFI_UNSUPPORTED
;
2048 // Attributes must be supported. Make sure the BAR range described by BarIndex, Offset, and
2049 // Length are valid for this PCI device.
2051 NonRelativeOffset
= *Offset
;
2052 Status
= PciIoVerifyBarAccess (
2060 if (EFI_ERROR (Status
)) {
2061 return EFI_UNSUPPORTED
;
2068 Test whether two Pci devices has same parent bridge.
2070 @param PciDevice1 The first pci device for testing.
2071 @param PciDevice2 The second pci device for testing.
2073 @retval TRUE Two Pci device has the same parent bridge.
2074 @retval FALSE Two Pci device has not the same parent bridge.
2078 PciDevicesOnTheSamePath (
2079 IN PCI_IO_DEVICE
*PciDevice1
,
2080 IN PCI_IO_DEVICE
*PciDevice2
2086 if (PciDevice1
->Parent
== PciDevice2
->Parent
) {
2090 Existed1
= PciDeviceExisted (PciDevice1
->Parent
, PciDevice2
);
2091 Existed2
= PciDeviceExisted (PciDevice2
->Parent
, PciDevice1
);
2093 return (BOOLEAN
)(Existed1
|| Existed2
);