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