nr-gnb.c 40.9 KB
Newer Older
laurent's avatar
laurent committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

/*! \file lte-enb.c
 * \brief Top-level threads for gNodeB
 * \author R. Knopp, F. Kaltenberger, Navid Nikaein
 * \date 2012
 * \version 0.1
 * \company Eurecom
 * \email: knopp@eurecom.fr,florian.kaltenberger@eurecom.fr, navid.nikaein@eurecom.fr
 * \note
 * \warning
 */

#define _GNU_SOURCE
#include <pthread.h>

#undef MALLOC //there are two conflicting definitions, so we better make sure we don't use it at all

#include "assertions.h"
Laurent's avatar
Laurent committed
39 40
#include <common/utils/LOG/log.h>
#include <common/utils/system.h>
laurent's avatar
laurent committed
41 42 43 44 45 46 47 48 49

#include "PHY/types.h"

#include "PHY/INIT/phy_init.h"

#include "PHY/defs_gNB.h"
#include "SCHED/sched_eNB.h"
#include "SCHED_NR/sched_nr.h"
#include "SCHED_NR/fapi_nr_l1.h"
50
#include "PHY/NR_TRANSPORT/nr_transport_proto.h"
Mahesh's avatar
Mahesh committed
51
#include "PHY/MODULATION/nr_modulation.h"
laurent's avatar
laurent committed
52 53 54 55 56 57 58 59 60 61 62 63 64

#undef MALLOC //there are two conflicting definitions, so we better make sure we don't use it at all
//#undef FRAME_LENGTH_COMPLEX_SAMPLES //there are two conflicting definitions, so we better make sure we don't use it at all

#include "../../ARCH/COMMON/common_lib.h"

//#undef FRAME_LENGTH_COMPLEX_SAMPLES //there are two conflicting definitions, so we better make sure we don't use it at all

#include "PHY/LTE_TRANSPORT/if4_tools.h"
#include "PHY/LTE_TRANSPORT/if5_tools.h"

#include "PHY/phy_extern.h"

65
#include "LAYER2/NR_MAC_COMMON/nr_mac_extern.h"
laurent's avatar
laurent committed
66 67 68 69 70 71 72 73 74
#include "RRC/LTE/rrc_extern.h"
#include "PHY_INTERFACE/phy_interface.h"
#include "common/utils/LOG/log_extern.h"
#include "UTIL/OTG/otg_tx.h"
#include "UTIL/OTG/otg_externs.h"
#include "UTIL/MATH/oml.h"
#include "common/utils/LOG/vcd_signal_dumper.h"
#include "UTIL/OPT/opt.h"
#include "enb_config.h"
75
#include "gnb_paramdef.h"
laurent's avatar
laurent committed
76 77 78 79 80 81


#ifndef OPENAIR2
  #include "UTIL/OTG/otg_extern.h"
#endif

82 83 84
#include "s1ap_eNB.h"
#include "SIMULATION/ETH_TRANSPORT/proto.h"

laurent's avatar
laurent committed
85 86

#include "T.h"
Mahesh's avatar
Mahesh committed
87
#include "nfapi/oai_integration/vendor_ext.h"
laurent's avatar
laurent committed
88 89 90 91 92 93 94
//#define DEBUG_THREADS 1

//#define USRP_DEBUG 1
// Fix per CC openair rf/if device update
// extern openair0_device openair0;


95 96
extern volatile int start_gNB;
extern volatile int start_UE;
97
extern volatile int oai_exit;
laurent's avatar
laurent committed
98 99 100 101 102

extern openair0_config_t openair0_cfg[MAX_CARDS];

extern int transmission_mode;

103 104
extern uint16_t sf_ahead;
extern uint16_t sl_ahead;
105

laurent's avatar
laurent committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
//pthread_t                       main_gNB_thread;

time_stats_t softmodem_stats_mt; // main thread
time_stats_t softmodem_stats_hw; //  hw acquisition
time_stats_t softmodem_stats_rxtx_sf; // total tx time
time_stats_t nfapi_meas; // total tx time
time_stats_t softmodem_stats_rx_sf; // total rx time

/* mutex, cond and variable to serialize phy proc TX calls
 * (this mechanism may be relaxed in the future for better
 * performances)
 */
static struct {
  pthread_mutex_t  mutex_phy_proc_tx;
  pthread_cond_t   cond_phy_proc_tx;
  volatile uint8_t phy_proc_CC_id;
} sync_phy_proc;

extern double cpuf;

void init_gNB(int,int);
void stop_gNB(int nb_inst);

129 130 131 132 133
int wakeup_txfh(PHY_VARS_gNB *gNB, gNB_L1_rxtx_proc_t *proc, int frame_tx, int slot_tx, uint64_t timestamp_tx);
int wakeup_tx(PHY_VARS_gNB *gNB, int frame_rx, int slot_rx, int frame_tx, int slot_tx, uint64_t timestamp_tx);
#include "executables/thread-common.h"
//extern PARALLEL_CONF_t get_thread_parallel_conf(void);
//extern WORKER_CONF_t   get_thread_worker_conf(void);
laurent's avatar
laurent committed
134 135


136
void wakeup_prach_gNB(PHY_VARS_gNB *gNB, RU_t *ru, int frame, int subframe);
laurent's avatar
laurent committed
137 138 139

extern uint8_t nfapi_mode;
extern void oai_subframe_ind(uint16_t sfn, uint16_t sf);
MaheshK1995's avatar
MaheshK1995 committed
140
extern void oai_slot_ind(uint16_t sfn, uint16_t slot);
laurent's avatar
laurent committed
141 142 143 144 145
extern void add_subframe(uint16_t *frameP, uint16_t *subframeP, int offset);

//#define TICK_TO_US(ts) (ts.diff)
#define TICK_TO_US(ts) (ts.trials==0?0:ts.diff/ts.trials)

