From 00f8f0d16286151d918fb9e90a29674ab91a1165 Mon Sep 17 00:00:00 2001 From: Tobiasz Dryjanski Date: Fri, 15 Sep 2023 20:02:30 +0200 Subject: [PATCH] host: remove buffer_acquire from host legacy/zephyr remove buffer ops from host-legacy, host-zephyr this is a continuation of changes from commit 4a03699 Signed-off-by: Tobiasz Dryjanski --- src/audio/host-legacy.c | 68 ++++++++------------------ src/audio/host-zephyr.c | 105 +++++++++++++++------------------------- 2 files changed, 59 insertions(+), 114 deletions(-) diff --git a/src/audio/host-legacy.c b/src/audio/host-legacy.c index 2574e3b54467..83cf3c8cd3c4 100644 --- a/src/audio/host-legacy.c +++ b/src/audio/host-legacy.c @@ -105,16 +105,13 @@ static int host_dma_set_config_and_copy(struct host_data *hd, struct comp_dev *d */ static uint32_t host_get_copy_bytes_one_shot(struct host_data *hd, struct comp_dev *dev) { - struct comp_buffer *buffer_c = buffer_acquire(hd->local_buffer); uint32_t copy_bytes; /* calculate minimum size to copy */ if (dev->direction == SOF_IPC_STREAM_PLAYBACK) - copy_bytes = audio_stream_get_free_bytes(&buffer_c->stream); + copy_bytes = audio_stream_get_free_bytes(&hd->local_buffer->stream); else - copy_bytes = audio_stream_get_avail_bytes(&buffer_c->stream); - - buffer_release(buffer_c); + copy_bytes = audio_stream_get_avail_bytes(&hd->local_buffer->stream); /* copy_bytes should be aligned to minimum possible chunk of * data to be copied by dma. @@ -168,17 +165,14 @@ static int host_copy_one_shot(struct host_data *hd, struct comp_dev *dev, copy_c static uint32_t host_get_copy_bytes_one_shot(struct host_data *hd, struct comp_dev *dev) { struct dma_sg_elem *local_elem = hd->config.elem_array.elems; - struct comp_buffer *buffer_c = buffer_acquire(hd->local_buffer); uint32_t copy_bytes; uint32_t split_value; /* calculate minimum size to copy */ if (dev->direction == SOF_IPC_STREAM_PLAYBACK) - copy_bytes = audio_stream_get_free_bytes(&buffer_c->stream); + copy_bytes = audio_stream_get_free_bytes(&hd->local_buffer->stream); else - copy_bytes = audio_stream_get_avail_bytes(&buffer_c->stream); - - buffer_release(buffer_c); + copy_bytes = audio_stream_get_avail_bytes(&hd->local_buffer->stream); /* copy_bytes should be aligned to minimum possible chunk of * data to be copied by dma. @@ -239,12 +233,12 @@ void host_common_update(struct host_data *hd, struct comp_dev *dev, uint32_t byt bool send_ipc = false; if (dev->direction == SOF_IPC_STREAM_PLAYBACK) { - source = buffer_acquire(hd->dma_buffer); - sink = buffer_acquire(hd->local_buffer); + source = hd->dma_buffer; + sink = hd->local_buffer; ret = dma_buffer_copy_from(source, sink, hd->process, bytes); } else { - source = buffer_acquire(hd->local_buffer); - sink = buffer_acquire(hd->dma_buffer); + source = hd->local_buffer; + sink = hd->dma_buffer; ret = dma_buffer_copy_to(source, sink, hd->process, bytes); } @@ -257,9 +251,6 @@ void host_common_update(struct host_data *hd, struct comp_dev *dev, uint32_t byt audio_stream_get_free_samples(&sink->stream) * audio_stream_frame_bytes(&sink->stream)); - buffer_release(sink); - buffer_release(source); - if (ret < 0) return; @@ -372,7 +363,6 @@ static void host_dma_cb(void *arg, enum notify_id type, void *data) */ static uint32_t host_get_copy_bytes_normal(struct host_data *hd, struct comp_dev *dev) { - struct comp_buffer *buffer_c; uint32_t avail_bytes = 0; uint32_t free_bytes = 0; uint32_t copy_bytes = 0; @@ -387,28 +377,24 @@ static uint32_t host_get_copy_bytes_normal(struct host_data *hd, struct comp_dev return 0; } - buffer_c = buffer_acquire(hd->local_buffer); - /* calculate minimum size to copy */ if (dev->direction == SOF_IPC_STREAM_PLAYBACK) { /* limit bytes per copy to one period for the whole pipeline * in order to avoid high load spike */ - free_bytes = audio_stream_get_free_bytes(&buffer_c->stream); + free_bytes = audio_stream_get_free_bytes(&hd->local_buffer->stream); copy_bytes = MIN(hd->period_bytes, MIN(avail_bytes, free_bytes)); if (!copy_bytes) comp_info(dev, "no bytes to copy, %d free in buffer, %d available in DMA", free_bytes, avail_bytes); } else { - avail_bytes = audio_stream_get_avail_bytes(&buffer_c->stream); + avail_bytes = audio_stream_get_avail_bytes(&hd->local_buffer->stream); copy_bytes = MIN(avail_bytes, free_bytes); if (!copy_bytes) comp_info(dev, "no bytes to copy, %d avail in buffer, %d free in DMA", avail_bytes, free_bytes); } - buffer_release(buffer_c); - /* copy_bytes should be aligned to minimum possible chunk of * data to be copied by dma. */ @@ -446,7 +432,6 @@ static int host_copy_normal(struct host_data *hd, struct comp_dev *dev, copy_cal static int create_local_elems(struct host_data *hd, struct comp_dev *dev, uint32_t buffer_count, uint32_t buffer_bytes) { - struct comp_buffer *dma_buf_c; struct dma_sg_elem_array *elem_array; uint32_t dir; int err; @@ -469,11 +454,9 @@ static int create_local_elems(struct host_data *hd, struct comp_dev *dev, uint32 elem_array = &hd->config.elem_array; } - dma_buf_c = buffer_acquire(hd->dma_buffer); err = dma_sg_alloc(elem_array, SOF_MEM_ZONE_RUNTIME, dir, buffer_count, buffer_bytes, - (uintptr_t)(audio_stream_get_addr(&dma_buf_c->stream)), 0); - buffer_release(dma_buf_c); + (uintptr_t)(audio_stream_get_addr(&hd->dma_buffer->stream)), 0); if (err < 0) { comp_err(dev, "create_local_elems(): dma_sg_alloc() failed"); return err; @@ -690,8 +673,6 @@ int host_common_params(struct host_data *hd, struct comp_dev *dev, struct sof_ipc_stream_params *params, notifier_callback_t cb) { struct dma_sg_config *config = &hd->config; - struct comp_buffer *host_buf_c; - struct comp_buffer *dma_buf_c; uint32_t period_count; uint32_t period_bytes; uint32_t buffer_size; @@ -740,10 +721,9 @@ int host_common_params(struct host_data *hd, struct comp_dev *dev, hd->local_buffer = list_first_item(&dev->bsource_list, struct comp_buffer, sink_list); - host_buf_c = buffer_acquire(hd->local_buffer); period_bytes = dev->frames * - audio_stream_frame_bytes(&host_buf_c->stream); + audio_stream_frame_bytes(&hd->local_buffer->stream); if (!period_bytes) { comp_err(dev, "host_params(): invalid period_bytes"); @@ -778,9 +758,7 @@ int host_common_params(struct host_data *hd, struct comp_dev *dev, * but we have to write back caches after we finish anyway */ if (hd->dma_buffer) { - dma_buf_c = buffer_acquire(hd->dma_buffer); - err = buffer_set_size(dma_buf_c, buffer_size, addr_align); - buffer_release(dma_buf_c); + err = buffer_set_size(hd->dma_buffer, buffer_size, addr_align); if (err < 0) { comp_err(dev, "host_params(): buffer_set_size() failed, buffer_size = %u", buffer_size); @@ -795,9 +773,7 @@ int host_common_params(struct host_data *hd, struct comp_dev *dev, goto out; } - dma_buf_c = buffer_acquire(hd->dma_buffer); - buffer_set_params(dma_buf_c, params, BUFFER_UPDATE_FORCE); - buffer_release(dma_buf_c); + buffer_set_params(hd->dma_buffer, params, BUFFER_UPDATE_FORCE); } /* create SG DMA elems for local DMA buffer */ @@ -806,8 +782,8 @@ int host_common_params(struct host_data *hd, struct comp_dev *dev, goto out; /* set up DMA configuration - copy in sample bytes. */ - config->src_width = audio_stream_sample_bytes(&host_buf_c->stream); - config->dest_width = audio_stream_sample_bytes(&host_buf_c->stream); + config->src_width = audio_stream_sample_bytes(&hd->local_buffer->stream); + config->dest_width = audio_stream_sample_bytes(&hd->local_buffer->stream); config->cyclic = 0; config->irq_disabled = pipeline_is_timer_driven(dev->pipeline); config->is_scheduling_source = comp_is_scheduling_source(dev); @@ -851,11 +827,11 @@ int host_common_params(struct host_data *hd, struct comp_dev *dev, host_copy_normal; /* set processing function */ - hd->process = pcm_get_conversion_function(audio_stream_get_frm_fmt(&host_buf_c->stream), - audio_stream_get_frm_fmt(&host_buf_c->stream)); + hd->process = + pcm_get_conversion_function(audio_stream_get_frm_fmt(&hd->local_buffer->stream), + audio_stream_get_frm_fmt(&hd->local_buffer->stream)); out: - buffer_release(host_buf_c); hd->cb_dev = dev; @@ -886,11 +862,7 @@ static int host_params(struct comp_dev *dev, int host_common_prepare(struct host_data *hd) { - struct comp_buffer *buf_c = buffer_acquire(hd->dma_buffer); - - buffer_zero(buf_c); - buffer_release(buf_c); - + buffer_zero(hd->dma_buffer); return 0; } diff --git a/src/audio/host-zephyr.c b/src/audio/host-zephyr.c index a2918b31e1b6..b4359b8149e2 100644 --- a/src/audio/host-zephyr.c +++ b/src/audio/host-zephyr.c @@ -109,18 +109,13 @@ static int host_dma_set_config_and_copy(struct host_data *hd, struct comp_dev *d static uint32_t host_get_copy_bytes_one_shot(struct host_data *hd) { struct comp_buffer *buffer = hd->local_buffer; - struct comp_buffer *buffer_c; uint32_t copy_bytes; - buffer_c = buffer_acquire(buffer); - /* calculate minimum size to copy */ if (hd->ipc_host.direction == SOF_IPC_STREAM_PLAYBACK) - copy_bytes = audio_stream_get_free_bytes(&buffer_c->stream); + copy_bytes = audio_stream_get_free_bytes(&buffer->stream); else - copy_bytes = audio_stream_get_avail_bytes(&buffer_c->stream); - - buffer_release(buffer_c); + copy_bytes = audio_stream_get_avail_bytes(&buffer->stream); /* copy_bytes should be aligned to minimum possible chunk of * data to be copied by dma. @@ -175,19 +170,14 @@ static uint32_t host_get_copy_bytes_one_shot(struct host_data *hd) { struct dma_sg_elem *local_elem = hd->config.elem_array.elems; struct comp_buffer *buffer = hd->local_buffer; - struct comp_buffer *buffer_c; uint32_t copy_bytes; uint32_t split_value; - buffer_c = buffer_acquire(buffer); - /* calculate minimum size to copy */ if (hd->ipc_host.direction == SOF_IPC_STREAM_PLAYBACK) - copy_bytes = audio_stream_get_free_bytes(&buffer_c->stream); + copy_bytes = audio_stream_get_free_bytes(&buffer->stream); else - copy_bytes = audio_stream_get_avail_bytes(&buffer_c->stream); - - buffer_release(buffer_c); + copy_bytes = audio_stream_get_avail_bytes(&buffer->stream); /* copy_bytes should be aligned to minimum possible chunk of * data to be copied by dma. @@ -247,22 +237,26 @@ void host_common_update(struct host_data *hd, struct comp_dev *dev, uint32_t byt bool update_mailbox = false; bool send_ipc = false; - if (dev->direction == SOF_IPC_STREAM_PLAYBACK) { - source = buffer_acquire(hd->dma_buffer); - sink = buffer_acquire(hd->local_buffer); + if (hd->ipc_host.direction == SOF_IPC_STREAM_PLAYBACK) { + source = hd->dma_buffer; + sink = hd->local_buffer; ret = dma_buffer_copy_from(source, sink, hd->process, bytes); } else { - source = buffer_acquire(hd->local_buffer); - sink = buffer_acquire(hd->dma_buffer); + source = hd->local_buffer; + sink = hd->dma_buffer; ret = dma_buffer_copy_to(source, sink, hd->process, bytes); } - buffer_release(sink); - buffer_release(source); + /* assert dma_buffer_copy succeed */ + if (ret < 0) + comp_err(dev, "host_common_update() dma buffer copy failed, dir %d bytes %d avail %d free %d", + hd->ipc_host.direction, bytes, + audio_stream_get_avail_samples(&source->stream) * + audio_stream_frame_bytes(&source->stream), + audio_stream_get_free_samples(&sink->stream) * + audio_stream_frame_bytes(&sink->stream)); if (ret < 0) { - comp_err(dev, "host_common_update() copy failed, dir %d bytes %d return: %d", - dev->direction, bytes, ret); return; } @@ -373,7 +367,6 @@ static void host_dma_cb(struct comp_dev *dev, size_t bytes) static uint32_t host_get_copy_bytes_normal(struct host_data *hd, struct comp_dev *dev) { struct comp_buffer *buffer = hd->local_buffer; - struct comp_buffer *buffer_c; struct dma_status dma_stat; uint32_t avail_samples; uint32_t free_samples; @@ -390,16 +383,14 @@ static uint32_t host_get_copy_bytes_normal(struct host_data *hd, struct comp_dev return 0; } - dma_sample_bytes = hd->config.src_width; - - buffer_c = buffer_acquire(buffer); + dma_sample_bytes = get_sample_bytes(audio_stream_get_frm_fmt(&hd->dma_buffer->stream)); /* calculate minimum size to copy */ if (dev->direction == SOF_IPC_STREAM_PLAYBACK) { avail_samples = (dma_stat.pending_length - hd->partial_size) / dma_sample_bytes; - free_samples = audio_stream_get_free_samples(&buffer_c->stream); + free_samples = audio_stream_get_free_samples(&buffer->stream); } else { - avail_samples = audio_stream_get_avail_samples(&buffer_c->stream); + avail_samples = audio_stream_get_avail_samples(&buffer->stream); free_samples = (dma_stat.free - hd->partial_size) / dma_sample_bytes; } @@ -416,8 +407,6 @@ static uint32_t host_get_copy_bytes_normal(struct host_data *hd, struct comp_dev comp_info(dev, "no bytes to copy, available samples: %d, free_samples: %d", avail_samples, free_samples); - buffer_release(buffer_c); - /* dma_copy_bytes should be aligned to minimum possible chunk of * data to be copied by dma. */ @@ -480,8 +469,8 @@ static int host_copy_normal(struct host_data *hd, struct comp_dev *dev, copy_cal * also adding a 2ms safety margin. */ if (!IS_ENABLED(CONFIG_HOST_DMA_RELOAD_DELAY_ENABLE) || - hd->dma_buffer_size < hd->period_bytes << 3 || - hd->dma_buffer_size - hd->partial_size <= + audio_stream_get_size(&hd->dma_buffer->stream) < hd->period_bytes << 3 || + audio_stream_get_size(&hd->dma_buffer->stream) - hd->partial_size <= (2 + threshold) * hd->period_bytes) { if (stream_sync(hd, dev)) { ret = dma_reload(hd->chan->dma->z_dev, hd->chan->index, 0, 0, @@ -492,13 +481,13 @@ static int host_copy_normal(struct host_data *hd, struct comp_dev *dev, copy_cal hd->partial_size = 0; } } + return ret; } static int create_local_elems(struct host_data *hd, struct comp_dev *dev, uint32_t buffer_count, uint32_t buffer_bytes, uint32_t direction) { - struct comp_buffer *dma_buf_c; struct dma_sg_elem_array *elem_array; uint32_t dir; int err; @@ -521,11 +510,9 @@ static int create_local_elems(struct host_data *hd, struct comp_dev *dev, uint32 elem_array = &hd->config.elem_array; } - dma_buf_c = buffer_acquire(hd->dma_buffer); err = dma_sg_alloc(elem_array, SOF_MEM_ZONE_RUNTIME, dir, buffer_count, buffer_bytes, - (uintptr_t)audio_stream_get_addr(&dma_buf_c->stream), 0); - buffer_release(dma_buf_c); + (uintptr_t)audio_stream_get_addr(&hd->dma_buffer->stream), 0); if (err < 0) { comp_err(dev, "create_local_elems(): dma_sg_alloc() failed"); return err; @@ -745,8 +732,6 @@ int host_common_params(struct host_data *hd, struct comp_dev *dev, struct dma_sg_elem *sg_elem; struct dma_config *dma_cfg = &hd->z_config; struct dma_block_config dma_block_cfg; - struct comp_buffer *host_buf_c; - struct comp_buffer *dma_buf_c; uint32_t period_count; uint32_t period_bytes; uint32_t buffer_size; @@ -795,14 +780,13 @@ int host_common_params(struct host_data *hd, struct comp_dev *dev, hd->local_buffer = list_first_item(&dev->bsource_list, struct comp_buffer, sink_list); - host_buf_c = buffer_acquire(hd->local_buffer); period_bytes = dev->frames * get_frame_bytes(params->frame_fmt, params->channels); if (!period_bytes) { comp_err(dev, "host_params(): invalid period_bytes"); err = -EINVAL; - goto out; + return err; } /* determine source and sink buffer elements */ @@ -834,13 +818,11 @@ int host_common_params(struct host_data *hd, struct comp_dev *dev, * but we have to write back caches after we finish anywae */ if (hd->dma_buffer) { - dma_buf_c = buffer_acquire(hd->dma_buffer); - err = buffer_set_size(dma_buf_c, buffer_size, addr_align); - buffer_release(dma_buf_c); + err = buffer_set_size(hd->dma_buffer, buffer_size, addr_align); if (err < 0) { comp_err(dev, "host_params(): buffer_set_size() failed, buffer_size = %u", buffer_size); - goto out; + return err; } } else { /* allocate not shared buffer */ @@ -849,33 +831,30 @@ int host_common_params(struct host_data *hd, struct comp_dev *dev, if (!hd->dma_buffer) { comp_err(dev, "host_params(): failed to alloc dma buffer"); err = -ENOMEM; - goto out; + return err; } - dma_buf_c = buffer_acquire(hd->dma_buffer); - buffer_set_params(dma_buf_c, params, BUFFER_UPDATE_FORCE); + buffer_set_params(hd->dma_buffer, params, BUFFER_UPDATE_FORCE); /* set processing function */ if (params->direction == SOF_IPC_STREAM_CAPTURE) hd->process = pcm_get_conversion_function( - audio_stream_get_frm_fmt(&host_buf_c->stream), - audio_stream_get_frm_fmt(&dma_buf_c->stream)); + audio_stream_get_frm_fmt(&hd->local_buffer->stream), + audio_stream_get_frm_fmt(&hd->dma_buffer->stream)); else hd->process = pcm_get_conversion_function( - audio_stream_get_frm_fmt(&dma_buf_c->stream), - audio_stream_get_frm_fmt(&host_buf_c->stream)); + audio_stream_get_frm_fmt(&hd->dma_buffer->stream), + audio_stream_get_frm_fmt(&hd->local_buffer->stream)); - config->src_width = audio_stream_sample_bytes(&dma_buf_c->stream); + config->src_width = audio_stream_sample_bytes(&hd->dma_buffer->stream); config->dest_width = config->src_width; - hd->dma_buffer_size = audio_stream_get_size(&dma_buf_c->stream); - buffer_release(dma_buf_c); } /* create SG DMA elems for local DMA buffer */ err = create_local_elems(hd, dev, period_count, buffer_size / period_count, params->direction); if (err < 0) - goto out; + return err; /* set up DMA configuration - copy in sample bytes. */ config->cyclic = 0; @@ -894,7 +873,7 @@ int host_common_params(struct host_data *hd, struct comp_dev *dev, if (channel < 0) { comp_err(dev, "host_params(): requested channel %d is busy", hda_chan); err = -ENODEV; - goto out; + return err; } hd->chan = &hd->dma->chan[channel]; @@ -948,7 +927,7 @@ int host_common_params(struct host_data *hd, struct comp_dev *dev, comp_err(dev, "host_params(): dma_config() failed"); dma_release_channel(hd->dma->z_dev, hd->chan->index); hd->chan = NULL; - goto out; + return err; } err = dma_get_attribute(hd->dma->z_dev, DMA_ATTR_COPY_ALIGNMENT, @@ -957,7 +936,7 @@ int host_common_params(struct host_data *hd, struct comp_dev *dev, if (err < 0) { comp_err(dev, "host_params(): dma_get_attribute()"); - goto out; + return err; } /* minimal copied data shouldn't be less than alignment */ @@ -970,8 +949,6 @@ int host_common_params(struct host_data *hd, struct comp_dev *dev, hd->copy = hd->copy_type == COMP_COPY_ONE_SHOT ? host_copy_one_shot : host_copy_normal; -out: - buffer_release(host_buf_c); return err; } @@ -994,11 +971,7 @@ static int host_params(struct comp_dev *dev, int host_common_prepare(struct host_data *hd) { - struct comp_buffer *buf_c = buffer_acquire(hd->dma_buffer); - - buffer_zero(buf_c); - buffer_release(buf_c); - + buffer_zero(hd->dma_buffer); return 0; }