pcm_memory.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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(struct snd_pcm_substream *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(struct snd_pcm_substream *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(struct snd_pcm_substream *substream)
  90. {
  91. snd_pcm_lib_preallocate_dma_free(substream);
  92. snd_info_unregister(substream->proc_prealloc_entry);
  93. substream->proc_prealloc_entry = NULL;
  94. return 0;
  95. }
  96. /**
  97. * snd_pcm_lib_preallocate_free_for_all - release all pre-allocated buffers on the pcm
  98. * @pcm: the pcm instance
  99. *
  100. * Releases all the pre-allocated buffers on the given pcm.
  101. *
  102. * Returns zero if successful, or a negative error code on failure.
  103. */
  104. int snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm)
  105. {
  106. struct snd_pcm_substream *substream;
  107. int stream;
  108. for (stream = 0; stream < 2; stream++)
  109. for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
  110. snd_pcm_lib_preallocate_free(substream);
  111. return 0;
  112. }
  113. #ifdef CONFIG_PROC_FS
  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. * write callback for prealloc proc file
  127. *
  128. * accepts the preallocation size in kB.
  129. */
  130. static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,
  131. struct snd_info_buffer *buffer)
  132. {
  133. struct snd_pcm_substream *substream = entry->private_data;
  134. char line[64], str[64];
  135. size_t size;
  136. struct snd_dma_buffer new_dmab;
  137. if (substream->runtime) {
  138. buffer->error = -EBUSY;
  139. return;
  140. }
  141. if (!snd_info_get_line(buffer, line, sizeof(line))) {
  142. snd_info_get_str(str, line, sizeof(str));
  143. size = simple_strtoul(str, NULL, 10) * 1024;
  144. if ((size != 0 && size < 8192) || size > substream->dma_max) {
  145. buffer->error = -EINVAL;
  146. return;
  147. }
  148. if (substream->dma_buffer.bytes == size)
  149. return;
  150. memset(&new_dmab, 0, sizeof(new_dmab));
  151. new_dmab.dev = substream->dma_buffer.dev;
  152. if (size > 0) {
  153. if (snd_dma_alloc_pages(substream->dma_buffer.dev.type,
  154. substream->dma_buffer.dev.dev,
  155. size, &new_dmab) < 0) {
  156. buffer->error = -ENOMEM;
  157. return;
  158. }
  159. substream->buffer_bytes_max = size;
  160. } else {
  161. substream->buffer_bytes_max = UINT_MAX;
  162. }
  163. if (substream->dma_buffer.area)
  164. snd_dma_free_pages(&substream->dma_buffer);
  165. substream->dma_buffer = new_dmab;
  166. } else {
  167. buffer->error = -EINVAL;
  168. }
  169. }
  170. static inline void preallocate_info_init(struct snd_pcm_substream *substream)
  171. {
  172. struct snd_info_entry *entry;
  173. if ((entry = snd_info_create_card_entry(substream->pcm->card, "prealloc", substream->proc_root)) != NULL) {
  174. entry->c.text.read_size = 64;
  175. entry->c.text.read = snd_pcm_lib_preallocate_proc_read;
  176. entry->c.text.write_size = 64;
  177. entry->c.text.write = snd_pcm_lib_preallocate_proc_write;
  178. entry->mode |= S_IWUSR;
  179. entry->private_data = substream;
  180. if (snd_info_register(entry) < 0) {
  181. snd_info_free_entry(entry);
  182. entry = NULL;
  183. }
  184. }
  185. substream->proc_prealloc_entry = entry;
  186. }
  187. #else /* !CONFIG_PROC_FS */
  188. #define preallocate_info_init(s)
  189. #endif
  190. /*
  191. * pre-allocate the buffer and create a proc file for the substream
  192. */
  193. static int snd_pcm_lib_preallocate_pages1(struct snd_pcm_substream *substream,
  194. size_t size, size_t max)
  195. {
  196. if (size > 0 && preallocate_dma && substream->number < maximum_substreams)
  197. preallocate_pcm_pages(substream, size);
  198. if (substream->dma_buffer.bytes > 0)
  199. substream->buffer_bytes_max = substream->dma_buffer.bytes;
  200. substream->dma_max = max;
  201. preallocate_info_init(substream);
  202. return 0;
  203. }
  204. /**
  205. * snd_pcm_lib_preallocate_pages - pre-allocation for the given DMA type
  206. * @substream: the pcm substream instance
  207. * @type: DMA type (SNDRV_DMA_TYPE_*)
  208. * @data: DMA type dependant data
  209. * @size: the requested pre-allocation size in bytes
  210. * @max: the max. allowed pre-allocation size
  211. *
  212. * Do pre-allocation for the given DMA buffer type.
  213. *
  214. * When substream->dma_buf_id is set, the function tries to look for
  215. * the reserved buffer, and the buffer is not freed but reserved at
  216. * destruction time. The dma_buf_id must be unique for all systems
  217. * (in the same DMA buffer type) e.g. using snd_dma_pci_buf_id().
  218. *
  219. * Returns zero if successful, or a negative error code on failure.
  220. */
  221. int snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream,
  222. int type, struct device *data,
  223. size_t size, size_t max)
  224. {
  225. substream->dma_buffer.dev.type = type;
  226. substream->dma_buffer.dev.dev = data;
  227. return snd_pcm_lib_preallocate_pages1(substream, size, max);
  228. }
  229. /**
  230. * snd_pcm_lib_preallocate_pages_for_all - pre-allocation for continous memory type (all substreams)
  231. * @pcm: the pcm instance
  232. * @type: DMA type (SNDRV_DMA_TYPE_*)
  233. * @data: DMA type dependant data
  234. * @size: the requested pre-allocation size in bytes
  235. * @max: the max. allowed pre-allocation size
  236. *
  237. * Do pre-allocation to all substreams of the given pcm for the
  238. * specified DMA type.
  239. *
  240. * Returns zero if successful, or a negative error code on failure.
  241. */
  242. int snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,
  243. int type, void *data,
  244. size_t size, size_t max)
  245. {
  246. struct snd_pcm_substream *substream;
  247. int stream, err;
  248. for (stream = 0; stream < 2; stream++)
  249. for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
  250. if ((err = snd_pcm_lib_preallocate_pages(substream, type, data, size, max)) < 0)
  251. return err;
  252. return 0;
  253. }
  254. /**
  255. * snd_pcm_sgbuf_ops_page - get the page struct at the given offset
  256. * @substream: the pcm substream instance
  257. * @offset: the buffer offset
  258. *
  259. * Returns the page struct at the given buffer offset.
  260. * Used as the page callback of PCM ops.
  261. */
  262. struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream, unsigned long offset)
  263. {
  264. struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
  265. unsigned int idx = offset >> PAGE_SHIFT;
  266. if (idx >= (unsigned int)sgbuf->pages)
  267. return NULL;
  268. return sgbuf->page_table[idx];
  269. }
  270. /**
  271. * snd_pcm_lib_malloc_pages - allocate the DMA buffer
  272. * @substream: the substream to allocate the DMA buffer to
  273. * @size: the requested buffer size in bytes
  274. *
  275. * Allocates the DMA buffer on the BUS type given earlier to
  276. * snd_pcm_lib_preallocate_xxx_pages().
  277. *
  278. * Returns 1 if the buffer is changed, 0 if not changed, or a negative
  279. * code on failure.
  280. */
  281. int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size)
  282. {
  283. struct snd_pcm_runtime *runtime;
  284. struct snd_dma_buffer *dmab = NULL;
  285. snd_assert(substream->dma_buffer.dev.type != SNDRV_DMA_TYPE_UNKNOWN, return -EINVAL);
  286. snd_assert(substream != NULL, return -EINVAL);
  287. runtime = substream->runtime;
  288. snd_assert(runtime != NULL, return -EINVAL);
  289. if (runtime->dma_buffer_p) {
  290. /* perphaps, we might free the large DMA memory region
  291. to save some space here, but the actual solution
  292. costs us less time */
  293. if (runtime->dma_buffer_p->bytes >= size) {
  294. runtime->dma_bytes = size;
  295. return 0; /* ok, do not change */
  296. }
  297. snd_pcm_lib_free_pages(substream);
  298. }
  299. if (substream->dma_buffer.area != NULL &&
  300. substream->dma_buffer.bytes >= size) {
  301. dmab = &substream->dma_buffer; /* use the pre-allocated buffer */
  302. } else {
  303. dmab = kzalloc(sizeof(*dmab), GFP_KERNEL);
  304. if (! dmab)
  305. return -ENOMEM;
  306. dmab->dev = substream->dma_buffer.dev;
  307. if (snd_dma_alloc_pages(substream->dma_buffer.dev.type,
  308. substream->dma_buffer.dev.dev,
  309. size, dmab) < 0) {
  310. kfree(dmab);
  311. return -ENOMEM;
  312. }
  313. }
  314. snd_pcm_set_runtime_buffer(substream, dmab);
  315. runtime->dma_bytes = size;
  316. return 1; /* area was changed */
  317. }
  318. /**
  319. * snd_pcm_lib_free_pages - release the allocated DMA buffer.
  320. * @substream: the substream to release the DMA buffer
  321. *
  322. * Releases the DMA buffer allocated via snd_pcm_lib_malloc_pages().
  323. *
  324. * Returns zero if successful, or a negative error code on failure.
  325. */
  326. int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream)
  327. {
  328. struct snd_pcm_runtime *runtime;
  329. snd_assert(substream != NULL, return -EINVAL);
  330. runtime = substream->runtime;
  331. snd_assert(runtime != NULL, return -EINVAL);
  332. if (runtime->dma_area == NULL)
  333. return 0;
  334. if (runtime->dma_buffer_p != &substream->dma_buffer) {
  335. /* it's a newly allocated buffer. release it now. */
  336. snd_dma_free_pages(runtime->dma_buffer_p);
  337. kfree(runtime->dma_buffer_p);
  338. }
  339. snd_pcm_set_runtime_buffer(substream, NULL);
  340. return 0;
  341. }