blob: c5665e17ccf0107fa6686956fef7f7c5a46eb280 [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;
109 EXPECT_TRUE(fdt_read_number(&ffa_node, "base-address",
110 &number));
111 cur_region->base_address = number;
112 EXPECT_TRUE(fdt_read_number(&ffa_node, "attributes",
113 &number));
114 cur_region->attributes = (uint32_t)number;
115 mem_count++;
116 } while (fdt_next_sibling(&ffa_node));
117
118 assert(mem_count < PARTITION_MAX_MEMORY_REGIONS);
119
120 ctx->partition_manifest.mem_region_count = mem_count;
121 }
122
123 ctx->is_ffa_manifest_parsed = true;
124}
125
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800126noreturn void hftest_service_main(const void *fdt_ptr)
127{
128 struct memiter args;
129 hftest_test_fn service;
130 struct hftest_context *ctx;
131 struct ffa_value ret;
132 struct fdt fdt;
J-Alves4dff3e92022-05-23 11:33:52 +0100133 ffa_vm_id_t own_id = hf_vm_get_id();
134 struct mailbox_buffers mb = set_up_mailbox();
135 ffa_notifications_bitmap_t bitmap;
136 struct ffa_partition_msg *message = (struct ffa_partition_msg *)mb.recv;
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800137
138 /* Receive the name of the service to run. */
J-Alves8dacd982023-02-08 17:42:25 +0000139 ret = ffa_msg_wait();
140 EXPECT_EQ(ret.func, FFA_RUN_32);
J-Alves4dff3e92022-05-23 11:33:52 +0100141
142 /*
143 * Expect to wake up with indirect message related to the next service
144 * to be executed.
145 */
146 ret = ffa_notification_get(own_id, 0,
147 FFA_NOTIFICATION_FLAG_BITMAP_SPM |
148 FFA_NOTIFICATION_FLAG_BITMAP_HYP);
149 ASSERT_EQ(ret.func, FFA_SUCCESS_32);
150 bitmap = ffa_notification_get_from_framework(ret);
151 ASSERT_TRUE(is_ffa_spm_buffer_full_notification(bitmap) ||
152 is_ffa_hyp_buffer_full_notification(bitmap));
153 ASSERT_EQ(own_id, ffa_rxtx_header_receiver(&message->header));
154 memiter_init(&args, message->payload, message->header.size);
155
156 /* Find service handler. */
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800157 service = find_service(&args);
158 EXPECT_EQ(ffa_rx_release().func, FFA_SUCCESS_32);
159
160 /* Check the service was found. */
161 if (service == NULL) {
162 HFTEST_LOG_FAILURE();
163 HFTEST_LOG(HFTEST_LOG_INDENT
164 "Unable to find requested service");
165 abort();
166 }
167
168 if (!fdt_struct_from_ptr(fdt_ptr, &fdt)) {
169 HFTEST_LOG(HFTEST_LOG_INDENT "Unable to access the FDT");
170 abort();
171 }
172
173 /* Clean the context. */
174 ctx = hftest_get_context();
J-Alvese2354862022-12-08 17:35:51 +0000175 hftest_context_init(ctx, mb.send, mb.recv);
J-Alves4dff3e92022-05-23 11:33:52 +0100176
177 /*
178 * The memory size argument is to be used only by VMs. It is part of
179 * the dt provided by the Hypervisor. SPs expect to receive their
180 * FF-A manifest which doesn't have a memory size field.
181 */
J-Alves3c282a32023-02-28 18:27:31 +0000182 if (IS_VM_ID(own_id) && !fdt_get_memory_size(&fdt, &ctx->memory_size)) {
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800183 HFTEST_LOG_FAILURE();
184 HFTEST_LOG(HFTEST_LOG_INDENT
185 "No entry in the FDT on memory size details");
186 abort();
J-Alves3c282a32023-02-28 18:27:31 +0000187 } else if (!IS_VM_ID(own_id)) {
188 /*
189 * It is secure partition. We are currently using the partition
190 * manifest for the SP.
191 */
192 hftest_parse_ffa_manifest(ctx, &fdt);
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800193 }
194
195 /* Pause so the next time cycles are given the service will be run. */
196 ffa_yield();
197
198 /* Let the service run. */
199 service();
200
201 /* Cleanly handle it if the service returns. */
202 if (ctx->failures) {
203 abort();
204 }
205
206 for (;;) {
207 /* Hang if the service returns. */
208 }
209}
Madhukar Pappireddy29235ec2022-12-22 10:18:41 -0600210
Kathleen Capellacf313482023-02-02 17:29:44 -0500211ffa_vm_id_t hftest_get_dir_req_source_id(void)
Madhukar Pappireddy29235ec2022-12-22 10:18:41 -0600212{
213 struct hftest_context *ctx = hftest_get_context();
214 return ctx->dir_req_source_id;
215}
216
217void hftest_set_dir_req_source_id(ffa_vm_id_t id)
218{
219 struct hftest_context *ctx = hftest_get_context();
220 ctx->dir_req_source_id = id;
221}