pcm_memory.c 12 KB

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