summaryrefslogtreecommitdiff
path: root/zephyr
diff options
context:
space:
mode:
authorFabio Baltieri <fabiobaltieri@google.com>2022-04-14 13:12:36 +0000
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-04-21 20:24:54 +0000
commit33be9c52c6910c4805fb1ef67b4e1d23e81471fd (patch)
treea2f7b9cb37e2e06df48df9d46193386cd6f9759c /zephyr
parent2fae2c312151cfb33d9a243efe12570e9b69960f (diff)
downloadchrome-ec-33be9c52c6910c4805fb1ef67b4e1d23e81471fd.tar.gz
zephyr: tasks: run HOSTCMD in main thread
Add an option to run the host command loop in the main thread, taking over the entire thread and reusing its thread support code and stack. This is still optional and automatically disabled on tests as test code actually needs main() to run the test code itself. The code ensures that the main thread is switched to the correct priority for the HOSTCMD code and changes the thread name as well. BRANCH=none BUG=b:223044986 TEST=zmake testall TEST=build and run on brya Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com> Change-Id: Ib2b3ba3d8a6fb883876ecdd432068ca3300e5344 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3593779 Reviewed-by: Jack Rosenthal <jrosenth@chromium.org> Reviewed-by: Keith Short <keithshort@chromium.org>
Diffstat (limited to 'zephyr')
-rw-r--r--zephyr/Kconfig.tasks18
-rw-r--r--zephyr/app/ec/main_shim.c17
-rw-r--r--zephyr/shim/include/shimmed_task_id.h6
-rw-r--r--zephyr/shim/include/zephyr_host_command.h10
-rw-r--r--zephyr/shim/src/host_command.c19
-rw-r--r--zephyr/shim/src/tasks.c7
6 files changed, 68 insertions, 9 deletions
diff --git a/zephyr/Kconfig.tasks b/zephyr/Kconfig.tasks
index d1e9e17769..d329150e22 100644
--- a/zephyr/Kconfig.tasks
+++ b/zephyr/Kconfig.tasks
@@ -88,8 +88,26 @@ config HAS_TASK_HOSTCMD
if HAS_TASK_HOSTCMD
+choice TASK_HOSTCMD_THREAD_MODE
+ prompt "Host command thread selection"
+ default TASK_HOSTCMD_THREAD_DEDICATED if ZTEST
+
+config TASK_HOSTCMD_THREAD_MAIN
+ bool "Run the host command in the main thread"
+ help
+ Run the HOSTCMD task in the main thread, reusing the main thread
+ resources. Set the stack size using MAIN_STACK_SIZE.
+
+config TASK_HOSTCMD_THREAD_DEDICATED
+ bool "Run the host command in a dedicated thread"
+ help
+ Run the HOSTCMD task in a dedicated thread.
+
+endchoice
+
config TASK_HOSTCMD_STACK_SIZE
int "HOSTCMD task stack size"
+ depends on TASK_HOSTCMD_THREAD_DEDICATED
default 1024
help
The size of the host-command task stack.
diff --git a/zephyr/app/ec/main_shim.c b/zephyr/app/ec/main_shim.c
index 35f45e0286..a827e0101c 100644
--- a/zephyr/app/ec/main_shim.c
+++ b/zephyr/app/ec/main_shim.c
@@ -5,18 +5,21 @@
#include <kernel.h>
#include "ec_app_main.h"
+#include "host_command.h"
/** A stub main to call the real ec app main function. LCOV_EXCL_START */
void main(void)
{
ec_app_main();
-#ifdef CONFIG_THREAD_MONITOR
- /*
- * Avoid returning so that the main stack is displayed by the
- * "kernel stacks" shell command.
- */
- k_sleep(K_FOREVER);
-#endif
+ if (IS_ENABLED(CONFIG_TASK_HOSTCMD_THREAD_MAIN)) {
+ host_command_main();
+ } else if (IS_ENABLED(CONFIG_THREAD_MONITOR)) {
+ /*
+ * Avoid returning so that the main stack is displayed by the
+ * "kernel stacks" shell command.
+ */
+ k_sleep(K_FOREVER);
+ }
}
/* LCOV_EXCL_STOP */
diff --git a/zephyr/shim/include/shimmed_task_id.h b/zephyr/shim/include/shimmed_task_id.h
index 1c746d5fc5..69efe50f1b 100644
--- a/zephyr/shim/include/shimmed_task_id.h
+++ b/zephyr/shim/include/shimmed_task_id.h
@@ -100,7 +100,7 @@ enum {
(CROS_EC_TASK(MOTIONSENSE, motion_sense_task, 0, \
CONFIG_TASK_MOTIONSENSE_STACK_SIZE, \
EC_TASK_MOTIONSENSE_PRIO)), ()) \
- COND_CODE_1(HAS_TASK_HOSTCMD, \
+ COND_CODE_1(CONFIG_TASK_HOSTCMD_THREAD_DEDICATED, \
(CROS_EC_TASK(HOSTCMD, host_command_task, 0, \
CONFIG_TASK_HOSTCMD_STACK_SIZE, \
EC_TASK_HOSTCMD_PRIO)), ()) \
@@ -195,9 +195,11 @@ enum {
#undef TASK_TEST
/*
- * Additional task IDs for features that runs on non shimmed threads.
+ * Additional task IDs for features that runs on non shimmed threads,
+ * task_get_current() needs to be updated to identify these ones.
*/
#define CROS_EC_EXTRA_TASKS(fn) \
+ COND_CODE_1(CONFIG_TASK_HOSTCMD_THREAD_MAIN, (fn(HOSTCMD)), ()) \
fn(SYSWORKQ)
#define EXTRA_TASK_INTERNAL_ID(name) EXTRA_TASK_##name,
diff --git a/zephyr/shim/include/zephyr_host_command.h b/zephyr/shim/include/zephyr_host_command.h
index 138c8636c7..1f07ac34d2 100644
--- a/zephyr/shim/include/zephyr_host_command.h
+++ b/zephyr/shim/include/zephyr_host_command.h
@@ -11,6 +11,16 @@
#define __CROS_EC_ZEPHYR_HOST_COMMAND_H
#include <init.h>
+#include <stdbool.h>
+
+/* Initializes and runs the host command handler loop. */
+void host_command_task(void *u);
+
+/* Takes over the main thread and runs the host command loop. */
+void host_command_main(void);
+
+/* True if running in the main thread. */
+bool in_host_command_main(void);
#ifdef CONFIG_PLATFORM_EC_HOSTCMD
diff --git a/zephyr/shim/src/host_command.c b/zephyr/shim/src/host_command.c
index bf863b48de..ee7f994fdb 100644
--- a/zephyr/shim/src/host_command.c
+++ b/zephyr/shim/src/host_command.c
@@ -3,7 +3,10 @@
* found in the LICENSE file.
*/
+#include <zephyr.h>
+
#include "host_command.h"
+#include "task.h"
struct host_command *zephyr_find_host_command(int command)
{
@@ -14,3 +17,19 @@ struct host_command *zephyr_find_host_command(int command)
return NULL;
}
+
+/* Pointer to the main thread, defined in kernel/init.c */
+extern struct k_thread z_main_thread;
+
+void host_command_main(void)
+{
+ k_thread_priority_set(&z_main_thread,
+ EC_TASK_PRIORITY(EC_TASK_HOSTCMD_PRIO));
+ k_thread_name_set(&z_main_thread, "HOSTCMD");
+ host_command_task(NULL);
+}
+
+bool in_host_command_main(void)
+{
+ return (k_current_get() == &z_main_thread);
+}
diff --git a/zephyr/shim/src/tasks.c b/zephyr/shim/src/tasks.c
index f0f6fec6ad..e24e27ff31 100644
--- a/zephyr/shim/src/tasks.c
+++ b/zephyr/shim/src/tasks.c
@@ -9,6 +9,7 @@
#include <shell/shell.h>
#include "common.h"
+#include "host_command.h"
#include "timer.h"
#include "task.h"
@@ -115,6 +116,12 @@ task_id_t task_get_current(void)
return TASK_ID_SYSWORKQ;
}
+#ifdef CONFIG_TASK_HOSTCMD_THREAD_MAIN
+ if (in_host_command_main()) {
+ return TASK_ID_HOSTCMD;
+ }
+#endif
+
for (size_t i = 0; i < TASK_ID_COUNT; ++i) {
if (shimmed_tasks_data[i].zephyr_tid == k_current_get())
return i;