Merge changes I97786b89,I38e8c5c9,Ia049f20f

* changes:
  cactus: select different stdout device at runtime
  pl011: allow alternate stdout to be used
  cactus: do not compile irrelevant test files
diff --git a/drivers/arm/pl011/aarch32/pl011_console.S b/drivers/arm/pl011/aarch32/pl011_console.S
index c65c4f6..b201f04 100644
--- a/drivers/arm/pl011/aarch32/pl011_console.S
+++ b/drivers/arm/pl011/aarch32/pl011_console.S
@@ -10,7 +10,7 @@
 #include <drivers/console.h>
 
 	.globl	console_init
-	.globl	console_putc
+	.globl	console_pl011_putc
 	.globl	console_getc
 	.globl	console_try_getc
 	.globl	console_flush
@@ -97,17 +97,19 @@
 	bx	lr
 endfunc console_core_init
 
-	/* ---------------------------------------------
-	 * int console_putc(int c)
+	/* -------------------------------------------------
+	 * To allow alternate implementation of putc, pl011
+	 * is appended in the function name.
+	 * int console_pl011_putc(int c)
 	 *
 	 * Clobber list : r1, r2
-	 * ---------------------------------------------
+	 * -------------------------------------------------
 	 */
-func console_putc
+func console_pl011_putc
 	ldr	r1, =console_base
 	ldr	r1, [r1]
 	b	console_core_putc
-endfunc console_putc
+endfunc console_pl011_putc
 
 	/* --------------------------------------------------------
 	 * int console_core_putc(int c, uintptr_t base_addr)
diff --git a/drivers/arm/pl011/aarch64/pl011_console.S b/drivers/arm/pl011/aarch64/pl011_console.S
index 8b1e062..0d607b9 100644
--- a/drivers/arm/pl011/aarch64/pl011_console.S
+++ b/drivers/arm/pl011/aarch64/pl011_console.S
@@ -10,7 +10,7 @@
 #include <drivers/console.h>
 
 	.globl	console_init
-	.globl	console_putc
+	.globl	console_pl011_putc
 	.globl	console_getc
 	.globl	console_try_getc
 	.globl	console_flush
@@ -93,17 +93,20 @@
 	ret
 endfunc console_core_init
 
-	/* ---------------------------------------------
-	 * int console_putc(int c)
+	/* -------------------------------------------------
+	 * To allow alternate implementation of putc, pl011
+	 * is appended in the function name.
+	 *
+	 * int console_pl011_putc(int c)
 	 *
 	 * Clobber list : x1, x2
-	 * ---------------------------------------------
+	 * -------------------------------------------------
 	 */
-func console_putc
+func console_pl011_putc
 	adrp	x1, console_base
 	ldr	x1, [x1, :lo12:console_base]
 	b	console_core_putc
-endfunc console_putc
+endfunc console_pl011_putc
 
 	/* ---------------------------------------------
 	 * int console_core_putc(int c, uintptr_t base_addr)
diff --git a/drivers/console/console.c b/drivers/console/console.c
new file mode 100644
index 0000000..b2bae28
--- /dev/null
+++ b/drivers/console/console.c
@@ -0,0 +1,12 @@
+/*
+ * Copyright (c) 2020, ARM Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <drivers/arm/pl011.h>
+
+int console_putc(int c)
+{
+	return console_pl011_putc(c);
+}
diff --git a/include/drivers/arm/pl011.h b/include/drivers/arm/pl011.h
index cba325d..3e19ee8 100644
--- a/include/drivers/arm/pl011.h
+++ b/include/drivers/arm/pl011.h
@@ -75,4 +75,13 @@
 /* Constants */
 #define PL011_BAUDRATE		115200
 
+#ifndef __ASSEMBLER__
+#include <stdint.h>
+
+/* Functions */
+
+int console_pl011_putc(int);
+
+#endif /* __ASSEMBLER__ */
+
 #endif	/* __PL011_H__ */
diff --git a/plat/arm/common/arm_common.mk b/plat/arm/common/arm_common.mk
index 73b4690..b146654 100644
--- a/plat/arm/common/arm_common.mk
+++ b/plat/arm/common/arm_common.mk
@@ -8,6 +8,7 @@
 
 PLAT_SOURCES	+=	drivers/arm/gic/gic_common.c			\
 			drivers/arm/pl011/${ARCH}/pl011_console.S	\
