Skip to contentSkip to conversation

pcm_lib.c: Fixed inaccurate calculation of hw_ptr_interrupt in snd_pcm_update_hw_ptr_interrupt function

PATCH In review Shine Liu 4 participants 11 messages Aug 19, 2009 – Aug 20, 2009

Subsystems (2)
lore
Share & tools
lore

Shine Liu

author
#0 lore raw

PATCH 1/1 pcm_lib.c: Fixed inaccurate calculation of hw_ptr_interrupt in snd_pcm_update_hw_ptr_interrupt function

There's no restriction on that the runtime->period_size should be the multiplicator of the runtime->buffer_size in current kernel code.

So "hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;" is not always the trueth.

I've encounter the problem of this bug, the kernel told me "PCM:
Unexpected hw_pointer value (stream=0, pos=8, intr_ptr=16384)".

The period_size and buffer_size I used was 0x800 and 0x3e80 when the bug occured.

Following patch will fix the bug.

-----------------------------------------------------------------------

sound/core/pcm_lib.c +6 -0
@@ -248,6 +248,12 @@
 	hw_base = runtime->hw_ptr_base;
 	new_hw_ptr = hw_base + pos;
 	hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
+	/* if the period_size is not the multiplicator of the 
+ 	 * buffer_size, the hw_ptr_interrupt calculated above 
+ 	 * may not be correct, should be fixed. 
+ 	 */
+	if(hw_ptr_interrupt > hw_base + runtime->buffer_size)
+		hw_ptr_interrupt = hw_base + runtime->buffer_size;
 	delta = new_hw_ptr - hw_ptr_interrupt;
 	if (hw_ptr_interrupt >= runtime->boundary) {
 		hw_ptr_interrupt -= runtime->boundary;
#1 lore raw

At Wed, 19 Aug 2009 18:22:19 +0800,

Quoted 6 lines · Shine Liu

There's no restriction on that the runtime->period_size should be the multiplicator of the runtime->buffer_size in current kernel code.

So "hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;" is not always the trueth.

The hw_ptr_interrupt is the expected position where the current irq is issued. So, the current code should be correct -- it's simply increased in the size of period_size.

Quoted 7 lines

I've encounter the problem of this bug, the kernel told me "PCM:
Unexpected hw_pointer value (stream=0, pos=8, intr_ptr=16384)".

The period_size and buffer_size I used was 0x800 and 0x3e80 when the bug occured.

Following patch will fix the bug.

Does the problem happen with the latest driver code (as found in sound git tree or alsa-driver-snapshot)? If yes, we'd need more further analysis, e.g. by enabling some xrun_debug flags.

thanks,

Takashi

Quoted 21 lines

-----------------------------------------------------------------------

sound/core/pcm_lib.c +6 -0
@@ -248,6 +248,12 @@
 	hw_base = runtime->hw_ptr_base;
 	new_hw_ptr = hw_base + pos;
 	hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
+	/* if the period_size is not the multiplicator of the 
+ 	 * buffer_size, the hw_ptr_interrupt calculated above 
+ 	 * may not be correct, should be fixed. 
+ 	 */
+	if(hw_ptr_interrupt > hw_base + runtime->buffer_size)
+		hw_ptr_interrupt = hw_base + runtime->buffer_size;
 	delta = new_hw_ptr - hw_ptr_interrupt;
 	if (hw_ptr_interrupt >= runtime->boundary) {
 		hw_ptr_interrupt -= runtime->boundary;
author
#2 lore raw
Quoted 6 lines · Takashi Iwai

At Wed, 19 Aug 2009 18:22:19 +0800,

Quoted 4 lines · Shine Liu

There's no restriction on that the runtime->period_size should be the multiplicator of the runtime->buffer_size in current kernel code.

Quoted 6 lines
Quoted 2 lines

So "hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;" is not always the trueth.

The hw_ptr_interrupt is the expected position where the current irq is issued. So, the current code should be correct -- it's simply increased in the size of period_size.

Yes, the period_size and buffer_size I used was 0x800 and 0x3e80.

The log from the kernel is :

period_update: pcmC0D0p:0: pos=0x809/0x800/0x3e80, hwptr=0x1c, hw_base=0x0, hw_intr=0x0
period_update: pcmC0D0p:0: pos=0x1008/0x800/0x3e80, hwptr=0x809, hw_base=0x0, hw_intr=0x800
period_update: pcmC0D0p:0: pos=0x1808/0x800/0x3e80, hwptr=0x10e3, hw_base=0x0, hw_intr=0x1000
period_update: pcmC0D0p:0: pos=0x2006/0x800/0x3e80, hwptr=0x1808, hw_base=0x0, hw_intr=0x1800
period_update: pcmC0D0p:0: pos=0x2808/0x800/0x3e80, hwptr=0x203f, hw_base=0x0, hw_intr=0x2000
period_update: pcmC0D0p:0: pos=0x3008/0x800/0x3e80, hwptr=0x2808, hw_base=0x0, hw_intr=0x2800
period_update: pcmC0D0p:0: pos=0x3808/0x800/0x3e80, hwptr=0x30e9, hw_base=0x0, hw_intr=0x3000
period_update: pcmC0D0p:0: pos=0x8/0x800/0x3e80, hwptr=0x3808, hw_base=0x0, hw_intr=0x3800
PCM: Unexpected hw_pointer value (stream=0, pos=8, intr_ptr=16384)

It's true that irq was issued when hw_ptr_interrupt is 0x800,0x1000,0x1800,...,0x3800,0x3e80.

But the irq last issued should be at 0x3e80, but not 0x3800 + 0x800 = 0x4000. The DMA engine loaded 0x680 frame not 0x800 frame at the last time.

In the snd_pcm_update_hw_ptr_interrupt function, hw_ptr_interrupt is caculated simply by increased in the size of period_size, but the runtime->period_size is not always consistent with the effective loaded size since the last irq. For example, the DMA engine loaded 0x680 frame when buffer reached the end at 0x3e80, and the irq issued.

Quoted 43 lines
Quoted 8 lines

I've encounter the problem of this bug, the kernel told me "PCM:
Unexpected hw_pointer value (stream=0, pos=8, intr_ptr=16384)".

The period_size and buffer_size I used was 0x800 and 0x3e80 when the bug occured.

Following patch will fix the bug.

Does the problem happen with the latest driver code (as found in sound git tree or alsa-driver-snapshot)? If yes, we'd need more further analysis, e.g. by enabling some xrun_debug flags.

thanks,

Takashi

Quoted 21 lines

-----------------------------------------------------------------------

sound/core/pcm_lib.c +6 -0
@@ -248,6 +248,12 @@
 	hw_base = runtime->hw_ptr_base;
 	new_hw_ptr = hw_base + pos;
 	hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
+	/* if the period_size is not the multiplicator of the 
+ 	 * buffer_size, the hw_ptr_interrupt calculated above 
+ 	 * may not be correct, should be fixed. 
+ 	 */
+	if(hw_ptr_interrupt > hw_base + runtime->buffer_size)
+		hw_ptr_interrupt = hw_base + runtime->buffer_size;
 	delta = new_hw_ptr - hw_ptr_interrupt;
 	if (hw_ptr_interrupt >= runtime->boundary) {
 		hw_ptr_interrupt -= runtime->boundary;
#3 lore raw

At Wed, 19 Aug 2009 18:59:31 +0800,

Quoted 29 lines · Shine Liu
Quoted 6 lines · Takashi Iwai

At Wed, 19 Aug 2009 18:22:19 +0800,

Quoted 4 lines · Shine Liu

There's no restriction on that the runtime->period_size should be the multiplicator of the runtime->buffer_size in current kernel code.

Quoted 6 lines
Quoted 2 lines

So "hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;" is not always the trueth.

The hw_ptr_interrupt is the expected position where the current irq is issued. So, the current code should be correct -- it's simply increased in the size of period_size.

Yes, the period_size and buffer_size I used was 0x800 and 0x3e80.

The log from the kernel is :

period_update: pcmC0D0p:0: pos=0x809/0x800/0x3e80, hwptr=0x1c, hw_base=0x0, hw_intr=0x0
period_update: pcmC0D0p:0: pos=0x1008/0x800/0x3e80, hwptr=0x809, hw_base=0x0, hw_intr=0x800
period_update: pcmC0D0p:0: pos=0x1808/0x800/0x3e80, hwptr=0x10e3, hw_base=0x0, hw_intr=0x1000
period_update: pcmC0D0p:0: pos=0x2006/0x800/0x3e80, hwptr=0x1808, hw_base=0x0, hw_intr=0x1800
period_update: pcmC0D0p:0: pos=0x2808/0x800/0x3e80, hwptr=0x203f, hw_base=0x0, hw_intr=0x2000
period_update: pcmC0D0p:0: pos=0x3008/0x800/0x3e80, hwptr=0x2808, hw_base=0x0, hw_intr=0x2800
period_update: pcmC0D0p:0: pos=0x3808/0x800/0x3e80, hwptr=0x30e9, hw_base=0x0, hw_intr=0x3000
period_update: pcmC0D0p:0: pos=0x8/0x800/0x3e80, hwptr=0x3808, hw_base=0x0, hw_intr=0x3800
PCM: Unexpected hw_pointer value (stream=0, pos=8, intr_ptr=16384)

Quoted 6 lines

It's true that irq was issued when hw_ptr_interrupt is 0x800,0x1000,0x1800,...,0x3800,0x3e80.

But the irq last issued should be at 0x3e80, but not 0x3800 + 0x800 = 0x4000. The DMA engine loaded 0x680 frame not 0x800 frame at the last time.

Then it's a driver bug. If unaligned period size is allowed, it means that the irq is really generated in that period, not at the buffer boundary. Otherwise, it must have a proper hw-constraint to align the period size to the buffer size.

What hardware is it?

thanks,

Takashi

author
#4 lore raw
Quoted 11 lines · Takashi Iwai
Quoted 4 lines

But the irq last issued should be at 0x3e80, but not 0x3800 + 0x800 = 0x4000. The DMA engine loaded 0x680 frame not 0x800 frame at the last time.

Then it's a driver bug. If unaligned period size is allowed, it means that the irq is really generated in that period, not at the buffer boundary. Otherwise, it must have a proper hw-constraint to align the period size to the buffer size.

What hardware is it?

Yes, but there's no constraint code currently to force to align the period size to the buffer size. The bug occurs on linux-2.6.31-rc6 on ASoC s3c24xx platform.

The constraint to force to align the period size to the buffer size should not be hardware depended, it shoud be done in the generic layer, is it?

Quoted 5 lines

thanks,

Takashi

#5 lore raw

At Wed, 19 Aug 2009 12:48:26 +0000,

Quoted 14 lines · Shine Liu
Quoted 11 lines · Takashi Iwai
Quoted 4 lines

But the irq last issued should be at 0x3e80, but not 0x3800 + 0x800 = 0x4000. The DMA engine loaded 0x680 frame not 0x800 frame at the last time.

Then it's a driver bug. If unaligned period size is allowed, it means that the irq is really generated in that period, not at the buffer boundary. Otherwise, it must have a proper hw-constraint to align the period size to the buffer size.

What hardware is it?

Quoted 7 lines

Yes, but there's no constraint code currently to force to align the period size to the buffer size. The bug occurs on linux-2.6.31-rc6 on ASoC s3c24xx platform.

The constraint to force to align the period size to the buffer size should not be hardware depended, it shoud be done in the generic layer, is it?

It is hardware dependent, i.e. how the irq is generated. If the irq is generated in a way like timer, the period size doesn't have to be aligned with the buffer size. But your case doesn't look like that.

Takashi

author
#6 lore raw
Quoted 9 lines · Takashi Iwai

It is hardware dependent, i.e. how the irq is generated. If the irq is generated in a way like timer, the period size doesn't have to be aligned with the buffer size. But your case doesn't look like that.

Takashi

Thanks, I'll find a way to fix this bug for the AsoC s3c24xx platform.

author
#7 lore raw
In reply to Takashi Iwai
Quoted 13 lines · Takashi Iwai

At Wed, 19 Aug 2009 12:48:26 +0000,

Quoted 11 lines · Shine Liu
Quoted 9 lines · Takashi Iwai
Quoted 4 lines

But the irq last issued should be at 0x3e80, but not 0x3800 + 0x800 = 0x4000. The DMA engine loaded 0x680 frame not 0x800 frame at the last time.

Then it's a driver bug. If unaligned period size is allowed, it means that the irq is really generated in that period, not at the buffer boundary. Otherwise, it must have a proper hw-constraint to align the period size to the buffer size.

Hi, Takashi

I carefully read the pcm_lib.c and pcm_native.c but I didn't found
any alignmnet constraint API. Althought there's a function called snd_pcm_hw_constraint_step, but this function can't be used in the situation referred in the last mail which one hw parameter should be aligned to the other hw parameter.

I've implement a snd_pcm_hw_constraint_aligned function based on
snd_interval_step.

The patch is based on linux-2.6.31-rc6, and I've tested.

-------------------------------------------------------------------

include/sound/pcm.h +4 -0
@@ -790,6 +790,10 @@
 			       unsigned int cond,
 			       snd_pcm_hw_param_t var,
 			       unsigned long step);
+int snd_pcm_hw_constraint_aligned(struct snd_pcm_runtime *runtime,
+			       unsigned int cond,
+			       snd_pcm_hw_param_t var,
+			       snd_pcm_hw_param_t unit);
 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
 			       unsigned int cond,
 			       snd_pcm_hw_param_t var);
sound/core/pcm_lib.c +27 -0
@@ -1319,6 +1319,33 @@
 
 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
 
+static int snd_pcm_hw_rule_aligned(struct snd_pcm_hw_params *params,
+				struct snd_pcm_hw_rule *rule)
+{
+	struct snd_interval * i = hw_param_interval(params, rule->deps[0]);
+	unsigned int unit = snd_interval_value(i);
+	return snd_interval_step(hw_param_interval(params, rule->var), 0, unit);
+}
+
+/**
+ * snd_pcm_hw_constraint_aligned - add a hw constraint alignment rule
+ * @runtime: PCM runtime instance
+ * @cond: condition bits
+ * @var: hw_params variable to apply the alignment constraint
+ * @unit: hw_params variable to provid the alignment unit
+ */
+int snd_pcm_hw_constraint_aligned(struct snd_pcm_runtime *runtime,
+			       unsigned int cond,
+			       snd_pcm_hw_param_t var,
+			       snd_pcm_hw_param_t unit)
+{
+	return snd_pcm_hw_rule_add(runtime, cond, var, 
+				   snd_pcm_hw_rule_aligned, (void *) 0,
+				   unit, -1);
+}
+
+EXPORT_SYMBOL(snd_pcm_hw_constraint_aligned);
+
 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
 {
 	static unsigned int pow2_sizes[] = {
Quoted 21 lines
Quoted 10 lines
Quoted 2 lines

What hardware is it?

Yes, but there's no constraint code currently to force to align the period size to the buffer size. The bug occurs on linux-2.6.31-rc6 on ASoC s3c24xx platform.

The constraint to force to align the period size to the buffer size should not be hardware depended, it shoud be done in the generic layer, is it?

It is hardware dependent, i.e. how the irq is generated. If the irq is generated in a way like timer, the period size doesn't have to be aligned with the buffer size. But your case doesn't look like that.

Takashi

#8 lore raw
Quoted 5 lines · Shine Liu

I carefully read the pcm_lib.c and pcm_native.c but I didn't found
any alignmnet constraint API. Althought there's a function called snd_pcm_hw_constraint_step, but this function can't be used in the situation referred in the last mail which one hw parameter should be aligned to the other hw parameter.

The period size, the buffer size, and the number of periods are all related, so you can align the period size to the buffer size by restricting the number of periods to be an integer:

  snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);

HTH
Clemens

author
#9 lore raw

PATCH 1/1 ASoC: S3C24XX : Align the peroid size to the buffer size

In reply to Takashi Iwai
Quoted 43 lines · Takashi Iwai

At Wed, 19 Aug 2009 18:59:31 +0800,

Quoted 29 lines · Shine Liu
Quoted 6 lines · Takashi Iwai

At Wed, 19 Aug 2009 18:22:19 +0800,

Quoted 4 lines · Shine Liu

There's no restriction on that the runtime->period_size should be the multiplicator of the runtime->buffer_size in current kernel code.

Quoted 6 lines
Quoted 2 lines

So "hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;" is not always the trueth.

The hw_ptr_interrupt is the expected position where the current irq is issued. So, the current code should be correct -- it's simply increased in the size of period_size.

Yes, the period_size and buffer_size I used was 0x800 and 0x3e80.

The log from the kernel is :

period_update: pcmC0D0p:0: pos=0x809/0x800/0x3e80, hwptr=0x1c, hw_base=0x0, hw_intr=0x0
period_update: pcmC0D0p:0: pos=0x1008/0x800/0x3e80, hwptr=0x809, hw_base=0x0, hw_intr=0x800
period_update: pcmC0D0p:0: pos=0x1808/0x800/0x3e80, hwptr=0x10e3, hw_base=0x0, hw_intr=0x1000
period_update: pcmC0D0p:0: pos=0x2006/0x800/0x3e80, hwptr=0x1808, hw_base=0x0, hw_intr=0x1800
period_update: pcmC0D0p:0: pos=0x2808/0x800/0x3e80, hwptr=0x203f, hw_base=0x0, hw_intr=0x2000
period_update: pcmC0D0p:0: pos=0x3008/0x800/0x3e80, hwptr=0x2808, hw_base=0x0, hw_intr=0x2800
period_update: pcmC0D0p:0: pos=0x3808/0x800/0x3e80, hwptr=0x30e9, hw_base=0x0, hw_intr=0x3000
period_update: pcmC0D0p:0: pos=0x8/0x800/0x3e80, hwptr=0x3808, hw_base=0x0, hw_intr=0x3800
PCM: Unexpected hw_pointer value (stream=0, pos=8, intr_ptr=16384)

Quoted 6 lines

It's true that irq was issued when hw_ptr_interrupt is 0x800,0x1000,0x1800,...,0x3800,0x3e80.

But the irq last issued should be at 0x3e80, but not 0x3800 + 0x800 = 0x4000. The DMA engine loaded 0x680 frame not 0x800 frame at the last time.

Then it's a driver bug. If unaligned period size is allowed, it means that the irq is really generated in that period, not at the buffer boundary. Otherwise, it must have a proper hw-constraint to align the period size to the buffer size.

Quoted 7 lines

What hardware is it?

thanks,

Takashi

This patch will fix the bug metioned in the above mail. Force the peroid size to be aligned with the buffer size.

Based and tested on linux-2.6.31-rc6.

---------------------------------------------------------------------------

sound/soc/s3c24xx/s3c24xx-pcm.c +1 -0
@@ -318,6 +318,7 @@
 
 	pr_debug("Entered %s\n", __func__);
 
+	snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
 	snd_soc_set_runtime_hwparams(substream, &s3c24xx_pcm_hardware);
 
 	prtd = kzalloc(sizeof(struct s3c24xx_runtime_data), GFP_KERNEL);
#10 lore raw

Re: [PATCH] ASoC: S3C24XX : Align the peroid size to the buffer size

Quoted 2 lines · Shine Liu

This patch will fix the bug metioned in the above mail. Force the peroid size to be aligned with the buffer size.

Applied (with a slightly updated commit message) - thanks!