[1/8] ALSA: pcm: Introduce managed buffer allocation mode
diff mbox series

Message ID 20191117085308.23915-2-tiwai@suse.de
State New
Headers show
Series
  • ALSA: pcm: API cleanups and extensions
Related show

Commit Message

Takashi Iwai Nov. 17, 2019, 8:53 a.m. UTC
This patch adds the support for the feature to automatically allocate
and free PCM buffers, so called "managed buffer allocation" mode.
It's set up via new PCM helpers, snd_pcm_set_managed_buffer() and
snd_pcm_set_managed_buffer_all(), both of which correspond to the
existing preallocator helpers, snd_pcm_lib_preallocate_pages() and
snd_pcm_lib_preallocate_pages_for_all().  When the new helper is used,
it not only performs the pre-allocation of buffers, but also it
manages to call snd_pcm_lib_malloc_pages() before the PCM hw_params
ops and snd_lib_pcm_free() after the PCM hw_free ops inside PCM core,
respectively.  This allows drivers to drop the explicit calls of the
memory allocation / release functions, and it will be a good amount of
code reduction in the end of this patch series.

When the PCM substream is set to the managed buffer allocation mode,
the managed_buffer_alloc flag is set in the substream object.  Since
some drivers want to know when a buffer is newly allocated or
re-allocated at hw_params callback (e.g. want to set up the additional
stuff for the given buffer only at allocation time), now PCM core
turns on buffer_changed flag when the buffer has changed.

The standard conversions to use the new API will be straightforward:
- Replace snd_pcm_lib_preallocate*() calls with the corresponding
  snd_pcm_set_managed_buffer*(); the arguments should be unchanged
- Drop superfluous snd_pcm_lib_malloc() and snd_pcm_lib_free() calls;
  the check of snd_pcm_lib_malloc() returns should be replaced with
  the check of runtime->buffer_changed flag.
- If hw_params or hw_free becomes empty, drop them from PCM ops

Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
 include/sound/pcm.h     |  8 +++++
 sound/core/pcm_memory.c | 83 +++++++++++++++++++++++++++++++++++++++++--------
 sound/core/pcm_native.c | 12 +++++++
 3 files changed, 90 insertions(+), 13 deletions(-)

Comments

Pierre-Louis Bossart Nov. 18, 2019, 4:24 p.m. UTC | #1
On 11/17/19 2:53 AM, Takashi Iwai wrote:
> This patch adds the support for the feature to automatically allocate
> and free PCM buffers, so called "managed buffer allocation" mode.
> It's set up via new PCM helpers, snd_pcm_set_managed_buffer() and
> snd_pcm_set_managed_buffer_all(), both of which correspond to the
> existing preallocator helpers, snd_pcm_lib_preallocate_pages() and
> snd_pcm_lib_preallocate_pages_for_all().  When the new helper is used,
> it not only performs the pre-allocation of buffers, but also it
> manages to call snd_pcm_lib_malloc_pages() before the PCM hw_params
> ops and snd_lib_pcm_free() after the PCM hw_free ops inside PCM core,
> respectively.  This allows drivers to drop the explicit calls of the
> memory allocation / release functions, and it will be a good amount of
> code reduction in the end of this patch series.
> 
> When the PCM substream is set to the managed buffer allocation mode,
> the managed_buffer_alloc flag is set in the substream object.  Since
> some drivers want to know when a buffer is newly allocated or
> re-allocated at hw_params callback (e.g. want to set up the additional
> stuff for the given buffer only at allocation time), now PCM core
> turns on buffer_changed flag when the buffer has changed.

I am a bit lost on the directions:
a) is this introducing a new API that will eventually replace the 
existing one?
b) or are we going to have two options, managed and non-managed buffers?
in this case, what would drive an implementer to keep using non-managed 
buffers? It'd be useful to provide examples.

In the cover letter, the wording 'almost all drivers' is used, which 
leads be to think option b) it is, but the exceptions are not clear to me.

