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 <hard_config.h> |
---|
12 | #include <utils.h> |
---|
13 | #include <tty0.h> |
---|
14 | #include <xcu_driver.h> |
---|
15 | |
---|
16 | ///////////////////////////////////////////////////////////////////////////////// |
---|
17 | // Extern variables and functions |
---|
18 | ///////////////////////////////////////////////////////////////////////////////// |
---|
19 | |
---|
20 | // defined in giet_kernel/switch.s file |
---|
21 | extern void _task_switch(unsigned int *, unsigned int *); |
---|
22 | |
---|
23 | // allocated in boot.c or kernel_init.c files |
---|
24 | extern static_scheduler_t* _schedulers[X_SIZE][Y_SIZE][NB_PROCS_MAX]; |
---|
25 | |
---|
26 | ////////////////// |
---|
27 | static void _ctx_kill_task( unsigned int ltid ) |
---|
28 | { |
---|
29 | // get scheduler address |
---|
30 | static_scheduler_t* psched = (static_scheduler_t*)_get_sched(); |
---|
31 | |
---|
32 | // release private TTY terminal if required |
---|
33 | if ( psched->context[ltid][CTX_TTY_ID] < NB_TTY_CHANNELS ) |
---|
34 | { |
---|
35 | psched->context[ltid][CTX_TTY_ID] = 0xFFFFFFFF; |
---|
36 | _sys_tty_release(); |
---|
37 | } |
---|
38 | |
---|
39 | // release private TIM channel if required |
---|
40 | if ( psched->context[ltid][CTX_TIM_ID] < NB_TIM_CHANNELS ) |
---|
41 | { |
---|
42 | psched->context[ltid][CTX_TIM_ID] = 0xFFFFFFFF; |
---|
43 | _sys_tim_release(); |
---|
44 | } |
---|
45 | |
---|
46 | // release private NIC_RX channel if required |
---|
47 | if ( psched->context[ltid][CTX_NIC_RX_ID] < NB_NIC_CHANNELS ) |
---|
48 | { |
---|
49 | psched->context[ltid][CTX_NIC_RX_ID] = 0xFFFFFFFF; |
---|
50 | _sys_nic_release( 1 ); |
---|
51 | } |
---|
52 | |
---|
53 | // release private NIC_TX channel if required |
---|
54 | if ( psched->context[ltid][CTX_NIC_TX_ID] < NB_NIC_CHANNELS ) |
---|
55 | { |
---|
56 | psched->context[ltid][CTX_NIC_TX_ID] = 0xFFFFFFFF; |
---|
57 | _sys_nic_release( 0 ); |
---|
58 | } |
---|
59 | |
---|
60 | // set NORUN_MASK_TASK bit |
---|
61 | _atomic_or( &psched->context[ltid][CTX_NORUN_ID], NORUN_MASK_TASK ); |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | ////////////////////////////////// |
---|
66 | void _ctx_display( unsigned int x, |
---|
67 | unsigned int y, |
---|
68 | unsigned int p, |
---|
69 | unsigned int ltid, |
---|
70 | char* string ) |
---|
71 | { |
---|
72 | static_scheduler_t* psched = _schedulers[x][y][p]; |
---|
73 | _printf("\n########## task[%d,%d,%d,%d] context\n" |
---|
74 | " - CTX_EPC = %x\n" |
---|
75 | " - CTX_PTAB = %x\n" |
---|
76 | " - CTX_PTPR = %x\n" |
---|
77 | " - CTX_VSID = %x\n" |
---|
78 | " - CTX_SR = %x\n" |
---|
79 | " - CTX_RA = %x\n" |
---|
80 | " - CTX_SP = %x\n" |
---|
81 | " - CTX_NORUN = %x\n" |
---|
82 | " - CTX_SIG = %x\n" |
---|
83 | "########## %s\n", |
---|
84 | x , y , p , ltid , |
---|
85 | psched->context[ltid][CTX_EPC_ID], |
---|
86 | psched->context[ltid][CTX_PTAB_ID], |
---|
87 | psched->context[ltid][CTX_PTPR_ID], |
---|
88 | psched->context[ltid][CTX_VSID_ID], |
---|
89 | psched->context[ltid][CTX_SR_ID], |
---|
90 | psched->context[ltid][CTX_RA_ID], |
---|
91 | psched->context[ltid][CTX_SP_ID], |
---|
92 | psched->context[ltid][CTX_NORUN_ID], |
---|
93 | psched->context[ltid][CTX_SIG_ID], |
---|
94 | string ); |
---|
95 | } // _ctx_display() |
---|
96 | |
---|
97 | |
---|
98 | ////////////////// |
---|
99 | void _ctx_switch() |
---|
100 | { |
---|
101 | unsigned int gpid = _get_procid(); |
---|
102 | unsigned int cluster_xy = gpid >> P_WIDTH; |
---|
103 | unsigned int lpid = gpid & ((1<<P_WIDTH)-1); |
---|
104 | |
---|
105 | // get scheduler address |
---|
106 | static_scheduler_t* psched = (static_scheduler_t*)_get_sched(); |
---|
107 | |
---|
108 | // get number of tasks allocated to scheduler |
---|
109 | unsigned int tasks = psched->tasks; |
---|
110 | |
---|
111 | // get current task index |
---|
112 | unsigned int curr_task_id = psched->current; |
---|
113 | |
---|
114 | // select the next task using a round-robin policy |
---|
115 | unsigned int next_task_id; |
---|
116 | unsigned int tid; |
---|
117 | unsigned int found = 0; |
---|
118 | |
---|
119 | for (tid = curr_task_id + 1; tid < curr_task_id + 1 + tasks; tid++) |
---|
120 | { |
---|
121 | next_task_id = tid % tasks; |
---|
122 | |
---|
123 | // this task needs to be killed |
---|
124 | if ( psched->context[next_task_id][CTX_SIG_ID] & SIG_MASK_KILL ) |
---|
125 | { |
---|
126 | _ctx_kill_task( next_task_id ); |
---|
127 | |
---|
128 | // acknowledge signal |
---|
129 | _atomic_and( &psched->context[next_task_id][CTX_SIG_ID], ~SIG_MASK_KILL ); |
---|
130 | |
---|
131 | // skip |
---|
132 | continue; |
---|
133 | } |
---|
134 | |
---|
135 | // test if the task is runable |
---|
136 | if ( psched->context[next_task_id][CTX_NORUN_ID] == 0 ) |
---|
137 | { |
---|
138 | found = 1; |
---|
139 | // TODO: don't break to process all pending signals. |
---|
140 | break; |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | // launch "idle" task if no runable task |
---|
145 | if (found == 0) next_task_id = IDLE_TASK_INDEX; |
---|
146 | |
---|
147 | #if GIET_DEBUG_SWITCH |
---|
148 | unsigned int x = cluster_xy >> Y_WIDTH; |
---|
149 | unsigned int y = cluster_xy & ((1<<Y_WIDTH)-1); |
---|
150 | _printf("\n[DEBUG SWITCH] (%d) -> (%d) on processor[%d,%d,%d] at cycle %d\n", |
---|
151 | curr_task_id, next_task_id, x, y , lpid, _get_proctime() ); |
---|
152 | #endif |
---|
153 | |
---|
154 | if (curr_task_id != next_task_id) // actual task switch required |
---|
155 | { |
---|
156 | unsigned int* curr_ctx_vaddr = &(psched->context[curr_task_id][0]); |
---|
157 | unsigned int* next_ctx_vaddr = &(psched->context[next_task_id][0]); |
---|
158 | |
---|
159 | // reset TICK timer counter. |
---|
160 | _xcu_timer_reset_cpt( cluster_xy, lpid ); |
---|
161 | |
---|
162 | // set current task index |
---|
163 | psched->current = next_task_id; |
---|
164 | |
---|
165 | // makes context switch |
---|
166 | _task_switch( curr_ctx_vaddr , next_ctx_vaddr ); |
---|
167 | } |
---|
168 | } //end _ctx_switch() |
---|
169 | |
---|
170 | |
---|
171 | ///////////////// |
---|
172 | void _idle_task() |
---|
173 | { |
---|
174 | unsigned int gpid = _get_procid(); |
---|
175 | unsigned int cluster_xy = gpid >> P_WIDTH; |
---|
176 | unsigned int x = cluster_xy >> Y_WIDTH; |
---|
177 | unsigned int y = cluster_xy & ((1<<Y_WIDTH)-1); |
---|
178 | unsigned int p = gpid & ((1<<P_WIDTH)-1); |
---|
179 | |
---|
180 | while(1) |
---|
181 | { |
---|
182 | // initialize counter |
---|
183 | unsigned int count = GIET_IDLE_TASK_PERIOD; |
---|
184 | |
---|
185 | // decounting loop |
---|
186 | asm volatile( |
---|
187 | "move $3, %0 \n" |
---|
188 | "_idle_task_loop: \n" |
---|
189 | "addi $3, $3, -1 \n" |
---|
190 | "bnez $3, _idle_task_loop \n" |
---|
191 | "nop \n" |
---|
192 | : |
---|
193 | : "r"(count) |
---|
194 | : "$3" ); |
---|
195 | |
---|
196 | // warning message |
---|
197 | _printf("\n[GIET WARNING] Processor[%d,%d,%d] still idle at cycle %d", |
---|
198 | x , y , p , _get_proctime() ); |
---|
199 | } |
---|
200 | } // end ctx_idle() |
---|
201 | |
---|
202 | |
---|
203 | //////////////// |
---|
204 | void _ctx_eret() |
---|
205 | { |
---|
206 | asm volatile("eret"); |
---|
207 | } |
---|
208 | |
---|
209 | |
---|
210 | // Local Variables: |
---|
211 | // tab-width: 4 |
---|
212 | // c-basic-offset: 4 |
---|
213 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
214 | // indent-tabs-mode: nil |
---|
215 | // End: |
---|
216 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
217 | |
---|