| 1 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 2 | // File : utils.h |
|---|
| 3 | // Date : 18/10/2013 |
|---|
| 4 | // Author : alain greiner |
|---|
| 5 | // Copyright (c) UPMC-LIP6 |
|---|
| 6 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 7 | // The utils.c and utils.h files are part of the GIET-VM nano-kernel. |
|---|
| 8 | // They define more or less the GIET-VM Hardware Abstraction Layer, |
|---|
| 9 | // and contains various utility functions, that can be used by both the |
|---|
| 10 | // boot code and the kernel code (but not by the user applications). |
|---|
| 11 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 12 | |
|---|
| 13 | #ifndef GIET_UTILS_H |
|---|
| 14 | #define GIET_UTILS_H |
|---|
| 15 | |
|---|
| 16 | #include <mapping_info.h> |
|---|
| 17 | #include <giet_config.h> |
|---|
| 18 | |
|---|
| 19 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 20 | // NULL pointer definition |
|---|
| 21 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 22 | |
|---|
| 23 | #define NULL (void *)0 |
|---|
| 24 | |
|---|
| 25 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 26 | // This structure is used to have one single lock in a cache line |
|---|
| 27 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 28 | |
|---|
| 29 | typedef struct giet_lock_s { unsigned int value; |
|---|
| 30 | unsigned int padding[15]; } giet_lock_t; |
|---|
| 31 | |
|---|
| 32 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 33 | // To access the virtual addresses defined in the giet_vsegs.ld file. |
|---|
| 34 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 35 | |
|---|
| 36 | typedef struct _ld_symbol_s _ld_symbol_t; |
|---|
| 37 | |
|---|
| 38 | extern _ld_symbol_t boot_code_vbase; |
|---|
| 39 | extern _ld_symbol_t boot_data_vbase; |
|---|
| 40 | |
|---|
| 41 | extern _ld_symbol_t kernel_code_vbase; |
|---|
| 42 | extern _ld_symbol_t kernel_data_vbase; |
|---|
| 43 | extern _ld_symbol_t kernel_uncdata_vbase; |
|---|
| 44 | extern _ld_symbol_t kernel_init_vbase; |
|---|
| 45 | |
|---|
| 46 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 47 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 48 | // CP0 registers access functions |
|---|
| 49 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 50 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 51 | |
|---|
| 52 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 53 | // Returns CP0_SCHED register content |
|---|
| 54 | // (virtual base address of the processor scheduler) |
|---|
| 55 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 56 | extern unsigned int _get_sched(void); |
|---|
| 57 | |
|---|
| 58 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 59 | // Returns CP0_EPC register content. |
|---|
| 60 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 61 | extern unsigned int _get_epc(void); |
|---|
| 62 | |
|---|
| 63 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 64 | // Returns CP0_BVAR register content. |
|---|
| 65 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 66 | extern unsigned int _get_bvar(void); |
|---|
| 67 | |
|---|
| 68 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 69 | // Returns CP0_CR register content. |
|---|
| 70 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 71 | extern unsigned int _get_cr(void); |
|---|
| 72 | |
|---|
| 73 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 74 | // Returns CP0_SR register content. |
|---|
| 75 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 76 | extern unsigned int _get_sr(void); |
|---|
| 77 | |
|---|
| 78 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 79 | // Returns CP0_PROCID register content. |
|---|
| 80 | // Processor identifier (12 bits) |
|---|
| 81 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 82 | extern unsigned int _get_procid(void); |
|---|
| 83 | |
|---|
| 84 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 85 | // Returns CP0_TIME register content. |
|---|
| 86 | // Processor local time (32 bits) |
|---|
| 87 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 88 | extern unsigned int _get_proctime(void); |
|---|
| 89 | |
|---|
| 90 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 91 | // Save CP0_SR value to variable pointed by save_sr_ptr and disable IRQs. |
|---|
| 92 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 93 | extern void _it_disable( unsigned int* save_sr_ptr ); |
|---|
| 94 | |
|---|
| 95 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 96 | // Restore CP0_SR register from variable pointed by save_sr_ptr. |
|---|
| 97 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 98 | extern void _it_restore( unsigned int* save_sr_ptr ); |
|---|
| 99 | |
|---|
| 100 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 101 | // Set a new value in CP0_SCHED register. |
|---|
| 102 | // (virtual base address of the processor scheduler) |
|---|
| 103 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 104 | extern void _set_sched(unsigned int value); |
|---|
| 105 | |
|---|
| 106 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 107 | // Set a new value in CP0_SR register. |
|---|
| 108 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 109 | extern void _set_sr(unsigned int value); |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 113 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 114 | // CP2 registers access functions |
|---|
| 115 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 116 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 117 | |
|---|
| 118 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 119 | // Returns CP2_PTPR register value. |
|---|
| 120 | // Page table physical base address for the running context. |
|---|
| 121 | // Contains only the 27 MSB bits, right justified. |
|---|
| 122 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 123 | extern unsigned int _get_mmu_ptpr(void); |
|---|
| 124 | |
|---|
| 125 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 126 | // Returns CP2_MODE register value. |
|---|
| 127 | // MMU current mode, defined by 4 bits, right justified: ITLB/DTLB/ICACHE/DCACHE |
|---|
| 128 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 129 | extern unsigned int _get_mmu_mode(void); |
|---|
| 130 | |
|---|
| 131 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 132 | // Set a new value in CP2_PTPR register. |
|---|
| 133 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 134 | extern void _set_mmu_ptpr(unsigned int value); |
|---|
| 135 | |
|---|
| 136 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 137 | // Set a new value in CP2_MODE register. |
|---|
| 138 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 139 | extern void _set_mmu_mode(unsigned int value); |
|---|
| 140 | |
|---|
| 141 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 142 | // Set a value in CP2_DCACHE_INVAL register. |
|---|
| 143 | // It invalidates the data cache line, if the virtual address defined by the |
|---|
| 144 | // value argument hit in DCACHE. |
|---|
| 145 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 146 | extern void _set_mmu_dcache_inval(unsigned int value); |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 151 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 152 | // Physical addressing functions |
|---|
| 153 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 154 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 155 | |
|---|
| 156 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 157 | // This function makes a physical read access to a 32 bits word in memory, |
|---|
| 158 | // after a temporary DTLB de-activation and paddr extension. |
|---|
| 159 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 160 | extern unsigned int _physical_read( unsigned long long paddr ); |
|---|
| 161 | |
|---|
| 162 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 163 | // This function makes a physical write access to a 32 bits word in memory, |
|---|
| 164 | // after a temporary DTLB de-activation and paddr extension. |
|---|
| 165 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 166 | extern void _physical_write( unsigned long long paddr, |
|---|
| 167 | unsigned int value ); |
|---|
| 168 | |
|---|
| 169 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 170 | // This function makes a physical read access to a 64 bits word in memory, |
|---|
| 171 | // after a temporary DTLB de-activation and paddr extension. |
|---|
| 172 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 173 | extern unsigned long long _physical_read_ull( unsigned long long paddr ); |
|---|
| 174 | |
|---|
| 175 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 176 | // This function makes a physical write access to a 64 bits word in memory, |
|---|
| 177 | // after a temporary DTLB de-activation and paddr extension. |
|---|
| 178 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 179 | extern void _physical_write_ull( unsigned long long paddr, |
|---|
| 180 | unsigned long long value ); |
|---|
| 181 | |
|---|
| 182 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 183 | // This function makes a memcpy from a source buffer to a destination buffer, |
|---|
| 184 | // using physical addresses, after a temporary DTLB de-activation. |
|---|
| 185 | // The source and destination buffers must be word aligned, and size must be |
|---|
| 186 | // multiple of 4 bytes. |
|---|
| 187 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 188 | extern void _physical_memcpy( unsigned long long dst_paddr, |
|---|
| 189 | unsigned long long src_paddr, |
|---|
| 190 | unsigned int size ); |
|---|
| 191 | |
|---|
| 192 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 193 | // This function set a data value in all words of a destination buffer, |
|---|
| 194 | // using physical addresses, after a temporary DTLB de-activation. |
|---|
| 195 | // The destination buffer must be word aligned, and size multiple of 4 bytes. |
|---|
| 196 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 197 | extern void _physical_memset( unsigned long long buf_paddr, |
|---|
| 198 | unsigned int size, |
|---|
| 199 | unsigned int data ); |
|---|
| 200 | |
|---|
| 201 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 202 | // This function is used by several drivers (_xxx_get_register() function). |
|---|
| 203 | // If the MMU is not activated, the virtual address is extended using |
|---|
| 204 | // X_IO and Y_IO to reach the cluster_io. |
|---|
| 205 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 206 | extern unsigned int _io_extended_read( unsigned int* vaddr ); |
|---|
| 207 | |
|---|
| 208 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 209 | // This function is used by all drivers (_xxx_set_register() function) |
|---|
| 210 | // If the MMU is not activated, the virtual address is extended using |
|---|
| 211 | // X_IO and Y_IO to reach the cluster_io. |
|---|
| 212 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 213 | extern void _io_extended_write( unsigned int* vaddr, |
|---|
| 214 | unsigned int value ); |
|---|
| 215 | |
|---|
| 216 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 217 | // Locks access functions |
|---|
| 218 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 222 | // Takes a lock with a blocking ll/sc atomic access. |
|---|
| 223 | // - When the cache coherence is granted by the hardware, |
|---|
| 224 | // the first read is a standard (cacheable) lw, as the local copy |
|---|
| 225 | // can be polled when the lock is already taken by another task, reducing |
|---|
| 226 | // trafic on the interconnect. When the lock is released by the owner task, |
|---|
| 227 | // the local copy is updated or invalidated by the coherence protocol. |
|---|
| 228 | // - If there is no hardware cache coherence a random delay is introduced |
|---|
| 229 | // between two successive retry. |
|---|
| 230 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 231 | extern void _get_lock(giet_lock_t* lock); |
|---|
| 232 | |
|---|
| 233 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 234 | // Release a previouly taken lock. |
|---|
| 235 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 236 | extern void _release_lock(giet_lock_t* lock); |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 240 | // TTY0 access functions |
|---|
| 241 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 242 | |
|---|
| 243 | extern void _puts( char* string ); |
|---|
| 244 | extern void _putx( unsigned int val ); |
|---|
| 245 | extern void _putd( unsigned int val ); |
|---|
| 246 | extern void _putl( unsigned long long val ); |
|---|
| 247 | |
|---|
| 248 | extern void _printf(char* format, ...); |
|---|
| 249 | |
|---|
| 250 | extern void _getc( char* byte ); |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 254 | // Scheduler and task context access functions |
|---|
| 255 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 259 | // Returns index of the currently running task from the processor scheduler. |
|---|
| 260 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 261 | extern unsigned int _get_current_task_id(void); |
|---|
| 262 | |
|---|
| 263 | //////////////////////////////////////////////////////////////////////////////////// |
|---|
| 264 | // This function returns the content of a context slot |
|---|
| 265 | // for a task identified by the ltid argument (local task index), |
|---|
| 266 | // and a processor identified by the (x,y,p) arguments. |
|---|
| 267 | //////////////////////////////////////////////////////////////////////////////////// |
|---|
| 268 | extern unsigned int _get_task_slot( unsigned int x, |
|---|
| 269 | unsigned int y, |
|---|
| 270 | unsigned int p, |
|---|
| 271 | unsigned int ltid, |
|---|
| 272 | unsigned int slot ); |
|---|
| 273 | |
|---|
| 274 | //////////////////////////////////////////////////////////////////////////////////// |
|---|
| 275 | // This function updates the content of a context slot |
|---|
| 276 | // for any task identified by the ltid argument (local task index), |
|---|
| 277 | // and a processor identified by the (x,y,p) arguments. |
|---|
| 278 | //////////////////////////////////////////////////////////////////////////////////// |
|---|
| 279 | extern void _set_task_slot( unsigned int x, |
|---|
| 280 | unsigned int y, |
|---|
| 281 | unsigned int p, |
|---|
| 282 | unsigned int ltid, |
|---|
| 283 | unsigned int slot, |
|---|
| 284 | unsigned int value ); |
|---|
| 285 | |
|---|
| 286 | //////////////////////////////////////////////////////////////////////////////////// |
|---|
| 287 | // This function returns the content of a context slot for the running task. |
|---|
| 288 | //////////////////////////////////////////////////////////////////////////////////// |
|---|
| 289 | extern unsigned int _get_context_slot( unsigned int slot ); |
|---|
| 290 | |
|---|
| 291 | //////////////////////////////////////////////////////////////////////////////////// |
|---|
| 292 | // This function updates the content of a context slot for the running task. |
|---|
| 293 | //////////////////////////////////////////////////////////////////////////////////// |
|---|
| 294 | extern void _set_context_slot( unsigned int slot, |
|---|
| 295 | unsigned int value ); |
|---|
| 296 | |
|---|
| 297 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 298 | // Mapping access functions |
|---|
| 299 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 300 | |
|---|
| 301 | extern mapping_cluster_t * _get_cluster_base(mapping_header_t* header); |
|---|
| 302 | extern mapping_pseg_t * _get_pseg_base(mapping_header_t* header); |
|---|
| 303 | extern mapping_vspace_t * _get_vspace_base(mapping_header_t* header); |
|---|
| 304 | extern mapping_vseg_t * _get_vseg_base(mapping_header_t* header); |
|---|
| 305 | extern mapping_vobj_t * _get_vobj_base(mapping_header_t* header); |
|---|
| 306 | extern mapping_task_t * _get_task_base(mapping_header_t* header); |
|---|
| 307 | extern mapping_proc_t * _get_proc_base(mapping_header_t* header); |
|---|
| 308 | extern mapping_irq_t * _get_irq_base(mapping_header_t* header); |
|---|
| 309 | extern mapping_coproc_t * _get_coproc_base(mapping_header_t* header); |
|---|
| 310 | extern mapping_cp_port_t * _get_cp_port_base(mapping_header_t* header); |
|---|
| 311 | extern mapping_periph_t * _get_periph_base(mapping_header_t* header); |
|---|
| 312 | |
|---|
| 313 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 314 | // Miscelaneous functions |
|---|
| 315 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 316 | |
|---|
| 317 | extern void _exit(void); |
|---|
| 318 | |
|---|
| 319 | extern void _random_wait( unsigned int value ); |
|---|
| 320 | |
|---|
| 321 | extern void _break( char* str); |
|---|
| 322 | |
|---|
| 323 | extern unsigned int _strncmp(const char* s1, |
|---|
| 324 | const char* s2, |
|---|
| 325 | unsigned int n); |
|---|
| 326 | |
|---|
| 327 | extern char* _strcpy( char* dest, |
|---|
| 328 | char* source ); |
|---|
| 329 | |
|---|
| 330 | extern void _dcache_buf_invalidate( unsigned int buf_vbase, |
|---|
| 331 | unsigned int buf_size ); |
|---|
| 332 | |
|---|
| 333 | extern unsigned int _heap_info( unsigned int* vaddr, |
|---|
| 334 | unsigned int* length, |
|---|
| 335 | unsigned int x, |
|---|
| 336 | unsigned int y ); |
|---|
| 337 | |
|---|
| 338 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 339 | // Required by GCC |
|---|
| 340 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 341 | |
|---|
| 342 | extern void* memcpy( void* _dst, |
|---|
| 343 | const void* _src, |
|---|
| 344 | unsigned int size ); |
|---|
| 345 | |
|---|
| 346 | extern void* memset( void* dst, |
|---|
| 347 | int s, |
|---|
| 348 | unsigned int count ); |
|---|
| 349 | |
|---|
| 350 | |
|---|
| 351 | #endif |
|---|
| 352 | |
|---|
| 353 | // Local Variables: |
|---|
| 354 | // tab-width: 4 |
|---|
| 355 | // c-basic-offset: 4 |
|---|
| 356 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
|---|
| 357 | // indent-tabs-mode: nil |
|---|
| 358 | // End: |
|---|
| 359 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
|---|
| 360 | |
|---|