xref: /rk3399_ARM-atf/lib/fconf/fconf_dyn_cfg_getter.c (revision e3a234971abb2402cbf376eca6fcb657a7709fae)
1 /*
2  * Copyright (c) 2019-2021, Arm Limited. All rights reserved.
3  *
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 
15 /* We currently use FW, TB_FW, SOC_FW, TOS_FW, NT_FW and HW configs  */
16 #define MAX_DTB_INFO	U(6)
17 /*
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  */
23 CASSERT(FW_CONFIG_ID != U(0), assert_invalid_fw_config_id);
24 
25 static struct dyn_cfg_dtb_info_t dtb_infos[MAX_DTB_INFO];
26 static OBJECT_POOL_ARRAY(dtb_info_pool, dtb_infos);
27 
28 /*
29  * This function is used to alloc memory for config information from
30  * global pool and set the configuration information.
31  */
32 void set_config_info(uintptr_t config_addr, uint32_t config_max_size,
33 			unsigned int config_id)
34 {
35 	struct dyn_cfg_dtb_info_t *dtb_info;
36 
37 	dtb_info = pool_alloc(&dtb_info_pool);
38 	dtb_info->config_addr = config_addr;
39 	dtb_info->config_max_size = config_max_size;
40 	dtb_info->config_id = config_id;
41 }
42 
43 /* Get index of the config_id image */
44 unsigned int dyn_cfg_dtb_info_get_index(unsigned int config_id)
45 {
46 	unsigned int index;
47 
48 	/* Positions index to the proper config-id */
49 	for (index = 0U; index < MAX_DTB_INFO; index++) {
50 		if (dtb_infos[index].config_id == config_id) {
51 			return index;
52 		}
53 	}
54 
55 	return FCONF_INVALID_IDX;
56 }
57 
58 struct dyn_cfg_dtb_info_t *dyn_cfg_dtb_info_getter(unsigned int config_id)
59 {
60 	/* Positions index to the proper config-id */
61 	unsigned int index = dyn_cfg_dtb_info_get_index(config_id);
62 
63 	if (index < MAX_DTB_INFO) {
64 		return &dtb_infos[index];
65 	}
66 
67 	WARN("FCONF: Invalid config id %u\n", config_id);
68 
69 	return NULL;
70 }
71 
72 int fconf_populate_dtb_registry(uintptr_t config)
73 {
74 	int rc;
75 	int node, child;
76 
77 	/* As libfdt use void *, we can't avoid this cast */
78 	const void *dtb = (void *)config;
79 
80 	/*
81 	 * In case of BL1, fw_config dtb information is already
82 	 * populated in global dtb_infos array by 'set_fw_config_info'
83 	 * function, Below check is present to avoid re-population of
84 	 * fw_config information.
85 	 *
86 	 * Other BLs, satisfy below check and populate fw_config information
87 	 * in global dtb_infos array.
88 	 */
89 	if (dtb_infos[0].config_id == 0U) {
90 		uint32_t config_max_size = fdt_totalsize(dtb);
91 		set_config_info(config, config_max_size, FW_CONFIG_ID);
92 	}
93 
94 	/* Find the node offset point to "fconf,dyn_cfg-dtb_registry" compatible property */
95 	const char *compatible_str = "fconf,dyn_cfg-dtb_registry";
96 	node = fdt_node_offset_by_compatible(dtb, -1, compatible_str);
97 	if (node < 0) {
98 		ERROR("FCONF: Can't find %s compatible in dtb\n", compatible_str);
99 		return node;
100 	}
101 
102 	fdt_for_each_subnode(child, dtb, node) {
103 		uint32_t config_max_size, config_id;
104 		uintptr_t config_addr;
105 		uint64_t val64;
106 
107 		/* Read configuration dtb information */
108 		rc = fdt_read_uint64(dtb, child, "load-address", &val64);
109 		if (rc < 0) {
110 			ERROR("FCONF: Incomplete configuration property in dtb-registry.\n");
111 			return rc;
112 		}
113 		config_addr = (uintptr_t)val64;
114 
115 		rc = fdt_read_uint32(dtb, child, "max-size", &config_max_size);
116 		if (rc < 0) {
117 			ERROR("FCONF: Incomplete configuration property in dtb-registry.\n");
118 			return rc;
119 		}
120 
121 		rc = fdt_read_uint32(dtb, child, "id", &config_id);
122 		if (rc < 0) {
123 			ERROR("FCONF: Incomplete configuration property in dtb-registry.\n");
124 			return rc;
125 		}
126 
127 		VERBOSE("FCONF: dyn_cfg.dtb_registry cell found with:\n");
128 		VERBOSE("\tload-address = %lx\n", config_addr);
129 		VERBOSE("\tmax-size = 0x%x\n", config_max_size);
130 		VERBOSE("\tconfig-id = %u\n", config_id);
131 
132 		set_config_info(config_addr, config_max_size, config_id);
133 	}
134 
135 	if ((child < 0) && (child != -FDT_ERR_NOTFOUND)) {
136 		ERROR("%d: fdt_for_each_subnode(): %d\n", __LINE__, node);
137 		return child;
138 	}
139 
140 	return 0;
141 }
142 
143 FCONF_REGISTER_POPULATOR(FW_CONFIG, dyn_cfg, fconf_populate_dtb_registry);
144