+			drivers/console/console.c			\
 			plat/arm/common/arm_setup.c			\
 			plat/arm/common/arm_timers.c
 
diff --git a/plat/hisilicon/hikey960/platform.mk b/plat/hisilicon/hikey960/platform.mk
index 4bc1626..20b42df 100644
--- a/plat/hisilicon/hikey960/platform.mk
+++ b/plat/hisilicon/hikey960/platform.mk
@@ -17,6 +17,7 @@
 			drivers/arm/gic/arm_gic_v2.c			\
 			drivers/arm/timer/system_timer.c		\
 			drivers/arm/timer/private_timer.c		\
+			drivers/console/console.c			\
 			plat/arm/common/arm_timers.c
 
 TFTF_CFLAGS		+= -Wno-maybe-uninitialized
diff --git a/spm/cactus/cactus.h b/spm/cactus/cactus.h
index 0b06eb2..cbf2dcb 100644
--- a/spm/cactus/cactus.h
+++ b/spm/cactus/cactus.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017, Arm Limited. All rights reserved.
+ * Copyright (c) 2017-2020, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -26,4 +26,11 @@
 #define CACTUS_BSS_START	((uintptr_t)&__BSS_START__)
 #define CACTUS_BSS_END		((uintptr_t)&__BSS_END__)
 
+enum stdout_route {
+	PL011_AS_STDOUT = 0,
+	HVC_CALL_AS_STDOUT,
+};
+
+void set_putc_impl(enum stdout_route);
+
 #endif /* __CACTUS_H__ */
diff --git a/spm/cactus/cactus.mk b/spm/cactus/cactus.mk
index c0b98f4..a32d3d3 100644
--- a/spm/cactus/cactus.mk
+++ b/spm/cactus/cactus.mk
@@ -4,7 +4,6 @@
 # SPDX-License-Identifier: BSD-3-Clause
 #
 
-include lib/sprt/sprt_client.mk
 include lib/xlat_tables_v2/xlat_tables.mk
 
 CACTUS_DTB	:= $(BUILD_PLAT)/cactus.dtb
@@ -16,22 +15,17 @@
 	-Iinclude/common/${ARCH}			\
 	-Iinclude/lib					\
 	-Iinclude/lib/${ARCH}				\
-	-Iinclude/lib/sprt				\
 	-Iinclude/lib/utils				\
 	-Iinclude/lib/xlat_tables			\
 	-Iinclude/runtime_services			\
-	-Iinclude/runtime_services/secure_el0_payloads	\
 	-Ispm/cactus					\
 	-Ispm/common					\
