Linux Sound subsystem development
 help / color / mirror / Atom feed
From: Takashi Iwai <tiwai@suse.de>
To: linux-sound@vger.kernel.org
Cc: David Rhodes <david.rhodes@cirrus.com>,
	Richard Fitzgerald <rf@opensource.cirrus.com>,
	patches@opensource.cirrus.com,
	Shenghao Ding <shenghao-ding@ti.com>, Kevin Lu <kevin-lu@ti.com>,
	Baojun Xu <baojun.xu@ti.com>
Subject: [PATCH 06/25] ALSA: hda: Use auto cleanup macros for DSP loader locks
Date: Mon, 11 Aug 2025 12:07:40 +0200	[thread overview]
Message-ID: <20250811100807.7962-7-tiwai@suse.de> (raw)
In-Reply-To: <20250811100807.7962-1-tiwai@suse.de>

There are temporary DSP locking/unlocking patterns found in various
places, and those can be cleaned up nicely with the guard() macro
calling snd_hdac_dsp_lock() and *_unlock().

Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
 include/sound/hdaudio.h       |  1 +
 sound/hda/common/controller.c | 47 +++++++++++++++--------------------
 sound/hda/core/stream.c       | 11 +++-----
 3 files changed, 24 insertions(+), 35 deletions(-)

diff --git a/include/sound/hdaudio.h b/include/sound/hdaudio.h
index d38234f8fe44..4e0c1d8af09f 100644
--- a/include/sound/hdaudio.h
+++ b/include/sound/hdaudio.h
@@ -651,6 +651,7 @@ int snd_hdac_stream_set_lpib(struct hdac_stream *azx_dev, u32 value);
 #define snd_hdac_dsp_lock(dev)		mutex_lock(&(dev)->dsp_mutex)
 #define snd_hdac_dsp_unlock(dev)	mutex_unlock(&(dev)->dsp_mutex)
 #define snd_hdac_stream_is_locked(dev)	((dev)->locked)
+DEFINE_GUARD(snd_hdac_dsp_lock, struct hdac_stream *, snd_hdac_dsp_lock(_T), snd_hdac_dsp_unlock(_T))
 /* DSP loader helpers */
 int snd_hdac_dsp_prepare(struct hdac_stream *azx_dev, unsigned int format,
 			 unsigned int byte_size, struct snd_dma_buffer *bufp);
diff --git a/sound/hda/common/controller.c b/sound/hda/common/controller.c
index 84387ed761be..ceab8625bb1f 100644
--- a/sound/hda/common/controller.c
+++ b/sound/hda/common/controller.c
@@ -32,8 +32,11 @@
 #include "controller_trace.h"
 
 /* DSP lock helpers */
-#define dsp_lock(dev)		snd_hdac_dsp_lock(azx_stream(dev))
-#define dsp_unlock(dev)		snd_hdac_dsp_unlock(azx_stream(dev))
+#ifdef CONFIG_SND_HDA_DSP_LOADER
+#define guard_dsp_lock(dev)	guard(snd_hdac_dsp_lock)(azx_stream(dev))
+#else
+#define guard_dsp_lock(dev)	do {} while (0)
+#endif
 #define dsp_is_locked(dev)	snd_hdac_stream_is_locked(azx_stream(dev))
 
 /* assign a stream for the PCM */
