xref: /linux/drivers/gpu/drm/v3d/v3d_power.c (revision c27e360545373b7aee9862a5beef3b9fb3df0c25)
1 // SPDX-License-Identifier: GPL-2.0+
2 /* Copyright (C) 2026 Raspberry Pi */
3 
4 #include <linux/clk.h>
5 
6 #include <drm/drm_print.h>
7 
8 #include "v3d_drv.h"
9 #include "v3d_regs.h"
10 
11 static int
12 v3d_resume_sms(struct v3d_dev *v3d)
13 {
14 	if (v3d->ver < V3D_GEN_71)
15 		return 0;
16 
17 	V3D_SMS_WRITE(V3D_SMS_TEE_CS, V3D_SMS_CLEAR_POWER_OFF);
18 
19 	if (wait_for((V3D_GET_FIELD(V3D_SMS_READ(V3D_SMS_TEE_CS),
20 				    V3D_SMS_STATE) == V3D_SMS_IDLE), 100)) {
21 		drm_err(&v3d->drm, "Failed to power up SMS\n");
22 		return -ETIMEDOUT;
23 	}
24 
25 	v3d_reset_sms(v3d);
26 
27 	return 0;
28 }
29 
30 static int
31 v3d_suspend_sms(struct v3d_dev *v3d)
32 {
33 	if (v3d->ver < V3D_GEN_71)
34 		return 0;
35 
36 	V3D_SMS_WRITE(V3D_SMS_TEE_CS, V3D_SMS_POWER_OFF);
37 
38 	if (wait_for((V3D_GET_FIELD(V3D_SMS_READ(V3D_SMS_TEE_CS),
39 				    V3D_SMS_STATE) == V3D_SMS_POWER_OFF_STATE), 100)) {
40 		drm_err(&v3d->drm, "Failed to power off SMS\n");
41 		return -ETIMEDOUT;
42 	}
43 
44 	return 0;
45 }
46 
47 int v3d_power_suspend(struct device *dev)
48 {
49 	struct drm_device *drm = dev_get_drvdata(dev);
50 	struct v3d_dev *v3d = to_v3d_dev(drm);
51 	int ret;
52 
53 	v3d_irq_disable(v3d);
54 
55 	v3d_clean_caches(v3d);
56 
57 	/* Wait until V3D has no active or pending AXI transactions. */
58 	v3d_idle_axi(v3d, 0);
59 	v3d_idle_gca(v3d);
60 
61 	ret = v3d_suspend_sms(v3d);
62 	if (ret) {
63 		/* Staying active: undo the GMP STOP_REQ from v3d_idle_axi(). */
64 		V3D_WRITE(V3D_GMP_CFG(v3d->ver),
65 			  V3D_READ(V3D_GMP_CFG(v3d->ver)) & ~V3D_GMP_CFG_STOP_REQ);
66 		v3d_irq_enable(v3d);
67 		return ret;
68 	}
69 
70 	clk_disable_unprepare(v3d->clk);
71 
72 	return 0;
73 }
74 
75 int v3d_power_resume(struct device *dev)
76 {
77 	struct drm_device *drm = dev_get_drvdata(dev);
78 	struct v3d_dev *v3d = to_v3d_dev(drm);
79 	int ret;
80 
81 	ret = clk_prepare_enable(v3d->clk);
82 	if (ret)
83 		return ret;
84 
85 	ret = v3d_resume_sms(v3d);
86 	if (ret) {
87 		clk_disable_unprepare(v3d->clk);
88 		return ret;
89 	}
90 
91 	v3d_init_hw_state(v3d);
92 	v3d_mmu_set_page_table(v3d);
93 	v3d_irq_enable(v3d);
94 
95 	return 0;
96 }
97