1 // SPDX-License-Identifier: MIT
3 * Copyright © 2021 Intel Corporation
6 #include <linux/debugfs.h>
10 #include "intel_atomic.h"
12 #include "intel_display_types.h"
13 #include "intel_drrs.h"
14 #include "intel_frontbuffer.h"
15 #include "intel_panel.h"
18 * DOC: Display Refresh Rate Switching (DRRS)
20 * Display Refresh Rate Switching (DRRS) is a power conservation feature
21 * which enables swtching between low and high refresh rates,
22 * dynamically, based on the usage scenario. This feature is applicable
23 * for internal panels.
25 * Indication that the panel supports DRRS is given by the panel EDID, which
26 * would list multiple refresh rates for one resolution.
28 * DRRS is of 2 types - static and seamless.
29 * Static DRRS involves changing refresh rate (RR) by doing a full modeset
30 * (may appear as a blink on screen) and is used in dock-undock scenario.
31 * Seamless DRRS involves changing RR without any visual effect to the user
32 * and can be used during normal system usage. This is done by programming
35 * Support for static/seamless DRRS may be indicated in the VBT based on
36 * inputs from the panel spec.
38 * DRRS saves power by switching to low RR based on usage scenarios.
40 * The implementation is based on frontbuffer tracking implementation. When
41 * there is a disturbance on the screen triggered by user activity or a periodic
42 * system activity, DRRS is disabled (RR is changed to high RR). When there is
43 * no movement on screen, after a timeout of 1 second, a switch to low RR is
46 * For integration with frontbuffer tracking code, intel_drrs_invalidate()
47 * and intel_drrs_flush() are called.
49 * DRRS can be further extended to support other internal panels and also
50 * the scenario of video playback wherein RR is set based on the rate
51 * requested by userspace.
54 const char *intel_drrs_type_str(enum drrs_type drrs_type)
56 static const char * const str[] = {
57 [DRRS_TYPE_NONE] = "none",
58 [DRRS_TYPE_STATIC] = "static",
59 [DRRS_TYPE_SEAMLESS] = "seamless",
62 if (drrs_type >= ARRAY_SIZE(str))
65 return str[drrs_type];
68 bool intel_cpu_transcoder_has_drrs(struct drm_i915_private *i915,
69 enum transcoder cpu_transcoder)
71 struct intel_display *display = &i915->display;
73 if (HAS_DOUBLE_BUFFERED_M_N(display))
76 return intel_cpu_transcoder_has_m2_n2(i915, cpu_transcoder);
80 intel_drrs_set_refresh_rate_pipeconf(struct intel_crtc *crtc,
81 enum drrs_refresh_rate refresh_rate)
83 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
84 enum transcoder cpu_transcoder = crtc->drrs.cpu_transcoder;
87 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
88 bit = TRANSCONF_REFRESH_RATE_ALT_VLV;
90 bit = TRANSCONF_REFRESH_RATE_ALT_ILK;
92 intel_de_rmw(dev_priv, TRANSCONF(dev_priv, cpu_transcoder),
93 bit, refresh_rate == DRRS_REFRESH_RATE_LOW ? bit : 0);
97 intel_drrs_set_refresh_rate_m_n(struct intel_crtc *crtc,
98 enum drrs_refresh_rate refresh_rate)
100 intel_cpu_transcoder_set_m1_n1(crtc, crtc->drrs.cpu_transcoder,
101 refresh_rate == DRRS_REFRESH_RATE_LOW ?
102 &crtc->drrs.m2_n2 : &crtc->drrs.m_n);
105 bool intel_drrs_is_active(struct intel_crtc *crtc)
107 return crtc->drrs.cpu_transcoder != INVALID_TRANSCODER;
110 static void intel_drrs_set_state(struct intel_crtc *crtc,
111 enum drrs_refresh_rate refresh_rate)
113 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
115 if (refresh_rate == crtc->drrs.refresh_rate)
118 if (intel_cpu_transcoder_has_m2_n2(dev_priv, crtc->drrs.cpu_transcoder))
119 intel_drrs_set_refresh_rate_pipeconf(crtc, refresh_rate);
121 intel_drrs_set_refresh_rate_m_n(crtc, refresh_rate);
123 crtc->drrs.refresh_rate = refresh_rate;
126 static void intel_drrs_schedule_work(struct intel_crtc *crtc)
128 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
130 mod_delayed_work(i915->unordered_wq, &crtc->drrs.work, msecs_to_jiffies(1000));
133 static unsigned int intel_drrs_frontbuffer_bits(const struct intel_crtc_state *crtc_state)
135 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
136 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
137 unsigned int frontbuffer_bits;
139 frontbuffer_bits = INTEL_FRONTBUFFER_ALL_MASK(crtc->pipe);
141 for_each_intel_crtc_in_pipe_mask(&i915->drm, crtc,
142 crtc_state->joiner_pipes)
143 frontbuffer_bits |= INTEL_FRONTBUFFER_ALL_MASK(crtc->pipe);
145 return frontbuffer_bits;
149 * intel_drrs_activate - activate DRRS
150 * @crtc_state: the crtc state
152 * Activates DRRS on the crtc.
154 void intel_drrs_activate(const struct intel_crtc_state *crtc_state)
156 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
158 if (!crtc_state->has_drrs)
161 if (!crtc_state->hw.active)
164 if (intel_crtc_is_joiner_secondary(crtc_state))
167 mutex_lock(&crtc->drrs.mutex);
169 crtc->drrs.cpu_transcoder = crtc_state->cpu_transcoder;
170 crtc->drrs.m_n = crtc_state->dp_m_n;
171 crtc->drrs.m2_n2 = crtc_state->dp_m2_n2;
172 crtc->drrs.frontbuffer_bits = intel_drrs_frontbuffer_bits(crtc_state);
173 crtc->drrs.busy_frontbuffer_bits = 0;
175 intel_drrs_schedule_work(crtc);
177 mutex_unlock(&crtc->drrs.mutex);
181 * intel_drrs_deactivate - deactivate DRRS
182 * @old_crtc_state: the old crtc state
184 * Deactivates DRRS on the crtc.
186 void intel_drrs_deactivate(const struct intel_crtc_state *old_crtc_state)
188 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
190 if (!old_crtc_state->has_drrs)
193 if (!old_crtc_state->hw.active)
196 if (intel_crtc_is_joiner_secondary(old_crtc_state))
199 mutex_lock(&crtc->drrs.mutex);
201 if (intel_drrs_is_active(crtc))
202 intel_drrs_set_state(crtc, DRRS_REFRESH_RATE_HIGH);
204 crtc->drrs.cpu_transcoder = INVALID_TRANSCODER;
205 crtc->drrs.frontbuffer_bits = 0;
206 crtc->drrs.busy_frontbuffer_bits = 0;
208 mutex_unlock(&crtc->drrs.mutex);
210 cancel_delayed_work_sync(&crtc->drrs.work);
213 static void intel_drrs_downclock_work(struct work_struct *work)
215 struct intel_crtc *crtc = container_of(work, typeof(*crtc), drrs.work.work);
217 mutex_lock(&crtc->drrs.mutex);
219 if (intel_drrs_is_active(crtc) && !crtc->drrs.busy_frontbuffer_bits)
220 intel_drrs_set_state(crtc, DRRS_REFRESH_RATE_LOW);
222 mutex_unlock(&crtc->drrs.mutex);
225 static void intel_drrs_frontbuffer_update(struct drm_i915_private *dev_priv,
226 unsigned int all_frontbuffer_bits,
229 struct intel_crtc *crtc;
231 for_each_intel_crtc(&dev_priv->drm, crtc) {
232 unsigned int frontbuffer_bits;
234 mutex_lock(&crtc->drrs.mutex);
236 frontbuffer_bits = all_frontbuffer_bits & crtc->drrs.frontbuffer_bits;
237 if (!frontbuffer_bits) {
238 mutex_unlock(&crtc->drrs.mutex);
243 crtc->drrs.busy_frontbuffer_bits |= frontbuffer_bits;
245 crtc->drrs.busy_frontbuffer_bits &= ~frontbuffer_bits;
247 /* flush/invalidate means busy screen hence upclock */
248 intel_drrs_set_state(crtc, DRRS_REFRESH_RATE_HIGH);
251 * flush also means no more activity hence schedule downclock, if all
252 * other fbs are quiescent too
254 if (!crtc->drrs.busy_frontbuffer_bits)
255 intel_drrs_schedule_work(crtc);
257 cancel_delayed_work(&crtc->drrs.work);
259 mutex_unlock(&crtc->drrs.mutex);
264 * intel_drrs_invalidate - Disable Idleness DRRS
265 * @dev_priv: i915 device
266 * @frontbuffer_bits: frontbuffer plane tracking bits
268 * This function gets called everytime rendering on the given planes start.
269 * Hence DRRS needs to be Upclocked, i.e. (LOW_RR -> HIGH_RR).
271 * Dirty frontbuffers relevant to DRRS are tracked in busy_frontbuffer_bits.
273 void intel_drrs_invalidate(struct drm_i915_private *dev_priv,
274 unsigned int frontbuffer_bits)
276 intel_drrs_frontbuffer_update(dev_priv, frontbuffer_bits, true);
280 * intel_drrs_flush - Restart Idleness DRRS
281 * @dev_priv: i915 device
282 * @frontbuffer_bits: frontbuffer plane tracking bits
284 * This function gets called every time rendering on the given planes has
285 * completed or flip on a crtc is completed. So DRRS should be upclocked
286 * (LOW_RR -> HIGH_RR). And also Idleness detection should be started again,
287 * if no other planes are dirty.
289 * Dirty frontbuffers relevant to DRRS are tracked in busy_frontbuffer_bits.
291 void intel_drrs_flush(struct drm_i915_private *dev_priv,
292 unsigned int frontbuffer_bits)
294 intel_drrs_frontbuffer_update(dev_priv, frontbuffer_bits, false);
298 * intel_drrs_crtc_init - Init DRRS for CRTC
301 * This function is called only once at driver load to initialize basic
305 void intel_drrs_crtc_init(struct intel_crtc *crtc)
307 INIT_DELAYED_WORK(&crtc->drrs.work, intel_drrs_downclock_work);
308 mutex_init(&crtc->drrs.mutex);
309 crtc->drrs.cpu_transcoder = INVALID_TRANSCODER;
312 static int intel_drrs_debugfs_status_show(struct seq_file *m, void *unused)
314 struct intel_crtc *crtc = m->private;
315 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
316 const struct intel_crtc_state *crtc_state;
319 ret = drm_modeset_lock_single_interruptible(&crtc->base.mutex);
323 crtc_state = to_intel_crtc_state(crtc->base.state);
325 mutex_lock(&crtc->drrs.mutex);
327 seq_printf(m, "DRRS capable: %s\n",
328 str_yes_no(intel_cpu_transcoder_has_drrs(i915,
329 crtc_state->cpu_transcoder)));
331 seq_printf(m, "DRRS enabled: %s\n",
332 str_yes_no(crtc_state->has_drrs));
334 seq_printf(m, "DRRS active: %s\n",
335 str_yes_no(intel_drrs_is_active(crtc)));
337 seq_printf(m, "DRRS refresh rate: %s\n",
338 crtc->drrs.refresh_rate == DRRS_REFRESH_RATE_LOW ?
341 seq_printf(m, "DRRS busy frontbuffer bits: 0x%x\n",
342 crtc->drrs.busy_frontbuffer_bits);
344 mutex_unlock(&crtc->drrs.mutex);
346 drm_modeset_unlock(&crtc->base.mutex);
351 DEFINE_SHOW_ATTRIBUTE(intel_drrs_debugfs_status);
353 static int intel_drrs_debugfs_ctl_set(void *data, u64 val)
355 struct intel_crtc *crtc = data;
356 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
357 struct intel_crtc_state *crtc_state;
358 struct drm_crtc_commit *commit;
361 ret = drm_modeset_lock_single_interruptible(&crtc->base.mutex);
365 crtc_state = to_intel_crtc_state(crtc->base.state);
367 if (!crtc_state->hw.active ||
368 !crtc_state->has_drrs)
371 commit = crtc_state->uapi.commit;
373 ret = wait_for_completion_interruptible(&commit->hw_done);
379 "Manually %sactivating DRRS\n", val ? "" : "de");
382 intel_drrs_activate(crtc_state);
384 intel_drrs_deactivate(crtc_state);
387 drm_modeset_unlock(&crtc->base.mutex);
392 DEFINE_DEBUGFS_ATTRIBUTE(intel_drrs_debugfs_ctl_fops,
393 NULL, intel_drrs_debugfs_ctl_set, "%llu\n");
395 void intel_drrs_crtc_debugfs_add(struct intel_crtc *crtc)
397 debugfs_create_file("i915_drrs_status", 0444, crtc->base.debugfs_entry,
398 crtc, &intel_drrs_debugfs_status_fops);
400 debugfs_create_file_unsafe("i915_drrs_ctl", 0644, crtc->base.debugfs_entry,
401 crtc, &intel_drrs_debugfs_ctl_fops);
404 static int intel_drrs_debugfs_type_show(struct seq_file *m, void *unused)
406 struct intel_connector *connector = m->private;
408 seq_printf(m, "DRRS type: %s\n",
409 intel_drrs_type_str(intel_panel_drrs_type(connector)));
414 DEFINE_SHOW_ATTRIBUTE(intel_drrs_debugfs_type);
416 void intel_drrs_connector_debugfs_add(struct intel_connector *connector)
418 if (intel_panel_drrs_type(connector) == DRRS_TYPE_NONE)
421 debugfs_create_file("i915_drrs_type", 0444, connector->base.debugfs_entry,
422 connector, &intel_drrs_debugfs_type_fops);