Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2020 Intel Corporation
3 : : */
4 : :
5 : : #include <stdalign.h>
6 : :
7 : : #include <rte_common.h>
8 : : #include <rte_lcore.h>
9 : : #include <rte_lcore_var.h>
10 : : #include <rte_rtm.h>
11 : : #include <rte_spinlock.h>
12 : :
13 : : #include "rte_power_intrinsics.h"
14 : :
15 : : /*
16 : : * Per-lcore structure holding current status of C0.2 sleeps.
17 : : */
18 : : struct power_wait_status {
19 : : rte_spinlock_t lock;
20 : : volatile void *monitor_addr; /**< NULL if not currently sleeping */
21 : : };
22 : :
23 : : RTE_LCORE_VAR_HANDLE(struct power_wait_status, wait_status);
24 : :
25 : : static void
26 : : init_wait_status(void)
27 : : {
28 [ # # # # : 0 : if (wait_status != NULL)
# # ]
29 : : return;
30 : 0 : RTE_LCORE_VAR_ALLOC(wait_status);
31 : : }
32 : :
33 : : /*
34 : : * This function uses UMONITOR/UMWAIT instructions and will enter C0.2 state.
35 : : * For more information about usage of these instructions, please refer to
36 : : * Intel(R) 64 and IA-32 Architectures Software Developer's Manual.
37 : : */
38 : 0 : static void intel_umonitor(volatile void *addr)
39 : : {
40 : : #if defined(RTE_TOOLCHAIN_MSVC) || defined(__WAITPKG__)
41 : : /* cast away "volatile" when using the intrinsic */
42 : : _umonitor((void *)(uintptr_t)addr);
43 : : #else
44 : : /*
45 : : * we're using raw byte codes for compiler versions which
46 : : * don't support this instruction natively.
47 : : */
48 : 0 : asm volatile(".byte 0xf3, 0x0f, 0xae, 0xf7;"
49 : : :
50 : : : "D"(addr));
51 : : #endif
52 : 0 : }
53 : :
54 : 0 : static void intel_umwait(const uint64_t timeout)
55 : : {
56 : : #if defined(RTE_TOOLCHAIN_MSVC) || defined(__WAITPKG__)
57 : : _umwait(0, timeout);
58 : : #else
59 : 0 : const uint32_t tsc_l = (uint32_t)timeout;
60 : 0 : const uint32_t tsc_h = (uint32_t)(timeout >> 32);
61 : :
62 : 0 : asm volatile(".byte 0xf2, 0x0f, 0xae, 0xf7;"
63 : : : /* ignore rflags */
64 : : : "D"(0), /* enter C0.2 */
65 : : "a"(tsc_l), "d"(tsc_h));
66 : : #endif
67 : 0 : }
68 : :
69 : : /*
70 : : * This function uses MONITORX/MWAITX instructions and will enter C1 state.
71 : : * For more information about usage of these instructions, please refer to
72 : : * AMD64 Architecture Programmer’s Manual.
73 : : */
74 : 0 : static void amd_monitorx(volatile void *addr)
75 : : {
76 : : #if defined(RTE_TOOLCHAIN_MSVC) || defined(__MWAITX__)
77 : : /* cast away "volatile" when using the intrinsic */
78 : : _mm_monitorx((void *)(uintptr_t)addr, 0, 0);
79 : : #else
80 : 0 : asm volatile(".byte 0x0f, 0x01, 0xfa;"
81 : : :
82 : : : "a"(addr),
83 : : "c"(0), /* no extensions */
84 : : "d"(0)); /* no hints */
85 : : #endif
86 : 0 : }
87 : :
88 : 0 : static void amd_mwaitx(const uint64_t timeout)
89 : : {
90 : : RTE_SET_USED(timeout);
91 : : #if defined(RTE_TOOLCHAIN_MSVC) || defined(__MWAITX__)
92 : : _mm_mwaitx(0, 0, 0);
93 : : #else
94 : 0 : asm volatile(".byte 0x0f, 0x01, 0xfb;"
95 : : : /* ignore rflags */
96 : : : "a"(0), /* enter C1 */
97 : : "c"(0)); /* no time-out */
98 : : #endif
99 : 0 : }
100 : :
101 : : static alignas(RTE_CACHE_LINE_SIZE) struct {
102 : : void (*mmonitor)(volatile void *addr);
103 : : void (*mwait)(const uint64_t timeout);
104 : : } power_monitor_ops;
105 : :
106 : : static inline void
107 : : __umwait_wakeup(volatile void *addr)
108 : : {
109 : : uint64_t val;
110 : :
111 : : /* trigger a write but don't change the value */
112 : 0 : val = rte_atomic_load_explicit((volatile __rte_atomic uint64_t *)addr,
113 : : rte_memory_order_relaxed);
114 : 0 : rte_atomic_compare_exchange_strong_explicit((volatile __rte_atomic uint64_t *)addr,
115 : : &val, val, rte_memory_order_relaxed, rte_memory_order_relaxed);
116 : 0 : }
117 : :
118 : : static bool wait_supported;
119 : : static bool wait_multi_supported;
120 : : static bool monitor_supported;
121 : :
122 : : static inline uint64_t
123 : : __get_umwait_val(const volatile void *p, const uint8_t sz)
124 : : {
125 : 0 : switch (sz) {
126 : 0 : case sizeof(uint8_t):
127 : 0 : return *(const volatile uint8_t *)p;
128 : 0 : case sizeof(uint16_t):
129 : 0 : return *(const volatile uint16_t *)p;
130 : 0 : case sizeof(uint32_t):
131 : 0 : return *(const volatile uint32_t *)p;
132 : 0 : case sizeof(uint64_t):
133 : 0 : return *(const volatile uint64_t *)p;
134 : : default:
135 : : /* shouldn't happen */
136 : : RTE_ASSERT(0);
137 : : return 0;
138 : : }
139 : : }
140 : :
141 : : static inline int
142 : : __check_val_size(const uint8_t sz)
143 : : {
144 : 0 : switch (sz) {
145 : : case sizeof(uint8_t): /* fall-through */
146 : : case sizeof(uint16_t): /* fall-through */
147 : : case sizeof(uint32_t): /* fall-through */
148 : : case sizeof(uint64_t): /* fall-through */
149 : : return 0;
150 : : default:
151 : : /* unexpected size */
152 : : return -1;
153 : : }
154 : : }
155 : :
156 : : /**
157 : : * This function uses UMONITOR/UMWAIT instructions and will enter C0.2 state.
158 : : * For more information about usage of these instructions, please refer to
159 : : * Intel(R) 64 and IA-32 Architectures Software Developer's Manual.
160 : : */
161 : : int
162 [ # # ]: 0 : rte_power_monitor(const struct rte_power_monitor_cond *pmc,
163 : : const uint64_t tsc_timestamp)
164 : : {
165 : : const unsigned int lcore_id = rte_lcore_id();
166 : : struct power_wait_status *s;
167 : : uint64_t cur_value;
168 : :
169 : : /* prevent user from running this instruction if it's not supported */
170 [ # # ]: 0 : if (!monitor_supported)
171 : : return -ENOTSUP;
172 : :
173 : : /* prevent non-EAL thread from using this API */
174 [ # # ]: 0 : if (lcore_id >= RTE_MAX_LCORE)
175 : : return -EINVAL;
176 : :
177 [ # # ]: 0 : if (pmc == NULL)
178 : : return -EINVAL;
179 : :
180 [ # # ]: 0 : if (__check_val_size(pmc->size) < 0)
181 : : return -EINVAL;
182 : :
183 [ # # ]: 0 : if (pmc->fn == NULL)
184 : : return -EINVAL;
185 : :
186 : : init_wait_status();
187 : 0 : s = RTE_LCORE_VAR_LCORE(lcore_id, wait_status);
188 : :
189 : : /* update sleep address */
190 : 0 : rte_spinlock_lock(&s->lock);
191 : 0 : s->monitor_addr = pmc->addr;
192 : :
193 : : /* set address for memory monitor */
194 : 0 : power_monitor_ops.mmonitor(pmc->addr);
195 : :
196 : : /* now that we've put this address into monitor, we can unlock */
197 : : rte_spinlock_unlock(&s->lock);
198 : :
199 [ # # # # : 0 : cur_value = __get_umwait_val(pmc->addr, pmc->size);
# ]
200 : :
201 : : /* check if callback indicates we should abort */
202 [ # # ]: 0 : if (pmc->fn(cur_value, pmc->opaque) != 0)
203 : 0 : goto end;
204 : :
205 : : /* execute mwait */
206 : 0 : power_monitor_ops.mwait(tsc_timestamp);
207 : :
208 : 0 : end:
209 : : /* erase sleep address */
210 : : rte_spinlock_lock(&s->lock);
211 : 0 : s->monitor_addr = NULL;
212 : : rte_spinlock_unlock(&s->lock);
213 : :
214 : 0 : return 0;
215 : : }
216 : :
217 : : /**
218 : : * This function uses TPAUSE instruction and will enter C0.2 state. For more
219 : : * information about usage of this instruction, please refer to Intel(R) 64 and
220 : : * IA-32 Architectures Software Developer's Manual.
221 : : */
222 : : int
223 : 0 : rte_power_pause(const uint64_t tsc_timestamp)
224 : : {
225 : : /* prevent user from running this instruction if it's not supported */
226 [ # # ]: 0 : if (!wait_supported)
227 : : return -ENOTSUP;
228 : :
229 : : /* execute TPAUSE */
230 : : #if defined(RTE_TOOLCHAIN_MSVC) || defined(__WAITPKG__)
231 : : _tpause(0, tsc_timestamp);
232 : : #else
233 : 0 : const uint32_t tsc_l = (uint32_t)tsc_timestamp;
234 : 0 : const uint32_t tsc_h = (uint32_t)(tsc_timestamp >> 32);
235 : :
236 : 0 : asm volatile(".byte 0x66, 0x0f, 0xae, 0xf7;"
237 : : : /* ignore rflags */
238 : : : "D"(0), /* enter C0.2 */
239 : : "a"(tsc_l), "d"(tsc_h));
240 : : #endif
241 : :
242 : 0 : return 0;
243 : : }
244 : :
245 : 252 : RTE_INIT(rte_power_intrinsics_init) {
246 : : struct rte_cpu_intrinsics i;
247 : :
248 : 252 : rte_cpu_get_intrinsics_support(&i);
249 : :
250 [ - + ]: 252 : if (i.power_monitor && i.power_pause)
251 : 0 : wait_supported = 1;
252 [ - + ]: 252 : if (i.power_monitor_multi)
253 : 0 : wait_multi_supported = 1;
254 [ - + ]: 252 : if (i.power_monitor)
255 : 0 : monitor_supported = 1;
256 : :
257 [ - + ]: 252 : if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_MONITORX)) {
258 : 0 : power_monitor_ops.mmonitor = &amd_monitorx;
259 : 0 : power_monitor_ops.mwait = &amd_mwaitx;
260 : : } else {
261 : 252 : power_monitor_ops.mmonitor = &intel_umonitor;
262 : 252 : power_monitor_ops.mwait = &intel_umwait;
263 : : }
264 : 252 : }
265 : :
266 : : int
267 : 0 : rte_power_monitor_wakeup(const unsigned int lcore_id)
268 : : {
269 : : struct power_wait_status *s;
270 : :
271 : : /* prevent user from running this instruction if it's not supported */
272 [ # # ]: 0 : if (!monitor_supported)
273 : : return -ENOTSUP;
274 : :
275 : : /* prevent buffer overrun */
276 [ # # ]: 0 : if (lcore_id >= RTE_MAX_LCORE)
277 : : return -EINVAL;
278 : :
279 : : init_wait_status();
280 : 0 : s = RTE_LCORE_VAR_LCORE(lcore_id, wait_status);
281 : :
282 : : /*
283 : : * There is a race condition between sleep, wakeup and locking, but we
284 : : * don't need to handle it.
285 : : *
286 : : * Possible situations:
287 : : *
288 : : * 1. T1 locks, sets address, unlocks
289 : : * 2. T2 locks, triggers wakeup, unlocks
290 : : * 3. T1 sleeps
291 : : *
292 : : * In this case, because T1 has already set the address for monitoring,
293 : : * we will wake up immediately even if T2 triggers wakeup before T1
294 : : * goes to sleep.
295 : : *
296 : : * 1. T1 locks, sets address, unlocks, goes to sleep, and wakes up
297 : : * 2. T2 locks, triggers wakeup, and unlocks
298 : : * 3. T1 locks, erases address, and unlocks
299 : : *
300 : : * In this case, since we've already woken up, the "wakeup" was
301 : : * unneeded, and since T1 is still waiting on T2 releasing the lock, the
302 : : * wakeup address is still valid so it's perfectly safe to write it.
303 : : *
304 : : * For multi-monitor case, the act of locking will in itself trigger the
305 : : * wakeup, so no additional writes necessary.
306 : : */
307 : 0 : rte_spinlock_lock(&s->lock);
308 [ # # ]: 0 : if (s->monitor_addr != NULL)
309 : : __umwait_wakeup(s->monitor_addr);
310 : : rte_spinlock_unlock(&s->lock);
311 : :
312 : 0 : return 0;
313 : : }
314 : :
315 : : int
316 : 0 : rte_power_monitor_multi(const struct rte_power_monitor_cond pmc[],
317 : : const uint32_t num, const uint64_t tsc_timestamp)
318 : : {
319 : : struct power_wait_status *s;
320 : : uint32_t i, rc;
321 : :
322 : : /* check if supported */
323 [ # # ]: 0 : if (!wait_multi_supported)
324 : : return -ENOTSUP;
325 : :
326 [ # # ]: 0 : if (pmc == NULL || num == 0)
327 : : return -EINVAL;
328 : :
329 : : init_wait_status();
330 : 0 : s = RTE_LCORE_VAR(wait_status);
331 : :
332 : : /* we are already inside transaction region, return */
333 [ # # ]: 0 : if (rte_xtest() != 0)
334 : : return 0;
335 : :
336 : : /* start new transaction region */
337 : : rc = rte_xbegin();
338 : :
339 : : /* transaction abort, possible write to one of wait addresses */
340 [ # # ]: 0 : if (rc != RTE_XBEGIN_STARTED)
341 : : return 0;
342 : :
343 : : /*
344 : : * the mere act of reading the lock status here adds the lock to
345 : : * the read set. This means that when we trigger a wakeup from another
346 : : * thread, even if we don't have a defined wakeup address and thus don't
347 : : * actually cause any writes, the act of locking our lock will itself
348 : : * trigger the wakeup and abort the transaction.
349 : : */
350 : : rte_spinlock_is_locked(&s->lock);
351 : :
352 : : /*
353 : : * add all addresses to wait on into transaction read-set and check if
354 : : * any of wakeup conditions are already met.
355 : : */
356 : : rc = 0;
357 [ # # ]: 0 : for (i = 0; i < num; i++) {
358 : 0 : const struct rte_power_monitor_cond *c = &pmc[i];
359 : :
360 : : /* cannot be NULL */
361 [ # # ]: 0 : if (c->fn == NULL) {
362 : : rc = -EINVAL;
363 : : break;
364 : : }
365 : :
366 [ # # # # : 0 : const uint64_t val = __get_umwait_val(c->addr, c->size);
# ]
367 : :
368 : : /* abort if callback indicates that we need to stop */
369 [ # # ]: 0 : if (c->fn(val, c->opaque) != 0)
370 : : break;
371 : : }
372 : :
373 : : /* none of the conditions were met, sleep until timeout */
374 [ # # ]: 0 : if (i == num)
375 : 0 : rte_power_pause(tsc_timestamp);
376 : :
377 : : /* end transaction region */
378 : : rte_xend();
379 : :
380 : 0 : return rc;
381 : : }
|