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 HDA DSP code loader 16 */ 17 18 #include <linux/firmware.h> 19 #include <sound/hdaudio_ext.h> 20 #include <sound/sof.h> 21 #include "../ops.h" 22 #include "hda.h" 23 24 #define HDA_FW_BOOT_ATTEMPTS 3 25 26 static int cl_stream_prepare(struct snd_sof_dev *sdev, unsigned int format, 27 unsigned int size, struct snd_dma_buffer *dmab, 28 int direction) 29 { 30 struct hdac_ext_stream *dsp_stream; 31 struct hdac_stream *hstream; 32 struct pci_dev *pci = to_pci_dev(sdev->dev); 33 int ret; 34 35 if (direction != SNDRV_PCM_STREAM_PLAYBACK) { 36 dev_err(sdev->dev, "error: code loading DMA is playback only\n"); 37 return -EINVAL; 38 } 39 40 dsp_stream = hda_dsp_stream_get(sdev, direction); 41 42 if (!dsp_stream) { 43 dev_err(sdev->dev, "error: no stream available\n"); 44 return -ENODEV; 45 } 46 hstream = &dsp_stream->hstream; 47 hstream->substream = NULL; 48 49 /* allocate DMA buffer */ 50 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG, &pci->dev, size, dmab); 51 if (ret < 0) { 52 dev_err(sdev->dev, "error: memory alloc failed: %x\n", ret); 53 goto error; 54 } 55 56 hstream->period_bytes = 0;/* initialize period_bytes */ 57 hstream->format_val = format; 58 hstream->bufsize = size; 59 60 ret = hda_dsp_stream_hw_params(sdev, dsp_stream, dmab, NULL); 61 if (ret < 0) { 62 dev_err(sdev->dev, "error: hdac prepare failed: %x\n", ret); 63 goto error; 64 } 65 66 hda_dsp_stream_spib_config(sdev, dsp_stream, HDA_DSP_SPIB_ENABLE, size); 67 68 return hstream->stream_tag; 69 70 error: 71 hda_dsp_stream_put(sdev, direction, hstream->stream_tag); 72 snd_dma_free_pages(dmab); 73 return ret; 74 } 75 76 /* 77 * first boot sequence has some extra steps. core 0 waits for power 78 * status on core 1, so power up core 1 also momentarily, keep it in 79 * reset/stall and then turn it off 80 */ 81 static int cl_dsp_init(struct snd_sof_dev *sdev, const void *fwdata, 82 u32 fwsize, int stream_tag) 83 { 84 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata; 85 const struct sof_intel_dsp_desc *chip = hda->desc; 86 unsigned int status; 87 int ret; 88 int i; 89 90 /* step 1: power up corex */ 91 ret = hda_dsp_core_power_up(sdev, chip->cores_mask); 92 if (ret < 0) { 93 dev_err(sdev->dev, "error: dsp core 0/1 power up failed\n"); 94 goto err; 95 } 96 97 /* DSP is powered up, set all SSPs to slave mode */ 98 for (i = 0; i < chip->ssp_count; i++) { 99 snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR, 100 chip->ssp_base_offset 101 + i * SSP_DEV_MEM_SIZE 102 + SSP_SSC1_OFFSET, 103 SSP_SET_SLAVE, 104 SSP_SET_SLAVE); 105 } 106 107 /* step 2: purge FW request */ 108 snd_sof_dsp_write(sdev, HDA_DSP_BAR, chip->ipc_req, 109 chip->ipc_req_mask | (HDA_DSP_IPC_PURGE_FW | 110 ((stream_tag - 1) << 9))); 111 112 /* step 3: unset core 0 reset state & unstall/run core 0 */ 113 ret = hda_dsp_core_run(sdev, HDA_DSP_CORE_MASK(0)); 114 if (ret < 0) { 115 dev_err(sdev->dev, "error: dsp core start failed %d\n", ret); 116 ret = -EIO; 117 goto err; 118 } 119 120 /* step 4: wait for IPC DONE bit from ROM */ 121 ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR, 122 chip->ipc_ack, status, 123 ((status & chip->ipc_ack_mask) 124 == chip->ipc_ack_mask), 125 HDA_DSP_REG_POLL_INTERVAL_US, 126 HDA_DSP_INIT_TIMEOUT_US); 127 128 if (ret < 0) { 129 dev_err(sdev->dev, "error: %s: timeout for HIPCIE done\n", 130 __func__); 131 goto err; 132 } 133 134 /* set DONE bit to clear the reply IPC message */ 135 snd_sof_dsp_update_bits_forced(sdev, HDA_DSP_BAR, 136 chip->ipc_ack, 137 chip->ipc_ack_mask, 138 chip->ipc_ack_mask); 139 140 /* step 5: power down corex */ 141 ret = hda_dsp_core_power_down(sdev, 142 chip->cores_mask & ~(HDA_DSP_CORE_MASK(0))); 143 if (ret < 0) { 144 dev_err(sdev->dev, "error: dsp core x power down failed\n"); 145 goto err; 146 } 147 148 /* step 6: enable IPC interrupts */ 149 hda_dsp_ipc_int_enable(sdev); 150 151 /* step 7: wait for ROM init */ 152 ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR, 153 HDA_DSP_SRAM_REG_ROM_STATUS, status, 154 ((status & HDA_DSP_ROM_STS_MASK) 155 == HDA_DSP_ROM_INIT), 156 HDA_DSP_REG_POLL_INTERVAL_US, 157 chip->rom_init_timeout * 158 USEC_PER_MSEC); 159 if (!ret) 160 return 0; 161 162 dev_err(sdev->dev, 163 "error: %s: timeout HDA_DSP_SRAM_REG_ROM_STATUS read\n", 164 __func__); 165 166 err: 167 hda_dsp_dump(sdev, SOF_DBG_REGS | SOF_DBG_PCI | SOF_DBG_MBOX); 168 hda_dsp_core_reset_power_down(sdev, chip->cores_mask); 169 170 return ret; 171 } 172 173 static int cl_trigger(struct snd_sof_dev *sdev, 174 struct hdac_ext_stream *stream, int cmd) 175 { 176 struct hdac_stream *hstream = &stream->hstream; 177 int sd_offset = SOF_STREAM_SD_OFFSET(hstream); 178 179 /* code loader is special case that reuses stream ops */ 180 switch (cmd) { 181 case SNDRV_PCM_TRIGGER_START: 182 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL, 183 1 << hstream->index, 184 1 << hstream->index); 185 186 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, 187 sd_offset, 188 SOF_HDA_SD_CTL_DMA_START | 189 SOF_HDA_CL_DMA_SD_INT_MASK, 190 SOF_HDA_SD_CTL_DMA_START | 191 SOF_HDA_CL_DMA_SD_INT_MASK); 192 193 hstream->running = true; 194 return 0; 195 default: 196 return hda_dsp_stream_trigger(sdev, stream, cmd); 197 } 198 } 199 200 static struct hdac_ext_stream *get_stream_with_tag(struct snd_sof_dev *sdev, 201 int tag) 202 { 203 struct hdac_bus *bus = sof_to_bus(sdev); 204 struct hdac_stream *s; 205 206 /* get stream with tag */ 207 list_for_each_entry(s, &bus->stream_list, list) { 208 if (s->direction == SNDRV_PCM_STREAM_PLAYBACK && 209 s->stream_tag == tag) { 210 return stream_to_hdac_ext_stream(s); 211 } 212 } 213 214 return NULL; 215 } 216 217 static int cl_cleanup(struct snd_sof_dev *sdev, struct snd_dma_buffer *dmab, 218 struct hdac_ext_stream *stream) 219 { 220 struct hdac_stream *hstream = &stream->hstream; 221 int sd_offset = SOF_STREAM_SD_OFFSET(hstream); 222 int ret; 223 224 ret = hda_dsp_stream_spib_config(sdev, stream, HDA_DSP_SPIB_DISABLE, 0); 225 226 hda_dsp_stream_put(sdev, SNDRV_PCM_STREAM_PLAYBACK, 227 hstream->stream_tag); 228 hstream->running = 0; 229 hstream->substream = NULL; 230 231 /* reset BDL address */ 232 snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, 233 sd_offset + SOF_HDA_ADSP_REG_CL_SD_BDLPL, 0); 234 snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, 235 sd_offset + SOF_HDA_ADSP_REG_CL_SD_BDLPU, 0); 236 237 snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, sd_offset, 0); 238 snd_dma_free_pages(dmab); 239 dmab->area = NULL; 240 hstream->bufsize = 0; 241 hstream->format_val = 0; 242 243 return ret; 244 } 245 246 static int cl_copy_fw(struct snd_sof_dev *sdev, struct hdac_ext_stream *stream) 247 { 248 unsigned int reg; 249 int ret, status; 250 251 ret = cl_trigger(sdev, stream, SNDRV_PCM_TRIGGER_START); 252 if (ret < 0) { 253 dev_err(sdev->dev, "error: DMA trigger start failed\n"); 254 return ret; 255 } 256 257 status = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR, 258 HDA_DSP_SRAM_REG_ROM_STATUS, reg, 259 ((reg & HDA_DSP_ROM_STS_MASK) 260 == HDA_DSP_ROM_FW_ENTERED), 261 HDA_DSP_REG_POLL_INTERVAL_US, 262 HDA_DSP_BASEFW_TIMEOUT_US); 263 264 /* 265 * even in case of errors we still need to stop the DMAs, 266 * but we return the initial error should the DMA stop also fail 267 */ 268 269 if (status < 0) { 270 dev_err(sdev->dev, 271 "error: %s: timeout HDA_DSP_SRAM_REG_ROM_STATUS read\n", 272 __func__); 273 } 274 275 ret = cl_trigger(sdev, stream, SNDRV_PCM_TRIGGER_STOP); 276 if (ret < 0) { 277 dev_err(sdev->dev, "error: DMA trigger stop failed\n"); 278 if (!status) 279 status = ret; 280 } 281 282 return status; 283 } 284 285 int hda_dsp_cl_boot_firmware(struct snd_sof_dev *sdev) 286 { 287 struct snd_sof_pdata *plat_data = sdev->pdata; 288 const struct sof_dev_desc *desc = plat_data->desc; 289 const struct sof_intel_dsp_desc *chip_info; 290 struct hdac_ext_stream *stream; 291 struct firmware stripped_firmware; 292 int ret, ret1, tag, i; 293 294 chip_info = desc->chip_info; 295 296 if (plat_data->fw->size <= plat_data->fw_offset) { 297 dev_err(sdev->dev, "error: firmware size must be greater than firmware offset\n"); 298 return -EINVAL; 299 } 300 301 stripped_firmware.data = plat_data->fw->data + plat_data->fw_offset; 302 stripped_firmware.size = plat_data->fw->size - plat_data->fw_offset; 303 304 /* init for booting wait */ 305 init_waitqueue_head(&sdev->boot_wait); 306 307 /* prepare DMA for code loader stream */ 308 tag = cl_stream_prepare(sdev, 0x40, stripped_firmware.size, 309 &sdev->dmab, SNDRV_PCM_STREAM_PLAYBACK); 310 311 if (tag < 0) { 312 dev_err(sdev->dev, "error: dma prepare for fw loading err: %x\n", 313 tag); 314 return tag; 315 } 316 317 /* get stream with tag */ 318 stream = get_stream_with_tag(sdev, tag); 319 if (!stream) { 320 dev_err(sdev->dev, 321 "error: could not get stream with stream tag %d\n", 322 tag); 323 ret = -ENODEV; 324 goto err; 325 } 326 327 memcpy(sdev->dmab.area, stripped_firmware.data, 328 stripped_firmware.size); 329 330 /* try ROM init a few times before giving up */ 331 for (i = 0; i < HDA_FW_BOOT_ATTEMPTS; i++) { 332 ret = cl_dsp_init(sdev, stripped_firmware.data, 333 stripped_firmware.size, tag); 334 335 /* don't retry anymore if successful */ 336 if (!ret) 337 break; 338 339 dev_dbg(sdev->dev, "iteration %d of Core En/ROM load failed: %d\n", 340 i, ret); 341 dev_dbg(sdev->dev, "Error code=0x%x: FW status=0x%x\n", 342 snd_sof_dsp_read(sdev, HDA_DSP_BAR, 343 HDA_DSP_SRAM_REG_ROM_ERROR), 344 snd_sof_dsp_read(sdev, HDA_DSP_BAR, 345 HDA_DSP_SRAM_REG_ROM_STATUS)); 346 } 347 348 if (i == HDA_FW_BOOT_ATTEMPTS) { 349 dev_err(sdev->dev, "error: dsp init failed after %d attempts with err: %d\n", 350 i, ret); 351 goto cleanup; 352 } 353 354 /* 355 * When a SoundWire link is in clock stop state, a Slave 356 * device may trigger in-band wakes for events such as jack 357 * insertion or acoustic event detection. This event will lead 358 * to a WAKEEN interrupt, handled by the PCI device and routed 359 * to PME if the PCI device is in D3. The resume function in 360 * audio PCI driver will be invoked by ACPI for PME event and 361 * initialize the device and process WAKEEN interrupt. 362 * 363 * The WAKEEN interrupt should be processed ASAP to prevent an 364 * interrupt flood, otherwise other interrupts, such IPC, 365 * cannot work normally. The WAKEEN is handled after the ROM 366 * is initialized successfully, which ensures power rails are 367 * enabled before accessing the SoundWire SHIM registers 368 */ 369 if (!sdev->first_boot) 370 hda_sdw_process_wakeen(sdev); 371 372 /* 373 * at this point DSP ROM has been initialized and 374 * should be ready for code loading and firmware boot 375 */ 376 ret = cl_copy_fw(sdev, stream); 377 if (!ret) 378 dev_dbg(sdev->dev, "Firmware download successful, booting...\n"); 379 else 380 dev_err(sdev->dev, "error: load fw failed ret: %d\n", ret); 381 382 cleanup: 383 /* 384 * Perform codeloader stream cleanup. 385 * This should be done even if firmware loading fails. 386 * If the cleanup also fails, we return the initial error 387 */ 388 ret1 = cl_cleanup(sdev, &sdev->dmab, stream); 389 if (ret1 < 0) { 390 dev_err(sdev->dev, "error: Code loader DSP cleanup failed\n"); 391 392 /* set return value to indicate cleanup failure */ 393 if (!ret) 394 ret = ret1; 395 } 396 397 /* 398 * return master core id if both fw copy 399 * and stream clean up are successful 400 */ 401 if (!ret) 402 return chip_info->init_core_mask; 403 404 /* dump dsp registers and disable DSP upon error */ 405 err: 406 hda_dsp_dump(sdev, SOF_DBG_REGS | SOF_DBG_PCI | SOF_DBG_MBOX); 407 408 /* disable DSP */ 409 snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, 410 SOF_HDA_REG_PP_PPCTL, 411 SOF_HDA_PPCTL_GPROCEN, 0); 412 return ret; 413 } 414 415 /* pre fw run operations */ 416 int hda_dsp_pre_fw_run(struct snd_sof_dev *sdev) 417 { 418 /* disable clock gating and power gating */ 419 return hda_dsp_ctrl_clock_power_gating(sdev, false); 420 } 421 422 /* post fw run operations */ 423 int hda_dsp_post_fw_run(struct snd_sof_dev *sdev) 424 { 425 int ret; 426 427 if (sdev->first_boot) { 428 ret = hda_sdw_startup(sdev); 429 if (ret < 0) { 430 dev_err(sdev->dev, 431 "error: could not startup SoundWire links\n"); 432 return ret; 433 } 434 } 435 436 hda_sdw_int_enable(sdev, true); 437 438 /* re-enable clock gating and power gating */ 439 return hda_dsp_ctrl_clock_power_gating(sdev, true); 440 } 441