forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathams.h
More file actions
299 lines (261 loc) · 9.22 KB
/
ams.h
File metadata and controls
299 lines (261 loc) · 9.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* SPDX-License-Identifier: BSD-3-Clause */
/*
* Copyright (c) 2023 Intel Corporation
*
* Author: Krzysztof Frydryk <krzysztofx.frydryk@intel.com>
*/
#ifndef __SOF_LIB_AMS_H__
#define __SOF_LIB_AMS_H__
#include <errno.h>
#include <rtos/task.h>
#include <ipc/topology.h>
#include <rtos/alloc.h>
#include <sof/coherent.h>
#include <sof/lib/uuid.h>
/* Reserved value "does not exist" or "unassigned" value for msg types */
#define AMS_INVALID_MSG_TYPE 0
/* Reserved value "does not exist" or "unassigned" value for slots */
#define AMS_INVALID_SLOT 0xFF
/* Wildcard for module_id and instance_id values */
#define AMS_ANY_ID 0xFFFF
/* max number of message UUIDs */
#define AMS_SERVICE_UUID_TABLE_SIZE 16
/* max number of async message routes */
#define AMS_ROUTING_TABLE_SIZE 16
/* Space allocated for async message content*/
#define AMS_MAX_MSG_SIZE 0x1000
/* Size of slots message, module id and instance id */
#define AMS_SLOT_SIZE(msg) (AMS_MESSAGE_SIZE(msg) + sizeof(uint16_t) * 2)
#define AMS_MESSAGE_SIZE(msg) (sizeof(*msg) - sizeof(char) + (sizeof(char) * (msg->message_length)))
/**
* \brief IXC message payload
*
* ams_message_payload - contains the actual Async Msg payload
*/
struct ams_message_payload {
/* Message IDs are assigned dynamically on new message entry creation
* For a new payload should be acquired by ams_get_message_type_id
*/
uint32_t message_type_id;
/* Producers module ID */
uint16_t producer_module_id;
/* Producers instance ID */
uint16_t producer_instance_id;
/* Message length */
uint32_t message_length;
/* Message payload */
uint8_t *message;
};
struct ams_slot {
uint16_t module_id;
uint16_t instance_id;
union {
struct ams_message_payload msg;
uint8_t msg_raw[AMS_MAX_MSG_SIZE];
} u;
uint32_t __aligned(PLATFORM_DCACHE_ALIGN) pad[];
};
/**
* \brief ams_msg_callback_fn
*
* Each subscriber provides this handler function for each message ID
*/
typedef void (*ams_msg_callback_fn)(const struct ams_message_payload *const ams_message_payload,
void *ctx);
/**
* \brief Internal struct ams_consumer_entry
*
* Describes a single consumer's subscription to a single message.
* Array of 'ams_consumer_entry' structs forms AsyncMessageService's routing
* table which allows for message dispatch.
*/
struct ams_consumer_entry {
/* Message ID that will be routed via this entry */
uint32_t message_type_id;
/* Callback provided by the subscribed consumer */
ams_msg_callback_fn consumer_callback;
/* Additional context for consumer_callback (optional) */
void *ctx;
/* Subscribed consumer's Module ID */
uint16_t consumer_module_id;
/* Subscribed consumer's Module Instance ID */
uint8_t consumer_instance_id;
/* Subscribed consumer's Module core id. Saved to speed up routing */
uint8_t consumer_core_id;
};
struct ams_producer {
/* Message ID that will be routed via this entry */
uint32_t message_type_id;
/* Subscribed producer's Module ID */
uint16_t producer_module_id;
/* Subscribed producer's Module Instance ID */
uint8_t producer_instance_id;
};
struct uuid_idx {
uint32_t message_type_id;
uint8_t message_uuid[UUID_SIZE];
};
struct ams_shared_context {
/* should be only used with ams_acquire/release function, not generic ones */
struct coherent c;
uint32_t last_used_msg_id;
struct ams_consumer_entry rt_table[AMS_ROUTING_TABLE_SIZE];
struct ams_producer producer_table[AMS_ROUTING_TABLE_SIZE];
struct uuid_idx uuid_table[AMS_SERVICE_UUID_TABLE_SIZE];
uint32_t slot_uses[CONFIG_CORE_COUNT];
/* marks which core already processed slot */
uint32_t slot_done[CONFIG_CORE_COUNT];
struct ams_slot slots[CONFIG_CORE_COUNT];
};
struct ams_context {
/* shared context must be always accessed with shared->c taken */
struct ams_shared_context *shared;
};
struct ams_task {
struct task ams_task;
struct async_message_service *ams;
uint32_t pending_slots;
};
struct async_message_service {
#if CONFIG_SMP
struct ams_task ams_task;
#endif /* CONFIG_SMP */
struct ams_context *ams_context;
};
#if CONFIG_AMS
int ams_init(void);
/**
* \brief Get Message Type ID
*
* assigns and returns a message type ID for specified message UUID.
* The value of message type ID is dynamically assigned and it will change between runs.
*
* \param[in] message_uuid UUID of message type
* \param[in] message_type_id Unique message type ID assigned by AMS
*/
int ams_get_message_type_id(const uint8_t *message_uuid,
uint32_t *message_type_id);
/**
* \brief Producer Register
*
* registers a producer of asynchronous messages of given message type.
* When a module instance calls this function,
* it informs the Asynchronous Messaging Service that it will be sending asynchronous messages.
*
* \param[in] message_type_id unique message type ID assigned during ams_get_message_type_id
* \param[in] module_id Module ID of module calling function
* \param[in] instance_id Instance ID of module calling function
*/
int ams_register_producer(uint32_t message_type_id,
uint16_t module_id,
uint16_t instance_id);
/**
* \brief Producer Unregister
*
* unregisters a producer of asynchronous messages of given type.
* When a module instance calls this function,
* it informs the Asynchronous Messaging Service
* that it will not be sending asynchronous messages anymore.
*
* \param[in] message_type_id unique message type ID assigned during ams_get_message_type_id
* \param[in] module_id Module ID of module calling function
* \param[in] instance_id Instance ID of module calling function
*/
int ams_unregister_producer(uint32_t message_type_id,
uint16_t module_id,
uint16_t instance_id);
/**
* \brief Register Consumer
*
* Registers a module instance as a consumer of specified message type.
* When specified message type is sent,
* a callback is called that was provided during registration process.
*
* The consumer callback is triggered when ams_send function was used to send a message
* and/or when ams_send_mi function with consumer's module ID and instance ID was used
* to send a message.
*
* \param[in] message_type_id unique message type ID assigned during ams_get_message_type_id
* \param[in] module_id Module ID of module calling function
* \param[in] instance_id Instance ID of module calling function
* \param[in] function callback that should be called when message is received
* \param[in] ctx Optional context that is passed to callback
*/
int ams_register_consumer(uint32_t message_type_id,
uint16_t module_id,
uint16_t instance_id,
ams_msg_callback_fn function,
void *ctx);
/**
* \brief Unegister Consumer
*
* Unregisters a consumer of specified message type
*
* \param[in] message_type_id unique message type ID assigned during ams_get_message_type_id
* \param[in] module_id Module ID of module calling function
* \param[in] instance_id Instance ID of module calling function
* \param[in] function callback that should be called when message is received
*/
int ams_unregister_consumer(uint32_t message_type_id,
uint16_t module_id,
uint16_t instance_id,
ams_msg_callback_fn function);
/**
* \brief Message Send
*
* Sends asynchronous message to all registered consumers by registered producer.
* The consumers registered on the same core may be called in context of a message producer
*
* \param[in] payload Message payload
*/
int ams_send(const struct ams_message_payload *payload);
/**
* \brief Message Send to Module Instance
*
* Sends asynchronous message to specified module instance.
* The consumer registered on the same core may be called in context of a message producer
*
* \param[in] payload Message payload
* \param[in] module_id Module ID of consumer that messages is sent to
* \param[in] instance_id Instance ID of consumer that messages is sent to
*/
int ams_send_mi(const struct ams_message_payload *payload,
uint16_t module_id, uint16_t instance_id);
static inline struct ams_shared_context *ams_ctx_get(void)
{
return sof_get()->ams_shared_ctx;
}
#else
static inline int ams_init(void) { return 0; }
static inline int ams_get_message_type_id(const uint8_t *message_uuid,
uint32_t *message_type_id) { return 0; }
static inline int ams_register_producer(uint32_t message_type_id,
uint16_t module_id,
uint16_t instance_id) { return 0; }
static inline int ams_unregister_producer(uint32_t message_type_id,
uint16_t module_id,
uint16_t instance_id) { return 0; }
static inline int ams_register_consumer(uint32_t message_type_id,
uint16_t module_id,
uint16_t instance_id,
ams_msg_callback_fn function,
void *ctx) { return 0; }
static inline int ams_unregister_consumer(uint32_t message_type_id,
uint16_t module_id,
uint16_t instance_id,
ams_msg_callback_fn function) { return 0; }
static inline int ams_send(const struct ams_message_payload *payload) { return 0; }
static inline int ams_send_mi(const struct ams_message_payload *payload, uint16_t module_id,
uint16_t instance_id) { return 0; }
static inline struct ams_shared_context *ams_ctx_get(void)
{
return NULL;
}
#endif /* CONFIG_AMS */
#if CONFIG_SMP && CONFIG_AMS
int process_incoming_message(uint32_t slot);
#else
static inline int process_incoming_message(uint32_t slot) { return 0; }
#endif /* CONFIG_SMP && CONFIG_AMS */
struct async_message_service **arch_ams_get(void);
#endif /* __SOF_LIB_AMS_H__ */