blob: a429beb6a48744a98613472e0484a3a7097439f4 [file] [log] [blame]
J-Alves35e61922021-05-06 10:01:05 +01001/*
2 * Copyright 2021 The Hafnium Authors.
3 *
4 * 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.
7 */
8
J-Alves3c282a32023-02-28 18:27:31 +00009#include "hf/check.h"
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -080010#include "hf/fdt_handler.h"
11#include "hf/ffa.h"
12#include "hf/memiter.h"
J-Alves35e61922021-05-06 10:01:05 +010013#include "hf/mm.h"
14#include "hf/std.h"
15
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -080016#include "vmapi/hf/call.h"
17
J-Alves35e61922021-05-06 10:01:05 +010018#include "test/hftest.h"
19#include "test/hftest_impl.h"
J-Alves4dff3e92022-05-23 11:33:52 +010020#include "test/vmapi/ffa.h"
J-Alves35e61922021-05-06 10:01:05 +010021
22HFTEST_ENABLE();
23
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -080024extern struct hftest_test hftest_begin[];
25extern struct hftest_test hftest_end[];
26
J-Alves35e61922021-05-06 10:01:05 +010027static struct hftest_context global_context;
28
29struct hftest_context *hftest_get_context(void)
30{
31 return &global_context;
32}
33
34noreturn void abort(void)
35{
36 HFTEST_LOG("Service contained failures.");
37 /* Cause a fault, as a secondary/SP can't power down the machine. */
38 *((volatile uint8_t *)1) = 1;
39
40 /* This should never be reached, but to make the compiler happy... */
41 for (;;) {
42 }
43}
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -080044
45/** Find the service with the name passed in the arguments. */
46static hftest_test_fn find_service(struct memiter *args)
47{
48 struct memiter service_name;
49 struct hftest_test *test;
50
51 if (!memiter_parse_str(args, &service_name)) {
52 return NULL;
53 }
54
55 for (test = hftest_begin; test < hftest_end; ++test) {
56 if (test->kind == HFTEST_KIND_SERVICE &&
57 memiter_iseq(&service_name, test->name)) {
58 return test->fn;
59 }
60 }
61
62 return NULL;
63}
64
J-Alvese2354862022-12-08 17:35:51 +000065void hftest_context_init(struct hftest_context *ctx, void *send, void *recv)
66{
67 memset_s(ctx, sizeof(*ctx), 0, sizeof(*ctx));
68 ctx->abort = abort;
69 ctx->send = send;
70 ctx->recv = recv;
71}
72
J-Alves3c282a32023-02-28 18:27:31 +000073/*
74 * Parse the FF-A partition's manifest.
75 * This function assumes the 'fdt' field of the passed 'ctx' has been
76 * initialized.
77 * TODO: Parse other fields as needed.
78 */
79static void hftest_parse_ffa_manifest(struct hftest_context *ctx,
80 struct fdt *fdt)
81{
82 struct fdt_node root;
83 struct fdt_node ffa_node;
84 struct string mem_region_node_name = STRING_INIT("memory-regions");
85 uint64_t number;
86
87 CHECK(ctx != NULL);
88 CHECK(fdt != NULL);
89
90 ASSERT_TRUE(fdt_find_node(fdt, "/", &root));
91 EXPECT_TRUE(fdt_is_compatible(&root, "arm,ffa-manifest-1.0"));
92 ASSERT_TRUE(fdt_read_number(&root, "load-address",
93 &ctx->partition_manifest.load_addr));
94 EXPECT_TRUE(fdt_read_number(&root, "ffa-version", &number));
95
96 ffa_node = root;
97
98 /* Look for the memory region node. */
99 if (fdt_find_child(&ffa_node, &mem_region_node_name) &&
100 fdt_first_child(&ffa_node)) {
101 uint32_t mem_count = 0;
102
103 do {
104 struct memory_region *cur_region =
105 &ctx->partition_manifest.mem_regions[mem_count];
106 EXPECT_TRUE(fdt_read_number(&ffa_node, "pages-count",
107 &number));
108 cur_region->page_count = (uint32_t)number;
Karl Meakinc2360152023-06-01 14:17:32 +0100109
110 if (!fdt_read_number(&ffa_node, "base-address",
111 &cur_region->base_address)) {
112 EXPECT_TRUE(fdt_read_number(&ffa_node,
113 "relative-address",
114 &number));
115 cur_region->base_address =
116 ctx->partition_manifest.load_addr +
117 number;
118 }
119
J-Alves3c282a32023-02-28 18:27:31 +0000120 EXPECT_TRUE(fdt_read_number(&ffa_node, "attributes",
121 &number));
122 cur_region->attributes = (uint32_t)number;
123 mem_count++;
124 } while (fdt_next_sibling(&ffa_node));
125
126 assert(mem_count < PARTITION_MAX_MEMORY_REGIONS);
127
128 ctx->partition_manifest.mem_region_count = mem_count;
129 }
130
131 ctx->is_ffa_manifest_parsed = true;
132}
133
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800134noreturn void hftest_service_main(const void *fdt_ptr)
135{
136 struct memiter args;
137 hftest_test_fn service;
138 struct hftest_context *ctx;
139 struct ffa_value ret;
140 struct fdt fdt;
J-Alves4dff3e92022-05-23 11:33:52 +0100141 ffa_vm_id_t own_id = hf_vm_get_id();
142 struct mailbox_buffers mb = set_up_mailbox();
143 ffa_notifications_bitmap_t bitmap;
144 struct ffa_partition_msg *message = (struct ffa_partition_msg *)mb.recv;
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800145
146 /* Receive the name of the service to run. */
J-Alves8dacd982023-02-08 17:42:25 +0000147 ret = ffa_msg_wait();
148 EXPECT_EQ(ret.func, FFA_RUN_32);
J-Alves4dff3e92022-05-23 11:33:52 +0100149
150 /*
151 * Expect to wake up with indirect message related to the next service
152 * to be executed.
153 */
154 ret = ffa_notification_get(own_id, 0,
155 FFA_NOTIFICATION_FLAG_BITMAP_SPM |
156 FFA_NOTIFICATION_FLAG_BITMAP_HYP);
157 ASSERT_EQ(ret.func, FFA_SUCCESS_32);
158 bitmap = ffa_notification_get_from_framework(ret);
159 ASSERT_TRUE(is_ffa_spm_buffer_full_notification(bitmap) ||
160 is_ffa_hyp_buffer_full_notification(bitmap));
161 ASSERT_EQ(own_id, ffa_rxtx_header_receiver(&message->header));
162 memiter_init(&args, message->payload, message->header.size);
163
164 /* Find service handler. */
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800165 service = find_service(&args);
166 EXPECT_EQ(ffa_rx_release().func, FFA_SUCCESS_32);
167
168 /* Check the service was found. */
169 if (service == NULL) {
170 HFTEST_LOG_FAILURE();
171 HFTEST_LOG(HFTEST_LOG_INDENT
172 "Unable to find requested service");
173 abort();
174 }
175
176 if (!fdt_struct_from_ptr(fdt_ptr, &fdt)) {
177 HFTEST_LOG(HFTEST_LOG_INDENT "Unable to access the FDT");
178 abort();
179 }
180
181 /* Clean the context. */
182 ctx = hftest_get_context();
J-Alvese2354862022-12-08 17:35:51 +0000183 hftest_context_init(ctx, mb.send, mb.recv);
J-Alves4dff3e92022-05-23 11:33:52 +0100184
185 /*
186 * The memory size argument is to be used only by VMs. It is part of
187 * the dt provided by the Hypervisor. SPs expect to receive their
188 * FF-A manifest which doesn't have a memory size field.
189 */
J-Alves3c282a32023-02-28 18:27:31 +0000190 if (IS_VM_ID(own_id) && !fdt_get_memory_size(&fdt, &ctx->memory_size)) {
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800191 HFTEST_LOG_FAILURE();
192 HFTEST_LOG(HFTEST_LOG_INDENT
193 "No entry in the FDT on memory size details");
194 abort();
J-Alves3c282a32023-02-28 18:27:31 +0000195 } else if (!IS_VM_ID(own_id)) {
196 /*
197 * It is secure partition. We are currently using the partition
198 * manifest for the SP.
199 */
200 hftest_parse_ffa_manifest(ctx, &fdt);
J-Alvese8976412023-02-13 11:39:08 +0000201
202 /* TODO: Determine memory size referring to the SP Pkg. */
203 ctx->memory_size = 1048576;
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800204 }
205
206 /* Pause so the next time cycles are given the service will be run. */
207 ffa_yield();
208
209 /* Let the service run. */
210 service();
211
212 /* Cleanly handle it if the service returns. */
213 if (ctx->failures) {
214 abort();
215 }
216
217 for (;;) {
218 /* Hang if the service returns. */
219 }
220}
Madhukar Pappireddy29235ec2022-12-22 10:18:41 -0600221
Kathleen Capellacf313482023-02-02 17:29:44 -0500222ffa_vm_id_t hftest_get_dir_req_source_id(void)
Madhukar Pappireddy29235ec2022-12-22 10:18:41 -0600223{
224 struct hftest_context *ctx = hftest_get_context();
225 return ctx->dir_req_source_id;
226}
227
228void hftest_set_dir_req_source_id(ffa_vm_id_t id)
229{
230 struct hftest_context *ctx = hftest_get_context();
231 ctx->dir_req_source_id = id;
232}