blob: 7af6a629ab65c2450661791f7314deea11716e68 [file] [log] [blame]
Andrew Scullf0551c82018-12-15 20:38:47 +00001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scullf0551c82018-12-15 20:38:47 +00003 *
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.
Andrew Scullf0551c82018-12-15 20:38:47 +00007 */
8
9#include <stdalign.h>
10#include <stdint.h>
11
Fuad Tabba50469e02020-06-30 15:14:28 +010012#include "hf/fdt_handler.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010013#include "hf/ffa.h"
Andrew Scullf0551c82018-12-15 20:38:47 +000014#include "hf/memiter.h"
David Brazdil711fbe92019-08-06 13:39:58 +010015#include "hf/mm.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010016#include "hf/std.h"
Andrew Scullf0551c82018-12-15 20:38:47 +000017
18#include "vmapi/hf/call.h"
19
Andrew Walbran1e7c7742019-12-13 17:10:02 +000020#include "test/hftest.h"
Andrew Scullf0551c82018-12-15 20:38:47 +000021
22alignas(4096) uint8_t kstack[4096];
23
24HFTEST_ENABLE();
25
26extern struct hftest_test hftest_begin[];
27extern struct hftest_test hftest_end[];
28
29static alignas(HF_MAILBOX_SIZE) uint8_t send[HF_MAILBOX_SIZE];
30static alignas(HF_MAILBOX_SIZE) uint8_t recv[HF_MAILBOX_SIZE];
31
32static hf_ipaddr_t send_addr = (hf_ipaddr_t)send;
33static hf_ipaddr_t recv_addr = (hf_ipaddr_t)recv;
34
35static struct hftest_context global_context;
36
37struct hftest_context *hftest_get_context(void)
38{
39 return &global_context;
40}
41
42/** Find the service with the name passed in the arguments. */
43static hftest_test_fn find_service(struct memiter *args)
44{
45 struct memiter service_name;
46 struct hftest_test *test;
47
48 if (!memiter_parse_str(args, &service_name)) {
49 return NULL;
50 }
51
52 for (test = hftest_begin; test < hftest_end; ++test) {
53 if (test->kind == HFTEST_KIND_SERVICE &&
54 memiter_iseq(&service_name, test->name)) {
55 return test->fn;
56 }
57 }
58
59 return NULL;
60}
61
Andrew Sculla59f9bc2019-04-03 15:24:35 +010062noreturn void abort(void)
Andrew Scullf0551c82018-12-15 20:38:47 +000063{
64 HFTEST_LOG("Service contained failures.");
Andrew Walbran2432e672019-04-17 15:23:38 +010065 /* Cause a fault, as a secondary can't power down the machine. */
66 *((volatile uint8_t *)1) = 1;
67
68 /* This should never be reached, but to make the compiler happy... */
Andrew Scullf0551c82018-12-15 20:38:47 +000069 for (;;) {
Andrew Scullf0551c82018-12-15 20:38:47 +000070 }
71}
72
Fuad Tabba50469e02020-06-30 15:14:28 +010073noreturn void kmain(const void *fdt_ptr)
Andrew Scullf0551c82018-12-15 20:38:47 +000074{
75 struct memiter args;
76 hftest_test_fn service;
Andrew Scullf0551c82018-12-15 20:38:47 +000077 struct hftest_context *ctx;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010078 struct ffa_value ret;
Fuad Tabba50469e02020-06-30 15:14:28 +010079 struct fdt fdt;
Andrew Scullf0551c82018-12-15 20:38:47 +000080
David Brazdil711fbe92019-08-06 13:39:58 +010081 /*
82 * Initialize the stage-1 MMU and identity-map the entire address space.
83 */
84 if (!hftest_mm_init()) {
85 HFTEST_LOG_FAILURE();
86 HFTEST_LOG(HFTEST_LOG_INDENT "Memory initialization failed");
Fuad Tabba50469e02020-06-30 15:14:28 +010087 abort();
David Brazdil711fbe92019-08-06 13:39:58 +010088 }
89
Andrew Scullf0551c82018-12-15 20:38:47 +000090 /* Prepare the context. */
91
92 /* Set up the mailbox. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010093 ffa_rxtx_map(send_addr, recv_addr);
Andrew Scullf0551c82018-12-15 20:38:47 +000094
95 /* Receive the name of the service to run. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010096 ret = ffa_msg_wait();
97 ASSERT_EQ(ret.func, FFA_MSG_SEND_32);
98 memiter_init(&args, recv, ffa_msg_send_size(ret));
Andrew Scullf0551c82018-12-15 20:38:47 +000099 service = find_service(&args);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100100 EXPECT_EQ(ffa_rx_release().func, FFA_SUCCESS_32);
Andrew Scullf0551c82018-12-15 20:38:47 +0000101
102 /* Check the service was found. */
103 if (service == NULL) {
104 HFTEST_LOG_FAILURE();
105 HFTEST_LOG(HFTEST_LOG_INDENT
106 "Unable to find requested service");
Fuad Tabba50469e02020-06-30 15:14:28 +0100107 abort();
108 }
109
110 if (!fdt_struct_from_ptr(fdt_ptr, &fdt)) {
111 HFTEST_LOG(HFTEST_LOG_INDENT "Unable to access the FDT");
112 abort();
Andrew Scullf0551c82018-12-15 20:38:47 +0000113 }
114
115 /* Clean the context. */
116 ctx = hftest_get_context();
Andrew Scull2b5fbad2019-04-05 13:55:56 +0100117 memset_s(ctx, sizeof(*ctx), 0, sizeof(*ctx));
Andrew Scullf0551c82018-12-15 20:38:47 +0000118 ctx->abort = abort;
Andrew Walbran70bc8622019-10-07 14:15:58 +0100119 ctx->send = send;
120 ctx->recv = recv;
Fuad Tabba50469e02020-06-30 15:14:28 +0100121 if (!fdt_get_memory_size(&fdt, &ctx->memory_size)) {
122 HFTEST_LOG_FAILURE();
123 HFTEST_LOG(HFTEST_LOG_INDENT
124 "No entry in the FDT on memory size details");
125 abort();
126 }
Andrew Scullf0551c82018-12-15 20:38:47 +0000127
128 /* Pause so the next time cycles are given the service will be run. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100129 ffa_yield();
Andrew Scullf0551c82018-12-15 20:38:47 +0000130
131 /* Let the service run. */
132 service();
133
134 /* Cleanly handle it if the service returns. */
135 if (ctx->failures) {
136 abort();
137 }
138
139 for (;;) {
140 /* Hang if the service returns. */
141 }
142}