3 PCI Root Bridge Io Protocol code.
5 Copyright (c) 1999 - 2018, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "PciHostBridge.h"
17 #include "PciRootBridge.h"
18 #include "PciHostResource.h"
20 extern EDKII_IOMMU_PROTOCOL
*mIoMmuProtocol
;
22 #define NO_MAPPING (VOID *) (UINTN) -1
25 // Lookup table for increment values based on transfer widths
28 1, // EfiPciWidthUint8
29 2, // EfiPciWidthUint16
30 4, // EfiPciWidthUint32
31 8, // EfiPciWidthUint64
32 0, // EfiPciWidthFifoUint8
33 0, // EfiPciWidthFifoUint16
34 0, // EfiPciWidthFifoUint32
35 0, // EfiPciWidthFifoUint64
36 1, // EfiPciWidthFillUint8
37 2, // EfiPciWidthFillUint16
38 4, // EfiPciWidthFillUint32
39 8 // EfiPciWidthFillUint64
43 // Lookup table for increment values based on transfer widths
45 UINT8 mOutStride
[] = {
46 1, // EfiPciWidthUint8
47 2, // EfiPciWidthUint16
48 4, // EfiPciWidthUint32
49 8, // EfiPciWidthUint64
50 1, // EfiPciWidthFifoUint8
51 2, // EfiPciWidthFifoUint16
52 4, // EfiPciWidthFifoUint32
53 8, // EfiPciWidthFifoUint64
54 0, // EfiPciWidthFillUint8
55 0, // EfiPciWidthFillUint16
56 0, // EfiPciWidthFillUint32
57 0 // EfiPciWidthFillUint64
61 Construct the Pci Root Bridge instance.
63 @param Bridge The root bridge instance.
65 @return The pointer to PCI_ROOT_BRIDGE_INSTANCE just created
66 or NULL if creation fails.
68 PCI_ROOT_BRIDGE_INSTANCE
*
70 IN PCI_ROOT_BRIDGE
*Bridge
73 PCI_ROOT_BRIDGE_INSTANCE
*RootBridge
;
74 PCI_RESOURCE_TYPE Index
;
75 CHAR16
*DevicePathStr
;
76 PCI_ROOT_BRIDGE_APERTURE
*Aperture
;
80 DEBUG ((EFI_D_INFO
, "RootBridge: "));
81 DEBUG ((EFI_D_INFO
, "%s\n", DevicePathStr
= ConvertDevicePathToText (Bridge
->DevicePath
, FALSE
, FALSE
)));
82 DEBUG ((EFI_D_INFO
, " Support/Attr: %lx / %lx\n", Bridge
->Supports
, Bridge
->Attributes
));
83 DEBUG ((EFI_D_INFO
, " DmaAbove4G: %s\n", Bridge
->DmaAbove4G
? L
"Yes" : L
"No"));
84 DEBUG ((EFI_D_INFO
, "NoExtConfSpace: %s\n", Bridge
->NoExtendedConfigSpace
? L
"Yes" : L
"No"));
85 DEBUG ((EFI_D_INFO
, " AllocAttr: %lx (%s%s)\n", Bridge
->AllocationAttributes
,
86 (Bridge
->AllocationAttributes
& EFI_PCI_HOST_BRIDGE_COMBINE_MEM_PMEM
) != 0 ? L
"CombineMemPMem " : L
"",
87 (Bridge
->AllocationAttributes
& EFI_PCI_HOST_BRIDGE_MEM64_DECODE
) != 0 ? L
"Mem64Decode" : L
""
90 EFI_D_INFO
, " Bus: %lx - %lx Translation=%lx\n",
91 Bridge
->Bus
.Base
, Bridge
->Bus
.Limit
, Bridge
->Bus
.Translation
94 // Translation for bus is not supported.
96 ASSERT (Bridge
->Bus
.Translation
== 0);
97 if (Bridge
->Bus
.Translation
!= 0) {
102 DEBUG_INFO
, " Io: %lx - %lx Translation=%lx\n",
103 Bridge
->Io
.Base
, Bridge
->Io
.Limit
, Bridge
->Io
.Translation
106 DEBUG_INFO
, " Mem: %lx - %lx Translation=%lx\n",
107 Bridge
->Mem
.Base
, Bridge
->Mem
.Limit
, Bridge
->Mem
.Translation
110 DEBUG_INFO
, " MemAbove4G: %lx - %lx Translation=%lx\n",
111 Bridge
->MemAbove4G
.Base
, Bridge
->MemAbove4G
.Limit
, Bridge
->MemAbove4G
.Translation
114 DEBUG_INFO
, " PMem: %lx - %lx Translation=%lx\n",
115 Bridge
->PMem
.Base
, Bridge
->PMem
.Limit
, Bridge
->PMem
.Translation
118 DEBUG_INFO
, " PMemAbove4G: %lx - %lx Translation=%lx\n",
119 Bridge
->PMemAbove4G
.Base
, Bridge
->PMemAbove4G
.Limit
, Bridge
->PMemAbove4G
.Translation
123 // Make sure Mem and MemAbove4G apertures are valid
125 if (Bridge
->Mem
.Base
<= Bridge
->Mem
.Limit
) {
126 ASSERT (Bridge
->Mem
.Limit
< SIZE_4GB
);
127 if (Bridge
->Mem
.Limit
>= SIZE_4GB
) {
131 if (Bridge
->MemAbove4G
.Base
<= Bridge
->MemAbove4G
.Limit
) {
132 ASSERT (Bridge
->MemAbove4G
.Base
>= SIZE_4GB
);
133 if (Bridge
->MemAbove4G
.Base
< SIZE_4GB
) {
137 if (Bridge
->PMem
.Base
<= Bridge
->PMem
.Limit
) {
138 ASSERT (Bridge
->PMem
.Limit
< SIZE_4GB
);
139 if (Bridge
->PMem
.Limit
>= SIZE_4GB
) {
143 if (Bridge
->PMemAbove4G
.Base
<= Bridge
->PMemAbove4G
.Limit
) {
144 ASSERT (Bridge
->PMemAbove4G
.Base
>= SIZE_4GB
);
145 if (Bridge
->PMemAbove4G
.Base
< SIZE_4GB
) {
151 // Ignore AllocationAttributes when resources were already assigned.
153 if (!Bridge
->ResourceAssigned
) {
154 if ((Bridge
->AllocationAttributes
& EFI_PCI_HOST_BRIDGE_COMBINE_MEM_PMEM
) != 0) {
156 // If this bit is set, then the PCI Root Bridge does not
157 // support separate windows for Non-prefetchable and Prefetchable
160 ASSERT (Bridge
->PMem
.Base
> Bridge
->PMem
.Limit
);
161 ASSERT (Bridge
->PMemAbove4G
.Base
> Bridge
->PMemAbove4G
.Limit
);
162 if ((Bridge
->PMem
.Base
<= Bridge
->PMem
.Limit
) ||
163 (Bridge
->PMemAbove4G
.Base
<= Bridge
->PMemAbove4G
.Limit
)
169 if ((Bridge
->AllocationAttributes
& EFI_PCI_HOST_BRIDGE_MEM64_DECODE
) == 0) {
171 // If this bit is not set, then the PCI Root Bridge does not support
172 // 64 bit memory windows.
174 ASSERT (Bridge
->MemAbove4G
.Base
> Bridge
->MemAbove4G
.Limit
);
175 ASSERT (Bridge
->PMemAbove4G
.Base
> Bridge
->PMemAbove4G
.Limit
);
176 if ((Bridge
->MemAbove4G
.Base
<= Bridge
->MemAbove4G
.Limit
) ||
177 (Bridge
->PMemAbove4G
.Base
<= Bridge
->PMemAbove4G
.Limit
)
184 RootBridge
= AllocateZeroPool (sizeof (PCI_ROOT_BRIDGE_INSTANCE
));
185 ASSERT (RootBridge
!= NULL
);
187 RootBridge
->Signature
= PCI_ROOT_BRIDGE_SIGNATURE
;
188 RootBridge
->Supports
= Bridge
->Supports
;
189 RootBridge
->Attributes
= Bridge
->Attributes
;
190 RootBridge
->DmaAbove4G
= Bridge
->DmaAbove4G
;
191 RootBridge
->NoExtendedConfigSpace
= Bridge
->NoExtendedConfigSpace
;
192 RootBridge
->AllocationAttributes
= Bridge
->AllocationAttributes
;
193 RootBridge
->DevicePath
= DuplicateDevicePath (Bridge
->DevicePath
);
194 RootBridge
->DevicePathStr
= DevicePathStr
;
195 RootBridge
->ConfigBuffer
= AllocatePool (
196 TypeMax
* sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) + sizeof (EFI_ACPI_END_TAG_DESCRIPTOR
)
198 ASSERT (RootBridge
->ConfigBuffer
!= NULL
);
199 InitializeListHead (&RootBridge
->Maps
);
201 CopyMem (&RootBridge
->Bus
, &Bridge
->Bus
, sizeof (PCI_ROOT_BRIDGE_APERTURE
));
202 CopyMem (&RootBridge
->Io
, &Bridge
->Io
, sizeof (PCI_ROOT_BRIDGE_APERTURE
));
203 CopyMem (&RootBridge
->Mem
, &Bridge
->Mem
, sizeof (PCI_ROOT_BRIDGE_APERTURE
));
204 CopyMem (&RootBridge
->MemAbove4G
, &Bridge
->MemAbove4G
, sizeof (PCI_ROOT_BRIDGE_APERTURE
));
205 CopyMem (&RootBridge
->PMem
, &Bridge
->PMem
, sizeof (PCI_ROOT_BRIDGE_APERTURE
));
206 CopyMem (&RootBridge
->PMemAbove4G
, &Bridge
->PMemAbove4G
, sizeof (PCI_ROOT_BRIDGE_APERTURE
));
208 for (Index
= TypeIo
; Index
< TypeMax
; Index
++) {
211 Aperture
= &RootBridge
->Bus
;
214 Aperture
= &RootBridge
->Io
;
217 Aperture
= &RootBridge
->Mem
;
220 Aperture
= &RootBridge
->MemAbove4G
;
223 Aperture
= &RootBridge
->PMem
;
226 Aperture
= &RootBridge
->PMemAbove4G
;
233 RootBridge
->ResAllocNode
[Index
].Type
= Index
;
234 if (Bridge
->ResourceAssigned
&& (Aperture
->Limit
>= Aperture
->Base
)) {
236 // Base in ResAllocNode is a host address, while Base in Aperture is a
239 RootBridge
->ResAllocNode
[Index
].Base
= TO_HOST_ADDRESS (Aperture
->Base
,
240 Aperture
->Translation
);
241 RootBridge
->ResAllocNode
[Index
].Length
= Aperture
->Limit
- Aperture
->Base
+ 1;
242 RootBridge
->ResAllocNode
[Index
].Status
= ResAllocated
;
244 RootBridge
->ResAllocNode
[Index
].Base
= 0;
245 RootBridge
->ResAllocNode
[Index
].Length
= 0;
246 RootBridge
->ResAllocNode
[Index
].Status
= ResNone
;
250 RootBridge
->RootBridgeIo
.SegmentNumber
= Bridge
->Segment
;
251 RootBridge
->RootBridgeIo
.PollMem
= RootBridgeIoPollMem
;
252 RootBridge
->RootBridgeIo
.PollIo
= RootBridgeIoPollIo
;
253 RootBridge
->RootBridgeIo
.Mem
.Read
= RootBridgeIoMemRead
;
254 RootBridge
->RootBridgeIo
.Mem
.Write
= RootBridgeIoMemWrite
;
255 RootBridge
->RootBridgeIo
.Io
.Read
= RootBridgeIoIoRead
;
256 RootBridge
->RootBridgeIo
.Io
.Write
= RootBridgeIoIoWrite
;
257 RootBridge
->RootBridgeIo
.CopyMem
= RootBridgeIoCopyMem
;
258 RootBridge
->RootBridgeIo
.Pci
.Read
= RootBridgeIoPciRead
;
259 RootBridge
->RootBridgeIo
.Pci
.Write
= RootBridgeIoPciWrite
;
260 RootBridge
->RootBridgeIo
.Map
= RootBridgeIoMap
;
261 RootBridge
->RootBridgeIo
.Unmap
= RootBridgeIoUnmap
;
262 RootBridge
->RootBridgeIo
.AllocateBuffer
= RootBridgeIoAllocateBuffer
;
263 RootBridge
->RootBridgeIo
.FreeBuffer
= RootBridgeIoFreeBuffer
;
264 RootBridge
->RootBridgeIo
.Flush
= RootBridgeIoFlush
;
265 RootBridge
->RootBridgeIo
.GetAttributes
= RootBridgeIoGetAttributes
;
266 RootBridge
->RootBridgeIo
.SetAttributes
= RootBridgeIoSetAttributes
;
267 RootBridge
->RootBridgeIo
.Configuration
= RootBridgeIoConfiguration
;
273 Check parameters for IO,MMIO,PCI read/write services of PCI Root Bridge IO.
275 The I/O operations are carried out exactly as requested. The caller is
276 responsible for satisfying any alignment and I/O width restrictions that a PI
277 System on a platform might require. For example on some platforms, width
278 requests of EfiCpuIoWidthUint64 do not work. Misaligned buffers, on the other
279 hand, will be handled by the driver.
281 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
283 @param[in] OperationType I/O operation type: IO/MMIO/PCI.
285 @param[in] Width Signifies the width of the I/O or Memory operation.
287 @param[in] Address The base address of the I/O operation.
289 @param[in] Count The number of I/O operations to perform. The number
290 of bytes moved is Width size * Count, starting at
293 @param[in] Buffer For read operations, the destination buffer to
294 store the results. For write operations, the source
295 buffer from which to write data.
297 @retval EFI_SUCCESS The parameters for this request pass the
300 @retval EFI_INVALID_PARAMETER Width is invalid for this PI system.
302 @retval EFI_INVALID_PARAMETER Buffer is NULL.
304 @retval EFI_UNSUPPORTED The Buffer is not aligned for the given Width.
306 @retval EFI_UNSUPPORTED The address range specified by Address, Width,
307 and Count is not valid for this PI system.
310 RootBridgeIoCheckParameter (
311 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*This
,
312 IN OPERATION_TYPE OperationType
,
313 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width
,
319 PCI_ROOT_BRIDGE_INSTANCE
*RootBridge
;
320 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS
*PciRbAddr
;
326 // Check to see if Buffer is NULL
328 if (Buffer
== NULL
) {
329 return EFI_INVALID_PARAMETER
;
333 // Check to see if Width is in the valid range
335 if ((UINT32
) Width
>= EfiPciWidthMaximum
) {
336 return EFI_INVALID_PARAMETER
;
340 // For FIFO type, the target address won't increase during the access,
341 // so treat Count as 1
343 if (Width
>= EfiPciWidthFifoUint8
&& Width
<= EfiPciWidthFifoUint64
) {
347 Width
= (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH
) (Width
& 0x03);
351 // Check to see if Address is aligned
353 if ((Address
& (Size
- 1)) != 0) {
354 return EFI_UNSUPPORTED
;
357 RootBridge
= ROOT_BRIDGE_FROM_THIS (This
);
360 // Check to see if any address associated with this transfer exceeds the
361 // maximum allowed address. The maximum address implied by the parameters
362 // passed in is Address + Size * Count. If the following condition is met,
363 // then the transfer is not supported.
365 // Address + Size * Count > Limit + 1
367 // Since Limit can be the maximum integer value supported by the CPU and
368 // Count can also be the maximum integer value supported by the CPU, this
369 // range check must be adjusted to avoid all oveflow conditions.
371 if (OperationType
== IoOperation
) {
373 // Allow Legacy IO access
375 if (Address
+ MultU64x32 (Count
, Size
) <= 0x1000) {
376 if ((RootBridge
->Attributes
& (
377 EFI_PCI_ATTRIBUTE_ISA_IO
| EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO
| EFI_PCI_ATTRIBUTE_VGA_IO
|
378 EFI_PCI_ATTRIBUTE_IDE_PRIMARY_IO
| EFI_PCI_ATTRIBUTE_IDE_SECONDARY_IO
|
379 EFI_PCI_ATTRIBUTE_ISA_IO_16
| EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO_16
| EFI_PCI_ATTRIBUTE_VGA_IO_16
)) != 0) {
383 Base
= RootBridge
->Io
.Base
;
384 Limit
= RootBridge
->Io
.Limit
;
385 } else if (OperationType
== MemOperation
) {
387 // Allow Legacy MMIO access
389 if ((Address
>= 0xA0000) && (Address
+ MultU64x32 (Count
, Size
)) <= 0xC0000) {
390 if ((RootBridge
->Attributes
& EFI_PCI_ATTRIBUTE_VGA_MEMORY
) != 0) {
395 // By comparing the Address against Limit we know which range to be used
398 if (Address
+ MultU64x32 (Count
, Size
) <= RootBridge
->Mem
.Limit
+ 1) {
399 Base
= RootBridge
->Mem
.Base
;
400 Limit
= RootBridge
->Mem
.Limit
;
402 Base
= RootBridge
->MemAbove4G
.Base
;
403 Limit
= RootBridge
->MemAbove4G
.Limit
;
406 PciRbAddr
= (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS
*) &Address
;
407 if (PciRbAddr
->Bus
< RootBridge
->Bus
.Base
||
408 PciRbAddr
->Bus
> RootBridge
->Bus
.Limit
) {
409 return EFI_INVALID_PARAMETER
;
412 if (PciRbAddr
->Device
> PCI_MAX_DEVICE
||
413 PciRbAddr
->Function
> PCI_MAX_FUNC
) {
414 return EFI_INVALID_PARAMETER
;
417 if (PciRbAddr
->ExtendedRegister
!= 0) {
418 Address
= PciRbAddr
->ExtendedRegister
;
420 Address
= PciRbAddr
->Register
;
423 Limit
= RootBridge
->NoExtendedConfigSpace
? 0xFF : 0xFFF;
426 if (Address
< Base
) {
427 return EFI_INVALID_PARAMETER
;
430 if (Address
+ MultU64x32 (Count
, Size
) > Limit
+ 1) {
431 return EFI_INVALID_PARAMETER
;
438 Use address to match apertures of memory type and then get the corresponding
441 @param RootBridge The root bridge instance.
442 @param Address The address used to match aperture.
443 @param Translation Pointer containing the output translation.
445 @return EFI_SUCCESS Get translation successfully.
446 @return EFI_INVALID_PARAMETER No matched memory aperture; the input Address
450 RootBridgeIoGetMemTranslationByAddress (
451 IN PCI_ROOT_BRIDGE_INSTANCE
*RootBridge
,
453 IN OUT UINT64
*Translation
456 if (Address
>= RootBridge
->Mem
.Base
&& Address
<= RootBridge
->Mem
.Limit
) {
457 *Translation
= RootBridge
->Mem
.Translation
;
458 } else if (Address
>= RootBridge
->PMem
.Base
&& Address
<= RootBridge
->PMem
.Limit
) {
459 *Translation
= RootBridge
->PMem
.Translation
;
460 } else if (Address
>= RootBridge
->MemAbove4G
.Base
&& Address
<= RootBridge
->MemAbove4G
.Limit
) {
461 *Translation
= RootBridge
->MemAbove4G
.Translation
;
462 } else if (Address
>= RootBridge
->PMemAbove4G
.Base
&& Address
<= RootBridge
->PMemAbove4G
.Limit
) {
463 *Translation
= RootBridge
->PMemAbove4G
.Translation
;
465 return EFI_INVALID_PARAMETER
;
472 Return the result of (Multiplicand * Multiplier / Divisor).
474 @param Multiplicand A 64-bit unsigned value.
475 @param Multiplier A 64-bit unsigned value.
476 @param Divisor A 32-bit unsigned value.
477 @param Remainder A pointer to a 32-bit unsigned value. This parameter is
478 optional and may be NULL.
480 @return Multiplicand * Multiplier / Divisor.
483 MultThenDivU64x64x32 (
484 IN UINT64 Multiplicand
,
485 IN UINT64 Multiplier
,
487 OUT UINT32
*Remainder OPTIONAL
491 UINT32 LocalRemainder
;
493 if (Multiplicand
> DivU64x64Remainder (MAX_UINT64
, Multiplier
, NULL
)) {
495 // Make sure Multiplicand is the bigger one.
497 if (Multiplicand
< Multiplier
) {
498 Uint64
= Multiplicand
;
499 Multiplicand
= Multiplier
;
503 // Because Multiplicand * Multiplier overflows,
504 // Multiplicand * Multiplier / Divisor
505 // = (2 * Multiplicand' + 1) * Multiplier / Divisor
506 // = 2 * (Multiplicand' * Multiplier / Divisor) + Multiplier / Divisor
508 Uint64
= MultThenDivU64x64x32 (RShiftU64 (Multiplicand
, 1), Multiplier
, Divisor
, &LocalRemainder
);
509 Uint64
= LShiftU64 (Uint64
, 1);
511 if ((Multiplicand
& 0x1) == 1) {
512 Uint64
+= DivU64x32Remainder (Multiplier
, Divisor
, &Uint32
);
514 return Uint64
+ DivU64x32Remainder (Uint32
+ LShiftU64 (LocalRemainder
, 1), Divisor
, Remainder
);
516 return DivU64x32Remainder (MultU64x64 (Multiplicand
, Multiplier
), Divisor
, Remainder
);
521 Return the elapsed tick count from CurrentTick.
523 @param CurrentTick On input, the previous tick count.
524 On output, the current tick count.
525 @param StartTick The value the performance counter starts with when it
527 @param EndTick The value that the performance counter ends with before
530 @return The elapsed tick count from CurrentTick.
541 PreviousTick
= *CurrentTick
;
542 *CurrentTick
= GetPerformanceCounter();
543 if (StartTick
< EndTick
) {
544 return *CurrentTick
- PreviousTick
;
546 return PreviousTick
- *CurrentTick
;
551 Polls an address in memory mapped I/O space until an exit condition is met,
554 This function provides a standard way to poll a PCI memory location. A PCI
555 memory read operation is performed at the PCI memory address specified by
556 Address for the width specified by Width. The result of this PCI memory read
557 operation is stored in Result. This PCI memory read operation is repeated
558 until either a timeout of Delay 100 ns units has expired, or (Result & Mask)
561 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
562 @param[in] Width Signifies the width of the memory operations.
563 @param[in] Address The base address of the memory operations. The caller
564 is responsible for aligning Address if required.
565 @param[in] Mask Mask used for the polling criteria. Bytes above Width
566 in Mask are ignored. The bits in the bytes below Width
567 which are zero in Mask are ignored when polling the
569 @param[in] Value The comparison value used for the polling exit
571 @param[in] Delay The number of 100 ns units to poll. Note that timer
572 available may be of poorer granularity.
573 @param[out] Result Pointer to the last value read from the memory
576 @retval EFI_SUCCESS The last data returned from the access matched
577 the poll exit criteria.
578 @retval EFI_INVALID_PARAMETER Width is invalid.
579 @retval EFI_INVALID_PARAMETER Result is NULL.
580 @retval EFI_TIMEOUT Delay expired before a match occurred.
581 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
586 RootBridgeIoPollMem (
587 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*This
,
588 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width
,
597 UINT64 NumberOfTicks
;
605 if (Result
== NULL
) {
606 return EFI_INVALID_PARAMETER
;
609 if ((UINT32
)Width
> EfiPciWidthUint64
) {
610 return EFI_INVALID_PARAMETER
;
614 // No matter what, always do a single poll.
616 Status
= This
->Mem
.Read (This
, Width
, Address
, 1, Result
);
617 if (EFI_ERROR (Status
)) {
621 if ((*Result
& Mask
) == Value
) {
630 // NumberOfTicks = Frenquency * Delay / EFI_TIMER_PERIOD_SECONDS(1)
632 Frequency
= GetPerformanceCounterProperties (&StartTick
, &EndTick
);
633 NumberOfTicks
= MultThenDivU64x64x32 (Frequency
, Delay
, (UINT32
)EFI_TIMER_PERIOD_SECONDS(1), &Remainder
);
634 if (Remainder
>= (UINTN
)EFI_TIMER_PERIOD_SECONDS(1) / 2) {
637 for ( ElapsedTick
= 0, CurrentTick
= GetPerformanceCounter()
638 ; ElapsedTick
<= NumberOfTicks
639 ; ElapsedTick
+= GetElapsedTick (&CurrentTick
, StartTick
, EndTick
)
641 Status
= This
->Mem
.Read (This
, Width
, Address
, 1, Result
);
642 if (EFI_ERROR (Status
)) {
646 if ((*Result
& Mask
) == Value
) {
655 Reads from the I/O space of a PCI Root Bridge. Returns when either the
656 polling exit criteria is satisfied or after a defined duration.
658 This function provides a standard way to poll a PCI I/O location. A PCI I/O
659 read operation is performed at the PCI I/O address specified by Address for
660 the width specified by Width.
661 The result of this PCI I/O read operation is stored in Result. This PCI I/O
662 read operation is repeated until either a timeout of Delay 100 ns units has
663 expired, or (Result & Mask) is equal to Value.
665 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
666 @param[in] Width Signifies the width of the I/O operations.
667 @param[in] Address The base address of the I/O operations. The caller is
668 responsible for aligning Address if required.
669 @param[in] Mask Mask used for the polling criteria. Bytes above Width in
670 Mask are ignored. The bits in the bytes below Width
671 which are zero in Mask are ignored when polling the I/O
673 @param[in] Value The comparison value used for the polling exit criteria.
674 @param[in] Delay The number of 100 ns units to poll. Note that timer
675 available may be of poorer granularity.
676 @param[out] Result Pointer to the last value read from the memory location.
678 @retval EFI_SUCCESS The last data returned from the access matched
679 the poll exit criteria.
680 @retval EFI_INVALID_PARAMETER Width is invalid.
681 @retval EFI_INVALID_PARAMETER Result is NULL.
682 @retval EFI_TIMEOUT Delay expired before a match occurred.
683 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
689 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*This
,
690 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width
,
699 UINT64 NumberOfTicks
;
708 // No matter what, always do a single poll.
711 if (Result
== NULL
) {
712 return EFI_INVALID_PARAMETER
;
715 if ((UINT32
)Width
> EfiPciWidthUint64
) {
716 return EFI_INVALID_PARAMETER
;
719 Status
= This
->Io
.Read (This
, Width
, Address
, 1, Result
);
720 if (EFI_ERROR (Status
)) {
723 if ((*Result
& Mask
) == Value
) {
732 // NumberOfTicks = Frenquency * Delay / EFI_TIMER_PERIOD_SECONDS(1)
734 Frequency
= GetPerformanceCounterProperties (&StartTick
, &EndTick
);
735 NumberOfTicks
= MultThenDivU64x64x32 (Frequency
, Delay
, (UINT32
)EFI_TIMER_PERIOD_SECONDS(1), &Remainder
);
736 if (Remainder
>= (UINTN
)EFI_TIMER_PERIOD_SECONDS(1) / 2) {
739 for ( ElapsedTick
= 0, CurrentTick
= GetPerformanceCounter()
740 ; ElapsedTick
<= NumberOfTicks
741 ; ElapsedTick
+= GetElapsedTick (&CurrentTick
, StartTick
, EndTick
)
743 Status
= This
->Io
.Read (This
, Width
, Address
, 1, Result
);
744 if (EFI_ERROR (Status
)) {
748 if ((*Result
& Mask
) == Value
) {
757 Enables a PCI driver to access PCI controller registers in the PCI root
760 The Mem.Read(), and Mem.Write() functions enable a driver to access PCI
761 controller registers in the PCI root bridge memory space.
762 The memory operations are carried out exactly as requested. The caller is
763 responsible for satisfying any alignment and memory width restrictions that a
764 PCI Root Bridge on a platform might require.
766 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
767 @param[in] Width Signifies the width of the memory operation.
768 @param[in] Address The base address of the memory operation. The caller
769 is responsible for aligning the Address if required.
770 @param[in] Count The number of memory operations to perform. Bytes
771 moved is Width size * Count, starting at Address.
772 @param[out] Buffer For read operations, the destination buffer to store
773 the results. For write operations, the source buffer
776 @retval EFI_SUCCESS The data was read from or written to the PCI
778 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
779 @retval EFI_INVALID_PARAMETER Buffer is NULL.
780 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
785 RootBridgeIoMemRead (
786 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*This
,
787 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width
,
794 PCI_ROOT_BRIDGE_INSTANCE
*RootBridge
;
797 Status
= RootBridgeIoCheckParameter (This
, MemOperation
, Width
, Address
,
799 if (EFI_ERROR (Status
)) {
803 RootBridge
= ROOT_BRIDGE_FROM_THIS (This
);
804 Status
= RootBridgeIoGetMemTranslationByAddress (RootBridge
, Address
, &Translation
);
805 if (EFI_ERROR (Status
)) {
809 // Address passed to CpuIo->Mem.Read needs to be a host address instead of
811 return mCpuIo
->Mem
.Read (mCpuIo
, (EFI_CPU_IO_PROTOCOL_WIDTH
) Width
,
812 TO_HOST_ADDRESS (Address
, Translation
), Count
, Buffer
);
816 Enables a PCI driver to access PCI controller registers in the PCI root
819 The Mem.Read(), and Mem.Write() functions enable a driver to access PCI
820 controller registers in the PCI root bridge memory space.
821 The memory operations are carried out exactly as requested. The caller is
822 responsible for satisfying any alignment and memory width restrictions that a
823 PCI Root Bridge on a platform might require.
825 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
826 @param[in] Width Signifies the width of the memory operation.
827 @param[in] Address The base address of the memory operation. The caller
828 is responsible for aligning the Address if required.
829 @param[in] Count The number of memory operations to perform. Bytes
830 moved is Width size * Count, starting at Address.
831 @param[in] Buffer For read operations, the destination buffer to store
832 the results. For write operations, the source buffer
835 @retval EFI_SUCCESS The data was read from or written to the PCI
837 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
838 @retval EFI_INVALID_PARAMETER Buffer is NULL.
839 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
844 RootBridgeIoMemWrite (
845 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*This
,
846 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width
,
853 PCI_ROOT_BRIDGE_INSTANCE
*RootBridge
;
856 Status
= RootBridgeIoCheckParameter (This
, MemOperation
, Width
, Address
,
858 if (EFI_ERROR (Status
)) {
862 RootBridge
= ROOT_BRIDGE_FROM_THIS (This
);
863 Status
= RootBridgeIoGetMemTranslationByAddress (RootBridge
, Address
, &Translation
);
864 if (EFI_ERROR (Status
)) {
868 // Address passed to CpuIo->Mem.Write needs to be a host address instead of
870 return mCpuIo
->Mem
.Write (mCpuIo
, (EFI_CPU_IO_PROTOCOL_WIDTH
) Width
,
871 TO_HOST_ADDRESS (Address
, Translation
), Count
, Buffer
);
875 Enables a PCI driver to access PCI controller registers in the PCI root
878 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
879 @param[in] Width Signifies the width of the memory operations.
880 @param[in] Address The base address of the I/O operation. The caller is
881 responsible for aligning the Address if required.
882 @param[in] Count The number of I/O operations to perform. Bytes moved
883 is Width size * Count, starting at Address.
884 @param[out] Buffer For read operations, the destination buffer to store
885 the results. For write operations, the source buffer
888 @retval EFI_SUCCESS The data was read from or written to the PCI
890 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
891 @retval EFI_INVALID_PARAMETER Buffer is NULL.
892 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
898 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*This
,
899 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width
,
906 PCI_ROOT_BRIDGE_INSTANCE
*RootBridge
;
908 Status
= RootBridgeIoCheckParameter (
909 This
, IoOperation
, Width
,
910 Address
, Count
, Buffer
912 if (EFI_ERROR (Status
)) {
916 RootBridge
= ROOT_BRIDGE_FROM_THIS (This
);
918 // Address passed to CpuIo->Io.Read needs to be a host address instead of
920 return mCpuIo
->Io
.Read (mCpuIo
, (EFI_CPU_IO_PROTOCOL_WIDTH
) Width
,
921 TO_HOST_ADDRESS (Address
, RootBridge
->Io
.Translation
), Count
, Buffer
);
925 Enables a PCI driver to access PCI controller registers in the PCI root
928 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
929 @param[in] Width Signifies the width of the memory operations.
930 @param[in] Address The base address of the I/O operation. The caller is
931 responsible for aligning the Address if required.
932 @param[in] Count The number of I/O operations to perform. Bytes moved
933 is Width size * Count, starting at Address.
934 @param[in] Buffer For read operations, the destination buffer to store
935 the results. For write operations, the source buffer
938 @retval EFI_SUCCESS The data was read from or written to the PCI
940 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
941 @retval EFI_INVALID_PARAMETER Buffer is NULL.
942 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
947 RootBridgeIoIoWrite (
948 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*This
,
949 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width
,
956 PCI_ROOT_BRIDGE_INSTANCE
*RootBridge
;
958 Status
= RootBridgeIoCheckParameter (
959 This
, IoOperation
, Width
,
960 Address
, Count
, Buffer
962 if (EFI_ERROR (Status
)) {
966 RootBridge
= ROOT_BRIDGE_FROM_THIS (This
);
968 // Address passed to CpuIo->Io.Write needs to be a host address instead of
970 return mCpuIo
->Io
.Write (mCpuIo
, (EFI_CPU_IO_PROTOCOL_WIDTH
) Width
,
971 TO_HOST_ADDRESS (Address
, RootBridge
->Io
.Translation
), Count
, Buffer
);
975 Enables a PCI driver to copy one region of PCI root bridge memory space to
976 another region of PCI root bridge memory space.
978 The CopyMem() function enables a PCI driver to copy one region of PCI root
979 bridge memory space to another region of PCI root bridge memory space. This
980 is especially useful for video scroll operation on a memory mapped video
982 The memory operations are carried out exactly as requested. The caller is
983 responsible for satisfying any alignment and memory width restrictions that a
984 PCI root bridge on a platform might require.
986 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
988 @param[in] Width Signifies the width of the memory operations.
989 @param[in] DestAddress The destination address of the memory operation. The
990 caller is responsible for aligning the DestAddress if
992 @param[in] SrcAddress The source address of the memory operation. The caller
993 is responsible for aligning the SrcAddress if
995 @param[in] Count The number of memory operations to perform. Bytes
996 moved is Width size * Count, starting at DestAddress
999 @retval EFI_SUCCESS The data was copied from one memory region
1000 to another memory region.
1001 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
1002 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
1007 RootBridgeIoCopyMem (
1008 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*This
,
1009 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width
,
1010 IN UINT64 DestAddress
,
1011 IN UINT64 SrcAddress
,
1021 if ((UINT32
) Width
> EfiPciWidthUint64
) {
1022 return EFI_INVALID_PARAMETER
;
1025 if (DestAddress
== SrcAddress
) {
1029 Stride
= (UINTN
) (1 << Width
);
1032 if ((DestAddress
> SrcAddress
) &&
1033 (DestAddress
< (SrcAddress
+ Count
* Stride
))) {
1035 SrcAddress
= SrcAddress
+ (Count
- 1) * Stride
;
1036 DestAddress
= DestAddress
+ (Count
- 1) * Stride
;
1039 for (Index
= 0; Index
< Count
; Index
++) {
1040 Status
= RootBridgeIoMemRead (
1047 if (EFI_ERROR (Status
)) {
1050 Status
= RootBridgeIoMemWrite (
1057 if (EFI_ERROR (Status
)) {
1061 SrcAddress
+= Stride
;
1062 DestAddress
+= Stride
;
1064 SrcAddress
-= Stride
;
1065 DestAddress
-= Stride
;
1073 PCI configuration space access.
1075 @param This A pointer to EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
1076 @param Read TRUE indicating it's a read operation.
1077 @param Width Signifies the width of the memory operation.
1078 @param Address The address within the PCI configuration space
1079 for the PCI controller.
1080 @param Count The number of PCI configuration operations
1082 @param Buffer The destination buffer to store the results.
1084 @retval EFI_SUCCESS The data was read/written from/to the PCI root bridge.
1085 @retval EFI_INVALID_PARAMETER Invalid parameters found.
1089 RootBridgeIoPciAccess (
1090 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*This
,
1092 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width
,
1099 PCI_ROOT_BRIDGE_INSTANCE
*RootBridge
;
1100 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS PciAddress
;
1106 Status
= RootBridgeIoCheckParameter (This
, PciOperation
, Width
, Address
, Count
, Buffer
);
1107 if (EFI_ERROR (Status
)) {
1112 // Read Pci configuration space
1114 RootBridge
= ROOT_BRIDGE_FROM_THIS (This
);
1115 CopyMem (&PciAddress
, &Address
, sizeof (PciAddress
));
1117 if (PciAddress
.ExtendedRegister
== 0) {
1118 PciAddress
.ExtendedRegister
= PciAddress
.Register
;
1121 Address
= PCI_SEGMENT_LIB_ADDRESS (
1122 RootBridge
->RootBridgeIo
.SegmentNumber
,
1125 PciAddress
.Function
,
1126 PciAddress
.ExtendedRegister
1130 // Select loop based on the width of the transfer
1132 InStride
= mInStride
[Width
];
1133 OutStride
= mOutStride
[Width
];
1134 Size
= (UINTN
) (1 << (Width
& 0x03));
1135 for (Uint8Buffer
= Buffer
; Count
> 0; Address
+= InStride
, Uint8Buffer
+= OutStride
, Count
--) {
1137 PciSegmentReadBuffer (Address
, Size
, Uint8Buffer
);
1139 PciSegmentWriteBuffer (Address
, Size
, Uint8Buffer
);
1146 Allows read from PCI configuration space.
1148 @param This A pointer to EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
1149 @param Width Signifies the width of the memory operation.
1150 @param Address The address within the PCI configuration space
1151 for the PCI controller.
1152 @param Count The number of PCI configuration operations
1154 @param Buffer The destination buffer to store the results.
1156 @retval EFI_SUCCESS The data was read from the PCI root bridge.
1157 @retval EFI_INVALID_PARAMETER Invalid parameters found.
1161 RootBridgeIoPciRead (
1162 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*This
,
1163 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width
,
1169 return RootBridgeIoPciAccess (This
, TRUE
, Width
, Address
, Count
, Buffer
);
1173 Allows write to PCI configuration space.
1175 @param This A pointer to EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
1176 @param Width Signifies the width of the memory operation.
1177 @param Address The address within the PCI configuration space
1178 for the PCI controller.
1179 @param Count The number of PCI configuration operations
1181 @param Buffer The source buffer to get the results.
1183 @retval EFI_SUCCESS The data was written to the PCI root bridge.
1184 @retval EFI_INVALID_PARAMETER Invalid parameters found.
1188 RootBridgeIoPciWrite (
1189 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*This
,
1190 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width
,
1196 return RootBridgeIoPciAccess (This
, FALSE
, Width
, Address
, Count
, Buffer
);
1200 Provides the PCI controller-specific address needed to access
1201 system memory for DMA.
1203 @param This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1204 @param Operation Indicate if the bus master is going to read or write
1206 @param HostAddress The system memory address to map on the PCI controller.
1207 @param NumberOfBytes On input the number of bytes to map.
1208 On output the number of bytes that were mapped.
1209 @param DeviceAddress The resulting map address for the bus master PCI
1210 controller to use to access the system memory's HostAddress.
1211 @param Mapping The value to pass to Unmap() when the bus master DMA
1212 operation is complete.
1214 @retval EFI_SUCCESS Success.
1215 @retval EFI_INVALID_PARAMETER Invalid parameters found.
1216 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
1217 @retval EFI_DEVICE_ERROR The System hardware could not map the requested address.
1218 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to lack of resources.
1223 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*This
,
1224 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_OPERATION Operation
,
1225 IN VOID
*HostAddress
,
1226 IN OUT UINTN
*NumberOfBytes
,
1227 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
1232 PCI_ROOT_BRIDGE_INSTANCE
*RootBridge
;
1233 EFI_PHYSICAL_ADDRESS PhysicalAddress
;
1236 if (HostAddress
== NULL
|| NumberOfBytes
== NULL
|| DeviceAddress
== NULL
||
1238 return EFI_INVALID_PARAMETER
;
1242 // Make sure that Operation is valid
1244 if ((UINT32
) Operation
>= EfiPciOperationMaximum
) {
1245 return EFI_INVALID_PARAMETER
;
1248 RootBridge
= ROOT_BRIDGE_FROM_THIS (This
);
1250 if (mIoMmuProtocol
!= NULL
) {
1251 if (!RootBridge
->DmaAbove4G
) {
1253 // Clear 64bit support
1255 if (Operation
> EfiPciOperationBusMasterCommonBuffer
) {
1256 Operation
= (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_OPERATION
) (Operation
- EfiPciOperationBusMasterRead64
);
1259 Status
= mIoMmuProtocol
->Map (
1261 (EDKII_IOMMU_OPERATION
) Operation
,
1270 PhysicalAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) HostAddress
;
1271 if ((!RootBridge
->DmaAbove4G
||
1272 (Operation
!= EfiPciOperationBusMasterRead64
&&
1273 Operation
!= EfiPciOperationBusMasterWrite64
&&
1274 Operation
!= EfiPciOperationBusMasterCommonBuffer64
)) &&
1275 ((PhysicalAddress
+ *NumberOfBytes
) > SIZE_4GB
)) {
1278 // If the root bridge or the device cannot handle performing DMA above
1279 // 4GB but any part of the DMA transfer being mapped is above 4GB, then
1280 // map the DMA transfer to a buffer below 4GB.
1283 if (Operation
== EfiPciOperationBusMasterCommonBuffer
||
1284 Operation
== EfiPciOperationBusMasterCommonBuffer64
) {
1286 // Common Buffer operations can not be remapped. If the common buffer
1287 // if above 4GB, then it is not possible to generate a mapping, so return
1290 return EFI_UNSUPPORTED
;
1294 // Allocate a MAP_INFO structure to remember the mapping when Unmap() is
1297 MapInfo
= AllocatePool (sizeof (MAP_INFO
));
1298 if (MapInfo
== NULL
) {
1300 return EFI_OUT_OF_RESOURCES
;
1304 // Initialize the MAP_INFO structure
1306 MapInfo
->Signature
= MAP_INFO_SIGNATURE
;
1307 MapInfo
->Operation
= Operation
;
1308 MapInfo
->NumberOfBytes
= *NumberOfBytes
;
1309 MapInfo
->NumberOfPages
= EFI_SIZE_TO_PAGES (MapInfo
->NumberOfBytes
);
1310 MapInfo
->HostAddress
= PhysicalAddress
;
1311 MapInfo
->MappedHostAddress
= SIZE_4GB
- 1;
1314 // Allocate a buffer below 4GB to map the transfer to.
1316 Status
= gBS
->AllocatePages (
1318 EfiBootServicesData
,
1319 MapInfo
->NumberOfPages
,
1320 &MapInfo
->MappedHostAddress
1322 if (EFI_ERROR (Status
)) {
1329 // If this is a read operation from the Bus Master's point of view,
1330 // then copy the contents of the real buffer into the mapped buffer
1331 // so the Bus Master can read the contents of the real buffer.
1333 if (Operation
== EfiPciOperationBusMasterRead
||
1334 Operation
== EfiPciOperationBusMasterRead64
) {
1336 (VOID
*) (UINTN
) MapInfo
->MappedHostAddress
,
1337 (VOID
*) (UINTN
) MapInfo
->HostAddress
,
1338 MapInfo
->NumberOfBytes
1342 InsertTailList (&RootBridge
->Maps
, &MapInfo
->Link
);
1345 // The DeviceAddress is the address of the maped buffer below 4GB
1347 *DeviceAddress
= MapInfo
->MappedHostAddress
;
1349 // Return a pointer to the MAP_INFO structure in Mapping
1354 // If the root bridge CAN handle performing DMA above 4GB or
1355 // the transfer is below 4GB, so the DeviceAddress is simply the
1358 *DeviceAddress
= PhysicalAddress
;
1359 *Mapping
= NO_MAPPING
;
1366 Completes the Map() operation and releases any corresponding resources.
1368 The Unmap() function completes the Map() operation and releases any
1369 corresponding resources.
1370 If the operation was an EfiPciOperationBusMasterWrite or
1371 EfiPciOperationBusMasterWrite64, the data is committed to the target system
1373 Any resources used for the mapping are freed.
1375 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1376 @param[in] Mapping The mapping value returned from Map().
1378 @retval EFI_SUCCESS The range was unmapped.
1379 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by Map().
1380 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
1385 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*This
,
1391 PCI_ROOT_BRIDGE_INSTANCE
*RootBridge
;
1394 if (mIoMmuProtocol
!= NULL
) {
1395 Status
= mIoMmuProtocol
->Unmap (
1402 RootBridge
= ROOT_BRIDGE_FROM_THIS (This
);
1405 // See if the Map() operation associated with this Unmap() required a mapping
1406 // buffer. If a mapping buffer was not required, then this function simply
1407 // returns EFI_SUCCESS.
1409 if (Mapping
== NO_MAPPING
) {
1413 MapInfo
= NO_MAPPING
;
1414 for (Link
= GetFirstNode (&RootBridge
->Maps
)
1415 ; !IsNull (&RootBridge
->Maps
, Link
)
1416 ; Link
= GetNextNode (&RootBridge
->Maps
, Link
)
1418 MapInfo
= MAP_INFO_FROM_LINK (Link
);
1419 if (MapInfo
== Mapping
) {
1424 // Mapping is not a valid value returned by Map()
1426 if (MapInfo
!= Mapping
) {
1427 return EFI_INVALID_PARAMETER
;
1429 RemoveEntryList (&MapInfo
->Link
);
1432 // If this is a write operation from the Bus Master's point of view,
1433 // then copy the contents of the mapped buffer into the real buffer
1434 // so the processor can read the contents of the real buffer.
1436 if (MapInfo
->Operation
== EfiPciOperationBusMasterWrite
||
1437 MapInfo
->Operation
== EfiPciOperationBusMasterWrite64
) {
1439 (VOID
*) (UINTN
) MapInfo
->HostAddress
,
1440 (VOID
*) (UINTN
) MapInfo
->MappedHostAddress
,
1441 MapInfo
->NumberOfBytes
1446 // Free the mapped buffer and the MAP_INFO structure.
1448 gBS
->FreePages (MapInfo
->MappedHostAddress
, MapInfo
->NumberOfPages
);
1454 Allocates pages that are suitable for an EfiPciOperationBusMasterCommonBuffer
1455 or EfiPciOperationBusMasterCommonBuffer64 mapping.
1457 @param This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1458 @param Type This parameter is not used and must be ignored.
1459 @param MemoryType The type of memory to allocate, EfiBootServicesData or
1460 EfiRuntimeServicesData.
1461 @param Pages The number of pages to allocate.
1462 @param HostAddress A pointer to store the base system memory address of the
1464 @param Attributes The requested bit mask of attributes for the allocated
1465 range. Only the attributes
1466 EFI_PCI_ATTRIBUTE_MEMORY_WRITE_COMBINE,
1467 EFI_PCI_ATTRIBUTE_MEMORY_CACHED, and
1468 EFI_PCI_ATTRIBUTE_DUAL_ADDRESS_CYCLE may be used with this
1471 @retval EFI_SUCCESS The requested memory pages were allocated.
1472 @retval EFI_INVALID_PARAMETER MemoryType is invalid.
1473 @retval EFI_INVALID_PARAMETER HostAddress is NULL.
1474 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal
1475 attribute bits are MEMORY_WRITE_COMBINE,
1476 MEMORY_CACHED, and DUAL_ADDRESS_CYCLE.
1477 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
1481 RootBridgeIoAllocateBuffer (
1482 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*This
,
1483 IN EFI_ALLOCATE_TYPE Type
,
1484 IN EFI_MEMORY_TYPE MemoryType
,
1486 OUT VOID
**HostAddress
,
1487 IN UINT64 Attributes
1491 EFI_PHYSICAL_ADDRESS PhysicalAddress
;
1492 PCI_ROOT_BRIDGE_INSTANCE
*RootBridge
;
1493 EFI_ALLOCATE_TYPE AllocateType
;
1496 // Validate Attributes
1498 if ((Attributes
& EFI_PCI_ATTRIBUTE_INVALID_FOR_ALLOCATE_BUFFER
) != 0) {
1499 return EFI_UNSUPPORTED
;
1503 // Check for invalid inputs
1505 if (HostAddress
== NULL
) {
1506 return EFI_INVALID_PARAMETER
;
1510 // The only valid memory types are EfiBootServicesData and
1511 // EfiRuntimeServicesData
1513 if (MemoryType
!= EfiBootServicesData
&&
1514 MemoryType
!= EfiRuntimeServicesData
) {
1515 return EFI_INVALID_PARAMETER
;
1518 RootBridge
= ROOT_BRIDGE_FROM_THIS (This
);
1520 if (mIoMmuProtocol
!= NULL
) {
1521 if (!RootBridge
->DmaAbove4G
) {
1523 // Clear DUAL_ADDRESS_CYCLE
1525 Attributes
&= ~((UINT64
) EFI_PCI_ATTRIBUTE_DUAL_ADDRESS_CYCLE
);
1527 Status
= mIoMmuProtocol
->AllocateBuffer (
1538 AllocateType
= AllocateAnyPages
;
1539 if (!RootBridge
->DmaAbove4G
||
1540 (Attributes
& EFI_PCI_ATTRIBUTE_DUAL_ADDRESS_CYCLE
) == 0) {
1542 // Limit allocations to memory below 4GB
1544 AllocateType
= AllocateMaxAddress
;
1545 PhysicalAddress
= (EFI_PHYSICAL_ADDRESS
) (SIZE_4GB
- 1);
1547 Status
= gBS
->AllocatePages (
1553 if (!EFI_ERROR (Status
)) {
1554 *HostAddress
= (VOID
*) (UINTN
) PhysicalAddress
;
1561 Frees memory that was allocated with AllocateBuffer().
1563 The FreeBuffer() function frees memory that was allocated with
1566 @param This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1567 @param Pages The number of pages to free.
1568 @param HostAddress The base system memory address of the allocated range.
1570 @retval EFI_SUCCESS The requested memory pages were freed.
1571 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and
1572 Pages was not allocated with AllocateBuffer().
1576 RootBridgeIoFreeBuffer (
1577 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*This
,
1579 OUT VOID
*HostAddress
1584 if (mIoMmuProtocol
!= NULL
) {
1585 Status
= mIoMmuProtocol
->FreeBuffer (
1593 return gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) HostAddress
, Pages
);
1597 Flushes all PCI posted write transactions from a PCI host bridge to system
1600 The Flush() function flushes any PCI posted write transactions from a PCI
1601 host bridge to system memory. Posted write transactions are generated by PCI
1602 bus masters when they perform write transactions to target addresses in
1604 This function does not flush posted write transactions from any PCI bridges.
1605 A PCI controller specific action must be taken to guarantee that the posted
1606 write transactions have been flushed from the PCI controller and from all the
1607 PCI bridges into the PCI host bridge. This is typically done with a PCI read
1608 transaction from the PCI controller prior to calling Flush().
1610 @param This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1612 @retval EFI_SUCCESS The PCI posted write transactions were flushed
1613 from the PCI host bridge to system memory.
1614 @retval EFI_DEVICE_ERROR The PCI posted write transactions were not flushed
1615 from the PCI host bridge due to a hardware error.
1620 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*This
1627 Gets the attributes that a PCI root bridge supports setting with
1628 SetAttributes(), and the attributes that a PCI root bridge is currently
1631 The GetAttributes() function returns the mask of attributes that this PCI
1632 root bridge supports and the mask of attributes that the PCI root bridge is
1635 @param This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1636 @param Supported A pointer to the mask of attributes that this PCI root
1637 bridge supports setting with SetAttributes().
1638 @param Attributes A pointer to the mask of attributes that this PCI root
1639 bridge is currently using.
1641 @retval EFI_SUCCESS If Supports is not NULL, then the attributes
1642 that the PCI root bridge supports is returned
1643 in Supports. If Attributes is not NULL, then
1644 the attributes that the PCI root bridge is
1645 currently using is returned in Attributes.
1646 @retval EFI_INVALID_PARAMETER Both Supports and Attributes are NULL.
1650 RootBridgeIoGetAttributes (
1651 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*This
,
1652 OUT UINT64
*Supported
,
1653 OUT UINT64
*Attributes
1656 PCI_ROOT_BRIDGE_INSTANCE
*RootBridge
;
1658 if (Attributes
== NULL
&& Supported
== NULL
) {
1659 return EFI_INVALID_PARAMETER
;
1662 RootBridge
= ROOT_BRIDGE_FROM_THIS (This
);
1664 // Set the return value for Supported and Attributes
1666 if (Supported
!= NULL
) {
1667 *Supported
= RootBridge
->Supports
;
1670 if (Attributes
!= NULL
) {
1671 *Attributes
= RootBridge
->Attributes
;
1678 Sets attributes for a resource range on a PCI root bridge.
1680 The SetAttributes() function sets the attributes specified in Attributes for
1681 the PCI root bridge on the resource range specified by ResourceBase and
1682 ResourceLength. Since the granularity of setting these attributes may vary
1683 from resource type to resource type, and from platform to platform, the
1684 actual resource range and the one passed in by the caller may differ. As a
1685 result, this function may set the attributes specified by Attributes on a
1686 larger resource range than the caller requested. The actual range is returned
1687 in ResourceBase and ResourceLength. The caller is responsible for verifying
1688 that the actual range for which the attributes were set is acceptable.
1690 @param This A pointer to the
1691 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1692 @param Attributes The mask of attributes to set. If the
1693 attribute bit MEMORY_WRITE_COMBINE,
1694 MEMORY_CACHED, or MEMORY_DISABLE is set,
1695 then the resource range is specified by
1696 ResourceBase and ResourceLength. If
1697 MEMORY_WRITE_COMBINE, MEMORY_CACHED, and
1698 MEMORY_DISABLE are not set, then
1699 ResourceBase and ResourceLength are ignored,
1701 @param ResourceBase A pointer to the base address of the
1702 resource range to be modified by the
1703 attributes specified by Attributes.
1704 @param ResourceLength A pointer to the length of the resource
1705 range to be modified by the attributes
1706 specified by Attributes.
1708 @retval EFI_SUCCESS The current configuration of this PCI root bridge
1709 was returned in Resources.
1710 @retval EFI_UNSUPPORTED The current configuration of this PCI root bridge
1711 could not be retrieved.
1715 RootBridgeIoSetAttributes (
1716 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*This
,
1717 IN UINT64 Attributes
,
1718 IN OUT UINT64
*ResourceBase
,
1719 IN OUT UINT64
*ResourceLength
1722 PCI_ROOT_BRIDGE_INSTANCE
*RootBridge
;
1724 RootBridge
= ROOT_BRIDGE_FROM_THIS (This
);
1726 if ((Attributes
& (~RootBridge
->Supports
)) != 0) {
1727 return EFI_UNSUPPORTED
;
1730 RootBridge
->Attributes
= Attributes
;
1735 Retrieves the current resource settings of this PCI root bridge in the form
1736 of a set of ACPI resource descriptors.
1738 There are only two resource descriptor types from the ACPI Specification that
1739 may be used to describe the current resources allocated to a PCI root bridge.
1740 These are the QWORD Address Space Descriptor, and the End Tag. The QWORD
1741 Address Space Descriptor can describe memory, I/O, and bus number ranges for
1742 dynamic or fixed resources. The configuration of a PCI root bridge is described
1743 with one or more QWORD Address Space Descriptors followed by an End Tag.
1745 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1746 @param[out] Resources A pointer to the resource descriptors that
1747 describe the current configuration of this PCI root
1748 bridge. The storage for the resource
1749 descriptors is allocated by this function. The
1750 caller must treat the return buffer as read-only
1751 data, and the buffer must not be freed by the
1754 @retval EFI_SUCCESS The current configuration of this PCI root bridge
1755 was returned in Resources.
1756 @retval EFI_UNSUPPORTED The current configuration of this PCI root bridge
1757 could not be retrieved.
1761 RootBridgeIoConfiguration (
1762 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*This
,
1763 OUT VOID
**Resources
1766 PCI_RESOURCE_TYPE Index
;
1767 PCI_ROOT_BRIDGE_INSTANCE
*RootBridge
;
1768 PCI_RES_NODE
*ResAllocNode
;
1769 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptor
;
1770 EFI_ACPI_END_TAG_DESCRIPTOR
*End
;
1773 // Get this instance of the Root Bridge.
1775 RootBridge
= ROOT_BRIDGE_FROM_THIS (This
);
1777 RootBridge
->ConfigBuffer
,
1778 TypeMax
* sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) + sizeof (EFI_ACPI_END_TAG_DESCRIPTOR
)
1780 Descriptor
= RootBridge
->ConfigBuffer
;
1781 for (Index
= TypeIo
; Index
< TypeMax
; Index
++) {
1783 ResAllocNode
= &RootBridge
->ResAllocNode
[Index
];
1785 if (ResAllocNode
->Status
!= ResAllocated
) {
1789 Descriptor
->Desc
= ACPI_ADDRESS_SPACE_DESCRIPTOR
;
1790 Descriptor
->Len
= sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) - 3;
1791 // According to UEFI 2.7, RootBridgeIo->Configuration should return address
1792 // range in CPU view (host address), and ResAllocNode->Base is already a CPU
1793 // view address (host address).
1794 Descriptor
->AddrRangeMin
= ResAllocNode
->Base
;
1795 Descriptor
->AddrRangeMax
= ResAllocNode
->Base
+ ResAllocNode
->Length
- 1;
1796 Descriptor
->AddrLen
= ResAllocNode
->Length
;
1797 Descriptor
->AddrTranslationOffset
= GetTranslationByResourceType (
1802 switch (ResAllocNode
->Type
) {
1805 Descriptor
->ResType
= ACPI_ADDRESS_SPACE_TYPE_IO
;
1809 Descriptor
->SpecificFlag
= EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE
;
1811 Descriptor
->ResType
= ACPI_ADDRESS_SPACE_TYPE_MEM
;
1812 Descriptor
->AddrSpaceGranularity
= 32;
1816 Descriptor
->SpecificFlag
= EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE
;
1818 Descriptor
->ResType
= ACPI_ADDRESS_SPACE_TYPE_MEM
;
1819 Descriptor
->AddrSpaceGranularity
= 64;
1823 Descriptor
->ResType
= ACPI_ADDRESS_SPACE_TYPE_BUS
;
1833 // Terminate the entries.
1835 End
= (EFI_ACPI_END_TAG_DESCRIPTOR
*) Descriptor
;
1836 End
->Desc
= ACPI_END_TAG_DESCRIPTOR
;
1837 End
->Checksum
= 0x0;
1839 *Resources
= RootBridge
->ConfigBuffer
;