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