blob: d0c15a5bb61f06dc64c39c1217338f86c7dc4c68 [file] [log] [blame]
Manish Pandeyd27b37d2021-03-02 14:41:58 +00001/*
Madhukar Pappireddy4d1f1122023-03-16 17:54:24 -05002 * Copyright (c) 2021-2023, Arm Limited. All rights reserved.
Manish Pandeyd27b37d2021-03-02 14:41:58 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <debug.h>
8
Madhukar Pappireddycd183ef2021-08-05 15:34:07 -05009#include "cactus_message_loop.h"
10#include "cactus_test_cmds.h"
11#include <drivers/arm/sp805.h>
Manish Pandeyd27b37d2021-03-02 14:41:58 +000012#include <ffa_helpers.h>
13#include <sp_helpers.h>
Madhukar Pappireddycd183ef2021-08-05 15:34:07 -050014#include "spm_common.h"
Manish Pandey58971b62020-09-21 21:10:38 +010015#include <spm_helpers.h>
Manish Pandeyd27b37d2021-03-02 14:41:58 +000016
Madhukar Pappireddycd183ef2021-08-05 15:34:07 -050017#include <platform_def.h>
Manish Pandeyf7aafef2021-03-03 11:31:47 +000018
J-Alvesa076d4c2021-10-19 16:06:15 +010019#define NOTIFICATION_PENDING_INTERRUPT_INTID 5
20
21extern void notification_pending_interrupt_handler(void);
22
Daniel Boulbye79d2072021-03-03 11:34:53 +000023extern ffa_id_t g_ffa_id;
Madhukar Pappireddye9c90932022-06-22 17:15:45 -050024extern ffa_id_t g_dir_req_source_id;
Madhukar Pappireddyca1e2012022-06-22 17:05:09 -050025static uint32_t managed_exit_interrupt_id;
Manish Pandeyf7aafef2021-03-03 11:31:47 +000026
Madhukar Pappireddyf4c8e8d2022-02-15 15:52:53 -060027/* Secure virtual interrupt that was last handled by Cactus SP. */
28uint32_t last_serviced_interrupt[PLATFORM_CORE_COUNT];
29
Madhukar Pappireddy7caaa4a2022-01-28 17:01:35 -060030extern spinlock_t sp_handler_lock[NUM_VINT_ID];
31
Madhukar Pappireddyca1e2012022-06-22 17:05:09 -050032/*
33 * Managed exit ID discoverable by querying the SPMC through
34 * FFA_FEATURES API.
35 */
36void discover_managed_exit_interrupt_id(void)
37{
38 struct ffa_value ffa_ret;
39
40 /* Interrupt ID value is returned through register W2. */
41 ffa_ret = ffa_features(FFA_FEATURE_MEI);
42 managed_exit_interrupt_id = ffa_feature_intid(ffa_ret);
43
44 VERBOSE("Discovered managed exit interrupt ID: %d\n",
45 managed_exit_interrupt_id);
46}
47
Madhukar Pappireddy92d2e812022-09-19 13:48:13 -050048/*
49 * Cactus SP does not implement application threads. Hence, once the Cactus SP
50 * sends the managed exit response to the direct request originator, execution
51 * is still frozen in interrupt handler context.
52 * Though it moves to WAITING state, it is not able to accept new direct request
53 * message from any endpoint. It can only receive a direct request message with
54 * the command CACTUS_RESUME_AFTER_MANAGED_EXIT from the originator of the
55 * suspended direct request message in order to return from the interrupt
56 * handler context and resume the processing of suspended request.
57 */
58void send_managed_exit_response(void)
59{
60 struct ffa_value ffa_ret;
61 bool waiting_resume_after_managed_exit;
62
Madhukar Pappireddy4d1f1122023-03-16 17:54:24 -050063 /*
64 * A secure partition performs its housekeeping and sends a direct
65 * response to signal interrupt completion. This is a pure virtual
66 * interrupt, no need for deactivation.
67 */
Madhukar Pappireddy92d2e812022-09-19 13:48:13 -050068 ffa_ret = cactus_response(g_ffa_id, g_dir_req_source_id,
69 MANAGED_EXIT_INTERRUPT_ID);
70 waiting_resume_after_managed_exit = true;
71
72 while (waiting_resume_after_managed_exit) {
73
74 waiting_resume_after_managed_exit =
75 (ffa_func_id(ffa_ret) != FFA_MSG_SEND_DIRECT_REQ_SMC32 &&
76 ffa_func_id(ffa_ret) != FFA_MSG_SEND_DIRECT_REQ_SMC64) ||
77 ffa_dir_msg_source(ffa_ret) != g_dir_req_source_id ||
78 cactus_get_cmd(ffa_ret) != CACTUS_RESUME_AFTER_MANAGED_EXIT;
79
80 if (waiting_resume_after_managed_exit) {
Madhukar Pappireddycf85f352022-10-12 18:00:17 -050081 VERBOSE("Expected a direct message request from endpoint"
Madhukar Pappireddy92d2e812022-09-19 13:48:13 -050082 " %x with command CACTUS_RESUME_AFTER_MANAGED_EXIT\n",
83 g_dir_req_source_id);
84 ffa_ret = cactus_error_resp(g_ffa_id,
85 ffa_dir_msg_source(ffa_ret),
86 CACTUS_ERROR_TEST);
87 }
88 }
89 VERBOSE("Resuming the suspended command\n");
90}
91
Madhukar Pappireddye9c18122024-09-10 16:28:48 -050092static void timer_interrupt_handler(void)
93{
94 /* Disable the EL1 physical arch timer. */
95 write_cntp_ctl_el0(0);
96
97 spm_interrupt_deactivate(TIMER_VIRTUAL_INTID);
98 NOTICE("serviced el1 physical timer\n");
99}
100
Madhukar Pappireddy4d1f1122023-03-16 17:54:24 -0500101void register_maintenance_interrupt_handlers(void)
102{
103 sp_register_interrupt_handler(send_managed_exit_response,
104 managed_exit_interrupt_id);
105 sp_register_interrupt_handler(notification_pending_interrupt_handler,
106 NOTIFICATION_PENDING_INTERRUPT_INTID);
Madhukar Pappireddye9c18122024-09-10 16:28:48 -0500107 sp_register_interrupt_handler(timer_interrupt_handler,
108 TIMER_VIRTUAL_INTID);
Madhukar Pappireddy4d1f1122023-03-16 17:54:24 -0500109}
110
Madhukar Pappireddye9c90932022-06-22 17:15:45 -0500111void cactus_interrupt_handler_irq(void)
112{
113 uint32_t intid = spm_interrupt_get();
Madhukar Pappireddye9c18122024-09-10 16:28:48 -0500114 unsigned int core_pos = spm_get_my_core_pos();
Madhukar Pappireddye9c90932022-06-22 17:15:45 -0500115
Madhukar Pappireddy4d1f1122023-03-16 17:54:24 -0500116 last_serviced_interrupt[core_pos] = intid;
117
118 /* Invoke the handler registered by the SP. */
119 spin_lock(&sp_handler_lock[intid]);
120 if (sp_interrupt_handler[intid]) {
121 sp_interrupt_handler[intid]();
Madhukar Pappireddye9c90932022-06-22 17:15:45 -0500122 } else {
Madhukar Pappireddy4d1f1122023-03-16 17:54:24 -0500123 ERROR("%s: Interrupt ID %x not handled!\n", __func__, intid);
124 panic();
Madhukar Pappireddye9c90932022-06-22 17:15:45 -0500125 }
Madhukar Pappireddy4d1f1122023-03-16 17:54:24 -0500126 spin_unlock(&sp_handler_lock[intid]);
Madhukar Pappireddye9c90932022-06-22 17:15:45 -0500127}
128
129void cactus_interrupt_handler_fiq(void)
130{
131 uint32_t intid = spm_interrupt_get();
Madhukar Pappireddye9c18122024-09-10 16:28:48 -0500132 unsigned int core_pos = spm_get_my_core_pos();
Madhukar Pappireddye9c90932022-06-22 17:15:45 -0500133
Madhukar Pappireddy4d1f1122023-03-16 17:54:24 -0500134 last_serviced_interrupt[core_pos] = intid;
135
136 if (intid == MANAGED_EXIT_INTERRUPT_ID) {
Madhukar Pappireddye9c90932022-06-22 17:15:45 -0500137 /*
138 * A secure partition performs its housekeeping and sends a
139 * direct response to signal interrupt completion.
140 * This is a pure virtual interrupt, no need for deactivation.
141 */
142 VERBOSE("vFIQ: Sending ME response to %x\n",
143 g_dir_req_source_id);
Madhukar Pappireddy92d2e812022-09-19 13:48:13 -0500144 send_managed_exit_response();
Madhukar Pappireddy4d1f1122023-03-16 17:54:24 -0500145 } else {
Madhukar Pappireddye9c90932022-06-22 17:15:45 -0500146 /*
147 * Currently only managed exit interrupt is supported by vFIQ.
148 */
149 panic();
Madhukar Pappireddye9c90932022-06-22 17:15:45 -0500150 }
Madhukar Pappireddye9c90932022-06-22 17:15:45 -0500151}