144-ASoC-atmel-pdc-Use-managed-DMA-buffer-allocation.patch 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. From 0b20c174a17dcfa805ddac1301a5af7298877ec3 Mon Sep 17 00:00:00 2001
  2. From: Lars-Peter Clausen <[email protected]>
  3. Date: Wed, 6 Jan 2021 14:36:48 +0100
  4. Subject: [PATCH 144/247] ASoC: atmel-pdc: Use managed DMA buffer allocation
  5. Instead of manually managing its DMA buffers using
  6. dma_{alloc,free}_coherent() lets the sound core take care of this using
  7. managed buffers.
  8. On one hand this reduces the amount of boiler plate code, but the main
  9. motivation for the change is to use the shared code where possible. This
  10. makes it easier to argue about correctness and that the code does not
  11. contain subtle bugs like data leakage or similar.
  12. Signed-off-by: Lars-Peter Clausen <[email protected]>
  13. Reviewed-by: Codrin Ciubotariu <[email protected]>
  14. Link: https://lore.kernel.org/r/[email protected]
  15. Signed-off-by: Mark Brown <[email protected]>
  16. ---
  17. sound/soc/atmel/atmel-pcm-pdc.c | 78 ++-------------------------------
  18. 1 file changed, 4 insertions(+), 74 deletions(-)
  19. --- a/sound/soc/atmel/atmel-pcm-pdc.c
  20. +++ b/sound/soc/atmel/atmel-pcm-pdc.c
  21. @@ -34,86 +34,21 @@
  22. #include "atmel-pcm.h"
  23. -static int atmel_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
  24. - int stream)
  25. -{
  26. - struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  27. - struct snd_dma_buffer *buf = &substream->dma_buffer;
  28. - size_t size = ATMEL_SSC_DMABUF_SIZE;
  29. -
  30. - buf->dev.type = SNDRV_DMA_TYPE_DEV;
  31. - buf->dev.dev = pcm->card->dev;
  32. - buf->private_data = NULL;
  33. - buf->area = dma_alloc_coherent(pcm->card->dev, size,
  34. - &buf->addr, GFP_KERNEL);
  35. - pr_debug("atmel-pcm: alloc dma buffer: area=%p, addr=%p, size=%zu\n",
  36. - (void *)buf->area, (void *)(long)buf->addr, size);
  37. -
  38. - if (!buf->area)
  39. - return -ENOMEM;
  40. -
  41. - buf->bytes = size;
  42. - return 0;
  43. -}
  44. -
  45. -static int atmel_pcm_mmap(struct snd_soc_component *component,
  46. - struct snd_pcm_substream *substream,
  47. - struct vm_area_struct *vma)
  48. -{
  49. - return remap_pfn_range(vma, vma->vm_start,
  50. - substream->dma_buffer.addr >> PAGE_SHIFT,
  51. - vma->vm_end - vma->vm_start, vma->vm_page_prot);
  52. -}
  53. -
  54. static int atmel_pcm_new(struct snd_soc_component *component,
  55. struct snd_soc_pcm_runtime *rtd)
  56. {
  57. struct snd_card *card = rtd->card->snd_card;
  58. - struct snd_pcm *pcm = rtd->pcm;
  59. int ret;
  60. ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
  61. if (ret)
  62. return ret;
  63. - if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  64. - pr_debug("atmel-pcm: allocating PCM playback DMA buffer\n");
  65. - ret = atmel_pcm_preallocate_dma_buffer(pcm,
  66. - SNDRV_PCM_STREAM_PLAYBACK);
  67. - if (ret)
  68. - goto out;
  69. - }
  70. + snd_pcm_set_managed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV,
  71. + card->dev, ATMEL_SSC_DMABUF_SIZE,
  72. + ATMEL_SSC_DMABUF_SIZE);
  73. - if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  74. - pr_debug("atmel-pcm: allocating PCM capture DMA buffer\n");
  75. - ret = atmel_pcm_preallocate_dma_buffer(pcm,
  76. - SNDRV_PCM_STREAM_CAPTURE);
  77. - if (ret)
  78. - goto out;
  79. - }
  80. - out:
  81. - return ret;
  82. -}
  83. -
  84. -static void atmel_pcm_free(struct snd_soc_component *component,
  85. - struct snd_pcm *pcm)
  86. -{
  87. - struct snd_pcm_substream *substream;
  88. - struct snd_dma_buffer *buf;
  89. - int stream;
  90. -
  91. - for (stream = 0; stream < 2; stream++) {
  92. - substream = pcm->streams[stream].substream;
  93. - if (!substream)
  94. - continue;
  95. -
  96. - buf = &substream->dma_buffer;
  97. - if (!buf->area)
  98. - continue;
  99. - dma_free_coherent(pcm->card->dev, buf->bytes,
  100. - buf->area, buf->addr);
  101. - buf->area = NULL;
  102. - }
  103. + return 0;
  104. }
  105. /*--------------------------------------------------------------------------*\
  106. @@ -210,9 +145,6 @@ static int atmel_pcm_hw_params(struct sn
  107. /* this may get called several times by oss emulation
  108. * with different params */
  109. - snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  110. - runtime->dma_bytes = params_buffer_bytes(params);
  111. -
  112. prtd->params = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
  113. prtd->params->dma_intr_handler = atmel_pcm_dma_irq;
  114. @@ -384,9 +316,7 @@ static const struct snd_soc_component_dr
  115. .prepare = atmel_pcm_prepare,
  116. .trigger = atmel_pcm_trigger,
  117. .pointer = atmel_pcm_pointer,
  118. - .mmap = atmel_pcm_mmap,
  119. .pcm_construct = atmel_pcm_new,
  120. - .pcm_destruct = atmel_pcm_free,
  121. };
  122. int atmel_pcm_pdc_platform_register(struct device *dev)