SYS/BIOS  7.00
SecureCB.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2022 Texas Instruments Incorporated - https://www.ti.com
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * ======== SecureCB.h ========
35  */
36 
37 #ifndef ti_spe_SecureCB__include
38 #define ti_spe_SecureCB__include
39 
40 #include <stddef.h>
41 #include <stdint.h>
42 
43 #if defined(__GNUC__) || defined(__IAR_SYSTEMS_ICC__)
44 #ifndef __STATIC_INLINE
45 #define __STATIC_INLINE static inline
46 #endif
47 #elif defined(__clang__) && defined(__ti__)
48 #define __STATIC_INLINE static __inline
49 #endif
50 
51 #if defined (__cplusplus)
52 extern "C" {
53 #endif
54 
55 #define NVIC_ISPR_ADDRESS 0xe000e200
56 
57 /* There is no dedicated interrupt for the SecureCB module.
58  * Using arbitrary interrupt which is not in use (hopefully).
59  */
60 #define SecureCB_INT_NUM 43
61 
62 #define SecureCB_STATUS_OK 0
63 #define SecureCB_STATUS_ERROR 1
64 
65 #define SecureCB_CRSTAT_DEFINED 1
66 #define SecureCB_CRSTAT_ENABLED 2
67 
68 typedef uintptr_t SecureCB_Arg;
69 typedef void (*SecureCB_FuncPtr)(SecureCB_Arg arg);
70 
71 typedef struct SecureCB_Object {
72  struct SecureCB_Object *volatile next;
73  struct SecureCB_Object *volatile prev;
74  uint16_t intNum;
75  uint16_t crstat;
77  SecureCB_Arg arg;
79 
81 
82 /*
83  * ======== SecureCB_init ========
84  * Initialize SecureCB driver
85  *
86  * Must be called once and prior to invoking any other SecureCB APIs.
87  *
88  * This API is only available to non-secure code
89  */
90 void SecureCB_init(void);
91 
92 /*
93  * ======== SecureCB_construct ========
94  * Construct a callback object and add it from the Callback dispatcher's
95  * service Queue.
96  *
97  * Returns a handle to the callback object if successful, NULL otherwise;
98  *
99  * The Callback is not immediately enabled.
100  *
101  * This API is only available to non-secure code
102  */
103 SecureCB_Handle SecureCB_construct(SecureCB_Object *scb, SecureCB_FuncPtr fxn, SecureCB_Arg arg);
104 
105 /*
106  * ======== SecureCB_destruct ========
107  * Destruct a callback object and remove it from the Callback dispatcher's
108  * service Queue.
109  *
110  * Returns SecureCB_STATUS_ERROR if object appears to be invalid.
111  *
112  * This API is only available to non-secure code
113  */
114 uint32_t SecureCB_destruct(SecureCB_Object *scb);
115 
116 /*
117  * ======== SecureCB_enable ========
118  * Enable a callback so that the next time the callback dispatch's
119  * interrupt occurs, the callback will be invoked.
120  *
121  * Returns SecureCB_STATUS_ERROR if object appears to be invalid.
122  *
123  * This API is available to both non-secure and secure code
124  */
125 __attribute__ ((always_inline)) __STATIC_INLINE
126 uint32_t SecureCB_enable(SecureCB_Object *scb)
127 {
128  if (scb->crstat & SecureCB_CRSTAT_DEFINED) {
129  scb->crstat |= SecureCB_CRSTAT_ENABLED;
130  return (SecureCB_STATUS_OK);
131  }
132  return (SecureCB_STATUS_ERROR);
133 }
134 
135 /*
136  * ======== SecureCB_setArg ========
137  * update the arg of an existing callback
138  *
139  * Returns SecureCB_STATUS_ERROR if object appears to be invalid.
140  *
141  * This API is available to both non-secure and secure code
142  */
143 __attribute__ ((always_inline)) __STATIC_INLINE
144 uint32_t SecureCB_setArg(SecureCB_Handle scb, SecureCB_Arg arg)
145 {
146  if (scb->crstat & SecureCB_CRSTAT_DEFINED) {
147  scb->arg = arg;
148  return (SecureCB_STATUS_OK);
149  }
150  return (SecureCB_STATUS_ERROR);
151 }
152 
153 /*
154  * ======== SecureCB_getArg ========
155  * fetch the arg of an existing callback
156  *
157  * Returns 0 if object appears to be invalid.
158  *
159  * This API is available to both non-secure and secure code
160  */
161 __attribute__ ((always_inline)) __STATIC_INLINE
162 SecureCB_Arg SecureCB_getArg(SecureCB_Handle scb)
163 {
164  if (scb->crstat & SecureCB_CRSTAT_DEFINED) {
165  return (scb->arg);
166  }
167  return (0);
168 }
169 
170 /*
171  * ======== SecureCB_post ========
172  * Post a callback
173  *
174  * Enables the callback and triggers the NS Callback
175  * Dispatcher's interrupt.
176  *
177  * Returns SecureCB_STATUS_ERROR if object appears to be invalid.
178  *
179  * This API is available to both non-secure and secure code
180  */
181 __attribute__ ((always_inline)) __STATIC_INLINE
182 uint32_t SecureCB_post(SecureCB_Handle scb)
183 {
184  uint32_t *ISPR = (uint32_t *)NVIC_ISPR_ADDRESS;
185  uint32_t IRQn = scb->intNum - 16;
186 
187  if (scb->crstat & SecureCB_CRSTAT_DEFINED) {
189  ISPR[IRQn >> 5UL] = 1 << (IRQn & 0x1f);
190  return (SecureCB_STATUS_OK);
191  }
192  return (SecureCB_STATUS_ERROR);
193 }
194 
195 #if defined (__cplusplus)
196 }
197 #endif
198 
199 #endif /* ti_spe_SecureCB__include */
#define SecureCB_CRSTAT_DEFINED
Definition: SecureCB.h:65
#define SecureCB_STATUS_ERROR
Definition: SecureCB.h:63
void(* SecureCB_FuncPtr)(SecureCB_Arg arg)
Definition: SecureCB.h:69
#define SecureCB_STATUS_OK
Definition: SecureCB.h:62
#define NVIC_ISPR_ADDRESS
Definition: SecureCB.h:55
__attribute__((always_inline)) __STATIC_INLINE uint32_t SecureCB_enable(SecureCB_Object *scb)
Definition: SecureCB.h:125
SecureCB_Arg arg
Definition: SecureCB.h:77
uintptr_t SecureCB_Arg
Definition: SecureCB.h:68
uint32_t SecureCB_destruct(SecureCB_Object *scb)
struct SecureCB_Object *volatile next
Definition: SecureCB.h:72
SecureCB_FuncPtr fxn
Definition: SecureCB.h:76
SecureCB_Handle SecureCB_construct(SecureCB_Object *scb, SecureCB_FuncPtr fxn, SecureCB_Arg arg)
uint16_t crstat
Definition: SecureCB.h:75
uint16_t intNum
Definition: SecureCB.h:74
#define SecureCB_CRSTAT_ENABLED
Definition: SecureCB.h:66
SecureCB_Object * SecureCB_Handle
Definition: SecureCB.h:80
SecureCB_Arg arg
Definition: SecureCB.h:145
void SecureCB_init(void)
Definition: SecureCB.h:71
struct SecureCB_Object *volatile prev
Definition: SecureCB.h:73
struct SecureCB_Object SecureCB_Object
© Copyright 1995-2022, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale