Merge series "ASoC: SOF: ipc: Small cleanups for message handler functions" from...
[linux-2.6-microblaze.git] / sound / soc / sof / ipc.c
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) 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.
12 //
13
14 #include <linux/mutex.h>
15 #include <linux/types.h>
16
17 #include "sof-priv.h"
18 #include "sof-audio.h"
19 #include "ops.h"
20
21 static void ipc_trace_message(struct snd_sof_dev *sdev, u32 msg_type);
22 static void ipc_stream_message(struct snd_sof_dev *sdev, u32 msg_cmd);
23
24 /*
25  * IPC message Tx/Rx message handling.
26  */
27
28 /* SOF generic IPC data */
29 struct snd_sof_ipc {
30         struct snd_sof_dev *sdev;
31
32         /* protects messages and the disable flag */
33         struct mutex tx_mutex;
34         /* disables further sending of ipc's */
35         bool disable_ipc_tx;
36
37         struct snd_sof_ipc_msg msg;
38 };
39
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;
48 };
49
50 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_VERBOSE_IPC)
51 static void ipc_log_header(struct device *dev, u8 *text, u32 cmd)
52 {
53         u8 *str;
54         u8 *str2 = NULL;
55         u32 glb;
56         u32 type;
57         bool vdbg = false;
58
59         glb = cmd & SOF_GLB_TYPE_MASK;
60         type = cmd & SOF_CMD_TYPE_MASK;
61
62         switch (glb) {
63         case SOF_IPC_GLB_REPLY:
64                 str = "GLB_REPLY"; break;
65         case SOF_IPC_GLB_COMPOUND:
66                 str = "GLB_COMPOUND"; break;
67         case SOF_IPC_GLB_TPLG_MSG:
68                 str = "GLB_TPLG_MSG";
69                 switch (type) {
70                 case SOF_IPC_TPLG_COMP_NEW:
71                         str2 = "COMP_NEW"; break;
72                 case SOF_IPC_TPLG_COMP_FREE:
73                         str2 = "COMP_FREE"; break;
74                 case SOF_IPC_TPLG_COMP_CONNECT:
75                         str2 = "COMP_CONNECT"; break;
76                 case SOF_IPC_TPLG_PIPE_NEW:
77                         str2 = "PIPE_NEW"; break;
78                 case SOF_IPC_TPLG_PIPE_FREE:
79                         str2 = "PIPE_FREE"; break;
80                 case SOF_IPC_TPLG_PIPE_CONNECT:
81                         str2 = "PIPE_CONNECT"; break;
82                 case SOF_IPC_TPLG_PIPE_COMPLETE:
83                         str2 = "PIPE_COMPLETE"; break;
84                 case SOF_IPC_TPLG_BUFFER_NEW:
85                         str2 = "BUFFER_NEW"; break;
86                 case SOF_IPC_TPLG_BUFFER_FREE:
87                         str2 = "BUFFER_FREE"; break;
88                 default:
89                         str2 = "unknown type"; break;
90                 }
91                 break;
92         case SOF_IPC_GLB_PM_MSG:
93                 str = "GLB_PM_MSG";
94                 switch (type) {
95                 case SOF_IPC_PM_CTX_SAVE:
96                         str2 = "CTX_SAVE"; break;
97                 case SOF_IPC_PM_CTX_RESTORE:
98                         str2 = "CTX_RESTORE"; break;
99                 case SOF_IPC_PM_CTX_SIZE:
100                         str2 = "CTX_SIZE"; break;
101                 case SOF_IPC_PM_CLK_SET:
102                         str2 = "CLK_SET"; break;
103                 case SOF_IPC_PM_CLK_GET:
104                         str2 = "CLK_GET"; break;
105                 case SOF_IPC_PM_CLK_REQ:
106                         str2 = "CLK_REQ"; break;
107                 case SOF_IPC_PM_CORE_ENABLE:
108                         str2 = "CORE_ENABLE"; break;
109                 case SOF_IPC_PM_GATE:
110                         str2 = "GATE"; break;
111                 default:
112                         str2 = "unknown type"; break;
113                 }
114                 break;
115         case SOF_IPC_GLB_COMP_MSG:
116                 str = "GLB_COMP_MSG";
117                 switch (type) {
118                 case SOF_IPC_COMP_SET_VALUE:
119                         str2 = "SET_VALUE"; break;
120                 case SOF_IPC_COMP_GET_VALUE:
121                         str2 = "GET_VALUE"; break;
122                 case SOF_IPC_COMP_SET_DATA:
123                         str2 = "SET_DATA"; break;
124                 case SOF_IPC_COMP_GET_DATA:
125                         str2 = "GET_DATA"; break;
126                 default:
127                         str2 = "unknown type"; break;
128                 }
129                 break;
130         case SOF_IPC_GLB_STREAM_MSG:
131                 str = "GLB_STREAM_MSG";
132                 switch (type) {
133                 case SOF_IPC_STREAM_PCM_PARAMS:
134                         str2 = "PCM_PARAMS"; break;
135                 case SOF_IPC_STREAM_PCM_PARAMS_REPLY:
136                         str2 = "PCM_REPLY"; break;
137                 case SOF_IPC_STREAM_PCM_FREE:
138                         str2 = "PCM_FREE"; break;
139                 case SOF_IPC_STREAM_TRIG_START:
140                         str2 = "TRIG_START"; break;
141                 case SOF_IPC_STREAM_TRIG_STOP:
142                         str2 = "TRIG_STOP"; break;
143                 case SOF_IPC_STREAM_TRIG_PAUSE:
144                         str2 = "TRIG_PAUSE"; break;
145                 case SOF_IPC_STREAM_TRIG_RELEASE:
146                         str2 = "TRIG_RELEASE"; break;
147                 case SOF_IPC_STREAM_TRIG_DRAIN:
148                         str2 = "TRIG_DRAIN"; break;
149                 case SOF_IPC_STREAM_TRIG_XRUN:
150                         str2 = "TRIG_XRUN"; break;
151                 case SOF_IPC_STREAM_POSITION:
152                         vdbg = true;
153                         str2 = "POSITION"; break;
154                 case SOF_IPC_STREAM_VORBIS_PARAMS:
155                         str2 = "VORBIS_PARAMS"; break;
156                 case SOF_IPC_STREAM_VORBIS_FREE:
157                         str2 = "VORBIS_FREE"; break;
158                 default:
159                         str2 = "unknown type"; break;
160                 }
161                 break;
162         case SOF_IPC_FW_READY:
163                 str = "FW_READY"; break;
164         case SOF_IPC_GLB_DAI_MSG:
165                 str = "GLB_DAI_MSG";
166                 switch (type) {
167                 case SOF_IPC_DAI_CONFIG:
168                         str2 = "CONFIG"; break;
169                 case SOF_IPC_DAI_LOOPBACK:
170                         str2 = "LOOPBACK"; break;
171                 default:
172                         str2 = "unknown type"; break;
173                 }
174                 break;
175         case SOF_IPC_GLB_TRACE_MSG:
176                 str = "GLB_TRACE_MSG"; break;
177         case SOF_IPC_GLB_TEST_MSG:
178                 str = "GLB_TEST_MSG";
179                 switch (type) {
180                 case SOF_IPC_TEST_IPC_FLOOD:
181                         str2 = "IPC_FLOOD"; break;
182                 default:
183                         str2 = "unknown type"; break;
184                 }
185                 break;
186         case SOF_IPC_GLB_DEBUG:
187                 str = "GLB_DEBUG";
188                 switch (type) {
189                 case SOF_IPC_DEBUG_MEM_USAGE:
190                         str2 = "MEM_USAGE"; break;
191                 default:
192                         str2 = "unknown type"; break;
193                 }
194                 break;
195         case SOF_IPC_GLB_PROBE:
196                 str = "GLB_PROBE";
197                 switch (type) {
198                 case SOF_IPC_PROBE_INIT:
199                         str2 = "INIT"; break;
200                 case SOF_IPC_PROBE_DEINIT:
201                         str2 = "DEINIT"; break;
202                 case SOF_IPC_PROBE_DMA_ADD:
203                         str2 = "DMA_ADD"; break;
204                 case SOF_IPC_PROBE_DMA_INFO:
205                         str2 = "DMA_INFO"; break;
206                 case SOF_IPC_PROBE_DMA_REMOVE:
207                         str2 = "DMA_REMOVE"; break;
208                 case SOF_IPC_PROBE_POINT_ADD:
209                         str2 = "POINT_ADD"; break;
210                 case SOF_IPC_PROBE_POINT_INFO:
211                         str2 = "POINT_INFO"; break;
212                 case SOF_IPC_PROBE_POINT_REMOVE:
213                         str2 = "POINT_REMOVE"; break;
214                 default:
215                         str2 = "unknown type"; break;
216                 }
217                 break;
218         default:
219                 str = "unknown GLB command"; break;
220         }
221
222         if (str2) {
223                 if (vdbg)
224                         dev_vdbg(dev, "%s: 0x%x: %s: %s\n", text, cmd, str, str2);
225                 else
226                         dev_dbg(dev, "%s: 0x%x: %s: %s\n", text, cmd, str, str2);
227         } else {
228                 dev_dbg(dev, "%s: 0x%x: %s\n", text, cmd, str);
229         }
230 }
231 #else
232 static inline void ipc_log_header(struct device *dev, u8 *text, u32 cmd)
233 {
234         if ((cmd & SOF_GLB_TYPE_MASK) != SOF_IPC_GLB_TRACE_MSG)
235                 dev_dbg(dev, "%s: 0x%x\n", text, cmd);
236 }
237 #endif
238
239 /* wait for IPC message reply */
240 static int tx_wait_done(struct snd_sof_ipc *ipc, struct snd_sof_ipc_msg *msg,
241                         void *reply_data)
242 {
243         struct snd_sof_dev *sdev = ipc->sdev;
244         struct sof_ipc_cmd_hdr *hdr = msg->msg_data;
245         int ret;
246
247         /* wait for DSP IPC completion */
248         ret = wait_event_timeout(msg->waitq, msg->ipc_complete,
249                                  msecs_to_jiffies(sdev->ipc_timeout));
250
251         if (ret == 0) {
252                 dev_err(sdev->dev, "error: ipc timed out for 0x%x size %d\n",
253                         hdr->cmd, hdr->size);
254                 snd_sof_handle_fw_exception(ipc->sdev);
255                 ret = -ETIMEDOUT;
256         } else {
257                 ret = msg->reply_error;
258                 if (ret < 0) {
259                         dev_err(sdev->dev, "error: ipc error for 0x%x size %zu\n",
260                                 hdr->cmd, msg->reply_size);
261                 } else {
262                         ipc_log_header(sdev->dev, "ipc tx succeeded", hdr->cmd);
263                         if (msg->reply_size)
264                                 /* copy the data returned from DSP */
265                                 memcpy(reply_data, msg->reply_data,
266                                        msg->reply_size);
267                 }
268         }
269
270         return ret;
271 }
272
273 /* send IPC message from host to DSP */
274 static int sof_ipc_tx_message_unlocked(struct snd_sof_ipc *ipc, u32 header,
275                                        void *msg_data, size_t msg_bytes,
276                                        void *reply_data, size_t reply_bytes)
277 {
278         struct snd_sof_dev *sdev = ipc->sdev;
279         struct snd_sof_ipc_msg *msg;
280         int ret;
281
282         if (ipc->disable_ipc_tx)
283                 return -ENODEV;
284
285         /*
286          * The spin-lock is also still needed to protect message objects against
287          * other atomic contexts.
288          */
289         spin_lock_irq(&sdev->ipc_lock);
290
291         /* initialise the message */
292         msg = &ipc->msg;
293
294         msg->header = header;
295         msg->msg_size = msg_bytes;
296         msg->reply_size = reply_bytes;
297         msg->reply_error = 0;
298
299         /* attach any data */
300         if (msg_bytes)
301                 memcpy(msg->msg_data, msg_data, msg_bytes);
302
303         sdev->msg = msg;
304
305         ret = snd_sof_dsp_send_msg(sdev, msg);
306         /* Next reply that we receive will be related to this message */
307         if (!ret)
308                 msg->ipc_complete = false;
309
310         spin_unlock_irq(&sdev->ipc_lock);
311
312         if (ret) {
313                 dev_err_ratelimited(sdev->dev,
314                                     "error: ipc tx failed with error %d\n",
315                                     ret);
316                 return ret;
317         }
318
319         ipc_log_header(sdev->dev, "ipc tx", msg->header);
320
321         /* now wait for completion */
322         return tx_wait_done(ipc, msg, reply_data);
323 }
324
325 /* send IPC message from host to DSP */
326 int sof_ipc_tx_message(struct snd_sof_ipc *ipc, u32 header,
327                        void *msg_data, size_t msg_bytes, void *reply_data,
328                        size_t reply_bytes)
329 {
330         const struct sof_dsp_power_state target_state = {
331                 .state = SOF_DSP_PM_D0,
332         };
333         int ret;
334
335         /* ensure the DSP is in D0 before sending a new IPC */
336         ret = snd_sof_dsp_set_power_state(ipc->sdev, &target_state);
337         if (ret < 0) {
338                 dev_err(ipc->sdev->dev, "error: resuming DSP %d\n", ret);
339                 return ret;
340         }
341
342         return sof_ipc_tx_message_no_pm(ipc, header, msg_data, msg_bytes,
343                                         reply_data, reply_bytes);
344 }
345 EXPORT_SYMBOL(sof_ipc_tx_message);
346
347 /*
348  * send IPC message from host to DSP without modifying the DSP state.
349  * This will be used for IPC's that can be handled by the DSP
350  * even in a low-power D0 substate.
351  */
352 int sof_ipc_tx_message_no_pm(struct snd_sof_ipc *ipc, u32 header,
353                              void *msg_data, size_t msg_bytes,
354                              void *reply_data, size_t reply_bytes)
355 {
356         int ret;
357
358         if (msg_bytes > SOF_IPC_MSG_MAX_SIZE ||
359             reply_bytes > SOF_IPC_MSG_MAX_SIZE)
360                 return -ENOBUFS;
361
362         /* Serialise IPC TX */
363         mutex_lock(&ipc->tx_mutex);
364
365         ret = sof_ipc_tx_message_unlocked(ipc, header, msg_data, msg_bytes,
366                                           reply_data, reply_bytes);
367
368         mutex_unlock(&ipc->tx_mutex);
369
370         return ret;
371 }
372 EXPORT_SYMBOL(sof_ipc_tx_message_no_pm);
373
374 /* handle reply message from DSP */
375 void snd_sof_ipc_reply(struct snd_sof_dev *sdev, u32 msg_id)
376 {
377         struct snd_sof_ipc_msg *msg = &sdev->ipc->msg;
378
379         if (msg->ipc_complete) {
380                 dev_dbg(sdev->dev,
381                         "no reply expected, received 0x%x, will be ignored",
382                         msg_id);
383                 return;
384         }
385
386         /* wake up and return the error if we have waiters on this message ? */
387         msg->ipc_complete = true;
388         wake_up(&msg->waitq);
389 }
390 EXPORT_SYMBOL(snd_sof_ipc_reply);
391
392 static void ipc_comp_notification(struct snd_sof_dev *sdev,
393                                   struct sof_ipc_cmd_hdr *hdr)
394 {
395         u32 msg_type = hdr->cmd & SOF_CMD_TYPE_MASK;
396         struct sof_ipc_ctrl_data *cdata;
397
398         switch (msg_type) {
399         case SOF_IPC_COMP_GET_VALUE:
400         case SOF_IPC_COMP_GET_DATA:
401                 cdata = kmalloc(hdr->size, GFP_KERNEL);
402                 if (!cdata)
403                         return;
404
405                 /* read back full message */
406                 snd_sof_ipc_msg_data(sdev, NULL, cdata, hdr->size);
407                 break;
408         default:
409                 dev_err(sdev->dev, "error: unhandled component message %#x\n", msg_type);
410                 return;
411         }
412
413         snd_sof_control_notify(sdev, cdata);
414
415         kfree(cdata);
416 }
417
418 /* DSP firmware has sent host a message  */
419 void snd_sof_ipc_msgs_rx(struct snd_sof_dev *sdev)
420 {
421         struct sof_ipc_cmd_hdr hdr;
422         u32 cmd, type;
423         int err = 0;
424
425         /* read back header */
426         snd_sof_ipc_msg_data(sdev, NULL, &hdr, sizeof(hdr));
427         ipc_log_header(sdev->dev, "ipc rx", hdr.cmd);
428
429         cmd = hdr.cmd & SOF_GLB_TYPE_MASK;
430         type = hdr.cmd & SOF_CMD_TYPE_MASK;
431
432         /* check message type */
433         switch (cmd) {
434         case SOF_IPC_GLB_REPLY:
435                 dev_err(sdev->dev, "error: ipc reply unknown\n");
436                 break;
437         case SOF_IPC_FW_READY:
438                 /* check for FW boot completion */
439                 if (sdev->fw_state == SOF_FW_BOOT_IN_PROGRESS) {
440                         err = sof_ops(sdev)->fw_ready(sdev, cmd);
441                         if (err < 0)
442                                 sdev->fw_state = SOF_FW_BOOT_READY_FAILED;
443                         else
444                                 sdev->fw_state = SOF_FW_BOOT_COMPLETE;
445
446                         /* wake up firmware loader */
447                         wake_up(&sdev->boot_wait);
448                 }
449                 break;
450         case SOF_IPC_GLB_COMPOUND:
451         case SOF_IPC_GLB_TPLG_MSG:
452         case SOF_IPC_GLB_PM_MSG:
453                 break;
454         case SOF_IPC_GLB_COMP_MSG:
455                 ipc_comp_notification(sdev, &hdr);
456                 break;
457         case SOF_IPC_GLB_STREAM_MSG:
458                 /* need to pass msg id into the function */
459                 ipc_stream_message(sdev, hdr.cmd);
460                 break;
461         case SOF_IPC_GLB_TRACE_MSG:
462                 ipc_trace_message(sdev, type);
463                 break;
464         default:
465                 dev_err(sdev->dev, "error: unknown DSP message 0x%x\n", cmd);
466                 break;
467         }
468
469         ipc_log_header(sdev->dev, "ipc rx done", hdr.cmd);
470 }
471 EXPORT_SYMBOL(snd_sof_ipc_msgs_rx);
472
473 /*
474  * IPC trace mechanism.
475  */
476
477 static void ipc_trace_message(struct snd_sof_dev *sdev, u32 msg_type)
478 {
479         struct sof_ipc_dma_trace_posn posn;
480
481         switch (msg_type) {
482         case SOF_IPC_TRACE_DMA_POSITION:
483                 /* read back full message */
484                 snd_sof_ipc_msg_data(sdev, NULL, &posn, sizeof(posn));
485                 snd_sof_trace_update_pos(sdev, &posn);
486                 break;
487         default:
488                 dev_err(sdev->dev, "error: unhandled trace message %#x\n", msg_type);
489                 break;
490         }
491 }
492
493 /*
494  * IPC stream position.
495  */
496
497 static void ipc_period_elapsed(struct snd_sof_dev *sdev, u32 msg_id)
498 {
499         struct snd_soc_component *scomp = sdev->component;
500         struct snd_sof_pcm_stream *stream;
501         struct sof_ipc_stream_posn posn;
502         struct snd_sof_pcm *spcm;
503         int direction;
504
505         spcm = snd_sof_find_spcm_comp(scomp, msg_id, &direction);
506         if (!spcm) {
507                 dev_err(sdev->dev,
508                         "error: period elapsed for unknown stream, msg_id %d\n",
509                         msg_id);
510                 return;
511         }
512
513         stream = &spcm->stream[direction];
514         snd_sof_ipc_msg_data(sdev, stream->substream, &posn, sizeof(posn));
515
516         dev_vdbg(sdev->dev, "posn : host 0x%llx dai 0x%llx wall 0x%llx\n",
517                  posn.host_posn, posn.dai_posn, posn.wallclock);
518
519         memcpy(&stream->posn, &posn, sizeof(posn));
520
521         /* only inform ALSA for period_wakeup mode */
522         if (!stream->substream->runtime->no_period_wakeup)
523                 snd_sof_pcm_period_elapsed(stream->substream);
524 }
525
526 /* DSP notifies host of an XRUN within FW */
527 static void ipc_xrun(struct snd_sof_dev *sdev, u32 msg_id)
528 {
529         struct snd_soc_component *scomp = sdev->component;
530         struct snd_sof_pcm_stream *stream;
531         struct sof_ipc_stream_posn posn;
532         struct snd_sof_pcm *spcm;
533         int direction;
534
535         spcm = snd_sof_find_spcm_comp(scomp, msg_id, &direction);
536         if (!spcm) {
537                 dev_err(sdev->dev, "error: XRUN for unknown stream, msg_id %d\n",
538                         msg_id);
539                 return;
540         }
541
542         stream = &spcm->stream[direction];
543         snd_sof_ipc_msg_data(sdev, stream->substream, &posn, sizeof(posn));
544
545         dev_dbg(sdev->dev,  "posn XRUN: host %llx comp %d size %d\n",
546                 posn.host_posn, posn.xrun_comp_id, posn.xrun_size);
547
548 #if defined(CONFIG_SND_SOC_SOF_DEBUG_XRUN_STOP)
549         /* stop PCM on XRUN - used for pipeline debug */
550         memcpy(&stream->posn, &posn, sizeof(posn));
551         snd_pcm_stop_xrun(stream->substream);
552 #endif
553 }
554
555 /* stream notifications from DSP FW */
556 static void ipc_stream_message(struct snd_sof_dev *sdev, u32 msg_cmd)
557 {
558         /* get msg cmd type and msd id */
559         u32 msg_type = msg_cmd & SOF_CMD_TYPE_MASK;
560         u32 msg_id = SOF_IPC_MESSAGE_ID(msg_cmd);
561
562         switch (msg_type) {
563         case SOF_IPC_STREAM_POSITION:
564                 ipc_period_elapsed(sdev, msg_id);
565                 break;
566         case SOF_IPC_STREAM_TRIG_XRUN:
567                 ipc_xrun(sdev, msg_id);
568                 break;
569         default:
570                 dev_err(sdev->dev, "error: unhandled stream message %#x\n",
571                         msg_id);
572                 break;
573         }
574 }
575
576 /* get stream position IPC - use faster MMIO method if available on platform */
577 int snd_sof_ipc_stream_posn(struct snd_soc_component *scomp,
578                             struct snd_sof_pcm *spcm, int direction,
579                             struct sof_ipc_stream_posn *posn)
580 {
581         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
582         struct sof_ipc_stream stream;
583         int err;
584
585         /* read position via slower IPC */
586         stream.hdr.size = sizeof(stream);
587         stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_POSITION;
588         stream.comp_id = spcm->stream[direction].comp_id;
589
590         /* send IPC to the DSP */
591         err = sof_ipc_tx_message(sdev->ipc,
592                                  stream.hdr.cmd, &stream, sizeof(stream), posn,
593                                  sizeof(*posn));
594         if (err < 0) {
595                 dev_err(sdev->dev, "error: failed to get stream %d position\n",
596                         stream.comp_id);
597                 return err;
598         }
599
600         return 0;
601 }
602 EXPORT_SYMBOL(snd_sof_ipc_stream_posn);
603
604 static int sof_get_ctrl_copy_params(enum sof_ipc_ctrl_type ctrl_type,
605                                     struct sof_ipc_ctrl_data *src,
606                                     struct sof_ipc_ctrl_data *dst,
607                                     struct sof_ipc_ctrl_data_params *sparams)
608 {
609         switch (ctrl_type) {
610         case SOF_CTRL_TYPE_VALUE_CHAN_GET:
611         case SOF_CTRL_TYPE_VALUE_CHAN_SET:
612                 sparams->src = (u8 *)src->chanv;
613                 sparams->dst = (u8 *)dst->chanv;
614                 break;
615         case SOF_CTRL_TYPE_VALUE_COMP_GET:
616         case SOF_CTRL_TYPE_VALUE_COMP_SET:
617                 sparams->src = (u8 *)src->compv;
618                 sparams->dst = (u8 *)dst->compv;
619                 break;
620         case SOF_CTRL_TYPE_DATA_GET:
621         case SOF_CTRL_TYPE_DATA_SET:
622                 sparams->src = (u8 *)src->data->data;
623                 sparams->dst = (u8 *)dst->data->data;
624                 break;
625         default:
626                 return -EINVAL;
627         }
628
629         /* calculate payload size and number of messages */
630         sparams->pl_size = SOF_IPC_MSG_MAX_SIZE - sparams->hdr_bytes;
631         sparams->num_msg = DIV_ROUND_UP(sparams->msg_bytes, sparams->pl_size);
632
633         return 0;
634 }
635
636 static int sof_set_get_large_ctrl_data(struct snd_sof_dev *sdev,
637                                        struct sof_ipc_ctrl_data *cdata,
638                                        struct sof_ipc_ctrl_data_params *sparams,
639                                        bool send)
640 {
641         struct sof_ipc_ctrl_data *partdata;
642         size_t send_bytes;
643         size_t offset = 0;
644         size_t msg_bytes;
645         size_t pl_size;
646         int err;
647         int i;
648
649         /* allocate max ipc size because we have at least one */
650         partdata = kzalloc(SOF_IPC_MSG_MAX_SIZE, GFP_KERNEL);
651         if (!partdata)
652                 return -ENOMEM;
653
654         if (send)
655                 err = sof_get_ctrl_copy_params(cdata->type, cdata, partdata,
656                                                sparams);
657         else
658                 err = sof_get_ctrl_copy_params(cdata->type, partdata, cdata,
659                                                sparams);
660         if (err < 0) {
661                 kfree(partdata);
662                 return err;
663         }
664
665         msg_bytes = sparams->msg_bytes;
666         pl_size = sparams->pl_size;
667
668         /* copy the header data */
669         memcpy(partdata, cdata, sparams->hdr_bytes);
670
671         /* Serialise IPC TX */
672         mutex_lock(&sdev->ipc->tx_mutex);
673
674         /* copy the payload data in a loop */
675         for (i = 0; i < sparams->num_msg; i++) {
676                 send_bytes = min(msg_bytes, pl_size);
677                 partdata->num_elems = send_bytes;
678                 partdata->rhdr.hdr.size = sparams->hdr_bytes + send_bytes;
679                 partdata->msg_index = i;
680                 msg_bytes -= send_bytes;
681                 partdata->elems_remaining = msg_bytes;
682
683                 if (send)
684                         memcpy(sparams->dst, sparams->src + offset, send_bytes);
685
686                 err = sof_ipc_tx_message_unlocked(sdev->ipc,
687                                                   partdata->rhdr.hdr.cmd,
688                                                   partdata,
689                                                   partdata->rhdr.hdr.size,
690                                                   partdata,
691                                                   partdata->rhdr.hdr.size);
692                 if (err < 0)
693                         break;
694
695                 if (!send)
696                         memcpy(sparams->dst + offset, sparams->src, send_bytes);
697
698                 offset += pl_size;
699         }
700
701         mutex_unlock(&sdev->ipc->tx_mutex);
702
703         kfree(partdata);
704         return err;
705 }
706
707 /*
708  * IPC get()/set() for kcontrols.
709  */
710 int snd_sof_ipc_set_get_comp_data(struct snd_sof_control *scontrol,
711                                   u32 ipc_cmd,
712                                   enum sof_ipc_ctrl_type ctrl_type,
713                                   enum sof_ipc_ctrl_cmd ctrl_cmd,
714                                   bool send)
715 {
716         struct snd_soc_component *scomp = scontrol->scomp;
717         struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
718         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
719         struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
720         struct sof_ipc_fw_version *v = &ready->version;
721         struct sof_ipc_ctrl_data_params sparams;
722         size_t send_bytes;
723         int err;
724
725         /* read or write firmware volume */
726         if (scontrol->readback_offset != 0) {
727                 /* write/read value header via mmaped region */
728                 send_bytes = sizeof(struct sof_ipc_ctrl_value_chan) *
729                 cdata->num_elems;
730                 if (send)
731                         err = snd_sof_dsp_block_write(sdev, SOF_FW_BLK_TYPE_IRAM,
732                                                       scontrol->readback_offset,
733                                                       cdata->chanv, send_bytes);
734
735                 else
736                         err = snd_sof_dsp_block_read(sdev, SOF_FW_BLK_TYPE_IRAM,
737                                                      scontrol->readback_offset,
738                                                      cdata->chanv, send_bytes);
739
740                 if (err)
741                         dev_err_once(sdev->dev, "error: %s TYPE_IRAM failed\n",
742                                      send ? "write to" :  "read from");
743                 return err;
744         }
745
746         cdata->rhdr.hdr.cmd = SOF_IPC_GLB_COMP_MSG | ipc_cmd;
747         cdata->cmd = ctrl_cmd;
748         cdata->type = ctrl_type;
749         cdata->comp_id = scontrol->comp_id;
750         cdata->msg_index = 0;
751
752         /* calculate header and data size */
753         switch (cdata->type) {
754         case SOF_CTRL_TYPE_VALUE_CHAN_GET:
755         case SOF_CTRL_TYPE_VALUE_CHAN_SET:
756                 sparams.msg_bytes = scontrol->num_channels *
757                         sizeof(struct sof_ipc_ctrl_value_chan);
758                 sparams.hdr_bytes = sizeof(struct sof_ipc_ctrl_data);
759                 sparams.elems = scontrol->num_channels;
760                 break;
761         case SOF_CTRL_TYPE_VALUE_COMP_GET:
762         case SOF_CTRL_TYPE_VALUE_COMP_SET:
763                 sparams.msg_bytes = scontrol->num_channels *
764                         sizeof(struct sof_ipc_ctrl_value_comp);
765                 sparams.hdr_bytes = sizeof(struct sof_ipc_ctrl_data);
766                 sparams.elems = scontrol->num_channels;
767                 break;
768         case SOF_CTRL_TYPE_DATA_GET:
769         case SOF_CTRL_TYPE_DATA_SET:
770                 sparams.msg_bytes = cdata->data->size;
771                 sparams.hdr_bytes = sizeof(struct sof_ipc_ctrl_data) +
772                         sizeof(struct sof_abi_hdr);
773                 sparams.elems = cdata->data->size;
774                 break;
775         default:
776                 return -EINVAL;
777         }
778
779         cdata->rhdr.hdr.size = sparams.msg_bytes + sparams.hdr_bytes;
780         cdata->num_elems = sparams.elems;
781         cdata->elems_remaining = 0;
782
783         /* send normal size ipc in one part */
784         if (cdata->rhdr.hdr.size <= SOF_IPC_MSG_MAX_SIZE) {
785                 err = sof_ipc_tx_message(sdev->ipc, cdata->rhdr.hdr.cmd, cdata,
786                                          cdata->rhdr.hdr.size, cdata,
787                                          cdata->rhdr.hdr.size);
788
789                 if (err < 0)
790                         dev_err(sdev->dev, "error: set/get ctrl ipc comp %d\n",
791                                 cdata->comp_id);
792
793                 return err;
794         }
795
796         /* data is bigger than max ipc size, chop into smaller pieces */
797         dev_dbg(sdev->dev, "large ipc size %u, control size %u\n",
798                 cdata->rhdr.hdr.size, scontrol->size);
799
800         /* large messages is only supported from ABI 3.3.0 onwards */
801         if (v->abi_version < SOF_ABI_VER(3, 3, 0)) {
802                 dev_err(sdev->dev, "error: incompatible FW ABI version\n");
803                 return -EINVAL;
804         }
805
806         err = sof_set_get_large_ctrl_data(sdev, cdata, &sparams, send);
807
808         if (err < 0)
809                 dev_err(sdev->dev, "error: set/get large ctrl ipc comp %d\n",
810                         cdata->comp_id);
811
812         return err;
813 }
814 EXPORT_SYMBOL(snd_sof_ipc_set_get_comp_data);
815
816 int snd_sof_ipc_valid(struct snd_sof_dev *sdev)
817 {
818         struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
819         struct sof_ipc_fw_version *v = &ready->version;
820
821         dev_info(sdev->dev,
822                  "Firmware info: version %d:%d:%d-%s\n",  v->major, v->minor,
823                  v->micro, v->tag);
824         dev_info(sdev->dev,
825                  "Firmware: ABI %d:%d:%d Kernel ABI %d:%d:%d\n",
826                  SOF_ABI_VERSION_MAJOR(v->abi_version),
827                  SOF_ABI_VERSION_MINOR(v->abi_version),
828                  SOF_ABI_VERSION_PATCH(v->abi_version),
829                  SOF_ABI_MAJOR, SOF_ABI_MINOR, SOF_ABI_PATCH);
830
831         if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, v->abi_version)) {
832                 dev_err(sdev->dev, "error: incompatible FW ABI version\n");
833                 return -EINVAL;
834         }
835
836         if (SOF_ABI_VERSION_MINOR(v->abi_version) > SOF_ABI_MINOR) {
837                 if (!IS_ENABLED(CONFIG_SND_SOC_SOF_STRICT_ABI_CHECKS)) {
838                         dev_warn(sdev->dev, "warn: FW ABI is more recent than kernel\n");
839                 } else {
840                         dev_err(sdev->dev, "error: FW ABI is more recent than kernel\n");
841                         return -EINVAL;
842                 }
843         }
844
845         if (ready->flags & SOF_IPC_INFO_BUILD) {
846                 dev_info(sdev->dev,
847                          "Firmware debug build %d on %s-%s - options:\n"
848                          " GDB: %s\n"
849                          " lock debug: %s\n"
850                          " lock vdebug: %s\n",
851                          v->build, v->date, v->time,
852                          (ready->flags & SOF_IPC_INFO_GDB) ?
853                                 "enabled" : "disabled",
854                          (ready->flags & SOF_IPC_INFO_LOCKS) ?
855                                 "enabled" : "disabled",
856                          (ready->flags & SOF_IPC_INFO_LOCKSV) ?
857                                 "enabled" : "disabled");
858         }
859
860         /* copy the fw_version into debugfs at first boot */
861         memcpy(&sdev->fw_version, v, sizeof(*v));
862
863         return 0;
864 }
865 EXPORT_SYMBOL(snd_sof_ipc_valid);
866
867 struct snd_sof_ipc *snd_sof_ipc_init(struct snd_sof_dev *sdev)
868 {
869         struct snd_sof_ipc *ipc;
870         struct snd_sof_ipc_msg *msg;
871
872         ipc = devm_kzalloc(sdev->dev, sizeof(*ipc), GFP_KERNEL);
873         if (!ipc)
874                 return NULL;
875
876         mutex_init(&ipc->tx_mutex);
877         ipc->sdev = sdev;
878         msg = &ipc->msg;
879
880         /* indicate that we aren't sending a message ATM */
881         msg->ipc_complete = true;
882
883         /* pre-allocate message data */
884         msg->msg_data = devm_kzalloc(sdev->dev, SOF_IPC_MSG_MAX_SIZE,
885                                      GFP_KERNEL);
886         if (!msg->msg_data)
887                 return NULL;
888
889         msg->reply_data = devm_kzalloc(sdev->dev, SOF_IPC_MSG_MAX_SIZE,
890                                        GFP_KERNEL);
891         if (!msg->reply_data)
892                 return NULL;
893
894         init_waitqueue_head(&msg->waitq);
895
896         return ipc;
897 }
898 EXPORT_SYMBOL(snd_sof_ipc_init);
899
900 void snd_sof_ipc_free(struct snd_sof_dev *sdev)
901 {
902         struct snd_sof_ipc *ipc = sdev->ipc;
903
904         if (!ipc)
905                 return;
906
907         /* disable sending of ipc's */
908         mutex_lock(&ipc->tx_mutex);
909         ipc->disable_ipc_tx = true;
910         mutex_unlock(&ipc->tx_mutex);
911 }
912 EXPORT_SYMBOL(snd_sof_ipc_free);