xref: /linux/drivers/gpu/drm/panthor/panthor_device.c (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
1 // SPDX-License-Identifier: GPL-2.0 or MIT
2 /* Copyright 2018 Marty E. Plummer <hanetzer@startmail.com> */
3 /* Copyright 2019 Linaro, Ltd, Rob Herring <robh@kernel.org> */
4 /* Copyright 2023 Collabora ltd. */
5 /* Copyright 2025 ARM Limited. All rights reserved. */
6 
7 #include <linux/clk.h>
8 #include <linux/mm.h>
9 #include <linux/platform_device.h>
10 #include <linux/pm_domain.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/regulator/consumer.h>
13 #include <linux/reset.h>
14 
15 #include <drm/drm_drv.h>
16 #include <drm/drm_managed.h>
17 #include <drm/drm_print.h>
18 
19 #include "panthor_devfreq.h"
20 #include "panthor_device.h"
21 #include "panthor_fw.h"
22 #include "panthor_fw_regs.h"
23 #include "panthor_gem.h"
24 #include "panthor_gpu.h"
25 #include "panthor_hw.h"
26 #include "panthor_mmu.h"
27 #include "panthor_pwr.h"
28 #include "panthor_sched.h"
29 
30 static int panthor_clk_init(struct panthor_device *ptdev)
31 {
32 	ptdev->clks.core = devm_clk_get(ptdev->base.dev, NULL);
33 	if (IS_ERR(ptdev->clks.core))
34 		return dev_err_probe(ptdev->base.dev,
35 				     PTR_ERR(ptdev->clks.core),
36 				     "get 'core' clock failed");
37 
38 	ptdev->clks.stacks = devm_clk_get_optional(ptdev->base.dev, "stacks");
39 	if (IS_ERR(ptdev->clks.stacks))
40 		return dev_err_probe(ptdev->base.dev,
41 				     PTR_ERR(ptdev->clks.stacks),
42 				     "get 'stacks' clock failed");
43 
44 	ptdev->clks.coregroup = devm_clk_get_optional(ptdev->base.dev, "coregroup");
45 	if (IS_ERR(ptdev->clks.coregroup))
46 		return dev_err_probe(ptdev->base.dev,
47 				     PTR_ERR(ptdev->clks.coregroup),
48 				     "get 'coregroup' clock failed");
49 
50 	drm_info(&ptdev->base, "clock rate = %lu\n", clk_get_rate(ptdev->clks.core));
51 	return 0;
52 }
53 
54 static int panthor_init_power(struct device *dev)
55 {
56 	struct dev_pm_domain_list  *pd_list = NULL;
57 
58 	if (dev->pm_domain)
59 		return 0;
60 
61 	return devm_pm_domain_attach_list(dev, NULL, &pd_list);
62 }
63 
64 void panthor_device_unplug(struct panthor_device *ptdev)
65 {
66 	/* This function can be called from two different path: the reset work
67 	 * and the platform device remove callback. drm_dev_unplug() doesn't
68 	 * deal with concurrent callers, so we have to protect drm_dev_unplug()
69 	 * calls with our own lock, and bail out if the device is already
70 	 * unplugged.
71 	 */
72 	mutex_lock(&ptdev->unplug.lock);
73 	if (drm_dev_is_unplugged(&ptdev->base)) {
74 		/* Someone beat us, release the lock and wait for the unplug
75 		 * operation to be reported as done.
76 		 **/
77 		mutex_unlock(&ptdev->unplug.lock);
78 		wait_for_completion(&ptdev->unplug.done);
79 		return;
80 	}
81 
82 	drm_WARN_ON(&ptdev->base, pm_runtime_get_sync(ptdev->base.dev) < 0);
83 
84 	/* Call drm_dev_unplug() so any access to HW blocks happening after
85 	 * that point get rejected.
86 	 */
87 	drm_dev_unplug(&ptdev->base);
88 
89 	/* We do the rest of the unplug with the unplug lock released,
90 	 * future callers will wait on ptdev->unplug.done anyway.
91 	 */
92 	mutex_unlock(&ptdev->unplug.lock);
93 
94 	/* Now, try to cleanly shutdown the GPU before the device resources
95 	 * get reclaimed.
96 	 */
97 	panthor_sched_unplug(ptdev);
98 	panthor_fw_unplug(ptdev);
99 	panthor_mmu_unplug(ptdev);
100 	panthor_gem_shrinker_unplug(ptdev);
101 	panthor_gpu_unplug(ptdev);
102 	panthor_pwr_unplug(ptdev);
103 
104 	pm_runtime_dont_use_autosuspend(ptdev->base.dev);
105 	pm_runtime_put_sync_suspend(ptdev->base.dev);
106 
107 	/* If PM is disabled, we need to call the suspend handler manually. */
108 	if (!IS_ENABLED(CONFIG_PM))
109 		panthor_device_suspend(ptdev->base.dev);
110 
111 	/* Report the unplug operation as done to unblock concurrent
112 	 * panthor_device_unplug() callers.
113 	 */
114 	complete_all(&ptdev->unplug.done);
115 }
116 
117 static void panthor_device_reset_cleanup(struct drm_device *ddev, void *data)
118 {
119 	struct panthor_device *ptdev = container_of(ddev, struct panthor_device, base);
120 
121 	disable_work_sync(&ptdev->reset.work);
122 	destroy_workqueue(ptdev->reset.wq);
123 }
124 
125 static void panthor_device_reset_work(struct work_struct *work)
126 {
127 	struct panthor_device *ptdev = container_of(work, struct panthor_device, reset.work);
128 	int ret = 0, cookie;
129 
130 	/* If the device is entering suspend, we don't reset. A slow reset will
131 	 * be forced at resume time instead.
132 	 */
133 	if (atomic_read(&ptdev->pm.state) != PANTHOR_DEVICE_PM_STATE_ACTIVE)
134 		return;
135 
136 	if (!drm_dev_enter(&ptdev->base, &cookie))
137 		return;
138 
139 	panthor_sched_pre_reset(ptdev);
140 	panthor_fw_pre_reset(ptdev, true);
141 	panthor_mmu_pre_reset(ptdev);
142 	panthor_hw_soft_reset(ptdev);
143 	panthor_hw_l2_power_on(ptdev);
144 	panthor_mmu_post_reset(ptdev);
145 	ret = panthor_fw_post_reset(ptdev);
146 	atomic_set(&ptdev->reset.pending, 0);
147 	panthor_sched_post_reset(ptdev, ret != 0);
148 	drm_dev_exit(cookie);
149 
150 	if (ret) {
151 		panthor_device_unplug(ptdev);
152 		drm_err(&ptdev->base, "Failed to boot MCU after reset, making device unusable.");
153 	}
154 }
155 
156 static bool panthor_device_is_initialized(struct panthor_device *ptdev)
157 {
158 	return !!ptdev->scheduler;
159 }
160 
161 static void panthor_device_free_page(struct drm_device *ddev, void *data)
162 {
163 	__free_page(data);
164 }
165 
166 int panthor_device_init(struct panthor_device *ptdev)
167 {
168 	u32 *dummy_page_virt;
169 	struct resource *res;
170 	struct page *p;
171 	int ret;
172 
173 	ptdev->soc_data = of_device_get_match_data(ptdev->base.dev);
174 
175 	init_completion(&ptdev->unplug.done);
176 	ret = drmm_mutex_init(&ptdev->base, &ptdev->unplug.lock);
177 	if (ret)
178 		return ret;
179 
180 	ret = drmm_mutex_init(&ptdev->base, &ptdev->pm.mmio_lock);
181 	if (ret)
182 		return ret;
183 
184 #ifdef CONFIG_DEBUG_FS
185 	drmm_mutex_init(&ptdev->base, &ptdev->gems.lock);
186 	INIT_LIST_HEAD(&ptdev->gems.node);
187 #endif
188 
189 	atomic_set(&ptdev->pm.state, PANTHOR_DEVICE_PM_STATE_SUSPENDED);
190 	p = alloc_page(GFP_KERNEL | __GFP_ZERO);
191 	if (!p)
192 		return -ENOMEM;
193 
194 	ptdev->pm.dummy_latest_flush = p;
195 	dummy_page_virt = page_address(p);
196 	ret = drmm_add_action_or_reset(&ptdev->base, panthor_device_free_page,
197 				       ptdev->pm.dummy_latest_flush);
198 	if (ret)
199 		return ret;
200 
201 	/*
202 	 * Set the dummy page holding the latest flush to 1. This will cause the
203 	 * flush to avoided as we know it isn't necessary if the submission
204 	 * happens while the dummy page is mapped. Zero cannot be used because
205 	 * that means 'always flush'.
206 	 */
207 	*dummy_page_virt = 1;
208 
209 	INIT_WORK(&ptdev->reset.work, panthor_device_reset_work);
210 	ptdev->reset.wq = alloc_ordered_workqueue("panthor-reset-wq", 0);
211 	if (!ptdev->reset.wq)
212 		return -ENOMEM;
213 
214 	ret = drmm_add_action_or_reset(&ptdev->base, panthor_device_reset_cleanup, NULL);
215 	if (ret)
216 		return ret;
217 
218 	ret = panthor_clk_init(ptdev);
219 	if (ret)
220 		return ret;
221 
222 	ret = panthor_init_power(ptdev->base.dev);
223 	if (ret < 0) {
224 		drm_err(&ptdev->base, "init power domains failed, ret=%d", ret);
225 		return ret;
226 	}
227 
228 	ret = panthor_devfreq_init(ptdev);
229 	if (ret)
230 		return ret;
231 
232 	ptdev->iomem = devm_platform_get_and_ioremap_resource(to_platform_device(ptdev->base.dev),
233 							      0, &res);
234 	if (IS_ERR(ptdev->iomem))
235 		return PTR_ERR(ptdev->iomem);
236 
237 	ptdev->phys_addr = res->start;
238 
239 	ret = devm_pm_runtime_enable(ptdev->base.dev);
240 	if (ret)
241 		return ret;
242 
243 	ret = pm_runtime_resume_and_get(ptdev->base.dev);
244 	if (ret)
245 		return ret;
246 
247 	/* If PM is disabled, we need to call panthor_device_resume() manually. */
248 	if (!IS_ENABLED(CONFIG_PM)) {
249 		ret = panthor_device_resume(ptdev->base.dev);
250 		if (ret)
251 			return ret;
252 	}
253 
254 	ret = panthor_hw_init(ptdev);
255 	if (ret)
256 		goto err_rpm_put;
257 
258 	ret = panthor_pwr_init(ptdev);
259 	if (ret)
260 		goto err_rpm_put;
261 
262 	ret = panthor_gpu_init(ptdev);
263 	if (ret)
264 		goto err_unplug_pwr;
265 
266 	ret = panthor_gpu_coherency_init(ptdev);
267 	if (ret)
268 		goto err_unplug_gpu;
269 
270 	ret = panthor_gem_shrinker_init(ptdev);
271 	if (ret)
272 		goto err_unplug_gpu;
273 
274 	ret = panthor_mmu_init(ptdev);
275 	if (ret)
276 		goto err_unplug_shrinker;
277 
278 	ret = panthor_fw_init(ptdev);
279 	if (ret)
280 		goto err_unplug_mmu;
281 
282 	ret = panthor_sched_init(ptdev);
283 	if (ret)
284 		goto err_unplug_fw;
285 
286 	panthor_gem_init(ptdev);
287 
288 	/* ~3 frames */
289 	pm_runtime_set_autosuspend_delay(ptdev->base.dev, 50);
290 	pm_runtime_use_autosuspend(ptdev->base.dev);
291 
292 	ret = drm_dev_register(&ptdev->base, 0);
293 	if (ret)
294 		goto err_disable_autosuspend;
295 
296 	pm_runtime_put_autosuspend(ptdev->base.dev);
297 	return 0;
298 
299 err_disable_autosuspend:
300 	pm_runtime_dont_use_autosuspend(ptdev->base.dev);
301 	panthor_sched_unplug(ptdev);
302 
303 err_unplug_fw:
304 	panthor_fw_unplug(ptdev);
305 
306 err_unplug_mmu:
307 	panthor_mmu_unplug(ptdev);
308 
309 err_unplug_shrinker:
310 	panthor_gem_shrinker_unplug(ptdev);
311 
312 err_unplug_gpu:
313 	panthor_gpu_unplug(ptdev);
314 
315 err_unplug_pwr:
316 	panthor_pwr_unplug(ptdev);
317 
318 err_rpm_put:
319 	pm_runtime_put_sync_suspend(ptdev->base.dev);
320 	return ret;
321 }
322 
323 #define PANTHOR_EXCEPTION(id) \
324 	[DRM_PANTHOR_EXCEPTION_ ## id] = { \
325 		.name = #id, \
326 	}
327 
328 struct panthor_exception_info {
329 	const char *name;
330 };
331 
332 static const struct panthor_exception_info panthor_exception_infos[] = {
333 	PANTHOR_EXCEPTION(OK),
334 	PANTHOR_EXCEPTION(TERMINATED),
335 	PANTHOR_EXCEPTION(KABOOM),
336 	PANTHOR_EXCEPTION(EUREKA),
337 	PANTHOR_EXCEPTION(ACTIVE),
338 	PANTHOR_EXCEPTION(CS_RES_TERM),
339 	PANTHOR_EXCEPTION(CS_CONFIG_FAULT),
340 	PANTHOR_EXCEPTION(CS_UNRECOVERABLE),
341 	PANTHOR_EXCEPTION(CS_ENDPOINT_FAULT),
342 	PANTHOR_EXCEPTION(CS_BUS_FAULT),
343 	PANTHOR_EXCEPTION(CS_INSTR_INVALID),
344 	PANTHOR_EXCEPTION(CS_CALL_STACK_OVERFLOW),
345 	PANTHOR_EXCEPTION(CS_INHERIT_FAULT),
346 	PANTHOR_EXCEPTION(INSTR_INVALID_PC),
347 	PANTHOR_EXCEPTION(INSTR_INVALID_ENC),
348 	PANTHOR_EXCEPTION(INSTR_BARRIER_FAULT),
349 	PANTHOR_EXCEPTION(DATA_INVALID_FAULT),
350 	PANTHOR_EXCEPTION(TILE_RANGE_FAULT),
351 	PANTHOR_EXCEPTION(ADDR_RANGE_FAULT),
352 	PANTHOR_EXCEPTION(IMPRECISE_FAULT),
353 	PANTHOR_EXCEPTION(OOM),
354 	PANTHOR_EXCEPTION(CSF_FW_INTERNAL_ERROR),
355 	PANTHOR_EXCEPTION(CSF_RES_EVICTION_TIMEOUT),
356 	PANTHOR_EXCEPTION(GPU_BUS_FAULT),
357 	PANTHOR_EXCEPTION(GPU_SHAREABILITY_FAULT),
358 	PANTHOR_EXCEPTION(SYS_SHAREABILITY_FAULT),
359 	PANTHOR_EXCEPTION(GPU_CACHEABILITY_FAULT),
360 	PANTHOR_EXCEPTION(TRANSLATION_FAULT_0),
361 	PANTHOR_EXCEPTION(TRANSLATION_FAULT_1),
362 	PANTHOR_EXCEPTION(TRANSLATION_FAULT_2),
363 	PANTHOR_EXCEPTION(TRANSLATION_FAULT_3),
364 	PANTHOR_EXCEPTION(TRANSLATION_FAULT_4),
365 	PANTHOR_EXCEPTION(PERM_FAULT_0),
366 	PANTHOR_EXCEPTION(PERM_FAULT_1),
367 	PANTHOR_EXCEPTION(PERM_FAULT_2),
368 	PANTHOR_EXCEPTION(PERM_FAULT_3),
369 	PANTHOR_EXCEPTION(ACCESS_FLAG_1),
370 	PANTHOR_EXCEPTION(ACCESS_FLAG_2),
371 	PANTHOR_EXCEPTION(ACCESS_FLAG_3),
372 	PANTHOR_EXCEPTION(ADDR_SIZE_FAULT_IN),
373 	PANTHOR_EXCEPTION(ADDR_SIZE_FAULT_OUT0),
374 	PANTHOR_EXCEPTION(ADDR_SIZE_FAULT_OUT1),
375 	PANTHOR_EXCEPTION(ADDR_SIZE_FAULT_OUT2),
376 	PANTHOR_EXCEPTION(ADDR_SIZE_FAULT_OUT3),
377 	PANTHOR_EXCEPTION(MEM_ATTR_FAULT_0),
378 	PANTHOR_EXCEPTION(MEM_ATTR_FAULT_1),
379 	PANTHOR_EXCEPTION(MEM_ATTR_FAULT_2),
380 	PANTHOR_EXCEPTION(MEM_ATTR_FAULT_3),
381 };
382 
383 const char *panthor_exception_name(struct panthor_device *ptdev, u32 exception_code)
384 {
385 	if (exception_code >= ARRAY_SIZE(panthor_exception_infos) ||
386 	    !panthor_exception_infos[exception_code].name)
387 		return "Unknown exception type";
388 
389 	return panthor_exception_infos[exception_code].name;
390 }
391 
392 static vm_fault_t panthor_mmio_vm_fault(struct vm_fault *vmf)
393 {
394 	struct vm_area_struct *vma = vmf->vma;
395 	struct panthor_device *ptdev = vma->vm_private_data;
396 	u64 offset = (u64)vma->vm_pgoff << PAGE_SHIFT;
397 	unsigned long pfn;
398 	pgprot_t pgprot;
399 	vm_fault_t ret;
400 	bool active;
401 	int cookie;
402 
403 	if (!drm_dev_enter(&ptdev->base, &cookie))
404 		return VM_FAULT_SIGBUS;
405 
406 	mutex_lock(&ptdev->pm.mmio_lock);
407 	active = atomic_read(&ptdev->pm.state) == PANTHOR_DEVICE_PM_STATE_ACTIVE;
408 
409 	switch (offset) {
410 	case DRM_PANTHOR_USER_FLUSH_ID_MMIO_OFFSET:
411 		if (active)
412 			pfn = __phys_to_pfn(ptdev->phys_addr + CSF_GPU_LATEST_FLUSH_ID);
413 		else
414 			pfn = page_to_pfn(ptdev->pm.dummy_latest_flush);
415 		break;
416 
417 	default:
418 		ret = VM_FAULT_SIGBUS;
419 		goto out_unlock;
420 	}
421 
422 	pgprot = vma->vm_page_prot;
423 	if (active)
424 		pgprot = pgprot_noncached(pgprot);
425 
426 	ret = vmf_insert_pfn_prot(vma, vmf->address, pfn, pgprot);
427 
428 out_unlock:
429 	mutex_unlock(&ptdev->pm.mmio_lock);
430 	drm_dev_exit(cookie);
431 	return ret;
432 }
433 
434 static const struct vm_operations_struct panthor_mmio_vm_ops = {
435 	.fault = panthor_mmio_vm_fault,
436 };
437 
438 int panthor_device_mmap_io(struct panthor_device *ptdev, struct vm_area_struct *vma)
439 {
440 	u64 offset = (u64)vma->vm_pgoff << PAGE_SHIFT;
441 
442 	if ((vma->vm_flags & VM_SHARED) == 0)
443 		return -EINVAL;
444 
445 	switch (offset) {
446 	case DRM_PANTHOR_USER_FLUSH_ID_MMIO_OFFSET:
447 		if (vma->vm_end - vma->vm_start != PAGE_SIZE ||
448 		    (vma->vm_flags & (VM_WRITE | VM_EXEC)))
449 			return -EINVAL;
450 		vm_flags_clear(vma, VM_MAYWRITE);
451 
452 		break;
453 
454 	default:
455 		return -EINVAL;
456 	}
457 
458 	/* Defer actual mapping to the fault handler. */
459 	vma->vm_private_data = ptdev;
460 	vma->vm_ops = &panthor_mmio_vm_ops;
461 	vm_flags_set(vma,
462 		     VM_IO | VM_DONTCOPY | VM_DONTEXPAND |
463 		     VM_NORESERVE | VM_DONTDUMP | VM_PFNMAP);
464 	return 0;
465 }
466 
467 static int panthor_device_resume_hw_components(struct panthor_device *ptdev)
468 {
469 	int ret;
470 
471 	panthor_pwr_resume(ptdev);
472 	panthor_gpu_resume(ptdev);
473 	panthor_mmu_resume(ptdev);
474 
475 	ret = panthor_fw_resume(ptdev);
476 	if (!ret)
477 		return 0;
478 
479 	panthor_mmu_suspend(ptdev);
480 	panthor_gpu_suspend(ptdev);
481 	panthor_pwr_suspend(ptdev);
482 	return ret;
483 }
484 
485 int panthor_device_resume(struct device *dev)
486 {
487 	struct panthor_device *ptdev = dev_get_drvdata(dev);
488 	int ret, cookie;
489 
490 	if (atomic_read(&ptdev->pm.state) != PANTHOR_DEVICE_PM_STATE_SUSPENDED)
491 		return -EINVAL;
492 
493 	atomic_set(&ptdev->pm.state, PANTHOR_DEVICE_PM_STATE_RESUMING);
494 
495 	ret = clk_prepare_enable(ptdev->clks.core);
496 	if (ret)
497 		goto err_set_suspended;
498 
499 	ret = clk_prepare_enable(ptdev->clks.stacks);
500 	if (ret)
501 		goto err_disable_core_clk;
502 
503 	ret = clk_prepare_enable(ptdev->clks.coregroup);
504 	if (ret)
505 		goto err_disable_stacks_clk;
506 
507 	panthor_devfreq_resume(ptdev);
508 
509 	if (panthor_device_is_initialized(ptdev) &&
510 	    drm_dev_enter(&ptdev->base, &cookie)) {
511 		/* If there was a reset pending at the time we suspended the
512 		 * device, we force a slow reset.
513 		 */
514 		if (atomic_read(&ptdev->reset.pending)) {
515 			ptdev->reset.fast = false;
516 			atomic_set(&ptdev->reset.pending, 0);
517 		}
518 
519 		ret = panthor_device_resume_hw_components(ptdev);
520 		if (ret && ptdev->reset.fast) {
521 			drm_err(&ptdev->base, "Fast reset failed, trying a slow reset");
522 			ptdev->reset.fast = false;
523 			ret = panthor_device_resume_hw_components(ptdev);
524 		}
525 
526 		if (!ret)
527 			panthor_sched_resume(ptdev);
528 
529 		drm_dev_exit(cookie);
530 
531 		if (ret)
532 			goto err_suspend_devfreq;
533 	}
534 
535 	/* Clear all IOMEM mappings pointing to this device after we've
536 	 * resumed. This way the fake mappings pointing to the dummy pages
537 	 * are removed and the real iomem mapping will be restored on next
538 	 * access.
539 	 */
540 	mutex_lock(&ptdev->pm.mmio_lock);
541 	unmap_mapping_range(ptdev->base.anon_inode->i_mapping,
542 			    DRM_PANTHOR_USER_MMIO_OFFSET, 0, 1);
543 	atomic_set(&ptdev->pm.state, PANTHOR_DEVICE_PM_STATE_ACTIVE);
544 	mutex_unlock(&ptdev->pm.mmio_lock);
545 	return 0;
546 
547 err_suspend_devfreq:
548 	panthor_devfreq_suspend(ptdev);
549 	clk_disable_unprepare(ptdev->clks.coregroup);
550 
551 err_disable_stacks_clk:
552 	clk_disable_unprepare(ptdev->clks.stacks);
553 
554 err_disable_core_clk:
555 	clk_disable_unprepare(ptdev->clks.core);
556 
557 err_set_suspended:
558 	atomic_set(&ptdev->pm.state, PANTHOR_DEVICE_PM_STATE_SUSPENDED);
559 	atomic_set(&ptdev->pm.recovery_needed, 1);
560 	return ret;
561 }
562 
563 int panthor_device_suspend(struct device *dev)
564 {
565 	struct panthor_device *ptdev = dev_get_drvdata(dev);
566 	int cookie;
567 
568 	if (atomic_read(&ptdev->pm.state) != PANTHOR_DEVICE_PM_STATE_ACTIVE)
569 		return -EINVAL;
570 
571 	/* Clear all IOMEM mappings pointing to this device before we
572 	 * shutdown the power-domain and clocks. Failing to do that results
573 	 * in external aborts when the process accesses the iomem region.
574 	 * We change the state and call unmap_mapping_range() with the
575 	 * mmio_lock held to make sure the vm_fault handler won't set up
576 	 * invalid mappings.
577 	 */
578 	mutex_lock(&ptdev->pm.mmio_lock);
579 	atomic_set(&ptdev->pm.state, PANTHOR_DEVICE_PM_STATE_SUSPENDING);
580 	unmap_mapping_range(ptdev->base.anon_inode->i_mapping,
581 			    DRM_PANTHOR_USER_MMIO_OFFSET, 0, 1);
582 	mutex_unlock(&ptdev->pm.mmio_lock);
583 
584 	if (panthor_device_is_initialized(ptdev) &&
585 	    drm_dev_enter(&ptdev->base, &cookie)) {
586 		cancel_work_sync(&ptdev->reset.work);
587 
588 		/* We prepare everything as if we were resetting the GPU.
589 		 * The end of the reset will happen in the resume path though.
590 		 */
591 		panthor_sched_suspend(ptdev);
592 		panthor_fw_suspend(ptdev);
593 		panthor_mmu_suspend(ptdev);
594 		panthor_gpu_suspend(ptdev);
595 		panthor_pwr_suspend(ptdev);
596 		drm_dev_exit(cookie);
597 	}
598 
599 	panthor_devfreq_suspend(ptdev);
600 
601 	clk_disable_unprepare(ptdev->clks.coregroup);
602 	clk_disable_unprepare(ptdev->clks.stacks);
603 	clk_disable_unprepare(ptdev->clks.core);
604 	atomic_set(&ptdev->pm.state, PANTHOR_DEVICE_PM_STATE_SUSPENDED);
605 	return 0;
606 }
607