xref: /linux/sound/core/memalloc.c (revision 64b14a184e83eb62ea0615e31a409956049d40e7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4  *                   Takashi Iwai <tiwai@suse.de>
5  *
6  *  Generic memory allocators
7  */
8 
9 #include <linux/slab.h>
10 #include <linux/mm.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/genalloc.h>
13 #include <linux/highmem.h>
14 #include <linux/vmalloc.h>
15 #ifdef CONFIG_X86
16 #include <asm/set_memory.h>
17 #endif
18 #include <sound/memalloc.h>
19 #include "memalloc_local.h"
20 
21 static const struct snd_malloc_ops *snd_dma_get_ops(struct snd_dma_buffer *dmab);
22 
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,
25 					  gfp_t default_gfp)
26 {
27 	if (!dmab->dev.dev)
28 		return default_gfp;
29 	else
30 		return (__force gfp_t)(unsigned long)dmab->dev.dev;
31 }
32 
33 static void *__snd_dma_alloc_pages(struct snd_dma_buffer *dmab, size_t size)
34 {
35 	const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
36 
37 	if (WARN_ON_ONCE(!ops || !ops->alloc))
38 		return NULL;
39 	return ops->alloc(dmab, size);
40 }
41 
42 /**
43  * snd_dma_alloc_dir_pages - allocate the buffer area according to the given
44  *	type and direction
45  * @type: the DMA buffer type
46  * @device: the device pointer
47  * @dir: DMA direction
48  * @size: the buffer size to allocate
49  * @dmab: buffer allocation record to store the allocated data
50  *
51  * Calls the memory-allocator function for the corresponding
52  * buffer type.
53  *
54  * Return: Zero if the buffer with the given size is allocated successfully,
55  * otherwise a negative value on error.
56  */
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)
60 {
61 	if (WARN_ON(!size))
62 		return -ENXIO;
63 	if (WARN_ON(!dmab))
64 		return -ENXIO;
65 
66 	size = PAGE_ALIGN(size);
67 	dmab->dev.type = type;
68 	dmab->dev.dev = device;
69 	dmab->dev.dir = dir;
70 	dmab->bytes = 0;
71 	dmab->addr = 0;
72 	dmab->private_data = NULL;
73 	dmab->area = __snd_dma_alloc_pages(dmab, size);
74 	if (!dmab->area)
75 		return -ENOMEM;
76 	dmab->bytes = size;
77 	return 0;
78 }
79 EXPORT_SYMBOL(snd_dma_alloc_dir_pages);
80 
81 /**
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
87  *
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
91  * res_size argument.
92  *
93  * Return: Zero if the buffer with the given size is allocated successfully,
94  * otherwise a negative value on error.
95  */
96 int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
97 				 struct snd_dma_buffer *dmab)
98 {
99 	int err;
100 
101 	while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
102 		if (err != -ENOMEM)
103 			return err;
104 		if (size <= PAGE_SIZE)
105 			return -ENOMEM;
106 		size >>= 1;
107 		size = PAGE_SIZE << get_order(size);
108 	}
109 	if (! dmab->area)
110 		return -ENOMEM;
111 	return 0;
112 }
113 EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
114 
115 /**
116  * snd_dma_free_pages - release the allocated buffer
117  * @dmab: the buffer allocation record to release
118  *
119  * Releases the allocated buffer via snd_dma_alloc_pages().
120  */
121 void snd_dma_free_pages(struct snd_dma_buffer *dmab)
122 {
123 	const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
124 
125 	if (ops && ops->free)
126 		ops->free(dmab);
127 }
128 EXPORT_SYMBOL(snd_dma_free_pages);
129 
130 /* called by devres */
131 static void __snd_release_pages(struct device *dev, void *res)
132 {
133 	snd_dma_free_pages(res);
134 }
135 
136 /**
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
142  *
143  * Allocate buffer pages depending on the given type and manage using devres.
144  * The pages will be released automatically at the device removal.
145  *
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.
149  *
150  * The function returns the snd_dma_buffer object at success, or NULL if failed.
151  */
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)
155 {
156 	struct snd_dma_buffer *dmab;
157 	int err;
158 
159 	if (WARN_ON(type == SNDRV_DMA_TYPE_CONTINUOUS ||
160 		    type == SNDRV_DMA_TYPE_VMALLOC))
161 		return NULL;
162 
163 	dmab = devres_alloc(__snd_release_pages, sizeof(*dmab), GFP_KERNEL);
164 	if (!dmab)
165 		return NULL;
166 
167 	err = snd_dma_alloc_dir_pages(type, dev, dir, size, dmab);
168 	if (err < 0) {
169 		devres_free(dmab);
170 		return NULL;
171 	}
172 
173 	devres_add(dev, dmab);
174 	return dmab;
175 }
176 EXPORT_SYMBOL_GPL(snd_devm_alloc_dir_pages);
177 
178 /**
179  * snd_dma_buffer_mmap - perform mmap of the given DMA buffer
180  * @dmab: buffer allocation information
181  * @area: VM area information
182  */
183 int snd_dma_buffer_mmap(struct snd_dma_buffer *dmab,
184 			struct vm_area_struct *area)
185 {
186 	const struct snd_malloc_ops *ops;
187 
188 	if (!dmab)
189 		return -ENOENT;
190 	ops = snd_dma_get_ops(dmab);
191 	if (ops && ops->mmap)
192 		return ops->mmap(dmab, area);
193 	else
194 		return -ENOENT;
195 }
196 EXPORT_SYMBOL(snd_dma_buffer_mmap);
197 
198 #ifdef CONFIG_HAS_DMA
199 /**
200  * snd_dma_buffer_sync - sync DMA buffer between CPU and device
201  * @dmab: buffer allocation information
202  * @mode: sync mode
203  */
204 void snd_dma_buffer_sync(struct snd_dma_buffer *dmab,
205 			 enum snd_dma_sync_mode mode)
206 {
207 	const struct snd_malloc_ops *ops;
208 
209 	if (!dmab || !dmab->dev.need_sync)
210 		return;
211 	ops = snd_dma_get_ops(dmab);
212 	if (ops && ops->sync)
213 		ops->sync(dmab, mode);
214 }
215 EXPORT_SYMBOL_GPL(snd_dma_buffer_sync);
216 #endif /* CONFIG_HAS_DMA */
217 
218 /**
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
222  */
223 dma_addr_t snd_sgbuf_get_addr(struct snd_dma_buffer *dmab, size_t offset)
224 {
225 	const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
226 
227 	if (ops && ops->get_addr)
228 		return ops->get_addr(dmab, offset);
229 	else
230 		return dmab->addr + offset;
231 }
232 EXPORT_SYMBOL(snd_sgbuf_get_addr);
233 
234 /**
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
238  */
239 struct page *snd_sgbuf_get_page(struct snd_dma_buffer *dmab, size_t offset)
240 {
241 	const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
242 
243 	if (ops && ops->get_page)
244 		return ops->get_page(dmab, offset);
245 	else
246 		return virt_to_page(dmab->area + offset);
247 }
248 EXPORT_SYMBOL(snd_sgbuf_get_page);
249 
250 /**
251  * snd_sgbuf_get_chunk_size - compute the max chunk size with continuous pages
252  *	on sg-buffer
253  * @dmab: buffer allocation information
254  * @ofs: offset in the ring buffer
255  * @size: the requested size
256  */
257 unsigned int snd_sgbuf_get_chunk_size(struct snd_dma_buffer *dmab,
258 				      unsigned int ofs, unsigned int size)
259 {
260 	const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
261 
262 	if (ops && ops->get_chunk_size)
263 		return ops->get_chunk_size(dmab, ofs, size);
264 	else
265 		return size;
266 }
267 EXPORT_SYMBOL(snd_sgbuf_get_chunk_size);
268 
269 /*
270  * Continuous pages allocator
271  */
272 static void *snd_dma_continuous_alloc(struct snd_dma_buffer *dmab, size_t size)
273 {
274 	gfp_t gfp = snd_mem_get_gfp_flags(dmab, GFP_KERNEL);
275 	void *p = alloc_pages_exact(size, gfp);
276 
277 	if (p)
278 		dmab->addr = page_to_phys(virt_to_page(p));
279 	return p;
280 }
281 
282 static void snd_dma_continuous_free(struct snd_dma_buffer *dmab)
283 {
284 	free_pages_exact(dmab->area, dmab->bytes);
285 }
286 
287 static int snd_dma_continuous_mmap(struct snd_dma_buffer *dmab,
288 				   struct vm_area_struct *area)
289 {
290 	return remap_pfn_range(area, area->vm_start,
291 			       dmab->addr >> PAGE_SHIFT,
292 			       area->vm_end - area->vm_start,
293 			       area->vm_page_prot);
294 }
295 
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,
300 };
301 
302 /*
303  * VMALLOC allocator
304  */
305 static void *snd_dma_vmalloc_alloc(struct snd_dma_buffer *dmab, size_t size)
306 {
307 	gfp_t gfp = snd_mem_get_gfp_flags(dmab, GFP_KERNEL | __GFP_HIGHMEM);
308 
309 	return __vmalloc(size, gfp);
310 }
311 
312 static void snd_dma_vmalloc_free(struct snd_dma_buffer *dmab)
313 {
314 	vfree(dmab->area);
315 }
316 
317 static int snd_dma_vmalloc_mmap(struct snd_dma_buffer *dmab,
318 				struct vm_area_struct *area)
319 {
320 	return remap_vmalloc_range(area, dmab->area, 0);
321 }
322 
323 #define get_vmalloc_page_addr(dmab, offset) \
324 	page_to_phys(vmalloc_to_page((dmab)->area + (offset)))
325 
326 static dma_addr_t snd_dma_vmalloc_get_addr(struct snd_dma_buffer *dmab,
327 					   size_t offset)
328 {
329 	return get_vmalloc_page_addr(dmab, offset) + offset % PAGE_SIZE;
330 }
331 
332 static struct page *snd_dma_vmalloc_get_page(struct snd_dma_buffer *dmab,
333 					     size_t offset)
334 {
335 	return vmalloc_to_page(dmab->area + offset);
336 }
337 
338 static unsigned int
339 snd_dma_vmalloc_get_chunk_size(struct snd_dma_buffer *dmab,
340 			       unsigned int ofs, unsigned int size)
341 {
342 	unsigned int start, end;
343 	unsigned long addr;
344 
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);
349 	for (;;) {
350 		start += PAGE_SIZE;
351 		if (start > end)
352 			break;
353 		addr += PAGE_SIZE;
354 		if (get_vmalloc_page_addr(dmab, start) != addr)
355 			return start - ofs;
356 	}
357 	/* ok, all on continuous pages */
358 	return size;
359 }
360 
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,
368 };
369 
370 #ifdef CONFIG_HAS_DMA
371 /*
372  * IRAM allocator
373  */
374 #ifdef CONFIG_GENERIC_ALLOCATOR
375 static void *snd_dma_iram_alloc(struct snd_dma_buffer *dmab, size_t size)
376 {
377 	struct device *dev = dmab->dev.dev;
378 	struct gen_pool *pool;
379 	void *p;
380 
381 	if (dev->of_node) {
382 		pool = of_gen_pool_get(dev->of_node, "iram", 0);
383 		/* Assign the pool into private_data field */
384 		dmab->private_data = pool;
385 
386 		p = gen_pool_dma_alloc_align(pool, size, &dmab->addr, PAGE_SIZE);
387 		if (p)
388 			return p;
389 	}
390 
391 	/* Internal memory might have limited size and no enough space,
392 	 * so if we fail to malloc, try to fetch memory traditionally.
393 	 */
394 	dmab->dev.type = SNDRV_DMA_TYPE_DEV;
395 	return __snd_dma_alloc_pages(dmab, size);
396 }
397 
398 static void snd_dma_iram_free(struct snd_dma_buffer *dmab)
399 {
400 	struct gen_pool *pool = dmab->private_data;
401 
402 	if (pool && dmab->area)
403 		gen_pool_free(pool, (unsigned long)dmab->area, dmab->bytes);
404 }
405 
406 static int snd_dma_iram_mmap(struct snd_dma_buffer *dmab,
407 			     struct vm_area_struct *area)
408 {
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,
413 			       area->vm_page_prot);
414 }
415 
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,
420 };
421 #endif /* CONFIG_GENERIC_ALLOCATOR */
422 
423 #define DEFAULT_GFP \
424 	(GFP_KERNEL | \
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 */
428 
429 /*
430  * Coherent device pages allocator
431  */
432 static void *snd_dma_dev_alloc(struct snd_dma_buffer *dmab, size_t size)
433 {
434 	void *p;
435 
436 	p = dma_alloc_coherent(dmab->dev.dev, size, &dmab->addr, DEFAULT_GFP);
437 #ifdef CONFIG_X86
438 	if (p && dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC)
439 		set_memory_wc((unsigned long)p, PAGE_ALIGN(size) >> PAGE_SHIFT);
440 #endif
441 	return p;
442 }
443 
444 static void snd_dma_dev_free(struct snd_dma_buffer *dmab)
445 {
446 #ifdef CONFIG_X86
447 	if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC)
448 		set_memory_wb((unsigned long)dmab->area,
449 			      PAGE_ALIGN(dmab->bytes) >> PAGE_SHIFT);
450 #endif
451 	dma_free_coherent(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
452 }
453 
454 static int snd_dma_dev_mmap(struct snd_dma_buffer *dmab,
455 			    struct vm_area_struct *area)
456 {
457 #ifdef CONFIG_X86
458 	if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC)
459 		area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
460 #endif
461 	return dma_mmap_coherent(dmab->dev.dev, area,
462 				 dmab->area, dmab->addr, dmab->bytes);
463 }
464 
465 static const struct snd_malloc_ops snd_dma_dev_ops = {
466 	.alloc = snd_dma_dev_alloc,
467 	.free = snd_dma_dev_free,
468 	.mmap = snd_dma_dev_mmap,
469 };
470 
471 /*
472  * Write-combined pages
473  */
474 #ifdef CONFIG_X86
475 /* On x86, share the same ops as the standard dev ops */
476 #define snd_dma_wc_ops	snd_dma_dev_ops
477 #else /* CONFIG_X86 */
478 static void *snd_dma_wc_alloc(struct snd_dma_buffer *dmab, size_t size)
479 {
480 	return dma_alloc_wc(dmab->dev.dev, size, &dmab->addr, DEFAULT_GFP);
481 }
482 
483 static void snd_dma_wc_free(struct snd_dma_buffer *dmab)
484 {
485 	dma_free_wc(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
486 }
487 
488 static int snd_dma_wc_mmap(struct snd_dma_buffer *dmab,
489 			   struct vm_area_struct *area)
490 {
491 	return dma_mmap_wc(dmab->dev.dev, area,
492 			   dmab->area, dmab->addr, dmab->bytes);
493 }
494 
495 static const struct snd_malloc_ops snd_dma_wc_ops = {
496 	.alloc = snd_dma_wc_alloc,
497 	.free = snd_dma_wc_free,
498 	.mmap = snd_dma_wc_mmap,
499 };
500 #endif /* CONFIG_X86 */
501 
502 /*
503  * Non-contiguous pages allocator
504  */
505 static void *snd_dma_noncontig_alloc(struct snd_dma_buffer *dmab, size_t size)
506 {
507 	struct sg_table *sgt;
508 	void *p;
509 
510 	sgt = dma_alloc_noncontiguous(dmab->dev.dev, size, dmab->dev.dir,
511 				      DEFAULT_GFP, 0);
512 	if (!sgt)
513 		return NULL;
514 	dmab->dev.need_sync = dma_need_sync(dmab->dev.dev, dmab->dev.dir);
515 	p = dma_vmap_noncontiguous(dmab->dev.dev, size, sgt);
516 	if (p)
517 		dmab->private_data = sgt;
518 	else
519 		dma_free_noncontiguous(dmab->dev.dev, size, sgt, dmab->dev.dir);
520 	return p;
521 }
522 
523 static void snd_dma_noncontig_free(struct snd_dma_buffer *dmab)
524 {
525 	dma_vunmap_noncontiguous(dmab->dev.dev, dmab->area);
526 	dma_free_noncontiguous(dmab->dev.dev, dmab->bytes, dmab->private_data,
527 			       dmab->dev.dir);
528 }
529 
530 static int snd_dma_noncontig_mmap(struct snd_dma_buffer *dmab,
531 				  struct vm_area_struct *area)
532 {
533 	return dma_mmap_noncontiguous(dmab->dev.dev, area,
534 				      dmab->bytes, dmab->private_data);
535 }
536 
537 static void snd_dma_noncontig_sync(struct snd_dma_buffer *dmab,
538 				   enum snd_dma_sync_mode mode)
539 {
540 	if (mode == SNDRV_DMA_SYNC_CPU) {
541 		if (dmab->dev.dir == DMA_TO_DEVICE)
542 			return;
543 		dma_sync_sgtable_for_cpu(dmab->dev.dev, dmab->private_data,
544 					 dmab->dev.dir);
545 		invalidate_kernel_vmap_range(dmab->area, dmab->bytes);
546 	} else {
547 		if (dmab->dev.dir == DMA_FROM_DEVICE)
548 			return;
549 		flush_kernel_vmap_range(dmab->area, dmab->bytes);
550 		dma_sync_sgtable_for_device(dmab->dev.dev, dmab->private_data,
551 					    dmab->dev.dir);
552 	}
553 }
554 
555 static inline void snd_dma_noncontig_iter_set(struct snd_dma_buffer *dmab,
556 					      struct sg_page_iter *piter,
557 					      size_t offset)
558 {
559 	struct sg_table *sgt = dmab->private_data;
560 
561 	__sg_page_iter_start(piter, sgt->sgl, sgt->orig_nents,
562 			     offset >> PAGE_SHIFT);
563 }
564 
565 static dma_addr_t snd_dma_noncontig_get_addr(struct snd_dma_buffer *dmab,
566 					     size_t offset)
567 {
568 	struct sg_dma_page_iter iter;
569 
570 	snd_dma_noncontig_iter_set(dmab, &iter.base, offset);
571 	__sg_page_iter_dma_next(&iter);
572 	return sg_page_iter_dma_address(&iter) + offset % PAGE_SIZE;
573 }
574 
575 static struct page *snd_dma_noncontig_get_page(struct snd_dma_buffer *dmab,
576 					       size_t offset)
577 {
578 	struct sg_page_iter iter;
579 
580 	snd_dma_noncontig_iter_set(dmab, &iter, offset);
581 	__sg_page_iter_next(&iter);
582 	return sg_page_iter_page(&iter);
583 }
584 
585 static unsigned int
586 snd_dma_noncontig_get_chunk_size(struct snd_dma_buffer *dmab,
587 				 unsigned int ofs, unsigned int size)
588 {
589 	struct sg_dma_page_iter iter;
590 	unsigned int start, end;
591 	unsigned long addr;
592 
593 	start = ALIGN_DOWN(ofs, PAGE_SIZE);
594 	end = ofs + size - 1; /* the last byte address */
595 	snd_dma_noncontig_iter_set(dmab, &iter.base, start);
596 	if (!__sg_page_iter_dma_next(&iter))
597 		return 0;
598 	/* check page continuity */
599 	addr = sg_page_iter_dma_address(&iter);
600 	for (;;) {
601 		start += PAGE_SIZE;
602 		if (start > end)
603 			break;
604 		addr += PAGE_SIZE;
605 		if (!__sg_page_iter_dma_next(&iter) ||
606 		    sg_page_iter_dma_address(&iter) != addr)
607 			return start - ofs;
608 	}
609 	/* ok, all on continuous pages */
610 	return size;
611 }
612 
613 static const struct snd_malloc_ops snd_dma_noncontig_ops = {
614 	.alloc = snd_dma_noncontig_alloc,
615 	.free = snd_dma_noncontig_free,
616 	.mmap = snd_dma_noncontig_mmap,
617 	.sync = snd_dma_noncontig_sync,
618 	.get_addr = snd_dma_noncontig_get_addr,
619 	.get_page = snd_dma_noncontig_get_page,
620 	.get_chunk_size = snd_dma_noncontig_get_chunk_size,
621 };
622 
623 /* x86-specific SG-buffer with WC pages */
624 #ifdef CONFIG_SND_DMA_SGBUF
625 #define sg_wc_address(it) ((unsigned long)page_address(sg_page_iter_page(it)))
626 
627 static void *snd_dma_sg_wc_alloc(struct snd_dma_buffer *dmab, size_t size)
628 {
629 	void *p = snd_dma_noncontig_alloc(dmab, size);
630 	struct sg_table *sgt = dmab->private_data;
631 	struct sg_page_iter iter;
632 
633 	if (!p)
634 		return NULL;
635 	for_each_sgtable_page(sgt, &iter, 0)
636 		set_memory_wc(sg_wc_address(&iter), 1);
637 	return p;
638 }
639 
640 static void snd_dma_sg_wc_free(struct snd_dma_buffer *dmab)
641 {
642 	struct sg_table *sgt = dmab->private_data;
643 	struct sg_page_iter iter;
644 
645 	for_each_sgtable_page(sgt, &iter, 0)
646 		set_memory_wb(sg_wc_address(&iter), 1);
647 	snd_dma_noncontig_free(dmab);
648 }
649 
650 static int snd_dma_sg_wc_mmap(struct snd_dma_buffer *dmab,
651 			      struct vm_area_struct *area)
652 {
653 	area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
654 	return dma_mmap_noncontiguous(dmab->dev.dev, area,
655 				      dmab->bytes, dmab->private_data);
656 }
657 
658 static const struct snd_malloc_ops snd_dma_sg_wc_ops = {
659 	.alloc = snd_dma_sg_wc_alloc,
660 	.free = snd_dma_sg_wc_free,
661 	.mmap = snd_dma_sg_wc_mmap,
662 	.sync = snd_dma_noncontig_sync,
663 	.get_addr = snd_dma_noncontig_get_addr,
664 	.get_page = snd_dma_noncontig_get_page,
665 	.get_chunk_size = snd_dma_noncontig_get_chunk_size,
666 };
667 #endif /* CONFIG_SND_DMA_SGBUF */
668 
669 /*
670  * Non-coherent pages allocator
671  */
672 static void *snd_dma_noncoherent_alloc(struct snd_dma_buffer *dmab, size_t size)
673 {
674 	dmab->dev.need_sync = dma_need_sync(dmab->dev.dev, dmab->dev.dir);
675 	return dma_alloc_noncoherent(dmab->dev.dev, size, &dmab->addr,
676 				     dmab->dev.dir, DEFAULT_GFP);
677 }
678 
679 static void snd_dma_noncoherent_free(struct snd_dma_buffer *dmab)
680 {
681 	dma_free_noncoherent(dmab->dev.dev, dmab->bytes, dmab->area,
682 			     dmab->addr, dmab->dev.dir);
683 }
684 
685 static int snd_dma_noncoherent_mmap(struct snd_dma_buffer *dmab,
686 				    struct vm_area_struct *area)
687 {
688 	area->vm_page_prot = vm_get_page_prot(area->vm_flags);
689 	return dma_mmap_pages(dmab->dev.dev, area,
690 			      area->vm_end - area->vm_start,
691 			      virt_to_page(dmab->area));
692 }
693 
694 static void snd_dma_noncoherent_sync(struct snd_dma_buffer *dmab,
695 				     enum snd_dma_sync_mode mode)
696 {
697 	if (mode == SNDRV_DMA_SYNC_CPU) {
698 		if (dmab->dev.dir != DMA_TO_DEVICE)
699 			dma_sync_single_for_cpu(dmab->dev.dev, dmab->addr,
700 						dmab->bytes, dmab->dev.dir);
701 	} else {
702 		if (dmab->dev.dir != DMA_FROM_DEVICE)
703 			dma_sync_single_for_device(dmab->dev.dev, dmab->addr,
704 						   dmab->bytes, dmab->dev.dir);
705 	}
706 }
707 
708 static const struct snd_malloc_ops snd_dma_noncoherent_ops = {
709 	.alloc = snd_dma_noncoherent_alloc,
710 	.free = snd_dma_noncoherent_free,
711 	.mmap = snd_dma_noncoherent_mmap,
712 	.sync = snd_dma_noncoherent_sync,
713 };
714 
715 #endif /* CONFIG_HAS_DMA */
716 
717 /*
718  * Entry points
719  */
720 static const struct snd_malloc_ops *dma_ops[] = {
721 	[SNDRV_DMA_TYPE_CONTINUOUS] = &snd_dma_continuous_ops,
722 	[SNDRV_DMA_TYPE_VMALLOC] = &snd_dma_vmalloc_ops,
723 #ifdef CONFIG_HAS_DMA
724 	[SNDRV_DMA_TYPE_DEV] = &snd_dma_dev_ops,
725 	[SNDRV_DMA_TYPE_DEV_WC] = &snd_dma_wc_ops,
726 	[SNDRV_DMA_TYPE_NONCONTIG] = &snd_dma_noncontig_ops,
727 	[SNDRV_DMA_TYPE_NONCOHERENT] = &snd_dma_noncoherent_ops,
728 #ifdef CONFIG_SND_DMA_SGBUF
729 	[SNDRV_DMA_TYPE_DEV_WC_SG] = &snd_dma_sg_wc_ops,
730 #endif
731 #ifdef CONFIG_GENERIC_ALLOCATOR
732 	[SNDRV_DMA_TYPE_DEV_IRAM] = &snd_dma_iram_ops,
733 #endif /* CONFIG_GENERIC_ALLOCATOR */
734 #endif /* CONFIG_HAS_DMA */
735 };
736 
737 static const struct snd_malloc_ops *snd_dma_get_ops(struct snd_dma_buffer *dmab)
738 {
739 	if (WARN_ON_ONCE(!dmab))
740 		return NULL;
741 	if (WARN_ON_ONCE(dmab->dev.type <= SNDRV_DMA_TYPE_UNKNOWN ||
742 			 dmab->dev.type >= ARRAY_SIZE(dma_ops)))
743 		return NULL;
744 	return dma_ops[dmab->dev.type];
745 }
746