xref: /openbmc/linux/sound/core/memalloc.c (revision dd3cb467)
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  * Return: 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  * Return: zero if successful, or a negative error code
184  */
185 int snd_dma_buffer_mmap(struct snd_dma_buffer *dmab,
186 			struct vm_area_struct *area)
187 {
188 	const struct snd_malloc_ops *ops;
189 
190 	if (!dmab)
191 		return -ENOENT;
192 	ops = snd_dma_get_ops(dmab);
193 	if (ops && ops->mmap)
194 		return ops->mmap(dmab, area);
195 	else
196 		return -ENOENT;
197 }
198 EXPORT_SYMBOL(snd_dma_buffer_mmap);
199 
200 #ifdef CONFIG_HAS_DMA
201 /**
202  * snd_dma_buffer_sync - sync DMA buffer between CPU and device
203  * @dmab: buffer allocation information
204  * @mode: sync mode
205  */
206 void snd_dma_buffer_sync(struct snd_dma_buffer *dmab,
207 			 enum snd_dma_sync_mode mode)
208 {
209 	const struct snd_malloc_ops *ops;
210 
211 	if (!dmab || !dmab->dev.need_sync)
212 		return;
213 	ops = snd_dma_get_ops(dmab);
214 	if (ops && ops->sync)
215 		ops->sync(dmab, mode);
216 }
217 EXPORT_SYMBOL_GPL(snd_dma_buffer_sync);
218 #endif /* CONFIG_HAS_DMA */
219 
220 /**
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
224  *
225  * Return: the physical address
226  */
227 dma_addr_t snd_sgbuf_get_addr(struct snd_dma_buffer *dmab, size_t offset)
228 {
229 	const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
230 
231 	if (ops && ops->get_addr)
232 		return ops->get_addr(dmab, offset);
233 	else
234 		return dmab->addr + offset;
235 }
236 EXPORT_SYMBOL(snd_sgbuf_get_addr);
237 
238 /**
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
242  *
243  * Return: the page pointer
244  */
245 struct page *snd_sgbuf_get_page(struct snd_dma_buffer *dmab, size_t offset)
246 {
247 	const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
248 
249 	if (ops && ops->get_page)
250 		return ops->get_page(dmab, offset);
251 	else
252 		return virt_to_page(dmab->area + offset);
253 }
254 EXPORT_SYMBOL(snd_sgbuf_get_page);
255 
256 /**
257  * snd_sgbuf_get_chunk_size - compute the max chunk size with continuous pages
258  *	on sg-buffer
259  * @dmab: buffer allocation information
260  * @ofs: offset in the ring buffer
261  * @size: the requested size
262  *
263  * Return: the chunk size
264  */
265 unsigned int snd_sgbuf_get_chunk_size(struct snd_dma_buffer *dmab,
266 				      unsigned int ofs, unsigned int size)
267 {
268 	const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
269 
270 	if (ops && ops->get_chunk_size)
271 		return ops->get_chunk_size(dmab, ofs, size);
272 	else
273 		return size;
274 }
275 EXPORT_SYMBOL(snd_sgbuf_get_chunk_size);
276 
277 /*
278  * Continuous pages allocator
279  */
280 static void *snd_dma_continuous_alloc(struct snd_dma_buffer *dmab, size_t size)
281 {
282 	gfp_t gfp = snd_mem_get_gfp_flags(dmab, GFP_KERNEL);
283 	void *p = alloc_pages_exact(size, gfp);
284 
285 	if (p)
286 		dmab->addr = page_to_phys(virt_to_page(p));
287 	return p;
288 }
289 
290 static void snd_dma_continuous_free(struct snd_dma_buffer *dmab)
291 {
292 	free_pages_exact(dmab->area, dmab->bytes);
293 }
294 
295 static int snd_dma_continuous_mmap(struct snd_dma_buffer *dmab,
296 				   struct vm_area_struct *area)
297 {
298 	return remap_pfn_range(area, area->vm_start,
299 			       dmab->addr >> PAGE_SHIFT,
300 			       area->vm_end - area->vm_start,
301 			       area->vm_page_prot);
302 }
303 
304 static const struct snd_malloc_ops snd_dma_continuous_ops = {
305 	.alloc = snd_dma_continuous_alloc,
306 	.free = snd_dma_continuous_free,
307 	.mmap = snd_dma_continuous_mmap,
308 };
309 
310 /*
311  * VMALLOC allocator
312  */
313 static void *snd_dma_vmalloc_alloc(struct snd_dma_buffer *dmab, size_t size)
314 {
315 	gfp_t gfp = snd_mem_get_gfp_flags(dmab, GFP_KERNEL | __GFP_HIGHMEM);
316 
317 	return __vmalloc(size, gfp);
318 }
319 
320 static void snd_dma_vmalloc_free(struct snd_dma_buffer *dmab)
321 {
322 	vfree(dmab->area);
323 }
324 
325 static int snd_dma_vmalloc_mmap(struct snd_dma_buffer *dmab,
326 				struct vm_area_struct *area)
327 {
328 	return remap_vmalloc_range(area, dmab->area, 0);
329 }
330 
331 #define get_vmalloc_page_addr(dmab, offset) \
332 	page_to_phys(vmalloc_to_page((dmab)->area + (offset)))
333 
334 static dma_addr_t snd_dma_vmalloc_get_addr(struct snd_dma_buffer *dmab,
335 					   size_t offset)
336 {
337 	return get_vmalloc_page_addr(dmab, offset) + offset % PAGE_SIZE;
338 }
339 
340 static struct page *snd_dma_vmalloc_get_page(struct snd_dma_buffer *dmab,
341 					     size_t offset)
342 {
343 	return vmalloc_to_page(dmab->area + offset);
344 }
345 
346 static unsigned int
347 snd_dma_vmalloc_get_chunk_size(struct snd_dma_buffer *dmab,
348 			       unsigned int ofs, unsigned int size)
349 {
350 	unsigned int start, end;
351 	unsigned long addr;
352 
353 	start = ALIGN_DOWN(ofs, PAGE_SIZE);
354 	end = ofs + size - 1; /* the last byte address */
355 	/* check page continuity */
356 	addr = get_vmalloc_page_addr(dmab, start);
357 	for (;;) {
358 		start += PAGE_SIZE;
359 		if (start > end)
360 			break;
361 		addr += PAGE_SIZE;
362 		if (get_vmalloc_page_addr(dmab, start) != addr)
363 			return start - ofs;
364 	}
365 	/* ok, all on continuous pages */
366 	return size;
367 }
368 
369 static const struct snd_malloc_ops snd_dma_vmalloc_ops = {
370 	.alloc = snd_dma_vmalloc_alloc,
371 	.free = snd_dma_vmalloc_free,
372 	.mmap = snd_dma_vmalloc_mmap,
373 	.get_addr = snd_dma_vmalloc_get_addr,
374 	.get_page = snd_dma_vmalloc_get_page,
375 	.get_chunk_size = snd_dma_vmalloc_get_chunk_size,
376 };
377 
378 #ifdef CONFIG_HAS_DMA
379 /*
380  * IRAM allocator
381  */
382 #ifdef CONFIG_GENERIC_ALLOCATOR
383 static void *snd_dma_iram_alloc(struct snd_dma_buffer *dmab, size_t size)
384 {
385 	struct device *dev = dmab->dev.dev;
386 	struct gen_pool *pool;
387 	void *p;
388 
389 	if (dev->of_node) {
390 		pool = of_gen_pool_get(dev->of_node, "iram", 0);
391 		/* Assign the pool into private_data field */
392 		dmab->private_data = pool;
393 
394 		p = gen_pool_dma_alloc_align(pool, size, &dmab->addr, PAGE_SIZE);
395 		if (p)
396 			return p;
397 	}
398 
399 	/* Internal memory might have limited size and no enough space,
400 	 * so if we fail to malloc, try to fetch memory traditionally.
401 	 */
402 	dmab->dev.type = SNDRV_DMA_TYPE_DEV;
403 	return __snd_dma_alloc_pages(dmab, size);
404 }
405 
406 static void snd_dma_iram_free(struct snd_dma_buffer *dmab)
407 {
408 	struct gen_pool *pool = dmab->private_data;
409 
410 	if (pool && dmab->area)
411 		gen_pool_free(pool, (unsigned long)dmab->area, dmab->bytes);
412 }
413 
414 static int snd_dma_iram_mmap(struct snd_dma_buffer *dmab,
415 			     struct vm_area_struct *area)
416 {
417 	area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
418 	return remap_pfn_range(area, area->vm_start,
419 			       dmab->addr >> PAGE_SHIFT,
420 			       area->vm_end - area->vm_start,
421 			       area->vm_page_prot);
422 }
423 
424 static const struct snd_malloc_ops snd_dma_iram_ops = {
425 	.alloc = snd_dma_iram_alloc,
426 	.free = snd_dma_iram_free,
427 	.mmap = snd_dma_iram_mmap,
428 };
429 #endif /* CONFIG_GENERIC_ALLOCATOR */
430 
431 #define DEFAULT_GFP \
432 	(GFP_KERNEL | \
433 	 __GFP_COMP |    /* compound page lets parts be mapped */ \
434 	 __GFP_NORETRY | /* don't trigger OOM-killer */ \
435 	 __GFP_NOWARN)   /* no stack trace print - this call is non-critical */
436 
437 /*
438  * Coherent device pages allocator
439  */
440 static void *snd_dma_dev_alloc(struct snd_dma_buffer *dmab, size_t size)
441 {
442 	return dma_alloc_coherent(dmab->dev.dev, size, &dmab->addr, DEFAULT_GFP);
443 }
444 
445 static void snd_dma_dev_free(struct snd_dma_buffer *dmab)
446 {
447 	dma_free_coherent(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
448 }
449 
450 static int snd_dma_dev_mmap(struct snd_dma_buffer *dmab,
451 			    struct vm_area_struct *area)
452 {
453 	return dma_mmap_coherent(dmab->dev.dev, area,
454 				 dmab->area, dmab->addr, dmab->bytes);
455 }
456 
457 static const struct snd_malloc_ops snd_dma_dev_ops = {
458 	.alloc = snd_dma_dev_alloc,
459 	.free = snd_dma_dev_free,
460 	.mmap = snd_dma_dev_mmap,
461 };
462 
463 /*
464  * Write-combined pages
465  */
466 static void *snd_dma_wc_alloc(struct snd_dma_buffer *dmab, size_t size)
467 {
468 	return dma_alloc_wc(dmab->dev.dev, size, &dmab->addr, DEFAULT_GFP);
469 }
470 
471 static void snd_dma_wc_free(struct snd_dma_buffer *dmab)
472 {
473 	dma_free_wc(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
474 }
475 
476 static int snd_dma_wc_mmap(struct snd_dma_buffer *dmab,
477 			   struct vm_area_struct *area)
478 {
479 	return dma_mmap_wc(dmab->dev.dev, area,
480 			   dmab->area, dmab->addr, dmab->bytes);
481 }
482 
483 static const struct snd_malloc_ops snd_dma_wc_ops = {
484 	.alloc = snd_dma_wc_alloc,
485 	.free = snd_dma_wc_free,
486 	.mmap = snd_dma_wc_mmap,
487 };
488 
489 #ifdef CONFIG_SND_DMA_SGBUF
490 static void *snd_dma_sg_fallback_alloc(struct snd_dma_buffer *dmab, size_t size);
491 #endif
492 
493 /*
494  * Non-contiguous pages allocator
495  */
496 static void *snd_dma_noncontig_alloc(struct snd_dma_buffer *dmab, size_t size)
497 {
498 	struct sg_table *sgt;
499 	void *p;
500 
501 	sgt = dma_alloc_noncontiguous(dmab->dev.dev, size, dmab->dev.dir,
502 				      DEFAULT_GFP, 0);
503 	if (!sgt) {
504 #ifdef CONFIG_SND_DMA_SGBUF
505 		if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG)
506 			dmab->dev.type = SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK;
507 		else
508 			dmab->dev.type = SNDRV_DMA_TYPE_DEV_SG_FALLBACK;
509 		return snd_dma_sg_fallback_alloc(dmab, size);
510 #else
511 		return NULL;
512 #endif
513 	}
514 
515 	dmab->dev.need_sync = dma_need_sync(dmab->dev.dev,
516 					    sg_dma_address(sgt->sgl));
517 	p = dma_vmap_noncontiguous(dmab->dev.dev, size, sgt);
518 	if (p)
519 		dmab->private_data = sgt;
520 	else
521 		dma_free_noncontiguous(dmab->dev.dev, size, sgt, dmab->dev.dir);
522 	return p;
523 }
524 
525 static void snd_dma_noncontig_free(struct snd_dma_buffer *dmab)
526 {
527 	dma_vunmap_noncontiguous(dmab->dev.dev, dmab->area);
528 	dma_free_noncontiguous(dmab->dev.dev, dmab->bytes, dmab->private_data,
529 			       dmab->dev.dir);
530 }
531 
532 static int snd_dma_noncontig_mmap(struct snd_dma_buffer *dmab,
533 				  struct vm_area_struct *area)
534 {
535 	return dma_mmap_noncontiguous(dmab->dev.dev, area,
536 				      dmab->bytes, dmab->private_data);
537 }
538 
539 static void snd_dma_noncontig_sync(struct snd_dma_buffer *dmab,
540 				   enum snd_dma_sync_mode mode)
541 {
542 	if (mode == SNDRV_DMA_SYNC_CPU) {
543 		if (dmab->dev.dir == DMA_TO_DEVICE)
544 			return;
545 		invalidate_kernel_vmap_range(dmab->area, dmab->bytes);
546 		dma_sync_sgtable_for_cpu(dmab->dev.dev, dmab->private_data,
547 					 dmab->dev.dir);
548 	} else {
549 		if (dmab->dev.dir == DMA_FROM_DEVICE)
550 			return;
551 		flush_kernel_vmap_range(dmab->area, dmab->bytes);
552 		dma_sync_sgtable_for_device(dmab->dev.dev, dmab->private_data,
553 					    dmab->dev.dir);
554 	}
555 }
556 
557 static inline void snd_dma_noncontig_iter_set(struct snd_dma_buffer *dmab,
558 					      struct sg_page_iter *piter,
559 					      size_t offset)
560 {
561 	struct sg_table *sgt = dmab->private_data;
562 
563 	__sg_page_iter_start(piter, sgt->sgl, sgt->orig_nents,
564 			     offset >> PAGE_SHIFT);
565 }
566 
567 static dma_addr_t snd_dma_noncontig_get_addr(struct snd_dma_buffer *dmab,
568 					     size_t offset)
569 {
570 	struct sg_dma_page_iter iter;
571 
572 	snd_dma_noncontig_iter_set(dmab, &iter.base, offset);
573 	__sg_page_iter_dma_next(&iter);
574 	return sg_page_iter_dma_address(&iter) + offset % PAGE_SIZE;
575 }
576 
577 static struct page *snd_dma_noncontig_get_page(struct snd_dma_buffer *dmab,
578 					       size_t offset)
579 {
580 	struct sg_page_iter iter;
581 
582 	snd_dma_noncontig_iter_set(dmab, &iter, offset);
583 	__sg_page_iter_next(&iter);
584 	return sg_page_iter_page(&iter);
585 }
586 
587 static unsigned int
588 snd_dma_noncontig_get_chunk_size(struct snd_dma_buffer *dmab,
589 				 unsigned int ofs, unsigned int size)
590 {
591 	struct sg_dma_page_iter iter;
592 	unsigned int start, end;
593 	unsigned long addr;
594 
595 	start = ALIGN_DOWN(ofs, PAGE_SIZE);
596 	end = ofs + size - 1; /* the last byte address */
597 	snd_dma_noncontig_iter_set(dmab, &iter.base, start);
598 	if (!__sg_page_iter_dma_next(&iter))
599 		return 0;
600 	/* check page continuity */
601 	addr = sg_page_iter_dma_address(&iter);
602 	for (;;) {
603 		start += PAGE_SIZE;
604 		if (start > end)
605 			break;
606 		addr += PAGE_SIZE;
607 		if (!__sg_page_iter_dma_next(&iter) ||
608 		    sg_page_iter_dma_address(&iter) != addr)
609 			return start - ofs;
610 	}
611 	/* ok, all on continuous pages */
612 	return size;
613 }
614 
615 static const struct snd_malloc_ops snd_dma_noncontig_ops = {
616 	.alloc = snd_dma_noncontig_alloc,
617 	.free = snd_dma_noncontig_free,
618 	.mmap = snd_dma_noncontig_mmap,
619 	.sync = snd_dma_noncontig_sync,
620 	.get_addr = snd_dma_noncontig_get_addr,
621 	.get_page = snd_dma_noncontig_get_page,
622 	.get_chunk_size = snd_dma_noncontig_get_chunk_size,
623 };
624 
625 /* x86-specific SG-buffer with WC pages */
626 #ifdef CONFIG_SND_DMA_SGBUF
627 #define sg_wc_address(it) ((unsigned long)page_address(sg_page_iter_page(it)))
628 
629 static void *snd_dma_sg_wc_alloc(struct snd_dma_buffer *dmab, size_t size)
630 {
631 	void *p = snd_dma_noncontig_alloc(dmab, size);
632 	struct sg_table *sgt = dmab->private_data;
633 	struct sg_page_iter iter;
634 
635 	if (!p)
636 		return NULL;
637 	if (dmab->dev.type != SNDRV_DMA_TYPE_DEV_WC_SG)
638 		return p;
639 	for_each_sgtable_page(sgt, &iter, 0)
640 		set_memory_wc(sg_wc_address(&iter), 1);
641 	return p;
642 }
643 
644 static void snd_dma_sg_wc_free(struct snd_dma_buffer *dmab)
645 {
646 	struct sg_table *sgt = dmab->private_data;
647 	struct sg_page_iter iter;
648 
649 	for_each_sgtable_page(sgt, &iter, 0)
650 		set_memory_wb(sg_wc_address(&iter), 1);
651 	snd_dma_noncontig_free(dmab);
652 }
653 
654 static int snd_dma_sg_wc_mmap(struct snd_dma_buffer *dmab,
655 			      struct vm_area_struct *area)
656 {
657 	area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
658 	return dma_mmap_noncontiguous(dmab->dev.dev, area,
659 				      dmab->bytes, dmab->private_data);
660 }
661 
662 static const struct snd_malloc_ops snd_dma_sg_wc_ops = {
663 	.alloc = snd_dma_sg_wc_alloc,
664 	.free = snd_dma_sg_wc_free,
665 	.mmap = snd_dma_sg_wc_mmap,
666 	.sync = snd_dma_noncontig_sync,
667 	.get_addr = snd_dma_noncontig_get_addr,
668 	.get_page = snd_dma_noncontig_get_page,
669 	.get_chunk_size = snd_dma_noncontig_get_chunk_size,
670 };
671 
672 /* Fallback SG-buffer allocations for x86 */
673 struct snd_dma_sg_fallback {
674 	size_t count;
675 	struct page **pages;
676 	dma_addr_t *addrs;
677 };
678 
679 static void __snd_dma_sg_fallback_free(struct snd_dma_buffer *dmab,
680 				       struct snd_dma_sg_fallback *sgbuf)
681 {
682 	size_t i;
683 
684 	if (sgbuf->count && dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK)
685 		set_pages_array_wb(sgbuf->pages, sgbuf->count);
686 	for (i = 0; i < sgbuf->count && sgbuf->pages[i]; i++)
687 		dma_free_coherent(dmab->dev.dev, PAGE_SIZE,
688 				  page_address(sgbuf->pages[i]),
689 				  sgbuf->addrs[i]);
690 	kvfree(sgbuf->pages);
691 	kvfree(sgbuf->addrs);
692 	kfree(sgbuf);
693 }
694 
695 static void *snd_dma_sg_fallback_alloc(struct snd_dma_buffer *dmab, size_t size)
696 {
697 	struct snd_dma_sg_fallback *sgbuf;
698 	struct page **pages;
699 	size_t i, count;
700 	void *p;
701 
702 	sgbuf = kzalloc(sizeof(*sgbuf), GFP_KERNEL);
703 	if (!sgbuf)
704 		return NULL;
705 	count = PAGE_ALIGN(size) >> PAGE_SHIFT;
706 	pages = kvcalloc(count, sizeof(*pages), GFP_KERNEL);
707 	if (!pages)
708 		goto error;
709 	sgbuf->pages = pages;
710 	sgbuf->addrs = kvcalloc(count, sizeof(*sgbuf->addrs), GFP_KERNEL);
711 	if (!sgbuf->addrs)
712 		goto error;
713 
714 	for (i = 0; i < count; sgbuf->count++, i++) {
715 		p = dma_alloc_coherent(dmab->dev.dev, PAGE_SIZE,
716 				       &sgbuf->addrs[i], DEFAULT_GFP);
717 		if (!p)
718 			goto error;
719 		sgbuf->pages[i] = virt_to_page(p);
720 	}
721 
722 	if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK)
723 		set_pages_array_wc(pages, count);
724 	p = vmap(pages, count, VM_MAP, PAGE_KERNEL);
725 	if (!p)
726 		goto error;
727 	dmab->private_data = sgbuf;
728 	return p;
729 
730  error:
731 	__snd_dma_sg_fallback_free(dmab, sgbuf);
732 	return NULL;
733 }
734 
735 static void snd_dma_sg_fallback_free(struct snd_dma_buffer *dmab)
736 {
737 	vunmap(dmab->area);
738 	__snd_dma_sg_fallback_free(dmab, dmab->private_data);
739 }
740 
741 static int snd_dma_sg_fallback_mmap(struct snd_dma_buffer *dmab,
742 				    struct vm_area_struct *area)
743 {
744 	struct snd_dma_sg_fallback *sgbuf = dmab->private_data;
745 
746 	if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK)
747 		area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
748 	return vm_map_pages(area, sgbuf->pages, sgbuf->count);
749 }
750 
751 static const struct snd_malloc_ops snd_dma_sg_fallback_ops = {
752 	.alloc = snd_dma_sg_fallback_alloc,
753 	.free = snd_dma_sg_fallback_free,
754 	.mmap = snd_dma_sg_fallback_mmap,
755 	/* reuse vmalloc helpers */
756 	.get_addr = snd_dma_vmalloc_get_addr,
757 	.get_page = snd_dma_vmalloc_get_page,
758 	.get_chunk_size = snd_dma_vmalloc_get_chunk_size,
759 };
760 #endif /* CONFIG_SND_DMA_SGBUF */
761 
762 /*
763  * Non-coherent pages allocator
764  */
765 static void *snd_dma_noncoherent_alloc(struct snd_dma_buffer *dmab, size_t size)
766 {
767 	void *p;
768 
769 	p = dma_alloc_noncoherent(dmab->dev.dev, size, &dmab->addr,
770 				  dmab->dev.dir, DEFAULT_GFP);
771 	if (p)
772 		dmab->dev.need_sync = dma_need_sync(dmab->dev.dev, dmab->addr);
773 	return p;
774 }
775 
776 static void snd_dma_noncoherent_free(struct snd_dma_buffer *dmab)
777 {
778 	dma_free_noncoherent(dmab->dev.dev, dmab->bytes, dmab->area,
779 			     dmab->addr, dmab->dev.dir);
780 }
781 
782 static int snd_dma_noncoherent_mmap(struct snd_dma_buffer *dmab,
783 				    struct vm_area_struct *area)
784 {
785 	area->vm_page_prot = vm_get_page_prot(area->vm_flags);
786 	return dma_mmap_pages(dmab->dev.dev, area,
787 			      area->vm_end - area->vm_start,
788 			      virt_to_page(dmab->area));
789 }
790 
791 static void snd_dma_noncoherent_sync(struct snd_dma_buffer *dmab,
792 				     enum snd_dma_sync_mode mode)
793 {
794 	if (mode == SNDRV_DMA_SYNC_CPU) {
795 		if (dmab->dev.dir != DMA_TO_DEVICE)
796 			dma_sync_single_for_cpu(dmab->dev.dev, dmab->addr,
797 						dmab->bytes, dmab->dev.dir);
798 	} else {
799 		if (dmab->dev.dir != DMA_FROM_DEVICE)
800 			dma_sync_single_for_device(dmab->dev.dev, dmab->addr,
801 						   dmab->bytes, dmab->dev.dir);
802 	}
803 }
804 
805 static const struct snd_malloc_ops snd_dma_noncoherent_ops = {
806 	.alloc = snd_dma_noncoherent_alloc,
807 	.free = snd_dma_noncoherent_free,
808 	.mmap = snd_dma_noncoherent_mmap,
809 	.sync = snd_dma_noncoherent_sync,
810 };
811 
812 #endif /* CONFIG_HAS_DMA */
813 
814 /*
815  * Entry points
816  */
817 static const struct snd_malloc_ops *dma_ops[] = {
818 	[SNDRV_DMA_TYPE_CONTINUOUS] = &snd_dma_continuous_ops,
819 	[SNDRV_DMA_TYPE_VMALLOC] = &snd_dma_vmalloc_ops,
820 #ifdef CONFIG_HAS_DMA
821 	[SNDRV_DMA_TYPE_DEV] = &snd_dma_dev_ops,
822 	[SNDRV_DMA_TYPE_DEV_WC] = &snd_dma_wc_ops,
823 	[SNDRV_DMA_TYPE_NONCONTIG] = &snd_dma_noncontig_ops,
824 	[SNDRV_DMA_TYPE_NONCOHERENT] = &snd_dma_noncoherent_ops,
825 #ifdef CONFIG_SND_DMA_SGBUF
826 	[SNDRV_DMA_TYPE_DEV_WC_SG] = &snd_dma_sg_wc_ops,
827 #endif
828 #ifdef CONFIG_GENERIC_ALLOCATOR
829 	[SNDRV_DMA_TYPE_DEV_IRAM] = &snd_dma_iram_ops,
830 #endif /* CONFIG_GENERIC_ALLOCATOR */
831 #ifdef CONFIG_SND_DMA_SGBUF
832 	[SNDRV_DMA_TYPE_DEV_SG_FALLBACK] = &snd_dma_sg_fallback_ops,
833 	[SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK] = &snd_dma_sg_fallback_ops,
834 #endif
835 #endif /* CONFIG_HAS_DMA */
836 };
837 
838 static const struct snd_malloc_ops *snd_dma_get_ops(struct snd_dma_buffer *dmab)
839 {
840 	if (WARN_ON_ONCE(!dmab))
841 		return NULL;
842 	if (WARN_ON_ONCE(dmab->dev.type <= SNDRV_DMA_TYPE_UNKNOWN ||
843 			 dmab->dev.type >= ARRAY_SIZE(dma_ops)))
844 		return NULL;
845 	return dma_ops[dmab->dev.type];
846 }
847