146
static inline int rxtx(PHY_VARS_gNB *gNB, int frame_rx, int slot_rx, int frame_tx, int slot_tx, char *thread_name) {
Mahesh's avatar
Mahesh committed
147 148 149
struct timespec current;
clock_gettime(CLOCK_MONOTONIC, &current);
//LOG_I(PHY,"%sCurrent time %d.%d,frame_rx %d,slot_rx %d,frame_tx %d,slot_tx %d\n", __FUNCTION__, current.tv_sec,current.tv_nsec,frame_rx,slot_rx,frame_tx,slot_tx);
Mahesh's avatar
Mahesh committed
150 151
  // if(nfapi_mode!=0)
  // sf_ahead = (uint16_t) ceil((float)6/(0x01<<gNB->frame_parms.numerology_index)); 
152

153
  sl_ahead = sf_ahead*gNB->frame_parms.slots_per_subframe;
154
  nfapi_nr_config_request_scf_t *cfg = &gNB->gNB_config;
Mahesh's avatar
Mahesh committed
155
  
laurent's avatar
laurent committed
156 157 158
  start_meas(&softmodem_stats_rxtx_sf);

  // *******************************************************************
159
  // NFAPI not yet supported for NR - this code has to be revised
Mahesh's avatar
Mahesh committed
160
  if (nfapi_mode == NFAPI_MODE_PNF) {
laurent's avatar
laurent committed
161 162 163 164 165
    // I am a PNF and I need to let nFAPI know that we have a (sub)frame tick
    //add_subframe(&frame, &subframe, 4);
    //oai_subframe_ind(proc->frame_tx, proc->subframe_tx);
    //LOG_D(PHY, "oai_subframe_ind(frame:%u, subframe:%d) - NOT CALLED ********\n", frame, subframe);
    start_meas(&nfapi_meas);
MaheshK1995's avatar
MaheshK1995 committed
166
    // oai_subframe_ind(frame_rx, slot_rx);
Mahesh's avatar
Mahesh committed
167

MaheshK1995's avatar
MaheshK1995 committed
168
    oai_slot_ind(frame_rx, slot_rx);
laurent's avatar
laurent committed
169 170
    stop_meas(&nfapi_meas);

171
    /*if (gNB->UL_INFO.rx_ind.rx_indication_body.number_of_pdus||
laurent's avatar
laurent committed
172 173
        gNB->UL_INFO.harq_ind.harq_indication_body.number_of_harqs ||
        gNB->UL_INFO.crc_ind.crc_indication_body.number_of_crcs ||
174
        gNB->UL_INFO.rach_ind.number_of_pdus ||
laurent's avatar
laurent committed
175 176
        gNB->UL_INFO.cqi_ind.number_of_cqis
       ) {
177
      LOG_D(PHY, "UL_info[rx_ind:%05d:%d harqs:%05d:%d crcs:%05d:%d rach_pdus:%0d.%d:%d cqis:%d] RX:%04d%d TX:%04d%d \n",
laurent's avatar
laurent committed
178 179 180
            NFAPI_SFNSF2DEC(gNB->UL_INFO.rx_ind.sfn_sf),   gNB->UL_INFO.rx_ind.rx_indication_body.number_of_pdus,
            NFAPI_SFNSF2DEC(gNB->UL_INFO.harq_ind.sfn_sf), gNB->UL_INFO.harq_ind.harq_indication_body.number_of_harqs,
            NFAPI_SFNSF2DEC(gNB->UL_INFO.crc_ind.sfn_sf),  gNB->UL_INFO.crc_ind.crc_indication_body.number_of_crcs,
181
            gNB->UL_INFO.rach_ind.sfn, gNB->UL_INFO.rach_ind.slot,gNB->UL_INFO.rach_ind.number_of_pdus,
laurent's avatar
laurent committed
182
            gNB->UL_INFO.cqi_ind.number_of_cqis,
183 184
            frame_rx, slot_rx,
            frame_tx, slot_tx);
185
    }*/
laurent's avatar
laurent committed
186 187
  }
  // ****************************************
188

189
  T(T_GNB_PHY_DL_TICK, T_INT(gNB->Mod_id), T_INT(frame_tx), T_INT(slot_tx));
190

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
  /* hack to remove UEs */
  extern int rnti_to_remove[10];
  extern volatile int rnti_to_remove_count;
  extern pthread_mutex_t rnti_to_remove_mutex;
  if (pthread_mutex_lock(&rnti_to_remove_mutex)) exit(1);
  int up_removed = 0;
  int down_removed = 0;
  int pucch_removed = 0;
  for (int i = 0; i < rnti_to_remove_count; i++) {
    LOG_W(PHY, "to remove rnti %d\n", rnti_to_remove[i]);
    void clean_gNB_ulsch(NR_gNB_ULSCH_t *ulsch);
    void clean_gNB_dlsch(NR_gNB_DLSCH_t *dlsch);
    int j;
    for (j = 0; j < NUMBER_OF_NR_ULSCH_MAX; j++)
      if (gNB->ulsch[j][0]->rnti == rnti_to_remove[i]) {
        gNB->ulsch[j][0]->rnti = 0;
        gNB->ulsch[j][0]->harq_mask = 0;
        //clean_gNB_ulsch(gNB->ulsch[j][0]);
        int h;
        for (h = 0; h < NR_MAX_ULSCH_HARQ_PROCESSES; h++) {
          gNB->ulsch[j][0]->harq_processes[h]->status = SCH_IDLE;
          gNB->ulsch[j][0]->harq_processes[h]->round  = 0;
          gNB->ulsch[j][0]->harq_processes[h]->handled = 0;
        }
        up_removed++;
      }
    for (j = 0; j < NUMBER_OF_NR_DLSCH_MAX; j++)
      if (gNB->dlsch[j][0]->rnti == rnti_to_remove[i]) {
        gNB->dlsch[j][0]->rnti = 0;
        gNB->dlsch[j][0]->harq_mask = 0;
        //clean_gNB_dlsch(gNB->dlsch[j][0]);
        down_removed++;
      }
    for (j = 0; j < NUMBER_OF_NR_PUCCH_MAX; j++)
      if (gNB->pucch[j]->active > 0 &&
          gNB->pucch[j]->pucch_pdu.rnti == rnti_to_remove[i]) {
        gNB->pucch[j]->active = 0;
        gNB->pucch[j]->pucch_pdu.rnti = 0;
        pucch_removed++;
      }
#if 0
    for (j = 0; j < NUMBER_OF_NR_PDCCH_MAX; j++)
      gNB->pdcch_pdu[j].frame = -1;
    for (j = 0; j < NUMBER_OF_NR_PDCCH_MAX; j++)
      gNB->ul_pdcch_pdu[j].frame = -1;
    for (j = 0; j < NUMBER_OF_NR_PRACH_MAX; j++)
      gNB->prach_vars.list[j].frame = -1;
#endif
  }
  if (rnti_to_remove_count) LOG_W(PHY, "to remove rnti_to_remove_count=%d, up_removed=%d down_removed=%d pucch_removed=%d\n", rnti_to_remove_count, up_removed, down_removed, pucch_removed);
  rnti_to_remove_count = 0;
  if (pthread_mutex_unlock(&rnti_to_remove_mutex)) exit(1);

244
  /*
245 246 247 248
  // if this is IF5 or 3GPP_gNB
  if (gNB && gNB->RU_list && gNB->RU_list[0] && gNB->RU_list[0]->function < NGFI_RAU_IF4p5) {
    wakeup_prach_gNB(gNB,NULL,proc->frame_rx,proc->slot_rx);
  }
249 250
  */
  // Call the scheduler
Khalid Ahmed's avatar
Khalid Ahmed committed
251

laurent's avatar
laurent committed
252
  pthread_mutex_lock(&gNB->UL_INFO_mutex);
253 254
  gNB->UL_INFO.frame     = frame_rx;
  gNB->UL_INFO.slot      = slot_rx;
laurent's avatar
laurent committed
255 256 257 258
  gNB->UL_INFO.module_id = gNB->Mod_id;
  gNB->UL_INFO.CC_id     = gNB->CC_id;
  gNB->if_inst->NR_UL_indication(&gNB->UL_INFO);
  pthread_mutex_unlock(&gNB->UL_INFO_mutex);
259 260 261 262 263 264 265 266
  
  // RX processing
  int tx_slot_type         = nr_slot_select(cfg,frame_tx,slot_tx);
  int rx_slot_type         = nr_slot_select(cfg,frame_rx,slot_rx);

  if (rx_slot_type == NR_UPLINK_SLOT || rx_slot_type == NR_MIXED_SLOT) {
    // UE-specific RX processing for subframe n
    // TODO: check if this is correct for PARALLEL_RU_L1_TRX_SPLIT
267 268

    // Do PRACH RU processing
269
    L1_nr_prach_procedures(gNB,frame_rx,slot_rx);
270

271 272 273 274 275 276 277 278
    //apply the rx signal rotation here
    apply_nr_rotation_ul(&gNB->frame_parms,
			 gNB->common_vars.rxdataF[0],
			 slot_rx,
			 0,
			 gNB->frame_parms.Ncp==EXTENDED?12:14,
			 gNB->frame_parms.ofdm_symbol_size);
    
279 280 281 282
    phy_procedures_gNB_uespec_RX(gNB, frame_rx, slot_rx);
  }

  if (oai_exit) return(-1);
laurent's avatar
laurent committed
283 284

  // *****************************************
285
  // TX processing for subframe n+sf_ahead
laurent's avatar
laurent committed
286 287 288 289
  // run PHY TX procedures the one after the other for all CCs to avoid race conditions
  // (may be relaxed in the future for performance reasons)
  // *****************************************

290
  if (tx_slot_type == NR_DOWNLINK_SLOT || tx_slot_type == NR_MIXED_SLOT) {
291

292 293 294
    if(get_thread_parallel_conf() != PARALLEL_RU_L1_TRX_SPLIT) {
      phy_procedures_gNB_TX(gNB, frame_tx,slot_tx, 1);
    }
laurent's avatar
laurent committed
295 296 297
  }

  stop_meas( &softmodem_stats_rxtx_sf );
298
  LOG_D(PHY,"%s() Exit proc[rx:%d%d tx:%d%d]\n", __FUNCTION__, frame_rx, slot_rx, frame_tx, slot_tx);
laurent's avatar
laurent committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
#if 0
  LOG_D(PHY, "rxtx:%lld nfapi:%lld phy:%lld tx:%lld rx:%lld prach:%lld ofdm:%lld ",
        softmodem_stats_rxtx_sf.diff_now, nfapi_meas.diff_now,
        TICK_TO_US(gNB->phy_proc),
        TICK_TO_US(gNB->phy_proc_tx),
        TICK_TO_US(gNB->phy_proc_rx),
        TICK_TO_US(gNB->rx_prach),
        TICK_TO_US(gNB->ofdm_mod_stats),
        softmodem_stats_rxtx_sf.diff_now, nfapi_meas.diff_now);
  LOG_D(PHY,
        "dlsch[enc:%lld mod:%lld scr:%lld rm:%lld t:%lld i:%lld] rx_dft:%lld ",
        TICK_TO_US(gNB->dlsch_encoding_stats),
        TICK_TO_US(gNB->dlsch_modulation_stats),
        TICK_TO_US(gNB->dlsch_scrambling_stats),
        TICK_TO_US(gNB->dlsch_rate_matching_stats),
        TICK_TO_US(gNB->dlsch_turbo_encoding_stats),
        TICK_TO_US(gNB->dlsch_interleaving_stats),
        TICK_TO_US(gNB->rx_dft_stats));
  LOG_D(PHY," ulsch[ch:%lld freq:%lld dec:%lld demod:%lld ru:%lld ",
        TICK_TO_US(gNB->ulsch_channel_estimation_stats),
        TICK_TO_US(gNB->ulsch_freq_offset_estimation_stats),
        TICK_TO_US(gNB->ulsch_decoding_stats),
        TICK_TO_US(gNB->ulsch_demodulation_stats),
        TICK_TO_US(gNB->ulsch_rate_unmatching_stats));
  LOG_D(PHY, "td:%lld dei:%lld dem:%lld llr:%lld tci:%lld ",
        TICK_TO_US(gNB->ulsch_turbo_decoding_stats),
        TICK_TO_US(gNB->ulsch_deinterleaving_stats),
        TICK_TO_US(gNB->ulsch_demultiplexing_stats),
        TICK_TO_US(gNB->ulsch_llr_stats),
        TICK_TO_US(gNB->ulsch_tc_init_stats));
  LOG_D(PHY, "tca:%lld tcb:%lld tcg:%lld tce:%lld l1:%lld l2:%lld]\n\n",
        TICK_TO_US(gNB->ulsch_tc_alpha_stats),
        TICK_TO_US(gNB->ulsch_tc_beta_stats),
        TICK_TO_US(gNB->ulsch_tc_gamma_stats),
        TICK_TO_US(gNB->ulsch_tc_ext_stats),
        TICK_TO_US(gNB->ulsch_tc_intl1_stats),
        TICK_TO_US(gNB->ulsch_tc_intl2_stats)
       );
#endif
  return(0);
}

