Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Common initialisation for Qualcomm Snapdragon boards. |
| 4 | * |
| 5 | * Copyright (c) 2024 Linaro Ltd. |
| 6 | * Author: Caleb Connolly <caleb.connolly@linaro.org> |
| 7 | */ |
| 8 | |
Caleb Connolly | 3b5faa5 | 2024-11-13 06:04:17 +0100 | [diff] [blame^] | 9 | #define LOG_CATEGORY LOGC_BOARD |
| 10 | #define pr_fmt(fmt) "QCOM: " fmt |
| 11 | |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 12 | #include <asm/armv8/mmu.h> |
| 13 | #include <asm/gpio.h> |
| 14 | #include <asm/io.h> |
| 15 | #include <asm/psci.h> |
| 16 | #include <asm/system.h> |
| 17 | #include <dm/device.h> |
| 18 | #include <dm/pinctrl.h> |
| 19 | #include <dm/uclass-internal.h> |
| 20 | #include <dm/read.h> |
Caleb Connolly | 5ecc90d | 2024-04-03 14:07:47 +0200 | [diff] [blame] | 21 | #include <power/regulator.h> |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 22 | #include <env.h> |
Caleb Connolly | c0cd294 | 2024-08-09 01:59:24 +0200 | [diff] [blame] | 23 | #include <fdt_support.h> |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 24 | #include <init.h> |
| 25 | #include <linux/arm-smccc.h> |
| 26 | #include <linux/bug.h> |
| 27 | #include <linux/psci.h> |
| 28 | #include <linux/sizes.h> |
Caleb Connolly | 3138566 | 2024-02-26 17:26:25 +0000 | [diff] [blame] | 29 | #include <lmb.h> |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 30 | #include <malloc.h> |
Volodymyr Babchuk | bdacfea | 2024-03-11 21:33:45 +0000 | [diff] [blame] | 31 | #include <fdt_support.h> |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 32 | #include <usb.h> |
Caleb Connolly | 8198246 | 2024-02-26 17:26:27 +0000 | [diff] [blame] | 33 | #include <sort.h> |
Caleb Connolly | 3b5faa5 | 2024-11-13 06:04:17 +0100 | [diff] [blame^] | 34 | #include <time.h> |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 35 | |
Caleb Connolly | 2983ae8 | 2024-04-03 14:07:45 +0200 | [diff] [blame] | 36 | #include "qcom-priv.h" |
| 37 | |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 38 | DECLARE_GLOBAL_DATA_PTR; |
| 39 | |
| 40 | static struct mm_region rbx_mem_map[CONFIG_NR_DRAM_BANKS + 2] = { { 0 } }; |
| 41 | |
| 42 | struct mm_region *mem_map = rbx_mem_map; |
| 43 | |
Caleb Connolly | e83b1cd | 2024-08-09 01:59:25 +0200 | [diff] [blame] | 44 | static struct { |
| 45 | phys_addr_t start; |
| 46 | phys_size_t size; |
| 47 | } prevbl_ddr_banks[CONFIG_NR_DRAM_BANKS] __section(".data") = { 0 }; |
| 48 | |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 49 | int dram_init(void) |
| 50 | { |
Caleb Connolly | e83b1cd | 2024-08-09 01:59:25 +0200 | [diff] [blame] | 51 | /* |
| 52 | * gd->ram_base / ram_size have been setup already |
| 53 | * in qcom_parse_memory(). |
| 54 | */ |
| 55 | return 0; |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static int ddr_bank_cmp(const void *v1, const void *v2) |
| 59 | { |
| 60 | const struct { |
| 61 | phys_addr_t start; |
| 62 | phys_size_t size; |
| 63 | } *res1 = v1, *res2 = v2; |
| 64 | |
| 65 | if (!res1->size) |
| 66 | return 1; |
| 67 | if (!res2->size) |
| 68 | return -1; |
| 69 | |
| 70 | return (res1->start >> 24) - (res2->start >> 24); |
| 71 | } |
| 72 | |
Caleb Connolly | e83b1cd | 2024-08-09 01:59:25 +0200 | [diff] [blame] | 73 | /* This has to be done post-relocation since gd->bd isn't preserved */ |
| 74 | static void qcom_configure_bi_dram(void) |
| 75 | { |
| 76 | int i; |
| 77 | |
| 78 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 79 | gd->bd->bi_dram[i].start = prevbl_ddr_banks[i].start; |
| 80 | gd->bd->bi_dram[i].size = prevbl_ddr_banks[i].size; |
| 81 | } |
| 82 | } |
| 83 | |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 84 | int dram_init_banksize(void) |
| 85 | { |
Caleb Connolly | e83b1cd | 2024-08-09 01:59:25 +0200 | [diff] [blame] | 86 | qcom_configure_bi_dram(); |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 87 | |
Caleb Connolly | e83b1cd | 2024-08-09 01:59:25 +0200 | [diff] [blame] | 88 | return 0; |
| 89 | } |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 90 | |
Caleb Connolly | e83b1cd | 2024-08-09 01:59:25 +0200 | [diff] [blame] | 91 | static void qcom_parse_memory(void) |
| 92 | { |
| 93 | ofnode node; |
| 94 | const fdt64_t *memory; |
| 95 | int memsize; |
| 96 | phys_addr_t ram_end = 0; |
| 97 | int i, j, banks; |
| 98 | |
| 99 | node = ofnode_path("/memory"); |
| 100 | if (!ofnode_valid(node)) { |
| 101 | log_err("No memory node found in device tree!\n"); |
| 102 | return; |
| 103 | } |
| 104 | memory = ofnode_read_prop(node, "reg", &memsize); |
| 105 | if (!memory) { |
| 106 | log_err("No memory configuration was provided by the previous bootloader!\n"); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | banks = min(memsize / (2 * sizeof(u64)), (ulong)CONFIG_NR_DRAM_BANKS); |
| 111 | |
| 112 | if (memsize / sizeof(u64) > CONFIG_NR_DRAM_BANKS * 2) |
| 113 | log_err("Provided more than the max of %d memory banks\n", CONFIG_NR_DRAM_BANKS); |
| 114 | |
| 115 | if (banks > CONFIG_NR_DRAM_BANKS) |
| 116 | log_err("Provided more memory banks than we can handle\n"); |
| 117 | |
| 118 | for (i = 0, j = 0; i < banks * 2; i += 2, j++) { |
| 119 | prevbl_ddr_banks[j].start = get_unaligned_be64(&memory[i]); |
| 120 | prevbl_ddr_banks[j].size = get_unaligned_be64(&memory[i + 1]); |
| 121 | /* SM8650 boards sometimes have empty regions! */ |
| 122 | if (!prevbl_ddr_banks[j].size) { |
| 123 | j--; |
| 124 | continue; |
| 125 | } |
| 126 | ram_end = max(ram_end, prevbl_ddr_banks[j].start + prevbl_ddr_banks[j].size); |
| 127 | } |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 128 | |
| 129 | /* Sort our RAM banks -_- */ |
Caleb Connolly | e83b1cd | 2024-08-09 01:59:25 +0200 | [diff] [blame] | 130 | qsort(prevbl_ddr_banks, banks, sizeof(prevbl_ddr_banks[0]), ddr_bank_cmp); |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 131 | |
Caleb Connolly | e83b1cd | 2024-08-09 01:59:25 +0200 | [diff] [blame] | 132 | gd->ram_base = prevbl_ddr_banks[0].start; |
| 133 | gd->ram_size = ram_end - gd->ram_base; |
| 134 | debug("ram_base = %#011lx, ram_size = %#011llx, ram_end = %#011llx\n", |
| 135 | gd->ram_base, gd->ram_size, ram_end); |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static void show_psci_version(void) |
| 139 | { |
| 140 | struct arm_smccc_res res; |
| 141 | |
| 142 | arm_smccc_smc(ARM_PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0, 0, 0, 0, 0, &res); |
| 143 | |
| 144 | debug("PSCI: v%ld.%ld\n", |
| 145 | PSCI_VERSION_MAJOR(res.a0), |
| 146 | PSCI_VERSION_MINOR(res.a0)); |
| 147 | } |
| 148 | |
Caleb Connolly | c0cd294 | 2024-08-09 01:59:24 +0200 | [diff] [blame] | 149 | /* We support booting U-Boot with an internal DT when running as a first-stage bootloader |
| 150 | * or for supporting quirky devices where it's easier to leave the downstream DT in place |
| 151 | * to improve ABL compatibility. Otherwise, we use the DT provided by ABL. |
| 152 | */ |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 153 | void *board_fdt_blob_setup(int *err) |
| 154 | { |
Caleb Connolly | c0cd294 | 2024-08-09 01:59:24 +0200 | [diff] [blame] | 155 | struct fdt_header *fdt; |
| 156 | bool internal_valid, external_valid; |
| 157 | |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 158 | *err = 0; |
Caleb Connolly | c0cd294 | 2024-08-09 01:59:24 +0200 | [diff] [blame] | 159 | fdt = (struct fdt_header *)get_prev_bl_fdt_addr(); |
| 160 | external_valid = fdt && !fdt_check_header(fdt); |
| 161 | internal_valid = !fdt_check_header(gd->fdt_blob); |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 162 | |
| 163 | /* |
Caleb Connolly | c0cd294 | 2024-08-09 01:59:24 +0200 | [diff] [blame] | 164 | * There is no point returning an error here, U-Boot can't do anything useful in this situation. |
| 165 | * Bail out while we can still print a useful error message. |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 166 | */ |
Caleb Connolly | c0cd294 | 2024-08-09 01:59:24 +0200 | [diff] [blame] | 167 | if (!internal_valid && !external_valid) |
| 168 | panic("Internal FDT is invalid and no external FDT was provided! (fdt=%#llx)\n", |
| 169 | (phys_addr_t)fdt); |
Volodymyr Babchuk | bdacfea | 2024-03-11 21:33:45 +0000 | [diff] [blame] | 170 | |
Caleb Connolly | c0cd294 | 2024-08-09 01:59:24 +0200 | [diff] [blame] | 171 | if (internal_valid) { |
| 172 | debug("Using built in FDT\n"); |
Caleb Connolly | c0cd294 | 2024-08-09 01:59:24 +0200 | [diff] [blame] | 173 | } else { |
| 174 | debug("Using external FDT\n"); |
Caleb Connolly | e83b1cd | 2024-08-09 01:59:25 +0200 | [diff] [blame] | 175 | /* So we can use it before returning */ |
| 176 | gd->fdt_blob = fdt; |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 177 | } |
Caleb Connolly | e83b1cd | 2024-08-09 01:59:25 +0200 | [diff] [blame] | 178 | |
| 179 | /* |
| 180 | * Parse the /memory node while we're here, |
| 181 | * this makes it easy to do other things early. |
| 182 | */ |
| 183 | qcom_parse_memory(); |
| 184 | |
| 185 | return (void *)gd->fdt_blob; |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void reset_cpu(void) |
| 189 | { |
| 190 | psci_system_reset(); |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * Some Qualcomm boards require GPIO configuration when switching USB modes. |
| 195 | * Support setting this configuration via pinctrl state. |
| 196 | */ |
| 197 | int board_usb_init(int index, enum usb_init_type init) |
| 198 | { |
| 199 | struct udevice *usb; |
| 200 | int ret = 0; |
| 201 | |
| 202 | /* USB device */ |
| 203 | ret = uclass_find_device_by_seq(UCLASS_USB, index, &usb); |
| 204 | if (ret) { |
| 205 | printf("Cannot find USB device\n"); |
| 206 | return ret; |
| 207 | } |
| 208 | |
| 209 | ret = dev_read_stringlist_search(usb, "pinctrl-names", |
| 210 | "device"); |
| 211 | /* No "device" pinctrl state, so just bail */ |
| 212 | if (ret < 0) |
| 213 | return 0; |
| 214 | |
| 215 | /* Select "default" or "device" pinctrl */ |
| 216 | switch (init) { |
| 217 | case USB_INIT_HOST: |
| 218 | pinctrl_select_state(usb, "default"); |
| 219 | break; |
| 220 | case USB_INIT_DEVICE: |
| 221 | pinctrl_select_state(usb, "device"); |
| 222 | break; |
| 223 | default: |
| 224 | debug("Unknown usb_init_type %d\n", init); |
| 225 | break; |
| 226 | } |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * Some boards still need board specific init code, they can implement that by |
| 233 | * overriding this function. |
| 234 | * |
| 235 | * FIXME: get rid of board specific init code |
| 236 | */ |
| 237 | void __weak qcom_board_init(void) |
| 238 | { |
| 239 | } |
| 240 | |
| 241 | int board_init(void) |
| 242 | { |
| 243 | show_psci_version(); |
Caleb Connolly | 2983ae8 | 2024-04-03 14:07:45 +0200 | [diff] [blame] | 244 | qcom_of_fixup_nodes(); |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 245 | qcom_board_init(); |
| 246 | return 0; |
| 247 | } |
| 248 | |
Caleb Connolly | 750a0fa | 2024-08-09 01:59:27 +0200 | [diff] [blame] | 249 | /** |
| 250 | * out_len includes the trailing null space |
| 251 | */ |
| 252 | static int get_cmdline_option(const char *cmdline, const char *key, char *out, int out_len) |
| 253 | { |
| 254 | const char *p, *p_end; |
| 255 | int len; |
| 256 | |
| 257 | p = strstr(cmdline, key); |
| 258 | if (!p) |
| 259 | return -ENOENT; |
| 260 | |
| 261 | p += strlen(key); |
| 262 | p_end = strstr(p, " "); |
| 263 | if (!p_end) |
| 264 | return -ENOENT; |
| 265 | |
| 266 | len = p_end - p; |
| 267 | if (len > out_len) |
| 268 | len = out_len; |
| 269 | |
| 270 | strncpy(out, p, len); |
| 271 | out[len] = '\0'; |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | /* The bootargs are populated by the previous stage bootloader */ |
| 277 | static const char *get_cmdline(void) |
| 278 | { |
| 279 | ofnode node; |
| 280 | static const char *cmdline = NULL; |
| 281 | |
| 282 | if (cmdline) |
| 283 | return cmdline; |
| 284 | |
| 285 | node = ofnode_path("/chosen"); |
| 286 | if (!ofnode_valid(node)) |
| 287 | return NULL; |
| 288 | |
| 289 | cmdline = ofnode_read_string(node, "bootargs"); |
| 290 | |
| 291 | return cmdline; |
| 292 | } |
| 293 | |
| 294 | void qcom_set_serialno(void) |
| 295 | { |
| 296 | const char *cmdline = get_cmdline(); |
| 297 | char serial[32]; |
| 298 | |
| 299 | if (!cmdline) { |
| 300 | log_debug("Failed to get bootargs\n"); |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | get_cmdline_option(cmdline, "androidboot.serialno=", serial, sizeof(serial)); |
| 305 | if (serial[0] != '\0') |
| 306 | env_set("serial#", serial); |
| 307 | } |
| 308 | |
Caleb Connolly | a015690 | 2024-02-26 17:26:26 +0000 | [diff] [blame] | 309 | /* Sets up the "board", and "soc" environment variables as well as constructing the devicetree |
| 310 | * path, with a few quirks to handle non-standard dtb filenames. This is not meant to be a |
| 311 | * comprehensive solution to automatically picking the DTB, but aims to be correct for the |
| 312 | * majority case. For most devices it should be possible to make this algorithm work by |
| 313 | * adjusting the root compatible property in the U-Boot DTS. Handling devices with multiple |
| 314 | * variants that are all supported by a single U-Boot image will require implementing device- |
| 315 | * specific detection. |
| 316 | */ |
| 317 | static void configure_env(void) |
| 318 | { |
| 319 | const char *first_compat, *last_compat; |
| 320 | char *tmp; |
| 321 | char buf[32] = { 0 }; |
| 322 | /* |
| 323 | * Most DTB filenames follow the scheme: qcom/<soc>-[vendor]-<board>.dtb |
| 324 | * The vendor is skipped when it's a Qualcomm reference board, or the |
| 325 | * db845c. |
| 326 | */ |
| 327 | char dt_path[64] = { 0 }; |
| 328 | int compat_count, ret; |
| 329 | ofnode root; |
| 330 | |
| 331 | root = ofnode_root(); |
| 332 | /* This is almost always 2, but be explicit that we want the first and last compatibles |
| 333 | * not the first and second. |
| 334 | */ |
| 335 | compat_count = ofnode_read_string_count(root, "compatible"); |
| 336 | if (compat_count < 2) { |
| 337 | log_warning("%s: only one root compatible bailing!\n", __func__); |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | /* The most specific device compatible (e.g. "thundercomm,db845c") */ |
| 342 | ret = ofnode_read_string_index(root, "compatible", 0, &first_compat); |
| 343 | if (ret < 0) { |
| 344 | log_warning("Can't read first compatible\n"); |
| 345 | return; |
| 346 | } |
| 347 | |
| 348 | /* The last compatible is always the SoC compatible */ |
| 349 | ret = ofnode_read_string_index(root, "compatible", compat_count - 1, &last_compat); |
| 350 | if (ret < 0) { |
| 351 | log_warning("Can't read second compatible\n"); |
| 352 | return; |
| 353 | } |
| 354 | |
| 355 | /* Copy the second compat (e.g. "qcom,sdm845") into buf */ |
| 356 | strlcpy(buf, last_compat, sizeof(buf) - 1); |
| 357 | tmp = buf; |
| 358 | |
| 359 | /* strsep() is destructive, it replaces the comma with a \0 */ |
| 360 | if (!strsep(&tmp, ",")) { |
| 361 | log_warning("second compatible '%s' has no ','\n", buf); |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | /* tmp now points to just the "sdm845" part of the string */ |
| 366 | env_set("soc", tmp); |
| 367 | |
| 368 | /* Now figure out the "board" part from the first compatible */ |
| 369 | memset(buf, 0, sizeof(buf)); |
| 370 | strlcpy(buf, first_compat, sizeof(buf) - 1); |
| 371 | tmp = buf; |
| 372 | |
| 373 | /* The Qualcomm reference boards (RBx, HDK, etc) */ |
| 374 | if (!strncmp("qcom", buf, strlen("qcom"))) { |
| 375 | /* |
| 376 | * They all have the first compatible as "qcom,<soc>-<board>" |
| 377 | * (e.g. "qcom,qrb5165-rb5"). We extract just the part after |
| 378 | * the dash. |
| 379 | */ |
| 380 | if (!strsep(&tmp, "-")) { |
| 381 | log_warning("compatible '%s' has no '-'\n", buf); |
| 382 | return; |
| 383 | } |
| 384 | /* tmp is now "rb5" */ |
| 385 | env_set("board", tmp); |
| 386 | } else { |
| 387 | if (!strsep(&tmp, ",")) { |
| 388 | log_warning("compatible '%s' has no ','\n", buf); |
| 389 | return; |
| 390 | } |
| 391 | /* for thundercomm we just want the bit after the comma (e.g. "db845c"), |
| 392 | * for all other boards we replace the comma with a '-' and take both |
| 393 | * (e.g. "oneplus-enchilada") |
| 394 | */ |
| 395 | if (!strncmp("thundercomm", buf, strlen("thundercomm"))) { |
| 396 | env_set("board", tmp); |
| 397 | } else { |
| 398 | *(tmp - 1) = '-'; |
| 399 | env_set("board", buf); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | /* Now build the full path name */ |
| 404 | snprintf(dt_path, sizeof(dt_path), "qcom/%s-%s.dtb", |
| 405 | env_get("soc"), env_get("board")); |
| 406 | env_set("fdtfile", dt_path); |
Caleb Connolly | 750a0fa | 2024-08-09 01:59:27 +0200 | [diff] [blame] | 407 | |
| 408 | qcom_set_serialno(); |
Caleb Connolly | a015690 | 2024-02-26 17:26:26 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Caleb Connolly | 3138566 | 2024-02-26 17:26:25 +0000 | [diff] [blame] | 411 | void __weak qcom_late_init(void) |
| 412 | { |
| 413 | } |
| 414 | |
| 415 | #define KERNEL_COMP_SIZE SZ_64M |
Caleb Connolly | 94a5084 | 2024-08-09 01:59:28 +0200 | [diff] [blame] | 416 | #ifdef CONFIG_FASTBOOT_BUF_SIZE |
| 417 | #define FASTBOOT_BUF_SIZE CONFIG_FASTBOOT_BUF_SIZE |
| 418 | #else |
| 419 | #define FASTBOOT_BUF_SIZE 0 |
| 420 | #endif |
Caleb Connolly | 3138566 | 2024-02-26 17:26:25 +0000 | [diff] [blame] | 421 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 422 | #define addr_alloc(size) lmb_alloc(size, SZ_2M) |
Caleb Connolly | 3138566 | 2024-02-26 17:26:25 +0000 | [diff] [blame] | 423 | |
| 424 | /* Stolen from arch/arm/mach-apple/board.c */ |
| 425 | int board_late_init(void) |
| 426 | { |
Caleb Connolly | 3138566 | 2024-02-26 17:26:25 +0000 | [diff] [blame] | 427 | u32 status = 0; |
Caleb Connolly | 3928873 | 2024-08-09 01:59:30 +0200 | [diff] [blame] | 428 | phys_addr_t addr; |
Caleb Connolly | eee2b6f | 2024-08-09 01:59:29 +0200 | [diff] [blame] | 429 | struct fdt_header *fdt_blob = (struct fdt_header *)gd->fdt_blob; |
Caleb Connolly | 3138566 | 2024-02-26 17:26:25 +0000 | [diff] [blame] | 430 | |
Caleb Connolly | 3138566 | 2024-02-26 17:26:25 +0000 | [diff] [blame] | 431 | /* We need to be fairly conservative here as we support boards with just 1G of TOTAL RAM */ |
Caleb Connolly | 3928873 | 2024-08-09 01:59:30 +0200 | [diff] [blame] | 432 | addr = addr_alloc(SZ_128M); |
| 433 | status |= env_set_hex("kernel_addr_r", addr); |
| 434 | status |= env_set_hex("loadaddr", addr); |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 435 | status |= env_set_hex("ramdisk_addr_r", addr_alloc(SZ_128M)); |
| 436 | status |= env_set_hex("kernel_comp_addr_r", addr_alloc(KERNEL_COMP_SIZE)); |
Caleb Connolly | 3138566 | 2024-02-26 17:26:25 +0000 | [diff] [blame] | 437 | status |= env_set_hex("kernel_comp_size", KERNEL_COMP_SIZE); |
Caleb Connolly | 94a5084 | 2024-08-09 01:59:28 +0200 | [diff] [blame] | 438 | if (IS_ENABLED(CONFIG_FASTBOOT)) |
| 439 | status |= env_set_hex("fastboot_addr_r", addr_alloc(FASTBOOT_BUF_SIZE)); |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 440 | status |= env_set_hex("scriptaddr", addr_alloc(SZ_4M)); |
| 441 | status |= env_set_hex("pxefile_addr_r", addr_alloc(SZ_4M)); |
Caleb Connolly | 3928873 | 2024-08-09 01:59:30 +0200 | [diff] [blame] | 442 | addr = addr_alloc(SZ_2M); |
| 443 | status |= env_set_hex("fdt_addr_r", addr); |
Caleb Connolly | 3138566 | 2024-02-26 17:26:25 +0000 | [diff] [blame] | 444 | |
| 445 | if (status) |
| 446 | log_warning("%s: Failed to set run time variables\n", __func__); |
| 447 | |
Caleb Connolly | eee2b6f | 2024-08-09 01:59:29 +0200 | [diff] [blame] | 448 | /* By default copy U-Boots FDT, it will be used as a fallback */ |
Caleb Connolly | 3928873 | 2024-08-09 01:59:30 +0200 | [diff] [blame] | 449 | memcpy((void *)addr, (void *)gd->fdt_blob, fdt32_to_cpu(fdt_blob->totalsize)); |
Caleb Connolly | eee2b6f | 2024-08-09 01:59:29 +0200 | [diff] [blame] | 450 | |
Caleb Connolly | a015690 | 2024-02-26 17:26:26 +0000 | [diff] [blame] | 451 | configure_env(); |
Caleb Connolly | 3138566 | 2024-02-26 17:26:25 +0000 | [diff] [blame] | 452 | qcom_late_init(); |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 457 | static void build_mem_map(void) |
| 458 | { |
Caleb Connolly | 8198246 | 2024-02-26 17:26:27 +0000 | [diff] [blame] | 459 | int i, j; |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 460 | |
| 461 | /* |
| 462 | * Ensure the peripheral block is sized to correctly cover the address range |
| 463 | * up to the first memory bank. |
| 464 | * Don't map the first page to ensure that we actually trigger an abort on a |
| 465 | * null pointer access rather than just hanging. |
| 466 | * FIXME: we should probably split this into more precise regions |
| 467 | */ |
| 468 | mem_map[0].phys = 0x1000; |
| 469 | mem_map[0].virt = mem_map[0].phys; |
| 470 | mem_map[0].size = gd->bd->bi_dram[0].start - mem_map[0].phys; |
| 471 | mem_map[0].attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 472 | PTE_BLOCK_NON_SHARE | |
| 473 | PTE_BLOCK_PXN | PTE_BLOCK_UXN; |
| 474 | |
Caleb Connolly | 8198246 | 2024-02-26 17:26:27 +0000 | [diff] [blame] | 475 | for (i = 1, j = 0; i < ARRAY_SIZE(rbx_mem_map) - 1 && gd->bd->bi_dram[j].size; i++, j++) { |
| 476 | mem_map[i].phys = gd->bd->bi_dram[j].start; |
| 477 | mem_map[i].virt = mem_map[i].phys; |
| 478 | mem_map[i].size = gd->bd->bi_dram[j].size; |
| 479 | mem_map[i].attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | \ |
| 480 | PTE_BLOCK_INNER_SHARE; |
| 481 | } |
| 482 | |
| 483 | mem_map[i].phys = UINT64_MAX; |
| 484 | mem_map[i].size = 0; |
| 485 | |
| 486 | #ifdef DEBUG |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 487 | debug("Configured memory map:\n"); |
Caleb Connolly | 8198246 | 2024-02-26 17:26:27 +0000 | [diff] [blame] | 488 | for (i = 0; mem_map[i].size; i++) |
| 489 | debug(" 0x%016llx - 0x%016llx: entry %d\n", |
| 490 | mem_map[i].phys, mem_map[i].phys + mem_map[i].size, i); |
| 491 | #endif |
| 492 | } |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 493 | |
Caleb Connolly | 8198246 | 2024-02-26 17:26:27 +0000 | [diff] [blame] | 494 | u64 get_page_table_size(void) |
| 495 | { |
Neil Armstrong | 4086a1f | 2024-08-09 01:59:26 +0200 | [diff] [blame] | 496 | return SZ_1M; |
Caleb Connolly | 8198246 | 2024-02-26 17:26:27 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | static int fdt_cmp_res(const void *v1, const void *v2) |
| 500 | { |
| 501 | const struct fdt_resource *res1 = v1, *res2 = v2; |
| 502 | |
| 503 | return res1->start - res2->start; |
| 504 | } |
| 505 | |
| 506 | #define N_RESERVED_REGIONS 32 |
| 507 | |
| 508 | /* Mark all no-map regions as PTE_TYPE_FAULT to prevent speculative access. |
| 509 | * On some platforms this is enough to trigger a security violation and trap |
| 510 | * to EL3. |
| 511 | */ |
| 512 | static void carve_out_reserved_memory(void) |
| 513 | { |
| 514 | static struct fdt_resource res[N_RESERVED_REGIONS] = { 0 }; |
| 515 | int parent, rmem, count, i = 0; |
| 516 | phys_addr_t start; |
| 517 | size_t size; |
| 518 | |
| 519 | /* Some reserved nodes must be carved out, as the cache-prefetcher may otherwise |
| 520 | * attempt to access them, causing a security exception. |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 521 | */ |
Caleb Connolly | 8198246 | 2024-02-26 17:26:27 +0000 | [diff] [blame] | 522 | parent = fdt_path_offset(gd->fdt_blob, "/reserved-memory"); |
| 523 | if (parent <= 0) { |
| 524 | log_err("No reserved memory regions found\n"); |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | /* Collect the reserved memory regions */ |
| 529 | fdt_for_each_subnode(rmem, gd->fdt_blob, parent) { |
| 530 | const fdt32_t *ptr; |
| 531 | int len; |
| 532 | if (!fdt_getprop(gd->fdt_blob, rmem, "no-map", NULL)) |
| 533 | continue; |
| 534 | |
| 535 | if (i == N_RESERVED_REGIONS) { |
| 536 | log_err("Too many reserved regions!\n"); |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 537 | break; |
| 538 | } |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 539 | |
Caleb Connolly | 8198246 | 2024-02-26 17:26:27 +0000 | [diff] [blame] | 540 | /* Read the address and size out from the reg property. Doing this "properly" with |
| 541 | * fdt_get_resource() takes ~70ms on SDM845, but open-coding the happy path here |
| 542 | * takes <1ms... Oh the woes of no dcache. |
| 543 | */ |
| 544 | ptr = fdt_getprop(gd->fdt_blob, rmem, "reg", &len); |
| 545 | if (ptr) { |
| 546 | /* Qualcomm devices use #address/size-cells = <2> but all reserved regions are within |
| 547 | * the 32-bit address space. So we can cheat here for speed. |
| 548 | */ |
| 549 | res[i].start = fdt32_to_cpu(ptr[1]); |
| 550 | res[i].end = res[i].start + fdt32_to_cpu(ptr[3]); |
| 551 | i++; |
| 552 | } |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 553 | } |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 554 | |
Caleb Connolly | 8198246 | 2024-02-26 17:26:27 +0000 | [diff] [blame] | 555 | /* Sort the reserved memory regions by address */ |
| 556 | count = i; |
| 557 | qsort(res, count, sizeof(struct fdt_resource), fdt_cmp_res); |
| 558 | |
| 559 | /* Now set the right attributes for them. Often a lot of the regions are tightly packed together |
| 560 | * so we can optimise the number of calls to mmu_change_region_attr() by combining adjacent |
| 561 | * regions. |
| 562 | */ |
| 563 | start = ALIGN_DOWN(res[0].start, SZ_2M); |
| 564 | size = ALIGN(res[0].end - start, SZ_2M); |
| 565 | for (i = 1; i <= count; i++) { |
| 566 | /* We ideally want to 2M align everything for more efficient pagetables, but we must avoid |
| 567 | * overwriting reserved memory regions which shouldn't be mapped as FAULT (like those with |
| 568 | * compatible properties). |
| 569 | * If within 2M of the previous region, bump the size to include this region. Otherwise |
| 570 | * start a new region. |
| 571 | */ |
| 572 | if (i == count || start + size < res[i].start - SZ_2M) { |
| 573 | debug(" 0x%016llx - 0x%016llx: reserved\n", |
| 574 | start, start + size); |
| 575 | mmu_change_region_attr(start, size, PTE_TYPE_FAULT); |
| 576 | /* If this is the final region then quit here before we index |
| 577 | * out of bounds... |
| 578 | */ |
| 579 | if (i == count) |
| 580 | break; |
| 581 | start = ALIGN_DOWN(res[i].start, SZ_2M); |
| 582 | size = ALIGN(res[i].end - start, SZ_2M); |
| 583 | } else { |
| 584 | /* Bump size if this region is immediately after the previous one */ |
| 585 | size = ALIGN(res[i].end - start, SZ_2M); |
| 586 | } |
| 587 | } |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Caleb Connolly | 8198246 | 2024-02-26 17:26:27 +0000 | [diff] [blame] | 590 | /* This function open-codes setup_all_pgtables() so that we can |
| 591 | * insert additional mappings *before* turning on the MMU. |
| 592 | */ |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 593 | void enable_caches(void) |
| 594 | { |
Caleb Connolly | 8198246 | 2024-02-26 17:26:27 +0000 | [diff] [blame] | 595 | u64 tlb_addr = gd->arch.tlb_addr; |
| 596 | u64 tlb_size = gd->arch.tlb_size; |
| 597 | u64 pt_size; |
| 598 | ulong carveout_start; |
| 599 | |
| 600 | gd->arch.tlb_fillptr = tlb_addr; |
| 601 | |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 602 | build_mem_map(); |
| 603 | |
| 604 | icache_enable(); |
Caleb Connolly | 8198246 | 2024-02-26 17:26:27 +0000 | [diff] [blame] | 605 | |
| 606 | /* Create normal system page tables */ |
| 607 | setup_pgtables(); |
| 608 | |
| 609 | pt_size = (uintptr_t)gd->arch.tlb_fillptr - |
| 610 | (uintptr_t)gd->arch.tlb_addr; |
| 611 | debug("Primary pagetable size: %lluKiB\n", pt_size / 1024); |
| 612 | |
| 613 | /* Create emergency page tables */ |
| 614 | gd->arch.tlb_size -= pt_size; |
| 615 | gd->arch.tlb_addr = gd->arch.tlb_fillptr; |
| 616 | setup_pgtables(); |
| 617 | gd->arch.tlb_emerg = gd->arch.tlb_addr; |
| 618 | gd->arch.tlb_addr = tlb_addr; |
| 619 | gd->arch.tlb_size = tlb_size; |
| 620 | |
Sam Day | 46760ba | 2024-05-07 18:41:23 +0000 | [diff] [blame] | 621 | /* We do the carveouts only for QCS404, for now. */ |
| 622 | if (fdt_node_check_compatible(gd->fdt_blob, 0, "qcom,qcs404") == 0) { |
| 623 | carveout_start = get_timer(0); |
| 624 | /* Takes ~20-50ms on SDM845 */ |
| 625 | carve_out_reserved_memory(); |
| 626 | debug("carveout time: %lums\n", get_timer(carveout_start)); |
| 627 | } |
Caleb Connolly | fe1694c | 2024-02-26 17:26:24 +0000 | [diff] [blame] | 628 | dcache_enable(); |
| 629 | } |