New upstream version 16.11.7
[deb_dpdk.git] / lib / librte_eal / common / include / arch / x86 / rte_spinlock.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
34 #ifndef _RTE_SPINLOCK_X86_64_H_
35 #define _RTE_SPINLOCK_X86_64_H_
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 #include "generic/rte_spinlock.h"
42 #include "rte_rtm.h"
43 #include "rte_cpuflags.h"
44 #include "rte_branch_prediction.h"
45 #include "rte_common.h"
46
47 #define RTE_RTM_MAX_RETRIES (10)
48 #define RTE_XABORT_LOCK_BUSY (0xff)
49
50 #ifndef RTE_FORCE_INTRINSICS
51 static inline void
52 rte_spinlock_lock(rte_spinlock_t *sl)
53 {
54         int lock_val = 1;
55         asm volatile (
56                         "1:\n"
57                         "xchg %[locked], %[lv]\n"
58                         "test %[lv], %[lv]\n"
59                         "jz 3f\n"
60                         "2:\n"
61                         "pause\n"
62                         "cmpl $0, %[locked]\n"
63                         "jnz 2b\n"
64                         "jmp 1b\n"
65                         "3:\n"
66                         : [locked] "=m" (sl->locked), [lv] "=q" (lock_val)
67                         : "[lv]" (lock_val)
68                         : "memory");
69 }
70
71 static inline void
72 rte_spinlock_unlock (rte_spinlock_t *sl)
73 {
74         int unlock_val = 0;
75         asm volatile (
76                         "xchg %[locked], %[ulv]\n"
77                         : [locked] "=m" (sl->locked), [ulv] "=q" (unlock_val)
78                         : "[ulv]" (unlock_val)
79                         : "memory");
80 }
81
82 static inline int
83 rte_spinlock_trylock (rte_spinlock_t *sl)
84 {
85         int lockval = 1;
86
87         asm volatile (
88                         "xchg %[locked], %[lockval]"
89                         : [locked] "=m" (sl->locked), [lockval] "=q" (lockval)
90                         : "[lockval]" (lockval)
91                         : "memory");
92
93         return lockval == 0;
94 }
95 #endif
96
97 extern uint8_t rte_rtm_supported;
98
99 static inline int rte_tm_supported(void)
100 {
101         return rte_rtm_supported;
102 }
103
104 static inline int
105 rte_try_tm(volatile int *lock)
106 {
107         int retries;
108
109         if (!rte_rtm_supported)
110                 return 0;
111
112         retries = RTE_RTM_MAX_RETRIES;
113
114         while (likely(retries--)) {
115
116                 unsigned int status = rte_xbegin();
117
118                 if (likely(RTE_XBEGIN_STARTED == status)) {
119                         if (unlikely(*lock))
120                                 rte_xabort(RTE_XABORT_LOCK_BUSY);
121                         else
122                                 return 1;
123                 }
124                 while (*lock)
125                         rte_pause();
126
127                 if ((status & RTE_XABORT_EXPLICIT) &&
128                         (RTE_XABORT_CODE(status) == RTE_XABORT_LOCK_BUSY))
129                         continue;
130
131                 if ((status & RTE_XABORT_RETRY) == 0) /* do not retry */
132                         break;
133         }
134         return 0;
135 }
136
137 static inline void
138 rte_spinlock_lock_tm(rte_spinlock_t *sl)
139 {
140         if (likely(rte_try_tm(&sl->locked)))
141                 return;
142
143         rte_spinlock_lock(sl); /* fall-back */
144 }
145
146 static inline int
147 rte_spinlock_trylock_tm(rte_spinlock_t *sl)
148 {
149         if (likely(rte_try_tm(&sl->locked)))
150                 return 1;
151
152         return rte_spinlock_trylock(sl);
153 }
154
155 static inline void
156 rte_spinlock_unlock_tm(rte_spinlock_t *sl)
157 {
158         if (unlikely(sl->locked))
159                 rte_spinlock_unlock(sl);
160         else
161                 rte_xend();
162 }
163
164 static inline void
165 rte_spinlock_recursive_lock_tm(rte_spinlock_recursive_t *slr)
166 {
167         if (likely(rte_try_tm(&slr->sl.locked)))
168                 return;
169
170         rte_spinlock_recursive_lock(slr); /* fall-back */
171 }
172
173 static inline void
174 rte_spinlock_recursive_unlock_tm(rte_spinlock_recursive_t *slr)
175 {
176         if (unlikely(slr->sl.locked))
177                 rte_spinlock_recursive_unlock(slr);
178         else
179                 rte_xend();
180 }
181
182 static inline int
183 rte_spinlock_recursive_trylock_tm(rte_spinlock_recursive_t *slr)
184 {
185         if (likely(rte_try_tm(&slr->sl.locked)))
186                 return 1;
187
188         return rte_spinlock_recursive_trylock(slr);
189 }
190
191
192 #ifdef __cplusplus
193 }
194 #endif
195
196 #endif /* _RTE_SPINLOCK_X86_64_H_ */