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
intel_cursor_base(const struct intel_plane_state * plane_state)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
intel_cursor_position(const struct intel_crtc_state * crtc_state,const struct intel_plane_state * plane_state,bool early_tpt)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
intel_cursor_size_ok(const struct intel_plane_state * plane_state)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
intel_cursor_check_surface(struct intel_plane_state * plane_state)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
intel_check_cursor(struct intel_crtc_state * crtc_state,struct intel_plane_state * plane_state)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
i845_cursor_max_stride(struct intel_plane * plane,u32 pixel_format,u64 modifier,unsigned int rotation)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
i845_cursor_min_alignment(struct intel_plane * plane,const struct drm_framebuffer * fb,int color_plane)197 static unsigned int i845_cursor_min_alignment(struct intel_plane *plane,
198 const struct drm_framebuffer *fb,
199 int color_plane)
200 {
201 return 32;
202 }
203
i845_cursor_ctl_crtc(const struct intel_crtc_state * crtc_state)204 static u32 i845_cursor_ctl_crtc(const struct intel_crtc_state *crtc_state)
205 {
206 u32 cntl = 0;
207
208 if (crtc_state->gamma_enable)
209 cntl |= CURSOR_PIPE_GAMMA_ENABLE;
210
211 return cntl;
212 }
213
i845_cursor_ctl(const struct intel_crtc_state * crtc_state,const struct intel_plane_state * plane_state)214 static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
215 const struct intel_plane_state *plane_state)
216 {
217 return CURSOR_ENABLE |
218 CURSOR_FORMAT_ARGB |
219 CURSOR_STRIDE(plane_state->view.color_plane[0].mapping_stride);
220 }
221
i845_cursor_size_ok(const struct intel_plane_state * plane_state)222 static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
223 {
224 int width = drm_rect_width(&plane_state->uapi.dst);
225
226 /*
227 * 845g/865g are only limited by the width of their cursors,
228 * the height is arbitrary up to the precision of the register.
229 */
230 return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
231 }
232
i845_check_cursor(struct intel_crtc_state * crtc_state,struct intel_plane_state * plane_state)233 static int i845_check_cursor(struct intel_crtc_state *crtc_state,
234 struct intel_plane_state *plane_state)
235 {
236 const struct drm_framebuffer *fb = plane_state->hw.fb;
237 struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
238 int ret;
239
240 ret = intel_check_cursor(crtc_state, plane_state);
241 if (ret)
242 return ret;
243
244 /* if we want to turn off the cursor ignore width and height */
245 if (!fb)
246 return 0;
247
248 /* Check for which cursor types we support */
249 if (!i845_cursor_size_ok(plane_state)) {
250 drm_dbg_kms(&i915->drm,
251 "Cursor dimension %dx%d not supported\n",
252 drm_rect_width(&plane_state->uapi.dst),
253 drm_rect_height(&plane_state->uapi.dst));
254 return -EINVAL;
255 }
256
257 drm_WARN_ON(&i915->drm, plane_state->uapi.visible &&
258 plane_state->view.color_plane[0].mapping_stride != fb->pitches[0]);
259
260 switch (fb->pitches[0]) {
261 case 256:
262 case 512:
263 case 1024:
264 case 2048:
265 break;
266 default:
267 drm_dbg_kms(&i915->drm, "Invalid cursor stride (%u)\n",
268 fb->pitches[0]);
269 return -EINVAL;
270 }
271
272 plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
273
274 return 0;
275 }
276
277 /* TODO: split into noarm+arm pair */
i845_cursor_update_arm(struct intel_dsb * dsb,struct intel_plane * plane,const struct intel_crtc_state * crtc_state,const struct intel_plane_state * plane_state)278 static void i845_cursor_update_arm(struct intel_dsb *dsb,
279 struct intel_plane *plane,
280 const struct intel_crtc_state *crtc_state,
281 const struct intel_plane_state *plane_state)
282 {
283 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
284 u32 cntl = 0, base = 0, pos = 0, size = 0;
285
286 if (plane_state && plane_state->uapi.visible) {
287 unsigned int width = drm_rect_width(&plane_state->uapi.dst);
288 unsigned int height = drm_rect_height(&plane_state->uapi.dst);
289
290 cntl = plane_state->ctl |
291 i845_cursor_ctl_crtc(crtc_state);
292
293 size = CURSOR_HEIGHT(height) | CURSOR_WIDTH(width);
294
295 base = intel_cursor_base(plane_state);
296 pos = intel_cursor_position(crtc_state, plane_state, false);
297 }
298
299 /* On these chipsets we can only modify the base/size/stride
300 * whilst the cursor is disabled.
301 */
302 if (plane->cursor.base != base ||
303 plane->cursor.size != size ||
304 plane->cursor.cntl != cntl) {
305 intel_de_write_fw(dev_priv, CURCNTR(dev_priv, PIPE_A), 0);
306 intel_de_write_fw(dev_priv, CURBASE(dev_priv, PIPE_A), base);
307 intel_de_write_fw(dev_priv, CURSIZE(dev_priv, PIPE_A), size);
308 intel_de_write_fw(dev_priv, CURPOS(dev_priv, PIPE_A), pos);
309 intel_de_write_fw(dev_priv, CURCNTR(dev_priv, PIPE_A), cntl);
310
311 plane->cursor.base = base;
312 plane->cursor.size = size;
313 plane->cursor.cntl = cntl;
314 } else {
315 intel_de_write_fw(dev_priv, CURPOS(dev_priv, PIPE_A), pos);
316 }
317 }
318
i845_cursor_disable_arm(struct intel_dsb * dsb,struct intel_plane * plane,const struct intel_crtc_state * crtc_state)319 static void i845_cursor_disable_arm(struct intel_dsb *dsb,
320 struct intel_plane *plane,
321 const struct intel_crtc_state *crtc_state)
322 {
323 i845_cursor_update_arm(dsb, plane, crtc_state, NULL);
324 }
325
i845_cursor_get_hw_state(struct intel_plane * plane,enum pipe * pipe)326 static bool i845_cursor_get_hw_state(struct intel_plane *plane,
327 enum pipe *pipe)
328 {
329 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
330 enum intel_display_power_domain power_domain;
331 intel_wakeref_t wakeref;
332 bool ret;
333
334 power_domain = POWER_DOMAIN_PIPE(PIPE_A);
335 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
336 if (!wakeref)
337 return false;
338
339 ret = intel_de_read(dev_priv, CURCNTR(dev_priv, PIPE_A)) & CURSOR_ENABLE;
340
341 *pipe = PIPE_A;
342
343 intel_display_power_put(dev_priv, power_domain, wakeref);
344
345 return ret;
346 }
347
348 static unsigned int
i9xx_cursor_max_stride(struct intel_plane * plane,u32 pixel_format,u64 modifier,unsigned int rotation)349 i9xx_cursor_max_stride(struct intel_plane *plane,
350 u32 pixel_format, u64 modifier,
351 unsigned int rotation)
352 {
353 return plane->base.dev->mode_config.cursor_width * 4;
354 }
355
i830_cursor_min_alignment(struct intel_plane * plane,const struct drm_framebuffer * fb,int color_plane)356 static unsigned int i830_cursor_min_alignment(struct intel_plane *plane,
357 const struct drm_framebuffer *fb,
358 int color_plane)
359 {
360 /* "AlmadorM Errata – Requires 32-bpp cursor data to be 16KB aligned." */
361 return 16 * 1024; /* physical */
362 }
363
i85x_cursor_min_alignment(struct intel_plane * plane,const struct drm_framebuffer * fb,int color_plane)364 static unsigned int i85x_cursor_min_alignment(struct intel_plane *plane,
365 const struct drm_framebuffer *fb,
366 int color_plane)
367 {
368 return 256; /* physical */
369 }
370
i9xx_cursor_min_alignment(struct intel_plane * plane,const struct drm_framebuffer * fb,int color_plane)371 static unsigned int i9xx_cursor_min_alignment(struct intel_plane *plane,
372 const struct drm_framebuffer *fb,
373 int color_plane)
374 {
375 return 4 * 1024; /* physical for i915/i945 */
376 }
377
i9xx_cursor_ctl_crtc(const struct intel_crtc_state * crtc_state)378 static u32 i9xx_cursor_ctl_crtc(const struct intel_crtc_state *crtc_state)
379 {
380 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
381 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
382 u32 cntl = 0;
383
384 if (DISPLAY_VER(dev_priv) >= 11)
385 return cntl;
386
387 if (crtc_state->gamma_enable)
388 cntl = MCURSOR_PIPE_GAMMA_ENABLE;
389
390 if (crtc_state->csc_enable)
391 cntl |= MCURSOR_PIPE_CSC_ENABLE;
392
393 if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv))
394 cntl |= MCURSOR_PIPE_SEL(crtc->pipe);
395
396 return cntl;
397 }
398
i9xx_cursor_ctl(const struct intel_crtc_state * crtc_state,const struct intel_plane_state * plane_state)399 static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
400 const struct intel_plane_state *plane_state)
401 {
402 struct drm_i915_private *dev_priv =
403 to_i915(plane_state->uapi.plane->dev);
404 u32 cntl = 0;
405
406 if (IS_SANDYBRIDGE(dev_priv) || IS_IVYBRIDGE(dev_priv))
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 (DISPLAY_VER(dev_priv) == 13)
429 cntl |= MCURSOR_ARB_SLOTS(1);
430
431 return cntl;
432 }
433
i9xx_cursor_size_ok(const struct intel_plane_state * plane_state)434 static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
435 {
436 struct drm_i915_private *dev_priv =
437 to_i915(plane_state->uapi.plane->dev);
438 int width = drm_rect_width(&plane_state->uapi.dst);
439 int height = drm_rect_height(&plane_state->uapi.dst);
440
441 if (!intel_cursor_size_ok(plane_state))
442 return false;
443
444 /* Cursor width is limited to a few power-of-two sizes */
445 switch (width) {
446 case 256:
447 case 128:
448 case 64:
449 break;
450 default:
451 return false;
452 }
453
454 /*
455 * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
456 * height from 8 lines up to the cursor width, when the
457 * cursor is not rotated. Everything else requires square
458 * cursors.
459 */
460 if (HAS_CUR_FBC(dev_priv) &&
461 plane_state->hw.rotation & DRM_MODE_ROTATE_0) {
462 if (height < 8 || height > width)
463 return false;
464 } else {
465 if (height != width)
466 return false;
467 }
468
469 return true;
470 }
471
i9xx_check_cursor(struct intel_crtc_state * crtc_state,struct intel_plane_state * plane_state)472 static int i9xx_check_cursor(struct intel_crtc_state *crtc_state,
473 struct intel_plane_state *plane_state)
474 {
475 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
476 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
477 const struct drm_framebuffer *fb = plane_state->hw.fb;
478 enum pipe pipe = plane->pipe;
479 int ret;
480
481 ret = intel_check_cursor(crtc_state, plane_state);
482 if (ret)
483 return ret;
484
485 /* if we want to turn off the cursor ignore width and height */
486 if (!fb)
487 return 0;
488
489 /* Check for which cursor types we support */
490 if (!i9xx_cursor_size_ok(plane_state)) {
491 drm_dbg(&dev_priv->drm,
492 "Cursor dimension %dx%d not supported\n",
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(&dev_priv->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(&dev_priv->drm,
504 "Invalid cursor stride (%u) (cursor width %d)\n",
505 fb->pitches[0],
506 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 (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
521 plane_state->uapi.visible && plane_state->uapi.dst.x1 < 0) {
522 drm_dbg_kms(&dev_priv->drm,
523 "CHV cursor C not allowed to straddle the left screen edge\n");
524 return -EINVAL;
525 }
526
527 plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
528
529 return 0;
530 }
531
i9xx_cursor_disable_sel_fetch_arm(struct intel_dsb * dsb,struct intel_plane * plane,const struct intel_crtc_state * crtc_state)532 static void i9xx_cursor_disable_sel_fetch_arm(struct intel_dsb *dsb,
533 struct intel_plane *plane,
534 const struct intel_crtc_state *crtc_state)
535 {
536 struct intel_display *display = to_intel_display(plane->base.dev);
537 enum pipe pipe = plane->pipe;
538
539 if (!crtc_state->enable_psr2_sel_fetch)
540 return;
541
542 intel_de_write_dsb(display, dsb, SEL_FETCH_CUR_CTL(pipe), 0);
543 }
544
wa_16021440873(struct intel_dsb * dsb,struct intel_plane * plane,const struct intel_crtc_state * crtc_state,const struct intel_plane_state * plane_state)545 static void wa_16021440873(struct intel_dsb *dsb,
546 struct intel_plane *plane,
547 const struct intel_crtc_state *crtc_state,
548 const struct intel_plane_state *plane_state)
549 {
550 struct intel_display *display = to_intel_display(plane->base.dev);
551 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
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(dev_priv, pipe),
562 CURSOR_POS_Y(et_y_position));
563 }
564
i9xx_cursor_update_sel_fetch_arm(struct intel_dsb * dsb,struct intel_plane * plane,const struct intel_crtc_state * crtc_state,const struct intel_plane_state * plane_state)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->base.dev);
571 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
572 enum pipe pipe = plane->pipe;
573
574 if (!crtc_state->enable_psr2_sel_fetch)
575 return;
576
577 if (drm_rect_height(&plane_state->psr2_sel_fetch_area) > 0) {
578 if (crtc_state->enable_psr2_su_region_et) {
579 u32 val = intel_cursor_position(crtc_state, plane_state,
580 true);
581
582 intel_de_write_dsb(display, dsb, CURPOS_ERLY_TPT(dev_priv, pipe), val);
583 }
584
585 intel_de_write_dsb(display, dsb, SEL_FETCH_CUR_CTL(pipe), plane_state->ctl);
586 } else {
587 /* Wa_16021440873 */
588 if (crtc_state->enable_psr2_su_region_et)
589 wa_16021440873(dsb, plane, crtc_state, plane_state);
590 else
591 i9xx_cursor_disable_sel_fetch_arm(dsb, plane, crtc_state);
592 }
593 }
594
skl_cursor_ddb_reg_val(const struct skl_ddb_entry * entry)595 static u32 skl_cursor_ddb_reg_val(const struct skl_ddb_entry *entry)
596 {
597 if (!entry->end)
598 return 0;
599
600 return CUR_BUF_END(entry->end - 1) |
601 CUR_BUF_START(entry->start);
602 }
603
skl_cursor_wm_reg_val(const struct skl_wm_level * level)604 static u32 skl_cursor_wm_reg_val(const struct skl_wm_level *level)
605 {
606 u32 val = 0;
607
608 if (level->enable)
609 val |= CUR_WM_EN;
610 if (level->ignore_lines)
611 val |= CUR_WM_IGNORE_LINES;
612 val |= REG_FIELD_PREP(CUR_WM_BLOCKS_MASK, level->blocks);
613 val |= REG_FIELD_PREP(CUR_WM_LINES_MASK, level->lines);
614
615 return val;
616 }
617
skl_write_cursor_wm(struct intel_dsb * dsb,struct intel_plane * plane,const struct intel_crtc_state * crtc_state)618 static void skl_write_cursor_wm(struct intel_dsb *dsb,
619 struct intel_plane *plane,
620 const struct intel_crtc_state *crtc_state)
621 {
622 struct intel_display *display = to_intel_display(plane->base.dev);
623 struct drm_i915_private *i915 = to_i915(plane->base.dev);
624 enum plane_id plane_id = plane->id;
625 enum pipe pipe = plane->pipe;
626 const struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
627 const struct skl_ddb_entry *ddb =
628 &crtc_state->wm.skl.plane_ddb[plane_id];
629 int level;
630
631 for (level = 0; level < i915->display.wm.num_levels; level++)
632 intel_de_write_dsb(display, dsb, CUR_WM(pipe, level),
633 skl_cursor_wm_reg_val(skl_plane_wm_level(pipe_wm, plane_id, level)));
634
635 intel_de_write_dsb(display, dsb, CUR_WM_TRANS(pipe),
636 skl_cursor_wm_reg_val(skl_plane_trans_wm(pipe_wm, plane_id)));
637
638 if (HAS_HW_SAGV_WM(i915)) {
639 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
640
641 intel_de_write_dsb(display, dsb, CUR_WM_SAGV(pipe),
642 skl_cursor_wm_reg_val(&wm->sagv.wm0));
643 intel_de_write_dsb(display, dsb, CUR_WM_SAGV_TRANS(pipe),
644 skl_cursor_wm_reg_val(&wm->sagv.trans_wm));
645 }
646
647 intel_de_write_dsb(display, dsb, CUR_BUF_CFG(pipe),
648 skl_cursor_ddb_reg_val(ddb));
649 }
650
651 /* TODO: split into noarm+arm pair */
i9xx_cursor_update_arm(struct intel_dsb * dsb,struct intel_plane * plane,const struct intel_crtc_state * crtc_state,const struct intel_plane_state * plane_state)652 static void i9xx_cursor_update_arm(struct intel_dsb *dsb,
653 struct intel_plane *plane,
654 const struct intel_crtc_state *crtc_state,
655 const struct intel_plane_state *plane_state)
656 {
657 struct intel_display *display = to_intel_display(plane->base.dev);
658 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
659 enum pipe pipe = plane->pipe;
660 u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
661
662 if (plane_state && plane_state->uapi.visible) {
663 int width = drm_rect_width(&plane_state->uapi.dst);
664 int height = drm_rect_height(&plane_state->uapi.dst);
665
666 cntl = plane_state->ctl |
667 i9xx_cursor_ctl_crtc(crtc_state);
668
669 if (width != height)
670 fbc_ctl = CUR_FBC_EN | CUR_FBC_HEIGHT(height - 1);
671
672 base = intel_cursor_base(plane_state);
673 pos = intel_cursor_position(crtc_state, plane_state, false);
674 }
675
676 /*
677 * On some platforms writing CURCNTR first will also
678 * cause CURPOS to be armed by the CURBASE write.
679 * Without the CURCNTR write the CURPOS write would
680 * arm itself. Thus we always update CURCNTR before
681 * CURPOS.
682 *
683 * On other platforms CURPOS always requires the
684 * CURBASE write to arm the update. Additonally
685 * a write to any of the cursor register will cancel
686 * an already armed cursor update. Thus leaving out
687 * the CURBASE write after CURPOS could lead to a
688 * cursor that doesn't appear to move, or even change
689 * shape. Thus we always write CURBASE.
690 *
691 * The other registers are armed by the CURBASE write
692 * except when the plane is getting enabled at which time
693 * the CURCNTR write arms the update.
694 */
695
696 if (DISPLAY_VER(dev_priv) >= 9)
697 skl_write_cursor_wm(dsb, plane, crtc_state);
698
699 if (plane_state)
700 i9xx_cursor_update_sel_fetch_arm(dsb, plane, crtc_state, plane_state);
701 else
702 i9xx_cursor_disable_sel_fetch_arm(dsb, plane, crtc_state);
703
704 if (plane->cursor.base != base ||
705 plane->cursor.size != fbc_ctl ||
706 plane->cursor.cntl != cntl) {
707 if (HAS_CUR_FBC(dev_priv))
708 intel_de_write_dsb(display, dsb, CUR_FBC_CTL(dev_priv, pipe), fbc_ctl);
709 intel_de_write_dsb(display, dsb, CURCNTR(dev_priv, pipe), cntl);
710 intel_de_write_dsb(display, dsb, CURPOS(dev_priv, pipe), pos);
711 intel_de_write_dsb(display, dsb, CURBASE(dev_priv, pipe), base);
712
713 plane->cursor.base = base;
714 plane->cursor.size = fbc_ctl;
715 plane->cursor.cntl = cntl;
716 } else {
717 intel_de_write_dsb(display, dsb, CURPOS(dev_priv, pipe), pos);
718 intel_de_write_dsb(display, dsb, CURBASE(dev_priv, pipe), base);
719 }
720 }
721
i9xx_cursor_disable_arm(struct intel_dsb * dsb,struct intel_plane * plane,const struct intel_crtc_state * crtc_state)722 static void i9xx_cursor_disable_arm(struct intel_dsb *dsb,
723 struct intel_plane *plane,
724 const struct intel_crtc_state *crtc_state)
725 {
726 i9xx_cursor_update_arm(dsb, plane, crtc_state, NULL);
727 }
728
i9xx_cursor_get_hw_state(struct intel_plane * plane,enum pipe * pipe)729 static bool i9xx_cursor_get_hw_state(struct intel_plane *plane,
730 enum pipe *pipe)
731 {
732 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
733 enum intel_display_power_domain power_domain;
734 intel_wakeref_t wakeref;
735 bool ret;
736 u32 val;
737
738 /*
739 * Not 100% correct for planes that can move between pipes,
740 * but that's only the case for gen2-3 which don't have any
741 * display power wells.
742 */
743 power_domain = POWER_DOMAIN_PIPE(plane->pipe);
744 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
745 if (!wakeref)
746 return false;
747
748 val = intel_de_read(dev_priv, CURCNTR(dev_priv, plane->pipe));
749
750 ret = val & MCURSOR_MODE_MASK;
751
752 if (DISPLAY_VER(dev_priv) >= 5 || IS_G4X(dev_priv))
753 *pipe = plane->pipe;
754 else
755 *pipe = REG_FIELD_GET(MCURSOR_PIPE_SEL_MASK, val);
756
757 intel_display_power_put(dev_priv, power_domain, wakeref);
758
759 return ret;
760 }
761
intel_cursor_format_mod_supported(struct drm_plane * _plane,u32 format,u64 modifier)762 static bool intel_cursor_format_mod_supported(struct drm_plane *_plane,
763 u32 format, u64 modifier)
764 {
765 if (!intel_fb_plane_supports_modifier(to_intel_plane(_plane), modifier))
766 return false;
767
768 return format == DRM_FORMAT_ARGB8888;
769 }
770
intel_cursor_unpin_work(struct kthread_work * base)771 void intel_cursor_unpin_work(struct kthread_work *base)
772 {
773 struct drm_vblank_work *work = to_drm_vblank_work(base);
774 struct intel_plane_state *plane_state =
775 container_of(work, typeof(*plane_state), unpin_work);
776 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
777
778 intel_plane_unpin_fb(plane_state);
779 intel_plane_destroy_state(&plane->base, &plane_state->uapi);
780 }
781
782 static int
intel_legacy_cursor_update(struct drm_plane * _plane,struct drm_crtc * _crtc,struct drm_framebuffer * fb,int crtc_x,int crtc_y,unsigned int crtc_w,unsigned int crtc_h,u32 src_x,u32 src_y,u32 src_w,u32 src_h,struct drm_modeset_acquire_ctx * ctx)783 intel_legacy_cursor_update(struct drm_plane *_plane,
784 struct drm_crtc *_crtc,
785 struct drm_framebuffer *fb,
786 int crtc_x, int crtc_y,
787 unsigned int crtc_w, unsigned int crtc_h,
788 u32 src_x, u32 src_y,
789 u32 src_w, u32 src_h,
790 struct drm_modeset_acquire_ctx *ctx)
791 {
792 struct intel_plane *plane = to_intel_plane(_plane);
793 struct intel_crtc *crtc = to_intel_crtc(_crtc);
794 struct drm_i915_private *i915 = to_i915(plane->base.dev);
795 struct intel_plane_state *old_plane_state =
796 to_intel_plane_state(plane->base.state);
797 struct intel_plane_state *new_plane_state;
798 struct intel_crtc_state *crtc_state =
799 to_intel_crtc_state(crtc->base.state);
800 struct intel_crtc_state *new_crtc_state;
801 struct intel_vblank_evade_ctx evade;
802 int ret;
803
804 /*
805 * When crtc is inactive or there is a modeset pending,
806 * wait for it to complete in the slowpath.
807 * PSR2 selective fetch also requires the slow path as
808 * PSR2 plane and transcoder registers can only be updated during
809 * vblank.
810 *
811 * FIXME joiner fastpath would be good
812 */
813 if (!crtc_state->hw.active ||
814 intel_crtc_needs_modeset(crtc_state) ||
815 intel_crtc_needs_fastset(crtc_state) ||
816 crtc_state->joiner_pipes)
817 goto slow;
818
819 /*
820 * Don't do an async update if there is an outstanding commit modifying
821 * the plane. This prevents our async update's changes from getting
822 * overridden by a previous synchronous update's state.
823 */
824 if (old_plane_state->uapi.commit &&
825 !try_wait_for_completion(&old_plane_state->uapi.commit->hw_done))
826 goto slow;
827
828 /*
829 * If any parameters change that may affect watermarks,
830 * take the slowpath. Only changing fb or position should be
831 * in the fastpath.
832 */
833 if (old_plane_state->uapi.crtc != &crtc->base ||
834 old_plane_state->uapi.src_w != src_w ||
835 old_plane_state->uapi.src_h != src_h ||
836 old_plane_state->uapi.crtc_w != crtc_w ||
837 old_plane_state->uapi.crtc_h != crtc_h ||
838 !old_plane_state->uapi.fb != !fb)
839 goto slow;
840
841 new_plane_state = to_intel_plane_state(intel_plane_duplicate_state(&plane->base));
842 if (!new_plane_state)
843 return -ENOMEM;
844
845 new_crtc_state = to_intel_crtc_state(intel_crtc_duplicate_state(&crtc->base));
846 if (!new_crtc_state) {
847 ret = -ENOMEM;
848 goto out_free;
849 }
850
851 drm_atomic_set_fb_for_plane(&new_plane_state->uapi, fb);
852
853 new_plane_state->uapi.src_x = src_x;
854 new_plane_state->uapi.src_y = src_y;
855 new_plane_state->uapi.src_w = src_w;
856 new_plane_state->uapi.src_h = src_h;
857 new_plane_state->uapi.crtc_x = crtc_x;
858 new_plane_state->uapi.crtc_y = crtc_y;
859 new_plane_state->uapi.crtc_w = crtc_w;
860 new_plane_state->uapi.crtc_h = crtc_h;
861
862 intel_plane_copy_uapi_to_hw_state(new_plane_state, new_plane_state, crtc);
863
864 ret = intel_plane_atomic_check_with_state(crtc_state, new_crtc_state,
865 old_plane_state, new_plane_state);
866 if (ret)
867 goto out_free;
868
869 ret = intel_plane_pin_fb(new_plane_state);
870 if (ret)
871 goto out_free;
872
873 intel_frontbuffer_flush(to_intel_frontbuffer(new_plane_state->hw.fb),
874 ORIGIN_CURSOR_UPDATE);
875 intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->hw.fb),
876 to_intel_frontbuffer(new_plane_state->hw.fb),
877 plane->frontbuffer_bit);
878
879 /* Swap plane state */
880 plane->base.state = &new_plane_state->uapi;
881
882 /*
883 * We cannot swap crtc_state as it may be in use by an atomic commit or
884 * page flip that's running simultaneously. If we swap crtc_state and
885 * destroy the old state, we will cause a use-after-free there.
886 *
887 * Only update active_planes, which is needed for our internal
888 * bookkeeping. Either value will do the right thing when updating
889 * planes atomically. If the cursor was part of the atomic update then
890 * we would have taken the slowpath.
891 */
892 crtc_state->active_planes = new_crtc_state->active_planes;
893
894 intel_vblank_evade_init(crtc_state, crtc_state, &evade);
895
896 intel_psr_lock(crtc_state);
897
898 if (!drm_WARN_ON(&i915->drm, drm_crtc_vblank_get(&crtc->base))) {
899 /*
900 * TODO: maybe check if we're still in PSR
901 * and skip the vblank evasion entirely?
902 */
903 intel_psr_wait_for_idle_locked(crtc_state);
904
905 local_irq_disable();
906
907 intel_vblank_evade(&evade);
908
909 drm_crtc_vblank_put(&crtc->base);
910 } else {
911 local_irq_disable();
912 }
913
914 if (new_plane_state->uapi.visible) {
915 intel_plane_update_noarm(NULL, plane, crtc_state, new_plane_state);
916 intel_plane_update_arm(NULL, plane, crtc_state, new_plane_state);
917 } else {
918 intel_plane_disable_arm(NULL, plane, crtc_state);
919 }
920
921 local_irq_enable();
922
923 intel_psr_unlock(crtc_state);
924
925 if (old_plane_state->ggtt_vma != new_plane_state->ggtt_vma) {
926 drm_vblank_work_init(&old_plane_state->unpin_work, &crtc->base,
927 intel_cursor_unpin_work);
928
929 drm_vblank_work_schedule(&old_plane_state->unpin_work,
930 drm_crtc_accurate_vblank_count(&crtc->base) + 1,
931 false);
932
933 old_plane_state = NULL;
934 } else {
935 intel_plane_unpin_fb(old_plane_state);
936 }
937
938 out_free:
939 if (new_crtc_state)
940 intel_crtc_destroy_state(&crtc->base, &new_crtc_state->uapi);
941 if (ret)
942 intel_plane_destroy_state(&plane->base, &new_plane_state->uapi);
943 else if (old_plane_state)
944 intel_plane_destroy_state(&plane->base, &old_plane_state->uapi);
945 return ret;
946
947 slow:
948 return drm_atomic_helper_update_plane(&plane->base, &crtc->base, fb,
949 crtc_x, crtc_y, crtc_w, crtc_h,
950 src_x, src_y, src_w, src_h, ctx);
951 }
952
953 static const struct drm_plane_funcs intel_cursor_plane_funcs = {
954 .update_plane = intel_legacy_cursor_update,
955 .disable_plane = drm_atomic_helper_disable_plane,
956 .destroy = intel_plane_destroy,
957 .atomic_duplicate_state = intel_plane_duplicate_state,
958 .atomic_destroy_state = intel_plane_destroy_state,
959 .format_mod_supported = intel_cursor_format_mod_supported,
960 };
961
intel_cursor_add_size_hints_property(struct intel_plane * plane)962 static void intel_cursor_add_size_hints_property(struct intel_plane *plane)
963 {
964 struct drm_i915_private *i915 = to_i915(plane->base.dev);
965 const struct drm_mode_config *config = &i915->drm.mode_config;
966 struct drm_plane_size_hint hints[4];
967 int size, max_size, num_hints = 0;
968
969 max_size = min(config->cursor_width, config->cursor_height);
970
971 /* for simplicity only enumerate the supported square+POT sizes */
972 for (size = 64; size <= max_size; size *= 2) {
973 if (drm_WARN_ON(&i915->drm, num_hints >= ARRAY_SIZE(hints)))
974 break;
975
976 hints[num_hints].width = size;
977 hints[num_hints].height = size;
978 num_hints++;
979 }
980
981 drm_plane_add_size_hints_property(&plane->base, hints, num_hints);
982 }
983
984 struct intel_plane *
intel_cursor_plane_create(struct drm_i915_private * dev_priv,enum pipe pipe)985 intel_cursor_plane_create(struct drm_i915_private *dev_priv,
986 enum pipe pipe)
987 {
988 struct intel_plane *cursor;
989 int ret, zpos;
990 u64 *modifiers;
991
992 cursor = intel_plane_alloc();
993 if (IS_ERR(cursor))
994 return cursor;
995
996 cursor->pipe = pipe;
997 cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
998 cursor->id = PLANE_CURSOR;
999 cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id);
1000
1001 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
1002 cursor->max_stride = i845_cursor_max_stride;
1003 cursor->min_alignment = i845_cursor_min_alignment;
1004 cursor->update_arm = i845_cursor_update_arm;
1005 cursor->disable_arm = i845_cursor_disable_arm;
1006 cursor->get_hw_state = i845_cursor_get_hw_state;
1007 cursor->check_plane = i845_check_cursor;
1008 } else {
1009 cursor->max_stride = i9xx_cursor_max_stride;
1010
1011 if (IS_I830(dev_priv))
1012 cursor->min_alignment = i830_cursor_min_alignment;
1013 else if (IS_I85X(dev_priv))
1014 cursor->min_alignment = i85x_cursor_min_alignment;
1015 else
1016 cursor->min_alignment = i9xx_cursor_min_alignment;
1017
1018 cursor->update_arm = i9xx_cursor_update_arm;
1019 cursor->disable_arm = i9xx_cursor_disable_arm;
1020 cursor->get_hw_state = i9xx_cursor_get_hw_state;
1021 cursor->check_plane = i9xx_check_cursor;
1022 }
1023
1024 cursor->cursor.base = ~0;
1025 cursor->cursor.cntl = ~0;
1026
1027 if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
1028 cursor->cursor.size = ~0;
1029
1030 modifiers = intel_fb_plane_get_modifiers(dev_priv, INTEL_PLANE_CAP_NONE);
1031
1032 ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
1033 0, &intel_cursor_plane_funcs,
1034 intel_cursor_formats,
1035 ARRAY_SIZE(intel_cursor_formats),
1036 modifiers,
1037 DRM_PLANE_TYPE_CURSOR,
1038 "cursor %c", pipe_name(pipe));
1039
1040 kfree(modifiers);
1041
1042 if (ret)
1043 goto fail;
1044
1045 if (DISPLAY_VER(dev_priv) >= 4)
1046 drm_plane_create_rotation_property(&cursor->base,
1047 DRM_MODE_ROTATE_0,
1048 DRM_MODE_ROTATE_0 |
1049 DRM_MODE_ROTATE_180);
1050
1051 intel_cursor_add_size_hints_property(cursor);
1052
1053 zpos = DISPLAY_RUNTIME_INFO(dev_priv)->num_sprites[pipe] + 1;
1054 drm_plane_create_zpos_immutable_property(&cursor->base, zpos);
1055
1056 if (DISPLAY_VER(dev_priv) >= 12)
1057 drm_plane_enable_fb_damage_clips(&cursor->base);
1058
1059 intel_plane_helper_add(cursor);
1060
1061 return cursor;
1062
1063 fail:
1064 intel_plane_free(cursor);
1065
1066 return ERR_PTR(ret);
1067 }
1068