blob: 0ec9b74d97e0d895661bfac1870f4486b80dd23a [file] [log] [blame]
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +05301/* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08002 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
29#include "LocateDeviceTree.h"
Runmin Wange74d8042016-07-19 18:09:07 -070030#include "UpdateDeviceTree.h"
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +053031#include <Library/Board.h>
Jeevan Shriram17f173d2017-10-24 22:11:07 -070032#include <Library/BootLinux.h>
33#include <Library/PartitionTableUpdate.h>
34#include <Library/Rtic.h>
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080035// Variables to initialize board data
36
Jeevan Shriram17f173d2017-10-24 22:11:07 -070037STATIC int
38platform_dt_absolute_match (struct dt_entry *cur_dt_entry,
39 struct dt_entry_node *dt_list);
40STATIC struct dt_entry *
41platform_dt_match_best (struct dt_entry_node *dt_list);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080042
Mukesh Ojhaa1bd1f82017-05-25 19:04:21 +053043STATIC BOOLEAN DtboNeed = TRUE;
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +053044
lijuang6fa55c02018-06-22 19:19:26 +080045STATIC INT32 DtboIdx = INVALID_PTN;
46INT32 GetDtboIdx (VOID)
47{
48 return DtboIdx;
49}
50
lijuangf0bbcad2017-08-16 16:59:18 +080051BOOLEAN GetDtboNeeded (VOID)
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +053052{
Jeevan Shriram17f173d2017-10-24 22:11:07 -070053 return DtboNeed;
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +053054}
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080055/* Add function to allocate dt entry list, used for recording
56 * the entry which conform to platform_dt_absolute_match()
57 */
Jeevan Shriram17f173d2017-10-24 22:11:07 -070058static struct dt_entry_node *dt_entry_list_init (VOID)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080059{
Jeevan Shriram17f173d2017-10-24 22:11:07 -070060 struct dt_entry_node *dt_node_member = NULL;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080061
Jeevan Shriram17f173d2017-10-24 22:11:07 -070062 dt_node_member =
63 (struct dt_entry_node *)AllocatePool (sizeof (struct dt_entry_node));
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080064
Jeevan Shriram17f173d2017-10-24 22:11:07 -070065 if (!dt_node_member) {
66 DEBUG ((EFI_D_ERROR, "Failed to allocate memory for dt_node_member\n"));
67 return NULL;
68 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080069
Jeevan Shriram17f173d2017-10-24 22:11:07 -070070 list_clear_node (&dt_node_member->node);
71 dt_node_member->dt_entry_m =
72 (struct dt_entry *)AllocatePool (sizeof (struct dt_entry));
73 if (!dt_node_member->dt_entry_m) {
74 DEBUG ((EFI_D_ERROR,
75 "Failed to allocate memory for dt_node_member->dt_entry_m\n"));
76 FreePool (dt_node_member);
77 dt_node_member = NULL;
78 return NULL;
79 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080080
Jeevan Shriram17f173d2017-10-24 22:11:07 -070081 memset (dt_node_member->dt_entry_m, 0, sizeof (struct dt_entry));
82 return dt_node_member;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080083}
84
Jeevan Shriram17f173d2017-10-24 22:11:07 -070085static VOID
86insert_dt_entry_in_queue (struct dt_entry_node *dt_list,
87 struct dt_entry_node *dt_node_member)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080088{
Jeevan Shriram17f173d2017-10-24 22:11:07 -070089 list_add_tail (&dt_list->node, &dt_node_member->node);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080090}
91
Jeevan Shriram17f173d2017-10-24 22:11:07 -070092static VOID
93dt_entry_list_delete (struct dt_entry_node *dt_node_member)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080094{
Jeevan Shriram17f173d2017-10-24 22:11:07 -070095 if (list_in_list (&dt_node_member->node)) {
96 list_delete (&dt_node_member->node);
97 FreePool (dt_node_member->dt_entry_m);
98 dt_node_member->dt_entry_m = NULL;
99 FreePool (dt_node_member);
100 dt_node_member = NULL;
101 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800102}
103
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700104static BOOLEAN
105DeviceTreeCompatible (VOID *dtb,
106 UINT32 dtb_size,
107 struct dt_entry_node *dtb_list)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800108{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700109 int root_offset;
110 const VOID *prop = NULL;
111 const char *plat_prop = NULL;
112 const char *board_prop = NULL;
113 const char *pmic_prop = NULL;
114 char *model = NULL;
115 struct dt_entry *dt_entry_array = NULL;
116 struct board_id *board_data = NULL;
117 struct plat_id *platform_data = NULL;
118 struct pmic_id *pmic_data = NULL;
119 int len;
120 int len_board_id;
121 int len_plat_id;
122 int min_plat_id_len = 0;
123 int len_pmic_id;
124 UINT32 dtb_ver;
jianzhoue687ba82017-11-27 16:08:51 +0800125 UINT64 NumEntries = 0;
126 UINT64 i;
127 UINT32 j, k, n;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700128 UINT32 msm_data_count;
129 UINT32 board_data_count;
130 UINT32 pmic_data_count;
131 BOOLEAN Result = FALSE;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800132
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700133 root_offset = fdt_path_offset (dtb, "/");
134 if (root_offset < 0)
135 return FALSE;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800136
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700137 prop = fdt_getprop (dtb, root_offset, "model", &len);
138 if (prop && len > 0) {
139 model = (char *)AllocatePool (sizeof (char) * len);
140 if (!model) {
141 DEBUG ((EFI_D_ERROR, "Failed to allocate memory for model\n"));
142 return FALSE;
143 }
144 AsciiStrnCpyS (model, (sizeof (CHAR8) * len), prop, len);
145 } else {
146 DEBUG ((EFI_D_ERROR, "model does not exist in device tree\n"));
147 }
148 /* Find the pmic-id prop from DTB , if pmic-id is present then
149 * the DTB is version 3, otherwise find the board-id prop from DTB ,
150 * if board-id is present then the DTB is version 2 */
151 pmic_prop = (const char *)fdt_getprop (dtb, root_offset, "qcom,pmic-id",
152 &len_pmic_id);
153 board_prop = (const char *)fdt_getprop (dtb, root_offset, "qcom,board-id",
154 &len_board_id);
155 if (pmic_prop && (len_pmic_id > 0) && board_prop && (len_board_id > 0)) {
156 if ((len_pmic_id % PMIC_ID_SIZE) || (len_board_id % BOARD_ID_SIZE)) {
157 DEBUG ((EFI_D_ERROR, "qcom,pmic-id (%d) or qcom,board-id(%d) in device "
158 "tree is not a multiple of (%d %d)\n",
159 len_pmic_id, len_board_id, PMIC_ID_SIZE, BOARD_ID_SIZE));
160 goto Exit;
161 }
162 dtb_ver = DEV_TREE_VERSION_V3;
163 min_plat_id_len = PLAT_ID_SIZE;
164 } else if (board_prop && len_board_id > 0) {
165 if (len_board_id % BOARD_ID_SIZE) {
166 DEBUG ((EFI_D_ERROR,
167 "qcom,pmic-id (%d) in device tree is not a multiple of (%d)\n",
168 len_board_id, BOARD_ID_SIZE));
169 goto Exit;
170 }
171 dtb_ver = DEV_TREE_VERSION_V2;
172 min_plat_id_len = PLAT_ID_SIZE;
173 } else {
174 dtb_ver = DEV_TREE_VERSION_V1;
175 min_plat_id_len = DT_ENTRY_V1_SIZE;
176 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800177
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700178 /* Get the msm-id prop from DTB */
179 plat_prop =
180 (const char *)fdt_getprop (dtb, root_offset, "qcom,msm-id", &len_plat_id);
181 if (!plat_prop || len_plat_id <= 0) {
182 DEBUG ((EFI_D_VERBOSE, "qcom,msm-id entry not found\n"));
183 goto Exit;
184 } else if (len_plat_id % min_plat_id_len) {
185 DEBUG ((EFI_D_ERROR,
186 "qcom, msm-id in device tree is (%d) not a multiple of (%d)\n",
187 len_plat_id, min_plat_id_len));
188 goto Exit;
189 }
190 if (dtb_ver == DEV_TREE_VERSION_V2 || dtb_ver == DEV_TREE_VERSION_V3) {
191 board_data_count = (len_board_id / BOARD_ID_SIZE);
192 msm_data_count = (len_plat_id / PLAT_ID_SIZE);
193 /* If dtb version is v2.0, the pmic_data_count will be <= 0 */
194 pmic_data_count = (len_pmic_id / PMIC_ID_SIZE);
195
196 /* If we are using dtb v3.0, then we have split board, msm & pmic data in
197 * the DTB
198 * If we are using dtb v2.0, then we have split board & msmdata in the DTB
199 */
200 board_data = (struct board_id *)AllocatePool (
201 sizeof (struct board_id) * (len_board_id / BOARD_ID_SIZE));
202 if (!board_data) {
203 DEBUG ((EFI_D_ERROR, "Failed to allocate memory for board_data\n"));
204 goto Exit;
205 }
206
207 platform_data = (struct plat_id *)AllocatePool (
208 sizeof (struct plat_id) * (len_plat_id / PLAT_ID_SIZE));
209 if (!platform_data) {
210 DEBUG ((EFI_D_ERROR, "Failed to allocate memory for platform_data\n"));
211 goto Exit;
212 }
213 if (dtb_ver == DEV_TREE_VERSION_V3) {
214 pmic_data = (struct pmic_id *)AllocatePool (sizeof (struct pmic_id) *
215 (len_pmic_id / PMIC_ID_SIZE));
216 if (!pmic_data) {
217 DEBUG ((EFI_D_ERROR, "Failed to allocate memory for pmic_data\n"));
lijuang46df8442017-09-28 19:06:36 +0800218 goto Exit;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700219 }
220 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800221
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700222 /* Extract board data from DTB */
223 for (i = 0; i < board_data_count; i++) {
224 board_data[i].variant_id =
225 fdt32_to_cpu (((struct board_id *)board_prop)->variant_id);
226 board_data[i].platform_subtype =
227 fdt32_to_cpu (((struct board_id *)board_prop)->platform_subtype);
228 /* For V2/V3 version of DTBs we have platform version field as part
229 * of variant ID, in such case the subtype will be mentioned as 0x0
230 * As the qcom, board-id = <0xSSPMPmPH, 0x0>
231 * SS -- Subtype
232 * PM -- Platform major version
233 * Pm -- Platform minor version
234 * PH -- Platform hardware CDP/MTP
235 * In such case to make it compatible with LK algorithm move the subtype
236 * from variant_id to subtype field
237 */
238 if (board_data[i].platform_subtype == 0)
239 board_data[i].platform_subtype =
240 fdt32_to_cpu (((struct board_id *)board_prop)->variant_id) >> 0x18;
Vijay Kumar Pendoti1e847bd2016-11-21 15:29:13 +0530241
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700242 len_board_id -= sizeof (struct board_id);
243 board_prop += sizeof (struct board_id);
244 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800245
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700246 /* Extract platform data from DTB */
247 for (i = 0; i < msm_data_count; i++) {
248 platform_data[i].platform_id =
249 fdt32_to_cpu (((struct plat_id *)plat_prop)->platform_id);
250 platform_data[i].soc_rev =
251 fdt32_to_cpu (((struct plat_id *)plat_prop)->soc_rev);
252 len_plat_id -= sizeof (struct plat_id);
253 plat_prop += sizeof (struct plat_id);
254 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800255
jianzhoue687ba82017-11-27 16:08:51 +0800256 if ((pmic_data_count != 0) &&
257 (MAX_UINT64 / pmic_data_count < (msm_data_count * board_data_count))) {
258 DEBUG ((EFI_D_ERROR, "NumEntries exceeds MAX_UINT64\n"));
259 goto Exit;
260 }
261
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700262 if (dtb_ver == DEV_TREE_VERSION_V3 && pmic_prop) {
263 /* Extract pmic data from DTB */
264 for (i = 0; i < pmic_data_count; i++) {
265 pmic_data[i].pmic_version[0] =
266 fdt32_to_cpu (((struct pmic_id *)pmic_prop)->pmic_version[0]);
267 pmic_data[i].pmic_version[1] =
268 fdt32_to_cpu (((struct pmic_id *)pmic_prop)->pmic_version[1]);
269 pmic_data[i].pmic_version[2] =
270 fdt32_to_cpu (((struct pmic_id *)pmic_prop)->pmic_version[2]);
271 pmic_data[i].pmic_version[3] =
272 fdt32_to_cpu (((struct pmic_id *)pmic_prop)->pmic_version[3]);
273 len_pmic_id -= sizeof (struct pmic_id);
274 pmic_prop += sizeof (struct pmic_id);
275 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800276
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700277 /* We need to merge board & platform data into dt entry structure */
jianzhoue687ba82017-11-27 16:08:51 +0800278 NumEntries = (uint64_t)msm_data_count * board_data_count *
279 pmic_data_count;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700280 } else {
281 /* We need to merge board & platform data into dt entry structure */
jianzhoue687ba82017-11-27 16:08:51 +0800282 NumEntries = (uint64_t)msm_data_count * board_data_count;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700283 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800284
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700285 dt_entry_array = (struct dt_entry *)AllocatePool (sizeof (struct dt_entry) *
jianzhoue687ba82017-11-27 16:08:51 +0800286 NumEntries);
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700287 if (!dt_entry_array) {
288 DEBUG ((EFI_D_ERROR, "Failed to allocate memory for dt_entry_array\n"));
289 goto Exit;
290 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800291
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700292 /* If we have '<X>; <Y>; <Z>' as platform data & '<A>; <B>; <C>' as board
293 * data.
294 * Then dt entry should look like
295 * <X ,A >;<X, B>;<X, C>;
296 * <Y ,A >;<Y, B>;<Y, C>;
297 * <Z ,A >;<Z, B>;<Z, C>;
298 */
299 k = 0;
300 for (i = 0; i < msm_data_count; i++) {
301 for (j = 0; j < board_data_count; j++) {
302 if (dtb_ver == DEV_TREE_VERSION_V3 && pmic_prop) {
303 for (n = 0; n < pmic_data_count; n++) {
304 dt_entry_array[k].platform_id = platform_data[i].platform_id;
305 dt_entry_array[k].soc_rev = platform_data[i].soc_rev;
306 dt_entry_array[k].variant_id = board_data[j].variant_id;
307 dt_entry_array[k].board_hw_subtype = board_data[j].platform_subtype;
308 dt_entry_array[k].pmic_rev[0] = pmic_data[n].pmic_version[0];
309 dt_entry_array[k].pmic_rev[1] = pmic_data[n].pmic_version[1];
310 dt_entry_array[k].pmic_rev[2] = pmic_data[n].pmic_version[2];
311 dt_entry_array[k].pmic_rev[3] = pmic_data[n].pmic_version[3];
312 dt_entry_array[k].offset = (UINT64)dtb;
313 dt_entry_array[k].size = dtb_size;
314 k++;
315 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800316
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700317 } else {
318 dt_entry_array[k].platform_id = platform_data[i].platform_id;
319 dt_entry_array[k].soc_rev = platform_data[i].soc_rev;
320 dt_entry_array[k].variant_id = board_data[j].variant_id;
321 dt_entry_array[k].board_hw_subtype = board_data[j].platform_subtype;
322 dt_entry_array[k].pmic_rev[0] = BoardPmicTarget (0);
323 dt_entry_array[k].pmic_rev[1] = BoardPmicTarget (1);
324 dt_entry_array[k].pmic_rev[2] = BoardPmicTarget (2);
325 dt_entry_array[k].pmic_rev[3] = BoardPmicTarget (3);
326 dt_entry_array[k].offset = (UINT64)dtb;
327 dt_entry_array[k].size = dtb_size;
328 k++;
lijuang46df8442017-09-28 19:06:36 +0800329 }
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700330 }
331 }
332
jianzhoue687ba82017-11-27 16:08:51 +0800333 for (i = 0; i < NumEntries; i++) {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700334 if (platform_dt_absolute_match (&(dt_entry_array[i]), dtb_list)) {
335 DEBUG ((EFI_D_VERBOSE, "Device tree exact match the board: <0x%x 0x%x "
336 "0x%x 0x%x> == <0x%x 0x%x 0x%x 0x%x>\n",
337 dt_entry_array[i].platform_id, dt_entry_array[i].variant_id,
338 dt_entry_array[i].soc_rev, dt_entry_array[i].board_hw_subtype,
339 BoardPlatformRawChipId (), BoardPlatformType (),
340 BoardPlatformChipVersion (), BoardPlatformSubType ()));
341 } else {
342 DEBUG ((EFI_D_VERBOSE, "Device tree's msm_id doesn't match the board: "
343 "<0x%x 0x%x 0x%x 0x%x> != <0x%x 0x%x 0x%x "
344 "0x%x>\n",
345 dt_entry_array[i].platform_id, dt_entry_array[i].variant_id,
346 dt_entry_array[i].soc_rev, dt_entry_array[i].board_hw_subtype,
347 BoardPlatformRawChipId (), BoardPlatformType (),
348 BoardPlatformChipVersion (), BoardPlatformSubType ()));
349 }
350 }
351 Result = TRUE;
352 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800353
lijuang46df8442017-09-28 19:06:36 +0800354Exit:
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700355 if (board_data) {
356 FreePool (board_data);
357 board_data = NULL;
358 }
359 if (platform_data) {
360 FreePool (platform_data);
361 platform_data = NULL;
362 }
363 if (pmic_data) {
364 FreePool (pmic_data);
365 pmic_data = NULL;
366 }
367 if (dt_entry_array) {
368 FreePool (dt_entry_array);
369 dt_entry_array = NULL;
370 }
371 if (model) {
372 FreePool (model);
373 model = NULL;
374 }
lijuang46df8442017-09-28 19:06:36 +0800375
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700376 return Result;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800377}
378
379/*
380 * Will relocate the DTB to the tags addr if the device tree is found and return
381 * its address
382 *
383 * Arguments: kernel - Start address of the kernel loaded in RAM
384 * tags - Start address of the tags loaded in RAM
385 * kernel_size - Size of the kernel in bytes
386 *
387 * Return Value: DTB address : If appended device tree is found
388 * 'NULL' : Otherwise
389 */
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700390VOID *
391DeviceTreeAppended (VOID *kernel,
392 UINT32 kernel_size,
393 UINT32 dtb_offset,
394 VOID *tags)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800395{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700396 EFI_STATUS Status;
397 uintptr_t kernel_end = (uintptr_t)kernel + kernel_size;
398 VOID *dtb = NULL;
399 VOID *bestmatch_tag = NULL;
400 UINT64 RamdiskLoadAddr;
401 UINT64 BaseMemory = 0;
402 struct dt_entry *best_match_dt_entry = NULL;
403 UINT32 bestmatch_tag_size;
404 struct dt_entry_node *dt_entry_queue = NULL;
405 struct dt_entry_node *dt_node_tmp1 = NULL;
406 struct dt_entry_node *dt_node_tmp2 = NULL;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800407
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700408 /* Initialize the dtb entry node*/
409 dt_entry_queue =
410 (struct dt_entry_node *)AllocatePool (sizeof (struct dt_entry_node));
411 if (!dt_entry_queue) {
412 DEBUG ((EFI_D_ERROR, "Out of memory\n"));
413 return NULL;
414 }
Jeevan Shrirame07dd432016-08-22 11:05:06 -0700415
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700416 memset (dt_entry_queue, 0, sizeof (struct dt_entry_node));
417 list_initialize (&dt_entry_queue->node);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800418
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700419 if (!dtb_offset) {
420 DEBUG ((EFI_D_ERROR, "DTB offset is NULL\n"));
421 goto out;
422 }
Runmin Wang7db42962016-06-27 14:35:35 -0700423
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700424 if (((uintptr_t)kernel + (uintptr_t)dtb_offset) < (uintptr_t)kernel) {
425 goto out;
426 }
427 dtb = kernel + dtb_offset;
428 while (((uintptr_t)dtb + sizeof (struct fdt_header)) <
429 (uintptr_t)kernel_end) {
430 struct fdt_header dtb_hdr;
431 UINT32 dtb_size;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800432
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700433 /* the DTB could be unaligned, so extract the header,
434 * and operate on it separately */
435 gBS->CopyMem (&dtb_hdr, dtb, sizeof (struct fdt_header));
436 if (fdt_check_header ((const VOID *)&dtb_hdr) != 0 ||
437 fdt_check_header_ext ((VOID *)&dtb_hdr) != 0 ||
438 ((uintptr_t)dtb + (uintptr_t)fdt_totalsize ((const VOID *)&dtb_hdr) <
439 (uintptr_t)dtb) ||
440 ((uintptr_t)dtb + (uintptr_t)fdt_totalsize ((const VOID *)&dtb_hdr) >
441 (uintptr_t)kernel_end))
442 break;
443 dtb_size = fdt_totalsize (&dtb_hdr);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800444
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700445 if (!DeviceTreeCompatible (dtb, dtb_size, dt_entry_queue)) {
446 DEBUG ((EFI_D_VERBOSE, "Error while DTB parse continue with next DTB\n"));
447 if (!GetRticDtb (dtb))
448 DEBUG ((EFI_D_VERBOSE,
449 "Error while DTB parsing RTIC prop continue with next DTB\n"));
450 }
Mukesh Ojhaa1bd1f82017-05-25 19:04:21 +0530451
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700452 /* goto the next device tree if any */
453 dtb += dtb_size;
454 }
455 best_match_dt_entry = platform_dt_match_best (dt_entry_queue);
456 if (best_match_dt_entry) {
457 bestmatch_tag = (VOID *)best_match_dt_entry->offset;
458 bestmatch_tag_size = best_match_dt_entry->size;
459 DEBUG ((EFI_D_INFO, "Best match DTB tags "
460 "%u/%08x/0x%08x/%x/%x/%x/%x/%x/(offset)0x%08x/"
461 "(size)0x%08x\n",
462 best_match_dt_entry->platform_id, best_match_dt_entry->variant_id,
463 best_match_dt_entry->board_hw_subtype, best_match_dt_entry->soc_rev,
464 best_match_dt_entry->pmic_rev[0], best_match_dt_entry->pmic_rev[1],
465 best_match_dt_entry->pmic_rev[2], best_match_dt_entry->pmic_rev[3],
466 best_match_dt_entry->offset, best_match_dt_entry->size));
467 DEBUG ((EFI_D_INFO, "Using pmic info 0x%0x/0x%x/0x%x/0x%0x for device "
468 "0x%0x/0x%x/0x%x/0x%0x\n",
469 best_match_dt_entry->pmic_rev[0], best_match_dt_entry->pmic_rev[1],
470 best_match_dt_entry->pmic_rev[2], best_match_dt_entry->pmic_rev[3],
471 BoardPmicTarget (0), BoardPmicTarget (1), BoardPmicTarget (2),
472 BoardPmicTarget (3)));
473 }
474 /* free queue's memory */
475 list_for_every_entry (&dt_entry_queue->node, dt_node_tmp1, dt_node, node)
476 {
477 if (!dt_node_tmp1) {
478 DEBUG ((EFI_D_VERBOSE, "Current node is the end\n"));
479 break;
480 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800481
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700482 dt_node_tmp2 = (struct dt_entry_node *)dt_node_tmp1->node.prev;
483 dt_entry_list_delete (dt_node_tmp1);
484 dt_node_tmp1 = dt_node_tmp2;
485 }
lijuangf71a4842017-07-27 19:26:31 +0800486
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700487 if (bestmatch_tag) {
488 Status = BaseMem (&BaseMemory);
489 if (Status != EFI_SUCCESS) {
490 DEBUG ((EFI_D_ERROR, "Unable to find Base memory for DDR %r\n", Status));
491 FreePool (dt_entry_queue);
492 dt_entry_queue = NULL;
493 goto out;
494 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800495
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700496 RamdiskLoadAddr =
497 (EFI_PHYSICAL_ADDRESS) (BaseMemory | PcdGet32 (RamdiskLoadAddress));
498 if ((RamdiskLoadAddr - (UINT64)tags) > RamdiskLoadAddr) {
499 DEBUG ((EFI_D_ERROR, "Tags address is not valid\n"));
500 goto out;
501 }
502 if ((RamdiskLoadAddr - (UINT64)tags) < bestmatch_tag_size) {
503 DEBUG ((EFI_D_ERROR, "Tag size is over the limit\n"));
504 goto out;
505 }
506 gBS->CopyMem (tags, bestmatch_tag, bestmatch_tag_size);
507 /* clear out the old DTB magic so kernel doesn't find it */
508 *((UINT32 *)(kernel + dtb_offset)) = 0;
lijuang46df8442017-09-28 19:06:36 +0800509 FreePool (dt_entry_queue);
510 dt_entry_queue = NULL;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700511
512 return tags;
513 }
514
515 DEBUG (
516 (EFI_D_ERROR,
517 "DTB offset is incorrect, kernel image does not have appended DTB\n"));
518
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700519out:
520 FreePool (dt_entry_queue);
521 dt_entry_queue = NULL;
522 return NULL;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800523}
524
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700525STATIC BOOLEAN
526CheckAllBitsSet (UINT32 DtMatchVal)
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530527{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700528 return (DtMatchVal & ALL_BITS_SET) == (ALL_BITS_SET);
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530529}
530
Mukesh Ojhafa815352017-11-08 14:43:41 +0530531STATIC VOID
532ReadBestPmicMatch (CONST CHAR8 *PmicProp, UINT32 PmicEntCount,
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530533 PmicIdInfo *BestPmicInfo)
Mukesh Ojhafa815352017-11-08 14:43:41 +0530534{
535 UINT32 PmicEntIdx;
536 UINT32 Idx;
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530537 PmicIdInfo CurPmicInfo;
Mukesh Ojhafa815352017-11-08 14:43:41 +0530538
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530539 memset (BestPmicInfo, 0, sizeof (PmicIdInfo));
Mukesh Ojhafa815352017-11-08 14:43:41 +0530540 for (PmicEntIdx = 0; PmicEntIdx < PmicEntCount; PmicEntIdx++) {
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530541 memset (&CurPmicInfo, 0, sizeof (PmicIdInfo));
Mukesh Ojhafa815352017-11-08 14:43:41 +0530542 for (Idx = 0; Idx < MAX_PMIC_IDX; Idx++) {
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530543 CurPmicInfo.DtPmicModel[Idx] =
Mukesh Ojhafa815352017-11-08 14:43:41 +0530544 fdt32_to_cpu (((struct pmic_id *)PmicProp)->pmic_version[Idx]);
545
546 DEBUG ((EFI_D_VERBOSE, "pmic_data[%u].:%x\n", Idx,
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530547 CurPmicInfo.DtPmicModel[Idx]));
548 CurPmicInfo.DtPmicRev[Idx] =
549 CurPmicInfo.DtPmicModel[Idx] & PMIC_REV_MASK;
550 CurPmicInfo.DtPmicModel[Idx] =
551 CurPmicInfo.DtPmicModel[Idx] & PMIC_MODEL_MASK;
Mukesh Ojhafa815352017-11-08 14:43:41 +0530552
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530553 if ((CurPmicInfo.DtPmicModel[Idx]) ==
Mukesh Ojhafa815352017-11-08 14:43:41 +0530554 BoardPmicModel (Idx)) {
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530555 CurPmicInfo.DtMatchVal |=
Mukesh Ojhafa815352017-11-08 14:43:41 +0530556 BIT ((PMIC_MATCH_EXACT_MODEL_IDX0 + Idx * PMIC_SHIFT_IDX));
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530557 } else if (CurPmicInfo.DtPmicModel[Idx] == 0) {
558 CurPmicInfo.DtMatchVal |=
Mukesh Ojhafa815352017-11-08 14:43:41 +0530559 BIT ((PMIC_MATCH_DEFAULT_MODEL_IDX0 + Idx * PMIC_SHIFT_IDX));
560 } else {
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530561 CurPmicInfo.DtMatchVal = BIT (NONE_MATCH);
Mukesh Ojhafa815352017-11-08 14:43:41 +0530562 DEBUG ((EFI_D_VERBOSE, "Pmic model does not match\n"));
563 break;
564 }
565
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530566 if (CurPmicInfo.DtPmicRev[Idx] == (BoardPmicTarget (Idx)
Mukesh Ojhafa815352017-11-08 14:43:41 +0530567 & PMIC_REV_MASK)) {
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530568 CurPmicInfo.DtMatchVal |=
Mukesh Ojhafa815352017-11-08 14:43:41 +0530569 BIT ((PMIC_MATCH_EXACT_REV_IDX0 + Idx * PMIC_SHIFT_IDX));
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530570 } else if (CurPmicInfo.DtPmicRev[Idx] <
Mukesh Ojhafa815352017-11-08 14:43:41 +0530571 (BoardPmicTarget (Idx) & PMIC_REV_MASK)) {
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530572 CurPmicInfo.DtMatchVal |= BIT ((PMIC_MATCH_BEST_REV_IDX0 +
Mukesh Ojhafa815352017-11-08 14:43:41 +0530573 Idx * PMIC_SHIFT_IDX));
574 } else {
575 DEBUG ((EFI_D_VERBOSE, "Pmic revision does not match\n"));
576 break;
577 }
578 }
579
580 DEBUG ((EFI_D_VERBOSE, "BestPmicInfo.DtMatchVal : %x"
581 " CurPmicInfo[%u]->DtMatchVal : %x\n", BestPmicInfo->DtMatchVal,
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530582 PmicEntIdx, CurPmicInfo.DtMatchVal));
583 if (BestPmicInfo->DtMatchVal < CurPmicInfo.DtMatchVal) {
584 gBS->CopyMem (BestPmicInfo, &CurPmicInfo,
Mukesh Ojhafa815352017-11-08 14:43:41 +0530585 sizeof (struct PmicIdInfo));
586 } else if (BestPmicInfo->DtMatchVal ==
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530587 CurPmicInfo.DtMatchVal) {
588 if (BestPmicInfo->DtPmicRev[0] < CurPmicInfo.DtPmicRev[0]) {
589 gBS->CopyMem (BestPmicInfo, &CurPmicInfo,
Mukesh Ojhafa815352017-11-08 14:43:41 +0530590 sizeof (struct PmicIdInfo));
591 } else if (BestPmicInfo->DtPmicRev[1] <
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530592 CurPmicInfo.DtPmicRev[1]) {
593 gBS->CopyMem (BestPmicInfo, &CurPmicInfo,
Mukesh Ojhafa815352017-11-08 14:43:41 +0530594 sizeof (struct PmicIdInfo));
595 } else if (BestPmicInfo->DtPmicRev[2] <
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530596 CurPmicInfo.DtPmicRev[2]) {
597 gBS->CopyMem (BestPmicInfo, &CurPmicInfo,
Mukesh Ojhafa815352017-11-08 14:43:41 +0530598 sizeof (struct PmicIdInfo));
599 } else if (BestPmicInfo->DtPmicRev[3] <
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530600 CurPmicInfo.DtPmicRev[3]) {
601 gBS->CopyMem (BestPmicInfo, &CurPmicInfo,
Mukesh Ojhafa815352017-11-08 14:43:41 +0530602 sizeof (struct PmicIdInfo));
603 }
604 }
605
606 PmicProp += sizeof (struct pmic_id);
607 }
608}
609
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800610STATIC EFI_STATUS GetPlatformMatchDtb (DtInfo * CurDtbInfo,
611 CONST CHAR8 *PlatProp,
612 INT32 LenPlatId,
613 INT32 MinPlatIdLen)
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530614{
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530615
lijuangf6b1c5f2018-03-20 13:15:47 +0800616 if (CurDtbInfo == NULL) {
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800617 DEBUG ((EFI_D_VERBOSE, "Input parameters null\n"));
618 return EFI_INVALID_PARAMETER;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700619 }
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530620
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700621 if (PlatProp && (LenPlatId > 0) && (!(LenPlatId % MinPlatIdLen))) {
622 /*Compare msm-id of the dtb vs Board*/
623 CurDtbInfo->DtPlatformId =
624 fdt32_to_cpu (((struct plat_id *)PlatProp)->platform_id);
625 DEBUG ((EFI_D_VERBOSE, "Boardsocid = %x, Dtsocid = %x\n",
626 (BoardPlatformRawChipId () & SOC_MASK),
627 (CurDtbInfo->DtPlatformId & SOC_MASK)));
628 if ((BoardPlatformRawChipId () & SOC_MASK) ==
629 (CurDtbInfo->DtPlatformId & SOC_MASK)) {
630 CurDtbInfo->DtMatchVal |= BIT (SOC_MATCH);
631 } else {
Mukesh Ojha0eedc352018-02-14 17:42:30 +0530632 DEBUG ((EFI_D_VERBOSE, "qcom,msm-id does not match\n"));
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700633 /* If it's neither exact nor default match don't select dtb */
634 CurDtbInfo->DtMatchVal = BIT (NONE_MATCH);
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800635 return EFI_NOT_FOUND;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700636 }
637 /*Compare soc rev of the dtb vs Board*/
638 CurDtbInfo->DtSocRev = fdt32_to_cpu (((struct plat_id *)PlatProp)->soc_rev);
639 DEBUG ((EFI_D_VERBOSE, "BoardSocRev = %x, DtSocRev =%x\n",
640 BoardPlatformChipVersion (), CurDtbInfo->DtSocRev));
641 if (CurDtbInfo->DtSocRev == BoardPlatformChipVersion ()) {
642 CurDtbInfo->DtMatchVal |= BIT (VERSION_EXACT_MATCH);
643 } else if (CurDtbInfo->DtSocRev < BoardPlatformChipVersion ()) {
644 CurDtbInfo->DtMatchVal |= BIT (VERSION_BEST_MATCH);
645 } else if (CurDtbInfo->DtSocRev) {
Mukesh Ojha0eedc352018-02-14 17:42:30 +0530646 DEBUG ((EFI_D_VERBOSE, "soc version does not match\n"));
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700647 }
648 /*Compare Foundry Id of the dtb vs Board*/
649 CurDtbInfo->DtFoundryId =
650 fdt32_to_cpu (((struct plat_id *)PlatProp)->platform_id) &
651 FOUNDRY_ID_MASK;
652 DEBUG ((EFI_D_VERBOSE, "BoardFoundry = %x, DtFoundry = %x\n",
653 (BoardPlatformFoundryId () << PLATFORM_FOUNDRY_SHIFT),
654 CurDtbInfo->DtFoundryId));
655 if (CurDtbInfo->DtFoundryId ==
656 (BoardPlatformFoundryId () << PLATFORM_FOUNDRY_SHIFT)) {
657 CurDtbInfo->DtMatchVal |= BIT (FOUNDRYID_EXACT_MATCH);
658 } else if (CurDtbInfo->DtFoundryId == 0) {
659 CurDtbInfo->DtMatchVal |= BIT (FOUNDRYID_DEFAULT_MATCH);
660 } else {
Mukesh Ojha0eedc352018-02-14 17:42:30 +0530661 DEBUG ((EFI_D_VERBOSE, "soc foundry does not match\n"));
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700662 /* If it's neither exact nor default match don't select dtb */
663 CurDtbInfo->DtMatchVal = BIT (NONE_MATCH);
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800664 return EFI_NOT_FOUND;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700665 }
666 } else {
667 DEBUG ((EFI_D_VERBOSE, "qcom, msm-id does not exist (or) is"
668 " (%d) not a multiple of (%d)\n",
669 LenPlatId, MinPlatIdLen));
670 }
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800671 return EFI_SUCCESS;
672}
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530673
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800674STATIC EFI_STATUS GetBoardMatchDtb (DtInfo *CurDtbInfo,
675 CONST CHAR8 *BoardProp,
676 INT32 LenBoardId)
677{
lijuangf6b1c5f2018-03-20 13:15:47 +0800678 if (CurDtbInfo == NULL) {
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800679 DEBUG ((EFI_D_VERBOSE, "Input parameters null\n"));
680 return EFI_INVALID_PARAMETER;
681 }
682
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700683 if (BoardProp && (LenBoardId > 0) && (!(LenBoardId % BOARD_ID_SIZE))) {
684 CurDtbInfo->DtVariantId =
685 fdt32_to_cpu (((struct board_id *)BoardProp)->variant_id);
686 CurDtbInfo->DtPlatformSubtype =
687 fdt32_to_cpu (((struct board_id *)BoardProp)->platform_subtype);
688 if (CurDtbInfo->DtPlatformSubtype == 0) {
689 CurDtbInfo->DtPlatformSubtype =
690 fdt32_to_cpu (((struct board_id *)BoardProp)->variant_id) >>
691 PLATFORM_SUBTYPE_SHIFT_ID;
692 }
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530693
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700694 DEBUG ((EFI_D_VERBOSE, "BoardVariant = %x, DtVariant = %x\n",
695 BoardPlatformType (), CurDtbInfo->DtVariantId));
696 CurDtbInfo->DtVariantMajor = CurDtbInfo->DtVariantId & VARIANT_MAJOR_MASK;
697 CurDtbInfo->DtVariantMinor = CurDtbInfo->DtVariantId & VARIANT_MINOR_MASK;
698 CurDtbInfo->DtVariantId = CurDtbInfo->DtVariantId & VARIANT_MASK;
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530699
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700700 if (CurDtbInfo->DtVariantId == BoardPlatformType ()) {
701 CurDtbInfo->DtMatchVal |= BIT (VARIANT_MATCH);
702 } else if (CurDtbInfo->DtVariantId) {
703 DEBUG ((EFI_D_VERBOSE, "qcom,board-id does not"
704 " match\n"));
705 /* If it's neither exact nor default match don't select dtb */
706 CurDtbInfo->DtMatchVal = BIT (NONE_MATCH);
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800707 return EFI_NOT_FOUND;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700708 }
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530709
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700710 if (CurDtbInfo->DtVariantMajor == (BoardTargetId () & VARIANT_MAJOR_MASK)) {
711 CurDtbInfo->DtMatchVal |= BIT (VARIANT_MAJOR_EXACT_MATCH);
712 } else if (CurDtbInfo->DtVariantMajor <
713 (BoardTargetId () & VARIANT_MAJOR_MASK)) {
714 CurDtbInfo->DtMatchVal |= BIT (VARIANT_MAJOR_BEST_MATCH);
715 } else if (CurDtbInfo->DtVariantMajor) {
716 DEBUG ((EFI_D_VERBOSE, "qcom,board-id major version "
717 "does not match\n"));
718 }
Parth Dixitd4887652017-09-13 22:28:52 +0530719
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700720 if (CurDtbInfo->DtVariantMinor == (BoardTargetId () & VARIANT_MINOR_MASK)) {
721 CurDtbInfo->DtMatchVal |= BIT (VARIANT_MINOR_EXACT_MATCH);
722 } else if (CurDtbInfo->DtVariantMinor <
723 (BoardTargetId () & VARIANT_MINOR_MASK)) {
724 CurDtbInfo->DtMatchVal |= BIT (VARIANT_MINOR_BEST_MATCH);
725 } else if (CurDtbInfo->DtVariantMinor) {
726 DEBUG ((EFI_D_VERBOSE, "qcom,board-id minor version "
727 "does not match\n"));
728 }
Parth Dixitd4887652017-09-13 22:28:52 +0530729
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700730 DEBUG ((EFI_D_VERBOSE, "BoardSubtype = %x, DtSubType = %x\n",
731 BoardPlatformSubType (), CurDtbInfo->DtPlatformSubtype));
732 if (CurDtbInfo->DtPlatformSubtype == BoardPlatformSubType ()) {
733 CurDtbInfo->DtMatchVal |= BIT (SUBTYPE_EXACT_MATCH);
734 } else if (CurDtbInfo->DtPlatformSubtype == 0) {
735 CurDtbInfo->DtMatchVal |= BIT (SUBTYPE_DEFAULT_MATCH);
jianzhou254e1e02017-11-01 14:52:02 +0800736 } else {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700737 DEBUG ((EFI_D_VERBOSE, "subtype-id doesnot match\n"));
738 /* If it's neither exact nor default match don't select dtb */
739 CurDtbInfo->DtMatchVal = BIT (NONE_MATCH);
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800740 return EFI_NOT_FOUND;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700741 }
742 } else {
743 DEBUG ((EFI_D_VERBOSE, "qcom,board-id does not exist (or) (%d) "
744 "is not a multiple of (%d)\n",
745 LenBoardId, BOARD_ID_SIZE));
746 }
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800747 return EFI_SUCCESS;
748}
749
750/* Dt selection table for quick reference
751 | SNO | Dt Property | CDT Property | Exact | Best | Default |
752 |-----+---------------+-----------------+-------+------+---------+
753 | | qcom, msm-id | | | | |
754 | | | PlatformId | Y | N | N |
755 | | | SocRev | N | Y | N |
756 | | | FoundryId | Y | N | 0 |
757 | | qcom,board-id | | | | |
758 | | | VariantId | Y | N | N |
759 | | | VariantMajor | N | Y | N |
760 | | | VariantMinor | N | Y | N |
761 | | | PlatformSubtype | Y | N | 0 |
762 | | qcom,pmic-id | | | | |
763 | | | PmicModelId | Y | N | 0 |
764 | | | PmicMetalRev | N | Y | N |
765 | | | PmicLayerRev | N | Y | N |
766 | | | PmicVariantRev | N | Y | N |
767*/
lijuang6fa55c02018-06-22 19:19:26 +0800768STATIC BOOLEAN
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800769ReadDtbFindMatch (DtInfo *CurDtbInfo, DtInfo *BestDtbInfo, UINT32 ExactMatch)
770{
771 EFI_STATUS Status;
772 CONST CHAR8 *PlatProp = NULL;
773 CONST CHAR8 *BoardProp = NULL;
774 CONST CHAR8 *PmicProp = NULL;
775 INT32 LenBoardId;
776 INT32 LenPlatId;
777 INT32 LenPmicId;
778 INT32 MinPlatIdLen = PLAT_ID_SIZE;
779 INT32 RootOffset = 0;
780 VOID *Dtb = CurDtbInfo->Dtb;
781 UINT32 Idx;
782 UINT32 PmicEntCount;
Bhanuprakash Modem64333302018-09-10 18:58:11 +0530783 UINT32 MsmDataCount;
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800784 PmicIdInfo BestPmicInfo;
lijuang6fa55c02018-06-22 19:19:26 +0800785 BOOLEAN FindBestMatch = FALSE;
Bhanuprakash Modem64333302018-09-10 18:58:11 +0530786 DtInfo TempDtbInfo = *CurDtbInfo;
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800787
788 memset (&BestPmicInfo, 0, sizeof (PmicIdInfo));
789 /*Ensure MatchVal to 0 initially*/
790 CurDtbInfo->DtMatchVal = 0;
791 RootOffset = fdt_path_offset (Dtb, "/");
792 if (RootOffset < 0) {
793 DEBUG ((EFI_D_ERROR, "Unable to locate root node\n"));
lijuang6fa55c02018-06-22 19:19:26 +0800794 return FALSE;
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800795 }
796
797 /* Get the msm-id prop from DTB */
798 PlatProp = (CONST CHAR8 *)fdt_getprop (Dtb, RootOffset, "qcom,msm-id",
799 &LenPlatId);
Bhanuprakash Modem64333302018-09-10 18:58:11 +0530800 if (PlatProp &&
801 (LenPlatId > 0) &&
802 (!(LenPlatId % MinPlatIdLen))) {
803 /*
804 For Multiple soc-id's, save the best SocRev match DT in temp and search
805 for the exact match. If exact match is not found, use best match DT from
806 temp.
807 */
808 MsmDataCount = (LenPlatId / MinPlatIdLen);
809
810 /* Ensure to reset the match value */
811 TempDtbInfo.DtMatchVal = NONE_MATCH;
812
813 for (Idx = 0; Idx < MsmDataCount; Idx++) {
814 /* Ensure MatchVal should be 0 for every match */
815 CurDtbInfo->DtMatchVal = NONE_MATCH;
816 Status = GetPlatformMatchDtb (CurDtbInfo,
817 PlatProp,
818 LenPlatId,
819 MinPlatIdLen);
820 if (Status == EFI_SUCCESS &&
821 CurDtbInfo->DtMatchVal > TempDtbInfo.DtMatchVal) {
822 TempDtbInfo = *CurDtbInfo;
823 }
824 LenPlatId -= PLAT_ID_SIZE;
825 PlatProp += PLAT_ID_SIZE;
826 }
827
828 *CurDtbInfo = TempDtbInfo;
829
830 if (CurDtbInfo->DtMatchVal == NONE_MATCH) {
831 DEBUG ((EFI_D_VERBOSE, "Platform dt prop search failed.\n"));
832 goto cleanup;
833 }
834 } else {
835 DEBUG ((EFI_D_VERBOSE, "qcom, msm-id does not exist (or) is"
836 " (%d) not a multiple of (%d)\n",
837 LenPlatId, MinPlatIdLen));
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800838 }
839
840 /* Get the properties like variant id, subtype from Dtb then compare the
841 * dtb vs Board*/
842 BoardProp = (CONST CHAR8 *)fdt_getprop (Dtb, RootOffset, "qcom,board-id",
843 &LenBoardId);
844 Status = GetBoardMatchDtb (CurDtbInfo, BoardProp, LenBoardId);
845 if (Status != EFI_SUCCESS) {
Mukesh Ojha0eedc352018-02-14 17:42:30 +0530846 DEBUG ((EFI_D_VERBOSE, "Board dt prop search failed.\n"));
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800847 goto cleanup;
848 }
Parth Dixitd4887652017-09-13 22:28:52 +0530849
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700850 /*Get the pmic property from Dtb then compare the dtb vs Board*/
851 PmicProp =
852 (CONST CHAR8 *)fdt_getprop (Dtb, RootOffset, "qcom,pmic-id", &LenPmicId);
853 if ((PmicProp) && (LenPmicId > 0) && (!(LenPmicId % PMIC_ID_SIZE))) {
Mukesh Ojhafa815352017-11-08 14:43:41 +0530854 PmicEntCount = LenPmicId / PMIC_ID_SIZE;
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530855 /* Get the best match pmic */
856 ReadBestPmicMatch (PmicProp, PmicEntCount, &BestPmicInfo);
Mukesh Ojhafa815352017-11-08 14:43:41 +0530857 CurDtbInfo->DtMatchVal |= BestPmicInfo.DtMatchVal;
858 for (Idx = 0; Idx < MAX_PMIC_IDX; Idx++) {
859 CurDtbInfo->DtPmicModel[Idx] = BestPmicInfo.DtPmicModel[Idx];
860 CurDtbInfo->DtPmicRev[Idx] = BestPmicInfo.DtPmicRev[Idx];
861 }
862
863 DEBUG ((EFI_D_VERBOSE, "CurDtbInfo->DtMatchVal : %x "
864 "BestPmicInfo.DtMatchVal :%x\n", CurDtbInfo->DtMatchVal,
865 BestPmicInfo.DtMatchVal));
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700866 } else {
867 DEBUG ((EFI_D_VERBOSE, "qcom,pmic-id does not exit (or) is (%d)"
868 " not a multiple of (%d)\n",
869 LenPmicId, PMIC_ID_SIZE));
870 }
Parth Dixitd4887652017-09-13 22:28:52 +0530871
872cleanup:
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700873 if (CurDtbInfo->DtMatchVal & BIT (ExactMatch)) {
874 if (BestDtbInfo->DtMatchVal < CurDtbInfo->DtMatchVal) {
875 gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo));
lijuang6fa55c02018-06-22 19:19:26 +0800876 FindBestMatch = TRUE;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700877 } else if (BestDtbInfo->DtMatchVal == CurDtbInfo->DtMatchVal) {
lijuang6fa55c02018-06-22 19:19:26 +0800878 FindBestMatch = TRUE;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700879 if (BestDtbInfo->DtSocRev < CurDtbInfo->DtSocRev) {
880 gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo));
881 } else if (BestDtbInfo->DtVariantMajor < CurDtbInfo->DtVariantMajor) {
882 gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo));
883 } else if (BestDtbInfo->DtVariantMinor < CurDtbInfo->DtVariantMinor) {
884 gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo));
885 } else if (BestDtbInfo->DtPmicRev[0] < CurDtbInfo->DtPmicRev[0]) {
886 gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo));
887 } else if (BestDtbInfo->DtPmicRev[1] < CurDtbInfo->DtPmicRev[1]) {
888 gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo));
889 } else if (BestDtbInfo->DtPmicRev[2] < CurDtbInfo->DtPmicRev[2]) {
890 gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo));
891 } else if (BestDtbInfo->DtPmicRev[3] < CurDtbInfo->DtPmicRev[3]) {
892 gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo));
lijuang6fa55c02018-06-22 19:19:26 +0800893 } else {
894 FindBestMatch = FALSE;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700895 }
896 }
897 }
lijuang6fa55c02018-06-22 19:19:26 +0800898
899 return FindBestMatch;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700900}
901
902VOID *
903GetSocDtb (VOID *Kernel, UINT32 KernelSize, UINT32 DtbOffset, VOID *DtbLoadAddr)
904{
905 uintptr_t KernelEnd = (uintptr_t)Kernel + KernelSize;
906 VOID *Dtb = NULL;
907 struct fdt_header DtbHdr;
908 UINT32 DtbSize = 0;
909 DtInfo CurDtbInfo = {0};
910 DtInfo BestDtbInfo = {0};
911 if (!DtbOffset) {
912 DEBUG ((EFI_D_ERROR, "DTB offset is NULL\n"));
913 return NULL;
914 }
915
916 if (((uintptr_t)Kernel + (uintptr_t)DtbOffset) < (uintptr_t)Kernel) {
917 return NULL;
918 }
919 Dtb = Kernel + DtbOffset;
920 while (((uintptr_t)Dtb + sizeof (struct fdt_header)) < (uintptr_t)KernelEnd) {
921 /* the DTB could be unaligned, so extract the header,
922 * and operate on it separately */
923 gBS->CopyMem (&DtbHdr, Dtb, sizeof (struct fdt_header));
924 DtbSize = fdt_totalsize ((const VOID *)&DtbHdr);
925 if (fdt_check_header ((const VOID *)&DtbHdr) != 0 ||
926 fdt_check_header_ext ((VOID *)&DtbHdr) != 0 ||
927 ((uintptr_t)Dtb + DtbSize < (uintptr_t)Dtb) ||
928 ((uintptr_t)Dtb + DtbSize > (uintptr_t)KernelEnd))
929 break;
930
931 CurDtbInfo.Dtb = Dtb;
932 ReadDtbFindMatch (&CurDtbInfo, &BestDtbInfo, SOC_MATCH);
933 if (CurDtbInfo.DtMatchVal) {
934 if (CurDtbInfo.DtMatchVal & BIT (SOC_MATCH)) {
935 if (CheckAllBitsSet (CurDtbInfo.DtMatchVal)) {
936 DEBUG ((EFI_D_VERBOSE, "Exact DTB match"
937 " found. DTBO search is not "
938 "required\n"));
939 DtboNeed = FALSE;
Parth Dixitd4887652017-09-13 22:28:52 +0530940 }
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700941 }
942 } else {
943 if (!GetRticDtb (Dtb)) {
944 DEBUG ((EFI_D_VERBOSE, "Error while DTB parsing"
945 " RTIC prop continue with next DTB\n"));
946 }
947 }
948
949 DEBUG ((EFI_D_VERBOSE, "Bestmatch = %x\n", BestDtbInfo.DtMatchVal));
950 Dtb += DtbSize;
951 }
952
953 if (!BestDtbInfo.Dtb) {
954 DEBUG ((EFI_D_ERROR, "No match found for Soc Dtb type\n"));
955 return NULL;
956 }
957
958 return BestDtbInfo.Dtb;
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530959}
960
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700961VOID *
962GetBoardDtb (BootInfo *Info, VOID *DtboImgBuffer)
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530963{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700964 struct DtboTableHdr *DtboTableHdr = DtboImgBuffer;
965 struct DtboTableEntry *DtboTableEntry = NULL;
966 UINT32 DtboCount = 0;
967 VOID *BoardDtb = NULL;
968 UINT32 DtboTableEntriesCount = 0;
969 UINT32 FirstDtboTableEntryOffset = 0;
970 DtInfo CurDtbInfo = {0};
971 DtInfo BestDtbInfo = {0};
lijuang6fa55c02018-06-22 19:19:26 +0800972 BOOLEAN FindBestDtb = FALSE;
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530973
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700974 if (!DtboImgBuffer) {
975 DEBUG ((EFI_D_ERROR, "Dtbo Img buffer is NULL\n"));
976 return NULL;
977 }
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530978
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700979 FirstDtboTableEntryOffset = fdt32_to_cpu (DtboTableHdr->DtEntryOffset);
980 if (CHECK_ADD64 ((UINT64)DtboImgBuffer, FirstDtboTableEntryOffset)) {
981 DEBUG ((EFI_D_ERROR, "Integer overflow deteced with Dtbo address\n"));
982 return NULL;
983 }
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530984
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700985 DtboTableEntry =
986 (struct DtboTableEntry *)(DtboImgBuffer + FirstDtboTableEntryOffset);
987 if (!DtboTableEntry) {
988 DEBUG ((EFI_D_ERROR, "No proper DtTable\n"));
989 return NULL;
990 }
Mukesh Ojhaa1bd1f82017-05-25 19:04:21 +0530991
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700992 DtboTableEntriesCount = fdt32_to_cpu (DtboTableHdr->DtEntryCount);
993 for (DtboCount = 0; DtboCount < DtboTableEntriesCount; DtboCount++) {
994 if (CHECK_ADD64 ((UINT64)DtboImgBuffer,
995 fdt32_to_cpu (DtboTableEntry->DtOffset))) {
Mukesh Ojha82911f82018-04-30 16:45:07 +0530996 DEBUG ((EFI_D_ERROR, "Integer overflow detected with Dtbo address\n"));
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700997 return NULL;
998 }
999 BoardDtb = DtboImgBuffer + fdt32_to_cpu (DtboTableEntry->DtOffset);
1000 if (fdt_check_header (BoardDtb) || fdt_check_header_ext (BoardDtb)) {
1001 DEBUG ((EFI_D_ERROR, "No Valid Dtb\n"));
1002 break;
1003 }
Mukesh Ojhaa1bd1f82017-05-25 19:04:21 +05301004
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001005 CurDtbInfo.Dtb = BoardDtb;
lijuang6fa55c02018-06-22 19:19:26 +08001006 FindBestDtb = ReadDtbFindMatch (&CurDtbInfo, &BestDtbInfo, VARIANT_MATCH);
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001007 DEBUG ((EFI_D_VERBOSE, "Dtbo count = %u LocalBoardDtMatch = %x"
1008 "\n",
1009 DtboCount, CurDtbInfo.DtMatchVal));
lijuang6fa55c02018-06-22 19:19:26 +08001010
1011 if (FindBestDtb) {
1012 DtboIdx = DtboCount;
1013 }
1014
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001015 DtboTableEntry++;
1016 }
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +05301017
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001018 if (!BestDtbInfo.Dtb) {
1019 DEBUG ((EFI_D_ERROR, "Unable to find the Board Dtb\n"));
1020 return NULL;
1021 }
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +05301022
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001023 return BestDtbInfo.Dtb;
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +05301024}
1025
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001026/* Returns 0 if the device tree is valid. */
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001027int
1028DeviceTreeValidate (UINT8 *DeviceTreeBuff,
1029 UINT32 PageSize,
1030 UINT32 *DeviceTreeSize)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001031{
lijuangc336a292017-08-22 19:48:49 +08001032 UINT32 dt_entry_size;
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001033 UINT64 hdr_size;
1034 struct dt_table *table;
1035 if (DeviceTreeSize) {
1036 table = (struct dt_table *)DeviceTreeBuff;
1037 if (table->magic != DEV_TREE_MAGIC) {
1038 // bad magic in device tree table
1039 return -1;
1040 }
1041 if (table->version == DEV_TREE_VERSION_V1) {
1042 dt_entry_size = sizeof (struct dt_entry_v1);
1043 } else if (table->version == DEV_TREE_VERSION_V2) {
1044 dt_entry_size = sizeof (struct dt_entry_v2);
1045 } else if (table->version == DEV_TREE_VERSION_V3) {
1046 dt_entry_size = sizeof (struct dt_entry);
1047 } else {
1048 // unsupported dt version
1049 return -1;
1050 }
1051 hdr_size =
1052 ((UINT64)table->num_entries * dt_entry_size) + DEV_TREE_HEADER_SIZE;
1053 // hdr_size = ROUNDUP(hdr_size, PageSize);
1054 hdr_size = EFI_SIZE_TO_PAGES (hdr_size);
1055 if (hdr_size > MAX_UINT64)
1056 return -1;
1057 else
1058 *DeviceTreeSize = hdr_size & MAX_UINT64;
1059 // dt_entry_ptr = (struct dt_entry *)((CHAR8 *)table +
1060 // DEV_TREE_HEADER_SIZE);
1061 // table_ptr = dt_entry_ptr;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001062
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001063 DEBUG ((EFI_D_ERROR, "DT Total number of entries: %d, DTB version: %d\n",
1064 table->num_entries, table->version));
1065 }
1066 return 0;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001067}
1068
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001069STATIC int
1070platform_dt_absolute_match (struct dt_entry *cur_dt_entry,
1071 struct dt_entry_node *dt_list)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001072{
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001073 UINT32 cur_dt_hw_platform;
1074 UINT32 cur_dt_hw_subtype;
1075 UINT32 cur_dt_msm_id;
1076 dt_node *dt_node_tmp = NULL;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001077
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001078 /* Platform-id
1079 * bit no |31 24|23 16|15 0|
1080 * |reserved|foundry-id|msm-id|
1081 */
1082 cur_dt_msm_id = (cur_dt_entry->platform_id & 0x0000ffff);
1083 cur_dt_hw_platform = (cur_dt_entry->variant_id & 0x000000ff);
1084 cur_dt_hw_subtype = (cur_dt_entry->board_hw_subtype & 0xff);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001085
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001086 /* 1. must match the msm_id, platform_hw_id, platform_subtype and DDR size
1087 * soc, board major/minor, pmic major/minor must less than board info
1088 * 2. find the matched DTB then return 1
1089 * 3. otherwise return 0
1090 */
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001091
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001092 if ((cur_dt_msm_id == (BoardPlatformRawChipId () & 0x0000ffff)) &&
1093 (cur_dt_hw_platform == BoardPlatformType ()) &&
1094 (cur_dt_hw_subtype == BoardPlatformSubType ()) &&
1095 (cur_dt_entry->soc_rev <= BoardPlatformChipVersion ()) &&
1096 ((cur_dt_entry->variant_id & 0x00ffff00) <=
1097 (BoardTargetId () & 0x00ffff00)) &&
1098 (cur_dt_entry->pmic_rev[0] <= BoardPmicTarget (0)) &&
1099 (cur_dt_entry->pmic_rev[1] <= BoardPmicTarget (1)) &&
1100 (cur_dt_entry->pmic_rev[2] <= BoardPmicTarget (2)) &&
1101 (cur_dt_entry->pmic_rev[3] <= BoardPmicTarget (3))) {
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001102
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001103 dt_node_tmp = dt_entry_list_init ();
1104 if (!dt_node_tmp) {
1105 DEBUG ((EFI_D_ERROR, "dt_node_tmp is NULL\n"));
1106 return 0;
1107 }
Vijay Kumar Pendoti1e847bd2016-11-21 15:29:13 +05301108
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001109 gBS->CopyMem ((VOID *)dt_node_tmp->dt_entry_m, (VOID *)cur_dt_entry,
1110 sizeof (struct dt_entry));
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001111
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001112 DEBUG (
1113 (EFI_D_VERBOSE,
1114 "Add DTB entry 0x%x/%08x/0x%08x/0x%x/0x%x/0x%x/0x%x/0x%x/0x%x/0x%x\n",
1115 dt_node_tmp->dt_entry_m->platform_id,
1116 dt_node_tmp->dt_entry_m->variant_id,
1117 dt_node_tmp->dt_entry_m->board_hw_subtype,
1118 dt_node_tmp->dt_entry_m->soc_rev, dt_node_tmp->dt_entry_m->pmic_rev[0],
1119 dt_node_tmp->dt_entry_m->pmic_rev[1],
1120 dt_node_tmp->dt_entry_m->pmic_rev[2],
1121 dt_node_tmp->dt_entry_m->pmic_rev[3], dt_node_tmp->dt_entry_m->offset,
1122 dt_node_tmp->dt_entry_m->size));
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001123
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001124 insert_dt_entry_in_queue (dt_list, dt_node_tmp);
1125 return 1;
1126 }
1127 return 0;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001128}
1129
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001130int
1131platform_dt_absolute_compat_match (struct dt_entry_node *dt_list,
1132 UINT32 dtb_info)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001133{
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001134 struct dt_entry_node *dt_node_tmp1 = NULL;
1135 struct dt_entry_node *dt_node_tmp2 = NULL;
1136 UINT32 current_info = 0;
1137 UINT32 board_info = 0;
1138 UINT32 best_info = 0;
1139 UINT32 current_pmic_model[4] = {0, 0, 0, 0};
1140 UINT32 board_pmic_model[4] = {0, 0, 0, 0};
1141 UINT32 best_pmic_model[4] = {0, 0, 0, 0};
1142 UINT32 delete_current_dt = 0;
1143 UINT32 i;
1144
1145 /* start to select the exact entry
1146 * default to exact match 0, if find current DTB entry info is the same as
1147 * board info,
1148 * then exact match board info.
1149 */
1150 list_for_every_entry (&dt_list->node, dt_node_tmp1, dt_node, node)
1151 {
1152 if (!dt_node_tmp1) {
1153 DEBUG ((EFI_D_ERROR, "Current node is the end\n"));
1154 break;
1155 }
1156 switch (dtb_info) {
1157 case DTB_FOUNDRY:
1158 current_info = ((dt_node_tmp1->dt_entry_m->platform_id) & 0x00ff0000);
1159 board_info = BoardPlatformFoundryId () << 16;
1160 break;
1161 case DTB_PMIC_MODEL:
1162 for (i = 0; i < 4; i++) {
1163 current_pmic_model[i] = (dt_node_tmp1->dt_entry_m->pmic_rev[i] & 0xff);
1164 board_pmic_model[i] = BoardPmicModel (i);
1165 }
1166 break;
1167 default:
1168 DEBUG ((EFI_D_ERROR,
1169 "ERROR: Unsupported version (%d) in dt node check \n", dtb_info));
1170 return 0;
1171 }
1172
1173 if (dtb_info == DTB_PMIC_MODEL) {
1174 if ((current_pmic_model[0] == board_pmic_model[0]) &&
1175 (current_pmic_model[1] == board_pmic_model[1]) &&
1176 (current_pmic_model[2] == board_pmic_model[2]) &&
1177 (current_pmic_model[3] == board_pmic_model[3])) {
1178
1179 for (i = 0; i < 4; i++) {
1180 best_pmic_model[i] = current_pmic_model[i];
1181 }
1182 break;
1183 }
1184 } else {
1185 if (current_info == board_info) {
1186 best_info = current_info;
1187 break;
1188 }
1189 }
1190 }
1191
1192 list_for_every_entry (&dt_list->node, dt_node_tmp1, dt_node, node)
1193 {
1194 if (!dt_node_tmp1) {
1195 DEBUG ((EFI_D_ERROR, "Current node is the end\n"));
1196 break;
1197 }
1198 switch (dtb_info) {
1199 case DTB_FOUNDRY:
1200 current_info = ((dt_node_tmp1->dt_entry_m->platform_id) & 0x00ff0000);
1201 break;
1202 case DTB_PMIC_MODEL:
1203 for (i = 0; i < 4; i++) {
1204 current_pmic_model[i] = (dt_node_tmp1->dt_entry_m->pmic_rev[i] & 0xff);
1205 }
1206 break;
1207 default:
1208 DEBUG ((EFI_D_ERROR,
1209 "ERROR: Unsupported version (%d) in dt node check \n", dtb_info));
1210 return 0;
1211 }
1212
1213 if (dtb_info == DTB_PMIC_MODEL) {
1214 if ((current_pmic_model[0] != best_pmic_model[0]) ||
1215 (current_pmic_model[1] != best_pmic_model[1]) ||
1216 (current_pmic_model[2] != best_pmic_model[2]) ||
1217 (current_pmic_model[3] != best_pmic_model[3])) {
1218
1219 delete_current_dt = 1;
1220 }
1221 } else {
1222 if (current_info != best_info) {
1223 delete_current_dt = 1;
1224 }
1225 }
1226
1227 if (delete_current_dt) {
1228 DEBUG (
1229 (EFI_D_VERBOSE,
1230 "Delete don't fit DTB entry %u/%08x/0x%08x/%x/%x/%x/%x/%x/%x/%x\n",
1231 dt_node_tmp1->dt_entry_m->platform_id,
1232 dt_node_tmp1->dt_entry_m->variant_id,
1233 dt_node_tmp1->dt_entry_m->board_hw_subtype,
1234 dt_node_tmp1->dt_entry_m->soc_rev,
1235 dt_node_tmp1->dt_entry_m->pmic_rev[0],
1236 dt_node_tmp1->dt_entry_m->pmic_rev[1],
1237 dt_node_tmp1->dt_entry_m->pmic_rev[2],
1238 dt_node_tmp1->dt_entry_m->pmic_rev[3],
1239 dt_node_tmp1->dt_entry_m->offset, dt_node_tmp1->dt_entry_m->size));
1240
1241 dt_node_tmp2 = (struct dt_entry_node *)dt_node_tmp1->node.prev;
1242 dt_entry_list_delete (dt_node_tmp1);
1243 dt_node_tmp1 = dt_node_tmp2;
1244 delete_current_dt = 0;
1245 }
1246 }
1247
1248 return 1;
1249}
1250
1251int
1252update_dtb_entry_node (struct dt_entry_node *dt_list, UINT32 dtb_info)
1253{
1254 struct dt_entry_node *dt_node_tmp1 = NULL;
1255 struct dt_entry_node *dt_node_tmp2 = NULL;
1256 UINT32 current_info = 0;
1257 UINT32 board_info = 0;
1258 UINT32 best_info = 0;
1259
1260 /* start to select the best entry*/
1261 list_for_every_entry (&dt_list->node, dt_node_tmp1, dt_node, node)
1262 {
1263 if (!dt_node_tmp1) {
1264 DEBUG ((EFI_D_ERROR, "Current node is the end\n"));
1265 break;
1266 }
1267 switch (dtb_info) {
1268 case DTB_SOC:
1269 current_info = dt_node_tmp1->dt_entry_m->soc_rev;
1270 board_info = BoardPlatformChipVersion ();
1271 break;
1272 case DTB_MAJOR_MINOR:
1273 current_info = ((dt_node_tmp1->dt_entry_m->variant_id) & 0x00ffff00);
1274 board_info = BoardTargetId () & 0x00ffff00;
1275 break;
1276 case DTB_PMIC0:
1277 current_info = dt_node_tmp1->dt_entry_m->pmic_rev[0];
1278 board_info = BoardPmicTarget (0);
1279 break;
1280 case DTB_PMIC1:
1281 current_info = dt_node_tmp1->dt_entry_m->pmic_rev[1];
1282 board_info = BoardPmicTarget (1);
1283 break;
1284 case DTB_PMIC2:
1285 current_info = dt_node_tmp1->dt_entry_m->pmic_rev[2];
1286 board_info = BoardPmicTarget (2);
1287 break;
1288 case DTB_PMIC3:
1289 current_info = dt_node_tmp1->dt_entry_m->pmic_rev[3];
1290 board_info = BoardPmicTarget (3);
1291 break;
1292 default:
1293 DEBUG ((EFI_D_ERROR,
1294 "ERROR: Unsupported version (%d) in dt node check \n", dtb_info));
1295 return 0;
1296 }
1297
1298 if (current_info == board_info) {
1299 best_info = current_info;
1300 break;
1301 }
1302 if ((current_info < board_info) && (current_info > best_info)) {
1303 best_info = current_info;
1304 }
1305 if (current_info < best_info) {
1306 DEBUG (
1307 (EFI_D_ERROR,
1308 "Delete don't fit DTB entry %u/%08x/0x%08x/%x/%x/%x/%x/%x/%x/%x\n",
1309 dt_node_tmp1->dt_entry_m->platform_id,
1310 dt_node_tmp1->dt_entry_m->variant_id,
1311 dt_node_tmp1->dt_entry_m->board_hw_subtype,
1312 dt_node_tmp1->dt_entry_m->soc_rev,
1313 dt_node_tmp1->dt_entry_m->pmic_rev[0],
1314 dt_node_tmp1->dt_entry_m->pmic_rev[1],
1315 dt_node_tmp1->dt_entry_m->pmic_rev[2],
1316 dt_node_tmp1->dt_entry_m->pmic_rev[3],
1317 dt_node_tmp1->dt_entry_m->offset, dt_node_tmp1->dt_entry_m->size));
1318
1319 dt_node_tmp2 = (struct dt_entry_node *)dt_node_tmp1->node.prev;
1320 dt_entry_list_delete (dt_node_tmp1);
1321 dt_node_tmp1 = dt_node_tmp2;
1322 }
1323 }
1324
1325 list_for_every_entry (&dt_list->node, dt_node_tmp1, dt_node, node)
1326 {
1327 if (!dt_node_tmp1) {
1328 DEBUG ((EFI_D_ERROR, "Current node is the end\n"));
1329 break;
1330 }
1331 switch (dtb_info) {
1332 case DTB_SOC:
1333 current_info = dt_node_tmp1->dt_entry_m->soc_rev;
1334 break;
1335 case DTB_MAJOR_MINOR:
1336 current_info = ((dt_node_tmp1->dt_entry_m->variant_id) & 0x00ffff00);
1337 break;
1338 case DTB_PMIC0:
1339 current_info = dt_node_tmp1->dt_entry_m->pmic_rev[0];
1340 break;
1341 case DTB_PMIC1:
1342 current_info = dt_node_tmp1->dt_entry_m->pmic_rev[1];
1343 break;
1344 case DTB_PMIC2:
1345 current_info = dt_node_tmp1->dt_entry_m->pmic_rev[2];
1346 break;
1347 case DTB_PMIC3:
1348 current_info = dt_node_tmp1->dt_entry_m->pmic_rev[3];
1349 break;
1350 default:
1351 DEBUG ((EFI_D_ERROR,
1352 "ERROR: Unsupported version (%d) in dt node check \n", dtb_info));
1353 return 0;
1354 }
1355
1356 if (current_info != best_info) {
1357 DEBUG (
1358 (EFI_D_VERBOSE,
1359 "Delete don't fit DTB entry %u/%08x/0x%08x/%x/%x/%x/%x/%x/%x/%x\n",
1360 dt_node_tmp1->dt_entry_m->platform_id,
1361 dt_node_tmp1->dt_entry_m->variant_id,
1362 dt_node_tmp1->dt_entry_m->board_hw_subtype,
1363 dt_node_tmp1->dt_entry_m->soc_rev,
1364 dt_node_tmp1->dt_entry_m->pmic_rev[0],
1365 dt_node_tmp1->dt_entry_m->pmic_rev[1],
1366 dt_node_tmp1->dt_entry_m->pmic_rev[2],
1367 dt_node_tmp1->dt_entry_m->pmic_rev[3],
1368 dt_node_tmp1->dt_entry_m->offset, dt_node_tmp1->dt_entry_m->size));
1369
1370 dt_node_tmp2 = (struct dt_entry_node *)dt_node_tmp1->node.prev;
1371 dt_entry_list_delete (dt_node_tmp1);
1372 dt_node_tmp1 = dt_node_tmp2;
1373 }
1374 }
1375 return 1;
1376}
1377
1378STATIC struct dt_entry *
1379platform_dt_match_best (struct dt_entry_node *dt_list)
1380{
1381 struct dt_entry_node *dt_node_tmp1 = NULL;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001382
lijuangc336a292017-08-22 19:48:49 +08001383 /* check Foundry id
1384 * the foundry id must exact match board founddry id, this is compatibility
1385 * check, if couldn't find the exact match from DTB, will exact match 0x0.
1386 */
1387 platform_dt_absolute_compat_match (dt_list, DTB_FOUNDRY);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001388
lijuangc336a292017-08-22 19:48:49 +08001389 /* check PMIC model
1390 * the PMIC model must exact match board PMIC model, this is compatibility
1391 * check, if couldn't find the exact match from DTB, will exact match 0x0.
1392 */
1393 platform_dt_absolute_compat_match (dt_list, DTB_PMIC_MODEL);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001394
lijuangc336a292017-08-22 19:48:49 +08001395 /* check soc version
1396 * the suitable soc version must less than or equal to board soc version
1397 */
1398 update_dtb_entry_node (dt_list, DTB_SOC);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001399
lijuangc336a292017-08-22 19:48:49 +08001400 /*check major and minor version
1401 * the suitable major&minor version must less than or equal to board
1402 * major&minor version
1403 */
1404 update_dtb_entry_node (dt_list, DTB_MAJOR_MINOR);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001405
lijuangc336a292017-08-22 19:48:49 +08001406 /*check pmic info
1407 * the suitable pmic major&minor info must less than or equal to board pmic
1408 * major&minor version
1409 */
1410 update_dtb_entry_node (dt_list, DTB_PMIC0);
1411 update_dtb_entry_node (dt_list, DTB_PMIC1);
1412 update_dtb_entry_node (dt_list, DTB_PMIC2);
1413 update_dtb_entry_node (dt_list, DTB_PMIC3);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001414
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001415 list_for_every_entry (&dt_list->node, dt_node_tmp1, dt_node, node)
1416 {
1417 if (!dt_node_tmp1) {
1418 DEBUG ((EFI_D_ERROR, "ERROR: Couldn't find the suitable DTB!\n"));
1419 return NULL;
1420 }
1421 if (dt_node_tmp1->dt_entry_m)
1422 return dt_node_tmp1->dt_entry_m;
1423 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001424
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001425 return NULL;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001426}
Bhanuprakash Modemed797c72018-09-05 20:39:07 +05301427
1428BOOLEAN
1429AppendToDtList (struct fdt_entry_node **DtList,
1430 UINT64 Address,
1431 UINT64 Size) {
1432 struct fdt_entry_node *Current = *DtList;
1433
1434 if (*DtList == NULL) {
1435 DEBUG ((EFI_D_VERBOSE, "DTs list: NULL\n"));
1436 Current = AllocatePool (sizeof (struct fdt_entry_node));
1437 if (!Current) {
1438 return FALSE;
1439 }
1440
1441 Current->address = Address;
1442 Current->size = Size;
1443 Current->next = NULL;
1444 *DtList = Current;
1445 return TRUE;
1446 } else {
1447
1448 while (Current->next != NULL) {
1449 Current = Current->next;
1450 }
1451
1452 Current->next = AllocatePool (sizeof (struct fdt_entry_node));
1453 if (!Current->next) {
1454 return FALSE;
1455 }
1456 Current->next->address = Address;
1457 Current->next->size = Size;
1458 Current->next->next = NULL;
1459 return TRUE;
1460 }
1461}
1462
1463VOID DeleteDtList (struct fdt_entry_node** DtList)
1464{
1465 struct fdt_entry_node *Current = *DtList;
1466 struct fdt_entry_node *Next;
1467
1468 while (Current != NULL) {
1469 Next = Current->next;
1470 FreePool (Current);
1471 Current = Next;
1472 }
1473
1474 *DtList = NULL;
1475}