xref: /rk3399_ARM-atf/include/common/bl_common.h (revision 5dffb46c9c0ba2d3ae455b50fb78f418bd1f1258)
14ecca339SDan Handley /*
2*5dffb46cSSoby Mathew  * Copyright (c) 2013-2017, ARM Limited and Contributors. All rights reserved.
34ecca339SDan Handley  *
44ecca339SDan Handley  * Redistribution and use in source and binary forms, with or without
54ecca339SDan Handley  * modification, are permitted provided that the following conditions are met:
64ecca339SDan Handley  *
74ecca339SDan Handley  * Redistributions of source code must retain the above copyright notice, this
84ecca339SDan Handley  * list of conditions and the following disclaimer.
94ecca339SDan Handley  *
104ecca339SDan Handley  * Redistributions in binary form must reproduce the above copyright notice,
114ecca339SDan Handley  * this list of conditions and the following disclaimer in the documentation
124ecca339SDan Handley  * and/or other materials provided with the distribution.
134ecca339SDan Handley  *
144ecca339SDan Handley  * Neither the name of ARM nor the names of its contributors may be used
154ecca339SDan Handley  * to endorse or promote products derived from this software without specific
164ecca339SDan Handley  * prior written permission.
174ecca339SDan Handley  *
184ecca339SDan Handley  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
194ecca339SDan Handley  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
204ecca339SDan Handley  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
214ecca339SDan Handley  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
224ecca339SDan Handley  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
234ecca339SDan Handley  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
244ecca339SDan Handley  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
254ecca339SDan Handley  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
264ecca339SDan Handley  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
274ecca339SDan Handley  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
284ecca339SDan Handley  * POSSIBILITY OF SUCH DAMAGE.
294ecca339SDan Handley  */
304ecca339SDan Handley 
314ecca339SDan Handley #ifndef __BL_COMMON_H__
324ecca339SDan Handley #define __BL_COMMON_H__
334ecca339SDan Handley 
34*5dffb46cSSoby Mathew #include <ep_info.h>
35*5dffb46cSSoby Mathew #include <param_header.h>
364ecca339SDan Handley 
374ecca339SDan Handley #define UP	1
384ecca339SDan Handley #define DOWN	0
394ecca339SDan Handley 
404ecca339SDan Handley /*******************************************************************************
418f55dfb4SSandrine Bailleux  * Constants to identify the location of a memory region in a given memory
428f55dfb4SSandrine Bailleux  * layout.
434ecca339SDan Handley ******************************************************************************/
448f55dfb4SSandrine Bailleux #define TOP	0x1
458f55dfb4SSandrine Bailleux #define BOTTOM	!TOP
464ecca339SDan Handley 
477baff11fSYatharth Kochar /*
487baff11fSYatharth Kochar  * The following are used for image state attributes.
497baff11fSYatharth Kochar  * Image can only be in one of the following state.
507baff11fSYatharth Kochar  */
517baff11fSYatharth Kochar #define IMAGE_STATE_RESET			0
527baff11fSYatharth Kochar #define IMAGE_STATE_COPIED			1
537baff11fSYatharth Kochar #define IMAGE_STATE_COPYING			2
547baff11fSYatharth Kochar #define IMAGE_STATE_AUTHENTICATED		3
557baff11fSYatharth Kochar #define IMAGE_STATE_EXECUTED			4
567baff11fSYatharth Kochar #define IMAGE_STATE_INTERRUPTED			5
577baff11fSYatharth Kochar 
5872600226SYatharth Kochar #define IMAGE_ATTRIB_SKIP_LOADING	0x02
5972600226SYatharth Kochar #define IMAGE_ATTRIB_PLAT_SETUP		0x04
604112bfa0SVikram Kanigiri 
617baff11fSYatharth Kochar #define INVALID_IMAGE_ID		(0xFFFFFFFF)
627baff11fSYatharth Kochar 
63bbf8f6f9SYatharth Kochar /*******************************************************************************
64bbf8f6f9SYatharth Kochar  * Constants to indicate type of exception to the common exception handler.
65bbf8f6f9SYatharth Kochar  ******************************************************************************/
66bbf8f6f9SYatharth Kochar #define SYNC_EXCEPTION_SP_EL0		0x0
67bbf8f6f9SYatharth Kochar #define IRQ_SP_EL0			0x1
68bbf8f6f9SYatharth Kochar #define FIQ_SP_EL0			0x2
69bbf8f6f9SYatharth Kochar #define SERROR_SP_EL0			0x3
70bbf8f6f9SYatharth Kochar #define SYNC_EXCEPTION_SP_ELX		0x4
71bbf8f6f9SYatharth Kochar #define IRQ_SP_ELX			0x5
72bbf8f6f9SYatharth Kochar #define FIQ_SP_ELX			0x6
73bbf8f6f9SYatharth Kochar #define SERROR_SP_ELX			0x7
74bbf8f6f9SYatharth Kochar #define SYNC_EXCEPTION_AARCH64		0x8
75bbf8f6f9SYatharth Kochar #define IRQ_AARCH64			0x9
76bbf8f6f9SYatharth Kochar #define FIQ_AARCH64			0xa
77bbf8f6f9SYatharth Kochar #define SERROR_AARCH64			0xb
78bbf8f6f9SYatharth Kochar #define SYNC_EXCEPTION_AARCH32		0xc
79bbf8f6f9SYatharth Kochar #define IRQ_AARCH32			0xd
80bbf8f6f9SYatharth Kochar #define FIQ_AARCH32			0xe
81bbf8f6f9SYatharth Kochar #define SERROR_AARCH32			0xf
82bbf8f6f9SYatharth Kochar 
834ecca339SDan Handley #ifndef __ASSEMBLY__
8429fb905dSVikram Kanigiri #include <cassert.h>
858f55dfb4SSandrine Bailleux #include <stddef.h>
86*5dffb46cSSoby Mathew #include <stdint.h>
874c0d0390SSoby Mathew #include <types.h>
88ed81f3ebSSandrine Bailleux #include <utils.h> /* To retain compatibility */
89a7e98ad5SVikram Kanigiri 
9090b3a6acSDan Handley /*
9190b3a6acSDan Handley  * Declarations of linker defined symbols to help determine memory layout of
9290b3a6acSDan Handley  * BL images
9390b3a6acSDan Handley  */
945d1c104fSSandrine Bailleux #if SEPARATE_CODE_AND_RODATA
954c0d0390SSoby Mathew extern uintptr_t __TEXT_START__;
964c0d0390SSoby Mathew extern uintptr_t __TEXT_END__;
974c0d0390SSoby Mathew extern uintptr_t __RODATA_START__;
984c0d0390SSoby Mathew extern uintptr_t __RODATA_END__;
995d1c104fSSandrine Bailleux #else
1004c0d0390SSoby Mathew extern uintptr_t __RO_START__;
1014c0d0390SSoby Mathew extern uintptr_t __RO_END__;
1025d1c104fSSandrine Bailleux #endif
1035d1c104fSSandrine Bailleux 
1043d8256b2SMasahiro Yamada #if defined(IMAGE_BL2)
1054c0d0390SSoby Mathew extern uintptr_t __BL2_END__;
1063d8256b2SMasahiro Yamada #elif defined(IMAGE_BL2U)
1074c0d0390SSoby Mathew extern uintptr_t __BL2U_END__;
1083d8256b2SMasahiro Yamada #elif defined(IMAGE_BL31)
1094c0d0390SSoby Mathew extern uintptr_t __BL31_END__;
1103d8256b2SMasahiro Yamada #elif defined(IMAGE_BL32)
1114c0d0390SSoby Mathew extern uintptr_t __BL32_END__;
11290b3a6acSDan Handley #endif /* IMAGE_BLX */
11390b3a6acSDan Handley 
11490b3a6acSDan Handley #if USE_COHERENT_MEM
1154c0d0390SSoby Mathew extern uintptr_t __COHERENT_RAM_START__;
1164c0d0390SSoby Mathew extern uintptr_t __COHERENT_RAM_END__;
11790b3a6acSDan Handley #endif
11890b3a6acSDan Handley 
1194ecca339SDan Handley /*******************************************************************************
1204ecca339SDan Handley  * Structure used for telling the next BL how much of a particular type of
1214ecca339SDan Handley  * memory is available for its use and how much is already used.
1224ecca339SDan Handley  ******************************************************************************/
123fb037bfbSDan Handley typedef struct meminfo {
1244c0d0390SSoby Mathew 	uintptr_t total_base;
1258f55dfb4SSandrine Bailleux 	size_t total_size;
12672600226SYatharth Kochar #if !LOAD_IMAGE_V2
1274c0d0390SSoby Mathew 	uintptr_t free_base;
1288f55dfb4SSandrine Bailleux 	size_t free_size;
12972600226SYatharth Kochar #endif
130fb037bfbSDan Handley } meminfo_t;
1314ecca339SDan Handley 
1324112bfa0SVikram Kanigiri /*****************************************************************************
1334112bfa0SVikram Kanigiri  * Image info binary provides information from the image loader that
1344112bfa0SVikram Kanigiri  * can be used by the firmware to manage available trusted RAM.
1354112bfa0SVikram Kanigiri  * More advanced firmware image formats can provide additional
1364112bfa0SVikram Kanigiri  * information that enables optimization or greater flexibility in the
1374112bfa0SVikram Kanigiri  * common firmware code
1384112bfa0SVikram Kanigiri  *****************************************************************************/
1394112bfa0SVikram Kanigiri typedef struct image_info {
1404112bfa0SVikram Kanigiri 	param_header_t h;
1414112bfa0SVikram Kanigiri 	uintptr_t image_base;   /* physical address of base of image */
1424112bfa0SVikram Kanigiri 	uint32_t image_size;    /* bytes read from image file */
14372600226SYatharth Kochar #if LOAD_IMAGE_V2
14472600226SYatharth Kochar 	uint32_t image_max_size;
14572600226SYatharth Kochar #endif
1464112bfa0SVikram Kanigiri } image_info_t;
1474ecca339SDan Handley 
1487baff11fSYatharth Kochar /*****************************************************************************
1497baff11fSYatharth Kochar  * The image descriptor struct definition.
1507baff11fSYatharth Kochar  *****************************************************************************/
1517baff11fSYatharth Kochar typedef struct image_desc {
1527baff11fSYatharth Kochar 	/* Contains unique image id for the image. */
1537baff11fSYatharth Kochar 	unsigned int image_id;
1547baff11fSYatharth Kochar 	/*
1557baff11fSYatharth Kochar 	 * This member contains Image state information.
1567baff11fSYatharth Kochar 	 * Refer IMAGE_STATE_XXX defined above.
1577baff11fSYatharth Kochar 	 */
1587baff11fSYatharth Kochar 	unsigned int state;
159843ddee4SYatharth Kochar 	uint32_t copied_size;	/* image size copied in blocks */
160b313d755SSoby Mathew 	image_info_t image_info;
161b313d755SSoby Mathew 	entry_point_info_t ep_info;
1627baff11fSYatharth Kochar } image_desc_t;
1637baff11fSYatharth Kochar 
16472600226SYatharth Kochar #if LOAD_IMAGE_V2
16572600226SYatharth Kochar /* BL image node in the BL image loading sequence */
16672600226SYatharth Kochar typedef struct bl_load_info_node {
16772600226SYatharth Kochar 	unsigned int image_id;
16872600226SYatharth Kochar 	image_info_t *image_info;
16972600226SYatharth Kochar 	struct bl_load_info_node *next_load_info;
17072600226SYatharth Kochar } bl_load_info_node_t;
17172600226SYatharth Kochar 
17272600226SYatharth Kochar /* BL image head node in the BL image loading sequence */
17372600226SYatharth Kochar typedef struct bl_load_info {
17472600226SYatharth Kochar 	param_header_t h;
17572600226SYatharth Kochar 	bl_load_info_node_t *head;
17672600226SYatharth Kochar } bl_load_info_t;
17772600226SYatharth Kochar 
17872600226SYatharth Kochar /* BL image node in the BL image execution sequence */
17972600226SYatharth Kochar typedef struct bl_params_node {
18072600226SYatharth Kochar 	unsigned int image_id;
18172600226SYatharth Kochar 	image_info_t *image_info;
18272600226SYatharth Kochar 	entry_point_info_t *ep_info;
18372600226SYatharth Kochar 	struct bl_params_node *next_params_info;
18472600226SYatharth Kochar } bl_params_node_t;
18572600226SYatharth Kochar 
18672600226SYatharth Kochar /*
18772600226SYatharth Kochar  * BL image head node in the BL image execution sequence
18872600226SYatharth Kochar  * It is also used to pass information to next BL image.
18972600226SYatharth Kochar  */
19072600226SYatharth Kochar typedef struct bl_params {
19172600226SYatharth Kochar 	param_header_t h;
19272600226SYatharth Kochar 	bl_params_node_t *head;
19372600226SYatharth Kochar } bl_params_t;
19472600226SYatharth Kochar 
19572600226SYatharth Kochar #else /* LOAD_IMAGE_V2 */
19672600226SYatharth Kochar 
1974ecca339SDan Handley /*******************************************************************************
1984ecca339SDan Handley  * This structure represents the superset of information that can be passed to
1994ecca339SDan Handley  * BL31 e.g. while passing control to it from BL2. The BL32 parameters will be
2004112bfa0SVikram Kanigiri  * populated only if BL2 detects its presence. A pointer to a structure of this
201d178637dSJuan Castillo  * type should be passed in X0 to BL31's cold boot entrypoint.
2024112bfa0SVikram Kanigiri  *
203d178637dSJuan Castillo  * Use of this structure and the X0 parameter is not mandatory: the BL31
2044112bfa0SVikram Kanigiri  * platform code can use other mechanisms to provide the necessary information
205d178637dSJuan Castillo  * about BL32 and BL33 to the common and SPD code.
2064112bfa0SVikram Kanigiri  *
207d178637dSJuan Castillo  * BL31 image information is mandatory if this structure is used. If either of
208d178637dSJuan Castillo  * the optional BL32 and BL33 image information is not provided, this is
2094112bfa0SVikram Kanigiri  * indicated by the respective image_info pointers being zero.
2104ecca339SDan Handley  ******************************************************************************/
2114112bfa0SVikram Kanigiri typedef struct bl31_params {
2124112bfa0SVikram Kanigiri 	param_header_t h;
2134112bfa0SVikram Kanigiri 	image_info_t *bl31_image_info;
2144112bfa0SVikram Kanigiri 	entry_point_info_t *bl32_ep_info;
2154112bfa0SVikram Kanigiri 	image_info_t *bl32_image_info;
2164112bfa0SVikram Kanigiri 	entry_point_info_t *bl33_ep_info;
2174112bfa0SVikram Kanigiri 	image_info_t *bl33_image_info;
2184112bfa0SVikram Kanigiri } bl31_params_t;
2194112bfa0SVikram Kanigiri 
22072600226SYatharth Kochar #endif /* LOAD_IMAGE_V2 */
2214112bfa0SVikram Kanigiri 
2224ecca339SDan Handley /*******************************************************************************
2234ecca339SDan Handley  * Function & variable prototypes
2244ecca339SDan Handley  ******************************************************************************/
2254c0d0390SSoby Mathew size_t image_size(unsigned int image_id);
22672600226SYatharth Kochar 
22799c5ebafSSandrine Bailleux int is_mem_free(uintptr_t free_base, size_t free_size,
22899c5ebafSSandrine Bailleux 		uintptr_t addr, size_t size);
22999c5ebafSSandrine Bailleux 
23072600226SYatharth Kochar #if LOAD_IMAGE_V2
23172600226SYatharth Kochar 
23272600226SYatharth Kochar int load_image(unsigned int image_id, image_info_t *image_data);
23372600226SYatharth Kochar int load_auth_image(unsigned int image_id, image_info_t *image_data);
23472600226SYatharth Kochar 
23572600226SYatharth Kochar #else
23672600226SYatharth Kochar 
23772600226SYatharth Kochar uintptr_t page_align(uintptr_t, unsigned);
2388f55dfb4SSandrine Bailleux int load_image(meminfo_t *mem_layout,
23916948ae1SJuan Castillo 	       unsigned int image_id,
2401779ba6bSJuan Castillo 	       uintptr_t image_base,
2411779ba6bSJuan Castillo 	       image_info_t *image_data,
2421779ba6bSJuan Castillo 	       entry_point_info_t *entry_point_info);
2431779ba6bSJuan Castillo int load_auth_image(meminfo_t *mem_layout,
24472600226SYatharth Kochar 		    unsigned int image_id,
2451779ba6bSJuan Castillo 		    uintptr_t image_base,
2468f55dfb4SSandrine Bailleux 		    image_info_t *image_data,
2478f55dfb4SSandrine Bailleux 		    entry_point_info_t *entry_point_info);
2484c0d0390SSoby Mathew void reserve_mem(uintptr_t *free_base, size_t *free_size,
2494c0d0390SSoby Mathew 		uintptr_t addr, size_t size);
2508f55dfb4SSandrine Bailleux 
25172600226SYatharth Kochar #endif /* LOAD_IMAGE_V2 */
25272600226SYatharth Kochar 
25372600226SYatharth Kochar extern const char build_message[];
25472600226SYatharth Kochar extern const char version_string[];
25572600226SYatharth Kochar 
25668a68c92SSandrine Bailleux void print_entry_point_info(const entry_point_info_t *ep_info);
25768a68c92SSandrine Bailleux 
2584ecca339SDan Handley #endif /*__ASSEMBLY__*/
2594ecca339SDan Handley 
2604ecca339SDan Handley #endif /* __BL_COMMON_H__ */
261