-
-
Notifications
You must be signed in to change notification settings - Fork 268
Expand file tree
/
Copy pathsmp.c
More file actions
605 lines (486 loc) · 15.2 KB
/
smp.c
File metadata and controls
605 lines (486 loc) · 15.2 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/* SPDX-License-Identifier: MIT */
#include "smp.h"
#include "adt.h"
#include "aic.h"
#include "aic_regs.h"
#include "cpu_regs.h"
#include "exception.h"
#include "fb.h"
#include "malloc.h"
#include "memory.h"
#include "pmgr.h"
#include "soc.h"
#include "string.h"
#include "types.h"
#include "utils.h"
#define CPU_START_OFF_S5L8960X 0x30000
#define CPU_START_OFF_S8000 0xd4000
#define CPU_START_OFF_T8103 0x54000
#define CPU_START_OFF_T8112 0x34000
#define CPU_START_OFF_T6020 0x28000
#define CPU_START_OFF_T6031 0x88000
#define CPU_REG_CORE GENMASK(7, 0)
#define CPU_REG_CLUSTER GENMASK(10, 8)
#define CPU_REG_DIE GENMASK(14, 11)
struct spin_table {
u64 mpidr;
u64 flag;
u64 target;
u64 args[4];
u64 retval;
};
void *_reset_stack;
void *_reset_stack_el1;
#define DUMMY_STACK_SIZE 0x1000
u8 dummy_stack[DUMMY_STACK_SIZE]; // Highest EL
u8 dummy_stack_el1[DUMMY_STACK_SIZE]; // EL1 stack if EL3 exists
u8 *secondary_stacks[MAX_CPUS] = {dummy_stack};
u8 *secondary_stacks_el3[MAX_EL3_CPUS];
static bool wfe_mode = false;
static int target_cpu;
static int cpu_nodes[MAX_CPUS];
static struct spin_table spin_table[MAX_CPUS];
static u64 pmgr_reg;
static u64 cpu_start_off;
extern u8 _vectors_start[0];
int boot_cpu_idx = -1;
u64 boot_cpu_mpidr = 0;
void smp_secondary_entry(void)
{
struct spin_table *me = &spin_table[target_cpu];
if (in_el2())
msr(TPIDR_EL2, target_cpu);
else
msr(TPIDR_EL1, target_cpu);
printf(" Index: %d (table: %p)\n\n", target_cpu, me);
me->mpidr = mrs(MPIDR_EL1) & 0xFFFFFF;
sysop("dmb sy");
me->flag = 1;
sysop("dmb sy");
u64 target;
if (!cpu_features->fast_ipi)
aic_write(AIC_IPI_MASK_SET, AIC_IPI_SELF); // we only use the "other" IPI
while (1) {
while (!(target = me->target)) {
if (wfe_mode) {
sysop("wfe");
} else {
deep_wfi();
if (cpu_features->fast_ipi) {
msr(SYS_IMP_APL_IPI_SR_EL1, 1);
} else {
aic_ack(); // Actually read IPI reason
aic_write(AIC_IPI_ACK, AIC_IPI_OTHER);
aic_write(AIC_IPI_MASK_CLR, AIC_IPI_OTHER);
}
}
sysop("isb");
}
sysop("dmb sy");
me->flag++;
sysop("dmb sy");
me->retval = ((u64 (*)(u64 a, u64 b, u64 c, u64 d))target)(me->args[0], me->args[1],
me->args[2], me->args[3]);
sysop("dmb sy");
me->target = 0;
sysop("dmb sy");
}
}
void smp_secondary_prep_el3(void)
{
msr(TPIDR_EL3, target_cpu);
return;
}
static void smp_prepare_cpu(int index)
{
memset(&spin_table[index], 0, sizeof(struct spin_table));
target_cpu = index;
secondary_stacks[index] = memalign(0x4000, SECONDARY_STACK_SIZE);
if (has_el3()) {
secondary_stacks_el3[index] = memalign(0x4000, SECONDARY_STACK_SIZE);
_reset_stack = secondary_stacks_el3[index] + SECONDARY_STACK_SIZE; // EL3
_reset_stack_el1 = secondary_stacks[index] + SECONDARY_STACK_SIZE; // EL1
dc_civac_range(&_reset_stack_el1, sizeof(void *));
} else
_reset_stack = secondary_stacks[index] + SECONDARY_STACK_SIZE;
dc_civac_range(&_reset_stack, sizeof(void *));
sysop("dsb sy");
}
static void smp_start_cpu(int index, int die, int cluster, int core, u64 impl, u64 cpu_start_base)
{
int i;
if (index >= MAX_CPUS)
return;
if (has_el3() && index >= MAX_EL3_CPUS)
return;
if (spin_table[index].flag)
return;
printf("Starting CPU %d (%d:%d:%d)... ", index, die, cluster, core);
smp_prepare_cpu(index);
write64(impl, (u64)_vectors_start);
cpu_start_base += die * PMGR_DIE_OFFSET;
// Some kind of system level startup/status bit
// Without this, IRQs don't work
write32(cpu_start_base + 0x4, 1 << (4 * cluster + core));
// Actually start the core
write32(cpu_start_base + 0x8 + 4 * cluster, 1 << core);
for (i = 0; i < 100; i++) {
sysop("dmb ld");
if (spin_table[index].flag)
break;
udelay(1000);
}
if (i >= 100)
printf("Failed!\n");
else
printf(" Started.\n");
_reset_stack = dummy_stack + DUMMY_STACK_SIZE;
_reset_stack_el1 = dummy_stack_el1 + DUMMY_STACK_SIZE;
}
static void smp_stop_cpu(int index, int die, int cluster, int core, u64 impl, u64 cpu_start_base,
bool deep_sleep)
{
int i;
if (index >= MAX_CPUS)
return;
if (!spin_table[index].flag)
return;
printf("Stopping CPU %d (%d:%d:%d)... ", index, die, cluster, core);
cpu_start_base += die * PMGR_DIE_OFFSET;
// Request CPU stop
write32(cpu_start_base + 0x0, 1 << (4 * cluster + core));
u64 dsleep = deep_sleep;
// Put the CPU to sleep
smp_call1(index, cpu_sleep, dsleep);
// If going into deep sleep, powering off the last core in a cluster kills our register
// access, so just wait a bit.
if (deep_sleep) {
udelay(10000);
printf(" Presumed stopped.\n");
memset(&spin_table[index], 0, sizeof(struct spin_table));
return;
}
// Check that it actually shut down
for (i = 0; i < 50; i++) {
sysop("dmb ld");
if (!(read64(impl + 0x100) & 0xff))
break;
udelay(1000);
}
if (i >= 50) {
printf("Failed!\n");
} else {
printf(" Stopped.\n");
memset(&spin_table[index], 0, sizeof(struct spin_table));
}
}
void smp_start_secondaries(void)
{
printf("Starting secondary CPUs...\n");
int pmgr_path[8];
if (adt_path_offset_trace(adt, "/arm-io/pmgr", pmgr_path) < 0) {
printf("Error getting /arm-io/pmgr node\n");
return;
}
if (adt_get_reg(adt, pmgr_path, "reg", 0, &pmgr_reg, NULL) < 0) {
printf("Error getting /arm-io/pmgr regs\n");
return;
}
int arm_io_node;
if ((arm_io_node = adt_path_offset(adt, "/arm-io")) < 0) {
printf("Error getting /arm-io node\n");
return;
}
int node = adt_path_offset(adt, "/cpus");
if (node < 0) {
printf("Error getting /cpus node\n");
return;
}
memset(cpu_nodes, 0, sizeof(cpu_nodes));
switch (chip_id) {
case S5L8960X:
case T7000:
case T7001:
cpu_start_off = CPU_START_OFF_S5L8960X;
break;
case S8000:
case S8001:
case S8003:
case T8010:
case T8011:
case T8012:
case T8015:
cpu_start_off = CPU_START_OFF_S8000;
break;
case T8103:
case T6000:
case T6001:
case T6002:
cpu_start_off = CPU_START_OFF_T8103;
break;
case T8112:
case T8122:
cpu_start_off = CPU_START_OFF_T8112;
break;
case T6020:
case T6021:
case T6022:
cpu_start_off = CPU_START_OFF_T6020;
break;
case T6030:
cpu_start_off = CPU_START_OFF_T8112;
break;
case T6031:
case T6034:
cpu_start_off = CPU_START_OFF_T6031;
break;
default:
printf("CPU start offset is unknown for this SoC!\n");
return;
}
ADT_FOREACH_CHILD(adt, node)
{
u32 cpu_id;
if (ADT_GETPROP(adt, node, "cpu-id", &cpu_id) < 0)
if (ADT_GETPROP(adt, node, "reg", &cpu_id) < 0)
continue;
if (cpu_id >= MAX_CPUS) {
printf("cpu-id %d exceeds max CPU count %d: increase MAX_CPUS\n", cpu_id, MAX_CPUS);
continue;
}
cpu_nodes[cpu_id] = node;
}
/* The boot cpu id never changes once set */
if (boot_cpu_idx == -1) {
/* Figure out which CPU we are on by seeing which CPU is running */
/* This seems silly but it's what XNU does */
for (int i = 0; i < MAX_CPUS; i++) {
int cpu_node = cpu_nodes[i];
if (!cpu_node)
continue;
const char *state = adt_getprop(adt, cpu_node, "state", NULL);
if (!state)
continue;
if (strcmp(state, "running") == 0) {
boot_cpu_idx = i;
boot_cpu_mpidr = mrs(MPIDR_EL1);
if (in_el2())
msr(TPIDR_EL2, boot_cpu_idx);
else
msr(TPIDR_EL1, boot_cpu_idx);
break;
}
}
}
if (boot_cpu_idx == -1) {
printf(
"Could not find currently running CPU in cpu table, can't start other processors!\n");
return;
}
spin_table[boot_cpu_idx].mpidr = mrs(MPIDR_EL1) & 0xFFFFFF;
for (int i = 0; i < MAX_CPUS; i++) {
int cpu_node = cpu_nodes[i];
if (!cpu_node)
continue;
u32 reg;
u64 cpu_impl_reg[2];
if (ADT_GETPROP(adt, cpu_node, "reg", ®) < 0)
continue;
if (ADT_GETPROP_ARRAY(adt, cpu_node, "cpu-impl-reg", cpu_impl_reg) < 0) {
u32 reg_len;
const u64 *regs = adt_getprop(adt, arm_io_node, "reg", ®_len);
if (!regs)
continue;
u32 index = 2 * i + 2;
if (reg_len < index)
continue;
memcpy(cpu_impl_reg, ®s[index], 16);
}
if (i == boot_cpu_idx) {
// Check if already locked
if (read64(cpu_impl_reg[0]) & 1)
continue;
// Unlocked, write _vectors_start into boot CPU's rvbar
write64(cpu_impl_reg[0], (u64)_vectors_start);
sysop("dmb sy");
continue;
}
u8 core = FIELD_GET(CPU_REG_CORE, reg);
u8 cluster = FIELD_GET(CPU_REG_CLUSTER, reg);
u8 die = FIELD_GET(CPU_REG_DIE, reg);
smp_start_cpu(i, die, cluster, core, cpu_impl_reg[0], pmgr_reg + cpu_start_off);
}
}
void smp_stop_secondaries(bool deep_sleep)
{
printf("Stopping secondary CPUs...\n");
int arm_io_node;
if ((arm_io_node = adt_path_offset(adt, "/arm-io")) < 0) {
printf("Error getting /arm-io node\n");
return;
}
smp_set_wfe_mode(true);
for (int i = 0; i < MAX_CPUS; i++) {
int node = cpu_nodes[i];
if (!node)
continue;
u32 reg;
u64 cpu_impl_reg[2];
if (ADT_GETPROP(adt, node, "reg", ®) < 0)
continue;
if (ADT_GETPROP_ARRAY(adt, node, "cpu-impl-reg", cpu_impl_reg) < 0) {
u32 reg_len;
const u64 *regs = adt_getprop(adt, arm_io_node, "reg", ®_len);
if (!regs)
continue;
u32 index = 2 * i + 2;
if (reg_len < index)
continue;
memcpy(cpu_impl_reg, ®s[index], 16);
}
u8 core = FIELD_GET(CPU_REG_CORE, reg);
u8 cluster = FIELD_GET(CPU_REG_CLUSTER, reg);
u8 die = FIELD_GET(CPU_REG_DIE, reg);
smp_stop_cpu(i, die, cluster, core, cpu_impl_reg[0], pmgr_reg + cpu_start_off, deep_sleep);
}
}
extern void cpu_reset(void) __attribute__((noreturn));
extern void *smp_switch_boot_cpu_entry(int cpu_index);
extern int smp_switch_boot_cpu_exit(void *prev_stack, u64 saved_sp);
static u64 switch_boot_cpu_init_new(int cpu_index, int old_index, u64 saved_sp)
{
int i;
void *prev_stack;
// wait for the previous previous boot CPU to be available as secondary
for (i = 0; i < 100; i++) {
sysop("dmb ld");
if (spin_table[old_index].flag)
break;
udelay(1000);
}
if (i >= 100)
printf("Previous boot CPU %d failed to start as secondary!\n", old_index);
else
printf(" Started.\n");
// restore _reset_stacks from secondary init of previous boot cpu
_reset_stack = dummy_stack + DUMMY_STACK_SIZE;
_reset_stack_el1 = dummy_stack_el1 + DUMMY_STACK_SIZE;
prev_stack = secondary_stacks[cpu_index];
secondary_stacks[cpu_index] = dummy_stack;
// setup current CPU as boot CPU
if (in_el2())
msr(TPIDR_EL2, boot_cpu_idx);
else
msr(TPIDR_EL1, boot_cpu_idx);
// clear spin table of new boot CPU
memset(&spin_table[boot_cpu_idx], 0, sizeof(struct spin_table));
spin_table[boot_cpu_idx].mpidr = mrs(MPIDR_EL1) & 0xFFFFFF;
exception_initialize();
mmu_init();
fb_set_active(true);
smp_switch_boot_cpu_exit(prev_stack, saved_sp);
__builtin_unreachable();
return -1;
}
void smp_do_switch_boot_cpu(int cpu_index, u64 saved_sp)
{
int old_index = boot_cpu_idx;
printf("Switching boot CPU from %d to %d\n", old_index, cpu_index);
// disable frame buffer until the new CPU has called mmu_init()
fb_set_active(false);
smp_call3(cpu_index, switch_boot_cpu_init_new, cpu_index, old_index, saved_sp);
// switch the boot CPU so the old boot CPU resets as secondary
boot_cpu_idx = cpu_index;
boot_cpu_mpidr = spin_table[cpu_index].mpidr;
smp_prepare_cpu(old_index);
cpu_reset();
__builtin_unreachable();
}
int smp_switch_boot_cpu(int cpu_index)
{
if (cpu_index == boot_cpu_idx)
return cpu_index;
if (!smp_is_alive(cpu_index)) {
printf("Trying to switch to offline CPU %d\n", cpu_index);
return -1;
}
// Call asm helper function to capture callee saved state and pass `sp` to the new boot CPU.
// This function will return on the new boot CPU.
void *old_stack = smp_switch_boot_cpu_entry(cpu_index);
free(old_stack);
return cpu_index;
}
void smp_send_ipi(int cpu)
{
if (cpu >= MAX_CPUS)
return;
u64 mpidr = spin_table[cpu].mpidr;
if (cpu_features->fast_ipi) {
msr(SYS_IMP_APL_IPI_RR_GLOBAL_EL1, (mpidr & 0xff) | ((mpidr & 0xff00) << 8));
} else {
aic_write(AIC_IPI_SEND, AIC_IPI_SEND_CPU(cpu));
}
}
void smp_call4(int cpu, void *func, u64 arg0, u64 arg1, u64 arg2, u64 arg3)
{
if (cpu >= MAX_CPUS)
return;
struct spin_table *target = &spin_table[cpu];
if (cpu == boot_cpu_idx)
return;
u64 flag = target->flag;
target->args[0] = arg0;
target->args[1] = arg1;
target->args[2] = arg2;
target->args[3] = arg3;
sysop("dmb sy");
target->target = (u64)func;
sysop("dsb sy");
if (wfe_mode)
sysop("sev");
else
smp_send_ipi(cpu);
while (target->flag == flag)
sysop("dmb sy");
}
u64 smp_wait(int cpu)
{
if (cpu >= MAX_CPUS)
return 0;
struct spin_table *target = &spin_table[cpu];
while (target->target)
sysop("dmb sy");
return target->retval;
}
void smp_set_wfe_mode(bool new_mode)
{
wfe_mode = new_mode;
sysop("dsb sy");
for (int cpu = 0; cpu < MAX_CPUS; cpu++)
if (cpu != boot_cpu_idx && smp_is_alive(cpu))
smp_send_ipi(cpu);
sysop("sev");
}
bool smp_is_alive(int cpu)
{
if (cpu >= MAX_CPUS)
return false;
return spin_table[cpu].flag;
}
uint64_t smp_get_mpidr(int cpu)
{
if (cpu >= MAX_CPUS)
return 0;
return spin_table[cpu].mpidr;
}
u64 smp_get_release_addr(int cpu)
{
struct spin_table *target = &spin_table[cpu];
if (cpu >= MAX_CPUS)
return 0;
target->args[0] = 0;
target->args[1] = 0;
target->args[2] = 0;
target->args[3] = 0;
return (u64)&target->target;
}