blob: a015ebdc6c470d46eca19547af756f59f0c2a284 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
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#pragma once
24
25#include <compiler.h>
26#include <limits.h>
27#include <stdbool.h>
28#include <stdint.h>
29#include <kernel/thread.h>
30
31__BEGIN_CDECLS;
32
33typedef uint32_t mp_cpu_mask_t;
34
35#define MP_CPU_ALL_BUT_LOCAL (UINT32_MAX)
36
37/* by default, mp_mbx_reschedule does not signal to cpus that are running realtime
38 * threads. Override this behavior.
39 */
40#define MP_RESCHEDULE_FLAG_REALTIME (0x1)
41
42typedef enum {
43 MP_IPI_GENERIC,
44 MP_IPI_RESCHEDULE,
45} mp_ipi_t;
46
47#ifdef WITH_SMP
48void mp_init(void);
49
50void mp_reschedule(mp_cpu_mask_t target, uint flags);
51void mp_set_curr_cpu_active(bool active);
52
53/* called from arch code during reschedule irq */
54enum handler_return mp_mbx_reschedule_irq(void);
55
56/* global mp state to track what the cpus are up to */
57struct mp_state {
58 volatile mp_cpu_mask_t active_cpus;
59
60 /* only safely accessible with thread lock held */
61 mp_cpu_mask_t idle_cpus;
62 mp_cpu_mask_t realtime_cpus;
63};
64
65extern struct mp_state mp;
66
67static inline int mp_is_cpu_active(uint cpu)
68{
69 return mp.active_cpus & (1 << cpu);
70}
71
72static inline int mp_is_cpu_idle(uint cpu)
73{
74 return mp.idle_cpus & (1 << cpu);
75}
76
77/* must be called with the thread lock held */
78static inline void mp_set_cpu_idle(uint cpu)
79{
80 mp.idle_cpus |= 1UL << cpu;
81}
82
83static inline void mp_set_cpu_busy(uint cpu)
84{
85 mp.idle_cpus &= ~(1UL << cpu);
86}
87
88static inline mp_cpu_mask_t mp_get_idle_mask(void)
89{
90 return mp.idle_cpus;
91}
92
93static inline void mp_set_cpu_realtime(uint cpu)
94{
95 mp.realtime_cpus |= 1UL << cpu;
96}
97
98static inline void mp_set_cpu_non_realtime(uint cpu)
99{
100 mp.realtime_cpus &= ~(1UL << cpu);
101}
102
103static inline mp_cpu_mask_t mp_get_realtime_mask(void)
104{
105 return mp.realtime_cpus;
106}
107#else
108static inline void mp_init(void) {}
109static inline void mp_reschedule(mp_cpu_mask_t target, uint flags) {}
110static inline void mp_set_curr_cpu_active(bool active) {}
111
112static inline enum handler_return mp_mbx_reschedule_irq(void) { return 0; }
113
114// only one cpu exists in UP and if you're calling these functions, it's active...
115static inline int mp_is_cpu_active(uint cpu) { return 1; }
116static inline int mp_is_cpu_idle(uint cpu) { return 0; }
117
118static inline void mp_set_cpu_idle(uint cpu) {}
119static inline void mp_set_cpu_busy(uint cpu) {}
120
121static inline mp_cpu_mask_t mp_get_idle_mask(void) { return 0; }
122
123static inline void mp_set_cpu_realtime(uint cpu) {}
124static inline void mp_set_cpu_non_realtime(uint cpu) {}
125
126static inline mp_cpu_mask_t mp_get_realtime_mask(void) { return 0; }
127#endif
128
129__END_CDECLS;