Skip to content

Commit a25684a

Browse files
committed
ALSA: memalloc: Support for non-contiguous page allocation
This patch adds the support for allocation of non-contiguous DMA pages in the common memalloc helper. It's another SG-buffer type, but unlike the existing one, this is directional and requires the explicit sync / invalidation of dirty pages on non-coherent architectures. For this enhancement, the following points are changed: - snd_dma_device stores the DMA direction. - snd_dma_device stores need_sync flag indicating whether the explicit sync is required or not. - A new variant of helper functions, snd_dma_alloc_dir_pages() and *_all() are introduced; the old snd_dma_alloc_pages() and *_all() kept as just wrappers with DMA_BIDIRECTIONAL. - A new helper snd_dma_buffer_sync() is introduced; this gets called in the appropriate places. - A new allocation type, SNDRV_DMA_TYPE_NONCONTIG, is introduced. When the driver allocates pages with this new type, and it may require the SNDRV_PCM_INFO_EXPLICIT_SYNC flag set to the PCM hardware.info for taking the full control of PCM applptr and hwptr changes (that implies disabling the mmap of control/status data). When the buffer allocation is managed by snd_pcm_set_managed_buffer(), this flag is automatically set depending on the result of dma_need_sync() internally. Otherwise, if the buffer is managed manually, the driver has to set the flag explicitly, too. The explicit sync between CPU and device for non-coherent memory is performed at the points before and after read/write transfer as well as the applptr/hwptr syncptr ioctl. In the case of mmap mode, user-space is supposed to call the syncptr ioctl with the hwptr flag to update and fetch the status at first; this corresponds to CPU-sync. Then user-space advances the applptr via syncptr ioctl again with applptr flag, and this corresponds to the device sync with flushing. Other than the DMA direction and the explicit sync, the usage of this new buffer type is almost equivalent with the existing SNDRV_DMA_TYPE_DEV_SG; you can get the page and the address via snd_sgbuf_get_page() and snd_sgbuf_get_addr(), also calculate the continuous pages via snd_sgbuf_get_chunk_size(). For those SG-page handling, the non-contig type shares the same ops with the vmalloc handler. As we do always vmap the SG pages at first, the actual address can be deduced from the vmapped address easily without iterating the SG-list. Link: https://lore.kernel.org/r/20211017074859.24112-2-tiwai@suse.de Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent 3c05f14 commit a25684a

8 files changed

Lines changed: 187 additions & 15 deletions

File tree

‎include/sound/memalloc.h‎

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,20 @@
99
#ifndef __SOUND_MEMALLOC_H
1010
#define __SOUND_MEMALLOC_H
1111

12+
#include <linux/dma-direction.h>
1213
#include <asm/page.h>
1314

1415
struct device;
1516
struct vm_area_struct;
17+
struct sg_table;
1618

1719
/*
1820
* buffer device info
1921
*/
2022
struct snd_dma_device {
2123
int type; /* SNDRV_DMA_TYPE_XXX */
24+
enum dma_data_direction dir; /* DMA direction */
25+
bool need_sync; /* explicit sync needed? */
2226
struct device *dev; /* generic device */
2327
};
2428

