Sound Open Firmware
Loading...
Searching...
No Matches
buffer.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-3-Clause
2 *
3 * Copyright(c) 2017 Intel Corporation. All rights reserved.
4 *
5 * Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
6 */
7
8#ifndef __SOF_AUDIO_BUFFER_H__
9#define __SOF_AUDIO_BUFFER_H__
10
13#include <sof/audio/pipeline.h>
14#include <sof/math/numbers.h>
15#include <sof/common.h>
16#include <rtos/panic.h>
17#include <rtos/alloc.h>
18#include <rtos/cache.h>
19#include <sof/list.h>
20#include <sof/coherent.h>
21#include <sof/math/numbers.h>
22#include <rtos/spinlock.h>
23#include <rtos/string.h>
24#include <sof/trace/trace.h>
25#include <ipc/stream.h>
26#include <ipc/topology.h>
27#include <user/trace.h>
28#include <sof/audio/format.h>
29
30#include <stdbool.h>
31#include <stddef.h>
32#include <stdint.h>
33
34struct comp_dev;
36
40
41/* buffer tracing */
42extern struct tr_ctx buffer_tr;
43
45#define trace_buf_get_tr_ctx(buf_ptr) (&(buf_ptr)->tctx)
46
48#define buf_get_id(buf_ptr) ((buf_ptr)->stream.runtime_stream_params.id)
49
50#if defined(__ZEPHYR__) && defined(CONFIG_ZEPHYR_LOG)
51
52#if CONFIG_IPC_MAJOR_4
53#define __BUF_FMT "buf:%u %#x "
54#else
55#define __BUF_FMT "buf:%u.%u "
56#endif
57
58#define buf_err(buf_ptr, __e, ...) LOG_ERR(__BUF_FMT __e, buffer_pipeline_id(buf_ptr), \
59 buf_get_id(buf_ptr), ##__VA_ARGS__)
60
61#define buf_warn(buf_ptr, __e, ...) LOG_WRN(__BUF_FMT __e, buffer_pipeline_id(buf_ptr), \
62 buf_get_id(buf_ptr), ##__VA_ARGS__)
63
64#define buf_info(buf_ptr, __e, ...) LOG_INF(__BUF_FMT __e, buffer_pipeline_id(buf_ptr), \
65 buf_get_id(buf_ptr), ##__VA_ARGS__)
66
67#define buf_dbg(buf_ptr, __e, ...) LOG_DBG(__BUF_FMT __e, buffer_pipeline_id(buf_ptr), \
68 buf_get_id(buf_ptr), ##__VA_ARGS__)
69
70#else
72#define buf_err(buf_ptr, __e, ...) \
73 trace_dev_err(trace_buf_get_tr_ctx, buffer_pipeline_id, \
74 buf_get_id, \
75 (__sparse_force const struct comp_buffer *)buf_ptr, \
76 __e, ##__VA_ARGS__)
77
79#define buf_warn(buf_ptr, __e, ...) \
80 trace_dev_warn(trace_buf_get_tr_ctx, buffer_pipeline_id, \
81 buf_get_id, \
82 (__sparse_force const struct comp_buffer *)buf_ptr, \
83 __e, ##__VA_ARGS__)
84
86#define buf_info(buf_ptr, __e, ...) \
87 trace_dev_info(trace_buf_get_tr_ctx, buffer_pipeline_id, \
88 buf_get_id, \
89 (__sparse_force const struct comp_buffer *)buf_ptr, \
90 __e, ##__VA_ARGS__)
91
93#if defined(CONFIG_LIBRARY)
94#define buf_dbg(buf_ptr, __e, ...)
95#else
96#define buf_dbg(buf_ptr, __e, ...) \
97 trace_dev_dbg(trace_buf_get_tr_ctx, buffer_pipeline_id, \
98 buf_get_id, \
99 (__sparse_force const struct comp_buffer *)buf_ptr, \
100 __e, ##__VA_ARGS__)
101#endif
102
103#endif /* #if defined(__ZEPHYR__) && defined(CONFIG_ZEPHYR_LOG) */
104
106
107/* buffer callback types */
108#define BUFF_CB_TYPE_PRODUCE BIT(0)
109#define BUFF_CB_TYPE_CONSUME BIT(1)
110
111#define BUFFER_UPDATE_IF_UNSET 0
112#define BUFFER_UPDATE_FORCE 1
113
114/* buffer parameters */
115#define BUFF_PARAMS_FRAME_FMT BIT(0)
116#define BUFF_PARAMS_BUFFER_FMT BIT(1)
117#define BUFF_PARAMS_RATE BIT(2)
118#define BUFF_PARAMS_CHANNELS BIT(3)
119
120/* buffer usage */
121#define BUFFER_USAGE_SHARED true /* buffer used by multiple DSP core and/or HW blocks */
122#define BUFFER_USAGE_NOT_SHARED false /* buffer only used by one HW block */
123
124/*
125 * audio component buffer - connects 2 audio components together in pipeline.
126 *
127 * this is a legacy component connector for pipeline 1.0
128 *
129 */
131 /* data buffer */
133
135
136 /* configuration */
137 uint32_t flags;
138 uint32_t core;
139 struct tr_ctx tctx; /* trace settings */
140
141 /* connected components */
142 struct comp_dev *source; /* source component */
143 struct comp_dev *sink; /* sink component */
144
145 /* lists */
146 struct list_item source_list; /* list in comp buffers */
147 struct list_item sink_list; /* list in comp buffers */
148
149 /* list of buffers, to be used i.e. in raw data processing mode*/
151
152#if CONFIG_PROBE
154 void (*probe_cb_produce)(void *arg, struct buffer_cb_transact *cb_data);
156 void (*probe_cb_free)(void *arg);
158 void *probe_cb_arg;
159#endif
160};
161
162/*
163 * get a component providing data to the buffer
164 */
165static inline struct comp_dev *comp_buffer_get_source_component(const struct comp_buffer *buffer)
166{
167 return buffer->source;
168}
169
170/*
171 * get a component consuming data from the buffer
172 */
173static inline struct comp_dev *comp_buffer_get_sink_component(const struct comp_buffer *buffer)
174{
175 return buffer->sink;
176}
177
178static inline
179void comp_buffer_set_source_component(struct comp_buffer *buffer, struct comp_dev *comp)
180{
181 buffer->source = comp;
182}
183
184static inline
185void comp_buffer_set_sink_component(struct comp_buffer *buffer, struct comp_dev *comp)
186{
187 buffer->sink = comp;
188}
189
190static inline void comp_buffer_reset_source_list(struct comp_buffer *buffer)
191{
192 list_init(&buffer->source_list);
193}
194
195static inline void comp_buffer_reset_sink_list(struct comp_buffer *buffer)
196{
197 list_init(&buffer->sink_list);
198}
199
200/* Used as parameter for probe produce callback */
206
207#define buffer_from_list(ptr, dir) \
208 ((dir) == PPL_DIR_DOWNSTREAM ? \
209 container_of(ptr, struct comp_buffer, source_list) : \
210 container_of(ptr, struct comp_buffer, sink_list))
211
212#define buffer_set_cb(buffer, func, data, type) \
213 do { \
214 buffer->cb = func; \
215 buffer->cb_data = data; \
216 buffer->cb_type = type; \
217 } while (0)
218
219struct mod_alloc_ctx;
220
221/* pipeline buffer creation and destruction */
222struct comp_buffer *buffer_alloc(struct mod_alloc_ctx *alloc, size_t size, uint32_t flags,
223 uint32_t align, bool is_shared);
224struct comp_buffer *buffer_alloc_range(struct mod_alloc_ctx *alloc, size_t preferred_size,
225 size_t minimum_size,
226 uint32_t flags, uint32_t align, bool is_shared);
227struct comp_buffer *buffer_new(struct mod_alloc_ctx *alloc, const struct sof_ipc_buffer *desc,
228 bool is_shared);
229
230int buffer_set_size(struct comp_buffer *buffer, uint32_t size, uint32_t alignment);
231int buffer_set_size_range(struct comp_buffer *buffer, size_t preferred_size, size_t minimum_size,
232 uint32_t alignment);
233
234/* legacy wrappers, to be removed. Don't use them if possible */
235static inline struct comp_buffer *comp_buffer_get_from_source(struct sof_source *source)
236{
237 struct sof_audio_buffer *audio_buffer = sof_audio_buffer_from_source(source);
238
239 return container_of(audio_buffer, struct comp_buffer, audio_buffer);
240}
241
242static inline struct comp_buffer *comp_buffer_get_from_sink(struct sof_sink *sink)
243{
244 struct sof_audio_buffer *audio_buffer = sof_audio_buffer_from_sink(sink);
245
246 return container_of(audio_buffer, struct comp_buffer, audio_buffer);
247}
248
249static inline void buffer_free(struct comp_buffer *buffer)
250{
252}
253
254/* end of legacy wrappers */
255
256void buffer_zero(struct comp_buffer *buffer);
257
258/* called by a component after producing data into this buffer */
259void comp_update_buffer_produce(struct comp_buffer *buffer, uint32_t bytes);
260
261/* called by a component after consuming data from this buffer */
262void comp_update_buffer_consume(struct comp_buffer *buffer, uint32_t bytes);
263
265 struct sof_ipc_stream_params *params, bool force_update);
266
268 struct sof_ipc_stream_params *params, uint32_t flag);
269
270static inline void buffer_stream_invalidate(struct comp_buffer *buffer, uint32_t bytes)
271{
272#if CONFIG_INCOHERENT
274 audio_stream_invalidate(&buffer->stream, bytes);
275#endif
276}
277
278static inline void buffer_stream_writeback(struct comp_buffer *buffer, uint32_t bytes)
279{
280#if CONFIG_INCOHERENT
282 audio_stream_writeback(&buffer->stream, bytes);
283#endif
284}
285
286
287/*
288 * Attach a new buffer at the beginning of the list. Note, that "head" must
289 * really be the head of the list, not a list head within another buffer. We
290 * don't synchronise its cache, so it must not be embedded in an object, using
291 * the coherent API. The caller takes care to protect list heads.
292 *
293 * Returns -EINVAL if the buffer is already linked in this direction
294 * (re-attaching would create a self-loop and corrupt the list).
295 */
296int buffer_attach(struct comp_buffer *buffer, struct list_item *head, int dir);
297
298/*
299 * Detach a buffer from anywhere in the list. "head" is again the head of the
300 * list, we need it to verify, whether this buffer was the first or the last on
301 * the list. Again it is assumed that the head's cache doesn't need to be
302 * synchronized. The caller takes care to protect list heads.
303 */
304void buffer_detach(struct comp_buffer *buffer, struct list_item *head, int dir);
305
306static inline struct comp_dev *buffer_get_comp(struct comp_buffer *buffer, int dir)
307{
308 struct comp_dev *comp = (dir == PPL_DIR_DOWNSTREAM) ?
311 return comp;
312}
313
314static inline uint32_t buffer_pipeline_id(const struct comp_buffer *buffer)
315{
316 return buffer->stream.runtime_stream_params.pipeline_id;
317}
318
319/* Run-time buffer re-configuration calls this too, so it must use cached access */
320static inline void buffer_init_stream(struct comp_buffer *buffer, size_t size)
321{
322 /* addr should be set in alloc function */
323 audio_stream_init(&buffer->stream, buffer->stream.addr, size);
324}
325
326#endif /* __SOF_AUDIO_BUFFER_H__ */
static bool audio_buffer_is_shared(struct sof_audio_buffer *buffer)
Definition audio_buffer.h:213
static struct sof_audio_buffer * sof_audio_buffer_from_sink(struct sof_sink *sink)
return a pointer to struct sof_audio_buffer from sink pointer NOTE!
Definition audio_buffer.h:275
static struct sof_audio_buffer * sof_audio_buffer_from_source(struct sof_source *source)
return a pointer to struct sof_audio_buffer from source pointer NOTE!
Definition audio_buffer.h:285
void audio_buffer_free(struct sof_audio_buffer *buffer)
free buffer and all allocated resources
Audio Stream API definition.
struct comp_buffer * buffer_alloc(struct mod_alloc_ctx *alloc, size_t size, uint32_t flags, uint32_t align, bool is_shared)
static void comp_buffer_reset_source_list(struct comp_buffer *buffer)
Definition buffer.h:190
static void comp_buffer_set_source_component(struct comp_buffer *buffer, struct comp_dev *comp)
Definition buffer.h:179
int buffer_set_size(struct comp_buffer *buffer, uint32_t size, uint32_t alignment)
int buffer_set_params(struct comp_buffer *buffer, struct sof_ipc_stream_params *params, bool force_update)
static struct comp_buffer * comp_buffer_get_from_sink(struct sof_sink *sink)
Definition buffer.h:242
struct comp_buffer * buffer_new(struct mod_alloc_ctx *alloc, const struct sof_ipc_buffer *desc, bool is_shared)
bool buffer_params_match(struct comp_buffer *buffer, struct sof_ipc_stream_params *params, uint32_t flag)
static void buffer_free(struct comp_buffer *buffer)
Definition buffer.h:249
static void buffer_stream_writeback(struct comp_buffer *buffer, uint32_t bytes)
Definition buffer.h:278
void buffer_zero(struct comp_buffer *buffer)
static struct comp_buffer * comp_buffer_get_from_source(struct sof_source *source)
Definition buffer.h:235
static struct comp_dev * buffer_get_comp(struct comp_buffer *buffer, int dir)
Definition buffer.h:306
struct comp_buffer * buffer_alloc_range(struct mod_alloc_ctx *alloc, size_t preferred_size, size_t minimum_size, uint32_t flags, uint32_t align, bool is_shared)
static struct comp_dev * comp_buffer_get_source_component(const struct comp_buffer *buffer)
Definition buffer.h:165
static void comp_buffer_set_sink_component(struct comp_buffer *buffer, struct comp_dev *comp)
Definition buffer.h:185
struct tr_ctx buffer_tr
void comp_update_buffer_produce(struct comp_buffer *buffer, uint32_t bytes)
void comp_update_buffer_consume(struct comp_buffer *buffer, uint32_t bytes)
static struct comp_dev * comp_buffer_get_sink_component(const struct comp_buffer *buffer)
Definition buffer.h:173
int buffer_set_size_range(struct comp_buffer *buffer, size_t preferred_size, size_t minimum_size, uint32_t alignment)
static void comp_buffer_reset_sink_list(struct comp_buffer *buffer)
Definition buffer.h:195
int buffer_attach(struct comp_buffer *buffer, struct list_item *head, int dir)
static void buffer_init_stream(struct comp_buffer *buffer, size_t size)
Definition buffer.h:320
static uint32_t buffer_pipeline_id(const struct comp_buffer *buffer)
Definition buffer.h:314
static void buffer_stream_invalidate(struct comp_buffer *buffer, uint32_t bytes)
Definition buffer.h:270
void buffer_detach(struct comp_buffer *buffer, struct list_item *head, int dir)
#define container_of(ptr, type, member)
Definition common.h:119
static void audio_stream_invalidate(struct audio_stream *buffer, uint32_t bytes)
Invalidates (in DSP d-cache) the buffer in range [r_ptr, r_ptr+bytes], with rollover if necessary.
Definition audio_stream.h:758
void audio_stream_init(struct audio_stream *audio_stream, void *buff_addr, uint32_t size)
Initializes the buffer with specified memory block and size.
static void audio_stream_writeback(struct audio_stream *buffer, uint32_t bytes)
Writes back (from DSP d-cache) the buffer in range [w_ptr, w_ptr+bytes], with rollover if necessary.
Definition audio_stream.h:781
IPC definitions for streams.
static void list_init(struct list_item *list)
Definition list.h:25
#define list_item(item, type, member)
Definition list.h:80
#define PPL_DIR_DOWNSTREAM
Definition pipeline.h:49
Audio stream is a circular buffer aware of audio format of the data in the buffer so provides API for...
Definition audio_stream.h:52
struct sof_audio_stream_params runtime_stream_params
Definition audio_stream.h:66
void * addr
Buffer base address.
Definition audio_stream.h:59
Definition buffer.h:201
struct comp_buffer * buffer
Definition buffer.h:202
uint32_t transaction_amount
Definition buffer.h:203
void * transaction_begin_address
Definition buffer.h:204
Definition buffer.h:130
struct tr_ctx tctx
Definition buffer.h:139
struct list_item source_list
Definition buffer.h:146
struct list_item sink_list
Definition buffer.h:147
struct list_item buffers_list
Definition buffer.h:150
uint32_t core
Definition buffer.h:138
struct audio_stream stream
Definition buffer.h:134
struct comp_dev * source
Definition buffer.h:142
struct comp_dev * sink
Definition buffer.h:143
uint32_t flags
Definition buffer.h:137
struct sof_audio_buffer audio_buffer
Definition buffer.h:132
Audio component base device "class".
Definition component.h:645
uint32_t size
component's allocated size
Definition component.h:657
Definition alloc.h:167
Definition audio_buffer.h:65
Definition topology.h:107
Definition stream.h:71
Definition trace.h:135