ASoC: dmaengine_pcm: Consider DMA cache caused delay in pointer callback
diff mbox series

Message ID 20200210140423.10232-1-peter.ujfalusi@ti.com
State New
Headers show
Series
  • ASoC: dmaengine_pcm: Consider DMA cache caused delay in pointer callback
Related show

Commit Message

Peter Ujfalusi Feb. 10, 2020, 2:04 p.m. UTC
Some DMA engines can have big FIFOs which adds to the latency.
The DMAengine framework can report the FIFO utilization in bytes. Use this
information for the delay reporting.

Signed-off-by: Peter Ujfalusi <peter.ujfalusi@ti.com>
---
Hi,

5.6-rc1 now have support for reporting the DMA cached data.
With this patch we can include it to the delay calculation.
The first DMA driver which reports this is the TI K3 UDMA driver.

Regards,
Peter

 sound/core/pcm_dmaengine.c | 6 ++++++
 sound/soc/soc-pcm.c        | 2 +-
 2 files changed, 7 insertions(+), 1 deletion(-)

Comments

Takashi Iwai Feb. 10, 2020, 2:21 p.m. UTC | #1
On Mon, 10 Feb 2020 15:04:23 +0100,
Peter Ujfalusi wrote:
> 
> Some DMA engines can have big FIFOs which adds to the latency.
> The DMAengine framework can report the FIFO utilization in bytes. Use this
> information for the delay reporting.
> 
> Signed-off-by: Peter Ujfalusi <peter.ujfalusi@ti.com>
> ---
> Hi,
> 
> 5.6-rc1 now have support for reporting the DMA cached data.
> With this patch we can include it to the delay calculation.
> The first DMA driver which reports this is the TI K3 UDMA driver.
> 
> Regards,
> Peter
> 
>  sound/core/pcm_dmaengine.c | 6 ++++++
>  sound/soc/soc-pcm.c        | 2 +-
>  2 files changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/sound/core/pcm_dmaengine.c b/sound/core/pcm_dmaengine.c
> index 5749a8a49784..4f1395fd0160 100644
> --- a/sound/core/pcm_dmaengine.c
> +++ b/sound/core/pcm_dmaengine.c
> @@ -247,9 +247,15 @@ snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
>  
>  	status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state);
>  	if (status == DMA_IN_PROGRESS || status == DMA_PAUSED) {
> +		struct snd_pcm_runtime *runtime = substream->runtime;
> +		int sample_bits = snd_pcm_format_physical_width(runtime->format);
> +
>  		buf_size = snd_pcm_lib_buffer_bytes(substream);
>  		if (state.residue > 0 && state.residue <= buf_size)
>  			pos = buf_size - state.residue;
> +
> +		sample_bits *= runtime->channels;
> +		runtime->delay = state.in_flight_bytes / (sample_bits / 8);

Can this be simply bytes_to_frames()?

		runtime->delay = bytes_to_frames(runtime, state.in_flight_bytes);

>  	}
>  
>  	return bytes_to_frames(substream->runtime, pos);
> diff --git a/sound/soc/soc-pcm.c b/sound/soc/soc-pcm.c
> index ff1b7c7078e5..58ef508d70a3 100644
> --- a/sound/soc/soc-pcm.c
> +++ b/sound/soc/soc-pcm.c
> @@ -1151,7 +1151,7 @@ static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
>  	}
>  	delay += codec_delay;
>  
> -	runtime->delay = delay;
> +	runtime->delay += delay;

Is it correct?
delay already takes runtime->delay as its basis, so it'll result in a
double.


thanks,

Takashi
Peter Ujfalusi Feb. 10, 2020, 2:28 p.m. UTC | #2
Hi Takashi,

