2022-01-17 20:56:54 +01:00
|
|
|
/* Copyright (c) 2022 Intel corporation
|
2018-01-29 18:23:49 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2022-05-06 11:04:23 +02:00
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
#include <zephyr/kernel_structs.h>
|
2023-11-06 23:29:35 +01:00
|
|
|
#include <zephyr/kernel/smp.h>
|
2022-05-06 11:04:23 +02:00
|
|
|
#include <zephyr/spinlock.h>
|
2018-01-17 20:34:50 +01:00
|
|
|
#include <kswap.h>
|
2018-02-08 18:10:46 +01:00
|
|
|
#include <kernel_internal.h>
|
2018-01-29 18:23:49 +01:00
|
|
|
|
kernel: Rework SMP irq_lock() compatibility layer
This was wrong in two ways, one subtle and one awful.
The subtle problem was that the IRQ lock isn't actually globally
recursive, it gets reset when you context switch (i.e. a _Swap()
implicitly releases and reacquires it). So the recursive count I was
keeping needs to be per-thread or else we risk deadlock any time we
swap away from a thread holding the lock.
And because part of my brain apparently knew this, there was an
"optimization" in the code that tested the current count vs. zero
outside the lock, on the argument that if it was non-zero we must
already hold the lock. Which would be true of a per-thread counter,
but NOT a global one: the other CPU may be holding that lock, and this
test will tell you *you* do. The upshot is that a recursive
irq_lock() would almost always SUCCEED INCORRECTLY when there was lock
contention. That this didn't break more things is amazing to me.
The rework is actually simpler than the original, thankfully. Though
there are some further subtleties:
* The lock state implied by irq_lock() allows the lock to be
implicitly released on context switch (i.e. you can _Swap() with the
lock held at a recursion level higher than 1, which needs to allow
other processes to run). So return paths into threads from _Swap()
and interrupt/exception exit need to check and restore the global
lock state, spinning as needed.
* The idle loop design specifies a k_cpu_idle() function that is on
common architectures expected to enable interrupts (for obvious
reasons), but there is no place to put non-arch code to wire it into
the global lock accounting. So on SMP, even CPU0 needs to use the
"dumb" spinning idle loop.
Finally this patch contains a simple bugfix too, found by inspection:
the interrupt return code used when CONFIG_SWITCH is enabled wasn't
correctly setting the active flag on the threads, opening up the
potential for a race that might result in a thread being scheduled on
two CPUs simultaneously.
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2018-04-12 21:50:05 +02:00
|
|
|
static atomic_t global_lock;
|
2023-11-02 21:23:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag to tell recently powered up CPU to start
|
|
|
|
* initialization routine.
|
|
|
|
*
|
|
|
|
* 0 to tell powered up CPU to wait.
|
|
|
|
* 1 to tell powered up CPU to continue initialization.
|
|
|
|
*/
|
2023-08-03 19:28:01 +02:00
|
|
|
static atomic_t cpu_start_flag;
|
2023-11-02 21:23:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag to tell caller that the target CPU is now
|
|
|
|
* powered up and ready to be initialized.
|
|
|
|
*
|
|
|
|
* 0 if target CPU is not yet ready.
|
|
|
|
* 1 if target CPU has powered up and ready to be initialized.
|
|
|
|
*/
|
2022-01-08 02:08:20 +01:00
|
|
|
static atomic_t ready_flag;
|
|
|
|
|
2023-11-06 23:29:35 +01:00
|
|
|
/**
|
|
|
|
* Struct holding the function to be called before handing off
|
|
|
|
* to schedule and its argument.
|
|
|
|
*/
|
|
|
|
static struct cpu_start_cb {
|
|
|
|
/**
|
|
|
|
* Function to be called before handing off to scheduler.
|
|
|
|
* Can be NULL.
|
|
|
|
*/
|
|
|
|
smp_init_fn fn;
|
|
|
|
|
|
|
|
/** Argument to @ref cpu_start_fn.fn. */
|
|
|
|
void *arg;
|
2023-11-08 22:00:43 +01:00
|
|
|
|
|
|
|
/** Invoke scheduler after CPU has started if true. */
|
|
|
|
bool invoke_sched;
|
|
|
|
|
|
|
|
#ifdef CONFIG_SYS_CLOCK_EXISTS
|
|
|
|
/** True if smp_timer_init() needs to be called. */
|
|
|
|
bool reinit_timer;
|
2024-03-08 12:00:10 +01:00
|
|
|
#endif /* CONFIG_SYS_CLOCK_EXISTS */
|
2023-11-06 23:29:35 +01:00
|
|
|
} cpu_start_fn;
|
|
|
|
|
|
|
|
static struct k_spinlock cpu_start_lock;
|
|
|
|
|
2019-03-08 22:19:05 +01:00
|
|
|
unsigned int z_smp_global_lock(void)
|
2018-01-29 18:23:49 +01:00
|
|
|
{
|
2019-11-07 21:43:29 +01:00
|
|
|
unsigned int key = arch_irq_lock();
|
2018-01-29 18:23:49 +01:00
|
|
|
|
kernel: Rework SMP irq_lock() compatibility layer
This was wrong in two ways, one subtle and one awful.
The subtle problem was that the IRQ lock isn't actually globally
recursive, it gets reset when you context switch (i.e. a _Swap()
implicitly releases and reacquires it). So the recursive count I was
keeping needs to be per-thread or else we risk deadlock any time we
swap away from a thread holding the lock.
And because part of my brain apparently knew this, there was an
"optimization" in the code that tested the current count vs. zero
outside the lock, on the argument that if it was non-zero we must
already hold the lock. Which would be true of a per-thread counter,
but NOT a global one: the other CPU may be holding that lock, and this
test will tell you *you* do. The upshot is that a recursive
irq_lock() would almost always SUCCEED INCORRECTLY when there was lock
contention. That this didn't break more things is amazing to me.
The rework is actually simpler than the original, thankfully. Though
there are some further subtleties:
* The lock state implied by irq_lock() allows the lock to be
implicitly released on context switch (i.e. you can _Swap() with the
lock held at a recursion level higher than 1, which needs to allow
other processes to run). So return paths into threads from _Swap()
and interrupt/exception exit need to check and restore the global
lock state, spinning as needed.
* The idle loop design specifies a k_cpu_idle() function that is on
common architectures expected to enable interrupts (for obvious
reasons), but there is no place to put non-arch code to wire it into
the global lock accounting. So on SMP, even CPU0 needs to use the
"dumb" spinning idle loop.
Finally this patch contains a simple bugfix too, found by inspection:
the interrupt return code used when CONFIG_SWITCH is enabled wasn't
correctly setting the active flag on the threads, opening up the
potential for a race that might result in a thread being scheduled on
two CPUs simultaneously.
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2018-04-12 21:50:05 +02:00
|
|
|
if (!_current->base.global_lock_count) {
|
|
|
|
while (!atomic_cas(&global_lock, 0, 1)) {
|
|
|
|
}
|
2018-01-29 18:23:49 +01:00
|
|
|
}
|
|
|
|
|
kernel: Rework SMP irq_lock() compatibility layer
This was wrong in two ways, one subtle and one awful.
The subtle problem was that the IRQ lock isn't actually globally
recursive, it gets reset when you context switch (i.e. a _Swap()
implicitly releases and reacquires it). So the recursive count I was
keeping needs to be per-thread or else we risk deadlock any time we
swap away from a thread holding the lock.
And because part of my brain apparently knew this, there was an
"optimization" in the code that tested the current count vs. zero
outside the lock, on the argument that if it was non-zero we must
already hold the lock. Which would be true of a per-thread counter,
but NOT a global one: the other CPU may be holding that lock, and this
test will tell you *you* do. The upshot is that a recursive
irq_lock() would almost always SUCCEED INCORRECTLY when there was lock
contention. That this didn't break more things is amazing to me.
The rework is actually simpler than the original, thankfully. Though
there are some further subtleties:
* The lock state implied by irq_lock() allows the lock to be
implicitly released on context switch (i.e. you can _Swap() with the
lock held at a recursion level higher than 1, which needs to allow
other processes to run). So return paths into threads from _Swap()
and interrupt/exception exit need to check and restore the global
lock state, spinning as needed.
* The idle loop design specifies a k_cpu_idle() function that is on
common architectures expected to enable interrupts (for obvious
reasons), but there is no place to put non-arch code to wire it into
the global lock accounting. So on SMP, even CPU0 needs to use the
"dumb" spinning idle loop.
Finally this patch contains a simple bugfix too, found by inspection:
the interrupt return code used when CONFIG_SWITCH is enabled wasn't
correctly setting the active flag on the threads, opening up the
potential for a race that might result in a thread being scheduled on
two CPUs simultaneously.
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2018-04-12 21:50:05 +02:00
|
|
|
_current->base.global_lock_count++;
|
2018-01-29 18:23:49 +01:00
|
|
|
|
kernel: Rework SMP irq_lock() compatibility layer
This was wrong in two ways, one subtle and one awful.
The subtle problem was that the IRQ lock isn't actually globally
recursive, it gets reset when you context switch (i.e. a _Swap()
implicitly releases and reacquires it). So the recursive count I was
keeping needs to be per-thread or else we risk deadlock any time we
swap away from a thread holding the lock.
And because part of my brain apparently knew this, there was an
"optimization" in the code that tested the current count vs. zero
outside the lock, on the argument that if it was non-zero we must
already hold the lock. Which would be true of a per-thread counter,
but NOT a global one: the other CPU may be holding that lock, and this
test will tell you *you* do. The upshot is that a recursive
irq_lock() would almost always SUCCEED INCORRECTLY when there was lock
contention. That this didn't break more things is amazing to me.
The rework is actually simpler than the original, thankfully. Though
there are some further subtleties:
* The lock state implied by irq_lock() allows the lock to be
implicitly released on context switch (i.e. you can _Swap() with the
lock held at a recursion level higher than 1, which needs to allow
other processes to run). So return paths into threads from _Swap()
and interrupt/exception exit need to check and restore the global
lock state, spinning as needed.
* The idle loop design specifies a k_cpu_idle() function that is on
common architectures expected to enable interrupts (for obvious
reasons), but there is no place to put non-arch code to wire it into
the global lock accounting. So on SMP, even CPU0 needs to use the
"dumb" spinning idle loop.
Finally this patch contains a simple bugfix too, found by inspection:
the interrupt return code used when CONFIG_SWITCH is enabled wasn't
correctly setting the active flag on the threads, opening up the
potential for a race that might result in a thread being scheduled on
two CPUs simultaneously.
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2018-04-12 21:50:05 +02:00
|
|
|
return key;
|
2018-01-29 18:23:49 +01:00
|
|
|
}
|
|
|
|
|
2019-03-08 22:19:05 +01:00
|
|
|
void z_smp_global_unlock(unsigned int key)
|
2018-01-29 18:23:49 +01:00
|
|
|
{
|
2022-07-19 22:30:17 +02:00
|
|
|
if (_current->base.global_lock_count != 0U) {
|
kernel: Rework SMP irq_lock() compatibility layer
This was wrong in two ways, one subtle and one awful.
The subtle problem was that the IRQ lock isn't actually globally
recursive, it gets reset when you context switch (i.e. a _Swap()
implicitly releases and reacquires it). So the recursive count I was
keeping needs to be per-thread or else we risk deadlock any time we
swap away from a thread holding the lock.
And because part of my brain apparently knew this, there was an
"optimization" in the code that tested the current count vs. zero
outside the lock, on the argument that if it was non-zero we must
already hold the lock. Which would be true of a per-thread counter,
but NOT a global one: the other CPU may be holding that lock, and this
test will tell you *you* do. The upshot is that a recursive
irq_lock() would almost always SUCCEED INCORRECTLY when there was lock
contention. That this didn't break more things is amazing to me.
The rework is actually simpler than the original, thankfully. Though
there are some further subtleties:
* The lock state implied by irq_lock() allows the lock to be
implicitly released on context switch (i.e. you can _Swap() with the
lock held at a recursion level higher than 1, which needs to allow
other processes to run). So return paths into threads from _Swap()
and interrupt/exception exit need to check and restore the global
lock state, spinning as needed.
* The idle loop design specifies a k_cpu_idle() function that is on
common architectures expected to enable interrupts (for obvious
reasons), but there is no place to put non-arch code to wire it into
the global lock accounting. So on SMP, even CPU0 needs to use the
"dumb" spinning idle loop.
Finally this patch contains a simple bugfix too, found by inspection:
the interrupt return code used when CONFIG_SWITCH is enabled wasn't
correctly setting the active flag on the threads, opening up the
potential for a race that might result in a thread being scheduled on
two CPUs simultaneously.
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2018-04-12 21:50:05 +02:00
|
|
|
_current->base.global_lock_count--;
|
|
|
|
|
|
|
|
if (!_current->base.global_lock_count) {
|
|
|
|
atomic_clear(&global_lock);
|
|
|
|
}
|
2018-01-29 18:23:49 +01:00
|
|
|
}
|
|
|
|
|
2019-11-07 21:43:29 +01:00
|
|
|
arch_irq_unlock(key);
|
kernel: Rework SMP irq_lock() compatibility layer
This was wrong in two ways, one subtle and one awful.
The subtle problem was that the IRQ lock isn't actually globally
recursive, it gets reset when you context switch (i.e. a _Swap()
implicitly releases and reacquires it). So the recursive count I was
keeping needs to be per-thread or else we risk deadlock any time we
swap away from a thread holding the lock.
And because part of my brain apparently knew this, there was an
"optimization" in the code that tested the current count vs. zero
outside the lock, on the argument that if it was non-zero we must
already hold the lock. Which would be true of a per-thread counter,
but NOT a global one: the other CPU may be holding that lock, and this
test will tell you *you* do. The upshot is that a recursive
irq_lock() would almost always SUCCEED INCORRECTLY when there was lock
contention. That this didn't break more things is amazing to me.
The rework is actually simpler than the original, thankfully. Though
there are some further subtleties:
* The lock state implied by irq_lock() allows the lock to be
implicitly released on context switch (i.e. you can _Swap() with the
lock held at a recursion level higher than 1, which needs to allow
other processes to run). So return paths into threads from _Swap()
and interrupt/exception exit need to check and restore the global
lock state, spinning as needed.
* The idle loop design specifies a k_cpu_idle() function that is on
common architectures expected to enable interrupts (for obvious
reasons), but there is no place to put non-arch code to wire it into
the global lock accounting. So on SMP, even CPU0 needs to use the
"dumb" spinning idle loop.
Finally this patch contains a simple bugfix too, found by inspection:
the interrupt return code used when CONFIG_SWITCH is enabled wasn't
correctly setting the active flag on the threads, opening up the
potential for a race that might result in a thread being scheduled on
two CPUs simultaneously.
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2018-04-12 21:50:05 +02:00
|
|
|
}
|
2018-01-29 18:23:49 +01:00
|
|
|
|
2019-03-08 22:19:05 +01:00
|
|
|
/* Called from within z_swap(), so assumes lock already held */
|
|
|
|
void z_smp_release_global_lock(struct k_thread *thread)
|
kernel: Rework SMP irq_lock() compatibility layer
This was wrong in two ways, one subtle and one awful.
The subtle problem was that the IRQ lock isn't actually globally
recursive, it gets reset when you context switch (i.e. a _Swap()
implicitly releases and reacquires it). So the recursive count I was
keeping needs to be per-thread or else we risk deadlock any time we
swap away from a thread holding the lock.
And because part of my brain apparently knew this, there was an
"optimization" in the code that tested the current count vs. zero
outside the lock, on the argument that if it was non-zero we must
already hold the lock. Which would be true of a per-thread counter,
but NOT a global one: the other CPU may be holding that lock, and this
test will tell you *you* do. The upshot is that a recursive
irq_lock() would almost always SUCCEED INCORRECTLY when there was lock
contention. That this didn't break more things is amazing to me.
The rework is actually simpler than the original, thankfully. Though
there are some further subtleties:
* The lock state implied by irq_lock() allows the lock to be
implicitly released on context switch (i.e. you can _Swap() with the
lock held at a recursion level higher than 1, which needs to allow
other processes to run). So return paths into threads from _Swap()
and interrupt/exception exit need to check and restore the global
lock state, spinning as needed.
* The idle loop design specifies a k_cpu_idle() function that is on
common architectures expected to enable interrupts (for obvious
reasons), but there is no place to put non-arch code to wire it into
the global lock accounting. So on SMP, even CPU0 needs to use the
"dumb" spinning idle loop.
Finally this patch contains a simple bugfix too, found by inspection:
the interrupt return code used when CONFIG_SWITCH is enabled wasn't
correctly setting the active flag on the threads, opening up the
potential for a race that might result in a thread being scheduled on
two CPUs simultaneously.
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2018-04-12 21:50:05 +02:00
|
|
|
{
|
|
|
|
if (!thread->base.global_lock_count) {
|
|
|
|
atomic_clear(&global_lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 02:08:20 +01:00
|
|
|
/* Tiny delay that relaxes bus traffic to avoid spamming a shared
|
|
|
|
* memory bus looking at an atomic variable
|
|
|
|
*/
|
|
|
|
static inline void local_delay(void)
|
|
|
|
{
|
|
|
|
for (volatile int i = 0; i < 1000; i++) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 19:28:01 +02:00
|
|
|
static void wait_for_start_signal(atomic_t *start_flag)
|
2018-01-17 20:34:50 +01:00
|
|
|
{
|
|
|
|
/* Wait for the signal to begin scheduling */
|
2023-08-03 19:28:01 +02:00
|
|
|
while (!atomic_get(start_flag)) {
|
2022-01-08 02:08:20 +01:00
|
|
|
local_delay();
|
2019-02-15 06:04:19 +01:00
|
|
|
}
|
2022-01-17 20:56:54 +01:00
|
|
|
}
|
2018-01-17 20:34:50 +01:00
|
|
|
|
2023-11-08 22:00:43 +01:00
|
|
|
static inline void smp_init_top(void *arg)
|
2021-04-01 13:46:57 +02:00
|
|
|
{
|
2024-04-26 09:37:58 +02:00
|
|
|
struct k_thread dummy_thread;
|
2024-01-29 18:13:54 +01:00
|
|
|
struct cpu_start_cb csc = arg ? *(struct cpu_start_cb *)arg : (struct cpu_start_cb){0};
|
2021-04-01 13:46:57 +02:00
|
|
|
|
2023-11-02 21:23:06 +01:00
|
|
|
/* Let start_cpu() know that this CPU has powered up. */
|
2022-01-08 02:08:20 +01:00
|
|
|
(void)atomic_set(&ready_flag, 1);
|
2022-01-17 20:56:54 +01:00
|
|
|
|
2023-11-02 21:23:06 +01:00
|
|
|
/* Wait for the CPU start caller to signal that
|
|
|
|
* we can start initialization.
|
|
|
|
*/
|
2023-11-06 23:29:35 +01:00
|
|
|
wait_for_start_signal(&cpu_start_flag);
|
2023-11-02 21:23:06 +01:00
|
|
|
|
2024-01-29 18:13:54 +01:00
|
|
|
if ((arg == NULL) || csc.invoke_sched) {
|
2024-01-31 19:54:53 +01:00
|
|
|
/* Initialize the dummy thread struct so that
|
|
|
|
* the scheduler can schedule actual threads to run.
|
|
|
|
*/
|
2024-04-26 09:37:58 +02:00
|
|
|
z_dummy_thread_init(&dummy_thread);
|
2024-01-31 19:54:53 +01:00
|
|
|
}
|
|
|
|
|
2023-06-19 18:18:03 +02:00
|
|
|
#ifdef CONFIG_SYS_CLOCK_EXISTS
|
2024-01-29 18:13:54 +01:00
|
|
|
if ((arg == NULL) || csc.reinit_timer) {
|
2023-11-08 22:00:43 +01:00
|
|
|
smp_timer_init();
|
|
|
|
}
|
2024-03-08 12:00:10 +01:00
|
|
|
#endif /* CONFIG_SYS_CLOCK_EXISTS */
|
2022-01-17 20:56:54 +01:00
|
|
|
|
2023-11-06 23:29:35 +01:00
|
|
|
/* Do additional initialization steps if needed. */
|
2024-01-29 18:13:54 +01:00
|
|
|
if (csc.fn != NULL) {
|
|
|
|
csc.fn(csc.arg);
|
2023-11-06 23:29:35 +01:00
|
|
|
}
|
|
|
|
|
2024-01-29 18:13:54 +01:00
|
|
|
if ((arg != NULL) && !csc.invoke_sched) {
|
2023-11-08 22:00:43 +01:00
|
|
|
/* Don't invoke scheduler. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-02 21:23:06 +01:00
|
|
|
/* Let scheduler decide what thread to run next. */
|
2019-03-08 22:19:05 +01:00
|
|
|
z_swap_unlocked();
|
2018-01-17 20:34:50 +01:00
|
|
|
|
2021-01-15 10:09:58 +01:00
|
|
|
CODE_UNREACHABLE; /* LCOV_EXCL_LINE */
|
2018-01-17 20:34:50 +01:00
|
|
|
}
|
2021-08-18 15:28:11 +02:00
|
|
|
|
2023-11-06 23:29:35 +01:00
|
|
|
static void start_cpu(int id, struct cpu_start_cb *csc)
|
2021-08-18 15:28:11 +02:00
|
|
|
{
|
2023-11-02 21:23:06 +01:00
|
|
|
/* Clear the ready flag so the newly powered up CPU can
|
|
|
|
* signal that it has powered up.
|
|
|
|
*/
|
2022-01-17 20:56:54 +01:00
|
|
|
(void)atomic_clear(&ready_flag);
|
2023-11-02 21:23:06 +01:00
|
|
|
|
|
|
|
/* Power up the CPU */
|
2023-11-08 18:05:17 +01:00
|
|
|
arch_cpu_start(id, z_interrupt_stacks[id], CONFIG_ISR_STACK_SIZE,
|
2023-11-06 23:29:35 +01:00
|
|
|
smp_init_top, csc);
|
2023-11-02 21:23:06 +01:00
|
|
|
|
|
|
|
/* Wait until the newly powered up CPU to signal that
|
|
|
|
* it has powered up.
|
|
|
|
*/
|
2022-01-17 20:56:54 +01:00
|
|
|
while (!atomic_get(&ready_flag)) {
|
|
|
|
local_delay();
|
|
|
|
}
|
2021-08-18 15:28:11 +02:00
|
|
|
}
|
|
|
|
|
2023-11-06 23:29:35 +01:00
|
|
|
void k_smp_cpu_start(int id, smp_init_fn fn, void *arg)
|
2022-01-17 20:56:54 +01:00
|
|
|
{
|
2023-11-06 23:29:35 +01:00
|
|
|
k_spinlock_key_t key = k_spin_lock(&cpu_start_lock);
|
|
|
|
|
|
|
|
cpu_start_fn.fn = fn;
|
|
|
|
cpu_start_fn.arg = arg;
|
2023-11-08 22:00:43 +01:00
|
|
|
cpu_start_fn.invoke_sched = true;
|
|
|
|
|
|
|
|
#ifdef CONFIG_SYS_CLOCK_EXISTS
|
|
|
|
cpu_start_fn.reinit_timer = true;
|
2024-03-08 12:00:10 +01:00
|
|
|
#endif /* CONFIG_SYS_CLOCK_EXISTS */
|
2023-11-08 22:00:43 +01:00
|
|
|
|
|
|
|
/* We are only starting one CPU so we do not need to synchronize
|
|
|
|
* across all CPUs using the start_flag. So just set it to 1.
|
|
|
|
*/
|
|
|
|
(void)atomic_set(&cpu_start_flag, 1); /* async, don't care */
|
|
|
|
|
|
|
|
/* Initialize various CPU structs related to this CPU. */
|
|
|
|
z_init_cpu(id);
|
|
|
|
|
|
|
|
/* Start the CPU! */
|
|
|
|
start_cpu(id, &cpu_start_fn);
|
|
|
|
|
|
|
|
k_spin_unlock(&cpu_start_lock, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void k_smp_cpu_resume(int id, smp_init_fn fn, void *arg,
|
|
|
|
bool reinit_timer, bool invoke_sched)
|
|
|
|
{
|
|
|
|
k_spinlock_key_t key = k_spin_lock(&cpu_start_lock);
|
|
|
|
|
|
|
|
cpu_start_fn.fn = fn;
|
|
|
|
cpu_start_fn.arg = arg;
|
|
|
|
cpu_start_fn.invoke_sched = invoke_sched;
|
|
|
|
|
|
|
|
#ifdef CONFIG_SYS_CLOCK_EXISTS
|
|
|
|
cpu_start_fn.reinit_timer = reinit_timer;
|
|
|
|
#else
|
|
|
|
ARG_UNUSED(reinit_timer);
|
2024-03-08 12:00:10 +01:00
|
|
|
#endif /* CONFIG_SYS_CLOCK_EXISTS */
|
2023-11-06 23:29:35 +01:00
|
|
|
|
2023-11-02 21:23:06 +01:00
|
|
|
/* We are only starting one CPU so we do not need to synchronize
|
|
|
|
* across all CPUs using the start_flag. So just set it to 1.
|
|
|
|
*/
|
|
|
|
(void)atomic_set(&cpu_start_flag, 1);
|
|
|
|
|
|
|
|
/* Start the CPU! */
|
2023-11-06 23:29:35 +01:00
|
|
|
start_cpu(id, &cpu_start_fn);
|
|
|
|
|
|
|
|
k_spin_unlock(&cpu_start_lock, key);
|
2022-01-17 20:56:54 +01:00
|
|
|
}
|
2018-01-17 20:34:50 +01:00
|
|
|
|
2019-06-05 17:58:42 +02:00
|
|
|
void z_smp_init(void)
|
2018-01-17 20:34:50 +01:00
|
|
|
{
|
2023-11-02 21:23:06 +01:00
|
|
|
/* We are powering up all CPUs and we want to synchronize their
|
|
|
|
* entry into scheduler. So set the start flag to 0 here.
|
|
|
|
*/
|
2023-08-03 19:28:01 +02:00
|
|
|
(void)atomic_clear(&cpu_start_flag);
|
2022-10-18 16:45:13 +02:00
|
|
|
|
2023-11-02 21:23:06 +01:00
|
|
|
/* Just start CPUs one by one. */
|
2022-10-18 16:45:13 +02:00
|
|
|
unsigned int num_cpus = arch_num_cpus();
|
|
|
|
|
|
|
|
for (int i = 1; i < num_cpus; i++) {
|
2023-11-08 22:00:43 +01:00
|
|
|
z_init_cpu(i);
|
2023-11-06 23:29:35 +01:00
|
|
|
start_cpu(i, NULL);
|
2020-03-12 23:37:29 +01:00
|
|
|
}
|
2023-11-02 21:23:06 +01:00
|
|
|
|
|
|
|
/* Let loose those CPUs so they can start scheduling
|
|
|
|
* threads to run.
|
|
|
|
*/
|
2023-08-03 19:28:01 +02:00
|
|
|
(void)atomic_set(&cpu_start_flag, 1);
|
2018-01-17 20:34:50 +01:00
|
|
|
}
|
2019-02-20 01:03:39 +01:00
|
|
|
|
2020-02-06 22:39:52 +01:00
|
|
|
bool z_smp_cpu_mobile(void)
|
|
|
|
{
|
|
|
|
unsigned int k = arch_irq_lock();
|
|
|
|
bool pinned = arch_is_in_isr() || !arch_irq_unlocked(k);
|
|
|
|
|
|
|
|
arch_irq_unlock(k);
|
|
|
|
return !pinned;
|
|
|
|
}
|