Skip to content

Commit 97983d0

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 2c10bc0 commit 97983d0

3 files changed

Lines changed: 40 additions & 44 deletions

File tree

src/audio/dai-zephyr.c

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

528528
dma_sg_init(&dd->config.elem_array);
529529
dd->xrun = 0;
530-
dd->chan = NULL;
531530

532531
/* I/O performance init, keep it last so the function does not reach this in case
533532
* of return on error, so that we do not waste a slot
@@ -597,6 +596,8 @@ __cold static struct comp_dev *dai_new(const struct comp_driver *drv,
597596
if (!dd)
598597
goto e_data;
599598

599+
dd->chan_index = -EINVAL;
600+
600601
comp_set_drvdata(dev, dd);
601602

602603
ret = dai_common_new(dd, dev, dai_cfg);
@@ -627,10 +628,8 @@ __cold void dai_common_free(struct dai_data *dd)
627628
if (dd->group)
628629
dai_group_put(dd->group);
629630

630-
if (dd->chan) {
631-
sof_dma_release_channel(dd->dma, dd->chan->index);
632-
dd->chan->dev_data = NULL;
633-
}
631+
if (dd->chan_index != -EINVAL)
632+
sof_dma_release_channel(dd->dma, dd->chan_index);
634633

635634
sof_dma_put(dd->dma);
636635

@@ -1160,9 +1159,9 @@ int dai_common_config_prepare(struct dai_data *dd, struct comp_dev *dev)
11601159
return -EINVAL;
11611160
}
11621161

1163-
if (dd->chan) {
1162+
if (dd->chan_index != -EINVAL) {
11641163
comp_info(dev, "dma channel index %d already configured",
1165-
dd->chan->index);
1164+
dd->chan_index);
11661165
return 0;
11671166
}
11681167

@@ -1176,18 +1175,14 @@ int dai_common_config_prepare(struct dai_data *dd, struct comp_dev *dev)
11761175
}
11771176

11781177
/* get DMA channel */
1179-
channel = sof_dma_request_channel(dd->dma, channel);
1180-
if (channel < 0) {
1178+
dd->chan_index = sof_dma_request_channel(dd->dma, channel);
1179+
if (dd->chan_index < 0) {
11811180
comp_err(dev, "dma_request_channel() failed");
1182-
dd->chan = NULL;
11831181
return -EIO;
11841182
}
11851183

1186-
dd->chan = &dd->dma->chan[channel];
1187-
dd->chan->dev_data = dd;
1188-
11891184
comp_dbg(dev, "new configured dma channel index %d",
1190-
dd->chan->index);
1185+
dd->chan_index);
11911186

11921187
return 0;
11931188
}
@@ -1198,8 +1193,8 @@ int dai_common_prepare(struct dai_data *dd, struct comp_dev *dev)
11981193

11991194
dd->total_data_processed = 0;
12001195

1201-
if (!dd->chan) {
1202-
comp_err(dev, "Missing dd->chan.");
1196+
if (dd->chan_index == -EINVAL) {
1197+
comp_err(dev, "Missing dd->chan_index.");
12031198
comp_set_state(dev, COMP_TRIGGER_RESET);
12041199
return -EINVAL;
12051200
}
@@ -1220,7 +1215,7 @@ int dai_common_prepare(struct dai_data *dd, struct comp_dev *dev)
12201215
return 0;
12211216
}
12221217

1223-
ret = sof_dma_config(dd->chan->dma, dd->chan->index, dd->z_config);
1218+
ret = sof_dma_config(dd->dma, dd->chan_index, dd->z_config);
12241219
if (ret < 0)
12251220
comp_set_state(dev, COMP_TRIGGER_RESET);
12261221

