1 | /////////////////////////////////////////////////////////////////////////////////// |
---|
2 | // File : bdv_driver.c |
---|
3 | // Date : 23/05/2013 |
---|
4 | // Author : alain greiner |
---|
5 | // Maintainer: cesar fuguet |
---|
6 | // Copyright (c) UPMC-LIP6 |
---|
7 | /////////////////////////////////////////////////////////////////////////////////// |
---|
8 | // Implementation notes: |
---|
9 | // 1. In order to share code, the two _bdv_read() and _bdv_write() functions |
---|
10 | // call the same _bdv_access() function. |
---|
11 | // 2. All accesses to BDV registers are done by the two |
---|
12 | // _bdv_set_register() and _bdv_get_register() low-level functions, |
---|
13 | // that are handling virtual / physical extended addressing. |
---|
14 | /////////////////////////////////////////////////////////////////////////////////// |
---|
15 | |
---|
16 | #include <giet_config.h> |
---|
17 | #include <hard_config.h> |
---|
18 | #include <bdv_driver.h> |
---|
19 | #include <xcu_driver.h> |
---|
20 | #include <ioc_driver.h> |
---|
21 | #include <utils.h> |
---|
22 | #include <tty_driver.h> |
---|
23 | #include <ctx_handler.h> |
---|
24 | |
---|
25 | #if !defined(GIET_NO_HARD_CC) |
---|
26 | # error: You must define GIET_NO_HARD_CC in the giet_config.h file |
---|
27 | #endif |
---|
28 | |
---|
29 | /////////////////////////////////////////////////////////////////////////////// |
---|
30 | // BDV global variables |
---|
31 | /////////////////////////////////////////////////////////////////////////////// |
---|
32 | |
---|
33 | #define in_unckdata __attribute__((section (".unckdata"))) |
---|
34 | #define in_kdata __attribute__((section (".kdata"))) |
---|
35 | |
---|
36 | #if GIET_NO_HARD_CC |
---|
37 | in_unckdata giet_lock_t _bdv_lock __attribute__((aligned(64))); |
---|
38 | in_unckdata volatile unsigned int _bdv_status; |
---|
39 | in_unckdata volatile unsigned int _bdv_gtid; |
---|
40 | #else |
---|
41 | in_kdata giet_lock_t _bdv_lock __attribute__((aligned(64))); |
---|
42 | in_kdata volatile unsigned int _bdv_status; |
---|
43 | in_kdata volatile unsigned int _bdv_gtid; |
---|
44 | #endif |
---|
45 | |
---|
46 | /////////////////////////////////////////////////////////////////////////////// |
---|
47 | // This low_level function returns the value contained in register (index). |
---|
48 | /////////////////////////////////////////////////////////////////////////////// |
---|
49 | unsigned int _bdv_get_register( unsigned int index ) |
---|
50 | { |
---|
51 | unsigned int* vaddr = (unsigned int*)SEG_IOC_BASE + index; |
---|
52 | return _io_extended_read( vaddr ); |
---|
53 | } |
---|
54 | |
---|
55 | /////////////////////////////////////////////////////////////////////////////// |
---|
56 | // This low-level function set a new value in register (index). |
---|
57 | /////////////////////////////////////////////////////////////////////////////// |
---|
58 | void _bdv_set_register( unsigned int index, |
---|
59 | unsigned int value ) |
---|
60 | { |
---|
61 | unsigned int* vaddr = (unsigned int*)SEG_IOC_BASE + index; |
---|
62 | _io_extended_write( vaddr, value ); |
---|
63 | } |
---|
64 | |
---|
65 | /////////////////////////////////////////////////////////////////////////////// |
---|
66 | // This function transfer data between a memory buffer and the block device. |
---|
67 | // The buffer lentgth is (count*block_size) bytes. |
---|
68 | // Arguments are: |
---|
69 | // - to_mem : from external storage to memory when non 0. |
---|
70 | // - mode : BOOT / KERNEL / USER |
---|
71 | // - lba : first block index on the external storage. |
---|
72 | // - buf_paddr : physical base address of the memory buffer. |
---|
73 | // - count : number of blocks to be transfered. |
---|
74 | // Returns 0 if success, > 0 if error. |
---|
75 | /////////////////////////////////////////////////////////////////////////////// |
---|
76 | static unsigned int _bdv_access( unsigned int to_mem, |
---|
77 | unsigned int mode, |
---|
78 | unsigned int lba, |
---|
79 | unsigned long long buf_paddr, |
---|
80 | unsigned int count) |
---|
81 | { |
---|
82 | unsigned int procid = _get_procid(); |
---|
83 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
84 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH) - 1); |
---|
85 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
86 | |
---|
87 | #if GIET_DEBUG_IOC_DRIVER |
---|
88 | _puts("\n[BDV DEBUG] _bdv_access() : P["); |
---|
89 | _putd( x ); |
---|
90 | _puts(","); |
---|
91 | _putd( y ); |
---|
92 | _puts(","); |
---|
93 | _putd( p ); |
---|
94 | _puts("] enters at cycle "); |
---|
95 | _putd( _get_proctime() ); |
---|
96 | _puts("\n - to_mem = "); |
---|
97 | _putd( to_mem ); |
---|
98 | _puts("\n - mode = "); |
---|
99 | _putd( mode ); |
---|
100 | _puts("\n - paddr = "); |
---|
101 | _putl( buf_paddr ); |
---|
102 | _puts("\n - sectors = "); |
---|
103 | _putd( count ); |
---|
104 | _puts("\n - lba = "); |
---|
105 | _putx( lba ); |
---|
106 | _puts("\n"); |
---|
107 | #endif |
---|
108 | |
---|
109 | unsigned int error = 0; |
---|
110 | |
---|
111 | // get the lock protecting BDV |
---|
112 | _get_lock(&_bdv_lock); |
---|
113 | |
---|
114 | #if GIET_DEBUG_IOC_DRIVER |
---|
115 | _puts("\n[BDV DEBUG] _bdv_access() : P["); |
---|
116 | _putd( x ); |
---|
117 | _puts(","); |
---|
118 | _putd( y ); |
---|
119 | _puts(","); |
---|
120 | _putd( p ); |
---|
121 | _puts("] get bdv_lock at cycle "); |
---|
122 | _pud( _get_proctime() ); |
---|
123 | _puts("\n"); |
---|
124 | #endif |
---|
125 | |
---|
126 | // set device registers |
---|
127 | _bdv_set_register( BLOCK_DEVICE_BUFFER , (unsigned int)buf_paddr ); |
---|
128 | _bdv_set_register( BLOCK_DEVICE_BUFFER_EXT, (unsigned int)(buf_paddr>>32) ); |
---|
129 | _bdv_set_register( BLOCK_DEVICE_COUNT , count ); |
---|
130 | _bdv_set_register( BLOCK_DEVICE_LBA , lba ); |
---|
131 | |
---|
132 | // In BOOT mode, we launch transfer, and poll the BDV_STATUS |
---|
133 | // register because IRQs are masked. |
---|
134 | if ( mode == IOC_BOOT_MODE ) |
---|
135 | { |
---|
136 | // Launch transfert |
---|
137 | if (to_mem == 0) _bdv_set_register( BLOCK_DEVICE_OP, BLOCK_DEVICE_WRITE ); |
---|
138 | else _bdv_set_register( BLOCK_DEVICE_OP, BLOCK_DEVICE_READ ); |
---|
139 | |
---|
140 | #if GIET_DEBUG_IOC_DRIVER |
---|
141 | _puts("\n[BDV DEBUG] _bdv_access() : P["); |
---|
142 | _putd( x ); |
---|
143 | _puts(","); |
---|
144 | _putd( y ); |
---|
145 | _puts(","); |
---|
146 | _putd( p ); |
---|
147 | _puts("] launch transfer in polling mode\n"); |
---|
148 | #endif |
---|
149 | unsigned int status; |
---|
150 | do |
---|
151 | { |
---|
152 | status = _bdv_get_register( BLOCK_DEVICE_STATUS ); |
---|
153 | |
---|
154 | #if GIET_DEBUG_IOC_DRIVER |
---|
155 | _puts("\n[BDV DEBUG] _bdv_access() : P["); |
---|
156 | _putd( x ); |
---|
157 | _puts(","); |
---|
158 | _putd( y ); |
---|
159 | _puts(","); |
---|
160 | _putd( p ); |
---|
161 | _puts("] wait on BDV_STATUS register ...\n"); |
---|
162 | #endif |
---|
163 | } |
---|
164 | while( (status != BLOCK_DEVICE_READ_SUCCESS) && |
---|
165 | (status != BLOCK_DEVICE_READ_ERROR) && |
---|
166 | (status != BLOCK_DEVICE_WRITE_SUCCESS) && |
---|
167 | (status != BLOCK_DEVICE_WRITE_ERROR) ); // busy waiting |
---|
168 | |
---|
169 | // analyse status |
---|
170 | error = ( (status == BLOCK_DEVICE_READ_ERROR) || |
---|
171 | (status == BLOCK_DEVICE_WRITE_ERROR) ); |
---|
172 | |
---|
173 | // release lock |
---|
174 | _release_lock(&_bdv_lock); |
---|
175 | } |
---|
176 | // in USER or KERNEL mode, we deschedule the task. |
---|
177 | // When the task is rescheduled, we check the _bdv_status variable, |
---|
178 | // and release the lock. |
---|
179 | // We need a critical section, because we must reset the RUN bit |
---|
180 | // before to launch the transfer, and we don't want to be descheduled |
---|
181 | // between these two operations. |
---|
182 | else |
---|
183 | { |
---|
184 | unsigned int save_sr; |
---|
185 | unsigned int ltid = _get_current_task_id(); |
---|
186 | |
---|
187 | // activates BDV interrupts |
---|
188 | _bdv_set_register( BLOCK_DEVICE_IRQ_ENABLE, 1 ); |
---|
189 | |
---|
190 | // set the _bdv_status variable |
---|
191 | _bdv_status = BLOCK_DEVICE_BUSY; |
---|
192 | |
---|
193 | // enters critical section |
---|
194 | _it_disable( &save_sr ); |
---|
195 | |
---|
196 | // set _bdv_gtid and reset runnable |
---|
197 | _bdv_gtid = (procid<<16) + ltid; |
---|
198 | _set_task_slot( x, y, p, ltid, CTX_RUN_ID, 0 ); |
---|
199 | |
---|
200 | // launch transfer |
---|
201 | if (to_mem == 0) _bdv_set_register( BLOCK_DEVICE_OP, BLOCK_DEVICE_WRITE ); |
---|
202 | else _bdv_set_register( BLOCK_DEVICE_OP, BLOCK_DEVICE_READ ); |
---|
203 | |
---|
204 | #if GIET_DEBUG_IOC_DRIVER |
---|
205 | _puts("\n[BDV DEBUG] _bdv_access() : P["); |
---|
206 | _putd( x ); |
---|
207 | _puts(","); |
---|
208 | _putd( y ); |
---|
209 | _puts(","); |
---|
210 | _putd( p ); |
---|
211 | _puts("] launch transfer in nterrupt mode\n"); |
---|
212 | #endif |
---|
213 | |
---|
214 | // deschedule task |
---|
215 | _ctx_switch(); |
---|
216 | |
---|
217 | #if GIET_DEBUG_IOC_DRIVER |
---|
218 | _puts("\n[BDV DEBUG] _bdv_access() : P["); |
---|
219 | _putd( x ); |
---|
220 | _puts(","); |
---|
221 | _putd( y ); |
---|
222 | _puts(","); |
---|
223 | _putd( p ); |
---|
224 | _puts("] resume execution after descheduling\n"); |
---|
225 | #endif |
---|
226 | // restore SR |
---|
227 | _it_restore( &save_sr ); |
---|
228 | |
---|
229 | // analyse status |
---|
230 | error = ( (_bdv_status == BLOCK_DEVICE_READ_ERROR) || |
---|
231 | (_bdv_status == BLOCK_DEVICE_WRITE_ERROR) ); |
---|
232 | |
---|
233 | // reset _bdv_status and release lock |
---|
234 | _bdv_status = BLOCK_DEVICE_IDLE; |
---|
235 | _release_lock(&_bdv_lock); |
---|
236 | } |
---|
237 | |
---|
238 | #if GIET_DEBUG_IOC_DRIVER |
---|
239 | _puts("\n[BDV DEBUG] _bdv_access() : P["); |
---|
240 | _putd( x ); |
---|
241 | _puts(","); |
---|
242 | _putd( y ); |
---|
243 | _puts(","); |
---|
244 | _putd( p ); |
---|
245 | _puts("] exit at cycle "); |
---|
246 | _putd( _get_proctime() ); |
---|
247 | _puts(" / error = "); |
---|
248 | _putd( error ) |
---|
249 | _puts("\n"); |
---|
250 | #endif |
---|
251 | |
---|
252 | return error; |
---|
253 | } // end _bdv_access() |
---|
254 | |
---|
255 | /////////////////////////////////////////////////////////////////////////////// |
---|
256 | // External functions |
---|
257 | /////////////////////////////////////////////////////////////////////////////// |
---|
258 | |
---|
259 | //////////////////////// |
---|
260 | unsigned int _bdv_init() |
---|
261 | { |
---|
262 | if ( _bdv_get_register( BLOCK_DEVICE_BLOCK_SIZE ) != 512 ) |
---|
263 | { |
---|
264 | _puts("\n[GIET ERROR] in _bdv_init() : block size must be 512 bytes\n"); |
---|
265 | return 1; |
---|
266 | } |
---|
267 | |
---|
268 | _bdv_set_register( BLOCK_DEVICE_IRQ_ENABLE, 0 ); |
---|
269 | return 0; |
---|
270 | } |
---|
271 | |
---|
272 | //////////////////////////////////////////////// |
---|
273 | unsigned int _bdv_read( unsigned int mode, |
---|
274 | unsigned int lba, |
---|
275 | unsigned long long buffer, |
---|
276 | unsigned int count) |
---|
277 | { |
---|
278 | return _bdv_access( 1, // read access |
---|
279 | mode, |
---|
280 | lba, |
---|
281 | buffer, |
---|
282 | count ); |
---|
283 | } |
---|
284 | |
---|
285 | ///////////////////////////////////////////////// |
---|
286 | unsigned int _bdv_write( unsigned int mode, |
---|
287 | unsigned int lba, |
---|
288 | unsigned long long buffer, |
---|
289 | unsigned int count ) |
---|
290 | { |
---|
291 | return _bdv_access( 0, // write access |
---|
292 | mode, |
---|
293 | lba, |
---|
294 | buffer, |
---|
295 | count ); |
---|
296 | } |
---|
297 | |
---|
298 | ////////////////////////////// |
---|
299 | unsigned int _bdv_get_status() |
---|
300 | { |
---|
301 | return _bdv_get_register( BLOCK_DEVICE_STATUS ); |
---|
302 | } |
---|
303 | |
---|
304 | ////////////////////////////////// |
---|
305 | unsigned int _bdv_get_block_size() |
---|
306 | { |
---|
307 | return _bdv_get_register( BLOCK_DEVICE_BLOCK_SIZE ); |
---|
308 | } |
---|
309 | |
---|
310 | ///////////////////////////////////// |
---|
311 | void _bdv_isr( unsigned int irq_type, // HWI / WTI |
---|
312 | unsigned int irq_id, // index returned by ICU |
---|
313 | unsigned int channel ) // unused |
---|
314 | { |
---|
315 | // get BDV status (and reset IRQ) |
---|
316 | unsigned int status = _bdv_get_register( BLOCK_DEVICE_STATUS ); |
---|
317 | |
---|
318 | // check status: does nothing if IDLE or BUSY |
---|
319 | if ( (status == BLOCK_DEVICE_IDLE) || |
---|
320 | (status == BLOCK_DEVICE_BUSY) ) return; |
---|
321 | |
---|
322 | // save status in kernel buffer _bdv_status |
---|
323 | _bdv_status = status; |
---|
324 | |
---|
325 | // identify task waiting on BDV |
---|
326 | unsigned int remote_procid = _bdv_gtid>>16; |
---|
327 | unsigned int ltid = _bdv_gtid & 0xFFFF; |
---|
328 | unsigned int remote_cluster = remote_procid >> P_WIDTH; |
---|
329 | unsigned int remote_x = remote_cluster >> Y_WIDTH; |
---|
330 | unsigned int remote_y = remote_cluster & ((1<<Y_WIDTH)-1); |
---|
331 | unsigned int remote_p = remote_procid & ((1<<P_WIDTH)-1); |
---|
332 | |
---|
333 | // re-activates sleeping task |
---|
334 | _set_task_slot( remote_x, |
---|
335 | remote_y, |
---|
336 | remote_p, |
---|
337 | ltid, |
---|
338 | CTX_RUN_ID, // CTX_RUN slot |
---|
339 | 1 ); // running |
---|
340 | |
---|
341 | // requires a context switch for remote processor running the waiting task |
---|
342 | _xcu_send_wti( remote_cluster, |
---|
343 | remote_p, |
---|
344 | 0 ); // don't force context switch if not idle |
---|
345 | |
---|
346 | #if GIET_DEBUG_IRQS // we don't take the TTY lock to avoid deadlock |
---|
347 | unsigned int procid = _get_procid(); |
---|
348 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
349 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1); |
---|
350 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
351 | |
---|
352 | _puts("\n[IRQS DEBUG] Processor["); |
---|
353 | _putd(x ); |
---|
354 | _puts(","); |
---|
355 | _putd(y ); |
---|
356 | _puts(","); |
---|
357 | _putd(p ); |
---|
358 | _puts("] enters _bdv_isr() at cycle "); |
---|
359 | _putd(_get_proctime() ); |
---|
360 | _puts("\n for task "); |
---|
361 | _putd(ltid ); |
---|
362 | _puts(" running on processor["); |
---|
363 | _putd(remote_x ); |
---|
364 | _puts(","); |
---|
365 | _putd(remore_y ); |
---|
366 | _puts(","); |
---|
367 | _putd(remote_p ); |
---|
368 | _puts(" / bdv status = "); |
---|
369 | _putx(_bdv_status ); |
---|
370 | _puts("\n"); |
---|
371 | #endif |
---|
372 | |
---|
373 | } |
---|
374 | |
---|
375 | |
---|
376 | // Local Variables: |
---|
377 | // tab-width: 4 |
---|
378 | // c-basic-offset: 4 |
---|
379 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
380 | // indent-tabs-mode: nil |
---|
381 | // End: |
---|
382 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
383 | |
---|