| 1 | /* |
|---|
| 2 | * core.c - core descriptor access function. |
|---|
| 3 | * |
|---|
| 4 | * Author Ghassan Almaless (2008,2009,2010,2011,2012) |
|---|
| 5 | * Alain Greiner (2016,2017) |
|---|
| 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 | |
|---|
| 25 | #include <kernel_config.h> |
|---|
| 26 | #include <hal_types.h> |
|---|
| 27 | #include <hal_special.h> |
|---|
| 28 | #include <errno.h> |
|---|
| 29 | #include <printk.h> |
|---|
| 30 | #include <thread.h> |
|---|
| 31 | #include <chdev.h> |
|---|
| 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 | ///////////////////////////////// |
|---|
| 40 | void core_init( core_t * core, |
|---|
| 41 | lid_t lid, |
|---|
| 42 | gid_t gid ) |
|---|
| 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; |
|---|
| 52 | core->rpc_threads = 0; |
|---|
| 53 | |
|---|
| 54 | list_root_init( &core->rpc_free_list ); |
|---|
| 55 | |
|---|
| 56 | core->thread_rpc = NULL; |
|---|
| 57 | core->thread_idle = NULL; |
|---|
| 58 | core->fpu_owner = NULL; |
|---|
| 59 | core->rand_last = hal_get_cycles() & 0xFFF; |
|---|
| 60 | |
|---|
| 61 | sched_init( core ); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | ////////////////////////////////////////////// |
|---|
| 65 | inline uint32_t core_get_rand( core_t * core ) |
|---|
| 66 | { |
|---|
| 67 | uint32_t value = ((core->rand_last * CONFIG_RDNG_PARAM_A) + |
|---|
| 68 | CONFIG_RDNG_PARAM_C) ^ (hal_get_cycles() & 0xFFF); |
|---|
| 69 | core->rand_last = value; |
|---|
| 70 | return value; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | //////////////////////////////////// |
|---|
| 74 | void core_get_time( core_t * core, |
|---|
| 75 | uint32_t * tm_s, |
|---|
| 76 | uint32_t * tm_us ) |
|---|
| 77 | { |
|---|
| 78 | uint64_t cycles = hal_get_cycles(); |
|---|
| 79 | |
|---|
| 80 | *tm_s = (cycles / CONFIG_CYCLES_PER_MS); |
|---|
| 81 | *tm_us = (cycles % CONFIG_CYCLES_PER_MS) / (CONFIG_CYCLES_PER_MS / 1000000); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | ////////////////////////////////////// |
|---|
| 85 | void core_time_update( core_t * core ) |
|---|
| 86 | { |
|---|
| 87 | uint32_t elapsed; |
|---|
| 88 | uint32_t ticks_nr = core->ticks_nr; |
|---|
| 89 | uint64_t cycles = core->cycles; |
|---|
| 90 | uint32_t time_stamp = core->time_stamp; |
|---|
| 91 | uint32_t time_now = hal_get_cycles(); |
|---|
| 92 | |
|---|
| 93 | // compute number of elapsed cycles taking into account 32 bits register wrap |
|---|
| 94 | if( time_now < time_stamp ) elapsed = (0xFFFFFFFF - time_stamp) + time_now; |
|---|
| 95 | else elapsed = time_now - time_stamp; |
|---|
| 96 | |
|---|
| 97 | cycles += elapsed; |
|---|
| 98 | ticks_nr = elapsed / core->ticks_period; |
|---|
| 99 | |
|---|
| 100 | core->time_stamp = time_now; |
|---|
| 101 | core->cycles = cycles + elapsed; |
|---|
| 102 | core->ticks_nr = ticks_nr + (elapsed / core->ticks_period); |
|---|
| 103 | hal_fence(); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | //////////////////////////////// |
|---|
| 107 | void core_clock( core_t * core ) |
|---|
| 108 | { |
|---|
| 109 | uint32_t ticks; |
|---|
| 110 | |
|---|
| 111 | // update cycles and ticks counter |
|---|
| 112 | core_time_update( core ); |
|---|
| 113 | |
|---|
| 114 | // get current ticks number |
|---|
| 115 | ticks = core->ticks_nr; |
|---|
| 116 | |
|---|
| 117 | // handle pending alarms TODO ??? [AG] |
|---|
| 118 | // alarm_clock( &core->alarm_mgr , ticks ); |
|---|
| 119 | |
|---|
| 120 | // handle scheduler TODO improve the scheduling condition ... AG |
|---|
| 121 | if( (ticks % 10) == 0 ) sched_yield(); |
|---|
| 122 | |
|---|
| 123 | // update DQDT TODO This update should depend on the cluster identifier, |
|---|
| 124 | // to avoid simultaneous updates from various clusters ... AG |
|---|
| 125 | if( ((ticks % CONFIG_DQDT_PERIOD) == 0) && (core->lid == 0) ) dqdt_global_update(); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | //////////////////////////////////////// |
|---|
| 129 | void core_compute_stats( core_t * core ) |
|---|
| 130 | { |
|---|
| 131 | thread_t * idle = core->thread_idle; |
|---|
| 132 | uint32_t ticks = core->ticks_nr; |
|---|
| 133 | |
|---|
| 134 | uint32_t idle_percent; |
|---|
| 135 | uint32_t busy_percent; |
|---|
| 136 | uint32_t usage; |
|---|
| 137 | |
|---|
| 138 | // compute cumulated usage |
|---|
| 139 | ticks = (ticks) ? ticks : 1; |
|---|
| 140 | idle_percent = (idle->ticks_nr * 100) / ticks; |
|---|
| 141 | idle_percent = (idle_percent > 100) ? 100 : idle_percent; |
|---|
| 142 | busy_percent = 100 - idle_percent; |
|---|
| 143 | usage = (busy_percent + core->usage) / 2; |
|---|
| 144 | |
|---|
| 145 | // update core descriptor |
|---|
| 146 | core->usage = usage; |
|---|
| 147 | hal_fence(); |
|---|
| 148 | |
|---|
| 149 | #if CONFIG_SHOW_CPU_USAGE |
|---|
| 150 | printk(INFO, "INFO: core %d in cluster %x : busy_percent = %d / cumulated_usage = %d\n", |
|---|
| 151 | core->lid, local_cxy , busy_percent , usage ); |
|---|
| 152 | #endif |
|---|
| 153 | |
|---|
| 154 | core->ticks_nr = 0; |
|---|
| 155 | idle->ticks_nr = 0; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | ///////////////////////////////////// |
|---|
| 159 | void core_reset_stats( core_t * core ) |
|---|
| 160 | { |
|---|
| 161 | core_time_update(core); |
|---|
| 162 | |
|---|
| 163 | core->ticks_nr = 0; |
|---|
| 164 | core->usage = 0; |
|---|
| 165 | core->thread_idle->ticks_nr = 0; |
|---|
| 166 | hal_fence(); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | /////////////////////////////////////////////////// |
|---|
| 170 | void core_set_irq_vector_entry( core_t * core, |
|---|
| 171 | uint32_t irq_type, |
|---|
| 172 | uint32_t irq_id, |
|---|
| 173 | chdev_t * chdev ) |
|---|
| 174 | { |
|---|
| 175 | if ( irq_type == WTI_TYPE ) core->wti_vector[irq_id] = chdev; |
|---|
| 176 | else if( irq_type == HWI_TYPE ) core->hwi_vector[irq_id] = chdev; |
|---|
| 177 | else core->pti_vector[irq_id] = chdev; |
|---|
| 178 | } |
|---|