blob: de5d1a1f440b2d6e44d5a2653e4ca3d4b45f1209 [file] [log] [blame]
Amit Nagal3a7d3042023-07-10 10:32:15 +05301/*
2 * Copyright (c) 2023, Advanced Micro Devices, Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
Akshay Belsaree3f2a102023-07-19 10:07:50 +05307#include <common/debug.h>
Amit Nagal3a7d3042023-07-10 10:32:15 +05308#include <common/fdt_fixup.h>
9#include <common/fdt_wrappers.h>
Akshay Belsaree3f2a102023-07-19 10:07:50 +053010#include <libfdt.h>
Amit Nagalc1248e82023-09-04 21:53:59 -120011#include <lib/xlat_tables/xlat_tables_v2.h>
Amit Nagal3a7d3042023-07-10 10:32:15 +053012
13#include <plat_fdt.h>
14#include <platform_def.h>
15
Amit Nagal3a7d3042023-07-10 10:32:15 +053016void prepare_dtb(void)
17{
Amit Nagal66d3cda2023-09-27 12:28:35 +053018#if defined(XILINX_OF_BOARD_DTB_ADDR)
Amit Nagal659c6aa2023-08-29 02:44:59 -120019 void *dtb;
Amit Nagal66d3cda2023-09-27 12:28:35 +053020 int map_ret = 0;
21 int ret = 0;
22
Amit Nagal659c6aa2023-08-29 02:44:59 -120023 dtb = (void *)XILINX_OF_BOARD_DTB_ADDR;
Amit Nagal66d3cda2023-09-27 12:28:35 +053024
25 if (!IS_TFA_IN_OCM(BL31_BASE)) {
Amit Nagal3a7d3042023-07-10 10:32:15 +053026
Amit Nagalc1248e82023-09-04 21:53:59 -120027#if defined(PLAT_XLAT_TABLES_DYNAMIC)
Amit Nagal66d3cda2023-09-27 12:28:35 +053028 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 Nagalc1248e82023-09-04 21:53:59 -120036#endif
37
Amit Nagal66d3cda2023-09-27 12:28:35 +053038 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 Nagal3a7d3042023-07-10 10:32:15 +053044
Amit Nagal66d3cda2023-09-27 12:28:35 +053045 if (ret < 0) {
46 ERROR("Invalid Device Tree at %p: error %d\n",
47 dtb, ret);
48 } else {
Amit Nagal3a7d3042023-07-10 10:32:15 +053049
Amit Nagal66d3cda2023-09-27 12:28:35 +053050 if (dt_add_psci_node(dtb)) {
51 WARN("Failed to add PSCI Device Tree node\n");
52 }
Amit Nagal3a7d3042023-07-10 10:32:15 +053053
Amit Nagal66d3cda2023-09-27 12:28:35 +053054 if (dt_add_psci_cpu_enable_methods(dtb)) {
55 WARN("Failed to add PSCI cpu enable methods in DT\n");
56 }
Amit Nagal3a7d3042023-07-10 10:32:15 +053057
Amit Nagal66d3cda2023-09-27 12:28:35 +053058 /* 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 Nagalc1248e82023-09-04 21:53:59 -120084
85#if defined(PLAT_XLAT_TABLES_DYNAMIC)
Amit Nagal66d3cda2023-09-27 12:28:35 +053086 if (!map_ret) {
87 ret = mmap_remove_dynamic_region((uintptr_t)dtb,
Amit Nagalc1248e82023-09-04 21:53:59 -120088 XILINX_OF_BOARD_DTB_MAX_SIZE);
Amit Nagal66d3cda2023-09-27 12:28:35 +053089 if (ret != 0) {
90 WARN("Failed to remove dynamic region for dtb:error %d\n",
91 ret);
92 }
93 }
Amit Nagalc1248e82023-09-04 21:53:59 -120094#endif
Amit Nagal66d3cda2023-09-27 12:28:35 +053095 }
Amit Nagalc1248e82023-09-04 21:53:59 -120096
Amit Nagal66d3cda2023-09-27 12:28:35 +053097#endif
Amit Nagal3a7d3042023-07-10 10:32:15 +053098}