xref: /linux/drivers/gpu/drm/i915/display/intel_cursor.c (revision b0a3a811634aa7422f8a92196db47a06a70bda16)
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_crtc.h"
17 #include "intel_cursor.h"
18 #include "intel_cursor_regs.h"
19 #include "intel_de.h"
20 #include "intel_display.h"
21 #include "intel_display_types.h"
22 #include "intel_display_utils.h"
23 #include "intel_display_wa.h"
24 #include "intel_fb.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 intel_cursor_lock_joined_planes(struct intel_display *display,
801 					   const struct intel_crtc_state *crtc_state,
802 					   struct drm_modeset_acquire_ctx *ctx)
803 {
804 	struct intel_crtc *pipe_crtc;
805 	int ret;
806 
807 	for_each_intel_crtc_in_pipe_mask(display, pipe_crtc,
808 					 intel_crtc_joined_pipe_mask(crtc_state)) {
809 		struct intel_plane *pipe_plane =
810 			intel_crtc_get_plane(pipe_crtc, PLANE_CURSOR);
811 
812 		ret = drm_modeset_lock(&pipe_crtc->base.mutex, ctx);
813 		if (ret)
814 			return ret;
815 
816 		ret = drm_modeset_lock(&pipe_plane->base.mutex, ctx);
817 		if (ret)
818 			return ret;
819 	}
820 	return 0;
821 }
822 
823 static bool
824 intel_cursor_joiner_commits_idle(struct intel_display *display,
825 				 const struct intel_crtc_state *crtc_state)
826 {
827 	struct intel_crtc *pipe_crtc;
828 
829 	for_each_intel_crtc_in_pipe_mask(display, pipe_crtc,
830 					 intel_crtc_joined_pipe_mask(crtc_state)) {
831 		struct intel_plane *pipe_plane =
832 			intel_crtc_get_plane(pipe_crtc, PLANE_CURSOR);
833 		struct intel_plane_state *pipe_plane_state =
834 			to_intel_plane_state(pipe_plane->base.state);
835 
836 		if (pipe_plane_state->uapi.commit &&
837 		    !try_wait_for_completion(&pipe_plane_state->uapi.commit->hw_done))
838 			return false;
839 	}
840 
841 	return true;
842 }
843 
844 static void
845 intel_cursor_fastpath_update_plane_state(struct intel_plane_state *plane_state,
846 					 const struct intel_plane_state *from_plane_state,
847 					 struct drm_framebuffer *fb,
848 					 struct intel_crtc *hw_crtc,
849 					 int crtc_x, int crtc_y,
850 					 unsigned int crtc_w, unsigned int crtc_h,
851 					 u32 src_x, u32 src_y,
852 					 u32 src_w, u32 src_h)
853 {
854 	/*
855 	 * Only the primary owns its uapi state; a secondary mirrors it, so
856 	 * its uapi.crtc/fb stay NULL and hw.crtc comes from hw_crtc.
857 	 */
858 	if (plane_state == from_plane_state) {
859 		drm_atomic_set_fb_for_plane(&plane_state->uapi, fb);
860 
861 		plane_state->uapi.src_x = src_x;
862 		plane_state->uapi.src_y = src_y;
863 		plane_state->uapi.src_w = src_w;
864 		plane_state->uapi.src_h = src_h;
865 		plane_state->uapi.crtc_x = crtc_x;
866 		plane_state->uapi.crtc_y = crtc_y;
867 		plane_state->uapi.crtc_w = crtc_w;
868 		plane_state->uapi.crtc_h = crtc_h;
869 	}
870 
871 	intel_plane_copy_uapi_to_hw_state(NULL, plane_state, from_plane_state, hw_crtc);
872 }
873 
874 struct intel_cursor_joiner_state {
875 	struct intel_plane *plane;
876 	struct intel_crtc *crtc;
877 	struct intel_crtc_state *crtc_state;
878 	struct intel_plane_state *old_plane_state;
879 	struct intel_plane_state *new_plane_state;
880 };
881 
882 static int
883 intel_legacy_cursor_update(struct drm_plane *_plane,
884 			   struct drm_crtc *_crtc,
885 			   struct drm_framebuffer *fb,
886 			   int crtc_x, int crtc_y,
887 			   unsigned int crtc_w, unsigned int crtc_h,
888 			   u32 src_x, u32 src_y,
889 			   u32 src_w, u32 src_h,
890 			   struct drm_modeset_acquire_ctx *ctx)
891 {
892 	struct intel_plane *plane = to_intel_plane(_plane);
893 	struct intel_crtc *crtc = to_intel_crtc(_crtc);
894 	struct intel_display *display = to_intel_display(plane);
895 	struct intel_plane_state *old_plane_state =
896 		to_intel_plane_state(plane->base.state);
897 	struct intel_crtc_state *crtc_state =
898 		to_intel_crtc_state(crtc->base.state);
899 	struct intel_vblank_evade_ctx evade;
900 	struct intel_cursor_joiner_state joined_pipe_state[I915_MAX_PIPES] = {};
901 	struct intel_crtc *pipe_crtc;
902 	int num_pipes = 0;
903 	u32 start_vbl_count, end_vbl_count;
904 	int ret;
905 
906 	/*
907 	 * When crtc is inactive or there is a modeset pending,
908 	 * wait for it to complete in the slowpath.
909 	 * PSR2 selective fetch also requires the slow path as
910 	 * PSR2 plane and transcoder registers can only be updated during
911 	 * vblank.
912 	 */
913 	if (!crtc_state->hw.active ||
914 	    intel_crtc_needs_modeset(crtc_state) ||
915 	    intel_crtc_needs_fastset(crtc_state))
916 		goto slow;
917 
918 	/*
919 	 * If any parameters change that may affect watermarks,
920 	 * take the slowpath. Only changing fb or position should be
921 	 * in the fastpath.
922 	 */
923 	if (old_plane_state->uapi.crtc != &crtc->base ||
924 	    old_plane_state->uapi.src_w != src_w ||
925 	    old_plane_state->uapi.src_h != src_h ||
926 	    old_plane_state->uapi.crtc_w != crtc_w ||
927 	    old_plane_state->uapi.crtc_h != crtc_h ||
928 	    !old_plane_state->uapi.fb != !fb)
929 		goto slow;
930 
931 	ret = intel_cursor_lock_joined_planes(display, crtc_state, ctx);
932 	if (ret == -EDEADLK)
933 		return ret;
934 	if (ret)
935 		goto slow;
936 
937 	/*
938 	 * Don't do an async update if there is an outstanding commit modifying
939 	 * any of the joined cursor planes. This prevents our async update's
940 	 * changes from getting overridden by a previous synchronous update's
941 	 * state.
942 	 */
943 	if (!intel_cursor_joiner_commits_idle(display, crtc_state))
944 		goto slow;
945 
946 	/*
947 	 * Iterate over all joined pipes (primary and secondary) uniformly.
948 	 * The joined pipe mask includes both the primary pipe and all
949 	 * secondary joiner pipes, allowing us to handle them all the same way.
950 	 */
951 	for_each_intel_crtc_in_pipe_mask(display, pipe_crtc,
952 					 intel_crtc_joined_pipe_mask(crtc_state)) {
953 		struct intel_cursor_joiner_state *j = &joined_pipe_state[num_pipes];
954 
955 		j->plane = intel_crtc_get_plane(pipe_crtc, PLANE_CURSOR);
956 		j->crtc = pipe_crtc;
957 		j->crtc_state = to_intel_crtc_state(pipe_crtc->base.state);
958 		j->old_plane_state = to_intel_plane_state(j->plane->base.state);
959 		j->new_plane_state =
960 			to_intel_plane_state(intel_plane_duplicate_state(&j->plane->base));
961 
962 		if (!j->new_plane_state) {
963 			ret = -ENOMEM;
964 			goto out_free;
965 		}
966 
967 		intel_cursor_fastpath_update_plane_state(j->new_plane_state,
968 							 joined_pipe_state[0].new_plane_state,
969 							 fb, pipe_crtc,
970 							 crtc_x, crtc_y,
971 							 crtc_w, crtc_h,
972 							 src_x, src_y,
973 							 src_w, src_h);
974 
975 		ret = j->plane->check_plane(j->crtc_state, j->new_plane_state);
976 		if (ret) {
977 			intel_plane_destroy_state(&j->plane->base,
978 						  &j->new_plane_state->uapi);
979 			goto out_free;
980 		}
981 
982 		ret = intel_plane_pin_fb(j->new_plane_state, j->old_plane_state);
983 		if (ret) {
984 			intel_plane_destroy_state(&j->plane->base,
985 						  &j->new_plane_state->uapi);
986 			goto out_free;
987 		}
988 
989 		num_pipes++;
990 	}
991 
992 	intel_frontbuffer_flush(to_intel_frontbuffer(joined_pipe_state[0].new_plane_state->hw.fb),
993 				ORIGIN_CURSOR_UPDATE);
994 
995 	for (int i = 0; i < num_pipes; i++) {
996 		struct intel_frontbuffer *old_front =
997 			to_intel_frontbuffer(joined_pipe_state[i].old_plane_state->hw.fb);
998 		struct intel_frontbuffer *new_front =
999 			to_intel_frontbuffer(joined_pipe_state[i].new_plane_state->hw.fb);
1000 
1001 		intel_frontbuffer_track(old_front, new_front,
1002 					joined_pipe_state[i].plane->frontbuffer_bit);
1003 	}
1004 
1005 	for (int i = 0; i < num_pipes; i++) {
1006 		joined_pipe_state[i].plane->base.state =
1007 			&joined_pipe_state[i].new_plane_state->uapi;
1008 
1009 		if (joined_pipe_state[i].new_plane_state->uapi.visible)
1010 			joined_pipe_state[i].crtc_state->active_planes |= BIT(PLANE_CURSOR);
1011 		else
1012 			joined_pipe_state[i].crtc_state->active_planes &= ~BIT(PLANE_CURSOR);
1013 	}
1014 
1015 	/*
1016 	 * We cannot swap crtc_state as it may be in use by an atomic commit or
1017 	 * page flip that's running simultaneously. If we swap crtc_state and
1018 	 * destroy the old state, we will cause a use-after-free there.
1019 	 *
1020 	 * Only update active_planes, which is needed for our internal
1021 	 * bookkeeping. Either value will do the right thing when updating
1022 	 * planes atomically. If the cursor was part of the atomic update then
1023 	 * we would have taken the slowpath.
1024 	 */
1025 
1026 	intel_vblank_evade_init(crtc_state, crtc_state, &evade);
1027 
1028 	intel_psr_lock(crtc_state);
1029 
1030 	if (!drm_WARN_ON(display->drm, drm_crtc_vblank_get(&crtc->base))) {
1031 		/*
1032 		 * TODO: maybe check if we're still in PSR
1033 		 * and skip the vblank evasion entirely?
1034 		 */
1035 		intel_psr_wait_for_idle_locked(crtc_state);
1036 
1037 		local_irq_disable();
1038 
1039 		intel_vblank_evade(&evade);
1040 
1041 		drm_crtc_vblank_put(&crtc->base);
1042 	} else {
1043 		local_irq_disable();
1044 	}
1045 
1046 	/*
1047 	 * Joiner pipes are vblank-synchronized, so sampling only the primary
1048 	 * pipe is sufficient to detect a straddle across all joined pipes.
1049 	 * The vblank evasion above also operates on the primary pipe only.
1050 	 */
1051 	start_vbl_count = intel_crtc_get_vblank_counter(joined_pipe_state[0].crtc);
1052 
1053 	for (int i = 0; i < num_pipes; i++) {
1054 		if (joined_pipe_state[i].new_plane_state->uapi.visible) {
1055 			intel_plane_update_noarm(NULL, joined_pipe_state[i].plane,
1056 						 joined_pipe_state[i].crtc_state,
1057 						 joined_pipe_state[i].new_plane_state);
1058 			intel_plane_update_arm(NULL, joined_pipe_state[i].plane,
1059 					       joined_pipe_state[i].crtc_state,
1060 					       joined_pipe_state[i].new_plane_state);
1061 		} else {
1062 			intel_plane_disable_arm(NULL, joined_pipe_state[i].plane,
1063 						joined_pipe_state[i].crtc_state);
1064 		}
1065 	}
1066 
1067 	end_vbl_count = intel_crtc_get_vblank_counter(joined_pipe_state[0].crtc);
1068 
1069 	local_irq_enable();
1070 
1071 	if (start_vbl_count != end_vbl_count)
1072 		drm_err(display->drm,
1073 			"Atomic update failure on pipe %c (start=%u end=%u)\n",
1074 			pipe_name(joined_pipe_state[0].crtc->pipe),
1075 			start_vbl_count, end_vbl_count);
1076 
1077 	intel_psr_unlock(crtc_state);
1078 
1079 	/*
1080 	 * Schedule or immediately unpin old framebuffers.
1081 	 * Protect against concurrent access.
1082 	 */
1083 	for (int i = 0; i < num_pipes; i++) {
1084 		struct intel_plane_state *old = joined_pipe_state[i].old_plane_state;
1085 
1086 		if (old->ggtt_vma != joined_pipe_state[i].new_plane_state->ggtt_vma) {
1087 			drm_vblank_work_init(&old->unpin_work, &crtc->base,
1088 					     intel_cursor_unpin_work);
1089 			drm_vblank_work_schedule(&old->unpin_work,
1090 						 drm_crtc_accurate_vblank_count(&crtc->base) + 1,
1091 						 false);
1092 			joined_pipe_state[i].old_plane_state = NULL;
1093 		} else {
1094 			intel_plane_unpin_fb(old);
1095 		}
1096 	}
1097 
1098 out_free:
1099 	if (ret) {
1100 		for (int i = 0; i < num_pipes; i++) {
1101 			intel_plane_unpin_fb(joined_pipe_state[i].new_plane_state);
1102 			intel_plane_destroy_state(&joined_pipe_state[i].plane->base,
1103 						  &joined_pipe_state[i].new_plane_state->uapi);
1104 		}
1105 	} else {
1106 		for (int i = 0; i < num_pipes; i++) {
1107 			if (!joined_pipe_state[i].old_plane_state)
1108 				continue;
1109 
1110 			intel_plane_destroy_state(&joined_pipe_state[i].plane->base,
1111 						  &joined_pipe_state[i].old_plane_state->uapi);
1112 		}
1113 	}
1114 	return ret;
1115 
1116 slow:
1117 	return drm_atomic_helper_update_plane(&plane->base, &crtc->base, fb,
1118 					      crtc_x, crtc_y, crtc_w, crtc_h,
1119 					      src_x, src_y, src_w, src_h, ctx);
1120 }
1121 
1122 static const struct drm_plane_funcs intel_cursor_plane_funcs = {
1123 	.update_plane = intel_legacy_cursor_update,
1124 	.disable_plane = drm_atomic_helper_disable_plane,
1125 	.destroy = intel_plane_destroy,
1126 	.atomic_duplicate_state = intel_plane_duplicate_state,
1127 	.atomic_destroy_state = intel_plane_destroy_state,
1128 	.format_mod_supported = intel_cursor_format_mod_supported,
1129 	.format_mod_supported_async = intel_plane_format_mod_supported_async,
1130 };
1131 
1132 static void intel_cursor_add_size_hints_property(struct intel_plane *plane)
1133 {
1134 	struct intel_display *display = to_intel_display(plane);
1135 	const struct drm_mode_config *config = &display->drm->mode_config;
1136 	struct drm_plane_size_hint hints[4];
1137 	int size, max_size, num_hints = 0;
1138 
1139 	max_size = min(config->cursor_width, config->cursor_height);
1140 
1141 	/* for simplicity only enumerate the supported square+POT sizes */
1142 	for (size = 64; size <= max_size; size *= 2) {
1143 		if (drm_WARN_ON(display->drm, num_hints >= ARRAY_SIZE(hints)))
1144 			break;
1145 
1146 		hints[num_hints].width = size;
1147 		hints[num_hints].height = size;
1148 		num_hints++;
1149 	}
1150 
1151 	drm_plane_add_size_hints_property(&plane->base, hints, num_hints);
1152 }
1153 
1154 struct intel_plane *
1155 intel_cursor_plane_create(struct intel_display *display,
1156 			  enum pipe pipe)
1157 {
1158 	struct intel_plane *cursor;
1159 	int ret, zpos;
1160 	u64 *modifiers;
1161 
1162 	cursor = intel_plane_alloc();
1163 	if (IS_ERR(cursor))
1164 		return cursor;
1165 
1166 	cursor->pipe = pipe;
1167 	cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
1168 	cursor->id = PLANE_CURSOR;
1169 	cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id);
1170 
1171 	if (display->platform.i845g || display->platform.i865g) {
1172 		cursor->max_stride = i845_cursor_max_stride;
1173 		cursor->min_alignment = i845_cursor_min_alignment;
1174 		cursor->update_arm = i845_cursor_update_arm;
1175 		cursor->disable_arm = i845_cursor_disable_arm;
1176 		cursor->get_hw_state = i845_cursor_get_hw_state;
1177 		cursor->check_plane = i845_check_cursor;
1178 	} else {
1179 		cursor->max_stride = i9xx_cursor_max_stride;
1180 
1181 		if (display->platform.i830)
1182 			cursor->min_alignment = i830_cursor_min_alignment;
1183 		else if (display->platform.i85x)
1184 			cursor->min_alignment = i85x_cursor_min_alignment;
1185 		else
1186 			cursor->min_alignment = i9xx_cursor_min_alignment;
1187 
1188 		if (intel_scanout_needs_vtd_wa(display))
1189 			cursor->vtd_guard = 2;
1190 
1191 		cursor->update_arm = i9xx_cursor_update_arm;
1192 		cursor->disable_arm = i9xx_cursor_disable_arm;
1193 		cursor->get_hw_state = i9xx_cursor_get_hw_state;
1194 		cursor->check_plane = i9xx_check_cursor;
1195 	}
1196 
1197 	cursor->surf_offset = intel_cursor_surf_offset;
1198 
1199 	if (DISPLAY_VER(display) >= 5 || display->platform.g4x)
1200 		cursor->capture_error = g4x_cursor_capture_error;
1201 	else
1202 		cursor->capture_error = i9xx_cursor_capture_error;
1203 
1204 	cursor->cursor.base = ~0;
1205 	cursor->cursor.cntl = ~0;
1206 
1207 	if (display->platform.i845g || display->platform.i865g || HAS_CUR_FBC(display))
1208 		cursor->cursor.size = ~0;
1209 
1210 	modifiers = intel_fb_plane_get_modifiers(display, INTEL_PLANE_CAP_NONE);
1211 
1212 	ret = drm_universal_plane_init(display->drm, &cursor->base,
1213 				       0, &intel_cursor_plane_funcs,
1214 				       intel_cursor_formats,
1215 				       ARRAY_SIZE(intel_cursor_formats),
1216 				       modifiers,
1217 				       DRM_PLANE_TYPE_CURSOR,
1218 				       "cursor %c", pipe_name(pipe));
1219 
1220 	kfree(modifiers);
1221 
1222 	if (ret)
1223 		goto fail;
1224 
1225 	if (DISPLAY_VER(display) >= 4)
1226 		drm_plane_create_rotation_property(&cursor->base,
1227 						   DRM_MODE_ROTATE_0,
1228 						   DRM_MODE_ROTATE_0 |
1229 						   DRM_MODE_ROTATE_180);
1230 
1231 	intel_cursor_add_size_hints_property(cursor);
1232 
1233 	drm_plane_create_blend_mode_property(&cursor->base,
1234 					     BIT(DRM_MODE_BLEND_PREMULTI));
1235 
1236 	zpos = DISPLAY_RUNTIME_INFO(display)->num_sprites[pipe] + 1;
1237 	drm_plane_create_zpos_immutable_property(&cursor->base, zpos);
1238 
1239 	if (DISPLAY_VER(display) >= 12)
1240 		drm_plane_enable_fb_damage_clips(&cursor->base);
1241 
1242 	intel_plane_helper_add(cursor);
1243 
1244 	return cursor;
1245 
1246 fail:
1247 	intel_plane_free(cursor);
1248 
1249 	return ERR_PTR(ret);
1250 }
1251 
1252 void intel_cursor_mode_config_init(struct intel_display *display)
1253 {
1254 	struct drm_mode_config *mode_config = &display->drm->mode_config;
1255 
1256 	if (display->platform.i845g) {
1257 		mode_config->cursor_width = 64;
1258 		mode_config->cursor_height = 1023;
1259 	} else if (display->platform.i865g) {
1260 		mode_config->cursor_width = 512;
1261 		mode_config->cursor_height = 1023;
1262 	} else if (display->platform.i830 || display->platform.i85x ||
1263 		   display->platform.i915g || display->platform.i915gm) {
1264 		mode_config->cursor_width = 64;
1265 		mode_config->cursor_height = 64;
1266 	} else {
1267 		mode_config->cursor_width = 256;
1268 		mode_config->cursor_height = 256;
1269 	}
1270 }
1271