rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 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 | #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 | |
| 33 | typedef 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 | |
| 42 | typedef enum { |
| 43 | MP_IPI_GENERIC, |
| 44 | MP_IPI_RESCHEDULE, |
| 45 | } mp_ipi_t; |
| 46 | |
| 47 | #ifdef WITH_SMP |
| 48 | void mp_init(void); |
| 49 | |
| 50 | void mp_reschedule(mp_cpu_mask_t target, uint flags); |
| 51 | void mp_set_curr_cpu_active(bool active); |
| 52 | |
| 53 | /* called from arch code during reschedule irq */ |
| 54 | enum handler_return mp_mbx_reschedule_irq(void); |
| 55 | |
| 56 | /* global mp state to track what the cpus are up to */ |
| 57 | struct 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 | |
| 65 | extern struct mp_state mp; |
| 66 | |
| 67 | static inline int mp_is_cpu_active(uint cpu) |
| 68 | { |
| 69 | return mp.active_cpus & (1 << cpu); |
| 70 | } |
| 71 | |
| 72 | static 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 */ |
| 78 | static inline void mp_set_cpu_idle(uint cpu) |
| 79 | { |
| 80 | mp.idle_cpus |= 1UL << cpu; |
| 81 | } |
| 82 | |
| 83 | static inline void mp_set_cpu_busy(uint cpu) |
| 84 | { |
| 85 | mp.idle_cpus &= ~(1UL << cpu); |
| 86 | } |
| 87 | |
| 88 | static inline mp_cpu_mask_t mp_get_idle_mask(void) |
| 89 | { |
| 90 | return mp.idle_cpus; |
| 91 | } |
| 92 | |
| 93 | static inline void mp_set_cpu_realtime(uint cpu) |
| 94 | { |
| 95 | mp.realtime_cpus |= 1UL << cpu; |
| 96 | } |
| 97 | |
| 98 | static inline void mp_set_cpu_non_realtime(uint cpu) |
| 99 | { |
| 100 | mp.realtime_cpus &= ~(1UL << cpu); |
| 101 | } |
| 102 | |
| 103 | static inline mp_cpu_mask_t mp_get_realtime_mask(void) |
| 104 | { |
| 105 | return mp.realtime_cpus; |
| 106 | } |
| 107 | #else |
| 108 | static inline void mp_init(void) {} |
| 109 | static inline void mp_reschedule(mp_cpu_mask_t target, uint flags) {} |
| 110 | static inline void mp_set_curr_cpu_active(bool active) {} |
| 111 | |
| 112 | static 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... |
| 115 | static inline int mp_is_cpu_active(uint cpu) { return 1; } |
| 116 | static inline int mp_is_cpu_idle(uint cpu) { return 0; } |
| 117 | |
| 118 | static inline void mp_set_cpu_idle(uint cpu) {} |
| 119 | static inline void mp_set_cpu_busy(uint cpu) {} |
| 120 | |
| 121 | static inline mp_cpu_mask_t mp_get_idle_mask(void) { return 0; } |
| 122 | |
| 123 | static inline void mp_set_cpu_realtime(uint cpu) {} |
| 124 | static inline void mp_set_cpu_non_realtime(uint cpu) {} |
| 125 | |
| 126 | static inline mp_cpu_mask_t mp_get_realtime_mask(void) { return 0; } |
| 127 | #endif |
| 128 | |
| 129 | __END_CDECLS; |