blob: 674b2e490aa969f1d06b6fd6e15cc99f6a0c0346 [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>
Lionel Landwerlin5488dc12016-02-26 17:05:00 +000031#include <drm/drm_mode.h>
Daniel Vettercc4ceb42014-07-25 21:30:38 +020032#include <drm/drm_plane_helper.h>
33
Thierry Redingbe35f942016-04-28 15:19:56 +020034#include "drm_crtc_internal.h"
35
Maarten Lankhorst036ef572015-05-18 10:06:40 +020036/**
37 * drm_atomic_state_default_release -
38 * release memory initialized by drm_atomic_state_init
39 * @state: atomic state
40 *
41 * Free all the memory allocated by drm_atomic_state_init.
42 * This is useful for drivers that subclass the atomic state.
43 */
44void drm_atomic_state_default_release(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +020045{
46 kfree(state->connectors);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020047 kfree(state->crtcs);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020048 kfree(state->planes);
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 Vettercc4ceb42014-07-25 21:30:38 +020068 state->crtcs = kcalloc(dev->mode_config.num_crtc,
69 sizeof(*state->crtcs), GFP_KERNEL);
70 if (!state->crtcs)
71 goto fail;
Daniel Vettercc4ceb42014-07-25 21:30:38 +020072 state->planes = kcalloc(dev->mode_config.num_total_plane,
73 sizeof(*state->planes), GFP_KERNEL);
74 if (!state->planes)
75 goto fail;
Daniel Vettercc4ceb42014-07-25 21:30:38 +020076
77 state->dev = dev;
78
Maarten Lankhorst036ef572015-05-18 10:06:40 +020079 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020080
Maarten Lankhorst036ef572015-05-18 10:06:40 +020081 return 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +020082fail:
Maarten Lankhorst036ef572015-05-18 10:06:40 +020083 drm_atomic_state_default_release(state);
84 return -ENOMEM;
85}
86EXPORT_SYMBOL(drm_atomic_state_init);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020087
Maarten Lankhorst036ef572015-05-18 10:06:40 +020088/**
89 * drm_atomic_state_alloc - allocate atomic state
90 * @dev: DRM device
91 *
92 * This allocates an empty atomic state to track updates.
93 */
94struct drm_atomic_state *
95drm_atomic_state_alloc(struct drm_device *dev)
96{
97 struct drm_mode_config *config = &dev->mode_config;
98 struct drm_atomic_state *state;
99
100 if (!config->funcs->atomic_state_alloc) {
101 state = kzalloc(sizeof(*state), GFP_KERNEL);
102 if (!state)
103 return NULL;
104 if (drm_atomic_state_init(dev, state) < 0) {
105 kfree(state);
106 return NULL;
107 }
108 return state;
109 }
110
111 return config->funcs->atomic_state_alloc(dev);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200112}
113EXPORT_SYMBOL(drm_atomic_state_alloc);
114
115/**
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200116 * drm_atomic_state_default_clear - clear base atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200117 * @state: atomic state
118 *
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200119 * Default implementation for clearing atomic state.
120 * This is useful for drivers that subclass the atomic state.
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200121 */
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200122void drm_atomic_state_default_clear(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200123{
124 struct drm_device *dev = state->dev;
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100125 struct drm_mode_config *config = &dev->mode_config;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200126 int i;
127
Daniel Vetter17a38d92015-02-22 12:24:16 +0100128 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200129
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100130 for (i = 0; i < state->num_connector; i++) {
Daniel Vetter63e83c12016-06-02 00:06:32 +0200131 struct drm_connector *connector = state->connectors[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200132
133 if (!connector)
134 continue;
135
Dave Airlied2307de2016-04-27 11:27:39 +1000136 connector->funcs->atomic_destroy_state(connector,
Daniel Vetter63e83c12016-06-02 00:06:32 +0200137 state->connectors[i].state);
138 state->connectors[i].ptr = NULL;
139 state->connectors[i].state = NULL;
Dave Airlieb164d312016-04-27 11:10:09 +1000140 drm_connector_unreference(connector);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200141 }
142
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100143 for (i = 0; i < config->num_crtc; i++) {
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200144 struct drm_crtc *crtc = state->crtcs[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200145
146 if (!crtc)
147 continue;
148
149 crtc->funcs->atomic_destroy_state(crtc,
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200150 state->crtcs[i].state);
151 state->crtcs[i].ptr = NULL;
152 state->crtcs[i].state = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200153 }
154
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100155 for (i = 0; i < config->num_total_plane; i++) {
Daniel Vetterb8b53422016-06-02 00:06:33 +0200156 struct drm_plane *plane = state->planes[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200157
158 if (!plane)
159 continue;
160
161 plane->funcs->atomic_destroy_state(plane,
Daniel Vetterb8b53422016-06-02 00:06:33 +0200162 state->planes[i].state);
163 state->planes[i].ptr = NULL;
164 state->planes[i].state = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200165 }
166}
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200167EXPORT_SYMBOL(drm_atomic_state_default_clear);
168
169/**
170 * drm_atomic_state_clear - clear state object
171 * @state: atomic state
172 *
173 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
174 * all locks. So someone else could sneak in and change the current modeset
175 * configuration. Which means that all the state assembled in @state is no
176 * longer an atomic update to the current state, but to some arbitrary earlier
177 * state. Which could break assumptions the driver's ->atomic_check likely
178 * relies on.
179 *
180 * Hence we must clear all cached state and completely start over, using this
181 * function.
182 */
183void drm_atomic_state_clear(struct drm_atomic_state *state)
184{
185 struct drm_device *dev = state->dev;
186 struct drm_mode_config *config = &dev->mode_config;
187
188 if (config->funcs->atomic_state_clear)
189 config->funcs->atomic_state_clear(state);
190 else
191 drm_atomic_state_default_clear(state);
192}
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200193EXPORT_SYMBOL(drm_atomic_state_clear);
194
195/**
196 * drm_atomic_state_free - free all memory for an atomic state
197 * @state: atomic state to deallocate
198 *
199 * This frees all memory associated with an atomic state, including all the
200 * per-object state for planes, crtcs and connectors.
201 */
202void drm_atomic_state_free(struct drm_atomic_state *state)
203{
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200204 struct drm_device *dev;
205 struct drm_mode_config *config;
206
Ander Conselvan de Oliveiraa0211bb2015-03-30 14:05:43 +0300207 if (!state)
208 return;
209
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200210 dev = state->dev;
211 config = &dev->mode_config;
212
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200213 drm_atomic_state_clear(state);
214
Daniel Vetter17a38d92015-02-22 12:24:16 +0100215 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200216
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200217 if (config->funcs->atomic_state_free) {
218 config->funcs->atomic_state_free(state);
219 } else {
220 drm_atomic_state_default_release(state);
221 kfree(state);
222 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200223}
224EXPORT_SYMBOL(drm_atomic_state_free);
225
226/**
227 * drm_atomic_get_crtc_state - get crtc state
228 * @state: global atomic state object
229 * @crtc: crtc to get state object for
230 *
231 * This function returns the crtc state for the given crtc, allocating it if
232 * needed. It will also grab the relevant crtc lock to make sure that the state
233 * is consistent.
234 *
235 * Returns:
236 *
237 * Either the allocated state or the error code encoded into the pointer. When
238 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
239 * entire atomic sequence must be restarted. All other errors are fatal.
240 */
241struct drm_crtc_state *
242drm_atomic_get_crtc_state(struct drm_atomic_state *state,
243 struct drm_crtc *crtc)
244{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200245 int ret, index = drm_crtc_index(crtc);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200246 struct drm_crtc_state *crtc_state;
247
Maarten Lankhorst7f4eaa82016-05-03 11:12:31 +0200248 WARN_ON(!state->acquire_ctx);
249
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200250 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
251 if (crtc_state)
252 return crtc_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200253
254 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
255 if (ret)
256 return ERR_PTR(ret);
257
258 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
259 if (!crtc_state)
260 return ERR_PTR(-ENOMEM);
261
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200262 state->crtcs[index].state = crtc_state;
263 state->crtcs[index].ptr = crtc;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200264 crtc_state->state = state;
265
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200266 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
267 crtc->base.id, crtc->name, crtc_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200268
269 return crtc_state;
270}
271EXPORT_SYMBOL(drm_atomic_get_crtc_state);
272
273/**
Daniel Stone819364d2015-05-26 14:36:48 +0100274 * drm_atomic_set_mode_for_crtc - set mode for CRTC
275 * @state: the CRTC whose incoming state to update
276 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
277 *
278 * Set a mode (originating from the kernel) on the desired CRTC state. Does
279 * not change any other state properties, including enable, active, or
280 * mode_changed.
281 *
282 * RETURNS:
283 * Zero on success, error code on failure. Cannot return -EDEADLK.
284 */
285int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
286 struct drm_display_mode *mode)
287{
Daniel Stone99cf4a22015-05-25 19:11:51 +0100288 struct drm_mode_modeinfo umode;
289
Daniel Stone819364d2015-05-26 14:36:48 +0100290 /* Early return for no change. */
291 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
292 return 0;
293
Markus Elfring5f911902015-11-06 12:03:46 +0100294 drm_property_unreference_blob(state->mode_blob);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100295 state->mode_blob = NULL;
296
Daniel Stone819364d2015-05-26 14:36:48 +0100297 if (mode) {
Daniel Stone99cf4a22015-05-25 19:11:51 +0100298 drm_mode_convert_to_umode(&umode, mode);
299 state->mode_blob =
300 drm_property_create_blob(state->crtc->dev,
301 sizeof(umode),
302 &umode);
303 if (IS_ERR(state->mode_blob))
304 return PTR_ERR(state->mode_blob);
305
Daniel Stone819364d2015-05-26 14:36:48 +0100306 drm_mode_copy(&state->mode, mode);
307 state->enable = true;
308 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
309 mode->name, state);
310 } else {
311 memset(&state->mode, 0, sizeof(state->mode));
312 state->enable = false;
313 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
314 state);
315 }
316
317 return 0;
318}
319EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
320
Daniel Stone819364d2015-05-26 14:36:48 +0100321/**
Daniel Stone955f3c32015-05-25 19:11:52 +0100322 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
323 * @state: the CRTC whose incoming state to update
324 * @blob: pointer to blob property to use for mode
325 *
326 * Set a mode (originating from a blob property) on the desired CRTC state.
327 * This function will take a reference on the blob property for the CRTC state,
328 * and release the reference held on the state's existing mode property, if any
329 * was set.
330 *
331 * RETURNS:
332 * Zero on success, error code on failure. Cannot return -EDEADLK.
333 */
334int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
335 struct drm_property_blob *blob)
336{
337 if (blob == state->mode_blob)
338 return 0;
339
Markus Elfring5f911902015-11-06 12:03:46 +0100340 drm_property_unreference_blob(state->mode_blob);
Daniel Stone955f3c32015-05-25 19:11:52 +0100341 state->mode_blob = NULL;
342
343 if (blob) {
344 if (blob->length != sizeof(struct drm_mode_modeinfo) ||
345 drm_mode_convert_umode(&state->mode,
346 (const struct drm_mode_modeinfo *)
347 blob->data))
348 return -EINVAL;
349
350 state->mode_blob = drm_property_reference_blob(blob);
351 state->enable = true;
352 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
353 state->mode.name, state);
354 } else {
355 memset(&state->mode, 0, sizeof(state->mode));
356 state->enable = false;
357 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
358 state);
359 }
360
361 return 0;
362}
363EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
364
365/**
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000366 * drm_atomic_replace_property_blob - replace a blob property
367 * @blob: a pointer to the member blob to be replaced
368 * @new_blob: the new blob to replace with
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000369 * @replaced: whether the blob has been replaced
370 *
371 * RETURNS:
372 * Zero on success, error code on failure
373 */
374static void
375drm_atomic_replace_property_blob(struct drm_property_blob **blob,
376 struct drm_property_blob *new_blob,
377 bool *replaced)
378{
379 struct drm_property_blob *old_blob = *blob;
380
381 if (old_blob == new_blob)
382 return;
383
384 if (old_blob)
385 drm_property_unreference_blob(old_blob);
386 if (new_blob)
387 drm_property_reference_blob(new_blob);
388 *blob = new_blob;
389 *replaced = true;
390
391 return;
392}
393
394static int
395drm_atomic_replace_property_blob_from_id(struct drm_crtc *crtc,
396 struct drm_property_blob **blob,
397 uint64_t blob_id,
398 ssize_t expected_size,
399 bool *replaced)
400{
401 struct drm_device *dev = crtc->dev;
402 struct drm_property_blob *new_blob = NULL;
403
404 if (blob_id != 0) {
405 new_blob = drm_property_lookup_blob(dev, blob_id);
406 if (new_blob == NULL)
407 return -EINVAL;
408 if (expected_size > 0 && expected_size != new_blob->length)
409 return -EINVAL;
410 }
411
412 drm_atomic_replace_property_blob(blob, new_blob, replaced);
413
414 return 0;
415}
416
417/**
Rob Clark40ecc692014-12-18 16:01:46 -0500418 * drm_atomic_crtc_set_property - set property on CRTC
419 * @crtc: the drm CRTC to set a property on
420 * @state: the state object to update with the new property value
421 * @property: the property to set
422 * @val: the new property value
423 *
424 * Use this instead of calling crtc->atomic_set_property directly.
425 * This function handles generic/core properties and calls out to
426 * driver's ->atomic_set_property() for driver properties. To ensure
427 * consistent behavior you must call this function rather than the
428 * driver hook directly.
429 *
430 * RETURNS:
431 * Zero on success, error code on failure
432 */
433int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
434 struct drm_crtc_state *state, struct drm_property *property,
435 uint64_t val)
436{
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100437 struct drm_device *dev = crtc->dev;
438 struct drm_mode_config *config = &dev->mode_config;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000439 bool replaced = false;
Daniel Stone955f3c32015-05-25 19:11:52 +0100440 int ret;
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100441
Daniel Stone27798362015-03-19 04:33:26 +0000442 if (property == config->prop_active)
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100443 state->active = val;
Daniel Stone955f3c32015-05-25 19:11:52 +0100444 else if (property == config->prop_mode_id) {
445 struct drm_property_blob *mode =
446 drm_property_lookup_blob(dev, val);
447 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
Markus Elfring5f911902015-11-06 12:03:46 +0100448 drm_property_unreference_blob(mode);
Daniel Stone955f3c32015-05-25 19:11:52 +0100449 return ret;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000450 } else if (property == config->degamma_lut_property) {
451 ret = drm_atomic_replace_property_blob_from_id(crtc,
452 &state->degamma_lut,
453 val,
454 -1,
455 &replaced);
456 state->color_mgmt_changed = replaced;
457 return ret;
458 } else if (property == config->ctm_property) {
459 ret = drm_atomic_replace_property_blob_from_id(crtc,
460 &state->ctm,
461 val,
462 sizeof(struct drm_color_ctm),
463 &replaced);
464 state->color_mgmt_changed = replaced;
465 return ret;
466 } else if (property == config->gamma_lut_property) {
467 ret = drm_atomic_replace_property_blob_from_id(crtc,
468 &state->gamma_lut,
469 val,
470 -1,
471 &replaced);
472 state->color_mgmt_changed = replaced;
473 return ret;
474 } else if (crtc->funcs->atomic_set_property)
Rob Clark40ecc692014-12-18 16:01:46 -0500475 return crtc->funcs->atomic_set_property(crtc, state, property, val);
Daniel Stone27798362015-03-19 04:33:26 +0000476 else
477 return -EINVAL;
478
479 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -0500480}
481EXPORT_SYMBOL(drm_atomic_crtc_set_property);
482
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100483/**
484 * drm_atomic_crtc_get_property - get property value from CRTC state
485 * @crtc: the drm CRTC to set a property on
486 * @state: the state object to get the property value from
487 * @property: the property to set
488 * @val: return location for the property value
489 *
Rob Clarkac9c9252014-12-18 16:01:47 -0500490 * This function handles generic/core properties and calls out to
491 * driver's ->atomic_get_property() for driver properties. To ensure
492 * consistent behavior you must call this function rather than the
493 * driver hook directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100494 *
495 * RETURNS:
496 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -0500497 */
Geliang Tangbf22f3b2015-09-24 03:01:03 -0700498static int
499drm_atomic_crtc_get_property(struct drm_crtc *crtc,
Rob Clarkac9c9252014-12-18 16:01:47 -0500500 const struct drm_crtc_state *state,
501 struct drm_property *property, uint64_t *val)
502{
Daniel Stone8f164ce2015-03-19 04:33:25 +0000503 struct drm_device *dev = crtc->dev;
504 struct drm_mode_config *config = &dev->mode_config;
505
506 if (property == config->prop_active)
507 *val = state->active;
Daniel Stone955f3c32015-05-25 19:11:52 +0100508 else if (property == config->prop_mode_id)
509 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000510 else if (property == config->degamma_lut_property)
511 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
512 else if (property == config->ctm_property)
513 *val = (state->ctm) ? state->ctm->base.id : 0;
514 else if (property == config->gamma_lut_property)
515 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
Daniel Stone8f164ce2015-03-19 04:33:25 +0000516 else if (crtc->funcs->atomic_get_property)
Rob Clarkac9c9252014-12-18 16:01:47 -0500517 return crtc->funcs->atomic_get_property(crtc, state, property, val);
Daniel Stone8f164ce2015-03-19 04:33:25 +0000518 else
519 return -EINVAL;
520
521 return 0;
Rob Clarkac9c9252014-12-18 16:01:47 -0500522}
Rob Clarkac9c9252014-12-18 16:01:47 -0500523
524/**
Rob Clark5e743732014-12-18 16:01:51 -0500525 * drm_atomic_crtc_check - check crtc state
526 * @crtc: crtc to check
527 * @state: crtc state to check
528 *
529 * Provides core sanity checks for crtc state.
530 *
531 * RETURNS:
532 * Zero on success, error code on failure
533 */
534static int drm_atomic_crtc_check(struct drm_crtc *crtc,
535 struct drm_crtc_state *state)
536{
537 /* NOTE: we explicitly don't enforce constraints such as primary
538 * layer covering entire screen, since that is something we want
539 * to allow (on hw that supports it). For hw that does not, it
540 * should be checked in driver's crtc->atomic_check() vfunc.
541 *
542 * TODO: Add generic modeset state checks once we support those.
543 */
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100544
545 if (state->active && !state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200546 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
547 crtc->base.id, crtc->name);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100548 return -EINVAL;
549 }
550
Daniel Stone99cf4a22015-05-25 19:11:51 +0100551 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
552 * as this is a kernel-internal detail that userspace should never
553 * be able to trigger. */
554 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
555 WARN_ON(state->enable && !state->mode_blob)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200556 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
557 crtc->base.id, crtc->name);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100558 return -EINVAL;
559 }
560
561 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
562 WARN_ON(!state->enable && state->mode_blob)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200563 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
564 crtc->base.id, crtc->name);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100565 return -EINVAL;
566 }
567
Daniel Vetter4cba6852015-12-08 09:49:20 +0100568 /*
569 * Reject event generation for when a CRTC is off and stays off.
570 * It wouldn't be hard to implement this, but userspace has a track
571 * record of happily burning through 100% cpu (or worse, crash) when the
572 * display pipe is suspended. To avoid all that fun just reject updates
573 * that ask for events since likely that indicates a bug in the
574 * compositor's drawing loop. This is consistent with the vblank IOCTL
575 * and legacy page_flip IOCTL which also reject service on a disabled
576 * pipe.
577 */
578 if (state->event && !state->active && !crtc->state->active) {
579 DRM_DEBUG_ATOMIC("[CRTC:%d] requesting event but off\n",
580 crtc->base.id);
581 return -EINVAL;
582 }
583
Rob Clark5e743732014-12-18 16:01:51 -0500584 return 0;
585}
586
587/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200588 * drm_atomic_get_plane_state - get plane state
589 * @state: global atomic state object
590 * @plane: plane to get state object for
591 *
592 * This function returns the plane state for the given plane, allocating it if
593 * needed. It will also grab the relevant plane lock to make sure that the state
594 * is consistent.
595 *
596 * Returns:
597 *
598 * Either the allocated state or the error code encoded into the pointer. When
599 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
600 * entire atomic sequence must be restarted. All other errors are fatal.
601 */
602struct drm_plane_state *
603drm_atomic_get_plane_state(struct drm_atomic_state *state,
604 struct drm_plane *plane)
605{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200606 int ret, index = drm_plane_index(plane);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200607 struct drm_plane_state *plane_state;
608
Maarten Lankhorst7f4eaa82016-05-03 11:12:31 +0200609 WARN_ON(!state->acquire_ctx);
610
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200611 plane_state = drm_atomic_get_existing_plane_state(state, plane);
612 if (plane_state)
613 return plane_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200614
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100615 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200616 if (ret)
617 return ERR_PTR(ret);
618
619 plane_state = plane->funcs->atomic_duplicate_state(plane);
620 if (!plane_state)
621 return ERR_PTR(-ENOMEM);
622
Daniel Vetterb8b53422016-06-02 00:06:33 +0200623 state->planes[index].state = plane_state;
624 state->planes[index].ptr = plane;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200625 plane_state->state = state;
626
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200627 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
628 plane->base.id, plane->name, plane_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200629
630 if (plane_state->crtc) {
631 struct drm_crtc_state *crtc_state;
632
633 crtc_state = drm_atomic_get_crtc_state(state,
634 plane_state->crtc);
635 if (IS_ERR(crtc_state))
636 return ERR_CAST(crtc_state);
637 }
638
639 return plane_state;
640}
641EXPORT_SYMBOL(drm_atomic_get_plane_state);
642
643/**
Rob Clark40ecc692014-12-18 16:01:46 -0500644 * drm_atomic_plane_set_property - set property on plane
645 * @plane: the drm plane to set a property on
646 * @state: the state object to update with the new property value
647 * @property: the property to set
648 * @val: the new property value
649 *
650 * Use this instead of calling plane->atomic_set_property directly.
651 * This function handles generic/core properties and calls out to
652 * driver's ->atomic_set_property() for driver properties. To ensure
653 * consistent behavior you must call this function rather than the
654 * driver hook directly.
655 *
656 * RETURNS:
657 * Zero on success, error code on failure
658 */
659int drm_atomic_plane_set_property(struct drm_plane *plane,
660 struct drm_plane_state *state, struct drm_property *property,
661 uint64_t val)
662{
Rob Clark6b4959f2014-12-18 16:01:53 -0500663 struct drm_device *dev = plane->dev;
664 struct drm_mode_config *config = &dev->mode_config;
665
666 if (property == config->prop_fb_id) {
667 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
668 drm_atomic_set_fb_for_plane(state, fb);
669 if (fb)
670 drm_framebuffer_unreference(fb);
671 } else if (property == config->prop_crtc_id) {
672 struct drm_crtc *crtc = drm_crtc_find(dev, val);
673 return drm_atomic_set_crtc_for_plane(state, crtc);
674 } else if (property == config->prop_crtc_x) {
675 state->crtc_x = U642I64(val);
676 } else if (property == config->prop_crtc_y) {
677 state->crtc_y = U642I64(val);
678 } else if (property == config->prop_crtc_w) {
679 state->crtc_w = val;
680 } else if (property == config->prop_crtc_h) {
681 state->crtc_h = val;
682 } else if (property == config->prop_src_x) {
683 state->src_x = val;
684 } else if (property == config->prop_src_y) {
685 state->src_y = val;
686 } else if (property == config->prop_src_w) {
687 state->src_w = val;
688 } else if (property == config->prop_src_h) {
689 state->src_h = val;
Matt Roper1da30622015-01-21 16:35:40 -0800690 } else if (property == config->rotation_property) {
691 state->rotation = val;
Rob Clark6b4959f2014-12-18 16:01:53 -0500692 } else if (plane->funcs->atomic_set_property) {
693 return plane->funcs->atomic_set_property(plane, state,
694 property, val);
695 } else {
696 return -EINVAL;
697 }
698
699 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -0500700}
701EXPORT_SYMBOL(drm_atomic_plane_set_property);
702
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100703/**
704 * drm_atomic_plane_get_property - get property value from plane state
705 * @plane: the drm plane to set a property on
706 * @state: the state object to get the property value from
707 * @property: the property to set
708 * @val: return location for the property value
709 *
Rob Clarkac9c9252014-12-18 16:01:47 -0500710 * This function handles generic/core properties and calls out to
711 * driver's ->atomic_get_property() for driver properties. To ensure
712 * consistent behavior you must call this function rather than the
713 * driver hook directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100714 *
715 * RETURNS:
716 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -0500717 */
Daniel Vettera97df1c2014-12-18 22:49:02 +0100718static int
719drm_atomic_plane_get_property(struct drm_plane *plane,
Rob Clarkac9c9252014-12-18 16:01:47 -0500720 const struct drm_plane_state *state,
721 struct drm_property *property, uint64_t *val)
722{
Rob Clark6b4959f2014-12-18 16:01:53 -0500723 struct drm_device *dev = plane->dev;
724 struct drm_mode_config *config = &dev->mode_config;
725
726 if (property == config->prop_fb_id) {
727 *val = (state->fb) ? state->fb->base.id : 0;
728 } else if (property == config->prop_crtc_id) {
729 *val = (state->crtc) ? state->crtc->base.id : 0;
730 } else if (property == config->prop_crtc_x) {
731 *val = I642U64(state->crtc_x);
732 } else if (property == config->prop_crtc_y) {
733 *val = I642U64(state->crtc_y);
734 } else if (property == config->prop_crtc_w) {
735 *val = state->crtc_w;
736 } else if (property == config->prop_crtc_h) {
737 *val = state->crtc_h;
738 } else if (property == config->prop_src_x) {
739 *val = state->src_x;
740 } else if (property == config->prop_src_y) {
741 *val = state->src_y;
742 } else if (property == config->prop_src_w) {
743 *val = state->src_w;
744 } else if (property == config->prop_src_h) {
745 *val = state->src_h;
Tvrtko Ursulin4cda09c2015-02-26 13:49:17 +0000746 } else if (property == config->rotation_property) {
747 *val = state->rotation;
Rob Clark6b4959f2014-12-18 16:01:53 -0500748 } else if (plane->funcs->atomic_get_property) {
Rob Clarkac9c9252014-12-18 16:01:47 -0500749 return plane->funcs->atomic_get_property(plane, state, property, val);
Rob Clark6b4959f2014-12-18 16:01:53 -0500750 } else {
751 return -EINVAL;
752 }
753
754 return 0;
Rob Clarkac9c9252014-12-18 16:01:47 -0500755}
Rob Clarkac9c9252014-12-18 16:01:47 -0500756
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200757static bool
758plane_switching_crtc(struct drm_atomic_state *state,
759 struct drm_plane *plane,
760 struct drm_plane_state *plane_state)
761{
762 if (!plane->state->crtc || !plane_state->crtc)
763 return false;
764
765 if (plane->state->crtc == plane_state->crtc)
766 return false;
767
768 /* This could be refined, but currently there's no helper or driver code
769 * to implement direct switching of active planes nor userspace to take
770 * advantage of more direct plane switching without the intermediate
771 * full OFF state.
772 */
773 return true;
774}
775
Rob Clarkac9c9252014-12-18 16:01:47 -0500776/**
Rob Clark5e743732014-12-18 16:01:51 -0500777 * drm_atomic_plane_check - check plane state
778 * @plane: plane to check
779 * @state: plane state to check
780 *
781 * Provides core sanity checks for plane state.
782 *
783 * RETURNS:
784 * Zero on success, error code on failure
785 */
786static int drm_atomic_plane_check(struct drm_plane *plane,
787 struct drm_plane_state *state)
788{
789 unsigned int fb_width, fb_height;
Laurent Pinchartead86102015-03-05 02:25:43 +0200790 int ret;
Rob Clark5e743732014-12-18 16:01:51 -0500791
792 /* either *both* CRTC and FB must be set, or neither */
793 if (WARN_ON(state->crtc && !state->fb)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100794 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
Rob Clark5e743732014-12-18 16:01:51 -0500795 return -EINVAL;
796 } else if (WARN_ON(state->fb && !state->crtc)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100797 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
Rob Clark5e743732014-12-18 16:01:51 -0500798 return -EINVAL;
799 }
800
801 /* if disabled, we don't care about the rest of the state: */
802 if (!state->crtc)
803 return 0;
804
805 /* Check whether this plane is usable on this CRTC */
806 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100807 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
Rob Clark5e743732014-12-18 16:01:51 -0500808 return -EINVAL;
809 }
810
811 /* Check whether this plane supports the fb pixel format. */
Laurent Pinchartead86102015-03-05 02:25:43 +0200812 ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
813 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100814 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
815 drm_get_format_name(state->fb->pixel_format));
Laurent Pinchartead86102015-03-05 02:25:43 +0200816 return ret;
Rob Clark5e743732014-12-18 16:01:51 -0500817 }
818
819 /* Give drivers some help against integer overflows */
820 if (state->crtc_w > INT_MAX ||
821 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
822 state->crtc_h > INT_MAX ||
823 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100824 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
825 state->crtc_w, state->crtc_h,
826 state->crtc_x, state->crtc_y);
Rob Clark5e743732014-12-18 16:01:51 -0500827 return -ERANGE;
828 }
829
830 fb_width = state->fb->width << 16;
831 fb_height = state->fb->height << 16;
832
833 /* Make sure source coordinates are inside the fb. */
834 if (state->src_w > fb_width ||
835 state->src_x > fb_width - state->src_w ||
836 state->src_h > fb_height ||
837 state->src_y > fb_height - state->src_h) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100838 DRM_DEBUG_ATOMIC("Invalid source coordinates "
839 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
840 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
841 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
842 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
843 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
Rob Clark5e743732014-12-18 16:01:51 -0500844 return -ENOSPC;
845 }
846
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200847 if (plane_switching_crtc(state->state, plane, state)) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200848 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
849 plane->base.id, plane->name);
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200850 return -EINVAL;
851 }
852
Rob Clark5e743732014-12-18 16:01:51 -0500853 return 0;
854}
855
856/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200857 * drm_atomic_get_connector_state - get connector state
858 * @state: global atomic state object
859 * @connector: connector to get state object for
860 *
861 * This function returns the connector state for the given connector,
862 * allocating it if needed. It will also grab the relevant connector lock to
863 * make sure that the state is consistent.
864 *
865 * Returns:
866 *
867 * Either the allocated state or the error code encoded into the pointer. When
868 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
869 * entire atomic sequence must be restarted. All other errors are fatal.
870 */
871struct drm_connector_state *
872drm_atomic_get_connector_state(struct drm_atomic_state *state,
873 struct drm_connector *connector)
874{
875 int ret, index;
876 struct drm_mode_config *config = &connector->dev->mode_config;
877 struct drm_connector_state *connector_state;
878
Maarten Lankhorst7f4eaa82016-05-03 11:12:31 +0200879 WARN_ON(!state->acquire_ctx);
880
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100881 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
882 if (ret)
883 return ERR_PTR(ret);
884
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200885 index = drm_connector_index(connector);
886
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100887 if (index >= state->num_connector) {
Daniel Vetter63e83c12016-06-02 00:06:32 +0200888 struct __drm_connnectors_state *c;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100889 int alloc = max(index + 1, config->num_connector);
890
891 c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
892 if (!c)
893 return ERR_PTR(-ENOMEM);
894
895 state->connectors = c;
896 memset(&state->connectors[state->num_connector], 0,
897 sizeof(*state->connectors) * (alloc - state->num_connector));
898
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100899 state->num_connector = alloc;
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100900 }
901
Daniel Vetter63e83c12016-06-02 00:06:32 +0200902 if (state->connectors[index].state)
903 return state->connectors[index].state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200904
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200905 connector_state = connector->funcs->atomic_duplicate_state(connector);
906 if (!connector_state)
907 return ERR_PTR(-ENOMEM);
908
Dave Airlieb164d312016-04-27 11:10:09 +1000909 drm_connector_reference(connector);
Daniel Vetter63e83c12016-06-02 00:06:32 +0200910 state->connectors[index].state = connector_state;
911 state->connectors[index].ptr = connector;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200912 connector_state->state = state;
913
Daniel Vetter17a38d92015-02-22 12:24:16 +0100914 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
915 connector->base.id, connector_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200916
917 if (connector_state->crtc) {
918 struct drm_crtc_state *crtc_state;
919
920 crtc_state = drm_atomic_get_crtc_state(state,
921 connector_state->crtc);
922 if (IS_ERR(crtc_state))
923 return ERR_CAST(crtc_state);
924 }
925
926 return connector_state;
927}
928EXPORT_SYMBOL(drm_atomic_get_connector_state);
929
930/**
Rob Clark40ecc692014-12-18 16:01:46 -0500931 * drm_atomic_connector_set_property - set property on connector.
932 * @connector: the drm connector to set a property on
933 * @state: the state object to update with the new property value
934 * @property: the property to set
935 * @val: the new property value
936 *
937 * Use this instead of calling connector->atomic_set_property directly.
938 * This function handles generic/core properties and calls out to
939 * driver's ->atomic_set_property() for driver properties. To ensure
940 * consistent behavior you must call this function rather than the
941 * driver hook directly.
942 *
943 * RETURNS:
944 * Zero on success, error code on failure
945 */
946int drm_atomic_connector_set_property(struct drm_connector *connector,
947 struct drm_connector_state *state, struct drm_property *property,
948 uint64_t val)
949{
950 struct drm_device *dev = connector->dev;
951 struct drm_mode_config *config = &dev->mode_config;
952
Rob Clarkae16c592014-12-18 16:01:54 -0500953 if (property == config->prop_crtc_id) {
954 struct drm_crtc *crtc = drm_crtc_find(dev, val);
955 return drm_atomic_set_crtc_for_connector(state, crtc);
956 } else if (property == config->dpms_property) {
Rob Clark40ecc692014-12-18 16:01:46 -0500957 /* setting DPMS property requires special handling, which
958 * is done in legacy setprop path for us. Disallow (for
959 * now?) atomic writes to DPMS property:
960 */
961 return -EINVAL;
962 } else if (connector->funcs->atomic_set_property) {
963 return connector->funcs->atomic_set_property(connector,
964 state, property, val);
965 } else {
966 return -EINVAL;
967 }
968}
969EXPORT_SYMBOL(drm_atomic_connector_set_property);
970
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100971/**
972 * drm_atomic_connector_get_property - get property value from connector state
973 * @connector: the drm connector to set a property on
974 * @state: the state object to get the property value from
975 * @property: the property to set
976 * @val: return location for the property value
977 *
Rob Clarkac9c9252014-12-18 16:01:47 -0500978 * This function handles generic/core properties and calls out to
979 * driver's ->atomic_get_property() for driver properties. To ensure
980 * consistent behavior you must call this function rather than the
981 * driver hook directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100982 *
983 * RETURNS:
984 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -0500985 */
Daniel Vettera97df1c2014-12-18 22:49:02 +0100986static int
987drm_atomic_connector_get_property(struct drm_connector *connector,
Rob Clarkac9c9252014-12-18 16:01:47 -0500988 const struct drm_connector_state *state,
989 struct drm_property *property, uint64_t *val)
990{
991 struct drm_device *dev = connector->dev;
992 struct drm_mode_config *config = &dev->mode_config;
993
Rob Clarkae16c592014-12-18 16:01:54 -0500994 if (property == config->prop_crtc_id) {
995 *val = (state->crtc) ? state->crtc->base.id : 0;
996 } else if (property == config->dpms_property) {
Rob Clarkac9c9252014-12-18 16:01:47 -0500997 *val = connector->dpms;
998 } else if (connector->funcs->atomic_get_property) {
999 return connector->funcs->atomic_get_property(connector,
1000 state, property, val);
1001 } else {
1002 return -EINVAL;
1003 }
1004
1005 return 0;
1006}
Rob Clarkac9c9252014-12-18 16:01:47 -05001007
Rob Clark88a48e22014-12-18 16:01:50 -05001008int drm_atomic_get_property(struct drm_mode_object *obj,
1009 struct drm_property *property, uint64_t *val)
1010{
1011 struct drm_device *dev = property->dev;
1012 int ret;
1013
1014 switch (obj->type) {
1015 case DRM_MODE_OBJECT_CONNECTOR: {
1016 struct drm_connector *connector = obj_to_connector(obj);
1017 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
1018 ret = drm_atomic_connector_get_property(connector,
1019 connector->state, property, val);
1020 break;
1021 }
1022 case DRM_MODE_OBJECT_CRTC: {
1023 struct drm_crtc *crtc = obj_to_crtc(obj);
1024 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
1025 ret = drm_atomic_crtc_get_property(crtc,
1026 crtc->state, property, val);
1027 break;
1028 }
1029 case DRM_MODE_OBJECT_PLANE: {
1030 struct drm_plane *plane = obj_to_plane(obj);
1031 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
1032 ret = drm_atomic_plane_get_property(plane,
1033 plane->state, property, val);
1034 break;
1035 }
1036 default:
1037 ret = -EINVAL;
1038 break;
1039 }
1040
1041 return ret;
1042}
1043
1044/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001045 * drm_atomic_set_crtc_for_plane - set crtc for plane
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001046 * @plane_state: the plane whose incoming state to update
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001047 * @crtc: crtc to use for the plane
1048 *
1049 * Changing the assigned crtc for a plane requires us to grab the lock and state
1050 * for the new crtc, as needed. This function takes care of all these details
1051 * besides updating the pointer in the state object itself.
1052 *
1053 * Returns:
1054 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1055 * then the w/w mutex code has detected a deadlock and the entire atomic
1056 * sequence must be restarted. All other errors are fatal.
1057 */
1058int
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001059drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
1060 struct drm_crtc *crtc)
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001061{
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001062 struct drm_plane *plane = plane_state->plane;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001063 struct drm_crtc_state *crtc_state;
1064
Rob Clark6ddd3882014-11-21 15:28:31 -05001065 if (plane_state->crtc) {
1066 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1067 plane_state->crtc);
1068 if (WARN_ON(IS_ERR(crtc_state)))
1069 return PTR_ERR(crtc_state);
1070
1071 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
1072 }
1073
1074 plane_state->crtc = crtc;
1075
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001076 if (crtc) {
1077 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1078 crtc);
1079 if (IS_ERR(crtc_state))
1080 return PTR_ERR(crtc_state);
Rob Clark6ddd3882014-11-21 15:28:31 -05001081 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001082 }
1083
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001084 if (crtc)
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001085 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
1086 plane_state, crtc->base.id, crtc->name);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001087 else
Daniel Vetter17a38d92015-02-22 12:24:16 +01001088 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
1089 plane_state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001090
1091 return 0;
1092}
1093EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
1094
1095/**
John Hunter16d78bc2e2015-04-07 19:38:50 +08001096 * drm_atomic_set_fb_for_plane - set framebuffer for plane
Daniel Vetter321ebf02014-11-04 22:57:27 +01001097 * @plane_state: atomic state object for the plane
1098 * @fb: fb to use for the plane
1099 *
1100 * Changing the assigned framebuffer for a plane requires us to grab a reference
1101 * to the new fb and drop the reference to the old fb, if there is one. This
1102 * function takes care of all these details besides updating the pointer in the
1103 * state object itself.
1104 */
1105void
1106drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1107 struct drm_framebuffer *fb)
1108{
1109 if (plane_state->fb)
1110 drm_framebuffer_unreference(plane_state->fb);
1111 if (fb)
1112 drm_framebuffer_reference(fb);
1113 plane_state->fb = fb;
1114
1115 if (fb)
Daniel Vetter17a38d92015-02-22 12:24:16 +01001116 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1117 fb->base.id, plane_state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01001118 else
Daniel Vetter17a38d92015-02-22 12:24:16 +01001119 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1120 plane_state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01001121}
1122EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1123
1124/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001125 * drm_atomic_set_crtc_for_connector - set crtc for connector
1126 * @conn_state: atomic state object for the connector
1127 * @crtc: crtc to use for the connector
1128 *
1129 * Changing the assigned crtc for a connector requires us to grab the lock and
1130 * state for the new crtc, as needed. This function takes care of all these
1131 * details besides updating the pointer in the state object itself.
1132 *
1133 * Returns:
1134 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1135 * then the w/w mutex code has detected a deadlock and the entire atomic
1136 * sequence must be restarted. All other errors are fatal.
1137 */
1138int
1139drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1140 struct drm_crtc *crtc)
1141{
1142 struct drm_crtc_state *crtc_state;
1143
Chris Wilsone2d800a2016-05-06 12:47:45 +01001144 if (conn_state->crtc == crtc)
1145 return 0;
1146
1147 if (conn_state->crtc) {
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001148 crtc_state = drm_atomic_get_existing_crtc_state(conn_state->state,
1149 conn_state->crtc);
1150
1151 crtc_state->connector_mask &=
1152 ~(1 << drm_connector_index(conn_state->connector));
Chris Wilsone2d800a2016-05-06 12:47:45 +01001153
1154 drm_connector_unreference(conn_state->connector);
1155 conn_state->crtc = NULL;
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001156 }
1157
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001158 if (crtc) {
1159 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1160 if (IS_ERR(crtc_state))
1161 return PTR_ERR(crtc_state);
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001162
1163 crtc_state->connector_mask |=
1164 1 << drm_connector_index(conn_state->connector);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001165
Chris Wilsone2d800a2016-05-06 12:47:45 +01001166 drm_connector_reference(conn_state->connector);
1167 conn_state->crtc = crtc;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001168
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001169 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
1170 conn_state, crtc->base.id, crtc->name);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001171 } else {
Daniel Vetter17a38d92015-02-22 12:24:16 +01001172 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1173 conn_state);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001174 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001175
1176 return 0;
1177}
1178EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1179
1180/**
1181 * drm_atomic_add_affected_connectors - add connectors for crtc
1182 * @state: atomic state
1183 * @crtc: DRM crtc
1184 *
1185 * This function walks the current configuration and adds all connectors
1186 * currently using @crtc to the atomic configuration @state. Note that this
1187 * function must acquire the connection mutex. This can potentially cause
1188 * unneeded seralization if the update is just for the planes on one crtc. Hence
1189 * drivers and helpers should only call this when really needed (e.g. when a
1190 * full modeset needs to happen due to some change).
1191 *
1192 * Returns:
1193 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1194 * then the w/w mutex code has detected a deadlock and the entire atomic
1195 * sequence must be restarted. All other errors are fatal.
1196 */
1197int
1198drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1199 struct drm_crtc *crtc)
1200{
1201 struct drm_mode_config *config = &state->dev->mode_config;
1202 struct drm_connector *connector;
1203 struct drm_connector_state *conn_state;
1204 int ret;
1205
1206 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1207 if (ret)
1208 return ret;
1209
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001210 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1211 crtc->base.id, crtc->name, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001212
1213 /*
1214 * Changed connectors are already in @state, so only need to look at the
1215 * current configuration.
1216 */
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02001217 drm_for_each_connector(connector, state->dev) {
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001218 if (connector->state->crtc != crtc)
1219 continue;
1220
1221 conn_state = drm_atomic_get_connector_state(state, connector);
1222 if (IS_ERR(conn_state))
1223 return PTR_ERR(conn_state);
1224 }
1225
1226 return 0;
1227}
1228EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1229
1230/**
Maarten Lankhorste01e9f72015-05-19 16:41:02 +02001231 * drm_atomic_add_affected_planes - add planes for crtc
1232 * @state: atomic state
1233 * @crtc: DRM crtc
1234 *
1235 * This function walks the current configuration and adds all planes
1236 * currently used by @crtc to the atomic configuration @state. This is useful
1237 * when an atomic commit also needs to check all currently enabled plane on
1238 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1239 * to avoid special code to force-enable all planes.
1240 *
1241 * Since acquiring a plane state will always also acquire the w/w mutex of the
1242 * current CRTC for that plane (if there is any) adding all the plane states for
1243 * a CRTC will not reduce parallism of atomic updates.
1244 *
1245 * Returns:
1246 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1247 * then the w/w mutex code has detected a deadlock and the entire atomic
1248 * sequence must be restarted. All other errors are fatal.
1249 */
1250int
1251drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1252 struct drm_crtc *crtc)
1253{
1254 struct drm_plane *plane;
1255
1256 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
1257
1258 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1259 struct drm_plane_state *plane_state =
1260 drm_atomic_get_plane_state(state, plane);
1261
1262 if (IS_ERR(plane_state))
1263 return PTR_ERR(plane_state);
1264 }
1265 return 0;
1266}
1267EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1268
1269/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001270 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1271 * @state: atomic state
1272 *
1273 * This function should be used by legacy entry points which don't understand
1274 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
John Hunter16d78bc2e2015-04-07 19:38:50 +08001275 * the slowpath completed.
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001276 */
1277void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1278{
1279 int ret;
1280
1281retry:
1282 drm_modeset_backoff(state->acquire_ctx);
1283
Thierry Reding06eaae42015-12-02 17:50:03 +01001284 ret = drm_modeset_lock_all_ctx(state->dev, state->acquire_ctx);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001285 if (ret)
1286 goto retry;
1287}
1288EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1289
1290/**
1291 * drm_atomic_check_only - check whether a given config would work
1292 * @state: atomic configuration to check
1293 *
1294 * Note that this function can return -EDEADLK if the driver needed to acquire
1295 * more locks but encountered a deadlock. The caller must then do the usual w/w
1296 * backoff dance and restart. All other errors are fatal.
1297 *
1298 * Returns:
1299 * 0 on success, negative error code on failure.
1300 */
1301int drm_atomic_check_only(struct drm_atomic_state *state)
1302{
Rob Clark5e743732014-12-18 16:01:51 -05001303 struct drm_device *dev = state->dev;
1304 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001305 struct drm_plane *plane;
1306 struct drm_plane_state *plane_state;
1307 struct drm_crtc *crtc;
1308 struct drm_crtc_state *crtc_state;
Rob Clark5e743732014-12-18 16:01:51 -05001309 int i, ret = 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001310
Daniel Vetter17a38d92015-02-22 12:24:16 +01001311 DRM_DEBUG_ATOMIC("checking %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001312
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001313 for_each_plane_in_state(state, plane, plane_state, i) {
1314 ret = drm_atomic_plane_check(plane, plane_state);
Rob Clark5e743732014-12-18 16:01:51 -05001315 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001316 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1317 plane->base.id, plane->name);
Rob Clark5e743732014-12-18 16:01:51 -05001318 return ret;
1319 }
1320 }
1321
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001322 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1323 ret = drm_atomic_crtc_check(crtc, crtc_state);
Rob Clark5e743732014-12-18 16:01:51 -05001324 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001325 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1326 crtc->base.id, crtc->name);
Rob Clark5e743732014-12-18 16:01:51 -05001327 return ret;
1328 }
1329 }
1330
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001331 if (config->funcs->atomic_check)
Rob Clark5e743732014-12-18 16:01:51 -05001332 ret = config->funcs->atomic_check(state->dev, state);
1333
Rob Clarkd34f20d2014-12-18 16:01:56 -05001334 if (!state->allow_modeset) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001335 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter2465ff62015-06-18 09:58:55 +02001336 if (drm_atomic_crtc_needs_modeset(crtc_state)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001337 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1338 crtc->base.id, crtc->name);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001339 return -EINVAL;
1340 }
1341 }
1342 }
1343
Rob Clark5e743732014-12-18 16:01:51 -05001344 return ret;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001345}
1346EXPORT_SYMBOL(drm_atomic_check_only);
1347
1348/**
1349 * drm_atomic_commit - commit configuration atomically
1350 * @state: atomic configuration to check
1351 *
1352 * Note that this function can return -EDEADLK if the driver needed to acquire
1353 * more locks but encountered a deadlock. The caller must then do the usual w/w
1354 * backoff dance and restart. All other errors are fatal.
1355 *
1356 * Also note that on successful execution ownership of @state is transferred
1357 * from the caller of this function to the function itself. The caller must not
1358 * free or in any other way access @state. If the function fails then the caller
1359 * must clean up @state itself.
1360 *
1361 * Returns:
1362 * 0 on success, negative error code on failure.
1363 */
1364int drm_atomic_commit(struct drm_atomic_state *state)
1365{
1366 struct drm_mode_config *config = &state->dev->mode_config;
1367 int ret;
1368
1369 ret = drm_atomic_check_only(state);
1370 if (ret)
1371 return ret;
1372
Daniel Vetter17a38d92015-02-22 12:24:16 +01001373 DRM_DEBUG_ATOMIC("commiting %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001374
1375 return config->funcs->atomic_commit(state->dev, state, false);
1376}
1377EXPORT_SYMBOL(drm_atomic_commit);
1378
1379/**
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02001380 * drm_atomic_nonblocking_commit - atomic&nonblocking configuration commit
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001381 * @state: atomic configuration to check
1382 *
1383 * Note that this function can return -EDEADLK if the driver needed to acquire
1384 * more locks but encountered a deadlock. The caller must then do the usual w/w
1385 * backoff dance and restart. All other errors are fatal.
1386 *
1387 * Also note that on successful execution ownership of @state is transferred
1388 * from the caller of this function to the function itself. The caller must not
1389 * free or in any other way access @state. If the function fails then the caller
1390 * must clean up @state itself.
1391 *
1392 * Returns:
1393 * 0 on success, negative error code on failure.
1394 */
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02001395int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001396{
1397 struct drm_mode_config *config = &state->dev->mode_config;
1398 int ret;
1399
1400 ret = drm_atomic_check_only(state);
1401 if (ret)
1402 return ret;
1403
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02001404 DRM_DEBUG_ATOMIC("commiting %p nonblocking\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001405
1406 return config->funcs->atomic_commit(state->dev, state, true);
1407}
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02001408EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001409
1410/*
1411 * The big monstor ioctl
1412 */
1413
1414static struct drm_pending_vblank_event *create_vblank_event(
1415 struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data)
1416{
1417 struct drm_pending_vblank_event *e = NULL;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01001418 int ret;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001419
1420 e = kzalloc(sizeof *e, GFP_KERNEL);
Daniel Vetter2dd500f2016-01-11 22:40:56 +01001421 if (!e)
1422 return NULL;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001423
1424 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01001425 e->event.base.length = sizeof(e->event);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001426 e->event.user_data = user_data;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001427
Daniel Vetter2dd500f2016-01-11 22:40:56 +01001428 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
1429 if (ret) {
1430 kfree(e);
1431 return NULL;
1432 }
1433
Rob Clarkd34f20d2014-12-18 16:01:56 -05001434 return e;
1435}
1436
Rob Clarkd34f20d2014-12-18 16:01:56 -05001437static int atomic_set_prop(struct drm_atomic_state *state,
1438 struct drm_mode_object *obj, struct drm_property *prop,
1439 uint64_t prop_value)
1440{
1441 struct drm_mode_object *ref;
1442 int ret;
1443
1444 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1445 return -EINVAL;
1446
1447 switch (obj->type) {
1448 case DRM_MODE_OBJECT_CONNECTOR: {
1449 struct drm_connector *connector = obj_to_connector(obj);
1450 struct drm_connector_state *connector_state;
1451
1452 connector_state = drm_atomic_get_connector_state(state, connector);
1453 if (IS_ERR(connector_state)) {
1454 ret = PTR_ERR(connector_state);
1455 break;
1456 }
1457
1458 ret = drm_atomic_connector_set_property(connector,
1459 connector_state, prop, prop_value);
1460 break;
1461 }
1462 case DRM_MODE_OBJECT_CRTC: {
1463 struct drm_crtc *crtc = obj_to_crtc(obj);
1464 struct drm_crtc_state *crtc_state;
1465
1466 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1467 if (IS_ERR(crtc_state)) {
1468 ret = PTR_ERR(crtc_state);
1469 break;
1470 }
1471
1472 ret = drm_atomic_crtc_set_property(crtc,
1473 crtc_state, prop, prop_value);
1474 break;
1475 }
1476 case DRM_MODE_OBJECT_PLANE: {
1477 struct drm_plane *plane = obj_to_plane(obj);
1478 struct drm_plane_state *plane_state;
1479
1480 plane_state = drm_atomic_get_plane_state(state, plane);
1481 if (IS_ERR(plane_state)) {
1482 ret = PTR_ERR(plane_state);
1483 break;
1484 }
1485
1486 ret = drm_atomic_plane_set_property(plane,
1487 plane_state, prop, prop_value);
1488 break;
1489 }
1490 default:
1491 ret = -EINVAL;
1492 break;
1493 }
1494
1495 drm_property_change_valid_put(prop, ref);
1496 return ret;
1497}
1498
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001499/**
Maarten Lankhorst9744bf42015-11-24 10:34:34 +01001500 * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001501 *
1502 * @dev: drm device to check.
1503 * @plane_mask: plane mask for planes that were updated.
1504 * @ret: return value, can be -EDEADLK for a retry.
1505 *
1506 * Before doing an update plane->old_fb is set to plane->fb,
1507 * but before dropping the locks old_fb needs to be set to NULL
1508 * and plane->fb updated. This is a common operation for each
1509 * atomic update, so this call is split off as a helper.
1510 */
1511void drm_atomic_clean_old_fb(struct drm_device *dev,
1512 unsigned plane_mask,
1513 int ret)
1514{
1515 struct drm_plane *plane;
1516
1517 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1518 * locks (ie. while it is still safe to deref plane->state). We
1519 * need to do this here because the driver entry points cannot
1520 * distinguish between legacy and atomic ioctls.
1521 */
1522 drm_for_each_plane_mask(plane, dev, plane_mask) {
1523 if (ret == 0) {
1524 struct drm_framebuffer *new_fb = plane->state->fb;
1525 if (new_fb)
1526 drm_framebuffer_reference(new_fb);
1527 plane->fb = new_fb;
1528 plane->crtc = plane->state->crtc;
1529
1530 if (plane->old_fb)
1531 drm_framebuffer_unreference(plane->old_fb);
1532 }
1533 plane->old_fb = NULL;
1534 }
1535}
1536EXPORT_SYMBOL(drm_atomic_clean_old_fb);
1537
Rob Clarkd34f20d2014-12-18 16:01:56 -05001538int drm_mode_atomic_ioctl(struct drm_device *dev,
1539 void *data, struct drm_file *file_priv)
1540{
1541 struct drm_mode_atomic *arg = data;
1542 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1543 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1544 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1545 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1546 unsigned int copied_objs, copied_props;
1547 struct drm_atomic_state *state;
1548 struct drm_modeset_acquire_ctx ctx;
1549 struct drm_plane *plane;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001550 struct drm_crtc *crtc;
1551 struct drm_crtc_state *crtc_state;
Maarten Lankhorst45723722015-11-11 11:29:08 +01001552 unsigned plane_mask;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001553 int ret = 0;
1554 unsigned int i, j;
1555
1556 /* disallow for drivers not supporting atomic: */
1557 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1558 return -EINVAL;
1559
1560 /* disallow for userspace that has not enabled atomic cap (even
1561 * though this may be a bit overkill, since legacy userspace
1562 * wouldn't know how to call this ioctl)
1563 */
1564 if (!file_priv->atomic)
1565 return -EINVAL;
1566
1567 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1568 return -EINVAL;
1569
1570 if (arg->reserved)
1571 return -EINVAL;
1572
1573 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1574 !dev->mode_config.async_page_flip)
1575 return -EINVAL;
1576
1577 /* can't test and expect an event at the same time. */
1578 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1579 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1580 return -EINVAL;
1581
1582 drm_modeset_acquire_init(&ctx, 0);
1583
1584 state = drm_atomic_state_alloc(dev);
1585 if (!state)
1586 return -ENOMEM;
1587
1588 state->acquire_ctx = &ctx;
1589 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1590
1591retry:
Maarten Lankhorst45723722015-11-11 11:29:08 +01001592 plane_mask = 0;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001593 copied_objs = 0;
1594 copied_props = 0;
1595
1596 for (i = 0; i < arg->count_objs; i++) {
1597 uint32_t obj_id, count_props;
1598 struct drm_mode_object *obj;
1599
1600 if (get_user(obj_id, objs_ptr + copied_objs)) {
1601 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001602 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001603 }
1604
1605 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
Dave Airlieb164d312016-04-27 11:10:09 +10001606 if (!obj) {
1607 ret = -ENOENT;
1608 goto out;
1609 }
1610
1611 if (!obj->properties) {
1612 drm_mode_object_unreference(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001613 ret = -ENOENT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001614 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001615 }
1616
Rob Clarkd34f20d2014-12-18 16:01:56 -05001617 if (get_user(count_props, count_props_ptr + copied_objs)) {
Dave Airlieb164d312016-04-27 11:10:09 +10001618 drm_mode_object_unreference(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001619 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001620 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001621 }
1622
1623 copied_objs++;
1624
1625 for (j = 0; j < count_props; j++) {
1626 uint32_t prop_id;
1627 uint64_t prop_value;
1628 struct drm_property *prop;
1629
1630 if (get_user(prop_id, props_ptr + copied_props)) {
Dave Airlieb164d312016-04-27 11:10:09 +10001631 drm_mode_object_unreference(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001632 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001633 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001634 }
1635
1636 prop = drm_property_find(dev, prop_id);
1637 if (!prop) {
Dave Airlieb164d312016-04-27 11:10:09 +10001638 drm_mode_object_unreference(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001639 ret = -ENOENT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001640 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001641 }
1642
Guenter Roeck42c58142015-01-12 21:12:17 -08001643 if (copy_from_user(&prop_value,
1644 prop_values_ptr + copied_props,
1645 sizeof(prop_value))) {
Dave Airlieb164d312016-04-27 11:10:09 +10001646 drm_mode_object_unreference(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001647 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001648 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001649 }
1650
1651 ret = atomic_set_prop(state, obj, prop, prop_value);
Dave Airlieb164d312016-04-27 11:10:09 +10001652 if (ret) {
1653 drm_mode_object_unreference(obj);
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001654 goto out;
Dave Airlieb164d312016-04-27 11:10:09 +10001655 }
Rob Clarkd34f20d2014-12-18 16:01:56 -05001656
1657 copied_props++;
1658 }
Maarten Lankhorsta9cc54e2015-06-24 08:59:24 +02001659
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02001660 if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
1661 !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
Maarten Lankhorsta9cc54e2015-06-24 08:59:24 +02001662 plane = obj_to_plane(obj);
1663 plane_mask |= (1 << drm_plane_index(plane));
1664 plane->old_fb = plane->fb;
1665 }
Dave Airlieb164d312016-04-27 11:10:09 +10001666 drm_mode_object_unreference(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001667 }
1668
1669 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001670 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Rob Clarkd34f20d2014-12-18 16:01:56 -05001671 struct drm_pending_vblank_event *e;
1672
Rob Clarkd34f20d2014-12-18 16:01:56 -05001673 e = create_vblank_event(dev, file_priv, arg->user_data);
1674 if (!e) {
1675 ret = -ENOMEM;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001676 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001677 }
1678
1679 crtc_state->event = e;
1680 }
1681 }
1682
1683 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02001684 /*
1685 * Unlike commit, check_only does not clean up state.
1686 * Below we call drm_atomic_state_free for it.
1687 */
Rob Clarkd34f20d2014-12-18 16:01:56 -05001688 ret = drm_atomic_check_only(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001689 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02001690 ret = drm_atomic_nonblocking_commit(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001691 } else {
1692 ret = drm_atomic_commit(state);
1693 }
1694
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001695out:
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001696 drm_atomic_clean_old_fb(dev, plane_mask, ret);
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001697
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02001698 if (ret && arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1699 /*
1700 * TEST_ONLY and PAGE_FLIP_EVENT are mutually exclusive,
1701 * if they weren't, this code should be called on success
1702 * for TEST_ONLY too.
1703 */
1704
1705 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1706 if (!crtc_state->event)
1707 continue;
1708
Daniel Vetter2dd500f2016-01-11 22:40:56 +01001709 drm_event_cancel_free(dev, &crtc_state->event->base);
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02001710 }
1711 }
1712
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001713 if (ret == -EDEADLK) {
1714 drm_atomic_state_clear(state);
1715 drm_modeset_backoff(&ctx);
1716 goto retry;
1717 }
1718
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02001719 if (ret || arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001720 drm_atomic_state_free(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001721
1722 drm_modeset_drop_locks(&ctx);
1723 drm_modeset_acquire_fini(&ctx);
1724
1725 return ret;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001726}