Imported Upstream version 16.04
[deb_dpdk.git] / lib / librte_eal / common / include / arch / x86 / rte_cycles.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 /*   BSD LICENSE
34  *
35  *   Copyright(c) 2013 6WIND.
36  *
37  *   Redistribution and use in source and binary forms, with or without
38  *   modification, are permitted provided that the following conditions
39  *   are met:
40  *
41  *     * Redistributions of source code must retain the above copyright
42  *       notice, this list of conditions and the following disclaimer.
43  *     * Redistributions in binary form must reproduce the above copyright
44  *       notice, this list of conditions and the following disclaimer in
45  *       the documentation and/or other materials provided with the
46  *       distribution.
47  *     * Neither the name of 6WIND S.A. nor the names of its
48  *       contributors may be used to endorse or promote products derived
49  *       from this software without specific prior written permission.
50  *
51  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  */
63
64 #ifndef _RTE_CYCLES_X86_64_H_
65 #define _RTE_CYCLES_X86_64_H_
66
67 #ifdef __cplusplus
68 extern "C" {
69 #endif
70
71 #include "generic/rte_cycles.h"
72
73 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
74 /* Global switch to use VMWARE mapping of TSC instead of RDTSC */
75 extern int rte_cycles_vmware_tsc_map;
76 #include <rte_branch_prediction.h>
77 #endif
78
79 static inline uint64_t
80 rte_rdtsc(void)
81 {
82         union {
83                 uint64_t tsc_64;
84                 struct {
85                         uint32_t lo_32;
86                         uint32_t hi_32;
87                 };
88         } tsc;
89
90 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
91         if (unlikely(rte_cycles_vmware_tsc_map)) {
92                 /* ecx = 0x10000 corresponds to the physical TSC for VMware */
93                 asm volatile("rdpmc" :
94                              "=a" (tsc.lo_32),
95                              "=d" (tsc.hi_32) :
96                              "c"(0x10000));
97                 return tsc.tsc_64;
98         }
99 #endif
100
101         asm volatile("rdtsc" :
102                      "=a" (tsc.lo_32),
103                      "=d" (tsc.hi_32));
104         return tsc.tsc_64;
105 }
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_X86_64_H_ */