xref: /linux/drivers/accel/ivpu/ivpu_pm.c (revision 27d19268cf394f2c78db732be0cb31852eeadb0a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2020-2023 Intel Corporation
4  */
5 
6 #include <linux/highmem.h>
7 #include <linux/moduleparam.h>
8 #include <linux/pci.h>
9 #include <linux/pm_runtime.h>
10 #include <linux/reboot.h>
11 
12 #include "vpu_boot_api.h"
13 #include "ivpu_drv.h"
14 #include "ivpu_hw.h"
15 #include "ivpu_fw.h"
16 #include "ivpu_fw_log.h"
17 #include "ivpu_ipc.h"
18 #include "ivpu_job.h"
19 #include "ivpu_jsm_msg.h"
20 #include "ivpu_mmu.h"
21 #include "ivpu_pm.h"
22 
23 static bool ivpu_disable_recovery;
24 module_param_named_unsafe(disable_recovery, ivpu_disable_recovery, bool, 0644);
25 MODULE_PARM_DESC(disable_recovery, "Disables recovery when VPU hang is detected");
26 
27 static unsigned long ivpu_tdr_timeout_ms;
28 module_param_named(tdr_timeout_ms, ivpu_tdr_timeout_ms, ulong, 0644);
29 MODULE_PARM_DESC(tdr_timeout_ms, "Timeout for device hang detection, in milliseconds, 0 - default");
30 
31 #define PM_RESCHEDULE_LIMIT     5
32 
33 static void ivpu_pm_prepare_cold_boot(struct ivpu_device *vdev)
34 {
35 	struct ivpu_fw_info *fw = vdev->fw;
36 
37 	ivpu_cmdq_reset_all_contexts(vdev);
38 	ivpu_ipc_reset(vdev);
39 	ivpu_fw_load(vdev);
40 	fw->entry_point = fw->cold_boot_entry_point;
41 }
42 
43 static void ivpu_pm_prepare_warm_boot(struct ivpu_device *vdev)
44 {
45 	struct ivpu_fw_info *fw = vdev->fw;
46 	struct vpu_boot_params *bp = ivpu_bo_vaddr(fw->mem);
47 
48 	if (!bp->save_restore_ret_address) {
49 		ivpu_pm_prepare_cold_boot(vdev);
50 		return;
51 	}
52 
53 	ivpu_dbg(vdev, FW_BOOT, "Save/restore entry point %llx", bp->save_restore_ret_address);
54 	fw->entry_point = bp->save_restore_ret_address;
55 }
56 
57 static int ivpu_suspend(struct ivpu_device *vdev)
58 {
59 	int ret;
60 
61 	ret = ivpu_shutdown(vdev);
62 	if (ret) {
63 		ivpu_err(vdev, "Failed to shutdown VPU: %d\n", ret);
64 		return ret;
65 	}
66 
67 	return ret;
68 }
69 
70 static int ivpu_resume(struct ivpu_device *vdev)
71 {
72 	int ret;
73 
74 retry:
75 	ret = ivpu_hw_power_up(vdev);
76 	if (ret) {
77 		ivpu_err(vdev, "Failed to power up HW: %d\n", ret);
78 		goto err_power_down;
79 	}
80 
81 	ret = ivpu_mmu_enable(vdev);
82 	if (ret) {
83 		ivpu_err(vdev, "Failed to resume MMU: %d\n", ret);
84 		goto err_power_down;
85 	}
86 
87 	ret = ivpu_boot(vdev);
88 	if (ret)
89 		goto err_mmu_disable;
90 
91 	return 0;
92 
93 err_mmu_disable:
94 	ivpu_mmu_disable(vdev);
95 err_power_down:
96 	ivpu_hw_power_down(vdev);
97 
98 	if (!ivpu_fw_is_cold_boot(vdev)) {
99 		ivpu_pm_prepare_cold_boot(vdev);
100 		goto retry;
101 	} else {
102 		ivpu_err(vdev, "Failed to resume the FW: %d\n", ret);
103 	}
104 
105 	return ret;
106 }
107 
108 static void ivpu_pm_recovery_work(struct work_struct *work)
109 {
110 	struct ivpu_pm_info *pm = container_of(work, struct ivpu_pm_info, recovery_work);
111 	struct ivpu_device *vdev = pm->vdev;
112 	char *evt[2] = {"IVPU_PM_EVENT=IVPU_RECOVER", NULL};
113 	int ret;
114 
115 	ivpu_err(vdev, "Recovering the VPU (reset #%d)\n", atomic_read(&vdev->pm->reset_counter));
116 
117 	ret = pm_runtime_resume_and_get(vdev->drm.dev);
118 	if (ret)
119 		ivpu_err(vdev, "Failed to resume VPU: %d\n", ret);
120 
121 	ivpu_fw_log_dump(vdev);
122 
123 retry:
124 	ret = pci_try_reset_function(to_pci_dev(vdev->drm.dev));
125 	if (ret == -EAGAIN && !drm_dev_is_unplugged(&vdev->drm)) {
126 		cond_resched();
127 		goto retry;
128 	}
129 
130 	if (ret && ret != -EAGAIN)
131 		ivpu_err(vdev, "Failed to reset VPU: %d\n", ret);
132 
133 	kobject_uevent_env(&vdev->drm.dev->kobj, KOBJ_CHANGE, evt);
134 	pm_runtime_mark_last_busy(vdev->drm.dev);
135 	pm_runtime_put_autosuspend(vdev->drm.dev);
136 }
137 
138 void ivpu_pm_trigger_recovery(struct ivpu_device *vdev, const char *reason)
139 {
140 	ivpu_err(vdev, "Recovery triggered by %s\n", reason);
141 
142 	if (ivpu_disable_recovery) {
143 		ivpu_err(vdev, "Recovery not available when disable_recovery param is set\n");
144 		return;
145 	}
146 
147 	if (ivpu_is_fpga(vdev)) {
148 		ivpu_err(vdev, "Recovery not available on FPGA\n");
149 		return;
150 	}
151 
152 	/* Trigger recovery if it's not in progress */
153 	if (atomic_cmpxchg(&vdev->pm->reset_pending, 0, 1) == 0) {
154 		ivpu_hw_diagnose_failure(vdev);
155 		ivpu_hw_irq_disable(vdev); /* Disable IRQ early to protect from IRQ storm */
156 		queue_work(system_long_wq, &vdev->pm->recovery_work);
157 	}
158 }
159 
160 static void ivpu_job_timeout_work(struct work_struct *work)
161 {
162 	struct ivpu_pm_info *pm = container_of(work, struct ivpu_pm_info, job_timeout_work.work);
163 	struct ivpu_device *vdev = pm->vdev;
164 
165 	ivpu_pm_trigger_recovery(vdev, "TDR");
166 }
167 
168 void ivpu_start_job_timeout_detection(struct ivpu_device *vdev)
169 {
170 	unsigned long timeout_ms = ivpu_tdr_timeout_ms ? ivpu_tdr_timeout_ms : vdev->timeout.tdr;
171 
172 	/* No-op if already queued */
173 	queue_delayed_work(system_wq, &vdev->pm->job_timeout_work, msecs_to_jiffies(timeout_ms));
174 }
175 
176 void ivpu_stop_job_timeout_detection(struct ivpu_device *vdev)
177 {
178 	cancel_delayed_work_sync(&vdev->pm->job_timeout_work);
179 }
180 
181 int ivpu_pm_suspend_cb(struct device *dev)
182 {
183 	struct drm_device *drm = dev_get_drvdata(dev);
184 	struct ivpu_device *vdev = to_ivpu_device(drm);
185 	unsigned long timeout;
186 
187 	ivpu_dbg(vdev, PM, "Suspend..\n");
188 
189 	timeout = jiffies + msecs_to_jiffies(vdev->timeout.tdr);
190 	while (!ivpu_hw_is_idle(vdev)) {
191 		cond_resched();
192 		if (time_after_eq(jiffies, timeout)) {
193 			ivpu_err(vdev, "Failed to enter idle on system suspend\n");
194 			return -EBUSY;
195 		}
196 	}
197 
198 	ivpu_jsm_pwr_d0i3_enter(vdev);
199 
200 	ivpu_suspend(vdev);
201 	ivpu_pm_prepare_warm_boot(vdev);
202 
203 	pci_save_state(to_pci_dev(dev));
204 	pci_set_power_state(to_pci_dev(dev), PCI_D3hot);
205 
206 	ivpu_dbg(vdev, PM, "Suspend done.\n");
207 
208 	return 0;
209 }
210 
211 int ivpu_pm_resume_cb(struct device *dev)
212 {
213 	struct drm_device *drm = dev_get_drvdata(dev);
214 	struct ivpu_device *vdev = to_ivpu_device(drm);
215 	int ret;
216 
217 	ivpu_dbg(vdev, PM, "Resume..\n");
218 
219 	pci_set_power_state(to_pci_dev(dev), PCI_D0);
220 	pci_restore_state(to_pci_dev(dev));
221 
222 	ret = ivpu_resume(vdev);
223 	if (ret)
224 		ivpu_err(vdev, "Failed to resume: %d\n", ret);
225 
226 	ivpu_dbg(vdev, PM, "Resume done.\n");
227 
228 	return ret;
229 }
230 
231 int ivpu_pm_runtime_suspend_cb(struct device *dev)
232 {
233 	struct drm_device *drm = dev_get_drvdata(dev);
234 	struct ivpu_device *vdev = to_ivpu_device(drm);
235 	bool hw_is_idle = true;
236 	int ret;
237 
238 	drm_WARN_ON(&vdev->drm, !xa_empty(&vdev->submitted_jobs_xa));
239 	drm_WARN_ON(&vdev->drm, work_pending(&vdev->pm->recovery_work));
240 
241 	ivpu_dbg(vdev, PM, "Runtime suspend..\n");
242 
243 	if (!ivpu_hw_is_idle(vdev) && vdev->pm->suspend_reschedule_counter) {
244 		ivpu_dbg(vdev, PM, "Failed to enter idle, rescheduling suspend, retries left %d\n",
245 			 vdev->pm->suspend_reschedule_counter);
246 		pm_schedule_suspend(dev, vdev->timeout.reschedule_suspend);
247 		vdev->pm->suspend_reschedule_counter--;
248 		return -EAGAIN;
249 	}
250 
251 	if (!vdev->pm->suspend_reschedule_counter)
252 		hw_is_idle = false;
253 	else if (ivpu_jsm_pwr_d0i3_enter(vdev))
254 		hw_is_idle = false;
255 
256 	ret = ivpu_suspend(vdev);
257 	if (ret)
258 		ivpu_err(vdev, "Failed to set suspend VPU: %d\n", ret);
259 
260 	if (!hw_is_idle) {
261 		ivpu_err(vdev, "VPU failed to enter idle, force suspended.\n");
262 		ivpu_fw_log_dump(vdev);
263 		ivpu_pm_prepare_cold_boot(vdev);
264 	} else {
265 		ivpu_pm_prepare_warm_boot(vdev);
266 	}
267 
268 	vdev->pm->suspend_reschedule_counter = PM_RESCHEDULE_LIMIT;
269 
270 	ivpu_dbg(vdev, PM, "Runtime suspend done.\n");
271 
272 	return 0;
273 }
274 
275 int ivpu_pm_runtime_resume_cb(struct device *dev)
276 {
277 	struct drm_device *drm = dev_get_drvdata(dev);
278 	struct ivpu_device *vdev = to_ivpu_device(drm);
279 	int ret;
280 
281 	ivpu_dbg(vdev, PM, "Runtime resume..\n");
282 
283 	ret = ivpu_resume(vdev);
284 	if (ret)
285 		ivpu_err(vdev, "Failed to set RESUME state: %d\n", ret);
286 
287 	ivpu_dbg(vdev, PM, "Runtime resume done.\n");
288 
289 	return ret;
290 }
291 
292 int ivpu_rpm_get(struct ivpu_device *vdev)
293 {
294 	int ret;
295 
296 	ret = pm_runtime_resume_and_get(vdev->drm.dev);
297 	if (!drm_WARN_ON(&vdev->drm, ret < 0))
298 		vdev->pm->suspend_reschedule_counter = PM_RESCHEDULE_LIMIT;
299 
300 	return ret;
301 }
302 
303 int ivpu_rpm_get_if_active(struct ivpu_device *vdev)
304 {
305 	int ret;
306 
307 	ret = pm_runtime_get_if_active(vdev->drm.dev, false);
308 	drm_WARN_ON(&vdev->drm, ret < 0);
309 
310 	return ret;
311 }
312 
313 void ivpu_rpm_put(struct ivpu_device *vdev)
314 {
315 	pm_runtime_mark_last_busy(vdev->drm.dev);
316 	pm_runtime_put_autosuspend(vdev->drm.dev);
317 }
318 
319 void ivpu_pm_reset_prepare_cb(struct pci_dev *pdev)
320 {
321 	struct ivpu_device *vdev = pci_get_drvdata(pdev);
322 
323 	ivpu_dbg(vdev, PM, "Pre-reset..\n");
324 	atomic_inc(&vdev->pm->reset_counter);
325 	atomic_set(&vdev->pm->reset_pending, 1);
326 
327 	pm_runtime_get_sync(vdev->drm.dev);
328 	down_write(&vdev->pm->reset_lock);
329 	ivpu_prepare_for_reset(vdev);
330 	ivpu_hw_reset(vdev);
331 	ivpu_pm_prepare_cold_boot(vdev);
332 	ivpu_jobs_abort_all(vdev);
333 	ivpu_dbg(vdev, PM, "Pre-reset done.\n");
334 }
335 
336 void ivpu_pm_reset_done_cb(struct pci_dev *pdev)
337 {
338 	struct ivpu_device *vdev = pci_get_drvdata(pdev);
339 	int ret;
340 
341 	ivpu_dbg(vdev, PM, "Post-reset..\n");
342 	ret = ivpu_resume(vdev);
343 	if (ret)
344 		ivpu_err(vdev, "Failed to set RESUME state: %d\n", ret);
345 	up_write(&vdev->pm->reset_lock);
346 	atomic_set(&vdev->pm->reset_pending, 0);
347 	ivpu_dbg(vdev, PM, "Post-reset done.\n");
348 
349 	pm_runtime_mark_last_busy(vdev->drm.dev);
350 	pm_runtime_put_autosuspend(vdev->drm.dev);
351 }
352 
353 void ivpu_pm_init(struct ivpu_device *vdev)
354 {
355 	struct device *dev = vdev->drm.dev;
356 	struct ivpu_pm_info *pm = vdev->pm;
357 	int delay;
358 
359 	pm->vdev = vdev;
360 	pm->suspend_reschedule_counter = PM_RESCHEDULE_LIMIT;
361 
362 	init_rwsem(&pm->reset_lock);
363 	atomic_set(&pm->reset_pending, 0);
364 	atomic_set(&pm->reset_counter, 0);
365 
366 	INIT_WORK(&pm->recovery_work, ivpu_pm_recovery_work);
367 	INIT_DELAYED_WORK(&pm->job_timeout_work, ivpu_job_timeout_work);
368 
369 	if (ivpu_disable_recovery)
370 		delay = -1;
371 	else
372 		delay = vdev->timeout.autosuspend;
373 
374 	pm_runtime_use_autosuspend(dev);
375 	pm_runtime_set_autosuspend_delay(dev, delay);
376 
377 	ivpu_dbg(vdev, PM, "Autosuspend delay = %d\n", delay);
378 }
379 
380 void ivpu_pm_cancel_recovery(struct ivpu_device *vdev)
381 {
382 	drm_WARN_ON(&vdev->drm, delayed_work_pending(&vdev->pm->job_timeout_work));
383 	cancel_work_sync(&vdev->pm->recovery_work);
384 }
385 
386 void ivpu_pm_enable(struct ivpu_device *vdev)
387 {
388 	struct device *dev = vdev->drm.dev;
389 
390 	pm_runtime_set_active(dev);
391 	pm_runtime_allow(dev);
392 	pm_runtime_mark_last_busy(dev);
393 	pm_runtime_put_autosuspend(dev);
394 }
395 
396 void ivpu_pm_disable(struct ivpu_device *vdev)
397 {
398 	pm_runtime_get_noresume(vdev->drm.dev);
399 	pm_runtime_forbid(vdev->drm.dev);
400 }
401