blob: 911f664870a6fb95e2eccd952ad0da1e284ab073 [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 Nagal659c6aa2023-08-29 02:44:59 -120018 void *dtb;
Amit Nagal3a7d3042023-07-10 10:32:15 +053019 int ret;
Amit Nagal659c6aa2023-08-29 02:44:59 -120020#if !defined(XILINX_OF_BOARD_DTB_ADDR)
21 return;
22#else
23 dtb = (void *)XILINX_OF_BOARD_DTB_ADDR;
24#endif
25 if (IS_TFA_IN_OCM(BL31_BASE))
26 return;
Amit Nagal3a7d3042023-07-10 10:32:15 +053027
Amit Nagalc1248e82023-09-04 21:53:59 -120028#if defined(PLAT_XLAT_TABLES_DYNAMIC)
29 ret = mmap_add_dynamic_region((unsigned long long)dtb,
30 (uintptr_t)dtb,
31 XILINX_OF_BOARD_DTB_MAX_SIZE,
32 MT_MEMORY | MT_RW | MT_NS);
33 if (ret != 0) {
34 WARN("Failed to add dynamic region for dtb: error %d\n", ret);
35 return;
36 }
37#endif
38
Amit Nagal3a7d3042023-07-10 10:32:15 +053039 /* 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 return;
43 }
44
45 ret = fdt_open_into(dtb, dtb, XILINX_OF_BOARD_DTB_MAX_SIZE);
46 if (ret < 0) {
47 ERROR("Invalid Device Tree at %p: error %d\n", dtb, ret);
48 return;
49 }
50
51 /* Reserve memory used by Trusted Firmware. */
Michal Simek53be5f62023-09-05 12:26:44 +020052 if (fdt_add_reserved_memory(dtb, "tf-a", BL31_BASE, BL31_LIMIT - BL31_BASE)) {
Amit Nagal3a7d3042023-07-10 10:32:15 +053053 WARN("Failed to add reserved memory nodes for BL31 to DT.\n");
54 return;
55 }
56
57 ret = fdt_pack(dtb);
58 if (ret < 0) {
59 ERROR("Failed to pack Device Tree at %p: error %d\n", dtb, ret);
60 return;
61 }
62
Amit Nagal29efcbe2023-09-04 21:54:00 -120063 flush_dcache_range((uintptr_t)dtb, fdt_blob_size(dtb));
Amit Nagalc1248e82023-09-04 21:53:59 -120064
65#if defined(PLAT_XLAT_TABLES_DYNAMIC)
66 ret = mmap_remove_dynamic_region((uintptr_t)dtb,
67 XILINX_OF_BOARD_DTB_MAX_SIZE);
68 if (ret != 0) {
69 WARN("Failed to remove dynamic region for dtb: error %d\n", ret);
70 return;
71 }
72#endif
73
Amit Nagal3a7d3042023-07-10 10:32:15 +053074 INFO("Changed device tree to advertise PSCI and reserved memories.\n");
75}