@@ -45,6 +49,7 @@ struct snd_dma_device {
4549
#define SNDRV_DMA_TYPE_DEV_IRAM SNDRV_DMA_TYPE_DEV
4650
#endif
4751
#define SNDRV_DMA_TYPE_VMALLOC 7 /* vmalloc'ed buffer */
52+
#define SNDRV_DMA_TYPE_NONCONTIG 8 /* non-coherent SG buffer */
4853

4954
/*
5055
* info for buffer allocation
@@ -66,22 +71,55 @@ static inline unsigned int snd_sgbuf_aligned_pages(size_t size)
6671
}
6772

6873
/* allocate/release a buffer */
69-
int snd_dma_alloc_pages(int type, struct device *dev, size_t size,
70-
struct snd_dma_buffer *dmab);
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+
7184
int snd_dma_alloc_pages_fallback(int type, struct device *dev, size_t size,
7285
struct snd_dma_buffer *dmab);
7386
void snd_dma_free_pages(struct snd_dma_buffer *dmab);
7487
int snd_dma_buffer_mmap(struct snd_dma_buffer *dmab,
7588
struct vm_area_struct *area);
7689

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+
77102
dma_addr_t snd_sgbuf_get_addr(struct snd_dma_buffer *dmab, size_t offset);
78103
struct page *snd_sgbuf_get_page(struct snd_dma_buffer *dmab, size_t offset);
79104
unsigned int snd_sgbuf_get_chunk_size(struct snd_dma_buffer *dmab,
80105
unsigned int ofs, unsigned int size);
81106

82107
/* device-managed memory allocator */
83-
struct snd_dma_buffer *snd_devm_alloc_pages(struct device *dev, int type,
84-
size_t size);
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+
}
85123

86124
#endif /* __SOUND_MEMALLOC_H */
87125

‎sound/core/memalloc.c‎

Lines changed: 101 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/mm.h>
1111
#include <linux/dma-mapping.h>
1212
#include <linux/genalloc.h>
13+
#include <linux/highmem.h>
1314
#include <linux/vmalloc.h>
1415
#ifdef CONFIG_X86
1516
#include <asm/set_memory.h>
@@ -39,9 +40,11 @@ static void *__snd_dma_alloc_pages(struct snd_dma_buffer *dmab, size_t size)
3940
}
4041

