| 1 |
|
| 2 |
|
| 3 |
#include <arch/barrier.h> |
| 4 |
#include <arch/encoding.h> |
| 5 |
#include <arch/smp/smp.h> |
| 6 |
#include <arch/smp/atomic.h> |
| 7 |
#include <console/console.h> |
| 8 |
#include <mcall.h> |
| 9 |
|
| 10 |
|
| 11 |
#define HART_SLEEPING 0x1 |
| 12 |
#define HART_AWAKE 0x2 |
| 13 |
|
| 14 |
void smp_pause(int working_hartid) |
| 15 |
{ |
| 16 |
int hartid = read_csr(mhartid); |
| 17 |
|
| 18 |
|
| 19 |
if (hartid != working_hartid) { |
| 20 |
clear_csr(mstatus, MSTATUS_MIE); |
| 21 |
set_msip(hartid, 0); |
| 22 |
write_csr(mie, MIP_MSIP); |
| 23 |
barrier(); |
| 24 |
atomic_set(&HLS()->entry.sync_a, HART_SLEEPING); |
| 25 |
|
| 26 |
|
| 27 |
do { |
| 28 |
__asm__ volatile ("wfi"); |
| 29 |
} while ((read_csr(mip) & MIP_MSIP) == 0); |
| 30 |
|
| 31 |
atomic_set(&HLS()->entry.sync_a, HART_AWAKE); |
| 32 |
HLS()->entry.fn(HLS()->entry.arg); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
void smp_resume(void (*fn)(void *), void *arg) |
| 38 |
{ |
| 39 |
if (fn == NULL) { |
| 40 |
printk(BIOS_ERR, "must pass a non-null function pointer\n"); |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
int working_hartid = read_csr(mhartid); |
| 45 |
|
| 46 |
int hart_count = CONFIG_MAX_CPUS; |
| 47 |
if (CONFIG(RISCV_GET_HART_COUNT_AT_RUNTIME)) |
| 48 |
hart_count = smp_get_hart_count(); |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
u32 count_awake_harts = 0; |
| 53 |
for (int i = 0; i < hart_count; i++) { |
| 54 |
|
| 55 |
if (i == working_hartid) |
| 56 |
continue; |
| 57 |
|
| 58 |
if (atomic_read(&OTHER_HLS(i)->entry.sync_a) != HART_SLEEPING) { |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
printk(BIOS_ERR, "hart %d did not enter smp_pause\n", i); |
| 65 |
OTHER_HLS(i)->enabled = 0; |
| 66 |
} else { |
| 67 |
|
| 68 |
|
| 69 |
OTHER_HLS(i)->entry.fn = fn; |
| 70 |
OTHER_HLS(i)->entry.arg = arg; |
| 71 |
barrier(); |
| 72 |
set_msip(i, 1); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
printk(BIOS_DEBUG, "waiting for all harts to wake up...\n"); |
| 77 |
|
| 78 |
for (int i = 0; i < hart_count; i++) { |
| 79 |
|
| 80 |
if (i == working_hartid || !OTHER_HLS(i)->enabled) |
| 81 |
continue; |
| 82 |
|
| 83 |
|
| 84 |
while (atomic_read(&OTHER_HLS(i)->entry.sync_a) != HART_AWAKE) |
| 85 |
; |
| 86 |
count_awake_harts++; |
| 87 |
} |
| 88 |
printk(BIOS_DEBUG, "all harts up and running...\n"); |
| 89 |
|
| 90 |
if ((hart_count - 1) != count_awake_harts) { |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
printk(BIOS_ERR, "some harts were too slow and could not resume\n"); |
| 97 |
} |
| 98 |
fn(arg); |
| 99 |
} |