pcm_memory.c 14 KB

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