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