2 PCI Segment Library implementation using PCI CFG2 PPI.
4 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Ppi/PciCfg2.h>
13 #include <Library/PciSegmentLib.h>
14 #include <Library/BaseLib.h>
15 #include <Library/PeiServicesTablePointerLib.h>
16 #include <Library/DebugLib.h>
17 #include <Library/PeiServicesLib.h>
20 Assert the validity of a PCI Segment address.
21 A valid PCI Segment address should not contain 1's in bits 28..31 and 48..63
23 @param A The address to validate.
24 @param M Additional bits to assert to be zero.
27 #define ASSERT_INVALID_PCI_SEGMENT_ADDRESS(A,M) \
28 ASSERT (((A) & (0xffff0000f0000000ULL | (M))) == 0)
31 Translate PCI Lib address into format of PCI CFG2 PPI.
33 @param A The address that encodes the PCI Bus, Device, Function and
37 #define PCI_TO_PCICFG2_ADDRESS(A) \
38 ((((UINT32)(A) << 4) & 0xff000000) | (((UINT32)(A) >> 4) & 0x00000700) | (((UINT32)(A) << 1) & 0x001f0000) | (LShiftU64((A) & 0xfff, 32)))
43 This internal function retrieves PCI CFG2 PPI from PPI database.
45 @param Address The address that encodes the PCI Segment, Bus, Device,
46 Function and Register.
48 @return The pointer to PCI CFG2 PPI.
51 EFI_PEI_PCI_CFG2_PPI
*
52 InternalGetPciCfg2Ppi (
58 EFI_PEI_PCI_CFG2_PPI
*PciCfg2Ppi
;
63 SegmentNumber
= BitFieldRead64 (Address
, 32, 63);
66 // Loop through all instances of the PPI and match segment number
69 Status
= PeiServicesLocatePpi(
75 ASSERT_EFI_ERROR (Status
);
77 } while (PciCfg2Ppi
->Segment
!= SegmentNumber
);
83 Internal worker function to read a PCI configuration register.
85 This function wraps EFI_PEI_PCI_CFG2_PPI.Read() service.
86 It reads and returns the PCI configuration register specified by Address,
87 the width of data is specified by Width.
89 @param Address The address that encodes the PCI Bus, Device, Function and
91 @param Width The width of data to read
93 @return The value read from the PCI configuration register.
97 PeiPciSegmentLibPciCfg2ReadWorker (
99 IN EFI_PEI_PCI_CFG_PPI_WIDTH Width
103 CONST EFI_PEI_PCI_CFG2_PPI
*PciCfg2Ppi
;
104 UINT64 PciCfg2Address
;
106 PciCfg2Ppi
= InternalGetPciCfg2Ppi (Address
);
107 PciCfg2Address
= PCI_TO_PCICFG2_ADDRESS (Address
);
109 GetPeiServicesTablePointer (),
120 Internal worker function to writes a PCI configuration register.
122 This function wraps EFI_PEI_PCI_CFG2_PPI.Write() service.
123 It writes the PCI configuration register specified by Address with the
124 value specified by Data. The width of data is specifed by Width.
127 @param Address The address that encodes the PCI Bus, Device, Function and
129 @param Width The width of data to write
130 @param Data The value to write.
132 @return The value written to the PCI configuration register.
136 PeiPciSegmentLibPciCfg2WriteWorker (
138 IN EFI_PEI_PCI_CFG_PPI_WIDTH Width
,
142 CONST EFI_PEI_PCI_CFG2_PPI
*PciCfg2Ppi
;
143 UINT64 PciCfg2Address
;
145 PciCfg2Ppi
= InternalGetPciCfg2Ppi (Address
);
146 PciCfg2Address
= PCI_TO_PCICFG2_ADDRESS (Address
);
148 GetPeiServicesTablePointer (),
159 Register a PCI device so PCI configuration registers may be accessed after
160 SetVirtualAddressMap().
162 If any reserved bits in Address are set, then ASSERT().
164 @param Address Address that encodes the PCI Bus, Device, Function and
167 @retval RETURN_SUCCESS The PCI device was registered for runtime access.
168 @retval RETURN_UNSUPPORTED An attempt was made to call this function
169 after ExitBootServices().
170 @retval RETURN_UNSUPPORTED The resources required to access the PCI device
171 at runtime could not be mapped.
172 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
173 complete the registration.
178 PciSegmentRegisterForRuntimeAccess (
182 ASSERT_INVALID_PCI_SEGMENT_ADDRESS (Address
, 0);
183 return RETURN_UNSUPPORTED
;
187 Reads an 8-bit PCI configuration register.
189 Reads and returns the 8-bit PCI configuration register specified by Address.
190 This function must guarantee that all PCI read and write operations are serialized.
192 If any reserved bits in Address are set, then ASSERT().
194 @param Address Address that encodes the PCI Segment, Bus, Device, Function, and Register.
196 @return The 8-bit PCI configuration register specified by Address.
205 ASSERT_INVALID_PCI_SEGMENT_ADDRESS (Address
, 0);
207 return (UINT8
) PeiPciSegmentLibPciCfg2ReadWorker (Address
, EfiPeiPciCfgWidthUint8
);
211 Writes an 8-bit PCI configuration register.
213 Writes the 8-bit PCI configuration register specified by Address with the value specified by Value.
214 Value is returned. This function must guarantee that all PCI read and write operations are serialized.
216 If any reserved bits in Address are set, then ASSERT().
218 @param Address Address that encodes the PCI Segment, Bus, Device, Function, and Register.
219 @param Value The value to write.
221 @return The value written to the PCI configuration register.
231 ASSERT_INVALID_PCI_SEGMENT_ADDRESS (Address
, 0);
233 return (UINT8
) PeiPciSegmentLibPciCfg2WriteWorker (Address
, EfiPeiPciCfgWidthUint8
, Value
);
237 Performs a bitwise OR of an 8-bit PCI configuration register with an 8-bit value.
239 Reads the 8-bit PCI configuration register specified by Address,
240 performs a bitwise OR between the read result and the value specified by OrData,
241 and writes the result to the 8-bit PCI configuration register specified by Address.
242 The value written to the PCI configuration register is returned.
243 This function must guarantee that all PCI read and write operations are serialized.
245 If any reserved bits in Address are set, then ASSERT().
247 @param Address Address that encodes the PCI Segment, Bus, Device, Function, and Register.
248 @param OrData The value to OR with the PCI configuration register.
250 @return The value written to the PCI configuration register.
260 return PciSegmentWrite8 (Address
, (UINT8
) (PciSegmentRead8 (Address
) | OrData
));
264 Performs a bitwise AND of an 8-bit PCI configuration register with an 8-bit value.
266 Reads the 8-bit PCI configuration register specified by Address,
267 performs a bitwise AND between the read result and the value specified by AndData,
268 and writes the result to the 8-bit PCI configuration register specified by Address.
269 The value written to the PCI configuration register is returned.
270 This function must guarantee that all PCI read and write operations are serialized.
271 If any reserved bits in Address are set, then ASSERT().
273 @param Address Address that encodes the PCI Segment, Bus, Device, Function, and Register.
274 @param AndData The value to AND with the PCI configuration register.
276 @return The value written to the PCI configuration register.
286 return PciSegmentWrite8 (Address
, (UINT8
) (PciSegmentRead8 (Address
) & AndData
));
290 Performs a bitwise AND of an 8-bit PCI configuration register with an 8-bit value,
291 followed a bitwise OR with another 8-bit value.
293 Reads the 8-bit PCI configuration register specified by Address,
294 performs a bitwise AND between the read result and the value specified by AndData,
295 performs a bitwise OR between the result of the AND operation and the value specified by OrData,
296 and writes the result to the 8-bit PCI configuration register specified by Address.
297 The value written to the PCI configuration register is returned.
298 This function must guarantee that all PCI read and write operations are serialized.
300 If any reserved bits in Address are set, then ASSERT().
302 @param Address Address that encodes the PCI Segment, Bus, Device, Function, and Register.
303 @param AndData The value to AND with the PCI configuration register.
304 @param OrData The value to OR with the PCI configuration register.
306 @return The value written to the PCI configuration register.
311 PciSegmentAndThenOr8 (
317 return PciSegmentWrite8 (Address
, (UINT8
) ((PciSegmentRead8 (Address
) & AndData
) | OrData
));
321 Reads a bit field of a PCI configuration register.
323 Reads the bit field in an 8-bit PCI configuration register. The bit field is
324 specified by the StartBit and the EndBit. The value of the bit field is
327 If any reserved bits in Address are set, then ASSERT().
328 If StartBit is greater than 7, then ASSERT().
329 If EndBit is greater than 7, then ASSERT().
330 If EndBit is less than StartBit, then ASSERT().
332 @param Address PCI configuration register to read.
333 @param StartBit The ordinal of the least significant bit in the bit field.
335 @param EndBit The ordinal of the most significant bit in the bit field.
338 @return The value of the bit field read from the PCI configuration register.
343 PciSegmentBitFieldRead8 (
349 return BitFieldRead8 (PciSegmentRead8 (Address
), StartBit
, EndBit
);
353 Writes a bit field to a PCI configuration register.
355 Writes Value to the bit field of the PCI configuration register. The bit
356 field is specified by the StartBit and the EndBit. All other bits in the
357 destination PCI configuration register are preserved. The new value of the
358 8-bit register is returned.
360 If any reserved bits in Address are set, then ASSERT().
361 If StartBit is greater than 7, then ASSERT().
362 If EndBit is greater than 7, then ASSERT().
363 If EndBit is less than StartBit, then ASSERT().
364 If Value is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
366 @param Address PCI configuration register to write.
367 @param StartBit The ordinal of the least significant bit in the bit field.
369 @param EndBit The ordinal of the most significant bit in the bit field.
371 @param Value New value of the bit field.
373 @return The value written back to the PCI configuration register.
378 PciSegmentBitFieldWrite8 (
385 return PciSegmentWrite8 (
387 BitFieldWrite8 (PciSegmentRead8 (Address
), StartBit
, EndBit
, Value
)
392 Reads a bit field in an 8-bit PCI configuration, performs a bitwise OR, and
393 writes the result back to the bit field in the 8-bit port.
395 Reads the 8-bit PCI configuration register specified by Address, performs a
396 bitwise OR between the read result and the value specified by
397 OrData, and writes the result to the 8-bit PCI configuration register
398 specified by Address. The value written to the PCI configuration register is
399 returned. This function must guarantee that all PCI read and write operations
400 are serialized. Extra left bits in OrData are stripped.
402 If any reserved bits in Address are set, then ASSERT().
403 If StartBit is greater than 7, then ASSERT().
404 If EndBit is greater than 7, then ASSERT().
405 If EndBit is less than StartBit, then ASSERT().
406 If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
408 @param Address PCI configuration register to write.
409 @param StartBit The ordinal of the least significant bit in the bit field.
411 @param EndBit The ordinal of the most significant bit in the bit field.
413 @param OrData The value to OR with the PCI configuration register.
415 @return The value written back to the PCI configuration register.
420 PciSegmentBitFieldOr8 (
427 return PciSegmentWrite8 (
429 BitFieldOr8 (PciSegmentRead8 (Address
), StartBit
, EndBit
, OrData
)
434 Reads a bit field in an 8-bit PCI configuration register, performs a bitwise
435 AND, and writes the result back to the bit field in the 8-bit register.
437 Reads the 8-bit PCI configuration register specified by Address, performs a
438 bitwise AND between the read result and the value specified by AndData, and
439 writes the result to the 8-bit PCI configuration register specified by
440 Address. The value written to the PCI configuration register is returned.
441 This function must guarantee that all PCI read and write operations are
442 serialized. Extra left bits in AndData are stripped.
444 If any reserved bits in Address are set, then ASSERT().
445 If StartBit is greater than 7, then ASSERT().
446 If EndBit is greater than 7, then ASSERT().
447 If EndBit is less than StartBit, then ASSERT().
448 If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
450 @param Address PCI configuration register to write.
451 @param StartBit The ordinal of the least significant bit in the bit field.
453 @param EndBit The ordinal of the most significant bit in the bit field.
455 @param AndData The value to AND with the PCI configuration register.
457 @return The value written back to the PCI configuration register.
462 PciSegmentBitFieldAnd8 (
469 return PciSegmentWrite8 (
471 BitFieldAnd8 (PciSegmentRead8 (Address
), StartBit
, EndBit
, AndData
)
476 Reads a bit field in an 8-bit port, performs a bitwise AND followed by a
477 bitwise OR, and writes the result back to the bit field in the 8-bit port.
479 Reads the 8-bit PCI configuration register specified by Address, performs a
480 bitwise AND followed by a bitwise OR between the read result and
481 the value specified by AndData, and writes the result to the 8-bit PCI
482 configuration register specified by Address. The value written to the PCI
483 configuration register is returned. This function must guarantee that all PCI
484 read and write operations are serialized. Extra left bits in both AndData and
487 If any reserved bits in Address are set, then ASSERT().
488 If StartBit is greater than 7, then ASSERT().
489 If EndBit is greater than 7, then ASSERT().
490 If EndBit is less than StartBit, then ASSERT().
491 If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
492 If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
494 @param Address PCI configuration register to write.
495 @param StartBit The ordinal of the least significant bit in the bit field.
497 @param EndBit The ordinal of the most significant bit in the bit field.
499 @param AndData The value to AND with the PCI configuration register.
500 @param OrData The value to OR with the result of the AND operation.
502 @return The value written back to the PCI configuration register.
507 PciSegmentBitFieldAndThenOr8 (
515 return PciSegmentWrite8 (
517 BitFieldAndThenOr8 (PciSegmentRead8 (Address
), StartBit
, EndBit
, AndData
, OrData
)
522 Reads a 16-bit PCI configuration register.
524 Reads and returns the 16-bit PCI configuration register specified by Address.
525 This function must guarantee that all PCI read and write operations are serialized.
527 If any reserved bits in Address are set, then ASSERT().
528 If Address is not aligned on a 16-bit boundary, then ASSERT().
530 @param Address Address that encodes the PCI Segment, Bus, Device, Function, and Register.
532 @return The 16-bit PCI configuration register specified by Address.
541 ASSERT_INVALID_PCI_SEGMENT_ADDRESS (Address
, 1);
543 return (UINT16
) PeiPciSegmentLibPciCfg2ReadWorker (Address
, EfiPeiPciCfgWidthUint16
);
547 Writes a 16-bit PCI configuration register.
549 Writes the 16-bit PCI configuration register specified by Address with the value specified by Value.
550 Value is returned. This function must guarantee that all PCI read and write operations are serialized.
552 If any reserved bits in Address are set, then ASSERT().
553 If Address is not aligned on a 16-bit boundary, then ASSERT().
555 @param Address Address that encodes the PCI Segment, Bus, Device, Function, and Register.
556 @param Value The value to write.
558 @return The parameter of Value.
568 ASSERT_INVALID_PCI_SEGMENT_ADDRESS (Address
, 1);
570 return (UINT16
) PeiPciSegmentLibPciCfg2WriteWorker (Address
, EfiPeiPciCfgWidthUint16
, Value
);
574 Performs a bitwise OR of a 16-bit PCI configuration register with
577 Reads the 16-bit PCI configuration register specified by Address, performs a
578 bitwise OR between the read result and the value specified by OrData, and
579 writes the result to the 16-bit PCI configuration register specified by Address.
580 The value written to the PCI configuration register is returned. This function
581 must guarantee that all PCI read and write operations are serialized.
583 If any reserved bits in Address are set, then ASSERT().
584 If Address is not aligned on a 16-bit boundary, then ASSERT().
586 @param Address Address that encodes the PCI Segment, Bus, Device, Function and
588 @param OrData The value to OR with the PCI configuration register.
590 @return The value written back to the PCI configuration register.
600 return PciSegmentWrite16 (Address
, (UINT16
) (PciSegmentRead16 (Address
) | OrData
));
604 Performs a bitwise AND of a 16-bit PCI configuration register with a 16-bit value.
606 Reads the 16-bit PCI configuration register specified by Address,
607 performs a bitwise AND between the read result and the value specified by AndData,
608 and writes the result to the 16-bit PCI configuration register specified by Address.
609 The value written to the PCI configuration register is returned.
610 This function must guarantee that all PCI read and write operations are serialized.
612 If any reserved bits in Address are set, then ASSERT().
613 If Address is not aligned on a 16-bit boundary, then ASSERT().
615 @param Address Address that encodes the PCI Segment, Bus, Device, Function, and Register.
616 @param AndData The value to AND with the PCI configuration register.
618 @return The value written to the PCI configuration register.
628 return PciSegmentWrite16 (Address
, (UINT16
) (PciSegmentRead16 (Address
) & AndData
));
632 Performs a bitwise AND of a 16-bit PCI configuration register with a 16-bit value,
633 followed a bitwise OR with another 16-bit value.
635 Reads the 16-bit PCI configuration register specified by Address,
636 performs a bitwise AND between the read result and the value specified by AndData,
637 performs a bitwise OR between the result of the AND operation and the value specified by OrData,
638 and writes the result to the 16-bit PCI configuration register specified by Address.
639 The value written to the PCI configuration register is returned.
640 This function must guarantee that all PCI read and write operations are serialized.
642 If any reserved bits in Address are set, then ASSERT().
643 If Address is not aligned on a 16-bit boundary, then ASSERT().
645 @param Address Address that encodes the PCI Segment, Bus, Device, Function, and Register.
646 @param AndData The value to AND with the PCI configuration register.
647 @param OrData The value to OR with the PCI configuration register.
649 @return The value written to the PCI configuration register.
654 PciSegmentAndThenOr16 (
660 return PciSegmentWrite16 (Address
, (UINT16
) ((PciSegmentRead16 (Address
) & AndData
) | OrData
));
664 Reads a bit field of a PCI configuration register.
666 Reads the bit field in a 16-bit PCI configuration register. The bit field is
667 specified by the StartBit and the EndBit. The value of the bit field is
670 If any reserved bits in Address are set, then ASSERT().
671 If Address is not aligned on a 16-bit boundary, then ASSERT().
672 If StartBit is greater than 15, then ASSERT().
673 If EndBit is greater than 15, then ASSERT().
674 If EndBit is less than StartBit, then ASSERT().
676 @param Address PCI configuration register to read.
677 @param StartBit The ordinal of the least significant bit in the bit field.
679 @param EndBit The ordinal of the most significant bit in the bit field.
682 @return The value of the bit field read from the PCI configuration register.
687 PciSegmentBitFieldRead16 (
693 return BitFieldRead16 (PciSegmentRead16 (Address
), StartBit
, EndBit
);
697 Writes a bit field to a PCI configuration register.
699 Writes Value to the bit field of the PCI configuration register. The bit
700 field is specified by the StartBit and the EndBit. All other bits in the
701 destination PCI configuration register are preserved. The new value of the
702 16-bit register is returned.
704 If any reserved bits in Address are set, then ASSERT().
705 If Address is not aligned on a 16-bit boundary, then ASSERT().
706 If StartBit is greater than 15, then ASSERT().
707 If EndBit is greater than 15, then ASSERT().
708 If EndBit is less than StartBit, then ASSERT().
709 If Value is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
711 @param Address PCI configuration register to write.
712 @param StartBit The ordinal of the least significant bit in the bit field.
714 @param EndBit The ordinal of the most significant bit in the bit field.
716 @param Value New value of the bit field.
718 @return The value written back to the PCI configuration register.
723 PciSegmentBitFieldWrite16 (
730 return PciSegmentWrite16 (
732 BitFieldWrite16 (PciSegmentRead16 (Address
), StartBit
, EndBit
, Value
)
737 Reads a bit field in a 16-bit PCI configuration, performs a bitwise OR, writes
738 the result back to the bit field in the 16-bit port.
740 Reads the 16-bit PCI configuration register specified by Address, performs a
741 bitwise OR between the read result and the value specified by
742 OrData, and writes the result to the 16-bit PCI configuration register
743 specified by Address. The value written to the PCI configuration register is
744 returned. This function must guarantee that all PCI read and write operations
745 are serialized. Extra left bits in OrData are stripped.
747 If any reserved bits in Address are set, then ASSERT().
748 If Address is not aligned on a 16-bit boundary, then ASSERT().
749 If StartBit is greater than 15, then ASSERT().
750 If EndBit is greater than 15, then ASSERT().
751 If EndBit is less than StartBit, then ASSERT().
752 If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
754 @param Address PCI configuration register to write.
755 @param StartBit The ordinal of the least significant bit in the bit field.
757 @param EndBit The ordinal of the most significant bit in the bit field.
759 @param OrData The value to OR with the PCI configuration register.
761 @return The value written back to the PCI configuration register.
766 PciSegmentBitFieldOr16 (
773 return PciSegmentWrite16 (
775 BitFieldOr16 (PciSegmentRead16 (Address
), StartBit
, EndBit
, OrData
)
780 Reads a bit field in a 16-bit PCI configuration register, performs a bitwise
781 AND, writes the result back to the bit field in the 16-bit register.
783 Reads the 16-bit PCI configuration register specified by Address, performs a
784 bitwise AND between the read result and the value specified by AndData, and
785 writes the result to the 16-bit PCI configuration register specified by
786 Address. The value written to the PCI configuration register is returned.
787 This function must guarantee that all PCI read and write operations are
788 serialized. Extra left bits in AndData are stripped.
790 If any reserved bits in Address are set, then ASSERT().
791 If Address is not aligned on a 16-bit boundary, then ASSERT().
792 If StartBit is greater than 15, then ASSERT().
793 If EndBit is greater than 15, then ASSERT().
794 If EndBit is less than StartBit, then ASSERT().
795 If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
797 @param Address Address that encodes the PCI Segment, Bus, Device, Function, and Register.
798 @param StartBit The ordinal of the least significant bit in the bit field.
800 @param EndBit The ordinal of the most significant bit in the bit field.
802 @param AndData The value to AND with the PCI configuration register.
804 @return The value written back to the PCI configuration register.
809 PciSegmentBitFieldAnd16 (
816 return PciSegmentWrite16 (
818 BitFieldAnd16 (PciSegmentRead16 (Address
), StartBit
, EndBit
, AndData
)
823 Reads a bit field in a 16-bit port, performs a bitwise AND followed by a
824 bitwise OR, and writes the result back to the bit field in the
827 Reads the 16-bit PCI configuration register specified by Address, performs a
828 bitwise AND followed by a bitwise OR between the read result and
829 the value specified by AndData, and writes the result to the 16-bit PCI
830 configuration register specified by Address. The value written to the PCI
831 configuration register is returned. This function must guarantee that all PCI
832 read and write operations are serialized. Extra left bits in both AndData and
835 If any reserved bits in Address are set, then ASSERT().
836 If StartBit is greater than 15, then ASSERT().
837 If EndBit is greater than 15, then ASSERT().
838 If EndBit is less than StartBit, then ASSERT().
839 If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
840 If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
842 @param Address PCI configuration register to write.
843 @param StartBit The ordinal of the least significant bit in the bit field.
845 @param EndBit The ordinal of the most significant bit in the bit field.
847 @param AndData The value to AND with the PCI configuration register.
848 @param OrData The value to OR with the result of the AND operation.
850 @return The value written back to the PCI configuration register.
855 PciSegmentBitFieldAndThenOr16 (
863 return PciSegmentWrite16 (
865 BitFieldAndThenOr16 (PciSegmentRead16 (Address
), StartBit
, EndBit
, AndData
, OrData
)
870 Reads a 32-bit PCI configuration register.
872 Reads and returns the 32-bit PCI configuration register specified by Address.
873 This function must guarantee that all PCI read and write operations are serialized.
875 If any reserved bits in Address are set, then ASSERT().
876 If Address is not aligned on a 32-bit boundary, then ASSERT().
878 @param Address Address that encodes the PCI Segment, Bus, Device, Function, and Register.
880 @return The 32-bit PCI configuration register specified by Address.
889 ASSERT_INVALID_PCI_SEGMENT_ADDRESS (Address
, 3);
891 return PeiPciSegmentLibPciCfg2ReadWorker (Address
, EfiPeiPciCfgWidthUint32
);
895 Writes a 32-bit PCI configuration register.
897 Writes the 32-bit PCI configuration register specified by Address with the value specified by Value.
898 Value is returned. This function must guarantee that all PCI read and write operations are serialized.
900 If any reserved bits in Address are set, then ASSERT().
901 If Address is not aligned on a 32-bit boundary, then ASSERT().
903 @param Address Address that encodes the PCI Segment, Bus, Device, Function, and Register.
904 @param Value The value to write.
906 @return The parameter of Value.
916 ASSERT_INVALID_PCI_SEGMENT_ADDRESS (Address
, 3);
918 return PeiPciSegmentLibPciCfg2WriteWorker (Address
, EfiPeiPciCfgWidthUint32
, Value
);
922 Performs a bitwise OR of a 32-bit PCI configuration register with a 32-bit value.
924 Reads the 32-bit PCI configuration register specified by Address,
925 performs a bitwise OR between the read result and the value specified by OrData,
926 and writes the result to the 32-bit PCI configuration register specified by Address.
927 The value written to the PCI configuration register is returned.
928 This function must guarantee that all PCI read and write operations are serialized.
930 If any reserved bits in Address are set, then ASSERT().
931 If Address is not aligned on a 32-bit boundary, then ASSERT().
933 @param Address Address that encodes the PCI Segment, Bus, Device, Function, and Register.
934 @param OrData The value to OR with the PCI configuration register.
936 @return The value written to the PCI configuration register.
946 return PciSegmentWrite32 (Address
, PciSegmentRead32 (Address
) | OrData
);
950 Performs a bitwise AND of a 32-bit PCI configuration register with a 32-bit value.
952 Reads the 32-bit PCI configuration register specified by Address,
953 performs a bitwise AND between the read result and the value specified by AndData,
954 and writes the result to the 32-bit PCI configuration register specified by Address.
955 The value written to the PCI configuration register is returned.
956 This function must guarantee that all PCI read and write operations are serialized.
958 If any reserved bits in Address are set, then ASSERT().
959 If Address is not aligned on a 32-bit boundary, then ASSERT().
961 @param Address Address that encodes the PCI Segment, Bus, Device, Function, and Register.
962 @param AndData The value to AND with the PCI configuration register.
964 @return The value written to the PCI configuration register.
974 return PciSegmentWrite32 (Address
, PciSegmentRead32 (Address
) & AndData
);
978 Performs a bitwise AND of a 32-bit PCI configuration register with a 32-bit value,
979 followed a bitwise OR with another 32-bit value.
981 Reads the 32-bit PCI configuration register specified by Address,
982 performs a bitwise AND between the read result and the value specified by AndData,
983 performs a bitwise OR between the result of the AND operation and the value specified by OrData,
984 and writes the result to the 32-bit PCI configuration register specified by Address.
985 The value written to the PCI configuration register is returned.
986 This function must guarantee that all PCI read and write operations are serialized.
988 If any reserved bits in Address are set, then ASSERT().
989 If Address is not aligned on a 32-bit boundary, then ASSERT().
991 @param Address Address that encodes the PCI Segment, Bus, Device, Function, and Register.
992 @param AndData The value to AND with the PCI configuration register.
993 @param OrData The value to OR with the PCI configuration register.
995 @return The value written to the PCI configuration register.
1000 PciSegmentAndThenOr32 (
1006 return PciSegmentWrite32 (Address
, (PciSegmentRead32 (Address
) & AndData
) | OrData
);
1010 Reads a bit field of a PCI configuration register.
1012 Reads the bit field in a 32-bit PCI configuration register. The bit field is
1013 specified by the StartBit and the EndBit. The value of the bit field is
1016 If any reserved bits in Address are set, then ASSERT().
1017 If Address is not aligned on a 32-bit boundary, then ASSERT().
1018 If StartBit is greater than 31, then ASSERT().
1019 If EndBit is greater than 31, then ASSERT().
1020 If EndBit is less than StartBit, then ASSERT().
1022 @param Address PCI configuration register to read.
1023 @param StartBit The ordinal of the least significant bit in the bit field.
1025 @param EndBit The ordinal of the most significant bit in the bit field.
1028 @return The value of the bit field read from the PCI configuration register.
1033 PciSegmentBitFieldRead32 (
1039 return BitFieldRead32 (PciSegmentRead32 (Address
), StartBit
, EndBit
);
1043 Writes a bit field to a PCI configuration register.
1045 Writes Value to the bit field of the PCI configuration register. The bit
1046 field is specified by the StartBit and the EndBit. All other bits in the
1047 destination PCI configuration register are preserved. The new value of the
1048 32-bit register is returned.
1050 If any reserved bits in Address are set, then ASSERT().
1051 If Address is not aligned on a 32-bit boundary, then ASSERT().
1052 If StartBit is greater than 31, then ASSERT().
1053 If EndBit is greater than 31, then ASSERT().
1054 If EndBit is less than StartBit, then ASSERT().
1055 If Value is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
1057 @param Address PCI configuration register to write.
1058 @param StartBit The ordinal of the least significant bit in the bit field.
1060 @param EndBit The ordinal of the most significant bit in the bit field.
1062 @param Value New value of the bit field.
1064 @return The value written back to the PCI configuration register.
1069 PciSegmentBitFieldWrite32 (
1076 return PciSegmentWrite32 (
1078 BitFieldWrite32 (PciSegmentRead32 (Address
), StartBit
, EndBit
, Value
)
1083 Reads a bit field in a 32-bit PCI configuration, performs a bitwise OR, and
1084 writes the result back to the bit field in the 32-bit port.
1086 Reads the 32-bit PCI configuration register specified by Address, performs a
1087 bitwise OR between the read result and the value specified by
1088 OrData, and writes the result to the 32-bit PCI configuration register
1089 specified by Address. The value written to the PCI configuration register is
1090 returned. This function must guarantee that all PCI read and write operations
1091 are serialized. Extra left bits in OrData are stripped.
1093 If any reserved bits in Address are set, then ASSERT().
1094 If StartBit is greater than 31, then ASSERT().
1095 If EndBit is greater than 31, then ASSERT().
1096 If EndBit is less than StartBit, then ASSERT().
1097 If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
1099 @param Address PCI configuration register to write.
1100 @param StartBit The ordinal of the least significant bit in the bit field.
1102 @param EndBit The ordinal of the most significant bit in the bit field.
1104 @param OrData The value to OR with the PCI configuration register.
1106 @return The value written back to the PCI configuration register.
1111 PciSegmentBitFieldOr32 (
1118 return PciSegmentWrite32 (
1120 BitFieldOr32 (PciSegmentRead32 (Address
), StartBit
, EndBit
, OrData
)
1125 Reads a bit field in a 32-bit PCI configuration register, performs a bitwise
1126 AND, and writes the result back to the bit field in the 32-bit register.
1129 Reads the 32-bit PCI configuration register specified by Address, performs a bitwise
1130 AND between the read result and the value specified by AndData, and writes the result
1131 to the 32-bit PCI configuration register specified by Address. The value written to
1132 the PCI configuration register is returned. This function must guarantee that all PCI
1133 read and write operations are serialized. Extra left bits in AndData are stripped.
1134 If any reserved bits in Address are set, then ASSERT().
1135 If Address is not aligned on a 32-bit boundary, then ASSERT().
1136 If StartBit is greater than 31, then ASSERT().
1137 If EndBit is greater than 31, then ASSERT().
1138 If EndBit is less than StartBit, then ASSERT().
1139 If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
1141 @param Address Address that encodes the PCI Segment, Bus, Device, Function, and Register.
1142 @param StartBit The ordinal of the least significant bit in the bit field.
1144 @param EndBit The ordinal of the most significant bit in the bit field.
1146 @param AndData The value to AND with the PCI configuration register.
1148 @return The value written back to the PCI configuration register.
1153 PciSegmentBitFieldAnd32 (
1160 return PciSegmentWrite32 (
1162 BitFieldAnd32 (PciSegmentRead32 (Address
), StartBit
, EndBit
, AndData
)
1167 Reads a bit field in a 32-bit port, performs a bitwise AND followed by a
1168 bitwise OR, and writes the result back to the bit field in the
1171 Reads the 32-bit PCI configuration register specified by Address, performs a
1172 bitwise AND followed by a bitwise OR between the read result and
1173 the value specified by AndData, and writes the result to the 32-bit PCI
1174 configuration register specified by Address. The value written to the PCI
1175 configuration register is returned. This function must guarantee that all PCI
1176 read and write operations are serialized. Extra left bits in both AndData and
1177 OrData are stripped.
1179 If any reserved bits in Address are set, then ASSERT().
1180 If StartBit is greater than 31, then ASSERT().
1181 If EndBit is greater than 31, then ASSERT().
1182 If EndBit is less than StartBit, then ASSERT().
1183 If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
1184 If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
1186 @param Address PCI configuration register to write.
1187 @param StartBit The ordinal of the least significant bit in the bit field.
1189 @param EndBit The ordinal of the most significant bit in the bit field.
1191 @param AndData The value to AND with the PCI configuration register.
1192 @param OrData The value to OR with the result of the AND operation.
1194 @return The value written back to the PCI configuration register.
1199 PciSegmentBitFieldAndThenOr32 (
1207 return PciSegmentWrite32 (
1209 BitFieldAndThenOr32 (PciSegmentRead32 (Address
), StartBit
, EndBit
, AndData
, OrData
)
1214 Reads a range of PCI configuration registers into a caller supplied buffer.
1216 Reads the range of PCI configuration registers specified by StartAddress and
1217 Size into the buffer specified by Buffer. This function only allows the PCI
1218 configuration registers from a single PCI function to be read. Size is
1219 returned. When possible 32-bit PCI configuration read cycles are used to read
1220 from StartAdress to StartAddress + Size. Due to alignment restrictions, 8-bit
1221 and 16-bit PCI configuration read cycles may be used at the beginning and the
1224 If any reserved bits in StartAddress are set, then ASSERT().
1225 If ((StartAddress & 0xFFF) + Size) > 0x1000, then ASSERT().
1226 If Size > 0 and Buffer is NULL, then ASSERT().
1228 @param StartAddress Starting address that encodes the PCI Segment, Bus, Device,
1229 Function and Register.
1230 @param Size Size in bytes of the transfer.
1231 @param Buffer Pointer to a buffer receiving the data read.
1238 PciSegmentReadBuffer (
1239 IN UINT64 StartAddress
,
1246 ASSERT_INVALID_PCI_SEGMENT_ADDRESS (StartAddress
, 0);
1247 ASSERT (((StartAddress
& 0xFFF) + Size
) <= 0x1000);
1253 ASSERT (Buffer
!= NULL
);
1256 // Save Size for return
1260 if ((StartAddress
& BIT0
) != 0) {
1262 // Read a byte if StartAddress is byte aligned
1264 *(volatile UINT8
*)Buffer
= PciSegmentRead8 (StartAddress
);
1265 StartAddress
+= sizeof (UINT8
);
1266 Size
-= sizeof (UINT8
);
1267 Buffer
= (UINT8
*)Buffer
+ 1;
1270 if (Size
>= sizeof (UINT16
) && (StartAddress
& BIT1
) != 0) {
1272 // Read a word if StartAddress is word aligned
1274 WriteUnaligned16 (Buffer
, PciSegmentRead16 (StartAddress
));
1275 StartAddress
+= sizeof (UINT16
);
1276 Size
-= sizeof (UINT16
);
1277 Buffer
= (UINT16
*)Buffer
+ 1;
1280 while (Size
>= sizeof (UINT32
)) {
1282 // Read as many double words as possible
1284 WriteUnaligned32 (Buffer
, PciSegmentRead32 (StartAddress
));
1285 StartAddress
+= sizeof (UINT32
);
1286 Size
-= sizeof (UINT32
);
1287 Buffer
= (UINT32
*)Buffer
+ 1;
1290 if (Size
>= sizeof (UINT16
)) {
1292 // Read the last remaining word if exist
1294 WriteUnaligned16 (Buffer
, PciSegmentRead16 (StartAddress
));
1295 StartAddress
+= sizeof (UINT16
);
1296 Size
-= sizeof (UINT16
);
1297 Buffer
= (UINT16
*)Buffer
+ 1;
1300 if (Size
>= sizeof (UINT8
)) {
1302 // Read the last remaining byte if exist
1304 *(volatile UINT8
*)Buffer
= PciSegmentRead8 (StartAddress
);
1312 Copies the data in a caller supplied buffer to a specified range of PCI
1313 configuration space.
1315 Writes the range of PCI configuration registers specified by StartAddress and
1316 Size from the buffer specified by Buffer. This function only allows the PCI
1317 configuration registers from a single PCI function to be written. Size is
1318 returned. When possible 32-bit PCI configuration write cycles are used to
1319 write from StartAdress to StartAddress + Size. Due to alignment restrictions,
1320 8-bit and 16-bit PCI configuration write cycles may be used at the beginning
1321 and the end of the range.
1323 If any reserved bits in StartAddress are set, then ASSERT().
1324 If ((StartAddress & 0xFFF) + Size) > 0x1000, then ASSERT().
1325 If Size > 0 and Buffer is NULL, then ASSERT().
1327 @param StartAddress Starting address that encodes the PCI Segment, Bus, Device,
1328 Function and Register.
1329 @param Size Size in bytes of the transfer.
1330 @param Buffer Pointer to a buffer containing the data to write.
1332 @return The parameter of Size.
1337 PciSegmentWriteBuffer (
1338 IN UINT64 StartAddress
,
1345 ASSERT_INVALID_PCI_SEGMENT_ADDRESS (StartAddress
, 0);
1346 ASSERT (((StartAddress
& 0xFFF) + Size
) <= 0x1000);
1352 ASSERT (Buffer
!= NULL
);
1355 // Save Size for return
1359 if ((StartAddress
& BIT0
) != 0) {
1361 // Write a byte if StartAddress is byte aligned
1363 PciSegmentWrite8 (StartAddress
, *(UINT8
*)Buffer
);
1364 StartAddress
+= sizeof (UINT8
);
1365 Size
-= sizeof (UINT8
);
1366 Buffer
= (UINT8
*)Buffer
+ 1;
1369 if (Size
>= sizeof (UINT16
) && (StartAddress
& BIT1
) != 0) {
1371 // Write a word if StartAddress is word aligned
1373 PciSegmentWrite16 (StartAddress
, ReadUnaligned16 (Buffer
));
1374 StartAddress
+= sizeof (UINT16
);
1375 Size
-= sizeof (UINT16
);
1376 Buffer
= (UINT16
*)Buffer
+ 1;
1379 while (Size
>= sizeof (UINT32
)) {
1381 // Write as many double words as possible
1383 PciSegmentWrite32 (StartAddress
, ReadUnaligned32 (Buffer
));
1384 StartAddress
+= sizeof (UINT32
);
1385 Size
-= sizeof (UINT32
);
1386 Buffer
= (UINT32
*)Buffer
+ 1;
1389 if (Size
>= sizeof (UINT16
)) {
1391 // Write the last remaining word if exist
1393 PciSegmentWrite16 (StartAddress
, ReadUnaligned16 (Buffer
));
1394 StartAddress
+= sizeof (UINT16
);
1395 Size
-= sizeof (UINT16
);
1396 Buffer
= (UINT16
*)Buffer
+ 1;
1399 if (Size
>= sizeof (UINT8
)) {
1401 // Write the last remaining byte if exist
1403 PciSegmentWrite8 (StartAddress
, *(UINT8
*)Buffer
);