sched_rx_pdsch.c 10.6 KB
Newer Older
1
/*******************************************************************************
2
3
    OpenAirInterface 
    Copyright(c) 1999 - 2014 Eurecom
4

5
6
7
8
    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
9
10


11
12
13
14
    OpenAirInterface is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
15

16
17
18
19
    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is 
    included in this distribution in the file called "COPYING". If not, 
    see <http://www.gnu.org/licenses/>.
20

21
22
23
24
25
   Contact Information
   OpenAirInterface Admin: openair_admin@eurecom.fr
   OpenAirInterface Tech : openair_tech@eurecom.fr
   OpenAirInterface Dev  : openair4g-devel@eurecom.fr
  
ghaddab's avatar
ghaddab committed
26
   Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE
27

28
 *******************************************************************************/
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

/*! \file sched_dlsch.c
* \brief DLSCH decoding thread (RTAI)
* \author R. Knopp, F. Kaltenberger
* \date 2011
* \version 0.1
* \company Eurecom
* \email: knopp@eurecom.fr,florian.kaltenberger@eurecom.fr
* \note
* \warning
*/
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>

#include "rt_wrapper.h"

#include <sys/mman.h>

#include "PHY/types.h"
#include "PHY/defs.h"
#include "PHY/extern.h"
#include "SCHED/defs.h"
#include "SCHED/extern.h"

#include "MAC_INTERFACE/extern.h"

#ifdef CBMIMO1
#include "ARCH/CBMIMO1/DEVICE_DRIVER/cbmimo1_device.h"
#include "ARCH/CBMIMO1/DEVICE_DRIVER/extern.h"
#include "ARCH/CBMIMO1/DEVICE_DRIVER/defs.h"
#endif // CBMIMO1

#include "UTIL/LOG/vcd_signal_dumper.h"

RTIME time0,time1;

#define DEBUG_PHY

/// Mutex for instance count on rx_pdsch scheduling 
pthread_mutex_t rx_pdsch_mutex;
/// Condition variable for rx_pdsch thread
pthread_cond_t rx_pdsch_cond;

pthread_t rx_pdsch_thread_var;
pthread_attr_t attr_rx_pdsch_thread;

// activity indicators for harq_pid's
int rx_pdsch_instance_cnt;
// process ids for cpu
int rx_pdsch_cpuid;
// subframe number for each harq_pid (needed to store ack in right place for UL)
int rx_pdsch_slot;

extern int oai_exit;
extern pthread_mutex_t dlsch_mutex[8];
extern int dlsch_instance_cnt[8];
extern int dlsch_subframe[8];
extern pthread_cond_t dlsch_cond[8];

/** RX_PDSCH Decoding Thread */
static void * rx_pdsch_thread(void *param) {

  //unsigned long cpuid;
gauthier's avatar
gauthier committed
93
94
95
  uint8_t rx_pdsch_thread_index = 0;
  uint8_t dlsch_thread_index = 0;
  uint8_t pilot1,pilot2,pilot3,harq_pid,subframe;
knopp's avatar
   
knopp committed
96
  //  uint8_t last_slot;
97

gauthier's avatar
gauthier committed
98
99
  uint8_t dual_stream_UE = 0;
  uint8_t i_mod = 0;
100
101
102
103
104
105
106
107

  RTIME time_in,time_out;
#ifdef RTAI
  RT_TASK *task;
#endif

  int m,eNB_id = 0;
  int eNB_id_i = 1;
knopp's avatar
   
knopp committed
108
  PHY_VARS_UE *UE = PHY_vars_UE_g[0];
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

#ifdef RTAI
  task = rt_task_init_schmod(nam2num("RX_PDSCH_THREAD"), 0, 0, 0, SCHED_FIFO, 0xF);

  if (task==NULL) {
    LOG_E(PHY,"[SCHED][RX_PDSCH] Problem starting rx_pdsch thread!!!!\n");
    return 0;
  }
  else {
    LOG_I(PHY,"[SCHED][RX_PDSCH] rx_pdsch_thread started for with id %p\n",task);
  }
#endif

  mlockall(MCL_CURRENT | MCL_FUTURE);

  //rt_set_runnable_on_cpuid(task,1);
  //cpuid = rtai_cpuid();

#ifdef HARD_RT
  rt_make_hard_real_time();
#endif

knopp's avatar
   
knopp committed
131
  if (UE->lte_frame_parms.Ncp == 0) {  // normal prefix
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
    pilot1 = 4;
    pilot2 = 7;
    pilot3 = 11;
  }
  else {  // extended prefix
    pilot1 = 3;
    pilot2 = 6;
    pilot3 = 9;
  }


  while (!oai_exit){
    vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_PDSCH_THREAD, 0);
    if (pthread_mutex_lock(&rx_pdsch_mutex) != 0) {
      LOG_E(PHY,"[SCHED][RX_PDSCH] error locking mutex.\n");
    }
    else {
      while (rx_pdsch_instance_cnt < 0) {
	pthread_cond_wait(&rx_pdsch_cond,&rx_pdsch_mutex);
      }
      
      if (pthread_mutex_unlock(&rx_pdsch_mutex) != 0) {	
	LOG_E(PHY,"[SCHED][RX_PDSCH] error unlocking mutex.\n");
      }
    }
    
    vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_PDSCH_THREAD, 1);
 
