1 From cf8b55ab71c780cefff2c152949303e82559701c Mon Sep 17 00:00:00 2001
2 From: Dom Cobley <popcornmix@gmail.com>
3 Date: Mon, 26 Jul 2021 18:03:53 +0100
4 Subject: [PATCH] drm/vc4: Increase the core clock based on HVS load
6 Depending on a given HVS output (HVS to PixelValves) and input (planes
7 attached to a channel) load, the HVS needs for the core clock to be
8 raised above its boot time default.
10 Failing to do so will result in a vblank timeout and a stalled display
13 Signed-off-by: Maxime Ripard <maxime@cerno.tech>
15 drivers/gpu/drm/vc4/vc4_crtc.c | 15 +++++
16 drivers/gpu/drm/vc4/vc4_drv.h | 3 +
17 drivers/gpu/drm/vc4/vc4_kms.c | 116 ++++++++++++++++++++++++++++++++-
18 3 files changed, 131 insertions(+), 3 deletions(-)
20 --- a/drivers/gpu/drm/vc4/vc4_crtc.c
21 +++ b/drivers/gpu/drm/vc4/vc4_crtc.c
22 @@ -660,12 +660,27 @@ static int vc4_crtc_atomic_check(struct
23 struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state);
24 struct drm_connector *conn;
25 struct drm_connector_state *conn_state;
26 + struct drm_encoder *encoder;
29 ret = vc4_hvs_atomic_check(crtc, state);
33 + encoder = vc4_get_crtc_encoder(crtc, crtc_state);
35 + const struct drm_display_mode *mode = &crtc_state->adjusted_mode;
36 + struct vc4_encoder *vc4_encoder = to_vc4_encoder(encoder);
38 + mode = &crtc_state->adjusted_mode;
39 + if (vc4_encoder->type == VC4_ENCODER_TYPE_HDMI0) {
40 + vc4_state->hvs_load = max(mode->clock * mode->hdisplay / mode->htotal + 1000,
41 + mode->clock * 9 / 10) * 1000;
43 + vc4_state->hvs_load = mode->clock * 1000;
47 for_each_new_connector_in_state(state, conn, conn_state,
49 if (conn_state->crtc != crtc)
50 --- a/drivers/gpu/drm/vc4/vc4_drv.h
51 +++ b/drivers/gpu/drm/vc4/vc4_drv.h
52 @@ -326,6 +326,7 @@ struct vc4_hvs {
56 + struct clk_request *core_req;
58 /* Memory manager for CRTCs to allocate space in the display
59 * list. Units are dwords.
60 @@ -537,6 +538,8 @@ struct vc4_crtc_state {
64 + unsigned long hvs_load;
66 /* Transitional state below, only valid during atomic commits */
69 --- a/drivers/gpu/drm/vc4/vc4_kms.c
70 +++ b/drivers/gpu/drm/vc4/vc4_kms.c
71 @@ -40,6 +40,9 @@ static struct vc4_ctm_state *to_vc4_ctm_
72 struct vc4_hvs_state {
73 struct drm_private_state base;
74 unsigned int unassigned_channels;
75 + unsigned int num_outputs;
76 + unsigned long fifo_load;
77 + unsigned long core_clock_rate;
80 static struct vc4_hvs_state *
81 @@ -186,6 +189,19 @@ vc4_ctm_commit(struct vc4_dev *vc4, stru
84 static struct vc4_hvs_state *
85 +vc4_hvs_get_new_global_state(struct drm_atomic_state *state)
87 + struct vc4_dev *vc4 = to_vc4_dev(state->dev);
88 + struct drm_private_state *priv_state;
90 + priv_state = drm_atomic_get_new_private_obj_state(state, &vc4->hvs_channels);
91 + if (IS_ERR(priv_state))
92 + return ERR_CAST(priv_state);
94 + return to_vc4_hvs_state(priv_state);
97 +static struct vc4_hvs_state *
98 vc4_hvs_get_global_state(struct drm_atomic_state *state)
100 struct vc4_dev *vc4 = to_vc4_dev(state->dev);
101 @@ -312,10 +328,15 @@ vc4_atomic_complete_commit(struct drm_at
102 struct vc4_dev *vc4 = to_vc4_dev(dev);
103 struct vc4_hvs *hvs = vc4->hvs;
104 struct drm_crtc_state *new_crtc_state;
105 + struct vc4_hvs_state *hvs_state;
106 struct drm_crtc *crtc;
107 struct clk_request *core_req;
110 + hvs_state = vc4_hvs_get_new_global_state(state);
111 + if (WARN_ON(!hvs_state))
114 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
115 struct vc4_crtc_state *vc4_crtc_state;
117 @@ -326,9 +347,20 @@ vc4_atomic_complete_commit(struct drm_at
118 vc4_hvs_mask_underrun(dev, vc4_crtc_state->assigned_channel);
121 - if (vc4->hvs && vc4->hvs->hvs5)
122 + if (vc4->hvs && vc4->hvs->hvs5) {
124 + * Do a temporary request on the core clock during the
127 core_req = clk_request_start(hvs->core_clk, 500000000);
130 + * And remove the previous one based on the HVS
131 + * requirements if any.
133 + clk_request_done(hvs->core_req);
136 drm_atomic_helper_wait_for_fences(dev, state, false);
138 drm_atomic_helper_wait_for_dependencies(state);
139 @@ -358,8 +390,20 @@ vc4_atomic_complete_commit(struct drm_at
141 drm_atomic_helper_commit_cleanup_done(state);
143 - if (vc4->hvs && vc4->hvs->hvs5)
144 + if (vc4->hvs && vc4->hvs->hvs5) {
145 + drm_dbg(dev, "Running the core clock at %lu Hz\n",
146 + hvs_state->core_clock_rate);
149 + * Request a clock rate based on the current HVS
152 + hvs->core_req = clk_request_start(hvs->core_clk,
153 + hvs_state->core_clock_rate);
155 + /* And drop the temporary request */
156 clk_request_done(core_req);
159 drm_atomic_state_put(state);
161 @@ -703,6 +747,9 @@ vc4_hvs_channels_duplicate_state(struct
162 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
164 state->unassigned_channels = old_state->unassigned_channels;
165 + state->fifo_load = old_state->fifo_load;
166 + state->num_outputs = old_state->num_outputs;
167 + state->core_clock_rate = old_state->core_clock_rate;
171 @@ -849,6 +896,65 @@ static int vc4_pv_muxing_atomic_check(st
175 +vc4_core_clock_atomic_check(struct drm_atomic_state *state)
177 + struct vc4_dev *vc4 = to_vc4_dev(state->dev);
178 + struct drm_private_state *priv_state;
179 + struct vc4_hvs_state *hvs_new_state;
180 + struct vc4_load_tracker_state *load_state;
181 + struct drm_crtc_state *old_crtc_state, *new_crtc_state;
182 + struct drm_crtc *crtc;
183 + unsigned long pixel_rate;
184 + unsigned long cob_rate;
187 + priv_state = drm_atomic_get_private_obj_state(state,
188 + &vc4->load_tracker);
189 + if (IS_ERR(priv_state))
190 + return PTR_ERR(priv_state);
192 + load_state = to_vc4_load_tracker_state(priv_state);
194 + hvs_new_state = vc4_hvs_get_global_state(state);
195 + if (!hvs_new_state)
198 + for_each_oldnew_crtc_in_state(state, crtc,
202 + if (old_crtc_state->active) {
203 + struct vc4_crtc_state *old_vc4_state =
204 + to_vc4_crtc_state(old_crtc_state);
206 + hvs_new_state->num_outputs -= 1;
207 + hvs_new_state->fifo_load -= old_vc4_state->hvs_load;
210 + if (new_crtc_state->active) {
211 + struct vc4_crtc_state *new_vc4_state =
212 + to_vc4_crtc_state(new_crtc_state);
214 + hvs_new_state->num_outputs += 1;
215 + hvs_new_state->fifo_load += new_vc4_state->hvs_load;
219 + cob_rate = hvs_new_state->fifo_load;
220 + pixel_rate = load_state->hvs_load;
221 + if (hvs_new_state->num_outputs > 1) {
222 + pixel_rate = (pixel_rate * 40) / 100;
224 + pixel_rate = (pixel_rate * 60) / 100;
227 + hvs_new_state->core_clock_rate = max(cob_rate, pixel_rate);
234 vc4_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
237 @@ -865,7 +971,11 @@ vc4_atomic_check(struct drm_device *dev,
241 - return vc4_load_tracker_atomic_check(state);
242 + ret = vc4_load_tracker_atomic_check(state);
246 + return vc4_core_clock_atomic_check(state);
249 static const struct drm_mode_config_funcs vc4_mode_funcs = {