Sound Open Firmware
Loading...
Searching...
No Matches
dma.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-3-Clause
2 *
3 * Copyright(c) 2016,2024 Intel Corporation.
4 */
5
6#ifndef __SOF_LIB_DMA_H__
7#define __SOF_LIB_DMA_H__
8
9#if defined(CONFIG_SCHEDULE_DMA_MULTI_CHANNEL) || \
10 defined(CONFIG_SCHEDULE_DMA_SINGLE_CHANNEL)
11/*
12 * The platform/lib/dma.h definitions are only needed
13 * when using old dma_{single,multi}_chan_domain
14 * implementations. For new SOF build targets, it is
15 * recommended to use CONFIG_DMA_DOMAIN instead if
16 * DMA-driven scheduling is needed.
17 */
18#include <platform/lib/dma.h>
19#endif
20
21#include <rtos/atomic.h>
22#include <rtos/bit.h>
23#include <rtos/alloc.h>
24#include <sof/lib/io.h>
25#include <sof/lib/memory.h>
26#include <rtos/sof.h>
27#include <rtos/spinlock.h>
28#include <stdbool.h>
29#include <stddef.h>
30#include <stdint.h>
31
32#include <zephyr/device.h>
33#include <zephyr/drivers/dma.h>
34
35struct comp_buffer;
36struct comp_dev;
37struct k_heap;
38
44
45/*
46 * There is significant overlap with SOF DMA interface and
47 * Zephyr zephyr/drivers/dma.h. Neither uses a unique namespace
48 * prefix, leading to at times confusing mix of DMA_ and dma_
49 * definitions, some coming from legacy SOF definitions, while
50 * some from Zephyr.
51 *
52 * Definitions in this file are used by following SOF code:
53 * - Some use of SOF DMA data types and definitions exists in
54 * generic application code (in IPC, host handling and a few other
55 * places). To support both XTOS and Zephyr builds, these
56 * definitions must be provided by the RTOS layer.
57 * - SOF builds where Zephyr is used but platform has not yet
58 * moved to native Zephyr drivers, so legacy/XTOS DMA are used.
59 * - Linking DMA resources to audio usages. Even SOF builds with
60 * native Zephyr build still use some SOF side DMA definitions
61 * to describe system DMA resources in terms of their capabilities
62 * for audio use. See sof/zephyr/lib/dma.c for most of this logic.
63 */
64
65/* DMA direction bitmasks used to define DMA copy direction */
66#define SOF_DMA_DIR_MEM_TO_MEM BIT(0)
67#define SOF_DMA_DIR_HMEM_TO_LMEM BIT(1)
68#define SOF_DMA_DIR_LMEM_TO_HMEM BIT(2)
69#define SOF_DMA_DIR_MEM_TO_DEV BIT(3)
70#define SOF_DMA_DIR_DEV_TO_MEM BIT(4)
71#define SOF_DMA_DIR_DEV_TO_DEV BIT(5)
72
73/* DMA capabilities bitmasks used to define the type of DMA */
74#define SOF_DMA_CAP_HDA BIT(0)
75#define SOF_DMA_CAP_GP_LP BIT(1)
76#define SOF_DMA_CAP_GP_HP BIT(2)
77#define SOF_DMA_CAP_BT BIT(3)
78#define SOF_DMA_CAP_SP BIT(4)
79#define SOF_DMA_CAP_DMIC BIT(5)
80#define SOF_DMA_CAP_SP_VIRTUAL BIT(6)
81#define SOF_DMA_CAP_HS_VIRTUAL BIT(7)
82#define SOF_DMA_CAP_HS BIT(8)
83#define SOF_DMA_CAP_SW BIT(9)
84#define SOF_DMA_CAP_TDM BIT(10)
85
86/* DMA dev type bitmasks used to define the type of DMA */
87
88#define SOF_DMA_DEV_HOST BIT(0)
89#define SOF_DMA_DEV_HDA BIT(1)
90#define SOF_DMA_DEV_SSP BIT(2)
91#define SOF_DMA_DEV_DMIC BIT(3)
92#define SOF_DMA_DEV_SSI BIT(4)
93#define SOF_DMA_DEV_ALH BIT(5)
94#define SOF_DMA_DEV_SAI BIT(6)
95#define SOF_DMA_DEV_ESAI BIT(7)
96#define SOF_DMA_DEV_BT BIT(8)
97#define SOF_DMA_DEV_SP BIT(9)
98#define SOF_DMA_DEV_AFE_MEMIF BIT(10)
99#define SOF_DMA_DEV_SP_VIRTUAL BIT(11)
100#define SOF_DMA_DEV_HS_VIRTUAL BIT(12)
101#define SOF_DMA_DEV_HS BIT(13)
102#define SOF_DMA_DEV_MICFIL BIT(14)
103#define SOF_DMA_DEV_SW BIT(15)
104#define SOF_DMA_DEV_TDM BIT(16)
105
106/* DMA access privilege flag */
107#define SOF_DMA_ACCESS_EXCLUSIVE 1
108#define SOF_DMA_ACCESS_SHARED 0
109
110/* DMA copy flags */
111#define SOF_DMA_COPY_BLOCKING BIT(0)
112#define SOF_DMA_COPY_ONE_SHOT BIT(1)
113
114/* We will use this enum in cb handler to inform dma what
115 * action we need to perform.
116 */
121
122#define SOF_DMA_CHAN_INVALID 0xFFFFFFFF
123#define SOF_DMA_CORE_INVALID 0xFFFFFFFF
124
125/* Attributes have been ported to Zephyr. This condition is necessary until full support of
126 * CONFIG_SOF_ZEPHYR_STRICT_HEADERS.
127 */
128#ifdef CONFIG_ZEPHYR_NATIVE_DRIVERS
129struct sof_dma;
130#else
131struct dma;
132#endif
133
138 uint32_t src;
139 uint32_t dest;
140 uint32_t size;
141};
142
151
156 uint32_t count;
158};
159
160/* DMA physical SG params */
162 uint32_t src_width; /* in bytes */
163 uint32_t dest_width; /* in bytes */
164 uint32_t burst_elems;
165 uint32_t direction;
166 uint32_t src_dev;
167 uint32_t dest_dev;
168 uint32_t cyclic; /* circular buffer */
169 uint64_t period;
170 struct dma_sg_elem_array elem_array; /* array of dma_sg elems */
173 /* true if configured DMA channel is the scheduling source */
175};
176
178 uint32_t state;
179 uint32_t flags;
180 uint32_t w_pos;
181 uint32_t r_pos;
182 uint32_t timestamp;
183
184 /* dma position info for ipc4 */
186};
187
188struct dma_ops;
189/* DMA platform data */
191 uint32_t id;
192 uint32_t dir; /* bitmask of supported copy directions */
193 uint32_t caps; /* bitmask of supported capabilities */
194 uint32_t devs; /* bitmask of supported devs */
195 uint32_t base;
196 uint32_t channels;
197 int irq;
198 const char *irq_name;
199 uint32_t chan_size;
200 const void *drv_plat_data;
201 uint32_t period_count;
202};
203
204#ifdef CONFIG_ZEPHYR_NATIVE_DRIVERS
205struct k_heap;
206struct sof_dma {
207 struct k_heap *heap;
208#else
209struct dma {
210#endif
212 struct k_spinlock lock;
213 int sref;
214 const struct dma_ops *ops;
215 atomic_t num_channels_busy; /* number of busy channels */
216 struct dma_chan_data *chan; /* channels array */
217 const struct device *z_dev; /* Zephyr driver */
219};
220
222#ifdef CONFIG_ZEPHYR_NATIVE_DRIVERS
223 struct sof_dma *dma;
224#else
225 struct dma *dma;
226#endif
227
228 uint32_t status;
229 uint32_t direction;
230 uint32_t desc_count;
231 uint32_t index;
232 uint32_t core;
233 uint64_t period; /* DMA channel's transfer period in us */
234 /* true if this DMA channel is the scheduling source */
236
237 /* device specific data set by the device that requests the DMA channel */
238 void *dev_data;
239
241};
242
243struct dma_info {
244#ifdef CONFIG_ZEPHYR_NATIVE_DRIVERS
245 struct sof_dma *dma_array;
246#else
247 struct dma *dma_array;
248#endif
249 size_t num_dmas;
250};
251
252/* generic DMA DSP <-> Host copier */
253struct dma_copy {
255#ifdef CONFIG_ZEPHYR_NATIVE_DRIVERS
256 struct sof_dma *dmac;
257#else
258 struct dma *dmac;
259#endif
260};
261
262struct cir_buf_source;
263struct cir_buf_sink;
264typedef int (*dma_process_func)(const struct cir_buf_source *source,
265 uint32_t src_channels, struct cir_buf_sink *sink,
266 uint32_t sink_channels, size_t source_samples, uint32_t chmap);
267
273int dmac_init(struct sof *sof);
274
275/*
276 * Need to use sof_dma.h to avoid "syscalls/dma.h" name conflict
277 * with Zephyr autogenerated headers for syscall support.
278 */
279#ifdef CONFIG_ZEPHYR_NATIVE_DRIVERS
280#include "sof_dma.h"
281#else
282#include "dma-legacy.h"
283#endif /* !CONFIG_ZEPHYR_NATIVE_DRIVERS */
284
285#if defined(CONFIG_SCHEDULE_DMA_MULTI_CHANNEL) || \
286 defined(CONFIG_SCHEDULE_DMA_SINGLE_CHANNEL)
287
288static inline bool dma_is_scheduling_source(struct dma_chan_data *channel)
289{
290 return channel->is_scheduling_source;
291}
292#endif
293
294static inline void dma_sg_init(struct dma_sg_elem_array *ea)
295{
296 ea->count = 0;
297 ea->elems = NULL;
298}
299
300int dma_sg_alloc(struct k_heap *heap,
301 struct dma_sg_elem_array *ea,
302 uint32_t flags,
303 uint32_t direction,
304 uint32_t buffer_count, uint32_t buffer_bytes,
305 uintptr_t dma_buffer_addr, uintptr_t external_addr);
306
307void dma_sg_free(struct k_heap *heap, struct dma_sg_elem_array *ea);
308
315static inline uint32_t dma_sg_get_size(struct dma_sg_elem_array *ea)
316{
317 int i;
318 uint32_t size = 0;
319
320 for (i = 0 ; i < ea->count; i++)
321 size += ea->elems[i].size;
322
323 return size;
324}
325
326/* copies data from DMA buffer using provided processing function */
328 struct comp_buffer *sink,
329 dma_process_func process, uint32_t source_bytes, uint32_t chmap);
330
331/*
332 * Used when copying stream audio into multiple sink buffers, one at a time using the provided
333 * conversion function. DMA buffer consume should be performed after the data has been copied
334 * to all sinks.
335 */
337 struct comp_buffer *source,
338 struct comp_buffer *sink,
339 dma_process_func process,
340 uint32_t source_bytes, uint32_t chmap);
341
342/* copies data to DMA buffer using provided processing function */
344 struct comp_buffer *sink,
345 dma_process_func process, uint32_t sink_bytes, uint32_t chmap);
346
347
348static inline const struct dma_info *dma_info_get(void)
349{
350 return sof_get()->dma_info;
351}
352
354
355#endif /* __SOF_LIB_DMA_H__ */
int dma_buffer_copy_to(struct comp_buffer *source, struct comp_buffer *sink, dma_process_func process, uint32_t sink_bytes, uint32_t chmap)
int dma_buffer_copy_from(struct comp_buffer *source, struct comp_buffer *sink, dma_process_func process, uint32_t source_bytes, uint32_t chmap)
void dma_sg_free(struct k_heap *heap, struct dma_sg_elem_array *ea)
static uint32_t dma_sg_get_size(struct dma_sg_elem_array *ea)
Get the total size of SG buffer.
Definition dma.h:315
int stream_copy_from_no_consume(struct comp_dev *dev, struct comp_buffer *source, struct comp_buffer *sink, dma_process_func process, uint32_t source_bytes, uint32_t chmap)
static void dma_sg_init(struct dma_sg_elem_array *ea)
Definition dma.h:294
int dma_sg_alloc(struct k_heap *heap, struct dma_sg_elem_array *ea, uint32_t flags, uint32_t direction, uint32_t buffer_count, uint32_t buffer_bytes, uintptr_t dma_buffer_addr, uintptr_t external_addr)
sof_dma_cb_status
Definition dma.h:117
int dmac_init(struct sof *sof)
API to initialize a platform DMA controllers.
static const struct dma_info * dma_info_get(void)
Definition dma.h:348
int(* dma_process_func)(const struct cir_buf_source *source, uint32_t src_channels, struct cir_buf_sink *sink, uint32_t sink_channels, size_t source_samples, uint32_t chmap)
Definition dma.h:264
@ SOF_DMA_CB_STATUS_RELOAD
Definition dma.h:118
@ SOF_DMA_CB_STATUS_END
Definition dma.h:119
struct sof * sof_get(void)
Definition buffer.h:130
Audio component base device "class".
Definition component.h:645
Data used in DMA callbacks.
Definition dma.h:146
struct dma_chan_data * channel
Definition dma.h:147
enum sof_dma_cb_status status
Definition dma.h:149
struct dma_sg_elem elem
Definition dma.h:148
Definition dma.h:221
uint32_t desc_count
Definition dma.h:230
uint32_t core
Definition dma.h:232
uint32_t index
Definition dma.h:231
void * priv_data
Definition dma.h:240
uint32_t direction
Definition dma.h:229
uint32_t status
Definition dma.h:228
uint64_t period
Definition dma.h:233
void * dev_data
Definition dma.h:238
struct dma * dma
Definition dma.h:225
bool is_scheduling_source
Definition dma.h:235
Definition dma.h:177
uint32_t timestamp
Definition dma.h:182
uint32_t w_pos
Definition dma.h:180
void * ipc_posn_data
Definition dma.h:185
uint32_t flags
Definition dma.h:179
uint32_t r_pos
Definition dma.h:181
uint32_t state
Definition dma.h:178
Definition dma.h:253
struct dma * dmac
Definition dma.h:258
struct dma_chan_data * chan
Definition dma.h:254
Definition dma.h:243
size_t num_dmas
Definition dma.h:249
struct dma * dma_array
Definition dma.h:247
Definition dma-legacy.h:47
Definition dma.h:190
int irq
Definition dma.h:197
uint32_t caps
Definition dma.h:193
uint32_t chan_size
Definition dma.h:199
uint32_t period_count
Definition dma.h:201
uint32_t devs
Definition dma.h:194
const void * drv_plat_data
Definition dma.h:200
uint32_t channels
Definition dma.h:196
uint32_t id
Definition dma.h:191
const char * irq_name
Definition dma.h:198
uint32_t dir
Definition dma.h:192
uint32_t base
Definition dma.h:195
Definition dma.h:161
uint32_t dest_width
Definition dma.h:163
bool scatter
Definition dma.h:171
struct dma_sg_elem_array elem_array
Definition dma.h:170
uint64_t period
Definition dma.h:169
uint32_t direction
Definition dma.h:165
uint32_t burst_elems
Definition dma.h:164
bool is_scheduling_source
Definition dma.h:174
uint32_t dest_dev
Definition dma.h:167
uint32_t src_width
Definition dma.h:162
bool irq_disabled
Definition dma.h:172
uint32_t src_dev
Definition dma.h:166
uint32_t cyclic
Definition dma.h:168
SG elem array.
Definition dma.h:155
uint32_t count
number of elements in elems
Definition dma.h:156
struct dma_sg_elem * elems
elements
Definition dma.h:157
Element of SG list (as array item).
Definition dma.h:137
uint32_t dest
destination address
Definition dma.h:139
uint32_t src
source address
Definition dma.h:138
uint32_t size
size (in bytes)
Definition dma.h:140
Definition dma.h:209
struct dma_chan_data * chan
Definition dma.h:216
int sref
simple ref counter, guarded by lock
Definition dma.h:213
struct dma_plat_data plat_data
Definition dma.h:211
const struct dma_ops * ops
Definition dma.h:214
atomic_t num_channels_busy
Definition dma.h:215
void * priv_data
Definition dma.h:218
const struct device * z_dev
Definition dma.h:217
struct k_spinlock lock
locking mechanism
Definition dma.h:212
General firmware context.
Definition sof.h:44
const struct dma_info * dma_info
Definition sof.h:90