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 "intel_atomic_plane.h"
39 #include "intel_drv.h"
41 #include "intel_sprite.h"
43 struct intel_plane *intel_plane_alloc(void)
45 struct intel_plane_state *plane_state;
46 struct intel_plane *plane;
48 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
50 return ERR_PTR(-ENOMEM);
52 plane_state = kzalloc(sizeof(*plane_state), GFP_KERNEL);
55 return ERR_PTR(-ENOMEM);
58 __drm_atomic_helper_plane_reset(&plane->base, &plane_state->base);
59 plane_state->scaler_id = -1;
64 void intel_plane_free(struct intel_plane *plane)
66 intel_plane_destroy_state(&plane->base, plane->base.state);
71 * intel_plane_duplicate_state - duplicate plane state
74 * Allocates and returns a copy of the plane state (both common and
75 * Intel-specific) for the specified plane.
77 * Returns: The newly allocated plane state, or NULL on failure.
79 struct drm_plane_state *
80 intel_plane_duplicate_state(struct drm_plane *plane)
82 struct drm_plane_state *state;
83 struct intel_plane_state *intel_state;
85 intel_state = kmemdup(plane->state, sizeof(*intel_state), GFP_KERNEL);
90 state = &intel_state->base;
92 __drm_atomic_helper_plane_duplicate_state(plane, state);
94 intel_state->vma = NULL;
95 intel_state->flags = 0;
101 * intel_plane_destroy_state - destroy plane state
103 * @state: state object to destroy
105 * Destroys the plane state (both common and Intel-specific) for the
109 intel_plane_destroy_state(struct drm_plane *plane,
110 struct drm_plane_state *state)
112 WARN_ON(to_intel_plane_state(state)->vma);
114 drm_atomic_helper_plane_destroy_state(plane, state);
117 unsigned int intel_plane_data_rate(const struct intel_crtc_state *crtc_state,
118 const struct intel_plane_state *plane_state)
120 const struct drm_framebuffer *fb = plane_state->base.fb;
123 if (!plane_state->base.visible)
126 cpp = fb->format->cpp[0];
129 * Based on HSD#:1408715493
130 * NV12 cpp == 4, P010 cpp == 8
132 * FIXME what is the logic behind this?
134 if (fb->format->is_yuv && fb->format->num_planes > 1)
137 return cpp * crtc_state->pixel_rate;
140 int intel_plane_atomic_check_with_state(const struct intel_crtc_state *old_crtc_state,
141 struct intel_crtc_state *new_crtc_state,
142 const struct intel_plane_state *old_plane_state,
143 struct intel_plane_state *new_plane_state)
145 struct intel_plane *plane = to_intel_plane(new_plane_state->base.plane);
148 new_crtc_state->active_planes &= ~BIT(plane->id);
149 new_crtc_state->nv12_planes &= ~BIT(plane->id);
150 new_crtc_state->c8_planes &= ~BIT(plane->id);
151 new_crtc_state->data_rate[plane->id] = 0;
152 new_plane_state->base.visible = false;
154 if (!new_plane_state->base.crtc && !old_plane_state->base.crtc)
157 ret = plane->check_plane(new_crtc_state, new_plane_state);
161 /* FIXME pre-g4x don't work like this */
162 if (new_plane_state->base.visible)
163 new_crtc_state->active_planes |= BIT(plane->id);
165 if (new_plane_state->base.visible &&
166 is_planar_yuv_format(new_plane_state->base.fb->format->format))
167 new_crtc_state->nv12_planes |= BIT(plane->id);
169 if (new_plane_state->base.visible &&
170 new_plane_state->base.fb->format->format == DRM_FORMAT_C8)
171 new_crtc_state->c8_planes |= BIT(plane->id);
173 if (new_plane_state->base.visible || old_plane_state->base.visible)
174 new_crtc_state->update_planes |= BIT(plane->id);
176 new_crtc_state->data_rate[plane->id] =
177 intel_plane_data_rate(new_crtc_state, new_plane_state);
179 return intel_plane_atomic_calc_changes(old_crtc_state, new_crtc_state,
180 old_plane_state, new_plane_state);
183 static struct intel_crtc *
184 get_crtc_from_states(const struct intel_plane_state *old_plane_state,
185 const struct intel_plane_state *new_plane_state)
187 if (new_plane_state->base.crtc)
188 return to_intel_crtc(new_plane_state->base.crtc);
190 if (old_plane_state->base.crtc)
191 return to_intel_crtc(old_plane_state->base.crtc);
196 static int intel_plane_atomic_check(struct drm_plane *_plane,
197 struct drm_plane_state *_new_plane_state)
199 struct intel_plane *plane = to_intel_plane(_plane);
200 struct intel_atomic_state *state =
201 to_intel_atomic_state(_new_plane_state->state);
202 struct intel_plane_state *new_plane_state =
203 to_intel_plane_state(_new_plane_state);
204 const struct intel_plane_state *old_plane_state =
205 intel_atomic_get_old_plane_state(state, plane);
206 struct intel_crtc *crtc =
207 get_crtc_from_states(old_plane_state, new_plane_state);
208 const struct intel_crtc_state *old_crtc_state;
209 struct intel_crtc_state *new_crtc_state;
211 new_plane_state->base.visible = false;
215 old_crtc_state = intel_atomic_get_old_crtc_state(state, crtc);
216 new_crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
218 return intel_plane_atomic_check_with_state(old_crtc_state,
224 static struct intel_plane *
225 skl_next_plane_to_commit(struct intel_atomic_state *state,
226 struct intel_crtc *crtc,
227 struct skl_ddb_entry entries_y[I915_MAX_PLANES],
228 struct skl_ddb_entry entries_uv[I915_MAX_PLANES],
229 unsigned int *update_mask)
231 struct intel_crtc_state *crtc_state =
232 intel_atomic_get_new_crtc_state(state, crtc);
233 struct intel_plane_state *plane_state;
234 struct intel_plane *plane;
237 if (*update_mask == 0)
240 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
241 enum plane_id plane_id = plane->id;
243 if (crtc->pipe != plane->pipe ||
244 !(*update_mask & BIT(plane_id)))
247 if (skl_ddb_allocation_overlaps(&crtc_state->wm.skl.plane_ddb_y[plane_id],
249 I915_MAX_PLANES, plane_id) ||
250 skl_ddb_allocation_overlaps(&crtc_state->wm.skl.plane_ddb_uv[plane_id],
252 I915_MAX_PLANES, plane_id))
255 *update_mask &= ~BIT(plane_id);
256 entries_y[plane_id] = crtc_state->wm.skl.plane_ddb_y[plane_id];
257 entries_uv[plane_id] = crtc_state->wm.skl.plane_ddb_uv[plane_id];
262 /* should never happen */
268 void intel_update_plane(struct intel_plane *plane,
269 const struct intel_crtc_state *crtc_state,
270 const struct intel_plane_state *plane_state)
272 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
274 trace_intel_update_plane(&plane->base, crtc);
275 plane->update_plane(plane, crtc_state, plane_state);
278 void intel_update_slave(struct intel_plane *plane,
279 const struct intel_crtc_state *crtc_state,
280 const struct intel_plane_state *plane_state)
282 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
284 trace_intel_update_plane(&plane->base, crtc);
285 plane->update_slave(plane, crtc_state, plane_state);
288 void intel_disable_plane(struct intel_plane *plane,
289 const struct intel_crtc_state *crtc_state)
291 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
293 trace_intel_disable_plane(&plane->base, crtc);
294 plane->disable_plane(plane, crtc_state);
297 void skl_update_planes_on_crtc(struct intel_atomic_state *state,
298 struct intel_crtc *crtc)
300 struct intel_crtc_state *old_crtc_state =
301 intel_atomic_get_old_crtc_state(state, crtc);
302 struct intel_crtc_state *new_crtc_state =
303 intel_atomic_get_new_crtc_state(state, crtc);
304 struct skl_ddb_entry entries_y[I915_MAX_PLANES];
305 struct skl_ddb_entry entries_uv[I915_MAX_PLANES];
306 u32 update_mask = new_crtc_state->update_planes;
307 struct intel_plane *plane;
309 memcpy(entries_y, old_crtc_state->wm.skl.plane_ddb_y,
310 sizeof(old_crtc_state->wm.skl.plane_ddb_y));
311 memcpy(entries_uv, old_crtc_state->wm.skl.plane_ddb_uv,
312 sizeof(old_crtc_state->wm.skl.plane_ddb_uv));
314 while ((plane = skl_next_plane_to_commit(state, crtc,
315 entries_y, entries_uv,
317 struct intel_plane_state *new_plane_state =
318 intel_atomic_get_new_plane_state(state, plane);
320 if (new_plane_state->base.visible) {
321 intel_update_plane(plane, new_crtc_state, new_plane_state);
322 } else if (new_plane_state->slave) {
323 struct intel_plane *master =
324 new_plane_state->linked_plane;
327 * We update the slave plane from this function because
328 * programming it from the master plane's update_plane
329 * callback runs into issues when the Y plane is
330 * reassigned, disabled or used by a different plane.
332 * The slave plane is updated with the master plane's
336 intel_atomic_get_new_plane_state(state, master);
338 intel_update_slave(plane, new_crtc_state, new_plane_state);
340 intel_disable_plane(plane, new_crtc_state);
345 void i9xx_update_planes_on_crtc(struct intel_atomic_state *state,
346 struct intel_crtc *crtc)
348 struct intel_crtc_state *new_crtc_state =
349 intel_atomic_get_new_crtc_state(state, crtc);
350 u32 update_mask = new_crtc_state->update_planes;
351 struct intel_plane_state *new_plane_state;
352 struct intel_plane *plane;
355 for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) {
356 if (crtc->pipe != plane->pipe ||
357 !(update_mask & BIT(plane->id)))
360 if (new_plane_state->base.visible)
361 intel_update_plane(plane, new_crtc_state, new_plane_state);
363 intel_disable_plane(plane, new_crtc_state);
367 const struct drm_plane_helper_funcs intel_plane_helper_funcs = {
368 .prepare_fb = intel_prepare_plane_fb,
369 .cleanup_fb = intel_cleanup_plane_fb,
370 .atomic_check = intel_plane_atomic_check,