Repository navigation
Expand file tree
/
Copy pathcommon.h
More file actions
495 lines (429 loc) · 14.7 KB
/
Copy pathcommon.h
File metadata and controls
495 lines (429 loc) · 14.7 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright(c) 2016 Intel Corporation. All rights reserved.
*
* Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
* Keyon Jie <yang.jie@linux.intel.com>
*/
#ifndef __SOF_IPC_COMMON_H__
#define __SOF_IPC_COMMON_H__
#include <rtos/bit.h>
#include <rtos/alloc.h>
#include <sof/list.h>
#include <rtos/task.h>
#include <rtos/spinlock.h>
#include <rtos/sof.h>
#include <user/trace.h>
#include <ipc/header.h>
#include <ipc/stream.h>
#include <sof/ipc/ipc_reply.h>
#include <stdbool.h>
#include <stdint.h>
struct comp_driver;
struct dma_sg_elem_array;
struct ipc_msg;
struct ipc4_message_request;
/* validates internal non tail structures within IPC command structure */
#define IPC_IS_SIZE_INVALID(object) \
((object).hdr.size != sizeof(object))
#define IPC_TAIL_IS_SIZE_INVALID(object) \
((object).comp.hdr.size + (object).comp.ext_data_length < sizeof(object))
/* ipc trace context, used by multiple units */
extern struct tr_ctx ipc_tr;
/* convenience error trace for mismatched internal structures */
#define IPC_SIZE_ERROR_TRACE(ctx, object) \
tr_err(ctx, "ipc: size %d expected %zu", \
(object).hdr.size, sizeof(object))
/* Returns pipeline source component */
#define ipc_get_ppl_src_comp(ipc, ppl_id) \
ipc_get_ppl_comp(ipc, ppl_id, PPL_DIR_UPSTREAM)
/* Returns pipeline sink component */
#define ipc_get_ppl_sink_comp(ipc, ppl_id) \
ipc_get_ppl_comp(ipc, ppl_id, PPL_DIR_DOWNSTREAM)
#define IPC_TASK_INLINE BIT(0)
#define IPC_TASK_IN_THREAD BIT(1)
#define IPC_TASK_SECONDARY_CORE BIT(2)
#define IPC_TASK_POWERDOWN BIT(3)
#ifdef CONFIG_CORE_COUNT
#define IPC_DSP_CORE_COUNT CONFIG_CORE_COUNT
#else
#define IPC_DSP_CORE_COUNT 1
#endif
/** @brief Maximum payload size of a cross-core request between user threads */
#define IPC_USER_XCORE_PAYLOAD_SIZE 128
/**
* @brief Cross-core request slot of a user-space IPC thread.
*
* User-space threads can't use IDC. Instead, they pass IDC messages for
* another core to the user-space IPC thread of that core via this slot.
*/
struct ipc_user_xcore {
/** @brief Serializes senders to this core */
struct k_sem *lock;
/** @brief Thread, which has reserved the slot, or NULL */
struct k_thread *owner;
/** @brief Signalled by the target core when the request is done */
struct k_sem *done;
/** @brief IDC message header and extension */
uint32_t header;
uint32_t extension;
/** @brief Result from the target core */
int result;
/** @brief Copy of IPC4 bind / unbind message, referenced by the payload */
uint32_t ipc4_bind_data[2];
/** @brief Copy of the IDC message payload */
uint8_t payload[IPC_USER_XCORE_PAYLOAD_SIZE] __aligned(8);
};
struct ipc_user {
struct k_thread *thread[IPC_DSP_CORE_COUNT];
struct k_sem *sem;
struct k_event *event[IPC_DSP_CORE_COUNT];
/** @brief Copy of IPC4 message primary word forwarded to user thread */
uint32_t ipc_msg_pri;
/** @brief Copy of IPC4 message extension word forwarded to user thread */
uint32_t ipc_msg_ext;
/**
* @brief SET_PIPELINE_STATE phase(s) to run for a forwarded single
* pipeline (IDC_PPL_STATE_PHASE_*), or 0 to process the message fully
*/
uint32_t ppl_phase;
/** @brief Result code from user thread processing */
int result;
/** @brief Reply extension word from user thread (e.g. CONFIG_GET result) */
uint32_t reply_ext;
/** @brief Reply TX data size from user thread (e.g. LARGE_CONFIG_GET result) */
uint32_t reply_tx_size;
/** @brief Reply TX data pointer from user thread (e.g. LARGE_CONFIG_GET result) */
void *reply_tx_data;
struct ipc *ipc;
struct k_thread *audio_thread[IPC_DSP_CORE_COUNT];
/** @brief Original kernel driver pointer for restoring dev->drv after create */
const struct comp_driver *init_drv;
/**
* @brief Component created by the kernel, for the user thread to add
* to the component list (DP modules), see ipc4_init_module_instance()
*/
struct comp_dev *init_dev;
/**
* @brief User-accessible copy of comp_driver + tr_ctx for create().
*
* The comp_driver and tr_ctx structs reside in kernel memory
* (.rodata/.data) which is not user-readable. The kernel handler
* copies them here before forwarding to the user thread.
* Size verified by BUILD_ASSERT in handler-user.c.
*/
uint8_t init_drv_data[160] __aligned(4);
/** @brief Cross-core request slots, one per target core */
struct ipc_user_xcore xcore[IPC_DSP_CORE_COUNT];
};
#ifdef CONFIG_SOF_USERSPACE_LL
/**
* Kernel-only IPC state.
*
* With CONFIG_SOF_USERSPACE_LL, struct ipc is in a partition that the LL and
* IPC user threads can write. The send queue, its lock, the completion
* state and the IPC processing task are used by kernel code only, so they
* are kept here instead. The field names match the struct ipc fields used
* without CONFIG_SOF_USERSPACE_LL, so code accesses both through ipc_ks().
*
* comp_data stays in struct ipc for the IPC user thread, but kernel code
* must use the copy here, ipc_ks(ipc)->comp_data, to find the buffer.
*/
struct ipc_kstate {
struct k_spinlock lock; /* locking mechanism */
void *comp_data; /* kernel copy of ipc->comp_data */
int pm_prepare_D3; /* do we need to prepare for D3 */
struct list_item msg_list; /* queue of messages to be sent */
uint32_t task_mask; /* tasks to be completed by this IPC */
/* processing task, run by the kernel scheduler */
#if CONFIG_TWB_IPC_TASK
struct task *ipc_task;
#else
struct task ipc_task;
#endif
};
/** Kernel context only. */
struct ipc_kstate *ipc_kstate_get(void);
struct ipc;
static inline struct ipc_kstate *ipc_ks(struct ipc *ipc)
{
(void)ipc;
return ipc_kstate_get();
}
#else
#define ipc_ks(ipc) (ipc)
#endif
struct ipc {
#ifndef CONFIG_SOF_USERSPACE_LL
/* in struct ipc_kstate with CONFIG_SOF_USERSPACE_LL, use ipc_ks() */
struct k_spinlock lock; /* locking mechanism */
int pm_prepare_D3; /* do we need to prepare for D3 */
struct list_item msg_list; /* queue of messages to be sent */
uint32_t task_mask; /* tasks to be completed by this IPC */
#endif
void *comp_data;
bool is_notification_pending; /* notification is being sent to host */
unsigned int core; /* core, processing the IPC */
struct list_item comp_list; /* list of component devices */
#ifndef CONFIG_SOF_USERSPACE_LL
/* processing task, in struct ipc_kstate with CONFIG_SOF_USERSPACE_LL */
#if CONFIG_TWB_IPC_TASK
struct task *ipc_task;
#else
struct task ipc_task;
#endif
#endif
#ifdef CONFIG_SOF_USERSPACE_LL
struct ipc_user *ipc_user_pdata;
struct mod_alloc_ctx *ll_alloc;
#endif
#ifdef CONFIG_SOF_TELEMETRY_IO_PERFORMANCE_MEASUREMENTS
/* io performance measurement */
struct io_perf_data_item *io_perf_in_msg_count;
struct io_perf_data_item *io_perf_out_msg_count;
#endif
void *private;
};
#define ipc_set_drvdata(ipc, data) \
((ipc)->private = data)
#define ipc_get_drvdata(ipc) \
((ipc)->private)
extern struct task_ops ipc_task_ops;
#ifdef __ZEPHYR__
/**
* Return the IPC send work queue.
*
* The queue is kept outside struct ipc on purpose: with
* CONFIG_SOF_USERSPACE_LL, struct ipc is in a partition that user threads
* can write, and the queue embeds its supervisor-mode thread object.
* Kernel context only.
*/
struct k_work_q *ipc_send_wq_get(void);
#endif
#ifdef CONFIG_SOF_USERSPACE_LL
struct ipc *ipc_get(void);
#else
/**
* \brief Get the IPC global context.
* @return The global IPC context.
*/
static inline struct ipc *ipc_get(void)
{
return sof_get()->ipc;
}
#endif /* CONFIG_SOF_USERSPACE_LL */
/**
* \brief Initialise global IPC context.
* @param[in,out] sof Global SOF context.
* @return 0 on success.
*/
int ipc_init(struct sof *sof);
/**
* \brief Free global IPC context.
* @param[in] ipc Global IPC context.
*/
void ipc_free(struct ipc *ipc);
/**
* \brief Data provided by the platform which use ipc...page_descriptors().
*
* Note: this should be made private for ipc-host-ptable.c and ipc
* drivers for platforms that use ptables.
*/
struct ipc_data_host_buffer {
/* DMA */
#if CONFIG_ZEPHYR_NATIVE_DRIVERS
struct sof_dma *dmac;
#else
struct dma *dmac;
#endif
uint8_t *page_table;
};
/**
* \brief Processes page tables for the host buffer.
* @param[in] ipc Ipc
* @param[in] ring Ring description sent via Ipc
* @param[in] direction Direction (playback/capture)
* @param[out] elem_array Array of SG elements
* @param[out] ring_size Size of the ring
* @return Status, 0 if successful, error code otherwise.
*/
int ipc_process_host_buffer(struct ipc *ipc,
struct sof_ipc_host_buffer *ring,
uint32_t direction,
struct dma_sg_elem_array *elem_array,
uint32_t *ring_size);
/**
* \brief send a IPC buffer status notify message
*/
void ipc_send_buffer_status_notify(void);
struct dai_data;
/**
* \brief Configure DAI.
* @return 0 on success.
*/
int ipc_dai_data_config(struct dai_data *dd, struct comp_dev *dev);
/**
* \brief Processes IPC4 userspace module message.
* @param[in] ipc4 IPC4 message request.
* @param[in] reply IPC message reply structure.
* @return IPC4_SUCCESS on success, error code otherwise.
*/
int ipc4_user_process_module_message(struct ipc4_message_request *ipc4, struct ipc_msg *reply);
/**
* \brief Processes IPC4 userspace global message.
* @param[in] ipc4 IPC4 message request.
* @param[in] reply IPC message reply structure.
* @return IPC4_SUCCESS on success, error code otherwise.
*/
int ipc4_user_process_glb_message(struct ipc4_message_request *ipc4, struct ipc_msg *reply);
/**
* \brief Complete the IPC compound message.
* @param[in] msg_id IPC message ID.
* @param[in] error Error code of the IPC command.
*/
void ipc_compound_msg_done(uint32_t msg_id, int error);
/**
* \brief create a IPC boot complete message.
* @param[in] header header.
* @param[in] data data.
*/
void ipc_boot_complete_msg(struct ipc_cmd_hdr *header, uint32_t data);
#if defined(CONFIG_PM_DEVICE) && defined(CONFIG_IPC_SERVICE_BACKEND_INTEL_ADSP_HOST_IPC)
/**
* @brief Send an IPC response to Host power transition request informing
* that power transition failed.
* @note Normally an reply to the Host IPC message is performed in the
* low level assembly code to make sure DSP completed all operations before
* power cut-off.
* However, when power transition fails for some reason, we should send the
* IPC response informing about the failure.
* This happens in abnormal circumstances since the response is send not during
* IPC task but during power transition logic in the Idle thread.
*/
void ipc_send_failed_power_transition_response(void);
#endif /* CONFIG_PM_DEVICE && CONFIG_IPC_SERVICE_BACKEND_INTEL_ADSP_HOST_IPC */
/**
* \brief Send a IPC notification that FW has hit
* a DSP notification.
*/
void ipc_send_panic_notification(void);
/**
* \brief Read a compact IPC message or return NULL for normal message.
* @return Pointer to the compact message data.
*/
struct ipc_cmd_hdr *ipc_compact_read_msg(void);
/**
* \brief Write a compact IPC message.
* @param[in] hdr Compact message header data.
* @return Number of words written.
*/
int ipc_compact_write_msg(struct ipc_cmd_hdr *hdr);
/**
* \brief Prepare an IPC message for sending.
* @param[in] msg The ipc msg.
* @return pointer to raw header or NULL.
*/
struct ipc_cmd_hdr *ipc_prepare_to_send(const struct ipc_msg *msg);
/**
* \brief Validate mailbox contents for valid IPC header.
* @return pointer to header if valid or NULL.
*/
struct ipc_cmd_hdr *mailbox_validate(void);
/**
* Generic IPC command handler. Expects that IPC command (the header plus
* any optional payload) is deserialized from the IPC HW by the platform
* specific method.
*
* @param _hdr Points to the IPC command header.
*/
void ipc_cmd(struct ipc_cmd_hdr *_hdr);
/**
* \brief IPC message to be processed on other core.
* @param[in] core Core id for IPC to be processed on.
* @param[in] blocking Process in blocking mode: wait for completion.
* @return 1 if successful (reply sent by other core), error code otherwise.
*/
int ipc_process_on_core(uint32_t core, bool blocking);
/**
* \brief Call platform-specific IPC completion function.
*/
void ipc_complete_cmd(struct ipc *ipc);
/* GDB stub: should enter GDB after completing the IPC processing */
extern bool ipc_enter_gdb;
#ifdef CONFIG_SOF_USERSPACE_LL
/* Events that wake the user-space IPC thread */
#define IPC_USER_EVENT_CMD BIT(0)
#define IPC_USER_EVENT_STOP BIT(1)
#define IPC_USER_EVENT_XCORE BIT(2)
/**
* @brief User-space IPC thread entry point.
*
* @param p1 struct ipc_user shared with the kernel
* @param p2 Core ID
* @param p3 Unused
*/
void ipc_user_thread_fn(void *p1, void *p2, void *p3);
/**
* @brief Forward an IPC command to the user-space thread and wait for it.
*
* Protocol-agnostic: only the two raw message words are handed across the
* kernel/user boundary. The active IPC major's ipc_user_thread_dispatch()
* interprets them in the user thread.
*
* @param primary Primary message word
* @param extension Extension message word
* @return Result code from user thread processing
*/
int ipc_user_forward_cmd(uint32_t primary, uint32_t extension, unsigned int core);
/**
* @brief Reserve the cross-core request slots of the given cores.
*
* Reserve the slots before taking LL scheduler locks, if requests to the
* cores are sent while holding the locks. Otherwise a request in flight to
* such a core, that needs one of the locks, would deadlock with the sender.
*
* @param cores Bitmask of target cores, not including the current core
*/
void ipc_user_xcore_reserve(uint32_t cores);
/**
* @brief Release the slots reserved with ipc_user_xcore_reserve().
*
* @param cores Bitmask of target cores, as passed to ipc_user_xcore_reserve()
*/
void ipc_user_xcore_release(uint32_t cores);
/**
* @brief Protocol-specific dispatch of a forwarded IPC command.
*
* Runs in the user-space IPC thread. A __weak default returns -ENOSYS;
* the active IPC major (e.g. IPC4) provides the real implementation.
*
* @param ipc_user User IPC context holding the forwarded message words
* @return Result code to report back to the host
*/
int ipc_user_thread_dispatch(struct ipc_user *ipc_user);
/**
* @brief Initialize IPC and LL scheduler threads on a booting secondary core.
*
* @param core Secondary core ID
* @return 0 or a negative error code
*/
int ipc_user_init_secondary(unsigned int core);
#endif
/**
* \brief get pointer to the userspace IPC thread for core
*/
struct k_thread *ipc_thread_user(unsigned int core);
/**
* Wait until the IPC user thread of core has finished its initialization.
* Kernel context only.
*/
void ipc_user_wait_core_init(unsigned int core);
/**
* Return the IPC user thread context.
*
* Use this instead of ipc->ipc_user_pdata in kernel code: the pointer is
* kept in kernel-only storage, so user threads can not redirect it.
* Kernel context only.
*/
struct ipc_user *ipc_user_get(void);
#endif /* __SOF_DRIVERS_IPC_H__ */