Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/boot-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@ runs the OS inside the armed window and emits `/chosen/aurora,ctxr-exec-window`.
With no usable window it does not start the OS. Elsewhere nothing changes.
[The boot object notes](macho-boot-object.md#running-the-os) say what it needs.

## Sleep and CPU power-off for an OS at EL2

There is no option for this. An OS that aura starts at EL2 has no monitor below
it, so it powers cores off and suspends the system itself. From M4 on, the Neo's
A18 Pro included, iBoot locks every core's reset vector to the boot object, so each
core then restarts inside aura. On those chips kboot publishes
`/chosen/aurora,s2r-mailbox`, the physical address of a 32-byte mailbox in aura's
image:

- a secondary comes back through its spin-table release address, which still holds
the OS's;
- the boot core jumps to the resume address the OS left in the mailbox, at EL2 with
the MMU and caches off and interrupts masked, or reboots if the OS left none.

The CPUs keep `spin-table`. The property says they can power off and come back:
Linux with the T8140 sleep series gives them its parking spin table then, and an OS
that doesn't know the property ignores it. Whether to deep-sleep is the OS's choice.
Once the mailbox is published, reset paths don't print, since the OS owns the UART
and the display. The mailbox sits in aura's image, which kboot reserves anyway, so it
takes no memory of its own. On A7 to M3 RVBAR isn't locked, so nothing is published,
and a guest at EL1 gets nothing.

## DebugUSB interception (development builds)

A build made with `DEBUGUSB_PROXY=1` also opens the 15 second `aura-proxy`
Expand Down
6 changes: 4 additions & 2 deletions docs/macho-boot-object.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,10 @@ These worked under the monitor, or were reachable through it, except power-off:
- **CPU hotplug.** Spin-table has no CPU off, and aura no longer parks a core for
the OS. The kernel in aurora-silicon/linux#985 has no `cpu_die` for spin-table, so
taking a CPU offline fails with `EOPNOTSUPP`. Suspend to RAM and hibernation take
the secondaries offline the same way, so they fail there too. A separate kernel
patch that parks the core itself ran once on a Neo, and it isn't in that PR.
the secondaries offline the same way, so they fail there too. aura publishes
`/chosen/aurora,s2r-mailbox` for an OS at EL2 on these chips, though, and a kernel
that reads it powers cores off and suspends itself
([boot options](boot-options.md#sleep-and-cpu-power-off-for-an-os-at-el2)).
- **PSCI `SYSTEM_RESET`.** Restart now needs `macsmc-reboot` or the Apple watchdog.
The monitor answered `SYSTEM_OFF` with a reset as well, so power-off never worked
through it, and now needs `macsmc-reboot`. The J700 tree describes both, but
Expand Down
3 changes: 2 additions & 1 deletion src/chickens.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "chickens.h"
#include "cpu_regs.h"
#include "midr.h"
#include "smp.h"
#include "uart.h"
#include "utils.h"

Expand Down Expand Up @@ -196,7 +197,7 @@ void init_cpu(void)
int part = FIELD_GET(MIDR_PART, midr);
int rev = (FIELD_GET(MIDR_REV_HIGH, midr) << 4) | FIELD_GET(MIDR_REV_LOW, midr);

bool quiet = (chip_id == T8142 || chip_id == T8152) && !is_boot_cpu();
bool quiet = ((chip_id == T8142 || chip_id == T8152) && !is_boot_cpu()) || smp_s2r_published();
if (!quiet)
printf(" CPU part: 0x%x rev: 0x%x\n", part, rev);

Expand Down
21 changes: 21 additions & 0 deletions src/kboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,24 @@ static int dt_set_ctxr_exec_window(int node)
return 0;
}

/*
* An OS at EL2 has no monitor below it, so it powers cores off and suspends the system
* itself, and where iBoot locked RVBAR a reset brings each core back through this image.
* The mailbox tells the OS so: a secondary returns through its spin-table release address,
* the boot core through the mailbox. The CPUs keep spin-table, which every kernel knows.
*/
static int dt_set_s2r_mailbox(int node)
{
if (!in_el2() || !smp_resets_return_here())
return 0;

u64 mailbox = smp_s2r_publish();
if (fdt_setprop_u64(dt, node, "aurora,s2r-mailbox", mailbox))
bail("FDT: couldn't set aurora,s2r-mailbox\n");
printf("FDT: S2R mailbox at 0x%lx\n", mailbox);
return 0;
}

static bool in_exec_window(u64 start, u64 size)
{
u64 window, window_size;
Expand Down Expand Up @@ -777,6 +795,9 @@ static int dt_set_chosen(void)
if (dt_set_ctxr_exec_window(node))
return -1;

if (dt_set_s2r_mailbox(node))
return -1;

if (chip_id == T8140 && dt_set_t8140_wfx_args(node))
return -1;

Expand Down
96 changes: 95 additions & 1 deletion src/smp.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,100 @@ static int smp_prepare_rvbar_t8152(const struct cpu_info *cpu)
return 0;
}

/* Whether a core whose RVBAR iBoot locked at @locked resets into this image. */
static bool smp_rvbar_reaches_here(u64 locked)
{
u64 target = (u64)_vectors_start;
int64_t distance = (int64_t)target - (int64_t)locked;

if (locked == target || locked == installed_relay)
return true;
if (relocated_from && locked == old_vectors_start())
return true;
/* A branch here an earlier stage left, or smp_prepare_rvbar() accepted. */
return !(distance & 3) && distance >= -0x8000000 && distance <= 0x7fffffc &&
read32(locked) == (0x14000000 | ((distance / 4) & 0x03ffffff));
}

/*
* Whether a reset brings the cores back into this image. From M4 on (A18 Pro too), iBoot locks
* every core's RVBAR to the same address, the boot core's too, and smp_prepare_rvbar() checked a
* running secondary's before starting it. On A7 to M3 RVBAR isn't locked: the OS can point it
* anywhere.
*/
bool smp_resets_return_here(void)
{
if (cpu_features->apple_sysregs_unlocked)
return false;
for (int i = 0; i < MAX_CPUS; i++) {
if (i == boot_cpu_idx || !cpu_info[i].valid || !smp_is_alive(i))
continue;
u64 rvbar = read64(cpu_info[i].impl_reg);
return (rvbar & RVBAR_LOCK) && smp_rvbar_reaches_here(rvbar & RVBAR_ADDR);
}
return false;
}

/*
* An OS at EL2 powers cores off and suspends itself, and the locked RVBAR brings every
* core back here. A secondary needs only its spin table entry, which still holds the
* OS's release target. The boot core takes resume_pa. Linux reads this layout in
* drivers/soc/apple/t8140-sleep.c.
*/
#define AURA_S2R_MAGIC 0x3152325341525541ULL /* "AURAS2R1" */

struct aura_s2r_mailbox {
u64 magic; /* aura, before the OS starts */
u64 boot_mpidr; /* aura: the only core that may take resume_pa */
u64 resume_pa; /* OS, cleaned to PoC before it sleeps; aura clears it */
u64 resumes; /* aura: resume_pa taken this many times */
};

static struct aura_s2r_mailbox s2r_mailbox SMP_SHARED ALIGNED(64);

/* Returns the mailbox's physical address: aura's mapping is the identity. */
u64 smp_s2r_publish(void)
{
s2r_mailbox.magic = AURA_S2R_MAGIC;
s2r_mailbox.boot_mpidr = mrs(MPIDR_EL1) & 0xFFFFFF;
s2r_mailbox.resume_pa = 0;
s2r_mailbox.resumes = 0;
dc_cvac_range(&s2r_mailbox, sizeof(s2r_mailbox));
sysop("dsb sy");
return (u64)&s2r_mailbox;
}

/* Once it is, the OS owns the UART and the display: reset paths must not print. */
bool smp_s2r_published(void)
{
return s2r_mailbox.magic == AURA_S2R_MAGIC;
}

/*
* The boot core back from a sleep, with the OS live: nothing here may rerun the boot
* flow. The OS gets EL2h with the MMU and caches off and interrupts masked, as at
* first entry, and init_cpu() has redone the per-core setup. The watchdog is the
* OS's: it armed it, and it stops it.
*/
void smp_s2r_return_to_os(void)
{
u64 entry = s2r_mailbox.resume_pa;

/* exception_initialize() unmasked everything. */
msr(DAIF, 0xf << 6);

/* A reset the OS didn't arm: reboot rather than guess what state it's in. */
if (!entry || (mrs(MPIDR_EL1) & 0xFFFFFF) != s2r_mailbox.boot_mpidr)
reboot();

s2r_mailbox.resume_pa = 0;
s2r_mailbox.resumes++;
sysop("dsb sy");

((void (*)(void))entry)();
reboot();
}

void smp_secondary_entry(void)
{
u64 mpidr = mrs(MPIDR_EL1) & 0xFFFFFF;
Expand Down Expand Up @@ -345,7 +439,7 @@ void smp_secondary_entry(void)
else
msr(TPIDR_EL1, index);

if (chip_id != T8142 && chip_id != T8152)
if (chip_id != T8142 && chip_id != T8152 && !smp_s2r_published())
printf(" Index: %d (table: %p)\n\n", index, me);

me->mpidr = mpidr;
Expand Down
5 changes: 5 additions & 0 deletions src/smp.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ uint64_t smp_get_mpidr(int cpu);
u64 smp_get_release_addr(int cpu);
u64 smp_get_kernel_release_addr(int cpu);
int smp_park_for_kernel(void);
/* Let an OS at EL2 that powers cores off come back through this image: the mailbox, in smp.c. */
bool smp_resets_return_here(void);
u64 smp_s2r_publish(void);
bool smp_s2r_published(void);
void smp_s2r_return_to_os(void) __attribute__((noreturn));
void smp_set_wfe_mode(bool new_mode);
void smp_send_ipi(int cpu);

Expand Down
4 changes: 3 additions & 1 deletion src/startup.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ void _cpu_reset_c(void *stack)
smp_secondary_entry();
return;
}
bool quiet = chip_id == T8142 && !is_boot_cpu();
bool quiet = (chip_id == T8142 && !is_boot_cpu()) || smp_s2r_published();
if (!quiet) {
if (!is_boot_cpu())
uart_puts("RVBAR entry on secondary CPU");
Expand All @@ -281,6 +281,8 @@ void _cpu_reset_c(void *stack)

if (!is_boot_cpu())
smp_secondary_entry();
else if (smp_s2r_published())
smp_s2r_return_to_os();
else
aura_main();
}
Loading