xref: /rk3399_ARM-atf/include/common/bl_common.h (revision 843ddee4aab78dfb940121f5ec28b52f7be5b28a)
14ecca339SDan Handley /*
2*843ddee4SYatharth Kochar  * Copyright (c) 2013-2016, 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 
344112bfa0SVikram Kanigiri #define SECURE		0x0
354112bfa0SVikram Kanigiri #define NON_SECURE	0x1
36d3280bebSJuan Castillo #define sec_state_is_valid(s) (((s) == SECURE) || ((s) == NON_SECURE))
374ecca339SDan Handley 
384ecca339SDan Handley #define UP	1
394ecca339SDan Handley #define DOWN	0
404ecca339SDan Handley 
414ecca339SDan Handley /*******************************************************************************
428f55dfb4SSandrine Bailleux  * Constants to identify the location of a memory region in a given memory
438f55dfb4SSandrine Bailleux  * layout.
444ecca339SDan Handley ******************************************************************************/
458f55dfb4SSandrine Bailleux #define TOP	0x1
468f55dfb4SSandrine Bailleux #define BOTTOM	!TOP
474ecca339SDan Handley 
4829fb905dSVikram Kanigiri /*******************************************************************************
4929fb905dSVikram Kanigiri  * Constants that allow assembler code to access members of and the
504112bfa0SVikram Kanigiri  * 'entry_point_info' structure at their correct offsets.
5129fb905dSVikram Kanigiri  ******************************************************************************/
524112bfa0SVikram Kanigiri #define ENTRY_POINT_INFO_PC_OFFSET	0x08
534112bfa0SVikram Kanigiri #define ENTRY_POINT_INFO_ARGS_OFFSET	0x18
544112bfa0SVikram Kanigiri 
557baff11fSYatharth Kochar /* The following are used to set/get image attributes. */
567baff11fSYatharth Kochar #define PARAM_EP_SECURITY_MASK		(0x1)
577baff11fSYatharth Kochar 
584112bfa0SVikram Kanigiri #define GET_SECURITY_STATE(x) (x & PARAM_EP_SECURITY_MASK)
594112bfa0SVikram Kanigiri #define SET_SECURITY_STATE(x, security) \
604112bfa0SVikram Kanigiri 			((x) = ((x) & ~PARAM_EP_SECURITY_MASK) | (security))
614112bfa0SVikram Kanigiri 
627baff11fSYatharth Kochar 
637baff11fSYatharth Kochar /*
647baff11fSYatharth Kochar  * The following are used for image state attributes.
657baff11fSYatharth Kochar  * Image can only be in one of the following state.
667baff11fSYatharth Kochar  */
677baff11fSYatharth Kochar #define IMAGE_STATE_RESET			0
687baff11fSYatharth Kochar #define IMAGE_STATE_COPIED			1
697baff11fSYatharth Kochar #define IMAGE_STATE_COPYING			2
707baff11fSYatharth Kochar #define IMAGE_STATE_AUTHENTICATED		3
717baff11fSYatharth Kochar #define IMAGE_STATE_EXECUTED			4
727baff11fSYatharth Kochar #define IMAGE_STATE_INTERRUPTED			5
737baff11fSYatharth Kochar 
74167a9357SAndrew Thoelke #define EP_EE_MASK	0x2
75167a9357SAndrew Thoelke #define EP_EE_LITTLE	0x0
76167a9357SAndrew Thoelke #define EP_EE_BIG	0x2
77167a9357SAndrew Thoelke #define EP_GET_EE(x) (x & EP_EE_MASK)
78167a9357SAndrew Thoelke #define EP_SET_EE(x, ee) ((x) = ((x) & ~EP_EE_MASK) | (ee))
79167a9357SAndrew Thoelke 
80167a9357SAndrew Thoelke #define EP_ST_MASK	0x4
81167a9357SAndrew Thoelke #define EP_ST_DISABLE	0x0
82167a9357SAndrew Thoelke #define EP_ST_ENABLE	0x4
83167a9357SAndrew Thoelke #define EP_GET_ST(x) (x & EP_ST_MASK)
84167a9357SAndrew Thoelke #define EP_SET_ST(x, ee) ((x) = ((x) & ~EP_ST_MASK) | (ee))
85167a9357SAndrew Thoelke 
86*843ddee4SYatharth Kochar #define EP_EXE_MASK	0x8
87*843ddee4SYatharth Kochar #define NON_EXECUTABLE	0x0
88*843ddee4SYatharth Kochar #define EXECUTABLE	0x8
89*843ddee4SYatharth Kochar #define EP_GET_EXE(x) (x & EP_EXE_MASK)
90*843ddee4SYatharth Kochar #define EP_SET_EXE(x, ee) ((x) = ((x) & ~EP_EXE_MASK) | (ee))
91*843ddee4SYatharth Kochar 
924112bfa0SVikram Kanigiri #define PARAM_EP		0x01
934112bfa0SVikram Kanigiri #define PARAM_IMAGE_BINARY	0x02
944112bfa0SVikram Kanigiri #define PARAM_BL31		0x03
954112bfa0SVikram Kanigiri 
964112bfa0SVikram Kanigiri #define VERSION_1	0x01
974112bfa0SVikram Kanigiri 
987baff11fSYatharth Kochar #define INVALID_IMAGE_ID		(0xFFFFFFFF)
997baff11fSYatharth Kochar 
1004112bfa0SVikram Kanigiri #define SET_PARAM_HEAD(_p, _type, _ver, _attr) do { \
1014112bfa0SVikram Kanigiri 	(_p)->h.type = (uint8_t)(_type); \
1024112bfa0SVikram Kanigiri 	(_p)->h.version = (uint8_t)(_ver); \
1034112bfa0SVikram Kanigiri 	(_p)->h.size = (uint16_t)sizeof(*_p); \
1044112bfa0SVikram Kanigiri 	(_p)->h.attr = (uint32_t)(_attr) ; \
1054112bfa0SVikram Kanigiri 	} while (0)
1064ecca339SDan Handley 
107*843ddee4SYatharth Kochar /* Following is used for populating structure members statically. */
108*843ddee4SYatharth Kochar #define SET_STATIC_PARAM_HEAD(_p, _type, _ver, _p_type, _attr)	\
109*843ddee4SYatharth Kochar 	._p.h.type = (uint8_t)(_type), \
110*843ddee4SYatharth Kochar 	._p.h.version = (uint8_t)(_ver), \
111*843ddee4SYatharth Kochar 	._p.h.size = (uint16_t)sizeof(_p_type), \
112*843ddee4SYatharth Kochar 	._p.h.attr = (uint32_t)(_attr)
113*843ddee4SYatharth Kochar 
114*843ddee4SYatharth Kochar 
115bbf8f6f9SYatharth Kochar /*******************************************************************************
116bbf8f6f9SYatharth Kochar  * Constants to indicate type of exception to the common exception handler.
117bbf8f6f9SYatharth Kochar  ******************************************************************************/
118bbf8f6f9SYatharth Kochar #define SYNC_EXCEPTION_SP_EL0		0x0
119bbf8f6f9SYatharth Kochar #define IRQ_SP_EL0			0x1
120bbf8f6f9SYatharth Kochar #define FIQ_SP_EL0			0x2
121bbf8f6f9SYatharth Kochar #define SERROR_SP_EL0			0x3
122bbf8f6f9SYatharth Kochar #define SYNC_EXCEPTION_SP_ELX		0x4
123bbf8f6f9SYatharth Kochar #define IRQ_SP_ELX			0x5
124bbf8f6f9SYatharth Kochar #define FIQ_SP_ELX			0x6
125bbf8f6f9SYatharth Kochar #define SERROR_SP_ELX			0x7
126bbf8f6f9SYatharth Kochar #define SYNC_EXCEPTION_AARCH64		0x8
127bbf8f6f9SYatharth Kochar #define IRQ_AARCH64			0x9
128bbf8f6f9SYatharth Kochar #define FIQ_AARCH64			0xa
129bbf8f6f9SYatharth Kochar #define SERROR_AARCH64			0xb
130bbf8f6f9SYatharth Kochar #define SYNC_EXCEPTION_AARCH32		0xc
131bbf8f6f9SYatharth Kochar #define IRQ_AARCH32			0xd
132bbf8f6f9SYatharth Kochar #define FIQ_AARCH32			0xe
133bbf8f6f9SYatharth Kochar #define SERROR_AARCH32			0xf
134bbf8f6f9SYatharth Kochar 
1354ecca339SDan Handley #ifndef __ASSEMBLY__
13697043ac9SDan Handley #include <cdefs.h> /* For __dead2 */
13729fb905dSVikram Kanigiri #include <cassert.h>
1384112bfa0SVikram Kanigiri #include <stdint.h>
1398f55dfb4SSandrine Bailleux #include <stddef.h>
1404ecca339SDan Handley 
141a7e98ad5SVikram Kanigiri #define ARRAY_SIZE(a)	(sizeof(a) / sizeof((a)[0]))
142a7e98ad5SVikram Kanigiri 
14390b3a6acSDan Handley /*
14490b3a6acSDan Handley  * Declarations of linker defined symbols to help determine memory layout of
14590b3a6acSDan Handley  * BL images
14690b3a6acSDan Handley  */
14790b3a6acSDan Handley extern unsigned long __RO_START__;
14890b3a6acSDan Handley extern unsigned long __RO_END__;
14990b3a6acSDan Handley #if IMAGE_BL2
15090b3a6acSDan Handley extern unsigned long __BL2_END__;
1519003fa0bSYatharth Kochar #elif IMAGE_BL2U
1529003fa0bSYatharth Kochar extern unsigned long __BL2U_END__;
15390b3a6acSDan Handley #elif IMAGE_BL31
15490b3a6acSDan Handley extern unsigned long __BL31_END__;
15590b3a6acSDan Handley #elif IMAGE_BL32
15690b3a6acSDan Handley extern unsigned long __BL32_END__;
15790b3a6acSDan Handley #endif /* IMAGE_BLX */
15890b3a6acSDan Handley 
15990b3a6acSDan Handley #if USE_COHERENT_MEM
16090b3a6acSDan Handley extern unsigned long __COHERENT_RAM_START__;
16190b3a6acSDan Handley extern unsigned long __COHERENT_RAM_END__;
16290b3a6acSDan Handley #endif
16390b3a6acSDan Handley 
16490b3a6acSDan Handley 
1654ecca339SDan Handley /*******************************************************************************
1664ecca339SDan Handley  * Structure used for telling the next BL how much of a particular type of
1674ecca339SDan Handley  * memory is available for its use and how much is already used.
1684ecca339SDan Handley  ******************************************************************************/
169fb037bfbSDan Handley typedef struct meminfo {
1708f55dfb4SSandrine Bailleux 	uint64_t total_base;
1718f55dfb4SSandrine Bailleux 	size_t total_size;
1728f55dfb4SSandrine Bailleux 	uint64_t free_base;
1738f55dfb4SSandrine Bailleux 	size_t free_size;
174fb037bfbSDan Handley } meminfo_t;
1754ecca339SDan Handley 
176fb037bfbSDan Handley typedef struct aapcs64_params {
1774ecca339SDan Handley 	unsigned long arg0;
1784ecca339SDan Handley 	unsigned long arg1;
1794ecca339SDan Handley 	unsigned long arg2;
1804ecca339SDan Handley 	unsigned long arg3;
1814ecca339SDan Handley 	unsigned long arg4;
1824ecca339SDan Handley 	unsigned long arg5;
1834ecca339SDan Handley 	unsigned long arg6;
1844ecca339SDan Handley 	unsigned long arg7;
185fb037bfbSDan Handley } aapcs64_params_t;
1864ecca339SDan Handley 
1874112bfa0SVikram Kanigiri /***************************************************************************
1884112bfa0SVikram Kanigiri  * This structure provides version information and the size of the
1894112bfa0SVikram Kanigiri  * structure, attributes for the structure it represents
1904112bfa0SVikram Kanigiri  ***************************************************************************/
1914112bfa0SVikram Kanigiri typedef struct param_header {
1924112bfa0SVikram Kanigiri 	uint8_t type;		/* type of the structure */
1934112bfa0SVikram Kanigiri 	uint8_t version;    /* version of this structure */
1944112bfa0SVikram Kanigiri 	uint16_t size;      /* size of this structure in bytes */
1954112bfa0SVikram Kanigiri 	uint32_t attr;      /* attributes: unused bits SBZ */
1964112bfa0SVikram Kanigiri } param_header_t;
1974112bfa0SVikram Kanigiri 
1984112bfa0SVikram Kanigiri /*****************************************************************************
1994112bfa0SVikram Kanigiri  * This structure represents the superset of information needed while
2004112bfa0SVikram Kanigiri  * switching exception levels. The only two mechanisms to do so are
2014112bfa0SVikram Kanigiri  * ERET & SMC. Security state is indicated using bit zero of header
2024112bfa0SVikram Kanigiri  * attribute
203b313d755SSoby Mathew  * NOTE: BL1 expects entrypoint followed by spsr at an offset from the start
204b313d755SSoby Mathew  * of this structure defined by the macro `ENTRY_POINT_INFO_PC_OFFSET` while
205b313d755SSoby Mathew  * processing SMC to jump to BL31.
2064112bfa0SVikram Kanigiri  *****************************************************************************/
2074112bfa0SVikram Kanigiri typedef struct entry_point_info {
2084112bfa0SVikram Kanigiri 	param_header_t h;
2094112bfa0SVikram Kanigiri 	uintptr_t pc;
2104112bfa0SVikram Kanigiri 	uint32_t spsr;
211fb037bfbSDan Handley 	aapcs64_params_t args;
2124112bfa0SVikram Kanigiri } entry_point_info_t;
2134112bfa0SVikram Kanigiri 
2144112bfa0SVikram Kanigiri /*****************************************************************************
2154112bfa0SVikram Kanigiri  * Image info binary provides information from the image loader that
2164112bfa0SVikram Kanigiri  * can be used by the firmware to manage available trusted RAM.
2174112bfa0SVikram Kanigiri  * More advanced firmware image formats can provide additional
2184112bfa0SVikram Kanigiri  * information that enables optimization or greater flexibility in the
2194112bfa0SVikram Kanigiri  * common firmware code
2204112bfa0SVikram Kanigiri  *****************************************************************************/
2214112bfa0SVikram Kanigiri typedef struct image_info {
2224112bfa0SVikram Kanigiri 	param_header_t h;
2234112bfa0SVikram Kanigiri 	uintptr_t image_base;   /* physical address of base of image */
2244112bfa0SVikram Kanigiri 	uint32_t image_size;    /* bytes read from image file */
2254112bfa0SVikram Kanigiri } image_info_t;
2264ecca339SDan Handley 
2277baff11fSYatharth Kochar /*****************************************************************************
2287baff11fSYatharth Kochar  * The image descriptor struct definition.
2297baff11fSYatharth Kochar  *****************************************************************************/
2307baff11fSYatharth Kochar typedef struct image_desc {
2317baff11fSYatharth Kochar 	/* Contains unique image id for the image. */
2327baff11fSYatharth Kochar 	unsigned int image_id;
2337baff11fSYatharth Kochar 	/*
2347baff11fSYatharth Kochar 	 * This member contains Image state information.
2357baff11fSYatharth Kochar 	 * Refer IMAGE_STATE_XXX defined above.
2367baff11fSYatharth Kochar 	 */
2377baff11fSYatharth Kochar 	unsigned int state;
238*843ddee4SYatharth Kochar 	uint32_t copied_size;	/* image size copied in blocks */
239b313d755SSoby Mathew 	image_info_t image_info;
240b313d755SSoby Mathew 	entry_point_info_t ep_info;
2417baff11fSYatharth Kochar } image_desc_t;
2427baff11fSYatharth Kochar 
2434ecca339SDan Handley /*******************************************************************************
2444ecca339SDan Handley  * This structure represents the superset of information that can be passed to
2454ecca339SDan Handley  * BL31 e.g. while passing control to it from BL2. The BL32 parameters will be
2464112bfa0SVikram Kanigiri  * populated only if BL2 detects its presence. A pointer to a structure of this
247d178637dSJuan Castillo  * type should be passed in X0 to BL31's cold boot entrypoint.
2484112bfa0SVikram Kanigiri  *
249d178637dSJuan Castillo  * Use of this structure and the X0 parameter is not mandatory: the BL31
2504112bfa0SVikram Kanigiri  * platform code can use other mechanisms to provide the necessary information
251d178637dSJuan Castillo  * about BL32 and BL33 to the common and SPD code.
2524112bfa0SVikram Kanigiri  *
253d178637dSJuan Castillo  * BL31 image information is mandatory if this structure is used. If either of
254d178637dSJuan Castillo  * the optional BL32 and BL33 image information is not provided, this is
2554112bfa0SVikram Kanigiri  * indicated by the respective image_info pointers being zero.
2564ecca339SDan Handley  ******************************************************************************/
2574112bfa0SVikram Kanigiri typedef struct bl31_params {
2584112bfa0SVikram Kanigiri 	param_header_t h;
2594112bfa0SVikram Kanigiri 	image_info_t *bl31_image_info;
2604112bfa0SVikram Kanigiri 	entry_point_info_t *bl32_ep_info;
2614112bfa0SVikram Kanigiri 	image_info_t *bl32_image_info;
2624112bfa0SVikram Kanigiri 	entry_point_info_t *bl33_ep_info;
2634112bfa0SVikram Kanigiri 	image_info_t *bl33_image_info;
2644112bfa0SVikram Kanigiri } bl31_params_t;
2654112bfa0SVikram Kanigiri 
2664112bfa0SVikram Kanigiri 
26729fb905dSVikram Kanigiri /*
2684112bfa0SVikram Kanigiri  * Compile time assertions related to the 'entry_point_info' structure to
26929fb905dSVikram Kanigiri  * ensure that the assembler and the compiler view of the offsets of
27029fb905dSVikram Kanigiri  * the structure members is the same.
27129fb905dSVikram Kanigiri  */
2724112bfa0SVikram Kanigiri CASSERT(ENTRY_POINT_INFO_PC_OFFSET ==
2734112bfa0SVikram Kanigiri 		__builtin_offsetof(entry_point_info_t, pc), \
27429fb905dSVikram Kanigiri 		assert_BL31_pc_offset_mismatch);
27529fb905dSVikram Kanigiri 
2764112bfa0SVikram Kanigiri CASSERT(ENTRY_POINT_INFO_ARGS_OFFSET == \
2774112bfa0SVikram Kanigiri 		__builtin_offsetof(entry_point_info_t, args), \
27829fb905dSVikram Kanigiri 		assert_BL31_args_offset_mismatch);
27929fb905dSVikram Kanigiri 
2804112bfa0SVikram Kanigiri CASSERT(sizeof(unsigned long) ==
2814112bfa0SVikram Kanigiri 		__builtin_offsetof(entry_point_info_t, spsr) - \
2824112bfa0SVikram Kanigiri 		__builtin_offsetof(entry_point_info_t, pc), \
28329fb905dSVikram Kanigiri 		assert_entrypoint_and_spsr_should_be_adjacent);
28429fb905dSVikram Kanigiri 
2854ecca339SDan Handley /*******************************************************************************
2864ecca339SDan Handley  * Function & variable prototypes
2874ecca339SDan Handley  ******************************************************************************/
288c6bc0710SDan Handley unsigned long page_align(unsigned long, unsigned);
28916948ae1SJuan Castillo unsigned long image_size(unsigned int image_id);
2908f55dfb4SSandrine Bailleux int load_image(meminfo_t *mem_layout,
29116948ae1SJuan Castillo 	       unsigned int image_id,
2921779ba6bSJuan Castillo 	       uintptr_t image_base,
2931779ba6bSJuan Castillo 	       image_info_t *image_data,
2941779ba6bSJuan Castillo 	       entry_point_info_t *entry_point_info);
2951779ba6bSJuan Castillo int load_auth_image(meminfo_t *mem_layout,
2961779ba6bSJuan Castillo 		    unsigned int image_name,
2971779ba6bSJuan Castillo 		    uintptr_t image_base,
2988f55dfb4SSandrine Bailleux 		    image_info_t *image_data,
2998f55dfb4SSandrine Bailleux 		    entry_point_info_t *entry_point_info);
3004ecca339SDan Handley extern const char build_message[];
301aaa3e722SJuan Castillo extern const char version_string[];
3024ecca339SDan Handley 
3038f55dfb4SSandrine Bailleux void reserve_mem(uint64_t *free_base, size_t *free_size,
3048f55dfb4SSandrine Bailleux 		uint64_t addr, size_t size);
3058f55dfb4SSandrine Bailleux 
30668a68c92SSandrine Bailleux void print_entry_point_info(const entry_point_info_t *ep_info);
30768a68c92SSandrine Bailleux 
3084ecca339SDan Handley #endif /*__ASSEMBLY__*/
3094ecca339SDan Handley 
3104ecca339SDan Handley #endif /* __BL_COMMON_H__ */
311