@@ -110,14 +113,11 @@ static int azx_pcm_hw_params(struct snd_pcm_substream *substream,
 	struct azx *chip = apcm->chip;
 	struct azx_dev *azx_dev = get_azx_dev(substream);
 	struct hdac_stream *hdas = azx_stream(azx_dev);
-	int ret = 0;
 
 	trace_azx_pcm_hw_params(chip, azx_dev);
-	dsp_lock(azx_dev);
-	if (dsp_is_locked(azx_dev)) {
-		ret = -EBUSY;
-		goto unlock;
-	}
+	guard_dsp_lock(azx_dev);
+	if (dsp_is_locked(azx_dev))
+		return -EBUSY;
 
 	/* Set up BDLEs here, return -ENOMEM if too many BDLEs are required */
 	hdas->bufsize = params_buffer_bytes(hw_params);
@@ -127,11 +127,9 @@ static int azx_pcm_hw_params(struct snd_pcm_substream *substream,
 		(hw_params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
 		(hw_params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
 	if (snd_hdac_stream_setup_periods(hdas) < 0)
-		ret = -ENOMEM;
+		return -ENOMEM;
 
-unlock:
-	dsp_unlock(azx_dev);
-	return ret;
+	return 0;
 }
 
 static int azx_pcm_hw_free(struct snd_pcm_substream *substream)
@@ -141,14 +139,13 @@ static int azx_pcm_hw_free(struct snd_pcm_substream *substream)
 	struct hda_pcm_stream *hinfo = to_hda_pcm_stream(substream);
 
 	/* reset BDL address */
-	dsp_lock(azx_dev);
+	guard_dsp_lock(azx_dev);
 	if (!dsp_is_locked(azx_dev))
 		snd_hdac_stream_cleanup(azx_stream(azx_dev));
 
 	snd_hda_codec_cleanup(apcm->codec, hinfo, substream);
 
 	azx_stream(azx_dev)->prepared = 0;
-	dsp_unlock(azx_dev);
 	return 0;
 }
 
@@ -166,11 +163,9 @@ static int azx_pcm_prepare(struct snd_pcm_substream *substream)
 	unsigned short ctls = spdif ? spdif->ctls : 0;
 
 	trace_azx_pcm_prepare(chip, azx_dev);
-	dsp_lock(azx_dev);
-	if (dsp_is_locked(azx_dev)) {
-		err = -EBUSY;
-		goto unlock;
-	}
+	guard_dsp_lock(azx_dev);
+	if (dsp_is_locked(azx_dev))
+		return -EBUSY;
 
 	snd_hdac_stream_reset(azx_stream(azx_dev));
 	bits = snd_hdac_stream_format_bits(runtime->format, SNDRV_PCM_SUBFORMAT_STD, hinfo->maxbps);
@@ -180,13 +175,12 @@ static int azx_pcm_prepare(struct snd_pcm_substream *substream)
 		dev_err(chip->card->dev,
 			"invalid format_val, rate=%d, ch=%d, format=%d\n",
 			runtime->rate, runtime->channels, runtime->format);
-		err = -EINVAL;
-		goto unlock;
+		return -EINVAL;
 	}
 
 	err = snd_hdac_stream_set_params(azx_stream(azx_dev), format_val);
 	if (err < 0)
-		goto unlock;
+		return err;
 
 	snd_hdac_stream_setup(azx_stream(azx_dev), false);
 
@@ -197,12 +191,11 @@ static int azx_pcm_prepare(struct snd_pcm_substream *substream)
 		stream_tag -= chip->capture_streams;
 	err = snd_hda_codec_prepare(apcm->codec, hinfo, stream_tag,
 				     azx_dev->core.format_val, substream);
+	if (err < 0)
+		return err;
 
- unlock:
-	if (!err)
-		azx_stream(azx_dev)->prepared = 1;
-	dsp_unlock(azx_dev);
-	return err;
+	azx_stream(azx_dev)->prepared = 1;
+	return 0;
 }
 
 static int azx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