@@ -1307,7 +1302,7 @@ static int dai_comp_trigger_internal(struct dai_data *dd, struct comp_dev *dev,
13071302

13081303
/* only start the DAI if we are not XRUN handling */
13091304
if (dd->xrun == 0) {
1310-
ret = sof_dma_start(dd->chan->dma, dd->chan->index);
1305+
ret = sof_dma_start(dd->dma, dd->chan_index);
13111306
if (ret < 0)
13121307
return ret;
13131308

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

13521347
/* dma_config needed after stop */
1353-
ret = sof_dma_config(dd->chan->dma, dd->chan->index, dd->z_config);
1348+
ret = sof_dma_config(dd->dma, dd->chan_index, dd->z_config);
13541349
if (ret < 0)
13551350
return ret;
13561351

1357-
ret = sof_dma_start(dd->chan->dma, dd->chan->index);
1352+
ret = sof_dma_start(dd->dma, dd->chan_index);
13581353
if (ret < 0)
13591354
return ret;
13601355

@@ -1382,11 +1377,11 @@ static int dai_comp_trigger_internal(struct dai_data *dd, struct comp_dev *dev,
13821377
* as soon as possible.
13831378
*/
13841379
#if CONFIG_COMP_DAI_STOP_TRIGGER_ORDER_REVERSE
1385-
ret = sof_dma_stop(dd->chan->dma, dd->chan->index);
1380+
ret = sof_dma_stop(dd->dma, dd->chan_index);
13861381
dai_trigger_op(dd->dai, cmd, dev->direction);
13871382
#else
13881383
dai_trigger_op(dd->dai, cmd, dev->direction);
1389-
ret = sof_dma_stop(dd->chan->dma, dd->chan->index);
1384+
ret = sof_dma_stop(dd->dma, dd->chan_index);
13901385
if (ret) {
13911386
comp_warn(dev, "dma was stopped earlier");
13921387
ret = 0;
@@ -1396,11 +1391,11 @@ static int dai_comp_trigger_internal(struct dai_data *dd, struct comp_dev *dev,
13961391
case COMP_TRIGGER_PAUSE:
13971392
comp_dbg(dev, "PAUSE");
13981393
#if CONFIG_COMP_DAI_STOP_TRIGGER_ORDER_REVERSE
1399-
ret = sof_dma_suspend(dd->chan->dma, dd->chan->index);
1394+
ret = sof_dma_suspend(dd->dma, dd->chan_index);
14001395
dai_trigger_op(dd->dai, cmd, dev->direction);
14011396
#else
14021397
dai_trigger_op(dd->dai, cmd, dev->direction);
1403-
ret = sof_dma_suspend(dd->chan->dma, dd->chan->index);
1398+
ret = sof_dma_suspend(dd->dma, dd->chan_index);
14041399
#endif
14051400
break;
14061401
case COMP_TRIGGER_PRE_START:
@@ -1498,7 +1493,7 @@ static int dai_comp_trigger(struct comp_dev *dev, int cmd)
14981493
*/
14991494
static int dai_get_status(struct comp_dev *dev, struct dai_data *dd, struct dma_status *stat)
15001495
{
1501-
int ret = sof_dma_get_status(dd->chan->dma, dd->chan->index, stat);
1496+
int ret = sof_dma_get_status(dd->dma, dd->chan_index, stat);
15021497
#if CONFIG_XRUN_NOTIFICATIONS_ENABLE
15031498
if (ret == -EPIPE && !dd->xrun_notification_sent) {
15041499
dd->xrun_notification_sent = send_copier_gateway_xrun_notif_msg
@@ -1603,7 +1598,7 @@ int dai_zephyr_multi_endpoint_copy(struct dai_data **dd, struct comp_dev *dev,
16031598
#endif
16041599

16051600
for (i = 0; i < num_endpoints; i++) {
1606-
ret = sof_dma_reload(dd[i]->chan->dma, dd[i]->chan->index, 0);
1601+
ret = sof_dma_reload(dd[i]->dma, dd[i]->chan_index, 0);
16071602
if (ret < 0) {
16081603
dai_report_reload_xrun(dd[i], dev, 0);
16091604
return ret;
@@ -1629,10 +1624,10 @@ int dai_zephyr_multi_endpoint_copy(struct dai_data **dd, struct comp_dev *dev,
16291624

16301625
status = dai_dma_multi_endpoint_cb(dd[i], dev, frames, multi_endpoint_buffer);
16311626
if (status == SOF_DMA_CB_STATUS_END)
1632-
sof_dma_stop(dd[i]->chan->dma, dd[i]->chan->index);
1627+
sof_dma_stop(dd[i]->dma, dd[i]->chan_index);
16331628

16341629
copy_bytes = frames * audio_stream_frame_bytes(&dd[i]->dma_buffer->stream);
1635-
ret = sof_dma_reload(dd[i]->chan->dma, dd[i]->chan->index, copy_bytes);
1630+
ret = sof_dma_reload(dd[i]->dma, dd[i]->chan_index, copy_bytes);
16361631
if (ret < 0) {
16371632
dai_report_reload_xrun(dd[i], dev, copy_bytes);
16381633
return ret;
@@ -1821,7 +1816,7 @@ int dai_common_copy(struct dai_data *dd, struct comp_dev *dev, pcm_converter_fun
18211816
comp_warn(dev, "nothing to copy, src_frames: %u, sink_frames: %u",
18221817
src_frames, sink_frames);
18231818
#endif
1824-
sof_dma_reload(dd->chan->dma, dd->chan->index, 0);
1819+
sof_dma_reload(dd->dma, dd->chan_index, 0);
18251820
return 0;
18261821
}
18271822

@@ -1831,9 +1826,9 @@ int dai_common_copy(struct dai_data *dd, struct comp_dev *dev, pcm_converter_fun
18311826
comp_warn(dev, "dai trigger copy failed");
18321827

18331828
if (dai_dma_cb(dd, dev, copy_bytes, converter) == SOF_DMA_CB_STATUS_END)
1834-
sof_dma_stop(dd->chan->dma, dd->chan->index);
1829+
sof_dma_stop(dd->dma, dd->chan_index);
18351830

1836-
ret = sof_dma_reload(dd->chan->dma, dd->chan->index, copy_bytes);
1831+
ret = sof_dma_reload(dd->dma, dd->chan_index, copy_bytes);
18371832
if (ret < 0) {
18381833
dai_report_reload_xrun(dd, dev, copy_bytes);
18391834
return ret;
@@ -1871,7 +1866,7 @@ int dai_common_ts_config_op(struct dai_data *dd, struct comp_dev *dev)
18711866
struct dai_ts_cfg *cfg = &dd->ts_config;
18721867

18731868
comp_dbg(dev, "dai_ts_config()");
1874-
if (!dd->chan) {
1869+
if (dd->chan_index == -EINVAL) {
18751870
comp_err(dev, "No DMA channel information");
18761871
return -EINVAL;
18771872
}
@@ -1894,7 +1889,7 @@ int dai_common_ts_config_op(struct dai_data *dd, struct comp_dev *dev)
18941889
cfg->direction = dai->direction;
18951890
cfg->index = dd->dai->index;
18961891
cfg->dma_id = dd->dma->plat_data.id;
1897-
cfg->dma_chan_index = dd->chan->index;
1892+
cfg->dma_chan_index = dd->chan_index;
18981893
cfg->dma_chan_count = dd->dma->plat_data.channels;
18991894

19001895
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
@@ -223,7 +223,7 @@ void dai_dma_release(struct dai_data *dd, struct comp_dev *dev)
223223
}
224224

225225
/* put the allocated DMA channel first */
226-
if (dd->chan) {
226+
if (dd->chan_index != -EINVAL) {
227227
struct ipc4_llp_reading_slot slot;
228228

229229
if (dd->slot_info.node_id) {
@@ -245,15 +245,16 @@ void dai_dma_release(struct dai_data *dd, struct comp_dev *dev)
245245
*/
246246
#if CONFIG_ZEPHYR_NATIVE_DRIVERS
247247
/* if reset is after pause dma has already been stopped */
248-
dma_stop(dd->chan->dma->z_dev, dd->chan->index);
248+
dma_stop(dd->dma->z_dev, dd->chan_index);
249249

250-
dma_release_channel(dd->chan->dma->z_dev, dd->chan->index);
250+
dma_release_channel(dd->dma->z_dev, dd->chan_index);
251251
#else
252+
/* TODO: to remove this, no longer works! */
252253
dma_stop_legacy(dd->chan);
253254
dma_channel_put_legacy(dd->chan);
254-
#endif
255-
dd->chan->dev_data = NULL;
256255
dd->chan = NULL;
256+
#endif
257+
257258
}
258259
}
259260

@@ -377,9 +378,9 @@ __cold int dai_config(struct dai_data *dd, struct comp_dev *dev,
377378
return 0;
378379
}
379380

380-
if (dd->chan) {
381+
if (dd->chan_index != -EINVAL) {
381382
comp_info(dev, "Configured. dma channel index %d, ignore...",
382-
dd->chan->index);
383+
dd->chan_index);
383384
return 0;
384385
}
385386

@@ -438,7 +439,7 @@ int dai_common_position(struct dai_data *dd, struct comp_dev *dev,
438439
platform_dai_wallclock(dev, &dd->wallclock);
439440
posn->wallclock = dd->wallclock;
440441

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

@@ -463,7 +464,7 @@ void dai_dma_position_update(struct dai_data *dd, struct comp_dev *dev)
463464
if (!dd->slot_info.node_id)
464465
return;
465466

466-
ret = dma_get_status(dd->dma->z_dev, dd->chan->index, &status);
467+
ret = dma_get_status(dd->dma->z_dev, dd->chan_index, &status);
467468
if (ret < 0)
468469
return;
469470

0 commit comments

Comments
 (0)