blob: f3a2dc41d9eb95cabc771c445c4a2026d20d4cba [file] [log] [blame]
Jonny Svärd44398c82020-10-06 14:18:28 +02001/*
2 * Copyright (c) 2020 Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#ifndef MAILBOX_HPP
20#define MAILBOX_HPP
21
Jonny Svärd44398c82020-10-06 14:18:28 +020022#include <cstddef>
Kristofer Jonsson73ebc482020-10-12 11:17:04 +020023#include <cstdint>
Jonny Svärd44398c82020-10-06 14:18:28 +020024#include <list>
25
26namespace Mailbox {
27
Jonny Svärd9fc527b2020-11-16 16:18:07 +010028/**
29 * The Mailbox parent class
30 *
31 * Intended to be implemented by a driver subclass, see MHU_v2 driver as example.
32 */
Jonny Svärd44398c82020-10-06 14:18:28 +020033class Mailbox {
34public:
Jonny Svärd9fc527b2020-11-16 16:18:07 +010035 /**
36 * Constructor/Destructor
37 */
Jonny Svärd44398c82020-10-06 14:18:28 +020038 Mailbox();
39 virtual ~Mailbox();
Jonny Svärd9fc527b2020-11-16 16:18:07 +010040
41 /**
42 * Intended to trigger an interrupt to an external block
43 * MUST be implemented by subclass.
44 */
45 virtual bool sendMessage() = 0;
46
47 /**
48 * Intended to be called when Cortex M has received an
49 * interrupt/event from mailbox/mhu. If an interrupt needs to be cleared,
50 * this is a good place to do that. MUST call notify().
51 * MUST be implemented by subclass.
52 */
Jonny Svärd44398c82020-10-06 14:18:28 +020053 virtual void handleMessage() = 0;
Jonny Svärd9fc527b2020-11-16 16:18:07 +010054
55 /**
56 * Can be used to verify that hardware versions match expected versions
57 * CAN be implemented by subclass, optional. Default impl returns true.
58 */
Jonny Svärd44398c82020-10-06 14:18:28 +020059 virtual bool verifyHardware();
Jonny Svärd9fc527b2020-11-16 16:18:07 +010060
61 /**
62 * Function signature for callbacks
63 */
Jonny Svärd44398c82020-10-06 14:18:28 +020064 typedef void (*CallbackFptr)(void *userArg);
Jonny Svärd9fc527b2020-11-16 16:18:07 +010065
66 /**
67 * Register a callback to be called when a message is received.
68 */
Jonny Svärd44398c82020-10-06 14:18:28 +020069 void registerCallback(CallbackFptr callback, void *userArg);
Jonny Svärd9fc527b2020-11-16 16:18:07 +010070
71 /**
72 * Remove a specific callback from the callback list.
73 */
Jonny Svärd44398c82020-10-06 14:18:28 +020074 void deregisterCallback(CallbackFptr callback, void *userArg);
75
76protected:
Jonny Svärd9fc527b2020-11-16 16:18:07 +010077 /**
78 * Calls every registered callback when a message has been received.
79 */
Jonny Svärd44398c82020-10-06 14:18:28 +020080 void notify();
Jonny Svärd9fc527b2020-11-16 16:18:07 +010081
82 /**
83 * Helper functions
84 */
Jonny Svärd44398c82020-10-06 14:18:28 +020085 uint32_t read32(volatile uint32_t *baseAddr, const uint32_t offset);
86 void write32(volatile uint32_t *baseAddr, const uint32_t offset, const uint32_t value);
87
88private:
89 struct Callback {
90 bool operator==(const Callback &b) const;
91 CallbackFptr callback;
92 void *userArg;
93 };
94
95 std::list<Callback> callbacks;
96};
97
98} // namespace Mailbox
99
100#endif