blob: c658e72bd39e6c939745fad0be171c5d4f5416d9 [file] [log] [blame]
Atish Patrad4ea6492020-04-21 11:15:01 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2020 Western Digital Corporation or its affiliates
4 *
5 */
6
Heinrich Schuchardtc5a44422020-06-30 11:30:59 +02007#define LOG_CATEGORY LOGC_ARCH
8
Atish Patrad4ea6492020-04-21 11:15:01 -07009#include <fdt_support.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Atish Patrad4ea6492020-04-21 11:15:01 -070011#include <mapmem.h>
Simon Glass401d1c42020-10-30 21:38:53 -060012#include <asm/global_data.h>
Atish Patrad4ea6492020-04-21 11:15:01 -070013
14DECLARE_GLOBAL_DATA_PTR;
15
16/**
17 * riscv_fdt_copy_resv_mem_node() - Copy reserve memory node entry
18 * @src: Pointer to the source device tree from which reserved memory node
19 * needs to be copied.
20 * @dst: Pointer to the destination device tree to which reserved memory node
21 * needs to be copied.
22 *
23 * Return: 0 on success or if source doesn't have reserved memory node.
24 * Error if copy process failed.
25 */
26int riscv_fdt_copy_resv_mem_node(const void *src, void *dst)
27{
28 u32 phandle;
29 struct fdt_memory pmp_mem;
30 fdt_addr_t addr;
31 fdt_size_t size;
32 int offset, node, err, rmem_offset;
Atish Patrad4ea6492020-04-21 11:15:01 -070033 char basename[32] = {0};
34 int bname_len;
35 int max_len = sizeof(basename);
36 const char *name;
37 char *temp;
38
39 offset = fdt_path_offset(src, "/reserved-memory");
40 if (offset < 0) {
Heinrich Schuchardtc5a44422020-06-30 11:30:59 +020041 log_debug("No reserved memory region found in source FDT\n");
Atish Patrad4ea6492020-04-21 11:15:01 -070042 return 0;
43 }
44
Bin Menga8492e22020-06-25 18:16:07 -070045 /*
46 * Extend the FDT by the following estimated size:
47 *
48 * Each PMP memory region entry occupies 64 bytes.
49 * With 16 PMP memory regions we need 64 * 16 = 1024 bytes.
50 */
51 err = fdt_open_into(dst, dst, fdt_totalsize(dst) + 1024);
52 if (err < 0) {
53 printf("Device Tree can't be expanded to accommodate new node");
54 return err;
55 }
56
Atish Patrad4ea6492020-04-21 11:15:01 -070057 fdt_for_each_subnode(node, src, offset) {
58 name = fdt_get_name(src, node, NULL);
59
Atish Patraedf4fc22020-06-24 14:56:15 -070060 addr = fdtdec_get_addr_size_auto_parent(src, offset, node,
61 "reg", 0, &size,
62 false);
Atish Patrad4ea6492020-04-21 11:15:01 -070063 if (addr == FDT_ADDR_T_NONE) {
Heinrich Schuchardtc5a44422020-06-30 11:30:59 +020064 log_debug("failed to read address/size for %s\n", name);
Atish Patrad4ea6492020-04-21 11:15:01 -070065 continue;
66 }
67 strncpy(basename, name, max_len);
68 temp = strchr(basename, '@');
69 if (temp) {
70 bname_len = strnlen(basename, max_len) - strnlen(temp,
71 max_len);
72 *(basename + bname_len) = '\0';
73 }
74 pmp_mem.start = addr;
75 pmp_mem.end = addr + size - 1;
76 err = fdtdec_add_reserved_memory(dst, basename, &pmp_mem,
Thierry Redingb9aad372021-09-03 15:16:21 +020077 NULL, 0, &phandle, 0);
Atish Patra7eb4bcc2020-06-24 14:56:14 -070078 if (err < 0 && err != -FDT_ERR_EXISTS) {
Heinrich Schuchardtc5a44422020-06-30 11:30:59 +020079 log_err("failed to add reserved memory: %d\n", err);
Atish Patrad4ea6492020-04-21 11:15:01 -070080 return err;
81 }
Samuel Holland0d625f42021-09-12 11:05:47 -050082 if (fdt_getprop(src, node, "no-map", NULL)) {
Atish Patrad4ea6492020-04-21 11:15:01 -070083 rmem_offset = fdt_node_offset_by_phandle(dst, phandle);
84 fdt_setprop_empty(dst, rmem_offset, "no-map");
85 }
86 }
87
88 return 0;
89}
90
91/**
92 * riscv_board_reserved_mem_fixup() - Fix up reserved memory node for a board
93 * @fdt: Pointer to the device tree in which reserved memory node needs to be
94 * added.
95 *
Bin Mengc4f7c502020-06-25 18:16:06 -070096 * In RISC-V, any board needs to copy the reserved memory node from the device
97 * tree provided by the firmware to the device tree used by U-Boot. This is a
98 * common function that individual board fixup functions can invoke.
Atish Patrad4ea6492020-04-21 11:15:01 -070099 *
100 * Return: 0 on success or error otherwise.
101 */
102int riscv_board_reserved_mem_fixup(void *fdt)
103{
104 int err;
105 void *src_fdt_addr;
106
107 src_fdt_addr = map_sysmem(gd->arch.firmware_fdt_addr, 0);
Bin Mengc4f7c502020-06-25 18:16:06 -0700108
109 /* avoid the copy if we are using the same device tree */
110 if (src_fdt_addr == fdt)
111 return 0;
112
Atish Patrad4ea6492020-04-21 11:15:01 -0700113 err = riscv_fdt_copy_resv_mem_node(src_fdt_addr, fdt);
114 if (err < 0)
115 return err;
116
117 return 0;
118}
Atish Patra0cb27852020-04-21 11:15:02 -0700119
120#ifdef CONFIG_OF_BOARD_FIXUP
121int board_fix_fdt(void *fdt)
122{
123 int err;
124
125 err = riscv_board_reserved_mem_fixup(fdt);
126 if (err < 0) {
Heinrich Schuchardtc5a44422020-06-30 11:30:59 +0200127 log_err("failed to fixup DT for reserved memory: %d\n", err);
Atish Patra0cb27852020-04-21 11:15:02 -0700128 return err;
129 }
130
131 return 0;
132}
133#endif
Atish Patra177c53f2020-04-21 11:15:04 -0700134
135int arch_fixup_fdt(void *blob)
136{
137 int err;
138#ifdef CONFIG_EFI_LOADER
139 u32 size;
140 int chosen_offset;
141
142 size = fdt_totalsize(blob);
143 err = fdt_open_into(blob, blob, size + 32);
144 if (err < 0) {
Heinrich Schuchardtc5a44422020-06-30 11:30:59 +0200145 log_err("Device Tree can't be expanded to accommodate new node");
Atish Patra177c53f2020-04-21 11:15:04 -0700146 return err;
147 }
148 chosen_offset = fdt_path_offset(blob, "/chosen");
149 if (chosen_offset < 0) {
Sean Andersone90cb0d2021-05-14 22:36:16 -0400150 chosen_offset = fdt_add_subnode(blob, 0, "chosen");
151 if (chosen_offset < 0) {
Heinrich Schuchardtc5a44422020-06-30 11:30:59 +0200152 log_err("chosen node cannot be added\n");
Sean Andersone90cb0d2021-05-14 22:36:16 -0400153 return chosen_offset;
Atish Patra177c53f2020-04-21 11:15:04 -0700154 }
155 }
156 /* Overwrite the boot-hartid as U-Boot is the last stage BL */
Sean Andersone90cb0d2021-05-14 22:36:16 -0400157 err = fdt_setprop_u32(blob, chosen_offset, "boot-hartid",
158 gd->arch.boot_hart);
159 if (err < 0)
160 return log_msg_ret("could not set boot-hartid", err);
Atish Patra177c53f2020-04-21 11:15:04 -0700161#endif
162
163 /* Copy the reserved-memory node to the DT used by OS */
164 err = riscv_fdt_copy_resv_mem_node(gd->fdt_blob, blob);
165 if (err < 0)
166 return err;
167
168 return 0;
169}