Skip to content

Commit 353c56a

Browse files
committed
Multicore for raspi4
All cores awake and answering. Closes #1
1 parent 8b10347 commit 353c56a

File tree

5 files changed

+48
-70
lines changed

5 files changed

+48
-70
lines changed

src/bootstrapper/boot.S

+29-25
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,50 @@
11
/*
22
* Bootstrapper. For details see: https://jsandler18.github.io/explanations/boot_S.html
3+
* For multi core on raspi4 see: https://github.com/szediwy/raspberry-pi-os/commit/f601eb8b88a97aaaa92be90f7c01272541367fac
34
*/
45
#include "mm.h"
5-
#include "smp.h"
66

77
.section ".text.boot"
88

99
.global _start
1010
_start:
1111
mrs x0, mpidr_el1
1212
and x0, x0, #0xFF // Check core id
13-
mov x19, x0 // Save core id
14-
cbz x0, start_primary_core
15-
b start_secondary_cores
16-
17-
start_secondary_cores:
18-
mrs x19, mpidr_el1
19-
and x19, x19, #0xFF // Check core id
20-
mov x0, STACK_SIZE
21-
mul x2, x0, x19 // Calculate SP for each core
22-
mov sp, x2 // Setup SP
23-
mov x1, #30000
24-
mul x0, x1, x19 // Calc delay for each core
25-
bl delay
26-
mov x0, x19
27-
bl kernel_secondary // To the kernel secondary core entry
13+
cbz x0, init_bss
14+
wfe // If not core 0 wait for sev instruction
15+
b master
2816

2917
proc_hang:
3018
b proc_hang
3119

32-
start_primary_core:
20+
init_bss:
3321
adr x0, __bss_start // Init bss
3422
adr x1, __bss_end
3523
sub x1, x1, x0
3624
bl memzero
3725

3826
// Setup and wake cores
39-
mov sp, #LOW_MEMORY
40-
bl kernel_main // To the kernel primary core entry
41-
mov x0, #2 // Tell core 2 ...
42-
adr x1, start_secondary_cores // ... where to start
43-
bl wakeup_core
44-
mov x0, #3
45-
bl wakeup_core
46-
b proc_hang // should never come here
27+
sev
28+
29+
// Enable the other cores see: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/arm64/booting.rst?h=v5.3#n255
30+
mov x0, #0
31+
adr x0, master
32+
33+
mov x1, #0xE0
34+
str x0, [x1]
35+
mov x1, #0xE8
36+
str x0, [x1]
37+
mov x1, #0xF0
38+
str x0, [x1]
39+
40+
master:
41+
mrs x0, mpidr_el1
42+
and x0, x0, #0xFF // Get core id
43+
44+
mov x1, #SECTION_SIZE
45+
mul x1, x1, x0 // Calculate SP for core
46+
add x1, x1, #LOW_MEMORY
47+
mov sp, x1 // Setup SP
48+
49+
bl kernel_main
50+
b proc_hang

src/bootstrapper/mm.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef _MM_H
22
#define _MM_H
33

4-
#define PAGE_SHIFT 12
4+
#define PAGE_SHIFT 12
55
#define TABLE_SHIFT 9
66
#define SECTION_SHIFT (PAGE_SHIFT + TABLE_SHIFT)
77

@@ -10,8 +10,6 @@
1010

1111
#define LOW_MEMORY (2 * SECTION_SIZE)
1212

13-
#define STACK_SIZE (1 << 20)
14-
1513
#ifndef __ASSEMBLER__
1614

1715
void memzero(unsigned long src, unsigned long n);

src/bootstrapper/smp.S

-6
This file was deleted.

src/bootstrapper/smp.h

-10
This file was deleted.

src/kernel/kernel.c

+18-26
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,30 @@
1-
#include <stdint.h>
2-
#include "../common/stdio.h"
3-
#include "../common/utils.h"
41
#include "../peripherals/uart.h"
52

6-
volatile unsigned int semaphore = 0;
3+
static unsigned int current_processor_index = 0;
74

8-
void kernel_do(int id)
5+
void kernel_main(unsigned long processor_index)
96
{
10-
// Wait for previous CPU to finish printing
11-
while(id != semaphore) { }
127

13-
puts("Hello, from processor ");
14-
putc(id + '0');
15-
puts("\r\n");
8+
if (processor_index == 0) {
9+
uart_init();
10+
}
11+
12+
while (processor_index != current_processor_index) { }
13+
14+
uart_send_string("Hello from processor ");
15+
uart_send(processor_index + '0');
16+
uart_send_string("!\r\n");
1617

17-
// Tells the next CPU to go
18-
semaphore++;
18+
// Tell the next core to go
19+
current_processor_index++;
1920

20-
// Only CPU #0 do the echo
21-
if (id == 0) {
22-
// Wait for everyone else to finish
23-
while(semaphore != 4) { }
21+
// Only core 0 do the echo
22+
if (processor_index == 0) {
23+
// if current_processor_index == 4 then all processors are done
24+
while (current_processor_index != 4) { }
2425

2526
while (1) {
26-
putc(getc());
27+
uart_send(uart_recv());
2728
}
2829
}
29-
}
30-
31-
void kernel_main(void) {
32-
uart_init();
33-
kernel_do(0);
34-
}
35-
36-
void kernel_secondary(int core_id) {
37-
kernel_do(core_id);
3830
}

0 commit comments

Comments
 (0)