init_top.c 13.3 KB
Newer Older
ghaddab's avatar
ghaddab committed
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
/*******************************************************************************
    OpenAirInterface 
    Copyright(c) 1999 - 2014 Eurecom

    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.


    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.

    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/>.

  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
ghaddab's avatar
ghaddab committed
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
/*!\brief Initilization and reconfiguration routines for LTE PHY */
#ifndef USER_MODE
#define __NO_VERSION__
#endif
 
#include "defs.h"
#include "PHY/extern.h"
#include "MAC_INTERFACE/extern.h"
//#include "ARCH/CBMIMO1/DEVICE_DRIVER/extern.h"

#ifdef CBMIMO1
#include "ARCH/COMMON/defs.h"
#include "ARCH/CBMIMO1/DEVICE_DRIVER/from_grlib_softconfig.h"
#include "ARCH/CBMIMO1/DEVICE_DRIVER/cbmimo1_device.h"
#include "ARCH/CBMIMO1/DEVICE_DRIVER/defs.h"
#include "ARCH/CBMIMO1/DEVICE_DRIVER/extern.h"
#include "ARCH/CBMIMO1/DEVICE_DRIVER/cbmimo1_pci.h"
//#include "pci_commands.h"
#endif //CBMIMO1

/*!
* @addtogroup _PHY_STRUCTURES_
* Memory Initializaion and Cleanup for LTE MODEM.
* @{
\section _Memory_init_ Memory Initialization for LTE MODEM
54

55 56
*/

57
//#define DEBUG_PHY
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 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

/*
#ifndef USER_MODE
#include "SCHED/defs.h"
#endif //USER_MODE
*/


#ifndef USER_MODE

dma_addr_t dma_handle[4];

