GNU Linux-libre 6.8.9-gnu
[releases.git] / drivers / accel / ivpu / ivpu_pm.c
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         /* Save PCI state before powering down as it sometimes gets corrupted if NPU hangs */
62         pci_save_state(to_pci_dev(vdev->drm.dev));
63
64         ret = ivpu_shutdown(vdev);
65         if (ret)
66                 ivpu_err(vdev, "Failed to shutdown VPU: %d\n", ret);
67
68         pci_set_power_state(to_pci_dev(vdev->drm.dev), PCI_D3hot);
69
70         return ret;
71 }
72
73 static int ivpu_resume(struct ivpu_device *vdev)
74 {
75         int ret;
76
77 retry:
78         pci_restore_state(to_pci_dev(vdev->drm.dev));
79         pci_set_power_state(to_pci_dev(vdev->drm.dev), PCI_D0);
80
81         ret = ivpu_hw_power_up(vdev);
82         if (ret) {
83                 ivpu_err(vdev, "Failed to power up HW: %d\n", ret);
84                 goto err_power_down;
85         }
86
87         ret = ivpu_mmu_enable(vdev);
88         if (ret) {
89                 ivpu_err(vdev, "Failed to resume MMU: %d\n", ret);
90                 goto err_power_down;
91         }
92
93         ret = ivpu_boot(vdev);
94         if (ret)
95                 goto err_mmu_disable;
96
97         return 0;
98
99 err_mmu_disable:
100         ivpu_mmu_disable(vdev);
101 err_power_down:
102         ivpu_hw_power_down(vdev);
103         pci_set_power_state(to_pci_dev(vdev->drm.dev), PCI_D3hot);
104
105         if (!ivpu_fw_is_cold_boot(vdev)) {
106                 ivpu_pm_prepare_cold_boot(vdev);
107                 goto retry;
108         } else {
109                 ivpu_err(vdev, "Failed to resume the FW: %d\n", ret);
110         }
111
112         return ret;
113 }
114
115 static void ivpu_pm_recovery_work(struct work_struct *work)
116 {
117         struct ivpu_pm_info *pm = container_of(work, struct ivpu_pm_info, recovery_work);
118         struct ivpu_device *vdev = pm->vdev;
119         char *evt[2] = {"IVPU_PM_EVENT=IVPU_RECOVER", NULL};
120         int ret;
121
122         ivpu_err(vdev, "Recovering the VPU (reset #%d)\n", atomic_read(&vdev->pm->reset_counter));
123
124         ret = pm_runtime_resume_and_get(vdev->drm.dev);
125         if (ret)
126                 ivpu_err(vdev, "Failed to resume VPU: %d\n", ret);
127
128         ivpu_fw_log_dump(vdev);
129
130         atomic_inc(&vdev->pm->reset_counter);
131         atomic_set(&vdev->pm->reset_pending, 1);
132         down_write(&vdev->pm->reset_lock);
133
134         ivpu_suspend(vdev);
135         ivpu_pm_prepare_cold_boot(vdev);
136         ivpu_jobs_abort_all(vdev);
137
138         ret = ivpu_resume(vdev);
139         if (ret)
140                 ivpu_err(vdev, "Failed to resume NPU: %d\n", ret);
141
142         up_write(&vdev->pm->reset_lock);
143         atomic_set(&vdev->pm->reset_pending, 0);
144
145         kobject_uevent_env(&vdev->drm.dev->kobj, KOBJ_CHANGE, evt);
146         pm_runtime_mark_last_busy(vdev->drm.dev);
147         pm_runtime_put_autosuspend(vdev->drm.dev);
148 }
149
150 void ivpu_pm_trigger_recovery(struct ivpu_device *vdev, const char *reason)
151 {
152         ivpu_err(vdev, "Recovery triggered by %s\n", reason);
153
154         if (ivpu_disable_recovery) {
155                 ivpu_err(vdev, "Recovery not available when disable_recovery param is set\n");
156                 return;
157         }
158
159         if (ivpu_is_fpga(vdev)) {
160                 ivpu_err(vdev, "Recovery not available on FPGA\n");
161                 return;
162         }
163
164         /* Trigger recovery if it's not in progress */
165         if (atomic_cmpxchg(&vdev->pm->reset_pending, 0, 1) == 0) {
166                 ivpu_hw_diagnose_failure(vdev);
167                 ivpu_hw_irq_disable(vdev); /* Disable IRQ early to protect from IRQ storm */
168                 queue_work(system_long_wq, &vdev->pm->recovery_work);
169         }
170 }
171
172 static void ivpu_job_timeout_work(struct work_struct *work)
173 {
174         struct ivpu_pm_info *pm = container_of(work, struct ivpu_pm_info, job_timeout_work.work);
175         struct ivpu_device *vdev = pm->vdev;
176
177         ivpu_pm_trigger_recovery(vdev, "TDR");
178 }
179
180 void ivpu_start_job_timeout_detection(struct ivpu_device *vdev)
181 {
182         unsigned long timeout_ms = ivpu_tdr_timeout_ms ? ivpu_tdr_timeout_ms : vdev->timeout.tdr;
183
184         /* No-op if already queued */
185         queue_delayed_work(system_wq, &vdev->pm->job_timeout_work, msecs_to_jiffies(timeout_ms));
186 }
187
188 void ivpu_stop_job_timeout_detection(struct ivpu_device *vdev)
189 {
190         cancel_delayed_work_sync(&vdev->pm->job_timeout_work);
191 }
192
193 int ivpu_pm_suspend_cb(struct device *dev)
194 {
195         struct drm_device *drm = dev_get_drvdata(dev);
196         struct ivpu_device *vdev = to_ivpu_device(drm);
197         unsigned long timeout;
198
199         ivpu_dbg(vdev, PM, "Suspend..\n");
200
201         timeout = jiffies + msecs_to_jiffies(vdev->timeout.tdr);
202         while (!ivpu_hw_is_idle(vdev)) {
203                 cond_resched();
204                 if (time_after_eq(jiffies, timeout)) {
205                         ivpu_err(vdev, "Failed to enter idle on system suspend\n");
206                         return -EBUSY;
207                 }
208         }
209
210         ivpu_jsm_pwr_d0i3_enter(vdev);
211
212         ivpu_suspend(vdev);
213         ivpu_pm_prepare_warm_boot(vdev);
214
215         ivpu_dbg(vdev, PM, "Suspend done.\n");
216
217         return 0;
218 }
219
220 int ivpu_pm_resume_cb(struct device *dev)
221 {
222         struct drm_device *drm = dev_get_drvdata(dev);
223         struct ivpu_device *vdev = to_ivpu_device(drm);
224         int ret;
225
226         ivpu_dbg(vdev, PM, "Resume..\n");
227
228         ret = ivpu_resume(vdev);
229         if (ret)
230                 ivpu_err(vdev, "Failed to resume: %d\n", ret);
231
232         ivpu_dbg(vdev, PM, "Resume done.\n");
233
234         return ret;
235 }
236
237 int ivpu_pm_runtime_suspend_cb(struct device *dev)
238 {
239         struct drm_device *drm = dev_get_drvdata(dev);
240         struct ivpu_device *vdev = to_ivpu_device(drm);
241         bool hw_is_idle = true;
242         int ret;
243
244         drm_WARN_ON(&vdev->drm, !xa_empty(&vdev->submitted_jobs_xa));
245         drm_WARN_ON(&vdev->drm, work_pending(&vdev->pm->recovery_work));
246
247         ivpu_dbg(vdev, PM, "Runtime suspend..\n");
248
249         if (!ivpu_hw_is_idle(vdev) && vdev->pm->suspend_reschedule_counter) {
250                 ivpu_dbg(vdev, PM, "Failed to enter idle, rescheduling suspend, retries left %d\n",
251                          vdev->pm->suspend_reschedule_counter);
252                 pm_schedule_suspend(dev, vdev->timeout.reschedule_suspend);
253                 vdev->pm->suspend_reschedule_counter--;
254                 return -EAGAIN;
255         }
256
257         if (!vdev->pm->suspend_reschedule_counter)
258                 hw_is_idle = false;
259         else if (ivpu_jsm_pwr_d0i3_enter(vdev))
260                 hw_is_idle = false;
261
262         ret = ivpu_suspend(vdev);
263         if (ret)
264                 ivpu_err(vdev, "Failed to set suspend VPU: %d\n", ret);
265
266         if (!hw_is_idle) {
267                 ivpu_err(vdev, "VPU failed to enter idle, force suspended.\n");
268                 ivpu_fw_log_dump(vdev);
269                 ivpu_pm_prepare_cold_boot(vdev);
270         } else {
271                 ivpu_pm_prepare_warm_boot(vdev);
272         }
273
274         vdev->pm->suspend_reschedule_counter = PM_RESCHEDULE_LIMIT;
275
276         ivpu_dbg(vdev, PM, "Runtime suspend done.\n");
277
278         return 0;
279 }
280
281 int ivpu_pm_runtime_resume_cb(struct device *dev)
282 {
283         struct drm_device *drm = dev_get_drvdata(dev);
284         struct ivpu_device *vdev = to_ivpu_device(drm);
285         int ret;
286
287         ivpu_dbg(vdev, PM, "Runtime resume..\n");
288
289         ret = ivpu_resume(vdev);
290         if (ret)
291                 ivpu_err(vdev, "Failed to set RESUME state: %d\n", ret);
292
293         ivpu_dbg(vdev, PM, "Runtime resume done.\n");
294
295         return ret;
296 }
297
298 int ivpu_rpm_get(struct ivpu_device *vdev)
299 {
300         int ret;
301
302         ret = pm_runtime_resume_and_get(vdev->drm.dev);
303         if (!drm_WARN_ON(&vdev->drm, ret < 0))
304                 vdev->pm->suspend_reschedule_counter = PM_RESCHEDULE_LIMIT;
305
306         return ret;
307 }
308
309 int ivpu_rpm_get_if_active(struct ivpu_device *vdev)
310 {
311         int ret;
312
313         ret = pm_runtime_get_if_active(vdev->drm.dev, false);
314         drm_WARN_ON(&vdev->drm, ret < 0);
315
316         return ret;
317 }
318
319 void ivpu_rpm_put(struct ivpu_device *vdev)
320 {
321         pm_runtime_mark_last_busy(vdev->drm.dev);
322         pm_runtime_put_autosuspend(vdev->drm.dev);
323 }
324
325 void ivpu_pm_reset_prepare_cb(struct pci_dev *pdev)
326 {
327         struct ivpu_device *vdev = pci_get_drvdata(pdev);
328
329         ivpu_dbg(vdev, PM, "Pre-reset..\n");
330         atomic_inc(&vdev->pm->reset_counter);
331         atomic_set(&vdev->pm->reset_pending, 1);
332
333         pm_runtime_get_sync(vdev->drm.dev);
334         down_write(&vdev->pm->reset_lock);
335         ivpu_prepare_for_reset(vdev);
336         ivpu_hw_reset(vdev);
337         ivpu_pm_prepare_cold_boot(vdev);
338         ivpu_jobs_abort_all(vdev);
339         ivpu_dbg(vdev, PM, "Pre-reset done.\n");
340 }
341
342 void ivpu_pm_reset_done_cb(struct pci_dev *pdev)
343 {
344         struct ivpu_device *vdev = pci_get_drvdata(pdev);
345         int ret;
346
347         ivpu_dbg(vdev, PM, "Post-reset..\n");
348         ret = ivpu_resume(vdev);
349         if (ret)
350                 ivpu_err(vdev, "Failed to set RESUME state: %d\n", ret);
351         up_write(&vdev->pm->reset_lock);
352         atomic_set(&vdev->pm->reset_pending, 0);
353         ivpu_dbg(vdev, PM, "Post-reset done.\n");
354
355         pm_runtime_mark_last_busy(vdev->drm.dev);
356         pm_runtime_put_autosuspend(vdev->drm.dev);
357 }
358
359 void ivpu_pm_init(struct ivpu_device *vdev)
360 {
361         struct device *dev = vdev->drm.dev;
362         struct ivpu_pm_info *pm = vdev->pm;
363         int delay;
364
365         pm->vdev = vdev;
366         pm->suspend_reschedule_counter = PM_RESCHEDULE_LIMIT;
367
368         init_rwsem(&pm->reset_lock);
369         atomic_set(&pm->reset_pending, 0);
370         atomic_set(&pm->reset_counter, 0);
371
372         INIT_WORK(&pm->recovery_work, ivpu_pm_recovery_work);
373         INIT_DELAYED_WORK(&pm->job_timeout_work, ivpu_job_timeout_work);
374
375         if (ivpu_disable_recovery)
376                 delay = -1;
377         else
378                 delay = vdev->timeout.autosuspend;
379
380         pm_runtime_use_autosuspend(dev);
381         pm_runtime_set_autosuspend_delay(dev, delay);
382
383         ivpu_dbg(vdev, PM, "Autosuspend delay = %d\n", delay);
384 }
385
386 void ivpu_pm_cancel_recovery(struct ivpu_device *vdev)
387 {
388         drm_WARN_ON(&vdev->drm, delayed_work_pending(&vdev->pm->job_timeout_work));
389         cancel_work_sync(&vdev->pm->recovery_work);
390 }
391
392 void ivpu_pm_enable(struct ivpu_device *vdev)
393 {
394         struct device *dev = vdev->drm.dev;
395
396         pm_runtime_set_active(dev);
397         pm_runtime_allow(dev);
398         pm_runtime_mark_last_busy(dev);
399         pm_runtime_put_autosuspend(dev);
400 }
401
402 void ivpu_pm_disable(struct ivpu_device *vdev)
403 {
404         pm_runtime_get_noresume(vdev->drm.dev);
405         pm_runtime_forbid(vdev->drm.dev);
406 }