static void *gNB_L1_thread_tx(void *param) {
  PHY_VARS_gNB *gNB        = (PHY_VARS_gNB *)param;
  gNB_L1_proc_t *gNB_proc  = &gNB->proc;
344
  gNB_L1_rxtx_proc_t *L1_proc_tx = &gNB_proc->L1_proc_tx;
laurent's avatar
laurent committed
345 346 347
  //PHY_VARS_gNB *gNB = RC.gNB[0][proc->CC_id];
  char thread_name[100];

348
  sprintf(thread_name,"gNB_L1_thread_tx\n");
laurent's avatar
laurent committed
349 350

  while (!oai_exit) {
351
    if (wait_on_condition(&L1_proc_tx->mutex,&L1_proc_tx->cond,&L1_proc_tx->instance_cnt,thread_name)<0) break;
laurent's avatar
laurent committed
352 353 354 355 356 357 358 359 360 361

    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_gNB_PROC_RXTX1, 1 );

    if (oai_exit) break;

    // *****************************************
    // TX processing for subframe n+4
    // run PHY TX procedures the one after the other for all CCs to avoid race conditions
    // (may be relaxed in the future for performance reasons)
    // *****************************************
362 363 364 365 366 367
    int      frame_tx = L1_proc_tx->frame_tx;
    int      slot_tx = L1_proc_tx->slot_tx;
    uint64_t timestamp_tx = L1_proc_tx->timestamp_tx;
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_SLOT_NUMBER_TX1_GNB,slot_tx);
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_TX1_GNB,frame_tx);
    phy_procedures_gNB_TX(gNB, frame_tx,slot_tx, 1);
368 369

    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_WAKEUP_TXFH, 1 );
370 371
    pthread_mutex_lock( &L1_proc_tx->mutex );
    L1_proc_tx->instance_cnt = -1;
laurent's avatar
laurent committed
372 373

    // the thread can now be woken up
374
    if (pthread_cond_signal(&L1_proc_tx->cond) != 0) {
laurent's avatar
laurent committed
375 376 377
      LOG_E( PHY, "[gNB] ERROR pthread_cond_signal for gNB TXnp4 thread\n");
      exit_fun( "ERROR pthread_cond_signal" );
    }
378
    pthread_mutex_unlock(&L1_proc_tx->mutex);
laurent's avatar
laurent committed
379

380
    wakeup_txfh(gNB,L1_proc_tx,frame_tx,slot_tx,timestamp_tx);
381
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_WAKEUP_TXFH, 0 );
382
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_gNB_PROC_RXTX1, 0 );
laurent's avatar
laurent committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
  }

  return 0;
}

/*!
 * \brief The RX UE-specific and TX thread of gNB.
 * \param param is a \ref gNB_L1_proc_t structure which contains the info what to process.
 * \returns a pointer to an int. The storage is not on the heap and must not be freed.
 */

static void *gNB_L1_thread( void *param ) {
  static int gNB_thread_rxtx_status;
  PHY_VARS_gNB *gNB        = (PHY_VARS_gNB *)param;
  gNB_L1_proc_t *gNB_proc  = &gNB->proc;
398
  gNB_L1_rxtx_proc_t *L1_proc = &gNB_proc->L1_proc;
laurent's avatar
laurent committed
399 400
  //PHY_VARS_gNB *gNB = RC.gNB[0][proc->CC_id];
  char thread_name[100];
laurent's avatar
laurent committed
401
  // set default return value
laurent's avatar
laurent committed
402 403
  // set default return value
  gNB_thread_rxtx_status = 0;
404 405 406

  sprintf(thread_name,"gNB_L1_thread");

laurent's avatar
laurent committed
407 408

  while (!oai_exit) {
Mahesh's avatar
Mahesh committed
409 410 411
    struct timespec t;
    clock_gettime(CLOCK_MONOTONIC,&t);
    //printf("\nbefore time %d.%d\n",t.tv_sec,t.tv_nsec);
laurent's avatar
laurent committed
412
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_gNB_PROC_RXTX0, 0 );
413
    if (wait_on_condition(&L1_proc->mutex,&L1_proc->cond,&L1_proc->instance_cnt,thread_name)<0) break;
laurent's avatar
laurent committed
414
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_gNB_PROC_RXTX0, 1 );
Mahesh's avatar
Mahesh committed
415
    clock_gettime(CLOCK_MONOTONIC,&t);
416

417 418 419 420 421
    int frame_rx          = L1_proc->frame_rx;
    int slot_rx           = L1_proc->slot_rx;
    int frame_tx          = L1_proc->frame_tx;
    int slot_tx           = L1_proc->slot_tx;
    uint64_t timestamp_tx = L1_proc->timestamp_tx;
