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; |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 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 Schuchardt | 8cfbea0 | 2020-06-30 11:30:59 +0200 | [diff] [blame] | 42 | log_debug("No reserved memory region found in source FDT\n"); |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 43 | return 0; |
| 44 | } |
| 45 | |
Bin Meng | 7a36bd8 | 2020-06-25 18:16:07 -0700 | [diff] [blame] | 46 | /* |
| 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 Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 58 | fdt_for_each_subnode(node, src, offset) { |
| 59 | name = fdt_get_name(src, node, NULL); |
| 60 | |
Atish Patra | 491734f | 2020-06-24 14:56:15 -0700 | [diff] [blame] | 61 | addr = fdtdec_get_addr_size_auto_parent(src, offset, node, |
| 62 | "reg", 0, &size, |
| 63 | false); |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 64 | if (addr == FDT_ADDR_T_NONE) { |
Heinrich Schuchardt | 8cfbea0 | 2020-06-30 11:30:59 +0200 | [diff] [blame] | 65 | log_debug("failed to read address/size for %s\n", name); |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 66 | 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 Reding | bedc439 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 78 | NULL, 0, &phandle, 0); |
Atish Patra | f0947db | 2020-06-24 14:56:14 -0700 | [diff] [blame] | 79 | if (err < 0 && err != -FDT_ERR_EXISTS) { |
Heinrich Schuchardt | 8cfbea0 | 2020-06-30 11:30:59 +0200 | [diff] [blame] | 80 | log_err("failed to add reserved memory: %d\n", err); |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 81 | return err; |
| 82 | } |
Samuel Holland | c7ad952 | 2021-09-12 11:05:47 -0500 | [diff] [blame] | 83 | if (fdt_getprop(src, node, "no-map", NULL)) { |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 84 | 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 Meng | 77073f4 | 2020-06-25 18:16:06 -0700 | [diff] [blame] | 97 | * 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 Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 100 | * |
| 101 | * Return: 0 on success or error otherwise. |
| 102 | */ |
| 103 | int 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 Meng | 77073f4 | 2020-06-25 18:16:06 -0700 | [diff] [blame] | 109 | |
| 110 | /* avoid the copy if we are using the same device tree */ |
| 111 | if (src_fdt_addr == fdt) |
| 112 | return 0; |
| 113 | |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 114 | err = riscv_fdt_copy_resv_mem_node(src_fdt_addr, fdt); |
| 115 | if (err < 0) |
| 116 | return err; |
| 117 | |
| 118 | return 0; |
| 119 | } |
Atish Patra | 7379192 | 2020-04-21 11:15:02 -0700 | [diff] [blame] | 120 | |
| 121 | #ifdef CONFIG_OF_BOARD_FIXUP |
| 122 | int board_fix_fdt(void *fdt) |
| 123 | { |
| 124 | int err; |
| 125 | |
| 126 | err = riscv_board_reserved_mem_fixup(fdt); |
| 127 | if (err < 0) { |
Heinrich Schuchardt | 8cfbea0 | 2020-06-30 11:30:59 +0200 | [diff] [blame] | 128 | log_err("failed to fixup DT for reserved memory: %d\n", err); |
Atish Patra | 7379192 | 2020-04-21 11:15:02 -0700 | [diff] [blame] | 129 | return err; |
| 130 | } |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | #endif |
Atish Patra | 82ed8ef | 2020-04-21 11:15:04 -0700 | [diff] [blame] | 135 | |
| 136 | int 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 Schuchardt | 8cfbea0 | 2020-06-30 11:30:59 +0200 | [diff] [blame] | 146 | log_err("Device Tree can't be expanded to accommodate new node"); |
Atish Patra | 82ed8ef | 2020-04-21 11:15:04 -0700 | [diff] [blame] | 147 | return err; |
| 148 | } |
| 149 | chosen_offset = fdt_path_offset(blob, "/chosen"); |
| 150 | if (chosen_offset < 0) { |
Sean Anderson | ac95f46 | 2021-05-14 22:36:16 -0400 | [diff] [blame] | 151 | chosen_offset = fdt_add_subnode(blob, 0, "chosen"); |
| 152 | if (chosen_offset < 0) { |
Heinrich Schuchardt | 8cfbea0 | 2020-06-30 11:30:59 +0200 | [diff] [blame] | 153 | log_err("chosen node cannot be added\n"); |
Sean Anderson | ac95f46 | 2021-05-14 22:36:16 -0400 | [diff] [blame] | 154 | return chosen_offset; |
Atish Patra | 82ed8ef | 2020-04-21 11:15:04 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | /* Overwrite the boot-hartid as U-Boot is the last stage BL */ |
Sean Anderson | ac95f46 | 2021-05-14 22:36:16 -0400 | [diff] [blame] | 158 | 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 Patra | 82ed8ef | 2020-04-21 11:15:04 -0700 | [diff] [blame] | 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 | } |