blob: 90350da43456feb88786f603907d443b7f380bee [file] [log] [blame]
Julian Hallead5b622021-11-23 17:31:07 +01001// SPDX-License-Identifier: BSD-3-Clause
2/*
Imre Kisd0ed5c22021-12-15 17:05:47 +01003 * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
Julian Hallead5b622021-11-23 17:31:07 +01004 */
5
Imre Kis85226922022-07-05 16:44:11 +02006#include "rpc/ffarpc/endpoint/ffarpc_call_ep.h"
Imre Kisd0ed5c22021-12-15 17:05:47 +01007#include "smm_gateway.h"
Imre Kis85226922022-07-05 16:44:11 +02008#include "config/ramstore/config_ramstore.h"
Imre Kisee70c1a2021-10-27 20:41:01 +02009#include "config/interface/config_store.h"
Imre Kis85226922022-07-05 16:44:11 +020010#include "config/loader/sp/sp_config_loader.h"
Imre Kisee70c1a2021-10-27 20:41:01 +020011#include "components/rpc/mm_communicate/endpoint/sp/mm_communicate_call_ep.h"
12#include "components/service/smm_variable/frontend/mm_communicate/smm_variable_mm_service.h"
13#include "platform/interface/memory_region.h"
Imre Kise700cb12022-07-20 15:19:17 +020014#include "protocols/common/mm/mm_smc.h"
Imre Kis85226922022-07-05 16:44:11 +020015#include "ffa_api.h"
16#include "sp_api.h"
17#include "sp_discovery.h"
18#include "sp_messaging.h"
19#include "sp_rxtx.h"
20#include "trace.h"
Julian Hallead5b622021-11-23 17:31:07 +010021
Imre Kisee70c1a2021-10-27 20:41:01 +020022#define CONFIG_NAME_MM_COMM_BUFFER_REGION "mm-comm-buffer"
Julian Hallead5b622021-11-23 17:31:07 +010023
Imre Kis85226922022-07-05 16:44:11 +020024static bool sp_init(uint16_t *own_sp_id);
Julian Hallead5b622021-11-23 17:31:07 +010025
26void __noreturn sp_main(struct ffa_init_info *init_info)
27{
Imre Kisee70c1a2021-10-27 20:41:01 +020028 struct memory_region mm_comm_buffer_region = { 0 };
29 struct rpc_interface *gateway_iface = NULL;
30 struct smm_variable_mm_service smm_var_service = { 0 };
31 struct mm_service_interface *smm_var_service_interface = NULL;
32 struct mm_communicate_ep mm_communicate_call_ep = { 0 };
33 struct ffa_direct_msg req_msg = { 0 };
34 struct ffa_direct_msg resp_msg = { 0 };
Imre Kisf6562652022-07-04 15:33:13 +020035 uint16_t own_id = 0;
Imre Kis85226922022-07-05 16:44:11 +020036 ffa_result result = FFA_DENIED;
Imre Kisee70c1a2021-10-27 20:41:01 +020037
38 static const EFI_GUID smm_variable_guid = SMM_VARIABLE_GUID;
Julian Hallead5b622021-11-23 17:31:07 +010039
40 /* Boot phase */
Imre Kis85226922022-07-05 16:44:11 +020041 if (!sp_init(&own_id)) {
42 EMSG("Failed to init SP");
43 goto fatal_error;
44 }
Julian Hallead5b622021-11-23 17:31:07 +010045
46 /* Load any dynamic configuration */
47 config_ramstore_init();
Imre Kis85226922022-07-05 16:44:11 +020048
49 if (!sp_config_load(init_info)) {
50 EMSG("Failed to load SP config");
51 goto fatal_error;
52 }
Julian Hallead5b622021-11-23 17:31:07 +010053
Imre Kisee70c1a2021-10-27 20:41:01 +020054 if (!config_store_query(CONFIG_CLASSIFIER_MEMORY_REGION, CONFIG_NAME_MM_COMM_BUFFER_REGION,
55 0, &mm_comm_buffer_region, sizeof(mm_comm_buffer_region))) {
56 EMSG(CONFIG_NAME_MM_COMM_BUFFER_REGION " is not set in SP configuration");
57 goto fatal_error;
58 }
59
Julian Hallead5b622021-11-23 17:31:07 +010060 /* Initialize service layer and associate with RPC endpoint */
Imre Kisee70c1a2021-10-27 20:41:01 +020061 gateway_iface = smm_gateway_create(own_id);
Imre Kis85226922022-07-05 16:44:11 +020062 if (!gateway_iface) {
63 EMSG("Failed to create SMM gateway");
64 goto fatal_error;
65 }
Imre Kisee70c1a2021-10-27 20:41:01 +020066
67 /* Initialize SMM variable MM service */
68 smm_var_service_interface = smm_variable_mm_service_init(&smm_var_service, gateway_iface);
Imre Kis85226922022-07-05 16:44:11 +020069 if (!smm_var_service_interface) {
70 EMSG("Failed to init SMM variable MM service");
71 goto fatal_error;
72 }
Imre Kisee70c1a2021-10-27 20:41:01 +020073
74 /* Initialize MM communication layer */
75 if (!mm_communicate_call_ep_init(&mm_communicate_call_ep,
76 (void *)mm_comm_buffer_region.base_addr,
Imre Kis85226922022-07-05 16:44:11 +020077 mm_comm_buffer_region.region_size)) {
78 EMSG("Failed to init MM communicate call EP");
Imre Kisee70c1a2021-10-27 20:41:01 +020079 goto fatal_error;
Imre Kis85226922022-07-05 16:44:11 +020080 }
Imre Kisee70c1a2021-10-27 20:41:01 +020081
82 /* Attach SMM variable service to MM communication layer */
83 mm_communicate_call_ep_attach_service(&mm_communicate_call_ep, &smm_variable_guid,
84 smm_var_service_interface);
Julian Hallead5b622021-11-23 17:31:07 +010085
86 /* End of boot phase */
Imre Kis85226922022-07-05 16:44:11 +020087 result = ffa_msg_wait(&req_msg);
88 if (result != FFA_OK) {
89 EMSG("Failed to send message wait %d", result);
90 goto fatal_error;
91 }
Julian Hallead5b622021-11-23 17:31:07 +010092
93 while (1) {
Imre Kise700cb12022-07-20 15:19:17 +020094 if (FFA_IS_32_BIT_FUNC(req_msg.function_id)) {
95 EMSG("MM communicate over 32 bit FF-A messages is not supported");
96 ffa_msg_send_direct_resp_32(req_msg.destination_id, req_msg.source_id,
97 MM_RETURN_CODE_NOT_SUPPORTED, 0, 0, 0, 0,
98 &req_msg);
99 continue;
100 }
101
Imre Kisee70c1a2021-10-27 20:41:01 +0200102 mm_communicate_call_ep_receive(&mm_communicate_call_ep, &req_msg, &resp_msg);
Julian Hallead5b622021-11-23 17:31:07 +0100103
Imre Kis85226922022-07-05 16:44:11 +0200104 result = ffa_msg_send_direct_resp_64(req_msg.destination_id,
105 req_msg.source_id, resp_msg.args.args64[0],
106 resp_msg.args.args64[1], resp_msg.args.args64[2],
107 resp_msg.args.args64[3], resp_msg.args.args64[4],
108 &req_msg);
109 if (result != FFA_OK) {
110 EMSG("Failed to send direct response %d", result);
111 result = ffa_msg_wait(&req_msg);
112 if (result != FFA_OK) {
113 EMSG("Failed to send message wait %d", result);
114 goto fatal_error;
115 }
116 }
Julian Hallead5b622021-11-23 17:31:07 +0100117 }
118
119fatal_error:
120 /* SP is not viable */
121 EMSG("SMM gateway SP error");
122 while (1) {}
123}
124
125void sp_interrupt_handler(uint32_t interrupt_id)
126{
127 (void)interrupt_id;
128}
129
Imre Kis85226922022-07-05 16:44:11 +0200130static bool sp_init(uint16_t *own_id)
Julian Hallead5b622021-11-23 17:31:07 +0100131{
Imre Kis85226922022-07-05 16:44:11 +0200132 sp_result sp_res = SP_RESULT_INTERNAL_ERROR;
Julian Hallead5b622021-11-23 17:31:07 +0100133 static uint8_t tx_buffer[4096] __aligned(4096);
134 static uint8_t rx_buffer[4096] __aligned(4096);
135
136 sp_res = sp_rxtx_buffer_map(tx_buffer, rx_buffer, sizeof(rx_buffer));
Imre Kis85226922022-07-05 16:44:11 +0200137 if (sp_res != SP_RESULT_OK) {
138 EMSG("Failed to map RXTX buffers: %d", sp_res);
139 return false;
Julian Hallead5b622021-11-23 17:31:07 +0100140 }
141
Imre Kis85226922022-07-05 16:44:11 +0200142 sp_res = sp_discovery_own_id_get(own_id);
143 if (sp_res != SP_RESULT_OK) {
144 EMSG("Failed to query own ID: %d", sp_res);
145 return false;
146 }
147
148 return true;
Julian Hallead5b622021-11-23 17:31:07 +0100149}