1 | /////////////////////////////////////////////////////////////////////////////////// |
---|
2 | // File : bdv_driver.c |
---|
3 | // Date : 23/05/2013 |
---|
4 | // Author : alain greiner cesar fuguet |
---|
5 | // Copyright (c) UPMC-LIP6 |
---|
6 | /////////////////////////////////////////////////////////////////////////////////// |
---|
7 | // Implementation notes: |
---|
8 | // All accesses to BDV registers are done by the two |
---|
9 | // _bdv_set_register() and _bdv_get_register() low-level functions, |
---|
10 | // that are handling virtual / physical extended addressing. |
---|
11 | /////////////////////////////////////////////////////////////////////////////////// |
---|
12 | |
---|
13 | #include <giet_config.h> |
---|
14 | #include <hard_config.h> |
---|
15 | #include <bdv_driver.h> |
---|
16 | #include <xcu_driver.h> |
---|
17 | #include <mmc_driver.h> |
---|
18 | #include <kernel_locks.h> |
---|
19 | #include <utils.h> |
---|
20 | #include <tty0.h> |
---|
21 | #include <ctx_handler.h> |
---|
22 | #include <irq_handler.h> |
---|
23 | |
---|
24 | /////////////////////////////////////////////////////////////////////////////// |
---|
25 | // Global variables |
---|
26 | /////////////////////////////////////////////////////////////////////////////// |
---|
27 | |
---|
28 | // lock protecting single channel BDV peripheral |
---|
29 | __attribute__((section(".kdata"))) |
---|
30 | spin_lock_t _bdv_lock __attribute__((aligned(64))); |
---|
31 | |
---|
32 | // global index of the waiting task (only used in descheduling mode) |
---|
33 | __attribute__((section(".kdata"))) |
---|
34 | unsigned int _bdv_gtid; |
---|
35 | |
---|
36 | // BDV peripheral status (only used in descheduling mode) |
---|
37 | __attribute__((section(".kdata"))) |
---|
38 | unsigned int _bdv_status; |
---|
39 | |
---|
40 | /////////////////////////////////////////////////////////////////////////////// |
---|
41 | // This low_level function returns the value contained in register (index). |
---|
42 | /////////////////////////////////////////////////////////////////////////////// |
---|
43 | unsigned int _bdv_get_register( unsigned int index ) |
---|
44 | { |
---|
45 | unsigned int* vaddr = (unsigned int*)SEG_IOC_BASE + index; |
---|
46 | return _io_extended_read( vaddr ); |
---|
47 | } |
---|
48 | |
---|
49 | /////////////////////////////////////////////////////////////////////////////// |
---|
50 | // This low-level function set a new value in register (index). |
---|
51 | /////////////////////////////////////////////////////////////////////////////// |
---|
52 | void _bdv_set_register( unsigned int index, |
---|
53 | unsigned int value ) |
---|
54 | { |
---|
55 | unsigned int* vaddr = (unsigned int*)SEG_IOC_BASE + index; |
---|
56 | _io_extended_write( vaddr, value ); |
---|
57 | } |
---|
58 | |
---|
59 | /////////////////////////////////////////////////////////////////////////////// |
---|
60 | // Extern functions |
---|
61 | /////////////////////////////////////////////////////////////////////////////// |
---|
62 | |
---|
63 | ///////////////////////////////////////////////////// |
---|
64 | unsigned int _bdv_access( unsigned int use_irq, |
---|
65 | unsigned int to_mem, |
---|
66 | unsigned int lba, |
---|
67 | unsigned long long buf_paddr, |
---|
68 | unsigned int count) |
---|
69 | { |
---|
70 | unsigned int procid = _get_procid(); |
---|
71 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
72 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH) - 1); |
---|
73 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
74 | |
---|
75 | #if GIET_DEBUG_IOC_DRIVER |
---|
76 | if ( _get_proctime() > GIET_DEBUG_IOC_DRIVER ) |
---|
77 | _printf("\n[BDV DEBUG] P[%d,%d,%d] enters _bdv_access at cycle %d\n" |
---|
78 | " use_irq = %d / to_mem = %d / lba = %x / paddr = %l / count = %d\n", |
---|
79 | x , y , p , _get_proctime() , use_irq , to_mem , lba , buf_paddr, count ); |
---|
80 | #endif |
---|
81 | |
---|
82 | // check buffer alignment |
---|
83 | if( buf_paddr & 0x1FF ) |
---|
84 | { |
---|
85 | _printf("\n[BDV ERROR] in _bdv_access() : buffer not block aligned\n"); |
---|
86 | return -1; |
---|
87 | } |
---|
88 | |
---|
89 | unsigned int error; |
---|
90 | unsigned int status; |
---|
91 | |
---|
92 | // get the lock protecting BDV |
---|
93 | _spin_lock_acquire( &_bdv_lock ); |
---|
94 | |
---|
95 | // set device registers |
---|
96 | _bdv_set_register( BLOCK_DEVICE_BUFFER , (unsigned int)buf_paddr ); |
---|
97 | _bdv_set_register( BLOCK_DEVICE_BUFFER_EXT, (unsigned int)(buf_paddr>>32) ); |
---|
98 | _bdv_set_register( BLOCK_DEVICE_COUNT , count ); |
---|
99 | _bdv_set_register( BLOCK_DEVICE_LBA , lba ); |
---|
100 | |
---|
101 | #if USE_IOB // software L2/L3 cache coherence |
---|
102 | if ( to_mem ) _mmc_inval( buf_paddr, count<<9 ); |
---|
103 | else _mmc_sync( buf_paddr, count<<9 ); |
---|
104 | #endif // end software L2/L3 cache coherence |
---|
105 | |
---|
106 | ///////////////////////////////////////////////////////////////////// |
---|
107 | // In synchronous mode, we launch transfer, |
---|
108 | // and poll the BDV_STATUS register until completion. |
---|
109 | ///////////////////////////////////////////////////////////////////// |
---|
110 | if ( use_irq == 0 ) |
---|
111 | { |
---|
112 | // Launch transfert |
---|
113 | if (to_mem == 0) _bdv_set_register( BLOCK_DEVICE_OP, BLOCK_DEVICE_WRITE ); |
---|
114 | else _bdv_set_register( BLOCK_DEVICE_OP, BLOCK_DEVICE_READ ); |
---|
115 | |
---|
116 | #if GIET_DEBUG_IOC_DRIVER |
---|
117 | if ( _get_proctime() > GIET_DEBUG_IOC_DRIVER ) |
---|
118 | _printf("\n[BDV DEBUG] _bdv_access() : P[%d,%d,%d] launch transfer" |
---|
119 | " in polling mode at cycle %d\n", |
---|
120 | x , y , p , _get_proctime() ); |
---|
121 | #endif |
---|
122 | |
---|
123 | do |
---|
124 | { |
---|
125 | status = _bdv_get_register( BLOCK_DEVICE_STATUS ); |
---|
126 | |
---|
127 | #if GIET_DEBUG_IOC_DRIVER |
---|
128 | if ( _get_proctime() > GIET_DEBUG_IOC_DRIVER ) |
---|
129 | _printf("\n[BDV DEBUG] _bdv_access() : P[%d,%d,%d] wait on BDV_STATUS ...\n", |
---|
130 | x , y , p ); |
---|
131 | #endif |
---|
132 | } |
---|
133 | while( (status != BLOCK_DEVICE_READ_SUCCESS) && |
---|
134 | (status != BLOCK_DEVICE_READ_ERROR) && |
---|
135 | (status != BLOCK_DEVICE_WRITE_SUCCESS) && |
---|
136 | (status != BLOCK_DEVICE_WRITE_ERROR) ); // busy waiting |
---|
137 | |
---|
138 | // analyse status |
---|
139 | error = ( (status == BLOCK_DEVICE_READ_ERROR) || |
---|
140 | (status == BLOCK_DEVICE_WRITE_ERROR) ); |
---|
141 | } |
---|
142 | |
---|
143 | ///////////////////////////////////////////////////////////////// |
---|
144 | // in descheduling mode, we deschedule the task |
---|
145 | // and use an interrupt to reschedule the task. |
---|
146 | // We need a critical section, because we must reset the RUN bit |
---|
147 | // before to launch the transfer, and we don't want to be |
---|
148 | // descheduled between these two operations. |
---|
149 | ///////////////////////////////////////////////////////////////// |
---|
150 | else |
---|
151 | { |
---|
152 | unsigned int save_sr; |
---|
153 | unsigned int ltid = _get_current_task_id(); |
---|
154 | |
---|
155 | // activates BDV interrupt |
---|
156 | _bdv_set_register( BLOCK_DEVICE_IRQ_ENABLE, 1 ); |
---|
157 | |
---|
158 | // set _bdv_gtid |
---|
159 | _bdv_gtid = (procid<<16) + ltid; |
---|
160 | |
---|
161 | // enters critical section |
---|
162 | _it_disable( &save_sr ); |
---|
163 | |
---|
164 | // reset runnable |
---|
165 | _set_task_slot( x, y, p, ltid, CTX_RUN_ID, 0 ); |
---|
166 | |
---|
167 | // launch transfer |
---|
168 | if (to_mem == 0) _bdv_set_register( BLOCK_DEVICE_OP, BLOCK_DEVICE_WRITE ); |
---|
169 | else _bdv_set_register( BLOCK_DEVICE_OP, BLOCK_DEVICE_READ ); |
---|
170 | |
---|
171 | #if GIET_DEBUG_IOC_DRIVER |
---|
172 | if ( _get_proctime() > GIET_DEBUG_IOC_DRIVER ) |
---|
173 | _printf("\n[BDV DEBUG] _bdv_access() : P[%d,%d,%d] launch transfer" |
---|
174 | " in descheduling mode at cycle %d\n", |
---|
175 | x , y , p , _get_proctime() ); |
---|
176 | #endif |
---|
177 | |
---|
178 | // deschedule task |
---|
179 | _ctx_switch(); |
---|
180 | |
---|
181 | #if GIET_DEBUG_IOC_DRIVER |
---|
182 | if ( _get_proctime() > GIET_DEBUG_IOC_DRIVER ) |
---|
183 | _printf("\n[BDV DEBUG] _bdv_access() : P[%d,%d,%d] resume execution at cycle %d\n", |
---|
184 | x , y , p , _get_proctime() ); |
---|
185 | #endif |
---|
186 | |
---|
187 | // restore SR |
---|
188 | _it_restore( &save_sr ); |
---|
189 | |
---|
190 | // analyse status |
---|
191 | error = ( (_bdv_status == BLOCK_DEVICE_READ_ERROR) || |
---|
192 | (_bdv_status == BLOCK_DEVICE_WRITE_ERROR) ); |
---|
193 | } |
---|
194 | |
---|
195 | // release lock |
---|
196 | _spin_lock_release( &_bdv_lock ); |
---|
197 | |
---|
198 | #if GIET_DEBUG_IOC_DRIVER |
---|
199 | if ( _get_proctime() > GIET_DEBUG_IOC_DRIVER ) |
---|
200 | _printf("\n[BDV DEBUG] _bdv_access() : P[%d,%d,%d] exit at cycle %d\n", |
---|
201 | x , y , p , _get_proctime() ); |
---|
202 | #endif |
---|
203 | |
---|
204 | return error; |
---|
205 | } // end _bdv_access() |
---|
206 | |
---|
207 | //////////////////////// |
---|
208 | unsigned int _bdv_init() |
---|
209 | { |
---|
210 | if ( _bdv_get_register( BLOCK_DEVICE_BLOCK_SIZE ) != 512 ) |
---|
211 | { |
---|
212 | _puts("\n[GIET ERROR] in _bdv_init() : block size must be 512 bytes\n"); |
---|
213 | return 1; |
---|
214 | } |
---|
215 | |
---|
216 | _bdv_set_register( BLOCK_DEVICE_IRQ_ENABLE, 0 ); |
---|
217 | return 0; |
---|
218 | } |
---|
219 | |
---|
220 | ///////////////////////////////////// |
---|
221 | void _bdv_isr( unsigned int irq_type, // HWI / WTI |
---|
222 | unsigned int irq_id, // index returned by ICU |
---|
223 | unsigned int channel ) // unused |
---|
224 | { |
---|
225 | // get BDV status and reset BDV_IRQ |
---|
226 | unsigned int status = _bdv_get_register( BLOCK_DEVICE_STATUS ); |
---|
227 | |
---|
228 | // check status: does nothing if IDLE or BUSY |
---|
229 | if ( (status == BLOCK_DEVICE_IDLE) || |
---|
230 | (status == BLOCK_DEVICE_BUSY) ) return; |
---|
231 | |
---|
232 | // register status in global variable |
---|
233 | _bdv_status = status; |
---|
234 | |
---|
235 | // identify task waiting on BDV |
---|
236 | unsigned int remote_procid = _bdv_gtid>>16; |
---|
237 | unsigned int ltid = _bdv_gtid & 0xFFFF; |
---|
238 | unsigned int remote_cluster = remote_procid >> P_WIDTH; |
---|
239 | unsigned int remote_x = remote_cluster >> Y_WIDTH; |
---|
240 | unsigned int remote_y = remote_cluster & ((1<<Y_WIDTH)-1); |
---|
241 | unsigned int remote_p = remote_procid & ((1<<P_WIDTH)-1); |
---|
242 | |
---|
243 | // re-activates sleeping task |
---|
244 | _set_task_slot( remote_x, |
---|
245 | remote_y, |
---|
246 | remote_p, |
---|
247 | ltid, |
---|
248 | CTX_RUN_ID, // CTX_RUN slot |
---|
249 | 1 ); // running value |
---|
250 | |
---|
251 | // send a WAKUP WTI to processor running the sleeping task |
---|
252 | _xcu_send_wti( remote_cluster, |
---|
253 | remote_p, |
---|
254 | 0 ); // don't force context switch |
---|
255 | |
---|
256 | #if GIET_DEBUG_IOC_DRIVER |
---|
257 | unsigned int procid = _get_procid(); |
---|
258 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
259 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1); |
---|
260 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
261 | if ( _get_proctime() > GIET_DEBUG_IOC_DRIVER ) |
---|
262 | _printf("\n[BDV DEBUG] Processor[%d,%d,%d] enters _bdv_isr() at cycle %d\n" |
---|
263 | " for task %d running on P[%d,%d,%d] / bdv_status = %x\n", |
---|
264 | x , y , p , _get_proctime() , |
---|
265 | ltid , remote_x , remote_y , remote_p , status ); |
---|
266 | #endif |
---|
267 | |
---|
268 | } // end bdv_isr() |
---|
269 | |
---|
270 | |
---|
271 | // Local Variables: |
---|
272 | // tab-width: 4 |
---|
273 | // c-basic-offset: 4 |
---|
274 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
275 | // indent-tabs-mode: nil |
---|
276 | // End: |
---|
277 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
278 | |
---|