-
Notifications
You must be signed in to change notification settings - Fork 358
schedule: zephyr_ll: implement user/kernel split with init_context() #10818
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kv2019i
wants to merge
3
commits into
thesofproject:main
Choose a base branch
from
kv2019i:202605-llsched-userkernel-split
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+225
−62
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -126,7 +126,8 @@ static void zephyr_domain_thread_fn(void *p1, void *p2, void *p3) | |
| } | ||
| #endif | ||
|
|
||
| dt->handler(dt->arg); | ||
| if (dt->handler) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how can it now be |
||
| dt->handler(dt->arg); | ||
|
|
||
| #ifdef CONFIG_SCHEDULE_LL_STATS_LOG | ||
| cycles1 = k_cycle_get_32(); | ||
|
|
@@ -289,63 +290,65 @@ static int zephyr_domain_unregister(struct ll_schedule_domain *domain, | |
|
|
||
| #else /* CONFIG_SOF_USERSPACE_LL */ | ||
|
|
||
| /* User-space implementation for register/unregister */ | ||
|
|
||
| static int zephyr_domain_register_user(struct ll_schedule_domain *domain, | ||
| struct task *task, | ||
| void (*handler)(void *arg), void *arg) | ||
| /* | ||
| * Privileged thread initialization for userspace LL scheduling. | ||
| * Creates the scheduling thread, sets up timer, grants access to kernel | ||
| * objects. Must be called from kernel context before any user-space | ||
| * domain_register() calls. | ||
| */ | ||
| static int zephyr_domain_thread_init(struct ll_schedule_domain *domain, | ||
| struct task *task) | ||
| { | ||
| struct zephyr_domain *zephyr_domain = ll_sch_domain_get_pdata(domain); | ||
| int core = cpu_get_id(); | ||
| struct zephyr_domain_thread *dt = zephyr_domain->domain_thread + core; | ||
| struct zephyr_domain_thread *dt; | ||
| char thread_name[] = "ll_thread0"; | ||
| k_tid_t thread; | ||
| int core = task->core; | ||
|
|
||
| tr_dbg(&ll_tr, "entry"); | ||
| tr_dbg(&ll_tr, "thread_init entry"); | ||
|
|
||
| /* domain work only needs registered once on each core */ | ||
| if (dt->handler) | ||
| return 0; | ||
| if (core >= CONFIG_CORE_COUNT) | ||
| return -EINVAL; | ||
|
|
||
| __ASSERT_NO_MSG(task->core == core); | ||
| dt = zephyr_domain->domain_thread + core; | ||
|
|
||
| dt->handler = handler; | ||
| dt->arg = arg; | ||
| /* thread only needs to be created once per core */ | ||
| if (dt->ll_thread) | ||
| return 0; | ||
|
|
||
| dt->handler = NULL; | ||
| dt->arg = NULL; | ||
|
|
||
| /* 10 is rather random, we better not accumulate 10 missed timer interrupts */ | ||
| k_sem_init(dt->sem, 0, 10); | ||
|
|
||
| thread_name[sizeof(thread_name) - 2] = '0' + core; | ||
|
|
||
| /* Allocate thread structure dynamically */ | ||
| dt->ll_thread = k_object_alloc(K_OBJ_THREAD); | ||
| if (!dt->ll_thread) { | ||
| /* Allocate thread structure dynamically */ | ||
| dt->ll_thread = k_object_alloc(K_OBJ_THREAD); | ||
| if (!dt->ll_thread) { | ||
| tr_err(&ll_tr, "Failed to allocate thread object for core %d", core); | ||
| dt->handler = NULL; | ||
| dt->arg = NULL; | ||
| return -ENOMEM; | ||
| } | ||
| tr_err(&ll_tr, "Failed to allocate thread object for core %d", core); | ||
| return -ENOMEM; | ||
| } | ||
|
|
||
| thread = k_thread_create(dt->ll_thread, ll_sched_stack[core], ZEPHYR_LL_STACK_SIZE, | ||
| zephyr_domain_thread_fn, zephyr_domain, | ||
| INT_TO_POINTER(core), NULL, CONFIG_LL_THREAD_PRIORITY, | ||
| K_USER, K_FOREVER); | ||
| thread = k_thread_create(dt->ll_thread, ll_sched_stack[core], ZEPHYR_LL_STACK_SIZE, | ||
| zephyr_domain_thread_fn, zephyr_domain, | ||
| INT_TO_POINTER(core), NULL, CONFIG_LL_THREAD_PRIORITY, | ||
| K_USER, K_FOREVER); | ||
|
|
||
| #ifdef CONFIG_SCHED_CPU_MASK | ||
| k_thread_cpu_mask_clear(thread); | ||
| k_thread_cpu_mask_enable(thread, core); | ||
| k_thread_cpu_mask_clear(thread); | ||
| k_thread_cpu_mask_enable(thread, core); | ||
| #endif | ||
| k_thread_name_set(thread, thread_name); | ||
| k_thread_name_set(thread, thread_name); | ||
|
|
||
| k_mem_domain_add_thread(zephyr_ll_mem_domain(), thread); | ||
| k_thread_access_grant(thread, dt->sem, domain->lock, zephyr_domain->timer); | ||
| user_grant_dai_access_all(thread); | ||
| user_grant_dma_access_all(thread); | ||
| tr_dbg(&ll_tr, "granted LL access to thread %p (core %d)", thread, core); | ||
| k_mem_domain_add_thread(zephyr_ll_mem_domain(), thread); | ||
| k_thread_access_grant(thread, dt->sem, domain->lock, zephyr_domain->timer); | ||
| user_grant_dai_access_all(thread); | ||
| user_grant_dma_access_all(thread); | ||
| tr_dbg(&ll_tr, "granted LL access to thread %p (core %d)", thread, core); | ||
|
|
||
| k_thread_start(thread); | ||
| } | ||
| k_thread_start(thread); | ||
|
|
||
| k_mutex_lock(domain->lock, K_FOREVER); | ||
| if (!k_timer_user_data_get(zephyr_domain->timer)) { | ||
|
|
@@ -368,6 +371,43 @@ static int zephyr_domain_register_user(struct ll_schedule_domain *domain, | |
| return 0; | ||
| } | ||
|
|
||
| /* | ||
| * User-space register: bookkeeping only. The privileged thread setup has | ||
| * already been done by domain_thread_init() called from kernel context. | ||
| */ | ||
| static int zephyr_domain_register_user(struct ll_schedule_domain *domain, | ||
| struct task *task, | ||
| void (*handler)(void *arg), void *arg) | ||
| { | ||
| struct zephyr_domain *zephyr_domain = ll_sch_domain_get_pdata(domain); | ||
| struct zephyr_domain_thread *dt; | ||
| int core; | ||
|
|
||
| tr_dbg(&ll_tr, "register_user entry"); | ||
|
|
||
| if (task->core >= CONFIG_CORE_COUNT) | ||
| return -EINVAL; | ||
|
|
||
| core = task->core; | ||
| dt = zephyr_domain->domain_thread + core; | ||
|
|
||
| if (!dt->ll_thread) { | ||
| tr_err(&ll_tr, "domain_thread_init() not called for core %d", core); | ||
| return -EINVAL; | ||
| } | ||
|
|
||
| __ASSERT_NO_MSG(!dt->handler || dt->handler == handler); | ||
| if (dt->handler) | ||
| return 0; | ||
|
|
||
| dt->handler = handler; | ||
| dt->arg = arg; | ||
|
|
||
| tr_info(&ll_tr, "task registered on core %d", core); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| static int zephyr_domain_unregister_user(struct ll_schedule_domain *domain, | ||
| struct task *task, uint32_t num_tasks) | ||
| { | ||
|
|
@@ -382,30 +422,58 @@ static int zephyr_domain_unregister_user(struct ll_schedule_domain *domain, | |
|
|
||
| k_mutex_lock(domain->lock, K_FOREVER); | ||
|
|
||
| if (!atomic_read(&domain->total_num_tasks)) { | ||
| /* Disable the watchdog */ | ||
| zephyr_domain->domain_thread[core].handler = NULL; | ||
|
|
||
| k_mutex_unlock(domain->lock); | ||
|
|
||
| /* | ||
| * In this user thread implementation, the timer is left | ||
| * running until privileged domain_thread_free() is called | ||
| * to clean up resources. | ||
| */ | ||
|
|
||
| tr_dbg(&ll_tr, "exit"); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| /* | ||
| * Free resources acquired by zephyr_domain_thread_init(). | ||
| * Stops the timer, aborts the scheduling thread and frees the thread object. | ||
| * Must be called from kernel context. | ||
| */ | ||
| static void zephyr_domain_thread_free(struct ll_schedule_domain *domain, | ||
| uint32_t num_tasks) | ||
| { | ||
| struct zephyr_domain *zephyr_domain = ll_sch_domain_get_pdata(domain); | ||
| int core = cpu_get_id(); | ||
| struct zephyr_domain_thread *dt = zephyr_domain->domain_thread + core; | ||
|
Comment on lines
+449
to
+450
|
||
|
|
||
| tr_dbg(&ll_tr, "thread_free entry, core %d, num_tasks %u", core, num_tasks); | ||
|
|
||
| /* Still tasks on other cores, only clean up this core's thread */ | ||
| k_mutex_lock(domain->lock, K_FOREVER); | ||
|
|
||
| if (!num_tasks && !atomic_read(&domain->total_num_tasks)) { | ||
| /* Last task globally: stop the timer and watchdog */ | ||
| watchdog_disable(core); | ||
|
|
||
| k_timer_stop(zephyr_domain->timer); | ||
| k_timer_user_data_set(zephyr_domain->timer, NULL); | ||
| } | ||
|
|
||
| zephyr_domain->domain_thread[core].handler = NULL; | ||
| dt->handler = NULL; | ||
| dt->arg = NULL; | ||
|
|
||
| k_mutex_unlock(domain->lock); | ||
|
|
||
| tr_info(&ll_tr, "domain->type %d domain->clk %d", | ||
| domain->type, domain->clk); | ||
|
|
||
| /* Thread not removed here, only the timer is stopped. | ||
| * Thread object cleanup would require k_thread_abort() which cannot | ||
| * be safely called from this context. The thread remains allocated | ||
| * but dormant until next registration or system shutdown. | ||
| */ | ||
|
|
||
| tr_dbg(&ll_tr, "exit"); | ||
| if (dt->ll_thread) { | ||
| k_thread_abort(dt->ll_thread); | ||
| k_object_free(dt->ll_thread); | ||
| dt->ll_thread = NULL; | ||
| } | ||
|
|
||
| return 0; | ||
| tr_info(&ll_tr, "thread_free done, core %d", core); | ||
| } | ||
|
|
||
| struct k_thread *zephyr_domain_thread_tid(struct ll_schedule_domain *domain) | ||
|
|
@@ -450,6 +518,8 @@ APP_TASK_DATA static const struct ll_schedule_domain_ops zephyr_domain_ops = { | |
| #ifdef CONFIG_SOF_USERSPACE_LL | ||
| .domain_register = zephyr_domain_register_user, | ||
| .domain_unregister = zephyr_domain_unregister_user, | ||
| .domain_thread_init = zephyr_domain_thread_init, | ||
| .domain_thread_free = zephyr_domain_thread_free, | ||
| #else | ||
| .domain_register = zephyr_domain_register, | ||
| .domain_unregister = zephyr_domain_unregister, | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick - when called for schedulers, which don't have
.scheduler_init_context()the loop will iterate over all schedulers instead ofreturn NULLwhen found. We only have under 5 schedulers so it's a non-issue, but still