xref: /rk3399_ARM-atf/plat/arm/common/arm_dyn_cfg.c (revision da5f274572c063962556821914e2f64db5ae3d2d)
1c228956aSSoby Mathew /*
2c228956aSSoby Mathew  * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
3c228956aSSoby Mathew  *
4c228956aSSoby Mathew  * SPDX-License-Identifier: BSD-3-Clause
5c228956aSSoby Mathew  */
6c228956aSSoby Mathew 
7cab0b5b0SSoby Mathew #include <arm_dyn_cfg_helpers.h>
8c228956aSSoby Mathew #include <assert.h>
9c228956aSSoby Mathew #include <debug.h>
10c228956aSSoby Mathew #include <desc_image_load.h>
11*da5f2745SSoby Mathew #include <plat_arm.h>
12c228956aSSoby Mathew #include <platform.h>
13c228956aSSoby Mathew #include <platform_def.h>
14c228956aSSoby Mathew #include <string.h>
15c228956aSSoby Mathew #include <tbbr_img_def.h>
16c228956aSSoby Mathew 
17c228956aSSoby Mathew #if LOAD_IMAGE_V2
18c228956aSSoby Mathew 
19cab0b5b0SSoby Mathew /* Variable to store the address to TB_FW_CONFIG passed from BL1 */
20cab0b5b0SSoby Mathew static void *tb_fw_cfg_dtb;
21cab0b5b0SSoby Mathew 
22c228956aSSoby Mathew /*
23c228956aSSoby Mathew  * Helper function to load TB_FW_CONFIG and populate the load information to
24c228956aSSoby Mathew  * arg0 of BL2 entrypoint info.
25c228956aSSoby Mathew  */
26c228956aSSoby Mathew void arm_load_tb_fw_config(void)
27c228956aSSoby Mathew {
28c228956aSSoby Mathew 	int err;
29c228956aSSoby Mathew 	uintptr_t config_base = 0;
30c228956aSSoby Mathew 	image_desc_t *image_desc;
31c228956aSSoby Mathew 
32c228956aSSoby Mathew 	image_desc_t arm_tb_fw_info = {
33c228956aSSoby Mathew 		.image_id = TB_FW_CONFIG_ID,
34c228956aSSoby Mathew 		SET_STATIC_PARAM_HEAD(image_info, PARAM_IMAGE_BINARY,
35c228956aSSoby Mathew 				VERSION_2, image_info_t, 0),
36c228956aSSoby Mathew 		.image_info.image_base = ARM_TB_FW_CONFIG_BASE,
37c228956aSSoby Mathew 		.image_info.image_max_size = ARM_TB_FW_CONFIG_LIMIT - ARM_TB_FW_CONFIG_BASE,
38c228956aSSoby Mathew 	};
39c228956aSSoby Mathew 
40c228956aSSoby Mathew 	VERBOSE("BL1: Loading TB_FW_CONFIG\n");
41c228956aSSoby Mathew 	err = load_auth_image(TB_FW_CONFIG_ID, &arm_tb_fw_info.image_info);
42*da5f2745SSoby Mathew 	if (err != 0) {
43c228956aSSoby Mathew 		/* Return if TB_FW_CONFIG is not loaded */
44c228956aSSoby Mathew 		VERBOSE("Failed to load TB_FW_CONFIG\n");
45c228956aSSoby Mathew 		return;
46c228956aSSoby Mathew 	}
47c228956aSSoby Mathew 
48c228956aSSoby Mathew 	config_base = arm_tb_fw_info.image_info.image_base;
49c228956aSSoby Mathew 
50c228956aSSoby Mathew 	/* The BL2 ep_info arg0 is modified to point to TB_FW_CONFIG */
51c228956aSSoby Mathew 	image_desc = bl1_plat_get_image_desc(BL2_IMAGE_ID);
52*da5f2745SSoby Mathew 	assert(image_desc != NULL);
53c228956aSSoby Mathew 	image_desc->ep_info.args.arg0 = config_base;
54c228956aSSoby Mathew 
55c228956aSSoby Mathew 	INFO("BL1: TB_FW_CONFIG loaded at address = %p\n",
56c228956aSSoby Mathew 			(void *) config_base);
57c228956aSSoby Mathew }
58c228956aSSoby Mathew 
59cab0b5b0SSoby Mathew /*
60cab0b5b0SSoby Mathew  * BL2 utility function to set the address of TB_FW_CONFIG passed from BL1.
61cab0b5b0SSoby Mathew  */
62cab0b5b0SSoby Mathew void arm_bl2_set_tb_cfg_addr(void *dtb)
63cab0b5b0SSoby Mathew {
64*da5f2745SSoby Mathew 	assert(dtb != NULL);
65cab0b5b0SSoby Mathew 	tb_fw_cfg_dtb = dtb;
66cab0b5b0SSoby Mathew }
67cab0b5b0SSoby Mathew 
68cab0b5b0SSoby Mathew /*
69cab0b5b0SSoby Mathew  * BL2 utility function to initialize dynamic configuration specified by
70cab0b5b0SSoby Mathew  * TB_FW_CONFIG. Return early if TB_FW_CONFIG is not found or HW_CONFIG is
71cab0b5b0SSoby Mathew  * not specified in TB_FW_CONFIG.
72cab0b5b0SSoby Mathew  */
73cab0b5b0SSoby Mathew void arm_bl2_dyn_cfg_init(void)
74cab0b5b0SSoby Mathew {
75cab0b5b0SSoby Mathew 	int err = 0;
76cab0b5b0SSoby Mathew 	int tb_fw_node;
77cab0b5b0SSoby Mathew 	bl_mem_params_node_t *hw_cfg_mem_params = NULL;
78cab0b5b0SSoby Mathew 
79cab0b5b0SSoby Mathew 	if (tb_fw_cfg_dtb == NULL) {
80cab0b5b0SSoby Mathew 		VERBOSE("No TB_FW_CONFIG specified\n");
81cab0b5b0SSoby Mathew 		return;
82cab0b5b0SSoby Mathew 	}
83cab0b5b0SSoby Mathew 
84cab0b5b0SSoby Mathew 	err = arm_dyn_tb_fw_cfg_init((void *)tb_fw_cfg_dtb, &tb_fw_node);
85cab0b5b0SSoby Mathew 	if (err < 0) {
86cab0b5b0SSoby Mathew 		ERROR("Invalid TB_FW_CONFIG passed from BL1\n");
87cab0b5b0SSoby Mathew 		panic();
88cab0b5b0SSoby Mathew 	}
89cab0b5b0SSoby Mathew 
90cab0b5b0SSoby Mathew 	/* Get the hw_config load address and size from TB_FW_CONFIG */
91cab0b5b0SSoby Mathew 	hw_cfg_mem_params = get_bl_mem_params_node(HW_CONFIG_ID);
92cab0b5b0SSoby Mathew 	if (hw_cfg_mem_params == NULL) {
93cab0b5b0SSoby Mathew 		VERBOSE("Couldn't find HW_CONFIG in bl_mem_params_node\n");
94cab0b5b0SSoby Mathew 		return;
95cab0b5b0SSoby Mathew 	}
96cab0b5b0SSoby Mathew 
97cab0b5b0SSoby Mathew 	err = arm_dyn_get_hwconfig_info((void *)tb_fw_cfg_dtb, tb_fw_node,
98cab0b5b0SSoby Mathew 		(uint64_t *) &hw_cfg_mem_params->image_info.image_base,
99cab0b5b0SSoby Mathew 		&hw_cfg_mem_params->image_info.image_max_size);
100cab0b5b0SSoby Mathew 	if (err < 0) {
101cab0b5b0SSoby Mathew 		VERBOSE("Couldn't find HW_CONFIG load info in TB_FW_CONFIG\n");
102cab0b5b0SSoby Mathew 		return;
103cab0b5b0SSoby Mathew 	}
104cab0b5b0SSoby Mathew 
105cab0b5b0SSoby Mathew 	/* Remove the IMAGE_ATTRIB_SKIP_LOADING attribute from HW_CONFIG node */
106cab0b5b0SSoby Mathew 	hw_cfg_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;
107cab0b5b0SSoby Mathew }
108cab0b5b0SSoby Mathew 
109c228956aSSoby Mathew #endif /* LOAD_IMAGE_V2 */
110