blob: e749287390a3ad957681e9165d5d0884e86bd887 [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>
31#include <drm/drm_plane_helper.h>
32
Maarten Lankhorst036ef572015-05-18 10:06:40 +020033/**
34 * drm_atomic_state_default_release -
35 * release memory initialized by drm_atomic_state_init
36 * @state: atomic state
37 *
38 * Free all the memory allocated by drm_atomic_state_init.
39 * This is useful for drivers that subclass the atomic state.
40 */
41void drm_atomic_state_default_release(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +020042{
43 kfree(state->connectors);
44 kfree(state->connector_states);
45 kfree(state->crtcs);
46 kfree(state->crtc_states);
47 kfree(state->planes);
48 kfree(state->plane_states);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020049}
Maarten Lankhorst036ef572015-05-18 10:06:40 +020050EXPORT_SYMBOL(drm_atomic_state_default_release);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020051
52/**
Maarten Lankhorst036ef572015-05-18 10:06:40 +020053 * drm_atomic_state_init - init new atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +020054 * @dev: DRM device
Maarten Lankhorst036ef572015-05-18 10:06:40 +020055 * @state: atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +020056 *
Maarten Lankhorst036ef572015-05-18 10:06:40 +020057 * Default implementation for filling in a new atomic state.
58 * This is useful for drivers that subclass the atomic state.
Daniel Vettercc4ceb42014-07-25 21:30:38 +020059 */
Maarten Lankhorst036ef572015-05-18 10:06:40 +020060int
61drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +020062{
Rob Clarkd34f20d2014-12-18 16:01:56 -050063 /* TODO legacy paths should maybe do a better job about
64 * setting this appropriately?
65 */
66 state->allow_modeset = true;
67
Daniel Vetterf52b69f12014-11-19 18:38:08 +010068 state->num_connector = ACCESS_ONCE(dev->mode_config.num_connector);
69
Daniel Vettercc4ceb42014-07-25 21:30:38 +020070 state->crtcs = kcalloc(dev->mode_config.num_crtc,
71 sizeof(*state->crtcs), GFP_KERNEL);
72 if (!state->crtcs)
73 goto fail;
74 state->crtc_states = kcalloc(dev->mode_config.num_crtc,
75 sizeof(*state->crtc_states), GFP_KERNEL);
76 if (!state->crtc_states)
77 goto fail;
78 state->planes = kcalloc(dev->mode_config.num_total_plane,
79 sizeof(*state->planes), GFP_KERNEL);
80 if (!state->planes)
81 goto fail;
82 state->plane_states = kcalloc(dev->mode_config.num_total_plane,
83 sizeof(*state->plane_states), GFP_KERNEL);
84 if (!state->plane_states)
85 goto fail;
Daniel Vetterf52b69f12014-11-19 18:38:08 +010086 state->connectors = kcalloc(state->num_connector,
Daniel Vettercc4ceb42014-07-25 21:30:38 +020087 sizeof(*state->connectors),
88 GFP_KERNEL);
89 if (!state->connectors)
90 goto fail;
Daniel Vetterf52b69f12014-11-19 18:38:08 +010091 state->connector_states = kcalloc(state->num_connector,
Daniel Vettercc4ceb42014-07-25 21:30:38 +020092 sizeof(*state->connector_states),
93 GFP_KERNEL);
94 if (!state->connector_states)
95 goto fail;
96
97 state->dev = dev;
98
Maarten Lankhorst036ef572015-05-18 10:06:40 +020099 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200100
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200101 return 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200102fail:
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200103 drm_atomic_state_default_release(state);
104 return -ENOMEM;
105}
106EXPORT_SYMBOL(drm_atomic_state_init);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200107
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200108/**
109 * drm_atomic_state_alloc - allocate atomic state
110 * @dev: DRM device
111 *
112 * This allocates an empty atomic state to track updates.
113 */
114struct drm_atomic_state *
115drm_atomic_state_alloc(struct drm_device *dev)
116{
117 struct drm_mode_config *config = &dev->mode_config;
118 struct drm_atomic_state *state;
119
120 if (!config->funcs->atomic_state_alloc) {
121 state = kzalloc(sizeof(*state), GFP_KERNEL);
122 if (!state)
123 return NULL;
124 if (drm_atomic_state_init(dev, state) < 0) {
125 kfree(state);
126 return NULL;
127 }
128 return state;
129 }
130
131 return config->funcs->atomic_state_alloc(dev);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200132}
133EXPORT_SYMBOL(drm_atomic_state_alloc);
134
135/**
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200136 * drm_atomic_state_default_clear - clear base atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200137 * @state: atomic state
138 *
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200139 * Default implementation for clearing atomic state.
140 * This is useful for drivers that subclass the atomic state.
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 Vettercc4ceb42014-07-25 21:30:38 +0200151 struct drm_connector *connector = state->connectors[i];
152
153 if (!connector)
154 continue;
155
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100156 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
157
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200158 connector->funcs->atomic_destroy_state(connector,
159 state->connector_states[i]);
Ander Conselvan de Oliveira8a5c0bd2015-03-30 10:41:19 +0300160 state->connectors[i] = NULL;
Ander Conselvan de Oliveira94692442015-01-23 09:27:59 +0200161 state->connector_states[i] = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200162 }
163
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100164 for (i = 0; i < config->num_crtc; i++) {
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200165 struct drm_crtc *crtc = state->crtcs[i];
166
167 if (!crtc)
168 continue;
169
170 crtc->funcs->atomic_destroy_state(crtc,
171 state->crtc_states[i]);
Ander Conselvan de Oliveira8a5c0bd2015-03-30 10:41:19 +0300172 state->crtcs[i] = NULL;
Ander Conselvan de Oliveira94692442015-01-23 09:27:59 +0200173 state->crtc_states[i] = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200174 }
175
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100176 for (i = 0; i < config->num_total_plane; i++) {
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200177 struct drm_plane *plane = state->planes[i];
178
179 if (!plane)
180 continue;
181
182 plane->funcs->atomic_destroy_state(plane,
183 state->plane_states[i]);
Ander Conselvan de Oliveira8a5c0bd2015-03-30 10:41:19 +0300184 state->planes[i] = NULL;
Ander Conselvan de Oliveira94692442015-01-23 09:27:59 +0200185 state->plane_states[i] = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200186 }
187}
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200188EXPORT_SYMBOL(drm_atomic_state_default_clear);
189
190/**
191 * drm_atomic_state_clear - clear state object
192 * @state: atomic state
193 *
194 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
195 * all locks. So someone else could sneak in and change the current modeset
196 * configuration. Which means that all the state assembled in @state is no
197 * longer an atomic update to the current state, but to some arbitrary earlier
198 * state. Which could break assumptions the driver's ->atomic_check likely
199 * relies on.
200 *
201 * Hence we must clear all cached state and completely start over, using this
202 * function.
203 */
204void drm_atomic_state_clear(struct drm_atomic_state *state)
205{
206 struct drm_device *dev = state->dev;
207 struct drm_mode_config *config = &dev->mode_config;
208
209 if (config->funcs->atomic_state_clear)
210 config->funcs->atomic_state_clear(state);
211 else
212 drm_atomic_state_default_clear(state);
213}
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200214EXPORT_SYMBOL(drm_atomic_state_clear);
215
216/**
217 * drm_atomic_state_free - free all memory for an atomic state
218 * @state: atomic state to deallocate
219 *
220 * This frees all memory associated with an atomic state, including all the
221 * per-object state for planes, crtcs and connectors.
222 */
223void drm_atomic_state_free(struct drm_atomic_state *state)
224{
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200225 struct drm_device *dev;
226 struct drm_mode_config *config;
227
Ander Conselvan de Oliveiraa0211bb2015-03-30 14:05:43 +0300228 if (!state)
229 return;
230
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200231 dev = state->dev;
232 config = &dev->mode_config;
233
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200234 drm_atomic_state_clear(state);
235
Daniel Vetter17a38d92015-02-22 12:24:16 +0100236 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200237
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200238 if (config->funcs->atomic_state_free) {
239 config->funcs->atomic_state_free(state);
240 } else {
241 drm_atomic_state_default_release(state);
242 kfree(state);
243 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200244}
245EXPORT_SYMBOL(drm_atomic_state_free);
246
247/**
248 * drm_atomic_get_crtc_state - get crtc state
249 * @state: global atomic state object
250 * @crtc: crtc to get state object for
251 *
252 * This function returns the crtc state for the given crtc, allocating it if
253 * needed. It will also grab the relevant crtc lock to make sure that the state
254 * is consistent.
255 *
256 * Returns:
257 *
258 * Either the allocated state or the error code encoded into the pointer. When
259 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
260 * entire atomic sequence must be restarted. All other errors are fatal.
261 */
262struct drm_crtc_state *
263drm_atomic_get_crtc_state(struct drm_atomic_state *state,
264 struct drm_crtc *crtc)
265{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200266 int ret, index = drm_crtc_index(crtc);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200267 struct drm_crtc_state *crtc_state;
268
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200269 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
270 if (crtc_state)
271 return crtc_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200272
273 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
274 if (ret)
275 return ERR_PTR(ret);
276
277 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
278 if (!crtc_state)
279 return ERR_PTR(-ENOMEM);
280
281 state->crtc_states[index] = crtc_state;
282 state->crtcs[index] = crtc;
283 crtc_state->state = state;
284
Daniel Vetter17a38d92015-02-22 12:24:16 +0100285 DRM_DEBUG_ATOMIC("Added [CRTC:%d] %p state to %p\n",
286 crtc->base.id, crtc_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200287
288 return crtc_state;
289}
290EXPORT_SYMBOL(drm_atomic_get_crtc_state);
291
292/**
Daniel Stone819364d2015-05-26 14:36:48 +0100293 * drm_atomic_set_mode_for_crtc - set mode for CRTC
294 * @state: the CRTC whose incoming state to update
295 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
296 *
297 * Set a mode (originating from the kernel) on the desired CRTC state. Does
298 * not change any other state properties, including enable, active, or
299 * mode_changed.
300 *
301 * RETURNS:
302 * Zero on success, error code on failure. Cannot return -EDEADLK.
303 */
304int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
305 struct drm_display_mode *mode)
306{
307 /* Early return for no change. */
308 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
309 return 0;
310
311 if (mode) {
312 drm_mode_copy(&state->mode, mode);
313 state->enable = true;
314 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
315 mode->name, state);
316 } else {
317 memset(&state->mode, 0, sizeof(state->mode));
318 state->enable = false;
319 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
320 state);
321 }
322
323 return 0;
324}
325EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
326
327
328/**
Rob Clark40ecc692014-12-18 16:01:46 -0500329 * drm_atomic_crtc_set_property - set property on CRTC
330 * @crtc: the drm CRTC to set a property on
331 * @state: the state object to update with the new property value
332 * @property: the property to set
333 * @val: the new property value
334 *
335 * Use this instead of calling crtc->atomic_set_property directly.
336 * This function handles generic/core properties and calls out to
337 * driver's ->atomic_set_property() for driver properties. To ensure
338 * consistent behavior you must call this function rather than the
339 * driver hook directly.
340 *
341 * RETURNS:
342 * Zero on success, error code on failure
343 */
344int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
345 struct drm_crtc_state *state, struct drm_property *property,
346 uint64_t val)
347{
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100348 struct drm_device *dev = crtc->dev;
349 struct drm_mode_config *config = &dev->mode_config;
350
351 /* FIXME: Mode prop is missing, which also controls ->enable. */
Daniel Stone27798362015-03-19 04:33:26 +0000352 if (property == config->prop_active)
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100353 state->active = val;
Daniel Stone27798362015-03-19 04:33:26 +0000354 else if (crtc->funcs->atomic_set_property)
Rob Clark40ecc692014-12-18 16:01:46 -0500355 return crtc->funcs->atomic_set_property(crtc, state, property, val);
Daniel Stone27798362015-03-19 04:33:26 +0000356 else
357 return -EINVAL;
358
359 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -0500360}
361EXPORT_SYMBOL(drm_atomic_crtc_set_property);
362
Daniel Vettera97df1c2014-12-18 22:49:02 +0100363/*
Rob Clarkac9c9252014-12-18 16:01:47 -0500364 * This function handles generic/core properties and calls out to
365 * driver's ->atomic_get_property() for driver properties. To ensure
366 * consistent behavior you must call this function rather than the
367 * driver hook directly.
Rob Clarkac9c9252014-12-18 16:01:47 -0500368 */
369int drm_atomic_crtc_get_property(struct drm_crtc *crtc,
370 const struct drm_crtc_state *state,
371 struct drm_property *property, uint64_t *val)
372{
Daniel Stone8f164ce2015-03-19 04:33:25 +0000373 struct drm_device *dev = crtc->dev;
374 struct drm_mode_config *config = &dev->mode_config;
375
376 if (property == config->prop_active)
377 *val = state->active;
378 else if (crtc->funcs->atomic_get_property)
Rob Clarkac9c9252014-12-18 16:01:47 -0500379 return crtc->funcs->atomic_get_property(crtc, state, property, val);
Daniel Stone8f164ce2015-03-19 04:33:25 +0000380 else
381 return -EINVAL;
382
383 return 0;
Rob Clarkac9c9252014-12-18 16:01:47 -0500384}
Rob Clarkac9c9252014-12-18 16:01:47 -0500385
386/**
Rob Clark5e743732014-12-18 16:01:51 -0500387 * drm_atomic_crtc_check - check crtc state
388 * @crtc: crtc to check
389 * @state: crtc state to check
390 *
391 * Provides core sanity checks for crtc state.
392 *
393 * RETURNS:
394 * Zero on success, error code on failure
395 */
396static int drm_atomic_crtc_check(struct drm_crtc *crtc,
397 struct drm_crtc_state *state)
398{
399 /* NOTE: we explicitly don't enforce constraints such as primary
400 * layer covering entire screen, since that is something we want
401 * to allow (on hw that supports it). For hw that does not, it
402 * should be checked in driver's crtc->atomic_check() vfunc.
403 *
404 * TODO: Add generic modeset state checks once we support those.
405 */
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100406
407 if (state->active && !state->enable) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100408 DRM_DEBUG_ATOMIC("[CRTC:%d] active without enabled\n",
409 crtc->base.id);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100410 return -EINVAL;
411 }
412
Rob Clark5e743732014-12-18 16:01:51 -0500413 return 0;
414}
415
416/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200417 * drm_atomic_get_plane_state - get plane state
418 * @state: global atomic state object
419 * @plane: plane to get state object for
420 *
421 * This function returns the plane state for the given plane, allocating it if
422 * needed. It will also grab the relevant plane lock to make sure that the state
423 * is consistent.
424 *
425 * Returns:
426 *
427 * Either the allocated state or the error code encoded into the pointer. When
428 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
429 * entire atomic sequence must be restarted. All other errors are fatal.
430 */
431struct drm_plane_state *
432drm_atomic_get_plane_state(struct drm_atomic_state *state,
433 struct drm_plane *plane)
434{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200435 int ret, index = drm_plane_index(plane);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200436 struct drm_plane_state *plane_state;
437
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200438 plane_state = drm_atomic_get_existing_plane_state(state, plane);
439 if (plane_state)
440 return plane_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200441
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100442 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200443 if (ret)
444 return ERR_PTR(ret);
445
446 plane_state = plane->funcs->atomic_duplicate_state(plane);
447 if (!plane_state)
448 return ERR_PTR(-ENOMEM);
449
450 state->plane_states[index] = plane_state;
451 state->planes[index] = plane;
452 plane_state->state = state;
453
Daniel Vetter17a38d92015-02-22 12:24:16 +0100454 DRM_DEBUG_ATOMIC("Added [PLANE:%d] %p state to %p\n",
455 plane->base.id, plane_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200456
457 if (plane_state->crtc) {
458 struct drm_crtc_state *crtc_state;
459
460 crtc_state = drm_atomic_get_crtc_state(state,
461 plane_state->crtc);
462 if (IS_ERR(crtc_state))
463 return ERR_CAST(crtc_state);
464 }
465
466 return plane_state;
467}
468EXPORT_SYMBOL(drm_atomic_get_plane_state);
469
470/**
Rob Clark40ecc692014-12-18 16:01:46 -0500471 * drm_atomic_plane_set_property - set property on plane
472 * @plane: the drm plane to set a property on
473 * @state: the state object to update with the new property value
474 * @property: the property to set
475 * @val: the new property value
476 *
477 * Use this instead of calling plane->atomic_set_property directly.
478 * This function handles generic/core properties and calls out to
479 * driver's ->atomic_set_property() for driver properties. To ensure
480 * consistent behavior you must call this function rather than the
481 * driver hook directly.
482 *
483 * RETURNS:
484 * Zero on success, error code on failure
485 */
486int drm_atomic_plane_set_property(struct drm_plane *plane,
487 struct drm_plane_state *state, struct drm_property *property,
488 uint64_t val)
489{
Rob Clark6b4959f2014-12-18 16:01:53 -0500490 struct drm_device *dev = plane->dev;
491 struct drm_mode_config *config = &dev->mode_config;
492
493 if (property == config->prop_fb_id) {
494 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
495 drm_atomic_set_fb_for_plane(state, fb);
496 if (fb)
497 drm_framebuffer_unreference(fb);
498 } else if (property == config->prop_crtc_id) {
499 struct drm_crtc *crtc = drm_crtc_find(dev, val);
500 return drm_atomic_set_crtc_for_plane(state, crtc);
501 } else if (property == config->prop_crtc_x) {
502 state->crtc_x = U642I64(val);
503 } else if (property == config->prop_crtc_y) {
504 state->crtc_y = U642I64(val);
505 } else if (property == config->prop_crtc_w) {
506 state->crtc_w = val;
507 } else if (property == config->prop_crtc_h) {
508 state->crtc_h = val;
509 } else if (property == config->prop_src_x) {
510 state->src_x = val;
511 } else if (property == config->prop_src_y) {
512 state->src_y = val;
513 } else if (property == config->prop_src_w) {
514 state->src_w = val;
515 } else if (property == config->prop_src_h) {
516 state->src_h = val;
Matt Roper1da30622015-01-21 16:35:40 -0800517 } else if (property == config->rotation_property) {
518 state->rotation = val;
Rob Clark6b4959f2014-12-18 16:01:53 -0500519 } else if (plane->funcs->atomic_set_property) {
520 return plane->funcs->atomic_set_property(plane, state,
521 property, val);
522 } else {
523 return -EINVAL;
524 }
525
526 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -0500527}
528EXPORT_SYMBOL(drm_atomic_plane_set_property);
529
Daniel Vettera97df1c2014-12-18 22:49:02 +0100530/*
Rob Clarkac9c9252014-12-18 16:01:47 -0500531 * This function handles generic/core properties and calls out to
532 * driver's ->atomic_get_property() for driver properties. To ensure
533 * consistent behavior you must call this function rather than the
534 * driver hook directly.
Rob Clarkac9c9252014-12-18 16:01:47 -0500535 */
Daniel Vettera97df1c2014-12-18 22:49:02 +0100536static int
537drm_atomic_plane_get_property(struct drm_plane *plane,
Rob Clarkac9c9252014-12-18 16:01:47 -0500538 const struct drm_plane_state *state,
539 struct drm_property *property, uint64_t *val)
540{
Rob Clark6b4959f2014-12-18 16:01:53 -0500541 struct drm_device *dev = plane->dev;
542 struct drm_mode_config *config = &dev->mode_config;
543
544 if (property == config->prop_fb_id) {
545 *val = (state->fb) ? state->fb->base.id : 0;
546 } else if (property == config->prop_crtc_id) {
547 *val = (state->crtc) ? state->crtc->base.id : 0;
548 } else if (property == config->prop_crtc_x) {
549 *val = I642U64(state->crtc_x);
550 } else if (property == config->prop_crtc_y) {
551 *val = I642U64(state->crtc_y);
552 } else if (property == config->prop_crtc_w) {
553 *val = state->crtc_w;
554 } else if (property == config->prop_crtc_h) {
555 *val = state->crtc_h;
556 } else if (property == config->prop_src_x) {
557 *val = state->src_x;
558 } else if (property == config->prop_src_y) {
559 *val = state->src_y;
560 } else if (property == config->prop_src_w) {
561 *val = state->src_w;
562 } else if (property == config->prop_src_h) {
563 *val = state->src_h;
Tvrtko Ursulin4cda09c2015-02-26 13:49:17 +0000564 } else if (property == config->rotation_property) {
565 *val = state->rotation;
Rob Clark6b4959f2014-12-18 16:01:53 -0500566 } else if (plane->funcs->atomic_get_property) {
Rob Clarkac9c9252014-12-18 16:01:47 -0500567 return plane->funcs->atomic_get_property(plane, state, property, val);
Rob Clark6b4959f2014-12-18 16:01:53 -0500568 } else {
569 return -EINVAL;
570 }
571
572 return 0;
Rob Clarkac9c9252014-12-18 16:01:47 -0500573}
Rob Clarkac9c9252014-12-18 16:01:47 -0500574
575/**
Rob Clark5e743732014-12-18 16:01:51 -0500576 * drm_atomic_plane_check - check plane state
577 * @plane: plane to check
578 * @state: plane state to check
579 *
580 * Provides core sanity checks for plane state.
581 *
582 * RETURNS:
583 * Zero on success, error code on failure
584 */
585static int drm_atomic_plane_check(struct drm_plane *plane,
586 struct drm_plane_state *state)
587{
588 unsigned int fb_width, fb_height;
Laurent Pinchartead86102015-03-05 02:25:43 +0200589 int ret;
Rob Clark5e743732014-12-18 16:01:51 -0500590
591 /* either *both* CRTC and FB must be set, or neither */
592 if (WARN_ON(state->crtc && !state->fb)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100593 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
Rob Clark5e743732014-12-18 16:01:51 -0500594 return -EINVAL;
595 } else if (WARN_ON(state->fb && !state->crtc)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100596 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
Rob Clark5e743732014-12-18 16:01:51 -0500597 return -EINVAL;
598 }
599
600 /* if disabled, we don't care about the rest of the state: */
601 if (!state->crtc)
602 return 0;
603
604 /* Check whether this plane is usable on this CRTC */
605 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100606 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
Rob Clark5e743732014-12-18 16:01:51 -0500607 return -EINVAL;
608 }
609
610 /* Check whether this plane supports the fb pixel format. */
Laurent Pinchartead86102015-03-05 02:25:43 +0200611 ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
612 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100613 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
614 drm_get_format_name(state->fb->pixel_format));
Laurent Pinchartead86102015-03-05 02:25:43 +0200615 return ret;
Rob Clark5e743732014-12-18 16:01:51 -0500616 }
617
618 /* Give drivers some help against integer overflows */
619 if (state->crtc_w > INT_MAX ||
620 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
621 state->crtc_h > INT_MAX ||
622 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100623 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
624 state->crtc_w, state->crtc_h,
625 state->crtc_x, state->crtc_y);
Rob Clark5e743732014-12-18 16:01:51 -0500626 return -ERANGE;
627 }
628
629 fb_width = state->fb->width << 16;
630 fb_height = state->fb->height << 16;
631
632 /* Make sure source coordinates are inside the fb. */
633 if (state->src_w > fb_width ||
634 state->src_x > fb_width - state->src_w ||
635 state->src_h > fb_height ||
636 state->src_y > fb_height - state->src_h) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100637 DRM_DEBUG_ATOMIC("Invalid source coordinates "
638 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
639 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
640 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
641 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
642 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
Rob Clark5e743732014-12-18 16:01:51 -0500643 return -ENOSPC;
644 }
645
646 return 0;
647}
648
649/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200650 * drm_atomic_get_connector_state - get connector state
651 * @state: global atomic state object
652 * @connector: connector to get state object for
653 *
654 * This function returns the connector state for the given connector,
655 * allocating it if needed. It will also grab the relevant connector lock to
656 * make sure that the state is consistent.
657 *
658 * Returns:
659 *
660 * Either the allocated state or the error code encoded into the pointer. When
661 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
662 * entire atomic sequence must be restarted. All other errors are fatal.
663 */
664struct drm_connector_state *
665drm_atomic_get_connector_state(struct drm_atomic_state *state,
666 struct drm_connector *connector)
667{
668 int ret, index;
669 struct drm_mode_config *config = &connector->dev->mode_config;
670 struct drm_connector_state *connector_state;
671
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100672 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
673 if (ret)
674 return ERR_PTR(ret);
675
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200676 index = drm_connector_index(connector);
677
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100678 /*
679 * Construction of atomic state updates can race with a connector
680 * hot-add which might overflow. In this case flip the table and just
681 * restart the entire ioctl - no one is fast enough to livelock a cpu
682 * with physical hotplug events anyway.
683 *
684 * Note that we only grab the indexes once we have the right lock to
685 * prevent hotplug/unplugging of connectors. So removal is no problem,
686 * at most the array is a bit too large.
687 */
688 if (index >= state->num_connector) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100689 DRM_DEBUG_ATOMIC("Hot-added connector would overflow state array, restarting\n");
Daniel Vetterfc2d2bc2014-11-20 09:53:35 +0100690 return ERR_PTR(-EAGAIN);
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100691 }
692
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200693 if (state->connector_states[index])
694 return state->connector_states[index];
695
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200696 connector_state = connector->funcs->atomic_duplicate_state(connector);
697 if (!connector_state)
698 return ERR_PTR(-ENOMEM);
699
700 state->connector_states[index] = connector_state;
701 state->connectors[index] = connector;
702 connector_state->state = state;
703
Daniel Vetter17a38d92015-02-22 12:24:16 +0100704 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
705 connector->base.id, connector_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200706
707 if (connector_state->crtc) {
708 struct drm_crtc_state *crtc_state;
709
710 crtc_state = drm_atomic_get_crtc_state(state,
711 connector_state->crtc);
712 if (IS_ERR(crtc_state))
713 return ERR_CAST(crtc_state);
714 }
715
716 return connector_state;
717}
718EXPORT_SYMBOL(drm_atomic_get_connector_state);
719
720/**
Rob Clark40ecc692014-12-18 16:01:46 -0500721 * drm_atomic_connector_set_property - set property on connector.
722 * @connector: the drm connector to set a property on
723 * @state: the state object to update with the new property value
724 * @property: the property to set
725 * @val: the new property value
726 *
727 * Use this instead of calling connector->atomic_set_property directly.
728 * This function handles generic/core properties and calls out to
729 * driver's ->atomic_set_property() for driver properties. To ensure
730 * consistent behavior you must call this function rather than the
731 * driver hook directly.
732 *
733 * RETURNS:
734 * Zero on success, error code on failure
735 */
736int drm_atomic_connector_set_property(struct drm_connector *connector,
737 struct drm_connector_state *state, struct drm_property *property,
738 uint64_t val)
739{
740 struct drm_device *dev = connector->dev;
741 struct drm_mode_config *config = &dev->mode_config;
742
Rob Clarkae16c592014-12-18 16:01:54 -0500743 if (property == config->prop_crtc_id) {
744 struct drm_crtc *crtc = drm_crtc_find(dev, val);
745 return drm_atomic_set_crtc_for_connector(state, crtc);
746 } else if (property == config->dpms_property) {
Rob Clark40ecc692014-12-18 16:01:46 -0500747 /* setting DPMS property requires special handling, which
748 * is done in legacy setprop path for us. Disallow (for
749 * now?) atomic writes to DPMS property:
750 */
751 return -EINVAL;
752 } else if (connector->funcs->atomic_set_property) {
753 return connector->funcs->atomic_set_property(connector,
754 state, property, val);
755 } else {
756 return -EINVAL;
757 }
758}
759EXPORT_SYMBOL(drm_atomic_connector_set_property);
760
Daniel Vettera97df1c2014-12-18 22:49:02 +0100761/*
Rob Clarkac9c9252014-12-18 16:01:47 -0500762 * This function handles generic/core properties and calls out to
763 * driver's ->atomic_get_property() for driver properties. To ensure
764 * consistent behavior you must call this function rather than the
765 * driver hook directly.
Rob Clarkac9c9252014-12-18 16:01:47 -0500766 */
Daniel Vettera97df1c2014-12-18 22:49:02 +0100767static int
768drm_atomic_connector_get_property(struct drm_connector *connector,
Rob Clarkac9c9252014-12-18 16:01:47 -0500769 const struct drm_connector_state *state,
770 struct drm_property *property, uint64_t *val)
771{
772 struct drm_device *dev = connector->dev;
773 struct drm_mode_config *config = &dev->mode_config;
774
Rob Clarkae16c592014-12-18 16:01:54 -0500775 if (property == config->prop_crtc_id) {
776 *val = (state->crtc) ? state->crtc->base.id : 0;
777 } else if (property == config->dpms_property) {
Rob Clarkac9c9252014-12-18 16:01:47 -0500778 *val = connector->dpms;
779 } else if (connector->funcs->atomic_get_property) {
780 return connector->funcs->atomic_get_property(connector,
781 state, property, val);
782 } else {
783 return -EINVAL;
784 }
785
786 return 0;
787}
Rob Clarkac9c9252014-12-18 16:01:47 -0500788
Rob Clark88a48e22014-12-18 16:01:50 -0500789int drm_atomic_get_property(struct drm_mode_object *obj,
790 struct drm_property *property, uint64_t *val)
791{
792 struct drm_device *dev = property->dev;
793 int ret;
794
795 switch (obj->type) {
796 case DRM_MODE_OBJECT_CONNECTOR: {
797 struct drm_connector *connector = obj_to_connector(obj);
798 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
799 ret = drm_atomic_connector_get_property(connector,
800 connector->state, property, val);
801 break;
802 }
803 case DRM_MODE_OBJECT_CRTC: {
804 struct drm_crtc *crtc = obj_to_crtc(obj);
805 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
806 ret = drm_atomic_crtc_get_property(crtc,
807 crtc->state, property, val);
808 break;
809 }
810 case DRM_MODE_OBJECT_PLANE: {
811 struct drm_plane *plane = obj_to_plane(obj);
812 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
813 ret = drm_atomic_plane_get_property(plane,
814 plane->state, property, val);
815 break;
816 }
817 default:
818 ret = -EINVAL;
819 break;
820 }
821
822 return ret;
823}
824
825/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200826 * drm_atomic_set_crtc_for_plane - set crtc for plane
Daniel Vetter07cc0ef2014-11-27 15:49:39 +0100827 * @plane_state: the plane whose incoming state to update
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200828 * @crtc: crtc to use for the plane
829 *
830 * Changing the assigned crtc for a plane requires us to grab the lock and state
831 * for the new crtc, as needed. This function takes care of all these details
832 * besides updating the pointer in the state object itself.
833 *
834 * Returns:
835 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
836 * then the w/w mutex code has detected a deadlock and the entire atomic
837 * sequence must be restarted. All other errors are fatal.
838 */
839int
Daniel Vetter07cc0ef2014-11-27 15:49:39 +0100840drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
841 struct drm_crtc *crtc)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200842{
Daniel Vetter07cc0ef2014-11-27 15:49:39 +0100843 struct drm_plane *plane = plane_state->plane;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200844 struct drm_crtc_state *crtc_state;
845
Rob Clark6ddd3882014-11-21 15:28:31 -0500846 if (plane_state->crtc) {
847 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
848 plane_state->crtc);
849 if (WARN_ON(IS_ERR(crtc_state)))
850 return PTR_ERR(crtc_state);
851
852 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
853 }
854
855 plane_state->crtc = crtc;
856
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200857 if (crtc) {
858 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
859 crtc);
860 if (IS_ERR(crtc_state))
861 return PTR_ERR(crtc_state);
Rob Clark6ddd3882014-11-21 15:28:31 -0500862 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200863 }
864
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200865 if (crtc)
Daniel Vetter17a38d92015-02-22 12:24:16 +0100866 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d]\n",
867 plane_state, crtc->base.id);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200868 else
Daniel Vetter17a38d92015-02-22 12:24:16 +0100869 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
870 plane_state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200871
872 return 0;
873}
874EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
875
876/**
John Hunter16d78bc2e2015-04-07 19:38:50 +0800877 * drm_atomic_set_fb_for_plane - set framebuffer for plane
Daniel Vetter321ebf02014-11-04 22:57:27 +0100878 * @plane_state: atomic state object for the plane
879 * @fb: fb to use for the plane
880 *
881 * Changing the assigned framebuffer for a plane requires us to grab a reference
882 * to the new fb and drop the reference to the old fb, if there is one. This
883 * function takes care of all these details besides updating the pointer in the
884 * state object itself.
885 */
886void
887drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
888 struct drm_framebuffer *fb)
889{
890 if (plane_state->fb)
891 drm_framebuffer_unreference(plane_state->fb);
892 if (fb)
893 drm_framebuffer_reference(fb);
894 plane_state->fb = fb;
895
896 if (fb)
Daniel Vetter17a38d92015-02-22 12:24:16 +0100897 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
898 fb->base.id, plane_state);
Daniel Vetter321ebf02014-11-04 22:57:27 +0100899 else
Daniel Vetter17a38d92015-02-22 12:24:16 +0100900 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
901 plane_state);
Daniel Vetter321ebf02014-11-04 22:57:27 +0100902}
903EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
904
905/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200906 * drm_atomic_set_crtc_for_connector - set crtc for connector
907 * @conn_state: atomic state object for the connector
908 * @crtc: crtc to use for the connector
909 *
910 * Changing the assigned crtc for a connector requires us to grab the lock and
911 * state for the new crtc, as needed. This function takes care of all these
912 * details besides updating the pointer in the state object itself.
913 *
914 * Returns:
915 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
916 * then the w/w mutex code has detected a deadlock and the entire atomic
917 * sequence must be restarted. All other errors are fatal.
918 */
919int
920drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
921 struct drm_crtc *crtc)
922{
923 struct drm_crtc_state *crtc_state;
924
925 if (crtc) {
926 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
927 if (IS_ERR(crtc_state))
928 return PTR_ERR(crtc_state);
929 }
930
931 conn_state->crtc = crtc;
932
933 if (crtc)
Daniel Vetter17a38d92015-02-22 12:24:16 +0100934 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d]\n",
935 conn_state, crtc->base.id);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200936 else
Daniel Vetter17a38d92015-02-22 12:24:16 +0100937 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
938 conn_state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200939
940 return 0;
941}
942EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
943
944/**
945 * drm_atomic_add_affected_connectors - add connectors for crtc
946 * @state: atomic state
947 * @crtc: DRM crtc
948 *
949 * This function walks the current configuration and adds all connectors
950 * currently using @crtc to the atomic configuration @state. Note that this
951 * function must acquire the connection mutex. This can potentially cause
952 * unneeded seralization if the update is just for the planes on one crtc. Hence
953 * drivers and helpers should only call this when really needed (e.g. when a
954 * full modeset needs to happen due to some change).
955 *
956 * Returns:
957 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
958 * then the w/w mutex code has detected a deadlock and the entire atomic
959 * sequence must be restarted. All other errors are fatal.
960 */
961int
962drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
963 struct drm_crtc *crtc)
964{
965 struct drm_mode_config *config = &state->dev->mode_config;
966 struct drm_connector *connector;
967 struct drm_connector_state *conn_state;
968 int ret;
969
970 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
971 if (ret)
972 return ret;
973
Daniel Vetter17a38d92015-02-22 12:24:16 +0100974 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d] to %p\n",
975 crtc->base.id, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200976
977 /*
978 * Changed connectors are already in @state, so only need to look at the
979 * current configuration.
980 */
981 list_for_each_entry(connector, &config->connector_list, head) {
982 if (connector->state->crtc != crtc)
983 continue;
984
985 conn_state = drm_atomic_get_connector_state(state, connector);
986 if (IS_ERR(conn_state))
987 return PTR_ERR(conn_state);
988 }
989
990 return 0;
991}
992EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
993
994/**
Maarten Lankhorste01e9f72015-05-19 16:41:02 +0200995 * drm_atomic_add_affected_planes - add planes for crtc
996 * @state: atomic state
997 * @crtc: DRM crtc
998 *
999 * This function walks the current configuration and adds all planes
1000 * currently used by @crtc to the atomic configuration @state. This is useful
1001 * when an atomic commit also needs to check all currently enabled plane on
1002 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1003 * to avoid special code to force-enable all planes.
1004 *
1005 * Since acquiring a plane state will always also acquire the w/w mutex of the
1006 * current CRTC for that plane (if there is any) adding all the plane states for
1007 * a CRTC will not reduce parallism of atomic updates.
1008 *
1009 * Returns:
1010 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1011 * then the w/w mutex code has detected a deadlock and the entire atomic
1012 * sequence must be restarted. All other errors are fatal.
1013 */
1014int
1015drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1016 struct drm_crtc *crtc)
1017{
1018 struct drm_plane *plane;
1019
1020 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
1021
1022 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1023 struct drm_plane_state *plane_state =
1024 drm_atomic_get_plane_state(state, plane);
1025
1026 if (IS_ERR(plane_state))
1027 return PTR_ERR(plane_state);
1028 }
1029 return 0;
1030}
1031EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1032
1033/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001034 * drm_atomic_connectors_for_crtc - count number of connected outputs
1035 * @state: atomic state
1036 * @crtc: DRM crtc
1037 *
1038 * This function counts all connectors which will be connected to @crtc
1039 * according to @state. Useful to recompute the enable state for @crtc.
1040 */
1041int
1042drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
1043 struct drm_crtc *crtc)
1044{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001045 struct drm_connector *connector;
1046 struct drm_connector_state *conn_state;
1047
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001048 int i, num_connected_connectors = 0;
1049
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001050 for_each_connector_in_state(state, connector, conn_state, i) {
1051 if (conn_state->crtc == crtc)
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001052 num_connected_connectors++;
1053 }
1054
Daniel Vetter17a38d92015-02-22 12:24:16 +01001055 DRM_DEBUG_ATOMIC("State %p has %i connectors for [CRTC:%d]\n",
1056 state, num_connected_connectors, crtc->base.id);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001057
1058 return num_connected_connectors;
1059}
1060EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
1061
1062/**
1063 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1064 * @state: atomic state
1065 *
1066 * This function should be used by legacy entry points which don't understand
1067 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
John Hunter16d78bc2e2015-04-07 19:38:50 +08001068 * the slowpath completed.
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001069 */
1070void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1071{
1072 int ret;
1073
1074retry:
1075 drm_modeset_backoff(state->acquire_ctx);
1076
1077 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
1078 state->acquire_ctx);
1079 if (ret)
1080 goto retry;
1081 ret = drm_modeset_lock_all_crtcs(state->dev,
1082 state->acquire_ctx);
1083 if (ret)
1084 goto retry;
1085}
1086EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1087
1088/**
1089 * drm_atomic_check_only - check whether a given config would work
1090 * @state: atomic configuration to check
1091 *
1092 * Note that this function can return -EDEADLK if the driver needed to acquire
1093 * more locks but encountered a deadlock. The caller must then do the usual w/w
1094 * backoff dance and restart. All other errors are fatal.
1095 *
1096 * Returns:
1097 * 0 on success, negative error code on failure.
1098 */
1099int drm_atomic_check_only(struct drm_atomic_state *state)
1100{
Rob Clark5e743732014-12-18 16:01:51 -05001101 struct drm_device *dev = state->dev;
1102 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001103 struct drm_plane *plane;
1104 struct drm_plane_state *plane_state;
1105 struct drm_crtc *crtc;
1106 struct drm_crtc_state *crtc_state;
Rob Clark5e743732014-12-18 16:01:51 -05001107 int i, ret = 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001108
Daniel Vetter17a38d92015-02-22 12:24:16 +01001109 DRM_DEBUG_ATOMIC("checking %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001110
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001111 for_each_plane_in_state(state, plane, plane_state, i) {
1112 ret = drm_atomic_plane_check(plane, plane_state);
Rob Clark5e743732014-12-18 16:01:51 -05001113 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +01001114 DRM_DEBUG_ATOMIC("[PLANE:%d] atomic core check failed\n",
1115 plane->base.id);
Rob Clark5e743732014-12-18 16:01:51 -05001116 return ret;
1117 }
1118 }
1119
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001120 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1121 ret = drm_atomic_crtc_check(crtc, crtc_state);
Rob Clark5e743732014-12-18 16:01:51 -05001122 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +01001123 DRM_DEBUG_ATOMIC("[CRTC:%d] atomic core check failed\n",
1124 crtc->base.id);
Rob Clark5e743732014-12-18 16:01:51 -05001125 return ret;
1126 }
1127 }
1128
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001129 if (config->funcs->atomic_check)
Rob Clark5e743732014-12-18 16:01:51 -05001130 ret = config->funcs->atomic_check(state->dev, state);
1131
Rob Clarkd34f20d2014-12-18 16:01:56 -05001132 if (!state->allow_modeset) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001133 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001134 if (crtc_state->mode_changed ||
1135 crtc_state->active_changed) {
Daniel Vetter17a38d92015-02-22 12:24:16 +01001136 DRM_DEBUG_ATOMIC("[CRTC:%d] requires full modeset\n",
1137 crtc->base.id);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001138 return -EINVAL;
1139 }
1140 }
1141 }
1142
Rob Clark5e743732014-12-18 16:01:51 -05001143 return ret;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001144}
1145EXPORT_SYMBOL(drm_atomic_check_only);
1146
1147/**
1148 * drm_atomic_commit - commit configuration atomically
1149 * @state: atomic configuration to check
1150 *
1151 * Note that this function can return -EDEADLK if the driver needed to acquire
1152 * more locks but encountered a deadlock. The caller must then do the usual w/w
1153 * backoff dance and restart. All other errors are fatal.
1154 *
1155 * Also note that on successful execution ownership of @state is transferred
1156 * from the caller of this function to the function itself. The caller must not
1157 * free or in any other way access @state. If the function fails then the caller
1158 * must clean up @state itself.
1159 *
1160 * Returns:
1161 * 0 on success, negative error code on failure.
1162 */
1163int drm_atomic_commit(struct drm_atomic_state *state)
1164{
1165 struct drm_mode_config *config = &state->dev->mode_config;
1166 int ret;
1167
1168 ret = drm_atomic_check_only(state);
1169 if (ret)
1170 return ret;
1171
Daniel Vetter17a38d92015-02-22 12:24:16 +01001172 DRM_DEBUG_ATOMIC("commiting %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001173
1174 return config->funcs->atomic_commit(state->dev, state, false);
1175}
1176EXPORT_SYMBOL(drm_atomic_commit);
1177
1178/**
1179 * drm_atomic_async_commit - atomic&async configuration commit
1180 * @state: atomic configuration to check
1181 *
1182 * Note that this function can return -EDEADLK if the driver needed to acquire
1183 * more locks but encountered a deadlock. The caller must then do the usual w/w
1184 * backoff dance and restart. All other errors are fatal.
1185 *
1186 * Also note that on successful execution ownership of @state is transferred
1187 * from the caller of this function to the function itself. The caller must not
1188 * free or in any other way access @state. If the function fails then the caller
1189 * must clean up @state itself.
1190 *
1191 * Returns:
1192 * 0 on success, negative error code on failure.
1193 */
1194int drm_atomic_async_commit(struct drm_atomic_state *state)
1195{
1196 struct drm_mode_config *config = &state->dev->mode_config;
1197 int ret;
1198
1199 ret = drm_atomic_check_only(state);
1200 if (ret)
1201 return ret;
1202
Daniel Vetter17a38d92015-02-22 12:24:16 +01001203 DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001204
1205 return config->funcs->atomic_commit(state->dev, state, true);
1206}
1207EXPORT_SYMBOL(drm_atomic_async_commit);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001208
1209/*
1210 * The big monstor ioctl
1211 */
1212
1213static struct drm_pending_vblank_event *create_vblank_event(
1214 struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data)
1215{
1216 struct drm_pending_vblank_event *e = NULL;
1217 unsigned long flags;
1218
1219 spin_lock_irqsave(&dev->event_lock, flags);
1220 if (file_priv->event_space < sizeof e->event) {
1221 spin_unlock_irqrestore(&dev->event_lock, flags);
1222 goto out;
1223 }
1224 file_priv->event_space -= sizeof e->event;
1225 spin_unlock_irqrestore(&dev->event_lock, flags);
1226
1227 e = kzalloc(sizeof *e, GFP_KERNEL);
1228 if (e == NULL) {
1229 spin_lock_irqsave(&dev->event_lock, flags);
1230 file_priv->event_space += sizeof e->event;
1231 spin_unlock_irqrestore(&dev->event_lock, flags);
1232 goto out;
1233 }
1234
1235 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1236 e->event.base.length = sizeof e->event;
1237 e->event.user_data = user_data;
1238 e->base.event = &e->event.base;
1239 e->base.file_priv = file_priv;
1240 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1241
1242out:
1243 return e;
1244}
1245
1246static void destroy_vblank_event(struct drm_device *dev,
1247 struct drm_file *file_priv, struct drm_pending_vblank_event *e)
1248{
1249 unsigned long flags;
1250
1251 spin_lock_irqsave(&dev->event_lock, flags);
1252 file_priv->event_space += sizeof e->event;
1253 spin_unlock_irqrestore(&dev->event_lock, flags);
1254 kfree(e);
1255}
1256
1257static int atomic_set_prop(struct drm_atomic_state *state,
1258 struct drm_mode_object *obj, struct drm_property *prop,
1259 uint64_t prop_value)
1260{
1261 struct drm_mode_object *ref;
1262 int ret;
1263
1264 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1265 return -EINVAL;
1266
1267 switch (obj->type) {
1268 case DRM_MODE_OBJECT_CONNECTOR: {
1269 struct drm_connector *connector = obj_to_connector(obj);
1270 struct drm_connector_state *connector_state;
1271
1272 connector_state = drm_atomic_get_connector_state(state, connector);
1273 if (IS_ERR(connector_state)) {
1274 ret = PTR_ERR(connector_state);
1275 break;
1276 }
1277
1278 ret = drm_atomic_connector_set_property(connector,
1279 connector_state, prop, prop_value);
1280 break;
1281 }
1282 case DRM_MODE_OBJECT_CRTC: {
1283 struct drm_crtc *crtc = obj_to_crtc(obj);
1284 struct drm_crtc_state *crtc_state;
1285
1286 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1287 if (IS_ERR(crtc_state)) {
1288 ret = PTR_ERR(crtc_state);
1289 break;
1290 }
1291
1292 ret = drm_atomic_crtc_set_property(crtc,
1293 crtc_state, prop, prop_value);
1294 break;
1295 }
1296 case DRM_MODE_OBJECT_PLANE: {
1297 struct drm_plane *plane = obj_to_plane(obj);
1298 struct drm_plane_state *plane_state;
1299
1300 plane_state = drm_atomic_get_plane_state(state, plane);
1301 if (IS_ERR(plane_state)) {
1302 ret = PTR_ERR(plane_state);
1303 break;
1304 }
1305
1306 ret = drm_atomic_plane_set_property(plane,
1307 plane_state, prop, prop_value);
1308 break;
1309 }
1310 default:
1311 ret = -EINVAL;
1312 break;
1313 }
1314
1315 drm_property_change_valid_put(prop, ref);
1316 return ret;
1317}
1318
1319int drm_mode_atomic_ioctl(struct drm_device *dev,
1320 void *data, struct drm_file *file_priv)
1321{
1322 struct drm_mode_atomic *arg = data;
1323 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1324 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1325 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1326 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1327 unsigned int copied_objs, copied_props;
1328 struct drm_atomic_state *state;
1329 struct drm_modeset_acquire_ctx ctx;
1330 struct drm_plane *plane;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001331 struct drm_crtc *crtc;
1332 struct drm_crtc_state *crtc_state;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001333 unsigned plane_mask = 0;
1334 int ret = 0;
1335 unsigned int i, j;
1336
1337 /* disallow for drivers not supporting atomic: */
1338 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1339 return -EINVAL;
1340
1341 /* disallow for userspace that has not enabled atomic cap (even
1342 * though this may be a bit overkill, since legacy userspace
1343 * wouldn't know how to call this ioctl)
1344 */
1345 if (!file_priv->atomic)
1346 return -EINVAL;
1347
1348 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1349 return -EINVAL;
1350
1351 if (arg->reserved)
1352 return -EINVAL;
1353
1354 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1355 !dev->mode_config.async_page_flip)
1356 return -EINVAL;
1357
1358 /* can't test and expect an event at the same time. */
1359 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1360 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1361 return -EINVAL;
1362
1363 drm_modeset_acquire_init(&ctx, 0);
1364
1365 state = drm_atomic_state_alloc(dev);
1366 if (!state)
1367 return -ENOMEM;
1368
1369 state->acquire_ctx = &ctx;
1370 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1371
1372retry:
1373 copied_objs = 0;
1374 copied_props = 0;
1375
1376 for (i = 0; i < arg->count_objs; i++) {
1377 uint32_t obj_id, count_props;
1378 struct drm_mode_object *obj;
1379
1380 if (get_user(obj_id, objs_ptr + copied_objs)) {
1381 ret = -EFAULT;
1382 goto fail;
1383 }
1384
1385 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
1386 if (!obj || !obj->properties) {
1387 ret = -ENOENT;
1388 goto fail;
1389 }
1390
1391 if (obj->type == DRM_MODE_OBJECT_PLANE) {
1392 plane = obj_to_plane(obj);
1393 plane_mask |= (1 << drm_plane_index(plane));
1394 plane->old_fb = plane->fb;
1395 }
1396
1397 if (get_user(count_props, count_props_ptr + copied_objs)) {
1398 ret = -EFAULT;
1399 goto fail;
1400 }
1401
1402 copied_objs++;
1403
1404 for (j = 0; j < count_props; j++) {
1405 uint32_t prop_id;
1406 uint64_t prop_value;
1407 struct drm_property *prop;
1408
1409 if (get_user(prop_id, props_ptr + copied_props)) {
1410 ret = -EFAULT;
1411 goto fail;
1412 }
1413
1414 prop = drm_property_find(dev, prop_id);
1415 if (!prop) {
1416 ret = -ENOENT;
1417 goto fail;
1418 }
1419
Guenter Roeck42c58142015-01-12 21:12:17 -08001420 if (copy_from_user(&prop_value,
1421 prop_values_ptr + copied_props,
1422 sizeof(prop_value))) {
Rob Clarkd34f20d2014-12-18 16:01:56 -05001423 ret = -EFAULT;
1424 goto fail;
1425 }
1426
1427 ret = atomic_set_prop(state, obj, prop, prop_value);
1428 if (ret)
1429 goto fail;
1430
1431 copied_props++;
1432 }
1433 }
1434
1435 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001436 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Rob Clarkd34f20d2014-12-18 16:01:56 -05001437 struct drm_pending_vblank_event *e;
1438
Rob Clarkd34f20d2014-12-18 16:01:56 -05001439 e = create_vblank_event(dev, file_priv, arg->user_data);
1440 if (!e) {
1441 ret = -ENOMEM;
1442 goto fail;
1443 }
1444
1445 crtc_state->event = e;
1446 }
1447 }
1448
1449 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1450 ret = drm_atomic_check_only(state);
1451 /* _check_only() does not free state, unlike _commit() */
1452 drm_atomic_state_free(state);
1453 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1454 ret = drm_atomic_async_commit(state);
1455 } else {
1456 ret = drm_atomic_commit(state);
1457 }
1458
1459 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1460 * locks (ie. while it is still safe to deref plane->state). We
1461 * need to do this here because the driver entry points cannot
1462 * distinguish between legacy and atomic ioctls.
1463 */
1464 drm_for_each_plane_mask(plane, dev, plane_mask) {
1465 if (ret == 0) {
1466 struct drm_framebuffer *new_fb = plane->state->fb;
1467 if (new_fb)
1468 drm_framebuffer_reference(new_fb);
1469 plane->fb = new_fb;
1470 plane->crtc = plane->state->crtc;
1471 } else {
1472 plane->old_fb = NULL;
1473 }
1474 if (plane->old_fb) {
1475 drm_framebuffer_unreference(plane->old_fb);
1476 plane->old_fb = NULL;
1477 }
1478 }
1479
1480 drm_modeset_drop_locks(&ctx);
1481 drm_modeset_acquire_fini(&ctx);
1482
1483 return ret;
1484
1485fail:
1486 if (ret == -EDEADLK)
1487 goto backoff;
1488
1489 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001490 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Rob Clarkd34f20d2014-12-18 16:01:56 -05001491 destroy_vblank_event(dev, file_priv, crtc_state->event);
1492 crtc_state->event = NULL;
1493 }
1494 }
1495
1496 drm_atomic_state_free(state);
1497
1498 drm_modeset_drop_locks(&ctx);
1499 drm_modeset_acquire_fini(&ctx);
1500
1501 return ret;
1502
1503backoff:
1504 drm_atomic_state_clear(state);
1505 drm_modeset_backoff(&ctx);
1506
1507 goto retry;
1508}