blob: 7b8c0410f37c51301da99c5399eb870489108f1f [file] [log] [blame]
David Brazdil3ad6e542019-09-13 17:17:09 +01001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
David Brazdil3ad6e542019-09-13 17:17:09 +01007 */
8
9#include "hf/arch/vm/power_mgmt.h"
10
11#include "hf/arch/mm.h"
Karl Meakin2b56fc12024-07-02 15:24:27 +010012#include "hf/arch/types.h"
David Brazdil3ad6e542019-09-13 17:17:09 +010013
J-Alves491ff302023-06-21 10:58:05 +010014#include "hf/mm.h"
David Brazdil3ad6e542019-09-13 17:17:09 +010015#include "hf/spinlock.h"
16
Andrew Walbran1e7c7742019-12-13 17:10:02 +000017#include "test/hftest.h"
David Brazdil3ad6e542019-09-13 17:17:09 +010018
19struct cpu_start_state {
Karl Meakinb4ef4322024-05-29 17:25:07 +010020 cpu_entry_point *entry;
David Brazdil3ad6e542019-09-13 17:17:09 +010021 uintreg_t arg;
22 struct spinlock lock;
23};
24
J-Alves491ff302023-06-21 10:58:05 +010025/*
26 * Stack for secondary execution contexts.
27 * Used in tests for MP partitions where multicore functionality is tested.
28 */
29alignas(PAGE_SIZE) uint8_t secondary_ec_stack[MAX_CPUS - 1][PAGE_SIZE];
30
David Brazdil3ad6e542019-09-13 17:17:09 +010031static noreturn void cpu_entry(uintptr_t arg)
32{
Daniel Boulby11d6c812021-10-04 16:54:56 +010033 /*
34 * The function prototype must match the entry function so we permit the
35 * int to pointer conversion.
36 */
37 // NOLINTNEXTLINE(performance-no-int-to-ptr)
David Brazdil3ad6e542019-09-13 17:17:09 +010038 struct cpu_start_state *s = (struct cpu_start_state *)arg;
39 struct cpu_start_state s_copy;
40
41 /*
42 * Initialize memory and enable caching. Must be the first thing we do.
43 */
44 hftest_mm_vcpu_init();
45
46 /* Make a copy of the cpu_start_state struct. */
47 s_copy = *s;
48
49 /* Inform cpu_start() that the state struct memory can now be freed. */
50 sl_unlock(&s->lock);
51
52 /* Call the given entry function with the given argument. */
53 s_copy.entry(s_copy.arg);
54
55 /* If the entry function returns, turn off the CPU. */
56 arch_cpu_stop();
57}
58
Karl Meakin2b56fc12024-07-02 15:24:27 +010059bool hftest_cpu_start(cpu_id_t id, cpu_entry_point *entry, uintptr_t arg)
David Brazdil3ad6e542019-09-13 17:17:09 +010060{
61 struct cpu_start_state s;
62 struct arch_cpu_start_state s_arch;
Kathleen Capella462ad8d2024-03-27 16:28:56 -040063 size_t stack_size = sizeof(secondary_ec_stack[0]);
David Brazdil3ad6e542019-09-13 17:17:09 +010064
65 /*
66 * Config for arch_cpu_start() which will start a new CPU and
67 * immediately jump to cpu_entry(). This function must guarantee that
68 * the state struct is not be freed until cpu_entry() is called.
69 */
Kathleen Capella462ad8d2024-03-27 16:28:56 -040070 s_arch.initial_sp = (uintptr_t)secondary_ec_stack + stack_size;
David Brazdil3ad6e542019-09-13 17:17:09 +010071 s_arch.entry = cpu_entry;
72 s_arch.arg = (uintptr_t)&s;
73
74 /*
Andrew Scullc059fbe2019-09-12 12:58:40 +010075 * Flush the `cpu_start_state` struct because the new CPU will be
David Brazdil3ad6e542019-09-13 17:17:09 +010076 * started without caching enabled and will need the data early on.
Andrew Scullc059fbe2019-09-12 12:58:40 +010077 * Write back is all that is really needed so flushing will definitely
78 * get the job done.
David Brazdil3ad6e542019-09-13 17:17:09 +010079 */
Andrew Scullc059fbe2019-09-12 12:58:40 +010080 arch_mm_flush_dcache(&s_arch, sizeof(s_arch));
David Brazdil3ad6e542019-09-13 17:17:09 +010081
82 if ((s_arch.initial_sp % STACK_ALIGN) != 0) {
83 HFTEST_FAIL(true,
84 "Stack pointer of new vCPU not properly aligned.");
85 }
86
87 /*
88 * Config for cpu_entry(). Its job is to initialize memory and call the
89 * provided entry point with the provided argument.
90 */
91 s.entry = entry;
92 s.arg = arg;
93 sl_init(&s.lock);
94
95 /*
96 * Lock the cpu_start_state struct which will be unlocked once
97 * cpu_entry() does not need its content anymore. This simultaneously
98 * protects the arch_cpu_start_state struct which must not be freed
99 * before cpu_entry() is called.
100 */
101 sl_lock(&s.lock);
102
103 /* Try to start the given CPU. */
104 if (!arch_cpu_start(id, &s_arch)) {
Karl Meakine8937d92024-03-19 16:04:25 +0000105 HFTEST_LOG("Couldn't start cpu %lu", id);
David Brazdil3ad6e542019-09-13 17:17:09 +0100106 return false;
107 }
108
109 /*
110 * Wait until cpu_entry() unlocks the cpu_start_state lock before
111 * freeing stack memory.
112 */
113 sl_lock(&s.lock);
114 return true;
115}