ALSA: hda: document/simplify hdac_ext handling
Subsystems (3)
cover ALSA: hda: document/simplify hdac_ext handling
This sound/hda/ext library is a confusing magic black box that very few understand. It needn't be that way, we can document/simplify and make the code clearer.
Pierre-Louis Bossart (8):
ALSA: hda: make snd_hdac_stream_clear() static
ALSA: hda: document state machine for hdac_streams
ALSA: hda: ext: make snd_hdac_ext_stream_init() static
ALSA: hda: Use hdac_ext prefix in snd_hdac_stream_free_all() for
clarity
ALSA: hda: add snd_hdac_stop_streams() helper
ALSA: hda: ext: simplify logic for stream assignment
ALSA: hda: ext: fix locking in stream_release
ALSA: hda: ext: remove always-true conditions on host and link release
include/sound/hdaudio.h | 3 +- include/sound/hdaudio_ext.h | 5 +-- sound/hda/ext/hdac_ext_stream.c | 34 +++++++-------- sound/hda/hdac_stream.c | 74 +++++++++++++++++++++++++++++---- sound/pci/hda/hda_controller.c | 4 +- sound/soc/intel/avs/core.c | 4 +- sound/soc/intel/skylake/skl.c | 2 +- 7 files changed, 87 insertions(+), 39 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/8 ALSA: hda: make snd_hdac_stream_clear() static
This helper has no users outside of hdac_stream.c. External users should only use snd_hdac_stream_start() and snd_hdac_stream_stop().
No functional change beyond making the function static and removing the symbol export.
--- include/sound/hdaudio.h | 1 - sound/hda/hdac_stream.c | 5 ++--- 2 files changed, 2 insertions(+), 4 deletions(-)
include/sound/hdaudio.h +0 -1
@@ -561,7 +561,6 @@ int snd_hdac_stream_setup_periods(struct hdac_stream *azx_dev);int snd_hdac_stream_set_params (struct hdac_stream *azx_dev,unsigned int format_val);void snd_hdac_stream_start (struct hdac_stream *azx_dev,bool fresh_start); -void snd_hdac_stream_clear (struct hdac_stream *azx_dev);void snd_hdac_stream_stop (struct hdac_stream *azx_dev);void snd_hdac_stop_streams_and_chip (struct hdac_bus *bus);void snd_hdac_stream_reset (struct hdac_stream *azx_dev);
sound/hda/hdac_stream.c +2 -3
@@ -112,10 +112,10 @@ void snd_hdac_stream_start(struct hdac_stream *azx_dev, bool fresh_start)EXPORT_SYMBOL_GPL (snd_hdac_stream_start);/** -* snd_hdac_stream_clear - stop a stream DMA +* snd_hdac_stream_clear - helper to clear stream registers and stop DMA transfers * @azx_dev: HD-audio core stream to stop */ -void snd_hdac_stream_clear (struct hdac_stream *azx_dev) +static void snd_hdac_stream_clear (struct hdac_stream *azx_dev) {snd_hdac_stream_updateb (azx_dev,SD_CTL ,SD_CTL_DMA_START |SD_INT_MASK ,0 );
@@ -124,7 +124,6 @@ void snd_hdac_stream_clear(struct hdac_stream *azx_dev)snd_hdac_stream_updateb (azx_dev,SD_CTL_3B ,SD_CTL_STRIPE_MASK ,0 ); azx_dev->running = false; } -EXPORT_SYMBOL_GPL (snd_hdac_stream_clear);/** * snd_hdac_stream_stop - stop a stream
Signature · 1 line
2.34.1
PATCH 2/8 ALSA: hda: document state machine for hdac_streams
In reply to Pierre-Louis BossartThe code in this library is far from self-explanatory, hopefully this state diagram reverse-engineered from the code will help others understand the expected transitions.
--- sound/hda/hdac_stream.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
sound/hda/hdac_stream.c +33 -0
@@ -13,6 +13,39 @@#include <sound/hda_register.h> #include "trace.h" +/* +* the hdac_stream library is intended to be used with the following +* transitions. The states are not formally defined in the code but loosely +* inspired by boolean variables. Note that the 'prepared' field is not used +* in this library but by the callers during the hw_params/prepare transitions +* +* | +* stream_init() | +* v +* +--+-------+ +* | unused | +* +--+----+--+ +* | ^ +* stream_assign() | | stream_release() +* v | +* +--+----+--+ +* | opened | +* +--+----+--+ +* | ^ +* stream_reset() | | +* stream_setup() | | stream_cleanup() +* v | +* +--+----+--+ +* | prepared | +* +--+----+--+ +* | ^ +* stream_start() | | stream_stop() +* v | +* +--+----+--+ +* | running | +* +----------+ +*/ +/** * snd_hdac_get_stream_stripe_ctl - get stripe control value * @bus: HD-audio core bus
Signature · 1 line
2.34.1
PATCH 3/8 ALSA: hda: ext: make snd_hdac_ext_stream_init() static
In reply to Pierre-Louis BossartThere are no external users of this helper, move to static and remove sympol export. No functionality change.
--- include/sound/hdaudio_ext.h | 3 --- sound/hda/ext/hdac_ext_stream.c | 7 +++---- 2 files changed, 3 insertions(+), 7 deletions(-)
include/sound/hdaudio_ext.h +0 -3
@@ -77,9 +77,6 @@ struct hdac_ext_stream {#define stream_to_hdac_ext_stream (s) \ container_of(s, struct hdac_ext_stream, hstream) -void snd_hdac_ext_stream_init (struct hdac_bus *bus, -struct hdac_ext_stream *hext_stream,int idx, -int direction,int tag);int snd_hdac_ext_stream_init_all (struct hdac_bus *bus,int start_idx,int num_stream,int dir);void snd_hdac_stream_free_all (struct hdac_bus *bus);
sound/hda/ext/hdac_ext_stream.c +3 -4
@@ -26,9 +26,9 @@* initialize the stream, if ppcap is enabled then init those and then * invoke hdac stream initialization routine */ -void snd_hdac_ext_stream_init (struct hdac_bus *bus, -struct hdac_ext_stream *hext_stream, -int idx,int direction,int tag) +static void snd_hdac_ext_stream_init (struct hdac_bus *bus, +struct hdac_ext_stream *hext_stream, +int idx,int direction,int tag) {if (bus->ppcap) { hext_stream->pphc_addr = bus->ppcap + AZX_PPHC_BASE +
@@ -56,7 +56,6 @@ void snd_hdac_ext_stream_init(struct hdac_bus *bus, hext_stream->decoupled = false;snd_hdac_stream_init (bus, &hext_stream->hstream, idx, direction, tag); } -EXPORT_SYMBOL_GPL (snd_hdac_ext_stream_init);/** * snd_hdac_ext_stream_init_all - create and initialize the stream objects
Signature · 1 line
2.34.1
PATCH 4/8 ALSA: hda: Use hdac_ext prefix in snd_hdac_stream_free_all() for clarity
In reply to Pierre-Louis BossartMake sure there's no ambiguity on layering with the appropriate prefix added.
Pure rename, no functionality changed.
--- include/sound/hdaudio_ext.h | 2 +- sound/hda/ext/hdac_ext_stream.c | 6 +++--- sound/soc/intel/avs/core.c | 4 ++-- sound/soc/intel/skylake/skl.c | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-)
include/sound/hdaudio_ext.h +1 -1
@@ -79,7 +79,7 @@ struct hdac_ext_stream {int snd_hdac_ext_stream_init_all (struct hdac_bus *bus,int start_idx,int num_stream,int dir); -void snd_hdac_stream_free_all (struct hdac_bus *bus); +void snd_hdac_ext_stream_free_all (struct hdac_bus *bus);void snd_hdac_link_free_all (struct hdac_bus *bus); struct hdac_ext_stream *snd_hdac_ext_stream_assign(struct hdac_bus *bus, struct snd_pcm_substream *substream,
sound/hda/ext/hdac_ext_stream.c +3 -3
@@ -87,11 +87,11 @@ int snd_hdac_ext_stream_init_all(struct hdac_bus *bus, int start_idx,EXPORT_SYMBOL_GPL (snd_hdac_ext_stream_init_all);/** -* snd_hdac_stream_free_all - free hdac extended stream objects +* snd_hdac_ext_stream_free_all - free hdac extended stream objects * * @bus: HD-audio core bus */ -void snd_hdac_stream_free_all (struct hdac_bus *bus) +void snd_hdac_ext_stream_free_all (struct hdac_bus *bus) {struct hdac_stream *s, *_s;struct hdac_ext_stream *hext_stream;
@@ -103,7 +103,7 @@ void snd_hdac_stream_free_all(struct hdac_bus *bus)kfree (hext_stream); } } -EXPORT_SYMBOL_GPL (snd_hdac_stream_free_all); +EXPORT_SYMBOL_GPL (snd_hdac_ext_stream_free_all); void snd_hdac_ext_stream_decouple_locked(struct hdac_bus *bus, struct hdac_ext_stream *hext_stream,
sound/soc/intel/avs/core.c +2 -2
@@ -466,7 +466,7 @@ static int avs_pci_probe(struct pci_dev *pci, const struct pci_device_id *id) err_acquire_irq:snd_hdac_bus_free_stream_pages (bus); -snd_hdac_stream_free_all (bus); +snd_hdac_ext_stream_free_all (bus); err_init_streams:iounmap (adev->dsp_ba); err_remap_bar4:
@@ -502,7 +502,7 @@ static void avs_pci_remove(struct pci_dev *pci)snd_hda_codec_unregister (hdac_to_hda_codec (hdev));snd_hdac_bus_free_stream_pages (bus); -snd_hdac_stream_free_all (bus); +snd_hdac_ext_stream_free_all (bus);/* reverse ml_capabilities */ snd_hdac_link_free_all (bus);snd_hdac_ext_bus_exit (bus);
sound/soc/intel/skylake/skl.c +1 -1
@@ -444,7 +444,7 @@ static int skl_free(struct hdac_bus *bus)if (bus->irq >=0 )free_irq (bus->irq, (void *)bus);snd_hdac_bus_free_stream_pages (bus); -snd_hdac_stream_free_all (bus); +snd_hdac_ext_stream_free_all (bus);snd_hdac_link_free_all (bus);if (bus->remap_addr)
Signature · 1 line
2.34.1
PATCH 5/8 ALSA: hda: add snd_hdac_stop_streams() helper
In reply to Pierre-Louis BossartMinor code reuse, no functionality change.
--- include/sound/hdaudio.h | 1 + sound/hda/hdac_stream.c | 17 ++++++++++++++--- sound/pci/hda/hda_controller.c | 4 +--- 3 files changed, 16 insertions(+), 6 deletions(-)
include/sound/hdaudio.h +1 -0
@@ -562,6 +562,7 @@ int snd_hdac_stream_set_params(struct hdac_stream *azx_dev, unsigned int format_val);void snd_hdac_stream_start (struct hdac_stream *azx_dev,bool fresh_start);void snd_hdac_stream_stop (struct hdac_stream *azx_dev); +void snd_hdac_stop_streams (struct hdac_bus *bus);void snd_hdac_stop_streams_and_chip (struct hdac_bus *bus);void snd_hdac_stream_reset (struct hdac_stream *azx_dev); void snd_hdac_stream_sync_trigger(struct hdac_stream *azx_dev, bool set,
sound/hda/hdac_stream.c +14 -3
@@ -174,17 +174,28 @@ void snd_hdac_stream_stop(struct hdac_stream *azx_dev) }EXPORT_SYMBOL_GPL (snd_hdac_stream_stop); +/** +* snd_hdac_stop_streams - stop all streams +* @bus: HD-audio core bus +*/ +void snd_hdac_stop_streams (struct hdac_bus *bus) +{ +struct hdac_stream *stream; + +list_for_each_entry (stream, &bus->stream_list, list) +snd_hdac_stream_stop (stream); +} +EXPORT_SYMBOL_GPL (snd_hdac_stop_streams); +/** * snd_hdac_stop_streams_and_chip - stop all streams and chip if running * @bus: HD-audio core bus */ void snd_hdac_stop_streams_and_chip (struct hdac_bus *bus) { -struct hdac_stream *stream;if (bus->chip_init) { -list_for_each_entry (stream, &bus->stream_list, list) -snd_hdac_stream_stop (stream); +snd_hdac_stop_streams (bus);snd_hdac_bus_stop_chip (bus); } }
sound/pci/hda/hda_controller.c +1 -3
@@ -1033,10 +1033,8 @@ EXPORT_SYMBOL_GPL(azx_init_chip);void azx_stop_all_streams (struct azx *chip) {struct hdac_bus *bus =azx_bus (chip); -struct hdac_stream *s; -list_for_each_entry (s, &bus->stream_list, list) -snd_hdac_stream_stop (s); +snd_hdac_stop_streams (bus); }EXPORT_SYMBOL_GPL (azx_stop_all_streams);
Signature · 1 line
2.34.1
PATCH 6/8 ALSA: hda: ext: simplify logic for stream assignment
In reply to Pierre-Louis BossartThe logic is needlessly complicated, the basic rule is:
The host streams can be found by checking the 'opened' boolean. The link streams can be found by checking the 'link_locked' boolean.
Once a stream is found, it can be unconditionally decoupled. The snd_hdac_ext_stream_decouple_locked() routine will make sure the register status is modified as needed and the 'decoupled' boolean set.
--- sound/hda/ext/hdac_ext_stream.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)
sound/hda/ext/hdac_ext_stream.c +4 -9
@@ -267,19 +267,15 @@ hdac_ext_link_stream_assign(struct hdac_bus *bus,if (hstream->direction != substream->stream)continue ; -/* check if decoupled stream and not in use is available */ -if (hext_stream->decoupled && !hext_stream->link_locked) { - res = hext_stream; -break ; - } - +/* check if link stream is available */ if (!hext_stream->link_locked) { -snd_hdac_ext_stream_decouple_locked (bus, hext_stream, true); res = hext_stream;break ; } + }if (res) { +snd_hdac_ext_stream_decouple_locked (bus, res, true); res->link_locked =1 ; res->link_substream = substream; }
@@ -308,13 +304,12 @@ hdac_ext_host_stream_assign(struct hdac_bus *bus,continue ;if (!hstream->opened) { -if (!hext_stream->decoupled) -snd_hdac_ext_stream_decouple_locked (bus, hext_stream, true); res = hext_stream;break ; } }if (res) { +snd_hdac_ext_stream_decouple_locked (bus, res, true); res->hstream.opened =1 ; res->hstream.running =0 ; res->hstream.substream = substream;
Signature · 1 line
2.34.1
PATCH 7/8 ALSA: hda: ext: fix locking in stream_release
In reply to Pierre-Louis BossartThe snd_hdac_ext_stream_release() routine uses the bus reg_lock, but releases it before calling snd_hdac_stream_release() where the bus reg_lock is taken again.
This creates a timing window where the link stream release could test an invalid 'opened' boolean status and fail to recouple the host and link parts.
Fix by exposing a locked version of snd_hdac_stream_release() and use it without releasing the spinlock.
--- include/sound/hdaudio.h | 1 + sound/hda/ext/hdac_ext_stream.c | 2 +- sound/hda/hdac_stream.c | 19 ++++++++++++++++--- 3 files changed, 18 insertions(+), 4 deletions(-)
include/sound/hdaudio.h +1 -0
@@ -551,6 +551,7 @@ void snd_hdac_stream_init(struct hdac_bus *bus, struct hdac_stream *azx_dev, int idx, int direction, int tag);struct hdac_stream *snd_hdac_stream_assign (struct hdac_bus *bus,struct snd_pcm_substream *substream); +void snd_hdac_stream_release_locked (struct hdac_stream *azx_dev);void snd_hdac_stream_release (struct hdac_stream *azx_dev);struct hdac_stream *snd_hdac_get_stream (struct hdac_bus *bus,int dir,int stream_tag);
sound/hda/ext/hdac_ext_stream.c +1 -1
@@ -384,8 +384,8 @@ void snd_hdac_ext_stream_release(struct hdac_ext_stream *hext_stream, int type)spin_lock_irq (&bus->reg_lock);if (hext_stream->decoupled && !hext_stream->link_locked)snd_hdac_ext_stream_decouple_locked (bus, hext_stream, false); +snd_hdac_stream_release_locked (&hext_stream->hstream);spin_unlock_irq (&bus->reg_lock); -snd_hdac_stream_release (&hext_stream->hstream);break ;case HDAC_EXT_STREAM_TYPE_LINK :
sound/hda/hdac_stream.c +16 -3
@@ -365,6 +365,21 @@ struct hdac_stream *snd_hdac_stream_assign(struct hdac_bus *bus, }EXPORT_SYMBOL_GPL (snd_hdac_stream_assign); +/** +* snd_hdac_stream_release_locked - release the assigned stream +* @azx_dev: HD-audio core stream to release +* +* Release the stream that has been assigned by snd_hdac_stream_assign(). +* The bus->reg_lock needs to be taken at a higher level +*/ +void snd_hdac_stream_release_locked (struct hdac_stream *azx_dev) +{ + azx_dev->opened =0 ; + azx_dev->running =0 ; + azx_dev->substream =NULL ; +} +EXPORT_SYMBOL_GPL (snd_hdac_stream_release_locked); +/** * snd_hdac_stream_release - release the assigned stream * @azx_dev: HD-audio core stream to release
@@ -376,9 +391,7 @@ void snd_hdac_stream_release(struct hdac_stream *azx_dev)struct hdac_bus *bus = azx_dev->bus;spin_lock_irq (&bus->reg_lock); - azx_dev->opened =0 ; - azx_dev->running =0 ; - azx_dev->substream =NULL ; +snd_hdac_stream_release_locked (azx_dev);spin_unlock_irq (&bus->reg_lock); }EXPORT_SYMBOL_GPL (snd_hdac_stream_release);
Signature · 1 line
2.34.1
PATCH 8/8 ALSA: hda: ext: remove always-true conditions on host and link release
In reply to Pierre-Louis BossartBy construction a host and link DMA are always decoupled. This decoupling happens in the assign() phase. There's no point in checking if the two parts are decoupled, this is by-design always-true.
--- sound/hda/ext/hdac_ext_stream.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
sound/hda/ext/hdac_ext_stream.c +4 -2
@@ -382,7 +382,8 @@ void snd_hdac_ext_stream_release(struct hdac_ext_stream *hext_stream, int type)case HDAC_EXT_STREAM_TYPE_HOST :spin_lock_irq (&bus->reg_lock); -if (hext_stream->decoupled && !hext_stream->link_locked) +/* couple link only if not in use */ +if (!hext_stream->link_locked)snd_hdac_ext_stream_decouple_locked (bus, hext_stream, false);snd_hdac_stream_release_locked (&hext_stream->hstream);spin_unlock_irq (&bus->reg_lock);
@@ -390,7 +391,8 @@ void snd_hdac_ext_stream_release(struct hdac_ext_stream *hext_stream, int type)case HDAC_EXT_STREAM_TYPE_LINK :spin_lock_irq (&bus->reg_lock); -if (hext_stream->decoupled && !hext_stream->hstream.opened) +/* couple host only if not in use */ +if (!hext_stream->hstream.opened)snd_hdac_ext_stream_decouple_locked (bus, hext_stream, false); hext_stream->link_locked =0 ; hext_stream->link_substream =NULL ;
Signature · 1 line
2.34.1
Takashi Iwai
AppliedQuoted 5 lines · Pierre-Louis Bossart
This
sound/hda/extlibrary is a confusing magic black box that very few understand. It needn't be that way, we can document/simplify and make the code clearer.
Quoted 10 lines
Pierre-Louis Bossart (8):
ALSA: hda: makesnd_hdac_stream_clear()static
ALSA: hda: document state machine forhdac_streams
ALSA: hda: ext: makesnd_hdac_ext_stream_init()static
ALSA: hda: Usehdac_extprefix insnd_hdac_stream_free_all()for
clarity
ALSA: hda: addsnd_hdac_stop_streams()helper
ALSA: hda: ext: simplify logic for stream assignment
ALSA: hda: ext: fix locking instream_release
ALSA: hda: ext: remove always-true conditions on host and link release
Applied all patches now. Thanks.
Takashi