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