[294] | 1 | ////////////////////////////////////////////////////////////////////////////////// |
---|
[258] | 2 | // File : ctx_handler.c |
---|
| 3 | // Date : 01/04/2012 |
---|
| 4 | // Authors : alain greiner & joel porquet |
---|
| 5 | // Copyright (c) UPMC-LIP6 |
---|
[294] | 6 | ////////////////////////////////////////////////////////////////////////////////// |
---|
[258] | 7 | |
---|
[440] | 8 | #include <ctx_handler.h> |
---|
[258] | 9 | #include <giet_config.h> |
---|
[440] | 10 | #include <utils.h> |
---|
[459] | 11 | #include <tty0.h> |
---|
[258] | 12 | #include <xcu_driver.h> |
---|
| 13 | |
---|
[440] | 14 | ////////// defined in giet_kernel/switch.s file ///////// |
---|
[258] | 15 | extern void _task_switch(unsigned int *, unsigned int *); |
---|
| 16 | |
---|
[440] | 17 | ////////////////// |
---|
[258] | 18 | void _ctx_switch() |
---|
| 19 | { |
---|
[294] | 20 | unsigned int gpid = _get_procid(); |
---|
[428] | 21 | unsigned int cluster_xy = gpid >> P_WIDTH; |
---|
| 22 | unsigned int lpid = gpid & ((1<<P_WIDTH)-1); |
---|
[294] | 23 | |
---|
[258] | 24 | // get scheduler address |
---|
| 25 | static_scheduler_t* psched = (static_scheduler_t*)_get_sched(); |
---|
| 26 | |
---|
| 27 | // get number of tasks allocated to scheduler |
---|
| 28 | unsigned int tasks = psched->tasks; |
---|
| 29 | |
---|
| 30 | // get current task index |
---|
| 31 | unsigned int curr_task_id = psched->current; |
---|
| 32 | |
---|
| 33 | // select the next task using a round-robin policy |
---|
| 34 | unsigned int next_task_id; |
---|
| 35 | unsigned int tid; |
---|
| 36 | unsigned int found = 0; |
---|
| 37 | |
---|
| 38 | for (tid = curr_task_id + 1; tid < curr_task_id + 1 + tasks; tid++) |
---|
| 39 | { |
---|
| 40 | next_task_id = tid % tasks; |
---|
| 41 | // test if the task is runable |
---|
| 42 | if ( psched->context[next_task_id][CTX_RUN_ID] ) |
---|
| 43 | { |
---|
| 44 | found = 1; |
---|
| 45 | break; |
---|
| 46 | } |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | // launch "idle" task if no runable task |
---|
| 50 | if (found == 0) |
---|
| 51 | { |
---|
| 52 | next_task_id = IDLE_TASK_INDEX; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | #if GIET_DEBUG_SWITCH |
---|
[294] | 56 | unsigned int x = cluster_xy >> Y_WIDTH; |
---|
| 57 | unsigned int y = cluster_xy & ((1<<Y_WIDTH)-1); |
---|
| 58 | |
---|
| 59 | _printf("\n[TASK SWITCH] (%d) -> (%d) on processor[%d,%d,%d] at cycle %d\n", |
---|
| 60 | curr_task_id, next_task_id, x, y , lpid, _get_proctime() ); |
---|
[258] | 61 | #endif |
---|
| 62 | |
---|
[294] | 63 | if (curr_task_id != next_task_id) // actual task switch required |
---|
| 64 | { |
---|
[275] | 65 | unsigned int* curr_ctx_vaddr = &(psched->context[curr_task_id][0]); |
---|
| 66 | unsigned int* next_ctx_vaddr = &(psched->context[next_task_id][0]); |
---|
| 67 | |
---|
[330] | 68 | // reset timer counter. |
---|
| 69 | // In all clusters, the first NB_PROCS_MAX |
---|
| 70 | // timers are system timers (TICK) |
---|
[294] | 71 | |
---|
[322] | 72 | #if USE_XCU |
---|
[294] | 73 | _xcu_timer_reset_cpt( cluster_xy, lpid ); |
---|
[275] | 74 | #else |
---|
[294] | 75 | _timer_reset_cpt( cluster_xy, lpid); |
---|
[275] | 76 | #endif |
---|
| 77 | |
---|
| 78 | // set current task index |
---|
| 79 | psched->current = next_task_id; |
---|
| 80 | |
---|
| 81 | // makes context switch |
---|
| 82 | _task_switch(curr_ctx_vaddr, next_ctx_vaddr); |
---|
[258] | 83 | } |
---|
| 84 | } //end _ctx_switch() |
---|
| 85 | |
---|
[440] | 86 | ///////////////// |
---|
[258] | 87 | void _idle_task() |
---|
| 88 | { |
---|
[391] | 89 | unsigned int gpid = _get_procid(); |
---|
[428] | 90 | unsigned int cluster_xy = gpid >> P_WIDTH; |
---|
[391] | 91 | unsigned int x = cluster_xy >> Y_WIDTH; |
---|
| 92 | unsigned int y = cluster_xy & ((1<<Y_WIDTH)-1); |
---|
[440] | 93 | unsigned int p = gpid & ((1<<P_WIDTH)-1); |
---|
[391] | 94 | |
---|
[258] | 95 | while(1) |
---|
| 96 | { |
---|
[391] | 97 | // initialize counter |
---|
[294] | 98 | unsigned int count = GIET_IDLE_TASK_PERIOD; |
---|
[275] | 99 | |
---|
[294] | 100 | // decounting loop |
---|
[258] | 101 | asm volatile( |
---|
| 102 | "move $3, %0 \n" |
---|
| 103 | "_idle_task_loop: \n" |
---|
| 104 | "addi $3, $3, -1 \n" |
---|
| 105 | "bnez $3, _idle_task_loop \n" |
---|
| 106 | "nop \n" |
---|
| 107 | : |
---|
| 108 | : "r"(count) |
---|
| 109 | : "$3" ); |
---|
| 110 | |
---|
[294] | 111 | // warning message |
---|
[440] | 112 | _puts("\n[GIET WARNING] Processor["); |
---|
| 113 | _putd( x ); |
---|
| 114 | _puts(","); |
---|
| 115 | _putd( y ); |
---|
| 116 | _puts(","); |
---|
| 117 | _putd( p ); |
---|
| 118 | _puts("] still idle at cycle "); |
---|
| 119 | _putd( _get_proctime() ); |
---|
| 120 | _puts("\n"); |
---|
[258] | 121 | } |
---|
| 122 | } // end ctx_idle() |
---|
| 123 | |
---|
| 124 | |
---|
[440] | 125 | //////////////// |
---|
[258] | 126 | void _ctx_eret() |
---|
| 127 | { |
---|
| 128 | asm volatile("eret"); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | // Local Variables: |
---|
| 133 | // tab-width: 4 |
---|
| 134 | // c-basic-offset: 4 |
---|
| 135 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
| 136 | // indent-tabs-mode: nil |
---|
| 137 | // End: |
---|
| 138 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
| 139 | |
---|