4142
/**
42-
* snd_dma_alloc_pages - allocate the buffer area according to the given type
43+
* snd_dma_alloc_dir_pages - allocate the buffer area according to the given
44+
* type and direction
4345
* @type: the DMA buffer type
4446
* @device: the device pointer
47+
* @dir: DMA direction
4548
* @size: the buffer size to allocate
4649
* @dmab: buffer allocation record to store the allocated data
4750
*
@@ -51,8 +54,9 @@ static void *__snd_dma_alloc_pages(struct snd_dma_buffer *dmab, size_t size)
5154
* Return: Zero if the buffer with the given size is allocated successfully,
5255
* otherwise a negative value on error.
5356
*/
54-
int snd_dma_alloc_pages(int type, struct device *device, size_t size,
55-
struct snd_dma_buffer *dmab)
57+
int snd_dma_alloc_dir_pages(int type, struct device *device,
58+
enum dma_data_direction dir, size_t size,
59+
struct snd_dma_buffer *dmab)
5660
{
5761
if (WARN_ON(!size))
5862
return -ENXIO;
@@ -62,6 +66,7 @@ int snd_dma_alloc_pages(int type, struct device *device, size_t size,
6266
size = PAGE_ALIGN(size);
6367
dmab->dev.type = type;
6468
dmab->dev.dev = device;
69+
dmab->dev.dir = dir;
6570
dmab->bytes = 0;
6671
dmab->addr = 0;
6772
dmab->private_data = NULL;
@@ -71,7 +76,7 @@ int snd_dma_alloc_pages(int type, struct device *device, size_t size,
7176
dmab->bytes = size;
7277
return 0;
7378
}
74-
EXPORT_SYMBOL(snd_dma_alloc_pages);
79+
EXPORT_SYMBOL(snd_dma_alloc_dir_pages);
7580

7681
/**
7782
* snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
@@ -129,9 +134,10 @@ static void __snd_release_pages(struct device *dev, void *res)
129134
}
130135

131136
/**
132-
* snd_devm_alloc_pages - allocate the buffer and manage with devres
137+
* snd_devm_alloc_dir_pages - allocate the buffer and manage with devres
133138
* @dev: the device pointer
134139
* @type: the DMA buffer type
140+
* @dir: DMA direction
135141
* @size: the buffer size to allocate
136142
*
137143
* Allocate buffer pages depending on the given type and manage using devres.
@@ -144,7 +150,8 @@ static void __snd_release_pages(struct device *dev, void *res)
144150
* The function returns the snd_dma_buffer object at success, or NULL if failed.
145151
*/
146152
struct snd_dma_buffer *
147-
snd_devm_alloc_pages(struct device *dev, int type, size_t size)
153+
snd_devm_alloc_dir_pages(struct device *dev, int type,
154+
enum dma_data_direction dir, size_t size)
148155
{
149156
struct snd_dma_buffer *dmab;
150157
int err;
@@ -157,7 +164,7 @@ snd_devm_alloc_pages(struct device *dev, int type, size_t size)
157164
if (!dmab)
158165
return NULL;
159166

160-
err = snd_dma_alloc_pages(type, dev, size, dmab);
167+
err = snd_dma_alloc_dir_pages(type, dev, dir, size, dmab);
161168
if (err < 0) {
162169
devres_free(dmab);
163170
return NULL;
@@ -166,7 +173,7 @@ snd_devm_alloc_pages(struct device *dev, int type, size_t size)
166173
devres_add(dev, dmab);
167174
return dmab;
168175
}
169-
EXPORT_SYMBOL_GPL(snd_devm_alloc_pages);
176+
EXPORT_SYMBOL_GPL(snd_devm_alloc_dir_pages);
170177

171178
/**
172179
* snd_dma_buffer_mmap - perform mmap of the given DMA buffer
@@ -185,6 +192,26 @@ int snd_dma_buffer_mmap(struct snd_dma_buffer *dmab,
185192
}
186193
EXPORT_SYMBOL(snd_dma_buffer_mmap);
187194

195+
#ifdef CONFIG_HAS_DMA
196+
/**
197+
* snd_dma_buffer_sync - sync DMA buffer between CPU and device
198+
* @dmab: buffer allocation information
199+
* @mod: sync mode
200+
*/
201+
void snd_dma_buffer_sync(struct snd_dma_buffer *dmab,
202+
enum snd_dma_sync_mode mode)
203+
{
204+
const struct snd_malloc_ops *ops;
205+
206+
if (!dmab || !dmab->dev.need_sync)
207+
return;
208+
ops = snd_dma_get_ops(dmab);
209+
if (ops && ops->sync)
210+
ops->sync(dmab, mode);
211+
}
212+
EXPORT_SYMBOL_GPL(snd_dma_buffer_sync);
213+
#endif /* CONFIG_HAS_DMA */
214+
188215
/**
189216
* snd_sgbuf_get_addr - return the physical address at the corresponding offset
190217
* @dmab: buffer allocation information
@@ -468,6 +495,71 @@ static const struct snd_malloc_ops snd_dma_wc_ops = {
468495
.mmap = snd_dma_wc_mmap,
469496
};
470497
#endif /* CONFIG_X86 */
498+
499+
/*
500+
* Non-contiguous pages allocator
501+
*/
502+
static void *snd_dma_noncontig_alloc(struct snd_dma_buffer *dmab, size_t size)
503+
{
504+
struct sg_table *sgt;
505+
void *p;
506+
507+
sgt = dma_alloc_noncontiguous(dmab->dev.dev, size, dmab->dev.dir,
508+
DEFAULT_GFP, 0);
509+
if (!sgt)
510+
return NULL;
511+
dmab->dev.need_sync = dma_need_sync(dmab->dev.dev, dmab->dev.dir);
512+
p = dma_vmap_noncontiguous(dmab->dev.dev, size, sgt);
513+
if (p)
514+
dmab->private_data = sgt;
515+
else
516+
dma_free_noncontiguous(dmab->dev.dev, size, sgt, dmab->dev.dir);
517+
return p;
518+
}
519+
520+
static void snd_dma_noncontig_free(struct snd_dma_buffer *dmab)
521+
{
522+
dma_vunmap_noncontiguous(dmab->dev.dev, dmab->area);
523+
dma_free_noncontiguous(dmab->dev.dev, dmab->bytes, dmab->private_data,
524+
dmab->dev.dir);
525+
}
526+
527+
static int snd_dma_noncontig_mmap(struct snd_dma_buffer *dmab,
528+
struct vm_area_struct *area)
529+
{
530+
return dma_mmap_noncontiguous(dmab->dev.dev, area,
531+
dmab->bytes, dmab->private_data);
532+
}
533+
534+
static void snd_dma_noncontig_sync(struct snd_dma_buffer *dmab,
535+
enum snd_dma_sync_mode mode)
536+
{
537+
if (mode == SNDRV_DMA_SYNC_CPU) {
538+
if (dmab->dev.dir == DMA_TO_DEVICE)
539+
return;
540+
dma_sync_sgtable_for_cpu(dmab->dev.dev, dmab->private_data,
541+
dmab->dev.dir);
542+
invalidate_kernel_vmap_range(dmab->area, dmab->bytes);
543+
} else {
544+
if (dmab->dev.dir == DMA_FROM_DEVICE)
545+
return;
546+
flush_kernel_vmap_range(dmab->area, dmab->bytes);
547+
dma_sync_sgtable_for_device(dmab->dev.dev, dmab->private_data,
548+
dmab->dev.dir);
549+
}
550+
}
551+
552+
static const struct snd_malloc_ops snd_dma_noncontig_ops = {
553+
.alloc = snd_dma_noncontig_alloc,
554+
.free = snd_dma_noncontig_free,
555+
.mmap = snd_dma_noncontig_mmap,
556+
.sync = snd_dma_noncontig_sync,
557+
/* re-use vmalloc helpers for get_* ops */
558+
.get_addr = snd_dma_vmalloc_get_addr,
559+
.get_page = snd_dma_vmalloc_get_page,
560+
.get_chunk_size = snd_dma_vmalloc_get_chunk_size,
561+
};
562+
471563
#endif /* CONFIG_HAS_DMA */
472564

473565
/*
@@ -479,6 +571,7 @@ static const struct snd_malloc_ops *dma_ops[] = {
479571
#ifdef CONFIG_HAS_DMA
480572
[SNDRV_DMA_TYPE_DEV] = &snd_dma_dev_ops,
481573
[SNDRV_DMA_TYPE_DEV_WC] = &snd_dma_wc_ops,
574+
[SNDRV_DMA_TYPE_NONCONTIG] = &snd_dma_noncontig_ops,
482575
#ifdef CONFIG_GENERIC_ALLOCATOR
483576
[SNDRV_DMA_TYPE_DEV_IRAM] = &snd_dma_iram_ops,
484577
#endif /* CONFIG_GENERIC_ALLOCATOR */

‎sound/core/memalloc_local.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ struct snd_malloc_ops {
1010
unsigned int (*get_chunk_size)(struct snd_dma_buffer *dmab,
1111
unsigned int ofs, unsigned int size);
1212
int (*mmap)(struct snd_dma_buffer *dmab, struct vm_area_struct *area);
13+
void (*sync)(struct snd_dma_buffer *dmab, enum snd_dma_sync_mode mode);
1314
};
1415

1516
#ifdef CONFIG_SND_DMA_SGBUF

‎sound/core/pcm_compat.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,8 @@ static int snd_pcm_ioctl_sync_ptr_x32(struct snd_pcm_substream *substream,
453453
sstatus.suspended_state = status->suspended_state;
454454
sstatus.audio_tstamp = status->audio_tstamp;
455455
snd_pcm_stream_unlock_irq(substream);
456+
if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL))
457+
snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
456458
if (put_user(sstatus.state, &src->s.status.state) ||
457459
put_user(sstatus.hw_ptr, &src->s.status.hw_ptr) ||
458460
put_user(sstatus.tstamp.tv_sec, &src->s.status.tstamp_sec) ||
@@ -533,6 +535,8 @@ static int snd_pcm_ioctl_sync_ptr_buggy(struct snd_pcm_substream *substream,
533535
sync_ptr.s.status.suspended_state = status->suspended_state;
534536
sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
535537
snd_pcm_stream_unlock_irq(substream);
538+
if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
539+
snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
536540
if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
537541
return -EFAULT;
538542
return 0;

‎sound/core/pcm_lib.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_ufram
106106
frames -= transfer;
107107
ofs = 0;
108108
}
109+
snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
109110
}
110111

111112
#ifdef CONFIG_SND_DEBUG
@@ -2256,8 +2257,12 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
22562257
goto _end_unlock;
22572258
}
22582259
snd_pcm_stream_unlock_irq(substream);
2260+
if (!is_playback)
2261+
snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_CPU);
22592262
err = writer(substream, appl_ofs, data, offset, frames,
22602263
transfer);
2264+
if (is_playback)
2265+
snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
22612266
snd_pcm_stream_lock_irq(substream);
22622267
if (err < 0)
22632268
goto _end_unlock;

