2 PCI Host Bridge Library instance for pci-ecam-generic DT nodes
4 Copyright (c) 2016, Linaro Ltd. All rights reserved.<BR>
6 This program and the accompanying materials are licensed and made available
7 under the terms and conditions of the BSD License which accompanies this
8 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
17 #include <Library/PciHostBridgeLib.h>
18 #include <Library/DebugLib.h>
19 #include <Library/DevicePathLib.h>
20 #include <Library/DxeServicesTableLib.h>
21 #include <Library/MemoryAllocationLib.h>
22 #include <Library/PcdLib.h>
23 #include <Library/UefiBootServicesTableLib.h>
25 #include <Protocol/FdtClient.h>
26 #include <Protocol/PciRootBridgeIo.h>
27 #include <Protocol/PciHostBridgeResourceAllocation.h>
31 ACPI_HID_DEVICE_PATH AcpiDevicePath
;
32 EFI_DEVICE_PATH_PROTOCOL EndDevicePath
;
33 } EFI_PCI_ROOT_BRIDGE_DEVICE_PATH
;
36 STATIC EFI_PCI_ROOT_BRIDGE_DEVICE_PATH mEfiPciRootBridgeDevicePath
= {
42 (UINT8
) (sizeof(ACPI_HID_DEVICE_PATH
)),
43 (UINT8
) ((sizeof(ACPI_HID_DEVICE_PATH
)) >> 8)
52 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
54 END_DEVICE_PATH_LENGTH
,
60 GLOBAL_REMOVE_IF_UNREFERENCED
61 CHAR16
*mPciHostBridgeLibAcpiAddressSpaceTypeStr
[] = {
62 L
"Mem", L
"I/O", L
"Bus"
66 // We expect the "ranges" property of "pci-host-ecam-generic" to consist of
75 } DTB_PCI_HOST_RANGE_RECORD
;
78 #define DTB_PCI_HOST_RANGE_RELOCATABLE BIT31
79 #define DTB_PCI_HOST_RANGE_PREFETCHABLE BIT30
80 #define DTB_PCI_HOST_RANGE_ALIASED BIT29
81 #define DTB_PCI_HOST_RANGE_MMIO32 BIT25
82 #define DTB_PCI_HOST_RANGE_MMIO64 (BIT25 | BIT24)
83 #define DTB_PCI_HOST_RANGE_IO BIT24
84 #define DTB_PCI_HOST_RANGE_TYPEMASK (BIT31 | BIT30 | BIT29 | BIT25 | BIT24)
95 Status
= gDS
->AddMemorySpace (EfiGcdMemoryTypeMemoryMappedIo
, Base
, Size
,
97 if (EFI_ERROR (Status
)) {
99 "%a: failed to add GCD memory space for region [0x%Lx+0x%Lx)\n",
100 __FUNCTION__
, Base
, Size
));
104 Status
= gDS
->SetMemorySpaceAttributes (Base
, Size
, EFI_MEMORY_UC
);
105 if (EFI_ERROR (Status
)) {
107 "%a: failed to set memory space attributes for region [0x%Lx+0x%Lx)\n",
108 __FUNCTION__
, Base
, Size
));
118 OUT UINT64
*Mmio32Base
,
119 OUT UINT64
*Mmio32Size
,
120 OUT UINT64
*Mmio64Base
,
121 OUT UINT64
*Mmio64Size
,
126 FDT_CLIENT_PROTOCOL
*FdtClient
;
128 UINT64 ConfigBase
, ConfigSize
;
133 UINT64 IoTranslation
;
134 UINT64 Mmio32Translation
;
135 UINT64 Mmio64Translation
;
138 // The following output arguments are initialized only in
139 // order to suppress '-Werror=maybe-uninitialized' warnings
140 // *incorrectly* emitted by some gcc versions.
144 *Mmio64Base
= MAX_UINT64
;
149 // *IoSize, *Mmio##Size and IoTranslation are initialized to zero because the
150 // logic below requires it. However, since they are also affected by the issue
151 // reported above, they are initialized early.
158 Status
= gBS
->LocateProtocol (&gFdtClientProtocolGuid
, NULL
,
159 (VOID
**)&FdtClient
);
160 ASSERT_EFI_ERROR (Status
);
162 Status
= FdtClient
->FindCompatibleNode (FdtClient
, "pci-host-ecam-generic",
164 if (EFI_ERROR (Status
)) {
166 "%a: No 'pci-host-ecam-generic' compatible DT node found\n",
168 return EFI_NOT_FOUND
;
175 // A DT can legally describe multiple PCI host bridges, but we are not
176 // equipped to deal with that. So assert that there is only one.
178 Status
= FdtClient
->FindNextCompatibleNode (FdtClient
,
179 "pci-host-ecam-generic", Node
, &Tmp
);
180 ASSERT (Status
== EFI_NOT_FOUND
);
183 Status
= FdtClient
->GetNodeProperty (FdtClient
, Node
, "reg", &Prop
, &Len
);
184 if (EFI_ERROR (Status
) || Len
!= 2 * sizeof (UINT64
)) {
185 DEBUG ((EFI_D_ERROR
, "%a: 'reg' property not found or invalid\n",
187 return EFI_PROTOCOL_ERROR
;
191 // Fetch the ECAM window.
193 ConfigBase
= SwapBytes64 (((CONST UINT64
*)Prop
)[0]);
194 ConfigSize
= SwapBytes64 (((CONST UINT64
*)Prop
)[1]);
197 // Fetch the bus range (note: inclusive).
199 Status
= FdtClient
->GetNodeProperty (FdtClient
, Node
, "bus-range", &Prop
,
201 if (EFI_ERROR (Status
) || Len
!= 2 * sizeof (UINT32
)) {
202 DEBUG ((EFI_D_ERROR
, "%a: 'bus-range' not found or invalid\n",
204 return EFI_PROTOCOL_ERROR
;
206 *BusMin
= SwapBytes32 (((CONST UINT32
*)Prop
)[0]);
207 *BusMax
= SwapBytes32 (((CONST UINT32
*)Prop
)[1]);
210 // Sanity check: the config space must accommodate all 4K register bytes of
211 // all 8 functions of all 32 devices of all buses.
213 if (*BusMax
< *BusMin
|| *BusMax
- *BusMin
== MAX_UINT32
||
214 DivU64x32 (ConfigSize
, SIZE_4KB
* 8 * 32) < *BusMax
- *BusMin
+ 1) {
215 DEBUG ((EFI_D_ERROR
, "%a: invalid 'bus-range' and/or 'reg'\n",
217 return EFI_PROTOCOL_ERROR
;
221 // Iterate over "ranges".
223 Status
= FdtClient
->GetNodeProperty (FdtClient
, Node
, "ranges", &Prop
, &Len
);
224 if (EFI_ERROR (Status
) || Len
== 0 ||
225 Len
% sizeof (DTB_PCI_HOST_RANGE_RECORD
) != 0) {
226 DEBUG ((EFI_D_ERROR
, "%a: 'ranges' not found or invalid\n", __FUNCTION__
));
227 return EFI_PROTOCOL_ERROR
;
230 for (RecordIdx
= 0; RecordIdx
< Len
/ sizeof (DTB_PCI_HOST_RANGE_RECORD
);
232 CONST DTB_PCI_HOST_RANGE_RECORD
*Record
;
234 Record
= (CONST DTB_PCI_HOST_RANGE_RECORD
*)Prop
+ RecordIdx
;
235 switch (SwapBytes32 (Record
->Type
) & DTB_PCI_HOST_RANGE_TYPEMASK
) {
236 case DTB_PCI_HOST_RANGE_IO
:
237 *IoBase
= SwapBytes64 (Record
->ChildBase
);
238 *IoSize
= SwapBytes64 (Record
->Size
);
239 IoTranslation
= SwapBytes64 (Record
->CpuBase
) - *IoBase
;
241 ASSERT (PcdGet64 (PcdPciIoTranslation
) == IoTranslation
);
244 case DTB_PCI_HOST_RANGE_MMIO32
:
245 *Mmio32Base
= SwapBytes64 (Record
->ChildBase
);
246 *Mmio32Size
= SwapBytes64 (Record
->Size
);
247 Mmio32Translation
= SwapBytes64 (Record
->CpuBase
) - *Mmio32Base
;
249 if (*Mmio32Base
> MAX_UINT32
|| *Mmio32Size
> MAX_UINT32
||
250 *Mmio32Base
+ *Mmio32Size
> SIZE_4GB
) {
251 DEBUG ((EFI_D_ERROR
, "%a: MMIO32 space invalid\n", __FUNCTION__
));
252 return EFI_PROTOCOL_ERROR
;
255 ASSERT (PcdGet64 (PcdPciMmio32Translation
) == Mmio32Translation
);
257 if (Mmio32Translation
!= 0) {
258 DEBUG ((EFI_D_ERROR
, "%a: unsupported nonzero MMIO32 translation "
259 "0x%Lx\n", __FUNCTION__
, Mmio32Translation
));
260 return EFI_UNSUPPORTED
;
265 case DTB_PCI_HOST_RANGE_MMIO64
:
266 *Mmio64Base
= SwapBytes64 (Record
->ChildBase
);
267 *Mmio64Size
= SwapBytes64 (Record
->Size
);
268 Mmio64Translation
= SwapBytes64 (Record
->CpuBase
) - *Mmio64Base
;
270 ASSERT (PcdGet64 (PcdPciMmio64Translation
) == Mmio64Translation
);
272 if (Mmio64Translation
!= 0) {
273 DEBUG ((EFI_D_ERROR
, "%a: unsupported nonzero MMIO64 translation "
274 "0x%Lx\n", __FUNCTION__
, Mmio64Translation
));
275 return EFI_UNSUPPORTED
;
281 if (*IoSize
== 0 || *Mmio32Size
== 0) {
282 DEBUG ((EFI_D_ERROR
, "%a: %a space empty\n", __FUNCTION__
,
283 (*IoSize
== 0) ? "IO" : "MMIO32"));
284 return EFI_PROTOCOL_ERROR
;
288 // The dynamic PCD PcdPciExpressBaseAddress should have already been set,
289 // and should match the value we found in the DT node.
291 ASSERT (PcdGet64 (PcdPciExpressBaseAddress
) == ConfigBase
);
293 DEBUG ((EFI_D_INFO
, "%a: Config[0x%Lx+0x%Lx) Bus[0x%x..0x%x] "
294 "Io[0x%Lx+0x%Lx)@0x%Lx Mem32[0x%Lx+0x%Lx)@0x0 Mem64[0x%Lx+0x%Lx)@0x0\n",
295 __FUNCTION__
, ConfigBase
, ConfigSize
, *BusMin
, *BusMax
, *IoBase
, *IoSize
,
296 IoTranslation
, *Mmio32Base
, *Mmio32Size
, *Mmio64Base
, *Mmio64Size
));
298 // Map the ECAM space in the GCD memory map
299 Status
= MapGcdMmioSpace (ConfigBase
, ConfigSize
);
300 ASSERT_EFI_ERROR (Status
);
301 if (EFI_ERROR (Status
)) {
306 // Map the MMIO window that provides I/O access - the PCI host bridge code
307 // is not aware of this translation and so it will only map the I/O view
308 // in the GCD I/O map.
310 Status
= MapGcdMmioSpace (*IoBase
+ IoTranslation
, *IoSize
);
311 ASSERT_EFI_ERROR (Status
);
316 STATIC PCI_ROOT_BRIDGE mRootBridge
;
319 Return all the root bridge instances in an array.
321 @param Count Return the count of root bridge instances.
323 @return All the root bridge instances in an array.
324 The array should be passed into PciHostBridgeFreeRootBridges()
329 PciHostBridgeGetRootBridges (
333 UINT64 IoBase
, IoSize
;
334 UINT64 Mmio32Base
, Mmio32Size
;
335 UINT64 Mmio64Base
, Mmio64Size
;
336 UINT32 BusMin
, BusMax
;
339 if (PcdGet64 (PcdPciExpressBaseAddress
) == 0) {
340 DEBUG ((EFI_D_INFO
, "%a: PCI host bridge not present\n", __FUNCTION__
));
346 Status
= ProcessPciHost (&IoBase
, &IoSize
, &Mmio32Base
, &Mmio32Size
,
347 &Mmio64Base
, &Mmio64Size
, &BusMin
, &BusMax
);
348 if (EFI_ERROR (Status
)) {
349 DEBUG ((EFI_D_ERROR
, "%a: failed to discover PCI host bridge: %r\n",
350 __FUNCTION__
, Status
));
357 mRootBridge
.Segment
= 0;
358 mRootBridge
.Supports
= EFI_PCI_ATTRIBUTE_ISA_IO_16
|
359 EFI_PCI_ATTRIBUTE_ISA_MOTHERBOARD_IO
|
360 EFI_PCI_ATTRIBUTE_VGA_IO_16
|
361 EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO_16
;
362 mRootBridge
.Attributes
= mRootBridge
.Supports
;
364 mRootBridge
.DmaAbove4G
= TRUE
;
365 mRootBridge
.NoExtendedConfigSpace
= FALSE
;
366 mRootBridge
.ResourceAssigned
= FALSE
;
368 mRootBridge
.AllocationAttributes
= EFI_PCI_HOST_BRIDGE_COMBINE_MEM_PMEM
;
370 mRootBridge
.Bus
.Base
= BusMin
;
371 mRootBridge
.Bus
.Limit
= BusMax
;
372 mRootBridge
.Io
.Base
= IoBase
;
373 mRootBridge
.Io
.Limit
= IoBase
+ IoSize
- 1;
374 mRootBridge
.Mem
.Base
= Mmio32Base
;
375 mRootBridge
.Mem
.Limit
= Mmio32Base
+ Mmio32Size
- 1;
377 if (sizeof (UINTN
) == sizeof (UINT64
)) {
378 mRootBridge
.MemAbove4G
.Base
= Mmio64Base
;
379 mRootBridge
.MemAbove4G
.Limit
= Mmio64Base
+ Mmio64Size
- 1;
380 if (Mmio64Size
> 0) {
381 mRootBridge
.AllocationAttributes
|= EFI_PCI_HOST_BRIDGE_MEM64_DECODE
;
385 // UEFI mandates a 1:1 virtual-to-physical mapping, so on a 32-bit
386 // architecture such as ARM, we will not be able to access 64-bit MMIO
387 // BARs unless they are allocated below 4 GB. So ignore the range above
388 // 4 GB in this case.
390 mRootBridge
.MemAbove4G
.Base
= MAX_UINT64
;
391 mRootBridge
.MemAbove4G
.Limit
= 0;
395 // No separate ranges for prefetchable and non-prefetchable BARs
397 mRootBridge
.PMem
.Base
= MAX_UINT64
;
398 mRootBridge
.PMem
.Limit
= 0;
399 mRootBridge
.PMemAbove4G
.Base
= MAX_UINT64
;
400 mRootBridge
.PMemAbove4G
.Limit
= 0;
402 mRootBridge
.DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*)&mEfiPciRootBridgeDevicePath
;
408 Free the root bridge instances array returned from
409 PciHostBridgeGetRootBridges().
411 @param Bridges The root bridge instances array.
412 @param Count The count of the array.
416 PciHostBridgeFreeRootBridges (
417 PCI_ROOT_BRIDGE
*Bridges
,
425 Inform the platform that the resource conflict happens.
427 @param HostBridgeHandle Handle of the Host Bridge.
428 @param Configuration Pointer to PCI I/O and PCI memory resource
429 descriptors. The Configuration contains the resources
430 for all the root bridges. The resource for each root
431 bridge is terminated with END descriptor and an
432 additional END is appended indicating the end of the
433 entire resources. The resource descriptor field
434 values follow the description in
435 EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
440 PciHostBridgeResourceConflict (
441 EFI_HANDLE HostBridgeHandle
,
445 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptor
;
446 UINTN RootBridgeIndex
;
447 DEBUG ((EFI_D_ERROR
, "PciHostBridge: Resource conflict happens!\n"));
450 Descriptor
= (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*) Configuration
;
451 while (Descriptor
->Desc
== ACPI_ADDRESS_SPACE_DESCRIPTOR
) {
452 DEBUG ((EFI_D_ERROR
, "RootBridge[%d]:\n", RootBridgeIndex
++));
453 for (; Descriptor
->Desc
== ACPI_ADDRESS_SPACE_DESCRIPTOR
; Descriptor
++) {
454 ASSERT (Descriptor
->ResType
<
455 (sizeof (mPciHostBridgeLibAcpiAddressSpaceTypeStr
) /
456 sizeof (mPciHostBridgeLibAcpiAddressSpaceTypeStr
[0])
459 DEBUG ((EFI_D_ERROR
, " %s: Length/Alignment = 0x%lx / 0x%lx\n",
460 mPciHostBridgeLibAcpiAddressSpaceTypeStr
[Descriptor
->ResType
],
461 Descriptor
->AddrLen
, Descriptor
->AddrRangeMax
463 if (Descriptor
->ResType
== ACPI_ADDRESS_SPACE_TYPE_MEM
) {
464 DEBUG ((EFI_D_ERROR
, " Granularity/SpecificFlag = %ld / %02x%s\n",
465 Descriptor
->AddrSpaceGranularity
, Descriptor
->SpecificFlag
,
466 ((Descriptor
->SpecificFlag
&
467 EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE
468 ) != 0) ? L
" (Prefetchable)" : L
""
473 // Skip the END descriptor for root bridge
475 ASSERT (Descriptor
->Desc
== ACPI_END_TAG_DESCRIPTOR
);
476 Descriptor
= (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*)(
477 (EFI_ACPI_END_TAG_DESCRIPTOR
*)Descriptor
+ 1