2 Functions in this library instance make use of MMIO functions in IoLib to
3 access memory mapped PCI configuration space.
5 All assertions for I/O operations are handled in MMIO functions in the IoLib
8 Copyright (c) 2006 - 2009, Intel Corporation<BR>
9 All rights reserved. This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
22 #include <Guid/EventGroup.h>
24 #include <Library/BaseLib.h>
25 #include <Library/PciExpressLib.h>
26 #include <Library/IoLib.h>
27 #include <Library/DebugLib.h>
28 #include <Library/PcdLib.h>
29 #include <Library/MemoryAllocationLib.h>
30 #include <Library/UefiBootServicesTableLib.h>
31 #include <Library/DxeServicesTableLib.h>
32 #include <Library/UefiRuntimeLib.h>
35 /// Define table for mapping PCI Express MMIO physical addresses to virtual addresses at OS runtime
38 UINTN PhysicalAddress
;
40 } PCI_EXPRESS_RUNTIME_REGISTRATION_TABLE
;
43 /// Set Virtual Address Map Event
45 EFI_EVENT mDxeRuntimePciExpressLibVirtualNotifyEvent
= NULL
;
48 /// Module global that contains the base physical address of the PCI Express MMIO range
50 UINTN mDxeRuntimePciExpressLibPciExpressBaseAddress
= 0;
53 /// The number of PCI devices that have been registered for runtime access
55 UINTN mDxeRuntimePciExpressLibNumberOfRuntimeRanges
= 0;
58 /// The table of PCI devices that have been registered for runtime access
60 PCI_EXPRESS_RUNTIME_REGISTRATION_TABLE
*mDxeRuntimePciExpressLibRegistrationTable
= NULL
;
63 /// The table index of the most recent virtual address lookup
65 UINTN mDxeRuntimePciExpressLibLastRuntimeRange
= 0;
69 Convert the physical PCI Express MMIO addresses for all registered PCI devices
72 @param[in] Event The Event that is being processed
73 @param[in] Context Event Context
77 DxeRuntimePciExpressLibVirtualNotify (
85 // If there have been no runtime registrations, then just return
87 if (mDxeRuntimePciExpressLibRegistrationTable
== NULL
) {
92 // Convert physical addresses associated with the set of registered PCI devices to
95 for (Index
= 0; Index
< mDxeRuntimePciExpressLibNumberOfRuntimeRanges
; Index
++) {
96 EfiConvertPointer (0, (VOID
**) &(mDxeRuntimePciExpressLibRegistrationTable
[Index
].VirtualAddress
));
100 // Convert table pointer that is allocated from EfiRuntimeServicesData to a virtual address.
102 EfiConvertPointer (0, (VOID
**) &mDxeRuntimePciExpressLibRegistrationTable
);
106 The constructor function caches the PCI Express Base Address and creates a
107 Set Virtual Address Map event to convert physical address to virtual addresses.
109 @param ImageHandle The firmware allocated handle for the EFI image.
110 @param SystemTable A pointer to the EFI System Table.
112 @retval EFI_SUCCESS The constructor completed successfully.
113 @retval Other value The constructor did not complete successfully.
118 DxeRuntimePciExpressLibConstructor (
119 IN EFI_HANDLE ImageHandle
,
120 IN EFI_SYSTEM_TABLE
*SystemTable
126 // Cache the physical address of the PCI Express MMIO range into a module global variable
128 mDxeRuntimePciExpressLibPciExpressBaseAddress
= (UINTN
) PcdGet64 (PcdPciExpressBaseAddress
);
131 // Register SetVirtualAddressMap () notify function
133 Status
= gBS
->CreateEventEx (
136 DxeRuntimePciExpressLibVirtualNotify
,
138 &gEfiEventVirtualAddressChangeGuid
,
139 &mDxeRuntimePciExpressLibVirtualNotifyEvent
141 ASSERT_EFI_ERROR (Status
);
147 The destructor function frees any allocated buffers and closes the Set Virtual
150 @param ImageHandle The firmware allocated handle for the EFI image.
151 @param SystemTable A pointer to the EFI System Table.
153 @retval EFI_SUCCESS The destructor completed successfully.
154 @retval Other value The destructor did not complete successfully.
159 DxeRuntimePciExpressLibDestructor (
160 IN EFI_HANDLE ImageHandle
,
161 IN EFI_SYSTEM_TABLE
*SystemTable
167 // If one or more PCI devices have been registered for runtime access, then
168 // free the registration table.
170 if (mDxeRuntimePciExpressLibRegistrationTable
!= NULL
) {
171 FreePool (mDxeRuntimePciExpressLibRegistrationTable
);
175 // Close the Set Virtual Address Map event
177 Status
= gBS
->CloseEvent (mDxeRuntimePciExpressLibVirtualNotifyEvent
);
178 ASSERT_EFI_ERROR (Status
);
184 Gets the base address of PCI Express.
186 This internal functions retrieves PCI Express Base Address via a PCD entry
187 PcdPciExpressBaseAddress.
189 @param Address Address that encodes the PCI Bus, Device, Function and Register.
190 @return The base address of PCI Express.
194 GetPciExpressAddress (
201 // Make sure Address is valid
203 ASSERT (((Address
) & ~0xfffffff) == 0);
206 // Convert Address to a physical address in the MMIO PCI Express range
208 Address
+= mDxeRuntimePciExpressLibPciExpressBaseAddress
;
211 // If SetVirtualAddressMap() has not been called, then just return the physical address
213 if (!EfiGoneVirtual ()) {
218 // See if there is a physical address match at the exact same index as the last address match
220 if (mDxeRuntimePciExpressLibRegistrationTable
[mDxeRuntimePciExpressLibLastRuntimeRange
].PhysicalAddress
== (Address
& (~0x00000fff))) {
222 // Convert the physical address to a virtual address and return the virtual address
224 return (Address
& 0x00000fff) + mDxeRuntimePciExpressLibRegistrationTable
[mDxeRuntimePciExpressLibLastRuntimeRange
].VirtualAddress
;
228 // Search the entire table for a physical address match
230 for (Index
= 0; Index
< mDxeRuntimePciExpressLibNumberOfRuntimeRanges
; Index
++) {
231 if (mDxeRuntimePciExpressLibRegistrationTable
[Index
].PhysicalAddress
== (Address
& (~0x00000fff))) {
233 // Cache the matching index value
235 mDxeRuntimePciExpressLibLastRuntimeRange
= Index
;
237 // Convert the physical address to a virtual address and return the virtual address
239 return (Address
& 0x00000fff) + mDxeRuntimePciExpressLibRegistrationTable
[Index
].VirtualAddress
;
244 // No match was found. This is a critical error at OS runtime, so ASSERT() and force a breakpoint.
250 // Return the physical address
256 Registers a PCI device so PCI configuration registers may be accessed after
257 SetVirtualAddressMap().
259 Registers the PCI device specified by Address so all the PCI configuration
260 registers associated with that PCI device may be accessed after SetVirtualAddressMap()
263 If Address > 0x0FFFFFFF, then ASSERT().
265 @param Address Address that encodes the PCI Bus, Device, Function and
268 @retval RETURN_SUCCESS The PCI device was registered for runtime access.
269 @retval RETURN_UNSUPPORTED An attempt was made to call this function
270 after ExitBootServices().
271 @retval RETURN_UNSUPPORTED The resources required to access the PCI device
272 at runtime could not be mapped.
273 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
274 complete the registration.
279 PciExpressRegisterForRuntimeAccess (
284 EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor
;
289 // Return an error if this function is called after ExitBootServices().
291 if (EfiAtRuntime ()) {
292 return RETURN_UNSUPPORTED
;
296 // Make sure Address is valid
298 ASSERT (((Address
) & ~0xfffffff) == 0);
301 // Convert Address to a physical address in the MMIO PCI Express range
302 // at the beginning of the PCI Configuration header for the specified
305 Address
= GetPciExpressAddress (Address
& 0x0ffff000);
308 // See if Address has already been registerd for runtime access
310 for (Index
= 0; Index
< mDxeRuntimePciExpressLibNumberOfRuntimeRanges
; Index
++) {
311 if (mDxeRuntimePciExpressLibRegistrationTable
[Index
].PhysicalAddress
== Address
) {
312 return RETURN_SUCCESS
;
317 // Get the GCD Memory Descriptor for the PCI Express Bus/Dev/Func specified by Address
319 Status
= gDS
->GetMemorySpaceDescriptor (Address
, &Descriptor
);
320 if (EFI_ERROR (Status
)) {
321 return RETURN_UNSUPPORTED
;
325 // Mark the 4KB region for the PCI Express Bus/Dev/Func as EFI_RUNTIME_MEMORY so the OS
326 // will allocate a virtual address range for the 4KB PCI Configuration Header.
328 Status
= gDS
->SetMemorySpaceAttributes (Address
, 0x1000, Descriptor
.Attributes
| EFI_MEMORY_RUNTIME
);
329 if (EFI_ERROR (Status
)) {
330 return RETURN_UNSUPPORTED
;
334 // Grow the size of the registration table
336 NewTable
= ReallocateRuntimePool (
337 (mDxeRuntimePciExpressLibNumberOfRuntimeRanges
+ 0) * sizeof (PCI_EXPRESS_RUNTIME_REGISTRATION_TABLE
),
338 (mDxeRuntimePciExpressLibNumberOfRuntimeRanges
+ 1) * sizeof (PCI_EXPRESS_RUNTIME_REGISTRATION_TABLE
),
339 mDxeRuntimePciExpressLibRegistrationTable
341 if (NewTable
== NULL
) {
342 return RETURN_OUT_OF_RESOURCES
;
344 mDxeRuntimePciExpressLibRegistrationTable
= NewTable
;
345 mDxeRuntimePciExpressLibRegistrationTable
[mDxeRuntimePciExpressLibNumberOfRuntimeRanges
].PhysicalAddress
= Address
;
346 mDxeRuntimePciExpressLibRegistrationTable
[mDxeRuntimePciExpressLibNumberOfRuntimeRanges
].VirtualAddress
= Address
;
347 mDxeRuntimePciExpressLibNumberOfRuntimeRanges
++;
349 return RETURN_SUCCESS
;
354 Reads an 8-bit PCI configuration register.
356 Reads and returns the 8-bit PCI configuration register specified by Address.
357 This function must guarantee that all PCI read and write operations are
360 If Address > 0x0FFFFFFF, then ASSERT().
362 @param Address Address that encodes the PCI Bus, Device, Function and
365 @return The read value from the PCI configuration register.
374 return MmioRead8 (GetPciExpressAddress (Address
));
378 Writes an 8-bit PCI configuration register.
380 Writes the 8-bit PCI configuration register specified by Address with the
381 value specified by Value. Value is returned. This function must guarantee
382 that all PCI read and write operations are serialized.
384 If Address > 0x0FFFFFFF, then ASSERT().
386 @param Address Address that encodes the PCI Bus, Device, Function and
388 @param Value The value to write.
390 @return The value written to the PCI configuration register.
400 return MmioWrite8 (GetPciExpressAddress (Address
), Value
);
404 Performs a bitwise OR of an 8-bit PCI configuration register with
407 Reads the 8-bit PCI configuration register specified by Address, performs a
408 bitwise OR between the read result and the value specified by
409 OrData, and writes the result to the 8-bit PCI configuration register
410 specified by Address. The value written to the PCI configuration register is
411 returned. This function must guarantee that all PCI read and write operations
414 If Address > 0x0FFFFFFF, then ASSERT().
416 @param Address Address that encodes the PCI Bus, Device, Function and
418 @param OrData The value to OR with the PCI configuration register.
420 @return The value written back to the PCI configuration register.
430 return MmioOr8 (GetPciExpressAddress (Address
), OrData
);
434 Performs a bitwise AND of an 8-bit PCI configuration register with an 8-bit
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
444 If Address > 0x0FFFFFFF, then ASSERT().
446 @param Address Address that encodes the PCI Bus, Device, Function and
448 @param AndData The value to AND with the PCI configuration register.
450 @return The value written back to the PCI configuration register.
460 return MmioAnd8 (GetPciExpressAddress (Address
), AndData
);
464 Performs a bitwise AND of an 8-bit PCI configuration register with an 8-bit
465 value, followed a bitwise OR with another 8-bit value.
467 Reads the 8-bit PCI configuration register specified by Address, performs a
468 bitwise AND between the read result and the value specified by AndData,
469 performs a bitwise OR between the result of the AND operation and
470 the value specified by OrData, and writes the result to the 8-bit PCI
471 configuration register specified by Address. The value written to the PCI
472 configuration register is returned. This function must guarantee that all PCI
473 read and write operations are serialized.
475 If Address > 0x0FFFFFFF, then ASSERT().
477 @param Address Address that encodes the PCI Bus, Device, Function and
479 @param AndData The value to AND with the PCI configuration register.
480 @param OrData The value to OR with the result of the AND operation.
482 @return The value written back to the PCI configuration register.
487 PciExpressAndThenOr8 (
493 return MmioAndThenOr8 (
494 GetPciExpressAddress (Address
),
501 Reads a bit field of a PCI configuration register.
503 Reads the bit field in an 8-bit PCI configuration register. The bit field is
504 specified by the StartBit and the EndBit. The value of the bit field is
507 If Address > 0x0FFFFFFF, then ASSERT().
508 If StartBit is greater than 7, then ASSERT().
509 If EndBit is greater than 7, then ASSERT().
510 If EndBit is less than StartBit, then ASSERT().
512 @param Address PCI configuration register to read.
513 @param StartBit The ordinal of the least significant bit in the bit field.
515 @param EndBit The ordinal of the most significant bit in the bit field.
518 @return The value of the bit field read from the PCI configuration register.
523 PciExpressBitFieldRead8 (
529 return MmioBitFieldRead8 (
530 GetPciExpressAddress (Address
),
537 Writes a bit field to a PCI configuration register.
539 Writes Value to the bit field of the PCI configuration register. The bit
540 field is specified by the StartBit and the EndBit. All other bits in the
541 destination PCI configuration register are preserved. The new value of the
542 8-bit register is returned.
544 If Address > 0x0FFFFFFF, then ASSERT().
545 If StartBit is greater than 7, then ASSERT().
546 If EndBit is greater than 7, then ASSERT().
547 If EndBit is less than StartBit, then ASSERT().
549 @param Address PCI configuration register to write.
550 @param StartBit The ordinal of the least significant bit in the bit field.
552 @param EndBit The ordinal of the most significant bit in the bit field.
554 @param Value New value of the bit field.
556 @return The value written back to the PCI configuration register.
561 PciExpressBitFieldWrite8 (
568 return MmioBitFieldWrite8 (
569 GetPciExpressAddress (Address
),
577 Reads a bit field in an 8-bit PCI configuration, performs a bitwise OR, and
578 writes the result back to the bit field in the 8-bit port.
580 Reads the 8-bit PCI configuration register specified by Address, performs a
581 bitwise OR between the read result and the value specified by
582 OrData, and writes the result to the 8-bit PCI configuration register
583 specified by Address. The value written to the PCI configuration register is
584 returned. This function must guarantee that all PCI read and write operations
585 are serialized. Extra left bits in OrData are stripped.
587 If Address > 0x0FFFFFFF, then ASSERT().
588 If StartBit is greater than 7, then ASSERT().
589 If EndBit is greater than 7, then ASSERT().
590 If EndBit is less than StartBit, then ASSERT().
592 @param Address PCI configuration register to write.
593 @param StartBit The ordinal of the least significant bit in the bit field.
595 @param EndBit The ordinal of the most significant bit in the bit field.
597 @param OrData The value to OR with the PCI configuration register.
599 @return The value written back to the PCI configuration register.
604 PciExpressBitFieldOr8 (
611 return MmioBitFieldOr8 (
612 GetPciExpressAddress (Address
),
620 Reads a bit field in an 8-bit PCI configuration register, performs a bitwise
621 AND, and writes the result back to the bit field in the 8-bit register.
623 Reads the 8-bit PCI configuration register specified by Address, performs a
624 bitwise AND between the read result and the value specified by AndData, and
625 writes the result to the 8-bit PCI configuration register specified by
626 Address. The value written to the PCI configuration register is returned.
627 This function must guarantee that all PCI read and write operations are
628 serialized. Extra left bits in AndData are stripped.
630 If Address > 0x0FFFFFFF, then ASSERT().
631 If StartBit is greater than 7, then ASSERT().
632 If EndBit is greater than 7, then ASSERT().
633 If EndBit is less than StartBit, then ASSERT().
635 @param Address PCI configuration register to write.
636 @param StartBit The ordinal of the least significant bit in the bit field.
638 @param EndBit The ordinal of the most significant bit in the bit field.
640 @param AndData The value to AND with the PCI configuration register.
642 @return The value written back to the PCI configuration register.
647 PciExpressBitFieldAnd8 (
654 return MmioBitFieldAnd8 (
655 GetPciExpressAddress (Address
),
663 Reads a bit field in an 8-bit port, performs a bitwise AND followed by a
664 bitwise OR, and writes the result back to the bit field in the
667 Reads the 8-bit PCI configuration register specified by Address, performs a
668 bitwise AND followed by a bitwise OR between the read result and
669 the value specified by AndData, and writes the result to the 8-bit PCI
670 configuration register specified by Address. The value written to the PCI
671 configuration register is returned. This function must guarantee that all PCI
672 read and write operations are serialized. Extra left bits in both AndData and
675 If Address > 0x0FFFFFFF, then ASSERT().
676 If StartBit is greater than 7, then ASSERT().
677 If EndBit is greater than 7, then ASSERT().
678 If EndBit is less than StartBit, then ASSERT().
680 @param Address PCI configuration register to write.
681 @param StartBit The ordinal of the least significant bit in the bit field.
683 @param EndBit The ordinal of the most significant bit in the bit field.
685 @param AndData The value to AND with the PCI configuration register.
686 @param OrData The value to OR with the result of the AND operation.
688 @return The value written back to the PCI configuration register.
693 PciExpressBitFieldAndThenOr8 (
701 return MmioBitFieldAndThenOr8 (
702 GetPciExpressAddress (Address
),
711 Reads a 16-bit PCI configuration register.
713 Reads and returns the 16-bit PCI configuration register specified by Address.
714 This function must guarantee that all PCI read and write operations are
717 If Address > 0x0FFFFFFF, then ASSERT().
718 If Address is not aligned on a 16-bit boundary, then ASSERT().
720 @param Address Address that encodes the PCI Bus, Device, Function and
723 @return The read value from the PCI configuration register.
732 return MmioRead16 (GetPciExpressAddress (Address
));
736 Writes a 16-bit PCI configuration register.
738 Writes the 16-bit PCI configuration register specified by Address with the
739 value specified by Value. Value is returned. This function must guarantee
740 that all PCI read and write operations are serialized.
742 If Address > 0x0FFFFFFF, then ASSERT().
743 If Address is not aligned on a 16-bit boundary, then ASSERT().
745 @param Address Address that encodes the PCI Bus, Device, Function and
747 @param Value The value to write.
749 @return The value written to the PCI configuration register.
759 return MmioWrite16 (GetPciExpressAddress (Address
), Value
);
763 Performs a bitwise OR of a 16-bit PCI configuration register with
766 Reads the 16-bit PCI configuration register specified by Address, performs a
767 bitwise OR between the read result and the value specified by
768 OrData, and writes the result to the 16-bit PCI configuration register
769 specified by Address. The value written to the PCI configuration register is
770 returned. This function must guarantee that all PCI read and write operations
773 If Address > 0x0FFFFFFF, then ASSERT().
774 If Address is not aligned on a 16-bit boundary, then ASSERT().
776 @param Address Address that encodes the PCI Bus, Device, Function and
778 @param OrData The value to OR with the PCI configuration register.
780 @return The value written back to the PCI configuration register.
790 return MmioOr16 (GetPciExpressAddress (Address
), OrData
);
794 Performs a bitwise AND of a 16-bit PCI configuration register with a 16-bit
797 Reads the 16-bit PCI configuration register specified by Address, performs a
798 bitwise AND between the read result and the value specified by AndData, and
799 writes the result to the 16-bit PCI configuration register specified by
800 Address. The value written to the PCI configuration register is returned.
801 This function must guarantee that all PCI read and write operations are
804 If Address > 0x0FFFFFFF, then ASSERT().
805 If Address is not aligned on a 16-bit boundary, then ASSERT().
807 @param Address Address that encodes the PCI Bus, Device, Function and
809 @param AndData The value to AND with the PCI configuration register.
811 @return The value written back to the PCI configuration register.
821 return MmioAnd16 (GetPciExpressAddress (Address
), AndData
);
825 Performs a bitwise AND of a 16-bit PCI configuration register with a 16-bit
826 value, followed a bitwise OR with another 16-bit value.
828 Reads the 16-bit PCI configuration register specified by Address, performs a
829 bitwise AND between the read result and the value specified by AndData,
830 performs a bitwise OR between the result of the AND operation and
831 the value specified by OrData, and writes the result to the 16-bit PCI
832 configuration register specified by Address. The value written to the PCI
833 configuration register is returned. This function must guarantee that all PCI
834 read and write operations are serialized.
836 If Address > 0x0FFFFFFF, then ASSERT().
837 If Address is not aligned on a 16-bit boundary, then ASSERT().
839 @param Address Address that encodes the PCI Bus, Device, Function and
841 @param AndData The value to AND with the PCI configuration register.
842 @param OrData The value to OR with the result of the AND operation.
844 @return The value written back to the PCI configuration register.
849 PciExpressAndThenOr16 (
855 return MmioAndThenOr16 (
856 GetPciExpressAddress (Address
),
863 Reads a bit field of a PCI configuration register.
865 Reads the bit field in a 16-bit PCI configuration register. The bit field is
866 specified by the StartBit and the EndBit. The value of the bit field is
869 If Address > 0x0FFFFFFF, then ASSERT().
870 If Address is not aligned on a 16-bit boundary, then ASSERT().
871 If StartBit is greater than 15, then ASSERT().
872 If EndBit is greater than 15, then ASSERT().
873 If EndBit is less than StartBit, then ASSERT().
875 @param Address PCI configuration register to read.
876 @param StartBit The ordinal of the least significant bit in the bit field.
878 @param EndBit The ordinal of the most significant bit in the bit field.
881 @return The value of the bit field read from the PCI configuration register.
886 PciExpressBitFieldRead16 (
892 return MmioBitFieldRead16 (
893 GetPciExpressAddress (Address
),
900 Writes a bit field to a PCI configuration register.
902 Writes Value to the bit field of the PCI configuration register. The bit
903 field is specified by the StartBit and the EndBit. All other bits in the
904 destination PCI configuration register are preserved. The new value of the
905 16-bit register is returned.
907 If Address > 0x0FFFFFFF, then ASSERT().
908 If Address is not aligned on a 16-bit boundary, then ASSERT().
909 If StartBit is greater than 15, then ASSERT().
910 If EndBit is greater than 15, then ASSERT().
911 If EndBit is less than StartBit, then ASSERT().
913 @param Address PCI configuration register to write.
914 @param StartBit The ordinal of the least significant bit in the bit field.
916 @param EndBit The ordinal of the most significant bit in the bit field.
918 @param Value New value of the bit field.
920 @return The value written back to the PCI configuration register.
925 PciExpressBitFieldWrite16 (
932 return MmioBitFieldWrite16 (
933 GetPciExpressAddress (Address
),
941 Reads a bit field in a 16-bit PCI configuration, performs a bitwise OR, and
942 writes the result back to the bit field in the 16-bit port.
944 Reads the 16-bit PCI configuration register specified by Address, performs a
945 bitwise OR between the read result and the value specified by
946 OrData, and writes the result to the 16-bit PCI configuration register
947 specified by Address. The value written to the PCI configuration register is
948 returned. This function must guarantee that all PCI read and write operations
949 are serialized. Extra left bits in OrData are stripped.
951 If Address > 0x0FFFFFFF, then ASSERT().
952 If Address is not aligned on a 16-bit boundary, then ASSERT().
953 If StartBit is greater than 15, then ASSERT().
954 If EndBit is greater than 15, then ASSERT().
955 If EndBit is less than StartBit, then ASSERT().
957 @param Address PCI configuration register to write.
958 @param StartBit The ordinal of the least significant bit in the bit field.
960 @param EndBit The ordinal of the most significant bit in the bit field.
962 @param OrData The value to OR with the PCI configuration register.
964 @return The value written back to the PCI configuration register.
969 PciExpressBitFieldOr16 (
976 return MmioBitFieldOr16 (
977 GetPciExpressAddress (Address
),
985 Reads a bit field in a 16-bit PCI configuration register, performs a bitwise
986 AND, and writes the result back to the bit field in the 16-bit register.
988 Reads the 16-bit PCI configuration register specified by Address, performs a
989 bitwise AND between the read result and the value specified by AndData, and
990 writes the result to the 16-bit PCI configuration register specified by
991 Address. The value written to the PCI configuration register is returned.
992 This function must guarantee that all PCI read and write operations are
993 serialized. Extra left bits in AndData are stripped.
995 If Address > 0x0FFFFFFF, then ASSERT().
996 If Address is not aligned on a 16-bit boundary, then ASSERT().
997 If StartBit is greater than 15, then ASSERT().
998 If EndBit is greater than 15, then ASSERT().
999 If EndBit is less than StartBit, then ASSERT().
1001 @param Address PCI configuration register to write.
1002 @param StartBit The ordinal of the least significant bit in the bit field.
1004 @param EndBit The ordinal of the most significant bit in the bit field.
1006 @param AndData The value to AND with the PCI configuration register.
1008 @return The value written back to the PCI configuration register.
1013 PciExpressBitFieldAnd16 (
1020 return MmioBitFieldAnd16 (
1021 GetPciExpressAddress (Address
),
1029 Reads a bit field in a 16-bit port, performs a bitwise AND followed by a
1030 bitwise OR, and writes the result back to the bit field in the
1033 Reads the 16-bit PCI configuration register specified by Address, performs a
1034 bitwise AND followed by a bitwise OR between the read result and
1035 the value specified by AndData, and writes the result to the 16-bit PCI
1036 configuration register specified by Address. The value written to the PCI
1037 configuration register is returned. This function must guarantee that all PCI
1038 read and write operations are serialized. Extra left bits in both AndData and
1039 OrData are stripped.
1041 If Address > 0x0FFFFFFF, then ASSERT().
1042 If Address is not aligned on a 16-bit boundary, then ASSERT().
1043 If StartBit is greater than 15, then ASSERT().
1044 If EndBit is greater than 15, then ASSERT().
1045 If EndBit is less than StartBit, then ASSERT().
1047 @param Address PCI configuration register to write.
1048 @param StartBit The ordinal of the least significant bit in the bit field.
1050 @param EndBit The ordinal of the most significant bit in the bit field.
1052 @param AndData The value to AND with the PCI configuration register.
1053 @param OrData The value to OR with the result of the AND operation.
1055 @return The value written back to the PCI configuration register.
1060 PciExpressBitFieldAndThenOr16 (
1068 return MmioBitFieldAndThenOr16 (
1069 GetPciExpressAddress (Address
),
1078 Reads a 32-bit PCI configuration register.
1080 Reads and returns the 32-bit PCI configuration register specified by Address.
1081 This function must guarantee that all PCI read and write operations are
1084 If Address > 0x0FFFFFFF, then ASSERT().
1085 If Address is not aligned on a 32-bit boundary, then ASSERT().
1087 @param Address Address that encodes the PCI Bus, Device, Function and
1090 @return The read value from the PCI configuration register.
1099 return MmioRead32 (GetPciExpressAddress (Address
));
1103 Writes a 32-bit PCI configuration register.
1105 Writes the 32-bit PCI configuration register specified by Address with the
1106 value specified by Value. Value is returned. This function must guarantee
1107 that all PCI read and write operations are serialized.
1109 If Address > 0x0FFFFFFF, then ASSERT().
1110 If Address is not aligned on a 32-bit boundary, then ASSERT().
1112 @param Address Address that encodes the PCI Bus, Device, Function and
1114 @param Value The value to write.
1116 @return The value written to the PCI configuration register.
1126 return MmioWrite32 (GetPciExpressAddress (Address
), Value
);
1130 Performs a bitwise OR of a 32-bit PCI configuration register with
1133 Reads the 32-bit PCI configuration register specified by Address, performs a
1134 bitwise OR between the read result and the value specified by
1135 OrData, and writes the result to the 32-bit PCI configuration register
1136 specified by Address. The value written to the PCI configuration register is
1137 returned. This function must guarantee that all PCI read and write operations
1140 If Address > 0x0FFFFFFF, then ASSERT().
1141 If Address is not aligned on a 32-bit boundary, then ASSERT().
1143 @param Address Address that encodes the PCI Bus, Device, Function and
1145 @param OrData The value to OR with the PCI configuration register.
1147 @return The value written back to the PCI configuration register.
1157 return MmioOr32 (GetPciExpressAddress (Address
), OrData
);
1161 Performs a bitwise AND of a 32-bit PCI configuration register with a 32-bit
1164 Reads the 32-bit PCI configuration register specified by Address, performs a
1165 bitwise AND between the read result and the value specified by AndData, and
1166 writes the result to the 32-bit PCI configuration register specified by
1167 Address. The value written to the PCI configuration register is returned.
1168 This function must guarantee that all PCI read and write operations are
1171 If Address > 0x0FFFFFFF, then ASSERT().
1172 If Address is not aligned on a 32-bit boundary, then ASSERT().
1174 @param Address Address that encodes the PCI Bus, Device, Function and
1176 @param AndData The value to AND with the PCI configuration register.
1178 @return The value written back to the PCI configuration register.
1188 return MmioAnd32 (GetPciExpressAddress (Address
), AndData
);
1192 Performs a bitwise AND of a 32-bit PCI configuration register with a 32-bit
1193 value, followed a bitwise OR with another 32-bit value.
1195 Reads the 32-bit PCI configuration register specified by Address, performs a
1196 bitwise AND between the read result and the value specified by AndData,
1197 performs a bitwise OR between the result of the AND operation and
1198 the value specified by OrData, and writes the result to the 32-bit PCI
1199 configuration register specified by Address. The value written to the PCI
1200 configuration register is returned. This function must guarantee that all PCI
1201 read and write operations are serialized.
1203 If Address > 0x0FFFFFFF, then ASSERT().
1204 If Address is not aligned on a 32-bit boundary, then ASSERT().
1206 @param Address Address that encodes the PCI Bus, Device, Function and
1208 @param AndData The value to AND with the PCI configuration register.
1209 @param OrData The value to OR with the result of the AND operation.
1211 @return The value written back to the PCI configuration register.
1216 PciExpressAndThenOr32 (
1222 return MmioAndThenOr32 (
1223 GetPciExpressAddress (Address
),
1230 Reads a bit field of a PCI configuration register.
1232 Reads the bit field in a 32-bit PCI configuration register. The bit field is
1233 specified by the StartBit and the EndBit. The value of the bit field is
1236 If Address > 0x0FFFFFFF, then ASSERT().
1237 If Address is not aligned on a 32-bit boundary, then ASSERT().
1238 If StartBit is greater than 31, then ASSERT().
1239 If EndBit is greater than 31, then ASSERT().
1240 If EndBit is less than StartBit, then ASSERT().
1242 @param Address PCI configuration register to read.
1243 @param StartBit The ordinal of the least significant bit in the bit field.
1245 @param EndBit The ordinal of the most significant bit in the bit field.
1248 @return The value of the bit field read from the PCI configuration register.
1253 PciExpressBitFieldRead32 (
1259 return MmioBitFieldRead32 (
1260 GetPciExpressAddress (Address
),
1267 Writes a bit field to a PCI configuration register.
1269 Writes Value to the bit field of the PCI configuration register. The bit
1270 field is specified by the StartBit and the EndBit. All other bits in the
1271 destination PCI configuration register are preserved. The new value of the
1272 32-bit register is returned.
1274 If Address > 0x0FFFFFFF, then ASSERT().
1275 If Address is not aligned on a 32-bit boundary, then ASSERT().
1276 If StartBit is greater than 31, then ASSERT().
1277 If EndBit is greater than 31, then ASSERT().
1278 If EndBit is less than StartBit, then ASSERT().
1280 @param Address PCI configuration register to write.
1281 @param StartBit The ordinal of the least significant bit in the bit field.
1283 @param EndBit The ordinal of the most significant bit in the bit field.
1285 @param Value New value of the bit field.
1287 @return The value written back to the PCI configuration register.
1292 PciExpressBitFieldWrite32 (
1299 return MmioBitFieldWrite32 (
1300 GetPciExpressAddress (Address
),
1308 Reads a bit field in a 32-bit PCI configuration, performs a bitwise OR, and
1309 writes the result back to the bit field in the 32-bit port.
1311 Reads the 32-bit PCI configuration register specified by Address, performs a
1312 bitwise OR between the read result and the value specified by
1313 OrData, and writes the result to the 32-bit PCI configuration register
1314 specified by Address. The value written to the PCI configuration register is
1315 returned. This function must guarantee that all PCI read and write operations
1316 are serialized. Extra left bits in OrData are stripped.
1318 If Address > 0x0FFFFFFF, then ASSERT().
1319 If Address is not aligned on a 32-bit boundary, then ASSERT().
1320 If StartBit is greater than 31, then ASSERT().
1321 If EndBit is greater than 31, then ASSERT().
1322 If EndBit is less than StartBit, then ASSERT().
1324 @param Address PCI configuration register to write.
1325 @param StartBit The ordinal of the least significant bit in the bit field.
1327 @param EndBit The ordinal of the most significant bit in the bit field.
1329 @param OrData The value to OR with the PCI configuration register.
1331 @return The value written back to the PCI configuration register.
1336 PciExpressBitFieldOr32 (
1343 return MmioBitFieldOr32 (
1344 GetPciExpressAddress (Address
),
1352 Reads a bit field in a 32-bit PCI configuration register, performs a bitwise
1353 AND, and writes the result back to the bit field in the 32-bit register.
1355 Reads the 32-bit PCI configuration register specified by Address, performs a
1356 bitwise AND between the read result and the value specified by AndData, and
1357 writes the result to the 32-bit PCI configuration register specified by
1358 Address. The value written to the PCI configuration register is returned.
1359 This function must guarantee that all PCI read and write operations are
1360 serialized. Extra left bits in AndData are stripped.
1362 If Address > 0x0FFFFFFF, then ASSERT().
1363 If Address is not aligned on a 32-bit boundary, then ASSERT().
1364 If StartBit is greater than 31, then ASSERT().
1365 If EndBit is greater than 31, then ASSERT().
1366 If EndBit is less than StartBit, then ASSERT().
1368 @param Address PCI configuration register to write.
1369 @param StartBit The ordinal of the least significant bit in the bit field.
1371 @param EndBit The ordinal of the most significant bit in the bit field.
1373 @param AndData The value to AND with the PCI configuration register.
1375 @return The value written back to the PCI configuration register.
1380 PciExpressBitFieldAnd32 (
1387 return MmioBitFieldAnd32 (
1388 GetPciExpressAddress (Address
),
1396 Reads a bit field in a 32-bit port, performs a bitwise AND followed by a
1397 bitwise OR, and writes the result back to the bit field in the
1400 Reads the 32-bit PCI configuration register specified by Address, performs a
1401 bitwise AND followed by a bitwise OR between the read result and
1402 the value specified by AndData, and writes the result to the 32-bit PCI
1403 configuration register specified by Address. The value written to the PCI
1404 configuration register is returned. This function must guarantee that all PCI
1405 read and write operations are serialized. Extra left bits in both AndData and
1406 OrData are stripped.
1408 If Address > 0x0FFFFFFF, then ASSERT().
1409 If Address is not aligned on a 32-bit boundary, then ASSERT().
1410 If StartBit is greater than 31, then ASSERT().
1411 If EndBit is greater than 31, then ASSERT().
1412 If EndBit is less than StartBit, then ASSERT().
1414 @param Address PCI configuration register to write.
1415 @param StartBit The ordinal of the least significant bit in the bit field.
1417 @param EndBit The ordinal of the most significant bit in the bit field.
1419 @param AndData The value to AND with the PCI configuration register.
1420 @param OrData The value to OR with the result of the AND operation.
1422 @return The value written back to the PCI configuration register.
1427 PciExpressBitFieldAndThenOr32 (
1435 return MmioBitFieldAndThenOr32 (
1436 GetPciExpressAddress (Address
),
1445 Reads a range of PCI configuration registers into a caller supplied buffer.
1447 Reads the range of PCI configuration registers specified by StartAddress and
1448 Size into the buffer specified by Buffer. This function only allows the PCI
1449 configuration registers from a single PCI function to be read. Size is
1450 returned. When possible 32-bit PCI configuration read cycles are used to read
1451 from StartAdress to StartAddress + Size. Due to alignment restrictions, 8-bit
1452 and 16-bit PCI configuration read cycles may be used at the beginning and the
1455 If StartAddress > 0x0FFFFFFF, then ASSERT().
1456 If ((StartAddress & 0xFFF) + Size) > 0x1000, then ASSERT().
1457 If Size > 0 and Buffer is NULL, then ASSERT().
1459 @param StartAddress Starting address that encodes the PCI Bus, Device,
1460 Function and Register.
1461 @param Size Size in bytes of the transfer.
1462 @param Buffer Pointer to a buffer receiving the data read.
1464 @return Size read data from StartAddress.
1469 PciExpressReadBuffer (
1470 IN UINTN StartAddress
,
1478 // Make sure Address is valid
1480 ASSERT (((StartAddress
) & ~0xfffffff) == 0);
1481 ASSERT (((StartAddress
& 0xFFF) + Size
) <= 0x1000);
1487 ASSERT (Buffer
!= NULL
);
1490 // Save Size for return
1494 if ((StartAddress
& 1) != 0) {
1496 // Read a byte if StartAddress is byte aligned
1498 *(volatile UINT8
*)Buffer
= PciExpressRead8 (StartAddress
);
1499 StartAddress
+= sizeof (UINT8
);
1500 Size
-= sizeof (UINT8
);
1501 Buffer
= (UINT8
*)Buffer
+ 1;
1504 if (Size
>= sizeof (UINT16
) && (StartAddress
& 2) != 0) {
1506 // Read a word if StartAddress is word aligned
1508 WriteUnaligned16 ((UINT16
*) Buffer
, (UINT16
) PciExpressRead16 (StartAddress
));
1510 StartAddress
+= sizeof (UINT16
);
1511 Size
-= sizeof (UINT16
);
1512 Buffer
= (UINT16
*)Buffer
+ 1;
1515 while (Size
>= sizeof (UINT32
)) {
1517 // Read as many double words as possible
1519 WriteUnaligned32 ((UINT32
*) Buffer
, (UINT32
) PciExpressRead32 (StartAddress
));
1521 StartAddress
+= sizeof (UINT32
);
1522 Size
-= sizeof (UINT32
);
1523 Buffer
= (UINT32
*)Buffer
+ 1;
1526 if (Size
>= sizeof (UINT16
)) {
1528 // Read the last remaining word if exist
1530 WriteUnaligned16 ((UINT16
*) Buffer
, (UINT16
) PciExpressRead16 (StartAddress
));
1531 StartAddress
+= sizeof (UINT16
);
1532 Size
-= sizeof (UINT16
);
1533 Buffer
= (UINT16
*)Buffer
+ 1;
1536 if (Size
>= sizeof (UINT8
)) {
1538 // Read the last remaining byte if exist
1540 *(volatile UINT8
*)Buffer
= PciExpressRead8 (StartAddress
);
1547 Copies the data in a caller supplied buffer to a specified range of PCI
1548 configuration space.
1550 Writes the range of PCI configuration registers specified by StartAddress and
1551 Size from the buffer specified by Buffer. This function only allows the PCI
1552 configuration registers from a single PCI function to be written. Size is
1553 returned. When possible 32-bit PCI configuration write cycles are used to
1554 write from StartAdress to StartAddress + Size. Due to alignment restrictions,
1555 8-bit and 16-bit PCI configuration write cycles may be used at the beginning
1556 and the end of the range.
1558 If StartAddress > 0x0FFFFFFF, then ASSERT().
1559 If ((StartAddress & 0xFFF) + Size) > 0x1000, then ASSERT().
1560 If Size > 0 and Buffer is NULL, then ASSERT().
1562 @param StartAddress Starting address that encodes the PCI Bus, Device,
1563 Function and Register.
1564 @param Size Size in bytes of the transfer.
1565 @param Buffer Pointer to a buffer containing the data to write.
1567 @return Size written to StartAddress.
1572 PciExpressWriteBuffer (
1573 IN UINTN StartAddress
,
1581 // Make sure Address is valid
1583 ASSERT (((StartAddress
) & ~0xfffffff) == 0);
1584 ASSERT (((StartAddress
& 0xFFF) + Size
) <= 0x1000);
1590 ASSERT (Buffer
!= NULL
);
1593 // Save Size for return
1597 if ((StartAddress
& 1) != 0) {
1599 // Write a byte if StartAddress is byte aligned
1601 PciExpressWrite8 (StartAddress
, *(UINT8
*)Buffer
);
1602 StartAddress
+= sizeof (UINT8
);
1603 Size
-= sizeof (UINT8
);
1604 Buffer
= (UINT8
*)Buffer
+ 1;
1607 if (Size
>= sizeof (UINT16
) && (StartAddress
& 2) != 0) {
1609 // Write a word if StartAddress is word aligned
1611 PciExpressWrite16 (StartAddress
, ReadUnaligned16 ((UINT16
*)Buffer
));
1612 StartAddress
+= sizeof (UINT16
);
1613 Size
-= sizeof (UINT16
);
1614 Buffer
= (UINT16
*)Buffer
+ 1;
1617 while (Size
>= sizeof (UINT32
)) {
1619 // Write as many double words as possible
1621 PciExpressWrite32 (StartAddress
, ReadUnaligned32 ((UINT32
*)Buffer
));
1622 StartAddress
+= sizeof (UINT32
);
1623 Size
-= sizeof (UINT32
);
1624 Buffer
= (UINT32
*)Buffer
+ 1;
1627 if (Size
>= sizeof (UINT16
)) {
1629 // Write the last remaining word if exist
1631 PciExpressWrite16 (StartAddress
, ReadUnaligned16 ((UINT16
*)Buffer
));
1632 StartAddress
+= sizeof (UINT16
);
1633 Size
-= sizeof (UINT16
);
1634 Buffer
= (UINT16
*)Buffer
+ 1;
1637 if (Size
>= sizeof (UINT8
)) {
1639 // Write the last remaining byte if exist
1641 PciExpressWrite8 (StartAddress
, *(UINT8
*)Buffer
);