memalloc.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. EXPORT_SYMBOL(snd_malloc_pages);
  54. /**
  55. * snd_free_pages - release the pages
  56. * @ptr: the buffer pointer to release
  57. * @size: the allocated buffer size
  58. *
  59. * Releases the buffer allocated via snd_malloc_pages().
  60. */
  61. void snd_free_pages(void *ptr, size_t size)
  62. {
  63. int pg;
  64. if (ptr == NULL)
  65. return;
  66. pg = get_order(size);
  67. free_pages((unsigned long) ptr, pg);
  68. }
  69. EXPORT_SYMBOL(snd_free_pages);
  70. /*
  71. *
  72. * Bus-specific memory allocators
  73. *
  74. */
  75. #ifdef CONFIG_HAS_DMA
  76. /* allocate the coherent DMA pages */
  77. static void *snd_malloc_dev_pages(struct device *dev, size_t size, dma_addr_t *dma)
  78. {
  79. int pg;
  80. gfp_t gfp_flags;
  81. if (WARN_ON(!dma))
  82. return NULL;
  83. pg = get_order(size);
  84. gfp_flags = GFP_KERNEL
  85. | __GFP_COMP /* compound page lets parts be mapped */
  86. | __GFP_NORETRY /* don't trigger OOM-killer */
  87. | __GFP_NOWARN; /* no stack trace print - this call is non-critical */
  88. return dma_alloc_coherent(dev, PAGE_SIZE << pg, dma, gfp_flags);
  89. }
  90. /* free the coherent DMA pages */
  91. static void snd_free_dev_pages(struct device *dev, size_t size, void *ptr,
  92. dma_addr_t dma)
  93. {
  94. int pg;
  95. if (ptr == NULL)
  96. return;
  97. pg = get_order(size);
  98. dma_free_coherent(dev, PAGE_SIZE << pg, ptr, dma);
  99. }
  100. #ifdef CONFIG_GENERIC_ALLOCATOR
  101. /**
  102. * snd_malloc_dev_iram - allocate memory from on-chip internal ram
  103. * @dmab: buffer allocation record to store the allocated data
  104. * @size: number of bytes to allocate from the iram
  105. *
  106. * This function requires iram phandle provided via of_node
  107. */
  108. static void snd_malloc_dev_iram(struct snd_dma_buffer *dmab, size_t size)
  109. {
  110. struct device *dev = dmab->dev.dev;
  111. struct gen_pool *pool = NULL;
  112. dmab->area = NULL;
  113. dmab->addr = 0;
  114. if (dev->of_node)
  115. pool = of_gen_pool_get(dev->of_node, "iram", 0);
  116. if (!pool)
  117. return;
  118. /* Assign the pool into private_data field */
  119. dmab->private_data = pool;
  120. dmab->area = gen_pool_dma_alloc(pool, size, &dmab->addr);
  121. }
  122. /**
  123. * snd_free_dev_iram - free allocated specific memory from on-chip internal ram
  124. * @dmab: buffer allocation record to store the allocated data
  125. */
  126. static void snd_free_dev_iram(struct snd_dma_buffer *dmab)
  127. {
  128. struct gen_pool *pool = dmab->private_data;
  129. if (pool && dmab->area)
  130. gen_pool_free(pool, (unsigned long)dmab->area, dmab->bytes);
  131. }
  132. #endif /* CONFIG_GENERIC_ALLOCATOR */
  133. #endif /* CONFIG_HAS_DMA */
  134. /*
  135. *
  136. * ALSA generic memory management
  137. *
  138. */
  139. /**
  140. * snd_dma_alloc_pages - allocate the buffer area according to the given type
  141. * @type: the DMA buffer type
  142. * @device: the device pointer
  143. * @size: the buffer size to allocate
  144. * @dmab: buffer allocation record to store the allocated data
  145. *
  146. * Calls the memory-allocator function for the corresponding
  147. * buffer type.
  148. *
  149. * Return: Zero if the buffer with the given size is allocated successfully,
  150. * otherwise a negative value on error.
  151. */
  152. int snd_dma_alloc_pages(int type, struct device *device, size_t size,
  153. struct snd_dma_buffer *dmab)
  154. {
  155. if (WARN_ON(!size))
  156. return -ENXIO;
  157. if (WARN_ON(!dmab))
  158. return -ENXIO;
  159. dmab->dev.type = type;
  160. dmab->dev.dev = device;
  161. dmab->bytes = 0;
  162. switch (type) {
  163. case SNDRV_DMA_TYPE_CONTINUOUS:
  164. dmab->area = snd_malloc_pages(size,
  165. (__force gfp_t)(unsigned long)device);
  166. dmab->addr = 0;
  167. break;
  168. #ifdef CONFIG_HAS_DMA
  169. #ifdef CONFIG_GENERIC_ALLOCATOR
  170. case SNDRV_DMA_TYPE_DEV_IRAM:
  171. snd_malloc_dev_iram(dmab, size);
  172. if (dmab->area)
  173. break;
  174. /* Internal memory might have limited size and no enough space,
  175. * so if we fail to malloc, try to fetch memory traditionally.
  176. */
  177. dmab->dev.type = SNDRV_DMA_TYPE_DEV;
  178. #endif /* CONFIG_GENERIC_ALLOCATOR */
  179. case SNDRV_DMA_TYPE_DEV:
  180. dmab->area = snd_malloc_dev_pages(device, size, &dmab->addr);
  181. break;
  182. #endif
  183. #ifdef CONFIG_SND_DMA_SGBUF
  184. case SNDRV_DMA_TYPE_DEV_SG:
  185. snd_malloc_sgbuf_pages(device, size, dmab, NULL);
  186. break;
  187. #endif
  188. default:
  189. pr_err("snd-malloc: invalid device type %d\n", type);
  190. dmab->area = NULL;
  191. dmab->addr = 0;
  192. return -ENXIO;
  193. }
  194. if (! dmab->area)
  195. return -ENOMEM;
  196. dmab->bytes = size;
  197. return 0;
  198. }
  199. EXPORT_SYMBOL(snd_dma_alloc_pages);
  200. /**
  201. * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
  202. * @type: the DMA buffer type
  203. * @device: the device pointer
  204. * @size: the buffer size to allocate
  205. * @dmab: buffer allocation record to store the allocated data
  206. *
  207. * Calls the memory-allocator function for the corresponding
  208. * buffer type. When no space is left, this function reduces the size and
  209. * tries to allocate again. The size actually allocated is stored in
  210. * res_size argument.
  211. *
  212. * Return: Zero if the buffer with the given size is allocated successfully,
  213. * otherwise a negative value on error.
  214. */
  215. int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
  216. struct snd_dma_buffer *dmab)
  217. {
  218. int err;
  219. while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
  220. if (err != -ENOMEM)
  221. return err;
  222. if (size <= PAGE_SIZE)
  223. return -ENOMEM;
  224. size >>= 1;
  225. size = PAGE_SIZE << get_order(size);
  226. }
  227. if (! dmab->area)
  228. return -ENOMEM;
  229. return 0;
  230. }
  231. EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
  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. EXPORT_SYMBOL(snd_dma_free_pages);