Atish Patra | d4ea649 | 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 | c5a4442 | 2020-06-30 11:30:59 +0200 | [diff] [blame] | 7 | #define LOG_CATEGORY LOGC_ARCH |
| 8 | |
Atish Patra | d4ea649 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <fdt_support.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Atish Patra | d4ea649 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 12 | #include <mapmem.h> |
| 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; |
| 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 Schuchardt | c5a4442 | 2020-06-30 11:30:59 +0200 | [diff] [blame] | 42 | log_debug("No reserved memory region found in source FDT\n"); |
Atish Patra | d4ea649 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 43 | return 0; |
| 44 | } |
| 45 | |
Bin Meng | a8492e2 | 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 | d4ea649 | 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 | edf4fc2 | 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 | d4ea649 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 64 | if (addr == FDT_ADDR_T_NONE) { |
Heinrich Schuchardt | c5a4442 | 2020-06-30 11:30:59 +0200 | [diff] [blame] | 65 | log_debug("failed to read address/size for %s\n", name); |
Atish Patra | d4ea649 | 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, |
| 78 | &phandle); |
Atish Patra | 7eb4bcc | 2020-06-24 14:56:14 -0700 | [diff] [blame] | 79 | if (err < 0 && err != -FDT_ERR_EXISTS) { |
Heinrich Schuchardt | c5a4442 | 2020-06-30 11:30:59 +0200 | [diff] [blame] | 80 | log_err("failed to add reserved memory: %d\n", err); |
Atish Patra | d4ea649 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 81 | 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 Meng | c4f7c50 | 2020-06-25 18:16:06 -0700 | [diff] [blame] | 99 | * 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 Patra | d4ea649 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 102 | * |
| 103 | * Return: 0 on success or error otherwise. |
| 104 | */ |
| 105 | int 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 Meng | c4f7c50 | 2020-06-25 18:16:06 -0700 | [diff] [blame] | 111 | |
| 112 | /* avoid the copy if we are using the same device tree */ |
| 113 | if (src_fdt_addr == fdt) |
| 114 | return 0; |
| 115 | |
Atish Patra | d4ea649 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 116 | err = riscv_fdt_copy_resv_mem_node(src_fdt_addr, fdt); |
| 117 | if (err < 0) |
| 118 | return err; |
| 119 | |
| 120 | return 0; |
| 121 | } |
Atish Patra | 0cb2785 | 2020-04-21 11:15:02 -0700 | [diff] [blame] | 122 | |
| 123 | #ifdef CONFIG_OF_BOARD_FIXUP |
| 124 | int board_fix_fdt(void *fdt) |
| 125 | { |
| 126 | int err; |
| 127 | |
| 128 | err = riscv_board_reserved_mem_fixup(fdt); |
| 129 | if (err < 0) { |
Heinrich Schuchardt | c5a4442 | 2020-06-30 11:30:59 +0200 | [diff] [blame] | 130 | log_err("failed to fixup DT for reserved memory: %d\n", err); |
Atish Patra | 0cb2785 | 2020-04-21 11:15:02 -0700 | [diff] [blame] | 131 | return err; |
| 132 | } |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | #endif |
Atish Patra | 177c53f | 2020-04-21 11:15:04 -0700 | [diff] [blame] | 137 | |
| 138 | int 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 Schuchardt | c5a4442 | 2020-06-30 11:30:59 +0200 | [diff] [blame] | 148 | log_err("Device Tree can't be expanded to accommodate new node"); |
Atish Patra | 177c53f | 2020-04-21 11:15:04 -0700 | [diff] [blame] | 149 | 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 Schuchardt | c5a4442 | 2020-06-30 11:30:59 +0200 | [diff] [blame] | 155 | log_err("chosen node cannot be added\n"); |
Atish Patra | 177c53f | 2020-04-21 11:15:04 -0700 | [diff] [blame] | 156 | 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 | } |