forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipc4.c
More file actions
444 lines (378 loc) · 11.1 KB
/
ipc4.c
File metadata and controls
444 lines (378 loc) · 11.1 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
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2022 Intel Corporation. All rights reserved.
//
// Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
/*
* SOF pipeline in userspace.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/poll.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <signal.h>
#include <mqueue.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <semaphore.h>
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <limits.h>
#include <getopt.h>
#include <dlfcn.h>
#include <rtos/sof.h>
#include <rtos/task.h>
#include <sof/lib/notifier.h>
#include <sof/ipc/driver.h>
#include <sof/ipc/topology.h>
#include <sof/lib/agent.h>
#include <sof/lib/dai.h>
#include <sof/lib/dma.h>
#include <sof/schedule/edf_schedule.h>
#include <sof/schedule/ll_schedule.h>
#include <sof/schedule/ll_schedule_domain.h>
#include <sof/schedule/schedule.h>
#include <sof/audio/pipeline.h>
#include <sof/audio/component.h>
#include <sof/audio/component_ext.h>
#include "common.h"
#include "pipe.h"
// TODO: take prefix from ALSA prefix
#define COMP_PREFIX "./sof_ep/install/lib/libsof_"
#define COMP_SUFFIX ".so"
#define UUID_STR_SIZE 32
struct sof_pipe_module_library_map {
int module_id;
const char *name;
};
static const struct sof_pipe_module_library_map library_map[] = {
{0x6, "libsof_volume.so"},
{0x2, "libsof_mixer.so"},
{0x3, "libsof_mixer.so"},
/*FIXME: hack for now to set up ALSA and SHM components */
{0x96, "libsof_mod_shm.so"}, /* host playback */
{0x97, "libsof_mod_alsa.so"}, /* dai playback */
{0x98, "libsof_mod_shm.so"}, /* host capture */
{0x99, "libsof_mod_alsa.so"}, /* dai capture */
};
static int pipe_register_comp(struct sof_pipe *sp, uint16_t module_id)
{
const struct sof_pipe_module_library_map *lib;
int i;
/* check if module already loaded */
for (i = 0; i < sp->mod_idx; i++) {
if (sp->module[i].module_id == module_id)
return 0; /* module found and already loaded */
}
for (i = 0; i < ARRAY_SIZE(library_map); i++) {
lib = &library_map[i];
if (module_id == lib->module_id)
break;
}
if (i == ARRAY_SIZE(library_map)) {
fprintf(stderr, "module ID: %d not supported\n", module_id);
return -ENOTSUP;
}
/* not loaded, so load module */
sp->module[sp->mod_idx].handle = dlopen(lib->name, RTLD_NOW);
if (!sp->module[sp->mod_idx].handle) {
fprintf(stderr, "error: cant load module %s: %s\n",
lib->name, dlerror());
return -errno;
}
sp->mod_idx++;
return 0;
}
#define iCS(x) ((x) & SOF_CMD_TYPE_MASK)
#define iGS(x) ((x) & SOF_GLB_TYPE_MASK)
static int pipe_sof_ipc_cmd_before(struct sof_pipe *sp, void *mailbox, size_t bytes)
{
struct ipc4_message_request *in = mailbox;
enum ipc4_message_target target = in->primary.r.msg_tgt;
int ret = 0;
switch (target) {
case SOF_IPC4_MESSAGE_TARGET_MODULE_MSG:
{
uint32_t type = in->primary.r.type;
switch (type) {
case SOF_IPC4_MOD_INIT_INSTANCE:
struct ipc4_module_init_instance *module_init =
(struct ipc4_module_init_instance *)in;
ret = pipe_register_comp(sp, module_init->primary.r.module_id);
break;
default:
break;
}
break;
}
case SOF_IPC4_MESSAGE_TARGET_FW_GEN_MSG:
{
uint32_t type = in->primary.r.type;
switch (type) {
case SOF_IPC4_GLB_SET_PIPELINE_STATE:
{
struct ipc4_pipeline_set_state *state;
struct ipc_comp_dev *ipc_pipe;
struct ipc *ipc = ipc_get();
unsigned int pipeline_id;
state = (struct ipc4_pipeline_set_state *)in;
pipeline_id = (unsigned int)state->primary.r.ppl_id;
if (state->primary.r.ppl_state == SOF_IPC4_PIPELINE_STATE_PAUSED) {
ipc_pipe = ipc_get_pipeline_by_id(ipc, pipeline_id);
if (!ipc_pipe) {
fprintf(stderr, "No pipeline with instance_id = %u",
pipeline_id);
return -EINVAL;
}
/* stop the pipeline thread */
ret = pipe_thread_stop(sp, ipc_pipe->pipeline);
if (ret < 0) {
printf("error: can't start pipeline %d thread\n",
ipc_pipe->pipeline->comp_id);
return ret;
}
}
break;
}
default:
break;
}
break;
}
default:
fprintf(sp->log, "ipc: unknown command target %u size %ld",
target, bytes);
ret = -EINVAL;
break;
}
return ret;
}
static int pipe_sof_ipc_cmd_after(struct sof_pipe *sp, void *mailbox, size_t bytes)
{
struct ipc4_message_request *in = mailbox;
enum ipc4_message_target target = in->primary.r.msg_tgt;
int ret = 0;
switch (target) {
case SOF_IPC4_MESSAGE_TARGET_MODULE_MSG:
break;
case SOF_IPC4_MESSAGE_TARGET_FW_GEN_MSG:
{
uint32_t type = in->primary.r.type;
switch (type) {
case SOF_IPC4_GLB_CREATE_PIPELINE:
{
struct ipc4_pipeline_create *pipe_desc = (struct ipc4_pipeline_create *)in;
struct ipc_comp_dev *ipc_pipe;
struct ipc *ipc = ipc_get();
unsigned int pipeline_id = (unsigned int)pipe_desc->primary.r.instance_id;
ipc_pipe = ipc_get_pipeline_by_id(ipc, pipeline_id);
if (!ipc_pipe) {
fprintf(stderr, "No pipeline with instance_id = %u\n",
pipeline_id);
return -EINVAL;
}
/* create new pipeline thread */
ret = pipe_thread_new(sp, ipc_pipe->pipeline);
if (ret < 0) {
printf("error: can't create pipeline %d thread\n",
ipc_pipe->pipeline->pipeline_id);
return ret;
}
break;
}
case SOF_IPC4_GLB_SET_PIPELINE_STATE:
{
struct ipc4_pipeline_set_state *state;
struct ipc_comp_dev *ipc_pipe;
struct ipc *ipc = ipc_get();
unsigned int pipeline_id;
state = (struct ipc4_pipeline_set_state *)in;
pipeline_id = (unsigned int)state->primary.r.ppl_id;
if (state->primary.r.ppl_state == SOF_IPC4_PIPELINE_STATE_RUNNING) {
ipc_pipe = ipc_get_pipeline_by_id(ipc, pipeline_id);
if (!ipc_pipe) {
fprintf(stderr, "No pipeline with instance_id = %u\n",
pipeline_id);
return -EINVAL;
}
/* start the pipeline thread */
ret = pipe_thread_start(sp, ipc_pipe->pipeline);
if (ret < 0) {
printf("error: can't start pipeline %d thread\n",
ipc_pipe->pipeline->comp_id);
return ret;
}
}
break;
}
case SOF_IPC4_GLB_DELETE_PIPELINE:
{
struct ipc4_pipeline_create *pipe_desc = (struct ipc4_pipeline_create *)in;
unsigned int pipeline_id = (unsigned int)pipe_desc->primary.r.instance_id;
/* free pipeline thread */
ret = pipe_thread_free(sp, pipeline_id);
if (ret < 0) {
printf("error: can't free pipeline %d thread\n",
pipeline_id);
return ret;
}
break;
}
default:
break;
}
break;
}
default:
fprintf(sp->log, "ipc: unknown command target %u size %ld",
target, bytes);
ret = -EINVAL;
break;
}
return ret;
}
int pipe_ipc_do(struct sof_pipe *sp, void *mailbox, size_t bytes)
{
char mailbox_copy[IPC3_MAX_MSG_SIZE] = {0};
int err = 0;
/* preserve mailbox contents for local "after" config */
memcpy(mailbox_copy, mailbox, bytes);
/* some IPCs require pipe to perform actions before core */
/* mailbox can be re-written here by local pipe if needed */
err = pipe_sof_ipc_cmd_before(sp, mailbox, bytes);
if (err < 0) {
fprintf(sp->log, "error: local IPC processing failed\n");
return err;
}
/* is the IPC local only or do we need send to infra ? */
err = pipe_ipc_message(sp, mailbox, bytes);
if (err < 0) {
fprintf(sp->log, "error: infra IPC processing failed\n");
return err;
}
/* some IPCs require pipe to perform actions before core */
err = pipe_sof_ipc_cmd_after(sp, mailbox_copy, bytes);
if (err < 0) {
fprintf(sp->log, "error: local IPC processing failed\n");
return err;
}
return err;
}
int pipe_ipc_process(struct sof_pipe *sp, struct plug_mq_desc *tx_mq, struct plug_mq_desc *rx_mq)
{
ssize_t ipc_size;
char mailbox[IPC3_MAX_MSG_SIZE] = {0};
int err;
struct timespec ts;
/* IPC thread should not preempt processing thread */
err = pipe_set_ipc_lowpri(sp);
if (err < 0)
fprintf(sp->log, "error: cant set PCM IPC thread to low priority");
/* create the IPC message queue */
err = plug_mq_create(tx_mq);
if (err < 0) {
fprintf(sp->log, "error: can't create TX IPC message queue : %s\n",
strerror(errno));
return -errno;
}
/* create the IPC message queue */
err = plug_mq_create(rx_mq);
if (err < 0) {
fprintf(sp->log, "error: can't create PCM IPC message queue : %s\n",
strerror(errno));
return -errno;
}
/* let main() know we are ready */
fprintf(sp->log, "sof-pipe: IPC TX %s thread ready\n", tx_mq->queue_name);
fprintf(sp->log, "sof-pipe: IPC RX %s thread ready\n", rx_mq->queue_name);
/* main PCM IPC handling loop */
while (1) {
memset(mailbox, 0, IPC3_MAX_MSG_SIZE);
/* is client dead ? */
if (sp->glb->state == SOF_PLUGIN_STATE_DEAD) {
fprintf(sp->log, "sof-pipe: IPC %s client complete\n", tx_mq->queue_name);
break;
}
ipc_size = mq_receive(tx_mq->mq, mailbox, IPC3_MAX_MSG_SIZE, NULL);
if (ipc_size < 0) {
fprintf(sp->log, "error: can't read PCM IPC message queue %s : %s\n",
tx_mq->queue_name, strerror(errno));
break;
}
/* TODO: properly validate message and continue if garbage */
if (*((uint32_t *)mailbox) == 0) {
fprintf(sp->log, "sof-pipe: IPC %s garbage read\n", tx_mq->queue_name);
ts.tv_sec = 0;
ts.tv_nsec = 20 * 1000 * 1000; /* 20 ms */
nanosleep(&ts, NULL);
continue;
}
/* do the message work */
//data_dump(mailbox, IPC3_MAX_MSG_SIZE);
err = pipe_ipc_do(sp, mailbox, ipc_size);
if (err < 0)
fprintf(sp->log, "error: local IPC processing failed\n");
/* now return message completion status found in mailbox */
err = mq_send(rx_mq->mq, mailbox, IPC3_MAX_MSG_SIZE, 0);
if (err < 0) {
fprintf(sp->log, "error: can't send PCM IPC message queue %s : %s\n",
rx_mq->queue_name, strerror(errno));
break;
}
}
fprintf(sp->log, "***sof-pipe: IPC %s thread finished !!\n", tx_mq->queue_name);
return 0;
}
int plug_mq_cmd(struct plug_mq_desc *ipc, void *msg, size_t len, void *reply, size_t rlen)
{
struct timespec ts;
ssize_t ipc_size;
char mailbox[IPC3_MAX_MSG_SIZE];
int err;
if (len > IPC3_MAX_MSG_SIZE) {
SNDERR("ipc: message too big %d\n", len);
return -EINVAL;
}
memset(mailbox, 0, IPC3_MAX_MSG_SIZE);
memcpy(mailbox, msg, len);
/* wait for sof-pipe reader to consume data or timeout */
err = clock_gettime(CLOCK_REALTIME, &ts);
if (err == -1) {
SNDERR("ipc: cant get time: %s", strerror(errno));
return -errno;
}
/* IPCs should be read under 10ms */
plug_timespec_add_ms(&ts, 10);
/* now return message completion status */
err = mq_timedsend(ipc->mq, mailbox, IPC3_MAX_MSG_SIZE, 0, &ts);
if (err < 0) {
SNDERR("error: can't send IPC message queue %s : %s\n",
ipc->queue_name, strerror(errno));
return -errno;
}
/* wait for sof-pipe reader to consume data or timeout */
err = clock_gettime(CLOCK_REALTIME, &ts);
if (err == -1) {
SNDERR("ipc: cant get time: %s", strerror(errno));
return -errno;
}
/* IPCs should be processed under 20ms */
plug_timespec_add_ms(&ts, 20);
ipc_size = mq_timedreceive(ipc->mq, mailbox, IPC3_MAX_MSG_SIZE, NULL, &ts);
if (ipc_size < 0) {
SNDERR("error: can't read IPC message queue %s : %s\n",
ipc->queue_name, strerror(errno));
return -errno;
}
/* do the message work */
//printf("cmd got IPC %ld reply bytes\n", ipc_size);
if (rlen && reply)
memcpy(reply, mailbox, rlen);
return 0;
}