memalloc.c 7.7 KB

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