memalloc.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  3. * Takashi Iwai <tiwai@suse.de>
  4. *
  5. * Generic memory allocators
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/slab.h>
  24. #include <linux/mm.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/genalloc.h>
  27. #include <sound/memalloc.h>
  28. /*
  29. *
  30. * Generic memory allocators
  31. *
  32. */
  33. /**
  34. * snd_malloc_pages - allocate pages with the given size
  35. * @size: the size to allocate in bytes
  36. * @gfp_flags: the allocation conditions, GFP_XXX
  37. *
  38. * Allocates the physically contiguous pages with the given size.
  39. *
  40. * Return: The pointer of the buffer, or %NULL if no enough memory.
  41. */
  42. void *snd_malloc_pages(size_t size, gfp_t gfp_flags)
  43. {
  44. int pg;
  45. if (WARN_ON(!size))
  46. return NULL;
  47. if (WARN_ON(!gfp_flags))
  48. return NULL;
  49. gfp_flags |= __GFP_COMP; /* compound page lets parts be mapped */
  50. pg = get_order(size);
  51. return (void *) __get_free_pages(gfp_flags, pg);
  52. }
  53. /**
  54. * snd_free_pages - release the pages
  55. * @ptr: the buffer pointer to release
  56. * @size: the allocated buffer size
  57. *
  58. * Releases the buffer allocated via snd_malloc_pages().
  59. */
  60. void snd_free_pages(void *ptr, size_t size)
  61. {
  62. int pg;
  63. if (ptr == NULL)
  64. return;
  65. pg = get_order(size);
  66. free_pages((unsigned long) ptr, pg);
  67. }
  68. /*
  69. *
  70. * Bus-specific memory allocators
  71. *
  72. */
  73. #ifdef CONFIG_HAS_DMA
  74. /* allocate the coherent DMA pages */
  75. static void *snd_malloc_dev_pages(struct device *dev, size_t size, dma_addr_t *dma)
  76. {
  77. int pg;
  78. gfp_t gfp_flags;
  79. if (WARN_ON(!dma))
  80. return NULL;
  81. pg = get_order(size);
  82. gfp_flags = GFP_KERNEL
  83. | __GFP_COMP /* compound page lets parts be mapped */
  84. | __GFP_NORETRY /* don't trigger OOM-killer */
  85. | __GFP_NOWARN; /* no stack trace print - this call is non-critical */
  86. return dma_alloc_coherent(dev, PAGE_SIZE << pg, dma, gfp_flags);
  87. }
  88. /* free the coherent DMA pages */
  89. static void snd_free_dev_pages(struct device *dev, size_t size, void *ptr,
  90. dma_addr_t dma)
  91. {
  92. int pg;
  93. if (ptr == NULL)
  94. return;
  95. pg = get_order(size);
  96. dma_free_coherent(dev, PAGE_SIZE << pg, ptr, dma);
  97. }
  98. #ifdef CONFIG_GENERIC_ALLOCATOR
  99. /**
  100. * snd_malloc_dev_iram - allocate memory from on-chip internal ram
  101. * @dmab: buffer allocation record to store the allocated data
  102. * @size: number of bytes to allocate from the iram
  103. *
  104. * This function requires iram phandle provided via of_node
  105. */
  106. static void snd_malloc_dev_iram(struct snd_dma_buffer *dmab, size_t size)
  107. {
  108. struct device *dev = dmab->dev.dev;
  109. struct gen_pool *pool = NULL;
  110. dmab->area = NULL;
  111. dmab->addr = 0;
  112. if (dev->of_node)
  113. pool = of_gen_pool_get(dev->of_node, "iram", 0);
  114. if (!pool)
  115. return;
  116. /* Assign the pool into private_data field */
  117. dmab->private_data = pool;
  118. dmab->area = gen_pool_dma_alloc(pool, size, &dmab->addr);
  119. }
  120. /**
  121. * snd_free_dev_iram - free allocated specific memory from on-chip internal ram
  122. * @dmab: buffer allocation record to store the allocated data
  123. */
  124. static void snd_free_dev_iram(struct snd_dma_buffer *dmab)
  125. {
  126. struct gen_pool *pool = dmab->private_data;
  127. if (pool && dmab->area)
  128. gen_pool_free(pool, (unsigned long)dmab->area, dmab->bytes);
  129. }
  130. #endif /* CONFIG_GENERIC_ALLOCATOR */
  131. #endif /* CONFIG_HAS_DMA */
  132. /*
  133. *
  134. * ALSA generic memory management
  135. *
  136. */
  137. /**
  138. * snd_dma_alloc_pages - allocate the buffer area according to the given type
  139. * @type: the DMA buffer type
  140. * @device: the device pointer
  141. * @size: the buffer size to allocate
  142. * @dmab: buffer allocation record to store the allocated data
  143. *
  144. * Calls the memory-allocator function for the corresponding
  145. * buffer type.
  146. *
  147. * Return: Zero if the buffer with the given size is allocated successfully,
  148. * otherwise a negative value on error.
  149. */
  150. int snd_dma_alloc_pages(int type, struct device *device, size_t size,
  151. struct snd_dma_buffer *dmab)
  152. {
  153. if (WARN_ON(!size))
  154. return -ENXIO;
  155. if (WARN_ON(!dmab))
  156. return -ENXIO;
  157. dmab->dev.type = type;
  158. dmab->dev.dev = device;
  159. dmab->bytes = 0;
  160. switch (type) {
  161. case SNDRV_DMA_TYPE_CONTINUOUS:
  162. dmab->area = snd_malloc_pages(size,
  163. (__force gfp_t)(unsigned long)device);
  164. dmab->addr = 0;
  165. break;
  166. #ifdef CONFIG_HAS_DMA
  167. #ifdef CONFIG_GENERIC_ALLOCATOR
  168. case SNDRV_DMA_TYPE_DEV_IRAM:
  169. snd_malloc_dev_iram(dmab, size);
  170. if (dmab->area)
  171. break;
  172. /* Internal memory might have limited size and no enough space,
  173. * so if we fail to malloc, try to fetch memory traditionally.
  174. */
  175. dmab->dev.type = SNDRV_DMA_TYPE_DEV;
  176. #endif /* CONFIG_GENERIC_ALLOCATOR */
  177. case SNDRV_DMA_TYPE_DEV:
  178. dmab->area = snd_malloc_dev_pages(device, size, &dmab->addr);
  179. break;
  180. #endif
  181. #ifdef CONFIG_SND_DMA_SGBUF
  182. case SNDRV_DMA_TYPE_DEV_SG:
  183. snd_malloc_sgbuf_pages(device, size, dmab, NULL);
  184. break;
  185. #endif
  186. default:
  187. pr_err("snd-malloc: invalid device type %d\n", type);
  188. dmab->area = NULL;
  189. dmab->addr = 0;
  190. return -ENXIO;
  191. }
  192. if (! dmab->area)
  193. return -ENOMEM;
  194. dmab->bytes = size;
  195. return 0;
  196. }
  197. /**
  198. * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
  199. * @type: the DMA buffer type
  200. * @device: the device pointer
  201. * @size: the buffer size to allocate
  202. * @dmab: buffer allocation record to store the allocated data
  203. *
  204. * Calls the memory-allocator function for the corresponding
  205. * buffer type. When no space is left, this function reduces the size and
  206. * tries to allocate again. The size actually allocated is stored in
  207. * res_size argument.
  208. *
  209. * Return: Zero if the buffer with the given size is allocated successfully,
  210. * otherwise a negative value on error.
  211. */
  212. int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
  213. struct snd_dma_buffer *dmab)
  214. {
  215. int err;
  216. while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
  217. size_t aligned_size;
  218. if (err != -ENOMEM)
  219. return err;
  220. if (size <= PAGE_SIZE)
  221. return -ENOMEM;
  222. aligned_size = PAGE_SIZE << get_order(size);
  223. if (size != aligned_size)
  224. size = aligned_size;
  225. else
  226. size >>= 1;
  227. }
  228. if (! dmab->area)
  229. return -ENOMEM;
  230. return 0;
  231. }
  232. /**
  233. * snd_dma_free_pages - release the allocated buffer
  234. * @dmab: the buffer allocation record to release
  235. *
  236. * Releases the allocated buffer via snd_dma_alloc_pages().
  237. */
  238. void snd_dma_free_pages(struct snd_dma_buffer *dmab)
  239. {
  240. switch (dmab->dev.type) {
  241. case SNDRV_DMA_TYPE_CONTINUOUS:
  242. snd_free_pages(dmab->area, dmab->bytes);
  243. break;
  244. #ifdef CONFIG_HAS_DMA
  245. #ifdef CONFIG_GENERIC_ALLOCATOR
  246. case SNDRV_DMA_TYPE_DEV_IRAM:
  247. snd_free_dev_iram(dmab);
  248. break;
  249. #endif /* CONFIG_GENERIC_ALLOCATOR */
  250. case SNDRV_DMA_TYPE_DEV:
  251. snd_free_dev_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
  252. break;
  253. #endif
  254. #ifdef CONFIG_SND_DMA_SGBUF
  255. case SNDRV_DMA_TYPE_DEV_SG:
  256. snd_free_sgbuf_pages(dmab);
  257. break;
  258. #endif
  259. default:
  260. pr_err("snd-malloc: invalid device type %d\n", dmab->dev.type);
  261. }
  262. }
  263. /*
  264. * exports
  265. */
  266. EXPORT_SYMBOL(snd_dma_alloc_pages);
  267. EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
  268. EXPORT_SYMBOL(snd_dma_free_pages);
  269. EXPORT_SYMBOL(snd_malloc_pages);
  270. EXPORT_SYMBOL(snd_free_pages);