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 <fdt_support.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 11 | #include <mapmem.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 13 | |
| 14 | DECLARE_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 | */ |
| 26 | int 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; |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 33 | char basename[32] = {0}; |
| 34 | int bname_len; |
| 35 | int max_len = sizeof(basename); |
| 36 | const char *name; |
| 37 | char *temp; |
| 38 | |
| 39 | offset = fdt_path_offset(src, "/reserved-memory"); |
| 40 | if (offset < 0) { |
Heinrich Schuchardt | 8cfbea0 | 2020-06-30 11:30:59 +0200 | [diff] [blame] | 41 | log_debug("No reserved memory region found in source FDT\n"); |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 42 | return 0; |
| 43 | } |
| 44 | |
Bin Meng | 7a36bd8 | 2020-06-25 18:16:07 -0700 | [diff] [blame] | 45 | /* |
| 46 | * Extend the FDT by the following estimated size: |
| 47 | * |
| 48 | * Each PMP memory region entry occupies 64 bytes. |
| 49 | * With 16 PMP memory regions we need 64 * 16 = 1024 bytes. |
| 50 | */ |
| 51 | err = fdt_open_into(dst, dst, fdt_totalsize(dst) + 1024); |
| 52 | if (err < 0) { |
| 53 | printf("Device Tree can't be expanded to accommodate new node"); |
| 54 | return err; |
| 55 | } |
| 56 | |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 57 | fdt_for_each_subnode(node, src, offset) { |
| 58 | name = fdt_get_name(src, node, NULL); |
| 59 | |
Atish Patra | 491734f | 2020-06-24 14:56:15 -0700 | [diff] [blame] | 60 | addr = fdtdec_get_addr_size_auto_parent(src, offset, node, |
| 61 | "reg", 0, &size, |
| 62 | false); |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 63 | if (addr == FDT_ADDR_T_NONE) { |
Heinrich Schuchardt | 8cfbea0 | 2020-06-30 11:30:59 +0200 | [diff] [blame] | 64 | log_debug("failed to read address/size for %s\n", name); |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 65 | continue; |
| 66 | } |
| 67 | strncpy(basename, name, max_len); |
| 68 | temp = strchr(basename, '@'); |
| 69 | if (temp) { |
| 70 | bname_len = strnlen(basename, max_len) - strnlen(temp, |
| 71 | max_len); |
| 72 | *(basename + bname_len) = '\0'; |
| 73 | } |
| 74 | pmp_mem.start = addr; |
| 75 | pmp_mem.end = addr + size - 1; |
| 76 | err = fdtdec_add_reserved_memory(dst, basename, &pmp_mem, |
Thierry Reding | bedc439 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 77 | NULL, 0, &phandle, 0); |
Atish Patra | f0947db | 2020-06-24 14:56:14 -0700 | [diff] [blame] | 78 | if (err < 0 && err != -FDT_ERR_EXISTS) { |
Heinrich Schuchardt | 8cfbea0 | 2020-06-30 11:30:59 +0200 | [diff] [blame] | 79 | log_err("failed to add reserved memory: %d\n", err); |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 80 | return err; |
| 81 | } |
Samuel Holland | c7ad952 | 2021-09-12 11:05:47 -0500 | [diff] [blame] | 82 | if (fdt_getprop(src, node, "no-map", NULL)) { |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 83 | rmem_offset = fdt_node_offset_by_phandle(dst, phandle); |
| 84 | fdt_setprop_empty(dst, rmem_offset, "no-map"); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * riscv_board_reserved_mem_fixup() - Fix up reserved memory node for a board |
| 93 | * @fdt: Pointer to the device tree in which reserved memory node needs to be |
| 94 | * added. |
| 95 | * |
Bin Meng | 77073f4 | 2020-06-25 18:16:06 -0700 | [diff] [blame] | 96 | * In RISC-V, any board needs to copy the reserved memory node from the device |
| 97 | * tree provided by the firmware to the device tree used by U-Boot. This is a |
| 98 | * common function that individual board fixup functions can invoke. |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 99 | * |
| 100 | * Return: 0 on success or error otherwise. |
| 101 | */ |
| 102 | int riscv_board_reserved_mem_fixup(void *fdt) |
| 103 | { |
| 104 | int err; |
| 105 | void *src_fdt_addr; |
| 106 | |
| 107 | src_fdt_addr = map_sysmem(gd->arch.firmware_fdt_addr, 0); |
Bin Meng | 77073f4 | 2020-06-25 18:16:06 -0700 | [diff] [blame] | 108 | |
| 109 | /* avoid the copy if we are using the same device tree */ |
| 110 | if (src_fdt_addr == fdt) |
| 111 | return 0; |
| 112 | |
Atish Patra | 111b804 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 113 | err = riscv_fdt_copy_resv_mem_node(src_fdt_addr, fdt); |
| 114 | if (err < 0) |
| 115 | return err; |
| 116 | |
| 117 | return 0; |
| 118 | } |
Atish Patra | 7379192 | 2020-04-21 11:15:02 -0700 | [diff] [blame] | 119 | |
| 120 | #ifdef CONFIG_OF_BOARD_FIXUP |
| 121 | int board_fix_fdt(void *fdt) |
| 122 | { |
| 123 | int err; |
| 124 | |
| 125 | err = riscv_board_reserved_mem_fixup(fdt); |
| 126 | if (err < 0) { |
Heinrich Schuchardt | 8cfbea0 | 2020-06-30 11:30:59 +0200 | [diff] [blame] | 127 | log_err("failed to fixup DT for reserved memory: %d\n", err); |
Atish Patra | 7379192 | 2020-04-21 11:15:02 -0700 | [diff] [blame] | 128 | return err; |
| 129 | } |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | #endif |
Atish Patra | 82ed8ef | 2020-04-21 11:15:04 -0700 | [diff] [blame] | 134 | |
| 135 | int arch_fixup_fdt(void *blob) |
| 136 | { |
| 137 | int err; |
| 138 | #ifdef CONFIG_EFI_LOADER |
| 139 | u32 size; |
| 140 | int chosen_offset; |
| 141 | |
| 142 | size = fdt_totalsize(blob); |
| 143 | err = fdt_open_into(blob, blob, size + 32); |
| 144 | if (err < 0) { |
Heinrich Schuchardt | 8cfbea0 | 2020-06-30 11:30:59 +0200 | [diff] [blame] | 145 | log_err("Device Tree can't be expanded to accommodate new node"); |
Atish Patra | 82ed8ef | 2020-04-21 11:15:04 -0700 | [diff] [blame] | 146 | return err; |
| 147 | } |
| 148 | chosen_offset = fdt_path_offset(blob, "/chosen"); |
| 149 | if (chosen_offset < 0) { |
Sean Anderson | ac95f46 | 2021-05-14 22:36:16 -0400 | [diff] [blame] | 150 | chosen_offset = fdt_add_subnode(blob, 0, "chosen"); |
| 151 | if (chosen_offset < 0) { |
Heinrich Schuchardt | 8cfbea0 | 2020-06-30 11:30:59 +0200 | [diff] [blame] | 152 | log_err("chosen node cannot be added\n"); |
Sean Anderson | ac95f46 | 2021-05-14 22:36:16 -0400 | [diff] [blame] | 153 | return chosen_offset; |
Atish Patra | 82ed8ef | 2020-04-21 11:15:04 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | /* Overwrite the boot-hartid as U-Boot is the last stage BL */ |
Sean Anderson | ac95f46 | 2021-05-14 22:36:16 -0400 | [diff] [blame] | 157 | err = fdt_setprop_u32(blob, chosen_offset, "boot-hartid", |
| 158 | gd->arch.boot_hart); |
| 159 | if (err < 0) |
| 160 | return log_msg_ret("could not set boot-hartid", err); |
Atish Patra | 82ed8ef | 2020-04-21 11:15:04 -0700 | [diff] [blame] | 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 | } |