blob: 46dceb51c90f38caf74c8ee4315af5801cb4ff32 [file] [log] [blame]
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001/*
2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the 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 COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robdclark@gmail.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
26 */
27
28
Sam Ravnborg0500c042019-05-26 19:35:35 +020029#include <linux/sync_file.h>
30
Daniel Vettercc4ceb42014-07-25 21:30:38 +020031#include <drm/drm_atomic.h>
Daniel Vetter72fdb40c2018-09-05 15:57:11 +020032#include <drm/drm_atomic_uapi.h>
Boris Brezillon75146592020-01-28 14:55:03 +010033#include <drm/drm_bridge.h>
Sam Ravnborg0500c042019-05-26 19:35:35 +020034#include <drm/drm_debugfs.h>
35#include <drm/drm_device.h>
36#include <drm/drm_drv.h>
37#include <drm/drm_file.h>
38#include <drm/drm_fourcc.h>
Lionel Landwerlin5488dc12016-02-26 17:05:00 +000039#include <drm/drm_mode.h>
Rob Clarkfceffb322016-11-05 11:08:09 -040040#include <drm/drm_print.h>
Brian Starkey935774c2017-03-29 17:42:32 +010041#include <drm/drm_writeback.h>
Daniel Vettercc4ceb42014-07-25 21:30:38 +020042
Thierry Redingbe35f942016-04-28 15:19:56 +020043#include "drm_crtc_internal.h"
Noralf Trønnesf02b6042017-11-07 20:13:41 +010044#include "drm_internal.h"
Thierry Redingbe35f942016-04-28 15:19:56 +020045
Daniel Vetterb3ba3f62016-12-21 14:03:35 +010046void __drm_crtc_commit_free(struct kref *kref)
Daniel Vetter3b24f7d2016-06-08 14:19:00 +020047{
48 struct drm_crtc_commit *commit =
49 container_of(kref, struct drm_crtc_commit, ref);
50
51 kfree(commit);
52}
Daniel Vetterb3ba3f62016-12-21 14:03:35 +010053EXPORT_SYMBOL(__drm_crtc_commit_free);
Daniel Vetter3b24f7d2016-06-08 14:19:00 +020054
Maarten Lankhorst036ef572015-05-18 10:06:40 +020055/**
Maxime Ripardb99c2c92021-01-11 09:44:01 +010056 * drm_crtc_commit_wait - Waits for a commit to complete
57 * @commit: &drm_crtc_commit to wait for
58 *
59 * Waits for a given &drm_crtc_commit to be programmed into the
60 * hardware and flipped to.
61 *
62 * Returns:
63 *
64 * 0 on success, a negative error code otherwise.
65 */
66int drm_crtc_commit_wait(struct drm_crtc_commit *commit)
67{
68 unsigned long timeout = 10 * HZ;
69 int ret;
70
71 if (!commit)
72 return 0;
73
74 ret = wait_for_completion_timeout(&commit->hw_done, timeout);
75 if (!ret) {
76 DRM_ERROR("hw_done timed out\n");
77 return -ETIMEDOUT;
78 }
79
80 /*
81 * Currently no support for overwriting flips, hence
82 * stall for previous one to execute completely.
83 */
84 ret = wait_for_completion_timeout(&commit->flip_done, timeout);
85 if (!ret) {
86 DRM_ERROR("flip_done timed out\n");
87 return -ETIMEDOUT;
88 }
89
90 return 0;
91}
92EXPORT_SYMBOL(drm_crtc_commit_wait);
93
94/**
Maarten Lankhorst036ef572015-05-18 10:06:40 +020095 * drm_atomic_state_default_release -
96 * release memory initialized by drm_atomic_state_init
97 * @state: atomic state
98 *
99 * Free all the memory allocated by drm_atomic_state_init.
Daniel Vetterda6c0592017-12-14 21:30:53 +0100100 * This should only be used by drivers which are still subclassing
101 * &drm_atomic_state and haven't switched to &drm_private_state yet.
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200102 */
103void drm_atomic_state_default_release(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200104{
105 kfree(state->connectors);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200106 kfree(state->crtcs);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200107 kfree(state->planes);
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700108 kfree(state->private_objs);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200109}
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200110EXPORT_SYMBOL(drm_atomic_state_default_release);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200111
112/**
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200113 * drm_atomic_state_init - init new atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200114 * @dev: DRM device
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200115 * @state: atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200116 *
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200117 * Default implementation for filling in a new atomic state.
Daniel Vetterda6c0592017-12-14 21:30:53 +0100118 * This should only be used by drivers which are still subclassing
119 * &drm_atomic_state and haven't switched to &drm_private_state yet.
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200120 */
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200121int
122drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200123{
Chris Wilson08536952016-10-14 13:18:18 +0100124 kref_init(&state->ref);
125
Rob Clarkd34f20d2014-12-18 16:01:56 -0500126 /* TODO legacy paths should maybe do a better job about
127 * setting this appropriately?
128 */
129 state->allow_modeset = true;
130
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200131 state->crtcs = kcalloc(dev->mode_config.num_crtc,
132 sizeof(*state->crtcs), GFP_KERNEL);
133 if (!state->crtcs)
134 goto fail;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200135 state->planes = kcalloc(dev->mode_config.num_total_plane,
136 sizeof(*state->planes), GFP_KERNEL);
137 if (!state->planes)
138 goto fail;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200139
140 state->dev = dev;
141
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200142 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200143
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200144 return 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200145fail:
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200146 drm_atomic_state_default_release(state);
147 return -ENOMEM;
148}
149EXPORT_SYMBOL(drm_atomic_state_init);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200150
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200151/**
152 * drm_atomic_state_alloc - allocate atomic state
153 * @dev: DRM device
154 *
155 * This allocates an empty atomic state to track updates.
156 */
157struct drm_atomic_state *
158drm_atomic_state_alloc(struct drm_device *dev)
159{
160 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200161
162 if (!config->funcs->atomic_state_alloc) {
Dawid Kurekac7c7482017-06-15 19:45:56 +0200163 struct drm_atomic_state *state;
164
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200165 state = kzalloc(sizeof(*state), GFP_KERNEL);
166 if (!state)
167 return NULL;
168 if (drm_atomic_state_init(dev, state) < 0) {
169 kfree(state);
170 return NULL;
171 }
172 return state;
173 }
174
175 return config->funcs->atomic_state_alloc(dev);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200176}
177EXPORT_SYMBOL(drm_atomic_state_alloc);
178
179/**
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200180 * drm_atomic_state_default_clear - clear base atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200181 * @state: atomic state
182 *
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200183 * Default implementation for clearing atomic state.
Daniel Vetterda6c0592017-12-14 21:30:53 +0100184 * This should only be used by drivers which are still subclassing
185 * &drm_atomic_state and haven't switched to &drm_private_state yet.
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200186 */
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200187void drm_atomic_state_default_clear(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200188{
189 struct drm_device *dev = state->dev;
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100190 struct drm_mode_config *config = &dev->mode_config;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200191 int i;
192
Daniel Vetter17a38d92015-02-22 12:24:16 +0100193 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200194
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100195 for (i = 0; i < state->num_connector; i++) {
Daniel Vetter63e83c12016-06-02 00:06:32 +0200196 struct drm_connector *connector = state->connectors[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200197
198 if (!connector)
199 continue;
200
Dave Airlied2307de2016-04-27 11:27:39 +1000201 connector->funcs->atomic_destroy_state(connector,
Daniel Vetter63e83c12016-06-02 00:06:32 +0200202 state->connectors[i].state);
203 state->connectors[i].ptr = NULL;
204 state->connectors[i].state = NULL;
Ville Syrjäläf0b408e2018-05-02 21:32:47 +0300205 state->connectors[i].old_state = NULL;
206 state->connectors[i].new_state = NULL;
Thierry Redingad093602017-02-28 15:46:39 +0100207 drm_connector_put(connector);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200208 }
209
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100210 for (i = 0; i < config->num_crtc; i++) {
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200211 struct drm_crtc *crtc = state->crtcs[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200212
213 if (!crtc)
214 continue;
215
216 crtc->funcs->atomic_destroy_state(crtc,
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200217 state->crtcs[i].state);
Daniel Vetter3b24f7d2016-06-08 14:19:00 +0200218
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200219 state->crtcs[i].ptr = NULL;
220 state->crtcs[i].state = NULL;
Ville Syrjäläf0b408e2018-05-02 21:32:47 +0300221 state->crtcs[i].old_state = NULL;
222 state->crtcs[i].new_state = NULL;
Leo Li4364bcb2018-10-15 09:46:40 -0400223
224 if (state->crtcs[i].commit) {
225 drm_crtc_commit_put(state->crtcs[i].commit);
226 state->crtcs[i].commit = NULL;
227 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200228 }
229
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100230 for (i = 0; i < config->num_total_plane; i++) {
Daniel Vetterb8b53422016-06-02 00:06:33 +0200231 struct drm_plane *plane = state->planes[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200232
233 if (!plane)
234 continue;
235
236 plane->funcs->atomic_destroy_state(plane,
Daniel Vetterb8b53422016-06-02 00:06:33 +0200237 state->planes[i].state);
238 state->planes[i].ptr = NULL;
239 state->planes[i].state = NULL;
Ville Syrjäläf0b408e2018-05-02 21:32:47 +0300240 state->planes[i].old_state = NULL;
241 state->planes[i].new_state = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200242 }
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700243
244 for (i = 0; i < state->num_private_objs; i++) {
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300245 struct drm_private_obj *obj = state->private_objs[i].ptr;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700246
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300247 obj->funcs->atomic_destroy_state(obj,
248 state->private_objs[i].state);
249 state->private_objs[i].ptr = NULL;
250 state->private_objs[i].state = NULL;
Ville Syrjäläb5cb2e52018-05-02 21:32:47 +0300251 state->private_objs[i].old_state = NULL;
252 state->private_objs[i].new_state = NULL;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700253 }
254 state->num_private_objs = 0;
255
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +0200256 if (state->fake_commit) {
257 drm_crtc_commit_put(state->fake_commit);
258 state->fake_commit = NULL;
259 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200260}
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200261EXPORT_SYMBOL(drm_atomic_state_default_clear);
262
263/**
264 * drm_atomic_state_clear - clear state object
265 * @state: atomic state
266 *
267 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
268 * all locks. So someone else could sneak in and change the current modeset
269 * configuration. Which means that all the state assembled in @state is no
270 * longer an atomic update to the current state, but to some arbitrary earlier
Daniel Vetterd5745282017-01-25 07:26:45 +0100271 * state. Which could break assumptions the driver's
272 * &drm_mode_config_funcs.atomic_check likely relies on.
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200273 *
274 * Hence we must clear all cached state and completely start over, using this
275 * function.
276 */
277void drm_atomic_state_clear(struct drm_atomic_state *state)
278{
279 struct drm_device *dev = state->dev;
280 struct drm_mode_config *config = &dev->mode_config;
281
282 if (config->funcs->atomic_state_clear)
283 config->funcs->atomic_state_clear(state);
284 else
285 drm_atomic_state_default_clear(state);
286}
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200287EXPORT_SYMBOL(drm_atomic_state_clear);
288
289/**
Chris Wilson08536952016-10-14 13:18:18 +0100290 * __drm_atomic_state_free - free all memory for an atomic state
291 * @ref: This atomic state to deallocate
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200292 *
293 * This frees all memory associated with an atomic state, including all the
Thierry Reding42240c92019-12-06 14:53:36 +0100294 * per-object state for planes, CRTCs and connectors.
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200295 */
Chris Wilson08536952016-10-14 13:18:18 +0100296void __drm_atomic_state_free(struct kref *ref)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200297{
Chris Wilson08536952016-10-14 13:18:18 +0100298 struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
299 struct drm_mode_config *config = &state->dev->mode_config;
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200300
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200301 drm_atomic_state_clear(state);
302
Daniel Vetter17a38d92015-02-22 12:24:16 +0100303 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200304
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200305 if (config->funcs->atomic_state_free) {
306 config->funcs->atomic_state_free(state);
307 } else {
308 drm_atomic_state_default_release(state);
309 kfree(state);
310 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200311}
Chris Wilson08536952016-10-14 13:18:18 +0100312EXPORT_SYMBOL(__drm_atomic_state_free);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200313
314/**
Thierry Reding42240c92019-12-06 14:53:36 +0100315 * drm_atomic_get_crtc_state - get CRTC state
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200316 * @state: global atomic state object
Thierry Reding42240c92019-12-06 14:53:36 +0100317 * @crtc: CRTC to get state object for
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200318 *
Thierry Reding42240c92019-12-06 14:53:36 +0100319 * This function returns the CRTC state for the given CRTC, allocating it if
320 * needed. It will also grab the relevant CRTC lock to make sure that the state
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200321 * is consistent.
322 *
Daniel Vetterfb6473a2020-09-25 10:46:50 +0200323 * WARNING: Drivers may only add new CRTC states to a @state if
324 * drm_atomic_state.allow_modeset is set, or if it's a driver-internal commit
325 * not created by userspace through an IOCTL call.
326 *
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200327 * Returns:
328 *
329 * Either the allocated state or the error code encoded into the pointer. When
330 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
331 * entire atomic sequence must be restarted. All other errors are fatal.
332 */
333struct drm_crtc_state *
334drm_atomic_get_crtc_state(struct drm_atomic_state *state,
335 struct drm_crtc *crtc)
336{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200337 int ret, index = drm_crtc_index(crtc);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200338 struct drm_crtc_state *crtc_state;
339
Maarten Lankhorst7f4eaa892016-05-03 11:12:31 +0200340 WARN_ON(!state->acquire_ctx);
341
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200342 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
343 if (crtc_state)
344 return crtc_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200345
346 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
347 if (ret)
348 return ERR_PTR(ret);
349
350 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
351 if (!crtc_state)
352 return ERR_PTR(-ENOMEM);
353
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200354 state->crtcs[index].state = crtc_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +0100355 state->crtcs[index].old_state = crtc->state;
356 state->crtcs[index].new_state = crtc_state;
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200357 state->crtcs[index].ptr = crtc;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200358 crtc_state->state = state;
359
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200360 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
361 crtc->base.id, crtc->name, crtc_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200362
363 return crtc_state;
364}
365EXPORT_SYMBOL(drm_atomic_get_crtc_state);
366
Ville Syrjäläb2432ad2018-11-01 20:46:45 +0200367static int drm_atomic_crtc_check(const struct drm_crtc_state *old_crtc_state,
368 const struct drm_crtc_state *new_crtc_state)
Rob Clark5e743732014-12-18 16:01:51 -0500369{
Ville Syrjäläb2432ad2018-11-01 20:46:45 +0200370 struct drm_crtc *crtc = new_crtc_state->crtc;
371
Rob Clark5e743732014-12-18 16:01:51 -0500372 /* NOTE: we explicitly don't enforce constraints such as primary
373 * layer covering entire screen, since that is something we want
374 * to allow (on hw that supports it). For hw that does not, it
375 * should be checked in driver's crtc->atomic_check() vfunc.
376 *
377 * TODO: Add generic modeset state checks once we support those.
378 */
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100379
Ville Syrjäläb2432ad2018-11-01 20:46:45 +0200380 if (new_crtc_state->active && !new_crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200381 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
382 crtc->base.id, crtc->name);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100383 return -EINVAL;
384 }
385
Daniel Stone99cf4a22015-05-25 19:11:51 +0100386 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
387 * as this is a kernel-internal detail that userspace should never
388 * be able to trigger. */
389 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
Ville Syrjäläb2432ad2018-11-01 20:46:45 +0200390 WARN_ON(new_crtc_state->enable && !new_crtc_state->mode_blob)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200391 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
392 crtc->base.id, crtc->name);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100393 return -EINVAL;
394 }
395
396 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
Ville Syrjäläb2432ad2018-11-01 20:46:45 +0200397 WARN_ON(!new_crtc_state->enable && new_crtc_state->mode_blob)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200398 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
399 crtc->base.id, crtc->name);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100400 return -EINVAL;
401 }
402
Daniel Vetter4cba6852015-12-08 09:49:20 +0100403 /*
404 * Reject event generation for when a CRTC is off and stays off.
405 * It wouldn't be hard to implement this, but userspace has a track
406 * record of happily burning through 100% cpu (or worse, crash) when the
407 * display pipe is suspended. To avoid all that fun just reject updates
408 * that ask for events since likely that indicates a bug in the
409 * compositor's drawing loop. This is consistent with the vblank IOCTL
410 * and legacy page_flip IOCTL which also reject service on a disabled
411 * pipe.
412 */
Ville Syrjäläb2432ad2018-11-01 20:46:45 +0200413 if (new_crtc_state->event &&
414 !new_crtc_state->active && !old_crtc_state->active) {
Russell King6ac7c542017-02-13 12:27:03 +0000415 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requesting event but off\n",
416 crtc->base.id, crtc->name);
Daniel Vetter4cba6852015-12-08 09:49:20 +0100417 return -EINVAL;
418 }
419
Rob Clark5e743732014-12-18 16:01:51 -0500420 return 0;
421}
422
Rob Clarkfceffb322016-11-05 11:08:09 -0400423static void drm_atomic_crtc_print_state(struct drm_printer *p,
424 const struct drm_crtc_state *state)
425{
426 struct drm_crtc *crtc = state->crtc;
427
428 drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
429 drm_printf(p, "\tenable=%d\n", state->enable);
430 drm_printf(p, "\tactive=%d\n", state->active);
Sean Paul1452c252019-06-12 10:50:19 -0400431 drm_printf(p, "\tself_refresh_active=%d\n", state->self_refresh_active);
Rob Clarkfceffb322016-11-05 11:08:09 -0400432 drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
433 drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
434 drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
435 drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
436 drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
437 drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
438 drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
439 drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
440 drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
441
442 if (crtc->funcs->atomic_print_state)
443 crtc->funcs->atomic_print_state(p, state);
444}
445
Brian Starkey935774c2017-03-29 17:42:32 +0100446static int drm_atomic_connector_check(struct drm_connector *connector,
447 struct drm_connector_state *state)
448{
449 struct drm_crtc_state *crtc_state;
450 struct drm_writeback_job *writeback_job = state->writeback_job;
Radhakrishna Sripada47e22ff2018-10-12 11:42:32 -0700451 const struct drm_display_info *info = &connector->display_info;
452
453 state->max_bpc = info->bpc ? info->bpc : 8;
454 if (connector->max_bpc_property)
455 state->max_bpc = min(state->max_bpc, state->max_requested_bpc);
Brian Starkey935774c2017-03-29 17:42:32 +0100456
457 if ((connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK) || !writeback_job)
458 return 0;
459
460 if (writeback_job->fb && !state->crtc) {
461 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] framebuffer without CRTC\n",
462 connector->base.id, connector->name);
463 return -EINVAL;
464 }
465
466 if (state->crtc)
467 crtc_state = drm_atomic_get_existing_crtc_state(state->state,
468 state->crtc);
469
470 if (writeback_job->fb && !crtc_state->active) {
471 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] has framebuffer, but [CRTC:%d] is off\n",
472 connector->base.id, connector->name,
473 state->crtc->base.id);
474 return -EINVAL;
475 }
476
Lowry Li (Arm Technology China)8581d512019-07-31 11:04:38 +0000477 if (!writeback_job->fb) {
478 if (writeback_job->out_fence) {
479 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] requesting out-fence without framebuffer\n",
480 connector->base.id, connector->name);
481 return -EINVAL;
482 }
483
484 drm_writeback_cleanup_job(writeback_job);
485 state->writeback_job = NULL;
Brian Starkeyb13cc8d2017-03-29 17:42:33 +0100486 }
487
Brian Starkey935774c2017-03-29 17:42:32 +0100488 return 0;
489}
490
491/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200492 * drm_atomic_get_plane_state - get plane state
493 * @state: global atomic state object
494 * @plane: plane to get state object for
495 *
496 * This function returns the plane state for the given plane, allocating it if
497 * needed. It will also grab the relevant plane lock to make sure that the state
498 * is consistent.
499 *
500 * Returns:
501 *
502 * Either the allocated state or the error code encoded into the pointer. When
503 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
504 * entire atomic sequence must be restarted. All other errors are fatal.
505 */
506struct drm_plane_state *
507drm_atomic_get_plane_state(struct drm_atomic_state *state,
508 struct drm_plane *plane)
509{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200510 int ret, index = drm_plane_index(plane);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200511 struct drm_plane_state *plane_state;
512
Maarten Lankhorst7f4eaa892016-05-03 11:12:31 +0200513 WARN_ON(!state->acquire_ctx);
514
Ville Syrjäläe00fb852018-05-25 21:50:45 +0300515 /* the legacy pointers should never be set */
516 WARN_ON(plane->fb);
517 WARN_ON(plane->old_fb);
518 WARN_ON(plane->crtc);
519
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200520 plane_state = drm_atomic_get_existing_plane_state(state, plane);
521 if (plane_state)
522 return plane_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200523
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100524 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200525 if (ret)
526 return ERR_PTR(ret);
527
528 plane_state = plane->funcs->atomic_duplicate_state(plane);
529 if (!plane_state)
530 return ERR_PTR(-ENOMEM);
531
Daniel Vetterb8b53422016-06-02 00:06:33 +0200532 state->planes[index].state = plane_state;
533 state->planes[index].ptr = plane;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +0100534 state->planes[index].old_state = plane->state;
535 state->planes[index].new_state = plane_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200536 plane_state->state = state;
537
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200538 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
539 plane->base.id, plane->name, plane_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200540
541 if (plane_state->crtc) {
542 struct drm_crtc_state *crtc_state;
543
544 crtc_state = drm_atomic_get_crtc_state(state,
545 plane_state->crtc);
546 if (IS_ERR(crtc_state))
547 return ERR_CAST(crtc_state);
548 }
549
550 return plane_state;
551}
552EXPORT_SYMBOL(drm_atomic_get_plane_state);
553
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200554static bool
Ville Syrjäläd9be05b2018-11-06 21:16:24 +0200555plane_switching_crtc(const struct drm_plane_state *old_plane_state,
556 const struct drm_plane_state *new_plane_state)
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200557{
Ville Syrjäläd9be05b2018-11-06 21:16:24 +0200558 if (!old_plane_state->crtc || !new_plane_state->crtc)
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200559 return false;
560
Ville Syrjäläd9be05b2018-11-06 21:16:24 +0200561 if (old_plane_state->crtc == new_plane_state->crtc)
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200562 return false;
563
564 /* This could be refined, but currently there's no helper or driver code
565 * to implement direct switching of active planes nor userspace to take
566 * advantage of more direct plane switching without the intermediate
567 * full OFF state.
568 */
569 return true;
570}
571
Rob Clarkac9c9252014-12-18 16:01:47 -0500572/**
Rob Clark5e743732014-12-18 16:01:51 -0500573 * drm_atomic_plane_check - check plane state
Ville Syrjäläd9be05b2018-11-06 21:16:24 +0200574 * @old_plane_state: old plane state to check
575 * @new_plane_state: new plane state to check
Rob Clark5e743732014-12-18 16:01:51 -0500576 *
577 * Provides core sanity checks for plane state.
578 *
579 * RETURNS:
580 * Zero on success, error code on failure
581 */
Ville Syrjäläd9be05b2018-11-06 21:16:24 +0200582static int drm_atomic_plane_check(const struct drm_plane_state *old_plane_state,
583 const struct drm_plane_state *new_plane_state)
Rob Clark5e743732014-12-18 16:01:51 -0500584{
Ville Syrjäläd9be05b2018-11-06 21:16:24 +0200585 struct drm_plane *plane = new_plane_state->plane;
586 struct drm_crtc *crtc = new_plane_state->crtc;
587 const struct drm_framebuffer *fb = new_plane_state->fb;
Rob Clark5e743732014-12-18 16:01:51 -0500588 unsigned int fb_width, fb_height;
Lukasz Spintzykd3b21762018-05-23 19:04:08 -0700589 struct drm_mode_rect *clips;
590 uint32_t num_clips;
Laurent Pinchartead86102015-03-05 02:25:43 +0200591 int ret;
Rob Clark5e743732014-12-18 16:01:51 -0500592
593 /* either *both* CRTC and FB must be set, or neither */
Ville Syrjäläd9be05b2018-11-06 21:16:24 +0200594 if (crtc && !fb) {
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300595 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] CRTC set but no FB\n",
596 plane->base.id, plane->name);
Rob Clark5e743732014-12-18 16:01:51 -0500597 return -EINVAL;
Ville Syrjäläd9be05b2018-11-06 21:16:24 +0200598 } else if (fb && !crtc) {
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300599 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] FB set but no CRTC\n",
600 plane->base.id, plane->name);
Rob Clark5e743732014-12-18 16:01:51 -0500601 return -EINVAL;
602 }
603
604 /* if disabled, we don't care about the rest of the state: */
Ville Syrjäläd9be05b2018-11-06 21:16:24 +0200605 if (!crtc)
Rob Clark5e743732014-12-18 16:01:51 -0500606 return 0;
607
608 /* Check whether this plane is usable on this CRTC */
Ville Syrjäläd9be05b2018-11-06 21:16:24 +0200609 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300610 DRM_DEBUG_ATOMIC("Invalid [CRTC:%d:%s] for [PLANE:%d:%s]\n",
Ville Syrjäläd9be05b2018-11-06 21:16:24 +0200611 crtc->base.id, crtc->name,
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300612 plane->base.id, plane->name);
Rob Clark5e743732014-12-18 16:01:51 -0500613 return -EINVAL;
614 }
615
616 /* Check whether this plane supports the fb pixel format. */
Ville Syrjäläd9be05b2018-11-06 21:16:24 +0200617 ret = drm_plane_check_pixel_format(plane, fb->format->format,
618 fb->modifier);
Laurent Pinchartead86102015-03-05 02:25:43 +0200619 if (ret) {
Sakari Ailus92f1d092021-02-16 17:57:22 +0200620 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid pixel format %p4cc, modifier 0x%llx\n",
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300621 plane->base.id, plane->name,
Sakari Ailus92f1d092021-02-16 17:57:22 +0200622 &fb->format->format, fb->modifier);
Laurent Pinchartead86102015-03-05 02:25:43 +0200623 return ret;
Rob Clark5e743732014-12-18 16:01:51 -0500624 }
625
626 /* Give drivers some help against integer overflows */
Ville Syrjäläd9be05b2018-11-06 21:16:24 +0200627 if (new_plane_state->crtc_w > INT_MAX ||
628 new_plane_state->crtc_x > INT_MAX - (int32_t) new_plane_state->crtc_w ||
629 new_plane_state->crtc_h > INT_MAX ||
630 new_plane_state->crtc_y > INT_MAX - (int32_t) new_plane_state->crtc_h) {
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300631 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid CRTC coordinates %ux%u+%d+%d\n",
632 plane->base.id, plane->name,
Ville Syrjäläd9be05b2018-11-06 21:16:24 +0200633 new_plane_state->crtc_w, new_plane_state->crtc_h,
634 new_plane_state->crtc_x, new_plane_state->crtc_y);
Rob Clark5e743732014-12-18 16:01:51 -0500635 return -ERANGE;
636 }
637
Ville Syrjäläd9be05b2018-11-06 21:16:24 +0200638 fb_width = fb->width << 16;
639 fb_height = fb->height << 16;
Rob Clark5e743732014-12-18 16:01:51 -0500640
641 /* Make sure source coordinates are inside the fb. */
Ville Syrjäläd9be05b2018-11-06 21:16:24 +0200642 if (new_plane_state->src_w > fb_width ||
643 new_plane_state->src_x > fb_width - new_plane_state->src_w ||
644 new_plane_state->src_h > fb_height ||
645 new_plane_state->src_y > fb_height - new_plane_state->src_h) {
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300646 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid source coordinates "
Ville Syrjälä0338f0d2017-11-01 20:35:33 +0200647 "%u.%06ux%u.%06u+%u.%06u+%u.%06u (fb %ux%u)\n",
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300648 plane->base.id, plane->name,
Ville Syrjäläd9be05b2018-11-06 21:16:24 +0200649 new_plane_state->src_w >> 16,
650 ((new_plane_state->src_w & 0xffff) * 15625) >> 10,
651 new_plane_state->src_h >> 16,
652 ((new_plane_state->src_h & 0xffff) * 15625) >> 10,
653 new_plane_state->src_x >> 16,
654 ((new_plane_state->src_x & 0xffff) * 15625) >> 10,
655 new_plane_state->src_y >> 16,
656 ((new_plane_state->src_y & 0xffff) * 15625) >> 10,
657 fb->width, fb->height);
Rob Clark5e743732014-12-18 16:01:51 -0500658 return -ENOSPC;
659 }
660
Lukasz Spintzykd3b21762018-05-23 19:04:08 -0700661 clips = drm_plane_get_damage_clips(new_plane_state);
662 num_clips = drm_plane_get_damage_clips_count(new_plane_state);
663
664 /* Make sure damage clips are valid and inside the fb. */
665 while (num_clips > 0) {
666 if (clips->x1 >= clips->x2 ||
667 clips->y1 >= clips->y2 ||
668 clips->x1 < 0 ||
669 clips->y1 < 0 ||
670 clips->x2 > fb_width ||
671 clips->y2 > fb_height) {
672 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid damage clip %d %d %d %d\n",
673 plane->base.id, plane->name, clips->x1,
674 clips->y1, clips->x2, clips->y2);
675 return -EINVAL;
676 }
677 clips++;
678 num_clips--;
679 }
680
Ville Syrjäläd9be05b2018-11-06 21:16:24 +0200681 if (plane_switching_crtc(old_plane_state, new_plane_state)) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200682 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
683 plane->base.id, plane->name);
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200684 return -EINVAL;
685 }
686
Rob Clark5e743732014-12-18 16:01:51 -0500687 return 0;
688}
689
Rob Clarkfceffb322016-11-05 11:08:09 -0400690static void drm_atomic_plane_print_state(struct drm_printer *p,
691 const struct drm_plane_state *state)
692{
693 struct drm_plane *plane = state->plane;
694 struct drm_rect src = drm_plane_state_src(state);
695 struct drm_rect dest = drm_plane_state_dest(state);
696
697 drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
698 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
699 drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
Noralf Trønnesf02b6042017-11-07 20:13:41 +0100700 if (state->fb)
701 drm_framebuffer_print_info(p, 2, state->fb);
Rob Clarkfceffb322016-11-05 11:08:09 -0400702 drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
703 drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
704 drm_printf(p, "\trotation=%x\n", state->rotation);
Benjamin Gaignardf8878bb2018-06-05 15:54:01 +0200705 drm_printf(p, "\tnormalized-zpos=%x\n", state->normalized_zpos);
Ville Syrjälä56dbbaf2018-02-19 22:28:46 +0200706 drm_printf(p, "\tcolor-encoding=%s\n",
707 drm_get_color_encoding_name(state->color_encoding));
708 drm_printf(p, "\tcolor-range=%s\n",
709 drm_get_color_range_name(state->color_range));
Rob Clarkfceffb322016-11-05 11:08:09 -0400710
711 if (plane->funcs->atomic_print_state)
712 plane->funcs->atomic_print_state(p, state);
713}
714
Rob Clark5e743732014-12-18 16:01:51 -0500715/**
Daniel Vetterda6c0592017-12-14 21:30:53 +0100716 * DOC: handling driver private state
717 *
718 * Very often the DRM objects exposed to userspace in the atomic modeset api
719 * (&drm_connector, &drm_crtc and &drm_plane) do not map neatly to the
720 * underlying hardware. Especially for any kind of shared resources (e.g. shared
721 * clocks, scaler units, bandwidth and fifo limits shared among a group of
722 * planes or CRTCs, and so on) it makes sense to model these as independent
723 * objects. Drivers then need to do similar state tracking and commit ordering for
724 * such private (since not exposed to userpace) objects as the atomic core and
725 * helpers already provide for connectors, planes and CRTCs.
726 *
727 * To make this easier on drivers the atomic core provides some support to track
728 * driver private state objects using struct &drm_private_obj, with the
729 * associated state struct &drm_private_state.
730 *
731 * Similar to userspace-exposed objects, private state structures can be
Daniel Vetter0380c682019-12-04 11:00:11 +0100732 * acquired by calling drm_atomic_get_private_obj_state(). This also takes care
733 * of locking, hence drivers should not have a need to call drm_modeset_lock()
734 * directly. Sequence of the actual hardware state commit is not handled,
735 * drivers might need to keep track of struct drm_crtc_commit within subclassed
736 * structure of &drm_private_state as necessary, e.g. similar to
737 * &drm_plane_state.commit. See also &drm_atomic_state.fake_commit.
Daniel Vetterda6c0592017-12-14 21:30:53 +0100738 *
739 * All private state structures contained in a &drm_atomic_state update can be
740 * iterated using for_each_oldnew_private_obj_in_state(),
741 * for_each_new_private_obj_in_state() and for_each_old_private_obj_in_state().
742 * Drivers are recommended to wrap these for each type of driver private state
743 * object they have, filtering on &drm_private_obj.funcs using for_each_if(), at
744 * least if they want to iterate over all objects of a given type.
745 *
746 * An earlier way to handle driver private state was by subclassing struct
747 * &drm_atomic_state. But since that encourages non-standard ways to implement
748 * the check/commit split atomic requires (by using e.g. "check and rollback or
749 * commit instead" of "duplicate state, check, then either commit or release
750 * duplicated state) it is deprecated in favour of using &drm_private_state.
751 */
752
753/**
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300754 * drm_atomic_private_obj_init - initialize private object
Rob Clarkb962a122018-10-22 14:31:22 +0200755 * @dev: DRM device this object will be attached to
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300756 * @obj: private object
757 * @state: initial private object state
758 * @funcs: pointer to the struct of function pointers that identify the object
759 * type
760 *
761 * Initialize the private object, which can be embedded into any
762 * driver private object that needs its own atomic state.
763 */
764void
Rob Clarkb962a122018-10-22 14:31:22 +0200765drm_atomic_private_obj_init(struct drm_device *dev,
766 struct drm_private_obj *obj,
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300767 struct drm_private_state *state,
768 const struct drm_private_state_funcs *funcs)
769{
770 memset(obj, 0, sizeof(*obj));
771
Rob Clarkb962a122018-10-22 14:31:22 +0200772 drm_modeset_lock_init(&obj->lock);
773
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300774 obj->state = state;
775 obj->funcs = funcs;
Rob Clarkb962a122018-10-22 14:31:22 +0200776 list_add_tail(&obj->head, &dev->mode_config.privobj_list);
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300777}
778EXPORT_SYMBOL(drm_atomic_private_obj_init);
779
780/**
781 * drm_atomic_private_obj_fini - finalize private object
782 * @obj: private object
783 *
784 * Finalize the private object.
785 */
786void
787drm_atomic_private_obj_fini(struct drm_private_obj *obj)
788{
Rob Clarkb962a122018-10-22 14:31:22 +0200789 list_del(&obj->head);
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300790 obj->funcs->atomic_destroy_state(obj, obj->state);
Rob Clarkb962a122018-10-22 14:31:22 +0200791 drm_modeset_lock_fini(&obj->lock);
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300792}
793EXPORT_SYMBOL(drm_atomic_private_obj_fini);
794
795/**
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700796 * drm_atomic_get_private_obj_state - get private object state
797 * @state: global atomic state
798 * @obj: private object to get the state for
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700799 *
800 * This function returns the private object state for the given private object,
Rob Clarkb962a122018-10-22 14:31:22 +0200801 * allocating the state if needed. It will also grab the relevant private
802 * object lock to make sure that the state is consistent.
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700803 *
804 * RETURNS:
805 *
806 * Either the allocated state or the error code encoded into a pointer.
807 */
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300808struct drm_private_state *
809drm_atomic_get_private_obj_state(struct drm_atomic_state *state,
810 struct drm_private_obj *obj)
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700811{
Rob Clarkb962a122018-10-22 14:31:22 +0200812 int index, num_objs, i, ret;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700813 size_t size;
814 struct __drm_private_objs_state *arr;
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300815 struct drm_private_state *obj_state;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700816
817 for (i = 0; i < state->num_private_objs; i++)
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300818 if (obj == state->private_objs[i].ptr)
819 return state->private_objs[i].state;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700820
Rob Clarkb962a122018-10-22 14:31:22 +0200821 ret = drm_modeset_lock(&obj->lock, state->acquire_ctx);
822 if (ret)
823 return ERR_PTR(ret);
824
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700825 num_objs = state->num_private_objs + 1;
826 size = sizeof(*state->private_objs) * num_objs;
827 arr = krealloc(state->private_objs, size, GFP_KERNEL);
828 if (!arr)
829 return ERR_PTR(-ENOMEM);
830
831 state->private_objs = arr;
832 index = state->num_private_objs;
833 memset(&state->private_objs[index], 0, sizeof(*state->private_objs));
834
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300835 obj_state = obj->funcs->atomic_duplicate_state(obj);
836 if (!obj_state)
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700837 return ERR_PTR(-ENOMEM);
838
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300839 state->private_objs[index].state = obj_state;
840 state->private_objs[index].old_state = obj->state;
841 state->private_objs[index].new_state = obj_state;
842 state->private_objs[index].ptr = obj;
Alexandru Gheorghee89ea352018-05-30 18:30:52 +0100843 obj_state->state = state;
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300844
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700845 state->num_private_objs = num_objs;
846
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300847 DRM_DEBUG_ATOMIC("Added new private object %p state %p to %p\n",
848 obj, obj_state, state);
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700849
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300850 return obj_state;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700851}
852EXPORT_SYMBOL(drm_atomic_get_private_obj_state);
853
854/**
james qian wang (Arm Technology China)9801a7e2019-01-22 11:05:41 +0000855 * drm_atomic_get_old_private_obj_state
856 * @state: global atomic state object
857 * @obj: private_obj to grab
858 *
859 * This function returns the old private object state for the given private_obj,
860 * or NULL if the private_obj is not part of the global atomic state.
861 */
862struct drm_private_state *
863drm_atomic_get_old_private_obj_state(struct drm_atomic_state *state,
864 struct drm_private_obj *obj)
865{
866 int i;
867
868 for (i = 0; i < state->num_private_objs; i++)
869 if (obj == state->private_objs[i].ptr)
870 return state->private_objs[i].old_state;
871
872 return NULL;
873}
874EXPORT_SYMBOL(drm_atomic_get_old_private_obj_state);
875
876/**
877 * drm_atomic_get_new_private_obj_state
878 * @state: global atomic state object
879 * @obj: private_obj to grab
880 *
881 * This function returns the new private object state for the given private_obj,
882 * or NULL if the private_obj is not part of the global atomic state.
883 */
884struct drm_private_state *
885drm_atomic_get_new_private_obj_state(struct drm_atomic_state *state,
886 struct drm_private_obj *obj)
887{
888 int i;
889
890 for (i = 0; i < state->num_private_objs; i++)
891 if (obj == state->private_objs[i].ptr)
892 return state->private_objs[i].new_state;
893
894 return NULL;
895}
896EXPORT_SYMBOL(drm_atomic_get_new_private_obj_state);
897
898/**
Laurent Pinchart1b27fbd2019-06-11 16:51:43 -0400899 * drm_atomic_get_old_connector_for_encoder - Get old connector for an encoder
900 * @state: Atomic state
901 * @encoder: The encoder to fetch the connector state for
902 *
903 * This function finds and returns the connector that was connected to @encoder
904 * as specified by the @state.
905 *
906 * If there is no connector in @state which previously had @encoder connected to
907 * it, this function will return NULL. While this may seem like an invalid use
908 * case, it is sometimes useful to differentiate commits which had no prior
909 * connectors attached to @encoder vs ones that did (and to inspect their
910 * state). This is especially true in enable hooks because the pipeline has
911 * changed.
912 *
913 * Returns: The old connector connected to @encoder, or NULL if the encoder is
914 * not connected.
915 */
916struct drm_connector *
917drm_atomic_get_old_connector_for_encoder(struct drm_atomic_state *state,
918 struct drm_encoder *encoder)
919{
920 struct drm_connector_state *conn_state;
921 struct drm_connector *connector;
922 unsigned int i;
923
924 for_each_old_connector_in_state(state, connector, conn_state, i) {
925 if (conn_state->best_encoder == encoder)
926 return connector;
927 }
928
929 return NULL;
930}
931EXPORT_SYMBOL(drm_atomic_get_old_connector_for_encoder);
932
933/**
934 * drm_atomic_get_new_connector_for_encoder - Get new connector for an encoder
935 * @state: Atomic state
936 * @encoder: The encoder to fetch the connector state for
937 *
938 * This function finds and returns the connector that will be connected to
939 * @encoder as specified by the @state.
940 *
941 * If there is no connector in @state which will have @encoder connected to it,
942 * this function will return NULL. While this may seem like an invalid use case,
943 * it is sometimes useful to differentiate commits which have no connectors
944 * attached to @encoder vs ones that do (and to inspect their state). This is
945 * especially true in disable hooks because the pipeline will change.
946 *
947 * Returns: The new connector connected to @encoder, or NULL if the encoder is
948 * not connected.
949 */
950struct drm_connector *
951drm_atomic_get_new_connector_for_encoder(struct drm_atomic_state *state,
952 struct drm_encoder *encoder)
953{
954 struct drm_connector_state *conn_state;
955 struct drm_connector *connector;
956 unsigned int i;
957
958 for_each_new_connector_in_state(state, connector, conn_state, i) {
959 if (conn_state->best_encoder == encoder)
960 return connector;
961 }
962
963 return NULL;
964}
965EXPORT_SYMBOL(drm_atomic_get_new_connector_for_encoder);
966
967/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200968 * drm_atomic_get_connector_state - get connector state
969 * @state: global atomic state object
970 * @connector: connector to get state object for
971 *
972 * This function returns the connector state for the given connector,
973 * allocating it if needed. It will also grab the relevant connector lock to
974 * make sure that the state is consistent.
975 *
976 * Returns:
977 *
978 * Either the allocated state or the error code encoded into the pointer. When
979 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
980 * entire atomic sequence must be restarted. All other errors are fatal.
981 */
982struct drm_connector_state *
983drm_atomic_get_connector_state(struct drm_atomic_state *state,
984 struct drm_connector *connector)
985{
986 int ret, index;
987 struct drm_mode_config *config = &connector->dev->mode_config;
988 struct drm_connector_state *connector_state;
989
Maarten Lankhorst7f4eaa892016-05-03 11:12:31 +0200990 WARN_ON(!state->acquire_ctx);
991
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100992 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
993 if (ret)
994 return ERR_PTR(ret);
995
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200996 index = drm_connector_index(connector);
997
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100998 if (index >= state->num_connector) {
Daniel Vetter63e83c12016-06-02 00:06:32 +0200999 struct __drm_connnectors_state *c;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01001000 int alloc = max(index + 1, config->num_connector);
1001
Bartosz Golaszewski32ce2552020-12-14 19:04:16 -08001002 c = krealloc_array(state->connectors, alloc,
1003 sizeof(*state->connectors), GFP_KERNEL);
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01001004 if (!c)
1005 return ERR_PTR(-ENOMEM);
1006
1007 state->connectors = c;
1008 memset(&state->connectors[state->num_connector], 0,
1009 sizeof(*state->connectors) * (alloc - state->num_connector));
1010
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01001011 state->num_connector = alloc;
Daniel Vetterf52b69f12014-11-19 18:38:08 +01001012 }
1013
Daniel Vetter63e83c12016-06-02 00:06:32 +02001014 if (state->connectors[index].state)
1015 return state->connectors[index].state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001016
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001017 connector_state = connector->funcs->atomic_duplicate_state(connector);
1018 if (!connector_state)
1019 return ERR_PTR(-ENOMEM);
1020
Thierry Redingad093602017-02-28 15:46:39 +01001021 drm_connector_get(connector);
Daniel Vetter63e83c12016-06-02 00:06:32 +02001022 state->connectors[index].state = connector_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01001023 state->connectors[index].old_state = connector->state;
1024 state->connectors[index].new_state = connector_state;
Daniel Vetter63e83c12016-06-02 00:06:32 +02001025 state->connectors[index].ptr = connector;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001026 connector_state->state = state;
1027
Russell King6ac7c542017-02-13 12:27:03 +00001028 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d:%s] %p state to %p\n",
1029 connector->base.id, connector->name,
1030 connector_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001031
1032 if (connector_state->crtc) {
1033 struct drm_crtc_state *crtc_state;
1034
1035 crtc_state = drm_atomic_get_crtc_state(state,
1036 connector_state->crtc);
1037 if (IS_ERR(crtc_state))
1038 return ERR_CAST(crtc_state);
1039 }
1040
1041 return connector_state;
1042}
1043EXPORT_SYMBOL(drm_atomic_get_connector_state);
1044
Rob Clarkfceffb322016-11-05 11:08:09 -04001045static void drm_atomic_connector_print_state(struct drm_printer *p,
1046 const struct drm_connector_state *state)
1047{
1048 struct drm_connector *connector = state->connector;
1049
1050 drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
1051 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
Sean Paul1452c252019-06-12 10:50:19 -04001052 drm_printf(p, "\tself_refresh_aware=%d\n", state->self_refresh_aware);
Rob Clarkfceffb322016-11-05 11:08:09 -04001053
Brian Starkey8cbc5ca2017-11-02 16:49:51 +00001054 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
1055 if (state->writeback_job && state->writeback_job->fb)
1056 drm_printf(p, "\tfb=%d\n", state->writeback_job->fb->base.id);
1057
Rob Clarkfceffb322016-11-05 11:08:09 -04001058 if (connector->funcs->atomic_print_state)
1059 connector->funcs->atomic_print_state(p, state);
1060}
1061
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001062/**
Boris Brezillon75146592020-01-28 14:55:03 +01001063 * drm_atomic_get_bridge_state - get bridge state
1064 * @state: global atomic state object
1065 * @bridge: bridge to get state object for
1066 *
1067 * This function returns the bridge state for the given bridge, allocating it
1068 * if needed. It will also grab the relevant bridge lock to make sure that the
1069 * state is consistent.
1070 *
1071 * Returns:
1072 *
1073 * Either the allocated state or the error code encoded into the pointer. When
1074 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
1075 * entire atomic sequence must be restarted.
1076 */
1077struct drm_bridge_state *
1078drm_atomic_get_bridge_state(struct drm_atomic_state *state,
1079 struct drm_bridge *bridge)
1080{
1081 struct drm_private_state *obj_state;
1082
1083 obj_state = drm_atomic_get_private_obj_state(state, &bridge->base);
1084 if (IS_ERR(obj_state))
1085 return ERR_CAST(obj_state);
1086
1087 return drm_priv_to_bridge_state(obj_state);
1088}
1089EXPORT_SYMBOL(drm_atomic_get_bridge_state);
1090
1091/**
1092 * drm_atomic_get_old_bridge_state - get old bridge state, if it exists
1093 * @state: global atomic state object
1094 * @bridge: bridge to grab
1095 *
1096 * This function returns the old bridge state for the given bridge, or NULL if
1097 * the bridge is not part of the global atomic state.
1098 */
1099struct drm_bridge_state *
1100drm_atomic_get_old_bridge_state(struct drm_atomic_state *state,
1101 struct drm_bridge *bridge)
1102{
1103 struct drm_private_state *obj_state;
1104
1105 obj_state = drm_atomic_get_old_private_obj_state(state, &bridge->base);
1106 if (!obj_state)
1107 return NULL;
1108
1109 return drm_priv_to_bridge_state(obj_state);
1110}
1111EXPORT_SYMBOL(drm_atomic_get_old_bridge_state);
1112
1113/**
1114 * drm_atomic_get_new_bridge_state - get new bridge state, if it exists
1115 * @state: global atomic state object
1116 * @bridge: bridge to grab
1117 *
1118 * This function returns the new bridge state for the given bridge, or NULL if
1119 * the bridge is not part of the global atomic state.
1120 */
1121struct drm_bridge_state *
1122drm_atomic_get_new_bridge_state(struct drm_atomic_state *state,
1123 struct drm_bridge *bridge)
1124{
1125 struct drm_private_state *obj_state;
1126
1127 obj_state = drm_atomic_get_new_private_obj_state(state, &bridge->base);
1128 if (!obj_state)
1129 return NULL;
1130
1131 return drm_priv_to_bridge_state(obj_state);
1132}
1133EXPORT_SYMBOL(drm_atomic_get_new_bridge_state);
1134
1135/**
1136 * drm_atomic_add_encoder_bridges - add bridges attached to an encoder
1137 * @state: atomic state
1138 * @encoder: DRM encoder
1139 *
1140 * This function adds all bridges attached to @encoder. This is needed to add
1141 * bridge states to @state and make them available when
Boris Brezillon91ea8332020-02-18 16:15:03 +01001142 * &drm_bridge_funcs.atomic_check(), &drm_bridge_funcs.atomic_pre_enable(),
1143 * &drm_bridge_funcs.atomic_enable(),
1144 * &drm_bridge_funcs.atomic_disable_post_disable() are called.
Boris Brezillon75146592020-01-28 14:55:03 +01001145 *
1146 * Returns:
1147 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1148 * then the w/w mutex code has detected a deadlock and the entire atomic
1149 * sequence must be restarted. All other errors are fatal.
1150 */
1151int
1152drm_atomic_add_encoder_bridges(struct drm_atomic_state *state,
1153 struct drm_encoder *encoder)
1154{
1155 struct drm_bridge_state *bridge_state;
1156 struct drm_bridge *bridge;
1157
1158 if (!encoder)
1159 return 0;
1160
1161 DRM_DEBUG_ATOMIC("Adding all bridges for [encoder:%d:%s] to %p\n",
1162 encoder->base.id, encoder->name, state);
1163
1164 drm_for_each_bridge_in_chain(encoder, bridge) {
1165 /* Skip bridges that don't implement the atomic state hooks. */
1166 if (!bridge->funcs->atomic_duplicate_state)
1167 continue;
1168
1169 bridge_state = drm_atomic_get_bridge_state(state, bridge);
1170 if (IS_ERR(bridge_state))
1171 return PTR_ERR(bridge_state);
1172 }
1173
1174 return 0;
1175}
1176EXPORT_SYMBOL(drm_atomic_add_encoder_bridges);
1177
1178/**
Thierry Reding42240c92019-12-06 14:53:36 +01001179 * drm_atomic_add_affected_connectors - add connectors for CRTC
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001180 * @state: atomic state
Thierry Reding42240c92019-12-06 14:53:36 +01001181 * @crtc: DRM CRTC
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001182 *
1183 * This function walks the current configuration and adds all connectors
1184 * currently using @crtc to the atomic configuration @state. Note that this
1185 * function must acquire the connection mutex. This can potentially cause
Thierry Reding42240c92019-12-06 14:53:36 +01001186 * unneeded seralization if the update is just for the planes on one CRTC. Hence
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001187 * drivers and helpers should only call this when really needed (e.g. when a
1188 * full modeset needs to happen due to some change).
1189 *
1190 * Returns:
1191 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1192 * then the w/w mutex code has detected a deadlock and the entire atomic
1193 * sequence must be restarted. All other errors are fatal.
1194 */
1195int
1196drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1197 struct drm_crtc *crtc)
1198{
1199 struct drm_mode_config *config = &state->dev->mode_config;
1200 struct drm_connector *connector;
1201 struct drm_connector_state *conn_state;
Daniel Vetter613051d2016-12-14 00:08:06 +01001202 struct drm_connector_list_iter conn_iter;
Maarten Lankhorst5351bbd2017-01-16 10:37:39 +01001203 struct drm_crtc_state *crtc_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001204 int ret;
1205
Maarten Lankhorst5351bbd2017-01-16 10:37:39 +01001206 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1207 if (IS_ERR(crtc_state))
1208 return PTR_ERR(crtc_state);
1209
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001210 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1211 if (ret)
1212 return ret;
1213
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001214 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1215 crtc->base.id, crtc->name, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001216
1217 /*
Maarten Lankhorst5351bbd2017-01-16 10:37:39 +01001218 * Changed connectors are already in @state, so only need to look
1219 * at the connector_mask in crtc_state.
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001220 */
Thierry Redingb982dab2017-02-28 15:46:43 +01001221 drm_connector_list_iter_begin(state->dev, &conn_iter);
Daniel Vetter613051d2016-12-14 00:08:06 +01001222 drm_for_each_connector_iter(connector, &conn_iter) {
Ville Syrjälä73705732018-06-26 22:47:10 +03001223 if (!(crtc_state->connector_mask & drm_connector_mask(connector)))
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001224 continue;
1225
1226 conn_state = drm_atomic_get_connector_state(state, connector);
Daniel Vetter613051d2016-12-14 00:08:06 +01001227 if (IS_ERR(conn_state)) {
Thierry Redingb982dab2017-02-28 15:46:43 +01001228 drm_connector_list_iter_end(&conn_iter);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001229 return PTR_ERR(conn_state);
Daniel Vetter613051d2016-12-14 00:08:06 +01001230 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001231 }
Thierry Redingb982dab2017-02-28 15:46:43 +01001232 drm_connector_list_iter_end(&conn_iter);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001233
1234 return 0;
1235}
1236EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1237
1238/**
Thierry Reding42240c92019-12-06 14:53:36 +01001239 * drm_atomic_add_affected_planes - add planes for CRTC
Maarten Lankhorste01e9f72015-05-19 16:41:02 +02001240 * @state: atomic state
Thierry Reding42240c92019-12-06 14:53:36 +01001241 * @crtc: DRM CRTC
Maarten Lankhorste01e9f72015-05-19 16:41:02 +02001242 *
1243 * This function walks the current configuration and adds all planes
1244 * currently used by @crtc to the atomic configuration @state. This is useful
1245 * when an atomic commit also needs to check all currently enabled plane on
1246 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1247 * to avoid special code to force-enable all planes.
1248 *
1249 * Since acquiring a plane state will always also acquire the w/w mutex of the
1250 * current CRTC for that plane (if there is any) adding all the plane states for
1251 * a CRTC will not reduce parallism of atomic updates.
1252 *
1253 * Returns:
1254 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1255 * then the w/w mutex code has detected a deadlock and the entire atomic
1256 * sequence must be restarted. All other errors are fatal.
1257 */
1258int
1259drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1260 struct drm_crtc *crtc)
1261{
Ville Syrjälä534903d2018-11-01 20:46:44 +02001262 const struct drm_crtc_state *old_crtc_state =
1263 drm_atomic_get_old_crtc_state(state, crtc);
Maarten Lankhorste01e9f72015-05-19 16:41:02 +02001264 struct drm_plane *plane;
1265
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001266 WARN_ON(!drm_atomic_get_new_crtc_state(state, crtc));
Maarten Lankhorste01e9f72015-05-19 16:41:02 +02001267
Ville Syrjäläb6f690a2018-06-11 22:34:01 +03001268 DRM_DEBUG_ATOMIC("Adding all current planes for [CRTC:%d:%s] to %p\n",
1269 crtc->base.id, crtc->name, state);
1270
Ville Syrjälä534903d2018-11-01 20:46:44 +02001271 drm_for_each_plane_mask(plane, state->dev, old_crtc_state->plane_mask) {
Maarten Lankhorste01e9f72015-05-19 16:41:02 +02001272 struct drm_plane_state *plane_state =
1273 drm_atomic_get_plane_state(state, plane);
1274
1275 if (IS_ERR(plane_state))
1276 return PTR_ERR(plane_state);
1277 }
1278 return 0;
1279}
1280EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1281
1282/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001283 * drm_atomic_check_only - check whether a given config would work
1284 * @state: atomic configuration to check
1285 *
1286 * Note that this function can return -EDEADLK if the driver needed to acquire
1287 * more locks but encountered a deadlock. The caller must then do the usual w/w
1288 * backoff dance and restart. All other errors are fatal.
1289 *
1290 * Returns:
1291 * 0 on success, negative error code on failure.
1292 */
1293int drm_atomic_check_only(struct drm_atomic_state *state)
1294{
Rob Clark5e743732014-12-18 16:01:51 -05001295 struct drm_device *dev = state->dev;
1296 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001297 struct drm_plane *plane;
Ville Syrjäläd9be05b2018-11-06 21:16:24 +02001298 struct drm_plane_state *old_plane_state;
1299 struct drm_plane_state *new_plane_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001300 struct drm_crtc *crtc;
Ville Syrjäläb2432ad2018-11-01 20:46:45 +02001301 struct drm_crtc_state *old_crtc_state;
1302 struct drm_crtc_state *new_crtc_state;
Brian Starkey935774c2017-03-29 17:42:32 +01001303 struct drm_connector *conn;
1304 struct drm_connector_state *conn_state;
Fabio M. De Francesco1cdb0052021-04-12 12:53:09 +02001305 unsigned int requested_crtc = 0;
1306 unsigned int affected_crtc = 0;
Rob Clark5e743732014-12-18 16:01:51 -05001307 int i, ret = 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001308
Daniel Vetter17a38d92015-02-22 12:24:16 +01001309 DRM_DEBUG_ATOMIC("checking %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001310
Daniel Vetterfb6473a2020-09-25 10:46:50 +02001311 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
1312 requested_crtc |= drm_crtc_mask(crtc);
1313
Ville Syrjäläd9be05b2018-11-06 21:16:24 +02001314 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
1315 ret = drm_atomic_plane_check(old_plane_state, new_plane_state);
Rob Clark5e743732014-12-18 16:01:51 -05001316 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001317 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1318 plane->base.id, plane->name);
Rob Clark5e743732014-12-18 16:01:51 -05001319 return ret;
1320 }
1321 }
1322
Ville Syrjäläb2432ad2018-11-01 20:46:45 +02001323 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
1324 ret = drm_atomic_crtc_check(old_crtc_state, new_crtc_state);
Rob Clark5e743732014-12-18 16:01:51 -05001325 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001326 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1327 crtc->base.id, crtc->name);
Rob Clark5e743732014-12-18 16:01:51 -05001328 return ret;
1329 }
1330 }
1331
Brian Starkey935774c2017-03-29 17:42:32 +01001332 for_each_new_connector_in_state(state, conn, conn_state, i) {
1333 ret = drm_atomic_connector_check(conn, conn_state);
1334 if (ret) {
1335 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] atomic core check failed\n",
1336 conn->base.id, conn->name);
1337 return ret;
1338 }
1339 }
1340
Lyude Paul14d4e522018-04-11 19:42:40 -04001341 if (config->funcs->atomic_check) {
Rob Clark5e743732014-12-18 16:01:51 -05001342 ret = config->funcs->atomic_check(state->dev, state);
1343
Lyude Paul14d4e522018-04-11 19:42:40 -04001344 if (ret) {
1345 DRM_DEBUG_ATOMIC("atomic driver check for %p failed: %d\n",
1346 state, ret);
1347 return ret;
1348 }
1349 }
Maarten Lankhorsta0ffc512017-08-15 11:57:06 +02001350
Rob Clarkd34f20d2014-12-18 16:01:56 -05001351 if (!state->allow_modeset) {
Ville Syrjäläb2432ad2018-11-01 20:46:45 +02001352 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1353 if (drm_atomic_crtc_needs_modeset(new_crtc_state)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001354 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1355 crtc->base.id, crtc->name);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001356 return -EINVAL;
1357 }
1358 }
1359 }
1360
Daniel Vetterfb6473a2020-09-25 10:46:50 +02001361 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
1362 affected_crtc |= drm_crtc_mask(crtc);
1363
1364 /*
1365 * For commits that allow modesets drivers can add other CRTCs to the
1366 * atomic commit, e.g. when they need to reallocate global resources.
1367 * This can cause spurious EBUSY, which robs compositors of a very
1368 * effective sanity check for their drawing loop. Therefor only allow
1369 * drivers to add unrelated CRTC states for modeset commits.
1370 *
1371 * FIXME: Should add affected_crtc mask to the ATOMIC IOCTL as an output
1372 * so compositors know what's going on.
1373 */
1374 if (affected_crtc != requested_crtc) {
1375 DRM_DEBUG_ATOMIC("driver added CRTC to commit: requested 0x%x, affected 0x%0x\n",
1376 requested_crtc, affected_crtc);
1377 WARN(!state->allow_modeset, "adding CRTC not allowed without modesets: requested 0x%x, affected 0x%0x\n",
1378 requested_crtc, affected_crtc);
1379 }
1380
Maarten Lankhorsta0ffc512017-08-15 11:57:06 +02001381 return 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001382}
1383EXPORT_SYMBOL(drm_atomic_check_only);
1384
1385/**
1386 * drm_atomic_commit - commit configuration atomically
1387 * @state: atomic configuration to check
1388 *
1389 * Note that this function can return -EDEADLK if the driver needed to acquire
1390 * more locks but encountered a deadlock. The caller must then do the usual w/w
1391 * backoff dance and restart. All other errors are fatal.
1392 *
Maarten Lankhorst76fede22017-01-04 12:34:00 +01001393 * This function will take its own reference on @state.
1394 * Callers should always release their reference with drm_atomic_state_put().
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001395 *
1396 * Returns:
1397 * 0 on success, negative error code on failure.
1398 */
1399int drm_atomic_commit(struct drm_atomic_state *state)
1400{
1401 struct drm_mode_config *config = &state->dev->mode_config;
1402 int ret;
1403
1404 ret = drm_atomic_check_only(state);
1405 if (ret)
1406 return ret;
1407
Colin Ian Kinga0752d42017-04-12 17:27:22 +01001408 DRM_DEBUG_ATOMIC("committing %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001409
1410 return config->funcs->atomic_commit(state->dev, state, false);
1411}
1412EXPORT_SYMBOL(drm_atomic_commit);
1413
1414/**
Daniel Vetterd5745282017-01-25 07:26:45 +01001415 * drm_atomic_nonblocking_commit - atomic nonblocking commit
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001416 * @state: atomic configuration to check
1417 *
1418 * Note that this function can return -EDEADLK if the driver needed to acquire
1419 * more locks but encountered a deadlock. The caller must then do the usual w/w
1420 * backoff dance and restart. All other errors are fatal.
1421 *
Maarten Lankhorst76fede22017-01-04 12:34:00 +01001422 * This function will take its own reference on @state.
1423 * Callers should always release their reference with drm_atomic_state_put().
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001424 *
1425 * Returns:
1426 * 0 on success, negative error code on failure.
1427 */
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02001428int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001429{
1430 struct drm_mode_config *config = &state->dev->mode_config;
1431 int ret;
1432
1433 ret = drm_atomic_check_only(state);
1434 if (ret)
1435 return ret;
1436
Colin Ian Kinga0752d42017-04-12 17:27:22 +01001437 DRM_DEBUG_ATOMIC("committing %p nonblocking\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001438
1439 return config->funcs->atomic_commit(state->dev, state, true);
1440}
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02001441EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001442
Noralf Trønnesdf737892019-05-31 16:01:10 +02001443/* just used from drm-client and atomic-helper: */
1444int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
1445 struct drm_plane_state *plane_state)
1446{
1447 int ret;
1448
1449 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1450 if (ret != 0)
1451 return ret;
1452
1453 drm_atomic_set_fb_for_plane(plane_state, NULL);
1454 plane_state->crtc_x = 0;
1455 plane_state->crtc_y = 0;
1456 plane_state->crtc_w = 0;
1457 plane_state->crtc_h = 0;
1458 plane_state->src_x = 0;
1459 plane_state->src_y = 0;
1460 plane_state->src_w = 0;
1461 plane_state->src_h = 0;
1462
1463 return 0;
1464}
1465EXPORT_SYMBOL(__drm_atomic_helper_disable_plane);
1466
1467static int update_output_state(struct drm_atomic_state *state,
1468 struct drm_mode_set *set)
1469{
1470 struct drm_device *dev = set->crtc->dev;
1471 struct drm_crtc *crtc;
1472 struct drm_crtc_state *new_crtc_state;
1473 struct drm_connector *connector;
1474 struct drm_connector_state *new_conn_state;
1475 int ret, i;
1476
1477 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1478 state->acquire_ctx);
1479 if (ret)
1480 return ret;
1481
1482 /* First disable all connectors on the target crtc. */
1483 ret = drm_atomic_add_affected_connectors(state, set->crtc);
1484 if (ret)
1485 return ret;
1486
1487 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
1488 if (new_conn_state->crtc == set->crtc) {
1489 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
1490 NULL);
1491 if (ret)
1492 return ret;
1493
1494 /* Make sure legacy setCrtc always re-trains */
1495 new_conn_state->link_status = DRM_LINK_STATUS_GOOD;
1496 }
1497 }
1498
1499 /* Then set all connectors from set->connectors on the target crtc */
1500 for (i = 0; i < set->num_connectors; i++) {
1501 new_conn_state = drm_atomic_get_connector_state(state,
1502 set->connectors[i]);
1503 if (IS_ERR(new_conn_state))
1504 return PTR_ERR(new_conn_state);
1505
1506 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
1507 set->crtc);
1508 if (ret)
1509 return ret;
1510 }
1511
1512 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1513 /*
1514 * Don't update ->enable for the CRTC in the set_config request,
1515 * since a mismatch would indicate a bug in the upper layers.
1516 * The actual modeset code later on will catch any
1517 * inconsistencies here.
1518 */
1519 if (crtc == set->crtc)
1520 continue;
1521
1522 if (!new_crtc_state->connector_mask) {
1523 ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state,
1524 NULL);
1525 if (ret < 0)
1526 return ret;
1527
1528 new_crtc_state->active = false;
1529 }
1530 }
1531
1532 return 0;
1533}
1534
1535/* just used from drm-client and atomic-helper: */
1536int __drm_atomic_helper_set_config(struct drm_mode_set *set,
1537 struct drm_atomic_state *state)
1538{
1539 struct drm_crtc_state *crtc_state;
1540 struct drm_plane_state *primary_state;
1541 struct drm_crtc *crtc = set->crtc;
1542 int hdisplay, vdisplay;
1543 int ret;
1544
1545 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1546 if (IS_ERR(crtc_state))
1547 return PTR_ERR(crtc_state);
1548
1549 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1550 if (IS_ERR(primary_state))
1551 return PTR_ERR(primary_state);
1552
1553 if (!set->mode) {
1554 WARN_ON(set->fb);
1555 WARN_ON(set->num_connectors);
1556
1557 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
1558 if (ret != 0)
1559 return ret;
1560
1561 crtc_state->active = false;
1562
1563 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
1564 if (ret != 0)
1565 return ret;
1566
1567 drm_atomic_set_fb_for_plane(primary_state, NULL);
1568
1569 goto commit;
1570 }
1571
1572 WARN_ON(!set->fb);
1573 WARN_ON(!set->num_connectors);
1574
1575 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
1576 if (ret != 0)
1577 return ret;
1578
1579 crtc_state->active = true;
1580
1581 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1582 if (ret != 0)
1583 return ret;
1584
1585 drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
1586
1587 drm_atomic_set_fb_for_plane(primary_state, set->fb);
1588 primary_state->crtc_x = 0;
1589 primary_state->crtc_y = 0;
1590 primary_state->crtc_w = hdisplay;
1591 primary_state->crtc_h = vdisplay;
1592 primary_state->src_x = set->x << 16;
1593 primary_state->src_y = set->y << 16;
1594 if (drm_rotation_90_or_270(primary_state->rotation)) {
1595 primary_state->src_w = vdisplay << 16;
1596 primary_state->src_h = hdisplay << 16;
1597 } else {
1598 primary_state->src_w = hdisplay << 16;
1599 primary_state->src_h = vdisplay << 16;
1600 }
1601
1602commit:
1603 ret = update_output_state(state, set);
1604 if (ret)
1605 return ret;
1606
1607 return 0;
1608}
1609EXPORT_SYMBOL(__drm_atomic_helper_set_config);
1610
Daniel Vetter72fdb40c2018-09-05 15:57:11 +02001611void drm_atomic_print_state(const struct drm_atomic_state *state)
Rob Clarkfceffb322016-11-05 11:08:09 -04001612{
1613 struct drm_printer p = drm_info_printer(state->dev->dev);
1614 struct drm_plane *plane;
1615 struct drm_plane_state *plane_state;
1616 struct drm_crtc *crtc;
1617 struct drm_crtc_state *crtc_state;
1618 struct drm_connector *connector;
1619 struct drm_connector_state *connector_state;
1620 int i;
1621
1622 DRM_DEBUG_ATOMIC("checking %p\n", state);
1623
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001624 for_each_new_plane_in_state(state, plane, plane_state, i)
Rob Clarkfceffb322016-11-05 11:08:09 -04001625 drm_atomic_plane_print_state(&p, plane_state);
1626
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001627 for_each_new_crtc_in_state(state, crtc, crtc_state, i)
Rob Clarkfceffb322016-11-05 11:08:09 -04001628 drm_atomic_crtc_print_state(&p, crtc_state);
1629
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001630 for_each_new_connector_in_state(state, connector, connector_state, i)
Rob Clarkfceffb322016-11-05 11:08:09 -04001631 drm_atomic_connector_print_state(&p, connector_state);
1632}
1633
Daniel Vetterc2d85562017-04-03 10:32:54 +02001634static void __drm_state_dump(struct drm_device *dev, struct drm_printer *p,
1635 bool take_locks)
1636{
1637 struct drm_mode_config *config = &dev->mode_config;
1638 struct drm_plane *plane;
1639 struct drm_crtc *crtc;
1640 struct drm_connector *connector;
1641 struct drm_connector_list_iter conn_iter;
1642
Lyude Paul3c499ea2018-09-17 13:37:33 -04001643 if (!drm_drv_uses_atomic_modeset(dev))
Daniel Vetterc2d85562017-04-03 10:32:54 +02001644 return;
1645
1646 list_for_each_entry(plane, &config->plane_list, head) {
1647 if (take_locks)
1648 drm_modeset_lock(&plane->mutex, NULL);
1649 drm_atomic_plane_print_state(p, plane->state);
1650 if (take_locks)
1651 drm_modeset_unlock(&plane->mutex);
1652 }
1653
1654 list_for_each_entry(crtc, &config->crtc_list, head) {
1655 if (take_locks)
1656 drm_modeset_lock(&crtc->mutex, NULL);
1657 drm_atomic_crtc_print_state(p, crtc->state);
1658 if (take_locks)
1659 drm_modeset_unlock(&crtc->mutex);
1660 }
1661
1662 drm_connector_list_iter_begin(dev, &conn_iter);
1663 if (take_locks)
1664 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1665 drm_for_each_connector_iter(connector, &conn_iter)
1666 drm_atomic_connector_print_state(p, connector->state);
1667 if (take_locks)
1668 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1669 drm_connector_list_iter_end(&conn_iter);
1670}
1671
Rob Clark6559c902016-11-05 11:08:10 -04001672/**
1673 * drm_state_dump - dump entire device atomic state
1674 * @dev: the drm device
1675 * @p: where to print the state to
1676 *
1677 * Just for debugging. Drivers might want an option to dump state
1678 * to dmesg in case of error irq's. (Hint, you probably want to
1679 * ratelimit this!)
1680 *
Daniel Vetterbd1fbef2020-10-02 09:56:20 +02001681 * The caller must wrap this drm_modeset_lock_all_ctx() and
1682 * drm_modeset_drop_locks(). If this is called from error irq handler, it should
1683 * not be enabled by default - if you are debugging errors you might
1684 * not care that this is racey, but calling this without all modeset locks held
1685 * is inherently unsafe.
Rob Clark6559c902016-11-05 11:08:10 -04001686 */
1687void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
1688{
Daniel Vetterc2d85562017-04-03 10:32:54 +02001689 __drm_state_dump(dev, p, false);
Rob Clark6559c902016-11-05 11:08:10 -04001690}
1691EXPORT_SYMBOL(drm_state_dump);
1692
1693#ifdef CONFIG_DEBUG_FS
1694static int drm_state_info(struct seq_file *m, void *data)
1695{
1696 struct drm_info_node *node = (struct drm_info_node *) m->private;
1697 struct drm_device *dev = node->minor->dev;
1698 struct drm_printer p = drm_seq_file_printer(m);
1699
Daniel Vetterc2d85562017-04-03 10:32:54 +02001700 __drm_state_dump(dev, &p, true);
Rob Clark6559c902016-11-05 11:08:10 -04001701
1702 return 0;
1703}
1704
1705/* any use in debugfs files to dump individual planes/crtc/etc? */
1706static const struct drm_info_list drm_atomic_debugfs_list[] = {
1707 {"state", drm_state_info, 0},
1708};
1709
Wambui Karuga7ce844712020-03-10 16:31:21 +03001710void drm_atomic_debugfs_init(struct drm_minor *minor)
Rob Clark6559c902016-11-05 11:08:10 -04001711{
Wambui Karugae196e142020-03-10 16:31:19 +03001712 drm_debugfs_create_files(drm_atomic_debugfs_list,
1713 ARRAY_SIZE(drm_atomic_debugfs_list),
1714 minor->debugfs_root, minor);
Rob Clark6559c902016-11-05 11:08:10 -04001715}
1716#endif