On 10/02/2020 16.21, Takashi Iwai wrote:
> On Mon, 10 Feb 2020 15:04:23 +0100,
> Peter Ujfalusi wrote:
>>
>> Some DMA engines can have big FIFOs which adds to the latency.
>> The DMAengine framework can report the FIFO utilization in bytes. Use this
>> information for the delay reporting.
>>
>> Signed-off-by: Peter Ujfalusi <peter.ujfalusi@ti.com>
>> ---
>> Hi,
>>
>> 5.6-rc1 now have support for reporting the DMA cached data.
>> With this patch we can include it to the delay calculation.
>> The first DMA driver which reports this is the TI K3 UDMA driver.
>>
>> Regards,
>> Peter
>>
>>  sound/core/pcm_dmaengine.c | 6 ++++++
>>  sound/soc/soc-pcm.c        | 2 +-
>>  2 files changed, 7 insertions(+), 1 deletion(-)
>>
>> diff --git a/sound/core/pcm_dmaengine.c b/sound/core/pcm_dmaengine.c
>> index 5749a8a49784..4f1395fd0160 100644
>> --- a/sound/core/pcm_dmaengine.c
>> +++ b/sound/core/pcm_dmaengine.c
>> @@ -247,9 +247,15 @@ snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
>>  
>>  	status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state);
>>  	if (status == DMA_IN_PROGRESS || status == DMA_PAUSED) {
>> +		struct snd_pcm_runtime *runtime = substream->runtime;
>> +		int sample_bits = snd_pcm_format_physical_width(runtime->format);
>> +
>>  		buf_size = snd_pcm_lib_buffer_bytes(substream);
>>  		if (state.residue > 0 && state.residue <= buf_size)
>>  			pos = buf_size - state.residue;
>> +
>> +		sample_bits *= runtime->channels;
>> +		runtime->delay = state.in_flight_bytes / (sample_bits / 8);
> 
> Can this be simply bytes_to_frames()?
> 
> 		runtime->delay = bytes_to_frames(runtime, state.in_flight_bytes);

Certainly it can, I looked at various helper but somehow missed the
bytes_to_frames().

I'll send v2 in about an hour.



> 
>>  	}
>>  
>>  	return bytes_to_frames(substream->runtime, pos);
>> diff --git a/sound/soc/soc-pcm.c b/sound/soc/soc-pcm.c
>> index ff1b7c7078e5..58ef508d70a3 100644
>> --- a/sound/soc/soc-pcm.c
>> +++ b/sound/soc/soc-pcm.c
>> @@ -1151,7 +1151,7 @@ static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
>>  	}
>>  	delay += codec_delay;
>>  
>> -	runtime->delay = delay;
>> +	runtime->delay += delay;
> 
> Is it correct?
> delay already takes runtime->delay as its basis, so it'll result in a
> double.

The delay here is coming from the DAI and the codec.
The runtime->delay hold the PCM (DMA) caused delay.

> 
> thanks,
> 
> Takashi
> 

- Péter

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki.
Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
Mark Brown Feb. 10, 2020, 2:37 p.m. UTC | #3
On Mon, Feb 10, 2020 at 04:28:44PM +0200, Peter Ujfalusi wrote:
> On 10/02/2020 16.21, Takashi Iwai wrote:

> >>  	delay += codec_delay;
> >>  
> >> -	runtime->delay = delay;
> >> +	runtime->delay += delay;

> > Is it correct?
> > delay already takes runtime->delay as its basis, so it'll result in a
> > double.

> The delay here is coming from the DAI and the codec.
> The runtime->delay hold the PCM (DMA) caused delay.

I think Takashi's point here (and a query I have) is that we end up with

	delay = runtime->delay;
	delay += stuff;
	runtime->delay += delay;

which is equivalent to

	runtime->delay = (runtime->delay * 2) + stuff;

and that's a bit surprising.
Takashi Iwai Feb. 10, 2020, 2:38 p.m. UTC | #4
On Mon, 10 Feb 2020 15:28:44 +0100,
Peter Ujfalusi wrote:
> 
> Hi Takashi,
> 
> >> --- a/sound/soc/soc-pcm.c
> >> +++ b/sound/soc/soc-pcm.c
> >> @@ -1151,7 +1151,7 @@ static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
> >>  	}
> >>  	delay += codec_delay;
> >>  
> >> -	runtime->delay = delay;
> >> +	runtime->delay += delay;
> > 
> > Is it correct?
> > delay already takes runtime->delay as its basis, so it'll result in a
> > double.
> 
> The delay here is coming from the DAI and the codec.
> The runtime->delay hold the PCM (DMA) caused delay.

Well, let's take a look at soc_pcm_pointer():

	/* clearing the previous total delay */
	runtime->delay = 0;

	offset = snd_soc_pcm_component_pointer(substream);

	/* base delay if assigned in pointer callback */
	delay = runtime->delay;

	delay += snd_soc_dai_delay(cpu_dai, substream);

	for_each_rtd_codec_dai(rtd, i, codec_dai) {
		codec_delay = max(codec_delay,
				  snd_soc_dai_delay(codec_dai, substream));
	}
	delay += codec_delay;

	runtime->delay = delay;

So, the code reads the current runtime->delay and saves it as delay
variable.  Then it adds the max delay from codec DAIs, and stores back
to runtime->delay.

If we change the last line to
	runtime->delay += delay;
it'll add to the already existing value again, so it'll be doubly if
runtime->delay was non-zero beforehand.

That said, judging from the code, I believe the current soc-pcm.c code
needs no change.


thanks,

