nr_dlsch_coding.c 14.6 KB
Newer Older
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
/*
 * 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.0  (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 PHY/LTE_TRANSPORT/dlsch_coding.c
* \brief Top-level routines for implementing LDPC-coded (DLSCH) transport channels from 38-212, 15.2
* \author H.Wang
* \date 2018
* \version 0.1
* \company Eurecom
* \email:
* \note
* \warning
*/

#include "PHY/defs_gNB.h"
#include "PHY/phy_extern.h"
#include "PHY/CODING/coding_extern.h"
#include "PHY/CODING/coding_defs.h"
#include "PHY/CODING/lte_interleaver_inline.h"
38
#include "PHY/CODING/nrLDPC_extern.h"
cig's avatar
cig committed
39
#include "PHY/NR_TRANSPORT/nr_transport_proto.h"
40
#include "PHY/NR_TRANSPORT/nr_transport_common_proto.h"
41
#include "PHY/NR_TRANSPORT/nr_dlsch.h"
42
#include "openair2/LAYER2/NR_MAC_gNB/mac_proto.h"
43
#include "SCHED_NR/sched_nr.h"
44 45
#include "common/utils/LOG/vcd_signal_dumper.h"
#include "common/utils/LOG/log.h"
46
#include <syscall.h>
47
#include <openair2/UTIL/OPT/opt.h>
48 49 50 51

//#define DEBUG_DLSCH_CODING
//#define DEBUG_DLSCH_FREE 1

52

cig's avatar
cig committed
53
void free_gNB_dlsch(NR_gNB_DLSCH_t **dlschptr, uint16_t N_RB)
54 55 56
{
  int r;

57
  NR_gNB_DLSCH_t *dlsch = *dlschptr;
58

59
  uint16_t a_segments = MAX_NUM_NR_DLSCH_SEGMENTS;  //number of segments to be allocated
60
  if (dlsch) {
61 62 63

    if (N_RB != 273) {
      a_segments = a_segments*N_RB;
64
      a_segments = a_segments/273 +1;
65
    }
66

67
#ifdef DEBUG_DLSCH_FREE
68
    LOG_D(PHY,"Freeing dlsch %p\n",dlsch);
69
#endif
70
    NR_DL_gNB_HARQ_t *harq = &dlsch->harq_process;
71
    if (harq->b) {
72
      free16(harq->b, a_segments * 1056);
73
      harq->b = NULL;
74
#ifdef DEBUG_DLSCH_FREE
75
      LOG_D(PHY, "Freeing harq->b (%p)\n", harq->b);
76 77
#endif

78
      if (harq->e) {
79 80
        free16(harq->e, 14 * N_RB * 12 * 8);
        harq->e = NULL;
81
#ifdef DEBUG_DLSCH_FREE
82
        printf("Freeing dlsch process %d e (%p)\n", i, harq->e);
83
#endif
84
      }
85

86
      if (harq->f) {
87 88
        free16(harq->f, 14 * N_RB * 12 * 8);
        harq->f = NULL;
89
#ifdef DEBUG_DLSCH_FREE
90
        printf("Freeing dlsch process %d f (%p)\n", i, harq->f);
91
#endif
92
      }
93

94
#ifdef DEBUG_DLSCH_FREE
95
      LOG_D(PHY, "Freeing dlsch process %d c (%p)\n", i, harq->c);
96 97
#endif

98
      for (r = 0; r < a_segments; r++) {
99
#ifdef DEBUG_DLSCH_FREE
100
        LOG_D(PHY, "Freeing dlsch process %d c[%d] (%p)\n", i, r, harq->c[r]);
101 102
#endif

103 104 105 106 107 108 109 110
        if (harq->c[r]) {
          free16(harq->c[r], 1056);
          harq->c[r] = NULL;
        }
        if (harq->d[r]) {
          free16(harq->d[r], 3 * 8448);
          harq->d[r] = NULL;
        }
111 112
      }
    }
yilmazt's avatar
yilmazt committed
113
  }
114

115 116
  free16(dlsch, sizeof(NR_gNB_DLSCH_t));
  *dlschptr = NULL;
117 118
}