knopp's avatar
   
knopp committed
160
161
    //      last_slot = rx_pdsch_slot;
      subframe = UE->slot_rx>>1;
162
      // Important! assumption that PDCCH procedure of next SF is not called yet
knopp's avatar
   
knopp committed
163
164
165
166
167
168
169
170
171
172
173
      harq_pid = UE->dlsch_ue[eNB_id][0]->current_harq_pid;
      UE->dlsch_ue[eNB_id][0]->harq_processes[harq_pid]->G = get_G(&UE->lte_frame_parms,
                                                                            UE->dlsch_ue[eNB_id][0]->harq_processes[harq_pid]->nb_rb,
                                                                            UE->dlsch_ue[eNB_id][0]->harq_processes[harq_pid]->rb_alloc,
                                                                            get_Qm(UE->dlsch_ue[eNB_id][0]->harq_processes[harq_pid]->mcs),  
									    UE->dlsch_ue[eNB_id][0]->harq_processes[harq_pid]->Nl,
									    UE->lte_ue_pdcch_vars[eNB_id]->num_pdcch_symbols,
                                                                            UE->frame_rx,subframe);

      if ((UE->transmission_mode[eNB_id] == 5) && 
          (UE->dlsch_ue[eNB_id][0]->harq_processes[harq_pid]->dl_power_off==0) &&
174
175
          (openair_daq_vars.use_ia_receiver > 0)) {
	dual_stream_UE = 1;
knopp's avatar
   
knopp committed
176
	eNB_id_i = UE->n_connected_eNB;
177
	if (openair_daq_vars.use_ia_receiver == 2) {
knopp's avatar
   
knopp committed
178
	  i_mod =  get_Qm(((UE->frame_rx%1024)/3)%28);
179
	} else {
knopp's avatar
   
knopp committed
180
	  i_mod =  get_Qm(UE->dlsch_ue[eNB_id][0]->harq_processes[harq_pid]->mcs);
181
	}
182
183
184
185
186
187
188
189
190
      }
      else {
          dual_stream_UE = 0;
          eNB_id_i = eNB_id+1;
          i_mod = 0;
      }

    if (oai_exit) break;

knopp's avatar
   
knopp committed
191
    LOG_D(PHY,"[SCHED][RX_PDSCH] Frame %d, slot %d: Calling rx_pdsch_decoding with harq_pid %d\n",UE->frame_rx,UE->slot_rx,harq_pid);
192
193
194
195

    time_in = rt_get_time_ns();

    // Check if we are in even or odd slot
knopp's avatar
   
