Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:58

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  *  @file
0005  *  @brief
0006  *
0007  *  This file implements a benchmark timer using the PPC Timebase Register.
0008  */
0009 
0010 /*
0011  *  COPYRIGHT (c) 1989-2014.
0012  *  On-Line Applications Research Corporation (OAR).
0013  *
0014  * Redistribution and use in source and binary forms, with or without
0015  * modification, are permitted provided that the following conditions
0016  * are met:
0017  * 1. Redistributions of source code must retain the above copyright
0018  *    notice, this list of conditions and the following disclaimer.
0019  * 2. Redistributions in binary form must reproduce the above copyright
0020  *    notice, this list of conditions and the following disclaimer in the
0021  *    documentation and/or other materials provided with the distribution.
0022  *
0023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0026  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0027  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0028  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0029  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0033  * POSSIBILITY OF SUCH DAMAGE.
0034  */
0035 
0036 #include <bsp.h>
0037 #include <rtems.h>
0038 #include <rtems/btimer.h>
0039 #include <assert.h>
0040 #include <libcpu/powerpc-utility.h>
0041 
0042 #ifndef BSP_Convert_decrementer
0043 #define BSP_Convert_decrementer(value) (value)
0044 #endif
0045 
0046 uint64_t   Timer_driver_Start_time;
0047 
0048 bool benchmark_timer_find_average_overhead = false;
0049 unsigned clicks_overhead = 0;
0050 
0051 /*
0052  * Timer Get overhead
0053  */
0054 static int Timer_get_clicks_overhead(void)
0055 {
0056   uint64_t    clicks;
0057 
0058   PPC_Set_timebase_register((uint64_t) 0);
0059   clicks = PPC_Get_timebase_register();
0060   assert(clicks <= 0xffffffff);
0061   clicks_overhead = (unsigned) clicks;
0062   return clicks_overhead;
0063 }
0064 
0065 /*
0066  * benchmark_timer_initialize
0067  */
0068 void benchmark_timer_initialize(void)
0069 {
0070 
0071   /*
0072    *  Timer runs long and accurate enough not to require an interrupt.
0073    */
0074 
0075   if (clicks_overhead == 0) clicks_overhead = Timer_get_clicks_overhead();
0076   PPC_Set_timebase_register((uint64_t) 0);
0077 }
0078 
0079 
0080 /*
0081  *  benchmark_timer_read
0082  */
0083 
0084 benchmark_timer_t benchmark_timer_read(void)
0085 {
0086   uint64_t    total64;
0087   uint32_t    total;
0088 
0089   /* approximately CLOCK_SPEED clicks per microsecond */
0090 
0091   total64 = PPC_Get_timebase_register();
0092 
0093   assert( total64 <= 0xffffffff );  /* fits into a uint32_t   */
0094 
0095   total = (uint32_t) total64;
0096 
0097   if ( benchmark_timer_find_average_overhead == true )
0098     return total;          /* in "clicks" of the decrementer units */
0099 
0100   return (int) BSP_Convert_decrementer(total - clicks_overhead);
0101 }
0102 
0103 void benchmark_timer_disable_subtracting_average_overhead(bool find_flag)
0104 {
0105   benchmark_timer_find_average_overhead = find_flag;
0106 }