pcm_memory.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 <linux/vmalloc.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. /* 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. #ifdef CONFIG_SND_DMA_SGBUF
  279. /**
  280. * snd_pcm_sgbuf_ops_page - get the page struct at the given offset
  281. * @substream: the pcm substream instance
  282. * @offset: the buffer offset
  283. *
  284. * Returns the page struct at the given buffer offset.
  285. * Used as the page callback of PCM ops.
  286. */
  287. struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream, unsigned long offset)
  288. {
  289. struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
  290. unsigned int idx = offset >> PAGE_SHIFT;
  291. if (idx >= (unsigned int)sgbuf->pages)
  292. return NULL;
  293. return sgbuf->page_table[idx];
  294. }
  295. EXPORT_SYMBOL(snd_pcm_sgbuf_ops_page);
  296. /*
  297. * compute the max chunk size with continuous pages on sg-buffer
  298. */
  299. unsigned int snd_pcm_sgbuf_get_chunk_size(struct snd_pcm_substream *substream,
  300. unsigned int ofs, unsigned int size)
  301. {
  302. struct snd_sg_buf *sg = snd_pcm_substream_sgbuf(substream);
  303. unsigned int start, end, pg;
  304. start = ofs >> PAGE_SHIFT;
  305. end = (ofs + size - 1) >> PAGE_SHIFT;
  306. /* check page continuity */
  307. pg = sg->table[start].addr >> PAGE_SHIFT;
  308. for (;;) {
  309. start++;
  310. if (start > end)
  311. break;
  312. pg++;
  313. if ((sg->table[start].addr >> PAGE_SHIFT) != pg)
  314. return (start << PAGE_SHIFT) - ofs;
  315. }
  316. /* ok, all on continuous pages */
  317. return size;
  318. }
  319. EXPORT_SYMBOL(snd_pcm_sgbuf_get_chunk_size);
  320. #endif /* CONFIG_SND_DMA_SGBUF */
  321. /**
  322. * snd_pcm_lib_malloc_pages - allocate the DMA buffer
  323. * @substream: the substream to allocate the DMA buffer to
  324. * @size: the requested buffer size in bytes
  325. *
  326. * Allocates the DMA buffer on the BUS type given earlier to
  327. * snd_pcm_lib_preallocate_xxx_pages().
  328. *
  329. * Returns 1 if the buffer is changed, 0 if not changed, or a negative
  330. * code on failure.
  331. */
  332. int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size)
  333. {
  334. struct snd_pcm_runtime *runtime;
  335. struct snd_dma_buffer *dmab = NULL;
  336. if (PCM_RUNTIME_CHECK(substream))
  337. return -EINVAL;
  338. if (snd_BUG_ON(substream->dma_buffer.dev.type ==
  339. SNDRV_DMA_TYPE_UNKNOWN))
  340. return -EINVAL;
  341. runtime = substream->runtime;
  342. if (runtime->dma_buffer_p) {
  343. /* perphaps, we might free the large DMA memory region
  344. to save some space here, but the actual solution
  345. costs us less time */
  346. if (runtime->dma_buffer_p->bytes >= size) {
  347. runtime->dma_bytes = size;
  348. return 0; /* ok, do not change */
  349. }
  350. snd_pcm_lib_free_pages(substream);
  351. }
  352. if (substream->dma_buffer.area != NULL &&
  353. substream->dma_buffer.bytes >= size) {
  354. dmab = &substream->dma_buffer; /* use the pre-allocated buffer */
  355. } else {
  356. dmab = kzalloc(sizeof(*dmab), GFP_KERNEL);
  357. if (! dmab)
  358. return -ENOMEM;
  359. dmab->dev = substream->dma_buffer.dev;
  360. if (snd_dma_alloc_pages(substream->dma_buffer.dev.type,
  361. substream->dma_buffer.dev.dev,
  362. size, dmab) < 0) {
  363. kfree(dmab);
  364. return -ENOMEM;
  365. }
  366. }
  367. snd_pcm_set_runtime_buffer(substream, dmab);
  368. runtime->dma_bytes = size;
  369. return 1; /* area was changed */
  370. }
  371. EXPORT_SYMBOL(snd_pcm_lib_malloc_pages);
  372. /**
  373. * snd_pcm_lib_free_pages - release the allocated DMA buffer.
  374. * @substream: the substream to release the DMA buffer
  375. *
  376. * Releases the DMA buffer allocated via snd_pcm_lib_malloc_pages().
  377. *
  378. * Returns zero if successful, or a negative error code on failure.
  379. */
  380. int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream)
  381. {
  382. struct snd_pcm_runtime *runtime;
  383. if (PCM_RUNTIME_CHECK(substream))
  384. return -EINVAL;
  385. runtime = substream->runtime;
  386. if (runtime->dma_area == NULL)
  387. return 0;
  388. if (runtime->dma_buffer_p != &substream->dma_buffer) {
  389. /* it's a newly allocated buffer. release it now. */
  390. snd_dma_free_pages(runtime->dma_buffer_p);
  391. kfree(runtime->dma_buffer_p);
  392. }
  393. snd_pcm_set_runtime_buffer(substream, NULL);
  394. return 0;
  395. }
  396. EXPORT_SYMBOL(snd_pcm_lib_free_pages);
  397. int _snd_pcm_lib_alloc_vmalloc_buffer(struct snd_pcm_substream *substream,
  398. size_t size, gfp_t gfp_flags)
  399. {
  400. struct snd_pcm_runtime *runtime;
  401. if (PCM_RUNTIME_CHECK(substream))
  402. return -EINVAL;
  403. runtime = substream->runtime;
  404. if (runtime->dma_area) {
  405. if (runtime->dma_bytes >= size)
  406. return 0; /* already large enough */
  407. vfree(runtime->dma_area);
  408. }
  409. runtime->dma_area = __vmalloc(size, gfp_flags, PAGE_KERNEL);
  410. if (!runtime->dma_area)
  411. return -ENOMEM;
  412. runtime->dma_bytes = size;
  413. return 1;
  414. }
  415. EXPORT_SYMBOL(_snd_pcm_lib_alloc_vmalloc_buffer);
  416. /**
  417. * snd_pcm_lib_free_vmalloc_buffer - free vmalloc buffer
  418. * @substream: the substream with a buffer allocated by
  419. * snd_pcm_lib_alloc_vmalloc_buffer()
  420. */
  421. int snd_pcm_lib_free_vmalloc_buffer(struct snd_pcm_substream *substream)
  422. {
  423. struct snd_pcm_runtime *runtime;
  424. if (PCM_RUNTIME_CHECK(substream))
  425. return -EINVAL;
  426. runtime = substream->runtime;
  427. vfree(runtime->dma_area);
  428. runtime->dma_area = NULL;
  429. return 0;
  430. }
  431. EXPORT_SYMBOL(snd_pcm_lib_free_vmalloc_buffer);
  432. /**
  433. * snd_pcm_lib_get_vmalloc_page - map vmalloc buffer offset to page struct
  434. * @substream: the substream with a buffer allocated by
  435. * snd_pcm_lib_alloc_vmalloc_buffer()
  436. * @offset: offset in the buffer
  437. *
  438. * This function is to be used as the page callback in the PCM ops.
  439. */
  440. struct page *snd_pcm_lib_get_vmalloc_page(struct snd_pcm_substream *substream,
  441. unsigned long offset)
  442. {
  443. return vmalloc_to_page(substream->runtime->dma_area + offset);
  444. }
  445. EXPORT_SYMBOL(snd_pcm_lib_get_vmalloc_page);