1 /*
2  * Copyright (c) 2025 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <errno.h>
8 #include <zephyr/ztest.h>
9 #include <zephyr/kernel.h>
10 #include <zephyr/sys/atomic.h>
11 #include <zephyr/sys/kobject.h>
12 #include <zephyr/sys/libc-hooks.h>
13 #include <zephyr/app_memory/mem_domain.h>
14 #include <zephyr/sys/util_loops.h>
15 #include <zephyr/sys/time_units.h>
16 #include <zephyr/timing/timing.h>
17 #include <zephyr/rtio/rtio.h>
18 
19 RTIO_POOL_DEFINE(rpool, 2, 8, 8);
20 
21 #ifdef CONFIG_USERSPACE
22 struct k_mem_domain pool_domain;
23 #endif
24 
25 
ZTEST_USER(rtio_pool,test_rtio_pool_acquire_release)26 ZTEST_USER(rtio_pool, test_rtio_pool_acquire_release)
27 {
28 	struct rtio *r = rtio_pool_acquire(&rpool);
29 
30 	zassert_not_null(r, "expected valid rtio context");
31 
32 	struct rtio_sqe nop_sqe;
33 	struct rtio_cqe nop_cqe;
34 
35 	rtio_sqe_prep_nop(&nop_sqe, NULL, NULL);
36 	rtio_sqe_copy_in(r, &nop_sqe, 1);
37 	rtio_submit(r, 1);
38 	rtio_cqe_copy_out(r, &nop_cqe, 1, K_FOREVER);
39 
40 	rtio_pool_release(&rpool, r);
41 }
42 
rtio_pool_setup(void)43 static void *rtio_pool_setup(void)
44 {
45 #ifdef CONFIG_USERSPACE
46 	k_mem_domain_init(&pool_domain, 0, NULL);
47 	k_mem_domain_add_partition(&pool_domain, &rtio_partition);
48 #if Z_LIBC_PARTITION_EXISTS
49 	k_mem_domain_add_partition(&pool_domain, &z_libc_partition);
50 #endif /* Z_LIBC_PARTITION_EXISTS */
51 #endif /* CONFIG_USERSPACE */
52 
53 	return NULL;
54 }
55 
rtio_pool_before(void * a)56 static void rtio_pool_before(void *a)
57 {
58 	ARG_UNUSED(a);
59 
60 #ifdef CONFIG_USERSPACE
61 	k_object_access_grant(&rpool, k_current_get());
62 #endif /* CONFIG_USERSPACE */
63 }
64 
65 ZTEST_SUITE(rtio_pool, NULL, rtio_pool_setup, rtio_pool_before, NULL, NULL);
66