xref: /linux/sound/core/memalloc.c (revision dbb60c8a26daf388f183f599e1e96de5bb9f96e1)
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/dma-map-ops.h>
13 #include <linux/genalloc.h>
14 #include <linux/highmem.h>
15 #include <linux/vmalloc.h>
16 #ifdef CONFIG_X86
17 #include <asm/set_memory.h>
18 #endif
19 #include <sound/memalloc.h>
20 #include "memalloc_local.h"
21 
22 #define DEFAULT_GFP \
23 	(GFP_KERNEL | \
24 	 __GFP_COMP |    /* compound page lets parts be mapped */ \
25 	 __GFP_RETRY_MAYFAIL | /* don't trigger OOM-killer */ \
26 	 __GFP_NOWARN)   /* no stack trace print - this call is non-critical */
27 
28 static const struct snd_malloc_ops *snd_dma_get_ops(struct snd_dma_buffer *dmab);
29 
30 #ifdef CONFIG_SND_DMA_SGBUF
31 static void *snd_dma_sg_fallback_alloc(struct snd_dma_buffer *dmab, size_t size);
32 #endif
33 
34 static void *__snd_dma_alloc_pages(struct snd_dma_buffer *dmab, size_t size)
35 {
36 	const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
37 
38 	if (WARN_ON_ONCE(!ops || !ops->alloc))
39 		return NULL;
40 	return ops->alloc(dmab, size);
41 }
42 
43 /**
44  * snd_dma_alloc_dir_pages - allocate the buffer area according to the given
45  *	type and direction
46  * @type: the DMA buffer type
47  * @device: the device pointer
48  * @dir: DMA direction
49  * @size: the buffer size to allocate
50  * @dmab: buffer allocation record to store the allocated data
51  *
52  * Calls the memory-allocator function for the corresponding
53  * buffer type.
54  *
55  * Return: Zero if the buffer with the given size is allocated successfully,
56  * otherwise a negative value on error.
57  */
58 int snd_dma_alloc_dir_pages(int type, struct device *device,
59 			    enum dma_data_direction dir, size_t size,
60 			    struct snd_dma_buffer *dmab)
61 {
62 	if (WARN_ON(!size))
63 		return -ENXIO;
64 	if (WARN_ON(!dmab))
65 		return -ENXIO;
66 
67 	size = PAGE_ALIGN(size);
68 	dmab->dev.type = type;
69 	dmab->dev.dev = device;
70 	dmab->dev.dir = dir;
71 	dmab->bytes = 0;
72 	dmab->addr = 0;
73 	dmab->private_data = NULL;
74 	dmab->area = __snd_dma_alloc_pages(dmab, size);
75 	if (!dmab->area)
76 		return -ENOMEM;
77 	dmab->bytes = size;
78 	return 0;
79 }
80 EXPORT_SYMBOL(snd_dma_alloc_dir_pages);
81 
82 /**
83  * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
84  * @type: the DMA buffer type
85  * @device: the device pointer
86  * @size: the buffer size to allocate
87  * @dmab: buffer allocation record to store the allocated data
88  *
89  * Calls the memory-allocator function for the corresponding
90  * buffer type.  When no space is left, this function reduces the size and
91  * tries to allocate again.  The size actually allocated is stored in
92  * res_size argument.
93  *
94  * Return: Zero if the buffer with the given size is allocated successfully,
95  * otherwise a negative value on error.
96  */
97 int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
98 				 struct snd_dma_buffer *dmab)
99 {
100 	int err;
101 
102 	while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
103 		if (err != -ENOMEM)
104 			return err;
105 		if (size <= PAGE_SIZE)
106 			return -ENOMEM;
107 		size >>= 1;
108 		size = PAGE_SIZE << get_order(size);
109 	}
110 	if (! dmab->area)
111 		return -ENOMEM;
112 	return 0;
113 }
114 EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
115 
116 /**
117  * snd_dma_free_pages - release the allocated buffer
118  * @dmab: the buffer allocation record to release
119  *
120  * Releases the allocated buffer via snd_dma_alloc_pages().
121  */
122 void snd_dma_free_pages(struct snd_dma_buffer *dmab)
123 {
124 	const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
125 
126 	if (ops && ops->free)
127 		ops->free(dmab);
128 }
129 EXPORT_SYMBOL(snd_dma_free_pages);
130 
131 /* called by devres */
132 static void __snd_release_pages(struct device *dev, void *res)
133 {
134 	snd_dma_free_pages(res);
135 }
136 
137 /**
138  * snd_devm_alloc_dir_pages - allocate the buffer and manage with devres
139  * @dev: the device pointer
140  * @type: the DMA buffer type
141  * @dir: DMA direction
142  * @size: the buffer size to allocate
143  *
144  * Allocate buffer pages depending on the given type and manage using devres.
145  * The pages will be released automatically at the device removal.
146  *
147  * Unlike snd_dma_alloc_pages(), this function requires the real device pointer,
148  * hence it can't work with SNDRV_DMA_TYPE_CONTINUOUS or
149  * SNDRV_DMA_TYPE_VMALLOC type.
150  *
151  * Return: the snd_dma_buffer object at success, or NULL if failed
152  */
153 struct snd_dma_buffer *
154 snd_devm_alloc_dir_pages(struct device *dev, int type,
155 			 enum dma_data_direction dir, size_t size)
156 {
157 	struct snd_dma_buffer *dmab;
158 	int err;
159 
160 	if (WARN_ON(type == SNDRV_DMA_TYPE_CONTINUOUS ||
161 		    type == SNDRV_DMA_TYPE_VMALLOC))
162 		return NULL;
163 
164 	dmab = devres_alloc(__snd_release_pages, sizeof(*dmab), GFP_KERNEL);
165 	if (!dmab)
166 		return NULL;
167 
168 	err = snd_dma_alloc_dir_pages(type, dev, dir, size, dmab);
169 	if (err < 0) {
170 		devres_free(dmab);
171 		return NULL;
172 	}
173 
174 	devres_add(dev, dmab);
175 	return dmab;
176 }
177 EXPORT_SYMBOL_GPL(snd_devm_alloc_dir_pages);
178 
179 /**
180  * snd_dma_buffer_mmap - perform mmap of the given DMA buffer
181  * @dmab: buffer allocation information
182  * @area: VM area information
183  *
184  * Return: zero if successful, or a negative error code
185  */
186 int snd_dma_buffer_mmap(struct snd_dma_buffer *dmab,
187 			struct vm_area_struct *area)
188 {
189 	const struct snd_malloc_ops *ops;
190 
191 	if (!dmab)
192 		return -ENOENT;
193 	ops = snd_dma_get_ops(dmab);
194 	if (ops && ops->mmap)
195 		return ops->mmap(dmab, area);
196 	else
197 		return -ENOENT;
198 }
199 EXPORT_SYMBOL(snd_dma_buffer_mmap);
200 
201 #ifdef CONFIG_HAS_DMA
202 /**
203  * snd_dma_buffer_sync - sync DMA buffer between CPU and device
204  * @dmab: buffer allocation information
205  * @mode: sync mode
206  */
207 void snd_dma_buffer_sync(struct snd_dma_buffer *dmab,
208 			 enum snd_dma_sync_mode mode)
209 {
210 	const struct snd_malloc_ops *ops;
211 
212 	if (!dmab || !dmab->dev.need_sync)
213 		return;
214 	ops = snd_dma_get_ops(dmab);
215 	if (ops && ops->sync)
216 		ops->sync(dmab, mode);
217 }
218 EXPORT_SYMBOL_GPL(snd_dma_buffer_sync);
219 #endif /* CONFIG_HAS_DMA */
220 
221 /**
222  * snd_sgbuf_get_addr - return the physical address at the corresponding offset
223  * @dmab: buffer allocation information
224  * @offset: offset in the ring buffer
225  *
226  * Return: the physical address
227  */
228 dma_addr_t snd_sgbuf_get_addr(struct snd_dma_buffer *dmab, size_t offset)
229 {
230 	const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
231 
232 	if (ops && ops->get_addr)
233 		return ops->get_addr(dmab, offset);
234 	else
235 		return dmab->addr + offset;
236 }
237 EXPORT_SYMBOL(snd_sgbuf_get_addr);
238 
239 /**
240  * snd_sgbuf_get_page - return the physical page at the corresponding offset
241  * @dmab: buffer allocation information
242  * @offset: offset in the ring buffer
243  *
244  * Return: the page pointer
245  */
246 struct page *snd_sgbuf_get_page(struct snd_dma_buffer *dmab, size_t offset)
247 {
248 	const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
249 
250 	if (ops && ops->get_page)
251 		return ops->get_page(dmab, offset);
252 	else
253 		return virt_to_page(dmab->area + offset);
254 }
255 EXPORT_SYMBOL(snd_sgbuf_get_page);
256 
257 /**
258  * snd_sgbuf_get_chunk_size - compute the max chunk size with continuous pages
259  *	on sg-buffer
260  * @dmab: buffer allocation information
261  * @ofs: offset in the ring buffer
262  * @size: the requested size
263  *
264  * Return: the chunk size
265  */
266 unsigned int snd_sgbuf_get_chunk_size(struct snd_dma_buffer *dmab,
267 				      unsigned int ofs, unsigned int size)
268 {
269 	const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
270 
271 	if (ops && ops->get_chunk_size)
272 		return ops->get_chunk_size(dmab, ofs, size);
273 	else
274 		return size;
275 }
276 EXPORT_SYMBOL(snd_sgbuf_get_chunk_size);
277 
278 /*
279  * Continuous pages allocator
280  */
281 static void *do_alloc_pages(struct device *dev, size_t size, dma_addr_t *addr,
282 			    bool wc)
283 {
284 	void *p;
285 	gfp_t gfp = GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN;
286 
287  again:
288 	p = alloc_pages_exact(size, gfp);
289 	if (!p)
290 		return NULL;
291 	*addr = page_to_phys(virt_to_page(p));
292 	if (!dev)
293 		return p;
294 	if ((*addr + size - 1) & ~dev->coherent_dma_mask) {
295 		if (IS_ENABLED(CONFIG_ZONE_DMA32) && !(gfp & GFP_DMA32)) {
296 			gfp |= GFP_DMA32;
297 			goto again;
298 		}
299 		if (IS_ENABLED(CONFIG_ZONE_DMA) && !(gfp & GFP_DMA)) {
300 			gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
301 			goto again;
302 		}
303 	}
304 #ifdef CONFIG_X86
305 	if (wc)
306 		set_memory_wc((unsigned long)(p), size >> PAGE_SHIFT);
307 #endif
308 	return p;
309 }
310 
311 static void do_free_pages(void *p, size_t size, bool wc)
312 {
313 #ifdef CONFIG_X86
314 	if (wc)
315 		set_memory_wb((unsigned long)(p), size >> PAGE_SHIFT);
316 #endif
317 	free_pages_exact(p, size);
318 }
319 
320 
321 static void *snd_dma_continuous_alloc(struct snd_dma_buffer *dmab, size_t size)
322 {
323 	return do_alloc_pages(dmab->dev.dev, size, &dmab->addr, false);
324 }
325 
326 static void snd_dma_continuous_free(struct snd_dma_buffer *dmab)
327 {
328 	do_free_pages(dmab->area, dmab->bytes, false);
329 }
330 
331 static int snd_dma_continuous_mmap(struct snd_dma_buffer *dmab,
332 				   struct vm_area_struct *area)
333 {
334 	return remap_pfn_range(area, area->vm_start,
335 			       dmab->addr >> PAGE_SHIFT,
336 			       area->vm_end - area->vm_start,
337 			       area->vm_page_prot);
338 }
339 
340 static const struct snd_malloc_ops snd_dma_continuous_ops = {
341 	.alloc = snd_dma_continuous_alloc,
342 	.free = snd_dma_continuous_free,
343 	.mmap = snd_dma_continuous_mmap,
344 };
345 
346 /*
347  * VMALLOC allocator
348  */
349 static void *snd_dma_vmalloc_alloc(struct snd_dma_buffer *dmab, size_t size)
350 {
351 	return vmalloc(size);
352 }
353 
354 static void snd_dma_vmalloc_free(struct snd_dma_buffer *dmab)
355 {
356 	vfree(dmab->area);
357 }
358 
359 static int snd_dma_vmalloc_mmap(struct snd_dma_buffer *dmab,
360 				struct vm_area_struct *area)
361 {
362 	return remap_vmalloc_range(area, dmab->area, 0);
363 }
364 
365 #define get_vmalloc_page_addr(dmab, offset) \
366 	page_to_phys(vmalloc_to_page((dmab)->area + (offset)))
367 
368 static dma_addr_t snd_dma_vmalloc_get_addr(struct snd_dma_buffer *dmab,
369 					   size_t offset)
370 {
371 	return get_vmalloc_page_addr(dmab, offset) + offset % PAGE_SIZE;
372 }
373 
374 static struct page *snd_dma_vmalloc_get_page(struct snd_dma_buffer *dmab,
375 					     size_t offset)
376 {
377 	return vmalloc_to_page(dmab->area + offset);
378 }
379 
380 static unsigned int
381 snd_dma_vmalloc_get_chunk_size(struct snd_dma_buffer *dmab,
382 			       unsigned int ofs, unsigned int size)
383 {
384 	unsigned int start, end;
385 	unsigned long addr;
386 
387 	start = ALIGN_DOWN(ofs, PAGE_SIZE);
388 	end = ofs + size - 1; /* the last byte address */
389 	/* check page continuity */
390 	addr = get_vmalloc_page_addr(dmab, start);
391 	for (;;) {
392 		start += PAGE_SIZE;
393 		if (start > end)
394 			break;
395 		addr += PAGE_SIZE;
396 		if (get_vmalloc_page_addr(dmab, start) != addr)
397 			return start - ofs;
398 	}
399 	/* ok, all on continuous pages */
400 	return size;
401 }
402 
403 static const struct snd_malloc_ops snd_dma_vmalloc_ops = {
404 	.alloc = snd_dma_vmalloc_alloc,
405 	.free = snd_dma_vmalloc_free,
406 	.mmap = snd_dma_vmalloc_mmap,
407 	.get_addr = snd_dma_vmalloc_get_addr,
408 	.get_page = snd_dma_vmalloc_get_page,
409 	.get_chunk_size = snd_dma_vmalloc_get_chunk_size,
410 };
411 
412 #ifdef CONFIG_HAS_DMA
413 /*
414  * IRAM allocator
415  */
416 #ifdef CONFIG_GENERIC_ALLOCATOR
417 static void *snd_dma_iram_alloc(struct snd_dma_buffer *dmab, size_t size)
418 {
419 	struct device *dev = dmab->dev.dev;
420 	struct gen_pool *pool;
421 	void *p;
422 
423 	if (dev->of_node) {
424 		pool = of_gen_pool_get(dev->of_node, "iram", 0);
425 		/* Assign the pool into private_data field */
426 		dmab->private_data = pool;
427 
428 		p = gen_pool_dma_alloc_align(pool, size, &dmab->addr, PAGE_SIZE);
429 		if (p)
430 			return p;
431 	}
432 
433 	/* Internal memory might have limited size and no enough space,
434 	 * so if we fail to malloc, try to fetch memory traditionally.
435 	 */
436 	dmab->dev.type = SNDRV_DMA_TYPE_DEV;
437 	return __snd_dma_alloc_pages(dmab, size);
438 }
439 
440 static void snd_dma_iram_free(struct snd_dma_buffer *dmab)
441 {
442 	struct gen_pool *pool = dmab->private_data;
443 
444 	if (pool && dmab->area)
445 		gen_pool_free(pool, (unsigned long)dmab->area, dmab->bytes);
446 }
447 
448 static int snd_dma_iram_mmap(struct snd_dma_buffer *dmab,
449 			     struct vm_area_struct *area)
450 {
451 	area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
452 	return remap_pfn_range(area, area->vm_start,
453 			       dmab->addr >> PAGE_SHIFT,
454 			       area->vm_end - area->vm_start,
455 			       area->vm_page_prot);
456 }
457 
458 static const struct snd_malloc_ops snd_dma_iram_ops = {
459 	.alloc = snd_dma_iram_alloc,
460 	.free = snd_dma_iram_free,
461 	.mmap = snd_dma_iram_mmap,
462 };
463 #endif /* CONFIG_GENERIC_ALLOCATOR */
464 
465 /*
466  * Coherent device pages allocator
467  */
468 static void *snd_dma_dev_alloc(struct snd_dma_buffer *dmab, size_t size)
469 {
470 	return dma_alloc_coherent(dmab->dev.dev, size, &dmab->addr, DEFAULT_GFP);
471 }
472 
473 static void snd_dma_dev_free(struct snd_dma_buffer *dmab)
474 {
475 	dma_free_coherent(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
476 }
477 
478 static int snd_dma_dev_mmap(struct snd_dma_buffer *dmab,
479 			    struct vm_area_struct *area)
480 {
481 	return dma_mmap_coherent(dmab->dev.dev, area,
482 				 dmab->area, dmab->addr, dmab->bytes);
483 }
484 
485 static const struct snd_malloc_ops snd_dma_dev_ops = {
486 	.alloc = snd_dma_dev_alloc,
487 	.free = snd_dma_dev_free,
488 	.mmap = snd_dma_dev_mmap,
489 };
490 
491 /*
492  * Write-combined pages
493  */
494 /* x86-specific allocations */
495 #ifdef CONFIG_SND_DMA_SGBUF
496 static void *snd_dma_wc_alloc(struct snd_dma_buffer *dmab, size_t size)
497 {
498 	return do_alloc_pages(dmab->dev.dev, size, &dmab->addr, true);
499 }
500 
501 static void snd_dma_wc_free(struct snd_dma_buffer *dmab)
502 {
503 	do_free_pages(dmab->area, dmab->bytes, true);
504 }
505 
506 static int snd_dma_wc_mmap(struct snd_dma_buffer *dmab,
507 			   struct vm_area_struct *area)
508 {
509 	area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
510 	return snd_dma_continuous_mmap(dmab, area);
511 }
512 #else
513 static void *snd_dma_wc_alloc(struct snd_dma_buffer *dmab, size_t size)
514 {
515 	return dma_alloc_wc(dmab->dev.dev, size, &dmab->addr, DEFAULT_GFP);
516 }
517 
518 static void snd_dma_wc_free(struct snd_dma_buffer *dmab)
519 {
520 	dma_free_wc(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
521 }
522 
523 static int snd_dma_wc_mmap(struct snd_dma_buffer *dmab,
524 			   struct vm_area_struct *area)
525 {
526 	return dma_mmap_wc(dmab->dev.dev, area,
527 			   dmab->area, dmab->addr, dmab->bytes);
528 }
529 #endif /* CONFIG_SND_DMA_SGBUF */
530 
531 static const struct snd_malloc_ops snd_dma_wc_ops = {
532 	.alloc = snd_dma_wc_alloc,
533 	.free = snd_dma_wc_free,
534 	.mmap = snd_dma_wc_mmap,
535 };
536 
537 /*
538  * Non-contiguous pages allocator
539  */
540 static void *snd_dma_noncontig_alloc(struct snd_dma_buffer *dmab, size_t size)
541 {
542 	struct sg_table *sgt;
543 	void *p;
544 
545 #ifdef CONFIG_SND_DMA_SGBUF
546 	if (!get_dma_ops(dmab->dev.dev)) {
547 		if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG)
548 			dmab->dev.type = SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK;
549 		else
550 			dmab->dev.type = SNDRV_DMA_TYPE_DEV_SG_FALLBACK;
551 		return snd_dma_sg_fallback_alloc(dmab, size);
552 	}
553 #endif
554 
555 	sgt = dma_alloc_noncontiguous(dmab->dev.dev, size, dmab->dev.dir,
556 				      DEFAULT_GFP, 0);
557 	if (!sgt)
558 		return NULL;
559 
560 	dmab->dev.need_sync = dma_need_sync(dmab->dev.dev,
561 					    sg_dma_address(sgt->sgl));
562 	p = dma_vmap_noncontiguous(dmab->dev.dev, size, sgt);
563 	if (p) {
564 		dmab->private_data = sgt;
565 		/* store the first page address for convenience */
566 		dmab->addr = snd_sgbuf_get_addr(dmab, 0);
567 	} else {
568 		dma_free_noncontiguous(dmab->dev.dev, size, sgt, dmab->dev.dir);
569 	}
570 	return p;
571 }
572 
573 static void snd_dma_noncontig_free(struct snd_dma_buffer *dmab)
574 {
575 	dma_vunmap_noncontiguous(dmab->dev.dev, dmab->area);
576 	dma_free_noncontiguous(dmab->dev.dev, dmab->bytes, dmab->private_data,
577 			       dmab->dev.dir);
578 }
579 
580 static int snd_dma_noncontig_mmap(struct snd_dma_buffer *dmab,
581 				  struct vm_area_struct *area)
582 {
583 	return dma_mmap_noncontiguous(dmab->dev.dev, area,
584 				      dmab->bytes, dmab->private_data);
585 }
586 
587 static void snd_dma_noncontig_sync(struct snd_dma_buffer *dmab,
588 				   enum snd_dma_sync_mode mode)
589 {
590 	if (mode == SNDRV_DMA_SYNC_CPU) {
591 		if (dmab->dev.dir == DMA_TO_DEVICE)
592 			return;
593 		invalidate_kernel_vmap_range(dmab->area, dmab->bytes);
594 		dma_sync_sgtable_for_cpu(dmab->dev.dev, dmab->private_data,
595 					 dmab->dev.dir);
596 	} else {
597 		if (dmab->dev.dir == DMA_FROM_DEVICE)
598 			return;
599 		flush_kernel_vmap_range(dmab->area, dmab->bytes);
600 		dma_sync_sgtable_for_device(dmab->dev.dev, dmab->private_data,
601 					    dmab->dev.dir);
602 	}
603 }
604 
605 static inline void snd_dma_noncontig_iter_set(struct snd_dma_buffer *dmab,
606 					      struct sg_page_iter *piter,
607 					      size_t offset)
608 {
609 	struct sg_table *sgt = dmab->private_data;
610 
611 	__sg_page_iter_start(piter, sgt->sgl, sgt->orig_nents,
612 			     offset >> PAGE_SHIFT);
613 }
614 
615 static dma_addr_t snd_dma_noncontig_get_addr(struct snd_dma_buffer *dmab,
616 					     size_t offset)
617 {
618 	struct sg_dma_page_iter iter;
619 
620 	snd_dma_noncontig_iter_set(dmab, &iter.base, offset);
621 	__sg_page_iter_dma_next(&iter);
622 	return sg_page_iter_dma_address(&iter) + offset % PAGE_SIZE;
623 }
624 
625 static struct page *snd_dma_noncontig_get_page(struct snd_dma_buffer *dmab,
626 					       size_t offset)
627 {
628 	struct sg_page_iter iter;
629 
630 	snd_dma_noncontig_iter_set(dmab, &iter, offset);
631 	__sg_page_iter_next(&iter);
632 	return sg_page_iter_page(&iter);
633 }
634 
635 static unsigned int
636 snd_dma_noncontig_get_chunk_size(struct snd_dma_buffer *dmab,
637 				 unsigned int ofs, unsigned int size)
638 {
639 	struct sg_dma_page_iter iter;
640 	unsigned int start, end;
641 	unsigned long addr;
642 
643 	start = ALIGN_DOWN(ofs, PAGE_SIZE);
644 	end = ofs + size - 1; /* the last byte address */
645 	snd_dma_noncontig_iter_set(dmab, &iter.base, start);
646 	if (!__sg_page_iter_dma_next(&iter))
647 		return 0;
648 	/* check page continuity */
649 	addr = sg_page_iter_dma_address(&iter);
650 	for (;;) {
651 		start += PAGE_SIZE;
652 		if (start > end)
653 			break;
654 		addr += PAGE_SIZE;
655 		if (!__sg_page_iter_dma_next(&iter) ||
656 		    sg_page_iter_dma_address(&iter) != addr)
657 			return start - ofs;
658 	}
659 	/* ok, all on continuous pages */
660 	return size;
661 }
662 
663 static const struct snd_malloc_ops snd_dma_noncontig_ops = {
664 	.alloc = snd_dma_noncontig_alloc,
665 	.free = snd_dma_noncontig_free,
666 	.mmap = snd_dma_noncontig_mmap,
667 	.sync = snd_dma_noncontig_sync,
668 	.get_addr = snd_dma_noncontig_get_addr,
669 	.get_page = snd_dma_noncontig_get_page,
670 	.get_chunk_size = snd_dma_noncontig_get_chunk_size,
671 };
672 
673 /* x86-specific SG-buffer with WC pages */
674 #ifdef CONFIG_SND_DMA_SGBUF
675 #define sg_wc_address(it) ((unsigned long)page_address(sg_page_iter_page(it)))
676 
677 static void *snd_dma_sg_wc_alloc(struct snd_dma_buffer *dmab, size_t size)
678 {
679 	void *p = snd_dma_noncontig_alloc(dmab, size);
680 	struct sg_table *sgt = dmab->private_data;
681 	struct sg_page_iter iter;
682 
683 	if (!p)
684 		return NULL;
685 	if (dmab->dev.type != SNDRV_DMA_TYPE_DEV_WC_SG)
686 		return p;
687 	for_each_sgtable_page(sgt, &iter, 0)
688 		set_memory_wc(sg_wc_address(&iter), 1);
689 	return p;
690 }
691 
692 static void snd_dma_sg_wc_free(struct snd_dma_buffer *dmab)
693 {
694 	struct sg_table *sgt = dmab->private_data;
695 	struct sg_page_iter iter;
696 
697 	for_each_sgtable_page(sgt, &iter, 0)
698 		set_memory_wb(sg_wc_address(&iter), 1);
699 	snd_dma_noncontig_free(dmab);
700 }
701 
702 static int snd_dma_sg_wc_mmap(struct snd_dma_buffer *dmab,
703 			      struct vm_area_struct *area)
704 {
705 	area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
706 	return dma_mmap_noncontiguous(dmab->dev.dev, area,
707 				      dmab->bytes, dmab->private_data);
708 }
709 
710 static const struct snd_malloc_ops snd_dma_sg_wc_ops = {
711 	.alloc = snd_dma_sg_wc_alloc,
712 	.free = snd_dma_sg_wc_free,
713 	.mmap = snd_dma_sg_wc_mmap,
714 	.sync = snd_dma_noncontig_sync,
715 	.get_addr = snd_dma_noncontig_get_addr,
716 	.get_page = snd_dma_noncontig_get_page,
717 	.get_chunk_size = snd_dma_noncontig_get_chunk_size,
718 };
719 
720 /* Fallback SG-buffer allocations for x86 */
721 struct snd_dma_sg_fallback {
722 	size_t count;
723 	struct page **pages;
724 	dma_addr_t *addrs;
725 };
726 
727 static void __snd_dma_sg_fallback_free(struct snd_dma_buffer *dmab,
728 				       struct snd_dma_sg_fallback *sgbuf)
729 {
730 	bool wc = dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK;
731 	size_t i;
732 
733 	for (i = 0; i < sgbuf->count && sgbuf->pages[i]; i++)
734 		do_free_pages(page_address(sgbuf->pages[i]), PAGE_SIZE, wc);
735 	kvfree(sgbuf->pages);
736 	kvfree(sgbuf->addrs);
737 	kfree(sgbuf);
738 }
739 
740 static void *snd_dma_sg_fallback_alloc(struct snd_dma_buffer *dmab, size_t size)
741 {
742 	struct snd_dma_sg_fallback *sgbuf;
743 	struct page **pages;
744 	size_t i, count;
745 	void *p;
746 	bool wc = dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK;
747 
748 	sgbuf = kzalloc(sizeof(*sgbuf), GFP_KERNEL);
749 	if (!sgbuf)
750 		return NULL;
751 	count = PAGE_ALIGN(size) >> PAGE_SHIFT;
752 	pages = kvcalloc(count, sizeof(*pages), GFP_KERNEL);
753 	if (!pages)
754 		goto error;
755 	sgbuf->pages = pages;
756 	sgbuf->addrs = kvcalloc(count, sizeof(*sgbuf->addrs), GFP_KERNEL);
757 	if (!sgbuf->addrs)
758 		goto error;
759 
760 	for (i = 0; i < count; sgbuf->count++, i++) {
761 		p = do_alloc_pages(dmab->dev.dev, PAGE_SIZE, &sgbuf->addrs[i], wc);
762 		if (!p)
763 			goto error;
764 		sgbuf->pages[i] = virt_to_page(p);
765 	}
766 
767 	p = vmap(pages, count, VM_MAP, PAGE_KERNEL);
768 	if (!p)
769 		goto error;
770 	dmab->private_data = sgbuf;
771 	/* store the first page address for convenience */
772 	dmab->addr = snd_sgbuf_get_addr(dmab, 0);
773 	return p;
774 
775  error:
776 	__snd_dma_sg_fallback_free(dmab, sgbuf);
777 	return NULL;
778 }
779 
780 static void snd_dma_sg_fallback_free(struct snd_dma_buffer *dmab)
781 {
782 	vunmap(dmab->area);
783 	__snd_dma_sg_fallback_free(dmab, dmab->private_data);
784 }
785 
786 static int snd_dma_sg_fallback_mmap(struct snd_dma_buffer *dmab,
787 				    struct vm_area_struct *area)
788 {
789 	struct snd_dma_sg_fallback *sgbuf = dmab->private_data;
790 
791 	if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK)
792 		area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
793 	return vm_map_pages(area, sgbuf->pages, sgbuf->count);
794 }
795 
796 static const struct snd_malloc_ops snd_dma_sg_fallback_ops = {
797 	.alloc = snd_dma_sg_fallback_alloc,
798 	.free = snd_dma_sg_fallback_free,
799 	.mmap = snd_dma_sg_fallback_mmap,
800 	/* reuse vmalloc helpers */
801 	.get_addr = snd_dma_vmalloc_get_addr,
802 	.get_page = snd_dma_vmalloc_get_page,
803 	.get_chunk_size = snd_dma_vmalloc_get_chunk_size,
804 };
805 #endif /* CONFIG_SND_DMA_SGBUF */
806 
807 /*
808  * Non-coherent pages allocator
809  */
810 static void *snd_dma_noncoherent_alloc(struct snd_dma_buffer *dmab, size_t size)
811 {
812 	void *p;
813 
814 	p = dma_alloc_noncoherent(dmab->dev.dev, size, &dmab->addr,
815 				  dmab->dev.dir, DEFAULT_GFP);
816 	if (p)
817 		dmab->dev.need_sync = dma_need_sync(dmab->dev.dev, dmab->addr);
818 	return p;
819 }
820 
821 static void snd_dma_noncoherent_free(struct snd_dma_buffer *dmab)
822 {
823 	dma_free_noncoherent(dmab->dev.dev, dmab->bytes, dmab->area,
824 			     dmab->addr, dmab->dev.dir);
825 }
826 
827 static int snd_dma_noncoherent_mmap(struct snd_dma_buffer *dmab,
828 				    struct vm_area_struct *area)
829 {
830 	area->vm_page_prot = vm_get_page_prot(area->vm_flags);
831 	return dma_mmap_pages(dmab->dev.dev, area,
832 			      area->vm_end - area->vm_start,
833 			      virt_to_page(dmab->area));
834 }
835 
836 static void snd_dma_noncoherent_sync(struct snd_dma_buffer *dmab,
837 				     enum snd_dma_sync_mode mode)
838 {
839 	if (mode == SNDRV_DMA_SYNC_CPU) {
840 		if (dmab->dev.dir != DMA_TO_DEVICE)
841 			dma_sync_single_for_cpu(dmab->dev.dev, dmab->addr,
842 						dmab->bytes, dmab->dev.dir);
843 	} else {
844 		if (dmab->dev.dir != DMA_FROM_DEVICE)
845 			dma_sync_single_for_device(dmab->dev.dev, dmab->addr,
846 						   dmab->bytes, dmab->dev.dir);
847 	}
848 }
849 
850 static const struct snd_malloc_ops snd_dma_noncoherent_ops = {
851 	.alloc = snd_dma_noncoherent_alloc,
852 	.free = snd_dma_noncoherent_free,
853 	.mmap = snd_dma_noncoherent_mmap,
854 	.sync = snd_dma_noncoherent_sync,
855 };
856 
857 #endif /* CONFIG_HAS_DMA */
858 
859 /*
860  * Entry points
861  */
862 static const struct snd_malloc_ops *snd_dma_ops[] = {
863 	[SNDRV_DMA_TYPE_CONTINUOUS] = &snd_dma_continuous_ops,
864 	[SNDRV_DMA_TYPE_VMALLOC] = &snd_dma_vmalloc_ops,
865 #ifdef CONFIG_HAS_DMA
866 	[SNDRV_DMA_TYPE_DEV] = &snd_dma_dev_ops,
867 	[SNDRV_DMA_TYPE_DEV_WC] = &snd_dma_wc_ops,
868 	[SNDRV_DMA_TYPE_NONCONTIG] = &snd_dma_noncontig_ops,
869 	[SNDRV_DMA_TYPE_NONCOHERENT] = &snd_dma_noncoherent_ops,
870 #ifdef CONFIG_SND_DMA_SGBUF
871 	[SNDRV_DMA_TYPE_DEV_WC_SG] = &snd_dma_sg_wc_ops,
872 #endif
873 #ifdef CONFIG_GENERIC_ALLOCATOR
874 	[SNDRV_DMA_TYPE_DEV_IRAM] = &snd_dma_iram_ops,
875 #endif /* CONFIG_GENERIC_ALLOCATOR */
876 #ifdef CONFIG_SND_DMA_SGBUF
877 	[SNDRV_DMA_TYPE_DEV_SG_FALLBACK] = &snd_dma_sg_fallback_ops,
878 	[SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK] = &snd_dma_sg_fallback_ops,
879 #endif
880 #endif /* CONFIG_HAS_DMA */
881 };
882 
883 static const struct snd_malloc_ops *snd_dma_get_ops(struct snd_dma_buffer *dmab)
884 {
885 	if (WARN_ON_ONCE(!dmab))
886 		return NULL;
887 	if (WARN_ON_ONCE(dmab->dev.type <= SNDRV_DMA_TYPE_UNKNOWN ||
888 			 dmab->dev.type >= ARRAY_SIZE(snd_dma_ops)))
889 		return NULL;
890 	return snd_dma_ops[dmab->dev.type];
891 }
892