pcm_memory.c 15 KB

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