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