diff --git a/sound/hda/core/stream.c b/sound/hda/core/stream.c
index 4a87bef8834f..0caeebcc591a 100644
--- a/sound/hda/core/stream.c
+++ b/sound/hda/core/stream.c
@@ -922,12 +922,11 @@ int snd_hdac_dsp_prepare(struct hdac_stream *azx_dev, unsigned int format,
 	struct hdac_bus *bus = azx_dev->bus;
 	int err;
 
-	snd_hdac_dsp_lock(azx_dev);
+	guard(snd_hdac_dsp_lock)(azx_dev);
 	spin_lock_irq(&bus->reg_lock);
 	if (azx_dev->running || azx_dev->locked) {
 		spin_unlock_irq(&bus->reg_lock);
-		err = -EBUSY;
-		goto unlock;
+		return -EBUSY;
 	}
 	azx_dev->locked = true;
 	spin_unlock_irq(&bus->reg_lock);
@@ -951,7 +950,6 @@ int snd_hdac_dsp_prepare(struct hdac_stream *azx_dev, unsigned int format,
 		goto error;
 
 	snd_hdac_stream_setup(azx_dev, true);
-	snd_hdac_dsp_unlock(azx_dev);
 	return azx_dev->stream_tag;
 
  error:
@@ -960,8 +958,6 @@ int snd_hdac_dsp_prepare(struct hdac_stream *azx_dev, unsigned int format,
 	spin_lock_irq(&bus->reg_lock);
 	azx_dev->locked = false;
 	spin_unlock_irq(&bus->reg_lock);
- unlock:
-	snd_hdac_dsp_unlock(azx_dev);
 	return err;
 }
 EXPORT_SYMBOL_GPL(snd_hdac_dsp_prepare);
@@ -993,7 +989,7 @@ void snd_hdac_dsp_cleanup(struct hdac_stream *azx_dev,
 	if (!dmab->area || !azx_dev->locked)
 		return;
 
-	snd_hdac_dsp_lock(azx_dev);
+	guard(snd_hdac_dsp_lock)(azx_dev);
 	/* reset BDL address */
 	snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
 	snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
@@ -1008,7 +1004,6 @@ void snd_hdac_dsp_cleanup(struct hdac_stream *azx_dev,
 	spin_lock_irq(&bus->reg_lock);
 	azx_dev->locked = false;
 	spin_unlock_irq(&bus->reg_lock);
-	snd_hdac_dsp_unlock(azx_dev);
 }
 EXPORT_SYMBOL_GPL(snd_hdac_dsp_cleanup);
 #endif /* CONFIG_SND_HDA_DSP_LOADER */
-- 
2.50.1


  parent reply	other threads:[~2025-08-11 10:08 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-11 10:07 [PATCH 00/25] ALSA: hda: Use auto-cleanup macros Takashi Iwai
2025-08-11 10:07 ` [PATCH 01/25] ALSA: hda: Introduce auto cleanup macros for PM Takashi Iwai
2025-08-11 10:07 ` [PATCH 02/25] ALSA: hda/ca0132: Use cleanup macros for PM controls Takashi Iwai
2025-08-11 10:07 ` [PATCH 03/25] ALSA: hda/hdmi: " Takashi Iwai
2025-08-11 10:07 ` [PATCH 04/25] ALSA: hda/realtek: " Takashi Iwai
2025-08-11 10:07 ` [PATCH 05/25] ALSA: hda/common: " Takashi Iwai
2025-08-11 10:07 ` Takashi Iwai [this message]
2025-08-11 10:07 ` [PATCH 07/25] ALSA: hda/common: Use guard() for mutex locks Takashi Iwai
2025-08-11 10:07 ` [PATCH 08/25] ALSA: hda/core: " Takashi Iwai
2025-08-11 10:07 ` [PATCH 09/25] ALSA: hda/ca0132: " Takashi Iwai
2025-08-11 10:07 ` [PATCH 10/25] ALSA: hda/hdmi: " Takashi Iwai
2025-08-11 10:07 ` [PATCH 11/25] ALSA: hda/realtek: " Takashi Iwai
2025-08-11 10:07 ` [PATCH 12/25] ALSA: hda/cs35l41: " Takashi Iwai
2025-08-11 10:07 ` [PATCH 13/25] ALSA: hda/tas2781: " Takashi Iwai
2025-08-11 10:07 ` [PATCH 14/25] ALSA: hda/cs8409: " Takashi Iwai
2025-08-11 10:07 ` [PATCH 15/25] ALSA: hda/component: " Takashi Iwai
2025-08-11 10:07 ` [PATCH 16/25] ALSA: hda/generic: " Takashi Iwai
2025-08-11 10:07 ` [PATCH 17/25] ALSA: hda/analog: " Takashi Iwai
2025-08-11 10:07 ` [PATCH 18/25] ALSA: hda/intel: " Takashi Iwai
2025-08-11 10:07 ` [PATCH 19/25] ALSA: hda/common: Use auto cleanup for temporary buffers Takashi Iwai
2025-08-11 10:07 ` [PATCH 20/25] ALSA: hda/realtek: " Takashi Iwai
2025-08-11 10:07 ` [PATCH 21/25] ALSA: hda/generic: " Takashi Iwai
2025-08-11 10:07 ` [PATCH 22/25] ALSA: hda/ext: Use guard() for spinlocks Takashi Iwai
2025-08-11 10:07 ` [PATCH 23/25] ALSA: hda/core: " Takashi Iwai
2025-08-13 14:07   ` Andy Shevchenko
2025-08-13 14:10     ` Andy Shevchenko
2025-08-13 14:19       ` Takashi Iwai
2025-08-11 10:07 ` [PATCH 24/25] ALSA: hda/common: " Takashi Iwai
2025-08-11 10:07 ` [PATCH 25/25] ALSA: hda/intel: " Takashi Iwai

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250811100807.7962-7-tiwai@suse.de \
    --to=tiwai@suse.de \
    --cc=baojun.xu@ti.com \
    --cc=david.rhodes@cirrus.com \
    --cc=kevin-lu@ti.com \
    --cc=linux-sound@vger.kernel.org \
    --cc=patches@opensource.cirrus.com \
    --cc=rf@opensource.cirrus.com \
    --cc=shenghao-ding@ti.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox