Add a Mailbox interface

Change-Id: If68e70709c496849cb781af5f7288fa3f7ddcdb5
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0487b44..3e14fe0 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -58,5 +58,8 @@
 # Build applications
 add_subdirectory(applications)
 
+# Build drivers
+add_subdirectory(drivers)
+
 # Merge libraries into static library
-target_link_libraries(ethosu_core INTERFACE tflu cmsis_core cmsis_device ethosu_applications)
+target_link_libraries(ethosu_core INTERFACE tflu cmsis_core cmsis_device ethosu_applications ethosu_drivers)
diff --git a/applications/message_process/CMakeLists.txt b/applications/message_process/CMakeLists.txt
index 51d514b..9c80672 100644
--- a/applications/message_process/CMakeLists.txt
+++ b/applications/message_process/CMakeLists.txt
@@ -18,4 +18,4 @@
 
 add_library(message_process STATIC src/message_process.cc)
 target_include_directories(message_process PUBLIC include ${LINUX_DRIVER_STACK_PATH}/kernel)
-target_link_libraries(message_process PRIVATE cmsis_core inference_process)
+target_link_libraries(message_process PRIVATE cmsis_core inference_process mailbox)
diff --git a/applications/message_process/include/message_process.hpp b/applications/message_process/include/message_process.hpp
index e820b60..b3c1bdd 100644
--- a/applications/message_process/include/message_process.hpp
+++ b/applications/message_process/include/message_process.hpp
@@ -21,6 +21,7 @@
 
 #include <ethosu_core_interface.h>
 #include <inference_process.hpp>
+#include <mailbox.hpp>
 
 #include <cstddef>
 #include <cstdio>
@@ -82,10 +83,12 @@
 
 class MessageProcess {
 public:
-    MessageProcess(ethosu_core_queue &in, ethosu_core_queue &out, InferenceProcess::InferenceProcess &inferenceProcess);
+    MessageProcess(ethosu_core_queue &in,
+                   ethosu_core_queue &out,
+                   Mailbox::Mailbox &mbox,
+                   InferenceProcess::InferenceProcess &inferenceProcess);
 
     void run();
-    void handleIrq();
     bool handleMessage();
     void sendPong();
     void sendInferenceRsp(uint64_t userArg, std::vector<InferenceProcess::DataPtr> &ofm, bool failed);
@@ -93,7 +96,10 @@
 private:
     QueueImpl queueIn;
     QueueImpl queueOut;
+    Mailbox::Mailbox &mailbox;
     InferenceProcess::InferenceProcess &inferenceProcess;
+    void handleIrq();
+    static void mailboxCallback(void *userArg);
 };
 
 } // namespace MessageProcess
diff --git a/applications/message_process/src/message_process.cc b/applications/message_process/src/message_process.cc
index 4333640..4533455 100644
--- a/applications/message_process/src/message_process.cc
+++ b/applications/message_process/src/message_process.cc
@@ -100,10 +100,6 @@
     SCB_CleanDCache();
 #endif
 
-    // TODO replace with mailbox driver APIs
-    volatile uint32_t *set = reinterpret_cast<volatile uint32_t *>(0x41A00014);
-    *set                   = 0x1;
-
     return true;
 }
 
@@ -132,9 +128,12 @@
 
 MessageProcess::MessageProcess(ethosu_core_queue &in,
                                ethosu_core_queue &out,
+                               Mailbox::Mailbox &mbox,
                                ::InferenceProcess::InferenceProcess &_inferenceProcess) :
     queueIn(in),
-    queueOut(out), inferenceProcess(_inferenceProcess) {}
+    queueOut(out), mailbox(mbox), inferenceProcess(_inferenceProcess) {
+    mailbox.registerCallback(mailboxCallback, reinterpret_cast<void *>(this));
+}
 
 void MessageProcess::run() {
     while (true) {
@@ -238,6 +237,7 @@
     if (!queueOut.write(ETHOSU_CORE_MSG_PONG)) {
         printf("Failed to write pong.\n");
     }
+    mailbox.sendMessage();
 }
 
 void MessageProcess::sendInferenceRsp(uint64_t userArg, vector<DataPtr> &ofm, bool failed) {
@@ -259,5 +259,12 @@
     if (!queueOut.write(ETHOSU_CORE_MSG_INFERENCE_RSP, rsp)) {
         printf("Failed to write inference.\n");
     }
+    mailbox.sendMessage();
 }
+
+void MessageProcess::mailboxCallback(void *userArg) {
+    MessageProcess *_this = reinterpret_cast<MessageProcess *>(userArg);
+    _this->handleIrq();
+}
+
 } // namespace MessageProcess
diff --git a/drivers/CMakeLists.txt b/drivers/CMakeLists.txt
new file mode 100644
index 0000000..cea6325
--- /dev/null
+++ b/drivers/CMakeLists.txt
@@ -0,0 +1,23 @@
+#
+# Copyright (c) 2020 Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed under the Apache License, Version 2.0 (the License); you may
+# not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an AS IS BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+add_library(ethosu_drivers INTERFACE)
+
+# Mailbox driver
+add_subdirectory(mailbox)
+target_link_libraries(ethosu_drivers INTERFACE mailbox)
diff --git a/drivers/mailbox/CMakeLists.txt b/drivers/mailbox/CMakeLists.txt
new file mode 100644
index 0000000..6d55407
--- /dev/null
+++ b/drivers/mailbox/CMakeLists.txt
@@ -0,0 +1,22 @@
+#
+# Copyright (c) 2020 Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed under the Apache License, Version 2.0 (the License); you may
+# not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an AS IS BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+add_library(mailbox STATIC)
+target_include_directories(mailbox PUBLIC include)
+target_sources(mailbox PRIVATE src/mailbox.cc)
+
diff --git a/drivers/mailbox/include/mailbox.hpp b/drivers/mailbox/include/mailbox.hpp
new file mode 100644
index 0000000..9074754
--- /dev/null
+++ b/drivers/mailbox/include/mailbox.hpp
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2020 Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed under the Apache License, Version 2.0 (the License); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef MAILBOX_HPP
+#define MAILBOX_HPP
+
+#include <cstddef>
+#include <list>
+
+namespace Mailbox {
+
+class Mailbox {
+public:
+    Mailbox();
+    virtual ~Mailbox();
+    virtual bool sendMessage() = 0;
+    virtual void handleMessage() = 0;
+    virtual bool verifyHardware();
+    typedef void (*CallbackFptr)(void *userArg);
+    void registerCallback(CallbackFptr callback, void *userArg);
+    void deregisterCallback(CallbackFptr callback, void *userArg);
+
+protected:
+    void notify();
+    uint32_t read32(volatile uint32_t *baseAddr, const uint32_t offset);
+    void write32(volatile uint32_t *baseAddr, const uint32_t offset, const uint32_t value);
+
+private:
+    struct Callback {
+        bool operator==(const Callback &b) const;
+        CallbackFptr callback;
+        void *userArg;
+    };
+
+    std::list<Callback> callbacks;
+};
+
+} // namespace Mailbox
+
+#endif
diff --git a/drivers/mailbox/src/mailbox.cc b/drivers/mailbox/src/mailbox.cc
new file mode 100644
index 0000000..0d3dfed
--- /dev/null
+++ b/drivers/mailbox/src/mailbox.cc
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2020 Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed under the Apache License, Version 2.0 (the License); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <mailbox.hpp>
+
+#include <cstddef>
+#include <cassert>
+
+namespace Mailbox {
+
+Mailbox::Mailbox() {}
+Mailbox::~Mailbox() {}
+
+bool Mailbox::verifyHardware() {
+    return true;
+}
+
+void Mailbox::registerCallback(CallbackFptr callback, void *userArg) {
+    callbacks.push_back({callback, userArg});
+}
+
+void Mailbox::deregisterCallback(CallbackFptr callback, void *userArg) {
+    callbacks.remove({callback, userArg});
+}
+
+void Mailbox::notify() {
+    for (auto &it : callbacks) {
+        it.callback(it.userArg);
+    }
+}
+
+uint32_t Mailbox::read32(volatile uint32_t *baseAddr, const uint32_t offset) {
+    assert(offset % 4 == 0);
+    volatile uint32_t *addr = baseAddr + (offset / 4);
+
+    return *addr;
+}
+
+void Mailbox::write32(volatile uint32_t *baseAddr, const uint32_t offset, const uint32_t value) {
+    assert(offset % 4 == 0);
+    volatile uint32_t *addr = baseAddr + (offset / 4);
+
+    *addr = value;
+}
+
+bool Mailbox::Callback::operator==(const Callback &b) const {
+    return (callback == b.callback && userArg == b.userArg);
+}
+
+} // namespace Mailbox