Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) */
0002 /*
0003  * Copyright (c) 2015-2021, Linaro Limited
0004  */
0005 #ifndef _OPTEE_MSG_H
0006 #define _OPTEE_MSG_H
0007 
0008 #include <linux/bitops.h>
0009 #include <linux/types.h>
0010 
0011 /*
0012  * This file defines the OP-TEE message protocol (ABI) used to communicate
0013  * with an instance of OP-TEE running in secure world.
0014  *
0015  * This file is divided into two sections.
0016  * 1. Formatting of messages.
0017  * 2. Requests from normal world
0018  */
0019 
0020 /*****************************************************************************
0021  * Part 1 - formatting of messages
0022  *****************************************************************************/
0023 
0024 #define OPTEE_MSG_ATTR_TYPE_NONE        0x0
0025 #define OPTEE_MSG_ATTR_TYPE_VALUE_INPUT     0x1
0026 #define OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT    0x2
0027 #define OPTEE_MSG_ATTR_TYPE_VALUE_INOUT     0x3
0028 #define OPTEE_MSG_ATTR_TYPE_RMEM_INPUT      0x5
0029 #define OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT     0x6
0030 #define OPTEE_MSG_ATTR_TYPE_RMEM_INOUT      0x7
0031 #define OPTEE_MSG_ATTR_TYPE_FMEM_INPUT      OPTEE_MSG_ATTR_TYPE_RMEM_INPUT
0032 #define OPTEE_MSG_ATTR_TYPE_FMEM_OUTPUT     OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT
0033 #define OPTEE_MSG_ATTR_TYPE_FMEM_INOUT      OPTEE_MSG_ATTR_TYPE_RMEM_INOUT
0034 #define OPTEE_MSG_ATTR_TYPE_TMEM_INPUT      0x9
0035 #define OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT     0xa
0036 #define OPTEE_MSG_ATTR_TYPE_TMEM_INOUT      0xb
0037 
0038 #define OPTEE_MSG_ATTR_TYPE_MASK        GENMASK(7, 0)
0039 
0040 /*
0041  * Meta parameter to be absorbed by the Secure OS and not passed
0042  * to the Trusted Application.
0043  *
0044  * Currently only used with OPTEE_MSG_CMD_OPEN_SESSION.
0045  */
0046 #define OPTEE_MSG_ATTR_META         BIT(8)
0047 
0048 /*
0049  * Pointer to a list of pages used to register user-defined SHM buffer.
0050  * Used with OPTEE_MSG_ATTR_TYPE_TMEM_*.
0051  * buf_ptr should point to the beginning of the buffer. Buffer will contain
0052  * list of page addresses. OP-TEE core can reconstruct contiguous buffer from
0053  * that page addresses list. Page addresses are stored as 64 bit values.
0054  * Last entry on a page should point to the next page of buffer.
0055  * Every entry in buffer should point to a 4k page beginning (12 least
0056  * significant bits must be equal to zero).
0057  *
0058  * 12 least significant bits of optee_msg_param.u.tmem.buf_ptr should hold
0059  * page offset of user buffer.
0060  *
0061  * So, entries should be placed like members of this structure:
0062  *
0063  * struct page_data {
0064  *   uint64_t pages_array[OPTEE_MSG_NONCONTIG_PAGE_SIZE/sizeof(uint64_t) - 1];
0065  *   uint64_t next_page_data;
0066  * };
0067  *
0068  * Structure is designed to exactly fit into the page size
0069  * OPTEE_MSG_NONCONTIG_PAGE_SIZE which is a standard 4KB page.
0070  *
0071  * The size of 4KB is chosen because this is the smallest page size for ARM
0072  * architectures. If REE uses larger pages, it should divide them to 4KB ones.
0073  */
0074 #define OPTEE_MSG_ATTR_NONCONTIG        BIT(9)
0075 
0076 /*
0077  * Memory attributes for caching passed with temp memrefs. The actual value
0078  * used is defined outside the message protocol with the exception of
0079  * OPTEE_MSG_ATTR_CACHE_PREDEFINED which means the attributes already
0080  * defined for the memory range should be used. If optee_smc.h is used as
0081  * bearer of this protocol OPTEE_SMC_SHM_* is used for values.
0082  */
0083 #define OPTEE_MSG_ATTR_CACHE_SHIFT      16
0084 #define OPTEE_MSG_ATTR_CACHE_MASK       GENMASK(2, 0)
0085 #define OPTEE_MSG_ATTR_CACHE_PREDEFINED     0
0086 
0087 /*
0088  * Same values as TEE_LOGIN_* from TEE Internal API
0089  */
0090 #define OPTEE_MSG_LOGIN_PUBLIC          0x00000000
0091 #define OPTEE_MSG_LOGIN_USER            0x00000001
0092 #define OPTEE_MSG_LOGIN_GROUP           0x00000002
0093 #define OPTEE_MSG_LOGIN_APPLICATION     0x00000004
0094 #define OPTEE_MSG_LOGIN_APPLICATION_USER    0x00000005
0095 #define OPTEE_MSG_LOGIN_APPLICATION_GROUP   0x00000006
0096 
0097 /*
0098  * Page size used in non-contiguous buffer entries
0099  */
0100 #define OPTEE_MSG_NONCONTIG_PAGE_SIZE       4096
0101 
0102 #define OPTEE_MSG_FMEM_INVALID_GLOBAL_ID    0xffffffffffffffff
0103 
0104 /**
0105  * struct optee_msg_param_tmem - temporary memory reference parameter
0106  * @buf_ptr:    Address of the buffer
0107  * @size:   Size of the buffer
0108  * @shm_ref:    Temporary shared memory reference, pointer to a struct tee_shm
0109  *
0110  * Secure and normal world communicates pointers as physical address
0111  * instead of the virtual address. This is because secure and normal world
0112  * have completely independent memory mapping. Normal world can even have a
0113  * hypervisor which need to translate the guest physical address (AKA IPA
0114  * in ARM documentation) to a real physical address before passing the
0115  * structure to secure world.
0116  */
0117 struct optee_msg_param_tmem {
0118     u64 buf_ptr;
0119     u64 size;
0120     u64 shm_ref;
0121 };
0122 
0123 /**
0124  * struct optee_msg_param_rmem - registered memory reference parameter
0125  * @offs:   Offset into shared memory reference
0126  * @size:   Size of the buffer
0127  * @shm_ref:    Shared memory reference, pointer to a struct tee_shm
0128  */
0129 struct optee_msg_param_rmem {
0130     u64 offs;
0131     u64 size;
0132     u64 shm_ref;
0133 };
0134 
0135 /**
0136  * struct optee_msg_param_fmem - ffa memory reference parameter
0137  * @offs_lower:    Lower bits of offset into shared memory reference
0138  * @offs_upper:    Upper bits of offset into shared memory reference
0139  * @internal_offs: Internal offset into the first page of shared memory
0140  *         reference
0141  * @size:      Size of the buffer
0142  * @global_id:     Global identifier of Shared memory
0143  */
0144 struct optee_msg_param_fmem {
0145     u32 offs_low;
0146     u16 offs_high;
0147     u16 internal_offs;
0148     u64 size;
0149     u64 global_id;
0150 };
0151 
0152 /**
0153  * struct optee_msg_param_value - opaque value parameter
0154  *
0155  * Value parameters are passed unchecked between normal and secure world.
0156  */
0157 struct optee_msg_param_value {
0158     u64 a;
0159     u64 b;
0160     u64 c;
0161 };
0162 
0163 /**
0164  * struct optee_msg_param - parameter used together with struct optee_msg_arg
0165  * @attr:   attributes
0166  * @tmem:   parameter by temporary memory reference
0167  * @rmem:   parameter by registered memory reference
0168  * @fmem:   parameter by ffa registered memory reference
0169  * @value:  parameter by opaque value
0170  * @octets: parameter by octet string
0171  *
0172  * @attr & OPTEE_MSG_ATTR_TYPE_MASK indicates if tmem, rmem or value is used in
0173  * the union. OPTEE_MSG_ATTR_TYPE_VALUE_* indicates value or octets,
0174  * OPTEE_MSG_ATTR_TYPE_TMEM_* indicates @tmem and
0175  * OPTEE_MSG_ATTR_TYPE_RMEM_* or the alias PTEE_MSG_ATTR_TYPE_FMEM_* indicates
0176  * @rmem or @fmem depending on the conduit.
0177  * OPTEE_MSG_ATTR_TYPE_NONE indicates that none of the members are used.
0178  */
0179 struct optee_msg_param {
0180     u64 attr;
0181     union {
0182         struct optee_msg_param_tmem tmem;
0183         struct optee_msg_param_rmem rmem;
0184         struct optee_msg_param_fmem fmem;
0185         struct optee_msg_param_value value;
0186         u8 octets[24];
0187     } u;
0188 };
0189 
0190 /**
0191  * struct optee_msg_arg - call argument
0192  * @cmd: Command, one of OPTEE_MSG_CMD_* or OPTEE_MSG_RPC_CMD_*
0193  * @func: Trusted Application function, specific to the Trusted Application,
0194  *       used if cmd == OPTEE_MSG_CMD_INVOKE_COMMAND
0195  * @session: In parameter for all OPTEE_MSG_CMD_* except
0196  *       OPTEE_MSG_CMD_OPEN_SESSION where it's an output parameter instead
0197  * @cancel_id: Cancellation id, a unique value to identify this request
0198  * @ret: return value
0199  * @ret_origin: origin of the return value
0200  * @num_params: number of parameters supplied to the OS Command
0201  * @params: the parameters supplied to the OS Command
0202  *
0203  * All normal calls to Trusted OS uses this struct. If cmd requires further
0204  * information than what these fields hold it can be passed as a parameter
0205  * tagged as meta (setting the OPTEE_MSG_ATTR_META bit in corresponding
0206  * attrs field). All parameters tagged as meta have to come first.
0207  */
0208 struct optee_msg_arg {
0209     u32 cmd;
0210     u32 func;
0211     u32 session;
0212     u32 cancel_id;
0213     u32 pad;
0214     u32 ret;
0215     u32 ret_origin;
0216     u32 num_params;
0217 
0218     /* num_params tells the actual number of element in params */
0219     struct optee_msg_param params[];
0220 };
0221 
0222 /**
0223  * OPTEE_MSG_GET_ARG_SIZE - return size of struct optee_msg_arg
0224  *
0225  * @num_params: Number of parameters embedded in the struct optee_msg_arg
0226  *
0227  * Returns the size of the struct optee_msg_arg together with the number
0228  * of embedded parameters.
0229  */
0230 #define OPTEE_MSG_GET_ARG_SIZE(num_params) \
0231     (sizeof(struct optee_msg_arg) + \
0232      sizeof(struct optee_msg_param) * (num_params))
0233 
0234 /*****************************************************************************
0235  * Part 2 - requests from normal world
0236  *****************************************************************************/
0237 
0238 /*
0239  * Return the following UID if using API specified in this file without
0240  * further extensions:
0241  * 384fb3e0-e7f8-11e3-af63-0002a5d5c51b.
0242  * Represented in 4 32-bit words in OPTEE_MSG_UID_0, OPTEE_MSG_UID_1,
0243  * OPTEE_MSG_UID_2, OPTEE_MSG_UID_3.
0244  */
0245 #define OPTEE_MSG_UID_0         0x384fb3e0
0246 #define OPTEE_MSG_UID_1         0xe7f811e3
0247 #define OPTEE_MSG_UID_2         0xaf630002
0248 #define OPTEE_MSG_UID_3         0xa5d5c51b
0249 #define OPTEE_MSG_FUNCID_CALLS_UID  0xFF01
0250 
0251 /*
0252  * Returns 2.0 if using API specified in this file without further
0253  * extensions. Represented in 2 32-bit words in OPTEE_MSG_REVISION_MAJOR
0254  * and OPTEE_MSG_REVISION_MINOR
0255  */
0256 #define OPTEE_MSG_REVISION_MAJOR    2
0257 #define OPTEE_MSG_REVISION_MINOR    0
0258 #define OPTEE_MSG_FUNCID_CALLS_REVISION 0xFF03
0259 
0260 /*
0261  * Get UUID of Trusted OS.
0262  *
0263  * Used by non-secure world to figure out which Trusted OS is installed.
0264  * Note that returned UUID is the UUID of the Trusted OS, not of the API.
0265  *
0266  * Returns UUID in 4 32-bit words in the same way as
0267  * OPTEE_MSG_FUNCID_CALLS_UID described above.
0268  */
0269 #define OPTEE_MSG_OS_OPTEE_UUID_0   0x486178e0
0270 #define OPTEE_MSG_OS_OPTEE_UUID_1   0xe7f811e3
0271 #define OPTEE_MSG_OS_OPTEE_UUID_2   0xbc5e0002
0272 #define OPTEE_MSG_OS_OPTEE_UUID_3   0xa5d5c51b
0273 #define OPTEE_MSG_FUNCID_GET_OS_UUID    0x0000
0274 
0275 /*
0276  * Get revision of Trusted OS.
0277  *
0278  * Used by non-secure world to figure out which version of the Trusted OS
0279  * is installed. Note that the returned revision is the revision of the
0280  * Trusted OS, not of the API.
0281  *
0282  * Returns revision in 2 32-bit words in the same way as
0283  * OPTEE_MSG_CALLS_REVISION described above.
0284  */
0285 #define OPTEE_MSG_FUNCID_GET_OS_REVISION    0x0001
0286 
0287 /*
0288  * Do a secure call with struct optee_msg_arg as argument
0289  * The OPTEE_MSG_CMD_* below defines what goes in struct optee_msg_arg::cmd
0290  *
0291  * OPTEE_MSG_CMD_OPEN_SESSION opens a session to a Trusted Application.
0292  * The first two parameters are tagged as meta, holding two value
0293  * parameters to pass the following information:
0294  * param[0].u.value.a-b uuid of Trusted Application
0295  * param[1].u.value.a-b uuid of Client
0296  * param[1].u.value.c Login class of client OPTEE_MSG_LOGIN_*
0297  *
0298  * OPTEE_MSG_CMD_INVOKE_COMMAND invokes a command a previously opened
0299  * session to a Trusted Application.  struct optee_msg_arg::func is Trusted
0300  * Application function, specific to the Trusted Application.
0301  *
0302  * OPTEE_MSG_CMD_CLOSE_SESSION closes a previously opened session to
0303  * Trusted Application.
0304  *
0305  * OPTEE_MSG_CMD_CANCEL cancels a currently invoked command.
0306  *
0307  * OPTEE_MSG_CMD_REGISTER_SHM registers a shared memory reference. The
0308  * information is passed as:
0309  * [in] param[0].attr           OPTEE_MSG_ATTR_TYPE_TMEM_INPUT
0310  *                  [| OPTEE_MSG_ATTR_NONCONTIG]
0311  * [in] param[0].u.tmem.buf_ptr     physical address (of first fragment)
0312  * [in] param[0].u.tmem.size        size (of first fragment)
0313  * [in] param[0].u.tmem.shm_ref     holds shared memory reference
0314  *
0315  * OPTEE_MSG_CMD_UNREGISTER_SHM unregisters a previously registered shared
0316  * memory reference. The information is passed as:
0317  * [in] param[0].attr           OPTEE_MSG_ATTR_TYPE_RMEM_INPUT
0318  * [in] param[0].u.rmem.shm_ref     holds shared memory reference
0319  * [in] param[0].u.rmem.offs        0
0320  * [in] param[0].u.rmem.size        0
0321  *
0322  * OPTEE_MSG_CMD_DO_BOTTOM_HALF does the scheduled bottom half processing
0323  * of a driver.
0324  *
0325  * OPTEE_MSG_CMD_STOP_ASYNC_NOTIF informs secure world that from now is
0326  * normal world unable to process asynchronous notifications. Typically
0327  * used when the driver is shut down.
0328  */
0329 #define OPTEE_MSG_CMD_OPEN_SESSION  0
0330 #define OPTEE_MSG_CMD_INVOKE_COMMAND    1
0331 #define OPTEE_MSG_CMD_CLOSE_SESSION 2
0332 #define OPTEE_MSG_CMD_CANCEL        3
0333 #define OPTEE_MSG_CMD_REGISTER_SHM  4
0334 #define OPTEE_MSG_CMD_UNREGISTER_SHM    5
0335 #define OPTEE_MSG_CMD_DO_BOTTOM_HALF    6
0336 #define OPTEE_MSG_CMD_STOP_ASYNC_NOTIF  7
0337 #define OPTEE_MSG_FUNCID_CALL_WITH_ARG  0x0004
0338 
0339 #endif /* _OPTEE_MSG_H */