blob: 36c16e9be2ae402754344ef17e0de6048362a584 [file] [log] [blame]
Atish Patra111b8042020-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 Schuchardt8cfbea02020-06-30 11:30:59 +02007#define LOG_CATEGORY LOGC_ARCH
8
Atish Patra111b8042020-04-21 11:15:01 -07009#include <common.h>
10#include <fdt_support.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Atish Patra111b8042020-04-21 11:15:01 -070012#include <mapmem.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Atish Patra111b8042020-04-21 11:15:01 -070014
15DECLARE_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 */
27int 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;
Atish Patra111b8042020-04-21 11:15:01 -070034 char basename[32] = {0};
35 int bname_len;
36 int max_len = sizeof(basename);
37 const char *name;
38 char *temp;
39
40 offset = fdt_path_offset(src, "/reserved-memory");
41 if (offset < 0) {
Heinrich Schuchardt8cfbea02020-06-30 11:30:59 +020042 log_debug("No reserved memory region found in source FDT\n");
Atish Patra111b8042020-04-21 11:15:01 -070043 return 0;
44 }
45
Bin Meng7a36bd82020-06-25 18:16:07 -070046 /*
47 * Extend the FDT by the following estimated size:
48 *
49 * Each PMP memory region entry occupies 64 bytes.
50 * With 16 PMP memory regions we need 64 * 16 = 1024 bytes.
51 */
52 err = fdt_open_into(dst, dst, fdt_totalsize(dst) + 1024);
53 if (err < 0) {
54 printf("Device Tree can't be expanded to accommodate new node");
55 return err;
56 }
57
Atish Patra111b8042020-04-21 11:15:01 -070058 fdt_for_each_subnode(node, src, offset) {
59 name = fdt_get_name(src, node, NULL);
60
Atish Patra491734f2020-06-24 14:56:15 -070061 addr = fdtdec_get_addr_size_auto_parent(src, offset, node,
62 "reg", 0, &size,
63 false);
Atish Patra111b8042020-04-21 11:15:01 -070064 if (addr == FDT_ADDR_T_NONE) {
Heinrich Schuchardt8cfbea02020-06-30 11:30:59 +020065 log_debug("failed to read address/size for %s\n", name);
Atish Patra111b8042020-04-21 11:15:01 -070066 continue;
67 }
68 strncpy(basename, name, max_len);
69 temp = strchr(basename, '@');
70 if (temp) {
71 bname_len = strnlen(basename, max_len) - strnlen(temp,
72 max_len);
73 *(basename + bname_len) = '\0';
74 }
75 pmp_mem.start = addr;
76 pmp_mem.end = addr + size - 1;
77 err = fdtdec_add_reserved_memory(dst, basename, &pmp_mem,
Thierry Redingbedc4392021-09-03 15:16:21 +020078 NULL, 0, &phandle, 0);
Atish Patraf0947db2020-06-24 14:56:14 -070079 if (err < 0 && err != -FDT_ERR_EXISTS) {
Heinrich Schuchardt8cfbea02020-06-30 11:30:59 +020080 log_err("failed to add reserved memory: %d\n", err);
Atish Patra111b8042020-04-21 11:15:01 -070081 return err;
82 }
Samuel Hollandc7ad9522021-09-12 11:05:47 -050083 if (fdt_getprop(src, node, "no-map", NULL)) {
Atish Patra111b8042020-04-21 11:15:01 -070084 rmem_offset = fdt_node_offset_by_phandle(dst, phandle);
85 fdt_setprop_empty(dst, rmem_offset, "no-map");
86 }
87 }
88
89 return 0;
90}
91
92/**
93 * riscv_board_reserved_mem_fixup() - Fix up reserved memory node for a board
94 * @fdt: Pointer to the device tree in which reserved memory node needs to be
95 * added.
96 *
Bin Meng77073f42020-06-25 18:16:06 -070097 * In RISC-V, any board needs to copy the reserved memory node from the device
98 * tree provided by the firmware to the device tree used by U-Boot. This is a
99 * common function that individual board fixup functions can invoke.
Atish Patra111b8042020-04-21 11:15:01 -0700100 *
101 * Return: 0 on success or error otherwise.
102 */
103int riscv_board_reserved_mem_fixup(void *fdt)
104{
105 int err;
106 void *src_fdt_addr;
107
108 src_fdt_addr = map_sysmem(gd->arch.firmware_fdt_addr, 0);
Bin Meng77073f42020-06-25 18:16:06 -0700109
110 /* avoid the copy if we are using the same device tree */
111 if (src_fdt_addr == fdt)
112 return 0;
113
Atish Patra111b8042020-04-21 11:15:01 -0700114 err = riscv_fdt_copy_resv_mem_node(src_fdt_addr, fdt);
115 if (err < 0)
116 return err;
117
118 return 0;
119}
Atish Patra73791922020-04-21 11:15:02 -0700120
121#ifdef CONFIG_OF_BOARD_FIXUP
122int board_fix_fdt(void *fdt)
123{
124 int err;
125
126 err = riscv_board_reserved_mem_fixup(fdt);
127 if (err < 0) {
Heinrich Schuchardt8cfbea02020-06-30 11:30:59 +0200128 log_err("failed to fixup DT for reserved memory: %d\n", err);
Atish Patra73791922020-04-21 11:15:02 -0700129 return err;
130 }
131
132 return 0;
133}
134#endif
Atish Patra82ed8ef2020-04-21 11:15:04 -0700135
136int arch_fixup_fdt(void *blob)
137{
138 int err;
139#ifdef CONFIG_EFI_LOADER
140 u32 size;
141 int chosen_offset;
142
143 size = fdt_totalsize(blob);
144 err = fdt_open_into(blob, blob, size + 32);
145 if (err < 0) {
Heinrich Schuchardt8cfbea02020-06-30 11:30:59 +0200146 log_err("Device Tree can't be expanded to accommodate new node");
Atish Patra82ed8ef2020-04-21 11:15:04 -0700147 return err;
148 }
149 chosen_offset = fdt_path_offset(blob, "/chosen");
150 if (chosen_offset < 0) {
Sean Andersonac95f462021-05-14 22:36:16 -0400151 chosen_offset = fdt_add_subnode(blob, 0, "chosen");
152 if (chosen_offset < 0) {
Heinrich Schuchardt8cfbea02020-06-30 11:30:59 +0200153 log_err("chosen node cannot be added\n");
Sean Andersonac95f462021-05-14 22:36:16 -0400154 return chosen_offset;
Atish Patra82ed8ef2020-04-21 11:15:04 -0700155 }
156 }
157 /* Overwrite the boot-hartid as U-Boot is the last stage BL */
Sean Andersonac95f462021-05-14 22:36:16 -0400158 err = fdt_setprop_u32(blob, chosen_offset, "boot-hartid",
159 gd->arch.boot_hart);
160 if (err < 0)
161 return log_msg_ret("could not set boot-hartid", err);
Atish Patra82ed8ef2020-04-21 11:15:04 -0700162#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}