Imported Upstream version 16.04
[deb_dpdk.git] / lib / librte_eal / common / include / arch / arm / rte_cycles_32.h
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015 RehiveTech. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of RehiveTech nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #ifndef _RTE_CYCLES_ARM32_H_
34 #define _RTE_CYCLES_ARM32_H_
35
36 /* ARM v7 does not have suitable source of clock signals. The only clock counter
37    available in the core is 32 bit wide. Therefore it is unsuitable as the
38    counter overlaps every few seconds and probably is not accessible by
39    userspace programs. Therefore we use clock_gettime(CLOCK_MONOTONIC_RAW) to
40    simulate counter running at 1GHz.
41 */
42
43 #include <time.h>
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 #include "generic/rte_cycles.h"
50
51 /**
52  * Read the time base register.
53  *
54  * @return
55  *   The time base for this lcore.
56  */
57 #ifndef RTE_ARM_EAL_RDTSC_USE_PMU
58
59 /**
60  * This call is easily portable to any ARM architecture, however,
61  * it may be damn slow and inprecise for some tasks.
62  */
63 static inline uint64_t
64 __rte_rdtsc_syscall(void)
65 {
66         struct timespec val;
67         uint64_t v;
68
69         while (clock_gettime(CLOCK_MONOTONIC_RAW, &val) != 0)
70                 /* no body */;
71
72         v  = (uint64_t) val.tv_sec * 1000000000LL;
73         v += (uint64_t) val.tv_nsec;
74         return v;
75 }
76 #define rte_rdtsc __rte_rdtsc_syscall
77
78 #else
79
80 /**
81  * This function requires to configure the PMCCNTR and enable
82  * userspace access to it:
83  *
84  *      asm volatile("mcr p15, 0, %0, c9, c14, 0" : : "r"(1));
85  *      asm volatile("mcr p15, 0, %0, c9, c12, 0" : : "r"(29));
86  *      asm volatile("mcr p15, 0, %0, c9, c12, 1" : : "r"(0x8000000f));
87  *
88  * which is possible only from the priviledged mode (kernel space).
89  */
90 static inline uint64_t
91 __rte_rdtsc_pmccntr(void)
92 {
93         unsigned tsc;
94         uint64_t final_tsc;
95
96         /* Read PMCCNTR */
97         asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r"(tsc));
98         /* 1 tick = 64 clocks */
99         final_tsc = ((uint64_t)tsc) << 6;
100
101         return (uint64_t)final_tsc;
102 }
103 #define rte_rdtsc __rte_rdtsc_pmccntr
104
105 #endif /* RTE_ARM_EAL_RDTSC_USE_PMU */
106
107 static inline uint64_t
108 rte_rdtsc_precise(void)
109 {
110         rte_mb();
111         return rte_rdtsc();
112 }
113
114 static inline uint64_t
115 rte_get_tsc_cycles(void) { return rte_rdtsc(); }
116
117 #ifdef __cplusplus
118 }
119 #endif
120
121 #endif /* _RTE_CYCLES_ARM32_H_ */