blob: 75b9cf1a8a1b3383665eb2d4e6725efe59718de0 [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
Sam Daycc789db2025-01-27 14:48:55 +0000165 /* Some older SoCs like MSM8916 don't always support PSCI */
166 if ((int)res.a0 == PSCI_RET_NOT_SUPPORTED)
167 return;
168
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000169 debug("PSCI: v%ld.%ld\n",
170 PSCI_VERSION_MAJOR(res.a0),
171 PSCI_VERSION_MINOR(res.a0));
172}
173
Sam Daycc789db2025-01-27 14:48:55 +0000174/**
175 * Most MSM8916 devices in the wild shipped without PSCI support, but the
176 * upstream DTs pretend that PSCI exists. If that situation is detected here,
177 * the /psci node is deleted. This is done very early to ensure the PSCI
178 * firmware driver doesn't bind (which then binds a sysreset driver that won't
179 * work).
180 */
181static void qcom_psci_fixup(void *fdt)
182{
183 int offset, ret;
184 struct arm_smccc_res res;
185
186 arm_smccc_smc(ARM_PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0, 0, 0, 0, 0, &res);
187
188 if ((int)res.a0 != PSCI_RET_NOT_SUPPORTED)
189 return;
190
191 offset = fdt_path_offset(fdt, "/psci");
192 if (offset < 0)
193 return;
194
195 debug("Found /psci DT node on device with no PSCI. Deleting.\n");
196 ret = fdt_del_node(fdt, offset);
197 if (ret)
198 log_err("Failed to delete /psci node: %d\n", ret);
199}
200
Caleb Connollyc0cd2942024-08-09 01:59:24 +0200201/* We support booting U-Boot with an internal DT when running as a first-stage bootloader
202 * or for supporting quirky devices where it's easier to leave the downstream DT in place
203 * to improve ABL compatibility. Otherwise, we use the DT provided by ABL.
204 */
Simon Glass94086b22024-11-02 11:49:42 -0600205int board_fdt_blob_setup(void **fdtp)
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000206{
Sam Day72546b02025-01-23 12:12:14 +0000207 struct fdt_header *external_fdt, *internal_fdt;
Caleb Connollyc0cd2942024-08-09 01:59:24 +0200208 bool internal_valid, external_valid;
Sam Day72546b02025-01-23 12:12:14 +0000209 int ret = -ENODATA;
Caleb Connollyc0cd2942024-08-09 01:59:24 +0200210
Sam Day72546b02025-01-23 12:12:14 +0000211 internal_fdt = (struct fdt_header *)*fdtp;
212 external_fdt = (struct fdt_header *)get_prev_bl_fdt_addr();
213 external_valid = external_fdt && !fdt_check_header(external_fdt);
214 internal_valid = !fdt_check_header(internal_fdt);
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000215
216 /*
Caleb Connollyc0cd2942024-08-09 01:59:24 +0200217 * There is no point returning an error here, U-Boot can't do anything useful in this situation.
218 * Bail out while we can still print a useful error message.
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000219 */
Caleb Connollyc0cd2942024-08-09 01:59:24 +0200220 if (!internal_valid && !external_valid)
221 panic("Internal FDT is invalid and no external FDT was provided! (fdt=%#llx)\n",
Sam Day72546b02025-01-23 12:12:14 +0000222 (phys_addr_t)external_fdt);
223
224 /* Prefer memory information from internal DT if it's present */
225 if (internal_valid)
226 ret = qcom_parse_memory(internal_fdt);
227
228 if (ret < 0 && external_valid) {
229 /* No internal FDT or it lacks a proper /memory node.
230 * The previous bootloader handed us something, let's try that.
231 */
232 if (internal_valid)
233 debug("No memory info in internal FDT, falling back to external\n");
234
235 ret = qcom_parse_memory(external_fdt);
236 }
237
238 if (ret < 0)
239 panic("No valid memory ranges found!\n");
240
241 debug("ram_base = %#011lx, ram_size = %#011llx\n",
242 gd->ram_base, gd->ram_size);
Volodymyr Babchukbdacfea2024-03-11 21:33:45 +0000243
Caleb Connollyc0cd2942024-08-09 01:59:24 +0200244 if (internal_valid) {
245 debug("Using built in FDT\n");
Sam Daycc789db2025-01-27 14:48:55 +0000246 ret = -EEXIST;
247 } else {
248 debug("Using external FDT\n");
249 *fdtp = external_fdt;
250 ret = 0;
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000251 }
Caleb Connollye83b1cd2024-08-09 01:59:25 +0200252
Sam Daycc789db2025-01-27 14:48:55 +0000253 qcom_psci_fixup(*fdtp);
254
255 return ret;
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000256}
257
258void reset_cpu(void)
259{
260 psci_system_reset();
261}
262
263/*
264 * Some Qualcomm boards require GPIO configuration when switching USB modes.
265 * Support setting this configuration via pinctrl state.
266 */
267int board_usb_init(int index, enum usb_init_type init)
268{
269 struct udevice *usb;
270 int ret = 0;
271
272 /* USB device */
273 ret = uclass_find_device_by_seq(UCLASS_USB, index, &usb);
274 if (ret) {
275 printf("Cannot find USB device\n");
276 return ret;
277 }
278
279 ret = dev_read_stringlist_search(usb, "pinctrl-names",
280 "device");
281 /* No "device" pinctrl state, so just bail */
282 if (ret < 0)
283 return 0;
284
285 /* Select "default" or "device" pinctrl */
286 switch (init) {
287 case USB_INIT_HOST:
288 pinctrl_select_state(usb, "default");
289 break;
290 case USB_INIT_DEVICE:
291 pinctrl_select_state(usb, "device");
292 break;
293 default:
294 debug("Unknown usb_init_type %d\n", init);
295 break;
296 }
297
298 return 0;
299}
300
301/*
302 * Some boards still need board specific init code, they can implement that by
303 * overriding this function.
304 *
305 * FIXME: get rid of board specific init code
306 */
307void __weak qcom_board_init(void)
308{
309}
310
311int board_init(void)
312{
313 show_psci_version();
Caleb Connolly2983ae82024-04-03 14:07:45 +0200314 qcom_of_fixup_nodes();
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000315 qcom_board_init();
316 return 0;
317}
318
Caleb Connolly750a0fa2024-08-09 01:59:27 +0200319/**
320 * out_len includes the trailing null space
321 */
322static int get_cmdline_option(const char *cmdline, const char *key, char *out, int out_len)
323{
324 const char *p, *p_end;
325 int len;
326
327 p = strstr(cmdline, key);
328 if (!p)
329 return -ENOENT;
330
331 p += strlen(key);
332 p_end = strstr(p, " ");
333 if (!p_end)
334 return -ENOENT;
335
336 len = p_end - p;
337 if (len > out_len)
338 len = out_len;
339
340 strncpy(out, p, len);
341 out[len] = '\0';
342
343 return 0;
344}
345
346/* The bootargs are populated by the previous stage bootloader */
347static const char *get_cmdline(void)
348{
349 ofnode node;
350 static const char *cmdline = NULL;
351
352 if (cmdline)
353 return cmdline;
354
355 node = ofnode_path("/chosen");
356 if (!ofnode_valid(node))
357 return NULL;
358
359 cmdline = ofnode_read_string(node, "bootargs");
360
361 return cmdline;
362}
363
364void qcom_set_serialno(void)
365{
366 const char *cmdline = get_cmdline();
367 char serial[32];
368
369 if (!cmdline) {
370 log_debug("Failed to get bootargs\n");
371 return;
372 }
373
374 get_cmdline_option(cmdline, "androidboot.serialno=", serial, sizeof(serial));
375 if (serial[0] != '\0')
376 env_set("serial#", serial);
377}
378
Caleb Connollya0156902024-02-26 17:26:26 +0000379/* Sets up the "board", and "soc" environment variables as well as constructing the devicetree
380 * path, with a few quirks to handle non-standard dtb filenames. This is not meant to be a
381 * comprehensive solution to automatically picking the DTB, but aims to be correct for the
382 * majority case. For most devices it should be possible to make this algorithm work by
383 * adjusting the root compatible property in the U-Boot DTS. Handling devices with multiple
384 * variants that are all supported by a single U-Boot image will require implementing device-
385 * specific detection.
386 */
387static void configure_env(void)
388{
389 const char *first_compat, *last_compat;
390 char *tmp;
391 char buf[32] = { 0 };
392 /*
393 * Most DTB filenames follow the scheme: qcom/<soc>-[vendor]-<board>.dtb
394 * The vendor is skipped when it's a Qualcomm reference board, or the
395 * db845c.
396 */
397 char dt_path[64] = { 0 };
398 int compat_count, ret;
399 ofnode root;
400
401 root = ofnode_root();
402 /* This is almost always 2, but be explicit that we want the first and last compatibles
403 * not the first and second.
404 */
405 compat_count = ofnode_read_string_count(root, "compatible");
406 if (compat_count < 2) {
407 log_warning("%s: only one root compatible bailing!\n", __func__);
408 return;
409 }
410
411 /* The most specific device compatible (e.g. "thundercomm,db845c") */
412 ret = ofnode_read_string_index(root, "compatible", 0, &first_compat);
413 if (ret < 0) {
414 log_warning("Can't read first compatible\n");
415 return;
416 }
417
418 /* The last compatible is always the SoC compatible */
419 ret = ofnode_read_string_index(root, "compatible", compat_count - 1, &last_compat);
420 if (ret < 0) {
421 log_warning("Can't read second compatible\n");
422 return;
423 }
424
425 /* Copy the second compat (e.g. "qcom,sdm845") into buf */
426 strlcpy(buf, last_compat, sizeof(buf) - 1);
427 tmp = buf;
428
429 /* strsep() is destructive, it replaces the comma with a \0 */
430 if (!strsep(&tmp, ",")) {
431 log_warning("second compatible '%s' has no ','\n", buf);
432 return;
433 }
434
435 /* tmp now points to just the "sdm845" part of the string */
436 env_set("soc", tmp);
437
438 /* Now figure out the "board" part from the first compatible */
439 memset(buf, 0, sizeof(buf));
440 strlcpy(buf, first_compat, sizeof(buf) - 1);
441 tmp = buf;
442
443 /* The Qualcomm reference boards (RBx, HDK, etc) */
444 if (!strncmp("qcom", buf, strlen("qcom"))) {
445 /*
446 * They all have the first compatible as "qcom,<soc>-<board>"
447 * (e.g. "qcom,qrb5165-rb5"). We extract just the part after
448 * the dash.
449 */
450 if (!strsep(&tmp, "-")) {
451 log_warning("compatible '%s' has no '-'\n", buf);
452 return;
453 }
454 /* tmp is now "rb5" */
455 env_set("board", tmp);
456 } else {
457 if (!strsep(&tmp, ",")) {
458 log_warning("compatible '%s' has no ','\n", buf);
459 return;
460 }
461 /* for thundercomm we just want the bit after the comma (e.g. "db845c"),
462 * for all other boards we replace the comma with a '-' and take both
463 * (e.g. "oneplus-enchilada")
464 */
465 if (!strncmp("thundercomm", buf, strlen("thundercomm"))) {
466 env_set("board", tmp);
467 } else {
468 *(tmp - 1) = '-';
469 env_set("board", buf);
470 }
471 }
472
473 /* Now build the full path name */
474 snprintf(dt_path, sizeof(dt_path), "qcom/%s-%s.dtb",
475 env_get("soc"), env_get("board"));
476 env_set("fdtfile", dt_path);
Caleb Connolly750a0fa2024-08-09 01:59:27 +0200477
478 qcom_set_serialno();
Caleb Connollya0156902024-02-26 17:26:26 +0000479}
480
Caleb Connolly31385662024-02-26 17:26:25 +0000481void __weak qcom_late_init(void)
482{
483}
484
485#define KERNEL_COMP_SIZE SZ_64M
Caleb Connolly94a50842024-08-09 01:59:28 +0200486#ifdef CONFIG_FASTBOOT_BUF_SIZE
487#define FASTBOOT_BUF_SIZE CONFIG_FASTBOOT_BUF_SIZE
488#else
489#define FASTBOOT_BUF_SIZE 0
490#endif
Caleb Connolly31385662024-02-26 17:26:25 +0000491
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530492#define addr_alloc(size) lmb_alloc(size, SZ_2M)
Caleb Connolly31385662024-02-26 17:26:25 +0000493
494/* Stolen from arch/arm/mach-apple/board.c */
495int board_late_init(void)
496{
Caleb Connolly31385662024-02-26 17:26:25 +0000497 u32 status = 0;
Caleb Connolly39288732024-08-09 01:59:30 +0200498 phys_addr_t addr;
Caleb Connollyeee2b6f2024-08-09 01:59:29 +0200499 struct fdt_header *fdt_blob = (struct fdt_header *)gd->fdt_blob;
Caleb Connolly31385662024-02-26 17:26:25 +0000500
Caleb Connolly31385662024-02-26 17:26:25 +0000501 /* We need to be fairly conservative here as we support boards with just 1G of TOTAL RAM */
Caleb Connolly39288732024-08-09 01:59:30 +0200502 addr = addr_alloc(SZ_128M);
503 status |= env_set_hex("kernel_addr_r", addr);
504 status |= env_set_hex("loadaddr", addr);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530505 status |= env_set_hex("ramdisk_addr_r", addr_alloc(SZ_128M));
506 status |= env_set_hex("kernel_comp_addr_r", addr_alloc(KERNEL_COMP_SIZE));
Caleb Connolly31385662024-02-26 17:26:25 +0000507 status |= env_set_hex("kernel_comp_size", KERNEL_COMP_SIZE);
Caleb Connolly94a50842024-08-09 01:59:28 +0200508 if (IS_ENABLED(CONFIG_FASTBOOT))
509 status |= env_set_hex("fastboot_addr_r", addr_alloc(FASTBOOT_BUF_SIZE));
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530510 status |= env_set_hex("scriptaddr", addr_alloc(SZ_4M));
511 status |= env_set_hex("pxefile_addr_r", addr_alloc(SZ_4M));
Caleb Connolly39288732024-08-09 01:59:30 +0200512 addr = addr_alloc(SZ_2M);
513 status |= env_set_hex("fdt_addr_r", addr);
Caleb Connolly31385662024-02-26 17:26:25 +0000514
515 if (status)
516 log_warning("%s: Failed to set run time variables\n", __func__);
517
Caleb Connollyeee2b6f2024-08-09 01:59:29 +0200518 /* By default copy U-Boots FDT, it will be used as a fallback */
Caleb Connolly39288732024-08-09 01:59:30 +0200519 memcpy((void *)addr, (void *)gd->fdt_blob, fdt32_to_cpu(fdt_blob->totalsize));
Caleb Connollyeee2b6f2024-08-09 01:59:29 +0200520
Caleb Connollya0156902024-02-26 17:26:26 +0000521 configure_env();
Caleb Connolly31385662024-02-26 17:26:25 +0000522 qcom_late_init();
523
Caleb Connolly2bdb2522024-10-12 15:57:19 +0200524 /* Configure the dfu_string for capsule updates */
525 qcom_configure_capsule_updates();
526
Caleb Connolly31385662024-02-26 17:26:25 +0000527 return 0;
528}
529
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000530static void build_mem_map(void)
531{
Caleb Connolly81982462024-02-26 17:26:27 +0000532 int i, j;
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000533
534 /*
535 * Ensure the peripheral block is sized to correctly cover the address range
536 * up to the first memory bank.
537 * Don't map the first page to ensure that we actually trigger an abort on a
538 * null pointer access rather than just hanging.
539 * FIXME: we should probably split this into more precise regions
540 */
541 mem_map[0].phys = 0x1000;
542 mem_map[0].virt = mem_map[0].phys;
543 mem_map[0].size = gd->bd->bi_dram[0].start - mem_map[0].phys;
544 mem_map[0].attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
545 PTE_BLOCK_NON_SHARE |
546 PTE_BLOCK_PXN | PTE_BLOCK_UXN;
547
Caleb Connolly81982462024-02-26 17:26:27 +0000548 for (i = 1, j = 0; i < ARRAY_SIZE(rbx_mem_map) - 1 && gd->bd->bi_dram[j].size; i++, j++) {
549 mem_map[i].phys = gd->bd->bi_dram[j].start;
550 mem_map[i].virt = mem_map[i].phys;
551 mem_map[i].size = gd->bd->bi_dram[j].size;
552 mem_map[i].attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | \
553 PTE_BLOCK_INNER_SHARE;
554 }
555
556 mem_map[i].phys = UINT64_MAX;
557 mem_map[i].size = 0;
558
559#ifdef DEBUG
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000560 debug("Configured memory map:\n");
Caleb Connolly81982462024-02-26 17:26:27 +0000561 for (i = 0; mem_map[i].size; i++)
562 debug(" 0x%016llx - 0x%016llx: entry %d\n",
563 mem_map[i].phys, mem_map[i].phys + mem_map[i].size, i);
564#endif
565}
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000566
Caleb Connolly81982462024-02-26 17:26:27 +0000567u64 get_page_table_size(void)
568{
Neil Armstrong4086a1f2024-08-09 01:59:26 +0200569 return SZ_1M;
Caleb Connolly81982462024-02-26 17:26:27 +0000570}
571
572static int fdt_cmp_res(const void *v1, const void *v2)
573{
574 const struct fdt_resource *res1 = v1, *res2 = v2;
575
576 return res1->start - res2->start;
577}
578
579#define N_RESERVED_REGIONS 32
580
581/* Mark all no-map regions as PTE_TYPE_FAULT to prevent speculative access.
582 * On some platforms this is enough to trigger a security violation and trap
583 * to EL3.
584 */
585static void carve_out_reserved_memory(void)
586{
587 static struct fdt_resource res[N_RESERVED_REGIONS] = { 0 };
588 int parent, rmem, count, i = 0;
589 phys_addr_t start;
590 size_t size;
591
592 /* Some reserved nodes must be carved out, as the cache-prefetcher may otherwise
593 * attempt to access them, causing a security exception.
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000594 */
Caleb Connolly81982462024-02-26 17:26:27 +0000595 parent = fdt_path_offset(gd->fdt_blob, "/reserved-memory");
596 if (parent <= 0) {
597 log_err("No reserved memory regions found\n");
598 return;
599 }
600
601 /* Collect the reserved memory regions */
602 fdt_for_each_subnode(rmem, gd->fdt_blob, parent) {
603 const fdt32_t *ptr;
604 int len;
605 if (!fdt_getprop(gd->fdt_blob, rmem, "no-map", NULL))
606 continue;
607
608 if (i == N_RESERVED_REGIONS) {
609 log_err("Too many reserved regions!\n");
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000610 break;
611 }
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000612
Caleb Connolly81982462024-02-26 17:26:27 +0000613 /* Read the address and size out from the reg property. Doing this "properly" with
614 * fdt_get_resource() takes ~70ms on SDM845, but open-coding the happy path here
615 * takes <1ms... Oh the woes of no dcache.
616 */
617 ptr = fdt_getprop(gd->fdt_blob, rmem, "reg", &len);
618 if (ptr) {
619 /* Qualcomm devices use #address/size-cells = <2> but all reserved regions are within
620 * the 32-bit address space. So we can cheat here for speed.
621 */
622 res[i].start = fdt32_to_cpu(ptr[1]);
623 res[i].end = res[i].start + fdt32_to_cpu(ptr[3]);
624 i++;
625 }
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000626 }
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000627
Caleb Connolly81982462024-02-26 17:26:27 +0000628 /* Sort the reserved memory regions by address */
629 count = i;
630 qsort(res, count, sizeof(struct fdt_resource), fdt_cmp_res);
631
632 /* Now set the right attributes for them. Often a lot of the regions are tightly packed together
633 * so we can optimise the number of calls to mmu_change_region_attr() by combining adjacent
634 * regions.
635 */
636 start = ALIGN_DOWN(res[0].start, SZ_2M);
637 size = ALIGN(res[0].end - start, SZ_2M);
638 for (i = 1; i <= count; i++) {
639 /* We ideally want to 2M align everything for more efficient pagetables, but we must avoid
640 * overwriting reserved memory regions which shouldn't be mapped as FAULT (like those with
641 * compatible properties).
642 * If within 2M of the previous region, bump the size to include this region. Otherwise
643 * start a new region.
644 */
645 if (i == count || start + size < res[i].start - SZ_2M) {
646 debug(" 0x%016llx - 0x%016llx: reserved\n",
647 start, start + size);
648 mmu_change_region_attr(start, size, PTE_TYPE_FAULT);
649 /* If this is the final region then quit here before we index
650 * out of bounds...
651 */
652 if (i == count)
653 break;
654 start = ALIGN_DOWN(res[i].start, SZ_2M);
655 size = ALIGN(res[i].end - start, SZ_2M);
656 } else {
657 /* Bump size if this region is immediately after the previous one */
658 size = ALIGN(res[i].end - start, SZ_2M);
659 }
660 }
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000661}
662
Caleb Connolly81982462024-02-26 17:26:27 +0000663/* This function open-codes setup_all_pgtables() so that we can
664 * insert additional mappings *before* turning on the MMU.
665 */
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000666void enable_caches(void)
667{
Caleb Connolly81982462024-02-26 17:26:27 +0000668 u64 tlb_addr = gd->arch.tlb_addr;
669 u64 tlb_size = gd->arch.tlb_size;
670 u64 pt_size;
671 ulong carveout_start;
672
673 gd->arch.tlb_fillptr = tlb_addr;
674
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000675 build_mem_map();
676
677 icache_enable();
Caleb Connolly81982462024-02-26 17:26:27 +0000678
679 /* Create normal system page tables */
680 setup_pgtables();
681
682 pt_size = (uintptr_t)gd->arch.tlb_fillptr -
683 (uintptr_t)gd->arch.tlb_addr;
684 debug("Primary pagetable size: %lluKiB\n", pt_size / 1024);
685
686 /* Create emergency page tables */
687 gd->arch.tlb_size -= pt_size;
688 gd->arch.tlb_addr = gd->arch.tlb_fillptr;
689 setup_pgtables();
690 gd->arch.tlb_emerg = gd->arch.tlb_addr;
691 gd->arch.tlb_addr = tlb_addr;
692 gd->arch.tlb_size = tlb_size;
693
Sam Day46760ba2024-05-07 18:41:23 +0000694 /* We do the carveouts only for QCS404, for now. */
695 if (fdt_node_check_compatible(gd->fdt_blob, 0, "qcom,qcs404") == 0) {
696 carveout_start = get_timer(0);
697 /* Takes ~20-50ms on SDM845 */
698 carve_out_reserved_memory();
699 debug("carveout time: %lums\n", get_timer(carveout_start));
700 }
Caleb Connollyfe1694c2024-02-26 17:26:24 +0000701 dcache_enable();
702}