119 120
NR_gNB_DLSCH_t *new_gNB_dlsch(NR_DL_FRAME_PARMS *frame_parms,
                              unsigned char Kmimo,
121 122
                              unsigned char Mdlharq,
                              uint32_t Nsoft,
123 124
                              uint8_t  abstraction_flag,
                              uint16_t N_RB)
125
{
126
  unsigned char i,r,aa,layer;
127
  int re;
128
  uint16_t a_segments = MAX_NUM_NR_DLSCH_SEGMENTS;  //number of segments to be allocated
129

130 131
  if (N_RB != 273) {
    a_segments = a_segments*N_RB;
132
    a_segments = a_segments/273 +1;
133
  }
134

135
  uint16_t dlsch_bytes = a_segments*1056;  // allocated bytes per segment
136

137 138
  NR_gNB_DLSCH_t *dlsch = malloc16(sizeof(NR_gNB_DLSCH_t));
  AssertFatal(dlsch, "cannot allocate dlsch\n");
139

140 141 142 143 144
  bzero(dlsch,sizeof(NR_gNB_DLSCH_t));
  dlsch->Kmimo = Kmimo;
  dlsch->Mdlharq = Mdlharq;
  dlsch->Mlimit = 4;
  dlsch->Nsoft = Nsoft;
145

146
  for (layer=0; layer<NR_MAX_NB_LAYERS; layer++) {
147
    dlsch->ue_spec_bf_weights[layer] = (int32_t**)malloc16(64*sizeof(int32_t*));
148

149 150 151
    for (aa=0; aa<64; aa++) {
      dlsch->ue_spec_bf_weights[layer][aa] = (int32_t *)malloc16(OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES*sizeof(int32_t));
      for (re=0;re<OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES; re++) {
152
        dlsch->ue_spec_bf_weights[layer][aa][re] = 0x00007fff;
153 154 155
      }
    }

156
    dlsch->txdataF[layer] = (int32_t *)malloc16((NR_MAX_PDSCH_ENCODED_LENGTH/NR_MAX_NB_LAYERS)*sizeof(int32_t)); // NR_MAX_NB_LAYERS is already included in NR_MAX_PDSCH_ENCODED_LENGTH
157
    dlsch->txdataF_precoding[layer] = (int32_t *)malloc16(2*14*frame_parms->ofdm_symbol_size*sizeof(int32_t));
158 159
  }

160 161
  for (int q=0; q<NR_MAX_NB_CODEWORDS; q++)
    dlsch->mod_symbs[q] = (int32_t *)malloc16(NR_MAX_PDSCH_ENCODED_LENGTH*sizeof(int32_t));
162

163 164 165
  dlsch->calib_dl_ch_estimates = (int32_t**)malloc16(64*sizeof(int32_t*));
  for (aa=0; aa<64; aa++) {
    dlsch->calib_dl_ch_estimates[aa] = (int32_t *)malloc16(OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES*sizeof(int32_t));
166
  }
167

168
  for (i=0; i<20; i++) {
169 170 171
    dlsch->harq_ids[0][i] = 0;
    dlsch->harq_ids[1][i] = 0;
  }
172

173
  NR_DL_gNB_HARQ_t *harq = &dlsch->harq_process;
174 175 176 177 178 179 180 181 182 183 184
  bzero(harq, sizeof(NR_DL_gNB_HARQ_t));

  harq->b = malloc16(dlsch_bytes);
  AssertFatal(harq->b, "cannot allocate memory for harq->b\n");
  harq->pdu = malloc16(dlsch_bytes);
  AssertFatal(harq->pdu, "cannot allocate memory for harq->pdu\n");
  bzero(harq->pdu, dlsch_bytes);
  nr_emulate_dlsch_payload(harq->pdu, (dlsch_bytes) >> 3);
  bzero(harq->b, dlsch_bytes);

  for (r = 0; r < a_segments; r++) {
185 186 187 188
    // account for filler in first segment and CRCs for multiple segment case
    // [hna] 8448 is the maximum CB size in NR
    //       68*348 = 68*(maximum size of Zc)
    //       In section 5.3.2 in 38.212, the for loop is up to N + 2*Zc (maximum size of N is 66*Zc, therefore 68*Zc)
189 190 191 192 193 194 195 196 197 198 199 200
    harq->c[r] = malloc16(8448);
    AssertFatal(harq->c[r], "cannot allocate harq->c[%d]\n", r);
    harq->d[r] = malloc16(68 * 384);
    AssertFatal(harq->d[r], "cannot allocate harq->d[%d]\n", r); // max size for coded output
    bzero(harq->c[r], 8448);
    bzero(harq->d[r], (3 * 8448));
    harq->e = malloc16(14 * N_RB * 12 * 8);
    AssertFatal(harq->e, "cannot allocate harq->e\n");
    bzero(harq->e, 14 * N_RB * 12 * 8);
    harq->f = malloc16(14 * N_RB * 12 * 8);
    AssertFatal(harq->f, "cannot allocate harq->f\n");
    bzero(harq->f, 14 * N_RB * 12 * 8);
201
  }
202

203
  return(dlsch);
204 205 206 207 208 209 210 211
}

