[1] | 1 | /* |
---|
| 2 | * core.c - core descriptor access function. |
---|
| 3 | * |
---|
| 4 | * Author Ghassan Almaless (2008,2009,2010,2011,2012) |
---|
| 5 | * Mohamed Lamine Karaoui (2015) |
---|
| 6 | * Alain Greiner (2016) |
---|
| 7 | * |
---|
| 8 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 9 | * |
---|
| 10 | * This file is part of ALMOS-MKH. |
---|
| 11 | * |
---|
| 12 | * ALMOS-MKH.is free software; you can redistribute it and/or modify it |
---|
| 13 | * under the terms of the GNU General Public License as published by |
---|
| 14 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 15 | * |
---|
| 16 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
| 17 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 19 | * General Public License for more details. |
---|
| 20 | * |
---|
| 21 | * You should have received a copy of the GNU General Public License |
---|
| 22 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
| 23 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 24 | */ |
---|
| 25 | |
---|
[14] | 26 | #include <kernel_config.h> |
---|
[1] | 27 | #include <hal_types.h> |
---|
| 28 | #include <hal_special.h> |
---|
| 29 | #include <errno.h> |
---|
| 30 | #include <printk.h> |
---|
| 31 | #include <thread.h> |
---|
[5] | 32 | #include <chdev.h> |
---|
[1] | 33 | #include <dev_icu.h> |
---|
| 34 | #include <rpc.h> |
---|
| 35 | #include <cluster.h> |
---|
| 36 | #include <kmem.h> |
---|
| 37 | #include <dqdt.h> |
---|
| 38 | #include <core.h> |
---|
| 39 | |
---|
[14] | 40 | // TODO #include <sysfs.h> |
---|
[1] | 41 | |
---|
| 42 | ///////////////////////////////// |
---|
| 43 | void core_init( core_t * core, |
---|
[5] | 44 | lid_t lid, |
---|
| 45 | gid_t gid ) |
---|
[1] | 46 | { |
---|
| 47 | core->lid = lid; |
---|
| 48 | core->gid = gid; |
---|
| 49 | core->cycles = 0; |
---|
| 50 | core->time_stamp = 0; |
---|
| 51 | core->ticks_nr = 0; |
---|
| 52 | core->ticks_period = CONFIG_SCHED_TICK_PERIOD; |
---|
| 53 | core->usage = 0; |
---|
| 54 | core->spurious_irqs = 0; |
---|
| 55 | core->rpc_threads = 0; |
---|
| 56 | |
---|
| 57 | rpc_fifo_init( &core->rpc_fifo ); |
---|
| 58 | |
---|
| 59 | list_root_init( &core->rpc_free_list ); |
---|
| 60 | |
---|
| 61 | core->thread_rpc = NULL; |
---|
| 62 | core->thread_idle = NULL; |
---|
| 63 | core->fpu_owner = NULL; |
---|
| 64 | core->rand_last = hal_time_stamp() & 0xFFF; |
---|
| 65 | |
---|
| 66 | sched_init( core ); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | ////////////////////////////////////////////// |
---|
| 70 | inline uint32_t core_get_rand( core_t * core ) |
---|
| 71 | { |
---|
| 72 | uint32_t value = ((core->rand_last * CONFIG_RDNG_PARAM_A) + |
---|
| 73 | CONFIG_RDNG_PARAM_C) ^ (hal_time_stamp() & 0xFFF); |
---|
| 74 | core->rand_last = value; |
---|
| 75 | return value; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | //////////////////////////////////////////////// |
---|
| 79 | inline uint64_t core_get_cycles( core_t * core ) |
---|
| 80 | { |
---|
| 81 | uint32_t elapsed; |
---|
| 82 | uint64_t cycles; |
---|
| 83 | uint32_t time_stamp = core->time_stamp; |
---|
| 84 | uint32_t time_now = hal_time_stamp(); |
---|
| 85 | |
---|
| 86 | // compute number of elapsed cycles, taking into account 32 bits register wrap |
---|
| 87 | if(time_now < time_stamp) elapsed = (0xFFFFFFFF - time_stamp) + time_now; |
---|
| 88 | else elapsed = (time_now - time_stamp); |
---|
| 89 | |
---|
| 90 | cycles = core->cycles + elapsed; |
---|
| 91 | |
---|
| 92 | // update core time |
---|
| 93 | core->time_stamp = time_now; |
---|
| 94 | core->cycles = cycles; |
---|
| 95 | hal_wbflush(); |
---|
| 96 | |
---|
| 97 | return cycles; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | //////////////////////////////////// |
---|
| 101 | void core_get_time( core_t * core, |
---|
| 102 | uint32_t * tm_ms, |
---|
| 103 | uint32_t * tm_us ) |
---|
| 104 | { |
---|
| 105 | // uint64_t cycles = core_get_cycles( core ); |
---|
| 106 | |
---|
| 107 | // TODO ces deux ligne ne compilent pas : "undefined referenc to __udivdi3" |
---|
| 108 | |
---|
| 109 | // *tm_ms = (cycles / CONFIG_CYCLES_PER_MS); |
---|
| 110 | // *tm_us = (cycles % CONFIG_CYCLES_PER_MS) / (CONFIG_CYCLES_PER_MS / 1000000); |
---|
| 111 | |
---|
| 112 | printk("\n[PANIC] in %s : not implemented yet\n", __FUNCTION__ ); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | ////////////////////////////////////// |
---|
| 116 | void core_time_update( core_t * core ) |
---|
| 117 | { |
---|
| 118 | uint32_t elapsed; |
---|
| 119 | uint32_t ticks_nr = core->ticks_nr; |
---|
| 120 | uint64_t cycles = core->cycles; |
---|
| 121 | uint32_t time_stamp = core->time_stamp; |
---|
| 122 | uint32_t time_now = hal_time_stamp(); |
---|
| 123 | |
---|
| 124 | // compute number of elapsed cycles taking into account 32 bits register wrap |
---|
| 125 | if( time_now < time_stamp ) elapsed = (0xFFFFFFFF - time_stamp) + time_now; |
---|
| 126 | else elapsed = time_now - time_stamp; |
---|
| 127 | |
---|
| 128 | cycles += elapsed; |
---|
| 129 | ticks_nr = elapsed / core->ticks_period; |
---|
| 130 | |
---|
| 131 | core->time_stamp = time_now; |
---|
| 132 | core->cycles = cycles + elapsed; |
---|
| 133 | core->ticks_nr = ticks_nr + (elapsed / core->ticks_period); |
---|
| 134 | hal_wbflush(); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | //////////////////////////////// |
---|
| 138 | void core_clock( core_t * core ) |
---|
| 139 | { |
---|
| 140 | uint32_t ticks; |
---|
| 141 | |
---|
| 142 | // update cycles and ticks counter |
---|
| 143 | core_time_update( core ); |
---|
| 144 | |
---|
| 145 | // get current ticks number |
---|
| 146 | ticks = core->ticks_nr; |
---|
| 147 | |
---|
| 148 | // handle pending alarms TODO ??? [AG] |
---|
| 149 | // alarm_clock( &core->alarm_mgr , ticks ); |
---|
| 150 | |
---|
| 151 | // handle scheduler TODO improve the scheduling condition ... AG |
---|
| 152 | if( (ticks % 10) == 0 ) sched_yield(); |
---|
| 153 | |
---|
| 154 | // update DQDT TODO This update should depend on the cluster identifier, |
---|
| 155 | // to avoid simultaneous updates from various clusters ... AG |
---|
| 156 | if( ((ticks % CONFIG_DQDT_PERIOD) == 0) && (core->lid == 0) ) dqdt_global_update(); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | //////////////////////////////////////// |
---|
| 160 | void core_compute_stats( core_t * core ) |
---|
| 161 | { |
---|
| 162 | thread_t * idle = core->thread_idle; |
---|
| 163 | uint32_t ticks = core->ticks_nr; |
---|
| 164 | |
---|
| 165 | uint32_t idle_percent; |
---|
| 166 | uint32_t busy_percent; |
---|
| 167 | uint32_t usage; |
---|
| 168 | |
---|
| 169 | // compute cumulated usage |
---|
| 170 | ticks = (ticks) ? ticks : 1; |
---|
| 171 | idle_percent = (idle->ticks_nr * 100) / ticks; |
---|
| 172 | idle_percent = (idle_percent > 100) ? 100 : idle_percent; |
---|
| 173 | busy_percent = 100 - idle_percent; |
---|
| 174 | usage = (busy_percent + core->usage) / 2; |
---|
| 175 | |
---|
| 176 | // update core descriptor |
---|
| 177 | core->usage = usage; |
---|
| 178 | hal_wbflush(); |
---|
| 179 | |
---|
| 180 | #if CONFIG_SHOW_CPU_USAGE |
---|
| 181 | printk(INFO, "INFO: core %d in cluster %x : busy_percent = %d / cumulated_usage = %d\n", |
---|
| 182 | core->lid, local_cxy , busy_percent , usage ); |
---|
| 183 | #endif |
---|
| 184 | |
---|
| 185 | core->ticks_nr = 0; |
---|
| 186 | idle->ticks_nr = 0; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | ///////////////////////////////////// |
---|
| 190 | void core_reset_stats( core_t * core ) |
---|
| 191 | { |
---|
| 192 | core_time_update(core); |
---|
| 193 | |
---|
| 194 | core->ticks_nr = 0; |
---|
| 195 | core->usage = 0; |
---|
| 196 | core->thread_idle->ticks_nr = 0; |
---|
| 197 | hal_wbflush(); |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | /////////////////////////////////////////////////// |
---|
[5] | 201 | void core_set_irq_vector_entry( core_t * core, |
---|
[1] | 202 | uint32_t irq_type, |
---|
| 203 | uint32_t irq_id, |
---|
[5] | 204 | chdev_t * chdev ) |
---|
[1] | 205 | { |
---|
[5] | 206 | if ( irq_type == WTI_TYPE ) core->wti_vector[irq_id] = chdev; |
---|
| 207 | else if( irq_type == HWI_TYPE ) core->hwi_vector[irq_id] = chdev; |
---|
| 208 | else core->pti_vector[irq_id] = chdev; |
---|
[1] | 209 | } |
---|