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