]> git.proxmox.com Git - mirror_edk2.git/blob - ArmVirtPkg/Library/PlatformPeiLib/PlatformPeiLib.c
8b5b3dd5dc1cd93b9ba0cfa456a941de98e7ddef
[mirror_edk2.git] / ArmVirtPkg / Library / PlatformPeiLib / PlatformPeiLib.c
1 /** @file
2 *
3 * Copyright (c) 2011-2014, ARM Limited. All rights reserved.
4 * Copyright (c) 2014-2020, Linaro Limited. All rights reserved.
5 *
6 * SPDX-License-Identifier: BSD-2-Clause-Patent
7 *
8 **/
9
10 #include <PiPei.h>
11
12 #include <Library/MemoryAllocationLib.h>
13 #include <Library/DebugLib.h>
14 #include <Library/HobLib.h>
15 #include <Library/PcdLib.h>
16 #include <Library/PeiServicesLib.h>
17 #include <libfdt.h>
18
19 #include <Guid/EarlyPL011BaseAddress.h>
20 #include <Guid/FdtHob.h>
21
22 STATIC CONST EFI_PEI_PPI_DESCRIPTOR mTpm2DiscoveredPpi = {
23 EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
24 &gOvmfTpmDiscoveredPpiGuid,
25 NULL
26 };
27
28 STATIC CONST EFI_PEI_PPI_DESCRIPTOR mTpm2InitializationDonePpi = {
29 EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
30 &gPeiTpmInitializationDonePpiGuid,
31 NULL
32 };
33
34 EFI_STATUS
35 EFIAPI
36 PlatformPeim (
37 VOID
38 )
39 {
40 VOID *Base;
41 VOID *NewBase;
42 UINTN FdtSize;
43 UINTN FdtPages;
44 UINT64 *FdtHobData;
45 UINT64 *UartHobData;
46 INT32 Node, Prev;
47 INT32 Parent, Depth;
48 CONST CHAR8 *Compatible;
49 CONST CHAR8 *CompItem;
50 CONST CHAR8 *NodeStatus;
51 INT32 Len;
52 INT32 RangesLen;
53 INT32 StatusLen;
54 CONST UINT64 *RegProp;
55 CONST UINT32 *RangesProp;
56 UINT64 UartBase;
57 UINT64 TpmBase;
58 EFI_STATUS Status;
59
60 Base = (VOID*)(UINTN)PcdGet64 (PcdDeviceTreeInitialBaseAddress);
61 ASSERT (Base != NULL);
62 ASSERT (fdt_check_header (Base) == 0);
63
64 FdtSize = fdt_totalsize (Base) + PcdGet32 (PcdDeviceTreeAllocationPadding);
65 FdtPages = EFI_SIZE_TO_PAGES (FdtSize);
66 NewBase = AllocatePages (FdtPages);
67 ASSERT (NewBase != NULL);
68 fdt_open_into (Base, NewBase, EFI_PAGES_TO_SIZE (FdtPages));
69
70 FdtHobData = BuildGuidHob (&gFdtHobGuid, sizeof *FdtHobData);
71 ASSERT (FdtHobData != NULL);
72 *FdtHobData = (UINTN)NewBase;
73
74 UartHobData = BuildGuidHob (&gEarlyPL011BaseAddressGuid, sizeof *UartHobData);
75 ASSERT (UartHobData != NULL);
76 *UartHobData = 0;
77
78 TpmBase = 0;
79
80 for (Prev = Depth = 0;; Prev = Node) {
81 Node = fdt_next_node (Base, Prev, &Depth);
82 if (Node < 0) {
83 break;
84 }
85
86 if (Depth == 1) {
87 Parent = Node;
88 }
89
90 Compatible = fdt_getprop (Base, Node, "compatible", &Len);
91
92 //
93 // Iterate over the NULL-separated items in the compatible string
94 //
95 for (CompItem = Compatible; CompItem != NULL && CompItem < Compatible + Len;
96 CompItem += 1 + AsciiStrLen (CompItem)) {
97
98 if (AsciiStrCmp (CompItem, "arm,pl011") == 0) {
99 NodeStatus = fdt_getprop (Base, Node, "status", &StatusLen);
100 if (NodeStatus != NULL && AsciiStrCmp (NodeStatus, "okay") != 0) {
101 continue;
102 }
103
104 RegProp = fdt_getprop (Base, Node, "reg", &Len);
105 ASSERT (Len == 16);
106
107 UartBase = fdt64_to_cpu (ReadUnaligned64 (RegProp));
108
109 DEBUG ((EFI_D_INFO, "%a: PL011 UART @ 0x%lx\n", __FUNCTION__, UartBase));
110
111 *UartHobData = UartBase;
112 break;
113 } else if (FeaturePcdGet (PcdTpm2SupportEnabled) &&
114 AsciiStrCmp (CompItem, "tcg,tpm-tis-mmio") == 0) {
115
116 RegProp = fdt_getprop (Base, Node, "reg", &Len);
117 ASSERT (Len == 8 || Len == 16);
118 if (Len == 8) {
119 TpmBase = fdt32_to_cpu (RegProp[0]);
120 } else if (Len == 16) {
121 TpmBase = fdt64_to_cpu (ReadUnaligned64 ((UINT64 *)RegProp));
122 }
123
124 if (Depth > 1) {
125 //
126 // QEMU/mach-virt may put the TPM on the platform bus, in which case
127 // we have to take its 'ranges' property into account to translate the
128 // MMIO address. This consists of a <child base, parent base, size>
129 // tuple, where the child base and the size use the same number of
130 // cells as the 'reg' property above, and the parent base uses 2 cells
131 //
132 RangesProp = fdt_getprop (Base, Parent, "ranges", &RangesLen);
133 ASSERT (RangesProp != NULL);
134
135 //
136 // a plain 'ranges' attribute without a value implies a 1:1 mapping
137 //
138 if (RangesLen != 0) {
139 //
140 // assume a single translated range with 2 cells for the parent base
141 //
142 if (RangesLen != Len + 2 * sizeof (UINT32)) {
143 DEBUG ((DEBUG_WARN,
144 "%a: 'ranges' property has unexpected size %d\n",
145 __FUNCTION__, RangesLen));
146 break;
147 }
148
149 if (Len == 8) {
150 TpmBase -= fdt32_to_cpu (RangesProp[0]);
151 } else {
152 TpmBase -= fdt64_to_cpu (ReadUnaligned64 ((UINT64 *)RangesProp));
153 }
154
155 //
156 // advance RangesProp to the parent bus address
157 //
158 RangesProp = (UINT32 *)((UINT8 *)RangesProp + Len / 2);
159 TpmBase += fdt64_to_cpu (ReadUnaligned64 ((UINT64 *)RangesProp));
160 }
161 }
162 break;
163 }
164 }
165 }
166
167 if (FeaturePcdGet (PcdTpm2SupportEnabled)) {
168 if (TpmBase != 0) {
169 DEBUG ((DEBUG_INFO, "%a: TPM @ 0x%lx\n", __FUNCTION__, TpmBase));
170
171 Status = (EFI_STATUS)PcdSet64S (PcdTpmBaseAddress, TpmBase);
172 ASSERT_EFI_ERROR (Status);
173
174 Status = PeiServicesInstallPpi (&mTpm2DiscoveredPpi);
175 } else {
176 Status = PeiServicesInstallPpi (&mTpm2InitializationDonePpi);
177 }
178 ASSERT_EFI_ERROR (Status);
179 }
180
181 BuildFvHob (PcdGet64 (PcdFvBaseAddress), PcdGet32 (PcdFvSize));
182
183 return EFI_SUCCESS;
184 }