Cactus: helper commands needed for interrupt testing

Following commands added
  1. CACTUS_SLEEP_CMD: request to run cactus in a busy loop for
     given time. Returns time lapsed in this routine.
  2. CACTUS_INTERRUPT_CMD: request to enable/disable given interrupt
     ID, returns success on completion.

Change-Id: I9c7903f1e483d3ea0dc91db5f07135995da77862
Signed-off-by: Manish Pandey <manish.pandey2@arm.com>
Signed-off-by: Olivier Deprez <olivier.deprez@arm.com>
diff --git a/include/runtime_services/cactus_test_cmds.h b/include/runtime_services/cactus_test_cmds.h
index 246f4f9..d03a97f 100644
--- a/include/runtime_services/cactus_test_cmds.h
+++ b/include/runtime_services/cactus_test_cmds.h
@@ -8,6 +8,7 @@
 #define CACTUS_TEST_CMDS
 
 #include <ffa_helpers.h>
+#include <spm_common.h>
 
 /**
  * Success and error return to be sent over a msg response.
@@ -249,4 +250,53 @@
 			       0);
 }
 
+/**
+ * Command to request cactus to sleep for the given time in ms
+ *
+ * The command id is the hex representation of string "sleep"
+ */
+#define CACTUS_SLEEP_CMD U(0x736c656570)
+
+static inline smc_ret_values cactus_sleep_cmd(
+	ffa_vm_id_t source, ffa_vm_id_t dest, uint32_t sleep_time)
+{
+	return cactus_send_cmd(source, dest, CACTUS_SLEEP_CMD, sleep_time, 0, 0,
+			       0);
+}
+
+static inline uint32_t cactus_get_sleep_time(smc_ret_values ret)
+{
+	return (uint32_t)ret.ret4;
+}
+
+/**
+ * Command to request cactus to enable/disable an interrupt
+ *
+ * The command id is the hex representation of string "intr"
+ */
+#define CACTUS_INTERRUPT_CMD U(0x696e7472)
+
+static inline smc_ret_values cactus_interrupt_cmd(
+	ffa_vm_id_t source, ffa_vm_id_t dest, uint32_t interrupt_id,
+	bool enable, uint32_t pin)
+{
+	return cactus_send_cmd(source, dest, CACTUS_INTERRUPT_CMD, interrupt_id,
+			       enable, pin, 0);
+}
+
+static inline uint32_t cactus_get_interrupt_id(smc_ret_values ret)
+{
+	return (uint32_t)ret.ret4;
+}
+
+static inline bool cactus_get_interrupt_enable(smc_ret_values ret)
+{
+	return (bool)ret.ret5;
+}
+
+static inline enum interrupt_pin cactus_get_interrupt_pin(smc_ret_values ret)
+{
+	return (enum interrupt_pin)ret.ret6;
+}
+
 #endif
diff --git a/spm/cactus/cactus.mk b/spm/cactus/cactus.mk
index 70961eb..a52120f 100644
--- a/spm/cactus/cactus.mk
+++ b/spm/cactus/cactus.mk
@@ -47,6 +47,7 @@
 		cactus_test_cpu_features.c		\
 		cactus_test_direct_messaging.c		\
 		cactus_test_ffa.c 			\
+		cactus_test_interrupts.c		\
 		cactus_test_memory_sharing.c		\
 	)
 
diff --git a/spm/cactus/cactus_tests/cactus_test_interrupts.c b/spm/cactus/cactus_tests/cactus_test_interrupts.c
new file mode 100644
index 0000000..b675dfc
--- /dev/null
+++ b/spm/cactus/cactus_tests/cactus_test_interrupts.c
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2021, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <common/debug.h>
+#include <sp_helpers.h>
+#include <spm_helpers.h>
+
+#include "cactus_message_loop.h"
+#include "cactus_test_cmds.h"
+
+CACTUS_CMD_HANDLER(sleep_cmd, CACTUS_SLEEP_CMD)
+{
+	uint64_t timer_freq = read_cntfrq_el0();
+	uint64_t time1, time2, time_lapsed;
+	uint32_t sleep_time = cactus_get_sleep_time(*args);
+
+	VERBOSE("Request to sleep %x for %ums.\n", ffa_dir_msg_dest(*args), sleep_time);
+
+	time1 = read_cntvct_el0();
+	sp_sleep(sleep_time);
+	time2 = read_cntvct_el0();
+
+	/* Lapsed time should be at least equal to sleep time */
+	time_lapsed = ((time2 - time1) * 1000) / timer_freq;
+
+	return cactus_response(ffa_dir_msg_dest(*args),
+			       ffa_dir_msg_source(*args),
+			       time_lapsed);
+}
+
+CACTUS_CMD_HANDLER(interrupt_cmd, CACTUS_INTERRUPT_CMD)
+{
+	uint32_t int_id = cactus_get_interrupt_id(*args);
+	bool enable = cactus_get_interrupt_enable(*args);
+	enum interrupt_pin pin = cactus_get_interrupt_pin(*args);
+	int64_t ret;
+
+	ret = spm_interrupt_enable(int_id, enable, pin);
+	if (ret != 0) {
+		return cactus_error_resp(ffa_dir_msg_dest(*args),
+					 ffa_dir_msg_source(*args),
+					 CACTUS_ERROR_TEST);
+	}
+
+	return cactus_response(ffa_dir_msg_dest(*args),
+			       ffa_dir_msg_source(*args),
+			       CACTUS_SUCCESS);
+}