Example: Add TF-M example partition

Change-Id: I52f2f24af2c9ed239d033c62c7c7bea7b431311c
Co-authored-by: Jamie Fox <jamie.fox@arm.com>
Signed-off-by: Jamie Fox <jamie.fox@arm.com>
diff --git a/example_partition/CMakeLists.txt b/example_partition/CMakeLists.txt
new file mode 100644
index 0000000..a39a69b
--- /dev/null
+++ b/example_partition/CMakeLists.txt
@@ -0,0 +1,59 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2020, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+if (NOT TFM_PARTITION_EXAMPLE)
+    return()
+endif()
+
+cmake_minimum_required(VERSION 3.15)
+cmake_policy(SET CMP0079 NEW)
+
+add_library(tfm_partition_example STATIC)
+
+target_sources(tfm_partition_example
+    PRIVATE
+        tfm_example_partition.c
+)
+
+target_include_directories(tfm_partition_example
+    PRIVATE
+        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
+        ${CMAKE_BINARY_DIR}/generated/secure_fw/partitions/example_partition
+)
+
+target_link_libraries(tfm_partition_example
+    PRIVATE
+        tfm_secure_api
+        psa_interface
+        platform_s
+)
+
+############################ Secure API ########################################
+
+target_sources(tfm_secure_api
+    PRIVATE
+        ${CMAKE_CURRENT_SOURCE_DIR}/tfm_example_partition_secure_api.c
+)
+
+# The veneers give warnings about not being properly declared so they get hidden
+# to not overshadow _real_ warnings.
+set_source_files_properties(tfm_example_partition_secure_api.c
+    PROPERTIES
+        COMPILE_FLAGS -Wno-implicit-function-declaration
+)
+
+############################ Partition Defs ####################################
+
+target_link_libraries(tfm_partitions
+    INTERFACE
+        tfm_partition_example
+)
+
+target_compile_definitions(tfm_partition_defs
+    INTERFACE
+        TFM_PARTITION_EXAMPLE
+)
diff --git a/example_partition/tfm_example_partition.c b/example_partition/tfm_example_partition.c
new file mode 100644
index 0000000..ffaea85
--- /dev/null
+++ b/example_partition/tfm_example_partition.c
@@ -0,0 +1,103 @@
+/*
+ * Copyright (c) 2020, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#include <stdint.h>
+
+#include "psa/service.h"
+#include "psa_manifest/tfm_example_partition.h"
+#include "log/tfm_log.h"
+#include "tfm/tfm_spm_services.h"
+#include "tfm_plat_test.h"
+
+/**
+ * \brief An example service implementation that prints out an argument from the
+ *        client and then starts a timer.
+ */
+static void tfm_example_service(void)
+{
+    psa_status_t status;
+    uint32_t arg;
+    psa_msg_t msg;
+
+    /* Retrieve the message corresponding to the example service signal */
+    status = psa_get(TFM_EXAMPLE_SERVICE_SIGNAL, &msg);
+    if (status != PSA_SUCCESS) {
+        return;
+    }
+
+    /* Decode the message */
+    switch (msg.type) {
+    case PSA_IPC_CONNECT:
+    case PSA_IPC_DISCONNECT:
+        /* This service does not require any setup or teardown on connect or
+         * disconnect, so just reply with success.
+         */
+        status = PSA_SUCCESS;
+        break;
+    case PSA_IPC_CALL:
+        if (msg.in_size[0] != sizeof(arg)) {
+            status = PSA_ERROR_PROGRAMMER_ERROR;
+            break;
+        }
+
+        /* Print arg from client */
+        psa_read(msg.handle, 0, &arg, sizeof(arg));
+        LOG_MSG("[Example partition] Service called! arg=%p\r\n", arg);
+
+        /* Start timer. The interrupt triggered when it expires will be handled
+         * by tfm_example_timer_handler().
+         */
+        tfm_plat_test_secure_timer_start();
+        LOG_MSG("[Example partition] Timer started...\r\n");
+
+        status = PSA_SUCCESS;
+        break;
+    default:
+        /* Invalid message type */
+        status = PSA_ERROR_PROGRAMMER_ERROR;
+        break;
+    }
+
+    /* Reply with the message result status to unblock the client */
+    psa_reply(msg.handle, status);
+}
+
+/**
+ * \brief An example interrupt handler.
+ */
+static void tfm_example_timer_handler(void)
+{
+    /* Stop timer */
+    tfm_plat_test_secure_timer_stop();
+    /* Inform the SPM that the timer interrupt has been handled */
+    psa_eoi(TFM_EXAMPLE_SIGNAL_TIMER_0_IRQ);
+}
+
+/**
+ * \brief The example partition's entry function.
+ */
+void tfm_example_partition_main(void)
+{
+    psa_signal_t signals;
+
+    /* Enable timer IRQ */
+    tfm_enable_irq(TFM_EXAMPLE_SIGNAL_TIMER_0_IRQ);
+
+    /* Continually wait for one or more of the partition's RoT Service or
+     * interrupt signals to be asserted and then handle the asserted signal(s).
+     */
+    while (1) {
+        signals = psa_wait(PSA_WAIT_ANY, PSA_BLOCK);
+
+        if (signals & TFM_EXAMPLE_SERVICE_SIGNAL) {
+            tfm_example_service();
+        }
+        if (signals & TFM_EXAMPLE_SIGNAL_TIMER_0_IRQ) {
+            tfm_example_timer_handler();
+        }
+    }
+}
diff --git a/example_partition/tfm_example_partition.yaml b/example_partition/tfm_example_partition.yaml
new file mode 100644
index 0000000..c9a230e
--- /dev/null
+++ b/example_partition/tfm_example_partition.yaml
@@ -0,0 +1,37 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2020, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+{
+  "psa_framework_version": 1.0,
+  "name": "TFM_SP_EXAMPLE",
+  "type": "APPLICATION-ROT",
+  "priority": "NORMAL",
+  "entry_point": "tfm_example_partition_main",
+  "stack_size": "0x200",
+  "mmio_regions": [
+    {
+      "name": "TFM_PERIPHERAL_TIMER0",
+      "permission": "READ-WRITE"
+    }
+  ],
+  "services": [
+    {
+      "name": "TFM_EXAMPLE_SERVICE",
+      "sid": "0x0000F120",
+      "non_secure_clients": true,
+      "version": 1,
+      "version_policy": "STRICT"
+    }
+  ],
+  "irqs": [
+    {
+      "source": "TFM_TIMER0_IRQ",
+      "signal": "TFM_EXAMPLE_SIGNAL_TIMER_0_IRQ",
+      "tfm_irq_priority": 64,
+    }
+  ],
+}
diff --git a/example_partition/tfm_example_partition_api.c b/example_partition/tfm_example_partition_api.c
new file mode 100644
index 0000000..5e83cd5
--- /dev/null
+++ b/example_partition/tfm_example_partition_api.c
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2020, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#include "tfm_example_partition_api.h"
+
+#include "psa/client.h"
+#include "psa_manifest/sid.h"
+
+psa_status_t tfm_example_partition_call(uint32_t arg)
+{
+    psa_status_t status;
+    psa_handle_t handle;
+    psa_invec in_vec[] = {
+        { .base = &arg, .len = sizeof(arg) },
+    };
+
+    handle = psa_connect(TFM_EXAMPLE_SERVICE_SID, TFM_EXAMPLE_SERVICE_VERSION);
+    if (!PSA_HANDLE_IS_VALID(handle)) {
+        return PSA_HANDLE_TO_ERROR(handle);
+    }
+
+    status = psa_call(handle, PSA_IPC_CALL, in_vec, 1, NULL, 0);
+
+    psa_close(handle);
+
+    return status;
+}
diff --git a/example_partition/tfm_example_partition_api.h b/example_partition/tfm_example_partition_api.h
new file mode 100644
index 0000000..f8876ef
--- /dev/null
+++ b/example_partition/tfm_example_partition_api.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2020, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __TFM_EXAMPLE_PARTITION_API_H__
+#define __TFM_EXAMPLE_PARTITION_API_H__
+
+#include <stdint.h>
+
+#include "psa/error.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * \brief Makes a psa_call to the example partition.
+ *
+ * \param[in] arg  Example parameter
+ *
+ * \return Returns error code as specified in \ref psa_status_t
+ */
+psa_status_t tfm_example_partition_call(uint32_t arg);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __TFM_EXAMPLE_PARTITION_API_H__ */
diff --git a/example_partition/tfm_example_partition_readme.rst b/example_partition/tfm_example_partition_readme.rst
new file mode 100644
index 0000000..47aca5c
--- /dev/null
+++ b/example_partition/tfm_example_partition_readme.rst
@@ -0,0 +1,36 @@
+###############################
+TF-M Example Partition - Readme
+###############################
+The TF-M example partition is a simple secure partition implementation provided
+to aid development of new secure partitions. It implements one RoT Service and
+one interrupt handler.
+
+********************************
+How to run the example partition
+********************************
+#. Copy the ``example_partition`` directory to the ``secure_fw/partitions``
+   directory of the TF-M repo.
+#. Add the following entry to ``tools/tfm_manifest_list.yaml``::
+
+    {
+      "name": "TF-M Example Partition",
+      "short_name": "TFM_SP_EXAMPLE",
+      "manifest": "secure_fw/partitions/example_partition/tfm_example_partition.yaml",
+      "tfm_partition_ipc": true,
+      "conditional": "TFM_PARTITION_EXAMPLE",
+      "version_major": 0,
+      "version_minor": 1,
+      "pid": 270,
+      "linker_pattern": {
+        "library_list": [
+           "*tfm_partition_example.*"
+        ]
+      }
+    }
+
+#. Build TF-M in the usual way, but provide ``-DTFM_PARTITION_EXAMPLE=ON`` as a
+   parameter to the CMake command.
+
+--------------
+
+*Copyright (c) 2020, Arm Limited. All rights reserved.*
diff --git a/example_partition/tfm_example_partition_secure_api.c b/example_partition/tfm_example_partition_secure_api.c
new file mode 100644
index 0000000..0a958c2
--- /dev/null
+++ b/example_partition/tfm_example_partition_secure_api.c
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2020, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#include "tfm_example_partition_api.h"
+
+#include "psa/client.h"
+#include "psa_manifest/sid.h"
+
+__attribute__((section("SFN")))
+psa_status_t tfm_example_partition_call(uint32_t arg)
+{
+    psa_status_t status;
+    psa_handle_t handle;
+    psa_invec in_vec[] = {
+        { .base = &arg, .len = sizeof(arg) },
+    };
+
+    handle = psa_connect(TFM_EXAMPLE_SERVICE_SID, TFM_EXAMPLE_SERVICE_VERSION);
+    if (!PSA_HANDLE_IS_VALID(handle)) {
+        return PSA_HANDLE_TO_ERROR(handle);
+    }
+
+    status = psa_call(handle, PSA_IPC_CALL, in_vec, 1, NULL, 0);
+
+    psa_close(handle);
+
+    return status;
+}