1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2020 Western Digital Corporation or its affiliates
4 *
5 */
6
7 #define LOG_CATEGORY LOGC_ARCH
8
9 #include <common.h>
10 #include <fdt_support.h>
11 #include <log.h>
12 #include <mapmem.h>
13 #include <asm/global_data.h>
14
15 DECLARE_GLOBAL_DATA_PTR;
16
17 /**
18 * riscv_fdt_copy_resv_mem_node() - Copy reserve memory node entry
19 * @src: Pointer to the source device tree from which reserved memory node
20 * needs to be copied.
21 * @dst: Pointer to the destination device tree to which reserved memory node
22 * needs to be copied.
23 *
24 * Return: 0 on success or if source doesn't have reserved memory node.
25 * Error if copy process failed.
26 */
riscv_fdt_copy_resv_mem_node(const void * src,void * dst)27 int riscv_fdt_copy_resv_mem_node(const void *src, void *dst)
28 {
29 u32 phandle;
30 struct fdt_memory pmp_mem;
31 fdt_addr_t addr;
32 fdt_size_t size;
33 int offset, node, err, rmem_offset;
34 bool nomap = true;
35 char basename[32] = {0};
36 int bname_len;
37 int max_len = sizeof(basename);
38 const char *name;
39 char *temp;
40
41 offset = fdt_path_offset(src, "/reserved-memory");
42 if (offset < 0) {
43 log_debug("No reserved memory region found in source FDT\n");
44 return 0;
45 }
46
47 /*
48 * Extend the FDT by the following estimated size:
49 *
50 * Each PMP memory region entry occupies 64 bytes.
51 * With 16 PMP memory regions we need 64 * 16 = 1024 bytes.
52 */
53 err = fdt_open_into(dst, dst, fdt_totalsize(dst) + 1024);
54 if (err < 0) {
55 printf("Device Tree can't be expanded to accommodate new node");
56 return err;
57 }
58
59 fdt_for_each_subnode(node, src, offset) {
60 name = fdt_get_name(src, node, NULL);
61
62 addr = fdtdec_get_addr_size_auto_parent(src, offset, node,
63 "reg", 0, &size,
64 false);
65 if (addr == FDT_ADDR_T_NONE) {
66 log_debug("failed to read address/size for %s\n", name);
67 continue;
68 }
69 strncpy(basename, name, max_len);
70 temp = strchr(basename, '@');
71 if (temp) {
72 bname_len = strnlen(basename, max_len) - strnlen(temp,
73 max_len);
74 *(basename + bname_len) = '\0';
75 }
76 pmp_mem.start = addr;
77 pmp_mem.end = addr + size - 1;
78 err = fdtdec_add_reserved_memory(dst, basename, &pmp_mem,
79 &phandle, false);
80 if (err < 0 && err != -FDT_ERR_EXISTS) {
81 log_err("failed to add reserved memory: %d\n", err);
82 return err;
83 }
84 if (!fdt_getprop(src, node, "no-map", NULL))
85 nomap = false;
86 if (nomap) {
87 rmem_offset = fdt_node_offset_by_phandle(dst, phandle);
88 fdt_setprop_empty(dst, rmem_offset, "no-map");
89 }
90 }
91
92 return 0;
93 }
94
95 /**
96 * riscv_board_reserved_mem_fixup() - Fix up reserved memory node for a board
97 * @fdt: Pointer to the device tree in which reserved memory node needs to be
98 * added.
99 *
100 * In RISC-V, any board needs to copy the reserved memory node from the device
101 * tree provided by the firmware to the device tree used by U-Boot. This is a
102 * common function that individual board fixup functions can invoke.
103 *
104 * Return: 0 on success or error otherwise.
105 */
riscv_board_reserved_mem_fixup(void * fdt)106 int riscv_board_reserved_mem_fixup(void *fdt)
107 {
108 int err;
109 void *src_fdt_addr;
110
111 src_fdt_addr = map_sysmem(gd->arch.firmware_fdt_addr, 0);
112
113 /* avoid the copy if we are using the same device tree */
114 if (src_fdt_addr == fdt)
115 return 0;
116
117 err = riscv_fdt_copy_resv_mem_node(src_fdt_addr, fdt);
118 if (err < 0)
119 return err;
120
121 return 0;
122 }
123
124 #ifdef CONFIG_OF_BOARD_FIXUP
board_fix_fdt(void * fdt)125 int board_fix_fdt(void *fdt)
126 {
127 int err;
128
129 err = riscv_board_reserved_mem_fixup(fdt);
130 if (err < 0) {
131 log_err("failed to fixup DT for reserved memory: %d\n", err);
132 return err;
133 }
134
135 return 0;
136 }
137 #endif
138
arch_fixup_fdt(void * blob)139 int arch_fixup_fdt(void *blob)
140 {
141 int err;
142 #ifdef CONFIG_EFI_LOADER
143 u32 size;
144 int chosen_offset;
145
146 size = fdt_totalsize(blob);
147 err = fdt_open_into(blob, blob, size + 32);
148 if (err < 0) {
149 log_err("Device Tree can't be expanded to accommodate new node");
150 return err;
151 }
152 chosen_offset = fdt_path_offset(blob, "/chosen");
153 if (chosen_offset < 0) {
154 err = fdt_add_subnode(blob, 0, "chosen");
155 if (err < 0) {
156 log_err("chosen node cannot be added\n");
157 return err;
158 }
159 }
160 /* Overwrite the boot-hartid as U-Boot is the last stage BL */
161 fdt_setprop_u32(blob, chosen_offset, "boot-hartid", gd->arch.boot_hart);
162 #endif
163
164 /* Copy the reserved-memory node to the DT used by OS */
165 err = riscv_fdt_copy_resv_mem_node(gd->fdt_blob, blob);
166 if (err < 0)
167 return err;
168
169 return 0;
170 }
171