From 7563937af890054f34745a8041cd2edbd9abda11 Mon Sep 17 00:00:00 2001
From: Robert Schmidt <robert.schmidt@openairinterface.org>
Date: Fri, 26 Jul 2024 16:58:38 +0200
Subject: [PATCH] Use gold cache for CSI-RS decoding

---
 openair1/PHY/INIT/nr_init.c                   | 21 --------
 openair1/PHY/INIT/nr_init_ue.c                | 18 -------
 openair1/PHY/NR_REFSIG/nr_refsig.h            |  6 ---
 openair1/PHY/NR_REFSIG/nr_refsig_common.h     |  1 +
 openair1/PHY/NR_REFSIG/refsig.c               |  8 +++
 openair1/PHY/NR_REFSIG/refsig_defs_ue.h       |  1 -
 openair1/PHY/NR_TRANSPORT/nr_csi_rs.c         | 53 +++++++------------
 .../PHY/NR_UE_TRANSPORT/nr_initial_sync.c     |  1 -
 openair1/PHY/defs_nr_common.h                 |  2 -
 9 files changed, 28 insertions(+), 83 deletions(-)

diff --git a/openair1/PHY/INIT/nr_init.c b/openair1/PHY/INIT/nr_init.c
index 93f5de1a665..61a517c962a 100644
--- a/openair1/PHY/INIT/nr_init.c
+++ b/openair1/PHY/INIT/nr_init.c
@@ -152,21 +152,7 @@ int phy_init_nr_gNB(PHY_VARS_gNB *gNB)
 
   // CSI RS init
   // ceil((NB_RB*8(max allocation per RB)*2(QPSK))/32)
-  int csi_dmrs_init_length =  ((fp->N_RB_DL<<4)>>5)+1;
   gNB->nr_csi_info = (nr_csi_info_t *)malloc16_clear(sizeof(nr_csi_info_t));
-  gNB->nr_csi_info->nr_gold_csi_rs = (uint32_t ***)malloc16(fp->slots_per_frame * sizeof(uint32_t **));
-  AssertFatal(gNB->nr_csi_info->nr_gold_csi_rs != NULL, "NR init: csi reference signal malloc failed\n");
-  for (int slot=0; slot<fp->slots_per_frame; slot++) {
-    gNB->nr_csi_info->nr_gold_csi_rs[slot] = (uint32_t **)malloc16(fp->symbols_per_slot * sizeof(uint32_t *));
-    AssertFatal(gNB->nr_csi_info->nr_gold_csi_rs[slot] != NULL, "NR init: csi reference signal for slot %d - malloc failed\n", slot);
-    for (int symb=0; symb<fp->symbols_per_slot; symb++) {
-      gNB->nr_csi_info->nr_gold_csi_rs[slot][symb] = (uint32_t *)malloc16(csi_dmrs_init_length * sizeof(uint32_t));
-      AssertFatal(gNB->nr_csi_info->nr_gold_csi_rs[slot][symb] != NULL, "NR init: csi reference signal for slot %d symbol %d - malloc failed\n", slot, symb);
-    }
-  }
-
-  gNB->nr_csi_info->csi_gold_init = cfg->cell_config.phy_cell_id.value;
-  nr_init_csi_rs(&gNB->frame_parms, gNB->nr_csi_info->nr_gold_csi_rs, cfg->cell_config.phy_cell_id.value);
 
   //PRS init
   nr_init_prs(gNB);
@@ -266,13 +252,6 @@ void phy_free_nr_gNB(PHY_VARS_gNB *gNB)
   free_and_zero(meas->n0_subband_power);
   free_and_zero(meas->n0_subband_power_dB);
 
