blob: ba7600ca4fcf4c42c6621812edf9e1dba55a5c79 [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
55#define K_OBJ(name, size) char name[size] __aligned(4)
56
57#if CONFIG_NUM_COOP_PRIORITIES > 0
58#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
59#else
60#define K_HIGHEST_THREAD_PRIO 0
61#endif
62
63#if CONFIG_NUM_PREEMPT_PRIORITIES > 0
64#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
65#else
66#define K_LOWEST_THREAD_PRIO -1
67#endif
68
69#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
70#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
71
72typedef sys_dlist_t _wait_q_t;
73
74#ifdef CONFIG_DEBUG_TRACING_KERNEL_OBJECTS
75#define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type) struct type *__next
76#define _DEBUG_TRACING_KERNEL_OBJECTS_INIT .__next = NULL,
77#else
78#define _DEBUG_TRACING_KERNEL_OBJECTS_INIT
79#define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type)
80#endif
81
82#define k_thread tcs
83struct tcs;
84struct k_mutex;
85struct k_sem;
86struct k_event;
87struct k_msgq;
88struct k_mbox;
89struct k_pipe;
90struct k_fifo;
91struct k_lifo;
92struct k_stack;
93struct k_mem_map;
94struct k_mem_pool;
95struct k_timer;
96
97typedef struct tcs *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040098
99/* threads/scheduler/execution contexts */
100
101enum execution_context_types {
102 K_ISR = 0,
103 K_COOP_THREAD,
104 K_PREEMPT_THREAD,
105};
106
107struct k_thread_config {
108 char *stack;
109 unsigned stack_size;
110 unsigned prio;
111};
112
113typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
114extern k_tid_t k_thread_spawn(char *stack, unsigned stack_size,
115 void (*entry)(void *, void *, void*),
116 void *p1, void *p2, void *p3,
117 int32_t prio, uint32_t options, int32_t delay);
118
119extern void k_sleep(int32_t duration);
120extern void k_busy_wait(uint32_t usec_to_wait);
121extern void k_yield(void);
122extern void k_wakeup(k_tid_t thread);
123extern k_tid_t k_current_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400124extern int k_current_priority_get(void);
125extern int k_thread_cancel(k_tid_t thread);
126
127extern void k_thread_abort(k_tid_t thread);
128
129#define K_THREAD_GROUP_EXE 0x1
130#define K_THREAD_GROUP_SYS 0x2
131#define K_THREAD_GROUP_FPU 0x4
132
133/* XXX - doesn't work because CONFIG_ARCH is a string */
134#if 0
135/* arch-specific groups */
136#if CONFIG_ARCH == "x86"
137#define K_THREAD_GROUP_SSE 0x4
138#endif
139#endif
140
141#ifdef CONFIG_NANO_TIMEOUTS
142#define _THREAD_TIMEOUT_INIT(obj) \
143 (obj).nano_timeout = { \
144 .node = { {0}, {0} }, \
145 .tcs = NULL, \
146 .wait_q = NULL, \
147 .delta_ticks_from_prev = -1, \
148 },
149#else
150#define _THREAD_TIMEOUT_INIT(obj)
151#endif
152
153#ifdef CONFIG_ERRNO
154#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
155#else
156#define _THREAD_ERRNO_INIT(obj)
157#endif
158
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400159struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400160 uint32_t init_groups;
161 int init_prio;
162 void (*init_entry)(void *, void *, void *);
163 void *init_p1;
164 void *init_p2;
165 void *init_p3;
166 void (*init_abort)(void);
167 union {
168 char *init_stack;
169 struct k_thread *thread;
170 };
171 unsigned int init_stack_size;
172};
173
174#define K_THREAD_INITIALIZER(stack, stack_size, \
175 entry, p1, p2, p3, \
176 abort, prio, groups) \
177 { \
178 .init_groups = (groups), \
179 .init_prio = (prio), \
180 .init_entry = entry, \
181 .init_p1 = (void *)p1, \
182 .init_p2 = (void *)p2, \
183 .init_p3 = (void *)p3, \
184 .init_abort = abort, \
185 .init_stack = (stack), \
186 .init_stack_size = (stack_size), \
187 }
188
189/*
190 * Define thread initializer object and initialize it
191 * NOTE: For thread group functions thread initializers must be organized
192 * in array and thus should not have gaps between them.
193 * On x86 by default compiler aligns them by 32 byte boundary. To prevent
194 * this 32-bit alignment in specified here.
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400195 * _static_thread_data structure sise needs to be kept 32-bit aligned as well
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400196 */
197#define K_THREAD_OBJ_DEFINE(name, stack_size, \
198 entry, p1, p2, p3, \
199 abort, prio, groups) \
200 extern void entry(void *, void *, void *); \
201 char __noinit __stack _k_thread_obj_##name[stack_size]; \
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400202 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400203 __in_section(_k_task_list, private, task) = \
204 K_THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
205 entry, p1, p2, p3, abort, prio, groups)
206
207#define K_THREAD_DEFINE(name, stack_size, entry, p1, p2, p3, \
208 abort, prio, groups) \
209 K_THREAD_OBJ_DEFINE(name, stack_size, entry, p1, p2, p3, \
210 abort, prio, groups); \
211 k_tid_t const name = (k_tid_t)_k_thread_obj_##name
212
213/* extern int k_thread_prio_get(k_tid_t thread); in sched.h */
214extern void k_thread_priority_set(k_tid_t thread, int prio);
215
Benjamin Walsh71d52282016-09-29 10:49:48 -0400216extern void k_thread_suspend(k_tid_t thread);
217extern void k_thread_resume(k_tid_t thread);
218extern void k_thread_abort_handler_set(void (*handler)(void));
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400219#if 0
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400220extern int k_thread_entry_set(k_tid_t thread,
221 void (*entry)(void*, void*, void*);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400222#endif
223
224extern void k_sched_time_slice_set(int32_t slice, int prio);
225extern int k_workload_get(void);
226extern void k_workload_time_slice_set(int32_t slice);
227
228extern int k_am_in_isr(void);
229
230extern void k_thread_custom_data_set(void *value);
231extern void *k_thread_custom_data_get(void);
232
233/**
234 * kernel timing
235 */
236
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400237#include <sys_clock.h>
238
239/* private internal time manipulation (users should never play with ticks) */
240
241static int64_t __ticks_to_ms(int64_t ticks)
242{
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400243#if CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400244 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400245#else
246 __ASSERT(ticks == 0, "");
247 return 0;
248#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400249}
250
251
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400252/* timeouts */
253
254struct _timeout;
255typedef void (*_timeout_func_t)(struct _timeout *t);
256
257struct _timeout {
258 sys_dlist_t node;
259 struct tcs *tcs;
260 sys_dlist_t *wait_q;
261 int32_t delta_ticks_from_prev;
262 _timeout_func_t func;
263};
264
265/* timers */
266
267struct k_timer {
268 /*
269 * _timeout structure must be first here if we want to use
270 * dynamic timer allocation. timeout.node is used in the double-linked
271 * list of free timers
272 */
273 struct _timeout timeout;
274
275 /* wait queue for the threads waiting on this timer */
276 _wait_q_t wait_q;
277
278 /* runs in ISR context */
279 void (*handler)(void *);
280 void *handler_arg;
281
282 /* runs in the context of the thread that calls k_timer_stop() */
283 void (*stop_handler)(void *);
284 void *stop_handler_arg;
285
286 /* timer period */
287 int32_t period;
288
289 /* user supplied data pointer returned to the thread*/
290 void *user_data;
291
292 /* user supplied data pointer */
293 void *user_data_internal;
294
295 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_timer);
296};
297
298#define K_TIMER_INITIALIZER(obj) \
299 { \
300 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
301 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
302 }
303
304#define K_TIMER_DEFINE(name) \
305 struct k_timer name = K_TIMER_INITIALIZER(name)
306
307extern void k_timer_init(struct k_timer *timer, void *data);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700308
309#if (CONFIG_NUM_DYNAMIC_TIMERS > 0)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400310extern struct k_timer *k_timer_alloc(void);
311extern void k_timer_free(struct k_timer *timer);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700312#endif
313
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400314extern void k_timer_start(struct k_timer *timer,
315 int32_t duration, int32_t period,
316 void (*handler)(void *), void *handler_arg,
317 void (*stop_handler)(void *), void *stop_handler_arg);
318extern void k_timer_restart(struct k_timer *timer, int32_t duration,
319 int32_t period);
320extern void k_timer_stop(struct k_timer *timer);
321extern int k_timer_test(struct k_timer *timer, void **data, int wait);
322extern int32_t k_timer_remaining_get(struct k_timer *timer);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400323
324
325/**
326 * @brief Get the time elapsed since the system booted (uptime)
327 *
328 * @return The current uptime of the system in ms
329 */
330
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400331extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400332
333/**
334 * @brief Get the lower 32-bit of time elapsed since the system booted (uptime)
335 *
336 * This function is potentially less onerous in both the time it takes to
337 * execute, the interrupt latency it introduces and the amount of 64-bit math
338 * it requires than k_uptime_get(), but it only provides an uptime value of
339 * 32-bits. The user must handle possible rollovers/spillovers.
340 *
341 * At a rate of increment of 1000 per second, it rolls over approximately every
342 * 50 days.
343 *
344 * @return The current uptime of the system in ms
345 */
346
347extern uint32_t k_uptime_get_32(void);
348
349/**
350 * @brief Get the difference between a reference time and the current uptime
351 *
352 * @param reftime A pointer to a reference time. It is updated with the current
353 * uptime upon return.
354 *
355 * @return The delta between the reference time and the current uptime.
356 */
357
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400358extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400359
360/**
361 * @brief Get the difference between a reference time and the current uptime
362 *
363 * The 32-bit version of k_uptime_delta(). It has the same perks and issues as
364 * k_uptime_get_32().
365 *
366 * @param reftime A pointer to a reference time. It is updated with the current
367 * uptime upon return.
368 *
369 * @return The delta between the reference time and the current uptime.
370 */
371
372extern uint32_t k_uptime_delta_32(int64_t *reftime);
373
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400374extern bool k_timer_pool_is_empty(void);
375
376extern uint32_t k_cycle_get_32(void);
377
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400378/**
379 * data transfers (basic)
380 */
381
382/* fifos */
383
384struct k_fifo {
385 _wait_q_t wait_q;
386 sys_slist_t data_q;
387
388 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo);
389};
390
391extern void k_fifo_init(struct k_fifo *fifo);
392extern void k_fifo_put(struct k_fifo *fifo, void *data);
393extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
394extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
395extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
396
397#define K_FIFO_INITIALIZER(obj) \
398 { \
399 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh9091e5d2016-09-30 10:42:47 -0400400 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400401 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
402 }
403
404#define K_FIFO_DEFINE(name) \
Benjamin Walsh0bee91d2016-09-15 17:16:38 -0400405 struct k_fifo name = K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400406
407/* lifos */
408
409struct k_lifo {
410 _wait_q_t wait_q;
411 void *list;
412
413 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo);
414};
415
416extern void k_lifo_init(struct k_lifo *lifo);
417extern void k_lifo_put(struct k_lifo *lifo, void *data);
418extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
419
420#define K_LIFO_INITIALIZER(obj) \
421 { \
422 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
423 .list = NULL, \
424 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
425 }
426
427#define K_LIFO_DEFINE(name) \
Benjamin Walsh0bee91d2016-09-15 17:16:38 -0400428 struct k_lifo name = K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400429
430/* stacks */
431
432struct k_stack {
433 _wait_q_t wait_q;
434 uint32_t *base, *next, *top;
435
436 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack);
437};
438
439extern void k_stack_init(struct k_stack *stack, int num_entries);
440extern void k_stack_init_with_buffer(struct k_stack *stack, int num_entries,
441 uint32_t *buffer);
442extern void k_stack_push(struct k_stack *stack, uint32_t data);
443extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
444
445#define K_STACK_INITIALIZER(obj, stack_num_entries, stack_buffer) \
446 { \
447 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
448 .base = stack_buffer, \
449 .next = stack_buffer, \
450 .top = stack_buffer + stack_num_entries, \
451 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
452 }
453
454#define K_STACK_DEFINE(name, stack_num_entries) \
455 uint32_t __noinit _k_stack_buf_##name[stack_num_entries]; \
Benjamin Walsh0bee91d2016-09-15 17:16:38 -0400456 struct k_stack name = \
457 K_STACK_INITIALIZER(name, stack_num_entries, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400458 _k_stack_buf_##name); \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400459
460#define K_STACK_SIZE(stack_num_entries) \
461 (sizeof(struct k_stack) + (stack_num_entries * sizeof(uint32_t)))
462
463/**
464 * workqueues
465 */
466
467struct k_work;
468
469typedef void (*k_work_handler_t)(struct k_work *);
470
471/**
472 * A workqueue is a fiber that executes @ref k_work items that are
473 * queued to it. This is useful for drivers which need to schedule
474 * execution of code which might sleep from ISR context. The actual
475 * fiber identifier is not stored in the structure in order to save
476 * space.
477 */
478struct k_work_q {
479 struct k_fifo fifo;
480};
481
482/**
483 * @brief Work flags.
484 */
485enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -0300486 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400487};
488
489/**
490 * @brief An item which can be scheduled on a @ref k_work_q.
491 */
492struct k_work {
493 void *_reserved; /* Used by k_fifo implementation. */
494 k_work_handler_t handler;
495 atomic_t flags[1];
496};
497
498/**
499 * @brief Statically initialize work item
500 */
501#define K_WORK_INITIALIZER(work_handler) \
502 { \
503 ._reserved = NULL, \
504 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300505 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400506 }
507
508/**
509 * @brief Dynamically initialize work item
510 */
511static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
512{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300513 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400514 work->handler = handler;
515}
516
517/**
518 * @brief Submit a work item to a workqueue.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +0300519 *
520 * This procedure schedules a work item to be processed.
521 * In the case where the work item has already been submitted and is pending
522 * execution, calling this function will result in a no-op. In this case, the
523 * work item must not be modified externally (e.g. by the caller of this
524 * function), since that could cause the work item to be processed in a
525 * corrupted state.
526 *
527 * @param work_q to schedule the work item
528 * @param work work item
529 *
530 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400531 */
532static inline void k_work_submit_to_queue(struct k_work_q *work_q,
533 struct k_work *work)
534{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +0300535 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400536 k_fifo_put(&work_q->fifo, work);
537 }
538}
539
540/**
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300541 * @brief Check if work item is pending.
542 */
543static inline int k_work_pending(struct k_work *work)
544{
Iván Briano9c7b5ea2016-10-04 18:11:05 -0300545 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300546}
547
548/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400549 * @brief Start a new workqueue. This routine can be called from either
550 * fiber or task context.
551 */
552extern void k_work_q_start(struct k_work_q *work_q,
553 const struct k_thread_config *config);
554
555#if defined(CONFIG_NANO_TIMEOUTS)
556
557 /*
558 * @brief An item which can be scheduled on a @ref k_work_q with a
559 * delay.
560 */
561struct k_delayed_work {
562 struct k_work work;
563 struct _timeout timeout;
564 struct k_work_q *work_q;
565};
566
567/**
568 * @brief Initialize delayed work
569 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400570extern void k_delayed_work_init(struct k_delayed_work *work,
571 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400572
573/**
574 * @brief Submit a delayed work item to a workqueue.
575 *
576 * This procedure schedules a work item to be processed after a delay.
577 * Once the delay has passed, the work item is submitted to the work queue:
578 * at this point, it is no longer possible to cancel it. Once the work item's
579 * handler is about to be executed, the work is considered complete and can be
580 * resubmitted.
581 *
582 * Care must be taken if the handler blocks or yield as there is no implicit
583 * mutual exclusion mechanism. Such usage is not recommended and if necessary,
584 * it should be explicitly done between the submitter and the handler.
585 *
586 * @param work_q to schedule the work item
587 * @param work Delayed work item
588 * @param ticks Ticks to wait before scheduling the work item
589 *
590 * @return 0 in case of success or negative value in case of error.
591 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400592extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
593 struct k_delayed_work *work,
594 int32_t ticks);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400595
596/**
597 * @brief Cancel a delayed work item
598 *
599 * This procedure cancels a scheduled work item. If the work has been completed
600 * or is idle, this will do nothing. The only case where this can fail is when
601 * the work has been submitted to the work queue, but the handler has not run
602 * yet.
603 *
604 * @param work Delayed work item to be canceled
605 *
606 * @return 0 in case of success or negative value in case of error.
607 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400608extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400609
610#endif /* CONFIG_NANO_TIMEOUTS */
611
612#if defined(CONFIG_SYSTEM_WORKQUEUE)
613
614extern struct k_work_q k_sys_work_q;
615
616/*
617 * @brief Submit a work item to the system workqueue.
618 *
619 * @ref k_work_submit_to_queue
620 *
621 * When using the system workqueue it is not recommended to block or yield
622 * on the handler since its fiber is shared system wide it may cause
623 * unexpected behavior.
624 */
625static inline void k_work_submit(struct k_work *work)
626{
627 k_work_submit_to_queue(&k_sys_work_q, work);
628}
629
630#if defined(CONFIG_NANO_TIMEOUTS)
631/*
632 * @brief Submit a delayed work item to the system workqueue.
633 *
634 * @ref k_delayed_work_submit_to_queue
635 *
636 * When using the system workqueue it is not recommended to block or yield
637 * on the handler since its fiber is shared system wide it may cause
638 * unexpected behavior.
639 */
640static inline int k_delayed_work_submit(struct k_delayed_work *work,
641 int ticks)
642{
643 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, ticks);
644}
645
646#endif /* CONFIG_NANO_TIMEOUTS */
647#endif /* CONFIG_SYSTEM_WORKQUEUE */
648
649/**
650 * synchronization
651 */
652
653/* mutexes */
654
655struct k_mutex {
656 _wait_q_t wait_q;
657 struct tcs *owner;
658 uint32_t lock_count;
659 int owner_orig_prio;
660#ifdef CONFIG_OBJECT_MONITOR
661 int num_lock_state_changes;
662 int num_conflicts;
663#endif
664
665 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex);
666};
667
668#ifdef CONFIG_OBJECT_MONITOR
669#define _MUTEX_INIT_OBJECT_MONITOR \
670 .num_lock_state_changes = 0, .num_conflicts = 0,
671#else
672#define _MUTEX_INIT_OBJECT_MONITOR
673#endif
674
675#define K_MUTEX_INITIALIZER(obj) \
676 { \
677 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
678 .owner = NULL, \
679 .lock_count = 0, \
680 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
681 _MUTEX_INIT_OBJECT_MONITOR \
682 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
683 }
684
685#define K_MUTEX_DEFINE(name) \
686 struct k_mutex name = K_MUTEX_INITIALIZER(name)
687
688extern void k_mutex_init(struct k_mutex *mutex);
689extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
690extern void k_mutex_unlock(struct k_mutex *mutex);
691
692/* semaphores */
693
694struct k_sem {
695 _wait_q_t wait_q;
696 unsigned int count;
697 unsigned int limit;
698
699 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem);
700};
701
702extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
703 unsigned int limit);
704extern int k_sem_take(struct k_sem *sem, int32_t timeout);
705extern void k_sem_give(struct k_sem *sem);
706
Benjamin Walsh70c68b92016-09-21 10:37:34 -0400707static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400708{
709 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400710}
711
Tomasz Bursztyka276086d2016-09-21 16:03:21 +0200712static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400713{
714 return sem->count;
715}
716
Peter Mitsis45403672016-09-09 14:24:06 -0400717#ifdef CONFIG_SEMAPHORE_GROUPS
718/**
719 * @brief Take the first available semaphore
720 *
721 * Given a list of semaphore pointers, this routine will attempt to take one
722 * of them, waiting up to a maximum of @a timeout ms to do so. The taken
723 * semaphore is identified by @a sem (set to NULL on error).
724 *
725 * Be aware that the more semaphores specified in the group, the more stack
726 * space is required by the waiting thread.
727 *
728 * @param sem_array Array of semaphore pointers terminated by a K_END entry
729 * @param sem Identifies the semaphore that was taken
730 * @param timeout Maximum number of milliseconds to wait
731 *
732 * @retval 0 A semaphore was successfully taken
733 * @retval -EBUSY No semaphore was available (@a timeout = K_NO_WAIT)
734 * @retval -EAGAIN Time out occurred while waiting for semaphore
735 */
736
737extern int k_sem_group_take(struct k_sem *sem_array[], struct k_sem **sem,
738 int32_t timeout);
739
740/**
741 * @brief Give all the semaphores in the group
742 *
743 * This routine will give each semaphore in the array of semaphore pointers.
744 *
745 * @param sem_array Array of semaphore pointers terminated by a K_END entry
746 *
747 * @return N/A
748 */
749extern void k_sem_group_give(struct k_sem *sem_array[]);
750
751/**
752 * @brief Reset the count to zero on each semaphore in the array
753 *
754 * This routine resets the count of each semaphore in the group to zero.
755 * Note that it does NOT have any impact on any thread that might have
756 * been previously pending on any of the semaphores.
757 *
758 * @param sem_array Array of semaphore pointers terminated by a K_END entry
759 *
760 * @return N/A
761 */
762extern void k_sem_group_reset(struct k_sem *sem_array[]);
763#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400764
765#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
766 { \
767 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
768 .count = initial_count, \
769 .limit = count_limit, \
770 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
771 }
772
773#define K_SEM_DEFINE(name, initial_count, count_limit) \
774 struct k_sem name = \
775 K_SEM_INITIALIZER(name, initial_count, count_limit)
776
777/* events */
778
779#define K_EVT_DEFAULT NULL
780#define K_EVT_IGNORE ((void *)(-1))
781
782typedef int (*k_event_handler_t)(struct k_event *);
783
784struct k_event {
785 k_event_handler_t handler;
786 atomic_t send_count;
787 struct k_work work_item;
788 struct k_sem sem;
789
790 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_event);
791};
792
793extern void _k_event_deliver(struct k_work *work);
794
795#define K_EVENT_INITIALIZER(obj, event_handler) \
796 { \
797 .handler = (k_event_handler_t)event_handler, \
798 .send_count = ATOMIC_INIT(0), \
799 .work_item = K_WORK_INITIALIZER(_k_event_deliver), \
800 .sem = K_SEM_INITIALIZER(obj.sem, 0, 1), \
801 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
802 }
803
804#define K_EVENT_DEFINE(name, event_handler) \
805 struct k_event name \
806 __in_section(_k_event_list, event, name) = \
807 K_EVENT_INITIALIZER(name, event_handler)
808
809extern void k_event_init(struct k_event *event, k_event_handler_t handler);
810extern int k_event_recv(struct k_event *event, int32_t timeout);
811extern void k_event_send(struct k_event *event);
812
813/**
814 * data transfers (complex)
815 */
816
817/* message queues */
818
819struct k_msgq {
820 _wait_q_t wait_q;
821 uint32_t msg_size;
822 uint32_t max_msgs;
823 char *buffer_start;
824 char *buffer_end;
825 char *read_ptr;
826 char *write_ptr;
827 uint32_t used_msgs;
828
829 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq);
830};
831
832#define K_MSGQ_INITIALIZER(obj, q_depth, q_width, q_buffer) \
833 { \
834 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
835 .max_msgs = q_depth, \
836 .msg_size = q_width, \
837 .buffer_start = q_buffer, \
838 .buffer_end = q_buffer + (q_depth * q_width), \
839 .read_ptr = q_buffer, \
840 .write_ptr = q_buffer, \
841 .used_msgs = 0, \
842 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
843 }
844
845#define K_MSGQ_DEFINE(name, q_depth, q_width) \
846 static char __noinit _k_fifo_buf_##name[(q_depth) * (q_width)]; \
847 struct k_msgq name = \
848 K_MSGQ_INITIALIZER(name, q_depth, q_width, _k_fifo_buf_##name)
849
850#define K_MSGQ_SIZE(q_depth, q_width) \
851 ((sizeof(struct k_msgq)) + ((q_width) * (q_depth)))
852
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400853extern void k_msgq_init(struct k_msgq *q, uint32_t msg_size,
854 uint32_t max_msgs, char *buffer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400855extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
856extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
857extern void k_msgq_purge(struct k_msgq *q);
858
859static inline int k_msgq_num_used_get(struct k_msgq *q)
860{
861 return q->used_msgs;
862}
863
864struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -0400865 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400866 void *addr_in_pool;
867 void *data;
868 uint32_t req_size;
869};
870
871/* mailboxes */
872
873struct k_mbox_msg {
874 /** internal use only - needed for legacy API support */
875 uint32_t _mailbox;
876 /** size of message (in bytes) */
877 uint32_t size;
878 /** application-defined information value */
879 uint32_t info;
880 /** sender's message data buffer */
881 void *tx_data;
882 /** internal use only - needed for legacy API support */
883 void *_rx_data;
884 /** message data block descriptor */
885 struct k_mem_block tx_block;
886 /** source thread id */
887 k_tid_t rx_source_thread;
888 /** target thread id */
889 k_tid_t tx_target_thread;
890 /** internal use only - thread waiting on send (may be a dummy) */
891 k_tid_t _syncing_thread;
892#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
893 /** internal use only - semaphore used during asynchronous send */
894 struct k_sem *_async_sem;
895#endif
896};
897
898struct k_mbox {
899 _wait_q_t tx_msg_queue;
900 _wait_q_t rx_msg_queue;
901
902 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox);
903};
904
905#define K_MBOX_INITIALIZER(obj) \
906 { \
907 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
908 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
909 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
910 }
911
912#define K_MBOX_DEFINE(name) \
913 struct k_mbox name = \
914 K_MBOX_INITIALIZER(name) \
915
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400916extern void k_mbox_init(struct k_mbox *mbox);
917
918extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *msg,
919 int32_t timeout);
920extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *msg,
921 struct k_sem *sem);
922
923extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *msg,
924 void *buffer, int32_t timeout);
925extern void k_mbox_data_get(struct k_mbox_msg *msg, void *buffer);
Peter Mitsis0cb65c32016-09-29 14:07:36 -0400926extern int k_mbox_data_block_get(struct k_mbox_msg *msg,
927 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400928 struct k_mem_block *block, int32_t timeout);
929
930/* pipes */
931
932struct k_pipe {
933 unsigned char *buffer; /* Pipe buffer: may be NULL */
934 size_t size; /* Buffer size */
935 size_t bytes_used; /* # bytes used in buffer */
936 size_t read_index; /* Where in buffer to read from */
937 size_t write_index; /* Where in buffer to write */
938
939 struct {
940 _wait_q_t readers; /* Reader wait queue */
941 _wait_q_t writers; /* Writer wait queue */
942 } wait_q;
943
944 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe);
945};
946
947#define K_PIPE_INITIALIZER(obj, pipe_buffer_size, pipe_buffer) \
948 { \
949 .buffer = pipe_buffer, \
950 .size = pipe_buffer_size, \
951 .bytes_used = 0, \
952 .read_index = 0, \
953 .write_index = 0, \
954 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
955 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
956 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
957 }
958
959#define K_PIPE_DEFINE(name, pipe_buffer_size) \
960 static unsigned char __noinit _k_pipe_buf_##name[pipe_buffer_size]; \
961 struct k_pipe name = \
962 K_PIPE_INITIALIZER(name, pipe_buffer_size, _k_pipe_buf_##name)
963
964#define K_PIPE_SIZE(buffer_size) (sizeof(struct k_pipe) + buffer_size)
965
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400966/**
967 * @brief Runtime initialization of a pipe
968 *
969 * @param pipe Pointer to pipe to initialize
970 * @param buffer Pointer to buffer to use for pipe's ring buffer
971 * @param size Size of the pipe's ring buffer
972 *
973 * @return N/A
974 */
975extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
976 size_t size);
977
978/**
979 * @brief Put a message into the specified pipe
980 *
981 * This routine synchronously adds a message into the pipe specified by
982 * @a pipe. It will wait up to @a timeout for the pipe to accept
983 * @a num_bytes_to_write bytes of data. If by @a timeout, the pipe could not
984 * accept @a min_bytes bytes of data, it fails. Fewer than @a min_bytes will
985 * only ever be written to the pipe if K_NO_WAIT < @a timeout < K_FOREVER.
986 *
987 * @param pipe Pointer to the pipe
988 * @param buffer Data to put into the pipe
989 * @param num_bytes_to_write Desired number of bytes to put into the pipe
990 * @param num_bytes_written Number of bytes the pipe accepted
991 * @param min_bytes Minimum number of bytes accepted for success
992 * @param timeout Maximum number of milliseconds to wait
993 *
994 * @retval 0 At least @a min_bytes were sent
995 * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT)
996 * @retval -EAGAIN Fewer than @a min_bytes were sent
997 */
998extern int k_pipe_put(struct k_pipe *pipe, void *buffer,
999 size_t num_bytes_to_write, size_t *num_bytes_written,
1000 size_t min_bytes, int32_t timeout);
1001
1002/**
1003 * @brief Get a message from the specified pipe
1004 *
1005 * This routine synchronously retrieves a message from the pipe specified by
1006 * @a pipe. It will wait up to @a timeout to retrieve @a num_bytes_to_read
1007 * bytes of data from the pipe. If by @a timeout, the pipe could not retrieve
1008 * @a min_bytes bytes of data, it fails. Fewer than @a min_bytes will
1009 * only ever be retrieved from the pipe if K_NO_WAIT < @a timeout < K_FOREVER.
1010 *
1011 * @param pipe Pointer to the pipe
1012 * @param buffer Location to place retrieved data
1013 * @param num_bytes_to_read Desired number of bytes to retrieve from the pipe
1014 * @param num_bytes_read Number of bytes retrieved from the pipe
1015 * @param min_bytes Minimum number of bytes retrieved for success
1016 * @param timeout Maximum number of milliseconds to wait
1017 *
1018 * @retval 0 At least @a min_bytes were transferred
1019 * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT)
1020 * @retval -EAGAIN Fewer than @a min_bytes were retrieved
1021 */
1022extern int k_pipe_get(struct k_pipe *pipe, void *buffer,
1023 size_t num_bytes_to_read, size_t *num_bytes_read,
1024 size_t min_bytes, int32_t timeout);
1025
1026/**
1027 * @brief Send a message to the specified pipe
1028 *
1029 * This routine asynchronously sends a message from the pipe specified by
1030 * @a pipe. Once all @a size bytes have been accepted by the pipe, it will
1031 * free the memory block @a block and give the semaphore @a sem (if specified).
1032 * Up to CONFIG_NUM_PIPE_ASYNC_MSGS asynchronous pipe messages can be in-flight
1033 * at any given time.
1034 *
1035 * @param pipe Pointer to the pipe
1036 * @param block Memory block containing data to send
1037 * @param size Number of data bytes in memory block to send
1038 * @param sem Semaphore to signal upon completion (else NULL)
1039 *
1040 * @retval N/A
1041 */
1042extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
1043 size_t size, struct k_sem *sem);
1044
1045/**
1046 * memory management
1047 */
1048
1049/* memory maps */
1050
1051struct k_mem_map {
1052 _wait_q_t wait_q;
1053 int num_blocks;
1054 int block_size;
1055 char *buffer;
1056 char *free_list;
1057 int num_used;
1058
1059 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_map);
1060};
1061
1062#define K_MEM_MAP_INITIALIZER(obj, map_num_blocks, map_block_size, \
1063 map_buffer) \
1064 { \
1065 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1066 .num_blocks = map_num_blocks, \
1067 .block_size = map_block_size, \
1068 .buffer = map_buffer, \
1069 .free_list = NULL, \
1070 .num_used = 0, \
1071 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1072 }
1073
1074#define K_MEM_MAP_DEFINE(name, map_num_blocks, map_block_size) \
1075 char _k_mem_map_buf_##name[(map_num_blocks) * (map_block_size)]; \
1076 struct k_mem_map name \
1077 __in_section(_k_mem_map_ptr, private, mem_map) = \
1078 K_MEM_MAP_INITIALIZER(name, map_num_blocks, \
1079 map_block_size, _k_mem_map_buf_##name)
1080
1081#define K_MEM_MAP_SIZE(map_num_blocks, map_block_size) \
1082 (sizeof(struct k_mem_map) + ((map_num_blocks) * (map_block_size)))
1083
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001084extern void k_mem_map_init(struct k_mem_map *map, int num_blocks,
1085 int block_size, void *buffer);
1086extern int k_mem_map_alloc(struct k_mem_map *map, void **mem, int32_t timeout);
1087extern void k_mem_map_free(struct k_mem_map *map, void **mem);
1088
1089static inline int k_mem_map_num_used_get(struct k_mem_map *map)
1090{
1091 return map->num_used;
1092}
1093
1094/* memory pools */
1095
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001096/*
1097 * Memory pool requires a buffer and two arrays of structures for the
1098 * memory block accounting:
1099 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
1100 * status of four blocks of memory.
1101 */
1102struct k_mem_pool_quad_block {
1103 char *mem_blocks; /* pointer to the first of four memory blocks */
1104 uint32_t mem_status; /* four bits. If bit is set, memory block is
1105 allocated */
1106};
1107/*
1108 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
1109 * blocks of one size. Block sizes go from maximal to minimal. Next memory
1110 * block size is 4 times less than the previous one and thus requires 4 times
1111 * bigger array of k_mem_pool_quad_block structures to keep track of the
1112 * memory blocks.
1113 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001114
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001115/*
1116 * The array of k_mem_pool_block_set keeps the information of each array of
1117 * k_mem_pool_quad_block structures
1118 */
1119struct k_mem_pool_block_set {
1120 int block_size; /* memory block size */
1121 int nr_of_entries; /* nr of quad block structures in the array */
1122 struct k_mem_pool_quad_block *quad_block;
1123 int count;
1124};
1125
1126/* Memory pool descriptor */
1127struct k_mem_pool {
1128 int max_block_size;
1129 int min_block_size;
1130 int nr_of_maxblocks;
1131 int nr_of_block_sets;
1132 struct k_mem_pool_block_set *block_set;
1133 char *bufblock;
1134 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001135 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool);
1136};
1137
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001138#ifdef CONFIG_ARM
1139#define _SECTION_TYPE_SIGN "%"
1140#else
1141#define _SECTION_TYPE_SIGN "@"
1142#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001143
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001144/*
1145 * Static memory pool initialization
1146 */
1147/*
1148 * Use .altmacro to be able to recalculate values and pass them as string
1149 * arguments when calling assembler macros resursively
1150 */
1151__asm__(".altmacro\n\t");
1152
1153/*
1154 * Recursively calls a macro
1155 * The followig global symbols need to be initialized:
1156 * __memory_pool_max_block_size - maximal size of the memory block
1157 * __memory_pool_min_block_size - minimal size of the memory block
1158 * Notes:
1159 * Global symbols are used due the fact that assembler macro allows only
1160 * one argument be passed with the % conversion
1161 * Some assemblers do not get division operation ("/"). To avoid it >> 2
1162 * is used instead of / 4.
1163 * n_max argument needs to go first in the invoked macro, as some
1164 * assemblers concatenate \name and %(\n_max * 4) arguments
1165 * if \name goes first
1166 */
1167__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
1168 ".ifge __memory_pool_max_block_size >> 2 -"
1169 " __memory_pool_min_block_size\n\t\t"
1170 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
1171 "\\macro_name %(\\n_max * 4) \\name\n\t"
1172 ".endif\n\t"
1173 ".endm\n");
1174
1175/*
1176 * Build quad blocks
1177 * Macro allocates space in memory for the array of k_mem_pool_quad_block
1178 * structures and recursively calls itself for the next array, 4 times
1179 * larger.
1180 * The followig global symbols need to be initialized:
1181 * __memory_pool_max_block_size - maximal size of the memory block
1182 * __memory_pool_min_block_size - minimal size of the memory block
1183 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
1184 */
1185__asm__(".macro _build_quad_blocks n_max, name\n\t"
1186 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
1187 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
1188 ".if \\n_max % 4\n\t\t"
1189 ".skip __memory_pool_quad_block_size\n\t"
1190 ".endif\n\t"
1191 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
1192 ".endm\n");
1193
1194/*
1195 * Build block sets and initialize them
1196 * Macro initializes the k_mem_pool_block_set structure and
1197 * recursively calls itself for the next one.
1198 * The followig global symbols need to be initialized:
1199 * __memory_pool_max_block_size - maximal size of the memory block
1200 * __memory_pool_min_block_size - minimal size of the memory block
1201 * __memory_pool_block_set_count, the number of the elements in the
1202 * block set array must be set to 0. Macro calculates it's real
1203 * value.
1204 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
1205 * structures, _build_quad_blocks must be called prior it.
1206 */
1207__asm__(".macro _build_block_set n_max, name\n\t"
1208 ".int __memory_pool_max_block_size\n\t" /* block_size */
1209 ".if \\n_max % 4\n\t\t"
1210 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
1211 ".else\n\t\t"
1212 ".int \\n_max >> 2\n\t"
1213 ".endif\n\t"
1214 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
1215 ".int 0\n\t" /* count */
1216 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
1217 "__do_recurse _build_block_set \\name \\n_max\n\t"
1218 ".endm\n");
1219
1220/*
1221 * Build a memory pool structure and initialize it
1222 * Macro uses __memory_pool_block_set_count global symbol,
1223 * block set addresses and buffer address, it may be called only after
1224 * _build_block_set
1225 */
1226__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
1227 ".pushsection ._k_memory_pool,\"aw\","
1228 _SECTION_TYPE_SIGN "progbits\n\t"
1229 ".globl \\name\n\t"
1230 "\\name:\n\t"
1231 ".int \\max_size\n\t" /* max_block_size */
1232 ".int \\min_size\n\t" /* min_block_size */
1233 ".int \\n_max\n\t" /* nr_of_maxblocks */
1234 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
1235 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
1236 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
1237 ".int 0\n\t" /* wait_q->head */
1238 ".int 0\n\t" /* wait_q->next */
1239 ".popsection\n\t"
1240 ".endm\n");
1241
1242#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
1243 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
1244 _SECTION_TYPE_SIGN "progbits\n\t"); \
1245 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
1246 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
1247 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
1248 STRINGIFY(name) "\n\t"); \
1249 __asm__(".popsection\n\t")
1250
1251#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
1252 __asm__("__memory_pool_block_set_count = 0\n\t"); \
1253 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
1254 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
1255 _SECTION_TYPE_SIGN "progbits\n\t"); \
1256 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
1257 __asm__("_build_block_set " STRINGIFY(n_max) " " \
1258 STRINGIFY(name) "\n\t"); \
1259 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
1260 __asm__(".int __memory_pool_block_set_count\n\t"); \
1261 __asm__(".popsection\n\t"); \
1262 extern uint32_t _mem_pool_block_set_count_##name; \
1263 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
1264
1265#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max) \
1266 char __noinit _mem_pool_buffer_##name[(max_size) * (n_max)]
1267
1268#define K_MEMORY_POOL_DEFINE(name, min_size, max_size, n_max) \
1269 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
1270 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
1271 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max); \
1272 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
1273 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
1274 extern struct k_mem_pool name
1275
1276/*
1277 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
1278 * to __memory_pool_quad_block_size absolute symbol.
1279 * This function does not get called, but compiler calculates the value and
1280 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
1281 */
1282static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
1283{
1284 __asm__(".globl __memory_pool_quad_block_size\n\t"
1285 "__memory_pool_quad_block_size = %c0\n\t"
1286 :
1287 : "n"(sizeof(struct k_mem_pool_quad_block)));
1288}
1289
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001290#define K_MEM_POOL_SIZE(max_block_size, num_max_blocks) \
1291 (sizeof(struct k_mem_pool) + ((max_block_size) * (num_max_blocks)))
1292
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001293extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001294 int size, int32_t timeout);
1295extern void k_mem_pool_free(struct k_mem_block *block);
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001296extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001297extern void *k_malloc(uint32_t size);
1298extern void k_free(void *p);
1299
1300/*
1301 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
1302 * hook into the device subsystem, which itself uses nanokernel semaphores,
1303 * and thus currently requires the definition of nano_sem.
1304 */
1305#include <legacy.h>
1306#include <arch/cpu.h>
1307
1308/*
1309 * private APIs that are utilized by one or more public APIs
1310 */
1311
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001312extern int _is_thread_essential(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001313extern void _init_static_threads(void);
1314
1315#ifdef __cplusplus
1316}
1317#endif
1318
1319#endif /* _kernel__h_ */