blob: e87551784b8d7afb4186533d43b8bddacba9faec [file] [log] [blame]
Caleb Connollyfe1694c2024-02-26 17:26:24 +00001// 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 Connolly3b5faa52024-11-13 06:04:17 +01009#define LOG_CATEGORY LOGC_BOARD
10#define pr_fmt(fmt) "QCOM: " fmt
11
Caleb Connollyfe1694c2024-02-26 17:26:24 +000012#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 Connolly5ecc90d2024-04-03 14:07:47 +020021#include <power/regulator.h>
Caleb Connollyfe1694c2024-02-26 17:26:24 +000022#include <env.h>
Caleb Connollyc0cd2942024-08-09 01:59:24 +020023#include <fdt_support.h>
Caleb Connollyfe1694c2024-02-26 17:26:24 +000024#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 Connolly31385662024-02-26 17:26:25 +000029#include <lmb.h>
Caleb Connollyfe1694c2024-02-26 17:26:24 +000030#include <malloc.h>
Volodymyr Babchukbdacfea2024-03-11 21:33:45 +000031#include <fdt_support.h>
Caleb Connollyfe1694c2024-02-26 17:26:24 +000032#include <usb.h>
Caleb Connolly81982462024-02-26 17:26:27 +000033#include <sort.h>
Caleb Connolly3b5faa52024-11-13 06:04:17 +010034#include <time.h>
Caleb Connollyfe1694c2024-02-26 17:26:24 +000035
Caleb Connolly2983ae82024-04-03 14:07:45 +020036#include "qcom-priv.h"
37
Caleb Connollyfe1694c2024-02-26 17:26:24 +000038DECLARE_GLOBAL_DATA_PTR;
39
40static struct mm_region rbx_mem_map[CONFIG_NR_DRAM_BANKS + 2] = { { 0 } };
41
42struct mm_region *mem_map = rbx_mem_map;
43
Caleb Connollye83b1cd2024-08-09 01:59:25 +020044static struct {
45 phys_addr_t start;
46 phys_size_t size;
47} prevbl_ddr_banks[CONFIG_NR_DRAM_BANKS] __section(".data") = { 0 };
48
Caleb Connollyfe1694c2024-02-26 17:26:24 +000049int dram_init(void)
50{
Caleb Connollye83b1cd2024-08-09 01:59:25 +020051 /*
52 * gd->ram_base / ram_size have been setup already
53 * in qcom_parse_memory().
54 */
55 return 0;
Caleb Connollyfe1694c2024-02-26 17:26:24 +000056}
57
58static 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 Connollye83b1cd2024-08-09 01:59:25 +020073/* This has to be done post-relocation since gd->bd isn't preserved */
74static 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 Connollyfe1694c2024-02-26 17:26:24 +000084int dram_init_banksize(void)
85{
Caleb Connollye83b1cd2024-08-09 01:59:25 +020086 qcom_configure_bi_dram();
Caleb Connollyfe1694c2024-02-26 17:26:24 +000087
Caleb Connollye83b1cd2024-08-09 01:59:25 +020088 return 0;
89}
Caleb Connollyfe1694c2024-02-26 17:26:24 +000090
Sam Day72546b02025-01-23 12:12:14 +000091/**
92 * The generic memory parsing code in U-Boot lacks a few things that we
93 * need on Qualcomm:
94 *
95 * 1. It sets gd->ram_size and gd->ram_base to represent a single memory block
96 * 2. setup_dest_addr() later relocates U-Boot to ram_base + ram_size, the end
97 * of that first memory block.
98 *
99 * This results in all memory beyond U-Boot being unusable in Linux when booting
100 * with EFI.
101 *
102 * Since the ranges in the memory node may be out of order, the only way for us
103 * to correctly determine the relocation address for U-Boot is to parse all
104 * memory regions and find the highest valid address.
105 *
106 * We can't use fdtdec_setup_memory_banksize() since it stores the result in
107 * gd->bd, which is not yet allocated.
108 *
109 * @fdt: FDT blob to parse /memory node from
110 *
111 * Return: 0 on success or -ENODATA if /memory node is missing or incomplete
112 */
113static int qcom_parse_memory(const void *fdt)
Caleb Connollye83b1cd2024-08-09 01:59:25 +0200114{
Sam Day798847f2025-01-22 10:26:59 +0000115 int offset;
Caleb Connollye83b1cd2024-08-09 01:59:25 +0200116 const fdt64_t *memory;
117 int memsize;
118 phys_addr_t ram_end = 0;
119 int i, j, banks;
120
Sam Day798847f2025-01-22 10:26:59 +0000121 offset = fdt_path_offset(fdt, "/memory");
Sam Day72546b02025-01-23 12:12:14 +0000122 if (offset < 0)
123 return -ENODATA;
Sam Day798847f2025-01-22 10:26:59 +0000124
125 memory = fdt_getprop(fdt, offset, "reg", &memsize);
Sam Day72546b02025-01-23 12:12:14 +0000126 if (!memory)
127 return -ENODATA;
Caleb Connollye83b1cd2024-08-09 01:59:25 +0200128
129 banks = min(memsize / (2 * sizeof(u64)), (ulong)CONFIG_NR_DRAM_BANKS);
130
131 if (memsize / sizeof(u64) > CONFIG_NR_DRAM_BANKS * 2)
132 log_err("Provided more than the max of %d memory banks\n", CONFIG_NR_DRAM_BANKS);
133
134 if (banks > CONFIG_NR_DRAM_BANKS)
135 log_err("Provided more memory banks than we can handle\n");
136
137 for (i = 0, j = 0; i < banks * 2; i += 2, j++) {
138 prevbl_ddr_banks[j].start = get_unaligned_be64(&memory[i]);
139 prevbl_ddr_banks[j].size = get_unaligned_be64(&memory[i + 1]);
Caleb Connollye83b1cd2024-08-09 01:59:25 +0200140 if (!prevbl_ddr_banks[j].size) {
141 j--;
142 continue;
143 }
144 ram_end = max(ram_end, prevbl_ddr_banks[j].start + prevbl_ddr_banks[j].size);
145 }
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000146
Sam Day72546b02025-01-23 12:12:14 +0000147 if (!banks || !prevbl_ddr_banks[0].size)
148 return -ENODATA;
149
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000150 /* Sort our RAM banks -_- */
Caleb Connollye83b1cd2024-08-09 01:59:25 +0200151 qsort(prevbl_ddr_banks, banks, sizeof(prevbl_ddr_banks[0]), ddr_bank_cmp);
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000152
Caleb Connollye83b1cd2024-08-09 01:59:25 +0200153 gd->ram_base = prevbl_ddr_banks[0].start;
154 gd->ram_size = ram_end - gd->ram_base;
Sam Day72546b02025-01-23 12:12:14 +0000155
156 return 0;
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000157}
158
159static void show_psci_version(void)
160{
161 struct arm_smccc_res res;
162
163 arm_smccc_smc(ARM_PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0, 0, 0, 0, 0, &res);
164
165 debug("PSCI: v%ld.%ld\n",
166 PSCI_VERSION_MAJOR(res.a0),
167 PSCI_VERSION_MINOR(res.a0));
168}
169
Caleb Connollyc0cd2942024-08-09 01:59:24 +0200170/* We support booting U-Boot with an internal DT when running as a first-stage bootloader
171 * or for supporting quirky devices where it's easier to leave the downstream DT in place
172 * to improve ABL compatibility. Otherwise, we use the DT provided by ABL.
173 */
Simon Glass94086b22024-11-02 11:49:42 -0600174int board_fdt_blob_setup(void **fdtp)
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000175{
Sam Day72546b02025-01-23 12:12:14 +0000176 struct fdt_header *external_fdt, *internal_fdt;
Caleb Connollyc0cd2942024-08-09 01:59:24 +0200177 bool internal_valid, external_valid;
Sam Day72546b02025-01-23 12:12:14 +0000178 int ret = -ENODATA;
Caleb Connollyc0cd2942024-08-09 01:59:24 +0200179
Sam Day72546b02025-01-23 12:12:14 +0000180 internal_fdt = (struct fdt_header *)*fdtp;
181 external_fdt = (struct fdt_header *)get_prev_bl_fdt_addr();
182 external_valid = external_fdt && !fdt_check_header(external_fdt);
183 internal_valid = !fdt_check_header(internal_fdt);
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000184
185 /*
Caleb Connollyc0cd2942024-08-09 01:59:24 +0200186 * There is no point returning an error here, U-Boot can't do anything useful in this situation.
187 * Bail out while we can still print a useful error message.
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000188 */
Caleb Connollyc0cd2942024-08-09 01:59:24 +0200189 if (!internal_valid && !external_valid)
190 panic("Internal FDT is invalid and no external FDT was provided! (fdt=%#llx)\n",
Sam Day72546b02025-01-23 12:12:14 +0000191 (phys_addr_t)external_fdt);
192
193 /* Prefer memory information from internal DT if it's present */
194 if (internal_valid)
195 ret = qcom_parse_memory(internal_fdt);
196
197 if (ret < 0 && external_valid) {
198 /* No internal FDT or it lacks a proper /memory node.
199 * The previous bootloader handed us something, let's try that.
200 */
201 if (internal_valid)
202 debug("No memory info in internal FDT, falling back to external\n");
203
204 ret = qcom_parse_memory(external_fdt);
205 }
206
207 if (ret < 0)
208 panic("No valid memory ranges found!\n");
209
210 debug("ram_base = %#011lx, ram_size = %#011llx\n",
211 gd->ram_base, gd->ram_size);
Volodymyr Babchukbdacfea2024-03-11 21:33:45 +0000212
Caleb Connollyc0cd2942024-08-09 01:59:24 +0200213 if (internal_valid) {
214 debug("Using built in FDT\n");
Sam Day72546b02025-01-23 12:12:14 +0000215 return -EEXIST;
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000216 }
Caleb Connollye83b1cd2024-08-09 01:59:25 +0200217
Sam Day72546b02025-01-23 12:12:14 +0000218 debug("Using external FDT\n");
219 *fdtp = external_fdt;
220 return 0;
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000221}
222
223void reset_cpu(void)
224{
225 psci_system_reset();
226}
227
228/*
229 * Some Qualcomm boards require GPIO configuration when switching USB modes.
230 * Support setting this configuration via pinctrl state.
231 */
232int board_usb_init(int index, enum usb_init_type init)
233{
234 struct udevice *usb;
235 int ret = 0;
236
237 /* USB device */
238 ret = uclass_find_device_by_seq(UCLASS_USB, index, &usb);
239 if (ret) {
240 printf("Cannot find USB device\n");
241 return ret;
242 }
243
244 ret = dev_read_stringlist_search(usb, "pinctrl-names",
245 "device");
246 /* No "device" pinctrl state, so just bail */
247 if (ret < 0)
248 return 0;
249
250 /* Select "default" or "device" pinctrl */
251 switch (init) {
252 case USB_INIT_HOST:
253 pinctrl_select_state(usb, "default");
254 break;
255 case USB_INIT_DEVICE:
256 pinctrl_select_state(usb, "device");
257 break;
258 default:
259 debug("Unknown usb_init_type %d\n", init);
260 break;
261 }
262
263 return 0;
264}
265
266/*
267 * Some boards still need board specific init code, they can implement that by
268 * overriding this function.
269 *
270 * FIXME: get rid of board specific init code
271 */
272void __weak qcom_board_init(void)
273{
274}
275
276int board_init(void)
277{
278 show_psci_version();
Caleb Connolly2983ae82024-04-03 14:07:45 +0200279 qcom_of_fixup_nodes();
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000280 qcom_board_init();
281 return 0;
282}
283
Caleb Connolly750a0fa2024-08-09 01:59:27 +0200284/**
285 * out_len includes the trailing null space
286 */
287static int get_cmdline_option(const char *cmdline, const char *key, char *out, int out_len)
288{
289 const char *p, *p_end;
290 int len;
291
292 p = strstr(cmdline, key);
293 if (!p)
294 return -ENOENT;
295
296 p += strlen(key);
297 p_end = strstr(p, " ");
298 if (!p_end)
299 return -ENOENT;
300
301 len = p_end - p;
302 if (len > out_len)
303 len = out_len;
304
305 strncpy(out, p, len);
306 out[len] = '\0';
307
308 return 0;
309}
310
311/* The bootargs are populated by the previous stage bootloader */
312static const char *get_cmdline(void)
313{
314 ofnode node;
315 static const char *cmdline = NULL;
316
317 if (cmdline)
318 return cmdline;
319
320 node = ofnode_path("/chosen");
321 if (!ofnode_valid(node))
322 return NULL;
323
324 cmdline = ofnode_read_string(node, "bootargs");
325
326 return cmdline;
327}
328
329void qcom_set_serialno(void)
330{
331 const char *cmdline = get_cmdline();
332 char serial[32];
333
334 if (!cmdline) {
335 log_debug("Failed to get bootargs\n");
336 return;
337 }
338
339 get_cmdline_option(cmdline, "androidboot.serialno=", serial, sizeof(serial));
340 if (serial[0] != '\0')
341 env_set("serial#", serial);
342}
343
Caleb Connollya0156902024-02-26 17:26:26 +0000344/* Sets up the "board", and "soc" environment variables as well as constructing the devicetree
345 * path, with a few quirks to handle non-standard dtb filenames. This is not meant to be a
346 * comprehensive solution to automatically picking the DTB, but aims to be correct for the
347 * majority case. For most devices it should be possible to make this algorithm work by
348 * adjusting the root compatible property in the U-Boot DTS. Handling devices with multiple
349 * variants that are all supported by a single U-Boot image will require implementing device-
350 * specific detection.
351 */
352static void configure_env(void)
353{
354 const char *first_compat, *last_compat;
355 char *tmp;
356 char buf[32] = { 0 };
357 /*
358 * Most DTB filenames follow the scheme: qcom/<soc>-[vendor]-<board>.dtb
359 * The vendor is skipped when it's a Qualcomm reference board, or the
360 * db845c.
361 */
362 char dt_path[64] = { 0 };
363 int compat_count, ret;
364 ofnode root;
365
366 root = ofnode_root();
367 /* This is almost always 2, but be explicit that we want the first and last compatibles
368 * not the first and second.
369 */
370 compat_count = ofnode_read_string_count(root, "compatible");
371 if (compat_count < 2) {
372 log_warning("%s: only one root compatible bailing!\n", __func__);
373 return;
374 }
375
376 /* The most specific device compatible (e.g. "thundercomm,db845c") */
377 ret = ofnode_read_string_index(root, "compatible", 0, &first_compat);
378 if (ret < 0) {
379 log_warning("Can't read first compatible\n");
380 return;
381 }
382
383 /* The last compatible is always the SoC compatible */
384 ret = ofnode_read_string_index(root, "compatible", compat_count - 1, &last_compat);
385 if (ret < 0) {
386 log_warning("Can't read second compatible\n");
387 return;
388 }
389
390 /* Copy the second compat (e.g. "qcom,sdm845") into buf */
391 strlcpy(buf, last_compat, sizeof(buf) - 1);
392 tmp = buf;
393
394 /* strsep() is destructive, it replaces the comma with a \0 */
395 if (!strsep(&tmp, ",")) {
396 log_warning("second compatible '%s' has no ','\n", buf);
397 return;
398 }
399
400 /* tmp now points to just the "sdm845" part of the string */
401 env_set("soc", tmp);
402
403 /* Now figure out the "board" part from the first compatible */
404 memset(buf, 0, sizeof(buf));
405 strlcpy(buf, first_compat, sizeof(buf) - 1);
406 tmp = buf;
407
408 /* The Qualcomm reference boards (RBx, HDK, etc) */
409 if (!strncmp("qcom", buf, strlen("qcom"))) {
410 /*
411 * They all have the first compatible as "qcom,<soc>-<board>"
412 * (e.g. "qcom,qrb5165-rb5"). We extract just the part after
413 * the dash.
414 */
415 if (!strsep(&tmp, "-")) {
416 log_warning("compatible '%s' has no '-'\n", buf);
417 return;
418 }
419 /* tmp is now "rb5" */
420 env_set("board", tmp);
421 } else {
422 if (!strsep(&tmp, ",")) {
423 log_warning("compatible '%s' has no ','\n", buf);
424 return;
425 }
426 /* for thundercomm we just want the bit after the comma (e.g. "db845c"),
427 * for all other boards we replace the comma with a '-' and take both
428 * (e.g. "oneplus-enchilada")
429 */
430 if (!strncmp("thundercomm", buf, strlen("thundercomm"))) {
431 env_set("board", tmp);
432 } else {
433 *(tmp - 1) = '-';
434 env_set("board", buf);
435 }
436 }
437
438 /* Now build the full path name */
439 snprintf(dt_path, sizeof(dt_path), "qcom/%s-%s.dtb",
440 env_get("soc"), env_get("board"));
441 env_set("fdtfile", dt_path);
Caleb Connolly750a0fa2024-08-09 01:59:27 +0200442
443 qcom_set_serialno();
Caleb Connollya0156902024-02-26 17:26:26 +0000444}
445
Caleb Connolly31385662024-02-26 17:26:25 +0000446void __weak qcom_late_init(void)
447{
448}
449
450#define KERNEL_COMP_SIZE SZ_64M
Caleb Connolly94a50842024-08-09 01:59:28 +0200451#ifdef CONFIG_FASTBOOT_BUF_SIZE
452#define FASTBOOT_BUF_SIZE CONFIG_FASTBOOT_BUF_SIZE
453#else
454#define FASTBOOT_BUF_SIZE 0
455#endif
Caleb Connolly31385662024-02-26 17:26:25 +0000456
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530457#define addr_alloc(size) lmb_alloc(size, SZ_2M)
Caleb Connolly31385662024-02-26 17:26:25 +0000458
459/* Stolen from arch/arm/mach-apple/board.c */
460int board_late_init(void)
461{
Caleb Connolly31385662024-02-26 17:26:25 +0000462 u32 status = 0;
Caleb Connolly39288732024-08-09 01:59:30 +0200463 phys_addr_t addr;
Caleb Connollyeee2b6f2024-08-09 01:59:29 +0200464 struct fdt_header *fdt_blob = (struct fdt_header *)gd->fdt_blob;
Caleb Connolly31385662024-02-26 17:26:25 +0000465
Caleb Connolly31385662024-02-26 17:26:25 +0000466 /* We need to be fairly conservative here as we support boards with just 1G of TOTAL RAM */
Caleb Connolly39288732024-08-09 01:59:30 +0200467 addr = addr_alloc(SZ_128M);
468 status |= env_set_hex("kernel_addr_r", addr);
469 status |= env_set_hex("loadaddr", addr);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530470 status |= env_set_hex("ramdisk_addr_r", addr_alloc(SZ_128M));
471 status |= env_set_hex("kernel_comp_addr_r", addr_alloc(KERNEL_COMP_SIZE));
Caleb Connolly31385662024-02-26 17:26:25 +0000472 status |= env_set_hex("kernel_comp_size", KERNEL_COMP_SIZE);
Caleb Connolly94a50842024-08-09 01:59:28 +0200473 if (IS_ENABLED(CONFIG_FASTBOOT))
474 status |= env_set_hex("fastboot_addr_r", addr_alloc(FASTBOOT_BUF_SIZE));
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530475 status |= env_set_hex("scriptaddr", addr_alloc(SZ_4M));
476 status |= env_set_hex("pxefile_addr_r", addr_alloc(SZ_4M));
Caleb Connolly39288732024-08-09 01:59:30 +0200477 addr = addr_alloc(SZ_2M);
478 status |= env_set_hex("fdt_addr_r", addr);
Caleb Connolly31385662024-02-26 17:26:25 +0000479
480 if (status)
481 log_warning("%s: Failed to set run time variables\n", __func__);
482
Caleb Connollyeee2b6f2024-08-09 01:59:29 +0200483 /* By default copy U-Boots FDT, it will be used as a fallback */
Caleb Connolly39288732024-08-09 01:59:30 +0200484 memcpy((void *)addr, (void *)gd->fdt_blob, fdt32_to_cpu(fdt_blob->totalsize));
Caleb Connollyeee2b6f2024-08-09 01:59:29 +0200485
Caleb Connollya0156902024-02-26 17:26:26 +0000486 configure_env();
Caleb Connolly31385662024-02-26 17:26:25 +0000487 qcom_late_init();
488
Caleb Connolly2bdb2522024-10-12 15:57:19 +0200489 /* Configure the dfu_string for capsule updates */
490 qcom_configure_capsule_updates();
491
Caleb Connolly31385662024-02-26 17:26:25 +0000492 return 0;
493}
494
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000495static void build_mem_map(void)
496{
Caleb Connolly81982462024-02-26 17:26:27 +0000497 int i, j;
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000498
499 /*
500 * Ensure the peripheral block is sized to correctly cover the address range
501 * up to the first memory bank.
502 * Don't map the first page to ensure that we actually trigger an abort on a
503 * null pointer access rather than just hanging.
504 * FIXME: we should probably split this into more precise regions
505 */
506 mem_map[0].phys = 0x1000;
507 mem_map[0].virt = mem_map[0].phys;
508 mem_map[0].size = gd->bd->bi_dram[0].start - mem_map[0].phys;
509 mem_map[0].attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
510 PTE_BLOCK_NON_SHARE |
511 PTE_BLOCK_PXN | PTE_BLOCK_UXN;
512
Caleb Connolly81982462024-02-26 17:26:27 +0000513 for (i = 1, j = 0; i < ARRAY_SIZE(rbx_mem_map) - 1 && gd->bd->bi_dram[j].size; i++, j++) {
514 mem_map[i].phys = gd->bd->bi_dram[j].start;
515 mem_map[i].virt = mem_map[i].phys;
516 mem_map[i].size = gd->bd->bi_dram[j].size;
517 mem_map[i].attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | \
518 PTE_BLOCK_INNER_SHARE;
519 }
520
521 mem_map[i].phys = UINT64_MAX;
522 mem_map[i].size = 0;
523
524#ifdef DEBUG
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000525 debug("Configured memory map:\n");
Caleb Connolly81982462024-02-26 17:26:27 +0000526 for (i = 0; mem_map[i].size; i++)
527 debug(" 0x%016llx - 0x%016llx: entry %d\n",
528 mem_map[i].phys, mem_map[i].phys + mem_map[i].size, i);
529#endif
530}
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000531
Caleb Connolly81982462024-02-26 17:26:27 +0000532u64 get_page_table_size(void)
533{
Neil Armstrong4086a1f2024-08-09 01:59:26 +0200534 return SZ_1M;
Caleb Connolly81982462024-02-26 17:26:27 +0000535}
536
537static int fdt_cmp_res(const void *v1, const void *v2)
538{
539 const struct fdt_resource *res1 = v1, *res2 = v2;
540
541 return res1->start - res2->start;
542}
543
544#define N_RESERVED_REGIONS 32
545
546/* Mark all no-map regions as PTE_TYPE_FAULT to prevent speculative access.
547 * On some platforms this is enough to trigger a security violation and trap
548 * to EL3.
549 */
550static void carve_out_reserved_memory(void)
551{
552 static struct fdt_resource res[N_RESERVED_REGIONS] = { 0 };
553 int parent, rmem, count, i = 0;
554 phys_addr_t start;
555 size_t size;
556
557 /* Some reserved nodes must be carved out, as the cache-prefetcher may otherwise
558 * attempt to access them, causing a security exception.
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000559 */
Caleb Connolly81982462024-02-26 17:26:27 +0000560 parent = fdt_path_offset(gd->fdt_blob, "/reserved-memory");
561 if (parent <= 0) {
562 log_err("No reserved memory regions found\n");
563 return;
564 }
565
566 /* Collect the reserved memory regions */
567 fdt_for_each_subnode(rmem, gd->fdt_blob, parent) {
568 const fdt32_t *ptr;
569 int len;
570 if (!fdt_getprop(gd->fdt_blob, rmem, "no-map", NULL))
571 continue;
572
573 if (i == N_RESERVED_REGIONS) {
574 log_err("Too many reserved regions!\n");
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000575 break;
576 }
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000577
Caleb Connolly81982462024-02-26 17:26:27 +0000578 /* Read the address and size out from the reg property. Doing this "properly" with
579 * fdt_get_resource() takes ~70ms on SDM845, but open-coding the happy path here
580 * takes <1ms... Oh the woes of no dcache.
581 */
582 ptr = fdt_getprop(gd->fdt_blob, rmem, "reg", &len);
583 if (ptr) {
584 /* Qualcomm devices use #address/size-cells = <2> but all reserved regions are within
585 * the 32-bit address space. So we can cheat here for speed.
586 */
587 res[i].start = fdt32_to_cpu(ptr[1]);
588 res[i].end = res[i].start + fdt32_to_cpu(ptr[3]);
589 i++;
590 }
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000591 }
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000592
Caleb Connolly81982462024-02-26 17:26:27 +0000593 /* Sort the reserved memory regions by address */
594 count = i;
595 qsort(res, count, sizeof(struct fdt_resource), fdt_cmp_res);
596
597 /* Now set the right attributes for them. Often a lot of the regions are tightly packed together
598 * so we can optimise the number of calls to mmu_change_region_attr() by combining adjacent
599 * regions.
600 */
601 start = ALIGN_DOWN(res[0].start, SZ_2M);
602 size = ALIGN(res[0].end - start, SZ_2M);
603 for (i = 1; i <= count; i++) {
604 /* We ideally want to 2M align everything for more efficient pagetables, but we must avoid
605 * overwriting reserved memory regions which shouldn't be mapped as FAULT (like those with
606 * compatible properties).
607 * If within 2M of the previous region, bump the size to include this region. Otherwise
608 * start a new region.
609 */
610 if (i == count || start + size < res[i].start - SZ_2M) {
611 debug(" 0x%016llx - 0x%016llx: reserved\n",
612 start, start + size);
613 mmu_change_region_attr(start, size, PTE_TYPE_FAULT);
614 /* If this is the final region then quit here before we index
615 * out of bounds...
616 */
617 if (i == count)
618 break;
619 start = ALIGN_DOWN(res[i].start, SZ_2M);
620 size = ALIGN(res[i].end - start, SZ_2M);
621 } else {
622 /* Bump size if this region is immediately after the previous one */
623 size = ALIGN(res[i].end - start, SZ_2M);
624 }
625 }
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000626}
627
Caleb Connolly81982462024-02-26 17:26:27 +0000628/* This function open-codes setup_all_pgtables() so that we can
629 * insert additional mappings *before* turning on the MMU.
630 */
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000631void enable_caches(void)
632{
Caleb Connolly81982462024-02-26 17:26:27 +0000633 u64 tlb_addr = gd->arch.tlb_addr;
634 u64 tlb_size = gd->arch.tlb_size;
635 u64 pt_size;
636 ulong carveout_start;
637
638 gd->arch.tlb_fillptr = tlb_addr;
639
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000640 build_mem_map();
641
642 icache_enable();
Caleb Connolly81982462024-02-26 17:26:27 +0000643
644 /* Create normal system page tables */
645 setup_pgtables();
646
647 pt_size = (uintptr_t)gd->arch.tlb_fillptr -
648 (uintptr_t)gd->arch.tlb_addr;
649 debug("Primary pagetable size: %lluKiB\n", pt_size / 1024);
650
651 /* Create emergency page tables */
652 gd->arch.tlb_size -= pt_size;
653 gd->arch.tlb_addr = gd->arch.tlb_fillptr;
654 setup_pgtables();
655 gd->arch.tlb_emerg = gd->arch.tlb_addr;
656 gd->arch.tlb_addr = tlb_addr;
657 gd->arch.tlb_size = tlb_size;
658
Sam Day46760ba2024-05-07 18:41:23 +0000659 /* We do the carveouts only for QCS404, for now. */
660 if (fdt_node_check_compatible(gd->fdt_blob, 0, "qcom,qcs404") == 0) {
661 carveout_start = get_timer(0);
662 /* Takes ~20-50ms on SDM845 */
663 carve_out_reserved_memory();
664 debug("carveout time: %lums\n", get_timer(carveout_start));
665 }
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000666 dcache_enable();
667}