nr_dlsch_coding.c 15.4 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
/*
 * 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/CODING/coding_extern.h"
#include "PHY/CODING/coding_defs.h"
#include "PHY/CODING/lte_interleaver_inline.h"
37
#include "PHY/CODING/nrLDPC_extern.h"
cig's avatar
cig committed
38
#include "PHY/NR_TRANSPORT/nr_transport_proto.h"
39
#include "PHY/NR_TRANSPORT/nr_transport_common_proto.h"
40
#include "PHY/NR_TRANSPORT/nr_dlsch.h"
41
#include "SCHED_NR/sched_nr.h"
42 43
#include "common/utils/LOG/vcd_signal_dumper.h"
#include "common/utils/LOG/log.h"
44
#include "common/utils/nr/nr_common.h"
45
#include <syscall.h>
46
#include <openair2/UTIL/OPT/opt.h>
47 48 49 50

//#define DEBUG_DLSCH_CODING
//#define DEBUG_DLSCH_FREE 1

francescomani's avatar
francescomani committed
51 52
void free_gNB_dlsch(NR_gNB_DLSCH_t *dlsch, uint16_t N_RB, const NR_DL_FRAME_PARMS *frame_parms)
{
53 54
  int max_layers = (frame_parms->nb_antennas_tx<NR_MAX_NB_LAYERS) ? frame_parms->nb_antennas_tx : NR_MAX_NB_LAYERS;
  uint16_t a_segments = MAX_NUM_NR_DLSCH_SEGMENTS_PER_LAYER*max_layers;
55

56 57 58 59 60 61 62 63 64
  if (N_RB != 273) {
    a_segments = a_segments*N_RB;
    a_segments = a_segments/273 +1;
  }

  NR_DL_gNB_HARQ_t *harq = &dlsch->harq_process;
  if (harq->b) {
    free16(harq->b, a_segments * 1056);
    harq->b = NULL;
yilmazt's avatar
yilmazt committed
65
  }
66 67 68 69
  if (harq->f) {
    free16(harq->f, N_RB * NR_SYMBOLS_PER_SLOT * NR_NB_SC_PER_RB * 8 * NR_MAX_NB_LAYERS);
    harq->f = NULL;
  }
70
  for (int r = 0; r < a_segments; r++) {
71 72 73
    free(harq->c[r]);
    harq->c[r] = NULL;
  }
francescomani's avatar
francescomani committed
74
  free(harq->c);
75 76
  free(harq->pdu);

77
  for (int layer = 0; layer < max_layers; layer++) {
78 79 80 81
    for (int aa = 0; aa < 64; aa++)
      free(dlsch->ue_spec_bf_weights[layer][aa]);
    free(dlsch->ue_spec_bf_weights[layer]);
  }
francescomani's avatar
francescomani committed
82
  free(dlsch->ue_spec_bf_weights);
83 84
}

francescomani's avatar
francescomani committed
85 86
NR_gNB_DLSCH_t new_gNB_dlsch(NR_DL_FRAME_PARMS *frame_parms, uint16_t N_RB)
{
87 88
  int max_layers = (frame_parms->nb_antennas_tx<NR_MAX_NB_LAYERS) ? frame_parms->nb_antennas_tx : NR_MAX_NB_LAYERS;
  uint16_t a_segments = MAX_NUM_NR_DLSCH_SEGMENTS_PER_LAYER*max_layers;  //number of segments to be allocated
89

90 91
  if (N_RB != 273) {
    a_segments = a_segments*N_RB;
92
    a_segments = a_segments/273 +1;
93
  }
94

95
  LOG_D(PHY,"Allocating %d segments (MAX %d, N_PRB %d)\n",a_segments,MAX_NUM_NR_DLSCH_SEGMENTS_PER_LAYER,N_RB);
96
  uint32_t dlsch_bytes = a_segments*1056;  // allocated bytes per segment
francescomani's avatar
francescomani committed
97
  NR_gNB_DLSCH_t dlsch;
98

francescomani's avatar
francescomani committed
99
  dlsch.ue_spec_bf_weights = (int32_t ***)malloc16(max_layers * sizeof(int32_t **));
100
  for (int layer=0; layer<max_layers; layer++) {
francescomani's avatar
francescomani committed
101
    dlsch.ue_spec_bf_weights[layer] = (int32_t **)malloc16(64 * sizeof(int32_t *));
102

103
    for (int aa=0; aa<64; aa++) {
francescomani's avatar
francescomani committed
104
      dlsch.ue_spec_bf_weights[layer][aa] = (int32_t *)malloc16(OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES * sizeof(int32_t));
105

106
      for (int re=0; re<OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES; re++) {
francescomani's avatar
francescomani committed
107
        dlsch.ue_spec_bf_weights[layer][aa][re] = 0x00007fff;
108 109 110 111
      }
    }
  }

francescomani's avatar
francescomani committed
112
  NR_DL_gNB_HARQ_t *harq = &dlsch.harq_process;
113 114 115 116 117 118 119 120 121
  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);

122 123
  harq->c = (uint8_t **)malloc16(a_segments*sizeof(uint8_t *));
  for (int r = 0; r < a_segments; r++) {
124 125 126 127
    // 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)
128 129 130
    harq->c[r] = malloc16(8448);
    AssertFatal(harq->c[r], "cannot allocate harq->c[%d]\n", r);
    bzero(harq->c[r], 8448);
131
  }
132

133 134 135 136
  harq->f = malloc16(N_RB * NR_SYMBOLS_PER_SLOT * NR_NB_SC_PER_RB * 8 * NR_MAX_NB_LAYERS);
  AssertFatal(harq->f, "cannot allocate harq->f\n");
  bzero(harq->f, N_RB * NR_SYMBOLS_PER_SLOT * NR_NB_SC_PER_RB * 8 * NR_MAX_NB_LAYERS);

137
  return(dlsch);
138 139
}

140
void clean_gNB_dlsch(NR_gNB_DLSCH_t *dlsch) {
141 142
  AssertFatal(dlsch!=NULL,"dlsch is null\n");
  dlsch->active = 0;
143 144
}

145 146
void ldpc8blocks(void *p)
{
147 148
  encoder_implemparams_t *impp=(encoder_implemparams_t *) p;
  NR_DL_gNB_HARQ_t *harq = (NR_DL_gNB_HARQ_t *)impp->harq;
149
  nfapi_nr_dl_tti_pdsch_pdu_rel15_t *rel15 = &harq->pdsch_pdu.pdsch_pdu_rel15;
150
  uint8_t mod_order = rel15->qamModOrder[0];
Raymond Knopp's avatar
Raymond Knopp committed
151 152
  uint16_t nb_rb = rel15->rbSize;
  uint8_t nb_symb_sch = rel15->NrOfSymbols;
153 154
  uint16_t length_dmrs = get_num_dmrs(rel15->dlDmrsSymbPos);
  uint32_t A = rel15->TBSize[0]<<3;
155
  uint8_t nb_re_dmrs;
156 157 158

  if (rel15->dmrsConfigType==NFAPI_NR_DMRS_TYPE1)
    nb_re_dmrs = 6*rel15->numDmrsCdmGrpsNoData;
159
  else
160 161
    nb_re_dmrs = 4*rel15->numDmrsCdmGrpsNoData;

162
  unsigned int G = nr_get_G(nb_rb, nb_symb_sch, nb_re_dmrs, length_dmrs, harq->unav_res, mod_order, rel15->nrOfLayers);
163 164 165 166 167
  LOG_D(PHY,"dlsch coding A %d  Kr %d G %d (nb_rb %d, nb_symb_sch %d, nb_re_dmrs %d, length_dmrs %d, mod_order %d)\n",
        A,impp->K,G, nb_rb,nb_symb_sch,nb_re_dmrs,length_dmrs,(int)mod_order);
  // nrLDPC_encoder output is in "d"
  // let's make this interface happy!
  uint8_t tmp[8][68 * 384]__attribute__((aligned(32)));
168
  uint8_t *d[impp->n_segments];
169
  for (int rr=impp->macro_num*8, i=0; rr < impp->n_segments && rr < (impp->macro_num+1)*8; rr++,i++ )
170 171
    d[rr] = tmp[i];
  ldpc_interface.LDPCencoder(harq->c, d, impp);
172 173 174 175 176 177 178
  // Compute where to place in output buffer that is concatenation of all segments
  uint32_t r_offset=0;
  for (int i=0; i < impp->macro_num*8; i++ )
     r_offset+=nr_get_E(G, impp->n_segments, mod_order, rel15->nrOfLayers, i);
  for (int rr=impp->macro_num*8; rr < impp->n_segments && rr < (impp->macro_num+1)*8; rr++ ) {
    if (impp->F>0) {
      // writing into positions d[r][k-2Zc] as in clause 5.3.2 step 2) in 38.212
179
      memset(&d[rr][impp->K - impp->F - 2 * impp->Zc], NR_NULL, impp->F);
180 181 182 183 184 185 186
    }

#ifdef DEBUG_DLSCH_CODING
    LOG_D(PHY,"rvidx in encoding = %d\n", rel15->rvIndex[0]);
#endif
    uint32_t E = nr_get_E(G, impp->n_segments, mod_order, rel15->nrOfLayers, rr);
    //#ifdef DEBUG_DLSCH_CODING
187 188 189
    LOG_D(NR_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,nrOfLayer %d)...\n",
190 191 192 193 194
          rr,
          impp->n_segments,
          G,
          E,
          impp->F,
195 196 197 198
          impp->K - impp->F - 2 * impp->Zc,
          mod_order,
          nb_rb,
          rel15->nrOfLayers);
199

200
    uint32_t Tbslbrm = rel15->maintenance_parms_v3.tbSizeLbrmBytes;
201 202 203

    uint8_t e[E];
    bzero (e, E);
204
    nr_rate_matching_ldpc(Tbslbrm,
205
                          impp->BG,
206 207
                          impp->Zc,
                          d[rr],
208 209 210
                          e,
                          impp->n_segments,
                          impp->F,
211
                          impp->K - impp->F - 2 * impp->Zc,
212 213
                          rel15->rvIndex[0],
                          E);
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    if (impp->K - impp->F - 2 * impp->Zc > E) {
      LOG_E(PHY,
            "dlsch coding A %d  Kr %d G %d (nb_rb %d, nb_symb_sch %d, nb_re_dmrs %d, length_dmrs %d, mod_order %d)\n",
            A,
            impp->K,
            G,
            nb_rb,
            nb_symb_sch,
            nb_re_dmrs,
            length_dmrs,
            (int)mod_order);

      LOG_E(NR_PHY,
            "Rate Matching, Code segment %d/%d (coded bits (G) %u, E %d, Kr %d, Filler bits %d, Filler offset %d mod_order %d, "
            "nb_rb %d)...\n",
            rr,
            impp->n_segments,
            G,
            E,
            impp->K,
            impp->F,
            impp->K - impp->F - 2 * impp->Zc,
            mod_order,
            nb_rb);
238
    }
239 240 241
#ifdef DEBUG_DLSCH_CODING

    for (int i =0; i<16; i++)
242
      printf("output ratematching e[%d]= %d r_offset %u\n", i,e[i], r_offset);
243 244 245 246 247 248 249 250 251 252

#endif
    nr_interleaving_ldpc(E,
                         mod_order,
                         e,
                         impp->output+r_offset);
#ifdef DEBUG_DLSCH_CODING

    for (int i =0; i<16; i++)
      printf("output interleaving f[%d]= %d r_offset %u\n", i,impp->output[i+r_offset], r_offset);
253

254 255 256 257 258 259 260 261 262 263 264
    if (r==impp->n_segments-1)
      write_output("enc_output.m","enc",impp->output,G,1,4);

#endif
    r_offset += E;
  }
}

int nr_dlsch_encoding(PHY_VARS_gNB *gNB,
                      int frame,
                      uint8_t slot,
265
                      NR_DL_gNB_HARQ_t *harq,
266
                      NR_DL_FRAME_PARMS *frame_parms,
francescomani's avatar
francescomani committed
267 268 269 270 271 272 273
                      unsigned char *output,
                      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,
274 275
                      time_stats_t *dlsch_segmentation_stats)
{
276 277 278 279
  encoder_implemparams_t impp;
  impp.output=output;
  unsigned int crc=1;
  nfapi_nr_dl_tti_pdsch_pdu_rel15_t *rel15 = &harq->pdsch_pdu.pdsch_pdu_rel15;
280
  impp.Zc = harq->Z;
Rakesh's avatar
Rakesh committed
281
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_gNB_DLSCH_ENCODING, VCD_FUNCTION_IN);
282
  uint32_t A = rel15->TBSize[0]<<3;
283
  unsigned char *a=harq->pdu;
284
  if (rel15->rnti != SI_RNTI)
Laurent THOMAS's avatar
Laurent THOMAS committed
285
    trace_NRpdu(DIRECTION_DOWNLINK, a, rel15->TBSize[0], WS_C_RNTI, rel15->rnti, frame, slot,0, 0);
286

287 288 289
  NR_gNB_PHY_STATS_t *phy_stats = NULL;
  if (rel15->rnti != 0xFFFF)
    phy_stats = get_phy_stats(gNB, rel15->rnti);
290

291 292 293 294 295
  if (phy_stats) {
    phy_stats->frame = frame;
    phy_stats->dlsch_stats.total_bytes_tx += rel15->TBSize[0];
    phy_stats->dlsch_stats.current_RI = rel15->nrOfLayers;
    phy_stats->dlsch_stats.current_Qm = rel15->qamModOrder[0];
296
  }
297

298
  int max_bytes = MAX_NUM_NR_DLSCH_SEGMENTS_PER_LAYER*rel15->nrOfLayers*1056;
299 300
  int B;
  if (A > NR_MAX_PDSCH_TBS) {
301 302
    // Add 24-bit crc (polynomial A) to payload
    crc = crc24a(a,A)>>8;
303 304 305
    a[A>>3] = ((uint8_t *)&crc)[2];
    a[1+(A>>3)] = ((uint8_t *)&crc)[1];
    a[2+(A>>3)] = ((uint8_t *)&crc)[0];
306 307
    //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)]);
308
    B = A + 24;
309
    //    harq->b = a;
310
    AssertFatal((A / 8) + 4 <= max_bytes,
311 312 313
                "A %d is too big (A/8+4 = %d > %d)\n",
                A,
                (A / 8) + 4,
314
                max_bytes);
315
    memcpy(harq->b, a, (A / 8) + 4); // why is this +4 if the CRC is only 3 bytes?
316
  } else {
317 318
    // Add 16-bit crc (polynomial A) to payload
    crc = crc16(a,A)>>16;
319 320
    a[A>>3] = ((uint8_t *)&crc)[1];
    a[1+(A>>3)] = ((uint8_t *)&crc)[0];
321 322
    //printf("CRC %x (A %d)\n",crc,A);
    //printf("a0 %d a1 %d \n", a[A>>3], a[1+(A>>3)]);
323
    B = A + 16;
324
    //    harq->b = a;
325
    AssertFatal((A / 8) + 3 <= max_bytes,
326 327 328
                "A %d is too big (A/8+3 = %d > %d)\n",
                A,
                (A / 8) + 3,
329
                max_bytes);
330
    memcpy(harq->b, a, (A / 8) + 3); // using 3 bytes to mimic the case of 24 bit crc
331
  }
332

333
  impp.BG = rel15->maintenance_parms_v3.ldpcBaseGraph;
334

335
  start_meas(dlsch_segmentation_stats);
336
  impp.Kb = nr_segmentation(harq->b, harq->c, B, &impp.n_segments, &impp.K, &impp.Zc, &impp.F, impp.BG);
337
  stop_meas(dlsch_segmentation_stats);
338

339
  if (impp.n_segments>MAX_NUM_NR_DLSCH_SEGMENTS_PER_LAYER*rel15->nrOfLayers) {
340
    LOG_E(PHY, "nr_segmentation.c: too many segments %d, B %d\n", impp.n_segments, B);
341 342
    return(-1);
  }
343
  for (int r=0; r<impp.n_segments; r++) {
344 345
    //d_tmp[r] = &harq->d[r][0];
    //channel_input[r] = &harq->d[r][0];
346
#ifdef DEBUG_DLSCH_CODING
347
    LOG_D(PHY,"Encoder: B %d F %d \n",harq->B, impp.F);
348
    LOG_D(PHY,"start ldpc encoder segment %d/%d\n",r,impp.n_segments);
349
    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]);
350

351
    for (int cnt =0 ; cnt < 22*(*impp.Zc)/8; cnt ++) {
352
      LOG_D(PHY,"%d ", harq->c[r][cnt]);
353
    }
354

355
    LOG_D(PHY,"\n");
356
#endif
357 358
    //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);
359
  }
360

361 362 363 364
  impp.tprep = tprep;
  impp.tinput = tinput;
  impp.tparity = tparity;
  impp.toutput = toutput;
365
  impp.harq = harq;
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
  if (gNB->ldpc_offload_flag && *rel15->mcsIndex > 2) {
    impp.Qm = rel15->qamModOrder[0];
    impp.rv = rel15->rvIndex[0];
    int nb_re_dmrs =
        (rel15->dmrsConfigType == NFAPI_NR_DMRS_TYPE1) ? (6 * rel15->numDmrsCdmGrpsNoData) : (4 * rel15->numDmrsCdmGrpsNoData);
    impp.G = nr_get_G(rel15->rbSize,
                      rel15->NrOfSymbols,
                      nb_re_dmrs,
                      get_num_dmrs(rel15->dlDmrsSymbPos),
                      harq->unav_res,
                      rel15->qamModOrder[0],
                      rel15->nrOfLayers);
    uint8_t tmp[68 * 384] __attribute__((aligned(32)));
    uint8_t *d = tmp;
    int r_offset = 0;
    for (int r = 0; r < impp.n_segments; r++) {
      impp.E = nr_get_E(impp.G, impp.n_segments, impp.Qm, rel15->nrOfLayers, r);
      impp.Kr = impp.K;
      ldpc_interface_offload.LDPCencoder(&harq->c[r], &d, &impp);
      uint8_t e[impp.E];
      bzero(e, impp.E);
      nr_rate_matching_ldpc(rel15->maintenance_parms_v3.tbSizeLbrmBytes,
                            impp.BG,
                            impp.Zc,
                            tmp,
                            e,
                            impp.n_segments,
                            impp.F,
                            impp.K - impp.F - 2 * impp.Zc,
                            impp.rv,
                            impp.E);
      nr_interleaving_ldpc(impp.E, impp.Qm, e, impp.output + r_offset);
      r_offset += impp.E;
    }
  } else {
    notifiedFIFO_t nf;
    initNotifiedFIFO(&nf);
    int nbJobs = 0;
    for (int j = 0; j < (impp.n_segments / 8 + ((impp.n_segments & 7) == 0 ? 0 : 1)); j++) {
      notifiedFIFO_elt_t *req = newNotifiedFIFO_elt(sizeof(impp), j, &nf, ldpc8blocks);
      encoder_implemparams_t *perJobImpp = (encoder_implemparams_t *)NotifiedFifoData(req);
      *perJobImpp = impp;
      perJobImpp->macro_num = j;
      pushTpool(&gNB->threadPool, req);
      nbJobs++;
    }
    while (nbJobs) {
      notifiedFIFO_elt_t *req = pullTpool(&nf, &gNB->threadPool);
      if (req == NULL)
        break; // Tpool has been stopped
      delNotifiedFIFO_elt(req);
      nbJobs--;
    }
419
  }
Rakesh's avatar
Rakesh committed
420
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_gNB_DLSCH_ENCODING, VCD_FUNCTION_OUT);
Khalid Ahmed's avatar
Khalid Ahmed committed
421
  return 0;
422
}