Amit Nagal | 3a7d304 | 2023-07-10 10:32:15 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2023, Advanced Micro Devices, Inc. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
Akshay Belsare | e3f2a10 | 2023-07-19 10:07:50 +0530 | [diff] [blame] | 7 | #include <common/debug.h> |
Amit Nagal | 3a7d304 | 2023-07-10 10:32:15 +0530 | [diff] [blame] | 8 | #include <common/fdt_fixup.h> |
| 9 | #include <common/fdt_wrappers.h> |
Akshay Belsare | e3f2a10 | 2023-07-19 10:07:50 +0530 | [diff] [blame] | 10 | #include <libfdt.h> |
Amit Nagal | c1248e8 | 2023-09-04 21:53:59 -1200 | [diff] [blame] | 11 | #include <lib/xlat_tables/xlat_tables_v2.h> |
Amit Nagal | 3a7d304 | 2023-07-10 10:32:15 +0530 | [diff] [blame] | 12 | |
| 13 | #include <plat_fdt.h> |
| 14 | #include <platform_def.h> |
| 15 | |
Amit Nagal | 3a7d304 | 2023-07-10 10:32:15 +0530 | [diff] [blame] | 16 | void prepare_dtb(void) |
| 17 | { |
Amit Nagal | 66d3cda | 2023-09-27 12:28:35 +0530 | [diff] [blame] | 18 | #if defined(XILINX_OF_BOARD_DTB_ADDR) |
Amit Nagal | 659c6aa | 2023-08-29 02:44:59 -1200 | [diff] [blame] | 19 | void *dtb; |
Amit Nagal | 66d3cda | 2023-09-27 12:28:35 +0530 | [diff] [blame] | 20 | int map_ret = 0; |
| 21 | int ret = 0; |
| 22 | |
Amit Nagal | 659c6aa | 2023-08-29 02:44:59 -1200 | [diff] [blame] | 23 | dtb = (void *)XILINX_OF_BOARD_DTB_ADDR; |
Amit Nagal | 66d3cda | 2023-09-27 12:28:35 +0530 | [diff] [blame] | 24 | |
| 25 | if (!IS_TFA_IN_OCM(BL31_BASE)) { |
Amit Nagal | 3a7d304 | 2023-07-10 10:32:15 +0530 | [diff] [blame] | 26 | |
Amit Nagal | c1248e8 | 2023-09-04 21:53:59 -1200 | [diff] [blame] | 27 | #if defined(PLAT_XLAT_TABLES_DYNAMIC) |
Amit Nagal | 66d3cda | 2023-09-27 12:28:35 +0530 | [diff] [blame] | 28 | map_ret = mmap_add_dynamic_region((unsigned long long)dtb, |
| 29 | (uintptr_t)dtb, |
| 30 | XILINX_OF_BOARD_DTB_MAX_SIZE, |
| 31 | MT_MEMORY | MT_RW | MT_NS); |
| 32 | if (map_ret != 0) { |
| 33 | WARN("Failed to add dynamic region for dtb: error %d\n", |
| 34 | map_ret); |
| 35 | } |
Amit Nagal | c1248e8 | 2023-09-04 21:53:59 -1200 | [diff] [blame] | 36 | #endif |
| 37 | |
Amit Nagal | 66d3cda | 2023-09-27 12:28:35 +0530 | [diff] [blame] | 38 | if (!map_ret) { |
| 39 | /* Return if no device tree is detected */ |
| 40 | if (fdt_check_header(dtb) != 0) { |
| 41 | NOTICE("Can't read DT at %p\n", dtb); |
| 42 | } else { |
| 43 | ret = fdt_open_into(dtb, dtb, XILINX_OF_BOARD_DTB_MAX_SIZE); |
Amit Nagal | 3a7d304 | 2023-07-10 10:32:15 +0530 | [diff] [blame] | 44 | |
Amit Nagal | 66d3cda | 2023-09-27 12:28:35 +0530 | [diff] [blame] | 45 | if (ret < 0) { |
| 46 | ERROR("Invalid Device Tree at %p: error %d\n", |
| 47 | dtb, ret); |
| 48 | } else { |
Amit Nagal | 3a7d304 | 2023-07-10 10:32:15 +0530 | [diff] [blame] | 49 | |
Amit Nagal | 66d3cda | 2023-09-27 12:28:35 +0530 | [diff] [blame] | 50 | if (dt_add_psci_node(dtb)) { |
| 51 | WARN("Failed to add PSCI Device Tree node\n"); |
| 52 | } |
Amit Nagal | 3a7d304 | 2023-07-10 10:32:15 +0530 | [diff] [blame] | 53 | |
Amit Nagal | 66d3cda | 2023-09-27 12:28:35 +0530 | [diff] [blame] | 54 | if (dt_add_psci_cpu_enable_methods(dtb)) { |
| 55 | WARN("Failed to add PSCI cpu enable methods in DT\n"); |
| 56 | } |
Amit Nagal | 3a7d304 | 2023-07-10 10:32:15 +0530 | [diff] [blame] | 57 | |
Amit Nagal | 66d3cda | 2023-09-27 12:28:35 +0530 | [diff] [blame] | 58 | /* Reserve memory used by Trusted Firmware. */ |
| 59 | ret = fdt_add_reserved_memory(dtb, |
| 60 | "tf-a", |
| 61 | BL31_BASE, |
| 62 | BL31_LIMIT |
| 63 | - |
| 64 | BL31_BASE); |
| 65 | if (ret < 0) { |
| 66 | WARN("Failed to add reserved memory nodes for BL31 to DT.\n"); |
| 67 | } |
| 68 | |
| 69 | ret = fdt_pack(dtb); |
| 70 | if (ret < 0) { |
| 71 | WARN("Failed to pack dtb at %p: error %d\n", |
| 72 | dtb, ret); |
| 73 | } |
| 74 | flush_dcache_range((uintptr_t)dtb, |
| 75 | fdt_blob_size(dtb)); |
| 76 | |
| 77 | INFO("Changed device tree to advertise PSCI and reserved memories.\n"); |
| 78 | |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | } |
| 83 | |
Amit Nagal | c1248e8 | 2023-09-04 21:53:59 -1200 | [diff] [blame] | 84 | |
| 85 | #if defined(PLAT_XLAT_TABLES_DYNAMIC) |
Amit Nagal | 66d3cda | 2023-09-27 12:28:35 +0530 | [diff] [blame] | 86 | if (!map_ret) { |
| 87 | ret = mmap_remove_dynamic_region((uintptr_t)dtb, |
Amit Nagal | c1248e8 | 2023-09-04 21:53:59 -1200 | [diff] [blame] | 88 | XILINX_OF_BOARD_DTB_MAX_SIZE); |
Amit Nagal | 66d3cda | 2023-09-27 12:28:35 +0530 | [diff] [blame] | 89 | if (ret != 0) { |
| 90 | WARN("Failed to remove dynamic region for dtb:error %d\n", |
| 91 | ret); |
| 92 | } |
| 93 | } |
Amit Nagal | c1248e8 | 2023-09-04 21:53:59 -1200 | [diff] [blame] | 94 | #endif |
Amit Nagal | 66d3cda | 2023-09-27 12:28:35 +0530 | [diff] [blame] | 95 | } |
Amit Nagal | c1248e8 | 2023-09-04 21:53:59 -1200 | [diff] [blame] | 96 | |
Amit Nagal | 66d3cda | 2023-09-27 12:28:35 +0530 | [diff] [blame] | 97 | #endif |
Amit Nagal | 3a7d304 | 2023-07-10 10:32:15 +0530 | [diff] [blame] | 98 | } |