blob: f6d9edb99bf3846a2817c1dd377a62af4c68bcf8 [file] [log] [blame]
Yulia Garboviche9cdc632021-11-23 20:00:04 +02001/*
2 * Copyright (c) 2020-2021 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
22#include <cstddef>
23#include <cstdint>
24#include <list>
25
26namespace Mailbox {
27
28/**
29 * The Mailbox parent class
30 *
31 * Intended to be implemented by a driver subclass, see MHU_v2 driver as example.
32 */
33class Mailbox {
34public:
35 /**
36 * Constructor/Destructor
37 */
38 Mailbox();
39 virtual ~Mailbox();
40
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 */
53 virtual void handleMessage() = 0;
54
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 */
59 virtual bool verifyHardware();
60
61 /**
62 * Function signature for callbacks
63 */
64 typedef void (*CallbackFptr)(void *userArg);
65
66 /**
67 * Register a callback to be called when a message is received.
68 */
69 void registerCallback(CallbackFptr callback, void *userArg);
70
71 /**
72 * Remove a specific callback from the callback list.
73 */
74 void deregisterCallback(CallbackFptr callback, void *userArg);
75
76protected:
77 /**
78 * Calls every registered callback when a message has been received.
79 */
80 void notify();
81
82 /**
83 * Helper functions
84 */
85 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