blob: d02062fd5bd69748b80f195674bc365e93be7f53 [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>
13
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;
33 bool nomap = true;
34 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,
Etienne Carriere646f8c62020-09-10 10:49:59 +020078 &phandle, false);
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 }
83 if (!fdt_getprop(src, node, "no-map", NULL))
84 nomap = false;
85 if (nomap) {
86 rmem_offset = fdt_node_offset_by_phandle(dst, phandle);
87 fdt_setprop_empty(dst, rmem_offset, "no-map");
88 }
89 }
90
91 return 0;
92}
93
94/**
95 * riscv_board_reserved_mem_fixup() - Fix up reserved memory node for a board
96 * @fdt: Pointer to the device tree in which reserved memory node needs to be
97 * added.
98 *
Bin Meng77073f42020-06-25 18:16:06 -070099 * In RISC-V, any board needs to copy the reserved memory node from the device
100 * tree provided by the firmware to the device tree used by U-Boot. This is a
101 * common function that individual board fixup functions can invoke.
Atish Patra111b8042020-04-21 11:15:01 -0700102 *
103 * Return: 0 on success or error otherwise.
104 */
105int riscv_board_reserved_mem_fixup(void *fdt)
106{
107 int err;
108 void *src_fdt_addr;
109
110 src_fdt_addr = map_sysmem(gd->arch.firmware_fdt_addr, 0);
Bin Meng77073f42020-06-25 18:16:06 -0700111
112 /* avoid the copy if we are using the same device tree */
113 if (src_fdt_addr == fdt)
114 return 0;
115
Atish Patra111b8042020-04-21 11:15:01 -0700116 err = riscv_fdt_copy_resv_mem_node(src_fdt_addr, fdt);
117 if (err < 0)
118 return err;
119
120 return 0;
121}
Atish Patra73791922020-04-21 11:15:02 -0700122
123#ifdef CONFIG_OF_BOARD_FIXUP
124int board_fix_fdt(void *fdt)
125{
126 int err;
127
128 err = riscv_board_reserved_mem_fixup(fdt);
129 if (err < 0) {
Heinrich Schuchardt8cfbea02020-06-30 11:30:59 +0200130 log_err("failed to fixup DT for reserved memory: %d\n", err);
Atish Patra73791922020-04-21 11:15:02 -0700131 return err;
132 }
133
134 return 0;
135}
136#endif
Atish Patra82ed8ef2020-04-21 11:15:04 -0700137
138int arch_fixup_fdt(void *blob)
139{
140 int err;
141#ifdef CONFIG_EFI_LOADER
142 u32 size;
143 int chosen_offset;
144
145 size = fdt_totalsize(blob);
146 err = fdt_open_into(blob, blob, size + 32);
147 if (err < 0) {
Heinrich Schuchardt8cfbea02020-06-30 11:30:59 +0200148 log_err("Device Tree can't be expanded to accommodate new node");
Atish Patra82ed8ef2020-04-21 11:15:04 -0700149 return err;
150 }
151 chosen_offset = fdt_path_offset(blob, "/chosen");
152 if (chosen_offset < 0) {
153 err = fdt_add_subnode(blob, 0, "chosen");
154 if (err < 0) {
Heinrich Schuchardt8cfbea02020-06-30 11:30:59 +0200155 log_err("chosen node cannot be added\n");
Atish Patra82ed8ef2020-04-21 11:15:04 -0700156 return err;
157 }
158 }
159 /* Overwrite the boot-hartid as U-Boot is the last stage BL */
160 fdt_setprop_u32(blob, chosen_offset, "boot-hartid", gd->arch.boot_hart);
161#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}