// Get from HW addresses
int init_signal_buffers(LTE_DL_FRAME_PARMS *frame_parms) {

  unsigned char card_id,i;

  int *tmp_ptr;
  mod_sym_t *tmp_ptr_tx;
  unsigned int tx_dma_buffer_size_bytes;

  for (card_id=0;card_id<number_of_cards;card_id++) {
    for (i=0;i<NB_ANTENNAS_TX;i++) {
      
      // Allocate memory for TX DMA Buffer
      
#ifdef IFFT_FPGA
      tx_dma_buffer_size_bytes = NUMBER_OF_USEFUL_CARRIERS*NUMBER_OF_SYMBOLS_PER_FRAME*sizeof(mod_sym_t);
#else
#ifdef BIT8_TX
      tx_dma_buffer_size_bytes = FRAME_LENGTH_BYTES>>1;
#else
      tx_dma_buffer_size_bytes = FRAME_LENGTH_BYTES;
#endif
#endif
      
      tmp_ptr_tx = (mod_sym_t *)bigmalloc16(tx_dma_buffer_size_bytes+2*PAGE_SIZE);
      
      if (tmp_ptr_tx==NULL) {
	msg("[PHY][INIT] Could not allocate TX_DMA %d (%x bytes)\n",i, 
	    (unsigned int)(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(mod_sym_t) + 2*PAGE_SIZE));
	return(-1);
      }
      else {
	bzero(tmp_ptr_tx,tx_dma_buffer_size_bytes+2*PAGE_SIZE);
	pci_buffer[card_id][(2*i)] = (unsigned int)tmp_ptr_tx;
	tmp_ptr_tx = (mod_sym_t*)(((unsigned int)tmp_ptr_tx + PAGE_SIZE -1) & PAGE_MASK);
	//      reserve_mem(tmp_ptr_tx,FRAME_LENGTH_BYTES+2*PAGE_SIZE);
#ifdef DEBUG_PHY
	msg("[PHY][INIT] TX_DMA_BUFFER %d at %p (%p), size 0x%x\n",i,
	    (void *)tmp_ptr_tx,
	    (void *)virt_to_phys(tmp_ptr_tx),
	    (unsigned int)(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(mod_sym_t)+2*PAGE_SIZE));
#endif
      }
      
      
      TX_DMA_BUFFER[card_id][i] = (int) tmp_ptr_tx;
    }
      
    for (i=0;i<NB_ANTENNAS_RX;i++) {
      // RX DMA Buffers
      tmp_ptr = (int *)bigmalloc16(FRAME_LENGTH_BYTES+OFDM_SYMBOL_SIZE_BYTES+2*PAGE_SIZE);
      
      if (tmp_ptr==NULL) {
#ifdef DEBUG_PHY
	msg("[PHY][INIT] Could not allocate RX_DMA %d (%x bytes)\n",i, 
	    FRAME_LENGTH_BYTES+2*OFDM_SYMBOL_SIZE_BYTES + 2*PAGE_SIZE);
#endif
	return(-1);
      }
      else {
	bzero(tmp_ptr,FRAME_LENGTH_BYTES+OFDM_SYMBOL_SIZE_BYTES+2*PAGE_SIZE);
	pci_buffer[card_id][1+(2*i)] = (int) tmp_ptr;
	tmp_ptr = (int*) (((unsigned long)tmp_ptr + PAGE_SIZE -1) & PAGE_MASK);
	//          reserve_mem(tmp_ptr,FRAME_LENGTH_BYTES+2*PAGE_SIZE);
	
#ifdef DEBUG_PHY
	msg("[PHY][INIT] RX_DMA_BUFFER %d at %p (%p), size 0x%x\n",i,
	    (void *)tmp_ptr,
	    (void *)virt_to_phys(tmp_ptr),(unsigned int)(FRAME_LENGTH_BYTES+OFDM_SYMBOL_SIZE_BYTES+2*PAGE_SIZE));
#endif
      }
      
      
      RX_DMA_BUFFER[card_id][i] = (int) tmp_ptr;
    }
        
  }    

#ifndef NOCARD_TEST
  for (card_id=0;card_id<number_of_cards;card_id++) {
    // Allocate memory for PCI interface and store pointers to dma buffers

    if (vid != XILINX_VENDOR) {
      msg("[PHY][INIT] Setting up Leon PCI interface structure\n");
      pci_interface[card_id] = (PCI_interface_t *)bigmalloc16(sizeof(PCI_interface_t));
      msg("[PHY][INIT] PCI interface %d at %p\n",card_id,pci_interface[card_id]);
      openair_writel(pdev[card_id],FROM_GRLIB_CFG_GRPCI_EUR_CTRL0_OFFSET+4,(unsigned int)virt_to_phys((volatile void*)pci_interface[card_id]));  
      
      for (i=0;i<NB_ANTENNAS_RX;i++) {
	pci_interface[card_id]->adc_head[i] = (unsigned int)virt_to_phys((volatile void*)RX_DMA_BUFFER[card_id][i]);
	pci_interface[card_id]->dac_head[i] = (unsigned int)virt_to_phys((volatile void*)TX_DMA_BUFFER[card_id][i]);
      }

      mbox = (unsigned int)(&pci_interface[0]->adac_cnt);
      msg("[PHY][INIT] mbox = %p\n",mbox);
    }
    else {
      msg("[PHY][INIT] PCIe interface %d at %p\n",card_id,exmimo_pci_interface);
      //      openair_writel(pdev[card_id],FROM_GRLIB_CFG_GRPCI_EUR_CTRL0_OFFSET+4,(unsigned int)virt_to_phys((volatile void*)pci_interface[card_id]));  
      DAQ_MBOX = (unsigned int)bigmalloc16(16);
      exmimo_pci_interface->rf.mbox        = (unsigned int)virt_to_phys((volatile void*)DAQ_MBOX);
      
      for (i=0;i<NB_ANTENNAS_RX;i++) {
	exmimo_pci_interface->rf.adc_head[i] = (unsigned int)virt_to_phys((volatile void*)RX_DMA_BUFFER[card_id][i]);
	exmimo_pci_interface->rf.dac_head[i] = (unsigned int)virt_to_phys((volatile void*)TX_DMA_BUFFER[card_id][i]);
      }
    }
  }
#endif //NOCARD_TEST

  return(0);

}
#endif // USER_MODE
/*
int init_frame_parms(LTE_DL_FRAME_PARMS *frame_parms) {

  if (frame_parms->Ncp==1) {
    frame_parms->nb_prefix_samples0=512;
    frame_parms->nb_prefix_samples = 512;
    frame_parms->symbols_per_tti = 12;
  }
  else {
    frame_parms->nb_prefix_samples0 = 160;
    frame_parms->nb_prefix_samples = 144;
    frame_parms->symbols_per_tti = 14;
  }
  
  switch (frame_parms->N_RB_DL) {
  case 100:
    frame_parms->ofdm_symbol_size = 2048;
    frame_parms->log2_symbol_size = 11;
    frame_parms->samples_per_tti = 30720;
    frame_parms->first_carrier_offset = 1448;
    break;
  case 50:
    frame_parms->ofdm_symbol_size = 1024;
    frame_parms->log2_symbol_size = 10;
    frame_parms->samples_per_tti = 15360;
    frame_parms->first_carrier_offset = 724; 
    frame_parms->nb_prefix_samples>>=1;
    frame_parms->nb_prefix_samples0>>=1;
   break;
  case 25:
    frame_parms->ofdm_symbol_size = 512;
    frame_parms->log2_symbol_size = 9;
    frame_parms->samples_per_tti = 7680;
    frame_parms->first_carrier_offset = 362;
    frame_parms->nb_prefix_samples>>=2;
    frame_parms->nb_prefix_samples0>>=2;
    break;
  case 15:
    frame_parms->ofdm_symbol_size = 256;
    frame_parms->log2_symbol_size = 8;
    frame_parms->samples_per_tti = 3840;
    frame_parms->first_carrier_offset = 166;
    frame_parms->nb_prefix_samples>>=3;
    frame_parms->nb_prefix_samples0>>=1;
    break;
  case 6:
    frame_parms->ofdm_symbol_size = 128;
    frame_parms->log2_symbol_size = 7;
    frame_parms->samples_per_tti = 1920;
    frame_parms->first_carrier_offset = 92;
    frame_parms->nb_prefix_samples>>=4;
    frame_parms->nb_prefix_samples0>>=1;
    break;

  default:
    msg("init_frame_parms: Error: Number of resource blocks (N_RB_DL %d) undefined, frame_parms = %p \n",frame_parms->N_RB_DL, frame_parms);
    return(-1);
    break;
  }

  //  frame_parms->tdd_config=3;
  return(0);
}
*/

