-
Notifications
You must be signed in to change notification settings - Fork 0
/
smp.cpp
89 lines (73 loc) · 2.09 KB
/
smp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
// Created by per on 2016-04-15.
//
#include <stdint.h>
#include <stdio.h>
#include "smp.h"
#include "mmu.h"
#include "fpu.h"
#include "system.h"
#include "coprocessor.h"
extern "C" void park_cpus();
namespace smp {
// Setup SMP (Boot Offset = $4000008C + ($10 * Core), Core = 1..3)
enum {
CORE_BASE = 0x4000008C,
Core0Boot = 0x00, // Core 0 Boot Offset
Core1Boot = 0x10, // Core 1 Boot Offset
Core2Boot = 0x20, // Core 2 Boot Offset
Core3Boot = 0x30, // Core 3 Boot Offset
};
typedef void (*fn)(void);
constexpr volatile fn *mailbox(int core) {
return (volatile fn *) (CORE_BASE + core * 0x10);
}
// Multiprocessor Affinity Register (MPIDR)
uint_fast8_t getCoreId() {
uint32_t mpidr;
READ_CP32(mpidr, MPIDR);
return mpidr & 0x3;
}
extern "C" {
void core_main(void);
}
volatile bool started;
volatile static start_fn_t tasks[4];
static void* taskArgs[4];
static inline void delay(int32_t count)
{
asm volatile("__delay_%=: subs %[count], %[count], #1; bne __delay_%=\n"
: : [count]"r"(count) : "cc");
}
void nullTask(void) {
uint_fast8_t coreId = getCoreId();
while(true) {
if (tasks[coreId]) {
tasks[coreId](taskArgs[coreId]);
tasks[coreId] = nullptr;
}
}
}
void core_main(void) {
System::uart().write("core is up: ID = ");
System::uart().writeU32(getCoreId());
System::uart().write("\n");
cpu::core::mmu::enable();
System::uart().write("core is virtual\n");
cpu::core::fpu::enable();
printf("core is floating\n");
started = true;
nullTask();
}
void start_core(int core) {
printf("starting core %c\n", "0123"[core]);
started = false;
*mailbox(core) = core_wakeup;
while (!started) { }
printf("started core %c\n", "0123"[core]);
}
void run(int core, start_fn_t fun, void* arg) {
taskArgs[core] = arg;
tasks[core] = fun;
}
}