352 lines · c
1/* SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) */2/*3 * Copyright (c) 2015-2021, Linaro Limited4 */5#ifndef _OPTEE_MSG_H6#define _OPTEE_MSG_H7 8#include <linux/bitops.h>9#include <linux/types.h>10 11/*12 * This file defines the OP-TEE message protocol (ABI) used to communicate13 * with an instance of OP-TEE running in secure world.14 *15 * This file is divided into two sections.16 * 1. Formatting of messages.17 * 2. Requests from normal world18 */19 20/*****************************************************************************21 * Part 1 - formatting of messages22 *****************************************************************************/23 24#define OPTEE_MSG_ATTR_TYPE_NONE 0x025#define OPTEE_MSG_ATTR_TYPE_VALUE_INPUT 0x126#define OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT 0x227#define OPTEE_MSG_ATTR_TYPE_VALUE_INOUT 0x328#define OPTEE_MSG_ATTR_TYPE_RMEM_INPUT 0x529#define OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT 0x630#define OPTEE_MSG_ATTR_TYPE_RMEM_INOUT 0x731#define OPTEE_MSG_ATTR_TYPE_FMEM_INPUT OPTEE_MSG_ATTR_TYPE_RMEM_INPUT32#define OPTEE_MSG_ATTR_TYPE_FMEM_OUTPUT OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT33#define OPTEE_MSG_ATTR_TYPE_FMEM_INOUT OPTEE_MSG_ATTR_TYPE_RMEM_INOUT34#define OPTEE_MSG_ATTR_TYPE_TMEM_INPUT 0x935#define OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT 0xa36#define OPTEE_MSG_ATTR_TYPE_TMEM_INOUT 0xb37 38#define OPTEE_MSG_ATTR_TYPE_MASK GENMASK(7, 0)39 40/*41 * Meta parameter to be absorbed by the Secure OS and not passed42 * to the Trusted Application.43 *44 * Currently only used with OPTEE_MSG_CMD_OPEN_SESSION.45 */46#define OPTEE_MSG_ATTR_META BIT(8)47 48/*49 * Pointer to a list of pages used to register user-defined SHM buffer.50 * Used with OPTEE_MSG_ATTR_TYPE_TMEM_*.51 * buf_ptr should point to the beginning of the buffer. Buffer will contain52 * list of page addresses. OP-TEE core can reconstruct contiguous buffer from53 * that page addresses list. Page addresses are stored as 64 bit values.54 * Last entry on a page should point to the next page of buffer.55 * Every entry in buffer should point to a 4k page beginning (12 least56 * significant bits must be equal to zero).57 *58 * 12 least significant bits of optee_msg_param.u.tmem.buf_ptr should hold59 * page offset of user buffer.60 *61 * So, entries should be placed like members of this structure:62 *63 * struct page_data {64 * uint64_t pages_array[OPTEE_MSG_NONCONTIG_PAGE_SIZE/sizeof(uint64_t) - 1];65 * uint64_t next_page_data;66 * };67 *68 * Structure is designed to exactly fit into the page size69 * OPTEE_MSG_NONCONTIG_PAGE_SIZE which is a standard 4KB page.70 *71 * The size of 4KB is chosen because this is the smallest page size for ARM72 * architectures. If REE uses larger pages, it should divide them to 4KB ones.73 */74#define OPTEE_MSG_ATTR_NONCONTIG BIT(9)75 76/*77 * Memory attributes for caching passed with temp memrefs. The actual value78 * used is defined outside the message protocol with the exception of79 * OPTEE_MSG_ATTR_CACHE_PREDEFINED which means the attributes already80 * defined for the memory range should be used. If optee_smc.h is used as81 * bearer of this protocol OPTEE_SMC_SHM_* is used for values.82 */83#define OPTEE_MSG_ATTR_CACHE_SHIFT 1684#define OPTEE_MSG_ATTR_CACHE_MASK GENMASK(2, 0)85#define OPTEE_MSG_ATTR_CACHE_PREDEFINED 086 87/*88 * Same values as TEE_LOGIN_* from TEE Internal API89 */90#define OPTEE_MSG_LOGIN_PUBLIC 0x0000000091#define OPTEE_MSG_LOGIN_USER 0x0000000192#define OPTEE_MSG_LOGIN_GROUP 0x0000000293#define OPTEE_MSG_LOGIN_APPLICATION 0x0000000494#define OPTEE_MSG_LOGIN_APPLICATION_USER 0x0000000595#define OPTEE_MSG_LOGIN_APPLICATION_GROUP 0x0000000696 97/*98 * Page size used in non-contiguous buffer entries99 */100#define OPTEE_MSG_NONCONTIG_PAGE_SIZE 4096101 102#define OPTEE_MSG_FMEM_INVALID_GLOBAL_ID 0xffffffffffffffff103 104/**105 * struct optee_msg_param_tmem - temporary memory reference parameter106 * @buf_ptr: Address of the buffer107 * @size: Size of the buffer108 * @shm_ref: Temporary shared memory reference, pointer to a struct tee_shm109 *110 * Secure and normal world communicates pointers as physical address111 * instead of the virtual address. This is because secure and normal world112 * have completely independent memory mapping. Normal world can even have a113 * hypervisor which need to translate the guest physical address (AKA IPA114 * in ARM documentation) to a real physical address before passing the115 * structure to secure world.116 */117struct optee_msg_param_tmem {118 u64 buf_ptr;119 u64 size;120 u64 shm_ref;121};122 123/**124 * struct optee_msg_param_rmem - registered memory reference parameter125 * @offs: Offset into shared memory reference126 * @size: Size of the buffer127 * @shm_ref: Shared memory reference, pointer to a struct tee_shm128 */129struct optee_msg_param_rmem {130 u64 offs;131 u64 size;132 u64 shm_ref;133};134 135/**136 * struct optee_msg_param_fmem - ffa memory reference parameter137 * @offs_lower: Lower bits of offset into shared memory reference138 * @offs_upper: Upper bits of offset into shared memory reference139 * @internal_offs: Internal offset into the first page of shared memory140 * reference141 * @size: Size of the buffer142 * @global_id: Global identifier of Shared memory143 */144struct optee_msg_param_fmem {145 u32 offs_low;146 u16 offs_high;147 u16 internal_offs;148 u64 size;149 u64 global_id;150};151 152/**153 * struct optee_msg_param_value - opaque value parameter154 *155 * Value parameters are passed unchecked between normal and secure world.156 */157struct optee_msg_param_value {158 u64 a;159 u64 b;160 u64 c;161};162 163/**164 * struct optee_msg_param - parameter used together with struct optee_msg_arg165 * @attr: attributes166 * @tmem: parameter by temporary memory reference167 * @rmem: parameter by registered memory reference168 * @fmem: parameter by ffa registered memory reference169 * @value: parameter by opaque value170 * @octets: parameter by octet string171 *172 * @attr & OPTEE_MSG_ATTR_TYPE_MASK indicates if tmem, rmem or value is used in173 * the union. OPTEE_MSG_ATTR_TYPE_VALUE_* indicates value or octets,174 * OPTEE_MSG_ATTR_TYPE_TMEM_* indicates @tmem and175 * OPTEE_MSG_ATTR_TYPE_RMEM_* or the alias PTEE_MSG_ATTR_TYPE_FMEM_* indicates176 * @rmem or @fmem depending on the conduit.177 * OPTEE_MSG_ATTR_TYPE_NONE indicates that none of the members are used.178 */179struct optee_msg_param {180 u64 attr;181 union {182 struct optee_msg_param_tmem tmem;183 struct optee_msg_param_rmem rmem;184 struct optee_msg_param_fmem fmem;185 struct optee_msg_param_value value;186 u8 octets[24];187 } u;188};189 190/**191 * struct optee_msg_arg - call argument192 * @cmd: Command, one of OPTEE_MSG_CMD_* or OPTEE_MSG_RPC_CMD_*193 * @func: Trusted Application function, specific to the Trusted Application,194 * used if cmd == OPTEE_MSG_CMD_INVOKE_COMMAND195 * @session: In parameter for all OPTEE_MSG_CMD_* except196 * OPTEE_MSG_CMD_OPEN_SESSION where it's an output parameter instead197 * @cancel_id: Cancellation id, a unique value to identify this request198 * @ret: return value199 * @ret_origin: origin of the return value200 * @num_params: number of parameters supplied to the OS Command201 * @params: the parameters supplied to the OS Command202 *203 * All normal calls to Trusted OS uses this struct. If cmd requires further204 * information than what these fields hold it can be passed as a parameter205 * tagged as meta (setting the OPTEE_MSG_ATTR_META bit in corresponding206 * attrs field). All parameters tagged as meta have to come first.207 */208struct optee_msg_arg {209 u32 cmd;210 u32 func;211 u32 session;212 u32 cancel_id;213 u32 pad;214 u32 ret;215 u32 ret_origin;216 u32 num_params;217 218 /* num_params tells the actual number of element in params */219 struct optee_msg_param params[];220};221 222/**223 * OPTEE_MSG_GET_ARG_SIZE - return size of struct optee_msg_arg224 *225 * @num_params: Number of parameters embedded in the struct optee_msg_arg226 *227 * Returns the size of the struct optee_msg_arg together with the number228 * of embedded parameters.229 */230#define OPTEE_MSG_GET_ARG_SIZE(num_params) \231 (sizeof(struct optee_msg_arg) + \232 sizeof(struct optee_msg_param) * (num_params))233 234/*****************************************************************************235 * Part 2 - requests from normal world236 *****************************************************************************/237 238/*239 * Return the following UID if using API specified in this file without240 * further extensions:241 * 384fb3e0-e7f8-11e3-af63-0002a5d5c51b.242 * Represented in 4 32-bit words in OPTEE_MSG_UID_0, OPTEE_MSG_UID_1,243 * OPTEE_MSG_UID_2, OPTEE_MSG_UID_3.244 *245 * In the case where the OP-TEE image is loaded by the kernel, this will246 * initially return an alternate UID to reflect that we are communicating with247 * the TF-A image loading service at that time instead of OP-TEE. That UID is:248 * a3fbeab1-1246-315d-c7c4-06b9c03cbea4.249 * Represented in 4 32-bit words in OPTEE_MSG_IMAGE_LOAD_UID_0,250 * OPTEE_MSG_IMAGE_LOAD_UID_1, OPTEE_MSG_IMAGE_LOAD_UID_2,251 * OPTEE_MSG_IMAGE_LOAD_UID_3.252 */253#define OPTEE_MSG_UID_0 0x384fb3e0254#define OPTEE_MSG_UID_1 0xe7f811e3255#define OPTEE_MSG_UID_2 0xaf630002256#define OPTEE_MSG_UID_3 0xa5d5c51b257#define OPTEE_MSG_IMAGE_LOAD_UID_0 0xa3fbeab1258#define OPTEE_MSG_IMAGE_LOAD_UID_1 0x1246315d259#define OPTEE_MSG_IMAGE_LOAD_UID_2 0xc7c406b9260#define OPTEE_MSG_IMAGE_LOAD_UID_3 0xc03cbea4261#define OPTEE_MSG_FUNCID_CALLS_UID 0xFF01262 263/*264 * Returns 2.0 if using API specified in this file without further265 * extensions. Represented in 2 32-bit words in OPTEE_MSG_REVISION_MAJOR266 * and OPTEE_MSG_REVISION_MINOR267 */268#define OPTEE_MSG_REVISION_MAJOR 2269#define OPTEE_MSG_REVISION_MINOR 0270#define OPTEE_MSG_FUNCID_CALLS_REVISION 0xFF03271 272/*273 * Get UUID of Trusted OS.274 *275 * Used by non-secure world to figure out which Trusted OS is installed.276 * Note that returned UUID is the UUID of the Trusted OS, not of the API.277 *278 * Returns UUID in 4 32-bit words in the same way as279 * OPTEE_MSG_FUNCID_CALLS_UID described above.280 */281#define OPTEE_MSG_OS_OPTEE_UUID_0 0x486178e0282#define OPTEE_MSG_OS_OPTEE_UUID_1 0xe7f811e3283#define OPTEE_MSG_OS_OPTEE_UUID_2 0xbc5e0002284#define OPTEE_MSG_OS_OPTEE_UUID_3 0xa5d5c51b285#define OPTEE_MSG_FUNCID_GET_OS_UUID 0x0000286 287/*288 * Get revision of Trusted OS.289 *290 * Used by non-secure world to figure out which version of the Trusted OS291 * is installed. Note that the returned revision is the revision of the292 * Trusted OS, not of the API.293 *294 * Returns revision in 2 32-bit words in the same way as295 * OPTEE_MSG_CALLS_REVISION described above.296 */297#define OPTEE_MSG_FUNCID_GET_OS_REVISION 0x0001298 299/*300 * Do a secure call with struct optee_msg_arg as argument301 * The OPTEE_MSG_CMD_* below defines what goes in struct optee_msg_arg::cmd302 *303 * OPTEE_MSG_CMD_OPEN_SESSION opens a session to a Trusted Application.304 * The first two parameters are tagged as meta, holding two value305 * parameters to pass the following information:306 * param[0].u.value.a-b uuid of Trusted Application307 * param[1].u.value.a-b uuid of Client308 * param[1].u.value.c Login class of client OPTEE_MSG_LOGIN_*309 *310 * OPTEE_MSG_CMD_INVOKE_COMMAND invokes a command a previously opened311 * session to a Trusted Application. struct optee_msg_arg::func is Trusted312 * Application function, specific to the Trusted Application.313 *314 * OPTEE_MSG_CMD_CLOSE_SESSION closes a previously opened session to315 * Trusted Application.316 *317 * OPTEE_MSG_CMD_CANCEL cancels a currently invoked command.318 *319 * OPTEE_MSG_CMD_REGISTER_SHM registers a shared memory reference. The320 * information is passed as:321 * [in] param[0].attr OPTEE_MSG_ATTR_TYPE_TMEM_INPUT322 * [| OPTEE_MSG_ATTR_NONCONTIG]323 * [in] param[0].u.tmem.buf_ptr physical address (of first fragment)324 * [in] param[0].u.tmem.size size (of first fragment)325 * [in] param[0].u.tmem.shm_ref holds shared memory reference326 *327 * OPTEE_MSG_CMD_UNREGISTER_SHM unregisters a previously registered shared328 * memory reference. The information is passed as:329 * [in] param[0].attr OPTEE_MSG_ATTR_TYPE_RMEM_INPUT330 * [in] param[0].u.rmem.shm_ref holds shared memory reference331 * [in] param[0].u.rmem.offs 0332 * [in] param[0].u.rmem.size 0333 *334 * OPTEE_MSG_CMD_DO_BOTTOM_HALF does the scheduled bottom half processing335 * of a driver.336 *337 * OPTEE_MSG_CMD_STOP_ASYNC_NOTIF informs secure world that from now is338 * normal world unable to process asynchronous notifications. Typically339 * used when the driver is shut down.340 */341#define OPTEE_MSG_CMD_OPEN_SESSION 0342#define OPTEE_MSG_CMD_INVOKE_COMMAND 1343#define OPTEE_MSG_CMD_CLOSE_SESSION 2344#define OPTEE_MSG_CMD_CANCEL 3345#define OPTEE_MSG_CMD_REGISTER_SHM 4346#define OPTEE_MSG_CMD_UNREGISTER_SHM 5347#define OPTEE_MSG_CMD_DO_BOTTOM_HALF 6348#define OPTEE_MSG_CMD_STOP_ASYNC_NOTIF 7349#define OPTEE_MSG_FUNCID_CALL_WITH_ARG 0x0004350 351#endif /* _OPTEE_MSG_H */352