-  uint32_t ***nr_gold_csi_rs = gNB->nr_csi_info->nr_gold_csi_rs;
-  for (int slot = 0; slot < fp->slots_per_frame; slot++) {
-    for (int symb = 0; symb < fp->symbols_per_slot; symb++)
-      free_and_zero(nr_gold_csi_rs[slot][symb]);
-    free_and_zero(nr_gold_csi_rs[slot]);
-  }
-  free_and_zero(nr_gold_csi_rs);
   free_and_zero(gNB->nr_csi_info);
 
   for (int id = 0; id < gNB->max_nb_srs; id++) {
diff --git a/openair1/PHY/INIT/nr_init_ue.c b/openair1/PHY/INIT/nr_init_ue.c
index 8220733402d..13f49224400 100644
--- a/openair1/PHY/INIT/nr_init_ue.c
+++ b/openair1/PHY/INIT/nr_init_ue.c
@@ -296,18 +296,7 @@ int init_nr_ue_signal(PHY_VARS_NR_UE *ue, int nb_connected_gNB)
     srs_vars[gNB_id]->active = false;
 
     // ceil((NB_RB*8(max allocation per RB)*2(QPSK))/32)
-    int csi_dmrs_init_length =  ((fp->N_RB_DL<<4)>>5)+1;
     ue->nr_csi_info = malloc16_clear(sizeof(nr_csi_info_t));
-    ue->nr_csi_info->nr_gold_csi_rs = malloc16(fp->slots_per_frame * sizeof(uint32_t **));
-    AssertFatal(ue->nr_csi_info->nr_gold_csi_rs != NULL, "NR init: csi reference signal malloc failed\n");
-    for (int slot=0; slot<fp->slots_per_frame; slot++) {
-      ue->nr_csi_info->nr_gold_csi_rs[slot] = malloc16(fp->symbols_per_slot * sizeof(uint32_t *));
-      AssertFatal(ue->nr_csi_info->nr_gold_csi_rs[slot] != NULL, "NR init: csi reference signal for slot %d - malloc failed\n", slot);
-      for (int symb=0; symb<fp->symbols_per_slot; symb++) {
-        ue->nr_csi_info->nr_gold_csi_rs[slot][symb] = malloc16(csi_dmrs_init_length * sizeof(uint32_t));
-        AssertFatal(ue->nr_csi_info->nr_gold_csi_rs[slot][symb] != NULL, "NR init: csi reference signal for slot %d symbol %d - malloc failed\n", slot, symb);
-      }
-    }
     ue->nr_csi_info->csi_rs_generated_signal = malloc16(NR_MAX_NB_PORTS * sizeof(int32_t *));
     for (i=0; i<NR_MAX_NB_PORTS; i++) {
       ue->nr_csi_info->csi_rs_generated_signal[i] = malloc16_clear(fp->samples_per_frame_wCP * sizeof(int32_t));
@@ -362,13 +351,6 @@ void term_nr_ue_signal(PHY_VARS_NR_UE *ue, int nb_connected_gNB)
       free_and_zero(ue->nr_csi_info->csi_rs_generated_signal[i]);
     }
     free_and_zero(ue->nr_csi_info->csi_rs_generated_signal);
-    for (int slot=0; slot<fp->slots_per_frame; slot++) {
-      for (int symb=0; symb<fp->symbols_per_slot; symb++) {
-        free_and_zero(ue->nr_csi_info->nr_gold_csi_rs[slot][symb]);
-      }
-      free_and_zero(ue->nr_csi_info->nr_gold_csi_rs[slot]);
-    }
-    free_and_zero(ue->nr_csi_info->nr_gold_csi_rs);
     free_and_zero(ue->nr_csi_info);
 
     free_and_zero(ue->nr_srs_info);
diff --git a/openair1/PHY/NR_REFSIG/nr_refsig.h b/openair1/PHY/NR_REFSIG/nr_refsig.h
index 7aad67f7af4..97b2bb0d37a 100644
--- a/openair1/PHY/NR_REFSIG/nr_refsig.h
+++ b/openair1/PHY/NR_REFSIG/nr_refsig.h
@@ -35,12 +35,6 @@ This function generates NR Gold Sequence(ts 138.211) for the PRS.
 */
 void nr_init_prs(PHY_VARS_gNB* gNB);
 
-/*!\brief This function generates the NR Gold sequence (38-211, Sec 5.2.1) for the PDCCH DMRS.
-@param PHY_VARS_gNB* gNB structure provides configuration, frame parameters and the pointers to the 32 bits sequence storage tables
-@param Nid is used for the initialization of x2, Physical cell Id by default or upper layer configured pdcch_scrambling_ID
- */
-void nr_init_csi_rs(const NR_DL_FRAME_PARMS *fp, uint32_t ***csi_rs, uint32_t Nid);
-
 int nr_pusch_dmrs_delta(uint8_t dmrs_config_type, unsigned short p);
 
 int nr_pusch_dmrs_rx(PHY_VARS_gNB *gNB,
diff --git a/openair1/PHY/NR_REFSIG/nr_refsig_common.h b/openair1/PHY/NR_REFSIG/nr_refsig_common.h
index 03f5153ca3d..e43c9f10d13 100644
--- a/openair1/PHY/NR_REFSIG/nr_refsig_common.h
+++ b/openair1/PHY/NR_REFSIG/nr_refsig_common.h
@@ -29,5 +29,6 @@ uint32_t *nr_gold_pbch(int Lmax, int Nid, int n_hf, int ssb);
 uint32_t *nr_gold_pdcch(int N_RB_DL, int symbols_per_slot, unsigned short n_idDMRS, int ns, int l);
 uint32_t *nr_gold_pdsch(int N_RB_DL, int symbols_per_slot, int nid, int nscid, int slot, int symbol);
 uint32_t *nr_gold_pusch(int N_RB_UL, int symbols_per_slot, int Nid, int nscid, int slot, int symbol);
+uint32_t *nr_gold_csi_rs(int N_RB_DL, int symbols_per_slot, int slot, int symb, uint32_t Nid);
 
 #endif
diff --git a/openair1/PHY/NR_REFSIG/refsig.c b/openair1/PHY/NR_REFSIG/refsig.c
index 3e7e5549ea8..5946227d852 100644
--- a/openair1/PHY/NR_REFSIG/refsig.c
+++ b/openair1/PHY/NR_REFSIG/refsig.c
@@ -197,3 +197,11 @@ uint32_t *nr_gold_pusch(int N_RB_UL, int symbols_per_slot, int Nid, int nscid, i
 {
   return nr_gold_pdsch(N_RB_UL, symbols_per_slot, Nid, nscid, slot, symbol);
 }
+
+uint32_t *nr_gold_csi_rs(int N_RB_DL, int symbols_per_slot, int slot, int symb, uint32_t Nid)
+{
+  int csi_dmrs_init_length = ((N_RB_DL << 4) >> 5) + 1;
+  uint64_t temp_x2 = (1ULL << 10) * ((uint64_t)symbols_per_slot * slot + symb + 1) * ((Nid << 1) + 1) + Nid;
+  uint32_t x2 = temp_x2 % (1U << 31);
+  return gold_cache(x2, csi_dmrs_init_length);
+}
diff --git a/openair1/PHY/NR_REFSIG/refsig_defs_ue.h b/openair1/PHY/NR_REFSIG/refsig_defs_ue.h
index a90383c254e..290ea89ffeb 100644
--- a/openair1/PHY/NR_REFSIG/refsig_defs_ue.h
+++ b/openair1/PHY/NR_REFSIG/refsig_defs_ue.h
@@ -51,7 +51,6 @@ int nr_pdsch_dmrs_rx(const PHY_VARS_NR_UE *ue,
                      unsigned short nb_pdsch_rb,
                      uint8_t config_type);
 
-void nr_init_csi_rs(const NR_DL_FRAME_PARMS *fp, uint32_t ***csi_rs, uint32_t Nid);
 void init_nr_gold_prs(PHY_VARS_NR_UE* ue);
 void sl_generate_pss(SL_NR_UE_INIT_PARAMS_t *sl_init_params, uint8_t n_sl_id2, uint16_t scaling);
 void sl_generate_pss_ifft_samples(sl_nr_ue_phy_params_t *sl_ue_params, SL_NR_UE_INIT_PARAMS_t *sl_init_params);
diff --git a/openair1/PHY/NR_TRANSPORT/nr_csi_rs.c b/openair1/PHY/NR_TRANSPORT/nr_csi_rs.c
index 001ad8ad87a..3c859a7e673 100644
--- a/openair1/PHY/NR_TRANSPORT/nr_csi_rs.c
+++ b/openair1/PHY/NR_TRANSPORT/nr_csi_rs.c
@@ -24,23 +24,6 @@
 
 //#define NR_CSIRS_DEBUG
 
-
-void nr_init_csi_rs(const NR_DL_FRAME_PARMS *fp, uint32_t ***csi_rs, uint32_t Nid)
-{
-  uint32_t x1 = 0;
-  int csi_dmrs_init_length = ((fp->N_RB_DL << 4) >> 5) + 1;
-  for (int slot = 0; slot < fp->slots_per_frame; slot++) {
-    for (int symb = 0; symb < fp->symbols_per_slot; symb++) {
-      uint8_t reset = 1;
-      uint32_t x2 = ((1 << 10) * (fp->symbols_per_slot * slot + symb + 1) * ((Nid << 1) + 1) + Nid);
-      for (uint32_t n = 0; n < csi_dmrs_init_length; n++) {
-        csi_rs[slot][symb][n] = lte_gold_generic(&x1, &x2, reset);
-        reset = 0;
-      }
-    }
-  }
-}
-
 void nr_generate_csi_rs(const NR_DL_FRAME_PARMS *frame_parms,
                         int32_t **dataF,
                         const int16_t amp,
@@ -74,7 +57,6 @@ void nr_generate_csi_rs(const NR_DL_FRAME_PARMS *frame_parms,
 #endif
 
   int dataF_offset = slot * frame_parms->samples_per_slot_wCP;
-  uint32_t **nr_gold_csi_rs = nr_csi_info->nr_gold_csi_rs[slot];
   //*8(max allocation per RB)*2(QPSK))
   int csi_rs_length =  frame_parms->N_RB_DL << 4;
   int16_t mod_csi[frame_parms->symbols_per_slot][csi_rs_length>>1] __attribute__((aligned(16)));
@@ -82,19 +64,11 @@ void nr_generate_csi_rs(const NR_DL_FRAME_PARMS *frame_parms,
   uint32_t beta = amp;
   nr_csi_info->csi_rs_generated_signal_bits = log2_approx(amp);
 
-  AssertFatal(b!=0, "Invalid CSI frequency domain mapping: no bit selected in bitmap\n");
-
-  // if the scrambling id is not the one previously used to initialize we need to re-initialize the rs
-  if (csi_params->scramb_id != nr_csi_info->csi_gold_init) {
-    nr_csi_info->csi_gold_init = csi_params->scramb_id;
-    nr_init_csi_rs(frame_parms, nr_csi_info->nr_gold_csi_rs, csi_params->scramb_id);
-  }
-
+  AssertFatal(b != 0, "Invalid CSI frequency domain mapping: no bit selected in bitmap\n");
   int size, ports, kprime, lprime;
   int j[16], k_n[6], koverline[16], loverline[16];
   int found = 0;
   int fi = 0;
-
   // implementation of table 7.4.1.5.3-1 of 38.211
   // lprime and kprime are the max value of l' and k'
   switch (csi_params->row) {
@@ -581,14 +555,25 @@ void nr_generate_csi_rs(const NR_DL_FRAME_PARMS *frame_parms,
 
   for (int lp = 0; lp <= lprime; lp++) {
     int symb = csi_params->symb_l0;
-    nr_modulation(nr_gold_csi_rs[symb + lp], csi_length, DMRS_MOD_ORDER, mod_csi[symb + lp]);
-    if ((csi_params->row == 5) || (csi_params->row == 7) || (csi_params->row == 11) || (csi_params->row == 13) || (csi_params->row == 16))
-      nr_modulation(nr_gold_csi_rs[symb + 1], csi_length, DMRS_MOD_ORDER, mod_csi[symb + 1]);
-    if ((csi_params->row == 14) || (csi_params->row == 13) || (csi_params->row == 16) || (csi_params->row == 17)) {
+    const uint32_t *gold =
+        nr_gold_csi_rs(frame_parms->N_RB_DL, frame_parms->symbols_per_slot, slot, symb + lp, csi_params->scramb_id);
+    nr_modulation(gold, csi_length, DMRS_MOD_ORDER, mod_csi[symb + lp]);
+    uint8_t row = csi_params->row;
+    if ((row == 5) || (row == 7) || (row == 11) || (row == 13) || (row == 16)) {
+      const uint32_t *gold =
+          nr_gold_csi_rs(frame_parms->N_RB_DL, frame_parms->symbols_per_slot, slot, symb + 1, csi_params->scramb_id);
+      nr_modulation(gold, csi_length, DMRS_MOD_ORDER, mod_csi[symb + 1]);
+    }
+    if ((row == 14) || (row == 13) || (row == 16) || (row == 17)) {
       symb = csi_params->symb_l1;
-      nr_modulation(nr_gold_csi_rs[symb + lp], csi_length, DMRS_MOD_ORDER, mod_csi[symb + lp]);
-      if ((csi_params->row == 13) || (csi_params->row == 16))
-        nr_modulation(nr_gold_csi_rs[symb + 1], csi_length, DMRS_MOD_ORDER, mod_csi[symb + 1]);
+      const uint32_t *gold =
+          nr_gold_csi_rs(frame_parms->N_RB_DL, frame_parms->symbols_per_slot, slot, symb + lp, csi_params->scramb_id);
+      nr_modulation(gold, csi_length, DMRS_MOD_ORDER, mod_csi[symb + lp]);
+      if ((row == 13) || (row == 16)) {
+        const uint32_t *gold =
+            nr_gold_csi_rs(frame_parms->N_RB_DL, frame_parms->symbols_per_slot, slot, symb + 1, csi_params->scramb_id);
+        nr_modulation(gold, csi_length, DMRS_MOD_ORDER, mod_csi[symb + 1]);
+      }
     }
   }
 
diff --git a/openair1/PHY/NR_UE_TRANSPORT/nr_initial_sync.c b/openair1/PHY/NR_UE_TRANSPORT/nr_initial_sync.c
index 5d15dc86a41..8febf221d29 100644
--- a/openair1/PHY/NR_UE_TRANSPORT/nr_initial_sync.c
+++ b/openair1/PHY/NR_UE_TRANSPORT/nr_initial_sync.c
@@ -407,7 +407,6 @@ nr_initial_sync_t nr_initial_sync(UE_nr_rxtx_proc_t *proc,
     // the n_frames we got sync
     ue->init_sync_frame = n_frames - 1 - res.syncRes.frame_id;
 
-    nr_init_csi_rs(fp, ue->nr_csi_info->nr_gold_csi_rs, fp->Nid_cell);
     // we also need to take into account the shift by samples_per_frame in case the if is true
     if (res.ssbOffset < sync_pos_frame) {
       res.syncRes.rx_offset = fp->samples_per_frame - sync_pos_frame + res.ssbOffset;
diff --git a/openair1/PHY/defs_nr_common.h b/openair1/PHY/defs_nr_common.h
index 9c2dff22b81..d3122eb78f4 100644
--- a/openair1/PHY/defs_nr_common.h
+++ b/openair1/PHY/defs_nr_common.h
@@ -122,8 +122,6 @@ typedef struct {
 } nr_srs_info_t;
 
 typedef struct {
-  uint16_t csi_gold_init;
-  uint32_t ***nr_gold_csi_rs;
   uint8_t csi_rs_generated_signal_bits;
   int32_t **csi_rs_generated_signal;
   bool csi_im_meas_computed;
-- 
GitLab