Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (c) 2020 Western Digital Corporation or its affiliates |
| 4 | * |
| 5 | */ |
| 6 | |
Heinrich Schuchardt | 8cfbea0 | 2020-06-30 11:30:59 +0200 | [diff] [blame] | 7 | #define LOG_CATEGORY LOGC_ARCH |
| 8 | |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <fdt_support.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 12 | #include <mapmem.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 13 | #include <asm/global_data.h> |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 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 | */ |
| 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) { |
Heinrich Schuchardt | 8cfbea0 | 2020-06-30 11:30:59 +0200 | [diff] [blame] | 43 | log_debug("No reserved memory region found in source FDT\n"); |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 44 | return 0; |
| 45 | } |
| 46 | |
Bin Meng | 7a36bd8 | 2020-06-25 18:16:07 -0700 | [diff] [blame] | 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 | |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 59 | fdt_for_each_subnode(node, src, offset) { |
| 60 | name = fdt_get_name(src, node, NULL); |
| 61 | |
Atish Patra | 491734f | 2020-06-24 14:56:15 -0700 | [diff] [blame] | 62 | addr = fdtdec_get_addr_size_auto_parent(src, offset, node, |
| 63 | "reg", 0, &size, |
| 64 | false); |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 65 | if (addr == FDT_ADDR_T_NONE) { |
Heinrich Schuchardt | 8cfbea0 | 2020-06-30 11:30:59 +0200 | [diff] [blame] | 66 | log_debug("failed to read address/size for %s\n", name); |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 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, |
Etienne Carriere | 646f8c6 | 2020-09-10 10:49:59 +0200 | [diff] [blame] | 79 | &phandle, false); |
Atish Patra | f0947db | 2020-06-24 14:56:14 -0700 | [diff] [blame] | 80 | if (err < 0 && err != -FDT_ERR_EXISTS) { |
Heinrich Schuchardt | 8cfbea0 | 2020-06-30 11:30:59 +0200 | [diff] [blame] | 81 | log_err("failed to add reserved memory: %d\n", err); |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 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 | * |
Bin Meng | 77073f4 | 2020-06-25 18:16:06 -0700 | [diff] [blame] | 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. |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 103 | * |
| 104 | * Return: 0 on success or error otherwise. |
| 105 | */ |
| 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); |
Bin Meng | 77073f4 | 2020-06-25 18:16:06 -0700 | [diff] [blame] | 112 | |
| 113 | /* avoid the copy if we are using the same device tree */ |
| 114 | if (src_fdt_addr == fdt) |
| 115 | return 0; |
| 116 | |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 117 | err = riscv_fdt_copy_resv_mem_node(src_fdt_addr, fdt); |
| 118 | if (err < 0) |
| 119 | return err; |
| 120 | |
| 121 | return 0; |
| 122 | } |
Atish Patra | 7379192 | 2020-04-21 11:15:02 -0700 | [diff] [blame] | 123 | |
| 124 | #ifdef CONFIG_OF_BOARD_FIXUP |
| 125 | int board_fix_fdt(void *fdt) |
| 126 | { |
| 127 | int err; |
| 128 | |
| 129 | err = riscv_board_reserved_mem_fixup(fdt); |
| 130 | if (err < 0) { |
Heinrich Schuchardt | 8cfbea0 | 2020-06-30 11:30:59 +0200 | [diff] [blame] | 131 | log_err("failed to fixup DT for reserved memory: %d\n", err); |
Atish Patra | 7379192 | 2020-04-21 11:15:02 -0700 | [diff] [blame] | 132 | return err; |
| 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | #endif |
Atish Patra | 82ed8ef | 2020-04-21 11:15:04 -0700 | [diff] [blame] | 138 | |
| 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) { |
Heinrich Schuchardt | 8cfbea0 | 2020-06-30 11:30:59 +0200 | [diff] [blame] | 149 | log_err("Device Tree can't be expanded to accommodate new node"); |
Atish Patra | 82ed8ef | 2020-04-21 11:15:04 -0700 | [diff] [blame] | 150 | return err; |
| 151 | } |
| 152 | chosen_offset = fdt_path_offset(blob, "/chosen"); |
| 153 | if (chosen_offset < 0) { |
Sean Anderson | ac95f46 | 2021-05-14 22:36:16 -0400 | [diff] [blame] | 154 | chosen_offset = fdt_add_subnode(blob, 0, "chosen"); |
| 155 | if (chosen_offset < 0) { |
Heinrich Schuchardt | 8cfbea0 | 2020-06-30 11:30:59 +0200 | [diff] [blame] | 156 | log_err("chosen node cannot be added\n"); |
Sean Anderson | ac95f46 | 2021-05-14 22:36:16 -0400 | [diff] [blame] | 157 | return chosen_offset; |
Atish Patra | 82ed8ef | 2020-04-21 11:15:04 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | /* Overwrite the boot-hartid as U-Boot is the last stage BL */ |
Sean Anderson | ac95f46 | 2021-05-14 22:36:16 -0400 | [diff] [blame] | 161 | err = fdt_setprop_u32(blob, chosen_offset, "boot-hartid", |
| 162 | gd->arch.boot_hart); |
| 163 | if (err < 0) |
| 164 | return log_msg_ret("could not set boot-hartid", err); |
Atish Patra | 82ed8ef | 2020-04-21 11:15:04 -0700 | [diff] [blame] | 165 | #endif |
| 166 | |
| 167 | /* Copy the reserved-memory node to the DT used by OS */ |
| 168 | err = riscv_fdt_copy_resv_mem_node(gd->fdt_blob, blob); |
| 169 | if (err < 0) |
| 170 | return err; |
| 171 | |
| 172 | return 0; |
| 173 | } |