Mahesh's avatar
Mahesh committed
422 423
    
    //printf("\nframe %d slot %d after wait time %d.%d\n",frame_rx,slot_rx,t.tv_sec,t.tv_nsec);
424 425


426 427 428 429
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_SLOT_NUMBER_TX0_GNB,slot_tx);
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_SLOT_NUMBER_RX0_GNB,slot_rx);
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_TX0_GNB,frame_tx);
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_RX0_GNB,frame_rx);
laurent's avatar
laurent committed
430 431 432 433

    if (oai_exit) break;

    if (gNB->CC_id==0) {
434
      if (rxtx(gNB,frame_rx,slot_rx,frame_tx,slot_tx,thread_name) < 0) break;
laurent's avatar
laurent committed
435 436
    }

437
    if (release_thread(&L1_proc->mutex,&L1_proc->instance_cnt,thread_name)<0) break;
laurent's avatar
laurent committed
438

439 440
    if(get_thread_parallel_conf() == PARALLEL_RU_L1_TRX_SPLIT)  wakeup_tx(gNB,frame_rx,slot_rx,frame_tx,slot_tx,timestamp_tx);
    else if(get_thread_parallel_conf() == PARALLEL_RU_L1_SPLIT) wakeup_txfh(gNB,L1_proc,frame_tx,slot_tx,timestamp_tx);
laurent's avatar
laurent committed
441 442 443 444 445 446 447 448
  } // while !oai_exit

  LOG_D(PHY, " *** Exiting gNB thread RXn_TXnp4\n");
  gNB_thread_rxtx_status = 0;
  return &gNB_thread_rxtx_status;
}


449
#if 0 
laurent's avatar
laurent committed
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
// Wait for gNB application initialization to be complete (gNB registration to MME)
static void wait_system_ready (char *message, volatile int *start_flag) {
  static char *indicator[] = {".    ", "..   ", "...  ", ".... ", ".....",
                              " ....", "  ...", "   ..", "    .", "     "
                             };
  int i = 0;

  while ((!oai_exit) && (*start_flag == 0)) {
    LOG_N(EMU, message, indicator[i]);
    fflush(stdout);
    i = (i + 1) % (sizeof(indicator) / sizeof(indicator[0]));
    usleep(200000);
  }

  LOG_D(EMU,"\n");
}
#endif





void gNB_top(PHY_VARS_gNB *gNB, int frame_rx, int slot_rx, char *string, struct RU_t_s *ru) {
  gNB_L1_proc_t *proc           = &gNB->proc;
  gNB_L1_rxtx_proc_t *L1_proc = &proc->L1_proc;
  NR_DL_FRAME_PARMS *fp = ru->nr_frame_parms;
  RU_proc_t *ru_proc=&ru->proc;
  proc->frame_rx    = frame_rx;
  proc->slot_rx = slot_rx;
479
  sl_ahead = sf_ahead*fp->slots_per_subframe;
laurent's avatar
laurent committed
480 481 482

  if (!oai_exit) {
    T(T_ENB_MASTER_TICK, T_INT(0), T_INT(proc->frame_rx), T_INT(proc->slot_rx));
483
    L1_proc->timestamp_tx = ru_proc->timestamp_rx + (sf_ahead*fp->samples_per_subframe);
laurent's avatar
laurent committed
484 485
    L1_proc->frame_rx     = ru_proc->frame_rx;
    L1_proc->slot_rx      = ru_proc->tti_rx;
486 487
    L1_proc->frame_tx     = (L1_proc->slot_rx > (fp->slots_per_frame-1-(fp->slots_per_subframe*sf_ahead))) ? (L1_proc->frame_rx+1)&1023 : L1_proc->frame_rx;
    L1_proc->slot_tx      = (L1_proc->slot_rx + (fp->slots_per_subframe*sf_ahead))%fp->slots_per_frame;
laurent's avatar
laurent committed
488

489
    if (rxtx(gNB,L1_proc->frame_rx,L1_proc->slot_rx,L1_proc->frame_tx,L1_proc->slot_tx,string) < 0) LOG_E(PHY,"gNB %d CC_id %d failed during execution\n",gNB->Mod_id,gNB->CC_id);
laurent's avatar
laurent committed
490 491 492 493 494 495 496

    ru_proc->timestamp_tx = L1_proc->timestamp_tx;
    ru_proc->tti_tx       = L1_proc->slot_tx;
    ru_proc->frame_tx     = L1_proc->frame_tx;
  }
}

497
int wakeup_txfh(PHY_VARS_gNB *gNB,gNB_L1_rxtx_proc_t *proc,int frame_tx,int slot_tx,uint64_t timestamp_tx) {
498

laurent's avatar
laurent committed
499 500
  RU_t *ru;
  RU_proc_t *ru_proc;
501

502 503 504
  int waitret = 0, ret = 0, time_ns = 1000*1000;
  struct timespec now, abstime;
  // note this should depend on the numerology used by the TX L1 thread, set here for 500us slot time
505
  // note this should depend on the numerology used by the TX L1 thread, set here for 500us slot time
506 507
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_GAIN_CONTROL,1);
  time_ns = time_ns/gNB->frame_parms.slots_per_subframe;
508 509
  AssertFatal((ret = pthread_mutex_lock(&proc->mutex_RUs_tx))==0,"mutex_lock returns %d\n",ret);
  while (proc->instance_cnt_RUs < 0) {
510 511 512 513 514 515 516 517 518
    clock_gettime(CLOCK_REALTIME, &now);
    abstime.tv_sec = now.tv_sec;
    abstime.tv_nsec = now.tv_nsec + time_ns;

    if (abstime.tv_nsec >= 1000*1000*1000) {
      abstime.tv_nsec -= 1000*1000*1000;
      abstime.tv_sec  += 1;
    }
    if((waitret = pthread_cond_timedwait(&proc->cond_RUs,&proc->mutex_RUs_tx,&abstime)) == 0) break; // this unlocks mutex_rxtx while waiting and then locks it again
519 520 521 522
  }
  proc->instance_cnt_RUs = -1;
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_RX0_UE,proc->instance_cnt_RUs);
  AssertFatal((ret = pthread_mutex_unlock(&proc->mutex_RUs_tx))==0,"mutex_unlock returns %d\n",ret);
523
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_GAIN_CONTROL,0);
laurent's avatar
laurent committed
524

525
  if (waitret == ETIMEDOUT) {
laurent's avatar
laurent committed
526
    LOG_W(PHY,"Dropping TX slot (%d.%d) because FH is blocked more than 1 slot times (500us)\n",frame_tx,slot_tx);
527

laurent's avatar
laurent committed
528 529 530 531 532 533 534
    AssertFatal((ret=pthread_mutex_lock(&gNB->proc.mutex_RU_tx))==0,"mutex_lock returns %d\n",ret);
    gNB->proc.RU_mask_tx = 0;
    AssertFatal((ret=pthread_mutex_unlock(&gNB->proc.mutex_RU_tx))==0,"mutex_unlock returns %d\n",ret);
    AssertFatal((ret=pthread_mutex_lock(&proc->mutex_RUs_tx))==0,"mutex_lock returns %d\n",ret);
    proc->instance_cnt_RUs = 0;
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_RX0_UE,proc->instance_cnt_RUs);
    AssertFatal((ret=pthread_mutex_unlock(&proc->mutex_RUs_tx))==0,"mutex_unlock returns %d\n",ret);
535

laurent's avatar
laurent committed
536 537
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_TX0_UE,1);
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_TX0_UE,0);
538

laurent's avatar
laurent committed
539 540 541
    return(-1);
  }

