1 /*
2  * Copyright (c) 2024 Croxel Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_INCLUDE_RTIO_WORKQ_H_
8 #define ZEPHYR_INCLUDE_RTIO_WORKQ_H_
9 
10 #include <stdint.h>
11 #include <zephyr/device.h>
12 #include <zephyr/rtio/rtio.h>
13 #include <zephyr/sys/p4wq.h>
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 /**
20  * @brief Callback API to execute work operation.
21  *
22  * @param iodev_sqe Associated SQE operation.
23  */
24 typedef void (*rtio_work_submit_t)(struct rtio_iodev_sqe *iodev_sqe);
25 
26 /**
27  * @brief RTIO Work request.
28  *
29  * This RTIO Work request to perform a work operation decoupled
30  * from its submission in the RTIO work-queues.
31  */
32 struct rtio_work_req {
33 	/** Work item used to submit unit of work. */
34 	struct k_p4wq_work work;
35 
36 	/** Handle to IODEV SQE containing the operation.
37 	 * This is filled inside @ref rtio_work_req_submit.
38 	 */
39 	struct rtio_iodev_sqe *iodev_sqe;
40 
41 	/** Callback handler where synchronous operation may be executed.
42 	 * This is filled inside @ref rtio_work_req_submit.
43 	 */
44 	rtio_work_submit_t handler;
45 };
46 
47 /**
48  * @brief Allocate item to perform an RTIO work request.
49  *
50  * @details This allocation utilizes its internal memory slab with
51  * pre-allocated elements.
52  *
53  * @return Pointer to allocated item if successful.
54  * @return NULL if allocation failed.
55  */
56 struct rtio_work_req *rtio_work_req_alloc(void);
57 
58 /**
59  * @brief Submit RTIO work request.
60  *
61  * @param req Item to fill with request information.
62  * @param iodev_sqe RTIO Operation information.
63  * @param handler Callback to handler where work operation is performed.
64  */
65 void rtio_work_req_submit(struct rtio_work_req *req,
66 			  struct rtio_iodev_sqe *iodev_sqe,
67 			  rtio_work_submit_t handler);
68 
69 /**
70  * @brief Obtain number of currently used items from the pre-allocated pool.
71  *
72  * @return Number of used items.
73  */
74 uint32_t rtio_work_req_used_count_get(void);
75 
76 #ifdef __cplusplus
77 }
78 #endif
79 
80 #endif /* ZEPHYR_INCLUDE_RTIO_WORKQ_H_ */
81