18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (c) by Jaroslav Kysela <perex@perex.cz> 48c2ecf20Sopenharmony_ci * Takashi Iwai <tiwai@suse.de> 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Generic memory allocators 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/slab.h> 108c2ecf20Sopenharmony_ci#include <linux/mm.h> 118c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h> 128c2ecf20Sopenharmony_ci#include <linux/genalloc.h> 138c2ecf20Sopenharmony_ci#include <linux/vmalloc.h> 148c2ecf20Sopenharmony_ci#ifdef CONFIG_X86 158c2ecf20Sopenharmony_ci#include <asm/set_memory.h> 168c2ecf20Sopenharmony_ci#endif 178c2ecf20Sopenharmony_ci#include <sound/memalloc.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci/* 208c2ecf20Sopenharmony_ci * 218c2ecf20Sopenharmony_ci * Bus-specific memory allocators 228c2ecf20Sopenharmony_ci * 238c2ecf20Sopenharmony_ci */ 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci#ifdef CONFIG_HAS_DMA 268c2ecf20Sopenharmony_ci/* allocate the coherent DMA pages */ 278c2ecf20Sopenharmony_cistatic void snd_malloc_dev_pages(struct snd_dma_buffer *dmab, size_t size) 288c2ecf20Sopenharmony_ci{ 298c2ecf20Sopenharmony_ci gfp_t gfp_flags; 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci gfp_flags = GFP_KERNEL 328c2ecf20Sopenharmony_ci | __GFP_COMP /* compound page lets parts be mapped */ 338c2ecf20Sopenharmony_ci | __GFP_NORETRY /* don't trigger OOM-killer */ 348c2ecf20Sopenharmony_ci | __GFP_NOWARN; /* no stack trace print - this call is non-critical */ 358c2ecf20Sopenharmony_ci dmab->area = dma_alloc_coherent(dmab->dev.dev, size, &dmab->addr, 368c2ecf20Sopenharmony_ci gfp_flags); 378c2ecf20Sopenharmony_ci#ifdef CONFIG_X86 388c2ecf20Sopenharmony_ci if (dmab->area && dmab->dev.type == SNDRV_DMA_TYPE_DEV_UC) 398c2ecf20Sopenharmony_ci set_memory_wc((unsigned long)dmab->area, 408c2ecf20Sopenharmony_ci PAGE_ALIGN(size) >> PAGE_SHIFT); 418c2ecf20Sopenharmony_ci#endif 428c2ecf20Sopenharmony_ci} 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci/* free the coherent DMA pages */ 458c2ecf20Sopenharmony_cistatic void snd_free_dev_pages(struct snd_dma_buffer *dmab) 468c2ecf20Sopenharmony_ci{ 478c2ecf20Sopenharmony_ci#ifdef CONFIG_X86 488c2ecf20Sopenharmony_ci if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_UC) 498c2ecf20Sopenharmony_ci set_memory_wb((unsigned long)dmab->area, 508c2ecf20Sopenharmony_ci PAGE_ALIGN(dmab->bytes) >> PAGE_SHIFT); 518c2ecf20Sopenharmony_ci#endif 528c2ecf20Sopenharmony_ci dma_free_coherent(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr); 538c2ecf20Sopenharmony_ci} 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci#ifdef CONFIG_GENERIC_ALLOCATOR 568c2ecf20Sopenharmony_ci/** 578c2ecf20Sopenharmony_ci * snd_malloc_dev_iram - allocate memory from on-chip internal ram 588c2ecf20Sopenharmony_ci * @dmab: buffer allocation record to store the allocated data 598c2ecf20Sopenharmony_ci * @size: number of bytes to allocate from the iram 608c2ecf20Sopenharmony_ci * 618c2ecf20Sopenharmony_ci * This function requires iram phandle provided via of_node 628c2ecf20Sopenharmony_ci */ 638c2ecf20Sopenharmony_cistatic void snd_malloc_dev_iram(struct snd_dma_buffer *dmab, size_t size) 648c2ecf20Sopenharmony_ci{ 658c2ecf20Sopenharmony_ci struct device *dev = dmab->dev.dev; 668c2ecf20Sopenharmony_ci struct gen_pool *pool = NULL; 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci dmab->area = NULL; 698c2ecf20Sopenharmony_ci dmab->addr = 0; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci if (dev->of_node) 728c2ecf20Sopenharmony_ci pool = of_gen_pool_get(dev->of_node, "iram", 0); 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci if (!pool) 758c2ecf20Sopenharmony_ci return; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci /* Assign the pool into private_data field */ 788c2ecf20Sopenharmony_ci dmab->private_data = pool; 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci dmab->area = gen_pool_dma_alloc_align(pool, size, &dmab->addr, 818c2ecf20Sopenharmony_ci PAGE_SIZE); 828c2ecf20Sopenharmony_ci} 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci/** 858c2ecf20Sopenharmony_ci * snd_free_dev_iram - free allocated specific memory from on-chip internal ram 868c2ecf20Sopenharmony_ci * @dmab: buffer allocation record to store the allocated data 878c2ecf20Sopenharmony_ci */ 888c2ecf20Sopenharmony_cistatic void snd_free_dev_iram(struct snd_dma_buffer *dmab) 898c2ecf20Sopenharmony_ci{ 908c2ecf20Sopenharmony_ci struct gen_pool *pool = dmab->private_data; 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci if (pool && dmab->area) 938c2ecf20Sopenharmony_ci gen_pool_free(pool, (unsigned long)dmab->area, dmab->bytes); 948c2ecf20Sopenharmony_ci} 958c2ecf20Sopenharmony_ci#endif /* CONFIG_GENERIC_ALLOCATOR */ 968c2ecf20Sopenharmony_ci#endif /* CONFIG_HAS_DMA */ 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci/* 998c2ecf20Sopenharmony_ci * 1008c2ecf20Sopenharmony_ci * ALSA generic memory management 1018c2ecf20Sopenharmony_ci * 1028c2ecf20Sopenharmony_ci */ 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_cistatic inline gfp_t snd_mem_get_gfp_flags(const struct device *dev, 1058c2ecf20Sopenharmony_ci gfp_t default_gfp) 1068c2ecf20Sopenharmony_ci{ 1078c2ecf20Sopenharmony_ci if (!dev) 1088c2ecf20Sopenharmony_ci return default_gfp; 1098c2ecf20Sopenharmony_ci else 1108c2ecf20Sopenharmony_ci return (__force gfp_t)(unsigned long)dev; 1118c2ecf20Sopenharmony_ci} 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci/** 1148c2ecf20Sopenharmony_ci * snd_dma_alloc_pages - allocate the buffer area according to the given type 1158c2ecf20Sopenharmony_ci * @type: the DMA buffer type 1168c2ecf20Sopenharmony_ci * @device: the device pointer 1178c2ecf20Sopenharmony_ci * @size: the buffer size to allocate 1188c2ecf20Sopenharmony_ci * @dmab: buffer allocation record to store the allocated data 1198c2ecf20Sopenharmony_ci * 1208c2ecf20Sopenharmony_ci * Calls the memory-allocator function for the corresponding 1218c2ecf20Sopenharmony_ci * buffer type. 1228c2ecf20Sopenharmony_ci * 1238c2ecf20Sopenharmony_ci * Return: Zero if the buffer with the given size is allocated successfully, 1248c2ecf20Sopenharmony_ci * otherwise a negative value on error. 1258c2ecf20Sopenharmony_ci */ 1268c2ecf20Sopenharmony_ciint snd_dma_alloc_pages(int type, struct device *device, size_t size, 1278c2ecf20Sopenharmony_ci struct snd_dma_buffer *dmab) 1288c2ecf20Sopenharmony_ci{ 1298c2ecf20Sopenharmony_ci gfp_t gfp; 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci if (WARN_ON(!size)) 1328c2ecf20Sopenharmony_ci return -ENXIO; 1338c2ecf20Sopenharmony_ci if (WARN_ON(!dmab)) 1348c2ecf20Sopenharmony_ci return -ENXIO; 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci size = PAGE_ALIGN(size); 1378c2ecf20Sopenharmony_ci dmab->dev.type = type; 1388c2ecf20Sopenharmony_ci dmab->dev.dev = device; 1398c2ecf20Sopenharmony_ci dmab->bytes = 0; 1408c2ecf20Sopenharmony_ci dmab->area = NULL; 1418c2ecf20Sopenharmony_ci dmab->addr = 0; 1428c2ecf20Sopenharmony_ci dmab->private_data = NULL; 1438c2ecf20Sopenharmony_ci switch (type) { 1448c2ecf20Sopenharmony_ci case SNDRV_DMA_TYPE_CONTINUOUS: 1458c2ecf20Sopenharmony_ci gfp = snd_mem_get_gfp_flags(device, GFP_KERNEL); 1468c2ecf20Sopenharmony_ci dmab->area = alloc_pages_exact(size, gfp); 1478c2ecf20Sopenharmony_ci break; 1488c2ecf20Sopenharmony_ci case SNDRV_DMA_TYPE_VMALLOC: 1498c2ecf20Sopenharmony_ci gfp = snd_mem_get_gfp_flags(device, GFP_KERNEL | __GFP_HIGHMEM); 1508c2ecf20Sopenharmony_ci dmab->area = __vmalloc(size, gfp); 1518c2ecf20Sopenharmony_ci break; 1528c2ecf20Sopenharmony_ci#ifdef CONFIG_HAS_DMA 1538c2ecf20Sopenharmony_ci#ifdef CONFIG_GENERIC_ALLOCATOR 1548c2ecf20Sopenharmony_ci case SNDRV_DMA_TYPE_DEV_IRAM: 1558c2ecf20Sopenharmony_ci snd_malloc_dev_iram(dmab, size); 1568c2ecf20Sopenharmony_ci if (dmab->area) 1578c2ecf20Sopenharmony_ci break; 1588c2ecf20Sopenharmony_ci /* Internal memory might have limited size and no enough space, 1598c2ecf20Sopenharmony_ci * so if we fail to malloc, try to fetch memory traditionally. 1608c2ecf20Sopenharmony_ci */ 1618c2ecf20Sopenharmony_ci dmab->dev.type = SNDRV_DMA_TYPE_DEV; 1628c2ecf20Sopenharmony_ci fallthrough; 1638c2ecf20Sopenharmony_ci#endif /* CONFIG_GENERIC_ALLOCATOR */ 1648c2ecf20Sopenharmony_ci case SNDRV_DMA_TYPE_DEV: 1658c2ecf20Sopenharmony_ci case SNDRV_DMA_TYPE_DEV_UC: 1668c2ecf20Sopenharmony_ci snd_malloc_dev_pages(dmab, size); 1678c2ecf20Sopenharmony_ci break; 1688c2ecf20Sopenharmony_ci#endif 1698c2ecf20Sopenharmony_ci#ifdef CONFIG_SND_DMA_SGBUF 1708c2ecf20Sopenharmony_ci case SNDRV_DMA_TYPE_DEV_SG: 1718c2ecf20Sopenharmony_ci case SNDRV_DMA_TYPE_DEV_UC_SG: 1728c2ecf20Sopenharmony_ci snd_malloc_sgbuf_pages(device, size, dmab, NULL); 1738c2ecf20Sopenharmony_ci break; 1748c2ecf20Sopenharmony_ci#endif 1758c2ecf20Sopenharmony_ci default: 1768c2ecf20Sopenharmony_ci pr_err("snd-malloc: invalid device type %d\n", type); 1778c2ecf20Sopenharmony_ci return -ENXIO; 1788c2ecf20Sopenharmony_ci } 1798c2ecf20Sopenharmony_ci if (! dmab->area) 1808c2ecf20Sopenharmony_ci return -ENOMEM; 1818c2ecf20Sopenharmony_ci dmab->bytes = size; 1828c2ecf20Sopenharmony_ci return 0; 1838c2ecf20Sopenharmony_ci} 1848c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_dma_alloc_pages); 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci/** 1878c2ecf20Sopenharmony_ci * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback 1888c2ecf20Sopenharmony_ci * @type: the DMA buffer type 1898c2ecf20Sopenharmony_ci * @device: the device pointer 1908c2ecf20Sopenharmony_ci * @size: the buffer size to allocate 1918c2ecf20Sopenharmony_ci * @dmab: buffer allocation record to store the allocated data 1928c2ecf20Sopenharmony_ci * 1938c2ecf20Sopenharmony_ci * Calls the memory-allocator function for the corresponding 1948c2ecf20Sopenharmony_ci * buffer type. When no space is left, this function reduces the size and 1958c2ecf20Sopenharmony_ci * tries to allocate again. The size actually allocated is stored in 1968c2ecf20Sopenharmony_ci * res_size argument. 1978c2ecf20Sopenharmony_ci * 1988c2ecf20Sopenharmony_ci * Return: Zero if the buffer with the given size is allocated successfully, 1998c2ecf20Sopenharmony_ci * otherwise a negative value on error. 2008c2ecf20Sopenharmony_ci */ 2018c2ecf20Sopenharmony_ciint snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size, 2028c2ecf20Sopenharmony_ci struct snd_dma_buffer *dmab) 2038c2ecf20Sopenharmony_ci{ 2048c2ecf20Sopenharmony_ci int err; 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) { 2078c2ecf20Sopenharmony_ci if (err != -ENOMEM) 2088c2ecf20Sopenharmony_ci return err; 2098c2ecf20Sopenharmony_ci if (size <= PAGE_SIZE) 2108c2ecf20Sopenharmony_ci return -ENOMEM; 2118c2ecf20Sopenharmony_ci size >>= 1; 2128c2ecf20Sopenharmony_ci size = PAGE_SIZE << get_order(size); 2138c2ecf20Sopenharmony_ci } 2148c2ecf20Sopenharmony_ci if (! dmab->area) 2158c2ecf20Sopenharmony_ci return -ENOMEM; 2168c2ecf20Sopenharmony_ci return 0; 2178c2ecf20Sopenharmony_ci} 2188c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_dma_alloc_pages_fallback); 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci/** 2228c2ecf20Sopenharmony_ci * snd_dma_free_pages - release the allocated buffer 2238c2ecf20Sopenharmony_ci * @dmab: the buffer allocation record to release 2248c2ecf20Sopenharmony_ci * 2258c2ecf20Sopenharmony_ci * Releases the allocated buffer via snd_dma_alloc_pages(). 2268c2ecf20Sopenharmony_ci */ 2278c2ecf20Sopenharmony_civoid snd_dma_free_pages(struct snd_dma_buffer *dmab) 2288c2ecf20Sopenharmony_ci{ 2298c2ecf20Sopenharmony_ci switch (dmab->dev.type) { 2308c2ecf20Sopenharmony_ci case SNDRV_DMA_TYPE_CONTINUOUS: 2318c2ecf20Sopenharmony_ci free_pages_exact(dmab->area, dmab->bytes); 2328c2ecf20Sopenharmony_ci break; 2338c2ecf20Sopenharmony_ci case SNDRV_DMA_TYPE_VMALLOC: 2348c2ecf20Sopenharmony_ci vfree(dmab->area); 2358c2ecf20Sopenharmony_ci break; 2368c2ecf20Sopenharmony_ci#ifdef CONFIG_HAS_DMA 2378c2ecf20Sopenharmony_ci#ifdef CONFIG_GENERIC_ALLOCATOR 2388c2ecf20Sopenharmony_ci case SNDRV_DMA_TYPE_DEV_IRAM: 2398c2ecf20Sopenharmony_ci snd_free_dev_iram(dmab); 2408c2ecf20Sopenharmony_ci break; 2418c2ecf20Sopenharmony_ci#endif /* CONFIG_GENERIC_ALLOCATOR */ 2428c2ecf20Sopenharmony_ci case SNDRV_DMA_TYPE_DEV: 2438c2ecf20Sopenharmony_ci case SNDRV_DMA_TYPE_DEV_UC: 2448c2ecf20Sopenharmony_ci snd_free_dev_pages(dmab); 2458c2ecf20Sopenharmony_ci break; 2468c2ecf20Sopenharmony_ci#endif 2478c2ecf20Sopenharmony_ci#ifdef CONFIG_SND_DMA_SGBUF 2488c2ecf20Sopenharmony_ci case SNDRV_DMA_TYPE_DEV_SG: 2498c2ecf20Sopenharmony_ci case SNDRV_DMA_TYPE_DEV_UC_SG: 2508c2ecf20Sopenharmony_ci snd_free_sgbuf_pages(dmab); 2518c2ecf20Sopenharmony_ci break; 2528c2ecf20Sopenharmony_ci#endif 2538c2ecf20Sopenharmony_ci default: 2548c2ecf20Sopenharmony_ci pr_err("snd-malloc: invalid device type %d\n", dmab->dev.type); 2558c2ecf20Sopenharmony_ci } 2568c2ecf20Sopenharmony_ci} 2578c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_dma_free_pages); 258