1 | /* |
---|
2 | * dev_ioc.c - IOC (Block Device Controler) generic device API implementation. |
---|
3 | * |
---|
4 | * Author Alain Greiner (2016,2017,2018,2019,2020) |
---|
5 | * |
---|
6 | * Copyright (c) UPMC Sorbonne Universites |
---|
7 | * |
---|
8 | * This file is part of ALMOS-MK |
---|
9 | * |
---|
10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
11 | * under the terms of the GNU General Public License as published by |
---|
12 | * the Free Software Foundation; version 2.0 of the License. |
---|
13 | * |
---|
14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | * General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
22 | */ |
---|
23 | |
---|
24 | #include <kernel_config.h> |
---|
25 | #include <hal_kernel_types.h> |
---|
26 | #include <hal_gpt.h> |
---|
27 | #include <hal_drivers.h> |
---|
28 | #include <thread.h> |
---|
29 | #include <printk.h> |
---|
30 | #include <chdev.h> |
---|
31 | #include <dev_ioc.h> |
---|
32 | |
---|
33 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
34 | // Extern global variables |
---|
35 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
36 | |
---|
37 | extern chdev_directory_t chdev_dir; // allocated in kernel_init.c |
---|
38 | |
---|
39 | //////////////////////////////////////////// |
---|
40 | char * dev_ioc_cmd_str( ioc_cmd_type_t cmd ) |
---|
41 | { |
---|
42 | if ( cmd == IOC_READ ) return "READ"; |
---|
43 | else if( cmd == IOC_WRITE ) return "WRITE"; |
---|
44 | else if( cmd == IOC_SYNC_READ ) return "SYNC_READ"; |
---|
45 | else if( cmd == IOC_SYNC_WRITE ) return "SYNC_WRITE"; |
---|
46 | else return "undefined"; |
---|
47 | } |
---|
48 | |
---|
49 | ////////////////////////////////// |
---|
50 | void dev_ioc_init( chdev_t * ioc ) |
---|
51 | { |
---|
52 | // get channel from chdev descriptor |
---|
53 | uint32_t channel = ioc->channel; |
---|
54 | |
---|
55 | // set chdev name |
---|
56 | snprintf( ioc->name , 16 , "ioc%d" , channel ); |
---|
57 | |
---|
58 | // call driver init function |
---|
59 | hal_drivers_ioc_init( ioc ); |
---|
60 | |
---|
61 | // select a core to execute the IOC server thread |
---|
62 | lid_t lid = cluster_select_local_core( local_cxy ); |
---|
63 | |
---|
64 | // bind the IOC IRQ to the selected core |
---|
65 | dev_pic_bind_irq( lid , ioc ); |
---|
66 | |
---|
67 | // enable IRQ |
---|
68 | dev_pic_enable_irq( lid , XPTR( local_cxy , ioc ) ); |
---|
69 | |
---|
70 | // create server thread |
---|
71 | thread_t * new_thread; |
---|
72 | error_t error; |
---|
73 | |
---|
74 | error = thread_kernel_create( &new_thread, |
---|
75 | THREAD_DEV, |
---|
76 | &chdev_server_func, |
---|
77 | ioc, |
---|
78 | lid ); |
---|
79 | |
---|
80 | assert( (error == 0) , "cannot create server thread" ); |
---|
81 | |
---|
82 | // set "server" field in chdev descriptor |
---|
83 | ioc->server = new_thread; |
---|
84 | |
---|
85 | // set "chdev" field in thread descriptor |
---|
86 | new_thread->chdev = ioc; |
---|
87 | |
---|
88 | // unblock server thread |
---|
89 | thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL ); |
---|
90 | |
---|
91 | } // end dev_ioc_init() |
---|
92 | |
---|
93 | //////////////////////////////////////////////////////////////////////////////////// |
---|
94 | // This static function executes an asynchronous SYNC_READ or SYNC_WRITE request. |
---|
95 | // thread in the IOC device waiting queue, activates the server thread, blocks on |
---|
96 | // the THREAD_BLOCKED_IO condition and deschedules. |
---|
97 | // The clent is re-activated by the server thread after IO operation completion. |
---|
98 | //////////////////////////////////////////////////////////////////////////////////// |
---|
99 | static error_t dev_ioc_move( uint32_t cmd_type, |
---|
100 | xptr_t buffer_xp, |
---|
101 | uint32_t lba, |
---|
102 | uint32_t count ) |
---|
103 | { |
---|
104 | thread_t * this = CURRENT_THREAD; // pointer on client thread |
---|
105 | |
---|
106 | // get extended pointer on IOC chdev descriptor |
---|
107 | xptr_t ioc_xp = chdev_dir.ioc[0]; |
---|
108 | |
---|
109 | // check dev_xp |
---|
110 | assert( (ioc_xp != XPTR_NULL) , "undefined IOC chdev descriptor" ); |
---|
111 | |
---|
112 | // register command in client thread |
---|
113 | this->ioc_cmd.dev_xp = ioc_xp; |
---|
114 | this->ioc_cmd.type = cmd_type; |
---|
115 | this->ioc_cmd.buf_xp = buffer_xp; |
---|
116 | this->ioc_cmd.lba = lba; |
---|
117 | this->ioc_cmd.count = count; |
---|
118 | |
---|
119 | // register client thread in IOC queue, blocks and deschedules |
---|
120 | chdev_register_command( ioc_xp ); |
---|
121 | |
---|
122 | // return I/O operation status |
---|
123 | return this->ioc_cmd.error; |
---|
124 | |
---|
125 | } // end dev_ioc_move() |
---|
126 | |
---|
127 | //////////////////////////////////////////////////////////////////////////////////// |
---|
128 | // This static function executes a synchronous READ or WRITE request. |
---|
129 | // It register the command in the client thread descriptor, and calls directly |
---|
130 | // the driver cmd function. |
---|
131 | //////////////////////////////////////////////////////////////////////////////////// |
---|
132 | error_t dev_ioc_sync_move( uint32_t cmd_type, |
---|
133 | xptr_t buffer_xp, |
---|
134 | uint32_t lba, |
---|
135 | uint32_t count ) |
---|
136 | { |
---|
137 | thread_t * this = CURRENT_THREAD; // pointer on client thread |
---|
138 | |
---|
139 | // get extended pointer on IOC chdev descriptor |
---|
140 | xptr_t ioc_xp = chdev_dir.ioc[0]; |
---|
141 | |
---|
142 | // check dev_xp |
---|
143 | assert( (ioc_xp != XPTR_NULL) , "undefined IOC chdev descriptor" ); |
---|
144 | |
---|
145 | // register command in calling thread descriptor |
---|
146 | this->ioc_cmd.dev_xp = ioc_xp; |
---|
147 | this->ioc_cmd.type = cmd_type; |
---|
148 | this->ioc_cmd.buf_xp = buffer_xp; |
---|
149 | this->ioc_cmd.lba = lba; |
---|
150 | this->ioc_cmd.count = count; |
---|
151 | |
---|
152 | // get driver command function |
---|
153 | cxy_t ioc_cxy = GET_CXY( ioc_xp ); |
---|
154 | chdev_t * ioc_ptr = GET_PTR( ioc_xp ); |
---|
155 | dev_cmd_t * cmd = (dev_cmd_t *)hal_remote_lpt( XPTR( ioc_cxy , &ioc_ptr->cmd ) ); |
---|
156 | |
---|
157 | // call driver function whithout blocking & descheduling |
---|
158 | cmd( XPTR( local_cxy , this ) ); |
---|
159 | |
---|
160 | // return I/O operation status |
---|
161 | return this->ioc_cmd.error; |
---|
162 | |
---|
163 | } // end dev_ioc_sync_move() |
---|
164 | |
---|
165 | /////////////////////////////////////////// |
---|
166 | error_t dev_ioc_read( xptr_t buffer_xp, |
---|
167 | uint32_t lba, |
---|
168 | uint32_t count ) |
---|
169 | { |
---|
170 | |
---|
171 | #if DEBUG_DEV_IOC_RX |
---|
172 | thread_t * this = CURRENT_THREAD; |
---|
173 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
174 | if( DEBUG_DEV_IOC_RX < cycle ) |
---|
175 | printk("\n[%s] thread[%x,%x] enters IOC_READ / lba %x / buffer[%x,%x] / cycle %d\n", |
---|
176 | __FUNCTION__, this->process->pid, this->trdid, lba, |
---|
177 | GET_CXY(buffer_xp), GET_PTR(buffer_xp), cycle ); |
---|
178 | #endif |
---|
179 | |
---|
180 | // software L2/L3 cache coherence for memory buffer |
---|
181 | if( chdev_dir.iob ) dev_mmc_inval( buffer_xp , count<<9 ); |
---|
182 | |
---|
183 | // request an asynchronous transfer |
---|
184 | error_t error = dev_ioc_move( IOC_READ, |
---|
185 | buffer_xp, |
---|
186 | lba, |
---|
187 | count ); |
---|
188 | #if(DEBUG_DEV_IOC_RX & 1) |
---|
189 | cycle = (uint32_t)hal_get_cycles(); |
---|
190 | if( DEBUG_DEV_IOC_RX < cycle ) |
---|
191 | printk("\n[%s] thread[%x,%x] exit IOC_READ / cycle %d\n", |
---|
192 | __FUNCTION__, this->process->pid , this->trdid , cycle ); |
---|
193 | #endif |
---|
194 | |
---|
195 | return error; |
---|
196 | |
---|
197 | } // end dev_ioc_read() |
---|
198 | |
---|
199 | /////////////////////////////////////////// |
---|
200 | error_t dev_ioc_write( xptr_t buffer_xp, |
---|
201 | uint32_t lba, |
---|
202 | uint32_t count ) |
---|
203 | { |
---|
204 | |
---|
205 | #if DEBUG_DEV_IOC_TX |
---|
206 | thread_t * this = CURRENT_THREAD; |
---|
207 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
208 | if( DEBUG_DEV_IOC_TX < cycle ) |
---|
209 | printk("\n[%s] thread[%x,%x] enters IOC_WRITE / lba %x / buffer[%x,%x] / cycle %d\n", |
---|
210 | __FUNCTION__, this->process->pid, this->trdid, lba, |
---|
211 | GET_CXY(buffer_xp), GET_PTR(buffer_xp), cycle ); |
---|
212 | #endif |
---|
213 | |
---|
214 | // software L2/L3 cache coherence for memory buffer |
---|
215 | if( chdev_dir.iob ) dev_mmc_sync ( buffer_xp , count<<9 ); |
---|
216 | |
---|
217 | // request a blocking, but asynchronous, transfer |
---|
218 | error_t error = dev_ioc_move( IOC_WRITE, |
---|
219 | buffer_xp, |
---|
220 | lba, |
---|
221 | count ); |
---|
222 | #if(DEBUG_DEV_IOC_TX & 1) |
---|
223 | cycle = (uint32_t)hal_get_cycles(); |
---|
224 | if( DEBUG_DEV_IOC_TX < cycle ) |
---|
225 | printk("\n[%s] thread[%x,%x] exit IOC_WRITE / cycle %d\n", |
---|
226 | __FUNCTION__, this->process->pid , this->trdid , cycle ); |
---|
227 | #endif |
---|
228 | |
---|
229 | return error; |
---|
230 | |
---|
231 | } // end dev_ioc_write() |
---|
232 | |
---|
233 | |
---|
234 | /////////////////////////////////////////// |
---|
235 | error_t dev_ioc_sync_read( xptr_t buffer_xp, |
---|
236 | uint32_t lba, |
---|
237 | uint32_t count ) |
---|
238 | { |
---|
239 | |
---|
240 | #if DEBUG_DEV_IOC_RX |
---|
241 | thread_t * this = CURRENT_THREAD; |
---|
242 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
243 | if( DEBUG_DEV_IOC_RX < cycle ) |
---|
244 | printk("\n[%s] thread[%x,%x] enters IOC_SYNC_READ / lba %x / buffer[%x,%x] / cycle %d\n", |
---|
245 | __FUNCTION__, this->process->pid, this->trdid, lba, |
---|
246 | GET_CXY(buffer_xp), GET_PTR(buffer_xp), cycle ); |
---|
247 | #endif |
---|
248 | |
---|
249 | // software L2/L3 cache coherence for memory buffer |
---|
250 | if( chdev_dir.iob ) dev_mmc_inval( buffer_xp , count<<9 ); |
---|
251 | |
---|
252 | // request an asynchronous transfer |
---|
253 | error_t error = dev_ioc_sync_move( IOC_SYNC_READ, |
---|
254 | buffer_xp, |
---|
255 | lba, |
---|
256 | count ); |
---|
257 | #if(DEBUG_DEV_IOC_RX & 1) |
---|
258 | cycle = (uint32_t)hal_get_cycles(); |
---|
259 | if( DEBUG_DEV_IOC_RX < cycle ) |
---|
260 | printk("\n[%s] thread[%x,%x] exit IOC_SYNC_READ / cycle %d\n", |
---|
261 | __FUNCTION__, this->process->pid , this->trdid , cycle ); |
---|
262 | #endif |
---|
263 | |
---|
264 | return error; |
---|
265 | |
---|
266 | } // end dev_ioc_sync_read() |
---|
267 | |
---|
268 | ///////////////////////////////////////////////// |
---|
269 | error_t dev_ioc_sync_write( xptr_t buffer_xp, |
---|
270 | uint32_t lba, |
---|
271 | uint32_t count ) |
---|
272 | { |
---|
273 | |
---|
274 | #if DEBUG_DEV_IOC_TX |
---|
275 | thread_t * this = CURRENT_THREAD; |
---|
276 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
277 | if( DEBUG_DEV_IOC_TX < cycle ) |
---|
278 | printk("\n[%s] thread[%x,%x] enters IOC_SYNC_WRITE / lba %x / buffer[%x,%x] / cycle %d\n", |
---|
279 | __FUNCTION__, this->process->pid, this->trdid, lba, |
---|
280 | GET_CXY(buffer_xp), GET_PTR(buffer_xp), cycle ); |
---|
281 | #endif |
---|
282 | |
---|
283 | // software L2/L3 cache coherence for memory buffer |
---|
284 | if( chdev_dir.iob ) dev_mmc_sync ( buffer_xp , count<<9 ); |
---|
285 | |
---|
286 | // request a blocking, but asynchronous, transfer |
---|
287 | error_t error = dev_ioc_sync_move( IOC_SYNC_WRITE, |
---|
288 | buffer_xp, |
---|
289 | lba, |
---|
290 | count ); |
---|
291 | #if(DEBUG_DEV_IOC_TX & 1) |
---|
292 | cycle = (uint32_t)hal_get_cycles(); |
---|
293 | if( DEBUG_DEV_IOC_TX < cycle ) |
---|
294 | printk("\n[%s] thread[%x,%x] exit IOC_SYNC_WRITE / cycle %d\n", |
---|
295 | __FUNCTION__, this->process->pid , this->trdid , cycle ); |
---|
296 | #endif |
---|
297 | |
---|
298 | return error; |
---|
299 | |
---|
300 | } // end dev_ioc_sync_write() |
---|
301 | |
---|
302 | |
---|