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