pcm_memory.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * Digital Audio (PCM) abstract layer
  3. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  4. *
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <linux/io.h>
  22. #include <linux/time.h>
  23. #include <linux/init.h>
  24. #include <linux/slab.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/export.h>
  28. #include <sound/core.h>
  29. #include <sound/pcm.h>
  30. #include <sound/info.h>
  31. #include <sound/initval.h>
  32. static int preallocate_dma = 1;
  33. module_param(preallocate_dma, int, 0444);
  34. MODULE_PARM_DESC(preallocate_dma, "Preallocate DMA memory when the PCM devices are initialized.");
  35. static int maximum_substreams = 4;
  36. module_param(maximum_substreams, int, 0444);
  37. MODULE_PARM_DESC(maximum_substreams, "Maximum substreams with preallocated DMA memory.");
  38. static const size_t snd_minimum_buffer = 16384;
  39. /*
  40. * try to allocate as the large pages as possible.
  41. * stores the resultant memory size in *res_size.
  42. *
  43. * the minimum size is snd_minimum_buffer. it should be power of 2.
  44. */
  45. static int preallocate_pcm_pages(struct snd_pcm_substream *substream, size_t size)
  46. {
  47. struct snd_dma_buffer *dmab = &substream->dma_buffer;
  48. size_t orig_size = size;
  49. int err;
  50. do {
  51. if ((err = snd_dma_alloc_pages(dmab->dev.type, dmab->dev.dev,
  52. size, dmab)) < 0) {
  53. if (err != -ENOMEM)
  54. return err; /* fatal error */
  55. } else
  56. return 0;
  57. size >>= 1;
  58. } while (size >= snd_minimum_buffer);
  59. dmab->bytes = 0; /* tell error */
  60. pr_warn("ALSA pcmC%dD%d%c,%d:%s: cannot preallocate for size %zu\n",
  61. substream->pcm->card->number, substream->pcm->device,
  62. substream->stream ? 'c' : 'p', substream->number,
  63. substream->pcm->name, orig_size);
  64. return 0;
  65. }
  66. /*
  67. * release the preallocated buffer if not yet done.
  68. */
  69. static void snd_pcm_lib_preallocate_dma_free(struct snd_pcm_substream *substream)
  70. {
  71. if (substream->dma_buffer.area == NULL)
  72. return;
  73. snd_dma_free_pages(&substream->dma_buffer);
  74. substream->dma_buffer.area = NULL;
  75. }
  76. /**
  77. * snd_pcm_lib_preallocate_free - release the preallocated buffer of the specified substream.
  78. * @substream: the pcm substream instance
  79. *
  80. * Releases the pre-allocated buffer of the given substream.
  81. *
  82. * Return: Zero if successful, or a negative error code on failure.
  83. */
  84. int snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream)
  85. {
  86. snd_pcm_lib_preallocate_dma_free(substream);
  87. #ifdef CONFIG_SND_VERBOSE_PROCFS
  88. snd_info_free_entry(substream->proc_prealloc_max_entry);
  89. substream->proc_prealloc_max_entry = NULL;
  90. snd_info_free_entry(substream->proc_prealloc_entry);
  91. substream->proc_prealloc_entry = NULL;
  92. #endif
  93. return 0;
  94. }
  95. /**
  96. * snd_pcm_lib_preallocate_free_for_all - release all pre-allocated buffers on the pcm
  97. * @pcm: the pcm instance
  98. *
  99. * Releases all the pre-allocated buffers on the given pcm.
  100. *
  101. * Return: Zero if successful, or a negative error code on failure.
  102. */
  103. int snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm)
  104. {
  105. struct snd_pcm_substream *substream;
  106. int stream;
  107. for (stream = 0; stream < 2; stream++)
  108. for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
  109. snd_pcm_lib_preallocate_free(substream);
  110. return 0;
  111. }
  112. EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all);
  113. #ifdef CONFIG_SND_VERBOSE_PROCFS
  114. /*
  115. * read callback for prealloc proc file
  116. *
  117. * prints the current allocated size in kB.
  118. */
  119. static void snd_pcm_lib_preallocate_proc_read(struct snd_info_entry *entry,
  120. struct snd_info_buffer *buffer)
  121. {
  122. struct snd_pcm_substream *substream = entry->private_data;
  123. snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_buffer.bytes / 1024);
  124. }
  125. /*
  126. * read callback for prealloc_max proc file
  127. *
  128. * prints the maximum allowed size in kB.
  129. */
  130. static void snd_pcm_lib_preallocate_max_proc_read(struct snd_info_entry *entry,
  131. struct snd_info_buffer *buffer)
  132. {
  133. struct snd_pcm_substream *substream = entry->private_data;
  134. snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_max / 1024);
  135. }
  136. /*
  137. * write callback for prealloc proc file
  138. *
  139. * accepts the preallocation size in kB.
  140. */
  141. static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,
  142. struct snd_info_buffer *buffer)
  143. {
  144. struct snd_pcm_substream *substream = entry->private_data;
  145. char line[64], str[64];
  146. size_t size;
  147. struct snd_dma_buffer new_dmab;
  148. if (substream->runtime) {
  149. buffer->error = -EBUSY;
  150. return;
  151. }
  152. if (!snd_info_get_line(buffer, line, sizeof(line))) {
  153. snd_info_get_str(str, line, sizeof(str));
  154. size = simple_strtoul(str, NULL, 10) * 1024;
  155. if ((size != 0 && size < 8192) || size > substream->dma_max) {
  156. buffer->error = -EINVAL;
  157. return;
  158. }
  159. if (substream->dma_buffer.bytes == size)
  160. return;
  161. memset(&new_dmab, 0, sizeof(new_dmab));
  162. new_dmab.dev = substream->dma_buffer.dev;
  163. if (size > 0) {
  164. if (snd_dma_alloc_pages(substream->dma_buffer.dev.type,
  165. substream->dma_buffer.dev.dev,
  166. size, &new_dmab) < 0) {
  167. buffer->error = -ENOMEM;
  168. return;
  169. }
  170. substream->buffer_bytes_max = size;
  171. } else {
  172. substream->buffer_bytes_max = UINT_MAX;
  173. }
  174. if (substream->dma_buffer.area)
  175. snd_dma_free_pages(&substream->dma_buffer);
  176. substream->dma_buffer = new_dmab;
  177. } else {
  178. buffer->error = -EINVAL;
  179. }
  180. }
  181. static inline void preallocate_info_init(struct snd_pcm_substream *substream)
  182. {
  183. struct snd_info_entry *entry;
  184. if ((entry = snd_info_create_card_entry(substream->pcm->card, "prealloc", substream->proc_root)) != NULL) {
  185. entry->c.text.read = snd_pcm_lib_preallocate_proc_read;
  186. entry->c.text.write = snd_pcm_lib_preallocate_proc_write;
  187. entry->mode |= S_IWUSR;
  188. entry->private_data = substream;
  189. if (snd_info_register(entry) < 0) {
  190. snd_info_free_entry(entry);
  191. entry = NULL;
  192. }
  193. }
  194. substream->proc_prealloc_entry = entry;
  195. if ((entry = snd_info_create_card_entry(substream->pcm->card, "prealloc_max", substream->proc_root)) != NULL) {
  196. entry->c.text.read = snd_pcm_lib_preallocate_max_proc_read;
  197. entry->private_data = substream;
  198. if (snd_info_register(entry) < 0) {
  199. snd_info_free_entry(entry);
  200. entry = NULL;
  201. }
  202. }
  203. substream->proc_prealloc_max_entry = entry;
  204. }
  205. #else /* !CONFIG_SND_VERBOSE_PROCFS */
  206. #define preallocate_info_init(s)
  207. #endif /* CONFIG_SND_VERBOSE_PROCFS */
  208. /*
  209. * pre-allocate the buffer and create a proc file for the substream
  210. */
  211. static int snd_pcm_lib_preallocate_pages1(struct snd_pcm_substream *substream,
  212. size_t size, size_t max)
  213. {
  214. if (size > 0 && preallocate_dma && substream->number < maximum_substreams)
  215. preallocate_pcm_pages(substream, size);
  216. if (substream->dma_buffer.bytes > 0)
  217. substream->buffer_bytes_max = substream->dma_buffer.bytes;
  218. substream->dma_max = max;
  219. preallocate_info_init(substream);
  220. return 0;
  221. }
  222. /**
  223. * snd_pcm_lib_preallocate_pages - pre-allocation for the given DMA type
  224. * @substream: the pcm substream instance
  225. * @type: DMA type (SNDRV_DMA_TYPE_*)
  226. * @data: DMA type dependent data
  227. * @size: the requested pre-allocation size in bytes
  228. * @max: the max. allowed pre-allocation size
  229. *
  230. * Do pre-allocation for the given DMA buffer type.
  231. *
  232. * Return: Zero if successful, or a negative error code on failure.
  233. */
  234. int snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream,
  235. int type, struct device *data,
  236. size_t size, size_t max)
  237. {
  238. substream->dma_buffer.dev.type = type;
  239. substream->dma_buffer.dev.dev = data;
  240. return snd_pcm_lib_preallocate_pages1(substream, size, max);
  241. }
  242. EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages);
  243. /**
  244. * snd_pcm_lib_preallocate_pages_for_all - pre-allocation for continuous memory type (all substreams)
  245. * @pcm: the pcm instance
  246. * @type: DMA type (SNDRV_DMA_TYPE_*)
  247. * @data: DMA type dependent data
  248. * @size: the requested pre-allocation size in bytes
  249. * @max: the max. allowed pre-allocation size
  250. *
  251. * Do pre-allocation to all substreams of the given pcm for the
  252. * specified DMA type.
  253. *
  254. * Return: Zero if successful, or a negative error code on failure.
  255. */
  256. int snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,
  257. int type, void *data,
  258. size_t size, size_t max)
  259. {
  260. struct snd_pcm_substream *substream;
  261. int stream, err;
  262. for (stream = 0; stream < 2; stream++)
  263. for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
  264. if ((err = snd_pcm_lib_preallocate_pages(substream, type, data, size, max)) < 0)
  265. return err;
  266. return 0;
  267. }
  268. EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all);
  269. #ifdef CONFIG_SND_DMA_SGBUF
  270. /**
  271. * snd_pcm_sgbuf_ops_page - get the page struct at the given offset
  272. * @substream: the pcm substream instance
  273. * @offset: the buffer offset
  274. *
  275. * Used as the page callback of PCM ops.
  276. *
  277. * Return: The page struct at the given buffer offset. %NULL on failure.
  278. */
  279. struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream, unsigned long offset)
  280. {
  281. struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
  282. unsigned int idx = offset >> PAGE_SHIFT;
  283. if (idx >= (unsigned int)sgbuf->pages)
  284. return NULL;
  285. return sgbuf->page_table[idx];
  286. }
  287. EXPORT_SYMBOL(snd_pcm_sgbuf_ops_page);
  288. #endif /* CONFIG_SND_DMA_SGBUF */
  289. /**
  290. * snd_pcm_lib_malloc_pages - allocate the DMA buffer
  291. * @substream: the substream to allocate the DMA buffer to
  292. * @size: the requested buffer size in bytes
  293. *
  294. * Allocates the DMA buffer on the BUS type given earlier to
  295. * snd_pcm_lib_preallocate_xxx_pages().
  296. *
  297. * Return: 1 if the buffer is changed, 0 if not changed, or a negative
  298. * code on failure.
  299. */
  300. int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size)
  301. {
  302. struct snd_pcm_runtime *runtime;
  303. struct snd_dma_buffer *dmab = NULL;
  304. if (PCM_RUNTIME_CHECK(substream))
  305. return -EINVAL;
  306. if (snd_BUG_ON(substream->dma_buffer.dev.type ==
  307. SNDRV_DMA_TYPE_UNKNOWN))
  308. return -EINVAL;
  309. runtime = substream->runtime;
  310. if (runtime->dma_buffer_p) {
  311. /* perphaps, we might free the large DMA memory region
  312. to save some space here, but the actual solution
  313. costs us less time */
  314. if (runtime->dma_buffer_p->bytes >= size) {
  315. runtime->dma_bytes = size;
  316. return 0; /* ok, do not change */
  317. }
  318. snd_pcm_lib_free_pages(substream);
  319. }
  320. if (substream->dma_buffer.area != NULL &&
  321. substream->dma_buffer.bytes >= size) {
  322. dmab = &substream->dma_buffer; /* use the pre-allocated buffer */
  323. } else {
  324. dmab = kzalloc(sizeof(*dmab), GFP_KERNEL);
  325. if (! dmab)
  326. return -ENOMEM;
  327. dmab->dev = substream->dma_buffer.dev;
  328. if (snd_dma_alloc_pages(substream->dma_buffer.dev.type,
  329. substream->dma_buffer.dev.dev,
  330. size, dmab) < 0) {
  331. kfree(dmab);
  332. return -ENOMEM;
  333. }
  334. }
  335. snd_pcm_set_runtime_buffer(substream, dmab);
  336. runtime->dma_bytes = size;
  337. return 1; /* area was changed */
  338. }
  339. EXPORT_SYMBOL(snd_pcm_lib_malloc_pages);
  340. /**
  341. * snd_pcm_lib_free_pages - release the allocated DMA buffer.
  342. * @substream: the substream to release the DMA buffer
  343. *
  344. * Releases the DMA buffer allocated via snd_pcm_lib_malloc_pages().
  345. *
  346. * Return: Zero if successful, or a negative error code on failure.
  347. */
  348. int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream)
  349. {
  350. struct snd_pcm_runtime *runtime;
  351. if (PCM_RUNTIME_CHECK(substream))
  352. return -EINVAL;
  353. runtime = substream->runtime;
  354. if (runtime->dma_area == NULL)
  355. return 0;
  356. if (runtime->dma_buffer_p != &substream->dma_buffer) {
  357. /* it's a newly allocated buffer. release it now. */
  358. snd_dma_free_pages(runtime->dma_buffer_p);
  359. kfree(runtime->dma_buffer_p);
  360. }
  361. snd_pcm_set_runtime_buffer(substream, NULL);
  362. return 0;
  363. }
  364. EXPORT_SYMBOL(snd_pcm_lib_free_pages);
  365. int _snd_pcm_lib_alloc_vmalloc_buffer(struct snd_pcm_substream *substream,
  366. size_t size, gfp_t gfp_flags)
  367. {
  368. struct snd_pcm_runtime *runtime;
  369. if (PCM_RUNTIME_CHECK(substream))
  370. return -EINVAL;
  371. runtime = substream->runtime;
  372. if (runtime->dma_area) {
  373. if (runtime->dma_bytes >= size)
  374. return 0; /* already large enough */
  375. vfree(runtime->dma_area);
  376. }
  377. runtime->dma_area = __vmalloc(size, gfp_flags, PAGE_KERNEL);
  378. if (!runtime->dma_area)
  379. return -ENOMEM;
  380. runtime->dma_bytes = size;
  381. return 1;
  382. }
  383. EXPORT_SYMBOL(_snd_pcm_lib_alloc_vmalloc_buffer);
  384. /**
  385. * snd_pcm_lib_free_vmalloc_buffer - free vmalloc buffer
  386. * @substream: the substream with a buffer allocated by
  387. * snd_pcm_lib_alloc_vmalloc_buffer()
  388. *
  389. * Return: Zero if successful, or a negative error code on failure.
  390. */
  391. int snd_pcm_lib_free_vmalloc_buffer(struct snd_pcm_substream *substream)
  392. {
  393. struct snd_pcm_runtime *runtime;
  394. if (PCM_RUNTIME_CHECK(substream))
  395. return -EINVAL;
  396. runtime = substream->runtime;
  397. vfree(runtime->dma_area);
  398. runtime->dma_area = NULL;
  399. return 0;
  400. }
  401. EXPORT_SYMBOL(snd_pcm_lib_free_vmalloc_buffer);
  402. /**
  403. * snd_pcm_lib_get_vmalloc_page - map vmalloc buffer offset to page struct
  404. * @substream: the substream with a buffer allocated by
  405. * snd_pcm_lib_alloc_vmalloc_buffer()
  406. * @offset: offset in the buffer
  407. *
  408. * This function is to be used as the page callback in the PCM ops.
  409. *
  410. * Return: The page struct, or %NULL on failure.
  411. */
  412. struct page *snd_pcm_lib_get_vmalloc_page(struct snd_pcm_substream *substream,
  413. unsigned long offset)
  414. {
  415. return vmalloc_to_page(substream->runtime->dma_area + offset);
  416. }
  417. EXPORT_SYMBOL(snd_pcm_lib_get_vmalloc_page);