blob: 3038c09e87cb6ce3a6296df5fa7b19a6710d0171 [file] [log] [blame]
Louis Mayencourt6d2b5732019-12-17 13:17:25 +00001/*
Manish V Badarkhe4c156422022-03-16 09:34:36 +00002 * Copyright (c) 2019-2022, Arm Limited. All rights reserved.
Louis Mayencourt6d2b5732019-12-17 13:17:25 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
8
9#include <common/debug.h>
10#include <common/fdt_wrappers.h>
11#include <lib/fconf/fconf_dyn_cfg_getter.h>
12#include <lib/object_pool.h>
13#include <libfdt.h>
14
Alexei Fedorov40e7d6c2020-07-13 14:10:00 +010015/* We currently use FW, TB_FW, SOC_FW, TOS_FW, NT_FW and HW configs */
Manish V Badarkhebb533c72020-06-11 22:17:30 +010016#define MAX_DTB_INFO U(6)
Manish V Badarkhe6a91e592020-07-15 05:08:37 +010017/*
18 * Compile time assert if FW_CONFIG_ID is 0 which is more
19 * unlikely as 0 is a valid image ID for FIP as per the current
20 * code but still to avoid code breakage in case of unlikely
21 * event when image IDs get changed.
22 */
23CASSERT(FW_CONFIG_ID != U(0), assert_invalid_fw_config_id);
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000024
25static struct dyn_cfg_dtb_info_t dtb_infos[MAX_DTB_INFO];
26static OBJECT_POOL_ARRAY(dtb_info_pool, dtb_infos);
Manish V Badarkhe99a8e142020-06-11 22:32:11 +010027
28/*
Manish V Badarkhe6a91e592020-07-15 05:08:37 +010029 * This function is used to alloc memory for config information from
30 * global pool and set the configuration information.
Manish V Badarkhe99a8e142020-06-11 22:32:11 +010031 */
Manish V Badarkhe4c156422022-03-16 09:34:36 +000032void set_config_info(uintptr_t config_addr, uintptr_t ns_config_addr,
33 uint32_t config_max_size,
34 unsigned int config_id)
Manish V Badarkhe99a8e142020-06-11 22:32:11 +010035{
36 struct dyn_cfg_dtb_info_t *dtb_info;
37
38 dtb_info = pool_alloc(&dtb_info_pool);
39 dtb_info->config_addr = config_addr;
Manish V Badarkhe4c156422022-03-16 09:34:36 +000040 dtb_info->ns_config_addr = ns_config_addr;
Manish V Badarkhe99a8e142020-06-11 22:32:11 +010041 dtb_info->config_max_size = config_max_size;
Manish V Badarkhe6a91e592020-07-15 05:08:37 +010042 dtb_info->config_id = config_id;
Manish V Badarkhe99a8e142020-06-11 22:32:11 +010043}
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000044
Yann Gautier5a57e252021-12-13 15:22:45 +010045/* Get index of the config_id image */
46unsigned int dyn_cfg_dtb_info_get_index(unsigned int config_id)
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000047{
48 unsigned int index;
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000049
50 /* Positions index to the proper config-id */
Manish V Badarkhe6a91e592020-07-15 05:08:37 +010051 for (index = 0U; index < MAX_DTB_INFO; index++) {
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000052 if (dtb_infos[index].config_id == config_id) {
Yann Gautier5a57e252021-12-13 15:22:45 +010053 return index;
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000054 }
55 }
56
Yann Gautier5a57e252021-12-13 15:22:45 +010057 return FCONF_INVALID_IDX;
58}
59
60struct dyn_cfg_dtb_info_t *dyn_cfg_dtb_info_getter(unsigned int config_id)
61{
62 /* Positions index to the proper config-id */
63 unsigned int index = dyn_cfg_dtb_info_get_index(config_id);
64
65 if (index < MAX_DTB_INFO) {
66 return &dtb_infos[index];
67 }
68
Manish V Badarkhe6a91e592020-07-15 05:08:37 +010069 WARN("FCONF: Invalid config id %u\n", config_id);
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000070
Manish V Badarkhe6a91e592020-07-15 05:08:37 +010071 return NULL;
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000072}
73
74int fconf_populate_dtb_registry(uintptr_t config)
75{
76 int rc;
77 int node, child;
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000078
79 /* As libfdt use void *, we can't avoid this cast */
80 const void *dtb = (void *)config;
81
Manish V Badarkhe99a8e142020-06-11 22:32:11 +010082 /*
Manish V Badarkhe99a8e142020-06-11 22:32:11 +010083 * In case of BL1, fw_config dtb information is already
84 * populated in global dtb_infos array by 'set_fw_config_info'
85 * function, Below check is present to avoid re-population of
86 * fw_config information.
87 *
88 * Other BLs, satisfy below check and populate fw_config information
89 * in global dtb_infos array.
90 */
Manish V Badarkhe6a91e592020-07-15 05:08:37 +010091 if (dtb_infos[0].config_id == 0U) {
92 uint32_t config_max_size = fdt_totalsize(dtb);
Manish V Badarkhe4c156422022-03-16 09:34:36 +000093 set_config_info(config, ~0UL, config_max_size, FW_CONFIG_ID);
Manish V Badarkhe99a8e142020-06-11 22:32:11 +010094 }
95
Louis Mayencourtb26fafa2020-04-20 14:17:21 +010096 /* Find the node offset point to "fconf,dyn_cfg-dtb_registry" compatible property */
97 const char *compatible_str = "fconf,dyn_cfg-dtb_registry";
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000098 node = fdt_node_offset_by_compatible(dtb, -1, compatible_str);
99 if (node < 0) {
100 ERROR("FCONF: Can't find %s compatible in dtb\n", compatible_str);
101 return node;
102 }
103
104 fdt_for_each_subnode(child, dtb, node) {
Manish V Badarkhe6a91e592020-07-15 05:08:37 +0100105 uint32_t config_max_size, config_id;
106 uintptr_t config_addr;
Manish V Badarkhe4c156422022-03-16 09:34:36 +0000107 uintptr_t ns_config_addr = ~0UL;
Andre Przywarafe5bdf52020-03-26 11:22:37 +0000108 uint64_t val64;
109
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000110 /* Read configuration dtb information */
Andre Przywarafe5bdf52020-03-26 11:22:37 +0000111 rc = fdt_read_uint64(dtb, child, "load-address", &val64);
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000112 if (rc < 0) {
113 ERROR("FCONF: Incomplete configuration property in dtb-registry.\n");
114 return rc;
115 }
Manish V Badarkhe6a91e592020-07-15 05:08:37 +0100116 config_addr = (uintptr_t)val64;
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000117
Manish V Badarkhe6a91e592020-07-15 05:08:37 +0100118 rc = fdt_read_uint32(dtb, child, "max-size", &config_max_size);
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000119 if (rc < 0) {
120 ERROR("FCONF: Incomplete configuration property in dtb-registry.\n");
121 return rc;
122 }
123
Manish V Badarkhe6a91e592020-07-15 05:08:37 +0100124 rc = fdt_read_uint32(dtb, child, "id", &config_id);
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000125 if (rc < 0) {
126 ERROR("FCONF: Incomplete configuration property in dtb-registry.\n");
127 return rc;
128 }
129
130 VERBOSE("FCONF: dyn_cfg.dtb_registry cell found with:\n");
Manish V Badarkhe6a91e592020-07-15 05:08:37 +0100131 VERBOSE("\tload-address = %lx\n", config_addr);
132 VERBOSE("\tmax-size = 0x%x\n", config_max_size);
133 VERBOSE("\tconfig-id = %u\n", config_id);
134
Manish V Badarkhe4c156422022-03-16 09:34:36 +0000135 rc = fdt_read_uint64(dtb, child, "ns-load-address", &val64);
136 if (rc == 0) {
137 ns_config_addr = (uintptr_t)val64;
138 VERBOSE("\tns-load-address = %lx\n", ns_config_addr);
139 }
140
141 set_config_info(config_addr, ns_config_addr, config_max_size,
142 config_id);
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000143 }
144
145 if ((child < 0) && (child != -FDT_ERR_NOTFOUND)) {
146 ERROR("%d: fdt_for_each_subnode(): %d\n", __LINE__, node);
147 return child;
148 }
149
150 return 0;
151}
152
Manish V Badarkhe64616a52020-05-31 08:53:40 +0100153FCONF_REGISTER_POPULATOR(FW_CONFIG, dyn_cfg, fconf_populate_dtb_registry);