pcm_memory.c 13 KB

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