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_SIGN << CURSOR_X_SHIFT;
55 		x = -x;
56 	}
57 	pos |= x << CURSOR_X_SHIFT;
58 
59 	if (y < 0) {
60 		pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
61 		y = -y;
62 	}
63 	pos |= y << CURSOR_Y_SHIFT;
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_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 	unsigned long irqflags;
259 
260 	if (plane_state && plane_state->uapi.visible) {
261 		unsigned int width = drm_rect_width(&plane_state->uapi.dst);
262 		unsigned int height = drm_rect_height(&plane_state->uapi.dst);
263 
264 		cntl = plane_state->ctl |
265 			i845_cursor_ctl_crtc(crtc_state);
266 
267 		size = (height << 12) | width;
268 
269 		base = intel_cursor_base(plane_state);
270 		pos = intel_cursor_position(plane_state);
271 	}
272 
273 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
274 
275 	/* On these chipsets we can only modify the base/size/stride
276 	 * whilst the cursor is disabled.
277 	 */
278 	if (plane->cursor.base != base ||
279 	    plane->cursor.size != size ||
280 	    plane->cursor.cntl != cntl) {
281 		intel_de_write_fw(dev_priv, CURCNTR(PIPE_A), 0);
282 		intel_de_write_fw(dev_priv, CURBASE(PIPE_A), base);
283 		intel_de_write_fw(dev_priv, CURSIZE, size);
284 		intel_de_write_fw(dev_priv, CURPOS(PIPE_A), pos);
285 		intel_de_write_fw(dev_priv, CURCNTR(PIPE_A), cntl);
286 
287 		plane->cursor.base = base;
288 		plane->cursor.size = size;
289 		plane->cursor.cntl = cntl;
290 	} else {
291 		intel_de_write_fw(dev_priv, CURPOS(PIPE_A), pos);
292 	}
293 
294 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
295 }
296 
297 static void i845_cursor_disable_arm(struct intel_plane *plane,
298 				    const struct intel_crtc_state *crtc_state)
299 {
300 	i845_cursor_update_arm(plane, crtc_state, NULL);
301 }
302 
303 static bool i845_cursor_get_hw_state(struct intel_plane *plane,
304 				     enum pipe *pipe)
305 {
306 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
307 	enum intel_display_power_domain power_domain;
308 	intel_wakeref_t wakeref;
309 	bool ret;
310 
311 	power_domain = POWER_DOMAIN_PIPE(PIPE_A);
312 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
313 	if (!wakeref)
314 		return false;
315 
316 	ret = intel_de_read(dev_priv, CURCNTR(PIPE_A)) & CURSOR_ENABLE;
317 
318 	*pipe = PIPE_A;
319 
320 	intel_display_power_put(dev_priv, power_domain, wakeref);
321 
322 	return ret;
323 }
324 
325 static unsigned int
326 i9xx_cursor_max_stride(struct intel_plane *plane,
327 		       u32 pixel_format, u64 modifier,
328 		       unsigned int rotation)
329 {
330 	return plane->base.dev->mode_config.cursor_width * 4;
331 }
332 
333 static u32 i9xx_cursor_ctl_crtc(const struct intel_crtc_state *crtc_state)
334 {
335 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
336 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
337 	u32 cntl = 0;
338 
339 	if (DISPLAY_VER(dev_priv) >= 11)
340 		return cntl;
341 
342 	if (crtc_state->gamma_enable)
343 		cntl = MCURSOR_GAMMA_ENABLE;
344 
345 	if (crtc_state->csc_enable)
346 		cntl |= MCURSOR_PIPE_CSC_ENABLE;
347 
348 	if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv))
349 		cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
350 
351 	return cntl;
352 }
353 
354 static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
355 			   const struct intel_plane_state *plane_state)
356 {
357 	struct drm_i915_private *dev_priv =
358 		to_i915(plane_state->uapi.plane->dev);
359 	u32 cntl = 0;
360 
361 	if (IS_SANDYBRIDGE(dev_priv) || IS_IVYBRIDGE(dev_priv))
362 		cntl |= MCURSOR_TRICKLE_FEED_DISABLE;
363 
364 	switch (drm_rect_width(&plane_state->uapi.dst)) {
365 	case 64:
366 		cntl |= MCURSOR_MODE_64_ARGB_AX;
367 		break;
368 	case 128:
369 		cntl |= MCURSOR_MODE_128_ARGB_AX;
370 		break;
371 	case 256:
372 		cntl |= MCURSOR_MODE_256_ARGB_AX;
373 		break;
374 	default:
375 		MISSING_CASE(drm_rect_width(&plane_state->uapi.dst));
376 		return 0;
377 	}
378 
379 	if (plane_state->hw.rotation & DRM_MODE_ROTATE_180)
380 		cntl |= MCURSOR_ROTATE_180;
381 
382 	/* Wa_22012358565:adl-p */
383 	if (DISPLAY_VER(dev_priv) == 13)
384 		cntl |= MCURSOR_ARB_SLOTS(1);
385 
386 	return cntl;
387 }
388 
389 static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
390 {
391 	struct drm_i915_private *dev_priv =
392 		to_i915(plane_state->uapi.plane->dev);
393 	int width = drm_rect_width(&plane_state->uapi.dst);
394 	int height = drm_rect_height(&plane_state->uapi.dst);
395 
396 	if (!intel_cursor_size_ok(plane_state))
397 		return false;
398 
399 	/* Cursor width is limited to a few power-of-two sizes */
400 	switch (width) {
401 	case 256:
402 	case 128:
403 	case 64:
404 		break;
405 	default:
406 		return false;
407 	}
408 
409 	/*
410 	 * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
411 	 * height from 8 lines up to the cursor width, when the
412 	 * cursor is not rotated. Everything else requires square
413 	 * cursors.
414 	 */
415 	if (HAS_CUR_FBC(dev_priv) &&
416 	    plane_state->hw.rotation & DRM_MODE_ROTATE_0) {
417 		if (height < 8 || height > width)
418 			return false;
419 	} else {
420 		if (height != width)
421 			return false;
422 	}
423 
424 	return true;
425 }
426 
427 static int i9xx_check_cursor(struct intel_crtc_state *crtc_state,
428 			     struct intel_plane_state *plane_state)
429 {
430 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
431 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
432 	const struct drm_framebuffer *fb = plane_state->hw.fb;
433 	enum pipe pipe = plane->pipe;
434 	int ret;
435 
436 	ret = intel_check_cursor(crtc_state, plane_state);
437 	if (ret)
438 		return ret;
439 
440 	/* if we want to turn off the cursor ignore width and height */
441 	if (!fb)
442 		return 0;
443 
444 	/* Check for which cursor types we support */
445 	if (!i9xx_cursor_size_ok(plane_state)) {
446 		drm_dbg(&dev_priv->drm,
447 			"Cursor dimension %dx%d not supported\n",
448 			drm_rect_width(&plane_state->uapi.dst),
449 			drm_rect_height(&plane_state->uapi.dst));
450 		return -EINVAL;
451 	}
452 
453 	drm_WARN_ON(&dev_priv->drm, plane_state->uapi.visible &&
454 		    plane_state->view.color_plane[0].mapping_stride != fb->pitches[0]);
455 
456 	if (fb->pitches[0] !=
457 	    drm_rect_width(&plane_state->uapi.dst) * fb->format->cpp[0]) {
458 		drm_dbg_kms(&dev_priv->drm,
459 			    "Invalid cursor stride (%u) (cursor width %d)\n",
460 			    fb->pitches[0],
461 			    drm_rect_width(&plane_state->uapi.dst));
462 		return -EINVAL;
463 	}
464 
465 	/*
466 	 * There's something wrong with the cursor on CHV pipe C.
467 	 * If it straddles the left edge of the screen then
468 	 * moving it away from the edge or disabling it often
469 	 * results in a pipe underrun, and often that can lead to
470 	 * dead pipe (constant underrun reported, and it scans
471 	 * out just a solid color). To recover from that, the
472 	 * display power well must be turned off and on again.
473 	 * Refuse the put the cursor into that compromised position.
474 	 */
475 	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
476 	    plane_state->uapi.visible && plane_state->uapi.dst.x1 < 0) {
477 		drm_dbg_kms(&dev_priv->drm,
478 			    "CHV cursor C not allowed to straddle the left screen edge\n");
479 		return -EINVAL;
480 	}
481 
482 	plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
483 
484 	return 0;
485 }
486 
487 /* TODO: split into noarm+arm pair */
488 static void i9xx_cursor_update_arm(struct intel_plane *plane,
489 				   const struct intel_crtc_state *crtc_state,
490 				   const struct intel_plane_state *plane_state)
491 {
492 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
493 	enum pipe pipe = plane->pipe;
494 	u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
495 	unsigned long irqflags;
496 
497 	if (plane_state && plane_state->uapi.visible) {
498 		int width = drm_rect_width(&plane_state->uapi.dst);
499 		int height = drm_rect_height(&plane_state->uapi.dst);
500 
501 		cntl = plane_state->ctl |
502 			i9xx_cursor_ctl_crtc(crtc_state);
503 
504 		if (width != height)
505 			fbc_ctl = CUR_FBC_CTL_EN | (height - 1);
506 
507 		base = intel_cursor_base(plane_state);
508 		pos = intel_cursor_position(plane_state);
509 	}
510 
511 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
512 
513 	/*
514 	 * On some platforms writing CURCNTR first will also
515 	 * cause CURPOS to be armed by the CURBASE write.
516 	 * Without the CURCNTR write the CURPOS write would
517 	 * arm itself. Thus we always update CURCNTR before
518 	 * CURPOS.
519 	 *
520 	 * On other platforms CURPOS always requires the
521 	 * CURBASE write to arm the update. Additonally
522 	 * a write to any of the cursor register will cancel
523 	 * an already armed cursor update. Thus leaving out
524 	 * the CURBASE write after CURPOS could lead to a
525 	 * cursor that doesn't appear to move, or even change
526 	 * shape. Thus we always write CURBASE.
527 	 *
528 	 * The other registers are armed by the CURBASE write
529 	 * except when the plane is getting enabled at which time
530 	 * the CURCNTR write arms the update.
531 	 */
532 
533 	if (DISPLAY_VER(dev_priv) >= 9)
534 		skl_write_cursor_wm(plane, crtc_state);
535 
536 	if (plane_state)
537 		intel_psr2_program_plane_sel_fetch(plane, crtc_state, plane_state, 0);
538 	else
539 		intel_psr2_disable_plane_sel_fetch(plane, crtc_state);
540 
541 	if (plane->cursor.base != base ||
542 	    plane->cursor.size != fbc_ctl ||
543 	    plane->cursor.cntl != cntl) {
544 		if (HAS_CUR_FBC(dev_priv))
545 			intel_de_write_fw(dev_priv, CUR_FBC_CTL(pipe),
546 					  fbc_ctl);
547 		intel_de_write_fw(dev_priv, CURCNTR(pipe), cntl);
548 		intel_de_write_fw(dev_priv, CURPOS(pipe), pos);
549 		intel_de_write_fw(dev_priv, CURBASE(pipe), base);
550 
551 		plane->cursor.base = base;
552 		plane->cursor.size = fbc_ctl;
553 		plane->cursor.cntl = cntl;
554 	} else {
555 		intel_de_write_fw(dev_priv, CURPOS(pipe), pos);
556 		intel_de_write_fw(dev_priv, CURBASE(pipe), base);
557 	}
558 
559 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
560 }
561 
562 static void i9xx_cursor_disable_arm(struct intel_plane *plane,
563 				    const struct intel_crtc_state *crtc_state)
564 {
565 	i9xx_cursor_update_arm(plane, crtc_state, NULL);
566 }
567 
568 static bool i9xx_cursor_get_hw_state(struct intel_plane *plane,
569 				     enum pipe *pipe)
570 {
571 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
572 	enum intel_display_power_domain power_domain;
573 	intel_wakeref_t wakeref;
574 	bool ret;
575 	u32 val;
576 
577 	/*
578 	 * Not 100% correct for planes that can move between pipes,
579 	 * but that's only the case for gen2-3 which don't have any
580 	 * display power wells.
581 	 */
582 	power_domain = POWER_DOMAIN_PIPE(plane->pipe);
583 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
584 	if (!wakeref)
585 		return false;
586 
587 	val = intel_de_read(dev_priv, CURCNTR(plane->pipe));
588 
589 	ret = val & MCURSOR_MODE;
590 
591 	if (DISPLAY_VER(dev_priv) >= 5 || IS_G4X(dev_priv))
592 		*pipe = plane->pipe;
593 	else
594 		*pipe = (val & MCURSOR_PIPE_SELECT_MASK) >>
595 			MCURSOR_PIPE_SELECT_SHIFT;
596 
597 	intel_display_power_put(dev_priv, power_domain, wakeref);
598 
599 	return ret;
600 }
601 
602 static bool intel_cursor_format_mod_supported(struct drm_plane *_plane,
603 					      u32 format, u64 modifier)
604 {
605 	if (!intel_fb_plane_supports_modifier(to_intel_plane(_plane), modifier))
606 		return false;
607 
608 	return format == DRM_FORMAT_ARGB8888;
609 }
610 
611 static int
612 intel_legacy_cursor_update(struct drm_plane *_plane,
613 			   struct drm_crtc *_crtc,
614 			   struct drm_framebuffer *fb,
615 			   int crtc_x, int crtc_y,
616 			   unsigned int crtc_w, unsigned int crtc_h,
617 			   u32 src_x, u32 src_y,
618 			   u32 src_w, u32 src_h,
619 			   struct drm_modeset_acquire_ctx *ctx)
620 {
621 	struct intel_plane *plane = to_intel_plane(_plane);
622 	struct intel_crtc *crtc = to_intel_crtc(_crtc);
623 	struct intel_plane_state *old_plane_state =
624 		to_intel_plane_state(plane->base.state);
625 	struct intel_plane_state *new_plane_state;
626 	struct intel_crtc_state *crtc_state =
627 		to_intel_crtc_state(crtc->base.state);
628 	struct intel_crtc_state *new_crtc_state;
629 	int ret;
630 
631 	/*
632 	 * When crtc is inactive or there is a modeset pending,
633 	 * wait for it to complete in the slowpath.
634 	 * PSR2 selective fetch also requires the slow path as
635 	 * PSR2 plane and transcoder registers can only be updated during
636 	 * vblank.
637 	 *
638 	 * FIXME bigjoiner fastpath would be good
639 	 */
640 	if (!crtc_state->hw.active || intel_crtc_needs_modeset(crtc_state) ||
641 	    crtc_state->update_pipe || crtc_state->bigjoiner)
642 		goto slow;
643 
644 	/*
645 	 * Don't do an async update if there is an outstanding commit modifying
646 	 * the plane.  This prevents our async update's changes from getting
647 	 * overridden by a previous synchronous update's state.
648 	 */
649 	if (old_plane_state->uapi.commit &&
650 	    !try_wait_for_completion(&old_plane_state->uapi.commit->hw_done))
651 		goto slow;
652 
653 	/*
654 	 * If any parameters change that may affect watermarks,
655 	 * take the slowpath. Only changing fb or position should be
656 	 * in the fastpath.
657 	 */
658 	if (old_plane_state->uapi.crtc != &crtc->base ||
659 	    old_plane_state->uapi.src_w != src_w ||
660 	    old_plane_state->uapi.src_h != src_h ||
661 	    old_plane_state->uapi.crtc_w != crtc_w ||
662 	    old_plane_state->uapi.crtc_h != crtc_h ||
663 	    !old_plane_state->uapi.fb != !fb)
664 		goto slow;
665 
666 	new_plane_state = to_intel_plane_state(intel_plane_duplicate_state(&plane->base));
667 	if (!new_plane_state)
668 		return -ENOMEM;
669 
670 	new_crtc_state = to_intel_crtc_state(intel_crtc_duplicate_state(&crtc->base));
671 	if (!new_crtc_state) {
672 		ret = -ENOMEM;
673 		goto out_free;
674 	}
675 
676 	drm_atomic_set_fb_for_plane(&new_plane_state->uapi, fb);
677 
678 	new_plane_state->uapi.src_x = src_x;
679 	new_plane_state->uapi.src_y = src_y;
680 	new_plane_state->uapi.src_w = src_w;
681 	new_plane_state->uapi.src_h = src_h;
682 	new_plane_state->uapi.crtc_x = crtc_x;
683 	new_plane_state->uapi.crtc_y = crtc_y;
684 	new_plane_state->uapi.crtc_w = crtc_w;
685 	new_plane_state->uapi.crtc_h = crtc_h;
686 
687 	intel_plane_copy_uapi_to_hw_state(new_plane_state, new_plane_state, crtc);
688 
689 	ret = intel_plane_atomic_check_with_state(crtc_state, new_crtc_state,
690 						  old_plane_state, new_plane_state);
691 	if (ret)
692 		goto out_free;
693 
694 	ret = intel_plane_pin_fb(new_plane_state);
695 	if (ret)
696 		goto out_free;
697 
698 	intel_frontbuffer_flush(to_intel_frontbuffer(new_plane_state->hw.fb),
699 				ORIGIN_CURSOR_UPDATE);
700 	intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->hw.fb),
701 				to_intel_frontbuffer(new_plane_state->hw.fb),
702 				plane->frontbuffer_bit);
703 
704 	/* Swap plane state */
705 	plane->base.state = &new_plane_state->uapi;
706 
707 	/*
708 	 * We cannot swap crtc_state as it may be in use by an atomic commit or
709 	 * page flip that's running simultaneously. If we swap crtc_state and
710 	 * destroy the old state, we will cause a use-after-free there.
711 	 *
712 	 * Only update active_planes, which is needed for our internal
713 	 * bookkeeping. Either value will do the right thing when updating
714 	 * planes atomically. If the cursor was part of the atomic update then
715 	 * we would have taken the slowpath.
716 	 */
717 	crtc_state->active_planes = new_crtc_state->active_planes;
718 
719 	if (new_plane_state->uapi.visible) {
720 		intel_plane_update_noarm(plane, crtc_state, new_plane_state);
721 		intel_plane_update_arm(plane, crtc_state, new_plane_state);
722 	} else {
723 		intel_plane_disable_arm(plane, crtc_state);
724 	}
725 
726 	intel_plane_unpin_fb(old_plane_state);
727 
728 out_free:
729 	if (new_crtc_state)
730 		intel_crtc_destroy_state(&crtc->base, &new_crtc_state->uapi);
731 	if (ret)
732 		intel_plane_destroy_state(&plane->base, &new_plane_state->uapi);
733 	else
734 		intel_plane_destroy_state(&plane->base, &old_plane_state->uapi);
735 	return ret;
736 
737 slow:
738 	return drm_atomic_helper_update_plane(&plane->base, &crtc->base, fb,
739 					      crtc_x, crtc_y, crtc_w, crtc_h,
740 					      src_x, src_y, src_w, src_h, ctx);
741 }
742 
743 static const struct drm_plane_funcs intel_cursor_plane_funcs = {
744 	.update_plane = intel_legacy_cursor_update,
745 	.disable_plane = drm_atomic_helper_disable_plane,
746 	.destroy = intel_plane_destroy,
747 	.atomic_duplicate_state = intel_plane_duplicate_state,
748 	.atomic_destroy_state = intel_plane_destroy_state,
749 	.format_mod_supported = intel_cursor_format_mod_supported,
750 };
751 
752 struct intel_plane *
753 intel_cursor_plane_create(struct drm_i915_private *dev_priv,
754 			  enum pipe pipe)
755 {
756 	struct intel_plane *cursor;
757 	int ret, zpos;
758 	u64 *modifiers;
759 
760 	cursor = intel_plane_alloc();
761 	if (IS_ERR(cursor))
762 		return cursor;
763 
764 	cursor->pipe = pipe;
765 	cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
766 	cursor->id = PLANE_CURSOR;
767 	cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id);
768 
769 	if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
770 		cursor->max_stride = i845_cursor_max_stride;
771 		cursor->update_arm = i845_cursor_update_arm;
772 		cursor->disable_arm = i845_cursor_disable_arm;
773 		cursor->get_hw_state = i845_cursor_get_hw_state;
774 		cursor->check_plane = i845_check_cursor;
775 	} else {
776 		cursor->max_stride = i9xx_cursor_max_stride;
777 		cursor->update_arm = i9xx_cursor_update_arm;
778 		cursor->disable_arm = i9xx_cursor_disable_arm;
779 		cursor->get_hw_state = i9xx_cursor_get_hw_state;
780 		cursor->check_plane = i9xx_check_cursor;
781 	}
782 
783 	cursor->cursor.base = ~0;
784 	cursor->cursor.cntl = ~0;
785 
786 	if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
787 		cursor->cursor.size = ~0;
788 
789 	modifiers = intel_fb_plane_get_modifiers(dev_priv, INTEL_PLANE_CAP_NONE);
790 
791 	ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
792 				       0, &intel_cursor_plane_funcs,
793 				       intel_cursor_formats,
794 				       ARRAY_SIZE(intel_cursor_formats),
795 				       modifiers,
796 				       DRM_PLANE_TYPE_CURSOR,
797 				       "cursor %c", pipe_name(pipe));
798 
799 	kfree(modifiers);
800 
801 	if (ret)
802 		goto fail;
803 
804 	if (DISPLAY_VER(dev_priv) >= 4)
805 		drm_plane_create_rotation_property(&cursor->base,
806 						   DRM_MODE_ROTATE_0,
807 						   DRM_MODE_ROTATE_0 |
808 						   DRM_MODE_ROTATE_180);
809 
810 	zpos = RUNTIME_INFO(dev_priv)->num_sprites[pipe] + 1;
811 	drm_plane_create_zpos_immutable_property(&cursor->base, zpos);
812 
813 	if (DISPLAY_VER(dev_priv) >= 12)
814 		drm_plane_enable_fb_damage_clips(&cursor->base);
815 
816 	intel_plane_helper_add(cursor);
817 
818 	return cursor;
819 
820 fail:
821 	intel_plane_free(cursor);
822 
823 	return ERR_PTR(ret);
824 }
825