Loading...
1/*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24/**
25 * DOC: atomic plane helpers
26 *
27 * The functions here are used by the atomic plane helper functions to
28 * implement legacy plane updates (i.e., drm_plane->update_plane() and
29 * drm_plane->disable_plane()). This allows plane updates to use the
30 * atomic state infrastructure and perform plane updates as separate
31 * prepare/check/commit/cleanup steps.
32 */
33
34#include <drm/drm_atomic_helper.h>
35#include <drm/drm_fourcc.h>
36
37#include "gt/intel_rps.h"
38
39#include "intel_atomic_plane.h"
40#include "intel_cdclk.h"
41#include "intel_display_trace.h"
42#include "intel_display_types.h"
43#include "intel_fb.h"
44#include "intel_fb_pin.h"
45#include "intel_sprite.h"
46#include "skl_scaler.h"
47#include "skl_watermark.h"
48
49static void intel_plane_state_reset(struct intel_plane_state *plane_state,
50 struct intel_plane *plane)
51{
52 memset(plane_state, 0, sizeof(*plane_state));
53
54 __drm_atomic_helper_plane_state_reset(&plane_state->uapi, &plane->base);
55
56 plane_state->scaler_id = -1;
57}
58
59struct intel_plane *intel_plane_alloc(void)
60{
61 struct intel_plane_state *plane_state;
62 struct intel_plane *plane;
63
64 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
65 if (!plane)
66 return ERR_PTR(-ENOMEM);
67
68 plane_state = kzalloc(sizeof(*plane_state), GFP_KERNEL);
69 if (!plane_state) {
70 kfree(plane);
71 return ERR_PTR(-ENOMEM);
72 }
73
74 intel_plane_state_reset(plane_state, plane);
75
76 plane->base.state = &plane_state->uapi;
77
78 return plane;
79}
80
81void intel_plane_free(struct intel_plane *plane)
82{
83 intel_plane_destroy_state(&plane->base, plane->base.state);
84 kfree(plane);
85}
86
87/**
88 * intel_plane_duplicate_state - duplicate plane state
89 * @plane: drm plane
90 *
91 * Allocates and returns a copy of the plane state (both common and
92 * Intel-specific) for the specified plane.
93 *
94 * Returns: The newly allocated plane state, or NULL on failure.
95 */
96struct drm_plane_state *
97intel_plane_duplicate_state(struct drm_plane *plane)
98{
99 struct intel_plane_state *intel_state;
100
101 intel_state = to_intel_plane_state(plane->state);
102 intel_state = kmemdup(intel_state, sizeof(*intel_state), GFP_KERNEL);
103
104 if (!intel_state)
105 return NULL;
106
107 __drm_atomic_helper_plane_duplicate_state(plane, &intel_state->uapi);
108
109 intel_state->ggtt_vma = NULL;
110 intel_state->dpt_vma = NULL;
111 intel_state->flags = 0;
112
113 /* add reference to fb */
114 if (intel_state->hw.fb)
115 drm_framebuffer_get(intel_state->hw.fb);
116
117 return &intel_state->uapi;
118}
119
120/**
121 * intel_plane_destroy_state - destroy plane state
122 * @plane: drm plane
123 * @state: state object to destroy
124 *
125 * Destroys the plane state (both common and Intel-specific) for the
126 * specified plane.
127 */
128void
129intel_plane_destroy_state(struct drm_plane *plane,
130 struct drm_plane_state *state)
131{
132 struct intel_plane_state *plane_state = to_intel_plane_state(state);
133
134 drm_WARN_ON(plane->dev, plane_state->ggtt_vma);
135 drm_WARN_ON(plane->dev, plane_state->dpt_vma);
136
137 __drm_atomic_helper_plane_destroy_state(&plane_state->uapi);
138 if (plane_state->hw.fb)
139 drm_framebuffer_put(plane_state->hw.fb);
140 kfree(plane_state);
141}
142
143unsigned int intel_adjusted_rate(const struct drm_rect *src,
144 const struct drm_rect *dst,
145 unsigned int rate)
146{
147 unsigned int src_w, src_h, dst_w, dst_h;
148
149 src_w = drm_rect_width(src) >> 16;
150 src_h = drm_rect_height(src) >> 16;
151 dst_w = drm_rect_width(dst);
152 dst_h = drm_rect_height(dst);
153
154 /* Downscaling limits the maximum pixel rate */
155 dst_w = min(src_w, dst_w);
156 dst_h = min(src_h, dst_h);
157
158 return DIV_ROUND_UP_ULL(mul_u32_u32(rate, src_w * src_h),
159 dst_w * dst_h);
160}
161
162unsigned int intel_plane_pixel_rate(const struct intel_crtc_state *crtc_state,
163 const struct intel_plane_state *plane_state)
164{
165 /*
166 * Note we don't check for plane visibility here as
167 * we want to use this when calculating the cursor
168 * watermarks even if the cursor is fully offscreen.
169 * That depends on the src/dst rectangles being
170 * correctly populated whenever the watermark code
171 * considers the cursor to be visible, whether or not
172 * it is actually visible.
173 *
174 * See: intel_wm_plane_visible() and intel_check_cursor()
175 */
176
177 return intel_adjusted_rate(&plane_state->uapi.src,
178 &plane_state->uapi.dst,
179 crtc_state->pixel_rate);
180}
181
182unsigned int intel_plane_data_rate(const struct intel_crtc_state *crtc_state,
183 const struct intel_plane_state *plane_state,
184 int color_plane)
185{
186 const struct drm_framebuffer *fb = plane_state->hw.fb;
187
188 if (!plane_state->uapi.visible)
189 return 0;
190
191 return intel_plane_pixel_rate(crtc_state, plane_state) *
192 fb->format->cpp[color_plane];
193}
194
195static bool
196use_min_ddb(const struct intel_crtc_state *crtc_state,
197 struct intel_plane *plane)
198{
199 struct drm_i915_private *i915 = to_i915(plane->base.dev);
200
201 return DISPLAY_VER(i915) >= 13 &&
202 crtc_state->uapi.async_flip &&
203 plane->async_flip;
204}
205
206static unsigned int
207intel_plane_relative_data_rate(const struct intel_crtc_state *crtc_state,
208 const struct intel_plane_state *plane_state,
209 int color_plane)
210{
211 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
212 const struct drm_framebuffer *fb = plane_state->hw.fb;
213 int width, height;
214
215 if (plane->id == PLANE_CURSOR)
216 return 0;
217
218 if (!plane_state->uapi.visible)
219 return 0;
220
221 /*
222 * We calculate extra ddb based on ratio plane rate/total data rate
223 * in case, in some cases we should not allocate extra ddb for the plane,
224 * so do not count its data rate, if this is the case.
225 */
226 if (use_min_ddb(crtc_state, plane))
227 return 0;
228
229 /*
230 * Src coordinates are already rotated by 270 degrees for
231 * the 90/270 degree plane rotation cases (to match the
232 * GTT mapping), hence no need to account for rotation here.
233 */
234 width = drm_rect_width(&plane_state->uapi.src) >> 16;
235 height = drm_rect_height(&plane_state->uapi.src) >> 16;
236
237 /* UV plane does 1/2 pixel sub-sampling */
238 if (color_plane == 1) {
239 width /= 2;
240 height /= 2;
241 }
242
243 return width * height * fb->format->cpp[color_plane];
244}
245
246int intel_plane_calc_min_cdclk(struct intel_atomic_state *state,
247 struct intel_plane *plane,
248 bool *need_cdclk_calc)
249{
250 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
251 const struct intel_plane_state *plane_state =
252 intel_atomic_get_new_plane_state(state, plane);
253 struct intel_crtc *crtc = to_intel_crtc(plane_state->hw.crtc);
254 const struct intel_cdclk_state *cdclk_state;
255 const struct intel_crtc_state *old_crtc_state;
256 struct intel_crtc_state *new_crtc_state;
257
258 if (!plane_state->uapi.visible || !plane->min_cdclk)
259 return 0;
260
261 old_crtc_state = intel_atomic_get_old_crtc_state(state, crtc);
262 new_crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
263
264 new_crtc_state->min_cdclk[plane->id] =
265 plane->min_cdclk(new_crtc_state, plane_state);
266
267 /*
268 * No need to check against the cdclk state if
269 * the min cdclk for the plane doesn't increase.
270 *
271 * Ie. we only ever increase the cdclk due to plane
272 * requirements. This can reduce back and forth
273 * display blinking due to constant cdclk changes.
274 */
275 if (new_crtc_state->min_cdclk[plane->id] <=
276 old_crtc_state->min_cdclk[plane->id])
277 return 0;
278
279 cdclk_state = intel_atomic_get_cdclk_state(state);
280 if (IS_ERR(cdclk_state))
281 return PTR_ERR(cdclk_state);
282
283 /*
284 * No need to recalculate the cdclk state if
285 * the min cdclk for the pipe doesn't increase.
286 *
287 * Ie. we only ever increase the cdclk due to plane
288 * requirements. This can reduce back and forth
289 * display blinking due to constant cdclk changes.
290 */
291 if (new_crtc_state->min_cdclk[plane->id] <=
292 cdclk_state->min_cdclk[crtc->pipe])
293 return 0;
294
295 drm_dbg_kms(&dev_priv->drm,
296 "[PLANE:%d:%s] min cdclk (%d kHz) > [CRTC:%d:%s] min cdclk (%d kHz)\n",
297 plane->base.base.id, plane->base.name,
298 new_crtc_state->min_cdclk[plane->id],
299 crtc->base.base.id, crtc->base.name,
300 cdclk_state->min_cdclk[crtc->pipe]);
301 *need_cdclk_calc = true;
302
303 return 0;
304}
305
306static void intel_plane_clear_hw_state(struct intel_plane_state *plane_state)
307{
308 if (plane_state->hw.fb)
309 drm_framebuffer_put(plane_state->hw.fb);
310
311 memset(&plane_state->hw, 0, sizeof(plane_state->hw));
312}
313
314void intel_plane_copy_uapi_to_hw_state(struct intel_plane_state *plane_state,
315 const struct intel_plane_state *from_plane_state,
316 struct intel_crtc *crtc)
317{
318 intel_plane_clear_hw_state(plane_state);
319
320 /*
321 * For the bigjoiner slave uapi.crtc will point at
322 * the master crtc. So we explicitly assign the right
323 * slave crtc to hw.crtc. uapi.crtc!=NULL simply indicates
324 * the plane is logically enabled on the uapi level.
325 */
326 plane_state->hw.crtc = from_plane_state->uapi.crtc ? &crtc->base : NULL;
327
328 plane_state->hw.fb = from_plane_state->uapi.fb;
329 if (plane_state->hw.fb)
330 drm_framebuffer_get(plane_state->hw.fb);
331
332 plane_state->hw.alpha = from_plane_state->uapi.alpha;
333 plane_state->hw.pixel_blend_mode =
334 from_plane_state->uapi.pixel_blend_mode;
335 plane_state->hw.rotation = from_plane_state->uapi.rotation;
336 plane_state->hw.color_encoding = from_plane_state->uapi.color_encoding;
337 plane_state->hw.color_range = from_plane_state->uapi.color_range;
338 plane_state->hw.scaling_filter = from_plane_state->uapi.scaling_filter;
339
340 plane_state->uapi.src = drm_plane_state_src(&from_plane_state->uapi);
341 plane_state->uapi.dst = drm_plane_state_dest(&from_plane_state->uapi);
342}
343
344void intel_plane_copy_hw_state(struct intel_plane_state *plane_state,
345 const struct intel_plane_state *from_plane_state)
346{
347 intel_plane_clear_hw_state(plane_state);
348
349 memcpy(&plane_state->hw, &from_plane_state->hw,
350 sizeof(plane_state->hw));
351
352 if (plane_state->hw.fb)
353 drm_framebuffer_get(plane_state->hw.fb);
354}
355
356void intel_plane_set_invisible(struct intel_crtc_state *crtc_state,
357 struct intel_plane_state *plane_state)
358{
359 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
360
361 crtc_state->active_planes &= ~BIT(plane->id);
362 crtc_state->scaled_planes &= ~BIT(plane->id);
363 crtc_state->nv12_planes &= ~BIT(plane->id);
364 crtc_state->c8_planes &= ~BIT(plane->id);
365 crtc_state->data_rate[plane->id] = 0;
366 crtc_state->data_rate_y[plane->id] = 0;
367 crtc_state->rel_data_rate[plane->id] = 0;
368 crtc_state->rel_data_rate_y[plane->id] = 0;
369 crtc_state->min_cdclk[plane->id] = 0;
370
371 plane_state->uapi.visible = false;
372}
373
374/* FIXME nuke when all wm code is atomic */
375static bool intel_wm_need_update(const struct intel_plane_state *cur,
376 struct intel_plane_state *new)
377{
378 /* Update watermarks on tiling or size changes. */
379 if (new->uapi.visible != cur->uapi.visible)
380 return true;
381
382 if (!cur->hw.fb || !new->hw.fb)
383 return false;
384
385 if (cur->hw.fb->modifier != new->hw.fb->modifier ||
386 cur->hw.rotation != new->hw.rotation ||
387 drm_rect_width(&new->uapi.src) != drm_rect_width(&cur->uapi.src) ||
388 drm_rect_height(&new->uapi.src) != drm_rect_height(&cur->uapi.src) ||
389 drm_rect_width(&new->uapi.dst) != drm_rect_width(&cur->uapi.dst) ||
390 drm_rect_height(&new->uapi.dst) != drm_rect_height(&cur->uapi.dst))
391 return true;
392
393 return false;
394}
395
396static bool intel_plane_is_scaled(const struct intel_plane_state *plane_state)
397{
398 int src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
399 int src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
400 int dst_w = drm_rect_width(&plane_state->uapi.dst);
401 int dst_h = drm_rect_height(&plane_state->uapi.dst);
402
403 return src_w != dst_w || src_h != dst_h;
404}
405
406static bool intel_plane_do_async_flip(struct intel_plane *plane,
407 const struct intel_crtc_state *old_crtc_state,
408 const struct intel_crtc_state *new_crtc_state)
409{
410 struct drm_i915_private *i915 = to_i915(plane->base.dev);
411
412 if (!plane->async_flip)
413 return false;
414
415 if (!new_crtc_state->uapi.async_flip)
416 return false;
417
418 /*
419 * In platforms after DISPLAY13, we might need to override
420 * first async flip in order to change watermark levels
421 * as part of optimization.
422 * So for those, we are checking if this is a first async flip.
423 * For platforms earlier than DISPLAY13 we always do async flip.
424 */
425 return DISPLAY_VER(i915) < 13 || old_crtc_state->uapi.async_flip;
426}
427
428static bool i9xx_must_disable_cxsr(const struct intel_crtc_state *new_crtc_state,
429 const struct intel_plane_state *old_plane_state,
430 const struct intel_plane_state *new_plane_state)
431{
432 struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
433 bool old_visible = old_plane_state->uapi.visible;
434 bool new_visible = new_plane_state->uapi.visible;
435 u32 old_ctl = old_plane_state->ctl;
436 u32 new_ctl = new_plane_state->ctl;
437 bool modeset, turn_on, turn_off;
438
439 if (plane->id == PLANE_CURSOR)
440 return false;
441
442 modeset = intel_crtc_needs_modeset(new_crtc_state);
443 turn_off = old_visible && (!new_visible || modeset);
444 turn_on = new_visible && (!old_visible || modeset);
445
446 /* Must disable CxSR around plane enable/disable */
447 if (turn_on || turn_off)
448 return true;
449
450 if (!old_visible || !new_visible)
451 return false;
452
453 /*
454 * Most plane control register updates are blocked while in CxSR.
455 *
456 * Tiling mode is one exception where the primary plane can
457 * apparently handle it, whereas the sprites can not (the
458 * sprite issue being only relevant on VLV/CHV where CxSR
459 * is actually possible with a sprite enabled).
460 */
461 if (plane->id == PLANE_PRIMARY) {
462 old_ctl &= ~DISP_TILED;
463 new_ctl &= ~DISP_TILED;
464 }
465
466 return old_ctl != new_ctl;
467}
468
469static int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
470 struct intel_crtc_state *new_crtc_state,
471 const struct intel_plane_state *old_plane_state,
472 struct intel_plane_state *new_plane_state)
473{
474 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
475 struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
476 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
477 bool mode_changed = intel_crtc_needs_modeset(new_crtc_state);
478 bool was_crtc_enabled = old_crtc_state->hw.active;
479 bool is_crtc_enabled = new_crtc_state->hw.active;
480 bool turn_off, turn_on, visible, was_visible;
481 int ret;
482
483 if (DISPLAY_VER(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
484 ret = skl_update_scaler_plane(new_crtc_state, new_plane_state);
485 if (ret)
486 return ret;
487 }
488
489 was_visible = old_plane_state->uapi.visible;
490 visible = new_plane_state->uapi.visible;
491
492 if (!was_crtc_enabled && drm_WARN_ON(&dev_priv->drm, was_visible))
493 was_visible = false;
494
495 /*
496 * Visibility is calculated as if the crtc was on, but
497 * after scaler setup everything depends on it being off
498 * when the crtc isn't active.
499 *
500 * FIXME this is wrong for watermarks. Watermarks should also
501 * be computed as if the pipe would be active. Perhaps move
502 * per-plane wm computation to the .check_plane() hook, and
503 * only combine the results from all planes in the current place?
504 */
505 if (!is_crtc_enabled) {
506 intel_plane_set_invisible(new_crtc_state, new_plane_state);
507 visible = false;
508 }
509
510 if (!was_visible && !visible)
511 return 0;
512
513 turn_off = was_visible && (!visible || mode_changed);
514 turn_on = visible && (!was_visible || mode_changed);
515
516 drm_dbg_atomic(&dev_priv->drm,
517 "[CRTC:%d:%s] with [PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
518 crtc->base.base.id, crtc->base.name,
519 plane->base.base.id, plane->base.name,
520 was_visible, visible,
521 turn_off, turn_on, mode_changed);
522
523 if (turn_on) {
524 if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv))
525 new_crtc_state->update_wm_pre = true;
526 } else if (turn_off) {
527 if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv))
528 new_crtc_state->update_wm_post = true;
529 } else if (intel_wm_need_update(old_plane_state, new_plane_state)) {
530 if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv)) {
531 /* FIXME bollocks */
532 new_crtc_state->update_wm_pre = true;
533 new_crtc_state->update_wm_post = true;
534 }
535 }
536
537 if (visible || was_visible)
538 new_crtc_state->fb_bits |= plane->frontbuffer_bit;
539
540 if (HAS_GMCH(dev_priv) &&
541 i9xx_must_disable_cxsr(new_crtc_state, old_plane_state, new_plane_state))
542 new_crtc_state->disable_cxsr = true;
543
544 /*
545 * ILK/SNB DVSACNTR/Sprite Enable
546 * IVB SPR_CTL/Sprite Enable
547 * "When in Self Refresh Big FIFO mode, a write to enable the
548 * plane will be internally buffered and delayed while Big FIFO
549 * mode is exiting."
550 *
551 * Which means that enabling the sprite can take an extra frame
552 * when we start in big FIFO mode (LP1+). Thus we need to drop
553 * down to LP0 and wait for vblank in order to make sure the
554 * sprite gets enabled on the next vblank after the register write.
555 * Doing otherwise would risk enabling the sprite one frame after
556 * we've already signalled flip completion. We can resume LP1+
557 * once the sprite has been enabled.
558 *
559 *
560 * WaCxSRDisabledForSpriteScaling:ivb
561 * IVB SPR_SCALE/Scaling Enable
562 * "Low Power watermarks must be disabled for at least one
563 * frame before enabling sprite scaling, and kept disabled
564 * until sprite scaling is disabled."
565 *
566 * ILK/SNB DVSASCALE/Scaling Enable
567 * "When in Self Refresh Big FIFO mode, scaling enable will be
568 * masked off while Big FIFO mode is exiting."
569 *
570 * Despite the w/a only being listed for IVB we assume that
571 * the ILK/SNB note has similar ramifications, hence we apply
572 * the w/a on all three platforms.
573 *
574 * With experimental results seems this is needed also for primary
575 * plane, not only sprite plane.
576 */
577 if (plane->id != PLANE_CURSOR &&
578 (IS_IRONLAKE(dev_priv) || IS_SANDYBRIDGE(dev_priv) ||
579 IS_IVYBRIDGE(dev_priv)) &&
580 (turn_on || (!intel_plane_is_scaled(old_plane_state) &&
581 intel_plane_is_scaled(new_plane_state))))
582 new_crtc_state->disable_lp_wm = true;
583
584 if (intel_plane_do_async_flip(plane, old_crtc_state, new_crtc_state))
585 new_crtc_state->do_async_flip = true;
586
587 return 0;
588}
589
590int intel_plane_atomic_check_with_state(const struct intel_crtc_state *old_crtc_state,
591 struct intel_crtc_state *new_crtc_state,
592 const struct intel_plane_state *old_plane_state,
593 struct intel_plane_state *new_plane_state)
594{
595 struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
596 const struct drm_framebuffer *fb = new_plane_state->hw.fb;
597 int ret;
598
599 intel_plane_set_invisible(new_crtc_state, new_plane_state);
600 new_crtc_state->enabled_planes &= ~BIT(plane->id);
601
602 if (!new_plane_state->hw.crtc && !old_plane_state->hw.crtc)
603 return 0;
604
605 ret = plane->check_plane(new_crtc_state, new_plane_state);
606 if (ret)
607 return ret;
608
609 if (fb)
610 new_crtc_state->enabled_planes |= BIT(plane->id);
611
612 /* FIXME pre-g4x don't work like this */
613 if (new_plane_state->uapi.visible)
614 new_crtc_state->active_planes |= BIT(plane->id);
615
616 if (new_plane_state->uapi.visible &&
617 intel_plane_is_scaled(new_plane_state))
618 new_crtc_state->scaled_planes |= BIT(plane->id);
619
620 if (new_plane_state->uapi.visible &&
621 intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
622 new_crtc_state->nv12_planes |= BIT(plane->id);
623
624 if (new_plane_state->uapi.visible &&
625 fb->format->format == DRM_FORMAT_C8)
626 new_crtc_state->c8_planes |= BIT(plane->id);
627
628 if (new_plane_state->uapi.visible || old_plane_state->uapi.visible)
629 new_crtc_state->update_planes |= BIT(plane->id);
630
631 if (new_plane_state->uapi.visible &&
632 intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier)) {
633 new_crtc_state->data_rate_y[plane->id] =
634 intel_plane_data_rate(new_crtc_state, new_plane_state, 0);
635 new_crtc_state->data_rate[plane->id] =
636 intel_plane_data_rate(new_crtc_state, new_plane_state, 1);
637
638 new_crtc_state->rel_data_rate_y[plane->id] =
639 intel_plane_relative_data_rate(new_crtc_state,
640 new_plane_state, 0);
641 new_crtc_state->rel_data_rate[plane->id] =
642 intel_plane_relative_data_rate(new_crtc_state,
643 new_plane_state, 1);
644 } else if (new_plane_state->uapi.visible) {
645 new_crtc_state->data_rate[plane->id] =
646 intel_plane_data_rate(new_crtc_state, new_plane_state, 0);
647
648 new_crtc_state->rel_data_rate[plane->id] =
649 intel_plane_relative_data_rate(new_crtc_state,
650 new_plane_state, 0);
651 }
652
653 return intel_plane_atomic_calc_changes(old_crtc_state, new_crtc_state,
654 old_plane_state, new_plane_state);
655}
656
657static struct intel_plane *
658intel_crtc_get_plane(struct intel_crtc *crtc, enum plane_id plane_id)
659{
660 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
661 struct intel_plane *plane;
662
663 for_each_intel_plane_on_crtc(&i915->drm, crtc, plane) {
664 if (plane->id == plane_id)
665 return plane;
666 }
667
668 return NULL;
669}
670
671int intel_plane_atomic_check(struct intel_atomic_state *state,
672 struct intel_plane *plane)
673{
674 struct drm_i915_private *i915 = to_i915(state->base.dev);
675 struct intel_plane_state *new_plane_state =
676 intel_atomic_get_new_plane_state(state, plane);
677 const struct intel_plane_state *old_plane_state =
678 intel_atomic_get_old_plane_state(state, plane);
679 const struct intel_plane_state *new_master_plane_state;
680 struct intel_crtc *crtc = intel_crtc_for_pipe(i915, plane->pipe);
681 const struct intel_crtc_state *old_crtc_state =
682 intel_atomic_get_old_crtc_state(state, crtc);
683 struct intel_crtc_state *new_crtc_state =
684 intel_atomic_get_new_crtc_state(state, crtc);
685
686 if (new_crtc_state && intel_crtc_is_bigjoiner_slave(new_crtc_state)) {
687 struct intel_crtc *master_crtc =
688 intel_master_crtc(new_crtc_state);
689 struct intel_plane *master_plane =
690 intel_crtc_get_plane(master_crtc, plane->id);
691
692 new_master_plane_state =
693 intel_atomic_get_new_plane_state(state, master_plane);
694 } else {
695 new_master_plane_state = new_plane_state;
696 }
697
698 intel_plane_copy_uapi_to_hw_state(new_plane_state,
699 new_master_plane_state,
700 crtc);
701
702 new_plane_state->uapi.visible = false;
703 if (!new_crtc_state)
704 return 0;
705
706 return intel_plane_atomic_check_with_state(old_crtc_state,
707 new_crtc_state,
708 old_plane_state,
709 new_plane_state);
710}
711
712static struct intel_plane *
713skl_next_plane_to_commit(struct intel_atomic_state *state,
714 struct intel_crtc *crtc,
715 struct skl_ddb_entry ddb[I915_MAX_PLANES],
716 struct skl_ddb_entry ddb_y[I915_MAX_PLANES],
717 unsigned int *update_mask)
718{
719 struct intel_crtc_state *crtc_state =
720 intel_atomic_get_new_crtc_state(state, crtc);
721 struct intel_plane_state *plane_state;
722 struct intel_plane *plane;
723 int i;
724
725 if (*update_mask == 0)
726 return NULL;
727
728 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
729 enum plane_id plane_id = plane->id;
730
731 if (crtc->pipe != plane->pipe ||
732 !(*update_mask & BIT(plane_id)))
733 continue;
734
735 if (skl_ddb_allocation_overlaps(&crtc_state->wm.skl.plane_ddb[plane_id],
736 ddb, I915_MAX_PLANES, plane_id) ||
737 skl_ddb_allocation_overlaps(&crtc_state->wm.skl.plane_ddb_y[plane_id],
738 ddb_y, I915_MAX_PLANES, plane_id))
739 continue;
740
741 *update_mask &= ~BIT(plane_id);
742 ddb[plane_id] = crtc_state->wm.skl.plane_ddb[plane_id];
743 ddb_y[plane_id] = crtc_state->wm.skl.plane_ddb_y[plane_id];
744
745 return plane;
746 }
747
748 /* should never happen */
749 drm_WARN_ON(state->base.dev, 1);
750
751 return NULL;
752}
753
754void intel_plane_update_noarm(struct intel_plane *plane,
755 const struct intel_crtc_state *crtc_state,
756 const struct intel_plane_state *plane_state)
757{
758 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
759
760 trace_intel_plane_update_noarm(plane, crtc);
761
762 if (plane->update_noarm)
763 plane->update_noarm(plane, crtc_state, plane_state);
764}
765
766void intel_plane_update_arm(struct intel_plane *plane,
767 const struct intel_crtc_state *crtc_state,
768 const struct intel_plane_state *plane_state)
769{
770 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
771
772 trace_intel_plane_update_arm(plane, crtc);
773
774 if (crtc_state->do_async_flip && plane->async_flip)
775 plane->async_flip(plane, crtc_state, plane_state, true);
776 else
777 plane->update_arm(plane, crtc_state, plane_state);
778}
779
780void intel_plane_disable_arm(struct intel_plane *plane,
781 const struct intel_crtc_state *crtc_state)
782{
783 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
784
785 trace_intel_plane_disable_arm(plane, crtc);
786 plane->disable_arm(plane, crtc_state);
787}
788
789void intel_crtc_planes_update_noarm(struct intel_atomic_state *state,
790 struct intel_crtc *crtc)
791{
792 struct intel_crtc_state *new_crtc_state =
793 intel_atomic_get_new_crtc_state(state, crtc);
794 u32 update_mask = new_crtc_state->update_planes;
795 struct intel_plane_state *new_plane_state;
796 struct intel_plane *plane;
797 int i;
798
799 if (new_crtc_state->do_async_flip)
800 return;
801
802 /*
803 * Since we only write non-arming registers here,
804 * the order does not matter even for skl+.
805 */
806 for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) {
807 if (crtc->pipe != plane->pipe ||
808 !(update_mask & BIT(plane->id)))
809 continue;
810
811 /* TODO: for mailbox updates this should be skipped */
812 if (new_plane_state->uapi.visible ||
813 new_plane_state->planar_slave)
814 intel_plane_update_noarm(plane, new_crtc_state, new_plane_state);
815 }
816}
817
818static void skl_crtc_planes_update_arm(struct intel_atomic_state *state,
819 struct intel_crtc *crtc)
820{
821 struct intel_crtc_state *old_crtc_state =
822 intel_atomic_get_old_crtc_state(state, crtc);
823 struct intel_crtc_state *new_crtc_state =
824 intel_atomic_get_new_crtc_state(state, crtc);
825 struct skl_ddb_entry ddb[I915_MAX_PLANES];
826 struct skl_ddb_entry ddb_y[I915_MAX_PLANES];
827 u32 update_mask = new_crtc_state->update_planes;
828 struct intel_plane *plane;
829
830 memcpy(ddb, old_crtc_state->wm.skl.plane_ddb,
831 sizeof(old_crtc_state->wm.skl.plane_ddb));
832 memcpy(ddb_y, old_crtc_state->wm.skl.plane_ddb_y,
833 sizeof(old_crtc_state->wm.skl.plane_ddb_y));
834
835 while ((plane = skl_next_plane_to_commit(state, crtc, ddb, ddb_y, &update_mask))) {
836 struct intel_plane_state *new_plane_state =
837 intel_atomic_get_new_plane_state(state, plane);
838
839 /*
840 * TODO: for mailbox updates intel_plane_update_noarm()
841 * would have to be called here as well.
842 */
843 if (new_plane_state->uapi.visible ||
844 new_plane_state->planar_slave)
845 intel_plane_update_arm(plane, new_crtc_state, new_plane_state);
846 else
847 intel_plane_disable_arm(plane, new_crtc_state);
848 }
849}
850
851static void i9xx_crtc_planes_update_arm(struct intel_atomic_state *state,
852 struct intel_crtc *crtc)
853{
854 struct intel_crtc_state *new_crtc_state =
855 intel_atomic_get_new_crtc_state(state, crtc);
856 u32 update_mask = new_crtc_state->update_planes;
857 struct intel_plane_state *new_plane_state;
858 struct intel_plane *plane;
859 int i;
860
861 for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) {
862 if (crtc->pipe != plane->pipe ||
863 !(update_mask & BIT(plane->id)))
864 continue;
865
866 /*
867 * TODO: for mailbox updates intel_plane_update_noarm()
868 * would have to be called here as well.
869 */
870 if (new_plane_state->uapi.visible)
871 intel_plane_update_arm(plane, new_crtc_state, new_plane_state);
872 else
873 intel_plane_disable_arm(plane, new_crtc_state);
874 }
875}
876
877void intel_crtc_planes_update_arm(struct intel_atomic_state *state,
878 struct intel_crtc *crtc)
879{
880 struct drm_i915_private *i915 = to_i915(state->base.dev);
881
882 if (DISPLAY_VER(i915) >= 9)
883 skl_crtc_planes_update_arm(state, crtc);
884 else
885 i9xx_crtc_planes_update_arm(state, crtc);
886}
887
888int intel_atomic_plane_check_clipping(struct intel_plane_state *plane_state,
889 struct intel_crtc_state *crtc_state,
890 int min_scale, int max_scale,
891 bool can_position)
892{
893 struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
894 struct drm_framebuffer *fb = plane_state->hw.fb;
895 struct drm_rect *src = &plane_state->uapi.src;
896 struct drm_rect *dst = &plane_state->uapi.dst;
897 const struct drm_rect *clip = &crtc_state->pipe_src;
898 unsigned int rotation = plane_state->hw.rotation;
899 int hscale, vscale;
900
901 if (!fb) {
902 plane_state->uapi.visible = false;
903 return 0;
904 }
905
906 drm_rect_rotate(src, fb->width << 16, fb->height << 16, rotation);
907
908 /* Check scaling */
909 hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
910 vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
911 if (hscale < 0 || vscale < 0) {
912 drm_dbg_kms(&i915->drm, "Invalid scaling of plane\n");
913 drm_rect_debug_print("src: ", src, true);
914 drm_rect_debug_print("dst: ", dst, false);
915 return -ERANGE;
916 }
917
918 /*
919 * FIXME: This might need further adjustment for seamless scaling
920 * with phase information, for the 2p2 and 2p1 scenarios.
921 */
922 plane_state->uapi.visible = drm_rect_clip_scaled(src, dst, clip);
923
924 drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16, rotation);
925
926 if (!can_position && plane_state->uapi.visible &&
927 !drm_rect_equals(dst, clip)) {
928 drm_dbg_kms(&i915->drm, "Plane must cover entire CRTC\n");
929 drm_rect_debug_print("dst: ", dst, false);
930 drm_rect_debug_print("clip: ", clip, false);
931 return -EINVAL;
932 }
933
934 /* final plane coordinates will be relative to the plane's pipe */
935 drm_rect_translate(dst, -clip->x1, -clip->y1);
936
937 return 0;
938}
939
940struct wait_rps_boost {
941 struct wait_queue_entry wait;
942
943 struct drm_crtc *crtc;
944 struct i915_request *request;
945};
946
947static int do_rps_boost(struct wait_queue_entry *_wait,
948 unsigned mode, int sync, void *key)
949{
950 struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
951 struct i915_request *rq = wait->request;
952
953 /*
954 * If we missed the vblank, but the request is already running it
955 * is reasonable to assume that it will complete before the next
956 * vblank without our intervention, so leave RPS alone.
957 */
958 if (!i915_request_started(rq))
959 intel_rps_boost(rq);
960 i915_request_put(rq);
961
962 drm_crtc_vblank_put(wait->crtc);
963
964 list_del(&wait->wait.entry);
965 kfree(wait);
966 return 1;
967}
968
969static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
970 struct dma_fence *fence)
971{
972 struct wait_rps_boost *wait;
973
974 if (!dma_fence_is_i915(fence))
975 return;
976
977 if (DISPLAY_VER(to_i915(crtc->dev)) < 6)
978 return;
979
980 if (drm_crtc_vblank_get(crtc))
981 return;
982
983 wait = kmalloc(sizeof(*wait), GFP_KERNEL);
984 if (!wait) {
985 drm_crtc_vblank_put(crtc);
986 return;
987 }
988
989 wait->request = to_request(dma_fence_get(fence));
990 wait->crtc = crtc;
991
992 wait->wait.func = do_rps_boost;
993 wait->wait.flags = 0;
994
995 add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
996}
997
998/**
999 * intel_prepare_plane_fb - Prepare fb for usage on plane
1000 * @_plane: drm plane to prepare for
1001 * @_new_plane_state: the plane state being prepared
1002 *
1003 * Prepares a framebuffer for usage on a display plane. Generally this
1004 * involves pinning the underlying object and updating the frontbuffer tracking
1005 * bits. Some older platforms need special physical address handling for
1006 * cursor planes.
1007 *
1008 * Returns 0 on success, negative error code on failure.
1009 */
1010static int
1011intel_prepare_plane_fb(struct drm_plane *_plane,
1012 struct drm_plane_state *_new_plane_state)
1013{
1014 struct i915_sched_attr attr = { .priority = I915_PRIORITY_DISPLAY };
1015 struct intel_plane *plane = to_intel_plane(_plane);
1016 struct intel_plane_state *new_plane_state =
1017 to_intel_plane_state(_new_plane_state);
1018 struct intel_atomic_state *state =
1019 to_intel_atomic_state(new_plane_state->uapi.state);
1020 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1021 const struct intel_plane_state *old_plane_state =
1022 intel_atomic_get_old_plane_state(state, plane);
1023 struct drm_i915_gem_object *obj = intel_fb_obj(new_plane_state->hw.fb);
1024 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_plane_state->hw.fb);
1025 int ret;
1026
1027 if (old_obj) {
1028 const struct intel_crtc_state *crtc_state =
1029 intel_atomic_get_new_crtc_state(state,
1030 to_intel_crtc(old_plane_state->hw.crtc));
1031
1032 /* Big Hammer, we also need to ensure that any pending
1033 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
1034 * current scanout is retired before unpinning the old
1035 * framebuffer. Note that we rely on userspace rendering
1036 * into the buffer attached to the pipe they are waiting
1037 * on. If not, userspace generates a GPU hang with IPEHR
1038 * point to the MI_WAIT_FOR_EVENT.
1039 *
1040 * This should only fail upon a hung GPU, in which case we
1041 * can safely continue.
1042 */
1043 if (intel_crtc_needs_modeset(crtc_state)) {
1044 ret = i915_sw_fence_await_reservation(&state->commit_ready,
1045 old_obj->base.resv,
1046 false, 0,
1047 GFP_KERNEL);
1048 if (ret < 0)
1049 return ret;
1050 }
1051 }
1052
1053 if (new_plane_state->uapi.fence) { /* explicit fencing */
1054 i915_gem_fence_wait_priority(new_plane_state->uapi.fence,
1055 &attr);
1056 ret = i915_sw_fence_await_dma_fence(&state->commit_ready,
1057 new_plane_state->uapi.fence,
1058 i915_fence_timeout(dev_priv),
1059 GFP_KERNEL);
1060 if (ret < 0)
1061 return ret;
1062 }
1063
1064 if (!obj)
1065 return 0;
1066
1067
1068 ret = intel_plane_pin_fb(new_plane_state);
1069 if (ret)
1070 return ret;
1071
1072 i915_gem_object_wait_priority(obj, 0, &attr);
1073
1074 if (!new_plane_state->uapi.fence) { /* implicit fencing */
1075 struct dma_resv_iter cursor;
1076 struct dma_fence *fence;
1077
1078 ret = i915_sw_fence_await_reservation(&state->commit_ready,
1079 obj->base.resv, false,
1080 i915_fence_timeout(dev_priv),
1081 GFP_KERNEL);
1082 if (ret < 0)
1083 goto unpin_fb;
1084
1085 dma_resv_iter_begin(&cursor, obj->base.resv,
1086 DMA_RESV_USAGE_WRITE);
1087 dma_resv_for_each_fence_unlocked(&cursor, fence) {
1088 add_rps_boost_after_vblank(new_plane_state->hw.crtc,
1089 fence);
1090 }
1091 dma_resv_iter_end(&cursor);
1092 } else {
1093 add_rps_boost_after_vblank(new_plane_state->hw.crtc,
1094 new_plane_state->uapi.fence);
1095 }
1096
1097 /*
1098 * We declare pageflips to be interactive and so merit a small bias
1099 * towards upclocking to deliver the frame on time. By only changing
1100 * the RPS thresholds to sample more regularly and aim for higher
1101 * clocks we can hopefully deliver low power workloads (like kodi)
1102 * that are not quite steady state without resorting to forcing
1103 * maximum clocks following a vblank miss (see do_rps_boost()).
1104 */
1105 if (!state->rps_interactive) {
1106 intel_rps_mark_interactive(&to_gt(dev_priv)->rps, true);
1107 state->rps_interactive = true;
1108 }
1109
1110 return 0;
1111
1112unpin_fb:
1113 intel_plane_unpin_fb(new_plane_state);
1114
1115 return ret;
1116}
1117
1118/**
1119 * intel_cleanup_plane_fb - Cleans up an fb after plane use
1120 * @plane: drm plane to clean up for
1121 * @_old_plane_state: the state from the previous modeset
1122 *
1123 * Cleans up a framebuffer that has just been removed from a plane.
1124 */
1125static void
1126intel_cleanup_plane_fb(struct drm_plane *plane,
1127 struct drm_plane_state *_old_plane_state)
1128{
1129 struct intel_plane_state *old_plane_state =
1130 to_intel_plane_state(_old_plane_state);
1131 struct intel_atomic_state *state =
1132 to_intel_atomic_state(old_plane_state->uapi.state);
1133 struct drm_i915_private *dev_priv = to_i915(plane->dev);
1134 struct drm_i915_gem_object *obj = intel_fb_obj(old_plane_state->hw.fb);
1135
1136 if (!obj)
1137 return;
1138
1139 if (state->rps_interactive) {
1140 intel_rps_mark_interactive(&to_gt(dev_priv)->rps, false);
1141 state->rps_interactive = false;
1142 }
1143
1144 /* Should only be called after a successful intel_prepare_plane_fb()! */
1145 intel_plane_unpin_fb(old_plane_state);
1146}
1147
1148static const struct drm_plane_helper_funcs intel_plane_helper_funcs = {
1149 .prepare_fb = intel_prepare_plane_fb,
1150 .cleanup_fb = intel_cleanup_plane_fb,
1151};
1152
1153void intel_plane_helper_add(struct intel_plane *plane)
1154{
1155 drm_plane_helper_add(&plane->base, &intel_plane_helper_funcs);
1156}
1/*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24/**
25 * DOC: atomic plane helpers
26 *
27 * The functions here are used by the atomic plane helper functions to
28 * implement legacy plane updates (i.e., drm_plane->update_plane() and
29 * drm_plane->disable_plane()). This allows plane updates to use the
30 * atomic state infrastructure and perform plane updates as separate
31 * prepare/check/commit/cleanup steps.
32 */
33
34#include <linux/dma-fence-chain.h>
35#include <linux/dma-resv.h>
36
37#include <drm/drm_atomic_helper.h>
38#include <drm/drm_blend.h>
39#include <drm/drm_fourcc.h>
40#include <drm/drm_gem.h>
41#include <drm/drm_gem_atomic_helper.h>
42
43#include "i915_config.h"
44#include "i9xx_plane_regs.h"
45#include "intel_atomic_plane.h"
46#include "intel_cdclk.h"
47#include "intel_cursor.h"
48#include "intel_display_rps.h"
49#include "intel_display_trace.h"
50#include "intel_display_types.h"
51#include "intel_fb.h"
52#include "intel_fb_pin.h"
53#include "skl_scaler.h"
54#include "skl_watermark.h"
55
56static void intel_plane_state_reset(struct intel_plane_state *plane_state,
57 struct intel_plane *plane)
58{
59 memset(plane_state, 0, sizeof(*plane_state));
60
61 __drm_atomic_helper_plane_state_reset(&plane_state->uapi, &plane->base);
62
63 plane_state->scaler_id = -1;
64}
65
66struct intel_plane *intel_plane_alloc(void)
67{
68 struct intel_plane_state *plane_state;
69 struct intel_plane *plane;
70
71 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
72 if (!plane)
73 return ERR_PTR(-ENOMEM);
74
75 plane_state = kzalloc(sizeof(*plane_state), GFP_KERNEL);
76 if (!plane_state) {
77 kfree(plane);
78 return ERR_PTR(-ENOMEM);
79 }
80
81 intel_plane_state_reset(plane_state, plane);
82
83 plane->base.state = &plane_state->uapi;
84
85 return plane;
86}
87
88void intel_plane_free(struct intel_plane *plane)
89{
90 intel_plane_destroy_state(&plane->base, plane->base.state);
91 kfree(plane);
92}
93
94/**
95 * intel_plane_duplicate_state - duplicate plane state
96 * @plane: drm plane
97 *
98 * Allocates and returns a copy of the plane state (both common and
99 * Intel-specific) for the specified plane.
100 *
101 * Returns: The newly allocated plane state, or NULL on failure.
102 */
103struct drm_plane_state *
104intel_plane_duplicate_state(struct drm_plane *plane)
105{
106 struct intel_plane_state *intel_state;
107
108 intel_state = to_intel_plane_state(plane->state);
109 intel_state = kmemdup(intel_state, sizeof(*intel_state), GFP_KERNEL);
110
111 if (!intel_state)
112 return NULL;
113
114 __drm_atomic_helper_plane_duplicate_state(plane, &intel_state->uapi);
115
116 intel_state->ggtt_vma = NULL;
117 intel_state->dpt_vma = NULL;
118 intel_state->flags = 0;
119
120 /* add reference to fb */
121 if (intel_state->hw.fb)
122 drm_framebuffer_get(intel_state->hw.fb);
123
124 return &intel_state->uapi;
125}
126
127/**
128 * intel_plane_destroy_state - destroy plane state
129 * @plane: drm plane
130 * @state: state object to destroy
131 *
132 * Destroys the plane state (both common and Intel-specific) for the
133 * specified plane.
134 */
135void
136intel_plane_destroy_state(struct drm_plane *plane,
137 struct drm_plane_state *state)
138{
139 struct intel_plane_state *plane_state = to_intel_plane_state(state);
140
141 drm_WARN_ON(plane->dev, plane_state->ggtt_vma);
142 drm_WARN_ON(plane->dev, plane_state->dpt_vma);
143
144 __drm_atomic_helper_plane_destroy_state(&plane_state->uapi);
145 if (plane_state->hw.fb)
146 drm_framebuffer_put(plane_state->hw.fb);
147 kfree(plane_state);
148}
149
150bool intel_plane_needs_physical(struct intel_plane *plane)
151{
152 struct drm_i915_private *i915 = to_i915(plane->base.dev);
153
154 return plane->id == PLANE_CURSOR &&
155 DISPLAY_INFO(i915)->cursor_needs_physical;
156}
157
158unsigned int intel_adjusted_rate(const struct drm_rect *src,
159 const struct drm_rect *dst,
160 unsigned int rate)
161{
162 unsigned int src_w, src_h, dst_w, dst_h;
163
164 src_w = drm_rect_width(src) >> 16;
165 src_h = drm_rect_height(src) >> 16;
166 dst_w = drm_rect_width(dst);
167 dst_h = drm_rect_height(dst);
168
169 /* Downscaling limits the maximum pixel rate */
170 dst_w = min(src_w, dst_w);
171 dst_h = min(src_h, dst_h);
172
173 return DIV_ROUND_UP_ULL(mul_u32_u32(rate, src_w * src_h),
174 dst_w * dst_h);
175}
176
177unsigned int intel_plane_pixel_rate(const struct intel_crtc_state *crtc_state,
178 const struct intel_plane_state *plane_state)
179{
180 /*
181 * Note we don't check for plane visibility here as
182 * we want to use this when calculating the cursor
183 * watermarks even if the cursor is fully offscreen.
184 * That depends on the src/dst rectangles being
185 * correctly populated whenever the watermark code
186 * considers the cursor to be visible, whether or not
187 * it is actually visible.
188 *
189 * See: intel_wm_plane_visible() and intel_check_cursor()
190 */
191
192 return intel_adjusted_rate(&plane_state->uapi.src,
193 &plane_state->uapi.dst,
194 crtc_state->pixel_rate);
195}
196
197unsigned int intel_plane_data_rate(const struct intel_crtc_state *crtc_state,
198 const struct intel_plane_state *plane_state,
199 int color_plane)
200{
201 const struct drm_framebuffer *fb = plane_state->hw.fb;
202
203 if (!plane_state->uapi.visible)
204 return 0;
205
206 return intel_plane_pixel_rate(crtc_state, plane_state) *
207 fb->format->cpp[color_plane];
208}
209
210static bool
211use_min_ddb(const struct intel_crtc_state *crtc_state,
212 struct intel_plane *plane)
213{
214 struct drm_i915_private *i915 = to_i915(plane->base.dev);
215
216 return DISPLAY_VER(i915) >= 13 &&
217 crtc_state->uapi.async_flip &&
218 plane->async_flip;
219}
220
221static unsigned int
222intel_plane_relative_data_rate(const struct intel_crtc_state *crtc_state,
223 const struct intel_plane_state *plane_state,
224 int color_plane)
225{
226 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
227 const struct drm_framebuffer *fb = plane_state->hw.fb;
228 int width, height;
229 unsigned int rel_data_rate;
230
231 if (plane->id == PLANE_CURSOR)
232 return 0;
233
234 if (!plane_state->uapi.visible)
235 return 0;
236
237 /*
238 * We calculate extra ddb based on ratio plane rate/total data rate
239 * in case, in some cases we should not allocate extra ddb for the plane,
240 * so do not count its data rate, if this is the case.
241 */
242 if (use_min_ddb(crtc_state, plane))
243 return 0;
244
245 /*
246 * Src coordinates are already rotated by 270 degrees for
247 * the 90/270 degree plane rotation cases (to match the
248 * GTT mapping), hence no need to account for rotation here.
249 */
250 width = drm_rect_width(&plane_state->uapi.src) >> 16;
251 height = drm_rect_height(&plane_state->uapi.src) >> 16;
252
253 /* UV plane does 1/2 pixel sub-sampling */
254 if (color_plane == 1) {
255 width /= 2;
256 height /= 2;
257 }
258
259 rel_data_rate = width * height * fb->format->cpp[color_plane];
260
261 return intel_adjusted_rate(&plane_state->uapi.src,
262 &plane_state->uapi.dst,
263 rel_data_rate);
264}
265
266int intel_plane_calc_min_cdclk(struct intel_atomic_state *state,
267 struct intel_plane *plane,
268 bool *need_cdclk_calc)
269{
270 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
271 const struct intel_plane_state *plane_state =
272 intel_atomic_get_new_plane_state(state, plane);
273 struct intel_crtc *crtc = to_intel_crtc(plane_state->hw.crtc);
274 const struct intel_cdclk_state *cdclk_state;
275 const struct intel_crtc_state *old_crtc_state;
276 struct intel_crtc_state *new_crtc_state;
277
278 if (!plane_state->uapi.visible || !plane->min_cdclk)
279 return 0;
280
281 old_crtc_state = intel_atomic_get_old_crtc_state(state, crtc);
282 new_crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
283
284 new_crtc_state->min_cdclk[plane->id] =
285 plane->min_cdclk(new_crtc_state, plane_state);
286
287 /*
288 * No need to check against the cdclk state if
289 * the min cdclk for the plane doesn't increase.
290 *
291 * Ie. we only ever increase the cdclk due to plane
292 * requirements. This can reduce back and forth
293 * display blinking due to constant cdclk changes.
294 */
295 if (new_crtc_state->min_cdclk[plane->id] <=
296 old_crtc_state->min_cdclk[plane->id])
297 return 0;
298
299 cdclk_state = intel_atomic_get_cdclk_state(state);
300 if (IS_ERR(cdclk_state))
301 return PTR_ERR(cdclk_state);
302
303 /*
304 * No need to recalculate the cdclk state if
305 * the min cdclk for the pipe doesn't increase.
306 *
307 * Ie. we only ever increase the cdclk due to plane
308 * requirements. This can reduce back and forth
309 * display blinking due to constant cdclk changes.
310 */
311 if (new_crtc_state->min_cdclk[plane->id] <=
312 cdclk_state->min_cdclk[crtc->pipe])
313 return 0;
314
315 drm_dbg_kms(&dev_priv->drm,
316 "[PLANE:%d:%s] min cdclk (%d kHz) > [CRTC:%d:%s] min cdclk (%d kHz)\n",
317 plane->base.base.id, plane->base.name,
318 new_crtc_state->min_cdclk[plane->id],
319 crtc->base.base.id, crtc->base.name,
320 cdclk_state->min_cdclk[crtc->pipe]);
321 *need_cdclk_calc = true;
322
323 return 0;
324}
325
326static void intel_plane_clear_hw_state(struct intel_plane_state *plane_state)
327{
328 if (plane_state->hw.fb)
329 drm_framebuffer_put(plane_state->hw.fb);
330
331 memset(&plane_state->hw, 0, sizeof(plane_state->hw));
332}
333
334void intel_plane_copy_uapi_to_hw_state(struct intel_plane_state *plane_state,
335 const struct intel_plane_state *from_plane_state,
336 struct intel_crtc *crtc)
337{
338 intel_plane_clear_hw_state(plane_state);
339
340 /*
341 * For the joiner secondary uapi.crtc will point at
342 * the primary crtc. So we explicitly assign the right
343 * secondary crtc to hw.crtc. uapi.crtc!=NULL simply
344 * indicates the plane is logically enabled on the uapi level.
345 */
346 plane_state->hw.crtc = from_plane_state->uapi.crtc ? &crtc->base : NULL;
347
348 plane_state->hw.fb = from_plane_state->uapi.fb;
349 if (plane_state->hw.fb)
350 drm_framebuffer_get(plane_state->hw.fb);
351
352 plane_state->hw.alpha = from_plane_state->uapi.alpha;
353 plane_state->hw.pixel_blend_mode =
354 from_plane_state->uapi.pixel_blend_mode;
355 plane_state->hw.rotation = from_plane_state->uapi.rotation;
356 plane_state->hw.color_encoding = from_plane_state->uapi.color_encoding;
357 plane_state->hw.color_range = from_plane_state->uapi.color_range;
358 plane_state->hw.scaling_filter = from_plane_state->uapi.scaling_filter;
359
360 plane_state->uapi.src = drm_plane_state_src(&from_plane_state->uapi);
361 plane_state->uapi.dst = drm_plane_state_dest(&from_plane_state->uapi);
362}
363
364void intel_plane_copy_hw_state(struct intel_plane_state *plane_state,
365 const struct intel_plane_state *from_plane_state)
366{
367 intel_plane_clear_hw_state(plane_state);
368
369 memcpy(&plane_state->hw, &from_plane_state->hw,
370 sizeof(plane_state->hw));
371
372 if (plane_state->hw.fb)
373 drm_framebuffer_get(plane_state->hw.fb);
374}
375
376void intel_plane_set_invisible(struct intel_crtc_state *crtc_state,
377 struct intel_plane_state *plane_state)
378{
379 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
380
381 crtc_state->active_planes &= ~BIT(plane->id);
382 crtc_state->scaled_planes &= ~BIT(plane->id);
383 crtc_state->nv12_planes &= ~BIT(plane->id);
384 crtc_state->c8_planes &= ~BIT(plane->id);
385 crtc_state->async_flip_planes &= ~BIT(plane->id);
386 crtc_state->data_rate[plane->id] = 0;
387 crtc_state->data_rate_y[plane->id] = 0;
388 crtc_state->rel_data_rate[plane->id] = 0;
389 crtc_state->rel_data_rate_y[plane->id] = 0;
390 crtc_state->min_cdclk[plane->id] = 0;
391
392 plane_state->uapi.visible = false;
393}
394
395static bool intel_plane_is_scaled(const struct intel_plane_state *plane_state)
396{
397 int src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
398 int src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
399 int dst_w = drm_rect_width(&plane_state->uapi.dst);
400 int dst_h = drm_rect_height(&plane_state->uapi.dst);
401
402 return src_w != dst_w || src_h != dst_h;
403}
404
405static bool intel_plane_do_async_flip(struct intel_plane *plane,
406 const struct intel_crtc_state *old_crtc_state,
407 const struct intel_crtc_state *new_crtc_state)
408{
409 struct drm_i915_private *i915 = to_i915(plane->base.dev);
410
411 if (!plane->async_flip)
412 return false;
413
414 if (!new_crtc_state->uapi.async_flip)
415 return false;
416
417 /*
418 * In platforms after DISPLAY13, we might need to override
419 * first async flip in order to change watermark levels
420 * as part of optimization.
421 *
422 * And let's do this for all skl+ so that we can eg. change the
423 * modifier as well.
424 *
425 * TODO: For older platforms there is less reason to do this as
426 * only X-tile is supported with async flips, though we could
427 * extend this so other scanout parameters (stride/etc) could
428 * be changed as well...
429 */
430 return DISPLAY_VER(i915) < 9 || old_crtc_state->uapi.async_flip;
431}
432
433static bool i9xx_must_disable_cxsr(const struct intel_crtc_state *new_crtc_state,
434 const struct intel_plane_state *old_plane_state,
435 const struct intel_plane_state *new_plane_state)
436{
437 struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
438 bool old_visible = old_plane_state->uapi.visible;
439 bool new_visible = new_plane_state->uapi.visible;
440 u32 old_ctl = old_plane_state->ctl;
441 u32 new_ctl = new_plane_state->ctl;
442 bool modeset, turn_on, turn_off;
443
444 if (plane->id == PLANE_CURSOR)
445 return false;
446
447 modeset = intel_crtc_needs_modeset(new_crtc_state);
448 turn_off = old_visible && (!new_visible || modeset);
449 turn_on = new_visible && (!old_visible || modeset);
450
451 /* Must disable CxSR around plane enable/disable */
452 if (turn_on || turn_off)
453 return true;
454
455 if (!old_visible || !new_visible)
456 return false;
457
458 /*
459 * Most plane control register updates are blocked while in CxSR.
460 *
461 * Tiling mode is one exception where the primary plane can
462 * apparently handle it, whereas the sprites can not (the
463 * sprite issue being only relevant on VLV/CHV where CxSR
464 * is actually possible with a sprite enabled).
465 */
466 if (plane->id == PLANE_PRIMARY) {
467 old_ctl &= ~DISP_TILED;
468 new_ctl &= ~DISP_TILED;
469 }
470
471 return old_ctl != new_ctl;
472}
473
474static bool ilk_must_disable_cxsr(const struct intel_crtc_state *new_crtc_state,
475 const struct intel_plane_state *old_plane_state,
476 const struct intel_plane_state *new_plane_state)
477{
478 struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
479 bool old_visible = old_plane_state->uapi.visible;
480 bool new_visible = new_plane_state->uapi.visible;
481 bool modeset, turn_on;
482
483 if (plane->id == PLANE_CURSOR)
484 return false;
485
486 modeset = intel_crtc_needs_modeset(new_crtc_state);
487 turn_on = new_visible && (!old_visible || modeset);
488
489 /*
490 * ILK/SNB DVSACNTR/Sprite Enable
491 * IVB SPR_CTL/Sprite Enable
492 * "When in Self Refresh Big FIFO mode, a write to enable the
493 * plane will be internally buffered and delayed while Big FIFO
494 * mode is exiting."
495 *
496 * Which means that enabling the sprite can take an extra frame
497 * when we start in big FIFO mode (LP1+). Thus we need to drop
498 * down to LP0 and wait for vblank in order to make sure the
499 * sprite gets enabled on the next vblank after the register write.
500 * Doing otherwise would risk enabling the sprite one frame after
501 * we've already signalled flip completion. We can resume LP1+
502 * once the sprite has been enabled.
503 *
504 * With experimental results seems this is needed also for primary
505 * plane, not only sprite plane.
506 */
507 if (turn_on)
508 return true;
509
510 /*
511 * WaCxSRDisabledForSpriteScaling:ivb
512 * IVB SPR_SCALE/Scaling Enable
513 * "Low Power watermarks must be disabled for at least one
514 * frame before enabling sprite scaling, and kept disabled
515 * until sprite scaling is disabled."
516 *
517 * ILK/SNB DVSASCALE/Scaling Enable
518 * "When in Self Refresh Big FIFO mode, scaling enable will be
519 * masked off while Big FIFO mode is exiting."
520 *
521 * Despite the w/a only being listed for IVB we assume that
522 * the ILK/SNB note has similar ramifications, hence we apply
523 * the w/a on all three platforms.
524 */
525 return !intel_plane_is_scaled(old_plane_state) &&
526 intel_plane_is_scaled(new_plane_state);
527}
528
529static int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
530 struct intel_crtc_state *new_crtc_state,
531 const struct intel_plane_state *old_plane_state,
532 struct intel_plane_state *new_plane_state)
533{
534 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
535 struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
536 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
537 bool mode_changed = intel_crtc_needs_modeset(new_crtc_state);
538 bool was_crtc_enabled = old_crtc_state->hw.active;
539 bool is_crtc_enabled = new_crtc_state->hw.active;
540 bool turn_off, turn_on, visible, was_visible;
541 int ret;
542
543 if (DISPLAY_VER(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
544 ret = skl_update_scaler_plane(new_crtc_state, new_plane_state);
545 if (ret)
546 return ret;
547 }
548
549 was_visible = old_plane_state->uapi.visible;
550 visible = new_plane_state->uapi.visible;
551
552 if (!was_crtc_enabled && drm_WARN_ON(&dev_priv->drm, was_visible))
553 was_visible = false;
554
555 /*
556 * Visibility is calculated as if the crtc was on, but
557 * after scaler setup everything depends on it being off
558 * when the crtc isn't active.
559 *
560 * FIXME this is wrong for watermarks. Watermarks should also
561 * be computed as if the pipe would be active. Perhaps move
562 * per-plane wm computation to the .check_plane() hook, and
563 * only combine the results from all planes in the current place?
564 */
565 if (!is_crtc_enabled) {
566 intel_plane_set_invisible(new_crtc_state, new_plane_state);
567 visible = false;
568 }
569
570 if (!was_visible && !visible)
571 return 0;
572
573 turn_off = was_visible && (!visible || mode_changed);
574 turn_on = visible && (!was_visible || mode_changed);
575
576 drm_dbg_atomic(&dev_priv->drm,
577 "[CRTC:%d:%s] with [PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
578 crtc->base.base.id, crtc->base.name,
579 plane->base.base.id, plane->base.name,
580 was_visible, visible,
581 turn_off, turn_on, mode_changed);
582
583 if (visible || was_visible)
584 new_crtc_state->fb_bits |= plane->frontbuffer_bit;
585
586 if (HAS_GMCH(dev_priv) &&
587 i9xx_must_disable_cxsr(new_crtc_state, old_plane_state, new_plane_state))
588 new_crtc_state->disable_cxsr = true;
589
590 if ((IS_IRONLAKE(dev_priv) || IS_SANDYBRIDGE(dev_priv) || IS_IVYBRIDGE(dev_priv)) &&
591 ilk_must_disable_cxsr(new_crtc_state, old_plane_state, new_plane_state))
592 new_crtc_state->disable_cxsr = true;
593
594 if (intel_plane_do_async_flip(plane, old_crtc_state, new_crtc_state)) {
595 new_crtc_state->do_async_flip = true;
596 new_crtc_state->async_flip_planes |= BIT(plane->id);
597 } else if (plane->need_async_flip_toggle_wa &&
598 new_crtc_state->uapi.async_flip) {
599 /*
600 * On platforms with double buffered async flip bit we
601 * set the bit already one frame early during the sync
602 * flip (see {i9xx,skl}_plane_update_arm()). The
603 * hardware will therefore be ready to perform a real
604 * async flip during the next commit, without having
605 * to wait yet another frame for the bit to latch.
606 */
607 new_crtc_state->async_flip_planes |= BIT(plane->id);
608 }
609
610 return 0;
611}
612
613int intel_plane_atomic_check_with_state(const struct intel_crtc_state *old_crtc_state,
614 struct intel_crtc_state *new_crtc_state,
615 const struct intel_plane_state *old_plane_state,
616 struct intel_plane_state *new_plane_state)
617{
618 struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
619 const struct drm_framebuffer *fb = new_plane_state->hw.fb;
620 int ret;
621
622 intel_plane_set_invisible(new_crtc_state, new_plane_state);
623 new_crtc_state->enabled_planes &= ~BIT(plane->id);
624
625 if (!new_plane_state->hw.crtc && !old_plane_state->hw.crtc)
626 return 0;
627
628 ret = plane->check_plane(new_crtc_state, new_plane_state);
629 if (ret)
630 return ret;
631
632 if (fb)
633 new_crtc_state->enabled_planes |= BIT(plane->id);
634
635 /* FIXME pre-g4x don't work like this */
636 if (new_plane_state->uapi.visible)
637 new_crtc_state->active_planes |= BIT(plane->id);
638
639 if (new_plane_state->uapi.visible &&
640 intel_plane_is_scaled(new_plane_state))
641 new_crtc_state->scaled_planes |= BIT(plane->id);
642
643 if (new_plane_state->uapi.visible &&
644 intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
645 new_crtc_state->nv12_planes |= BIT(plane->id);
646
647 if (new_plane_state->uapi.visible &&
648 fb->format->format == DRM_FORMAT_C8)
649 new_crtc_state->c8_planes |= BIT(plane->id);
650
651 if (new_plane_state->uapi.visible || old_plane_state->uapi.visible)
652 new_crtc_state->update_planes |= BIT(plane->id);
653
654 if (new_plane_state->uapi.visible &&
655 intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier)) {
656 new_crtc_state->data_rate_y[plane->id] =
657 intel_plane_data_rate(new_crtc_state, new_plane_state, 0);
658 new_crtc_state->data_rate[plane->id] =
659 intel_plane_data_rate(new_crtc_state, new_plane_state, 1);
660
661 new_crtc_state->rel_data_rate_y[plane->id] =
662 intel_plane_relative_data_rate(new_crtc_state,
663 new_plane_state, 0);
664 new_crtc_state->rel_data_rate[plane->id] =
665 intel_plane_relative_data_rate(new_crtc_state,
666 new_plane_state, 1);
667 } else if (new_plane_state->uapi.visible) {
668 new_crtc_state->data_rate[plane->id] =
669 intel_plane_data_rate(new_crtc_state, new_plane_state, 0);
670
671 new_crtc_state->rel_data_rate[plane->id] =
672 intel_plane_relative_data_rate(new_crtc_state,
673 new_plane_state, 0);
674 }
675
676 return intel_plane_atomic_calc_changes(old_crtc_state, new_crtc_state,
677 old_plane_state, new_plane_state);
678}
679
680static struct intel_plane *
681intel_crtc_get_plane(struct intel_crtc *crtc, enum plane_id plane_id)
682{
683 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
684 struct intel_plane *plane;
685
686 for_each_intel_plane_on_crtc(&i915->drm, crtc, plane) {
687 if (plane->id == plane_id)
688 return plane;
689 }
690
691 return NULL;
692}
693
694int intel_plane_atomic_check(struct intel_atomic_state *state,
695 struct intel_plane *plane)
696{
697 struct intel_display *display = to_intel_display(state);
698 struct intel_plane_state *new_plane_state =
699 intel_atomic_get_new_plane_state(state, plane);
700 const struct intel_plane_state *old_plane_state =
701 intel_atomic_get_old_plane_state(state, plane);
702 const struct intel_plane_state *new_primary_crtc_plane_state;
703 struct intel_crtc *crtc = intel_crtc_for_pipe(display, plane->pipe);
704 const struct intel_crtc_state *old_crtc_state =
705 intel_atomic_get_old_crtc_state(state, crtc);
706 struct intel_crtc_state *new_crtc_state =
707 intel_atomic_get_new_crtc_state(state, crtc);
708
709 if (new_crtc_state && intel_crtc_is_joiner_secondary(new_crtc_state)) {
710 struct intel_crtc *primary_crtc =
711 intel_primary_crtc(new_crtc_state);
712 struct intel_plane *primary_crtc_plane =
713 intel_crtc_get_plane(primary_crtc, plane->id);
714
715 new_primary_crtc_plane_state =
716 intel_atomic_get_new_plane_state(state, primary_crtc_plane);
717 } else {
718 new_primary_crtc_plane_state = new_plane_state;
719 }
720
721 intel_plane_copy_uapi_to_hw_state(new_plane_state,
722 new_primary_crtc_plane_state,
723 crtc);
724
725 new_plane_state->uapi.visible = false;
726 if (!new_crtc_state)
727 return 0;
728
729 return intel_plane_atomic_check_with_state(old_crtc_state,
730 new_crtc_state,
731 old_plane_state,
732 new_plane_state);
733}
734
735static struct intel_plane *
736skl_next_plane_to_commit(struct intel_atomic_state *state,
737 struct intel_crtc *crtc,
738 struct skl_ddb_entry ddb[I915_MAX_PLANES],
739 struct skl_ddb_entry ddb_y[I915_MAX_PLANES],
740 unsigned int *update_mask)
741{
742 struct intel_crtc_state *crtc_state =
743 intel_atomic_get_new_crtc_state(state, crtc);
744 struct intel_plane_state __maybe_unused *plane_state;
745 struct intel_plane *plane;
746 int i;
747
748 if (*update_mask == 0)
749 return NULL;
750
751 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
752 enum plane_id plane_id = plane->id;
753
754 if (crtc->pipe != plane->pipe ||
755 !(*update_mask & BIT(plane_id)))
756 continue;
757
758 if (skl_ddb_allocation_overlaps(&crtc_state->wm.skl.plane_ddb[plane_id],
759 ddb, I915_MAX_PLANES, plane_id) ||
760 skl_ddb_allocation_overlaps(&crtc_state->wm.skl.plane_ddb_y[plane_id],
761 ddb_y, I915_MAX_PLANES, plane_id))
762 continue;
763
764 *update_mask &= ~BIT(plane_id);
765 ddb[plane_id] = crtc_state->wm.skl.plane_ddb[plane_id];
766 ddb_y[plane_id] = crtc_state->wm.skl.plane_ddb_y[plane_id];
767
768 return plane;
769 }
770
771 /* should never happen */
772 drm_WARN_ON(state->base.dev, 1);
773
774 return NULL;
775}
776
777void intel_plane_update_noarm(struct intel_dsb *dsb,
778 struct intel_plane *plane,
779 const struct intel_crtc_state *crtc_state,
780 const struct intel_plane_state *plane_state)
781{
782 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
783
784 trace_intel_plane_update_noarm(plane, crtc);
785
786 if (plane->update_noarm)
787 plane->update_noarm(dsb, plane, crtc_state, plane_state);
788}
789
790void intel_plane_async_flip(struct intel_dsb *dsb,
791 struct intel_plane *plane,
792 const struct intel_crtc_state *crtc_state,
793 const struct intel_plane_state *plane_state,
794 bool async_flip)
795{
796 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
797
798 trace_intel_plane_async_flip(plane, crtc, async_flip);
799 plane->async_flip(dsb, plane, crtc_state, plane_state, async_flip);
800}
801
802void intel_plane_update_arm(struct intel_dsb *dsb,
803 struct intel_plane *plane,
804 const struct intel_crtc_state *crtc_state,
805 const struct intel_plane_state *plane_state)
806{
807 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
808
809 if (crtc_state->do_async_flip && plane->async_flip) {
810 intel_plane_async_flip(dsb, plane, crtc_state, plane_state, true);
811 return;
812 }
813
814 trace_intel_plane_update_arm(plane, crtc);
815 plane->update_arm(dsb, plane, crtc_state, plane_state);
816}
817
818void intel_plane_disable_arm(struct intel_dsb *dsb,
819 struct intel_plane *plane,
820 const struct intel_crtc_state *crtc_state)
821{
822 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
823
824 trace_intel_plane_disable_arm(plane, crtc);
825 plane->disable_arm(dsb, plane, crtc_state);
826}
827
828void intel_crtc_planes_update_noarm(struct intel_dsb *dsb,
829 struct intel_atomic_state *state,
830 struct intel_crtc *crtc)
831{
832 struct intel_crtc_state *new_crtc_state =
833 intel_atomic_get_new_crtc_state(state, crtc);
834 u32 update_mask = new_crtc_state->update_planes;
835 struct intel_plane_state *new_plane_state;
836 struct intel_plane *plane;
837 int i;
838
839 if (new_crtc_state->do_async_flip)
840 return;
841
842 /*
843 * Since we only write non-arming registers here,
844 * the order does not matter even for skl+.
845 */
846 for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) {
847 if (crtc->pipe != plane->pipe ||
848 !(update_mask & BIT(plane->id)))
849 continue;
850
851 /* TODO: for mailbox updates this should be skipped */
852 if (new_plane_state->uapi.visible ||
853 new_plane_state->planar_slave)
854 intel_plane_update_noarm(dsb, plane,
855 new_crtc_state, new_plane_state);
856 }
857}
858
859static void skl_crtc_planes_update_arm(struct intel_dsb *dsb,
860 struct intel_atomic_state *state,
861 struct intel_crtc *crtc)
862{
863 struct intel_crtc_state *old_crtc_state =
864 intel_atomic_get_old_crtc_state(state, crtc);
865 struct intel_crtc_state *new_crtc_state =
866 intel_atomic_get_new_crtc_state(state, crtc);
867 struct skl_ddb_entry ddb[I915_MAX_PLANES];
868 struct skl_ddb_entry ddb_y[I915_MAX_PLANES];
869 u32 update_mask = new_crtc_state->update_planes;
870 struct intel_plane *plane;
871
872 memcpy(ddb, old_crtc_state->wm.skl.plane_ddb,
873 sizeof(old_crtc_state->wm.skl.plane_ddb));
874 memcpy(ddb_y, old_crtc_state->wm.skl.plane_ddb_y,
875 sizeof(old_crtc_state->wm.skl.plane_ddb_y));
876
877 while ((plane = skl_next_plane_to_commit(state, crtc, ddb, ddb_y, &update_mask))) {
878 struct intel_plane_state *new_plane_state =
879 intel_atomic_get_new_plane_state(state, plane);
880
881 /*
882 * TODO: for mailbox updates intel_plane_update_noarm()
883 * would have to be called here as well.
884 */
885 if (new_plane_state->uapi.visible ||
886 new_plane_state->planar_slave)
887 intel_plane_update_arm(dsb, plane, new_crtc_state, new_plane_state);
888 else
889 intel_plane_disable_arm(dsb, plane, new_crtc_state);
890 }
891}
892
893static void i9xx_crtc_planes_update_arm(struct intel_dsb *dsb,
894 struct intel_atomic_state *state,
895 struct intel_crtc *crtc)
896{
897 struct intel_crtc_state *new_crtc_state =
898 intel_atomic_get_new_crtc_state(state, crtc);
899 u32 update_mask = new_crtc_state->update_planes;
900 struct intel_plane_state *new_plane_state;
901 struct intel_plane *plane;
902 int i;
903
904 for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) {
905 if (crtc->pipe != plane->pipe ||
906 !(update_mask & BIT(plane->id)))
907 continue;
908
909 /*
910 * TODO: for mailbox updates intel_plane_update_noarm()
911 * would have to be called here as well.
912 */
913 if (new_plane_state->uapi.visible)
914 intel_plane_update_arm(dsb, plane, new_crtc_state, new_plane_state);
915 else
916 intel_plane_disable_arm(dsb, plane, new_crtc_state);
917 }
918}
919
920void intel_crtc_planes_update_arm(struct intel_dsb *dsb,
921 struct intel_atomic_state *state,
922 struct intel_crtc *crtc)
923{
924 struct drm_i915_private *i915 = to_i915(state->base.dev);
925
926 if (DISPLAY_VER(i915) >= 9)
927 skl_crtc_planes_update_arm(dsb, state, crtc);
928 else
929 i9xx_crtc_planes_update_arm(dsb, state, crtc);
930}
931
932int intel_atomic_plane_check_clipping(struct intel_plane_state *plane_state,
933 struct intel_crtc_state *crtc_state,
934 int min_scale, int max_scale,
935 bool can_position)
936{
937 struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
938 struct drm_framebuffer *fb = plane_state->hw.fb;
939 struct drm_rect *src = &plane_state->uapi.src;
940 struct drm_rect *dst = &plane_state->uapi.dst;
941 const struct drm_rect *clip = &crtc_state->pipe_src;
942 unsigned int rotation = plane_state->hw.rotation;
943 int hscale, vscale;
944
945 if (!fb) {
946 plane_state->uapi.visible = false;
947 return 0;
948 }
949
950 drm_rect_rotate(src, fb->width << 16, fb->height << 16, rotation);
951
952 /* Check scaling */
953 hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
954 vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
955 if (hscale < 0 || vscale < 0) {
956 drm_dbg_kms(&i915->drm, "Invalid scaling of plane\n");
957 drm_rect_debug_print("src: ", src, true);
958 drm_rect_debug_print("dst: ", dst, false);
959 return -ERANGE;
960 }
961
962 /*
963 * FIXME: This might need further adjustment for seamless scaling
964 * with phase information, for the 2p2 and 2p1 scenarios.
965 */
966 plane_state->uapi.visible = drm_rect_clip_scaled(src, dst, clip);
967
968 drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16, rotation);
969
970 if (!can_position && plane_state->uapi.visible &&
971 !drm_rect_equals(dst, clip)) {
972 drm_dbg_kms(&i915->drm, "Plane must cover entire CRTC\n");
973 drm_rect_debug_print("dst: ", dst, false);
974 drm_rect_debug_print("clip: ", clip, false);
975 return -EINVAL;
976 }
977
978 /* final plane coordinates will be relative to the plane's pipe */
979 drm_rect_translate(dst, -clip->x1, -clip->y1);
980
981 return 0;
982}
983
984int intel_plane_check_src_coordinates(struct intel_plane_state *plane_state)
985{
986 struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
987 const struct drm_framebuffer *fb = plane_state->hw.fb;
988 struct drm_rect *src = &plane_state->uapi.src;
989 u32 src_x, src_y, src_w, src_h, hsub, vsub;
990 bool rotated = drm_rotation_90_or_270(plane_state->hw.rotation);
991
992 /*
993 * FIXME hsub/vsub vs. block size is a mess. Pre-tgl CCS
994 * abuses hsub/vsub so we can't use them here. But as they
995 * are limited to 32bpp RGB formats we don't actually need
996 * to check anything.
997 */
998 if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
999 fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS)
1000 return 0;
1001
1002 /*
1003 * Hardware doesn't handle subpixel coordinates.
1004 * Adjust to (macro)pixel boundary, but be careful not to
1005 * increase the source viewport size, because that could
1006 * push the downscaling factor out of bounds.
1007 */
1008 src_x = src->x1 >> 16;
1009 src_w = drm_rect_width(src) >> 16;
1010 src_y = src->y1 >> 16;
1011 src_h = drm_rect_height(src) >> 16;
1012
1013 drm_rect_init(src, src_x << 16, src_y << 16,
1014 src_w << 16, src_h << 16);
1015
1016 if (fb->format->format == DRM_FORMAT_RGB565 && rotated) {
1017 hsub = 2;
1018 vsub = 2;
1019 } else if (DISPLAY_VER(i915) >= 20 &&
1020 intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier)) {
1021 /*
1022 * This allows NV12 and P0xx formats to have odd size and/or odd
1023 * source coordinates on DISPLAY_VER(i915) >= 20
1024 */
1025 hsub = 1;
1026 vsub = 1;
1027
1028 /* Wa_16023981245 */
1029 if ((DISPLAY_VERx100(i915) == 2000 ||
1030 DISPLAY_VERx100(i915) == 3000) &&
1031 src_x % 2 != 0)
1032 hsub = 2;
1033 } else {
1034 hsub = fb->format->hsub;
1035 vsub = fb->format->vsub;
1036 }
1037
1038 if (rotated)
1039 hsub = vsub = max(hsub, vsub);
1040
1041 if (src_x % hsub || src_w % hsub) {
1042 drm_dbg_kms(&i915->drm, "src x/w (%u, %u) must be a multiple of %u (rotated: %s)\n",
1043 src_x, src_w, hsub, str_yes_no(rotated));
1044 return -EINVAL;
1045 }
1046
1047 if (src_y % vsub || src_h % vsub) {
1048 drm_dbg_kms(&i915->drm, "src y/h (%u, %u) must be a multiple of %u (rotated: %s)\n",
1049 src_y, src_h, vsub, str_yes_no(rotated));
1050 return -EINVAL;
1051 }
1052
1053 return 0;
1054}
1055
1056static int add_dma_resv_fences(struct dma_resv *resv,
1057 struct drm_plane_state *new_plane_state)
1058{
1059 struct dma_fence *fence = dma_fence_get(new_plane_state->fence);
1060 struct dma_fence *new;
1061 int ret;
1062
1063 ret = dma_resv_get_singleton(resv, dma_resv_usage_rw(false), &new);
1064 if (ret)
1065 goto error;
1066
1067 if (new && fence) {
1068 struct dma_fence_chain *chain = dma_fence_chain_alloc();
1069
1070 if (!chain) {
1071 ret = -ENOMEM;
1072 goto error;
1073 }
1074
1075 dma_fence_chain_init(chain, fence, new, 1);
1076 fence = &chain->base;
1077
1078 } else if (new) {
1079 fence = new;
1080 }
1081
1082 dma_fence_put(new_plane_state->fence);
1083 new_plane_state->fence = fence;
1084 return 0;
1085
1086error:
1087 dma_fence_put(fence);
1088 return ret;
1089}
1090
1091/**
1092 * intel_prepare_plane_fb - Prepare fb for usage on plane
1093 * @_plane: drm plane to prepare for
1094 * @_new_plane_state: the plane state being prepared
1095 *
1096 * Prepares a framebuffer for usage on a display plane. Generally this
1097 * involves pinning the underlying object and updating the frontbuffer tracking
1098 * bits. Some older platforms need special physical address handling for
1099 * cursor planes.
1100 *
1101 * Returns 0 on success, negative error code on failure.
1102 */
1103static int
1104intel_prepare_plane_fb(struct drm_plane *_plane,
1105 struct drm_plane_state *_new_plane_state)
1106{
1107 struct i915_sched_attr attr = { .priority = I915_PRIORITY_DISPLAY };
1108 struct intel_plane *plane = to_intel_plane(_plane);
1109 struct intel_plane_state *new_plane_state =
1110 to_intel_plane_state(_new_plane_state);
1111 struct intel_atomic_state *state =
1112 to_intel_atomic_state(new_plane_state->uapi.state);
1113 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1114 struct intel_plane_state *old_plane_state =
1115 intel_atomic_get_old_plane_state(state, plane);
1116 struct drm_gem_object *obj = intel_fb_bo(new_plane_state->hw.fb);
1117 struct drm_gem_object *old_obj = intel_fb_bo(old_plane_state->hw.fb);
1118 int ret;
1119
1120 if (old_obj) {
1121 const struct intel_crtc_state *new_crtc_state =
1122 intel_atomic_get_new_crtc_state(state,
1123 to_intel_crtc(old_plane_state->hw.crtc));
1124
1125 /* Big Hammer, we also need to ensure that any pending
1126 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
1127 * current scanout is retired before unpinning the old
1128 * framebuffer. Note that we rely on userspace rendering
1129 * into the buffer attached to the pipe they are waiting
1130 * on. If not, userspace generates a GPU hang with IPEHR
1131 * point to the MI_WAIT_FOR_EVENT.
1132 *
1133 * This should only fail upon a hung GPU, in which case we
1134 * can safely continue.
1135 */
1136 if (new_crtc_state && intel_crtc_needs_modeset(new_crtc_state)) {
1137 ret = add_dma_resv_fences(old_obj->resv,
1138 &new_plane_state->uapi);
1139 if (ret < 0)
1140 return ret;
1141 }
1142 }
1143
1144 if (!obj)
1145 return 0;
1146
1147 ret = intel_plane_pin_fb(new_plane_state);
1148 if (ret)
1149 return ret;
1150
1151 ret = drm_gem_plane_helper_prepare_fb(&plane->base, &new_plane_state->uapi);
1152 if (ret < 0)
1153 goto unpin_fb;
1154
1155 if (new_plane_state->uapi.fence) {
1156 i915_gem_fence_wait_priority(new_plane_state->uapi.fence,
1157 &attr);
1158
1159 intel_display_rps_boost_after_vblank(new_plane_state->hw.crtc,
1160 new_plane_state->uapi.fence);
1161 }
1162
1163 /*
1164 * We declare pageflips to be interactive and so merit a small bias
1165 * towards upclocking to deliver the frame on time. By only changing
1166 * the RPS thresholds to sample more regularly and aim for higher
1167 * clocks we can hopefully deliver low power workloads (like kodi)
1168 * that are not quite steady state without resorting to forcing
1169 * maximum clocks following a vblank miss (see do_rps_boost()).
1170 */
1171 intel_display_rps_mark_interactive(dev_priv, state, true);
1172
1173 return 0;
1174
1175unpin_fb:
1176 intel_plane_unpin_fb(new_plane_state);
1177
1178 return ret;
1179}
1180
1181/**
1182 * intel_cleanup_plane_fb - Cleans up an fb after plane use
1183 * @plane: drm plane to clean up for
1184 * @_old_plane_state: the state from the previous modeset
1185 *
1186 * Cleans up a framebuffer that has just been removed from a plane.
1187 */
1188static void
1189intel_cleanup_plane_fb(struct drm_plane *plane,
1190 struct drm_plane_state *_old_plane_state)
1191{
1192 struct intel_plane_state *old_plane_state =
1193 to_intel_plane_state(_old_plane_state);
1194 struct intel_atomic_state *state =
1195 to_intel_atomic_state(old_plane_state->uapi.state);
1196 struct drm_i915_private *dev_priv = to_i915(plane->dev);
1197 struct drm_gem_object *obj = intel_fb_bo(old_plane_state->hw.fb);
1198
1199 if (!obj)
1200 return;
1201
1202 intel_display_rps_mark_interactive(dev_priv, state, false);
1203
1204 intel_plane_unpin_fb(old_plane_state);
1205}
1206
1207static const struct drm_plane_helper_funcs intel_plane_helper_funcs = {
1208 .prepare_fb = intel_prepare_plane_fb,
1209 .cleanup_fb = intel_cleanup_plane_fb,
1210};
1211
1212void intel_plane_helper_add(struct intel_plane *plane)
1213{
1214 drm_plane_helper_add(&plane->base, &intel_plane_helper_funcs);
1215}
1216
1217void intel_plane_init_cursor_vblank_work(struct intel_plane_state *old_plane_state,
1218 struct intel_plane_state *new_plane_state)
1219{
1220 if (!old_plane_state->ggtt_vma ||
1221 old_plane_state->ggtt_vma == new_plane_state->ggtt_vma)
1222 return;
1223
1224 drm_vblank_work_init(&old_plane_state->unpin_work, old_plane_state->uapi.crtc,
1225 intel_cursor_unpin_work);
1226}