1 /* 2 * Copyright (c) 2021, Arm Limited. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 */ 7 #ifndef __TFM_NS_CTX_H__ 8 #define __TFM_NS_CTX_H__ 9 10 #include <stdint.h> 11 #include <stdbool.h> 12 13 /* Supported maximum context for NS. Only support single context for now. */ 14 #define TFM_NS_CONTEXT_MAX 1 15 16 #define TFM_NS_CONTEXT_MAX_TID 0xFF 17 18 /* Non-secure context structure */ 19 struct tfm_ns_ctx_t { 20 int32_t nsid; /* Non-secure Client ID, must be < 0 */ 21 uint8_t gid; /* Group ID. Threads in same group share one context */ 22 uint8_t tid; /* Thread ID. Used to identify threads in same group */ 23 uint8_t ref_cnt; /* The number of threads sharing this context */ 24 }; 25 26 /* Initialize the non-secure context */ 27 bool init_ns_ctx(void); 28 29 /* 30 * Acquire the non-secure context for a non-secure client thread 31 * gid: The group ID of the thread. The threads in one group share one context. 32 * idx: Output buffer to retrieve the index of the allocated context. 33 * Return: bool type to indicate success of the context allocation. 34 */ 35 bool acquire_ns_ctx(uint8_t gid, uint8_t *idx); 36 37 /* 38 * Release the non-secure context for a non-secure client thread 39 * gid: The group ID of the thread. The threads in one group share one context. 40 * tid: The thread ID. 41 * idx: The context index for that thread. 42 * Return: bool type to indicate success of the context release. 43 */ 44 bool release_ns_ctx(uint8_t gid, uint8_t tid, uint8_t idx); 45 46 /* 47 * Load the context for the non-secure client thread 48 * gid: The group ID of the thread. The threads in one group share one context. 49 * tid: The thread ID. 50 * nsid: The non-secure client ID for that thread. 51 * idx: The context index for that thread. 52 * Return: bool type to indicate success of the context load. 53 */ 54 bool load_ns_ctx(uint8_t gid, uint8_t tid, int32_t nsid, uint8_t idx); 55 56 /* 57 * Save the context for the non-secure client thread 58 * gid: The group ID of the thread. The threads in one group share one context. 59 * tid: The thread ID. 60 * idx: The context index for that thread. 61 * Return: bool type to indicate success of the context save. 62 */ 63 bool save_ns_ctx(uint8_t gid, uint8_t tid, uint8_t idx); 64 65 /* 66 * Return non-secure client ID in the active non-secure context. 67 * Return invalid NSID if no context is active. 68 */ 69 int32_t get_nsid_from_active_ns_ctx(void); 70 71 #endif /* __TFM_NS_CTX_H__ */ 72