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 // Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com> 9 // Ranjani Sridharan <ranjani.sridharan@linux.intel.com> 10 // Rander Wang <rander.wang@intel.com> 11 // Keyon Jie <yang.jie@linux.intel.com> 12 // 13 14 /* 15 * Hardware interface for audio DSP on Cannonlake. 16 */ 17 18 #include <sound/sof/ext_manifest4.h> 19 #include <sound/sof/ipc4/header.h> 20 #include <trace/events/sof_intel.h> 21 #include "../ipc4-priv.h" 22 #include "../ops.h" 23 #include "hda.h" 24 #include "hda-ipc.h" 25 #include "../sof-audio.h" 26 27 static const struct snd_sof_debugfs_map cnl_dsp_debugfs[] = { 28 {"hda", HDA_DSP_HDA_BAR, 0, 0x4000, SOF_DEBUGFS_ACCESS_ALWAYS}, 29 {"pp", HDA_DSP_PP_BAR, 0, 0x1000, SOF_DEBUGFS_ACCESS_ALWAYS}, 30 {"dsp", HDA_DSP_BAR, 0, 0x10000, SOF_DEBUGFS_ACCESS_ALWAYS}, 31 }; 32 33 static void cnl_ipc_host_done(struct snd_sof_dev *sdev); 34 static void cnl_ipc_dsp_done(struct snd_sof_dev *sdev); 35 36 irqreturn_t cnl_ipc4_irq_thread(int irq, void *context) 37 { 38 struct sof_ipc4_msg notification_data = {{ 0 }}; 39 struct snd_sof_dev *sdev = context; 40 bool ack_received = false; 41 bool ipc_irq = false; 42 u32 hipcida, hipctdr; 43 44 hipcida = snd_sof_dsp_read(sdev, HDA_DSP_BAR, CNL_DSP_REG_HIPCIDA); 45 hipctdr = snd_sof_dsp_read(sdev, HDA_DSP_BAR, CNL_DSP_REG_HIPCTDR); 46 if (hipcida & CNL_DSP_REG_HIPCIDA_DONE) { 47 /* DSP received the message */ 48 snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, 49 CNL_DSP_REG_HIPCCTL, 50 CNL_DSP_REG_HIPCCTL_DONE, 0); 51 cnl_ipc_dsp_done(sdev); 52 53 ipc_irq = true; 54 ack_received = true; 55 } 56 57 if (hipctdr & CNL_DSP_REG_HIPCTDR_BUSY) { 58 /* Message from DSP (reply or notification) */ 59 u32 hipctdd = snd_sof_dsp_read(sdev, HDA_DSP_BAR, 60 CNL_DSP_REG_HIPCTDD); 61 u32 primary = hipctdr & CNL_DSP_REG_HIPCTDR_MSG_MASK; 62 u32 extension = hipctdd & CNL_DSP_REG_HIPCTDD_MSG_MASK; 63 64 if (primary & SOF_IPC4_MSG_DIR_MASK) { 65 /* Reply received */ 66 if (likely(sdev->fw_state == SOF_FW_BOOT_COMPLETE)) { 67 struct sof_ipc4_msg *data = sdev->ipc->msg.reply_data; 68 69 data->primary = primary; 70 data->extension = extension; 71 72 spin_lock_irq(&sdev->ipc_lock); 73 74 snd_sof_ipc_get_reply(sdev); 75 cnl_ipc_host_done(sdev); 76 snd_sof_ipc_reply(sdev, data->primary); 77 78 spin_unlock_irq(&sdev->ipc_lock); 79 } else { 80 dev_dbg_ratelimited(sdev->dev, 81 "IPC reply before FW_READY: %#x|%#x\n", 82 primary, extension); 83 } 84 } else { 85 /* Notification received */ 86 notification_data.primary = primary; 87 notification_data.extension = extension; 88 89 sdev->ipc->msg.rx_data = ¬ification_data; 90 snd_sof_ipc_msgs_rx(sdev); 91 sdev->ipc->msg.rx_data = NULL; 92 93 /* Let DSP know that we have finished processing the message */ 94 cnl_ipc_host_done(sdev); 95 } 96 97 ipc_irq = true; 98 } 99 100 if (!ipc_irq) 101 /* This interrupt is not shared so no need to return IRQ_NONE. */ 102 dev_dbg_ratelimited(sdev->dev, "nothing to do in IPC IRQ thread\n"); 103 104 if (ack_received) { 105 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; 106 107 if (hdev->delayed_ipc_tx_msg) 108 cnl_ipc4_send_msg(sdev, hdev->delayed_ipc_tx_msg); 109 } 110 111 return IRQ_HANDLED; 112 } 113 114 irqreturn_t cnl_ipc_irq_thread(int irq, void *context) 115 { 116 struct snd_sof_dev *sdev = context; 117 u32 hipci; 118 u32 hipcida; 119 u32 hipctdr; 120 u32 hipctdd; 121 u32 msg; 122 u32 msg_ext; 123 bool ipc_irq = false; 124 125 hipcida = snd_sof_dsp_read(sdev, HDA_DSP_BAR, CNL_DSP_REG_HIPCIDA); 126 hipctdr = snd_sof_dsp_read(sdev, HDA_DSP_BAR, CNL_DSP_REG_HIPCTDR); 127 hipctdd = snd_sof_dsp_read(sdev, HDA_DSP_BAR, CNL_DSP_REG_HIPCTDD); 128 hipci = snd_sof_dsp_read(sdev, HDA_DSP_BAR, CNL_DSP_REG_HIPCIDR); 129 130 /* reply message from DSP */ 131 if (hipcida & CNL_DSP_REG_HIPCIDA_DONE) { 132 msg_ext = hipci & CNL_DSP_REG_HIPCIDR_MSG_MASK; 133 msg = hipcida & CNL_DSP_REG_HIPCIDA_MSG_MASK; 134 135 trace_sof_intel_ipc_firmware_response(sdev, msg, msg_ext); 136 137 /* mask Done interrupt */ 138 snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, 139 CNL_DSP_REG_HIPCCTL, 140 CNL_DSP_REG_HIPCCTL_DONE, 0); 141 142 if (likely(sdev->fw_state == SOF_FW_BOOT_COMPLETE)) { 143 spin_lock_irq(&sdev->ipc_lock); 144 145 /* handle immediate reply from DSP core */ 146 hda_dsp_ipc_get_reply(sdev); 147 snd_sof_ipc_reply(sdev, msg); 148 149 cnl_ipc_dsp_done(sdev); 150 151 spin_unlock_irq(&sdev->ipc_lock); 152 } else { 153 dev_dbg_ratelimited(sdev->dev, "IPC reply before FW_READY: %#x\n", 154 msg); 155 } 156 157 ipc_irq = true; 158 } 159 160 /* new message from DSP */ 161 if (hipctdr & CNL_DSP_REG_HIPCTDR_BUSY) { 162 msg = hipctdr & CNL_DSP_REG_HIPCTDR_MSG_MASK; 163 msg_ext = hipctdd & CNL_DSP_REG_HIPCTDD_MSG_MASK; 164 165 trace_sof_intel_ipc_firmware_initiated(sdev, msg, msg_ext); 166 167 /* handle messages from DSP */ 168 if ((hipctdr & SOF_IPC_PANIC_MAGIC_MASK) == SOF_IPC_PANIC_MAGIC) { 169 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata; 170 bool non_recoverable = true; 171 172 /* 173 * This is a PANIC message! 174 * 175 * If it is arriving during firmware boot and it is not 176 * the last boot attempt then change the non_recoverable 177 * to false as the DSP might be able to boot in the next 178 * iteration(s) 179 */ 180 if (sdev->fw_state == SOF_FW_BOOT_IN_PROGRESS && 181 hda->boot_iteration < HDA_FW_BOOT_ATTEMPTS) 182 non_recoverable = false; 183 184 snd_sof_dsp_panic(sdev, HDA_DSP_PANIC_OFFSET(msg_ext), 185 non_recoverable); 186 } else { 187 snd_sof_ipc_msgs_rx(sdev); 188 } 189 190 cnl_ipc_host_done(sdev); 191 192 ipc_irq = true; 193 } 194 195 if (!ipc_irq) { 196 /* 197 * This interrupt is not shared so no need to return IRQ_NONE. 198 */ 199 dev_dbg_ratelimited(sdev->dev, 200 "nothing to do in IPC IRQ thread\n"); 201 } 202 203 return IRQ_HANDLED; 204 } 205 206 static void cnl_ipc_host_done(struct snd_sof_dev *sdev) 207 { 208 /* 209 * clear busy interrupt to tell dsp controller this 210 * interrupt has been accepted, not trigger it again 211 */ 212 snd_sof_dsp_update_bits_forced(sdev, HDA_DSP_BAR, 213 CNL_DSP_REG_HIPCTDR, 214 CNL_DSP_REG_HIPCTDR_BUSY, 215 CNL_DSP_REG_HIPCTDR_BUSY); 216 /* 217 * set done bit to ack dsp the msg has been 218 * processed and send reply msg to dsp 219 */ 220 snd_sof_dsp_update_bits_forced(sdev, HDA_DSP_BAR, 221 CNL_DSP_REG_HIPCTDA, 222 CNL_DSP_REG_HIPCTDA_DONE, 223 CNL_DSP_REG_HIPCTDA_DONE); 224 } 225 226 static void cnl_ipc_dsp_done(struct snd_sof_dev *sdev) 227 { 228 /* 229 * set DONE bit - tell DSP we have received the reply msg 230 * from DSP, and processed it, don't send more reply to host 231 */ 232 snd_sof_dsp_update_bits_forced(sdev, HDA_DSP_BAR, 233 CNL_DSP_REG_HIPCIDA, 234 CNL_DSP_REG_HIPCIDA_DONE, 235 CNL_DSP_REG_HIPCIDA_DONE); 236 237 /* unmask Done interrupt */ 238 snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, 239 CNL_DSP_REG_HIPCCTL, 240 CNL_DSP_REG_HIPCCTL_DONE, 241 CNL_DSP_REG_HIPCCTL_DONE); 242 } 243 244 static bool cnl_compact_ipc_compress(struct snd_sof_ipc_msg *msg, 245 u32 *dr, u32 *dd) 246 { 247 struct sof_ipc_pm_gate *pm_gate = msg->msg_data; 248 249 if (pm_gate->hdr.cmd == (SOF_IPC_GLB_PM_MSG | SOF_IPC_PM_GATE)) { 250 /* send the compact message via the primary register */ 251 *dr = HDA_IPC_MSG_COMPACT | HDA_IPC_PM_GATE; 252 253 /* send payload via the extended data register */ 254 *dd = pm_gate->flags; 255 256 return true; 257 } 258 259 return false; 260 } 261 262 int cnl_ipc4_send_msg(struct snd_sof_dev *sdev, struct snd_sof_ipc_msg *msg) 263 { 264 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; 265 struct sof_ipc4_msg *msg_data = msg->msg_data; 266 267 if (hda_ipc4_tx_is_busy(sdev)) { 268 hdev->delayed_ipc_tx_msg = msg; 269 return 0; 270 } 271 272 hdev->delayed_ipc_tx_msg = NULL; 273 274 /* send the message via mailbox */ 275 if (msg_data->data_size) 276 sof_mailbox_write(sdev, sdev->host_box.offset, msg_data->data_ptr, 277 msg_data->data_size); 278 279 snd_sof_dsp_write(sdev, HDA_DSP_BAR, CNL_DSP_REG_HIPCIDD, msg_data->extension); 280 snd_sof_dsp_write(sdev, HDA_DSP_BAR, CNL_DSP_REG_HIPCIDR, 281 msg_data->primary | CNL_DSP_REG_HIPCIDR_BUSY); 282 283 return 0; 284 } 285 286 int cnl_ipc_send_msg(struct snd_sof_dev *sdev, struct snd_sof_ipc_msg *msg) 287 { 288 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; 289 struct sof_ipc_cmd_hdr *hdr; 290 u32 dr = 0; 291 u32 dd = 0; 292 293 /* 294 * Currently the only compact IPC supported is the PM_GATE 295 * IPC which is used for transitioning the DSP between the 296 * D0I0 and D0I3 states. And these are sent only during the 297 * set_power_state() op. Therefore, there will never be a case 298 * that a compact IPC results in the DSP exiting D0I3 without 299 * the host and FW being in sync. 300 */ 301 if (cnl_compact_ipc_compress(msg, &dr, &dd)) { 302 /* send the message via IPC registers */ 303 snd_sof_dsp_write(sdev, HDA_DSP_BAR, CNL_DSP_REG_HIPCIDD, 304 dd); 305 snd_sof_dsp_write(sdev, HDA_DSP_BAR, CNL_DSP_REG_HIPCIDR, 306 CNL_DSP_REG_HIPCIDR_BUSY | dr); 307 return 0; 308 } 309 310 /* send the message via mailbox */ 311 sof_mailbox_write(sdev, sdev->host_box.offset, msg->msg_data, 312 msg->msg_size); 313 snd_sof_dsp_write(sdev, HDA_DSP_BAR, CNL_DSP_REG_HIPCIDR, 314 CNL_DSP_REG_HIPCIDR_BUSY); 315 316 hdr = msg->msg_data; 317 318 /* 319 * Use mod_delayed_work() to schedule the delayed work 320 * to avoid scheduling multiple workqueue items when 321 * IPCs are sent at a high-rate. mod_delayed_work() 322 * modifies the timer if the work is pending. 323 * Also, a new delayed work should not be queued after the 324 * CTX_SAVE IPC, which is sent before the DSP enters D3. 325 */ 326 if (hdr->cmd != (SOF_IPC_GLB_PM_MSG | SOF_IPC_PM_CTX_SAVE)) 327 mod_delayed_work(system_wq, &hdev->d0i3_work, 328 msecs_to_jiffies(SOF_HDA_D0I3_WORK_DELAY_MS)); 329 330 return 0; 331 } 332 333 void cnl_ipc_dump(struct snd_sof_dev *sdev) 334 { 335 u32 hipcctl; 336 u32 hipcida; 337 u32 hipctdr; 338 339 hda_ipc_irq_dump(sdev); 340 341 /* read IPC status */ 342 hipcida = snd_sof_dsp_read(sdev, HDA_DSP_BAR, CNL_DSP_REG_HIPCIDA); 343 hipcctl = snd_sof_dsp_read(sdev, HDA_DSP_BAR, CNL_DSP_REG_HIPCCTL); 344 hipctdr = snd_sof_dsp_read(sdev, HDA_DSP_BAR, CNL_DSP_REG_HIPCTDR); 345 346 /* dump the IPC regs */ 347 /* TODO: parse the raw msg */ 348 dev_err(sdev->dev, 349 "error: host status 0x%8.8x dsp status 0x%8.8x mask 0x%8.8x\n", 350 hipcida, hipctdr, hipcctl); 351 } 352 353 void cnl_ipc4_dump(struct snd_sof_dev *sdev) 354 { 355 u32 hipcidr, hipcidd, hipcida, hipctdr, hipctdd, hipctda, hipcctl; 356 357 hda_ipc_irq_dump(sdev); 358 359 hipcidr = snd_sof_dsp_read(sdev, HDA_DSP_BAR, CNL_DSP_REG_HIPCIDR); 360 hipcidd = snd_sof_dsp_read(sdev, HDA_DSP_BAR, CNL_DSP_REG_HIPCIDD); 361 hipcida = snd_sof_dsp_read(sdev, HDA_DSP_BAR, CNL_DSP_REG_HIPCIDA); 362 hipctdr = snd_sof_dsp_read(sdev, HDA_DSP_BAR, CNL_DSP_REG_HIPCTDR); 363 hipctdd = snd_sof_dsp_read(sdev, HDA_DSP_BAR, CNL_DSP_REG_HIPCTDD); 364 hipctda = snd_sof_dsp_read(sdev, HDA_DSP_BAR, CNL_DSP_REG_HIPCTDA); 365 hipcctl = snd_sof_dsp_read(sdev, HDA_DSP_BAR, CNL_DSP_REG_HIPCCTL); 366 367 /* dump the IPC regs */ 368 /* TODO: parse the raw msg */ 369 dev_err(sdev->dev, 370 "Host IPC initiator: %#x|%#x|%#x, target: %#x|%#x|%#x, ctl: %#x\n", 371 hipcidr, hipcidd, hipcida, hipctdr, hipctdd, hipctda, hipcctl); 372 } 373 374 /* cannonlake ops */ 375 struct snd_sof_dsp_ops sof_cnl_ops; 376 EXPORT_SYMBOL_NS(sof_cnl_ops, SND_SOC_SOF_INTEL_HDA_COMMON); 377 378 int sof_cnl_ops_init(struct snd_sof_dev *sdev) 379 { 380 /* common defaults */ 381 memcpy(&sof_cnl_ops, &sof_hda_common_ops, sizeof(struct snd_sof_dsp_ops)); 382 383 /* probe/remove/shutdown */ 384 sof_cnl_ops.shutdown = hda_dsp_shutdown; 385 386 /* ipc */ 387 if (sdev->pdata->ipc_type == SOF_IPC) { 388 /* doorbell */ 389 sof_cnl_ops.irq_thread = cnl_ipc_irq_thread; 390 391 /* ipc */ 392 sof_cnl_ops.send_msg = cnl_ipc_send_msg; 393 394 /* debug */ 395 sof_cnl_ops.ipc_dump = cnl_ipc_dump; 396 } 397 398 if (sdev->pdata->ipc_type == SOF_INTEL_IPC4) { 399 struct sof_ipc4_fw_data *ipc4_data; 400 401 sdev->private = devm_kzalloc(sdev->dev, sizeof(*ipc4_data), GFP_KERNEL); 402 if (!sdev->private) 403 return -ENOMEM; 404 405 ipc4_data = sdev->private; 406 ipc4_data->manifest_fw_hdr_offset = SOF_MAN4_FW_HDR_OFFSET; 407 408 ipc4_data->mtrace_type = SOF_IPC4_MTRACE_INTEL_CAVS_1_8; 409 410 /* External library loading support */ 411 ipc4_data->load_library = hda_dsp_ipc4_load_library; 412 413 /* doorbell */ 414 sof_cnl_ops.irq_thread = cnl_ipc4_irq_thread; 415 416 /* ipc */ 417 sof_cnl_ops.send_msg = cnl_ipc4_send_msg; 418 419 /* debug */ 420 sof_cnl_ops.ipc_dump = cnl_ipc4_dump; 421 } 422 423 /* set DAI driver ops */ 424 hda_set_dai_drv_ops(sdev, &sof_cnl_ops); 425 426 /* debug */ 427 sof_cnl_ops.debug_map = cnl_dsp_debugfs; 428 sof_cnl_ops.debug_map_count = ARRAY_SIZE(cnl_dsp_debugfs); 429 430 /* pre/post fw run */ 431 sof_cnl_ops.post_fw_run = hda_dsp_post_fw_run; 432 433 /* firmware run */ 434 sof_cnl_ops.run = hda_dsp_cl_boot_firmware; 435 436 /* dsp core get/put */ 437 sof_cnl_ops.core_get = hda_dsp_core_get; 438 439 return 0; 440 }; 441 EXPORT_SYMBOL_NS(sof_cnl_ops_init, SND_SOC_SOF_INTEL_HDA_COMMON); 442 443 const struct sof_intel_dsp_desc cnl_chip_info = { 444 /* Cannonlake */ 445 .cores_num = 4, 446 .init_core_mask = 1, 447 .host_managed_cores_mask = GENMASK(3, 0), 448 .ipc_req = CNL_DSP_REG_HIPCIDR, 449 .ipc_req_mask = CNL_DSP_REG_HIPCIDR_BUSY, 450 .ipc_ack = CNL_DSP_REG_HIPCIDA, 451 .ipc_ack_mask = CNL_DSP_REG_HIPCIDA_DONE, 452 .ipc_ctl = CNL_DSP_REG_HIPCCTL, 453 .rom_status_reg = HDA_DSP_SRAM_REG_ROM_STATUS, 454 .rom_init_timeout = 300, 455 .ssp_count = CNL_SSP_COUNT, 456 .ssp_base_offset = CNL_SSP_BASE_OFFSET, 457 .sdw_shim_base = SDW_SHIM_BASE, 458 .sdw_alh_base = SDW_ALH_BASE, 459 .d0i3_offset = SOF_HDA_VS_D0I3C, 460 .read_sdw_lcount = hda_sdw_check_lcount_common, 461 .enable_sdw_irq = hda_common_enable_sdw_irq, 462 .check_sdw_irq = hda_common_check_sdw_irq, 463 .check_ipc_irq = hda_dsp_check_ipc_irq, 464 .cl_init = cl_dsp_init, 465 .power_down_dsp = hda_power_down_dsp, 466 .disable_interrupts = hda_dsp_disable_interrupts, 467 .hw_ip_version = SOF_INTEL_CAVS_1_8, 468 }; 469 EXPORT_SYMBOL_NS(cnl_chip_info, SND_SOC_SOF_INTEL_HDA_COMMON); 470 471 /* 472 * JasperLake is technically derived from IceLake, and should be in 473 * described in icl.c. However since JasperLake was designed with 474 * two cores, it cannot support the IceLake-specific power-up sequences 475 * which rely on core3. To simplify, JasperLake uses the CannonLake ops and 476 * is described in cnl.c 477 */ 478 const struct sof_intel_dsp_desc jsl_chip_info = { 479 /* Jasperlake */ 480 .cores_num = 2, 481 .init_core_mask = 1, 482 .host_managed_cores_mask = GENMASK(1, 0), 483 .ipc_req = CNL_DSP_REG_HIPCIDR, 484 .ipc_req_mask = CNL_DSP_REG_HIPCIDR_BUSY, 485 .ipc_ack = CNL_DSP_REG_HIPCIDA, 486 .ipc_ack_mask = CNL_DSP_REG_HIPCIDA_DONE, 487 .ipc_ctl = CNL_DSP_REG_HIPCCTL, 488 .rom_status_reg = HDA_DSP_SRAM_REG_ROM_STATUS, 489 .rom_init_timeout = 300, 490 .ssp_count = ICL_SSP_COUNT, 491 .ssp_base_offset = CNL_SSP_BASE_OFFSET, 492 .sdw_shim_base = SDW_SHIM_BASE, 493 .sdw_alh_base = SDW_ALH_BASE, 494 .d0i3_offset = SOF_HDA_VS_D0I3C, 495 .read_sdw_lcount = hda_sdw_check_lcount_common, 496 .enable_sdw_irq = hda_common_enable_sdw_irq, 497 .check_sdw_irq = hda_common_check_sdw_irq, 498 .check_ipc_irq = hda_dsp_check_ipc_irq, 499 .cl_init = cl_dsp_init, 500 .power_down_dsp = hda_power_down_dsp, 501 .disable_interrupts = hda_dsp_disable_interrupts, 502 .hw_ip_version = SOF_INTEL_CAVS_2_0, 503 }; 504 EXPORT_SYMBOL_NS(jsl_chip_info, SND_SOC_SOF_INTEL_HDA_COMMON); 505