void clean_gNB_dlsch(NR_gNB_DLSCH_t *dlsch)
{

  unsigned char Mdlharq;
  unsigned char i,j,r;

212 213 214 215 216
  AssertFatal(dlsch!=NULL,"dlsch is null\n");
  Mdlharq = dlsch->Mdlharq;
  dlsch->rnti = 0;
  dlsch->active = 0;
  NR_DL_gNB_HARQ_t *harq=&dlsch->harq_process;
217

218
  for (i=0; i<10; i++) {
219 220
    dlsch->harq_ids[0][i] = Mdlharq;
    dlsch->harq_ids[1][i] = Mdlharq;
221 222 223 224
  }
  for (i=0; i<Mdlharq; i++) {
    for (j=0; j<96; j++)
      for (r=0; r<MAX_NUM_NR_DLSCH_SEGMENTS; r++)
225 226
        if (harq->d[r])
          harq->d[r][j] = NR_NULL;
227 228 229
  }
}

230 231
int nr_dlsch_encoding(PHY_VARS_gNB *gNB,
		      unsigned char *a,
yilmazt's avatar
yilmazt committed
232 233 234
                      int frame,
                      uint8_t slot,
                      NR_gNB_DLSCH_t *dlsch,
235 236 237 238
                      NR_DL_FRAME_PARMS* frame_parms,
		      time_stats_t *tinput,time_stats_t *tprep,time_stats_t *tparity,time_stats_t *toutput,
		      time_stats_t *dlsch_rate_matching_stats,time_stats_t *dlsch_interleaving_stats,
		      time_stats_t *dlsch_segmentation_stats)