> 
> The standard conversions to use the new API will be straightforward:
> - Replace snd_pcm_lib_preallocate*() calls with the corresponding
>    snd_pcm_set_managed_buffer*(); the arguments should be unchanged
> - Drop superfluous snd_pcm_lib_malloc() and snd_pcm_lib_free() calls;
>    the check of snd_pcm_lib_malloc() returns should be replaced with
>    the check of runtime->buffer_changed flag.
> - If hw_params or hw_free becomes empty, drop them from PCM ops
> 
> Signed-off-by: Takashi Iwai <tiwai@suse.de>
> ---
>   include/sound/pcm.h     |  8 +++++
>   sound/core/pcm_memory.c | 83 +++++++++++++++++++++++++++++++++++++++++--------
>   sound/core/pcm_native.c | 12 +++++++
>   3 files changed, 90 insertions(+), 13 deletions(-)
> 
> diff --git a/include/sound/pcm.h b/include/sound/pcm.h
> index 2c0aa884f5f1..253d15c61ce2 100644
> --- a/include/sound/pcm.h
> +++ b/include/sound/pcm.h
> @@ -414,6 +414,7 @@ struct snd_pcm_runtime {
>   	size_t dma_bytes;		/* size of DMA area */
>   
>   	struct snd_dma_buffer *dma_buffer_p;	/* allocated buffer */
> +	unsigned int buffer_changed:1;	/* buffer allocation changed; set only in managed mode */
>   
>   	/* -- audio timestamp config -- */
>   	struct snd_pcm_audio_tstamp_config audio_tstamp_config;
> @@ -475,6 +476,7 @@ struct snd_pcm_substream {
>   #endif /* CONFIG_SND_VERBOSE_PROCFS */
>   	/* misc flags */
>   	unsigned int hw_opened: 1;
> +	unsigned int managed_buffer_alloc:1;
>   };
>   
>   #define SUBSTREAM_BUSY(substream) ((substream)->ref_count > 0)
> @@ -1186,6 +1188,12 @@ void snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,
>   int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size);
>   int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream);
>   
> +void snd_pcm_set_managed_buffer(struct snd_pcm_substream *substream, int type,
> +				struct device *data, size_t size, size_t max);
> +void snd_pcm_set_managed_buffer_all(struct snd_pcm *pcm, int type,
> +				    struct device *data,
> +				    size_t size, size_t max);
> +
>   int _snd_pcm_lib_alloc_vmalloc_buffer(struct snd_pcm_substream *substream,
>   				      size_t size, gfp_t gfp_flags);
>   int snd_pcm_lib_free_vmalloc_buffer(struct snd_pcm_substream *substream);
> diff --git a/sound/core/pcm_memory.c b/sound/core/pcm_memory.c
> index 286f333f8e4c..73b770db2372 100644
> --- a/sound/core/pcm_memory.c
> +++ b/sound/core/pcm_memory.c
> @@ -193,9 +193,15 @@ static inline void preallocate_info_init(struct snd_pcm_substream *substream)
>   /*
>    * pre-allocate the buffer and create a proc file for the substream
>    */
> -static void snd_pcm_lib_preallocate_pages1(struct snd_pcm_substream *substream,
> -					  size_t size, size_t max)
> +static void preallocate_pages(struct snd_pcm_substream *substream,
> +			      int type, struct device *data,
> +			      size_t size, size_t max, bool managed)
>   {
> +	if (snd_BUG_ON(substream->dma_buffer.dev.type))
> +		return;
> +
> +	substream->dma_buffer.dev.type = type;
> +	substream->dma_buffer.dev.dev = data;
>   
>   	if (size > 0 && preallocate_dma && substream->number < maximum_substreams)
>   		preallocate_pcm_pages(substream, size);
> @@ -205,8 +211,23 @@ static void snd_pcm_lib_preallocate_pages1(struct snd_pcm_substream *substream,
>   	substream->dma_max = max;
>   	if (max > 0)
>   		preallocate_info_init(substream);
> +	if (managed)
> +		substream->managed_buffer_alloc = 1;
>   }
>   
> +static void preallocate_pages_for_all(struct snd_pcm *pcm, int type,
> +				      void *data, size_t size, size_t max,
> +				      bool managed)
> +{
> +	struct snd_pcm_substream *substream;
> +	int stream;
> +
> +	for (stream = 0; stream < 2; stream++)
> +		for (substream = pcm->streams[stream].substream; substream;
> +		     substream = substream->next)
> +			preallocate_pages(substream, type, data, size, max,
> +					  managed);
> +}
>   
>   /**
>    * snd_pcm_lib_preallocate_pages - pre-allocation for the given DMA type
> @@ -222,11 +243,7 @@ void snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream,
>   				  int type, struct device *data,
>   				  size_t size, size_t max)
>   {
> -	if (snd_BUG_ON(substream->dma_buffer.dev.type))
> -		return;
> -	substream->dma_buffer.dev.type = type;
> -	substream->dma_buffer.dev.dev = data;
> -	snd_pcm_lib_preallocate_pages1(substream, size, max);
> +	preallocate_pages(substream, type, data, size, max, false);
>   }
>   EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages);
>   
> @@ -245,15 +262,55 @@ void snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,
>   					  int type, void *data,
>   					  size_t size, size_t max)
>   {
> -	struct snd_pcm_substream *substream;
> -	int stream;
> -
> -	for (stream = 0; stream < 2; stream++)
> -		for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
> -			snd_pcm_lib_preallocate_pages(substream, type, data, size, max);
> +	preallocate_pages_for_all(pcm, type, data, size, max, false);
>   }
>   EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all);
>   
> +/**
> + * snd_pcm_set_managed_buffer - set up buffer management for a substream
> + * @substream: the pcm substream instance
> + * @type: DMA type (SNDRV_DMA_TYPE_*)
> + * @data: DMA type dependent data
> + * @size: the requested pre-allocation size in bytes
> + * @max: the max. allowed pre-allocation size
> + *
> + * Do pre-allocation for the given DMA buffer type, and set the managed
> + * buffer allocation mode to the given substream.
> + * In this mode, PCM core will allocate a buffer automatically before PCM
> + * hw_params ops call, and release the buffer after PCM hw_free ops call
> + * as well, so that the driver doesn't need to invoke the allocation and
> + * the release explicitly in its callback.
> + * When a buffer is actually allocated before the PCM hw_params call, it
> + * turns on the runtime buffer_changed flag for drivers changing their h/w
> + * parameters accordingly.
> + */
> +void snd_pcm_set_managed_buffer(struct snd_pcm_substream *substream, int type,
> +				struct device *data, size_t size, size_t max)
> +{
> +	preallocate_pages(substream, type, data, size, max, true);
> +}
> +EXPORT_SYMBOL(snd_pcm_set_managed_buffer);
> +
> +/**
> + * snd_pcm_set_managed_buffer_all - set up buffer management for all substreams
> + *	for all substreams
> + * @pcm: the pcm instance
> + * @type: DMA type (SNDRV_DMA_TYPE_*)
> + * @data: DMA type dependent data
> + * @size: the requested pre-allocation size in bytes
> + * @max: the max. allowed pre-allocation size
> + *
> + * Do pre-allocation to all substreams of the given pcm for the specified DMA
> + * type and size, and set the managed_buffer_alloc flag to each substream.
> + */
> +void snd_pcm_set_managed_buffer_all(struct snd_pcm *pcm, int type,
> +				    struct device *data,
> +				    size_t size, size_t max)
> +{
> +	preallocate_pages_for_all(pcm, type, data, size, max, true);
> +}
> +EXPORT_SYMBOL(snd_pcm_set_managed_buffer_all);
> +
>   #ifdef CONFIG_SND_DMA_SGBUF
>   /*
>    * snd_pcm_sgbuf_ops_page - get the page struct at the given offset
> diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
> index 0c27009dc3df..f1646735bde6 100644
> --- a/sound/core/pcm_native.c
> +++ b/sound/core/pcm_native.c
> @@ -662,6 +662,14 @@ static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
>   	if (err < 0)
>   		goto _error;
>   
> +	if (substream->managed_buffer_alloc) {
> +		err = snd_pcm_lib_malloc_pages(substream,
> +					       params_buffer_bytes(params));
> +		if (err < 0)
> +			goto _error;
> +		runtime->buffer_changed = err > 0;
> +	}
> +
>   	if (substream->ops->hw_params != NULL) {
>   		err = substream->ops->hw_params(substream, params);
>   		if (err < 0)
> @@ -723,6 +731,8 @@ static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
>   	snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
>   	if (substream->ops->hw_free != NULL)
>   		substream->ops->hw_free(substream);
> +	if (substream->managed_buffer_alloc)
> +		snd_pcm_lib_free_pages(substream);
>   	return err;
>   }
>   
> @@ -769,6 +779,8 @@ static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
>   		return -EBADFD;
>   	if (substream->ops->hw_free)
>   		result = substream->ops->hw_free(substream);
> +	if (substream->managed_buffer_alloc)
> +		snd_pcm_lib_free_pages(substream);
>   	snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
>   	pm_qos_remove_request(&substream->latency_pm_qos_req);
>   	return result;
>
Takashi Iwai Nov. 18, 2019, 6:46 p.m. UTC | #2
On Mon, 18 Nov 2019 17:24:45 +0100,
Pierre-Louis Bossart wrote:
> 
> 
> 
> On 11/17/19 2:53 AM, Takashi Iwai wrote:
> > This patch adds the support for the feature to automatically allocate
> > and free PCM buffers, so called "managed buffer allocation" mode.
> > It's set up via new PCM helpers, snd_pcm_set_managed_buffer() and
> > snd_pcm_set_managed_buffer_all(), both of which correspond to the
> > existing preallocator helpers, snd_pcm_lib_preallocate_pages() and
> > snd_pcm_lib_preallocate_pages_for_all().  When the new helper is used,
> > it not only performs the pre-allocation of buffers, but also it
> > manages to call snd_pcm_lib_malloc_pages() before the PCM hw_params
> > ops and snd_lib_pcm_free() after the PCM hw_free ops inside PCM core,
> > respectively.  This allows drivers to drop the explicit calls of the
> > memory allocation / release functions, and it will be a good amount of
> > code reduction in the end of this patch series.
> >
> > When the PCM substream is set to the managed buffer allocation mode,
> > the managed_buffer_alloc flag is set in the substream object.  Since
> > some drivers want to know when a buffer is newly allocated or
> > re-allocated at hw_params callback (e.g. want to set up the additional
> > stuff for the given buffer only at allocation time), now PCM core
> > turns on buffer_changed flag when the buffer has changed.
> 
> I am a bit lost on the directions:
> a) is this introducing a new API that will eventually replace the
> existing one?
> b) or are we going to have two options, managed and non-managed buffers?
> in this case, what would drive an implementer to keep using
> non-managed buffers? It'd be useful to provide examples.
>
> In the cover letter, the wording 'almost all drivers' is used, which
> leads be to think option b) it is, but the exceptions are not clear to
> me.

Yes, it's currently it's (b), the new API is completely optional and
the driver may keep the old API.  Actually in my upcoming patchset
(see below), only three drivers are left with the old API due to the
special buffer handling and all the rest are replaced with the new
API.

You can find the conversion results in topic/pcm-managed branch.
As seen in the patch diff below, it's a long series.


thanks,

Takashi

 .../sound/kernel-api/writing-an-alsa-driver.rst    | 148 +++++++++++++++------
 .../gpu/drm/bridge/synopsys/dw-hdmi-ahb-audio.c    |  48 ++++---
 drivers/media/pci/cobalt/cobalt-alsa-pcm.c         |  61 +--------
 drivers/media/pci/cx18/cx18-alsa-pcm.c             |  62 +--------
 drivers/media/pci/ivtv/ivtv-alsa-pcm.c             |  63 +--------
 drivers/media/pci/solo6x10/solo6x10-g723.c         |  23 +---
 drivers/media/pci/tw686x/tw686x-audio.c            |  15 +--
 drivers/media/usb/cx231xx/cx231xx-audio.c          |  78 +----------
 drivers/media/usb/em28xx/em28xx-audio.c            |  86 +-----------
 drivers/media/usb/go7007/snd-go7007.c              |  24 +---
 drivers/media/usb/tm6000/tm6000-alsa.c             |  81 +----------
 drivers/media/usb/usbtv/usbtv-audio.c              |  28 +---
 drivers/staging/most/sound/sound.c                 |  44 +-----
 .../vc04_services/bcm2835-audio/bcm2835-pcm.c      |  17 +--
 drivers/usb/gadget/function/u_audio.c              |  18 +--
 include/sound/core.h                               |   1 +
 include/sound/pcm.h                                |  52 ++------
 sound/aoa/soundbus/i2sbus/pcm.c                    |  11 +-
 sound/arm/aaci.c                                   |  40 +++---
 sound/atmel/ac97c.c                                |  20 +--
 sound/core/init.c                                  |   1 +
 sound/core/pcm_local.h                             |   2 +
 sound/core/pcm_memory.c                            | 143 ++++++++++----------
 sound/core/pcm_native.c                            |  48 ++++++-
 sound/drivers/aloop.c                              |  12 +-
 sound/drivers/dummy.c                              |  14 +-
 sound/drivers/ml403-ac97cr.c                       |  29 +---
 sound/drivers/pcsp/pcsp_lib.c                      |  17 +--
 sound/drivers/vx/vx_pcm.c                          |  27 +---
 sound/firewire/bebob/bebob_pcm.c                   |  11 +-
 sound/firewire/dice/dice-pcm.c                     |  13 +-
 sound/firewire/digi00x/digi00x-pcm.c               |  11 +-
 sound/firewire/fireface/ff-pcm.c                   |   9 +-
 sound/firewire/fireworks/fireworks_pcm.c           |  11 +-
 sound/firewire/isight.c                            |  10 +-
 sound/firewire/motu/motu-pcm.c                     |  11 +-
 sound/firewire/oxfw/oxfw-pcm.c                     |  17 +--
 sound/firewire/tascam/tascam-pcm.c                 |  11 +-
 sound/isa/ad1816a/ad1816a_lib.c                    |  20 +--
 sound/isa/cmi8330.c                                |   5 +-
 sound/isa/es1688/es1688_lib.c                      |  20 +--
 sound/isa/es18xx.c                                 |  24 +---
 sound/isa/gus/gus_pcm.c                            |  28 ++--
 sound/isa/sb/sb16_main.c                           |  21 +--
 sound/isa/sb/sb8_main.c                            |  21 +--
 sound/isa/wss/wss_lib.c                            |  23 +---
 sound/mips/hal2.c                                  |  25 +---
 sound/mips/sgio2audio.c                            |  20 +--
 sound/parisc/harmony.c                             |  18 +--
 sound/pci/ad1889.c                                 |  24 +---
 sound/pci/ali5451/ali5451.c                        |  30 +----
 sound/pci/als300.c                                 |  23 +---
 sound/pci/als4000.c                                |  23 +---
 sound/pci/asihpi/asihpi.c                          |  10 +-
 sound/pci/atiixp.c                                 |  14 +-
 sound/pci/atiixp_modem.c                           |   9 +-
 sound/pci/au88x0/au88x0_pcm.c                      |  15 +--
 sound/pci/aw2/aw2-alsa.c                           |  45 ++-----
 sound/pci/azt3328.c                                |  30 +----
 sound/pci/bt87x.c                                  |  14 +-
 sound/pci/ca0106/ca0106_main.c                     |  56 +-------
 sound/pci/cmipci.c                                 |  36 ++---
 sound/pci/cs4281.c                                 |  20 +--
 sound/pci/cs5535audio/cs5535audio_pcm.c            |  12 +-
 sound/pci/ctxfi/ctpcm.c                            |  15 +--
 sound/pci/echoaudio/echoaudio.c                    |  19 +--
 sound/pci/emu10k1/emu10k1x.c                       |  15 +--
 sound/pci/emu10k1/emupcm.c                         |  41 +-----
 sound/pci/emu10k1/p16v.c                           |  48 +------
 sound/pci/ens1370.c                                |  27 +---
 sound/pci/es1938.c                                 |  28 +---
 sound/pci/fm801.c                                  |  20 +--
 sound/pci/hda/hda_controller.c                     |  13 +-
 sound/pci/ice1712/ice1712.c                        |  42 ++----
 sound/pci/ice1712/ice1724.c                        |  25 ++--
 sound/pci/intel8x0.c                               |  11 +-
 sound/pci/intel8x0m.c                              |  23 +---
 sound/pci/lola/lola_pcm.c                          |  11 +-
 sound/pci/lx6464es/lx6464es.c                      |  14 +-
 sound/pci/maestro3.c                               |   9 +-
 sound/pci/mixart/mixart.c                          |  14 +-
 sound/pci/oxygen/oxygen_pcm.c                      |  52 ++++----
 sound/pci/pcxhr/pcxhr.c                            |  31 +----
 sound/pci/riptide/riptide.c                        |  10 +-
 sound/pci/rme32.c                                  |  35 +----
 sound/pci/sis7019.c                                |  25 +---
 sound/pci/sonicvibes.c                             |  20 +--
 sound/pci/trident/trident_main.c                   |  49 +++----
 sound/pci/via82xx.c                                |  45 +++----
 sound/pci/via82xx_modem.c                          |   9 +-
 sound/pci/ymfpci/ymfpci_main.c                     |  33 ++---
 sound/pcmcia/pdaudiocf/pdaudiocf_pcm.c             |  25 +---
 sound/ppc/pmac.c                                   |  28 +---
 sound/ppc/snd_ps3.c                                |  28 +---
 sound/sh/aica.c                                    |  29 +---
 sound/sh/sh_dac_audio.c                            |  20 +--
 sound/soc/amd/acp-pcm-dma.c                        |  58 +++-----
 sound/soc/amd/raven/acp3x-pcm-dma.c                |  30 +----
 sound/soc/au1x/dbdma2.c                            |  14 +-
 sound/soc/au1x/dma.c                               |  21 +--
 sound/soc/codecs/cros_ec_codec.c                   |   8 +-
 sound/soc/codecs/rt5514-spi.c                      |  10 +-
 sound/soc/codecs/rt5677-spi.c                      |  10 +-
 sound/soc/dwc/dwc-pcm.c                            |  24 +---
 sound/soc/intel/atom/sst-mfld-platform-pcm.c       |  25 +---
 sound/soc/intel/baytrail/sst-baytrail-pcm.c        |  19 +--
 sound/soc/intel/haswell/sst-haswell-pcm.c          |  17 +--
 sound/soc/intel/skylake/skl-pcm.c                  |  26 +---
 sound/soc/mediatek/common/mtk-afe-fe-dai.c         |  14 +-
 sound/soc/mediatek/common/mtk-afe-fe-dai.h         |   2 -
 .../soc/mediatek/common/mtk-afe-platform-driver.c  |  12 +-
 .../soc/mediatek/common/mtk-afe-platform-driver.h  |   2 -
 sound/soc/mediatek/mt2701/mt2701-afe-pcm.c         |   2 -
 sound/soc/mediatek/mt6797/mt6797-afe-pcm.c         |   1 -
 sound/soc/mediatek/mt8183/mt8183-afe-pcm.c         |   1 -
 sound/soc/meson/axg-fifo.c                         |  13 +-
 sound/soc/sh/dma-sh7760.c                          |  14 +-
 sound/soc/sh/fsi.c                                 |  18 +--
 sound/soc/sh/rcar/core.c                           |  23 +---
 sound/soc/sh/siu_pcm.c                             |  39 +-----
 sound/soc/soc-generic-dmaengine-pcm.c              |  12 +-
 sound/soc/sof/pcm.c                                |  34 ++---
 sound/soc/stm/stm32_adfsdm.c                       |  29 +---
 sound/soc/txx9/txx9aclc.c                          |  14 +-
 sound/soc/uniphier/aio-dma.c                       |  30 +----
 sound/soc/xilinx/xlnx_formatter_pcm.c              |  13 +-
 sound/soc/xtensa/xtfpga-i2s.c                      |   9 +-
 sound/sparc/amd7930.c                              |  20 +--
 sound/sparc/cs4231.c                               |  17 +--
 sound/sparc/dbri.c                                 |  13 +-
 sound/spi/at73c213.c                               |  11 +-
 sound/usb/6fire/pcm.c                              |  17 +--
 sound/usb/caiaq/audio.c                            |  13 +-
 sound/usb/hiface/pcm.c                             |  18 +--
 sound/usb/line6/pcm.c                              |  13 +-
 sound/usb/misc/ua101.c                             |  23 +---
 sound/usb/pcm.c                                    |  15 +--
 sound/usb/usx2y/usbusx2yaudio.c                    |  26 ++--
 sound/usb/usx2y/usx2yhwdeppcm.c                    |  18 +--
 sound/x86/intel_hdmi_audio.c                       |  16 +--
 140 files changed, 781 insertions(+), 2700 deletions(-)

Patch
diff mbox series

diff --git a/include/sound/pcm.h b/include/sound/pcm.h
index 2c0aa884f5f1..253d15c61ce2 100644
--- a/include/sound/pcm.h
+++ b/include/sound/pcm.h
@@ -414,6 +414,7 @@  struct snd_pcm_runtime {
 	size_t dma_bytes;		/* size of DMA area */
 
 	struct snd_dma_buffer *dma_buffer_p;	/* allocated buffer */
+	unsigned int buffer_changed:1;	/* buffer allocation changed; set only in managed mode */
 
 	/* -- audio timestamp config -- */
 	struct snd_pcm_audio_tstamp_config audio_tstamp_config;
@@ -475,6 +476,7 @@  struct snd_pcm_substream {
 #endif /* CONFIG_SND_VERBOSE_PROCFS */
 	/* misc flags */
 	unsigned int hw_opened: 1;
+	unsigned int managed_buffer_alloc:1;
 };
 
 #define SUBSTREAM_BUSY(substream) ((substream)->ref_count > 0)
@@ -1186,6 +1188,12 @@  void snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,
 int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size);
 int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream);
 
