ipc4-pcm.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
  2. //
  3. // This file is provided under a dual BSD/GPLv2 license. When using or
  4. // redistributing this file, you may do so under either license.
  5. //
  6. // Copyright(c) 2022 Intel Corporation
  7. //
  8. #include <sound/pcm_params.h>
  9. #include <sound/sof/ipc4/header.h>
  10. #include "sof-audio.h"
  11. #include "sof-priv.h"
  12. #include "ops.h"
  13. #include "ipc4-priv.h"
  14. #include "ipc4-topology.h"
  15. #include "ipc4-fw-reg.h"
  16. /**
  17. * struct sof_ipc4_timestamp_info - IPC4 timestamp info
  18. * @host_copier: the host copier of the pcm stream
  19. * @dai_copier: the dai copier of the pcm stream
  20. * @stream_start_offset: reported by fw in memory window (converted to
  21. * frames at host_copier sampling rate)
  22. * @stream_end_offset: reported by fw in memory window (converted to
  23. * frames at host_copier sampling rate)
  24. * @llp_offset: llp offset in memory window
  25. * @delay: Calculated and stored in pointer callback. The stored value is
  26. * returned in the delay callback. Expressed in frames at host copier
  27. * sampling rate.
  28. */
  29. struct sof_ipc4_timestamp_info {
  30. struct sof_ipc4_copier *host_copier;
  31. struct sof_ipc4_copier *dai_copier;
  32. u64 stream_start_offset;
  33. u64 stream_end_offset;
  34. u32 llp_offset;
  35. snd_pcm_sframes_t delay;
  36. };
  37. /**
  38. * struct sof_ipc4_pcm_stream_priv - IPC4 specific private data
  39. * @time_info: pointer to time info struct if it is supported, otherwise NULL
  40. * @chain_dma_allocated: indicates the ChainDMA allocation state
  41. */
  42. struct sof_ipc4_pcm_stream_priv {
  43. struct sof_ipc4_timestamp_info *time_info;
  44. bool chain_dma_allocated;
  45. };
  46. /*
  47. * Modulus to use to compare host and link position counters. The sampling
  48. * rates may be different, so the raw hardware counters will wrap
  49. * around at different times. To calculate differences, use
  50. * DELAY_BOUNDARY as a common modulus. This value must be smaller than
  51. * the wrap-around point of any hardware counter, and larger than any
  52. * valid delay measurement.
  53. */
  54. #define DELAY_BOUNDARY U32_MAX
  55. static inline struct sof_ipc4_timestamp_info *
  56. sof_ipc4_sps_to_time_info(struct snd_sof_pcm_stream *sps)
  57. {
  58. struct sof_ipc4_pcm_stream_priv *stream_priv = sps->private;
  59. return stream_priv->time_info;
  60. }
  61. static int sof_ipc4_set_multi_pipeline_state(struct snd_sof_dev *sdev, u32 state,
  62. struct ipc4_pipeline_set_state_data *trigger_list)
  63. {
  64. struct sof_ipc4_msg msg = {{ 0 }};
  65. u32 primary, ipc_size;
  66. /* trigger a single pipeline */
  67. if (trigger_list->count == 1)
  68. return sof_ipc4_set_pipeline_state(sdev, trigger_list->pipeline_instance_ids[0],
  69. state);
  70. primary = state;
  71. primary |= SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_GLB_SET_PIPELINE_STATE);
  72. primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
  73. primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_FW_GEN_MSG);
  74. msg.primary = primary;
  75. /* trigger multiple pipelines with a single IPC */
  76. msg.extension = SOF_IPC4_GLB_PIPE_STATE_EXT_MULTI;
  77. /* ipc_size includes the count and the pipeline IDs for the number of pipelines */
  78. ipc_size = sizeof(u32) * (trigger_list->count + 1);
  79. msg.data_size = ipc_size;
  80. msg.data_ptr = trigger_list;
  81. return sof_ipc_tx_message_no_reply(sdev->ipc, &msg, ipc_size);
  82. }
  83. int sof_ipc4_set_pipeline_state(struct snd_sof_dev *sdev, u32 instance_id, u32 state)
  84. {
  85. struct sof_ipc4_msg msg = {{ 0 }};
  86. u32 primary;
  87. dev_dbg(sdev->dev, "ipc4 set pipeline instance %d state %d", instance_id, state);
  88. primary = state;
  89. primary |= SOF_IPC4_GLB_PIPE_STATE_ID(instance_id);
  90. primary |= SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_GLB_SET_PIPELINE_STATE);
  91. primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
  92. primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_FW_GEN_MSG);
  93. msg.primary = primary;
  94. return sof_ipc_tx_message_no_reply(sdev->ipc, &msg, 0);
  95. }
  96. EXPORT_SYMBOL(sof_ipc4_set_pipeline_state);
  97. static void sof_ipc4_add_pipeline_by_priority(struct ipc4_pipeline_set_state_data *trigger_list,
  98. struct snd_sof_widget *pipe_widget,
  99. s8 *pipe_priority, bool ascend)
  100. {
  101. struct sof_ipc4_pipeline *pipeline = pipe_widget->private;
  102. int i, j;
  103. for (i = 0; i < trigger_list->count; i++) {
  104. /* add pipeline from low priority to high */
  105. if (ascend && pipeline->priority < pipe_priority[i])
  106. break;
  107. /* add pipeline from high priority to low */
  108. else if (!ascend && pipeline->priority > pipe_priority[i])
  109. break;
  110. }
  111. for (j = trigger_list->count - 1; j >= i; j--) {
  112. trigger_list->pipeline_instance_ids[j + 1] = trigger_list->pipeline_instance_ids[j];
  113. pipe_priority[j + 1] = pipe_priority[j];
  114. }
  115. trigger_list->pipeline_instance_ids[i] = pipe_widget->instance_id;
  116. trigger_list->count++;
  117. pipe_priority[i] = pipeline->priority;
  118. }
  119. static void
  120. sof_ipc4_add_pipeline_to_trigger_list(struct snd_sof_dev *sdev, int state,
  121. struct snd_sof_pipeline *spipe,
  122. struct ipc4_pipeline_set_state_data *trigger_list,
  123. s8 *pipe_priority)
  124. {
  125. struct snd_sof_widget *pipe_widget = spipe->pipe_widget;
  126. struct sof_ipc4_pipeline *pipeline = pipe_widget->private;
  127. if (pipeline->skip_during_fe_trigger && state != SOF_IPC4_PIPE_RESET)
  128. return;
  129. switch (state) {
  130. case SOF_IPC4_PIPE_RUNNING:
  131. /*
  132. * Trigger pipeline if all PCMs containing it are paused or if it is RUNNING
  133. * for the first time
  134. */
  135. if (spipe->started_count == spipe->paused_count)
  136. sof_ipc4_add_pipeline_by_priority(trigger_list, pipe_widget, pipe_priority,
  137. false);
  138. break;
  139. case SOF_IPC4_PIPE_RESET:
  140. /* RESET if the pipeline is neither running nor paused */
  141. if (!spipe->started_count && !spipe->paused_count)
  142. sof_ipc4_add_pipeline_by_priority(trigger_list, pipe_widget, pipe_priority,
  143. true);
  144. break;
  145. case SOF_IPC4_PIPE_PAUSED:
  146. /* Pause the pipeline only when its started_count is 1 more than paused_count */
  147. if (spipe->paused_count == (spipe->started_count - 1))
  148. sof_ipc4_add_pipeline_by_priority(trigger_list, pipe_widget, pipe_priority,
  149. true);
  150. break;
  151. default:
  152. break;
  153. }
  154. }
  155. static void
  156. sof_ipc4_update_pipeline_state(struct snd_sof_dev *sdev, int state, int cmd,
  157. struct snd_sof_pipeline *spipe,
  158. struct ipc4_pipeline_set_state_data *trigger_list)
  159. {
  160. struct snd_sof_widget *pipe_widget = spipe->pipe_widget;
  161. struct sof_ipc4_pipeline *pipeline = pipe_widget->private;
  162. int i;
  163. if (pipeline->skip_during_fe_trigger && state != SOF_IPC4_PIPE_RESET)
  164. return;
  165. /* set state for pipeline if it was just triggered */
  166. for (i = 0; i < trigger_list->count; i++) {
  167. if (trigger_list->pipeline_instance_ids[i] == pipe_widget->instance_id) {
  168. pipeline->state = state;
  169. break;
  170. }
  171. }
  172. switch (state) {
  173. case SOF_IPC4_PIPE_PAUSED:
  174. switch (cmd) {
  175. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  176. /*
  177. * increment paused_count if the PAUSED is the final state during
  178. * the PAUSE trigger
  179. */
  180. spipe->paused_count++;
  181. break;
  182. case SNDRV_PCM_TRIGGER_STOP:
  183. case SNDRV_PCM_TRIGGER_SUSPEND:
  184. /*
  185. * decrement started_count if PAUSED is the final state during the
  186. * STOP trigger
  187. */
  188. spipe->started_count--;
  189. break;
  190. default:
  191. break;
  192. }
  193. break;
  194. case SOF_IPC4_PIPE_RUNNING:
  195. switch (cmd) {
  196. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  197. /* decrement paused_count for RELEASE */
  198. spipe->paused_count--;
  199. break;
  200. case SNDRV_PCM_TRIGGER_START:
  201. case SNDRV_PCM_TRIGGER_RESUME:
  202. /* increment started_count for START/RESUME */
  203. spipe->started_count++;
  204. break;
  205. default:
  206. break;
  207. }
  208. break;
  209. default:
  210. break;
  211. }
  212. }
  213. /*
  214. * The picture below represents the pipeline state machine wrt PCM actions corresponding to the
  215. * triggers and ioctls
  216. * +---------------+
  217. * | |
  218. * | INIT |
  219. * | |
  220. * +-------+-------+
  221. * |
  222. * |
  223. * | START
  224. * |
  225. * |
  226. * +----------------+ +------v-------+ +-------------+
  227. * | | START | | HW_FREE | |
  228. * | RUNNING <-------------+ PAUSED +--------------> + RESET |
  229. * | | PAUSE | | | |
  230. * +------+---------+ RELEASE +---------+----+ +-------------+
  231. * | ^
  232. * | |
  233. * | |
  234. * | |
  235. * | PAUSE |
  236. * +---------------------------------+
  237. * STOP/SUSPEND
  238. *
  239. * Note that during system suspend, the suspend trigger is followed by a hw_free in
  240. * sof_pcm_trigger(). So, the final state during suspend would be RESET.
  241. * Also, since the SOF driver doesn't support full resume, streams would be restarted with the
  242. * prepare ioctl before the START trigger.
  243. */
  244. /*
  245. * Chained DMA is a special case where there is no processing on
  246. * DSP. The samples are just moved over by host side DMA to a single
  247. * buffer on DSP and directly from there to link DMA. However, the
  248. * model on SOF driver has two notional pipelines, one at host DAI,
  249. * and another at link DAI. They both shall have the use_chain_dma
  250. * attribute.
  251. */
  252. static int sof_ipc4_chain_dma_trigger(struct snd_sof_dev *sdev,
  253. struct snd_sof_pcm *spcm, int direction,
  254. struct snd_sof_pcm_stream_pipeline_list *pipeline_list,
  255. int state, int cmd)
  256. {
  257. struct sof_ipc4_fw_data *ipc4_data = sdev->private;
  258. struct sof_ipc4_pcm_stream_priv *stream_priv;
  259. bool allocate, enable, set_fifo_size;
  260. struct sof_ipc4_msg msg = {{ 0 }};
  261. int ret, i;
  262. stream_priv = spcm->stream[direction].private;
  263. switch (state) {
  264. case SOF_IPC4_PIPE_RUNNING: /* Allocate and start chained dma */
  265. allocate = true;
  266. enable = true;
  267. /*
  268. * SOF assumes creation of a new stream from the presence of fifo_size
  269. * in the message, so we must leave it out in pause release case.
  270. */
  271. if (cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE)
  272. set_fifo_size = false;
  273. else
  274. set_fifo_size = true;
  275. break;
  276. case SOF_IPC4_PIPE_PAUSED: /* Disable chained DMA. */
  277. allocate = true;
  278. enable = false;
  279. set_fifo_size = false;
  280. break;
  281. case SOF_IPC4_PIPE_RESET: /* Disable and free chained DMA. */
  282. /* ChainDMA can only be reset if it has been allocated */
  283. if (!stream_priv->chain_dma_allocated)
  284. return 0;
  285. allocate = false;
  286. enable = false;
  287. set_fifo_size = false;
  288. break;
  289. default:
  290. dev_err(sdev->dev, "Unexpected state %d", state);
  291. return -EINVAL;
  292. }
  293. msg.primary = SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_GLB_CHAIN_DMA);
  294. msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
  295. msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_FW_GEN_MSG);
  296. /*
  297. * To set-up the DMA chain, the host DMA ID and SCS setting
  298. * are retrieved from the host pipeline configuration. Likewise
  299. * the link DMA ID and fifo_size are retrieved from the link
  300. * pipeline configuration.
  301. */
  302. for (i = 0; i < pipeline_list->count; i++) {
  303. struct snd_sof_pipeline *spipe = pipeline_list->pipelines[i];
  304. struct snd_sof_widget *pipe_widget = spipe->pipe_widget;
  305. struct sof_ipc4_pipeline *pipeline = pipe_widget->private;
  306. if (!pipeline->use_chain_dma) {
  307. dev_err(sdev->dev,
  308. "All pipelines in chained DMA stream should have use_chain_dma attribute set.");
  309. return -EINVAL;
  310. }
  311. msg.primary |= pipeline->msg.primary;
  312. /* Add fifo_size (actually DMA buffer size) field to the message */
  313. if (set_fifo_size)
  314. msg.extension |= pipeline->msg.extension;
  315. }
  316. if (direction == SNDRV_PCM_STREAM_CAPTURE) {
  317. /*
  318. * For ChainDMA the DMA ids are unique with the following mapping:
  319. * playback: 0 - (num_playback_streams - 1)
  320. * capture: num_playback_streams - (num_playback_streams +
  321. * num_capture_streams - 1)
  322. *
  323. * Add the num_playback_streams offset to the DMA ids stored in
  324. * msg.primary in case capture
  325. */
  326. msg.primary += SOF_IPC4_GLB_CHAIN_DMA_HOST_ID(ipc4_data->num_playback_streams);
  327. msg.primary += SOF_IPC4_GLB_CHAIN_DMA_LINK_ID(ipc4_data->num_playback_streams);
  328. }
  329. if (allocate)
  330. msg.primary |= SOF_IPC4_GLB_CHAIN_DMA_ALLOCATE_MASK;
  331. if (enable)
  332. msg.primary |= SOF_IPC4_GLB_CHAIN_DMA_ENABLE_MASK;
  333. ret = sof_ipc_tx_message_no_reply(sdev->ipc, &msg, 0);
  334. /* Update the ChainDMA allocation state */
  335. if (!ret)
  336. stream_priv->chain_dma_allocated = allocate;
  337. return ret;
  338. }
  339. static int sof_ipc4_trigger_pipelines(struct snd_soc_component *component,
  340. struct snd_pcm_substream *substream, int state, int cmd)
  341. {
  342. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  343. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  344. struct snd_sof_pcm_stream_pipeline_list *pipeline_list;
  345. struct sof_ipc4_fw_data *ipc4_data = sdev->private;
  346. struct ipc4_pipeline_set_state_data *trigger_list;
  347. struct snd_sof_widget *pipe_widget;
  348. struct sof_ipc4_pipeline *pipeline;
  349. struct snd_sof_pipeline *spipe;
  350. struct snd_sof_pcm *spcm;
  351. u8 *pipe_priority;
  352. int ret;
  353. int i;
  354. dev_dbg(sdev->dev, "trigger cmd: %d state: %d\n", cmd, state);
  355. spcm = snd_sof_find_spcm_dai(component, rtd);
  356. if (!spcm)
  357. return -EINVAL;
  358. pipeline_list = &spcm->stream[substream->stream].pipeline_list;
  359. /* nothing to trigger if the list is empty */
  360. if (!pipeline_list->pipelines || !pipeline_list->count)
  361. return 0;
  362. spipe = pipeline_list->pipelines[0];
  363. pipe_widget = spipe->pipe_widget;
  364. pipeline = pipe_widget->private;
  365. /*
  366. * If use_chain_dma attribute is set we proceed to chained DMA
  367. * trigger function that handles the rest for the substream.
  368. */
  369. if (pipeline->use_chain_dma) {
  370. struct sof_ipc4_timestamp_info *time_info;
  371. time_info = sof_ipc4_sps_to_time_info(&spcm->stream[substream->stream]);
  372. ret = sof_ipc4_chain_dma_trigger(sdev, spcm, substream->stream,
  373. pipeline_list, state, cmd);
  374. if (ret || !time_info)
  375. return ret;
  376. if (state == SOF_IPC4_PIPE_PAUSED) {
  377. /*
  378. * Record the DAI position for delay reporting
  379. * To handle multiple pause/resume/xrun we need to add
  380. * the positions to simulate how the firmware behaves
  381. */
  382. u64 pos = snd_sof_pcm_get_dai_frame_counter(sdev, component,
  383. substream);
  384. time_info->stream_end_offset += pos;
  385. } else if (state == SOF_IPC4_PIPE_RESET) {
  386. /* Reset the end offset as the stream is stopped */
  387. time_info->stream_end_offset = 0;
  388. }
  389. return 0;
  390. }
  391. /* allocate memory for the pipeline data */
  392. trigger_list = kzalloc(struct_size(trigger_list, pipeline_instance_ids,
  393. pipeline_list->count), GFP_KERNEL);
  394. if (!trigger_list)
  395. return -ENOMEM;
  396. pipe_priority = kzalloc(pipeline_list->count, GFP_KERNEL);
  397. if (!pipe_priority) {
  398. kfree(trigger_list);
  399. return -ENOMEM;
  400. }
  401. mutex_lock(&ipc4_data->pipeline_state_mutex);
  402. /*
  403. * IPC4 requires pipelines to be triggered in order starting at the sink and
  404. * walking all the way to the source. So traverse the pipeline_list in the order
  405. * sink->source when starting PCM's and in the reverse order to pause/stop PCM's.
  406. * Skip the pipelines that have their skip_during_fe_trigger flag set. If there is a fork
  407. * in the pipeline, the order of triggering between the left/right paths will be
  408. * indeterministic. But the sink->source trigger order sink->source would still be
  409. * guaranteed for each fork independently.
  410. */
  411. if (state == SOF_IPC4_PIPE_RUNNING || state == SOF_IPC4_PIPE_RESET)
  412. for (i = pipeline_list->count - 1; i >= 0; i--) {
  413. spipe = pipeline_list->pipelines[i];
  414. sof_ipc4_add_pipeline_to_trigger_list(sdev, state, spipe, trigger_list,
  415. pipe_priority);
  416. }
  417. else
  418. for (i = 0; i < pipeline_list->count; i++) {
  419. spipe = pipeline_list->pipelines[i];
  420. sof_ipc4_add_pipeline_to_trigger_list(sdev, state, spipe, trigger_list,
  421. pipe_priority);
  422. }
  423. /* return if all pipelines are in the requested state already */
  424. if (!trigger_list->count) {
  425. ret = 0;
  426. goto free;
  427. }
  428. /* no need to pause before reset or before pause release */
  429. if (state == SOF_IPC4_PIPE_RESET || cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE)
  430. goto skip_pause_transition;
  431. /*
  432. * set paused state for pipelines if the final state is PAUSED or when the pipeline
  433. * is set to RUNNING for the first time after the PCM is started.
  434. */
  435. ret = sof_ipc4_set_multi_pipeline_state(sdev, SOF_IPC4_PIPE_PAUSED, trigger_list);
  436. if (ret < 0) {
  437. dev_err(sdev->dev, "failed to pause all pipelines\n");
  438. goto free;
  439. }
  440. /* update PAUSED state for all pipelines just triggered */
  441. for (i = 0; i < pipeline_list->count ; i++) {
  442. spipe = pipeline_list->pipelines[i];
  443. sof_ipc4_update_pipeline_state(sdev, SOF_IPC4_PIPE_PAUSED, cmd, spipe,
  444. trigger_list);
  445. }
  446. /* return if this is the final state */
  447. if (state == SOF_IPC4_PIPE_PAUSED) {
  448. struct sof_ipc4_timestamp_info *time_info;
  449. /*
  450. * Invalidate the stream_start_offset to make sure that it is
  451. * going to be updated if the stream resumes
  452. */
  453. time_info = sof_ipc4_sps_to_time_info(&spcm->stream[substream->stream]);
  454. if (time_info)
  455. time_info->stream_start_offset = SOF_IPC4_INVALID_STREAM_POSITION;
  456. goto free;
  457. }
  458. skip_pause_transition:
  459. /* else set the RUNNING/RESET state in the DSP */
  460. ret = sof_ipc4_set_multi_pipeline_state(sdev, state, trigger_list);
  461. if (ret < 0) {
  462. dev_err(sdev->dev, "failed to set final state %d for all pipelines\n", state);
  463. /*
  464. * workaround: if the firmware is crashed while setting the
  465. * pipelines to reset state we must ignore the error code and
  466. * reset it to 0.
  467. * Since the firmware is crashed we will not send IPC messages
  468. * and we are going to see errors printed, but the state of the
  469. * widgets will be correct for the next boot.
  470. */
  471. if (sdev->fw_state != SOF_FW_CRASHED || state != SOF_IPC4_PIPE_RESET)
  472. goto free;
  473. ret = 0;
  474. }
  475. /* update RUNNING/RESET state for all pipelines that were just triggered */
  476. for (i = 0; i < pipeline_list->count; i++) {
  477. spipe = pipeline_list->pipelines[i];
  478. sof_ipc4_update_pipeline_state(sdev, state, cmd, spipe, trigger_list);
  479. }
  480. free:
  481. mutex_unlock(&ipc4_data->pipeline_state_mutex);
  482. kfree(trigger_list);
  483. kfree(pipe_priority);
  484. return ret;
  485. }
  486. static int sof_ipc4_pcm_trigger(struct snd_soc_component *component,
  487. struct snd_pcm_substream *substream, int cmd)
  488. {
  489. int state;
  490. /* determine the pipeline state */
  491. switch (cmd) {
  492. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  493. case SNDRV_PCM_TRIGGER_RESUME:
  494. case SNDRV_PCM_TRIGGER_START:
  495. state = SOF_IPC4_PIPE_RUNNING;
  496. break;
  497. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  498. case SNDRV_PCM_TRIGGER_SUSPEND:
  499. case SNDRV_PCM_TRIGGER_STOP:
  500. state = SOF_IPC4_PIPE_PAUSED;
  501. break;
  502. default:
  503. dev_err(component->dev, "%s: unhandled trigger cmd %d\n", __func__, cmd);
  504. return -EINVAL;
  505. }
  506. /* set the pipeline state */
  507. return sof_ipc4_trigger_pipelines(component, substream, state, cmd);
  508. }
  509. static int sof_ipc4_pcm_hw_free(struct snd_soc_component *component,
  510. struct snd_pcm_substream *substream)
  511. {
  512. /* command is not relevant with RESET, so just pass 0 */
  513. return sof_ipc4_trigger_pipelines(component, substream, SOF_IPC4_PIPE_RESET, 0);
  514. }
  515. static void ipc4_ssp_dai_config_pcm_params_match(struct snd_sof_dev *sdev, const char *link_name,
  516. struct snd_pcm_hw_params *params)
  517. {
  518. struct snd_sof_dai_link *slink;
  519. struct snd_sof_dai *dai;
  520. bool dai_link_found = false;
  521. int i;
  522. list_for_each_entry(slink, &sdev->dai_link_list, list) {
  523. if (!strcmp(slink->link->name, link_name)) {
  524. dai_link_found = true;
  525. break;
  526. }
  527. }
  528. if (!dai_link_found)
  529. return;
  530. for (i = 0; i < slink->num_hw_configs; i++) {
  531. struct snd_soc_tplg_hw_config *hw_config = &slink->hw_configs[i];
  532. if (params_rate(params) == le32_to_cpu(hw_config->fsync_rate)) {
  533. /* set current config for all DAI's with matching name */
  534. list_for_each_entry(dai, &sdev->dai_list, list)
  535. if (!strcmp(slink->link->name, dai->name))
  536. dai->current_config = le32_to_cpu(hw_config->id);
  537. break;
  538. }
  539. }
  540. }
  541. /*
  542. * Fixup DAI link parameters for sampling rate based on
  543. * DAI copier configuration.
  544. */
  545. static int sof_ipc4_pcm_dai_link_fixup_rate(struct snd_sof_dev *sdev,
  546. struct snd_pcm_hw_params *params,
  547. struct sof_ipc4_copier *ipc4_copier)
  548. {
  549. struct sof_ipc4_pin_format *pin_fmts = ipc4_copier->available_fmt.input_pin_fmts;
  550. struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  551. int num_input_formats = ipc4_copier->available_fmt.num_input_formats;
  552. unsigned int fe_rate = params_rate(params);
  553. bool fe_be_rate_match = false;
  554. bool single_be_rate = true;
  555. unsigned int be_rate;
  556. int i;
  557. /*
  558. * Copier does not change sampling rate, so we
  559. * need to only consider the input pin information.
  560. */
  561. for (i = 0; i < num_input_formats; i++) {
  562. unsigned int val = pin_fmts[i].audio_fmt.sampling_frequency;
  563. if (i == 0)
  564. be_rate = val;
  565. else if (val != be_rate)
  566. single_be_rate = false;
  567. if (val == fe_rate) {
  568. fe_be_rate_match = true;
  569. break;
  570. }
  571. }
  572. /*
  573. * If rate is different than FE rate, topology must
  574. * contain an SRC. But we do require topology to
  575. * define a single rate in the DAI copier config in
  576. * this case (FE rate may be variable).
  577. */
  578. if (!fe_be_rate_match) {
  579. if (!single_be_rate) {
  580. dev_err(sdev->dev, "Unable to select sampling rate for DAI link\n");
  581. return -EINVAL;
  582. }
  583. rate->min = be_rate;
  584. rate->max = rate->min;
  585. }
  586. return 0;
  587. }
  588. static int sof_ipc4_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd,
  589. struct snd_pcm_hw_params *params)
  590. {
  591. struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
  592. struct snd_sof_dai *dai = snd_sof_find_dai(component, rtd->dai_link->name);
  593. struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  594. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  595. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  596. struct sof_ipc4_audio_format *ipc4_fmt;
  597. struct sof_ipc4_copier *ipc4_copier;
  598. bool single_bitdepth = false;
  599. u32 valid_bits = 0;
  600. int dir, ret;
  601. if (!dai) {
  602. dev_err(component->dev, "%s: No DAI found with name %s\n", __func__,
  603. rtd->dai_link->name);
  604. return -EINVAL;
  605. }
  606. ipc4_copier = dai->private;
  607. if (!ipc4_copier) {
  608. dev_err(component->dev, "%s: No private data found for DAI %s\n",
  609. __func__, rtd->dai_link->name);
  610. return -EINVAL;
  611. }
  612. for_each_pcm_streams(dir) {
  613. struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(cpu_dai, dir);
  614. if (w) {
  615. struct sof_ipc4_available_audio_format *available_fmt =
  616. &ipc4_copier->available_fmt;
  617. struct snd_sof_widget *swidget = w->dobj.private;
  618. struct snd_sof_widget *pipe_widget = swidget->spipe->pipe_widget;
  619. struct sof_ipc4_pipeline *pipeline = pipe_widget->private;
  620. /* Chain DMA does not use copiers, so no fixup needed */
  621. if (pipeline->use_chain_dma)
  622. return 0;
  623. if (dir == SNDRV_PCM_STREAM_PLAYBACK) {
  624. if (sof_ipc4_copier_is_single_bitdepth(sdev,
  625. available_fmt->output_pin_fmts,
  626. available_fmt->num_output_formats)) {
  627. ipc4_fmt = &available_fmt->output_pin_fmts->audio_fmt;
  628. single_bitdepth = true;
  629. }
  630. } else {
  631. if (sof_ipc4_copier_is_single_bitdepth(sdev,
  632. available_fmt->input_pin_fmts,
  633. available_fmt->num_input_formats)) {
  634. ipc4_fmt = &available_fmt->input_pin_fmts->audio_fmt;
  635. single_bitdepth = true;
  636. }
  637. }
  638. }
  639. }
  640. ret = sof_ipc4_pcm_dai_link_fixup_rate(sdev, params, ipc4_copier);
  641. if (ret)
  642. return ret;
  643. if (single_bitdepth) {
  644. snd_mask_none(fmt);
  645. valid_bits = SOF_IPC4_AUDIO_FORMAT_CFG_V_BIT_DEPTH(ipc4_fmt->fmt_cfg);
  646. dev_dbg(component->dev, "Set %s to %d bit format\n", dai->name, valid_bits);
  647. }
  648. /* Set format if it is specified */
  649. switch (valid_bits) {
  650. case 16:
  651. snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
  652. break;
  653. case 24:
  654. snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
  655. break;
  656. case 32:
  657. snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE);
  658. break;
  659. default:
  660. break;
  661. }
  662. switch (ipc4_copier->dai_type) {
  663. case SOF_DAI_INTEL_SSP:
  664. ipc4_ssp_dai_config_pcm_params_match(sdev, (char *)rtd->dai_link->name, params);
  665. break;
  666. default:
  667. break;
  668. }
  669. return 0;
  670. }
  671. static void sof_ipc4_pcm_free(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm)
  672. {
  673. struct snd_sof_pcm_stream_pipeline_list *pipeline_list;
  674. struct sof_ipc4_pcm_stream_priv *stream_priv;
  675. int stream;
  676. for_each_pcm_streams(stream) {
  677. pipeline_list = &spcm->stream[stream].pipeline_list;
  678. kfree(pipeline_list->pipelines);
  679. pipeline_list->pipelines = NULL;
  680. stream_priv = spcm->stream[stream].private;
  681. kfree(stream_priv->time_info);
  682. kfree(spcm->stream[stream].private);
  683. spcm->stream[stream].private = NULL;
  684. }
  685. }
  686. static int sof_ipc4_pcm_setup(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm)
  687. {
  688. struct snd_sof_pcm_stream_pipeline_list *pipeline_list;
  689. struct sof_ipc4_fw_data *ipc4_data = sdev->private;
  690. struct sof_ipc4_pcm_stream_priv *stream_priv;
  691. struct sof_ipc4_timestamp_info *time_info;
  692. bool support_info = true;
  693. u32 abi_version;
  694. u32 abi_offset;
  695. int stream;
  696. abi_offset = offsetof(struct sof_ipc4_fw_registers, abi_ver);
  697. sof_mailbox_read(sdev, sdev->fw_info_box.offset + abi_offset, &abi_version,
  698. sizeof(abi_version));
  699. if (abi_version < SOF_IPC4_FW_REGS_ABI_VER)
  700. support_info = false;
  701. /* For delay reporting the get_host_byte_counter callback is needed */
  702. if (!sof_ops(sdev) || !sof_ops(sdev)->get_host_byte_counter)
  703. support_info = false;
  704. for_each_pcm_streams(stream) {
  705. pipeline_list = &spcm->stream[stream].pipeline_list;
  706. /* allocate memory for max number of pipeline IDs */
  707. pipeline_list->pipelines = kcalloc(ipc4_data->max_num_pipelines,
  708. sizeof(*pipeline_list->pipelines),
  709. GFP_KERNEL);
  710. if (!pipeline_list->pipelines) {
  711. sof_ipc4_pcm_free(sdev, spcm);
  712. return -ENOMEM;
  713. }
  714. stream_priv = kzalloc(sizeof(*stream_priv), GFP_KERNEL);
  715. if (!stream_priv) {
  716. sof_ipc4_pcm_free(sdev, spcm);
  717. return -ENOMEM;
  718. }
  719. spcm->stream[stream].private = stream_priv;
  720. /* Delay reporting is only supported on playback */
  721. if (!support_info || stream == SNDRV_PCM_STREAM_CAPTURE)
  722. continue;
  723. time_info = kzalloc(sizeof(*time_info), GFP_KERNEL);
  724. if (!time_info) {
  725. sof_ipc4_pcm_free(sdev, spcm);
  726. return -ENOMEM;
  727. }
  728. stream_priv->time_info = time_info;
  729. }
  730. return 0;
  731. }
  732. static void sof_ipc4_build_time_info(struct snd_sof_dev *sdev, struct snd_sof_pcm_stream *sps)
  733. {
  734. struct sof_ipc4_copier *host_copier = NULL;
  735. struct sof_ipc4_copier *dai_copier = NULL;
  736. struct sof_ipc4_llp_reading_slot llp_slot;
  737. struct sof_ipc4_timestamp_info *time_info;
  738. struct snd_soc_dapm_widget *widget;
  739. struct snd_sof_dai *dai;
  740. int i;
  741. /* find host & dai to locate info in memory window */
  742. for_each_dapm_widgets(sps->list, i, widget) {
  743. struct snd_sof_widget *swidget = widget->dobj.private;
  744. if (!swidget)
  745. continue;
  746. if (WIDGET_IS_AIF(swidget->widget->id)) {
  747. host_copier = swidget->private;
  748. } else if (WIDGET_IS_DAI(swidget->widget->id)) {
  749. dai = swidget->private;
  750. dai_copier = dai->private;
  751. }
  752. }
  753. /* both host and dai copier must be valid for time_info */
  754. if (!host_copier || !dai_copier) {
  755. dev_err(sdev->dev, "host or dai copier are not found\n");
  756. return;
  757. }
  758. time_info = sof_ipc4_sps_to_time_info(sps);
  759. time_info->host_copier = host_copier;
  760. time_info->dai_copier = dai_copier;
  761. time_info->llp_offset = offsetof(struct sof_ipc4_fw_registers,
  762. llp_gpdma_reading_slots) + sdev->fw_info_box.offset;
  763. /* find llp slot used by current dai */
  764. for (i = 0; i < SOF_IPC4_MAX_LLP_GPDMA_READING_SLOTS; i++) {
  765. sof_mailbox_read(sdev, time_info->llp_offset, &llp_slot, sizeof(llp_slot));
  766. if (llp_slot.node_id == dai_copier->data.gtw_cfg.node_id)
  767. break;
  768. time_info->llp_offset += sizeof(llp_slot);
  769. }
  770. if (i < SOF_IPC4_MAX_LLP_GPDMA_READING_SLOTS)
  771. return;
  772. /* if no llp gpdma slot is used, check aggregated sdw slot */
  773. time_info->llp_offset = offsetof(struct sof_ipc4_fw_registers,
  774. llp_sndw_reading_slots) + sdev->fw_info_box.offset;
  775. for (i = 0; i < SOF_IPC4_MAX_LLP_SNDW_READING_SLOTS; i++) {
  776. sof_mailbox_read(sdev, time_info->llp_offset, &llp_slot, sizeof(llp_slot));
  777. if (llp_slot.node_id == dai_copier->data.gtw_cfg.node_id)
  778. break;
  779. time_info->llp_offset += sizeof(llp_slot);
  780. }
  781. if (i < SOF_IPC4_MAX_LLP_SNDW_READING_SLOTS)
  782. return;
  783. /* check EVAD slot */
  784. time_info->llp_offset = offsetof(struct sof_ipc4_fw_registers,
  785. llp_evad_reading_slot) + sdev->fw_info_box.offset;
  786. sof_mailbox_read(sdev, time_info->llp_offset, &llp_slot, sizeof(llp_slot));
  787. if (llp_slot.node_id != dai_copier->data.gtw_cfg.node_id)
  788. time_info->llp_offset = 0;
  789. }
  790. static int sof_ipc4_pcm_hw_params(struct snd_soc_component *component,
  791. struct snd_pcm_substream *substream,
  792. struct snd_pcm_hw_params *params,
  793. struct snd_sof_platform_stream_params *platform_params)
  794. {
  795. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  796. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  797. struct sof_ipc4_timestamp_info *time_info;
  798. struct snd_sof_pcm *spcm;
  799. spcm = snd_sof_find_spcm_dai(component, rtd);
  800. if (!spcm)
  801. return -EINVAL;
  802. time_info = sof_ipc4_sps_to_time_info(&spcm->stream[substream->stream]);
  803. /* delay calculation is not supported by current fw_reg ABI */
  804. if (!time_info)
  805. return 0;
  806. time_info->stream_start_offset = SOF_IPC4_INVALID_STREAM_POSITION;
  807. time_info->llp_offset = 0;
  808. sof_ipc4_build_time_info(sdev, &spcm->stream[substream->stream]);
  809. return 0;
  810. }
  811. static u64 sof_ipc4_frames_dai_to_host(struct sof_ipc4_timestamp_info *time_info, u64 value)
  812. {
  813. u64 dai_rate, host_rate;
  814. if (!time_info->dai_copier || !time_info->host_copier)
  815. return value;
  816. /*
  817. * copiers do not change sampling rate, so we can use the
  818. * out_format independently of stream direction
  819. */
  820. dai_rate = time_info->dai_copier->data.out_format.sampling_frequency;
  821. host_rate = time_info->host_copier->data.out_format.sampling_frequency;
  822. if (!dai_rate || !host_rate || dai_rate == host_rate)
  823. return value;
  824. /* take care not to overflow u64, rates can be up to 768000 */
  825. if (value > U32_MAX) {
  826. value = div64_u64(value, dai_rate);
  827. value *= host_rate;
  828. } else {
  829. value *= host_rate;
  830. value = div64_u64(value, dai_rate);
  831. }
  832. return value;
  833. }
  834. static int sof_ipc4_get_stream_start_offset(struct snd_sof_dev *sdev,
  835. struct snd_pcm_substream *substream,
  836. struct snd_sof_pcm_stream *sps,
  837. struct sof_ipc4_timestamp_info *time_info)
  838. {
  839. struct sof_ipc4_copier *host_copier = time_info->host_copier;
  840. struct sof_ipc4_copier *dai_copier = time_info->dai_copier;
  841. struct sof_ipc4_pipeline_registers ppl_reg;
  842. u32 dai_sample_size;
  843. u32 ch, node_index;
  844. u32 offset;
  845. if (!host_copier || !dai_copier)
  846. return -EINVAL;
  847. if (host_copier->data.gtw_cfg.node_id == SOF_IPC4_INVALID_NODE_ID) {
  848. return -EINVAL;
  849. } else if (host_copier->data.gtw_cfg.node_id == SOF_IPC4_CHAIN_DMA_NODE_ID) {
  850. /*
  851. * While the firmware does not support time_info reporting for
  852. * streams using ChainDMA, it is granted that ChainDMA can only
  853. * be used on Host+Link pairs where the link position is
  854. * accessible from the host side.
  855. *
  856. * Enable delay calculation in case of ChainDMA via host
  857. * accessible registers.
  858. *
  859. * The ChainDMA prefills the link DMA with a preamble
  860. * of zero samples. Set the stream start offset based
  861. * on size of the preamble (driver provided fifo size
  862. * multiplied by 2.5). We add 1ms of margin as the FW
  863. * will align the buffer size to DMA hardware
  864. * alignment that is not known to host.
  865. */
  866. int pre_ms = SOF_IPC4_CHAIN_DMA_BUF_SIZE_MS * 5 / 2 + 1;
  867. time_info->stream_start_offset = pre_ms * substream->runtime->rate / MSEC_PER_SEC;
  868. goto out;
  869. }
  870. node_index = SOF_IPC4_NODE_INDEX(host_copier->data.gtw_cfg.node_id);
  871. offset = offsetof(struct sof_ipc4_fw_registers, pipeline_regs) + node_index * sizeof(ppl_reg);
  872. sof_mailbox_read(sdev, sdev->fw_info_box.offset + offset, &ppl_reg, sizeof(ppl_reg));
  873. if (ppl_reg.stream_start_offset == SOF_IPC4_INVALID_STREAM_POSITION)
  874. return -EINVAL;
  875. ch = dai_copier->data.out_format.fmt_cfg;
  876. ch = SOF_IPC4_AUDIO_FORMAT_CFG_CHANNELS_COUNT(ch);
  877. dai_sample_size = (dai_copier->data.out_format.bit_depth >> 3) * ch;
  878. /* convert offsets to frame count */
  879. time_info->stream_start_offset = ppl_reg.stream_start_offset;
  880. do_div(time_info->stream_start_offset, dai_sample_size);
  881. time_info->stream_end_offset = ppl_reg.stream_end_offset;
  882. do_div(time_info->stream_end_offset, dai_sample_size);
  883. /* convert to host frame time */
  884. time_info->stream_start_offset =
  885. sof_ipc4_frames_dai_to_host(time_info, time_info->stream_start_offset);
  886. time_info->stream_end_offset =
  887. sof_ipc4_frames_dai_to_host(time_info, time_info->stream_end_offset);
  888. out:
  889. /* Initialize the delay value to 0 (no delay) */
  890. time_info->delay = 0;
  891. return 0;
  892. }
  893. static int sof_ipc4_pcm_pointer(struct snd_soc_component *component,
  894. struct snd_pcm_substream *substream,
  895. snd_pcm_uframes_t *pointer)
  896. {
  897. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  898. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  899. struct sof_ipc4_timestamp_info *time_info;
  900. struct sof_ipc4_llp_reading_slot llp;
  901. snd_pcm_uframes_t head_cnt, tail_cnt;
  902. struct snd_sof_pcm_stream *sps;
  903. u64 dai_cnt, host_cnt, host_ptr;
  904. struct snd_sof_pcm *spcm;
  905. int ret;
  906. spcm = snd_sof_find_spcm_dai(component, rtd);
  907. if (!spcm)
  908. return -EOPNOTSUPP;
  909. sps = &spcm->stream[substream->stream];
  910. time_info = sof_ipc4_sps_to_time_info(sps);
  911. if (!time_info)
  912. return -EOPNOTSUPP;
  913. /*
  914. * stream_start_offset is updated to memory window by FW based on
  915. * pipeline statistics and it may be invalid if host query happens before
  916. * the statistics is complete. And it will not change after the first initiailization.
  917. */
  918. if (time_info->stream_start_offset == SOF_IPC4_INVALID_STREAM_POSITION) {
  919. ret = sof_ipc4_get_stream_start_offset(sdev, substream, sps, time_info);
  920. if (ret < 0)
  921. return -EOPNOTSUPP;
  922. }
  923. /* For delay calculation we need the host counter */
  924. host_cnt = snd_sof_pcm_get_host_byte_counter(sdev, component, substream);
  925. /* Store the original value to host_ptr */
  926. host_ptr = host_cnt;
  927. /* convert the host_cnt to frames */
  928. host_cnt = div64_u64(host_cnt, frames_to_bytes(substream->runtime, 1));
  929. /*
  930. * If the LLP counter is not reported by firmware in the SRAM window
  931. * then read the dai (link) counter via host accessible means if
  932. * available.
  933. */
  934. if (!time_info->llp_offset) {
  935. dai_cnt = snd_sof_pcm_get_dai_frame_counter(sdev, component, substream);
  936. if (!dai_cnt)
  937. return -EOPNOTSUPP;
  938. } else {
  939. sof_mailbox_read(sdev, time_info->llp_offset, &llp, sizeof(llp));
  940. dai_cnt = ((u64)llp.reading.llp_u << 32) | llp.reading.llp_l;
  941. }
  942. dai_cnt = sof_ipc4_frames_dai_to_host(time_info, dai_cnt);
  943. dai_cnt += time_info->stream_end_offset;
  944. /* In two cases dai dma counter is not accurate
  945. * (1) dai pipeline is started before host pipeline
  946. * (2) multiple streams mixed into one. Each stream has the same dai dma
  947. * counter
  948. *
  949. * Firmware calculates correct stream_start_offset for all cases
  950. * including above two.
  951. * Driver subtracts stream_start_offset from dai dma counter to get
  952. * accurate one
  953. */
  954. /*
  955. * On stream start the dai counter might not yet have reached the
  956. * stream_start_offset value which means that no frames have left the
  957. * DSP yet from the audio stream (on playback, capture streams have
  958. * offset of 0 as we start capturing right away).
  959. * In this case we need to adjust the distance between the counters by
  960. * increasing the host counter by (offset - dai_counter).
  961. * Otherwise the dai_counter needs to be adjusted to reflect the number
  962. * of valid frames passed on the DAI side.
  963. *
  964. * The delay is the difference between the counters on the two
  965. * sides of the DSP.
  966. */
  967. if (dai_cnt < time_info->stream_start_offset) {
  968. host_cnt += time_info->stream_start_offset - dai_cnt;
  969. dai_cnt = 0;
  970. } else {
  971. dai_cnt -= time_info->stream_start_offset;
  972. }
  973. /* Convert to a common base before comparisons */
  974. dai_cnt &= DELAY_BOUNDARY;
  975. host_cnt &= DELAY_BOUNDARY;
  976. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  977. head_cnt = host_cnt;
  978. tail_cnt = dai_cnt;
  979. } else {
  980. head_cnt = dai_cnt;
  981. tail_cnt = host_cnt;
  982. }
  983. if (unlikely(head_cnt < tail_cnt))
  984. time_info->delay = DELAY_BOUNDARY - tail_cnt + head_cnt;
  985. else
  986. time_info->delay = head_cnt - tail_cnt;
  987. /*
  988. * Convert the host byte counter to PCM pointer which wraps in buffer
  989. * and it is in frames
  990. */
  991. div64_u64_rem(host_ptr, snd_pcm_lib_buffer_bytes(substream), &host_ptr);
  992. *pointer = bytes_to_frames(substream->runtime, host_ptr);
  993. return 0;
  994. }
  995. static snd_pcm_sframes_t sof_ipc4_pcm_delay(struct snd_soc_component *component,
  996. struct snd_pcm_substream *substream)
  997. {
  998. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  999. struct sof_ipc4_timestamp_info *time_info;
  1000. struct snd_sof_pcm *spcm;
  1001. spcm = snd_sof_find_spcm_dai(component, rtd);
  1002. if (!spcm)
  1003. return 0;
  1004. time_info = sof_ipc4_sps_to_time_info(&spcm->stream[substream->stream]);
  1005. /*
  1006. * Report the stored delay value calculated in the pointer callback.
  1007. * In the unlikely event that the calculation was skipped/aborted, the
  1008. * default 0 delay returned.
  1009. */
  1010. if (time_info)
  1011. return time_info->delay;
  1012. /* No delay information available, report 0 as delay */
  1013. return 0;
  1014. }
  1015. const struct sof_ipc_pcm_ops ipc4_pcm_ops = {
  1016. .hw_params = sof_ipc4_pcm_hw_params,
  1017. .trigger = sof_ipc4_pcm_trigger,
  1018. .hw_free = sof_ipc4_pcm_hw_free,
  1019. .dai_link_fixup = sof_ipc4_pcm_dai_link_fixup,
  1020. .pcm_setup = sof_ipc4_pcm_setup,
  1021. .pcm_free = sof_ipc4_pcm_free,
  1022. .pointer = sof_ipc4_pcm_pointer,
  1023. .delay = sof_ipc4_pcm_delay,
  1024. .ipc_first_on_start = true,
  1025. .platform_stop_during_hw_free = true,
  1026. };