| 1 | /** |
|---|
| 2 | * \file : reset_utils.c |
|---|
| 3 | * \date : August 2012 |
|---|
| 4 | * \author : Cesar Fuguet |
|---|
| 5 | * |
|---|
| 6 | * Definition of utilities functions used by the TSAR pre-loader |
|---|
| 7 | */ |
|---|
| 8 | |
|---|
| 9 | #include <reset_utils.h> |
|---|
| 10 | |
|---|
| 11 | /******************************************************************** |
|---|
| 12 | * proctime() |
|---|
| 13 | * |
|---|
| 14 | * Returns processor local time. |
|---|
| 15 | ********************************************************************/ |
|---|
| 16 | inline unsigned int proctime() |
|---|
| 17 | { |
|---|
| 18 | unsigned int ret; |
|---|
| 19 | asm volatile ("mfc0 %0, $9":"=r" (ret)); |
|---|
| 20 | return ret; |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | /******************************************************************** |
|---|
| 24 | * memcpy( _dst, _src, size ) |
|---|
| 25 | * |
|---|
| 26 | * Transfer data between two memory buffers. |
|---|
| 27 | * |
|---|
| 28 | * \param _dst : Destination buffer base address |
|---|
| 29 | * \param _src : Source buffer base address |
|---|
| 30 | * \param size : Number of bytes to transfer |
|---|
| 31 | * |
|---|
| 32 | ********************************************************************/ |
|---|
| 33 | void * memcpy(void *_dst, const void *_src, unsigned int size) |
|---|
| 34 | { |
|---|
| 35 | unsigned int *dst = _dst; |
|---|
| 36 | const unsigned int *src = _src; |
|---|
| 37 | if ( ! ((unsigned int)dst & 3) && ! ((unsigned int)src & 3) ) |
|---|
| 38 | while (size > 3) |
|---|
| 39 | { |
|---|
| 40 | *dst++ = *src++; |
|---|
| 41 | size -= 4; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | unsigned char *cdst = (unsigned char*) dst; |
|---|
| 45 | unsigned char *csrc = (unsigned char*) src; |
|---|
| 46 | |
|---|
| 47 | while (size--) |
|---|
| 48 | { |
|---|
| 49 | *cdst++ = *csrc++; |
|---|
| 50 | } |
|---|
| 51 | return _dst; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | /******************************************************************** |
|---|
| 55 | * memset( _dst, value, size ) |
|---|
| 56 | * |
|---|
| 57 | * Initialize memory buffers with predefined value. |
|---|
| 58 | * |
|---|
| 59 | * \param _dst : Destination buffer base address |
|---|
| 60 | * \param value : Initialization value |
|---|
| 61 | * \param size : Number of bytes to initialize |
|---|
| 62 | * |
|---|
| 63 | ********************************************************************/ |
|---|
| 64 | void * memset(void *_dst, const int value, unsigned int size) |
|---|
| 65 | { |
|---|
| 66 | char * dst = (char *) _dst; |
|---|
| 67 | |
|---|
| 68 | while(size--) *dst++ = (char) value; |
|---|
| 69 | |
|---|
| 70 | return _dst; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | /******************************************************************** |
|---|
| 74 | * reset_print_elf_phdr( elf_phdr_ptr ) |
|---|
| 75 | * |
|---|
| 76 | * Print some fields of a ELF program header |
|---|
| 77 | * |
|---|
| 78 | * \param elf_phdr_ptr : Pointer to the ELF program header to print |
|---|
| 79 | * |
|---|
| 80 | ********************************************************************/ |
|---|
| 81 | void reset_print_elf_phdr(Elf32_Phdr * elf_phdr_ptr) |
|---|
| 82 | { |
|---|
| 83 | reset_puts("- type : "); |
|---|
| 84 | reset_putx(elf_phdr_ptr->p_type); |
|---|
| 85 | |
|---|
| 86 | reset_puts("\n- offset : "); |
|---|
| 87 | reset_putx(elf_phdr_ptr->p_offset); |
|---|
| 88 | |
|---|
| 89 | reset_puts("\n- vaddr : "); |
|---|
| 90 | reset_putx(elf_phdr_ptr->p_vaddr); |
|---|
| 91 | |
|---|
| 92 | reset_puts("\n- paddr : "); |
|---|
| 93 | reset_putx(elf_phdr_ptr->p_paddr); |
|---|
| 94 | |
|---|
| 95 | reset_puts("\n- filesz : "); |
|---|
| 96 | reset_putx(elf_phdr_ptr->p_filesz); |
|---|
| 97 | |
|---|
| 98 | reset_puts("\n- memsz : "); |
|---|
| 99 | reset_putx(elf_phdr_ptr->p_memsz); |
|---|
| 100 | |
|---|
| 101 | reset_puts("\n- flags : "); |
|---|
| 102 | reset_putx(elf_phdr_ptr->p_flags); |
|---|
| 103 | |
|---|
| 104 | reset_puts("\n- align : "); |
|---|
| 105 | reset_putx(elf_phdr_ptr->p_align); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | /******************************************************************** |
|---|
| 110 | * reset_mcc_inval() |
|---|
| 111 | * |
|---|
| 112 | * Invalidate all data cache lines corresponding to a memory buffer |
|---|
| 113 | * (identified by an address and a size) in L2 cache. |
|---|
| 114 | ********************************************************************/ |
|---|
| 115 | #if USE_IOB |
|---|
| 116 | void reset_mcc_invalidate ( const void * buffer, |
|---|
| 117 | unsigned int size) |
|---|
| 118 | { |
|---|
| 119 | unsigned int * mcc_address = (unsigned int *)MCC_PADDR_BASE; |
|---|
| 120 | |
|---|
| 121 | // get the hard lock assuring exclusive access to MEMC |
|---|
| 122 | while (ioread32(&mcc_address[MCC_LOCK])); |
|---|
| 123 | |
|---|
| 124 | // write invalidate paremeters on the memory cache this preloader |
|---|
| 125 | // use only the cluster 0 and then the HI bits are not used |
|---|
| 126 | |
|---|
| 127 | iowrite32(&mcc_address[MCC_ADDR_LO], (unsigned int) buffer); |
|---|
| 128 | iowrite32(&mcc_address[MCC_ADDR_HI], (unsigned int) 0); |
|---|
| 129 | iowrite32(&mcc_address[MCC_LENGTH] , (unsigned int) size); |
|---|
| 130 | iowrite32(&mcc_address[MCC_CMD] , (unsigned int) MCC_CMD_INVAL); |
|---|
| 131 | |
|---|
| 132 | // release the lock protecting MEMC |
|---|
| 133 | iowrite32(&mcc_address[MCC_LOCK], (unsigned int) 0); |
|---|
| 134 | } |
|---|
| 135 | #endif |
|---|
| 136 | |
|---|
| 137 | /******************************************************************** |
|---|
| 138 | * reset_dcache_buf_invalidate() |
|---|
| 139 | * |
|---|
| 140 | * Invalidate all data cache lines corresponding to a memory buffer |
|---|
| 141 | * (identified by an address and a size) in L1 cache and L2 cache. |
|---|
| 142 | ********************************************************************/ |
|---|
| 143 | #if (CACHE_COHERENCE == 0) || USE_IOB |
|---|
| 144 | void reset_buf_invalidate ( const void * buffer, |
|---|
| 145 | unsigned int line_size, |
|---|
| 146 | unsigned int size) |
|---|
| 147 | { |
|---|
| 148 | unsigned int i; |
|---|
| 149 | |
|---|
| 150 | // iterate on cache lines |
|---|
| 151 | for (i = 0; i <= size; i += line_size) |
|---|
| 152 | { |
|---|
| 153 | asm volatile( |
|---|
| 154 | " cache %0, %1" |
|---|
| 155 | :// no outputs |
|---|
| 156 | :"i" (0x11), "R" (*((unsigned char *) buffer + i)) |
|---|
| 157 | ); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | #if USE_IOB |
|---|
| 161 | reset_mcc_invalidate(buffer, count * 512); |
|---|
| 162 | #endif |
|---|
| 163 | } |
|---|
| 164 | #endif |
|---|
| 165 | |
|---|
| 166 | // vim: tabstop=4 : softtabstop=4 : shiftwidth=4 : expandtab |
|---|