Add generic RPC layer

Adds generic RPC caller and endpoint. This is the base for specialized
RPC communication using e.g. FF-A messaging. The protocol description
is implemented in two flavours: packed C structures and protobuf.

Change-Id: I17f450b7272bd4008fc20fe8f716a253f6754000
Signed-off-by: Julian Hall <julian.hall@arm.com>
diff --git a/components/rpc/common/caller/component.cmake b/components/rpc/common/caller/component.cmake
new file mode 100644
index 0000000..9cb5138
--- /dev/null
+++ b/components/rpc/common/caller/component.cmake
@@ -0,0 +1,13 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2020, 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_sources(${TGT} PRIVATE
+	"${CMAKE_CURRENT_LIST_DIR}/rpc_caller.c"
+	)
diff --git a/components/rpc/common/caller/rpc_caller.c b/components/rpc/common/caller/rpc_caller.c
new file mode 100644
index 0000000..4fd1e5a
--- /dev/null
+++ b/components/rpc/common/caller/rpc_caller.c
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2020, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <rpc_caller.h>
+#include <stdint.h>
+
+rpc_call_handle rpc_caller_begin(struct rpc_caller *s,
+								uint8_t **req_buf, size_t req_len)
+{
+	return s->call_begin(s->context, req_buf, req_len);
+}
+
+rpc_status_t rpc_caller_invoke(struct rpc_caller *s, rpc_call_handle handle,
+			uint32_t opcode, int *opstatus, uint8_t **resp_buf, size_t *resp_len)
+{
+	return s->call_invoke(s->context, handle, opcode, opstatus, resp_buf, resp_len);
+}
+
+void rpc_caller_end(struct rpc_caller *s, rpc_call_handle handle)
+{
+	s->call_end(s->context, handle);
+}
diff --git a/components/rpc/common/endpoint/call_ep.h b/components/rpc/common/endpoint/call_ep.h
new file mode 100644
index 0000000..1b5d03d
--- /dev/null
+++ b/components/rpc/common/endpoint/call_ep.h
@@ -0,0 +1,134 @@
+/*
+ * Copyright (c) 2020, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef CALL_EP_H
+#define CALL_EP_H
+
+#include <stddef.h>
+#include <stdint.h>
+#include <rpc_status.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Definitions related to an rpc call endpoint */
+
+/** \brief Call parameter buffer
+ *
+ * Describes a buffer for holding call request and response parameters.
+ */
+struct call_param_buf {
+	size_t size;
+	size_t data_len;
+	void *data;
+};
+
+static inline struct call_param_buf call_param_buf_init_empty(void *data, size_t size)
+{
+	struct call_param_buf v;
+
+	v.size = size;
+	v.data_len = 0;
+	v.data = data;
+
+	return v;
+}
+
+static inline struct call_param_buf call_param_buf_init_full(void *data,
+							       size_t size,
+							       size_t data_len)
+{
+	struct call_param_buf v;
+
+	v.size = size;
+	v.data_len = data_len;
+	v.data = data;
+
+	return v;
+}
+
+/** \brief Serializer for handling call parameters
+ *
+ * An abstract serializer pointer, used for attaching a concrete
+ * serializer to a call request for serializing/deserializing call
+ * parameters.  The strategy for selecting an appropriate serializer
+ * could be hard-coded or dynamic, based on a content type identifier
+ * carried by a concrete rpc.
+ */
+typedef const void* call_param_serializer_ptr;
+
+/** \brief Call request
+ *
+ * A call request object represents a request from a client that will
+ * be handled by a call endpoint.
+ */
+struct call_req {
+	uint32_t caller_id;
+	uint32_t opcode;
+	int opstatus;
+	call_param_serializer_ptr serializer;
+	struct call_param_buf req_buf;
+	struct call_param_buf resp_buf;
+};
+
+static inline uint32_t call_req_get_caller_id(const struct call_req *req)
+{
+	return req->caller_id;
+}
+
+static inline uint32_t call_req_get_opcode(const struct call_req *req)
+{
+	return req->opcode;
+}
+
+static inline int call_req_get_opstatus(const struct call_req *req)
+{
+	return req->opstatus;
+}
+
+static inline void call_req_set_opstatus(struct call_req *req, int opstatus)
+{
+	req->opstatus = opstatus;
+}
+
+static inline call_param_serializer_ptr call_req_get_serializer(const struct call_req *req)
+{
+    return req->serializer;
+}
+
+static inline struct call_param_buf *call_req_get_req_buf(struct call_req *req)
+{
+	return &req->req_buf;
+}
+
+static inline struct call_param_buf *call_req_get_resp_buf(struct call_req *req)
+{
+	return &req->resp_buf;
+}
+
+/** \brief Call endpoint
+ *
+ * A generalized call endpoint.  Provides a standard interface for a
+ * call endpoint that handles incoming call requests.
+ */
+struct call_ep
+{
+	void *context;
+	rpc_status_t (*receive)(struct call_ep *ep, struct call_req *req);
+};
+
+static inline rpc_status_t call_ep_receive(struct call_ep *ep,
+					  struct call_req *req)
+{
+	return ep->receive(ep, req);
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* CALL_EP_H */
diff --git a/components/rpc/common/interface/component.cmake b/components/rpc/common/interface/component.cmake
new file mode 100644
index 0000000..d567602
--- /dev/null
+++ b/components/rpc/common/interface/component.cmake
@@ -0,0 +1,18 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2020, 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()
+
+set_property(TARGET ${TGT} PROPERTY RPC_CALLER_PUBLIC_HEADER_FILES
+	"${CMAKE_CURRENT_LIST_DIR}/rpc_caller.h"
+	"${CMAKE_CURRENT_LIST_DIR}/rpc_status.h"
+	)
+
+target_include_directories(${TGT} PUBLIC
+	"${CMAKE_CURRENT_LIST_DIR}"
+	)
diff --git a/components/rpc/common/interface/rpc_caller.h b/components/rpc/common/interface/rpc_caller.h
new file mode 100644
index 0000000..a75bdd9
--- /dev/null
+++ b/components/rpc/common/interface/rpc_caller.h
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2020, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef RPC_CALLER_H
+#define RPC_CALLER_H
+
+#include <stddef.h>
+#include <stdint.h>
+#include "rpc_status.h"
+
+/*
+ * The rpc_caller puplic interface may be exported as a public interface to
+ * a shared library.
+ */
+#ifdef EXPORT_PUBLIC_INTERFACE_RPC_CALLER
+#define RPC_CALLER_EXPORTED __attribute__((__visibility__("default")))
+#else
+#define RPC_CALLER_EXPORTED
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Defines an abstract interface for calling operations provided by an rpc endpoint.
+ * Concrete specializations will map the an RPC or direct calling mechanism to
+ * suite the deployment.
+ */
+
+typedef void *rpc_call_handle;
+
+struct rpc_caller
+{
+	void *context;
+
+	/* A concrete rpc_caller implements these methods */
+	rpc_call_handle (*call_begin)(void *context, uint8_t **req_buf, size_t req_len);
+
+	rpc_status_t (*call_invoke)(void *context, rpc_call_handle handle, uint32_t opcode,
+		     	int *opstatus, uint8_t **resp_buf, size_t *resp_len);
+
+	void (*call_end)(void *context, rpc_call_handle handle);
+};
+
+/*
+ * Starts a call transaction. The returned handle is an identifier for the
+ * transaction and must be passed as a parameter to call_invoke() and
+ * call_end(). A concrete rpc_caller may perform resource allocation during
+ * this call. This will include a buffer for the request message parameters.
+ * Returns a NULL handle on failure.
+ */
+RPC_CALLER_EXPORTED rpc_call_handle rpc_caller_begin(struct rpc_caller *s,
+			uint8_t **req_buf, size_t req_len);
+
+/*
+ * Invokes the operation identified by the opcode. This method blocks
+ * until the operation completes. The status of the call is returned. An
+ * additional endpoint specific status value is also returned. If a response
+ * message was received, the concrete rpc_caller will have allocated a
+ * buffer for the reponse. This buffer will hold valid data until the point when
+ * call_end() is called for the transaction.
+ */
+RPC_CALLER_EXPORTED rpc_status_t rpc_caller_invoke(struct rpc_caller *s, rpc_call_handle handle,
+			uint32_t opcode, int *opstatus, uint8_t **resp_buf, size_t *resp_len);
+
+/*
+ * Ends the call transaction, allowing any resource associated with the
+ * transaction to be freed.
+ */
+RPC_CALLER_EXPORTED void rpc_caller_end(struct rpc_caller *s, rpc_call_handle handle);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* RPC_CALLER_H */
diff --git a/components/rpc/common/interface/rpc_status.h b/components/rpc/common/interface/rpc_status.h
new file mode 100644
index 0000000..0405a64
--- /dev/null
+++ b/components/rpc/common/interface/rpc_status.h
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2020, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef RPC_STATUS_H
+#define RPC_STATUS_H
+
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** \brief RPC status code type
+ *
+ * Used for returning the status of an RPC transaction.  This is
+ * different from the opstatus which is used to return an endpoint
+ * specific status value.
+ */
+typedef int32_t rpc_status_t;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* RPC_STATUS_H */