Skip to content

Commit 5f2f019

Browse files
committed
audio: dai-zephyr: rework calls to DMA driver, remove channel pointer
For historical reasons, dai-zephyr has somewhat complicated code to manage the DMA channel instance information. When a DMA channel is allocated, a pointer to the system DMA channel table is acquired and some additional information is stored per channel. This is however redundant as the only piece of information actually needed is the channel index. Simplify the code by not storing the channel pointer anymore, but rather just store the channel index and use that in all calls to the DMA driver. Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
1 parent d85a793 commit 5f2f019

4 files changed

Lines changed: 42 additions & 44 deletions

File tree

src/audio/copier/copier_dai.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ __cold static int copier_dai_init(struct comp_dev *dev,
208208
if (!dd)
209209
return -ENOMEM;
210210
memset(dd, 0, sizeof(*dd));
211+
dd->chan_index = -1;
212+
comp_info(dev, "dd %p initialized, index %d", dd, dd->chan_index);
211213

212214
ret = dai_common_new(dd, dev, dai);
213215
if (ret < 0)

src/audio/dai-zephyr.c

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,6 @@ __cold int dai_common_new(struct dai_data *dd, struct comp_dev *dev,
505505

506506
dma_sg_init(&dd->config.elem_array);
507507
dd->xrun = 0;
508-
dd->chan = NULL;
509508

510509
#ifdef CONFIG_SOF_USERSPACE_LL
511510
/*
@@ -591,6 +590,8 @@ __cold static struct comp_dev *dai_new(const struct comp_driver *drv,
591590

592591
memset(dd, 0, sizeof(*dd));
593592
dd->heap = heap;
593+
dd->chan_index = -1;
594+
comp_info(dev, "dd %p initialized, index %d", dd, dd->chan_index);
594595

595596
comp_set_drvdata(dev, dd);
596597

@@ -622,10 +623,8 @@ __cold void dai_common_free(struct dai_data *dd)
622623
if (dd->group)
623624
dai_group_put(dd->group);
624625

625-
if (dd->chan) {
626-
sof_dma_release_channel(dd->dma, dd->chan->index);
627-
dd->chan->dev_data = NULL;
628-
}
626+
if (dd->chan_index != -1)
627+
sof_dma_release_channel(dd->dma, dd->chan_index);
629628

630629
sof_dma_put(dd->dma);
631630

@@ -1157,9 +1156,9 @@ int dai_common_config_prepare(struct dai_data *dd, struct comp_dev *dev)
11571156
return -EINVAL;
11581157
}
11591158

1160-
if (dd->chan) {
1159+
if (dd->chan_index != -1) {
11611160
comp_info(dev, "dma channel index %d already configured",
1162-
dd->chan->index);
1161+
dd->chan_index);
11631162
return 0;
11641163
}
11651164

@@ -1173,18 +1172,14 @@ int dai_common_config_prepare(struct dai_data *dd, struct comp_dev *dev)
11731172
}
11741173

11751174
/* get DMA channel */
1176-
channel = sof_dma_request_channel(dd->dma, channel);
1177-
if (channel < 0) {
1175+
dd->chan_index = sof_dma_request_channel(dd->dma, channel);
1176+
if (dd->chan_index < 0) {
11781177
comp_err(dev, "dma_request_channel() failed");
1179-
dd->chan = NULL;
11801178
return -EIO;
11811179
}
11821180

1183-
dd->chan = &dd->dma->chan[channel];
1184-
dd->chan->dev_data = dd;
1185-
11861181
comp_dbg(dev, "new configured dma channel index %d",
1187-
dd->chan->index);
1182+
dd->chan_index);
11881183

11891184
return 0;
11901185
}
@@ -1195,8 +1190,8 @@ int dai_common_prepare(struct dai_data *dd, struct comp_dev *dev)
11951190

11961191
dd->total_data_processed = 0;
11971192

1198-
if (!dd->chan) {
1199-
comp_err(dev, "Missing dd->chan.");
1193+
if (dd->chan_index == -1) {
1194+
comp_err(dev, "Missing dd->chan_index.");
12001195
comp_set_state(dev, COMP_TRIGGER_RESET);
12011196
return -EINVAL;
12021197
}
@@ -1217,7 +1212,7 @@ int dai_common_prepare(struct dai_data *dd, struct comp_dev *dev)
12171212
return 0;
12181213
}
12191214

1220-
ret = sof_dma_config(dd->chan->dma, dd->chan->index, dd->z_config);
1215+
ret = sof_dma_config(dd->dma, dd->chan_index, dd->z_config);
12211216
if (ret < 0)
12221217
comp_set_state(dev, COMP_TRIGGER_RESET);
12231218

@@ -1304,7 +1299,7 @@ static int dai_comp_trigger_internal(struct dai_data *dd, struct comp_dev *dev,
13041299

13051300
/* only start the DAI if we are not XRUN handling */
13061301
if (dd->xrun == 0) {
1307-
ret = sof_dma_start(dd->chan->dma, dd->chan->index);
1302+
ret = sof_dma_start(dd->dma, dd->chan_index);
13081303
if (ret < 0)
13091304
return ret;
13101305

@@ -1342,16 +1337,16 @@ static int dai_comp_trigger_internal(struct dai_data *dd, struct comp_dev *dev,
13421337
/* only start the DAI if we are not XRUN handling */
13431338
if (dd->xrun == 0) {
13441339
/* recover valid start position */
1345-
ret = sof_dma_stop(dd->chan->dma, dd->chan->index);
1340+
ret = sof_dma_stop(dd->dma, dd->chan_index);
13461341
if (ret < 0)
13471342
return ret;
13481343

13491344
/* dma_config needed after stop */
1350-
ret = sof_dma_config(dd->chan->dma, dd->chan->index, dd->z_config);
1345+
ret = sof_dma_config(dd->dma, dd->chan_index, dd->z_config);
13511346
if (ret < 0)
13521347
return ret;
13531348

1354-
ret = sof_dma_start(dd->chan->dma, dd->chan->index);
1349+
ret = sof_dma_start(dd->dma, dd->chan_index);
13551350
if (ret < 0)
13561351
return ret;
13571352

@@ -1379,11 +1374,11 @@ static int dai_comp_trigger_internal(struct dai_data *dd, struct comp_dev *dev,
13791374
* as soon as possible.
13801375
*/
13811376
#if CONFIG_COMP_DAI_STOP_TRIGGER_ORDER_REVERSE
1382-
ret = sof_dma_stop(dd->chan->dma, dd->chan->index);
1377+
ret = sof_dma_stop(dd->dma, dd->chan_index);
13831378
dai_trigger_op(dd->dai, cmd, dev->direction);
13841379
#else
13851380
dai_trigger_op(dd->dai, cmd, dev->direction);
1386-
ret = sof_dma_stop(dd->chan->dma, dd->chan->index);
1381+
ret = sof_dma_stop(dd->dma, dd->chan_index);
13871382
if (ret) {
13881383
comp_warn(dev, "dma was stopped earlier");
13891384
ret = 0;
@@ -1393,11 +1388,11 @@ static int dai_comp_trigger_internal(struct dai_data *dd, struct comp_dev *dev,
13931388
case COMP_TRIGGER_PAUSE:
13941389
comp_dbg(dev, "PAUSE");
13951390
#if CONFIG_COMP_DAI_STOP_TRIGGER_ORDER_REVERSE
1396-
ret = sof_dma_suspend(dd->chan->dma, dd->chan->index);
1391+
ret = sof_dma_suspend(dd->dma, dd->chan_index);
13971392
dai_trigger_op(dd->dai, cmd, dev->direction);
13981393
#else
13991394
dai_trigger_op(dd->dai, cmd, dev->direction);
1400-
ret = sof_dma_suspend(dd->chan->dma, dd->chan->index);
1395+
ret = sof_dma_suspend(dd->dma, dd->chan_index);
14011396
#endif
14021397
break;
14031398
case COMP_TRIGGER_PRE_START:
@@ -1495,7 +1490,7 @@ static int dai_comp_trigger(struct comp_dev *dev, int cmd)
14951490
*/
14961491
static int dai_get_status(struct comp_dev *dev, struct dai_data *dd, struct dma_status *stat)
14971492
{
1498-
int ret = sof_dma_get_status(dd->chan->dma, dd->chan->index, stat);
1493+
int ret = sof_dma_get_status(dd->dma, dd->chan_index, stat);
14991494
#if CONFIG_XRUN_NOTIFICATIONS_ENABLE
15001495
if (ret == -EPIPE && !dd->xrun_notification_sent) {
15011496
struct ipc_msg *notify = ipc_notification_pool_get(IPC4_RESOURCE_EVENT_SIZE);
@@ -1611,7 +1606,7 @@ int dai_zephyr_multi_endpoint_copy(struct dai_data **dd, struct comp_dev *dev,
16111606
#endif
16121607

16131608
for (i = 0; i < num_endpoints; i++) {
1614-
ret = sof_dma_reload(dd[i]->chan->dma, dd[i]->chan->index, 0);
1609+
ret = sof_dma_reload(dd[i]->dma, dd[i]->chan_index, 0);
16151610
if (ret < 0) {
16161611
dai_report_reload_xrun(dd[i], dev, 0);
16171612
return ret;
@@ -1637,10 +1632,10 @@ int dai_zephyr_multi_endpoint_copy(struct dai_data **dd, struct comp_dev *dev,
16371632

16381633
status = dai_dma_multi_endpoint_cb(dd[i], dev, frames, multi_endpoint_buffer);
16391634
if (status == SOF_DMA_CB_STATUS_END)
1640-
sof_dma_stop(dd[i]->chan->dma, dd[i]->chan->index);
1635+
sof_dma_stop(dd[i]->dma, dd[i]->chan_index);
16411636

16421637
copy_bytes = frames * audio_stream_frame_bytes(&dd[i]->dma_buffer->stream);
1643-
ret = sof_dma_reload(dd[i]->chan->dma, dd[i]->chan->index, copy_bytes);
1638+
ret = sof_dma_reload(dd[i]->dma, dd[i]->chan_index, copy_bytes);
16441639
if (ret < 0) {
16451640
dai_report_reload_xrun(dd[i], dev, copy_bytes);
16461641
return ret;
@@ -1829,7 +1824,7 @@ int dai_common_copy(struct dai_data *dd, struct comp_dev *dev, pcm_converter_fun
18291824
comp_warn(dev, "nothing to copy, src_frames: %u, sink_frames: %u",
18301825
src_frames, sink_frames);
18311826
#endif
1832-
sof_dma_reload(dd->chan->dma, dd->chan->index, 0);
1827+
sof_dma_reload(dd->dma, dd->chan_index, 0);
18331828
return 0;
18341829
}
18351830

@@ -1839,9 +1834,9 @@ int dai_common_copy(struct dai_data *dd, struct comp_dev *dev, pcm_converter_fun
18391834
comp_warn(dev, "dai trigger copy failed");
18401835

18411836
if (dai_dma_cb(dd, dev, copy_bytes, converter) == SOF_DMA_CB_STATUS_END)
1842-
sof_dma_stop(dd->chan->dma, dd->chan->index);
1837+
sof_dma_stop(dd->dma, dd->chan_index);
18431838

1844-
ret = sof_dma_reload(dd->chan->dma, dd->chan->index, copy_bytes);
1839+
ret = sof_dma_reload(dd->dma, dd->chan_index, copy_bytes);
18451840
if (ret < 0) {
18461841
dai_report_reload_xrun(dd, dev, copy_bytes);
18471842
return ret;
@@ -1879,7 +1874,7 @@ int dai_common_ts_config_op(struct dai_data *dd, struct comp_dev *dev)
18791874
struct dai_ts_cfg *cfg = &dd->ts_config;
18801875

18811876
comp_dbg(dev, "dai_ts_config()");
1882-
if (!dd->chan) {
1877+
if (dd->chan_index == -1) {
18831878
comp_err(dev, "No DMA channel information");
18841879
return -EINVAL;
18851880
}
@@ -1902,7 +1897,7 @@ int dai_common_ts_config_op(struct dai_data *dd, struct comp_dev *dev)
19021897
cfg->direction = dai->direction;
19031898
cfg->index = dd->dai->index;
19041899
cfg->dma_id = dd->dma->plat_data.id;
1905-
cfg->dma_chan_index = dd->chan->index;
1900+
cfg->dma_chan_index = dd->chan_index;
19061901
cfg->dma_chan_count = dd->dma->plat_data.channels;
19071902

19081903
return dai_ts_config(dd->dai->dev, cfg);

src/include/sof/lib/dai-zephyr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ typedef int (*channel_copy_func)(const struct audio_stream *src, unsigned int sr
117117
*/
118118
struct dai_data {
119119
/* local DMA config */
120-
struct dma_chan_data *chan;
120+
int chan_index;
121121
uint32_t stream_id;
122122
struct dma_sg_config config;
123123
struct dma_config *z_config;

src/ipc/ipc4/dai.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ void dai_dma_release(struct dai_data *dd, struct comp_dev *dev)
202202
}
203203

204204
/* put the allocated DMA channel first */
205-
if (dd->chan) {
205+
if (dd->chan_index != -1) {
206206
struct ipc4_llp_reading_slot slot;
207207

208208
if (dd->slot_info.node_id) {
@@ -224,15 +224,16 @@ void dai_dma_release(struct dai_data *dd, struct comp_dev *dev)
224224
*/
225225
#if CONFIG_ZEPHYR_NATIVE_DRIVERS
226226
/* if reset is after pause dma has already been stopped */
227-
dma_stop(dd->chan->dma->z_dev, dd->chan->index);
227+
dma_stop(dd->dma->z_dev, dd->chan_index);
228228

229-
dma_release_channel(dd->chan->dma->z_dev, dd->chan->index);
229+
dma_release_channel(dd->dma->z_dev, dd->chan_index);
230230
#else
231+
/* TODO: to remove this, no longer works! */
231232
dma_stop_legacy(dd->chan);
232233
dma_channel_put_legacy(dd->chan);
233-
#endif
234-
dd->chan->dev_data = NULL;
235234
dd->chan = NULL;
235+
#endif
236+
236237
}
237238
}
238239

@@ -351,9 +352,9 @@ __cold int dai_config(struct dai_data *dd, struct comp_dev *dev,
351352
return 0;
352353
}
353354

354-
if (dd->chan) {
355+
if (dd->chan_index != -1) {
355356
comp_info(dev, "Configured. dma channel index %d, ignore...",
356-
dd->chan->index);
357+
dd->chan_index);
357358
return 0;
358359
}
359360

@@ -414,7 +415,7 @@ int dai_common_position(struct dai_data *dd, struct comp_dev *dev,
414415
platform_dai_wallclock(dev, &dd->wallclock);
415416
posn->wallclock = dd->wallclock;
416417

417-
ret = dma_get_status(dd->dma->z_dev, dd->chan->index, &status);
418+
ret = dma_get_status(dd->dma->z_dev, dd->chan_index, &status);
418419
if (ret < 0)
419420
return ret;
420421

@@ -439,7 +440,7 @@ void dai_dma_position_update(struct dai_data *dd, struct comp_dev *dev)
439440
if (!dd->slot_info.node_id)
440441
return;
441442

442-
ret = dma_get_status(dd->dma->z_dev, dd->chan->index, &status);
443+
ret = dma_get_status(dd->dma->z_dev, dd->chan_index, &status);
443444
if (ret < 0)
444445
return;
445446

0 commit comments

Comments
 (0)