239 240 241 242
{

  unsigned int G;
  unsigned int crc=1;
243
  NR_DL_gNB_HARQ_t *harq = &dlsch->harq_process;
244
  nfapi_nr_dl_tti_pdsch_pdu_rel15_t *rel15 = &harq->pdsch_pdu.pdsch_pdu_rel15;
Raymond Knopp's avatar
Raymond Knopp committed
245 246
  uint16_t nb_rb = rel15->rbSize;
  uint8_t nb_symb_sch = rel15->NrOfSymbols;
Sakthivel Velumani's avatar
Sakthivel Velumani committed
247
  uint32_t A, Kb, F=0;
248
  uint32_t *Zc = &dlsch->harq_process.Z;
Raymond Knopp's avatar
Raymond Knopp committed
249
  uint8_t mod_order = rel15->qamModOrder[0];
Francesco Mani's avatar
Francesco Mani committed
250 251
  uint16_t Kr=0,r;
  uint32_t r_offset=0;
252
  uint32_t E;
253
  uint8_t Ilbrm = 1;
254
  uint32_t Tbslbrm = 950984; //max tbs
255
  uint8_t nb_re_dmrs;
256 257 258

  if (rel15->dmrsConfigType==NFAPI_NR_DMRS_TYPE1)
    nb_re_dmrs = 6*rel15->numDmrsCdmGrpsNoData;
259
  else
260 261
    nb_re_dmrs = 4*rel15->numDmrsCdmGrpsNoData;

Raymond Knopp's avatar
Raymond Knopp committed
262 263
  uint16_t length_dmrs = get_num_dmrs(rel15->dlDmrsSymbPos);
  uint16_t R=rel15->targetCodeRate[0];
264
  float Coderate = 0.0;
265
  uint8_t Nl = 4;
266

Rakesh's avatar
Rakesh committed
267
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_gNB_DLSCH_ENCODING, VCD_FUNCTION_IN);
268

Raymond Knopp's avatar
Raymond Knopp committed
269
  A = rel15->TBSize[0]<<3;
270 271
  if ( dlsch->rnti != SI_RNTI )
     trace_NRpdu(DIRECTION_DOWNLINK, a, rel15->TBSize[0], 0, WS_C_RNTI, dlsch->rnti, frame, slot,0, 0);
272

273 274 275 276 277 278 279 280 281 282 283
  NR_gNB_SCH_STATS_t *stats=NULL;
  int first_free=-1;
  for (int i=0;i<NUMBER_OF_NR_SCH_STATS_MAX;i++) {
    if (gNB->dlsch_stats[i].rnti == 0 && first_free == -1) {
      first_free = i;
      stats=&gNB->dlsch_stats[i];
    }
    if (gNB->dlsch_stats[i].rnti == dlsch->rnti) {
      stats=&gNB->dlsch_stats[i];
      break;
    }
284 285
  }

286 287
  if (stats) {
    stats->rnti = dlsch->rnti;
288 289 290
    stats->total_bytes_tx += rel15->TBSize[0];
    stats->current_RI   = rel15->nrOfLayers;
    stats->current_Qm   = rel15->qamModOrder[0];
291
  }
Raymond Knopp's avatar
Raymond Knopp committed
292
  G = nr_get_G(nb_rb, nb_symb_sch, nb_re_dmrs, length_dmrs,mod_order,rel15->nrOfLayers);
293

Laurent THOMAS's avatar
Laurent THOMAS committed
294
  LOG_D(PHY,"dlsch coding A %d G %d (nb_rb %d, nb_symb_sch %d, nb_re_dmrs %d, length_dmrs %d, mod_order %d)\n", A,G, nb_rb,nb_symb_sch,nb_re_dmrs,length_dmrs,(int)mod_order);
295

