Skip to content

Commit 2d9ea39

Browse files
committed
ALSA: memalloc: Convert x86 SG-buffer handling with non-contiguous type
We'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. Link: https://lore.kernel.org/r/20211017074859.24112-4-tiwai@suse.de Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent 73325f6 commit 2d9ea39

4 files changed

Lines changed: 54 additions & 213 deletions

File tree

‎include/sound/memalloc.h‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,6 @@ struct snd_dma_device {
3636
#define SNDRV_DMA_TYPE_CONTINUOUS 1 /* continuous no-DMA memory */
3737
#define SNDRV_DMA_TYPE_DEV 2 /* generic device continuous */
3838
#define SNDRV_DMA_TYPE_DEV_WC 5 /* continuous write-combined */
39-
#ifdef CONFIG_SND_DMA_SGBUF
40-
#define SNDRV_DMA_TYPE_DEV_SG 3 /* generic device SG-buffer */
41-
#define SNDRV_DMA_TYPE_DEV_WC_SG 6 /* SG write-combined */
42-
#else
43-
#define SNDRV_DMA_TYPE_DEV_SG SNDRV_DMA_TYPE_DEV /* no SG-buf support */
44-
#define SNDRV_DMA_TYPE_DEV_WC_SG SNDRV_DMA_TYPE_DEV_WC
45-
#endif
4639
#ifdef CONFIG_GENERIC_ALLOCATOR
4740
#define SNDRV_DMA_TYPE_DEV_IRAM 4 /* generic device iram-buffer */
4841
#else
@@ -51,6 +44,13 @@ struct snd_dma_device {
5144
#define SNDRV_DMA_TYPE_VMALLOC 7 /* vmalloc'ed buffer */
5245
#define SNDRV_DMA_TYPE_NONCONTIG 8 /* non-coherent SG buffer */
5346
#define SNDRV_DMA_TYPE_NONCOHERENT 9 /* non-coherent buffer */
47+
#ifdef CONFIG_SND_DMA_SGBUF
48+
#define SNDRV_DMA_TYPE_DEV_SG SNDRV_DMA_TYPE_NONCONTIG
49+
#define SNDRV_DMA_TYPE_DEV_WC_SG 6 /* SG write-combined */
50+
#else
51+
#define SNDRV_DMA_TYPE_DEV_SG SNDRV_DMA_TYPE_DEV /* no SG-buf support */
52+
#define SNDRV_DMA_TYPE_DEV_WC_SG SNDRV_DMA_TYPE_DEV_WC
53+
#endif
5454

5555
/*
5656
* info for buffer allocation

‎sound/core/Makefile‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ snd-$(CONFIG_SND_JACK) += ctljack.o jack.o
1919
snd-pcm-y := pcm.o pcm_native.o pcm_lib.o pcm_misc.o \
2020
pcm_memory.o memalloc.o
2121
snd-pcm-$(CONFIG_SND_PCM_TIMER) += pcm_timer.o
22-
snd-pcm-$(CONFIG_SND_DMA_SGBUF) += sgbuf.o
2322
snd-pcm-$(CONFIG_SND_PCM_ELD) += pcm_drm_eld.o
2423
snd-pcm-$(CONFIG_SND_PCM_IEC958) += pcm_iec958.o
2524

‎sound/core/memalloc.c‎

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,50 @@ static const struct snd_malloc_ops snd_dma_noncontig_ops = {
560560
.get_chunk_size = snd_dma_vmalloc_get_chunk_size,
561561
};
562562

563+
/* x86-specific SG-buffer with WC pages */
564+
#ifdef CONFIG_SND_DMA_SGBUF
565+
#define vmalloc_to_virt(v) (unsigned long)page_to_virt(vmalloc_to_page(v))
566+
567+
static void *snd_dma_sg_wc_alloc(struct snd_dma_buffer *dmab, size_t size)
568+
{
569+
void *p = snd_dma_noncontig_alloc(dmab, size);
570+
size_t ofs;
571+
572+
if (!p)
573+
return NULL;
574+
for (ofs = 0; ofs < size; ofs += PAGE_SIZE)
575+
set_memory_uc(vmalloc_to_virt(p + ofs), 1);
576+
return p;
577+
}
578+
579+
static void snd_dma_sg_wc_free(struct snd_dma_buffer *dmab)
580+
{
581+
size_t ofs;
582+
583+
for (ofs = 0; ofs < dmab->bytes; ofs += PAGE_SIZE)
584+
set_memory_wb(vmalloc_to_virt(dmab->area + ofs), 1);
585+
snd_dma_noncontig_free(dmab);
586+
}
587+
588+
static int snd_dma_sg_wc_mmap(struct snd_dma_buffer *dmab,
589+
struct vm_area_struct *area)
590+
{
591+
area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
592+
/* FIXME: dma_mmap_noncontiguous() works? */
593+
return -ENOENT; /* continue with the default mmap handler */
594+
}
595+
596+
const struct snd_malloc_ops snd_dma_sg_wc_ops = {
597+
.alloc = snd_dma_sg_wc_alloc,
598+
.free = snd_dma_sg_wc_free,
599+
.mmap = snd_dma_sg_wc_mmap,
600+
.sync = snd_dma_noncontig_sync,
601+
.get_addr = snd_dma_vmalloc_get_addr,
602+
.get_page = snd_dma_vmalloc_get_page,
603+
.get_chunk_size = snd_dma_vmalloc_get_chunk_size,
604+
};
605+
#endif /* CONFIG_SND_DMA_SGBUF */
606+
563607
/*
564608
* Non-coherent pages allocator
565609
*/
@@ -619,14 +663,13 @@ static const struct snd_malloc_ops *dma_ops[] = {
619663
[SNDRV_DMA_TYPE_DEV_WC] = &snd_dma_wc_ops,
620664
[SNDRV_DMA_TYPE_NONCONTIG] = &snd_dma_noncontig_ops,
621665
[SNDRV_DMA_TYPE_NONCOHERENT] = &snd_dma_noncoherent_ops,
666+
#ifdef CONFIG_SND_DMA_SGBUF
667+
[SNDRV_DMA_TYPE_DEV_WC_SG] = &snd_dma_sg_wc_ops,
668+
#endif
622669
#ifdef CONFIG_GENERIC_ALLOCATOR
623670
[SNDRV_DMA_TYPE_DEV_IRAM] = &snd_dma_iram_ops,
624671
#endif /* CONFIG_GENERIC_ALLOCATOR */
625672
#endif /* CONFIG_HAS_DMA */
626-
#ifdef CONFIG_SND_DMA_SGBUF
627-
[SNDRV_DMA_TYPE_DEV_SG] = &snd_dma_sg_ops,
628-
[SNDRV_DMA_TYPE_DEV_WC_SG] = &snd_dma_sg_ops,
629-
#endif
630673
};
631674

632675
static const struct snd_malloc_ops *snd_dma_get_ops(struct snd_dma_buffer *dmab)

‎sound/core/sgbuf.c‎

Lines changed: 0 additions & 201 deletions
This file was deleted.

0 commit comments

Comments
 (0)