1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020-2023 Intel Corporation
6 #include <linux/highmem.h>
7 #include <linux/moduleparam.h>
9 #include <linux/pm_runtime.h>
10 #include <linux/reboot.h>
12 #include "vpu_boot_api.h"
21 static bool ivpu_disable_recovery;
22 module_param_named_unsafe(disable_recovery, ivpu_disable_recovery, bool, 0644);
23 MODULE_PARM_DESC(disable_recovery, "Disables recovery when VPU hang is detected");
25 #define PM_RESCHEDULE_LIMIT 5
27 static void ivpu_pm_prepare_cold_boot(struct ivpu_device *vdev)
29 struct ivpu_fw_info *fw = vdev->fw;
31 ivpu_cmdq_reset_all_contexts(vdev);
34 fw->entry_point = fw->cold_boot_entry_point;
37 static void ivpu_pm_prepare_warm_boot(struct ivpu_device *vdev)
39 struct ivpu_fw_info *fw = vdev->fw;
40 struct vpu_boot_params *bp = fw->mem->kvaddr;
42 if (!bp->save_restore_ret_address) {
43 ivpu_pm_prepare_cold_boot(vdev);
47 ivpu_dbg(vdev, FW_BOOT, "Save/restore entry point %llx", bp->save_restore_ret_address);
48 fw->entry_point = bp->save_restore_ret_address;
51 static int ivpu_suspend(struct ivpu_device *vdev)
55 ret = ivpu_shutdown(vdev);
57 ivpu_err(vdev, "Failed to shutdown VPU: %d\n", ret);
64 static int ivpu_resume(struct ivpu_device *vdev)
69 ret = ivpu_hw_power_up(vdev);
71 ivpu_err(vdev, "Failed to power up HW: %d\n", ret);
75 ret = ivpu_mmu_enable(vdev);
77 ivpu_err(vdev, "Failed to resume MMU: %d\n", ret);
78 ivpu_hw_power_down(vdev);
82 ret = ivpu_boot(vdev);
84 ivpu_mmu_disable(vdev);
85 ivpu_hw_power_down(vdev);
86 if (!ivpu_fw_is_cold_boot(vdev)) {
87 ivpu_warn(vdev, "Failed to resume the FW: %d. Retrying cold boot..\n", ret);
88 ivpu_pm_prepare_cold_boot(vdev);
91 ivpu_err(vdev, "Failed to resume the FW: %d\n", ret);
98 static void ivpu_pm_recovery_work(struct work_struct *work)
100 struct ivpu_pm_info *pm = container_of(work, struct ivpu_pm_info, recovery_work);
101 struct ivpu_device *vdev = pm->vdev;
102 char *evt[2] = {"IVPU_PM_EVENT=IVPU_RECOVER", NULL};
106 ret = pci_try_reset_function(to_pci_dev(vdev->drm.dev));
107 if (ret == -EAGAIN && !drm_dev_is_unplugged(&vdev->drm)) {
112 if (ret && ret != -EAGAIN)
113 ivpu_err(vdev, "Failed to reset VPU: %d\n", ret);
115 kobject_uevent_env(&vdev->drm.dev->kobj, KOBJ_CHANGE, evt);
118 void ivpu_pm_schedule_recovery(struct ivpu_device *vdev)
120 struct ivpu_pm_info *pm = vdev->pm;
122 if (ivpu_disable_recovery) {
123 ivpu_err(vdev, "Recovery not available when disable_recovery param is set\n");
127 if (ivpu_is_fpga(vdev)) {
128 ivpu_err(vdev, "Recovery not available on FPGA\n");
132 /* Schedule recovery if it's not in progress */
133 if (atomic_cmpxchg(&pm->in_reset, 0, 1) == 0) {
134 ivpu_hw_irq_disable(vdev);
135 queue_work(system_long_wq, &pm->recovery_work);
139 int ivpu_pm_suspend_cb(struct device *dev)
141 struct drm_device *drm = dev_get_drvdata(dev);
142 struct ivpu_device *vdev = to_ivpu_device(drm);
143 unsigned long timeout;
145 ivpu_dbg(vdev, PM, "Suspend..\n");
147 timeout = jiffies + msecs_to_jiffies(vdev->timeout.tdr);
148 while (!ivpu_hw_is_idle(vdev)) {
150 if (time_after_eq(jiffies, timeout)) {
151 ivpu_err(vdev, "Failed to enter idle on system suspend\n");
157 ivpu_pm_prepare_warm_boot(vdev);
159 pci_save_state(to_pci_dev(dev));
160 pci_set_power_state(to_pci_dev(dev), PCI_D3hot);
162 ivpu_dbg(vdev, PM, "Suspend done.\n");
167 int ivpu_pm_resume_cb(struct device *dev)
169 struct drm_device *drm = dev_get_drvdata(dev);
170 struct ivpu_device *vdev = to_ivpu_device(drm);
173 ivpu_dbg(vdev, PM, "Resume..\n");
175 pci_set_power_state(to_pci_dev(dev), PCI_D0);
176 pci_restore_state(to_pci_dev(dev));
178 ret = ivpu_resume(vdev);
180 ivpu_err(vdev, "Failed to resume: %d\n", ret);
182 ivpu_dbg(vdev, PM, "Resume done.\n");
187 int ivpu_pm_runtime_suspend_cb(struct device *dev)
189 struct drm_device *drm = dev_get_drvdata(dev);
190 struct ivpu_device *vdev = to_ivpu_device(drm);
193 ivpu_dbg(vdev, PM, "Runtime suspend..\n");
195 if (!ivpu_hw_is_idle(vdev) && vdev->pm->suspend_reschedule_counter) {
196 ivpu_dbg(vdev, PM, "Failed to enter idle, rescheduling suspend, retries left %d\n",
197 vdev->pm->suspend_reschedule_counter);
198 pm_schedule_suspend(dev, vdev->timeout.reschedule_suspend);
199 vdev->pm->suspend_reschedule_counter--;
203 ret = ivpu_suspend(vdev);
205 ivpu_err(vdev, "Failed to set suspend VPU: %d\n", ret);
207 if (!vdev->pm->suspend_reschedule_counter) {
208 ivpu_warn(vdev, "VPU failed to enter idle, force suspended.\n");
209 ivpu_pm_prepare_cold_boot(vdev);
211 ivpu_pm_prepare_warm_boot(vdev);
214 vdev->pm->suspend_reschedule_counter = PM_RESCHEDULE_LIMIT;
216 ivpu_dbg(vdev, PM, "Runtime suspend done.\n");
221 int ivpu_pm_runtime_resume_cb(struct device *dev)
223 struct drm_device *drm = dev_get_drvdata(dev);
224 struct ivpu_device *vdev = to_ivpu_device(drm);
227 ivpu_dbg(vdev, PM, "Runtime resume..\n");
229 ret = ivpu_resume(vdev);
231 ivpu_err(vdev, "Failed to set RESUME state: %d\n", ret);
233 ivpu_dbg(vdev, PM, "Runtime resume done.\n");
238 int ivpu_rpm_get(struct ivpu_device *vdev)
242 ret = pm_runtime_resume_and_get(vdev->drm.dev);
243 if (!drm_WARN_ON(&vdev->drm, ret < 0))
244 vdev->pm->suspend_reschedule_counter = PM_RESCHEDULE_LIMIT;
249 void ivpu_rpm_put(struct ivpu_device *vdev)
251 pm_runtime_mark_last_busy(vdev->drm.dev);
252 pm_runtime_put_autosuspend(vdev->drm.dev);
255 void ivpu_pm_reset_prepare_cb(struct pci_dev *pdev)
257 struct ivpu_device *vdev = pci_get_drvdata(pdev);
259 pm_runtime_get_sync(vdev->drm.dev);
261 ivpu_dbg(vdev, PM, "Pre-reset..\n");
262 atomic_inc(&vdev->pm->reset_counter);
263 atomic_set(&vdev->pm->in_reset, 1);
265 ivpu_pm_prepare_cold_boot(vdev);
266 ivpu_jobs_abort_all(vdev);
267 ivpu_dbg(vdev, PM, "Pre-reset done.\n");
270 void ivpu_pm_reset_done_cb(struct pci_dev *pdev)
272 struct ivpu_device *vdev = pci_get_drvdata(pdev);
275 ivpu_dbg(vdev, PM, "Post-reset..\n");
276 ret = ivpu_resume(vdev);
278 ivpu_err(vdev, "Failed to set RESUME state: %d\n", ret);
279 atomic_set(&vdev->pm->in_reset, 0);
280 ivpu_dbg(vdev, PM, "Post-reset done.\n");
282 pm_runtime_put_autosuspend(vdev->drm.dev);
285 int ivpu_pm_init(struct ivpu_device *vdev)
287 struct device *dev = vdev->drm.dev;
288 struct ivpu_pm_info *pm = vdev->pm;
291 pm->suspend_reschedule_counter = PM_RESCHEDULE_LIMIT;
293 atomic_set(&pm->in_reset, 0);
294 INIT_WORK(&pm->recovery_work, ivpu_pm_recovery_work);
296 pm_runtime_use_autosuspend(dev);
298 if (ivpu_disable_recovery)
299 pm_runtime_set_autosuspend_delay(dev, -1);
300 else if (ivpu_is_silicon(vdev))
301 pm_runtime_set_autosuspend_delay(dev, 100);
303 pm_runtime_set_autosuspend_delay(dev, 60000);
308 void ivpu_pm_cancel_recovery(struct ivpu_device *vdev)
310 cancel_work_sync(&vdev->pm->recovery_work);
313 void ivpu_pm_enable(struct ivpu_device *vdev)
315 struct device *dev = vdev->drm.dev;
317 pm_runtime_set_active(dev);
318 pm_runtime_allow(dev);
319 pm_runtime_mark_last_busy(dev);
320 pm_runtime_put_autosuspend(dev);
323 void ivpu_pm_disable(struct ivpu_device *vdev)
325 pm_runtime_get_noresume(vdev->drm.dev);
326 pm_runtime_forbid(vdev->drm.dev);