1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) 2 // 3 // This file is provided under a dual BSD/GPLv2 license. When using or 4 // redistributing this file, you may do so under either license. 5 // 6 // Copyright(c) 2018 Intel Corporation. All rights reserved. 7 // 8 // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com> 9 // 10 11 #include "ops.h" 12 #include "sof-priv.h" 13 #include "sof-audio.h" 14 15 /* 16 * Helper function to determine the target DSP state during 17 * system suspend. This function only cares about the device 18 * D-states. Platform-specific substates, if any, should be 19 * handled by the platform-specific parts. 20 */ 21 static u32 snd_sof_dsp_power_target(struct snd_sof_dev *sdev) 22 { 23 u32 target_dsp_state; 24 25 switch (sdev->system_suspend_target) { 26 case SOF_SUSPEND_S3: 27 /* DSP should be in D3 if the system is suspending to S3 */ 28 target_dsp_state = SOF_DSP_PM_D3; 29 break; 30 case SOF_SUSPEND_S0IX: 31 /* 32 * Currently, the only criterion for retaining the DSP in D0 33 * is that there are streams that ignored the suspend trigger. 34 * Additional criteria such Soundwire clock-stop mode and 35 * device suspend latency considerations will be added later. 36 */ 37 if (snd_sof_stream_suspend_ignored(sdev)) 38 target_dsp_state = SOF_DSP_PM_D0; 39 else 40 target_dsp_state = SOF_DSP_PM_D3; 41 break; 42 default: 43 /* This case would be during runtime suspend */ 44 target_dsp_state = SOF_DSP_PM_D3; 45 break; 46 } 47 48 return target_dsp_state; 49 } 50 51 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE) 52 static void sof_cache_debugfs(struct snd_sof_dev *sdev) 53 { 54 struct snd_sof_dfsentry *dfse; 55 56 list_for_each_entry(dfse, &sdev->dfsentry_list, list) { 57 58 /* nothing to do if debugfs buffer is not IO mem */ 59 if (dfse->type == SOF_DFSENTRY_TYPE_BUF) 60 continue; 61 62 /* cache memory that is only accessible in D0 */ 63 if (dfse->access_type == SOF_DEBUGFS_ACCESS_D0_ONLY) 64 memcpy_fromio(dfse->cache_buf, dfse->io_mem, 65 dfse->size); 66 } 67 } 68 #endif 69 70 static int sof_resume(struct device *dev, bool runtime_resume) 71 { 72 struct snd_sof_dev *sdev = dev_get_drvdata(dev); 73 const struct sof_ipc_pm_ops *pm_ops = sdev->ipc->ops->pm; 74 const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg; 75 u32 old_state = sdev->dsp_power_state.state; 76 int ret; 77 78 /* do nothing if dsp resume callbacks are not set */ 79 if (!runtime_resume && !sof_ops(sdev)->resume) 80 return 0; 81 82 if (runtime_resume && !sof_ops(sdev)->runtime_resume) 83 return 0; 84 85 /* DSP was never successfully started, nothing to resume */ 86 if (sdev->first_boot) 87 return 0; 88 89 /* 90 * if the runtime_resume flag is set, call the runtime_resume routine 91 * or else call the system resume routine 92 */ 93 if (runtime_resume) 94 ret = snd_sof_dsp_runtime_resume(sdev); 95 else 96 ret = snd_sof_dsp_resume(sdev); 97 if (ret < 0) { 98 dev_err(sdev->dev, 99 "error: failed to power up DSP after resume\n"); 100 return ret; 101 } 102 103 /* 104 * Nothing further to be done for platforms that support the low power 105 * D0 substate. 106 */ 107 if (!runtime_resume && sof_ops(sdev)->set_power_state && 108 old_state == SOF_DSP_PM_D0) 109 return 0; 110 111 sof_set_fw_state(sdev, SOF_FW_BOOT_PREPARE); 112 113 /* load the firmware */ 114 ret = snd_sof_load_firmware(sdev); 115 if (ret < 0) { 116 dev_err(sdev->dev, 117 "error: failed to load DSP firmware after resume %d\n", 118 ret); 119 sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED); 120 return ret; 121 } 122 123 sof_set_fw_state(sdev, SOF_FW_BOOT_IN_PROGRESS); 124 125 /* 126 * Boot the firmware. The FW boot status will be modified 127 * in snd_sof_run_firmware() depending on the outcome. 128 */ 129 ret = snd_sof_run_firmware(sdev); 130 if (ret < 0) { 131 dev_err(sdev->dev, 132 "error: failed to boot DSP firmware after resume %d\n", 133 ret); 134 sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED); 135 return ret; 136 } 137 138 /* resume DMA trace, only need send ipc */ 139 ret = snd_sof_init_trace_ipc(sdev); 140 if (ret < 0) { 141 /* non fatal */ 142 dev_warn(sdev->dev, 143 "warning: failed to init trace after resume %d\n", 144 ret); 145 } 146 147 /* restore pipelines */ 148 if (tplg_ops->set_up_all_pipelines) { 149 ret = tplg_ops->set_up_all_pipelines(sdev, false); 150 if (ret < 0) { 151 dev_err(sdev->dev, "Failed to restore pipeline after resume %d\n", ret); 152 return ret; 153 } 154 } 155 156 /* Notify clients not managed by pm framework about core resume */ 157 sof_resume_clients(sdev); 158 159 /* notify DSP of system resume */ 160 if (pm_ops && pm_ops->ctx_restore) { 161 ret = pm_ops->ctx_restore(sdev); 162 if (ret < 0) 163 dev_err(sdev->dev, "ctx_restore IPC error during resume: %d\n", ret); 164 } 165 166 return ret; 167 } 168 169 static int sof_suspend(struct device *dev, bool runtime_suspend) 170 { 171 struct snd_sof_dev *sdev = dev_get_drvdata(dev); 172 const struct sof_ipc_pm_ops *pm_ops = sdev->ipc->ops->pm; 173 const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg; 174 pm_message_t pm_state; 175 u32 target_state = 0; 176 int ret; 177 178 /* do nothing if dsp suspend callback is not set */ 179 if (!runtime_suspend && !sof_ops(sdev)->suspend) 180 return 0; 181 182 if (runtime_suspend && !sof_ops(sdev)->runtime_suspend) 183 return 0; 184 185 if (sdev->fw_state != SOF_FW_BOOT_COMPLETE) 186 goto suspend; 187 188 /* prepare for streams to be resumed properly upon resume */ 189 if (!runtime_suspend) { 190 ret = sof_set_hw_params_upon_resume(sdev->dev); 191 if (ret < 0) { 192 dev_err(sdev->dev, 193 "error: setting hw_params flag during suspend %d\n", 194 ret); 195 return ret; 196 } 197 } 198 199 target_state = snd_sof_dsp_power_target(sdev); 200 pm_state.event = target_state; 201 202 /* Skip to platform-specific suspend if DSP is entering D0 */ 203 if (target_state == SOF_DSP_PM_D0) { 204 /* Notify clients not managed by pm framework about core suspend */ 205 sof_suspend_clients(sdev, pm_state); 206 goto suspend; 207 } 208 209 if (tplg_ops->tear_down_all_pipelines) 210 tplg_ops->tear_down_all_pipelines(sdev, false); 211 212 /* release trace */ 213 snd_sof_release_trace(sdev); 214 215 /* Notify clients not managed by pm framework about core suspend */ 216 sof_suspend_clients(sdev, pm_state); 217 218 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE) 219 /* cache debugfs contents during runtime suspend */ 220 if (runtime_suspend) 221 sof_cache_debugfs(sdev); 222 #endif 223 /* notify DSP of upcoming power down */ 224 if (pm_ops && pm_ops->ctx_save) { 225 ret = pm_ops->ctx_save(sdev); 226 if (ret == -EBUSY || ret == -EAGAIN) { 227 /* 228 * runtime PM has logic to handle -EBUSY/-EAGAIN so 229 * pass these errors up 230 */ 231 dev_err(sdev->dev, "ctx_save IPC error during suspend: %d\n", ret); 232 return ret; 233 } else if (ret < 0) { 234 /* FW in unexpected state, continue to power down */ 235 dev_warn(sdev->dev, "ctx_save IPC error: %d, proceeding with suspend\n", 236 ret); 237 } 238 } 239 240 suspend: 241 242 /* return if the DSP was not probed successfully */ 243 if (sdev->fw_state == SOF_FW_BOOT_NOT_STARTED) 244 return 0; 245 246 /* platform-specific suspend */ 247 if (runtime_suspend) 248 ret = snd_sof_dsp_runtime_suspend(sdev); 249 else 250 ret = snd_sof_dsp_suspend(sdev, target_state); 251 if (ret < 0) 252 dev_err(sdev->dev, 253 "error: failed to power down DSP during suspend %d\n", 254 ret); 255 256 /* Do not reset FW state if DSP is in D0 */ 257 if (target_state == SOF_DSP_PM_D0) 258 return ret; 259 260 /* reset FW state */ 261 sof_set_fw_state(sdev, SOF_FW_BOOT_NOT_STARTED); 262 sdev->enabled_cores_mask = 0; 263 264 return ret; 265 } 266 267 int snd_sof_dsp_power_down_notify(struct snd_sof_dev *sdev) 268 { 269 const struct sof_ipc_pm_ops *pm_ops = sdev->ipc->ops->pm; 270 271 /* Notify DSP of upcoming power down */ 272 if (sof_ops(sdev)->remove && pm_ops && pm_ops->ctx_save) 273 return pm_ops->ctx_save(sdev); 274 275 return 0; 276 } 277 278 int snd_sof_runtime_suspend(struct device *dev) 279 { 280 return sof_suspend(dev, true); 281 } 282 EXPORT_SYMBOL(snd_sof_runtime_suspend); 283 284 int snd_sof_runtime_idle(struct device *dev) 285 { 286 struct snd_sof_dev *sdev = dev_get_drvdata(dev); 287 288 return snd_sof_dsp_runtime_idle(sdev); 289 } 290 EXPORT_SYMBOL(snd_sof_runtime_idle); 291 292 int snd_sof_runtime_resume(struct device *dev) 293 { 294 return sof_resume(dev, true); 295 } 296 EXPORT_SYMBOL(snd_sof_runtime_resume); 297 298 int snd_sof_resume(struct device *dev) 299 { 300 return sof_resume(dev, false); 301 } 302 EXPORT_SYMBOL(snd_sof_resume); 303 304 int snd_sof_suspend(struct device *dev) 305 { 306 return sof_suspend(dev, false); 307 } 308 EXPORT_SYMBOL(snd_sof_suspend); 309 310 int snd_sof_prepare(struct device *dev) 311 { 312 struct snd_sof_dev *sdev = dev_get_drvdata(dev); 313 const struct sof_dev_desc *desc = sdev->pdata->desc; 314 315 /* will suspend to S3 by default */ 316 sdev->system_suspend_target = SOF_SUSPEND_S3; 317 318 /* 319 * if the firmware is crashed or boot failed then we try to aim for S3 320 * to reboot the firmware 321 */ 322 if (sdev->fw_state == SOF_FW_CRASHED || 323 sdev->fw_state == SOF_FW_BOOT_FAILED) 324 return 0; 325 326 if (!desc->use_acpi_target_states) 327 return 0; 328 329 #if defined(CONFIG_ACPI) 330 if (acpi_target_system_state() == ACPI_STATE_S0) 331 sdev->system_suspend_target = SOF_SUSPEND_S0IX; 332 #endif 333 334 return 0; 335 } 336 EXPORT_SYMBOL(snd_sof_prepare); 337 338 void snd_sof_complete(struct device *dev) 339 { 340 struct snd_sof_dev *sdev = dev_get_drvdata(dev); 341 342 sdev->system_suspend_target = SOF_SUSPEND_NONE; 343 } 344 EXPORT_SYMBOL(snd_sof_complete); 345