nr_dci.c 10.9 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
/*
 * 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 PHY/NR_TRANSPORT/nr_dci.c
23
* \brief Implements DCI encoding and PDCCH TX procedures (38.212/38.213/38.214). V15.4.0 2019-01.
24 25 26 27 28 29 30 31 32
* \author Guy De Souza
* \date 2018
* \version 0.1
* \company Eurecom
* \email: desouza@eurecom.fr
* \note
* \warning
*/

33

34
#include "nr_dci.h"
35
#include "nr_dlsch.h"
Khalid Ahmed's avatar
Khalid Ahmed committed
36
#include "nr_sch_dmrs.h"
37
#include "PHY/MODULATION/nr_modulation.h"
38

39
//#define DEBUG_PDCCH_DMRS
Raymond Knopp's avatar
Raymond Knopp committed
40 41
//#define DEBUG_DCI
//#define DEBUG_CHANNEL_CODING
42

43
void nr_pdcch_scrambling(uint32_t *in,
44
                         uint32_t size,
45
                         uint32_t Nid,
cig's avatar
cig committed
46
                         uint32_t scrambling_RNTI,
47
                         uint32_t *out) {
48 49 50
  uint8_t reset;
  uint32_t x1, x2, s=0;
  reset = 1;
cig's avatar
cig committed
51 52
  x2 = (scrambling_RNTI<<16) + Nid;
  LOG_D(PHY,"PDCCH Scrambling x2 %x : scrambling_RNTI %x \n", x2, scrambling_RNTI);
53
  for (int i=0; i<size; i++) {
54 55 56
    if ((i&0x1f)==0) {
      s = lte_gold_generic(&x1, &x2, reset);
      reset = 0;
57 58 59 60

      if (i) {
        in++;
        out++;
61
      }
62
    }
63

Hongzhi's avatar
Hongzhi committed
64
    (*out) ^= ((((*in)>>(i&0x1f))&1) ^ ((s>>(i&0x1f))&1))<<(i&0x1f);
65
  }
66 67
}