542 543
  for(int i=0; i<gNB->num_RU; i++)
  {
laurent's avatar
laurent committed
544 545 546
    ru      = gNB->RU_list[i];
    ru_proc = &ru->proc;

547

laurent's avatar
laurent committed
548
    if (ru_proc->instance_cnt_gNBs == 0) {
549
      VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_TRX_TST_UE, 1);
laurent's avatar
laurent committed
550
      LOG_E(PHY,"Frame %d, subframe %d: TX FH thread busy, dropping Frame %d, subframe %d\n", ru_proc->frame_tx, ru_proc->tti_tx, proc->frame_rx, proc->slot_rx);
551
      AssertFatal((ret=pthread_mutex_lock(&gNB->proc.mutex_RU_tx))==0,"mutex_lock returns %d\n",ret);
552
      gNB->proc.RU_mask_tx = 0;
553
      AssertFatal((ret=pthread_mutex_unlock(&gNB->proc.mutex_RU_tx))==0,"mutex_unlock returns %d\n",ret);
554

555
      VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_TRX_TST_UE, 0);
laurent's avatar
laurent committed
556 557
      return(-1);
    }
laurent's avatar
laurent committed
558
    AssertFatal((ret = pthread_mutex_lock(&ru_proc->mutex_gNBs))==0,"ERROR pthread_mutex_lock failed on mutex_gNBs L1_thread_tx with ret=%d\n",ret);
559

laurent's avatar
laurent committed
560
    ru_proc->instance_cnt_gNBs = 0;
561 562 563
    ru_proc->timestamp_tx = timestamp_tx;
    ru_proc->tti_tx       = slot_tx;
    ru_proc->frame_tx     = frame_tx;
564

565
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_RX1_UE, ru_proc->instance_cnt_gNBs);
566

laurent's avatar
laurent committed
567
    LOG_D(PHY,"Signaling tx_thread_fh for %d.%d\n",frame_tx,slot_tx);
laurent's avatar
laurent committed
568
    // the thread can now be woken up
569
    AssertFatal(pthread_cond_signal(&ru_proc->cond_gNBs) == 0,
laurent's avatar
laurent committed
570
                "[gNB] ERROR pthread_cond_signal for gNB TXnp4 thread\n");
571
    AssertFatal((ret=pthread_mutex_unlock(&ru_proc->mutex_gNBs))==0,"mutex_unlock returned %d\n",ret);
laurent's avatar
laurent committed
572

573
  }
laurent's avatar
laurent committed
574 575 576
  return(0);
}

577
int wakeup_tx(PHY_VARS_gNB *gNB,int frame_rx,int slot_rx,int frame_tx,int slot_tx,uint64_t timestamp_tx) {
578 579


580
  gNB_L1_rxtx_proc_t *L1_proc_tx = &gNB->proc.L1_proc_tx;
581

582
  int ret;
583 584
 
  
585
  AssertFatal((ret = pthread_mutex_lock(&L1_proc_tx->mutex))==0,"mutex_lock returns %d\n",ret);
laurent's avatar
laurent committed
586

587

laurent's avatar
laurent committed
588
  while(L1_proc_tx->instance_cnt == 0) {
laurent's avatar
laurent committed
589 590 591 592
    pthread_cond_wait(&L1_proc_tx->cond,&L1_proc_tx->mutex);
  }

  L1_proc_tx->instance_cnt = 0;
593 594

  
595 596 597 598 599
  L1_proc_tx->slot_rx       = slot_rx;
  L1_proc_tx->frame_rx      = frame_rx;
  L1_proc_tx->slot_tx       = slot_tx;
  L1_proc_tx->frame_tx      = frame_tx;
  L1_proc_tx->timestamp_tx  = timestamp_tx;
600 601

 
602 603
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_TX1_UE,1);
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_TX1_UE,0);
laurent's avatar
laurent committed
604
  // the thread can now be woken up
605
  // the thread can now be woken up
606
  AssertFatal(pthread_cond_signal(&L1_proc_tx->cond) == 0, "ERROR pthread_cond_signal for gNB L1 thread\n");
607
  
608
  AssertFatal((ret=pthread_mutex_unlock(&L1_proc_tx->mutex))==0,"mutex_unlock returns %d\n",ret);
609

laurent's avatar
laurent committed
610 611 612 613 614 615 616 617
  return(0);
}