-	${SPRT_LIB_INCLUDES}
 
 CACTUS_SOURCES	:=					\
 	$(addprefix spm/cactus/,			\
 		aarch64/cactus_entrypoint.S		\
+		cactus_debug.c				\
 		cactus_main.c				\
-		cactus_tests_memory_attributes.c	\
-		cactus_tests_misc.c			\
-		cactus_tests_system_setup.c		\
 	)						\
 	$(addprefix spm/common/,			\
 		aarch64/sp_arch_helpers.S		\
@@ -51,7 +45,6 @@
 			lib/smc/${ARCH}/hvc.c				\
 			lib/locks/${ARCH}/spinlock.S			\
 			lib/utils/mp_printf.c				\
-			${SPRT_LIB_SOURCES}				\
 			${XLAT_TABLES_LIB_SRCS}
 
 CACTUS_LINKERFILE	:=	spm/cactus/cactus.ld.S
diff --git a/spm/cactus/cactus_debug.c b/spm/cactus/cactus_debug.c
new file mode 100644
index 0000000..cce0973
--- /dev/null
+++ b/spm/cactus/cactus_debug.c
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2020, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <drivers/arm/pl011.h>
+#include <drivers/console.h>
+
+#include "cactus.h"
+#include "spci_helpers.h"
+
+static int (*putc_impl)(int);
+
+static int putc_hypcall(int c)
+{
+	spm_debug_log((char)c);
+
+	return c;
+}
+
+static int putc_uart(int c)
+{
+	console_pl011_putc(c);
+
+	return c;
+}
+
+void set_putc_impl(enum stdout_route route)
+{
+	switch (route) {
+
+	case HVC_CALL_AS_STDOUT:
+		putc_impl = putc_hypcall;
+		return;
+
+	case PL011_AS_STDOUT:
+	default:
+		break;
+	}
+
+	putc_impl = putc_uart;
+}
+
+int console_putc(int c)
+{
+	if (!putc_impl) {
+		return -1;
+	}
+
+	return putc_impl(c);
+}
diff --git a/spm/cactus/cactus_def.h b/spm/cactus/cactus_def.h
index e865036..6eceb01 100644
--- a/spm/cactus/cactus_def.h
+++ b/spm/cactus/cactus_def.h
@@ -32,29 +32,4 @@
 #define CACTUS_TEST_MEM_BASE		(CACTUS_NS_BUF_BASE + CACTUS_NS_BUF_SIZE)
 #define CACTUS_TEST_MEM_SIZE		ULL(0x20000)
 
-/*
- * UUIDs of Secure Services provided by Cactus
- */
-
-#define CACTUS_SERVICE1_UUID	U(0x01234567), U(0x89ABCDEF), U(0x76543210), U(0xFEDCBA98)
-#define CACTUS_SERVICE2_UUID	U(0x0A1B2C3D), U(0x4E5F6789), U(0x55AA00FF), U(0x0F1E2D3C)
-#define CACTUS_INVALID_UUID	U(0x1), U(0x2), U(0x3), U(0x4)
-
-#define CACTUS_SERVICE1_UUID_RD	U(0x01234567) U(0x89ABCDEF) U(0x76543210) U(0xFEDCBA98)
-#define CACTUS_SERVICE2_UUID_RD	U(0x0A1B2C3D) U(0x4E5F6789) U(0x55AA00FF) U(0x0F1E2D3C)
-#define CACTUS_INVALID_UUID_RD	U(0x1) U(0x2) U(0x3) U(0x4)
-
-/*
- * Service IDs
- */
-
-/* Print a magic number unique to Cactus and return */
-#define CACTUS_PRINT_MAGIC		U(1)
-/* Return a magic number unique to Cactus */
-#define CACTUS_GET_MAGIC		U(2)
-/* Sleep for a number of milliseconds */
-#define CACTUS_SLEEP_MS			U(3)
-
-#define CACTUS_MAGIC_NUMBER		U(0x12481369)
-
 #endif /* CACTUS_DEF_H */
diff --git a/spm/cactus/cactus_main.c b/spm/cactus/cactus_main.c
index c2c8ec2..be137ad 100644
--- a/spm/cactus/cactus_main.c
+++ b/spm/cactus/cactus_main.c
@@ -5,7 +5,6 @@
  */
 
 #include <assert.h>
-#include <cactus_def.h>
 #include <debug.h>
 #include <drivers/arm/pl011.h>
 #include <drivers/console.h>
@@ -16,112 +15,16 @@
 #include <plat_arm.h>
 #include <plat/common/platform.h>
 #include <platform_def.h>
-#include <sp_helpers.h>
-#include <spci_svc.h>
 #include <std_svc.h>
 
 #include "cactus.h"
 #include "cactus_def.h"
-#include "cactus_tests.h"
-
-#include "tftf_lib.h"
-
-#define SPM_VM_ID_FIRST			(1)
-
-#define SPM_VM_GET_COUNT		(0xFF01)
-#define SPM_VCPU_GET_COUNT		(0xFF02)
-#define SPM_DEBUG_LOG			(0xBD000000)
-
-/* Hypervisor ID at physical SPCI instance */
-#define HYP_ID		(0)
-
-/* By convention, SP IDs (as opposed to VM IDs) have bit 15 set */
-#define SP_ID(x)	(x | (1 << 15))
-
-typedef unsigned short spci_vm_id_t;
-typedef unsigned short spci_vm_count_t;
-typedef unsigned short spci_vcpu_count_t;
+#include "spci_helpers.h"
 
 /* Host machine information injected by the build system in the ELF file. */
 extern const char build_message[];
 extern const char version_string[];
 
-static spci_vcpu_count_t spm_vcpu_get_count(spci_vm_id_t vm_id)
-{
-	hvc_args args = {
-		.fid = SPM_VCPU_GET_COUNT,
-		.arg1 = vm_id
-	};
-
-	hvc_ret_values ret = tftf_hvc(&args);
-
-	return ret.ret0;
-}
-
-static spci_vm_count_t spm_vm_get_count(void)
-{
-	hvc_args args = {
-		.fid = SPM_VM_GET_COUNT
-	};
-
-	hvc_ret_values ret = tftf_hvc(&args);
-
-	return ret.ret0;
-}
-
-static void spm_debug_log(char c)
-{
-	hvc_args args = {
-		.fid = SPM_DEBUG_LOG,
-		.arg1 = c
-	};
-
-	(void)tftf_hvc(&args);
-}
-
-static smc_ret_values spci_id_get(void)
-{
-	smc_args args = {
-		.fid = SPCI_ID_GET
-	};
-
-	return tftf_smc(&args);
-}
-
-static smc_ret_values spci_msg_wait(void)
-{
-	smc_args args = {
-		.fid = SPCI_MSG_WAIT
-	};
-
-	return tftf_smc(&args);
-}
-
-/* Send response through registers using direct messaging */
-static smc_ret_values spci_msg_send_direct_resp(spci_vm_id_t sender_vm_id,
-					 spci_vm_id_t target_vm_id,
-					 uint32_t message)
-{
-	smc_args args = {
-		.fid = SPCI_MSG_SEND_DIRECT_RESP_SMC32,
-		.arg1 = ((uint32_t)sender_vm_id << 16) | target_vm_id,
-		.arg3 = message
-	};
-
-	return tftf_smc(&args);
-}
-
-static smc_ret_values spci_error(int32_t error_code)
-{
-	smc_args args = {
-		.fid = SPCI_ERROR,
-		.arg1 = 0,
-		.arg2 = error_code
-	};
-
-	return tftf_smc(&args);
-}
-
 /*
  *
  * Message loop function
@@ -252,36 +155,32 @@
 	}
 
 	spci_vm_id_t spci_id = spci_id_ret.ret2 & 0xffff;
-	if (spci_id > SPM_VM_ID_FIRST) {
-		/* Indicate secondary VM start through debug log hypercall */
-		spm_debug_log('2');
-		spm_debug_log('N');
-		spm_debug_log('D');
-		spm_debug_log('\n');
 
-		/* Run straight to the message loop */
-		message_loop(spci_id);
+	if (spci_id == SPM_VM_ID_FIRST) {
+		console_init(PL011_UART2_BASE,
+			PL011_UART2_CLK_IN_HZ,
+			PL011_BAUDRATE);
+
+		set_putc_impl(PL011_AS_STDOUT);
+
+		NOTICE("Booting Primary Cactus Secure Partition\n%s\n%s\n",
+			build_message, version_string);
+
+		cactus_print_memory_layout();
+
+		NOTICE("SPCI id: %u\n", spci_id); /* Expect VM id 1 */
+
+		/* Get number of VMs */
+		NOTICE("VM count: %u\n", spm_vm_get_count());
+
+		/* Get virtual CPU count for current VM */
+		NOTICE("vCPU count: %u\n", spm_vcpu_get_count(spci_id));
+	} else {
+		set_putc_impl(HVC_CALL_AS_STDOUT);
+
+		NOTICE("Booting Secondary Cactus Secure Partition\n%s\n%s\n",
+			build_message, version_string);
 	}
-
-	/* Next initialization steps only performed by primary VM */
-
-	console_init(PL011_UART2_BASE,
-		     PL011_UART2_CLK_IN_HZ,
-		     PL011_BAUDRATE);
-
-	NOTICE("Booting Cactus Secure Partition\n%s\n%s\n",
-	       build_message, version_string);
-
-	cactus_print_memory_layout();
-
-	NOTICE("SPCI id: %u\n", spci_id); /* Expect VM id 1 */
-
-	/* Get number of VMs */
-	NOTICE("VM count: %u\n", spm_vm_get_count());
-
-	/* Get virtual CPU count for current VM */
-	NOTICE("vCPU count: %u\n", spm_vcpu_get_count(spci_id));
-
 	/* End up to message loop */
 	message_loop(spci_id);
 
diff --git a/spm/cactus/spci_helpers.h b/spm/cactus/spci_helpers.h
new file mode 100644
index 0000000..189df9a
--- /dev/null
+++ b/spm/cactus/spci_helpers.h
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2020, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef __SPCI_HELPERS_H__
+#define __SPCI_HELPERS_H__
+
+
+#include <spci_svc.h>
+#include "tftf_lib.h"
+
+#define SPM_VM_ID_FIRST                 (1)
+
+#define SPM_VM_GET_COUNT                (0xFF01)
+#define SPM_VCPU_GET_COUNT              (0xFF02)
+#define SPM_DEBUG_LOG                   (0xBD000000)
+
+/* Hypervisor ID at physical SPCI instance */
+#define HYP_ID          (0)
+
+/* By convention, SP IDs (as opposed to VM IDs) have bit 15 set */
+#define SP_ID(x)        ((x) | (1 << 15))
+
+typedef unsigned short spci_vm_id_t;
+typedef unsigned short spci_vm_count_t;
+typedef unsigned short spci_vcpu_count_t;
+
+/* Functions */
+
+static inline spci_vcpu_count_t spm_vcpu_get_count(spci_vm_id_t vm_id)
+{
+	hvc_args args = {
+		.fid = SPM_VCPU_GET_COUNT,
+		.arg1 = vm_id
+	};
+
+	hvc_ret_values ret = tftf_hvc(&args);
+
+	return ret.ret0;
+}
+
+static inline spci_vm_count_t spm_vm_get_count(void)
+{
+	hvc_args args = {
+		.fid = SPM_VM_GET_COUNT
+	};
+
+	hvc_ret_values ret = tftf_hvc(&args);
+
+	return ret.ret0;
+}
+
+static inline void spm_debug_log(char c)
+{
+	hvc_args args = {
+		.fid = SPM_DEBUG_LOG,
+		.arg1 = c
+	};
+
+	(void)tftf_hvc(&args);
+}
+
+static inline smc_ret_values spci_id_get(void)
+{
+	smc_args args = {
+		.fid = SPCI_ID_GET
+	};
+
+	return tftf_smc(&args);
+}
+
+static inline smc_ret_values spci_msg_wait(void)
+{
+	smc_args args = {
+		.fid = SPCI_MSG_WAIT
+	};
+
+	return tftf_smc(&args);
+}
+
+/* Send response through registers using direct messaging */
+static inline smc_ret_values spci_msg_send_direct_resp(spci_vm_id_t sender_vm_id,
+						spci_vm_id_t target_vm_id,
+						uint32_t message)
+{
+	smc_args args = {
+		.fid = SPCI_MSG_SEND_DIRECT_RESP_SMC32,
+		.arg1 = ((uint32_t)sender_vm_id << 16) | target_vm_id,
+		.arg3 = message
+	};
+
+	return tftf_smc(&args);
+}
+
+static inline smc_ret_values spci_error(int32_t error_code)
+{
+	smc_args args = {
+		.fid = SPCI_ERROR,
+		.arg1 = 0,
+		.arg2 = error_code
+	};
+
+	return tftf_smc(&args);
+}
+
+#endif /* __SPCI_HELPERS_H__ */
diff --git a/spm/cactus_mm/cactus_mm.mk b/spm/cactus_mm/cactus_mm.mk
index c76890d..b96580c 100644
--- a/spm/cactus_mm/cactus_mm.mk
+++ b/spm/cactus_mm/cactus_mm.mk
@@ -39,6 +39,7 @@
 
 CACTUS_MM_SOURCES	+=				\
 	drivers/arm/pl011/${ARCH}/pl011_console.S	\
+	drivers/console/console.c			\
 	lib/${ARCH}/cache_helpers.S			\
 	lib/${ARCH}/misc_helpers.S			\
 
diff --git a/spm/ivy/ivy.mk b/spm/ivy/ivy.mk
index 16f62ad..8d5475a 100644
--- a/spm/ivy/ivy.mk
+++ b/spm/ivy/ivy.mk
@@ -39,6 +39,7 @@
 	tftf/framework/${ARCH}/asm_debug.S
 
 IVY_SOURCES	+= 	drivers/arm/pl011/${ARCH}/pl011_console.S	\
+			drivers/console/console.c			\
 			lib/${ARCH}/cache_helpers.S			\
 			lib/${ARCH}/misc_helpers.S			\
 			lib/locks/${ARCH}/spinlock.S			\