| 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 <giet_config.h> |
|---|
| 10 | #include <hard_config.h> |
|---|
| 11 | #include <utils.h> |
|---|
| 12 | #include <tty0.h> |
|---|
| 13 | #include <xcu_driver.h> |
|---|
| 14 | |
|---|
| 15 | ///////////////////////////////////////////////////////////////////////////////// |
|---|
| 16 | // Extern variables and functions |
|---|
| 17 | ///////////////////////////////////////////////////////////////////////////////// |
|---|
| 18 | |
|---|
| 19 | // defined in giet_kernel/switch.s file |
|---|
| 20 | extern void _task_switch(unsigned int *, unsigned int *); |
|---|
| 21 | |
|---|
| 22 | // allocated in boot.c or kernel_init.c files |
|---|
| 23 | extern static_scheduler_t* _schedulers[X_SIZE][Y_SIZE][NB_PROCS_MAX]; |
|---|
| 24 | |
|---|
| 25 | ////////////////////////////////// |
|---|
| 26 | void _ctx_display( unsigned int x, |
|---|
| 27 | unsigned int y, |
|---|
| 28 | unsigned int p, |
|---|
| 29 | unsigned int ltid, |
|---|
| 30 | char* string ) |
|---|
| 31 | { |
|---|
| 32 | static_scheduler_t* psched = _schedulers[x][y][p]; |
|---|
| 33 | _printf("\n########## task[%d,%d,%d,%d] context\n" |
|---|
| 34 | " - CTX_EPC = %x\n" |
|---|
| 35 | " - CTX_PTAB = %x\n" |
|---|
| 36 | " - CTX_PTPR = %x\n" |
|---|
| 37 | " - CTX_VSID = %x\n" |
|---|
| 38 | " - CTX_SR = %x\n" |
|---|
| 39 | " - CTX_RA = %x\n" |
|---|
| 40 | " - CTX_SP = %x\n" |
|---|
| 41 | " - CTX_NORUN = %x\n" |
|---|
| 42 | "########## %s\n", |
|---|
| 43 | x , y , p , ltid , |
|---|
| 44 | psched->context[ltid][CTX_EPC_ID], |
|---|
| 45 | psched->context[ltid][CTX_PTAB_ID], |
|---|
| 46 | psched->context[ltid][CTX_PTPR_ID], |
|---|
| 47 | psched->context[ltid][CTX_VSID_ID], |
|---|
| 48 | psched->context[ltid][CTX_SR_ID], |
|---|
| 49 | psched->context[ltid][CTX_RA_ID], |
|---|
| 50 | psched->context[ltid][CTX_SP_ID], |
|---|
| 51 | psched->context[ltid][CTX_NORUN_ID], |
|---|
| 52 | string ); |
|---|
| 53 | } // _ctx_display() |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | ////////////////// |
|---|
| 57 | void _ctx_switch() |
|---|
| 58 | { |
|---|
| 59 | unsigned int gpid = _get_procid(); |
|---|
| 60 | unsigned int cluster_xy = gpid >> P_WIDTH; |
|---|
| 61 | unsigned int lpid = gpid & ((1<<P_WIDTH)-1); |
|---|
| 62 | |
|---|
| 63 | // get scheduler address |
|---|
| 64 | static_scheduler_t* psched = (static_scheduler_t*)_get_sched(); |
|---|
| 65 | |
|---|
| 66 | // get number of tasks allocated to scheduler |
|---|
| 67 | unsigned int tasks = psched->tasks; |
|---|
| 68 | |
|---|
| 69 | // get current task index |
|---|
| 70 | unsigned int curr_task_id = psched->current; |
|---|
| 71 | |
|---|
| 72 | // select the next task using a round-robin policy |
|---|
| 73 | unsigned int next_task_id; |
|---|
| 74 | unsigned int tid; |
|---|
| 75 | unsigned int found = 0; |
|---|
| 76 | |
|---|
| 77 | for (tid = curr_task_id + 1; tid < curr_task_id + 1 + tasks; tid++) |
|---|
| 78 | { |
|---|
| 79 | next_task_id = tid % tasks; |
|---|
| 80 | // test if the task is runable |
|---|
| 81 | if ( psched->context[next_task_id][CTX_NORUN_ID] == 0 ) |
|---|
| 82 | { |
|---|
| 83 | found = 1; |
|---|
| 84 | break; |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | // launch "idle" task if no runable task |
|---|
| 89 | if (found == 0) next_task_id = IDLE_TASK_INDEX; |
|---|
| 90 | |
|---|
| 91 | #if GIET_DEBUG_SWITCH |
|---|
| 92 | unsigned int x = cluster_xy >> Y_WIDTH; |
|---|
| 93 | unsigned int y = cluster_xy & ((1<<Y_WIDTH)-1); |
|---|
| 94 | _printf("\n[DEBUG SWITCH] (%d) -> (%d) on processor[%d,%d,%d] at cycle %d\n", |
|---|
| 95 | curr_task_id, next_task_id, x, y , lpid, _get_proctime() ); |
|---|
| 96 | #endif |
|---|
| 97 | |
|---|
| 98 | if (curr_task_id != next_task_id) // actual task switch required |
|---|
| 99 | { |
|---|
| 100 | unsigned int* curr_ctx_vaddr = &(psched->context[curr_task_id][0]); |
|---|
| 101 | unsigned int* next_ctx_vaddr = &(psched->context[next_task_id][0]); |
|---|
| 102 | |
|---|
| 103 | // reset TICK timer counter. |
|---|
| 104 | _xcu_timer_reset_cpt( cluster_xy, lpid ); |
|---|
| 105 | |
|---|
| 106 | // set current task index |
|---|
| 107 | psched->current = next_task_id; |
|---|
| 108 | |
|---|
| 109 | // makes context switch |
|---|
| 110 | _task_switch( curr_ctx_vaddr , next_ctx_vaddr ); |
|---|
| 111 | } |
|---|
| 112 | } //end _ctx_switch() |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | ///////////////// |
|---|
| 116 | void _idle_task() |
|---|
| 117 | { |
|---|
| 118 | unsigned int gpid = _get_procid(); |
|---|
| 119 | unsigned int cluster_xy = gpid >> P_WIDTH; |
|---|
| 120 | unsigned int x = cluster_xy >> Y_WIDTH; |
|---|
| 121 | unsigned int y = cluster_xy & ((1<<Y_WIDTH)-1); |
|---|
| 122 | unsigned int p = gpid & ((1<<P_WIDTH)-1); |
|---|
| 123 | |
|---|
| 124 | while(1) |
|---|
| 125 | { |
|---|
| 126 | // initialize counter |
|---|
| 127 | unsigned int count = GIET_IDLE_TASK_PERIOD; |
|---|
| 128 | |
|---|
| 129 | // decounting loop |
|---|
| 130 | asm volatile( |
|---|
| 131 | "move $3, %0 \n" |
|---|
| 132 | "_idle_task_loop: \n" |
|---|
| 133 | "addi $3, $3, -1 \n" |
|---|
| 134 | "bnez $3, _idle_task_loop \n" |
|---|
| 135 | "nop \n" |
|---|
| 136 | : |
|---|
| 137 | : "r"(count) |
|---|
| 138 | : "$3" ); |
|---|
| 139 | |
|---|
| 140 | // warning message |
|---|
| 141 | _printf("\n[GIET WARNING] Processor[%d,%d,%d] still idle at cycle %d", |
|---|
| 142 | x , y , p , _get_proctime() ); |
|---|
| 143 | } |
|---|
| 144 | } // end ctx_idle() |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | //////////////// |
|---|
| 148 | void _ctx_eret() |
|---|
| 149 | { |
|---|
| 150 | asm volatile("eret"); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | // Local Variables: |
|---|
| 155 | // tab-width: 4 |
|---|
| 156 | // c-basic-offset: 4 |
|---|
| 157 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
|---|
| 158 | // indent-tabs-mode: nil |
|---|
| 159 | // End: |
|---|
| 160 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
|---|
| 161 | |
|---|