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