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
14#ifndef __SOF_AUDIO_BUFFER_H__
15#define __SOF_AUDIO_BUFFER_H__
16
19#include <sof/audio/pipeline.h>
20#include <sof/math/numbers.h>
21#include <sof/common.h>
22#include <rtos/panic.h>
23#include <rtos/alloc.h>
24#include <rtos/cache.h>
25#include <sof/list.h>
26#include <sof/coherent.h>
27#include <sof/math/numbers.h>
28#include <rtos/spinlock.h>
29#include <rtos/string.h>
30#include <sof/trace/trace.h>
31#include <ipc/stream.h>
32#include <ipc/topology.h>
33#include <user/trace.h>
34#include <sof/audio/format.h>
35
36#include <stdbool.h>
37#include <stddef.h>
38#include <stdint.h>
39
44struct comp_dev;
46
51/* buffer tracing */
52extern struct tr_ctx buffer_tr;
53
55#define buf_get_id(buf_ptr) ((buf_ptr)->stream.runtime_stream_params.id)
56
57#if defined(__ZEPHYR__) && defined(CONFIG_ZEPHYR_LOG)
58
59#if CONFIG_IPC_MAJOR_4
60#define __BUF_FMT "buf:%u %#x "
61#else
62#define __BUF_FMT "buf:%u.%u "
63#endif
64
65#define buf_err(buf_ptr, __e, ...) LOG_ERR(__BUF_FMT __e, buffer_pipeline_id(buf_ptr), \
66 buf_get_id(buf_ptr), ##__VA_ARGS__)
67
68#define buf_warn(buf_ptr, __e, ...) LOG_WRN(__BUF_FMT __e, buffer_pipeline_id(buf_ptr), \
69 buf_get_id(buf_ptr), ##__VA_ARGS__)
70
71#define buf_info(buf_ptr, __e, ...) LOG_INF(__BUF_FMT __e, buffer_pipeline_id(buf_ptr), \
72 buf_get_id(buf_ptr), ##__VA_ARGS__)
73
74#define buf_dbg(buf_ptr, __e, ...) LOG_DBG(__BUF_FMT __e, buffer_pipeline_id(buf_ptr), \
75 buf_get_id(buf_ptr), ##__VA_ARGS__)
76
77#else
79#define trace_buf_get_tr_ctx(buf_ptr) (&(buf_ptr)->tctx)
80
82#define buf_err(buf_ptr, __e, ...) \
83 trace_dev_err(trace_buf_get_tr_ctx, buffer_pipeline_id, \
84 buf_get_id, \
85 (__sparse_force const struct comp_buffer *)buf_ptr, \
86 __e, ##__VA_ARGS__)
87
89#define buf_warn(buf_ptr, __e, ...) \
90 trace_dev_warn(trace_buf_get_tr_ctx, buffer_pipeline_id, \
91 buf_get_id, \
92 (__sparse_force const struct comp_buffer *)buf_ptr, \
93 __e, ##__VA_ARGS__)
94
96#define buf_info(buf_ptr, __e, ...) \
97 trace_dev_info(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
103#if defined(CONFIG_LIBRARY)
104#define buf_dbg(buf_ptr, __e, ...)
105#else
106#define buf_dbg(buf_ptr, __e, ...) \
107 trace_dev_dbg(trace_buf_get_tr_ctx, buffer_pipeline_id, \
108 buf_get_id, \
109 (__sparse_force const struct comp_buffer *)buf_ptr, \
110 __e, ##__VA_ARGS__)
111#endif
112
113#endif /* #if defined(__ZEPHYR__) && defined(CONFIG_ZEPHYR_LOG) */
114
120#define BUFF_CB_TYPE_PRODUCE BIT(0)
121#define BUFF_CB_TYPE_CONSUME BIT(1)
127#define BUFFER_UPDATE_IF_UNSET 0
128#define BUFFER_UPDATE_FORCE 1
134#define BUFF_PARAMS_FRAME_FMT BIT(0)
135#define BUFF_PARAMS_BUFFER_FMT BIT(1)
136#define BUFF_PARAMS_RATE BIT(2)
137#define BUFF_PARAMS_CHANNELS BIT(3)
143#define BUFFER_USAGE_SHARED true
144#define BUFFER_USAGE_NOT_SHARED false
151 /* data buffer */
156 /* configuration */
157 uint32_t flags;
158 uint32_t core;
160#if !CONFIG_ZEPHYR_LOG
161 struct tr_ctx tctx;
162#endif
163
164 /* connected components */
165 struct comp_dev *source;
166 struct comp_dev *sink;
168 /* lists */
172 /* list of buffers, to be used i.e. in raw data processing mode*/
175#if CONFIG_PROBE
177 void (*probe_cb_produce)(void *arg, struct buffer_cb_transact *cb_data);
179 void (*probe_cb_free)(void *arg);
181 void *probe_cb_arg;
182#endif
183};
184
190static inline struct comp_dev *comp_buffer_get_source_component(const struct comp_buffer *buffer)
191{
192 return buffer->source;
193}
194
200static inline struct comp_dev *comp_buffer_get_sink_component(const struct comp_buffer *buffer)
201{
202 return buffer->sink;
203}
204
210static inline
211void comp_buffer_set_source_component(struct comp_buffer *buffer, struct comp_dev *comp)
212{
213 buffer->source = comp;
214}
215
221static inline
222void comp_buffer_set_sink_component(struct comp_buffer *buffer, struct comp_dev *comp)
223{
224 buffer->sink = comp;
225}
226
231static inline void comp_buffer_reset_source_list(struct comp_buffer *buffer)
232{
233 list_init(&buffer->source_list);
234}
235
240static inline void comp_buffer_reset_sink_list(struct comp_buffer *buffer)
241{
242 list_init(&buffer->sink_list);
243}
244
253
260#define buffer_from_list(ptr, dir) \
261 ((dir) == PPL_DIR_DOWNSTREAM ? \
262 container_of(ptr, struct comp_buffer, source_list) : \
263 container_of(ptr, struct comp_buffer, sink_list))
264
272#define buffer_set_cb(buffer, func, data, type) \
273 do { \
274 buffer->cb = func; \
275 buffer->cb_data = data; \
276 buffer->cb_type = type; \
277 } while (0)
278
279struct mod_alloc_ctx;
280
290struct comp_buffer *buffer_alloc(struct mod_alloc_ctx *alloc, size_t size, uint32_t flags,
291 uint32_t align, bool is_shared);
292
303struct comp_buffer *buffer_alloc_range(struct mod_alloc_ctx *alloc, size_t preferred_size,
304 size_t minimum_size,
305 uint32_t flags, uint32_t align, bool is_shared);
306
314struct comp_buffer *buffer_new(struct mod_alloc_ctx *alloc, const struct sof_ipc_buffer *desc,
315 bool is_shared);
316
324int buffer_set_size(struct comp_buffer *buffer, uint32_t size, uint32_t alignment);
325
334int buffer_set_size_range(struct comp_buffer *buffer, size_t preferred_size, size_t minimum_size,
335 uint32_t alignment);
336
337/* legacy wrappers, to be removed. Don't use them if possible */
338
345{
346 struct sof_audio_buffer *audio_buffer = sof_audio_buffer_from_source(source);
347
348 return container_of(audio_buffer, struct comp_buffer, audio_buffer);
349}
350
357{
358 struct sof_audio_buffer *audio_buffer = sof_audio_buffer_from_sink(sink);
359
360 return container_of(audio_buffer, struct comp_buffer, audio_buffer);
361}
362
367static inline void buffer_free(struct comp_buffer *buffer)
368{
370}
371
372/* end of legacy wrappers */
373
378void buffer_zero(struct comp_buffer *buffer);
379
385void comp_update_buffer_produce(struct comp_buffer *buffer, uint32_t bytes);
386
392void comp_update_buffer_consume(struct comp_buffer *buffer, uint32_t bytes);
393
402 struct sof_ipc_stream_params *params, bool force_update);
403
412 struct sof_ipc_stream_params *params, uint32_t flag);
413
419static inline void buffer_stream_invalidate(struct comp_buffer *buffer, uint32_t bytes)
420{
421#if CONFIG_INCOHERENT
423 audio_stream_invalidate(&buffer->stream, bytes);
424#endif
425}
426
432static inline void buffer_stream_writeback(struct comp_buffer *buffer, uint32_t bytes)
433{
434#if CONFIG_INCOHERENT
436 audio_stream_writeback(&buffer->stream, bytes);
437#endif
438}
439
453int buffer_attach(struct comp_buffer *buffer, struct list_item *head, int dir);
454
466void buffer_detach(struct comp_buffer *buffer, struct list_item *head, int dir);
467
474static inline struct comp_dev *buffer_get_comp(struct comp_buffer *buffer, int dir)
475{
476 struct comp_dev *comp = (dir == PPL_DIR_DOWNSTREAM) ?
479 return comp;
480}
481
487static inline uint32_t buffer_pipeline_id(const struct comp_buffer *buffer)
488{
490}
491
497static inline void buffer_init_stream(struct comp_buffer *buffer, size_t size)
498{
499 /* addr should be set in alloc function */
500 audio_stream_init(&buffer->stream, buffer->stream.addr, size);
501}
502
505#endif /* __SOF_AUDIO_BUFFER_H__ */
Memory allocation API, zones, heaps, and alignment utilities.
Audio buffer operations, descriptors, and unified audio buffer interfaces.
RTOS data and instruction cache invalidation and writeback helpers.
Cache-coherent multi-core shared memory access abstractions.
#define container_of(ptr, type, member)
Definition common.h:117
struct comp_buffer * buffer_alloc(struct mod_alloc_ctx *alloc, size_t size, uint32_t flags, uint32_t align, bool is_shared)
Allocate a pipeline component audio buffer with explicit size and alignment.
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:728
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 comp_buffer_reset_source_list(struct comp_buffer *buffer)
Reset buffer source list node.
Definition buffer.h:231
static void comp_buffer_set_source_component(struct comp_buffer *buffer, struct comp_dev *comp)
Set component providing data to the buffer.
Definition buffer.h:211
int buffer_set_size(struct comp_buffer *buffer, uint32_t size, uint32_t alignment)
Resize an existing pipeline component buffer.
int buffer_set_params(struct comp_buffer *buffer, struct sof_ipc_stream_params *params, bool force_update)
Set audio stream parameters on a component buffer.
static struct comp_buffer * comp_buffer_get_from_sink(struct sof_sink *sink)
Retrieve enclosing comp_buffer from an audio sink interface pointer.
Definition buffer.h:356
struct comp_buffer * buffer_new(struct mod_alloc_ctx *alloc, const struct sof_ipc_buffer *desc, bool is_shared)
Instantiate a pipeline component buffer from an IPC configuration descriptor.
bool buffer_params_match(struct comp_buffer *buffer, struct sof_ipc_stream_params *params, uint32_t flag)
Test if buffer stream parameters match requested parameters.
static void buffer_free(struct comp_buffer *buffer)
Free an interconnect component buffer and its underlying memory.
Definition buffer.h:367
static void buffer_stream_writeback(struct comp_buffer *buffer, uint32_t bytes)
Write back cache for a shared buffer stream if incoherent.
Definition buffer.h:432
void buffer_zero(struct comp_buffer *buffer)
Zero-fill the entire payload of an interconnect buffer.
static struct comp_buffer * comp_buffer_get_from_source(struct sof_source *source)
Retrieve enclosing comp_buffer from an audio source interface pointer.
Definition buffer.h:344
static struct comp_dev * buffer_get_comp(struct comp_buffer *buffer, int dir)
Get component connected to buffer in specified traversal direction.
Definition buffer.h:474
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)
Allocate a pipeline component buffer with an elastic size range.
static struct comp_dev * comp_buffer_get_source_component(const struct comp_buffer *buffer)
Get component providing data to the buffer.
Definition buffer.h:190
static void comp_buffer_set_sink_component(struct comp_buffer *buffer, struct comp_dev *comp)
Set component consuming data from the buffer.
Definition buffer.h:222
static bool audio_buffer_is_shared(struct sof_audio_buffer *buffer)
Check if audio buffer is shared across cores.
Definition audio_buffer.h:210
struct tr_ctx buffer_tr
void comp_update_buffer_produce(struct comp_buffer *buffer, uint32_t bytes)
Notify buffer that audio data has been produced by a component.
void comp_update_buffer_consume(struct comp_buffer *buffer, uint32_t bytes)
Notify buffer that audio data has been consumed by a component.
static struct comp_dev * comp_buffer_get_sink_component(const struct comp_buffer *buffer)
Get component consuming data from the buffer.
Definition buffer.h:200
static struct sof_audio_buffer * sof_audio_buffer_from_sink(struct sof_sink *sink)
Retrieve enclosing sof_audio_buffer from sink interface pointer.
Definition audio_buffer.h:317
static struct sof_audio_buffer * sof_audio_buffer_from_source(struct sof_source *source)
Retrieve enclosing sof_audio_buffer from source interface pointer.
Definition audio_buffer.h:331
int buffer_set_size_range(struct comp_buffer *buffer, size_t preferred_size, size_t minimum_size, uint32_t alignment)
Resize a pipeline component buffer within a preferred-to-minimum range.
static void comp_buffer_reset_sink_list(struct comp_buffer *buffer)
Reset buffer sink list node.
Definition buffer.h:240
int buffer_attach(struct comp_buffer *buffer, struct list_item *head, int dir)
Attach a new buffer at the beginning of a component buffer list.
static void buffer_init_stream(struct comp_buffer *buffer, size_t size)
Initialize circular stream buffer descriptor for a component buffer.
Definition buffer.h:497
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:751
static uint32_t buffer_pipeline_id(const struct comp_buffer *buffer)
Retrieve pipeline ID associated with a component buffer.
Definition buffer.h:487
static void buffer_stream_invalidate(struct comp_buffer *buffer, uint32_t bytes)
Invalidate cache for a shared buffer stream if incoherent.
Definition buffer.h:419
void buffer_detach(struct comp_buffer *buffer, struct list_item *head, int dir)
Detach a buffer from anywhere in a component buffer list.
void audio_buffer_free(struct sof_audio_buffer *buffer)
Free audio buffer and all allocated resources.
#define PPL_DIR_DOWNSTREAM
Downstream playback/processing direction.
Definition pipeline.h:65
static void list_init(struct list_item *list)
Definition list.h:25
Fixed-point arithmetic, saturation, rounding, and number format conversions.
Audio Stream API definition.
Audio pipeline management, graph scheduling, and execution control.
RTOS spinlock primitives and initialization.
User-space ABI log levels and binary trace packet header definitions.
Audio stream is a circular buffer aware of audio format of the data in the buffer so provides API for...
Definition audio_stream.h:51
struct sof_audio_stream_params runtime_stream_params
Definition audio_stream.h:65
void * addr
Buffer base address.
Definition audio_stream.h:58
Transaction descriptor passed to buffer probe callbacks.
Definition buffer.h:248
struct comp_buffer * buffer
Buffer being transacted.
Definition buffer.h:249
uint32_t transaction_amount
Number of bytes transacted.
Definition buffer.h:250
void * transaction_begin_address
Starting address of transaction.
Definition buffer.h:251
Audio component interconnect buffer connecting two pipeline components.
Definition buffer.h:150
struct tr_ctx tctx
Trace context.
Definition buffer.h:161
struct list_item source_list
List node in source component's buffer list.
Definition buffer.h:169
struct list_item sink_list
List node in sink component's buffer list.
Definition buffer.h:170
struct list_item buffers_list
List of buffers used in raw data processing.
Definition buffer.h:173
uint32_t core
DSP core ID managing this buffer.
Definition buffer.h:158
struct audio_stream stream
Legacy audio stream representation.
Definition buffer.h:154
struct comp_dev * source
Source component feeding this buffer.
Definition buffer.h:165
struct comp_dev * sink
Sink component consuming from this buffer.
Definition buffer.h:166
uint32_t flags
Buffer flags.
Definition buffer.h:157
struct sof_audio_buffer audio_buffer
Unified audio buffer base class.
Definition buffer.h:152
Audio component base device "class" used by all other component types.
Definition component.h:587
uint32_t size
Component allocated size in bytes.
Definition component.h:595
Definition list.h:16
Module and pipeline allocation context aggregating a Zephyr heap and optional virtual region.
Definition alloc.h:251
Base class for all audio buffers.
Definition audio_buffer.h:92
uint32_t pipeline_id
Definition audio_stream.h:51
Definition topology.h:107
Definition stream.h:71
internals of sink API.
Definition sink_api.h:141
internals of source API.
Definition source_api.h:137
Definition trace.h:132