+void snd_pcm_set_managed_buffer(struct snd_pcm_substream *substream, int type,
+				struct device *data, size_t size, size_t max);
+void snd_pcm_set_managed_buffer_all(struct snd_pcm *pcm, int type,
+				    struct device *data,
+				    size_t size, size_t max);
+
 int _snd_pcm_lib_alloc_vmalloc_buffer(struct snd_pcm_substream *substream,
 				      size_t size, gfp_t gfp_flags);
 int snd_pcm_lib_free_vmalloc_buffer(struct snd_pcm_substream *substream);
diff --git a/sound/core/pcm_memory.c b/sound/core/pcm_memory.c
index 286f333f8e4c..73b770db2372 100644
--- a/sound/core/pcm_memory.c
+++ b/sound/core/pcm_memory.c
@@ -193,9 +193,15 @@  static inline void preallocate_info_init(struct snd_pcm_substream *substream)
 /*
  * pre-allocate the buffer and create a proc file for the substream
  */
-static void snd_pcm_lib_preallocate_pages1(struct snd_pcm_substream *substream,
-					  size_t size, size_t max)
+static void preallocate_pages(struct snd_pcm_substream *substream,
+			      int type, struct device *data,
+			      size_t size, size_t max, bool managed)
 {
+	if (snd_BUG_ON(substream->dma_buffer.dev.type))
+		return;
+
+	substream->dma_buffer.dev.type = type;
+	substream->dma_buffer.dev.dev = data;
 
 	if (size > 0 && preallocate_dma && substream->number < maximum_substreams)
 		preallocate_pcm_pages(substream, size);
@@ -205,8 +211,23 @@  static void snd_pcm_lib_preallocate_pages1(struct snd_pcm_substream *substream,
 	substream->dma_max = max;
 	if (max > 0)
 		preallocate_info_init(substream);
+	if (managed)
+		substream->managed_buffer_alloc = 1;
 }
 
+static void preallocate_pages_for_all(struct snd_pcm *pcm, int type,
+				      void *data, size_t size, size_t max,
+				      bool managed)
+{
+	struct snd_pcm_substream *substream;
+	int stream;
+
+	for (stream = 0; stream < 2; stream++)
+		for (substream = pcm->streams[stream].substream; substream;
+		     substream = substream->next)
+			preallocate_pages(substream, type, data, size, max,
+					  managed);
+}
 
 /**
  * snd_pcm_lib_preallocate_pages - pre-allocation for the given DMA type
@@ -222,11 +243,7 @@  void snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream,
 				  int type, struct device *data,
 				  size_t size, size_t max)
 {
-	if (snd_BUG_ON(substream->dma_buffer.dev.type))
-		return;
-	substream->dma_buffer.dev.type = type;
-	substream->dma_buffer.dev.dev = data;
-	snd_pcm_lib_preallocate_pages1(substream, size, max);
+	preallocate_pages(substream, type, data, size, max, false);
 }
 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages);
 
@@ -245,15 +262,55 @@  void snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,
 					  int type, void *data,
 					  size_t size, size_t max)
 {
-	struct snd_pcm_substream *substream;
-	int stream;
-
-	for (stream = 0; stream < 2; stream++)
-		for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
-			snd_pcm_lib_preallocate_pages(substream, type, data, size, max);
+	preallocate_pages_for_all(pcm, type, data, size, max, false);
 }
 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all);
 
+/**
+ * snd_pcm_set_managed_buffer - set up buffer management for a substream
+ * @substream: the pcm substream instance
+ * @type: DMA type (SNDRV_DMA_TYPE_*)
+ * @data: DMA type dependent data
+ * @size: the requested pre-allocation size in bytes
+ * @max: the max. allowed pre-allocation size
+ *
+ * Do pre-allocation for the given DMA buffer type, and set the managed
+ * buffer allocation mode to the given substream.
+ * In this mode, PCM core will allocate a buffer automatically before PCM
+ * hw_params ops call, and release the buffer after PCM hw_free ops call
+ * as well, so that the driver doesn't need to invoke the allocation and
+ * the release explicitly in its callback.
+ * When a buffer is actually allocated before the PCM hw_params call, it
+ * turns on the runtime buffer_changed flag for drivers changing their h/w
+ * parameters accordingly.
+ */
+void snd_pcm_set_managed_buffer(struct snd_pcm_substream *substream, int type,
+				struct device *data, size_t size, size_t max)
+{
+	preallocate_pages(substream, type, data, size, max, true);
+}
+EXPORT_SYMBOL(snd_pcm_set_managed_buffer);
+
+/**
+ * snd_pcm_set_managed_buffer_all - set up buffer management for all substreams
+ *	for all substreams
+ * @pcm: the pcm instance
+ * @type: DMA type (SNDRV_DMA_TYPE_*)
+ * @data: DMA type dependent data
+ * @size: the requested pre-allocation size in bytes
+ * @max: the max. allowed pre-allocation size
+ *
+ * Do pre-allocation to all substreams of the given pcm for the specified DMA
+ * type and size, and set the managed_buffer_alloc flag to each substream.
+ */
+void snd_pcm_set_managed_buffer_all(struct snd_pcm *pcm, int type,
+				    struct device *data,
+				    size_t size, size_t max)
+{
+	preallocate_pages_for_all(pcm, type, data, size, max, true);
+}
+EXPORT_SYMBOL(snd_pcm_set_managed_buffer_all);
+
 #ifdef CONFIG_SND_DMA_SGBUF
 /*
  * snd_pcm_sgbuf_ops_page - get the page struct at the given offset
diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
index 0c27009dc3df..f1646735bde6 100644
--- a/sound/core/pcm_native.c
+++ b/sound/core/pcm_native.c
@@ -662,6 +662,14 @@  static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
 	if (err < 0)
 		goto _error;
 
+	if (substream->managed_buffer_alloc) {
+		err = snd_pcm_lib_malloc_pages(substream,
+					       params_buffer_bytes(params));
+		if (err < 0)
+			goto _error;
+		runtime->buffer_changed = err > 0;
+	}
+
 	if (substream->ops->hw_params != NULL) {
 		err = substream->ops->hw_params(substream, params);
 		if (err < 0)
@@ -723,6 +731,8 @@  static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
 	snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
 	if (substream->ops->hw_free != NULL)
 		substream->ops->hw_free(substream);
+	if (substream->managed_buffer_alloc)
+		snd_pcm_lib_free_pages(substream);
 	return err;
 }
 
@@ -769,6 +779,8 @@  static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
 		return -EBADFD;
 	if (substream->ops->hw_free)
 		result = substream->ops->hw_free(substream);
+	if (substream->managed_buffer_alloc)
+		snd_pcm_lib_free_pages(substream);
 	snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
 	pm_qos_remove_request(&substream->latency_pm_qos_req);
 	return result;