[444] | 1 | /* |
---|
| 2 | * Copyright (c) 2011 Aeroflex Gaisler |
---|
| 3 | * |
---|
| 4 | * BSD license: |
---|
| 5 | * |
---|
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
| 7 | * of this software and associated documentation files (the "Software"), to deal |
---|
| 8 | * in the Software without restriction, including without limitation the rights |
---|
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
| 10 | * copies of the Software, and to permit persons to whom the Software is |
---|
| 11 | * furnished to do so, subject to the following conditions: |
---|
| 12 | * |
---|
| 13 | * The above copyright notice and this permission notice shall be included in |
---|
| 14 | * all copies or substantial portions of the Software. |
---|
| 15 | * |
---|
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
---|
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
| 22 | * THE SOFTWARE. |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | |
---|
| 26 | #include <asm-leon/contextswitch.h> |
---|
| 27 | #include <asm-leon/leonbare_kernel.h> |
---|
| 28 | #include <asm-leon/leonbare_debug.h> |
---|
| 29 | #include <asm-leon/stack.h> |
---|
| 30 | #include <asm-leon/leonstack.h> |
---|
| 31 | #include <asm-leon/irq.h> |
---|
| 32 | |
---|
| 33 | struct leonbare_kernel leonbare_kernel; |
---|
| 34 | |
---|
| 35 | /* |
---|
| 36 | * queue 0: [ <acc=2>], |
---|
| 37 | * queue 1: [ <acc=10>, <acc=8>,<acc=8>,<acc=1> ], |
---|
| 38 | * queue 2: [ ... ], |
---|
| 39 | * ... |
---|
| 40 | * queue n: [ ... ] |
---|
| 41 | * |
---|
| 42 | * Seach through ready queue [0 - LEONBARE_RUNQ_READY_NR-1] for the |
---|
| 43 | * first thread in a queue'ss head to discover |
---|
| 44 | * leonbare_thread_tick_callback() will put threads that have their th_caccount |
---|
| 45 | * consumed into the prepare-run queues. th_caccount is already initialized |
---|
| 46 | * to the value for the next schedule round. So all there is to do is to |
---|
| 47 | * move the to prepare-run queues to run queues [0 - LEONBARE_RUNQ_READY_NR-1]. |
---|
| 48 | * return the first thread in any queue, similary to leonbare_sched_next(). |
---|
| 49 | * Using LEONBARE_KR_RUNQ_WHICH and leonbare_thread.th_runq_which one can |
---|
| 50 | * determine weather the thread is in a runqueue or a prepare-runqueue: |
---|
| 51 | * LEONBARE_KR_RUNQ_WHICH == leonbare_thread.th_runq_which : thread in runqueue |
---|
| 52 | * LEONBARE_KR_RUNQ_WHICH != leonbare_thread.th_runq_which : thread in prepare-runqueue |
---|
| 53 | * after a leonbare_thread_tick_callback() or a run queue change, call |
---|
| 54 | * leonbare_sched_update() to update LEONBARE_KR_NEXT and |
---|
| 55 | * LEONBARE_KR_NEED_SCHEDULE |
---|
| 56 | */ |
---|
| 57 | int |
---|
| 58 | leonbare_sched_update () |
---|
| 59 | { |
---|
| 60 | int idx; |
---|
| 61 | leonbare_thread_t n = 0; |
---|
| 62 | int i = 0; |
---|
| 63 | LEONBARE_VERIFYIRQDISABLED (); |
---|
| 64 | LEONBARE_VERIFYSCHED (); |
---|
| 65 | for (i = 0; i < LEONBARE_RUNQ_READY_NR; i++) |
---|
| 66 | { |
---|
| 67 | leonbare_thread_t c; |
---|
| 68 | if (!LBTAILQ_EMPTY (LEONBARE_KR_RUNQ (i))) |
---|
| 69 | { |
---|
| 70 | n = LBTAILQ_FIRST (LEONBARE_KR_RUNQ (i)); |
---|
| 71 | break; |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | if (!n) |
---|
| 75 | { |
---|
| 76 | for (idx = 0; idx < LEONBARE_RUNQ_READY_NR; idx++) |
---|
| 77 | { |
---|
| 78 | struct leonbare_thread_queue *h0 = |
---|
| 79 | (struct leonbare_thread_queue *) LEONBARE_KR_RUNQ (idx); |
---|
| 80 | struct leonbare_thread_queue *h1 = (struct leonbare_thread_queue *) |
---|
| 81 | LEONBARE_KR_RUNQ (idx + LEONBARE_RUNQ_PREPARE_IDX); |
---|
| 82 | if (LBTAILQ_EMPTY (h1)) |
---|
| 83 | { |
---|
| 84 | LBTAILQ_INIT (h0); |
---|
| 85 | } |
---|
| 86 | else |
---|
| 87 | { |
---|
| 88 | *h0 = *h1; |
---|
| 89 | if (LBTAILQ_FIRST (h0)) |
---|
| 90 | { |
---|
| 91 | LBTAILQ_FIRST (h0)->th_runq.tqe_prev = &(h0)->tqh_first; |
---|
| 92 | } |
---|
| 93 | if (!n) |
---|
| 94 | { |
---|
| 95 | n = LBTAILQ_FIRST (h0); |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | for (idx = 0; idx < LEONBARE_RUNQ_READY_NR; idx++) |
---|
| 100 | { |
---|
| 101 | LBTAILQ_INIT (LEONBARE_KR_RUNQ (idx + LEONBARE_RUNQ_PREPARE_IDX)); |
---|
| 102 | } |
---|
| 103 | LEONBARE_KR_RUNQ_WHICH++; |
---|
| 104 | LEONBARE_VERIFYSCHED (); |
---|
| 105 | LEONBARE_PRINTQUEUES (); |
---|
| 106 | } |
---|
| 107 | LEONBARE_KR_NEXT = n; |
---|
| 108 | return (LEONBARE_KR_NEED_SCHEDULE); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | /* called in the timer irq handling. Decrements the |
---|
| 112 | * th_caccount field. On consumption of th_caccount the |
---|
| 113 | * thread will be removed from the ready queue nad placed into the |
---|
| 114 | * prepare-runqueue for later readdition by leonbare_sched_readyprepare() |
---|
| 115 | * called from gettimeofday.c's installed ticker_callback callback |
---|
| 116 | * leonbare_thread_tick_callback() might change the kernel state in which case |
---|
| 117 | * state on return from leonbare_thread_tick_callback() leonbare_thread_schedule_callback() |
---|
| 118 | * will be called from rtrap_fast.S . |
---|
| 119 | */ |
---|
| 120 | int |
---|
| 121 | leonbare_thread_tick_callback () |
---|
| 122 | { |
---|
| 123 | LEONBARE_PROTECT_DECL (flags); |
---|
| 124 | unsigned int r; |
---|
| 125 | volatile leonbare_thread_t c = LEONBARE_KR_CURRENT; |
---|
| 126 | leonbare_thread_t i; |
---|
| 127 | LBDEBUG_FNCALL; |
---|
| 128 | if (c && LEONBARE_KR_IS_PREEMPTION) |
---|
| 129 | { |
---|
| 130 | |
---|
| 131 | LEONBARE_PROTECT_KERNEL_START (); |
---|
| 132 | { |
---|
| 133 | |
---|
| 134 | LEONBARE_VERIFYIRQDISABLED (); |
---|
| 135 | LEONBARE_VERIFYSCHED (); |
---|
| 136 | |
---|
| 137 | if ((--c->th_caccount) < 0) |
---|
| 138 | { |
---|
| 139 | LBDEBUG_HEADER_PRINTF (LBDEBUG_QUEUE_NR, "remove %s(%x)\n", |
---|
| 140 | LEONBARE_TH_NAME_DBG (c), c); |
---|
| 141 | LBTAILQ_REMOVE (LEONBARE_KR_RUNQ (c->th_runq_idx), c, th_runq); |
---|
| 142 | LBTAILQ_INSERT_TAIL (LEONBARE_KR_RUNQ |
---|
| 143 | (c->th_runq_idx + LEONBARE_RUNQ_PREPARE_IDX), |
---|
| 144 | c, th_runq); |
---|
| 145 | c->th_caccount = c->th_account; |
---|
| 146 | c->th_runq_which++; |
---|
| 147 | } |
---|
| 148 | else |
---|
| 149 | { |
---|
| 150 | /* todo: round robbin scheme */ |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | LEONBARE_PROTECT_KERNEL_END (); |
---|
| 154 | } |
---|
| 155 | r = leonbare_sched_update (); |
---|
| 156 | return r; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | /* called from rtrap_fast.S's installed schedule_callback callback */ |
---|
| 160 | int |
---|
| 161 | leonbare_thread_schedule_callback (struct leonbare_pt_regs *ptregs) |
---|
| 162 | { |
---|
| 163 | unsigned int retval; |
---|
| 164 | LBDEBUG_FNCALL; |
---|
| 165 | if (LEONBARE_KR_IS_IN_KERNEL == 0 && LEONBARE_KR_NEED_SCHEDULE) |
---|
| 166 | { |
---|
| 167 | |
---|
| 168 | leonbare_sched (); |
---|
| 169 | |
---|
| 170 | //KERNEL_ENTER; |
---|
| 171 | //KERNEL_EXIT(LEONBARE_KR_NEED_SCHEDULE, retval); |
---|
| 172 | } |
---|
| 173 | LBDEBUG_FNEXIT; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | |
---|
| 177 | struct leonbare_thread _thread_main; |
---|
| 178 | int |
---|
| 179 | leonbare_thread_init () |
---|
| 180 | { |
---|
| 181 | int i; |
---|
| 182 | LBDEBUG_FNCALL; |
---|
| 183 | |
---|
| 184 | memset ((void *) &_thread_main, 0, sizeof (_thread_main)); |
---|
| 185 | _thread_main.th_reentp = _impure_ptr; |
---|
| 186 | _thread_main.th_name = "<main>"; |
---|
| 187 | _thread_main.th_runq_idx = 0; |
---|
| 188 | _thread_main.th_pri_idx = 0; |
---|
| 189 | _thread_main.th_account = 0; |
---|
| 190 | |
---|
| 191 | LBTAILQ_INIT (LEONBARE_KR_ALLQ); |
---|
| 192 | for (i = 0; i < LEONBARE_RUNQ_NR; i++) |
---|
| 193 | { |
---|
| 194 | LBTAILQ_INIT (LEONBARE_KR_RUNQ (i)); |
---|
| 195 | } |
---|
| 196 | LBTAILQ_INIT (LEONBARE_KR_ALLM); |
---|
| 197 | |
---|
| 198 | /* queues */ |
---|
| 199 | LBTAILQ_INSERT_TAIL (LEONBARE_KR_ALLQ, &_thread_main, th_allq); |
---|
| 200 | |
---|
| 201 | /* inseart into ready queue 0 at head */ |
---|
| 202 | LBTAILQ_INSERT_HEAD (LEONBARE_KR_RUNQ (_thread_main.th_runq_idx), |
---|
| 203 | &_thread_main, th_runq); |
---|
| 204 | |
---|
| 205 | LEONBARE_KR_CURRENT = &_thread_main; |
---|
| 206 | LEONBARE_KR_IS_IN_KERNEL = 0; |
---|
| 207 | |
---|
| 208 | leonbare_init_ticks (); |
---|
| 209 | schedule_callback = (schedulehandler) leonbare_thread_schedule_callback; |
---|
| 210 | ticker_callback = (tickerhandler) leonbare_thread_tick_callback; |
---|
| 211 | |
---|
| 212 | /* disable later */ |
---|
| 213 | LEONBARE_KR_IS_PREEMPTION = 1; |
---|
| 214 | |
---|
| 215 | |
---|
| 216 | LBDEBUG_FNEXIT; |
---|
| 217 | } |
---|