root/sound/soc/sof/ipc.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ipc_log_header
  2. ipc_log_header
  3. tx_wait_done
  4. sof_ipc_tx_message_unlocked
  5. sof_ipc_tx_message
  6. snd_sof_ipc_reply
  7. snd_sof_ipc_msgs_rx
  8. ipc_trace_message
  9. ipc_period_elapsed
  10. ipc_xrun
  11. ipc_stream_message
  12. snd_sof_ipc_stream_posn
  13. sof_get_ctrl_copy_params
  14. sof_set_get_large_ctrl_data
  15. snd_sof_ipc_set_get_comp_data
  16. snd_sof_dsp_mailbox_init
  17. snd_sof_ipc_valid
  18. snd_sof_ipc_init
  19. snd_sof_ipc_free

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

/* [<][>][^][v][top][bottom][index][help] */