1 #include "HalideRuntime.h"
2 #include "printer.h"
3 #include "runtime_internal.h"
4 
5 namespace Halide {
6 namespace Runtime {
7 namespace Internal {
8 namespace Qurt {
9 
10 enum { QURT_EOK = 0 };
11 
12 }
13 }  // namespace Internal
14 }  // namespace Runtime
15 }  // namespace Halide
16 
17 extern "C" {
18 typedef unsigned int qurt_thread_t;
19 
20 /*
21    Macros for QuRT thread attributes.
22  */
23 
24 // clang-format off
25 
26 /**
27  * \defgroup qurt_thread_macros QURT threading macros
28  * @{
29  */
30 #define QURT_HTHREAD_L1I_PREFETCH 0x1 /**< Enables hardware L1 instruction cache prefetching. */
31 #define QURT_HTHREAD_L1D_PREFETCH 0x2 /**< Enables hardware L1 data cache prefetching. */
32 #define QURT_HTHREAD_L2I_PREFETCH 0x4 /**< Enables hardware L2 instruction cache prefetching. */
33 #define QURT_HTHREAD_L2D_PREFETCH 0x8 /**< Enables hardware L2 data cache prefetching. */
34 #define QURT_HTHREAD_DCFETCH 0x10     /**< Enables DC fetch to the provided virtual address.                   \
35                                           DC fetch instructs the hardware that a data memory access is likely. \
36                                           Instructions are dropped in the case of high bus utilization. */
37 
38 #define QURT_THREAD_ATTR_NAME_MAXLEN 16                                           /**< */
39 #define QURT_THREAD_ATTR_TCB_PARTITION_RAM 0                                      /**< Creates threads in RAM/DDR. */
40 #define QURT_THREAD_ATTR_TCB_PARTITION_TCM 1                                      /**< Creates threads in TCM. */
41 #define QURT_THREAD_ATTR_TCB_PARTITION_DEFAULT QURT_THREAD_ATTR_TCB_PARTITION_RAM /**< Backward compatibility. */
42 #define QURT_THREAD_ATTR_PRIORITY_DEFAULT 255                                     /**< */
43 #define QURT_THREAD_ATTR_ASID_DEFAULT 0                                           /**< */
44 #define QURT_THREAD_ATTR_AFFINITY_DEFAULT (-1)                                    /**< */
45 #define QURT_THREAD_ATTR_BUS_PRIO_DEFAULT 255                                     /**< */
46 #define QURT_THREAD_ATTR_TIMETEST_ID_DEFAULT (-2)                                 /**< */
47 /** @} */
48 
49 // clang-format on
50 
51 /** Thread attributes */
52 typedef struct _qurt_thread_attr {
53     /** @cond */
54     char name[QURT_THREAD_ATTR_NAME_MAXLEN]; /**< Thread name. */
55     unsigned char tcb_partition;             /**< Should the thread TCB reside in RAM or
56                                        on chip memory (i.e. TCM). */
57     unsigned char affinity;                  /**< Hardware bitmask indicating the threads it
58                                        can run on. */
59     unsigned short priority;                 /**< Thread priority. */
60     unsigned char asid;                      /**< Address space ID. */
61     unsigned char bus_priority;              /**< internal bus priority. */
62     unsigned short timetest_id;              /**< Timetest ID. */
63     unsigned int stack_size;                 /**< Thread stack size. */
64     void *stack_addr;                        /**< Stack address base, the range of the stack is
65                                        (stack_addr, stack_addr+stack_size-1). */
66     /** @endcond */
67 } qurt_thread_attr_t;
68 
69 /*=============================================================================
70                                                                                                 FUNCTIONS
71 =============================================================================*/
72 /**
73   Initializes the structure used to set the thread attributes when a thread is created.
74   After an attribute structure is initialized, the individual attributes in the structure can be
75   explicitly set using the thread attribute operations.
76 
77   The default attribute values set the by the initialize operation are the following: \n
78   - Name -- Null string \n
79   - Timetest ID -- QURT_THREAD_ATTR_TIMETEST_ID_DEFAULT \n
80   - Priority -- QURT_THREAD_ATTR_PRIORITY_DEFAULT \n
81   - Affinity -- QURT_THREAD_ATTR_AFFINITY_DEFAULT \n
82   - Bus priority -- QURT_THREAD_ATTR_BUS_PRIO_DEFAULT \n
83   - TCB partition -- QURT_THREAD_ATTR_TCB_PARTITION_DEFAULT
84   - stack_size -- zero
85   - stack_addr -- zero
86 
87   @datatypes
88   #qurt_thread_attr_t
89 
90   @param[in,out] attr Thread attribute structure.
91 
92   @return
93   None.
94 */
95 // extern void qurt_thread_attr_init(qurt_thread_attr_t *attr); //pdb remove
qurt_thread_attr_init(qurt_thread_attr_t * attr)96 static inline void qurt_thread_attr_init(qurt_thread_attr_t *attr) {
97 
98     attr->name[0] = 0;
99     attr->tcb_partition = QURT_THREAD_ATTR_TCB_PARTITION_DEFAULT;
100     attr->priority = QURT_THREAD_ATTR_PRIORITY_DEFAULT;
101     attr->asid = QURT_THREAD_ATTR_ASID_DEFAULT;
102     attr->affinity = QURT_THREAD_ATTR_AFFINITY_DEFAULT;
103     attr->bus_priority = QURT_THREAD_ATTR_BUS_PRIO_DEFAULT;
104     attr->timetest_id = QURT_THREAD_ATTR_TIMETEST_ID_DEFAULT;
105     attr->stack_size = 0;
106     attr->stack_addr = 0;
107 }
108 
109 /**
110   Sets the thread stack size attribute.\n
111   Specifies the size of the memory area to be used for a thread's call stack.
112 
113   The thread stack address (Section \ref qurt_thread_attr_set_stack_addr ) and stack size specify the memory area used as a
114   call stack for the thread. The user is responsible for allocating the memory area used for
115   the stack.
116 
117   @datatypes
118   #qurt_thread_attr_t
119 
120   @param[in,out] attr Thread attribute structure.
121   @param[in] stack_size Size (in bytes) of the thread stack.
122 
123   @return
124   None.
125 */
126 // extern void qurt_thread_attr_set_stack_size(qurt_thread_attr_t *attr, unsigned int stack_size); // pdb remove
qurt_thread_attr_set_stack_size(qurt_thread_attr_t * attr,unsigned int stack_size)127 static inline void qurt_thread_attr_set_stack_size(qurt_thread_attr_t *attr, unsigned int stack_size) {
128     attr->stack_size = stack_size;
129 }
130 
131 /**
132   Sets the thread stack address attribute. \n
133   Specifies the base address of the memory area to be used for a thread's call stack.
134 
135   stack_addr must contain an address value that is 8-byte aligned.
136 
137   The thread stack address and stack size (Section \ref qurt_thread_attr_set_stack_size ) specify the memory area used as a
138   call stack for the thread. \n
139   @note The user is responsible for allocating the memory area used for the thread
140              stack. The memory area must be large enough to contain the stack that is
141              created by the thread.
142 
143   @datatypes
144   #qurt_thread_attr_t
145 
146   @param[in,out] attr Thread attribute structure.
147   @param[in] stack_addr  8-byte aligned address of the thread stack.
148 
149   @return
150   None.
151 */
qurt_thread_attr_set_stack_addr(qurt_thread_attr_t * attr,void * stack_addr)152 static inline void qurt_thread_attr_set_stack_addr(qurt_thread_attr_t *attr, void *stack_addr) {
153     attr->stack_addr = stack_addr;
154 }
155 
156 /**
157   Sets the thread priority to be assigned to a thread.
158   Thread priorities are specified as numeric values in the range 1-255, with 1 representing
159   the highest priority.
160 
161   @datatypes
162   #qurt_thread_attr_t
163 
164   @param[in,out] attr Thread attribute structure.
165   @param[in] priority Thread priority.
166 
167   @return
168   None.
169 */
qurt_thread_attr_set_priority(qurt_thread_attr_t * attr,unsigned short priority)170 static inline void qurt_thread_attr_set_priority(qurt_thread_attr_t *attr, unsigned short priority) {
171     attr->priority = priority;
172 }
173 
174 extern int qurt_thread_set_priority(qurt_thread_t threadid, unsigned short newprio);
175 extern int qurt_thread_create(qurt_thread_t *thread_id, qurt_thread_attr_t *attr, void (*entrypoint)(void *), void *arg);
176 /**
177    Waits for a specified thread to finish.
178    The specified thread should be another thread within the same process.
179    The caller thread is suspended until the specified thread exits. When this happens the
180    caller thread is awakened. \n
181    @note If the specified thread has already exited, this function returns immediately
182               with the result value QURT_ENOTHREAD. \par
183    @note Two threads cannot call qurt_thread_join to wait for the same thread to finish.
184               If this happens QuRT generates an exception.
185 
186    @param[in]   tid     Thread identifier.
187    @param[out]  status  Destination variable for thread exit status. Returns an application-defined
188                         value indicating the termination status of the specified thread.
189 
190    @return
191    QURT_ENOTHREAD -- Thread has already exited. \n
192    QURT_EOK -- Thread successfully joined with valid status value.
193  */
194 extern int qurt_thread_join(unsigned int tid, int *status);
195 
196 /** QuRT mutex type.
197 
198    Both non-recursive mutex lock/unlock and recursive
199    mutex lock/unlock can be applied to this type.
200  */
201 typedef union qurt_mutex_aligned8 {
202     /** @cond */
203     struct {
204         unsigned int holder;
205         unsigned int count;
206         unsigned int queue;
207         unsigned int wait_count;
208     };
209     unsigned long long int raw;
210     /** @endcond */
211 } qurt_mutex_t;
212 
213 /** QuRT condition variable type.  */
214 typedef union {
215     /** @cond */
216     unsigned long long raw;
217     struct {
218         unsigned int count;
219         unsigned int n_waiting;
220         unsigned int queue;
221         unsigned int reserved;
222     } X;
223     /** @endcond */
224 } qurt_cond_t;
225 
226 extern void qurt_mutex_init(qurt_mutex_t *lock);
227 extern void qurt_mutex_destroy(qurt_mutex_t *lock);
228 extern void qurt_mutex_lock(qurt_mutex_t *lock);   /* blocking */
229 extern void qurt_mutex_unlock(qurt_mutex_t *lock); /* unlock */
230 
231 extern void qurt_cond_init(qurt_cond_t *cond);
232 extern void qurt_cond_destroy(qurt_cond_t *cond);
233 extern void qurt_cond_signal(qurt_cond_t *cond);
234 extern void qurt_cond_wait(qurt_cond_t *cond, qurt_mutex_t *mutex);
235 
236 typedef enum {
237     QURT_HVX_MODE_64B = 0, /**< HVX mode of 64 bytes */
238     QURT_HVX_MODE_128B = 1 /**< HVX mode of 128 bytes */
239 } qurt_hvx_mode_t;
240 
241 extern int qurt_hvx_lock(qurt_hvx_mode_t lock_mode);
242 extern int qurt_hvx_unlock(void);
243 extern int qurt_hvx_get_mode(void);
244 
245 typedef unsigned int qurt_size_t;
246 typedef unsigned int qurt_mem_pool_t;
247 }
248