Skip to content

Commit a0a7952

Browse files
lyakhkv2019i
authored andcommitted
dai: protect common properties objects
Some DAI types share properties objects between playback and capture directions and write to them and read from them with no locking. Add a DAI spinlock to protect them. Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
1 parent 235dd98 commit a0a7952

2 files changed

Lines changed: 35 additions & 7 deletions

File tree

src/audio/dai-zephyr.c

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <sof/lib/notifier.h>
2323
#include <sof/lib/uuid.h>
2424
#include <sof/list.h>
25+
#include <rtos/spinlock.h>
2526
#include <rtos/string.h>
2627
#include <sof/ut.h>
2728
#include <sof/trace/trace.h>
@@ -164,36 +165,55 @@ int dai_set_config(struct dai *dai, struct ipc_config_dai *common_config,
164165
/* called from ipc/ipc3/dai.c */
165166
int dai_get_handshake(struct dai *dai, int direction, int stream_id)
166167
{
167-
const struct dai_properties *props = dai_get_properties(dai->dev, direction, stream_id);
168+
k_spinlock_key_t key = k_spin_lock(&dai->lock);
169+
const struct dai_properties *props = dai_get_properties(dai->dev, direction,
170+
stream_id);
171+
int hs_id = props->dma_hs_id;
168172

169-
return props->dma_hs_id;
173+
k_spin_unlock(&dai->lock, key);
174+
175+
return hs_id;
170176
}
171177

172178
/* called from ipc/ipc3/dai.c and ipc/ipc4/dai.c */
173179
int dai_get_fifo_depth(struct dai *dai, int direction)
174180
{
175181
const struct dai_properties *props;
182+
k_spinlock_key_t key;
183+
int fifo_depth;
176184

177185
if (!dai)
178186
return 0;
179187

188+
key = k_spin_lock(&dai->lock);
180189
props = dai_get_properties(dai->dev, direction, 0);
190+
fifo_depth = props->fifo_depth;
191+
k_spin_unlock(&dai->lock, key);
181192

182-
return props->fifo_depth;
193+
return fifo_depth;
183194
}
184195

185196
int dai_get_stream_id(struct dai *dai, int direction)
186197
{
198+
k_spinlock_key_t key = k_spin_lock(&dai->lock);
187199
const struct dai_properties *props = dai_get_properties(dai->dev, direction, 0);
200+
int stream_id = props->stream_id;
201+
202+
k_spin_unlock(&dai->lock, key);
188203

189-
return props->stream_id;
204+
return stream_id;
190205
}
191206

192207
static int dai_get_fifo(struct dai *dai, int direction, int stream_id)
193208
{
194-
const struct dai_properties *props = dai_get_properties(dai->dev, direction, stream_id);
209+
k_spinlock_key_t key = k_spin_lock(&dai->lock);
210+
const struct dai_properties *props = dai_get_properties(dai->dev, direction,
211+
stream_id);
212+
int fifo_address = props->fifo_address;
195213

196-
return props->fifo_address;
214+
k_spin_unlock(&dai->lock, key);
215+
216+
return fifo_address;
197217
}
198218

199219
/* this is called by DMA driver every time descriptor has completed */
@@ -312,6 +332,8 @@ static struct comp_dev *dai_new(const struct comp_driver *drv,
312332
goto error;
313333
}
314334

335+
k_spinlock_init(&dd->dai->lock);
336+
315337
dma_sg_init(&dd->config.elem_array);
316338
dd->xrun = 0;
317339
dd->chan = NULL;
@@ -1374,13 +1396,18 @@ static int dai_ts_stop_op(struct comp_dev *dev)
13741396
uint32_t dai_get_init_delay_ms(struct dai *dai)
13751397
{
13761398
const struct dai_properties *props;
1399+
k_spinlock_key_t key;
1400+
uint32_t init_delay;
13771401

13781402
if (!dai)
13791403
return 0;
13801404

1405+
key = k_spin_lock(&dai->lock);
13811406
props = dai_get_properties(dai->dev, 0, 0);
1407+
init_delay = props->reg_init_delay;
1408+
k_spin_unlock(&dai->lock, key);
13821409

1383-
return props->reg_init_delay;
1410+
return init_delay;
13841411
}
13851412

13861413
static uint64_t dai_get_processed_data(struct comp_dev *dev, uint32_t stream_no, bool input)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ struct dai {
4949
uint32_t dma_dev;
5050
const struct device *dev;
5151
const struct dai_data *dd;
52+
struct k_spinlock lock; /* protect properties */
5253
};
5354

5455
struct timestamp_cfg {

0 commit comments

Comments
 (0)