blob: 58956d622815c2f30d0477904b83c8b3b93b208f [file] [log] [blame]
Louis Mayencourt6d2b5732019-12-17 13:17:25 +00001/*
Manish V Badarkhecad8b292023-02-07 11:26:38 +00002 * Copyright (c) 2019-2023, 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
Claus Pedersen785e66c2022-09-12 22:42:58 +000015#include <platform_def.h>
16
Alexei Fedorov40e7d6c2020-07-13 14:10:00 +010017/* We currently use FW, TB_FW, SOC_FW, TOS_FW, NT_FW and HW configs */
Manish V Badarkhebb533c72020-06-11 22:17:30 +010018#define MAX_DTB_INFO U(6)
Manish V Badarkhe6a91e592020-07-15 05:08:37 +010019/*
20 * Compile time assert if FW_CONFIG_ID is 0 which is more
21 * unlikely as 0 is a valid image ID for FIP as per the current
22 * code but still to avoid code breakage in case of unlikely
23 * event when image IDs get changed.
24 */
25CASSERT(FW_CONFIG_ID != U(0), assert_invalid_fw_config_id);
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000026
27static struct dyn_cfg_dtb_info_t dtb_infos[MAX_DTB_INFO];
28static OBJECT_POOL_ARRAY(dtb_info_pool, dtb_infos);
Manish V Badarkhe99a8e142020-06-11 22:32:11 +010029
30/*
Manish V Badarkhe6a91e592020-07-15 05:08:37 +010031 * This function is used to alloc memory for config information from
32 * global pool and set the configuration information.
Manish V Badarkhe99a8e142020-06-11 22:32:11 +010033 */
Manish V Badarkhecad8b292023-02-07 11:26:38 +000034void set_config_info(uintptr_t config_addr, uintptr_t secondary_config_addr,
Manish V Badarkhe4c156422022-03-16 09:34:36 +000035 uint32_t config_max_size,
36 unsigned int config_id)
Manish V Badarkhe99a8e142020-06-11 22:32:11 +010037{
38 struct dyn_cfg_dtb_info_t *dtb_info;
39
40 dtb_info = pool_alloc(&dtb_info_pool);
41 dtb_info->config_addr = config_addr;
Manish V Badarkhecad8b292023-02-07 11:26:38 +000042 dtb_info->secondary_config_addr = secondary_config_addr;
Manish V Badarkhe99a8e142020-06-11 22:32:11 +010043 dtb_info->config_max_size = config_max_size;
Manish V Badarkhe6a91e592020-07-15 05:08:37 +010044 dtb_info->config_id = config_id;
Manish V Badarkhe99a8e142020-06-11 22:32:11 +010045}
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000046
Yann Gautier5a57e252021-12-13 15:22:45 +010047/* Get index of the config_id image */
48unsigned int dyn_cfg_dtb_info_get_index(unsigned int config_id)
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000049{
50 unsigned int index;
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000051
52 /* Positions index to the proper config-id */
Manish V Badarkhe6a91e592020-07-15 05:08:37 +010053 for (index = 0U; index < MAX_DTB_INFO; index++) {
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000054 if (dtb_infos[index].config_id == config_id) {
Yann Gautier5a57e252021-12-13 15:22:45 +010055 return index;
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000056 }
57 }
58
Yann Gautier5a57e252021-12-13 15:22:45 +010059 return FCONF_INVALID_IDX;
60}
61
62struct dyn_cfg_dtb_info_t *dyn_cfg_dtb_info_getter(unsigned int config_id)
63{
64 /* Positions index to the proper config-id */
65 unsigned int index = dyn_cfg_dtb_info_get_index(config_id);
66
67 if (index < MAX_DTB_INFO) {
68 return &dtb_infos[index];
69 }
70
Manish V Badarkhe6a91e592020-07-15 05:08:37 +010071 WARN("FCONF: Invalid config id %u\n", config_id);
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000072
Manish V Badarkhe6a91e592020-07-15 05:08:37 +010073 return NULL;
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000074}
75
76int fconf_populate_dtb_registry(uintptr_t config)
77{
78 int rc;
79 int node, child;
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000080
81 /* As libfdt use void *, we can't avoid this cast */
82 const void *dtb = (void *)config;
83
Manish V Badarkhe99a8e142020-06-11 22:32:11 +010084 /*
Manish V Badarkhe99a8e142020-06-11 22:32:11 +010085 * In case of BL1, fw_config dtb information is already
Sandrine Bailleuxccfda132023-06-06 14:25:00 +020086 * populated in global dtb_infos array by 'set_config_info'
Manish V Badarkhe99a8e142020-06-11 22:32:11 +010087 * function, Below check is present to avoid re-population of
88 * fw_config information.
89 *
90 * Other BLs, satisfy below check and populate fw_config information
91 * in global dtb_infos array.
92 */
Manish V Badarkhe6a91e592020-07-15 05:08:37 +010093 if (dtb_infos[0].config_id == 0U) {
94 uint32_t config_max_size = fdt_totalsize(dtb);
Manish V Badarkhe4c156422022-03-16 09:34:36 +000095 set_config_info(config, ~0UL, config_max_size, FW_CONFIG_ID);
Manish V Badarkhe99a8e142020-06-11 22:32:11 +010096 }
97
Louis Mayencourtb26fafa2020-04-20 14:17:21 +010098 /* Find the node offset point to "fconf,dyn_cfg-dtb_registry" compatible property */
99 const char *compatible_str = "fconf,dyn_cfg-dtb_registry";
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000100 node = fdt_node_offset_by_compatible(dtb, -1, compatible_str);
101 if (node < 0) {
102 ERROR("FCONF: Can't find %s compatible in dtb\n", compatible_str);
103 return node;
104 }
105
106 fdt_for_each_subnode(child, dtb, node) {
Manish V Badarkhe6a91e592020-07-15 05:08:37 +0100107 uint32_t config_max_size, config_id;
108 uintptr_t config_addr;
Manish V Badarkhecad8b292023-02-07 11:26:38 +0000109 uintptr_t secondary_config_addr = ~0UL;
Andre Przywarafe5bdf52020-03-26 11:22:37 +0000110 uint64_t val64;
111
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000112 /* Read configuration dtb information */
Andre Przywarafe5bdf52020-03-26 11:22:37 +0000113 rc = fdt_read_uint64(dtb, child, "load-address", &val64);
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000114 if (rc < 0) {
115 ERROR("FCONF: Incomplete configuration property in dtb-registry.\n");
116 return rc;
117 }
Manish V Badarkhe6a91e592020-07-15 05:08:37 +0100118 config_addr = (uintptr_t)val64;
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000119
Manish V Badarkhe6a91e592020-07-15 05:08:37 +0100120 rc = fdt_read_uint32(dtb, child, "max-size", &config_max_size);
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000121 if (rc < 0) {
122 ERROR("FCONF: Incomplete configuration property in dtb-registry.\n");
123 return rc;
124 }
125
Manish V Badarkhe6a91e592020-07-15 05:08:37 +0100126 rc = fdt_read_uint32(dtb, child, "id", &config_id);
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000127 if (rc < 0) {
128 ERROR("FCONF: Incomplete configuration property in dtb-registry.\n");
129 return rc;
130 }
131
132 VERBOSE("FCONF: dyn_cfg.dtb_registry cell found with:\n");
Manish V Badarkhe6a91e592020-07-15 05:08:37 +0100133 VERBOSE("\tload-address = %lx\n", config_addr);
134 VERBOSE("\tmax-size = 0x%x\n", config_max_size);
135 VERBOSE("\tconfig-id = %u\n", config_id);
136
Manish V Badarkhecad8b292023-02-07 11:26:38 +0000137 rc = fdt_read_uint64(dtb, child, "secondary-load-address",
138 &val64);
Manish V Badarkhe4c156422022-03-16 09:34:36 +0000139 if (rc == 0) {
Manish V Badarkhecad8b292023-02-07 11:26:38 +0000140 secondary_config_addr = (uintptr_t)val64;
141 VERBOSE("\tsecondary-load-address = %lx\n",
142 secondary_config_addr);
Manish V Badarkhe4c156422022-03-16 09:34:36 +0000143 }
144
Manish V Badarkhecad8b292023-02-07 11:26:38 +0000145 set_config_info(config_addr, secondary_config_addr,
146 config_max_size, config_id);
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000147 }
148
149 if ((child < 0) && (child != -FDT_ERR_NOTFOUND)) {
150 ERROR("%d: fdt_for_each_subnode(): %d\n", __LINE__, node);
151 return child;
152 }
153
154 return 0;
155}
156
Manish V Badarkhe64616a52020-05-31 08:53:40 +0100157FCONF_REGISTER_POPULATOR(FW_CONFIG, dyn_cfg, fconf_populate_dtb_registry);