xref: /linux/drivers/gpu/drm/i915/display/hsw_ips.c (revision a713222906e4f77b5fb1b5346d4f5de1adc639b4)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2022 Intel Corporation
4  */
5 
6 #include <linux/debugfs.h>
7 
8 #include "hsw_ips.h"
9 #include "i915_drv.h"
10 #include "i915_reg.h"
11 #include "intel_color_regs.h"
12 #include "intel_de.h"
13 #include "intel_display_rpm.h"
14 #include "intel_display_types.h"
15 #include "intel_pcode.h"
16 
17 static void hsw_ips_enable(const struct intel_crtc_state *crtc_state)
18 {
19 	struct intel_display *display = to_intel_display(crtc_state);
20 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
21 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
22 	u32 val;
23 
24 	if (!crtc_state->ips_enabled)
25 		return;
26 
27 	/*
28 	 * We can only enable IPS after we enable a plane and wait for a vblank
29 	 * This function is called from post_plane_update, which is run after
30 	 * a vblank wait.
31 	 */
32 	drm_WARN_ON(display->drm,
33 		    !(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
34 
35 	val = IPS_ENABLE;
36 
37 	if (display->ips.false_color)
38 		val |= IPS_FALSE_COLOR;
39 
40 	if (display->platform.broadwell) {
41 		drm_WARN_ON(display->drm,
42 			    snb_pcode_write(&i915->uncore, DISPLAY_IPS_CONTROL,
43 					    val | IPS_PCODE_CONTROL));
44 		/*
45 		 * Quoting Art Runyan: "its not safe to expect any particular
46 		 * value in IPS_CTL bit 31 after enabling IPS through the
47 		 * mailbox." Moreover, the mailbox may return a bogus state,
48 		 * so we need to just enable it and continue on.
49 		 */
50 	} else {
51 		intel_de_write(display, IPS_CTL, val);
52 		/*
53 		 * The bit only becomes 1 in the next vblank, so this wait here
54 		 * is essentially intel_wait_for_vblank. If we don't have this
55 		 * and don't wait for vblanks until the end of crtc_enable, then
56 		 * the HW state readout code will complain that the expected
57 		 * IPS_CTL value is not the one we read.
58 		 */
59 		if (intel_de_wait_for_set(display, IPS_CTL, IPS_ENABLE, 50))
60 			drm_err(display->drm,
61 				"Timed out waiting for IPS enable\n");
62 	}
63 }
64 
65 bool hsw_ips_disable(const struct intel_crtc_state *crtc_state)
66 {
67 	struct intel_display *display = to_intel_display(crtc_state);
68 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
69 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
70 	bool need_vblank_wait = false;
71 
72 	if (!crtc_state->ips_enabled)
73 		return need_vblank_wait;
74 
75 	if (display->platform.broadwell) {
76 		drm_WARN_ON(display->drm,
77 			    snb_pcode_write(&i915->uncore, DISPLAY_IPS_CONTROL, 0));
78 		/*
79 		 * Wait for PCODE to finish disabling IPS. The BSpec specified
80 		 * 42ms timeout value leads to occasional timeouts so use 100ms
81 		 * instead.
82 		 */
83 		if (intel_de_wait_for_clear(display, IPS_CTL, IPS_ENABLE, 100))
84 			drm_err(display->drm,
85 				"Timed out waiting for IPS disable\n");
86 	} else {
87 		intel_de_write(display, IPS_CTL, 0);
88 		intel_de_posting_read(display, IPS_CTL);
89 	}
90 
91 	/* We need to wait for a vblank before we can disable the plane. */
92 	need_vblank_wait = true;
93 
94 	return need_vblank_wait;
95 }
96 
97 static bool hsw_ips_need_disable(struct intel_atomic_state *state,
98 				 struct intel_crtc *crtc)
99 {
100 	struct intel_display *display = to_intel_display(state);
101 	const struct intel_crtc_state *old_crtc_state =
102 		intel_atomic_get_old_crtc_state(state, crtc);
103 	const struct intel_crtc_state *new_crtc_state =
104 		intel_atomic_get_new_crtc_state(state, crtc);
105 
106 	if (!old_crtc_state->ips_enabled)
107 		return false;
108 
109 	if (intel_crtc_needs_modeset(new_crtc_state))
110 		return true;
111 
112 	/*
113 	 * Workaround : Do not read or write the pipe palette/gamma data while
114 	 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
115 	 *
116 	 * Disable IPS before we program the LUT.
117 	 */
118 	if (display->platform.haswell &&
119 	    intel_crtc_needs_color_update(new_crtc_state) &&
120 	    new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
121 		return true;
122 
123 	return !new_crtc_state->ips_enabled;
124 }
125 
126 bool hsw_ips_pre_update(struct intel_atomic_state *state,
127 			struct intel_crtc *crtc)
128 {
129 	const struct intel_crtc_state *old_crtc_state =
130 		intel_atomic_get_old_crtc_state(state, crtc);
131 
132 	if (!hsw_ips_need_disable(state, crtc))
133 		return false;
134 
135 	return hsw_ips_disable(old_crtc_state);
136 }
137 
138 static bool hsw_ips_need_enable(struct intel_atomic_state *state,
139 				struct intel_crtc *crtc)
140 {
141 	struct intel_display *display = to_intel_display(state);
142 	const struct intel_crtc_state *old_crtc_state =
143 		intel_atomic_get_old_crtc_state(state, crtc);
144 	const struct intel_crtc_state *new_crtc_state =
145 		intel_atomic_get_new_crtc_state(state, crtc);
146 
147 	if (!new_crtc_state->ips_enabled)
148 		return false;
149 
150 	if (intel_crtc_needs_modeset(new_crtc_state))
151 		return true;
152 
153 	/*
154 	 * Workaround : Do not read or write the pipe palette/gamma data while
155 	 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
156 	 *
157 	 * Re-enable IPS after the LUT has been programmed.
158 	 */
159 	if (display->platform.haswell &&
160 	    intel_crtc_needs_color_update(new_crtc_state) &&
161 	    new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
162 		return true;
163 
164 	/*
165 	 * We can't read out IPS on broadwell, assume the worst and
166 	 * forcibly enable IPS on the first fastset.
167 	 */
168 	if (intel_crtc_needs_fastset(new_crtc_state) && old_crtc_state->inherited)
169 		return true;
170 
171 	return !old_crtc_state->ips_enabled;
172 }
173 
174 void hsw_ips_post_update(struct intel_atomic_state *state,
175 			 struct intel_crtc *crtc)
176 {
177 	const struct intel_crtc_state *new_crtc_state =
178 		intel_atomic_get_new_crtc_state(state, crtc);
179 
180 	if (!hsw_ips_need_enable(state, crtc))
181 		return;
182 
183 	hsw_ips_enable(new_crtc_state);
184 }
185 
186 /* IPS only exists on ULT machines and is tied to pipe A. */
187 bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
188 {
189 	struct intel_display *display = to_intel_display(crtc);
190 
191 	return HAS_IPS(display) && crtc->pipe == PIPE_A;
192 }
193 
194 static bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
195 {
196 	struct intel_display *display = to_intel_display(crtc_state);
197 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
198 
199 	/* IPS only exists on ULT machines and is tied to pipe A. */
200 	if (!hsw_crtc_supports_ips(crtc))
201 		return false;
202 
203 	if (!display->params.enable_ips)
204 		return false;
205 
206 	if (crtc_state->pipe_bpp > 24)
207 		return false;
208 
209 	/*
210 	 * We compare against max which means we must take
211 	 * the increased cdclk requirement into account when
212 	 * calculating the new cdclk.
213 	 *
214 	 * Should measure whether using a lower cdclk w/o IPS
215 	 */
216 	if (display->platform.broadwell &&
217 	    crtc_state->pixel_rate > display->cdclk.max_cdclk_freq * 95 / 100)
218 		return false;
219 
220 	return true;
221 }
222 
223 int hsw_ips_min_cdclk(const struct intel_crtc_state *crtc_state)
224 {
225 	struct intel_display *display = to_intel_display(crtc_state);
226 
227 	if (!display->platform.broadwell)
228 		return 0;
229 
230 	if (!hsw_crtc_state_ips_capable(crtc_state))
231 		return 0;
232 
233 	/* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
234 	return DIV_ROUND_UP(crtc_state->pixel_rate * 100, 95);
235 }
236 
237 int hsw_ips_compute_config(struct intel_atomic_state *state,
238 			   struct intel_crtc *crtc)
239 {
240 	struct intel_display *display = to_intel_display(state);
241 	struct intel_crtc_state *crtc_state =
242 		intel_atomic_get_new_crtc_state(state, crtc);
243 
244 	crtc_state->ips_enabled = false;
245 
246 	if (!hsw_crtc_state_ips_capable(crtc_state))
247 		return 0;
248 
249 	/*
250 	 * When IPS gets enabled, the pipe CRC changes. Since IPS gets
251 	 * enabled and disabled dynamically based on package C states,
252 	 * user space can't make reliable use of the CRCs, so let's just
253 	 * completely disable it.
254 	 */
255 	if (crtc_state->crc_enabled)
256 		return 0;
257 
258 	/* IPS should be fine as long as at least one plane is enabled. */
259 	if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
260 		return 0;
261 
262 	if (display->platform.broadwell) {
263 		const struct intel_cdclk_state *cdclk_state;
264 
265 		cdclk_state = intel_atomic_get_cdclk_state(state);
266 		if (IS_ERR(cdclk_state))
267 			return PTR_ERR(cdclk_state);
268 
269 		/* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
270 		if (crtc_state->pixel_rate > cdclk_state->logical.cdclk * 95 / 100)
271 			return 0;
272 	}
273 
274 	crtc_state->ips_enabled = true;
275 
276 	return 0;
277 }
278 
279 void hsw_ips_get_config(struct intel_crtc_state *crtc_state)
280 {
281 	struct intel_display *display = to_intel_display(crtc_state);
282 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
283 
284 	if (!hsw_crtc_supports_ips(crtc))
285 		return;
286 
287 	if (display->platform.haswell) {
288 		crtc_state->ips_enabled = intel_de_read(display, IPS_CTL) & IPS_ENABLE;
289 	} else {
290 		/*
291 		 * We cannot readout IPS state on broadwell, set to
292 		 * true so we can set it to a defined state on first
293 		 * commit.
294 		 */
295 		crtc_state->ips_enabled = true;
296 	}
297 }
298 
299 static int hsw_ips_debugfs_false_color_get(void *data, u64 *val)
300 {
301 	struct intel_crtc *crtc = data;
302 	struct intel_display *display = to_intel_display(crtc);
303 
304 	*val = display->ips.false_color;
305 
306 	return 0;
307 }
308 
309 static int hsw_ips_debugfs_false_color_set(void *data, u64 val)
310 {
311 	struct intel_crtc *crtc = data;
312 	struct intel_display *display = to_intel_display(crtc);
313 	struct intel_crtc_state *crtc_state;
314 	int ret;
315 
316 	ret = drm_modeset_lock(&crtc->base.mutex, NULL);
317 	if (ret)
318 		return ret;
319 
320 	display->ips.false_color = val;
321 
322 	crtc_state = to_intel_crtc_state(crtc->base.state);
323 
324 	if (!crtc_state->hw.active)
325 		goto unlock;
326 
327 	if (crtc_state->uapi.commit &&
328 	    !try_wait_for_completion(&crtc_state->uapi.commit->hw_done))
329 		goto unlock;
330 
331 	hsw_ips_enable(crtc_state);
332 
333  unlock:
334 	drm_modeset_unlock(&crtc->base.mutex);
335 
336 	return ret;
337 }
338 
339 DEFINE_DEBUGFS_ATTRIBUTE(hsw_ips_debugfs_false_color_fops,
340 			 hsw_ips_debugfs_false_color_get,
341 			 hsw_ips_debugfs_false_color_set,
342 			 "%llu\n");
343 
344 static int hsw_ips_debugfs_status_show(struct seq_file *m, void *unused)
345 {
346 	struct intel_crtc *crtc = m->private;
347 	struct intel_display *display = to_intel_display(crtc);
348 	struct ref_tracker *wakeref;
349 
350 	wakeref = intel_display_rpm_get(display);
351 
352 	seq_printf(m, "Enabled by kernel parameter: %s\n",
353 		   str_yes_no(display->params.enable_ips));
354 
355 	if (DISPLAY_VER(display) >= 8) {
356 		seq_puts(m, "Currently: unknown\n");
357 	} else {
358 		if (intel_de_read(display, IPS_CTL) & IPS_ENABLE)
359 			seq_puts(m, "Currently: enabled\n");
360 		else
361 			seq_puts(m, "Currently: disabled\n");
362 	}
363 
364 	intel_display_rpm_put(display, wakeref);
365 
366 	return 0;
367 }
368 
369 DEFINE_SHOW_ATTRIBUTE(hsw_ips_debugfs_status);
370 
371 void hsw_ips_crtc_debugfs_add(struct intel_crtc *crtc)
372 {
373 	if (!hsw_crtc_supports_ips(crtc))
374 		return;
375 
376 	debugfs_create_file("i915_ips_false_color", 0644, crtc->base.debugfs_entry,
377 			    crtc, &hsw_ips_debugfs_false_color_fops);
378 
379 	debugfs_create_file("i915_ips_status", 0444, crtc->base.debugfs_entry,
380 			    crtc, &hsw_ips_debugfs_status_fops);
381 }
382