forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent.c
More file actions
260 lines (227 loc) · 7.49 KB
/
component.c
File metadata and controls
260 lines (227 loc) · 7.49 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
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2016 Intel Corporation. All rights reserved.
//
// Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
#include <sof/audio/component_ext.h>
#include <sof/common.h>
#include <sof/debug/panic.h>
#include <rtos/interrupt.h>
#include <sof/ipc/msg.h>
#include <rtos/alloc.h>
#include <rtos/cache.h>
#include <sof/lib/memory.h>
#include <sof/list.h>
#include <sof/sof.h>
#include <rtos/string.h>
#include <ipc/topology.h>
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
LOG_MODULE_REGISTER(component, CONFIG_SOF_LOG_LEVEL);
static SHARED_DATA struct comp_driver_list cd;
/* 7c42ce8b-0108-43d0-9137-56d660478c5f */
DECLARE_SOF_UUID("component", comp_uuid, 0x7c42ce8b, 0x0108, 0x43d0,
0x91, 0x37, 0x56, 0xd6, 0x60, 0x47, 0x8c, 0x5f);
DECLARE_TR_CTX(comp_tr, SOF_UUID(comp_uuid), LOG_LEVEL_INFO);
int comp_register(struct comp_driver_info *drv)
{
struct comp_driver_list *drivers = comp_drivers_get();
k_spinlock_key_t key;
key = k_spin_lock(&drivers->lock);
list_item_prepend(&drv->list, &drivers->list);
k_spin_unlock(&drivers->lock, key);
return 0;
}
void comp_unregister(struct comp_driver_info *drv)
{
struct comp_driver_list *drivers = comp_drivers_get();
k_spinlock_key_t key;
key = k_spin_lock(&drivers->lock);
list_item_del(&drv->list);
k_spin_unlock(&drivers->lock, key);
}
/* NOTE: Keep the component state diagram up to date:
* sof-docs/developer_guides/firmware/components/images/comp-dev-states.pu
*/
int comp_set_state(struct comp_dev *dev, int cmd)
{
int requested_state = comp_get_requested_state(cmd);
if (dev->state == requested_state) {
comp_info(dev, "comp_set_state(), state already set to %u",
dev->state);
return COMP_STATUS_STATE_ALREADY_SET;
}
switch (cmd) {
case COMP_TRIGGER_START:
if (dev->state != COMP_STATE_PRE_ACTIVE) {
comp_err(dev, "comp_set_state(): wrong state = %u, COMP_TRIGGER_START",
dev->state);
return -EINVAL;
}
break;
case COMP_TRIGGER_RELEASE:
if (dev->state != COMP_STATE_PRE_ACTIVE) {
comp_err(dev, "comp_set_state(): wrong state = %u, COMP_TRIGGER_RELEASE",
dev->state);
return -EINVAL;
}
break;
case COMP_TRIGGER_STOP:
if (dev->state != COMP_STATE_ACTIVE &&
dev->state != COMP_STATE_PAUSED) {
comp_err(dev, "comp_set_state(): wrong state = %u, COMP_TRIGGER_STOP",
dev->state);
return -EINVAL;
}
break;
case COMP_TRIGGER_PAUSE:
/* only support pausing for running */
if (dev->state != COMP_STATE_ACTIVE) {
comp_err(dev, "comp_set_state(): wrong state = %u, COMP_TRIGGER_PAUSE",
dev->state);
return -EINVAL;
}
break;
case COMP_TRIGGER_RESET:
/* reset always succeeds */
if (dev->state == COMP_STATE_ACTIVE)
comp_err(dev, "comp_set_state(): wrong state = %u, COMP_TRIGGER_RESET",
dev->state);
else if (dev->state == COMP_STATE_PAUSED)
comp_info(dev, "comp_set_state(): state = %u, COMP_TRIGGER_RESET",
dev->state);
break;
case COMP_TRIGGER_PREPARE:
if (dev->state != COMP_STATE_READY) {
comp_err(dev, "comp_set_state(): wrong state = %u, COMP_TRIGGER_PREPARE",
dev->state);
return -EINVAL;
}
break;
case COMP_TRIGGER_PRE_START:
if (dev->state != COMP_STATE_PREPARE) {
comp_err(dev,
"comp_set_state(): wrong state = %u, COMP_TRIGGER_PRE_START",
dev->state);
return -EINVAL;
}
break;
case COMP_TRIGGER_PRE_RELEASE:
if (dev->state != COMP_STATE_PAUSED) {
comp_err(dev,
"comp_set_state(): wrong state = %u, COMP_TRIGGER_PRE_RELEASE",
dev->state);
return -EINVAL;
}
break;
default:
return 0;
}
dev->state = requested_state;
return 0;
}
void sys_comp_init(struct sof *sof)
{
sof->comp_drivers = platform_shared_get(&cd, sizeof(cd));
list_init(&sof->comp_drivers->list);
k_spinlock_init(&sof->comp_drivers->lock);
}
void comp_get_copy_limits(struct comp_buffer __sparse_cache *source,
struct comp_buffer __sparse_cache *sink,
struct comp_copy_limits *cl)
{
cl->frames = audio_stream_avail_frames(&source->stream, &sink->stream);
cl->source_frame_bytes = audio_stream_frame_bytes(&source->stream);
cl->sink_frame_bytes = audio_stream_frame_bytes(&sink->stream);
cl->source_bytes = cl->frames * cl->source_frame_bytes;
cl->sink_bytes = cl->frames * cl->sink_frame_bytes;
}
void comp_get_copy_limits_frame_aligned(const struct comp_buffer __sparse_cache *source,
const struct comp_buffer __sparse_cache *sink,
struct comp_copy_limits *cl)
{
cl->frames = audio_stream_avail_frames_aligned(&source->stream, &sink->stream);
cl->source_frame_bytes = audio_stream_frame_bytes(&source->stream);
cl->sink_frame_bytes = audio_stream_frame_bytes(&sink->stream);
cl->source_bytes = cl->frames * cl->source_frame_bytes;
cl->sink_bytes = cl->frames * cl->sink_frame_bytes;
}
int audio_stream_copy(const struct audio_stream __sparse_cache *source, uint32_t ioffset,
struct audio_stream __sparse_cache *sink, uint32_t ooffset, uint32_t samples)
{
int ssize = audio_stream_sample_bytes(source); /* src fmt == sink fmt */
uint8_t *src = audio_stream_wrap(source, (uint8_t *)source->r_ptr + ioffset * ssize);
uint8_t *snk = audio_stream_wrap(sink, (uint8_t *)sink->w_ptr + ooffset * ssize);
size_t bytes = samples * ssize;
size_t bytes_src;
size_t bytes_snk;
size_t bytes_copied;
while (bytes) {
bytes_src = audio_stream_bytes_without_wrap(source, src);
bytes_snk = audio_stream_bytes_without_wrap(sink, snk);
bytes_copied = MIN(bytes_src, bytes_snk);
bytes_copied = MIN(bytes, bytes_copied);
memcpy(snk, src, bytes_copied);
bytes -= bytes_copied;
src = audio_stream_wrap(source, src + bytes_copied);
snk = audio_stream_wrap(sink, snk + bytes_copied);
}
return samples;
}
void audio_stream_copy_from_linear(const void *linear_source, int ioffset,
struct audio_stream __sparse_cache *sink, int ooffset,
unsigned int samples)
{
int ssize = audio_stream_sample_bytes(sink); /* src fmt == sink fmt */
uint8_t *src = (uint8_t *)linear_source + ioffset * ssize;
uint8_t *snk = audio_stream_wrap(sink, (uint8_t *)sink->w_ptr + ooffset * ssize);
size_t bytes = samples * ssize;
size_t bytes_snk;
size_t bytes_copied;
while (bytes) {
bytes_snk = audio_stream_bytes_without_wrap(sink, snk);
bytes_copied = MIN(bytes, bytes_snk);
memcpy(snk, src, bytes_copied);
bytes -= bytes_copied;
src += bytes_copied;
snk = audio_stream_wrap(sink, snk + bytes_copied);
}
}
void audio_stream_copy_to_linear(const struct audio_stream __sparse_cache *source, int ioffset,
void *linear_sink, int ooffset, unsigned int samples)
{
int ssize = audio_stream_sample_bytes(source); /* src fmt == sink fmt */
uint8_t *src = audio_stream_wrap(source, (uint8_t *)source->r_ptr + ioffset * ssize);
uint8_t *snk = (uint8_t *)linear_sink + ooffset * ssize;
size_t bytes = samples * ssize;
size_t bytes_src;
size_t bytes_copied;
while (bytes) {
bytes_src = audio_stream_bytes_without_wrap(source, src);
bytes_copied = MIN(bytes, bytes_src);
memcpy(snk, src, bytes_copied);
bytes -= bytes_copied;
src = audio_stream_wrap(source, src + bytes_copied);
snk += bytes_copied;
}
}
/** See comp_ops::copy */
int comp_copy(struct comp_dev *dev)
{
int ret = 0;
assert(dev->drv->ops.copy);
/* copy only if we are the owner of the component */
if (cpu_is_me(dev->ipc_config.core)) {
#if CONFIG_PERFORMANCE_COUNTERS
perf_cnt_init(&dev->pcd);
#endif
ret = dev->drv->ops.copy(dev);
#if CONFIG_PERFORMANCE_COUNTERS
perf_cnt_stamp(&dev->pcd, perf_trace_null, dev);
perf_cnt_average(&dev->pcd, comp_perf_avg_info, dev);
#endif
}
return ret;
}