compress.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
  2. //
  3. // Copyright 2021 NXP
  4. //
  5. // Author: Daniel Baluta <daniel.baluta@nxp.com>
  6. #include <sound/soc.h>
  7. #include <sound/sof.h>
  8. #include <sound/compress_driver.h>
  9. #include "sof-audio.h"
  10. #include "sof-priv.h"
  11. #include "sof-utils.h"
  12. #include "ops.h"
  13. static void sof_set_transferred_bytes(struct sof_compr_stream *sstream,
  14. u64 host_pos, u64 buffer_size)
  15. {
  16. u64 prev_pos;
  17. unsigned int copied;
  18. div64_u64_rem(sstream->copied_total, buffer_size, &prev_pos);
  19. if (host_pos < prev_pos)
  20. copied = (buffer_size - prev_pos) + host_pos;
  21. else
  22. copied = host_pos - prev_pos;
  23. sstream->copied_total += copied;
  24. }
  25. static void snd_sof_compr_fragment_elapsed_work(struct work_struct *work)
  26. {
  27. struct snd_sof_pcm_stream *sps =
  28. container_of(work, struct snd_sof_pcm_stream,
  29. period_elapsed_work);
  30. snd_compr_fragment_elapsed(sps->cstream);
  31. }
  32. void snd_sof_compr_init_elapsed_work(struct work_struct *work)
  33. {
  34. INIT_WORK(work, snd_sof_compr_fragment_elapsed_work);
  35. }
  36. /*
  37. * sof compr fragment elapse, this could be called in irq thread context
  38. */
  39. void snd_sof_compr_fragment_elapsed(struct snd_compr_stream *cstream)
  40. {
  41. struct snd_soc_pcm_runtime *rtd;
  42. struct snd_compr_runtime *crtd;
  43. struct snd_soc_component *component;
  44. struct sof_compr_stream *sstream;
  45. struct snd_sof_pcm *spcm;
  46. if (!cstream)
  47. return;
  48. rtd = cstream->private_data;
  49. crtd = cstream->runtime;
  50. sstream = crtd->private_data;
  51. component = snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
  52. spcm = snd_sof_find_spcm_dai(component, rtd);
  53. if (!spcm) {
  54. dev_err(component->dev,
  55. "fragment elapsed called for unknown stream!\n");
  56. return;
  57. }
  58. sof_set_transferred_bytes(sstream, spcm->stream[cstream->direction].posn.host_posn,
  59. crtd->buffer_size);
  60. /* use the same workqueue-based solution as for PCM, cf. snd_sof_pcm_elapsed */
  61. schedule_work(&spcm->stream[cstream->direction].period_elapsed_work);
  62. }
  63. static int create_page_table(struct snd_soc_component *component,
  64. struct snd_compr_stream *cstream,
  65. unsigned char *dma_area, size_t size)
  66. {
  67. struct snd_dma_buffer *dmab = cstream->runtime->dma_buffer_p;
  68. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  69. int dir = cstream->direction;
  70. struct snd_sof_pcm *spcm;
  71. spcm = snd_sof_find_spcm_dai(component, rtd);
  72. if (!spcm)
  73. return -EINVAL;
  74. return snd_sof_create_page_table(component->dev, dmab,
  75. spcm->stream[dir].page_table.area, size);
  76. }
  77. static int sof_compr_open(struct snd_soc_component *component,
  78. struct snd_compr_stream *cstream)
  79. {
  80. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  81. struct snd_compr_runtime *crtd = cstream->runtime;
  82. struct sof_compr_stream *sstream;
  83. struct snd_sof_pcm *spcm;
  84. int dir;
  85. sstream = kzalloc_obj(*sstream);
  86. if (!sstream)
  87. return -ENOMEM;
  88. spcm = snd_sof_find_spcm_dai(component, rtd);
  89. if (!spcm) {
  90. kfree(sstream);
  91. return -EINVAL;
  92. }
  93. dir = cstream->direction;
  94. if (spcm->stream[dir].cstream) {
  95. kfree(sstream);
  96. return -EBUSY;
  97. }
  98. spcm->stream[dir].cstream = cstream;
  99. spcm->stream[dir].posn.host_posn = 0;
  100. spcm->stream[dir].posn.dai_posn = 0;
  101. spcm->prepared[dir] = false;
  102. crtd->private_data = sstream;
  103. return 0;
  104. }
  105. static int sof_compr_free(struct snd_soc_component *component,
  106. struct snd_compr_stream *cstream)
  107. {
  108. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  109. struct sof_compr_stream *sstream = cstream->runtime->private_data;
  110. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  111. struct sof_ipc_stream stream;
  112. struct snd_sof_pcm *spcm;
  113. int ret = 0;
  114. spcm = snd_sof_find_spcm_dai(component, rtd);
  115. if (!spcm)
  116. return -EINVAL;
  117. stream.hdr.size = sizeof(stream);
  118. stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE;
  119. stream.comp_id = spcm->stream[cstream->direction].comp_id;
  120. if (spcm->prepared[cstream->direction]) {
  121. ret = sof_ipc_tx_message_no_reply(sdev->ipc, &stream, sizeof(stream));
  122. if (!ret)
  123. spcm->prepared[cstream->direction] = false;
  124. }
  125. cancel_work_sync(&spcm->stream[cstream->direction].period_elapsed_work);
  126. spcm->stream[cstream->direction].cstream = NULL;
  127. kfree(sstream);
  128. return ret;
  129. }
  130. static int sof_compr_set_params(struct snd_soc_component *component,
  131. struct snd_compr_stream *cstream, struct snd_compr_params *params)
  132. {
  133. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  134. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  135. struct snd_compr_runtime *crtd = cstream->runtime;
  136. struct sof_ipc_pcm_params_reply ipc_params_reply;
  137. struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
  138. struct sof_ipc_fw_version *v = &ready->version;
  139. struct sof_compr_stream *sstream;
  140. struct sof_ipc_pcm_params *pcm;
  141. struct snd_sof_pcm *spcm;
  142. size_t ext_data_size;
  143. int ret;
  144. if (v->abi_version < SOF_ABI_VER(3, 22, 0)) {
  145. dev_err(component->dev,
  146. "Compress params not supported with FW ABI version %d:%d:%d\n",
  147. SOF_ABI_VERSION_MAJOR(v->abi_version),
  148. SOF_ABI_VERSION_MINOR(v->abi_version),
  149. SOF_ABI_VERSION_PATCH(v->abi_version));
  150. return -EINVAL;
  151. }
  152. sstream = crtd->private_data;
  153. spcm = snd_sof_find_spcm_dai(component, rtd);
  154. if (!spcm)
  155. return -EINVAL;
  156. ext_data_size = sizeof(params->codec);
  157. if (sizeof(*pcm) + ext_data_size > sdev->ipc->max_payload_size)
  158. return -EINVAL;
  159. /*
  160. * Make sure that the DSP is booted up, which might not be the
  161. * case if the on-demand DSP boot is used
  162. */
  163. ret = snd_sof_boot_dsp_firmware(sdev);
  164. if (ret)
  165. return ret;
  166. pcm = kzalloc(sizeof(*pcm) + ext_data_size, GFP_KERNEL);
  167. if (!pcm)
  168. return -ENOMEM;
  169. cstream->dma_buffer.dev.type = SNDRV_DMA_TYPE_DEV_SG;
  170. cstream->dma_buffer.dev.dev = sdev->dev;
  171. ret = snd_compr_malloc_pages(cstream, crtd->buffer_size);
  172. if (ret < 0)
  173. goto out;
  174. ret = create_page_table(component, cstream, crtd->dma_area, crtd->dma_bytes);
  175. if (ret < 0)
  176. goto out;
  177. pcm->params.buffer.pages = PFN_UP(crtd->dma_bytes);
  178. pcm->hdr.size = sizeof(*pcm) + ext_data_size;
  179. pcm->hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
  180. pcm->comp_id = spcm->stream[cstream->direction].comp_id;
  181. pcm->params.hdr.size = sizeof(pcm->params) + ext_data_size;
  182. pcm->params.buffer.phy_addr = spcm->stream[cstream->direction].page_table.addr;
  183. pcm->params.buffer.size = crtd->dma_bytes;
  184. pcm->params.direction = cstream->direction;
  185. pcm->params.channels = params->codec.ch_out;
  186. pcm->params.rate = params->codec.sample_rate;
  187. pcm->params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
  188. pcm->params.frame_fmt = SOF_IPC_FRAME_S32_LE;
  189. pcm->params.sample_container_bytes =
  190. snd_pcm_format_physical_width(SNDRV_PCM_FORMAT_S32) >> 3;
  191. pcm->params.host_period_bytes = params->buffer.fragment_size;
  192. pcm->params.ext_data_length = ext_data_size;
  193. memcpy((u8 *)pcm->params.ext_data, &params->codec, ext_data_size);
  194. ret = sof_ipc_tx_message(sdev->ipc, pcm, sizeof(*pcm) + ext_data_size,
  195. &ipc_params_reply, sizeof(ipc_params_reply));
  196. if (ret < 0) {
  197. dev_err(component->dev, "error ipc failed\n");
  198. goto out;
  199. }
  200. ret = snd_sof_set_stream_data_offset(sdev, &spcm->stream[cstream->direction],
  201. ipc_params_reply.posn_offset);
  202. if (ret < 0) {
  203. dev_err(component->dev, "Invalid stream data offset for Compr %d\n",
  204. spcm->pcm.pcm_id);
  205. goto out;
  206. }
  207. sstream->sampling_rate = params->codec.sample_rate;
  208. sstream->channels = params->codec.ch_out;
  209. sstream->sample_container_bytes = pcm->params.sample_container_bytes;
  210. spcm->prepared[cstream->direction] = true;
  211. out:
  212. kfree(pcm);
  213. return ret;
  214. }
  215. static int sof_compr_get_params(struct snd_soc_component *component,
  216. struct snd_compr_stream *cstream, struct snd_codec *params)
  217. {
  218. /* TODO: we don't query the supported codecs for now, if the
  219. * application asks for an unsupported codec the set_params() will fail.
  220. */
  221. return 0;
  222. }
  223. static int sof_compr_trigger(struct snd_soc_component *component,
  224. struct snd_compr_stream *cstream, int cmd)
  225. {
  226. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  227. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  228. struct sof_ipc_stream stream;
  229. struct snd_sof_pcm *spcm;
  230. spcm = snd_sof_find_spcm_dai(component, rtd);
  231. if (!spcm)
  232. return -EINVAL;
  233. stream.hdr.size = sizeof(stream);
  234. stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG;
  235. stream.comp_id = spcm->stream[cstream->direction].comp_id;
  236. switch (cmd) {
  237. case SNDRV_PCM_TRIGGER_START:
  238. stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START;
  239. break;
  240. case SNDRV_PCM_TRIGGER_STOP:
  241. stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP;
  242. break;
  243. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  244. stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE;
  245. break;
  246. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  247. stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE;
  248. break;
  249. default:
  250. dev_err(component->dev, "error: unhandled trigger cmd %d\n", cmd);
  251. break;
  252. }
  253. return sof_ipc_tx_message_no_reply(sdev->ipc, &stream, sizeof(stream));
  254. }
  255. static int sof_compr_copy_playback(struct snd_compr_runtime *rtd,
  256. char __user *buf, size_t count)
  257. {
  258. void *ptr;
  259. unsigned int offset, n;
  260. int ret;
  261. div_u64_rem(rtd->total_bytes_available, rtd->buffer_size, &offset);
  262. ptr = rtd->dma_area + offset;
  263. n = rtd->buffer_size - offset;
  264. if (count < n) {
  265. ret = copy_from_user(ptr, buf, count);
  266. } else {
  267. ret = copy_from_user(ptr, buf, n);
  268. ret += copy_from_user(rtd->dma_area, buf + n, count - n);
  269. }
  270. return count - ret;
  271. }
  272. static int sof_compr_copy_capture(struct snd_compr_runtime *rtd,
  273. char __user *buf, size_t count)
  274. {
  275. void *ptr;
  276. unsigned int offset, n;
  277. int ret;
  278. div_u64_rem(rtd->total_bytes_transferred, rtd->buffer_size, &offset);
  279. ptr = rtd->dma_area + offset;
  280. n = rtd->buffer_size - offset;
  281. if (count < n) {
  282. ret = copy_to_user(buf, ptr, count);
  283. } else {
  284. ret = copy_to_user(buf, ptr, n);
  285. ret += copy_to_user(buf + n, rtd->dma_area, count - n);
  286. }
  287. return count - ret;
  288. }
  289. static int sof_compr_copy(struct snd_soc_component *component,
  290. struct snd_compr_stream *cstream,
  291. char __user *buf, size_t count)
  292. {
  293. struct snd_compr_runtime *rtd = cstream->runtime;
  294. if (count > rtd->buffer_size)
  295. count = rtd->buffer_size;
  296. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  297. return sof_compr_copy_playback(rtd, buf, count);
  298. else
  299. return sof_compr_copy_capture(rtd, buf, count);
  300. }
  301. static int sof_compr_pointer(struct snd_soc_component *component,
  302. struct snd_compr_stream *cstream,
  303. struct snd_compr_tstamp64 *tstamp)
  304. {
  305. struct snd_sof_pcm *spcm;
  306. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  307. struct sof_compr_stream *sstream = cstream->runtime->private_data;
  308. spcm = snd_sof_find_spcm_dai(component, rtd);
  309. if (!spcm)
  310. return -EINVAL;
  311. tstamp->sampling_rate = sstream->sampling_rate;
  312. tstamp->copied_total = sstream->copied_total;
  313. tstamp->pcm_io_frames = div_u64(spcm->stream[cstream->direction].posn.dai_posn,
  314. sstream->channels * sstream->sample_container_bytes);
  315. return 0;
  316. }
  317. struct snd_compress_ops sof_compressed_ops = {
  318. .open = sof_compr_open,
  319. .free = sof_compr_free,
  320. .set_params = sof_compr_set_params,
  321. .get_params = sof_compr_get_params,
  322. .trigger = sof_compr_trigger,
  323. .pointer = sof_compr_pointer,
  324. .copy = sof_compr_copy,
  325. };
  326. EXPORT_SYMBOL(sof_compressed_ops);