xref: /rk3399_ARM-atf/include/common/bl_common.h (revision 167a935733a6e3e412b8ed6a60034d0d84895f2e)
14ecca339SDan Handley /*
24ecca339SDan Handley  * Copyright (c) 2013-2014, 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
364ecca339SDan Handley 
374ecca339SDan Handley #define UP	1
384ecca339SDan Handley #define DOWN	0
394ecca339SDan Handley 
404ecca339SDan Handley /*******************************************************************************
414ecca339SDan Handley  * Constants for loading images. When BLx wants to load BLy, it looks at a
424ecca339SDan Handley  * meminfo structure to find the extents of free memory. Then depending upon
434ecca339SDan Handley  * how it has been configured, it can either load BLy at the top or bottom of
444ecca339SDan Handley  * the free memory. These constants indicate the choice.
454ecca339SDan Handley  * TODO: Make this configurable while building the trusted firmware.
464ecca339SDan Handley  ******************************************************************************/
474ecca339SDan Handley #define TOP_LOAD	0x1
484ecca339SDan Handley #define BOT_LOAD	!TOP_LOAD
494ecca339SDan Handley #define LOAD_MASK	(1 << 0)
504ecca339SDan Handley 
514ecca339SDan Handley /******************************************************************************
524ecca339SDan Handley  * Opcode passed in x0 to tell next EL that we want to run an image.
534ecca339SDan Handley  * Corresponds to the function ID of the only SMC that the BL1 exception
544ecca339SDan Handley  * handlers service. That's why the chosen value is the first function ID of
554ecca339SDan Handley  * the ARM SMC64 range.
564ecca339SDan Handley  *****************************************************************************/
574ecca339SDan Handley #define RUN_IMAGE	0xC0000000
584ecca339SDan Handley 
5929fb905dSVikram Kanigiri /*******************************************************************************
6029fb905dSVikram Kanigiri  * Constants that allow assembler code to access members of and the
614112bfa0SVikram Kanigiri  * 'entry_point_info' structure at their correct offsets.
6229fb905dSVikram Kanigiri  ******************************************************************************/
634112bfa0SVikram Kanigiri #define ENTRY_POINT_INFO_PC_OFFSET	0x08
644112bfa0SVikram Kanigiri #define ENTRY_POINT_INFO_ARGS_OFFSET	0x18
654112bfa0SVikram Kanigiri 
66*167a9357SAndrew Thoelke #define PARAM_EP_SECURITY_MASK    0x1
674112bfa0SVikram Kanigiri #define GET_SECURITY_STATE(x) (x & PARAM_EP_SECURITY_MASK)
684112bfa0SVikram Kanigiri #define SET_SECURITY_STATE(x, security) \
694112bfa0SVikram Kanigiri 			((x) = ((x) & ~PARAM_EP_SECURITY_MASK) | (security))
704112bfa0SVikram Kanigiri 
71*167a9357SAndrew Thoelke #define EP_EE_MASK	0x2
72*167a9357SAndrew Thoelke #define EP_EE_LITTLE	0x0
73*167a9357SAndrew Thoelke #define EP_EE_BIG	0x2
74*167a9357SAndrew Thoelke #define EP_GET_EE(x) (x & EP_EE_MASK)
75*167a9357SAndrew Thoelke #define EP_SET_EE(x, ee) ((x) = ((x) & ~EP_EE_MASK) | (ee))
76*167a9357SAndrew Thoelke 
77*167a9357SAndrew Thoelke #define EP_ST_MASK	0x4
78*167a9357SAndrew Thoelke #define EP_ST_DISABLE	0x0
79*167a9357SAndrew Thoelke #define EP_ST_ENABLE	0x4
80*167a9357SAndrew Thoelke #define EP_GET_ST(x) (x & EP_ST_MASK)
81*167a9357SAndrew Thoelke #define EP_SET_ST(x, ee) ((x) = ((x) & ~EP_ST_MASK) | (ee))
82*167a9357SAndrew Thoelke 
834112bfa0SVikram Kanigiri #define PARAM_EP     0x01
844112bfa0SVikram Kanigiri #define PARAM_IMAGE_BINARY  0x02
854112bfa0SVikram Kanigiri #define PARAM_BL31       0x03
864112bfa0SVikram Kanigiri 
874112bfa0SVikram Kanigiri #define VERSION_1		0x01
884112bfa0SVikram Kanigiri 
894112bfa0SVikram Kanigiri #define SET_PARAM_HEAD(_p, _type, _ver, _attr) do { \
904112bfa0SVikram Kanigiri 	(_p)->h.type = (uint8_t)(_type); \
914112bfa0SVikram Kanigiri 	(_p)->h.version = (uint8_t)(_ver); \
924112bfa0SVikram Kanigiri 	(_p)->h.size = (uint16_t)sizeof(*_p); \
934112bfa0SVikram Kanigiri 	(_p)->h.attr = (uint32_t)(_attr) ; \
944112bfa0SVikram Kanigiri 	} while (0)
954ecca339SDan Handley 
964ecca339SDan Handley #ifndef __ASSEMBLY__
9797043ac9SDan Handley #include <cdefs.h> /* For __dead2 */
9829fb905dSVikram Kanigiri #include <cassert.h>
994112bfa0SVikram Kanigiri #include <stdint.h>
1004ecca339SDan Handley 
1014ecca339SDan Handley /*******************************************************************************
1024ecca339SDan Handley  * Structure used for telling the next BL how much of a particular type of
1034ecca339SDan Handley  * memory is available for its use and how much is already used.
1044ecca339SDan Handley  ******************************************************************************/
105fb037bfbSDan Handley typedef struct meminfo {
1064ecca339SDan Handley 	unsigned long total_base;
1074ecca339SDan Handley 	long total_size;
1084ecca339SDan Handley 	unsigned long free_base;
1094ecca339SDan Handley 	long free_size;
1104ecca339SDan Handley 	unsigned long attr;
1114ecca339SDan Handley 	unsigned long next;
112fb037bfbSDan Handley } meminfo_t;
1134ecca339SDan Handley 
114fb037bfbSDan Handley typedef struct aapcs64_params {
1154ecca339SDan Handley 	unsigned long arg0;
1164ecca339SDan Handley 	unsigned long arg1;
1174ecca339SDan Handley 	unsigned long arg2;
1184ecca339SDan Handley 	unsigned long arg3;
1194ecca339SDan Handley 	unsigned long arg4;
1204ecca339SDan Handley 	unsigned long arg5;
1214ecca339SDan Handley 	unsigned long arg6;
1224ecca339SDan Handley 	unsigned long arg7;
123fb037bfbSDan Handley } aapcs64_params_t;
1244ecca339SDan Handley 
1254112bfa0SVikram Kanigiri /***************************************************************************
1264112bfa0SVikram Kanigiri  * This structure provides version information and the size of the
1274112bfa0SVikram Kanigiri  * structure, attributes for the structure it represents
1284112bfa0SVikram Kanigiri  ***************************************************************************/
1294112bfa0SVikram Kanigiri typedef struct param_header {
1304112bfa0SVikram Kanigiri 	uint8_t type;		/* type of the structure */
1314112bfa0SVikram Kanigiri 	uint8_t version;    /* version of this structure */
1324112bfa0SVikram Kanigiri 	uint16_t size;      /* size of this structure in bytes */
1334112bfa0SVikram Kanigiri 	uint32_t attr;      /* attributes: unused bits SBZ */
1344112bfa0SVikram Kanigiri } param_header_t;
1354112bfa0SVikram Kanigiri 
1364112bfa0SVikram Kanigiri /*****************************************************************************
1374112bfa0SVikram Kanigiri  * This structure represents the superset of information needed while
1384112bfa0SVikram Kanigiri  * switching exception levels. The only two mechanisms to do so are
1394112bfa0SVikram Kanigiri  * ERET & SMC. Security state is indicated using bit zero of header
1404112bfa0SVikram Kanigiri  * attribute
1414112bfa0SVikram Kanigiri  * NOTE: BL1 expects entrypoint followed by spsr while processing
1424112bfa0SVikram Kanigiri  * SMC to jump to BL31 from the start of entry_point_info
1434112bfa0SVikram Kanigiri  *****************************************************************************/
1444112bfa0SVikram Kanigiri typedef struct entry_point_info {
1454112bfa0SVikram Kanigiri 	param_header_t h;
1464112bfa0SVikram Kanigiri 	uintptr_t pc;
1474112bfa0SVikram Kanigiri 	uint32_t spsr;
148fb037bfbSDan Handley 	aapcs64_params_t args;
1494112bfa0SVikram Kanigiri } entry_point_info_t;
1504112bfa0SVikram Kanigiri 
1514112bfa0SVikram Kanigiri /*****************************************************************************
1524112bfa0SVikram Kanigiri  * Image info binary provides information from the image loader that
1534112bfa0SVikram Kanigiri  * can be used by the firmware to manage available trusted RAM.
1544112bfa0SVikram Kanigiri  * More advanced firmware image formats can provide additional
1554112bfa0SVikram Kanigiri  * information that enables optimization or greater flexibility in the
1564112bfa0SVikram Kanigiri  * common firmware code
1574112bfa0SVikram Kanigiri  *****************************************************************************/
1584112bfa0SVikram Kanigiri typedef struct image_info {
1594112bfa0SVikram Kanigiri 	param_header_t h;
1604112bfa0SVikram Kanigiri 	uintptr_t image_base;   /* physical address of base of image */
1614112bfa0SVikram Kanigiri 	uint32_t image_size;    /* bytes read from image file */
1624112bfa0SVikram Kanigiri } image_info_t;
1634ecca339SDan Handley 
1644ecca339SDan Handley /*******************************************************************************
1654ecca339SDan Handley  * This structure represents the superset of information that can be passed to
1664ecca339SDan Handley  * BL31 e.g. while passing control to it from BL2. The BL32 parameters will be
1674112bfa0SVikram Kanigiri  * populated only if BL2 detects its presence. A pointer to a structure of this
1684112bfa0SVikram Kanigiri  * type should be passed in X3 to BL31's cold boot entrypoint
1694112bfa0SVikram Kanigiri  *
1704112bfa0SVikram Kanigiri  * Use of this structure and the X3 parameter is not mandatory: the BL3-1
1714112bfa0SVikram Kanigiri  * platform code can use other mechanisms to provide the necessary information
1724112bfa0SVikram Kanigiri  * about BL3-2 and BL3-3 to the common and SPD code.
1734112bfa0SVikram Kanigiri  *
1744112bfa0SVikram Kanigiri  * BL3-1 image information is mandatory if this structure is used. If either of
1754112bfa0SVikram Kanigiri  * the optional BL3-2 and BL3-3 image information is not provided, this is
1764112bfa0SVikram Kanigiri  * indicated by the respective image_info pointers being zero.
1774ecca339SDan Handley  ******************************************************************************/
1784112bfa0SVikram Kanigiri typedef struct bl31_params {
1794112bfa0SVikram Kanigiri 	param_header_t h;
1804112bfa0SVikram Kanigiri 	image_info_t *bl31_image_info;
1814112bfa0SVikram Kanigiri 	entry_point_info_t *bl32_ep_info;
1824112bfa0SVikram Kanigiri 	image_info_t *bl32_image_info;
1834112bfa0SVikram Kanigiri 	entry_point_info_t *bl33_ep_info;
1844112bfa0SVikram Kanigiri 	image_info_t *bl33_image_info;
1854112bfa0SVikram Kanigiri } bl31_params_t;
1864112bfa0SVikram Kanigiri 
1874112bfa0SVikram Kanigiri 
18829fb905dSVikram Kanigiri /*
1894112bfa0SVikram Kanigiri  * Compile time assertions related to the 'entry_point_info' structure to
19029fb905dSVikram Kanigiri  * ensure that the assembler and the compiler view of the offsets of
19129fb905dSVikram Kanigiri  * the structure members is the same.
19229fb905dSVikram Kanigiri  */
1934112bfa0SVikram Kanigiri CASSERT(ENTRY_POINT_INFO_PC_OFFSET ==
1944112bfa0SVikram Kanigiri 		__builtin_offsetof(entry_point_info_t, pc), \
19529fb905dSVikram Kanigiri 		assert_BL31_pc_offset_mismatch);
19629fb905dSVikram Kanigiri 
1974112bfa0SVikram Kanigiri CASSERT(ENTRY_POINT_INFO_ARGS_OFFSET == \
1984112bfa0SVikram Kanigiri 		__builtin_offsetof(entry_point_info_t, args), \
19929fb905dSVikram Kanigiri 		assert_BL31_args_offset_mismatch);
20029fb905dSVikram Kanigiri 
2014112bfa0SVikram Kanigiri CASSERT(sizeof(unsigned long) ==
2024112bfa0SVikram Kanigiri 		__builtin_offsetof(entry_point_info_t, spsr) - \
2034112bfa0SVikram Kanigiri 		__builtin_offsetof(entry_point_info_t, pc), \
20429fb905dSVikram Kanigiri 		assert_entrypoint_and_spsr_should_be_adjacent);
20529fb905dSVikram Kanigiri 
2064ecca339SDan Handley /*******************************************************************************
2074ecca339SDan Handley  * Function & variable prototypes
2084ecca339SDan Handley  ******************************************************************************/
209c6bc0710SDan Handley unsigned long page_align(unsigned long, unsigned);
210c6bc0710SDan Handley void change_security_state(unsigned int);
211c6bc0710SDan Handley unsigned long image_size(const char *);
212c6bc0710SDan Handley int load_image(meminfo_t *,
2134ecca339SDan Handley 		const char *,
2144ecca339SDan Handley 		unsigned int,
2154112bfa0SVikram Kanigiri 		unsigned long,
2164112bfa0SVikram Kanigiri 		image_info_t *,
2174112bfa0SVikram Kanigiri 		entry_point_info_t *);
2184ecca339SDan Handley extern const char build_message[];
2194ecca339SDan Handley 
2204ecca339SDan Handley #endif /*__ASSEMBLY__*/
2214ecca339SDan Handley 
2224ecca339SDan Handley #endif /* __BL_COMMON_H__ */
223