xref: /linux/drivers/gpu/drm/i915/display/intel_cursor.c (revision e78f70bad29c5ae1e1076698b690b15794e9b81e)
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 #include <drm/drm_print.h>
13 #include <drm/drm_vblank.h>
14 
15 #include "i915_reg.h"
16 #include "i915_utils.h"
17 #include "intel_atomic.h"
18 #include "intel_atomic_plane.h"
19 #include "intel_cursor.h"
20 #include "intel_cursor_regs.h"
21 #include "intel_de.h"
22 #include "intel_display.h"
23 #include "intel_display_types.h"
24 #include "intel_fb.h"
25 #include "intel_fb_pin.h"
26 #include "intel_frontbuffer.h"
27 #include "intel_psr.h"
28 #include "intel_psr_regs.h"
29 #include "intel_vblank.h"
30 #include "skl_watermark.h"
31 
32 /* Cursor formats */
33 static const u32 intel_cursor_formats[] = {
34 	DRM_FORMAT_ARGB8888,
35 };
36 
37 static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
38 {
39 	struct intel_display *display = to_intel_display(plane_state);
40 	u32 base;
41 
42 	if (DISPLAY_INFO(display)->cursor_needs_physical)
43 		base = plane_state->phys_dma_addr;
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_crtc_state *crtc_state,
51 				 const struct intel_plane_state *plane_state,
52 				 bool early_tpt)
53 {
54 	int x = plane_state->uapi.dst.x1;
55 	int y = plane_state->uapi.dst.y1;
56 	u32 pos = 0;
57 
58 	/*
59 	 * Formula from Bspec:
60 	 * MAX(-1 * <Cursor vertical size from CUR_CTL base on cursor mode
61 	 * select setting> + 1, CUR_POS Y Position - Update region Y position
62 	 */
63 	if (early_tpt)
64 		y = max(-1 * drm_rect_height(&plane_state->uapi.dst) + 1,
65 			y - crtc_state->psr2_su_area.y1);
66 
67 	if (x < 0) {
68 		pos |= CURSOR_POS_X_SIGN;
69 		x = -x;
70 	}
71 	pos |= CURSOR_POS_X(x);
72 
73 	if (y < 0) {
74 		pos |= CURSOR_POS_Y_SIGN;
75 		y = -y;
76 	}
77 	pos |= CURSOR_POS_Y(y);
78 
79 	return pos;
80 }
81 
82 static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
83 {
84 	const struct drm_mode_config *config =
85 		&plane_state->uapi.plane->dev->mode_config;
86 	int width = drm_rect_width(&plane_state->uapi.dst);
87 	int height = drm_rect_height(&plane_state->uapi.dst);
88 
89 	return width > 0 && width <= config->cursor_width &&
90 		height > 0 && height <= config->cursor_height;
91 }
92 
93 static int intel_cursor_check_surface(struct intel_plane_state *plane_state)
94 {
95 	struct intel_display *display = to_intel_display(plane_state);
96 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
97 	unsigned int rotation = plane_state->hw.rotation;
98 	int src_x, src_y;
99 	u32 offset;
100 	int ret;
101 
102 	ret = intel_plane_compute_gtt(plane_state);
103 	if (ret)
104 		return ret;
105 
106 	if (!plane_state->uapi.visible)
107 		return 0;
108 
109 	src_x = plane_state->uapi.src.x1 >> 16;
110 	src_y = plane_state->uapi.src.y1 >> 16;
111 
112 	intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
113 	offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
114 						    plane_state, 0);
115 
116 	if (src_x != 0 || src_y != 0) {
117 		drm_dbg_kms(display->drm,
118 			    "[PLANE:%d:%s] arbitrary cursor panning not supported\n",
119 			    plane->base.base.id, plane->base.name);
120 		return -EINVAL;
121 	}
122 
123 	/*
124 	 * Put the final coordinates back so that the src
125 	 * coordinate checks will see the right values.
126 	 */
127 	drm_rect_translate_to(&plane_state->uapi.src,
128 			      src_x << 16, src_y << 16);
129 
130 	/* ILK+ do this automagically in hardware */
131 	if (HAS_GMCH(display) && rotation & DRM_MODE_ROTATE_180) {
132 		const struct drm_framebuffer *fb = plane_state->hw.fb;
133 		int src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
134 		int src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
135 
136 		offset += (src_h * src_w - 1) * fb->format->cpp[0];
137 	}
138 
139 	plane_state->view.color_plane[0].offset = offset;
140 	plane_state->view.color_plane[0].x = src_x;
141 	plane_state->view.color_plane[0].y = src_y;
142 
143 	return 0;
144 }
145 
146 static int intel_check_cursor(struct intel_crtc_state *crtc_state,
147 			      struct intel_plane_state *plane_state)
148 {
149 	struct intel_display *display = to_intel_display(plane_state);
150 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
151 	const struct drm_framebuffer *fb = plane_state->hw.fb;
152 	const struct drm_rect src = plane_state->uapi.src;
153 	const struct drm_rect dst = plane_state->uapi.dst;
154 	int ret;
155 
156 	if (fb && fb->modifier != DRM_FORMAT_MOD_LINEAR) {
157 		drm_dbg_kms(display->drm, "[PLANE:%d:%s] cursor cannot be tiled\n",
158 			    plane->base.base.id, plane->base.name);
159 		return -EINVAL;
160 	}
161 
162 	ret = intel_atomic_plane_check_clipping(plane_state, crtc_state,
163 						DRM_PLANE_NO_SCALING,
164 						DRM_PLANE_NO_SCALING,
165 						true);
166 	if (ret)
167 		return ret;
168 
169 	/* Use the unclipped src/dst rectangles, which we program to hw */
170 	plane_state->uapi.src = src;
171 	plane_state->uapi.dst = dst;
172 
173 	/* final plane coordinates will be relative to the plane's pipe */
174 	drm_rect_translate(&plane_state->uapi.dst,
175 			   -crtc_state->pipe_src.x1,
176 			   -crtc_state->pipe_src.y1);
177 
178 	ret = intel_cursor_check_surface(plane_state);
179 	if (ret)
180 		return ret;
181 
182 	if (!plane_state->uapi.visible)
183 		return 0;
184 
185 	ret = intel_plane_check_src_coordinates(plane_state);
186 	if (ret)
187 		return ret;
188 
189 	return 0;
190 }
191 
192 static unsigned int
193 i845_cursor_max_stride(struct intel_plane *plane,
194 		       u32 pixel_format, u64 modifier,
195 		       unsigned int rotation)
196 {
197 	return 2048;
198 }
199 
200 static unsigned int i845_cursor_min_alignment(struct intel_plane *plane,
201 					      const struct drm_framebuffer *fb,
202 					      int color_plane)
203 {
204 	return 32;
205 }
206 
207 static u32 i845_cursor_ctl_crtc(const struct intel_crtc_state *crtc_state)
208 {
209 	u32 cntl = 0;
210 
211 	if (crtc_state->gamma_enable)
212 		cntl |= CURSOR_PIPE_GAMMA_ENABLE;
213 
214 	return cntl;
215 }
216 
217 static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
218 			   const struct intel_plane_state *plane_state)
219 {
220 	return CURSOR_ENABLE |
221 		CURSOR_FORMAT_ARGB |
222 		CURSOR_STRIDE(plane_state->view.color_plane[0].mapping_stride);
223 }
224 
225 static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
226 {
227 	int width = drm_rect_width(&plane_state->uapi.dst);
228 
229 	/*
230 	 * 845g/865g are only limited by the width of their cursors,
231 	 * the height is arbitrary up to the precision of the register.
232 	 */
233 	return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
234 }
235 
236 static int i845_check_cursor(struct intel_crtc_state *crtc_state,
237 			     struct intel_plane_state *plane_state)
238 {
239 	struct intel_display *display = to_intel_display(plane_state);
240 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
241 	const struct drm_framebuffer *fb = plane_state->hw.fb;
242 	int ret;
243 
244 	ret = intel_check_cursor(crtc_state, plane_state);
245 	if (ret)
246 		return ret;
247 
248 	/* if we want to turn off the cursor ignore width and height */
249 	if (!fb)
250 		return 0;
251 
252 	/* Check for which cursor types we support */
253 	if (!i845_cursor_size_ok(plane_state)) {
254 		drm_dbg_kms(display->drm,
255 			    "[PLANE:%d:%s] cursor dimension %dx%d not supported\n",
256 			    plane->base.base.id, plane->base.name,
257 			    drm_rect_width(&plane_state->uapi.dst),
258 			    drm_rect_height(&plane_state->uapi.dst));
259 		return -EINVAL;
260 	}
261 
262 	drm_WARN_ON(display->drm, plane_state->uapi.visible &&
263 		    plane_state->view.color_plane[0].mapping_stride != fb->pitches[0]);
264 
265 	switch (fb->pitches[0]) {
266 	case 256:
267 	case 512:
268 	case 1024:
269 	case 2048:
270 		break;
271 	default:
272 		 drm_dbg_kms(display->drm, "[PLANE:%d:%s] invalid cursor stride (%u)\n",
273 			     plane->base.base.id, plane->base.name,
274 			     fb->pitches[0]);
275 		return -EINVAL;
276 	}
277 
278 	plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
279 
280 	return 0;
281 }
282 
283 /* TODO: split into noarm+arm pair */
284 static void i845_cursor_update_arm(struct intel_dsb *dsb,
285 				   struct intel_plane *plane,
286 				   const struct intel_crtc_state *crtc_state,
287 				   const struct intel_plane_state *plane_state)
288 {
289 	struct intel_display *display = to_intel_display(plane);
290 	u32 cntl = 0, base = 0, pos = 0, size = 0;
291 
292 	if (plane_state && plane_state->uapi.visible) {
293 		unsigned int width = drm_rect_width(&plane_state->uapi.dst);
294 		unsigned int height = drm_rect_height(&plane_state->uapi.dst);
295 
296 		cntl = plane_state->ctl |
297 			i845_cursor_ctl_crtc(crtc_state);
298 
299 		size = CURSOR_HEIGHT(height) | CURSOR_WIDTH(width);
300 
301 		base = intel_cursor_base(plane_state);
302 		pos = intel_cursor_position(crtc_state, plane_state, false);
303 	}
304 
305 	/* On these chipsets we can only modify the base/size/stride
306 	 * whilst the cursor is disabled.
307 	 */
308 	if (plane->cursor.base != base ||
309 	    plane->cursor.size != size ||
310 	    plane->cursor.cntl != cntl) {
311 		intel_de_write_fw(display, CURCNTR(display, PIPE_A), 0);
312 		intel_de_write_fw(display, CURBASE(display, PIPE_A), base);
313 		intel_de_write_fw(display, CURSIZE(display, PIPE_A), size);
314 		intel_de_write_fw(display, CURPOS(display, PIPE_A), pos);
315 		intel_de_write_fw(display, CURCNTR(display, PIPE_A), cntl);
316 
317 		plane->cursor.base = base;
318 		plane->cursor.size = size;
319 		plane->cursor.cntl = cntl;
320 	} else {
321 		intel_de_write_fw(display, CURPOS(display, PIPE_A), pos);
322 	}
323 }
324 
325 static void i845_cursor_disable_arm(struct intel_dsb *dsb,
326 				    struct intel_plane *plane,
327 				    const struct intel_crtc_state *crtc_state)
328 {
329 	i845_cursor_update_arm(dsb, plane, crtc_state, NULL);
330 }
331 
332 static bool i845_cursor_get_hw_state(struct intel_plane *plane,
333 				     enum pipe *pipe)
334 {
335 	struct intel_display *display = to_intel_display(plane);
336 	enum intel_display_power_domain power_domain;
337 	intel_wakeref_t wakeref;
338 	bool ret;
339 
340 	power_domain = POWER_DOMAIN_PIPE(PIPE_A);
341 	wakeref = intel_display_power_get_if_enabled(display, power_domain);
342 	if (!wakeref)
343 		return false;
344 
345 	ret = intel_de_read(display, CURCNTR(display, PIPE_A)) & CURSOR_ENABLE;
346 
347 	*pipe = PIPE_A;
348 
349 	intel_display_power_put(display, power_domain, wakeref);
350 
351 	return ret;
352 }
353 
354 static unsigned int
355 i9xx_cursor_max_stride(struct intel_plane *plane,
356 		       u32 pixel_format, u64 modifier,
357 		       unsigned int rotation)
358 {
359 	return plane->base.dev->mode_config.cursor_width * 4;
360 }
361 
362 static unsigned int i830_cursor_min_alignment(struct intel_plane *plane,
363 					      const struct drm_framebuffer *fb,
364 					      int color_plane)
365 {
366 	/* "AlmadorM Errata – Requires 32-bpp cursor data to be 16KB aligned." */
367 	return 16 * 1024; /* physical */
368 }
369 
370 static unsigned int i85x_cursor_min_alignment(struct intel_plane *plane,
371 					      const struct drm_framebuffer *fb,
372 					      int color_plane)
373 {
374 	return 256; /* physical */
375 }
376 
377 static unsigned int i9xx_cursor_min_alignment(struct intel_plane *plane,
378 					      const struct drm_framebuffer *fb,
379 					      int color_plane)
380 {
381 	struct intel_display *display = to_intel_display(plane);
382 
383 	if (intel_scanout_needs_vtd_wa(display))
384 		return 64 * 1024;
385 
386 	return 4 * 1024; /* physical for i915/i945 */
387 }
388 
389 static u32 i9xx_cursor_ctl_crtc(const struct intel_crtc_state *crtc_state)
390 {
391 	struct intel_display *display = to_intel_display(crtc_state);
392 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
393 	u32 cntl = 0;
394 
395 	if (DISPLAY_VER(display) >= 11)
396 		return cntl;
397 
398 	if (crtc_state->gamma_enable)
399 		cntl = MCURSOR_PIPE_GAMMA_ENABLE;
400 
401 	if (crtc_state->csc_enable)
402 		cntl |= MCURSOR_PIPE_CSC_ENABLE;
403 
404 	if (DISPLAY_VER(display) < 5 && !display->platform.g4x)
405 		cntl |= MCURSOR_PIPE_SEL(crtc->pipe);
406 
407 	return cntl;
408 }
409 
410 static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
411 			   const struct intel_plane_state *plane_state)
412 {
413 	struct intel_display *display = to_intel_display(plane_state);
414 	u32 cntl = 0;
415 
416 	if (display->platform.sandybridge || display->platform.ivybridge)
417 		cntl |= MCURSOR_TRICKLE_FEED_DISABLE;
418 
419 	switch (drm_rect_width(&plane_state->uapi.dst)) {
420 	case 64:
421 		cntl |= MCURSOR_MODE_64_ARGB_AX;
422 		break;
423 	case 128:
424 		cntl |= MCURSOR_MODE_128_ARGB_AX;
425 		break;
426 	case 256:
427 		cntl |= MCURSOR_MODE_256_ARGB_AX;
428 		break;
429 	default:
430 		MISSING_CASE(drm_rect_width(&plane_state->uapi.dst));
431 		return 0;
432 	}
433 
434 	if (plane_state->hw.rotation & DRM_MODE_ROTATE_180)
435 		cntl |= MCURSOR_ROTATE_180;
436 
437 	/* Wa_22012358565:adl-p */
438 	if (DISPLAY_VER(display) == 13)
439 		cntl |= MCURSOR_ARB_SLOTS(1);
440 
441 	return cntl;
442 }
443 
444 static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
445 {
446 	struct intel_display *display = to_intel_display(plane_state);
447 	int width = drm_rect_width(&plane_state->uapi.dst);
448 	int height = drm_rect_height(&plane_state->uapi.dst);
449 
450 	if (!intel_cursor_size_ok(plane_state))
451 		return false;
452 
453 	/* Cursor width is limited to a few power-of-two sizes */
454 	switch (width) {
455 	case 256:
456 	case 128:
457 	case 64:
458 		break;
459 	default:
460 		return false;
461 	}
462 
463 	/*
464 	 * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
465 	 * height from 8 lines up to the cursor width, when the
466 	 * cursor is not rotated. Everything else requires square
467 	 * cursors.
468 	 */
469 	if (HAS_CUR_FBC(display) &&
470 	    plane_state->hw.rotation & DRM_MODE_ROTATE_0) {
471 		if (height < 8 || height > width)
472 			return false;
473 	} else {
474 		if (height != width)
475 			return false;
476 	}
477 
478 	return true;
479 }
480 
481 static int i9xx_check_cursor(struct intel_crtc_state *crtc_state,
482 			     struct intel_plane_state *plane_state)
483 {
484 	struct intel_display *display = to_intel_display(plane_state);
485 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
486 	const struct drm_framebuffer *fb = plane_state->hw.fb;
487 	enum pipe pipe = plane->pipe;
488 	int ret;
489 
490 	ret = intel_check_cursor(crtc_state, plane_state);
491 	if (ret)
492 		return ret;
493 
494 	/* if we want to turn off the cursor ignore width and height */
495 	if (!fb)
496 		return 0;
497 
498 	/* Check for which cursor types we support */
499 	if (!i9xx_cursor_size_ok(plane_state)) {
500 		drm_dbg_kms(display->drm,
501 			    "[PLANE:%d:%s] cursor dimension %dx%d not supported\n",
502 			    plane->base.base.id, plane->base.name,
503 			    drm_rect_width(&plane_state->uapi.dst),
504 			    drm_rect_height(&plane_state->uapi.dst));
505 		return -EINVAL;
506 	}
507 
508 	drm_WARN_ON(display->drm, plane_state->uapi.visible &&
509 		    plane_state->view.color_plane[0].mapping_stride != fb->pitches[0]);
510 
511 	if (fb->pitches[0] !=
512 	    drm_rect_width(&plane_state->uapi.dst) * fb->format->cpp[0]) {
513 		drm_dbg_kms(display->drm,
514 			    "[PLANE:%d:%s] invalid cursor stride (%u) (cursor width %d)\n",
515 			    plane->base.base.id, plane->base.name,
516 			    fb->pitches[0], drm_rect_width(&plane_state->uapi.dst));
517 		return -EINVAL;
518 	}
519 
520 	/*
521 	 * There's something wrong with the cursor on CHV pipe C.
522 	 * If it straddles the left edge of the screen then
523 	 * moving it away from the edge or disabling it often
524 	 * results in a pipe underrun, and often that can lead to
525 	 * dead pipe (constant underrun reported, and it scans
526 	 * out just a solid color). To recover from that, the
527 	 * display power well must be turned off and on again.
528 	 * Refuse the put the cursor into that compromised position.
529 	 */
530 	if (display->platform.cherryview && pipe == PIPE_C &&
531 	    plane_state->uapi.visible && plane_state->uapi.dst.x1 < 0) {
532 		drm_dbg_kms(display->drm,
533 			    "[PLANE:%d:%s] cursor not allowed to straddle the left screen edge\n",
534 			    plane->base.base.id, plane->base.name);
535 		return -EINVAL;
536 	}
537 
538 	plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
539 
540 	return 0;
541 }
542 
543 static void i9xx_cursor_disable_sel_fetch_arm(struct intel_dsb *dsb,
544 					      struct intel_plane *plane,
545 					      const struct intel_crtc_state *crtc_state)
546 {
547 	struct intel_display *display = to_intel_display(plane);
548 	enum pipe pipe = plane->pipe;
549 
550 	if (!crtc_state->enable_psr2_sel_fetch)
551 		return;
552 
553 	intel_de_write_dsb(display, dsb, SEL_FETCH_CUR_CTL(pipe), 0);
554 }
555 
556 static void wa_16021440873(struct intel_dsb *dsb,
557 			   struct intel_plane *plane,
558 			   const struct intel_crtc_state *crtc_state,
559 			   const struct intel_plane_state *plane_state)
560 {
561 	struct intel_display *display = to_intel_display(plane);
562 	u32 ctl = plane_state->ctl;
563 	int et_y_position = drm_rect_height(&crtc_state->pipe_src) + 1;
564 	enum pipe pipe = plane->pipe;
565 
566 	ctl &= ~MCURSOR_MODE_MASK;
567 	ctl |= MCURSOR_MODE_64_2B;
568 
569 	intel_de_write_dsb(display, dsb, SEL_FETCH_CUR_CTL(pipe), ctl);
570 
571 	intel_de_write_dsb(display, dsb, CURPOS_ERLY_TPT(display, pipe),
572 			   CURSOR_POS_Y(et_y_position));
573 }
574 
575 static void i9xx_cursor_update_sel_fetch_arm(struct intel_dsb *dsb,
576 					     struct intel_plane *plane,
577 					     const struct intel_crtc_state *crtc_state,
578 					     const struct intel_plane_state *plane_state)
579 {
580 	struct intel_display *display = to_intel_display(plane);
581 	enum pipe pipe = plane->pipe;
582 
583 	if (!crtc_state->enable_psr2_sel_fetch)
584 		return;
585 
586 	if (drm_rect_height(&plane_state->psr2_sel_fetch_area) > 0) {
587 		if (crtc_state->enable_psr2_su_region_et) {
588 			u32 val = intel_cursor_position(crtc_state, plane_state,
589 				true);
590 
591 			intel_de_write_dsb(display, dsb, CURPOS_ERLY_TPT(display, pipe), val);
592 		}
593 
594 		intel_de_write_dsb(display, dsb, SEL_FETCH_CUR_CTL(pipe), plane_state->ctl);
595 	} else {
596 		/* Wa_16021440873 */
597 		if (crtc_state->enable_psr2_su_region_et)
598 			wa_16021440873(dsb, plane, crtc_state, plane_state);
599 		else
600 			i9xx_cursor_disable_sel_fetch_arm(dsb, plane, crtc_state);
601 	}
602 }
603 
604 static u32 skl_cursor_ddb_reg_val(const struct skl_ddb_entry *entry)
605 {
606 	if (!entry->end)
607 		return 0;
608 
609 	return CUR_BUF_END(entry->end - 1) |
610 		CUR_BUF_START(entry->start);
611 }
612 
613 static u32 skl_cursor_wm_reg_val(const struct skl_wm_level *level)
614 {
615 	u32 val = 0;
616 
617 	if (level->enable)
618 		val |= CUR_WM_EN;
619 	if (level->ignore_lines)
620 		val |= CUR_WM_IGNORE_LINES;
621 	val |= REG_FIELD_PREP(CUR_WM_BLOCKS_MASK, level->blocks);
622 	val |= REG_FIELD_PREP(CUR_WM_LINES_MASK, level->lines);
623 
624 	return val;
625 }
626 
627 static void skl_write_cursor_wm(struct intel_dsb *dsb,
628 				struct intel_plane *plane,
629 				const struct intel_crtc_state *crtc_state)
630 {
631 	struct intel_display *display = to_intel_display(plane->base.dev);
632 	enum plane_id plane_id = plane->id;
633 	enum pipe pipe = plane->pipe;
634 	const struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
635 	const struct skl_ddb_entry *ddb =
636 		&crtc_state->wm.skl.plane_ddb[plane_id];
637 	int level;
638 
639 	for (level = 0; level < display->wm.num_levels; level++)
640 		intel_de_write_dsb(display, dsb, CUR_WM(pipe, level),
641 				   skl_cursor_wm_reg_val(skl_plane_wm_level(pipe_wm, plane_id, level)));
642 
643 	intel_de_write_dsb(display, dsb, CUR_WM_TRANS(pipe),
644 			   skl_cursor_wm_reg_val(skl_plane_trans_wm(pipe_wm, plane_id)));
645 
646 	if (HAS_HW_SAGV_WM(display)) {
647 		const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
648 
649 		intel_de_write_dsb(display, dsb, CUR_WM_SAGV(pipe),
650 				   skl_cursor_wm_reg_val(&wm->sagv.wm0));
651 		intel_de_write_dsb(display, dsb, CUR_WM_SAGV_TRANS(pipe),
652 				   skl_cursor_wm_reg_val(&wm->sagv.trans_wm));
653 	}
654 
655 	intel_de_write_dsb(display, dsb, CUR_BUF_CFG(pipe),
656 			   skl_cursor_ddb_reg_val(ddb));
657 }
658 
659 /* TODO: split into noarm+arm pair */
660 static void i9xx_cursor_update_arm(struct intel_dsb *dsb,
661 				   struct intel_plane *plane,
662 				   const struct intel_crtc_state *crtc_state,
663 				   const struct intel_plane_state *plane_state)
664 {
665 	struct intel_display *display = to_intel_display(plane);
666 	enum pipe pipe = plane->pipe;
667 	u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
668 
669 	if (plane_state && plane_state->uapi.visible) {
670 		int width = drm_rect_width(&plane_state->uapi.dst);
671 		int height = drm_rect_height(&plane_state->uapi.dst);
672 
673 		cntl = plane_state->ctl |
674 			i9xx_cursor_ctl_crtc(crtc_state);
675 
676 		if (width != height)
677 			fbc_ctl = CUR_FBC_EN | CUR_FBC_HEIGHT(height - 1);
678 
679 		base = intel_cursor_base(plane_state);
680 		pos = intel_cursor_position(crtc_state, plane_state, false);
681 	}
682 
683 	/*
684 	 * On some platforms writing CURCNTR first will also
685 	 * cause CURPOS to be armed by the CURBASE write.
686 	 * Without the CURCNTR write the CURPOS write would
687 	 * arm itself. Thus we always update CURCNTR before
688 	 * CURPOS.
689 	 *
690 	 * On other platforms CURPOS always requires the
691 	 * CURBASE write to arm the update. Additionally
692 	 * a write to any of the cursor register will cancel
693 	 * an already armed cursor update. Thus leaving out
694 	 * the CURBASE write after CURPOS could lead to a
695 	 * cursor that doesn't appear to move, or even change
696 	 * shape. Thus we always write CURBASE.
697 	 *
698 	 * The other registers are armed by the CURBASE write
699 	 * except when the plane is getting enabled at which time
700 	 * the CURCNTR write arms the update.
701 	 */
702 
703 	if (DISPLAY_VER(display) >= 9)
704 		skl_write_cursor_wm(dsb, plane, crtc_state);
705 
706 	if (plane_state)
707 		i9xx_cursor_update_sel_fetch_arm(dsb, plane, crtc_state, plane_state);
708 	else
709 		i9xx_cursor_disable_sel_fetch_arm(dsb, plane, crtc_state);
710 
711 	if (plane->cursor.base != base ||
712 	    plane->cursor.size != fbc_ctl ||
713 	    plane->cursor.cntl != cntl) {
714 		if (HAS_CUR_FBC(display))
715 			intel_de_write_dsb(display, dsb, CUR_FBC_CTL(display, pipe), fbc_ctl);
716 		intel_de_write_dsb(display, dsb, CURCNTR(display, pipe), cntl);
717 		intel_de_write_dsb(display, dsb, CURPOS(display, pipe), pos);
718 		intel_de_write_dsb(display, dsb, CURBASE(display, pipe), base);
719 
720 		plane->cursor.base = base;
721 		plane->cursor.size = fbc_ctl;
722 		plane->cursor.cntl = cntl;
723 	} else {
724 		intel_de_write_dsb(display, dsb, CURPOS(display, pipe), pos);
725 		intel_de_write_dsb(display, dsb, CURBASE(display, pipe), base);
726 	}
727 }
728 
729 static void i9xx_cursor_disable_arm(struct intel_dsb *dsb,
730 				    struct intel_plane *plane,
731 				    const struct intel_crtc_state *crtc_state)
732 {
733 	i9xx_cursor_update_arm(dsb, plane, crtc_state, NULL);
734 }
735 
736 static bool i9xx_cursor_get_hw_state(struct intel_plane *plane,
737 				     enum pipe *pipe)
738 {
739 	struct intel_display *display = to_intel_display(plane);
740 	enum intel_display_power_domain power_domain;
741 	intel_wakeref_t wakeref;
742 	bool ret;
743 	u32 val;
744 
745 	/*
746 	 * Not 100% correct for planes that can move between pipes,
747 	 * but that's only the case for gen2-3 which don't have any
748 	 * display power wells.
749 	 */
750 	power_domain = POWER_DOMAIN_PIPE(plane->pipe);
751 	wakeref = intel_display_power_get_if_enabled(display, power_domain);
752 	if (!wakeref)
753 		return false;
754 
755 	val = intel_de_read(display, CURCNTR(display, plane->pipe));
756 
757 	ret = val & MCURSOR_MODE_MASK;
758 
759 	if (DISPLAY_VER(display) >= 5 || display->platform.g4x)
760 		*pipe = plane->pipe;
761 	else
762 		*pipe = REG_FIELD_GET(MCURSOR_PIPE_SEL_MASK, val);
763 
764 	intel_display_power_put(display, power_domain, wakeref);
765 
766 	return ret;
767 }
768 
769 static void g4x_cursor_capture_error(struct intel_crtc *crtc,
770 				     struct intel_plane *plane,
771 				     struct intel_plane_error *error)
772 {
773 	struct intel_display *display = to_intel_display(plane);
774 
775 	error->ctl = intel_de_read(display, CURCNTR(display, crtc->pipe));
776 	error->surf = intel_de_read(display, CURBASE(display, crtc->pipe));
777 	error->surflive = intel_de_read(display, CURSURFLIVE(display, crtc->pipe));
778 }
779 
780 static void i9xx_cursor_capture_error(struct intel_crtc *crtc,
781 				      struct intel_plane *plane,
782 				      struct intel_plane_error *error)
783 {
784 	struct intel_display *display = to_intel_display(plane);
785 
786 	error->ctl = intel_de_read(display, CURCNTR(display, crtc->pipe));
787 	error->surf = intel_de_read(display, CURBASE(display, crtc->pipe));
788 }
789 
790 static bool intel_cursor_format_mod_supported(struct drm_plane *_plane,
791 					      u32 format, u64 modifier)
792 {
793 	if (!intel_fb_plane_supports_modifier(to_intel_plane(_plane), modifier))
794 		return false;
795 
796 	return format == DRM_FORMAT_ARGB8888;
797 }
798 
799 void intel_cursor_unpin_work(struct kthread_work *base)
800 {
801 	struct drm_vblank_work *work = to_drm_vblank_work(base);
802 	struct intel_plane_state *plane_state =
803 		container_of(work, typeof(*plane_state), unpin_work);
804 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
805 
806 	intel_plane_unpin_fb(plane_state);
807 	intel_plane_destroy_state(&plane->base, &plane_state->uapi);
808 }
809 
810 static int
811 intel_legacy_cursor_update(struct drm_plane *_plane,
812 			   struct drm_crtc *_crtc,
813 			   struct drm_framebuffer *fb,
814 			   int crtc_x, int crtc_y,
815 			   unsigned int crtc_w, unsigned int crtc_h,
816 			   u32 src_x, u32 src_y,
817 			   u32 src_w, u32 src_h,
818 			   struct drm_modeset_acquire_ctx *ctx)
819 {
820 	struct intel_plane *plane = to_intel_plane(_plane);
821 	struct intel_crtc *crtc = to_intel_crtc(_crtc);
822 	struct intel_display *display = to_intel_display(plane);
823 	struct intel_plane_state *old_plane_state =
824 		to_intel_plane_state(plane->base.state);
825 	struct intel_plane_state *new_plane_state;
826 	struct intel_crtc_state *crtc_state =
827 		to_intel_crtc_state(crtc->base.state);
828 	struct intel_crtc_state *new_crtc_state;
829 	struct intel_vblank_evade_ctx evade;
830 	int ret;
831 
832 	/*
833 	 * When crtc is inactive or there is a modeset pending,
834 	 * wait for it to complete in the slowpath.
835 	 * PSR2 selective fetch also requires the slow path as
836 	 * PSR2 plane and transcoder registers can only be updated during
837 	 * vblank.
838 	 *
839 	 * FIXME joiner fastpath would be good
840 	 */
841 	if (!crtc_state->hw.active ||
842 	    intel_crtc_needs_modeset(crtc_state) ||
843 	    intel_crtc_needs_fastset(crtc_state) ||
844 	    crtc_state->joiner_pipes)
845 		goto slow;
846 
847 	/*
848 	 * Don't do an async update if there is an outstanding commit modifying
849 	 * the plane.  This prevents our async update's changes from getting
850 	 * overridden by a previous synchronous update's state.
851 	 */
852 	if (old_plane_state->uapi.commit &&
853 	    !try_wait_for_completion(&old_plane_state->uapi.commit->hw_done))
854 		goto slow;
855 
856 	/*
857 	 * If any parameters change that may affect watermarks,
858 	 * take the slowpath. Only changing fb or position should be
859 	 * in the fastpath.
860 	 */
861 	if (old_plane_state->uapi.crtc != &crtc->base ||
862 	    old_plane_state->uapi.src_w != src_w ||
863 	    old_plane_state->uapi.src_h != src_h ||
864 	    old_plane_state->uapi.crtc_w != crtc_w ||
865 	    old_plane_state->uapi.crtc_h != crtc_h ||
866 	    !old_plane_state->uapi.fb != !fb)
867 		goto slow;
868 
869 	new_plane_state = to_intel_plane_state(intel_plane_duplicate_state(&plane->base));
870 	if (!new_plane_state)
871 		return -ENOMEM;
872 
873 	new_crtc_state = to_intel_crtc_state(intel_crtc_duplicate_state(&crtc->base));
874 	if (!new_crtc_state) {
875 		ret = -ENOMEM;
876 		goto out_free;
877 	}
878 
879 	drm_atomic_set_fb_for_plane(&new_plane_state->uapi, fb);
880 
881 	new_plane_state->uapi.src_x = src_x;
882 	new_plane_state->uapi.src_y = src_y;
883 	new_plane_state->uapi.src_w = src_w;
884 	new_plane_state->uapi.src_h = src_h;
885 	new_plane_state->uapi.crtc_x = crtc_x;
886 	new_plane_state->uapi.crtc_y = crtc_y;
887 	new_plane_state->uapi.crtc_w = crtc_w;
888 	new_plane_state->uapi.crtc_h = crtc_h;
889 
890 	intel_plane_copy_uapi_to_hw_state(new_plane_state, new_plane_state, crtc);
891 
892 	ret = intel_plane_atomic_check_with_state(crtc_state, new_crtc_state,
893 						  old_plane_state, new_plane_state);
894 	if (ret)
895 		goto out_free;
896 
897 	ret = intel_plane_pin_fb(new_plane_state, old_plane_state);
898 	if (ret)
899 		goto out_free;
900 
901 	intel_frontbuffer_flush(to_intel_frontbuffer(new_plane_state->hw.fb),
902 				ORIGIN_CURSOR_UPDATE);
903 	intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->hw.fb),
904 				to_intel_frontbuffer(new_plane_state->hw.fb),
905 				plane->frontbuffer_bit);
906 
907 	/* Swap plane state */
908 	plane->base.state = &new_plane_state->uapi;
909 
910 	/*
911 	 * We cannot swap crtc_state as it may be in use by an atomic commit or
912 	 * page flip that's running simultaneously. If we swap crtc_state and
913 	 * destroy the old state, we will cause a use-after-free there.
914 	 *
915 	 * Only update active_planes, which is needed for our internal
916 	 * bookkeeping. Either value will do the right thing when updating
917 	 * planes atomically. If the cursor was part of the atomic update then
918 	 * we would have taken the slowpath.
919 	 */
920 	crtc_state->active_planes = new_crtc_state->active_planes;
921 
922 	intel_vblank_evade_init(crtc_state, crtc_state, &evade);
923 
924 	intel_psr_lock(crtc_state);
925 
926 	if (!drm_WARN_ON(display->drm, drm_crtc_vblank_get(&crtc->base))) {
927 		/*
928 		 * TODO: maybe check if we're still in PSR
929 		 * and skip the vblank evasion entirely?
930 		 */
931 		intel_psr_wait_for_idle_locked(crtc_state);
932 
933 		local_irq_disable();
934 
935 		intel_vblank_evade(&evade);
936 
937 		drm_crtc_vblank_put(&crtc->base);
938 	} else {
939 		local_irq_disable();
940 	}
941 
942 	if (new_plane_state->uapi.visible) {
943 		intel_plane_update_noarm(NULL, plane, crtc_state, new_plane_state);
944 		intel_plane_update_arm(NULL, plane, crtc_state, new_plane_state);
945 	} else {
946 		intel_plane_disable_arm(NULL, plane, crtc_state);
947 	}
948 
949 	local_irq_enable();
950 
951 	intel_psr_unlock(crtc_state);
952 
953 	if (old_plane_state->ggtt_vma != new_plane_state->ggtt_vma) {
954 		drm_vblank_work_init(&old_plane_state->unpin_work, &crtc->base,
955 				     intel_cursor_unpin_work);
956 
957 		drm_vblank_work_schedule(&old_plane_state->unpin_work,
958 					 drm_crtc_accurate_vblank_count(&crtc->base) + 1,
959 					 false);
960 
961 		old_plane_state = NULL;
962 	} else {
963 		intel_plane_unpin_fb(old_plane_state);
964 	}
965 
966 out_free:
967 	if (new_crtc_state)
968 		intel_crtc_destroy_state(&crtc->base, &new_crtc_state->uapi);
969 	if (ret)
970 		intel_plane_destroy_state(&plane->base, &new_plane_state->uapi);
971 	else if (old_plane_state)
972 		intel_plane_destroy_state(&plane->base, &old_plane_state->uapi);
973 	return ret;
974 
975 slow:
976 	return drm_atomic_helper_update_plane(&plane->base, &crtc->base, fb,
977 					      crtc_x, crtc_y, crtc_w, crtc_h,
978 					      src_x, src_y, src_w, src_h, ctx);
979 }
980 
981 static const struct drm_plane_funcs intel_cursor_plane_funcs = {
982 	.update_plane = intel_legacy_cursor_update,
983 	.disable_plane = drm_atomic_helper_disable_plane,
984 	.destroy = intel_plane_destroy,
985 	.atomic_duplicate_state = intel_plane_duplicate_state,
986 	.atomic_destroy_state = intel_plane_destroy_state,
987 	.format_mod_supported = intel_cursor_format_mod_supported,
988 };
989 
990 static void intel_cursor_add_size_hints_property(struct intel_plane *plane)
991 {
992 	struct intel_display *display = to_intel_display(plane);
993 	const struct drm_mode_config *config = &display->drm->mode_config;
994 	struct drm_plane_size_hint hints[4];
995 	int size, max_size, num_hints = 0;
996 
997 	max_size = min(config->cursor_width, config->cursor_height);
998 
999 	/* for simplicity only enumerate the supported square+POT sizes */
1000 	for (size = 64; size <= max_size; size *= 2) {
1001 		if (drm_WARN_ON(display->drm, num_hints >= ARRAY_SIZE(hints)))
1002 			break;
1003 
1004 		hints[num_hints].width = size;
1005 		hints[num_hints].height = size;
1006 		num_hints++;
1007 	}
1008 
1009 	drm_plane_add_size_hints_property(&plane->base, hints, num_hints);
1010 }
1011 
1012 struct intel_plane *
1013 intel_cursor_plane_create(struct intel_display *display,
1014 			  enum pipe pipe)
1015 {
1016 	struct intel_plane *cursor;
1017 	int ret, zpos;
1018 	u64 *modifiers;
1019 
1020 	cursor = intel_plane_alloc();
1021 	if (IS_ERR(cursor))
1022 		return cursor;
1023 
1024 	cursor->pipe = pipe;
1025 	cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
1026 	cursor->id = PLANE_CURSOR;
1027 	cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id);
1028 
1029 	if (display->platform.i845g || display->platform.i865g) {
1030 		cursor->max_stride = i845_cursor_max_stride;
1031 		cursor->min_alignment = i845_cursor_min_alignment;
1032 		cursor->update_arm = i845_cursor_update_arm;
1033 		cursor->disable_arm = i845_cursor_disable_arm;
1034 		cursor->get_hw_state = i845_cursor_get_hw_state;
1035 		cursor->check_plane = i845_check_cursor;
1036 	} else {
1037 		cursor->max_stride = i9xx_cursor_max_stride;
1038 
1039 		if (display->platform.i830)
1040 			cursor->min_alignment = i830_cursor_min_alignment;
1041 		else if (display->platform.i85x)
1042 			cursor->min_alignment = i85x_cursor_min_alignment;
1043 		else
1044 			cursor->min_alignment = i9xx_cursor_min_alignment;
1045 
1046 		if (intel_scanout_needs_vtd_wa(display))
1047 			cursor->vtd_guard = 2;
1048 
1049 		cursor->update_arm = i9xx_cursor_update_arm;
1050 		cursor->disable_arm = i9xx_cursor_disable_arm;
1051 		cursor->get_hw_state = i9xx_cursor_get_hw_state;
1052 		cursor->check_plane = i9xx_check_cursor;
1053 	}
1054 
1055 	if (DISPLAY_VER(display) >= 5 || display->platform.g4x)
1056 		cursor->capture_error = g4x_cursor_capture_error;
1057 	else
1058 		cursor->capture_error = i9xx_cursor_capture_error;
1059 
1060 	cursor->cursor.base = ~0;
1061 	cursor->cursor.cntl = ~0;
1062 
1063 	if (display->platform.i845g || display->platform.i865g || HAS_CUR_FBC(display))
1064 		cursor->cursor.size = ~0;
1065 
1066 	modifiers = intel_fb_plane_get_modifiers(display, INTEL_PLANE_CAP_NONE);
1067 
1068 	ret = drm_universal_plane_init(display->drm, &cursor->base,
1069 				       0, &intel_cursor_plane_funcs,
1070 				       intel_cursor_formats,
1071 				       ARRAY_SIZE(intel_cursor_formats),
1072 				       modifiers,
1073 				       DRM_PLANE_TYPE_CURSOR,
1074 				       "cursor %c", pipe_name(pipe));
1075 
1076 	kfree(modifiers);
1077 
1078 	if (ret)
1079 		goto fail;
1080 
1081 	if (DISPLAY_VER(display) >= 4)
1082 		drm_plane_create_rotation_property(&cursor->base,
1083 						   DRM_MODE_ROTATE_0,
1084 						   DRM_MODE_ROTATE_0 |
1085 						   DRM_MODE_ROTATE_180);
1086 
1087 	intel_cursor_add_size_hints_property(cursor);
1088 
1089 	zpos = DISPLAY_RUNTIME_INFO(display)->num_sprites[pipe] + 1;
1090 	drm_plane_create_zpos_immutable_property(&cursor->base, zpos);
1091 
1092 	if (DISPLAY_VER(display) >= 12)
1093 		drm_plane_enable_fb_damage_clips(&cursor->base);
1094 
1095 	intel_plane_helper_add(cursor);
1096 
1097 	return cursor;
1098 
1099 fail:
1100 	intel_plane_free(cursor);
1101 
1102 	return ERR_PTR(ret);
1103 }
1104