2 PCI Host Bridge Library instance for pci-ecam-generic DT nodes
4 Copyright (c) 2016, Linaro Ltd. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <Library/PciHostBridgeLib.h>
11 #include <Library/DebugLib.h>
12 #include <Library/DevicePathLib.h>
13 #include <Library/DxeServicesTableLib.h>
14 #include <Library/MemoryAllocationLib.h>
15 #include <Library/PcdLib.h>
16 #include <Library/UefiBootServicesTableLib.h>
18 #include <Protocol/FdtClient.h>
19 #include <Protocol/PciRootBridgeIo.h>
20 #include <Protocol/PciHostBridgeResourceAllocation.h>
24 ACPI_HID_DEVICE_PATH AcpiDevicePath
;
25 EFI_DEVICE_PATH_PROTOCOL EndDevicePath
;
26 } EFI_PCI_ROOT_BRIDGE_DEVICE_PATH
;
29 STATIC EFI_PCI_ROOT_BRIDGE_DEVICE_PATH mEfiPciRootBridgeDevicePath
= {
35 (UINT8
) (sizeof(ACPI_HID_DEVICE_PATH
)),
36 (UINT8
) ((sizeof(ACPI_HID_DEVICE_PATH
)) >> 8)
45 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
47 END_DEVICE_PATH_LENGTH
,
53 GLOBAL_REMOVE_IF_UNREFERENCED
54 CHAR16
*mPciHostBridgeLibAcpiAddressSpaceTypeStr
[] = {
55 L
"Mem", L
"I/O", L
"Bus"
59 // We expect the "ranges" property of "pci-host-ecam-generic" to consist of
68 } DTB_PCI_HOST_RANGE_RECORD
;
71 #define DTB_PCI_HOST_RANGE_RELOCATABLE BIT31
72 #define DTB_PCI_HOST_RANGE_PREFETCHABLE BIT30
73 #define DTB_PCI_HOST_RANGE_ALIASED BIT29
74 #define DTB_PCI_HOST_RANGE_MMIO32 BIT25
75 #define DTB_PCI_HOST_RANGE_MMIO64 (BIT25 | BIT24)
76 #define DTB_PCI_HOST_RANGE_IO BIT24
77 #define DTB_PCI_HOST_RANGE_TYPEMASK (BIT31 | BIT30 | BIT29 | BIT25 | BIT24)
88 Status
= gDS
->AddMemorySpace (EfiGcdMemoryTypeMemoryMappedIo
, Base
, Size
,
90 if (EFI_ERROR (Status
)) {
92 "%a: failed to add GCD memory space for region [0x%Lx+0x%Lx)\n",
93 __FUNCTION__
, Base
, Size
));
97 Status
= gDS
->SetMemorySpaceAttributes (Base
, Size
, EFI_MEMORY_UC
);
98 if (EFI_ERROR (Status
)) {
100 "%a: failed to set memory space attributes for region [0x%Lx+0x%Lx)\n",
101 __FUNCTION__
, Base
, Size
));
111 OUT UINT64
*Mmio32Base
,
112 OUT UINT64
*Mmio32Size
,
113 OUT UINT64
*Mmio64Base
,
114 OUT UINT64
*Mmio64Size
,
119 FDT_CLIENT_PROTOCOL
*FdtClient
;
121 UINT64 ConfigBase
, ConfigSize
;
126 UINT64 IoTranslation
;
127 UINT64 Mmio32Translation
;
128 UINT64 Mmio64Translation
;
131 // The following output arguments are initialized only in
132 // order to suppress '-Werror=maybe-uninitialized' warnings
133 // *incorrectly* emitted by some gcc versions.
137 *Mmio64Base
= MAX_UINT64
;
142 // *IoSize, *Mmio##Size and IoTranslation are initialized to zero because the
143 // logic below requires it. However, since they are also affected by the issue
144 // reported above, they are initialized early.
151 Status
= gBS
->LocateProtocol (&gFdtClientProtocolGuid
, NULL
,
152 (VOID
**)&FdtClient
);
153 ASSERT_EFI_ERROR (Status
);
155 Status
= FdtClient
->FindCompatibleNode (FdtClient
, "pci-host-ecam-generic",
157 if (EFI_ERROR (Status
)) {
159 "%a: No 'pci-host-ecam-generic' compatible DT node found\n",
161 return EFI_NOT_FOUND
;
168 // A DT can legally describe multiple PCI host bridges, but we are not
169 // equipped to deal with that. So assert that there is only one.
171 Status
= FdtClient
->FindNextCompatibleNode (FdtClient
,
172 "pci-host-ecam-generic", Node
, &Tmp
);
173 ASSERT (Status
== EFI_NOT_FOUND
);
176 Status
= FdtClient
->GetNodeProperty (FdtClient
, Node
, "reg", &Prop
, &Len
);
177 if (EFI_ERROR (Status
) || Len
!= 2 * sizeof (UINT64
)) {
178 DEBUG ((EFI_D_ERROR
, "%a: 'reg' property not found or invalid\n",
180 return EFI_PROTOCOL_ERROR
;
184 // Fetch the ECAM window.
186 ConfigBase
= SwapBytes64 (((CONST UINT64
*)Prop
)[0]);
187 ConfigSize
= SwapBytes64 (((CONST UINT64
*)Prop
)[1]);
190 // Fetch the bus range (note: inclusive).
192 Status
= FdtClient
->GetNodeProperty (FdtClient
, Node
, "bus-range", &Prop
,
194 if (EFI_ERROR (Status
) || Len
!= 2 * sizeof (UINT32
)) {
195 DEBUG ((EFI_D_ERROR
, "%a: 'bus-range' not found or invalid\n",
197 return EFI_PROTOCOL_ERROR
;
199 *BusMin
= SwapBytes32 (((CONST UINT32
*)Prop
)[0]);
200 *BusMax
= SwapBytes32 (((CONST UINT32
*)Prop
)[1]);
203 // Sanity check: the config space must accommodate all 4K register bytes of
204 // all 8 functions of all 32 devices of all buses.
206 if (*BusMax
< *BusMin
|| *BusMax
- *BusMin
== MAX_UINT32
||
207 DivU64x32 (ConfigSize
, SIZE_4KB
* 8 * 32) < *BusMax
- *BusMin
+ 1) {
208 DEBUG ((EFI_D_ERROR
, "%a: invalid 'bus-range' and/or 'reg'\n",
210 return EFI_PROTOCOL_ERROR
;
214 // Iterate over "ranges".
216 Status
= FdtClient
->GetNodeProperty (FdtClient
, Node
, "ranges", &Prop
, &Len
);
217 if (EFI_ERROR (Status
) || Len
== 0 ||
218 Len
% sizeof (DTB_PCI_HOST_RANGE_RECORD
) != 0) {
219 DEBUG ((EFI_D_ERROR
, "%a: 'ranges' not found or invalid\n", __FUNCTION__
));
220 return EFI_PROTOCOL_ERROR
;
223 for (RecordIdx
= 0; RecordIdx
< Len
/ sizeof (DTB_PCI_HOST_RANGE_RECORD
);
225 CONST DTB_PCI_HOST_RANGE_RECORD
*Record
;
227 Record
= (CONST DTB_PCI_HOST_RANGE_RECORD
*)Prop
+ RecordIdx
;
228 switch (SwapBytes32 (Record
->Type
) & DTB_PCI_HOST_RANGE_TYPEMASK
) {
229 case DTB_PCI_HOST_RANGE_IO
:
230 *IoBase
= SwapBytes64 (Record
->ChildBase
);
231 *IoSize
= SwapBytes64 (Record
->Size
);
232 IoTranslation
= SwapBytes64 (Record
->CpuBase
) - *IoBase
;
234 ASSERT (PcdGet64 (PcdPciIoTranslation
) == IoTranslation
);
237 case DTB_PCI_HOST_RANGE_MMIO32
:
238 *Mmio32Base
= SwapBytes64 (Record
->ChildBase
);
239 *Mmio32Size
= SwapBytes64 (Record
->Size
);
240 Mmio32Translation
= SwapBytes64 (Record
->CpuBase
) - *Mmio32Base
;
242 if (*Mmio32Base
> MAX_UINT32
|| *Mmio32Size
> MAX_UINT32
||
243 *Mmio32Base
+ *Mmio32Size
> SIZE_4GB
) {
244 DEBUG ((EFI_D_ERROR
, "%a: MMIO32 space invalid\n", __FUNCTION__
));
245 return EFI_PROTOCOL_ERROR
;
248 ASSERT (PcdGet64 (PcdPciMmio32Translation
) == Mmio32Translation
);
250 if (Mmio32Translation
!= 0) {
251 DEBUG ((EFI_D_ERROR
, "%a: unsupported nonzero MMIO32 translation "
252 "0x%Lx\n", __FUNCTION__
, Mmio32Translation
));
253 return EFI_UNSUPPORTED
;
258 case DTB_PCI_HOST_RANGE_MMIO64
:
259 *Mmio64Base
= SwapBytes64 (Record
->ChildBase
);
260 *Mmio64Size
= SwapBytes64 (Record
->Size
);
261 Mmio64Translation
= SwapBytes64 (Record
->CpuBase
) - *Mmio64Base
;
263 ASSERT (PcdGet64 (PcdPciMmio64Translation
) == Mmio64Translation
);
265 if (Mmio64Translation
!= 0) {
266 DEBUG ((EFI_D_ERROR
, "%a: unsupported nonzero MMIO64 translation "
267 "0x%Lx\n", __FUNCTION__
, Mmio64Translation
));
268 return EFI_UNSUPPORTED
;
274 if (*IoSize
== 0 || *Mmio32Size
== 0) {
275 DEBUG ((EFI_D_ERROR
, "%a: %a space empty\n", __FUNCTION__
,
276 (*IoSize
== 0) ? "IO" : "MMIO32"));
277 return EFI_PROTOCOL_ERROR
;
281 // The dynamic PCD PcdPciExpressBaseAddress should have already been set,
282 // and should match the value we found in the DT node.
284 ASSERT (PcdGet64 (PcdPciExpressBaseAddress
) == ConfigBase
);
286 DEBUG ((EFI_D_INFO
, "%a: Config[0x%Lx+0x%Lx) Bus[0x%x..0x%x] "
287 "Io[0x%Lx+0x%Lx)@0x%Lx Mem32[0x%Lx+0x%Lx)@0x0 Mem64[0x%Lx+0x%Lx)@0x0\n",
288 __FUNCTION__
, ConfigBase
, ConfigSize
, *BusMin
, *BusMax
, *IoBase
, *IoSize
,
289 IoTranslation
, *Mmio32Base
, *Mmio32Size
, *Mmio64Base
, *Mmio64Size
));
291 // Map the ECAM space in the GCD memory map
292 Status
= MapGcdMmioSpace (ConfigBase
, ConfigSize
);
293 ASSERT_EFI_ERROR (Status
);
294 if (EFI_ERROR (Status
)) {
299 // Map the MMIO window that provides I/O access - the PCI host bridge code
300 // is not aware of this translation and so it will only map the I/O view
301 // in the GCD I/O map.
303 Status
= MapGcdMmioSpace (*IoBase
+ IoTranslation
, *IoSize
);
304 ASSERT_EFI_ERROR (Status
);
309 STATIC PCI_ROOT_BRIDGE mRootBridge
;
312 Return all the root bridge instances in an array.
314 @param Count Return the count of root bridge instances.
316 @return All the root bridge instances in an array.
317 The array should be passed into PciHostBridgeFreeRootBridges()
322 PciHostBridgeGetRootBridges (
326 UINT64 IoBase
, IoSize
;
327 UINT64 Mmio32Base
, Mmio32Size
;
328 UINT64 Mmio64Base
, Mmio64Size
;
329 UINT32 BusMin
, BusMax
;
332 if (PcdGet64 (PcdPciExpressBaseAddress
) == 0) {
333 DEBUG ((EFI_D_INFO
, "%a: PCI host bridge not present\n", __FUNCTION__
));
339 Status
= ProcessPciHost (&IoBase
, &IoSize
, &Mmio32Base
, &Mmio32Size
,
340 &Mmio64Base
, &Mmio64Size
, &BusMin
, &BusMax
);
341 if (EFI_ERROR (Status
)) {
342 DEBUG ((EFI_D_ERROR
, "%a: failed to discover PCI host bridge: %r\n",
343 __FUNCTION__
, Status
));
350 mRootBridge
.Segment
= 0;
351 mRootBridge
.Supports
= EFI_PCI_ATTRIBUTE_ISA_IO_16
|
352 EFI_PCI_ATTRIBUTE_ISA_MOTHERBOARD_IO
|
353 EFI_PCI_ATTRIBUTE_VGA_IO_16
|
354 EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO_16
;
355 mRootBridge
.Attributes
= mRootBridge
.Supports
;
357 mRootBridge
.DmaAbove4G
= TRUE
;
358 mRootBridge
.NoExtendedConfigSpace
= FALSE
;
359 mRootBridge
.ResourceAssigned
= FALSE
;
361 mRootBridge
.AllocationAttributes
= EFI_PCI_HOST_BRIDGE_COMBINE_MEM_PMEM
;
363 mRootBridge
.Bus
.Base
= BusMin
;
364 mRootBridge
.Bus
.Limit
= BusMax
;
365 mRootBridge
.Io
.Base
= IoBase
;
366 mRootBridge
.Io
.Limit
= IoBase
+ IoSize
- 1;
367 mRootBridge
.Mem
.Base
= Mmio32Base
;
368 mRootBridge
.Mem
.Limit
= Mmio32Base
+ Mmio32Size
- 1;
370 if (sizeof (UINTN
) == sizeof (UINT64
)) {
371 mRootBridge
.MemAbove4G
.Base
= Mmio64Base
;
372 mRootBridge
.MemAbove4G
.Limit
= Mmio64Base
+ Mmio64Size
- 1;
373 if (Mmio64Size
> 0) {
374 mRootBridge
.AllocationAttributes
|= EFI_PCI_HOST_BRIDGE_MEM64_DECODE
;
378 // UEFI mandates a 1:1 virtual-to-physical mapping, so on a 32-bit
379 // architecture such as ARM, we will not be able to access 64-bit MMIO
380 // BARs unless they are allocated below 4 GB. So ignore the range above
381 // 4 GB in this case.
383 mRootBridge
.MemAbove4G
.Base
= MAX_UINT64
;
384 mRootBridge
.MemAbove4G
.Limit
= 0;
388 // No separate ranges for prefetchable and non-prefetchable BARs
390 mRootBridge
.PMem
.Base
= MAX_UINT64
;
391 mRootBridge
.PMem
.Limit
= 0;
392 mRootBridge
.PMemAbove4G
.Base
= MAX_UINT64
;
393 mRootBridge
.PMemAbove4G
.Limit
= 0;
395 mRootBridge
.DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*)&mEfiPciRootBridgeDevicePath
;
401 Free the root bridge instances array returned from
402 PciHostBridgeGetRootBridges().
404 @param Bridges The root bridge instances array.
405 @param Count The count of the array.
409 PciHostBridgeFreeRootBridges (
410 PCI_ROOT_BRIDGE
*Bridges
,
418 Inform the platform that the resource conflict happens.
420 @param HostBridgeHandle Handle of the Host Bridge.
421 @param Configuration Pointer to PCI I/O and PCI memory resource
422 descriptors. The Configuration contains the resources
423 for all the root bridges. The resource for each root
424 bridge is terminated with END descriptor and an
425 additional END is appended indicating the end of the
426 entire resources. The resource descriptor field
427 values follow the description in
428 EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
433 PciHostBridgeResourceConflict (
434 EFI_HANDLE HostBridgeHandle
,
438 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptor
;
439 UINTN RootBridgeIndex
;
440 DEBUG ((EFI_D_ERROR
, "PciHostBridge: Resource conflict happens!\n"));
443 Descriptor
= (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*) Configuration
;
444 while (Descriptor
->Desc
== ACPI_ADDRESS_SPACE_DESCRIPTOR
) {
445 DEBUG ((EFI_D_ERROR
, "RootBridge[%d]:\n", RootBridgeIndex
++));
446 for (; Descriptor
->Desc
== ACPI_ADDRESS_SPACE_DESCRIPTOR
; Descriptor
++) {
447 ASSERT (Descriptor
->ResType
<
448 (sizeof (mPciHostBridgeLibAcpiAddressSpaceTypeStr
) /
449 sizeof (mPciHostBridgeLibAcpiAddressSpaceTypeStr
[0])
452 DEBUG ((EFI_D_ERROR
, " %s: Length/Alignment = 0x%lx / 0x%lx\n",
453 mPciHostBridgeLibAcpiAddressSpaceTypeStr
[Descriptor
->ResType
],
454 Descriptor
->AddrLen
, Descriptor
->AddrRangeMax
456 if (Descriptor
->ResType
== ACPI_ADDRESS_SPACE_TYPE_MEM
) {
457 DEBUG ((EFI_D_ERROR
, " Granularity/SpecificFlag = %ld / %02x%s\n",
458 Descriptor
->AddrSpaceGranularity
, Descriptor
->SpecificFlag
,
459 ((Descriptor
->SpecificFlag
&
460 EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE
461 ) != 0) ? L
" (Prefetchable)" : L
""
466 // Skip the END descriptor for root bridge
468 ASSERT (Descriptor
->Desc
== ACPI_END_TAG_DESCRIPTOR
);
469 Descriptor
= (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*)(
470 (EFI_ACPI_END_TAG_DESCRIPTOR
*)Descriptor
+ 1