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 "skl_watermark.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_NO_SCALING,
147 						DRM_PLANE_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 	/* final plane coordinates will be relative to the plane's pipe */
157 	drm_rect_translate(&plane_state->uapi.dst,
158 			   -crtc_state->pipe_src.x1,
159 			   -crtc_state->pipe_src.y1);
160 
161 	ret = intel_cursor_check_surface(plane_state);
162 	if (ret)
163 		return ret;
164 
165 	if (!plane_state->uapi.visible)
166 		return 0;
167 
168 	ret = intel_plane_check_src_coordinates(plane_state);
169 	if (ret)
170 		return ret;
171 
172 	return 0;
173 }
174 
175 static unsigned int
176 i845_cursor_max_stride(struct intel_plane *plane,
177 		       u32 pixel_format, u64 modifier,
178 		       unsigned int rotation)
179 {
180 	return 2048;
181 }
182 
183 static u32 i845_cursor_ctl_crtc(const struct intel_crtc_state *crtc_state)
184 {
185 	u32 cntl = 0;
186 
187 	if (crtc_state->gamma_enable)
188 		cntl |= CURSOR_PIPE_GAMMA_ENABLE;
189 
190 	return cntl;
191 }
192 
193 static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
194 			   const struct intel_plane_state *plane_state)
195 {
196 	return CURSOR_ENABLE |
197 		CURSOR_FORMAT_ARGB |
198 		CURSOR_STRIDE(plane_state->view.color_plane[0].mapping_stride);
199 }
200 
201 static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
202 {
203 	int width = drm_rect_width(&plane_state->uapi.dst);
204 
205 	/*
206 	 * 845g/865g are only limited by the width of their cursors,
207 	 * the height is arbitrary up to the precision of the register.
208 	 */
209 	return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
210 }
211 
212 static int i845_check_cursor(struct intel_crtc_state *crtc_state,
213 			     struct intel_plane_state *plane_state)
214 {
215 	const struct drm_framebuffer *fb = plane_state->hw.fb;
216 	struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
217 	int ret;
218 
219 	ret = intel_check_cursor(crtc_state, plane_state);
220 	if (ret)
221 		return ret;
222 
223 	/* if we want to turn off the cursor ignore width and height */
224 	if (!fb)
225 		return 0;
226 
227 	/* Check for which cursor types we support */
228 	if (!i845_cursor_size_ok(plane_state)) {
229 		drm_dbg_kms(&i915->drm,
230 			    "Cursor dimension %dx%d not supported\n",
231 			    drm_rect_width(&plane_state->uapi.dst),
232 			    drm_rect_height(&plane_state->uapi.dst));
233 		return -EINVAL;
234 	}
235 
236 	drm_WARN_ON(&i915->drm, plane_state->uapi.visible &&
237 		    plane_state->view.color_plane[0].mapping_stride != fb->pitches[0]);
238 
239 	switch (fb->pitches[0]) {
240 	case 256:
241 	case 512:
242 	case 1024:
243 	case 2048:
244 		break;
245 	default:
246 		 drm_dbg_kms(&i915->drm, "Invalid cursor stride (%u)\n",
247 			     fb->pitches[0]);
248 		return -EINVAL;
249 	}
250 
251 	plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
252 
253 	return 0;
254 }
255 
256 /* TODO: split into noarm+arm pair */
257 static void i845_cursor_update_arm(struct intel_plane *plane,
258 				   const struct intel_crtc_state *crtc_state,
259 				   const struct intel_plane_state *plane_state)
260 {
261 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
262 	u32 cntl = 0, base = 0, pos = 0, size = 0;
263 
264 	if (plane_state && plane_state->uapi.visible) {
265 		unsigned int width = drm_rect_width(&plane_state->uapi.dst);
266 		unsigned int height = drm_rect_height(&plane_state->uapi.dst);
267 
268 		cntl = plane_state->ctl |
269 			i845_cursor_ctl_crtc(crtc_state);
270 
271 		size = CURSOR_HEIGHT(height) | CURSOR_WIDTH(width);
272 
273 		base = intel_cursor_base(plane_state);
274 		pos = intel_cursor_position(plane_state);
275 	}
276 
277 	/* On these chipsets we can only modify the base/size/stride
278 	 * whilst the cursor is disabled.
279 	 */
280 	if (plane->cursor.base != base ||
281 	    plane->cursor.size != size ||
282 	    plane->cursor.cntl != cntl) {
283 		intel_de_write_fw(dev_priv, CURCNTR(PIPE_A), 0);
284 		intel_de_write_fw(dev_priv, CURBASE(PIPE_A), base);
285 		intel_de_write_fw(dev_priv, CURSIZE(PIPE_A), size);
286 		intel_de_write_fw(dev_priv, CURPOS(PIPE_A), pos);
287 		intel_de_write_fw(dev_priv, CURCNTR(PIPE_A), cntl);
288 
289 		plane->cursor.base = base;
290 		plane->cursor.size = size;
291 		plane->cursor.cntl = cntl;
292 	} else {
293 		intel_de_write_fw(dev_priv, CURPOS(PIPE_A), pos);
294 	}
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_PIPE_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_SEL(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 
496 	if (plane_state && plane_state->uapi.visible) {
497 		int width = drm_rect_width(&plane_state->uapi.dst);
498 		int height = drm_rect_height(&plane_state->uapi.dst);
499 
500 		cntl = plane_state->ctl |
501 			i9xx_cursor_ctl_crtc(crtc_state);
502 
503 		if (width != height)
504 			fbc_ctl = CUR_FBC_EN | CUR_FBC_HEIGHT(height - 1);
505 
506 		base = intel_cursor_base(plane_state);
507 		pos = intel_cursor_position(plane_state);
508 	}
509 
510 	/*
511 	 * On some platforms writing CURCNTR first will also
512 	 * cause CURPOS to be armed by the CURBASE write.
513 	 * Without the CURCNTR write the CURPOS write would
514 	 * arm itself. Thus we always update CURCNTR before
515 	 * CURPOS.
516 	 *
517 	 * On other platforms CURPOS always requires the
518 	 * CURBASE write to arm the update. Additonally
519 	 * a write to any of the cursor register will cancel
520 	 * an already armed cursor update. Thus leaving out
521 	 * the CURBASE write after CURPOS could lead to a
522 	 * cursor that doesn't appear to move, or even change
523 	 * shape. Thus we always write CURBASE.
524 	 *
525 	 * The other registers are armed by the CURBASE write
526 	 * except when the plane is getting enabled at which time
527 	 * the CURCNTR write arms the update.
528 	 */
529 
530 	if (DISPLAY_VER(dev_priv) >= 9)
531 		skl_write_cursor_wm(plane, crtc_state);
532 
533 	if (plane_state)
534 		intel_psr2_program_plane_sel_fetch_arm(plane, crtc_state,
535 						       plane_state);
536 	else
537 		intel_psr2_disable_plane_sel_fetch_arm(plane, crtc_state);
538 
539 	if (plane->cursor.base != base ||
540 	    plane->cursor.size != fbc_ctl ||
541 	    plane->cursor.cntl != cntl) {
542 		if (HAS_CUR_FBC(dev_priv))
543 			intel_de_write_fw(dev_priv, CUR_FBC_CTL(pipe),
544 					  fbc_ctl);
545 		intel_de_write_fw(dev_priv, CURCNTR(pipe), cntl);
546 		intel_de_write_fw(dev_priv, CURPOS(pipe), pos);
547 		intel_de_write_fw(dev_priv, CURBASE(pipe), base);
548 
549 		plane->cursor.base = base;
550 		plane->cursor.size = fbc_ctl;
551 		plane->cursor.cntl = cntl;
552 	} else {
553 		intel_de_write_fw(dev_priv, CURPOS(pipe), pos);
554 		intel_de_write_fw(dev_priv, CURBASE(pipe), base);
555 	}
556 }
557 
558 static void i9xx_cursor_disable_arm(struct intel_plane *plane,
559 				    const struct intel_crtc_state *crtc_state)
560 {
561 	i9xx_cursor_update_arm(plane, crtc_state, NULL);
562 }
563 
564 static bool i9xx_cursor_get_hw_state(struct intel_plane *plane,
565 				     enum pipe *pipe)
566 {
567 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
568 	enum intel_display_power_domain power_domain;
569 	intel_wakeref_t wakeref;
570 	bool ret;
571 	u32 val;
572 
573 	/*
574 	 * Not 100% correct for planes that can move between pipes,
575 	 * but that's only the case for gen2-3 which don't have any
576 	 * display power wells.
577 	 */
578 	power_domain = POWER_DOMAIN_PIPE(plane->pipe);
579 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
580 	if (!wakeref)
581 		return false;
582 
583 	val = intel_de_read(dev_priv, CURCNTR(plane->pipe));
584 
585 	ret = val & MCURSOR_MODE_MASK;
586 
587 	if (DISPLAY_VER(dev_priv) >= 5 || IS_G4X(dev_priv))
588 		*pipe = plane->pipe;
589 	else
590 		*pipe = REG_FIELD_GET(MCURSOR_PIPE_SEL_MASK, val);
591 
592 	intel_display_power_put(dev_priv, power_domain, wakeref);
593 
594 	return ret;
595 }
596 
597 static bool intel_cursor_format_mod_supported(struct drm_plane *_plane,
598 					      u32 format, u64 modifier)
599 {
600 	if (!intel_fb_plane_supports_modifier(to_intel_plane(_plane), modifier))
601 		return false;
602 
603 	return format == DRM_FORMAT_ARGB8888;
604 }
605 
606 static int
607 intel_legacy_cursor_update(struct drm_plane *_plane,
608 			   struct drm_crtc *_crtc,
609 			   struct drm_framebuffer *fb,
610 			   int crtc_x, int crtc_y,
611 			   unsigned int crtc_w, unsigned int crtc_h,
612 			   u32 src_x, u32 src_y,
613 			   u32 src_w, u32 src_h,
614 			   struct drm_modeset_acquire_ctx *ctx)
615 {
616 	struct intel_plane *plane = to_intel_plane(_plane);
617 	struct intel_crtc *crtc = to_intel_crtc(_crtc);
618 	struct intel_plane_state *old_plane_state =
619 		to_intel_plane_state(plane->base.state);
620 	struct intel_plane_state *new_plane_state;
621 	struct intel_crtc_state *crtc_state =
622 		to_intel_crtc_state(crtc->base.state);
623 	struct intel_crtc_state *new_crtc_state;
624 	int ret;
625 
626 	/*
627 	 * When crtc is inactive or there is a modeset pending,
628 	 * wait for it to complete in the slowpath.
629 	 * PSR2 selective fetch also requires the slow path as
630 	 * PSR2 plane and transcoder registers can only be updated during
631 	 * vblank.
632 	 *
633 	 * FIXME bigjoiner fastpath would be good
634 	 */
635 	if (!crtc_state->hw.active ||
636 	    intel_crtc_needs_modeset(crtc_state) ||
637 	    intel_crtc_needs_fastset(crtc_state) ||
638 	    crtc_state->bigjoiner_pipes)
639 		goto slow;
640 
641 	/*
642 	 * Don't do an async update if there is an outstanding commit modifying
643 	 * the plane.  This prevents our async update's changes from getting
644 	 * overridden by a previous synchronous update's state.
645 	 */
646 	if (old_plane_state->uapi.commit &&
647 	    !try_wait_for_completion(&old_plane_state->uapi.commit->hw_done))
648 		goto slow;
649 
650 	/*
651 	 * If any parameters change that may affect watermarks,
652 	 * take the slowpath. Only changing fb or position should be
653 	 * in the fastpath.
654 	 */
655 	if (old_plane_state->uapi.crtc != &crtc->base ||
656 	    old_plane_state->uapi.src_w != src_w ||
657 	    old_plane_state->uapi.src_h != src_h ||
658 	    old_plane_state->uapi.crtc_w != crtc_w ||
659 	    old_plane_state->uapi.crtc_h != crtc_h ||
660 	    !old_plane_state->uapi.fb != !fb)
661 		goto slow;
662 
663 	new_plane_state = to_intel_plane_state(intel_plane_duplicate_state(&plane->base));
664 	if (!new_plane_state)
665 		return -ENOMEM;
666 
667 	new_crtc_state = to_intel_crtc_state(intel_crtc_duplicate_state(&crtc->base));
668 	if (!new_crtc_state) {
669 		ret = -ENOMEM;
670 		goto out_free;
671 	}
672 
673 	drm_atomic_set_fb_for_plane(&new_plane_state->uapi, fb);
674 
675 	new_plane_state->uapi.src_x = src_x;
676 	new_plane_state->uapi.src_y = src_y;
677 	new_plane_state->uapi.src_w = src_w;
678 	new_plane_state->uapi.src_h = src_h;
679 	new_plane_state->uapi.crtc_x = crtc_x;
680 	new_plane_state->uapi.crtc_y = crtc_y;
681 	new_plane_state->uapi.crtc_w = crtc_w;
682 	new_plane_state->uapi.crtc_h = crtc_h;
683 
684 	intel_plane_copy_uapi_to_hw_state(new_plane_state, new_plane_state, crtc);
685 
686 	ret = intel_plane_atomic_check_with_state(crtc_state, new_crtc_state,
687 						  old_plane_state, new_plane_state);
688 	if (ret)
689 		goto out_free;
690 
691 	ret = intel_plane_pin_fb(new_plane_state);
692 	if (ret)
693 		goto out_free;
694 
695 	intel_frontbuffer_flush(to_intel_frontbuffer(new_plane_state->hw.fb),
696 				ORIGIN_CURSOR_UPDATE);
697 	intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->hw.fb),
698 				to_intel_frontbuffer(new_plane_state->hw.fb),
699 				plane->frontbuffer_bit);
700 
701 	/* Swap plane state */
702 	plane->base.state = &new_plane_state->uapi;
703 
704 	/*
705 	 * We cannot swap crtc_state as it may be in use by an atomic commit or
706 	 * page flip that's running simultaneously. If we swap crtc_state and
707 	 * destroy the old state, we will cause a use-after-free there.
708 	 *
709 	 * Only update active_planes, which is needed for our internal
710 	 * bookkeeping. Either value will do the right thing when updating
711 	 * planes atomically. If the cursor was part of the atomic update then
712 	 * we would have taken the slowpath.
713 	 */
714 	crtc_state->active_planes = new_crtc_state->active_planes;
715 
716 	/*
717 	 * Technically we should do a vblank evasion here to make
718 	 * sure all the cursor registers update on the same frame.
719 	 * For now just make sure the register writes happen as
720 	 * quickly as possible to minimize the race window.
721 	 */
722 	local_irq_disable();
723 
724 	if (new_plane_state->uapi.visible) {
725 		intel_plane_update_noarm(plane, crtc_state, new_plane_state);
726 		intel_plane_update_arm(plane, crtc_state, new_plane_state);
727 	} else {
728 		intel_plane_disable_arm(plane, crtc_state);
729 	}
730 
731 	local_irq_enable();
732 
733 	intel_plane_unpin_fb(old_plane_state);
734 
735 out_free:
736 	if (new_crtc_state)
737 		intel_crtc_destroy_state(&crtc->base, &new_crtc_state->uapi);
738 	if (ret)
739 		intel_plane_destroy_state(&plane->base, &new_plane_state->uapi);
740 	else
741 		intel_plane_destroy_state(&plane->base, &old_plane_state->uapi);
742 	return ret;
743 
744 slow:
745 	return drm_atomic_helper_update_plane(&plane->base, &crtc->base, fb,
746 					      crtc_x, crtc_y, crtc_w, crtc_h,
747 					      src_x, src_y, src_w, src_h, ctx);
748 }
749 
750 static const struct drm_plane_funcs intel_cursor_plane_funcs = {
751 	.update_plane = intel_legacy_cursor_update,
752 	.disable_plane = drm_atomic_helper_disable_plane,
753 	.destroy = intel_plane_destroy,
754 	.atomic_duplicate_state = intel_plane_duplicate_state,
755 	.atomic_destroy_state = intel_plane_destroy_state,
756 	.format_mod_supported = intel_cursor_format_mod_supported,
757 };
758 
759 struct intel_plane *
760 intel_cursor_plane_create(struct drm_i915_private *dev_priv,
761 			  enum pipe pipe)
762 {
763 	struct intel_plane *cursor;
764 	int ret, zpos;
765 	u64 *modifiers;
766 
767 	cursor = intel_plane_alloc();
768 	if (IS_ERR(cursor))
769 		return cursor;
770 
771 	cursor->pipe = pipe;
772 	cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
773 	cursor->id = PLANE_CURSOR;
774 	cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id);
775 
776 	if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
777 		cursor->max_stride = i845_cursor_max_stride;
778 		cursor->update_arm = i845_cursor_update_arm;
779 		cursor->disable_arm = i845_cursor_disable_arm;
780 		cursor->get_hw_state = i845_cursor_get_hw_state;
781 		cursor->check_plane = i845_check_cursor;
782 	} else {
783 		cursor->max_stride = i9xx_cursor_max_stride;
784 		cursor->update_arm = i9xx_cursor_update_arm;
785 		cursor->disable_arm = i9xx_cursor_disable_arm;
786 		cursor->get_hw_state = i9xx_cursor_get_hw_state;
787 		cursor->check_plane = i9xx_check_cursor;
788 	}
789 
790 	cursor->cursor.base = ~0;
791 	cursor->cursor.cntl = ~0;
792 
793 	if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
794 		cursor->cursor.size = ~0;
795 
796 	modifiers = intel_fb_plane_get_modifiers(dev_priv, INTEL_PLANE_CAP_NONE);
797 
798 	ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
799 				       0, &intel_cursor_plane_funcs,
800 				       intel_cursor_formats,
801 				       ARRAY_SIZE(intel_cursor_formats),
802 				       modifiers,
803 				       DRM_PLANE_TYPE_CURSOR,
804 				       "cursor %c", pipe_name(pipe));
805 
806 	kfree(modifiers);
807 
808 	if (ret)
809 		goto fail;
810 
811 	if (DISPLAY_VER(dev_priv) >= 4)
812 		drm_plane_create_rotation_property(&cursor->base,
813 						   DRM_MODE_ROTATE_0,
814 						   DRM_MODE_ROTATE_0 |
815 						   DRM_MODE_ROTATE_180);
816 
817 	zpos = RUNTIME_INFO(dev_priv)->num_sprites[pipe] + 1;
818 	drm_plane_create_zpos_immutable_property(&cursor->base, zpos);
819 
820 	if (DISPLAY_VER(dev_priv) >= 12)
821 		drm_plane_enable_fb_damage_clips(&cursor->base);
822 
823 	intel_plane_helper_add(cursor);
824 
825 	return cursor;
826 
827 fail:
828 	intel_plane_free(cursor);
829 
830 	return ERR_PTR(ret);
831 }
832