spmd_logical_sp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /*
  2. * Copyright (c) 2023-2024, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #include "spmd_private.h"
  10. #include <common/debug.h>
  11. #include <common/uuid.h>
  12. #include <lib/el3_runtime/context_mgmt.h>
  13. #include <services/el3_spmd_logical_sp.h>
  14. #include <services/spmc_svc.h>
  15. #include <smccc_helpers.h>
  16. /*
  17. * Maximum ffa_partition_info entries that can be returned by an invocation
  18. * of FFA_PARTITION_INFO_GET_REGS_64 is size in bytes, of available
  19. * registers/args in struct ffa_value divided by size of struct
  20. * ffa_partition_info. For this ABI, arg3-arg17 in ffa_value can be used, i.e.
  21. * 15 uint64_t fields. For FF-A v1.1, this value should be 5.
  22. */
  23. #define MAX_INFO_REGS_ENTRIES_PER_CALL \
  24. (uint8_t)((15 * sizeof(uint64_t)) / \
  25. sizeof(struct ffa_partition_info_v1_1))
  26. CASSERT(MAX_INFO_REGS_ENTRIES_PER_CALL == 5, assert_too_many_info_reg_entries);
  27. #if ENABLE_SPMD_LP
  28. static bool is_spmd_lp_inited;
  29. static bool is_spmc_inited;
  30. /*
  31. * Helper function to obtain the array storing the EL3
  32. * SPMD Logical Partition descriptors.
  33. */
  34. static struct spmd_lp_desc *get_spmd_el3_lp_array(void)
  35. {
  36. return (struct spmd_lp_desc *) SPMD_LP_DESCS_START;
  37. }
  38. /*******************************************************************************
  39. * Validate any logical partition descriptors before we initialize.
  40. * Initialization of said partitions will be taken care of during SPMD boot.
  41. ******************************************************************************/
  42. static int el3_spmd_sp_desc_validate(struct spmd_lp_desc *lp_array)
  43. {
  44. /* Check the array bounds are valid. */
  45. assert(SPMD_LP_DESCS_END > SPMD_LP_DESCS_START);
  46. /*
  47. * No support for SPMD logical partitions when SPMC is at EL3.
  48. */
  49. assert(!is_spmc_at_el3());
  50. /* If no SPMD logical partitions are implemented then simply bail out. */
  51. if (SPMD_LP_DESCS_COUNT == 0U) {
  52. return -1;
  53. }
  54. for (uint32_t index = 0U; index < SPMD_LP_DESCS_COUNT; index++) {
  55. struct spmd_lp_desc *lp_desc = &lp_array[index];
  56. /* Validate our logical partition descriptors. */
  57. if (lp_desc == NULL) {
  58. ERROR("Invalid SPMD Logical SP Descriptor\n");
  59. return -EINVAL;
  60. }
  61. /*
  62. * Ensure the ID follows the convention to indicate it resides
  63. * in the secure world.
  64. */
  65. if (!ffa_is_secure_world_id(lp_desc->sp_id)) {
  66. ERROR("Invalid SPMD Logical SP ID (0x%x)\n",
  67. lp_desc->sp_id);
  68. return -EINVAL;
  69. }
  70. /* Ensure SPMD logical partition is in valid range. */
  71. if (!is_spmd_lp_id(lp_desc->sp_id)) {
  72. ERROR("Invalid SPMD Logical Partition ID (0x%x)\n",
  73. lp_desc->sp_id);
  74. return -EINVAL;
  75. }
  76. /* Ensure the UUID is not the NULL UUID. */
  77. if (lp_desc->uuid[0] == 0 && lp_desc->uuid[1] == 0 &&
  78. lp_desc->uuid[2] == 0 && lp_desc->uuid[3] == 0) {
  79. ERROR("Invalid UUID for SPMD Logical SP (0x%x)\n",
  80. lp_desc->sp_id);
  81. return -EINVAL;
  82. }
  83. /* Ensure init function callback is registered. */
  84. if (lp_desc->init == NULL) {
  85. ERROR("Missing init function for Logical SP(0x%x)\n",
  86. lp_desc->sp_id);
  87. return -EINVAL;
  88. }
  89. /* Ensure that SPMD LP only supports sending direct requests. */
  90. if (lp_desc->properties != FFA_PARTITION_DIRECT_REQ_SEND) {
  91. ERROR("Invalid SPMD logical partition properties (0x%x)\n",
  92. lp_desc->properties);
  93. return -EINVAL;
  94. }
  95. /* Ensure that all partition IDs are unique. */
  96. for (uint32_t inner_idx = index + 1;
  97. inner_idx < SPMD_LP_DESCS_COUNT; inner_idx++) {
  98. if (lp_desc->sp_id == lp_array[inner_idx].sp_id) {
  99. ERROR("Duplicate SPMD logical SP ID Detected (0x%x)\n",
  100. lp_desc->sp_id);
  101. return -EINVAL;
  102. }
  103. }
  104. }
  105. return 0;
  106. }
  107. static void spmd_encode_ffa_error(struct ffa_value *retval, int32_t error_code)
  108. {
  109. retval->func = FFA_ERROR;
  110. retval->arg1 = FFA_TARGET_INFO_MBZ;
  111. retval->arg2 = (uint32_t)error_code;
  112. retval->arg3 = FFA_TARGET_INFO_MBZ;
  113. retval->arg4 = FFA_TARGET_INFO_MBZ;
  114. retval->arg5 = FFA_TARGET_INFO_MBZ;
  115. retval->arg6 = FFA_TARGET_INFO_MBZ;
  116. retval->arg7 = FFA_TARGET_INFO_MBZ;
  117. }
  118. static void spmd_build_direct_message_req(spmd_spm_core_context_t *ctx,
  119. uint64_t x1, uint64_t x2,
  120. uint64_t x3, uint64_t x4)
  121. {
  122. gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
  123. write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_MSG_SEND_DIRECT_REQ_SMC32);
  124. write_ctx_reg(gpregs, CTX_GPREG_X1, x1);
  125. write_ctx_reg(gpregs, CTX_GPREG_X2, x2);
  126. write_ctx_reg(gpregs, CTX_GPREG_X3, x3);
  127. write_ctx_reg(gpregs, CTX_GPREG_X4, x4);
  128. write_ctx_reg(gpregs, CTX_GPREG_X5, 0U);
  129. write_ctx_reg(gpregs, CTX_GPREG_X6, 0U);
  130. write_ctx_reg(gpregs, CTX_GPREG_X7, 0U);
  131. }
  132. static void spmd_encode_ctx_to_ffa_value(spmd_spm_core_context_t *ctx,
  133. struct ffa_value *retval)
  134. {
  135. gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
  136. retval->func = read_ctx_reg(gpregs, CTX_GPREG_X0);
  137. retval->arg1 = read_ctx_reg(gpregs, CTX_GPREG_X1);
  138. retval->arg2 = read_ctx_reg(gpregs, CTX_GPREG_X2);
  139. retval->arg3 = read_ctx_reg(gpregs, CTX_GPREG_X3);
  140. retval->arg4 = read_ctx_reg(gpregs, CTX_GPREG_X4);
  141. retval->arg5 = read_ctx_reg(gpregs, CTX_GPREG_X5);
  142. retval->arg6 = read_ctx_reg(gpregs, CTX_GPREG_X6);
  143. retval->arg7 = read_ctx_reg(gpregs, CTX_GPREG_X7);
  144. retval->arg8 = read_ctx_reg(gpregs, CTX_GPREG_X8);
  145. retval->arg9 = read_ctx_reg(gpregs, CTX_GPREG_X9);
  146. retval->arg10 = read_ctx_reg(gpregs, CTX_GPREG_X10);
  147. retval->arg11 = read_ctx_reg(gpregs, CTX_GPREG_X11);
  148. retval->arg12 = read_ctx_reg(gpregs, CTX_GPREG_X12);
  149. retval->arg13 = read_ctx_reg(gpregs, CTX_GPREG_X13);
  150. retval->arg14 = read_ctx_reg(gpregs, CTX_GPREG_X14);
  151. retval->arg15 = read_ctx_reg(gpregs, CTX_GPREG_X15);
  152. retval->arg16 = read_ctx_reg(gpregs, CTX_GPREG_X16);
  153. retval->arg17 = read_ctx_reg(gpregs, CTX_GPREG_X17);
  154. }
  155. static void spmd_logical_sp_set_dir_req_ongoing(spmd_spm_core_context_t *ctx)
  156. {
  157. ctx->spmd_lp_sync_req_ongoing |= SPMD_LP_FFA_DIR_REQ_ONGOING;
  158. }
  159. static void spmd_logical_sp_reset_dir_req_ongoing(spmd_spm_core_context_t *ctx)
  160. {
  161. ctx->spmd_lp_sync_req_ongoing &= ~SPMD_LP_FFA_DIR_REQ_ONGOING;
  162. }
  163. static void spmd_build_ffa_info_get_regs(spmd_spm_core_context_t *ctx,
  164. const uint32_t uuid[4],
  165. const uint16_t start_index,
  166. const uint16_t tag)
  167. {
  168. gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
  169. uint64_t arg1 = (uint64_t)uuid[1] << 32 | uuid[0];
  170. uint64_t arg2 = (uint64_t)uuid[3] << 32 | uuid[2];
  171. uint64_t arg3 = start_index | (uint64_t)tag << 16;
  172. write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_PARTITION_INFO_GET_REGS_SMC64);
  173. write_ctx_reg(gpregs, CTX_GPREG_X1, arg1);
  174. write_ctx_reg(gpregs, CTX_GPREG_X2, arg2);
  175. write_ctx_reg(gpregs, CTX_GPREG_X3, arg3);
  176. write_ctx_reg(gpregs, CTX_GPREG_X4, 0U);
  177. write_ctx_reg(gpregs, CTX_GPREG_X5, 0U);
  178. write_ctx_reg(gpregs, CTX_GPREG_X6, 0U);
  179. write_ctx_reg(gpregs, CTX_GPREG_X7, 0U);
  180. write_ctx_reg(gpregs, CTX_GPREG_X8, 0U);
  181. write_ctx_reg(gpregs, CTX_GPREG_X9, 0U);
  182. write_ctx_reg(gpregs, CTX_GPREG_X10, 0U);
  183. write_ctx_reg(gpregs, CTX_GPREG_X11, 0U);
  184. write_ctx_reg(gpregs, CTX_GPREG_X12, 0U);
  185. write_ctx_reg(gpregs, CTX_GPREG_X13, 0U);
  186. write_ctx_reg(gpregs, CTX_GPREG_X14, 0U);
  187. write_ctx_reg(gpregs, CTX_GPREG_X15, 0U);
  188. write_ctx_reg(gpregs, CTX_GPREG_X16, 0U);
  189. write_ctx_reg(gpregs, CTX_GPREG_X17, 0U);
  190. }
  191. static void spmd_logical_sp_set_info_regs_ongoing(spmd_spm_core_context_t *ctx)
  192. {
  193. ctx->spmd_lp_sync_req_ongoing |= SPMD_LP_FFA_INFO_GET_REG_ONGOING;
  194. }
  195. static void spmd_logical_sp_reset_info_regs_ongoing(
  196. spmd_spm_core_context_t *ctx)
  197. {
  198. ctx->spmd_lp_sync_req_ongoing &= ~SPMD_LP_FFA_INFO_GET_REG_ONGOING;
  199. }
  200. static void spmd_fill_lp_info_array(
  201. struct ffa_partition_info_v1_1 (*partitions)[EL3_SPMD_MAX_NUM_LP],
  202. uint32_t uuid[4], uint16_t *lp_count_out)
  203. {
  204. uint16_t lp_count = 0;
  205. struct spmd_lp_desc *lp_array;
  206. bool uuid_is_null = is_null_uuid(uuid);
  207. if (SPMD_LP_DESCS_COUNT == 0U) {
  208. *lp_count_out = 0;
  209. return;
  210. }
  211. lp_array = get_spmd_el3_lp_array();
  212. for (uint16_t index = 0; index < SPMD_LP_DESCS_COUNT; ++index) {
  213. struct spmd_lp_desc *lp = &lp_array[index];
  214. if (uuid_is_null || uuid_match(uuid, lp->uuid)) {
  215. uint16_t array_index = lp_count;
  216. ++lp_count;
  217. (*partitions)[array_index].ep_id = lp->sp_id;
  218. (*partitions)[array_index].execution_ctx_count = 1;
  219. (*partitions)[array_index].properties = lp->properties;
  220. (*partitions)[array_index].properties |=
  221. (FFA_PARTITION_INFO_GET_AARCH64_STATE <<
  222. FFA_PARTITION_INFO_GET_EXEC_STATE_SHIFT);
  223. if (uuid_is_null) {
  224. memcpy(&((*partitions)[array_index].uuid),
  225. &lp->uuid, sizeof(lp->uuid));
  226. }
  227. }
  228. }
  229. *lp_count_out = lp_count;
  230. }
  231. static inline void spmd_pack_lp_count_props(
  232. uint64_t *xn, uint16_t ep_id, uint16_t vcpu_count,
  233. uint32_t properties)
  234. {
  235. *xn = (uint64_t)ep_id;
  236. *xn |= (uint64_t)vcpu_count << 16;
  237. *xn |= (uint64_t)properties << 32;
  238. }
  239. static inline void spmd_pack_lp_uuid(uint64_t *xn_1, uint64_t *xn_2,
  240. uint32_t uuid[4])
  241. {
  242. *xn_1 = (uint64_t)uuid[0];
  243. *xn_1 |= (uint64_t)uuid[1] << 32;
  244. *xn_2 = (uint64_t)uuid[2];
  245. *xn_2 |= (uint64_t)uuid[3] << 32;
  246. }
  247. #endif
  248. /*
  249. * Initialize SPMD logical partitions. This function assumes that it is called
  250. * only after the SPMC has successfully initialized.
  251. */
  252. int32_t spmd_logical_sp_init(void)
  253. {
  254. #if ENABLE_SPMD_LP
  255. int32_t rc = 0;
  256. struct spmd_lp_desc *spmd_lp_descs;
  257. assert(SPMD_LP_DESCS_COUNT <= EL3_SPMD_MAX_NUM_LP);
  258. if (is_spmd_lp_inited == true) {
  259. return 0;
  260. }
  261. if (is_spmc_inited == false) {
  262. return -1;
  263. }
  264. spmd_lp_descs = get_spmd_el3_lp_array();
  265. /* Perform initial validation of the SPMD Logical Partitions. */
  266. rc = el3_spmd_sp_desc_validate(spmd_lp_descs);
  267. if (rc != 0) {
  268. ERROR("Logical SPMD Partition validation failed!\n");
  269. return rc;
  270. }
  271. VERBOSE("SPMD Logical Secure Partition init start.\n");
  272. for (unsigned int i = 0U; i < SPMD_LP_DESCS_COUNT; i++) {
  273. rc = spmd_lp_descs[i].init();
  274. if (rc != 0) {
  275. ERROR("SPMD Logical SP (0x%x) failed to initialize\n",
  276. spmd_lp_descs[i].sp_id);
  277. return rc;
  278. }
  279. VERBOSE("SPMD Logical SP (0x%x) Initialized\n",
  280. spmd_lp_descs[i].sp_id);
  281. }
  282. INFO("SPMD Logical Secure Partition init completed.\n");
  283. if (rc == 0) {
  284. is_spmd_lp_inited = true;
  285. }
  286. return rc;
  287. #else
  288. return 0;
  289. #endif
  290. }
  291. void spmd_logical_sp_set_spmc_initialized(void)
  292. {
  293. #if ENABLE_SPMD_LP
  294. is_spmc_inited = true;
  295. #endif
  296. }
  297. void spmd_logical_sp_set_spmc_failure(void)
  298. {
  299. #if ENABLE_SPMD_LP
  300. is_spmc_inited = false;
  301. #endif
  302. }
  303. /*
  304. * This function takes an ffa_value structure populated with partition
  305. * information from an FFA_PARTITION_INFO_GET_REGS ABI call, extracts
  306. * the values and writes it into a ffa_partition_info_v1_1 structure for
  307. * other code to consume.
  308. */
  309. bool ffa_partition_info_regs_get_part_info(
  310. struct ffa_value *args, uint8_t idx,
  311. struct ffa_partition_info_v1_1 *partition_info)
  312. {
  313. uint64_t *arg_ptrs;
  314. uint64_t info, uuid_lo, uuid_high;
  315. /*
  316. * Each partition information is encoded in 3 registers, so there can be
  317. * a maximum of 5 entries.
  318. */
  319. if (idx >= 5 || partition_info == NULL) {
  320. return false;
  321. }
  322. /*
  323. * List of pointers to args in return value. arg0/func encodes ff-a
  324. * function, arg1 is reserved, arg2 encodes indices. arg3 and greater
  325. * values reflect partition properties.
  326. */
  327. arg_ptrs = (uint64_t *)args + ((idx * 3) + 3);
  328. info = *arg_ptrs;
  329. arg_ptrs++;
  330. uuid_lo = *arg_ptrs;
  331. arg_ptrs++;
  332. uuid_high = *arg_ptrs;
  333. partition_info->ep_id = (uint16_t)(info & 0xFFFFU);
  334. partition_info->execution_ctx_count = (uint16_t)((info >> 16) & 0xFFFFU);
  335. partition_info->properties = (uint32_t)(info >> 32);
  336. partition_info->uuid[0] = (uint32_t)(uuid_lo & 0xFFFFFFFFU);
  337. partition_info->uuid[1] = (uint32_t)((uuid_lo >> 32) & 0xFFFFFFFFU);
  338. partition_info->uuid[2] = (uint32_t)(uuid_high & 0xFFFFFFFFU);
  339. partition_info->uuid[3] = (uint32_t)((uuid_high >> 32) & 0xFFFFFFFFU);
  340. return true;
  341. }
  342. /*
  343. * This function is called by the SPMD in response to
  344. * an FFA_PARTITION_INFO_GET_REG ABI invocation by the SPMC. Secure partitions
  345. * are allowed to discover the presence of EL3 SPMD logical partitions by
  346. * invoking the aforementioned ABI and this function populates the required
  347. * information about EL3 SPMD logical partitions.
  348. */
  349. uint64_t spmd_el3_populate_logical_partition_info(void *handle, uint64_t x1,
  350. uint64_t x2, uint64_t x3)
  351. {
  352. #if ENABLE_SPMD_LP
  353. uint32_t target_uuid[4] = { 0 };
  354. uint32_t w0;
  355. uint32_t w1;
  356. uint32_t w2;
  357. uint32_t w3;
  358. uint16_t start_index;
  359. uint16_t tag;
  360. static struct ffa_partition_info_v1_1 partitions[EL3_SPMD_MAX_NUM_LP];
  361. uint16_t lp_count = 0;
  362. uint16_t max_idx = 0;
  363. uint16_t curr_idx = 0;
  364. uint8_t num_entries_to_ret = 0;
  365. struct ffa_value ret = { 0 };
  366. uint64_t *arg_ptrs = (uint64_t *)&ret + 3;
  367. w0 = (uint32_t)(x1 & 0xFFFFFFFFU);
  368. w1 = (uint32_t)(x1 >> 32);
  369. w2 = (uint32_t)(x2 & 0xFFFFFFFFU);
  370. w3 = (uint32_t)(x2 >> 32);
  371. target_uuid[0] = w0;
  372. target_uuid[1] = w1;
  373. target_uuid[2] = w2;
  374. target_uuid[3] = w3;
  375. start_index = (uint16_t)(x3 & 0xFFFFU);
  376. tag = (uint16_t)((x3 >> 16) & 0xFFFFU);
  377. assert(handle == cm_get_context(SECURE));
  378. if (tag != 0) {
  379. VERBOSE("Tag is not 0. Cannot return partition info.\n");
  380. return spmd_ffa_error_return(handle, FFA_ERROR_RETRY);
  381. }
  382. memset(&partitions, 0, sizeof(partitions));
  383. spmd_fill_lp_info_array(&partitions, target_uuid, &lp_count);
  384. if (lp_count == 0) {
  385. VERBOSE("No SPDM EL3 logical partitions exist.\n");
  386. return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
  387. }
  388. if (start_index >= lp_count) {
  389. VERBOSE("start_index = %d, lp_count = %d (start index must be"
  390. " less than partition count.\n",
  391. start_index, lp_count);
  392. return spmd_ffa_error_return(handle,
  393. FFA_ERROR_INVALID_PARAMETER);
  394. }
  395. max_idx = lp_count - 1;
  396. num_entries_to_ret = (max_idx - start_index) + 1;
  397. num_entries_to_ret =
  398. MIN(num_entries_to_ret, MAX_INFO_REGS_ENTRIES_PER_CALL);
  399. curr_idx = start_index + num_entries_to_ret - 1;
  400. assert(curr_idx <= max_idx);
  401. ret.func = FFA_SUCCESS_SMC64;
  402. ret.arg2 = (uint64_t)((sizeof(struct ffa_partition_info_v1_1) & 0xFFFFU) << 48);
  403. ret.arg2 |= (uint64_t)(curr_idx << 16);
  404. ret.arg2 |= (uint64_t)max_idx;
  405. for (uint16_t idx = start_index; idx <= curr_idx; ++idx) {
  406. spmd_pack_lp_count_props(arg_ptrs, partitions[idx].ep_id,
  407. partitions[idx].execution_ctx_count,
  408. partitions[idx].properties);
  409. arg_ptrs++;
  410. if (is_null_uuid(target_uuid)) {
  411. spmd_pack_lp_uuid(arg_ptrs, (arg_ptrs + 1),
  412. partitions[idx].uuid);
  413. }
  414. arg_ptrs += 2;
  415. }
  416. SMC_RET18(handle, ret.func, ret.arg1, ret.arg2, ret.arg3, ret.arg4,
  417. ret.arg5, ret.arg6, ret.arg7, ret.arg8, ret.arg9, ret.arg10,
  418. ret.arg11, ret.arg12, ret.arg13, ret.arg14, ret.arg15,
  419. ret.arg16, ret.arg17);
  420. #else
  421. return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
  422. #endif
  423. }
  424. /* This function can be used by an SPMD logical partition to invoke the
  425. * FFA_PARTITION_INFO_GET_REGS ABI to the SPMC, to discover the secure
  426. * partitions in the system. The function takes a UUID, start index and
  427. * tag and the partition information are returned in an ffa_value structure
  428. * and can be consumed by using appropriate helper functions.
  429. */
  430. bool spmd_el3_invoke_partition_info_get(
  431. const uint32_t target_uuid[4],
  432. const uint16_t start_index,
  433. const uint16_t tag,
  434. struct ffa_value *retval)
  435. {
  436. #if ENABLE_SPMD_LP
  437. uint64_t rc = UINT64_MAX;
  438. spmd_spm_core_context_t *ctx = spmd_get_context();
  439. if (retval == NULL) {
  440. return false;
  441. }
  442. memset(retval, 0, sizeof(*retval));
  443. if (!is_spmc_inited) {
  444. VERBOSE("Cannot discover partition before,"
  445. " SPMC is initialized.\n");
  446. spmd_encode_ffa_error(retval, FFA_ERROR_DENIED);
  447. return true;
  448. }
  449. if (tag != 0) {
  450. VERBOSE("Tag must be zero. other tags unsupported\n");
  451. spmd_encode_ffa_error(retval,
  452. FFA_ERROR_INVALID_PARAMETER);
  453. return true;
  454. }
  455. /* Save the non-secure context before entering SPMC */
  456. #if SPMD_SPM_AT_SEL2
  457. cm_el2_sysregs_context_save(NON_SECURE);
  458. #else
  459. cm_el1_sysregs_context_save(NON_SECURE);
  460. #endif
  461. spmd_build_ffa_info_get_regs(ctx, target_uuid, start_index, tag);
  462. spmd_logical_sp_set_info_regs_ongoing(ctx);
  463. rc = spmd_spm_core_sync_entry(ctx);
  464. if (rc != 0ULL) {
  465. ERROR("%s failed (%lx) on CPU%u\n", __func__, rc,
  466. plat_my_core_pos());
  467. panic();
  468. }
  469. spmd_logical_sp_reset_info_regs_ongoing(ctx);
  470. spmd_encode_ctx_to_ffa_value(ctx, retval);
  471. assert(is_ffa_error(retval) || is_ffa_success(retval));
  472. #if SPMD_SPM_AT_SEL2
  473. cm_el2_sysregs_context_restore(NON_SECURE);
  474. #else
  475. cm_el1_sysregs_context_restore(NON_SECURE);
  476. #endif
  477. cm_set_next_eret_context(NON_SECURE);
  478. return true;
  479. #else
  480. return false;
  481. #endif
  482. }
  483. /*******************************************************************************
  484. * This function sends an FF-A Direct Request from a partition in EL3 to a
  485. * partition that may reside under an SPMC (only lower ELs supported). The main
  486. * use of this API is for SPMD logical partitions.
  487. * The API is expected to be used when there are platform specific SMCs that
  488. * need to be routed to a secure partition that is FF-A compliant or when
  489. * there are group 0 interrupts that need to be handled first in EL3 and then
  490. * forwarded to an FF-A compliant secure partition. Therefore, it is expected
  491. * that the handle to the context provided belongs to the non-secure context.
  492. * This also means that interrupts/SMCs that trap to EL3 during secure execution
  493. * cannot use this API.
  494. * x1, x2, x3 and x4 are encoded as specified in the FF-A specification.
  495. * retval is used to pass the direct response values to the caller.
  496. * The function returns true if retval has valid values, and false otherwise.
  497. ******************************************************************************/
  498. bool spmd_el3_ffa_msg_direct_req(uint64_t x1,
  499. uint64_t x2,
  500. uint64_t x3,
  501. uint64_t x4,
  502. void *handle,
  503. struct ffa_value *retval)
  504. {
  505. #if ENABLE_SPMD_LP
  506. uint64_t rc = UINT64_MAX;
  507. spmd_spm_core_context_t *ctx = spmd_get_context();
  508. if (retval == NULL) {
  509. return false;
  510. }
  511. memset(retval, 0, sizeof(*retval));
  512. if (!is_spmd_lp_inited || !is_spmc_inited) {
  513. VERBOSE("Cannot send SPMD logical partition direct message,"
  514. " Partitions not initialized or SPMC not initialized.\n");
  515. spmd_encode_ffa_error(retval, FFA_ERROR_DENIED);
  516. return true;
  517. }
  518. /*
  519. * x2 must be zero, since there is no support for framework message via
  520. * an SPMD logical partition. This is sort of a useless check and it is
  521. * possible to not take parameter. However, as the framework extends it
  522. * may be useful to have x2 and extend this function later with
  523. * functionality based on x2.
  524. */
  525. if (x2 != 0) {
  526. VERBOSE("x2 must be zero. Cannot send framework message.\n");
  527. spmd_encode_ffa_error(retval, FFA_ERROR_DENIED);
  528. return true;
  529. }
  530. /*
  531. * Current context must be non-secure. API is expected to be used
  532. * when entry into EL3 and the SPMD logical partition is via an
  533. * interrupt that occurs when execution is in normal world and
  534. * SMCs from normal world. FF-A compliant SPMCs are expected to
  535. * trap interrupts during secure execution in lower ELs since they
  536. * are usually not re-entrant and SMCs from secure world can be
  537. * handled synchronously. There is no known use case for an SPMD
  538. * logical partition to send a direct message to another partition
  539. * in response to a secure interrupt or SMCs from secure world.
  540. */
  541. if (handle != cm_get_context(NON_SECURE)) {
  542. VERBOSE("Handle must be for the non-secure context.\n");
  543. spmd_encode_ffa_error(retval, FFA_ERROR_DENIED);
  544. return true;
  545. }
  546. if (!is_spmd_lp_id(ffa_endpoint_source(x1))) {
  547. VERBOSE("Source ID must be valid SPMD logical partition"
  548. " ID.\n");
  549. spmd_encode_ffa_error(retval,
  550. FFA_ERROR_INVALID_PARAMETER);
  551. return true;
  552. }
  553. if (is_spmd_lp_id(ffa_endpoint_destination(x1))) {
  554. VERBOSE("Destination ID must not be SPMD logical partition"
  555. " ID.\n");
  556. spmd_encode_ffa_error(retval,
  557. FFA_ERROR_INVALID_PARAMETER);
  558. return true;
  559. }
  560. if (!ffa_is_secure_world_id(ffa_endpoint_destination(x1))) {
  561. VERBOSE("Destination ID must be secure world ID.\n");
  562. spmd_encode_ffa_error(retval,
  563. FFA_ERROR_INVALID_PARAMETER);
  564. return true;
  565. }
  566. if (ffa_endpoint_destination(x1) == SPMD_DIRECT_MSG_ENDPOINT_ID) {
  567. VERBOSE("Destination ID must not be SPMD ID.\n");
  568. spmd_encode_ffa_error(retval,
  569. FFA_ERROR_INVALID_PARAMETER);
  570. return true;
  571. }
  572. if (ffa_endpoint_destination(x1) == spmd_spmc_id_get()) {
  573. VERBOSE("Destination ID must not be SPMC ID.\n");
  574. spmd_encode_ffa_error(retval,
  575. FFA_ERROR_INVALID_PARAMETER);
  576. return true;
  577. }
  578. /* Save the non-secure context before entering SPMC */
  579. #if SPMD_SPM_AT_SEL2
  580. cm_el2_sysregs_context_save(NON_SECURE);
  581. #else
  582. cm_el1_sysregs_context_save(NON_SECURE);
  583. #endif
  584. /*
  585. * Perform synchronous entry into the SPMC. Synchronous entry is
  586. * required because the spec requires that a direct message request
  587. * from an SPMD LP look like a function call from it's perspective.
  588. */
  589. spmd_build_direct_message_req(ctx, x1, x2, x3, x4);
  590. spmd_logical_sp_set_dir_req_ongoing(ctx);
  591. rc = spmd_spm_core_sync_entry(ctx);
  592. spmd_logical_sp_reset_dir_req_ongoing(ctx);
  593. if (rc != 0ULL) {
  594. ERROR("%s failed (%lx) on CPU%u\n", __func__, rc,
  595. plat_my_core_pos());
  596. panic();
  597. } else {
  598. spmd_encode_ctx_to_ffa_value(ctx, retval);
  599. /*
  600. * Only expect error or direct response,
  601. * spmd_spm_core_sync_exit should not be called on other paths.
  602. * Checks are asserts since the LSP can fail gracefully if the
  603. * source or destination ids are not the same. Panic'ing would
  604. * not provide any benefit.
  605. */
  606. assert(is_ffa_error(retval) || is_ffa_direct_msg_resp(retval));
  607. assert(is_ffa_error(retval) ||
  608. (ffa_endpoint_destination(retval->arg1) ==
  609. ffa_endpoint_source(x1)));
  610. assert(is_ffa_error(retval) ||
  611. (ffa_endpoint_source(retval->arg1) ==
  612. ffa_endpoint_destination(x1)));
  613. }
  614. #if SPMD_SPM_AT_SEL2
  615. cm_el2_sysregs_context_restore(NON_SECURE);
  616. #else
  617. cm_el1_sysregs_context_restore(NON_SECURE);
  618. #endif
  619. cm_set_next_eret_context(NON_SECURE);
  620. return true;
  621. #else
  622. return false;
  623. #endif
  624. }
  625. bool is_spmd_logical_sp_info_regs_req_in_progress(
  626. spmd_spm_core_context_t *ctx)
  627. {
  628. #if ENABLE_SPMD_LP
  629. return ((ctx->spmd_lp_sync_req_ongoing & SPMD_LP_FFA_INFO_GET_REG_ONGOING)
  630. == SPMD_LP_FFA_INFO_GET_REG_ONGOING);
  631. #else
  632. return false;
  633. #endif
  634. }
  635. bool is_spmd_logical_sp_dir_req_in_progress(
  636. spmd_spm_core_context_t *ctx)
  637. {
  638. #if ENABLE_SPMD_LP
  639. return ((ctx->spmd_lp_sync_req_ongoing & SPMD_LP_FFA_DIR_REQ_ONGOING)
  640. == SPMD_LP_FFA_DIR_REQ_ONGOING);
  641. #else
  642. return false;
  643. #endif
  644. }