summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeith Short <keithshort@chromium.org>2022-09-21 14:12:38 -0600
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-10-05 19:05:18 +0000
commite000ea82e086d3d671180ca23feee1faea1537dd (patch)
tree3b65d6c86cbe098bf6d9563a4b0b30d146a79a73
parentc9dcc20ddf2af9b2473f0209462d717fe0aa34f5 (diff)
downloadchrome-ec-e000ea82e086d3d671180ca23feee1faea1537dd.tar.gz
zephyr: Update tasks shim to use K_THREAD_DEFINE
Use Zephyr's K_THREAD_DEFINE to statically create the needed threads instead of dynamically creating them with k_thread_create(). This saves a small amount of RAM (40 bytes) and increases flash usage by about 140 bytes. This change should not merge until https://github.com/zephyrproject-rtos/zephyr/pull/50508 merges. BUG=none BRANCH=none TEST=zmake build -a TEST=./twister TEST=verify zephyr image boots on herobrine. Verify thread priorities are unchanged. Cq-Depend: chromium:3936185 Signed-off-by: Keith Short <keithshort@chromium.org> Change-Id: Ie11c8fd99e0d424e2a03f33c545fa847d9d73ff9 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3916581 Reviewed-by: Fabio Baltieri <fabiobaltieri@google.com> Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com>
-rw-r--r--zephyr/shim/src/tasks.c106
1 files changed, 23 insertions, 83 deletions
diff --git a/zephyr/shim/src/tasks.c b/zephyr/shim/src/tasks.c
index 4143215442..35d4ab7c42 100644
--- a/zephyr/shim/src/tasks.c
+++ b/zephyr/shim/src/tasks.c
@@ -18,14 +18,6 @@ BUILD_ASSERT(EC_TASK_PRIORITY(EC_TASK_PRIO_LOWEST) < K_IDLE_PRIO,
"CONFIG_NUM_PREEMPT_PRIORITIES too small, some tasks would run at "
"idle priority");
-/* Declare all task stacks here */
-#define CROS_EC_TASK(name, e, p, size, pr) \
- K_THREAD_STACK_DEFINE(name##_STACK, size);
-#define TASK_TEST(name, e, p, size) CROS_EC_TASK(name, e, p, size)
-CROS_EC_TASK_LIST
-#undef CROS_EC_TASK
-#undef TASK_TEST
-
/* Forward declare all task entry point functions */
#define CROS_EC_TASK(name, entry, ...) void entry(void *p);
#define TASK_TEST(name, entry, ...) CROS_EC_TASK(name, entry)
@@ -33,23 +25,14 @@ CROS_EC_TASK_LIST
#undef CROS_EC_TASK
#undef TASK_TEST
-/** Context for each CROS EC task that is run in its own zephyr thread */
-struct task_ctx_cfg {
-#ifdef CONFIG_THREAD_NAME
- /** Name of thread (for debugging) */
- const char *name;
-#endif
- /** Address of Zephyr thread's stack */
- k_thread_stack_t *stack;
- /** Usabled size in bytes of above thread stack */
- size_t stack_size;
- /** Task (platform/ec) entry point */
- void (*entry)(void *p);
- /** The parameter that is passed into the task entry point */
- intptr_t parameter;
- /** The task priority */
- int priority;
-};
+/* Statically declare all threads here */
+#define CROS_EC_TASK(name, entry, parameter, stack_size, priority) \
+ K_THREAD_DEFINE(name, stack_size, entry, parameter, NULL, NULL, \
+ EC_TASK_PRIORITY(priority), 0, -1);
+#define TASK_TEST(name, e, p, size) CROS_EC_TASK(name, e, p, size)
+CROS_EC_TASK_LIST
+#undef CROS_EC_TASK
+#undef TASK_TEST
struct task_ctx_base_data {
/** A wait-able event that is raised when a new task event is posted */
@@ -58,32 +41,20 @@ struct task_ctx_base_data {
atomic_t event_mask;
};
-struct task_ctx_data {
- /** Zephyr thread structure that hosts EC tasks */
- struct k_thread zephyr_thread;
- /** Zephyr thread id for above thread */
- k_tid_t zephyr_tid;
- /** Base context data */
- struct task_ctx_base_data base;
-};
-
-#define CROS_EC_TASK(_name, _entry, _parameter, _size, _prio) \
- { .entry = _entry, \
- .parameter = _parameter, \
- .stack = _name##_STACK, \
- .stack_size = _size, \
- .priority = EC_TASK_PRIORITY(_prio), \
- COND_CODE_1(CONFIG_THREAD_NAME, (.name = #_name, ), ()) },
-#define TASK_TEST(_name, _entry, _parameter, _size) \
- CROS_EC_TASK(_name, _entry, _parameter, _size)
-const static struct task_ctx_cfg shimmed_tasks_cfg[TASK_ID_COUNT] = {
- CROS_EC_TASK_LIST
+/*
+ * Create a mapping from the cros-ec task ID to the Zephyr thread.
+ */
+#undef CROS_EC_TASK
+#define CROS_EC_TASK(_name, _entry, _parameter, _size, _prio) _name,
#ifdef TEST_BUILD
- [TASK_ID_TEST_RUNNER] = {},
+static k_tid_t task_to_k_tid[TASK_ID_COUNT] = {
+#else
+const static k_tid_t task_to_k_tid[TASK_ID_COUNT] = {
#endif
+ CROS_EC_TASK_LIST
};
-static struct task_ctx_data shimmed_tasks_data[TASK_ID_COUNT];
+static struct task_ctx_base_data shimmed_tasks_data[TASK_ID_COUNT];
static struct task_ctx_base_data extra_tasks_data[EXTRA_TASK_COUNT];
/* Task timer structures. Keep separate from the context ones to avoid memory
* holes due to int64_t fields in struct _timeout.
@@ -104,7 +75,7 @@ static struct task_ctx_base_data *task_get_base_data(task_id_t cros_task_id)
return &extra_tasks_data[cros_task_id - TASK_ID_COUNT];
}
- return &shimmed_tasks_data[cros_task_id].base;
+ return &shimmed_tasks_data[cros_task_id];
}
task_id_t task_get_current(void)
@@ -120,7 +91,7 @@ task_id_t task_get_current(void)
#endif
for (size_t i = 0; i < TASK_ID_COUNT; ++i) {
- if (shimmed_tasks_data[i].zephyr_tid == k_current_get())
+ if (task_to_k_tid[i] == k_current_get())
return i;
}
@@ -235,18 +206,6 @@ uint32_t task_wait_event_mask(uint32_t event_mask, int timeout_us)
return events & event_mask;
}
-static void task_entry(void *task_context_cfg, void *task_context_data,
- void *unused1)
-{
- ARG_UNUSED(task_context_data);
- ARG_UNUSED(unused1);
-
- struct task_ctx_cfg *const cfg = task_context_cfg;
-
- /* Call into task entry point */
- cfg->entry((void *)cfg->parameter);
-}
-
/*
* Callback function to use with k_timer_start to set the
* TASK_EVENT_TIMER event on a task.
@@ -291,7 +250,7 @@ void timer_cancel(task_id_t cros_ec_task_id)
#ifdef TEST_BUILD
void set_test_runner_tid(void)
{
- shimmed_tasks_data[TASK_ID_TEST_RUNNER].zephyr_tid = k_current_get();
+ task_to_k_tid[TASK_ID_TEST_RUNNER] = k_current_get();
}
#ifdef CONFIG_TASKS_SET_TEST_RUNNER_TID_RULE
@@ -315,32 +274,13 @@ void start_ec_tasks(void)
}
for (size_t i = 0; i < TASK_ID_COUNT; ++i) {
- struct task_ctx_data *const data = &shimmed_tasks_data[i];
- const struct task_ctx_cfg *const cfg = &shimmed_tasks_cfg[i];
-
#ifdef TEST_BUILD
- /* Do not create thread for test runner; it will be set later */
+ /* The test runner thread is automatically started. */
if (i == TASK_ID_TEST_RUNNER) {
- data->zephyr_tid = NULL;
continue;
}
#endif
-
- /*
- * TODO(b/172361873): Add K_FP_REGS for FPU tasks. See
- * comment in config.h for CONFIG_TASK_LIST for existing flags
- * implementation.
- */
- data->zephyr_tid = k_thread_create(&data->zephyr_thread,
- cfg->stack, cfg->stack_size,
- task_entry, (void *)cfg,
- data, NULL, cfg->priority, 0,
- K_NO_WAIT);
-
-#ifdef CONFIG_THREAD_NAME
- /* Name thread for debugging */
- k_thread_name_set(data->zephyr_tid, cfg->name);
-#endif
+ k_thread_start(task_to_k_tid[i]);
}
tasks_started = 1;