pcm_memory.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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/slab.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/vmalloc.h>
  27. #include <sound/core.h>
  28. #include <sound/pcm.h>
  29. #include <sound/info.h>
  30. #include <sound/initval.h>
  31. static int preallocate_dma = 1;
  32. module_param(preallocate_dma, int, 0444);
  33. MODULE_PARM_DESC(preallocate_dma, "Preallocate DMA memory when the PCM devices are initialized.");
  34. static int maximum_substreams = 4;
  35. module_param(maximum_substreams, int, 0444);
  36. MODULE_PARM_DESC(maximum_substreams, "Maximum substreams with preallocated DMA memory.");
  37. static const size_t snd_minimum_buffer = 16384;
  38. /*
  39. * try to allocate as the large pages as possible.
  40. * stores the resultant memory size in *res_size.
  41. *
  42. * the minimum size is snd_minimum_buffer. it should be power of 2.
  43. */
  44. static int preallocate_pcm_pages(struct snd_pcm_substream *substream, size_t size)
  45. {
  46. struct snd_dma_buffer *dmab = &substream->dma_buffer;
  47. int err;
  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. #ifdef CONFIG_SND_VERBOSE_PROCFS
  93. snd_info_free_entry(substream->proc_prealloc_max_entry);
  94. substream->proc_prealloc_max_entry = NULL;
  95. snd_info_free_entry(substream->proc_prealloc_entry);
  96. substream->proc_prealloc_entry = NULL;
  97. #endif
  98. return 0;
  99. }
  100. /**
  101. * snd_pcm_lib_preallocate_free_for_all - release all pre-allocated buffers on the pcm
  102. * @pcm: the pcm instance
  103. *
  104. * Releases all the pre-allocated buffers on the given pcm.
  105. *
  106. * Returns zero if successful, or a negative error code on failure.
  107. */
  108. int snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm)
  109. {
  110. struct snd_pcm_substream *substream;
  111. int stream;
  112. for (stream = 0; stream < 2; stream++)
  113. for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
  114. snd_pcm_lib_preallocate_free(substream);
  115. return 0;
  116. }
  117. EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all);
  118. #ifdef CONFIG_SND_VERBOSE_PROCFS
  119. /*
  120. * read callback for prealloc proc file
  121. *
  122. * prints the current allocated size in kB.
  123. */
  124. static void snd_pcm_lib_preallocate_proc_read(struct snd_info_entry *entry,
  125. struct snd_info_buffer *buffer)
  126. {
  127. struct snd_pcm_substream *substream = entry->private_data;
  128. snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_buffer.bytes / 1024);
  129. }
  130. /*
  131. * read callback for prealloc_max proc file
  132. *
  133. * prints the maximum allowed size in kB.
  134. */
  135. static void snd_pcm_lib_preallocate_max_proc_read(struct snd_info_entry *entry,
  136. struct snd_info_buffer *buffer)
  137. {
  138. struct snd_pcm_substream *substream = entry->private_data;
  139. snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_max / 1024);
  140. }
  141. /*
  142. * write callback for prealloc proc file
  143. *
  144. * accepts the preallocation size in kB.
  145. */
  146. static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,
  147. struct snd_info_buffer *buffer)
  148. {
  149. struct snd_pcm_substream *substream = entry->private_data;
  150. char line[64], str[64];
  151. size_t size;
  152. struct snd_dma_buffer new_dmab;
  153. if (substream->runtime) {
  154. buffer->error = -EBUSY;
  155. return;
  156. }
  157. if (!snd_info_get_line(buffer, line, sizeof(line))) {
  158. snd_info_get_str(str, line, sizeof(str));
  159. size = simple_strtoul(str, NULL, 10) * 1024;
  160. if ((size != 0 && size < 8192) || size > substream->dma_max) {
  161. buffer->error = -EINVAL;
  162. return;
  163. }
  164. if (substream->dma_buffer.bytes == size)
  165. return;
  166. memset(&new_dmab, 0, sizeof(new_dmab));
  167. new_dmab.dev = substream->dma_buffer.dev;
  168. if (size > 0) {
  169. if (snd_dma_alloc_pages(substream->dma_buffer.dev.type,
  170. substream->dma_buffer.dev.dev,
  171. size, &new_dmab) < 0) {
  172. buffer->error = -ENOMEM;
  173. return;
  174. }
  175. substream->buffer_bytes_max = size;
  176. } else {
  177. substream->buffer_bytes_max = UINT_MAX;
  178. }
  179. if (substream->dma_buffer.area)
  180. snd_dma_free_pages(&substream->dma_buffer);
  181. substream->dma_buffer = new_dmab;
  182. } else {
  183. buffer->error = -EINVAL;
  184. }
  185. }
  186. static inline void preallocate_info_init(struct snd_pcm_substream *substream)
  187. {
  188. struct snd_info_entry *entry;
  189. if ((entry = snd_info_create_card_entry(substream->pcm->card, "prealloc", substream->proc_root)) != NULL) {
  190. entry->c.text.read = snd_pcm_lib_preallocate_proc_read;
  191. entry->c.text.write = snd_pcm_lib_preallocate_proc_write;
  192. entry->mode |= S_IWUSR;
  193. entry->private_data = substream;
  194. if (snd_info_register(entry) < 0) {
  195. snd_info_free_entry(entry);
  196. entry = NULL;
  197. }
  198. }
  199. substream->proc_prealloc_entry = entry;
  200. if ((entry = snd_info_create_card_entry(substream->pcm->card, "prealloc_max", substream->proc_root)) != NULL) {
  201. entry->c.text.read = snd_pcm_lib_preallocate_max_proc_read;
  202. entry->private_data = substream;
  203. if (snd_info_register(entry) < 0) {
  204. snd_info_free_entry(entry);
  205. entry = NULL;
  206. }
  207. }
  208. substream->proc_prealloc_max_entry = entry;
  209. }
  210. #else /* !CONFIG_SND_VERBOSE_PROCFS */
  211. #define preallocate_info_init(s)
  212. #endif /* CONFIG_SND_VERBOSE_PROCFS */
  213. /*
  214. * pre-allocate the buffer and create a proc file for the substream
  215. */
  216. static int snd_pcm_lib_preallocate_pages1(struct snd_pcm_substream *substream,
  217. size_t size, size_t max)
  218. {
  219. if (size > 0 && preallocate_dma && substream->number < maximum_substreams)
  220. preallocate_pcm_pages(substream, size);
  221. if (substream->dma_buffer.bytes > 0)
  222. substream->buffer_bytes_max = substream->dma_buffer.bytes;
  223. substream->dma_max = max;
  224. preallocate_info_init(substream);
  225. return 0;
  226. }
  227. /**
  228. * snd_pcm_lib_preallocate_pages - pre-allocation for the given DMA type
  229. * @substream: the pcm substream instance
  230. * @type: DMA type (SNDRV_DMA_TYPE_*)
  231. * @data: DMA type dependent data
  232. * @size: the requested pre-allocation size in bytes
  233. * @max: the max. allowed pre-allocation size
  234. *
  235. * Do pre-allocation for the given DMA buffer type.
  236. *
  237. * When substream->dma_buf_id is set, the function tries to look for
  238. * the reserved buffer, and the buffer is not freed but reserved at
  239. * destruction time. The dma_buf_id must be unique for all systems
  240. * (in the same DMA buffer type) e.g. using snd_dma_pci_buf_id().
  241. *
  242. * Returns zero if successful, or a negative error code on failure.
  243. */
  244. int snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream,
  245. int type, struct device *data,
  246. size_t size, size_t max)
  247. {
  248. substream->dma_buffer.dev.type = type;
  249. substream->dma_buffer.dev.dev = data;
  250. return snd_pcm_lib_preallocate_pages1(substream, size, max);
  251. }
  252. EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages);
  253. /**
  254. * snd_pcm_lib_preallocate_pages_for_all - pre-allocation for continuous memory type (all substreams)
  255. * @pcm: the pcm instance
  256. * @type: DMA type (SNDRV_DMA_TYPE_*)
  257. * @data: DMA type dependent data
  258. * @size: the requested pre-allocation size in bytes
  259. * @max: the max. allowed pre-allocation size
  260. *
  261. * Do pre-allocation to all substreams of the given pcm for the
  262. * specified DMA type.
  263. *
  264. * Returns zero if successful, or a negative error code on failure.
  265. */
  266. int snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,
  267. int type, void *data,
  268. size_t size, size_t max)
  269. {
  270. struct snd_pcm_substream *substream;
  271. int stream, err;
  272. for (stream = 0; stream < 2; stream++)
  273. for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
  274. if ((err = snd_pcm_lib_preallocate_pages(substream, type, data, size, max)) < 0)
  275. return err;
  276. return 0;
  277. }
  278. EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all);
  279. #ifdef CONFIG_SND_DMA_SGBUF
  280. /**
  281. * snd_pcm_sgbuf_ops_page - get the page struct at the given offset
  282. * @substream: the pcm substream instance
  283. * @offset: the buffer offset
  284. *
  285. * Returns the page struct at the given buffer offset.
  286. * Used as the page callback of PCM ops.
  287. */
  288. struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream, unsigned long offset)
  289. {
  290. struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
  291. unsigned int idx = offset >> PAGE_SHIFT;
  292. if (idx >= (unsigned int)sgbuf->pages)
  293. return NULL;
  294. return sgbuf->page_table[idx];
  295. }
  296. EXPORT_SYMBOL(snd_pcm_sgbuf_ops_page);
  297. /*
  298. * compute the max chunk size with continuous pages on sg-buffer
  299. */
  300. unsigned int snd_pcm_sgbuf_get_chunk_size(struct snd_pcm_substream *substream,
  301. unsigned int ofs, unsigned int size)
  302. {
  303. struct snd_sg_buf *sg = snd_pcm_substream_sgbuf(substream);
  304. unsigned int start, end, pg;
  305. start = ofs >> PAGE_SHIFT;
  306. end = (ofs + size - 1) >> PAGE_SHIFT;
  307. /* check page continuity */
  308. pg = sg->table[start].addr >> PAGE_SHIFT;
  309. for (;;) {
  310. start++;
  311. if (start > end)
  312. break;
  313. pg++;
  314. if ((sg->table[start].addr >> PAGE_SHIFT) != pg)
  315. return (start << PAGE_SHIFT) - ofs;
  316. }
  317. /* ok, all on continuous pages */
  318. return size;
  319. }
  320. EXPORT_SYMBOL(snd_pcm_sgbuf_get_chunk_size);
  321. #endif /* CONFIG_SND_DMA_SGBUF */
  322. /**
  323. * snd_pcm_lib_malloc_pages - allocate the DMA buffer
  324. * @substream: the substream to allocate the DMA buffer to
  325. * @size: the requested buffer size in bytes
  326. *
  327. * Allocates the DMA buffer on the BUS type given earlier to
  328. * snd_pcm_lib_preallocate_xxx_pages().
  329. *
  330. * Returns 1 if the buffer is changed, 0 if not changed, or a negative
  331. * code on failure.
  332. */
  333. int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size)
  334. {
  335. struct snd_pcm_runtime *runtime;
  336. struct snd_dma_buffer *dmab = NULL;
  337. if (PCM_RUNTIME_CHECK(substream))
  338. return -EINVAL;
  339. if (snd_BUG_ON(substream->dma_buffer.dev.type ==
  340. SNDRV_DMA_TYPE_UNKNOWN))
  341. return -EINVAL;
  342. runtime = substream->runtime;
  343. if (runtime->dma_buffer_p) {
  344. /* perphaps, we might free the large DMA memory region
  345. to save some space here, but the actual solution
  346. costs us less time */
  347. if (runtime->dma_buffer_p->bytes >= size) {
  348. runtime->dma_bytes = size;
  349. return 0; /* ok, do not change */
  350. }
  351. snd_pcm_lib_free_pages(substream);
  352. }
  353. if (substream->dma_buffer.area != NULL &&
  354. substream->dma_buffer.bytes >= size) {
  355. dmab = &substream->dma_buffer; /* use the pre-allocated buffer */
  356. } else {
  357. dmab = kzalloc(sizeof(*dmab), GFP_KERNEL);
  358. if (! dmab)
  359. return -ENOMEM;
  360. dmab->dev = substream->dma_buffer.dev;
  361. if (snd_dma_alloc_pages(substream->dma_buffer.dev.type,
  362. substream->dma_buffer.dev.dev,
  363. size, dmab) < 0) {
  364. kfree(dmab);
  365. return -ENOMEM;
  366. }
  367. }
  368. snd_pcm_set_runtime_buffer(substream, dmab);
  369. runtime->dma_bytes = size;
  370. return 1; /* area was changed */
  371. }
  372. EXPORT_SYMBOL(snd_pcm_lib_malloc_pages);
  373. /**
  374. * snd_pcm_lib_free_pages - release the allocated DMA buffer.
  375. * @substream: the substream to release the DMA buffer
  376. *
  377. * Releases the DMA buffer allocated via snd_pcm_lib_malloc_pages().
  378. *
  379. * Returns zero if successful, or a negative error code on failure.
  380. */
  381. int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream)
  382. {
  383. struct snd_pcm_runtime *runtime;
  384. if (PCM_RUNTIME_CHECK(substream))
  385. return -EINVAL;
  386. runtime = substream->runtime;
  387. if (runtime->dma_area == NULL)
  388. return 0;
  389. if (runtime->dma_buffer_p != &substream->dma_buffer) {
  390. /* it's a newly allocated buffer. release it now. */
  391. snd_dma_free_pages(runtime->dma_buffer_p);
  392. kfree(runtime->dma_buffer_p);
  393. }
  394. snd_pcm_set_runtime_buffer(substream, NULL);
  395. return 0;
  396. }
  397. EXPORT_SYMBOL(snd_pcm_lib_free_pages);
  398. int _snd_pcm_lib_alloc_vmalloc_buffer(struct snd_pcm_substream *substream,
  399. size_t size, gfp_t gfp_flags)
  400. {
  401. struct snd_pcm_runtime *runtime;
  402. if (PCM_RUNTIME_CHECK(substream))
  403. return -EINVAL;
  404. runtime = substream->runtime;
  405. if (runtime->dma_area) {
  406. if (runtime->dma_bytes >= size)
  407. return 0; /* already large enough */
  408. vfree(runtime->dma_area);
  409. }
  410. runtime->dma_area = __vmalloc(size, gfp_flags, PAGE_KERNEL);
  411. if (!runtime->dma_area)
  412. return -ENOMEM;
  413. runtime->dma_bytes = size;
  414. return 1;
  415. }
  416. EXPORT_SYMBOL(_snd_pcm_lib_alloc_vmalloc_buffer);
  417. /**
  418. * snd_pcm_lib_free_vmalloc_buffer - free vmalloc buffer
  419. * @substream: the substream with a buffer allocated by
  420. * snd_pcm_lib_alloc_vmalloc_buffer()
  421. */
  422. int snd_pcm_lib_free_vmalloc_buffer(struct snd_pcm_substream *substream)
  423. {
  424. struct snd_pcm_runtime *runtime;
  425. if (PCM_RUNTIME_CHECK(substream))
  426. return -EINVAL;
  427. runtime = substream->runtime;
  428. vfree(runtime->dma_area);
  429. runtime->dma_area = NULL;
  430. return 0;
  431. }
  432. EXPORT_SYMBOL(snd_pcm_lib_free_vmalloc_buffer);
  433. /**
  434. * snd_pcm_lib_get_vmalloc_page - map vmalloc buffer offset to page struct
  435. * @substream: the substream with a buffer allocated by
  436. * snd_pcm_lib_alloc_vmalloc_buffer()
  437. * @offset: offset in the buffer
  438. *
  439. * This function is to be used as the page callback in the PCM ops.
  440. */
  441. struct page *snd_pcm_lib_get_vmalloc_page(struct snd_pcm_substream *substream,
  442. unsigned long offset)
  443. {
  444. return vmalloc_to_page(substream->runtime->dma_area + offset);
  445. }
  446. EXPORT_SYMBOL(snd_pcm_lib_get_vmalloc_page);