knopp committed
196
    if (UE->slot_rx%2) { // odd slots
197
198
199

        // measure time
        //time0 = rt_get_time_ns();
knopp's avatar
   
knopp committed
200
201
        //        rt_printk("[SCHED][RX_PDSCH][before rx_pdsch] Frame %d, slot %d, time %llu\n",UE->frame,last_slot,rt_get_time_ns());
        for (m=pilot2;m<UE->lte_frame_parms.symbols_per_tti;m++) {
202

knopp's avatar
   
knopp committed
203
            rx_pdsch(UE,
204
205
206
207
208
209
210
211
212
213
214
215
                     PDSCH,
                     eNB_id,
                     eNB_id_i,
                     subframe,
                     m,
                     0,
                     dual_stream_UE,
                     i_mod,
                     harq_pid);
            
        }
        //        time1 = rt_get_time_ns();
knopp's avatar
   
knopp committed
216
        //        rt_printk("[SCHED][RX_PDSCH] Frame %d, slot %d, start %llu, end %llu, proc time: %llu ns\n",UE->frame_rx,last_slot,time0,time1,(time1-time0));
217
218
219
220

        dlsch_thread_index = harq_pid;
	
        if (pthread_mutex_lock (&dlsch_mutex[dlsch_thread_index]) != 0) {               // Signal MAC_PHY Scheduler
knopp's avatar
   
knopp committed
221
            LOG_E(PHY,"[UE  %d] ERROR pthread_mutex_lock\n",UE->Mod_id);     // lock before accessing shared resource
222
223
224
225
226
227
228
229
230
            //	vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_UE_RX, VCD_FUNCTION_OUT);
            //return(-1);
        }
        dlsch_instance_cnt[dlsch_thread_index]++;
        dlsch_subframe[dlsch_thread_index] = subframe;
        pthread_mutex_unlock (&dlsch_mutex[dlsch_thread_index]);
        
        if (dlsch_instance_cnt[dlsch_thread_index] == 0) {
            if (pthread_cond_signal(&dlsch_cond[dlsch_thread_index]) != 0) {
knopp's avatar
   
knopp committed
231
                LOG_E(PHY,"[UE  %d] ERROR pthread_cond_signal for dlsch_cond[%d]\n",UE->Mod_id,dlsch_thread_index);
232
233
234
235
236
                //	  vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_UE_RX, VCD_FUNCTION_OUT);
                //return(-1);
            }
        }
        else {
knopp's avatar
   
knopp committed
237
            LOG_W(PHY,"[UE  %d] DLSCH thread for dlsch_thread_index %d busy!!!\n",UE->Mod_id,dlsch_thread_index);
238
239
240
241
242
243
            //	vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_UE_RX, VCD_FUNCTION_OUT);
            //return(-1);
        }
        
    } else { // even slots

knopp's avatar
   
knopp committed
244
        for (m=UE->lte_ue_pdcch_vars[eNB_id]->num_pdcch_symbols;m<pilot2;m++) { 
245

knopp's avatar
   
knopp committed
246
            rx_pdsch(UE,
247
248
249
250
251
                     PDSCH,
                     eNB_id,
                     eNB_id_i,
                     subframe,
                     m,
knopp's avatar
   
knopp committed
252
                     (m==UE->lte_ue_pdcch_vars[eNB_id]->num_pdcch_symbols)?1:0,   // first_symbol_flag
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
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
                     dual_stream_UE,
                     i_mod,
                     harq_pid);
        }
    }    
    
    time_out = rt_get_time_ns();
    
    if (pthread_mutex_lock(&rx_pdsch_mutex) != 0) {
        msg("[openair][SCHED][RX_PDSCH] error locking mutex.\n");
    }
    else {
        rx_pdsch_instance_cnt--;
        
        if (pthread_mutex_unlock(&rx_pdsch_mutex) != 0) {	
            msg("[openair][SCHED][RX_PDSCH] error unlocking mutex.\n");
        }
    }
    
  }

#ifdef HARD_RT
  rt_make_soft_real_time();
#endif

  LOG_D(PHY,"[openair][SCHED][RX_PDSCH] RX_PDSCH thread exiting\n");  

  return 0;
}

int init_rx_pdsch_thread(void) {
  
  int error_code;
  struct sched_param p;

  pthread_mutex_init(&rx_pdsch_mutex,NULL);
  
  pthread_cond_init(&rx_pdsch_cond,NULL);

  pthread_attr_init (&attr_rx_pdsch_thread);
  pthread_attr_setstacksize(&attr_rx_pdsch_thread,OPENAIR_THREAD_STACK_SIZE);
  
  //attr_rx_pdsch_thread.priority = 1;

  p.sched_priority = OPENAIR_THREAD_PRIORITY;
  pthread_attr_setschedparam  (&attr_rx_pdsch_thread, &p);
#ifndef RTAI_ISNT_POSIX
  pthread_attr_setschedpolicy (&attr_rx_pdsch_thread, SCHED_FIFO);
#endif

  rx_pdsch_instance_cnt = -1;
  rt_printk("[openair][SCHED][RX_PDSCH][INIT] Allocating RX_PDSCH thread\n");
  error_code = pthread_create(&rx_pdsch_thread_var,
                              &attr_rx_pdsch_thread,
                              rx_pdsch_thread,
                              0);

  if (error_code!= 0) {
    rt_printk("[openair][SCHED][RX_PDSCH][INIT] Could not allocate rx_pdsch_thread, error %d\n",error_code);
    return(error_code);
  }
  else {
    rt_printk("[openair][SCHED][RX_PDSCH][INIT] Allocate rx_pdsch_thread successful\n");
    return(0);
  }
   
}

void cleanup_rx_pdsch_thread(void) {

  rt_printk("[openair][SCHED][RX_PDSCH] Scheduling rx_pdsch_thread to exit\n");

  rx_pdsch_instance_cnt = 0;
  if (pthread_cond_signal(&rx_pdsch_cond) != 0)
    rt_printk("[openair][SCHED][RX_PDSCH] ERROR pthread_cond_signal\n");
  else
    rt_printk("[openair][SCHED][RX_PDSCH] Signalled rx_pdsch_thread to exit\n");
    
  rt_printk("[openair][SCHED][RX_PDSCH] Exiting ...\n");
  pthread_cond_destroy(&rx_pdsch_cond);
  pthread_mutex_destroy(&rx_pdsch_mutex);
}