1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz> 4 * Takashi Iwai <tiwai@suse.de> 5 * 6 * Generic memory allocators 7 */ 8 9 #ifndef __SOUND_MEMALLOC_H 10 #define __SOUND_MEMALLOC_H 11 12 #include <linux/dma-direction.h> 13 #include <asm/page.h> 14 15 struct device; 16 struct vm_area_struct; 17 struct sg_table; 18 19 /* 20 * buffer device info 21 */ 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 */ 27 }; 28 29 #define snd_dma_continuous_data(x) ((struct device *)(__force unsigned long)(x)) 30 31 32 /* 33 * buffer types 34 */ 35 #define SNDRV_DMA_TYPE_UNKNOWN 0 /* not defined */ 36 #define SNDRV_DMA_TYPE_CONTINUOUS 1 /* continuous no-DMA memory */ 37 #define SNDRV_DMA_TYPE_DEV 2 /* generic device continuous */ 38 #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 46 #ifdef CONFIG_GENERIC_ALLOCATOR 47 #define SNDRV_DMA_TYPE_DEV_IRAM 4 /* generic device iram-buffer */ 48 #else 49 #define SNDRV_DMA_TYPE_DEV_IRAM SNDRV_DMA_TYPE_DEV 50 #endif 51 #define SNDRV_DMA_TYPE_VMALLOC 7 /* vmalloc'ed buffer */ 52 #define SNDRV_DMA_TYPE_NONCONTIG 8 /* non-coherent SG buffer */ 53 54 /* 55 * info for buffer allocation 56 */ 57 struct snd_dma_buffer { 58 struct snd_dma_device dev; /* device type */ 59 unsigned char *area; /* virtual pointer */ 60 dma_addr_t addr; /* physical address */ 61 size_t bytes; /* buffer size in bytes */ 62 void *private_data; /* private for allocator; don't touch */ 63 }; 64 65 /* 66 * return the pages matching with the given byte size 67 */ 68 static inline unsigned int snd_sgbuf_aligned_pages(size_t size) 69 { 70 return (size + PAGE_SIZE - 1) >> PAGE_SHIFT; 71 } 72 73 /* allocate/release a buffer */ 74 int snd_dma_alloc_dir_pages(int type, struct device *dev, 75 enum dma_data_direction dir, size_t size, 76 struct snd_dma_buffer *dmab); 77 78 static inline int snd_dma_alloc_pages(int type, struct device *dev, 79 size_t size, struct snd_dma_buffer *dmab) 80 { 81 return snd_dma_alloc_dir_pages(type, dev, DMA_BIDIRECTIONAL, size, dmab); 82 } 83 84 int snd_dma_alloc_pages_fallback(int type, struct device *dev, size_t size, 85 struct snd_dma_buffer *dmab); 86 void snd_dma_free_pages(struct snd_dma_buffer *dmab); 87 int snd_dma_buffer_mmap(struct snd_dma_buffer *dmab, 88 struct vm_area_struct *area); 89 90 enum snd_dma_sync_mode { SNDRV_DMA_SYNC_CPU, SNDRV_DMA_SYNC_DEVICE }; 91 #ifdef CONFIG_HAS_DMA 92 void snd_dma_buffer_sync(struct snd_dma_buffer *dmab, 93 enum snd_dma_sync_mode mode); 94 #else 95 static inline void snd_dma_buffer_sync(struct snd_dma_buffer *dmab, 96 enum snd_dma_sync_mode mode) {} 97 #endif 98 99 void snd_dma_buffer_sync(struct snd_dma_buffer *dmab, 100 enum snd_dma_sync_mode mode); 101 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); 106 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, 110 size_t size); 111 112 static inline struct snd_dma_buffer * 113 snd_devm_alloc_pages(struct device *dev, int type, size_t size) 114 { 115 return snd_devm_alloc_dir_pages(dev, type, DMA_BIDIRECTIONAL, size); 116 } 117 118 static inline struct sg_table * 119 snd_dma_noncontig_sg_table(struct snd_dma_buffer *dmab) 120 { 121 return dmab->private_data; 122 } 123 124 #endif /* __SOUND_MEMALLOC_H */ 125 126