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