[8961188] | 1 | /* timer.c |
---|
| 2 | * |
---|
| 3 | * This file implements a benchmark timer using the General Purpose Timer on |
---|
| 4 | * the MEC. |
---|
| 5 | * |
---|
| 6 | * COPYRIGHT (c) 1989, 1990, 1991, 1992, 1993, 1994. |
---|
| 7 | * On-Line Applications Research Corporation (OAR). |
---|
| 8 | * All rights assigned to U.S. Government, 1994. |
---|
| 9 | * |
---|
| 10 | * This material may be reproduced by or for the U.S. Government pursuant |
---|
| 11 | * to the copyright license under the clause at DFARS 252.227-7013. This |
---|
| 12 | * notice must appear in all copies of this file and its derivatives. |
---|
| 13 | * |
---|
| 14 | * Ported to ERC32 implementation of the SPARC by On-Line Applications |
---|
| 15 | * Research Corporation (OAR) under contract to the European Space |
---|
| 16 | * Agency (ESA). |
---|
| 17 | * |
---|
| 18 | * ERC32 modifications of respective RTEMS file: COPYRIGHT (c) 1995. |
---|
| 19 | * European Space Agency. |
---|
| 20 | * |
---|
| 21 | * $Id$ |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | #include <assert.h> |
---|
| 25 | |
---|
| 26 | #include <bsp.h> |
---|
| 27 | |
---|
| 28 | rtems_unsigned64 Timer_driver_Start_time; |
---|
| 29 | |
---|
| 30 | rtems_boolean Timer_driver_Find_average_overhead; |
---|
| 31 | |
---|
| 32 | static inline rtems_unsigned64 PPC_Get_timebase_register( void ) |
---|
| 33 | { |
---|
| 34 | rtems_unsigned32 tbr_low; |
---|
| 35 | rtems_unsigned32 tbr_high; |
---|
| 36 | rtems_unsigned32 tbr_high_old; |
---|
| 37 | rtems_unsigned64 tbr; |
---|
| 38 | |
---|
| 39 | do { |
---|
| 40 | asm volatile( "mftbu %0" : "=r" (tbr_high_old)); |
---|
| 41 | asm volatile( "mftb %0" : "=r" (tbr_low)); |
---|
| 42 | asm volatile( "mftbu %0" : "=r" (tbr_high)); |
---|
| 43 | } while ( tbr_high_old != tbr_high ); |
---|
| 44 | |
---|
| 45 | tbr = tbr_high; |
---|
| 46 | tbr <<= 32; |
---|
| 47 | tbr |= tbr_low; |
---|
| 48 | return tbr; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | void Timer_initialize() |
---|
| 52 | { |
---|
| 53 | /* |
---|
| 54 | * Timer runs long and accurate enough not to require an interrupt. |
---|
| 55 | */ |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | Timer_driver_Start_time = PPC_Get_timebase_register(); |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | #define AVG_OVERHEAD 24 /* It typically takes 24 instructions */ |
---|
| 64 | /* to start/stop the timer. */ |
---|
| 65 | #define LEAST_VALID 1 /* Don't trust a value lower than this */ |
---|
| 66 | /* psim can count instructions. :) */ |
---|
| 67 | |
---|
| 68 | int Read_timer() |
---|
| 69 | { |
---|
| 70 | rtems_unsigned64 clicks; |
---|
| 71 | rtems_unsigned64 total64; |
---|
| 72 | rtems_unsigned32 total; |
---|
| 73 | |
---|
| 74 | /* approximately CLOCK_SPEED clicks per microsecond */ |
---|
| 75 | |
---|
| 76 | clicks = PPC_Get_timebase_register(); |
---|
| 77 | |
---|
| 78 | assert( clicks > Timer_driver_Start_time ); |
---|
| 79 | |
---|
| 80 | total64 = clicks - Timer_driver_Start_time; |
---|
| 81 | |
---|
| 82 | assert( total64 <= 0xffffffff ); /* fits into a unsigned32 */ |
---|
| 83 | |
---|
| 84 | total = (rtems_unsigned32) total64; |
---|
| 85 | |
---|
| 86 | if ( Timer_driver_Find_average_overhead == 1 ) |
---|
| 87 | return total; /* in one microsecond units */ |
---|
| 88 | |
---|
| 89 | if ( total < LEAST_VALID ) |
---|
| 90 | return 0; /* below timer resolution */ |
---|
| 91 | |
---|
| 92 | return total - AVG_OVERHEAD; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | rtems_status_code Empty_function( void ) |
---|
| 96 | { |
---|
| 97 | return RTEMS_SUCCESSFUL; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | void Set_find_average_overhead( |
---|
| 101 | rtems_boolean find_flag |
---|
| 102 | ) |
---|
| 103 | { |
---|
| 104 | Timer_driver_Find_average_overhead = find_flag; |
---|
| 105 | } |
---|