ipc4-pcm.c 40 KB

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