int wakeup_rxtx(PHY_VARS_gNB *gNB,RU_t *ru) {
  gNB_L1_proc_t *proc=&gNB->proc;
  gNB_L1_rxtx_proc_t *L1_proc=&proc->L1_proc;
  NR_DL_FRAME_PARMS *fp = &gNB->frame_parms;
  RU_proc_t *ru_proc=&ru->proc;
618
  int ret;
laurent's avatar
laurent committed
619
  int i;
620
  struct timespec abstime;
621
  int time_ns = 50000;
622 623
  int wait_timer = 0;
  bool do_last_check = 1;
624
  
625
  AssertFatal((ret=pthread_mutex_lock(&proc->mutex_RU))==0,"mutex_lock returns %d\n",ret);
laurent's avatar
laurent committed
626
  for (i=0; i<gNB->num_RU; i++) {
laurent's avatar
laurent committed
627 628
    if (ru == gNB->RU_list[i]) {
      if ((proc->RU_mask&(1<<i)) > 0)
laurent's avatar
laurent committed
629 630
        LOG_E(PHY,"gNB %d frame %d, subframe %d : previous information from RU %d (num_RU %d,mask %x) has not been served yet!\n",
              gNB->Mod_id,proc->frame_rx,proc->slot_rx,ru->idx,gNB->num_RU,proc->RU_mask);
laurent's avatar
laurent committed
631 632 633 634 635
      proc->RU_mask |= (1<<i);
    }
  }
  if (proc->RU_mask != (1<<gNB->num_RU)-1) {  // not all RUs have provided their information so return
    LOG_E(PHY,"Not all RUs have provided their info\n");
636
    AssertFatal((ret=pthread_mutex_unlock(&proc->mutex_RU))==0,"mutex_unlock returns %d\n",ret);
laurent's avatar
laurent committed
637
    return(0);
638 639
  }
  else { // all RUs have provided their information so continue on and wakeup gNB processing
laurent's avatar
laurent committed
640
    proc->RU_mask = 0;
641
    AssertFatal((ret=pthread_mutex_unlock(&proc->mutex_RU))==0,"muex_unlock returns %d\n",ret);
laurent's avatar
laurent committed
642 643
  }

644
  // wake up TX for subframe n+sf_ahead
laurent's avatar
laurent committed
645
  // lock the TX mutex and make sure the thread is ready
646 647 648
  while (wait_timer < 200) {
    clock_gettime(CLOCK_REALTIME, &abstime);
    abstime.tv_nsec = abstime.tv_nsec + time_ns;
laurent's avatar
laurent committed
649

650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
    if (abstime.tv_nsec >= 1000*1000*1000) {
      abstime.tv_nsec -= 1000*1000*1000;
      abstime.tv_sec  += 1;
    }

    AssertFatal((ret=pthread_mutex_timedlock(&L1_proc->mutex, &abstime)) == 0,"mutex_lock returns %d\n", ret);

    if (L1_proc->instance_cnt == 0) { // L1_thread is busy so wait for a bit
      AssertFatal((ret=pthread_mutex_unlock( &L1_proc->mutex))==0,"muex_unlock return %d\n",ret);
      wait_timer += 50;
      usleep(50);
    }
    else {
      do_last_check = 0;
      break;
    }
  }
  if (do_last_check) {
    AssertFatal((ret=pthread_mutex_timedlock(&L1_proc->mutex, &abstime)) == 0,"mutex_lock returns %d\n", ret);
    if (L1_proc->instance_cnt == 0) { // L1_thread is busy so abort the subframe
      AssertFatal((ret=pthread_mutex_unlock( &L1_proc->mutex))==0,"muex_unlock return %d\n",ret);
      LOG_W(PHY,"L1_thread isn't ready in %d.%d, aborting RX processing\n",ru_proc->frame_rx,ru_proc->tti_rx);
      return (-1);
    }
674
  }
laurent's avatar
laurent committed
675

laurent's avatar
laurent committed
676
  ++L1_proc->instance_cnt;
laurent's avatar
laurent committed
677 678
  // We have just received and processed the common part of a subframe, say n.
  // TS_rx is the last received timestamp (start of 1st slot), TS_tx is the desired
laurent's avatar
laurent committed
679
  // transmitted timestamp of the next TX slot (first).
laurent's avatar
laurent committed
680

681
  // The last (TS_rx mod samples_per_frame) was n*samples_per_tti, 
682 683 684
  // we want to generate subframe (n+sf_ahead), so TS_tx = TX_rx+sf_ahead*samples_per_tti,
  // and proc->slot_tx = proc->slot_rx+sf_ahead
  L1_proc->timestamp_tx = ru_proc->timestamp_rx + (sf_ahead*fp->samples_per_subframe);
laurent's avatar
laurent committed
685 686
  L1_proc->frame_rx     = ru_proc->frame_rx;
  L1_proc->slot_rx  = ru_proc->tti_rx;
687 688
  L1_proc->frame_tx = (L1_proc->slot_rx > (fp->slots_per_frame-1-(fp->slots_per_subframe*sf_ahead))) ? (L1_proc->frame_rx+1)&1023 : L1_proc->frame_rx;
  L1_proc->slot_tx  = (L1_proc->slot_rx + (fp->slots_per_subframe*sf_ahead))%fp->slots_per_frame;
689

690
  LOG_D(PHY,"wakeupL1: passing parameter IC = %d, RX: %d.%d, TX: %d.%d to L1 sf_ahead = %d\n", L1_proc->instance_cnt, L1_proc->frame_rx, L1_proc->slot_rx, L1_proc->frame_tx, L1_proc->slot_tx, sf_ahead);
691

692
  pthread_mutex_unlock( &L1_proc->mutex );
laurent's avatar
laurent committed
693 694 695 696 697 698 699

  // the thread can now be woken up
  if (pthread_cond_signal(&L1_proc->cond) != 0) {
    LOG_E( PHY, "[gNB] ERROR pthread_cond_signal for gNB RXn-TXnp4 thread\n");
    exit_fun( "ERROR pthread_cond_signal" );
    return(-1);
  }
laurent's avatar
laurent committed
700

laurent's avatar
laurent committed
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
  return(0);
}
/*
void wakeup_prach_gNB(PHY_VARS_gNB *gNB,RU_t *ru,int frame,int subframe) {

  gNB_L1_proc_t *proc = &gNB->proc;
  LTE_DL_FRAME_PARMS *fp=&gNB->frame_parms;
  int i;

  if (ru!=NULL) {
    pthread_mutex_lock(&proc->mutex_RU_PRACH);
    for (i=0;i<gNB->num_RU;i++) {
      if (ru == gNB->RU_list[i]) {
  LOG_D(PHY,"frame %d, subframe %d: RU %d for gNB %d signals PRACH (mask %x, num_RU %d)\n",frame,subframe,i,gNB->Mod_id,proc->RU_mask_prach,gNB->num_RU);
  if ((proc->RU_mask_prach&(1<<i)) > 0)
    LOG_E(PHY,"gNB %d frame %d, subframe %d : previous information (PRACH) from RU %d (num_RU %d, mask %x) has not been served yet!\n",
    gNB->Mod_id,frame,subframe,ru->idx,gNB->num_RU,proc->RU_mask_prach);
  proc->RU_mask_prach |= (1<<i);
      }
    }
    if (proc->RU_mask_prach != (1<<gNB->num_RU)-1) {  // not all RUs have provided their information so return
      pthread_mutex_unlock(&proc->mutex_RU_PRACH);
      return;
    }
    else { // all RUs have provided their information so continue on and wakeup gNB processing
      proc->RU_mask_prach = 0;
      pthread_mutex_unlock(&proc->mutex_RU_PRACH);
    }
  }

  // check if we have to detect PRACH first
  if (is_prach_subframe(fp,frame,subframe)>0) {
    LOG_D(PHY,"Triggering prach processing, frame %d, subframe %d\n",frame,subframe);
    if (proc->instance_cnt_prach == 0) {
      LOG_W(PHY,"[gNB] Frame %d Subframe %d, dropping PRACH\n", frame,subframe);
      return;
    }

    // wake up thread for PRACH RX
    if (pthread_mutex_lock(&proc->mutex_prach) != 0) {
      LOG_E( PHY, "[gNB] ERROR pthread_mutex_lock for gNB PRACH thread %d (IC %d)\n", proc->thread_index, proc->instance_cnt_prach);
      exit_fun( "error locking mutex_prach" );
      return;
    }

    ++proc->instance_cnt_prach;
    // set timing for prach thread
    proc->frame_prach = frame;
    proc->subframe_prach = subframe;

    // the thread can now be woken up
    if (pthread_cond_signal(&proc->cond_prach) != 0) {
      LOG_E( PHY, "[gNB] ERROR pthread_cond_signal for gNB PRACH thread %d\n", proc->thread_index);
      exit_fun( "ERROR pthread_cond_signal" );
      return;
    }

    pthread_mutex_unlock( &proc->mutex_prach );
  }

}*/

/*!
 * \brief The prach receive thread of gNB.
 * \param param is a \ref gNB_L1_proc_t structure which contains the info what to process.
 * \returns a pointer to an int. The storage is not on the heap and must not be freed.
 */
/*
static void* gNB_thread_prach( void* param ) {
 static int gNB_thread_prach_status;


 PHY_VARS_gNB *gNB= (PHY_VARS_gNB *)param;
 gNB_L1_proc_t *proc = &gNB->proc;

 // set default return value
 gNB_thread_prach_status = 0;



 while (!oai_exit) {

   if (oai_exit) break;


   if (wait_on_condition(&proc->mutex_prach,&proc->cond_prach,&proc->instance_cnt_prach,"gNB_prach_thread") < 0) break;

   LOG_D(PHY,"Running gNB prach procedures\n");
789
   prach_procedures(gNB ,0);
laurent's avatar
laurent committed
790 791 792 793 794 795 796 797 798 799 800

   if (release_thread(&proc->mutex_prach,&proc->instance_cnt_prach,"gNB_prach_thread") < 0) break;
 }

 LOG_I(PHY, "Exiting gNB thread PRACH\n");

 gNB_thread_prach_status = 0;
 return &gNB_thread_prach_status;
}
*/

Laurent's avatar
Laurent committed
801 802
extern void init_td_thread(PHY_VARS_gNB *);
extern void init_te_thread(PHY_VARS_gNB *);
laurent's avatar
laurent committed
803

laurent's avatar
laurent committed
804
static void *process_stats_thread(void *param) {
805

laurent's avatar
laurent committed
806
  PHY_VARS_gNB *gNB  = (PHY_VARS_gNB *)param;
807

808 809 810
  reset_meas(&gNB->dlsch_encoding_stats);
  reset_meas(&gNB->dlsch_scrambling_stats);
  reset_meas(&gNB->dlsch_modulation_stats);
811

812 813
  wait_sync("process_stats_thread");

814 815
  while(!oai_exit)
  {
816 817 818 819 820 821 822 823 824
    sleep(1);
    print_meas(&gNB->dlsch_encoding_stats, "pdsch_encoding", NULL, NULL);
    print_meas(&gNB->dlsch_scrambling_stats, "pdsch_scrambling", NULL, NULL);
    print_meas(&gNB->dlsch_modulation_stats, "pdsch_modulation", NULL, NULL);
  }
  return(NULL);
}


