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