Skip to content

Commit 73325f6

Browse files
committed
ALSA: memalloc: Support for non-coherent page allocation
Following to 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. Link: https://lore.kernel.org/r/20211017074859.24112-3-tiwai@suse.de Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent a25684a commit 73325f6

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

‎include/sound/memalloc.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct snd_dma_device {
5050
#endif
5151
#define SNDRV_DMA_TYPE_VMALLOC 7 /* vmalloc'ed buffer */
5252
#define SNDRV_DMA_TYPE_NONCONTIG 8 /* non-coherent SG buffer */
53+
#define SNDRV_DMA_TYPE_NONCOHERENT 9 /* non-coherent buffer */
5354

5455
/*
5556
* info for buffer allocation

‎sound/core/memalloc.c‎

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

563+
/*
564+
* Non-coherent pages allocator
565+
*/
566+
static void *snd_dma_noncoherent_alloc(struct snd_dma_buffer *dmab, size_t size)
567+
{
568+
dmab->dev.need_sync = dma_need_sync(dmab->dev.dev, dmab->dev.dir);
569+
return dma_alloc_noncoherent(dmab->dev.dev, size, &dmab->addr,
570+
dmab->dev.dir, DEFAULT_GFP);
571+
}
572+
573+
static void snd_dma_noncoherent_free(struct snd_dma_buffer *dmab)
574+
{
575+
dma_free_noncoherent(dmab->dev.dev, dmab->bytes, dmab->area,
576+
dmab->addr, dmab->dev.dir);
577+
}
578+
579+
static int snd_dma_noncoherent_mmap(struct snd_dma_buffer *dmab,
580+
struct vm_area_struct *area)
581+
{
582+
area->vm_page_prot = vm_get_page_prot(area->vm_flags);
583+
return dma_mmap_pages(dmab->dev.dev, area,
584+
area->vm_end - area->vm_start,
585+
virt_to_page(dmab->area));
586+
}
587+
588+
static void snd_dma_noncoherent_sync(struct snd_dma_buffer *dmab,
589+
enum snd_dma_sync_mode mode)
590+
{
591+
if (mode == SNDRV_DMA_SYNC_CPU) {
592+
if (dmab->dev.dir != DMA_TO_DEVICE)
593+
dma_sync_single_for_cpu(dmab->dev.dev, dmab->addr,
594+
dmab->bytes, dmab->dev.dir);
595+
} else {
596+
if (dmab->dev.dir != DMA_FROM_DEVICE)
597+
dma_sync_single_for_device(dmab->dev.dev, dmab->addr,
598+
dmab->bytes, dmab->dev.dir);
599+
}
600+
}
601+
602+
static const struct snd_malloc_ops snd_dma_noncoherent_ops = {
603+
.alloc = snd_dma_noncoherent_alloc,
604+
.free = snd_dma_noncoherent_free,
605+
.mmap = snd_dma_noncoherent_mmap,
606+
.sync = snd_dma_noncoherent_sync,
607+
};
608+
563609
#endif /* CONFIG_HAS_DMA */
564610

565611
/*
@@ -572,6 +618,7 @@ static const struct snd_malloc_ops *dma_ops[] = {
572618
[SNDRV_DMA_TYPE_DEV] = &snd_dma_dev_ops,
573619
[SNDRV_DMA_TYPE_DEV_WC] = &snd_dma_wc_ops,
574620
[SNDRV_DMA_TYPE_NONCONTIG] = &snd_dma_noncontig_ops,
621+
[SNDRV_DMA_TYPE_NONCOHERENT] = &snd_dma_noncoherent_ops,
575622
#ifdef CONFIG_GENERIC_ALLOCATOR
576623
[SNDRV_DMA_TYPE_DEV_IRAM] = &snd_dma_iram_ops,
577624
#endif /* CONFIG_GENERIC_ALLOCATOR */

0 commit comments

Comments
 (0)