2019-05-27 14:55:05 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
2007-10-15 15:50:19 +08:00
|
|
|
* Copyright (c) by Jaroslav Kysela <perex@perex.cz>
|
2005-04-17 06:20:36 +08:00
|
|
|
* Takashi Iwai <tiwai@suse.de>
|
|
|
|
*
|
|
|
|
* Generic memory allocators
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/dma-mapping.h>
|
2022-11-10 21:22:16 +08:00
|
|
|
#include <linux/dma-map-ops.h>
|
2013-10-23 11:47:43 +08:00
|
|
|
#include <linux/genalloc.h>
|
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>
2021-10-17 15:48:57 +08:00
|
|
|
#include <linux/highmem.h>
|
2019-11-05 16:01:36 +08:00
|
|
|
#include <linux/vmalloc.h>
|
2018-08-08 23:01:00 +08:00
|
|
|
#ifdef CONFIG_X86
|
|
|
|
#include <asm/set_memory.h>
|
|
|
|
#endif
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <sound/memalloc.h>
|
2021-06-10 00:25:49 +08:00
|
|
|
#include "memalloc_local.h"
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2022-08-23 19:57:39 +08:00
|
|
|
#define DEFAULT_GFP \
|
|
|
|
(GFP_KERNEL | \
|
ALSA: memalloc: use __GFP_RETRY_MAYFAIL for DMA mem allocs
Use __GFP_RETRY_MAYFAIL instead of __GFP__NORETRY in
snd_dma_dev_alloc(), snd_dma_wc_alloc() and friends, to allocate pages
for device memory. The MAYFAIL flag retains the semantics of not
triggering the OOM killer, but lowers the risk of alloc failure.
MAYFAIL flag was added in commit dcda9b04713c3 ("mm, tree wide: replace
__GFP_REPEAT by __GFP_RETRY_MAYFAIL with more useful semantic").
This change addresses recurring failures with SOF audio driver in test
cases where a system suspend-resume stress test is run, combined with an
active high memory-load use-case. The failure typically shows up as:
[ 379.480229] sof-audio-pci-intel-tgl 0000:00:1f.3: booting DSP firmware
[ 379.484803] sof-audio-pci-intel-tgl 0000:00:1f.3: error: memory alloc failed: -12
[ 379.484810] sof-audio-pci-intel-tgl 0000:00:1f.3: error: dma prepare for ICCMAX stream failed
Multiple fixes to reduce the memory usage of DSP boot have been
identified in SOF driver, but even with those fixes, debug on affected
systems has shown that even a single page alloc may fail with
__GFP_NORETRY. When this occurs, system is under significant load on
physical memory, but a lot of reclaimable pages are available, so the
system has not run out of memory. With __GFP_RETRY_MAYFAIL, the errors
are not hit in these stress tests.
The alloc failure is severe as audio capability is completely lost if
alloc failure is hit at system resume.
An alternative solution was considered where the resources for DSP boot
would be kept allocated until driver is unbound. This would avoid the
allocation failure, but consume memory that is only needed temporarily
at probe and resume time. It seems better to not hang on to the memory,
but rather work a bit harder for allocating the pages at resume.
BugLink: https://github.com/thesofproject/linux/issues/3844
Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
Reviewed-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
Link: https://lore.kernel.org/r/20220923153501.3326041-1-kai.vehmanen@linux.intel.com
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2022-09-23 23:35:01 +08:00
|
|
|
__GFP_RETRY_MAYFAIL | /* don't trigger OOM-killer */ \
|
2022-08-23 19:57:39 +08:00
|
|
|
__GFP_NOWARN) /* no stack trace print - this call is non-critical */
|
|
|
|
|
2021-06-10 00:25:49 +08:00
|
|
|
static const struct snd_malloc_ops *snd_dma_get_ops(struct snd_dma_buffer *dmab);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2022-08-21 23:59:11 +08:00
|
|
|
#ifdef CONFIG_SND_DMA_SGBUF
|
|
|
|
static void *snd_dma_sg_fallback_alloc(struct snd_dma_buffer *dmab, size_t size);
|
|
|
|
#endif
|
|
|
|
|
2021-08-02 15:28:01 +08:00
|
|
|
static void *__snd_dma_alloc_pages(struct snd_dma_buffer *dmab, size_t size)
|
2013-10-23 11:47:43 +08:00
|
|
|
{
|
2021-06-10 00:25:49 +08:00
|
|
|
const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
|
2013-10-23 11:47:43 +08:00
|
|
|
|
2021-06-10 00:25:49 +08:00
|
|
|
if (WARN_ON_ONCE(!ops || !ops->alloc))
|
2021-08-02 15:28:01 +08:00
|
|
|
return NULL;
|
2021-06-10 00:25:49 +08:00
|
|
|
return ops->alloc(dmab, size);
|
2019-11-05 16:01:35 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/**
|
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>
2021-10-17 15:48:57 +08:00
|
|
|
* snd_dma_alloc_dir_pages - allocate the buffer area according to the given
|
|
|
|
* type and direction
|
2005-04-17 06:20:36 +08:00
|
|
|
* @type: the DMA buffer type
|
|
|
|
* @device: the device pointer
|
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>
2021-10-17 15:48:57 +08:00
|
|
|
* @dir: DMA direction
|
2005-04-17 06:20:36 +08:00
|
|
|
* @size: the buffer size to allocate
|
|
|
|
* @dmab: buffer allocation record to store the allocated data
|
|
|
|
*
|
|
|
|
* Calls the memory-allocator function for the corresponding
|
|
|
|
* buffer type.
|
2013-03-12 05:05:14 +08:00
|
|
|
*
|
|
|
|
* Return: Zero if the buffer with the given size is allocated successfully,
|
|
|
|
* otherwise a negative value on error.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
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>
2021-10-17 15:48:57 +08:00
|
|
|
int snd_dma_alloc_dir_pages(int type, struct device *device,
|
|
|
|
enum dma_data_direction dir, size_t size,
|
|
|
|
struct snd_dma_buffer *dmab)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-08-08 23:09:09 +08:00
|
|
|
if (WARN_ON(!size))
|
|
|
|
return -ENXIO;
|
|
|
|
if (WARN_ON(!dmab))
|
|
|
|
return -ENXIO;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2020-12-18 22:56:24 +08:00
|
|
|
size = PAGE_ALIGN(size);
|
2005-04-17 06:20:36 +08:00
|
|
|
dmab->dev.type = type;
|
|
|
|
dmab->dev.dev = device;
|
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>
2021-10-17 15:48:57 +08:00
|
|
|
dmab->dev.dir = dir;
|
2005-04-17 06:20:36 +08:00
|
|
|
dmab->bytes = 0;
|
2020-06-16 00:00:43 +08:00
|
|
|
dmab->addr = 0;
|
|
|
|
dmab->private_data = NULL;
|
2021-08-02 15:28:01 +08:00
|
|
|
dmab->area = __snd_dma_alloc_pages(dmab, size);
|
2021-06-10 00:25:49 +08:00
|
|
|
if (!dmab->area)
|
2005-04-17 06:20:36 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
dmab->bytes = size;
|
|
|
|
return 0;
|
|
|
|
}
|
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>
2021-10-17 15:48:57 +08:00
|
|
|
EXPORT_SYMBOL(snd_dma_alloc_dir_pages);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
|
|
|
|
* @type: the DMA buffer type
|
|
|
|
* @device: the device pointer
|
|
|
|
* @size: the buffer size to allocate
|
|
|
|
* @dmab: buffer allocation record to store the allocated data
|
|
|
|
*
|
|
|
|
* Calls the memory-allocator function for the corresponding
|
|
|
|
* buffer type. When no space is left, this function reduces the size and
|
|
|
|
* tries to allocate again. The size actually allocated is stored in
|
|
|
|
* res_size argument.
|
2013-03-12 05:05:14 +08:00
|
|
|
*
|
|
|
|
* Return: Zero if the buffer with the given size is allocated successfully,
|
|
|
|
* otherwise a negative value on error.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
|
|
|
|
struct snd_dma_buffer *dmab)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
|
|
|
|
if (err != -ENOMEM)
|
|
|
|
return err;
|
|
|
|
if (size <= PAGE_SIZE)
|
|
|
|
return -ENOMEM;
|
2018-07-19 17:01:04 +08:00
|
|
|
size >>= 1;
|
|
|
|
size = PAGE_SIZE << get_order(size);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
if (! dmab->area)
|
|
|
|
return -ENOMEM;
|
|
|
|
return 0;
|
|
|
|
}
|
2017-06-16 22:16:33 +08:00
|
|
|
EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* snd_dma_free_pages - release the allocated buffer
|
|
|
|
* @dmab: the buffer allocation record to release
|
|
|
|
*
|
|
|
|
* Releases the allocated buffer via snd_dma_alloc_pages().
|
|
|
|
*/
|
|
|
|
void snd_dma_free_pages(struct snd_dma_buffer *dmab)
|
|
|
|
{
|
2021-06-10 00:25:49 +08:00
|
|
|
const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
|
|
|
|
|
|
|
|
if (ops && ops->free)
|
|
|
|
ops->free(dmab);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(snd_dma_free_pages);
|
|
|
|
|
2021-07-15 15:58:23 +08:00
|
|
|
/* called by devres */
|
|
|
|
static void __snd_release_pages(struct device *dev, void *res)
|
|
|
|
{
|
|
|
|
snd_dma_free_pages(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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>
2021-10-17 15:48:57 +08:00
|
|
|
* snd_devm_alloc_dir_pages - allocate the buffer and manage with devres
|
2021-07-15 15:58:23 +08:00
|
|
|
* @dev: the device pointer
|
|
|
|
* @type: the DMA buffer type
|
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>
2021-10-17 15:48:57 +08:00
|
|
|
* @dir: DMA direction
|
2021-07-15 15:58:23 +08:00
|
|
|
* @size: the buffer size to allocate
|
|
|
|
*
|
|
|
|
* Allocate buffer pages depending on the given type and manage using devres.
|
|
|
|
* The pages will be released automatically at the device removal.
|
|
|
|
*
|
|
|
|
* Unlike snd_dma_alloc_pages(), this function requires the real device pointer,
|
|
|
|
* hence it can't work with SNDRV_DMA_TYPE_CONTINUOUS or
|
|
|
|
* SNDRV_DMA_TYPE_VMALLOC type.
|
|
|
|
*
|
2022-07-13 18:47:58 +08:00
|
|
|
* Return: the snd_dma_buffer object at success, or NULL if failed
|
2021-07-15 15:58:23 +08:00
|
|
|
*/
|
|
|
|
struct snd_dma_buffer *
|
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>
2021-10-17 15:48:57 +08:00
|
|
|
snd_devm_alloc_dir_pages(struct device *dev, int type,
|
|
|
|
enum dma_data_direction dir, size_t size)
|
2021-07-15 15:58:23 +08:00
|
|
|
{
|
|
|
|
struct snd_dma_buffer *dmab;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (WARN_ON(type == SNDRV_DMA_TYPE_CONTINUOUS ||
|
|
|
|
type == SNDRV_DMA_TYPE_VMALLOC))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
dmab = devres_alloc(__snd_release_pages, sizeof(*dmab), GFP_KERNEL);
|
|
|
|
if (!dmab)
|
|
|
|
return NULL;
|
|
|
|
|
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>
2021-10-17 15:48:57 +08:00
|
|
|
err = snd_dma_alloc_dir_pages(type, dev, dir, size, dmab);
|
2021-07-15 15:58:23 +08:00
|
|
|
if (err < 0) {
|
|
|
|
devres_free(dmab);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
devres_add(dev, dmab);
|
|
|
|
return dmab;
|
|
|
|
}
|
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>
2021-10-17 15:48:57 +08:00
|
|
|
EXPORT_SYMBOL_GPL(snd_devm_alloc_dir_pages);
|
2021-07-15 15:58:23 +08:00
|
|
|
|
2021-06-10 00:25:50 +08:00
|
|
|
/**
|
|
|
|
* snd_dma_buffer_mmap - perform mmap of the given DMA buffer
|
|
|
|
* @dmab: buffer allocation information
|
|
|
|
* @area: VM area information
|
2022-07-13 18:47:58 +08:00
|
|
|
*
|
|
|
|
* Return: zero if successful, or a negative error code
|
2021-06-10 00:25:50 +08:00
|
|
|
*/
|
|
|
|
int snd_dma_buffer_mmap(struct snd_dma_buffer *dmab,
|
|
|
|
struct vm_area_struct *area)
|
|
|
|
{
|
2021-11-08 00:39:11 +08:00
|
|
|
const struct snd_malloc_ops *ops;
|
2021-06-10 00:25:50 +08:00
|
|
|
|
2021-11-08 00:39:11 +08:00
|
|
|
if (!dmab)
|
|
|
|
return -ENOENT;
|
|
|
|
ops = snd_dma_get_ops(dmab);
|
2021-06-10 00:25:50 +08:00
|
|
|
if (ops && ops->mmap)
|
|
|
|
return ops->mmap(dmab, area);
|
|
|
|
else
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(snd_dma_buffer_mmap);
|
|
|
|
|
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>
2021-10-17 15:48:57 +08:00
|
|
|
#ifdef CONFIG_HAS_DMA
|
|
|
|
/**
|
|
|
|
* snd_dma_buffer_sync - sync DMA buffer between CPU and device
|
|
|
|
* @dmab: buffer allocation information
|
2021-10-19 14:05:36 +08:00
|
|
|
* @mode: sync mode
|
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>
2021-10-17 15:48:57 +08:00
|
|
|
*/
|
|
|
|
void snd_dma_buffer_sync(struct snd_dma_buffer *dmab,
|
|
|
|
enum snd_dma_sync_mode mode)
|
|
|
|
{
|
|
|
|
const struct snd_malloc_ops *ops;
|
|
|
|
|
|
|
|
if (!dmab || !dmab->dev.need_sync)
|
|
|
|
return;
|
|
|
|
ops = snd_dma_get_ops(dmab);
|
|
|
|
if (ops && ops->sync)
|
|
|
|
ops->sync(dmab, mode);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(snd_dma_buffer_sync);
|
|
|
|
#endif /* CONFIG_HAS_DMA */
|
|
|
|
|
2021-06-10 00:25:49 +08:00
|
|
|
/**
|
|
|
|
* snd_sgbuf_get_addr - return the physical address at the corresponding offset
|
|
|
|
* @dmab: buffer allocation information
|
|
|
|
* @offset: offset in the ring buffer
|
2022-07-13 18:47:58 +08:00
|
|
|
*
|
|
|
|
* Return: the physical address
|
2021-06-10 00:25:49 +08:00
|
|
|
*/
|
|
|
|
dma_addr_t snd_sgbuf_get_addr(struct snd_dma_buffer *dmab, size_t offset)
|
|
|
|
{
|
|
|
|
const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
|
|
|
|
|
|
|
|
if (ops && ops->get_addr)
|
|
|
|
return ops->get_addr(dmab, offset);
|
|
|
|
else
|
|
|
|
return dmab->addr + offset;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(snd_sgbuf_get_addr);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* snd_sgbuf_get_page - return the physical page at the corresponding offset
|
|
|
|
* @dmab: buffer allocation information
|
|
|
|
* @offset: offset in the ring buffer
|
2022-07-13 18:47:58 +08:00
|
|
|
*
|
|
|
|
* Return: the page pointer
|
2021-06-10 00:25:49 +08:00
|
|
|
*/
|
|
|
|
struct page *snd_sgbuf_get_page(struct snd_dma_buffer *dmab, size_t offset)
|
|
|
|
{
|
|
|
|
const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
|
|
|
|
|
|
|
|
if (ops && ops->get_page)
|
|
|
|
return ops->get_page(dmab, offset);
|
|
|
|
else
|
|
|
|
return virt_to_page(dmab->area + offset);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(snd_sgbuf_get_page);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* snd_sgbuf_get_chunk_size - compute the max chunk size with continuous pages
|
|
|
|
* on sg-buffer
|
|
|
|
* @dmab: buffer allocation information
|
|
|
|
* @ofs: offset in the ring buffer
|
|
|
|
* @size: the requested size
|
2022-07-13 18:47:58 +08:00
|
|
|
*
|
|
|
|
* Return: the chunk size
|
2021-06-10 00:25:49 +08:00
|
|
|
*/
|
|
|
|
unsigned int snd_sgbuf_get_chunk_size(struct snd_dma_buffer *dmab,
|
|
|
|
unsigned int ofs, unsigned int size)
|
|
|
|
{
|
|
|
|
const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
|
|
|
|
|
|
|
|
if (ops && ops->get_chunk_size)
|
|
|
|
return ops->get_chunk_size(dmab, ofs, size);
|
|
|
|
else
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(snd_sgbuf_get_chunk_size);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Continuous pages allocator
|
|
|
|
*/
|
2022-08-23 19:57:39 +08:00
|
|
|
static void *do_alloc_pages(struct device *dev, size_t size, dma_addr_t *addr,
|
|
|
|
bool wc)
|
2021-06-10 00:25:49 +08:00
|
|
|
{
|
2022-08-23 19:57:39 +08:00
|
|
|
void *p;
|
|
|
|
gfp_t gfp = GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN;
|
2021-06-10 00:25:49 +08:00
|
|
|
|
2022-08-23 19:57:39 +08:00
|
|
|
again:
|
|
|
|
p = alloc_pages_exact(size, gfp);
|
|
|
|
if (!p)
|
|
|
|
return NULL;
|
|
|
|
*addr = page_to_phys(virt_to_page(p));
|
|
|
|
if (!dev)
|
|
|
|
return p;
|
|
|
|
if ((*addr + size - 1) & ~dev->coherent_dma_mask) {
|
|
|
|
if (IS_ENABLED(CONFIG_ZONE_DMA32) && !(gfp & GFP_DMA32)) {
|
|
|
|
gfp |= GFP_DMA32;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
if (IS_ENABLED(CONFIG_ZONE_DMA) && !(gfp & GFP_DMA)) {
|
|
|
|
gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef CONFIG_X86
|
|
|
|
if (wc)
|
|
|
|
set_memory_wc((unsigned long)(p), size >> PAGE_SHIFT);
|
|
|
|
#endif
|
2021-08-04 15:41:25 +08:00
|
|
|
return p;
|
2021-06-10 00:25:49 +08:00
|
|
|
}
|
|
|
|
|
2022-08-23 19:57:39 +08:00
|
|
|
static void do_free_pages(void *p, size_t size, bool wc)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_X86
|
|
|
|
if (wc)
|
|
|
|
set_memory_wb((unsigned long)(p), size >> PAGE_SHIFT);
|
|
|
|
#endif
|
|
|
|
free_pages_exact(p, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-21 23:59:11 +08:00
|
|
|
static void *snd_dma_continuous_alloc(struct snd_dma_buffer *dmab, size_t size)
|
|
|
|
{
|
2022-08-23 19:57:39 +08:00
|
|
|
return do_alloc_pages(dmab->dev.dev, size, &dmab->addr, false);
|
2022-08-21 23:59:11 +08:00
|
|
|
}
|
|
|
|
|
2021-06-10 00:25:49 +08:00
|
|
|
static void snd_dma_continuous_free(struct snd_dma_buffer *dmab)
|
|
|
|
{
|
2022-08-23 19:57:39 +08:00
|
|
|
do_free_pages(dmab->area, dmab->bytes, false);
|
2021-06-10 00:25:49 +08:00
|
|
|
}
|
|
|
|
|
2021-06-10 00:25:51 +08:00
|
|
|
static int snd_dma_continuous_mmap(struct snd_dma_buffer *dmab,
|
|
|
|
struct vm_area_struct *area)
|
|
|
|
{
|
|
|
|
return remap_pfn_range(area, area->vm_start,
|
2021-08-04 15:41:25 +08:00
|
|
|
dmab->addr >> PAGE_SHIFT,
|
2021-06-10 00:25:51 +08:00
|
|
|
area->vm_end - area->vm_start,
|
|
|
|
area->vm_page_prot);
|
|
|
|
}
|
|
|
|
|
2021-06-10 00:25:49 +08:00
|
|
|
static const struct snd_malloc_ops snd_dma_continuous_ops = {
|
|
|
|
.alloc = snd_dma_continuous_alloc,
|
|
|
|
.free = snd_dma_continuous_free,
|
2021-06-10 00:25:51 +08:00
|
|
|
.mmap = snd_dma_continuous_mmap,
|
2021-06-10 00:25:49 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* VMALLOC allocator
|
|
|
|
*/
|
2021-08-02 15:28:01 +08:00
|
|
|
static void *snd_dma_vmalloc_alloc(struct snd_dma_buffer *dmab, size_t size)
|
2021-06-10 00:25:49 +08:00
|
|
|
{
|
2022-08-23 19:57:39 +08:00
|
|
|
return vmalloc(size);
|
2021-06-10 00:25:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void snd_dma_vmalloc_free(struct snd_dma_buffer *dmab)
|
|
|
|
{
|
|
|
|
vfree(dmab->area);
|
|
|
|
}
|
|
|
|
|
2021-06-10 00:25:51 +08:00
|
|
|
static int snd_dma_vmalloc_mmap(struct snd_dma_buffer *dmab,
|
|
|
|
struct vm_area_struct *area)
|
|
|
|
{
|
|
|
|
return remap_vmalloc_range(area, dmab->area, 0);
|
|
|
|
}
|
|
|
|
|
2021-08-13 16:16:45 +08:00
|
|
|
#define get_vmalloc_page_addr(dmab, offset) \
|
|
|
|
page_to_phys(vmalloc_to_page((dmab)->area + (offset)))
|
|
|
|
|
2021-06-10 00:25:49 +08:00
|
|
|
static dma_addr_t snd_dma_vmalloc_get_addr(struct snd_dma_buffer *dmab,
|
|
|
|
size_t offset)
|
|
|
|
{
|
2021-08-13 16:16:45 +08:00
|
|
|
return get_vmalloc_page_addr(dmab, offset) + offset % PAGE_SIZE;
|
2021-06-10 00:25:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct page *snd_dma_vmalloc_get_page(struct snd_dma_buffer *dmab,
|
|
|
|
size_t offset)
|
|
|
|
{
|
|
|
|
return vmalloc_to_page(dmab->area + offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int
|
|
|
|
snd_dma_vmalloc_get_chunk_size(struct snd_dma_buffer *dmab,
|
|
|
|
unsigned int ofs, unsigned int size)
|
|
|
|
{
|
2021-08-13 16:16:45 +08:00
|
|
|
unsigned int start, end;
|
|
|
|
unsigned long addr;
|
|
|
|
|
|
|
|
start = ALIGN_DOWN(ofs, PAGE_SIZE);
|
|
|
|
end = ofs + size - 1; /* the last byte address */
|
|
|
|
/* check page continuity */
|
|
|
|
addr = get_vmalloc_page_addr(dmab, start);
|
|
|
|
for (;;) {
|
|
|
|
start += PAGE_SIZE;
|
|
|
|
if (start > end)
|
|
|
|
break;
|
|
|
|
addr += PAGE_SIZE;
|
|
|
|
if (get_vmalloc_page_addr(dmab, start) != addr)
|
|
|
|
return start - ofs;
|
|
|
|
}
|
|
|
|
/* ok, all on continuous pages */
|
|
|
|
return size;
|
2021-06-10 00:25:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct snd_malloc_ops snd_dma_vmalloc_ops = {
|
|
|
|
.alloc = snd_dma_vmalloc_alloc,
|
|
|
|
.free = snd_dma_vmalloc_free,
|
2021-06-10 00:25:51 +08:00
|
|
|
.mmap = snd_dma_vmalloc_mmap,
|
2021-06-10 00:25:49 +08:00
|
|
|
.get_addr = snd_dma_vmalloc_get_addr,
|
|
|
|
.get_page = snd_dma_vmalloc_get_page,
|
|
|
|
.get_chunk_size = snd_dma_vmalloc_get_chunk_size,
|
|
|
|
};
|
|
|
|
|
2007-07-27 00:59:36 +08:00
|
|
|
#ifdef CONFIG_HAS_DMA
|
2021-06-10 00:25:49 +08:00
|
|
|
/*
|
|
|
|
* IRAM allocator
|
|
|
|
*/
|
2013-10-24 20:25:32 +08:00
|
|
|
#ifdef CONFIG_GENERIC_ALLOCATOR
|
2021-08-02 15:28:01 +08:00
|
|
|
static void *snd_dma_iram_alloc(struct snd_dma_buffer *dmab, size_t size)
|
2021-06-10 00:25:49 +08:00
|
|
|
{
|
|
|
|
struct device *dev = dmab->dev.dev;
|
|
|
|
struct gen_pool *pool;
|
2021-08-02 15:28:01 +08:00
|
|
|
void *p;
|
2021-06-10 00:25:49 +08:00
|
|
|
|
|
|
|
if (dev->of_node) {
|
|
|
|
pool = of_gen_pool_get(dev->of_node, "iram", 0);
|
|
|
|
/* Assign the pool into private_data field */
|
|
|
|
dmab->private_data = pool;
|
|
|
|
|
2021-08-02 15:28:01 +08:00
|
|
|
p = gen_pool_dma_alloc_align(pool, size, &dmab->addr, PAGE_SIZE);
|
|
|
|
if (p)
|
|
|
|
return p;
|
2021-06-10 00:25:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Internal memory might have limited size and no enough space,
|
|
|
|
* so if we fail to malloc, try to fetch memory traditionally.
|
|
|
|
*/
|
|
|
|
dmab->dev.type = SNDRV_DMA_TYPE_DEV;
|
|
|
|
return __snd_dma_alloc_pages(dmab, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void snd_dma_iram_free(struct snd_dma_buffer *dmab)
|
|
|
|
{
|
|
|
|
struct gen_pool *pool = dmab->private_data;
|
|
|
|
|
|
|
|
if (pool && dmab->area)
|
|
|
|
gen_pool_free(pool, (unsigned long)dmab->area, dmab->bytes);
|
|
|
|
}
|
|
|
|
|
2021-06-10 00:25:50 +08:00
|
|
|
static int snd_dma_iram_mmap(struct snd_dma_buffer *dmab,
|
|
|
|
struct vm_area_struct *area)
|
|
|
|
{
|
|
|
|
area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
|
|
|
|
return remap_pfn_range(area, area->vm_start,
|
|
|
|
dmab->addr >> PAGE_SHIFT,
|
|
|
|
area->vm_end - area->vm_start,
|
|
|
|
area->vm_page_prot);
|
|
|
|
}
|
|
|
|
|
2021-06-10 00:25:49 +08:00
|
|
|
static const struct snd_malloc_ops snd_dma_iram_ops = {
|
|
|
|
.alloc = snd_dma_iram_alloc,
|
|
|
|
.free = snd_dma_iram_free,
|
2021-06-10 00:25:50 +08:00
|
|
|
.mmap = snd_dma_iram_mmap,
|
2021-06-10 00:25:49 +08:00
|
|
|
};
|
2013-10-24 20:25:32 +08:00
|
|
|
#endif /* CONFIG_GENERIC_ALLOCATOR */
|
2021-06-10 00:25:49 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Coherent device pages allocator
|
|
|
|
*/
|
2021-08-02 15:28:01 +08:00
|
|
|
static void *snd_dma_dev_alloc(struct snd_dma_buffer *dmab, size_t size)
|
2021-06-10 00:25:49 +08:00
|
|
|
{
|
2022-06-20 15:34:40 +08:00
|
|
|
return dma_alloc_coherent(dmab->dev.dev, size, &dmab->addr, DEFAULT_GFP);
|
2021-06-10 00:25:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void snd_dma_dev_free(struct snd_dma_buffer *dmab)
|
|
|
|
{
|
|
|
|
dma_free_coherent(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
|
|
|
|
}
|
|
|
|
|
2021-06-10 00:25:50 +08:00
|
|
|
static int snd_dma_dev_mmap(struct snd_dma_buffer *dmab,
|
|
|
|
struct vm_area_struct *area)
|
|
|
|
{
|
|
|
|
return dma_mmap_coherent(dmab->dev.dev, area,
|
|
|
|
dmab->area, dmab->addr, dmab->bytes);
|
|
|
|
}
|
|
|
|
|
2021-06-10 00:25:49 +08:00
|
|
|
static const struct snd_malloc_ops snd_dma_dev_ops = {
|
|
|
|
.alloc = snd_dma_dev_alloc,
|
|
|
|
.free = snd_dma_dev_free,
|
2021-06-10 00:25:50 +08:00
|
|
|
.mmap = snd_dma_dev_mmap,
|
2021-06-10 00:25:49 +08:00
|
|
|
};
|
2021-08-02 15:28:04 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Write-combined pages
|
|
|
|
*/
|
2022-08-21 23:59:11 +08:00
|
|
|
/* x86-specific allocations */
|
|
|
|
#ifdef CONFIG_SND_DMA_SGBUF
|
|
|
|
static void *snd_dma_wc_alloc(struct snd_dma_buffer *dmab, size_t size)
|
|
|
|
{
|
2022-08-23 19:57:39 +08:00
|
|
|
return do_alloc_pages(dmab->dev.dev, size, &dmab->addr, true);
|
2022-08-21 23:59:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void snd_dma_wc_free(struct snd_dma_buffer *dmab)
|
|
|
|
{
|
2022-08-23 19:57:39 +08:00
|
|
|
do_free_pages(dmab->area, dmab->bytes, true);
|
2022-08-21 23:59:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int snd_dma_wc_mmap(struct snd_dma_buffer *dmab,
|
|
|
|
struct vm_area_struct *area)
|
|
|
|
{
|
|
|
|
area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
|
|
|
|
return snd_dma_continuous_mmap(dmab, area);
|
|
|
|
}
|
|
|
|
#else
|
2021-08-02 15:28:04 +08:00
|
|
|
static void *snd_dma_wc_alloc(struct snd_dma_buffer *dmab, size_t size)
|
|
|
|
{
|
|
|
|
return dma_alloc_wc(dmab->dev.dev, size, &dmab->addr, DEFAULT_GFP);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void snd_dma_wc_free(struct snd_dma_buffer *dmab)
|
|
|
|
{
|
|
|
|
dma_free_wc(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int snd_dma_wc_mmap(struct snd_dma_buffer *dmab,
|
|
|
|
struct vm_area_struct *area)
|
|
|
|
{
|
|
|
|
return dma_mmap_wc(dmab->dev.dev, area,
|
|
|
|
dmab->area, dmab->addr, dmab->bytes);
|
|
|
|
}
|
2022-08-21 23:59:11 +08:00
|
|
|
#endif /* CONFIG_SND_DMA_SGBUF */
|
2021-08-02 15:28:04 +08:00
|
|
|
|
|
|
|
static const struct snd_malloc_ops snd_dma_wc_ops = {
|
|
|
|
.alloc = snd_dma_wc_alloc,
|
|
|
|
.free = snd_dma_wc_free,
|
|
|
|
.mmap = snd_dma_wc_mmap,
|
|
|
|
};
|
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>
2021-10-17 15:48:57 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Non-contiguous pages allocator
|
|
|
|
*/
|
|
|
|
static void *snd_dma_noncontig_alloc(struct snd_dma_buffer *dmab, size_t size)
|
|
|
|
{
|
|
|
|
struct sg_table *sgt;
|
|
|
|
void *p;
|
|
|
|
|
2022-11-12 16:47:18 +08:00
|
|
|
sgt = dma_alloc_noncontiguous(dmab->dev.dev, size, dmab->dev.dir,
|
2022-12-20 16:05:36 +08:00
|
|
|
DEFAULT_GFP, 0);
|
ALSA: memalloc: Add fallback SG-buffer allocations for x86
The recent change for memory allocator replaced the SG-buffer handling
helper for x86 with the standard non-contiguous page handler. This
works for most cases, but there is a corner case I obviously
overlooked, namely, the fallback of non-contiguous handler without
IOMMU. When the system runs without IOMMU, the core handler tries to
use the continuous pages with a single SGL entry. It works nicely for
most cases, but when the system memory gets fragmented, the large
allocation may fail frequently.
Ideally the non-contig handler could deal with the proper SG pages,
it's cumbersome to extend for now. As a workaround, here we add new
types for (minimalistic) SG allocations, instead, so that the
allocator falls back to those types automatically when the allocation
with the standard API failed.
BTW, one better (but pretty minor) improvement from the previous
SG-buffer code is that this provides the proper mmap support without
the PCM's page fault handling.
Fixes: 2c95b92ecd92 ("ALSA: memalloc: Unify x86 SG-buffer handling (take#3)")
BugLink: https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/2272
BugLink: https://bugzilla.suse.com/show_bug.cgi?id=1198248
Cc: <stable@vger.kernel.org>
Link: https://lore.kernel.org/r/20220413054808.7547-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2022-04-13 13:48:08 +08:00
|
|
|
#ifdef CONFIG_SND_DMA_SGBUF
|
2022-11-12 16:47:18 +08:00
|
|
|
if (!sgt && !get_dma_ops(dmab->dev.dev)) {
|
ALSA: memalloc: Add fallback SG-buffer allocations for x86
The recent change for memory allocator replaced the SG-buffer handling
helper for x86 with the standard non-contiguous page handler. This
works for most cases, but there is a corner case I obviously
overlooked, namely, the fallback of non-contiguous handler without
IOMMU. When the system runs without IOMMU, the core handler tries to
use the continuous pages with a single SGL entry. It works nicely for
most cases, but when the system memory gets fragmented, the large
allocation may fail frequently.
Ideally the non-contig handler could deal with the proper SG pages,
it's cumbersome to extend for now. As a workaround, here we add new
types for (minimalistic) SG allocations, instead, so that the
allocator falls back to those types automatically when the allocation
with the standard API failed.
BTW, one better (but pretty minor) improvement from the previous
SG-buffer code is that this provides the proper mmap support without
the PCM's page fault handling.
Fixes: 2c95b92ecd92 ("ALSA: memalloc: Unify x86 SG-buffer handling (take#3)")
BugLink: https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/2272
BugLink: https://bugzilla.suse.com/show_bug.cgi?id=1198248
Cc: <stable@vger.kernel.org>
Link: https://lore.kernel.org/r/20220413054808.7547-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2022-04-13 13:48:08 +08:00
|
|
|
if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG)
|
|
|
|
dmab->dev.type = SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK;
|
|
|
|
else
|
|
|
|
dmab->dev.type = SNDRV_DMA_TYPE_DEV_SG_FALLBACK;
|
|
|
|
return snd_dma_sg_fallback_alloc(dmab, size);
|
|
|
|
}
|
2022-11-10 21:22:16 +08:00
|
|
|
#endif
|
|
|
|
if (!sgt)
|
|
|
|
return NULL;
|
ALSA: memalloc: Add fallback SG-buffer allocations for x86
The recent change for memory allocator replaced the SG-buffer handling
helper for x86 with the standard non-contiguous page handler. This
works for most cases, but there is a corner case I obviously
overlooked, namely, the fallback of non-contiguous handler without
IOMMU. When the system runs without IOMMU, the core handler tries to
use the continuous pages with a single SGL entry. It works nicely for
most cases, but when the system memory gets fragmented, the large
allocation may fail frequently.
Ideally the non-contig handler could deal with the proper SG pages,
it's cumbersome to extend for now. As a workaround, here we add new
types for (minimalistic) SG allocations, instead, so that the
allocator falls back to those types automatically when the allocation
with the standard API failed.
BTW, one better (but pretty minor) improvement from the previous
SG-buffer code is that this provides the proper mmap support without
the PCM's page fault handling.
Fixes: 2c95b92ecd92 ("ALSA: memalloc: Unify x86 SG-buffer handling (take#3)")
BugLink: https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/2272
BugLink: https://bugzilla.suse.com/show_bug.cgi?id=1198248
Cc: <stable@vger.kernel.org>
Link: https://lore.kernel.org/r/20220413054808.7547-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2022-04-13 13:48:08 +08:00
|
|
|
|
2022-02-10 20:33:43 +08:00
|
|
|
dmab->dev.need_sync = dma_need_sync(dmab->dev.dev,
|
|
|
|
sg_dma_address(sgt->sgl));
|
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>
2021-10-17 15:48:57 +08:00
|
|
|
p = dma_vmap_noncontiguous(dmab->dev.dev, size, sgt);
|
2022-09-06 17:03:19 +08:00
|
|
|
if (p) {
|
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>
2021-10-17 15:48:57 +08:00
|
|
|
dmab->private_data = sgt;
|
2022-09-06 17:03:19 +08:00
|
|
|
/* store the first page address for convenience */
|
|
|
|
dmab->addr = snd_sgbuf_get_addr(dmab, 0);
|
|
|
|
} else {
|
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>
2021-10-17 15:48:57 +08:00
|
|
|
dma_free_noncontiguous(dmab->dev.dev, size, sgt, dmab->dev.dir);
|
2022-09-06 17:03:19 +08:00
|
|
|
}
|
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>
2021-10-17 15:48:57 +08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void snd_dma_noncontig_free(struct snd_dma_buffer *dmab)
|
|
|
|
{
|
|
|
|
dma_vunmap_noncontiguous(dmab->dev.dev, dmab->area);
|
|
|
|
dma_free_noncontiguous(dmab->dev.dev, dmab->bytes, dmab->private_data,
|
|
|
|
dmab->dev.dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int snd_dma_noncontig_mmap(struct snd_dma_buffer *dmab,
|
|
|
|
struct vm_area_struct *area)
|
|
|
|
{
|
|
|
|
return dma_mmap_noncontiguous(dmab->dev.dev, area,
|
|
|
|
dmab->bytes, dmab->private_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void snd_dma_noncontig_sync(struct snd_dma_buffer *dmab,
|
|
|
|
enum snd_dma_sync_mode mode)
|
|
|
|
{
|
|
|
|
if (mode == SNDRV_DMA_SYNC_CPU) {
|
|
|
|
if (dmab->dev.dir == DMA_TO_DEVICE)
|
|
|
|
return;
|
2022-02-10 20:33:44 +08:00
|
|
|
invalidate_kernel_vmap_range(dmab->area, dmab->bytes);
|
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>
2021-10-17 15:48:57 +08:00
|
|
|
dma_sync_sgtable_for_cpu(dmab->dev.dev, dmab->private_data,
|
|
|
|
dmab->dev.dir);
|
|
|
|
} else {
|
|
|
|
if (dmab->dev.dir == DMA_FROM_DEVICE)
|
|
|
|
return;
|
|
|
|
flush_kernel_vmap_range(dmab->area, dmab->bytes);
|
|
|
|
dma_sync_sgtable_for_device(dmab->dev.dev, dmab->private_data,
|
|
|
|
dmab->dev.dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 23:10:59 +08:00
|
|
|
static inline void snd_dma_noncontig_iter_set(struct snd_dma_buffer *dmab,
|
|
|
|
struct sg_page_iter *piter,
|
|
|
|
size_t offset)
|
|
|
|
{
|
|
|
|
struct sg_table *sgt = dmab->private_data;
|
|
|
|
|
|
|
|
__sg_page_iter_start(piter, sgt->sgl, sgt->orig_nents,
|
|
|
|
offset >> PAGE_SHIFT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static dma_addr_t snd_dma_noncontig_get_addr(struct snd_dma_buffer *dmab,
|
|
|
|
size_t offset)
|
|
|
|
{
|
|
|
|
struct sg_dma_page_iter iter;
|
|
|
|
|
|
|
|
snd_dma_noncontig_iter_set(dmab, &iter.base, offset);
|
|
|
|
__sg_page_iter_dma_next(&iter);
|
|
|
|
return sg_page_iter_dma_address(&iter) + offset % PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct page *snd_dma_noncontig_get_page(struct snd_dma_buffer *dmab,
|
|
|
|
size_t offset)
|
|
|
|
{
|
|
|
|
struct sg_page_iter iter;
|
|
|
|
|
|
|
|
snd_dma_noncontig_iter_set(dmab, &iter, offset);
|
|
|
|
__sg_page_iter_next(&iter);
|
|
|
|
return sg_page_iter_page(&iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int
|
|
|
|
snd_dma_noncontig_get_chunk_size(struct snd_dma_buffer *dmab,
|
|
|
|
unsigned int ofs, unsigned int size)
|
|
|
|
{
|
|
|
|
struct sg_dma_page_iter iter;
|
|
|
|
unsigned int start, end;
|
|
|
|
unsigned long addr;
|
|
|
|
|
|
|
|
start = ALIGN_DOWN(ofs, PAGE_SIZE);
|
|
|
|
end = ofs + size - 1; /* the last byte address */
|
|
|
|
snd_dma_noncontig_iter_set(dmab, &iter.base, start);
|
|
|
|
if (!__sg_page_iter_dma_next(&iter))
|
|
|
|
return 0;
|
|
|
|
/* check page continuity */
|
|
|
|
addr = sg_page_iter_dma_address(&iter);
|
|
|
|
for (;;) {
|
|
|
|
start += PAGE_SIZE;
|
|
|
|
if (start > end)
|
|
|
|
break;
|
|
|
|
addr += PAGE_SIZE;
|
|
|
|
if (!__sg_page_iter_dma_next(&iter) ||
|
|
|
|
sg_page_iter_dma_address(&iter) != addr)
|
|
|
|
return start - ofs;
|
|
|
|
}
|
|
|
|
/* ok, all on continuous pages */
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
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>
2021-10-17 15:48:57 +08:00
|
|
|
static const struct snd_malloc_ops snd_dma_noncontig_ops = {
|
|
|
|
.alloc = snd_dma_noncontig_alloc,
|
|
|
|
.free = snd_dma_noncontig_free,
|
|
|
|
.mmap = snd_dma_noncontig_mmap,
|
|
|
|
.sync = snd_dma_noncontig_sync,
|
2021-11-08 23:10:59 +08:00
|
|
|
.get_addr = snd_dma_noncontig_get_addr,
|
|
|
|
.get_page = snd_dma_noncontig_get_page,
|
|
|
|
.get_chunk_size = snd_dma_noncontig_get_chunk_size,
|
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>
2021-10-17 15:48:57 +08:00
|
|
|
};
|
|
|
|
|
2021-11-16 15:33:58 +08:00
|
|
|
/* x86-specific SG-buffer with WC pages */
|
|
|
|
#ifdef CONFIG_SND_DMA_SGBUF
|
|
|
|
#define sg_wc_address(it) ((unsigned long)page_address(sg_page_iter_page(it)))
|
|
|
|
|
|
|
|
static void *snd_dma_sg_wc_alloc(struct snd_dma_buffer *dmab, size_t size)
|
|
|
|
{
|
|
|
|
void *p = snd_dma_noncontig_alloc(dmab, size);
|
|
|
|
struct sg_table *sgt = dmab->private_data;
|
|
|
|
struct sg_page_iter iter;
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
return NULL;
|
ALSA: memalloc: Add fallback SG-buffer allocations for x86
The recent change for memory allocator replaced the SG-buffer handling
helper for x86 with the standard non-contiguous page handler. This
works for most cases, but there is a corner case I obviously
overlooked, namely, the fallback of non-contiguous handler without
IOMMU. When the system runs without IOMMU, the core handler tries to
use the continuous pages with a single SGL entry. It works nicely for
most cases, but when the system memory gets fragmented, the large
allocation may fail frequently.
Ideally the non-contig handler could deal with the proper SG pages,
it's cumbersome to extend for now. As a workaround, here we add new
types for (minimalistic) SG allocations, instead, so that the
allocator falls back to those types automatically when the allocation
with the standard API failed.
BTW, one better (but pretty minor) improvement from the previous
SG-buffer code is that this provides the proper mmap support without
the PCM's page fault handling.
Fixes: 2c95b92ecd92 ("ALSA: memalloc: Unify x86 SG-buffer handling (take#3)")
BugLink: https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/2272
BugLink: https://bugzilla.suse.com/show_bug.cgi?id=1198248
Cc: <stable@vger.kernel.org>
Link: https://lore.kernel.org/r/20220413054808.7547-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2022-04-13 13:48:08 +08:00
|
|
|
if (dmab->dev.type != SNDRV_DMA_TYPE_DEV_WC_SG)
|
|
|
|
return p;
|
2021-11-16 15:33:58 +08:00
|
|
|
for_each_sgtable_page(sgt, &iter, 0)
|
|
|
|
set_memory_wc(sg_wc_address(&iter), 1);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void snd_dma_sg_wc_free(struct snd_dma_buffer *dmab)
|
|
|
|
{
|
|
|
|
struct sg_table *sgt = dmab->private_data;
|
|
|
|
struct sg_page_iter iter;
|
|
|
|
|
|
|
|
for_each_sgtable_page(sgt, &iter, 0)
|
|
|
|
set_memory_wb(sg_wc_address(&iter), 1);
|
|
|
|
snd_dma_noncontig_free(dmab);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int snd_dma_sg_wc_mmap(struct snd_dma_buffer *dmab,
|
|
|
|
struct vm_area_struct *area)
|
|
|
|
{
|
|
|
|
area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
|
|
|
|
return dma_mmap_noncontiguous(dmab->dev.dev, area,
|
|
|
|
dmab->bytes, dmab->private_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct snd_malloc_ops snd_dma_sg_wc_ops = {
|
|
|
|
.alloc = snd_dma_sg_wc_alloc,
|
|
|
|
.free = snd_dma_sg_wc_free,
|
|
|
|
.mmap = snd_dma_sg_wc_mmap,
|
|
|
|
.sync = snd_dma_noncontig_sync,
|
|
|
|
.get_addr = snd_dma_noncontig_get_addr,
|
|
|
|
.get_page = snd_dma_noncontig_get_page,
|
|
|
|
.get_chunk_size = snd_dma_noncontig_get_chunk_size,
|
|
|
|
};
|
ALSA: memalloc: Add fallback SG-buffer allocations for x86
The recent change for memory allocator replaced the SG-buffer handling
helper for x86 with the standard non-contiguous page handler. This
works for most cases, but there is a corner case I obviously
overlooked, namely, the fallback of non-contiguous handler without
IOMMU. When the system runs without IOMMU, the core handler tries to
use the continuous pages with a single SGL entry. It works nicely for
most cases, but when the system memory gets fragmented, the large
allocation may fail frequently.
Ideally the non-contig handler could deal with the proper SG pages,
it's cumbersome to extend for now. As a workaround, here we add new
types for (minimalistic) SG allocations, instead, so that the
allocator falls back to those types automatically when the allocation
with the standard API failed.
BTW, one better (but pretty minor) improvement from the previous
SG-buffer code is that this provides the proper mmap support without
the PCM's page fault handling.
Fixes: 2c95b92ecd92 ("ALSA: memalloc: Unify x86 SG-buffer handling (take#3)")
BugLink: https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/2272
BugLink: https://bugzilla.suse.com/show_bug.cgi?id=1198248
Cc: <stable@vger.kernel.org>
Link: https://lore.kernel.org/r/20220413054808.7547-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2022-04-13 13:48:08 +08:00
|
|
|
|
|
|
|
/* Fallback SG-buffer allocations for x86 */
|
|
|
|
struct snd_dma_sg_fallback {
|
|
|
|
size_t count;
|
|
|
|
struct page **pages;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void __snd_dma_sg_fallback_free(struct snd_dma_buffer *dmab,
|
|
|
|
struct snd_dma_sg_fallback *sgbuf)
|
|
|
|
{
|
2022-08-21 23:59:11 +08:00
|
|
|
bool wc = dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK;
|
ALSA: memalloc: Add fallback SG-buffer allocations for x86
The recent change for memory allocator replaced the SG-buffer handling
helper for x86 with the standard non-contiguous page handler. This
works for most cases, but there is a corner case I obviously
overlooked, namely, the fallback of non-contiguous handler without
IOMMU. When the system runs without IOMMU, the core handler tries to
use the continuous pages with a single SGL entry. It works nicely for
most cases, but when the system memory gets fragmented, the large
allocation may fail frequently.
Ideally the non-contig handler could deal with the proper SG pages,
it's cumbersome to extend for now. As a workaround, here we add new
types for (minimalistic) SG allocations, instead, so that the
allocator falls back to those types automatically when the allocation
with the standard API failed.
BTW, one better (but pretty minor) improvement from the previous
SG-buffer code is that this provides the proper mmap support without
the PCM's page fault handling.
Fixes: 2c95b92ecd92 ("ALSA: memalloc: Unify x86 SG-buffer handling (take#3)")
BugLink: https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/2272
BugLink: https://bugzilla.suse.com/show_bug.cgi?id=1198248
Cc: <stable@vger.kernel.org>
Link: https://lore.kernel.org/r/20220413054808.7547-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2022-04-13 13:48:08 +08:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < sgbuf->count && sgbuf->pages[i]; i++)
|
2022-08-23 19:57:39 +08:00
|
|
|
do_free_pages(page_address(sgbuf->pages[i]), PAGE_SIZE, wc);
|
ALSA: memalloc: Add fallback SG-buffer allocations for x86
The recent change for memory allocator replaced the SG-buffer handling
helper for x86 with the standard non-contiguous page handler. This
works for most cases, but there is a corner case I obviously
overlooked, namely, the fallback of non-contiguous handler without
IOMMU. When the system runs without IOMMU, the core handler tries to
use the continuous pages with a single SGL entry. It works nicely for
most cases, but when the system memory gets fragmented, the large
allocation may fail frequently.
Ideally the non-contig handler could deal with the proper SG pages,
it's cumbersome to extend for now. As a workaround, here we add new
types for (minimalistic) SG allocations, instead, so that the
allocator falls back to those types automatically when the allocation
with the standard API failed.
BTW, one better (but pretty minor) improvement from the previous
SG-buffer code is that this provides the proper mmap support without
the PCM's page fault handling.
Fixes: 2c95b92ecd92 ("ALSA: memalloc: Unify x86 SG-buffer handling (take#3)")
BugLink: https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/2272
BugLink: https://bugzilla.suse.com/show_bug.cgi?id=1198248
Cc: <stable@vger.kernel.org>
Link: https://lore.kernel.org/r/20220413054808.7547-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2022-04-13 13:48:08 +08:00
|
|
|
kvfree(sgbuf->pages);
|
|
|
|
kfree(sgbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *snd_dma_sg_fallback_alloc(struct snd_dma_buffer *dmab, size_t size)
|
|
|
|
{
|
|
|
|
struct snd_dma_sg_fallback *sgbuf;
|
2022-11-14 22:16:58 +08:00
|
|
|
struct page **pagep, *curp;
|
|
|
|
size_t chunk, npages;
|
|
|
|
dma_addr_t addr;
|
ALSA: memalloc: Add fallback SG-buffer allocations for x86
The recent change for memory allocator replaced the SG-buffer handling
helper for x86 with the standard non-contiguous page handler. This
works for most cases, but there is a corner case I obviously
overlooked, namely, the fallback of non-contiguous handler without
IOMMU. When the system runs without IOMMU, the core handler tries to
use the continuous pages with a single SGL entry. It works nicely for
most cases, but when the system memory gets fragmented, the large
allocation may fail frequently.
Ideally the non-contig handler could deal with the proper SG pages,
it's cumbersome to extend for now. As a workaround, here we add new
types for (minimalistic) SG allocations, instead, so that the
allocator falls back to those types automatically when the allocation
with the standard API failed.
BTW, one better (but pretty minor) improvement from the previous
SG-buffer code is that this provides the proper mmap support without
the PCM's page fault handling.
Fixes: 2c95b92ecd92 ("ALSA: memalloc: Unify x86 SG-buffer handling (take#3)")
BugLink: https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/2272
BugLink: https://bugzilla.suse.com/show_bug.cgi?id=1198248
Cc: <stable@vger.kernel.org>
Link: https://lore.kernel.org/r/20220413054808.7547-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2022-04-13 13:48:08 +08:00
|
|
|
void *p;
|
2022-08-21 23:59:11 +08:00
|
|
|
bool wc = dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK;
|
ALSA: memalloc: Add fallback SG-buffer allocations for x86
The recent change for memory allocator replaced the SG-buffer handling
helper for x86 with the standard non-contiguous page handler. This
works for most cases, but there is a corner case I obviously
overlooked, namely, the fallback of non-contiguous handler without
IOMMU. When the system runs without IOMMU, the core handler tries to
use the continuous pages with a single SGL entry. It works nicely for
most cases, but when the system memory gets fragmented, the large
allocation may fail frequently.
Ideally the non-contig handler could deal with the proper SG pages,
it's cumbersome to extend for now. As a workaround, here we add new
types for (minimalistic) SG allocations, instead, so that the
allocator falls back to those types automatically when the allocation
with the standard API failed.
BTW, one better (but pretty minor) improvement from the previous
SG-buffer code is that this provides the proper mmap support without
the PCM's page fault handling.
Fixes: 2c95b92ecd92 ("ALSA: memalloc: Unify x86 SG-buffer handling (take#3)")
BugLink: https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/2272
BugLink: https://bugzilla.suse.com/show_bug.cgi?id=1198248
Cc: <stable@vger.kernel.org>
Link: https://lore.kernel.org/r/20220413054808.7547-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2022-04-13 13:48:08 +08:00
|
|
|
|
|
|
|
sgbuf = kzalloc(sizeof(*sgbuf), GFP_KERNEL);
|
|
|
|
if (!sgbuf)
|
|
|
|
return NULL;
|
2022-11-14 22:16:58 +08:00
|
|
|
size = PAGE_ALIGN(size);
|
|
|
|
sgbuf->count = size >> PAGE_SHIFT;
|
|
|
|
sgbuf->pages = kvcalloc(sgbuf->count, sizeof(*sgbuf->pages), GFP_KERNEL);
|
|
|
|
if (!sgbuf->pages)
|
ALSA: memalloc: Add fallback SG-buffer allocations for x86
The recent change for memory allocator replaced the SG-buffer handling
helper for x86 with the standard non-contiguous page handler. This
works for most cases, but there is a corner case I obviously
overlooked, namely, the fallback of non-contiguous handler without
IOMMU. When the system runs without IOMMU, the core handler tries to
use the continuous pages with a single SGL entry. It works nicely for
most cases, but when the system memory gets fragmented, the large
allocation may fail frequently.
Ideally the non-contig handler could deal with the proper SG pages,
it's cumbersome to extend for now. As a workaround, here we add new
types for (minimalistic) SG allocations, instead, so that the
allocator falls back to those types automatically when the allocation
with the standard API failed.
BTW, one better (but pretty minor) improvement from the previous
SG-buffer code is that this provides the proper mmap support without
the PCM's page fault handling.
Fixes: 2c95b92ecd92 ("ALSA: memalloc: Unify x86 SG-buffer handling (take#3)")
BugLink: https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/2272
BugLink: https://bugzilla.suse.com/show_bug.cgi?id=1198248
Cc: <stable@vger.kernel.org>
Link: https://lore.kernel.org/r/20220413054808.7547-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2022-04-13 13:48:08 +08:00
|
|
|
goto error;
|
|
|
|
|
2022-11-14 22:16:58 +08:00
|
|
|
pagep = sgbuf->pages;
|
|
|
|
chunk = size;
|
|
|
|
while (size > 0) {
|
|
|
|
chunk = min(size, chunk);
|
|
|
|
p = do_alloc_pages(dmab->dev.dev, chunk, &addr, wc);
|
|
|
|
if (!p) {
|
|
|
|
if (chunk <= PAGE_SIZE)
|
|
|
|
goto error;
|
|
|
|
chunk >>= 1;
|
|
|
|
chunk = PAGE_SIZE << get_order(chunk);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
size -= chunk;
|
|
|
|
/* fill pages */
|
|
|
|
npages = chunk >> PAGE_SHIFT;
|
|
|
|
curp = virt_to_page(p);
|
|
|
|
while (npages--)
|
|
|
|
*pagep++ = curp++;
|
ALSA: memalloc: Add fallback SG-buffer allocations for x86
The recent change for memory allocator replaced the SG-buffer handling
helper for x86 with the standard non-contiguous page handler. This
works for most cases, but there is a corner case I obviously
overlooked, namely, the fallback of non-contiguous handler without
IOMMU. When the system runs without IOMMU, the core handler tries to
use the continuous pages with a single SGL entry. It works nicely for
most cases, but when the system memory gets fragmented, the large
allocation may fail frequently.
Ideally the non-contig handler could deal with the proper SG pages,
it's cumbersome to extend for now. As a workaround, here we add new
types for (minimalistic) SG allocations, instead, so that the
allocator falls back to those types automatically when the allocation
with the standard API failed.
BTW, one better (but pretty minor) improvement from the previous
SG-buffer code is that this provides the proper mmap support without
the PCM's page fault handling.
Fixes: 2c95b92ecd92 ("ALSA: memalloc: Unify x86 SG-buffer handling (take#3)")
BugLink: https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/2272
BugLink: https://bugzilla.suse.com/show_bug.cgi?id=1198248
Cc: <stable@vger.kernel.org>
Link: https://lore.kernel.org/r/20220413054808.7547-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2022-04-13 13:48:08 +08:00
|
|
|
}
|
|
|
|
|
2022-11-14 22:16:58 +08:00
|
|
|
p = vmap(sgbuf->pages, sgbuf->count, VM_MAP, PAGE_KERNEL);
|
ALSA: memalloc: Add fallback SG-buffer allocations for x86
The recent change for memory allocator replaced the SG-buffer handling
helper for x86 with the standard non-contiguous page handler. This
works for most cases, but there is a corner case I obviously
overlooked, namely, the fallback of non-contiguous handler without
IOMMU. When the system runs without IOMMU, the core handler tries to
use the continuous pages with a single SGL entry. It works nicely for
most cases, but when the system memory gets fragmented, the large
allocation may fail frequently.
Ideally the non-contig handler could deal with the proper SG pages,
it's cumbersome to extend for now. As a workaround, here we add new
types for (minimalistic) SG allocations, instead, so that the
allocator falls back to those types automatically when the allocation
with the standard API failed.
BTW, one better (but pretty minor) improvement from the previous
SG-buffer code is that this provides the proper mmap support without
the PCM's page fault handling.
Fixes: 2c95b92ecd92 ("ALSA: memalloc: Unify x86 SG-buffer handling (take#3)")
BugLink: https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/2272
BugLink: https://bugzilla.suse.com/show_bug.cgi?id=1198248
Cc: <stable@vger.kernel.org>
Link: https://lore.kernel.org/r/20220413054808.7547-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2022-04-13 13:48:08 +08:00
|
|
|
if (!p)
|
|
|
|
goto error;
|
|
|
|
dmab->private_data = sgbuf;
|
2022-09-06 17:03:19 +08:00
|
|
|
/* store the first page address for convenience */
|
|
|
|
dmab->addr = snd_sgbuf_get_addr(dmab, 0);
|
ALSA: memalloc: Add fallback SG-buffer allocations for x86
The recent change for memory allocator replaced the SG-buffer handling
helper for x86 with the standard non-contiguous page handler. This
works for most cases, but there is a corner case I obviously
overlooked, namely, the fallback of non-contiguous handler without
IOMMU. When the system runs without IOMMU, the core handler tries to
use the continuous pages with a single SGL entry. It works nicely for
most cases, but when the system memory gets fragmented, the large
allocation may fail frequently.
Ideally the non-contig handler could deal with the proper SG pages,
it's cumbersome to extend for now. As a workaround, here we add new
types for (minimalistic) SG allocations, instead, so that the
allocator falls back to those types automatically when the allocation
with the standard API failed.
BTW, one better (but pretty minor) improvement from the previous
SG-buffer code is that this provides the proper mmap support without
the PCM's page fault handling.
Fixes: 2c95b92ecd92 ("ALSA: memalloc: Unify x86 SG-buffer handling (take#3)")
BugLink: https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/2272
BugLink: https://bugzilla.suse.com/show_bug.cgi?id=1198248
Cc: <stable@vger.kernel.org>
Link: https://lore.kernel.org/r/20220413054808.7547-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2022-04-13 13:48:08 +08:00
|
|
|
return p;
|
|
|
|
|
|
|
|
error:
|
|
|
|
__snd_dma_sg_fallback_free(dmab, sgbuf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void snd_dma_sg_fallback_free(struct snd_dma_buffer *dmab)
|
|
|
|
{
|
|
|
|
vunmap(dmab->area);
|
|
|
|
__snd_dma_sg_fallback_free(dmab, dmab->private_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int snd_dma_sg_fallback_mmap(struct snd_dma_buffer *dmab,
|
|
|
|
struct vm_area_struct *area)
|
|
|
|
{
|
|
|
|
struct snd_dma_sg_fallback *sgbuf = dmab->private_data;
|
|
|
|
|
|
|
|
if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK)
|
|
|
|
area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
|
|
|
|
return vm_map_pages(area, sgbuf->pages, sgbuf->count);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct snd_malloc_ops snd_dma_sg_fallback_ops = {
|
|
|
|
.alloc = snd_dma_sg_fallback_alloc,
|
|
|
|
.free = snd_dma_sg_fallback_free,
|
|
|
|
.mmap = snd_dma_sg_fallback_mmap,
|
|
|
|
/* reuse vmalloc helpers */
|
|
|
|
.get_addr = snd_dma_vmalloc_get_addr,
|
|
|
|
.get_page = snd_dma_vmalloc_get_page,
|
|
|
|
.get_chunk_size = snd_dma_vmalloc_get_chunk_size,
|
|
|
|
};
|
2021-11-16 15:33:58 +08:00
|
|
|
#endif /* CONFIG_SND_DMA_SGBUF */
|
|
|
|
|
2021-10-17 15:48:58 +08:00
|
|
|
/*
|
|
|
|
* Non-coherent pages allocator
|
|
|
|
*/
|
|
|
|
static void *snd_dma_noncoherent_alloc(struct snd_dma_buffer *dmab, size_t size)
|
|
|
|
{
|
2022-02-10 20:33:43 +08:00
|
|
|
void *p;
|
|
|
|
|
|
|
|
p = dma_alloc_noncoherent(dmab->dev.dev, size, &dmab->addr,
|
2022-12-20 16:05:36 +08:00
|
|
|
dmab->dev.dir, DEFAULT_GFP);
|
2022-02-10 20:33:43 +08:00
|
|
|
if (p)
|
|
|
|
dmab->dev.need_sync = dma_need_sync(dmab->dev.dev, dmab->addr);
|
|
|
|
return p;
|
2021-10-17 15:48:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void snd_dma_noncoherent_free(struct snd_dma_buffer *dmab)
|
|
|
|
{
|
|
|
|
dma_free_noncoherent(dmab->dev.dev, dmab->bytes, dmab->area,
|
|
|
|
dmab->addr, dmab->dev.dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int snd_dma_noncoherent_mmap(struct snd_dma_buffer *dmab,
|
|
|
|
struct vm_area_struct *area)
|
|
|
|
{
|
|
|
|
area->vm_page_prot = vm_get_page_prot(area->vm_flags);
|
|
|
|
return dma_mmap_pages(dmab->dev.dev, area,
|
|
|
|
area->vm_end - area->vm_start,
|
|
|
|
virt_to_page(dmab->area));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void snd_dma_noncoherent_sync(struct snd_dma_buffer *dmab,
|
|
|
|
enum snd_dma_sync_mode mode)
|
|
|
|
{
|
|
|
|
if (mode == SNDRV_DMA_SYNC_CPU) {
|
|
|
|
if (dmab->dev.dir != DMA_TO_DEVICE)
|
|
|
|
dma_sync_single_for_cpu(dmab->dev.dev, dmab->addr,
|
|
|
|
dmab->bytes, dmab->dev.dir);
|
|
|
|
} else {
|
|
|
|
if (dmab->dev.dir != DMA_FROM_DEVICE)
|
|
|
|
dma_sync_single_for_device(dmab->dev.dev, dmab->addr,
|
|
|
|
dmab->bytes, dmab->dev.dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct snd_malloc_ops snd_dma_noncoherent_ops = {
|
|
|
|
.alloc = snd_dma_noncoherent_alloc,
|
|
|
|
.free = snd_dma_noncoherent_free,
|
|
|
|
.mmap = snd_dma_noncoherent_mmap,
|
|
|
|
.sync = snd_dma_noncoherent_sync,
|
|
|
|
};
|
|
|
|
|
2021-06-10 00:25:49 +08:00
|
|
|
#endif /* CONFIG_HAS_DMA */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Entry points
|
|
|
|
*/
|
2022-11-10 21:22:16 +08:00
|
|
|
static const struct snd_malloc_ops *snd_dma_ops[] = {
|
2021-06-10 00:25:49 +08:00
|
|
|
[SNDRV_DMA_TYPE_CONTINUOUS] = &snd_dma_continuous_ops,
|
|
|
|
[SNDRV_DMA_TYPE_VMALLOC] = &snd_dma_vmalloc_ops,
|
|
|
|
#ifdef CONFIG_HAS_DMA
|
|
|
|
[SNDRV_DMA_TYPE_DEV] = &snd_dma_dev_ops,
|
2021-08-02 15:28:04 +08:00
|
|
|
[SNDRV_DMA_TYPE_DEV_WC] = &snd_dma_wc_ops,
|
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>
2021-10-17 15:48:57 +08:00
|
|
|
[SNDRV_DMA_TYPE_NONCONTIG] = &snd_dma_noncontig_ops,
|
2021-10-17 15:48:58 +08:00
|
|
|
[SNDRV_DMA_TYPE_NONCOHERENT] = &snd_dma_noncoherent_ops,
|
2021-11-16 15:33:58 +08:00
|
|
|
#ifdef CONFIG_SND_DMA_SGBUF
|
|
|
|
[SNDRV_DMA_TYPE_DEV_WC_SG] = &snd_dma_sg_wc_ops,
|
|
|
|
#endif
|
2021-06-10 00:25:49 +08:00
|
|
|
#ifdef CONFIG_GENERIC_ALLOCATOR
|
|
|
|
[SNDRV_DMA_TYPE_DEV_IRAM] = &snd_dma_iram_ops,
|
|
|
|
#endif /* CONFIG_GENERIC_ALLOCATOR */
|
ALSA: memalloc: Add fallback SG-buffer allocations for x86
The recent change for memory allocator replaced the SG-buffer handling
helper for x86 with the standard non-contiguous page handler. This
works for most cases, but there is a corner case I obviously
overlooked, namely, the fallback of non-contiguous handler without
IOMMU. When the system runs without IOMMU, the core handler tries to
use the continuous pages with a single SGL entry. It works nicely for
most cases, but when the system memory gets fragmented, the large
allocation may fail frequently.
Ideally the non-contig handler could deal with the proper SG pages,
it's cumbersome to extend for now. As a workaround, here we add new
types for (minimalistic) SG allocations, instead, so that the
allocator falls back to those types automatically when the allocation
with the standard API failed.
BTW, one better (but pretty minor) improvement from the previous
SG-buffer code is that this provides the proper mmap support without
the PCM's page fault handling.
Fixes: 2c95b92ecd92 ("ALSA: memalloc: Unify x86 SG-buffer handling (take#3)")
BugLink: https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/2272
BugLink: https://bugzilla.suse.com/show_bug.cgi?id=1198248
Cc: <stable@vger.kernel.org>
Link: https://lore.kernel.org/r/20220413054808.7547-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2022-04-13 13:48:08 +08:00
|
|
|
#ifdef CONFIG_SND_DMA_SGBUF
|
|
|
|
[SNDRV_DMA_TYPE_DEV_SG_FALLBACK] = &snd_dma_sg_fallback_ops,
|
|
|
|
[SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK] = &snd_dma_sg_fallback_ops,
|
|
|
|
#endif
|
2021-06-10 00:25:49 +08:00
|
|
|
#endif /* CONFIG_HAS_DMA */
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct snd_malloc_ops *snd_dma_get_ops(struct snd_dma_buffer *dmab)
|
|
|
|
{
|
2021-11-05 18:21:03 +08:00
|
|
|
if (WARN_ON_ONCE(!dmab))
|
|
|
|
return NULL;
|
2021-06-10 00:25:49 +08:00
|
|
|
if (WARN_ON_ONCE(dmab->dev.type <= SNDRV_DMA_TYPE_UNKNOWN ||
|
2022-11-10 21:22:16 +08:00
|
|
|
dmab->dev.type >= ARRAY_SIZE(snd_dma_ops)))
|
2021-06-10 00:25:49 +08:00
|
|
|
return NULL;
|
2022-11-10 21:22:16 +08:00
|
|
|
return snd_dma_ops[dmab->dev.type];
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|