int phy_init_top(LTE_DL_FRAME_PARMS *frame_parms) {

  // bzero((void *)PHY_vars,sizeof(PHY_VARS));
  LOG_I(PHY,"[INIT] OFDM size             : %d\n",NUMBER_OF_OFDM_CARRIERS);
  LOG_I(PHY,"[INIT] FRAME_LENGTH_SAMPLES  : %d\n",FRAME_LENGTH_SAMPLES);
  LOG_I(PHY,"[INIT] NUMBER_OF_SYMBOLS_PER_FRAME  : %d\n",NUMBER_OF_SYMBOLS_PER_FRAME);
  LOG_I(PHY,"[INIT] Initializing FFT engine using %d point fft (%d, %p)\n",NUMBER_OF_OFDM_CARRIERS,LOG2_NUMBER_OF_OFDM_CARRIERS,rev );

#ifndef USER_MODE
  init_signal_buffers(frame_parms);
#endif
   
  // Initialize fft variables
  init_fft(NUMBER_OF_OFDM_CARRIERS,LOG2_NUMBER_OF_OFDM_CARRIERS,rev);   // TX/RX
  init_fft(4*NUMBER_OF_OFDM_CARRIERS,2+LOG2_NUMBER_OF_OFDM_CARRIERS,rev_times4);   // Synch
  init_fft(NUMBER_OF_OFDM_CARRIERS/2,LOG2_NUMBER_OF_OFDM_CARRIERS-1,rev_half);   // for interpolation of channel est

  init_fft(8192,13,rev8192); 
  init_fft(4096,12,rev4096);
  init_fft(2048,11,rev2048);
  init_fft(1024,10,rev1024);
  init_fft(512,9,rev512);
  
  twiddle_fft = (short *)malloc16(4095*4*2);
  twiddle_ifft = (short *)malloc16(4095*4*2);
  twiddle_fft_times4 = (short*)malloc16(4095*4*2);
  twiddle_ifft_times4 = (short*)malloc16(4095*4*2);
  twiddle_fft_half = (short*)malloc16(4095*4*2);
  twiddle_ifft_half = (short*)malloc16(4095*4*2);

  LOG_I(PHY,"[INIT] twiddle_fft= %p, twiddle_ifft=%p, twiddle_fft_times4=%p,twiddle_ifft_times4=%p\n",
	 (void *)twiddle_fft,(void *)twiddle_ifft,(void *)twiddle_fft_times4,(void *)twiddle_ifft_times4);

  switch (NUMBER_OF_OFDM_CARRIERS) {
	  
  case 64:
    memcpy(twiddle_fft,&twiddle_fft64[0],63*4*2);
    memcpy(twiddle_ifft,&twiddle_ifft64[0],63*4*2);
    memcpy(twiddle_fft_times4,&twiddle_fft256[0],255*4*2);
    memcpy(twiddle_ifft_times4,&twiddle_ifft256[0],255*4*2);
    //memcpy(twiddle_fft_half,&twiddle_fft32[0],31*4*2);
    //memcpy(twiddle_ifft_half,&twiddle_ifft32[0],31*4*2);
    break;
  case 128:
    memcpy(twiddle_fft,&twiddle_fft128[0],127*4*2);
    memcpy(twiddle_ifft,&twiddle_ifft128[0],127*4*2);
    memcpy(twiddle_fft_times4,&twiddle_fft512[0],511*4*2);
    memcpy(twiddle_ifft_times4,&twiddle_ifft512[0],511*4*2);
    memcpy(twiddle_fft_half,&twiddle_fft64[0],63*4*2);
    memcpy(twiddle_ifft_half,&twiddle_ifft64[0],63*4*2);
    break;
  case 256:
    memcpy(twiddle_fft,&twiddle_fft256[0],255*4*2);
    memcpy(twiddle_ifft,&twiddle_ifft256[0],255*4*2);
    memcpy(twiddle_fft_times4,&twiddle_fft1024[0],1023*4*2);
    memcpy(twiddle_ifft_times4,&twiddle_ifft1024[0],1023*4*2);
    memcpy(twiddle_fft_half,&twiddle_fft128[0],127*4*2);
    memcpy(twiddle_ifft_half,&twiddle_ifft128[0],127*4*2);
    break;
  case 512:
    memcpy(twiddle_fft,&twiddle_fft512[0],511*4*2);
    memcpy(twiddle_ifft,&twiddle_ifft512[0],511*4*2);
    memcpy(twiddle_fft_times4,&twiddle_fft2048[0],2047*4*2);
    memcpy(twiddle_ifft_times4,&twiddle_ifft2048[0],2047*4*2);
    memcpy(twiddle_fft_half,&twiddle_fft256[0],255*4*2);
    memcpy(twiddle_ifft_half,&twiddle_ifft256[0],255*4*2);
    break;
  case 1024:
    memcpy(twiddle_fft,&twiddle_fft1024[0],1023*4*2);
    memcpy(twiddle_ifft,&twiddle_ifft1024[0],1023*4*2);
    memcpy(twiddle_fft_times4,&twiddle_fft4096[0],4095*4*2);
    memcpy(twiddle_ifft_times4,&twiddle_ifft4096[0],4095*4*2);
    memcpy(twiddle_fft_half,&twiddle_fft512[0],511*4*2);
    memcpy(twiddle_ifft_half,&twiddle_ifft512[0],511*4*2);
    break;
  case 2048:
    memcpy(twiddle_fft,&twiddle_fft2048[0],2047*4*2);
    memcpy(twiddle_ifft,&twiddle_ifft2048[0],2047*4*2);
    memcpy(twiddle_fft_times4,&twiddle_fft8192[0],8191*4*2);
    memcpy(twiddle_ifft_times4,&twiddle_ifft8192[0],8191*4*2);
    memcpy(twiddle_fft_half,&twiddle_fft1024[0],1023*4*2);
    memcpy(twiddle_ifft_half,&twiddle_ifft1024[0],1023*4*2);
    break;
  default:
    memcpy(twiddle_fft,&twiddle_fft64[0],63*4*2);
    memcpy(twiddle_ifft,&twiddle_ifft64[0],63*4*2);
    memcpy(twiddle_fft_times4,&twiddle_fft256[0],255*4*2);
    memcpy(twiddle_ifft_times4,&twiddle_ifft256[0],255*4*2);
    //memcpy(twiddle_fft_half,&twiddle_fft32[0],31*4*2);
    //memcpy(twiddle_ifft_half,&twiddle_ifft32[0],31*4*2);
    break;
  }

  frame_parms->twiddle_fft      = twiddle_fft;
  frame_parms->twiddle_ifft     = twiddle_ifft;
  frame_parms->rev              = rev;

  return(1);
}

