1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2020-2024 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 "ivpu_coredump.h"
13 #include "ivpu_drv.h"
14 #include "ivpu_fw.h"
15 #include "ivpu_fw_log.h"
16 #include "ivpu_hw.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_ms.h"
22 #include "ivpu_pm.h"
23 #include "ivpu_trace.h"
24 #include "vpu_boot_api.h"
25 
26 static bool ivpu_disable_recovery;
27 #if IS_ENABLED(CONFIG_DRM_ACCEL_IVPU_DEBUG)
28 module_param_named_unsafe(disable_recovery, ivpu_disable_recovery, bool, 0644);
29 MODULE_PARM_DESC(disable_recovery, "Disables recovery when NPU hang is detected");
30 #endif
31 
32 static unsigned long ivpu_tdr_timeout_ms;
33 module_param_named(tdr_timeout_ms, ivpu_tdr_timeout_ms, ulong, 0644);
34 MODULE_PARM_DESC(tdr_timeout_ms, "Timeout for device hang detection, in milliseconds, 0 - default");
35 
36 #define PM_RESCHEDULE_LIMIT     5
37 
ivpu_pm_prepare_cold_boot(struct ivpu_device * vdev)38 static void ivpu_pm_prepare_cold_boot(struct ivpu_device *vdev)
39 {
40 	struct ivpu_fw_info *fw = vdev->fw;
41 
42 	ivpu_cmdq_reset_all_contexts(vdev);
43 	ivpu_ipc_reset(vdev);
44 	ivpu_fw_log_reset(vdev);
45 	ivpu_fw_load(vdev);
46 	fw->entry_point = fw->cold_boot_entry_point;
47 }
48 
ivpu_pm_prepare_warm_boot(struct ivpu_device * vdev)49 static void ivpu_pm_prepare_warm_boot(struct ivpu_device *vdev)
50 {
51 	struct ivpu_fw_info *fw = vdev->fw;
52 	struct vpu_boot_params *bp = ivpu_bo_vaddr(fw->mem);
53 
54 	if (!bp->save_restore_ret_address) {
55 		ivpu_pm_prepare_cold_boot(vdev);
56 		return;
57 	}
58 
59 	ivpu_dbg(vdev, FW_BOOT, "Save/restore entry point %llx", bp->save_restore_ret_address);
60 	fw->entry_point = bp->save_restore_ret_address;
61 }
62 
ivpu_suspend(struct ivpu_device * vdev)63 static int ivpu_suspend(struct ivpu_device *vdev)
64 {
65 	int ret;
66 
67 	ivpu_prepare_for_reset(vdev);
68 
69 	ret = ivpu_shutdown(vdev);
70 	if (ret)
71 		ivpu_err(vdev, "Failed to shutdown NPU: %d\n", ret);
72 
73 	return ret;
74 }
75 
ivpu_resume(struct ivpu_device * vdev)76 static int ivpu_resume(struct ivpu_device *vdev)
77 {
78 	int ret;
79 
80 retry:
81 	pci_set_power_state(to_pci_dev(vdev->drm.dev), PCI_D0);
82 	pci_restore_state(to_pci_dev(vdev->drm.dev));
83 
84 	ret = ivpu_hw_power_up(vdev);
85 	if (ret) {
86 		ivpu_err(vdev, "Failed to power up HW: %d\n", ret);
87 		goto err_power_down;
88 	}
89 
90 	ret = ivpu_mmu_enable(vdev);
91 	if (ret) {
92 		ivpu_err(vdev, "Failed to resume MMU: %d\n", ret);
93 		goto err_power_down;
94 	}
95 
96 	ret = ivpu_boot(vdev);
97 	if (ret)
98 		goto err_mmu_disable;
99 
100 	return 0;
101 
102 err_mmu_disable:
103 	ivpu_mmu_disable(vdev);
104 err_power_down:
105 	ivpu_hw_power_down(vdev);
106 	pci_set_power_state(to_pci_dev(vdev->drm.dev), PCI_D3hot);
107 
108 	if (!ivpu_fw_is_cold_boot(vdev)) {
109 		ivpu_pm_prepare_cold_boot(vdev);
110 		goto retry;
111 	} else {
112 		ivpu_err(vdev, "Failed to resume the FW: %d\n", ret);
113 	}
114 
115 	return ret;
116 }
117 
ivpu_pm_reset_begin(struct ivpu_device * vdev)118 static void ivpu_pm_reset_begin(struct ivpu_device *vdev)
119 {
120 	pm_runtime_disable(vdev->drm.dev);
121 
122 	atomic_inc(&vdev->pm->reset_counter);
123 	atomic_set(&vdev->pm->reset_pending, 1);
124 	down_write(&vdev->pm->reset_lock);
125 }
126 
ivpu_pm_reset_complete(struct ivpu_device * vdev)127 static void ivpu_pm_reset_complete(struct ivpu_device *vdev)
128 {
129 	int ret;
130 
131 	ivpu_pm_prepare_cold_boot(vdev);
132 	ivpu_jobs_abort_all(vdev);
133 	ivpu_ms_cleanup_all(vdev);
134 
135 	ret = ivpu_resume(vdev);
136 	if (ret) {
137 		ivpu_err(vdev, "Failed to resume NPU: %d\n", ret);
138 		pm_runtime_set_suspended(vdev->drm.dev);
139 	} else {
140 		pm_runtime_set_active(vdev->drm.dev);
141 	}
142 
143 	up_write(&vdev->pm->reset_lock);
144 	atomic_set(&vdev->pm->reset_pending, 0);
145 
146 	pm_runtime_mark_last_busy(vdev->drm.dev);
147 	pm_runtime_enable(vdev->drm.dev);
148 }
149 
ivpu_pm_recovery_work(struct work_struct * work)150 static void ivpu_pm_recovery_work(struct work_struct *work)
151 {
152 	struct ivpu_pm_info *pm = container_of(work, struct ivpu_pm_info, recovery_work);
153 	struct ivpu_device *vdev = pm->vdev;
154 	char *evt[2] = {"IVPU_PM_EVENT=IVPU_RECOVER", NULL};
155 
156 	ivpu_err(vdev, "Recovering the NPU (reset #%d)\n", atomic_read(&vdev->pm->reset_counter));
157 
158 	ivpu_pm_reset_begin(vdev);
159 
160 	if (!pm_runtime_status_suspended(vdev->drm.dev)) {
161 		ivpu_jsm_state_dump(vdev);
162 		ivpu_dev_coredump(vdev);
163 		ivpu_suspend(vdev);
164 	}
165 
166 	ivpu_pm_reset_complete(vdev);
167 
168 	kobject_uevent_env(&vdev->drm.dev->kobj, KOBJ_CHANGE, evt);
169 }
170 
ivpu_pm_trigger_recovery(struct ivpu_device * vdev,const char * reason)171 void ivpu_pm_trigger_recovery(struct ivpu_device *vdev, const char *reason)
172 {
173 	ivpu_err(vdev, "Recovery triggered by %s\n", reason);
174 
175 	if (ivpu_disable_recovery) {
176 		ivpu_err(vdev, "Recovery not available when disable_recovery param is set\n");
177 		return;
178 	}
179 
180 	/* Trigger recovery if it's not in progress */
181 	if (atomic_cmpxchg(&vdev->pm->reset_pending, 0, 1) == 0) {
182 		ivpu_hw_diagnose_failure(vdev);
183 		ivpu_hw_irq_disable(vdev); /* Disable IRQ early to protect from IRQ storm */
184 		queue_work(system_unbound_wq, &vdev->pm->recovery_work);
185 	}
186 }
187 
ivpu_job_timeout_work(struct work_struct * work)188 static void ivpu_job_timeout_work(struct work_struct *work)
189 {
190 	struct ivpu_pm_info *pm = container_of(work, struct ivpu_pm_info, job_timeout_work.work);
191 	struct ivpu_device *vdev = pm->vdev;
192 
193 	ivpu_pm_trigger_recovery(vdev, "TDR");
194 }
195 
ivpu_start_job_timeout_detection(struct ivpu_device * vdev)196 void ivpu_start_job_timeout_detection(struct ivpu_device *vdev)
197 {
198 	unsigned long timeout_ms = ivpu_tdr_timeout_ms ? ivpu_tdr_timeout_ms : vdev->timeout.tdr;
199 
200 	/* No-op if already queued */
201 	queue_delayed_work(system_wq, &vdev->pm->job_timeout_work, msecs_to_jiffies(timeout_ms));
202 }
203 
ivpu_stop_job_timeout_detection(struct ivpu_device * vdev)204 void ivpu_stop_job_timeout_detection(struct ivpu_device *vdev)
205 {
206 	cancel_delayed_work_sync(&vdev->pm->job_timeout_work);
207 }
208 
ivpu_pm_suspend_cb(struct device * dev)209 int ivpu_pm_suspend_cb(struct device *dev)
210 {
211 	struct drm_device *drm = dev_get_drvdata(dev);
212 	struct ivpu_device *vdev = to_ivpu_device(drm);
213 	unsigned long timeout;
214 
215 	trace_pm("suspend");
216 	ivpu_dbg(vdev, PM, "Suspend..\n");
217 
218 	timeout = jiffies + msecs_to_jiffies(vdev->timeout.tdr);
219 	while (!ivpu_hw_is_idle(vdev)) {
220 		cond_resched();
221 		if (time_after_eq(jiffies, timeout)) {
222 			ivpu_err(vdev, "Failed to enter idle on system suspend\n");
223 			return -EBUSY;
224 		}
225 	}
226 
227 	ivpu_jsm_pwr_d0i3_enter(vdev);
228 
229 	ivpu_suspend(vdev);
230 	ivpu_pm_prepare_warm_boot(vdev);
231 
232 	ivpu_dbg(vdev, PM, "Suspend done.\n");
233 	trace_pm("suspend done");
234 
235 	return 0;
236 }
237 
ivpu_pm_resume_cb(struct device * dev)238 int ivpu_pm_resume_cb(struct device *dev)
239 {
240 	struct drm_device *drm = dev_get_drvdata(dev);
241 	struct ivpu_device *vdev = to_ivpu_device(drm);
242 	int ret;
243 
244 	trace_pm("resume");
245 	ivpu_dbg(vdev, PM, "Resume..\n");
246 
247 	ret = ivpu_resume(vdev);
248 	if (ret)
249 		ivpu_err(vdev, "Failed to resume: %d\n", ret);
250 
251 	ivpu_dbg(vdev, PM, "Resume done.\n");
252 	trace_pm("resume done");
253 
254 	return ret;
255 }
256 
ivpu_pm_runtime_suspend_cb(struct device * dev)257 int ivpu_pm_runtime_suspend_cb(struct device *dev)
258 {
259 	struct drm_device *drm = dev_get_drvdata(dev);
260 	struct ivpu_device *vdev = to_ivpu_device(drm);
261 	int ret, ret_d0i3;
262 	bool is_idle;
263 
264 	drm_WARN_ON(&vdev->drm, !xa_empty(&vdev->submitted_jobs_xa));
265 	drm_WARN_ON(&vdev->drm, work_pending(&vdev->pm->recovery_work));
266 
267 	trace_pm("runtime suspend");
268 	ivpu_dbg(vdev, PM, "Runtime suspend..\n");
269 
270 	ivpu_mmu_disable(vdev);
271 
272 	is_idle = ivpu_hw_is_idle(vdev) || vdev->pm->dct_active_percent;
273 	if (!is_idle)
274 		ivpu_err(vdev, "NPU is not idle before autosuspend\n");
275 
276 	ret_d0i3 = ivpu_jsm_pwr_d0i3_enter(vdev);
277 	if (ret_d0i3)
278 		ivpu_err(vdev, "Failed to prepare for d0i3: %d\n", ret_d0i3);
279 
280 	ret = ivpu_suspend(vdev);
281 	if (ret)
282 		ivpu_err(vdev, "Failed to suspend NPU: %d\n", ret);
283 
284 	if (!is_idle || ret_d0i3) {
285 		ivpu_err(vdev, "Forcing cold boot due to previous errors\n");
286 		atomic_inc(&vdev->pm->reset_counter);
287 		ivpu_dev_coredump(vdev);
288 		ivpu_pm_prepare_cold_boot(vdev);
289 	} else {
290 		ivpu_pm_prepare_warm_boot(vdev);
291 	}
292 
293 	ivpu_dbg(vdev, PM, "Runtime suspend done.\n");
294 	trace_pm("runtime suspend done");
295 
296 	return 0;
297 }
298 
ivpu_pm_runtime_resume_cb(struct device * dev)299 int ivpu_pm_runtime_resume_cb(struct device *dev)
300 {
301 	struct drm_device *drm = dev_get_drvdata(dev);
302 	struct ivpu_device *vdev = to_ivpu_device(drm);
303 	int ret;
304 
305 	trace_pm("runtime resume");
306 	ivpu_dbg(vdev, PM, "Runtime resume..\n");
307 
308 	ret = ivpu_resume(vdev);
309 	if (ret)
310 		ivpu_err(vdev, "Failed to set RESUME state: %d\n", ret);
311 
312 	ivpu_dbg(vdev, PM, "Runtime resume done.\n");
313 	trace_pm("runtime resume done");
314 
315 	return ret;
316 }
317 
ivpu_rpm_get(struct ivpu_device * vdev)318 int ivpu_rpm_get(struct ivpu_device *vdev)
319 {
320 	int ret;
321 
322 	ret = pm_runtime_resume_and_get(vdev->drm.dev);
323 	if (ret < 0) {
324 		ivpu_err(vdev, "Failed to resume NPU: %d\n", ret);
325 		pm_runtime_set_suspended(vdev->drm.dev);
326 	}
327 
328 	return ret;
329 }
330 
ivpu_rpm_put(struct ivpu_device * vdev)331 void ivpu_rpm_put(struct ivpu_device *vdev)
332 {
333 	pm_runtime_mark_last_busy(vdev->drm.dev);
334 	pm_runtime_put_autosuspend(vdev->drm.dev);
335 }
336 
ivpu_pm_reset_prepare_cb(struct pci_dev * pdev)337 void ivpu_pm_reset_prepare_cb(struct pci_dev *pdev)
338 {
339 	struct ivpu_device *vdev = pci_get_drvdata(pdev);
340 
341 	ivpu_dbg(vdev, PM, "Pre-reset..\n");
342 
343 	ivpu_pm_reset_begin(vdev);
344 
345 	if (!pm_runtime_status_suspended(vdev->drm.dev)) {
346 		ivpu_prepare_for_reset(vdev);
347 		ivpu_hw_reset(vdev);
348 	}
349 
350 	ivpu_dbg(vdev, PM, "Pre-reset done.\n");
351 }
352 
ivpu_pm_reset_done_cb(struct pci_dev * pdev)353 void ivpu_pm_reset_done_cb(struct pci_dev *pdev)
354 {
355 	struct ivpu_device *vdev = pci_get_drvdata(pdev);
356 
357 	ivpu_dbg(vdev, PM, "Post-reset..\n");
358 
359 	ivpu_pm_reset_complete(vdev);
360 
361 	ivpu_dbg(vdev, PM, "Post-reset done.\n");
362 }
363 
ivpu_pm_init(struct ivpu_device * vdev)364 void ivpu_pm_init(struct ivpu_device *vdev)
365 {
366 	struct device *dev = vdev->drm.dev;
367 	struct ivpu_pm_info *pm = vdev->pm;
368 	int delay;
369 
370 	pm->vdev = vdev;
371 
372 	init_rwsem(&pm->reset_lock);
373 	atomic_set(&pm->reset_pending, 0);
374 	atomic_set(&pm->reset_counter, 0);
375 
376 	INIT_WORK(&pm->recovery_work, ivpu_pm_recovery_work);
377 	INIT_DELAYED_WORK(&pm->job_timeout_work, ivpu_job_timeout_work);
378 
379 	if (ivpu_disable_recovery)
380 		delay = -1;
381 	else
382 		delay = vdev->timeout.autosuspend;
383 
384 	pm_runtime_use_autosuspend(dev);
385 	pm_runtime_set_autosuspend_delay(dev, delay);
386 	pm_runtime_set_active(dev);
387 
388 	ivpu_dbg(vdev, PM, "Autosuspend delay = %d\n", delay);
389 }
390 
ivpu_pm_cancel_recovery(struct ivpu_device * vdev)391 void ivpu_pm_cancel_recovery(struct ivpu_device *vdev)
392 {
393 	drm_WARN_ON(&vdev->drm, delayed_work_pending(&vdev->pm->job_timeout_work));
394 	cancel_work_sync(&vdev->pm->recovery_work);
395 }
396 
ivpu_pm_enable(struct ivpu_device * vdev)397 void ivpu_pm_enable(struct ivpu_device *vdev)
398 {
399 	struct device *dev = vdev->drm.dev;
400 
401 	pm_runtime_allow(dev);
402 	pm_runtime_mark_last_busy(dev);
403 	pm_runtime_put_autosuspend(dev);
404 }
405 
ivpu_pm_disable(struct ivpu_device * vdev)406 void ivpu_pm_disable(struct ivpu_device *vdev)
407 {
408 	pm_runtime_get_noresume(vdev->drm.dev);
409 	pm_runtime_forbid(vdev->drm.dev);
410 }
411 
ivpu_pm_dct_init(struct ivpu_device * vdev)412 int ivpu_pm_dct_init(struct ivpu_device *vdev)
413 {
414 	if (vdev->pm->dct_active_percent)
415 		return ivpu_pm_dct_enable(vdev, vdev->pm->dct_active_percent);
416 
417 	return 0;
418 }
419 
ivpu_pm_dct_enable(struct ivpu_device * vdev,u8 active_percent)420 int ivpu_pm_dct_enable(struct ivpu_device *vdev, u8 active_percent)
421 {
422 	u32 active_us, inactive_us;
423 	int ret;
424 
425 	if (active_percent == 0 || active_percent > 100)
426 		return -EINVAL;
427 
428 	active_us = (DCT_PERIOD_US * active_percent) / 100;
429 	inactive_us = DCT_PERIOD_US - active_us;
430 
431 	vdev->pm->dct_active_percent = active_percent;
432 
433 	ivpu_dbg(vdev, PM, "DCT requested %u%% (D0: %uus, D0i2: %uus)\n",
434 		 active_percent, active_us, inactive_us);
435 
436 	ret = ivpu_jsm_dct_enable(vdev, active_us, inactive_us);
437 	if (ret) {
438 		ivpu_err_ratelimited(vdev, "Failed to enable DCT: %d\n", ret);
439 		return ret;
440 	}
441 
442 	return 0;
443 }
444 
ivpu_pm_dct_disable(struct ivpu_device * vdev)445 int ivpu_pm_dct_disable(struct ivpu_device *vdev)
446 {
447 	int ret;
448 
449 	vdev->pm->dct_active_percent = 0;
450 
451 	ivpu_dbg(vdev, PM, "DCT requested to be disabled\n");
452 
453 	ret = ivpu_jsm_dct_disable(vdev);
454 	if (ret) {
455 		ivpu_err_ratelimited(vdev, "Failed to disable DCT: %d\n", ret);
456 		return ret;
457 	}
458 
459 	return 0;
460 }
461 
ivpu_pm_irq_dct_work_fn(struct work_struct * work)462 void ivpu_pm_irq_dct_work_fn(struct work_struct *work)
463 {
464 	struct ivpu_device *vdev = container_of(work, struct ivpu_device, irq_dct_work);
465 	bool enable;
466 	int ret;
467 
468 	if (ivpu_hw_btrs_dct_get_request(vdev, &enable))
469 		return;
470 
471 	if (enable)
472 		ret = ivpu_pm_dct_enable(vdev, DCT_DEFAULT_ACTIVE_PERCENT);
473 	else
474 		ret = ivpu_pm_dct_disable(vdev);
475 
476 	if (!ret)
477 		ivpu_hw_btrs_dct_set_status(vdev, enable, vdev->pm->dct_active_percent);
478 }
479