laurent's avatar
laurent committed
825 826
void init_gNB_proc(int inst) {
  int i=0;
827
  int CC_id = 0;
laurent's avatar
laurent committed
828 829 830
  PHY_VARS_gNB *gNB;
  gNB_L1_proc_t *proc;
  gNB_L1_rxtx_proc_t *L1_proc,*L1_proc_tx;
831
//  LOG_I(PHY,"%s(inst:%d) RC.nb_nr_CC[inst]:%d \n",__FUNCTION__,inst,RC.nb_nr_CC[inst]);
832 833
  gNB = RC.gNB[inst];
  LOG_I(PHY,"Initializing gNB processes instance:%d CC_id %d \n",inst,CC_id);
834
  
835 836 837 838 839 840 841 842 843
  proc = &gNB->proc;
  L1_proc                        = &proc->L1_proc;
  L1_proc_tx                     = &proc->L1_proc_tx;
  L1_proc->instance_cnt          = -1;
  L1_proc_tx->instance_cnt       = -1;
  L1_proc->instance_cnt_RUs      = 0;
  L1_proc_tx->instance_cnt_RUs   = 0;
  proc->instance_cnt_prach       = -1;
  proc->instance_cnt_asynch_rxtx = -1;
844
  proc->CC_id                    = CC_id;
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
  proc->first_rx                 =1;
  proc->first_tx                 =1;
  proc->RU_mask                  =0;
  proc->RU_mask_tx               = (1<<gNB->num_RU)-1;
  proc->RU_mask_prach            =0;
  pthread_mutex_init( &gNB->UL_INFO_mutex, NULL);
  pthread_mutex_init( &L1_proc->mutex, NULL);
  pthread_mutex_init( &L1_proc_tx->mutex, NULL);
  pthread_cond_init( &L1_proc->cond, NULL);
  pthread_cond_init( &L1_proc_tx->cond, NULL);
  pthread_mutex_init( &proc->mutex_prach, NULL);
  pthread_mutex_init( &proc->mutex_asynch_rxtx, NULL);
  pthread_mutex_init( &proc->mutex_RU,NULL);
  pthread_mutex_init( &proc->mutex_RU_tx,NULL);
  pthread_mutex_init( &proc->mutex_RU_PRACH,NULL);
  pthread_cond_init( &proc->cond_prach, NULL);
  pthread_cond_init( &proc->cond_asynch_rxtx, NULL);
  LOG_I(PHY,"gNB->single_thread_flag:%d\n", gNB->single_thread_flag);
  
  if (get_thread_parallel_conf() == PARALLEL_RU_L1_SPLIT || get_thread_parallel_conf() == PARALLEL_RU_L1_TRX_SPLIT) {
    threadCreate( &L1_proc->pthread, gNB_L1_thread, gNB, "L1_proc", -1, OAI_PRIORITY_RT );
    threadCreate( &L1_proc_tx->pthread, gNB_L1_thread_tx, gNB,"L1_proc_tx", -1, OAI_PRIORITY_RT);
laurent's avatar
laurent committed
867
  }
868
  
869 870 871 872 873 874 875 876 877 878 879 880
  if(opp_enabled == 1) threadCreate(&proc->L1_stats_thread, process_stats_thread,(void *)gNB, "time_meas", -1, OAI_PRIORITY_RT_LOW);
  //pthread_create( &proc->pthread_prach, attr_prach, gNB_thread_prach, gNB );
  char name[16];
  
  if (gNB->single_thread_flag==0) {
    snprintf( name, sizeof(name), "L1 %d", i );
    pthread_setname_np( L1_proc->pthread, name );
    snprintf( name, sizeof(name), "L1TX %d", i );
    pthread_setname_np( L1_proc_tx->pthread, name );
  }
  
  AssertFatal(proc->instance_cnt_prach == -1,"instance_cnt_prach = %d\n",proc->instance_cnt_prach);
laurent's avatar
laurent committed
881 882 883 884 885

  /* setup PHY proc TX sync mechanism */
  pthread_mutex_init(&sync_phy_proc.mutex_phy_proc_tx, NULL);
  pthread_cond_init(&sync_phy_proc.cond_phy_proc_tx, NULL);
  sync_phy_proc.phy_proc_CC_id = 0;
Sakthivel Velumani's avatar
Sakthivel Velumani committed
886 887 888

  gNB->threadPool = (tpool_t*)malloc(sizeof(tpool_t));
  gNB->respDecode = (notifiedFIFO_t*) malloc(sizeof(notifiedFIFO_t));
889
  int numCPU = sysconf(_SC_NPROCESSORS_ONLN);
890 891 892
  uint32_t num_threads_pusch;
  paramdef_t PUSCHThreads[] = NUM_THREADS_DESC;
  config_get( PUSCHThreads,sizeof(PUSCHThreads)/sizeof(paramdef_t),NULL);
893
  int threadCnt = min(numCPU, num_threads_pusch);
894
  char ul_pool[80];
895
  sprintf(ul_pool,"-1");
896
  int s_offset = 0;
897
  for (int icpu=1; icpu<threadCnt; icpu++) {
898 899
    sprintf(ul_pool+2+s_offset,",-1");
    s_offset += 3;
900
  }
Sakthi's avatar
Sakthi committed
901
  initTpool(ul_pool, gNB->threadPool, false);
Sakthivel Velumani's avatar
Sakthivel Velumani committed
902
  initNotifiedFIFO(gNB->respDecode);
laurent's avatar
laurent committed
903 904 905 906 907 908 909 910 911 912 913 914 915
}



/*!
 * \brief Terminate gNB TX and RX threads.
 */
void kill_gNB_proc(int inst) {
  int *status;
  PHY_VARS_gNB *gNB;
  gNB_L1_proc_t *proc;
  gNB_L1_rxtx_proc_t *L1_proc, *L1_proc_tx;

916 917 918 919
  gNB=RC.gNB[inst];
  proc = &gNB->proc;
  L1_proc     = &proc->L1_proc;
  L1_proc_tx  = &proc->L1_proc_tx;
920
  LOG_I(PHY, "Killing TX inst %d\n",inst );
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
  
  if (get_thread_parallel_conf() == PARALLEL_RU_L1_SPLIT || get_thread_parallel_conf() == PARALLEL_RU_L1_TRX_SPLIT) {
    pthread_mutex_lock(&L1_proc->mutex);
    L1_proc->instance_cnt = 0;
    pthread_cond_signal(&L1_proc->cond);
    pthread_mutex_unlock(&L1_proc->mutex);
    pthread_mutex_lock(&L1_proc_tx->mutex);
    L1_proc_tx->instance_cnt = 0;
    pthread_cond_signal(&L1_proc_tx->cond);
    pthread_mutex_unlock(&L1_proc_tx->mutex);
  }
  
  proc->instance_cnt_prach = 0;
  pthread_cond_signal( &proc->cond_prach );
  pthread_cond_signal( &proc->cond_asynch_rxtx );
  pthread_cond_broadcast(&sync_phy_proc.cond_phy_proc_tx);
  //    LOG_D(PHY, "joining pthread_prach\n");
  //    pthread_join( proc->pthread_prach, (void**)&status );
  LOG_I(PHY, "Destroying prach mutex/cond\n");
  pthread_mutex_destroy( &proc->mutex_prach );
  pthread_cond_destroy( &proc->cond_prach );
  LOG_I(PHY, "Destroying UL_INFO mutex\n");
  pthread_mutex_destroy(&gNB->UL_INFO_mutex);
  
  if (get_thread_parallel_conf() == PARALLEL_RU_L1_SPLIT || get_thread_parallel_conf() == PARALLEL_RU_L1_TRX_SPLIT) {
    LOG_I(PHY, "Joining L1_proc mutex/cond\n");
    pthread_join( L1_proc->pthread, (void **)&status );
    LOG_I(PHY, "Joining L1_proc_tx mutex/cond\n");
    pthread_join( L1_proc_tx->pthread, (void **)&status );
laurent's avatar
laurent committed
950
  }
951 952 953 954 955 956 957 958 959 960
  
  LOG_I(PHY, "Destroying L1_proc mutex/cond\n");
  pthread_mutex_destroy( &L1_proc->mutex );
  pthread_cond_destroy( &L1_proc->cond );
  LOG_I(PHY, "Destroying L1_proc_tx mutex/cond\n");
  pthread_mutex_destroy( &L1_proc_tx->mutex );
  pthread_cond_destroy( &L1_proc_tx->cond );
  pthread_mutex_destroy( &proc->mutex_RU );
  pthread_mutex_destroy( &proc->mutex_RU_tx );
  
laurent's avatar
laurent committed
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
}




