memalloc.c 7.9 KB

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