68
void nr_generate_dci(PHY_VARS_gNB *gNB,
69 70 71 72
                        nfapi_nr_dl_tti_pdcch_pdu_rel15_t *pdcch_pdu_rel15,
                        uint32_t **gold_pdcch_dmrs,
                        int32_t *txdataF,
                        int16_t amp,
73
                        NR_DL_FRAME_PARMS *frame_parms) {
74

75
  int16_t mod_dmrs[NR_MAX_CSET_DURATION][NR_MAX_PDCCH_DMRS_LENGTH>>1] __attribute__((aligned(16))); // 3 for the max coreset duration
76 77
  uint16_t cset_start_sc;
  uint8_t cset_start_symb, cset_nsymb;
78
  int k,l,k_prime,dci_idx, dmrs_idx;
79

80 81 82 83
  // find coreset descriptor
    
  int rb_offset;
  int n_rb;
Raymond Knopp's avatar
Raymond Knopp committed
84
  
85
  // compute rb_offset and n_prb based on frequency allocation
86
  nr_fill_cce_list(gNB,0,pdcch_pdu_rel15);
87
  get_coreset_rballoc(pdcch_pdu_rel15->FreqDomainResource,&n_rb,&rb_offset);
88
  cset_start_sc = frame_parms->first_carrier_offset + (pdcch_pdu_rel15->BWPStart + rb_offset) * NR_NB_SC_PER_RB;
89 90 91 92 93 94

  for (int d=0;d<pdcch_pdu_rel15->numDlDci;d++) {
    /*The coreset is initialised
     * in frequency: the first subcarrier is obtained by adding the first CRB overlapping the SSB and the rb_offset for coreset 0
     * or the rb_offset for other coresets
     * in time: by its first slot and its first symbol*/
95
    const nfapi_nr_dl_dci_pdu_t *dci_pdu = &pdcch_pdu_rel15->dci_pdu[d];
96 97 98 99

    cset_start_symb = pdcch_pdu_rel15->StartSymbolIndex;
    cset_nsymb = pdcch_pdu_rel15->DurationSymbols;
    dci_idx = 0;
Eurecom's avatar
Eurecom committed
100 101
    LOG_D(PHY, "Coreset rb_offset %d, nb_rb %d BWP Start %d\n",rb_offset,n_rb,pdcch_pdu_rel15->BWPStart);
    LOG_D(PHY, "Coreset starting subcarrier %d on symbol %d (%d symbols)\n", cset_start_sc, cset_start_symb, cset_nsymb);
102
    // DMRS length is per OFDM symbol
rmagueta's avatar
rmagueta committed
103
    uint32_t dmrs_length = n_rb*6; //2(QPSK)*3(per RB)*6(REG per CCE)
104
    uint32_t encoded_length = dci_pdu->AggregationLevel*108; //2(QPSK)*9(per RB)*6(REG per CCE)
rmagueta's avatar
rmagueta committed
105
    LOG_D(PHY, "DL_DCI : rb_offset %d, nb_rb %d, DMRS length per symbol %d\t DCI encoded length %d (precoder_granularity %d,reg_mapping %d),Scrambling_Id %d,ScramblingRNTI %x,PayloadSizeBits %d\n",
Raymond Knopp's avatar
Raymond Knopp committed
106
    rb_offset, n_rb,dmrs_length, encoded_length,pdcch_pdu_rel15->precoderGranularity,pdcch_pdu_rel15->CceRegMappingType,
107
    dci_pdu->ScramblingId,dci_pdu->ScramblingRNTI,dci_pdu->PayloadSizeBits);
108 109 110 111 112 113 114 115
    dmrs_length += rb_offset*6; // To accommodate more DMRS symbols in case of rb offset
      
    /// DMRS QPSK modulation
    for (int symb=cset_start_symb; symb<cset_start_symb + pdcch_pdu_rel15->DurationSymbols; symb++) {
      
      nr_modulation(gold_pdcch_dmrs[symb], dmrs_length, DMRS_MOD_ORDER, mod_dmrs[symb]); //Qm = 2 as DMRS is QPSK modulated
      
#ifdef DEBUG_PDCCH_DMRS
Raymond Knopp's avatar
Raymond Knopp committed
116
       if(dci_pdu->RNTI!=0xFFFF) {      
117
      for (int i=0; i<dmrs_length>>1; i++)
Raymond Knopp's avatar
Raymond Knopp committed
118 119 120
	printf("symb %d i %d %p gold seq 0x%08x mod_dmrs %d %d\n", symb, i,
	       &gold_pdcch_dmrs[symb][i>>5],gold_pdcch_dmrs[symb][i>>5], mod_dmrs[symb][i<<1], mod_dmrs[symb][(i<<1)+1] );
    }  
121
#endif
122 123 124 125 126
    }
    
    /// DCI payload processing
    // CRC attachment + Scrambling + Channel coding + Rate matching
    uint32_t encoder_output[NR_MAX_DCI_SIZE_DWORD];
127

128 129 130
    uint16_t n_RNTI = dci_pdu->RNTI;
    uint16_t Nid    = dci_pdu->ScramblingId;
    uint16_t scrambling_RNTI = dci_pdu->ScramblingRNTI;
131

132
    t_nrPolar_params *currentPtr = nr_polar_params(NR_POLAR_DCI_MESSAGE_TYPE, 
133 134
						   dci_pdu->PayloadSizeBits,
						   dci_pdu->AggregationLevel,
135
						   0,NULL);
136
    polar_encoder_fast((uint64_t*)dci_pdu->Payload, (void*)encoder_output, n_RNTI,1,currentPtr);
Sakthivel Velumani's avatar
Sakthivel Velumani committed
137 138
#if DEBUG_CHANNEL_CODING
    printf("polar rnti %x,length %d, L %d\n",n_RNTI, dci_pdu->PayloadSizeBits,dci_pdu->AggregationLevel);
139
    printf("DCI PDU: [0]->0x%lx \t [1]->0x%lx\n",
140
	   ((uint64_t*)dci_pdu->Payload)[0], ((uint64_t*)dci_pdu->Payload)[1]);
141 142 143
    printf("Encoded Payload (length:%d dwords):\n", encoded_length>>5);
    
    for (int i=0; i<encoded_length>>5; i++)
144 145
      printf("[%d]->0x%08x \t", i,encoder_output[i]);    

146
    printf("\n");
Guy De Souza's avatar
Guy De Souza committed
147
#endif
148 149
    /// Scrambling
    uint32_t scrambled_output[NR_MAX_DCI_SIZE_DWORD]= {0};
150
    nr_pdcch_scrambling(encoder_output, encoded_length, Nid, scrambling_RNTI, scrambled_output);
Hongzhi's avatar
Hongzhi committed
151
#ifdef DEBUG_CHANNEL_CODING
152
    printf("scrambled output: [0]->0x%08x \t [1]->0x%08x \t [2]->0x%08x \t [3]->0x%08x\t [4]->0x%08x\t [5]->0x%08x\t \
Hongzhi's avatar
Hongzhi committed
153
[6]->0x%08x \t [7]->0x%08x \t [8]->0x%08x \t [9]->0x%08x\t [10]->0x%08x\t [11]->0x%08x\n",
154 155
	   scrambled_output[0], scrambled_output[1], scrambled_output[2], scrambled_output[3], scrambled_output[4],scrambled_output[5],
	   scrambled_output[6], scrambled_output[7], scrambled_output[8], scrambled_output[9], scrambled_output[10],scrambled_output[11] );
Hongzhi's avatar
Hongzhi committed
156
#endif
157
    /// QPSK modulation
158
    int16_t mod_dci[NR_MAX_DCI_SIZE>>1] __attribute__((aligned(16)));
159
    nr_modulation(scrambled_output, encoded_length, DMRS_MOD_ORDER, mod_dci); //Qm = 2 as DMRS is QPSK modulated
Guy De Souza's avatar
Guy De Souza committed
160
#ifdef DEBUG_DCI
161 162 163 164
    
    for (int i=0; i<encoded_length>>1; i++)
      printf("i %d mod_dci %d %d\n", i, mod_dci[i<<1], mod_dci[(i<<1)+1] );
    
Guy De Souza's avatar
Guy De Souza committed
165
#endif
rmagueta's avatar
rmagueta committed
166

167
    /// Resource mapping
rmagueta's avatar
rmagueta committed
168

169 170
    if (cset_start_sc >= frame_parms->ofdm_symbol_size)
      cset_start_sc -= frame_parms->ofdm_symbol_size;
rmagueta's avatar
rmagueta committed
171 172 173

    // Get cce_list indices by reg_idx in ascending order
    int reg_list_index = 0;
174 175
    int reg_list_order[NR_MAX_PDCCH_AGG_LEVEL] = {};
    for (int p = 0; p < NR_MAX_PDCCH_AGG_LEVEL; p++) {
176
      for(int p2 = 0; p2 < dci_pdu->AggregationLevel; p2++) {
rmagueta's avatar
rmagueta committed
177 178 179 180 181 182 183
        if(gNB->cce_list[d][p2].reg_list[0].reg_idx == p * NR_NB_REG_PER_CCE) {
          reg_list_order[reg_list_index] = p2;
          reg_list_index++;
          break;
        }
      }
    }
184

185
    /*Mapping the encoded DCI along with the DMRS */
186
    for(int symbol_idx = 0; symbol_idx < pdcch_pdu_rel15->DurationSymbols; symbol_idx++) {
187
      for (int cce_count = 0; cce_count < dci_pdu->AggregationLevel; cce_count+=pdcch_pdu_rel15->DurationSymbols) {
rmagueta's avatar
rmagueta committed
188

189
        int8_t cce_idx = reg_list_order[cce_count];
rmagueta's avatar
rmagueta committed
190

191
        for (int reg_in_cce_idx = 0; reg_in_cce_idx < NR_NB_REG_PER_CCE; reg_in_cce_idx++) {
rmagueta's avatar
rmagueta committed
192

193
          k = cset_start_sc + gNB->cce_list[d][cce_idx].reg_list[reg_in_cce_idx].start_sc_idx;
rmagueta's avatar
rmagueta committed
194

195 196
          if (k >= frame_parms->ofdm_symbol_size)
            k -= frame_parms->ofdm_symbol_size;
rmagueta's avatar
rmagueta committed
197

198
          l = cset_start_symb + symbol_idx;
rmagueta's avatar
rmagueta committed
199

200 201 202 203 204
          // dmrs index depends on reference point for k according to 38.211 7.4.1.3.2
          if (pdcch_pdu_rel15->CoreSetType == NFAPI_NR_CSET_CONFIG_PDCCH_CONFIG)
            dmrs_idx = (gNB->cce_list[d][cce_idx].reg_list[reg_in_cce_idx].reg_idx) * 3;
          else
            dmrs_idx = (gNB->cce_list[d][cce_idx].reg_list[reg_in_cce_idx].reg_idx + rb_offset) * 3;
rmagueta's avatar
rmagueta committed
205

206
          k_prime = 0;
rmagueta's avatar
rmagueta committed
207

208 209
          for (int m = 0; m < NR_NB_SC_PER_RB; m++) {
            if (m == (k_prime << 2) + 1) { // DMRS if not already mapped
210
              ((int16_t *) txdataF)[(l * frame_parms->ofdm_symbol_size + k) << 1] =
211
                  (amp * mod_dmrs[l][dmrs_idx << 1]) >> 15;
212
              ((int16_t *) txdataF)[((l * frame_parms->ofdm_symbol_size + k) << 1) + 1] =
213
                  (amp * mod_dmrs[l][(dmrs_idx << 1) + 1]) >> 15;
rmagueta's avatar
rmagueta committed
214

215
#ifdef DEBUG_PDCCH_DMRS
rmagueta's avatar
rmagueta committed
216
              LOG_D(PHY,"PDCCH DMRS %d: l %d position %d => (%d,%d)\n",dmrs_idx,l,k,((int16_t *)txdataF)[(l*frame_parms->ofdm_symbol_size + k)<<1],
217
               ((int16_t *)txdataF)[((l*frame_parms->ofdm_symbol_size + k)<<1)+1]);
218
#endif
rmagueta's avatar
rmagueta committed
219

220 221
              dmrs_idx++;
              k_prime++;
rmagueta's avatar
rmagueta committed
222

223
            } else { // DCI payload
224 225
              ((int16_t *) txdataF)[(l * frame_parms->ofdm_symbol_size + k) << 1] = (amp * mod_dci[dci_idx << 1]) >> 15;
              ((int16_t *) txdataF)[((l * frame_parms->ofdm_symbol_size + k) << 1) + 1] =
226
                  (amp * mod_dci[(dci_idx << 1) + 1]) >> 15;
227
#ifdef DEBUG_DCI
rmagueta's avatar
rmagueta committed
228
              LOG_D(PHY,"PDCCH: l %d position %d => (%d,%d)\n",l,k,((int16_t *)txdataF)[(l*frame_parms->ofdm_symbol_size + k)<<1],
229
               ((int16_t *)txdataF)[((l*frame_parms->ofdm_symbol_size + k)<<1)+1]);
230
#endif
231

232 233
              dci_idx++;
            }
rmagueta's avatar
rmagueta committed
234

235
            k++;
rmagueta's avatar
rmagueta committed
236

237 238
            if (k >= frame_parms->ofdm_symbol_size)
              k -= frame_parms->ofdm_symbol_size;
rmagueta's avatar
rmagueta committed
239

240 241 242
          } // m
        } // reg_in_cce_idx
      } // cce_count
243
    } // symbol_idx
244

245 246 247 248
    LOG_D(PHY,
          "DCI: payloadSize = %d | payload = %llx\n",
          dci_pdu->PayloadSizeBits,
          *(unsigned long long *)dci_pdu->Payload);
249
  } // for (int d=0;d<pdcch_pdu_rel15->numDlDci;d++)
250
}
251

252
void nr_generate_dci_top(PHY_VARS_gNB *gNB,
253 254 255 256 257
			    nfapi_nr_dl_tti_pdcch_pdu *pdcch_pdu,
			    nfapi_nr_dl_tti_pdcch_pdu *ul_dci_pdu,
                            uint32_t **gold_pdcch_dmrs,
                            int32_t *txdataF,
                            int16_t amp,
258
                            NR_DL_FRAME_PARMS *frame_parms) {
259 260 261

  AssertFatal(pdcch_pdu!=NULL || ul_dci_pdu!=NULL,"At least one pointer has to be !NULL\n");

sfn's avatar
sfn committed
262
  if (pdcch_pdu) {
263 264
    nr_generate_dci(gNB,&pdcch_pdu->pdcch_pdu_rel15,gold_pdcch_dmrs,txdataF,amp,frame_parms);
  }
sfn's avatar
sfn committed
265
  if (ul_dci_pdu) {
266
    nr_generate_dci(gNB,&ul_dci_pdu->pdcch_pdu_rel15,gold_pdcch_dmrs,txdataF,amp,frame_parms);
sfn's avatar
sfn committed
267
  }
268 269
}