Lijuan Gao | 1259da7 | 2021-03-05 10:39:12 +0800 | [diff] [blame^] | 1 | /* Copyright (c) 2012-2021, The Linux Foundation. All rights reserved. |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 2 | * |
| 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 Wang | e74d804 | 2016-07-19 18:09:07 -0700 | [diff] [blame] | 30 | #include "UpdateDeviceTree.h" |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 31 | #include <Library/Board.h> |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 32 | #include <Library/BootLinux.h> |
| 33 | #include <Library/PartitionTableUpdate.h> |
| 34 | #include <Library/Rtic.h> |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 35 | // Variables to initialize board data |
| 36 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 37 | STATIC int |
| 38 | platform_dt_absolute_match (struct dt_entry *cur_dt_entry, |
| 39 | struct dt_entry_node *dt_list); |
| 40 | STATIC struct dt_entry * |
| 41 | platform_dt_match_best (struct dt_entry_node *dt_list); |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 42 | |
Mukesh Ojha | a1bd1f8 | 2017-05-25 19:04:21 +0530 | [diff] [blame] | 43 | STATIC BOOLEAN DtboNeed = TRUE; |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 44 | |
lijuang | 6fa55c0 | 2018-06-22 19:19:26 +0800 | [diff] [blame] | 45 | STATIC INT32 DtboIdx = INVALID_PTN; |
| 46 | INT32 GetDtboIdx (VOID) |
| 47 | { |
| 48 | return DtboIdx; |
| 49 | } |
| 50 | |
Prakruthi Deepak Heragu | 9c385aa | 2019-03-20 10:54:29 -0700 | [diff] [blame] | 51 | STATIC INT32 DtbIdx = INVALID_PTN; |
| 52 | INT32 GetDtbIdx (VOID) |
| 53 | { |
| 54 | return DtbIdx; |
| 55 | } |
| 56 | |
lijuang | f0bbcad | 2017-08-16 16:59:18 +0800 | [diff] [blame] | 57 | BOOLEAN GetDtboNeeded (VOID) |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 58 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 59 | return DtboNeed; |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 60 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 61 | /* Add function to allocate dt entry list, used for recording |
| 62 | * the entry which conform to platform_dt_absolute_match() |
| 63 | */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 64 | static struct dt_entry_node *dt_entry_list_init (VOID) |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 65 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 66 | struct dt_entry_node *dt_node_member = NULL; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 67 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 68 | dt_node_member = |
Bhanuprakash Modem | 763cdd5 | 2018-11-01 14:49:55 +0530 | [diff] [blame] | 69 | (struct dt_entry_node *)AllocateZeroPool (sizeof (struct dt_entry_node)); |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 70 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 71 | if (!dt_node_member) { |
| 72 | DEBUG ((EFI_D_ERROR, "Failed to allocate memory for dt_node_member\n")); |
| 73 | return NULL; |
| 74 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 75 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 76 | list_clear_node (&dt_node_member->node); |
| 77 | dt_node_member->dt_entry_m = |
Bhanuprakash Modem | 763cdd5 | 2018-11-01 14:49:55 +0530 | [diff] [blame] | 78 | (struct dt_entry *)AllocateZeroPool (sizeof (struct dt_entry)); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 79 | if (!dt_node_member->dt_entry_m) { |
| 80 | DEBUG ((EFI_D_ERROR, |
| 81 | "Failed to allocate memory for dt_node_member->dt_entry_m\n")); |
| 82 | FreePool (dt_node_member); |
| 83 | dt_node_member = NULL; |
| 84 | return NULL; |
| 85 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 86 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 87 | return dt_node_member; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 90 | static VOID |
| 91 | insert_dt_entry_in_queue (struct dt_entry_node *dt_list, |
| 92 | struct dt_entry_node *dt_node_member) |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 93 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 94 | list_add_tail (&dt_list->node, &dt_node_member->node); |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 97 | static VOID |
| 98 | dt_entry_list_delete (struct dt_entry_node *dt_node_member) |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 99 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 100 | if (list_in_list (&dt_node_member->node)) { |
| 101 | list_delete (&dt_node_member->node); |
| 102 | FreePool (dt_node_member->dt_entry_m); |
| 103 | dt_node_member->dt_entry_m = NULL; |
| 104 | FreePool (dt_node_member); |
| 105 | dt_node_member = NULL; |
| 106 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 109 | static BOOLEAN |
| 110 | DeviceTreeCompatible (VOID *dtb, |
| 111 | UINT32 dtb_size, |
| 112 | struct dt_entry_node *dtb_list) |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 113 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 114 | int root_offset; |
| 115 | const VOID *prop = NULL; |
| 116 | const char *plat_prop = NULL; |
| 117 | const char *board_prop = NULL; |
| 118 | const char *pmic_prop = NULL; |
| 119 | char *model = NULL; |
| 120 | struct dt_entry *dt_entry_array = NULL; |
| 121 | struct board_id *board_data = NULL; |
| 122 | struct plat_id *platform_data = NULL; |
| 123 | struct pmic_id *pmic_data = NULL; |
| 124 | int len; |
| 125 | int len_board_id; |
| 126 | int len_plat_id; |
| 127 | int min_plat_id_len = 0; |
| 128 | int len_pmic_id; |
| 129 | UINT32 dtb_ver; |
jianzhou | e687ba8 | 2017-11-27 16:08:51 +0800 | [diff] [blame] | 130 | UINT64 NumEntries = 0; |
| 131 | UINT64 i; |
| 132 | UINT32 j, k, n; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 133 | UINT32 msm_data_count; |
| 134 | UINT32 board_data_count; |
| 135 | UINT32 pmic_data_count; |
| 136 | BOOLEAN Result = FALSE; |
Prakruthi Deepak Heragu | 9c385aa | 2019-03-20 10:54:29 -0700 | [diff] [blame] | 137 | static UINT32 DtbCount; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 138 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 139 | root_offset = fdt_path_offset (dtb, "/"); |
| 140 | if (root_offset < 0) |
| 141 | return FALSE; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 142 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 143 | prop = fdt_getprop (dtb, root_offset, "model", &len); |
| 144 | if (prop && len > 0) { |
Bhanuprakash Modem | 763cdd5 | 2018-11-01 14:49:55 +0530 | [diff] [blame] | 145 | model = (char *)AllocateZeroPool (sizeof (char) * len); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 146 | if (!model) { |
| 147 | DEBUG ((EFI_D_ERROR, "Failed to allocate memory for model\n")); |
| 148 | return FALSE; |
| 149 | } |
| 150 | AsciiStrnCpyS (model, (sizeof (CHAR8) * len), prop, len); |
| 151 | } else { |
| 152 | DEBUG ((EFI_D_ERROR, "model does not exist in device tree\n")); |
| 153 | } |
| 154 | /* Find the pmic-id prop from DTB , if pmic-id is present then |
| 155 | * the DTB is version 3, otherwise find the board-id prop from DTB , |
| 156 | * if board-id is present then the DTB is version 2 */ |
| 157 | pmic_prop = (const char *)fdt_getprop (dtb, root_offset, "qcom,pmic-id", |
| 158 | &len_pmic_id); |
| 159 | board_prop = (const char *)fdt_getprop (dtb, root_offset, "qcom,board-id", |
| 160 | &len_board_id); |
| 161 | if (pmic_prop && (len_pmic_id > 0) && board_prop && (len_board_id > 0)) { |
| 162 | if ((len_pmic_id % PMIC_ID_SIZE) || (len_board_id % BOARD_ID_SIZE)) { |
| 163 | DEBUG ((EFI_D_ERROR, "qcom,pmic-id (%d) or qcom,board-id(%d) in device " |
| 164 | "tree is not a multiple of (%d %d)\n", |
| 165 | len_pmic_id, len_board_id, PMIC_ID_SIZE, BOARD_ID_SIZE)); |
| 166 | goto Exit; |
| 167 | } |
| 168 | dtb_ver = DEV_TREE_VERSION_V3; |
| 169 | min_plat_id_len = PLAT_ID_SIZE; |
| 170 | } else if (board_prop && len_board_id > 0) { |
| 171 | if (len_board_id % BOARD_ID_SIZE) { |
| 172 | DEBUG ((EFI_D_ERROR, |
Elliot Berman | bc28726 | 2020-01-16 09:46:53 -0800 | [diff] [blame] | 173 | "qcom,board-id (%d) in device tree is not a multiple of (%d)\n", |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 174 | len_board_id, BOARD_ID_SIZE)); |
| 175 | goto Exit; |
| 176 | } |
| 177 | dtb_ver = DEV_TREE_VERSION_V2; |
| 178 | min_plat_id_len = PLAT_ID_SIZE; |
| 179 | } else { |
| 180 | dtb_ver = DEV_TREE_VERSION_V1; |
| 181 | min_plat_id_len = DT_ENTRY_V1_SIZE; |
| 182 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 183 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 184 | /* Get the msm-id prop from DTB */ |
| 185 | plat_prop = |
| 186 | (const char *)fdt_getprop (dtb, root_offset, "qcom,msm-id", &len_plat_id); |
| 187 | if (!plat_prop || len_plat_id <= 0) { |
| 188 | DEBUG ((EFI_D_VERBOSE, "qcom,msm-id entry not found\n")); |
| 189 | goto Exit; |
| 190 | } else if (len_plat_id % min_plat_id_len) { |
| 191 | DEBUG ((EFI_D_ERROR, |
| 192 | "qcom, msm-id in device tree is (%d) not a multiple of (%d)\n", |
| 193 | len_plat_id, min_plat_id_len)); |
| 194 | goto Exit; |
| 195 | } |
| 196 | if (dtb_ver == DEV_TREE_VERSION_V2 || dtb_ver == DEV_TREE_VERSION_V3) { |
| 197 | board_data_count = (len_board_id / BOARD_ID_SIZE); |
| 198 | msm_data_count = (len_plat_id / PLAT_ID_SIZE); |
| 199 | /* If dtb version is v2.0, the pmic_data_count will be <= 0 */ |
| 200 | pmic_data_count = (len_pmic_id / PMIC_ID_SIZE); |
| 201 | |
| 202 | /* If we are using dtb v3.0, then we have split board, msm & pmic data in |
| 203 | * the DTB |
| 204 | * If we are using dtb v2.0, then we have split board & msmdata in the DTB |
| 205 | */ |
Bhanuprakash Modem | 763cdd5 | 2018-11-01 14:49:55 +0530 | [diff] [blame] | 206 | board_data = (struct board_id *)AllocateZeroPool ( |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 207 | sizeof (struct board_id) * (len_board_id / BOARD_ID_SIZE)); |
| 208 | if (!board_data) { |
| 209 | DEBUG ((EFI_D_ERROR, "Failed to allocate memory for board_data\n")); |
| 210 | goto Exit; |
| 211 | } |
| 212 | |
Bhanuprakash Modem | 763cdd5 | 2018-11-01 14:49:55 +0530 | [diff] [blame] | 213 | platform_data = (struct plat_id *)AllocateZeroPool ( |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 214 | sizeof (struct plat_id) * (len_plat_id / PLAT_ID_SIZE)); |
| 215 | if (!platform_data) { |
| 216 | DEBUG ((EFI_D_ERROR, "Failed to allocate memory for platform_data\n")); |
| 217 | goto Exit; |
| 218 | } |
| 219 | if (dtb_ver == DEV_TREE_VERSION_V3) { |
Bhanuprakash Modem | 763cdd5 | 2018-11-01 14:49:55 +0530 | [diff] [blame] | 220 | pmic_data = (struct pmic_id *)AllocateZeroPool (sizeof (struct pmic_id) * |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 221 | (len_pmic_id / PMIC_ID_SIZE)); |
| 222 | if (!pmic_data) { |
| 223 | DEBUG ((EFI_D_ERROR, "Failed to allocate memory for pmic_data\n")); |
lijuang | 46df844 | 2017-09-28 19:06:36 +0800 | [diff] [blame] | 224 | goto Exit; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 225 | } |
| 226 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 227 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 228 | /* Extract board data from DTB */ |
| 229 | for (i = 0; i < board_data_count; i++) { |
| 230 | board_data[i].variant_id = |
| 231 | fdt32_to_cpu (((struct board_id *)board_prop)->variant_id); |
| 232 | board_data[i].platform_subtype = |
| 233 | fdt32_to_cpu (((struct board_id *)board_prop)->platform_subtype); |
| 234 | /* For V2/V3 version of DTBs we have platform version field as part |
| 235 | * of variant ID, in such case the subtype will be mentioned as 0x0 |
| 236 | * As the qcom, board-id = <0xSSPMPmPH, 0x0> |
| 237 | * SS -- Subtype |
| 238 | * PM -- Platform major version |
| 239 | * Pm -- Platform minor version |
| 240 | * PH -- Platform hardware CDP/MTP |
| 241 | * In such case to make it compatible with LK algorithm move the subtype |
| 242 | * from variant_id to subtype field |
| 243 | */ |
| 244 | if (board_data[i].platform_subtype == 0) |
| 245 | board_data[i].platform_subtype = |
| 246 | fdt32_to_cpu (((struct board_id *)board_prop)->variant_id) >> 0x18; |
Vijay Kumar Pendoti | 1e847bd | 2016-11-21 15:29:13 +0530 | [diff] [blame] | 247 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 248 | len_board_id -= sizeof (struct board_id); |
| 249 | board_prop += sizeof (struct board_id); |
| 250 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 251 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 252 | /* Extract platform data from DTB */ |
| 253 | for (i = 0; i < msm_data_count; i++) { |
| 254 | platform_data[i].platform_id = |
| 255 | fdt32_to_cpu (((struct plat_id *)plat_prop)->platform_id); |
| 256 | platform_data[i].soc_rev = |
| 257 | fdt32_to_cpu (((struct plat_id *)plat_prop)->soc_rev); |
| 258 | len_plat_id -= sizeof (struct plat_id); |
| 259 | plat_prop += sizeof (struct plat_id); |
| 260 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 261 | |
jianzhou | e687ba8 | 2017-11-27 16:08:51 +0800 | [diff] [blame] | 262 | if ((pmic_data_count != 0) && |
| 263 | (MAX_UINT64 / pmic_data_count < (msm_data_count * board_data_count))) { |
| 264 | DEBUG ((EFI_D_ERROR, "NumEntries exceeds MAX_UINT64\n")); |
| 265 | goto Exit; |
| 266 | } |
| 267 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 268 | if (dtb_ver == DEV_TREE_VERSION_V3 && pmic_prop) { |
| 269 | /* Extract pmic data from DTB */ |
| 270 | for (i = 0; i < pmic_data_count; i++) { |
| 271 | pmic_data[i].pmic_version[0] = |
| 272 | fdt32_to_cpu (((struct pmic_id *)pmic_prop)->pmic_version[0]); |
| 273 | pmic_data[i].pmic_version[1] = |
| 274 | fdt32_to_cpu (((struct pmic_id *)pmic_prop)->pmic_version[1]); |
| 275 | pmic_data[i].pmic_version[2] = |
| 276 | fdt32_to_cpu (((struct pmic_id *)pmic_prop)->pmic_version[2]); |
| 277 | pmic_data[i].pmic_version[3] = |
| 278 | fdt32_to_cpu (((struct pmic_id *)pmic_prop)->pmic_version[3]); |
| 279 | len_pmic_id -= sizeof (struct pmic_id); |
| 280 | pmic_prop += sizeof (struct pmic_id); |
| 281 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 282 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 283 | /* We need to merge board & platform data into dt entry structure */ |
jianzhou | e687ba8 | 2017-11-27 16:08:51 +0800 | [diff] [blame] | 284 | NumEntries = (uint64_t)msm_data_count * board_data_count * |
| 285 | pmic_data_count; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 286 | } else { |
| 287 | /* We need to merge board & platform data into dt entry structure */ |
jianzhou | e687ba8 | 2017-11-27 16:08:51 +0800 | [diff] [blame] | 288 | NumEntries = (uint64_t)msm_data_count * board_data_count; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 289 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 290 | |
Bhanuprakash Modem | 763cdd5 | 2018-11-01 14:49:55 +0530 | [diff] [blame] | 291 | dt_entry_array = (struct dt_entry *)AllocateZeroPool ( |
| 292 | sizeof (struct dt_entry) * |
| 293 | NumEntries); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 294 | if (!dt_entry_array) { |
| 295 | DEBUG ((EFI_D_ERROR, "Failed to allocate memory for dt_entry_array\n")); |
| 296 | goto Exit; |
| 297 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 298 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 299 | /* If we have '<X>; <Y>; <Z>' as platform data & '<A>; <B>; <C>' as board |
| 300 | * data. |
| 301 | * Then dt entry should look like |
| 302 | * <X ,A >;<X, B>;<X, C>; |
| 303 | * <Y ,A >;<Y, B>;<Y, C>; |
| 304 | * <Z ,A >;<Z, B>;<Z, C>; |
| 305 | */ |
| 306 | k = 0; |
Prakruthi Deepak Heragu | 9c385aa | 2019-03-20 10:54:29 -0700 | [diff] [blame] | 307 | DtbCount++; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 308 | for (i = 0; i < msm_data_count; i++) { |
| 309 | for (j = 0; j < board_data_count; j++) { |
| 310 | if (dtb_ver == DEV_TREE_VERSION_V3 && pmic_prop) { |
| 311 | for (n = 0; n < pmic_data_count; n++) { |
| 312 | dt_entry_array[k].platform_id = platform_data[i].platform_id; |
| 313 | dt_entry_array[k].soc_rev = platform_data[i].soc_rev; |
| 314 | dt_entry_array[k].variant_id = board_data[j].variant_id; |
| 315 | dt_entry_array[k].board_hw_subtype = board_data[j].platform_subtype; |
| 316 | dt_entry_array[k].pmic_rev[0] = pmic_data[n].pmic_version[0]; |
| 317 | dt_entry_array[k].pmic_rev[1] = pmic_data[n].pmic_version[1]; |
| 318 | dt_entry_array[k].pmic_rev[2] = pmic_data[n].pmic_version[2]; |
| 319 | dt_entry_array[k].pmic_rev[3] = pmic_data[n].pmic_version[3]; |
| 320 | dt_entry_array[k].offset = (UINT64)dtb; |
| 321 | dt_entry_array[k].size = dtb_size; |
Prakruthi Deepak Heragu | 9c385aa | 2019-03-20 10:54:29 -0700 | [diff] [blame] | 322 | dt_entry_array[k].Idx = DtbCount; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 323 | k++; |
| 324 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 325 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 326 | } else { |
| 327 | dt_entry_array[k].platform_id = platform_data[i].platform_id; |
| 328 | dt_entry_array[k].soc_rev = platform_data[i].soc_rev; |
| 329 | dt_entry_array[k].variant_id = board_data[j].variant_id; |
| 330 | dt_entry_array[k].board_hw_subtype = board_data[j].platform_subtype; |
| 331 | dt_entry_array[k].pmic_rev[0] = BoardPmicTarget (0); |
| 332 | dt_entry_array[k].pmic_rev[1] = BoardPmicTarget (1); |
| 333 | dt_entry_array[k].pmic_rev[2] = BoardPmicTarget (2); |
| 334 | dt_entry_array[k].pmic_rev[3] = BoardPmicTarget (3); |
| 335 | dt_entry_array[k].offset = (UINT64)dtb; |
| 336 | dt_entry_array[k].size = dtb_size; |
Prakruthi Deepak Heragu | 9c385aa | 2019-03-20 10:54:29 -0700 | [diff] [blame] | 337 | dt_entry_array[k].Idx = DtbCount; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 338 | k++; |
lijuang | 46df844 | 2017-09-28 19:06:36 +0800 | [diff] [blame] | 339 | } |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
jianzhou | e687ba8 | 2017-11-27 16:08:51 +0800 | [diff] [blame] | 343 | for (i = 0; i < NumEntries; i++) { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 344 | if (platform_dt_absolute_match (&(dt_entry_array[i]), dtb_list)) { |
| 345 | DEBUG ((EFI_D_VERBOSE, "Device tree exact match the board: <0x%x 0x%x " |
| 346 | "0x%x 0x%x> == <0x%x 0x%x 0x%x 0x%x>\n", |
| 347 | dt_entry_array[i].platform_id, dt_entry_array[i].variant_id, |
| 348 | dt_entry_array[i].soc_rev, dt_entry_array[i].board_hw_subtype, |
| 349 | BoardPlatformRawChipId (), BoardPlatformType (), |
| 350 | BoardPlatformChipVersion (), BoardPlatformSubType ())); |
| 351 | } else { |
| 352 | DEBUG ((EFI_D_VERBOSE, "Device tree's msm_id doesn't match the board: " |
| 353 | "<0x%x 0x%x 0x%x 0x%x> != <0x%x 0x%x 0x%x " |
| 354 | "0x%x>\n", |
| 355 | dt_entry_array[i].platform_id, dt_entry_array[i].variant_id, |
| 356 | dt_entry_array[i].soc_rev, dt_entry_array[i].board_hw_subtype, |
| 357 | BoardPlatformRawChipId (), BoardPlatformType (), |
| 358 | BoardPlatformChipVersion (), BoardPlatformSubType ())); |
| 359 | } |
| 360 | } |
| 361 | Result = TRUE; |
| 362 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 363 | |
lijuang | 46df844 | 2017-09-28 19:06:36 +0800 | [diff] [blame] | 364 | Exit: |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 365 | if (board_data) { |
| 366 | FreePool (board_data); |
| 367 | board_data = NULL; |
| 368 | } |
| 369 | if (platform_data) { |
| 370 | FreePool (platform_data); |
| 371 | platform_data = NULL; |
| 372 | } |
| 373 | if (pmic_data) { |
| 374 | FreePool (pmic_data); |
| 375 | pmic_data = NULL; |
| 376 | } |
| 377 | if (dt_entry_array) { |
| 378 | FreePool (dt_entry_array); |
| 379 | dt_entry_array = NULL; |
| 380 | } |
| 381 | if (model) { |
| 382 | FreePool (model); |
| 383 | model = NULL; |
| 384 | } |
lijuang | 46df844 | 2017-09-28 19:06:36 +0800 | [diff] [blame] | 385 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 386 | return Result; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /* |
| 390 | * Will relocate the DTB to the tags addr if the device tree is found and return |
| 391 | * its address |
| 392 | * |
Prakruthi Deepak Heragu | 9c385aa | 2019-03-20 10:54:29 -0700 | [diff] [blame] | 393 | * For Header Version 2, the arguments Kernel and KernelSize will be |
| 394 | * the entire bootimage and the bootimage size. |
| 395 | * |
| 396 | * Arguments: kernel - Start address of the kernel/bootimage |
| 397 | * loaded in RAM |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 398 | * tags - Start address of the tags loaded in RAM |
Prakruthi Deepak Heragu | 9c385aa | 2019-03-20 10:54:29 -0700 | [diff] [blame] | 399 | * kernel_size - Size of the kernel/bootimage in bytes |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 400 | * |
| 401 | * Return Value: DTB address : If appended device tree is found |
| 402 | * 'NULL' : Otherwise |
| 403 | */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 404 | VOID * |
| 405 | DeviceTreeAppended (VOID *kernel, |
| 406 | UINT32 kernel_size, |
| 407 | UINT32 dtb_offset, |
| 408 | VOID *tags) |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 409 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 410 | EFI_STATUS Status; |
| 411 | uintptr_t kernel_end = (uintptr_t)kernel + kernel_size; |
| 412 | VOID *dtb = NULL; |
| 413 | VOID *bestmatch_tag = NULL; |
| 414 | UINT64 RamdiskLoadAddr; |
| 415 | UINT64 BaseMemory = 0; |
| 416 | struct dt_entry *best_match_dt_entry = NULL; |
| 417 | UINT32 bestmatch_tag_size; |
| 418 | struct dt_entry_node *dt_entry_queue = NULL; |
| 419 | struct dt_entry_node *dt_node_tmp1 = NULL; |
| 420 | struct dt_entry_node *dt_node_tmp2 = NULL; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 421 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 422 | /* Initialize the dtb entry node*/ |
| 423 | dt_entry_queue = |
Bhanuprakash Modem | 763cdd5 | 2018-11-01 14:49:55 +0530 | [diff] [blame] | 424 | (struct dt_entry_node *)AllocateZeroPool (sizeof (struct dt_entry_node)); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 425 | if (!dt_entry_queue) { |
| 426 | DEBUG ((EFI_D_ERROR, "Out of memory\n")); |
| 427 | return NULL; |
| 428 | } |
Jeevan Shriram | e07dd43 | 2016-08-22 11:05:06 -0700 | [diff] [blame] | 429 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 430 | list_initialize (&dt_entry_queue->node); |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 431 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 432 | if (!dtb_offset) { |
| 433 | DEBUG ((EFI_D_ERROR, "DTB offset is NULL\n")); |
| 434 | goto out; |
| 435 | } |
Runmin Wang | 7db4296 | 2016-06-27 14:35:35 -0700 | [diff] [blame] | 436 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 437 | if (((uintptr_t)kernel + (uintptr_t)dtb_offset) < (uintptr_t)kernel) { |
| 438 | goto out; |
| 439 | } |
| 440 | dtb = kernel + dtb_offset; |
| 441 | while (((uintptr_t)dtb + sizeof (struct fdt_header)) < |
| 442 | (uintptr_t)kernel_end) { |
| 443 | struct fdt_header dtb_hdr; |
| 444 | UINT32 dtb_size; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 445 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 446 | /* the DTB could be unaligned, so extract the header, |
| 447 | * and operate on it separately */ |
| 448 | gBS->CopyMem (&dtb_hdr, dtb, sizeof (struct fdt_header)); |
| 449 | if (fdt_check_header ((const VOID *)&dtb_hdr) != 0 || |
| 450 | fdt_check_header_ext ((VOID *)&dtb_hdr) != 0 || |
| 451 | ((uintptr_t)dtb + (uintptr_t)fdt_totalsize ((const VOID *)&dtb_hdr) < |
| 452 | (uintptr_t)dtb) || |
| 453 | ((uintptr_t)dtb + (uintptr_t)fdt_totalsize ((const VOID *)&dtb_hdr) > |
| 454 | (uintptr_t)kernel_end)) |
| 455 | break; |
| 456 | dtb_size = fdt_totalsize (&dtb_hdr); |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 457 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 458 | if (!DeviceTreeCompatible (dtb, dtb_size, dt_entry_queue)) { |
| 459 | DEBUG ((EFI_D_VERBOSE, "Error while DTB parse continue with next DTB\n")); |
| 460 | if (!GetRticDtb (dtb)) |
| 461 | DEBUG ((EFI_D_VERBOSE, |
| 462 | "Error while DTB parsing RTIC prop continue with next DTB\n")); |
| 463 | } |
Mukesh Ojha | a1bd1f8 | 2017-05-25 19:04:21 +0530 | [diff] [blame] | 464 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 465 | /* goto the next device tree if any */ |
| 466 | dtb += dtb_size; |
| 467 | } |
| 468 | best_match_dt_entry = platform_dt_match_best (dt_entry_queue); |
| 469 | if (best_match_dt_entry) { |
| 470 | bestmatch_tag = (VOID *)best_match_dt_entry->offset; |
| 471 | bestmatch_tag_size = best_match_dt_entry->size; |
| 472 | DEBUG ((EFI_D_INFO, "Best match DTB tags " |
| 473 | "%u/%08x/0x%08x/%x/%x/%x/%x/%x/(offset)0x%08x/" |
| 474 | "(size)0x%08x\n", |
| 475 | best_match_dt_entry->platform_id, best_match_dt_entry->variant_id, |
| 476 | best_match_dt_entry->board_hw_subtype, best_match_dt_entry->soc_rev, |
| 477 | best_match_dt_entry->pmic_rev[0], best_match_dt_entry->pmic_rev[1], |
| 478 | best_match_dt_entry->pmic_rev[2], best_match_dt_entry->pmic_rev[3], |
| 479 | best_match_dt_entry->offset, best_match_dt_entry->size)); |
| 480 | DEBUG ((EFI_D_INFO, "Using pmic info 0x%0x/0x%x/0x%x/0x%0x for device " |
| 481 | "0x%0x/0x%x/0x%x/0x%0x\n", |
| 482 | best_match_dt_entry->pmic_rev[0], best_match_dt_entry->pmic_rev[1], |
| 483 | best_match_dt_entry->pmic_rev[2], best_match_dt_entry->pmic_rev[3], |
| 484 | BoardPmicTarget (0), BoardPmicTarget (1), BoardPmicTarget (2), |
| 485 | BoardPmicTarget (3))); |
| 486 | } |
| 487 | /* free queue's memory */ |
| 488 | list_for_every_entry (&dt_entry_queue->node, dt_node_tmp1, dt_node, node) |
| 489 | { |
| 490 | if (!dt_node_tmp1) { |
| 491 | DEBUG ((EFI_D_VERBOSE, "Current node is the end\n")); |
| 492 | break; |
| 493 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 494 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 495 | dt_node_tmp2 = (struct dt_entry_node *)dt_node_tmp1->node.prev; |
| 496 | dt_entry_list_delete (dt_node_tmp1); |
| 497 | dt_node_tmp1 = dt_node_tmp2; |
| 498 | } |
lijuang | f71a484 | 2017-07-27 19:26:31 +0800 | [diff] [blame] | 499 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 500 | if (bestmatch_tag) { |
| 501 | Status = BaseMem (&BaseMemory); |
| 502 | if (Status != EFI_SUCCESS) { |
| 503 | DEBUG ((EFI_D_ERROR, "Unable to find Base memory for DDR %r\n", Status)); |
| 504 | FreePool (dt_entry_queue); |
| 505 | dt_entry_queue = NULL; |
| 506 | goto out; |
| 507 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 508 | |
Venkata Narendra Kumar Gutta | 00f4685 | 2018-11-29 12:38:08 -0800 | [diff] [blame] | 509 | RamdiskLoadAddr = SetandGetLoadAddr (NULL, LOAD_ADDR_RAMDISK); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 510 | if ((RamdiskLoadAddr - (UINT64)tags) > RamdiskLoadAddr) { |
| 511 | DEBUG ((EFI_D_ERROR, "Tags address is not valid\n")); |
| 512 | goto out; |
| 513 | } |
| 514 | if ((RamdiskLoadAddr - (UINT64)tags) < bestmatch_tag_size) { |
| 515 | DEBUG ((EFI_D_ERROR, "Tag size is over the limit\n")); |
| 516 | goto out; |
| 517 | } |
| 518 | gBS->CopyMem (tags, bestmatch_tag, bestmatch_tag_size); |
Prakruthi Deepak Heragu | 9c385aa | 2019-03-20 10:54:29 -0700 | [diff] [blame] | 519 | DtbIdx = best_match_dt_entry->Idx; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 520 | /* clear out the old DTB magic so kernel doesn't find it */ |
| 521 | *((UINT32 *)(kernel + dtb_offset)) = 0; |
lijuang | 46df844 | 2017-09-28 19:06:36 +0800 | [diff] [blame] | 522 | FreePool (dt_entry_queue); |
| 523 | dt_entry_queue = NULL; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 524 | |
| 525 | return tags; |
| 526 | } |
| 527 | |
| 528 | DEBUG ( |
| 529 | (EFI_D_ERROR, |
| 530 | "DTB offset is incorrect, kernel image does not have appended DTB\n")); |
| 531 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 532 | out: |
| 533 | FreePool (dt_entry_queue); |
| 534 | dt_entry_queue = NULL; |
| 535 | return NULL; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 536 | } |
| 537 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 538 | STATIC BOOLEAN |
Lijuan Gao | 1259da7 | 2021-03-05 10:39:12 +0800 | [diff] [blame^] | 539 | CheckAllBitsSet (UINT64 DtMatchVal) |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 540 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 541 | return (DtMatchVal & ALL_BITS_SET) == (ALL_BITS_SET); |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 542 | } |
| 543 | |
Mukesh Ojha | fa81535 | 2017-11-08 14:43:41 +0530 | [diff] [blame] | 544 | STATIC VOID |
Mayank Grover | c8ad1e7 | 2020-12-02 15:14:00 +0530 | [diff] [blame] | 545 | ReadBestPmicMatch (CONST CHAR8 *PmicProp, INT32 PmicMaxIdx, |
| 546 | UINT32 PmicEntCount, PmicIdInfo *BestPmicInfo) |
Mukesh Ojha | fa81535 | 2017-11-08 14:43:41 +0530 | [diff] [blame] | 547 | { |
| 548 | UINT32 PmicEntIdx; |
| 549 | UINT32 Idx; |
Mukesh Ojha | 8ff5b4a | 2017-12-28 17:47:19 +0530 | [diff] [blame] | 550 | PmicIdInfo CurPmicInfo; |
Mukesh Ojha | fa81535 | 2017-11-08 14:43:41 +0530 | [diff] [blame] | 551 | |
Mukesh Ojha | 8ff5b4a | 2017-12-28 17:47:19 +0530 | [diff] [blame] | 552 | memset (BestPmicInfo, 0, sizeof (PmicIdInfo)); |
Mukesh Ojha | fa81535 | 2017-11-08 14:43:41 +0530 | [diff] [blame] | 553 | for (PmicEntIdx = 0; PmicEntIdx < PmicEntCount; PmicEntIdx++) { |
Mukesh Ojha | 8ff5b4a | 2017-12-28 17:47:19 +0530 | [diff] [blame] | 554 | memset (&CurPmicInfo, 0, sizeof (PmicIdInfo)); |
Mayank Grover | c8ad1e7 | 2020-12-02 15:14:00 +0530 | [diff] [blame] | 555 | for (Idx = 0; Idx < PmicMaxIdx; Idx++) { |
Mukesh Ojha | 8ff5b4a | 2017-12-28 17:47:19 +0530 | [diff] [blame] | 556 | CurPmicInfo.DtPmicModel[Idx] = |
Mukesh Ojha | fa81535 | 2017-11-08 14:43:41 +0530 | [diff] [blame] | 557 | fdt32_to_cpu (((struct pmic_id *)PmicProp)->pmic_version[Idx]); |
| 558 | |
| 559 | DEBUG ((EFI_D_VERBOSE, "pmic_data[%u].:%x\n", Idx, |
Mukesh Ojha | 8ff5b4a | 2017-12-28 17:47:19 +0530 | [diff] [blame] | 560 | CurPmicInfo.DtPmicModel[Idx])); |
| 561 | CurPmicInfo.DtPmicRev[Idx] = |
| 562 | CurPmicInfo.DtPmicModel[Idx] & PMIC_REV_MASK; |
| 563 | CurPmicInfo.DtPmicModel[Idx] = |
| 564 | CurPmicInfo.DtPmicModel[Idx] & PMIC_MODEL_MASK; |
Mukesh Ojha | fa81535 | 2017-11-08 14:43:41 +0530 | [diff] [blame] | 565 | |
Mukesh Ojha | 8ff5b4a | 2017-12-28 17:47:19 +0530 | [diff] [blame] | 566 | if ((CurPmicInfo.DtPmicModel[Idx]) == |
Mukesh Ojha | fa81535 | 2017-11-08 14:43:41 +0530 | [diff] [blame] | 567 | BoardPmicModel (Idx)) { |
Mukesh Ojha | 8ff5b4a | 2017-12-28 17:47:19 +0530 | [diff] [blame] | 568 | CurPmicInfo.DtMatchVal |= |
Mukesh Ojha | fa81535 | 2017-11-08 14:43:41 +0530 | [diff] [blame] | 569 | BIT ((PMIC_MATCH_EXACT_MODEL_IDX0 + Idx * PMIC_SHIFT_IDX)); |
Mukesh Ojha | 8ff5b4a | 2017-12-28 17:47:19 +0530 | [diff] [blame] | 570 | } else if (CurPmicInfo.DtPmicModel[Idx] == 0) { |
| 571 | CurPmicInfo.DtMatchVal |= |
Mukesh Ojha | fa81535 | 2017-11-08 14:43:41 +0530 | [diff] [blame] | 572 | BIT ((PMIC_MATCH_DEFAULT_MODEL_IDX0 + Idx * PMIC_SHIFT_IDX)); |
| 573 | } else { |
Mukesh Ojha | 8ff5b4a | 2017-12-28 17:47:19 +0530 | [diff] [blame] | 574 | CurPmicInfo.DtMatchVal = BIT (NONE_MATCH); |
Mukesh Ojha | fa81535 | 2017-11-08 14:43:41 +0530 | [diff] [blame] | 575 | DEBUG ((EFI_D_VERBOSE, "Pmic model does not match\n")); |
| 576 | break; |
| 577 | } |
| 578 | |
Lijuan Gao | 1259da7 | 2021-03-05 10:39:12 +0800 | [diff] [blame^] | 579 | /* first match the first four pmic revision */ |
| 580 | if (Idx < PMIC_IDX4) { |
| 581 | if (CurPmicInfo.DtPmicRev[Idx] == (BoardPmicTarget (Idx) |
| 582 | & PMIC_REV_MASK)) { |
| 583 | CurPmicInfo.DtMatchVal |= |
| 584 | BIT ((PMIC_MATCH_EXACT_REV_IDX0 + Idx * PMIC_SHIFT_IDX)); |
| 585 | } else if (CurPmicInfo.DtPmicRev[Idx] < |
| 586 | (BoardPmicTarget (Idx) & PMIC_REV_MASK)) { |
| 587 | CurPmicInfo.DtMatchVal |= BIT ((PMIC_MATCH_BEST_REV_IDX0 + |
| 588 | Idx * PMIC_SHIFT_IDX)); |
| 589 | } else { |
| 590 | DEBUG ((EFI_D_VERBOSE, "Pmic revision does not match\n")); |
| 591 | break; |
| 592 | } |
Mukesh Ojha | fa81535 | 2017-11-08 14:43:41 +0530 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
Lijuan Gao | 1259da7 | 2021-03-05 10:39:12 +0800 | [diff] [blame^] | 596 | DEBUG ((EFI_D_VERBOSE, "BestPmicInfo.DtMatchVal : 0x%llx" |
| 597 | " CurPmicInfo[%u]->DtMatchVal : 0x%llx\n", BestPmicInfo->DtMatchVal, |
Mukesh Ojha | 8ff5b4a | 2017-12-28 17:47:19 +0530 | [diff] [blame] | 598 | PmicEntIdx, CurPmicInfo.DtMatchVal)); |
| 599 | if (BestPmicInfo->DtMatchVal < CurPmicInfo.DtMatchVal) { |
| 600 | gBS->CopyMem (BestPmicInfo, &CurPmicInfo, |
Mukesh Ojha | fa81535 | 2017-11-08 14:43:41 +0530 | [diff] [blame] | 601 | sizeof (struct PmicIdInfo)); |
| 602 | } else if (BestPmicInfo->DtMatchVal == |
Mukesh Ojha | 8ff5b4a | 2017-12-28 17:47:19 +0530 | [diff] [blame] | 603 | CurPmicInfo.DtMatchVal) { |
Mayank Grover | c8ad1e7 | 2020-12-02 15:14:00 +0530 | [diff] [blame] | 604 | for (Idx = 0; Idx < PmicMaxIdx; Idx++) { |
| 605 | if (BestPmicInfo->DtPmicRev[Idx] < CurPmicInfo.DtPmicRev[Idx]) { |
| 606 | gBS->CopyMem (BestPmicInfo, &CurPmicInfo, sizeof (struct PmicIdInfo)); |
| 607 | } |
Mukesh Ojha | fa81535 | 2017-11-08 14:43:41 +0530 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
Mayank Grover | c8ad1e7 | 2020-12-02 15:14:00 +0530 | [diff] [blame] | 611 | PmicProp += sizeof (UINT32) * PmicMaxIdx; |
Mukesh Ojha | fa81535 | 2017-11-08 14:43:41 +0530 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | |
Kamalpreet Singh | e3b8fe4 | 2017-12-07 14:03:36 -0800 | [diff] [blame] | 615 | STATIC EFI_STATUS GetPlatformMatchDtb (DtInfo * CurDtbInfo, |
| 616 | CONST CHAR8 *PlatProp, |
| 617 | INT32 LenPlatId, |
| 618 | INT32 MinPlatIdLen) |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 619 | { |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 620 | |
lijuang | f6b1c5f | 2018-03-20 13:15:47 +0800 | [diff] [blame] | 621 | if (CurDtbInfo == NULL) { |
Kamalpreet Singh | e3b8fe4 | 2017-12-07 14:03:36 -0800 | [diff] [blame] | 622 | DEBUG ((EFI_D_VERBOSE, "Input parameters null\n")); |
| 623 | return EFI_INVALID_PARAMETER; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 624 | } |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 625 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 626 | if (PlatProp && (LenPlatId > 0) && (!(LenPlatId % MinPlatIdLen))) { |
| 627 | /*Compare msm-id of the dtb vs Board*/ |
| 628 | CurDtbInfo->DtPlatformId = |
| 629 | fdt32_to_cpu (((struct plat_id *)PlatProp)->platform_id); |
| 630 | DEBUG ((EFI_D_VERBOSE, "Boardsocid = %x, Dtsocid = %x\n", |
| 631 | (BoardPlatformRawChipId () & SOC_MASK), |
| 632 | (CurDtbInfo->DtPlatformId & SOC_MASK))); |
| 633 | if ((BoardPlatformRawChipId () & SOC_MASK) == |
| 634 | (CurDtbInfo->DtPlatformId & SOC_MASK)) { |
| 635 | CurDtbInfo->DtMatchVal |= BIT (SOC_MATCH); |
| 636 | } else { |
Mukesh Ojha | 0eedc35 | 2018-02-14 17:42:30 +0530 | [diff] [blame] | 637 | DEBUG ((EFI_D_VERBOSE, "qcom,msm-id does not match\n")); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 638 | /* If it's neither exact nor default match don't select dtb */ |
| 639 | CurDtbInfo->DtMatchVal = BIT (NONE_MATCH); |
Kamalpreet Singh | e3b8fe4 | 2017-12-07 14:03:36 -0800 | [diff] [blame] | 640 | return EFI_NOT_FOUND; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 641 | } |
| 642 | /*Compare soc rev of the dtb vs Board*/ |
| 643 | CurDtbInfo->DtSocRev = fdt32_to_cpu (((struct plat_id *)PlatProp)->soc_rev); |
| 644 | DEBUG ((EFI_D_VERBOSE, "BoardSocRev = %x, DtSocRev =%x\n", |
| 645 | BoardPlatformChipVersion (), CurDtbInfo->DtSocRev)); |
| 646 | if (CurDtbInfo->DtSocRev == BoardPlatformChipVersion ()) { |
| 647 | CurDtbInfo->DtMatchVal |= BIT (VERSION_EXACT_MATCH); |
| 648 | } else if (CurDtbInfo->DtSocRev < BoardPlatformChipVersion ()) { |
| 649 | CurDtbInfo->DtMatchVal |= BIT (VERSION_BEST_MATCH); |
| 650 | } else if (CurDtbInfo->DtSocRev) { |
Mukesh Ojha | 0eedc35 | 2018-02-14 17:42:30 +0530 | [diff] [blame] | 651 | DEBUG ((EFI_D_VERBOSE, "soc version does not match\n")); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 652 | } |
| 653 | /*Compare Foundry Id of the dtb vs Board*/ |
| 654 | CurDtbInfo->DtFoundryId = |
| 655 | fdt32_to_cpu (((struct plat_id *)PlatProp)->platform_id) & |
| 656 | FOUNDRY_ID_MASK; |
| 657 | DEBUG ((EFI_D_VERBOSE, "BoardFoundry = %x, DtFoundry = %x\n", |
| 658 | (BoardPlatformFoundryId () << PLATFORM_FOUNDRY_SHIFT), |
| 659 | CurDtbInfo->DtFoundryId)); |
| 660 | if (CurDtbInfo->DtFoundryId == |
| 661 | (BoardPlatformFoundryId () << PLATFORM_FOUNDRY_SHIFT)) { |
| 662 | CurDtbInfo->DtMatchVal |= BIT (FOUNDRYID_EXACT_MATCH); |
| 663 | } else if (CurDtbInfo->DtFoundryId == 0) { |
| 664 | CurDtbInfo->DtMatchVal |= BIT (FOUNDRYID_DEFAULT_MATCH); |
| 665 | } else { |
Mukesh Ojha | 0eedc35 | 2018-02-14 17:42:30 +0530 | [diff] [blame] | 666 | DEBUG ((EFI_D_VERBOSE, "soc foundry does not match\n")); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 667 | /* If it's neither exact nor default match don't select dtb */ |
| 668 | CurDtbInfo->DtMatchVal = BIT (NONE_MATCH); |
Kamalpreet Singh | e3b8fe4 | 2017-12-07 14:03:36 -0800 | [diff] [blame] | 669 | return EFI_NOT_FOUND; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 670 | } |
| 671 | } else { |
| 672 | DEBUG ((EFI_D_VERBOSE, "qcom, msm-id does not exist (or) is" |
| 673 | " (%d) not a multiple of (%d)\n", |
| 674 | LenPlatId, MinPlatIdLen)); |
| 675 | } |
Kamalpreet Singh | e3b8fe4 | 2017-12-07 14:03:36 -0800 | [diff] [blame] | 676 | return EFI_SUCCESS; |
| 677 | } |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 678 | |
Kamalpreet Singh | e3b8fe4 | 2017-12-07 14:03:36 -0800 | [diff] [blame] | 679 | STATIC EFI_STATUS GetBoardMatchDtb (DtInfo *CurDtbInfo, |
| 680 | CONST CHAR8 *BoardProp, |
| 681 | INT32 LenBoardId) |
| 682 | { |
lijuang | f6b1c5f | 2018-03-20 13:15:47 +0800 | [diff] [blame] | 683 | if (CurDtbInfo == NULL) { |
Kamalpreet Singh | e3b8fe4 | 2017-12-07 14:03:36 -0800 | [diff] [blame] | 684 | DEBUG ((EFI_D_VERBOSE, "Input parameters null\n")); |
| 685 | return EFI_INVALID_PARAMETER; |
| 686 | } |
| 687 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 688 | if (BoardProp && (LenBoardId > 0) && (!(LenBoardId % BOARD_ID_SIZE))) { |
| 689 | CurDtbInfo->DtVariantId = |
| 690 | fdt32_to_cpu (((struct board_id *)BoardProp)->variant_id); |
| 691 | CurDtbInfo->DtPlatformSubtype = |
| 692 | fdt32_to_cpu (((struct board_id *)BoardProp)->platform_subtype); |
| 693 | if (CurDtbInfo->DtPlatformSubtype == 0) { |
| 694 | CurDtbInfo->DtPlatformSubtype = |
| 695 | fdt32_to_cpu (((struct board_id *)BoardProp)->variant_id) >> |
| 696 | PLATFORM_SUBTYPE_SHIFT_ID; |
| 697 | } |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 698 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 699 | DEBUG ((EFI_D_VERBOSE, "BoardVariant = %x, DtVariant = %x\n", |
| 700 | BoardPlatformType (), CurDtbInfo->DtVariantId)); |
| 701 | CurDtbInfo->DtVariantMajor = CurDtbInfo->DtVariantId & VARIANT_MAJOR_MASK; |
| 702 | CurDtbInfo->DtVariantMinor = CurDtbInfo->DtVariantId & VARIANT_MINOR_MASK; |
| 703 | CurDtbInfo->DtVariantId = CurDtbInfo->DtVariantId & VARIANT_MASK; |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 704 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 705 | if (CurDtbInfo->DtVariantId == BoardPlatformType ()) { |
| 706 | CurDtbInfo->DtMatchVal |= BIT (VARIANT_MATCH); |
| 707 | } else if (CurDtbInfo->DtVariantId) { |
| 708 | DEBUG ((EFI_D_VERBOSE, "qcom,board-id does not" |
| 709 | " match\n")); |
| 710 | /* If it's neither exact nor default match don't select dtb */ |
| 711 | CurDtbInfo->DtMatchVal = BIT (NONE_MATCH); |
Kamalpreet Singh | e3b8fe4 | 2017-12-07 14:03:36 -0800 | [diff] [blame] | 712 | return EFI_NOT_FOUND; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 713 | } |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 714 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 715 | if (CurDtbInfo->DtVariantMajor == (BoardTargetId () & VARIANT_MAJOR_MASK)) { |
| 716 | CurDtbInfo->DtMatchVal |= BIT (VARIANT_MAJOR_EXACT_MATCH); |
| 717 | } else if (CurDtbInfo->DtVariantMajor < |
| 718 | (BoardTargetId () & VARIANT_MAJOR_MASK)) { |
| 719 | CurDtbInfo->DtMatchVal |= BIT (VARIANT_MAJOR_BEST_MATCH); |
| 720 | } else if (CurDtbInfo->DtVariantMajor) { |
| 721 | DEBUG ((EFI_D_VERBOSE, "qcom,board-id major version " |
| 722 | "does not match\n")); |
| 723 | } |
Parth Dixit | d488765 | 2017-09-13 22:28:52 +0530 | [diff] [blame] | 724 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 725 | if (CurDtbInfo->DtVariantMinor == (BoardTargetId () & VARIANT_MINOR_MASK)) { |
| 726 | CurDtbInfo->DtMatchVal |= BIT (VARIANT_MINOR_EXACT_MATCH); |
| 727 | } else if (CurDtbInfo->DtVariantMinor < |
| 728 | (BoardTargetId () & VARIANT_MINOR_MASK)) { |
| 729 | CurDtbInfo->DtMatchVal |= BIT (VARIANT_MINOR_BEST_MATCH); |
| 730 | } else if (CurDtbInfo->DtVariantMinor) { |
| 731 | DEBUG ((EFI_D_VERBOSE, "qcom,board-id minor version " |
| 732 | "does not match\n")); |
| 733 | } |
Parth Dixit | d488765 | 2017-09-13 22:28:52 +0530 | [diff] [blame] | 734 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 735 | DEBUG ((EFI_D_VERBOSE, "BoardSubtype = %x, DtSubType = %x\n", |
| 736 | BoardPlatformSubType (), CurDtbInfo->DtPlatformSubtype)); |
Chandra Sai Chidipudi | 64ced63 | 2020-01-22 12:25:28 +0530 | [diff] [blame] | 737 | if ((CurDtbInfo->DtPlatformSubtype & PLATFORM_SUBTYPE_MASK) == |
| 738 | BoardPlatformSubType ()) { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 739 | CurDtbInfo->DtMatchVal |= BIT (SUBTYPE_EXACT_MATCH); |
Chandra Sai Chidipudi | 64ced63 | 2020-01-22 12:25:28 +0530 | [diff] [blame] | 740 | } else if ((CurDtbInfo->DtPlatformSubtype & PLATFORM_SUBTYPE_MASK) == 0) { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 741 | CurDtbInfo->DtMatchVal |= BIT (SUBTYPE_DEFAULT_MATCH); |
jianzhou | 254e1e0 | 2017-11-01 14:52:02 +0800 | [diff] [blame] | 742 | } else { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 743 | DEBUG ((EFI_D_VERBOSE, "subtype-id doesnot match\n")); |
| 744 | /* If it's neither exact nor default match don't select dtb */ |
| 745 | CurDtbInfo->DtMatchVal = BIT (NONE_MATCH); |
Kamalpreet Singh | e3b8fe4 | 2017-12-07 14:03:36 -0800 | [diff] [blame] | 746 | return EFI_NOT_FOUND; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 747 | } |
Chandra Sai Chidipudi | 64ced63 | 2020-01-22 12:25:28 +0530 | [diff] [blame] | 748 | |
| 749 | if ((CurDtbInfo->DtPlatformSubtype & DDR_MASK) == |
| 750 | (BoardPlatformHlosSubType() & DDR_MASK)) { |
| 751 | CurDtbInfo->DtMatchVal |= BIT (DDR_MATCH); |
| 752 | } else { |
| 753 | DEBUG ((EFI_D_VERBOSE, "ddr size does not match\n")); |
| 754 | } |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 755 | } else { |
| 756 | DEBUG ((EFI_D_VERBOSE, "qcom,board-id does not exist (or) (%d) " |
| 757 | "is not a multiple of (%d)\n", |
| 758 | LenBoardId, BOARD_ID_SIZE)); |
| 759 | } |
Kamalpreet Singh | e3b8fe4 | 2017-12-07 14:03:36 -0800 | [diff] [blame] | 760 | return EFI_SUCCESS; |
| 761 | } |
| 762 | |
| 763 | /* Dt selection table for quick reference |
| 764 | | SNO | Dt Property | CDT Property | Exact | Best | Default | |
| 765 | |-----+---------------+-----------------+-------+------+---------+ |
| 766 | | | qcom, msm-id | | | | | |
| 767 | | | | PlatformId | Y | N | N | |
| 768 | | | | SocRev | N | Y | N | |
| 769 | | | | FoundryId | Y | N | 0 | |
| 770 | | | qcom,board-id | | | | | |
| 771 | | | | VariantId | Y | N | N | |
| 772 | | | | VariantMajor | N | Y | N | |
| 773 | | | | VariantMinor | N | Y | N | |
| 774 | | | | PlatformSubtype | Y | N | 0 | |
| 775 | | | qcom,pmic-id | | | | | |
| 776 | | | | PmicModelId | Y | N | 0 | |
| 777 | | | | PmicMetalRev | N | Y | N | |
| 778 | | | | PmicLayerRev | N | Y | N | |
| 779 | | | | PmicVariantRev | N | Y | N | |
| 780 | */ |
lijuang | 6fa55c0 | 2018-06-22 19:19:26 +0800 | [diff] [blame] | 781 | STATIC BOOLEAN |
Kamalpreet Singh | e3b8fe4 | 2017-12-07 14:03:36 -0800 | [diff] [blame] | 782 | ReadDtbFindMatch (DtInfo *CurDtbInfo, DtInfo *BestDtbInfo, UINT32 ExactMatch) |
| 783 | { |
| 784 | EFI_STATUS Status; |
| 785 | CONST CHAR8 *PlatProp = NULL; |
| 786 | CONST CHAR8 *BoardProp = NULL; |
| 787 | CONST CHAR8 *PmicProp = NULL; |
Mayank Grover | c8ad1e7 | 2020-12-02 15:14:00 +0530 | [diff] [blame] | 788 | CONST CHAR8 *PmicPropSz = NULL; |
Kamalpreet Singh | e3b8fe4 | 2017-12-07 14:03:36 -0800 | [diff] [blame] | 789 | INT32 LenBoardId; |
| 790 | INT32 LenPlatId; |
| 791 | INT32 LenPmicId; |
Mayank Grover | c8ad1e7 | 2020-12-02 15:14:00 +0530 | [diff] [blame] | 792 | INT32 LenPmicIdSz; |
| 793 | INT32 PmicMaxIdx; |
| 794 | INT32 PmicEntSz; |
Kamalpreet Singh | e3b8fe4 | 2017-12-07 14:03:36 -0800 | [diff] [blame] | 795 | INT32 MinPlatIdLen = PLAT_ID_SIZE; |
| 796 | INT32 RootOffset = 0; |
| 797 | VOID *Dtb = CurDtbInfo->Dtb; |
| 798 | UINT32 Idx; |
| 799 | UINT32 PmicEntCount; |
Bhanuprakash Modem | 6433330 | 2018-09-10 18:58:11 +0530 | [diff] [blame] | 800 | UINT32 MsmDataCount; |
Kamalpreet Singh | e3b8fe4 | 2017-12-07 14:03:36 -0800 | [diff] [blame] | 801 | PmicIdInfo BestPmicInfo; |
lijuang | 6fa55c0 | 2018-06-22 19:19:26 +0800 | [diff] [blame] | 802 | BOOLEAN FindBestMatch = FALSE; |
Bhanuprakash Modem | 6433330 | 2018-09-10 18:58:11 +0530 | [diff] [blame] | 803 | DtInfo TempDtbInfo = *CurDtbInfo; |
Kamalpreet Singh | e3b8fe4 | 2017-12-07 14:03:36 -0800 | [diff] [blame] | 804 | |
| 805 | memset (&BestPmicInfo, 0, sizeof (PmicIdInfo)); |
| 806 | /*Ensure MatchVal to 0 initially*/ |
| 807 | CurDtbInfo->DtMatchVal = 0; |
| 808 | RootOffset = fdt_path_offset (Dtb, "/"); |
| 809 | if (RootOffset < 0) { |
| 810 | DEBUG ((EFI_D_ERROR, "Unable to locate root node\n")); |
lijuang | 6fa55c0 | 2018-06-22 19:19:26 +0800 | [diff] [blame] | 811 | return FALSE; |
Kamalpreet Singh | e3b8fe4 | 2017-12-07 14:03:36 -0800 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | /* Get the msm-id prop from DTB */ |
| 815 | PlatProp = (CONST CHAR8 *)fdt_getprop (Dtb, RootOffset, "qcom,msm-id", |
| 816 | &LenPlatId); |
Bhanuprakash Modem | 6433330 | 2018-09-10 18:58:11 +0530 | [diff] [blame] | 817 | if (PlatProp && |
| 818 | (LenPlatId > 0) && |
| 819 | (!(LenPlatId % MinPlatIdLen))) { |
| 820 | /* |
| 821 | For Multiple soc-id's, save the best SocRev match DT in temp and search |
| 822 | for the exact match. If exact match is not found, use best match DT from |
| 823 | temp. |
| 824 | */ |
| 825 | MsmDataCount = (LenPlatId / MinPlatIdLen); |
| 826 | |
| 827 | /* Ensure to reset the match value */ |
| 828 | TempDtbInfo.DtMatchVal = NONE_MATCH; |
| 829 | |
| 830 | for (Idx = 0; Idx < MsmDataCount; Idx++) { |
| 831 | /* Ensure MatchVal should be 0 for every match */ |
| 832 | CurDtbInfo->DtMatchVal = NONE_MATCH; |
| 833 | Status = GetPlatformMatchDtb (CurDtbInfo, |
| 834 | PlatProp, |
| 835 | LenPlatId, |
| 836 | MinPlatIdLen); |
| 837 | if (Status == EFI_SUCCESS && |
| 838 | CurDtbInfo->DtMatchVal > TempDtbInfo.DtMatchVal) { |
| 839 | TempDtbInfo = *CurDtbInfo; |
| 840 | } |
| 841 | LenPlatId -= PLAT_ID_SIZE; |
| 842 | PlatProp += PLAT_ID_SIZE; |
| 843 | } |
| 844 | |
| 845 | *CurDtbInfo = TempDtbInfo; |
| 846 | |
| 847 | if (CurDtbInfo->DtMatchVal == NONE_MATCH) { |
| 848 | DEBUG ((EFI_D_VERBOSE, "Platform dt prop search failed.\n")); |
| 849 | goto cleanup; |
| 850 | } |
| 851 | } else { |
| 852 | DEBUG ((EFI_D_VERBOSE, "qcom, msm-id does not exist (or) is" |
| 853 | " (%d) not a multiple of (%d)\n", |
| 854 | LenPlatId, MinPlatIdLen)); |
Kamalpreet Singh | e3b8fe4 | 2017-12-07 14:03:36 -0800 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | /* Get the properties like variant id, subtype from Dtb then compare the |
| 858 | * dtb vs Board*/ |
| 859 | BoardProp = (CONST CHAR8 *)fdt_getprop (Dtb, RootOffset, "qcom,board-id", |
| 860 | &LenBoardId); |
| 861 | Status = GetBoardMatchDtb (CurDtbInfo, BoardProp, LenBoardId); |
| 862 | if (Status != EFI_SUCCESS) { |
Mukesh Ojha | 0eedc35 | 2018-02-14 17:42:30 +0530 | [diff] [blame] | 863 | DEBUG ((EFI_D_VERBOSE, "Board dt prop search failed.\n")); |
Kamalpreet Singh | e3b8fe4 | 2017-12-07 14:03:36 -0800 | [diff] [blame] | 864 | goto cleanup; |
| 865 | } |
Parth Dixit | d488765 | 2017-09-13 22:28:52 +0530 | [diff] [blame] | 866 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 867 | /*Get the pmic property from Dtb then compare the dtb vs Board*/ |
| 868 | PmicProp = |
| 869 | (CONST CHAR8 *)fdt_getprop (Dtb, RootOffset, "qcom,pmic-id", &LenPmicId); |
Mayank Grover | c8ad1e7 | 2020-12-02 15:14:00 +0530 | [diff] [blame] | 870 | |
| 871 | if ((PmicProp) && |
| 872 | (LenPmicId > 0)) { |
| 873 | PmicPropSz = |
| 874 | (CONST CHAR8 *)fdt_getprop (Dtb, RootOffset, "qcom,pmic-id-size", |
| 875 | &LenPmicIdSz); |
| 876 | if ((PmicPropSz) && |
| 877 | (LenPmicIdSz > 0)) { |
| 878 | PmicMaxIdx = (fdt32_to_cpu (*((UINT32 *)PmicPropSz))); |
| 879 | } else { |
| 880 | /* By default support four pmic, qcom,pmic-id = <a, b, c, d>*/ |
| 881 | PmicMaxIdx = PMIC_IDX4; |
| 882 | } |
| 883 | |
| 884 | PmicEntSz = PmicMaxIdx * sizeof (UINT32); |
| 885 | if (LenPmicId % PmicEntSz) { |
| 886 | DEBUG ((EFI_D_VERBOSE, |
| 887 | "LenPmicId(%d) is not multiple of PmicEntSz(%d)\n", |
| 888 | LenPmicId, PmicEntSz)); |
| 889 | goto cleanup; |
| 890 | } |
| 891 | |
| 892 | PmicEntCount = LenPmicId / PmicEntSz; |
Mukesh Ojha | 8ff5b4a | 2017-12-28 17:47:19 +0530 | [diff] [blame] | 893 | /* Get the best match pmic */ |
Mayank Grover | c8ad1e7 | 2020-12-02 15:14:00 +0530 | [diff] [blame] | 894 | ReadBestPmicMatch (PmicProp, PmicMaxIdx, PmicEntCount, &BestPmicInfo); |
Mukesh Ojha | fa81535 | 2017-11-08 14:43:41 +0530 | [diff] [blame] | 895 | CurDtbInfo->DtMatchVal |= BestPmicInfo.DtMatchVal; |
Mayank Grover | c8ad1e7 | 2020-12-02 15:14:00 +0530 | [diff] [blame] | 896 | for (Idx = 0; Idx < PmicMaxIdx; Idx++) { |
Mukesh Ojha | fa81535 | 2017-11-08 14:43:41 +0530 | [diff] [blame] | 897 | CurDtbInfo->DtPmicModel[Idx] = BestPmicInfo.DtPmicModel[Idx]; |
| 898 | CurDtbInfo->DtPmicRev[Idx] = BestPmicInfo.DtPmicRev[Idx]; |
| 899 | } |
| 900 | |
Lijuan Gao | 1259da7 | 2021-03-05 10:39:12 +0800 | [diff] [blame^] | 901 | DEBUG ((EFI_D_VERBOSE, "CurDtbInfo->DtMatchVal : 0x%llx " |
| 902 | "BestPmicInfo.DtMatchVal :0x%llx\n", CurDtbInfo->DtMatchVal, |
Mukesh Ojha | fa81535 | 2017-11-08 14:43:41 +0530 | [diff] [blame] | 903 | BestPmicInfo.DtMatchVal)); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 904 | } else { |
Mayank Grover | c8ad1e7 | 2020-12-02 15:14:00 +0530 | [diff] [blame] | 905 | DEBUG ((EFI_D_VERBOSE, "qcom,pmic-id does not exit\n")); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 906 | } |
Parth Dixit | d488765 | 2017-09-13 22:28:52 +0530 | [diff] [blame] | 907 | |
| 908 | cleanup: |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 909 | if (CurDtbInfo->DtMatchVal & BIT (ExactMatch)) { |
| 910 | if (BestDtbInfo->DtMatchVal < CurDtbInfo->DtMatchVal) { |
| 911 | gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo)); |
lijuang | 6fa55c0 | 2018-06-22 19:19:26 +0800 | [diff] [blame] | 912 | FindBestMatch = TRUE; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 913 | } else if (BestDtbInfo->DtMatchVal == CurDtbInfo->DtMatchVal) { |
lijuang | 6fa55c0 | 2018-06-22 19:19:26 +0800 | [diff] [blame] | 914 | FindBestMatch = TRUE; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 915 | if (BestDtbInfo->DtSocRev < CurDtbInfo->DtSocRev) { |
| 916 | gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo)); |
| 917 | } else if (BestDtbInfo->DtVariantMajor < CurDtbInfo->DtVariantMajor) { |
| 918 | gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo)); |
| 919 | } else if (BestDtbInfo->DtVariantMinor < CurDtbInfo->DtVariantMinor) { |
| 920 | gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo)); |
| 921 | } else if (BestDtbInfo->DtPmicRev[0] < CurDtbInfo->DtPmicRev[0]) { |
| 922 | gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo)); |
| 923 | } else if (BestDtbInfo->DtPmicRev[1] < CurDtbInfo->DtPmicRev[1]) { |
| 924 | gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo)); |
| 925 | } else if (BestDtbInfo->DtPmicRev[2] < CurDtbInfo->DtPmicRev[2]) { |
| 926 | gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo)); |
| 927 | } else if (BestDtbInfo->DtPmicRev[3] < CurDtbInfo->DtPmicRev[3]) { |
| 928 | gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo)); |
lijuang | 6fa55c0 | 2018-06-22 19:19:26 +0800 | [diff] [blame] | 929 | } else { |
| 930 | FindBestMatch = FALSE; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 931 | } |
| 932 | } |
| 933 | } |
lijuang | 6fa55c0 | 2018-06-22 19:19:26 +0800 | [diff] [blame] | 934 | |
| 935 | return FindBestMatch; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 936 | } |
Prakruthi Deepak Heragu | 9c385aa | 2019-03-20 10:54:29 -0700 | [diff] [blame] | 937 | /* |
| 938 | * For Header Version 2, the arguments Kernel and KernelSize will be |
| 939 | * the entire bootimage and the bootimage size. |
Raghavendra Rao Ananta | c3668a2 | 2020-01-13 18:20:02 -0800 | [diff] [blame] | 940 | * For Header Version 3, Kernel holds the base of the vendor_boot |
Raghavendra Rao Ananta | 9ba062d | 2019-10-12 11:26:33 -0700 | [diff] [blame] | 941 | * image and KernelSize holds its size. |
Prakruthi Deepak Heragu | 9c385aa | 2019-03-20 10:54:29 -0700 | [diff] [blame] | 942 | */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 943 | VOID * |
| 944 | GetSocDtb (VOID *Kernel, UINT32 KernelSize, UINT32 DtbOffset, VOID *DtbLoadAddr) |
| 945 | { |
| 946 | uintptr_t KernelEnd = (uintptr_t)Kernel + KernelSize; |
| 947 | VOID *Dtb = NULL; |
| 948 | struct fdt_header DtbHdr; |
| 949 | UINT32 DtbSize = 0; |
Prakruthi Deepak Heragu | 9c385aa | 2019-03-20 10:54:29 -0700 | [diff] [blame] | 950 | INT32 DtbCount = 0; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 951 | DtInfo CurDtbInfo = {0}; |
| 952 | DtInfo BestDtbInfo = {0}; |
| 953 | if (!DtbOffset) { |
| 954 | DEBUG ((EFI_D_ERROR, "DTB offset is NULL\n")); |
| 955 | return NULL; |
| 956 | } |
| 957 | |
| 958 | if (((uintptr_t)Kernel + (uintptr_t)DtbOffset) < (uintptr_t)Kernel) { |
| 959 | return NULL; |
| 960 | } |
| 961 | Dtb = Kernel + DtbOffset; |
| 962 | while (((uintptr_t)Dtb + sizeof (struct fdt_header)) < (uintptr_t)KernelEnd) { |
| 963 | /* the DTB could be unaligned, so extract the header, |
| 964 | * and operate on it separately */ |
| 965 | gBS->CopyMem (&DtbHdr, Dtb, sizeof (struct fdt_header)); |
| 966 | DtbSize = fdt_totalsize ((const VOID *)&DtbHdr); |
| 967 | if (fdt_check_header ((const VOID *)&DtbHdr) != 0 || |
| 968 | fdt_check_header_ext ((VOID *)&DtbHdr) != 0 || |
| 969 | ((uintptr_t)Dtb + DtbSize < (uintptr_t)Dtb) || |
| 970 | ((uintptr_t)Dtb + DtbSize > (uintptr_t)KernelEnd)) |
| 971 | break; |
| 972 | |
| 973 | CurDtbInfo.Dtb = Dtb; |
Prakruthi Deepak Heragu | 9c385aa | 2019-03-20 10:54:29 -0700 | [diff] [blame] | 974 | if (ReadDtbFindMatch (&CurDtbInfo, &BestDtbInfo, SOC_MATCH)) { |
| 975 | DtbIdx = DtbCount; |
| 976 | } |
| 977 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 978 | if (CurDtbInfo.DtMatchVal) { |
| 979 | if (CurDtbInfo.DtMatchVal & BIT (SOC_MATCH)) { |
| 980 | if (CheckAllBitsSet (CurDtbInfo.DtMatchVal)) { |
| 981 | DEBUG ((EFI_D_VERBOSE, "Exact DTB match" |
| 982 | " found. DTBO search is not " |
| 983 | "required\n")); |
| 984 | DtboNeed = FALSE; |
Parth Dixit | d488765 | 2017-09-13 22:28:52 +0530 | [diff] [blame] | 985 | } |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 986 | } |
| 987 | } else { |
| 988 | if (!GetRticDtb (Dtb)) { |
| 989 | DEBUG ((EFI_D_VERBOSE, "Error while DTB parsing" |
| 990 | " RTIC prop continue with next DTB\n")); |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | DEBUG ((EFI_D_VERBOSE, "Bestmatch = %x\n", BestDtbInfo.DtMatchVal)); |
| 995 | Dtb += DtbSize; |
Prakruthi Deepak Heragu | 9c385aa | 2019-03-20 10:54:29 -0700 | [diff] [blame] | 996 | DtbCount++; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | if (!BestDtbInfo.Dtb) { |
| 1000 | DEBUG ((EFI_D_ERROR, "No match found for Soc Dtb type\n")); |
| 1001 | return NULL; |
| 1002 | } |
| 1003 | |
| 1004 | return BestDtbInfo.Dtb; |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 1005 | } |
| 1006 | |
Mayank Grover | 98dcd67 | 2019-02-05 14:46:25 +0530 | [diff] [blame] | 1007 | /* |
| 1008 | Function to extract Dtb from user dtbo partition. |
| 1009 | */ |
| 1010 | EFI_STATUS |
| 1011 | GetOvrdDtb ( VOID **DtboImgBuffer) |
| 1012 | { |
| 1013 | struct DtboTableHdr *DtboTableHdr = NULL; |
| 1014 | struct DtboTableEntry *DtboTableEntry = NULL; |
| 1015 | VOID *OvrdDtb = NULL; |
| 1016 | UINT32 DtboTableEntriesCount = 0; |
| 1017 | UINT32 DtboTableEntryOffset = 0; |
| 1018 | EFI_STATUS Status = EFI_SUCCESS; |
| 1019 | UINT32 DtboImgSz = 0; |
| 1020 | CHAR16 PtnName[MAX_GPT_NAME_SIZE] = {0}; |
| 1021 | |
| 1022 | /** Get size of user_dtbo partition **/ |
| 1023 | UINT32 BlkIOAttrib = 0; |
| 1024 | PartiSelectFilter HandleFilter; |
| 1025 | UINT32 MaxHandles = 1; |
| 1026 | EFI_BLOCK_IO_PROTOCOL *BlockIo = NULL; |
| 1027 | HandleInfo HandleInfoList[1]; |
| 1028 | |
| 1029 | GUARD ( StrnCpyS (PtnName, |
| 1030 | MAX_GPT_NAME_SIZE, |
| 1031 | (CONST CHAR16 *)L"user_dtbo", |
| 1032 | (UINTN)StrLen (L"user_dtbo"))); |
| 1033 | |
| 1034 | BlkIOAttrib |= BLK_IO_SEL_PARTITIONED_MBR; |
| 1035 | BlkIOAttrib |= BLK_IO_SEL_PARTITIONED_GPT; |
| 1036 | BlkIOAttrib |= BLK_IO_SEL_MEDIA_TYPE_NON_REMOVABLE; |
| 1037 | BlkIOAttrib |= BLK_IO_SEL_MATCH_PARTITION_LABEL; |
| 1038 | |
| 1039 | HandleFilter.RootDeviceType = NULL; |
| 1040 | HandleFilter.PartitionLabel = NULL; |
| 1041 | HandleFilter.VolumeName = NULL; |
| 1042 | HandleFilter.PartitionLabel = PtnName; |
| 1043 | |
| 1044 | Status = |
| 1045 | GetBlkIOHandles (BlkIOAttrib, &HandleFilter, HandleInfoList, &MaxHandles); |
| 1046 | if (Status != EFI_SUCCESS || |
| 1047 | MaxHandles != 1) { |
| 1048 | DEBUG ((EFI_D_ERROR, |
| 1049 | "Override DTB: GetBlkIOHandles failed loading user_dtbo!\n")); |
| 1050 | Status = EFI_LOAD_ERROR; |
| 1051 | goto err; |
| 1052 | } |
| 1053 | |
| 1054 | BlockIo = HandleInfoList[0].BlkIo; |
Lijuan Gao | be74b9e | 2020-09-21 20:29:27 +0800 | [diff] [blame] | 1055 | DtboImgSz = GetPartitionSize (BlockIo); |
| 1056 | if (!DtboImgSz) { |
| 1057 | Status = EFI_BAD_BUFFER_SIZE; |
| 1058 | goto err; |
| 1059 | } |
Mayank Grover | 98dcd67 | 2019-02-05 14:46:25 +0530 | [diff] [blame] | 1060 | *DtboImgBuffer = AllocateZeroPool (DtboImgSz); |
| 1061 | if (*DtboImgBuffer == NULL) { |
| 1062 | DEBUG ((EFI_D_ERROR, "Override DTB: Buffer allocation failure\n")); |
| 1063 | Status = EFI_OUT_OF_RESOURCES; |
| 1064 | goto err; |
| 1065 | } |
| 1066 | |
| 1067 | /** Load user_dtbo image. **/ |
| 1068 | Status = LoadImageFromPartition (*DtboImgBuffer, &DtboImgSz, PtnName); |
| 1069 | if (Status != EFI_SUCCESS) { |
| 1070 | DEBUG ((EFI_D_ERROR, "Override DTB: DtboImgBuffer loading falied\n")); |
| 1071 | return Status; |
| 1072 | } |
| 1073 | |
| 1074 | DtboTableHdr = (struct DtboTableHdr *)*DtboImgBuffer; |
| 1075 | DtboTableEntryOffset = fdt32_to_cpu (DtboTableHdr->DtEntryOffset); |
| 1076 | if (CHECK_ADD64 ((UINT64)*DtboImgBuffer, DtboTableEntryOffset)) { |
| 1077 | DEBUG ((EFI_D_ERROR, |
| 1078 | "Override DTB: Integer overflow detected Dtbo address\n")); |
| 1079 | Status = EFI_INVALID_PARAMETER; |
| 1080 | goto err; |
| 1081 | } |
| 1082 | |
| 1083 | DtboTableEntry = |
| 1084 | (struct DtboTableEntry *)(*DtboImgBuffer + DtboTableEntryOffset); |
| 1085 | if (!DtboTableEntry) { |
| 1086 | DEBUG ((EFI_D_ERROR, "Override DTB: No proper DtTable\n")); |
| 1087 | Status = EFI_INVALID_PARAMETER; |
| 1088 | goto err; |
| 1089 | } |
| 1090 | |
| 1091 | // Support only one dtb in user dtbo image. |
| 1092 | DtboTableEntriesCount = fdt32_to_cpu (DtboTableHdr->DtEntryCount); |
| 1093 | if (DtboTableEntriesCount > 1) { |
| 1094 | DEBUG ((EFI_D_ERROR, |
| 1095 | "Override DTB: Exceeding maximum supported dtb count in Image\n")); |
| 1096 | Status = EFI_INVALID_PARAMETER; |
| 1097 | goto err; |
| 1098 | } |
| 1099 | |
| 1100 | OvrdDtb = *DtboImgBuffer + fdt32_to_cpu (DtboTableEntry->DtOffset); |
| 1101 | if (fdt_check_header (OvrdDtb) || |
| 1102 | fdt_check_header_ext (OvrdDtb)) { |
| 1103 | DEBUG ((EFI_D_ERROR, "Override DTB: No Valid DTB in image\n")); |
| 1104 | Status = EFI_INVALID_PARAMETER; |
| 1105 | goto err; |
| 1106 | } |
| 1107 | *DtboImgBuffer = OvrdDtb; |
| 1108 | return Status; |
| 1109 | |
| 1110 | err: |
| 1111 | if (*DtboImgBuffer) { |
| 1112 | FreePool (*DtboImgBuffer); |
| 1113 | } |
| 1114 | return Status; |
| 1115 | } |
| 1116 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1117 | VOID * |
| 1118 | GetBoardDtb (BootInfo *Info, VOID *DtboImgBuffer) |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 1119 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1120 | struct DtboTableHdr *DtboTableHdr = DtboImgBuffer; |
| 1121 | struct DtboTableEntry *DtboTableEntry = NULL; |
| 1122 | UINT32 DtboCount = 0; |
| 1123 | VOID *BoardDtb = NULL; |
| 1124 | UINT32 DtboTableEntriesCount = 0; |
| 1125 | UINT32 FirstDtboTableEntryOffset = 0; |
| 1126 | DtInfo CurDtbInfo = {0}; |
| 1127 | DtInfo BestDtbInfo = {0}; |
lijuang | 6fa55c0 | 2018-06-22 19:19:26 +0800 | [diff] [blame] | 1128 | BOOLEAN FindBestDtb = FALSE; |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 1129 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1130 | if (!DtboImgBuffer) { |
| 1131 | DEBUG ((EFI_D_ERROR, "Dtbo Img buffer is NULL\n")); |
| 1132 | return NULL; |
| 1133 | } |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 1134 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1135 | FirstDtboTableEntryOffset = fdt32_to_cpu (DtboTableHdr->DtEntryOffset); |
| 1136 | if (CHECK_ADD64 ((UINT64)DtboImgBuffer, FirstDtboTableEntryOffset)) { |
| 1137 | DEBUG ((EFI_D_ERROR, "Integer overflow deteced with Dtbo address\n")); |
| 1138 | return NULL; |
| 1139 | } |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 1140 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1141 | DtboTableEntry = |
| 1142 | (struct DtboTableEntry *)(DtboImgBuffer + FirstDtboTableEntryOffset); |
| 1143 | if (!DtboTableEntry) { |
| 1144 | DEBUG ((EFI_D_ERROR, "No proper DtTable\n")); |
| 1145 | return NULL; |
| 1146 | } |
Mukesh Ojha | a1bd1f8 | 2017-05-25 19:04:21 +0530 | [diff] [blame] | 1147 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1148 | DtboTableEntriesCount = fdt32_to_cpu (DtboTableHdr->DtEntryCount); |
| 1149 | for (DtboCount = 0; DtboCount < DtboTableEntriesCount; DtboCount++) { |
| 1150 | if (CHECK_ADD64 ((UINT64)DtboImgBuffer, |
| 1151 | fdt32_to_cpu (DtboTableEntry->DtOffset))) { |
Mukesh Ojha | 82911f8 | 2018-04-30 16:45:07 +0530 | [diff] [blame] | 1152 | DEBUG ((EFI_D_ERROR, "Integer overflow detected with Dtbo address\n")); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1153 | return NULL; |
| 1154 | } |
| 1155 | BoardDtb = DtboImgBuffer + fdt32_to_cpu (DtboTableEntry->DtOffset); |
| 1156 | if (fdt_check_header (BoardDtb) || fdt_check_header_ext (BoardDtb)) { |
| 1157 | DEBUG ((EFI_D_ERROR, "No Valid Dtb\n")); |
| 1158 | break; |
| 1159 | } |
Mukesh Ojha | a1bd1f8 | 2017-05-25 19:04:21 +0530 | [diff] [blame] | 1160 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1161 | CurDtbInfo.Dtb = BoardDtb; |
lijuang | 6fa55c0 | 2018-06-22 19:19:26 +0800 | [diff] [blame] | 1162 | FindBestDtb = ReadDtbFindMatch (&CurDtbInfo, &BestDtbInfo, VARIANT_MATCH); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1163 | DEBUG ((EFI_D_VERBOSE, "Dtbo count = %u LocalBoardDtMatch = %x" |
| 1164 | "\n", |
| 1165 | DtboCount, CurDtbInfo.DtMatchVal)); |
lijuang | 6fa55c0 | 2018-06-22 19:19:26 +0800 | [diff] [blame] | 1166 | |
| 1167 | if (FindBestDtb) { |
| 1168 | DtboIdx = DtboCount; |
| 1169 | } |
| 1170 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1171 | DtboTableEntry++; |
| 1172 | } |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 1173 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1174 | if (!BestDtbInfo.Dtb) { |
| 1175 | DEBUG ((EFI_D_ERROR, "Unable to find the Board Dtb\n")); |
| 1176 | return NULL; |
| 1177 | } |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 1178 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1179 | return BestDtbInfo.Dtb; |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 1180 | } |
| 1181 | |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 1182 | /* Returns 0 if the device tree is valid. */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1183 | int |
| 1184 | DeviceTreeValidate (UINT8 *DeviceTreeBuff, |
| 1185 | UINT32 PageSize, |
| 1186 | UINT32 *DeviceTreeSize) |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 1187 | { |
lijuang | c336a29 | 2017-08-22 19:48:49 +0800 | [diff] [blame] | 1188 | UINT32 dt_entry_size; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1189 | UINT64 hdr_size; |
| 1190 | struct dt_table *table; |
| 1191 | if (DeviceTreeSize) { |
| 1192 | table = (struct dt_table *)DeviceTreeBuff; |
| 1193 | if (table->magic != DEV_TREE_MAGIC) { |
| 1194 | // bad magic in device tree table |
| 1195 | return -1; |
| 1196 | } |
| 1197 | if (table->version == DEV_TREE_VERSION_V1) { |
| 1198 | dt_entry_size = sizeof (struct dt_entry_v1); |
| 1199 | } else if (table->version == DEV_TREE_VERSION_V2) { |
| 1200 | dt_entry_size = sizeof (struct dt_entry_v2); |
| 1201 | } else if (table->version == DEV_TREE_VERSION_V3) { |
| 1202 | dt_entry_size = sizeof (struct dt_entry); |
| 1203 | } else { |
| 1204 | // unsupported dt version |
| 1205 | return -1; |
| 1206 | } |
| 1207 | hdr_size = |
| 1208 | ((UINT64)table->num_entries * dt_entry_size) + DEV_TREE_HEADER_SIZE; |
| 1209 | // hdr_size = ROUNDUP(hdr_size, PageSize); |
| 1210 | hdr_size = EFI_SIZE_TO_PAGES (hdr_size); |
| 1211 | if (hdr_size > MAX_UINT64) |
| 1212 | return -1; |
| 1213 | else |
| 1214 | *DeviceTreeSize = hdr_size & MAX_UINT64; |
| 1215 | // dt_entry_ptr = (struct dt_entry *)((CHAR8 *)table + |
| 1216 | // DEV_TREE_HEADER_SIZE); |
| 1217 | // table_ptr = dt_entry_ptr; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 1218 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1219 | DEBUG ((EFI_D_ERROR, "DT Total number of entries: %d, DTB version: %d\n", |
| 1220 | table->num_entries, table->version)); |
| 1221 | } |
| 1222 | return 0; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 1223 | } |
| 1224 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1225 | STATIC int |
| 1226 | platform_dt_absolute_match (struct dt_entry *cur_dt_entry, |
| 1227 | struct dt_entry_node *dt_list) |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 1228 | { |
Chandra Sai Chidipudi | 64ced63 | 2020-01-22 12:25:28 +0530 | [diff] [blame] | 1229 | UINT32 cur_dt_hlos_ddr; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1230 | UINT32 cur_dt_hw_platform; |
| 1231 | UINT32 cur_dt_hw_subtype; |
| 1232 | UINT32 cur_dt_msm_id; |
| 1233 | dt_node *dt_node_tmp = NULL; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 1234 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1235 | /* Platform-id |
| 1236 | * bit no |31 24|23 16|15 0| |
| 1237 | * |reserved|foundry-id|msm-id| |
| 1238 | */ |
| 1239 | cur_dt_msm_id = (cur_dt_entry->platform_id & 0x0000ffff); |
| 1240 | cur_dt_hw_platform = (cur_dt_entry->variant_id & 0x000000ff); |
| 1241 | cur_dt_hw_subtype = (cur_dt_entry->board_hw_subtype & 0xff); |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 1242 | |
Chandra Sai Chidipudi | 64ced63 | 2020-01-22 12:25:28 +0530 | [diff] [blame] | 1243 | /* Bits 10:8 contain ddr information */ |
| 1244 | cur_dt_hlos_ddr = (cur_dt_entry->board_hw_subtype & 0x700); |
| 1245 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1246 | /* 1. must match the msm_id, platform_hw_id, platform_subtype and DDR size |
| 1247 | * soc, board major/minor, pmic major/minor must less than board info |
| 1248 | * 2. find the matched DTB then return 1 |
| 1249 | * 3. otherwise return 0 |
| 1250 | */ |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 1251 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1252 | if ((cur_dt_msm_id == (BoardPlatformRawChipId () & 0x0000ffff)) && |
| 1253 | (cur_dt_hw_platform == BoardPlatformType ()) && |
| 1254 | (cur_dt_hw_subtype == BoardPlatformSubType ()) && |
Chandra Sai Chidipudi | 64ced63 | 2020-01-22 12:25:28 +0530 | [diff] [blame] | 1255 | (cur_dt_hlos_ddr == (BoardPlatformHlosSubType() & 0x700)) && |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1256 | (cur_dt_entry->soc_rev <= BoardPlatformChipVersion ()) && |
| 1257 | ((cur_dt_entry->variant_id & 0x00ffff00) <= |
| 1258 | (BoardTargetId () & 0x00ffff00)) && |
| 1259 | (cur_dt_entry->pmic_rev[0] <= BoardPmicTarget (0)) && |
| 1260 | (cur_dt_entry->pmic_rev[1] <= BoardPmicTarget (1)) && |
| 1261 | (cur_dt_entry->pmic_rev[2] <= BoardPmicTarget (2)) && |
| 1262 | (cur_dt_entry->pmic_rev[3] <= BoardPmicTarget (3))) { |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 1263 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1264 | dt_node_tmp = dt_entry_list_init (); |
| 1265 | if (!dt_node_tmp) { |
| 1266 | DEBUG ((EFI_D_ERROR, "dt_node_tmp is NULL\n")); |
| 1267 | return 0; |
| 1268 | } |
Vijay Kumar Pendoti | 1e847bd | 2016-11-21 15:29:13 +0530 | [diff] [blame] | 1269 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1270 | gBS->CopyMem ((VOID *)dt_node_tmp->dt_entry_m, (VOID *)cur_dt_entry, |
| 1271 | sizeof (struct dt_entry)); |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 1272 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1273 | DEBUG ( |
| 1274 | (EFI_D_VERBOSE, |
| 1275 | "Add DTB entry 0x%x/%08x/0x%08x/0x%x/0x%x/0x%x/0x%x/0x%x/0x%x/0x%x\n", |
| 1276 | dt_node_tmp->dt_entry_m->platform_id, |
| 1277 | dt_node_tmp->dt_entry_m->variant_id, |
| 1278 | dt_node_tmp->dt_entry_m->board_hw_subtype, |
| 1279 | dt_node_tmp->dt_entry_m->soc_rev, dt_node_tmp->dt_entry_m->pmic_rev[0], |
| 1280 | dt_node_tmp->dt_entry_m->pmic_rev[1], |
| 1281 | dt_node_tmp->dt_entry_m->pmic_rev[2], |
| 1282 | dt_node_tmp->dt_entry_m->pmic_rev[3], dt_node_tmp->dt_entry_m->offset, |
| 1283 | dt_node_tmp->dt_entry_m->size)); |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 1284 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1285 | insert_dt_entry_in_queue (dt_list, dt_node_tmp); |
| 1286 | return 1; |
| 1287 | } |
| 1288 | return 0; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 1289 | } |
| 1290 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1291 | int |
| 1292 | platform_dt_absolute_compat_match (struct dt_entry_node *dt_list, |
| 1293 | UINT32 dtb_info) |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 1294 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1295 | struct dt_entry_node *dt_node_tmp1 = NULL; |
| 1296 | struct dt_entry_node *dt_node_tmp2 = NULL; |
| 1297 | UINT32 current_info = 0; |
| 1298 | UINT32 board_info = 0; |
| 1299 | UINT32 best_info = 0; |
| 1300 | UINT32 current_pmic_model[4] = {0, 0, 0, 0}; |
| 1301 | UINT32 board_pmic_model[4] = {0, 0, 0, 0}; |
| 1302 | UINT32 best_pmic_model[4] = {0, 0, 0, 0}; |
| 1303 | UINT32 delete_current_dt = 0; |
| 1304 | UINT32 i; |
| 1305 | |
| 1306 | /* start to select the exact entry |
| 1307 | * default to exact match 0, if find current DTB entry info is the same as |
| 1308 | * board info, |
| 1309 | * then exact match board info. |
| 1310 | */ |
| 1311 | list_for_every_entry (&dt_list->node, dt_node_tmp1, dt_node, node) |
| 1312 | { |
| 1313 | if (!dt_node_tmp1) { |
| 1314 | DEBUG ((EFI_D_ERROR, "Current node is the end\n")); |
| 1315 | break; |
| 1316 | } |
| 1317 | switch (dtb_info) { |
| 1318 | case DTB_FOUNDRY: |
| 1319 | current_info = ((dt_node_tmp1->dt_entry_m->platform_id) & 0x00ff0000); |
| 1320 | board_info = BoardPlatformFoundryId () << 16; |
| 1321 | break; |
Chandra Sai Chidipudi | 3b1450c | 2020-02-07 15:06:15 +0530 | [diff] [blame] | 1322 | case DTB_DDR: |
| 1323 | current_info = ((dt_node_tmp1->dt_entry_m->board_hw_subtype) & 0x700); |
| 1324 | board_info = (BoardPlatformHlosSubType () & 0x700); |
| 1325 | break; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1326 | case DTB_PMIC_MODEL: |
| 1327 | for (i = 0; i < 4; i++) { |
| 1328 | current_pmic_model[i] = (dt_node_tmp1->dt_entry_m->pmic_rev[i] & 0xff); |
| 1329 | board_pmic_model[i] = BoardPmicModel (i); |
| 1330 | } |
| 1331 | break; |
| 1332 | default: |
| 1333 | DEBUG ((EFI_D_ERROR, |
| 1334 | "ERROR: Unsupported version (%d) in dt node check \n", dtb_info)); |
| 1335 | return 0; |
| 1336 | } |
| 1337 | |
| 1338 | if (dtb_info == DTB_PMIC_MODEL) { |
| 1339 | if ((current_pmic_model[0] == board_pmic_model[0]) && |
| 1340 | (current_pmic_model[1] == board_pmic_model[1]) && |
| 1341 | (current_pmic_model[2] == board_pmic_model[2]) && |
| 1342 | (current_pmic_model[3] == board_pmic_model[3])) { |
| 1343 | |
| 1344 | for (i = 0; i < 4; i++) { |
| 1345 | best_pmic_model[i] = current_pmic_model[i]; |
| 1346 | } |
| 1347 | break; |
| 1348 | } |
| 1349 | } else { |
| 1350 | if (current_info == board_info) { |
| 1351 | best_info = current_info; |
| 1352 | break; |
| 1353 | } |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | list_for_every_entry (&dt_list->node, dt_node_tmp1, dt_node, node) |
| 1358 | { |
| 1359 | if (!dt_node_tmp1) { |
| 1360 | DEBUG ((EFI_D_ERROR, "Current node is the end\n")); |
| 1361 | break; |
| 1362 | } |
| 1363 | switch (dtb_info) { |
| 1364 | case DTB_FOUNDRY: |
| 1365 | current_info = ((dt_node_tmp1->dt_entry_m->platform_id) & 0x00ff0000); |
| 1366 | break; |
Chandra Sai Chidipudi | 3b1450c | 2020-02-07 15:06:15 +0530 | [diff] [blame] | 1367 | case DTB_DDR: |
| 1368 | current_info = ((dt_node_tmp1->dt_entry_m->board_hw_subtype) & 0x700); |
| 1369 | break; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1370 | case DTB_PMIC_MODEL: |
| 1371 | for (i = 0; i < 4; i++) { |
| 1372 | current_pmic_model[i] = (dt_node_tmp1->dt_entry_m->pmic_rev[i] & 0xff); |
| 1373 | } |
| 1374 | break; |
| 1375 | default: |
| 1376 | DEBUG ((EFI_D_ERROR, |
| 1377 | "ERROR: Unsupported version (%d) in dt node check \n", dtb_info)); |
| 1378 | return 0; |
| 1379 | } |
| 1380 | |
| 1381 | if (dtb_info == DTB_PMIC_MODEL) { |
| 1382 | if ((current_pmic_model[0] != best_pmic_model[0]) || |
| 1383 | (current_pmic_model[1] != best_pmic_model[1]) || |
| 1384 | (current_pmic_model[2] != best_pmic_model[2]) || |
| 1385 | (current_pmic_model[3] != best_pmic_model[3])) { |
| 1386 | |
| 1387 | delete_current_dt = 1; |
| 1388 | } |
| 1389 | } else { |
| 1390 | if (current_info != best_info) { |
| 1391 | delete_current_dt = 1; |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | if (delete_current_dt) { |
| 1396 | DEBUG ( |
| 1397 | (EFI_D_VERBOSE, |
| 1398 | "Delete don't fit DTB entry %u/%08x/0x%08x/%x/%x/%x/%x/%x/%x/%x\n", |
| 1399 | dt_node_tmp1->dt_entry_m->platform_id, |
| 1400 | dt_node_tmp1->dt_entry_m->variant_id, |
| 1401 | dt_node_tmp1->dt_entry_m->board_hw_subtype, |
| 1402 | dt_node_tmp1->dt_entry_m->soc_rev, |
| 1403 | dt_node_tmp1->dt_entry_m->pmic_rev[0], |
| 1404 | dt_node_tmp1->dt_entry_m->pmic_rev[1], |
| 1405 | dt_node_tmp1->dt_entry_m->pmic_rev[2], |
| 1406 | dt_node_tmp1->dt_entry_m->pmic_rev[3], |
| 1407 | dt_node_tmp1->dt_entry_m->offset, dt_node_tmp1->dt_entry_m->size)); |
| 1408 | |
| 1409 | dt_node_tmp2 = (struct dt_entry_node *)dt_node_tmp1->node.prev; |
| 1410 | dt_entry_list_delete (dt_node_tmp1); |
| 1411 | dt_node_tmp1 = dt_node_tmp2; |
| 1412 | delete_current_dt = 0; |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | return 1; |
| 1417 | } |
| 1418 | |
| 1419 | int |
| 1420 | update_dtb_entry_node (struct dt_entry_node *dt_list, UINT32 dtb_info) |
| 1421 | { |
| 1422 | struct dt_entry_node *dt_node_tmp1 = NULL; |
| 1423 | struct dt_entry_node *dt_node_tmp2 = NULL; |
| 1424 | UINT32 current_info = 0; |
| 1425 | UINT32 board_info = 0; |
| 1426 | UINT32 best_info = 0; |
| 1427 | |
| 1428 | /* start to select the best entry*/ |
| 1429 | list_for_every_entry (&dt_list->node, dt_node_tmp1, dt_node, node) |
| 1430 | { |
| 1431 | if (!dt_node_tmp1) { |
| 1432 | DEBUG ((EFI_D_ERROR, "Current node is the end\n")); |
| 1433 | break; |
| 1434 | } |
| 1435 | switch (dtb_info) { |
| 1436 | case DTB_SOC: |
| 1437 | current_info = dt_node_tmp1->dt_entry_m->soc_rev; |
| 1438 | board_info = BoardPlatformChipVersion (); |
| 1439 | break; |
| 1440 | case DTB_MAJOR_MINOR: |
| 1441 | current_info = ((dt_node_tmp1->dt_entry_m->variant_id) & 0x00ffff00); |
| 1442 | board_info = BoardTargetId () & 0x00ffff00; |
| 1443 | break; |
| 1444 | case DTB_PMIC0: |
| 1445 | current_info = dt_node_tmp1->dt_entry_m->pmic_rev[0]; |
| 1446 | board_info = BoardPmicTarget (0); |
| 1447 | break; |
| 1448 | case DTB_PMIC1: |
| 1449 | current_info = dt_node_tmp1->dt_entry_m->pmic_rev[1]; |
| 1450 | board_info = BoardPmicTarget (1); |
| 1451 | break; |
| 1452 | case DTB_PMIC2: |
| 1453 | current_info = dt_node_tmp1->dt_entry_m->pmic_rev[2]; |
| 1454 | board_info = BoardPmicTarget (2); |
| 1455 | break; |
| 1456 | case DTB_PMIC3: |
| 1457 | current_info = dt_node_tmp1->dt_entry_m->pmic_rev[3]; |
| 1458 | board_info = BoardPmicTarget (3); |
| 1459 | break; |
| 1460 | default: |
| 1461 | DEBUG ((EFI_D_ERROR, |
| 1462 | "ERROR: Unsupported version (%d) in dt node check \n", dtb_info)); |
| 1463 | return 0; |
| 1464 | } |
| 1465 | |
| 1466 | if (current_info == board_info) { |
| 1467 | best_info = current_info; |
| 1468 | break; |
| 1469 | } |
| 1470 | if ((current_info < board_info) && (current_info > best_info)) { |
| 1471 | best_info = current_info; |
| 1472 | } |
| 1473 | if (current_info < best_info) { |
| 1474 | DEBUG ( |
| 1475 | (EFI_D_ERROR, |
| 1476 | "Delete don't fit DTB entry %u/%08x/0x%08x/%x/%x/%x/%x/%x/%x/%x\n", |
| 1477 | dt_node_tmp1->dt_entry_m->platform_id, |
| 1478 | dt_node_tmp1->dt_entry_m->variant_id, |
| 1479 | dt_node_tmp1->dt_entry_m->board_hw_subtype, |
| 1480 | dt_node_tmp1->dt_entry_m->soc_rev, |
| 1481 | dt_node_tmp1->dt_entry_m->pmic_rev[0], |
| 1482 | dt_node_tmp1->dt_entry_m->pmic_rev[1], |
| 1483 | dt_node_tmp1->dt_entry_m->pmic_rev[2], |
| 1484 | dt_node_tmp1->dt_entry_m->pmic_rev[3], |
| 1485 | dt_node_tmp1->dt_entry_m->offset, dt_node_tmp1->dt_entry_m->size)); |
| 1486 | |
| 1487 | dt_node_tmp2 = (struct dt_entry_node *)dt_node_tmp1->node.prev; |
| 1488 | dt_entry_list_delete (dt_node_tmp1); |
| 1489 | dt_node_tmp1 = dt_node_tmp2; |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | list_for_every_entry (&dt_list->node, dt_node_tmp1, dt_node, node) |
| 1494 | { |
| 1495 | if (!dt_node_tmp1) { |
| 1496 | DEBUG ((EFI_D_ERROR, "Current node is the end\n")); |
| 1497 | break; |
| 1498 | } |
| 1499 | switch (dtb_info) { |
| 1500 | case DTB_SOC: |
| 1501 | current_info = dt_node_tmp1->dt_entry_m->soc_rev; |
| 1502 | break; |
| 1503 | case DTB_MAJOR_MINOR: |
| 1504 | current_info = ((dt_node_tmp1->dt_entry_m->variant_id) & 0x00ffff00); |
| 1505 | break; |
| 1506 | case DTB_PMIC0: |
| 1507 | current_info = dt_node_tmp1->dt_entry_m->pmic_rev[0]; |
| 1508 | break; |
| 1509 | case DTB_PMIC1: |
| 1510 | current_info = dt_node_tmp1->dt_entry_m->pmic_rev[1]; |
| 1511 | break; |
| 1512 | case DTB_PMIC2: |
| 1513 | current_info = dt_node_tmp1->dt_entry_m->pmic_rev[2]; |
| 1514 | break; |
| 1515 | case DTB_PMIC3: |
| 1516 | current_info = dt_node_tmp1->dt_entry_m->pmic_rev[3]; |
| 1517 | break; |
| 1518 | default: |
| 1519 | DEBUG ((EFI_D_ERROR, |
| 1520 | "ERROR: Unsupported version (%d) in dt node check \n", dtb_info)); |
| 1521 | return 0; |
| 1522 | } |
| 1523 | |
| 1524 | if (current_info != best_info) { |
| 1525 | DEBUG ( |
| 1526 | (EFI_D_VERBOSE, |
| 1527 | "Delete don't fit DTB entry %u/%08x/0x%08x/%x/%x/%x/%x/%x/%x/%x\n", |
| 1528 | dt_node_tmp1->dt_entry_m->platform_id, |
| 1529 | dt_node_tmp1->dt_entry_m->variant_id, |
| 1530 | dt_node_tmp1->dt_entry_m->board_hw_subtype, |
| 1531 | dt_node_tmp1->dt_entry_m->soc_rev, |
| 1532 | dt_node_tmp1->dt_entry_m->pmic_rev[0], |
| 1533 | dt_node_tmp1->dt_entry_m->pmic_rev[1], |
| 1534 | dt_node_tmp1->dt_entry_m->pmic_rev[2], |
| 1535 | dt_node_tmp1->dt_entry_m->pmic_rev[3], |
| 1536 | dt_node_tmp1->dt_entry_m->offset, dt_node_tmp1->dt_entry_m->size)); |
| 1537 | |
| 1538 | dt_node_tmp2 = (struct dt_entry_node *)dt_node_tmp1->node.prev; |
| 1539 | dt_entry_list_delete (dt_node_tmp1); |
| 1540 | dt_node_tmp1 = dt_node_tmp2; |
| 1541 | } |
| 1542 | } |
| 1543 | return 1; |
| 1544 | } |
| 1545 | |
| 1546 | STATIC struct dt_entry * |
| 1547 | platform_dt_match_best (struct dt_entry_node *dt_list) |
| 1548 | { |
| 1549 | struct dt_entry_node *dt_node_tmp1 = NULL; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 1550 | |
lijuang | c336a29 | 2017-08-22 19:48:49 +0800 | [diff] [blame] | 1551 | /* check Foundry id |
| 1552 | * the foundry id must exact match board founddry id, this is compatibility |
| 1553 | * check, if couldn't find the exact match from DTB, will exact match 0x0. |
| 1554 | */ |
| 1555 | platform_dt_absolute_compat_match (dt_list, DTB_FOUNDRY); |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 1556 | |
Chandra Sai Chidipudi | 3b1450c | 2020-02-07 15:06:15 +0530 | [diff] [blame] | 1557 | /* check DDR type |
| 1558 | * the DDR type must exact match board DDR tpe, this is compatibility |
| 1559 | * check, if couldn't find the exact match from DTB, will exact match 0x0. |
| 1560 | */ |
| 1561 | platform_dt_absolute_compat_match (dt_list, DTB_DDR); |
| 1562 | |
lijuang | c336a29 | 2017-08-22 19:48:49 +0800 | [diff] [blame] | 1563 | /* check PMIC model |
| 1564 | * the PMIC model must exact match board PMIC model, this is compatibility |
| 1565 | * check, if couldn't find the exact match from DTB, will exact match 0x0. |
| 1566 | */ |
| 1567 | platform_dt_absolute_compat_match (dt_list, DTB_PMIC_MODEL); |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 1568 | |
lijuang | c336a29 | 2017-08-22 19:48:49 +0800 | [diff] [blame] | 1569 | /* check soc version |
| 1570 | * the suitable soc version must less than or equal to board soc version |
| 1571 | */ |
| 1572 | update_dtb_entry_node (dt_list, DTB_SOC); |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 1573 | |
lijuang | c336a29 | 2017-08-22 19:48:49 +0800 | [diff] [blame] | 1574 | /*check major and minor version |
| 1575 | * the suitable major&minor version must less than or equal to board |
| 1576 | * major&minor version |
| 1577 | */ |
| 1578 | update_dtb_entry_node (dt_list, DTB_MAJOR_MINOR); |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 1579 | |
lijuang | c336a29 | 2017-08-22 19:48:49 +0800 | [diff] [blame] | 1580 | /*check pmic info |
| 1581 | * the suitable pmic major&minor info must less than or equal to board pmic |
| 1582 | * major&minor version |
| 1583 | */ |
| 1584 | update_dtb_entry_node (dt_list, DTB_PMIC0); |
| 1585 | update_dtb_entry_node (dt_list, DTB_PMIC1); |
| 1586 | update_dtb_entry_node (dt_list, DTB_PMIC2); |
| 1587 | update_dtb_entry_node (dt_list, DTB_PMIC3); |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 1588 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1589 | list_for_every_entry (&dt_list->node, dt_node_tmp1, dt_node, node) |
| 1590 | { |
| 1591 | if (!dt_node_tmp1) { |
| 1592 | DEBUG ((EFI_D_ERROR, "ERROR: Couldn't find the suitable DTB!\n")); |
| 1593 | return NULL; |
| 1594 | } |
| 1595 | if (dt_node_tmp1->dt_entry_m) |
| 1596 | return dt_node_tmp1->dt_entry_m; |
| 1597 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 1598 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1599 | return NULL; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 1600 | } |
Bhanuprakash Modem | ed797c7 | 2018-09-05 20:39:07 +0530 | [diff] [blame] | 1601 | |
| 1602 | BOOLEAN |
| 1603 | AppendToDtList (struct fdt_entry_node **DtList, |
| 1604 | UINT64 Address, |
| 1605 | UINT64 Size) { |
| 1606 | struct fdt_entry_node *Current = *DtList; |
| 1607 | |
| 1608 | if (*DtList == NULL) { |
| 1609 | DEBUG ((EFI_D_VERBOSE, "DTs list: NULL\n")); |
Bhanuprakash Modem | 763cdd5 | 2018-11-01 14:49:55 +0530 | [diff] [blame] | 1610 | Current = AllocateZeroPool (sizeof (struct fdt_entry_node)); |
Bhanuprakash Modem | ed797c7 | 2018-09-05 20:39:07 +0530 | [diff] [blame] | 1611 | if (!Current) { |
| 1612 | return FALSE; |
| 1613 | } |
| 1614 | |
| 1615 | Current->address = Address; |
| 1616 | Current->size = Size; |
| 1617 | Current->next = NULL; |
| 1618 | *DtList = Current; |
| 1619 | return TRUE; |
| 1620 | } else { |
| 1621 | |
| 1622 | while (Current->next != NULL) { |
| 1623 | Current = Current->next; |
| 1624 | } |
| 1625 | |
Bhanuprakash Modem | 763cdd5 | 2018-11-01 14:49:55 +0530 | [diff] [blame] | 1626 | Current->next = AllocateZeroPool (sizeof (struct fdt_entry_node)); |
Bhanuprakash Modem | ed797c7 | 2018-09-05 20:39:07 +0530 | [diff] [blame] | 1627 | if (!Current->next) { |
| 1628 | return FALSE; |
| 1629 | } |
| 1630 | Current->next->address = Address; |
| 1631 | Current->next->size = Size; |
| 1632 | Current->next->next = NULL; |
| 1633 | return TRUE; |
| 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | VOID DeleteDtList (struct fdt_entry_node** DtList) |
| 1638 | { |
| 1639 | struct fdt_entry_node *Current = *DtList; |
| 1640 | struct fdt_entry_node *Next; |
| 1641 | |
| 1642 | while (Current != NULL) { |
| 1643 | Next = Current->next; |
| 1644 | FreePool (Current); |
| 1645 | Current = Next; |
| 1646 | } |
| 1647 | |
| 1648 | *DtList = NULL; |
| 1649 | } |