1 /*
2 * Copyright (c) 2014 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #include <kernel/mp.h>
25
26 #include <stdlib.h>
27 #include <debug.h>
28 #include <assert.h>
29 #include <trace.h>
30 #include <arch/mp.h>
31 #include <kernel/spinlock.h>
32
33 #define LOCAL_TRACE 0
34
35 #if WITH_SMP
36 /* a global state structure, aligned on cpu cache line to minimize aliasing */
37 struct mp_state mp __CPU_ALIGN;
38
mp_init(void)39 void mp_init(void)
40 {
41 }
42
mp_reschedule(mp_cpu_mask_t target,uint flags)43 void mp_reschedule(mp_cpu_mask_t target, uint flags)
44 {
45 uint local_cpu = arch_curr_cpu_num();
46
47 LTRACEF("local %d, target 0x%x\n", local_cpu, target);
48
49 /* mask out cpus that are not active and the local cpu */
50 target &= mp.active_cpus;
51
52 /* mask out cpus that are currently running realtime code */
53 if ((flags & MP_RESCHEDULE_FLAG_REALTIME) == 0) {
54 target &= ~mp.realtime_cpus;
55 }
56 target &= ~(1U << local_cpu);
57
58 LTRACEF("local %d, post mask target now 0x%x\n", local_cpu, target);
59
60 arch_mp_send_ipi(target, MP_IPI_RESCHEDULE);
61 }
62
mp_set_curr_cpu_active(bool active)63 void mp_set_curr_cpu_active(bool active)
64 {
65 atomic_or((volatile int *)&mp.active_cpus, 1U << arch_curr_cpu_num());
66 }
67
mp_mbx_reschedule_irq(void)68 enum handler_return mp_mbx_reschedule_irq(void)
69 {
70 uint cpu = arch_curr_cpu_num();
71
72 LTRACEF("cpu %u\n", cpu);
73
74 THREAD_STATS_INC(reschedule_ipis);
75
76 return (mp.active_cpus & (1U << cpu)) ? INT_RESCHEDULE : INT_NO_RESCHEDULE;
77 }
78 #endif
79