1 // SPDX-License-Identifier: GPL-2.0-or-later
6 * Generic memory allocators
9 #include <linux/slab.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/genalloc.h>
13 #include <linux/highmem.h>
14 #include <linux/vmalloc.h>
16 #include <asm/set_memory.h>
18 #include <sound/memalloc.h>
19 #include "memalloc_local.h"
21 static const struct snd_malloc_ops *snd_dma_get_ops(struct snd_dma_buffer *dmab);
23 /* a cast to gfp flag from the dev pointer; for CONTINUOUS and VMALLOC types */
24 static inline gfp_t snd_mem_get_gfp_flags(const struct snd_dma_buffer *dmab,
30 return (__force gfp_t)(unsigned long)dmab->dev.dev;
33 static void *__snd_dma_alloc_pages(struct snd_dma_buffer *dmab, size_t size)
35 const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
37 if (WARN_ON_ONCE(!ops || !ops->alloc))
39 return ops->alloc(dmab, size);
43 * snd_dma_alloc_dir_pages - allocate the buffer area according to the given
45 * @type: the DMA buffer type
46 * @device: the device pointer
48 * @size: the buffer size to allocate
49 * @dmab: buffer allocation record to store the allocated data
51 * Calls the memory-allocator function for the corresponding
54 * Return: Zero if the buffer with the given size is allocated successfully,
55 * otherwise a negative value on error.
57 int snd_dma_alloc_dir_pages(int type, struct device *device,
58 enum dma_data_direction dir, size_t size,
59 struct snd_dma_buffer *dmab)
66 size = PAGE_ALIGN(size);
67 dmab->dev.type = type;
68 dmab->dev.dev = device;
72 dmab->private_data = NULL;
73 dmab->area = __snd_dma_alloc_pages(dmab, size);
79 EXPORT_SYMBOL(snd_dma_alloc_dir_pages);
82 * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
83 * @type: the DMA buffer type
84 * @device: the device pointer
85 * @size: the buffer size to allocate
86 * @dmab: buffer allocation record to store the allocated data
88 * Calls the memory-allocator function for the corresponding
89 * buffer type. When no space is left, this function reduces the size and
90 * tries to allocate again. The size actually allocated is stored in
93 * Return: Zero if the buffer with the given size is allocated successfully,
94 * otherwise a negative value on error.
96 int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
97 struct snd_dma_buffer *dmab)
101 while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
104 if (size <= PAGE_SIZE)
107 size = PAGE_SIZE << get_order(size);
113 EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
116 * snd_dma_free_pages - release the allocated buffer
117 * @dmab: the buffer allocation record to release
119 * Releases the allocated buffer via snd_dma_alloc_pages().
121 void snd_dma_free_pages(struct snd_dma_buffer *dmab)
123 const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
125 if (ops && ops->free)
128 EXPORT_SYMBOL(snd_dma_free_pages);
130 /* called by devres */
131 static void __snd_release_pages(struct device *dev, void *res)
133 snd_dma_free_pages(res);
137 * snd_devm_alloc_dir_pages - allocate the buffer and manage with devres
138 * @dev: the device pointer
139 * @type: the DMA buffer type
140 * @dir: DMA direction
141 * @size: the buffer size to allocate
143 * Allocate buffer pages depending on the given type and manage using devres.
144 * The pages will be released automatically at the device removal.
146 * Unlike snd_dma_alloc_pages(), this function requires the real device pointer,
147 * hence it can't work with SNDRV_DMA_TYPE_CONTINUOUS or
148 * SNDRV_DMA_TYPE_VMALLOC type.
150 * The function returns the snd_dma_buffer object at success, or NULL if failed.
152 struct snd_dma_buffer *
153 snd_devm_alloc_dir_pages(struct device *dev, int type,
154 enum dma_data_direction dir, size_t size)
156 struct snd_dma_buffer *dmab;
159 if (WARN_ON(type == SNDRV_DMA_TYPE_CONTINUOUS ||
160 type == SNDRV_DMA_TYPE_VMALLOC))
163 dmab = devres_alloc(__snd_release_pages, sizeof(*dmab), GFP_KERNEL);
167 err = snd_dma_alloc_dir_pages(type, dev, dir, size, dmab);
173 devres_add(dev, dmab);
176 EXPORT_SYMBOL_GPL(snd_devm_alloc_dir_pages);
179 * snd_dma_buffer_mmap - perform mmap of the given DMA buffer
180 * @dmab: buffer allocation information
181 * @area: VM area information
183 int snd_dma_buffer_mmap(struct snd_dma_buffer *dmab,
184 struct vm_area_struct *area)
186 const struct snd_malloc_ops *ops;
190 ops = snd_dma_get_ops(dmab);
191 if (ops && ops->mmap)
192 return ops->mmap(dmab, area);
196 EXPORT_SYMBOL(snd_dma_buffer_mmap);
198 #ifdef CONFIG_HAS_DMA
200 * snd_dma_buffer_sync - sync DMA buffer between CPU and device
201 * @dmab: buffer allocation information
204 void snd_dma_buffer_sync(struct snd_dma_buffer *dmab,
205 enum snd_dma_sync_mode mode)
207 const struct snd_malloc_ops *ops;
209 if (!dmab || !dmab->dev.need_sync)
211 ops = snd_dma_get_ops(dmab);
212 if (ops && ops->sync)
213 ops->sync(dmab, mode);
215 EXPORT_SYMBOL_GPL(snd_dma_buffer_sync);
216 #endif /* CONFIG_HAS_DMA */
219 * snd_sgbuf_get_addr - return the physical address at the corresponding offset
220 * @dmab: buffer allocation information
221 * @offset: offset in the ring buffer
223 dma_addr_t snd_sgbuf_get_addr(struct snd_dma_buffer *dmab, size_t offset)
225 const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
227 if (ops && ops->get_addr)
228 return ops->get_addr(dmab, offset);
230 return dmab->addr + offset;
232 EXPORT_SYMBOL(snd_sgbuf_get_addr);
235 * snd_sgbuf_get_page - return the physical page at the corresponding offset
236 * @dmab: buffer allocation information
237 * @offset: offset in the ring buffer
239 struct page *snd_sgbuf_get_page(struct snd_dma_buffer *dmab, size_t offset)
241 const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
243 if (ops && ops->get_page)
244 return ops->get_page(dmab, offset);
246 return virt_to_page(dmab->area + offset);
248 EXPORT_SYMBOL(snd_sgbuf_get_page);
251 * snd_sgbuf_get_chunk_size - compute the max chunk size with continuous pages
253 * @dmab: buffer allocation information
254 * @ofs: offset in the ring buffer
255 * @size: the requested size
257 unsigned int snd_sgbuf_get_chunk_size(struct snd_dma_buffer *dmab,
258 unsigned int ofs, unsigned int size)
260 const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
262 if (ops && ops->get_chunk_size)
263 return ops->get_chunk_size(dmab, ofs, size);
267 EXPORT_SYMBOL(snd_sgbuf_get_chunk_size);
270 * Continuous pages allocator
272 static void *snd_dma_continuous_alloc(struct snd_dma_buffer *dmab, size_t size)
274 gfp_t gfp = snd_mem_get_gfp_flags(dmab, GFP_KERNEL);
275 void *p = alloc_pages_exact(size, gfp);
278 dmab->addr = page_to_phys(virt_to_page(p));
282 static void snd_dma_continuous_free(struct snd_dma_buffer *dmab)
284 free_pages_exact(dmab->area, dmab->bytes);
287 static int snd_dma_continuous_mmap(struct snd_dma_buffer *dmab,
288 struct vm_area_struct *area)
290 return remap_pfn_range(area, area->vm_start,
291 dmab->addr >> PAGE_SHIFT,
292 area->vm_end - area->vm_start,
296 static const struct snd_malloc_ops snd_dma_continuous_ops = {
297 .alloc = snd_dma_continuous_alloc,
298 .free = snd_dma_continuous_free,
299 .mmap = snd_dma_continuous_mmap,
305 static void *snd_dma_vmalloc_alloc(struct snd_dma_buffer *dmab, size_t size)
307 gfp_t gfp = snd_mem_get_gfp_flags(dmab, GFP_KERNEL | __GFP_HIGHMEM);
309 return __vmalloc(size, gfp);
312 static void snd_dma_vmalloc_free(struct snd_dma_buffer *dmab)
317 static int snd_dma_vmalloc_mmap(struct snd_dma_buffer *dmab,
318 struct vm_area_struct *area)
320 return remap_vmalloc_range(area, dmab->area, 0);
323 #define get_vmalloc_page_addr(dmab, offset) \
324 page_to_phys(vmalloc_to_page((dmab)->area + (offset)))
326 static dma_addr_t snd_dma_vmalloc_get_addr(struct snd_dma_buffer *dmab,
329 return get_vmalloc_page_addr(dmab, offset) + offset % PAGE_SIZE;
332 static struct page *snd_dma_vmalloc_get_page(struct snd_dma_buffer *dmab,
335 return vmalloc_to_page(dmab->area + offset);
339 snd_dma_vmalloc_get_chunk_size(struct snd_dma_buffer *dmab,
340 unsigned int ofs, unsigned int size)
342 unsigned int start, end;
345 start = ALIGN_DOWN(ofs, PAGE_SIZE);
346 end = ofs + size - 1; /* the last byte address */
347 /* check page continuity */
348 addr = get_vmalloc_page_addr(dmab, start);
354 if (get_vmalloc_page_addr(dmab, start) != addr)
357 /* ok, all on continuous pages */
361 static const struct snd_malloc_ops snd_dma_vmalloc_ops = {
362 .alloc = snd_dma_vmalloc_alloc,
363 .free = snd_dma_vmalloc_free,
364 .mmap = snd_dma_vmalloc_mmap,
365 .get_addr = snd_dma_vmalloc_get_addr,
366 .get_page = snd_dma_vmalloc_get_page,
367 .get_chunk_size = snd_dma_vmalloc_get_chunk_size,
370 #ifdef CONFIG_HAS_DMA
374 #ifdef CONFIG_GENERIC_ALLOCATOR
375 static void *snd_dma_iram_alloc(struct snd_dma_buffer *dmab, size_t size)
377 struct device *dev = dmab->dev.dev;
378 struct gen_pool *pool;
382 pool = of_gen_pool_get(dev->of_node, "iram", 0);
383 /* Assign the pool into private_data field */
384 dmab->private_data = pool;
386 p = gen_pool_dma_alloc_align(pool, size, &dmab->addr, PAGE_SIZE);
391 /* Internal memory might have limited size and no enough space,
392 * so if we fail to malloc, try to fetch memory traditionally.
394 dmab->dev.type = SNDRV_DMA_TYPE_DEV;
395 return __snd_dma_alloc_pages(dmab, size);
398 static void snd_dma_iram_free(struct snd_dma_buffer *dmab)
400 struct gen_pool *pool = dmab->private_data;
402 if (pool && dmab->area)
403 gen_pool_free(pool, (unsigned long)dmab->area, dmab->bytes);
406 static int snd_dma_iram_mmap(struct snd_dma_buffer *dmab,
407 struct vm_area_struct *area)
409 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
410 return remap_pfn_range(area, area->vm_start,
411 dmab->addr >> PAGE_SHIFT,
412 area->vm_end - area->vm_start,
416 static const struct snd_malloc_ops snd_dma_iram_ops = {
417 .alloc = snd_dma_iram_alloc,
418 .free = snd_dma_iram_free,
419 .mmap = snd_dma_iram_mmap,
421 #endif /* CONFIG_GENERIC_ALLOCATOR */
423 #define DEFAULT_GFP \
425 __GFP_COMP | /* compound page lets parts be mapped */ \
426 __GFP_NORETRY | /* don't trigger OOM-killer */ \
427 __GFP_NOWARN) /* no stack trace print - this call is non-critical */
430 * Coherent device pages allocator
432 static void *snd_dma_dev_alloc(struct snd_dma_buffer *dmab, size_t size)
434 return dma_alloc_coherent(dmab->dev.dev, size, &dmab->addr, DEFAULT_GFP);
437 static void snd_dma_dev_free(struct snd_dma_buffer *dmab)
439 dma_free_coherent(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
442 static int snd_dma_dev_mmap(struct snd_dma_buffer *dmab,
443 struct vm_area_struct *area)
445 return dma_mmap_coherent(dmab->dev.dev, area,
446 dmab->area, dmab->addr, dmab->bytes);
449 static const struct snd_malloc_ops snd_dma_dev_ops = {
450 .alloc = snd_dma_dev_alloc,
451 .free = snd_dma_dev_free,
452 .mmap = snd_dma_dev_mmap,
456 * Write-combined pages
458 static void *snd_dma_wc_alloc(struct snd_dma_buffer *dmab, size_t size)
460 return dma_alloc_wc(dmab->dev.dev, size, &dmab->addr, DEFAULT_GFP);
463 static void snd_dma_wc_free(struct snd_dma_buffer *dmab)
465 dma_free_wc(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
468 static int snd_dma_wc_mmap(struct snd_dma_buffer *dmab,
469 struct vm_area_struct *area)
471 return dma_mmap_wc(dmab->dev.dev, area,
472 dmab->area, dmab->addr, dmab->bytes);
475 static const struct snd_malloc_ops snd_dma_wc_ops = {
476 .alloc = snd_dma_wc_alloc,
477 .free = snd_dma_wc_free,
478 .mmap = snd_dma_wc_mmap,
481 #ifdef CONFIG_SND_DMA_SGBUF
482 static void *snd_dma_sg_fallback_alloc(struct snd_dma_buffer *dmab, size_t size);
486 * Non-contiguous pages allocator
488 static void *snd_dma_noncontig_alloc(struct snd_dma_buffer *dmab, size_t size)
490 struct sg_table *sgt;
493 sgt = dma_alloc_noncontiguous(dmab->dev.dev, size, dmab->dev.dir,
496 #ifdef CONFIG_SND_DMA_SGBUF
497 if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG)
498 dmab->dev.type = SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK;
500 dmab->dev.type = SNDRV_DMA_TYPE_DEV_SG_FALLBACK;
501 return snd_dma_sg_fallback_alloc(dmab, size);
507 dmab->dev.need_sync = dma_need_sync(dmab->dev.dev,
508 sg_dma_address(sgt->sgl));
509 p = dma_vmap_noncontiguous(dmab->dev.dev, size, sgt);
511 dmab->private_data = sgt;
513 dma_free_noncontiguous(dmab->dev.dev, size, sgt, dmab->dev.dir);
517 static void snd_dma_noncontig_free(struct snd_dma_buffer *dmab)
519 dma_vunmap_noncontiguous(dmab->dev.dev, dmab->area);
520 dma_free_noncontiguous(dmab->dev.dev, dmab->bytes, dmab->private_data,
524 static int snd_dma_noncontig_mmap(struct snd_dma_buffer *dmab,
525 struct vm_area_struct *area)
527 return dma_mmap_noncontiguous(dmab->dev.dev, area,
528 dmab->bytes, dmab->private_data);
531 static void snd_dma_noncontig_sync(struct snd_dma_buffer *dmab,
532 enum snd_dma_sync_mode mode)
534 if (mode == SNDRV_DMA_SYNC_CPU) {
535 if (dmab->dev.dir == DMA_TO_DEVICE)
537 invalidate_kernel_vmap_range(dmab->area, dmab->bytes);
538 dma_sync_sgtable_for_cpu(dmab->dev.dev, dmab->private_data,
541 if (dmab->dev.dir == DMA_FROM_DEVICE)
543 flush_kernel_vmap_range(dmab->area, dmab->bytes);
544 dma_sync_sgtable_for_device(dmab->dev.dev, dmab->private_data,
549 static inline void snd_dma_noncontig_iter_set(struct snd_dma_buffer *dmab,
550 struct sg_page_iter *piter,
553 struct sg_table *sgt = dmab->private_data;
555 __sg_page_iter_start(piter, sgt->sgl, sgt->orig_nents,
556 offset >> PAGE_SHIFT);
559 static dma_addr_t snd_dma_noncontig_get_addr(struct snd_dma_buffer *dmab,
562 struct sg_dma_page_iter iter;
564 snd_dma_noncontig_iter_set(dmab, &iter.base, offset);
565 __sg_page_iter_dma_next(&iter);
566 return sg_page_iter_dma_address(&iter) + offset % PAGE_SIZE;
569 static struct page *snd_dma_noncontig_get_page(struct snd_dma_buffer *dmab,
572 struct sg_page_iter iter;
574 snd_dma_noncontig_iter_set(dmab, &iter, offset);
575 __sg_page_iter_next(&iter);
576 return sg_page_iter_page(&iter);
580 snd_dma_noncontig_get_chunk_size(struct snd_dma_buffer *dmab,
581 unsigned int ofs, unsigned int size)
583 struct sg_dma_page_iter iter;
584 unsigned int start, end;
587 start = ALIGN_DOWN(ofs, PAGE_SIZE);
588 end = ofs + size - 1; /* the last byte address */
589 snd_dma_noncontig_iter_set(dmab, &iter.base, start);
590 if (!__sg_page_iter_dma_next(&iter))
592 /* check page continuity */
593 addr = sg_page_iter_dma_address(&iter);
599 if (!__sg_page_iter_dma_next(&iter) ||
600 sg_page_iter_dma_address(&iter) != addr)
603 /* ok, all on continuous pages */
607 static const struct snd_malloc_ops snd_dma_noncontig_ops = {
608 .alloc = snd_dma_noncontig_alloc,
609 .free = snd_dma_noncontig_free,
610 .mmap = snd_dma_noncontig_mmap,
611 .sync = snd_dma_noncontig_sync,
612 .get_addr = snd_dma_noncontig_get_addr,
613 .get_page = snd_dma_noncontig_get_page,
614 .get_chunk_size = snd_dma_noncontig_get_chunk_size,
617 /* x86-specific SG-buffer with WC pages */
618 #ifdef CONFIG_SND_DMA_SGBUF
619 #define sg_wc_address(it) ((unsigned long)page_address(sg_page_iter_page(it)))
621 static void *snd_dma_sg_wc_alloc(struct snd_dma_buffer *dmab, size_t size)
623 void *p = snd_dma_noncontig_alloc(dmab, size);
624 struct sg_table *sgt = dmab->private_data;
625 struct sg_page_iter iter;
629 if (dmab->dev.type != SNDRV_DMA_TYPE_DEV_WC_SG)
631 for_each_sgtable_page(sgt, &iter, 0)
632 set_memory_wc(sg_wc_address(&iter), 1);
636 static void snd_dma_sg_wc_free(struct snd_dma_buffer *dmab)
638 struct sg_table *sgt = dmab->private_data;
639 struct sg_page_iter iter;
641 for_each_sgtable_page(sgt, &iter, 0)
642 set_memory_wb(sg_wc_address(&iter), 1);
643 snd_dma_noncontig_free(dmab);
646 static int snd_dma_sg_wc_mmap(struct snd_dma_buffer *dmab,
647 struct vm_area_struct *area)
649 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
650 return dma_mmap_noncontiguous(dmab->dev.dev, area,
651 dmab->bytes, dmab->private_data);
654 static const struct snd_malloc_ops snd_dma_sg_wc_ops = {
655 .alloc = snd_dma_sg_wc_alloc,
656 .free = snd_dma_sg_wc_free,
657 .mmap = snd_dma_sg_wc_mmap,
658 .sync = snd_dma_noncontig_sync,
659 .get_addr = snd_dma_noncontig_get_addr,
660 .get_page = snd_dma_noncontig_get_page,
661 .get_chunk_size = snd_dma_noncontig_get_chunk_size,
664 /* Fallback SG-buffer allocations for x86 */
665 struct snd_dma_sg_fallback {
671 static void __snd_dma_sg_fallback_free(struct snd_dma_buffer *dmab,
672 struct snd_dma_sg_fallback *sgbuf)
676 if (sgbuf->count && dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK)
677 set_pages_array_wb(sgbuf->pages, sgbuf->count);
678 for (i = 0; i < sgbuf->count && sgbuf->pages[i]; i++)
679 dma_free_coherent(dmab->dev.dev, PAGE_SIZE,
680 page_address(sgbuf->pages[i]),
682 kvfree(sgbuf->pages);
683 kvfree(sgbuf->addrs);
687 static void *snd_dma_sg_fallback_alloc(struct snd_dma_buffer *dmab, size_t size)
689 struct snd_dma_sg_fallback *sgbuf;
694 sgbuf = kzalloc(sizeof(*sgbuf), GFP_KERNEL);
697 count = PAGE_ALIGN(size) >> PAGE_SHIFT;
698 pages = kvcalloc(count, sizeof(*pages), GFP_KERNEL);
701 sgbuf->pages = pages;
702 sgbuf->addrs = kvcalloc(count, sizeof(*sgbuf->addrs), GFP_KERNEL);
706 for (i = 0; i < count; sgbuf->count++, i++) {
707 p = dma_alloc_coherent(dmab->dev.dev, PAGE_SIZE,
708 &sgbuf->addrs[i], DEFAULT_GFP);
711 sgbuf->pages[i] = virt_to_page(p);
714 if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK)
715 set_pages_array_wc(pages, count);
716 p = vmap(pages, count, VM_MAP, PAGE_KERNEL);
719 dmab->private_data = sgbuf;
723 __snd_dma_sg_fallback_free(dmab, sgbuf);
727 static void snd_dma_sg_fallback_free(struct snd_dma_buffer *dmab)
730 __snd_dma_sg_fallback_free(dmab, dmab->private_data);
733 static int snd_dma_sg_fallback_mmap(struct snd_dma_buffer *dmab,
734 struct vm_area_struct *area)
736 struct snd_dma_sg_fallback *sgbuf = dmab->private_data;
738 if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK)
739 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
740 return vm_map_pages(area, sgbuf->pages, sgbuf->count);
743 static const struct snd_malloc_ops snd_dma_sg_fallback_ops = {
744 .alloc = snd_dma_sg_fallback_alloc,
745 .free = snd_dma_sg_fallback_free,
746 .mmap = snd_dma_sg_fallback_mmap,
747 /* reuse vmalloc helpers */
748 .get_addr = snd_dma_vmalloc_get_addr,
749 .get_page = snd_dma_vmalloc_get_page,
750 .get_chunk_size = snd_dma_vmalloc_get_chunk_size,
752 #endif /* CONFIG_SND_DMA_SGBUF */
755 * Non-coherent pages allocator
757 static void *snd_dma_noncoherent_alloc(struct snd_dma_buffer *dmab, size_t size)
761 p = dma_alloc_noncoherent(dmab->dev.dev, size, &dmab->addr,
762 dmab->dev.dir, DEFAULT_GFP);
764 dmab->dev.need_sync = dma_need_sync(dmab->dev.dev, dmab->addr);
768 static void snd_dma_noncoherent_free(struct snd_dma_buffer *dmab)
770 dma_free_noncoherent(dmab->dev.dev, dmab->bytes, dmab->area,
771 dmab->addr, dmab->dev.dir);
774 static int snd_dma_noncoherent_mmap(struct snd_dma_buffer *dmab,
775 struct vm_area_struct *area)
777 area->vm_page_prot = vm_get_page_prot(area->vm_flags);
778 return dma_mmap_pages(dmab->dev.dev, area,
779 area->vm_end - area->vm_start,
780 virt_to_page(dmab->area));
783 static void snd_dma_noncoherent_sync(struct snd_dma_buffer *dmab,
784 enum snd_dma_sync_mode mode)
786 if (mode == SNDRV_DMA_SYNC_CPU) {
787 if (dmab->dev.dir != DMA_TO_DEVICE)
788 dma_sync_single_for_cpu(dmab->dev.dev, dmab->addr,
789 dmab->bytes, dmab->dev.dir);
791 if (dmab->dev.dir != DMA_FROM_DEVICE)
792 dma_sync_single_for_device(dmab->dev.dev, dmab->addr,
793 dmab->bytes, dmab->dev.dir);
797 static const struct snd_malloc_ops snd_dma_noncoherent_ops = {
798 .alloc = snd_dma_noncoherent_alloc,
799 .free = snd_dma_noncoherent_free,
800 .mmap = snd_dma_noncoherent_mmap,
801 .sync = snd_dma_noncoherent_sync,
804 #endif /* CONFIG_HAS_DMA */
809 static const struct snd_malloc_ops *dma_ops[] = {
810 [SNDRV_DMA_TYPE_CONTINUOUS] = &snd_dma_continuous_ops,
811 [SNDRV_DMA_TYPE_VMALLOC] = &snd_dma_vmalloc_ops,
812 #ifdef CONFIG_HAS_DMA
813 [SNDRV_DMA_TYPE_DEV] = &snd_dma_dev_ops,
814 [SNDRV_DMA_TYPE_DEV_WC] = &snd_dma_wc_ops,
815 [SNDRV_DMA_TYPE_NONCONTIG] = &snd_dma_noncontig_ops,
816 [SNDRV_DMA_TYPE_NONCOHERENT] = &snd_dma_noncoherent_ops,
817 #ifdef CONFIG_SND_DMA_SGBUF
818 [SNDRV_DMA_TYPE_DEV_WC_SG] = &snd_dma_sg_wc_ops,
820 #ifdef CONFIG_GENERIC_ALLOCATOR
821 [SNDRV_DMA_TYPE_DEV_IRAM] = &snd_dma_iram_ops,
822 #endif /* CONFIG_GENERIC_ALLOCATOR */
823 #ifdef CONFIG_SND_DMA_SGBUF
824 [SNDRV_DMA_TYPE_DEV_SG_FALLBACK] = &snd_dma_sg_fallback_ops,
825 [SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK] = &snd_dma_sg_fallback_ops,
827 #endif /* CONFIG_HAS_DMA */
830 static const struct snd_malloc_ops *snd_dma_get_ops(struct snd_dma_buffer *dmab)
832 if (WARN_ON_ONCE(!dmab))
834 if (WARN_ON_ONCE(dmab->dev.type <= SNDRV_DMA_TYPE_UNKNOWN ||
835 dmab->dev.type >= ARRAY_SIZE(dma_ops)))
837 return dma_ops[dmab->dev.type];