void phy_cleanup(void) {

#ifndef USER_MODE
  unsigned int dummy_ptr;
  unsigned char card_id,i;
#endif //USER_MODE

  // stop PHY_thread


  LOG_I(PHY,"[INIT] cleanup\n");

  
#ifndef USER_MODE

  for (card_id=0;card_id<number_of_cards;card_id++) {


    
    for (i=0;i<NB_ANTENNAS_RX;i++) {
      
      if (pci_buffer[card_id][2*i]) {
#ifdef DEBUG_PHY    
	msg("[openair][PHY][INIT] pci_buffer card %d %d\n",card_id,2*i);
#endif    
	bigfree(pci_buffer[card_id][2*i],FRAME_LENGTH_BYTES+2*PAGE_SIZE);
	//      free_pages(pci_buffer[2*i],8);
#ifdef DEBUG_PHY    
	msg("[openair][PHY][INIT] Freed TX_DMA_BUFFER %d\n",i);
#endif
      }

      if (pci_buffer[card_id][1+(2*i)]) {
#ifdef DEBUG_PHY    
	msg("[openair][PHY][INIT] pci_buffer %d %d\n",card_id,1+(2*i));
#endif
	dummy_ptr = virt_to_phys(pci_buffer[card_id][1+(2*i)]);
	bigfree(pci_buffer[card_id][1+(2*i)],FRAME_LENGTH_BYTES+2*PAGE_SIZE);
	
	
	//      free_pages(pci_buffer[1+(2*i)],8);
#ifdef DEBUG_PHY    
	msg("[openair][PHY][INIT] Freed RX_DMA_BUFFER %d\n",i);
#endif
      }
    }
  }  

#else
      // Do USER_MODE cleanup here
#endif // USER_MODE
  msg("[openair][CLEANUP] Done!\n");
}


/*
 * @}*/