Zephyr API Documentation 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
rtio.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: Copyright (c) 2022 Intel Corporation
3 * SPDX-FileCopyrightText: <text>Copyright (c) 2026 Infineon Technologies AG,
4 * or an affiliate of Infineon Technologies AG. All rights reserved.</text>
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
28
29#ifndef ZEPHYR_INCLUDE_RTIO_RTIO_H_
30#define ZEPHYR_INCLUDE_RTIO_RTIO_H_
31
32#include <string.h>
33
35#include <zephyr/device.h>
36#include <zephyr/kernel.h>
37#include <zephyr/sys/__assert.h>
38#include <zephyr/sys/atomic.h>
40#include <zephyr/sys/util.h>
42#include <zephyr/rtio/sqe.h>
43#include <zephyr/rtio/cqe.h>
44#include <zephyr/rtio/iodev.h>
45
46#ifdef __cplusplus
47extern "C" {
48#endif
49
50
59
71struct rtio {
72#ifdef CONFIG_RTIO_SUBMIT_SEM
73 /* A wait semaphore which may suspend the calling thread
74 * to wait for some number of completions when calling submit
75 */
76 struct k_sem *submit_sem;
77
78 uint32_t submit_count;
79#endif
80
81#ifdef CONFIG_RTIO_CONSUME_SEM
82 /* A wait semaphore which may suspend the calling thread
83 * to wait for some number of completions while consuming
84 * them from the completion queue
85 */
86 struct k_sem *consume_sem;
87#endif
88
91
94
96 struct rtio_sqe_pool *sqe_pool;
97
99 struct rtio_cqe_pool *cqe_pool;
100
101#ifdef CONFIG_RTIO_SYS_MEM_BLOCKS
102 /* Mem block pool */
103 struct sys_mem_blocks *block_pool;
104#endif
105
107 struct mpsc sq;
108
110 struct mpsc cq;
111};
112
113/* @cond ignore */
114#define Z_RTIO_DEFINE(name, _sqe_pool, _cqe_pool, _block_pool) \
115 IF_ENABLED(CONFIG_RTIO_SUBMIT_SEM, \
116 (static K_SEM_DEFINE(CONCAT(_submit_sem_, name), 0, K_SEM_MAX_LIMIT))) \
117 IF_ENABLED(CONFIG_RTIO_CONSUME_SEM, \
118 (static K_SEM_DEFINE(CONCAT(_consume_sem_, name), 0, K_SEM_MAX_LIMIT))) \
119 STRUCT_SECTION_ITERABLE(rtio, name) = { \
120 IF_ENABLED(CONFIG_RTIO_SUBMIT_SEM, (.submit_sem = &CONCAT(_submit_sem_, name),)) \
121 IF_ENABLED(CONFIG_RTIO_SUBMIT_SEM, (.submit_count = 0,)) \
122 IF_ENABLED(CONFIG_RTIO_CONSUME_SEM, (.consume_sem = &CONCAT(_consume_sem_, name),))\
123 .cq_count = ATOMIC_INIT(0), \
124 .xcqcnt = ATOMIC_INIT(0), \
125 .sqe_pool = _sqe_pool, \
126 .cqe_pool = _cqe_pool, \
127 IF_ENABLED(CONFIG_RTIO_SYS_MEM_BLOCKS, (.block_pool = _block_pool,)) \
128 .sq = MPSC_INIT((name.sq)), \
129 .cq = MPSC_INIT((name.cq)), \
130 }
131/* @endcond */
132
140#define RTIO_DEFINE(name, sq_sz, cq_sz) \
141 Z_RTIO_SQE_POOL_DEFINE(CONCAT(name, _sqe_pool), sq_sz); \
142 Z_RTIO_CQE_POOL_DEFINE(CONCAT(name, _cqe_pool), cq_sz); \
143 Z_RTIO_DEFINE(name, &CONCAT(name, _sqe_pool), \
144 &CONCAT(name, _cqe_pool), NULL)
145
146
154static inline size_t rtio_mempool_block_size(const struct rtio *r)
155{
156#ifndef CONFIG_RTIO_SYS_MEM_BLOCKS
157 ARG_UNUSED(r);
158 return 0;
159#else
160 if (r == NULL || r->block_pool == NULL) {
161 return 0;
162 }
163 return BIT(r->block_pool->info.blk_sz_shift);
164#endif
165}
166
174#if defined(CONFIG_RTIO_SYS_MEM_BLOCKS) || defined(__DOXYGEN__)
175static inline uint16_t __rtio_compute_mempool_block_index(const struct rtio *r, const void *ptr)
176{
177 uintptr_t addr = (uintptr_t)ptr;
178 struct sys_mem_blocks *mem_pool = r->block_pool;
179 uint32_t block_size = rtio_mempool_block_size(r);
180
181 uintptr_t buff = (uintptr_t)mem_pool->buffer;
182 uint32_t buff_size = mem_pool->info.num_blocks * block_size;
183
184 if (addr < buff || addr >= buff + buff_size) {
185 return UINT16_MAX;
186 }
187 return (addr - buff) / block_size;
188}
189#endif
190
192
193static inline int rtio_block_pool_alloc(struct rtio *r, size_t min_sz,
194 size_t max_sz, uint8_t **buf, uint32_t *buf_len)
195{
196#ifndef CONFIG_RTIO_SYS_MEM_BLOCKS
197 ARG_UNUSED(r);
198 ARG_UNUSED(min_sz);
199 ARG_UNUSED(max_sz);
200 ARG_UNUSED(buf);
201 ARG_UNUSED(buf_len);
202 return -ENOTSUP;
203#else
204 const uint32_t block_size = rtio_mempool_block_size(r);
205 uint32_t bytes = max_sz;
206
207 /* Not every context has a block pool and the block size may return 0 in
208 * that case
209 */
210 if (block_size == 0) {
211 return -ENOMEM;
212 }
213
214 do {
215 size_t num_blks = DIV_ROUND_UP(bytes, block_size);
216 int rc = sys_mem_blocks_alloc_contiguous(r->block_pool, num_blks, (void **)buf);
217
218 if (rc == 0) {
219 *buf_len = num_blks * block_size;
220 return 0;
221 }
222
223 if (bytes <= block_size) {
224 break;
225 }
226
227 bytes -= block_size;
228 } while (bytes >= min_sz);
229
230 return -ENOMEM;
231#endif
232}
233
234static inline void rtio_block_pool_free(struct rtio *r, void *buf, uint32_t buf_len)
235{
236#ifndef CONFIG_RTIO_SYS_MEM_BLOCKS
237 ARG_UNUSED(r);
238 ARG_UNUSED(buf);
239 ARG_UNUSED(buf_len);
240#else
241 size_t num_blks = buf_len >> r->block_pool->info.blk_sz_shift;
242
243 sys_mem_blocks_free_contiguous(r->block_pool, buf, num_blks);
244#endif
245}
246
248
249
251extern struct k_mem_partition rtio_partition;
252
253
254/* Do not try and reformat the macros */
255
265#if CONFIG_RTIO_BLOCK_POOL_PLACEMENT_DTCM
266#define RTIO_BMEM Z_GENERIC_SECTION(".dtcm_bss") static
267#elif defined(CONFIG_RTIO_BLOCK_POOL_PLACEMENT_NOCACHE)
268#define RTIO_BMEM __nocache static
269#else
270#define RTIO_BMEM COND_CODE_1(CONFIG_USERSPACE, (K_APP_BMEM(rtio_partition) static), (static))
271#endif
272
282#if CONFIG_RTIO_BLOCK_POOL_PLACEMENT_DTCM
283#define RTIO_DMEM Z_GENERIC_SECTION(".dtcm_data") static
284#elif defined(CONFIG_RTIO_BLOCK_POOL_PLACEMENT_NOCACHE)
285#define RTIO_DMEM __nocache_load static
286#else
287#define RTIO_DMEM COND_CODE_1(CONFIG_USERSPACE, (K_APP_DMEM(rtio_partition) static), (static))
288#endif
289
290/* clang-format off */
291/* @cond ignore */
292#define Z_RTIO_BLOCK_POOL_DEFINE(name, blk_sz, blk_cnt, blk_align) \
293 RTIO_BMEM uint8_t __aligned(WB_UP(blk_align)) \
294 CONCAT(_block_pool_, name)[(blk_cnt) * WB_UP(blk_sz)]; \
295 _SYS_MEM_BLOCKS_DEFINE_WITH_EXT_BUF(name, WB_UP(blk_sz), (blk_cnt), \
296 CONCAT(_block_pool_, name), RTIO_DMEM)
297
298/* @endcond */
299
300
301/* clang-format on */
302
313#define RTIO_DEFINE_WITH_MEMPOOL(name, sq_sz, cq_sz, num_blks, blk_size, balign) \
314 Z_RTIO_SQE_POOL_DEFINE(name##_sqe_pool, sq_sz); \
315 Z_RTIO_CQE_POOL_DEFINE(name##_cqe_pool, cq_sz); \
316 Z_RTIO_BLOCK_POOL_DEFINE(name##_block_pool, blk_size, num_blks, balign); \
317 Z_RTIO_DEFINE(name, &name##_sqe_pool, &name##_cqe_pool, &name##_block_pool)
318
319/* clang-format on */
320
328static inline uint32_t rtio_sqe_acquirable(struct rtio *r)
329{
330 return r->sqe_pool->pool_free;
331}
332
341static inline struct rtio_sqe *rtio_sqe_acquire(struct rtio *r)
342{
343 SYS_PORT_TRACING_FUNC_ENTER(rtio, sqe_acquire, r);
344 struct rtio_iodev_sqe *iodev_sqe = rtio_sqe_pool_alloc(r->sqe_pool);
345
346 if (iodev_sqe == NULL) {
347 SYS_PORT_TRACING_FUNC_EXIT(rtio, sqe_acquire, r, NULL);
348 return NULL;
349 }
350
351 mpsc_push(&r->sq, &iodev_sqe->q);
352
353 SYS_PORT_TRACING_FUNC_EXIT(rtio, sqe_acquire, r, &iodev_sqe->sqe);
354 return &iodev_sqe->sqe;
355}
356
369static inline int rtio_sqe_acquire_array(struct rtio *r, size_t n, struct rtio_sqe **sqes)
370{
371 struct rtio_iodev_sqe *iodev_sqe;
372 size_t i;
373
374 for (i = 0; i < n; i++) {
375 iodev_sqe = rtio_sqe_pool_alloc(r->sqe_pool);
376 if (iodev_sqe == NULL) {
377 break;
378 }
379 sqes[i] = &iodev_sqe->sqe;
380 }
381
382 /* Not enough SQEs in the pool */
383 if (i < n) {
384 while (i > 0) {
385 i--;
386 iodev_sqe = CONTAINER_OF(sqes[i], struct rtio_iodev_sqe, sqe);
387 rtio_sqe_pool_free(r->sqe_pool, iodev_sqe);
388 sqes[i] = NULL;
389 }
390
391 return -ENOMEM;
392 }
393
394 for (i = 0; i < n; i++) {
395 iodev_sqe = CONTAINER_OF(sqes[i], struct rtio_iodev_sqe, sqe);
396 mpsc_push(&r->sq, &iodev_sqe->q);
397 }
398
399 return 0;
400}
401
407static inline void rtio_sqe_drop_all(struct rtio *r)
408{
409 struct rtio_iodev_sqe *iodev_sqe;
410 struct mpsc_node *node = mpsc_pop(&r->sq);
411
412 while (node != NULL) {
413 iodev_sqe = CONTAINER_OF(node, struct rtio_iodev_sqe, q);
414 rtio_sqe_pool_free(r->sqe_pool, iodev_sqe);
415 node = mpsc_pop(&r->sq);
416 }
417}
418
422static inline struct rtio_cqe *rtio_cqe_acquire(struct rtio *r)
423{
424 SYS_PORT_TRACING_FUNC_ENTER(rtio, cqe_acquire, r);
425 struct rtio_cqe *cqe = rtio_cqe_pool_alloc(r->cqe_pool);
426
427 if (cqe == NULL) {
428 SYS_PORT_TRACING_FUNC_EXIT(rtio, cqe_acquire, r, NULL);
429 return NULL;
430 }
431
432 memset(cqe, 0, sizeof(struct rtio_cqe));
433
434 SYS_PORT_TRACING_FUNC_EXIT(rtio, cqe_acquire, r, cqe);
435 return cqe;
436}
437
441static inline void rtio_cqe_produce(struct rtio *r, struct rtio_cqe *cqe)
442{
443 mpsc_push(&r->cq, &cqe->q);
444}
445
457static inline struct rtio_cqe *rtio_cqe_consume(struct rtio *r)
458{
459 SYS_PORT_TRACING_FUNC_ENTER(rtio, cqe_consume, r);
460 struct mpsc_node *node;
461 struct rtio_cqe *cqe = NULL;
462
463#ifdef CONFIG_RTIO_CONSUME_SEM
464 if (k_sem_take(r->consume_sem, K_NO_WAIT) != 0) {
465 SYS_PORT_TRACING_FUNC_EXIT(rtio, cqe_consume, r, NULL);
466 return NULL;
467 }
468#endif
469
470 node = mpsc_pop(&r->cq);
471 if (node == NULL) {
472 SYS_PORT_TRACING_FUNC_EXIT(rtio, cqe_consume, r, NULL);
473 return NULL;
474 }
475 cqe = CONTAINER_OF(node, struct rtio_cqe, q);
476
477 SYS_PORT_TRACING_FUNC_EXIT(rtio, cqe_consume, r, cqe);
478 return cqe;
479}
480
491static inline struct rtio_cqe *rtio_cqe_consume_block(struct rtio *r)
492{
493 struct mpsc_node *node;
494 struct rtio_cqe *cqe;
495
496#ifdef CONFIG_RTIO_CONSUME_SEM
497 k_sem_take(r->consume_sem, K_FOREVER);
498#endif
499 node = mpsc_pop(&r->cq);
500 while (node == NULL) {
501 Z_SPIN_DELAY(1);
502 node = mpsc_pop(&r->cq);
503 }
504 cqe = CONTAINER_OF(node, struct rtio_cqe, q);
505
506 return cqe;
507}
508
515static inline void rtio_cqe_release(struct rtio *r, struct rtio_cqe *cqe)
516{
517 SYS_PORT_TRACING_FUNC(rtio, cqe_release, r, cqe);
518 rtio_cqe_pool_free(r->cqe_pool, cqe);
519}
520
529static inline int rtio_flush_completion_queue(struct rtio *r)
530{
531 struct rtio_cqe *cqe;
532 int res = 0;
533
534 do {
535 cqe = rtio_cqe_consume(r);
536 if (cqe != NULL) {
537 if ((cqe->result < 0) && (res == 0)) {
538 res = cqe->result;
539 }
540 rtio_cqe_release(r, cqe);
541 }
542 } while (cqe != NULL);
543
544 return res;
545}
546
558__syscall void rtio_sqe_signal(struct rtio_sqe *sqe);
559
560static inline void z_impl_rtio_sqe_signal(struct rtio_sqe *sqe)
561{
562 struct rtio_iodev_sqe *iodev_sqe = CONTAINER_OF(sqe, struct rtio_iodev_sqe, sqe);
563
564 if (!atomic_cas(&iodev_sqe->sqe.await.ok, 0, 1)) {
565 iodev_sqe->sqe.await.callback(iodev_sqe, iodev_sqe->sqe.await.userdata);
566 }
567}
568
575static inline uint32_t rtio_cqe_compute_flags(struct rtio_iodev_sqe *iodev_sqe)
576{
577 uint32_t flags = 0;
578
579#ifdef CONFIG_RTIO_SYS_MEM_BLOCKS
580 if (iodev_sqe->sqe.op == RTIO_OP_RX && iodev_sqe->sqe.flags & RTIO_SQE_MEMPOOL_BUFFER) {
581 struct rtio *r = iodev_sqe->r;
582 struct sys_mem_blocks *mem_pool = r->block_pool;
583 unsigned int blk_index = 0;
584 unsigned int blk_count = 0;
585
586 if (iodev_sqe->sqe.rx.buf) {
587 blk_index = (iodev_sqe->sqe.rx.buf - mem_pool->buffer) >>
588 mem_pool->info.blk_sz_shift;
589 blk_count = iodev_sqe->sqe.rx.buf_len >> mem_pool->info.blk_sz_shift;
590 }
591 flags = RTIO_CQE_FLAG_PREP_MEMPOOL(blk_index, blk_count);
592 }
593#else
594 ARG_UNUSED(iodev_sqe);
595#endif
596
597 return flags;
598}
599
615__syscall int rtio_cqe_get_mempool_buffer(const struct rtio *r, struct rtio_cqe *cqe,
616 uint8_t **buff, uint32_t *buff_len);
617
618static inline int z_impl_rtio_cqe_get_mempool_buffer(const struct rtio *r, struct rtio_cqe *cqe,
619 uint8_t **buff, uint32_t *buff_len)
620{
621#ifdef CONFIG_RTIO_SYS_MEM_BLOCKS
623 unsigned int blk_idx = RTIO_CQE_FLAG_MEMPOOL_GET_BLK_IDX(cqe->flags);
624 unsigned int blk_count = RTIO_CQE_FLAG_MEMPOOL_GET_BLK_CNT(cqe->flags);
625 uint32_t blk_size = rtio_mempool_block_size(r);
626
627 *buff_len = blk_count * blk_size;
628
629 if (blk_count > 0) {
630 *buff = r->block_pool->buffer + blk_idx * blk_size;
631
632 __ASSERT_NO_MSG(*buff >= r->block_pool->buffer);
633 __ASSERT_NO_MSG(*buff <
634 r->block_pool->buffer + blk_size * r->block_pool->info.num_blocks);
635 } else {
636 *buff = NULL;
637 }
638 return 0;
639 }
640 return -EINVAL;
641#else
642 ARG_UNUSED(r);
643 ARG_UNUSED(cqe);
644 ARG_UNUSED(buff);
645 ARG_UNUSED(buff_len);
646
647 return -ENOTSUP;
648#endif
649}
650
652
653void rtio_executor_submit(struct rtio *r);
654void rtio_executor_ok(struct rtio_iodev_sqe *iodev_sqe, int result);
655void rtio_executor_err(struct rtio_iodev_sqe *iodev_sqe, int result);
656
658
667static inline void rtio_iodev_sqe_ok(struct rtio_iodev_sqe *iodev_sqe, int result)
668{
669 rtio_executor_ok(iodev_sqe, result);
670}
671
680static inline void rtio_iodev_sqe_err(struct rtio_iodev_sqe *iodev_sqe, int result)
681{
682 rtio_executor_err(iodev_sqe, result);
683}
684
696static inline void rtio_cqe_submit(struct rtio *r, int result, void *userdata, uint32_t flags)
697{
698 SYS_PORT_TRACING_FUNC_ENTER(rtio, cqe_submit, r, result, flags);
699 struct rtio_cqe *cqe = rtio_cqe_acquire(r);
700
701 if (cqe == NULL) {
702 atomic_inc(&r->xcqcnt);
703 } else {
704 cqe->result = result;
705 cqe->userdata = userdata;
706 cqe->flags = flags;
707 rtio_cqe_produce(r, cqe);
708#ifdef CONFIG_RTIO_CONSUME_SEM
709 k_sem_give(r->consume_sem);
710#endif
711 }
712
713 /* atomic_t isn't guaranteed to wrap correctly as it could be signed, so
714 * we must resort to a cas loop.
715 */
716 atomic_t val, new_val;
717
718 do {
719 val = atomic_get(&r->cq_count);
720 new_val = (atomic_t)((uintptr_t)val + 1);
721 } while (!atomic_cas(&r->cq_count, val, new_val));
722
723#ifdef CONFIG_RTIO_SUBMIT_SEM
724 if (r->submit_count > 0) {
725 r->submit_count--;
726 if (r->submit_count == 0) {
727 k_sem_give(r->submit_sem);
728 }
729 }
730#endif
731 SYS_PORT_TRACING_FUNC_EXIT(rtio, cqe_submit, r);
732}
733
734#define __RTIO_MEMPOOL_GET_NUM_BLKS(num_bytes, blk_size) (((num_bytes) + (blk_size)-1) / (blk_size))
735
748static inline int rtio_sqe_rx_buf(const struct rtio_iodev_sqe *iodev_sqe, uint32_t min_buf_len,
749 uint32_t max_buf_len, uint8_t **buf, uint32_t *buf_len)
750{
751 struct rtio_sqe *sqe = (struct rtio_sqe *)&iodev_sqe->sqe;
752
753#ifdef CONFIG_RTIO_SYS_MEM_BLOCKS
754 if (sqe->op == RTIO_OP_RX && sqe->flags & RTIO_SQE_MEMPOOL_BUFFER) {
755 struct rtio *r = iodev_sqe->r;
756
757 if (sqe->rx.buf != NULL) {
758 if (sqe->rx.buf_len < min_buf_len) {
759 return -ENOMEM;
760 }
761 *buf = sqe->rx.buf;
762 *buf_len = sqe->rx.buf_len;
763 return 0;
764 }
765
766 int rc = rtio_block_pool_alloc(r, min_buf_len, max_buf_len, buf, buf_len);
767 if (rc == 0) {
768 sqe->rx.buf = *buf;
769 sqe->rx.buf_len = *buf_len;
770 return 0;
771 }
772
773 return -ENOMEM;
774 }
775#else
776 ARG_UNUSED(max_buf_len);
777#endif
778
779 if (sqe->rx.buf_len < min_buf_len) {
780 return -ENOMEM;
781 }
782
783 *buf = sqe->rx.buf;
784 *buf_len = sqe->rx.buf_len;
785 return 0;
786}
787
802__syscall void rtio_release_buffer(struct rtio *r, void *buff, uint32_t buff_len);
803
804static inline void z_impl_rtio_release_buffer(struct rtio *r, void *buff, uint32_t buff_len)
805{
806#ifdef CONFIG_RTIO_SYS_MEM_BLOCKS
807 if (r == NULL || buff == NULL || r->block_pool == NULL || buff_len == 0) {
808 return;
809 }
810
811 rtio_block_pool_free(r, buff, buff_len);
812#else
813 ARG_UNUSED(r);
814 ARG_UNUSED(buff);
815 ARG_UNUSED(buff_len);
816#endif
817}
818
825static inline void rtio_access_grant(struct rtio *r, struct k_thread *t)
826{
828
829#ifdef CONFIG_RTIO_SUBMIT_SEM
830 k_object_access_grant(r->submit_sem, t);
831#endif
832
833#ifdef CONFIG_RTIO_CONSUME_SEM
834 k_object_access_grant(r->consume_sem, t);
835#endif
836
837#ifdef CONFIG_RTIO_OP_DELAY
838 /* Delay submissions are dispatched to the shared timeout iodev, so a thread
839 * allowed to use this context must also be able to reference it.
840 */
841 k_object_access_grant(&rtio_timeout_iodev, t);
842#endif
843}
844
845
852static inline void rtio_access_revoke(struct rtio *r, struct k_thread *t)
853{
855
856#ifdef CONFIG_RTIO_SUBMIT_SEM
857 k_object_access_revoke(r->submit_sem, t);
858#endif
859
860#ifdef CONFIG_RTIO_CONSUME_SEM
861 k_object_access_revoke(r->consume_sem, t);
862#endif
863
864#ifdef CONFIG_RTIO_OP_DELAY
865 k_object_access_revoke(&rtio_timeout_iodev, t);
866#endif
867}
868
879__syscall int rtio_sqe_cancel(struct rtio_sqe *sqe);
880
881static inline int z_impl_rtio_sqe_cancel(struct rtio_sqe *sqe)
882{
883 SYS_PORT_TRACING_FUNC(rtio, sqe_cancel, sqe);
884 struct rtio_iodev_sqe *iodev_sqe = CONTAINER_OF(sqe, struct rtio_iodev_sqe, sqe);
885
886 do {
887 iodev_sqe->sqe.flags |= RTIO_SQE_CANCELED;
888 iodev_sqe = rtio_iodev_sqe_next(iodev_sqe);
889 } while (iodev_sqe != NULL);
890
891 return 0;
892}
893
909__syscall int rtio_sqe_copy_in_get_handles(struct rtio *r, const struct rtio_sqe *sqes,
910 struct rtio_sqe **handle, size_t sqe_count);
911
912static inline int z_impl_rtio_sqe_copy_in_get_handles(struct rtio *r, const struct rtio_sqe *sqes,
913 struct rtio_sqe **handle,
914 size_t sqe_count)
915{
916 struct rtio_sqe *sqe;
917 uint32_t acquirable = rtio_sqe_acquirable(r);
918
919 if (acquirable < sqe_count) {
920 return -ENOMEM;
921 }
922
923 for (unsigned long i = 0; i < sqe_count; i++) {
924 sqe = rtio_sqe_acquire(r);
925 __ASSERT_NO_MSG(sqe != NULL);
926 if (handle != NULL && i == 0) {
927 *handle = sqe;
928 }
929 *sqe = sqes[i];
930 }
931
932 return 0;
933}
934
951static inline int rtio_sqe_copy_in(struct rtio *r, const struct rtio_sqe *sqes, size_t sqe_count)
952{
953 return rtio_sqe_copy_in_get_handles(r, sqes, NULL, sqe_count);
954}
955
971__syscall int rtio_cqe_copy_out(struct rtio *r,
972 struct rtio_cqe *cqes,
973 size_t cqe_count,
974 k_timeout_t timeout);
975static inline int z_impl_rtio_cqe_copy_out(struct rtio *r,
976 struct rtio_cqe *cqes,
977 size_t cqe_count,
978 k_timeout_t timeout)
979{
980 size_t copied = 0;
981 struct rtio_cqe *cqe;
982 k_timepoint_t end = sys_timepoint_calc(timeout);
983
984 do {
986 : rtio_cqe_consume(r);
987 if (cqe == NULL) {
988 Z_SPIN_DELAY(25);
989 continue;
990 }
991 cqes[copied++] = *cqe;
992 rtio_cqe_release(r, cqe);
993 } while (copied < cqe_count && !sys_timepoint_expired(end));
994
995 return copied;
996}
997
1013__syscall int rtio_submit(struct rtio *r, uint32_t wait_count);
1014
1015#ifdef CONFIG_RTIO_SUBMIT_SEM
1016static inline int z_impl_rtio_submit(struct rtio *r, uint32_t wait_count)
1017{
1018 SYS_PORT_TRACING_FUNC_ENTER(rtio, submit, r, wait_count);
1019 int res = 0;
1020
1021 if (wait_count > 0) {
1022 __ASSERT(!k_is_in_isr(),
1023 "expected rtio submit with wait count to be called from a thread");
1024
1025 k_sem_reset(r->submit_sem);
1026 r->submit_count = wait_count;
1027 }
1028
1029 rtio_executor_submit(r);
1030
1031 if (wait_count > 0) {
1032 res = k_sem_take(r->submit_sem, K_FOREVER);
1033 __ASSERT(res == 0,
1034 "semaphore was reset or timed out while waiting on completions!");
1035 }
1036
1037 SYS_PORT_TRACING_FUNC_EXIT(rtio, submit, r);
1038 return res;
1039}
1040#else
1041static inline int z_impl_rtio_submit(struct rtio *r, uint32_t wait_count)
1042{
1043
1044 SYS_PORT_TRACING_FUNC_ENTER(rtio, submit, r, wait_count);
1045 int res = 0;
1046 uintptr_t cq_count = (uintptr_t)atomic_get(&r->cq_count);
1047 uintptr_t cq_complete_count = cq_count + wait_count;
1048 bool wraps = cq_complete_count < cq_count;
1049
1050 rtio_executor_submit(r);
1051
1052 if (wraps) {
1053 while ((uintptr_t)atomic_get(&r->cq_count) >= cq_count) {
1054 Z_SPIN_DELAY(10);
1055 k_yield();
1056 }
1057 }
1058
1059 while ((uintptr_t)atomic_get(&r->cq_count) < cq_complete_count) {
1060 Z_SPIN_DELAY(10);
1061 k_yield();
1062 }
1063
1064 SYS_PORT_TRACING_FUNC_EXIT(rtio, submit, r);
1065 return res;
1066}
1067#endif /* CONFIG_RTIO_SUBMIT_SEM */
1068
1075
1077 struct rtio **contexts;
1078
1081};
1082
1091__syscall struct rtio *rtio_pool_acquire(struct rtio_pool *pool);
1092
1093static inline struct rtio *z_impl_rtio_pool_acquire(struct rtio_pool *pool)
1094{
1095 struct rtio *r = NULL;
1096
1097 for (size_t i = 0; i < pool->pool_size; i++) {
1098 if (atomic_test_and_set_bit(pool->used, i) == 0) {
1099 r = pool->contexts[i];
1100 break;
1101 }
1102 }
1103
1104 if (r != NULL) {
1106 }
1107
1108 return r;
1109}
1110
1117__syscall void rtio_pool_release(struct rtio_pool *pool, struct rtio *r);
1118
1119static inline void z_impl_rtio_pool_release(struct rtio_pool *pool, struct rtio *r)
1120{
1121
1122 if (k_is_user_context()) {
1124 }
1125
1126 for (size_t i = 0; i < pool->pool_size; i++) {
1127 if (pool->contexts[i] == r) {
1128 atomic_clear_bit(pool->used, i);
1129 break;
1130 }
1131 }
1132}
1133
1134/* clang-format off */
1135
1137
1138#define Z_RTIO_POOL_NAME_N(n, name) \
1139 name##_##n
1140
1141#define Z_RTIO_POOL_DEFINE_N(n, name, sq_sz, cq_sz) \
1142 RTIO_DEFINE(Z_RTIO_POOL_NAME_N(n, name), sq_sz, cq_sz)
1143
1144#define Z_RTIO_POOL_REF_N(n, name) \
1145 &Z_RTIO_POOL_NAME_N(n, name)
1146
1148
1157#define RTIO_POOL_DEFINE(name, pool_sz, sq_sz, cq_sz) \
1158 LISTIFY(pool_sz, Z_RTIO_POOL_DEFINE_N, (;), name, sq_sz, cq_sz); \
1159 static struct rtio *name##_contexts[] = { \
1160 LISTIFY(pool_sz, Z_RTIO_POOL_REF_N, (,), name) \
1161 }; \
1162 ATOMIC_DEFINE(name##_used, pool_sz); \
1163 STRUCT_SECTION_ITERABLE(rtio_pool, name) = { \
1164 .pool_size = pool_sz, \
1165 .contexts = name##_contexts, \
1166 .used = name##_used, \
1167 }
1168
1169/* clang-format on */
1170
1174
1175#ifdef __cplusplus
1176}
1177#endif
1178
1179#include <zephyr/syscalls/rtio.h>
1180
1181#endif /* ZEPHYR_INCLUDE_RTIO_RTIO_H_ */
Header file for the Atomic operations API.
RTIO Completion Queue Events and Related Functions.
static _Bool atomic_test_and_set_bit(atomic_t *target, int bit)
Atomically set a bit and test it.
Definition atomic.h:181
long atomic_t
Atomic integer variable.
Definition atomic_types.h:31
static void atomic_clear_bit(atomic_t *target, int bit)
Atomically clear a bit.
Definition atomic.h:227
atomic_val_t atomic_get(const atomic_t *target)
Atomic get.
atomic_val_t atomic_inc(atomic_t *target)
Atomic increment.
_Bool atomic_cas(atomic_t *target, atomic_val_t old_value, atomic_val_t new_value)
Atomic compare-and-set.
#define K_FOREVER
Generate infinite timeout delay.
Definition kernel.h:1712
#define K_NO_WAIT
Generate null timeout delay.
Definition kernel.h:1602
k_timepoint_t sys_timepoint_calc(k_timeout_t timeout)
Calculate a timepoint value.
static bool sys_timepoint_expired(k_timepoint_t timepoint)
Indicates if timepoint is expired.
Definition clock.h:388
#define K_TIMEOUT_EQ(a, b)
Compare timeouts for equality.
Definition clock.h:80
bool k_is_in_isr(void)
Determine if code is running at interrupt level.
int sys_mem_blocks_free_contiguous(sys_mem_blocks_t *mem_block, void *block, size_t count)
Free contiguous multiple memory blocks.
int sys_mem_blocks_alloc_contiguous(sys_mem_blocks_t *mem_block, size_t count, void **out_block)
Allocate a contiguous set of memory blocks.
static ALWAYS_INLINE void mpsc_push(struct mpsc *q, struct mpsc_node *n)
Push a node.
Definition mpsc_lockfree.h:126
static struct mpsc_node * mpsc_pop(struct mpsc *q)
Pop a node off of the list.
Definition mpsc_lockfree.h:145
#define RTIO_CQE_FLAG_MEMPOOL_GET_BLK_CNT(flags)
Get the block count of a mempool flags.
Definition cqe.h:70
#define RTIO_CQE_FLAG_MEMPOOL_GET_BLK_IDX(flags)
Get the block index of a mempool flags.
Definition cqe.h:62
#define RTIO_CQE_FLAG_MEMPOOL_BUFFER
The entry's buffer was allocated from the RTIO's mempool.
Definition cqe.h:46
#define RTIO_CQE_FLAG_PREP_MEMPOOL(blk_idx, blk_cnt)
Prepare CQE flags for a mempool read.
Definition cqe.h:79
#define RTIO_CQE_FLAG_GET(flags)
Get the flag bits of a CQE flags value.
Definition cqe.h:54
#define RTIO_OP_RX
An operation that receives (reads).
Definition sqe.h:145
#define RTIO_SQE_MEMPOOL_BUFFER
The buffer should be allocated by the RTIO mempool.
Definition sqe.h:106
#define RTIO_SQE_CANCELED
The SQE should not execute if possible.
Definition sqe.h:114
void rtio_pool_release(struct rtio_pool *pool, struct rtio *r)
Return an RTIO context to a pool.
static uint32_t rtio_sqe_acquirable(struct rtio *r)
Count of acquirable submission queue events.
Definition rtio.h:328
struct rtio * rtio_pool_acquire(struct rtio_pool *pool)
Obtain an RTIO context from a pool.
static size_t rtio_mempool_block_size(const struct rtio *r)
Get the mempool block size of the RTIO context.
Definition rtio.h:154
static void rtio_cqe_submit(struct rtio *r, int result, void *userdata, uint32_t flags)
Submit a completion queue event with a given result and userdata.
Definition rtio.h:696
void rtio_release_buffer(struct rtio *r, void *buff, uint32_t buff_len)
Release memory that was allocated by the RTIO's memory pool.
static int rtio_sqe_copy_in(struct rtio *r, const struct rtio_sqe *sqes, size_t sqe_count)
Copy an array of SQEs into the queue.
Definition rtio.h:951
static void rtio_cqe_produce(struct rtio *r, struct rtio_cqe *cqe)
Produce a complete queue event if available.
Definition rtio.h:441
static uint32_t rtio_cqe_compute_flags(struct rtio_iodev_sqe *iodev_sqe)
Compute the CQE flags from the rtio_iodev_sqe entry.
Definition rtio.h:575
int rtio_sqe_copy_in_get_handles(struct rtio *r, const struct rtio_sqe *sqes, struct rtio_sqe **handle, size_t sqe_count)
Copy an array of SQEs into the queue and get resulting handles back.
struct k_mem_partition rtio_partition
The memory partition associated with all RTIO context information.
static struct rtio_sqe * rtio_sqe_acquire(struct rtio *r)
Acquire a single submission queue event if available.
Definition rtio.h:341
static void rtio_sqe_drop_all(struct rtio *r)
Drop all previously acquired sqe.
Definition rtio.h:407
int rtio_cqe_copy_out(struct rtio *r, struct rtio_cqe *cqes, size_t cqe_count, k_timeout_t timeout)
Copy an array of CQEs from the queue.
static int rtio_flush_completion_queue(struct rtio *r)
Flush completion queue.
Definition rtio.h:529
static void rtio_access_revoke(struct rtio *r, struct k_thread *t)
Revoke access to an RTIO context from a user thread.
Definition rtio.h:852
static void rtio_access_grant(struct rtio *r, struct k_thread *t)
Grant access to an RTIO context to a user thread.
Definition rtio.h:825
static void rtio_cqe_release(struct rtio *r, struct rtio_cqe *cqe)
Release consumed completion queue event.
Definition rtio.h:515
static int rtio_sqe_rx_buf(const struct rtio_iodev_sqe *iodev_sqe, uint32_t min_buf_len, uint32_t max_buf_len, uint8_t **buf, uint32_t *buf_len)
Get the buffer associate with the RX submission.
Definition rtio.h:748
static void rtio_iodev_sqe_err(struct rtio_iodev_sqe *iodev_sqe, int result)
Inform the executor of a submissions completion with error.
Definition rtio.h:680
int rtio_sqe_cancel(struct rtio_sqe *sqe)
Attempt to cancel an SQE.
static void rtio_iodev_sqe_ok(struct rtio_iodev_sqe *iodev_sqe, int result)
Inform the executor of a submission completion with success.
Definition rtio.h:667
static struct rtio_cqe * rtio_cqe_acquire(struct rtio *r)
Acquire a complete queue event if available.
Definition rtio.h:422
static struct rtio_cqe * rtio_cqe_consume(struct rtio *r)
Consume a single completion queue event if available.
Definition rtio.h:457
void rtio_sqe_signal(struct rtio_sqe *sqe)
Signal an AWAIT SQE.
static struct rtio_iodev_sqe * rtio_iodev_sqe_next(const struct rtio_iodev_sqe *iodev_sqe)
Get the next sqe in the chain or transaction.
Definition sqe.h:783
int rtio_cqe_get_mempool_buffer(const struct rtio *r, struct rtio_cqe *cqe, uint8_t **buff, uint32_t *buff_len)
Retrieve the mempool buffer that was allocated for the CQE.
static struct rtio_cqe * rtio_cqe_consume_block(struct rtio *r)
Wait for and consume a single completion queue event.
Definition rtio.h:491
static int rtio_sqe_acquire_array(struct rtio *r, size_t n, struct rtio_sqe **sqes)
Acquire a number of submission queue events if available.
Definition rtio.h:369
int rtio_submit(struct rtio *r, uint32_t wait_count)
Submit I/O requests to the underlying executor.
void k_sem_reset(struct k_sem *sem)
Resets a semaphore's count to zero.
void k_sem_give(struct k_sem *sem)
Give a semaphore.
int k_sem_take(struct k_sem *sem, k_timeout_t timeout)
Take a semaphore.
#define SYS_PORT_TRACING_FUNC_ENTER(type, func,...)
Tracing macro for the entry into a function that might or might not return a value.
Definition tracing_macros.h:257
#define SYS_PORT_TRACING_FUNC_EXIT(type, func,...)
Tracing macro for when a function ends its execution.
Definition tracing_macros.h:283
#define SYS_PORT_TRACING_FUNC(type, func,...)
Tracing macro for function calls which are not directly associated with a specific type of object.
Definition tracing_macros.h:244
#define BIT(n)
Unsigned integer with bit position n set (signed in assembly language).
Definition util_macro.h:44
#define CONTAINER_OF(ptr, type, field)
Get a pointer to a structure containing the element.
Definition util.h:281
#define DIV_ROUND_UP(n, d)
Divide and round up.
Definition util.h:348
#define EINVAL
Invalid argument.
Definition errno.h:61
#define ENOMEM
Not enough core.
Definition errno.h:51
#define ENOTSUP
Unsupported value.
Definition errno.h:115
void k_yield(void)
Yield the current thread.
static __attribute_const__ k_tid_t k_current_get(void)
Get thread ID of the current thread.
Definition kernel.h:859
void k_object_access_grant(const void *object, struct k_thread *thread)
Grant a thread access to a kernel object.
void k_object_access_revoke(const void *object, struct k_thread *thread)
Revoke a thread's access to a kernel object.
RTIO I/O Device and Related Functions.
Public kernel APIs.
Memory Blocks Allocator.
flags
Definition parser.h:97
RTIO Submission Queue Events and Related Functions.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
#define UINT16_MAX
Definition stdint.h:28
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:105
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
void * memset(void *buf, int c, size_t n)
Memory Partition.
Definition mem_domain.h:55
Semaphore structure.
Definition kernel.h:3757
Thread Structure.
Definition thread.h:259
Kernel timeout type.
Definition clock.h:65
Kernel timepoint type.
Definition clock.h:291
Queue member.
Definition mpsc_lockfree.h:79
MPSC Queue.
Definition mpsc_lockfree.h:86
A completion queue event.
Definition cqe.h:90
void * userdata
Associated userdata with operation.
Definition cqe.h:96
uint32_t flags
Flags associated with the operation.
Definition cqe.h:97
int32_t result
Result from operation.
Definition cqe.h:95
IO device submission queue entry.
Definition sqe.h:398
struct rtio_sqe sqe
Submission this entry carries.
Definition sqe.h:399
struct rtio * r
RTIO context the submission belongs to.
Definition sqe.h:402
struct mpsc_node q
Link used to enqueue this entry.
Definition sqe.h:400
Pool of RTIO contexts to use with dynamically created threads.
Definition rtio.h:1072
struct rtio ** contexts
Array containing contexts of the pool.
Definition rtio.h:1077
atomic_t * used
Atomic bitmap to signal a member is used/unused.
Definition rtio.h:1080
size_t pool_size
Size of the pool.
Definition rtio.h:1074
A submission queue event.
Definition sqe.h:305
void * userdata
User provided data which is returned upon operation completion.
Definition sqe.h:323
uint8_t op
Op code.
Definition sqe.h:306
struct rtio_sqe::@126267262255374054123217063150244034155174062054::@222067021034074304061254367152327164076222165070 rx
OP_RX.
struct rtio_sqe::@126267262255374054123217063150244034155174062054::@236333123355174166163204241333175337261032350217 await
OP_AWAIT.
uint32_t buf_len
Length of buffer.
Definition sqe.h:330
uint16_t flags
Op Flags.
Definition sqe.h:310
const uint8_t * buf
Buffer to write from.
Definition sqe.h:331
rtio_callback_t callback
Function to run.
Definition sqe.h:348
An RTIO context containing what can be viewed as a pair of queues.
Definition rtio.h:71
struct rtio_cqe_pool * cqe_pool
Completion queue object pool with free list.
Definition rtio.h:99
struct mpsc sq
Submission queue.
Definition rtio.h:107
atomic_t cq_count
Total number of completions.
Definition rtio.h:90
struct rtio_sqe_pool * sqe_pool
Submission queue object pool with free list.
Definition rtio.h:96
atomic_t xcqcnt
Number of completions dropped because no CQE was available.
Definition rtio.h:93
struct mpsc cq
Completion queue.
Definition rtio.h:110
Iterable sections helpers.
Misc utilities.
static bool k_is_user_context(void)
Indicate whether the CPU is currently in user mode.
Definition syscall.h:120