ALSA: Support for non-contiguous and non-coherent page allocations
cover ALSA: Support for non-contiguous and non-coherent page allocations
Hi,
here is a patch set to add the support for non-contiguous and non-coherent page allocations to the ALSA core memalloc helpers. Currently no driver is using, but this also gave a nice cleanup for the existing SG-buffer helpers on x86.
Takashi
===
Takashi Iwai (3):
ALSA: memalloc: Support for non-contiguous page allocation
ALSA: memalloc: Support for non-coherent page allocation
ALSA: memalloc: Convert x86 SG-buffer handling with non-contiguous
type
include/sound/memalloc.h | 61 +++++++++-- sound/core/Makefile | 1 - sound/core/memalloc.c | 207 +++++++++++++++++++++++++++++++++--- sound/core/memalloc_local.h | 1 + sound/core/pcm_lib.c | 3 + sound/core/pcm_local.h | 7 ++ sound/core/pcm_memory.c | 13 ++- sound/core/pcm_native.c | 16 +++ sound/core/sgbuf.c | 201 ---------------------------------- 9 files changed, 282 insertions(+), 228 deletions(-) delete mode 100644 sound/core/sgbuf.c
Signature · 1 line
2.26.2
PATCH 1/3 ALSA: memalloc: Support for non-contiguous page allocation
This patch adds the support for allocation of non-contiguous DMA pages in the common memalloc helper. It's another SG-buffer type, but unlike the existing one, this is directional and requires the explicit sync / invalidation of dirty pages on non-coherent architectures.
For this enhancement, the following points are changed:
- snd_dma_device stores the DMA direction.
- snd_dma_device stores need_sync flag indicating whether the explicit sync is required or not.
- A new variant of helper functions, snd_dma_alloc_dir_pages() and *_all() are introduced; the old snd_dma_alloc_pages() and *_all() kept as just wrappers with DMA_BIDIRECTIONAL.
- A new helper snd_dma_buffer_sync() is introduced; this gets called in the appropriate places.
- A new allocation type, SNDRV_DMA_TYPE_NONCONTIG, is introduced.
When the driver allocates pages with this new type, and it may require the SNDRV_PCM_INFO_EXPLICIT_SYNC flag set to the PCM hardware.info for taking the full control of PCM applptr and hwptr changes (that implies disabling the mmap of control/status data). When the buffer allocation is managed by snd_pcm_set_managed_buffer(), this flag is automatically set depending on the result of dma_need_sync() internally. Otherwise, if the buffer is managed manually, the driver has to set the flag explicitly, too.
The explicit sync between CPU and device for non-coherent memory is performed at the points before and after read/write transfer as well as the applptr/hwptr syncptr ioctl. In the case of mmap mode, user-space is supposed to call the syncptr ioctl with the hwptr flag to update and fetch the status at first; this corresponds to CPU-sync. Then user-space advances the applptr via syncptr ioctl again with applptr flag, and this corresponds to the device sync with flushing.
Other than the DMA direction and the explicit sync, the usage of this new buffer type is almost equivalent with the existing SNDRV_DMA_TYPE_DEV_SG; you can get the page and the address via snd_sgbuf_get_page() and snd_sgbuf_get_addr(), also calculate the continuous pages via snd_sgbuf_get_chunk_size().
For those SG-page handling, the non-contig type shares the same ops with the vmalloc handler. As we do always vmap the SG pages at first, the actual address can be deduced from the vmapped address easily without iterating the SG-list.
--- include/sound/memalloc.h | 46 +++++++++++++-- sound/core/memalloc.c | 109 +++++++++++++++++++++++++++++++++--- sound/core/memalloc_local.h | 1 + sound/core/pcm_lib.c | 3 + sound/core/pcm_local.h | 7 +++ sound/core/pcm_memory.c | 13 ++++- sound/core/pcm_native.c | 16 ++++++ 7 files changed, 180 insertions(+), 15 deletions(-)
include/sound/memalloc.h +42 -4
@@ -9,16 +9,20 @@#ifndef __SOUND_MEMALLOC_H#define __SOUND_MEMALLOC_H +#include <linux/dma-direction.h> #include <asm/page.h> struct device ;struct vm_area_struct ; +struct sg_table ;/* * buffer device info */ struct snd_dma_device {int type;/* SNDRV_DMA_TYPE_XXX */ +enum dma_data_direction dir;/* DMA direction */ +bool need_sync;/* explicit sync needed? */ struct device *dev;/* generic device */ };
@@ -45,6 +49,7 @@ struct snd_dma_device {#define SNDRV_DMA_TYPE_DEV_IRAM SNDRV_DMA_TYPE_DEV#endif #define SNDRV_DMA_TYPE_VMALLOC 7/* vmalloc'ed buffer */ +#define SNDRV_DMA_TYPE_NONCONTIG 8/* non-coherent SG buffer */ /* * info for buffer allocation
@@ -66,22 +71,55 @@ static inline unsigned int snd_sgbuf_aligned_pages(size_t size) }/* allocate/release a buffer */ -int snd_dma_alloc_pages (int type,struct device *dev,size_t size, -struct snd_dma_buffer *dmab); +int snd_dma_alloc_dir_pages (int type,struct device *dev, +enum dma_data_direction dir,size_t size, +struct snd_dma_buffer *dmab); + +static inline int snd_dma_alloc_pages (int type,struct device *dev, +size_t size,struct snd_dma_buffer *dmab) +{ +return snd_dma_alloc_dir_pages (type, dev,DMA_BIDIRECTIONAL , size, dmab); +} +int snd_dma_alloc_pages_fallback (int type,struct device *dev,size_t size,struct snd_dma_buffer *dmab);void snd_dma_free_pages (struct snd_dma_buffer *dmab);int snd_dma_buffer_mmap (struct snd_dma_buffer *dmab,struct vm_area_struct *area); +enum snd_dma_sync_mode {SNDRV_DMA_SYNC_CPU ,SNDRV_DMA_SYNC_DEVICE }; +#ifdef CONFIG_HAS_DMA +void snd_dma_buffer_sync (struct snd_dma_buffer *dmab, +enum snd_dma_sync_mode mode); +#else +static inline void snd_dma_buffer_sync (struct snd_dma_buffer *dmab, +enum snd_dma_sync_mode mode) {} +#endif + +void snd_dma_buffer_sync (struct snd_dma_buffer *dmab, +enum snd_dma_sync_mode mode); +dma_addr_t snd_sgbuf_get_addr (struct snd_dma_buffer *dmab,size_t offset);struct page *snd_sgbuf_get_page (struct snd_dma_buffer *dmab,size_t offset);unsigned int snd_sgbuf_get_chunk_size (struct snd_dma_buffer *dmab,unsigned int ofs,unsigned int size);/* device-managed memory allocator */ -struct snd_dma_buffer *snd_devm_alloc_pages (struct device *dev,int type, -size_t size); +struct snd_dma_buffer *snd_devm_alloc_dir_pages (struct device *dev,int type, +enum dma_data_direction dir, +size_t size); + +static inline struct snd_dma_buffer * +snd_devm_alloc_pages (struct device *dev,int type,size_t size) +{ +return snd_devm_alloc_dir_pages (dev, type,DMA_BIDIRECTIONAL , size); +} + +static inline struct sg_table * +snd_dma_noncontig_sg_table (struct snd_dma_buffer *dmab) +{ +return dmab->private_data; +}#endif /* __SOUND_MEMALLOC_H */
sound/core/memalloc.c +101 -8
185 lines not on this page · raw message
sound/core/memalloc_local.h +1 -0
@@ -10,6 +10,7 @@ struct snd_malloc_ops {unsigned int (*get_chunk_size)(struct snd_dma_buffer *dmab,unsigned int ofs,unsigned int size);int (*mmap)(struct snd_dma_buffer *dmab,struct vm_area_struct *area); +void (*sync)(struct snd_dma_buffer *dmab,enum snd_dma_sync_mode mode); };#ifdef CONFIG_SND_DMA_SGBUF
sound/core/pcm_lib.c +3 -0
@@ -106,6 +106,7 @@ void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_ufram frames -= transfer; ofs =0 ; } +snd_pcm_dma_buffer_sync (substream,SNDRV_DMA_SYNC_DEVICE ); }#ifdef CONFIG_SND_DEBUG
@@ -2256,8 +2257,10 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream, goto _end_unlock; }snd_pcm_stream_unlock_irq (substream); +snd_pcm_dma_buffer_sync (substream,SNDRV_DMA_SYNC_CPU ); err =writer (substream, appl_ofs, data, offset, frames, transfer); +snd_pcm_dma_buffer_sync (substream,SNDRV_DMA_SYNC_DEVICE );snd_pcm_stream_lock_irq (substream);if (err <0 ) goto _end_unlock;
sound/core/pcm_local.h +7 -0
@@ -73,4 +73,11 @@ void snd_pcm_sync_stop(struct snd_pcm_substream *substream, bool sync_irq);for ((subs) = (pcm)->streams[str].substream; (subs); \ (subs) = (subs)->next) +static inline void snd_pcm_dma_buffer_sync(struct snd_pcm_substream *substream, +enum snd_dma_sync_mode mode) +{ +if (substream->runtime->info &SNDRV_PCM_INFO_EXPLICIT_SYNC ) +snd_dma_buffer_sync (snd_pcm_get_dma_buf (substream), mode); +} +#endif /* __SOUND_CORE_PCM_LOCAL_H */
sound/core/pcm_memory.c +10 -3
@@ -32,15 +32,20 @@ module_param(max_alloc_per_card, ulong, 0644);MODULE_PARM_DESC (max_alloc_per_card,"Max total allocation bytes per card." );static int do_alloc_pages (struct snd_card *card,int type,struct device *dev, -size_t size,struct snd_dma_buffer *dmab) +int str,size_t size,struct snd_dma_buffer *dmab) { +enum dma_data_direction dir;int err;if (max_alloc_per_card && card->total_pcm_alloc_bytes + size > max_alloc_per_card)return -ENOMEM ; - err =snd_dma_alloc_pages (type, dev, size, dmab); +if (str ==SNDRV_PCM_STREAM_PLAYBACK ) + dir =DMA_TO_DEVICE ; +else + dir =DMA_FROM_DEVICE ; + err =snd_dma_alloc_dir_pages (type, dev, dir, size, dmab);if (!err) {mutex_lock (&card->memory_mutex); card->total_pcm_alloc_bytes += dmab->bytes;
@@ -77,7 +82,7 @@ static int preallocate_pcm_pages(struct snd_pcm_substream *substream, do { err = do_alloc_pages(card, dmab->dev.type, dmab->dev.dev, - size, dmab); + substream->stream, size, dmab); if (err != -ENOMEM) return err; if (no_fallback)
@@ -177,6 +182,7 @@ static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,if (do_alloc_pages (card, substream->dma_buffer.dev.type, substream->dma_buffer.dev.dev, + substream->stream, size, &new_dmab) <0 ) { buffer->error = -ENOMEM ; pr_debug("ALSA pcmC%dD%d%c,%d:%s: cannot preallocate for size %zu\n",
@@ -418,6 +424,7 @@ int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size)if (do_alloc_pages (card, substream->dma_buffer.dev.type, substream->dma_buffer.dev.dev, + substream->stream, size, dmab) <0 ) {kfree (dmab); pr_debug("ALSA pcmC%dD%d%c,%d:%s: cannot preallocate for size %zu\n",
sound/core/pcm_native.c +16 -0
@@ -2685,6 +2685,13 @@ int snd_pcm_open_substream(struct snd_pcm *pcm, int stream, goto error; } +/* automatically set EXPLICIT_SYNC flag in the managed mode whenever +* the DMA buffer requires it +*/ +if (substream->managed_buffer_alloc && + substream->dma_buffer.dev.need_sync) + substream->runtime->hw.info |=SNDRV_PCM_INFO_EXPLICIT_SYNC ; + *rsubstream = substream;return 0 ;
@@ -2912,6 +2919,8 @@ static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream, ret =rewind_appl_ptr (substream, frames,snd_pcm_hw_avail (substream));snd_pcm_stream_unlock_irq (substream); +if (ret >=0 ) +snd_pcm_dma_buffer_sync (substream,SNDRV_DMA_SYNC_DEVICE );return ret; }
@@ -2929,6 +2938,8 @@ static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream, ret =forward_appl_ptr (substream, frames,snd_pcm_avail (substream));snd_pcm_stream_unlock_irq (substream); +if (ret >=0 ) +snd_pcm_dma_buffer_sync (substream,SNDRV_DMA_SYNC_DEVICE );return ret; }
@@ -2939,6 +2950,7 @@ static int snd_pcm_hwsync(struct snd_pcm_substream *substream)snd_pcm_stream_lock_irq (substream); err =do_pcm_hwsync (substream);snd_pcm_stream_unlock_irq (substream); +snd_pcm_dma_buffer_sync (substream,SNDRV_DMA_SYNC_CPU );return err; }
@@ -3000,6 +3012,8 @@ static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream, sync_ptr.s.status.suspended_state = status->suspended_state; sync_ptr.s.status.audio_tstamp = status->audio_tstamp;snd_pcm_stream_unlock_irq (substream); +if (!(sync_ptr.flags &SNDRV_PCM_SYNC_PTR_APPL )) +snd_pcm_dma_buffer_sync (substream,SNDRV_DMA_SYNC_DEVICE );if (copy_to_user (_sync_ptr, &sync_ptr,sizeof (sync_ptr)))return -EFAULT ;return 0 ;
@@ -3096,6 +3110,8 @@ static int snd_pcm_ioctl_sync_ptr_compat(struct snd_pcm_substream *substream, sstatus.suspended_state = status->suspended_state; sstatus.audio_tstamp = status->audio_tstamp;snd_pcm_stream_unlock_irq (substream); +if (!(sflags &SNDRV_PCM_SYNC_PTR_APPL )) +snd_pcm_dma_buffer_sync (substream,SNDRV_DMA_SYNC_DEVICE ); if (put_user(sstatus.state, &src->s.status.state) || put_user(sstatus.hw_ptr, &src->s.status.hw_ptr) || put_user(sstatus.tstamp.tv_sec, &src->s.status.tstamp_sec) ||
Signature · 1 line
2.26.2
PATCH 2/3 ALSA: memalloc: Support for non-coherent page allocation
In reply to Takashi IwaiFollowing after the addition of non-contiguous pages, this patch adds the new contiguous non-coherent page allocation to the standard memalloc helper. Like the previous non-contig type, this non-coherent type is also directional and requires the explicit sync, too. Hence the driver using this type of buffer may need to set SNDRV_PCM_INFO_EXPLICIT_SYNC flag to the PCM hardware.info as well, unless it's set up in the managed mode.
--- include/sound/memalloc.h | 1 + sound/core/memalloc.c | 47 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+)
include/sound/memalloc.h +1 -0
@@ -50,6 +50,7 @@ struct snd_dma_device {#endif #define SNDRV_DMA_TYPE_VMALLOC 7/* vmalloc'ed buffer */ #define SNDRV_DMA_TYPE_NONCONTIG 8/* non-coherent SG buffer */ +#define SNDRV_DMA_TYPE_NONCOHERENT 9/* non-coherent buffer */ /* * info for buffer allocation
sound/core/memalloc.c +47 -0
59 lines not on this page · raw message
Signature · 1 line
2.26.2
PATCH 3/3 ALSA: memalloc: Convert x86 SG-buffer handling with non-contiguous type
In reply to Takashi IwaiWe've had an x86-specific SG-buffer handling code, but now it can be merged gracefully with the standard non-contiguous DMA pages.
After the migration, SNDRV_DMA_TYPE_DMA_SG becomes identical with SNDRV_DMA_TYPE_NONCONTIG on x86, while others still fall back to SNDRV_DMA_TYPE_DEV.
The remaining problem is about the SG-buffer with WC pages: the DMA core stuff on x86 doesn't treat it well, so we still need some special handling to manipulate the page attribute manually. The mmap handler for SNDRV_DMA_TYPE_DEV_SG_WC still returns -ENOENT intentionally for the fallback to the default handler.
--- include/sound/memalloc.h | 14 +-- sound/core/Makefile | 1 - sound/core/memalloc.c | 51 +++++++++- sound/core/sgbuf.c | 201 --------------------------------------- 4 files changed, 54 insertions(+), 213 deletions(-) delete mode 100644 sound/core/sgbuf.c
include/sound/memalloc.h +7 -7
@@ -36,13 +36,6 @@ struct snd_dma_device {#define SNDRV_DMA_TYPE_CONTINUOUS 1/* continuous no-DMA memory */ #define SNDRV_DMA_TYPE_DEV 2/* generic device continuous */ #define SNDRV_DMA_TYPE_DEV_WC 5/* continuous write-combined */ -#ifdef CONFIG_SND_DMA_SGBUF -#define SNDRV_DMA_TYPE_DEV_SG 3/* generic device SG-buffer */ -#define SNDRV_DMA_TYPE_DEV_WC_SG 6/* SG write-combined */ -#else -#define SNDRV_DMA_TYPE_DEV_SG SNDRV_DMA_TYPE_DEV/* no SG-buf support */ -#define SNDRV_DMA_TYPE_DEV_WC_SG SNDRV_DMA_TYPE_DEV_WC -#endif #ifdef CONFIG_GENERIC_ALLOCATOR #define SNDRV_DMA_TYPE_DEV_IRAM 4/* generic device iram-buffer */ #else
@@ -51,6 +44,13 @@ struct snd_dma_device {#define SNDRV_DMA_TYPE_VMALLOC 7/* vmalloc'ed buffer */ #define SNDRV_DMA_TYPE_NONCONTIG 8/* non-coherent SG buffer */ #define SNDRV_DMA_TYPE_NONCOHERENT 9/* non-coherent buffer */ +#ifdef CONFIG_SND_DMA_SGBUF +#define SNDRV_DMA_TYPE_DEV_SG SNDRV_DMA_TYPE_NONCONTIG +#define SNDRV_DMA_TYPE_DEV_WC_SG 6/* SG write-combined */ +#else +#define SNDRV_DMA_TYPE_DEV_SG SNDRV_DMA_TYPE_DEV/* no SG-buf support */ +#define SNDRV_DMA_TYPE_DEV_WC_SG SNDRV_DMA_TYPE_DEV_WC +#endif /* * info for buffer allocation
sound/core/Makefile +0 -1
@@ -17,7 +17,6 @@ snd-$(CONFIG_SND_JACK) += ctljack.o jack.o snd-pcm-y := pcm.o pcm_native.o pcm_lib.o pcm_misc.o \ pcm_memory.o memalloc.o snd-pcm-$(CONFIG_SND_PCM_TIMER) += pcm_timer.o -snd-pcm-$(CONFIG_SND_DMA_SGBUF) += sgbuf.o snd-pcm-$(CONFIG_SND_PCM_ELD) += pcm_drm_eld.o snd-pcm-$(CONFIG_SND_PCM_IEC958) += pcm_iec958.o
sound/core/memalloc.c +47 -4
67 lines not on this page · raw message
sound/core/sgbuf.c deleted +0 -201
201 lines not on this page · raw message
Signature · 1 line
2.26.2
Quoted 7 lines · Takashi Iwai
Hi,
here is a patch set to add the support for non-contiguous and non-coherent page allocations to the ALSA core memalloc helpers. Currently no driver is using, but this also gave a nice cleanup for the existing SG-buffer helpers on x86.
It turned out that I created the patch set from a slightly old branch. Will resubmit the latest code.
Sorry for the mess.
Takashi