[1] | 1 | /* |
---|
| 2 | * cluster.c - Cluster-Manager related operations |
---|
[19] | 3 | * |
---|
[1] | 4 | * Author Ghassan Almaless (2008,2009,2010,2011,2012) |
---|
| 5 | * Mohamed Lamine Karaoui (2015) |
---|
[437] | 6 | * Alain Greiner (2016,2017,2018) |
---|
[1] | 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> |
---|
[456] | 27 | #include <hal_kernel_types.h> |
---|
[1] | 28 | #include <hal_atomic.h> |
---|
| 29 | #include <hal_special.h> |
---|
[50] | 30 | #include <hal_ppm.h> |
---|
[407] | 31 | #include <remote_fifo.h> |
---|
[1] | 32 | #include <printk.h> |
---|
| 33 | #include <errno.h> |
---|
| 34 | #include <spinlock.h> |
---|
| 35 | #include <core.h> |
---|
[443] | 36 | #include <chdev.h> |
---|
[1] | 37 | #include <scheduler.h> |
---|
| 38 | #include <list.h> |
---|
| 39 | #include <cluster.h> |
---|
| 40 | #include <boot_info.h> |
---|
| 41 | #include <bits.h> |
---|
| 42 | #include <ppm.h> |
---|
| 43 | #include <thread.h> |
---|
| 44 | #include <kmem.h> |
---|
| 45 | #include <process.h> |
---|
| 46 | #include <dqdt.h> |
---|
[561] | 47 | #include <cluster_info.h> |
---|
[1] | 48 | |
---|
[408] | 49 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[1] | 50 | // Extern global variables |
---|
[408] | 51 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[1] | 52 | |
---|
[443] | 53 | extern process_t process_zero; // allocated in kernel_init.c file |
---|
| 54 | extern chdev_directory_t chdev_dir; // allocated in kernel_init.c file |
---|
[1] | 55 | |
---|
[443] | 56 | ///////////////////////////////////////////////n |
---|
[1] | 57 | error_t cluster_init( struct boot_info_s * info ) |
---|
| 58 | { |
---|
[428] | 59 | error_t error; |
---|
| 60 | lpid_t lpid; // local process_index |
---|
| 61 | lid_t lid; // local core index |
---|
| 62 | uint32_t i; // index in loop on external peripherals |
---|
| 63 | boot_device_t * dev; // pointer on external peripheral |
---|
| 64 | uint32_t func; // external peripheral functionnal type |
---|
[1] | 65 | |
---|
| 66 | cluster_t * cluster = LOCAL_CLUSTER; |
---|
| 67 | |
---|
| 68 | // initialize cluster global parameters |
---|
[19] | 69 | cluster->paddr_width = info->paddr_width; |
---|
[1] | 70 | cluster->x_width = info->x_width; |
---|
| 71 | cluster->y_width = info->y_width; |
---|
| 72 | cluster->x_size = info->x_size; |
---|
| 73 | cluster->y_size = info->y_size; |
---|
[530] | 74 | cluster->x_max = info->x_max; // [FIXME] |
---|
| 75 | cluster->y_max = info->y_max; // [FIXME] |
---|
[1] | 76 | cluster->io_cxy = info->io_cxy; |
---|
| 77 | |
---|
[557] | 78 | // initialize the cluster_info[][] array |
---|
| 79 | int x; |
---|
| 80 | int y; |
---|
| 81 | for (x = 0; x < CONFIG_MAX_CLUSTERS_X; x++) { |
---|
| 82 | for (y = 0; y < CONFIG_MAX_CLUSTERS_Y;y++) { |
---|
| 83 | cluster->cluster_info[x][y] = info->cluster_info[x][y]; |
---|
| 84 | } |
---|
| 85 | } |
---|
[428] | 86 | // initialize external peripherals channels |
---|
| 87 | for( i = 0 ; i < info->ext_dev_nr ; i++ ) |
---|
| 88 | { |
---|
| 89 | dev = &info->ext_dev[i]; |
---|
| 90 | func = FUNC_FROM_TYPE( dev->type ); |
---|
| 91 | if( func == DEV_FUNC_TXT ) cluster->nb_txt_channels = dev->channels; |
---|
| 92 | if( func == DEV_FUNC_NIC ) cluster->nb_nic_channels = dev->channels; |
---|
| 93 | if( func == DEV_FUNC_IOC ) cluster->nb_ioc_channels = dev->channels; |
---|
| 94 | if( func == DEV_FUNC_FBF ) cluster->nb_fbf_channels = dev->channels; |
---|
| 95 | } |
---|
| 96 | |
---|
[1] | 97 | // initialize cluster local parameters |
---|
| 98 | cluster->cores_nr = info->cores_nr; |
---|
| 99 | |
---|
[19] | 100 | // initialize the lock protecting the embedded kcm allocator |
---|
[1] | 101 | spinlock_init( &cluster->kcm_lock ); |
---|
| 102 | |
---|
[438] | 103 | #if DEBUG_CLUSTER_INIT |
---|
[433] | 104 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 105 | if( DEBUG_CLUSTER_INIT < cycle ) |
---|
[437] | 106 | printk("\n[DBG] %s : thread %x enters for cluster %x / cycle %d\n", |
---|
| 107 | __FUNCTION__ , CURRENT_THREAD , local_cxy , cycle ); |
---|
[433] | 108 | #endif |
---|
[50] | 109 | |
---|
[19] | 110 | // initialises DQDT |
---|
[530] | 111 | cluster->dqdt_root_level = dqdt_init( info->x_max, // [FIXME] |
---|
| 112 | info->y_max, // [FIXME] |
---|
[438] | 113 | info->y_width ) - 1; |
---|
[1] | 114 | |
---|
| 115 | // initialises embedded PPM |
---|
[50] | 116 | error = hal_ppm_init( info ); |
---|
[1] | 117 | |
---|
[50] | 118 | if( error ) |
---|
| 119 | { |
---|
| 120 | printk("\n[ERROR] in %s : cannot initialize PPM in cluster %x\n", |
---|
| 121 | __FUNCTION__ , local_cxy ); |
---|
| 122 | return ENOMEM; |
---|
| 123 | } |
---|
| 124 | |
---|
[438] | 125 | #if( DEBUG_CLUSTER_INIT & 1 ) |
---|
[433] | 126 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 127 | if( DEBUG_CLUSTER_INIT < cycle ) |
---|
[437] | 128 | printk("\n[DBG] %s : PPM initialized in cluster %x / cycle %d\n", |
---|
[433] | 129 | __FUNCTION__ , local_cxy , cycle ); |
---|
| 130 | #endif |
---|
[50] | 131 | |
---|
[1] | 132 | // initialises embedded KHM |
---|
| 133 | khm_init( &cluster->khm ); |
---|
[19] | 134 | |
---|
[438] | 135 | #if( DEBUG_CLUSTER_INIT & 1 ) |
---|
[457] | 136 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 137 | if( DEBUG_CLUSTER_INIT < cycle ) |
---|
[437] | 138 | printk("\n[DBG] %s : KHM initialized in cluster %x at cycle %d\n", |
---|
| 139 | __FUNCTION__ , local_cxy , hal_get_cycles() ); |
---|
| 140 | #endif |
---|
[50] | 141 | |
---|
[19] | 142 | // initialises embedded KCM |
---|
[5] | 143 | kcm_init( &cluster->kcm , KMEM_KCM ); |
---|
[1] | 144 | |
---|
[438] | 145 | #if( DEBUG_CLUSTER_INIT & 1 ) |
---|
[457] | 146 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 147 | if( DEBUG_CLUSTER_INIT < cycle ) |
---|
[437] | 148 | printk("\n[DBG] %s : KCM initialized in cluster %x at cycle %d\n", |
---|
| 149 | __FUNCTION__ , local_cxy , hal_get_cycles() ); |
---|
| 150 | #endif |
---|
[50] | 151 | |
---|
[296] | 152 | // initialises all cores descriptors |
---|
[1] | 153 | for( lid = 0 ; lid < cluster->cores_nr; lid++ ) |
---|
| 154 | { |
---|
| 155 | core_init( &cluster->core_tbl[lid], // target core descriptor |
---|
| 156 | lid, // local core index |
---|
| 157 | info->core[lid].gid ); // gid from boot_info_t |
---|
| 158 | } |
---|
[19] | 159 | |
---|
[438] | 160 | #if( DEBUG_CLUSTER_INIT & 1 ) |
---|
[433] | 161 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 162 | if( DEBUG_CLUSTER_INIT < cycle ) |
---|
[437] | 163 | printk("\n[DBG] %s : cores initialized in cluster %x / cycle %d\n", |
---|
[433] | 164 | __FUNCTION__ , local_cxy , cycle ); |
---|
| 165 | #endif |
---|
[50] | 166 | |
---|
[440] | 167 | // initialises RPC FIFOs |
---|
| 168 | for( lid = 0 ; lid < cluster->cores_nr; lid++ ) |
---|
| 169 | { |
---|
| 170 | local_fifo_init( &cluster->rpc_fifo[lid] ); |
---|
| 171 | cluster->rpc_threads[lid] = 0; |
---|
| 172 | } |
---|
[1] | 173 | |
---|
[438] | 174 | #if( DEBUG_CLUSTER_INIT & 1 ) |
---|
[437] | 175 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 176 | if( DEBUG_CLUSTER_INIT < cycle ) |
---|
[437] | 177 | printk("\n[DBG] %s : RPC fifo inialized in cluster %x at cycle %d\n", |
---|
[407] | 178 | __FUNCTION__ , local_cxy , hal_get_cycles() ); |
---|
[437] | 179 | #endif |
---|
[50] | 180 | |
---|
[1] | 181 | // initialise pref_tbl[] in process manager |
---|
| 182 | spinlock_init( &cluster->pmgr.pref_lock ); |
---|
| 183 | cluster->pmgr.pref_nr = 0; |
---|
[19] | 184 | cluster->pmgr.pref_tbl[0] = XPTR( local_cxy , &process_zero ); |
---|
[1] | 185 | for( lpid = 1 ; lpid < CONFIG_MAX_PROCESS_PER_CLUSTER ; lpid++ ) |
---|
| 186 | { |
---|
| 187 | cluster->pmgr.pref_tbl[lpid] = XPTR_NULL; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | // initialise local_list in process manager |
---|
[23] | 191 | remote_spinlock_init( XPTR( local_cxy , &cluster->pmgr.local_lock ) ); |
---|
| 192 | xlist_root_init( XPTR( local_cxy , &cluster->pmgr.local_root ) ); |
---|
[1] | 193 | cluster->pmgr.local_nr = 0; |
---|
| 194 | |
---|
| 195 | // initialise copies_lists in process manager |
---|
[101] | 196 | for( lpid = 0 ; lpid < CONFIG_MAX_PROCESS_PER_CLUSTER ; lpid++ ) |
---|
[1] | 197 | { |
---|
| 198 | remote_spinlock_init( XPTR( local_cxy , &cluster->pmgr.copies_lock[lpid] ) ); |
---|
| 199 | cluster->pmgr.copies_nr[lpid] = 0; |
---|
| 200 | xlist_root_init( XPTR( local_cxy , &cluster->pmgr.copies_root[lpid] ) ); |
---|
[19] | 201 | } |
---|
[1] | 202 | |
---|
[438] | 203 | #if DEBUG_CLUSTER_INIT |
---|
[433] | 204 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 205 | if( DEBUG_CLUSTER_INIT < cycle ) |
---|
[437] | 206 | printk("\n[DBG] %s , thread %x exit for cluster %x / cycle %d\n", |
---|
| 207 | __FUNCTION__ , CURRENT_THREAD , local_cxy , cycle ); |
---|
[433] | 208 | #endif |
---|
[50] | 209 | |
---|
[124] | 210 | hal_fence(); |
---|
[1] | 211 | |
---|
| 212 | return 0; |
---|
| 213 | } // end cluster_init() |
---|
| 214 | |
---|
[561] | 215 | ///////////////////////////////// |
---|
| 216 | cxy_t cluster_random_select( void ) |
---|
| 217 | { |
---|
| 218 | uint32_t x_size; |
---|
| 219 | uint32_t y_size; |
---|
| 220 | uint32_t y_width; |
---|
| 221 | uint32_t index; |
---|
| 222 | uint32_t x; |
---|
| 223 | uint32_t y; |
---|
| 224 | |
---|
| 225 | do { |
---|
| 226 | x_size = LOCAL_CLUSTER->x_size; |
---|
| 227 | y_size = LOCAL_CLUSTER->y_size; |
---|
| 228 | y_width = LOCAL_CLUSTER->y_width; |
---|
| 229 | index = ( hal_get_cycles() + hal_get_gid() ) % (x_size * y_size); |
---|
| 230 | x = index / y_size; |
---|
| 231 | y = index % y_size; |
---|
| 232 | } while ( cluster_info_is_active( LOCAL_CLUSTER->cluster_info[x][y] ) == 0 ); |
---|
| 233 | |
---|
| 234 | return (x<<y_width) + y; |
---|
| 235 | } |
---|
| 236 | |
---|
[1] | 237 | //////////////////////////////////////// |
---|
| 238 | bool_t cluster_is_undefined( cxy_t cxy ) |
---|
| 239 | { |
---|
| 240 | cluster_t * cluster = LOCAL_CLUSTER; |
---|
| 241 | |
---|
| 242 | uint32_t y_width = cluster->y_width; |
---|
| 243 | |
---|
| 244 | uint32_t x = cxy >> y_width; |
---|
| 245 | uint32_t y = cxy & ((1<<y_width)-1); |
---|
| 246 | |
---|
[19] | 247 | if( x >= cluster->x_size ) return true; |
---|
| 248 | if( y >= cluster->y_size ) return true; |
---|
[1] | 249 | |
---|
| 250 | return false; |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | //////////////////////////////////////////////////////////////////////////////////// |
---|
| 254 | // Cores related functions |
---|
| 255 | //////////////////////////////////////////////////////////////////////////////////// |
---|
| 256 | |
---|
| 257 | ///////////////////////////////// |
---|
[485] | 258 | lid_t cluster_select_local_core( void ) |
---|
[1] | 259 | { |
---|
[440] | 260 | uint32_t min = 1000; |
---|
| 261 | lid_t sel = 0; |
---|
| 262 | uint32_t nthreads; |
---|
| 263 | lid_t lid; |
---|
| 264 | scheduler_t * sched; |
---|
[1] | 265 | |
---|
| 266 | cluster_t * cluster = LOCAL_CLUSTER; |
---|
| 267 | |
---|
| 268 | for( lid = 0 ; lid < cluster->cores_nr ; lid++ ) |
---|
| 269 | { |
---|
[440] | 270 | sched = &cluster->core_tbl[lid].scheduler; |
---|
| 271 | nthreads = sched->u_threads_nr + sched->k_threads_nr; |
---|
| 272 | |
---|
| 273 | if( nthreads < min ) |
---|
[1] | 274 | { |
---|
[440] | 275 | min = nthreads; |
---|
[1] | 276 | sel = lid; |
---|
| 277 | } |
---|
[19] | 278 | } |
---|
[1] | 279 | return sel; |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | //////////////////////////////////////////////////////////////////////////////////// |
---|
[428] | 283 | // Process related functions |
---|
[1] | 284 | //////////////////////////////////////////////////////////////////////////////////// |
---|
| 285 | |
---|
[433] | 286 | |
---|
| 287 | ////////////////////////////////////////////////////// |
---|
[443] | 288 | xptr_t cluster_get_process_from_pid_in_cxy( cxy_t cxy, |
---|
| 289 | pid_t pid ) |
---|
| 290 | { |
---|
| 291 | xptr_t root_xp; // xptr on root of list of processes in owner cluster |
---|
| 292 | xptr_t lock_xp; // xptr on lock protecting this list |
---|
| 293 | xptr_t iter_xp; // iterator |
---|
| 294 | xptr_t current_xp; // xptr on current process descriptor |
---|
| 295 | bool_t found; |
---|
| 296 | |
---|
| 297 | cluster_t * cluster = LOCAL_CLUSTER; |
---|
| 298 | |
---|
| 299 | // get owner cluster and lpid |
---|
| 300 | cxy_t owner_cxy = CXY_FROM_PID( pid ); |
---|
| 301 | lpid_t lpid = LPID_FROM_PID( pid ); |
---|
| 302 | |
---|
| 303 | // get lock & root of list of copies from owner cluster |
---|
| 304 | root_xp = XPTR( owner_cxy , &cluster->pmgr.copies_root[lpid] ); |
---|
| 305 | lock_xp = XPTR( owner_cxy , &cluster->pmgr.copies_lock[lpid] ); |
---|
| 306 | |
---|
| 307 | // take the lock protecting the list of processes |
---|
| 308 | remote_spinlock_lock( lock_xp ); |
---|
| 309 | |
---|
| 310 | // scan list of processes |
---|
| 311 | found = false; |
---|
| 312 | XLIST_FOREACH( root_xp , iter_xp ) |
---|
| 313 | { |
---|
| 314 | current_xp = XLIST_ELEMENT( iter_xp , process_t , copies_list ); |
---|
| 315 | |
---|
| 316 | if( GET_CXY( current_xp ) == cxy ) |
---|
| 317 | { |
---|
| 318 | found = true; |
---|
| 319 | break; |
---|
| 320 | } |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | // release the lock protecting the list of processes |
---|
| 324 | remote_spinlock_unlock( lock_xp ); |
---|
| 325 | |
---|
| 326 | // return extended pointer on process descriptor in owner cluster |
---|
| 327 | if( found ) return current_xp; |
---|
| 328 | else return XPTR_NULL; |
---|
| 329 | |
---|
| 330 | } // end cluster_get_process_from_pid_in_cxy() |
---|
| 331 | |
---|
| 332 | |
---|
| 333 | ////////////////////////////////////////////////////// |
---|
[433] | 334 | xptr_t cluster_get_owner_process_from_pid( pid_t pid ) |
---|
| 335 | { |
---|
| 336 | xptr_t root_xp; // xptr on root of list of processes in owner cluster |
---|
[436] | 337 | xptr_t lock_xp; // xptr on lock protecting this list |
---|
[433] | 338 | xptr_t iter_xp; // iterator |
---|
| 339 | xptr_t current_xp; // xptr on current process descriptor |
---|
| 340 | process_t * current_ptr; // local pointer on current process |
---|
| 341 | pid_t current_pid; // current process identifier |
---|
| 342 | bool_t found; |
---|
| 343 | |
---|
| 344 | cluster_t * cluster = LOCAL_CLUSTER; |
---|
| 345 | |
---|
| 346 | // get owner cluster and lpid |
---|
| 347 | cxy_t owner_cxy = CXY_FROM_PID( pid ); |
---|
| 348 | |
---|
| 349 | // get lock & root of list of process in owner cluster |
---|
| 350 | root_xp = XPTR( owner_cxy , &cluster->pmgr.local_root ); |
---|
| 351 | lock_xp = XPTR( owner_cxy , &cluster->pmgr.local_lock ); |
---|
| 352 | |
---|
| 353 | // take the lock protecting the list of processes |
---|
| 354 | remote_spinlock_lock( lock_xp ); |
---|
| 355 | |
---|
| 356 | // scan list of processes in owner cluster |
---|
| 357 | found = false; |
---|
| 358 | XLIST_FOREACH( root_xp , iter_xp ) |
---|
| 359 | { |
---|
| 360 | current_xp = XLIST_ELEMENT( iter_xp , process_t , local_list ); |
---|
| 361 | current_ptr = GET_PTR( current_xp ); |
---|
| 362 | current_pid = hal_remote_lw( XPTR( owner_cxy , ¤t_ptr->pid ) ); |
---|
| 363 | |
---|
| 364 | if( current_pid == pid ) |
---|
| 365 | { |
---|
| 366 | found = true; |
---|
| 367 | break; |
---|
| 368 | } |
---|
| 369 | } |
---|
| 370 | |
---|
| 371 | // release the lock protecting the list of processes |
---|
| 372 | remote_spinlock_unlock( lock_xp ); |
---|
| 373 | |
---|
| 374 | // return extended pointer on process descriptor in owner cluster |
---|
| 375 | if( found ) return current_xp; |
---|
| 376 | else return XPTR_NULL; |
---|
| 377 | |
---|
[436] | 378 | } // end cluster_get_owner_process_from_pid() |
---|
| 379 | |
---|
[443] | 380 | |
---|
[1] | 381 | ////////////////////////////////////////////////////////// |
---|
| 382 | xptr_t cluster_get_reference_process_from_pid( pid_t pid ) |
---|
[19] | 383 | { |
---|
[23] | 384 | xptr_t ref_xp; // extended pointer on reference process descriptor |
---|
[1] | 385 | |
---|
| 386 | cluster_t * cluster = LOCAL_CLUSTER; |
---|
| 387 | |
---|
| 388 | // get owner cluster and lpid |
---|
| 389 | cxy_t owner_cxy = CXY_FROM_PID( pid ); |
---|
| 390 | lpid_t lpid = LPID_FROM_PID( pid ); |
---|
| 391 | |
---|
[19] | 392 | // Check valid PID |
---|
[23] | 393 | if( lpid >= CONFIG_MAX_PROCESS_PER_CLUSTER ) return XPTR_NULL; |
---|
[1] | 394 | |
---|
| 395 | if( local_cxy == owner_cxy ) // local cluster is owner cluster |
---|
[19] | 396 | { |
---|
[23] | 397 | ref_xp = cluster->pmgr.pref_tbl[lpid]; |
---|
[1] | 398 | } |
---|
| 399 | else // use a remote_lwd to access owner cluster |
---|
| 400 | { |
---|
[23] | 401 | ref_xp = (xptr_t)hal_remote_lwd( XPTR( owner_cxy , &cluster->pmgr.pref_tbl[lpid] ) ); |
---|
[1] | 402 | } |
---|
| 403 | |
---|
[23] | 404 | return ref_xp; |
---|
[1] | 405 | } |
---|
| 406 | |
---|
[416] | 407 | /////////////////////////////////////////////// |
---|
| 408 | error_t cluster_pid_alloc( process_t * process, |
---|
| 409 | pid_t * pid ) |
---|
[1] | 410 | { |
---|
| 411 | lpid_t lpid; |
---|
| 412 | bool_t found; |
---|
| 413 | |
---|
[440] | 414 | #if DEBUG_CLUSTER_PID_ALLOC |
---|
| 415 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 416 | if( DEBUG_CLUSTER_PID_ALLOC < cycle ) |
---|
| 417 | printk("\n[DBG] %s : thread %x enters in cluster %x / cycle %d\n", |
---|
| 418 | __FUNCTION__ , CURRENT_THREAD , local_cxy , cycle ); |
---|
| 419 | #endif |
---|
| 420 | |
---|
[1] | 421 | pmgr_t * pm = &LOCAL_CLUSTER->pmgr; |
---|
| 422 | |
---|
| 423 | // get the process manager lock |
---|
| 424 | spinlock_lock( &pm->pref_lock ); |
---|
| 425 | |
---|
| 426 | // search an empty slot |
---|
| 427 | found = false; |
---|
| 428 | for( lpid = 0 ; lpid < CONFIG_MAX_PROCESS_PER_CLUSTER ; lpid++ ) |
---|
| 429 | { |
---|
| 430 | if( pm->pref_tbl[lpid] == XPTR_NULL ) |
---|
| 431 | { |
---|
| 432 | found = true; |
---|
| 433 | break; |
---|
| 434 | } |
---|
| 435 | } |
---|
| 436 | |
---|
| 437 | if( found ) |
---|
| 438 | { |
---|
| 439 | // register process in pref_tbl[] |
---|
[416] | 440 | pm->pref_tbl[lpid] = XPTR( local_cxy , process ); |
---|
[1] | 441 | pm->pref_nr++; |
---|
| 442 | |
---|
| 443 | // returns pid |
---|
| 444 | *pid = PID( local_cxy , lpid ); |
---|
| 445 | |
---|
[416] | 446 | // release the processs_manager lock |
---|
| 447 | spinlock_unlock( &pm->pref_lock ); |
---|
| 448 | |
---|
| 449 | return 0; |
---|
[1] | 450 | } |
---|
| 451 | else |
---|
| 452 | { |
---|
[416] | 453 | // release the processs_manager lock |
---|
| 454 | spinlock_unlock( &pm->pref_lock ); |
---|
| 455 | |
---|
| 456 | return -1; |
---|
[19] | 457 | } |
---|
[1] | 458 | |
---|
[440] | 459 | #if DEBUG_CLUSTER_PID_ALLOC |
---|
| 460 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 461 | if( DEBUG_CLUSTER_PID_ALLOC < cycle ) |
---|
| 462 | printk("\n[DBG] %s : thread %x exit in cluster %x / pid %x / cycle %d\n", |
---|
| 463 | __FUNCTION__ , CURRENT_THREAD , local_cxy , *pid , cycle ); |
---|
| 464 | #endif |
---|
| 465 | |
---|
[1] | 466 | } // end cluster_pid_alloc() |
---|
| 467 | |
---|
| 468 | ///////////////////////////////////// |
---|
| 469 | void cluster_pid_release( pid_t pid ) |
---|
| 470 | { |
---|
[440] | 471 | |
---|
| 472 | #if DEBUG_CLUSTER_PID_RELEASE |
---|
| 473 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 474 | if( DEBUG_CLUSTER_PID_RELEASE < cycle ) |
---|
| 475 | printk("\n[DBG] %s : thread %x enters in cluster %x / pid %x / cycle %d\n", |
---|
| 476 | __FUNCTION__ , CURRENT_THREAD , local_cxy , pid , cycle ); |
---|
| 477 | #endif |
---|
| 478 | |
---|
[1] | 479 | cxy_t owner_cxy = CXY_FROM_PID( pid ); |
---|
| 480 | lpid_t lpid = LPID_FROM_PID( pid ); |
---|
| 481 | |
---|
[409] | 482 | pmgr_t * pm = &LOCAL_CLUSTER->pmgr; |
---|
| 483 | |
---|
[440] | 484 | // check lpid |
---|
[492] | 485 | assert( (lpid < CONFIG_MAX_PROCESS_PER_CLUSTER), |
---|
[440] | 486 | "illegal LPID = %d" , lpid ); |
---|
[1] | 487 | |
---|
[440] | 488 | // check owner cluster |
---|
[492] | 489 | assert( (owner_cxy == local_cxy) , |
---|
[440] | 490 | "local_cluster %x != owner_cluster %x" , local_cxy , owner_cxy ); |
---|
| 491 | |
---|
[1] | 492 | // get the process manager lock |
---|
| 493 | spinlock_lock( &pm->pref_lock ); |
---|
| 494 | |
---|
| 495 | // remove process from pref_tbl[] |
---|
| 496 | pm->pref_tbl[lpid] = XPTR_NULL; |
---|
| 497 | pm->pref_nr--; |
---|
| 498 | |
---|
| 499 | // release the processs_manager lock |
---|
| 500 | spinlock_unlock( &pm->pref_lock ); |
---|
| 501 | |
---|
[440] | 502 | #if DEBUG_CLUSTER_PID_RELEASE |
---|
| 503 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 504 | if( DEBUG_CLUSTER_PID_RELEASE < cycle ) |
---|
| 505 | printk("\n[DBG] %s : thread %x exit in cluster %x / cycle %d\n", |
---|
| 506 | __FUNCTION__ , CURRENT_THREAD , local_cxy , cycle ); |
---|
| 507 | #endif |
---|
| 508 | |
---|
[1] | 509 | } // end cluster_pid_release() |
---|
| 510 | |
---|
| 511 | /////////////////////////////////////////////////////////// |
---|
| 512 | process_t * cluster_get_local_process_from_pid( pid_t pid ) |
---|
| 513 | { |
---|
[23] | 514 | xptr_t process_xp; |
---|
| 515 | process_t * process_ptr; |
---|
| 516 | xptr_t root_xp; |
---|
| 517 | xptr_t iter_xp; |
---|
| 518 | bool_t found; |
---|
[19] | 519 | |
---|
[23] | 520 | found = false; |
---|
| 521 | root_xp = XPTR( local_cxy , &LOCAL_CLUSTER->pmgr.local_root ); |
---|
| 522 | |
---|
| 523 | XLIST_FOREACH( root_xp , iter_xp ) |
---|
[1] | 524 | { |
---|
[23] | 525 | process_xp = XLIST_ELEMENT( iter_xp , process_t , local_list ); |
---|
| 526 | process_ptr = (process_t *)GET_PTR( process_xp ); |
---|
| 527 | if( process_ptr->pid == pid ) |
---|
[1] | 528 | { |
---|
[23] | 529 | found = true; |
---|
[1] | 530 | break; |
---|
| 531 | } |
---|
| 532 | } |
---|
| 533 | |
---|
[23] | 534 | if (found ) return process_ptr; |
---|
| 535 | else return NULL; |
---|
| 536 | |
---|
[1] | 537 | } // end cluster_get_local_process_from_pid() |
---|
| 538 | |
---|
| 539 | ////////////////////////////////////////////////////// |
---|
| 540 | void cluster_process_local_link( process_t * process ) |
---|
| 541 | { |
---|
[443] | 542 | reg_t save_sr; |
---|
| 543 | |
---|
[1] | 544 | pmgr_t * pm = &LOCAL_CLUSTER->pmgr; |
---|
| 545 | |
---|
[443] | 546 | // get extended pointers on local process list root & lock |
---|
| 547 | xptr_t root_xp = XPTR( local_cxy , &pm->local_root ); |
---|
| 548 | xptr_t lock_xp = XPTR( local_cxy , &pm->local_lock ); |
---|
| 549 | |
---|
[1] | 550 | // get lock protecting the process manager local list |
---|
[443] | 551 | remote_spinlock_lock_busy( lock_xp , &save_sr ); |
---|
[1] | 552 | |
---|
[443] | 553 | // register process in local list |
---|
| 554 | xlist_add_last( root_xp , XPTR( local_cxy , &process->local_list ) ); |
---|
[1] | 555 | pm->local_nr++; |
---|
| 556 | |
---|
| 557 | // release lock protecting the process manager local list |
---|
[443] | 558 | remote_spinlock_unlock_busy( lock_xp , save_sr ); |
---|
[1] | 559 | } |
---|
| 560 | |
---|
| 561 | //////////////////////////////////////////////////////// |
---|
| 562 | void cluster_process_local_unlink( process_t * process ) |
---|
| 563 | { |
---|
[443] | 564 | reg_t save_sr; |
---|
| 565 | |
---|
[1] | 566 | pmgr_t * pm = &LOCAL_CLUSTER->pmgr; |
---|
| 567 | |
---|
[443] | 568 | // get extended pointers on local process list lock |
---|
| 569 | xptr_t lock_xp = XPTR( local_cxy , &pm->local_lock ); |
---|
| 570 | |
---|
[1] | 571 | // get lock protecting the process manager local list |
---|
[443] | 572 | remote_spinlock_lock_busy( lock_xp , &save_sr ); |
---|
[1] | 573 | |
---|
[443] | 574 | // remove process from local list |
---|
[23] | 575 | xlist_unlink( XPTR( local_cxy , &process->local_list ) ); |
---|
[1] | 576 | pm->local_nr--; |
---|
| 577 | |
---|
| 578 | // release lock protecting the process manager local list |
---|
[443] | 579 | remote_spinlock_unlock_busy( lock_xp , save_sr ); |
---|
[1] | 580 | } |
---|
| 581 | |
---|
| 582 | /////////////////////////////////////////////////////// |
---|
| 583 | void cluster_process_copies_link( process_t * process ) |
---|
| 584 | { |
---|
[436] | 585 | reg_t irq_state; |
---|
[1] | 586 | pmgr_t * pm = &LOCAL_CLUSTER->pmgr; |
---|
| 587 | |
---|
[438] | 588 | #if DEBUG_CLUSTER_PROCESS_COPIES |
---|
[436] | 589 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 590 | if( DEBUG_CLUSTER_PROCESS_COPIES < cycle ) |
---|
[436] | 591 | printk("\n[DBG] %s enters / cluster %x / process %x / cycle %d\n", |
---|
| 592 | __FUNCTION__ , local_cxy , process , cycle ); |
---|
| 593 | #endif |
---|
| 594 | |
---|
[1] | 595 | // get owner cluster identifier CXY and process LPID |
---|
| 596 | pid_t pid = process->pid; |
---|
| 597 | cxy_t owner_cxy = CXY_FROM_PID( pid ); |
---|
| 598 | lpid_t lpid = LPID_FROM_PID( pid ); |
---|
| 599 | |
---|
| 600 | // get extended pointer on lock protecting copies_list[lpid] |
---|
[120] | 601 | xptr_t copies_lock = XPTR( owner_cxy , &pm->copies_lock[lpid] ); |
---|
[1] | 602 | |
---|
| 603 | // get extended pointer on the copies_list[lpid] root |
---|
[120] | 604 | xptr_t copies_root = XPTR( owner_cxy , &pm->copies_root[lpid] ); |
---|
[1] | 605 | |
---|
| 606 | // get extended pointer on the local copies_list entry |
---|
| 607 | xptr_t copies_entry = XPTR( local_cxy , &process->copies_list ); |
---|
| 608 | |
---|
[19] | 609 | // get lock protecting copies_list[lpid] |
---|
[407] | 610 | remote_spinlock_lock_busy( copies_lock , &irq_state ); |
---|
[1] | 611 | |
---|
[436] | 612 | // add copy to copies_list |
---|
[1] | 613 | xlist_add_first( copies_root , copies_entry ); |
---|
| 614 | hal_remote_atomic_add( XPTR( owner_cxy , &pm->copies_nr[lpid] ) , 1 ); |
---|
| 615 | |
---|
[19] | 616 | // release lock protecting copies_list[lpid] |
---|
[407] | 617 | remote_spinlock_unlock_busy( copies_lock , irq_state ); |
---|
[1] | 618 | |
---|
[438] | 619 | #if DEBUG_CLUSTER_PROCESS_COPIES |
---|
[436] | 620 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 621 | if( DEBUG_CLUSTER_PROCESS_COPIES < cycle ) |
---|
[436] | 622 | printk("\n[DBG] %s exit / cluster %x / process %x / cycle %d\n", |
---|
| 623 | __FUNCTION__ , local_cxy , process , cycle ); |
---|
| 624 | #endif |
---|
| 625 | |
---|
| 626 | } // end cluster_process_copies_link() |
---|
| 627 | |
---|
[1] | 628 | ///////////////////////////////////////////////////////// |
---|
| 629 | void cluster_process_copies_unlink( process_t * process ) |
---|
| 630 | { |
---|
[407] | 631 | uint32_t irq_state; |
---|
[1] | 632 | pmgr_t * pm = &LOCAL_CLUSTER->pmgr; |
---|
| 633 | |
---|
[438] | 634 | #if DEBUG_CLUSTER_PROCESS_COPIES |
---|
[436] | 635 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 636 | if( DEBUG_CLUSTER_PROCESS_COPIES < cycle ) |
---|
[436] | 637 | printk("\n[DBG] %s enters / cluster %x / process %x / cycle %d\n", |
---|
| 638 | __FUNCTION__ , local_cxy , process , cycle ); |
---|
| 639 | #endif |
---|
| 640 | |
---|
[1] | 641 | // get owner cluster identifier CXY and process LPID |
---|
| 642 | pid_t pid = process->pid; |
---|
| 643 | cxy_t owner_cxy = CXY_FROM_PID( pid ); |
---|
| 644 | lpid_t lpid = LPID_FROM_PID( pid ); |
---|
| 645 | |
---|
| 646 | // get extended pointer on lock protecting copies_list[lpid] |
---|
[436] | 647 | xptr_t copies_lock = XPTR( owner_cxy , &pm->copies_lock[lpid] ); |
---|
[1] | 648 | |
---|
| 649 | // get extended pointer on the local copies_list entry |
---|
| 650 | xptr_t copies_entry = XPTR( local_cxy , &process->copies_list ); |
---|
| 651 | |
---|
[19] | 652 | // get lock protecting copies_list[lpid] |
---|
[407] | 653 | remote_spinlock_lock_busy( copies_lock , &irq_state ); |
---|
[1] | 654 | |
---|
[436] | 655 | // remove copy from copies_list |
---|
[1] | 656 | xlist_unlink( copies_entry ); |
---|
| 657 | hal_remote_atomic_add( XPTR( owner_cxy , &pm->copies_nr[lpid] ) , -1 ); |
---|
| 658 | |
---|
[19] | 659 | // release lock protecting copies_list[lpid] |
---|
[407] | 660 | remote_spinlock_unlock_busy( copies_lock , irq_state ); |
---|
[1] | 661 | |
---|
[438] | 662 | #if DEBUG_CLUSTER_PROCESS_COPIES |
---|
[436] | 663 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 664 | if( DEBUG_CLUSTER_PROCESS_COPIES < cycle ) |
---|
[436] | 665 | printk("\n[DBG] %s exit / cluster %x / process %x / cycle %d\n", |
---|
| 666 | __FUNCTION__ , local_cxy , process , cycle ); |
---|
| 667 | #endif |
---|
| 668 | |
---|
| 669 | } // end cluster_process_copies_unlink() |
---|
| 670 | |
---|
[428] | 671 | /////////////////////////////////////////// |
---|
| 672 | void cluster_processes_display( cxy_t cxy ) |
---|
[1] | 673 | { |
---|
[428] | 674 | xptr_t root_xp; |
---|
[443] | 675 | xptr_t lock_xp; |
---|
[428] | 676 | xptr_t iter_xp; |
---|
[443] | 677 | xptr_t process_xp; |
---|
| 678 | cxy_t txt0_cxy; |
---|
| 679 | chdev_t * txt0_ptr; |
---|
| 680 | xptr_t txt0_xp; |
---|
| 681 | xptr_t txt0_lock_xp; |
---|
| 682 | reg_t txt0_save_sr; // save SR to take TXT0 lock in busy mode |
---|
[1] | 683 | |
---|
[443] | 684 | assert( (cluster_is_undefined( cxy ) == false), |
---|
[492] | 685 | "illegal cluster index" ); |
---|
[443] | 686 | |
---|
| 687 | // get extended pointer on root and lock for local process list in cluster |
---|
[428] | 688 | root_xp = XPTR( cxy , &LOCAL_CLUSTER->pmgr.local_root ); |
---|
[443] | 689 | lock_xp = XPTR( cxy , &LOCAL_CLUSTER->pmgr.local_lock ); |
---|
[1] | 690 | |
---|
[443] | 691 | // get pointers on TXT0 chdev |
---|
| 692 | txt0_xp = chdev_dir.txt_tx[0]; |
---|
| 693 | txt0_cxy = GET_CXY( txt0_xp ); |
---|
| 694 | txt0_ptr = GET_PTR( txt0_xp ); |
---|
[1] | 695 | |
---|
[443] | 696 | // get extended pointer on TXT0 lock |
---|
| 697 | txt0_lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock ); |
---|
| 698 | |
---|
| 699 | // get lock on local process list |
---|
| 700 | remote_spinlock_lock( lock_xp ); |
---|
| 701 | |
---|
| 702 | // get TXT0 lock in busy waiting mode |
---|
| 703 | remote_spinlock_lock_busy( txt0_lock_xp , &txt0_save_sr ); |
---|
| 704 | |
---|
| 705 | // display header |
---|
| 706 | nolock_printk("\n***** processes in cluster %x / cycle %d\n", |
---|
| 707 | cxy , (uint32_t)hal_get_cycles() ); |
---|
| 708 | |
---|
| 709 | // loop on all processes in cluster cxy |
---|
[428] | 710 | XLIST_FOREACH( root_xp , iter_xp ) |
---|
| 711 | { |
---|
| 712 | process_xp = XLIST_ELEMENT( iter_xp , process_t , local_list ); |
---|
| 713 | process_display( process_xp ); |
---|
| 714 | } |
---|
[443] | 715 | |
---|
| 716 | // release TXT0 lock in busy waiting mode |
---|
| 717 | remote_spinlock_unlock_busy( txt0_lock_xp , txt0_save_sr ); |
---|
| 718 | |
---|
| 719 | // release lock on local process list |
---|
| 720 | remote_spinlock_unlock( lock_xp ); |
---|
| 721 | |
---|
[428] | 722 | } // end cluster_processes_display() |
---|
[1] | 723 | |
---|
[19] | 724 | |
---|