|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Meta Platforms |
| 3 | + * Copyright (c) 2021 Intel Corporation |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + */ |
| 7 | + |
| 8 | +#include <ipi.h> |
| 9 | + |
| 10 | +#include <zephyr/devicetree.h> |
| 11 | +#include <zephyr/drivers/interrupt_controller/riscv_plic.h> |
| 12 | +#include <zephyr/kernel.h> |
| 13 | +#include <zephyr/sys/util.h> |
| 14 | + |
| 15 | +#ifdef CONFIG_FPU_SHARING |
| 16 | +#define FPU_IPI_NODE DT_NODELABEL(fpu_ipi) |
| 17 | +#define FPU_IPI_NUM_IRQS DT_NUM_IRQS(FPU_IPI_NODE) |
| 18 | +#define FPU_IPI_IRQ(n) DT_IRQN_BY_IDX(FPU_IPI_NODE, n + CONFIG_MP_MAX_NUM_CPUS) |
| 19 | +#define FPU_IPI_IRQS_FN(n, _) DT_IRQN_BY_IDX(FPU_IPI_NODE, n) |
| 20 | +static const uint32_t fpu_ipi_irqs[FPU_IPI_NUM_IRQS] = { |
| 21 | + LISTIFY(FPU_IPI_NUM_IRQS, FPU_IPI_IRQS_FN, (,)), |
| 22 | +}; |
| 23 | + |
| 24 | +static ALWAYS_INLINE void send_fpu_ipi(int cpu) |
| 25 | +{ |
| 26 | + riscv_plic_irq_set_pending(fpu_ipi_irqs[cpu]); |
| 27 | +} |
| 28 | + |
| 29 | +static ALWAYS_INLINE bool fpu_ipi_irq_is_pending(int cpu) |
| 30 | +{ |
| 31 | + return riscv_plic_irq_is_pending(fpu_ipi_irqs[cpu]); |
| 32 | +} |
| 33 | + |
| 34 | +static ALWAYS_INLINE void fpu_ipi_irq_clear_pending(int cpu) |
| 35 | +{ |
| 36 | + riscv_plic_irq_clear_pending(fpu_ipi_irqs[cpu]); |
| 37 | +} |
| 38 | + |
| 39 | +static void fpu_ipi_handler(const void *arg) |
| 40 | +{ |
| 41 | + ARG_UNUSED(arg); |
| 42 | + |
| 43 | + /* disable IRQs */ |
| 44 | + csr_clear(mstatus, MSTATUS_IEN); |
| 45 | + /* perform the flush */ |
| 46 | + arch_flush_local_fpu(); |
| 47 | + /* |
| 48 | + * No need to re-enable IRQs here as long as |
| 49 | + * this remains the last case. |
| 50 | + */ |
| 51 | +} |
| 52 | + |
| 53 | +void arch_flush_fpu_ipi(unsigned int cpu) |
| 54 | +{ |
| 55 | + send_fpu_ipi(i); |
| 56 | +} |
| 57 | + |
| 58 | +/* |
| 59 | + * Make sure there is no pending FPU flush request for this CPU while |
| 60 | + * waiting for a contended spinlock to become available. This prevents |
| 61 | + * a deadlock when the lock we need is already taken by another CPU |
| 62 | + * that also wants its FPU content to be reinstated while such content |
| 63 | + * is still live in this CPU's FPU. |
| 64 | + */ |
| 65 | +void arch_spin_relax(void) |
| 66 | +{ |
| 67 | + int cpu = _current_cpu->id; |
| 68 | + |
| 69 | + if (fpu_ipi_irq_is_pending(cpu)) { |
| 70 | + fpu_ipi_irq_clear_pending(cpu); |
| 71 | + /* |
| 72 | + * We may not be in IRQ context here hence cannot use |
| 73 | + * arch_flush_local_fpu() directly. |
| 74 | + */ |
| 75 | + arch_float_disable(_current_cpu->arch.fpu_owner); |
| 76 | + } |
| 77 | +} |
| 78 | +#define FPU_IPI_IRQ_CONNECT(n, _) \ |
| 79 | + IRQ_CONNECT(FPU_IPI_IRQ(n), 1, fpu_ipi_handler, UINT_TO_POINTER(n), 0); \ |
| 80 | + irq_enable(FPU_IPI_IRQ(n)); \ |
| 81 | + riscv_plic_irq_set_affinity(FPU_IPI_IRQ(n), BIT(n)) |
| 82 | + |
| 83 | +#define fpu_ipi_irqs_setup() LISTIFY(CONFIG_MP_MAX_NUM_CPUS, FPU_IPI_IRQ_CONNECT, (;)) |
| 84 | +#else |
| 85 | +#define fpu_ipi_irqs_setup() |
| 86 | +#endif /* CONFIG_FPU_SHARING */ |
| 87 | + |
| 88 | +#define SCHED_IPI_NODE DT_NODELABEL(sched_ipi) |
| 89 | +#define SCHED_IPI_NUM_IRQS DT_NUM_IRQS(SCHED_IPI_NODE) |
| 90 | +#define SCHED_IPI_IRQ(n) DT_IRQN_BY_IDX(SCHED_IPI_NODE, n) |
| 91 | +#define SCHED_IPI_IRQS_FN(n, _) DT_IRQN_BY_IDX(SCHED_IPI_NODE, n) |
| 92 | +static const uint32_t sched_ipi_irqs[SCHED_IPI_NUM_IRQS] = { |
| 93 | + LISTIFY(SCHED_IPI_NUM_IRQS, SCHED_IPI_IRQS_FN, (,)), |
| 94 | +}; |
| 95 | + |
| 96 | +static ALWAYS_INLINE void send_sched_ipi(int cpu) |
| 97 | +{ |
| 98 | + riscv_plic_irq_set_pending(sched_ipi_irqs[cpu]); |
| 99 | +} |
| 100 | + |
| 101 | +void arch_sched_directed_ipi(uint32_t cpu_bitmap) |
| 102 | +{ |
| 103 | + unsigned int key = arch_irq_lock(); |
| 104 | + unsigned int id = _current_cpu->id; |
| 105 | + unsigned int num_cpus = arch_num_cpus(); |
| 106 | + |
| 107 | + for (unsigned int i = 0; i < num_cpus; i++) { |
| 108 | + if ((i != id) && _kernel.cpus[i].arch.online && ((cpu_bitmap & BIT(i)) != 0)) { |
| 109 | + send_sched_ipi(i); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + arch_irq_unlock(key); |
| 114 | +} |
| 115 | + |
| 116 | +static void sched_ipi_handler(const void *arg) |
| 117 | +{ |
| 118 | + ARG_UNUSED(arg); |
| 119 | + |
| 120 | + z_sched_ipi(); |
| 121 | +} |
| 122 | + |
| 123 | +#define SCHED_IPI_IRQ_CONNECT(n, _) \ |
| 124 | + IRQ_CONNECT(SCHED_IPI_IRQ(n), 1, sched_ipi_handler, UINT_TO_POINTER(n), 0); \ |
| 125 | + irq_enable(SCHED_IPI_IRQ(n)); \ |
| 126 | + riscv_plic_irq_set_affinity(SCHED_IPI_IRQ(n), BIT(n)) |
| 127 | + |
| 128 | +#define sched_ipi_irqs_setup() LISTIFY(CONFIG_MP_MAX_NUM_CPUS, SCHED_IPI_IRQ_CONNECT, (;)) |
| 129 | + |
| 130 | +int arch_smp_init(void) |
| 131 | +{ |
| 132 | + sched_ipi_irqs_setup(); |
| 133 | + fpu_ipi_irqs_setup(); |
| 134 | + |
| 135 | + return 0; |
| 136 | +} |
| 137 | + |
0 commit comments