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 <[email protected]> 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 #include "ipc3-ops.h" 21 22 typedef void (*ipc_rx_callback)(struct snd_sof_dev *sdev, void *msg_buf); 23 24 static void ipc_trace_message(struct snd_sof_dev *sdev, void *msg_buf); 25 static void ipc_stream_message(struct snd_sof_dev *sdev, void *msg_buf); 26 27 /* 28 * IPC message Tx/Rx message handling. 29 */ 30 31 struct sof_ipc_ctrl_data_params { 32 size_t msg_bytes; 33 size_t hdr_bytes; 34 size_t pl_size; 35 size_t elems; 36 u32 num_msg; 37 u8 *src; 38 u8 *dst; 39 }; 40 41 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_VERBOSE_IPC) 42 static void ipc_log_header(struct device *dev, u8 *text, u32 cmd) 43 { 44 u8 *str; 45 u8 *str2 = NULL; 46 u32 glb; 47 u32 type; 48 bool vdbg = false; 49 50 glb = cmd & SOF_GLB_TYPE_MASK; 51 type = cmd & SOF_CMD_TYPE_MASK; 52 53 switch (glb) { 54 case SOF_IPC_GLB_REPLY: 55 str = "GLB_REPLY"; break; 56 case SOF_IPC_GLB_COMPOUND: 57 str = "GLB_COMPOUND"; break; 58 case SOF_IPC_GLB_TPLG_MSG: 59 str = "GLB_TPLG_MSG"; 60 switch (type) { 61 case SOF_IPC_TPLG_COMP_NEW: 62 str2 = "COMP_NEW"; break; 63 case SOF_IPC_TPLG_COMP_FREE: 64 str2 = "COMP_FREE"; break; 65 case SOF_IPC_TPLG_COMP_CONNECT: 66 str2 = "COMP_CONNECT"; break; 67 case SOF_IPC_TPLG_PIPE_NEW: 68 str2 = "PIPE_NEW"; break; 69 case SOF_IPC_TPLG_PIPE_FREE: 70 str2 = "PIPE_FREE"; break; 71 case SOF_IPC_TPLG_PIPE_CONNECT: 72 str2 = "PIPE_CONNECT"; break; 73 case SOF_IPC_TPLG_PIPE_COMPLETE: 74 str2 = "PIPE_COMPLETE"; break; 75 case SOF_IPC_TPLG_BUFFER_NEW: 76 str2 = "BUFFER_NEW"; break; 77 case SOF_IPC_TPLG_BUFFER_FREE: 78 str2 = "BUFFER_FREE"; break; 79 default: 80 str2 = "unknown type"; break; 81 } 82 break; 83 case SOF_IPC_GLB_PM_MSG: 84 str = "GLB_PM_MSG"; 85 switch (type) { 86 case SOF_IPC_PM_CTX_SAVE: 87 str2 = "CTX_SAVE"; break; 88 case SOF_IPC_PM_CTX_RESTORE: 89 str2 = "CTX_RESTORE"; break; 90 case SOF_IPC_PM_CTX_SIZE: 91 str2 = "CTX_SIZE"; break; 92 case SOF_IPC_PM_CLK_SET: 93 str2 = "CLK_SET"; break; 94 case SOF_IPC_PM_CLK_GET: 95 str2 = "CLK_GET"; break; 96 case SOF_IPC_PM_CLK_REQ: 97 str2 = "CLK_REQ"; break; 98 case SOF_IPC_PM_CORE_ENABLE: 99 str2 = "CORE_ENABLE"; break; 100 case SOF_IPC_PM_GATE: 101 str2 = "GATE"; break; 102 default: 103 str2 = "unknown type"; break; 104 } 105 break; 106 case SOF_IPC_GLB_COMP_MSG: 107 str = "GLB_COMP_MSG"; 108 switch (type) { 109 case SOF_IPC_COMP_SET_VALUE: 110 str2 = "SET_VALUE"; break; 111 case SOF_IPC_COMP_GET_VALUE: 112 str2 = "GET_VALUE"; break; 113 case SOF_IPC_COMP_SET_DATA: 114 str2 = "SET_DATA"; break; 115 case SOF_IPC_COMP_GET_DATA: 116 str2 = "GET_DATA"; break; 117 default: 118 str2 = "unknown type"; break; 119 } 120 break; 121 case SOF_IPC_GLB_STREAM_MSG: 122 str = "GLB_STREAM_MSG"; 123 switch (type) { 124 case SOF_IPC_STREAM_PCM_PARAMS: 125 str2 = "PCM_PARAMS"; break; 126 case SOF_IPC_STREAM_PCM_PARAMS_REPLY: 127 str2 = "PCM_REPLY"; break; 128 case SOF_IPC_STREAM_PCM_FREE: 129 str2 = "PCM_FREE"; break; 130 case SOF_IPC_STREAM_TRIG_START: 131 str2 = "TRIG_START"; break; 132 case SOF_IPC_STREAM_TRIG_STOP: 133 str2 = "TRIG_STOP"; break; 134 case SOF_IPC_STREAM_TRIG_PAUSE: 135 str2 = "TRIG_PAUSE"; break; 136 case SOF_IPC_STREAM_TRIG_RELEASE: 137 str2 = "TRIG_RELEASE"; break; 138 case SOF_IPC_STREAM_TRIG_DRAIN: 139 str2 = "TRIG_DRAIN"; break; 140 case SOF_IPC_STREAM_TRIG_XRUN: 141 str2 = "TRIG_XRUN"; break; 142 case SOF_IPC_STREAM_POSITION: 143 vdbg = true; 144 str2 = "POSITION"; break; 145 case SOF_IPC_STREAM_VORBIS_PARAMS: 146 str2 = "VORBIS_PARAMS"; break; 147 case SOF_IPC_STREAM_VORBIS_FREE: 148 str2 = "VORBIS_FREE"; break; 149 default: 150 str2 = "unknown type"; break; 151 } 152 break; 153 case SOF_IPC_FW_READY: 154 str = "FW_READY"; break; 155 case SOF_IPC_GLB_DAI_MSG: 156 str = "GLB_DAI_MSG"; 157 switch (type) { 158 case SOF_IPC_DAI_CONFIG: 159 str2 = "CONFIG"; break; 160 case SOF_IPC_DAI_LOOPBACK: 161 str2 = "LOOPBACK"; break; 162 default: 163 str2 = "unknown type"; break; 164 } 165 break; 166 case SOF_IPC_GLB_TRACE_MSG: 167 str = "GLB_TRACE_MSG"; 168 switch (type) { 169 case SOF_IPC_TRACE_DMA_PARAMS: 170 str2 = "DMA_PARAMS"; break; 171 case SOF_IPC_TRACE_DMA_POSITION: 172 str2 = "DMA_POSITION"; break; 173 case SOF_IPC_TRACE_DMA_PARAMS_EXT: 174 str2 = "DMA_PARAMS_EXT"; break; 175 case SOF_IPC_TRACE_FILTER_UPDATE: 176 str2 = "FILTER_UPDATE"; break; 177 case SOF_IPC_TRACE_DMA_FREE: 178 str2 = "DMA_FREE"; break; 179 default: 180 str2 = "unknown type"; break; 181 } 182 break; 183 case SOF_IPC_GLB_TEST_MSG: 184 str = "GLB_TEST_MSG"; 185 switch (type) { 186 case SOF_IPC_TEST_IPC_FLOOD: 187 str2 = "IPC_FLOOD"; break; 188 default: 189 str2 = "unknown type"; break; 190 } 191 break; 192 case SOF_IPC_GLB_DEBUG: 193 str = "GLB_DEBUG"; 194 switch (type) { 195 case SOF_IPC_DEBUG_MEM_USAGE: 196 str2 = "MEM_USAGE"; break; 197 default: 198 str2 = "unknown type"; break; 199 } 200 break; 201 case SOF_IPC_GLB_PROBE: 202 str = "GLB_PROBE"; 203 switch (type) { 204 case SOF_IPC_PROBE_INIT: 205 str2 = "INIT"; break; 206 case SOF_IPC_PROBE_DEINIT: 207 str2 = "DEINIT"; break; 208 case SOF_IPC_PROBE_DMA_ADD: 209 str2 = "DMA_ADD"; break; 210 case SOF_IPC_PROBE_DMA_INFO: 211 str2 = "DMA_INFO"; break; 212 case SOF_IPC_PROBE_DMA_REMOVE: 213 str2 = "DMA_REMOVE"; break; 214 case SOF_IPC_PROBE_POINT_ADD: 215 str2 = "POINT_ADD"; break; 216 case SOF_IPC_PROBE_POINT_INFO: 217 str2 = "POINT_INFO"; break; 218 case SOF_IPC_PROBE_POINT_REMOVE: 219 str2 = "POINT_REMOVE"; break; 220 default: 221 str2 = "unknown type"; break; 222 } 223 break; 224 default: 225 str = "unknown GLB command"; break; 226 } 227 228 if (str2) { 229 if (vdbg) 230 dev_vdbg(dev, "%s: 0x%x: %s: %s\n", text, cmd, str, str2); 231 else 232 dev_dbg(dev, "%s: 0x%x: %s: %s\n", text, cmd, str, str2); 233 } else { 234 dev_dbg(dev, "%s: 0x%x: %s\n", text, cmd, str); 235 } 236 } 237 #else 238 static inline void ipc_log_header(struct device *dev, u8 *text, u32 cmd) 239 { 240 if ((cmd & SOF_GLB_TYPE_MASK) != SOF_IPC_GLB_TRACE_MSG) 241 dev_dbg(dev, "%s: 0x%x\n", text, cmd); 242 } 243 #endif 244 245 /* wait for IPC message reply */ 246 static int tx_wait_done(struct snd_sof_ipc *ipc, struct snd_sof_ipc_msg *msg, 247 void *reply_data) 248 { 249 struct snd_sof_dev *sdev = ipc->sdev; 250 struct sof_ipc_cmd_hdr *hdr = msg->msg_data; 251 int ret; 252 253 /* wait for DSP IPC completion */ 254 ret = wait_event_timeout(msg->waitq, msg->ipc_complete, 255 msecs_to_jiffies(sdev->ipc_timeout)); 256 257 if (ret == 0) { 258 dev_err(sdev->dev, 259 "ipc tx timed out for %#x (msg/reply size: %d/%zu)\n", 260 hdr->cmd, hdr->size, msg->reply_size); 261 snd_sof_handle_fw_exception(ipc->sdev); 262 ret = -ETIMEDOUT; 263 } else { 264 ret = msg->reply_error; 265 if (ret < 0) { 266 dev_err(sdev->dev, 267 "ipc tx error for %#x (msg/reply size: %d/%zu): %d\n", 268 hdr->cmd, hdr->size, msg->reply_size, ret); 269 } else { 270 ipc_log_header(sdev->dev, "ipc tx succeeded", hdr->cmd); 271 if (msg->reply_size) 272 /* copy the data returned from DSP */ 273 memcpy(reply_data, msg->reply_data, 274 msg->reply_size); 275 } 276 277 /* re-enable dumps after successful IPC tx */ 278 if (sdev->ipc_dump_printed) { 279 sdev->dbg_dump_printed = false; 280 sdev->ipc_dump_printed = false; 281 } 282 } 283 284 return ret; 285 } 286 287 /* send IPC message from host to DSP */ 288 static int sof_ipc_tx_message_unlocked(struct snd_sof_ipc *ipc, 289 void *msg_data, size_t msg_bytes, 290 void *reply_data, size_t reply_bytes) 291 { 292 struct sof_ipc_cmd_hdr *hdr = msg_data; 293 struct snd_sof_dev *sdev = ipc->sdev; 294 struct snd_sof_ipc_msg *msg; 295 int ret; 296 297 if (!msg_data || msg_bytes < sizeof(*hdr)) { 298 dev_err_ratelimited(sdev->dev, "No IPC message to send\n"); 299 return -EINVAL; 300 } 301 302 if (ipc->disable_ipc_tx || sdev->fw_state != SOF_FW_BOOT_COMPLETE) 303 return -ENODEV; 304 305 /* 306 * The spin-lock is also still needed to protect message objects against 307 * other atomic contexts. 308 */ 309 spin_lock_irq(&sdev->ipc_lock); 310 311 /* initialise the message */ 312 msg = &ipc->msg; 313 314 /* attach message data */ 315 msg->msg_data = msg_data; 316 msg->msg_size = msg_bytes; 317 318 msg->reply_size = reply_bytes; 319 msg->reply_error = 0; 320 321 sdev->msg = msg; 322 323 ret = snd_sof_dsp_send_msg(sdev, msg); 324 /* Next reply that we receive will be related to this message */ 325 if (!ret) 326 msg->ipc_complete = false; 327 328 spin_unlock_irq(&sdev->ipc_lock); 329 330 if (ret) { 331 dev_err_ratelimited(sdev->dev, 332 "error: ipc tx failed with error %d\n", 333 ret); 334 return ret; 335 } 336 337 ipc_log_header(sdev->dev, "ipc tx", hdr->cmd); 338 339 /* now wait for completion */ 340 return tx_wait_done(ipc, msg, reply_data); 341 } 342 343 /** 344 * sof_ipc_send_msg - generic function to prepare and send one IPC message 345 * @sdev: pointer to SOF core device struct 346 * @msg_data: pointer to a message to send 347 * @msg_bytes: number of bytes in the message 348 * @reply_bytes: number of bytes available for the reply. 349 * The buffer for the reply data is not passed to this 350 * function, the available size is an information for the 351 * reply handling functions. 352 * 353 * On success the function returns 0, otherwise negative error number. 354 * 355 * Note: higher level sdev->ipc->tx_mutex must be held to make sure that 356 * transfers are synchronized. 357 */ 358 int sof_ipc_send_msg(struct snd_sof_dev *sdev, void *msg_data, size_t msg_bytes, 359 size_t reply_bytes) 360 { 361 struct snd_sof_ipc *ipc = sdev->ipc; 362 struct snd_sof_ipc_msg *msg; 363 int ret; 364 365 if (ipc->disable_ipc_tx || sdev->fw_state != SOF_FW_BOOT_COMPLETE) 366 return -ENODEV; 367 368 /* 369 * The spin-lock is needed to protect message objects against other 370 * atomic contexts. 371 */ 372 spin_lock_irq(&sdev->ipc_lock); 373 374 /* initialise the message */ 375 msg = &ipc->msg; 376 377 /* attach message data */ 378 msg->msg_data = msg_data; 379 msg->msg_size = msg_bytes; 380 381 msg->reply_size = reply_bytes; 382 msg->reply_error = 0; 383 384 sdev->msg = msg; 385 386 ret = snd_sof_dsp_send_msg(sdev, msg); 387 /* Next reply that we receive will be related to this message */ 388 if (!ret) 389 msg->ipc_complete = false; 390 391 spin_unlock_irq(&sdev->ipc_lock); 392 393 return ret; 394 } 395 396 /* send IPC message from host to DSP */ 397 int sof_ipc_tx_message(struct snd_sof_ipc *ipc, void *msg_data, size_t msg_bytes, 398 void *reply_data, size_t reply_bytes) 399 { 400 const struct sof_dsp_power_state target_state = { 401 .state = SOF_DSP_PM_D0, 402 }; 403 int ret; 404 405 /* ensure the DSP is in D0 before sending a new IPC */ 406 ret = snd_sof_dsp_set_power_state(ipc->sdev, &target_state); 407 if (ret < 0) { 408 dev_err(ipc->sdev->dev, "error: resuming DSP %d\n", ret); 409 return ret; 410 } 411 412 return sof_ipc_tx_message_no_pm(ipc, msg_data, msg_bytes, 413 reply_data, reply_bytes); 414 } 415 EXPORT_SYMBOL(sof_ipc_tx_message); 416 417 /* 418 * send IPC message from host to DSP without modifying the DSP state. 419 * This will be used for IPC's that can be handled by the DSP 420 * even in a low-power D0 substate. 421 */ 422 int sof_ipc_tx_message_no_pm(struct snd_sof_ipc *ipc, void *msg_data, size_t msg_bytes, 423 void *reply_data, size_t reply_bytes) 424 { 425 int ret; 426 427 if (msg_bytes > ipc->max_payload_size || 428 reply_bytes > ipc->max_payload_size) 429 return -ENOBUFS; 430 431 /* Serialise IPC TX */ 432 mutex_lock(&ipc->tx_mutex); 433 434 ret = sof_ipc_tx_message_unlocked(ipc, msg_data, msg_bytes, 435 reply_data, reply_bytes); 436 437 mutex_unlock(&ipc->tx_mutex); 438 439 return ret; 440 } 441 EXPORT_SYMBOL(sof_ipc_tx_message_no_pm); 442 443 /* Generic helper function to retrieve the reply */ 444 void snd_sof_ipc_get_reply(struct snd_sof_dev *sdev) 445 { 446 struct snd_sof_ipc_msg *msg = sdev->msg; 447 struct sof_ipc_reply *reply; 448 int ret = 0; 449 450 /* 451 * Sometimes, there is unexpected reply ipc arriving. The reply 452 * ipc belongs to none of the ipcs sent from driver. 453 * In this case, the driver must ignore the ipc. 454 */ 455 if (!msg) { 456 dev_warn(sdev->dev, "unexpected ipc interrupt raised!\n"); 457 return; 458 } 459 460 /* get the generic reply */ 461 reply = msg->reply_data; 462 snd_sof_dsp_mailbox_read(sdev, sdev->host_box.offset, reply, sizeof(*reply)); 463 464 if (reply->error < 0) { 465 ret = reply->error; 466 } else if (!reply->hdr.size) { 467 /* Reply should always be >= sizeof(struct sof_ipc_reply) */ 468 if (msg->reply_size) 469 dev_err(sdev->dev, 470 "empty reply received, expected %zu bytes\n", 471 msg->reply_size); 472 else 473 dev_err(sdev->dev, "empty reply received\n"); 474 475 ret = -EINVAL; 476 } else if (msg->reply_size > 0) { 477 if (reply->hdr.size == msg->reply_size) { 478 ret = 0; 479 } else if (reply->hdr.size < msg->reply_size) { 480 dev_dbg(sdev->dev, 481 "reply size (%u) is less than expected (%zu)\n", 482 reply->hdr.size, msg->reply_size); 483 484 msg->reply_size = reply->hdr.size; 485 ret = 0; 486 } else { 487 dev_err(sdev->dev, 488 "reply size (%u) exceeds the buffer size (%zu)\n", 489 reply->hdr.size, msg->reply_size); 490 ret = -EINVAL; 491 } 492 493 /* 494 * get the full message if reply->hdr.size <= msg->reply_size 495 * and the reply->hdr.size > sizeof(struct sof_ipc_reply) 496 */ 497 if (!ret && msg->reply_size > sizeof(*reply)) 498 snd_sof_dsp_mailbox_read(sdev, sdev->host_box.offset, 499 msg->reply_data, msg->reply_size); 500 } 501 502 msg->reply_error = ret; 503 } 504 EXPORT_SYMBOL(snd_sof_ipc_get_reply); 505 506 /* handle reply message from DSP */ 507 void snd_sof_ipc_reply(struct snd_sof_dev *sdev, u32 msg_id) 508 { 509 struct snd_sof_ipc_msg *msg = &sdev->ipc->msg; 510 511 if (msg->ipc_complete) { 512 dev_dbg(sdev->dev, 513 "no reply expected, received 0x%x, will be ignored", 514 msg_id); 515 return; 516 } 517 518 /* wake up and return the error if we have waiters on this message ? */ 519 msg->ipc_complete = true; 520 wake_up(&msg->waitq); 521 } 522 EXPORT_SYMBOL(snd_sof_ipc_reply); 523 524 static void ipc_comp_notification(struct snd_sof_dev *sdev, void *msg_buf) 525 { 526 const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg; 527 struct sof_ipc_cmd_hdr *hdr = msg_buf; 528 u32 msg_type = hdr->cmd & SOF_CMD_TYPE_MASK; 529 530 switch (msg_type) { 531 case SOF_IPC_COMP_GET_VALUE: 532 case SOF_IPC_COMP_GET_DATA: 533 break; 534 default: 535 dev_err(sdev->dev, "error: unhandled component message %#x\n", msg_type); 536 return; 537 } 538 539 if (tplg_ops->control->update) 540 tplg_ops->control->update(sdev, msg_buf); 541 } 542 543 /* DSP firmware has sent host a message */ 544 void snd_sof_ipc_msgs_rx(struct snd_sof_dev *sdev) 545 { 546 ipc_rx_callback rx_callback = NULL; 547 struct sof_ipc_cmd_hdr hdr; 548 void *msg_buf; 549 u32 cmd; 550 int err; 551 552 /* read back header */ 553 err = snd_sof_ipc_msg_data(sdev, NULL, &hdr, sizeof(hdr)); 554 if (err < 0) { 555 dev_warn(sdev->dev, "failed to read IPC header: %d\n", err); 556 return; 557 } 558 559 if (hdr.size < sizeof(hdr)) { 560 dev_err(sdev->dev, "The received message size is invalid\n"); 561 return; 562 } 563 564 ipc_log_header(sdev->dev, "ipc rx", hdr.cmd); 565 566 cmd = hdr.cmd & SOF_GLB_TYPE_MASK; 567 568 /* check message type */ 569 switch (cmd) { 570 case SOF_IPC_GLB_REPLY: 571 dev_err(sdev->dev, "error: ipc reply unknown\n"); 572 break; 573 case SOF_IPC_FW_READY: 574 /* check for FW boot completion */ 575 if (sdev->fw_state == SOF_FW_BOOT_IN_PROGRESS) { 576 err = sof_ops(sdev)->fw_ready(sdev, cmd); 577 if (err < 0) 578 sof_set_fw_state(sdev, SOF_FW_BOOT_READY_FAILED); 579 else 580 sof_set_fw_state(sdev, SOF_FW_BOOT_READY_OK); 581 582 /* wake up firmware loader */ 583 wake_up(&sdev->boot_wait); 584 } 585 break; 586 case SOF_IPC_GLB_COMPOUND: 587 case SOF_IPC_GLB_TPLG_MSG: 588 case SOF_IPC_GLB_PM_MSG: 589 break; 590 case SOF_IPC_GLB_COMP_MSG: 591 rx_callback = ipc_comp_notification; 592 break; 593 case SOF_IPC_GLB_STREAM_MSG: 594 rx_callback = ipc_stream_message; 595 break; 596 case SOF_IPC_GLB_TRACE_MSG: 597 rx_callback = ipc_trace_message; 598 break; 599 default: 600 dev_err(sdev->dev, "%s: Unknown DSP message: 0x%x\n", __func__, cmd); 601 break; 602 } 603 604 /* read the full message */ 605 msg_buf = kmalloc(hdr.size, GFP_KERNEL); 606 if (!msg_buf) 607 return; 608 609 err = snd_sof_ipc_msg_data(sdev, NULL, msg_buf, hdr.size); 610 if (err < 0) { 611 dev_err(sdev->dev, "%s: Failed to read message: %d\n", __func__, err); 612 } else { 613 /* Call local handler for the message */ 614 if (rx_callback) 615 rx_callback(sdev, msg_buf); 616 617 /* Notify registered clients */ 618 sof_client_ipc_rx_dispatcher(sdev, msg_buf); 619 } 620 621 kfree(msg_buf); 622 623 ipc_log_header(sdev->dev, "ipc rx done", hdr.cmd); 624 } 625 EXPORT_SYMBOL(snd_sof_ipc_msgs_rx); 626 627 /* 628 * IPC trace mechanism. 629 */ 630 631 static void ipc_trace_message(struct snd_sof_dev *sdev, void *msg_buf) 632 { 633 struct sof_ipc_cmd_hdr *hdr = msg_buf; 634 u32 msg_type = hdr->cmd & SOF_CMD_TYPE_MASK; 635 636 switch (msg_type) { 637 case SOF_IPC_TRACE_DMA_POSITION: 638 snd_sof_trace_update_pos(sdev, msg_buf); 639 break; 640 default: 641 dev_err(sdev->dev, "error: unhandled trace message %#x\n", msg_type); 642 break; 643 } 644 } 645 646 /* 647 * IPC stream position. 648 */ 649 650 static void ipc_period_elapsed(struct snd_sof_dev *sdev, u32 msg_id) 651 { 652 struct snd_soc_component *scomp = sdev->component; 653 struct snd_sof_pcm_stream *stream; 654 struct sof_ipc_stream_posn posn; 655 struct snd_sof_pcm *spcm; 656 int direction, ret; 657 658 spcm = snd_sof_find_spcm_comp(scomp, msg_id, &direction); 659 if (!spcm) { 660 dev_err(sdev->dev, 661 "error: period elapsed for unknown stream, msg_id %d\n", 662 msg_id); 663 return; 664 } 665 666 stream = &spcm->stream[direction]; 667 ret = snd_sof_ipc_msg_data(sdev, stream->substream, &posn, sizeof(posn)); 668 if (ret < 0) { 669 dev_warn(sdev->dev, "failed to read stream position: %d\n", ret); 670 return; 671 } 672 673 dev_vdbg(sdev->dev, "posn : host 0x%llx dai 0x%llx wall 0x%llx\n", 674 posn.host_posn, posn.dai_posn, posn.wallclock); 675 676 memcpy(&stream->posn, &posn, sizeof(posn)); 677 678 if (spcm->pcm.compress) 679 snd_sof_compr_fragment_elapsed(stream->cstream); 680 else if (stream->substream->runtime && 681 !stream->substream->runtime->no_period_wakeup) 682 /* only inform ALSA for period_wakeup mode */ 683 snd_sof_pcm_period_elapsed(stream->substream); 684 } 685 686 /* DSP notifies host of an XRUN within FW */ 687 static void ipc_xrun(struct snd_sof_dev *sdev, u32 msg_id) 688 { 689 struct snd_soc_component *scomp = sdev->component; 690 struct snd_sof_pcm_stream *stream; 691 struct sof_ipc_stream_posn posn; 692 struct snd_sof_pcm *spcm; 693 int direction, ret; 694 695 spcm = snd_sof_find_spcm_comp(scomp, msg_id, &direction); 696 if (!spcm) { 697 dev_err(sdev->dev, "error: XRUN for unknown stream, msg_id %d\n", 698 msg_id); 699 return; 700 } 701 702 stream = &spcm->stream[direction]; 703 ret = snd_sof_ipc_msg_data(sdev, stream->substream, &posn, sizeof(posn)); 704 if (ret < 0) { 705 dev_warn(sdev->dev, "failed to read overrun position: %d\n", ret); 706 return; 707 } 708 709 dev_dbg(sdev->dev, "posn XRUN: host %llx comp %d size %d\n", 710 posn.host_posn, posn.xrun_comp_id, posn.xrun_size); 711 712 #if defined(CONFIG_SND_SOC_SOF_DEBUG_XRUN_STOP) 713 /* stop PCM on XRUN - used for pipeline debug */ 714 memcpy(&stream->posn, &posn, sizeof(posn)); 715 snd_pcm_stop_xrun(stream->substream); 716 #endif 717 } 718 719 /* stream notifications from DSP FW */ 720 static void ipc_stream_message(struct snd_sof_dev *sdev, void *msg_buf) 721 { 722 struct sof_ipc_cmd_hdr *hdr = msg_buf; 723 u32 msg_type = hdr->cmd & SOF_CMD_TYPE_MASK; 724 u32 msg_id = SOF_IPC_MESSAGE_ID(hdr->cmd); 725 726 switch (msg_type) { 727 case SOF_IPC_STREAM_POSITION: 728 ipc_period_elapsed(sdev, msg_id); 729 break; 730 case SOF_IPC_STREAM_TRIG_XRUN: 731 ipc_xrun(sdev, msg_id); 732 break; 733 default: 734 dev_err(sdev->dev, "error: unhandled stream message %#x\n", 735 msg_id); 736 break; 737 } 738 } 739 740 /* get stream position IPC - use faster MMIO method if available on platform */ 741 int snd_sof_ipc_stream_posn(struct snd_soc_component *scomp, 742 struct snd_sof_pcm *spcm, int direction, 743 struct sof_ipc_stream_posn *posn) 744 { 745 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 746 struct sof_ipc_stream stream; 747 int err; 748 749 /* read position via slower IPC */ 750 stream.hdr.size = sizeof(stream); 751 stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_POSITION; 752 stream.comp_id = spcm->stream[direction].comp_id; 753 754 /* send IPC to the DSP */ 755 err = sof_ipc_tx_message(sdev->ipc, &stream, sizeof(stream), posn, 756 sizeof(*posn)); 757 if (err < 0) { 758 dev_err(sdev->dev, "error: failed to get stream %d position\n", 759 stream.comp_id); 760 return err; 761 } 762 763 return 0; 764 } 765 EXPORT_SYMBOL(snd_sof_ipc_stream_posn); 766 767 static int sof_get_ctrl_copy_params(enum sof_ipc_ctrl_type ctrl_type, 768 struct sof_ipc_ctrl_data *src, 769 struct sof_ipc_ctrl_data *dst, 770 struct sof_ipc_ctrl_data_params *sparams) 771 { 772 switch (ctrl_type) { 773 case SOF_CTRL_TYPE_VALUE_CHAN_GET: 774 case SOF_CTRL_TYPE_VALUE_CHAN_SET: 775 sparams->src = (u8 *)src->chanv; 776 sparams->dst = (u8 *)dst->chanv; 777 break; 778 case SOF_CTRL_TYPE_DATA_GET: 779 case SOF_CTRL_TYPE_DATA_SET: 780 sparams->src = (u8 *)src->data->data; 781 sparams->dst = (u8 *)dst->data->data; 782 break; 783 default: 784 return -EINVAL; 785 } 786 787 /* calculate payload size and number of messages */ 788 sparams->pl_size = SOF_IPC_MSG_MAX_SIZE - sparams->hdr_bytes; 789 sparams->num_msg = DIV_ROUND_UP(sparams->msg_bytes, sparams->pl_size); 790 791 return 0; 792 } 793 794 static int sof_set_get_large_ctrl_data(struct snd_sof_dev *sdev, 795 struct sof_ipc_ctrl_data *cdata, 796 struct sof_ipc_ctrl_data_params *sparams, 797 bool set) 798 { 799 struct sof_ipc_ctrl_data *partdata; 800 size_t send_bytes; 801 size_t offset = 0; 802 size_t msg_bytes; 803 size_t pl_size; 804 int err; 805 int i; 806 807 /* allocate max ipc size because we have at least one */ 808 partdata = kzalloc(SOF_IPC_MSG_MAX_SIZE, GFP_KERNEL); 809 if (!partdata) 810 return -ENOMEM; 811 812 if (set) 813 err = sof_get_ctrl_copy_params(cdata->type, cdata, partdata, 814 sparams); 815 else 816 err = sof_get_ctrl_copy_params(cdata->type, partdata, cdata, 817 sparams); 818 if (err < 0) { 819 kfree(partdata); 820 return err; 821 } 822 823 msg_bytes = sparams->msg_bytes; 824 pl_size = sparams->pl_size; 825 826 /* copy the header data */ 827 memcpy(partdata, cdata, sparams->hdr_bytes); 828 829 /* Serialise IPC TX */ 830 mutex_lock(&sdev->ipc->tx_mutex); 831 832 /* copy the payload data in a loop */ 833 for (i = 0; i < sparams->num_msg; i++) { 834 send_bytes = min(msg_bytes, pl_size); 835 partdata->num_elems = send_bytes; 836 partdata->rhdr.hdr.size = sparams->hdr_bytes + send_bytes; 837 partdata->msg_index = i; 838 msg_bytes -= send_bytes; 839 partdata->elems_remaining = msg_bytes; 840 841 if (set) 842 memcpy(sparams->dst, sparams->src + offset, send_bytes); 843 844 err = sof_ipc_tx_message_unlocked(sdev->ipc, 845 partdata, 846 partdata->rhdr.hdr.size, 847 partdata, 848 partdata->rhdr.hdr.size); 849 if (err < 0) 850 break; 851 852 if (!set) 853 memcpy(sparams->dst + offset, sparams->src, send_bytes); 854 855 offset += pl_size; 856 } 857 858 mutex_unlock(&sdev->ipc->tx_mutex); 859 860 kfree(partdata); 861 return err; 862 } 863 864 /* 865 * IPC get()/set() for kcontrols. 866 */ 867 int snd_sof_ipc_set_get_comp_data(struct snd_sof_control *scontrol, bool set) 868 { 869 struct snd_soc_component *scomp = scontrol->scomp; 870 struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data; 871 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 872 struct sof_ipc_fw_ready *ready = &sdev->fw_ready; 873 struct sof_ipc_fw_version *v = &ready->version; 874 struct sof_ipc_ctrl_data_params sparams; 875 enum sof_ipc_ctrl_type ctrl_type; 876 struct snd_sof_widget *swidget; 877 bool widget_found = false; 878 u32 ipc_cmd; 879 int err; 880 881 list_for_each_entry(swidget, &sdev->widget_list, list) { 882 if (swidget->comp_id == scontrol->comp_id) { 883 widget_found = true; 884 break; 885 } 886 } 887 888 if (!widget_found) { 889 dev_err(sdev->dev, "error: can't find widget with id %d\n", scontrol->comp_id); 890 return -EINVAL; 891 } 892 893 /* 894 * Volatile controls should always be part of static pipelines and the widget use_count 895 * would always be > 0 in this case. For the others, just return the cached value if the 896 * widget is not set up. 897 */ 898 if (!swidget->use_count) 899 return 0; 900 901 /* 902 * Select the IPC cmd and the ctrl_type based on the ctrl_cmd and the 903 * direction 904 * Note: SOF_CTRL_TYPE_VALUE_COMP_* is not used and supported currently 905 * for ctrl_type 906 */ 907 if (cdata->cmd == SOF_CTRL_CMD_BINARY) { 908 ipc_cmd = set ? SOF_IPC_COMP_SET_DATA : SOF_IPC_COMP_GET_DATA; 909 ctrl_type = set ? SOF_CTRL_TYPE_DATA_SET : SOF_CTRL_TYPE_DATA_GET; 910 } else { 911 ipc_cmd = set ? SOF_IPC_COMP_SET_VALUE : SOF_IPC_COMP_GET_VALUE; 912 ctrl_type = set ? SOF_CTRL_TYPE_VALUE_CHAN_SET : SOF_CTRL_TYPE_VALUE_CHAN_GET; 913 } 914 915 cdata->rhdr.hdr.cmd = SOF_IPC_GLB_COMP_MSG | ipc_cmd; 916 cdata->type = ctrl_type; 917 cdata->comp_id = scontrol->comp_id; 918 cdata->msg_index = 0; 919 920 /* calculate header and data size */ 921 switch (cdata->type) { 922 case SOF_CTRL_TYPE_VALUE_CHAN_GET: 923 case SOF_CTRL_TYPE_VALUE_CHAN_SET: 924 sparams.msg_bytes = scontrol->num_channels * 925 sizeof(struct sof_ipc_ctrl_value_chan); 926 sparams.hdr_bytes = sizeof(struct sof_ipc_ctrl_data); 927 sparams.elems = scontrol->num_channels; 928 break; 929 case SOF_CTRL_TYPE_DATA_GET: 930 case SOF_CTRL_TYPE_DATA_SET: 931 sparams.msg_bytes = cdata->data->size; 932 sparams.hdr_bytes = sizeof(struct sof_ipc_ctrl_data) + 933 sizeof(struct sof_abi_hdr); 934 sparams.elems = cdata->data->size; 935 break; 936 default: 937 return -EINVAL; 938 } 939 940 cdata->rhdr.hdr.size = sparams.msg_bytes + sparams.hdr_bytes; 941 cdata->num_elems = sparams.elems; 942 cdata->elems_remaining = 0; 943 944 /* send normal size ipc in one part */ 945 if (cdata->rhdr.hdr.size <= SOF_IPC_MSG_MAX_SIZE) { 946 err = sof_ipc_tx_message(sdev->ipc, cdata, cdata->rhdr.hdr.size, 947 cdata, cdata->rhdr.hdr.size); 948 949 if (err < 0) 950 dev_err(sdev->dev, "error: set/get ctrl ipc comp %d\n", 951 cdata->comp_id); 952 953 return err; 954 } 955 956 /* data is bigger than max ipc size, chop into smaller pieces */ 957 dev_dbg(sdev->dev, "large ipc size %u, control size %u\n", 958 cdata->rhdr.hdr.size, scontrol->size); 959 960 /* large messages is only supported from ABI 3.3.0 onwards */ 961 if (v->abi_version < SOF_ABI_VER(3, 3, 0)) { 962 dev_err(sdev->dev, "error: incompatible FW ABI version\n"); 963 return -EINVAL; 964 } 965 966 err = sof_set_get_large_ctrl_data(sdev, cdata, &sparams, set); 967 968 if (err < 0) 969 dev_err(sdev->dev, "error: set/get large ctrl ipc comp %d\n", 970 cdata->comp_id); 971 972 return err; 973 } 974 EXPORT_SYMBOL(snd_sof_ipc_set_get_comp_data); 975 976 int snd_sof_ipc_valid(struct snd_sof_dev *sdev) 977 { 978 struct sof_ipc_fw_ready *ready = &sdev->fw_ready; 979 struct sof_ipc_fw_version *v = &ready->version; 980 981 dev_info(sdev->dev, 982 "Firmware info: version %d:%d:%d-%s\n", v->major, v->minor, 983 v->micro, v->tag); 984 dev_info(sdev->dev, 985 "Firmware: ABI %d:%d:%d Kernel ABI %d:%d:%d\n", 986 SOF_ABI_VERSION_MAJOR(v->abi_version), 987 SOF_ABI_VERSION_MINOR(v->abi_version), 988 SOF_ABI_VERSION_PATCH(v->abi_version), 989 SOF_ABI_MAJOR, SOF_ABI_MINOR, SOF_ABI_PATCH); 990 991 if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, v->abi_version)) { 992 dev_err(sdev->dev, "error: incompatible FW ABI version\n"); 993 return -EINVAL; 994 } 995 996 if (SOF_ABI_VERSION_MINOR(v->abi_version) > SOF_ABI_MINOR) { 997 if (!IS_ENABLED(CONFIG_SND_SOC_SOF_STRICT_ABI_CHECKS)) { 998 dev_warn(sdev->dev, "warn: FW ABI is more recent than kernel\n"); 999 } else { 1000 dev_err(sdev->dev, "error: FW ABI is more recent than kernel\n"); 1001 return -EINVAL; 1002 } 1003 } 1004 1005 if (ready->flags & SOF_IPC_INFO_BUILD) { 1006 dev_info(sdev->dev, 1007 "Firmware debug build %d on %s-%s - options:\n" 1008 " GDB: %s\n" 1009 " lock debug: %s\n" 1010 " lock vdebug: %s\n", 1011 v->build, v->date, v->time, 1012 (ready->flags & SOF_IPC_INFO_GDB) ? 1013 "enabled" : "disabled", 1014 (ready->flags & SOF_IPC_INFO_LOCKS) ? 1015 "enabled" : "disabled", 1016 (ready->flags & SOF_IPC_INFO_LOCKSV) ? 1017 "enabled" : "disabled"); 1018 } 1019 1020 /* copy the fw_version into debugfs at first boot */ 1021 memcpy(&sdev->fw_version, v, sizeof(*v)); 1022 1023 return 0; 1024 } 1025 EXPORT_SYMBOL(snd_sof_ipc_valid); 1026 1027 int sof_ipc_init_msg_memory(struct snd_sof_dev *sdev) 1028 { 1029 struct snd_sof_ipc_msg *msg; 1030 1031 msg = &sdev->ipc->msg; 1032 1033 msg->reply_data = devm_kzalloc(sdev->dev, SOF_IPC_MSG_MAX_SIZE, GFP_KERNEL); 1034 if (!msg->reply_data) 1035 return -ENOMEM; 1036 1037 sdev->ipc->max_payload_size = SOF_IPC_MSG_MAX_SIZE; 1038 1039 return 0; 1040 } 1041 1042 struct snd_sof_ipc *snd_sof_ipc_init(struct snd_sof_dev *sdev) 1043 { 1044 struct snd_sof_ipc *ipc; 1045 struct snd_sof_ipc_msg *msg; 1046 1047 ipc = devm_kzalloc(sdev->dev, sizeof(*ipc), GFP_KERNEL); 1048 if (!ipc) 1049 return NULL; 1050 1051 mutex_init(&ipc->tx_mutex); 1052 ipc->sdev = sdev; 1053 msg = &ipc->msg; 1054 1055 /* indicate that we aren't sending a message ATM */ 1056 msg->ipc_complete = true; 1057 1058 init_waitqueue_head(&msg->waitq); 1059 1060 /* 1061 * Use IPC3 ops as it is the only available version now. With the addition of new IPC 1062 * versions, this will need to be modified to use the selected version at runtime. 1063 */ 1064 ipc->ops = &ipc3_ops; 1065 1066 /* check for mandatory ops */ 1067 if (!ipc->ops->pcm || !ipc->ops->tplg || !ipc->ops->tplg->widget || 1068 !ipc->ops->tplg->control) { 1069 dev_err(sdev->dev, "Invalid IPC ops\n"); 1070 return NULL; 1071 } 1072 1073 return ipc; 1074 } 1075 EXPORT_SYMBOL(snd_sof_ipc_init); 1076 1077 void snd_sof_ipc_free(struct snd_sof_dev *sdev) 1078 { 1079 struct snd_sof_ipc *ipc = sdev->ipc; 1080 1081 if (!ipc) 1082 return; 1083 1084 /* disable sending of ipc's */ 1085 mutex_lock(&ipc->tx_mutex); 1086 ipc->disable_ipc_tx = true; 1087 mutex_unlock(&ipc->tx_mutex); 1088 } 1089 EXPORT_SYMBOL(snd_sof_ipc_free); 1090