/////////////////////////////////////////////////////////////////////////////////// // File : sys_handler.c // Date : 01/04/2012 // Author : alain greiner and joel porquet // Copyright (c) UPMC-LIP6 /////////////////////////////////////////////////////////////////////////////////// #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if !defined(SEG_BOOT_MAPPING_BASE) # error: You must define SEG_BOOT_MAPPING_BASE in the hard_config.h file #endif #if !defined(NB_TTY_CHANNELS) # error: You must define NB_TTY_CHANNELS in the hard_config.h file #endif #if (NB_TTY_CHANNELS < 1) # error: NB_TTY_CHANNELS cannot be smaller than 1! #endif #if !defined(NB_TIM_CHANNELS) # error: You must define NB_TIM_CHANNELS in the hard_config.h file #endif #if !defined(NB_NIC_CHANNELS) # error: You must define NB_NIC_CHANNELS in the hard_config.h file #endif #if !defined(NB_CMA_CHANNELS) # error: You must define NB_CMA_CHANNELS in the hard_config.h file #endif #if !defined(GIET_NIC_NBUFS) # error: You must define GIET_NIC_NBUFS in the giet_config.h file #endif #if !defined(GIET_NIC_BUFSIZE) # error: You must define GIET_NIC_BUFSIZE in the giet_config.h file #endif #if !defined(GIET_NIC_TIMEOUT) # error: You must define GIET_NIC_TIMEOUT in the giet_config.h file #endif #if !defined(GIET_NO_HARD_CC) # error: You must define GIET_NO_HARD_CC in the giet_config.h file #endif //////////////////////////////////////////////////////////////////////////// // Channel allocators for peripherals // (TTY[0] is reserved for kernel) //////////////////////////////////////////////////////////////////////////// unsigned int _tty_channel_allocator = 1; unsigned int _tim_channel_allocator = 0; unsigned int _cma_channel_allocator = 0; unsigned int _nic_rx_channel_allocator = 0; unsigned int _nic_tx_channel_allocator = 0; //////////////////////////////////////////////////////////////////////////// // NIC_RX and NIC_TX chbuf arrays and associated containers arrays //////////////////////////////////////////////////////////////////////////// spin_lock_t _nic_rx_lock[NB_NIC_CHANNELS] __attribute__((aligned(64))); spin_lock_t _nic_tx_lock[NB_NIC_CHANNELS] __attribute__((aligned(64))); nic_chbuf_t _nic_rx_chbuf[NB_NIC_CHANNELS] __attribute__((aligned(64))); nic_chbuf_t _nic_tx_chbuf[NB_NIC_CHANNELS] __attribute__((aligned(64))); unsigned char _nic_rx_cont[GIET_NIC_BUFSIZE] [GIET_NIC_NBUFS] [NB_NIC_CHANNELS] __attribute__((aligned(64))); unsigned char _nic_tx_cont[GIET_NIC_BUFSIZE] [GIET_NIC_NBUFS] [NB_NIC_CHANNELS] __attribute__((aligned(64))); //////////////////////////////////////////////////////////////////////////// // Initialize the syscall vector with syscall handlers // Note: This array must be synchronised with the define in file stdio.h //////////////////////////////////////////////////////////////////////////// const void * _syscall_vector[64] = { &_sys_proc_xyp, /* 0x00 */ &_get_proctime, /* 0x01 */ &_sys_tty_write, /* 0x02 */ &_sys_tty_read, /* 0x03 */ &_sys_tty_alloc, /* 0x04 */ &_sys_tty_get_lock, /* 0x05 */ &_sys_tty_release_lock, /* 0x06 */ &_sys_heap_info, /* 0x07 */ &_sys_local_task_id, /* 0x08 */ &_sys_global_task_id, /* 0x09 */ &_sys_fbf_cma_alloc, /* 0x0A */ &_sys_fbf_cma_start, /* 0x0B */ &_sys_fbf_cma_display, /* 0x0C */ &_sys_fbf_cma_stop, /* 0x0D */ &_sys_task_exit, /* 0x0E */ &_sys_procs_number, /* 0x0F */ &_sys_fbf_sync_write, /* 0x10 */ &_sys_fbf_sync_read, /* 0x11 */ &_sys_thread_id, /* 0x12 */ &_sys_ukn, /* 0x13 */ &_sys_tim_alloc, /* 0x14 */ &_sys_tim_start, /* 0x15 */ &_sys_tim_stop, /* 0x16 */ &_sys_ukn, /* 0x17 */ &_sys_ukn, /* 0x18 */ &_context_switch, /* 0x19 */ &_sys_vobj_get_vbase, /* 0x1A */ &_sys_vobj_get_length, /* 0x1B */ &_sys_xy_from_ptr, /* 0x1C */ &_sys_ukn, /* 0x1D */ &_sys_ukn, /* 0x1E */ &_sys_ukn, /* 0x1F */ &_fat_user_open, /* 0x20 */ &_fat_user_read, /* 0x21 */ &_fat_user_write, /* 0x22 */ &_fat_user_lseek, /* 0x23 */ &_fat_fstat, /* 0x24 */ &_fat_close, /* 0x25 */ &_sys_ukn, /* 0x26 */ &_sys_ukn, /* 0x27 */ &_sys_ukn, /* 0x28 */ &_sys_ukn, /* 0x29 */ &_sys_ukn, /* 0x2A */ &_sys_ukn, /* 0x2B */ &_sys_ukn, /* 0x2C */ &_sys_ukn, /* 0x2D */ &_sys_ukn, /* 0x2E */ &_sys_ukn, /* 0x2F */ &_sys_nic_alloc, /* 0x30 */ &_sys_nic_start, /* 0x31 */ &_sys_nic_move, /* 0x32 */ &_sys_nic_stop, /* 0x33 */ &_sys_nic_stats, /* 0x34 */ &_sys_nic_clear, /* 0x35 */ &_sys_ukn, /* 0x36 */ &_sys_ukn, /* 0x37 */ &_sys_ukn, /* 0x38 */ &_sys_ukn, /* 0x39 */ &_sys_ukn, /* 0x3A */ &_sys_ukn, /* 0x3B */ &_sys_ukn, /* 0x3C */ &_sys_ukn, /* 0x3D */ &_sys_ukn, /* 0x3E */ &_sys_ukn, /* 0x3F */ }; ////////////////////////////////////////////////////////////////////////////// // TTY related syscall handlers ////////////////////////////////////////////////////////////////////////////// //////////////////// int _sys_tty_alloc() { // get a new TTY terminal index unsigned int channel = _atomic_increment( &_tty_channel_allocator, 1 ); unsigned int thread = _get_context_slot( CTX_TRDID_ID ); unsigned int vspace = _get_context_slot( CTX_VSID_ID ); if ( channel >= NB_TTY_CHANNELS ) { _printf("\n[GIET_ERROR] in _sys_tty_alloc() : not enough TTY channels\n"); return -1; } else { _printf("\n[GIET WARNING] TTY channel %d allocated " " to thread %d in vspace %d\n", channel, thread, vspace ); _set_context_slot( CTX_TTY_ID, channel ); return 0; } } ///////////////////////////////////////////////// int _sys_tty_write( const char* buffer, unsigned int length, // number of characters unsigned int channel) // channel index { unsigned int nwritten; // compute and check tty channel if( channel == 0xFFFFFFFF ) channel = _get_context_slot(CTX_TTY_ID); if( channel >= NB_TTY_CHANNELS ) return -1; // write string to TTY channel for (nwritten = 0; nwritten < length; nwritten++) { // check tty's status if ( _tty_get_register( channel, TTY_STATUS ) & 0x2 ) break; // write one byte if (buffer[nwritten] == '\n') { _tty_set_register( channel, TTY_WRITE, (unsigned int)'\r' ); } _tty_set_register( channel, TTY_WRITE, (unsigned int)buffer[nwritten] ); } return nwritten; } //////////////////////////////////////////////// int _sys_tty_read( char* buffer, unsigned int length, // unused unsigned int channel) // channel index { // compute and check tty channel if( channel == 0xFFFFFFFF ) channel = _get_context_slot(CTX_TTY_ID); if( channel >= NB_TTY_CHANNELS ) return -1; // read one character from TTY channel if (_tty_rx_full[channel] == 0) { return 0; } else { *buffer = _tty_rx_buf[channel]; _tty_rx_full[channel] = 0; return 1; } } /////////////////////////////////////////// int _sys_tty_get_lock( unsigned int channel, unsigned int * save_sr_ptr ) { // compute and check tty channel if( channel == 0xFFFFFFFF ) channel = _get_context_slot(CTX_TTY_ID); if( channel >= NB_TTY_CHANNELS ) return -1; _it_disable( save_sr_ptr ); _sbt_lock_acquire( &_tty_tx_lock[channel] ); return 0; } /////////////////////////////////////////////// int _sys_tty_release_lock( unsigned int channel, unsigned int * save_sr_ptr ) { // compute and check tty channel if( channel == 0xFFFFFFFF ) channel = _get_context_slot(CTX_TTY_ID); if( channel >= NB_TTY_CHANNELS ) return -1; _sbt_lock_release( &_tty_tx_lock[channel] ); _it_restore( save_sr_ptr ); return 0; } ////////////////////////////////////////////////////////////////////////////// // TIM related syscall handlers ////////////////////////////////////////////////////////////////////////////// //////////////////// int _sys_tim_alloc() { // get a new timer index unsigned int channel = _atomic_increment( &_tim_channel_allocator, 1 ); unsigned int thread = _get_context_slot( CTX_TRDID_ID ); unsigned int vspace = _get_context_slot( CTX_VSID_ID ); if ( channel >= NB_TIM_CHANNELS ) { _printf("\n[GIET_ERROR] in _sys_tim_alloc() : not enough TIM channels\n"); return -1; } else { _printf("\n[GIET WARNING] TIM channel %d allocated " " to thread %d in vspace %d\n", channel, thread, vspace ); _set_context_slot( CTX_TIM_ID, channel ); return 0; } } ///////////////////////////////////////// int _sys_tim_start( unsigned int period ) { // get timer index unsigned int channel = _get_context_slot( CTX_TIM_ID ); if ( channel >= NB_TIM_CHANNELS ) { _printf("\n[GIET_ERROR] in _sys_tim_start() : not enough TIM channels\n"); return -1; } // start timer _timer_start( channel, period ); return 0; } /////////////////// int _sys_tim_stop() { // get timer index unsigned int channel = _get_context_slot( CTX_TIM_ID ); if ( channel >= NB_TIM_CHANNELS ) { _printf("\n[GIET_ERROR] in _sys_tim_stop() : illegal timer index\n"); return -1; } // stop timer _timer_stop( channel ); return 0; } ////////////////////////////////////////////////////////////////////////////// // NIC related syscall handlers ////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////// int _sys_nic_alloc( unsigned int is_rx ) { #if GIET_DEBUG_NIC unsigned int thread = _get_context_slot( CTX_TRDID_ID ); unsigned int vspace = _get_context_slot( CTX_VSID_ID ); _printf("\n[GIET DEBUG NIC] Task %d in vspace %d enters sys_nic_alloc()\n", thread, vspace ); #endif unsigned int nic_channel; unsigned int cma_channel; // get a NIC_RX or NIC_TX channel index if ( is_rx ) nic_channel = _atomic_increment( &_nic_rx_channel_allocator, 1 ); else nic_channel = _atomic_increment( &_nic_tx_channel_allocator, 1 ); if ( (nic_channel >= NB_NIC_CHANNELS) ) { _printf("\n[GIET_ERROR] in _sys_nic_alloc() not enough NIC channels\n"); return -1; } // get a CMA channel index cma_channel = _atomic_increment( &_cma_channel_allocator, 1 ); if ( cma_channel >= NB_CMA_CHANNELS ) { _printf("\n[GIET_ERROR] in _sys_nic_alloc() not enough CMA channels\n"); return -1; } // register nic_index and cma_index in task context if ( is_rx ) { _set_context_slot( CTX_NIC_RX_ID, nic_channel ); _set_context_slot( CTX_CMA_RX_ID, cma_channel ); } else { _set_context_slot( CTX_NIC_TX_ID, nic_channel ); _set_context_slot( CTX_CMA_TX_ID, cma_channel ); } #if GIET_DEBUG_NIC _printf("\n[GIET DEBUG NIC] Task %d in vspace %d exit _sys_nic_alloc() : " "NIC channel = %d / CMA channel = %d\n", thread, vspace, nic_channel, cma_channel ); #endif return nic_channel; } // end _sys_nic_alloc() //////////////////////////////////////// int _sys_nic_start( unsigned int is_rx ) { #if GIET_DEBUG_NIC unsigned int thread = _get_context_slot( CTX_TRDID_ID ); unsigned int vspace = _get_context_slot( CTX_VSID_ID ); _printf("\n[GIET DEBUG NIC] Task %d of vspace %d enters _sys_nic_start()\n", thread, vspace ); #endif unsigned int nic_channel; unsigned int cma_channel; // get NIC channel index and CMA channel index if ( is_rx ) { nic_channel = _get_context_slot( CTX_NIC_RX_ID ); cma_channel = _get_context_slot( CTX_CMA_RX_ID ); } else { nic_channel = _get_context_slot( CTX_NIC_TX_ID ); cma_channel = _get_context_slot( CTX_CMA_TX_ID ); } #if GIET_DEBUG_NIC _printf("\n[GIET DEBUG NIC] Task %d of vspace %d in _sys_nic_start()" " get NIC channel = %d / CMA channel = %d\n", thread, vspace, nic_channel, cma_channel ); #endif if ( nic_channel >= NB_NIC_CHANNELS ) { _printf("\n[GIET_ERROR] in _sys_nic_start(): NIC channel not allocated\n"); return -1; } if ( cma_channel >= NB_CMA_CHANNELS ) { _printf("\n[GIET_ERROR] in _sys_nic_start(): CMA channel not allocated\n"); return -1; } unsigned long long nic_chbuf_pbase; // NIC chbuf physical address unsigned long long ker_chbuf_pbase; // kernel chbuf physical address unsigned long long ker_cont_pbase; // kernel container array physical address // These variables are used for V2P translation unsigned int ptab = _get_context_slot(CTX_PTAB_ID); unsigned int ppn; unsigned int flags; unsigned int ko; unsigned int vaddr; // get the NIC chbuf descriptor physical address unsigned int offset; if ( is_rx ) offset = 0x4000; else offset = 0x4010; nic_chbuf_pbase = (((unsigned long long)((X_IO << Y_WIDTH) + Y_IO))<<32) | (SEG_NIC_BASE + (nic_channel<<15) + offset); #if GIET_DEBUG_NIC _printf("\n[GIET DEBUG NIC] Task %d of vspace %d in _sys_nic_start()" " get NIC chbuf paddr = %l\n", thread, vspace, nic_chbuf_pbase ); #endif // compute the kernel chbuf descriptor physical address if ( is_rx ) vaddr = (unsigned int)( &_nic_rx_chbuf[nic_channel] ); else vaddr = (unsigned int)( &_nic_tx_chbuf[nic_channel] ); ko = _v2p_translate( (page_table_t*)ptab, vaddr>>12, &ppn, &flags ); if ( ko ) { _puts("\n[GIET ERROR] in _sys_nic_start() : kernel chbuf unmapped\n"); return -1; } ker_chbuf_pbase = (((unsigned long long)ppn) << 12) | (vaddr & 0x00000FFF); #if GIET_DEBUG_NIC _printf("\n[GIET DEBUG NIC] Task %d of vspace %d in _sys_nic_start()" " get kernel chbuf paddr = %l\n", thread, vspace, ker_chbuf_pbase ); #endif // compute the container array physical address if ( is_rx ) vaddr = ((unsigned int)&_nic_rx_cont[0][0][nic_channel]); else vaddr = ((unsigned int)&_nic_tx_cont[0][0][nic_channel]); ko = _v2p_translate( (page_table_t*)ptab, vaddr>>12, &ppn, &flags ); if ( ko ) { _puts("\n[GIET ERROR] in _sys_nic_start() : containers array unmapped\n"); return -1; } ker_cont_pbase = (((unsigned long long)ppn) << 12) | (vaddr & 0x00000FFF); #if GIET_DEBUG_NIC _printf("\n[GIET DEBUG NIC] Task %d of vspace %d in _sys_nic_start()" " get kernel containers array paddr = %l\n", thread, vspace, ker_cont_pbase ); #endif // initializes the kernel chbuf descriptor unsigned int index; if ( is_rx ) { _spin_lock_init( &_nic_rx_lock[nic_channel] ); _nic_rx_chbuf[nic_channel].index = 0; for( index = 0 ; index < GIET_NIC_NBUFS ; index++ ) { _nic_rx_chbuf[nic_channel].buffer[index] = ker_cont_pbase + (index<<12); } } else { _spin_lock_init( &_nic_tx_lock[nic_channel] ); _nic_tx_chbuf[nic_channel].index = 0; for( index = 0 ; index < GIET_NIC_NBUFS ; index++ ) { _nic_tx_chbuf[nic_channel].buffer[index] = ker_cont_pbase + (index<<12); } } // sync the kernel chbuf in L2 after write in L2 _mmc_sync( ker_chbuf_pbase, sizeof( nic_chbuf_t ) ); // initializes CMA registers defining the source & destination chbufs if ( is_rx ) // NIC to kernel { _cma_set_register( cma_channel, CHBUF_SRC_DESC , (unsigned int)(nic_chbuf_pbase) ); _cma_set_register( cma_channel, CHBUF_SRC_EXT , (unsigned int)(nic_chbuf_pbase>>32) ); _cma_set_register( cma_channel, CHBUF_SRC_NBUFS, 2 ); _cma_set_register( cma_channel, CHBUF_DST_DESC , (unsigned int)(ker_chbuf_pbase) ); _cma_set_register( cma_channel, CHBUF_DST_EXT , (unsigned int)(ker_chbuf_pbase>>32) ); _cma_set_register( cma_channel, CHBUF_DST_NBUFS, GIET_NIC_NBUFS ); } else // kernel to NIC { _cma_set_register( cma_channel, CHBUF_SRC_DESC , (unsigned int)(ker_chbuf_pbase) ); _cma_set_register( cma_channel, CHBUF_SRC_EXT , (unsigned int)(ker_chbuf_pbase>>32) ); _cma_set_register( cma_channel, CHBUF_SRC_NBUFS, GIET_NIC_NBUFS ); _cma_set_register( cma_channel, CHBUF_DST_DESC , (unsigned int)(nic_chbuf_pbase) ); _cma_set_register( cma_channel, CHBUF_DST_EXT , (unsigned int)(nic_chbuf_pbase>>32) ); _cma_set_register( cma_channel, CHBUF_DST_NBUFS, 2 ); } // start CMA transfer _cma_set_register( cma_channel, CHBUF_BUF_SIZE , 4096 ); _cma_set_register( cma_channel, CHBUF_PERIOD , 300 ); _cma_set_register( cma_channel, CHBUF_RUN , 1 ); // activates NIC channel _nic_channel_start( nic_channel, is_rx, GIET_NIC_MAC4, GIET_NIC_MAC2 ); #if GIET_DEBUG_NIC _printf("\n[GIET DEBUG NIC] Task %d of vspace %d exit _sys_nic_start()\n", thread, vspace ); #endif return 0; } // end sys_nic_start() ////////////////////////////////////// int _sys_nic_move( unsigned int is_rx, unsigned int channel, void* buffer ) { #if GIET_DEBUG_NIC unsigned int thread = _get_context_slot( CTX_TRDID_ID ); unsigned int vspace = _get_context_slot( CTX_VSID_ID ); _printf("\n[GIET DEBUG NIC] Task %d of vspace %d enters _sys_nic_move()\n", thread, vspace ); #endif unsigned long long user_buffer_paddr; // user buffer physical address unsigned long long kernel_buffer_paddr; // kernel buffer physical address unsigned long long kernel_chbuf_paddr; // kernel chbuf physical address unsigned long long kernel_buffer_desc; // kernel buffer descriptor // The following variables are used for V2P translation unsigned int ptab = _get_context_slot( CTX_PTAB_ID ); unsigned int ppn; unsigned int flags; unsigned int vaddr; unsigned int ko; // Compute user buffer physical address and check access rights vaddr = (unsigned int)buffer; ko = _v2p_translate( (page_table_t*)ptab, vaddr>>12, &ppn, &flags ); if ( ko ) { _printf("\n[GIET ERROR] in _sys_nic_move() : user buffer unmapped\n"); return -1; } if ( (flags & PTE_U) == 0 ) { _printf("\n[GIET ERROR] in _sys_nic_tx_move() : illegal buffer address\n"); return -1; } user_buffer_paddr = ((unsigned long long)ppn << 12) | (vaddr & 0x00000FFF); #if GIET_DEBUG_NIC _printf("\n[GIET DEBUG NIC] Task %d of vspace %d in _sys_nic_move()" " get user buffer paddr = %l\n", thread, vspace, user_buffer_paddr ); #endif // check NIC channel index if ( channel >= NB_NIC_CHANNELS ) { _printf("\n[GIET_ERROR] in _sys_nic_move() : illegal NIC channel index\n"); return -1; } // get kernel chbuf and lock addresses nic_chbuf_t* chbuf; spin_lock_t* lock; if ( is_rx ) { chbuf = &_nic_rx_chbuf[channel]; lock = &_nic_rx_lock[channel]; } else { chbuf = &_nic_tx_chbuf[channel]; lock = &_nic_tx_lock[channel]; } // compute kernel chbuf physical address (required for sync) vaddr = (unsigned int)chbuf; ko = _v2p_translate( (page_table_t*)ptab, vaddr>>12, &ppn, &flags ); if ( ko ) { _printf("\n[GIET ERROR] in _sys_nic_move() : kernel chbuf unmapped\n"); return -1; } kernel_chbuf_paddr = ((unsigned long long)ppn << 12) | (vaddr & 0x00000FFF); // get the lock protecting the chbuf _spin_lock_acquire( lock ); // polling on the kernel chbuf status unsigned int full; unsigned int ok = 0; unsigned int index = chbuf->index; unsigned int timeout = GIET_NIC_TIMEOUT; while( ok == 0 ) { // inval kernel chbuf in L2 before read in L2 _mmc_inval( kernel_chbuf_paddr, sizeof( nic_chbuf_t ) ); // get kernel buffer descriptor kernel_buffer_desc = chbuf->buffer[index]; #if GIET_DEBUG_NIC _printf("\n[GIET DEBUG NIC] Task %d of vspace %d in _sys_nic_move()" " poll kernel container descriptor = %l\n", thread, vspace, kernel_buffer_desc ); #endif full = kernel_buffer_desc >> 63; if ( (is_rx && full) || ((is_rx == 0) && (full == 0)) ) ok = 1; else timeout--; if ( timeout == 0 ) { _printf("\n[GIET_ERROR] in _sys_nic_move() : timeout\n"); return -1; } } // compute kernel buffer physical address kernel_buffer_paddr = kernel_buffer_desc & 0x0000FFFFFFFFFFFFULL; // move one container, using a physical_memcpy // We must maintain L2/L3 cache coherence if ( is_rx ) { // inval kernel buffer in L2 before read in L2 _mmc_inval( kernel_buffer_paddr, GIET_NIC_BUFSIZE ); // transfer data from kernel buffer to user buffer _physical_memcpy( user_buffer_paddr, kernel_buffer_paddr, GIET_NIC_BUFSIZE ); } else { // transfer data from user buffer to kernel buffer _physical_memcpy( kernel_buffer_paddr, user_buffer_paddr, GIET_NIC_BUFSIZE ); // sync kernel buffer in L2 after write in L2 _mmc_sync( kernel_buffer_paddr, GIET_NIC_BUFSIZE ); } // update kernel chbuf status and index if ( is_rx ) chbuf->buffer[index] = kernel_buffer_paddr & 0x0000FFFFFFFFFFFFULL; else chbuf->buffer[index] = kernel_buffer_paddr | 0x8000000000000000ULL; if ( index == (GIET_NIC_NBUFS - 1) ) chbuf->index = 0; else chbuf->index = index + 1; // sync kernel chbuf in L2 after write in L2 _mmc_sync( kernel_chbuf_paddr, sizeof( nic_chbuf_t ) ); // release the lock protecting the chbuf _spin_lock_release( lock ); #if GIET_DEBUG_NIC _printf("\n[GIET DEBUG NIC] Task %d of vspace %d exit _sys_nic_move()\n", thread, vspace ); #endif return 0; } // end _sys_nic_move() //////////////////////////////////////// int _sys_nic_stop( unsigned int is_rx ) { unsigned int nic_channel; unsigned int cma_channel; // get NIC channel index and CMA channel index if ( is_rx ) { nic_channel = _get_context_slot( CTX_NIC_RX_ID ); cma_channel = _get_context_slot( CTX_CMA_RX_ID ); } else { nic_channel = _get_context_slot( CTX_NIC_TX_ID ); cma_channel = _get_context_slot( CTX_CMA_TX_ID ); } if ( nic_channel >= NB_NIC_CHANNELS ) { _printf("\n[GIET_ERROR] in _sys_nic_start(): NIC channel not allocated\n"); return -1; } if ( cma_channel >= NB_CMA_CHANNELS ) { _printf("\n[GIET_ERROR] in _sys_nic_start(): CMA channel not allocated\n"); return -1; } // desactivates the NIC channel _nic_channel_stop( nic_channel, is_rx ); // desactivates the CMA channel _cma_channel_stop( cma_channel ); return 0; } //////////////////////////////////////// int _sys_nic_clear( unsigned int is_rx ) { unsigned int nic_channel; // get NIC channel if ( is_rx ) nic_channel = _get_context_slot( CTX_NIC_RX_ID ); else nic_channel = _get_context_slot( CTX_NIC_TX_ID ); if ( nic_channel >= NB_NIC_CHANNELS ) { _printf("\n[GIET_ERROR] in _sys_nic_start(): NIC channel not allocated\n"); return -1; } if ( is_rx ) { _nic_set_global_register( NIC_G_NPKT_RX_G2S_RECEIVED , 0 ); _nic_set_global_register( NIC_G_NPKT_RX_DES_TOO_SMALL , 0 ); _nic_set_global_register( NIC_G_NPKT_RX_DES_TOO_BIG , 0 ); _nic_set_global_register( NIC_G_NPKT_RX_DES_MFIFO_FULL , 0 ); _nic_set_global_register( NIC_G_NPKT_RX_DES_CRC_FAIL , 0 ); _nic_set_global_register( NIC_G_NPKT_RX_DISPATCH_RECEIVED , 0 ); _nic_set_global_register( NIC_G_NPKT_RX_DISPATCH_BROADCAST , 0 ); _nic_set_global_register( NIC_G_NPKT_RX_DISPATCH_DST_FAIL , 0 ); _nic_set_global_register( NIC_G_NPKT_RX_DISPATCH_CH_FULL , 0 ); } else { _nic_set_global_register( NIC_G_NPKT_TX_DISPATCH_RECEIVED , 0 ); _nic_set_global_register( NIC_G_NPKT_TX_DISPATCH_TRANSMIT , 0 ); _nic_set_global_register( NIC_G_NPKT_TX_DISPATCH_TOO_BIG , 0 ); _nic_set_global_register( NIC_G_NPKT_TX_DISPATCH_TOO_SMALL , 0 ); _nic_set_global_register( NIC_G_NPKT_TX_DISPATCH_SRC_FAIL , 0 ); _nic_set_global_register( NIC_G_NPKT_TX_DISPATCH_BYPASS , 0 ); _nic_set_global_register( NIC_G_NPKT_TX_DISPATCH_BROADCAST , 0 ); } return 0; } //////////////////////////////////////// int _sys_nic_stats( unsigned int is_rx ) { unsigned int nic_channel; // get NIC channel if ( is_rx ) nic_channel = _get_context_slot( CTX_NIC_RX_ID ); else nic_channel = _get_context_slot( CTX_NIC_TX_ID ); if ( nic_channel >= NB_NIC_CHANNELS ) { _printf("\n[GIET_ERROR] in _sys_nic_start(): NIC channel not allocated\n"); return -1; } if ( is_rx ) { unsigned int received = _nic_get_global_register( NIC_G_NPKT_RX_G2S_RECEIVED ); unsigned int too_small = _nic_get_global_register( NIC_G_NPKT_RX_DES_TOO_SMALL ); unsigned int too_big = _nic_get_global_register( NIC_G_NPKT_RX_DES_TOO_BIG ); unsigned int fifo_full = _nic_get_global_register( NIC_G_NPKT_RX_DES_MFIFO_FULL ); unsigned int crc_fail = _nic_get_global_register( NIC_G_NPKT_RX_DES_CRC_FAIL ); unsigned int transmit = _nic_get_global_register( NIC_G_NPKT_RX_DISPATCH_RECEIVED ); unsigned int broadcast = _nic_get_global_register( NIC_G_NPKT_RX_DISPATCH_BROADCAST ); unsigned int dst_fail = _nic_get_global_register( NIC_G_NPKT_RX_DISPATCH_DST_FAIL ); unsigned int ch_full = _nic_get_global_register( NIC_G_NPKT_RX_DISPATCH_CH_FULL ); _printf("\n### Network Controller RX Statistics ###\n" "- packets received : %d\n" "- packets transmit : %d\n" "- too small : %d\n" "- too big : %d\n" "- fifo full : %d\n" "- crc fail : %d\n" "- dst mac fail : %d\n" "- channel full : %d\n" "- broadcast : %d\n", received, transmit, too_small, too_big, fifo_full, crc_fail, dst_fail, ch_full, broadcast ); } else { unsigned int received = _nic_get_global_register( NIC_G_NPKT_TX_DISPATCH_RECEIVED ); unsigned int transmit = _nic_get_global_register( NIC_G_NPKT_TX_DISPATCH_TRANSMIT ); unsigned int too_big = _nic_get_global_register( NIC_G_NPKT_TX_DISPATCH_TOO_BIG ); unsigned int too_small = _nic_get_global_register( NIC_G_NPKT_TX_DISPATCH_TOO_SMALL ); unsigned int src_fail = _nic_get_global_register( NIC_G_NPKT_TX_DISPATCH_SRC_FAIL ); unsigned int bypass = _nic_get_global_register( NIC_G_NPKT_TX_DISPATCH_BYPASS ); unsigned int broadcast = _nic_get_global_register( NIC_G_NPKT_TX_DISPATCH_BROADCAST ); _printf("\n### Network Controller TX Statistics ###\n" "- packets received : %d\n" "- packets transmit : %d\n" "- too small : %d\n" "- too big : %d\n" "- src mac fail : %d\n" "- bypass : %d\n" "- broadcast : %d\n", received, transmit, too_big, too_small, src_fail, bypass, broadcast ); } return 0; } ///////////////////////////////////////////////////////////////////////////////////////// // FBF related syscall handlers ///////////////////////////////////////////////////////////////////////////////////////// // Array of fbf_chbuf descriptors, indexed by the CMA channel index. __attribute__((section (".unckdata"))) volatile fbf_chbuf_t _fbf_chbuf[NB_CMA_CHANNELS] __attribute__((aligned(32))); // Physical addresses of these fbf_chbuf descriptors (required for L2 cache sync) __attribute__((section (".unckdata"))) unsigned long long _fbf_chbuf_paddr[NB_CMA_CHANNELS]; ///////////////////////////////////////////// int _sys_fbf_sync_write( unsigned int offset, void* buffer, unsigned int length ) { char* fbf_address = (char *)SEG_FBF_BASE + offset; memcpy( fbf_address, buffer, length); return 0; } ///////////////////////////////////////////// int _sys_fbf_sync_read( unsigned int offset, void* buffer, unsigned int length ) { char* fbf_address = (char *)SEG_FBF_BASE + offset; memcpy( buffer, fbf_address, length); return 0; } //////////////////////// int _sys_fbf_cma_alloc() { // get a new CMA channel index unsigned int channel = _atomic_increment( &_cma_channel_allocator, 1 ); unsigned int thread = _get_context_slot( CTX_TRDID_ID ); unsigned int vspace = _get_context_slot( CTX_VSID_ID ); if ( channel >= NB_CMA_CHANNELS ) { _printf("\n[GIET ERROR] in _sys_fbf_cma_alloc() : not enough CMA channels\n"); return -1; } else { _printf("\n[GIET WARNING] FBF_CMA channel %d allocated " " to thread %d in vspace %d\n", channel, thread, vspace ); _set_context_slot( CTX_CMA_FB_ID, channel ); return 0; } } // end sys_fbf_cma_alloc() //////////////////////////////////////////// int _sys_fbf_cma_start( void* vbase0, void* vbase1, unsigned int length ) { #if NB_CMA_CHANNELS > 0 unsigned int ptab; // page table virtual address unsigned int ko; // unsuccessfull V2P translation unsigned int vaddr; // virtual address unsigned int flags; // protection flags unsigned int ppn; // physical page number unsigned long long chbuf_paddr; // physical address of source chbuf descriptor // get channel index unsigned int channel = _get_context_slot( CTX_CMA_FB_ID ); if ( channel >= NB_CMA_CHANNELS ) { _printf("\n[GIET ERROR] in _fbf_cma_start() : CMA channel index too large\n"); return -1; } #if GIET_DEBUG_FBF_CMA _printf("\n[FBF_CMA DEBUG] enters _sys_fbf_cma_start()\n" " - channel = %d\n" " - buf0 vbase = %x\n" " - buf1 vbase = %x\n" " - buffer size = %x\n", channel, (unsigned int)vbase0, (unsigned int)vbase1, length ); #endif // checking user buffers virtual addresses and length alignment if ( ((unsigned int)vbase0 & 0x3) || ((unsigned int)vbase1 & 0x3) || (length & 0x3) ) { _printf("\n[GIET ERROR] in _fbf_cma_start() : user buffer not word aligned\n"); return -1; } // get page table virtual address ptab = _get_context_slot(CTX_PTAB_ID); // compute frame buffer physical address and initialize _fbf_chbuf[channel] vaddr = ((unsigned int)SEG_FBF_BASE); ko = _v2p_translate( (page_table_t*) ptab, (vaddr >> 12), &ppn, &flags ); if (ko) { _printf("\n[GIET ERROR] in _fb_cma_start() : frame buffer unmapped\n"); return -1; } _fbf_chbuf[channel].fbf = ((paddr_t)ppn << 12) | (vaddr & 0x00000FFF); // Compute user buffer 0 physical addresses and intialize _fbf_chbuf[channel] vaddr = (unsigned int)vbase0; ko = _v2p_translate( (page_table_t*) ptab, (vaddr >> 12), &ppn, &flags ); if (ko) { _printf("\n[GIET ERROR] in _fbf_cma_start() : user buffer 0 unmapped\n"); return -1; } if ((flags & PTE_U) == 0) { _printf("\n[GIET ERROR] in _fbf_cma_start() : user buffer 0 not in user space\n"); return -1; } _fbf_chbuf[channel].buf0 = ((paddr_t)ppn << 12) | (vaddr & 0x00000FFF); // Compute user buffer 1 physical addresses and intialize _fbf_chbuf[channel] vaddr = (unsigned int)vbase1; ko = _v2p_translate( (page_table_t*) ptab, (vaddr >> 12), &ppn, &flags ); if (ko) { _printf("\n[GIET ERROR] in _fbf_cma_start() : user buffer 1 unmapped\n"); return -1; } if ((flags & PTE_U) == 0) { _printf("\n[GIET ERROR] in _fbf_cma_start() : user buffer 1 not in user space\n"); return -1; } _fbf_chbuf[channel].buf1 = ((paddr_t)ppn << 12) | (vaddr & 0x00000FFF); // initializes buffer length _fbf_chbuf[channel].length = length; // Compute and register physical adress of the chbuf descriptor vaddr = (unsigned int)(&_fbf_chbuf[channel]); ko = _v2p_translate( (page_table_t*) ptab, (vaddr >> 12), &ppn, &flags ); if (ko) { _printf("\n[GIET ERROR] in _fbf_cma_start() : chbuf descriptor unmapped\n"); return -1; } chbuf_paddr = (((paddr_t)ppn) << 12) | (vaddr & 0x00000FFF); _fbf_chbuf_paddr[channel] = chbuf_paddr; if ( USE_IOB ) { // SYNC request for channel descriptor _mmc_sync( chbuf_paddr, 32 ); } #if GIET_DEBUG_FBF_CMA _printf(" - fbf pbase = %l\n" " - buf0 pbase = %l\n" " - buf1 pbase = %l\n" " - chbuf pbase = %l\n", _fbf_chbuf[channel].fbf, _fbf_chbuf[channel].buf0, _fbf_chbuf[channel].buf1, chbuf_paddr ); #endif // call CMA driver to start transfer _cma_channel_start( channel, chbuf_paddr, 2, chbuf_paddr + 16, 1, length ); return 0; #else _printf("\n[GIET ERROR] in _sys_fbf_cma_start() : NB_CMA_CHANNELS = 0\n"); return -1; #endif } // end _sys_fbf_cma_start() ///////////////////////////////////////////////////// int _sys_fbf_cma_display( unsigned int buffer_index ) { #if NB_CMA_CHANNELS > 0 volatile paddr_t buf_paddr; unsigned int full = 1; // get channel index unsigned int channel = _get_context_slot( CTX_CMA_FB_ID ); if ( channel >= NB_CMA_CHANNELS ) { _printf("\n[GIET ERROR] in _sys_fbf_cma_display() : CMA channel index too large\n"); return -1; } #if GIET_DEBUG_FBF_CMA _printf("\n[FBF_CMA DEBUG] enters _sys_fb_cma_display()\n" " - channel = %d\n" " - buffer = %d\n", channel, buffer_index ); #endif // waiting user buffer empty while ( full ) { if ( USE_IOB ) { // INVAL L1 cache for the chbuf descriptor, _dcache_buf_invalidate( (unsigned int)&_fbf_chbuf[channel], 32 ); // INVAL L2 cache for the chbuf descriptor, _mmc_inval( _fbf_chbuf_paddr[channel], 32 ); } // read user buffer descriptor if ( buffer_index == 0 ) buf_paddr = _fbf_chbuf[channel].buf0; else buf_paddr = _fbf_chbuf[channel].buf1; full = ( (unsigned int)(buf_paddr>>63) ); } if ( USE_IOB ) { // SYNC request for the user buffer, because // it will be read from XRAM by the CMA component _mmc_sync( buf_paddr, _fbf_chbuf[channel].length ); } // set user buffer status if ( buffer_index == 0 ) _fbf_chbuf[channel].buf0 = buf_paddr | 0x8000000000000000ULL; else _fbf_chbuf[channel].buf1 = buf_paddr | 0x8000000000000000ULL; // reset fbf buffer status _fbf_chbuf[channel].fbf = _fbf_chbuf[channel].fbf & 0x7FFFFFFFFFFFFFFFULL; if ( USE_IOB ) { // SYNC request for the channel descriptor, because // it will be read from XRAM by the CMA component _mmc_sync( _fbf_chbuf_paddr[channel], 32 ); } #if GIET_DEBUG_FBF_CMA _printf(" - fbf pbase = %l\n" " - buf0 pbase = %l\n" " - buf1 pbase = %l\n", _fbf_chbuf[channel].fbf, _fbf_chbuf[channel].buf0, _fbf_chbuf[channel].buf1 ); #endif return 0; #else _printf("\n[GIET ERROR] in _sys_fbf_cma_display() : no CMA channel allocated\n"); return -1; #endif } // end _sys_fbf_cma_display() /////////////////////// int _sys_fbf_cma_stop() { #if NB_CMA_CHANNELS > 0 // get channel index unsigned int channel = _get_context_slot( CTX_CMA_FB_ID ); if ( channel >= NB_CMA_CHANNELS ) { _printf("\n[GIET ERROR] in _sys_fbf_cma_stop() : CMA channel index too large\n"); return -1; } // Desactivate CMA channel _cma_channel_stop( channel ); return 0; #else _printf("\n[GIET ERROR] in _sys_fbf_cma_stop() : no CMA channel allocated\n"); return -1; #endif } // end _sys_fbf_cma_stop() ////////////////////////////////////////////////////////////////////////////// // Miscelaneous syscall handlers ////////////////////////////////////////////////////////////////////////////// /////////////// int _sys_ukn() { _printf("\n[GIET ERROR] Undefined System Call / EPC = %x\n", _get_epc() ); return -1; } //////////////////////////////////// int _sys_proc_xyp( unsigned int* x, unsigned int* y, unsigned int* p ) { unsigned int gpid = _get_procid(); // global processor index from CPO register *x = (gpid >> (Y_WIDTH + P_WIDTH)) & ((1<> P_WIDTH) & ((1<> P_WIDTH; unsigned int y = cluster_xy & ((1<> Y_WIDTH; unsigned int lpid = gpid & ((1<vspaces; vspace_id++) { if (_strncmp( vspace[vspace_id].name, vspace_name, 31) == 0) { // scan vobjs for (vobj_id = vspace[vspace_id].vobj_offset; vobj_id < (vspace[vspace_id].vobj_offset + vspace[vspace_id].vobjs); vobj_id++) { if (_strncmp(vobj[vobj_id].name, vobj_name, 31) == 0) { *vbase = vobj[vobj_id].vbase; return 0; } } } } return -1; // not found } ///////////////////////////////////////////////////////// int _sys_vobj_get_length( char* vspace_name, char* vobj_name, unsigned int* length ) { mapping_header_t * header = (mapping_header_t *)SEG_BOOT_MAPPING_BASE; mapping_vspace_t * vspace = _get_vspace_base(header); mapping_vobj_t * vobj = _get_vobj_base(header); unsigned int vspace_id; unsigned int vobj_id; // scan vspaces for (vspace_id = 0; vspace_id < header->vspaces; vspace_id++) { if (_strncmp( vspace[vspace_id].name, vspace_name, 31) == 0) { // scan vobjs for (vobj_id = vspace[vspace_id].vobj_offset; vobj_id < (vspace[vspace_id].vobj_offset + vspace[vspace_id].vobjs); vobj_id++) { if (_strncmp(vobj[vobj_id].name, vobj_name, 31) == 0) { *length = vobj[vobj_id].length; return 0; } } } } return -1; // not found } //////////////////////////////////////// int _sys_xy_from_ptr( void* ptr, unsigned int* x, unsigned int* y ) { unsigned int ret; unsigned int ppn; unsigned int flags; unsigned int vpn = (((unsigned int)ptr)>>12); // get the page table pointer page_table_t* pt = (page_table_t*)_get_context_slot( CTX_PTAB_ID ); // compute the physical address if ( (ret = _v2p_translate( pt, vpn, &ppn, &flags )) ) return -1; *x = (ppn>>24) & 0xF; *y = (ppn>>20) & 0xF; return 0; } ///////////////////////////////////////// int _sys_heap_info( unsigned int* vaddr, unsigned int* length, unsigned int x, unsigned int y ) { mapping_header_t * header = (mapping_header_t *)SEG_BOOT_MAPPING_BASE; mapping_task_t * tasks = _get_task_base(header); mapping_vobj_t * vobjs = _get_vobj_base(header); mapping_vspace_t * vspaces = _get_vspace_base(header); unsigned int task_id; unsigned int vspace_id; unsigned int vobj_id = 0xFFFFFFFF; // searching the heap vobj_id if ( (x < X_SIZE) && (y < Y_SIZE) ) // searching a task in cluster(x,y) { // get vspace global index vspace_id = _get_context_slot(CTX_VSID_ID); // scan all tasks in vspace unsigned int min = vspaces[vspace_id].task_offset ; unsigned int max = min + vspaces[vspace_id].tasks ; for ( task_id = min ; task_id < max ; task_id++ ) { if ( tasks[task_id].clusterid == (x * Y_SIZE + y) ) { vobj_id = tasks[task_id].heap_vobj_id; if ( vobj_id != 0xFFFFFFFF ) break; } } } else // searching in the calling task { task_id = _get_context_slot(CTX_GTID_ID); vobj_id = tasks[task_id].heap_vobj_id; } // analysing the vobj_id if ( vobj_id != 0xFFFFFFFF ) { *vaddr = vobjs[vobj_id].vbase; *length = vobjs[vobj_id].length; return 0; } else { *vaddr = 0; *length = 0; return -1; } } // end _sys_heap_info() // Local Variables: // tab-width: 4 // c-basic-offset: 4 // c-file-offsets:((innamespace . 0)(inline-open . 0)) // indent-tabs-mode: nil // End: // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4