1 /*
2  * Copyright (c) 2021 Carlo Caione <ccaione@baylibre.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_INCLUDE_IPC_SERVICE_IPC_STATIC_VRINGS_H_
8 #define ZEPHYR_INCLUDE_IPC_SERVICE_IPC_STATIC_VRINGS_H_
9 
10 #include <zephyr/ipc/ipc_service.h>
11 #include <openamp/open_amp.h>
12 #include <metal/device.h>
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 /**
19  * @brief IPC service static VRINGs API
20  * @defgroup ipc_service_static_vrings_api IPC service static VRINGs API
21  * @ingroup ipc
22  * @{
23  */
24 
25 /** Number of used VRING buffers. */
26 #define VRING_COUNT	(2)
27 
28 /**
29  * Memory alignment.
30  *
31  * This should take into account the cache line if the cache is enabled, otherwise
32  * it should be naturally aligned to the machine word size.
33  */
34 #define MEM_ALIGNMENT	CONFIG_IPC_SERVICE_STATIC_VRINGS_MEM_ALIGNMENT
35 
36 /**
37  * @typedef ipc_notify_cb
38  * @brief Define the notify callback.
39  *
40  * This callback is defined at instance level and it is called on virtqueue notify.
41  *
42  * @param vq Virtqueue.
43  * @param priv Priv data.
44  */
45 typedef void (*ipc_notify_cb)(struct virtqueue *vq, void *priv);
46 
47 /** @brief Static VRINGs structure.
48  *
49  *  Struct used to represent and carry information about static allocation of VRINGs.
50  */
51 struct ipc_static_vrings {
52 	/** virtIO device. */
53 	struct virtio_device vdev;
54 
55 	/** SHM physmap. */
56 	metal_phys_addr_t shm_physmap[1];
57 
58 	/** SHM device. */
59 	struct metal_device shm_device;
60 
61 	/** SHM and addresses. */
62 	uintptr_t status_reg_addr;
63 
64 	/** TX VRING address. */
65 	uintptr_t tx_addr;
66 
67 	/** RX VRING address. */
68 	uintptr_t rx_addr;
69 
70 	/** VRING size. */
71 	size_t vring_size;
72 
73 	/** Shared memory region address. */
74 	uintptr_t shm_addr;
75 
76 	/** Share memory region size. */
77 	size_t shm_size;
78 
79 	/** SHM IO region. */
80 	struct metal_io_region *shm_io;
81 
82 	/** VRINGs */
83 	struct virtio_vring_info rvrings[VRING_COUNT];
84 
85 	/** Virtqueues. */
86 	struct virtqueue *vq[VRING_COUNT];
87 
88 	/** Private data to be passed to the notify callback. */
89 	void *priv;
90 
91 	/** Notify callback. */
92 	ipc_notify_cb notify_cb;
93 };
94 
95 /** @brief Init the static VRINGs.
96  *
97  *  Init VRINGs and Virtqueues of an OpenAMP / RPMsg instance.
98  *
99  *  @param vr Pointer to the VRINGs instance struct.
100  *  @param role Host / Remote role.
101  *
102  *  @retval -EINVAL When some parameter is missing.
103  *  @retval -ENOMEM When memory is not enough for VQs allocation.
104  *  @retval 0 If successful.
105  *  @retval Other errno codes depending on the OpenAMP implementation.
106  */
107 int ipc_static_vrings_init(struct ipc_static_vrings *vr, unsigned int role);
108 
109 /** @brief Deinitialise the static VRINGs.
110  *
111  *  Deinitialise VRINGs and Virtqueues of an OpenAMP / RPMsg instance.
112  *
113  *  @param vr Pointer to the VRINGs instance struct.
114  *  @param role Host / Remote role.
115  *
116  *  @retval 0 If successful.
117  *  @retval Other errno codes depending on the OpenAMP implementation.
118  */
119 int ipc_static_vrings_deinit(struct ipc_static_vrings *vr, unsigned int role);
120 
121 /**
122  * @}
123  */
124 
125 #ifdef __cplusplus
126 }
127 #endif
128 
129 #endif /* ZEPHYR_INCLUDE_IPC_SERVICE_IPC_STATIC_VRINGS_H_ */
130