xref: /linux/drivers/gpu/drm/i915/display/intel_cursor.c (revision 3e0bc2855b573bcffa2a52955a878f537f5ac0cd)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2020 Intel Corporation
4  */
5 #include <linux/kernel.h>
6 
7 #include <drm/drm_atomic_helper.h>
8 #include <drm/drm_atomic_uapi.h>
9 #include <drm/drm_blend.h>
10 #include <drm/drm_damage_helper.h>
11 #include <drm/drm_fourcc.h>
12 
13 #include "i915_reg.h"
14 #include "intel_atomic.h"
15 #include "intel_atomic_plane.h"
16 #include "intel_cursor.h"
17 #include "intel_de.h"
18 #include "intel_display.h"
19 #include "intel_display_types.h"
20 #include "intel_fb.h"
21 #include "intel_fb_pin.h"
22 #include "intel_frontbuffer.h"
23 #include "intel_psr.h"
24 #include "intel_psr_regs.h"
25 #include "skl_watermark.h"
26 
27 #include "gem/i915_gem_object.h"
28 
29 /* Cursor formats */
30 static const u32 intel_cursor_formats[] = {
31 	DRM_FORMAT_ARGB8888,
32 };
33 
34 static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
35 {
36 	struct drm_i915_private *dev_priv =
37 		to_i915(plane_state->uapi.plane->dev);
38 	const struct drm_framebuffer *fb = plane_state->hw.fb;
39 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
40 	u32 base;
41 
42 	if (DISPLAY_INFO(dev_priv)->cursor_needs_physical)
43 		base = i915_gem_object_get_dma_address(obj, 0);
44 	else
45 		base = intel_plane_ggtt_offset(plane_state);
46 
47 	return base + plane_state->view.color_plane[0].offset;
48 }
49 
50 static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
51 {
52 	int x = plane_state->uapi.dst.x1;
53 	int y = plane_state->uapi.dst.y1;
54 	u32 pos = 0;
55 
56 	if (x < 0) {
57 		pos |= CURSOR_POS_X_SIGN;
58 		x = -x;
59 	}
60 	pos |= CURSOR_POS_X(x);
61 
62 	if (y < 0) {
63 		pos |= CURSOR_POS_Y_SIGN;
64 		y = -y;
65 	}
66 	pos |= CURSOR_POS_Y(y);
67 
68 	return pos;
69 }
70 
71 static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
72 {
73 	const struct drm_mode_config *config =
74 		&plane_state->uapi.plane->dev->mode_config;
75 	int width = drm_rect_width(&plane_state->uapi.dst);
76 	int height = drm_rect_height(&plane_state->uapi.dst);
77 
78 	return width > 0 && width <= config->cursor_width &&
79 		height > 0 && height <= config->cursor_height;
80 }
81 
82 static int intel_cursor_check_surface(struct intel_plane_state *plane_state)
83 {
84 	struct drm_i915_private *dev_priv =
85 		to_i915(plane_state->uapi.plane->dev);
86 	unsigned int rotation = plane_state->hw.rotation;
87 	int src_x, src_y;
88 	u32 offset;
89 	int ret;
90 
91 	ret = intel_plane_compute_gtt(plane_state);
92 	if (ret)
93 		return ret;
94 
95 	if (!plane_state->uapi.visible)
96 		return 0;
97 
98 	src_x = plane_state->uapi.src.x1 >> 16;
99 	src_y = plane_state->uapi.src.y1 >> 16;
100 
101 	intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
102 	offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
103 						    plane_state, 0);
104 
105 	if (src_x != 0 || src_y != 0) {
106 		drm_dbg_kms(&dev_priv->drm,
107 			    "Arbitrary cursor panning not supported\n");
108 		return -EINVAL;
109 	}
110 
111 	/*
112 	 * Put the final coordinates back so that the src
113 	 * coordinate checks will see the right values.
114 	 */
115 	drm_rect_translate_to(&plane_state->uapi.src,
116 			      src_x << 16, src_y << 16);
117 
118 	/* ILK+ do this automagically in hardware */
119 	if (HAS_GMCH(dev_priv) && rotation & DRM_MODE_ROTATE_180) {
120 		const struct drm_framebuffer *fb = plane_state->hw.fb;
121 		int src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
122 		int src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
123 
124 		offset += (src_h * src_w - 1) * fb->format->cpp[0];
125 	}
126 
127 	plane_state->view.color_plane[0].offset = offset;
128 	plane_state->view.color_plane[0].x = src_x;
129 	plane_state->view.color_plane[0].y = src_y;
130 
131 	return 0;
132 }
133 
134 static int intel_check_cursor(struct intel_crtc_state *crtc_state,
135 			      struct intel_plane_state *plane_state)
136 {
137 	const struct drm_framebuffer *fb = plane_state->hw.fb;
138 	struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
139 	const struct drm_rect src = plane_state->uapi.src;
140 	const struct drm_rect dst = plane_state->uapi.dst;
141 	int ret;
142 
143 	if (fb && fb->modifier != DRM_FORMAT_MOD_LINEAR) {
144 		drm_dbg_kms(&i915->drm, "cursor cannot be tiled\n");
145 		return -EINVAL;
146 	}
147 
148 	ret = intel_atomic_plane_check_clipping(plane_state, crtc_state,
149 						DRM_PLANE_NO_SCALING,
150 						DRM_PLANE_NO_SCALING,
151 						true);
152 	if (ret)
153 		return ret;
154 
155 	/* Use the unclipped src/dst rectangles, which we program to hw */
156 	plane_state->uapi.src = src;
157 	plane_state->uapi.dst = dst;
158 
159 	/* final plane coordinates will be relative to the plane's pipe */
160 	drm_rect_translate(&plane_state->uapi.dst,
161 			   -crtc_state->pipe_src.x1,
162 			   -crtc_state->pipe_src.y1);
163 
164 	ret = intel_cursor_check_surface(plane_state);
165 	if (ret)
166 		return ret;
167 
168 	if (!plane_state->uapi.visible)
169 		return 0;
170 
171 	ret = intel_plane_check_src_coordinates(plane_state);
172 	if (ret)
173 		return ret;
174 
175 	return 0;
176 }
177 
178 static unsigned int
179 i845_cursor_max_stride(struct intel_plane *plane,
180 		       u32 pixel_format, u64 modifier,
181 		       unsigned int rotation)
182 {
183 	return 2048;
184 }
185 
186 static u32 i845_cursor_ctl_crtc(const struct intel_crtc_state *crtc_state)
187 {
188 	u32 cntl = 0;
189 
190 	if (crtc_state->gamma_enable)
191 		cntl |= CURSOR_PIPE_GAMMA_ENABLE;
192 
193 	return cntl;
194 }
195 
196 static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
197 			   const struct intel_plane_state *plane_state)
198 {
199 	return CURSOR_ENABLE |
200 		CURSOR_FORMAT_ARGB |
201 		CURSOR_STRIDE(plane_state->view.color_plane[0].mapping_stride);
202 }
203 
204 static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
205 {
206 	int width = drm_rect_width(&plane_state->uapi.dst);
207 
208 	/*
209 	 * 845g/865g are only limited by the width of their cursors,
210 	 * the height is arbitrary up to the precision of the register.
211 	 */
212 	return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
213 }
214 
215 static int i845_check_cursor(struct intel_crtc_state *crtc_state,
216 			     struct intel_plane_state *plane_state)
217 {
218 	const struct drm_framebuffer *fb = plane_state->hw.fb;
219 	struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
220 	int ret;
221 
222 	ret = intel_check_cursor(crtc_state, plane_state);
223 	if (ret)
224 		return ret;
225 
226 	/* if we want to turn off the cursor ignore width and height */
227 	if (!fb)
228 		return 0;
229 
230 	/* Check for which cursor types we support */
231 	if (!i845_cursor_size_ok(plane_state)) {
232 		drm_dbg_kms(&i915->drm,
233 			    "Cursor dimension %dx%d not supported\n",
234 			    drm_rect_width(&plane_state->uapi.dst),
235 			    drm_rect_height(&plane_state->uapi.dst));
236 		return -EINVAL;
237 	}
238 
239 	drm_WARN_ON(&i915->drm, plane_state->uapi.visible &&
240 		    plane_state->view.color_plane[0].mapping_stride != fb->pitches[0]);
241 
242 	switch (fb->pitches[0]) {
243 	case 256:
244 	case 512:
245 	case 1024:
246 	case 2048:
247 		break;
248 	default:
249 		 drm_dbg_kms(&i915->drm, "Invalid cursor stride (%u)\n",
250 			     fb->pitches[0]);
251 		return -EINVAL;
252 	}
253 
254 	plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
255 
256 	return 0;
257 }
258 
259 /* TODO: split into noarm+arm pair */
260 static void i845_cursor_update_arm(struct intel_plane *plane,
261 				   const struct intel_crtc_state *crtc_state,
262 				   const struct intel_plane_state *plane_state)
263 {
264 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
265 	u32 cntl = 0, base = 0, pos = 0, size = 0;
266 
267 	if (plane_state && plane_state->uapi.visible) {
268 		unsigned int width = drm_rect_width(&plane_state->uapi.dst);
269 		unsigned int height = drm_rect_height(&plane_state->uapi.dst);
270 
271 		cntl = plane_state->ctl |
272 			i845_cursor_ctl_crtc(crtc_state);
273 
274 		size = CURSOR_HEIGHT(height) | CURSOR_WIDTH(width);
275 
276 		base = intel_cursor_base(plane_state);
277 		pos = intel_cursor_position(plane_state);
278 	}
279 
280 	/* On these chipsets we can only modify the base/size/stride
281 	 * whilst the cursor is disabled.
282 	 */
283 	if (plane->cursor.base != base ||
284 	    plane->cursor.size != size ||
285 	    plane->cursor.cntl != cntl) {
286 		intel_de_write_fw(dev_priv, CURCNTR(PIPE_A), 0);
287 		intel_de_write_fw(dev_priv, CURBASE(PIPE_A), base);
288 		intel_de_write_fw(dev_priv, CURSIZE(PIPE_A), size);
289 		intel_de_write_fw(dev_priv, CURPOS(PIPE_A), pos);
290 		intel_de_write_fw(dev_priv, CURCNTR(PIPE_A), cntl);
291 
292 		plane->cursor.base = base;
293 		plane->cursor.size = size;
294 		plane->cursor.cntl = cntl;
295 	} else {
296 		intel_de_write_fw(dev_priv, CURPOS(PIPE_A), pos);
297 	}
298 }
299 
300 static void i845_cursor_disable_arm(struct intel_plane *plane,
301 				    const struct intel_crtc_state *crtc_state)
302 {
303 	i845_cursor_update_arm(plane, crtc_state, NULL);
304 }
305 
306 static bool i845_cursor_get_hw_state(struct intel_plane *plane,
307 				     enum pipe *pipe)
308 {
309 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
310 	enum intel_display_power_domain power_domain;
311 	intel_wakeref_t wakeref;
312 	bool ret;
313 
314 	power_domain = POWER_DOMAIN_PIPE(PIPE_A);
315 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
316 	if (!wakeref)
317 		return false;
318 
319 	ret = intel_de_read(dev_priv, CURCNTR(PIPE_A)) & CURSOR_ENABLE;
320 
321 	*pipe = PIPE_A;
322 
323 	intel_display_power_put(dev_priv, power_domain, wakeref);
324 
325 	return ret;
326 }
327 
328 static unsigned int
329 i9xx_cursor_max_stride(struct intel_plane *plane,
330 		       u32 pixel_format, u64 modifier,
331 		       unsigned int rotation)
332 {
333 	return plane->base.dev->mode_config.cursor_width * 4;
334 }
335 
336 static u32 i9xx_cursor_ctl_crtc(const struct intel_crtc_state *crtc_state)
337 {
338 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
339 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
340 	u32 cntl = 0;
341 
342 	if (DISPLAY_VER(dev_priv) >= 11)
343 		return cntl;
344 
345 	if (crtc_state->gamma_enable)
346 		cntl = MCURSOR_PIPE_GAMMA_ENABLE;
347 
348 	if (crtc_state->csc_enable)
349 		cntl |= MCURSOR_PIPE_CSC_ENABLE;
350 
351 	if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv))
352 		cntl |= MCURSOR_PIPE_SEL(crtc->pipe);
353 
354 	return cntl;
355 }
356 
357 static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
358 			   const struct intel_plane_state *plane_state)
359 {
360 	struct drm_i915_private *dev_priv =
361 		to_i915(plane_state->uapi.plane->dev);
362 	u32 cntl = 0;
363 
364 	if (IS_SANDYBRIDGE(dev_priv) || IS_IVYBRIDGE(dev_priv))
365 		cntl |= MCURSOR_TRICKLE_FEED_DISABLE;
366 
367 	switch (drm_rect_width(&plane_state->uapi.dst)) {
368 	case 64:
369 		cntl |= MCURSOR_MODE_64_ARGB_AX;
370 		break;
371 	case 128:
372 		cntl |= MCURSOR_MODE_128_ARGB_AX;
373 		break;
374 	case 256:
375 		cntl |= MCURSOR_MODE_256_ARGB_AX;
376 		break;
377 	default:
378 		MISSING_CASE(drm_rect_width(&plane_state->uapi.dst));
379 		return 0;
380 	}
381 
382 	if (plane_state->hw.rotation & DRM_MODE_ROTATE_180)
383 		cntl |= MCURSOR_ROTATE_180;
384 
385 	/* Wa_22012358565:adl-p */
386 	if (DISPLAY_VER(dev_priv) == 13)
387 		cntl |= MCURSOR_ARB_SLOTS(1);
388 
389 	return cntl;
390 }
391 
392 static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
393 {
394 	struct drm_i915_private *dev_priv =
395 		to_i915(plane_state->uapi.plane->dev);
396 	int width = drm_rect_width(&plane_state->uapi.dst);
397 	int height = drm_rect_height(&plane_state->uapi.dst);
398 
399 	if (!intel_cursor_size_ok(plane_state))
400 		return false;
401 
402 	/* Cursor width is limited to a few power-of-two sizes */
403 	switch (width) {
404 	case 256:
405 	case 128:
406 	case 64:
407 		break;
408 	default:
409 		return false;
410 	}
411 
412 	/*
413 	 * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
414 	 * height from 8 lines up to the cursor width, when the
415 	 * cursor is not rotated. Everything else requires square
416 	 * cursors.
417 	 */
418 	if (HAS_CUR_FBC(dev_priv) &&
419 	    plane_state->hw.rotation & DRM_MODE_ROTATE_0) {
420 		if (height < 8 || height > width)
421 			return false;
422 	} else {
423 		if (height != width)
424 			return false;
425 	}
426 
427 	return true;
428 }
429 
430 static int i9xx_check_cursor(struct intel_crtc_state *crtc_state,
431 			     struct intel_plane_state *plane_state)
432 {
433 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
434 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
435 	const struct drm_framebuffer *fb = plane_state->hw.fb;
436 	enum pipe pipe = plane->pipe;
437 	int ret;
438 
439 	ret = intel_check_cursor(crtc_state, plane_state);
440 	if (ret)
441 		return ret;
442 
443 	/* if we want to turn off the cursor ignore width and height */
444 	if (!fb)
445 		return 0;
446 
447 	/* Check for which cursor types we support */
448 	if (!i9xx_cursor_size_ok(plane_state)) {
449 		drm_dbg(&dev_priv->drm,
450 			"Cursor dimension %dx%d not supported\n",
451 			drm_rect_width(&plane_state->uapi.dst),
452 			drm_rect_height(&plane_state->uapi.dst));
453 		return -EINVAL;
454 	}
455 
456 	drm_WARN_ON(&dev_priv->drm, plane_state->uapi.visible &&
457 		    plane_state->view.color_plane[0].mapping_stride != fb->pitches[0]);
458 
459 	if (fb->pitches[0] !=
460 	    drm_rect_width(&plane_state->uapi.dst) * fb->format->cpp[0]) {
461 		drm_dbg_kms(&dev_priv->drm,
462 			    "Invalid cursor stride (%u) (cursor width %d)\n",
463 			    fb->pitches[0],
464 			    drm_rect_width(&plane_state->uapi.dst));
465 		return -EINVAL;
466 	}
467 
468 	/*
469 	 * There's something wrong with the cursor on CHV pipe C.
470 	 * If it straddles the left edge of the screen then
471 	 * moving it away from the edge or disabling it often
472 	 * results in a pipe underrun, and often that can lead to
473 	 * dead pipe (constant underrun reported, and it scans
474 	 * out just a solid color). To recover from that, the
475 	 * display power well must be turned off and on again.
476 	 * Refuse the put the cursor into that compromised position.
477 	 */
478 	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
479 	    plane_state->uapi.visible && plane_state->uapi.dst.x1 < 0) {
480 		drm_dbg_kms(&dev_priv->drm,
481 			    "CHV cursor C not allowed to straddle the left screen edge\n");
482 		return -EINVAL;
483 	}
484 
485 	plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
486 
487 	return 0;
488 }
489 
490 static void i9xx_cursor_disable_sel_fetch_arm(struct intel_plane *plane,
491 					      const struct intel_crtc_state *crtc_state)
492 {
493 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
494 	enum pipe pipe = plane->pipe;
495 
496 	if (!crtc_state->enable_psr2_sel_fetch)
497 		return;
498 
499 	intel_de_write_fw(dev_priv, PLANE_SEL_FETCH_CTL(pipe, plane->id), 0);
500 }
501 
502 static void i9xx_cursor_update_sel_fetch_arm(struct intel_plane *plane,
503 					     const struct intel_crtc_state *crtc_state,
504 					     const struct intel_plane_state *plane_state)
505 {
506 	struct drm_i915_private *i915 = to_i915(plane->base.dev);
507 	enum pipe pipe = plane->pipe;
508 
509 	if (!crtc_state->enable_psr2_sel_fetch)
510 		return;
511 
512 	if (drm_rect_height(&plane_state->psr2_sel_fetch_area) > 0)
513 		intel_de_write_fw(i915, PLANE_SEL_FETCH_CTL(pipe, plane->id),
514 				  plane_state->ctl);
515 	else
516 		i9xx_cursor_disable_sel_fetch_arm(plane, crtc_state);
517 }
518 
519 /* TODO: split into noarm+arm pair */
520 static void i9xx_cursor_update_arm(struct intel_plane *plane,
521 				   const struct intel_crtc_state *crtc_state,
522 				   const struct intel_plane_state *plane_state)
523 {
524 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
525 	enum pipe pipe = plane->pipe;
526 	u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
527 
528 	if (plane_state && plane_state->uapi.visible) {
529 		int width = drm_rect_width(&plane_state->uapi.dst);
530 		int height = drm_rect_height(&plane_state->uapi.dst);
531 
532 		cntl = plane_state->ctl |
533 			i9xx_cursor_ctl_crtc(crtc_state);
534 
535 		if (width != height)
536 			fbc_ctl = CUR_FBC_EN | CUR_FBC_HEIGHT(height - 1);
537 
538 		base = intel_cursor_base(plane_state);
539 		pos = intel_cursor_position(plane_state);
540 	}
541 
542 	/*
543 	 * On some platforms writing CURCNTR first will also
544 	 * cause CURPOS to be armed by the CURBASE write.
545 	 * Without the CURCNTR write the CURPOS write would
546 	 * arm itself. Thus we always update CURCNTR before
547 	 * CURPOS.
548 	 *
549 	 * On other platforms CURPOS always requires the
550 	 * CURBASE write to arm the update. Additonally
551 	 * a write to any of the cursor register will cancel
552 	 * an already armed cursor update. Thus leaving out
553 	 * the CURBASE write after CURPOS could lead to a
554 	 * cursor that doesn't appear to move, or even change
555 	 * shape. Thus we always write CURBASE.
556 	 *
557 	 * The other registers are armed by the CURBASE write
558 	 * except when the plane is getting enabled at which time
559 	 * the CURCNTR write arms the update.
560 	 */
561 
562 	if (DISPLAY_VER(dev_priv) >= 9)
563 		skl_write_cursor_wm(plane, crtc_state);
564 
565 	if (plane_state)
566 		i9xx_cursor_update_sel_fetch_arm(plane, crtc_state,
567 						 plane_state);
568 	else
569 		i9xx_cursor_disable_sel_fetch_arm(plane, crtc_state);
570 
571 	if (plane->cursor.base != base ||
572 	    plane->cursor.size != fbc_ctl ||
573 	    plane->cursor.cntl != cntl) {
574 		if (HAS_CUR_FBC(dev_priv))
575 			intel_de_write_fw(dev_priv, CUR_FBC_CTL(pipe),
576 					  fbc_ctl);
577 		intel_de_write_fw(dev_priv, CURCNTR(pipe), cntl);
578 		intel_de_write_fw(dev_priv, CURPOS(pipe), pos);
579 		intel_de_write_fw(dev_priv, CURBASE(pipe), base);
580 
581 		plane->cursor.base = base;
582 		plane->cursor.size = fbc_ctl;
583 		plane->cursor.cntl = cntl;
584 	} else {
585 		intel_de_write_fw(dev_priv, CURPOS(pipe), pos);
586 		intel_de_write_fw(dev_priv, CURBASE(pipe), base);
587 	}
588 }
589 
590 static void i9xx_cursor_disable_arm(struct intel_plane *plane,
591 				    const struct intel_crtc_state *crtc_state)
592 {
593 	i9xx_cursor_update_arm(plane, crtc_state, NULL);
594 }
595 
596 static bool i9xx_cursor_get_hw_state(struct intel_plane *plane,
597 				     enum pipe *pipe)
598 {
599 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
600 	enum intel_display_power_domain power_domain;
601 	intel_wakeref_t wakeref;
602 	bool ret;
603 	u32 val;
604 
605 	/*
606 	 * Not 100% correct for planes that can move between pipes,
607 	 * but that's only the case for gen2-3 which don't have any
608 	 * display power wells.
609 	 */
610 	power_domain = POWER_DOMAIN_PIPE(plane->pipe);
611 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
612 	if (!wakeref)
613 		return false;
614 
615 	val = intel_de_read(dev_priv, CURCNTR(plane->pipe));
616 
617 	ret = val & MCURSOR_MODE_MASK;
618 
619 	if (DISPLAY_VER(dev_priv) >= 5 || IS_G4X(dev_priv))
620 		*pipe = plane->pipe;
621 	else
622 		*pipe = REG_FIELD_GET(MCURSOR_PIPE_SEL_MASK, val);
623 
624 	intel_display_power_put(dev_priv, power_domain, wakeref);
625 
626 	return ret;
627 }
628 
629 static bool intel_cursor_format_mod_supported(struct drm_plane *_plane,
630 					      u32 format, u64 modifier)
631 {
632 	if (!intel_fb_plane_supports_modifier(to_intel_plane(_plane), modifier))
633 		return false;
634 
635 	return format == DRM_FORMAT_ARGB8888;
636 }
637 
638 static int
639 intel_legacy_cursor_update(struct drm_plane *_plane,
640 			   struct drm_crtc *_crtc,
641 			   struct drm_framebuffer *fb,
642 			   int crtc_x, int crtc_y,
643 			   unsigned int crtc_w, unsigned int crtc_h,
644 			   u32 src_x, u32 src_y,
645 			   u32 src_w, u32 src_h,
646 			   struct drm_modeset_acquire_ctx *ctx)
647 {
648 	struct intel_plane *plane = to_intel_plane(_plane);
649 	struct intel_crtc *crtc = to_intel_crtc(_crtc);
650 	struct intel_plane_state *old_plane_state =
651 		to_intel_plane_state(plane->base.state);
652 	struct intel_plane_state *new_plane_state;
653 	struct intel_crtc_state *crtc_state =
654 		to_intel_crtc_state(crtc->base.state);
655 	struct intel_crtc_state *new_crtc_state;
656 	int ret;
657 
658 	/*
659 	 * When crtc is inactive or there is a modeset pending,
660 	 * wait for it to complete in the slowpath.
661 	 * PSR2 selective fetch also requires the slow path as
662 	 * PSR2 plane and transcoder registers can only be updated during
663 	 * vblank.
664 	 *
665 	 * FIXME bigjoiner fastpath would be good
666 	 */
667 	if (!crtc_state->hw.active ||
668 	    intel_crtc_needs_modeset(crtc_state) ||
669 	    intel_crtc_needs_fastset(crtc_state) ||
670 	    crtc_state->bigjoiner_pipes)
671 		goto slow;
672 
673 	/*
674 	 * Don't do an async update if there is an outstanding commit modifying
675 	 * the plane.  This prevents our async update's changes from getting
676 	 * overridden by a previous synchronous update's state.
677 	 */
678 	if (old_plane_state->uapi.commit &&
679 	    !try_wait_for_completion(&old_plane_state->uapi.commit->hw_done))
680 		goto slow;
681 
682 	/*
683 	 * If any parameters change that may affect watermarks,
684 	 * take the slowpath. Only changing fb or position should be
685 	 * in the fastpath.
686 	 */
687 	if (old_plane_state->uapi.crtc != &crtc->base ||
688 	    old_plane_state->uapi.src_w != src_w ||
689 	    old_plane_state->uapi.src_h != src_h ||
690 	    old_plane_state->uapi.crtc_w != crtc_w ||
691 	    old_plane_state->uapi.crtc_h != crtc_h ||
692 	    !old_plane_state->uapi.fb != !fb)
693 		goto slow;
694 
695 	new_plane_state = to_intel_plane_state(intel_plane_duplicate_state(&plane->base));
696 	if (!new_plane_state)
697 		return -ENOMEM;
698 
699 	new_crtc_state = to_intel_crtc_state(intel_crtc_duplicate_state(&crtc->base));
700 	if (!new_crtc_state) {
701 		ret = -ENOMEM;
702 		goto out_free;
703 	}
704 
705 	drm_atomic_set_fb_for_plane(&new_plane_state->uapi, fb);
706 
707 	new_plane_state->uapi.src_x = src_x;
708 	new_plane_state->uapi.src_y = src_y;
709 	new_plane_state->uapi.src_w = src_w;
710 	new_plane_state->uapi.src_h = src_h;
711 	new_plane_state->uapi.crtc_x = crtc_x;
712 	new_plane_state->uapi.crtc_y = crtc_y;
713 	new_plane_state->uapi.crtc_w = crtc_w;
714 	new_plane_state->uapi.crtc_h = crtc_h;
715 
716 	intel_plane_copy_uapi_to_hw_state(new_plane_state, new_plane_state, crtc);
717 
718 	ret = intel_plane_atomic_check_with_state(crtc_state, new_crtc_state,
719 						  old_plane_state, new_plane_state);
720 	if (ret)
721 		goto out_free;
722 
723 	ret = intel_plane_pin_fb(new_plane_state);
724 	if (ret)
725 		goto out_free;
726 
727 	intel_frontbuffer_flush(to_intel_frontbuffer(new_plane_state->hw.fb),
728 				ORIGIN_CURSOR_UPDATE);
729 	intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->hw.fb),
730 				to_intel_frontbuffer(new_plane_state->hw.fb),
731 				plane->frontbuffer_bit);
732 
733 	/* Swap plane state */
734 	plane->base.state = &new_plane_state->uapi;
735 
736 	/*
737 	 * We cannot swap crtc_state as it may be in use by an atomic commit or
738 	 * page flip that's running simultaneously. If we swap crtc_state and
739 	 * destroy the old state, we will cause a use-after-free there.
740 	 *
741 	 * Only update active_planes, which is needed for our internal
742 	 * bookkeeping. Either value will do the right thing when updating
743 	 * planes atomically. If the cursor was part of the atomic update then
744 	 * we would have taken the slowpath.
745 	 */
746 	crtc_state->active_planes = new_crtc_state->active_planes;
747 
748 	/*
749 	 * Technically we should do a vblank evasion here to make
750 	 * sure all the cursor registers update on the same frame.
751 	 * For now just make sure the register writes happen as
752 	 * quickly as possible to minimize the race window.
753 	 */
754 	local_irq_disable();
755 
756 	if (new_plane_state->uapi.visible) {
757 		intel_plane_update_noarm(plane, crtc_state, new_plane_state);
758 		intel_plane_update_arm(plane, crtc_state, new_plane_state);
759 	} else {
760 		intel_plane_disable_arm(plane, crtc_state);
761 	}
762 
763 	local_irq_enable();
764 
765 	intel_plane_unpin_fb(old_plane_state);
766 
767 out_free:
768 	if (new_crtc_state)
769 		intel_crtc_destroy_state(&crtc->base, &new_crtc_state->uapi);
770 	if (ret)
771 		intel_plane_destroy_state(&plane->base, &new_plane_state->uapi);
772 	else
773 		intel_plane_destroy_state(&plane->base, &old_plane_state->uapi);
774 	return ret;
775 
776 slow:
777 	return drm_atomic_helper_update_plane(&plane->base, &crtc->base, fb,
778 					      crtc_x, crtc_y, crtc_w, crtc_h,
779 					      src_x, src_y, src_w, src_h, ctx);
780 }
781 
782 static const struct drm_plane_funcs intel_cursor_plane_funcs = {
783 	.update_plane = intel_legacy_cursor_update,
784 	.disable_plane = drm_atomic_helper_disable_plane,
785 	.destroy = intel_plane_destroy,
786 	.atomic_duplicate_state = intel_plane_duplicate_state,
787 	.atomic_destroy_state = intel_plane_destroy_state,
788 	.format_mod_supported = intel_cursor_format_mod_supported,
789 };
790 
791 struct intel_plane *
792 intel_cursor_plane_create(struct drm_i915_private *dev_priv,
793 			  enum pipe pipe)
794 {
795 	struct intel_plane *cursor;
796 	int ret, zpos;
797 	u64 *modifiers;
798 
799 	cursor = intel_plane_alloc();
800 	if (IS_ERR(cursor))
801 		return cursor;
802 
803 	cursor->pipe = pipe;
804 	cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
805 	cursor->id = PLANE_CURSOR;
806 	cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id);
807 
808 	if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
809 		cursor->max_stride = i845_cursor_max_stride;
810 		cursor->update_arm = i845_cursor_update_arm;
811 		cursor->disable_arm = i845_cursor_disable_arm;
812 		cursor->get_hw_state = i845_cursor_get_hw_state;
813 		cursor->check_plane = i845_check_cursor;
814 	} else {
815 		cursor->max_stride = i9xx_cursor_max_stride;
816 		cursor->update_arm = i9xx_cursor_update_arm;
817 		cursor->disable_arm = i9xx_cursor_disable_arm;
818 		cursor->get_hw_state = i9xx_cursor_get_hw_state;
819 		cursor->check_plane = i9xx_check_cursor;
820 	}
821 
822 	cursor->cursor.base = ~0;
823 	cursor->cursor.cntl = ~0;
824 
825 	if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
826 		cursor->cursor.size = ~0;
827 
828 	modifiers = intel_fb_plane_get_modifiers(dev_priv, INTEL_PLANE_CAP_NONE);
829 
830 	ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
831 				       0, &intel_cursor_plane_funcs,
832 				       intel_cursor_formats,
833 				       ARRAY_SIZE(intel_cursor_formats),
834 				       modifiers,
835 				       DRM_PLANE_TYPE_CURSOR,
836 				       "cursor %c", pipe_name(pipe));
837 
838 	kfree(modifiers);
839 
840 	if (ret)
841 		goto fail;
842 
843 	if (DISPLAY_VER(dev_priv) >= 4)
844 		drm_plane_create_rotation_property(&cursor->base,
845 						   DRM_MODE_ROTATE_0,
846 						   DRM_MODE_ROTATE_0 |
847 						   DRM_MODE_ROTATE_180);
848 
849 	zpos = DISPLAY_RUNTIME_INFO(dev_priv)->num_sprites[pipe] + 1;
850 	drm_plane_create_zpos_immutable_property(&cursor->base, zpos);
851 
852 	if (DISPLAY_VER(dev_priv) >= 12)
853 		drm_plane_enable_fb_damage_clips(&cursor->base);
854 
855 	intel_plane_helper_add(cursor);
856 
857 	return cursor;
858 
859 fail:
860 	intel_plane_free(cursor);
861 
862 	return ERR_PTR(ret);
863 }
864