Commit 313d9f28 authored by Takashi Iwai's avatar Takashi Iwai

ALSA: x86: Properly manage PCM substream lifetype

The PCM substream is referred not only in the PCM callbacks but also
in the irq handler and in the hotplug/unplug codes.  The latter code
paths don't take the PCM lock, thus the PCM may be released
unexpectedly while calling PCM helper functions or accessing
pcm->runtime fields.

This patch implements a simple refcount to assure the PCM substream
accessibility while the other codes are accessing.  It needed some
code refactoring in the relevant functions for avoiding the doubly
spinlocks.
Signed-off-by: default avatarTakashi Iwai <tiwai@suse.de>
parent 7d9e7986
...@@ -154,6 +154,36 @@ static const struct snd_pcm_hardware snd_intel_hadstream = { ...@@ -154,6 +154,36 @@ static const struct snd_pcm_hardware snd_intel_hadstream = {
.fifo_size = HAD_FIFO_SIZE, .fifo_size = HAD_FIFO_SIZE,
}; };
/* Get the active PCM substream;
* Call had_substream_put() for unreferecing.
* Don't call this inside had_spinlock, as it takes by itself
*/
static struct snd_pcm_substream *
had_substream_get(struct snd_intelhad *intelhaddata)
{
struct snd_pcm_substream *substream;
unsigned long flags;
spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
substream = intelhaddata->stream_info.substream;
if (substream)
intelhaddata->stream_info.substream_refcount++;
spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
return substream;
}
/* Unref the active PCM substream;
* Don't call this inside had_spinlock, as it takes by itself
*/
static void had_substream_put(struct snd_intelhad *intelhaddata)
{
unsigned long flags;
spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
intelhaddata->stream_info.substream_refcount--;
spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
}
/* Register access functions */ /* Register access functions */
static inline void static inline void
mid_hdmi_audio_read(struct snd_intelhad *ctx, u32 reg, u32 *val) mid_hdmi_audio_read(struct snd_intelhad *ctx, u32 reg, u32 *val)
...@@ -215,7 +245,8 @@ static int had_read_modify(struct snd_intelhad *intelhaddata, u32 offset, ...@@ -215,7 +245,8 @@ static int had_read_modify(struct snd_intelhad *intelhaddata, u32 offset,
} }
/* /*
* function to read-modify AUD_CONFIG register on VLV2. * enable / disable audio configuration
*
* The had_read_modify() function should not directly be used on VLV2 for * The had_read_modify() function should not directly be used on VLV2 for
* updating AUD_CONFIG register. * updating AUD_CONFIG register.
* This is because: * This is because:
...@@ -227,39 +258,33 @@ static int had_read_modify(struct snd_intelhad *intelhaddata, u32 offset, ...@@ -227,39 +258,33 @@ static int had_read_modify(struct snd_intelhad *intelhaddata, u32 offset,
* causes the "channels" field to be updated as 0xy binary resulting in * causes the "channels" field to be updated as 0xy binary resulting in
* bad audio. The fix is to always write the AUD_CONFIG[6:4] with * bad audio. The fix is to always write the AUD_CONFIG[6:4] with
* appropriate value when doing read-modify of AUD_CONFIG register. * appropriate value when doing read-modify of AUD_CONFIG register.
*
* @substream: the current substream or NULL if no active substream
* @data : data to be written
* @mask : mask
*
*/ */
static int had_read_modify_aud_config_v2(struct snd_intelhad *intelhaddata, static void snd_intelhad_enable_audio(struct snd_pcm_substream *substream,
u32 data, u32 mask) struct snd_intelhad *intelhaddata,
bool enable)
{ {
struct snd_pcm_substream *substream;
union aud_cfg cfg_val = {.cfg_regval = 0}; union aud_cfg cfg_val = {.cfg_regval = 0};
u8 channels; u8 channels, data, mask;
/* /*
* If substream is NULL, there is no active stream. * If substream is NULL, there is no active stream.
* In this case just set channels to 2 * In this case just set channels to 2
*/ */
substream = intelhaddata->stream_info.had_substream; channels = substream ? substream->runtime->channels : 2;
if (substream && substream->runtime)
channels = substream->runtime->channels;
else
channels = 2;
cfg_val.cfg_regx.num_ch = channels - 2; cfg_val.cfg_regx.num_ch = channels - 2;
data = data | cfg_val.cfg_regval; data = cfg_val.cfg_regval;
mask = mask | AUD_CONFIG_CH_MASK; if (enable)
data |= 1;
mask = AUD_CONFIG_CH_MASK | 1;
dev_dbg(intelhaddata->dev, "%s : data = %x, mask =%x\n", dev_dbg(intelhaddata->dev, "%s : data = %x, mask =%x\n",
__func__, data, mask); __func__, data, mask);
return had_read_modify(intelhaddata, AUD_CONFIG, data, mask); had_read_modify(intelhaddata, AUD_CONFIG, data, mask);
} }
/* enable / disable the audio interface */
static void snd_intelhad_enable_audio_int(struct snd_intelhad *ctx, bool enable) static void snd_intelhad_enable_audio_int(struct snd_intelhad *ctx, bool enable)
{ {
u32 status_reg; u32 status_reg;
...@@ -272,13 +297,6 @@ static void snd_intelhad_enable_audio_int(struct snd_intelhad *ctx, bool enable) ...@@ -272,13 +297,6 @@ static void snd_intelhad_enable_audio_int(struct snd_intelhad *ctx, bool enable)
} }
} }
static void snd_intelhad_enable_audio(struct snd_intelhad *intelhaddata,
bool enable)
{
had_read_modify_aud_config_v2(intelhaddata, enable ? BIT(0) : 0,
BIT(0));
}
static void snd_intelhad_reset_audio(struct snd_intelhad *intelhaddata, static void snd_intelhad_reset_audio(struct snd_intelhad *intelhaddata,
u8 reset) u8 reset)
{ {
...@@ -647,21 +665,17 @@ static void snd_intelhad_prog_dip(struct snd_pcm_substream *substream, ...@@ -647,21 +665,17 @@ static void snd_intelhad_prog_dip(struct snd_pcm_substream *substream,
/* /*
* snd_intelhad_prog_buffer - programs buffer address and length registers * snd_intelhad_prog_buffer - programs buffer address and length registers
* @substream:substream for which the prepare function is called * @substream: substream for which the prepare function is called
* @intelhaddata:substream private data * @intelhaddata: substream private data
* *
* This function programs ring buffer address and length into registers. * This function programs ring buffer address and length into registers.
*/ */
static int snd_intelhad_prog_buffer(struct snd_intelhad *intelhaddata, static int snd_intelhad_prog_buffer(struct snd_pcm_substream *substream,
int start, int end) struct snd_intelhad *intelhaddata,
int start, int end)
{ {
u32 ring_buf_addr, ring_buf_size, period_bytes; u32 ring_buf_addr, ring_buf_size, period_bytes;
u8 i, num_periods; u8 i, num_periods;
struct snd_pcm_substream *substream;
substream = intelhaddata->stream_info.had_substream;
if (WARN_ON(!substream))
return 0;
ring_buf_addr = substream->runtime->dma_addr; ring_buf_addr = substream->runtime->dma_addr;
ring_buf_size = snd_pcm_lib_buffer_bytes(substream); ring_buf_size = snd_pcm_lib_buffer_bytes(substream);
...@@ -989,23 +1003,17 @@ static int snd_intelhad_open(struct snd_pcm_substream *substream) ...@@ -989,23 +1003,17 @@ static int snd_intelhad_open(struct snd_pcm_substream *substream)
goto error; goto error;
} }
spin_lock_irq(&intelhaddata->had_spinlock);
intelhaddata->stream_info.substream = substream;
intelhaddata->stream_info.substream_refcount++;
spin_unlock_irq(&intelhaddata->had_spinlock);
return retval; return retval;
error: error:
pm_runtime_put(intelhaddata->dev); pm_runtime_put(intelhaddata->dev);
return retval; return retval;
} }
/*
* had_period_elapsed - updates the hardware pointer status
* @had_substream: substream for which the stream function is called
*/
static void had_period_elapsed(struct snd_pcm_substream *substream)
{
if (!substream || !substream->runtime)
return;
snd_pcm_period_elapsed(substream);
}
/* /*
* snd_intelhad_close - to free parameteres when stream is stopped * snd_intelhad_close - to free parameteres when stream is stopped
* @substream: substream for which the function is called * @substream: substream for which the function is called
...@@ -1019,7 +1027,15 @@ static int snd_intelhad_close(struct snd_pcm_substream *substream) ...@@ -1019,7 +1027,15 @@ static int snd_intelhad_close(struct snd_pcm_substream *substream)
intelhaddata = snd_pcm_substream_chip(substream); intelhaddata = snd_pcm_substream_chip(substream);
intelhaddata->stream_info.buffer_rendered = 0; intelhaddata->stream_info.buffer_rendered = 0;
intelhaddata->stream_info.had_substream = NULL; spin_lock_irq(&intelhaddata->had_spinlock);
intelhaddata->stream_info.substream = NULL;
intelhaddata->stream_info.substream_refcount--;
while (intelhaddata->stream_info.substream_refcount > 0) {
spin_unlock_irq(&intelhaddata->had_spinlock);
cpu_relax();
spin_lock_irq(&intelhaddata->had_spinlock);
}
spin_unlock_irq(&intelhaddata->had_spinlock);
/* Check if following drv_status modification is required - VA */ /* Check if following drv_status modification is required - VA */
if (intelhaddata->drv_status != HAD_DRV_DISCONNECTED) { if (intelhaddata->drv_status != HAD_DRV_DISCONNECTED) {
...@@ -1125,7 +1141,7 @@ static int snd_intelhad_pcm_trigger(struct snd_pcm_substream *substream, ...@@ -1125,7 +1141,7 @@ static int snd_intelhad_pcm_trigger(struct snd_pcm_substream *substream,
/* Enable Audio */ /* Enable Audio */
snd_intelhad_enable_audio_int(intelhaddata, true); snd_intelhad_enable_audio_int(intelhaddata, true);
snd_intelhad_enable_audio(intelhaddata, true); snd_intelhad_enable_audio(substream, intelhaddata, true);
break; break;
case SNDRV_PCM_TRIGGER_STOP: case SNDRV_PCM_TRIGGER_STOP:
...@@ -1138,7 +1154,7 @@ static int snd_intelhad_pcm_trigger(struct snd_pcm_substream *substream, ...@@ -1138,7 +1154,7 @@ static int snd_intelhad_pcm_trigger(struct snd_pcm_substream *substream,
spin_unlock(&intelhaddata->had_spinlock); spin_unlock(&intelhaddata->had_spinlock);
/* Disable Audio */ /* Disable Audio */
snd_intelhad_enable_audio_int(intelhaddata, false); snd_intelhad_enable_audio_int(intelhaddata, false);
snd_intelhad_enable_audio(intelhaddata, false); snd_intelhad_enable_audio(substream, intelhaddata, false);
/* Reset buffer pointers */ /* Reset buffer pointers */
snd_intelhad_reset_audio(intelhaddata, 1); snd_intelhad_reset_audio(intelhaddata, 1);
snd_intelhad_reset_audio(intelhaddata, 0); snd_intelhad_reset_audio(intelhaddata, 0);
...@@ -1185,7 +1201,6 @@ static int snd_intelhad_pcm_prepare(struct snd_pcm_substream *substream) ...@@ -1185,7 +1201,6 @@ static int snd_intelhad_pcm_prepare(struct snd_pcm_substream *substream)
dev_dbg(intelhaddata->dev, "rate=%d\n", runtime->rate); dev_dbg(intelhaddata->dev, "rate=%d\n", runtime->rate);
dev_dbg(intelhaddata->dev, "channels=%d\n", runtime->channels); dev_dbg(intelhaddata->dev, "channels=%d\n", runtime->channels);
intelhaddata->stream_info.had_substream = substream;
intelhaddata->stream_info.buffer_rendered = 0; intelhaddata->stream_info.buffer_rendered = 0;
/* Get N value in KHz */ /* Get N value in KHz */
...@@ -1211,7 +1226,7 @@ static int snd_intelhad_pcm_prepare(struct snd_pcm_substream *substream) ...@@ -1211,7 +1226,7 @@ static int snd_intelhad_pcm_prepare(struct snd_pcm_substream *substream)
retval = snd_intelhad_audio_ctrl(substream, intelhaddata); retval = snd_intelhad_audio_ctrl(substream, intelhaddata);
/* Prog buffer address */ /* Prog buffer address */
retval = snd_intelhad_prog_buffer(intelhaddata, retval = snd_intelhad_prog_buffer(substream, intelhaddata,
HAD_BUF_TYPE_A, HAD_BUF_TYPE_D); HAD_BUF_TYPE_A, HAD_BUF_TYPE_D);
/* /*
...@@ -1306,12 +1321,12 @@ static int hdmi_audio_mode_change(struct snd_intelhad *intelhaddata) ...@@ -1306,12 +1321,12 @@ static int hdmi_audio_mode_change(struct snd_intelhad *intelhaddata)
u32 disp_samp_freq, n_param; u32 disp_samp_freq, n_param;
u32 link_rate = 0; u32 link_rate = 0;
substream = intelhaddata->stream_info.had_substream; substream = had_substream_get(intelhaddata);
if (!substream || !substream->runtime) if (!substream)
return 0; return 0;
/* Disable Audio */ /* Disable Audio */
snd_intelhad_enable_audio(intelhaddata, false); snd_intelhad_enable_audio(substream, intelhaddata, false);
/* Update CTS value */ /* Update CTS value */
disp_samp_freq = intelhaddata->tmds_clock_speed; disp_samp_freq = intelhaddata->tmds_clock_speed;
...@@ -1332,9 +1347,10 @@ static int hdmi_audio_mode_change(struct snd_intelhad *intelhaddata) ...@@ -1332,9 +1347,10 @@ static int hdmi_audio_mode_change(struct snd_intelhad *intelhaddata)
n_param, intelhaddata); n_param, intelhaddata);
/* Enable Audio */ /* Enable Audio */
snd_intelhad_enable_audio(intelhaddata, true); snd_intelhad_enable_audio(substream, intelhaddata, true);
out: out:
had_substream_put(intelhaddata);
return retval; return retval;
} }
...@@ -1348,11 +1364,9 @@ static int hdmi_lpe_audio_suspend(struct platform_device *pdev, ...@@ -1348,11 +1364,9 @@ static int hdmi_lpe_audio_suspend(struct platform_device *pdev,
pm_message_t state) pm_message_t state)
{ {
struct had_stream_data *had_stream; struct had_stream_data *had_stream;
struct snd_pcm_substream *substream;
struct snd_intelhad *intelhaddata = platform_get_drvdata(pdev); struct snd_intelhad *intelhaddata = platform_get_drvdata(pdev);
had_stream = &intelhaddata->stream_data; had_stream = &intelhaddata->stream_data;
substream = intelhaddata->stream_info.had_substream;
if (!pm_runtime_status_suspended(intelhaddata->dev)) { if (!pm_runtime_status_suspended(intelhaddata->dev)) {
dev_err(intelhaddata->dev, "audio stream is active\n"); dev_err(intelhaddata->dev, "audio stream is active\n");
...@@ -1463,10 +1477,10 @@ static int had_process_buffer_done(struct snd_intelhad *intelhaddata) ...@@ -1463,10 +1477,10 @@ static int had_process_buffer_done(struct snd_intelhad *intelhaddata)
enum intel_had_aud_buf_type buf_id; enum intel_had_aud_buf_type buf_id;
enum intel_had_aud_buf_type buff_done; enum intel_had_aud_buf_type buff_done;
struct pcm_stream_info *stream; struct pcm_stream_info *stream;
struct snd_pcm_substream *substream;
u32 buf_size; u32 buf_size;
struct had_stream_data *had_stream; struct had_stream_data *had_stream;
int intr_count; int intr_count;
enum had_status_stream stream_type;
unsigned long flags; unsigned long flags;
had_stream = &intelhaddata->stream_data; had_stream = &intelhaddata->stream_data;
...@@ -1484,7 +1498,6 @@ static int had_process_buffer_done(struct snd_intelhad *intelhaddata) ...@@ -1484,7 +1498,6 @@ static int had_process_buffer_done(struct snd_intelhad *intelhaddata)
intelhaddata->buff_done = buf_id; intelhaddata->buff_done = buf_id;
buff_done = intelhaddata->buff_done; buff_done = intelhaddata->buff_done;
buf_size = intelhaddata->buf_info[buf_id].buf_size; buf_size = intelhaddata->buf_info[buf_id].buf_size;
stream_type = had_stream->stream_type;
/* Every debug statement has an implication /* Every debug statement has an implication
* of ~5msec. Thus, avoid having >3 debug statements * of ~5msec. Thus, avoid having >3 debug statements
...@@ -1536,11 +1549,13 @@ static int had_process_buffer_done(struct snd_intelhad *intelhaddata) ...@@ -1536,11 +1549,13 @@ static int had_process_buffer_done(struct snd_intelhad *intelhaddata)
/* In case of actual data, /* In case of actual data,
* report buffer_done to above ALSA layer * report buffer_done to above ALSA layer
*/ */
buf_size = intelhaddata->buf_info[buf_id].buf_size; substream = had_substream_get(intelhaddata);
if (stream_type >= HAD_RUNNING_STREAM) { if (substream) {
buf_size = intelhaddata->buf_info[buf_id].buf_size;
intelhaddata->stream_info.buffer_rendered += intelhaddata->stream_info.buffer_rendered +=
(intr_count * buf_size); (intr_count * buf_size);
had_period_elapsed(stream->had_substream); snd_pcm_period_elapsed(substream);
had_substream_put(intelhaddata);
} }
return 0; return 0;
...@@ -1552,6 +1567,7 @@ static int had_process_buffer_underrun(struct snd_intelhad *intelhaddata) ...@@ -1552,6 +1567,7 @@ static int had_process_buffer_underrun(struct snd_intelhad *intelhaddata)
enum intel_had_aud_buf_type buf_id; enum intel_had_aud_buf_type buf_id;
struct pcm_stream_info *stream; struct pcm_stream_info *stream;
struct had_stream_data *had_stream; struct had_stream_data *had_stream;
struct snd_pcm_substream *substream;
enum had_status_stream stream_type; enum had_status_stream stream_type;
unsigned long flags; unsigned long flags;
int drv_status; int drv_status;
...@@ -1582,7 +1598,11 @@ static int had_process_buffer_underrun(struct snd_intelhad *intelhaddata) ...@@ -1582,7 +1598,11 @@ static int had_process_buffer_underrun(struct snd_intelhad *intelhaddata)
if (stream_type == HAD_RUNNING_STREAM) { if (stream_type == HAD_RUNNING_STREAM) {
/* Report UNDERRUN error to above layers */ /* Report UNDERRUN error to above layers */
snd_pcm_stop_xrun(stream->had_substream); substream = had_substream_get(intelhaddata);
if (substream) {
snd_pcm_stop_xrun(substream);
had_substream_put(intelhaddata);
}
} }
return 0; return 0;
...@@ -1595,7 +1615,6 @@ static void had_process_hot_plug(struct snd_intelhad *intelhaddata) ...@@ -1595,7 +1615,6 @@ static void had_process_hot_plug(struct snd_intelhad *intelhaddata)
struct snd_pcm_substream *substream; struct snd_pcm_substream *substream;
struct had_stream_data *had_stream; struct had_stream_data *had_stream;
substream = intelhaddata->stream_info.had_substream;
had_stream = &intelhaddata->stream_data; had_stream = &intelhaddata->stream_data;
spin_lock_irq(&intelhaddata->had_spinlock); spin_lock_irq(&intelhaddata->had_spinlock);
...@@ -1617,11 +1636,13 @@ static void had_process_hot_plug(struct snd_intelhad *intelhaddata) ...@@ -1617,11 +1636,13 @@ static void had_process_hot_plug(struct snd_intelhad *intelhaddata)
buf_id); buf_id);
/* Safety check */ /* Safety check */
substream = had_substream_get(intelhaddata);
if (substream) { if (substream) {
dev_dbg(intelhaddata->dev, dev_dbg(intelhaddata->dev,
"Force to stop the active stream by disconnection\n"); "Force to stop the active stream by disconnection\n");
/* Set runtime->state to hw_params done */ /* Set runtime->state to hw_params done */
snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP); snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
had_substream_put(intelhaddata);
} }
had_build_channel_allocation_map(intelhaddata); had_build_channel_allocation_map(intelhaddata);
...@@ -1632,38 +1653,40 @@ static void had_process_hot_unplug(struct snd_intelhad *intelhaddata) ...@@ -1632,38 +1653,40 @@ static void had_process_hot_unplug(struct snd_intelhad *intelhaddata)
{ {
enum intel_had_aud_buf_type buf_id; enum intel_had_aud_buf_type buf_id;
struct had_stream_data *had_stream; struct had_stream_data *had_stream;
struct snd_pcm_substream *substream;
had_stream = &intelhaddata->stream_data; had_stream = &intelhaddata->stream_data;
buf_id = intelhaddata->curr_buf; buf_id = intelhaddata->curr_buf;
substream = had_substream_get(intelhaddata);
spin_lock_irq(&intelhaddata->had_spinlock); spin_lock_irq(&intelhaddata->had_spinlock);
if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) { if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
dev_dbg(intelhaddata->dev, "Device already disconnected\n"); dev_dbg(intelhaddata->dev, "Device already disconnected\n");
spin_unlock_irq(&intelhaddata->had_spinlock); spin_unlock_irq(&intelhaddata->had_spinlock);
return; goto out;
} }
/* Disable Audio */ /* Disable Audio */
snd_intelhad_enable_audio_int(intelhaddata, false); snd_intelhad_enable_audio_int(intelhaddata, false);
snd_intelhad_enable_audio(intelhaddata, false); snd_intelhad_enable_audio(substream, intelhaddata, false);
intelhaddata->drv_status = HAD_DRV_DISCONNECTED; intelhaddata->drv_status = HAD_DRV_DISCONNECTED;
dev_dbg(intelhaddata->dev, dev_dbg(intelhaddata->dev,
"%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n", "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n",
__func__, __LINE__); __func__, __LINE__);
had_stream->stream_type = HAD_INIT;
spin_unlock_irq(&intelhaddata->had_spinlock);
/* Report to above ALSA layer */ /* Report to above ALSA layer */
if (intelhaddata->stream_info.had_substream != NULL) { if (substream)
spin_unlock_irq(&intelhaddata->had_spinlock); snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
snd_pcm_stop(intelhaddata->stream_info.had_substream,
SNDRV_PCM_STATE_SETUP);
spin_lock_irq(&intelhaddata->had_spinlock);
}
had_stream->stream_type = HAD_INIT; out:
spin_unlock_irq(&intelhaddata->had_spinlock); if (substream)
had_substream_put(intelhaddata);
kfree(intelhaddata->chmap->chmap); kfree(intelhaddata->chmap->chmap);
intelhaddata->chmap->chmap = NULL; intelhaddata->chmap->chmap = NULL;
} }
......
...@@ -72,9 +72,10 @@ ...@@ -72,9 +72,10 @@
#define AUD_CONFIG_CH_MASK 0x70 #define AUD_CONFIG_CH_MASK 0x70
struct pcm_stream_info { struct pcm_stream_info {
struct snd_pcm_substream *had_substream; struct snd_pcm_substream *substream;
u64 buffer_rendered; u64 buffer_rendered;
u32 ring_buf_size; u32 ring_buf_size;
int substream_refcount;
}; };
struct ring_buf_info { struct ring_buf_info {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment