1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4 * Takashi Iwai <tiwai@suse.de>
6 * Generic memory allocators
9 #ifndef __SOUND_MEMALLOC_H
10 #define __SOUND_MEMALLOC_H
12 #include <linux/dma-direction.h>
16 struct vm_area_struct;
22 struct snd_dma_device {
23 int type; /* SNDRV_DMA_TYPE_XXX */
24 enum dma_data_direction dir; /* DMA direction */
25 bool need_sync; /* explicit sync needed? */
26 struct device *dev; /* generic device */
32 #define SNDRV_DMA_TYPE_UNKNOWN 0 /* not defined */
33 #define SNDRV_DMA_TYPE_CONTINUOUS 1 /* continuous no-DMA memory */
34 #define SNDRV_DMA_TYPE_DEV 2 /* generic device continuous */
35 #define SNDRV_DMA_TYPE_DEV_WC 5 /* continuous write-combined */
36 #ifdef CONFIG_GENERIC_ALLOCATOR
37 #define SNDRV_DMA_TYPE_DEV_IRAM 4 /* generic device iram-buffer */
39 #define SNDRV_DMA_TYPE_DEV_IRAM SNDRV_DMA_TYPE_DEV
41 #define SNDRV_DMA_TYPE_VMALLOC 7 /* vmalloc'ed buffer */
42 #define SNDRV_DMA_TYPE_NONCONTIG 8 /* non-coherent SG buffer */
43 #define SNDRV_DMA_TYPE_NONCOHERENT 9 /* non-coherent buffer */
44 #ifdef CONFIG_SND_DMA_SGBUF
45 #define SNDRV_DMA_TYPE_DEV_SG SNDRV_DMA_TYPE_NONCONTIG
46 #define SNDRV_DMA_TYPE_DEV_WC_SG 6 /* SG write-combined */
48 #define SNDRV_DMA_TYPE_DEV_SG SNDRV_DMA_TYPE_DEV /* no SG-buf support */
49 #define SNDRV_DMA_TYPE_DEV_WC_SG SNDRV_DMA_TYPE_DEV_WC
51 /* fallback types, don't use those directly */
52 #ifdef CONFIG_SND_DMA_SGBUF
53 #define SNDRV_DMA_TYPE_DEV_SG_FALLBACK 10
54 #define SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK 11
58 * info for buffer allocation
60 struct snd_dma_buffer {
61 struct snd_dma_device dev; /* device type */
62 unsigned char *area; /* virtual pointer */
63 dma_addr_t addr; /* physical address */
64 size_t bytes; /* buffer size in bytes */
65 void *private_data; /* private for allocator; don't touch */
69 * return the pages matching with the given byte size
71 static inline unsigned int snd_sgbuf_aligned_pages(size_t size)
73 return (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
76 /* allocate/release a buffer */
77 int snd_dma_alloc_dir_pages(int type, struct device *dev,
78 enum dma_data_direction dir, size_t size,
79 struct snd_dma_buffer *dmab);
81 static inline int snd_dma_alloc_pages(int type, struct device *dev,
82 size_t size, struct snd_dma_buffer *dmab)
84 return snd_dma_alloc_dir_pages(type, dev, DMA_BIDIRECTIONAL, size, dmab);
87 int snd_dma_alloc_pages_fallback(int type, struct device *dev, size_t size,
88 struct snd_dma_buffer *dmab);
89 void snd_dma_free_pages(struct snd_dma_buffer *dmab);
90 int snd_dma_buffer_mmap(struct snd_dma_buffer *dmab,
91 struct vm_area_struct *area);
93 enum snd_dma_sync_mode { SNDRV_DMA_SYNC_CPU, SNDRV_DMA_SYNC_DEVICE };
95 void snd_dma_buffer_sync(struct snd_dma_buffer *dmab,
96 enum snd_dma_sync_mode mode);
98 static inline void snd_dma_buffer_sync(struct snd_dma_buffer *dmab,
99 enum snd_dma_sync_mode mode) {}
102 dma_addr_t snd_sgbuf_get_addr(struct snd_dma_buffer *dmab, size_t offset);
103 struct page *snd_sgbuf_get_page(struct snd_dma_buffer *dmab, size_t offset);
104 unsigned int snd_sgbuf_get_chunk_size(struct snd_dma_buffer *dmab,
105 unsigned int ofs, unsigned int size);
107 /* device-managed memory allocator */
108 struct snd_dma_buffer *snd_devm_alloc_dir_pages(struct device *dev, int type,
109 enum dma_data_direction dir,
112 static inline struct snd_dma_buffer *
113 snd_devm_alloc_pages(struct device *dev, int type, size_t size)
115 return snd_devm_alloc_dir_pages(dev, type, DMA_BIDIRECTIONAL, size);
118 static inline struct sg_table *
119 snd_dma_noncontig_sg_table(struct snd_dma_buffer *dmab)
121 return dmab->private_data;
124 #endif /* __SOUND_MEMALLOC_H */