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