296 297 298 299 300 301 302 303
  if (A > 3824) {
    // Add 24-bit crc (polynomial A) to payload
    crc = crc24a(a,A)>>8;
    a[A>>3] = ((uint8_t*)&crc)[2];
    a[1+(A>>3)] = ((uint8_t*)&crc)[1];
    a[2+(A>>3)] = ((uint8_t*)&crc)[0];
    //printf("CRC %x (A %d)\n",crc,A);
    //printf("a0 %d a1 %d a2 %d\n", a[A>>3], a[1+(A>>3)], a[2+(A>>3)]);
304

305 306
    harq->B = A+24;
    //    harq->b = a;
307 308 309 310 311 312 313 314

    AssertFatal((A / 8) + 4 <= MAX_NR_DLSCH_PAYLOAD_BYTES,
                "A %d is too big (A/8+4 = %d > %d)\n",
                A,
                (A / 8) + 4,
                MAX_NR_DLSCH_PAYLOAD_BYTES);

    memcpy(harq->b, a, (A / 8) + 4); // why is this +4 if the CRC is only 3 bytes?
315 316 317 318 319 320 321 322
  }
  else {
    // Add 16-bit crc (polynomial A) to payload
    crc = crc16(a,A)>>16;
    a[A>>3] = ((uint8_t*)&crc)[1];
    a[1+(A>>3)] = ((uint8_t*)&crc)[0];
    //printf("CRC %x (A %d)\n",crc,A);
    //printf("a0 %d a1 %d \n", a[A>>3], a[1+(A>>3)]);
323

324 325
    harq->B = A+16;
    //    harq->b = a;
326 327 328 329 330 331 332 333

    AssertFatal((A / 8) + 3 <= MAX_NR_DLSCH_PAYLOAD_BYTES,
                "A %d is too big (A/8+3 = %d > %d)\n",
                A,
                (A / 8) + 3,
                MAX_NR_DLSCH_PAYLOAD_BYTES);

    memcpy(harq->b, a, (A / 8) + 3); // using 3 bytes to mimic the case of 24 bit crc
334 335 336 337 338
  }
  if (R<1000)
    Coderate = (float) R /(float) 1024;
  else  // to scale for mcs 20 and 26 in table 5.1.3.1-2 which are decimal and input 2* in nr_tbs_tools
    Coderate = (float) R /(float) 2048;
339

340 341 342 343
  if ((A <=292) || ((A<=3824) && (Coderate <= 0.6667)) || Coderate <= 0.25)
    harq->BG = 2;
  else
    harq->BG = 1;
344

345
  start_meas(dlsch_segmentation_stats);
346
  Kb = nr_segmentation(harq->b, harq->c, harq->B, &harq->C, &harq->K, Zc, &harq->F, harq->BG);
347 348
  stop_meas(dlsch_segmentation_stats);
  F = harq->F;
349

350
  Kr = harq->K;
351
#ifdef DEBUG_DLSCH_CODING
352 353
  uint16_t Kr_bytes;
  Kr_bytes = Kr>>3;
354
#endif
355

356
  //printf("segment Z %d k %d Kr %d BG %d C %d\n", *Zc,harq->K,Kr,BG,harq->C);
Hongzhi Wang's avatar
Hongzhi Wang committed
357

358 359 360
  for (r=0; r<harq->C; r++) {
    //d_tmp[r] = &harq->d[r][0];
    //channel_input[r] = &harq->d[r][0];
361
#ifdef DEBUG_DLSCH_CODING
362 363 364 365 366
    LOG_D(PHY,"Encoder: B %d F %d \n",harq->B, harq->F);
    LOG_D(PHY,"start ldpc encoder segment %d/%d\n",r,harq->C);
    LOG_D(PHY,"input %d %d %d %d %d \n", harq->c[r][0], harq->c[r][1], harq->c[r][2],harq->c[r][3], harq->c[r][4]);
    for (int cnt =0 ; cnt < 22*(*Zc)/8; cnt ++){
      LOG_D(PHY,"%d ", harq->c[r][cnt]);
367
    }
368
    LOG_D(PHY,"\n");
369

370
#endif
371 372
    //ldpc_encoder_orig((unsigned char*)harq->c[r],harq->d[r],*Zc,Kb,Kr,BG,0);
    //ldpc_encoder_optim((unsigned char*)harq->c[r],(unsigned char*)&harq->d[r][0],*Zc,Kb,Kr,BG,NULL,NULL,NULL,NULL);
373
  }
374 375 376 377 378 379
  encoder_implemparams_t impp;
  impp.n_segments=harq->C;
  impp.tprep = tprep;
  impp.tinput = tinput;
  impp.tparity = tparity;
  impp.toutput = toutput;
380

381 382 383 384
  for(int j=0;j<(harq->C/8+1);j++) {
    impp.macro_num=j;
    nrLDPC_encoder(harq->c,harq->d,*Zc,Kb,Kr,harq->BG,&impp);
  }
