forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneric.c
More file actions
524 lines (439 loc) · 13.8 KB
/
generic.c
File metadata and controls
524 lines (439 loc) · 13.8 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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2020 Intel Corporation. All rights reserved.
//
// Author: Marcin Rajwa <marcin.rajwa@linux.intel.com>
/*
* \file generic.c
* \brief Generic Codec API
* \author Marcin Rajwa <marcin.rajwa@linux.intel.com>
*
*/
#include <rtos/symbol.h>
#include <sof/audio/module_adapter/module/generic.h>
LOG_MODULE_DECLARE(module_adapter, CONFIG_SOF_LOG_LEVEL);
int module_load_config(struct comp_dev *dev, const void *cfg, size_t size)
{
int ret;
struct module_config *dst;
/* loadable module must use module adapter */
struct processing_module *mod = comp_mod(dev);
struct module_data *md = &mod->priv;
comp_dbg(dev, "module_load_config() start");
if (!cfg || !size) {
comp_err(dev, "module_load_config(): wrong input params! dev %zx, cfg %zx size %zu",
(size_t)dev, (size_t)cfg, size);
return -EINVAL;
}
dst = &md->cfg;
if (!dst->data) {
/* No space for config available yet, allocate now */
dst->data = rballoc(0, SOF_MEM_CAPS_RAM, size);
} else if (dst->size != size) {
/* The size allocated for previous config doesn't match the new one.
* Free old container and allocate new one.
*/
rfree(dst->data);
dst->data = rballoc(0, SOF_MEM_CAPS_RAM, size);
}
if (!dst->data) {
comp_err(dev, "module_load_config(): failed to allocate space for setup config.");
return -ENOMEM;
}
ret = memcpy_s(dst->data, size, cfg, size);
assert(!ret);
/* Config loaded, mark it as valid */
dst->size = size;
dst->avail = true;
comp_dbg(dev, "module_load_config() done");
return ret;
}
int module_init(struct processing_module *mod)
{
int ret;
struct module_data *md = &mod->priv;
struct comp_dev *dev = mod->dev;
const struct module_interface *const interface = dev->drv->adapter_ops;
comp_dbg(dev, "module_init() start");
#if CONFIG_IPC_MAJOR_3
if (mod->priv.state == MODULE_INITIALIZED)
return 0;
if (mod->priv.state > MODULE_INITIALIZED)
return -EPERM;
#endif
if (!interface) {
comp_err(dev, "module_init(): module interface not defined for comp id %d",
dev_comp_id(dev));
return -EIO;
}
/* check interface, there must be one and only one of processing procedure */
if (!interface->init ||
(!!interface->process + !!interface->process_audio_stream +
!!interface->process_raw_data < 1)) {
comp_err(dev, "module_init(): comp %d is missing mandatory interfaces",
dev_comp_id(dev));
return -EIO;
}
/* Init memory list */
list_init(&md->memory.mem_list);
/* Now we can proceed with module specific initialization */
ret = interface->init(mod);
if (ret) {
comp_err(dev, "module_init() error %d: module specific init failed, comp id %d",
ret, dev_comp_id(dev));
return ret;
}
comp_dbg(dev, "module_init() done");
#if CONFIG_IPC_MAJOR_3
md->state = MODULE_INITIALIZED;
#endif
return 0;
}
void *module_allocate_memory(struct processing_module *mod, uint32_t size, uint32_t alignment)
{
struct comp_dev *dev = mod->dev;
struct module_memory *container;
void *ptr;
if (!size) {
comp_err(dev, "module_allocate_memory: requested allocation of 0 bytes.");
return NULL;
}
/* Allocate memory container */
container = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM,
sizeof(struct module_memory));
if (!container) {
comp_err(dev, "module_allocate_memory: failed to allocate memory container.");
return NULL;
}
/* Allocate memory for module */
if (alignment)
ptr = rballoc_align(0, SOF_MEM_CAPS_RAM, size, alignment);
else
ptr = rballoc(0, SOF_MEM_CAPS_RAM, size);
if (!ptr) {
comp_err(dev, "module_allocate_memory: failed to allocate memory for comp %x.",
dev_comp_id(dev));
return NULL;
}
/* Store reference to allocated memory */
container->ptr = ptr;
list_item_prepend(&container->mem_list, &mod->priv.memory.mem_list);
return ptr;
}
int module_free_memory(struct processing_module *mod, void *ptr)
{
struct module_memory *mem;
struct list_item *mem_list;
struct list_item *_mem_list;
if (!ptr)
return 0;
/* Find which container keeps this memory */
list_for_item_safe(mem_list, _mem_list, &mod->priv.memory.mem_list) {
mem = container_of(mem_list, struct module_memory, mem_list);
if (mem->ptr == ptr) {
rfree(mem->ptr);
list_item_del(&mem->mem_list);
rfree(mem);
return 0;
}
}
comp_err(mod->dev, "module_free_memory: error: could not find memory pointed by %p",
ptr);
return -EINVAL;
}
int module_prepare(struct processing_module *mod,
struct sof_source **sources, int num_of_sources,
struct sof_sink **sinks, int num_of_sinks)
{
struct module_data *md = &mod->priv;
struct comp_dev *dev = mod->dev;
const struct module_interface *const ops = dev->drv->adapter_ops;
comp_dbg(dev, "module_prepare() start");
#if CONFIG_IPC_MAJOR_3
if (mod->priv.state == MODULE_IDLE)
return 0;
if (mod->priv.state < MODULE_INITIALIZED)
return -EPERM;
#endif
if (ops->prepare) {
int ret = ops->prepare(mod, sources, num_of_sources, sinks, num_of_sinks);
if (ret) {
comp_err(dev, "module_prepare() error %d: module specific prepare failed, comp_id %d",
ret, dev_comp_id(dev));
return ret;
}
}
/* After prepare is done we no longer need runtime configuration
* as it has been applied during the procedure - it is safe to
* free it.
*/
rfree(md->cfg.data);
md->cfg.avail = false;
md->cfg.data = NULL;
#if CONFIG_IPC_MAJOR_3
md->state = MODULE_IDLE;
#endif
comp_dbg(dev, "module_prepare() done");
return 0;
}
int module_process_legacy(struct processing_module *mod,
struct input_stream_buffer *input_buffers, int num_input_buffers,
struct output_stream_buffer *output_buffers,
int num_output_buffers)
{
struct comp_dev *dev = mod->dev;
const struct module_interface *const ops = dev->drv->adapter_ops;
int ret;
comp_dbg(dev, "module_process_legacy() start");
#if CONFIG_IPC_MAJOR_3
struct module_data *md = &mod->priv;
if (md->state != MODULE_IDLE) {
comp_err(dev, "module_process(): wrong state of comp_id %x, state %d",
dev_comp_id(dev), md->state);
return -EPERM;
}
/* set state to processing */
md->state = MODULE_PROCESSING;
#endif
if (IS_PROCESSING_MODE_AUDIO_STREAM(mod))
ret = ops->process_audio_stream(mod, input_buffers, num_input_buffers,
output_buffers, num_output_buffers);
else if (IS_PROCESSING_MODE_RAW_DATA(mod))
ret = ops->process_raw_data(mod, input_buffers, num_input_buffers,
output_buffers, num_output_buffers);
else
ret = -EOPNOTSUPP;
if (ret && ret != -ENOSPC && ret != -ENODATA) {
comp_err(dev, "module_process() error %d: for comp %d",
ret, dev_comp_id(dev));
return ret;
}
comp_dbg(dev, "module_process_legacy() done");
#if CONFIG_IPC_MAJOR_3
/* reset state to idle */
md->state = MODULE_IDLE;
#endif
return 0;
}
int module_process_sink_src(struct processing_module *mod,
struct sof_source **sources, int num_of_sources,
struct sof_sink **sinks, int num_of_sinks)
{
struct comp_dev *dev = mod->dev;
const struct module_interface *const ops = dev->drv->adapter_ops;
int ret;
comp_dbg(dev, "module_process sink src() start");
#if CONFIG_IPC_MAJOR_3
struct module_data *md = &mod->priv;
if (md->state != MODULE_IDLE) {
comp_err(dev, "module_process(): wrong state of comp_id %x, state %d",
dev_comp_id(dev), md->state);
return -EPERM;
}
/* set state to processing */
md->state = MODULE_PROCESSING;
#endif
assert(ops->process);
ret = ops->process(mod, sources, num_of_sources, sinks, num_of_sinks);
if (ret && ret != -ENOSPC && ret != -ENODATA) {
comp_err(dev, "module_process() error %d: for comp %d",
ret, dev_comp_id(dev));
return ret;
}
comp_dbg(dev, "module_process sink src() done");
#if CONFIG_IPC_MAJOR_3
/* reset state to idle */
md->state = MODULE_IDLE;
#endif
return 0;
}
int module_reset(struct processing_module *mod)
{
int ret;
const struct module_interface *const ops = mod->dev->drv->adapter_ops;
struct module_data *md = &mod->priv;
#if CONFIG_IPC_MAJOR_3
/* if the module was never prepared, no need to reset */
if (md->state < MODULE_IDLE)
return 0;
#endif
/* cancel task if DP task*/
if (mod->dev->ipc_config.proc_domain == COMP_PROCESSING_DOMAIN_DP && mod->dev->task)
schedule_task_cancel(mod->dev->task);
if (ops->reset) {
ret = ops->reset(mod);
if (ret) {
if (ret != PPL_STATUS_PATH_STOP)
comp_err(mod->dev,
"module_reset() error %d: module specific reset() failed for comp %d",
ret, dev_comp_id(mod->dev));
return ret;
}
}
md->cfg.avail = false;
md->cfg.size = 0;
rfree(md->cfg.data);
md->cfg.data = NULL;
#if CONFIG_IPC_MAJOR_3
/*
* reset the state to allow the module's prepare callback to be invoked again for the
* subsequent triggers
*/
md->state = MODULE_INITIALIZED;
#endif
return 0;
}
void module_free_all_memory(struct processing_module *mod)
{
struct module_memory *mem;
struct list_item *mem_list;
struct list_item *_mem_list;
/* Find which container keeps this memory */
list_for_item_safe(mem_list, _mem_list, &mod->priv.memory.mem_list) {
mem = container_of(mem_list, struct module_memory, mem_list);
rfree(mem->ptr);
list_item_del(&mem->mem_list);
rfree(mem);
}
}
int module_free(struct processing_module *mod)
{
const struct module_interface *const ops = mod->dev->drv->adapter_ops;
struct module_data *md = &mod->priv;
int ret = 0;
if (ops->free) {
ret = ops->free(mod);
if (ret)
comp_warn(mod->dev, "module_free(): error: %d for %d",
ret, dev_comp_id(mod->dev));
}
/* Free all memory shared by module_adapter & module */
md->cfg.avail = false;
md->cfg.size = 0;
rfree(md->cfg.data);
md->cfg.data = NULL;
if (md->runtime_params) {
rfree(md->runtime_params);
md->runtime_params = NULL;
}
#if CONFIG_IPC_MAJOR_3
md->state = MODULE_DISABLED;
#endif
return ret;
}
/*
* \brief Set module configuration - Common method to assemble large configuration message
* \param[in] mod - struct processing_module pointer
* \param[in] config_id - Configuration ID
* \param[in] pos - position of the fragment in the large message
* \param[in] data_offset_size: size of the whole configuration if it is the first fragment or the
* only fragment. Otherwise, it is the offset of the fragment in the
* whole configuration.
* \param[in] fragment: configuration fragment buffer
* \param[in] fragment_size: size of @fragment
* \params[in] response: optional response buffer to fill
* \params[in] response_size: size of @response
*
* \return: 0 upon success or error upon failure
*/
int module_set_configuration(struct processing_module *mod,
uint32_t config_id,
enum module_cfg_fragment_position pos, size_t data_offset_size,
const uint8_t *fragment_in, size_t fragment_size, uint8_t *response,
size_t response_size)
{
#if CONFIG_IPC_MAJOR_3
struct sof_ipc_ctrl_data *cdata = (struct sof_ipc_ctrl_data *)fragment_in;
const uint8_t *fragment = (const uint8_t *)cdata->data[0].data;
#elif CONFIG_IPC_MAJOR_4
const uint8_t *fragment = fragment_in;
#endif
struct module_data *md = &mod->priv;
struct comp_dev *dev = mod->dev;
size_t offset = 0;
uint8_t *dst;
int ret;
switch (pos) {
case MODULE_CFG_FRAGMENT_FIRST:
case MODULE_CFG_FRAGMENT_SINGLE:
/*
* verify input params & allocate memory for the config blob when the first
* fragment arrives
*/
md->new_cfg_size = data_offset_size;
/* Check that there is no previous request in progress */
if (md->runtime_params) {
comp_err(dev, "module_set_configuration(): error: busy with previous request");
return -EBUSY;
}
if (!md->new_cfg_size)
return 0;
if (md->new_cfg_size > MAX_BLOB_SIZE) {
comp_err(dev, "module_set_configuration(): error: blob size is too big cfg size %zu, allowed %d",
md->new_cfg_size, MAX_BLOB_SIZE);
return -EINVAL;
}
/* Allocate buffer for new params */
md->runtime_params = rballoc(0, SOF_MEM_CAPS_RAM, md->new_cfg_size);
if (!md->runtime_params) {
comp_err(dev, "module_set_configuration(): space allocation for new params failed");
return -ENOMEM;
}
memset(md->runtime_params, 0, md->new_cfg_size);
break;
default:
if (!md->runtime_params) {
comp_err(dev, "module_set_configuration(): error: no memory available for runtime params in consecutive load");
return -EIO;
}
/* set offset for intermediate and last fragments */
offset = data_offset_size;
break;
}
dst = (uint8_t *)md->runtime_params + offset;
ret = memcpy_s(dst, md->new_cfg_size - offset, fragment, fragment_size);
if (ret < 0) {
comp_err(dev, "module_set_configuration(): error: %d failed to copy fragment",
ret);
return ret;
}
/* return as more fragments of config data expected */
if (pos == MODULE_CFG_FRAGMENT_MIDDLE || pos == MODULE_CFG_FRAGMENT_FIRST)
return 0;
/* config fully copied, now load it */
ret = module_load_config(dev, md->runtime_params, md->new_cfg_size);
if (ret)
comp_err(dev, "module_set_configuration(): error %d: config failed", ret);
else
comp_dbg(dev, "module_set_configuration(): config load successful");
md->new_cfg_size = 0;
if (md->runtime_params)
rfree(md->runtime_params);
md->runtime_params = NULL;
return ret;
}
EXPORT_SYMBOL(module_set_configuration);
int module_bind(struct processing_module *mod, void *data)
{
const struct module_interface *const ops = mod->dev->drv->adapter_ops;
if (ops->bind)
return ops->bind(mod, data);
return 0;
}
int module_unbind(struct processing_module *mod, void *data)
{
const struct module_interface *const ops = mod->dev->drv->adapter_ops;
if (ops->unbind)
return ops->unbind(mod, data);
return 0;
}
void module_update_buffer_position(struct input_stream_buffer *input_buffers,
struct output_stream_buffer *output_buffers,
uint32_t frames)
{
struct audio_stream *source = input_buffers->data;
struct audio_stream *sink = output_buffers->data;
input_buffers->consumed += audio_stream_frame_bytes(source) * frames;
output_buffers->size += audio_stream_frame_bytes(sink) * frames;
}
EXPORT_SYMBOL(module_update_buffer_position);