probes.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // Copyright(c) 2021-2022 Intel Corporation
  4. //
  5. // Authors: Cezary Rojewski <cezary.rojewski@intel.com>
  6. // Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
  7. //
  8. #include <sound/compress_driver.h>
  9. #include <sound/hdaudio_ext.h>
  10. #include <sound/hdaudio.h>
  11. #include <sound/soc.h>
  12. #include "avs.h"
  13. #include "messages.h"
  14. static int avs_dsp_init_probe(struct avs_dev *adev, union avs_connector_node_id node_id,
  15. size_t buffer_size)
  16. {
  17. struct avs_probe_cfg cfg = {{0}};
  18. struct avs_module_entry mentry;
  19. u8 dummy;
  20. int ret;
  21. ret = avs_get_module_entry(adev, &AVS_PROBE_MOD_UUID, &mentry);
  22. if (ret)
  23. return ret;
  24. /*
  25. * Probe module uses no cycles, audio data format and input and output
  26. * frame sizes are unused. It is also not owned by any pipeline.
  27. */
  28. cfg.base.ibs = 1;
  29. /* BSS module descriptor is always segment of index=2. */
  30. cfg.base.is_pages = mentry.segments[2].flags.length;
  31. cfg.gtw_cfg.node_id = node_id;
  32. cfg.gtw_cfg.dma_buffer_size = buffer_size;
  33. return avs_dsp_init_module(adev, mentry.module_id, INVALID_PIPELINE_ID, 0, 0, &cfg,
  34. sizeof(cfg), &dummy);
  35. }
  36. static void avs_dsp_delete_probe(struct avs_dev *adev)
  37. {
  38. struct avs_module_entry mentry;
  39. int ret;
  40. ret = avs_get_module_entry(adev, &AVS_PROBE_MOD_UUID, &mentry);
  41. if (!ret)
  42. /* There is only ever one probe module instance. */
  43. avs_dsp_delete_module(adev, mentry.module_id, 0, INVALID_PIPELINE_ID, 0);
  44. }
  45. static inline struct hdac_ext_stream *avs_compr_get_host_stream(struct snd_compr_stream *cstream)
  46. {
  47. return cstream->runtime->private_data;
  48. }
  49. static int avs_probe_compr_open(struct snd_compr_stream *cstream, struct snd_soc_dai *dai)
  50. {
  51. struct avs_dev *adev = to_avs_dev(dai->dev);
  52. struct hdac_bus *bus = &adev->base.core;
  53. struct hdac_ext_stream *host_stream;
  54. if (adev->extractor) {
  55. dev_err(dai->dev, "Cannot open more than one extractor stream\n");
  56. return -EEXIST;
  57. }
  58. host_stream = snd_hdac_ext_cstream_assign(bus, cstream);
  59. if (!host_stream) {
  60. dev_err(dai->dev, "Failed to assign HDAudio stream for extraction\n");
  61. return -EBUSY;
  62. }
  63. adev->extractor = host_stream;
  64. hdac_stream(host_stream)->curr_pos = 0;
  65. cstream->runtime->private_data = host_stream;
  66. return 0;
  67. }
  68. static int avs_probe_compr_free(struct snd_compr_stream *cstream, struct snd_soc_dai *dai)
  69. {
  70. struct hdac_ext_stream *host_stream = avs_compr_get_host_stream(cstream);
  71. struct avs_dev *adev = to_avs_dev(dai->dev);
  72. struct avs_probe_point_desc *desc;
  73. /* Extractor node identifier. */
  74. unsigned int vindex = INVALID_NODE_ID.vindex;
  75. size_t num_desc;
  76. int i, ret;
  77. /* Disconnect all probe points. */
  78. ret = avs_ipc_probe_get_points(adev, &desc, &num_desc);
  79. if (ret) {
  80. dev_err(dai->dev, "get probe points failed: %d\n", ret);
  81. ret = AVS_IPC_RET(ret);
  82. goto exit;
  83. }
  84. for (i = 0; i < num_desc; i++)
  85. if (desc[i].node_id.vindex == vindex)
  86. avs_ipc_probe_disconnect_points(adev, &desc[i].id, 1);
  87. kfree(desc);
  88. exit:
  89. if (adev->num_probe_streams) {
  90. adev->num_probe_streams--;
  91. if (!adev->num_probe_streams) {
  92. avs_dsp_delete_probe(adev);
  93. avs_dsp_enable_d0ix(adev);
  94. }
  95. }
  96. snd_hdac_stream_cleanup(hdac_stream(host_stream));
  97. hdac_stream(host_stream)->prepared = 0;
  98. snd_hdac_ext_stream_release(host_stream, HDAC_EXT_STREAM_TYPE_HOST);
  99. snd_compr_free_pages(cstream);
  100. adev->extractor = NULL;
  101. return ret;
  102. }
  103. static int avs_probe_compr_set_params(struct snd_compr_stream *cstream,
  104. struct snd_compr_params *params, struct snd_soc_dai *dai)
  105. {
  106. struct hdac_ext_stream *host_stream = avs_compr_get_host_stream(cstream);
  107. struct snd_compr_runtime *rtd = cstream->runtime;
  108. struct avs_dev *adev = to_avs_dev(dai->dev);
  109. /* compr params do not store bit depth, default to S32_LE. */
  110. snd_pcm_format_t format = SNDRV_PCM_FORMAT_S32_LE;
  111. unsigned int format_val;
  112. int bps, ret;
  113. hdac_stream(host_stream)->bufsize = 0;
  114. hdac_stream(host_stream)->period_bytes = 0;
  115. hdac_stream(host_stream)->format_val = 0;
  116. cstream->dma_buffer.dev.type = SNDRV_DMA_TYPE_DEV_SG;
  117. cstream->dma_buffer.dev.dev = adev->dev;
  118. ret = snd_compr_malloc_pages(cstream, rtd->buffer_size);
  119. if (ret < 0)
  120. return ret;
  121. bps = snd_pcm_format_physical_width(format);
  122. if (bps < 0)
  123. return bps;
  124. format_val = snd_hdac_stream_format(params->codec.ch_out, bps, params->codec.sample_rate);
  125. ret = snd_hdac_stream_set_params(hdac_stream(host_stream), format_val);
  126. if (ret < 0)
  127. return ret;
  128. ret = snd_hdac_stream_setup(hdac_stream(host_stream), false);
  129. if (ret < 0)
  130. return ret;
  131. hdac_stream(host_stream)->prepared = 1;
  132. if (!adev->num_probe_streams) {
  133. union avs_connector_node_id node_id;
  134. /* D0ix not allowed during probing. */
  135. ret = avs_dsp_disable_d0ix(adev);
  136. if (ret)
  137. return ret;
  138. node_id.vindex = hdac_stream(host_stream)->stream_tag - 1;
  139. node_id.dma_type = AVS_DMA_HDA_HOST_INPUT;
  140. ret = avs_dsp_init_probe(adev, node_id, rtd->dma_bytes);
  141. if (ret < 0) {
  142. dev_err(dai->dev, "probe init failed: %d\n", ret);
  143. avs_dsp_enable_d0ix(adev);
  144. return ret;
  145. }
  146. }
  147. adev->num_probe_streams++;
  148. return 0;
  149. }
  150. static int avs_probe_compr_trigger(struct snd_compr_stream *cstream, int cmd,
  151. struct snd_soc_dai *dai)
  152. {
  153. struct hdac_ext_stream *host_stream = avs_compr_get_host_stream(cstream);
  154. struct avs_dev *adev = to_avs_dev(dai->dev);
  155. struct hdac_bus *bus = &adev->base.core;
  156. unsigned long cookie;
  157. if (!hdac_stream(host_stream)->prepared)
  158. return -EPIPE;
  159. switch (cmd) {
  160. case SNDRV_PCM_TRIGGER_START:
  161. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  162. case SNDRV_PCM_TRIGGER_RESUME:
  163. spin_lock_irqsave(&bus->reg_lock, cookie);
  164. snd_hdac_stream_start(hdac_stream(host_stream));
  165. spin_unlock_irqrestore(&bus->reg_lock, cookie);
  166. break;
  167. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  168. case SNDRV_PCM_TRIGGER_SUSPEND:
  169. case SNDRV_PCM_TRIGGER_STOP:
  170. spin_lock_irqsave(&bus->reg_lock, cookie);
  171. snd_hdac_stream_stop(hdac_stream(host_stream));
  172. spin_unlock_irqrestore(&bus->reg_lock, cookie);
  173. break;
  174. default:
  175. return -EINVAL;
  176. }
  177. return 0;
  178. }
  179. static int avs_probe_compr_pointer(struct snd_compr_stream *cstream,
  180. struct snd_compr_tstamp *tstamp, struct snd_soc_dai *dai)
  181. {
  182. struct hdac_ext_stream *host_stream = avs_compr_get_host_stream(cstream);
  183. struct snd_soc_pcm_stream *pstream;
  184. pstream = &dai->driver->capture;
  185. tstamp->copied_total = hdac_stream(host_stream)->curr_pos;
  186. tstamp->sampling_rate = snd_pcm_rate_bit_to_rate(pstream->rates);
  187. return 0;
  188. }
  189. static int avs_probe_compr_copy(struct snd_soc_component *comp, struct snd_compr_stream *cstream,
  190. char __user *buf, size_t count)
  191. {
  192. struct snd_compr_runtime *rtd = cstream->runtime;
  193. unsigned int offset, n;
  194. void *ptr;
  195. int ret;
  196. if (count > rtd->buffer_size)
  197. count = rtd->buffer_size;
  198. div_u64_rem(rtd->total_bytes_transferred, rtd->buffer_size, &offset);
  199. ptr = rtd->dma_area + offset;
  200. n = rtd->buffer_size - offset;
  201. if (count < n) {
  202. ret = copy_to_user(buf, ptr, count);
  203. } else {
  204. ret = copy_to_user(buf, ptr, n);
  205. ret += copy_to_user(buf + n, rtd->dma_area, count - n);
  206. }
  207. if (ret)
  208. return count - ret;
  209. return count;
  210. }
  211. static const struct snd_soc_cdai_ops avs_probe_cdai_ops = {
  212. .startup = avs_probe_compr_open,
  213. .shutdown = avs_probe_compr_free,
  214. .set_params = avs_probe_compr_set_params,
  215. .trigger = avs_probe_compr_trigger,
  216. .pointer = avs_probe_compr_pointer,
  217. };
  218. static const struct snd_soc_dai_ops avs_probe_dai_ops = {
  219. .compress_new = snd_soc_new_compress,
  220. };
  221. static const struct snd_compress_ops avs_probe_compress_ops = {
  222. .copy = avs_probe_compr_copy,
  223. };
  224. static struct snd_soc_dai_driver probe_cpu_dais[] = {
  225. {
  226. .name = "Probe Extraction CPU DAI",
  227. .cops = &avs_probe_cdai_ops,
  228. .ops = &avs_probe_dai_ops,
  229. .capture = {
  230. .stream_name = "Probe Extraction",
  231. .channels_min = 1,
  232. .channels_max = 8,
  233. .rates = SNDRV_PCM_RATE_48000,
  234. .rate_min = 48000,
  235. .rate_max = 48000,
  236. },
  237. },
  238. };
  239. static const struct snd_soc_component_driver avs_probe_component_driver = {
  240. .name = "avs-probe-compr",
  241. .compress_ops = &avs_probe_compress_ops,
  242. .module_get_upon_open = 1, /* increment refcount when a stream is opened */
  243. };
  244. int avs_probe_platform_register(struct avs_dev *adev, const char *name)
  245. {
  246. return avs_soc_component_register(adev->dev, name, &avs_probe_component_driver,
  247. probe_cpu_dais, ARRAY_SIZE(probe_cpu_dais));
  248. }