2 * Copyright © 2014 Intel Corporation
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:
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
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.
25 * DOC: atomic plane helpers
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.
34 #include <drm/drm_atomic_helper.h>
35 #include <drm/drm_fourcc.h>
36 #include <drm/drm_plane_helper.h>
38 #include "gt/intel_rps.h"
40 #include "intel_atomic_plane.h"
41 #include "intel_cdclk.h"
42 #include "intel_display_trace.h"
43 #include "intel_display_types.h"
45 #include "intel_fb_pin.h"
47 #include "intel_sprite.h"
48 #include "skl_scaler.h"
50 static void intel_plane_state_reset(struct intel_plane_state *plane_state,
51 struct intel_plane *plane)
53 memset(plane_state, 0, sizeof(*plane_state));
55 __drm_atomic_helper_plane_state_reset(&plane_state->uapi, &plane->base);
57 plane_state->scaler_id = -1;
60 struct intel_plane *intel_plane_alloc(void)
62 struct intel_plane_state *plane_state;
63 struct intel_plane *plane;
65 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
67 return ERR_PTR(-ENOMEM);
69 plane_state = kzalloc(sizeof(*plane_state), GFP_KERNEL);
72 return ERR_PTR(-ENOMEM);
75 intel_plane_state_reset(plane_state, plane);
77 plane->base.state = &plane_state->uapi;
82 void intel_plane_free(struct intel_plane *plane)
84 intel_plane_destroy_state(&plane->base, plane->base.state);
89 * intel_plane_duplicate_state - duplicate plane state
92 * Allocates and returns a copy of the plane state (both common and
93 * Intel-specific) for the specified plane.
95 * Returns: The newly allocated plane state, or NULL on failure.
97 struct drm_plane_state *
98 intel_plane_duplicate_state(struct drm_plane *plane)
100 struct intel_plane_state *intel_state;
102 intel_state = to_intel_plane_state(plane->state);
103 intel_state = kmemdup(intel_state, sizeof(*intel_state), GFP_KERNEL);
108 __drm_atomic_helper_plane_duplicate_state(plane, &intel_state->uapi);
110 intel_state->ggtt_vma = NULL;
111 intel_state->dpt_vma = NULL;
112 intel_state->flags = 0;
114 /* add reference to fb */
115 if (intel_state->hw.fb)
116 drm_framebuffer_get(intel_state->hw.fb);
118 return &intel_state->uapi;
122 * intel_plane_destroy_state - destroy plane state
124 * @state: state object to destroy
126 * Destroys the plane state (both common and Intel-specific) for the
130 intel_plane_destroy_state(struct drm_plane *plane,
131 struct drm_plane_state *state)
133 struct intel_plane_state *plane_state = to_intel_plane_state(state);
135 drm_WARN_ON(plane->dev, plane_state->ggtt_vma);
136 drm_WARN_ON(plane->dev, plane_state->dpt_vma);
138 __drm_atomic_helper_plane_destroy_state(&plane_state->uapi);
139 if (plane_state->hw.fb)
140 drm_framebuffer_put(plane_state->hw.fb);
144 unsigned int intel_adjusted_rate(const struct drm_rect *src,
145 const struct drm_rect *dst,
148 unsigned int src_w, src_h, dst_w, dst_h;
150 src_w = drm_rect_width(src) >> 16;
151 src_h = drm_rect_height(src) >> 16;
152 dst_w = drm_rect_width(dst);
153 dst_h = drm_rect_height(dst);
155 /* Downscaling limits the maximum pixel rate */
156 dst_w = min(src_w, dst_w);
157 dst_h = min(src_h, dst_h);
159 return DIV_ROUND_UP_ULL(mul_u32_u32(rate, src_w * src_h),
163 unsigned int intel_plane_pixel_rate(const struct intel_crtc_state *crtc_state,
164 const struct intel_plane_state *plane_state)
167 * Note we don't check for plane visibility here as
168 * we want to use this when calculating the cursor
169 * watermarks even if the cursor is fully offscreen.
170 * That depends on the src/dst rectangles being
171 * correctly populated whenever the watermark code
172 * considers the cursor to be visible, whether or not
173 * it is actually visible.
175 * See: intel_wm_plane_visible() and intel_check_cursor()
178 return intel_adjusted_rate(&plane_state->uapi.src,
179 &plane_state->uapi.dst,
180 crtc_state->pixel_rate);
183 unsigned int intel_plane_data_rate(const struct intel_crtc_state *crtc_state,
184 const struct intel_plane_state *plane_state,
187 const struct drm_framebuffer *fb = plane_state->hw.fb;
189 if (!plane_state->uapi.visible)
192 return intel_plane_pixel_rate(crtc_state, plane_state) *
193 fb->format->cpp[color_plane];
197 use_min_ddb(const struct intel_crtc_state *crtc_state,
198 struct intel_plane *plane)
200 struct drm_i915_private *i915 = to_i915(plane->base.dev);
202 return DISPLAY_VER(i915) >= 13 &&
203 crtc_state->uapi.async_flip &&
208 intel_plane_relative_data_rate(const struct intel_crtc_state *crtc_state,
209 const struct intel_plane_state *plane_state,
212 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
213 const struct drm_framebuffer *fb = plane_state->hw.fb;
216 if (plane->id == PLANE_CURSOR)
219 if (!plane_state->uapi.visible)
223 * We calculate extra ddb based on ratio plane rate/total data rate
224 * in case, in some cases we should not allocate extra ddb for the plane,
225 * so do not count its data rate, if this is the case.
227 if (use_min_ddb(crtc_state, plane))
231 * Src coordinates are already rotated by 270 degrees for
232 * the 90/270 degree plane rotation cases (to match the
233 * GTT mapping), hence no need to account for rotation here.
235 width = drm_rect_width(&plane_state->uapi.src) >> 16;
236 height = drm_rect_height(&plane_state->uapi.src) >> 16;
238 /* UV plane does 1/2 pixel sub-sampling */
239 if (color_plane == 1) {
244 return width * height * fb->format->cpp[color_plane];
247 int intel_plane_calc_min_cdclk(struct intel_atomic_state *state,
248 struct intel_plane *plane,
249 bool *need_cdclk_calc)
251 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
252 const struct intel_plane_state *plane_state =
253 intel_atomic_get_new_plane_state(state, plane);
254 struct intel_crtc *crtc = to_intel_crtc(plane_state->hw.crtc);
255 const struct intel_cdclk_state *cdclk_state;
256 const struct intel_crtc_state *old_crtc_state;
257 struct intel_crtc_state *new_crtc_state;
259 if (!plane_state->uapi.visible || !plane->min_cdclk)
262 old_crtc_state = intel_atomic_get_old_crtc_state(state, crtc);
263 new_crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
265 new_crtc_state->min_cdclk[plane->id] =
266 plane->min_cdclk(new_crtc_state, plane_state);
269 * No need to check against the cdclk state if
270 * the min cdclk for the plane doesn't increase.
272 * Ie. we only ever increase the cdclk due to plane
273 * requirements. This can reduce back and forth
274 * display blinking due to constant cdclk changes.
276 if (new_crtc_state->min_cdclk[plane->id] <=
277 old_crtc_state->min_cdclk[plane->id])
280 cdclk_state = intel_atomic_get_cdclk_state(state);
281 if (IS_ERR(cdclk_state))
282 return PTR_ERR(cdclk_state);
285 * No need to recalculate the cdclk state if
286 * the min cdclk for the pipe doesn't increase.
288 * Ie. we only ever increase the cdclk due to plane
289 * requirements. This can reduce back and forth
290 * display blinking due to constant cdclk changes.
292 if (new_crtc_state->min_cdclk[plane->id] <=
293 cdclk_state->min_cdclk[crtc->pipe])
296 drm_dbg_kms(&dev_priv->drm,
297 "[PLANE:%d:%s] min cdclk (%d kHz) > [CRTC:%d:%s] min cdclk (%d kHz)\n",
298 plane->base.base.id, plane->base.name,
299 new_crtc_state->min_cdclk[plane->id],
300 crtc->base.base.id, crtc->base.name,
301 cdclk_state->min_cdclk[crtc->pipe]);
302 *need_cdclk_calc = true;
307 static void intel_plane_clear_hw_state(struct intel_plane_state *plane_state)
309 if (plane_state->hw.fb)
310 drm_framebuffer_put(plane_state->hw.fb);
312 memset(&plane_state->hw, 0, sizeof(plane_state->hw));
315 void intel_plane_copy_uapi_to_hw_state(struct intel_plane_state *plane_state,
316 const struct intel_plane_state *from_plane_state,
317 struct intel_crtc *crtc)
319 intel_plane_clear_hw_state(plane_state);
322 * For the bigjoiner slave uapi.crtc will point at
323 * the master crtc. So we explicitly assign the right
324 * slave crtc to hw.crtc. uapi.crtc!=NULL simply indicates
325 * the plane is logically enabled on the uapi level.
327 plane_state->hw.crtc = from_plane_state->uapi.crtc ? &crtc->base : NULL;
329 plane_state->hw.fb = from_plane_state->uapi.fb;
330 if (plane_state->hw.fb)
331 drm_framebuffer_get(plane_state->hw.fb);
333 plane_state->hw.alpha = from_plane_state->uapi.alpha;
334 plane_state->hw.pixel_blend_mode =
335 from_plane_state->uapi.pixel_blend_mode;
336 plane_state->hw.rotation = from_plane_state->uapi.rotation;
337 plane_state->hw.color_encoding = from_plane_state->uapi.color_encoding;
338 plane_state->hw.color_range = from_plane_state->uapi.color_range;
339 plane_state->hw.scaling_filter = from_plane_state->uapi.scaling_filter;
341 plane_state->uapi.src = drm_plane_state_src(&from_plane_state->uapi);
342 plane_state->uapi.dst = drm_plane_state_dest(&from_plane_state->uapi);
345 void intel_plane_copy_hw_state(struct intel_plane_state *plane_state,
346 const struct intel_plane_state *from_plane_state)
348 intel_plane_clear_hw_state(plane_state);
350 memcpy(&plane_state->hw, &from_plane_state->hw,
351 sizeof(plane_state->hw));
353 if (plane_state->hw.fb)
354 drm_framebuffer_get(plane_state->hw.fb);
357 void intel_plane_set_invisible(struct intel_crtc_state *crtc_state,
358 struct intel_plane_state *plane_state)
360 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
362 crtc_state->active_planes &= ~BIT(plane->id);
363 crtc_state->scaled_planes &= ~BIT(plane->id);
364 crtc_state->nv12_planes &= ~BIT(plane->id);
365 crtc_state->c8_planes &= ~BIT(plane->id);
366 crtc_state->data_rate[plane->id] = 0;
367 crtc_state->data_rate_y[plane->id] = 0;
368 crtc_state->rel_data_rate[plane->id] = 0;
369 crtc_state->rel_data_rate_y[plane->id] = 0;
370 crtc_state->min_cdclk[plane->id] = 0;
372 plane_state->uapi.visible = false;
375 /* FIXME nuke when all wm code is atomic */
376 static bool intel_wm_need_update(const struct intel_plane_state *cur,
377 struct intel_plane_state *new)
379 /* Update watermarks on tiling or size changes. */
380 if (new->uapi.visible != cur->uapi.visible)
383 if (!cur->hw.fb || !new->hw.fb)
386 if (cur->hw.fb->modifier != new->hw.fb->modifier ||
387 cur->hw.rotation != new->hw.rotation ||
388 drm_rect_width(&new->uapi.src) != drm_rect_width(&cur->uapi.src) ||
389 drm_rect_height(&new->uapi.src) != drm_rect_height(&cur->uapi.src) ||
390 drm_rect_width(&new->uapi.dst) != drm_rect_width(&cur->uapi.dst) ||
391 drm_rect_height(&new->uapi.dst) != drm_rect_height(&cur->uapi.dst))
397 static bool intel_plane_is_scaled(const struct intel_plane_state *plane_state)
399 int src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
400 int src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
401 int dst_w = drm_rect_width(&plane_state->uapi.dst);
402 int dst_h = drm_rect_height(&plane_state->uapi.dst);
404 return src_w != dst_w || src_h != dst_h;
407 static bool intel_plane_do_async_flip(struct intel_plane *plane,
408 const struct intel_crtc_state *old_crtc_state,
409 const struct intel_crtc_state *new_crtc_state)
411 struct drm_i915_private *i915 = to_i915(plane->base.dev);
413 if (!plane->async_flip)
416 if (!new_crtc_state->uapi.async_flip)
420 * In platforms after DISPLAY13, we might need to override
421 * first async flip in order to change watermark levels
422 * as part of optimization.
423 * So for those, we are checking if this is a first async flip.
424 * For platforms earlier than DISPLAY13 we always do async flip.
426 return DISPLAY_VER(i915) < 13 || old_crtc_state->uapi.async_flip;
429 static int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
430 struct intel_crtc_state *new_crtc_state,
431 const struct intel_plane_state *old_plane_state,
432 struct intel_plane_state *new_plane_state)
434 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
435 struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
436 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
437 bool mode_changed = intel_crtc_needs_modeset(new_crtc_state);
438 bool was_crtc_enabled = old_crtc_state->hw.active;
439 bool is_crtc_enabled = new_crtc_state->hw.active;
440 bool turn_off, turn_on, visible, was_visible;
443 if (DISPLAY_VER(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
444 ret = skl_update_scaler_plane(new_crtc_state, new_plane_state);
449 was_visible = old_plane_state->uapi.visible;
450 visible = new_plane_state->uapi.visible;
452 if (!was_crtc_enabled && drm_WARN_ON(&dev_priv->drm, was_visible))
456 * Visibility is calculated as if the crtc was on, but
457 * after scaler setup everything depends on it being off
458 * when the crtc isn't active.
460 * FIXME this is wrong for watermarks. Watermarks should also
461 * be computed as if the pipe would be active. Perhaps move
462 * per-plane wm computation to the .check_plane() hook, and
463 * only combine the results from all planes in the current place?
465 if (!is_crtc_enabled) {
466 intel_plane_set_invisible(new_crtc_state, new_plane_state);
470 if (!was_visible && !visible)
473 turn_off = was_visible && (!visible || mode_changed);
474 turn_on = visible && (!was_visible || mode_changed);
476 drm_dbg_atomic(&dev_priv->drm,
477 "[CRTC:%d:%s] with [PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
478 crtc->base.base.id, crtc->base.name,
479 plane->base.base.id, plane->base.name,
480 was_visible, visible,
481 turn_off, turn_on, mode_changed);
484 if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv))
485 new_crtc_state->update_wm_pre = true;
487 /* must disable cxsr around plane enable/disable */
488 if (plane->id != PLANE_CURSOR)
489 new_crtc_state->disable_cxsr = true;
490 } else if (turn_off) {
491 if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv))
492 new_crtc_state->update_wm_post = true;
494 /* must disable cxsr around plane enable/disable */
495 if (plane->id != PLANE_CURSOR)
496 new_crtc_state->disable_cxsr = true;
497 } else if (intel_wm_need_update(old_plane_state, new_plane_state)) {
498 if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv)) {
500 new_crtc_state->update_wm_pre = true;
501 new_crtc_state->update_wm_post = true;
505 if (visible || was_visible)
506 new_crtc_state->fb_bits |= plane->frontbuffer_bit;
509 * ILK/SNB DVSACNTR/Sprite Enable
510 * IVB SPR_CTL/Sprite Enable
511 * "When in Self Refresh Big FIFO mode, a write to enable the
512 * plane will be internally buffered and delayed while Big FIFO
515 * Which means that enabling the sprite can take an extra frame
516 * when we start in big FIFO mode (LP1+). Thus we need to drop
517 * down to LP0 and wait for vblank in order to make sure the
518 * sprite gets enabled on the next vblank after the register write.
519 * Doing otherwise would risk enabling the sprite one frame after
520 * we've already signalled flip completion. We can resume LP1+
521 * once the sprite has been enabled.
524 * WaCxSRDisabledForSpriteScaling:ivb
525 * IVB SPR_SCALE/Scaling Enable
526 * "Low Power watermarks must be disabled for at least one
527 * frame before enabling sprite scaling, and kept disabled
528 * until sprite scaling is disabled."
530 * ILK/SNB DVSASCALE/Scaling Enable
531 * "When in Self Refresh Big FIFO mode, scaling enable will be
532 * masked off while Big FIFO mode is exiting."
534 * Despite the w/a only being listed for IVB we assume that
535 * the ILK/SNB note has similar ramifications, hence we apply
536 * the w/a on all three platforms.
538 * With experimental results seems this is needed also for primary
539 * plane, not only sprite plane.
541 if (plane->id != PLANE_CURSOR &&
542 (IS_IRONLAKE(dev_priv) || IS_SANDYBRIDGE(dev_priv) ||
543 IS_IVYBRIDGE(dev_priv)) &&
544 (turn_on || (!intel_plane_is_scaled(old_plane_state) &&
545 intel_plane_is_scaled(new_plane_state))))
546 new_crtc_state->disable_lp_wm = true;
548 if (intel_plane_do_async_flip(plane, old_crtc_state, new_crtc_state))
549 new_crtc_state->do_async_flip = true;
554 int intel_plane_atomic_check_with_state(const struct intel_crtc_state *old_crtc_state,
555 struct intel_crtc_state *new_crtc_state,
556 const struct intel_plane_state *old_plane_state,
557 struct intel_plane_state *new_plane_state)
559 struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
560 const struct drm_framebuffer *fb = new_plane_state->hw.fb;
563 intel_plane_set_invisible(new_crtc_state, new_plane_state);
564 new_crtc_state->enabled_planes &= ~BIT(plane->id);
566 if (!new_plane_state->hw.crtc && !old_plane_state->hw.crtc)
569 ret = plane->check_plane(new_crtc_state, new_plane_state);
574 new_crtc_state->enabled_planes |= BIT(plane->id);
576 /* FIXME pre-g4x don't work like this */
577 if (new_plane_state->uapi.visible)
578 new_crtc_state->active_planes |= BIT(plane->id);
580 if (new_plane_state->uapi.visible &&
581 intel_plane_is_scaled(new_plane_state))
582 new_crtc_state->scaled_planes |= BIT(plane->id);
584 if (new_plane_state->uapi.visible &&
585 intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
586 new_crtc_state->nv12_planes |= BIT(plane->id);
588 if (new_plane_state->uapi.visible &&
589 fb->format->format == DRM_FORMAT_C8)
590 new_crtc_state->c8_planes |= BIT(plane->id);
592 if (new_plane_state->uapi.visible || old_plane_state->uapi.visible)
593 new_crtc_state->update_planes |= BIT(plane->id);
595 if (new_plane_state->uapi.visible &&
596 intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier)) {
597 new_crtc_state->data_rate_y[plane->id] =
598 intel_plane_data_rate(new_crtc_state, new_plane_state, 0);
599 new_crtc_state->data_rate[plane->id] =
600 intel_plane_data_rate(new_crtc_state, new_plane_state, 1);
602 new_crtc_state->rel_data_rate_y[plane->id] =
603 intel_plane_relative_data_rate(new_crtc_state,
605 new_crtc_state->rel_data_rate[plane->id] =
606 intel_plane_relative_data_rate(new_crtc_state,
608 } else if (new_plane_state->uapi.visible) {
609 new_crtc_state->data_rate[plane->id] =
610 intel_plane_data_rate(new_crtc_state, new_plane_state, 0);
612 new_crtc_state->rel_data_rate[plane->id] =
613 intel_plane_relative_data_rate(new_crtc_state,
617 return intel_plane_atomic_calc_changes(old_crtc_state, new_crtc_state,
618 old_plane_state, new_plane_state);
621 static struct intel_plane *
622 intel_crtc_get_plane(struct intel_crtc *crtc, enum plane_id plane_id)
624 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
625 struct intel_plane *plane;
627 for_each_intel_plane_on_crtc(&i915->drm, crtc, plane) {
628 if (plane->id == plane_id)
635 int intel_plane_atomic_check(struct intel_atomic_state *state,
636 struct intel_plane *plane)
638 struct drm_i915_private *i915 = to_i915(state->base.dev);
639 struct intel_plane_state *new_plane_state =
640 intel_atomic_get_new_plane_state(state, plane);
641 const struct intel_plane_state *old_plane_state =
642 intel_atomic_get_old_plane_state(state, plane);
643 const struct intel_plane_state *new_master_plane_state;
644 struct intel_crtc *crtc = intel_crtc_for_pipe(i915, plane->pipe);
645 const struct intel_crtc_state *old_crtc_state =
646 intel_atomic_get_old_crtc_state(state, crtc);
647 struct intel_crtc_state *new_crtc_state =
648 intel_atomic_get_new_crtc_state(state, crtc);
650 if (new_crtc_state && intel_crtc_is_bigjoiner_slave(new_crtc_state)) {
651 struct intel_crtc *master_crtc =
652 intel_master_crtc(new_crtc_state);
653 struct intel_plane *master_plane =
654 intel_crtc_get_plane(master_crtc, plane->id);
656 new_master_plane_state =
657 intel_atomic_get_new_plane_state(state, master_plane);
659 new_master_plane_state = new_plane_state;
662 intel_plane_copy_uapi_to_hw_state(new_plane_state,
663 new_master_plane_state,
666 new_plane_state->uapi.visible = false;
670 return intel_plane_atomic_check_with_state(old_crtc_state,
676 static struct intel_plane *
677 skl_next_plane_to_commit(struct intel_atomic_state *state,
678 struct intel_crtc *crtc,
679 struct skl_ddb_entry ddb[I915_MAX_PLANES],
680 struct skl_ddb_entry ddb_y[I915_MAX_PLANES],
681 unsigned int *update_mask)
683 struct intel_crtc_state *crtc_state =
684 intel_atomic_get_new_crtc_state(state, crtc);
685 struct intel_plane_state *plane_state;
686 struct intel_plane *plane;
689 if (*update_mask == 0)
692 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
693 enum plane_id plane_id = plane->id;
695 if (crtc->pipe != plane->pipe ||
696 !(*update_mask & BIT(plane_id)))
699 if (skl_ddb_allocation_overlaps(&crtc_state->wm.skl.plane_ddb[plane_id],
700 ddb, I915_MAX_PLANES, plane_id) ||
701 skl_ddb_allocation_overlaps(&crtc_state->wm.skl.plane_ddb_y[plane_id],
702 ddb_y, I915_MAX_PLANES, plane_id))
705 *update_mask &= ~BIT(plane_id);
706 ddb[plane_id] = crtc_state->wm.skl.plane_ddb[plane_id];
707 ddb_y[plane_id] = crtc_state->wm.skl.plane_ddb_y[plane_id];
712 /* should never happen */
713 drm_WARN_ON(state->base.dev, 1);
718 void intel_plane_update_noarm(struct intel_plane *plane,
719 const struct intel_crtc_state *crtc_state,
720 const struct intel_plane_state *plane_state)
722 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
724 trace_intel_plane_update_noarm(&plane->base, crtc);
726 if (plane->update_noarm)
727 plane->update_noarm(plane, crtc_state, plane_state);
730 void intel_plane_update_arm(struct intel_plane *plane,
731 const struct intel_crtc_state *crtc_state,
732 const struct intel_plane_state *plane_state)
734 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
736 trace_intel_plane_update_arm(&plane->base, crtc);
738 if (crtc_state->do_async_flip && plane->async_flip)
739 plane->async_flip(plane, crtc_state, plane_state, true);
741 plane->update_arm(plane, crtc_state, plane_state);
744 void intel_plane_disable_arm(struct intel_plane *plane,
745 const struct intel_crtc_state *crtc_state)
747 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
749 trace_intel_plane_disable_arm(&plane->base, crtc);
750 plane->disable_arm(plane, crtc_state);
753 void intel_crtc_planes_update_noarm(struct intel_atomic_state *state,
754 struct intel_crtc *crtc)
756 struct intel_crtc_state *new_crtc_state =
757 intel_atomic_get_new_crtc_state(state, crtc);
758 u32 update_mask = new_crtc_state->update_planes;
759 struct intel_plane_state *new_plane_state;
760 struct intel_plane *plane;
763 if (new_crtc_state->do_async_flip)
767 * Since we only write non-arming registers here,
768 * the order does not matter even for skl+.
770 for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) {
771 if (crtc->pipe != plane->pipe ||
772 !(update_mask & BIT(plane->id)))
775 /* TODO: for mailbox updates this should be skipped */
776 if (new_plane_state->uapi.visible ||
777 new_plane_state->planar_slave)
778 intel_plane_update_noarm(plane, new_crtc_state, new_plane_state);
782 static void skl_crtc_planes_update_arm(struct intel_atomic_state *state,
783 struct intel_crtc *crtc)
785 struct intel_crtc_state *old_crtc_state =
786 intel_atomic_get_old_crtc_state(state, crtc);
787 struct intel_crtc_state *new_crtc_state =
788 intel_atomic_get_new_crtc_state(state, crtc);
789 struct skl_ddb_entry ddb[I915_MAX_PLANES];
790 struct skl_ddb_entry ddb_y[I915_MAX_PLANES];
791 u32 update_mask = new_crtc_state->update_planes;
792 struct intel_plane *plane;
794 memcpy(ddb, old_crtc_state->wm.skl.plane_ddb,
795 sizeof(old_crtc_state->wm.skl.plane_ddb));
796 memcpy(ddb_y, old_crtc_state->wm.skl.plane_ddb_y,
797 sizeof(old_crtc_state->wm.skl.plane_ddb_y));
799 while ((plane = skl_next_plane_to_commit(state, crtc, ddb, ddb_y, &update_mask))) {
800 struct intel_plane_state *new_plane_state =
801 intel_atomic_get_new_plane_state(state, plane);
804 * TODO: for mailbox updates intel_plane_update_noarm()
805 * would have to be called here as well.
807 if (new_plane_state->uapi.visible ||
808 new_plane_state->planar_slave)
809 intel_plane_update_arm(plane, new_crtc_state, new_plane_state);
811 intel_plane_disable_arm(plane, new_crtc_state);
815 static void i9xx_crtc_planes_update_arm(struct intel_atomic_state *state,
816 struct intel_crtc *crtc)
818 struct intel_crtc_state *new_crtc_state =
819 intel_atomic_get_new_crtc_state(state, crtc);
820 u32 update_mask = new_crtc_state->update_planes;
821 struct intel_plane_state *new_plane_state;
822 struct intel_plane *plane;
825 for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) {
826 if (crtc->pipe != plane->pipe ||
827 !(update_mask & BIT(plane->id)))
831 * TODO: for mailbox updates intel_plane_update_noarm()
832 * would have to be called here as well.
834 if (new_plane_state->uapi.visible)
835 intel_plane_update_arm(plane, new_crtc_state, new_plane_state);
837 intel_plane_disable_arm(plane, new_crtc_state);
841 void intel_crtc_planes_update_arm(struct intel_atomic_state *state,
842 struct intel_crtc *crtc)
844 struct drm_i915_private *i915 = to_i915(state->base.dev);
846 if (DISPLAY_VER(i915) >= 9)
847 skl_crtc_planes_update_arm(state, crtc);
849 i9xx_crtc_planes_update_arm(state, crtc);
852 int intel_atomic_plane_check_clipping(struct intel_plane_state *plane_state,
853 struct intel_crtc_state *crtc_state,
854 int min_scale, int max_scale,
857 struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
858 struct drm_framebuffer *fb = plane_state->hw.fb;
859 struct drm_rect *src = &plane_state->uapi.src;
860 struct drm_rect *dst = &plane_state->uapi.dst;
861 const struct drm_rect *clip = &crtc_state->pipe_src;
862 unsigned int rotation = plane_state->hw.rotation;
866 plane_state->uapi.visible = false;
870 drm_rect_rotate(src, fb->width << 16, fb->height << 16, rotation);
873 hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
874 vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
875 if (hscale < 0 || vscale < 0) {
876 drm_dbg_kms(&i915->drm, "Invalid scaling of plane\n");
877 drm_rect_debug_print("src: ", src, true);
878 drm_rect_debug_print("dst: ", dst, false);
883 * FIXME: This might need further adjustment for seamless scaling
884 * with phase information, for the 2p2 and 2p1 scenarios.
886 plane_state->uapi.visible = drm_rect_clip_scaled(src, dst, clip);
888 drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16, rotation);
890 if (!can_position && plane_state->uapi.visible &&
891 !drm_rect_equals(dst, clip)) {
892 drm_dbg_kms(&i915->drm, "Plane must cover entire CRTC\n");
893 drm_rect_debug_print("dst: ", dst, false);
894 drm_rect_debug_print("clip: ", clip, false);
898 /* final plane coordinates will be relative to the plane's pipe */
899 drm_rect_translate(dst, -clip->x1, -clip->y1);
904 struct wait_rps_boost {
905 struct wait_queue_entry wait;
907 struct drm_crtc *crtc;
908 struct i915_request *request;
911 static int do_rps_boost(struct wait_queue_entry *_wait,
912 unsigned mode, int sync, void *key)
914 struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
915 struct i915_request *rq = wait->request;
918 * If we missed the vblank, but the request is already running it
919 * is reasonable to assume that it will complete before the next
920 * vblank without our intervention, so leave RPS alone.
922 if (!i915_request_started(rq))
924 i915_request_put(rq);
926 drm_crtc_vblank_put(wait->crtc);
928 list_del(&wait->wait.entry);
933 static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
934 struct dma_fence *fence)
936 struct wait_rps_boost *wait;
938 if (!dma_fence_is_i915(fence))
941 if (DISPLAY_VER(to_i915(crtc->dev)) < 6)
944 if (drm_crtc_vblank_get(crtc))
947 wait = kmalloc(sizeof(*wait), GFP_KERNEL);
949 drm_crtc_vblank_put(crtc);
953 wait->request = to_request(dma_fence_get(fence));
956 wait->wait.func = do_rps_boost;
957 wait->wait.flags = 0;
959 add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
963 * intel_prepare_plane_fb - Prepare fb for usage on plane
964 * @_plane: drm plane to prepare for
965 * @_new_plane_state: the plane state being prepared
967 * Prepares a framebuffer for usage on a display plane. Generally this
968 * involves pinning the underlying object and updating the frontbuffer tracking
969 * bits. Some older platforms need special physical address handling for
972 * Returns 0 on success, negative error code on failure.
975 intel_prepare_plane_fb(struct drm_plane *_plane,
976 struct drm_plane_state *_new_plane_state)
978 struct i915_sched_attr attr = { .priority = I915_PRIORITY_DISPLAY };
979 struct intel_plane *plane = to_intel_plane(_plane);
980 struct intel_plane_state *new_plane_state =
981 to_intel_plane_state(_new_plane_state);
982 struct intel_atomic_state *state =
983 to_intel_atomic_state(new_plane_state->uapi.state);
984 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
985 const struct intel_plane_state *old_plane_state =
986 intel_atomic_get_old_plane_state(state, plane);
987 struct drm_i915_gem_object *obj = intel_fb_obj(new_plane_state->hw.fb);
988 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_plane_state->hw.fb);
992 const struct intel_crtc_state *crtc_state =
993 intel_atomic_get_new_crtc_state(state,
994 to_intel_crtc(old_plane_state->hw.crtc));
996 /* Big Hammer, we also need to ensure that any pending
997 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
998 * current scanout is retired before unpinning the old
999 * framebuffer. Note that we rely on userspace rendering
1000 * into the buffer attached to the pipe they are waiting
1001 * on. If not, userspace generates a GPU hang with IPEHR
1002 * point to the MI_WAIT_FOR_EVENT.
1004 * This should only fail upon a hung GPU, in which case we
1005 * can safely continue.
1007 if (intel_crtc_needs_modeset(crtc_state)) {
1008 ret = i915_sw_fence_await_reservation(&state->commit_ready,
1009 old_obj->base.resv, NULL,
1017 if (new_plane_state->uapi.fence) { /* explicit fencing */
1018 i915_gem_fence_wait_priority(new_plane_state->uapi.fence,
1020 ret = i915_sw_fence_await_dma_fence(&state->commit_ready,
1021 new_plane_state->uapi.fence,
1022 i915_fence_timeout(dev_priv),
1032 ret = intel_plane_pin_fb(new_plane_state);
1036 i915_gem_object_wait_priority(obj, 0, &attr);
1038 if (!new_plane_state->uapi.fence) { /* implicit fencing */
1039 struct dma_resv_iter cursor;
1040 struct dma_fence *fence;
1042 ret = i915_sw_fence_await_reservation(&state->commit_ready,
1043 obj->base.resv, NULL,
1045 i915_fence_timeout(dev_priv),
1050 dma_resv_iter_begin(&cursor, obj->base.resv,
1051 DMA_RESV_USAGE_WRITE);
1052 dma_resv_for_each_fence_unlocked(&cursor, fence) {
1053 add_rps_boost_after_vblank(new_plane_state->hw.crtc,
1056 dma_resv_iter_end(&cursor);
1058 add_rps_boost_after_vblank(new_plane_state->hw.crtc,
1059 new_plane_state->uapi.fence);
1063 * We declare pageflips to be interactive and so merit a small bias
1064 * towards upclocking to deliver the frame on time. By only changing
1065 * the RPS thresholds to sample more regularly and aim for higher
1066 * clocks we can hopefully deliver low power workloads (like kodi)
1067 * that are not quite steady state without resorting to forcing
1068 * maximum clocks following a vblank miss (see do_rps_boost()).
1070 if (!state->rps_interactive) {
1071 intel_rps_mark_interactive(&to_gt(dev_priv)->rps, true);
1072 state->rps_interactive = true;
1078 intel_plane_unpin_fb(new_plane_state);
1084 * intel_cleanup_plane_fb - Cleans up an fb after plane use
1085 * @plane: drm plane to clean up for
1086 * @_old_plane_state: the state from the previous modeset
1088 * Cleans up a framebuffer that has just been removed from a plane.
1091 intel_cleanup_plane_fb(struct drm_plane *plane,
1092 struct drm_plane_state *_old_plane_state)
1094 struct intel_plane_state *old_plane_state =
1095 to_intel_plane_state(_old_plane_state);
1096 struct intel_atomic_state *state =
1097 to_intel_atomic_state(old_plane_state->uapi.state);
1098 struct drm_i915_private *dev_priv = to_i915(plane->dev);
1099 struct drm_i915_gem_object *obj = intel_fb_obj(old_plane_state->hw.fb);
1104 if (state->rps_interactive) {
1105 intel_rps_mark_interactive(&to_gt(dev_priv)->rps, false);
1106 state->rps_interactive = false;
1109 /* Should only be called after a successful intel_prepare_plane_fb()! */
1110 intel_plane_unpin_fb(old_plane_state);
1113 static const struct drm_plane_helper_funcs intel_plane_helper_funcs = {
1114 .prepare_fb = intel_prepare_plane_fb,
1115 .cleanup_fb = intel_cleanup_plane_fb,
1118 void intel_plane_helper_add(struct intel_plane *plane)
1120 drm_plane_helper_add(&plane->base, &intel_plane_helper_funcs);