Liam Girdwood | 8920153c | 2019-04-12 11:05:16 -0500 | [diff] [blame] | 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 | |
| 11 | #include "ops.h" |
| 12 | #include "sof-priv.h" |
Ranjani Sridharan | ee1e79b | 2019-12-04 15:15:51 -0600 | [diff] [blame] | 13 | #include "sof-audio.h" |
Liam Girdwood | 8920153c | 2019-04-12 11:05:16 -0500 | [diff] [blame] | 14 | |
Keyon Jie | 7c7eba2 | 2019-10-25 17:41:08 -0500 | [diff] [blame] | 15 | static int sof_send_pm_ctx_ipc(struct snd_sof_dev *sdev, int cmd) |
Liam Girdwood | 8920153c | 2019-04-12 11:05:16 -0500 | [diff] [blame] | 16 | { |
| 17 | struct sof_ipc_pm_ctx pm_ctx; |
| 18 | struct sof_ipc_reply reply; |
| 19 | |
| 20 | memset(&pm_ctx, 0, sizeof(pm_ctx)); |
| 21 | |
| 22 | /* configure ctx save ipc message */ |
| 23 | pm_ctx.hdr.size = sizeof(pm_ctx); |
| 24 | pm_ctx.hdr.cmd = SOF_IPC_GLB_PM_MSG | cmd; |
| 25 | |
| 26 | /* send ctx save ipc to dsp */ |
| 27 | return sof_ipc_tx_message(sdev->ipc, pm_ctx.hdr.cmd, &pm_ctx, |
| 28 | sizeof(pm_ctx), &reply, sizeof(reply)); |
| 29 | } |
| 30 | |
Liam Girdwood | 8920153c | 2019-04-12 11:05:16 -0500 | [diff] [blame] | 31 | #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE) |
| 32 | static void sof_cache_debugfs(struct snd_sof_dev *sdev) |
| 33 | { |
| 34 | struct snd_sof_dfsentry *dfse; |
| 35 | |
| 36 | list_for_each_entry(dfse, &sdev->dfsentry_list, list) { |
| 37 | |
| 38 | /* nothing to do if debugfs buffer is not IO mem */ |
| 39 | if (dfse->type == SOF_DFSENTRY_TYPE_BUF) |
| 40 | continue; |
| 41 | |
| 42 | /* cache memory that is only accessible in D0 */ |
| 43 | if (dfse->access_type == SOF_DEBUGFS_ACCESS_D0_ONLY) |
| 44 | memcpy_fromio(dfse->cache_buf, dfse->io_mem, |
| 45 | dfse->size); |
| 46 | } |
| 47 | } |
| 48 | #endif |
| 49 | |
| 50 | static int sof_resume(struct device *dev, bool runtime_resume) |
| 51 | { |
| 52 | struct snd_sof_dev *sdev = dev_get_drvdata(dev); |
Ranjani Sridharan | fb9a811 | 2020-01-29 16:07:19 -0600 | [diff] [blame^] | 53 | enum sof_d0_substate old_d0_substate = sdev->d0_substate; |
Liam Girdwood | 8920153c | 2019-04-12 11:05:16 -0500 | [diff] [blame] | 54 | int ret; |
| 55 | |
| 56 | /* do nothing if dsp resume callbacks are not set */ |
| 57 | if (!sof_ops(sdev)->resume || !sof_ops(sdev)->runtime_resume) |
| 58 | return 0; |
| 59 | |
Pierre-Louis Bossart | 410e5e5 | 2020-01-24 15:36:21 -0600 | [diff] [blame] | 60 | /* DSP was never successfully started, nothing to resume */ |
| 61 | if (sdev->first_boot) |
| 62 | return 0; |
| 63 | |
Ranjani Sridharan | fb9a811 | 2020-01-29 16:07:19 -0600 | [diff] [blame^] | 64 | /* resume from D0I3 */ |
| 65 | if (!runtime_resume && old_d0_substate == SOF_DSP_D0I3) { |
| 66 | ret = snd_sof_set_d0_substate(sdev, SOF_DSP_D0I0); |
| 67 | if (ret < 0 && ret != -ENOTSUPP) { |
| 68 | dev_err(sdev->dev, |
| 69 | "error: failed to resume from D0I3 %d\n", |
| 70 | ret); |
| 71 | return ret; |
| 72 | } |
| 73 | } |
| 74 | |
Liam Girdwood | 8920153c | 2019-04-12 11:05:16 -0500 | [diff] [blame] | 75 | /* |
| 76 | * if the runtime_resume flag is set, call the runtime_resume routine |
| 77 | * or else call the system resume routine |
| 78 | */ |
| 79 | if (runtime_resume) |
| 80 | ret = snd_sof_dsp_runtime_resume(sdev); |
| 81 | else |
| 82 | ret = snd_sof_dsp_resume(sdev); |
| 83 | if (ret < 0) { |
| 84 | dev_err(sdev->dev, |
| 85 | "error: failed to power up DSP after resume\n"); |
| 86 | return ret; |
| 87 | } |
| 88 | |
Ranjani Sridharan | fb9a811 | 2020-01-29 16:07:19 -0600 | [diff] [blame^] | 89 | /* Nothing further to do if resuming from D0I3 */ |
| 90 | if (!runtime_resume && old_d0_substate == SOF_DSP_D0I3) |
| 91 | return 0; |
| 92 | |
Ranjani Sridharan | 6ca5cec | 2019-12-17 18:26:09 -0600 | [diff] [blame] | 93 | sdev->fw_state = SOF_FW_BOOT_PREPARE; |
| 94 | |
Liam Girdwood | 8920153c | 2019-04-12 11:05:16 -0500 | [diff] [blame] | 95 | /* load the firmware */ |
| 96 | ret = snd_sof_load_firmware(sdev); |
| 97 | if (ret < 0) { |
| 98 | dev_err(sdev->dev, |
| 99 | "error: failed to load DSP firmware after resume %d\n", |
| 100 | ret); |
| 101 | return ret; |
| 102 | } |
| 103 | |
Ranjani Sridharan | 6ca5cec | 2019-12-17 18:26:09 -0600 | [diff] [blame] | 104 | sdev->fw_state = SOF_FW_BOOT_IN_PROGRESS; |
| 105 | |
| 106 | /* |
| 107 | * Boot the firmware. The FW boot status will be modified |
| 108 | * in snd_sof_run_firmware() depending on the outcome. |
| 109 | */ |
Liam Girdwood | 8920153c | 2019-04-12 11:05:16 -0500 | [diff] [blame] | 110 | ret = snd_sof_run_firmware(sdev); |
| 111 | if (ret < 0) { |
| 112 | dev_err(sdev->dev, |
| 113 | "error: failed to boot DSP firmware after resume %d\n", |
| 114 | ret); |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | /* resume DMA trace, only need send ipc */ |
| 119 | ret = snd_sof_init_trace_ipc(sdev); |
| 120 | if (ret < 0) { |
| 121 | /* non fatal */ |
| 122 | dev_warn(sdev->dev, |
| 123 | "warning: failed to init trace after resume %d\n", |
| 124 | ret); |
| 125 | } |
| 126 | |
| 127 | /* restore pipelines */ |
Ranjani Sridharan | ee1e79b | 2019-12-04 15:15:51 -0600 | [diff] [blame] | 128 | ret = sof_restore_pipelines(sdev->dev); |
Liam Girdwood | 8920153c | 2019-04-12 11:05:16 -0500 | [diff] [blame] | 129 | if (ret < 0) { |
| 130 | dev_err(sdev->dev, |
| 131 | "error: failed to restore pipeline after resume %d\n", |
| 132 | ret); |
| 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | /* notify DSP of system resume */ |
Keyon Jie | 7c7eba2 | 2019-10-25 17:41:08 -0500 | [diff] [blame] | 137 | ret = sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_RESTORE); |
Liam Girdwood | 8920153c | 2019-04-12 11:05:16 -0500 | [diff] [blame] | 138 | if (ret < 0) |
| 139 | dev_err(sdev->dev, |
| 140 | "error: ctx_restore ipc error during resume %d\n", |
| 141 | ret); |
| 142 | |
Keyon Jie | 09fe6b5 | 2019-10-25 17:40:58 -0500 | [diff] [blame] | 143 | /* initialize default D0 sub-state */ |
| 144 | sdev->d0_substate = SOF_DSP_D0I0; |
| 145 | |
Liam Girdwood | 8920153c | 2019-04-12 11:05:16 -0500 | [diff] [blame] | 146 | return ret; |
| 147 | } |
| 148 | |
| 149 | static int sof_suspend(struct device *dev, bool runtime_suspend) |
| 150 | { |
| 151 | struct snd_sof_dev *sdev = dev_get_drvdata(dev); |
| 152 | int ret; |
| 153 | |
| 154 | /* do nothing if dsp suspend callback is not set */ |
| 155 | if (!sof_ops(sdev)->suspend) |
| 156 | return 0; |
| 157 | |
Ranjani Sridharan | 6ca5cec | 2019-12-17 18:26:09 -0600 | [diff] [blame] | 158 | if (sdev->fw_state != SOF_FW_BOOT_COMPLETE) |
Ranjani Sridharan | fb9a811 | 2020-01-29 16:07:19 -0600 | [diff] [blame^] | 159 | goto suspend; |
Liam Girdwood | 8920153c | 2019-04-12 11:05:16 -0500 | [diff] [blame] | 160 | |
| 161 | /* set restore_stream for all streams during system suspend */ |
Ranjani Sridharan | 7077a07 | 2019-06-12 12:23:38 -0500 | [diff] [blame] | 162 | if (!runtime_suspend) { |
Ranjani Sridharan | ee1e79b | 2019-12-04 15:15:51 -0600 | [diff] [blame] | 163 | ret = sof_set_hw_params_upon_resume(sdev->dev); |
Ranjani Sridharan | 7077a07 | 2019-06-12 12:23:38 -0500 | [diff] [blame] | 164 | if (ret < 0) { |
| 165 | dev_err(sdev->dev, |
| 166 | "error: setting hw_params flag during suspend %d\n", |
| 167 | ret); |
| 168 | return ret; |
| 169 | } |
| 170 | } |
Liam Girdwood | 8920153c | 2019-04-12 11:05:16 -0500 | [diff] [blame] | 171 | |
Ranjani Sridharan | fb9a811 | 2020-01-29 16:07:19 -0600 | [diff] [blame^] | 172 | if (snd_sof_dsp_d0i3_on_suspend(sdev)) { |
| 173 | /* suspend to D0i3 */ |
| 174 | ret = snd_sof_set_d0_substate(sdev, SOF_DSP_D0I3); |
| 175 | if (ret < 0) { |
| 176 | dev_err(sdev->dev, "error: failed to enter D0I3, %d\n", |
| 177 | ret); |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | /* Skip to platform-specific suspend if DSP is entering D0I3 */ |
| 182 | goto suspend; |
| 183 | } |
| 184 | |
| 185 | /* release trace */ |
| 186 | snd_sof_release_trace(sdev); |
| 187 | |
Liam Girdwood | 8920153c | 2019-04-12 11:05:16 -0500 | [diff] [blame] | 188 | #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE) |
| 189 | /* cache debugfs contents during runtime suspend */ |
| 190 | if (runtime_suspend) |
| 191 | sof_cache_debugfs(sdev); |
| 192 | #endif |
| 193 | /* notify DSP of upcoming power down */ |
Keyon Jie | 7c7eba2 | 2019-10-25 17:41:08 -0500 | [diff] [blame] | 194 | ret = sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_SAVE); |
Kai Vehmanen | e2eba55 | 2019-06-12 11:57:04 -0500 | [diff] [blame] | 195 | if (ret == -EBUSY || ret == -EAGAIN) { |
| 196 | /* |
| 197 | * runtime PM has logic to handle -EBUSY/-EAGAIN so |
| 198 | * pass these errors up |
| 199 | */ |
Liam Girdwood | 8920153c | 2019-04-12 11:05:16 -0500 | [diff] [blame] | 200 | dev_err(sdev->dev, |
| 201 | "error: ctx_save ipc error during suspend %d\n", |
| 202 | ret); |
| 203 | return ret; |
Kai Vehmanen | e2eba55 | 2019-06-12 11:57:04 -0500 | [diff] [blame] | 204 | } else if (ret < 0) { |
| 205 | /* FW in unexpected state, continue to power down */ |
| 206 | dev_warn(sdev->dev, |
| 207 | "ctx_save ipc error %d, proceeding with suspend\n", |
| 208 | ret); |
Liam Girdwood | 8920153c | 2019-04-12 11:05:16 -0500 | [diff] [blame] | 209 | } |
| 210 | |
Ranjani Sridharan | fb9a811 | 2020-01-29 16:07:19 -0600 | [diff] [blame^] | 211 | suspend: |
Ranjani Sridharan | 6ca5cec | 2019-12-17 18:26:09 -0600 | [diff] [blame] | 212 | |
| 213 | /* return if the DSP was not probed successfully */ |
| 214 | if (sdev->fw_state == SOF_FW_BOOT_NOT_STARTED) |
| 215 | return 0; |
| 216 | |
Ranjani Sridharan | fb9a811 | 2020-01-29 16:07:19 -0600 | [diff] [blame^] | 217 | /* platform-specific suspend */ |
Liam Girdwood | 8920153c | 2019-04-12 11:05:16 -0500 | [diff] [blame] | 218 | if (runtime_suspend) |
Fred Oh | 1c38c92 | 2019-07-22 09:13:50 -0500 | [diff] [blame] | 219 | ret = snd_sof_dsp_runtime_suspend(sdev); |
Liam Girdwood | 8920153c | 2019-04-12 11:05:16 -0500 | [diff] [blame] | 220 | else |
Fred Oh | 1c38c92 | 2019-07-22 09:13:50 -0500 | [diff] [blame] | 221 | ret = snd_sof_dsp_suspend(sdev); |
Liam Girdwood | 8920153c | 2019-04-12 11:05:16 -0500 | [diff] [blame] | 222 | if (ret < 0) |
| 223 | dev_err(sdev->dev, |
| 224 | "error: failed to power down DSP during suspend %d\n", |
| 225 | ret); |
| 226 | |
Ranjani Sridharan | fb9a811 | 2020-01-29 16:07:19 -0600 | [diff] [blame^] | 227 | /* Do not reset FW state if DSP is in D0I3 */ |
| 228 | if (sdev->d0_substate == SOF_DSP_D0I3) |
| 229 | return ret; |
| 230 | |
Ranjani Sridharan | 6ca5cec | 2019-12-17 18:26:09 -0600 | [diff] [blame] | 231 | /* reset FW state */ |
| 232 | sdev->fw_state = SOF_FW_BOOT_NOT_STARTED; |
| 233 | |
Liam Girdwood | 8920153c | 2019-04-12 11:05:16 -0500 | [diff] [blame] | 234 | return ret; |
| 235 | } |
| 236 | |
| 237 | int snd_sof_runtime_suspend(struct device *dev) |
| 238 | { |
| 239 | return sof_suspend(dev, true); |
| 240 | } |
| 241 | EXPORT_SYMBOL(snd_sof_runtime_suspend); |
| 242 | |
Kai Vehmanen | 62fde97 | 2019-07-02 16:24:27 +0300 | [diff] [blame] | 243 | int snd_sof_runtime_idle(struct device *dev) |
| 244 | { |
| 245 | struct snd_sof_dev *sdev = dev_get_drvdata(dev); |
| 246 | |
| 247 | return snd_sof_dsp_runtime_idle(sdev); |
| 248 | } |
| 249 | EXPORT_SYMBOL(snd_sof_runtime_idle); |
| 250 | |
Liam Girdwood | 8920153c | 2019-04-12 11:05:16 -0500 | [diff] [blame] | 251 | int snd_sof_runtime_resume(struct device *dev) |
| 252 | { |
| 253 | return sof_resume(dev, true); |
| 254 | } |
| 255 | EXPORT_SYMBOL(snd_sof_runtime_resume); |
| 256 | |
Keyon Jie | 6012528 | 2019-10-25 17:41:13 -0500 | [diff] [blame] | 257 | int snd_sof_set_d0_substate(struct snd_sof_dev *sdev, |
| 258 | enum sof_d0_substate d0_substate) |
| 259 | { |
| 260 | int ret; |
| 261 | |
Keyon Jie | 58a972e | 2019-11-11 16:33:41 -0600 | [diff] [blame] | 262 | if (sdev->d0_substate == d0_substate) |
| 263 | return 0; |
| 264 | |
Keyon Jie | 6012528 | 2019-10-25 17:41:13 -0500 | [diff] [blame] | 265 | /* do platform specific set_state */ |
| 266 | ret = snd_sof_dsp_set_power_state(sdev, d0_substate); |
| 267 | if (ret < 0) |
| 268 | return ret; |
| 269 | |
| 270 | /* update dsp D0 sub-state */ |
| 271 | sdev->d0_substate = d0_substate; |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | EXPORT_SYMBOL(snd_sof_set_d0_substate); |
| 276 | |
Keyon Jie | 74b4dd0 | 2019-11-11 16:33:40 -0600 | [diff] [blame] | 277 | /* |
| 278 | * Audio DSP states may transform as below:- |
| 279 | * |
| 280 | * D0I3 compatible stream |
| 281 | * Runtime +---------------------+ opened only, timeout |
| 282 | * suspend | +--------------------+ |
| 283 | * +------------+ D0(active) | | |
| 284 | * | | <---------------+ | |
| 285 | * | +--------> | | | |
| 286 | * | |Runtime +--^--+---------^--+--+ The last | | |
| 287 | * | |resume | | | | opened D0I3 | | |
| 288 | * | | | | | | compatible | | |
| 289 | * | | resume| | | | stream closed | | |
| 290 | * | | from | | D3 | | | | |
| 291 | * | | D3 | |suspend | | d0i3 | | |
| 292 | * | | | | | |suspend | | |
| 293 | * | | | | | | | | |
| 294 | * | | | | | | | | |
| 295 | * +-v---+-----------+--v-------+ | | +------+----v----+ |
| 296 | * | | | +-----------> | |
| 297 | * | D3 (suspended) | | | D0I3 +-----+ |
| 298 | * | | +--------------+ | | |
| 299 | * | | resume from | | | |
| 300 | * +-------------------^--------+ d0i3 suspend +----------------+ | |
| 301 | * | | |
| 302 | * | D3 suspend | |
| 303 | * +------------------------------------------------+ |
| 304 | * |
| 305 | * d0i3_suspend = s0_suspend && D0I3 stream opened, |
| 306 | * D3 suspend = !d0i3_suspend, |
| 307 | */ |
| 308 | |
Liam Girdwood | 8920153c | 2019-04-12 11:05:16 -0500 | [diff] [blame] | 309 | int snd_sof_resume(struct device *dev) |
| 310 | { |
| 311 | return sof_resume(dev, false); |
| 312 | } |
| 313 | EXPORT_SYMBOL(snd_sof_resume); |
| 314 | |
| 315 | int snd_sof_suspend(struct device *dev) |
| 316 | { |
| 317 | return sof_suspend(dev, false); |
| 318 | } |
| 319 | EXPORT_SYMBOL(snd_sof_suspend); |
Keyon Jie | 0b50b3b | 2019-10-25 17:41:17 -0500 | [diff] [blame] | 320 | |
| 321 | int snd_sof_prepare(struct device *dev) |
| 322 | { |
| 323 | struct snd_sof_dev *sdev = dev_get_drvdata(dev); |
| 324 | |
| 325 | #if defined(CONFIG_ACPI) |
| 326 | sdev->s0_suspend = acpi_target_system_state() == ACPI_STATE_S0; |
| 327 | #else |
| 328 | /* will suspend to S3 by default */ |
| 329 | sdev->s0_suspend = false; |
| 330 | #endif |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | EXPORT_SYMBOL(snd_sof_prepare); |
| 335 | |
| 336 | void snd_sof_complete(struct device *dev) |
| 337 | { |
| 338 | struct snd_sof_dev *sdev = dev_get_drvdata(dev); |
| 339 | |
| 340 | sdev->s0_suspend = false; |
| 341 | } |
| 342 | EXPORT_SYMBOL(snd_sof_complete); |