spmd_main.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  1. /*
  2. * Copyright (c) 2020-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 <inttypes.h>
  9. #include <stdint.h>
  10. #include <string.h>
  11. #include <arch_helpers.h>
  12. #include <arch/aarch64/arch_features.h>
  13. #include <bl31/bl31.h>
  14. #include <bl31/interrupt_mgmt.h>
  15. #include <common/debug.h>
  16. #include <common/runtime_svc.h>
  17. #include <common/tbbr/tbbr_img_def.h>
  18. #include <lib/el3_runtime/context_mgmt.h>
  19. #include <lib/fconf/fconf.h>
  20. #include <lib/fconf/fconf_dyn_cfg_getter.h>
  21. #include <lib/smccc.h>
  22. #include <lib/spinlock.h>
  23. #include <lib/utils.h>
  24. #include <lib/xlat_tables/xlat_tables_v2.h>
  25. #include <plat/common/common_def.h>
  26. #include <plat/common/platform.h>
  27. #include <platform_def.h>
  28. #include <services/el3_spmd_logical_sp.h>
  29. #include <services/ffa_svc.h>
  30. #include <services/spmc_svc.h>
  31. #include <services/spmd_svc.h>
  32. #include <smccc_helpers.h>
  33. #include "spmd_private.h"
  34. /*******************************************************************************
  35. * SPM Core context information.
  36. ******************************************************************************/
  37. static spmd_spm_core_context_t spm_core_context[PLATFORM_CORE_COUNT];
  38. /*******************************************************************************
  39. * SPM Core attribute information is read from its manifest if the SPMC is not
  40. * at EL3. Else, it is populated from the SPMC directly.
  41. ******************************************************************************/
  42. static spmc_manifest_attribute_t spmc_attrs;
  43. /*******************************************************************************
  44. * SPM Core entry point information. Discovered on the primary core and reused
  45. * on secondary cores.
  46. ******************************************************************************/
  47. static entry_point_info_t *spmc_ep_info;
  48. /*******************************************************************************
  49. * SPM Core context on current CPU get helper.
  50. ******************************************************************************/
  51. spmd_spm_core_context_t *spmd_get_context(void)
  52. {
  53. return &spm_core_context[plat_my_core_pos()];
  54. }
  55. /*******************************************************************************
  56. * SPM Core ID getter.
  57. ******************************************************************************/
  58. uint16_t spmd_spmc_id_get(void)
  59. {
  60. return spmc_attrs.spmc_id;
  61. }
  62. /*******************************************************************************
  63. * Static function declaration.
  64. ******************************************************************************/
  65. static int32_t spmd_init(void);
  66. static int spmd_spmc_init(void *pm_addr);
  67. static uint64_t spmd_smc_forward(uint32_t smc_fid,
  68. bool secure_origin,
  69. uint64_t x1,
  70. uint64_t x2,
  71. uint64_t x3,
  72. uint64_t x4,
  73. void *cookie,
  74. void *handle,
  75. uint64_t flags);
  76. /******************************************************************************
  77. * Builds an SPMD to SPMC direct message request.
  78. *****************************************************************************/
  79. void spmd_build_spmc_message(gp_regs_t *gpregs, uint8_t target_func,
  80. unsigned long long message)
  81. {
  82. write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_MSG_SEND_DIRECT_REQ_SMC32);
  83. write_ctx_reg(gpregs, CTX_GPREG_X1,
  84. (SPMD_DIRECT_MSG_ENDPOINT_ID << FFA_DIRECT_MSG_SOURCE_SHIFT) |
  85. spmd_spmc_id_get());
  86. write_ctx_reg(gpregs, CTX_GPREG_X2, BIT(31) | target_func);
  87. write_ctx_reg(gpregs, CTX_GPREG_X3, message);
  88. /* Zero out x4-x7 for the direct request emitted towards the SPMC. */
  89. write_ctx_reg(gpregs, CTX_GPREG_X4, 0);
  90. write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
  91. write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
  92. write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
  93. }
  94. /*******************************************************************************
  95. * This function takes an SPMC context pointer and performs a synchronous
  96. * SPMC entry.
  97. ******************************************************************************/
  98. uint64_t spmd_spm_core_sync_entry(spmd_spm_core_context_t *spmc_ctx)
  99. {
  100. uint64_t rc;
  101. assert(spmc_ctx != NULL);
  102. cm_set_context(&(spmc_ctx->cpu_ctx), SECURE);
  103. /* Restore the context assigned above */
  104. #if SPMD_SPM_AT_SEL2
  105. cm_el2_sysregs_context_restore(SECURE);
  106. #else
  107. cm_el1_sysregs_context_restore(SECURE);
  108. #endif
  109. cm_set_next_eret_context(SECURE);
  110. /* Enter SPMC */
  111. rc = spmd_spm_core_enter(&spmc_ctx->c_rt_ctx);
  112. /* Save secure state */
  113. #if SPMD_SPM_AT_SEL2
  114. cm_el2_sysregs_context_save(SECURE);
  115. #else
  116. cm_el1_sysregs_context_save(SECURE);
  117. #endif
  118. return rc;
  119. }
  120. /*******************************************************************************
  121. * This function returns to the place where spmd_spm_core_sync_entry() was
  122. * called originally.
  123. ******************************************************************************/
  124. __dead2 void spmd_spm_core_sync_exit(uint64_t rc)
  125. {
  126. spmd_spm_core_context_t *ctx = spmd_get_context();
  127. /* Get current CPU context from SPMC context */
  128. assert(cm_get_context(SECURE) == &(ctx->cpu_ctx));
  129. /*
  130. * The SPMD must have initiated the original request through a
  131. * synchronous entry into SPMC. Jump back to the original C runtime
  132. * context with the value of rc in x0;
  133. */
  134. spmd_spm_core_exit(ctx->c_rt_ctx, rc);
  135. panic();
  136. }
  137. /*******************************************************************************
  138. * Jump to the SPM Core for the first time.
  139. ******************************************************************************/
  140. static int32_t spmd_init(void)
  141. {
  142. spmd_spm_core_context_t *ctx = spmd_get_context();
  143. uint64_t rc;
  144. VERBOSE("SPM Core init start.\n");
  145. /* Primary boot core enters the SPMC for initialization. */
  146. ctx->state = SPMC_STATE_ON_PENDING;
  147. rc = spmd_spm_core_sync_entry(ctx);
  148. if (rc != 0ULL) {
  149. ERROR("SPMC initialisation failed 0x%" PRIx64 "\n", rc);
  150. return 0;
  151. }
  152. ctx->state = SPMC_STATE_ON;
  153. VERBOSE("SPM Core init end.\n");
  154. spmd_logical_sp_set_spmc_initialized();
  155. rc = spmd_logical_sp_init();
  156. if (rc != 0) {
  157. WARN("SPMD Logical partitions failed init.\n");
  158. }
  159. return 1;
  160. }
  161. /*******************************************************************************
  162. * spmd_secure_interrupt_handler
  163. * Enter the SPMC for further handling of the secure interrupt by the SPMC
  164. * itself or a Secure Partition.
  165. ******************************************************************************/
  166. static uint64_t spmd_secure_interrupt_handler(uint32_t id,
  167. uint32_t flags,
  168. void *handle,
  169. void *cookie)
  170. {
  171. spmd_spm_core_context_t *ctx = spmd_get_context();
  172. gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
  173. int64_t rc;
  174. /* Sanity check the security state when the exception was generated */
  175. assert(get_interrupt_src_ss(flags) == NON_SECURE);
  176. /* Sanity check the pointer to this cpu's context */
  177. assert(handle == cm_get_context(NON_SECURE));
  178. /* Save the non-secure context before entering SPMC */
  179. #if SPMD_SPM_AT_SEL2
  180. cm_el2_sysregs_context_save(NON_SECURE);
  181. #else
  182. cm_el1_sysregs_context_save(NON_SECURE);
  183. #if CTX_INCLUDE_FPREGS || CTX_INCLUDE_SVE_REGS
  184. /*
  185. * The hint bit denoting absence of SVE live state is effectively false
  186. * in this scenario where execution was trapped to EL3 due to FIQ.
  187. */
  188. simd_ctx_save(NON_SECURE, false);
  189. #endif
  190. #endif
  191. /* Convey the event to the SPMC through the FFA_INTERRUPT interface. */
  192. write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_INTERRUPT);
  193. write_ctx_reg(gpregs, CTX_GPREG_X1, 0);
  194. write_ctx_reg(gpregs, CTX_GPREG_X2, 0);
  195. write_ctx_reg(gpregs, CTX_GPREG_X3, 0);
  196. write_ctx_reg(gpregs, CTX_GPREG_X4, 0);
  197. write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
  198. write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
  199. write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
  200. /* Mark current core as handling a secure interrupt. */
  201. ctx->secure_interrupt_ongoing = true;
  202. #if CTX_INCLUDE_FPREGS || CTX_INCLUDE_SVE_REGS
  203. simd_ctx_restore(SECURE);
  204. #endif
  205. rc = spmd_spm_core_sync_entry(ctx);
  206. #if CTX_INCLUDE_FPREGS || CTX_INCLUDE_SVE_REGS
  207. simd_ctx_save(SECURE, false);
  208. #endif
  209. if (rc != 0ULL) {
  210. ERROR("%s failed (%" PRId64 ") on CPU%u\n", __func__, rc, plat_my_core_pos());
  211. }
  212. ctx->secure_interrupt_ongoing = false;
  213. #if SPMD_SPM_AT_SEL2
  214. cm_el2_sysregs_context_restore(NON_SECURE);
  215. #else
  216. cm_el1_sysregs_context_restore(NON_SECURE);
  217. #if CTX_INCLUDE_FPREGS || CTX_INCLUDE_SVE_REGS
  218. simd_ctx_restore(NON_SECURE);
  219. #endif
  220. #endif
  221. cm_set_next_eret_context(NON_SECURE);
  222. SMC_RET0(&ctx->cpu_ctx);
  223. }
  224. #if (EL3_EXCEPTION_HANDLING == 0)
  225. /*******************************************************************************
  226. * spmd_group0_interrupt_handler_nwd
  227. * Group0 secure interrupt in the normal world are trapped to EL3. Delegate the
  228. * handling of the interrupt to the platform handler, and return only upon
  229. * successfully handling the Group0 interrupt.
  230. ******************************************************************************/
  231. static uint64_t spmd_group0_interrupt_handler_nwd(uint32_t id,
  232. uint32_t flags,
  233. void *handle,
  234. void *cookie)
  235. {
  236. uint32_t intid;
  237. /* Sanity check the security state when the exception was generated. */
  238. assert(get_interrupt_src_ss(flags) == NON_SECURE);
  239. /* Sanity check the pointer to this cpu's context. */
  240. assert(handle == cm_get_context(NON_SECURE));
  241. assert(id == INTR_ID_UNAVAILABLE);
  242. assert(plat_ic_get_pending_interrupt_type() == INTR_TYPE_EL3);
  243. intid = plat_ic_acknowledge_interrupt();
  244. if (plat_spmd_handle_group0_interrupt(intid) < 0) {
  245. ERROR("Group0 interrupt %u not handled\n", intid);
  246. panic();
  247. }
  248. /* Deactivate the corresponding Group0 interrupt. */
  249. plat_ic_end_of_interrupt(intid);
  250. return 0U;
  251. }
  252. #endif
  253. /*******************************************************************************
  254. * spmd_handle_group0_intr_swd
  255. * SPMC delegates handling of Group0 secure interrupt to EL3 firmware using
  256. * FFA_EL3_INTR_HANDLE SMC call. Further, SPMD delegates the handling of the
  257. * interrupt to the platform handler, and returns only upon successfully
  258. * handling the Group0 interrupt.
  259. ******************************************************************************/
  260. static uint64_t spmd_handle_group0_intr_swd(void *handle)
  261. {
  262. uint32_t intid;
  263. /* Sanity check the pointer to this cpu's context */
  264. assert(handle == cm_get_context(SECURE));
  265. assert(plat_ic_get_pending_interrupt_type() == INTR_TYPE_EL3);
  266. intid = plat_ic_acknowledge_interrupt();
  267. /*
  268. * TODO: Currently due to a limitation in SPMD implementation, the
  269. * platform handler is expected to not delegate handling to NWd while
  270. * processing Group0 secure interrupt.
  271. */
  272. if (plat_spmd_handle_group0_interrupt(intid) < 0) {
  273. /* Group0 interrupt was not handled by the platform. */
  274. ERROR("Group0 interrupt %u not handled\n", intid);
  275. panic();
  276. }
  277. /* Deactivate the corresponding Group0 interrupt. */
  278. plat_ic_end_of_interrupt(intid);
  279. /* Return success. */
  280. SMC_RET8(handle, FFA_SUCCESS_SMC32, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
  281. FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
  282. FFA_PARAM_MBZ);
  283. }
  284. #if ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31
  285. static int spmd_dynamic_map_mem(uintptr_t base_addr, size_t size,
  286. unsigned int attr, uintptr_t *align_addr,
  287. size_t *align_size)
  288. {
  289. uintptr_t base_addr_align;
  290. size_t mapped_size_align;
  291. int rc;
  292. /* Page aligned address and size if necessary */
  293. base_addr_align = page_align(base_addr, DOWN);
  294. mapped_size_align = page_align(size, UP);
  295. if ((base_addr != base_addr_align) &&
  296. (size == mapped_size_align)) {
  297. mapped_size_align += PAGE_SIZE;
  298. }
  299. /*
  300. * Map dynamically given region with its aligned base address and
  301. * size
  302. */
  303. rc = mmap_add_dynamic_region((unsigned long long)base_addr_align,
  304. base_addr_align,
  305. mapped_size_align,
  306. attr);
  307. if (rc == 0) {
  308. *align_addr = base_addr_align;
  309. *align_size = mapped_size_align;
  310. }
  311. return rc;
  312. }
  313. static void spmd_do_sec_cpy(uintptr_t root_base_addr, uintptr_t sec_base_addr,
  314. size_t size)
  315. {
  316. uintptr_t root_base_addr_align, sec_base_addr_align;
  317. size_t root_mapped_size_align, sec_mapped_size_align;
  318. int rc;
  319. assert(root_base_addr != 0UL);
  320. assert(sec_base_addr != 0UL);
  321. assert(size != 0UL);
  322. /* Map the memory with required attributes */
  323. rc = spmd_dynamic_map_mem(root_base_addr, size, MT_RO_DATA | MT_ROOT,
  324. &root_base_addr_align,
  325. &root_mapped_size_align);
  326. if (rc != 0) {
  327. ERROR("%s %s %lu (%d)\n", "Error while mapping", "root region",
  328. root_base_addr, rc);
  329. panic();
  330. }
  331. rc = spmd_dynamic_map_mem(sec_base_addr, size, MT_RW_DATA | MT_SECURE,
  332. &sec_base_addr_align, &sec_mapped_size_align);
  333. if (rc != 0) {
  334. ERROR("%s %s %lu (%d)\n", "Error while mapping",
  335. "secure region", sec_base_addr, rc);
  336. panic();
  337. }
  338. /* Do copy operation */
  339. (void)memcpy((void *)sec_base_addr, (void *)root_base_addr, size);
  340. /* Unmap root memory region */
  341. rc = mmap_remove_dynamic_region(root_base_addr_align,
  342. root_mapped_size_align);
  343. if (rc != 0) {
  344. ERROR("%s %s %lu (%d)\n", "Error while unmapping",
  345. "root region", root_base_addr_align, rc);
  346. panic();
  347. }
  348. /* Unmap secure memory region */
  349. rc = mmap_remove_dynamic_region(sec_base_addr_align,
  350. sec_mapped_size_align);
  351. if (rc != 0) {
  352. ERROR("%s %s %lu (%d)\n", "Error while unmapping",
  353. "secure region", sec_base_addr_align, rc);
  354. panic();
  355. }
  356. }
  357. #endif /* ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31 */
  358. /*******************************************************************************
  359. * Loads SPMC manifest and inits SPMC.
  360. ******************************************************************************/
  361. static int spmd_spmc_init(void *pm_addr)
  362. {
  363. cpu_context_t *cpu_ctx;
  364. unsigned int core_id;
  365. uint32_t ep_attr, flags;
  366. int rc;
  367. const struct dyn_cfg_dtb_info_t *image_info __unused;
  368. /* Load the SPM Core manifest */
  369. rc = plat_spm_core_manifest_load(&spmc_attrs, pm_addr);
  370. if (rc != 0) {
  371. WARN("No or invalid SPM Core manifest image provided by BL2\n");
  372. return rc;
  373. }
  374. /*
  375. * Ensure that the SPM Core version is compatible with the SPM
  376. * Dispatcher version.
  377. */
  378. if ((spmc_attrs.major_version != FFA_VERSION_MAJOR) ||
  379. (spmc_attrs.minor_version > FFA_VERSION_MINOR)) {
  380. WARN("Unsupported FFA version (%u.%u)\n",
  381. spmc_attrs.major_version, spmc_attrs.minor_version);
  382. return -EINVAL;
  383. }
  384. VERBOSE("FFA version (%u.%u)\n", spmc_attrs.major_version,
  385. spmc_attrs.minor_version);
  386. VERBOSE("SPM Core run time EL%x.\n",
  387. SPMD_SPM_AT_SEL2 ? MODE_EL2 : MODE_EL1);
  388. /* Validate the SPMC ID, Ensure high bit is set */
  389. if (((spmc_attrs.spmc_id >> SPMC_SECURE_ID_SHIFT) &
  390. SPMC_SECURE_ID_MASK) == 0U) {
  391. WARN("Invalid ID (0x%x) for SPMC.\n", spmc_attrs.spmc_id);
  392. return -EINVAL;
  393. }
  394. /* Validate the SPM Core execution state */
  395. if ((spmc_attrs.exec_state != MODE_RW_64) &&
  396. (spmc_attrs.exec_state != MODE_RW_32)) {
  397. WARN("Unsupported %s%x.\n", "SPM Core execution state 0x",
  398. spmc_attrs.exec_state);
  399. return -EINVAL;
  400. }
  401. VERBOSE("%s%x.\n", "SPM Core execution state 0x",
  402. spmc_attrs.exec_state);
  403. #if SPMD_SPM_AT_SEL2
  404. /* Ensure manifest has not requested AArch32 state in S-EL2 */
  405. if (spmc_attrs.exec_state == MODE_RW_32) {
  406. WARN("AArch32 state at S-EL2 is not supported.\n");
  407. return -EINVAL;
  408. }
  409. /*
  410. * Check if S-EL2 is supported on this system if S-EL2
  411. * is required for SPM
  412. */
  413. if (!is_feat_sel2_supported()) {
  414. WARN("SPM Core run time S-EL2 is not supported.\n");
  415. return -EINVAL;
  416. }
  417. #endif /* SPMD_SPM_AT_SEL2 */
  418. /* Initialise an entrypoint to set up the CPU context */
  419. ep_attr = SECURE | EP_ST_ENABLE;
  420. if ((read_sctlr_el3() & SCTLR_EE_BIT) != 0ULL) {
  421. ep_attr |= EP_EE_BIG;
  422. }
  423. SET_PARAM_HEAD(spmc_ep_info, PARAM_EP, VERSION_1, ep_attr);
  424. /*
  425. * Populate SPSR for SPM Core based upon validated parameters from the
  426. * manifest.
  427. */
  428. if (spmc_attrs.exec_state == MODE_RW_32) {
  429. spmc_ep_info->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
  430. SPSR_E_LITTLE,
  431. DAIF_FIQ_BIT |
  432. DAIF_IRQ_BIT |
  433. DAIF_ABT_BIT);
  434. } else {
  435. #if SPMD_SPM_AT_SEL2
  436. static const uint32_t runtime_el = MODE_EL2;
  437. #else
  438. static const uint32_t runtime_el = MODE_EL1;
  439. #endif
  440. spmc_ep_info->spsr = SPSR_64(runtime_el,
  441. MODE_SP_ELX,
  442. DISABLE_ALL_EXCEPTIONS);
  443. }
  444. #if ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31
  445. image_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TOS_FW_CONFIG_ID);
  446. assert(image_info != NULL);
  447. if ((image_info->config_addr == 0UL) ||
  448. (image_info->secondary_config_addr == 0UL) ||
  449. (image_info->config_max_size == 0UL)) {
  450. return -EINVAL;
  451. }
  452. /* Copy manifest from root->secure region */
  453. spmd_do_sec_cpy(image_info->config_addr,
  454. image_info->secondary_config_addr,
  455. image_info->config_max_size);
  456. /* Update ep info of BL32 */
  457. assert(spmc_ep_info != NULL);
  458. spmc_ep_info->args.arg0 = image_info->secondary_config_addr;
  459. #endif /* ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31 */
  460. /* Set an initial SPMC context state for all cores. */
  461. for (core_id = 0U; core_id < PLATFORM_CORE_COUNT; core_id++) {
  462. spm_core_context[core_id].state = SPMC_STATE_OFF;
  463. /* Setup an initial cpu context for the SPMC. */
  464. cpu_ctx = &spm_core_context[core_id].cpu_ctx;
  465. cm_setup_context(cpu_ctx, spmc_ep_info);
  466. /*
  467. * Pass the core linear ID to the SPMC through x4.
  468. * (TF-A implementation defined behavior helping
  469. * a legacy TOS migration to adopt FF-A).
  470. */
  471. write_ctx_reg(get_gpregs_ctx(cpu_ctx), CTX_GPREG_X4, core_id);
  472. }
  473. /* Register power management hooks with PSCI */
  474. psci_register_spd_pm_hook(&spmd_pm);
  475. /* Register init function for deferred init. */
  476. bl31_register_bl32_init(&spmd_init);
  477. INFO("SPM Core setup done.\n");
  478. /*
  479. * Register an interrupt handler routing secure interrupts to SPMD
  480. * while the NWd is running.
  481. */
  482. flags = 0;
  483. set_interrupt_rm_flag(flags, NON_SECURE);
  484. rc = register_interrupt_type_handler(INTR_TYPE_S_EL1,
  485. spmd_secure_interrupt_handler,
  486. flags);
  487. if (rc != 0) {
  488. panic();
  489. }
  490. /*
  491. * Permit configurations where the SPM resides at S-EL1/2 and upon a
  492. * Group0 interrupt triggering while the normal world runs, the
  493. * interrupt is routed either through the EHF or directly to the SPMD:
  494. *
  495. * EL3_EXCEPTION_HANDLING=0: the Group0 interrupt is routed to the SPMD
  496. * for handling by spmd_group0_interrupt_handler_nwd.
  497. *
  498. * EL3_EXCEPTION_HANDLING=1: the Group0 interrupt is routed to the EHF.
  499. *
  500. */
  501. #if (EL3_EXCEPTION_HANDLING == 0)
  502. /*
  503. * If EL3 interrupts are supported by the platform, register an
  504. * interrupt handler routing Group0 interrupts to SPMD while the NWd is
  505. * running.
  506. */
  507. if (plat_ic_has_interrupt_type(INTR_TYPE_EL3)) {
  508. rc = register_interrupt_type_handler(INTR_TYPE_EL3,
  509. spmd_group0_interrupt_handler_nwd,
  510. flags);
  511. if (rc != 0) {
  512. panic();
  513. }
  514. }
  515. #endif
  516. return 0;
  517. }
  518. /*******************************************************************************
  519. * Initialize context of SPM Core.
  520. ******************************************************************************/
  521. int spmd_setup(void)
  522. {
  523. int rc;
  524. void *spmc_manifest;
  525. /*
  526. * If the SPMC is at EL3, then just initialise it directly. The
  527. * shenanigans of when it is at a lower EL are not needed.
  528. */
  529. if (is_spmc_at_el3()) {
  530. /* Allow the SPMC to populate its attributes directly. */
  531. spmc_populate_attrs(&spmc_attrs);
  532. rc = spmc_setup();
  533. if (rc != 0) {
  534. WARN("SPMC initialisation failed 0x%x.\n", rc);
  535. }
  536. return 0;
  537. }
  538. spmc_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
  539. if (spmc_ep_info == NULL) {
  540. WARN("No SPM Core image provided by BL2 boot loader.\n");
  541. return 0;
  542. }
  543. /* Under no circumstances will this parameter be 0 */
  544. assert(spmc_ep_info->pc != 0ULL);
  545. /*
  546. * Check if BL32 ep_info has a reference to 'tos_fw_config'. This will
  547. * be used as a manifest for the SPM Core at the next lower EL/mode.
  548. */
  549. spmc_manifest = (void *)spmc_ep_info->args.arg0;
  550. if (spmc_manifest == NULL) {
  551. WARN("Invalid or absent SPM Core manifest.\n");
  552. return 0;
  553. }
  554. /* Load manifest, init SPMC */
  555. rc = spmd_spmc_init(spmc_manifest);
  556. if (rc != 0) {
  557. WARN("Booting device without SPM initialization.\n");
  558. }
  559. return 0;
  560. }
  561. /*******************************************************************************
  562. * Forward FF-A SMCs to the other security state.
  563. ******************************************************************************/
  564. uint64_t spmd_smc_switch_state(uint32_t smc_fid,
  565. bool secure_origin,
  566. uint64_t x1,
  567. uint64_t x2,
  568. uint64_t x3,
  569. uint64_t x4,
  570. void *handle,
  571. uint64_t flags)
  572. {
  573. unsigned int secure_state_in = (secure_origin) ? SECURE : NON_SECURE;
  574. unsigned int secure_state_out = (!secure_origin) ? SECURE : NON_SECURE;
  575. void *ctx_out;
  576. #if SPMD_SPM_AT_SEL2
  577. if ((secure_state_out == SECURE) && (is_sve_hint_set(flags) == true)) {
  578. /*
  579. * Set the SVE hint bit in x0 and pass to the lower secure EL,
  580. * if it was set by the caller.
  581. */
  582. smc_fid |= (FUNCID_SVE_HINT_MASK << FUNCID_SVE_HINT_SHIFT);
  583. }
  584. #endif
  585. /* Save incoming security state */
  586. #if SPMD_SPM_AT_SEL2
  587. cm_el2_sysregs_context_save(secure_state_in);
  588. #else
  589. cm_el1_sysregs_context_save(secure_state_in);
  590. #if CTX_INCLUDE_FPREGS || CTX_INCLUDE_SVE_REGS
  591. /* Forward the hint bit denoting the absence of SVE live state. */
  592. simd_ctx_save(secure_state_in, (!secure_origin && (is_sve_hint_set(flags) == true)));
  593. #endif
  594. #endif
  595. /* Restore outgoing security state */
  596. #if SPMD_SPM_AT_SEL2
  597. cm_el2_sysregs_context_restore(secure_state_out);
  598. #else
  599. cm_el1_sysregs_context_restore(secure_state_out);
  600. #if CTX_INCLUDE_FPREGS || CTX_INCLUDE_SVE_REGS
  601. simd_ctx_restore(secure_state_out);
  602. #endif
  603. #endif
  604. cm_set_next_eret_context(secure_state_out);
  605. ctx_out = cm_get_context(secure_state_out);
  606. #if SPMD_SPM_AT_SEL2
  607. /*
  608. * If SPMC is at SEL2, save additional registers x8-x17, which may
  609. * be used in FF-A calls such as FFA_PARTITION_INFO_GET_REGS.
  610. * Note that technically, all SPMCs can support this, but this code is
  611. * under ifdef to minimize breakage in case other SPMCs do not save
  612. * and restore x8-x17.
  613. * We also need to pass through these registers since not all FF-A ABIs
  614. * modify x8-x17, in which case, SMCCC requires that these registers be
  615. * preserved, so the SPMD passes through these registers and expects the
  616. * SPMC to save and restore (potentially also modify) them.
  617. */
  618. SMC_RET18(ctx_out, smc_fid, x1, x2, x3, x4,
  619. SMC_GET_GP(handle, CTX_GPREG_X5),
  620. SMC_GET_GP(handle, CTX_GPREG_X6),
  621. SMC_GET_GP(handle, CTX_GPREG_X7),
  622. SMC_GET_GP(handle, CTX_GPREG_X8),
  623. SMC_GET_GP(handle, CTX_GPREG_X9),
  624. SMC_GET_GP(handle, CTX_GPREG_X10),
  625. SMC_GET_GP(handle, CTX_GPREG_X11),
  626. SMC_GET_GP(handle, CTX_GPREG_X12),
  627. SMC_GET_GP(handle, CTX_GPREG_X13),
  628. SMC_GET_GP(handle, CTX_GPREG_X14),
  629. SMC_GET_GP(handle, CTX_GPREG_X15),
  630. SMC_GET_GP(handle, CTX_GPREG_X16),
  631. SMC_GET_GP(handle, CTX_GPREG_X17)
  632. );
  633. #else
  634. SMC_RET8(ctx_out, smc_fid, x1, x2, x3, x4,
  635. SMC_GET_GP(handle, CTX_GPREG_X5),
  636. SMC_GET_GP(handle, CTX_GPREG_X6),
  637. SMC_GET_GP(handle, CTX_GPREG_X7));
  638. #endif
  639. }
  640. /*******************************************************************************
  641. * Forward SMCs to the other security state.
  642. ******************************************************************************/
  643. static uint64_t spmd_smc_forward(uint32_t smc_fid,
  644. bool secure_origin,
  645. uint64_t x1,
  646. uint64_t x2,
  647. uint64_t x3,
  648. uint64_t x4,
  649. void *cookie,
  650. void *handle,
  651. uint64_t flags)
  652. {
  653. if (is_spmc_at_el3() && !secure_origin) {
  654. return spmc_smc_handler(smc_fid, secure_origin, x1, x2, x3, x4,
  655. cookie, handle, flags);
  656. }
  657. return spmd_smc_switch_state(smc_fid, secure_origin, x1, x2, x3, x4,
  658. handle, flags);
  659. }
  660. /*******************************************************************************
  661. * Return FFA_ERROR with specified error code
  662. ******************************************************************************/
  663. uint64_t spmd_ffa_error_return(void *handle, int error_code)
  664. {
  665. SMC_RET8(handle, (uint32_t) FFA_ERROR,
  666. FFA_TARGET_INFO_MBZ, (uint32_t)error_code,
  667. FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
  668. FFA_PARAM_MBZ, FFA_PARAM_MBZ);
  669. }
  670. /*******************************************************************************
  671. * spmd_check_address_in_binary_image
  672. ******************************************************************************/
  673. bool spmd_check_address_in_binary_image(uint64_t address)
  674. {
  675. assert(!check_uptr_overflow(spmc_attrs.load_address, spmc_attrs.binary_size));
  676. return ((address >= spmc_attrs.load_address) &&
  677. (address < (spmc_attrs.load_address + spmc_attrs.binary_size)));
  678. }
  679. /******************************************************************************
  680. * spmd_is_spmc_message
  681. *****************************************************************************/
  682. static bool spmd_is_spmc_message(unsigned int ep)
  683. {
  684. if (is_spmc_at_el3()) {
  685. return false;
  686. }
  687. return ((ffa_endpoint_destination(ep) == SPMD_DIRECT_MSG_ENDPOINT_ID)
  688. && (ffa_endpoint_source(ep) == spmc_attrs.spmc_id));
  689. }
  690. /*******************************************************************************
  691. * This function forwards FF-A SMCs to either the main SPMD handler or the
  692. * SPMC at EL3, depending on the origin security state, if enabled.
  693. ******************************************************************************/
  694. uint64_t spmd_ffa_smc_handler(uint32_t smc_fid,
  695. uint64_t x1,
  696. uint64_t x2,
  697. uint64_t x3,
  698. uint64_t x4,
  699. void *cookie,
  700. void *handle,
  701. uint64_t flags)
  702. {
  703. if (is_spmc_at_el3()) {
  704. /*
  705. * If we have an SPMC at EL3 allow handling of the SMC first.
  706. * The SPMC will call back through to SPMD handler if required.
  707. */
  708. if (is_caller_secure(flags)) {
  709. return spmc_smc_handler(smc_fid,
  710. is_caller_secure(flags),
  711. x1, x2, x3, x4, cookie,
  712. handle, flags);
  713. }
  714. }
  715. return spmd_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
  716. handle, flags);
  717. }
  718. /*******************************************************************************
  719. * This function handles all SMCs in the range reserved for FFA. Each call is
  720. * either forwarded to the other security state or handled by the SPM dispatcher
  721. ******************************************************************************/
  722. uint64_t spmd_smc_handler(uint32_t smc_fid,
  723. uint64_t x1,
  724. uint64_t x2,
  725. uint64_t x3,
  726. uint64_t x4,
  727. void *cookie,
  728. void *handle,
  729. uint64_t flags)
  730. {
  731. spmd_spm_core_context_t *ctx = spmd_get_context();
  732. bool secure_origin;
  733. int ret;
  734. uint32_t input_version;
  735. /* Determine which security state this SMC originated from */
  736. secure_origin = is_caller_secure(flags);
  737. VERBOSE("SPM(%u): 0x%x 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64
  738. " 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 "\n",
  739. plat_my_core_pos(), smc_fid, x1, x2, x3, x4,
  740. SMC_GET_GP(handle, CTX_GPREG_X5),
  741. SMC_GET_GP(handle, CTX_GPREG_X6),
  742. SMC_GET_GP(handle, CTX_GPREG_X7));
  743. /*
  744. * If there is an on-going info regs from EL3 SPMD LP, unconditionally
  745. * return, we don't expect any other FF-A ABIs to be called between
  746. * calls to FFA_PARTITION_INFO_GET_REGS.
  747. */
  748. if (is_spmd_logical_sp_info_regs_req_in_progress(ctx)) {
  749. assert(secure_origin);
  750. spmd_spm_core_sync_exit(0ULL);
  751. }
  752. switch (smc_fid) {
  753. case FFA_ERROR:
  754. /*
  755. * Check if this is the first invocation of this interface on
  756. * this CPU. If so, then indicate that the SPM Core initialised
  757. * unsuccessfully.
  758. */
  759. if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) {
  760. spmd_spm_core_sync_exit(x2);
  761. }
  762. /*
  763. * If there was an SPMD logical partition direct request on-going,
  764. * return back to the SPMD logical partition so the error can be
  765. * consumed.
  766. */
  767. if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
  768. assert(secure_origin);
  769. spmd_spm_core_sync_exit(0ULL);
  770. }
  771. return spmd_smc_forward(smc_fid, secure_origin,
  772. x1, x2, x3, x4, cookie,
  773. handle, flags);
  774. break; /* not reached */
  775. case FFA_VERSION:
  776. input_version = (uint32_t)(0xFFFFFFFF & x1);
  777. /*
  778. * If caller is secure and SPMC was initialized,
  779. * return FFA_VERSION of SPMD.
  780. * If caller is non secure and SPMC was initialized,
  781. * forward to the EL3 SPMC if enabled, otherwise return
  782. * the SPMC version if implemented at a lower EL.
  783. * Sanity check to "input_version".
  784. * If the EL3 SPMC is enabled, ignore the SPMC state as
  785. * this is not used.
  786. */
  787. if ((input_version & FFA_VERSION_BIT31_MASK) ||
  788. (!is_spmc_at_el3() && (ctx->state == SPMC_STATE_RESET))) {
  789. ret = FFA_ERROR_NOT_SUPPORTED;
  790. } else if (!secure_origin) {
  791. if (is_spmc_at_el3()) {
  792. /*
  793. * Forward the call directly to the EL3 SPMC, if
  794. * enabled, as we don't need to wrap the call in
  795. * a direct request.
  796. */
  797. return spmd_smc_forward(smc_fid, secure_origin,
  798. x1, x2, x3, x4, cookie,
  799. handle, flags);
  800. }
  801. gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
  802. uint64_t rc;
  803. if (spmc_attrs.major_version == 1 &&
  804. spmc_attrs.minor_version == 0) {
  805. ret = MAKE_FFA_VERSION(spmc_attrs.major_version,
  806. spmc_attrs.minor_version);
  807. SMC_RET8(handle, (uint32_t)ret,
  808. FFA_TARGET_INFO_MBZ,
  809. FFA_TARGET_INFO_MBZ,
  810. FFA_PARAM_MBZ, FFA_PARAM_MBZ,
  811. FFA_PARAM_MBZ, FFA_PARAM_MBZ,
  812. FFA_PARAM_MBZ);
  813. break;
  814. }
  815. /* Save non-secure system registers context */
  816. #if SPMD_SPM_AT_SEL2
  817. cm_el2_sysregs_context_save(NON_SECURE);
  818. #else
  819. cm_el1_sysregs_context_save(NON_SECURE);
  820. #endif
  821. /*
  822. * The incoming request has FFA_VERSION as X0 smc_fid
  823. * and requested version in x1. Prepare a direct request
  824. * from SPMD to SPMC with FFA_VERSION framework function
  825. * identifier in X2 and requested version in X3.
  826. */
  827. spmd_build_spmc_message(gpregs,
  828. SPMD_FWK_MSG_FFA_VERSION_REQ,
  829. input_version);
  830. /*
  831. * Ensure x8-x17 NS GP register values are untouched when returning
  832. * from the SPMC.
  833. */
  834. write_ctx_reg(gpregs, CTX_GPREG_X8, SMC_GET_GP(handle, CTX_GPREG_X8));
  835. write_ctx_reg(gpregs, CTX_GPREG_X9, SMC_GET_GP(handle, CTX_GPREG_X9));
  836. write_ctx_reg(gpregs, CTX_GPREG_X10, SMC_GET_GP(handle, CTX_GPREG_X10));
  837. write_ctx_reg(gpregs, CTX_GPREG_X11, SMC_GET_GP(handle, CTX_GPREG_X11));
  838. write_ctx_reg(gpregs, CTX_GPREG_X12, SMC_GET_GP(handle, CTX_GPREG_X12));
  839. write_ctx_reg(gpregs, CTX_GPREG_X13, SMC_GET_GP(handle, CTX_GPREG_X13));
  840. write_ctx_reg(gpregs, CTX_GPREG_X14, SMC_GET_GP(handle, CTX_GPREG_X14));
  841. write_ctx_reg(gpregs, CTX_GPREG_X15, SMC_GET_GP(handle, CTX_GPREG_X15));
  842. write_ctx_reg(gpregs, CTX_GPREG_X16, SMC_GET_GP(handle, CTX_GPREG_X16));
  843. write_ctx_reg(gpregs, CTX_GPREG_X17, SMC_GET_GP(handle, CTX_GPREG_X17));
  844. rc = spmd_spm_core_sync_entry(ctx);
  845. if ((rc != 0ULL) ||
  846. (SMC_GET_GP(gpregs, CTX_GPREG_X0) !=
  847. FFA_MSG_SEND_DIRECT_RESP_SMC32) ||
  848. (SMC_GET_GP(gpregs, CTX_GPREG_X2) !=
  849. (FFA_FWK_MSG_BIT |
  850. SPMD_FWK_MSG_FFA_VERSION_RESP))) {
  851. ERROR("Failed to forward FFA_VERSION\n");
  852. ret = FFA_ERROR_NOT_SUPPORTED;
  853. } else {
  854. ret = SMC_GET_GP(gpregs, CTX_GPREG_X3);
  855. }
  856. /*
  857. * x0-x4 are updated by spmd_smc_forward below.
  858. * Zero out x5-x7 in the FFA_VERSION response.
  859. */
  860. write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
  861. write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
  862. write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
  863. /*
  864. * Return here after SPMC has handled FFA_VERSION.
  865. * The returned SPMC version is held in X3.
  866. * Forward this version in X0 to the non-secure caller.
  867. */
  868. return spmd_smc_forward(ret, true, FFA_PARAM_MBZ,
  869. FFA_PARAM_MBZ, FFA_PARAM_MBZ,
  870. FFA_PARAM_MBZ, cookie, gpregs,
  871. flags);
  872. } else {
  873. ret = MAKE_FFA_VERSION(FFA_VERSION_MAJOR,
  874. FFA_VERSION_MINOR);
  875. }
  876. SMC_RET8(handle, (uint32_t)ret, FFA_TARGET_INFO_MBZ,
  877. FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
  878. FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ);
  879. break; /* not reached */
  880. case FFA_FEATURES:
  881. /*
  882. * This is an optional interface. Do the minimal checks and
  883. * forward to SPM Core which will handle it if implemented.
  884. */
  885. /* Forward SMC from Normal world to the SPM Core */
  886. if (!secure_origin) {
  887. return spmd_smc_forward(smc_fid, secure_origin,
  888. x1, x2, x3, x4, cookie,
  889. handle, flags);
  890. }
  891. /*
  892. * Return success if call was from secure world i.e. all
  893. * FFA functions are supported. This is essentially a
  894. * nop.
  895. */
  896. SMC_RET8(handle, FFA_SUCCESS_SMC32, x1, x2, x3, x4,
  897. SMC_GET_GP(handle, CTX_GPREG_X5),
  898. SMC_GET_GP(handle, CTX_GPREG_X6),
  899. SMC_GET_GP(handle, CTX_GPREG_X7));
  900. break; /* not reached */
  901. case FFA_ID_GET:
  902. /*
  903. * Returns the ID of the calling FFA component.
  904. */
  905. if (!secure_origin) {
  906. SMC_RET8(handle, FFA_SUCCESS_SMC32,
  907. FFA_TARGET_INFO_MBZ, FFA_NS_ENDPOINT_ID,
  908. FFA_PARAM_MBZ, FFA_PARAM_MBZ,
  909. FFA_PARAM_MBZ, FFA_PARAM_MBZ,
  910. FFA_PARAM_MBZ);
  911. }
  912. SMC_RET8(handle, FFA_SUCCESS_SMC32,
  913. FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
  914. FFA_PARAM_MBZ, FFA_PARAM_MBZ,
  915. FFA_PARAM_MBZ, FFA_PARAM_MBZ,
  916. FFA_PARAM_MBZ);
  917. break; /* not reached */
  918. case FFA_SECONDARY_EP_REGISTER_SMC64:
  919. if (secure_origin) {
  920. ret = spmd_pm_secondary_ep_register(x1);
  921. if (ret < 0) {
  922. SMC_RET8(handle, FFA_ERROR_SMC64,
  923. FFA_TARGET_INFO_MBZ, ret,
  924. FFA_PARAM_MBZ, FFA_PARAM_MBZ,
  925. FFA_PARAM_MBZ, FFA_PARAM_MBZ,
  926. FFA_PARAM_MBZ);
  927. } else {
  928. SMC_RET8(handle, FFA_SUCCESS_SMC64,
  929. FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ,
  930. FFA_PARAM_MBZ, FFA_PARAM_MBZ,
  931. FFA_PARAM_MBZ, FFA_PARAM_MBZ,
  932. FFA_PARAM_MBZ);
  933. }
  934. }
  935. return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
  936. break; /* Not reached */
  937. case FFA_SPM_ID_GET:
  938. if (MAKE_FFA_VERSION(1, 1) > FFA_VERSION_COMPILED) {
  939. return spmd_ffa_error_return(handle,
  940. FFA_ERROR_NOT_SUPPORTED);
  941. }
  942. /*
  943. * Returns the ID of the SPMC or SPMD depending on the FF-A
  944. * instance where this function is invoked
  945. */
  946. if (!secure_origin) {
  947. SMC_RET8(handle, FFA_SUCCESS_SMC32,
  948. FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
  949. FFA_PARAM_MBZ, FFA_PARAM_MBZ,
  950. FFA_PARAM_MBZ, FFA_PARAM_MBZ,
  951. FFA_PARAM_MBZ);
  952. }
  953. SMC_RET8(handle, FFA_SUCCESS_SMC32,
  954. FFA_TARGET_INFO_MBZ, SPMD_DIRECT_MSG_ENDPOINT_ID,
  955. FFA_PARAM_MBZ, FFA_PARAM_MBZ,
  956. FFA_PARAM_MBZ, FFA_PARAM_MBZ,
  957. FFA_PARAM_MBZ);
  958. break; /* not reached */
  959. case FFA_MSG_SEND_DIRECT_REQ_SMC32:
  960. case FFA_MSG_SEND_DIRECT_REQ_SMC64:
  961. case FFA_MSG_SEND_DIRECT_REQ2_SMC64:
  962. /*
  963. * Regardless of secure_origin, SPMD logical partitions cannot
  964. * handle direct messages. They can only initiate direct
  965. * messages and consume direct responses or errors.
  966. */
  967. if (is_spmd_lp_id(ffa_endpoint_source(x1)) ||
  968. is_spmd_lp_id(ffa_endpoint_destination(x1))) {
  969. return spmd_ffa_error_return(handle,
  970. FFA_ERROR_INVALID_PARAMETER
  971. );
  972. }
  973. /*
  974. * When there is an ongoing SPMD logical partition direct
  975. * request, there cannot be another direct request. Return
  976. * error in this case. Panic'ing is an option but that does
  977. * not provide the opportunity for caller to abort based on
  978. * error codes.
  979. */
  980. if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
  981. assert(secure_origin);
  982. return spmd_ffa_error_return(handle,
  983. FFA_ERROR_DENIED);
  984. }
  985. if (!secure_origin) {
  986. /* Validate source endpoint is non-secure for non-secure caller. */
  987. if (ffa_is_secure_world_id(ffa_endpoint_source(x1))) {
  988. return spmd_ffa_error_return(handle,
  989. FFA_ERROR_INVALID_PARAMETER);
  990. }
  991. }
  992. if (secure_origin && spmd_is_spmc_message(x1)) {
  993. return spmd_ffa_error_return(handle,
  994. FFA_ERROR_DENIED);
  995. } else {
  996. /* Forward direct message to the other world */
  997. return spmd_smc_forward(smc_fid, secure_origin,
  998. x1, x2, x3, x4, cookie,
  999. handle, flags);
  1000. }
  1001. break; /* Not reached */
  1002. case FFA_MSG_SEND_DIRECT_RESP_SMC32:
  1003. case FFA_MSG_SEND_DIRECT_RESP_SMC64:
  1004. case FFA_MSG_SEND_DIRECT_RESP2_SMC64:
  1005. if (secure_origin && (spmd_is_spmc_message(x1) ||
  1006. is_spmd_logical_sp_dir_req_in_progress(ctx))) {
  1007. spmd_spm_core_sync_exit(0ULL);
  1008. } else {
  1009. /* Forward direct message to the other world */
  1010. return spmd_smc_forward(smc_fid, secure_origin,
  1011. x1, x2, x3, x4, cookie,
  1012. handle, flags);
  1013. }
  1014. break; /* Not reached */
  1015. case FFA_RX_RELEASE:
  1016. case FFA_RXTX_MAP_SMC32:
  1017. case FFA_RXTX_MAP_SMC64:
  1018. case FFA_RXTX_UNMAP:
  1019. case FFA_PARTITION_INFO_GET:
  1020. #if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED
  1021. case FFA_NOTIFICATION_BITMAP_CREATE:
  1022. case FFA_NOTIFICATION_BITMAP_DESTROY:
  1023. case FFA_NOTIFICATION_BIND:
  1024. case FFA_NOTIFICATION_UNBIND:
  1025. case FFA_NOTIFICATION_SET:
  1026. case FFA_NOTIFICATION_GET:
  1027. case FFA_NOTIFICATION_INFO_GET:
  1028. case FFA_NOTIFICATION_INFO_GET_SMC64:
  1029. case FFA_MSG_SEND2:
  1030. case FFA_RX_ACQUIRE:
  1031. #endif
  1032. case FFA_MSG_RUN:
  1033. /*
  1034. * Above calls should be invoked only by the Normal world and
  1035. * must not be forwarded from Secure world to Normal world.
  1036. */
  1037. if (secure_origin) {
  1038. return spmd_ffa_error_return(handle,
  1039. FFA_ERROR_NOT_SUPPORTED);
  1040. }
  1041. /* Forward the call to the other world */
  1042. /* fallthrough */
  1043. case FFA_MSG_SEND:
  1044. case FFA_MEM_DONATE_SMC32:
  1045. case FFA_MEM_DONATE_SMC64:
  1046. case FFA_MEM_LEND_SMC32:
  1047. case FFA_MEM_LEND_SMC64:
  1048. case FFA_MEM_SHARE_SMC32:
  1049. case FFA_MEM_SHARE_SMC64:
  1050. case FFA_MEM_RETRIEVE_REQ_SMC32:
  1051. case FFA_MEM_RETRIEVE_REQ_SMC64:
  1052. case FFA_MEM_RETRIEVE_RESP:
  1053. case FFA_MEM_RELINQUISH:
  1054. case FFA_MEM_RECLAIM:
  1055. case FFA_MEM_FRAG_TX:
  1056. case FFA_MEM_FRAG_RX:
  1057. case FFA_SUCCESS_SMC32:
  1058. case FFA_SUCCESS_SMC64:
  1059. /*
  1060. * If there is an ongoing direct request from an SPMD logical
  1061. * partition, return an error.
  1062. */
  1063. if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
  1064. assert(secure_origin);
  1065. return spmd_ffa_error_return(handle,
  1066. FFA_ERROR_DENIED);
  1067. }
  1068. return spmd_smc_forward(smc_fid, secure_origin,
  1069. x1, x2, x3, x4, cookie,
  1070. handle, flags);
  1071. break; /* not reached */
  1072. case FFA_MSG_WAIT:
  1073. /*
  1074. * Check if this is the first invocation of this interface on
  1075. * this CPU from the Secure world. If so, then indicate that the
  1076. * SPM Core initialised successfully.
  1077. */
  1078. if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) {
  1079. spmd_spm_core_sync_exit(0ULL);
  1080. }
  1081. /* Forward the call to the other world */
  1082. /* fallthrough */
  1083. case FFA_INTERRUPT:
  1084. case FFA_MSG_YIELD:
  1085. /* This interface must be invoked only by the Secure world */
  1086. if (!secure_origin) {
  1087. return spmd_ffa_error_return(handle,
  1088. FFA_ERROR_NOT_SUPPORTED);
  1089. }
  1090. if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
  1091. assert(secure_origin);
  1092. return spmd_ffa_error_return(handle,
  1093. FFA_ERROR_DENIED);
  1094. }
  1095. return spmd_smc_forward(smc_fid, secure_origin,
  1096. x1, x2, x3, x4, cookie,
  1097. handle, flags);
  1098. break; /* not reached */
  1099. case FFA_NORMAL_WORLD_RESUME:
  1100. if (secure_origin && ctx->secure_interrupt_ongoing) {
  1101. spmd_spm_core_sync_exit(0ULL);
  1102. } else {
  1103. return spmd_ffa_error_return(handle, FFA_ERROR_DENIED);
  1104. }
  1105. break; /* Not reached */
  1106. #if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED
  1107. case FFA_PARTITION_INFO_GET_REGS_SMC64:
  1108. if (secure_origin) {
  1109. return spmd_el3_populate_logical_partition_info(handle, x1,
  1110. x2, x3);
  1111. }
  1112. /* Call only supported with SMCCC 1.2+ */
  1113. if (MAKE_SMCCC_VERSION(SMCCC_MAJOR_VERSION, SMCCC_MINOR_VERSION) < 0x10002) {
  1114. return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
  1115. }
  1116. return spmd_smc_forward(smc_fid, secure_origin,
  1117. x1, x2, x3, x4, cookie,
  1118. handle, flags);
  1119. break; /* Not reached */
  1120. #endif
  1121. case FFA_CONSOLE_LOG_SMC32:
  1122. case FFA_CONSOLE_LOG_SMC64:
  1123. /* This interface must not be forwarded to other worlds. */
  1124. return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
  1125. break; /* not reached */
  1126. case FFA_EL3_INTR_HANDLE:
  1127. if (secure_origin) {
  1128. return spmd_handle_group0_intr_swd(handle);
  1129. } else {
  1130. return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
  1131. }
  1132. default:
  1133. WARN("SPM: Unsupported call 0x%08x\n", smc_fid);
  1134. return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
  1135. }
  1136. }