]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Pci/PciHostBridgeDxe/PciRootBridgeIo.c
2c373e41de4d24a92ad9e9def016505a704a40d6
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / PciHostBridgeDxe / PciRootBridgeIo.c
1 /** @file
2
3 PCI Root Bridge Io Protocol code.
4
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
10
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.
13
14 **/
15
16 #include "PciHostBridge.h"
17 #include "PciRootBridge.h"
18 #include "PciHostResource.h"
19
20 extern EDKII_IOMMU_PROTOCOL *mIoMmuProtocol;
21
22 #define NO_MAPPING (VOID *) (UINTN) -1
23
24 //
25 // Lookup table for increment values based on transfer widths
26 //
27 UINT8 mInStride[] = {
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
40 };
41
42 //
43 // Lookup table for increment values based on transfer widths
44 //
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
58 };
59
60 /**
61 Construct the Pci Root Bridge instance.
62
63 @param Bridge The root bridge instance.
64
65 @return The pointer to PCI_ROOT_BRIDGE_INSTANCE just created
66 or NULL if creation fails.
67 **/
68 PCI_ROOT_BRIDGE_INSTANCE *
69 CreateRootBridge (
70 IN PCI_ROOT_BRIDGE *Bridge
71 )
72 {
73 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;
74 PCI_RESOURCE_TYPE Index;
75 CHAR16 *DevicePathStr;
76 PCI_ROOT_BRIDGE_APERTURE *Aperture;
77
78 DevicePathStr = NULL;
79
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""
88 ));
89 DEBUG ((
90 EFI_D_INFO, " Bus: %lx - %lx Translation=%lx\n",
91 Bridge->Bus.Base, Bridge->Bus.Limit, Bridge->Bus.Translation
92 ));
93 //
94 // Translation for bus is not supported.
95 //
96 ASSERT (Bridge->Bus.Translation == 0);
97 if (Bridge->Bus.Translation != 0) {
98 return NULL;
99 }
100
101 DEBUG ((
102 DEBUG_INFO, " Io: %lx - %lx Translation=%lx\n",
103 Bridge->Io.Base, Bridge->Io.Limit, Bridge->Io.Translation
104 ));
105 DEBUG ((
106 DEBUG_INFO, " Mem: %lx - %lx Translation=%lx\n",
107 Bridge->Mem.Base, Bridge->Mem.Limit, Bridge->Mem.Translation
108 ));
109 DEBUG ((
110 DEBUG_INFO, " MemAbove4G: %lx - %lx Translation=%lx\n",
111 Bridge->MemAbove4G.Base, Bridge->MemAbove4G.Limit, Bridge->MemAbove4G.Translation
112 ));
113 DEBUG ((
114 DEBUG_INFO, " PMem: %lx - %lx Translation=%lx\n",
115 Bridge->PMem.Base, Bridge->PMem.Limit, Bridge->PMem.Translation
116 ));
117 DEBUG ((
118 DEBUG_INFO, " PMemAbove4G: %lx - %lx Translation=%lx\n",
119 Bridge->PMemAbove4G.Base, Bridge->PMemAbove4G.Limit, Bridge->PMemAbove4G.Translation
120 ));
121
122 //
123 // Make sure Mem and MemAbove4G apertures are valid
124 //
125 if (Bridge->Mem.Base <= Bridge->Mem.Limit) {
126 ASSERT (Bridge->Mem.Limit < SIZE_4GB);
127 if (Bridge->Mem.Limit >= SIZE_4GB) {
128 return NULL;
129 }
130 }
131 if (Bridge->MemAbove4G.Base <= Bridge->MemAbove4G.Limit) {
132 ASSERT (Bridge->MemAbove4G.Base >= SIZE_4GB);
133 if (Bridge->MemAbove4G.Base < SIZE_4GB) {
134 return NULL;
135 }
136 }
137 if (Bridge->PMem.Base <= Bridge->PMem.Limit) {
138 ASSERT (Bridge->PMem.Limit < SIZE_4GB);
139 if (Bridge->PMem.Limit >= SIZE_4GB) {
140 return NULL;
141 }
142 }
143 if (Bridge->PMemAbove4G.Base <= Bridge->PMemAbove4G.Limit) {
144 ASSERT (Bridge->PMemAbove4G.Base >= SIZE_4GB);
145 if (Bridge->PMemAbove4G.Base < SIZE_4GB) {
146 return NULL;
147 }
148 }
149
150 //
151 // Ignore AllocationAttributes when resources were already assigned.
152 //
153 if (!Bridge->ResourceAssigned) {
154 if ((Bridge->AllocationAttributes & EFI_PCI_HOST_BRIDGE_COMBINE_MEM_PMEM) != 0) {
155 //
156 // If this bit is set, then the PCI Root Bridge does not
157 // support separate windows for Non-prefetchable and Prefetchable
158 // memory.
159 //
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)
164 ) {
165 return NULL;
166 }
167 }
168
169 if ((Bridge->AllocationAttributes & EFI_PCI_HOST_BRIDGE_MEM64_DECODE) == 0) {
170 //
171 // If this bit is not set, then the PCI Root Bridge does not support
172 // 64 bit memory windows.
173 //
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)
178 ) {
179 return NULL;
180 }
181 }
182 }
183
184 RootBridge = AllocateZeroPool (sizeof (PCI_ROOT_BRIDGE_INSTANCE));
185 ASSERT (RootBridge != NULL);
186
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)
197 );
198 ASSERT (RootBridge->ConfigBuffer != NULL);
199 InitializeListHead (&RootBridge->Maps);
200
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));
207
208 for (Index = TypeIo; Index < TypeMax; Index++) {
209 switch (Index) {
210 case TypeBus:
211 Aperture = &RootBridge->Bus;
212 break;
213 case TypeIo:
214 Aperture = &RootBridge->Io;
215 break;
216 case TypeMem32:
217 Aperture = &RootBridge->Mem;
218 break;
219 case TypeMem64:
220 Aperture = &RootBridge->MemAbove4G;
221 break;
222 case TypePMem32:
223 Aperture = &RootBridge->PMem;
224 break;
225 case TypePMem64:
226 Aperture = &RootBridge->PMemAbove4G;
227 break;
228 default:
229 ASSERT (FALSE);
230 Aperture = NULL;
231 break;
232 }
233 RootBridge->ResAllocNode[Index].Type = Index;
234 if (Bridge->ResourceAssigned && (Aperture->Limit >= Aperture->Base)) {
235 //
236 // Base in ResAllocNode is a host address, while Base in Aperture is a
237 // device address.
238 //
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;
243 } else {
244 RootBridge->ResAllocNode[Index].Base = 0;
245 RootBridge->ResAllocNode[Index].Length = 0;
246 RootBridge->ResAllocNode[Index].Status = ResNone;
247 }
248 }
249
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;
268
269 return RootBridge;
270 }
271
272 /**
273 Check parameters for IO,MMIO,PCI read/write services of PCI Root Bridge IO.
274
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.
280
281 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
282
283 @param[in] OperationType I/O operation type: IO/MMIO/PCI.
284
285 @param[in] Width Signifies the width of the I/O or Memory operation.
286
287 @param[in] Address The base address of the I/O operation.
288
289 @param[in] Count The number of I/O operations to perform. The number
290 of bytes moved is Width size * Count, starting at
291 Address.
292
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.
296
297 @retval EFI_SUCCESS The parameters for this request pass the
298 checks.
299
300 @retval EFI_INVALID_PARAMETER Width is invalid for this PI system.
301
302 @retval EFI_INVALID_PARAMETER Buffer is NULL.
303
304 @retval EFI_INVALID_PARAMETER Address or Count is invalid.
305
306 @retval EFI_UNSUPPORTED The Buffer is not aligned for the given Width.
307
308 @retval EFI_UNSUPPORTED The address range specified by Address, Width,
309 and Count is not valid for this PI system.
310 **/
311 EFI_STATUS
312 RootBridgeIoCheckParameter (
313 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
314 IN OPERATION_TYPE OperationType,
315 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
316 IN UINT64 Address,
317 IN UINTN Count,
318 IN VOID *Buffer
319 )
320 {
321 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;
322 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS *PciRbAddr;
323 UINT64 Base;
324 UINT64 Limit;
325 UINT32 Size;
326 UINT64 Length;
327
328 //
329 // Check to see if Buffer is NULL
330 //
331 if (Buffer == NULL) {
332 return EFI_INVALID_PARAMETER;
333 }
334
335 //
336 // Check to see if Width is in the valid range
337 //
338 if ((UINT32) Width >= EfiPciWidthMaximum) {
339 return EFI_INVALID_PARAMETER;
340 }
341
342 //
343 // For FIFO type, the device address won't increase during the access,
344 // so treat Count as 1
345 //
346 if (Width >= EfiPciWidthFifoUint8 && Width <= EfiPciWidthFifoUint64) {
347 Count = 1;
348 }
349
350 Width = (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH) (Width & 0x03);
351 Size = 1 << Width;
352
353 //
354 // Make sure (Count * Size) doesn't exceed MAX_UINT64
355 //
356 if (Count > DivU64x32 (MAX_UINT64, Size)) {
357 return EFI_INVALID_PARAMETER;
358 }
359
360 //
361 // Check to see if Address is aligned
362 //
363 if ((Address & (Size - 1)) != 0) {
364 return EFI_UNSUPPORTED;
365 }
366
367 //
368 // Make sure (Address + Count * Size) doesn't exceed MAX_UINT64
369 //
370 Length = MultU64x32 (Count, Size);
371 if (Address > MAX_UINT64 - Length) {
372 return EFI_INVALID_PARAMETER;
373 }
374
375 RootBridge = ROOT_BRIDGE_FROM_THIS (This);
376
377 //
378 // Check to see if any address associated with this transfer exceeds the
379 // maximum allowed address. The maximum address implied by the parameters
380 // passed in is Address + Size * Count. If the following condition is met,
381 // then the transfer is not supported.
382 //
383 // Address + Size * Count > Limit + 1
384 //
385 // Since Limit can be the maximum integer value supported by the CPU and
386 // Count can also be the maximum integer value supported by the CPU, this
387 // range check must be adjusted to avoid all oveflow conditions.
388 //
389 if (OperationType == IoOperation) {
390 //
391 // Allow Legacy IO access
392 //
393 if (Address + Length <= 0x1000) {
394 if ((RootBridge->Attributes & (
395 EFI_PCI_ATTRIBUTE_ISA_IO | EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO | EFI_PCI_ATTRIBUTE_VGA_IO |
396 EFI_PCI_ATTRIBUTE_IDE_PRIMARY_IO | EFI_PCI_ATTRIBUTE_IDE_SECONDARY_IO |
397 EFI_PCI_ATTRIBUTE_ISA_IO_16 | EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO_16 | EFI_PCI_ATTRIBUTE_VGA_IO_16)) != 0) {
398 return EFI_SUCCESS;
399 }
400 }
401 Base = RootBridge->Io.Base;
402 Limit = RootBridge->Io.Limit;
403 } else if (OperationType == MemOperation) {
404 //
405 // Allow Legacy MMIO access
406 //
407 if ((Address >= 0xA0000) && (Address + Length) <= 0xC0000) {
408 if ((RootBridge->Attributes & EFI_PCI_ATTRIBUTE_VGA_MEMORY) != 0) {
409 return EFI_SUCCESS;
410 }
411 }
412 //
413 // By comparing the Address against Limit we know which range to be used
414 // for checking
415 //
416 if (Address + Length <= RootBridge->Mem.Limit + 1) {
417 Base = RootBridge->Mem.Base;
418 Limit = RootBridge->Mem.Limit;
419 } else {
420 Base = RootBridge->MemAbove4G.Base;
421 Limit = RootBridge->MemAbove4G.Limit;
422 }
423 } else {
424 PciRbAddr = (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS*) &Address;
425 if (PciRbAddr->Bus < RootBridge->Bus.Base ||
426 PciRbAddr->Bus > RootBridge->Bus.Limit) {
427 return EFI_INVALID_PARAMETER;
428 }
429
430 if (PciRbAddr->Device > PCI_MAX_DEVICE ||
431 PciRbAddr->Function > PCI_MAX_FUNC) {
432 return EFI_INVALID_PARAMETER;
433 }
434
435 if (PciRbAddr->ExtendedRegister != 0) {
436 Address = PciRbAddr->ExtendedRegister;
437 } else {
438 Address = PciRbAddr->Register;
439 }
440 Base = 0;
441 Limit = RootBridge->NoExtendedConfigSpace ? 0xFF : 0xFFF;
442 }
443
444 if (Address < Base) {
445 return EFI_INVALID_PARAMETER;
446 }
447
448 if (Address + Length > Limit + 1) {
449 return EFI_INVALID_PARAMETER;
450 }
451
452 return EFI_SUCCESS;
453 }
454
455 /**
456 Use address to match apertures of memory type and then get the corresponding
457 translation.
458
459 @param RootBridge The root bridge instance.
460 @param Address The address used to match aperture.
461 @param Translation Pointer containing the output translation.
462
463 @return EFI_SUCCESS Get translation successfully.
464 @return EFI_INVALID_PARAMETER No matched memory aperture; the input Address
465 must be invalid.
466 **/
467 EFI_STATUS
468 RootBridgeIoGetMemTranslationByAddress (
469 IN PCI_ROOT_BRIDGE_INSTANCE *RootBridge,
470 IN UINT64 Address,
471 IN OUT UINT64 *Translation
472 )
473 {
474 if (Address >= RootBridge->Mem.Base && Address <= RootBridge->Mem.Limit) {
475 *Translation = RootBridge->Mem.Translation;
476 } else if (Address >= RootBridge->PMem.Base && Address <= RootBridge->PMem.Limit) {
477 *Translation = RootBridge->PMem.Translation;
478 } else if (Address >= RootBridge->MemAbove4G.Base && Address <= RootBridge->MemAbove4G.Limit) {
479 *Translation = RootBridge->MemAbove4G.Translation;
480 } else if (Address >= RootBridge->PMemAbove4G.Base && Address <= RootBridge->PMemAbove4G.Limit) {
481 *Translation = RootBridge->PMemAbove4G.Translation;
482 } else {
483 return EFI_INVALID_PARAMETER;
484 }
485
486 return EFI_SUCCESS;
487 }
488
489 /**
490 Return the result of (Multiplicand * Multiplier / Divisor).
491
492 @param Multiplicand A 64-bit unsigned value.
493 @param Multiplier A 64-bit unsigned value.
494 @param Divisor A 32-bit unsigned value.
495 @param Remainder A pointer to a 32-bit unsigned value. This parameter is
496 optional and may be NULL.
497
498 @return Multiplicand * Multiplier / Divisor.
499 **/
500 UINT64
501 MultThenDivU64x64x32 (
502 IN UINT64 Multiplicand,
503 IN UINT64 Multiplier,
504 IN UINT32 Divisor,
505 OUT UINT32 *Remainder OPTIONAL
506 )
507 {
508 UINT64 Uint64;
509 UINT32 LocalRemainder;
510 UINT32 Uint32;
511 if (Multiplicand > DivU64x64Remainder (MAX_UINT64, Multiplier, NULL)) {
512 //
513 // Make sure Multiplicand is the bigger one.
514 //
515 if (Multiplicand < Multiplier) {
516 Uint64 = Multiplicand;
517 Multiplicand = Multiplier;
518 Multiplier = Uint64;
519 }
520 //
521 // Because Multiplicand * Multiplier overflows,
522 // Multiplicand * Multiplier / Divisor
523 // = (2 * Multiplicand' + 1) * Multiplier / Divisor
524 // = 2 * (Multiplicand' * Multiplier / Divisor) + Multiplier / Divisor
525 //
526 Uint64 = MultThenDivU64x64x32 (RShiftU64 (Multiplicand, 1), Multiplier, Divisor, &LocalRemainder);
527 Uint64 = LShiftU64 (Uint64, 1);
528 Uint32 = 0;
529 if ((Multiplicand & 0x1) == 1) {
530 Uint64 += DivU64x32Remainder (Multiplier, Divisor, &Uint32);
531 }
532 return Uint64 + DivU64x32Remainder (Uint32 + LShiftU64 (LocalRemainder, 1), Divisor, Remainder);
533 } else {
534 return DivU64x32Remainder (MultU64x64 (Multiplicand, Multiplier), Divisor, Remainder);
535 }
536 }
537
538 /**
539 Return the elapsed tick count from CurrentTick.
540
541 @param CurrentTick On input, the previous tick count.
542 On output, the current tick count.
543 @param StartTick The value the performance counter starts with when it
544 rolls over.
545 @param EndTick The value that the performance counter ends with before
546 it rolls over.
547
548 @return The elapsed tick count from CurrentTick.
549 **/
550 UINT64
551 GetElapsedTick (
552 UINT64 *CurrentTick,
553 UINT64 StartTick,
554 UINT64 EndTick
555 )
556 {
557 UINT64 PreviousTick;
558
559 PreviousTick = *CurrentTick;
560 *CurrentTick = GetPerformanceCounter();
561 if (StartTick < EndTick) {
562 return *CurrentTick - PreviousTick;
563 } else {
564 return PreviousTick - *CurrentTick;
565 }
566 }
567
568 /**
569 Polls an address in memory mapped I/O space until an exit condition is met,
570 or a timeout occurs.
571
572 This function provides a standard way to poll a PCI memory location. A PCI
573 memory read operation is performed at the PCI memory address specified by
574 Address for the width specified by Width. The result of this PCI memory read
575 operation is stored in Result. This PCI memory read operation is repeated
576 until either a timeout of Delay 100 ns units has expired, or (Result & Mask)
577 is equal to Value.
578
579 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
580 @param[in] Width Signifies the width of the memory operations.
581 @param[in] Address The base address of the memory operations. The caller
582 is responsible for aligning Address if required.
583 @param[in] Mask Mask used for the polling criteria. Bytes above Width
584 in Mask are ignored. The bits in the bytes below Width
585 which are zero in Mask are ignored when polling the
586 memory address.
587 @param[in] Value The comparison value used for the polling exit
588 criteria.
589 @param[in] Delay The number of 100 ns units to poll. Note that timer
590 available may be of poorer granularity.
591 @param[out] Result Pointer to the last value read from the memory
592 location.
593
594 @retval EFI_SUCCESS The last data returned from the access matched
595 the poll exit criteria.
596 @retval EFI_INVALID_PARAMETER Width is invalid.
597 @retval EFI_INVALID_PARAMETER Result is NULL.
598 @retval EFI_TIMEOUT Delay expired before a match occurred.
599 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
600 lack of resources.
601 **/
602 EFI_STATUS
603 EFIAPI
604 RootBridgeIoPollMem (
605 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
606 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
607 IN UINT64 Address,
608 IN UINT64 Mask,
609 IN UINT64 Value,
610 IN UINT64 Delay,
611 OUT UINT64 *Result
612 )
613 {
614 EFI_STATUS Status;
615 UINT64 NumberOfTicks;
616 UINT32 Remainder;
617 UINT64 StartTick;
618 UINT64 EndTick;
619 UINT64 CurrentTick;
620 UINT64 ElapsedTick;
621 UINT64 Frequency;
622
623 if (Result == NULL) {
624 return EFI_INVALID_PARAMETER;
625 }
626
627 if ((UINT32)Width > EfiPciWidthUint64) {
628 return EFI_INVALID_PARAMETER;
629 }
630
631 //
632 // No matter what, always do a single poll.
633 //
634 Status = This->Mem.Read (This, Width, Address, 1, Result);
635 if (EFI_ERROR (Status)) {
636 return Status;
637 }
638
639 if ((*Result & Mask) == Value) {
640 return EFI_SUCCESS;
641 }
642
643 if (Delay == 0) {
644 return EFI_SUCCESS;
645
646 } else {
647 //
648 // NumberOfTicks = Frenquency * Delay / EFI_TIMER_PERIOD_SECONDS(1)
649 //
650 Frequency = GetPerformanceCounterProperties (&StartTick, &EndTick);
651 NumberOfTicks = MultThenDivU64x64x32 (Frequency, Delay, (UINT32)EFI_TIMER_PERIOD_SECONDS(1), &Remainder);
652 if (Remainder >= (UINTN)EFI_TIMER_PERIOD_SECONDS(1) / 2) {
653 NumberOfTicks++;
654 }
655 for ( ElapsedTick = 0, CurrentTick = GetPerformanceCounter()
656 ; ElapsedTick <= NumberOfTicks
657 ; ElapsedTick += GetElapsedTick (&CurrentTick, StartTick, EndTick)
658 ) {
659 Status = This->Mem.Read (This, Width, Address, 1, Result);
660 if (EFI_ERROR (Status)) {
661 return Status;
662 }
663
664 if ((*Result & Mask) == Value) {
665 return EFI_SUCCESS;
666 }
667 }
668 }
669 return EFI_TIMEOUT;
670 }
671
672 /**
673 Reads from the I/O space of a PCI Root Bridge. Returns when either the
674 polling exit criteria is satisfied or after a defined duration.
675
676 This function provides a standard way to poll a PCI I/O location. A PCI I/O
677 read operation is performed at the PCI I/O address specified by Address for
678 the width specified by Width.
679 The result of this PCI I/O read operation is stored in Result. This PCI I/O
680 read operation is repeated until either a timeout of Delay 100 ns units has
681 expired, or (Result & Mask) is equal to Value.
682
683 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
684 @param[in] Width Signifies the width of the I/O operations.
685 @param[in] Address The base address of the I/O operations. The caller is
686 responsible for aligning Address if required.
687 @param[in] Mask Mask used for the polling criteria. Bytes above Width in
688 Mask are ignored. The bits in the bytes below Width
689 which are zero in Mask are ignored when polling the I/O
690 address.
691 @param[in] Value The comparison value used for the polling exit criteria.
692 @param[in] Delay The number of 100 ns units to poll. Note that timer
693 available may be of poorer granularity.
694 @param[out] Result Pointer to the last value read from the memory location.
695
696 @retval EFI_SUCCESS The last data returned from the access matched
697 the poll exit criteria.
698 @retval EFI_INVALID_PARAMETER Width is invalid.
699 @retval EFI_INVALID_PARAMETER Result is NULL.
700 @retval EFI_TIMEOUT Delay expired before a match occurred.
701 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
702 lack of resources.
703 **/
704 EFI_STATUS
705 EFIAPI
706 RootBridgeIoPollIo (
707 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
708 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
709 IN UINT64 Address,
710 IN UINT64 Mask,
711 IN UINT64 Value,
712 IN UINT64 Delay,
713 OUT UINT64 *Result
714 )
715 {
716 EFI_STATUS Status;
717 UINT64 NumberOfTicks;
718 UINT32 Remainder;
719 UINT64 StartTick;
720 UINT64 EndTick;
721 UINT64 CurrentTick;
722 UINT64 ElapsedTick;
723 UINT64 Frequency;
724
725 //
726 // No matter what, always do a single poll.
727 //
728
729 if (Result == NULL) {
730 return EFI_INVALID_PARAMETER;
731 }
732
733 if ((UINT32)Width > EfiPciWidthUint64) {
734 return EFI_INVALID_PARAMETER;
735 }
736
737 Status = This->Io.Read (This, Width, Address, 1, Result);
738 if (EFI_ERROR (Status)) {
739 return Status;
740 }
741 if ((*Result & Mask) == Value) {
742 return EFI_SUCCESS;
743 }
744
745 if (Delay == 0) {
746 return EFI_SUCCESS;
747
748 } else {
749 //
750 // NumberOfTicks = Frenquency * Delay / EFI_TIMER_PERIOD_SECONDS(1)
751 //
752 Frequency = GetPerformanceCounterProperties (&StartTick, &EndTick);
753 NumberOfTicks = MultThenDivU64x64x32 (Frequency, Delay, (UINT32)EFI_TIMER_PERIOD_SECONDS(1), &Remainder);
754 if (Remainder >= (UINTN)EFI_TIMER_PERIOD_SECONDS(1) / 2) {
755 NumberOfTicks++;
756 }
757 for ( ElapsedTick = 0, CurrentTick = GetPerformanceCounter()
758 ; ElapsedTick <= NumberOfTicks
759 ; ElapsedTick += GetElapsedTick (&CurrentTick, StartTick, EndTick)
760 ) {
761 Status = This->Io.Read (This, Width, Address, 1, Result);
762 if (EFI_ERROR (Status)) {
763 return Status;
764 }
765
766 if ((*Result & Mask) == Value) {
767 return EFI_SUCCESS;
768 }
769 }
770 }
771 return EFI_TIMEOUT;
772 }
773
774 /**
775 Enables a PCI driver to access PCI controller registers in the PCI root
776 bridge memory space.
777
778 The Mem.Read(), and Mem.Write() functions enable a driver to access PCI
779 controller registers in the PCI root bridge memory space.
780 The memory operations are carried out exactly as requested. The caller is
781 responsible for satisfying any alignment and memory width restrictions that a
782 PCI Root Bridge on a platform might require.
783
784 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
785 @param[in] Width Signifies the width of the memory operation.
786 @param[in] Address The base address of the memory operation. The caller
787 is responsible for aligning the Address if required.
788 @param[in] Count The number of memory operations to perform. Bytes
789 moved is Width size * Count, starting at Address.
790 @param[out] Buffer For read operations, the destination buffer to store
791 the results. For write operations, the source buffer
792 to write data from.
793
794 @retval EFI_SUCCESS The data was read from or written to the PCI
795 root bridge.
796 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
797 @retval EFI_INVALID_PARAMETER Buffer is NULL.
798 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
799 lack of resources.
800 **/
801 EFI_STATUS
802 EFIAPI
803 RootBridgeIoMemRead (
804 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
805 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
806 IN UINT64 Address,
807 IN UINTN Count,
808 OUT VOID *Buffer
809 )
810 {
811 EFI_STATUS Status;
812 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;
813 UINT64 Translation;
814
815 Status = RootBridgeIoCheckParameter (This, MemOperation, Width, Address,
816 Count, Buffer);
817 if (EFI_ERROR (Status)) {
818 return Status;
819 }
820
821 RootBridge = ROOT_BRIDGE_FROM_THIS (This);
822 Status = RootBridgeIoGetMemTranslationByAddress (RootBridge, Address, &Translation);
823 if (EFI_ERROR (Status)) {
824 return Status;
825 }
826
827 // Address passed to CpuIo->Mem.Read needs to be a host address instead of
828 // device address.
829 return mCpuIo->Mem.Read (mCpuIo, (EFI_CPU_IO_PROTOCOL_WIDTH) Width,
830 TO_HOST_ADDRESS (Address, Translation), Count, Buffer);
831 }
832
833 /**
834 Enables a PCI driver to access PCI controller registers in the PCI root
835 bridge memory space.
836
837 The Mem.Read(), and Mem.Write() functions enable a driver to access PCI
838 controller registers in the PCI root bridge memory space.
839 The memory operations are carried out exactly as requested. The caller is
840 responsible for satisfying any alignment and memory width restrictions that a
841 PCI Root Bridge on a platform might require.
842
843 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
844 @param[in] Width Signifies the width of the memory operation.
845 @param[in] Address The base address of the memory operation. The caller
846 is responsible for aligning the Address if required.
847 @param[in] Count The number of memory operations to perform. Bytes
848 moved is Width size * Count, starting at Address.
849 @param[in] Buffer For read operations, the destination buffer to store
850 the results. For write operations, the source buffer
851 to write data from.
852
853 @retval EFI_SUCCESS The data was read from or written to the PCI
854 root bridge.
855 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
856 @retval EFI_INVALID_PARAMETER Buffer is NULL.
857 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
858 lack of resources.
859 **/
860 EFI_STATUS
861 EFIAPI
862 RootBridgeIoMemWrite (
863 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
864 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
865 IN UINT64 Address,
866 IN UINTN Count,
867 IN VOID *Buffer
868 )
869 {
870 EFI_STATUS Status;
871 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;
872 UINT64 Translation;
873
874 Status = RootBridgeIoCheckParameter (This, MemOperation, Width, Address,
875 Count, Buffer);
876 if (EFI_ERROR (Status)) {
877 return Status;
878 }
879
880 RootBridge = ROOT_BRIDGE_FROM_THIS (This);
881 Status = RootBridgeIoGetMemTranslationByAddress (RootBridge, Address, &Translation);
882 if (EFI_ERROR (Status)) {
883 return Status;
884 }
885
886 // Address passed to CpuIo->Mem.Write needs to be a host address instead of
887 // device address.
888 return mCpuIo->Mem.Write (mCpuIo, (EFI_CPU_IO_PROTOCOL_WIDTH) Width,
889 TO_HOST_ADDRESS (Address, Translation), Count, Buffer);
890 }
891
892 /**
893 Enables a PCI driver to access PCI controller registers in the PCI root
894 bridge I/O space.
895
896 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
897 @param[in] Width Signifies the width of the memory operations.
898 @param[in] Address The base address of the I/O operation. The caller is
899 responsible for aligning the Address if required.
900 @param[in] Count The number of I/O operations to perform. Bytes moved
901 is Width size * Count, starting at Address.
902 @param[out] Buffer For read operations, the destination buffer to store
903 the results. For write operations, the source buffer
904 to write data from.
905
906 @retval EFI_SUCCESS The data was read from or written to the PCI
907 root bridge.
908 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
909 @retval EFI_INVALID_PARAMETER Buffer is NULL.
910 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
911 lack of resources.
912 **/
913 EFI_STATUS
914 EFIAPI
915 RootBridgeIoIoRead (
916 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
917 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
918 IN UINT64 Address,
919 IN UINTN Count,
920 OUT VOID *Buffer
921 )
922 {
923 EFI_STATUS Status;
924 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;
925
926 Status = RootBridgeIoCheckParameter (
927 This, IoOperation, Width,
928 Address, Count, Buffer
929 );
930 if (EFI_ERROR (Status)) {
931 return Status;
932 }
933
934 RootBridge = ROOT_BRIDGE_FROM_THIS (This);
935
936 // Address passed to CpuIo->Io.Read needs to be a host address instead of
937 // device address.
938 return mCpuIo->Io.Read (mCpuIo, (EFI_CPU_IO_PROTOCOL_WIDTH) Width,
939 TO_HOST_ADDRESS (Address, RootBridge->Io.Translation), Count, Buffer);
940 }
941
942 /**
943 Enables a PCI driver to access PCI controller registers in the PCI root
944 bridge I/O space.
945
946 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
947 @param[in] Width Signifies the width of the memory operations.
948 @param[in] Address The base address of the I/O operation. The caller is
949 responsible for aligning the Address if required.
950 @param[in] Count The number of I/O operations to perform. Bytes moved
951 is Width size * Count, starting at Address.
952 @param[in] Buffer For read operations, the destination buffer to store
953 the results. For write operations, the source buffer
954 to write data from.
955
956 @retval EFI_SUCCESS The data was read from or written to the PCI
957 root bridge.
958 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
959 @retval EFI_INVALID_PARAMETER Buffer is NULL.
960 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
961 lack of resources.
962 **/
963 EFI_STATUS
964 EFIAPI
965 RootBridgeIoIoWrite (
966 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
967 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
968 IN UINT64 Address,
969 IN UINTN Count,
970 IN VOID *Buffer
971 )
972 {
973 EFI_STATUS Status;
974 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;
975
976 Status = RootBridgeIoCheckParameter (
977 This, IoOperation, Width,
978 Address, Count, Buffer
979 );
980 if (EFI_ERROR (Status)) {
981 return Status;
982 }
983
984 RootBridge = ROOT_BRIDGE_FROM_THIS (This);
985
986 // Address passed to CpuIo->Io.Write needs to be a host address instead of
987 // device address.
988 return mCpuIo->Io.Write (mCpuIo, (EFI_CPU_IO_PROTOCOL_WIDTH) Width,
989 TO_HOST_ADDRESS (Address, RootBridge->Io.Translation), Count, Buffer);
990 }
991
992 /**
993 Enables a PCI driver to copy one region of PCI root bridge memory space to
994 another region of PCI root bridge memory space.
995
996 The CopyMem() function enables a PCI driver to copy one region of PCI root
997 bridge memory space to another region of PCI root bridge memory space. This
998 is especially useful for video scroll operation on a memory mapped video
999 buffer.
1000 The memory operations are carried out exactly as requested. The caller is
1001 responsible for satisfying any alignment and memory width restrictions that a
1002 PCI root bridge on a platform might require.
1003
1004 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
1005 instance.
1006 @param[in] Width Signifies the width of the memory operations.
1007 @param[in] DestAddress The destination address of the memory operation. The
1008 caller is responsible for aligning the DestAddress if
1009 required.
1010 @param[in] SrcAddress The source address of the memory operation. The caller
1011 is responsible for aligning the SrcAddress if
1012 required.
1013 @param[in] Count The number of memory operations to perform. Bytes
1014 moved is Width size * Count, starting at DestAddress
1015 and SrcAddress.
1016
1017 @retval EFI_SUCCESS The data was copied from one memory region
1018 to another memory region.
1019 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
1020 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
1021 lack of resources.
1022 **/
1023 EFI_STATUS
1024 EFIAPI
1025 RootBridgeIoCopyMem (
1026 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
1027 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
1028 IN UINT64 DestAddress,
1029 IN UINT64 SrcAddress,
1030 IN UINTN Count
1031 )
1032 {
1033 EFI_STATUS Status;
1034 BOOLEAN Forward;
1035 UINTN Stride;
1036 UINTN Index;
1037 UINT64 Result;
1038
1039 if ((UINT32) Width > EfiPciWidthUint64) {
1040 return EFI_INVALID_PARAMETER;
1041 }
1042
1043 if (DestAddress == SrcAddress) {
1044 return EFI_SUCCESS;
1045 }
1046
1047 Stride = (UINTN) (1 << Width);
1048
1049 Forward = TRUE;
1050 if ((DestAddress > SrcAddress) &&
1051 (DestAddress < (SrcAddress + Count * Stride))) {
1052 Forward = FALSE;
1053 SrcAddress = SrcAddress + (Count - 1) * Stride;
1054 DestAddress = DestAddress + (Count - 1) * Stride;
1055 }
1056
1057 for (Index = 0; Index < Count; Index++) {
1058 Status = RootBridgeIoMemRead (
1059 This,
1060 Width,
1061 SrcAddress,
1062 1,
1063 &Result
1064 );
1065 if (EFI_ERROR (Status)) {
1066 return Status;
1067 }
1068 Status = RootBridgeIoMemWrite (
1069 This,
1070 Width,
1071 DestAddress,
1072 1,
1073 &Result
1074 );
1075 if (EFI_ERROR (Status)) {
1076 return Status;
1077 }
1078 if (Forward) {
1079 SrcAddress += Stride;
1080 DestAddress += Stride;
1081 } else {
1082 SrcAddress -= Stride;
1083 DestAddress -= Stride;
1084 }
1085 }
1086 return EFI_SUCCESS;
1087 }
1088
1089
1090 /**
1091 PCI configuration space access.
1092
1093 @param This A pointer to EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
1094 @param Read TRUE indicating it's a read operation.
1095 @param Width Signifies the width of the memory operation.
1096 @param Address The address within the PCI configuration space
1097 for the PCI controller.
1098 @param Count The number of PCI configuration operations
1099 to perform.
1100 @param Buffer The destination buffer to store the results.
1101
1102 @retval EFI_SUCCESS The data was read/written from/to the PCI root bridge.
1103 @retval EFI_INVALID_PARAMETER Invalid parameters found.
1104 **/
1105 EFI_STATUS
1106 EFIAPI
1107 RootBridgeIoPciAccess (
1108 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
1109 IN BOOLEAN Read,
1110 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
1111 IN UINT64 Address,
1112 IN UINTN Count,
1113 IN OUT VOID *Buffer
1114 )
1115 {
1116 EFI_STATUS Status;
1117 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;
1118 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS PciAddress;
1119 UINT8 *Uint8Buffer;
1120 UINT8 InStride;
1121 UINT8 OutStride;
1122 UINTN Size;
1123
1124 Status = RootBridgeIoCheckParameter (This, PciOperation, Width, Address, Count, Buffer);
1125 if (EFI_ERROR (Status)) {
1126 return Status;
1127 }
1128
1129 //
1130 // Read Pci configuration space
1131 //
1132 RootBridge = ROOT_BRIDGE_FROM_THIS (This);
1133 CopyMem (&PciAddress, &Address, sizeof (PciAddress));
1134
1135 if (PciAddress.ExtendedRegister == 0) {
1136 PciAddress.ExtendedRegister = PciAddress.Register;
1137 }
1138
1139 Address = PCI_SEGMENT_LIB_ADDRESS (
1140 RootBridge->RootBridgeIo.SegmentNumber,
1141 PciAddress.Bus,
1142 PciAddress.Device,
1143 PciAddress.Function,
1144 PciAddress.ExtendedRegister
1145 );
1146
1147 //
1148 // Select loop based on the width of the transfer
1149 //
1150 InStride = mInStride[Width];
1151 OutStride = mOutStride[Width];
1152 Size = (UINTN) (1 << (Width & 0x03));
1153 for (Uint8Buffer = Buffer; Count > 0; Address += InStride, Uint8Buffer += OutStride, Count--) {
1154 if (Read) {
1155 PciSegmentReadBuffer (Address, Size, Uint8Buffer);
1156 } else {
1157 PciSegmentWriteBuffer (Address, Size, Uint8Buffer);
1158 }
1159 }
1160 return EFI_SUCCESS;
1161 }
1162
1163 /**
1164 Allows read from PCI configuration space.
1165
1166 @param This A pointer to EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
1167 @param Width Signifies the width of the memory operation.
1168 @param Address The address within the PCI configuration space
1169 for the PCI controller.
1170 @param Count The number of PCI configuration operations
1171 to perform.
1172 @param Buffer The destination buffer to store the results.
1173
1174 @retval EFI_SUCCESS The data was read from the PCI root bridge.
1175 @retval EFI_INVALID_PARAMETER Invalid parameters found.
1176 **/
1177 EFI_STATUS
1178 EFIAPI
1179 RootBridgeIoPciRead (
1180 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
1181 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
1182 IN UINT64 Address,
1183 IN UINTN Count,
1184 IN OUT VOID *Buffer
1185 )
1186 {
1187 return RootBridgeIoPciAccess (This, TRUE, Width, Address, Count, Buffer);
1188 }
1189
1190 /**
1191 Allows write to PCI configuration space.
1192
1193 @param This A pointer to EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
1194 @param Width Signifies the width of the memory operation.
1195 @param Address The address within the PCI configuration space
1196 for the PCI controller.
1197 @param Count The number of PCI configuration operations
1198 to perform.
1199 @param Buffer The source buffer to get the results.
1200
1201 @retval EFI_SUCCESS The data was written to the PCI root bridge.
1202 @retval EFI_INVALID_PARAMETER Invalid parameters found.
1203 **/
1204 EFI_STATUS
1205 EFIAPI
1206 RootBridgeIoPciWrite (
1207 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
1208 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
1209 IN UINT64 Address,
1210 IN UINTN Count,
1211 IN OUT VOID *Buffer
1212 )
1213 {
1214 return RootBridgeIoPciAccess (This, FALSE, Width, Address, Count, Buffer);
1215 }
1216
1217 /**
1218 Provides the PCI controller-specific address needed to access
1219 system memory for DMA.
1220
1221 @param This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1222 @param Operation Indicate if the bus master is going to read or write
1223 to system memory.
1224 @param HostAddress The system memory address to map on the PCI controller.
1225 @param NumberOfBytes On input the number of bytes to map.
1226 On output the number of bytes that were mapped.
1227 @param DeviceAddress The resulting map address for the bus master PCI
1228 controller to use to access the system memory's HostAddress.
1229 @param Mapping The value to pass to Unmap() when the bus master DMA
1230 operation is complete.
1231
1232 @retval EFI_SUCCESS Success.
1233 @retval EFI_INVALID_PARAMETER Invalid parameters found.
1234 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
1235 @retval EFI_DEVICE_ERROR The System hardware could not map the requested address.
1236 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to lack of resources.
1237 **/
1238 EFI_STATUS
1239 EFIAPI
1240 RootBridgeIoMap (
1241 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
1242 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_OPERATION Operation,
1243 IN VOID *HostAddress,
1244 IN OUT UINTN *NumberOfBytes,
1245 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,
1246 OUT VOID **Mapping
1247 )
1248 {
1249 EFI_STATUS Status;
1250 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;
1251 EFI_PHYSICAL_ADDRESS PhysicalAddress;
1252 MAP_INFO *MapInfo;
1253
1254 if (HostAddress == NULL || NumberOfBytes == NULL || DeviceAddress == NULL ||
1255 Mapping == NULL) {
1256 return EFI_INVALID_PARAMETER;
1257 }
1258
1259 //
1260 // Make sure that Operation is valid
1261 //
1262 if ((UINT32) Operation >= EfiPciOperationMaximum) {
1263 return EFI_INVALID_PARAMETER;
1264 }
1265
1266 RootBridge = ROOT_BRIDGE_FROM_THIS (This);
1267
1268 if (mIoMmuProtocol != NULL) {
1269 if (!RootBridge->DmaAbove4G) {
1270 //
1271 // Clear 64bit support
1272 //
1273 if (Operation > EfiPciOperationBusMasterCommonBuffer) {
1274 Operation = (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_OPERATION) (Operation - EfiPciOperationBusMasterRead64);
1275 }
1276 }
1277 Status = mIoMmuProtocol->Map (
1278 mIoMmuProtocol,
1279 (EDKII_IOMMU_OPERATION) Operation,
1280 HostAddress,
1281 NumberOfBytes,
1282 DeviceAddress,
1283 Mapping
1284 );
1285 return Status;
1286 }
1287
1288 PhysicalAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) HostAddress;
1289 if ((!RootBridge->DmaAbove4G ||
1290 (Operation != EfiPciOperationBusMasterRead64 &&
1291 Operation != EfiPciOperationBusMasterWrite64 &&
1292 Operation != EfiPciOperationBusMasterCommonBuffer64)) &&
1293 ((PhysicalAddress + *NumberOfBytes) > SIZE_4GB)) {
1294
1295 //
1296 // If the root bridge or the device cannot handle performing DMA above
1297 // 4GB but any part of the DMA transfer being mapped is above 4GB, then
1298 // map the DMA transfer to a buffer below 4GB.
1299 //
1300
1301 if (Operation == EfiPciOperationBusMasterCommonBuffer ||
1302 Operation == EfiPciOperationBusMasterCommonBuffer64) {
1303 //
1304 // Common Buffer operations can not be remapped. If the common buffer
1305 // if above 4GB, then it is not possible to generate a mapping, so return
1306 // an error.
1307 //
1308 return EFI_UNSUPPORTED;
1309 }
1310
1311 //
1312 // Allocate a MAP_INFO structure to remember the mapping when Unmap() is
1313 // called later.
1314 //
1315 MapInfo = AllocatePool (sizeof (MAP_INFO));
1316 if (MapInfo == NULL) {
1317 *NumberOfBytes = 0;
1318 return EFI_OUT_OF_RESOURCES;
1319 }
1320
1321 //
1322 // Initialize the MAP_INFO structure
1323 //
1324 MapInfo->Signature = MAP_INFO_SIGNATURE;
1325 MapInfo->Operation = Operation;
1326 MapInfo->NumberOfBytes = *NumberOfBytes;
1327 MapInfo->NumberOfPages = EFI_SIZE_TO_PAGES (MapInfo->NumberOfBytes);
1328 MapInfo->HostAddress = PhysicalAddress;
1329 MapInfo->MappedHostAddress = SIZE_4GB - 1;
1330
1331 //
1332 // Allocate a buffer below 4GB to map the transfer to.
1333 //
1334 Status = gBS->AllocatePages (
1335 AllocateMaxAddress,
1336 EfiBootServicesData,
1337 MapInfo->NumberOfPages,
1338 &MapInfo->MappedHostAddress
1339 );
1340 if (EFI_ERROR (Status)) {
1341 FreePool (MapInfo);
1342 *NumberOfBytes = 0;
1343 return Status;
1344 }
1345
1346 //
1347 // If this is a read operation from the Bus Master's point of view,
1348 // then copy the contents of the real buffer into the mapped buffer
1349 // so the Bus Master can read the contents of the real buffer.
1350 //
1351 if (Operation == EfiPciOperationBusMasterRead ||
1352 Operation == EfiPciOperationBusMasterRead64) {
1353 CopyMem (
1354 (VOID *) (UINTN) MapInfo->MappedHostAddress,
1355 (VOID *) (UINTN) MapInfo->HostAddress,
1356 MapInfo->NumberOfBytes
1357 );
1358 }
1359
1360 InsertTailList (&RootBridge->Maps, &MapInfo->Link);
1361
1362 //
1363 // The DeviceAddress is the address of the maped buffer below 4GB
1364 //
1365 *DeviceAddress = MapInfo->MappedHostAddress;
1366 //
1367 // Return a pointer to the MAP_INFO structure in Mapping
1368 //
1369 *Mapping = MapInfo;
1370 } else {
1371 //
1372 // If the root bridge CAN handle performing DMA above 4GB or
1373 // the transfer is below 4GB, so the DeviceAddress is simply the
1374 // HostAddress
1375 //
1376 *DeviceAddress = PhysicalAddress;
1377 *Mapping = NO_MAPPING;
1378 }
1379
1380 return EFI_SUCCESS;
1381 }
1382
1383 /**
1384 Completes the Map() operation and releases any corresponding resources.
1385
1386 The Unmap() function completes the Map() operation and releases any
1387 corresponding resources.
1388 If the operation was an EfiPciOperationBusMasterWrite or
1389 EfiPciOperationBusMasterWrite64, the data is committed to the target system
1390 memory.
1391 Any resources used for the mapping are freed.
1392
1393 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1394 @param[in] Mapping The mapping value returned from Map().
1395
1396 @retval EFI_SUCCESS The range was unmapped.
1397 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by Map().
1398 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
1399 **/
1400 EFI_STATUS
1401 EFIAPI
1402 RootBridgeIoUnmap (
1403 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
1404 IN VOID *Mapping
1405 )
1406 {
1407 MAP_INFO *MapInfo;
1408 LIST_ENTRY *Link;
1409 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;
1410 EFI_STATUS Status;
1411
1412 if (mIoMmuProtocol != NULL) {
1413 Status = mIoMmuProtocol->Unmap (
1414 mIoMmuProtocol,
1415 Mapping
1416 );
1417 return Status;
1418 }
1419
1420 RootBridge = ROOT_BRIDGE_FROM_THIS (This);
1421
1422 //
1423 // See if the Map() operation associated with this Unmap() required a mapping
1424 // buffer. If a mapping buffer was not required, then this function simply
1425 // returns EFI_SUCCESS.
1426 //
1427 if (Mapping == NO_MAPPING) {
1428 return EFI_SUCCESS;
1429 }
1430
1431 MapInfo = NO_MAPPING;
1432 for (Link = GetFirstNode (&RootBridge->Maps)
1433 ; !IsNull (&RootBridge->Maps, Link)
1434 ; Link = GetNextNode (&RootBridge->Maps, Link)
1435 ) {
1436 MapInfo = MAP_INFO_FROM_LINK (Link);
1437 if (MapInfo == Mapping) {
1438 break;
1439 }
1440 }
1441 //
1442 // Mapping is not a valid value returned by Map()
1443 //
1444 if (MapInfo != Mapping) {
1445 return EFI_INVALID_PARAMETER;
1446 }
1447 RemoveEntryList (&MapInfo->Link);
1448
1449 //
1450 // If this is a write operation from the Bus Master's point of view,
1451 // then copy the contents of the mapped buffer into the real buffer
1452 // so the processor can read the contents of the real buffer.
1453 //
1454 if (MapInfo->Operation == EfiPciOperationBusMasterWrite ||
1455 MapInfo->Operation == EfiPciOperationBusMasterWrite64) {
1456 CopyMem (
1457 (VOID *) (UINTN) MapInfo->HostAddress,
1458 (VOID *) (UINTN) MapInfo->MappedHostAddress,
1459 MapInfo->NumberOfBytes
1460 );
1461 }
1462
1463 //
1464 // Free the mapped buffer and the MAP_INFO structure.
1465 //
1466 gBS->FreePages (MapInfo->MappedHostAddress, MapInfo->NumberOfPages);
1467 FreePool (Mapping);
1468 return EFI_SUCCESS;
1469 }
1470
1471 /**
1472 Allocates pages that are suitable for an EfiPciOperationBusMasterCommonBuffer
1473 or EfiPciOperationBusMasterCommonBuffer64 mapping.
1474
1475 @param This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1476 @param Type This parameter is not used and must be ignored.
1477 @param MemoryType The type of memory to allocate, EfiBootServicesData or
1478 EfiRuntimeServicesData.
1479 @param Pages The number of pages to allocate.
1480 @param HostAddress A pointer to store the base system memory address of the
1481 allocated range.
1482 @param Attributes The requested bit mask of attributes for the allocated
1483 range. Only the attributes
1484 EFI_PCI_ATTRIBUTE_MEMORY_WRITE_COMBINE,
1485 EFI_PCI_ATTRIBUTE_MEMORY_CACHED, and
1486 EFI_PCI_ATTRIBUTE_DUAL_ADDRESS_CYCLE may be used with this
1487 function.
1488
1489 @retval EFI_SUCCESS The requested memory pages were allocated.
1490 @retval EFI_INVALID_PARAMETER MemoryType is invalid.
1491 @retval EFI_INVALID_PARAMETER HostAddress is NULL.
1492 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal
1493 attribute bits are MEMORY_WRITE_COMBINE,
1494 MEMORY_CACHED, and DUAL_ADDRESS_CYCLE.
1495 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
1496 **/
1497 EFI_STATUS
1498 EFIAPI
1499 RootBridgeIoAllocateBuffer (
1500 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
1501 IN EFI_ALLOCATE_TYPE Type,
1502 IN EFI_MEMORY_TYPE MemoryType,
1503 IN UINTN Pages,
1504 OUT VOID **HostAddress,
1505 IN UINT64 Attributes
1506 )
1507 {
1508 EFI_STATUS Status;
1509 EFI_PHYSICAL_ADDRESS PhysicalAddress;
1510 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;
1511 EFI_ALLOCATE_TYPE AllocateType;
1512
1513 //
1514 // Validate Attributes
1515 //
1516 if ((Attributes & EFI_PCI_ATTRIBUTE_INVALID_FOR_ALLOCATE_BUFFER) != 0) {
1517 return EFI_UNSUPPORTED;
1518 }
1519
1520 //
1521 // Check for invalid inputs
1522 //
1523 if (HostAddress == NULL) {
1524 return EFI_INVALID_PARAMETER;
1525 }
1526
1527 //
1528 // The only valid memory types are EfiBootServicesData and
1529 // EfiRuntimeServicesData
1530 //
1531 if (MemoryType != EfiBootServicesData &&
1532 MemoryType != EfiRuntimeServicesData) {
1533 return EFI_INVALID_PARAMETER;
1534 }
1535
1536 RootBridge = ROOT_BRIDGE_FROM_THIS (This);
1537
1538 if (mIoMmuProtocol != NULL) {
1539 if (!RootBridge->DmaAbove4G) {
1540 //
1541 // Clear DUAL_ADDRESS_CYCLE
1542 //
1543 Attributes &= ~((UINT64) EFI_PCI_ATTRIBUTE_DUAL_ADDRESS_CYCLE);
1544 }
1545 Status = mIoMmuProtocol->AllocateBuffer (
1546 mIoMmuProtocol,
1547 Type,
1548 MemoryType,
1549 Pages,
1550 HostAddress,
1551 Attributes
1552 );
1553 return Status;
1554 }
1555
1556 AllocateType = AllocateAnyPages;
1557 if (!RootBridge->DmaAbove4G ||
1558 (Attributes & EFI_PCI_ATTRIBUTE_DUAL_ADDRESS_CYCLE) == 0) {
1559 //
1560 // Limit allocations to memory below 4GB
1561 //
1562 AllocateType = AllocateMaxAddress;
1563 PhysicalAddress = (EFI_PHYSICAL_ADDRESS) (SIZE_4GB - 1);
1564 }
1565 Status = gBS->AllocatePages (
1566 AllocateType,
1567 MemoryType,
1568 Pages,
1569 &PhysicalAddress
1570 );
1571 if (!EFI_ERROR (Status)) {
1572 *HostAddress = (VOID *) (UINTN) PhysicalAddress;
1573 }
1574
1575 return Status;
1576 }
1577
1578 /**
1579 Frees memory that was allocated with AllocateBuffer().
1580
1581 The FreeBuffer() function frees memory that was allocated with
1582 AllocateBuffer().
1583
1584 @param This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1585 @param Pages The number of pages to free.
1586 @param HostAddress The base system memory address of the allocated range.
1587
1588 @retval EFI_SUCCESS The requested memory pages were freed.
1589 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and
1590 Pages was not allocated with AllocateBuffer().
1591 **/
1592 EFI_STATUS
1593 EFIAPI
1594 RootBridgeIoFreeBuffer (
1595 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
1596 IN UINTN Pages,
1597 OUT VOID *HostAddress
1598 )
1599 {
1600 EFI_STATUS Status;
1601
1602 if (mIoMmuProtocol != NULL) {
1603 Status = mIoMmuProtocol->FreeBuffer (
1604 mIoMmuProtocol,
1605 Pages,
1606 HostAddress
1607 );
1608 return Status;
1609 }
1610
1611 return gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) HostAddress, Pages);
1612 }
1613
1614 /**
1615 Flushes all PCI posted write transactions from a PCI host bridge to system
1616 memory.
1617
1618 The Flush() function flushes any PCI posted write transactions from a PCI
1619 host bridge to system memory. Posted write transactions are generated by PCI
1620 bus masters when they perform write transactions to target addresses in
1621 system memory.
1622 This function does not flush posted write transactions from any PCI bridges.
1623 A PCI controller specific action must be taken to guarantee that the posted
1624 write transactions have been flushed from the PCI controller and from all the
1625 PCI bridges into the PCI host bridge. This is typically done with a PCI read
1626 transaction from the PCI controller prior to calling Flush().
1627
1628 @param This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1629
1630 @retval EFI_SUCCESS The PCI posted write transactions were flushed
1631 from the PCI host bridge to system memory.
1632 @retval EFI_DEVICE_ERROR The PCI posted write transactions were not flushed
1633 from the PCI host bridge due to a hardware error.
1634 **/
1635 EFI_STATUS
1636 EFIAPI
1637 RootBridgeIoFlush (
1638 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This
1639 )
1640 {
1641 return EFI_SUCCESS;
1642 }
1643
1644 /**
1645 Gets the attributes that a PCI root bridge supports setting with
1646 SetAttributes(), and the attributes that a PCI root bridge is currently
1647 using.
1648
1649 The GetAttributes() function returns the mask of attributes that this PCI
1650 root bridge supports and the mask of attributes that the PCI root bridge is
1651 currently using.
1652
1653 @param This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1654 @param Supported A pointer to the mask of attributes that this PCI root
1655 bridge supports setting with SetAttributes().
1656 @param Attributes A pointer to the mask of attributes that this PCI root
1657 bridge is currently using.
1658
1659 @retval EFI_SUCCESS If Supports is not NULL, then the attributes
1660 that the PCI root bridge supports is returned
1661 in Supports. If Attributes is not NULL, then
1662 the attributes that the PCI root bridge is
1663 currently using is returned in Attributes.
1664 @retval EFI_INVALID_PARAMETER Both Supports and Attributes are NULL.
1665 **/
1666 EFI_STATUS
1667 EFIAPI
1668 RootBridgeIoGetAttributes (
1669 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
1670 OUT UINT64 *Supported,
1671 OUT UINT64 *Attributes
1672 )
1673 {
1674 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;
1675
1676 if (Attributes == NULL && Supported == NULL) {
1677 return EFI_INVALID_PARAMETER;
1678 }
1679
1680 RootBridge = ROOT_BRIDGE_FROM_THIS (This);
1681 //
1682 // Set the return value for Supported and Attributes
1683 //
1684 if (Supported != NULL) {
1685 *Supported = RootBridge->Supports;
1686 }
1687
1688 if (Attributes != NULL) {
1689 *Attributes = RootBridge->Attributes;
1690 }
1691
1692 return EFI_SUCCESS;
1693 }
1694
1695 /**
1696 Sets attributes for a resource range on a PCI root bridge.
1697
1698 The SetAttributes() function sets the attributes specified in Attributes for
1699 the PCI root bridge on the resource range specified by ResourceBase and
1700 ResourceLength. Since the granularity of setting these attributes may vary
1701 from resource type to resource type, and from platform to platform, the
1702 actual resource range and the one passed in by the caller may differ. As a
1703 result, this function may set the attributes specified by Attributes on a
1704 larger resource range than the caller requested. The actual range is returned
1705 in ResourceBase and ResourceLength. The caller is responsible for verifying
1706 that the actual range for which the attributes were set is acceptable.
1707
1708 @param This A pointer to the
1709 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1710 @param Attributes The mask of attributes to set. If the
1711 attribute bit MEMORY_WRITE_COMBINE,
1712 MEMORY_CACHED, or MEMORY_DISABLE is set,
1713 then the resource range is specified by
1714 ResourceBase and ResourceLength. If
1715 MEMORY_WRITE_COMBINE, MEMORY_CACHED, and
1716 MEMORY_DISABLE are not set, then
1717 ResourceBase and ResourceLength are ignored,
1718 and may be NULL.
1719 @param ResourceBase A pointer to the base address of the
1720 resource range to be modified by the
1721 attributes specified by Attributes.
1722 @param ResourceLength A pointer to the length of the resource
1723 range to be modified by the attributes
1724 specified by Attributes.
1725
1726 @retval EFI_SUCCESS The current configuration of this PCI root bridge
1727 was returned in Resources.
1728 @retval EFI_UNSUPPORTED The current configuration of this PCI root bridge
1729 could not be retrieved.
1730 **/
1731 EFI_STATUS
1732 EFIAPI
1733 RootBridgeIoSetAttributes (
1734 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
1735 IN UINT64 Attributes,
1736 IN OUT UINT64 *ResourceBase,
1737 IN OUT UINT64 *ResourceLength
1738 )
1739 {
1740 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;
1741
1742 RootBridge = ROOT_BRIDGE_FROM_THIS (This);
1743
1744 if ((Attributes & (~RootBridge->Supports)) != 0) {
1745 return EFI_UNSUPPORTED;
1746 }
1747
1748 RootBridge->Attributes = Attributes;
1749 return EFI_SUCCESS;
1750 }
1751
1752 /**
1753 Retrieves the current resource settings of this PCI root bridge in the form
1754 of a set of ACPI resource descriptors.
1755
1756 There are only two resource descriptor types from the ACPI Specification that
1757 may be used to describe the current resources allocated to a PCI root bridge.
1758 These are the QWORD Address Space Descriptor, and the End Tag. The QWORD
1759 Address Space Descriptor can describe memory, I/O, and bus number ranges for
1760 dynamic or fixed resources. The configuration of a PCI root bridge is described
1761 with one or more QWORD Address Space Descriptors followed by an End Tag.
1762
1763 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1764 @param[out] Resources A pointer to the resource descriptors that
1765 describe the current configuration of this PCI root
1766 bridge. The storage for the resource
1767 descriptors is allocated by this function. The
1768 caller must treat the return buffer as read-only
1769 data, and the buffer must not be freed by the
1770 caller.
1771
1772 @retval EFI_SUCCESS The current configuration of this PCI root bridge
1773 was returned in Resources.
1774 @retval EFI_UNSUPPORTED The current configuration of this PCI root bridge
1775 could not be retrieved.
1776 **/
1777 EFI_STATUS
1778 EFIAPI
1779 RootBridgeIoConfiguration (
1780 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
1781 OUT VOID **Resources
1782 )
1783 {
1784 PCI_RESOURCE_TYPE Index;
1785 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;
1786 PCI_RES_NODE *ResAllocNode;
1787 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptor;
1788 EFI_ACPI_END_TAG_DESCRIPTOR *End;
1789
1790 //
1791 // Get this instance of the Root Bridge.
1792 //
1793 RootBridge = ROOT_BRIDGE_FROM_THIS (This);
1794 ZeroMem (
1795 RootBridge->ConfigBuffer,
1796 TypeMax * sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) + sizeof (EFI_ACPI_END_TAG_DESCRIPTOR)
1797 );
1798 Descriptor = RootBridge->ConfigBuffer;
1799 for (Index = TypeIo; Index < TypeMax; Index++) {
1800
1801 ResAllocNode = &RootBridge->ResAllocNode[Index];
1802
1803 if (ResAllocNode->Status != ResAllocated) {
1804 continue;
1805 }
1806
1807 Descriptor->Desc = ACPI_ADDRESS_SPACE_DESCRIPTOR;
1808 Descriptor->Len = sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) - 3;
1809 // According to UEFI 2.7, RootBridgeIo->Configuration should return address
1810 // range in CPU view (host address), and ResAllocNode->Base is already a CPU
1811 // view address (host address).
1812 Descriptor->AddrRangeMin = ResAllocNode->Base;
1813 Descriptor->AddrRangeMax = ResAllocNode->Base + ResAllocNode->Length - 1;
1814 Descriptor->AddrLen = ResAllocNode->Length;
1815 Descriptor->AddrTranslationOffset = GetTranslationByResourceType (
1816 RootBridge,
1817 ResAllocNode->Type
1818 );
1819
1820 switch (ResAllocNode->Type) {
1821
1822 case TypeIo:
1823 Descriptor->ResType = ACPI_ADDRESS_SPACE_TYPE_IO;
1824 break;
1825
1826 case TypePMem32:
1827 Descriptor->SpecificFlag = EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE;
1828 case TypeMem32:
1829 Descriptor->ResType = ACPI_ADDRESS_SPACE_TYPE_MEM;
1830 Descriptor->AddrSpaceGranularity = 32;
1831 break;
1832
1833 case TypePMem64:
1834 Descriptor->SpecificFlag = EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE;
1835 case TypeMem64:
1836 Descriptor->ResType = ACPI_ADDRESS_SPACE_TYPE_MEM;
1837 Descriptor->AddrSpaceGranularity = 64;
1838 break;
1839
1840 case TypeBus:
1841 Descriptor->ResType = ACPI_ADDRESS_SPACE_TYPE_BUS;
1842 break;
1843
1844 default:
1845 break;
1846 }
1847
1848 Descriptor++;
1849 }
1850 //
1851 // Terminate the entries.
1852 //
1853 End = (EFI_ACPI_END_TAG_DESCRIPTOR *) Descriptor;
1854 End->Desc = ACPI_END_TAG_DESCRIPTOR;
1855 End->Checksum = 0x0;
1856
1857 *Resources = RootBridge->ConfigBuffer;
1858 return EFI_SUCCESS;
1859 }