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 <giet_config.h> |
---|
9 | #include <tim_driver.h> |
---|
10 | #include <xcu_driver.h> |
---|
11 | #include <tty_driver.h> |
---|
12 | #include <utils.h> |
---|
13 | #include <ctx_handler.h> |
---|
14 | #include <mapping_info.h> |
---|
15 | #include <sys_handler.h> |
---|
16 | |
---|
17 | extern void _task_switch(unsigned int *, unsigned int *); |
---|
18 | |
---|
19 | ///////////////////////////////////////////////////////////////////////////////// |
---|
20 | // This function performs a context switch between the running task |
---|
21 | // and another task, using a round-robin sheduling policy between all |
---|
22 | // tasks allocated to a given processor (static allocation). |
---|
23 | // It selects the next runable task to resume execution. |
---|
24 | // If the only runable task is the current task, return without context switch. |
---|
25 | // If there is no runable task, the scheduler switch to the default "idle" task. |
---|
26 | ///////////////////////////////////////////////////////////////////////////////// |
---|
27 | // Implementation note |
---|
28 | // The return address contained in $31 is saved in the current task context |
---|
29 | // (in the ctx[31] slot), and the function actually returns to the address |
---|
30 | // contained in the ctx[31] slot of the next task context. |
---|
31 | ///////////////////////////////////////////////////////////////////////////////// |
---|
32 | void _ctx_switch() |
---|
33 | { |
---|
34 | unsigned int gpid = _get_procid(); |
---|
35 | unsigned int cluster_xy = gpid / NB_PROCS_MAX; |
---|
36 | unsigned int lpid = gpid % NB_PROCS_MAX; |
---|
37 | |
---|
38 | // get scheduler address |
---|
39 | static_scheduler_t* psched = (static_scheduler_t*)_get_sched(); |
---|
40 | |
---|
41 | // get number of tasks allocated to scheduler |
---|
42 | unsigned int tasks = psched->tasks; |
---|
43 | |
---|
44 | // get current task index |
---|
45 | unsigned int curr_task_id = psched->current; |
---|
46 | |
---|
47 | // select the next task using a round-robin policy |
---|
48 | unsigned int next_task_id; |
---|
49 | unsigned int tid; |
---|
50 | unsigned int found = 0; |
---|
51 | |
---|
52 | for (tid = curr_task_id + 1; tid < curr_task_id + 1 + tasks; tid++) |
---|
53 | { |
---|
54 | next_task_id = tid % tasks; |
---|
55 | // test if the task is runable |
---|
56 | if ( psched->context[next_task_id][CTX_RUN_ID] ) |
---|
57 | { |
---|
58 | found = 1; |
---|
59 | break; |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | // launch "idle" task if no runable task |
---|
64 | if (found == 0) |
---|
65 | { |
---|
66 | next_task_id = IDLE_TASK_INDEX; |
---|
67 | } |
---|
68 | |
---|
69 | #if GIET_DEBUG_SWITCH |
---|
70 | unsigned int x = cluster_xy >> Y_WIDTH; |
---|
71 | unsigned int y = cluster_xy & ((1<<Y_WIDTH)-1); |
---|
72 | |
---|
73 | _printf("\n[TASK SWITCH] (%d) -> (%d) on processor[%d,%d,%d] at cycle %d\n", |
---|
74 | curr_task_id, next_task_id, x, y , lpid, _get_proctime() ); |
---|
75 | #endif |
---|
76 | |
---|
77 | if (curr_task_id != next_task_id) // actual task switch required |
---|
78 | { |
---|
79 | unsigned int* curr_ctx_vaddr = &(psched->context[curr_task_id][0]); |
---|
80 | unsigned int* next_ctx_vaddr = &(psched->context[next_task_id][0]); |
---|
81 | |
---|
82 | // reset timer counter. In each cluster, |
---|
83 | // the NB_PROCS_MAX timers are system timers (TICK) |
---|
84 | |
---|
85 | #if USE_XICU |
---|
86 | _xcu_timer_reset_cpt( cluster_xy, lpid ); |
---|
87 | #else |
---|
88 | _timer_reset_cpt( cluster_xy, lpid); |
---|
89 | #endif |
---|
90 | |
---|
91 | // set current task index |
---|
92 | psched->current = next_task_id; |
---|
93 | |
---|
94 | // makes context switch |
---|
95 | _task_switch(curr_ctx_vaddr, next_ctx_vaddr); |
---|
96 | } |
---|
97 | } //end _ctx_switch() |
---|
98 | |
---|
99 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
100 | // This function is executed as the"idle" task when no other task can be executed |
---|
101 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
102 | void _idle_task() |
---|
103 | { |
---|
104 | while(1) |
---|
105 | { |
---|
106 | unsigned int count = GIET_IDLE_TASK_PERIOD; |
---|
107 | |
---|
108 | // decounting loop |
---|
109 | asm volatile( |
---|
110 | "move $3, %0 \n" |
---|
111 | "_idle_task_loop: \n" |
---|
112 | "addi $3, $3, -1 \n" |
---|
113 | "bnez $3, _idle_task_loop \n" |
---|
114 | "nop \n" |
---|
115 | : |
---|
116 | : "r"(count) |
---|
117 | : "$3" ); |
---|
118 | |
---|
119 | // warning message |
---|
120 | unsigned int gpid = _get_procid(); |
---|
121 | unsigned int cluster_xy = gpid / NB_PROCS_MAX; |
---|
122 | unsigned int lpid = gpid % NB_PROCS_MAX; |
---|
123 | unsigned int x = cluster_xy >> Y_WIDTH; |
---|
124 | unsigned int y = cluster_xy & ((1<<Y_WIDTH)-1); |
---|
125 | |
---|
126 | _printf("\n[GIET WARNING] Processor[%d,%d,%d] still idle at cycle %d\n", |
---|
127 | x, y, lpid, _get_proctime() ); |
---|
128 | |
---|
129 | } |
---|
130 | } // end ctx_idle() |
---|
131 | |
---|
132 | |
---|
133 | ///////////////////////////////////////////////////////////////////////////////// |
---|
134 | // The address of this function is used to initialise the return address |
---|
135 | // in the "idle" task context. |
---|
136 | ///////////////////////////////////////////////////////////////////////////////// |
---|
137 | void _ctx_eret() |
---|
138 | { |
---|
139 | asm volatile("eret"); |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | // Local Variables: |
---|
144 | // tab-width: 4 |
---|
145 | // c-basic-offset: 4 |
---|
146 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
147 | // indent-tabs-mode: nil |
---|
148 | // End: |
---|
149 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
150 | |
---|