gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / sound / soc / sof / ipc.c
blob1c6794918cbb2e148abc18344eea68738728fded
1 // SPDX-License-Identifier: (GPL-2.0 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) 2018 Intel Corporation. All rights reserved.
7 //
8 // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9 //
10 // Generic IPC layer that can work over MMIO and SPI/I2C. PHY layer provided
11 // by platform driver code.
14 #include <linux/mutex.h>
15 #include <linux/types.h>
17 #include "sof-priv.h"
18 #include "sof-audio.h"
19 #include "ops.h"
21 static void ipc_trace_message(struct snd_sof_dev *sdev, u32 msg_id);
22 static void ipc_stream_message(struct snd_sof_dev *sdev, u32 msg_cmd);
25 * IPC message Tx/Rx message handling.
28 /* SOF generic IPC data */
29 struct snd_sof_ipc {
30 struct snd_sof_dev *sdev;
32 /* protects messages and the disable flag */
33 struct mutex tx_mutex;
34 /* disables further sending of ipc's */
35 bool disable_ipc_tx;
37 struct snd_sof_ipc_msg msg;
40 struct sof_ipc_ctrl_data_params {
41 size_t msg_bytes;
42 size_t hdr_bytes;
43 size_t pl_size;
44 size_t elems;
45 u32 num_msg;
46 u8 *src;
47 u8 *dst;
50 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_VERBOSE_IPC)
51 static void ipc_log_header(struct device *dev, u8 *text, u32 cmd)
53 u8 *str;
54 u8 *str2 = NULL;
55 u32 glb;
56 u32 type;
58 glb = cmd & SOF_GLB_TYPE_MASK;
59 type = cmd & SOF_CMD_TYPE_MASK;
61 switch (glb) {
62 case SOF_IPC_GLB_REPLY:
63 str = "GLB_REPLY"; break;
64 case SOF_IPC_GLB_COMPOUND:
65 str = "GLB_COMPOUND"; break;
66 case SOF_IPC_GLB_TPLG_MSG:
67 str = "GLB_TPLG_MSG";
68 switch (type) {
69 case SOF_IPC_TPLG_COMP_NEW:
70 str2 = "COMP_NEW"; break;
71 case SOF_IPC_TPLG_COMP_FREE:
72 str2 = "COMP_FREE"; break;
73 case SOF_IPC_TPLG_COMP_CONNECT:
74 str2 = "COMP_CONNECT"; break;
75 case SOF_IPC_TPLG_PIPE_NEW:
76 str2 = "PIPE_NEW"; break;
77 case SOF_IPC_TPLG_PIPE_FREE:
78 str2 = "PIPE_FREE"; break;
79 case SOF_IPC_TPLG_PIPE_CONNECT:
80 str2 = "PIPE_CONNECT"; break;
81 case SOF_IPC_TPLG_PIPE_COMPLETE:
82 str2 = "PIPE_COMPLETE"; break;
83 case SOF_IPC_TPLG_BUFFER_NEW:
84 str2 = "BUFFER_NEW"; break;
85 case SOF_IPC_TPLG_BUFFER_FREE:
86 str2 = "BUFFER_FREE"; break;
87 default:
88 str2 = "unknown type"; break;
90 break;
91 case SOF_IPC_GLB_PM_MSG:
92 str = "GLB_PM_MSG";
93 switch (type) {
94 case SOF_IPC_PM_CTX_SAVE:
95 str2 = "CTX_SAVE"; break;
96 case SOF_IPC_PM_CTX_RESTORE:
97 str2 = "CTX_RESTORE"; break;
98 case SOF_IPC_PM_CTX_SIZE:
99 str2 = "CTX_SIZE"; break;
100 case SOF_IPC_PM_CLK_SET:
101 str2 = "CLK_SET"; break;
102 case SOF_IPC_PM_CLK_GET:
103 str2 = "CLK_GET"; break;
104 case SOF_IPC_PM_CLK_REQ:
105 str2 = "CLK_REQ"; break;
106 case SOF_IPC_PM_CORE_ENABLE:
107 str2 = "CORE_ENABLE"; break;
108 default:
109 str2 = "unknown type"; break;
111 break;
112 case SOF_IPC_GLB_COMP_MSG:
113 str = "GLB_COMP_MSG";
114 switch (type) {
115 case SOF_IPC_COMP_SET_VALUE:
116 str2 = "SET_VALUE"; break;
117 case SOF_IPC_COMP_GET_VALUE:
118 str2 = "GET_VALUE"; break;
119 case SOF_IPC_COMP_SET_DATA:
120 str2 = "SET_DATA"; break;
121 case SOF_IPC_COMP_GET_DATA:
122 str2 = "GET_DATA"; break;
123 default:
124 str2 = "unknown type"; break;
126 break;
127 case SOF_IPC_GLB_STREAM_MSG:
128 str = "GLB_STREAM_MSG";
129 switch (type) {
130 case SOF_IPC_STREAM_PCM_PARAMS:
131 str2 = "PCM_PARAMS"; break;
132 case SOF_IPC_STREAM_PCM_PARAMS_REPLY:
133 str2 = "PCM_REPLY"; break;
134 case SOF_IPC_STREAM_PCM_FREE:
135 str2 = "PCM_FREE"; break;
136 case SOF_IPC_STREAM_TRIG_START:
137 str2 = "TRIG_START"; break;
138 case SOF_IPC_STREAM_TRIG_STOP:
139 str2 = "TRIG_STOP"; break;
140 case SOF_IPC_STREAM_TRIG_PAUSE:
141 str2 = "TRIG_PAUSE"; break;
142 case SOF_IPC_STREAM_TRIG_RELEASE:
143 str2 = "TRIG_RELEASE"; break;
144 case SOF_IPC_STREAM_TRIG_DRAIN:
145 str2 = "TRIG_DRAIN"; break;
146 case SOF_IPC_STREAM_TRIG_XRUN:
147 str2 = "TRIG_XRUN"; break;
148 case SOF_IPC_STREAM_POSITION:
149 str2 = "POSITION"; break;
150 case SOF_IPC_STREAM_VORBIS_PARAMS:
151 str2 = "VORBIS_PARAMS"; break;
152 case SOF_IPC_STREAM_VORBIS_FREE:
153 str2 = "VORBIS_FREE"; break;
154 default:
155 str2 = "unknown type"; break;
157 break;
158 case SOF_IPC_FW_READY:
159 str = "FW_READY"; break;
160 case SOF_IPC_GLB_DAI_MSG:
161 str = "GLB_DAI_MSG";
162 switch (type) {
163 case SOF_IPC_DAI_CONFIG:
164 str2 = "CONFIG"; break;
165 case SOF_IPC_DAI_LOOPBACK:
166 str2 = "LOOPBACK"; break;
167 default:
168 str2 = "unknown type"; break;
170 break;
171 case SOF_IPC_GLB_TRACE_MSG:
172 str = "GLB_TRACE_MSG"; break;
173 case SOF_IPC_GLB_TEST_MSG:
174 str = "GLB_TEST_MSG";
175 switch (type) {
176 case SOF_IPC_TEST_IPC_FLOOD:
177 str2 = "IPC_FLOOD"; break;
178 default:
179 str2 = "unknown type"; break;
181 break;
182 default:
183 str = "unknown GLB command"; break;
186 if (str2)
187 dev_dbg(dev, "%s: 0x%x: %s: %s\n", text, cmd, str, str2);
188 else
189 dev_dbg(dev, "%s: 0x%x: %s\n", text, cmd, str);
191 #else
192 static inline void ipc_log_header(struct device *dev, u8 *text, u32 cmd)
194 if ((cmd & SOF_GLB_TYPE_MASK) != SOF_IPC_GLB_TRACE_MSG)
195 dev_dbg(dev, "%s: 0x%x\n", text, cmd);
197 #endif
199 /* wait for IPC message reply */
200 static int tx_wait_done(struct snd_sof_ipc *ipc, struct snd_sof_ipc_msg *msg,
201 void *reply_data)
203 struct snd_sof_dev *sdev = ipc->sdev;
204 struct sof_ipc_cmd_hdr *hdr = msg->msg_data;
205 int ret;
207 /* wait for DSP IPC completion */
208 ret = wait_event_timeout(msg->waitq, msg->ipc_complete,
209 msecs_to_jiffies(sdev->ipc_timeout));
211 if (ret == 0) {
212 dev_err(sdev->dev, "error: ipc timed out for 0x%x size %d\n",
213 hdr->cmd, hdr->size);
214 snd_sof_handle_fw_exception(ipc->sdev);
215 ret = -ETIMEDOUT;
216 } else {
217 ret = msg->reply_error;
218 if (ret < 0) {
219 dev_err(sdev->dev, "error: ipc error for 0x%x size %zu\n",
220 hdr->cmd, msg->reply_size);
221 } else {
222 ipc_log_header(sdev->dev, "ipc tx succeeded", hdr->cmd);
223 if (msg->reply_size)
224 /* copy the data returned from DSP */
225 memcpy(reply_data, msg->reply_data,
226 msg->reply_size);
230 return ret;
233 /* send IPC message from host to DSP */
234 static int sof_ipc_tx_message_unlocked(struct snd_sof_ipc *ipc, u32 header,
235 void *msg_data, size_t msg_bytes,
236 void *reply_data, size_t reply_bytes)
238 struct snd_sof_dev *sdev = ipc->sdev;
239 struct snd_sof_ipc_msg *msg;
240 int ret;
242 if (ipc->disable_ipc_tx)
243 return -ENODEV;
246 * The spin-lock is also still needed to protect message objects against
247 * other atomic contexts.
249 spin_lock_irq(&sdev->ipc_lock);
251 /* initialise the message */
252 msg = &ipc->msg;
254 msg->header = header;
255 msg->msg_size = msg_bytes;
256 msg->reply_size = reply_bytes;
257 msg->reply_error = 0;
259 /* attach any data */
260 if (msg_bytes)
261 memcpy(msg->msg_data, msg_data, msg_bytes);
263 sdev->msg = msg;
265 ret = snd_sof_dsp_send_msg(sdev, msg);
266 /* Next reply that we receive will be related to this message */
267 if (!ret)
268 msg->ipc_complete = false;
270 spin_unlock_irq(&sdev->ipc_lock);
272 if (ret < 0) {
273 dev_err_ratelimited(sdev->dev,
274 "error: ipc tx failed with error %d\n",
275 ret);
276 return ret;
279 ipc_log_header(sdev->dev, "ipc tx", msg->header);
281 /* now wait for completion */
282 if (!ret)
283 ret = tx_wait_done(ipc, msg, reply_data);
285 return ret;
288 /* send IPC message from host to DSP */
289 int sof_ipc_tx_message(struct snd_sof_ipc *ipc, u32 header,
290 void *msg_data, size_t msg_bytes, void *reply_data,
291 size_t reply_bytes)
293 const struct sof_dsp_power_state target_state = {
294 .state = SOF_DSP_PM_D0,
296 int ret;
298 /* ensure the DSP is in D0 before sending a new IPC */
299 ret = snd_sof_dsp_set_power_state(ipc->sdev, &target_state);
300 if (ret < 0) {
301 dev_err(ipc->sdev->dev, "error: resuming DSP %d\n", ret);
302 return ret;
305 return sof_ipc_tx_message_no_pm(ipc, header, msg_data, msg_bytes,
306 reply_data, reply_bytes);
308 EXPORT_SYMBOL(sof_ipc_tx_message);
311 * send IPC message from host to DSP without modifying the DSP state.
312 * This will be used for IPC's that can be handled by the DSP
313 * even in a low-power D0 substate.
315 int sof_ipc_tx_message_no_pm(struct snd_sof_ipc *ipc, u32 header,
316 void *msg_data, size_t msg_bytes,
317 void *reply_data, size_t reply_bytes)
319 int ret;
321 if (msg_bytes > SOF_IPC_MSG_MAX_SIZE ||
322 reply_bytes > SOF_IPC_MSG_MAX_SIZE)
323 return -ENOBUFS;
325 /* Serialise IPC TX */
326 mutex_lock(&ipc->tx_mutex);
328 ret = sof_ipc_tx_message_unlocked(ipc, header, msg_data, msg_bytes,
329 reply_data, reply_bytes);
331 mutex_unlock(&ipc->tx_mutex);
333 return ret;
335 EXPORT_SYMBOL(sof_ipc_tx_message_no_pm);
337 /* handle reply message from DSP */
338 int snd_sof_ipc_reply(struct snd_sof_dev *sdev, u32 msg_id)
340 struct snd_sof_ipc_msg *msg = &sdev->ipc->msg;
342 if (msg->ipc_complete) {
343 dev_err(sdev->dev, "error: no reply expected, received 0x%x",
344 msg_id);
345 return -EINVAL;
348 /* wake up and return the error if we have waiters on this message ? */
349 msg->ipc_complete = true;
350 wake_up(&msg->waitq);
352 return 0;
354 EXPORT_SYMBOL(snd_sof_ipc_reply);
356 /* DSP firmware has sent host a message */
357 void snd_sof_ipc_msgs_rx(struct snd_sof_dev *sdev)
359 struct sof_ipc_cmd_hdr hdr;
360 u32 cmd, type;
361 int err = 0;
363 /* read back header */
364 snd_sof_ipc_msg_data(sdev, NULL, &hdr, sizeof(hdr));
365 ipc_log_header(sdev->dev, "ipc rx", hdr.cmd);
367 cmd = hdr.cmd & SOF_GLB_TYPE_MASK;
368 type = hdr.cmd & SOF_CMD_TYPE_MASK;
370 /* check message type */
371 switch (cmd) {
372 case SOF_IPC_GLB_REPLY:
373 dev_err(sdev->dev, "error: ipc reply unknown\n");
374 break;
375 case SOF_IPC_FW_READY:
376 /* check for FW boot completion */
377 if (sdev->fw_state == SOF_FW_BOOT_IN_PROGRESS) {
378 err = sof_ops(sdev)->fw_ready(sdev, cmd);
379 if (err < 0)
380 sdev->fw_state = SOF_FW_BOOT_READY_FAILED;
381 else
382 sdev->fw_state = SOF_FW_BOOT_COMPLETE;
384 /* wake up firmware loader */
385 wake_up(&sdev->boot_wait);
387 break;
388 case SOF_IPC_GLB_COMPOUND:
389 case SOF_IPC_GLB_TPLG_MSG:
390 case SOF_IPC_GLB_PM_MSG:
391 case SOF_IPC_GLB_COMP_MSG:
392 break;
393 case SOF_IPC_GLB_STREAM_MSG:
394 /* need to pass msg id into the function */
395 ipc_stream_message(sdev, hdr.cmd);
396 break;
397 case SOF_IPC_GLB_TRACE_MSG:
398 ipc_trace_message(sdev, type);
399 break;
400 default:
401 dev_err(sdev->dev, "error: unknown DSP message 0x%x\n", cmd);
402 break;
405 ipc_log_header(sdev->dev, "ipc rx done", hdr.cmd);
407 EXPORT_SYMBOL(snd_sof_ipc_msgs_rx);
410 * IPC trace mechanism.
413 static void ipc_trace_message(struct snd_sof_dev *sdev, u32 msg_id)
415 struct sof_ipc_dma_trace_posn posn;
417 switch (msg_id) {
418 case SOF_IPC_TRACE_DMA_POSITION:
419 /* read back full message */
420 snd_sof_ipc_msg_data(sdev, NULL, &posn, sizeof(posn));
421 snd_sof_trace_update_pos(sdev, &posn);
422 break;
423 default:
424 dev_err(sdev->dev, "error: unhandled trace message %x\n",
425 msg_id);
426 break;
431 * IPC stream position.
434 static void ipc_period_elapsed(struct snd_sof_dev *sdev, u32 msg_id)
436 struct snd_soc_component *scomp = sdev->component;
437 struct snd_sof_pcm_stream *stream;
438 struct sof_ipc_stream_posn posn;
439 struct snd_sof_pcm *spcm;
440 int direction;
442 spcm = snd_sof_find_spcm_comp(scomp, msg_id, &direction);
443 if (!spcm) {
444 dev_err(sdev->dev,
445 "error: period elapsed for unknown stream, msg_id %d\n",
446 msg_id);
447 return;
450 stream = &spcm->stream[direction];
451 snd_sof_ipc_msg_data(sdev, stream->substream, &posn, sizeof(posn));
453 dev_dbg(sdev->dev, "posn : host 0x%llx dai 0x%llx wall 0x%llx\n",
454 posn.host_posn, posn.dai_posn, posn.wallclock);
456 memcpy(&stream->posn, &posn, sizeof(posn));
458 /* only inform ALSA for period_wakeup mode */
459 if (!stream->substream->runtime->no_period_wakeup)
460 snd_sof_pcm_period_elapsed(stream->substream);
463 /* DSP notifies host of an XRUN within FW */
464 static void ipc_xrun(struct snd_sof_dev *sdev, u32 msg_id)
466 struct snd_soc_component *scomp = sdev->component;
467 struct snd_sof_pcm_stream *stream;
468 struct sof_ipc_stream_posn posn;
469 struct snd_sof_pcm *spcm;
470 int direction;
472 spcm = snd_sof_find_spcm_comp(scomp, msg_id, &direction);
473 if (!spcm) {
474 dev_err(sdev->dev, "error: XRUN for unknown stream, msg_id %d\n",
475 msg_id);
476 return;
479 stream = &spcm->stream[direction];
480 snd_sof_ipc_msg_data(sdev, stream->substream, &posn, sizeof(posn));
482 dev_dbg(sdev->dev, "posn XRUN: host %llx comp %d size %d\n",
483 posn.host_posn, posn.xrun_comp_id, posn.xrun_size);
485 #if defined(CONFIG_SND_SOC_SOF_DEBUG_XRUN_STOP)
486 /* stop PCM on XRUN - used for pipeline debug */
487 memcpy(&stream->posn, &posn, sizeof(posn));
488 snd_pcm_stop_xrun(stream->substream);
489 #endif
492 /* stream notifications from DSP FW */
493 static void ipc_stream_message(struct snd_sof_dev *sdev, u32 msg_cmd)
495 /* get msg cmd type and msd id */
496 u32 msg_type = msg_cmd & SOF_CMD_TYPE_MASK;
497 u32 msg_id = SOF_IPC_MESSAGE_ID(msg_cmd);
499 switch (msg_type) {
500 case SOF_IPC_STREAM_POSITION:
501 ipc_period_elapsed(sdev, msg_id);
502 break;
503 case SOF_IPC_STREAM_TRIG_XRUN:
504 ipc_xrun(sdev, msg_id);
505 break;
506 default:
507 dev_err(sdev->dev, "error: unhandled stream message %x\n",
508 msg_id);
509 break;
513 /* get stream position IPC - use faster MMIO method if available on platform */
514 int snd_sof_ipc_stream_posn(struct snd_soc_component *scomp,
515 struct snd_sof_pcm *spcm, int direction,
516 struct sof_ipc_stream_posn *posn)
518 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
519 struct sof_ipc_stream stream;
520 int err;
522 /* read position via slower IPC */
523 stream.hdr.size = sizeof(stream);
524 stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_POSITION;
525 stream.comp_id = spcm->stream[direction].comp_id;
527 /* send IPC to the DSP */
528 err = sof_ipc_tx_message(sdev->ipc,
529 stream.hdr.cmd, &stream, sizeof(stream), posn,
530 sizeof(*posn));
531 if (err < 0) {
532 dev_err(sdev->dev, "error: failed to get stream %d position\n",
533 stream.comp_id);
534 return err;
537 return 0;
539 EXPORT_SYMBOL(snd_sof_ipc_stream_posn);
541 static int sof_get_ctrl_copy_params(enum sof_ipc_ctrl_type ctrl_type,
542 struct sof_ipc_ctrl_data *src,
543 struct sof_ipc_ctrl_data *dst,
544 struct sof_ipc_ctrl_data_params *sparams)
546 switch (ctrl_type) {
547 case SOF_CTRL_TYPE_VALUE_CHAN_GET:
548 case SOF_CTRL_TYPE_VALUE_CHAN_SET:
549 sparams->src = (u8 *)src->chanv;
550 sparams->dst = (u8 *)dst->chanv;
551 break;
552 case SOF_CTRL_TYPE_VALUE_COMP_GET:
553 case SOF_CTRL_TYPE_VALUE_COMP_SET:
554 sparams->src = (u8 *)src->compv;
555 sparams->dst = (u8 *)dst->compv;
556 break;
557 case SOF_CTRL_TYPE_DATA_GET:
558 case SOF_CTRL_TYPE_DATA_SET:
559 sparams->src = (u8 *)src->data->data;
560 sparams->dst = (u8 *)dst->data->data;
561 break;
562 default:
563 return -EINVAL;
566 /* calculate payload size and number of messages */
567 sparams->pl_size = SOF_IPC_MSG_MAX_SIZE - sparams->hdr_bytes;
568 sparams->num_msg = DIV_ROUND_UP(sparams->msg_bytes, sparams->pl_size);
570 return 0;
573 static int sof_set_get_large_ctrl_data(struct snd_sof_dev *sdev,
574 struct sof_ipc_ctrl_data *cdata,
575 struct sof_ipc_ctrl_data_params *sparams,
576 bool send)
578 struct sof_ipc_ctrl_data *partdata;
579 size_t send_bytes;
580 size_t offset = 0;
581 size_t msg_bytes;
582 size_t pl_size;
583 int err;
584 int i;
586 /* allocate max ipc size because we have at least one */
587 partdata = kzalloc(SOF_IPC_MSG_MAX_SIZE, GFP_KERNEL);
588 if (!partdata)
589 return -ENOMEM;
591 if (send)
592 err = sof_get_ctrl_copy_params(cdata->type, cdata, partdata,
593 sparams);
594 else
595 err = sof_get_ctrl_copy_params(cdata->type, partdata, cdata,
596 sparams);
597 if (err < 0) {
598 kfree(partdata);
599 return err;
602 msg_bytes = sparams->msg_bytes;
603 pl_size = sparams->pl_size;
605 /* copy the header data */
606 memcpy(partdata, cdata, sparams->hdr_bytes);
608 /* Serialise IPC TX */
609 mutex_lock(&sdev->ipc->tx_mutex);
611 /* copy the payload data in a loop */
612 for (i = 0; i < sparams->num_msg; i++) {
613 send_bytes = min(msg_bytes, pl_size);
614 partdata->num_elems = send_bytes;
615 partdata->rhdr.hdr.size = sparams->hdr_bytes + send_bytes;
616 partdata->msg_index = i;
617 msg_bytes -= send_bytes;
618 partdata->elems_remaining = msg_bytes;
620 if (send)
621 memcpy(sparams->dst, sparams->src + offset, send_bytes);
623 err = sof_ipc_tx_message_unlocked(sdev->ipc,
624 partdata->rhdr.hdr.cmd,
625 partdata,
626 partdata->rhdr.hdr.size,
627 partdata,
628 partdata->rhdr.hdr.size);
629 if (err < 0)
630 break;
632 if (!send)
633 memcpy(sparams->dst + offset, sparams->src, send_bytes);
635 offset += pl_size;
638 mutex_unlock(&sdev->ipc->tx_mutex);
640 kfree(partdata);
641 return err;
645 * IPC get()/set() for kcontrols.
647 int snd_sof_ipc_set_get_comp_data(struct snd_sof_control *scontrol,
648 u32 ipc_cmd,
649 enum sof_ipc_ctrl_type ctrl_type,
650 enum sof_ipc_ctrl_cmd ctrl_cmd,
651 bool send)
653 struct snd_soc_component *scomp = scontrol->scomp;
654 struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
655 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
656 struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
657 struct sof_ipc_fw_version *v = &ready->version;
658 struct sof_ipc_ctrl_data_params sparams;
659 size_t send_bytes;
660 int err;
662 /* read or write firmware volume */
663 if (scontrol->readback_offset != 0) {
664 /* write/read value header via mmaped region */
665 send_bytes = sizeof(struct sof_ipc_ctrl_value_chan) *
666 cdata->num_elems;
667 if (send)
668 snd_sof_dsp_block_write(sdev, sdev->mmio_bar,
669 scontrol->readback_offset,
670 cdata->chanv, send_bytes);
672 else
673 snd_sof_dsp_block_read(sdev, sdev->mmio_bar,
674 scontrol->readback_offset,
675 cdata->chanv, send_bytes);
676 return 0;
679 cdata->rhdr.hdr.cmd = SOF_IPC_GLB_COMP_MSG | ipc_cmd;
680 cdata->cmd = ctrl_cmd;
681 cdata->type = ctrl_type;
682 cdata->comp_id = scontrol->comp_id;
683 cdata->msg_index = 0;
685 /* calculate header and data size */
686 switch (cdata->type) {
687 case SOF_CTRL_TYPE_VALUE_CHAN_GET:
688 case SOF_CTRL_TYPE_VALUE_CHAN_SET:
689 sparams.msg_bytes = scontrol->num_channels *
690 sizeof(struct sof_ipc_ctrl_value_chan);
691 sparams.hdr_bytes = sizeof(struct sof_ipc_ctrl_data);
692 sparams.elems = scontrol->num_channels;
693 break;
694 case SOF_CTRL_TYPE_VALUE_COMP_GET:
695 case SOF_CTRL_TYPE_VALUE_COMP_SET:
696 sparams.msg_bytes = scontrol->num_channels *
697 sizeof(struct sof_ipc_ctrl_value_comp);
698 sparams.hdr_bytes = sizeof(struct sof_ipc_ctrl_data);
699 sparams.elems = scontrol->num_channels;
700 break;
701 case SOF_CTRL_TYPE_DATA_GET:
702 case SOF_CTRL_TYPE_DATA_SET:
703 sparams.msg_bytes = cdata->data->size;
704 sparams.hdr_bytes = sizeof(struct sof_ipc_ctrl_data) +
705 sizeof(struct sof_abi_hdr);
706 sparams.elems = cdata->data->size;
707 break;
708 default:
709 return -EINVAL;
712 cdata->rhdr.hdr.size = sparams.msg_bytes + sparams.hdr_bytes;
713 cdata->num_elems = sparams.elems;
714 cdata->elems_remaining = 0;
716 /* send normal size ipc in one part */
717 if (cdata->rhdr.hdr.size <= SOF_IPC_MSG_MAX_SIZE) {
718 err = sof_ipc_tx_message(sdev->ipc, cdata->rhdr.hdr.cmd, cdata,
719 cdata->rhdr.hdr.size, cdata,
720 cdata->rhdr.hdr.size);
722 if (err < 0)
723 dev_err(sdev->dev, "error: set/get ctrl ipc comp %d\n",
724 cdata->comp_id);
726 return err;
729 /* data is bigger than max ipc size, chop into smaller pieces */
730 dev_dbg(sdev->dev, "large ipc size %u, control size %u\n",
731 cdata->rhdr.hdr.size, scontrol->size);
733 /* large messages is only supported from ABI 3.3.0 onwards */
734 if (v->abi_version < SOF_ABI_VER(3, 3, 0)) {
735 dev_err(sdev->dev, "error: incompatible FW ABI version\n");
736 return -EINVAL;
739 err = sof_set_get_large_ctrl_data(sdev, cdata, &sparams, send);
741 if (err < 0)
742 dev_err(sdev->dev, "error: set/get large ctrl ipc comp %d\n",
743 cdata->comp_id);
745 return err;
747 EXPORT_SYMBOL(snd_sof_ipc_set_get_comp_data);
750 * IPC layer enumeration.
753 int snd_sof_dsp_mailbox_init(struct snd_sof_dev *sdev, u32 dspbox,
754 size_t dspbox_size, u32 hostbox,
755 size_t hostbox_size)
757 sdev->dsp_box.offset = dspbox;
758 sdev->dsp_box.size = dspbox_size;
759 sdev->host_box.offset = hostbox;
760 sdev->host_box.size = hostbox_size;
761 return 0;
763 EXPORT_SYMBOL(snd_sof_dsp_mailbox_init);
765 int snd_sof_ipc_valid(struct snd_sof_dev *sdev)
767 struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
768 struct sof_ipc_fw_version *v = &ready->version;
770 dev_info(sdev->dev,
771 "Firmware info: version %d:%d:%d-%s\n", v->major, v->minor,
772 v->micro, v->tag);
773 dev_info(sdev->dev,
774 "Firmware: ABI %d:%d:%d Kernel ABI %d:%d:%d\n",
775 SOF_ABI_VERSION_MAJOR(v->abi_version),
776 SOF_ABI_VERSION_MINOR(v->abi_version),
777 SOF_ABI_VERSION_PATCH(v->abi_version),
778 SOF_ABI_MAJOR, SOF_ABI_MINOR, SOF_ABI_PATCH);
780 if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, v->abi_version)) {
781 dev_err(sdev->dev, "error: incompatible FW ABI version\n");
782 return -EINVAL;
785 if (v->abi_version > SOF_ABI_VERSION) {
786 if (!IS_ENABLED(CONFIG_SND_SOC_SOF_STRICT_ABI_CHECKS)) {
787 dev_warn(sdev->dev, "warn: FW ABI is more recent than kernel\n");
788 } else {
789 dev_err(sdev->dev, "error: FW ABI is more recent than kernel\n");
790 return -EINVAL;
794 if (ready->flags & SOF_IPC_INFO_BUILD) {
795 dev_info(sdev->dev,
796 "Firmware debug build %d on %s-%s - options:\n"
797 " GDB: %s\n"
798 " lock debug: %s\n"
799 " lock vdebug: %s\n",
800 v->build, v->date, v->time,
801 (ready->flags & SOF_IPC_INFO_GDB) ?
802 "enabled" : "disabled",
803 (ready->flags & SOF_IPC_INFO_LOCKS) ?
804 "enabled" : "disabled",
805 (ready->flags & SOF_IPC_INFO_LOCKSV) ?
806 "enabled" : "disabled");
809 /* copy the fw_version into debugfs at first boot */
810 memcpy(&sdev->fw_version, v, sizeof(*v));
812 return 0;
814 EXPORT_SYMBOL(snd_sof_ipc_valid);
816 struct snd_sof_ipc *snd_sof_ipc_init(struct snd_sof_dev *sdev)
818 struct snd_sof_ipc *ipc;
819 struct snd_sof_ipc_msg *msg;
821 ipc = devm_kzalloc(sdev->dev, sizeof(*ipc), GFP_KERNEL);
822 if (!ipc)
823 return NULL;
825 mutex_init(&ipc->tx_mutex);
826 ipc->sdev = sdev;
827 msg = &ipc->msg;
829 /* indicate that we aren't sending a message ATM */
830 msg->ipc_complete = true;
832 /* pre-allocate message data */
833 msg->msg_data = devm_kzalloc(sdev->dev, SOF_IPC_MSG_MAX_SIZE,
834 GFP_KERNEL);
835 if (!msg->msg_data)
836 return NULL;
838 msg->reply_data = devm_kzalloc(sdev->dev, SOF_IPC_MSG_MAX_SIZE,
839 GFP_KERNEL);
840 if (!msg->reply_data)
841 return NULL;
843 init_waitqueue_head(&msg->waitq);
845 return ipc;
847 EXPORT_SYMBOL(snd_sof_ipc_init);
849 void snd_sof_ipc_free(struct snd_sof_dev *sdev)
851 struct snd_sof_ipc *ipc = sdev->ipc;
853 if (!ipc)
854 return;
856 /* disable sending of ipc's */
857 mutex_lock(&ipc->tx_mutex);
858 ipc->disable_ipc_tx = true;
859 mutex_unlock(&ipc->tx_mutex);
861 EXPORT_SYMBOL(snd_sof_ipc_free);