blob: 3f74193885f1ff1df95db09fa76e01174f6353b4 [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 Vetter460e8e22015-07-29 12:51:41 +0200156 /*
157 * FIXME: Async commits can race with connector unplugging and
158 * there's currently nothing that prevents cleanup up state for
159 * deleted connectors. As long as the callback doesn't look at
160 * the connector we'll be fine though, so make sure that's the
161 * case by setting all connector pointers to NULL.
162 */
163 state->connector_states[i]->connector = NULL;
164 connector->funcs->atomic_destroy_state(NULL,
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200165 state->connector_states[i]);
Ander Conselvan de Oliveira8a5c0bd2015-03-30 10:41:19 +0300166 state->connectors[i] = NULL;
Ander Conselvan de Oliveira94692442015-01-23 09:27:59 +0200167 state->connector_states[i] = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200168 }
169
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100170 for (i = 0; i < config->num_crtc; i++) {
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200171 struct drm_crtc *crtc = state->crtcs[i];
172
173 if (!crtc)
174 continue;
175
176 crtc->funcs->atomic_destroy_state(crtc,
177 state->crtc_states[i]);
Ander Conselvan de Oliveira8a5c0bd2015-03-30 10:41:19 +0300178 state->crtcs[i] = NULL;
Ander Conselvan de Oliveira94692442015-01-23 09:27:59 +0200179 state->crtc_states[i] = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200180 }
181
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100182 for (i = 0; i < config->num_total_plane; i++) {
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200183 struct drm_plane *plane = state->planes[i];
184
185 if (!plane)
186 continue;
187
188 plane->funcs->atomic_destroy_state(plane,
189 state->plane_states[i]);
Ander Conselvan de Oliveira8a5c0bd2015-03-30 10:41:19 +0300190 state->planes[i] = NULL;
Ander Conselvan de Oliveira94692442015-01-23 09:27:59 +0200191 state->plane_states[i] = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200192 }
193}
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200194EXPORT_SYMBOL(drm_atomic_state_default_clear);
195
196/**
197 * drm_atomic_state_clear - clear state object
198 * @state: atomic state
199 *
200 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
201 * all locks. So someone else could sneak in and change the current modeset
202 * configuration. Which means that all the state assembled in @state is no
203 * longer an atomic update to the current state, but to some arbitrary earlier
204 * state. Which could break assumptions the driver's ->atomic_check likely
205 * relies on.
206 *
207 * Hence we must clear all cached state and completely start over, using this
208 * function.
209 */
210void drm_atomic_state_clear(struct drm_atomic_state *state)
211{
212 struct drm_device *dev = state->dev;
213 struct drm_mode_config *config = &dev->mode_config;
214
215 if (config->funcs->atomic_state_clear)
216 config->funcs->atomic_state_clear(state);
217 else
218 drm_atomic_state_default_clear(state);
219}
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200220EXPORT_SYMBOL(drm_atomic_state_clear);
221
222/**
223 * drm_atomic_state_free - free all memory for an atomic state
224 * @state: atomic state to deallocate
225 *
226 * This frees all memory associated with an atomic state, including all the
227 * per-object state for planes, crtcs and connectors.
228 */
229void drm_atomic_state_free(struct drm_atomic_state *state)
230{
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200231 struct drm_device *dev;
232 struct drm_mode_config *config;
233
Ander Conselvan de Oliveiraa0211bb2015-03-30 14:05:43 +0300234 if (!state)
235 return;
236
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200237 dev = state->dev;
238 config = &dev->mode_config;
239
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200240 drm_atomic_state_clear(state);
241
Daniel Vetter17a38d92015-02-22 12:24:16 +0100242 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200243
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200244 if (config->funcs->atomic_state_free) {
245 config->funcs->atomic_state_free(state);
246 } else {
247 drm_atomic_state_default_release(state);
248 kfree(state);
249 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200250}
251EXPORT_SYMBOL(drm_atomic_state_free);
252
253/**
254 * drm_atomic_get_crtc_state - get crtc state
255 * @state: global atomic state object
256 * @crtc: crtc to get state object for
257 *
258 * This function returns the crtc state for the given crtc, allocating it if
259 * needed. It will also grab the relevant crtc lock to make sure that the state
260 * is consistent.
261 *
262 * Returns:
263 *
264 * Either the allocated state or the error code encoded into the pointer. When
265 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
266 * entire atomic sequence must be restarted. All other errors are fatal.
267 */
268struct drm_crtc_state *
269drm_atomic_get_crtc_state(struct drm_atomic_state *state,
270 struct drm_crtc *crtc)
271{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200272 int ret, index = drm_crtc_index(crtc);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200273 struct drm_crtc_state *crtc_state;
274
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200275 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
276 if (crtc_state)
277 return crtc_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200278
279 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
280 if (ret)
281 return ERR_PTR(ret);
282
283 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
284 if (!crtc_state)
285 return ERR_PTR(-ENOMEM);
286
287 state->crtc_states[index] = crtc_state;
288 state->crtcs[index] = crtc;
289 crtc_state->state = state;
290
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200291 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
292 crtc->base.id, crtc->name, crtc_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200293
294 return crtc_state;
295}
296EXPORT_SYMBOL(drm_atomic_get_crtc_state);
297
298/**
Daniel Stone819364d2015-05-26 14:36:48 +0100299 * drm_atomic_set_mode_for_crtc - set mode for CRTC
300 * @state: the CRTC whose incoming state to update
301 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
302 *
303 * Set a mode (originating from the kernel) on the desired CRTC state. Does
304 * not change any other state properties, including enable, active, or
305 * mode_changed.
306 *
307 * RETURNS:
308 * Zero on success, error code on failure. Cannot return -EDEADLK.
309 */
310int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
311 struct drm_display_mode *mode)
312{
Daniel Stone99cf4a22015-05-25 19:11:51 +0100313 struct drm_mode_modeinfo umode;
314
Daniel Stone819364d2015-05-26 14:36:48 +0100315 /* Early return for no change. */
316 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
317 return 0;
318
Markus Elfring5f911902015-11-06 12:03:46 +0100319 drm_property_unreference_blob(state->mode_blob);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100320 state->mode_blob = NULL;
321
Daniel Stone819364d2015-05-26 14:36:48 +0100322 if (mode) {
Daniel Stone99cf4a22015-05-25 19:11:51 +0100323 drm_mode_convert_to_umode(&umode, mode);
324 state->mode_blob =
325 drm_property_create_blob(state->crtc->dev,
326 sizeof(umode),
327 &umode);
328 if (IS_ERR(state->mode_blob))
329 return PTR_ERR(state->mode_blob);
330
Daniel Stone819364d2015-05-26 14:36:48 +0100331 drm_mode_copy(&state->mode, mode);
332 state->enable = true;
333 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
334 mode->name, state);
335 } else {
336 memset(&state->mode, 0, sizeof(state->mode));
337 state->enable = false;
338 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
339 state);
340 }
341
342 return 0;
343}
344EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
345
Daniel Stone819364d2015-05-26 14:36:48 +0100346/**
Daniel Stone955f3c32015-05-25 19:11:52 +0100347 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
348 * @state: the CRTC whose incoming state to update
349 * @blob: pointer to blob property to use for mode
350 *
351 * Set a mode (originating from a blob property) on the desired CRTC state.
352 * This function will take a reference on the blob property for the CRTC state,
353 * and release the reference held on the state's existing mode property, if any
354 * was set.
355 *
356 * RETURNS:
357 * Zero on success, error code on failure. Cannot return -EDEADLK.
358 */
359int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
360 struct drm_property_blob *blob)
361{
362 if (blob == state->mode_blob)
363 return 0;
364
Markus Elfring5f911902015-11-06 12:03:46 +0100365 drm_property_unreference_blob(state->mode_blob);
Daniel Stone955f3c32015-05-25 19:11:52 +0100366 state->mode_blob = NULL;
367
368 if (blob) {
369 if (blob->length != sizeof(struct drm_mode_modeinfo) ||
370 drm_mode_convert_umode(&state->mode,
371 (const struct drm_mode_modeinfo *)
372 blob->data))
373 return -EINVAL;
374
375 state->mode_blob = drm_property_reference_blob(blob);
376 state->enable = true;
377 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
378 state->mode.name, state);
379 } else {
380 memset(&state->mode, 0, sizeof(state->mode));
381 state->enable = false;
382 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
383 state);
384 }
385
386 return 0;
387}
388EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
389
390/**
Rob Clark40ecc692014-12-18 16:01:46 -0500391 * drm_atomic_crtc_set_property - set property on CRTC
392 * @crtc: the drm CRTC to set a property on
393 * @state: the state object to update with the new property value
394 * @property: the property to set
395 * @val: the new property value
396 *
397 * Use this instead of calling crtc->atomic_set_property directly.
398 * This function handles generic/core properties and calls out to
399 * driver's ->atomic_set_property() for driver properties. To ensure
400 * consistent behavior you must call this function rather than the
401 * driver hook directly.
402 *
403 * RETURNS:
404 * Zero on success, error code on failure
405 */
406int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
407 struct drm_crtc_state *state, struct drm_property *property,
408 uint64_t val)
409{
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100410 struct drm_device *dev = crtc->dev;
411 struct drm_mode_config *config = &dev->mode_config;
Daniel Stone955f3c32015-05-25 19:11:52 +0100412 int ret;
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100413
Daniel Stone27798362015-03-19 04:33:26 +0000414 if (property == config->prop_active)
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100415 state->active = val;
Daniel Stone955f3c32015-05-25 19:11:52 +0100416 else if (property == config->prop_mode_id) {
417 struct drm_property_blob *mode =
418 drm_property_lookup_blob(dev, val);
419 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
Markus Elfring5f911902015-11-06 12:03:46 +0100420 drm_property_unreference_blob(mode);
Daniel Stone955f3c32015-05-25 19:11:52 +0100421 return ret;
422 }
Daniel Stone27798362015-03-19 04:33:26 +0000423 else if (crtc->funcs->atomic_set_property)
Rob Clark40ecc692014-12-18 16:01:46 -0500424 return crtc->funcs->atomic_set_property(crtc, state, property, val);
Daniel Stone27798362015-03-19 04:33:26 +0000425 else
426 return -EINVAL;
427
428 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -0500429}
430EXPORT_SYMBOL(drm_atomic_crtc_set_property);
431
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100432/**
433 * drm_atomic_crtc_get_property - get property value from CRTC state
434 * @crtc: the drm CRTC to set a property on
435 * @state: the state object to get the property value from
436 * @property: the property to set
437 * @val: return location for the property value
438 *
Rob Clarkac9c9252014-12-18 16:01:47 -0500439 * This function handles generic/core properties and calls out to
440 * driver's ->atomic_get_property() for driver properties. To ensure
441 * consistent behavior you must call this function rather than the
442 * driver hook directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100443 *
444 * RETURNS:
445 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -0500446 */
Geliang Tangbf22f3b2015-09-24 03:01:03 -0700447static int
448drm_atomic_crtc_get_property(struct drm_crtc *crtc,
Rob Clarkac9c9252014-12-18 16:01:47 -0500449 const struct drm_crtc_state *state,
450 struct drm_property *property, uint64_t *val)
451{
Daniel Stone8f164ce2015-03-19 04:33:25 +0000452 struct drm_device *dev = crtc->dev;
453 struct drm_mode_config *config = &dev->mode_config;
454
455 if (property == config->prop_active)
456 *val = state->active;
Daniel Stone955f3c32015-05-25 19:11:52 +0100457 else if (property == config->prop_mode_id)
458 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
Daniel Stone8f164ce2015-03-19 04:33:25 +0000459 else if (crtc->funcs->atomic_get_property)
Rob Clarkac9c9252014-12-18 16:01:47 -0500460 return crtc->funcs->atomic_get_property(crtc, state, property, val);
Daniel Stone8f164ce2015-03-19 04:33:25 +0000461 else
462 return -EINVAL;
463
464 return 0;
Rob Clarkac9c9252014-12-18 16:01:47 -0500465}
Rob Clarkac9c9252014-12-18 16:01:47 -0500466
467/**
Rob Clark5e743732014-12-18 16:01:51 -0500468 * drm_atomic_crtc_check - check crtc state
469 * @crtc: crtc to check
470 * @state: crtc state to check
471 *
472 * Provides core sanity checks for crtc state.
473 *
474 * RETURNS:
475 * Zero on success, error code on failure
476 */
477static int drm_atomic_crtc_check(struct drm_crtc *crtc,
478 struct drm_crtc_state *state)
479{
480 /* NOTE: we explicitly don't enforce constraints such as primary
481 * layer covering entire screen, since that is something we want
482 * to allow (on hw that supports it). For hw that does not, it
483 * should be checked in driver's crtc->atomic_check() vfunc.
484 *
485 * TODO: Add generic modeset state checks once we support those.
486 */
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100487
488 if (state->active && !state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200489 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
490 crtc->base.id, crtc->name);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100491 return -EINVAL;
492 }
493
Daniel Stone99cf4a22015-05-25 19:11:51 +0100494 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
495 * as this is a kernel-internal detail that userspace should never
496 * be able to trigger. */
497 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
498 WARN_ON(state->enable && !state->mode_blob)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200499 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
500 crtc->base.id, crtc->name);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100501 return -EINVAL;
502 }
503
504 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
505 WARN_ON(!state->enable && state->mode_blob)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200506 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
507 crtc->base.id, crtc->name);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100508 return -EINVAL;
509 }
510
Daniel Vetter4cba6852015-12-08 09:49:20 +0100511 /*
512 * Reject event generation for when a CRTC is off and stays off.
513 * It wouldn't be hard to implement this, but userspace has a track
514 * record of happily burning through 100% cpu (or worse, crash) when the
515 * display pipe is suspended. To avoid all that fun just reject updates
516 * that ask for events since likely that indicates a bug in the
517 * compositor's drawing loop. This is consistent with the vblank IOCTL
518 * and legacy page_flip IOCTL which also reject service on a disabled
519 * pipe.
520 */
521 if (state->event && !state->active && !crtc->state->active) {
522 DRM_DEBUG_ATOMIC("[CRTC:%d] requesting event but off\n",
523 crtc->base.id);
524 return -EINVAL;
525 }
526
Rob Clark5e743732014-12-18 16:01:51 -0500527 return 0;
528}
529
530/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200531 * drm_atomic_get_plane_state - get plane state
532 * @state: global atomic state object
533 * @plane: plane to get state object for
534 *
535 * This function returns the plane state for the given plane, allocating it if
536 * needed. It will also grab the relevant plane lock to make sure that the state
537 * is consistent.
538 *
539 * Returns:
540 *
541 * Either the allocated state or the error code encoded into the pointer. When
542 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
543 * entire atomic sequence must be restarted. All other errors are fatal.
544 */
545struct drm_plane_state *
546drm_atomic_get_plane_state(struct drm_atomic_state *state,
547 struct drm_plane *plane)
548{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200549 int ret, index = drm_plane_index(plane);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200550 struct drm_plane_state *plane_state;
551
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200552 plane_state = drm_atomic_get_existing_plane_state(state, plane);
553 if (plane_state)
554 return plane_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200555
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100556 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200557 if (ret)
558 return ERR_PTR(ret);
559
560 plane_state = plane->funcs->atomic_duplicate_state(plane);
561 if (!plane_state)
562 return ERR_PTR(-ENOMEM);
563
564 state->plane_states[index] = plane_state;
565 state->planes[index] = plane;
566 plane_state->state = state;
567
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200568 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
569 plane->base.id, plane->name, plane_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200570
571 if (plane_state->crtc) {
572 struct drm_crtc_state *crtc_state;
573
574 crtc_state = drm_atomic_get_crtc_state(state,
575 plane_state->crtc);
576 if (IS_ERR(crtc_state))
577 return ERR_CAST(crtc_state);
578 }
579
580 return plane_state;
581}
582EXPORT_SYMBOL(drm_atomic_get_plane_state);
583
584/**
Rob Clark40ecc692014-12-18 16:01:46 -0500585 * drm_atomic_plane_set_property - set property on plane
586 * @plane: the drm plane to set a property on
587 * @state: the state object to update with the new property value
588 * @property: the property to set
589 * @val: the new property value
590 *
591 * Use this instead of calling plane->atomic_set_property directly.
592 * This function handles generic/core properties and calls out to
593 * driver's ->atomic_set_property() for driver properties. To ensure
594 * consistent behavior you must call this function rather than the
595 * driver hook directly.
596 *
597 * RETURNS:
598 * Zero on success, error code on failure
599 */
600int drm_atomic_plane_set_property(struct drm_plane *plane,
601 struct drm_plane_state *state, struct drm_property *property,
602 uint64_t val)
603{
Rob Clark6b4959f2014-12-18 16:01:53 -0500604 struct drm_device *dev = plane->dev;
605 struct drm_mode_config *config = &dev->mode_config;
606
607 if (property == config->prop_fb_id) {
608 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
609 drm_atomic_set_fb_for_plane(state, fb);
610 if (fb)
611 drm_framebuffer_unreference(fb);
612 } else if (property == config->prop_crtc_id) {
613 struct drm_crtc *crtc = drm_crtc_find(dev, val);
614 return drm_atomic_set_crtc_for_plane(state, crtc);
615 } else if (property == config->prop_crtc_x) {
616 state->crtc_x = U642I64(val);
617 } else if (property == config->prop_crtc_y) {
618 state->crtc_y = U642I64(val);
619 } else if (property == config->prop_crtc_w) {
620 state->crtc_w = val;
621 } else if (property == config->prop_crtc_h) {
622 state->crtc_h = val;
623 } else if (property == config->prop_src_x) {
624 state->src_x = val;
625 } else if (property == config->prop_src_y) {
626 state->src_y = val;
627 } else if (property == config->prop_src_w) {
628 state->src_w = val;
629 } else if (property == config->prop_src_h) {
630 state->src_h = val;
Matt Roper1da30622015-01-21 16:35:40 -0800631 } else if (property == config->rotation_property) {
632 state->rotation = val;
Rob Clark6b4959f2014-12-18 16:01:53 -0500633 } else if (plane->funcs->atomic_set_property) {
634 return plane->funcs->atomic_set_property(plane, state,
635 property, val);
636 } else {
637 return -EINVAL;
638 }
639
640 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -0500641}
642EXPORT_SYMBOL(drm_atomic_plane_set_property);
643
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100644/**
645 * drm_atomic_plane_get_property - get property value from plane state
646 * @plane: the drm plane to set a property on
647 * @state: the state object to get the property value from
648 * @property: the property to set
649 * @val: return location for the property value
650 *
Rob Clarkac9c9252014-12-18 16:01:47 -0500651 * This function handles generic/core properties and calls out to
652 * driver's ->atomic_get_property() for driver properties. To ensure
653 * consistent behavior you must call this function rather than the
654 * driver hook directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100655 *
656 * RETURNS:
657 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -0500658 */
Daniel Vettera97df1c2014-12-18 22:49:02 +0100659static int
660drm_atomic_plane_get_property(struct drm_plane *plane,
Rob Clarkac9c9252014-12-18 16:01:47 -0500661 const struct drm_plane_state *state,
662 struct drm_property *property, uint64_t *val)
663{
Rob Clark6b4959f2014-12-18 16:01:53 -0500664 struct drm_device *dev = plane->dev;
665 struct drm_mode_config *config = &dev->mode_config;
666
667 if (property == config->prop_fb_id) {
668 *val = (state->fb) ? state->fb->base.id : 0;
669 } else if (property == config->prop_crtc_id) {
670 *val = (state->crtc) ? state->crtc->base.id : 0;
671 } else if (property == config->prop_crtc_x) {
672 *val = I642U64(state->crtc_x);
673 } else if (property == config->prop_crtc_y) {
674 *val = I642U64(state->crtc_y);
675 } else if (property == config->prop_crtc_w) {
676 *val = state->crtc_w;
677 } else if (property == config->prop_crtc_h) {
678 *val = state->crtc_h;
679 } else if (property == config->prop_src_x) {
680 *val = state->src_x;
681 } else if (property == config->prop_src_y) {
682 *val = state->src_y;
683 } else if (property == config->prop_src_w) {
684 *val = state->src_w;
685 } else if (property == config->prop_src_h) {
686 *val = state->src_h;
Tvrtko Ursulin4cda09c2015-02-26 13:49:17 +0000687 } else if (property == config->rotation_property) {
688 *val = state->rotation;
Rob Clark6b4959f2014-12-18 16:01:53 -0500689 } else if (plane->funcs->atomic_get_property) {
Rob Clarkac9c9252014-12-18 16:01:47 -0500690 return plane->funcs->atomic_get_property(plane, state, property, val);
Rob Clark6b4959f2014-12-18 16:01:53 -0500691 } else {
692 return -EINVAL;
693 }
694
695 return 0;
Rob Clarkac9c9252014-12-18 16:01:47 -0500696}
Rob Clarkac9c9252014-12-18 16:01:47 -0500697
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200698static bool
699plane_switching_crtc(struct drm_atomic_state *state,
700 struct drm_plane *plane,
701 struct drm_plane_state *plane_state)
702{
703 if (!plane->state->crtc || !plane_state->crtc)
704 return false;
705
706 if (plane->state->crtc == plane_state->crtc)
707 return false;
708
709 /* This could be refined, but currently there's no helper or driver code
710 * to implement direct switching of active planes nor userspace to take
711 * advantage of more direct plane switching without the intermediate
712 * full OFF state.
713 */
714 return true;
715}
716
Rob Clarkac9c9252014-12-18 16:01:47 -0500717/**
Rob Clark5e743732014-12-18 16:01:51 -0500718 * drm_atomic_plane_check - check plane state
719 * @plane: plane to check
720 * @state: plane state to check
721 *
722 * Provides core sanity checks for plane state.
723 *
724 * RETURNS:
725 * Zero on success, error code on failure
726 */
727static int drm_atomic_plane_check(struct drm_plane *plane,
728 struct drm_plane_state *state)
729{
730 unsigned int fb_width, fb_height;
Laurent Pinchartead86102015-03-05 02:25:43 +0200731 int ret;
Rob Clark5e743732014-12-18 16:01:51 -0500732
733 /* either *both* CRTC and FB must be set, or neither */
734 if (WARN_ON(state->crtc && !state->fb)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100735 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
Rob Clark5e743732014-12-18 16:01:51 -0500736 return -EINVAL;
737 } else if (WARN_ON(state->fb && !state->crtc)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100738 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
Rob Clark5e743732014-12-18 16:01:51 -0500739 return -EINVAL;
740 }
741
742 /* if disabled, we don't care about the rest of the state: */
743 if (!state->crtc)
744 return 0;
745
746 /* Check whether this plane is usable on this CRTC */
747 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100748 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
Rob Clark5e743732014-12-18 16:01:51 -0500749 return -EINVAL;
750 }
751
752 /* Check whether this plane supports the fb pixel format. */
Laurent Pinchartead86102015-03-05 02:25:43 +0200753 ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
754 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100755 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
756 drm_get_format_name(state->fb->pixel_format));
Laurent Pinchartead86102015-03-05 02:25:43 +0200757 return ret;
Rob Clark5e743732014-12-18 16:01:51 -0500758 }
759
760 /* Give drivers some help against integer overflows */
761 if (state->crtc_w > INT_MAX ||
762 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
763 state->crtc_h > INT_MAX ||
764 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100765 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
766 state->crtc_w, state->crtc_h,
767 state->crtc_x, state->crtc_y);
Rob Clark5e743732014-12-18 16:01:51 -0500768 return -ERANGE;
769 }
770
771 fb_width = state->fb->width << 16;
772 fb_height = state->fb->height << 16;
773
774 /* Make sure source coordinates are inside the fb. */
775 if (state->src_w > fb_width ||
776 state->src_x > fb_width - state->src_w ||
777 state->src_h > fb_height ||
778 state->src_y > fb_height - state->src_h) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100779 DRM_DEBUG_ATOMIC("Invalid source coordinates "
780 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
781 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
782 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
783 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
784 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
Rob Clark5e743732014-12-18 16:01:51 -0500785 return -ENOSPC;
786 }
787
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200788 if (plane_switching_crtc(state->state, plane, state)) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200789 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
790 plane->base.id, plane->name);
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200791 return -EINVAL;
792 }
793
Rob Clark5e743732014-12-18 16:01:51 -0500794 return 0;
795}
796
797/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200798 * drm_atomic_get_connector_state - get connector state
799 * @state: global atomic state object
800 * @connector: connector to get state object for
801 *
802 * This function returns the connector state for the given connector,
803 * allocating it if needed. It will also grab the relevant connector lock to
804 * make sure that the state is consistent.
805 *
806 * Returns:
807 *
808 * Either the allocated state or the error code encoded into the pointer. When
809 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
810 * entire atomic sequence must be restarted. All other errors are fatal.
811 */
812struct drm_connector_state *
813drm_atomic_get_connector_state(struct drm_atomic_state *state,
814 struct drm_connector *connector)
815{
816 int ret, index;
817 struct drm_mode_config *config = &connector->dev->mode_config;
818 struct drm_connector_state *connector_state;
819
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100820 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
821 if (ret)
822 return ERR_PTR(ret);
823
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200824 index = drm_connector_index(connector);
825
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100826 /*
827 * Construction of atomic state updates can race with a connector
828 * hot-add which might overflow. In this case flip the table and just
829 * restart the entire ioctl - no one is fast enough to livelock a cpu
830 * with physical hotplug events anyway.
831 *
832 * Note that we only grab the indexes once we have the right lock to
833 * prevent hotplug/unplugging of connectors. So removal is no problem,
834 * at most the array is a bit too large.
835 */
836 if (index >= state->num_connector) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100837 DRM_DEBUG_ATOMIC("Hot-added connector would overflow state array, restarting\n");
Daniel Vetterfc2d2bc2014-11-20 09:53:35 +0100838 return ERR_PTR(-EAGAIN);
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100839 }
840
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200841 if (state->connector_states[index])
842 return state->connector_states[index];
843
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200844 connector_state = connector->funcs->atomic_duplicate_state(connector);
845 if (!connector_state)
846 return ERR_PTR(-ENOMEM);
847
848 state->connector_states[index] = connector_state;
849 state->connectors[index] = connector;
850 connector_state->state = state;
851
Daniel Vetter17a38d92015-02-22 12:24:16 +0100852 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
853 connector->base.id, connector_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200854
855 if (connector_state->crtc) {
856 struct drm_crtc_state *crtc_state;
857
858 crtc_state = drm_atomic_get_crtc_state(state,
859 connector_state->crtc);
860 if (IS_ERR(crtc_state))
861 return ERR_CAST(crtc_state);
862 }
863
864 return connector_state;
865}
866EXPORT_SYMBOL(drm_atomic_get_connector_state);
867
868/**
Rob Clark40ecc692014-12-18 16:01:46 -0500869 * drm_atomic_connector_set_property - set property on connector.
870 * @connector: the drm connector to set a property on
871 * @state: the state object to update with the new property value
872 * @property: the property to set
873 * @val: the new property value
874 *
875 * Use this instead of calling connector->atomic_set_property directly.
876 * This function handles generic/core properties and calls out to
877 * driver's ->atomic_set_property() for driver properties. To ensure
878 * consistent behavior you must call this function rather than the
879 * driver hook directly.
880 *
881 * RETURNS:
882 * Zero on success, error code on failure
883 */
884int drm_atomic_connector_set_property(struct drm_connector *connector,
885 struct drm_connector_state *state, struct drm_property *property,
886 uint64_t val)
887{
888 struct drm_device *dev = connector->dev;
889 struct drm_mode_config *config = &dev->mode_config;
890
Rob Clarkae16c592014-12-18 16:01:54 -0500891 if (property == config->prop_crtc_id) {
892 struct drm_crtc *crtc = drm_crtc_find(dev, val);
893 return drm_atomic_set_crtc_for_connector(state, crtc);
894 } else if (property == config->dpms_property) {
Rob Clark40ecc692014-12-18 16:01:46 -0500895 /* setting DPMS property requires special handling, which
896 * is done in legacy setprop path for us. Disallow (for
897 * now?) atomic writes to DPMS property:
898 */
899 return -EINVAL;
900 } else if (connector->funcs->atomic_set_property) {
901 return connector->funcs->atomic_set_property(connector,
902 state, property, val);
903 } else {
904 return -EINVAL;
905 }
906}
907EXPORT_SYMBOL(drm_atomic_connector_set_property);
908
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100909/**
910 * drm_atomic_connector_get_property - get property value from connector state
911 * @connector: the drm connector to set a property on
912 * @state: the state object to get the property value from
913 * @property: the property to set
914 * @val: return location for the property value
915 *
Rob Clarkac9c9252014-12-18 16:01:47 -0500916 * This function handles generic/core properties and calls out to
917 * driver's ->atomic_get_property() for driver properties. To ensure
918 * consistent behavior you must call this function rather than the
919 * driver hook directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100920 *
921 * RETURNS:
922 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -0500923 */
Daniel Vettera97df1c2014-12-18 22:49:02 +0100924static int
925drm_atomic_connector_get_property(struct drm_connector *connector,
Rob Clarkac9c9252014-12-18 16:01:47 -0500926 const struct drm_connector_state *state,
927 struct drm_property *property, uint64_t *val)
928{
929 struct drm_device *dev = connector->dev;
930 struct drm_mode_config *config = &dev->mode_config;
931
Rob Clarkae16c592014-12-18 16:01:54 -0500932 if (property == config->prop_crtc_id) {
933 *val = (state->crtc) ? state->crtc->base.id : 0;
934 } else if (property == config->dpms_property) {
Rob Clarkac9c9252014-12-18 16:01:47 -0500935 *val = connector->dpms;
936 } else if (connector->funcs->atomic_get_property) {
937 return connector->funcs->atomic_get_property(connector,
938 state, property, val);
939 } else {
940 return -EINVAL;
941 }
942
943 return 0;
944}
Rob Clarkac9c9252014-12-18 16:01:47 -0500945
Rob Clark88a48e22014-12-18 16:01:50 -0500946int drm_atomic_get_property(struct drm_mode_object *obj,
947 struct drm_property *property, uint64_t *val)
948{
949 struct drm_device *dev = property->dev;
950 int ret;
951
952 switch (obj->type) {
953 case DRM_MODE_OBJECT_CONNECTOR: {
954 struct drm_connector *connector = obj_to_connector(obj);
955 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
956 ret = drm_atomic_connector_get_property(connector,
957 connector->state, property, val);
958 break;
959 }
960 case DRM_MODE_OBJECT_CRTC: {
961 struct drm_crtc *crtc = obj_to_crtc(obj);
962 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
963 ret = drm_atomic_crtc_get_property(crtc,
964 crtc->state, property, val);
965 break;
966 }
967 case DRM_MODE_OBJECT_PLANE: {
968 struct drm_plane *plane = obj_to_plane(obj);
969 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
970 ret = drm_atomic_plane_get_property(plane,
971 plane->state, property, val);
972 break;
973 }
974 default:
975 ret = -EINVAL;
976 break;
977 }
978
979 return ret;
980}
981
982/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200983 * drm_atomic_set_crtc_for_plane - set crtc for plane
Daniel Vetter07cc0ef2014-11-27 15:49:39 +0100984 * @plane_state: the plane whose incoming state to update
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200985 * @crtc: crtc to use for the plane
986 *
987 * Changing the assigned crtc for a plane requires us to grab the lock and state
988 * for the new crtc, as needed. This function takes care of all these details
989 * besides updating the pointer in the state object itself.
990 *
991 * Returns:
992 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
993 * then the w/w mutex code has detected a deadlock and the entire atomic
994 * sequence must be restarted. All other errors are fatal.
995 */
996int
Daniel Vetter07cc0ef2014-11-27 15:49:39 +0100997drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
998 struct drm_crtc *crtc)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200999{
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001000 struct drm_plane *plane = plane_state->plane;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001001 struct drm_crtc_state *crtc_state;
1002
Rob Clark6ddd3882014-11-21 15:28:31 -05001003 if (plane_state->crtc) {
1004 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1005 plane_state->crtc);
1006 if (WARN_ON(IS_ERR(crtc_state)))
1007 return PTR_ERR(crtc_state);
1008
1009 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
1010 }
1011
1012 plane_state->crtc = crtc;
1013
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001014 if (crtc) {
1015 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1016 crtc);
1017 if (IS_ERR(crtc_state))
1018 return PTR_ERR(crtc_state);
Rob Clark6ddd3882014-11-21 15:28:31 -05001019 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001020 }
1021
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001022 if (crtc)
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001023 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
1024 plane_state, crtc->base.id, crtc->name);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001025 else
Daniel Vetter17a38d92015-02-22 12:24:16 +01001026 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
1027 plane_state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001028
1029 return 0;
1030}
1031EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
1032
1033/**
John Hunter16d78bc2e2015-04-07 19:38:50 +08001034 * drm_atomic_set_fb_for_plane - set framebuffer for plane
Daniel Vetter321ebf02014-11-04 22:57:27 +01001035 * @plane_state: atomic state object for the plane
1036 * @fb: fb to use for the plane
1037 *
1038 * Changing the assigned framebuffer for a plane requires us to grab a reference
1039 * to the new fb and drop the reference to the old fb, if there is one. This
1040 * function takes care of all these details besides updating the pointer in the
1041 * state object itself.
1042 */
1043void
1044drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1045 struct drm_framebuffer *fb)
1046{
1047 if (plane_state->fb)
1048 drm_framebuffer_unreference(plane_state->fb);
1049 if (fb)
1050 drm_framebuffer_reference(fb);
1051 plane_state->fb = fb;
1052
1053 if (fb)
Daniel Vetter17a38d92015-02-22 12:24:16 +01001054 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1055 fb->base.id, plane_state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01001056 else
Daniel Vetter17a38d92015-02-22 12:24:16 +01001057 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1058 plane_state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01001059}
1060EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1061
1062/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001063 * drm_atomic_set_crtc_for_connector - set crtc for connector
1064 * @conn_state: atomic state object for the connector
1065 * @crtc: crtc to use for the connector
1066 *
1067 * Changing the assigned crtc for a connector requires us to grab the lock and
1068 * state for the new crtc, as needed. This function takes care of all these
1069 * details besides updating the pointer in the state object itself.
1070 *
1071 * Returns:
1072 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1073 * then the w/w mutex code has detected a deadlock and the entire atomic
1074 * sequence must be restarted. All other errors are fatal.
1075 */
1076int
1077drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1078 struct drm_crtc *crtc)
1079{
1080 struct drm_crtc_state *crtc_state;
1081
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001082 if (conn_state->crtc && conn_state->crtc != crtc) {
1083 crtc_state = drm_atomic_get_existing_crtc_state(conn_state->state,
1084 conn_state->crtc);
1085
1086 crtc_state->connector_mask &=
1087 ~(1 << drm_connector_index(conn_state->connector));
1088 }
1089
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001090 if (crtc) {
1091 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1092 if (IS_ERR(crtc_state))
1093 return PTR_ERR(crtc_state);
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001094
1095 crtc_state->connector_mask |=
1096 1 << drm_connector_index(conn_state->connector);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001097 }
1098
1099 conn_state->crtc = crtc;
1100
1101 if (crtc)
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001102 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
1103 conn_state, crtc->base.id, crtc->name);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001104 else
Daniel Vetter17a38d92015-02-22 12:24:16 +01001105 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1106 conn_state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001107
1108 return 0;
1109}
1110EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1111
1112/**
1113 * drm_atomic_add_affected_connectors - add connectors for crtc
1114 * @state: atomic state
1115 * @crtc: DRM crtc
1116 *
1117 * This function walks the current configuration and adds all connectors
1118 * currently using @crtc to the atomic configuration @state. Note that this
1119 * function must acquire the connection mutex. This can potentially cause
1120 * unneeded seralization if the update is just for the planes on one crtc. Hence
1121 * drivers and helpers should only call this when really needed (e.g. when a
1122 * full modeset needs to happen due to some change).
1123 *
1124 * Returns:
1125 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1126 * then the w/w mutex code has detected a deadlock and the entire atomic
1127 * sequence must be restarted. All other errors are fatal.
1128 */
1129int
1130drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1131 struct drm_crtc *crtc)
1132{
1133 struct drm_mode_config *config = &state->dev->mode_config;
1134 struct drm_connector *connector;
1135 struct drm_connector_state *conn_state;
1136 int ret;
1137
1138 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1139 if (ret)
1140 return ret;
1141
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001142 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1143 crtc->base.id, crtc->name, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001144
1145 /*
1146 * Changed connectors are already in @state, so only need to look at the
1147 * current configuration.
1148 */
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02001149 drm_for_each_connector(connector, state->dev) {
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001150 if (connector->state->crtc != crtc)
1151 continue;
1152
1153 conn_state = drm_atomic_get_connector_state(state, connector);
1154 if (IS_ERR(conn_state))
1155 return PTR_ERR(conn_state);
1156 }
1157
1158 return 0;
1159}
1160EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1161
1162/**
Maarten Lankhorste01e9f72015-05-19 16:41:02 +02001163 * drm_atomic_add_affected_planes - add planes for crtc
1164 * @state: atomic state
1165 * @crtc: DRM crtc
1166 *
1167 * This function walks the current configuration and adds all planes
1168 * currently used by @crtc to the atomic configuration @state. This is useful
1169 * when an atomic commit also needs to check all currently enabled plane on
1170 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1171 * to avoid special code to force-enable all planes.
1172 *
1173 * Since acquiring a plane state will always also acquire the w/w mutex of the
1174 * current CRTC for that plane (if there is any) adding all the plane states for
1175 * a CRTC will not reduce parallism of atomic updates.
1176 *
1177 * Returns:
1178 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1179 * then the w/w mutex code has detected a deadlock and the entire atomic
1180 * sequence must be restarted. All other errors are fatal.
1181 */
1182int
1183drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1184 struct drm_crtc *crtc)
1185{
1186 struct drm_plane *plane;
1187
1188 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
1189
1190 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1191 struct drm_plane_state *plane_state =
1192 drm_atomic_get_plane_state(state, plane);
1193
1194 if (IS_ERR(plane_state))
1195 return PTR_ERR(plane_state);
1196 }
1197 return 0;
1198}
1199EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1200
1201/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001202 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1203 * @state: atomic state
1204 *
1205 * This function should be used by legacy entry points which don't understand
1206 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
John Hunter16d78bc2e2015-04-07 19:38:50 +08001207 * the slowpath completed.
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001208 */
1209void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1210{
1211 int ret;
1212
1213retry:
1214 drm_modeset_backoff(state->acquire_ctx);
1215
Thierry Reding06eaae42015-12-02 17:50:03 +01001216 ret = drm_modeset_lock_all_ctx(state->dev, state->acquire_ctx);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001217 if (ret)
1218 goto retry;
1219}
1220EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1221
1222/**
1223 * drm_atomic_check_only - check whether a given config would work
1224 * @state: atomic configuration to check
1225 *
1226 * Note that this function can return -EDEADLK if the driver needed to acquire
1227 * more locks but encountered a deadlock. The caller must then do the usual w/w
1228 * backoff dance and restart. All other errors are fatal.
1229 *
1230 * Returns:
1231 * 0 on success, negative error code on failure.
1232 */
1233int drm_atomic_check_only(struct drm_atomic_state *state)
1234{
Rob Clark5e743732014-12-18 16:01:51 -05001235 struct drm_device *dev = state->dev;
1236 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001237 struct drm_plane *plane;
1238 struct drm_plane_state *plane_state;
1239 struct drm_crtc *crtc;
1240 struct drm_crtc_state *crtc_state;
Rob Clark5e743732014-12-18 16:01:51 -05001241 int i, ret = 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001242
Daniel Vetter17a38d92015-02-22 12:24:16 +01001243 DRM_DEBUG_ATOMIC("checking %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001244
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001245 for_each_plane_in_state(state, plane, plane_state, i) {
1246 ret = drm_atomic_plane_check(plane, plane_state);
Rob Clark5e743732014-12-18 16:01:51 -05001247 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001248 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1249 plane->base.id, plane->name);
Rob Clark5e743732014-12-18 16:01:51 -05001250 return ret;
1251 }
1252 }
1253
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001254 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1255 ret = drm_atomic_crtc_check(crtc, crtc_state);
Rob Clark5e743732014-12-18 16:01:51 -05001256 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001257 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1258 crtc->base.id, crtc->name);
Rob Clark5e743732014-12-18 16:01:51 -05001259 return ret;
1260 }
1261 }
1262
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001263 if (config->funcs->atomic_check)
Rob Clark5e743732014-12-18 16:01:51 -05001264 ret = config->funcs->atomic_check(state->dev, state);
1265
Rob Clarkd34f20d2014-12-18 16:01:56 -05001266 if (!state->allow_modeset) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001267 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter2465ff62015-06-18 09:58:55 +02001268 if (drm_atomic_crtc_needs_modeset(crtc_state)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001269 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1270 crtc->base.id, crtc->name);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001271 return -EINVAL;
1272 }
1273 }
1274 }
1275
Rob Clark5e743732014-12-18 16:01:51 -05001276 return ret;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001277}
1278EXPORT_SYMBOL(drm_atomic_check_only);
1279
1280/**
1281 * drm_atomic_commit - commit configuration atomically
1282 * @state: atomic configuration to check
1283 *
1284 * Note that this function can return -EDEADLK if the driver needed to acquire
1285 * more locks but encountered a deadlock. The caller must then do the usual w/w
1286 * backoff dance and restart. All other errors are fatal.
1287 *
1288 * Also note that on successful execution ownership of @state is transferred
1289 * from the caller of this function to the function itself. The caller must not
1290 * free or in any other way access @state. If the function fails then the caller
1291 * must clean up @state itself.
1292 *
1293 * Returns:
1294 * 0 on success, negative error code on failure.
1295 */
1296int drm_atomic_commit(struct drm_atomic_state *state)
1297{
1298 struct drm_mode_config *config = &state->dev->mode_config;
1299 int ret;
1300
1301 ret = drm_atomic_check_only(state);
1302 if (ret)
1303 return ret;
1304
Daniel Vetter17a38d92015-02-22 12:24:16 +01001305 DRM_DEBUG_ATOMIC("commiting %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001306
1307 return config->funcs->atomic_commit(state->dev, state, false);
1308}
1309EXPORT_SYMBOL(drm_atomic_commit);
1310
1311/**
1312 * drm_atomic_async_commit - atomic&async configuration commit
1313 * @state: atomic configuration to check
1314 *
1315 * Note that this function can return -EDEADLK if the driver needed to acquire
1316 * more locks but encountered a deadlock. The caller must then do the usual w/w
1317 * backoff dance and restart. All other errors are fatal.
1318 *
1319 * Also note that on successful execution ownership of @state is transferred
1320 * from the caller of this function to the function itself. The caller must not
1321 * free or in any other way access @state. If the function fails then the caller
1322 * must clean up @state itself.
1323 *
1324 * Returns:
1325 * 0 on success, negative error code on failure.
1326 */
1327int drm_atomic_async_commit(struct drm_atomic_state *state)
1328{
1329 struct drm_mode_config *config = &state->dev->mode_config;
1330 int ret;
1331
1332 ret = drm_atomic_check_only(state);
1333 if (ret)
1334 return ret;
1335
Daniel Vetter17a38d92015-02-22 12:24:16 +01001336 DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001337
1338 return config->funcs->atomic_commit(state->dev, state, true);
1339}
1340EXPORT_SYMBOL(drm_atomic_async_commit);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001341
1342/*
1343 * The big monstor ioctl
1344 */
1345
1346static struct drm_pending_vblank_event *create_vblank_event(
1347 struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data)
1348{
1349 struct drm_pending_vblank_event *e = NULL;
1350 unsigned long flags;
1351
1352 spin_lock_irqsave(&dev->event_lock, flags);
1353 if (file_priv->event_space < sizeof e->event) {
1354 spin_unlock_irqrestore(&dev->event_lock, flags);
1355 goto out;
1356 }
1357 file_priv->event_space -= sizeof e->event;
1358 spin_unlock_irqrestore(&dev->event_lock, flags);
1359
1360 e = kzalloc(sizeof *e, GFP_KERNEL);
1361 if (e == NULL) {
1362 spin_lock_irqsave(&dev->event_lock, flags);
1363 file_priv->event_space += sizeof e->event;
1364 spin_unlock_irqrestore(&dev->event_lock, flags);
1365 goto out;
1366 }
1367
1368 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1369 e->event.base.length = sizeof e->event;
1370 e->event.user_data = user_data;
1371 e->base.event = &e->event.base;
1372 e->base.file_priv = file_priv;
1373 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1374
1375out:
1376 return e;
1377}
1378
1379static void destroy_vblank_event(struct drm_device *dev,
1380 struct drm_file *file_priv, struct drm_pending_vblank_event *e)
1381{
1382 unsigned long flags;
1383
1384 spin_lock_irqsave(&dev->event_lock, flags);
1385 file_priv->event_space += sizeof e->event;
1386 spin_unlock_irqrestore(&dev->event_lock, flags);
1387 kfree(e);
1388}
1389
1390static int atomic_set_prop(struct drm_atomic_state *state,
1391 struct drm_mode_object *obj, struct drm_property *prop,
1392 uint64_t prop_value)
1393{
1394 struct drm_mode_object *ref;
1395 int ret;
1396
1397 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1398 return -EINVAL;
1399
1400 switch (obj->type) {
1401 case DRM_MODE_OBJECT_CONNECTOR: {
1402 struct drm_connector *connector = obj_to_connector(obj);
1403 struct drm_connector_state *connector_state;
1404
1405 connector_state = drm_atomic_get_connector_state(state, connector);
1406 if (IS_ERR(connector_state)) {
1407 ret = PTR_ERR(connector_state);
1408 break;
1409 }
1410
1411 ret = drm_atomic_connector_set_property(connector,
1412 connector_state, prop, prop_value);
1413 break;
1414 }
1415 case DRM_MODE_OBJECT_CRTC: {
1416 struct drm_crtc *crtc = obj_to_crtc(obj);
1417 struct drm_crtc_state *crtc_state;
1418
1419 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1420 if (IS_ERR(crtc_state)) {
1421 ret = PTR_ERR(crtc_state);
1422 break;
1423 }
1424
1425 ret = drm_atomic_crtc_set_property(crtc,
1426 crtc_state, prop, prop_value);
1427 break;
1428 }
1429 case DRM_MODE_OBJECT_PLANE: {
1430 struct drm_plane *plane = obj_to_plane(obj);
1431 struct drm_plane_state *plane_state;
1432
1433 plane_state = drm_atomic_get_plane_state(state, plane);
1434 if (IS_ERR(plane_state)) {
1435 ret = PTR_ERR(plane_state);
1436 break;
1437 }
1438
1439 ret = drm_atomic_plane_set_property(plane,
1440 plane_state, prop, prop_value);
1441 break;
1442 }
1443 default:
1444 ret = -EINVAL;
1445 break;
1446 }
1447
1448 drm_property_change_valid_put(prop, ref);
1449 return ret;
1450}
1451
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001452/**
Maarten Lankhorst9744bf42015-11-24 10:34:34 +01001453 * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001454 *
1455 * @dev: drm device to check.
1456 * @plane_mask: plane mask for planes that were updated.
1457 * @ret: return value, can be -EDEADLK for a retry.
1458 *
1459 * Before doing an update plane->old_fb is set to plane->fb,
1460 * but before dropping the locks old_fb needs to be set to NULL
1461 * and plane->fb updated. This is a common operation for each
1462 * atomic update, so this call is split off as a helper.
1463 */
1464void drm_atomic_clean_old_fb(struct drm_device *dev,
1465 unsigned plane_mask,
1466 int ret)
1467{
1468 struct drm_plane *plane;
1469
1470 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1471 * locks (ie. while it is still safe to deref plane->state). We
1472 * need to do this here because the driver entry points cannot
1473 * distinguish between legacy and atomic ioctls.
1474 */
1475 drm_for_each_plane_mask(plane, dev, plane_mask) {
1476 if (ret == 0) {
1477 struct drm_framebuffer *new_fb = plane->state->fb;
1478 if (new_fb)
1479 drm_framebuffer_reference(new_fb);
1480 plane->fb = new_fb;
1481 plane->crtc = plane->state->crtc;
1482
1483 if (plane->old_fb)
1484 drm_framebuffer_unreference(plane->old_fb);
1485 }
1486 plane->old_fb = NULL;
1487 }
1488}
1489EXPORT_SYMBOL(drm_atomic_clean_old_fb);
1490
Rob Clarkd34f20d2014-12-18 16:01:56 -05001491int drm_mode_atomic_ioctl(struct drm_device *dev,
1492 void *data, struct drm_file *file_priv)
1493{
1494 struct drm_mode_atomic *arg = data;
1495 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1496 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1497 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1498 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1499 unsigned int copied_objs, copied_props;
1500 struct drm_atomic_state *state;
1501 struct drm_modeset_acquire_ctx ctx;
1502 struct drm_plane *plane;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001503 struct drm_crtc *crtc;
1504 struct drm_crtc_state *crtc_state;
Maarten Lankhorst45723722015-11-11 11:29:08 +01001505 unsigned plane_mask;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001506 int ret = 0;
1507 unsigned int i, j;
1508
1509 /* disallow for drivers not supporting atomic: */
1510 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1511 return -EINVAL;
1512
1513 /* disallow for userspace that has not enabled atomic cap (even
1514 * though this may be a bit overkill, since legacy userspace
1515 * wouldn't know how to call this ioctl)
1516 */
1517 if (!file_priv->atomic)
1518 return -EINVAL;
1519
1520 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1521 return -EINVAL;
1522
1523 if (arg->reserved)
1524 return -EINVAL;
1525
1526 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1527 !dev->mode_config.async_page_flip)
1528 return -EINVAL;
1529
1530 /* can't test and expect an event at the same time. */
1531 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1532 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1533 return -EINVAL;
1534
1535 drm_modeset_acquire_init(&ctx, 0);
1536
1537 state = drm_atomic_state_alloc(dev);
1538 if (!state)
1539 return -ENOMEM;
1540
1541 state->acquire_ctx = &ctx;
1542 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1543
1544retry:
Maarten Lankhorst45723722015-11-11 11:29:08 +01001545 plane_mask = 0;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001546 copied_objs = 0;
1547 copied_props = 0;
1548
1549 for (i = 0; i < arg->count_objs; i++) {
1550 uint32_t obj_id, count_props;
1551 struct drm_mode_object *obj;
1552
1553 if (get_user(obj_id, objs_ptr + copied_objs)) {
1554 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001555 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001556 }
1557
1558 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
1559 if (!obj || !obj->properties) {
1560 ret = -ENOENT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001561 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001562 }
1563
Rob Clarkd34f20d2014-12-18 16:01:56 -05001564 if (get_user(count_props, count_props_ptr + copied_objs)) {
1565 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001566 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001567 }
1568
1569 copied_objs++;
1570
1571 for (j = 0; j < count_props; j++) {
1572 uint32_t prop_id;
1573 uint64_t prop_value;
1574 struct drm_property *prop;
1575
1576 if (get_user(prop_id, props_ptr + copied_props)) {
1577 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001578 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001579 }
1580
1581 prop = drm_property_find(dev, prop_id);
1582 if (!prop) {
1583 ret = -ENOENT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001584 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001585 }
1586
Guenter Roeck42c58142015-01-12 21:12:17 -08001587 if (copy_from_user(&prop_value,
1588 prop_values_ptr + copied_props,
1589 sizeof(prop_value))) {
Rob Clarkd34f20d2014-12-18 16:01:56 -05001590 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001591 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001592 }
1593
1594 ret = atomic_set_prop(state, obj, prop, prop_value);
1595 if (ret)
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001596 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001597
1598 copied_props++;
1599 }
Maarten Lankhorsta9cc54e2015-06-24 08:59:24 +02001600
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02001601 if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
1602 !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
Maarten Lankhorsta9cc54e2015-06-24 08:59:24 +02001603 plane = obj_to_plane(obj);
1604 plane_mask |= (1 << drm_plane_index(plane));
1605 plane->old_fb = plane->fb;
1606 }
Rob Clarkd34f20d2014-12-18 16:01:56 -05001607 }
1608
1609 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001610 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Rob Clarkd34f20d2014-12-18 16:01:56 -05001611 struct drm_pending_vblank_event *e;
1612
Rob Clarkd34f20d2014-12-18 16:01:56 -05001613 e = create_vblank_event(dev, file_priv, arg->user_data);
1614 if (!e) {
1615 ret = -ENOMEM;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001616 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001617 }
1618
1619 crtc_state->event = e;
1620 }
1621 }
1622
1623 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02001624 /*
1625 * Unlike commit, check_only does not clean up state.
1626 * Below we call drm_atomic_state_free for it.
1627 */
Rob Clarkd34f20d2014-12-18 16:01:56 -05001628 ret = drm_atomic_check_only(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001629 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1630 ret = drm_atomic_async_commit(state);
1631 } else {
1632 ret = drm_atomic_commit(state);
1633 }
1634
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001635out:
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001636 drm_atomic_clean_old_fb(dev, plane_mask, ret);
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001637
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02001638 if (ret && arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1639 /*
1640 * TEST_ONLY and PAGE_FLIP_EVENT are mutually exclusive,
1641 * if they weren't, this code should be called on success
1642 * for TEST_ONLY too.
1643 */
1644
1645 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1646 if (!crtc_state->event)
1647 continue;
1648
1649 destroy_vblank_event(dev, file_priv,
1650 crtc_state->event);
1651 }
1652 }
1653
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001654 if (ret == -EDEADLK) {
1655 drm_atomic_state_clear(state);
1656 drm_modeset_backoff(&ctx);
1657 goto retry;
1658 }
1659
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02001660 if (ret || arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001661 drm_atomic_state_free(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001662
1663 drm_modeset_drop_locks(&ctx);
1664 drm_modeset_acquire_fini(&ctx);
1665
1666 return ret;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001667}