pcm_memory.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Digital Audio (PCM) abstract layer
  4. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  5. */
  6. #include <linux/io.h>
  7. #include <linux/time.h>
  8. #include <linux/init.h>
  9. #include <linux/slab.h>
  10. #include <linux/moduleparam.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/export.h>
  13. #include <sound/core.h>
  14. #include <sound/pcm.h>
  15. #include <sound/info.h>
  16. #include <sound/initval.h>
  17. #include "pcm_local.h"
  18. static int preallocate_dma = 1;
  19. module_param(preallocate_dma, int, 0444);
  20. MODULE_PARM_DESC(preallocate_dma, "Preallocate DMA memory when the PCM devices are initialized.");
  21. static int maximum_substreams = 4;
  22. module_param(maximum_substreams, int, 0444);
  23. MODULE_PARM_DESC(maximum_substreams, "Maximum substreams with preallocated DMA memory.");
  24. static const size_t snd_minimum_buffer = 16384;
  25. static unsigned long max_alloc_per_card = 32UL * 1024UL * 1024UL;
  26. module_param(max_alloc_per_card, ulong, 0644);
  27. MODULE_PARM_DESC(max_alloc_per_card, "Max total allocation bytes per card.");
  28. static int do_alloc_pages(struct snd_card *card, int type, struct device *dev,
  29. size_t size, struct snd_dma_buffer *dmab)
  30. {
  31. int err;
  32. if (max_alloc_per_card &&
  33. card->total_pcm_alloc_bytes + size > max_alloc_per_card)
  34. return -ENOMEM;
  35. err = snd_dma_alloc_pages(type, dev, size, dmab);
  36. if (!err) {
  37. mutex_lock(&card->memory_mutex);
  38. card->total_pcm_alloc_bytes += dmab->bytes;
  39. mutex_unlock(&card->memory_mutex);
  40. }
  41. return err;
  42. }
  43. static void do_free_pages(struct snd_card *card, struct snd_dma_buffer *dmab)
  44. {
  45. if (!dmab->area)
  46. return;
  47. mutex_lock(&card->memory_mutex);
  48. WARN_ON(card->total_pcm_alloc_bytes < dmab->bytes);
  49. card->total_pcm_alloc_bytes -= dmab->bytes;
  50. mutex_unlock(&card->memory_mutex);
  51. snd_dma_free_pages(dmab);
  52. dmab->area = NULL;
  53. }
  54. /*
  55. * try to allocate as the large pages as possible.
  56. * stores the resultant memory size in *res_size.
  57. *
  58. * the minimum size is snd_minimum_buffer. it should be power of 2.
  59. */
  60. static int preallocate_pcm_pages(struct snd_pcm_substream *substream, size_t size)
  61. {
  62. struct snd_dma_buffer *dmab = &substream->dma_buffer;
  63. struct snd_card *card = substream->pcm->card;
  64. size_t orig_size = size;
  65. int err;
  66. do {
  67. err = do_alloc_pages(card, dmab->dev.type, dmab->dev.dev,
  68. size, dmab);
  69. if (err != -ENOMEM)
  70. return err;
  71. size >>= 1;
  72. } while (size >= snd_minimum_buffer);
  73. dmab->bytes = 0; /* tell error */
  74. pr_warn("ALSA pcmC%dD%d%c,%d:%s: cannot preallocate for size %zu\n",
  75. substream->pcm->card->number, substream->pcm->device,
  76. substream->stream ? 'c' : 'p', substream->number,
  77. substream->pcm->name, orig_size);
  78. return 0;
  79. }
  80. /*
  81. * release the preallocated buffer if not yet done.
  82. */
  83. static void snd_pcm_lib_preallocate_dma_free(struct snd_pcm_substream *substream)
  84. {
  85. do_free_pages(substream->pcm->card, &substream->dma_buffer);
  86. }
  87. /**
  88. * snd_pcm_lib_preallocate_free - release the preallocated buffer of the specified substream.
  89. * @substream: the pcm substream instance
  90. *
  91. * Releases the pre-allocated buffer of the given substream.
  92. */
  93. void snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream)
  94. {
  95. snd_pcm_lib_preallocate_dma_free(substream);
  96. }
  97. /**
  98. * snd_pcm_lib_preallocate_free_for_all - release all pre-allocated buffers on the pcm
  99. * @pcm: the pcm instance
  100. *
  101. * Releases all the pre-allocated buffers on the given pcm.
  102. */
  103. void snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm)
  104. {
  105. struct snd_pcm_substream *substream;
  106. int stream;
  107. for (stream = 0; stream < 2; stream++)
  108. for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
  109. snd_pcm_lib_preallocate_free(substream);
  110. }
  111. EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all);
  112. #ifdef CONFIG_SND_VERBOSE_PROCFS
  113. /*
  114. * read callback for prealloc proc file
  115. *
  116. * prints the current allocated size in kB.
  117. */
  118. static void snd_pcm_lib_preallocate_proc_read(struct snd_info_entry *entry,
  119. struct snd_info_buffer *buffer)
  120. {
  121. struct snd_pcm_substream *substream = entry->private_data;
  122. snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_buffer.bytes / 1024);
  123. }
  124. /*
  125. * read callback for prealloc_max proc file
  126. *
  127. * prints the maximum allowed size in kB.
  128. */
  129. static void snd_pcm_lib_preallocate_max_proc_read(struct snd_info_entry *entry,
  130. struct snd_info_buffer *buffer)
  131. {
  132. struct snd_pcm_substream *substream = entry->private_data;
  133. snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_max / 1024);
  134. }
  135. /*
  136. * write callback for prealloc proc file
  137. *
  138. * accepts the preallocation size in kB.
  139. */
  140. static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,
  141. struct snd_info_buffer *buffer)
  142. {
  143. struct snd_pcm_substream *substream = entry->private_data;
  144. struct snd_card *card = substream->pcm->card;
  145. char line[64], str[64];
  146. size_t size;
  147. struct snd_dma_buffer new_dmab;
  148. mutex_lock(&substream->pcm->open_mutex);
  149. if (substream->runtime) {
  150. buffer->error = -EBUSY;
  151. goto unlock;
  152. }
  153. if (!snd_info_get_line(buffer, line, sizeof(line))) {
  154. snd_info_get_str(str, line, sizeof(str));
  155. size = simple_strtoul(str, NULL, 10) * 1024;
  156. if ((size != 0 && size < 8192) || size > substream->dma_max) {
  157. buffer->error = -EINVAL;
  158. goto unlock;
  159. }
  160. if (substream->dma_buffer.bytes == size)
  161. goto unlock;
  162. memset(&new_dmab, 0, sizeof(new_dmab));
  163. new_dmab.dev = substream->dma_buffer.dev;
  164. if (size > 0) {
  165. if (do_alloc_pages(card,
  166. substream->dma_buffer.dev.type,
  167. substream->dma_buffer.dev.dev,
  168. size, &new_dmab) < 0) {
  169. buffer->error = -ENOMEM;
  170. goto unlock;
  171. }
  172. substream->buffer_bytes_max = size;
  173. } else {
  174. substream->buffer_bytes_max = UINT_MAX;
  175. }
  176. if (substream->dma_buffer.area)
  177. do_free_pages(card, &substream->dma_buffer);
  178. substream->dma_buffer = new_dmab;
  179. } else {
  180. buffer->error = -EINVAL;
  181. }
  182. unlock:
  183. mutex_unlock(&substream->pcm->open_mutex);
  184. }
  185. static inline void preallocate_info_init(struct snd_pcm_substream *substream)
  186. {
  187. struct snd_info_entry *entry;
  188. entry = snd_info_create_card_entry(substream->pcm->card, "prealloc",
  189. substream->proc_root);
  190. if (entry) {
  191. snd_info_set_text_ops(entry, substream,
  192. snd_pcm_lib_preallocate_proc_read);
  193. entry->c.text.write = snd_pcm_lib_preallocate_proc_write;
  194. entry->mode |= 0200;
  195. }
  196. entry = snd_info_create_card_entry(substream->pcm->card, "prealloc_max",
  197. substream->proc_root);
  198. if (entry)
  199. snd_info_set_text_ops(entry, substream,
  200. snd_pcm_lib_preallocate_max_proc_read);
  201. }
  202. #else /* !CONFIG_SND_VERBOSE_PROCFS */
  203. #define preallocate_info_init(s)
  204. #endif /* CONFIG_SND_VERBOSE_PROCFS */
  205. /*
  206. * pre-allocate the buffer and create a proc file for the substream
  207. */
  208. static void preallocate_pages(struct snd_pcm_substream *substream,
  209. int type, struct device *data,
  210. size_t size, size_t max, bool managed)
  211. {
  212. if (snd_BUG_ON(substream->dma_buffer.dev.type))
  213. return;
  214. substream->dma_buffer.dev.type = type;
  215. substream->dma_buffer.dev.dev = data;
  216. if (size > 0 && preallocate_dma && substream->number < maximum_substreams)
  217. preallocate_pcm_pages(substream, size);
  218. if (substream->dma_buffer.bytes > 0)
  219. substream->buffer_bytes_max = substream->dma_buffer.bytes;
  220. substream->dma_max = max;
  221. if (max > 0)
  222. preallocate_info_init(substream);
  223. if (managed)
  224. substream->managed_buffer_alloc = 1;
  225. }
  226. static void preallocate_pages_for_all(struct snd_pcm *pcm, int type,
  227. void *data, size_t size, size_t max,
  228. bool managed)
  229. {
  230. struct snd_pcm_substream *substream;
  231. int stream;
  232. for (stream = 0; stream < 2; stream++)
  233. for (substream = pcm->streams[stream].substream; substream;
  234. substream = substream->next)
  235. preallocate_pages(substream, type, data, size, max,
  236. managed);
  237. }
  238. /**
  239. * snd_pcm_lib_preallocate_pages - pre-allocation for the given DMA type
  240. * @substream: the pcm substream instance
  241. * @type: DMA type (SNDRV_DMA_TYPE_*)
  242. * @data: DMA type dependent data
  243. * @size: the requested pre-allocation size in bytes
  244. * @max: the max. allowed pre-allocation size
  245. *
  246. * Do pre-allocation for the given DMA buffer type.
  247. */
  248. void snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream,
  249. int type, struct device *data,
  250. size_t size, size_t max)
  251. {
  252. preallocate_pages(substream, type, data, size, max, false);
  253. }
  254. EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages);
  255. /**
  256. * snd_pcm_lib_preallocate_pages_for_all - pre-allocation for continuous memory type (all substreams)
  257. * @pcm: the pcm instance
  258. * @type: DMA type (SNDRV_DMA_TYPE_*)
  259. * @data: DMA type dependent data
  260. * @size: the requested pre-allocation size in bytes
  261. * @max: the max. allowed pre-allocation size
  262. *
  263. * Do pre-allocation to all substreams of the given pcm for the
  264. * specified DMA type.
  265. */
  266. void 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. preallocate_pages_for_all(pcm, type, data, size, max, false);
  271. }
  272. EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all);
  273. /**
  274. * snd_pcm_set_managed_buffer - set up buffer management for a substream
  275. * @substream: the pcm substream instance
  276. * @type: DMA type (SNDRV_DMA_TYPE_*)
  277. * @data: DMA type dependent data
  278. * @size: the requested pre-allocation size in bytes
  279. * @max: the max. allowed pre-allocation size
  280. *
  281. * Do pre-allocation for the given DMA buffer type, and set the managed
  282. * buffer allocation mode to the given substream.
  283. * In this mode, PCM core will allocate a buffer automatically before PCM
  284. * hw_params ops call, and release the buffer after PCM hw_free ops call
  285. * as well, so that the driver doesn't need to invoke the allocation and
  286. * the release explicitly in its callback.
  287. * When a buffer is actually allocated before the PCM hw_params call, it
  288. * turns on the runtime buffer_changed flag for drivers changing their h/w
  289. * parameters accordingly.
  290. */
  291. void snd_pcm_set_managed_buffer(struct snd_pcm_substream *substream, int type,
  292. struct device *data, size_t size, size_t max)
  293. {
  294. preallocate_pages(substream, type, data, size, max, true);
  295. }
  296. EXPORT_SYMBOL(snd_pcm_set_managed_buffer);
  297. /**
  298. * snd_pcm_set_managed_buffer_all - set up buffer management for all substreams
  299. * for all substreams
  300. * @pcm: the pcm instance
  301. * @type: DMA type (SNDRV_DMA_TYPE_*)
  302. * @data: DMA type dependent data
  303. * @size: the requested pre-allocation size in bytes
  304. * @max: the max. allowed pre-allocation size
  305. *
  306. * Do pre-allocation to all substreams of the given pcm for the specified DMA
  307. * type and size, and set the managed_buffer_alloc flag to each substream.
  308. */
  309. void snd_pcm_set_managed_buffer_all(struct snd_pcm *pcm, int type,
  310. struct device *data,
  311. size_t size, size_t max)
  312. {
  313. preallocate_pages_for_all(pcm, type, data, size, max, true);
  314. }
  315. EXPORT_SYMBOL(snd_pcm_set_managed_buffer_all);
  316. #ifdef CONFIG_SND_DMA_SGBUF
  317. /*
  318. * snd_pcm_sgbuf_ops_page - get the page struct at the given offset
  319. * @substream: the pcm substream instance
  320. * @offset: the buffer offset
  321. *
  322. * Used as the page callback of PCM ops.
  323. *
  324. * Return: The page struct at the given buffer offset. %NULL on failure.
  325. */
  326. struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream, unsigned long offset)
  327. {
  328. struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
  329. unsigned int idx = offset >> PAGE_SHIFT;
  330. if (idx >= (unsigned int)sgbuf->pages)
  331. return NULL;
  332. return sgbuf->page_table[idx];
  333. }
  334. #endif /* CONFIG_SND_DMA_SGBUF */
  335. /**
  336. * snd_pcm_lib_malloc_pages - allocate the DMA buffer
  337. * @substream: the substream to allocate the DMA buffer to
  338. * @size: the requested buffer size in bytes
  339. *
  340. * Allocates the DMA buffer on the BUS type given earlier to
  341. * snd_pcm_lib_preallocate_xxx_pages().
  342. *
  343. * Return: 1 if the buffer is changed, 0 if not changed, or a negative
  344. * code on failure.
  345. */
  346. int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size)
  347. {
  348. struct snd_card *card;
  349. struct snd_pcm_runtime *runtime;
  350. struct snd_dma_buffer *dmab = NULL;
  351. if (PCM_RUNTIME_CHECK(substream))
  352. return -EINVAL;
  353. if (snd_BUG_ON(substream->dma_buffer.dev.type ==
  354. SNDRV_DMA_TYPE_UNKNOWN))
  355. return -EINVAL;
  356. runtime = substream->runtime;
  357. card = substream->pcm->card;
  358. if (runtime->dma_buffer_p) {
  359. /* perphaps, we might free the large DMA memory region
  360. to save some space here, but the actual solution
  361. costs us less time */
  362. if (runtime->dma_buffer_p->bytes >= size) {
  363. runtime->dma_bytes = size;
  364. return 0; /* ok, do not change */
  365. }
  366. snd_pcm_lib_free_pages(substream);
  367. }
  368. if (substream->dma_buffer.area != NULL &&
  369. substream->dma_buffer.bytes >= size) {
  370. dmab = &substream->dma_buffer; /* use the pre-allocated buffer */
  371. } else {
  372. dmab = kzalloc(sizeof(*dmab), GFP_KERNEL);
  373. if (! dmab)
  374. return -ENOMEM;
  375. dmab->dev = substream->dma_buffer.dev;
  376. if (do_alloc_pages(card,
  377. substream->dma_buffer.dev.type,
  378. substream->dma_buffer.dev.dev,
  379. size, dmab) < 0) {
  380. kfree(dmab);
  381. return -ENOMEM;
  382. }
  383. }
  384. snd_pcm_set_runtime_buffer(substream, dmab);
  385. runtime->dma_bytes = size;
  386. return 1; /* area was changed */
  387. }
  388. EXPORT_SYMBOL(snd_pcm_lib_malloc_pages);
  389. /**
  390. * snd_pcm_lib_free_pages - release the allocated DMA buffer.
  391. * @substream: the substream to release the DMA buffer
  392. *
  393. * Releases the DMA buffer allocated via snd_pcm_lib_malloc_pages().
  394. *
  395. * Return: Zero if successful, or a negative error code on failure.
  396. */
  397. int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream)
  398. {
  399. struct snd_card *card = substream->pcm->card;
  400. struct snd_pcm_runtime *runtime;
  401. if (PCM_RUNTIME_CHECK(substream))
  402. return -EINVAL;
  403. runtime = substream->runtime;
  404. if (runtime->dma_area == NULL)
  405. return 0;
  406. if (runtime->dma_buffer_p != &substream->dma_buffer) {
  407. /* it's a newly allocated buffer. release it now. */
  408. do_free_pages(card, runtime->dma_buffer_p);
  409. kfree(runtime->dma_buffer_p);
  410. }
  411. snd_pcm_set_runtime_buffer(substream, NULL);
  412. return 0;
  413. }
  414. EXPORT_SYMBOL(snd_pcm_lib_free_pages);
  415. int _snd_pcm_lib_alloc_vmalloc_buffer(struct snd_pcm_substream *substream,
  416. size_t size, gfp_t gfp_flags)
  417. {
  418. struct snd_pcm_runtime *runtime;
  419. if (PCM_RUNTIME_CHECK(substream))
  420. return -EINVAL;
  421. runtime = substream->runtime;
  422. if (runtime->dma_area) {
  423. if (runtime->dma_bytes >= size)
  424. return 0; /* already large enough */
  425. vfree(runtime->dma_area);
  426. }
  427. runtime->dma_area = __vmalloc(size, gfp_flags);
  428. if (!runtime->dma_area)
  429. return -ENOMEM;
  430. runtime->dma_bytes = size;
  431. return 1;
  432. }
  433. EXPORT_SYMBOL(_snd_pcm_lib_alloc_vmalloc_buffer);
  434. /**
  435. * snd_pcm_lib_free_vmalloc_buffer - free vmalloc buffer
  436. * @substream: the substream with a buffer allocated by
  437. * snd_pcm_lib_alloc_vmalloc_buffer()
  438. *
  439. * Return: Zero if successful, or a negative error code on failure.
  440. */
  441. int snd_pcm_lib_free_vmalloc_buffer(struct snd_pcm_substream *substream)
  442. {
  443. struct snd_pcm_runtime *runtime;
  444. if (PCM_RUNTIME_CHECK(substream))
  445. return -EINVAL;
  446. runtime = substream->runtime;
  447. vfree(runtime->dma_area);
  448. runtime->dma_area = NULL;
  449. return 0;
  450. }
  451. EXPORT_SYMBOL(snd_pcm_lib_free_vmalloc_buffer);
  452. /**
  453. * snd_pcm_lib_get_vmalloc_page - map vmalloc buffer offset to page struct
  454. * @substream: the substream with a buffer allocated by
  455. * snd_pcm_lib_alloc_vmalloc_buffer()
  456. * @offset: offset in the buffer
  457. *
  458. * This function is to be used as the page callback in the PCM ops.
  459. *
  460. * Return: The page struct, or %NULL on failure.
  461. */
  462. struct page *snd_pcm_lib_get_vmalloc_page(struct snd_pcm_substream *substream,
  463. unsigned long offset)
  464. {
  465. return vmalloc_to_page(substream->runtime->dma_area + offset);
  466. }
  467. EXPORT_SYMBOL(snd_pcm_lib_get_vmalloc_page);