]> git.proxmox.com Git - ceph.git/blob - ceph/src/dpdk/lib/librte_eal/common/include/arch/x86/rte_spinlock.h
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / dpdk / 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 if (!rte_rtm_supported)
108 return 0;
109
110 int retries = RTE_RTM_MAX_RETRIES;
111
112 while (likely(retries--)) {
113
114 unsigned int status = rte_xbegin();
115
116 if (likely(RTE_XBEGIN_STARTED == status)) {
117 if (unlikely(*lock))
118 rte_xabort(RTE_XABORT_LOCK_BUSY);
119 else
120 return 1;
121 }
122 while (*lock)
123 rte_pause();
124
125 if ((status & RTE_XABORT_EXPLICIT) &&
126 (RTE_XABORT_CODE(status) == RTE_XABORT_LOCK_BUSY))
127 continue;
128
129 if ((status & RTE_XABORT_RETRY) == 0) /* do not retry */
130 break;
131 }
132 return 0;
133 }
134
135 static inline void
136 rte_spinlock_lock_tm(rte_spinlock_t *sl)
137 {
138 if (likely(rte_try_tm(&sl->locked)))
139 return;
140
141 rte_spinlock_lock(sl); /* fall-back */
142 }
143
144 static inline int
145 rte_spinlock_trylock_tm(rte_spinlock_t *sl)
146 {
147 if (likely(rte_try_tm(&sl->locked)))
148 return 1;
149
150 return rte_spinlock_trylock(sl);
151 }
152
153 static inline void
154 rte_spinlock_unlock_tm(rte_spinlock_t *sl)
155 {
156 if (unlikely(sl->locked))
157 rte_spinlock_unlock(sl);
158 else
159 rte_xend();
160 }
161
162 static inline void
163 rte_spinlock_recursive_lock_tm(rte_spinlock_recursive_t *slr)
164 {
165 if (likely(rte_try_tm(&slr->sl.locked)))
166 return;
167
168 rte_spinlock_recursive_lock(slr); /* fall-back */
169 }
170
171 static inline void
172 rte_spinlock_recursive_unlock_tm(rte_spinlock_recursive_t *slr)
173 {
174 if (unlikely(slr->sl.locked))
175 rte_spinlock_recursive_unlock(slr);
176 else
177 rte_xend();
178 }
179
180 static inline int
181 rte_spinlock_recursive_trylock_tm(rte_spinlock_recursive_t *slr)
182 {
183 if (likely(rte_try_tm(&slr->sl.locked)))
184 return 1;
185
186 return rte_spinlock_recursive_trylock(slr);
187 }
188
189
190 #ifdef __cplusplus
191 }
192 #endif
193
194 #endif /* _RTE_SPINLOCK_X86_64_H_ */