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