‎sound/core/pcm_local.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,11 @@ void snd_pcm_sync_stop(struct snd_pcm_substream *substream, bool sync_irq);
7373
for ((subs) = (pcm)->streams[str].substream; (subs); \
7474
(subs) = (subs)->next)
7575

76+
static inline void snd_pcm_dma_buffer_sync(struct snd_pcm_substream *substream,
77+
enum snd_dma_sync_mode mode)
78+
{
79+
if (substream->runtime->info & SNDRV_PCM_INFO_EXPLICIT_SYNC)
80+
snd_dma_buffer_sync(snd_pcm_get_dma_buf(substream), mode);
81+
}
82+
7683
#endif /* __SOUND_CORE_PCM_LOCAL_H */

‎sound/core/pcm_memory.c‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,20 @@ module_param(max_alloc_per_card, ulong, 0644);
3232
MODULE_PARM_DESC(max_alloc_per_card, "Max total allocation bytes per card.");
3333

3434
static int do_alloc_pages(struct snd_card *card, int type, struct device *dev,
35-
size_t size, struct snd_dma_buffer *dmab)
35+
int str, size_t size, struct snd_dma_buffer *dmab)
3636
{
37+
enum dma_data_direction dir;
3738
int err;
3839

3940
if (max_alloc_per_card &&
4041
card->total_pcm_alloc_bytes + size > max_alloc_per_card)
4142
return -ENOMEM;
4243

43-
err = snd_dma_alloc_pages(type, dev, size, dmab);
44+
if (str == SNDRV_PCM_STREAM_PLAYBACK)
45+
dir = DMA_TO_DEVICE;
46+
else
47+
dir = DMA_FROM_DEVICE;
48+
err = snd_dma_alloc_dir_pages(type, dev, dir, size, dmab);
4449
if (!err) {
4550
mutex_lock(&card->memory_mutex);
4651
card->total_pcm_alloc_bytes += dmab->bytes;
@@ -77,7 +82,7 @@ static int preallocate_pcm_pages(struct snd_pcm_substream *substream,
7782

7883
do {
7984
err = do_alloc_pages(card, dmab->dev.type, dmab->dev.dev,
80-
size, dmab);
85+
substream->stream, size, dmab);
8186
if (err != -ENOMEM)
8287
return err;
8388
if (no_fallback)
@@ -177,6 +182,7 @@ static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,
177182
if (do_alloc_pages(card,
178183
substream->dma_buffer.dev.type,
179184
substream->dma_buffer.dev.dev,
185+
substream->stream,
180186
size, &new_dmab) < 0) {
181187
buffer->error = -ENOMEM;
182188
pr_debug("ALSA pcmC%dD%d%c,%d:%s: cannot preallocate for size %zu\n",
@@ -418,6 +424,7 @@ int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size)
418424
if (do_alloc_pages(card,
419425
substream->dma_buffer.dev.type,
420426
substream->dma_buffer.dev.dev,
427+
substream->stream,
421428
size, dmab) < 0) {
422429
kfree(dmab);
423430
pr_debug("ALSA pcmC%dD%d%c,%d:%s: cannot preallocate for size %zu\n",

0 commit comments

Comments
 (0)