blob: 9df7ad1acc4f619478c593ebf4971871fb91979d [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * @file
19 *
20 * @brief Public kernel APIs.
21 */
22
23#ifndef _kernel__h_
24#define _kernel__h_
25
26#include <stddef.h>
27#include <stdint.h>
28#include <toolchain.h>
29#include <sections.h>
30#include <atomic.h>
31#include <errno.h>
32#include <misc/__assert.h>
33#include <misc/dlist.h>
34#include <misc/slist.h>
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40#ifdef CONFIG_KERNEL_V2_DEBUG
41#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
42#else
43#define K_DEBUG(fmt, ...)
44#endif
45
46#define K_PRIO_COOP(x) (-(CONFIG_NUM_COOP_PRIORITIES - (x)))
47#define K_PRIO_PREEMPT(x) (x)
48
49#define K_FOREVER (-1)
50#define K_NO_WAIT 0
51
52#define K_ANY NULL
53#define K_END NULL
54
Benjamin Walsh456c6da2016-09-02 18:55:39 -040055#if CONFIG_NUM_COOP_PRIORITIES > 0
56#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
57#else
58#define K_HIGHEST_THREAD_PRIO 0
59#endif
60
61#if CONFIG_NUM_PREEMPT_PRIORITIES > 0
62#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
63#else
64#define K_LOWEST_THREAD_PRIO -1
65#endif
66
67#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
68#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
69
70typedef sys_dlist_t _wait_q_t;
71
72#ifdef CONFIG_DEBUG_TRACING_KERNEL_OBJECTS
73#define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type) struct type *__next
74#define _DEBUG_TRACING_KERNEL_OBJECTS_INIT .__next = NULL,
75#else
76#define _DEBUG_TRACING_KERNEL_OBJECTS_INIT
77#define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type)
78#endif
79
80#define k_thread tcs
81struct tcs;
82struct k_mutex;
83struct k_sem;
84struct k_event;
85struct k_msgq;
86struct k_mbox;
87struct k_pipe;
88struct k_fifo;
89struct k_lifo;
90struct k_stack;
91struct k_mem_map;
92struct k_mem_pool;
93struct k_timer;
94
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -040095typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040096
97/* threads/scheduler/execution contexts */
98
99enum execution_context_types {
100 K_ISR = 0,
101 K_COOP_THREAD,
102 K_PREEMPT_THREAD,
103};
104
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400105typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
106extern k_tid_t k_thread_spawn(char *stack, unsigned stack_size,
107 void (*entry)(void *, void *, void*),
108 void *p1, void *p2, void *p3,
109 int32_t prio, uint32_t options, int32_t delay);
110
111extern void k_sleep(int32_t duration);
112extern void k_busy_wait(uint32_t usec_to_wait);
113extern void k_yield(void);
114extern void k_wakeup(k_tid_t thread);
115extern k_tid_t k_current_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400116extern int k_thread_cancel(k_tid_t thread);
117
118extern void k_thread_abort(k_tid_t thread);
119
120#define K_THREAD_GROUP_EXE 0x1
121#define K_THREAD_GROUP_SYS 0x2
122#define K_THREAD_GROUP_FPU 0x4
123
124/* XXX - doesn't work because CONFIG_ARCH is a string */
125#if 0
126/* arch-specific groups */
127#if CONFIG_ARCH == "x86"
128#define K_THREAD_GROUP_SSE 0x4
129#endif
130#endif
131
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400132#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400133#define _THREAD_TIMEOUT_INIT(obj) \
134 (obj).nano_timeout = { \
135 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400136 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400137 .wait_q = NULL, \
138 .delta_ticks_from_prev = -1, \
139 },
140#else
141#define _THREAD_TIMEOUT_INIT(obj)
142#endif
143
144#ifdef CONFIG_ERRNO
145#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
146#else
147#define _THREAD_ERRNO_INIT(obj)
148#endif
149
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400150struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400151 union {
152 char *init_stack;
153 struct k_thread *thread;
154 };
155 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500156 void (*init_entry)(void *, void *, void *);
157 void *init_p1;
158 void *init_p2;
159 void *init_p3;
160 int init_prio;
161 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400162 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500163 void (*init_abort)(void);
164 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400165};
166
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400167#define _THREAD_INITIALIZER(stack, stack_size, \
168 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500169 prio, options, delay, abort, groups) \
170 { \
171 .init_stack = (stack), \
172 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400173 .init_entry = (void (*)(void *, void *, void *))entry, \
174 .init_p1 = (void *)p1, \
175 .init_p2 = (void *)p2, \
176 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500177 .init_prio = (prio), \
178 .init_options = (options), \
179 .init_delay = (delay), \
180 .init_abort = (abort), \
181 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400182 }
183
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400184/**
Allan Stephens6cfe1322016-10-26 10:16:51 -0500185 * @brief Define a static thread.
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400186 *
187 * @internal It has been observed that the x86 compiler by default aligns
188 * these _static_thread_data structures to 32-byte boundaries, thereby
189 * wasting space. To work around this, force a 4-byte alignment.
190 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500191#define K_THREAD_DEFINE(name, stack_size, \
192 entry, p1, p2, p3, \
193 prio, options, delay) \
194 char __noinit __stack _k_thread_obj_##name[stack_size]; \
195 struct _static_thread_data _k_thread_data_##name __aligned(4) \
196 __in_section(_k_task_list, private, task) = \
197 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
198 entry, p1, p2, p3, prio, options, delay, \
199 NULL, 0)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400200
Allan Stephens399d0ad2016-10-07 13:41:34 -0500201extern int k_thread_priority_get(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400202extern void k_thread_priority_set(k_tid_t thread, int prio);
203
Benjamin Walsh71d52282016-09-29 10:49:48 -0400204extern void k_thread_suspend(k_tid_t thread);
205extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400206
207extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400208
209extern int k_am_in_isr(void);
210
211extern void k_thread_custom_data_set(void *value);
212extern void *k_thread_custom_data_get(void);
213
214/**
215 * kernel timing
216 */
217
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400218#include <sys_clock.h>
219
220/* private internal time manipulation (users should never play with ticks) */
221
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500222/* added tick needed to account for tick in progress */
223#define _TICK_ALIGN 1
224
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400225static int64_t __ticks_to_ms(int64_t ticks)
226{
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400227#if CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400228 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400229#else
230 __ASSERT(ticks == 0, "");
231 return 0;
232#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400233}
234
235
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400236/* timeouts */
237
238struct _timeout;
239typedef void (*_timeout_func_t)(struct _timeout *t);
240
241struct _timeout {
242 sys_dlist_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400243 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400244 sys_dlist_t *wait_q;
245 int32_t delta_ticks_from_prev;
246 _timeout_func_t func;
247};
248
Allan Stephens45bfa372016-10-12 12:39:42 -0500249
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400250/* timers */
251
252struct k_timer {
253 /*
254 * _timeout structure must be first here if we want to use
255 * dynamic timer allocation. timeout.node is used in the double-linked
256 * list of free timers
257 */
258 struct _timeout timeout;
259
Allan Stephens45bfa372016-10-12 12:39:42 -0500260 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400261 _wait_q_t wait_q;
262
263 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500264 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400265
266 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500267 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400268
269 /* timer period */
270 int32_t period;
271
Allan Stephens45bfa372016-10-12 12:39:42 -0500272 /* timer status */
273 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400274
Allan Stephens45bfa372016-10-12 12:39:42 -0500275 /* used to support legacy timer APIs */
276 void *_legacy_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400277
278 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_timer);
279};
280
281#define K_TIMER_INITIALIZER(obj) \
282 { \
283 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
284 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
285 }
286
287#define K_TIMER_DEFINE(name) \
288 struct k_timer name = K_TIMER_INITIALIZER(name)
289
Allan Stephens45bfa372016-10-12 12:39:42 -0500290/**
291 * @brief Initialize a timer.
292 *
293 * This routine must be called before the timer is used.
294 *
295 * @param timer Address of timer.
296 * @param expiry_fn Function to invoke each time timer expires.
297 * @param stop_fn Function to invoke if timer is stopped while running.
298 *
299 * @return N/A
300 */
301extern void k_timer_init(struct k_timer *timer,
302 void (*expiry_fn)(struct k_timer *),
303 void (*stop_fn)(struct k_timer *));
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700304
Allan Stephens45bfa372016-10-12 12:39:42 -0500305/**
306 * @brief Start a timer.
307 *
308 * This routine starts a timer, and resets its status to zero. The timer
309 * begins counting down using the specified duration and period values.
310 *
311 * Attempting to start a timer that is already running is permitted.
312 * The timer's status is reset to zero and the timer begins counting down
313 * using the new duration and period values.
314 *
315 * @param timer Address of timer.
316 * @param duration Initial timer duration (in milliseconds).
317 * @param period Timer period (in milliseconds).
318 *
319 * @return N/A
320 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400321extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500322 int32_t duration, int32_t period);
323
324/**
325 * @brief Stop a timer.
326 *
327 * This routine stops a running timer prematurely. The timer's stop function,
328 * if one exists, is invoked by the caller.
329 *
330 * Attempting to stop a timer that is not running is permitted, but has no
331 * effect on the timer since it is already stopped.
332 *
333 * @param timer Address of timer.
334 *
335 * @return N/A
336 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400337extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500338
339/**
340 * @brief Read timer status.
341 *
342 * This routine reads the timer's status, which indicates the number of times
343 * it has expired since its status was last read.
344 *
345 * Calling this routine resets the timer's status to zero.
346 *
347 * @param timer Address of timer.
348 *
349 * @return Timer status.
350 */
351extern uint32_t k_timer_status_get(struct k_timer *timer);
352
353/**
354 * @brief Synchronize thread to timer expiration.
355 *
356 * This routine blocks the calling thread until the timer's status is non-zero
357 * (indicating that it has expired at least once since it was last examined)
358 * or the timer is stopped. If the timer status is already non-zero,
359 * or the timer is already stopped, the caller continues without waiting.
360 *
361 * Calling this routine resets the timer's status to zero.
362 *
363 * This routine must not be used by interrupt handlers, since they are not
364 * allowed to block.
365 *
366 * @param timer Address of timer.
367 *
368 * @return Timer status.
369 */
370extern uint32_t k_timer_status_sync(struct k_timer *timer);
371
372/**
373 * @brief Get timer remaining before next timer expiration.
374 *
375 * This routine computes the (approximate) time remaining before a running
376 * timer next expires. If the timer is not running, it returns zero.
377 *
378 * @param timer Address of timer.
379 *
380 * @return Remaining time (in milliseconds).
381 */
382
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400383extern int32_t k_timer_remaining_get(struct k_timer *timer);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400384
385
Allan Stephens45bfa372016-10-12 12:39:42 -0500386/* kernel clocks */
387
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400388/**
389 * @brief Get the time elapsed since the system booted (uptime)
390 *
391 * @return The current uptime of the system in ms
392 */
393
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400394extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400395
396/**
397 * @brief Get the lower 32-bit of time elapsed since the system booted (uptime)
398 *
399 * This function is potentially less onerous in both the time it takes to
400 * execute, the interrupt latency it introduces and the amount of 64-bit math
401 * it requires than k_uptime_get(), but it only provides an uptime value of
402 * 32-bits. The user must handle possible rollovers/spillovers.
403 *
404 * At a rate of increment of 1000 per second, it rolls over approximately every
405 * 50 days.
406 *
407 * @return The current uptime of the system in ms
408 */
409
410extern uint32_t k_uptime_get_32(void);
411
412/**
413 * @brief Get the difference between a reference time and the current uptime
414 *
415 * @param reftime A pointer to a reference time. It is updated with the current
416 * uptime upon return.
417 *
418 * @return The delta between the reference time and the current uptime.
419 */
420
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400421extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400422
423/**
424 * @brief Get the difference between a reference time and the current uptime
425 *
426 * The 32-bit version of k_uptime_delta(). It has the same perks and issues as
427 * k_uptime_get_32().
428 *
429 * @param reftime A pointer to a reference time. It is updated with the current
430 * uptime upon return.
431 *
432 * @return The delta between the reference time and the current uptime.
433 */
434
435extern uint32_t k_uptime_delta_32(int64_t *reftime);
436
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400437extern uint32_t k_cycle_get_32(void);
438
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400439/**
440 * data transfers (basic)
441 */
442
443/* fifos */
444
445struct k_fifo {
446 _wait_q_t wait_q;
447 sys_slist_t data_q;
448
449 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo);
450};
451
452extern void k_fifo_init(struct k_fifo *fifo);
453extern void k_fifo_put(struct k_fifo *fifo, void *data);
454extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
455extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
456extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
457
458#define K_FIFO_INITIALIZER(obj) \
459 { \
460 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh9091e5d2016-09-30 10:42:47 -0400461 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400462 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
463 }
464
465#define K_FIFO_DEFINE(name) \
Benjamin Walsh0bee91d2016-09-15 17:16:38 -0400466 struct k_fifo name = K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400467
468/* lifos */
469
470struct k_lifo {
471 _wait_q_t wait_q;
472 void *list;
473
474 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo);
475};
476
477extern void k_lifo_init(struct k_lifo *lifo);
478extern void k_lifo_put(struct k_lifo *lifo, void *data);
479extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
480
481#define K_LIFO_INITIALIZER(obj) \
482 { \
483 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
484 .list = NULL, \
485 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
486 }
487
488#define K_LIFO_DEFINE(name) \
Benjamin Walsh0bee91d2016-09-15 17:16:38 -0400489 struct k_lifo name = K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400490
491/* stacks */
492
493struct k_stack {
494 _wait_q_t wait_q;
495 uint32_t *base, *next, *top;
496
497 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack);
498};
499
Allan Stephens018cd9a2016-10-07 15:13:24 -0500500extern void k_stack_init(struct k_stack *stack,
501 uint32_t *buffer, int num_entries);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400502extern void k_stack_push(struct k_stack *stack, uint32_t data);
503extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
504
Peter Mitsis602e6a82016-10-17 11:48:43 -0400505#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400506 { \
507 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
508 .base = stack_buffer, \
509 .next = stack_buffer, \
510 .top = stack_buffer + stack_num_entries, \
511 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
512 }
513
Peter Mitsis602e6a82016-10-17 11:48:43 -0400514#define K_STACK_DEFINE(name, stack_num_entries) \
515 uint32_t __noinit \
516 _k_stack_buf_##name[stack_num_entries]; \
517 struct k_stack name = \
518 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
519 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400520
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400521/**
522 * workqueues
523 */
524
525struct k_work;
526
527typedef void (*k_work_handler_t)(struct k_work *);
528
529/**
530 * A workqueue is a fiber that executes @ref k_work items that are
531 * queued to it. This is useful for drivers which need to schedule
532 * execution of code which might sleep from ISR context. The actual
533 * fiber identifier is not stored in the structure in order to save
534 * space.
535 */
536struct k_work_q {
537 struct k_fifo fifo;
538};
539
540/**
541 * @brief Work flags.
542 */
543enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -0300544 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400545};
546
547/**
548 * @brief An item which can be scheduled on a @ref k_work_q.
549 */
550struct k_work {
551 void *_reserved; /* Used by k_fifo implementation. */
552 k_work_handler_t handler;
553 atomic_t flags[1];
554};
555
556/**
557 * @brief Statically initialize work item
558 */
559#define K_WORK_INITIALIZER(work_handler) \
560 { \
561 ._reserved = NULL, \
562 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300563 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400564 }
565
566/**
567 * @brief Dynamically initialize work item
568 */
569static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
570{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300571 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400572 work->handler = handler;
573}
574
575/**
576 * @brief Submit a work item to a workqueue.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +0300577 *
578 * This procedure schedules a work item to be processed.
579 * In the case where the work item has already been submitted and is pending
580 * execution, calling this function will result in a no-op. In this case, the
581 * work item must not be modified externally (e.g. by the caller of this
582 * function), since that could cause the work item to be processed in a
583 * corrupted state.
584 *
585 * @param work_q to schedule the work item
586 * @param work work item
587 *
588 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400589 */
590static inline void k_work_submit_to_queue(struct k_work_q *work_q,
591 struct k_work *work)
592{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +0300593 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400594 k_fifo_put(&work_q->fifo, work);
595 }
596}
597
598/**
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300599 * @brief Check if work item is pending.
600 */
601static inline int k_work_pending(struct k_work *work)
602{
Iván Briano9c7b5ea2016-10-04 18:11:05 -0300603 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300604}
605
606/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400607 * @brief Start a new workqueue. This routine can be called from either
608 * fiber or task context.
609 */
Allan Stephens904cf972016-10-07 13:59:23 -0500610extern void k_work_q_start(struct k_work_q *work_q, char *stack,
611 unsigned stack_size, unsigned prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400612
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400613#if defined(CONFIG_SYS_CLOCK_EXISTS)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400614
615 /*
616 * @brief An item which can be scheduled on a @ref k_work_q with a
617 * delay.
618 */
619struct k_delayed_work {
620 struct k_work work;
621 struct _timeout timeout;
622 struct k_work_q *work_q;
623};
624
625/**
626 * @brief Initialize delayed work
627 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400628extern void k_delayed_work_init(struct k_delayed_work *work,
629 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400630
631/**
632 * @brief Submit a delayed work item to a workqueue.
633 *
634 * This procedure schedules a work item to be processed after a delay.
635 * Once the delay has passed, the work item is submitted to the work queue:
636 * at this point, it is no longer possible to cancel it. Once the work item's
637 * handler is about to be executed, the work is considered complete and can be
638 * resubmitted.
639 *
640 * Care must be taken if the handler blocks or yield as there is no implicit
641 * mutual exclusion mechanism. Such usage is not recommended and if necessary,
642 * it should be explicitly done between the submitter and the handler.
643 *
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500644 * @param work_q Workqueue to schedule the work item
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400645 * @param work Delayed work item
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500646 * @param delay Delay before scheduling the work item (in milliseconds)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400647 *
648 * @return 0 in case of success or negative value in case of error.
649 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400650extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
651 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500652 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400653
654/**
655 * @brief Cancel a delayed work item
656 *
657 * This procedure cancels a scheduled work item. If the work has been completed
658 * or is idle, this will do nothing. The only case where this can fail is when
659 * the work has been submitted to the work queue, but the handler has not run
660 * yet.
661 *
662 * @param work Delayed work item to be canceled
663 *
664 * @return 0 in case of success or negative value in case of error.
665 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400666extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400667
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400668#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400669
670#if defined(CONFIG_SYSTEM_WORKQUEUE)
671
672extern struct k_work_q k_sys_work_q;
673
674/*
675 * @brief Submit a work item to the system workqueue.
676 *
677 * @ref k_work_submit_to_queue
678 *
679 * When using the system workqueue it is not recommended to block or yield
680 * on the handler since its fiber is shared system wide it may cause
681 * unexpected behavior.
682 */
683static inline void k_work_submit(struct k_work *work)
684{
685 k_work_submit_to_queue(&k_sys_work_q, work);
686}
687
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400688#if defined(CONFIG_SYS_CLOCK_EXISTS)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400689/*
690 * @brief Submit a delayed work item to the system workqueue.
691 *
692 * @ref k_delayed_work_submit_to_queue
693 *
694 * When using the system workqueue it is not recommended to block or yield
695 * on the handler since its fiber is shared system wide it may cause
696 * unexpected behavior.
697 */
698static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500699 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400700{
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500701 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400702}
703
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400704#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400705#endif /* CONFIG_SYSTEM_WORKQUEUE */
706
707/**
708 * synchronization
709 */
710
711/* mutexes */
712
713struct k_mutex {
714 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400715 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400716 uint32_t lock_count;
717 int owner_orig_prio;
718#ifdef CONFIG_OBJECT_MONITOR
719 int num_lock_state_changes;
720 int num_conflicts;
721#endif
722
723 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex);
724};
725
726#ifdef CONFIG_OBJECT_MONITOR
727#define _MUTEX_INIT_OBJECT_MONITOR \
728 .num_lock_state_changes = 0, .num_conflicts = 0,
729#else
730#define _MUTEX_INIT_OBJECT_MONITOR
731#endif
732
733#define K_MUTEX_INITIALIZER(obj) \
734 { \
735 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
736 .owner = NULL, \
737 .lock_count = 0, \
738 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
739 _MUTEX_INIT_OBJECT_MONITOR \
740 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
741 }
742
743#define K_MUTEX_DEFINE(name) \
744 struct k_mutex name = K_MUTEX_INITIALIZER(name)
745
746extern void k_mutex_init(struct k_mutex *mutex);
747extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
748extern void k_mutex_unlock(struct k_mutex *mutex);
749
750/* semaphores */
751
752struct k_sem {
753 _wait_q_t wait_q;
754 unsigned int count;
755 unsigned int limit;
756
757 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem);
758};
759
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400760/**
761 * @brief Initialize a semaphore object.
762 *
763 * An initial count and a count limit can be specified. The count will never go
764 * over the count limit if the semaphore is given multiple times without being
765 * taken.
766 *
767 * Cannot be called from ISR.
768 *
769 * @param sem Pointer to a semaphore object.
770 * @param initial_count Initial count.
771 * @param limit Highest value the count can take during operation.
772 *
773 * @return N/A
774 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400775extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
776 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400777
778/**
779 * @brief Take a semaphore, possibly pending if not available.
780 *
781 * The current execution context tries to obtain the semaphore. If the
782 * semaphore is unavailable and a timeout other than K_NO_WAIT is specified,
783 * the context will pend.
784 *
785 * @param sem Pointer to a semaphore object.
786 * @param timeout Number of milliseconds to wait if semaphore is unavailable,
787 * or one of the special values K_NO_WAIT and K_FOREVER.
788 *
789 * @warning If it is called from the context of an ISR, then the only legal
790 * value for @a timeout is K_NO_WAIT.
791 *
792 * @retval 0 When semaphore is obtained successfully.
793 * @retval -EAGAIN When timeout expires.
794 * @retval -EBUSY When unavailable and the timeout is K_NO_WAIT.
795 *
796 * @sa K_NO_WAIT, K_FOREVER
797 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400798extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400799
800/**
801 * @brief Give a semaphore.
802 *
803 * Increase the semaphore's internal count by 1, up to its limit, if no thread
804 * is waiting on the semaphore; otherwise, wake up the first thread in the
805 * semaphore's waiting queue.
806 *
807 * If the latter case, and if the current context is preemptible, the thread
808 * that is taken off the wait queue will be scheduled in and will preempt the
809 * current thread.
810 *
811 * @param sem Pointer to a semaphore object.
812 *
813 * @return N/A
814 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400815extern void k_sem_give(struct k_sem *sem);
816
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400817/**
818 * @brief Reset a semaphore's count to zero.
819 *
820 * The only effect is that the count is set to zero. There is no other
821 * side-effect to calling this function.
822 *
823 * @param sem Pointer to a semaphore object.
824 *
825 * @return N/A
826 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -0400827static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400828{
829 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400830}
831
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400832/**
833 * @brief Get a semaphore's count.
834 *
835 * Note there is no guarantee the count has not changed by the time this
836 * function returns.
837 *
838 * @param sem Pointer to a semaphore object.
839 *
840 * @return The current semaphore count.
841 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +0200842static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400843{
844 return sem->count;
845}
846
Peter Mitsis45403672016-09-09 14:24:06 -0400847#ifdef CONFIG_SEMAPHORE_GROUPS
848/**
849 * @brief Take the first available semaphore
850 *
851 * Given a list of semaphore pointers, this routine will attempt to take one
852 * of them, waiting up to a maximum of @a timeout ms to do so. The taken
853 * semaphore is identified by @a sem (set to NULL on error).
854 *
855 * Be aware that the more semaphores specified in the group, the more stack
856 * space is required by the waiting thread.
857 *
858 * @param sem_array Array of semaphore pointers terminated by a K_END entry
859 * @param sem Identifies the semaphore that was taken
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400860 * @param timeout Number of milliseconds to wait if semaphores are unavailable,
861 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsis45403672016-09-09 14:24:06 -0400862 *
863 * @retval 0 A semaphore was successfully taken
864 * @retval -EBUSY No semaphore was available (@a timeout = K_NO_WAIT)
865 * @retval -EAGAIN Time out occurred while waiting for semaphore
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400866 *
867 * @sa K_NO_WAIT, K_FOREVER
Peter Mitsis45403672016-09-09 14:24:06 -0400868 */
869
870extern int k_sem_group_take(struct k_sem *sem_array[], struct k_sem **sem,
871 int32_t timeout);
872
873/**
874 * @brief Give all the semaphores in the group
875 *
876 * This routine will give each semaphore in the array of semaphore pointers.
877 *
878 * @param sem_array Array of semaphore pointers terminated by a K_END entry
879 *
880 * @return N/A
881 */
882extern void k_sem_group_give(struct k_sem *sem_array[]);
883
884/**
885 * @brief Reset the count to zero on each semaphore in the array
886 *
887 * This routine resets the count of each semaphore in the group to zero.
888 * Note that it does NOT have any impact on any thread that might have
889 * been previously pending on any of the semaphores.
890 *
891 * @param sem_array Array of semaphore pointers terminated by a K_END entry
892 *
893 * @return N/A
894 */
895extern void k_sem_group_reset(struct k_sem *sem_array[]);
896#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400897
898#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
899 { \
900 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
901 .count = initial_count, \
902 .limit = count_limit, \
903 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
904 }
905
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400906/**
907 * @def K_SEM_DEFINE
908 *
909 * @brief Statically define and initialize a global semaphore.
910 *
911 * Create a global semaphore named @name. It is initialized as if k_sem_init()
912 * was called on it. If the semaphore is to be accessed outside the module
913 * where it is defined, it can be declared via
914 *
915 * extern struct k_sem @name;
916 *
917 * @param name Name of the semaphore variable.
918 * @param initial_count Initial count.
919 * @param count_limit Highest value the count can take during operation.
920 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400921#define K_SEM_DEFINE(name, initial_count, count_limit) \
922 struct k_sem name = \
923 K_SEM_INITIALIZER(name, initial_count, count_limit)
924
925/* events */
926
927#define K_EVT_DEFAULT NULL
928#define K_EVT_IGNORE ((void *)(-1))
929
930typedef int (*k_event_handler_t)(struct k_event *);
931
932struct k_event {
933 k_event_handler_t handler;
934 atomic_t send_count;
935 struct k_work work_item;
936 struct k_sem sem;
937
938 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_event);
939};
940
941extern void _k_event_deliver(struct k_work *work);
942
943#define K_EVENT_INITIALIZER(obj, event_handler) \
944 { \
945 .handler = (k_event_handler_t)event_handler, \
946 .send_count = ATOMIC_INIT(0), \
947 .work_item = K_WORK_INITIALIZER(_k_event_deliver), \
948 .sem = K_SEM_INITIALIZER(obj.sem, 0, 1), \
949 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
950 }
951
952#define K_EVENT_DEFINE(name, event_handler) \
953 struct k_event name \
954 __in_section(_k_event_list, event, name) = \
955 K_EVENT_INITIALIZER(name, event_handler)
956
957extern void k_event_init(struct k_event *event, k_event_handler_t handler);
958extern int k_event_recv(struct k_event *event, int32_t timeout);
959extern void k_event_send(struct k_event *event);
960
961/**
962 * data transfers (complex)
963 */
964
965/* message queues */
966
967struct k_msgq {
968 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -0400969 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400970 uint32_t max_msgs;
971 char *buffer_start;
972 char *buffer_end;
973 char *read_ptr;
974 char *write_ptr;
975 uint32_t used_msgs;
976
977 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq);
978};
979
Peter Mitsis1da807e2016-10-06 11:36:59 -0400980#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400981 { \
982 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -0400983 .max_msgs = q_max_msgs, \
984 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400985 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -0400986 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400987 .read_ptr = q_buffer, \
988 .write_ptr = q_buffer, \
989 .used_msgs = 0, \
990 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
991 }
992
Peter Mitsis1da807e2016-10-06 11:36:59 -0400993/**
994 * @brief Define a message queue
995 *
996 * This declares and initializes a message queue whose buffer is aligned to
997 * a @a q_align -byte boundary. The new message queue can be passed to the
998 * kernel's message queue functions.
999 *
1000 * Note that for each of the mesages in the message queue to be aligned to
1001 * @a q_align bytes, then @a q_msg_size must be a multiple of @a q_align.
1002 *
1003 * @param q_name Name of the message queue
1004 * @param q_msg_size The size in bytes of each message
1005 * @param q_max_msgs Maximum number of messages the queue can hold
1006 * @param q_align Alignment of the message queue's buffer (power of 2)
1007 */
1008#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
1009 static char __noinit __aligned(q_align) \
1010 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
1011 struct k_msgq q_name = \
1012 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
1013 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001014
Peter Mitsisd7a37502016-10-13 11:37:40 -04001015/**
1016 * @brief Initialize a message queue.
1017 *
1018 * @param q Pointer to the message queue object.
1019 * @param buffer Pointer to memory area that holds queued messages.
1020 * @param msg_size Message size, in bytes.
1021 * @param max_msgs Maximum number of messages that can be queued.
1022 *
1023 * @return N/A
1024 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04001025extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001026 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001027
1028/**
1029 * @brief Add a message to a message queue.
1030 *
1031 * This routine adds an item to the message queue. When the message queue is
1032 * full, the routine will wait either for space to become available, or until
1033 * the specified time limit is reached.
1034 *
1035 * @param q Pointer to the message queue object.
1036 * @param data Pointer to message data area.
1037 * @param timeout Number of milliseconds to wait until space becomes available
1038 * to add the message into the message queue, or one of the
1039 * special values K_NO_WAIT and K_FOREVER.
1040 *
1041 * @return 0 if successful, -ENOMSG if failed immediately or after queue purge,
1042 * -EAGAIN if timed out
1043 *
1044 * @sa K_NO_WAIT, K_FOREVER
1045 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001046extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001047
1048/**
1049 * @brief Obtain a message from a message queue.
1050 *
1051 * This routine fetches the oldest item from the message queue. When the message
1052 * queue is found empty, the routine will wait either until an item is added to
1053 * the message queue or until the specified time limit is reached.
1054 *
1055 * @param q Pointer to the message queue object.
1056 * @param data Pointer to message data area.
1057 * @param timeout Number of milliseconds to wait to obtain message, or one of
1058 * the special values K_NO_WAIT and K_FOREVER.
1059 *
1060 * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out
1061 *
1062 * @sa K_NO_WAIT, K_FOREVER
1063 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001064extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001065
1066/**
1067 * @brief Purge contents of a message queue.
1068 *
1069 * Discards all messages currently in the message queue, and cancels
1070 * any "add message" operations initiated by waiting threads.
1071 *
1072 * @param q Pointer to the message queue object.
1073 *
1074 * @return N/A
1075 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001076extern void k_msgq_purge(struct k_msgq *q);
1077
Peter Mitsis67be2492016-10-07 11:44:34 -04001078/**
1079 * @brief Get the number of unused messages
1080 *
1081 * @param q Message queue to query
1082 *
1083 * @return Number of unused messages
1084 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001085static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04001086{
1087 return q->max_msgs - q->used_msgs;
1088}
1089
Peter Mitsisd7a37502016-10-13 11:37:40 -04001090/**
1091 * @brief Get the number of used messages
1092 *
1093 * @param q Message queue to query
1094 *
1095 * @return Number of used messages
1096 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001097static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001098{
1099 return q->used_msgs;
1100}
1101
1102struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001103 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001104 void *addr_in_pool;
1105 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04001106 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001107};
1108
1109/* mailboxes */
1110
1111struct k_mbox_msg {
1112 /** internal use only - needed for legacy API support */
1113 uint32_t _mailbox;
1114 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04001115 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001116 /** application-defined information value */
1117 uint32_t info;
1118 /** sender's message data buffer */
1119 void *tx_data;
1120 /** internal use only - needed for legacy API support */
1121 void *_rx_data;
1122 /** message data block descriptor */
1123 struct k_mem_block tx_block;
1124 /** source thread id */
1125 k_tid_t rx_source_thread;
1126 /** target thread id */
1127 k_tid_t tx_target_thread;
1128 /** internal use only - thread waiting on send (may be a dummy) */
1129 k_tid_t _syncing_thread;
1130#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1131 /** internal use only - semaphore used during asynchronous send */
1132 struct k_sem *_async_sem;
1133#endif
1134};
1135
1136struct k_mbox {
1137 _wait_q_t tx_msg_queue;
1138 _wait_q_t rx_msg_queue;
1139
1140 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox);
1141};
1142
1143#define K_MBOX_INITIALIZER(obj) \
1144 { \
1145 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
1146 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
1147 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1148 }
1149
Peter Mitsis12092702016-10-14 12:57:23 -04001150/**
1151 * @brief Define a mailbox
1152 *
1153 * This declares and initializes a mailbox. The new mailbox can be passed to
Peter Mitsisd7a37502016-10-13 11:37:40 -04001154 * the kernel's mailbox functions.
Peter Mitsis12092702016-10-14 12:57:23 -04001155 *
1156 * @param name Name of the mailbox
1157 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001158#define K_MBOX_DEFINE(name) \
1159 struct k_mbox name = \
1160 K_MBOX_INITIALIZER(name) \
1161
Peter Mitsis12092702016-10-14 12:57:23 -04001162/**
1163 * @brief Initialize a mailbox.
1164 *
1165 * @param mbox Pointer to the mailbox object
1166 *
1167 * @return N/A
1168 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001169extern void k_mbox_init(struct k_mbox *mbox);
1170
Peter Mitsis12092702016-10-14 12:57:23 -04001171/**
1172 * @brief Send a mailbox message in a synchronous manner.
1173 *
1174 * Sends a message to a mailbox and waits for a receiver to process it.
1175 * The message data may be in a buffer, in a memory pool block, or non-existent
1176 * (i.e. empty message).
1177 *
1178 * @param mbox Pointer to the mailbox object.
1179 * @param tx_msg Pointer to transmit message descriptor.
1180 * @param timeout Maximum time (milliseconds) to wait for the message to be
1181 * received (although not necessarily completely processed).
1182 * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long
1183 * as necessary.
1184 *
1185 * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out
1186 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001187extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001188 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001189
1190#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1191/**
1192 * @brief Send a mailbox message in an asynchronous manner.
1193 *
1194 * Sends a message to a mailbox without waiting for a receiver to process it.
1195 * The message data may be in a buffer, in a memory pool block, or non-existent
1196 * (i.e. an empty message). Optionally, the specified semaphore will be given
1197 * by the mailbox when the message has been both received and disposed of
1198 * by the receiver.
1199 *
1200 * @param mbox Pointer to the mailbox object.
1201 * @param tx_msg Pointer to transmit message descriptor.
1202 * @param sem Semaphore identifier, or NULL if none specified.
1203 *
1204 * @return N/A
1205 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001206extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001207 struct k_sem *sem);
Peter Mitsis12092702016-10-14 12:57:23 -04001208#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001209
Peter Mitsis12092702016-10-14 12:57:23 -04001210/**
1211 * @brief Receive a mailbox message.
1212 *
1213 * Receives a message from a mailbox, then optionally retrieves its data
1214 * and disposes of the message.
1215 *
1216 * @param mbox Pointer to the mailbox object.
1217 * @param rx_msg Pointer to receive message descriptor.
1218 * @param buffer Pointer to buffer to receive data.
1219 * (Use NULL to defer data retrieval and message disposal until later.)
1220 * @param timeout Maximum time (milliseconds) to wait for a message.
1221 * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long as
1222 * necessary.
1223 *
1224 * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out
1225 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001226extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001227 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001228
1229/**
1230 * @brief Retrieve mailbox message data into a buffer.
1231 *
1232 * Completes the processing of a received message by retrieving its data
1233 * into a buffer, then disposing of the message.
1234 *
1235 * Alternatively, this routine can be used to dispose of a received message
1236 * without retrieving its data.
1237 *
1238 * @param rx_msg Pointer to receive message descriptor.
1239 * @param buffer Pointer to buffer to receive data. (Use NULL to discard data.)
1240 *
1241 * @return N/A
1242 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001243extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04001244
1245/**
1246 * @brief Retrieve mailbox message data into a memory pool block.
1247 *
1248 * Completes the processing of a received message by retrieving its data
1249 * into a memory pool block, then disposing of the message. The memory pool
1250 * block that results from successful retrieval must be returned to the pool
1251 * once the data has been processed, even in cases where zero bytes of data
1252 * are retrieved.
1253 *
1254 * Alternatively, this routine can be used to dispose of a received message
1255 * without retrieving its data. In this case there is no need to return a
1256 * memory pool block to the pool.
1257 *
1258 * This routine allocates a new memory pool block for the data only if the
1259 * data is not already in one. If a new block cannot be allocated, the routine
1260 * returns a failure code and the received message is left unchanged. This
1261 * permits the caller to reattempt data retrieval at a later time or to dispose
1262 * of the received message without retrieving its data.
1263 *
1264 * @param rx_msg Pointer to receive message descriptor.
1265 * @param pool Memory pool identifier. (Use NULL to discard data.)
1266 * @param block Pointer to area to hold memory pool block info.
1267 * @param timeout Maximum time (milliseconds) to wait for a memory pool block.
1268 * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long as
1269 * necessary.
1270 *
1271 * @return 0 if successful, -ENOMEM if failed immediately, -EAGAIN if timed out
1272 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001273extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001274 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001275 struct k_mem_block *block, int32_t timeout);
1276
1277/* pipes */
1278
1279struct k_pipe {
1280 unsigned char *buffer; /* Pipe buffer: may be NULL */
1281 size_t size; /* Buffer size */
1282 size_t bytes_used; /* # bytes used in buffer */
1283 size_t read_index; /* Where in buffer to read from */
1284 size_t write_index; /* Where in buffer to write */
1285
1286 struct {
1287 _wait_q_t readers; /* Reader wait queue */
1288 _wait_q_t writers; /* Writer wait queue */
1289 } wait_q;
1290
1291 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe);
1292};
1293
Peter Mitsise5d9c582016-10-14 14:44:57 -04001294#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001295 { \
1296 .buffer = pipe_buffer, \
1297 .size = pipe_buffer_size, \
1298 .bytes_used = 0, \
1299 .read_index = 0, \
1300 .write_index = 0, \
1301 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
1302 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
1303 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1304 }
1305
Peter Mitsise5d9c582016-10-14 14:44:57 -04001306#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
1307 static unsigned char __noinit __aligned(pipe_align) \
1308 _k_pipe_buf_##name[pipe_buffer_size]; \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001309 struct k_pipe name = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04001310 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001311
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001312/**
1313 * @brief Runtime initialization of a pipe
1314 *
1315 * @param pipe Pointer to pipe to initialize
1316 * @param buffer Pointer to buffer to use for pipe's ring buffer
1317 * @param size Size of the pipe's ring buffer
1318 *
1319 * @return N/A
1320 */
1321extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
1322 size_t size);
1323
1324/**
1325 * @brief Put a message into the specified pipe
1326 *
1327 * This routine synchronously adds a message into the pipe specified by
1328 * @a pipe. It will wait up to @a timeout for the pipe to accept
Peter Mitsise5d9c582016-10-14 14:44:57 -04001329 * @a bytes_to_write bytes of data. If by @a timeout, the pipe could not
1330 * accept @a min_xfer bytes of data, it fails. Fewer than @a min_xfer will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001331 * only ever be written to the pipe if K_NO_WAIT < @a timeout < K_FOREVER.
1332 *
1333 * @param pipe Pointer to the pipe
Peter Mitsise5d9c582016-10-14 14:44:57 -04001334 * @param data Data to put into the pipe
1335 * @param bytes_to_write Desired number of bytes to put into the pipe
1336 * @param bytes_written Number of bytes the pipe accepted
1337 * @param min_xfer Minimum number of bytes accepted for success
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001338 * @param timeout Maximum number of milliseconds to wait
1339 *
Peter Mitsise5d9c582016-10-14 14:44:57 -04001340 * @retval 0 At least @a min_xfer were sent
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001341 * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT)
Peter Mitsise5d9c582016-10-14 14:44:57 -04001342 * @retval -EAGAIN Fewer than @a min_xfer were sent
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001343 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001344extern int k_pipe_put(struct k_pipe *pipe, void *data,
1345 size_t bytes_to_write, size_t *bytes_written,
1346 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001347
1348/**
1349 * @brief Get a message from the specified pipe
1350 *
1351 * This routine synchronously retrieves a message from the pipe specified by
Peter Mitsise5d9c582016-10-14 14:44:57 -04001352 * @a pipe. It will wait up to @a timeout to retrieve @a bytes_to_read
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001353 * bytes of data from the pipe. If by @a timeout, the pipe could not retrieve
Peter Mitsise5d9c582016-10-14 14:44:57 -04001354 * @a min_xfer bytes of data, it fails. Fewer than @a min_xfer will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001355 * only ever be retrieved from the pipe if K_NO_WAIT < @a timeout < K_FOREVER.
1356 *
1357 * @param pipe Pointer to the pipe
Peter Mitsise5d9c582016-10-14 14:44:57 -04001358 * @param data Location to place retrieved data
1359 * @param bytes_to_read Desired number of bytes to retrieve from the pipe
1360 * @param bytes_read Number of bytes retrieved from the pipe
1361 * @param min_xfer Minimum number of bytes retrieved for success
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001362 * @param timeout Maximum number of milliseconds to wait
1363 *
Peter Mitsise5d9c582016-10-14 14:44:57 -04001364 * @retval 0 At least @a min_xfer were transferred
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001365 * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT)
Peter Mitsise5d9c582016-10-14 14:44:57 -04001366 * @retval -EAGAIN Fewer than @a min_xfer were retrieved
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001367 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001368extern int k_pipe_get(struct k_pipe *pipe, void *data,
1369 size_t bytes_to_read, size_t *bytes_read,
1370 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001371
Peter Mitsis2fef0232016-10-14 14:53:44 -04001372#if (CONFIG_NUM_PIPE_ASYNC_MSGS > 0)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001373/**
1374 * @brief Send a message to the specified pipe
1375 *
1376 * This routine asynchronously sends a message from the pipe specified by
1377 * @a pipe. Once all @a size bytes have been accepted by the pipe, it will
1378 * free the memory block @a block and give the semaphore @a sem (if specified).
1379 * Up to CONFIG_NUM_PIPE_ASYNC_MSGS asynchronous pipe messages can be in-flight
1380 * at any given time.
1381 *
1382 * @param pipe Pointer to the pipe
1383 * @param block Memory block containing data to send
1384 * @param size Number of data bytes in memory block to send
1385 * @param sem Semaphore to signal upon completion (else NULL)
1386 *
1387 * @retval N/A
1388 */
1389extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
1390 size_t size, struct k_sem *sem);
Peter Mitsis2fef0232016-10-14 14:53:44 -04001391#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001392
1393/**
1394 * memory management
1395 */
1396
1397/* memory maps */
1398
1399struct k_mem_map {
1400 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04001401 uint32_t num_blocks;
1402 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001403 char *buffer;
1404 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04001405 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001406
1407 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_map);
1408};
1409
Peter Mitsis578f9112016-10-07 13:50:31 -04001410#define K_MEM_MAP_INITIALIZER(obj, map_buffer, map_block_size, map_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001411 { \
1412 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1413 .num_blocks = map_num_blocks, \
1414 .block_size = map_block_size, \
1415 .buffer = map_buffer, \
1416 .free_list = NULL, \
1417 .num_used = 0, \
1418 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1419 }
1420
Peter Mitsis578f9112016-10-07 13:50:31 -04001421/**
1422 * @brief Define a memory map
1423 *
1424 * This declares and initializes a memory map whose buffer is aligned to
1425 * a @a map_align -byte boundary. The new memory map can be passed to the
1426 * kernel's memory map functions.
1427 *
1428 * Note that for each of the blocks in the memory map to be aligned to
1429 * @a map_align bytes, then @a map_block_size must be a multiple of
1430 * @a map_align.
1431 *
1432 * @param name Name of the memory map
1433 * @param map_block_size Size of each block in the buffer (in bytes)
1434 * @param map_num_blocks Number blocks in the buffer
1435 * @param map_align Alignment of the memory map's buffer (power of 2)
1436 */
1437#define K_MEM_MAP_DEFINE(name, map_block_size, map_num_blocks, map_align) \
Allan Stephens35ffaff2016-10-21 14:31:37 -05001438 char __noinit __aligned(map_align) \
Peter Mitsis578f9112016-10-07 13:50:31 -04001439 _k_mem_map_buf_##name[(map_num_blocks) * (map_block_size)]; \
1440 struct k_mem_map name \
1441 __in_section(_k_mem_map_ptr, private, mem_map) = \
1442 K_MEM_MAP_INITIALIZER(name, _k_mem_map_buf_##name, \
1443 map_block_size, map_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001444
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04001445/**
1446 * @brief Initialize a memory map.
1447 *
1448 * Initializes the memory map and creates its list of free blocks.
1449 *
1450 * @param map Pointer to the memory map object
1451 * @param buffer Pointer to buffer used for the blocks.
1452 * @param block_size Size of each block, in bytes.
1453 * @param num_blocks Number of blocks.
1454 *
1455 * @return N/A
1456 */
Peter Mitsis578f9112016-10-07 13:50:31 -04001457extern void k_mem_map_init(struct k_mem_map *map, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04001458 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04001459
1460/**
1461 * @brief Allocate a memory map block.
1462 *
1463 * Takes a block from the list of unused blocks.
1464 *
1465 * @param map Pointer to memory map object.
1466 * @param mem Pointer to area to receive block address.
1467 * @param timeout Maximum time (milliseconds) to wait for allocation to
1468 * complete. Use K_NO_WAIT to return immediately, or K_FOREVER to wait
1469 * as long as necessary.
1470 *
1471 * @return 0 if successful, -ENOMEM if failed immediately, -EAGAIN if timed out
1472 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001473extern int k_mem_map_alloc(struct k_mem_map *map, void **mem, int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04001474
1475/**
1476 * @brief Free a memory map block.
1477 *
1478 * Gives block to a waiting thread if there is one, otherwise returns it to
1479 * the list of unused blocks.
1480 *
1481 * @param map Pointer to memory map object.
1482 * @param mem Pointer to area to containing block address.
1483 *
1484 * @return N/A
1485 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001486extern void k_mem_map_free(struct k_mem_map *map, void **mem);
1487
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04001488/**
1489 * @brief Get the number of used memory blocks
1490 *
1491 * This routine gets the current number of used memory blocks in the
1492 * specified pool. It should be used for stats purposes only as that
1493 * value may potentially be out-of-date by the time it is used.
1494 *
1495 * @param map Memory map to query
1496 *
1497 * @return Number of used memory blocks
1498 */
Peter Mitsisfb02d572016-10-13 16:55:45 -04001499static inline uint32_t k_mem_map_num_used_get(struct k_mem_map *map)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001500{
1501 return map->num_used;
1502}
1503
Peter Mitsisc001aa82016-10-13 13:53:37 -04001504/**
1505 * @brief Get the number of unused memory blocks
1506 *
1507 * This routine gets the current number of unused memory blocks in the
1508 * specified pool. It should be used for stats purposes only as that value
1509 * may potentially be out-of-date by the time it is used.
1510 *
1511 * @param map Memory map to query
1512 *
1513 * @return Number of unused memory blocks
1514 */
Peter Mitsisfb02d572016-10-13 16:55:45 -04001515static inline uint32_t k_mem_map_num_free_get(struct k_mem_map *map)
Peter Mitsisc001aa82016-10-13 13:53:37 -04001516{
1517 return map->num_blocks - map->num_used;
1518}
1519
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001520/* memory pools */
1521
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001522/*
1523 * Memory pool requires a buffer and two arrays of structures for the
1524 * memory block accounting:
1525 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
1526 * status of four blocks of memory.
1527 */
1528struct k_mem_pool_quad_block {
1529 char *mem_blocks; /* pointer to the first of four memory blocks */
1530 uint32_t mem_status; /* four bits. If bit is set, memory block is
1531 allocated */
1532};
1533/*
1534 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
1535 * blocks of one size. Block sizes go from maximal to minimal. Next memory
1536 * block size is 4 times less than the previous one and thus requires 4 times
1537 * bigger array of k_mem_pool_quad_block structures to keep track of the
1538 * memory blocks.
1539 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001540
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001541/*
1542 * The array of k_mem_pool_block_set keeps the information of each array of
1543 * k_mem_pool_quad_block structures
1544 */
1545struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04001546 size_t block_size; /* memory block size */
1547 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001548 struct k_mem_pool_quad_block *quad_block;
1549 int count;
1550};
1551
1552/* Memory pool descriptor */
1553struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04001554 size_t max_block_size;
1555 size_t min_block_size;
1556 uint32_t nr_of_maxblocks;
1557 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001558 struct k_mem_pool_block_set *block_set;
1559 char *bufblock;
1560 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001561 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool);
1562};
1563
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001564#ifdef CONFIG_ARM
1565#define _SECTION_TYPE_SIGN "%"
1566#else
1567#define _SECTION_TYPE_SIGN "@"
1568#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001569
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001570/*
1571 * Static memory pool initialization
1572 */
1573/*
1574 * Use .altmacro to be able to recalculate values and pass them as string
1575 * arguments when calling assembler macros resursively
1576 */
1577__asm__(".altmacro\n\t");
1578
1579/*
1580 * Recursively calls a macro
1581 * The followig global symbols need to be initialized:
1582 * __memory_pool_max_block_size - maximal size of the memory block
1583 * __memory_pool_min_block_size - minimal size of the memory block
1584 * Notes:
1585 * Global symbols are used due the fact that assembler macro allows only
1586 * one argument be passed with the % conversion
1587 * Some assemblers do not get division operation ("/"). To avoid it >> 2
1588 * is used instead of / 4.
1589 * n_max argument needs to go first in the invoked macro, as some
1590 * assemblers concatenate \name and %(\n_max * 4) arguments
1591 * if \name goes first
1592 */
1593__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
1594 ".ifge __memory_pool_max_block_size >> 2 -"
1595 " __memory_pool_min_block_size\n\t\t"
1596 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
1597 "\\macro_name %(\\n_max * 4) \\name\n\t"
1598 ".endif\n\t"
1599 ".endm\n");
1600
1601/*
1602 * Build quad blocks
1603 * Macro allocates space in memory for the array of k_mem_pool_quad_block
1604 * structures and recursively calls itself for the next array, 4 times
1605 * larger.
1606 * The followig global symbols need to be initialized:
1607 * __memory_pool_max_block_size - maximal size of the memory block
1608 * __memory_pool_min_block_size - minimal size of the memory block
1609 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
1610 */
1611__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04001612 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001613 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
1614 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
1615 ".if \\n_max % 4\n\t\t"
1616 ".skip __memory_pool_quad_block_size\n\t"
1617 ".endif\n\t"
1618 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
1619 ".endm\n");
1620
1621/*
1622 * Build block sets and initialize them
1623 * Macro initializes the k_mem_pool_block_set structure and
1624 * recursively calls itself for the next one.
1625 * The followig global symbols need to be initialized:
1626 * __memory_pool_max_block_size - maximal size of the memory block
1627 * __memory_pool_min_block_size - minimal size of the memory block
1628 * __memory_pool_block_set_count, the number of the elements in the
1629 * block set array must be set to 0. Macro calculates it's real
1630 * value.
1631 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
1632 * structures, _build_quad_blocks must be called prior it.
1633 */
1634__asm__(".macro _build_block_set n_max, name\n\t"
1635 ".int __memory_pool_max_block_size\n\t" /* block_size */
1636 ".if \\n_max % 4\n\t\t"
1637 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
1638 ".else\n\t\t"
1639 ".int \\n_max >> 2\n\t"
1640 ".endif\n\t"
1641 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
1642 ".int 0\n\t" /* count */
1643 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
1644 "__do_recurse _build_block_set \\name \\n_max\n\t"
1645 ".endm\n");
1646
1647/*
1648 * Build a memory pool structure and initialize it
1649 * Macro uses __memory_pool_block_set_count global symbol,
1650 * block set addresses and buffer address, it may be called only after
1651 * _build_block_set
1652 */
1653__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
1654 ".pushsection ._k_memory_pool,\"aw\","
1655 _SECTION_TYPE_SIGN "progbits\n\t"
1656 ".globl \\name\n\t"
1657 "\\name:\n\t"
1658 ".int \\max_size\n\t" /* max_block_size */
1659 ".int \\min_size\n\t" /* min_block_size */
1660 ".int \\n_max\n\t" /* nr_of_maxblocks */
1661 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
1662 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
1663 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
1664 ".int 0\n\t" /* wait_q->head */
1665 ".int 0\n\t" /* wait_q->next */
1666 ".popsection\n\t"
1667 ".endm\n");
1668
1669#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
1670 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
1671 _SECTION_TYPE_SIGN "progbits\n\t"); \
1672 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
1673 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
1674 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
1675 STRINGIFY(name) "\n\t"); \
1676 __asm__(".popsection\n\t")
1677
1678#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
1679 __asm__("__memory_pool_block_set_count = 0\n\t"); \
1680 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
1681 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
1682 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04001683 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001684 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
1685 __asm__("_build_block_set " STRINGIFY(n_max) " " \
1686 STRINGIFY(name) "\n\t"); \
1687 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
1688 __asm__(".int __memory_pool_block_set_count\n\t"); \
1689 __asm__(".popsection\n\t"); \
1690 extern uint32_t _mem_pool_block_set_count_##name; \
1691 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
1692
Peter Mitsis2a2b0752016-10-06 16:27:01 -04001693#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
1694 char __noinit __aligned(align) \
1695 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001696
Peter Mitsis2a2b0752016-10-06 16:27:01 -04001697/**
1698 * @brief Define a memory pool
1699 *
1700 * This declares and initializes a memory pool whose buffer is aligned to
1701 * a @a align -byte boundary. The new memory pool can be passed to the
1702 * kernel's memory pool functions.
1703 *
1704 * Note that for each of the minimum sized blocks to be aligned to @a align
1705 * bytes, then @a min_size must be a multiple of @a align.
1706 *
1707 * @param name Name of the memory pool
1708 * @param min_size Minimum block size in the pool
1709 * @param max_size Maximum block size in the pool
1710 * @param n_max Number of maximum sized blocks in the pool
1711 * @param align Alignment of the memory pool's buffer
1712 */
1713#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001714 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
1715 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04001716 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001717 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
1718 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
1719 extern struct k_mem_pool name
1720
1721/*
1722 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
1723 * to __memory_pool_quad_block_size absolute symbol.
1724 * This function does not get called, but compiler calculates the value and
1725 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
1726 */
1727static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
1728{
1729 __asm__(".globl __memory_pool_quad_block_size\n\t"
Andrew Boie431607c2016-10-25 11:47:52 -07001730#ifdef CONFIG_NIOS2
1731 "__memory_pool_quad_block_size = %0\n\t"
1732#else
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001733 "__memory_pool_quad_block_size = %c0\n\t"
Andrew Boie431607c2016-10-25 11:47:52 -07001734#endif
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001735 :
1736 : "n"(sizeof(struct k_mem_pool_quad_block)));
1737}
1738
Peter Mitsis937042c2016-10-13 13:18:26 -04001739/**
1740 * @brief Allocate memory from a memory pool
1741 *
1742 * @param pool Pointer to the memory pool object
1743 * @param block Pointer to the allocated memory's block descriptor
1744 * @param size Minimum number of bytes to allocate
1745 * @param timeout Maximum time (milliseconds) to wait for operation to
1746 * complete. Use K_NO_WAIT to return immediately, or K_FOREVER
1747 * to wait as long as necessary.
1748 *
1749 * @return 0 on success, -ENOMEM on failure
1750 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001751extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04001752 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04001753
1754/**
1755 * @brief Return previously allocated memory to its memory pool
1756 *
1757 * @param block Pointer to allocated memory's block descriptor
1758 *
1759 * @return N/A
1760 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001761extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04001762
1763/**
1764 * @brief Defragment the specified memory pool
1765 *
1766 * @param pool Pointer to the memory pool object
1767 *
1768 * @return N/A
1769 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001770extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04001771
1772/**
Allan Stephens480a1312016-10-13 15:44:48 -05001773 * @brief Allocate memory from heap
Peter Mitsis937042c2016-10-13 13:18:26 -04001774 *
Allan Stephens480a1312016-10-13 15:44:48 -05001775 * This routine provides traditional malloc() semantics. The memory is
1776 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04001777 *
1778 * @param size Size of memory requested by the caller (in bytes)
1779 *
1780 * @return Address of the allocated memory on success; otherwise NULL
1781 */
Peter Mitsis5f399242016-10-13 13:26:25 -04001782extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04001783
1784/**
Allan Stephens480a1312016-10-13 15:44:48 -05001785 * @brief Free memory allocated from heap
1786 *
1787 * This routine provides traditional free() semantics. The memory being
1788 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04001789 *
1790 * @param ptr Pointer to previously allocated memory
1791 *
1792 * @return N/A
1793 */
1794extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001795
1796/*
1797 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
1798 * hook into the device subsystem, which itself uses nanokernel semaphores,
1799 * and thus currently requires the definition of nano_sem.
1800 */
1801#include <legacy.h>
1802#include <arch/cpu.h>
1803
1804/*
1805 * private APIs that are utilized by one or more public APIs
1806 */
1807
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001808extern int _is_thread_essential(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001809extern void _init_static_threads(void);
1810
1811#ifdef __cplusplus
1812}
1813#endif
1814
1815#endif /* _kernel__h_ */