Skip to content

Commit 57ad498

Browse files
committed
audio: dai-zephyr: convert spinlock into mutex for properties
The spinlock used to protect access to DAI properties can be converted to a mutex as this is only accessed from IPC and LL threads and both are normal Zephyr threads. As an additional benefit, use of mutex allows to run the dai-zephyr module in user-space. Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
1 parent 5f2f019 commit 57ad498

2 files changed

Lines changed: 36 additions & 22 deletions

File tree

src/audio/dai-zephyr.c

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,13 @@ __cold int dai_set_config(struct dai *dai, struct ipc_config_dai *common_config,
203203
/* called from ipc/ipc3/dai.c */
204204
int dai_get_handshake(struct dai *dai, int direction, int stream_id)
205205
{
206-
k_spinlock_key_t key = k_spin_lock(&dai->lock);
207-
const struct dai_properties *props = dai_get_properties(dai->dev, direction,
208-
stream_id);
209-
int hs_id = props->dma_hs_id;
206+
const struct dai_properties *props;
207+
int hs_id;
210208

211-
k_spin_unlock(&dai->lock, key);
209+
k_mutex_lock(dai->lock, K_FOREVER);
210+
props = dai_get_properties(dai->dev, direction, stream_id);
211+
hs_id = props->dma_hs_id;
212+
k_mutex_unlock(dai->lock);
212213

213214
return hs_id;
214215
}
@@ -217,39 +218,41 @@ int dai_get_handshake(struct dai *dai, int direction, int stream_id)
217218
int dai_get_fifo_depth(struct dai *dai, int direction)
218219
{
219220
const struct dai_properties *props;
220-
k_spinlock_key_t key;
221221
int fifo_depth;
222222

223223
if (!dai)
224224
return 0;
225225

226-
key = k_spin_lock(&dai->lock);
226+
k_mutex_lock(dai->lock, K_FOREVER);
227227
props = dai_get_properties(dai->dev, direction, 0);
228228
fifo_depth = props->fifo_depth;
229-
k_spin_unlock(&dai->lock, key);
229+
k_mutex_unlock(dai->lock);
230230

231231
return fifo_depth;
232232
}
233233

234234
int dai_get_stream_id(struct dai *dai, int direction)
235235
{
236-
k_spinlock_key_t key = k_spin_lock(&dai->lock);
237-
const struct dai_properties *props = dai_get_properties(dai->dev, direction, 0);
238-
int stream_id = props->stream_id;
236+
const struct dai_properties *props;
237+
int stream_id;
239238

240-
k_spin_unlock(&dai->lock, key);
239+
k_mutex_lock(dai->lock, K_FOREVER);
240+
props = dai_get_properties(dai->dev, direction, 0);
241+
stream_id = props->stream_id;
242+
k_mutex_unlock(dai->lock);
241243

242244
return stream_id;
243245
}
244246

245247
static int dai_get_fifo(struct dai *dai, int direction, int stream_id)
246248
{
247-
k_spinlock_key_t key = k_spin_lock(&dai->lock);
248-
const struct dai_properties *props = dai_get_properties(dai->dev, direction,
249-
stream_id);
250-
int fifo_address = props->fifo_address;
249+
const struct dai_properties *props;
250+
int fifo_address;
251251

252-
k_spin_unlock(&dai->lock, key);
252+
k_mutex_lock(dai->lock, K_FOREVER);
253+
props = dai_get_properties(dai->dev, direction, stream_id);
254+
fifo_address = props->fifo_address;
255+
k_mutex_unlock(dai->lock);
253256

254257
return fifo_address;
255258
}
@@ -501,7 +504,12 @@ __cold int dai_common_new(struct dai_data *dd, struct comp_dev *dev,
501504
return -ENODEV;
502505
}
503506

504-
k_spinlock_init(&dd->dai->lock);
507+
#ifdef CONFIG_SOF_USERSPACE_LL
508+
dd->dai->lock = k_object_alloc(K_OBJ_MUTEX);
509+
#else
510+
dd->dai->lock = &dd->dai->lock_obj;
511+
#endif
512+
k_mutex_init(dd->dai->lock);
505513

506514
dma_sg_init(&dd->config.elem_array);
507515
dd->xrun = 0;
@@ -630,6 +638,10 @@ __cold void dai_common_free(struct dai_data *dd)
630638

631639
dai_release_llp_slot(dd);
632640

641+
#ifdef CONFIG_SOF_USERSPACE_LL
642+
k_object_free(dd->dai->lock);
643+
#endif
644+
633645
dai_put(dd->dai);
634646

635647
sof_heap_free(dd->heap, dd->dai_spec_config);
@@ -1956,16 +1968,15 @@ static int dai_ts_stop_op(struct comp_dev *dev)
19561968
uint32_t dai_get_init_delay_ms(struct dai *dai)
19571969
{
19581970
const struct dai_properties *props;
1959-
k_spinlock_key_t key;
19601971
uint32_t init_delay;
19611972

19621973
if (!dai)
19631974
return 0;
19641975

1965-
key = k_spin_lock(&dai->lock);
1976+
k_mutex_lock(dai->lock, K_FOREVER);
19661977
props = dai_get_properties(dai->dev, 0, 0);
19671978
init_delay = props->reg_init_delay;
1968-
k_spin_unlock(&dai->lock, key);
1979+
k_mutex_unlock(dai->lock);
19691980

19701981
return init_delay;
19711982
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ struct dai {
5252
uint32_t dma_dev;
5353
const struct device *dev;
5454
const struct dai_data *dd;
55-
struct k_spinlock lock; /* protect properties */
55+
struct k_mutex *lock; /* protect properties */
56+
#ifndef CONFIG_SOF_USERSPACE_LL
57+
struct k_mutex lock_obj;
58+
#endif
5659
};
5760

5861
union hdalink_cfg {

0 commit comments

Comments
 (0)