1 | ////////////////////////////////////////////////////////////////////////////////// |
---|
2 | // File : ctx_handler.c |
---|
3 | // Date : 01/04/2012 |
---|
4 | // Authors : alain greiner & joel porquet |
---|
5 | // Copyright (c) UPMC-LIP6 |
---|
6 | ////////////////////////////////////////////////////////////////////////////////// |
---|
7 | |
---|
8 | #include <ctx_handler.h> |
---|
9 | #include <sys_handler.h> |
---|
10 | #include <giet_config.h> |
---|
11 | #include <fat32.h> |
---|
12 | #include <hard_config.h> |
---|
13 | #include <utils.h> |
---|
14 | #include <tty0.h> |
---|
15 | #include <xcu_driver.h> |
---|
16 | #include <bdv_driver.h> |
---|
17 | |
---|
18 | ///////////////////////////////////////////////////////////////////////////////// |
---|
19 | // Extern variables and functions |
---|
20 | ///////////////////////////////////////////////////////////////////////////////// |
---|
21 | |
---|
22 | // defined in giet_kernel/switch.s file |
---|
23 | extern void _thread_switch( thread_context_t* , thread_context_t* ); |
---|
24 | |
---|
25 | // allocated in boot.c or kernel_init.c files |
---|
26 | extern static_scheduler_t* _schedulers[X_SIZE][Y_SIZE][NB_PROCS_MAX]; |
---|
27 | |
---|
28 | // allocated in kernel_init.c file |
---|
29 | extern fat_desc_t _fat; |
---|
30 | |
---|
31 | ///////////////////////////////////////////////////////////////////////////////// |
---|
32 | // This function is called by the _ctx_switch() function. |
---|
33 | // It desactivates a thread that received a KILL signal. |
---|
34 | // We must release all ressources allocated to the thread |
---|
35 | // before the actual desactivation, that set the NORUN_MASK_THREAD |
---|
36 | // bit in the thread context. |
---|
37 | ////////////////////////////////////////////////////////////////////////////////// |
---|
38 | static void _ctx_kill_thread( unsigned int x, |
---|
39 | unsigned int y, |
---|
40 | unsigned int p, |
---|
41 | unsigned int ltid ) |
---|
42 | { |
---|
43 | // get scheduler address |
---|
44 | static_scheduler_t* psched = _schedulers[x][y][p]; |
---|
45 | |
---|
46 | // pretend the thread to kill is the currently scheduled thread |
---|
47 | // (required by the _sys_***_release() calls) |
---|
48 | unsigned int cur_thread = psched->current; |
---|
49 | psched->current = ltid; |
---|
50 | |
---|
51 | // release BDV lock if taken and reset BDV peripheral |
---|
52 | if ( psched->context[ltid].slot[CTX_LOCKS_ID] & LOCKS_MASK_BDV ) |
---|
53 | { |
---|
54 | _bdv_set_register( BLOCK_DEVICE_STATUS , 0 ); |
---|
55 | _spin_lock_release( &_bdv_lock ); |
---|
56 | } |
---|
57 | |
---|
58 | // release FAT lock if taken |
---|
59 | if ( psched->context[ltid].slot[CTX_LOCKS_ID] & LOCKS_MASK_FAT ) |
---|
60 | { |
---|
61 | |
---|
62 | _spin_lock_release( &_fat.fat_lock ); |
---|
63 | } |
---|
64 | |
---|
65 | // release FBF lock if taken |
---|
66 | if ( psched->context[ltid].slot[CTX_LOCKS_ID] & LOCKS_MASK_FBF ) |
---|
67 | { |
---|
68 | _sys_fbf_release(); |
---|
69 | } |
---|
70 | |
---|
71 | // release private TTY terminal if required |
---|
72 | if ( psched->context[ltid].slot[CTX_TTY_ID] < NB_TTY_CHANNELS ) |
---|
73 | _sys_tty_release(); |
---|
74 | |
---|
75 | // release private TIM channel if required |
---|
76 | |
---|
77 | if ( psched->context[ltid].slot[CTX_TIM_ID] < NB_TIM_CHANNELS ) |
---|
78 | { |
---|
79 | _sys_tim_release(); |
---|
80 | } |
---|
81 | |
---|
82 | // release private NIC_RX and CMA_RX channels if required |
---|
83 | if ( psched->context[ltid].slot[CTX_NIC_RX_ID] < NB_NIC_CHANNELS ) |
---|
84 | { |
---|
85 | _sys_nic_release( 1 ); |
---|
86 | } |
---|
87 | |
---|
88 | // release private NIC_TX and CMA_TX channels if required |
---|
89 | if ( psched->context[ltid].slot[CTX_NIC_TX_ID] < NB_NIC_CHANNELS ) |
---|
90 | { |
---|
91 | _sys_nic_release( 0 ); |
---|
92 | } |
---|
93 | |
---|
94 | // release private FBF_CMA channel if required |
---|
95 | if ( psched->context[ltid].slot[CTX_CMA_FB_ID] < NB_CMA_CHANNELS ) |
---|
96 | { |
---|
97 | _sys_fbf_cma_release(); |
---|
98 | } |
---|
99 | |
---|
100 | // restore scheduled thread index |
---|
101 | psched->current = cur_thread; |
---|
102 | |
---|
103 | // set NORUN_MASK_THREAD bit to desactivate the target thread |
---|
104 | psched->context[ltid].slot[CTX_NORUN_ID] = NORUN_MASK_THREAD; |
---|
105 | |
---|
106 | } // end _ctx_kill_thread() |
---|
107 | |
---|
108 | |
---|
109 | ////////////////// |
---|
110 | void _ctx_switch() |
---|
111 | { |
---|
112 | unsigned int gpid = _get_procid(); |
---|
113 | unsigned int cluster_xy = gpid >> P_WIDTH; |
---|
114 | unsigned int x = cluster_xy >> Y_WIDTH; |
---|
115 | unsigned int y = cluster_xy & ((1<<Y_WIDTH)-1); |
---|
116 | unsigned int p = gpid & ((1<<P_WIDTH)-1); |
---|
117 | |
---|
118 | unsigned int ltid; // index for loops on threads in scheduler |
---|
119 | |
---|
120 | // get calling thread scheduler address |
---|
121 | static_scheduler_t* psched = (static_scheduler_t*)_get_sched(); |
---|
122 | |
---|
123 | // get number of threads allocated to scheduler |
---|
124 | unsigned int threads = psched->threads; |
---|
125 | |
---|
126 | // get current thread ltid |
---|
127 | unsigned int curr_thread_id = psched->current; |
---|
128 | |
---|
129 | // first loop on threads: handle all pending KILL signals |
---|
130 | for ( ltid = 0 ; ltid < threads ; ltid++ ) |
---|
131 | { |
---|
132 | if ( psched->context[ltid].slot[CTX_SIGS_ID] & SIGS_MASK_KILL ) |
---|
133 | { |
---|
134 | // acknowledge KILL signal |
---|
135 | _atomic_and( &psched->context[ltid].slot[CTX_SIGS_ID], ~SIGS_MASK_KILL ); |
---|
136 | |
---|
137 | // desactivate the killed thread |
---|
138 | _ctx_kill_thread( x , y , p , ltid ); |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | // second loop: select next thread using a round-robin policy |
---|
143 | unsigned int next_thread_id; |
---|
144 | unsigned int found = 0; |
---|
145 | for ( ltid = curr_thread_id + 1 ; ltid < (curr_thread_id + 1 + threads) ; ltid++ ) |
---|
146 | { |
---|
147 | next_thread_id = ltid % threads; |
---|
148 | |
---|
149 | // test if the thread is runable |
---|
150 | if ( psched->context[next_thread_id].slot[CTX_NORUN_ID] == 0 ) |
---|
151 | { |
---|
152 | found = 1; |
---|
153 | break; |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | // launch idle_thread if no runable thread |
---|
158 | if ( found == 0 ) next_thread_id = IDLE_THREAD_INDEX; |
---|
159 | |
---|
160 | if ( curr_thread_id != next_thread_id ) // actual thread switch required |
---|
161 | { |
---|
162 | |
---|
163 | #if GIET_DEBUG_SWITCH |
---|
164 | unsigned int x = cluster_xy >> Y_WIDTH; |
---|
165 | unsigned int y = cluster_xy & ((1<<Y_WIDTH)-1); |
---|
166 | if ( (_get_proctime() > GIET_DEBUG_SWITCH) && (x == 0) && (y == 0) && (p == 0) ) |
---|
167 | _printf("\n[DEBUG SWITCH] (%d) -> (%d) on processor[%d,%d,%d] at cycle %d\n", |
---|
168 | curr_thread_id, next_thread_id, x, y , p, _get_proctime() ); |
---|
169 | #endif |
---|
170 | |
---|
171 | thread_context_t* curr_ctx_vaddr = &(psched->context[curr_thread_id]); |
---|
172 | thread_context_t* next_ctx_vaddr = &(psched->context[next_thread_id]); |
---|
173 | |
---|
174 | // reset TICK timer counter. |
---|
175 | _xcu_timer_reset_cpt( cluster_xy, p ); |
---|
176 | |
---|
177 | // set current thread index |
---|
178 | psched->current = next_thread_id; |
---|
179 | |
---|
180 | // makes context switch |
---|
181 | _thread_switch( curr_ctx_vaddr , next_ctx_vaddr ); |
---|
182 | |
---|
183 | // only modify the PTPR when necessary, in order to avoid unnecessary |
---|
184 | // TLBs flush. |
---|
185 | if (curr_ctx_vaddr->slot[CTX_PTPR_ID] != |
---|
186 | next_ctx_vaddr->slot[CTX_PTPR_ID]) |
---|
187 | { |
---|
188 | _set_mmu_ptpr(next_ctx_vaddr->slot[CTX_PTPR_ID]); |
---|
189 | } |
---|
190 | } |
---|
191 | } //end _ctx_switch() |
---|
192 | |
---|
193 | |
---|
194 | /////////////////// |
---|
195 | void _idle_thread() |
---|
196 | { |
---|
197 | unsigned int gpid = _get_procid(); |
---|
198 | unsigned int cluster_xy = gpid >> P_WIDTH; |
---|
199 | unsigned int x = cluster_xy >> Y_WIDTH; |
---|
200 | unsigned int y = cluster_xy & ((1<<Y_WIDTH)-1); |
---|
201 | unsigned int p = gpid & ((1<<P_WIDTH)-1); |
---|
202 | |
---|
203 | while(1) |
---|
204 | { |
---|
205 | _sleep( GIET_IDLE_THREAD_PERIOD ); |
---|
206 | |
---|
207 | // warning message |
---|
208 | _printf("\n[GIET WARNING] Processor[%d,%d,%d] still idle at cycle %u", |
---|
209 | x , y , p , _get_proctime() ); |
---|
210 | } |
---|
211 | } // end ctx_idle() |
---|
212 | |
---|
213 | |
---|
214 | //////////////// |
---|
215 | void _ctx_eret() |
---|
216 | { |
---|
217 | asm volatile("eret"); |
---|
218 | } |
---|
219 | |
---|
220 | |
---|
221 | // Local Variables: |
---|
222 | // tab-width: 4 |
---|
223 | // c-basic-offset: 4 |
---|
224 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
225 | // indent-tabs-mode: nil |
---|
226 | // End: |
---|
227 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
228 | |
---|