Merge tag 'pull-work.iov_iter-rebased' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / include / sound / memalloc.h
CommitLineData
1a59d1b8 1/* SPDX-License-Identifier: GPL-2.0-or-later */
1da177e4 2/*
c1017a4c 3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
1da177e4
LT
4 * Takashi Iwai <tiwai@suse.de>
5 *
6 * Generic memory allocators
1da177e4
LT
7 */
8
9#ifndef __SOUND_MEMALLOC_H
10#define __SOUND_MEMALLOC_H
11
a25684a9 12#include <linux/dma-direction.h>
83fbcaed
TI
13#include <asm/page.h>
14
1da177e4 15struct device;
a202bd1a 16struct vm_area_struct;
a25684a9 17struct sg_table;
1da177e4
LT
18
19/*
20 * buffer device info
21 */
22struct snd_dma_device {
23 int type; /* SNDRV_DMA_TYPE_XXX */
a25684a9
TI
24 enum dma_data_direction dir; /* DMA direction */
25 bool need_sync; /* explicit sync needed? */
1da177e4
LT
26 struct device *dev; /* generic device */
27};
28
019d80db 29#define snd_dma_continuous_data(x) ((struct device *)(__force unsigned long)(x))
1da177e4
LT
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 */
58a95dfa 38#define SNDRV_DMA_TYPE_DEV_WC 5 /* continuous write-combined */
a5606f85 39#ifdef CONFIG_GENERIC_ALLOCATOR
05503214 40#define SNDRV_DMA_TYPE_DEV_IRAM 4 /* generic device iram-buffer */
a5606f85
TI
41#else
42#define SNDRV_DMA_TYPE_DEV_IRAM SNDRV_DMA_TYPE_DEV
43#endif
1fe7f397 44#define SNDRV_DMA_TYPE_VMALLOC 7 /* vmalloc'ed buffer */
a25684a9 45#define SNDRV_DMA_TYPE_NONCONTIG 8 /* non-coherent SG buffer */
73325f60 46#define SNDRV_DMA_TYPE_NONCOHERENT 9 /* non-coherent buffer */
2c95b92e
TI
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
925ca893
TI
54/* fallback types, don't use those directly */
55#ifdef CONFIG_SND_DMA_SGBUF
56#define SNDRV_DMA_TYPE_DEV_SG_FALLBACK 10
57#define SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK 11
58#endif
1da177e4
LT
59
60/*
61 * info for buffer allocation
62 */
63struct snd_dma_buffer {
64 struct snd_dma_device dev; /* device type */
65 unsigned char *area; /* virtual pointer */
66 dma_addr_t addr; /* physical address */
67 size_t bytes; /* buffer size in bytes */
68 void *private_data; /* private for allocator; don't touch */
69};
70
4cae99d9
PLB
71/*
72 * return the pages matching with the given byte size
73 */
74static inline unsigned int snd_sgbuf_aligned_pages(size_t size)
75{
76 return (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
77}
78
1da177e4 79/* allocate/release a buffer */
a25684a9
TI
80int snd_dma_alloc_dir_pages(int type, struct device *dev,
81 enum dma_data_direction dir, size_t size,
82 struct snd_dma_buffer *dmab);
83
84static inline int snd_dma_alloc_pages(int type, struct device *dev,
85 size_t size, struct snd_dma_buffer *dmab)
86{
87 return snd_dma_alloc_dir_pages(type, dev, DMA_BIDIRECTIONAL, size, dmab);
88}
89
1da177e4
LT
90int snd_dma_alloc_pages_fallback(int type, struct device *dev, size_t size,
91 struct snd_dma_buffer *dmab);
92void snd_dma_free_pages(struct snd_dma_buffer *dmab);
a202bd1a
TI
93int snd_dma_buffer_mmap(struct snd_dma_buffer *dmab,
94 struct vm_area_struct *area);
1da177e4 95
a25684a9
TI
96enum snd_dma_sync_mode { SNDRV_DMA_SYNC_CPU, SNDRV_DMA_SYNC_DEVICE };
97#ifdef CONFIG_HAS_DMA
98void snd_dma_buffer_sync(struct snd_dma_buffer *dmab,
99 enum snd_dma_sync_mode mode);
100#else
101static inline void snd_dma_buffer_sync(struct snd_dma_buffer *dmab,
102 enum snd_dma_sync_mode mode) {}
103#endif
104
37af81c5
TI
105dma_addr_t snd_sgbuf_get_addr(struct snd_dma_buffer *dmab, size_t offset);
106struct page *snd_sgbuf_get_page(struct snd_dma_buffer *dmab, size_t offset);
107unsigned int snd_sgbuf_get_chunk_size(struct snd_dma_buffer *dmab,
108 unsigned int ofs, unsigned int size);
109
427ae268 110/* device-managed memory allocator */
a25684a9
TI
111struct snd_dma_buffer *snd_devm_alloc_dir_pages(struct device *dev, int type,
112 enum dma_data_direction dir,
113 size_t size);
114
115static inline struct snd_dma_buffer *
116snd_devm_alloc_pages(struct device *dev, int type, size_t size)
117{
118 return snd_devm_alloc_dir_pages(dev, type, DMA_BIDIRECTIONAL, size);
119}
120
121static inline struct sg_table *
122snd_dma_noncontig_sg_table(struct snd_dma_buffer *dmab)
123{
124 return dmab->private_data;
125}
427ae268 126
1da177e4
LT
127#endif /* __SOUND_MEMALLOC_H */
128