Takashi
Peter Ujfalusi Feb. 10, 2020, 2:59 p.m. UTC | #5
On 10/02/2020 16.37, Mark Brown wrote:
> On Mon, Feb 10, 2020 at 04:28:44PM +0200, Peter Ujfalusi wrote:
>> On 10/02/2020 16.21, Takashi Iwai wrote:
> 
>>>>  	delay += codec_delay;
>>>>  
>>>> -	runtime->delay = delay;
>>>> +	runtime->delay += delay;
> 
>>> Is it correct?
>>> delay already takes runtime->delay as its basis, so it'll result in a
>>> double.
> 
>> The delay here is coming from the DAI and the codec.
>> The runtime->delay hold the PCM (DMA) caused delay.
> 
> I think Takashi's point here (and a query I have) is that we end up with
> 
> 	delay = runtime->delay;
> 	delay += stuff;
> 	runtime->delay += delay;
> 
> which is equivalent to
> 
> 	runtime->delay = (runtime->delay * 2) + stuff;
> 
> and that's a bit surprising.

I see, I have missed what
9fb4c2bf130b ASoC: soc-pcm: Use delay set in component pointer function

did.
the soc-pcm part of the patch can be dropped then.

- Péter

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki.
Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
Peter Ujfalusi Feb. 10, 2020, 3:01 p.m. UTC | #6
On 10/02/2020 16.38, Takashi Iwai wrote:
> On Mon, 10 Feb 2020 15:28:44 +0100,
> Peter Ujfalusi wrote:
>>
>> Hi Takashi,
>>
>>>> --- a/sound/soc/soc-pcm.c
>>>> +++ b/sound/soc/soc-pcm.c
>>>> @@ -1151,7 +1151,7 @@ static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
>>>>  	}
>>>>  	delay += codec_delay;
>>>>  
>>>> -	runtime->delay = delay;
>>>> +	runtime->delay += delay;
>>>
>>> Is it correct?
>>> delay already takes runtime->delay as its basis, so it'll result in a
>>> double.
>>
>> The delay here is coming from the DAI and the codec.
>> The runtime->delay hold the PCM (DMA) caused delay.
> 
> Well, let's take a look at soc_pcm_pointer():
> 
> 	/* clearing the previous total delay */
> 	runtime->delay = 0;
> 
> 	offset = snd_soc_pcm_component_pointer(substream);
> 
> 	/* base delay if assigned in pointer callback */
> 	delay = runtime->delay;
> 
> 	delay += snd_soc_dai_delay(cpu_dai, substream);
> 
> 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
> 		codec_delay = max(codec_delay,
> 				  snd_soc_dai_delay(codec_dai, substream));
> 	}
> 	delay += codec_delay;
> 
> 	runtime->delay = delay;
> 
> So, the code reads the current runtime->delay and saves it as delay
> variable.  Then it adds the max delay from codec DAIs, and stores back
> to runtime->delay.
> 
> If we change the last line to
> 	runtime->delay += delay;
> it'll add to the already existing value again, so it'll be doubly if
> runtime->delay was non-zero beforehand.

Yes, you are right.
The change is added by
9fb4c2bf130b ASoC: soc-pcm: Use delay set in component pointer function

which I have missed, apparently.

> That said, judging from the code, I believe the current soc-pcm.c code
> needs no change.

Yes, there is no need to change soc-pcm.

> 
> 
> thanks,
> 
> Takashi
> 

- Péter

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki.
Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

Patch
diff mbox series

diff --git a/sound/core/pcm_dmaengine.c b/sound/core/pcm_dmaengine.c
index 5749a8a49784..4f1395fd0160 100644
--- a/sound/core/pcm_dmaengine.c
+++ b/sound/core/pcm_dmaengine.c
@@ -247,9 +247,15 @@  snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
 
 	status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state);
 	if (status == DMA_IN_PROGRESS || status == DMA_PAUSED) {
+		struct snd_pcm_runtime *runtime = substream->runtime;
+		int sample_bits = snd_pcm_format_physical_width(runtime->format);
+
 		buf_size = snd_pcm_lib_buffer_bytes(substream);
 		if (state.residue > 0 && state.residue <= buf_size)
 			pos = buf_size - state.residue;
+
+		sample_bits *= runtime->channels;
+		runtime->delay = state.in_flight_bytes / (sample_bits / 8);
 	}
 
 	return bytes_to_frames(substream->runtime, pos);
diff --git a/sound/soc/soc-pcm.c b/sound/soc/soc-pcm.c
index ff1b7c7078e5..58ef508d70a3 100644
--- a/sound/soc/soc-pcm.c
+++ b/sound/soc/soc-pcm.c
@@ -1151,7 +1151,7 @@  static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
 	}
 	delay += codec_delay;
 
-	runtime->delay = delay;
+	runtime->delay += delay;
 
 	return offset;
 }