xref: /dpdk/lib/eal/common/eal_common_launch.c (revision 2a7a42a5918af42dbf229d30dbba13697e68320f)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson #include <errno.h>
699a2dd95SBruce Richardson 
745a685caSAnkur Dwivedi #include <eal_trace_internal.h>
899a2dd95SBruce Richardson #include <rte_launch.h>
999a2dd95SBruce Richardson #include <rte_pause.h>
1099a2dd95SBruce Richardson #include <rte_lcore.h>
1199a2dd95SBruce Richardson 
1299a2dd95SBruce Richardson #include "eal_private.h"
13a95d7054SDavid Marchand #include "eal_thread.h"
1499a2dd95SBruce Richardson 
1599a2dd95SBruce Richardson /*
1699a2dd95SBruce Richardson  * Wait until a lcore finished its job.
1799a2dd95SBruce Richardson  */
1899a2dd95SBruce Richardson int
rte_eal_wait_lcore(unsigned worker_id)1999a2dd95SBruce Richardson rte_eal_wait_lcore(unsigned worker_id)
2099a2dd95SBruce Richardson {
21*2a7a42a5STyler Retzlaff 	while (rte_atomic_load_explicit(&lcore_config[worker_id].state,
22*2a7a42a5STyler Retzlaff 			rte_memory_order_acquire) != WAIT)
2399a2dd95SBruce Richardson 		rte_pause();
2499a2dd95SBruce Richardson 
2599a2dd95SBruce Richardson 	return lcore_config[worker_id].ret;
2699a2dd95SBruce Richardson }
2799a2dd95SBruce Richardson 
2899a2dd95SBruce Richardson /*
29a95d7054SDavid Marchand  * Send a message to a worker lcore identified by worker_id to call a
30a95d7054SDavid Marchand  * function f with argument arg. Once the execution is done, the
31a95d7054SDavid Marchand  * remote lcore switches to WAIT state.
32a95d7054SDavid Marchand  */
33a95d7054SDavid Marchand int
rte_eal_remote_launch(lcore_function_t * f,void * arg,unsigned int worker_id)34a95d7054SDavid Marchand rte_eal_remote_launch(lcore_function_t *f, void *arg, unsigned int worker_id)
35a95d7054SDavid Marchand {
36a95d7054SDavid Marchand 	int rc = -EBUSY;
37a95d7054SDavid Marchand 
38a95d7054SDavid Marchand 	/* Check if the worker is in 'WAIT' state. Use acquire order
39a95d7054SDavid Marchand 	 * since 'state' variable is used as the guard variable.
40a95d7054SDavid Marchand 	 */
41*2a7a42a5STyler Retzlaff 	if (rte_atomic_load_explicit(&lcore_config[worker_id].state,
42*2a7a42a5STyler Retzlaff 			rte_memory_order_acquire) != WAIT)
43a95d7054SDavid Marchand 		goto finish;
44a95d7054SDavid Marchand 
45a95d7054SDavid Marchand 	lcore_config[worker_id].arg = arg;
46a95d7054SDavid Marchand 	/* Ensure that all the memory operations are completed
47a95d7054SDavid Marchand 	 * before the worker thread starts running the function.
48a95d7054SDavid Marchand 	 * Use worker thread function as the guard variable.
49a95d7054SDavid Marchand 	 */
50*2a7a42a5STyler Retzlaff 	rte_atomic_store_explicit(&lcore_config[worker_id].f, f, rte_memory_order_release);
51a95d7054SDavid Marchand 
529856af40SBruce Richardson 	rc = eal_thread_wake_worker(worker_id);
53a95d7054SDavid Marchand 
54a95d7054SDavid Marchand finish:
55a95d7054SDavid Marchand 	rte_eal_trace_thread_remote_launch(f, arg, worker_id, rc);
56a95d7054SDavid Marchand 	return rc;
57a95d7054SDavid Marchand }
58a95d7054SDavid Marchand 
59a95d7054SDavid Marchand /*
6099a2dd95SBruce Richardson  * Check that every WORKER lcores are in WAIT state, then call
6199a2dd95SBruce Richardson  * rte_eal_remote_launch() for all of them. If call_main is true
6299a2dd95SBruce Richardson  * (set to CALL_MAIN), also call the function on the main lcore.
6399a2dd95SBruce Richardson  */
6499a2dd95SBruce Richardson int
rte_eal_mp_remote_launch(int (* f)(void *),void * arg,enum rte_rmt_call_main_t call_main)6599a2dd95SBruce Richardson rte_eal_mp_remote_launch(int (*f)(void *), void *arg,
6699a2dd95SBruce Richardson 			 enum rte_rmt_call_main_t call_main)
6799a2dd95SBruce Richardson {
6899a2dd95SBruce Richardson 	int lcore_id;
6999a2dd95SBruce Richardson 	int main_lcore = rte_get_main_lcore();
7099a2dd95SBruce Richardson 
7199a2dd95SBruce Richardson 	/* check state of lcores */
7299a2dd95SBruce Richardson 	RTE_LCORE_FOREACH_WORKER(lcore_id) {
7399a2dd95SBruce Richardson 		if (lcore_config[lcore_id].state != WAIT)
7499a2dd95SBruce Richardson 			return -EBUSY;
7599a2dd95SBruce Richardson 	}
7699a2dd95SBruce Richardson 
7799a2dd95SBruce Richardson 	/* send messages to cores */
7899a2dd95SBruce Richardson 	RTE_LCORE_FOREACH_WORKER(lcore_id) {
7999a2dd95SBruce Richardson 		rte_eal_remote_launch(f, arg, lcore_id);
8099a2dd95SBruce Richardson 	}
8199a2dd95SBruce Richardson 
8299a2dd95SBruce Richardson 	if (call_main == CALL_MAIN) {
8399a2dd95SBruce Richardson 		lcore_config[main_lcore].ret = f(arg);
84f6c6c686SHonnappa Nagarahalli 		lcore_config[main_lcore].state = WAIT;
8599a2dd95SBruce Richardson 	}
8699a2dd95SBruce Richardson 
8799a2dd95SBruce Richardson 	return 0;
8899a2dd95SBruce Richardson }
8999a2dd95SBruce Richardson 
9099a2dd95SBruce Richardson /*
9199a2dd95SBruce Richardson  * Return the state of the lcore identified by worker_id.
9299a2dd95SBruce Richardson  */
9399a2dd95SBruce Richardson enum rte_lcore_state_t
rte_eal_get_lcore_state(unsigned lcore_id)9499a2dd95SBruce Richardson rte_eal_get_lcore_state(unsigned lcore_id)
9599a2dd95SBruce Richardson {
9699a2dd95SBruce Richardson 	return lcore_config[lcore_id].state;
9799a2dd95SBruce Richardson }
9899a2dd95SBruce Richardson 
9999a2dd95SBruce Richardson /*
10099a2dd95SBruce Richardson  * Do a rte_eal_wait_lcore() for every lcore. The return values are
10199a2dd95SBruce Richardson  * ignored.
10299a2dd95SBruce Richardson  */
10399a2dd95SBruce Richardson void
rte_eal_mp_wait_lcore(void)10499a2dd95SBruce Richardson rte_eal_mp_wait_lcore(void)
10599a2dd95SBruce Richardson {
10699a2dd95SBruce Richardson 	unsigned lcore_id;
10799a2dd95SBruce Richardson 
10899a2dd95SBruce Richardson 	RTE_LCORE_FOREACH_WORKER(lcore_id) {
10999a2dd95SBruce Richardson 		rte_eal_wait_lcore(lcore_id);
11099a2dd95SBruce Richardson 	}
11199a2dd95SBruce Richardson }
112