385

386
#ifdef DEBUG_DLSCH_CODING
387 388
  write_output("enc_input0.m","enc_in0",&harq->c[0][0],Kr_bytes,1,4);
  write_output("enc_output0.m","enc0",&harq->d[0][0],(3*8*Kr_bytes)+12,1,4);
389 390
#endif

391
  F = harq->F;
392

393 394
  Kr = harq->K;
  for (r=0; r<harq->C; r++) {
395

396
    if (F>0) {
Francesco Mani's avatar
Francesco Mani committed
397
      for (int k=(Kr-F-2*(*Zc)); k<Kr-2*(*Zc); k++) {
398
	// writing into positions d[r][k-2Zc] as in clause 5.3.2 step 2) in 38.212
399
        harq->d[r][k] = NR_NULL;
400
	//if (k<(Kr-F+8))
401
	//printf("r %d filler bits [%d] = %d \n", r,k, harq->d[r][k]);
402 403 404
      }
    }

405
#ifdef DEBUG_DLSCH_CODING
406
    LOG_D(PHY,"rvidx in encoding = %d\n", rel15->rvIndex[0]);
407 408
#endif

409
    E = nr_get_E(G, harq->C, mod_order, rel15->nrOfLayers, r);
410

411 412 413
    //#ifdef DEBUG_DLSCH_CODING
    LOG_D(PHY,"Rate Matching, Code segment %d/%d (coded bits (G) %u, E %d, Filler bits %d, Filler offset %d mod_order %d, nb_rb %d)...\n",
	  r,
414
	  harq->C,
415 416 417 418 419
	  G,
	  E,
	  F,
	  Kr-F-2*(*Zc),
	  mod_order,nb_rb);
420

421
    // for tbslbrm calculation according to 5.4.2.1 of 38.212
Raymond Knopp's avatar
Raymond Knopp committed
422 423
    if (rel15->nrOfLayers < Nl)
      Nl = rel15->nrOfLayers;
424

Sakthivel Velumani's avatar
Sakthivel Velumani committed
425
    Tbslbrm = nr_compute_tbslbrm(rel15->mcsTable[0],nb_rb,Nl);
426

427
    start_meas(dlsch_rate_matching_stats);
428 429
    nr_rate_matching_ldpc(Ilbrm,
                          Tbslbrm,
430
                          harq->BG,
Francesco Mani's avatar
Francesco Mani committed
431
                          *Zc,
432 433 434
                          harq->d[r],
                          harq->e+r_offset,
                          harq->C,
435 436
                          F,
                          Kr-F-2*(*Zc),
Raymond Knopp's avatar
Raymond Knopp committed
437
                          rel15->rvIndex[0],
438
                          E);
439
    stop_meas(dlsch_rate_matching_stats);
440 441
#ifdef DEBUG_DLSCH_CODING
    for (int i =0; i<16; i++)
442
      printf("output ratematching e[%d]= %d r_offset %u\n", i,harq->e[i+r_offset], r_offset);
443
#endif
444

445 446 447
    start_meas(dlsch_interleaving_stats);
    nr_interleaving_ldpc(E,
			 mod_order,
448 449
			 harq->e+r_offset,
			 harq->f+r_offset);
450
    stop_meas(dlsch_interleaving_stats);
451

452 453
#ifdef DEBUG_DLSCH_CODING
    for (int i =0; i<16; i++)
454
      printf("output interleaving f[%d]= %d r_offset %u\n", i,harq->f[i+r_offset], r_offset);
455

456 457
    if (r==harq->C-1)
      write_output("enc_output.m","enc",harq->f,G,1,4);
458
#endif
459 460

    r_offset += E;
461 462
  }

Rakesh's avatar
Rakesh committed
463
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_gNB_DLSCH_ENCODING, VCD_FUNCTION_OUT);
464

Khalid Ahmed's avatar
Khalid Ahmed committed
465
  return 0;
466
}