Remove OP-TEE dependency on trace functions

Introducing a custom trace component which replaces the one that comes
from the SP dev kit. The new component provides the same DMSG, IMSG,
EMSG macros for printing trace messages with different levels.

Signed-off-by: Imre Kis <imre.kis@arm.com>
Change-Id: I5f9466c5a32fefeb9ef350b179dc22ee33324f7e
diff --git a/components/common/trace/component.cmake b/components/common/trace/component.cmake
new file mode 100644
index 0000000..c591832
--- /dev/null
+++ b/components/common/trace/component.cmake
@@ -0,0 +1,20 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+if (NOT DEFINED TGT)
+	message(FATAL_ERROR "mandatory parameter TGT is not defined.")
+endif()
+
+
+target_include_directories(${TGT}
+	PUBLIC
+		"${CMAKE_CURRENT_LIST_DIR}/include"
+	)
+
+target_sources(${TGT} PRIVATE
+	"${CMAKE_CURRENT_LIST_DIR}/trace.c"
+	)
diff --git a/components/common/trace/include/trace.h b/components/common/trace/include/trace.h
new file mode 100644
index 0000000..f89411b
--- /dev/null
+++ b/components/common/trace/include/trace.h
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ */
+
+#ifndef TRACE_H_
+#define TRACE_H_
+
+#include "compiler.h"
+
+#define TRACE_LEVEL_NONE	(0)
+#define TRACE_LEVEL_ERROR	(1)
+#define TRACE_LEVEL_INFO	(2)
+#define TRACE_LEVEL_DEBUG	(3)
+
+#ifndef TRACE_LEVEL
+#define TRACE_LEVEL	TRACE_LEVEL_ERROR
+#endif /* TRACE_LEVEL */
+
+/**
+ * no_trace_printf will be optimized out becase of the 'if (0)' but all the
+ * checks will still run against the format string and the parameters.
+ */
+#define no_trace_printf(func, line, level, fmt, ...)				\
+	do {									\
+		if (0) {							\
+			trace_printf(func, line, level, fmt, ##__VA_ARGS__);	\
+		}								\
+	} while (0)
+
+void trace_puts(const char *str);
+void trace_printf(const char *func, int line, int level, const char *fmt, ...) __printf(4, 5);
+
+#if TRACE_LEVEL >= TRACE_LEVEL_ERROR
+#define EMSG(...)	trace_printf(__func__, __LINE__, TRACE_LEVEL_ERROR, __VA_ARGS__)
+#else
+#define EMSG(...)	no_trace_printf(__func__, __LINE__, TRACE_LEVEL_ERROR, __VA_ARGS__)
+#endif /* TRACE_LEVEL >= TRACE_LEVEL_ERROR */
+
+#if TRACE_LEVEL >= TRACE_LEVEL_INFO
+#define IMSG(...)	trace_printf(__func__, __LINE__, TRACE_LEVEL_INFO, __VA_ARGS__)
+#else
+#define IMSG(...)	no_trace_printf(__func__, __LINE__, TRACE_LEVEL_INFO, __VA_ARGS__)
+#endif /* TRACE_LEVEL >= TRACE_LEVEL_INFO */
+
+#if TRACE_LEVEL >= TRACE_LEVEL_DEBUG
+#define DMSG(...)	trace_printf(__func__, __LINE__, TRACE_LEVEL_DEBUG, __VA_ARGS__)
+#else
+#define DMSG(...)	no_trace_printf(__func__, __LINE__, TRACE_LEVEL_DEBUG, __VA_ARGS__)
+#endif /* TRACE_LEVEL >= TRACE_LEVEL_DEBUG */
+
+#endif /* TRACE_H_ */
diff --git a/components/common/trace/trace.c b/components/common/trace/trace.c
new file mode 100644
index 0000000..bc7f0a9
--- /dev/null
+++ b/components/common/trace/trace.c
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: BSD-3-Clause
+/*
+ * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ */
+
+#include "trace.h"
+#include <stdarg.h>
+#include <stdio.h>
+
+#if TRACE_LEVEL >= TRACE_LEVEL_ERROR
+#ifndef TRACE_PREFIX
+#error TRACE_PREFIX must be defined
+#endif /* TRACE_PREFIX */
+
+void trace_printf(const char *func, int line, int level, const char *fmt, ...)
+{
+	char buffer[256];
+	char level_char = 0;
+	int offset = 0;
+	va_list ap;
+	static const char levels[] = {'E', 'I', 'D'};
+
+	if (TRACE_LEVEL_ERROR <= level && TRACE_LEVEL_DEBUG >= level)
+		level_char = levels[level - TRACE_LEVEL_ERROR];
+	else
+		level_char = '?';
+
+	offset = snprintf(buffer, sizeof(buffer), "%c/" TRACE_PREFIX ": %s:%d ",
+			  level_char, func, line);
+
+	if (offset < sizeof(buffer)) {
+		va_start(ap, fmt);
+		vsnprintf(buffer + offset, sizeof(buffer) - offset, fmt, ap);
+		va_end(ap);
+	}
+
+	trace_puts(buffer);
+}
+#endif