1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4 * Takashi Iwai <tiwai@suse.de>
6 * Generic memory allocators
9 #include <linux/slab.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/dma-map-ops.h>
13 #include <linux/genalloc.h>
14 #include <linux/highmem.h>
15 #include <linux/vmalloc.h>
17 #include <asm/set_memory.h>
19 #include <sound/memalloc.h>
20 #include "memalloc_local.h"
24 __GFP_RETRY_MAYFAIL | /* don't trigger OOM-killer */ \
25 __GFP_NOWARN) /* no stack trace print - this call is non-critical */
27 static const struct snd_malloc_ops *snd_dma_get_ops(struct snd_dma_buffer *dmab);
29 #ifdef CONFIG_SND_DMA_SGBUF
30 static void *snd_dma_sg_fallback_alloc(struct snd_dma_buffer *dmab, size_t size);
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 * Return: 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 * Return: zero if successful, or a negative error code
185 int snd_dma_buffer_mmap(struct snd_dma_buffer *dmab,
186 struct vm_area_struct *area)
188 const struct snd_malloc_ops *ops;
192 ops = snd_dma_get_ops(dmab);
193 if (ops && ops->mmap)
194 return ops->mmap(dmab, area);
198 EXPORT_SYMBOL(snd_dma_buffer_mmap);
200 #ifdef CONFIG_HAS_DMA
202 * snd_dma_buffer_sync - sync DMA buffer between CPU and device
203 * @dmab: buffer allocation information
206 void snd_dma_buffer_sync(struct snd_dma_buffer *dmab,
207 enum snd_dma_sync_mode mode)
209 const struct snd_malloc_ops *ops;
211 if (!dmab || !dmab->dev.need_sync)
213 ops = snd_dma_get_ops(dmab);
214 if (ops && ops->sync)
215 ops->sync(dmab, mode);
217 EXPORT_SYMBOL_GPL(snd_dma_buffer_sync);
218 #endif /* CONFIG_HAS_DMA */
221 * snd_sgbuf_get_addr - return the physical address at the corresponding offset
222 * @dmab: buffer allocation information
223 * @offset: offset in the ring buffer
225 * Return: the physical address
227 dma_addr_t snd_sgbuf_get_addr(struct snd_dma_buffer *dmab, size_t offset)
229 const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
231 if (ops && ops->get_addr)
232 return ops->get_addr(dmab, offset);
234 return dmab->addr + offset;
236 EXPORT_SYMBOL(snd_sgbuf_get_addr);
239 * snd_sgbuf_get_page - return the physical page at the corresponding offset
240 * @dmab: buffer allocation information
241 * @offset: offset in the ring buffer
243 * Return: the page pointer
245 struct page *snd_sgbuf_get_page(struct snd_dma_buffer *dmab, size_t offset)
247 const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
249 if (ops && ops->get_page)
250 return ops->get_page(dmab, offset);
252 return virt_to_page(dmab->area + offset);
254 EXPORT_SYMBOL(snd_sgbuf_get_page);
257 * snd_sgbuf_get_chunk_size - compute the max chunk size with continuous pages
259 * @dmab: buffer allocation information
260 * @ofs: offset in the ring buffer
261 * @size: the requested size
263 * Return: the chunk size
265 unsigned int snd_sgbuf_get_chunk_size(struct snd_dma_buffer *dmab,
266 unsigned int ofs, unsigned int size)
268 const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
270 if (ops && ops->get_chunk_size)
271 return ops->get_chunk_size(dmab, ofs, size);
275 EXPORT_SYMBOL(snd_sgbuf_get_chunk_size);
278 * Continuous pages allocator
280 static void *do_alloc_pages(struct device *dev, size_t size, dma_addr_t *addr,
284 gfp_t gfp = GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN;
287 p = alloc_pages_exact(size, gfp);
290 *addr = page_to_phys(virt_to_page(p));
293 if ((*addr + size - 1) & ~dev->coherent_dma_mask) {
294 if (IS_ENABLED(CONFIG_ZONE_DMA32) && !(gfp & GFP_DMA32)) {
298 if (IS_ENABLED(CONFIG_ZONE_DMA) && !(gfp & GFP_DMA)) {
299 gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
305 set_memory_wc((unsigned long)(p), size >> PAGE_SHIFT);
310 static void do_free_pages(void *p, size_t size, bool wc)
314 set_memory_wb((unsigned long)(p), size >> PAGE_SHIFT);
316 free_pages_exact(p, size);
320 static void *snd_dma_continuous_alloc(struct snd_dma_buffer *dmab, size_t size)
322 return do_alloc_pages(dmab->dev.dev, size, &dmab->addr, false);
325 static void snd_dma_continuous_free(struct snd_dma_buffer *dmab)
327 do_free_pages(dmab->area, dmab->bytes, false);
330 static int snd_dma_continuous_mmap(struct snd_dma_buffer *dmab,
331 struct vm_area_struct *area)
333 return remap_pfn_range(area, area->vm_start,
334 dmab->addr >> PAGE_SHIFT,
335 area->vm_end - area->vm_start,
339 static const struct snd_malloc_ops snd_dma_continuous_ops = {
340 .alloc = snd_dma_continuous_alloc,
341 .free = snd_dma_continuous_free,
342 .mmap = snd_dma_continuous_mmap,
348 static void *snd_dma_vmalloc_alloc(struct snd_dma_buffer *dmab, size_t size)
350 return vmalloc(size);
353 static void snd_dma_vmalloc_free(struct snd_dma_buffer *dmab)
358 static int snd_dma_vmalloc_mmap(struct snd_dma_buffer *dmab,
359 struct vm_area_struct *area)
361 return remap_vmalloc_range(area, dmab->area, 0);
364 #define get_vmalloc_page_addr(dmab, offset) \
365 page_to_phys(vmalloc_to_page((dmab)->area + (offset)))
367 static dma_addr_t snd_dma_vmalloc_get_addr(struct snd_dma_buffer *dmab,
370 return get_vmalloc_page_addr(dmab, offset) + offset % PAGE_SIZE;
373 static struct page *snd_dma_vmalloc_get_page(struct snd_dma_buffer *dmab,
376 return vmalloc_to_page(dmab->area + offset);
380 snd_dma_vmalloc_get_chunk_size(struct snd_dma_buffer *dmab,
381 unsigned int ofs, unsigned int size)
383 unsigned int start, end;
386 start = ALIGN_DOWN(ofs, PAGE_SIZE);
387 end = ofs + size - 1; /* the last byte address */
388 /* check page continuity */
389 addr = get_vmalloc_page_addr(dmab, start);
395 if (get_vmalloc_page_addr(dmab, start) != addr)
398 /* ok, all on continuous pages */
402 static const struct snd_malloc_ops snd_dma_vmalloc_ops = {
403 .alloc = snd_dma_vmalloc_alloc,
404 .free = snd_dma_vmalloc_free,
405 .mmap = snd_dma_vmalloc_mmap,
406 .get_addr = snd_dma_vmalloc_get_addr,
407 .get_page = snd_dma_vmalloc_get_page,
408 .get_chunk_size = snd_dma_vmalloc_get_chunk_size,
411 #ifdef CONFIG_HAS_DMA
415 #ifdef CONFIG_GENERIC_ALLOCATOR
416 static void *snd_dma_iram_alloc(struct snd_dma_buffer *dmab, size_t size)
418 struct device *dev = dmab->dev.dev;
419 struct gen_pool *pool;
423 pool = of_gen_pool_get(dev->of_node, "iram", 0);
424 /* Assign the pool into private_data field */
425 dmab->private_data = pool;
427 p = gen_pool_dma_alloc_align(pool, size, &dmab->addr, PAGE_SIZE);
432 /* Internal memory might have limited size and no enough space,
433 * so if we fail to malloc, try to fetch memory traditionally.
435 dmab->dev.type = SNDRV_DMA_TYPE_DEV;
436 return __snd_dma_alloc_pages(dmab, size);
439 static void snd_dma_iram_free(struct snd_dma_buffer *dmab)
441 struct gen_pool *pool = dmab->private_data;
443 if (pool && dmab->area)
444 gen_pool_free(pool, (unsigned long)dmab->area, dmab->bytes);
447 static int snd_dma_iram_mmap(struct snd_dma_buffer *dmab,
448 struct vm_area_struct *area)
450 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
451 return remap_pfn_range(area, area->vm_start,
452 dmab->addr >> PAGE_SHIFT,
453 area->vm_end - area->vm_start,
457 static const struct snd_malloc_ops snd_dma_iram_ops = {
458 .alloc = snd_dma_iram_alloc,
459 .free = snd_dma_iram_free,
460 .mmap = snd_dma_iram_mmap,
462 #endif /* CONFIG_GENERIC_ALLOCATOR */
465 * Coherent device pages allocator
467 static void *snd_dma_dev_alloc(struct snd_dma_buffer *dmab, size_t size)
469 return dma_alloc_coherent(dmab->dev.dev, size, &dmab->addr, DEFAULT_GFP);
472 static void snd_dma_dev_free(struct snd_dma_buffer *dmab)
474 dma_free_coherent(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
477 static int snd_dma_dev_mmap(struct snd_dma_buffer *dmab,
478 struct vm_area_struct *area)
480 return dma_mmap_coherent(dmab->dev.dev, area,
481 dmab->area, dmab->addr, dmab->bytes);
484 static const struct snd_malloc_ops snd_dma_dev_ops = {
485 .alloc = snd_dma_dev_alloc,
486 .free = snd_dma_dev_free,
487 .mmap = snd_dma_dev_mmap,
491 * Write-combined pages
493 /* x86-specific allocations */
494 #ifdef CONFIG_SND_DMA_SGBUF
495 static void *snd_dma_wc_alloc(struct snd_dma_buffer *dmab, size_t size)
497 return do_alloc_pages(dmab->dev.dev, size, &dmab->addr, true);
500 static void snd_dma_wc_free(struct snd_dma_buffer *dmab)
502 do_free_pages(dmab->area, dmab->bytes, true);
505 static int snd_dma_wc_mmap(struct snd_dma_buffer *dmab,
506 struct vm_area_struct *area)
508 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
509 return snd_dma_continuous_mmap(dmab, area);
512 static void *snd_dma_wc_alloc(struct snd_dma_buffer *dmab, size_t size)
514 return dma_alloc_wc(dmab->dev.dev, size, &dmab->addr, DEFAULT_GFP);
517 static void snd_dma_wc_free(struct snd_dma_buffer *dmab)
519 dma_free_wc(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
522 static int snd_dma_wc_mmap(struct snd_dma_buffer *dmab,
523 struct vm_area_struct *area)
525 return dma_mmap_wc(dmab->dev.dev, area,
526 dmab->area, dmab->addr, dmab->bytes);
528 #endif /* CONFIG_SND_DMA_SGBUF */
530 static const struct snd_malloc_ops snd_dma_wc_ops = {
531 .alloc = snd_dma_wc_alloc,
532 .free = snd_dma_wc_free,
533 .mmap = snd_dma_wc_mmap,
537 * Non-contiguous pages allocator
539 static void *snd_dma_noncontig_alloc(struct snd_dma_buffer *dmab, size_t size)
541 struct sg_table *sgt;
544 #ifdef CONFIG_SND_DMA_SGBUF
545 if (cpu_feature_enabled(X86_FEATURE_XENPV))
546 return snd_dma_sg_fallback_alloc(dmab, size);
548 sgt = dma_alloc_noncontiguous(dmab->dev.dev, size, dmab->dev.dir,
550 #ifdef CONFIG_SND_DMA_SGBUF
551 if (!sgt && !get_dma_ops(dmab->dev.dev))
552 return snd_dma_sg_fallback_alloc(dmab, size);
557 dmab->dev.need_sync = dma_need_sync(dmab->dev.dev,
558 sg_dma_address(sgt->sgl));
559 p = dma_vmap_noncontiguous(dmab->dev.dev, size, sgt);
561 dmab->private_data = sgt;
562 /* store the first page address for convenience */
563 dmab->addr = snd_sgbuf_get_addr(dmab, 0);
565 dma_free_noncontiguous(dmab->dev.dev, size, sgt, dmab->dev.dir);
570 static void snd_dma_noncontig_free(struct snd_dma_buffer *dmab)
572 dma_vunmap_noncontiguous(dmab->dev.dev, dmab->area);
573 dma_free_noncontiguous(dmab->dev.dev, dmab->bytes, dmab->private_data,
577 static int snd_dma_noncontig_mmap(struct snd_dma_buffer *dmab,
578 struct vm_area_struct *area)
580 return dma_mmap_noncontiguous(dmab->dev.dev, area,
581 dmab->bytes, dmab->private_data);
584 static void snd_dma_noncontig_sync(struct snd_dma_buffer *dmab,
585 enum snd_dma_sync_mode mode)
587 if (mode == SNDRV_DMA_SYNC_CPU) {
588 if (dmab->dev.dir == DMA_TO_DEVICE)
590 invalidate_kernel_vmap_range(dmab->area, dmab->bytes);
591 dma_sync_sgtable_for_cpu(dmab->dev.dev, dmab->private_data,
594 if (dmab->dev.dir == DMA_FROM_DEVICE)
596 flush_kernel_vmap_range(dmab->area, dmab->bytes);
597 dma_sync_sgtable_for_device(dmab->dev.dev, dmab->private_data,
602 static inline void snd_dma_noncontig_iter_set(struct snd_dma_buffer *dmab,
603 struct sg_page_iter *piter,
606 struct sg_table *sgt = dmab->private_data;
608 __sg_page_iter_start(piter, sgt->sgl, sgt->orig_nents,
609 offset >> PAGE_SHIFT);
612 static dma_addr_t snd_dma_noncontig_get_addr(struct snd_dma_buffer *dmab,
615 struct sg_dma_page_iter iter;
617 snd_dma_noncontig_iter_set(dmab, &iter.base, offset);
618 __sg_page_iter_dma_next(&iter);
619 return sg_page_iter_dma_address(&iter) + offset % PAGE_SIZE;
622 static struct page *snd_dma_noncontig_get_page(struct snd_dma_buffer *dmab,
625 struct sg_page_iter iter;
627 snd_dma_noncontig_iter_set(dmab, &iter, offset);
628 __sg_page_iter_next(&iter);
629 return sg_page_iter_page(&iter);
633 snd_dma_noncontig_get_chunk_size(struct snd_dma_buffer *dmab,
634 unsigned int ofs, unsigned int size)
636 struct sg_dma_page_iter iter;
637 unsigned int start, end;
640 start = ALIGN_DOWN(ofs, PAGE_SIZE);
641 end = ofs + size - 1; /* the last byte address */
642 snd_dma_noncontig_iter_set(dmab, &iter.base, start);
643 if (!__sg_page_iter_dma_next(&iter))
645 /* check page continuity */
646 addr = sg_page_iter_dma_address(&iter);
652 if (!__sg_page_iter_dma_next(&iter) ||
653 sg_page_iter_dma_address(&iter) != addr)
656 /* ok, all on continuous pages */
660 static const struct snd_malloc_ops snd_dma_noncontig_ops = {
661 .alloc = snd_dma_noncontig_alloc,
662 .free = snd_dma_noncontig_free,
663 .mmap = snd_dma_noncontig_mmap,
664 .sync = snd_dma_noncontig_sync,
665 .get_addr = snd_dma_noncontig_get_addr,
666 .get_page = snd_dma_noncontig_get_page,
667 .get_chunk_size = snd_dma_noncontig_get_chunk_size,
670 /* x86-specific SG-buffer with WC pages */
671 #ifdef CONFIG_SND_DMA_SGBUF
672 #define sg_wc_address(it) ((unsigned long)page_address(sg_page_iter_page(it)))
674 static void *snd_dma_sg_wc_alloc(struct snd_dma_buffer *dmab, size_t size)
676 void *p = snd_dma_noncontig_alloc(dmab, size);
677 struct sg_table *sgt = dmab->private_data;
678 struct sg_page_iter iter;
682 if (dmab->dev.type != SNDRV_DMA_TYPE_DEV_WC_SG)
684 for_each_sgtable_page(sgt, &iter, 0)
685 set_memory_wc(sg_wc_address(&iter), 1);
689 static void snd_dma_sg_wc_free(struct snd_dma_buffer *dmab)
691 struct sg_table *sgt = dmab->private_data;
692 struct sg_page_iter iter;
694 for_each_sgtable_page(sgt, &iter, 0)
695 set_memory_wb(sg_wc_address(&iter), 1);
696 snd_dma_noncontig_free(dmab);
699 static int snd_dma_sg_wc_mmap(struct snd_dma_buffer *dmab,
700 struct vm_area_struct *area)
702 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
703 return dma_mmap_noncontiguous(dmab->dev.dev, area,
704 dmab->bytes, dmab->private_data);
707 static const struct snd_malloc_ops snd_dma_sg_wc_ops = {
708 .alloc = snd_dma_sg_wc_alloc,
709 .free = snd_dma_sg_wc_free,
710 .mmap = snd_dma_sg_wc_mmap,
711 .sync = snd_dma_noncontig_sync,
712 .get_addr = snd_dma_noncontig_get_addr,
713 .get_page = snd_dma_noncontig_get_page,
714 .get_chunk_size = snd_dma_noncontig_get_chunk_size,
717 /* Fallback SG-buffer allocations for x86 */
718 struct snd_dma_sg_fallback {
719 bool use_dma_alloc_coherent;
722 /* DMA address array; the first page contains #pages in ~PAGE_MASK */
726 static void __snd_dma_sg_fallback_free(struct snd_dma_buffer *dmab,
727 struct snd_dma_sg_fallback *sgbuf)
731 if (sgbuf->pages && sgbuf->addrs) {
733 while (i < sgbuf->count) {
734 if (!sgbuf->pages[i] || !sgbuf->addrs[i])
736 size = sgbuf->addrs[i] & ~PAGE_MASK;
739 if (sgbuf->use_dma_alloc_coherent)
740 dma_free_coherent(dmab->dev.dev, size << PAGE_SHIFT,
741 page_address(sgbuf->pages[i]),
742 sgbuf->addrs[i] & PAGE_MASK);
744 do_free_pages(page_address(sgbuf->pages[i]),
745 size << PAGE_SHIFT, false);
749 kvfree(sgbuf->pages);
750 kvfree(sgbuf->addrs);
754 static void *snd_dma_sg_fallback_alloc(struct snd_dma_buffer *dmab, size_t size)
756 struct snd_dma_sg_fallback *sgbuf;
757 struct page **pagep, *curp;
758 size_t chunk, npages;
763 /* correct the type */
764 if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_SG)
765 dmab->dev.type = SNDRV_DMA_TYPE_DEV_SG_FALLBACK;
766 else if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG)
767 dmab->dev.type = SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK;
769 sgbuf = kzalloc(sizeof(*sgbuf), GFP_KERNEL);
772 sgbuf->use_dma_alloc_coherent = cpu_feature_enabled(X86_FEATURE_XENPV);
773 size = PAGE_ALIGN(size);
774 sgbuf->count = size >> PAGE_SHIFT;
775 sgbuf->pages = kvcalloc(sgbuf->count, sizeof(*sgbuf->pages), GFP_KERNEL);
776 sgbuf->addrs = kvcalloc(sgbuf->count, sizeof(*sgbuf->addrs), GFP_KERNEL);
777 if (!sgbuf->pages || !sgbuf->addrs)
780 pagep = sgbuf->pages;
781 addrp = sgbuf->addrs;
782 chunk = (PAGE_SIZE - 1) << PAGE_SHIFT; /* to fit in low bits in addrs */
784 chunk = min(size, chunk);
785 if (sgbuf->use_dma_alloc_coherent)
786 p = dma_alloc_coherent(dmab->dev.dev, chunk, &addr, DEFAULT_GFP);
788 p = do_alloc_pages(dmab->dev.dev, chunk, &addr, false);
790 if (chunk <= PAGE_SIZE)
793 chunk = PAGE_SIZE << get_order(chunk);
799 npages = chunk >> PAGE_SHIFT;
800 *addrp = npages; /* store in lower bits */
801 curp = virt_to_page(p);
809 p = vmap(sgbuf->pages, sgbuf->count, VM_MAP, PAGE_KERNEL);
813 if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK)
814 set_pages_array_wc(sgbuf->pages, sgbuf->count);
816 dmab->private_data = sgbuf;
817 /* store the first page address for convenience */
818 dmab->addr = sgbuf->addrs[0] & PAGE_MASK;
822 __snd_dma_sg_fallback_free(dmab, sgbuf);
826 static void snd_dma_sg_fallback_free(struct snd_dma_buffer *dmab)
828 struct snd_dma_sg_fallback *sgbuf = dmab->private_data;
830 if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK)
831 set_pages_array_wb(sgbuf->pages, sgbuf->count);
833 __snd_dma_sg_fallback_free(dmab, dmab->private_data);
836 static dma_addr_t snd_dma_sg_fallback_get_addr(struct snd_dma_buffer *dmab,
839 struct snd_dma_sg_fallback *sgbuf = dmab->private_data;
840 size_t index = offset >> PAGE_SHIFT;
842 return (sgbuf->addrs[index] & PAGE_MASK) | (offset & ~PAGE_MASK);
845 static int snd_dma_sg_fallback_mmap(struct snd_dma_buffer *dmab,
846 struct vm_area_struct *area)
848 struct snd_dma_sg_fallback *sgbuf = dmab->private_data;
850 if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK)
851 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
852 return vm_map_pages(area, sgbuf->pages, sgbuf->count);
855 static const struct snd_malloc_ops snd_dma_sg_fallback_ops = {
856 .alloc = snd_dma_sg_fallback_alloc,
857 .free = snd_dma_sg_fallback_free,
858 .mmap = snd_dma_sg_fallback_mmap,
859 .get_addr = snd_dma_sg_fallback_get_addr,
860 /* reuse vmalloc helpers */
861 .get_page = snd_dma_vmalloc_get_page,
862 .get_chunk_size = snd_dma_vmalloc_get_chunk_size,
864 #endif /* CONFIG_SND_DMA_SGBUF */
867 * Non-coherent pages allocator
869 static void *snd_dma_noncoherent_alloc(struct snd_dma_buffer *dmab, size_t size)
873 p = dma_alloc_noncoherent(dmab->dev.dev, size, &dmab->addr,
874 dmab->dev.dir, DEFAULT_GFP);
876 dmab->dev.need_sync = dma_need_sync(dmab->dev.dev, dmab->addr);
880 static void snd_dma_noncoherent_free(struct snd_dma_buffer *dmab)
882 dma_free_noncoherent(dmab->dev.dev, dmab->bytes, dmab->area,
883 dmab->addr, dmab->dev.dir);
886 static int snd_dma_noncoherent_mmap(struct snd_dma_buffer *dmab,
887 struct vm_area_struct *area)
889 area->vm_page_prot = vm_get_page_prot(area->vm_flags);
890 return dma_mmap_pages(dmab->dev.dev, area,
891 area->vm_end - area->vm_start,
892 virt_to_page(dmab->area));
895 static void snd_dma_noncoherent_sync(struct snd_dma_buffer *dmab,
896 enum snd_dma_sync_mode mode)
898 if (mode == SNDRV_DMA_SYNC_CPU) {
899 if (dmab->dev.dir != DMA_TO_DEVICE)
900 dma_sync_single_for_cpu(dmab->dev.dev, dmab->addr,
901 dmab->bytes, dmab->dev.dir);
903 if (dmab->dev.dir != DMA_FROM_DEVICE)
904 dma_sync_single_for_device(dmab->dev.dev, dmab->addr,
905 dmab->bytes, dmab->dev.dir);
909 static const struct snd_malloc_ops snd_dma_noncoherent_ops = {
910 .alloc = snd_dma_noncoherent_alloc,
911 .free = snd_dma_noncoherent_free,
912 .mmap = snd_dma_noncoherent_mmap,
913 .sync = snd_dma_noncoherent_sync,
916 #endif /* CONFIG_HAS_DMA */
921 static const struct snd_malloc_ops *snd_dma_ops[] = {
922 [SNDRV_DMA_TYPE_CONTINUOUS] = &snd_dma_continuous_ops,
923 [SNDRV_DMA_TYPE_VMALLOC] = &snd_dma_vmalloc_ops,
924 #ifdef CONFIG_HAS_DMA
925 [SNDRV_DMA_TYPE_DEV] = &snd_dma_dev_ops,
926 [SNDRV_DMA_TYPE_DEV_WC] = &snd_dma_wc_ops,
927 [SNDRV_DMA_TYPE_NONCONTIG] = &snd_dma_noncontig_ops,
928 [SNDRV_DMA_TYPE_NONCOHERENT] = &snd_dma_noncoherent_ops,
929 #ifdef CONFIG_SND_DMA_SGBUF
930 [SNDRV_DMA_TYPE_DEV_WC_SG] = &snd_dma_sg_wc_ops,
932 #ifdef CONFIG_GENERIC_ALLOCATOR
933 [SNDRV_DMA_TYPE_DEV_IRAM] = &snd_dma_iram_ops,
934 #endif /* CONFIG_GENERIC_ALLOCATOR */
935 #ifdef CONFIG_SND_DMA_SGBUF
936 [SNDRV_DMA_TYPE_DEV_SG_FALLBACK] = &snd_dma_sg_fallback_ops,
937 [SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK] = &snd_dma_sg_fallback_ops,
939 #endif /* CONFIG_HAS_DMA */
942 static const struct snd_malloc_ops *snd_dma_get_ops(struct snd_dma_buffer *dmab)
944 if (WARN_ON_ONCE(!dmab))
946 if (WARN_ON_ONCE(dmab->dev.type <= SNDRV_DMA_TYPE_UNKNOWN ||
947 dmab->dev.type >= ARRAY_SIZE(snd_dma_ops)))
949 return snd_dma_ops[dmab->dev.type];