blob: 4e961b03f161ad2770a07a84ba55328b5b8b4a7b [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
29#include <drm/drmP.h>
30#include <drm/drm_atomic.h>
Daniel Vetter72fdb40c2018-09-05 15:57:11 +020031#include <drm/drm_atomic_uapi.h>
Lionel Landwerlin5488dc12016-02-26 17:05:00 +000032#include <drm/drm_mode.h>
Rob Clarkfceffb322016-11-05 11:08:09 -040033#include <drm/drm_print.h>
Brian Starkey935774c2017-03-29 17:42:32 +010034#include <drm/drm_writeback.h>
Gustavo Padovan96260142016-11-15 22:06:39 +090035#include <linux/sync_file.h>
Daniel Vettercc4ceb42014-07-25 21:30:38 +020036
Thierry Redingbe35f942016-04-28 15:19:56 +020037#include "drm_crtc_internal.h"
Noralf Trønnesf02b6042017-11-07 20:13:41 +010038#include "drm_internal.h"
Thierry Redingbe35f942016-04-28 15:19:56 +020039
Daniel Vetterb3ba3f62016-12-21 14:03:35 +010040void __drm_crtc_commit_free(struct kref *kref)
Daniel Vetter3b24f7d2016-06-08 14:19:00 +020041{
42 struct drm_crtc_commit *commit =
43 container_of(kref, struct drm_crtc_commit, ref);
44
45 kfree(commit);
46}
Daniel Vetterb3ba3f62016-12-21 14:03:35 +010047EXPORT_SYMBOL(__drm_crtc_commit_free);
Daniel Vetter3b24f7d2016-06-08 14:19:00 +020048
Maarten Lankhorst036ef572015-05-18 10:06:40 +020049/**
50 * drm_atomic_state_default_release -
51 * release memory initialized by drm_atomic_state_init
52 * @state: atomic state
53 *
54 * Free all the memory allocated by drm_atomic_state_init.
Daniel Vetterda6c0592017-12-14 21:30:53 +010055 * This should only be used by drivers which are still subclassing
56 * &drm_atomic_state and haven't switched to &drm_private_state yet.
Maarten Lankhorst036ef572015-05-18 10:06:40 +020057 */
58void drm_atomic_state_default_release(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +020059{
60 kfree(state->connectors);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020061 kfree(state->crtcs);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020062 kfree(state->planes);
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -070063 kfree(state->private_objs);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020064}
Maarten Lankhorst036ef572015-05-18 10:06:40 +020065EXPORT_SYMBOL(drm_atomic_state_default_release);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020066
67/**
Maarten Lankhorst036ef572015-05-18 10:06:40 +020068 * drm_atomic_state_init - init new atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +020069 * @dev: DRM device
Maarten Lankhorst036ef572015-05-18 10:06:40 +020070 * @state: atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +020071 *
Maarten Lankhorst036ef572015-05-18 10:06:40 +020072 * Default implementation for filling in a new atomic state.
Daniel Vetterda6c0592017-12-14 21:30:53 +010073 * This should only be used by drivers which are still subclassing
74 * &drm_atomic_state and haven't switched to &drm_private_state yet.
Daniel Vettercc4ceb42014-07-25 21:30:38 +020075 */
Maarten Lankhorst036ef572015-05-18 10:06:40 +020076int
77drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +020078{
Chris Wilson08536952016-10-14 13:18:18 +010079 kref_init(&state->ref);
80
Rob Clarkd34f20d2014-12-18 16:01:56 -050081 /* TODO legacy paths should maybe do a better job about
82 * setting this appropriately?
83 */
84 state->allow_modeset = true;
85
Daniel Vettercc4ceb42014-07-25 21:30:38 +020086 state->crtcs = kcalloc(dev->mode_config.num_crtc,
87 sizeof(*state->crtcs), GFP_KERNEL);
88 if (!state->crtcs)
89 goto fail;
Daniel Vettercc4ceb42014-07-25 21:30:38 +020090 state->planes = kcalloc(dev->mode_config.num_total_plane,
91 sizeof(*state->planes), GFP_KERNEL);
92 if (!state->planes)
93 goto fail;
Daniel Vettercc4ceb42014-07-25 21:30:38 +020094
95 state->dev = dev;
96
Maarten Lankhorst036ef572015-05-18 10:06:40 +020097 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020098
Maarten Lankhorst036ef572015-05-18 10:06:40 +020099 return 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200100fail:
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200101 drm_atomic_state_default_release(state);
102 return -ENOMEM;
103}
104EXPORT_SYMBOL(drm_atomic_state_init);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200105
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200106/**
107 * drm_atomic_state_alloc - allocate atomic state
108 * @dev: DRM device
109 *
110 * This allocates an empty atomic state to track updates.
111 */
112struct drm_atomic_state *
113drm_atomic_state_alloc(struct drm_device *dev)
114{
115 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200116
117 if (!config->funcs->atomic_state_alloc) {
Dawid Kurekac7c7482017-06-15 19:45:56 +0200118 struct drm_atomic_state *state;
119
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200120 state = kzalloc(sizeof(*state), GFP_KERNEL);
121 if (!state)
122 return NULL;
123 if (drm_atomic_state_init(dev, state) < 0) {
124 kfree(state);
125 return NULL;
126 }
127 return state;
128 }
129
130 return config->funcs->atomic_state_alloc(dev);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200131}
132EXPORT_SYMBOL(drm_atomic_state_alloc);
133
134/**
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200135 * drm_atomic_state_default_clear - clear base atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200136 * @state: atomic state
137 *
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200138 * Default implementation for clearing atomic state.
Daniel Vetterda6c0592017-12-14 21:30:53 +0100139 * This should only be used by drivers which are still subclassing
140 * &drm_atomic_state and haven't switched to &drm_private_state yet.
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200141 */
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200142void drm_atomic_state_default_clear(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200143{
144 struct drm_device *dev = state->dev;
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100145 struct drm_mode_config *config = &dev->mode_config;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200146 int i;
147
Daniel Vetter17a38d92015-02-22 12:24:16 +0100148 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200149
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100150 for (i = 0; i < state->num_connector; i++) {
Daniel Vetter63e83c12016-06-02 00:06:32 +0200151 struct drm_connector *connector = state->connectors[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200152
153 if (!connector)
154 continue;
155
Dave Airlied2307de2016-04-27 11:27:39 +1000156 connector->funcs->atomic_destroy_state(connector,
Daniel Vetter63e83c12016-06-02 00:06:32 +0200157 state->connectors[i].state);
158 state->connectors[i].ptr = NULL;
159 state->connectors[i].state = NULL;
Ville Syrjäläf0b408e2018-05-02 21:32:47 +0300160 state->connectors[i].old_state = NULL;
161 state->connectors[i].new_state = NULL;
Thierry Redingad093602017-02-28 15:46:39 +0100162 drm_connector_put(connector);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200163 }
164
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100165 for (i = 0; i < config->num_crtc; i++) {
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200166 struct drm_crtc *crtc = state->crtcs[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200167
168 if (!crtc)
169 continue;
170
171 crtc->funcs->atomic_destroy_state(crtc,
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200172 state->crtcs[i].state);
Daniel Vetter3b24f7d2016-06-08 14:19:00 +0200173
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200174 state->crtcs[i].ptr = NULL;
175 state->crtcs[i].state = NULL;
Ville Syrjäläf0b408e2018-05-02 21:32:47 +0300176 state->crtcs[i].old_state = NULL;
177 state->crtcs[i].new_state = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200178 }
179
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100180 for (i = 0; i < config->num_total_plane; i++) {
Daniel Vetterb8b53422016-06-02 00:06:33 +0200181 struct drm_plane *plane = state->planes[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200182
183 if (!plane)
184 continue;
185
186 plane->funcs->atomic_destroy_state(plane,
Daniel Vetterb8b53422016-06-02 00:06:33 +0200187 state->planes[i].state);
188 state->planes[i].ptr = NULL;
189 state->planes[i].state = NULL;
Ville Syrjäläf0b408e2018-05-02 21:32:47 +0300190 state->planes[i].old_state = NULL;
191 state->planes[i].new_state = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200192 }
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700193
194 for (i = 0; i < state->num_private_objs; i++) {
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300195 struct drm_private_obj *obj = state->private_objs[i].ptr;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700196
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300197 obj->funcs->atomic_destroy_state(obj,
198 state->private_objs[i].state);
199 state->private_objs[i].ptr = NULL;
200 state->private_objs[i].state = NULL;
Ville Syrjäläb5cb2e52018-05-02 21:32:47 +0300201 state->private_objs[i].old_state = NULL;
202 state->private_objs[i].new_state = NULL;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700203 }
204 state->num_private_objs = 0;
205
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +0200206 if (state->fake_commit) {
207 drm_crtc_commit_put(state->fake_commit);
208 state->fake_commit = NULL;
209 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200210}
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200211EXPORT_SYMBOL(drm_atomic_state_default_clear);
212
213/**
214 * drm_atomic_state_clear - clear state object
215 * @state: atomic state
216 *
217 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
218 * all locks. So someone else could sneak in and change the current modeset
219 * configuration. Which means that all the state assembled in @state is no
220 * longer an atomic update to the current state, but to some arbitrary earlier
Daniel Vetterd5745282017-01-25 07:26:45 +0100221 * state. Which could break assumptions the driver's
222 * &drm_mode_config_funcs.atomic_check likely relies on.
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200223 *
224 * Hence we must clear all cached state and completely start over, using this
225 * function.
226 */
227void drm_atomic_state_clear(struct drm_atomic_state *state)
228{
229 struct drm_device *dev = state->dev;
230 struct drm_mode_config *config = &dev->mode_config;
231
232 if (config->funcs->atomic_state_clear)
233 config->funcs->atomic_state_clear(state);
234 else
235 drm_atomic_state_default_clear(state);
236}
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200237EXPORT_SYMBOL(drm_atomic_state_clear);
238
239/**
Chris Wilson08536952016-10-14 13:18:18 +0100240 * __drm_atomic_state_free - free all memory for an atomic state
241 * @ref: This atomic state to deallocate
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200242 *
243 * This frees all memory associated with an atomic state, including all the
244 * per-object state for planes, crtcs and connectors.
245 */
Chris Wilson08536952016-10-14 13:18:18 +0100246void __drm_atomic_state_free(struct kref *ref)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200247{
Chris Wilson08536952016-10-14 13:18:18 +0100248 struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
249 struct drm_mode_config *config = &state->dev->mode_config;
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200250
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200251 drm_atomic_state_clear(state);
252
Daniel Vetter17a38d92015-02-22 12:24:16 +0100253 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200254
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200255 if (config->funcs->atomic_state_free) {
256 config->funcs->atomic_state_free(state);
257 } else {
258 drm_atomic_state_default_release(state);
259 kfree(state);
260 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200261}
Chris Wilson08536952016-10-14 13:18:18 +0100262EXPORT_SYMBOL(__drm_atomic_state_free);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200263
264/**
265 * drm_atomic_get_crtc_state - get crtc state
266 * @state: global atomic state object
267 * @crtc: crtc to get state object for
268 *
269 * This function returns the crtc state for the given crtc, allocating it if
270 * needed. It will also grab the relevant crtc lock to make sure that the state
271 * is consistent.
272 *
273 * Returns:
274 *
275 * Either the allocated state or the error code encoded into the pointer. When
276 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
277 * entire atomic sequence must be restarted. All other errors are fatal.
278 */
279struct drm_crtc_state *
280drm_atomic_get_crtc_state(struct drm_atomic_state *state,
281 struct drm_crtc *crtc)
282{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200283 int ret, index = drm_crtc_index(crtc);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200284 struct drm_crtc_state *crtc_state;
285
Maarten Lankhorst7f4eaa892016-05-03 11:12:31 +0200286 WARN_ON(!state->acquire_ctx);
287
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200288 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
289 if (crtc_state)
290 return crtc_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200291
292 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
293 if (ret)
294 return ERR_PTR(ret);
295
296 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
297 if (!crtc_state)
298 return ERR_PTR(-ENOMEM);
299
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200300 state->crtcs[index].state = crtc_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +0100301 state->crtcs[index].old_state = crtc->state;
302 state->crtcs[index].new_state = crtc_state;
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200303 state->crtcs[index].ptr = crtc;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200304 crtc_state->state = state;
305
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200306 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
307 crtc->base.id, crtc->name, crtc_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200308
309 return crtc_state;
310}
311EXPORT_SYMBOL(drm_atomic_get_crtc_state);
312
Ville Syrjäläb2432ad2018-11-01 20:46:45 +0200313static int drm_atomic_crtc_check(const struct drm_crtc_state *old_crtc_state,
314 const struct drm_crtc_state *new_crtc_state)
Rob Clark5e743732014-12-18 16:01:51 -0500315{
Ville Syrjäläb2432ad2018-11-01 20:46:45 +0200316 struct drm_crtc *crtc = new_crtc_state->crtc;
317
Rob Clark5e743732014-12-18 16:01:51 -0500318 /* NOTE: we explicitly don't enforce constraints such as primary
319 * layer covering entire screen, since that is something we want
320 * to allow (on hw that supports it). For hw that does not, it
321 * should be checked in driver's crtc->atomic_check() vfunc.
322 *
323 * TODO: Add generic modeset state checks once we support those.
324 */
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100325
Ville Syrjäläb2432ad2018-11-01 20:46:45 +0200326 if (new_crtc_state->active && !new_crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200327 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
328 crtc->base.id, crtc->name);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100329 return -EINVAL;
330 }
331
Daniel Stone99cf4a22015-05-25 19:11:51 +0100332 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
333 * as this is a kernel-internal detail that userspace should never
334 * be able to trigger. */
335 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
Ville Syrjäläb2432ad2018-11-01 20:46:45 +0200336 WARN_ON(new_crtc_state->enable && !new_crtc_state->mode_blob)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200337 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
338 crtc->base.id, crtc->name);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100339 return -EINVAL;
340 }
341
342 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
Ville Syrjäläb2432ad2018-11-01 20:46:45 +0200343 WARN_ON(!new_crtc_state->enable && new_crtc_state->mode_blob)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200344 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
345 crtc->base.id, crtc->name);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100346 return -EINVAL;
347 }
348
Daniel Vetter4cba6852015-12-08 09:49:20 +0100349 /*
350 * Reject event generation for when a CRTC is off and stays off.
351 * It wouldn't be hard to implement this, but userspace has a track
352 * record of happily burning through 100% cpu (or worse, crash) when the
353 * display pipe is suspended. To avoid all that fun just reject updates
354 * that ask for events since likely that indicates a bug in the
355 * compositor's drawing loop. This is consistent with the vblank IOCTL
356 * and legacy page_flip IOCTL which also reject service on a disabled
357 * pipe.
358 */
Ville Syrjäläb2432ad2018-11-01 20:46:45 +0200359 if (new_crtc_state->event &&
360 !new_crtc_state->active && !old_crtc_state->active) {
Russell King6ac7c542017-02-13 12:27:03 +0000361 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requesting event but off\n",
362 crtc->base.id, crtc->name);
Daniel Vetter4cba6852015-12-08 09:49:20 +0100363 return -EINVAL;
364 }
365
Rob Clark5e743732014-12-18 16:01:51 -0500366 return 0;
367}
368
Rob Clarkfceffb322016-11-05 11:08:09 -0400369static void drm_atomic_crtc_print_state(struct drm_printer *p,
370 const struct drm_crtc_state *state)
371{
372 struct drm_crtc *crtc = state->crtc;
373
374 drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
375 drm_printf(p, "\tenable=%d\n", state->enable);
376 drm_printf(p, "\tactive=%d\n", state->active);
377 drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
378 drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
379 drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
380 drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
381 drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
382 drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
383 drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
384 drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
385 drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
386
387 if (crtc->funcs->atomic_print_state)
388 crtc->funcs->atomic_print_state(p, state);
389}
390
Brian Starkey935774c2017-03-29 17:42:32 +0100391static int drm_atomic_connector_check(struct drm_connector *connector,
392 struct drm_connector_state *state)
393{
394 struct drm_crtc_state *crtc_state;
395 struct drm_writeback_job *writeback_job = state->writeback_job;
396
397 if ((connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK) || !writeback_job)
398 return 0;
399
400 if (writeback_job->fb && !state->crtc) {
401 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] framebuffer without CRTC\n",
402 connector->base.id, connector->name);
403 return -EINVAL;
404 }
405
406 if (state->crtc)
407 crtc_state = drm_atomic_get_existing_crtc_state(state->state,
408 state->crtc);
409
410 if (writeback_job->fb && !crtc_state->active) {
411 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] has framebuffer, but [CRTC:%d] is off\n",
412 connector->base.id, connector->name,
413 state->crtc->base.id);
414 return -EINVAL;
415 }
416
Brian Starkeyb13cc8d2017-03-29 17:42:33 +0100417 if (writeback_job->out_fence && !writeback_job->fb) {
418 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] requesting out-fence without framebuffer\n",
419 connector->base.id, connector->name);
420 return -EINVAL;
421 }
422
Brian Starkey935774c2017-03-29 17:42:32 +0100423 return 0;
424}
425
426/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200427 * drm_atomic_get_plane_state - get plane state
428 * @state: global atomic state object
429 * @plane: plane to get state object for
430 *
431 * This function returns the plane state for the given plane, allocating it if
432 * needed. It will also grab the relevant plane lock to make sure that the state
433 * is consistent.
434 *
435 * Returns:
436 *
437 * Either the allocated state or the error code encoded into the pointer. When
438 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
439 * entire atomic sequence must be restarted. All other errors are fatal.
440 */
441struct drm_plane_state *
442drm_atomic_get_plane_state(struct drm_atomic_state *state,
443 struct drm_plane *plane)
444{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200445 int ret, index = drm_plane_index(plane);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200446 struct drm_plane_state *plane_state;
447
Maarten Lankhorst7f4eaa892016-05-03 11:12:31 +0200448 WARN_ON(!state->acquire_ctx);
449
Ville Syrjäläe00fb852018-05-25 21:50:45 +0300450 /* the legacy pointers should never be set */
451 WARN_ON(plane->fb);
452 WARN_ON(plane->old_fb);
453 WARN_ON(plane->crtc);
454
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200455 plane_state = drm_atomic_get_existing_plane_state(state, plane);
456 if (plane_state)
457 return plane_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200458
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100459 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200460 if (ret)
461 return ERR_PTR(ret);
462
463 plane_state = plane->funcs->atomic_duplicate_state(plane);
464 if (!plane_state)
465 return ERR_PTR(-ENOMEM);
466
Daniel Vetterb8b53422016-06-02 00:06:33 +0200467 state->planes[index].state = plane_state;
468 state->planes[index].ptr = plane;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +0100469 state->planes[index].old_state = plane->state;
470 state->planes[index].new_state = plane_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200471 plane_state->state = state;
472
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200473 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
474 plane->base.id, plane->name, plane_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200475
476 if (plane_state->crtc) {
477 struct drm_crtc_state *crtc_state;
478
479 crtc_state = drm_atomic_get_crtc_state(state,
480 plane_state->crtc);
481 if (IS_ERR(crtc_state))
482 return ERR_CAST(crtc_state);
483 }
484
485 return plane_state;
486}
487EXPORT_SYMBOL(drm_atomic_get_plane_state);
488
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200489static bool
490plane_switching_crtc(struct drm_atomic_state *state,
491 struct drm_plane *plane,
492 struct drm_plane_state *plane_state)
493{
494 if (!plane->state->crtc || !plane_state->crtc)
495 return false;
496
497 if (plane->state->crtc == plane_state->crtc)
498 return false;
499
500 /* This could be refined, but currently there's no helper or driver code
501 * to implement direct switching of active planes nor userspace to take
502 * advantage of more direct plane switching without the intermediate
503 * full OFF state.
504 */
505 return true;
506}
507
Rob Clarkac9c9252014-12-18 16:01:47 -0500508/**
Rob Clark5e743732014-12-18 16:01:51 -0500509 * drm_atomic_plane_check - check plane state
510 * @plane: plane to check
511 * @state: plane state to check
512 *
513 * Provides core sanity checks for plane state.
514 *
515 * RETURNS:
516 * Zero on success, error code on failure
517 */
518static int drm_atomic_plane_check(struct drm_plane *plane,
519 struct drm_plane_state *state)
520{
521 unsigned int fb_width, fb_height;
Laurent Pinchartead86102015-03-05 02:25:43 +0200522 int ret;
Rob Clark5e743732014-12-18 16:01:51 -0500523
524 /* either *both* CRTC and FB must be set, or neither */
Maarten Lankhorstfa5aaee2018-01-30 11:27:04 +0100525 if (state->crtc && !state->fb) {
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300526 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] CRTC set but no FB\n",
527 plane->base.id, plane->name);
Rob Clark5e743732014-12-18 16:01:51 -0500528 return -EINVAL;
Maarten Lankhorstfa5aaee2018-01-30 11:27:04 +0100529 } else if (state->fb && !state->crtc) {
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300530 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] FB set but no CRTC\n",
531 plane->base.id, plane->name);
Rob Clark5e743732014-12-18 16:01:51 -0500532 return -EINVAL;
533 }
534
535 /* if disabled, we don't care about the rest of the state: */
536 if (!state->crtc)
537 return 0;
538
539 /* Check whether this plane is usable on this CRTC */
540 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300541 DRM_DEBUG_ATOMIC("Invalid [CRTC:%d:%s] for [PLANE:%d:%s]\n",
542 state->crtc->base.id, state->crtc->name,
543 plane->base.id, plane->name);
Rob Clark5e743732014-12-18 16:01:51 -0500544 return -EINVAL;
545 }
546
547 /* Check whether this plane supports the fb pixel format. */
Ville Syrjälä23163a72017-12-22 21:22:30 +0200548 ret = drm_plane_check_pixel_format(plane, state->fb->format->format,
549 state->fb->modifier);
Laurent Pinchartead86102015-03-05 02:25:43 +0200550 if (ret) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +0000551 struct drm_format_name_buf format_name;
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300552 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid pixel format %s, modifier 0x%llx\n",
553 plane->base.id, plane->name,
Ville Syrjälä23163a72017-12-22 21:22:30 +0200554 drm_get_format_name(state->fb->format->format,
555 &format_name),
556 state->fb->modifier);
Laurent Pinchartead86102015-03-05 02:25:43 +0200557 return ret;
Rob Clark5e743732014-12-18 16:01:51 -0500558 }
559
560 /* Give drivers some help against integer overflows */
561 if (state->crtc_w > INT_MAX ||
562 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
563 state->crtc_h > INT_MAX ||
564 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300565 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid CRTC coordinates %ux%u+%d+%d\n",
566 plane->base.id, plane->name,
Daniel Vetter17a38d92015-02-22 12:24:16 +0100567 state->crtc_w, state->crtc_h,
568 state->crtc_x, state->crtc_y);
Rob Clark5e743732014-12-18 16:01:51 -0500569 return -ERANGE;
570 }
571
572 fb_width = state->fb->width << 16;
573 fb_height = state->fb->height << 16;
574
575 /* Make sure source coordinates are inside the fb. */
576 if (state->src_w > fb_width ||
577 state->src_x > fb_width - state->src_w ||
578 state->src_h > fb_height ||
579 state->src_y > fb_height - state->src_h) {
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300580 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid source coordinates "
Ville Syrjälä0338f0d2017-11-01 20:35:33 +0200581 "%u.%06ux%u.%06u+%u.%06u+%u.%06u (fb %ux%u)\n",
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300582 plane->base.id, plane->name,
Daniel Vetter17a38d92015-02-22 12:24:16 +0100583 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
584 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
585 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
Ville Syrjälä0338f0d2017-11-01 20:35:33 +0200586 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10,
587 state->fb->width, state->fb->height);
Rob Clark5e743732014-12-18 16:01:51 -0500588 return -ENOSPC;
589 }
590
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200591 if (plane_switching_crtc(state->state, plane, state)) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200592 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
593 plane->base.id, plane->name);
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200594 return -EINVAL;
595 }
596
Rob Clark5e743732014-12-18 16:01:51 -0500597 return 0;
598}
599
Rob Clarkfceffb322016-11-05 11:08:09 -0400600static void drm_atomic_plane_print_state(struct drm_printer *p,
601 const struct drm_plane_state *state)
602{
603 struct drm_plane *plane = state->plane;
604 struct drm_rect src = drm_plane_state_src(state);
605 struct drm_rect dest = drm_plane_state_dest(state);
606
607 drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
608 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
609 drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
Noralf Trønnesf02b6042017-11-07 20:13:41 +0100610 if (state->fb)
611 drm_framebuffer_print_info(p, 2, state->fb);
Rob Clarkfceffb322016-11-05 11:08:09 -0400612 drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
613 drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
614 drm_printf(p, "\trotation=%x\n", state->rotation);
Benjamin Gaignardf8878bb2018-06-05 15:54:01 +0200615 drm_printf(p, "\tnormalized-zpos=%x\n", state->normalized_zpos);
Ville Syrjälä56dbbaf2018-02-19 22:28:46 +0200616 drm_printf(p, "\tcolor-encoding=%s\n",
617 drm_get_color_encoding_name(state->color_encoding));
618 drm_printf(p, "\tcolor-range=%s\n",
619 drm_get_color_range_name(state->color_range));
Rob Clarkfceffb322016-11-05 11:08:09 -0400620
621 if (plane->funcs->atomic_print_state)
622 plane->funcs->atomic_print_state(p, state);
623}
624
Rob Clark5e743732014-12-18 16:01:51 -0500625/**
Daniel Vetterda6c0592017-12-14 21:30:53 +0100626 * DOC: handling driver private state
627 *
628 * Very often the DRM objects exposed to userspace in the atomic modeset api
629 * (&drm_connector, &drm_crtc and &drm_plane) do not map neatly to the
630 * underlying hardware. Especially for any kind of shared resources (e.g. shared
631 * clocks, scaler units, bandwidth and fifo limits shared among a group of
632 * planes or CRTCs, and so on) it makes sense to model these as independent
633 * objects. Drivers then need to do similar state tracking and commit ordering for
634 * such private (since not exposed to userpace) objects as the atomic core and
635 * helpers already provide for connectors, planes and CRTCs.
636 *
637 * To make this easier on drivers the atomic core provides some support to track
638 * driver private state objects using struct &drm_private_obj, with the
639 * associated state struct &drm_private_state.
640 *
641 * Similar to userspace-exposed objects, private state structures can be
642 * acquired by calling drm_atomic_get_private_obj_state(). Since this function
643 * does not take care of locking, drivers should wrap it for each type of
644 * private state object they have with the required call to drm_modeset_lock()
645 * for the corresponding &drm_modeset_lock.
646 *
647 * All private state structures contained in a &drm_atomic_state update can be
648 * iterated using for_each_oldnew_private_obj_in_state(),
649 * for_each_new_private_obj_in_state() and for_each_old_private_obj_in_state().
650 * Drivers are recommended to wrap these for each type of driver private state
651 * object they have, filtering on &drm_private_obj.funcs using for_each_if(), at
652 * least if they want to iterate over all objects of a given type.
653 *
654 * An earlier way to handle driver private state was by subclassing struct
655 * &drm_atomic_state. But since that encourages non-standard ways to implement
656 * the check/commit split atomic requires (by using e.g. "check and rollback or
657 * commit instead" of "duplicate state, check, then either commit or release
658 * duplicated state) it is deprecated in favour of using &drm_private_state.
659 */
660
661/**
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300662 * drm_atomic_private_obj_init - initialize private object
663 * @obj: private object
664 * @state: initial private object state
665 * @funcs: pointer to the struct of function pointers that identify the object
666 * type
667 *
668 * Initialize the private object, which can be embedded into any
669 * driver private object that needs its own atomic state.
670 */
671void
672drm_atomic_private_obj_init(struct drm_private_obj *obj,
673 struct drm_private_state *state,
674 const struct drm_private_state_funcs *funcs)
675{
676 memset(obj, 0, sizeof(*obj));
677
678 obj->state = state;
679 obj->funcs = funcs;
680}
681EXPORT_SYMBOL(drm_atomic_private_obj_init);
682
683/**
684 * drm_atomic_private_obj_fini - finalize private object
685 * @obj: private object
686 *
687 * Finalize the private object.
688 */
689void
690drm_atomic_private_obj_fini(struct drm_private_obj *obj)
691{
692 obj->funcs->atomic_destroy_state(obj, obj->state);
693}
694EXPORT_SYMBOL(drm_atomic_private_obj_fini);
695
696/**
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700697 * drm_atomic_get_private_obj_state - get private object state
698 * @state: global atomic state
699 * @obj: private object to get the state for
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700700 *
701 * This function returns the private object state for the given private object,
702 * allocating the state if needed. It does not grab any locks as the caller is
703 * expected to care of any required locking.
704 *
705 * RETURNS:
706 *
707 * Either the allocated state or the error code encoded into a pointer.
708 */
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300709struct drm_private_state *
710drm_atomic_get_private_obj_state(struct drm_atomic_state *state,
711 struct drm_private_obj *obj)
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700712{
713 int index, num_objs, i;
714 size_t size;
715 struct __drm_private_objs_state *arr;
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300716 struct drm_private_state *obj_state;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700717
718 for (i = 0; i < state->num_private_objs; i++)
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300719 if (obj == state->private_objs[i].ptr)
720 return state->private_objs[i].state;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700721
722 num_objs = state->num_private_objs + 1;
723 size = sizeof(*state->private_objs) * num_objs;
724 arr = krealloc(state->private_objs, size, GFP_KERNEL);
725 if (!arr)
726 return ERR_PTR(-ENOMEM);
727
728 state->private_objs = arr;
729 index = state->num_private_objs;
730 memset(&state->private_objs[index], 0, sizeof(*state->private_objs));
731
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300732 obj_state = obj->funcs->atomic_duplicate_state(obj);
733 if (!obj_state)
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700734 return ERR_PTR(-ENOMEM);
735
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300736 state->private_objs[index].state = obj_state;
737 state->private_objs[index].old_state = obj->state;
738 state->private_objs[index].new_state = obj_state;
739 state->private_objs[index].ptr = obj;
Alexandru Gheorghee89ea352018-05-30 18:30:52 +0100740 obj_state->state = state;
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300741
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700742 state->num_private_objs = num_objs;
743
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300744 DRM_DEBUG_ATOMIC("Added new private object %p state %p to %p\n",
745 obj, obj_state, state);
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700746
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300747 return obj_state;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700748}
749EXPORT_SYMBOL(drm_atomic_get_private_obj_state);
750
751/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200752 * drm_atomic_get_connector_state - get connector state
753 * @state: global atomic state object
754 * @connector: connector to get state object for
755 *
756 * This function returns the connector state for the given connector,
757 * allocating it if needed. It will also grab the relevant connector lock to
758 * make sure that the state is consistent.
759 *
760 * Returns:
761 *
762 * Either the allocated state or the error code encoded into the pointer. When
763 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
764 * entire atomic sequence must be restarted. All other errors are fatal.
765 */
766struct drm_connector_state *
767drm_atomic_get_connector_state(struct drm_atomic_state *state,
768 struct drm_connector *connector)
769{
770 int ret, index;
771 struct drm_mode_config *config = &connector->dev->mode_config;
772 struct drm_connector_state *connector_state;
773
Maarten Lankhorst7f4eaa892016-05-03 11:12:31 +0200774 WARN_ON(!state->acquire_ctx);
775
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100776 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
777 if (ret)
778 return ERR_PTR(ret);
779
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200780 index = drm_connector_index(connector);
781
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100782 if (index >= state->num_connector) {
Daniel Vetter63e83c12016-06-02 00:06:32 +0200783 struct __drm_connnectors_state *c;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100784 int alloc = max(index + 1, config->num_connector);
785
786 c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
787 if (!c)
788 return ERR_PTR(-ENOMEM);
789
790 state->connectors = c;
791 memset(&state->connectors[state->num_connector], 0,
792 sizeof(*state->connectors) * (alloc - state->num_connector));
793
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100794 state->num_connector = alloc;
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100795 }
796
Daniel Vetter63e83c12016-06-02 00:06:32 +0200797 if (state->connectors[index].state)
798 return state->connectors[index].state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200799
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200800 connector_state = connector->funcs->atomic_duplicate_state(connector);
801 if (!connector_state)
802 return ERR_PTR(-ENOMEM);
803
Thierry Redingad093602017-02-28 15:46:39 +0100804 drm_connector_get(connector);
Daniel Vetter63e83c12016-06-02 00:06:32 +0200805 state->connectors[index].state = connector_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +0100806 state->connectors[index].old_state = connector->state;
807 state->connectors[index].new_state = connector_state;
Daniel Vetter63e83c12016-06-02 00:06:32 +0200808 state->connectors[index].ptr = connector;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200809 connector_state->state = state;
810
Russell King6ac7c542017-02-13 12:27:03 +0000811 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d:%s] %p state to %p\n",
812 connector->base.id, connector->name,
813 connector_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200814
815 if (connector_state->crtc) {
816 struct drm_crtc_state *crtc_state;
817
818 crtc_state = drm_atomic_get_crtc_state(state,
819 connector_state->crtc);
820 if (IS_ERR(crtc_state))
821 return ERR_CAST(crtc_state);
822 }
823
824 return connector_state;
825}
826EXPORT_SYMBOL(drm_atomic_get_connector_state);
827
Rob Clarkfceffb322016-11-05 11:08:09 -0400828static void drm_atomic_connector_print_state(struct drm_printer *p,
829 const struct drm_connector_state *state)
830{
831 struct drm_connector *connector = state->connector;
832
833 drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
834 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
835
Brian Starkey8cbc5ca2017-11-02 16:49:51 +0000836 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
837 if (state->writeback_job && state->writeback_job->fb)
838 drm_printf(p, "\tfb=%d\n", state->writeback_job->fb->base.id);
839
Rob Clarkfceffb322016-11-05 11:08:09 -0400840 if (connector->funcs->atomic_print_state)
841 connector->funcs->atomic_print_state(p, state);
842}
843
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200844/**
845 * drm_atomic_add_affected_connectors - add connectors for crtc
846 * @state: atomic state
847 * @crtc: DRM crtc
848 *
849 * This function walks the current configuration and adds all connectors
850 * currently using @crtc to the atomic configuration @state. Note that this
851 * function must acquire the connection mutex. This can potentially cause
852 * unneeded seralization if the update is just for the planes on one crtc. Hence
853 * drivers and helpers should only call this when really needed (e.g. when a
854 * full modeset needs to happen due to some change).
855 *
856 * Returns:
857 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
858 * then the w/w mutex code has detected a deadlock and the entire atomic
859 * sequence must be restarted. All other errors are fatal.
860 */
861int
862drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
863 struct drm_crtc *crtc)
864{
865 struct drm_mode_config *config = &state->dev->mode_config;
866 struct drm_connector *connector;
867 struct drm_connector_state *conn_state;
Daniel Vetter613051d2016-12-14 00:08:06 +0100868 struct drm_connector_list_iter conn_iter;
Maarten Lankhorst5351bbd2017-01-16 10:37:39 +0100869 struct drm_crtc_state *crtc_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200870 int ret;
871
Maarten Lankhorst5351bbd2017-01-16 10:37:39 +0100872 crtc_state = drm_atomic_get_crtc_state(state, crtc);
873 if (IS_ERR(crtc_state))
874 return PTR_ERR(crtc_state);
875
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200876 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
877 if (ret)
878 return ret;
879
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200880 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
881 crtc->base.id, crtc->name, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200882
883 /*
Maarten Lankhorst5351bbd2017-01-16 10:37:39 +0100884 * Changed connectors are already in @state, so only need to look
885 * at the connector_mask in crtc_state.
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200886 */
Thierry Redingb982dab2017-02-28 15:46:43 +0100887 drm_connector_list_iter_begin(state->dev, &conn_iter);
Daniel Vetter613051d2016-12-14 00:08:06 +0100888 drm_for_each_connector_iter(connector, &conn_iter) {
Ville Syrjälä73705732018-06-26 22:47:10 +0300889 if (!(crtc_state->connector_mask & drm_connector_mask(connector)))
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200890 continue;
891
892 conn_state = drm_atomic_get_connector_state(state, connector);
Daniel Vetter613051d2016-12-14 00:08:06 +0100893 if (IS_ERR(conn_state)) {
Thierry Redingb982dab2017-02-28 15:46:43 +0100894 drm_connector_list_iter_end(&conn_iter);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200895 return PTR_ERR(conn_state);
Daniel Vetter613051d2016-12-14 00:08:06 +0100896 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200897 }
Thierry Redingb982dab2017-02-28 15:46:43 +0100898 drm_connector_list_iter_end(&conn_iter);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200899
900 return 0;
901}
902EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
903
904/**
Maarten Lankhorste01e9f72015-05-19 16:41:02 +0200905 * drm_atomic_add_affected_planes - add planes for crtc
906 * @state: atomic state
907 * @crtc: DRM crtc
908 *
909 * This function walks the current configuration and adds all planes
910 * currently used by @crtc to the atomic configuration @state. This is useful
911 * when an atomic commit also needs to check all currently enabled plane on
912 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
913 * to avoid special code to force-enable all planes.
914 *
915 * Since acquiring a plane state will always also acquire the w/w mutex of the
916 * current CRTC for that plane (if there is any) adding all the plane states for
917 * a CRTC will not reduce parallism of atomic updates.
918 *
919 * Returns:
920 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
921 * then the w/w mutex code has detected a deadlock and the entire atomic
922 * sequence must be restarted. All other errors are fatal.
923 */
924int
925drm_atomic_add_affected_planes(struct drm_atomic_state *state,
926 struct drm_crtc *crtc)
927{
Ville Syrjälä534903d2018-11-01 20:46:44 +0200928 const struct drm_crtc_state *old_crtc_state =
929 drm_atomic_get_old_crtc_state(state, crtc);
Maarten Lankhorste01e9f72015-05-19 16:41:02 +0200930 struct drm_plane *plane;
931
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100932 WARN_ON(!drm_atomic_get_new_crtc_state(state, crtc));
Maarten Lankhorste01e9f72015-05-19 16:41:02 +0200933
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300934 DRM_DEBUG_ATOMIC("Adding all current planes for [CRTC:%d:%s] to %p\n",
935 crtc->base.id, crtc->name, state);
936
Ville Syrjälä534903d2018-11-01 20:46:44 +0200937 drm_for_each_plane_mask(plane, state->dev, old_crtc_state->plane_mask) {
Maarten Lankhorste01e9f72015-05-19 16:41:02 +0200938 struct drm_plane_state *plane_state =
939 drm_atomic_get_plane_state(state, plane);
940
941 if (IS_ERR(plane_state))
942 return PTR_ERR(plane_state);
943 }
944 return 0;
945}
946EXPORT_SYMBOL(drm_atomic_add_affected_planes);
947
948/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200949 * drm_atomic_check_only - check whether a given config would work
950 * @state: atomic configuration to check
951 *
952 * Note that this function can return -EDEADLK if the driver needed to acquire
953 * more locks but encountered a deadlock. The caller must then do the usual w/w
954 * backoff dance and restart. All other errors are fatal.
955 *
956 * Returns:
957 * 0 on success, negative error code on failure.
958 */
959int drm_atomic_check_only(struct drm_atomic_state *state)
960{
Rob Clark5e743732014-12-18 16:01:51 -0500961 struct drm_device *dev = state->dev;
962 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300963 struct drm_plane *plane;
964 struct drm_plane_state *plane_state;
965 struct drm_crtc *crtc;
Ville Syrjäläb2432ad2018-11-01 20:46:45 +0200966 struct drm_crtc_state *old_crtc_state;
967 struct drm_crtc_state *new_crtc_state;
Brian Starkey935774c2017-03-29 17:42:32 +0100968 struct drm_connector *conn;
969 struct drm_connector_state *conn_state;
Rob Clark5e743732014-12-18 16:01:51 -0500970 int i, ret = 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200971
Daniel Vetter17a38d92015-02-22 12:24:16 +0100972 DRM_DEBUG_ATOMIC("checking %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200973
Maarten Lankhorst5721a382017-01-16 10:37:40 +0100974 for_each_new_plane_in_state(state, plane, plane_state, i) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300975 ret = drm_atomic_plane_check(plane, plane_state);
Rob Clark5e743732014-12-18 16:01:51 -0500976 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200977 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
978 plane->base.id, plane->name);
Rob Clark5e743732014-12-18 16:01:51 -0500979 return ret;
980 }
981 }
982
Ville Syrjäläb2432ad2018-11-01 20:46:45 +0200983 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
984 ret = drm_atomic_crtc_check(old_crtc_state, new_crtc_state);
Rob Clark5e743732014-12-18 16:01:51 -0500985 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200986 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
987 crtc->base.id, crtc->name);
Rob Clark5e743732014-12-18 16:01:51 -0500988 return ret;
989 }
990 }
991
Brian Starkey935774c2017-03-29 17:42:32 +0100992 for_each_new_connector_in_state(state, conn, conn_state, i) {
993 ret = drm_atomic_connector_check(conn, conn_state);
994 if (ret) {
995 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] atomic core check failed\n",
996 conn->base.id, conn->name);
997 return ret;
998 }
999 }
1000
Lyude Paul14d4e522018-04-11 19:42:40 -04001001 if (config->funcs->atomic_check) {
Rob Clark5e743732014-12-18 16:01:51 -05001002 ret = config->funcs->atomic_check(state->dev, state);
1003
Lyude Paul14d4e522018-04-11 19:42:40 -04001004 if (ret) {
1005 DRM_DEBUG_ATOMIC("atomic driver check for %p failed: %d\n",
1006 state, ret);
1007 return ret;
1008 }
1009 }
Maarten Lankhorsta0ffc512017-08-15 11:57:06 +02001010
Rob Clarkd34f20d2014-12-18 16:01:56 -05001011 if (!state->allow_modeset) {
Ville Syrjäläb2432ad2018-11-01 20:46:45 +02001012 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1013 if (drm_atomic_crtc_needs_modeset(new_crtc_state)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001014 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1015 crtc->base.id, crtc->name);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001016 return -EINVAL;
1017 }
1018 }
1019 }
1020
Maarten Lankhorsta0ffc512017-08-15 11:57:06 +02001021 return 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001022}
1023EXPORT_SYMBOL(drm_atomic_check_only);
1024
1025/**
1026 * drm_atomic_commit - commit configuration atomically
1027 * @state: atomic configuration to check
1028 *
1029 * Note that this function can return -EDEADLK if the driver needed to acquire
1030 * more locks but encountered a deadlock. The caller must then do the usual w/w
1031 * backoff dance and restart. All other errors are fatal.
1032 *
Maarten Lankhorst76fede22017-01-04 12:34:00 +01001033 * This function will take its own reference on @state.
1034 * Callers should always release their reference with drm_atomic_state_put().
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001035 *
1036 * Returns:
1037 * 0 on success, negative error code on failure.
1038 */
1039int drm_atomic_commit(struct drm_atomic_state *state)
1040{
1041 struct drm_mode_config *config = &state->dev->mode_config;
1042 int ret;
1043
1044 ret = drm_atomic_check_only(state);
1045 if (ret)
1046 return ret;
1047
Colin Ian Kinga0752d42017-04-12 17:27:22 +01001048 DRM_DEBUG_ATOMIC("committing %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001049
1050 return config->funcs->atomic_commit(state->dev, state, false);
1051}
1052EXPORT_SYMBOL(drm_atomic_commit);
1053
1054/**
Daniel Vetterd5745282017-01-25 07:26:45 +01001055 * drm_atomic_nonblocking_commit - atomic nonblocking commit
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001056 * @state: atomic configuration to check
1057 *
1058 * Note that this function can return -EDEADLK if the driver needed to acquire
1059 * more locks but encountered a deadlock. The caller must then do the usual w/w
1060 * backoff dance and restart. All other errors are fatal.
1061 *
Maarten Lankhorst76fede22017-01-04 12:34:00 +01001062 * This function will take its own reference on @state.
1063 * Callers should always release their reference with drm_atomic_state_put().
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001064 *
1065 * Returns:
1066 * 0 on success, negative error code on failure.
1067 */
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02001068int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001069{
1070 struct drm_mode_config *config = &state->dev->mode_config;
1071 int ret;
1072
1073 ret = drm_atomic_check_only(state);
1074 if (ret)
1075 return ret;
1076
Colin Ian Kinga0752d42017-04-12 17:27:22 +01001077 DRM_DEBUG_ATOMIC("committing %p nonblocking\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001078
1079 return config->funcs->atomic_commit(state->dev, state, true);
1080}
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02001081EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001082
Daniel Vetter72fdb40c2018-09-05 15:57:11 +02001083void drm_atomic_print_state(const struct drm_atomic_state *state)
Rob Clarkfceffb322016-11-05 11:08:09 -04001084{
1085 struct drm_printer p = drm_info_printer(state->dev->dev);
1086 struct drm_plane *plane;
1087 struct drm_plane_state *plane_state;
1088 struct drm_crtc *crtc;
1089 struct drm_crtc_state *crtc_state;
1090 struct drm_connector *connector;
1091 struct drm_connector_state *connector_state;
1092 int i;
1093
1094 DRM_DEBUG_ATOMIC("checking %p\n", state);
1095
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001096 for_each_new_plane_in_state(state, plane, plane_state, i)
Rob Clarkfceffb322016-11-05 11:08:09 -04001097 drm_atomic_plane_print_state(&p, plane_state);
1098
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001099 for_each_new_crtc_in_state(state, crtc, crtc_state, i)
Rob Clarkfceffb322016-11-05 11:08:09 -04001100 drm_atomic_crtc_print_state(&p, crtc_state);
1101
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001102 for_each_new_connector_in_state(state, connector, connector_state, i)
Rob Clarkfceffb322016-11-05 11:08:09 -04001103 drm_atomic_connector_print_state(&p, connector_state);
1104}
1105
Daniel Vetterc2d85562017-04-03 10:32:54 +02001106static void __drm_state_dump(struct drm_device *dev, struct drm_printer *p,
1107 bool take_locks)
1108{
1109 struct drm_mode_config *config = &dev->mode_config;
1110 struct drm_plane *plane;
1111 struct drm_crtc *crtc;
1112 struct drm_connector *connector;
1113 struct drm_connector_list_iter conn_iter;
1114
Lyude Paul3c499ea2018-09-17 13:37:33 -04001115 if (!drm_drv_uses_atomic_modeset(dev))
Daniel Vetterc2d85562017-04-03 10:32:54 +02001116 return;
1117
1118 list_for_each_entry(plane, &config->plane_list, head) {
1119 if (take_locks)
1120 drm_modeset_lock(&plane->mutex, NULL);
1121 drm_atomic_plane_print_state(p, plane->state);
1122 if (take_locks)
1123 drm_modeset_unlock(&plane->mutex);
1124 }
1125
1126 list_for_each_entry(crtc, &config->crtc_list, head) {
1127 if (take_locks)
1128 drm_modeset_lock(&crtc->mutex, NULL);
1129 drm_atomic_crtc_print_state(p, crtc->state);
1130 if (take_locks)
1131 drm_modeset_unlock(&crtc->mutex);
1132 }
1133
1134 drm_connector_list_iter_begin(dev, &conn_iter);
1135 if (take_locks)
1136 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1137 drm_for_each_connector_iter(connector, &conn_iter)
1138 drm_atomic_connector_print_state(p, connector->state);
1139 if (take_locks)
1140 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1141 drm_connector_list_iter_end(&conn_iter);
1142}
1143
Rob Clark6559c902016-11-05 11:08:10 -04001144/**
1145 * drm_state_dump - dump entire device atomic state
1146 * @dev: the drm device
1147 * @p: where to print the state to
1148 *
1149 * Just for debugging. Drivers might want an option to dump state
1150 * to dmesg in case of error irq's. (Hint, you probably want to
1151 * ratelimit this!)
1152 *
1153 * The caller must drm_modeset_lock_all(), or if this is called
1154 * from error irq handler, it should not be enabled by default.
1155 * (Ie. if you are debugging errors you might not care that this
1156 * is racey. But calling this without all modeset locks held is
1157 * not inherently safe.)
1158 */
1159void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
1160{
Daniel Vetterc2d85562017-04-03 10:32:54 +02001161 __drm_state_dump(dev, p, false);
Rob Clark6559c902016-11-05 11:08:10 -04001162}
1163EXPORT_SYMBOL(drm_state_dump);
1164
1165#ifdef CONFIG_DEBUG_FS
1166static int drm_state_info(struct seq_file *m, void *data)
1167{
1168 struct drm_info_node *node = (struct drm_info_node *) m->private;
1169 struct drm_device *dev = node->minor->dev;
1170 struct drm_printer p = drm_seq_file_printer(m);
1171
Daniel Vetterc2d85562017-04-03 10:32:54 +02001172 __drm_state_dump(dev, &p, true);
Rob Clark6559c902016-11-05 11:08:10 -04001173
1174 return 0;
1175}
1176
1177/* any use in debugfs files to dump individual planes/crtc/etc? */
1178static const struct drm_info_list drm_atomic_debugfs_list[] = {
1179 {"state", drm_state_info, 0},
1180};
1181
1182int drm_atomic_debugfs_init(struct drm_minor *minor)
1183{
1184 return drm_debugfs_create_files(drm_atomic_debugfs_list,
1185 ARRAY_SIZE(drm_atomic_debugfs_list),
1186 minor->debugfs_root, minor);
1187}
1188#endif
1189