1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
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.
6 // Copyright(c) 2021 Intel Corporation. All rights reserved.
10 #include <sound/sof/stream.h>
11 #include <sound/sof/control.h>
13 #include "sof-audio.h"
14 #include "ipc3-priv.h"
17 typedef void (*ipc3_rx_callback)(struct snd_sof_dev *sdev, void *msg_buf);
19 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_VERBOSE_IPC)
20 static void ipc3_log_header(struct device *dev, u8 *text, u32 cmd)
28 glb = cmd & SOF_GLB_TYPE_MASK;
29 type = cmd & SOF_CMD_TYPE_MASK;
32 case SOF_IPC_GLB_REPLY:
33 str = "GLB_REPLY"; break;
34 case SOF_IPC_GLB_COMPOUND:
35 str = "GLB_COMPOUND"; break;
36 case SOF_IPC_GLB_TPLG_MSG:
39 case SOF_IPC_TPLG_COMP_NEW:
40 str2 = "COMP_NEW"; break;
41 case SOF_IPC_TPLG_COMP_FREE:
42 str2 = "COMP_FREE"; break;
43 case SOF_IPC_TPLG_COMP_CONNECT:
44 str2 = "COMP_CONNECT"; break;
45 case SOF_IPC_TPLG_PIPE_NEW:
46 str2 = "PIPE_NEW"; break;
47 case SOF_IPC_TPLG_PIPE_FREE:
48 str2 = "PIPE_FREE"; break;
49 case SOF_IPC_TPLG_PIPE_CONNECT:
50 str2 = "PIPE_CONNECT"; break;
51 case SOF_IPC_TPLG_PIPE_COMPLETE:
52 str2 = "PIPE_COMPLETE"; break;
53 case SOF_IPC_TPLG_BUFFER_NEW:
54 str2 = "BUFFER_NEW"; break;
55 case SOF_IPC_TPLG_BUFFER_FREE:
56 str2 = "BUFFER_FREE"; break;
58 str2 = "unknown type"; break;
61 case SOF_IPC_GLB_PM_MSG:
64 case SOF_IPC_PM_CTX_SAVE:
65 str2 = "CTX_SAVE"; break;
66 case SOF_IPC_PM_CTX_RESTORE:
67 str2 = "CTX_RESTORE"; break;
68 case SOF_IPC_PM_CTX_SIZE:
69 str2 = "CTX_SIZE"; break;
70 case SOF_IPC_PM_CLK_SET:
71 str2 = "CLK_SET"; break;
72 case SOF_IPC_PM_CLK_GET:
73 str2 = "CLK_GET"; break;
74 case SOF_IPC_PM_CLK_REQ:
75 str2 = "CLK_REQ"; break;
76 case SOF_IPC_PM_CORE_ENABLE:
77 str2 = "CORE_ENABLE"; break;
81 str2 = "unknown type"; break;
84 case SOF_IPC_GLB_COMP_MSG:
87 case SOF_IPC_COMP_SET_VALUE:
88 str2 = "SET_VALUE"; break;
89 case SOF_IPC_COMP_GET_VALUE:
90 str2 = "GET_VALUE"; break;
91 case SOF_IPC_COMP_SET_DATA:
92 str2 = "SET_DATA"; break;
93 case SOF_IPC_COMP_GET_DATA:
94 str2 = "GET_DATA"; break;
96 str2 = "unknown type"; break;
99 case SOF_IPC_GLB_STREAM_MSG:
100 str = "GLB_STREAM_MSG";
102 case SOF_IPC_STREAM_PCM_PARAMS:
103 str2 = "PCM_PARAMS"; break;
104 case SOF_IPC_STREAM_PCM_PARAMS_REPLY:
105 str2 = "PCM_REPLY"; break;
106 case SOF_IPC_STREAM_PCM_FREE:
107 str2 = "PCM_FREE"; break;
108 case SOF_IPC_STREAM_TRIG_START:
109 str2 = "TRIG_START"; break;
110 case SOF_IPC_STREAM_TRIG_STOP:
111 str2 = "TRIG_STOP"; break;
112 case SOF_IPC_STREAM_TRIG_PAUSE:
113 str2 = "TRIG_PAUSE"; break;
114 case SOF_IPC_STREAM_TRIG_RELEASE:
115 str2 = "TRIG_RELEASE"; break;
116 case SOF_IPC_STREAM_TRIG_DRAIN:
117 str2 = "TRIG_DRAIN"; break;
118 case SOF_IPC_STREAM_TRIG_XRUN:
119 str2 = "TRIG_XRUN"; break;
120 case SOF_IPC_STREAM_POSITION:
122 str2 = "POSITION"; break;
123 case SOF_IPC_STREAM_VORBIS_PARAMS:
124 str2 = "VORBIS_PARAMS"; break;
125 case SOF_IPC_STREAM_VORBIS_FREE:
126 str2 = "VORBIS_FREE"; break;
128 str2 = "unknown type"; break;
131 case SOF_IPC_FW_READY:
132 str = "FW_READY"; break;
133 case SOF_IPC_GLB_DAI_MSG:
136 case SOF_IPC_DAI_CONFIG:
137 str2 = "CONFIG"; break;
138 case SOF_IPC_DAI_LOOPBACK:
139 str2 = "LOOPBACK"; break;
141 str2 = "unknown type"; break;
144 case SOF_IPC_GLB_TRACE_MSG:
145 str = "GLB_TRACE_MSG";
147 case SOF_IPC_TRACE_DMA_PARAMS:
148 str2 = "DMA_PARAMS"; break;
149 case SOF_IPC_TRACE_DMA_POSITION:
150 if (!sof_debug_check_flag(SOF_DBG_PRINT_DMA_POSITION_UPDATE_LOGS))
152 str2 = "DMA_POSITION"; break;
153 case SOF_IPC_TRACE_DMA_PARAMS_EXT:
154 str2 = "DMA_PARAMS_EXT"; break;
155 case SOF_IPC_TRACE_FILTER_UPDATE:
156 str2 = "FILTER_UPDATE"; break;
157 case SOF_IPC_TRACE_DMA_FREE:
158 str2 = "DMA_FREE"; break;
160 str2 = "unknown type"; break;
163 case SOF_IPC_GLB_TEST_MSG:
164 str = "GLB_TEST_MSG";
166 case SOF_IPC_TEST_IPC_FLOOD:
167 str2 = "IPC_FLOOD"; break;
169 str2 = "unknown type"; break;
172 case SOF_IPC_GLB_DEBUG:
175 case SOF_IPC_DEBUG_MEM_USAGE:
176 str2 = "MEM_USAGE"; break;
178 str2 = "unknown type"; break;
181 case SOF_IPC_GLB_PROBE:
184 case SOF_IPC_PROBE_INIT:
185 str2 = "INIT"; break;
186 case SOF_IPC_PROBE_DEINIT:
187 str2 = "DEINIT"; break;
188 case SOF_IPC_PROBE_DMA_ADD:
189 str2 = "DMA_ADD"; break;
190 case SOF_IPC_PROBE_DMA_INFO:
191 str2 = "DMA_INFO"; break;
192 case SOF_IPC_PROBE_DMA_REMOVE:
193 str2 = "DMA_REMOVE"; break;
194 case SOF_IPC_PROBE_POINT_ADD:
195 str2 = "POINT_ADD"; break;
196 case SOF_IPC_PROBE_POINT_INFO:
197 str2 = "POINT_INFO"; break;
198 case SOF_IPC_PROBE_POINT_REMOVE:
199 str2 = "POINT_REMOVE"; break;
201 str2 = "unknown type"; break;
205 str = "unknown GLB command"; break;
210 dev_vdbg(dev, "%s: 0x%x: %s: %s\n", text, cmd, str, str2);
212 dev_dbg(dev, "%s: 0x%x: %s: %s\n", text, cmd, str, str2);
214 dev_dbg(dev, "%s: 0x%x: %s\n", text, cmd, str);
218 static inline void ipc3_log_header(struct device *dev, u8 *text, u32 cmd)
220 if ((cmd & SOF_GLB_TYPE_MASK) != SOF_IPC_GLB_TRACE_MSG)
221 dev_dbg(dev, "%s: 0x%x\n", text, cmd);
225 static int sof_ipc3_get_reply(struct snd_sof_dev *sdev)
227 struct snd_sof_ipc_msg *msg = sdev->msg;
228 struct sof_ipc_reply *reply;
231 /* get the generic reply */
232 reply = msg->reply_data;
233 snd_sof_dsp_mailbox_read(sdev, sdev->host_box.offset, reply, sizeof(*reply));
235 if (reply->error < 0)
238 if (!reply->hdr.size) {
239 /* Reply should always be >= sizeof(struct sof_ipc_reply) */
242 "empty reply received, expected %zu bytes\n",
245 dev_err(sdev->dev, "empty reply received\n");
250 if (msg->reply_size > 0) {
251 if (reply->hdr.size == msg->reply_size) {
253 } else if (reply->hdr.size < msg->reply_size) {
255 "reply size (%u) is less than expected (%zu)\n",
256 reply->hdr.size, msg->reply_size);
258 msg->reply_size = reply->hdr.size;
262 "reply size (%u) exceeds the buffer size (%zu)\n",
263 reply->hdr.size, msg->reply_size);
268 * get the full message if reply->hdr.size <= msg->reply_size
269 * and the reply->hdr.size > sizeof(struct sof_ipc_reply)
271 if (!ret && msg->reply_size > sizeof(*reply))
272 snd_sof_dsp_mailbox_read(sdev, sdev->host_box.offset,
273 msg->reply_data, msg->reply_size);
279 /* wait for IPC message reply */
280 static int ipc3_wait_tx_done(struct snd_sof_ipc *ipc, void *reply_data)
282 struct snd_sof_ipc_msg *msg = &ipc->msg;
283 struct sof_ipc_cmd_hdr *hdr = msg->msg_data;
284 struct snd_sof_dev *sdev = ipc->sdev;
287 /* wait for DSP IPC completion */
288 ret = wait_event_timeout(msg->waitq, msg->ipc_complete,
289 msecs_to_jiffies(sdev->ipc_timeout));
293 "ipc tx timed out for %#x (msg/reply size: %d/%zu)\n",
294 hdr->cmd, hdr->size, msg->reply_size);
295 snd_sof_handle_fw_exception(ipc->sdev, "IPC timeout");
298 ret = msg->reply_error;
301 "ipc tx error for %#x (msg/reply size: %d/%zu): %d\n",
302 hdr->cmd, hdr->size, msg->reply_size, ret);
304 if (sof_debug_check_flag(SOF_DBG_PRINT_IPC_SUCCESS_LOGS))
305 ipc3_log_header(sdev->dev, "ipc tx succeeded", hdr->cmd);
307 /* copy the data returned from DSP */
308 memcpy(reply_data, msg->reply_data,
312 /* re-enable dumps after successful IPC tx */
313 if (sdev->ipc_dump_printed) {
314 sdev->dbg_dump_printed = false;
315 sdev->ipc_dump_printed = false;
322 /* send IPC message from host to DSP */
323 static int ipc3_tx_msg_unlocked(struct snd_sof_ipc *ipc,
324 void *msg_data, size_t msg_bytes,
325 void *reply_data, size_t reply_bytes)
327 struct sof_ipc_cmd_hdr *hdr = msg_data;
328 struct snd_sof_dev *sdev = ipc->sdev;
331 ret = sof_ipc_send_msg(sdev, msg_data, msg_bytes, reply_bytes);
334 dev_err_ratelimited(sdev->dev,
335 "%s: ipc message send for %#x failed: %d\n",
336 __func__, hdr->cmd, ret);
340 ipc3_log_header(sdev->dev, "ipc tx", hdr->cmd);
342 /* now wait for completion */
343 return ipc3_wait_tx_done(ipc, reply_data);
346 static int sof_ipc3_tx_msg(struct snd_sof_dev *sdev, void *msg_data, size_t msg_bytes,
347 void *reply_data, size_t reply_bytes, bool no_pm)
349 struct snd_sof_ipc *ipc = sdev->ipc;
352 if (!msg_data || msg_bytes < sizeof(struct sof_ipc_cmd_hdr)) {
353 dev_err_ratelimited(sdev->dev, "No IPC message to send\n");
358 const struct sof_dsp_power_state target_state = {
359 .state = SOF_DSP_PM_D0,
362 /* ensure the DSP is in D0 before sending a new IPC */
363 ret = snd_sof_dsp_set_power_state(sdev, &target_state);
365 dev_err(sdev->dev, "%s: resuming DSP failed: %d\n",
371 /* Serialise IPC TX */
372 mutex_lock(&ipc->tx_mutex);
374 ret = ipc3_tx_msg_unlocked(ipc, msg_data, msg_bytes, reply_data, reply_bytes);
376 mutex_unlock(&ipc->tx_mutex);
381 static int sof_ipc3_set_get_data(struct snd_sof_dev *sdev, void *data, size_t data_bytes,
384 size_t msg_bytes, hdr_bytes, payload_size, send_bytes;
385 struct sof_ipc_ctrl_data *cdata = data;
386 struct sof_ipc_ctrl_data *cdata_chunk;
387 struct snd_sof_ipc *ipc = sdev->ipc;
394 if (!cdata || data_bytes < sizeof(*cdata))
397 if ((cdata->rhdr.hdr.cmd & SOF_GLB_TYPE_MASK) != SOF_IPC_GLB_COMP_MSG) {
398 dev_err(sdev->dev, "%s: Not supported message type of %#x\n",
399 __func__, cdata->rhdr.hdr.cmd);
403 /* send normal size ipc in one part */
404 if (cdata->rhdr.hdr.size <= ipc->max_payload_size)
405 return sof_ipc3_tx_msg(sdev, cdata, cdata->rhdr.hdr.size,
406 cdata, cdata->rhdr.hdr.size, false);
408 cdata_chunk = kzalloc(ipc->max_payload_size, GFP_KERNEL);
412 switch (cdata->type) {
413 case SOF_CTRL_TYPE_VALUE_CHAN_GET:
414 case SOF_CTRL_TYPE_VALUE_CHAN_SET:
415 hdr_bytes = sizeof(struct sof_ipc_ctrl_data);
417 src = (u8 *)cdata->chanv;
418 dst = (u8 *)cdata_chunk->chanv;
420 src = (u8 *)cdata_chunk->chanv;
421 dst = (u8 *)cdata->chanv;
424 case SOF_CTRL_TYPE_DATA_GET:
425 case SOF_CTRL_TYPE_DATA_SET:
426 hdr_bytes = sizeof(struct sof_ipc_ctrl_data) + sizeof(struct sof_abi_hdr);
428 src = (u8 *)cdata->data->data;
429 dst = (u8 *)cdata_chunk->data->data;
431 src = (u8 *)cdata_chunk->data->data;
432 dst = (u8 *)cdata->data->data;
440 msg_bytes = cdata->rhdr.hdr.size - hdr_bytes;
441 payload_size = ipc->max_payload_size - hdr_bytes;
442 num_msg = DIV_ROUND_UP(msg_bytes, payload_size);
444 /* copy the header data */
445 memcpy(cdata_chunk, cdata, hdr_bytes);
447 /* Serialise IPC TX */
448 mutex_lock(&sdev->ipc->tx_mutex);
450 /* copy the payload data in a loop */
451 for (i = 0; i < num_msg; i++) {
452 send_bytes = min(msg_bytes, payload_size);
453 cdata_chunk->num_elems = send_bytes;
454 cdata_chunk->rhdr.hdr.size = hdr_bytes + send_bytes;
455 cdata_chunk->msg_index = i;
456 msg_bytes -= send_bytes;
457 cdata_chunk->elems_remaining = msg_bytes;
460 memcpy(dst, src + offset, send_bytes);
462 ret = ipc3_tx_msg_unlocked(sdev->ipc,
463 cdata_chunk, cdata_chunk->rhdr.hdr.size,
464 cdata_chunk, cdata_chunk->rhdr.hdr.size);
469 memcpy(dst + offset, src, send_bytes);
471 offset += payload_size;
474 mutex_unlock(&sdev->ipc->tx_mutex);
481 int sof_ipc3_get_ext_windows(struct snd_sof_dev *sdev,
482 const struct sof_ipc_ext_data_hdr *ext_hdr)
484 const struct sof_ipc_window *w =
485 container_of(ext_hdr, struct sof_ipc_window, ext_hdr);
487 if (w->num_windows == 0 || w->num_windows > SOF_IPC_MAX_ELEMS)
490 if (sdev->info_window) {
491 if (memcmp(sdev->info_window, w, ext_hdr->hdr.size)) {
492 dev_err(sdev->dev, "mismatch between window descriptor from extended manifest and mailbox");
498 /* keep a local copy of the data */
499 sdev->info_window = devm_kmemdup(sdev->dev, w, ext_hdr->hdr.size, GFP_KERNEL);
500 if (!sdev->info_window)
506 int sof_ipc3_get_cc_info(struct snd_sof_dev *sdev,
507 const struct sof_ipc_ext_data_hdr *ext_hdr)
511 const struct sof_ipc_cc_version *cc =
512 container_of(ext_hdr, struct sof_ipc_cc_version, ext_hdr);
514 if (sdev->cc_version) {
515 if (memcmp(sdev->cc_version, cc, cc->ext_hdr.hdr.size)) {
517 "Receive diverged cc_version descriptions");
524 "Firmware info: used compiler %s %d:%d:%d%s used optimization flags %s\n",
525 cc->name, cc->major, cc->minor, cc->micro, cc->desc, cc->optim);
527 /* create read-only cc_version debugfs to store compiler version info */
528 /* use local copy of the cc_version to prevent data corruption */
529 if (sdev->first_boot) {
530 sdev->cc_version = devm_kmalloc(sdev->dev, cc->ext_hdr.hdr.size,
533 if (!sdev->cc_version)
536 memcpy(sdev->cc_version, cc, cc->ext_hdr.hdr.size);
537 ret = snd_sof_debugfs_buf_item(sdev, sdev->cc_version,
538 cc->ext_hdr.hdr.size,
541 /* errors are only due to memory allocation, not debugfs */
543 dev_err(sdev->dev, "snd_sof_debugfs_buf_item failed\n");
551 /* parse the extended FW boot data structures from FW boot message */
552 static int ipc3_fw_parse_ext_data(struct snd_sof_dev *sdev, u32 offset)
554 struct sof_ipc_ext_data_hdr *ext_hdr;
558 ext_data = kzalloc(PAGE_SIZE, GFP_KERNEL);
562 /* get first header */
563 snd_sof_dsp_block_read(sdev, SOF_FW_BLK_TYPE_SRAM, offset, ext_data,
567 while (ext_hdr->hdr.cmd == SOF_IPC_FW_READY) {
568 /* read in ext structure */
569 snd_sof_dsp_block_read(sdev, SOF_FW_BLK_TYPE_SRAM,
570 offset + sizeof(*ext_hdr),
571 (void *)((u8 *)ext_data + sizeof(*ext_hdr)),
572 ext_hdr->hdr.size - sizeof(*ext_hdr));
574 dev_dbg(sdev->dev, "found ext header type %d size 0x%x\n",
575 ext_hdr->type, ext_hdr->hdr.size);
577 /* process structure data */
578 switch (ext_hdr->type) {
579 case SOF_IPC_EXT_WINDOW:
580 ret = sof_ipc3_get_ext_windows(sdev, ext_hdr);
582 case SOF_IPC_EXT_CC_INFO:
583 ret = sof_ipc3_get_cc_info(sdev, ext_hdr);
585 case SOF_IPC_EXT_UNUSED:
586 case SOF_IPC_EXT_PROBE_INFO:
587 case SOF_IPC_EXT_USER_ABI_INFO:
588 /* They are supported but we don't do anything here */
591 dev_info(sdev->dev, "unknown ext header type %d size 0x%x\n",
592 ext_hdr->type, ext_hdr->hdr.size);
598 dev_err(sdev->dev, "Failed to parse ext data type %d\n",
603 /* move to next header */
604 offset += ext_hdr->hdr.size;
605 snd_sof_dsp_block_read(sdev, SOF_FW_BLK_TYPE_SRAM, offset, ext_data,
614 static void ipc3_get_windows(struct snd_sof_dev *sdev)
616 struct sof_ipc_window_elem *elem;
617 u32 outbox_offset = 0;
618 u32 stream_offset = 0;
619 u32 inbox_offset = 0;
624 u32 debug_offset = 0;
628 if (!sdev->info_window) {
629 dev_err(sdev->dev, "%s: No window info present\n", __func__);
633 for (i = 0; i < sdev->info_window->num_windows; i++) {
634 elem = &sdev->info_window->window[i];
636 window_offset = snd_sof_dsp_get_window_offset(sdev, elem->id);
637 if (window_offset < 0) {
638 dev_warn(sdev->dev, "No offset for window %d\n", elem->id);
642 switch (elem->type) {
643 case SOF_IPC_REGION_UPBOX:
644 inbox_offset = window_offset + elem->offset;
645 inbox_size = elem->size;
646 snd_sof_debugfs_add_region_item(sdev, SOF_FW_BLK_TYPE_SRAM,
649 SOF_DEBUGFS_ACCESS_D0_ONLY);
651 case SOF_IPC_REGION_DOWNBOX:
652 outbox_offset = window_offset + elem->offset;
653 outbox_size = elem->size;
654 snd_sof_debugfs_add_region_item(sdev, SOF_FW_BLK_TYPE_SRAM,
656 elem->size, "outbox",
657 SOF_DEBUGFS_ACCESS_D0_ONLY);
659 case SOF_IPC_REGION_TRACE:
660 snd_sof_debugfs_add_region_item(sdev, SOF_FW_BLK_TYPE_SRAM,
661 window_offset + elem->offset,
662 elem->size, "etrace",
663 SOF_DEBUGFS_ACCESS_D0_ONLY);
665 case SOF_IPC_REGION_DEBUG:
666 debug_offset = window_offset + elem->offset;
667 debug_size = elem->size;
668 snd_sof_debugfs_add_region_item(sdev, SOF_FW_BLK_TYPE_SRAM,
669 window_offset + elem->offset,
671 SOF_DEBUGFS_ACCESS_D0_ONLY);
673 case SOF_IPC_REGION_STREAM:
674 stream_offset = window_offset + elem->offset;
675 stream_size = elem->size;
676 snd_sof_debugfs_add_region_item(sdev, SOF_FW_BLK_TYPE_SRAM,
678 elem->size, "stream",
679 SOF_DEBUGFS_ACCESS_D0_ONLY);
681 case SOF_IPC_REGION_REGS:
682 snd_sof_debugfs_add_region_item(sdev, SOF_FW_BLK_TYPE_SRAM,
683 window_offset + elem->offset,
685 SOF_DEBUGFS_ACCESS_D0_ONLY);
687 case SOF_IPC_REGION_EXCEPTION:
688 sdev->dsp_oops_offset = window_offset + elem->offset;
689 snd_sof_debugfs_add_region_item(sdev, SOF_FW_BLK_TYPE_SRAM,
690 window_offset + elem->offset,
691 elem->size, "exception",
692 SOF_DEBUGFS_ACCESS_D0_ONLY);
695 dev_err(sdev->dev, "%s: Illegal window info: %u\n",
696 __func__, elem->type);
701 if (outbox_size == 0 || inbox_size == 0) {
702 dev_err(sdev->dev, "%s: Illegal mailbox window\n", __func__);
706 sdev->dsp_box.offset = inbox_offset;
707 sdev->dsp_box.size = inbox_size;
709 sdev->host_box.offset = outbox_offset;
710 sdev->host_box.size = outbox_size;
712 sdev->stream_box.offset = stream_offset;
713 sdev->stream_box.size = stream_size;
715 sdev->debug_box.offset = debug_offset;
716 sdev->debug_box.size = debug_size;
718 dev_dbg(sdev->dev, " mailbox upstream 0x%x - size 0x%x\n",
719 inbox_offset, inbox_size);
720 dev_dbg(sdev->dev, " mailbox downstream 0x%x - size 0x%x\n",
721 outbox_offset, outbox_size);
722 dev_dbg(sdev->dev, " stream region 0x%x - size 0x%x\n",
723 stream_offset, stream_size);
724 dev_dbg(sdev->dev, " debug region 0x%x - size 0x%x\n",
725 debug_offset, debug_size);
728 static int ipc3_init_reply_data_buffer(struct snd_sof_dev *sdev)
730 struct snd_sof_ipc_msg *msg = &sdev->ipc->msg;
732 msg->reply_data = devm_kzalloc(sdev->dev, SOF_IPC_MSG_MAX_SIZE, GFP_KERNEL);
733 if (!msg->reply_data)
736 sdev->ipc->max_payload_size = SOF_IPC_MSG_MAX_SIZE;
741 int sof_ipc3_validate_fw_version(struct snd_sof_dev *sdev)
743 struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
744 struct sof_ipc_fw_version *v = &ready->version;
747 "Firmware info: version %d:%d:%d-%s\n", v->major, v->minor,
750 "Firmware: ABI %d:%d:%d Kernel ABI %d:%d:%d\n",
751 SOF_ABI_VERSION_MAJOR(v->abi_version),
752 SOF_ABI_VERSION_MINOR(v->abi_version),
753 SOF_ABI_VERSION_PATCH(v->abi_version),
754 SOF_ABI_MAJOR, SOF_ABI_MINOR, SOF_ABI_PATCH);
756 if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, v->abi_version)) {
757 dev_err(sdev->dev, "incompatible FW ABI version\n");
761 if (IS_ENABLED(CONFIG_SND_SOC_SOF_STRICT_ABI_CHECKS) &&
762 SOF_ABI_VERSION_MINOR(v->abi_version) > SOF_ABI_MINOR) {
763 dev_err(sdev->dev, "FW ABI is more recent than kernel\n");
767 if (ready->flags & SOF_IPC_INFO_BUILD) {
769 "Firmware debug build %d on %s-%s - options:\n"
772 " lock vdebug: %s\n",
773 v->build, v->date, v->time,
774 (ready->flags & SOF_IPC_INFO_GDB) ?
775 "enabled" : "disabled",
776 (ready->flags & SOF_IPC_INFO_LOCKS) ?
777 "enabled" : "disabled",
778 (ready->flags & SOF_IPC_INFO_LOCKSV) ?
779 "enabled" : "disabled");
782 /* copy the fw_version into debugfs at first boot */
783 memcpy(&sdev->fw_version, v, sizeof(*v));
788 static int ipc3_fw_ready(struct snd_sof_dev *sdev, u32 cmd)
790 struct sof_ipc_fw_ready *fw_ready = &sdev->fw_ready;
794 /* mailbox must be on 4k boundary */
795 offset = snd_sof_dsp_get_mailbox_offset(sdev);
797 dev_err(sdev->dev, "%s: no mailbox offset\n", __func__);
801 dev_dbg(sdev->dev, "DSP is ready 0x%8.8x offset 0x%x\n", cmd, offset);
803 /* no need to re-check version/ABI for subsequent boots */
804 if (!sdev->first_boot)
808 * copy data from the DSP FW ready offset
809 * Subsequent error handling is not needed for BLK_TYPE_SRAM
811 ret = snd_sof_dsp_block_read(sdev, SOF_FW_BLK_TYPE_SRAM, offset, fw_ready,
815 "Unable to read fw_ready, read from TYPE_SRAM failed\n");
819 /* make sure ABI version is compatible */
820 ret = sof_ipc3_validate_fw_version(sdev);
824 /* now check for extended data */
825 ipc3_fw_parse_ext_data(sdev, offset + sizeof(struct sof_ipc_fw_ready));
827 ipc3_get_windows(sdev);
829 return ipc3_init_reply_data_buffer(sdev);
832 /* IPC stream position. */
833 static void ipc3_period_elapsed(struct snd_sof_dev *sdev, u32 msg_id)
835 struct snd_soc_component *scomp = sdev->component;
836 struct snd_sof_pcm_stream *stream;
837 struct sof_ipc_stream_posn posn;
838 struct snd_sof_pcm *spcm;
841 spcm = snd_sof_find_spcm_comp(scomp, msg_id, &direction);
843 dev_err(sdev->dev, "period elapsed for unknown stream, msg_id %d\n",
848 stream = &spcm->stream[direction];
849 ret = snd_sof_ipc_msg_data(sdev, stream->substream, &posn, sizeof(posn));
851 dev_warn(sdev->dev, "failed to read stream position: %d\n", ret);
855 dev_vdbg(sdev->dev, "posn : host 0x%llx dai 0x%llx wall 0x%llx\n",
856 posn.host_posn, posn.dai_posn, posn.wallclock);
858 memcpy(&stream->posn, &posn, sizeof(posn));
860 if (spcm->pcm.compress)
861 snd_sof_compr_fragment_elapsed(stream->cstream);
862 else if (stream->substream->runtime &&
863 !stream->substream->runtime->no_period_wakeup)
864 /* only inform ALSA for period_wakeup mode */
865 snd_sof_pcm_period_elapsed(stream->substream);
868 /* DSP notifies host of an XRUN within FW */
869 static void ipc3_xrun(struct snd_sof_dev *sdev, u32 msg_id)
871 struct snd_soc_component *scomp = sdev->component;
872 struct snd_sof_pcm_stream *stream;
873 struct sof_ipc_stream_posn posn;
874 struct snd_sof_pcm *spcm;
877 spcm = snd_sof_find_spcm_comp(scomp, msg_id, &direction);
879 dev_err(sdev->dev, "XRUN for unknown stream, msg_id %d\n",
884 stream = &spcm->stream[direction];
885 ret = snd_sof_ipc_msg_data(sdev, stream->substream, &posn, sizeof(posn));
887 dev_warn(sdev->dev, "failed to read overrun position: %d\n", ret);
891 dev_dbg(sdev->dev, "posn XRUN: host %llx comp %d size %d\n",
892 posn.host_posn, posn.xrun_comp_id, posn.xrun_size);
894 #if defined(CONFIG_SND_SOC_SOF_DEBUG_XRUN_STOP)
895 /* stop PCM on XRUN - used for pipeline debug */
896 memcpy(&stream->posn, &posn, sizeof(posn));
897 snd_pcm_stop_xrun(stream->substream);
901 /* stream notifications from firmware */
902 static void ipc3_stream_message(struct snd_sof_dev *sdev, void *msg_buf)
904 struct sof_ipc_cmd_hdr *hdr = msg_buf;
905 u32 msg_type = hdr->cmd & SOF_CMD_TYPE_MASK;
906 u32 msg_id = SOF_IPC_MESSAGE_ID(hdr->cmd);
909 case SOF_IPC_STREAM_POSITION:
910 ipc3_period_elapsed(sdev, msg_id);
912 case SOF_IPC_STREAM_TRIG_XRUN:
913 ipc3_xrun(sdev, msg_id);
916 dev_err(sdev->dev, "unhandled stream message %#x\n",
922 /* component notifications from firmware */
923 static void ipc3_comp_notification(struct snd_sof_dev *sdev, void *msg_buf)
925 const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
926 struct sof_ipc_cmd_hdr *hdr = msg_buf;
927 u32 msg_type = hdr->cmd & SOF_CMD_TYPE_MASK;
930 case SOF_IPC_COMP_GET_VALUE:
931 case SOF_IPC_COMP_GET_DATA:
934 dev_err(sdev->dev, "unhandled component message %#x\n", msg_type);
938 if (tplg_ops->control->update)
939 tplg_ops->control->update(sdev, msg_buf);
942 static void ipc3_trace_message(struct snd_sof_dev *sdev, void *msg_buf)
944 struct sof_ipc_cmd_hdr *hdr = msg_buf;
945 u32 msg_type = hdr->cmd & SOF_CMD_TYPE_MASK;
948 case SOF_IPC_TRACE_DMA_POSITION:
949 ipc3_dtrace_posn_update(sdev, msg_buf);
952 dev_err(sdev->dev, "unhandled trace message %#x\n", msg_type);
957 /* DSP firmware has sent host a message */
958 static void sof_ipc3_rx_msg(struct snd_sof_dev *sdev)
960 ipc3_rx_callback rx_callback = NULL;
961 struct sof_ipc_cmd_hdr hdr;
966 /* read back header */
967 err = snd_sof_ipc_msg_data(sdev, NULL, &hdr, sizeof(hdr));
969 dev_warn(sdev->dev, "failed to read IPC header: %d\n", err);
973 if (hdr.size < sizeof(hdr)) {
974 dev_err(sdev->dev, "The received message size is invalid\n");
978 ipc3_log_header(sdev->dev, "ipc rx", hdr.cmd);
980 cmd = hdr.cmd & SOF_GLB_TYPE_MASK;
982 /* check message type */
984 case SOF_IPC_GLB_REPLY:
985 dev_err(sdev->dev, "ipc reply unknown\n");
987 case SOF_IPC_FW_READY:
988 /* check for FW boot completion */
989 if (sdev->fw_state == SOF_FW_BOOT_IN_PROGRESS) {
990 err = ipc3_fw_ready(sdev, cmd);
992 sof_set_fw_state(sdev, SOF_FW_BOOT_READY_FAILED);
994 sof_set_fw_state(sdev, SOF_FW_BOOT_READY_OK);
996 /* wake up firmware loader */
997 wake_up(&sdev->boot_wait);
1000 case SOF_IPC_GLB_COMPOUND:
1001 case SOF_IPC_GLB_TPLG_MSG:
1002 case SOF_IPC_GLB_PM_MSG:
1004 case SOF_IPC_GLB_COMP_MSG:
1005 rx_callback = ipc3_comp_notification;
1007 case SOF_IPC_GLB_STREAM_MSG:
1008 rx_callback = ipc3_stream_message;
1010 case SOF_IPC_GLB_TRACE_MSG:
1011 rx_callback = ipc3_trace_message;
1014 dev_err(sdev->dev, "%s: Unknown DSP message: 0x%x\n", __func__, cmd);
1018 /* read the full message */
1019 msg_buf = kmalloc(hdr.size, GFP_KERNEL);
1023 err = snd_sof_ipc_msg_data(sdev, NULL, msg_buf, hdr.size);
1025 dev_err(sdev->dev, "%s: Failed to read message: %d\n", __func__, err);
1027 /* Call local handler for the message */
1029 rx_callback(sdev, msg_buf);
1031 /* Notify registered clients */
1032 sof_client_ipc_rx_dispatcher(sdev, msg_buf);
1037 ipc3_log_header(sdev->dev, "ipc rx done", hdr.cmd);
1040 static int sof_ipc3_set_core_state(struct snd_sof_dev *sdev, int core_idx, bool on)
1042 struct sof_ipc_pm_core_config core_cfg = {
1043 .hdr.size = sizeof(core_cfg),
1044 .hdr.cmd = SOF_IPC_GLB_PM_MSG | SOF_IPC_PM_CORE_ENABLE,
1046 struct sof_ipc_reply reply;
1049 core_cfg.enable_mask = sdev->enabled_cores_mask | BIT(core_idx);
1051 core_cfg.enable_mask = sdev->enabled_cores_mask & ~BIT(core_idx);
1053 return sof_ipc3_tx_msg(sdev, &core_cfg, sizeof(core_cfg),
1054 &reply, sizeof(reply), false);
1057 static int sof_ipc3_ctx_ipc(struct snd_sof_dev *sdev, int cmd)
1059 struct sof_ipc_pm_ctx pm_ctx = {
1060 .hdr.size = sizeof(pm_ctx),
1061 .hdr.cmd = SOF_IPC_GLB_PM_MSG | cmd,
1063 struct sof_ipc_reply reply;
1065 /* send ctx save ipc to dsp */
1066 return sof_ipc3_tx_msg(sdev, &pm_ctx, sizeof(pm_ctx),
1067 &reply, sizeof(reply), false);
1070 static int sof_ipc3_ctx_save(struct snd_sof_dev *sdev)
1072 return sof_ipc3_ctx_ipc(sdev, SOF_IPC_PM_CTX_SAVE);
1075 static int sof_ipc3_ctx_restore(struct snd_sof_dev *sdev)
1077 return sof_ipc3_ctx_ipc(sdev, SOF_IPC_PM_CTX_RESTORE);
1080 static const struct sof_ipc_pm_ops ipc3_pm_ops = {
1081 .ctx_save = sof_ipc3_ctx_save,
1082 .ctx_restore = sof_ipc3_ctx_restore,
1083 .set_core_state = sof_ipc3_set_core_state,
1086 const struct sof_ipc_ops ipc3_ops = {
1087 .tplg = &ipc3_tplg_ops,
1089 .pcm = &ipc3_pcm_ops,
1090 .fw_loader = &ipc3_loader_ops,
1091 .fw_tracing = &ipc3_dtrace_ops,
1093 .tx_msg = sof_ipc3_tx_msg,
1094 .rx_msg = sof_ipc3_rx_msg,
1095 .set_get_data = sof_ipc3_set_get_data,
1096 .get_reply = sof_ipc3_get_reply,