pcm_memory.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * Digital Audio (PCM) abstract layer
  3. * Copyright (c) by Jaroslav Kysela <perex@suse.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 <sound/driver.h>
  22. #include <asm/io.h>
  23. #include <linux/time.h>
  24. #include <linux/init.h>
  25. #include <linux/moduleparam.h>
  26. #include <sound/core.h>
  27. #include <sound/pcm.h>
  28. #include <sound/info.h>
  29. #include <sound/initval.h>
  30. static int preallocate_dma = 1;
  31. module_param(preallocate_dma, int, 0444);
  32. MODULE_PARM_DESC(preallocate_dma, "Preallocate DMA memory when the PCM devices are initialized.");
  33. static int maximum_substreams = 4;
  34. module_param(maximum_substreams, int, 0444);
  35. MODULE_PARM_DESC(maximum_substreams, "Maximum substreams with preallocated DMA memory.");
  36. static const size_t snd_minimum_buffer = 16384;
  37. /*
  38. * try to allocate as the large pages as possible.
  39. * stores the resultant memory size in *res_size.
  40. *
  41. * the minimum size is snd_minimum_buffer. it should be power of 2.
  42. */
  43. static int preallocate_pcm_pages(snd_pcm_substream_t *substream, size_t size)
  44. {
  45. struct snd_dma_buffer *dmab = &substream->dma_buffer;
  46. int err;
  47. snd_assert(size > 0, return -EINVAL);
  48. /* already reserved? */
  49. if (snd_dma_get_reserved_buf(dmab, substream->dma_buf_id) > 0) {
  50. if (dmab->bytes >= size)
  51. return 0; /* yes */
  52. /* no, free the reserved block */
  53. snd_dma_free_pages(dmab);
  54. dmab->bytes = 0;
  55. }
  56. do {
  57. if ((err = snd_dma_alloc_pages(dmab->dev.type, dmab->dev.dev,
  58. size, dmab)) < 0) {
  59. if (err != -ENOMEM)
  60. return err; /* fatal error */
  61. } else
  62. return 0;
  63. size >>= 1;
  64. } while (size >= snd_minimum_buffer);
  65. dmab->bytes = 0; /* tell error */
  66. return 0;
  67. }
  68. /*
  69. * release the preallocated buffer if not yet done.
  70. */
  71. static void snd_pcm_lib_preallocate_dma_free(snd_pcm_substream_t *substream)
  72. {
  73. if (substream->dma_buffer.area == NULL)
  74. return;
  75. if (substream->dma_buf_id)
  76. snd_dma_reserve_buf(&substream->dma_buffer, substream->dma_buf_id);
  77. else
  78. snd_dma_free_pages(&substream->dma_buffer);
  79. substream->dma_buffer.area = NULL;
  80. }
  81. /**
  82. * snd_pcm_lib_preallocate_free - release the preallocated buffer of the specified substream.
  83. * @substream: the pcm substream instance
  84. *
  85. * Releases the pre-allocated buffer of the given substream.
  86. *
  87. * Returns zero if successful, or a negative error code on failure.
  88. */
  89. int snd_pcm_lib_preallocate_free(snd_pcm_substream_t *substream)
  90. {
  91. snd_pcm_lib_preallocate_dma_free(substream);
  92. if (substream->proc_prealloc_entry) {
  93. snd_info_unregister(substream->proc_prealloc_entry);
  94. substream->proc_prealloc_entry = NULL;
  95. }
  96. return 0;
  97. }
  98. /**
  99. * snd_pcm_lib_preallocate_free_for_all - release all pre-allocated buffers on the pcm
  100. * @pcm: the pcm instance
  101. *
  102. * Releases all the pre-allocated buffers on the given pcm.
  103. *
  104. * Returns zero if successful, or a negative error code on failure.
  105. */
  106. int snd_pcm_lib_preallocate_free_for_all(snd_pcm_t *pcm)
  107. {
  108. snd_pcm_substream_t *substream;
  109. int stream;
  110. for (stream = 0; stream < 2; stream++)
  111. for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
  112. snd_pcm_lib_preallocate_free(substream);
  113. return 0;
  114. }
  115. /*
  116. * read callback for prealloc proc file
  117. *
  118. * prints the current allocated size in kB.
  119. */
  120. static void snd_pcm_lib_preallocate_proc_read(snd_info_entry_t *entry,
  121. snd_info_buffer_t *buffer)
  122. {
  123. snd_pcm_substream_t *substream = (snd_pcm_substream_t *)entry->private_data;
  124. snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_buffer.bytes / 1024);
  125. }
  126. /*
  127. * write callback for prealloc proc file
  128. *
  129. * accepts the preallocation size in kB.
  130. */
  131. static void snd_pcm_lib_preallocate_proc_write(snd_info_entry_t *entry,
  132. snd_info_buffer_t *buffer)
  133. {
  134. snd_pcm_substream_t *substream = (snd_pcm_substream_t *)entry->private_data;
  135. char line[64], str[64];
  136. size_t size;
  137. struct snd_dma_buffer new_dmab;
  138. if (substream->runtime) {
  139. buffer->error = -EBUSY;
  140. return;
  141. }
  142. if (!snd_info_get_line(buffer, line, sizeof(line))) {
  143. snd_info_get_str(str, line, sizeof(str));
  144. size = simple_strtoul(str, NULL, 10) * 1024;
  145. if ((size != 0 && size < 8192) || size > substream->dma_max) {
  146. buffer->error = -EINVAL;
  147. return;
  148. }
  149. if (substream->dma_buffer.bytes == size)
  150. return;
  151. memset(&new_dmab, 0, sizeof(new_dmab));
  152. new_dmab.dev = substream->dma_buffer.dev;
  153. if (size > 0) {
  154. if (snd_dma_alloc_pages(substream->dma_buffer.dev.type,
  155. substream->dma_buffer.dev.dev,
  156. size, &new_dmab) < 0) {
  157. buffer->error = -ENOMEM;
  158. return;
  159. }
  160. substream->buffer_bytes_max = size;
  161. } else {
  162. substream->buffer_bytes_max = UINT_MAX;
  163. }
  164. if (substream->dma_buffer.area)
  165. snd_dma_free_pages(&substream->dma_buffer);
  166. substream->dma_buffer = new_dmab;
  167. } else {
  168. buffer->error = -EINVAL;
  169. }
  170. }
  171. /*
  172. * pre-allocate the buffer and create a proc file for the substream
  173. */
  174. static int snd_pcm_lib_preallocate_pages1(snd_pcm_substream_t *substream,
  175. size_t size, size_t max)
  176. {
  177. snd_info_entry_t *entry;
  178. if (size > 0 && preallocate_dma && substream->number < maximum_substreams)
  179. preallocate_pcm_pages(substream, size);
  180. if (substream->dma_buffer.bytes > 0)
  181. substream->buffer_bytes_max = substream->dma_buffer.bytes;
  182. substream->dma_max = max;
  183. if ((entry = snd_info_create_card_entry(substream->pcm->card, "prealloc", substream->proc_root)) != NULL) {
  184. entry->c.text.read_size = 64;
  185. entry->c.text.read = snd_pcm_lib_preallocate_proc_read;
  186. entry->c.text.write_size = 64;
  187. entry->c.text.write = snd_pcm_lib_preallocate_proc_write;
  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. return 0;
  196. }
  197. /**
  198. * snd_pcm_lib_preallocate_pages - pre-allocation for the given DMA type
  199. * @substream: the pcm substream instance
  200. * @type: DMA type (SNDRV_DMA_TYPE_*)
  201. * @data: DMA type dependant data
  202. * @size: the requested pre-allocation size in bytes
  203. * @max: the max. allowed pre-allocation size
  204. *
  205. * Do pre-allocation for the given DMA buffer type.
  206. *
  207. * When substream->dma_buf_id is set, the function tries to look for
  208. * the reserved buffer, and the buffer is not freed but reserved at
  209. * destruction time. The dma_buf_id must be unique for all systems
  210. * (in the same DMA buffer type) e.g. using snd_dma_pci_buf_id().
  211. *
  212. * Returns zero if successful, or a negative error code on failure.
  213. */
  214. int snd_pcm_lib_preallocate_pages(snd_pcm_substream_t *substream,
  215. int type, struct device *data,
  216. size_t size, size_t max)
  217. {
  218. substream->dma_buffer.dev.type = type;
  219. substream->dma_buffer.dev.dev = data;
  220. return snd_pcm_lib_preallocate_pages1(substream, size, max);
  221. }
  222. /**
  223. * snd_pcm_lib_preallocate_pages_for_all - pre-allocation for continous memory type (all substreams)
  224. * @substream: the pcm substream instance
  225. * @type: DMA type (SNDRV_DMA_TYPE_*)
  226. * @data: DMA type dependant data
  227. * @size: the requested pre-allocation size in bytes
  228. * @max: the max. allowed pre-allocation size
  229. *
  230. * Do pre-allocation to all substreams of the given pcm for the
  231. * specified DMA type.
  232. *
  233. * Returns zero if successful, or a negative error code on failure.
  234. */
  235. int snd_pcm_lib_preallocate_pages_for_all(snd_pcm_t *pcm,
  236. int type, void *data,
  237. size_t size, size_t max)
  238. {
  239. snd_pcm_substream_t *substream;
  240. int stream, err;
  241. for (stream = 0; stream < 2; stream++)
  242. for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
  243. if ((err = snd_pcm_lib_preallocate_pages(substream, type, data, size, max)) < 0)
  244. return err;
  245. return 0;
  246. }
  247. /**
  248. * snd_pcm_sgbuf_ops_page - get the page struct at the given offset
  249. * @substream: the pcm substream instance
  250. * @offset: the buffer offset
  251. *
  252. * Returns the page struct at the given buffer offset.
  253. * Used as the page callback of PCM ops.
  254. */
  255. struct page *snd_pcm_sgbuf_ops_page(snd_pcm_substream_t *substream, unsigned long offset)
  256. {
  257. struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
  258. unsigned int idx = offset >> PAGE_SHIFT;
  259. if (idx >= (unsigned int)sgbuf->pages)
  260. return NULL;
  261. return sgbuf->page_table[idx];
  262. }
  263. /**
  264. * snd_pcm_lib_malloc_pages - allocate the DMA buffer
  265. * @substream: the substream to allocate the DMA buffer to
  266. * @size: the requested buffer size in bytes
  267. *
  268. * Allocates the DMA buffer on the BUS type given earlier to
  269. * snd_pcm_lib_preallocate_xxx_pages().
  270. *
  271. * Returns 1 if the buffer is changed, 0 if not changed, or a negative
  272. * code on failure.
  273. */
  274. int snd_pcm_lib_malloc_pages(snd_pcm_substream_t *substream, size_t size)
  275. {
  276. snd_pcm_runtime_t *runtime;
  277. struct snd_dma_buffer *dmab = NULL;
  278. snd_assert(substream->dma_buffer.dev.type != SNDRV_DMA_TYPE_UNKNOWN, return -EINVAL);
  279. snd_assert(substream != NULL, return -EINVAL);
  280. runtime = substream->runtime;
  281. snd_assert(runtime != NULL, return -EINVAL);
  282. if (runtime->dma_buffer_p) {
  283. /* perphaps, we might free the large DMA memory region
  284. to save some space here, but the actual solution
  285. costs us less time */
  286. if (runtime->dma_buffer_p->bytes >= size) {
  287. runtime->dma_bytes = size;
  288. return 0; /* ok, do not change */
  289. }
  290. snd_pcm_lib_free_pages(substream);
  291. }
  292. if (substream->dma_buffer.area != NULL && substream->dma_buffer.bytes >= size) {
  293. dmab = &substream->dma_buffer; /* use the pre-allocated buffer */
  294. } else {
  295. dmab = kcalloc(1, sizeof(*dmab), GFP_KERNEL);
  296. if (! dmab)
  297. return -ENOMEM;
  298. dmab->dev = substream->dma_buffer.dev;
  299. if (snd_dma_alloc_pages(substream->dma_buffer.dev.type,
  300. substream->dma_buffer.dev.dev,
  301. size, dmab) < 0) {
  302. kfree(dmab);
  303. return -ENOMEM;
  304. }
  305. }
  306. snd_pcm_set_runtime_buffer(substream, dmab);
  307. runtime->dma_bytes = size;
  308. return 1; /* area was changed */
  309. }
  310. /**
  311. * snd_pcm_lib_free_pages - release the allocated DMA buffer.
  312. * @substream: the substream to release the DMA buffer
  313. *
  314. * Releases the DMA buffer allocated via snd_pcm_lib_malloc_pages().
  315. *
  316. * Returns zero if successful, or a negative error code on failure.
  317. */
  318. int snd_pcm_lib_free_pages(snd_pcm_substream_t *substream)
  319. {
  320. snd_pcm_runtime_t *runtime;
  321. snd_assert(substream != NULL, return -EINVAL);
  322. runtime = substream->runtime;
  323. snd_assert(runtime != NULL, return -EINVAL);
  324. if (runtime->dma_area == NULL)
  325. return 0;
  326. if (runtime->dma_buffer_p != &substream->dma_buffer) {
  327. /* it's a newly allocated buffer. release it now. */
  328. snd_dma_free_pages(runtime->dma_buffer_p);
  329. kfree(runtime->dma_buffer_p);
  330. }
  331. snd_pcm_set_runtime_buffer(substream, NULL);
  332. return 0;
  333. }