Repository navigation
Expand file tree
/
Copy pathdma.c
More file actions
241 lines (202 loc) · 6.21 KB
/
Copy pathdma.c
File metadata and controls
241 lines (202 loc) · 6.21 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
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2016 Intel Corporation. All rights reserved.
//
// Author: Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
#include <sof/atomic.h>
#include <sof/audio/audio_stream.h>
#include <sof/audio/buffer.h>
#include <sof/lib/alloc.h>
#include <sof/lib/cache.h>
#include <sof/lib/dma.h>
#include <sof/lib/memory.h>
#include <sof/lib/uuid.h>
#include <sof/spinlock.h>
#include <sof/trace/trace.h>
#include <ipc/topology.h>
#include <user/trace.h>
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
LOG_MODULE_REGISTER(dma, CONFIG_SOF_LOG_LEVEL);
/* bc3526a7-9b86-4ab4-84a5-2e02ae70cc10 */
DECLARE_SOF_UUID("dma", dma_uuid, 0xbc3526a7, 0x9b86, 0x4ab4,
0x84, 0xa5, 0x2e, 0x02, 0xae, 0x70, 0xcc, 0x10);
DECLARE_TR_CTX(dma_tr, SOF_UUID(dma_uuid), LOG_LEVEL_INFO);
struct dma *dma_get(uint32_t dir, uint32_t cap, uint32_t dev, uint32_t flags)
{
const struct dma_info *info = dma_info_get();
int users, ret;
int min_users = INT32_MAX;
struct dma *d = NULL, *dmin = NULL;
k_spinlock_key_t key;
if (!info->num_dmas) {
tr_err(&dma_tr, "dma_get(): No DMACs installed");
return NULL;
}
/* find DMAC with free channels that matches request */
for (d = info->dma_array; d < info->dma_array + info->num_dmas;
d++) {
/* skip if this DMAC does not support the requested dir */
if (dir && (d->plat_data.dir & dir) == 0)
continue;
/* skip if this DMAC does not support the requested caps */
if (cap && (d->plat_data.caps & cap) == 0)
continue;
/* skip if this DMAC does not support the requested dev */
if (dev && (d->plat_data.devs & dev) == 0)
continue;
/* skip if this DMAC has 1 user per avail channel */
/* TODO: this should be fixed in dai.c to allow more users */
if (d->sref >= d->plat_data.channels)
continue;
/* if exclusive access is requested */
if (flags & DMA_ACCESS_EXCLUSIVE) {
/* ret DMA with no users */
if (!d->sref) {
dmin = d;
break;
}
} else {
/* get number of users for this DMAC*/
users = d->sref;
/* pick DMAC with the least num of users */
if (users < min_users) {
dmin = d;
min_users = users;
}
}
}
if (!dmin) {
tr_err(&dma_tr, "No DMAC dir %d caps 0x%x dev 0x%x flags 0x%x",
dir, cap, dev, flags);
for (d = info->dma_array;
d < info->dma_array + info->num_dmas;
d++) {
tr_err(&dma_tr, " DMAC ID %d users %d busy channels %ld",
d->plat_data.id, d->sref,
atomic_read(&d->num_channels_busy));
tr_err(&dma_tr, " caps 0x%x dev 0x%x",
d->plat_data.caps, d->plat_data.devs);
}
return NULL;
}
/* return DMAC */
tr_dbg(&dma_tr, "dma_get(), dma-probe id = %d",
dmin->plat_data.id);
/* Shared DMA controllers with multiple channels
* may be requested many times, let the probe()
* do on-first-use initialization.
*/
key = k_spin_lock(&dmin->lock);
ret = 0;
if (!dmin->sref) {
ret = dma_probe(dmin);
if (ret < 0) {
tr_err(&dma_tr, "dma_get(): dma-probe failed id = %d, ret = %d",
dmin->plat_data.id, ret);
}
}
if (!ret)
dmin->sref++;
tr_info(&dma_tr, "dma_get() ID %d sref = %d busy channels %ld",
dmin->plat_data.id, dmin->sref,
atomic_read(&dmin->num_channels_busy));
k_spin_unlock(&dmin->lock, key);
return !ret ? dmin : NULL;
}
void dma_put(struct dma *dma)
{
k_spinlock_key_t key;
int ret;
key = k_spin_lock(&dma->lock);
if (--dma->sref == 0) {
ret = dma_remove(dma);
if (ret < 0) {
tr_err(&dma_tr, "dma_put(): dma_remove() failed id = %d, ret = %d",
dma->plat_data.id, ret);
}
}
tr_info(&dma_tr, "dma_put(), dma = %p, sref = %d",
dma, dma->sref);
k_spin_unlock(&dma->lock, key);
}
int dma_sg_alloc(struct dma_sg_elem_array *elem_array,
enum mem_zone zone,
uint32_t direction,
uint32_t buffer_count, uint32_t buffer_bytes,
uintptr_t dma_buffer_addr, uintptr_t external_addr)
{
int i;
elem_array->elems = rzalloc(zone, 0, SOF_MEM_CAPS_RAM,
sizeof(struct dma_sg_elem) * buffer_count);
if (!elem_array->elems)
return -ENOMEM;
for (i = 0; i < buffer_count; i++) {
elem_array->elems[i].size = buffer_bytes;
// TODO: may count offsets once
switch (direction) {
case DMA_DIR_MEM_TO_DEV:
case DMA_DIR_LMEM_TO_HMEM:
elem_array->elems[i].src = dma_buffer_addr;
elem_array->elems[i].dest = external_addr;
break;
default:
elem_array->elems[i].src = external_addr;
elem_array->elems[i].dest = dma_buffer_addr;
break;
}
dma_buffer_addr += buffer_bytes;
}
elem_array->count = buffer_count;
return 0;
}
void dma_sg_free(struct dma_sg_elem_array *elem_array)
{
rfree(elem_array->elems);
dma_sg_init(elem_array);
}
int dma_buffer_copy_from(struct comp_buffer *source, struct comp_buffer *sink,
dma_process_func process, uint32_t source_bytes)
{
struct audio_stream *istream = &source->stream;
uint32_t samples = source_bytes /
audio_stream_sample_bytes(istream);
uint32_t sink_bytes = audio_stream_sample_bytes(&sink->stream) *
samples;
int ret;
/* source buffer contains data copied by DMA */
audio_stream_invalidate(istream, source_bytes);
/* process data */
ret = process(istream, 0, &sink->stream, 0, samples);
buffer_stream_writeback(sink, sink_bytes);
/*
* consume istream using audio_stream API because this buffer doesn't
* appear in topology so notifier event is not needed
*/
audio_stream_consume(istream, source_bytes);
comp_update_buffer_produce(sink, sink_bytes);
return ret;
}
int dma_buffer_copy_to(struct comp_buffer *source, struct comp_buffer *sink,
dma_process_func process, uint32_t sink_bytes)
{
struct audio_stream *ostream = &sink->stream;
uint32_t samples = sink_bytes /
audio_stream_sample_bytes(ostream);
uint32_t source_bytes = audio_stream_sample_bytes(&source->stream) *
samples;
int ret;
buffer_stream_invalidate(source, source_bytes);
/* process data */
ret = process(&source->stream, 0, ostream, 0, samples);
/* sink buffer contains data meant to copied to DMA */
audio_stream_writeback(ostream, sink_bytes);
/*
* produce ostream using audio_stream API because this buffer doesn't
* appear in topology so notifier event is not needed
*/
audio_stream_produce(ostream, sink_bytes);
comp_update_buffer_consume(source, source_bytes);
return ret;
}