blob: ddeb5674feef7f5cd0a105dd902698442c25d563 [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 =
Bhanuprakash Modem763cdd52018-11-01 14:49:55 +053063 (struct dt_entry_node *)AllocateZeroPool (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 =
Bhanuprakash Modem763cdd52018-11-01 14:49:55 +053072 (struct dt_entry *)AllocateZeroPool (sizeof (struct dt_entry));
Jeevan Shriram17f173d2017-10-24 22:11:07 -070073 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 return dt_node_member;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080082}
83
Jeevan Shriram17f173d2017-10-24 22:11:07 -070084static VOID
85insert_dt_entry_in_queue (struct dt_entry_node *dt_list,
86 struct dt_entry_node *dt_node_member)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080087{
Jeevan Shriram17f173d2017-10-24 22:11:07 -070088 list_add_tail (&dt_list->node, &dt_node_member->node);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080089}
90
Jeevan Shriram17f173d2017-10-24 22:11:07 -070091static VOID
92dt_entry_list_delete (struct dt_entry_node *dt_node_member)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080093{
Jeevan Shriram17f173d2017-10-24 22:11:07 -070094 if (list_in_list (&dt_node_member->node)) {
95 list_delete (&dt_node_member->node);
96 FreePool (dt_node_member->dt_entry_m);
97 dt_node_member->dt_entry_m = NULL;
98 FreePool (dt_node_member);
99 dt_node_member = NULL;
100 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800101}
102
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700103static BOOLEAN
104DeviceTreeCompatible (VOID *dtb,
105 UINT32 dtb_size,
106 struct dt_entry_node *dtb_list)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800107{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700108 int root_offset;
109 const VOID *prop = NULL;
110 const char *plat_prop = NULL;
111 const char *board_prop = NULL;
112 const char *pmic_prop = NULL;
113 char *model = NULL;
114 struct dt_entry *dt_entry_array = NULL;
115 struct board_id *board_data = NULL;
116 struct plat_id *platform_data = NULL;
117 struct pmic_id *pmic_data = NULL;
118 int len;
119 int len_board_id;
120 int len_plat_id;
121 int min_plat_id_len = 0;
122 int len_pmic_id;
123 UINT32 dtb_ver;
jianzhoue687ba82017-11-27 16:08:51 +0800124 UINT64 NumEntries = 0;
125 UINT64 i;
126 UINT32 j, k, n;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700127 UINT32 msm_data_count;
128 UINT32 board_data_count;
129 UINT32 pmic_data_count;
130 BOOLEAN Result = FALSE;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800131
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700132 root_offset = fdt_path_offset (dtb, "/");
133 if (root_offset < 0)
134 return FALSE;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800135
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700136 prop = fdt_getprop (dtb, root_offset, "model", &len);
137 if (prop && len > 0) {
Bhanuprakash Modem763cdd52018-11-01 14:49:55 +0530138 model = (char *)AllocateZeroPool (sizeof (char) * len);
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700139 if (!model) {
140 DEBUG ((EFI_D_ERROR, "Failed to allocate memory for model\n"));
141 return FALSE;
142 }
143 AsciiStrnCpyS (model, (sizeof (CHAR8) * len), prop, len);
144 } else {
145 DEBUG ((EFI_D_ERROR, "model does not exist in device tree\n"));
146 }
147 /* Find the pmic-id prop from DTB , if pmic-id is present then
148 * the DTB is version 3, otherwise find the board-id prop from DTB ,
149 * if board-id is present then the DTB is version 2 */
150 pmic_prop = (const char *)fdt_getprop (dtb, root_offset, "qcom,pmic-id",
151 &len_pmic_id);
152 board_prop = (const char *)fdt_getprop (dtb, root_offset, "qcom,board-id",
153 &len_board_id);
154 if (pmic_prop && (len_pmic_id > 0) && board_prop && (len_board_id > 0)) {
155 if ((len_pmic_id % PMIC_ID_SIZE) || (len_board_id % BOARD_ID_SIZE)) {
156 DEBUG ((EFI_D_ERROR, "qcom,pmic-id (%d) or qcom,board-id(%d) in device "
157 "tree is not a multiple of (%d %d)\n",
158 len_pmic_id, len_board_id, PMIC_ID_SIZE, BOARD_ID_SIZE));
159 goto Exit;
160 }
161 dtb_ver = DEV_TREE_VERSION_V3;
162 min_plat_id_len = PLAT_ID_SIZE;
163 } else if (board_prop && len_board_id > 0) {
164 if (len_board_id % BOARD_ID_SIZE) {
165 DEBUG ((EFI_D_ERROR,
166 "qcom,pmic-id (%d) in device tree is not a multiple of (%d)\n",
167 len_board_id, BOARD_ID_SIZE));
168 goto Exit;
169 }
170 dtb_ver = DEV_TREE_VERSION_V2;
171 min_plat_id_len = PLAT_ID_SIZE;
172 } else {
173 dtb_ver = DEV_TREE_VERSION_V1;
174 min_plat_id_len = DT_ENTRY_V1_SIZE;
175 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800176
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700177 /* Get the msm-id prop from DTB */
178 plat_prop =
179 (const char *)fdt_getprop (dtb, root_offset, "qcom,msm-id", &len_plat_id);
180 if (!plat_prop || len_plat_id <= 0) {
181 DEBUG ((EFI_D_VERBOSE, "qcom,msm-id entry not found\n"));
182 goto Exit;
183 } else if (len_plat_id % min_plat_id_len) {
184 DEBUG ((EFI_D_ERROR,
185 "qcom, msm-id in device tree is (%d) not a multiple of (%d)\n",
186 len_plat_id, min_plat_id_len));
187 goto Exit;
188 }
189 if (dtb_ver == DEV_TREE_VERSION_V2 || dtb_ver == DEV_TREE_VERSION_V3) {
190 board_data_count = (len_board_id / BOARD_ID_SIZE);
191 msm_data_count = (len_plat_id / PLAT_ID_SIZE);
192 /* If dtb version is v2.0, the pmic_data_count will be <= 0 */
193 pmic_data_count = (len_pmic_id / PMIC_ID_SIZE);
194
195 /* If we are using dtb v3.0, then we have split board, msm & pmic data in
196 * the DTB
197 * If we are using dtb v2.0, then we have split board & msmdata in the DTB
198 */
Bhanuprakash Modem763cdd52018-11-01 14:49:55 +0530199 board_data = (struct board_id *)AllocateZeroPool (
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700200 sizeof (struct board_id) * (len_board_id / BOARD_ID_SIZE));
201 if (!board_data) {
202 DEBUG ((EFI_D_ERROR, "Failed to allocate memory for board_data\n"));
203 goto Exit;
204 }
205
Bhanuprakash Modem763cdd52018-11-01 14:49:55 +0530206 platform_data = (struct plat_id *)AllocateZeroPool (
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700207 sizeof (struct plat_id) * (len_plat_id / PLAT_ID_SIZE));
208 if (!platform_data) {
209 DEBUG ((EFI_D_ERROR, "Failed to allocate memory for platform_data\n"));
210 goto Exit;
211 }
212 if (dtb_ver == DEV_TREE_VERSION_V3) {
Bhanuprakash Modem763cdd52018-11-01 14:49:55 +0530213 pmic_data = (struct pmic_id *)AllocateZeroPool (sizeof (struct pmic_id) *
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700214 (len_pmic_id / PMIC_ID_SIZE));
215 if (!pmic_data) {
216 DEBUG ((EFI_D_ERROR, "Failed to allocate memory for pmic_data\n"));
lijuang46df8442017-09-28 19:06:36 +0800217 goto Exit;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700218 }
219 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800220
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700221 /* Extract board data from DTB */
222 for (i = 0; i < board_data_count; i++) {
223 board_data[i].variant_id =
224 fdt32_to_cpu (((struct board_id *)board_prop)->variant_id);
225 board_data[i].platform_subtype =
226 fdt32_to_cpu (((struct board_id *)board_prop)->platform_subtype);
227 /* For V2/V3 version of DTBs we have platform version field as part
228 * of variant ID, in such case the subtype will be mentioned as 0x0
229 * As the qcom, board-id = <0xSSPMPmPH, 0x0>
230 * SS -- Subtype
231 * PM -- Platform major version
232 * Pm -- Platform minor version
233 * PH -- Platform hardware CDP/MTP
234 * In such case to make it compatible with LK algorithm move the subtype
235 * from variant_id to subtype field
236 */
237 if (board_data[i].platform_subtype == 0)
238 board_data[i].platform_subtype =
239 fdt32_to_cpu (((struct board_id *)board_prop)->variant_id) >> 0x18;
Vijay Kumar Pendoti1e847bd2016-11-21 15:29:13 +0530240
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700241 len_board_id -= sizeof (struct board_id);
242 board_prop += sizeof (struct board_id);
243 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800244
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700245 /* Extract platform data from DTB */
246 for (i = 0; i < msm_data_count; i++) {
247 platform_data[i].platform_id =
248 fdt32_to_cpu (((struct plat_id *)plat_prop)->platform_id);
249 platform_data[i].soc_rev =
250 fdt32_to_cpu (((struct plat_id *)plat_prop)->soc_rev);
251 len_plat_id -= sizeof (struct plat_id);
252 plat_prop += sizeof (struct plat_id);
253 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800254
jianzhoue687ba82017-11-27 16:08:51 +0800255 if ((pmic_data_count != 0) &&
256 (MAX_UINT64 / pmic_data_count < (msm_data_count * board_data_count))) {
257 DEBUG ((EFI_D_ERROR, "NumEntries exceeds MAX_UINT64\n"));
258 goto Exit;
259 }
260
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700261 if (dtb_ver == DEV_TREE_VERSION_V3 && pmic_prop) {
262 /* Extract pmic data from DTB */
263 for (i = 0; i < pmic_data_count; i++) {
264 pmic_data[i].pmic_version[0] =
265 fdt32_to_cpu (((struct pmic_id *)pmic_prop)->pmic_version[0]);
266 pmic_data[i].pmic_version[1] =
267 fdt32_to_cpu (((struct pmic_id *)pmic_prop)->pmic_version[1]);
268 pmic_data[i].pmic_version[2] =
269 fdt32_to_cpu (((struct pmic_id *)pmic_prop)->pmic_version[2]);
270 pmic_data[i].pmic_version[3] =
271 fdt32_to_cpu (((struct pmic_id *)pmic_prop)->pmic_version[3]);
272 len_pmic_id -= sizeof (struct pmic_id);
273 pmic_prop += sizeof (struct pmic_id);
274 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800275
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700276 /* We need to merge board & platform data into dt entry structure */
jianzhoue687ba82017-11-27 16:08:51 +0800277 NumEntries = (uint64_t)msm_data_count * board_data_count *
278 pmic_data_count;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700279 } else {
280 /* We need to merge board & platform data into dt entry structure */
jianzhoue687ba82017-11-27 16:08:51 +0800281 NumEntries = (uint64_t)msm_data_count * board_data_count;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700282 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800283
Bhanuprakash Modem763cdd52018-11-01 14:49:55 +0530284 dt_entry_array = (struct dt_entry *)AllocateZeroPool (
285 sizeof (struct dt_entry) *
286 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 =
Bhanuprakash Modem763cdd52018-11-01 14:49:55 +0530410 (struct dt_entry_node *)AllocateZeroPool (sizeof (struct dt_entry_node));
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700411 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 list_initialize (&dt_entry_queue->node);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800417
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700418 if (!dtb_offset) {
419 DEBUG ((EFI_D_ERROR, "DTB offset is NULL\n"));
420 goto out;
421 }
Runmin Wang7db42962016-06-27 14:35:35 -0700422
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700423 if (((uintptr_t)kernel + (uintptr_t)dtb_offset) < (uintptr_t)kernel) {
424 goto out;
425 }
426 dtb = kernel + dtb_offset;
427 while (((uintptr_t)dtb + sizeof (struct fdt_header)) <
428 (uintptr_t)kernel_end) {
429 struct fdt_header dtb_hdr;
430 UINT32 dtb_size;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800431
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700432 /* the DTB could be unaligned, so extract the header,
433 * and operate on it separately */
434 gBS->CopyMem (&dtb_hdr, dtb, sizeof (struct fdt_header));
435 if (fdt_check_header ((const VOID *)&dtb_hdr) != 0 ||
436 fdt_check_header_ext ((VOID *)&dtb_hdr) != 0 ||
437 ((uintptr_t)dtb + (uintptr_t)fdt_totalsize ((const VOID *)&dtb_hdr) <
438 (uintptr_t)dtb) ||
439 ((uintptr_t)dtb + (uintptr_t)fdt_totalsize ((const VOID *)&dtb_hdr) >
440 (uintptr_t)kernel_end))
441 break;
442 dtb_size = fdt_totalsize (&dtb_hdr);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800443
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700444 if (!DeviceTreeCompatible (dtb, dtb_size, dt_entry_queue)) {
445 DEBUG ((EFI_D_VERBOSE, "Error while DTB parse continue with next DTB\n"));
446 if (!GetRticDtb (dtb))
447 DEBUG ((EFI_D_VERBOSE,
448 "Error while DTB parsing RTIC prop continue with next DTB\n"));
449 }
Mukesh Ojhaa1bd1f82017-05-25 19:04:21 +0530450
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700451 /* goto the next device tree if any */
452 dtb += dtb_size;
453 }
454 best_match_dt_entry = platform_dt_match_best (dt_entry_queue);
455 if (best_match_dt_entry) {
456 bestmatch_tag = (VOID *)best_match_dt_entry->offset;
457 bestmatch_tag_size = best_match_dt_entry->size;
458 DEBUG ((EFI_D_INFO, "Best match DTB tags "
459 "%u/%08x/0x%08x/%x/%x/%x/%x/%x/(offset)0x%08x/"
460 "(size)0x%08x\n",
461 best_match_dt_entry->platform_id, best_match_dt_entry->variant_id,
462 best_match_dt_entry->board_hw_subtype, best_match_dt_entry->soc_rev,
463 best_match_dt_entry->pmic_rev[0], best_match_dt_entry->pmic_rev[1],
464 best_match_dt_entry->pmic_rev[2], best_match_dt_entry->pmic_rev[3],
465 best_match_dt_entry->offset, best_match_dt_entry->size));
466 DEBUG ((EFI_D_INFO, "Using pmic info 0x%0x/0x%x/0x%x/0x%0x for device "
467 "0x%0x/0x%x/0x%x/0x%0x\n",
468 best_match_dt_entry->pmic_rev[0], best_match_dt_entry->pmic_rev[1],
469 best_match_dt_entry->pmic_rev[2], best_match_dt_entry->pmic_rev[3],
470 BoardPmicTarget (0), BoardPmicTarget (1), BoardPmicTarget (2),
471 BoardPmicTarget (3)));
472 }
473 /* free queue's memory */
474 list_for_every_entry (&dt_entry_queue->node, dt_node_tmp1, dt_node, node)
475 {
476 if (!dt_node_tmp1) {
477 DEBUG ((EFI_D_VERBOSE, "Current node is the end\n"));
478 break;
479 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800480
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700481 dt_node_tmp2 = (struct dt_entry_node *)dt_node_tmp1->node.prev;
482 dt_entry_list_delete (dt_node_tmp1);
483 dt_node_tmp1 = dt_node_tmp2;
484 }
lijuangf71a4842017-07-27 19:26:31 +0800485
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700486 if (bestmatch_tag) {
487 Status = BaseMem (&BaseMemory);
488 if (Status != EFI_SUCCESS) {
489 DEBUG ((EFI_D_ERROR, "Unable to find Base memory for DDR %r\n", Status));
490 FreePool (dt_entry_queue);
491 dt_entry_queue = NULL;
492 goto out;
493 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800494
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700495 RamdiskLoadAddr =
496 (EFI_PHYSICAL_ADDRESS) (BaseMemory | PcdGet32 (RamdiskLoadAddress));
497 if ((RamdiskLoadAddr - (UINT64)tags) > RamdiskLoadAddr) {
498 DEBUG ((EFI_D_ERROR, "Tags address is not valid\n"));
499 goto out;
500 }
501 if ((RamdiskLoadAddr - (UINT64)tags) < bestmatch_tag_size) {
502 DEBUG ((EFI_D_ERROR, "Tag size is over the limit\n"));
503 goto out;
504 }
505 gBS->CopyMem (tags, bestmatch_tag, bestmatch_tag_size);
506 /* clear out the old DTB magic so kernel doesn't find it */
507 *((UINT32 *)(kernel + dtb_offset)) = 0;
lijuang46df8442017-09-28 19:06:36 +0800508 FreePool (dt_entry_queue);
509 dt_entry_queue = NULL;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700510
511 return tags;
512 }
513
514 DEBUG (
515 (EFI_D_ERROR,
516 "DTB offset is incorrect, kernel image does not have appended DTB\n"));
517
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700518out:
519 FreePool (dt_entry_queue);
520 dt_entry_queue = NULL;
521 return NULL;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800522}
523
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700524STATIC BOOLEAN
525CheckAllBitsSet (UINT32 DtMatchVal)
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530526{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700527 return (DtMatchVal & ALL_BITS_SET) == (ALL_BITS_SET);
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530528}
529
Mukesh Ojhafa815352017-11-08 14:43:41 +0530530STATIC VOID
531ReadBestPmicMatch (CONST CHAR8 *PmicProp, UINT32 PmicEntCount,
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530532 PmicIdInfo *BestPmicInfo)
Mukesh Ojhafa815352017-11-08 14:43:41 +0530533{
534 UINT32 PmicEntIdx;
535 UINT32 Idx;
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530536 PmicIdInfo CurPmicInfo;
Mukesh Ojhafa815352017-11-08 14:43:41 +0530537
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530538 memset (BestPmicInfo, 0, sizeof (PmicIdInfo));
Mukesh Ojhafa815352017-11-08 14:43:41 +0530539 for (PmicEntIdx = 0; PmicEntIdx < PmicEntCount; PmicEntIdx++) {
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530540 memset (&CurPmicInfo, 0, sizeof (PmicIdInfo));
Mukesh Ojhafa815352017-11-08 14:43:41 +0530541 for (Idx = 0; Idx < MAX_PMIC_IDX; Idx++) {
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530542 CurPmicInfo.DtPmicModel[Idx] =
Mukesh Ojhafa815352017-11-08 14:43:41 +0530543 fdt32_to_cpu (((struct pmic_id *)PmicProp)->pmic_version[Idx]);
544
545 DEBUG ((EFI_D_VERBOSE, "pmic_data[%u].:%x\n", Idx,
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530546 CurPmicInfo.DtPmicModel[Idx]));
547 CurPmicInfo.DtPmicRev[Idx] =
548 CurPmicInfo.DtPmicModel[Idx] & PMIC_REV_MASK;
549 CurPmicInfo.DtPmicModel[Idx] =
550 CurPmicInfo.DtPmicModel[Idx] & PMIC_MODEL_MASK;
Mukesh Ojhafa815352017-11-08 14:43:41 +0530551
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530552 if ((CurPmicInfo.DtPmicModel[Idx]) ==
Mukesh Ojhafa815352017-11-08 14:43:41 +0530553 BoardPmicModel (Idx)) {
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530554 CurPmicInfo.DtMatchVal |=
Mukesh Ojhafa815352017-11-08 14:43:41 +0530555 BIT ((PMIC_MATCH_EXACT_MODEL_IDX0 + Idx * PMIC_SHIFT_IDX));
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530556 } else if (CurPmicInfo.DtPmicModel[Idx] == 0) {
557 CurPmicInfo.DtMatchVal |=
Mukesh Ojhafa815352017-11-08 14:43:41 +0530558 BIT ((PMIC_MATCH_DEFAULT_MODEL_IDX0 + Idx * PMIC_SHIFT_IDX));
559 } else {
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530560 CurPmicInfo.DtMatchVal = BIT (NONE_MATCH);
Mukesh Ojhafa815352017-11-08 14:43:41 +0530561 DEBUG ((EFI_D_VERBOSE, "Pmic model does not match\n"));
562 break;
563 }
564
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530565 if (CurPmicInfo.DtPmicRev[Idx] == (BoardPmicTarget (Idx)
Mukesh Ojhafa815352017-11-08 14:43:41 +0530566 & PMIC_REV_MASK)) {
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530567 CurPmicInfo.DtMatchVal |=
Mukesh Ojhafa815352017-11-08 14:43:41 +0530568 BIT ((PMIC_MATCH_EXACT_REV_IDX0 + Idx * PMIC_SHIFT_IDX));
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530569 } else if (CurPmicInfo.DtPmicRev[Idx] <
Mukesh Ojhafa815352017-11-08 14:43:41 +0530570 (BoardPmicTarget (Idx) & PMIC_REV_MASK)) {
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530571 CurPmicInfo.DtMatchVal |= BIT ((PMIC_MATCH_BEST_REV_IDX0 +
Mukesh Ojhafa815352017-11-08 14:43:41 +0530572 Idx * PMIC_SHIFT_IDX));
573 } else {
574 DEBUG ((EFI_D_VERBOSE, "Pmic revision does not match\n"));
575 break;
576 }
577 }
578
579 DEBUG ((EFI_D_VERBOSE, "BestPmicInfo.DtMatchVal : %x"
580 " CurPmicInfo[%u]->DtMatchVal : %x\n", BestPmicInfo->DtMatchVal,
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530581 PmicEntIdx, CurPmicInfo.DtMatchVal));
582 if (BestPmicInfo->DtMatchVal < CurPmicInfo.DtMatchVal) {
583 gBS->CopyMem (BestPmicInfo, &CurPmicInfo,
Mukesh Ojhafa815352017-11-08 14:43:41 +0530584 sizeof (struct PmicIdInfo));
585 } else if (BestPmicInfo->DtMatchVal ==
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530586 CurPmicInfo.DtMatchVal) {
587 if (BestPmicInfo->DtPmicRev[0] < CurPmicInfo.DtPmicRev[0]) {
588 gBS->CopyMem (BestPmicInfo, &CurPmicInfo,
Mukesh Ojhafa815352017-11-08 14:43:41 +0530589 sizeof (struct PmicIdInfo));
590 } else if (BestPmicInfo->DtPmicRev[1] <
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530591 CurPmicInfo.DtPmicRev[1]) {
592 gBS->CopyMem (BestPmicInfo, &CurPmicInfo,
Mukesh Ojhafa815352017-11-08 14:43:41 +0530593 sizeof (struct PmicIdInfo));
594 } else if (BestPmicInfo->DtPmicRev[2] <
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530595 CurPmicInfo.DtPmicRev[2]) {
596 gBS->CopyMem (BestPmicInfo, &CurPmicInfo,
Mukesh Ojhafa815352017-11-08 14:43:41 +0530597 sizeof (struct PmicIdInfo));
598 } else if (BestPmicInfo->DtPmicRev[3] <
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530599 CurPmicInfo.DtPmicRev[3]) {
600 gBS->CopyMem (BestPmicInfo, &CurPmicInfo,
Mukesh Ojhafa815352017-11-08 14:43:41 +0530601 sizeof (struct PmicIdInfo));
602 }
603 }
604
605 PmicProp += sizeof (struct pmic_id);
606 }
607}
608
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800609STATIC EFI_STATUS GetPlatformMatchDtb (DtInfo * CurDtbInfo,
610 CONST CHAR8 *PlatProp,
611 INT32 LenPlatId,
612 INT32 MinPlatIdLen)
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530613{
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530614
lijuangf6b1c5f2018-03-20 13:15:47 +0800615 if (CurDtbInfo == NULL) {
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800616 DEBUG ((EFI_D_VERBOSE, "Input parameters null\n"));
617 return EFI_INVALID_PARAMETER;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700618 }
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530619
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700620 if (PlatProp && (LenPlatId > 0) && (!(LenPlatId % MinPlatIdLen))) {
621 /*Compare msm-id of the dtb vs Board*/
622 CurDtbInfo->DtPlatformId =
623 fdt32_to_cpu (((struct plat_id *)PlatProp)->platform_id);
624 DEBUG ((EFI_D_VERBOSE, "Boardsocid = %x, Dtsocid = %x\n",
625 (BoardPlatformRawChipId () & SOC_MASK),
626 (CurDtbInfo->DtPlatformId & SOC_MASK)));
627 if ((BoardPlatformRawChipId () & SOC_MASK) ==
628 (CurDtbInfo->DtPlatformId & SOC_MASK)) {
629 CurDtbInfo->DtMatchVal |= BIT (SOC_MATCH);
630 } else {
Mukesh Ojha0eedc352018-02-14 17:42:30 +0530631 DEBUG ((EFI_D_VERBOSE, "qcom,msm-id does not match\n"));
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700632 /* If it's neither exact nor default match don't select dtb */
633 CurDtbInfo->DtMatchVal = BIT (NONE_MATCH);
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800634 return EFI_NOT_FOUND;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700635 }
636 /*Compare soc rev of the dtb vs Board*/
637 CurDtbInfo->DtSocRev = fdt32_to_cpu (((struct plat_id *)PlatProp)->soc_rev);
638 DEBUG ((EFI_D_VERBOSE, "BoardSocRev = %x, DtSocRev =%x\n",
639 BoardPlatformChipVersion (), CurDtbInfo->DtSocRev));
640 if (CurDtbInfo->DtSocRev == BoardPlatformChipVersion ()) {
641 CurDtbInfo->DtMatchVal |= BIT (VERSION_EXACT_MATCH);
642 } else if (CurDtbInfo->DtSocRev < BoardPlatformChipVersion ()) {
643 CurDtbInfo->DtMatchVal |= BIT (VERSION_BEST_MATCH);
644 } else if (CurDtbInfo->DtSocRev) {
Mukesh Ojha0eedc352018-02-14 17:42:30 +0530645 DEBUG ((EFI_D_VERBOSE, "soc version does not match\n"));
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700646 }
647 /*Compare Foundry Id of the dtb vs Board*/
648 CurDtbInfo->DtFoundryId =
649 fdt32_to_cpu (((struct plat_id *)PlatProp)->platform_id) &
650 FOUNDRY_ID_MASK;
651 DEBUG ((EFI_D_VERBOSE, "BoardFoundry = %x, DtFoundry = %x\n",
652 (BoardPlatformFoundryId () << PLATFORM_FOUNDRY_SHIFT),
653 CurDtbInfo->DtFoundryId));
654 if (CurDtbInfo->DtFoundryId ==
655 (BoardPlatformFoundryId () << PLATFORM_FOUNDRY_SHIFT)) {
656 CurDtbInfo->DtMatchVal |= BIT (FOUNDRYID_EXACT_MATCH);
657 } else if (CurDtbInfo->DtFoundryId == 0) {
658 CurDtbInfo->DtMatchVal |= BIT (FOUNDRYID_DEFAULT_MATCH);
659 } else {
Mukesh Ojha0eedc352018-02-14 17:42:30 +0530660 DEBUG ((EFI_D_VERBOSE, "soc foundry does not match\n"));
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700661 /* If it's neither exact nor default match don't select dtb */
662 CurDtbInfo->DtMatchVal = BIT (NONE_MATCH);
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800663 return EFI_NOT_FOUND;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700664 }
665 } else {
666 DEBUG ((EFI_D_VERBOSE, "qcom, msm-id does not exist (or) is"
667 " (%d) not a multiple of (%d)\n",
668 LenPlatId, MinPlatIdLen));
669 }
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800670 return EFI_SUCCESS;
671}
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530672
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800673STATIC EFI_STATUS GetBoardMatchDtb (DtInfo *CurDtbInfo,
674 CONST CHAR8 *BoardProp,
675 INT32 LenBoardId)
676{
lijuangf6b1c5f2018-03-20 13:15:47 +0800677 if (CurDtbInfo == NULL) {
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800678 DEBUG ((EFI_D_VERBOSE, "Input parameters null\n"));
679 return EFI_INVALID_PARAMETER;
680 }
681
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700682 if (BoardProp && (LenBoardId > 0) && (!(LenBoardId % BOARD_ID_SIZE))) {
683 CurDtbInfo->DtVariantId =
684 fdt32_to_cpu (((struct board_id *)BoardProp)->variant_id);
685 CurDtbInfo->DtPlatformSubtype =
686 fdt32_to_cpu (((struct board_id *)BoardProp)->platform_subtype);
687 if (CurDtbInfo->DtPlatformSubtype == 0) {
688 CurDtbInfo->DtPlatformSubtype =
689 fdt32_to_cpu (((struct board_id *)BoardProp)->variant_id) >>
690 PLATFORM_SUBTYPE_SHIFT_ID;
691 }
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530692
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700693 DEBUG ((EFI_D_VERBOSE, "BoardVariant = %x, DtVariant = %x\n",
694 BoardPlatformType (), CurDtbInfo->DtVariantId));
695 CurDtbInfo->DtVariantMajor = CurDtbInfo->DtVariantId & VARIANT_MAJOR_MASK;
696 CurDtbInfo->DtVariantMinor = CurDtbInfo->DtVariantId & VARIANT_MINOR_MASK;
697 CurDtbInfo->DtVariantId = CurDtbInfo->DtVariantId & VARIANT_MASK;
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530698
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700699 if (CurDtbInfo->DtVariantId == BoardPlatformType ()) {
700 CurDtbInfo->DtMatchVal |= BIT (VARIANT_MATCH);
701 } else if (CurDtbInfo->DtVariantId) {
702 DEBUG ((EFI_D_VERBOSE, "qcom,board-id does not"
703 " match\n"));
704 /* If it's neither exact nor default match don't select dtb */
705 CurDtbInfo->DtMatchVal = BIT (NONE_MATCH);
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800706 return EFI_NOT_FOUND;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700707 }
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530708
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700709 if (CurDtbInfo->DtVariantMajor == (BoardTargetId () & VARIANT_MAJOR_MASK)) {
710 CurDtbInfo->DtMatchVal |= BIT (VARIANT_MAJOR_EXACT_MATCH);
711 } else if (CurDtbInfo->DtVariantMajor <
712 (BoardTargetId () & VARIANT_MAJOR_MASK)) {
713 CurDtbInfo->DtMatchVal |= BIT (VARIANT_MAJOR_BEST_MATCH);
714 } else if (CurDtbInfo->DtVariantMajor) {
715 DEBUG ((EFI_D_VERBOSE, "qcom,board-id major version "
716 "does not match\n"));
717 }
Parth Dixitd4887652017-09-13 22:28:52 +0530718
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700719 if (CurDtbInfo->DtVariantMinor == (BoardTargetId () & VARIANT_MINOR_MASK)) {
720 CurDtbInfo->DtMatchVal |= BIT (VARIANT_MINOR_EXACT_MATCH);
721 } else if (CurDtbInfo->DtVariantMinor <
722 (BoardTargetId () & VARIANT_MINOR_MASK)) {
723 CurDtbInfo->DtMatchVal |= BIT (VARIANT_MINOR_BEST_MATCH);
724 } else if (CurDtbInfo->DtVariantMinor) {
725 DEBUG ((EFI_D_VERBOSE, "qcom,board-id minor version "
726 "does not match\n"));
727 }
Parth Dixitd4887652017-09-13 22:28:52 +0530728
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700729 DEBUG ((EFI_D_VERBOSE, "BoardSubtype = %x, DtSubType = %x\n",
730 BoardPlatformSubType (), CurDtbInfo->DtPlatformSubtype));
731 if (CurDtbInfo->DtPlatformSubtype == BoardPlatformSubType ()) {
732 CurDtbInfo->DtMatchVal |= BIT (SUBTYPE_EXACT_MATCH);
733 } else if (CurDtbInfo->DtPlatformSubtype == 0) {
734 CurDtbInfo->DtMatchVal |= BIT (SUBTYPE_DEFAULT_MATCH);
jianzhou254e1e02017-11-01 14:52:02 +0800735 } else {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700736 DEBUG ((EFI_D_VERBOSE, "subtype-id doesnot match\n"));
737 /* If it's neither exact nor default match don't select dtb */
738 CurDtbInfo->DtMatchVal = BIT (NONE_MATCH);
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800739 return EFI_NOT_FOUND;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700740 }
741 } else {
742 DEBUG ((EFI_D_VERBOSE, "qcom,board-id does not exist (or) (%d) "
743 "is not a multiple of (%d)\n",
744 LenBoardId, BOARD_ID_SIZE));
745 }
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800746 return EFI_SUCCESS;
747}
748
749/* Dt selection table for quick reference
750 | SNO | Dt Property | CDT Property | Exact | Best | Default |
751 |-----+---------------+-----------------+-------+------+---------+
752 | | qcom, msm-id | | | | |
753 | | | PlatformId | Y | N | N |
754 | | | SocRev | N | Y | N |
755 | | | FoundryId | Y | N | 0 |
756 | | qcom,board-id | | | | |
757 | | | VariantId | Y | N | N |
758 | | | VariantMajor | N | Y | N |
759 | | | VariantMinor | N | Y | N |
760 | | | PlatformSubtype | Y | N | 0 |
761 | | qcom,pmic-id | | | | |
762 | | | PmicModelId | Y | N | 0 |
763 | | | PmicMetalRev | N | Y | N |
764 | | | PmicLayerRev | N | Y | N |
765 | | | PmicVariantRev | N | Y | N |
766*/
lijuang6fa55c02018-06-22 19:19:26 +0800767STATIC BOOLEAN
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800768ReadDtbFindMatch (DtInfo *CurDtbInfo, DtInfo *BestDtbInfo, UINT32 ExactMatch)
769{
770 EFI_STATUS Status;
771 CONST CHAR8 *PlatProp = NULL;
772 CONST CHAR8 *BoardProp = NULL;
773 CONST CHAR8 *PmicProp = NULL;
774 INT32 LenBoardId;
775 INT32 LenPlatId;
776 INT32 LenPmicId;
777 INT32 MinPlatIdLen = PLAT_ID_SIZE;
778 INT32 RootOffset = 0;
779 VOID *Dtb = CurDtbInfo->Dtb;
780 UINT32 Idx;
781 UINT32 PmicEntCount;
Bhanuprakash Modem64333302018-09-10 18:58:11 +0530782 UINT32 MsmDataCount;
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800783 PmicIdInfo BestPmicInfo;
lijuang6fa55c02018-06-22 19:19:26 +0800784 BOOLEAN FindBestMatch = FALSE;
Bhanuprakash Modem64333302018-09-10 18:58:11 +0530785 DtInfo TempDtbInfo = *CurDtbInfo;
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800786
787 memset (&BestPmicInfo, 0, sizeof (PmicIdInfo));
788 /*Ensure MatchVal to 0 initially*/
789 CurDtbInfo->DtMatchVal = 0;
790 RootOffset = fdt_path_offset (Dtb, "/");
791 if (RootOffset < 0) {
792 DEBUG ((EFI_D_ERROR, "Unable to locate root node\n"));
lijuang6fa55c02018-06-22 19:19:26 +0800793 return FALSE;
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800794 }
795
796 /* Get the msm-id prop from DTB */
797 PlatProp = (CONST CHAR8 *)fdt_getprop (Dtb, RootOffset, "qcom,msm-id",
798 &LenPlatId);
Bhanuprakash Modem64333302018-09-10 18:58:11 +0530799 if (PlatProp &&
800 (LenPlatId > 0) &&
801 (!(LenPlatId % MinPlatIdLen))) {
802 /*
803 For Multiple soc-id's, save the best SocRev match DT in temp and search
804 for the exact match. If exact match is not found, use best match DT from
805 temp.
806 */
807 MsmDataCount = (LenPlatId / MinPlatIdLen);
808
809 /* Ensure to reset the match value */
810 TempDtbInfo.DtMatchVal = NONE_MATCH;
811
812 for (Idx = 0; Idx < MsmDataCount; Idx++) {
813 /* Ensure MatchVal should be 0 for every match */
814 CurDtbInfo->DtMatchVal = NONE_MATCH;
815 Status = GetPlatformMatchDtb (CurDtbInfo,
816 PlatProp,
817 LenPlatId,
818 MinPlatIdLen);
819 if (Status == EFI_SUCCESS &&
820 CurDtbInfo->DtMatchVal > TempDtbInfo.DtMatchVal) {
821 TempDtbInfo = *CurDtbInfo;
822 }
823 LenPlatId -= PLAT_ID_SIZE;
824 PlatProp += PLAT_ID_SIZE;
825 }
826
827 *CurDtbInfo = TempDtbInfo;
828
829 if (CurDtbInfo->DtMatchVal == NONE_MATCH) {
830 DEBUG ((EFI_D_VERBOSE, "Platform dt prop search failed.\n"));
831 goto cleanup;
832 }
833 } else {
834 DEBUG ((EFI_D_VERBOSE, "qcom, msm-id does not exist (or) is"
835 " (%d) not a multiple of (%d)\n",
836 LenPlatId, MinPlatIdLen));
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800837 }
838
839 /* Get the properties like variant id, subtype from Dtb then compare the
840 * dtb vs Board*/
841 BoardProp = (CONST CHAR8 *)fdt_getprop (Dtb, RootOffset, "qcom,board-id",
842 &LenBoardId);
843 Status = GetBoardMatchDtb (CurDtbInfo, BoardProp, LenBoardId);
844 if (Status != EFI_SUCCESS) {
Mukesh Ojha0eedc352018-02-14 17:42:30 +0530845 DEBUG ((EFI_D_VERBOSE, "Board dt prop search failed.\n"));
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800846 goto cleanup;
847 }
Parth Dixitd4887652017-09-13 22:28:52 +0530848
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700849 /*Get the pmic property from Dtb then compare the dtb vs Board*/
850 PmicProp =
851 (CONST CHAR8 *)fdt_getprop (Dtb, RootOffset, "qcom,pmic-id", &LenPmicId);
852 if ((PmicProp) && (LenPmicId > 0) && (!(LenPmicId % PMIC_ID_SIZE))) {
Mukesh Ojhafa815352017-11-08 14:43:41 +0530853 PmicEntCount = LenPmicId / PMIC_ID_SIZE;
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530854 /* Get the best match pmic */
855 ReadBestPmicMatch (PmicProp, PmicEntCount, &BestPmicInfo);
Mukesh Ojhafa815352017-11-08 14:43:41 +0530856 CurDtbInfo->DtMatchVal |= BestPmicInfo.DtMatchVal;
857 for (Idx = 0; Idx < MAX_PMIC_IDX; Idx++) {
858 CurDtbInfo->DtPmicModel[Idx] = BestPmicInfo.DtPmicModel[Idx];
859 CurDtbInfo->DtPmicRev[Idx] = BestPmicInfo.DtPmicRev[Idx];
860 }
861
862 DEBUG ((EFI_D_VERBOSE, "CurDtbInfo->DtMatchVal : %x "
863 "BestPmicInfo.DtMatchVal :%x\n", CurDtbInfo->DtMatchVal,
864 BestPmicInfo.DtMatchVal));
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700865 } else {
866 DEBUG ((EFI_D_VERBOSE, "qcom,pmic-id does not exit (or) is (%d)"
867 " not a multiple of (%d)\n",
868 LenPmicId, PMIC_ID_SIZE));
869 }
Parth Dixitd4887652017-09-13 22:28:52 +0530870
871cleanup:
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700872 if (CurDtbInfo->DtMatchVal & BIT (ExactMatch)) {
873 if (BestDtbInfo->DtMatchVal < CurDtbInfo->DtMatchVal) {
874 gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo));
lijuang6fa55c02018-06-22 19:19:26 +0800875 FindBestMatch = TRUE;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700876 } else if (BestDtbInfo->DtMatchVal == CurDtbInfo->DtMatchVal) {
lijuang6fa55c02018-06-22 19:19:26 +0800877 FindBestMatch = TRUE;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700878 if (BestDtbInfo->DtSocRev < CurDtbInfo->DtSocRev) {
879 gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo));
880 } else if (BestDtbInfo->DtVariantMajor < CurDtbInfo->DtVariantMajor) {
881 gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo));
882 } else if (BestDtbInfo->DtVariantMinor < CurDtbInfo->DtVariantMinor) {
883 gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo));
884 } else if (BestDtbInfo->DtPmicRev[0] < CurDtbInfo->DtPmicRev[0]) {
885 gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo));
886 } else if (BestDtbInfo->DtPmicRev[1] < CurDtbInfo->DtPmicRev[1]) {
887 gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo));
888 } else if (BestDtbInfo->DtPmicRev[2] < CurDtbInfo->DtPmicRev[2]) {
889 gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo));
890 } else if (BestDtbInfo->DtPmicRev[3] < CurDtbInfo->DtPmicRev[3]) {
891 gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo));
lijuang6fa55c02018-06-22 19:19:26 +0800892 } else {
893 FindBestMatch = FALSE;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700894 }
895 }
896 }
lijuang6fa55c02018-06-22 19:19:26 +0800897
898 return FindBestMatch;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700899}
900
901VOID *
902GetSocDtb (VOID *Kernel, UINT32 KernelSize, UINT32 DtbOffset, VOID *DtbLoadAddr)
903{
904 uintptr_t KernelEnd = (uintptr_t)Kernel + KernelSize;
905 VOID *Dtb = NULL;
906 struct fdt_header DtbHdr;
907 UINT32 DtbSize = 0;
908 DtInfo CurDtbInfo = {0};
909 DtInfo BestDtbInfo = {0};
910 if (!DtbOffset) {
911 DEBUG ((EFI_D_ERROR, "DTB offset is NULL\n"));
912 return NULL;
913 }
914
915 if (((uintptr_t)Kernel + (uintptr_t)DtbOffset) < (uintptr_t)Kernel) {
916 return NULL;
917 }
918 Dtb = Kernel + DtbOffset;
919 while (((uintptr_t)Dtb + sizeof (struct fdt_header)) < (uintptr_t)KernelEnd) {
920 /* the DTB could be unaligned, so extract the header,
921 * and operate on it separately */
922 gBS->CopyMem (&DtbHdr, Dtb, sizeof (struct fdt_header));
923 DtbSize = fdt_totalsize ((const VOID *)&DtbHdr);
924 if (fdt_check_header ((const VOID *)&DtbHdr) != 0 ||
925 fdt_check_header_ext ((VOID *)&DtbHdr) != 0 ||
926 ((uintptr_t)Dtb + DtbSize < (uintptr_t)Dtb) ||
927 ((uintptr_t)Dtb + DtbSize > (uintptr_t)KernelEnd))
928 break;
929
930 CurDtbInfo.Dtb = Dtb;
931 ReadDtbFindMatch (&CurDtbInfo, &BestDtbInfo, SOC_MATCH);
932 if (CurDtbInfo.DtMatchVal) {
933 if (CurDtbInfo.DtMatchVal & BIT (SOC_MATCH)) {
934 if (CheckAllBitsSet (CurDtbInfo.DtMatchVal)) {
935 DEBUG ((EFI_D_VERBOSE, "Exact DTB match"
936 " found. DTBO search is not "
937 "required\n"));
938 DtboNeed = FALSE;
Parth Dixitd4887652017-09-13 22:28:52 +0530939 }
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700940 }
941 } else {
942 if (!GetRticDtb (Dtb)) {
943 DEBUG ((EFI_D_VERBOSE, "Error while DTB parsing"
944 " RTIC prop continue with next DTB\n"));
945 }
946 }
947
948 DEBUG ((EFI_D_VERBOSE, "Bestmatch = %x\n", BestDtbInfo.DtMatchVal));
949 Dtb += DtbSize;
950 }
951
952 if (!BestDtbInfo.Dtb) {
953 DEBUG ((EFI_D_ERROR, "No match found for Soc Dtb type\n"));
954 return NULL;
955 }
956
957 return BestDtbInfo.Dtb;
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530958}
959
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700960VOID *
961GetBoardDtb (BootInfo *Info, VOID *DtboImgBuffer)
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530962{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700963 struct DtboTableHdr *DtboTableHdr = DtboImgBuffer;
964 struct DtboTableEntry *DtboTableEntry = NULL;
965 UINT32 DtboCount = 0;
966 VOID *BoardDtb = NULL;
967 UINT32 DtboTableEntriesCount = 0;
968 UINT32 FirstDtboTableEntryOffset = 0;
969 DtInfo CurDtbInfo = {0};
970 DtInfo BestDtbInfo = {0};
lijuang6fa55c02018-06-22 19:19:26 +0800971 BOOLEAN FindBestDtb = FALSE;
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530972
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700973 if (!DtboImgBuffer) {
974 DEBUG ((EFI_D_ERROR, "Dtbo Img buffer is NULL\n"));
975 return NULL;
976 }
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530977
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700978 FirstDtboTableEntryOffset = fdt32_to_cpu (DtboTableHdr->DtEntryOffset);
979 if (CHECK_ADD64 ((UINT64)DtboImgBuffer, FirstDtboTableEntryOffset)) {
980 DEBUG ((EFI_D_ERROR, "Integer overflow deteced with Dtbo address\n"));
981 return NULL;
982 }
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530983
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700984 DtboTableEntry =
985 (struct DtboTableEntry *)(DtboImgBuffer + FirstDtboTableEntryOffset);
986 if (!DtboTableEntry) {
987 DEBUG ((EFI_D_ERROR, "No proper DtTable\n"));
988 return NULL;
989 }
Mukesh Ojhaa1bd1f82017-05-25 19:04:21 +0530990
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700991 DtboTableEntriesCount = fdt32_to_cpu (DtboTableHdr->DtEntryCount);
992 for (DtboCount = 0; DtboCount < DtboTableEntriesCount; DtboCount++) {
993 if (CHECK_ADD64 ((UINT64)DtboImgBuffer,
994 fdt32_to_cpu (DtboTableEntry->DtOffset))) {
Mukesh Ojha82911f82018-04-30 16:45:07 +0530995 DEBUG ((EFI_D_ERROR, "Integer overflow detected with Dtbo address\n"));
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700996 return NULL;
997 }
998 BoardDtb = DtboImgBuffer + fdt32_to_cpu (DtboTableEntry->DtOffset);
999 if (fdt_check_header (BoardDtb) || fdt_check_header_ext (BoardDtb)) {
1000 DEBUG ((EFI_D_ERROR, "No Valid Dtb\n"));
1001 break;
1002 }
Mukesh Ojhaa1bd1f82017-05-25 19:04:21 +05301003
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001004 CurDtbInfo.Dtb = BoardDtb;
lijuang6fa55c02018-06-22 19:19:26 +08001005 FindBestDtb = ReadDtbFindMatch (&CurDtbInfo, &BestDtbInfo, VARIANT_MATCH);
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001006 DEBUG ((EFI_D_VERBOSE, "Dtbo count = %u LocalBoardDtMatch = %x"
1007 "\n",
1008 DtboCount, CurDtbInfo.DtMatchVal));
lijuang6fa55c02018-06-22 19:19:26 +08001009
1010 if (FindBestDtb) {
1011 DtboIdx = DtboCount;
1012 }
1013
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001014 DtboTableEntry++;
1015 }
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +05301016
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001017 if (!BestDtbInfo.Dtb) {
1018 DEBUG ((EFI_D_ERROR, "Unable to find the Board Dtb\n"));
1019 return NULL;
1020 }
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +05301021
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001022 return BestDtbInfo.Dtb;
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +05301023}
1024
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001025/* Returns 0 if the device tree is valid. */
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001026int
1027DeviceTreeValidate (UINT8 *DeviceTreeBuff,
1028 UINT32 PageSize,
1029 UINT32 *DeviceTreeSize)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001030{
lijuangc336a292017-08-22 19:48:49 +08001031 UINT32 dt_entry_size;
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001032 UINT64 hdr_size;
1033 struct dt_table *table;
1034 if (DeviceTreeSize) {
1035 table = (struct dt_table *)DeviceTreeBuff;
1036 if (table->magic != DEV_TREE_MAGIC) {
1037 // bad magic in device tree table
1038 return -1;
1039 }
1040 if (table->version == DEV_TREE_VERSION_V1) {
1041 dt_entry_size = sizeof (struct dt_entry_v1);
1042 } else if (table->version == DEV_TREE_VERSION_V2) {
1043 dt_entry_size = sizeof (struct dt_entry_v2);
1044 } else if (table->version == DEV_TREE_VERSION_V3) {
1045 dt_entry_size = sizeof (struct dt_entry);
1046 } else {
1047 // unsupported dt version
1048 return -1;
1049 }
1050 hdr_size =
1051 ((UINT64)table->num_entries * dt_entry_size) + DEV_TREE_HEADER_SIZE;
1052 // hdr_size = ROUNDUP(hdr_size, PageSize);
1053 hdr_size = EFI_SIZE_TO_PAGES (hdr_size);
1054 if (hdr_size > MAX_UINT64)
1055 return -1;
1056 else
1057 *DeviceTreeSize = hdr_size & MAX_UINT64;
1058 // dt_entry_ptr = (struct dt_entry *)((CHAR8 *)table +
1059 // DEV_TREE_HEADER_SIZE);
1060 // table_ptr = dt_entry_ptr;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001061
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001062 DEBUG ((EFI_D_ERROR, "DT Total number of entries: %d, DTB version: %d\n",
1063 table->num_entries, table->version));
1064 }
1065 return 0;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001066}
1067
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001068STATIC int
1069platform_dt_absolute_match (struct dt_entry *cur_dt_entry,
1070 struct dt_entry_node *dt_list)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001071{
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001072 UINT32 cur_dt_hw_platform;
1073 UINT32 cur_dt_hw_subtype;
1074 UINT32 cur_dt_msm_id;
1075 dt_node *dt_node_tmp = NULL;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001076
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001077 /* Platform-id
1078 * bit no |31 24|23 16|15 0|
1079 * |reserved|foundry-id|msm-id|
1080 */
1081 cur_dt_msm_id = (cur_dt_entry->platform_id & 0x0000ffff);
1082 cur_dt_hw_platform = (cur_dt_entry->variant_id & 0x000000ff);
1083 cur_dt_hw_subtype = (cur_dt_entry->board_hw_subtype & 0xff);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001084
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001085 /* 1. must match the msm_id, platform_hw_id, platform_subtype and DDR size
1086 * soc, board major/minor, pmic major/minor must less than board info
1087 * 2. find the matched DTB then return 1
1088 * 3. otherwise return 0
1089 */
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001090
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001091 if ((cur_dt_msm_id == (BoardPlatformRawChipId () & 0x0000ffff)) &&
1092 (cur_dt_hw_platform == BoardPlatformType ()) &&
1093 (cur_dt_hw_subtype == BoardPlatformSubType ()) &&
1094 (cur_dt_entry->soc_rev <= BoardPlatformChipVersion ()) &&
1095 ((cur_dt_entry->variant_id & 0x00ffff00) <=
1096 (BoardTargetId () & 0x00ffff00)) &&
1097 (cur_dt_entry->pmic_rev[0] <= BoardPmicTarget (0)) &&
1098 (cur_dt_entry->pmic_rev[1] <= BoardPmicTarget (1)) &&
1099 (cur_dt_entry->pmic_rev[2] <= BoardPmicTarget (2)) &&
1100 (cur_dt_entry->pmic_rev[3] <= BoardPmicTarget (3))) {
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001101
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001102 dt_node_tmp = dt_entry_list_init ();
1103 if (!dt_node_tmp) {
1104 DEBUG ((EFI_D_ERROR, "dt_node_tmp is NULL\n"));
1105 return 0;
1106 }
Vijay Kumar Pendoti1e847bd2016-11-21 15:29:13 +05301107
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001108 gBS->CopyMem ((VOID *)dt_node_tmp->dt_entry_m, (VOID *)cur_dt_entry,
1109 sizeof (struct dt_entry));
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001110
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001111 DEBUG (
1112 (EFI_D_VERBOSE,
1113 "Add DTB entry 0x%x/%08x/0x%08x/0x%x/0x%x/0x%x/0x%x/0x%x/0x%x/0x%x\n",
1114 dt_node_tmp->dt_entry_m->platform_id,
1115 dt_node_tmp->dt_entry_m->variant_id,
1116 dt_node_tmp->dt_entry_m->board_hw_subtype,
1117 dt_node_tmp->dt_entry_m->soc_rev, dt_node_tmp->dt_entry_m->pmic_rev[0],
1118 dt_node_tmp->dt_entry_m->pmic_rev[1],
1119 dt_node_tmp->dt_entry_m->pmic_rev[2],
1120 dt_node_tmp->dt_entry_m->pmic_rev[3], dt_node_tmp->dt_entry_m->offset,
1121 dt_node_tmp->dt_entry_m->size));
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001122
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001123 insert_dt_entry_in_queue (dt_list, dt_node_tmp);
1124 return 1;
1125 }
1126 return 0;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001127}
1128
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001129int
1130platform_dt_absolute_compat_match (struct dt_entry_node *dt_list,
1131 UINT32 dtb_info)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001132{
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001133 struct dt_entry_node *dt_node_tmp1 = NULL;
1134 struct dt_entry_node *dt_node_tmp2 = NULL;
1135 UINT32 current_info = 0;
1136 UINT32 board_info = 0;
1137 UINT32 best_info = 0;
1138 UINT32 current_pmic_model[4] = {0, 0, 0, 0};
1139 UINT32 board_pmic_model[4] = {0, 0, 0, 0};
1140 UINT32 best_pmic_model[4] = {0, 0, 0, 0};
1141 UINT32 delete_current_dt = 0;
1142 UINT32 i;
1143
1144 /* start to select the exact entry
1145 * default to exact match 0, if find current DTB entry info is the same as
1146 * board info,
1147 * then exact match board info.
1148 */
1149 list_for_every_entry (&dt_list->node, dt_node_tmp1, dt_node, node)
1150 {
1151 if (!dt_node_tmp1) {
1152 DEBUG ((EFI_D_ERROR, "Current node is the end\n"));
1153 break;
1154 }
1155 switch (dtb_info) {
1156 case DTB_FOUNDRY:
1157 current_info = ((dt_node_tmp1->dt_entry_m->platform_id) & 0x00ff0000);
1158 board_info = BoardPlatformFoundryId () << 16;
1159 break;
1160 case DTB_PMIC_MODEL:
1161 for (i = 0; i < 4; i++) {
1162 current_pmic_model[i] = (dt_node_tmp1->dt_entry_m->pmic_rev[i] & 0xff);
1163 board_pmic_model[i] = BoardPmicModel (i);
1164 }
1165 break;
1166 default:
1167 DEBUG ((EFI_D_ERROR,
1168 "ERROR: Unsupported version (%d) in dt node check \n", dtb_info));
1169 return 0;
1170 }
1171
1172 if (dtb_info == DTB_PMIC_MODEL) {
1173 if ((current_pmic_model[0] == board_pmic_model[0]) &&
1174 (current_pmic_model[1] == board_pmic_model[1]) &&
1175 (current_pmic_model[2] == board_pmic_model[2]) &&
1176 (current_pmic_model[3] == board_pmic_model[3])) {
1177
1178 for (i = 0; i < 4; i++) {
1179 best_pmic_model[i] = current_pmic_model[i];
1180 }
1181 break;
1182 }
1183 } else {
1184 if (current_info == board_info) {
1185 best_info = current_info;
1186 break;
1187 }
1188 }
1189 }
1190
1191 list_for_every_entry (&dt_list->node, dt_node_tmp1, dt_node, node)
1192 {
1193 if (!dt_node_tmp1) {
1194 DEBUG ((EFI_D_ERROR, "Current node is the end\n"));
1195 break;
1196 }
1197 switch (dtb_info) {
1198 case DTB_FOUNDRY:
1199 current_info = ((dt_node_tmp1->dt_entry_m->platform_id) & 0x00ff0000);
1200 break;
1201 case DTB_PMIC_MODEL:
1202 for (i = 0; i < 4; i++) {
1203 current_pmic_model[i] = (dt_node_tmp1->dt_entry_m->pmic_rev[i] & 0xff);
1204 }
1205 break;
1206 default:
1207 DEBUG ((EFI_D_ERROR,
1208 "ERROR: Unsupported version (%d) in dt node check \n", dtb_info));
1209 return 0;
1210 }
1211
1212 if (dtb_info == DTB_PMIC_MODEL) {
1213 if ((current_pmic_model[0] != best_pmic_model[0]) ||
1214 (current_pmic_model[1] != best_pmic_model[1]) ||
1215 (current_pmic_model[2] != best_pmic_model[2]) ||
1216 (current_pmic_model[3] != best_pmic_model[3])) {
1217
1218 delete_current_dt = 1;
1219 }
1220 } else {
1221 if (current_info != best_info) {
1222 delete_current_dt = 1;
1223 }
1224 }
1225
1226 if (delete_current_dt) {
1227 DEBUG (
1228 (EFI_D_VERBOSE,
1229 "Delete don't fit DTB entry %u/%08x/0x%08x/%x/%x/%x/%x/%x/%x/%x\n",
1230 dt_node_tmp1->dt_entry_m->platform_id,
1231 dt_node_tmp1->dt_entry_m->variant_id,
1232 dt_node_tmp1->dt_entry_m->board_hw_subtype,
1233 dt_node_tmp1->dt_entry_m->soc_rev,
1234 dt_node_tmp1->dt_entry_m->pmic_rev[0],
1235 dt_node_tmp1->dt_entry_m->pmic_rev[1],
1236 dt_node_tmp1->dt_entry_m->pmic_rev[2],
1237 dt_node_tmp1->dt_entry_m->pmic_rev[3],
1238 dt_node_tmp1->dt_entry_m->offset, dt_node_tmp1->dt_entry_m->size));
1239
1240 dt_node_tmp2 = (struct dt_entry_node *)dt_node_tmp1->node.prev;
1241 dt_entry_list_delete (dt_node_tmp1);
1242 dt_node_tmp1 = dt_node_tmp2;
1243 delete_current_dt = 0;
1244 }
1245 }
1246
1247 return 1;
1248}
1249
1250int
1251update_dtb_entry_node (struct dt_entry_node *dt_list, UINT32 dtb_info)
1252{
1253 struct dt_entry_node *dt_node_tmp1 = NULL;
1254 struct dt_entry_node *dt_node_tmp2 = NULL;
1255 UINT32 current_info = 0;
1256 UINT32 board_info = 0;
1257 UINT32 best_info = 0;
1258
1259 /* start to select the best entry*/
1260 list_for_every_entry (&dt_list->node, dt_node_tmp1, dt_node, node)
1261 {
1262 if (!dt_node_tmp1) {
1263 DEBUG ((EFI_D_ERROR, "Current node is the end\n"));
1264 break;
1265 }
1266 switch (dtb_info) {
1267 case DTB_SOC:
1268 current_info = dt_node_tmp1->dt_entry_m->soc_rev;
1269 board_info = BoardPlatformChipVersion ();
1270 break;
1271 case DTB_MAJOR_MINOR:
1272 current_info = ((dt_node_tmp1->dt_entry_m->variant_id) & 0x00ffff00);
1273 board_info = BoardTargetId () & 0x00ffff00;
1274 break;
1275 case DTB_PMIC0:
1276 current_info = dt_node_tmp1->dt_entry_m->pmic_rev[0];
1277 board_info = BoardPmicTarget (0);
1278 break;
1279 case DTB_PMIC1:
1280 current_info = dt_node_tmp1->dt_entry_m->pmic_rev[1];
1281 board_info = BoardPmicTarget (1);
1282 break;
1283 case DTB_PMIC2:
1284 current_info = dt_node_tmp1->dt_entry_m->pmic_rev[2];
1285 board_info = BoardPmicTarget (2);
1286 break;
1287 case DTB_PMIC3:
1288 current_info = dt_node_tmp1->dt_entry_m->pmic_rev[3];
1289 board_info = BoardPmicTarget (3);
1290 break;
1291 default:
1292 DEBUG ((EFI_D_ERROR,
1293 "ERROR: Unsupported version (%d) in dt node check \n", dtb_info));
1294 return 0;
1295 }
1296
1297 if (current_info == board_info) {
1298 best_info = current_info;
1299 break;
1300 }
1301 if ((current_info < board_info) && (current_info > best_info)) {
1302 best_info = current_info;
1303 }
1304 if (current_info < best_info) {
1305 DEBUG (
1306 (EFI_D_ERROR,
1307 "Delete don't fit DTB entry %u/%08x/0x%08x/%x/%x/%x/%x/%x/%x/%x\n",
1308 dt_node_tmp1->dt_entry_m->platform_id,
1309 dt_node_tmp1->dt_entry_m->variant_id,
1310 dt_node_tmp1->dt_entry_m->board_hw_subtype,
1311 dt_node_tmp1->dt_entry_m->soc_rev,
1312 dt_node_tmp1->dt_entry_m->pmic_rev[0],
1313 dt_node_tmp1->dt_entry_m->pmic_rev[1],
1314 dt_node_tmp1->dt_entry_m->pmic_rev[2],
1315 dt_node_tmp1->dt_entry_m->pmic_rev[3],
1316 dt_node_tmp1->dt_entry_m->offset, dt_node_tmp1->dt_entry_m->size));
1317
1318 dt_node_tmp2 = (struct dt_entry_node *)dt_node_tmp1->node.prev;
1319 dt_entry_list_delete (dt_node_tmp1);
1320 dt_node_tmp1 = dt_node_tmp2;
1321 }
1322 }
1323
1324 list_for_every_entry (&dt_list->node, dt_node_tmp1, dt_node, node)
1325 {
1326 if (!dt_node_tmp1) {
1327 DEBUG ((EFI_D_ERROR, "Current node is the end\n"));
1328 break;
1329 }
1330 switch (dtb_info) {
1331 case DTB_SOC:
1332 current_info = dt_node_tmp1->dt_entry_m->soc_rev;
1333 break;
1334 case DTB_MAJOR_MINOR:
1335 current_info = ((dt_node_tmp1->dt_entry_m->variant_id) & 0x00ffff00);
1336 break;
1337 case DTB_PMIC0:
1338 current_info = dt_node_tmp1->dt_entry_m->pmic_rev[0];
1339 break;
1340 case DTB_PMIC1:
1341 current_info = dt_node_tmp1->dt_entry_m->pmic_rev[1];
1342 break;
1343 case DTB_PMIC2:
1344 current_info = dt_node_tmp1->dt_entry_m->pmic_rev[2];
1345 break;
1346 case DTB_PMIC3:
1347 current_info = dt_node_tmp1->dt_entry_m->pmic_rev[3];
1348 break;
1349 default:
1350 DEBUG ((EFI_D_ERROR,
1351 "ERROR: Unsupported version (%d) in dt node check \n", dtb_info));
1352 return 0;
1353 }
1354
1355 if (current_info != best_info) {
1356 DEBUG (
1357 (EFI_D_VERBOSE,
1358 "Delete don't fit DTB entry %u/%08x/0x%08x/%x/%x/%x/%x/%x/%x/%x\n",
1359 dt_node_tmp1->dt_entry_m->platform_id,
1360 dt_node_tmp1->dt_entry_m->variant_id,
1361 dt_node_tmp1->dt_entry_m->board_hw_subtype,
1362 dt_node_tmp1->dt_entry_m->soc_rev,
1363 dt_node_tmp1->dt_entry_m->pmic_rev[0],
1364 dt_node_tmp1->dt_entry_m->pmic_rev[1],
1365 dt_node_tmp1->dt_entry_m->pmic_rev[2],
1366 dt_node_tmp1->dt_entry_m->pmic_rev[3],
1367 dt_node_tmp1->dt_entry_m->offset, dt_node_tmp1->dt_entry_m->size));
1368
1369 dt_node_tmp2 = (struct dt_entry_node *)dt_node_tmp1->node.prev;
1370 dt_entry_list_delete (dt_node_tmp1);
1371 dt_node_tmp1 = dt_node_tmp2;
1372 }
1373 }
1374 return 1;
1375}
1376
1377STATIC struct dt_entry *
1378platform_dt_match_best (struct dt_entry_node *dt_list)
1379{
1380 struct dt_entry_node *dt_node_tmp1 = NULL;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001381
lijuangc336a292017-08-22 19:48:49 +08001382 /* check Foundry id
1383 * the foundry id must exact match board founddry id, this is compatibility
1384 * check, if couldn't find the exact match from DTB, will exact match 0x0.
1385 */
1386 platform_dt_absolute_compat_match (dt_list, DTB_FOUNDRY);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001387
lijuangc336a292017-08-22 19:48:49 +08001388 /* check PMIC model
1389 * the PMIC model must exact match board PMIC model, this is compatibility
1390 * check, if couldn't find the exact match from DTB, will exact match 0x0.
1391 */
1392 platform_dt_absolute_compat_match (dt_list, DTB_PMIC_MODEL);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001393
lijuangc336a292017-08-22 19:48:49 +08001394 /* check soc version
1395 * the suitable soc version must less than or equal to board soc version
1396 */
1397 update_dtb_entry_node (dt_list, DTB_SOC);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001398
lijuangc336a292017-08-22 19:48:49 +08001399 /*check major and minor version
1400 * the suitable major&minor version must less than or equal to board
1401 * major&minor version
1402 */
1403 update_dtb_entry_node (dt_list, DTB_MAJOR_MINOR);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001404
lijuangc336a292017-08-22 19:48:49 +08001405 /*check pmic info
1406 * the suitable pmic major&minor info must less than or equal to board pmic
1407 * major&minor version
1408 */
1409 update_dtb_entry_node (dt_list, DTB_PMIC0);
1410 update_dtb_entry_node (dt_list, DTB_PMIC1);
1411 update_dtb_entry_node (dt_list, DTB_PMIC2);
1412 update_dtb_entry_node (dt_list, DTB_PMIC3);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001413
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001414 list_for_every_entry (&dt_list->node, dt_node_tmp1, dt_node, node)
1415 {
1416 if (!dt_node_tmp1) {
1417 DEBUG ((EFI_D_ERROR, "ERROR: Couldn't find the suitable DTB!\n"));
1418 return NULL;
1419 }
1420 if (dt_node_tmp1->dt_entry_m)
1421 return dt_node_tmp1->dt_entry_m;
1422 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001423
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001424 return NULL;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001425}
Bhanuprakash Modemed797c72018-09-05 20:39:07 +05301426
1427BOOLEAN
1428AppendToDtList (struct fdt_entry_node **DtList,
1429 UINT64 Address,
1430 UINT64 Size) {
1431 struct fdt_entry_node *Current = *DtList;
1432
1433 if (*DtList == NULL) {
1434 DEBUG ((EFI_D_VERBOSE, "DTs list: NULL\n"));
Bhanuprakash Modem763cdd52018-11-01 14:49:55 +05301435 Current = AllocateZeroPool (sizeof (struct fdt_entry_node));
Bhanuprakash Modemed797c72018-09-05 20:39:07 +05301436 if (!Current) {
1437 return FALSE;
1438 }
1439
1440 Current->address = Address;
1441 Current->size = Size;
1442 Current->next = NULL;
1443 *DtList = Current;
1444 return TRUE;
1445 } else {
1446
1447 while (Current->next != NULL) {
1448 Current = Current->next;
1449 }
1450
Bhanuprakash Modem763cdd52018-11-01 14:49:55 +05301451 Current->next = AllocateZeroPool (sizeof (struct fdt_entry_node));
Bhanuprakash Modemed797c72018-09-05 20:39:07 +05301452 if (!Current->next) {
1453 return FALSE;
1454 }
1455 Current->next->address = Address;
1456 Current->next->size = Size;
1457 Current->next->next = NULL;
1458 return TRUE;
1459 }
1460}
1461
1462VOID DeleteDtList (struct fdt_entry_node** DtList)
1463{
1464 struct fdt_entry_node *Current = *DtList;
1465 struct fdt_entry_node *Next;
1466
1467 while (Current != NULL) {
1468 Next = Current->next;
1469 FreePool (Current);
1470 Current = Next;
1471 }
1472
1473 *DtList = NULL;
1474}