1 | /* |
---|
2 | * remote_buf.h - Remotely accessible, circular buffer definition. |
---|
3 | * |
---|
4 | * Authors : |
---|
5 | * Alain Greiner (2016,2017,2018,2019,2020) |
---|
6 | * |
---|
7 | * Copyright (c) UPMC Sorbonne Universites |
---|
8 | * |
---|
9 | * This file is part of ALMOS-MKH. |
---|
10 | * |
---|
11 | * ALMOS-MHH is free software; you can redistribute it and/or modify it |
---|
12 | * under the terms of the GNU General Public License as published by |
---|
13 | * the Free Software Foundation; version 2.0 of the License. |
---|
14 | * |
---|
15 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
18 | * General Public License for more details. |
---|
19 | * |
---|
20 | * You should have received a copy of the GNU General Public License |
---|
21 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
22 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
23 | */ |
---|
24 | |
---|
25 | #ifndef _REMOTE_BUFFER_H_ |
---|
26 | #define _REMOTE_BUFFER_H_ |
---|
27 | |
---|
28 | #include <kernel_config.h> |
---|
29 | #include <hal_kernel_types.h> |
---|
30 | |
---|
31 | /************************************************************************************ |
---|
32 | * This structure and the associated access functions define a remotely accessible, |
---|
33 | * kernel buffer, handled as a single-reader & single-writer FIFO. |
---|
34 | * This kernel buffer is implementes as an array of bytes. The size must be a power |
---|
35 | * of 2, dynamically defined by an argument of the "remote_buf_init()" function. |
---|
36 | * The "put()" and "get()" functions defined below move the content of another |
---|
37 | * buffer (can be in kernel or in user space) to/from this circular kernel buffer. |
---|
38 | * This structure is NOT protected by a lock, as the only shared variable is the |
---|
39 | * "sts" variable, that is updated by the hal_remote_atomic_add() primitive. |
---|
40 | * |
---|
41 | * - It is used to implement the three socket buffers (rx_buf / r2tq / crqq). |
---|
42 | * - It is also used to implement the inter-process communications channels |
---|
43 | * based on named "fifos", or unamed "pipes". |
---|
44 | ***********************************************************************************/ |
---|
45 | |
---|
46 | typedef struct remote_buf_s |
---|
47 | { |
---|
48 | uint32_t order; /*! ln2( size of data buffer in bytes) */ |
---|
49 | uint32_t rid; /*! first empty slot index */ |
---|
50 | uint32_t wid; /*! first non-empty slot index */ |
---|
51 | uint32_t sts; /*! current number of non empty slots */ |
---|
52 | uint8_t * data; /*! local pointer on local data buffer */ |
---|
53 | } |
---|
54 | remote_buf_t; |
---|
55 | |
---|
56 | /************************************************************************************ |
---|
57 | * This function allocates memory for a remote_buf descriptor in cluster |
---|
58 | * defined by the <cxy> argument, and return a local pointer on this descriptor. |
---|
59 | ************************************************************************************ |
---|
60 | * @ cxy : target cluster identifier. |
---|
61 | * @ return local pointer on remote_buf descriptor. |
---|
62 | ***********************************************************************************/ |
---|
63 | remote_buf_t * remote_buf_alloc( cxy_t cxy ); |
---|
64 | |
---|
65 | /************************************************************************************ |
---|
66 | * This function initializes a remote_buf descriptor identified by the <buf_xp> |
---|
67 | * argument. It allocates memory for the data, as defined by the <order> argument, |
---|
68 | * and initializes the buffer as empty. It must be called by a local thread. |
---|
69 | * The <order> value cannot be larger than 31. |
---|
70 | ************************************************************************************ |
---|
71 | * @ buf_xp : [in] extended pointer on buffer descriptor. |
---|
72 | * @ order : [in] ln2( number of bytes in buffer) / must be in [0,31]. |
---|
73 | * @ return 0 if success / return -1 if memory failure or illegal order. |
---|
74 | ***********************************************************************************/ |
---|
75 | error_t remote_buf_init( xptr_t buf_xp, |
---|
76 | uint32_t order ); |
---|
77 | |
---|
78 | /************************************************************************************ |
---|
79 | * This function releases memory allocated for the data buffer of an embedded |
---|
80 | * remote-buf descriptor identified by the <buf_xp> argument, when the "data" |
---|
81 | * pointer is not NULL. It does nothing if the data buffer was not allocated. |
---|
82 | * WARNING : do NOT use this function for a dynamically allocated remote_buf. |
---|
83 | ************************************************************************************ |
---|
84 | * @ buf_xp : [in] extended pointer on buffer descriptor. |
---|
85 | ***********************************************************************************/ |
---|
86 | void remote_buf_release_data( xptr_t buf_xp ); |
---|
87 | |
---|
88 | /************************************************************************************ |
---|
89 | * This function releases both the memory allocated for the data buffer, and the |
---|
90 | * memory allocated for the remote_buf descriptor, for a remote-buf identified |
---|
91 | * by the <buf_xp> argument. |
---|
92 | * WARNING : do NOT use this function an embedded remote_buf. |
---|
93 | ************************************************************************************ |
---|
94 | * @ buf_xp : [in] extended pointer on buffer descriptor. |
---|
95 | ***********************************************************************************/ |
---|
96 | void remote_buf_destroy( xptr_t buf_xp ); |
---|
97 | |
---|
98 | /************************************************************************************ |
---|
99 | * This function initialises as empty a buffer identified by the <buf_xp> argument. |
---|
100 | * It can be called by a thread runing in any cluster. |
---|
101 | ************************************************************************************ |
---|
102 | * @ buf_xp : [in] extended pointer on remote buffer descriptor. |
---|
103 | ***********************************************************************************/ |
---|
104 | void remote_buf_reset( xptr_t buf_xp ); |
---|
105 | |
---|
106 | /************************************************************************************ |
---|
107 | * This function moves <nbytes> from a remote_buf_t identified by the <buf_xp> |
---|
108 | * argument to an user buffer identified by the <u_buf> argument. |
---|
109 | * It uses the hal_copy_to_uspace() function, and updates "sts" and "ptr". |
---|
110 | * The calling function is supposed to previously check the buffer status. |
---|
111 | * It can be called by a thread runing in any cluster. |
---|
112 | ************************************************************************************ |
---|
113 | * @ buf_xp : [in] extended pointer pointer on remote buffer descriptor. |
---|
114 | * @ u_buf : [in] pointer on destination user buffer in user space. |
---|
115 | * @ nbytes : [in] number of bytes to move. |
---|
116 | * @ return 0 if success / return -1 if not enough bytes in buffer. |
---|
117 | ***********************************************************************************/ |
---|
118 | error_t remote_buf_get_to_user( xptr_t buf_xp, |
---|
119 | uint8_t * u_buf, |
---|
120 | uint32_t nbytes ); |
---|
121 | |
---|
122 | /************************************************************************************ |
---|
123 | * This function moves <nbytes> from a remote_buf_t identified by the <buf_xp> |
---|
124 | * argument to a local kernel buffer identified by the <k_buf> argument. |
---|
125 | * It uses an hal_remote_memcpy() function, and updates the "sts" and "ptr" fields. |
---|
126 | * The calling function is supposed to previously check the buffer status. |
---|
127 | * It can be called by a thread runing in any cluster. |
---|
128 | ************************************************************************************ |
---|
129 | * @ buf_xp : [in] extended pointer pointer on remote buffer descriptor. |
---|
130 | * @ k_buf : [in] local pointer on local destination kernel buffer. |
---|
131 | * @ nbytes : [in] number of bytes to move. |
---|
132 | * @ return 0 if success / return -1 if not enough bytes in buffer. |
---|
133 | ***********************************************************************************/ |
---|
134 | error_t remote_buf_get_to_kernel( xptr_t buf_xp, |
---|
135 | uint8_t * k_buf, |
---|
136 | uint32_t nbytes ); |
---|
137 | |
---|
138 | /************************************************************************************ |
---|
139 | * This function moves <nbytes> from an user buffer identified by the <u_buf> |
---|
140 | * argument to a remote_buf_t identified by the <buf_xp> argument. |
---|
141 | * It uses the hal_copy_from_uspace() function, and updates "sts" and "ptw".. |
---|
142 | * The calling function is supposed to previously check the buffer status. |
---|
143 | * It can be called by a thread runing in any cluster. |
---|
144 | ************************************************************************************ |
---|
145 | * @ buf_xp : [in] extended pointer pointer on remote buffer descriptor. |
---|
146 | * @ u_buf : [in] pointer on source user buffer in user space. |
---|
147 | * @ nbytes : [in] number of bytes to move. |
---|
148 | * @ return 0 if success / return -1 if not enough bytes in buffer. |
---|
149 | ***********************************************************************************/ |
---|
150 | error_t remote_buf_put_from_user( xptr_t buf_xp, |
---|
151 | uint8_t * u_buf, |
---|
152 | uint32_t nbytes ); |
---|
153 | |
---|
154 | /************************************************************************************ |
---|
155 | * This function moves <nbytes> from a local kernel buffer identified by the <k_buf> |
---|
156 | * argument to a remote_buf_t identified by the <buf_xp> argument. |
---|
157 | * It uses an hal_remote_memcpy() function, and updates the "sts" and "ptw" fields. |
---|
158 | * The calling function is supposed to previously check the buffer status. |
---|
159 | * It can be called by a thread runing in any cluster. |
---|
160 | ************************************************************************************ |
---|
161 | * @ buf_xp : [in] extended pointer pointer on remote buffer descriptor. |
---|
162 | * @ k_buf : [in] local pointer on local source kernel buffer. |
---|
163 | * @ nbytes : [in] number of bytes to move. |
---|
164 | * @ return 0 if success / return -1 if not enough bytes in buffer. |
---|
165 | ***********************************************************************************/ |
---|
166 | error_t remote_buf_put_from_kernel( xptr_t buf_xp, |
---|
167 | uint8_t * k_buf, |
---|
168 | uint32_t nbytes ); |
---|
169 | |
---|
170 | /************************************************************************************ |
---|
171 | * This function returns the current number of bytes stored in buffer. |
---|
172 | ************************************************************************************ |
---|
173 | * @ buf_xp : [in] extended pointer pointer on remote buffer descriptor. |
---|
174 | * @ return current number of bytes in buffer. |
---|
175 | ***********************************************************************************/ |
---|
176 | uint32_t remote_buf_status( xptr_t buf_xp ); |
---|
177 | |
---|
178 | #endif /* _REMOTE_BUFFER_H_ */ |
---|