void reset_opp_meas(void) {
  int sfn;
  reset_meas(&softmodem_stats_mt);
  reset_meas(&softmodem_stats_hw);

  for (sfn=0; sfn < 10; sfn++) {
    reset_meas(&softmodem_stats_rxtx_sf);
    reset_meas(&softmodem_stats_rx_sf);
  }
}


void print_opp_meas(void) {
  int sfn=0;
  print_meas(&softmodem_stats_mt, "Main gNB Thread", NULL, NULL);
  print_meas(&softmodem_stats_hw, "HW Acquisation", NULL, NULL);

  for (sfn=0; sfn < 10; sfn++) {
    print_meas(&softmodem_stats_rxtx_sf,"[gNB][total_phy_proc_rxtx]",NULL, NULL);
    print_meas(&softmodem_stats_rx_sf,"[gNB][total_phy_proc_rx]",NULL,NULL);
  }
}


/// eNB kept in function name for nffapi calls, TO FIX
void init_eNB_afterRU(void) {
992
  int inst,ru_id,i,aa;
laurent's avatar
laurent committed
993 994 995 996
  PHY_VARS_gNB *gNB;
  LOG_I(PHY,"%s() RC.nb_nr_inst:%d\n", __FUNCTION__, RC.nb_nr_inst);

  for (inst=0; inst<RC.nb_nr_inst; inst++) {
997
    LOG_I(PHY,"RC.nb_nr_CC[inst:%d]:%p\n", inst, RC.gNB[inst]);
998 999
    gNB                                  =  RC.gNB[inst];
    phy_init_nr_gNB(gNB,0,0);
1000

1001 1002
    // map antennas and PRACH signals to gNB RX
    if (0) AssertFatal(gNB->num_RU>0,"Number of RU attached to gNB %d is zero\n",gNB->Mod_id);
1003

1004 1005
    LOG_I(PHY,"Mapping RX ports from %d RUs to gNB %d\n",gNB->num_RU,gNB->Mod_id);
    LOG_I(PHY,"gNB->num_RU:%d\n", gNB->num_RU);
1006

1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
    for (ru_id=0,aa=0; ru_id<gNB->num_RU; ru_id++) {
      AssertFatal(gNB->RU_list[ru_id]->common.rxdataF!=NULL,
		  "RU %d : common.rxdataF is NULL\n",
		  gNB->RU_list[ru_id]->idx);
      AssertFatal(gNB->RU_list[ru_id]->prach_rxsigF!=NULL,
		  "RU %d : prach_rxsigF is NULL\n",
		  gNB->RU_list[ru_id]->idx);
      
      for (i=0; i<gNB->RU_list[ru_id]->nb_rx; aa++,i++) {
	LOG_I(PHY,"Attaching RU %d antenna %d to gNB antenna %d\n",gNB->RU_list[ru_id]->idx,i,aa);
1017
	gNB->prach_vars.rxsigF[aa]    =  gNB->RU_list[ru_id]->prach_rxsigF[0][i];
1018
	gNB->common_vars.rxdataF[aa]     =  gNB->RU_list[ru_id]->common.rxdataF[i];
laurent's avatar
laurent committed
1019 1020 1021
      }
    }

1022 1023 1024 1025 1026
    /* TODO: review this code, there is something wrong.
     * In monolithic mode, we come here with nb_antennas_rx == 0
     * (not tested in other modes).
     */
    //init_precoding_weights(RC.gNB[inst]);
laurent's avatar
laurent committed
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
    init_gNB_proc(inst);
  }

  for (ru_id=0; ru_id<RC.nb_RU; ru_id++) {
    AssertFatal(RC.ru[ru_id]!=NULL,"ru_id %d is null\n",ru_id);
    RC.ru[ru_id]->nr_wakeup_rxtx         = wakeup_rxtx;
    //    RC.ru[ru_id]->wakeup_prach_eNB    = wakeup_prach_gNB;
    RC.ru[ru_id]->gNB_top             = gNB_top;
  }
}

void init_gNB(int single_thread_flag,int wait_for_sync) {
1039

laurent's avatar
laurent committed
1040 1041 1042
  int inst;
  PHY_VARS_gNB *gNB;

Raymond Knopp's avatar
Raymond Knopp committed
1043 1044 1045 1046 1047 1048 1049
  if (RC.gNB == NULL) {
    RC.gNB = (PHY_VARS_gNB **) malloc((1+RC.nb_nr_L1_inst)*sizeof(PHY_VARS_gNB *));
    for (inst=0; inst<RC.nb_nr_L1_inst; inst++) {
      RC.gNB[inst] = (PHY_VARS_gNB *) malloc(sizeof(PHY_VARS_gNB));
      memset((void*)RC.gNB[inst],0,sizeof(PHY_VARS_gNB));
    }
  }
laurent's avatar
laurent committed
1050

1051
  LOG_I(PHY,"gNB L1 structure RC.gNB allocated @ %p\n",RC.gNB);
laurent's avatar
laurent committed
1052 1053

  for (inst=0; inst<RC.nb_nr_L1_inst; inst++) {
Raymond Knopp's avatar
Raymond Knopp committed
1054

1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
    LOG_I(PHY,"[lte-softmodem.c] gNB structure RC.gNB[%d] allocated @ %p\n",inst,RC.gNB[inst]);
    gNB                     = RC.gNB[inst];
    gNB->abstraction_flag   = 0;
    gNB->single_thread_flag = single_thread_flag;
    /*nr_polar_init(&gNB->nrPolar_params,
      NR_POLAR_PBCH_MESSAGE_TYPE,
      NR_POLAR_PBCH_PAYLOAD_BITS,
      NR_POLAR_PBCH_AGGREGATION_LEVEL);*/
    LOG_I(PHY,"Initializing gNB %d single_thread_flag:%d\n",inst,gNB->single_thread_flag);
    LOG_I(PHY,"Initializing gNB %d\n",inst);
1065

1066 1067 1068 1069 1070 1071 1072
    LOG_I(PHY,"Registering with MAC interface module (before %p)\n",gNB->if_inst);
    AssertFatal((gNB->if_inst         = NR_IF_Module_init(inst))!=NULL,"Cannot register interface");
    LOG_I(PHY,"Registering with MAC interface module (after %p)\n",gNB->if_inst);
    gNB->if_inst->NR_Schedule_response   = nr_schedule_response;
    gNB->if_inst->NR_PHY_config_req      = nr_phy_config_request;
    memset((void *)&gNB->UL_INFO,0,sizeof(gNB->UL_INFO));
    LOG_I(PHY,"Setting indication lists\n");
1073

1074 1075 1076
    gNB->UL_INFO.rx_ind.pdu_list = gNB->rx_pdu_list;
    gNB->UL_INFO.crc_ind.crc_list = gNB->crc_pdu_list;
    /*gNB->UL_INFO.sr_ind.sr_indication_body.sr_pdu_list = gNB->sr_pdu_list;
1077 1078
    gNB->UL_INFO.harq_ind.harq_indication_body.harq_pdu_list = gNB->harq_pdu_list;
    gNB->UL_INFO.cqi_ind.cqi_pdu_list = gNB->cqi_pdu_list;
1079
    gNB->UL_INFO.cqi_ind.cqi_raw_pdu_list = gNB->cqi_raw_pdu_list;*/
1080

1081
    gNB->prach_energy_counter = 0;
laurent's avatar
laurent committed
1082
  }
1083
  
laurent's avatar
laurent committed
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094

  LOG_I(PHY,"[nr-softmodem.c] gNB structure allocated\n");
}


void stop_gNB(int nb_inst) {
  for (int inst=0; inst<nb_inst; inst++) {
    LOG_I(PHY,"Killing gNB %d processing threads\n",inst);
    kill_gNB_proc(inst);
  }
}