sp500/dsp.h Source File

sp500/dsp.h Source File#

Mobilint REGULUS: sp500/dsp.h Source File
Mobilint REGULUS v4.1
SP500 DSP API
dsp.h
Go to the documentation of this file.
1
41#ifndef SP500_DSP_H
42#define SP500_DSP_H
43
44#include <stdint.h>
45#include <stddef.h>
46
47/* ---------------------------------------------------------------- version -- */
48
49#define SP500_VERSION_MAJOR 1
50#define SP500_VERSION_MINOR 0
51#define SP500_VERSION_PATCH 0
52
54#define SP500_VERSION_INT (((SP500_VERSION_MAJOR) * 10000) + \
55 ((SP500_VERSION_MINOR) * 100) + (SP500_VERSION_PATCH))
56
57/* -------------------------------------------------------- linkage macros -- */
58
59#if defined(__GNUC__) || defined(__clang__)
60# define SP500_API __attribute__((visibility("default")))
61#else
62# define SP500_API
63#endif
64
65#ifdef __cplusplus
66# define SP500_INLINE inline
67extern "C" {
68#else
69# define SP500_INLINE static inline
70#endif
71
72/* ----------------------------------------------------------------- status -- */
73
83typedef int32_t sp500_status;
84
85enum
86{
97};
98
100SP500_API const char* sp500_status_string(sp500_status status);
101
107SP500_API uint32_t sp500_version(void);
108
110SP500_API const char* sp500_version_string(void);
111
112/* -------------------------------------------------------------- lifecycle -- */
113
115#define SP500_DEFAULT_OPEN_TIMEOUT_MS 10000u
116
118#define SP500_DEFAULT_DSP_TIMEOUT_MS 3000u
119
142SP500_API sp500_status sp500_dsp_open(uint32_t timeout_ms);
143
145SP500_API int sp500_dsp_is_open(void);
146
165SP500_API void sp500_set_dsp_timeout_ms(uint32_t timeout_ms);
166
168SP500_API uint32_t sp500_get_dsp_timeout_ms(void);
169
170/* ------------------------------------------------------------------- lock -- */
171
184SP500_API void sp500_dsp_lock(void);
185
187SP500_API void sp500_dsp_unlock(void);
188
189/* ---------------------------------------------------------- shared memory -- */
190
192#define SP500_ALIGN_DSP 32u
193
195#define SP500_ALIGN_NPU 4096u
196
216SP500_API void* sp500_shmem_alloc(uint32_t size, uint32_t alignment);
217
219SP500_API void sp500_shmem_free(void* ptr);
220
227SP500_API int sp500_shmem_contains(const void* ptr);
228
239SP500_API uint64_t sp500_shmem_phys_addr(const void* ptr);
240
241/* ---------------------------------------------------------------- buffers -- */
242
244enum
245{
249};
250
260typedef struct sp500_buffer
261{
262 void* data;
263 uint32_t size;
264 uint32_t flags;
266
269{
270 sp500_buffer b;
271 b.data = NULL;
272 b.size = 0u;
273 b.flags = 0u;
274 return b;
275}
276
286SP500_INLINE sp500_buffer sp500_buffer_alloc(uint32_t size, uint32_t alignment)
287{
289 b.data = sp500_shmem_alloc(size, alignment);
290 if(b.data != NULL)
291 {
292 b.size = size;
294 }
295 return b;
296}
297
302SP500_INLINE sp500_buffer sp500_buffer_wrap(void* data, uint32_t size)
303{
305 b.data = data;
306 b.size = size;
307 return b;
308}
309
316SP500_INLINE void sp500_buffer_release(sp500_buffer* buffer)
317{
318 if(buffer != NULL)
319 {
320 if((buffer->flags & SP500_BUFFER_SHARED) != 0u)
321 {
322 sp500_shmem_free(buffer->data);
323 }
324 *buffer = sp500_buffer_none();
325 }
326}
327
328#ifdef __cplusplus
329} /* extern "C" */
330#endif
331
332#endif /* SP500_DSP_H */
sp500_buffer sp500_buffer_alloc(uint32_t size, uint32_t alignment)
Allocate a zero-copy buffer in shared memory.
Definition dsp.h:286
int32_t sp500_status
Result of every sp500 call.
Definition dsp.h:83
void sp500_dsp_unlock(void)
Release one level of the lock taken by sp500_dsp_lock().
uint32_t sp500_get_dsp_timeout_ms(void)
Deadline currently in force, in milliseconds, after clamping.
void sp500_dsp_lock(void)
Take the one reentrant DSP lock, across threads and across processes.
void sp500_shmem_free(void *ptr)
Release a buffer from sp500_shmem_alloc(). NULL is ignored.
int sp500_dsp_is_open(void)
Non-blocking: 1 once sp500_dsp_open() has succeeded, 0 otherwise.
uint64_t sp500_shmem_phys_addr(const void *ptr)
Physical address of a shared-memory pointer, for NPU DMA.
sp500_buffer sp500_buffer_none(void)
An unused buffer, for an optional tensor.
Definition dsp.h:268
@ SP500_ERR_BUFFER_TOO_SMALL
Definition dsp.h:91
@ SP500_ERR_TIMEOUT
Definition dsp.h:94
@ SP500_ERR_NOT_READY
Definition dsp.h:92
@ SP500_OK
Definition dsp.h:87
@ SP500_ERR_NO_MEMORY
Definition dsp.h:93
@ SP500_ERR_INTERNAL
Definition dsp.h:96
@ SP500_ERR_DSP
Definition dsp.h:95
@ SP500_ERR_UNSUPPORTED
Definition dsp.h:90
@ SP500_ERR_INVALID_ARG
Definition dsp.h:88
const char * sp500_version_string(void)
Version of the loaded library as "major.minor.patch". Never NULL.
void * sp500_shmem_alloc(uint32_t size, uint32_t alignment)
Allocate inside the DSP shared-memory window.
const char * sp500_status_string(sp500_status status)
Human-readable name of a status code. Never NULL, never allocates.
uint32_t sp500_version(void)
Version of the loaded library, encoded like SP500_VERSION_INT.
void sp500_set_dsp_timeout_ms(uint32_t timeout_ms)
Raise the deadline applied to every subsequent DSP call.
int sp500_shmem_contains(const void *ptr)
1 when ptr lies inside the shared-memory window, 0 otherwise.
@ SP500_BUFFER_SHARED
Definition dsp.h:248
sp500_status sp500_dsp_open(uint32_t timeout_ms)
Attach to the DSP. Must succeed before any other DSP call.
void sp500_buffer_release(sp500_buffer *buffer)
Release a buffer and reset it to unused.
Definition dsp.h:316
sp500_buffer sp500_buffer_wrap(void *data, uint32_t size)
Wrap memory you already own. The call stages one copy in each direction; nothing is taken over and no...
Definition dsp.h:302
A tensor handed to or received from the DSP.
Definition dsp.h:261
void * data
Definition dsp.h:262
uint32_t flags
Definition dsp.h:264
uint32_t size
Definition dsp.h:263