blob: 1c472b1baeb818126daa0f0524b83f6107d45ee9 [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
33static void kfree_state(struct drm_atomic_state *state)
34{
35 kfree(state->connectors);
36 kfree(state->connector_states);
37 kfree(state->crtcs);
38 kfree(state->crtc_states);
39 kfree(state->planes);
40 kfree(state->plane_states);
41 kfree(state);
42}
43
44/**
45 * drm_atomic_state_alloc - allocate atomic state
46 * @dev: DRM device
47 *
48 * This allocates an empty atomic state to track updates.
49 */
50struct drm_atomic_state *
51drm_atomic_state_alloc(struct drm_device *dev)
52{
53 struct drm_atomic_state *state;
54
55 state = kzalloc(sizeof(*state), GFP_KERNEL);
56 if (!state)
57 return NULL;
58
Daniel Vetterf52b69f12014-11-19 18:38:08 +010059 state->num_connector = ACCESS_ONCE(dev->mode_config.num_connector);
60
Daniel Vettercc4ceb42014-07-25 21:30:38 +020061 state->crtcs = kcalloc(dev->mode_config.num_crtc,
62 sizeof(*state->crtcs), GFP_KERNEL);
63 if (!state->crtcs)
64 goto fail;
65 state->crtc_states = kcalloc(dev->mode_config.num_crtc,
66 sizeof(*state->crtc_states), GFP_KERNEL);
67 if (!state->crtc_states)
68 goto fail;
69 state->planes = kcalloc(dev->mode_config.num_total_plane,
70 sizeof(*state->planes), GFP_KERNEL);
71 if (!state->planes)
72 goto fail;
73 state->plane_states = kcalloc(dev->mode_config.num_total_plane,
74 sizeof(*state->plane_states), GFP_KERNEL);
75 if (!state->plane_states)
76 goto fail;
Daniel Vetterf52b69f12014-11-19 18:38:08 +010077 state->connectors = kcalloc(state->num_connector,
Daniel Vettercc4ceb42014-07-25 21:30:38 +020078 sizeof(*state->connectors),
79 GFP_KERNEL);
80 if (!state->connectors)
81 goto fail;
Daniel Vetterf52b69f12014-11-19 18:38:08 +010082 state->connector_states = kcalloc(state->num_connector,
Daniel Vettercc4ceb42014-07-25 21:30:38 +020083 sizeof(*state->connector_states),
84 GFP_KERNEL);
85 if (!state->connector_states)
86 goto fail;
87
88 state->dev = dev;
89
90 DRM_DEBUG_KMS("Allocate atomic state %p\n", state);
91
92 return state;
93fail:
94 kfree_state(state);
95
96 return NULL;
97}
98EXPORT_SYMBOL(drm_atomic_state_alloc);
99
100/**
101 * drm_atomic_state_clear - clear state object
102 * @state: atomic state
103 *
104 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
105 * all locks. So someone else could sneak in and change the current modeset
106 * configuration. Which means that all the state assembled in @state is no
107 * longer an atomic update to the current state, but to some arbitrary earlier
108 * state. Which could break assumptions the driver's ->atomic_check likely
109 * relies on.
110 *
111 * Hence we must clear all cached state and completely start over, using this
112 * function.
113 */
114void drm_atomic_state_clear(struct drm_atomic_state *state)
115{
116 struct drm_device *dev = state->dev;
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100117 struct drm_mode_config *config = &dev->mode_config;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200118 int i;
119
120 DRM_DEBUG_KMS("Clearing atomic state %p\n", state);
121
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100122 for (i = 0; i < state->num_connector; i++) {
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200123 struct drm_connector *connector = state->connectors[i];
124
125 if (!connector)
126 continue;
127
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100128 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
129
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200130 connector->funcs->atomic_destroy_state(connector,
131 state->connector_states[i]);
132 }
133
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100134 for (i = 0; i < config->num_crtc; i++) {
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200135 struct drm_crtc *crtc = state->crtcs[i];
136
137 if (!crtc)
138 continue;
139
140 crtc->funcs->atomic_destroy_state(crtc,
141 state->crtc_states[i]);
142 }
143
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100144 for (i = 0; i < config->num_total_plane; i++) {
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200145 struct drm_plane *plane = state->planes[i];
146
147 if (!plane)
148 continue;
149
150 plane->funcs->atomic_destroy_state(plane,
151 state->plane_states[i]);
152 }
153}
154EXPORT_SYMBOL(drm_atomic_state_clear);
155
156/**
157 * drm_atomic_state_free - free all memory for an atomic state
158 * @state: atomic state to deallocate
159 *
160 * This frees all memory associated with an atomic state, including all the
161 * per-object state for planes, crtcs and connectors.
162 */
163void drm_atomic_state_free(struct drm_atomic_state *state)
164{
165 drm_atomic_state_clear(state);
166
167 DRM_DEBUG_KMS("Freeing atomic state %p\n", state);
168
169 kfree_state(state);
170}
171EXPORT_SYMBOL(drm_atomic_state_free);
172
173/**
174 * drm_atomic_get_crtc_state - get crtc state
175 * @state: global atomic state object
176 * @crtc: crtc to get state object for
177 *
178 * This function returns the crtc state for the given crtc, allocating it if
179 * needed. It will also grab the relevant crtc lock to make sure that the state
180 * is consistent.
181 *
182 * Returns:
183 *
184 * Either the allocated state or the error code encoded into the pointer. When
185 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
186 * entire atomic sequence must be restarted. All other errors are fatal.
187 */
188struct drm_crtc_state *
189drm_atomic_get_crtc_state(struct drm_atomic_state *state,
190 struct drm_crtc *crtc)
191{
192 int ret, index;
193 struct drm_crtc_state *crtc_state;
194
195 index = drm_crtc_index(crtc);
196
197 if (state->crtc_states[index])
198 return state->crtc_states[index];
199
200 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
201 if (ret)
202 return ERR_PTR(ret);
203
204 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
205 if (!crtc_state)
206 return ERR_PTR(-ENOMEM);
207
208 state->crtc_states[index] = crtc_state;
209 state->crtcs[index] = crtc;
210 crtc_state->state = state;
211
212 DRM_DEBUG_KMS("Added [CRTC:%d] %p state to %p\n",
213 crtc->base.id, crtc_state, state);
214
215 return crtc_state;
216}
217EXPORT_SYMBOL(drm_atomic_get_crtc_state);
218
219/**
Rob Clark40ecc692014-12-18 16:01:46 -0500220 * drm_atomic_crtc_set_property - set property on CRTC
221 * @crtc: the drm CRTC to set a property on
222 * @state: the state object to update with the new property value
223 * @property: the property to set
224 * @val: the new property value
225 *
226 * Use this instead of calling crtc->atomic_set_property directly.
227 * This function handles generic/core properties and calls out to
228 * driver's ->atomic_set_property() for driver properties. To ensure
229 * consistent behavior you must call this function rather than the
230 * driver hook directly.
231 *
232 * RETURNS:
233 * Zero on success, error code on failure
234 */
235int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
236 struct drm_crtc_state *state, struct drm_property *property,
237 uint64_t val)
238{
239 if (crtc->funcs->atomic_set_property)
240 return crtc->funcs->atomic_set_property(crtc, state, property, val);
241 return -EINVAL;
242}
243EXPORT_SYMBOL(drm_atomic_crtc_set_property);
244
245/**
Rob Clarkac9c9252014-12-18 16:01:47 -0500246 * drm_atomic_crtc_get_property - get property on CRTC
247 * @crtc: the drm CRTC to get a property on
248 * @state: the state object with the property value to read
249 * @property: the property to get
250 * @val: the property value (returned by reference)
251 *
252 * Use this instead of calling crtc->atomic_get_property directly.
253 * This function handles generic/core properties and calls out to
254 * driver's ->atomic_get_property() for driver properties. To ensure
255 * consistent behavior you must call this function rather than the
256 * driver hook directly.
257 *
258 * RETURNS:
259 * Zero on success, error code on failure
260 */
261int drm_atomic_crtc_get_property(struct drm_crtc *crtc,
262 const struct drm_crtc_state *state,
263 struct drm_property *property, uint64_t *val)
264{
265 if (crtc->funcs->atomic_get_property)
266 return crtc->funcs->atomic_get_property(crtc, state, property, val);
267 return -EINVAL;
268}
269EXPORT_SYMBOL(drm_atomic_crtc_get_property);
270
271/**
Rob Clark5e743732014-12-18 16:01:51 -0500272 * drm_atomic_crtc_check - check crtc state
273 * @crtc: crtc to check
274 * @state: crtc state to check
275 *
276 * Provides core sanity checks for crtc state.
277 *
278 * RETURNS:
279 * Zero on success, error code on failure
280 */
281static int drm_atomic_crtc_check(struct drm_crtc *crtc,
282 struct drm_crtc_state *state)
283{
284 /* NOTE: we explicitly don't enforce constraints such as primary
285 * layer covering entire screen, since that is something we want
286 * to allow (on hw that supports it). For hw that does not, it
287 * should be checked in driver's crtc->atomic_check() vfunc.
288 *
289 * TODO: Add generic modeset state checks once we support those.
290 */
291 return 0;
292}
293
294/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200295 * drm_atomic_get_plane_state - get plane state
296 * @state: global atomic state object
297 * @plane: plane to get state object for
298 *
299 * This function returns the plane state for the given plane, allocating it if
300 * needed. It will also grab the relevant plane lock to make sure that the state
301 * is consistent.
302 *
303 * Returns:
304 *
305 * Either the allocated state or the error code encoded into the pointer. When
306 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
307 * entire atomic sequence must be restarted. All other errors are fatal.
308 */
309struct drm_plane_state *
310drm_atomic_get_plane_state(struct drm_atomic_state *state,
311 struct drm_plane *plane)
312{
313 int ret, index;
314 struct drm_plane_state *plane_state;
315
316 index = drm_plane_index(plane);
317
318 if (state->plane_states[index])
319 return state->plane_states[index];
320
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100321 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200322 if (ret)
323 return ERR_PTR(ret);
324
325 plane_state = plane->funcs->atomic_duplicate_state(plane);
326 if (!plane_state)
327 return ERR_PTR(-ENOMEM);
328
329 state->plane_states[index] = plane_state;
330 state->planes[index] = plane;
331 plane_state->state = state;
332
333 DRM_DEBUG_KMS("Added [PLANE:%d] %p state to %p\n",
334 plane->base.id, plane_state, state);
335
336 if (plane_state->crtc) {
337 struct drm_crtc_state *crtc_state;
338
339 crtc_state = drm_atomic_get_crtc_state(state,
340 plane_state->crtc);
341 if (IS_ERR(crtc_state))
342 return ERR_CAST(crtc_state);
343 }
344
345 return plane_state;
346}
347EXPORT_SYMBOL(drm_atomic_get_plane_state);
348
349/**
Rob Clark40ecc692014-12-18 16:01:46 -0500350 * drm_atomic_plane_set_property - set property on plane
351 * @plane: the drm plane to set a property on
352 * @state: the state object to update with the new property value
353 * @property: the property to set
354 * @val: the new property value
355 *
356 * Use this instead of calling plane->atomic_set_property directly.
357 * This function handles generic/core properties and calls out to
358 * driver's ->atomic_set_property() for driver properties. To ensure
359 * consistent behavior you must call this function rather than the
360 * driver hook directly.
361 *
362 * RETURNS:
363 * Zero on success, error code on failure
364 */
365int drm_atomic_plane_set_property(struct drm_plane *plane,
366 struct drm_plane_state *state, struct drm_property *property,
367 uint64_t val)
368{
369 if (plane->funcs->atomic_set_property)
370 return plane->funcs->atomic_set_property(plane, state, property, val);
371 return -EINVAL;
372}
373EXPORT_SYMBOL(drm_atomic_plane_set_property);
374
375/**
Rob Clarkac9c9252014-12-18 16:01:47 -0500376 * drm_atomic_plane_get_property - get property on plane
377 * @plane: the drm plane to get a property on
378 * @state: the state object with the property value to read
379 * @property: the property to get
380 * @val: the property value (returned by reference)
381 *
382 * Use this instead of calling plane->atomic_get_property directly.
383 * This function handles generic/core properties and calls out to
384 * driver's ->atomic_get_property() for driver properties. To ensure
385 * consistent behavior you must call this function rather than the
386 * driver hook directly.
387 *
388 * RETURNS:
389 * Zero on success, error code on failure
390 */
391int drm_atomic_plane_get_property(struct drm_plane *plane,
392 const struct drm_plane_state *state,
393 struct drm_property *property, uint64_t *val)
394{
395 if (plane->funcs->atomic_get_property)
396 return plane->funcs->atomic_get_property(plane, state, property, val);
397 return -EINVAL;
398}
399EXPORT_SYMBOL(drm_atomic_plane_get_property);
400
401/**
Rob Clark5e743732014-12-18 16:01:51 -0500402 * drm_atomic_plane_check - check plane state
403 * @plane: plane to check
404 * @state: plane state to check
405 *
406 * Provides core sanity checks for plane state.
407 *
408 * RETURNS:
409 * Zero on success, error code on failure
410 */
411static int drm_atomic_plane_check(struct drm_plane *plane,
412 struct drm_plane_state *state)
413{
414 unsigned int fb_width, fb_height;
415 unsigned int i;
416
417 /* either *both* CRTC and FB must be set, or neither */
418 if (WARN_ON(state->crtc && !state->fb)) {
419 DRM_DEBUG_KMS("CRTC set but no FB\n");
420 return -EINVAL;
421 } else if (WARN_ON(state->fb && !state->crtc)) {
422 DRM_DEBUG_KMS("FB set but no CRTC\n");
423 return -EINVAL;
424 }
425
426 /* if disabled, we don't care about the rest of the state: */
427 if (!state->crtc)
428 return 0;
429
430 /* Check whether this plane is usable on this CRTC */
431 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
432 DRM_DEBUG_KMS("Invalid crtc for plane\n");
433 return -EINVAL;
434 }
435
436 /* Check whether this plane supports the fb pixel format. */
437 for (i = 0; i < plane->format_count; i++)
438 if (state->fb->pixel_format == plane->format_types[i])
439 break;
440 if (i == plane->format_count) {
441 DRM_DEBUG_KMS("Invalid pixel format %s\n",
442 drm_get_format_name(state->fb->pixel_format));
443 return -EINVAL;
444 }
445
446 /* Give drivers some help against integer overflows */
447 if (state->crtc_w > INT_MAX ||
448 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
449 state->crtc_h > INT_MAX ||
450 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
451 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
452 state->crtc_w, state->crtc_h,
453 state->crtc_x, state->crtc_y);
454 return -ERANGE;
455 }
456
457 fb_width = state->fb->width << 16;
458 fb_height = state->fb->height << 16;
459
460 /* Make sure source coordinates are inside the fb. */
461 if (state->src_w > fb_width ||
462 state->src_x > fb_width - state->src_w ||
463 state->src_h > fb_height ||
464 state->src_y > fb_height - state->src_h) {
465 DRM_DEBUG_KMS("Invalid source coordinates "
466 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
467 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
468 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
469 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
470 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
471 return -ENOSPC;
472 }
473
474 return 0;
475}
476
477/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200478 * drm_atomic_get_connector_state - get connector state
479 * @state: global atomic state object
480 * @connector: connector to get state object for
481 *
482 * This function returns the connector state for the given connector,
483 * allocating it if needed. It will also grab the relevant connector lock to
484 * make sure that the state is consistent.
485 *
486 * Returns:
487 *
488 * Either the allocated state or the error code encoded into the pointer. When
489 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
490 * entire atomic sequence must be restarted. All other errors are fatal.
491 */
492struct drm_connector_state *
493drm_atomic_get_connector_state(struct drm_atomic_state *state,
494 struct drm_connector *connector)
495{
496 int ret, index;
497 struct drm_mode_config *config = &connector->dev->mode_config;
498 struct drm_connector_state *connector_state;
499
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100500 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
501 if (ret)
502 return ERR_PTR(ret);
503
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200504 index = drm_connector_index(connector);
505
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100506 /*
507 * Construction of atomic state updates can race with a connector
508 * hot-add which might overflow. In this case flip the table and just
509 * restart the entire ioctl - no one is fast enough to livelock a cpu
510 * with physical hotplug events anyway.
511 *
512 * Note that we only grab the indexes once we have the right lock to
513 * prevent hotplug/unplugging of connectors. So removal is no problem,
514 * at most the array is a bit too large.
515 */
516 if (index >= state->num_connector) {
517 DRM_DEBUG_KMS("Hot-added connector would overflow state array, restarting\n");
Daniel Vetterfc2d2bc2014-11-20 09:53:35 +0100518 return ERR_PTR(-EAGAIN);
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100519 }
520
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200521 if (state->connector_states[index])
522 return state->connector_states[index];
523
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200524 connector_state = connector->funcs->atomic_duplicate_state(connector);
525 if (!connector_state)
526 return ERR_PTR(-ENOMEM);
527
528 state->connector_states[index] = connector_state;
529 state->connectors[index] = connector;
530 connector_state->state = state;
531
532 DRM_DEBUG_KMS("Added [CONNECTOR:%d] %p state to %p\n",
533 connector->base.id, connector_state, state);
534
535 if (connector_state->crtc) {
536 struct drm_crtc_state *crtc_state;
537
538 crtc_state = drm_atomic_get_crtc_state(state,
539 connector_state->crtc);
540 if (IS_ERR(crtc_state))
541 return ERR_CAST(crtc_state);
542 }
543
544 return connector_state;
545}
546EXPORT_SYMBOL(drm_atomic_get_connector_state);
547
548/**
Rob Clark40ecc692014-12-18 16:01:46 -0500549 * drm_atomic_connector_set_property - set property on connector.
550 * @connector: the drm connector to set a property on
551 * @state: the state object to update with the new property value
552 * @property: the property to set
553 * @val: the new property value
554 *
555 * Use this instead of calling connector->atomic_set_property directly.
556 * This function handles generic/core properties and calls out to
557 * driver's ->atomic_set_property() for driver properties. To ensure
558 * consistent behavior you must call this function rather than the
559 * driver hook directly.
560 *
561 * RETURNS:
562 * Zero on success, error code on failure
563 */
564int drm_atomic_connector_set_property(struct drm_connector *connector,
565 struct drm_connector_state *state, struct drm_property *property,
566 uint64_t val)
567{
568 struct drm_device *dev = connector->dev;
569 struct drm_mode_config *config = &dev->mode_config;
570
571 if (property == config->dpms_property) {
572 /* setting DPMS property requires special handling, which
573 * is done in legacy setprop path for us. Disallow (for
574 * now?) atomic writes to DPMS property:
575 */
576 return -EINVAL;
577 } else if (connector->funcs->atomic_set_property) {
578 return connector->funcs->atomic_set_property(connector,
579 state, property, val);
580 } else {
581 return -EINVAL;
582 }
583}
584EXPORT_SYMBOL(drm_atomic_connector_set_property);
585
586/**
Rob Clarkac9c9252014-12-18 16:01:47 -0500587 * drm_atomic_connector_get_property - get property on connector
588 * @connector: the drm connector to get a property on
589 * @state: the state object with the property value to read
590 * @property: the property to get
591 * @val: the property value (returned by reference)
592 *
593 * Use this instead of calling connector->atomic_get_property directly.
594 * This function handles generic/core properties and calls out to
595 * driver's ->atomic_get_property() for driver properties. To ensure
596 * consistent behavior you must call this function rather than the
597 * driver hook directly.
598 *
599 * RETURNS:
600 * Zero on success, error code on failure
601 */
602int drm_atomic_connector_get_property(struct drm_connector *connector,
603 const struct drm_connector_state *state,
604 struct drm_property *property, uint64_t *val)
605{
606 struct drm_device *dev = connector->dev;
607 struct drm_mode_config *config = &dev->mode_config;
608
609 if (property == config->dpms_property) {
610 *val = connector->dpms;
611 } else if (connector->funcs->atomic_get_property) {
612 return connector->funcs->atomic_get_property(connector,
613 state, property, val);
614 } else {
615 return -EINVAL;
616 }
617
618 return 0;
619}
620EXPORT_SYMBOL(drm_atomic_connector_get_property);
621
622/**
Rob Clark88a48e22014-12-18 16:01:50 -0500623 * drm_atomic_get_property - helper to read atomic property
624 * @obj: drm mode object whose property to read
625 * @property: the property to read
626 * @val: the read value, returned by reference
627 *
628 * RETURNS:
629 * Zero on success, error code on failure
630 */
631int drm_atomic_get_property(struct drm_mode_object *obj,
632 struct drm_property *property, uint64_t *val)
633{
634 struct drm_device *dev = property->dev;
635 int ret;
636
637 switch (obj->type) {
638 case DRM_MODE_OBJECT_CONNECTOR: {
639 struct drm_connector *connector = obj_to_connector(obj);
640 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
641 ret = drm_atomic_connector_get_property(connector,
642 connector->state, property, val);
643 break;
644 }
645 case DRM_MODE_OBJECT_CRTC: {
646 struct drm_crtc *crtc = obj_to_crtc(obj);
647 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
648 ret = drm_atomic_crtc_get_property(crtc,
649 crtc->state, property, val);
650 break;
651 }
652 case DRM_MODE_OBJECT_PLANE: {
653 struct drm_plane *plane = obj_to_plane(obj);
654 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
655 ret = drm_atomic_plane_get_property(plane,
656 plane->state, property, val);
657 break;
658 }
659 default:
660 ret = -EINVAL;
661 break;
662 }
663
664 return ret;
665}
666
667/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200668 * drm_atomic_set_crtc_for_plane - set crtc for plane
Daniel Vetter07cc0ef2014-11-27 15:49:39 +0100669 * @plane_state: the plane whose incoming state to update
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200670 * @crtc: crtc to use for the plane
671 *
672 * Changing the assigned crtc for a plane requires us to grab the lock and state
673 * for the new crtc, as needed. This function takes care of all these details
674 * besides updating the pointer in the state object itself.
675 *
676 * Returns:
677 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
678 * then the w/w mutex code has detected a deadlock and the entire atomic
679 * sequence must be restarted. All other errors are fatal.
680 */
681int
Daniel Vetter07cc0ef2014-11-27 15:49:39 +0100682drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
683 struct drm_crtc *crtc)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200684{
Daniel Vetter07cc0ef2014-11-27 15:49:39 +0100685 struct drm_plane *plane = plane_state->plane;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200686 struct drm_crtc_state *crtc_state;
687
Rob Clark6ddd3882014-11-21 15:28:31 -0500688 if (plane_state->crtc) {
689 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
690 plane_state->crtc);
691 if (WARN_ON(IS_ERR(crtc_state)))
692 return PTR_ERR(crtc_state);
693
694 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
695 }
696
697 plane_state->crtc = crtc;
698
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200699 if (crtc) {
700 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
701 crtc);
702 if (IS_ERR(crtc_state))
703 return PTR_ERR(crtc_state);
Rob Clark6ddd3882014-11-21 15:28:31 -0500704 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200705 }
706
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200707 if (crtc)
708 DRM_DEBUG_KMS("Link plane state %p to [CRTC:%d]\n",
709 plane_state, crtc->base.id);
710 else
711 DRM_DEBUG_KMS("Link plane state %p to [NOCRTC]\n", plane_state);
712
713 return 0;
714}
715EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
716
717/**
Daniel Vetter321ebf02014-11-04 22:57:27 +0100718 * drm_atomic_set_fb_for_plane - set crtc for plane
719 * @plane_state: atomic state object for the plane
720 * @fb: fb to use for the plane
721 *
722 * Changing the assigned framebuffer for a plane requires us to grab a reference
723 * to the new fb and drop the reference to the old fb, if there is one. This
724 * function takes care of all these details besides updating the pointer in the
725 * state object itself.
726 */
727void
728drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
729 struct drm_framebuffer *fb)
730{
731 if (plane_state->fb)
732 drm_framebuffer_unreference(plane_state->fb);
733 if (fb)
734 drm_framebuffer_reference(fb);
735 plane_state->fb = fb;
736
737 if (fb)
738 DRM_DEBUG_KMS("Set [FB:%d] for plane state %p\n",
739 fb->base.id, plane_state);
740 else
741 DRM_DEBUG_KMS("Set [NOFB] for plane state %p\n", plane_state);
742}
743EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
744
745/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200746 * drm_atomic_set_crtc_for_connector - set crtc for connector
747 * @conn_state: atomic state object for the connector
748 * @crtc: crtc to use for the connector
749 *
750 * Changing the assigned crtc for a connector requires us to grab the lock and
751 * state for the new crtc, as needed. This function takes care of all these
752 * details besides updating the pointer in the state object itself.
753 *
754 * Returns:
755 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
756 * then the w/w mutex code has detected a deadlock and the entire atomic
757 * sequence must be restarted. All other errors are fatal.
758 */
759int
760drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
761 struct drm_crtc *crtc)
762{
763 struct drm_crtc_state *crtc_state;
764
765 if (crtc) {
766 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
767 if (IS_ERR(crtc_state))
768 return PTR_ERR(crtc_state);
769 }
770
771 conn_state->crtc = crtc;
772
773 if (crtc)
774 DRM_DEBUG_KMS("Link connector state %p to [CRTC:%d]\n",
775 conn_state, crtc->base.id);
776 else
777 DRM_DEBUG_KMS("Link connector state %p to [NOCRTC]\n",
778 conn_state);
779
780 return 0;
781}
782EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
783
784/**
785 * drm_atomic_add_affected_connectors - add connectors for crtc
786 * @state: atomic state
787 * @crtc: DRM crtc
788 *
789 * This function walks the current configuration and adds all connectors
790 * currently using @crtc to the atomic configuration @state. Note that this
791 * function must acquire the connection mutex. This can potentially cause
792 * unneeded seralization if the update is just for the planes on one crtc. Hence
793 * drivers and helpers should only call this when really needed (e.g. when a
794 * full modeset needs to happen due to some change).
795 *
796 * Returns:
797 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
798 * then the w/w mutex code has detected a deadlock and the entire atomic
799 * sequence must be restarted. All other errors are fatal.
800 */
801int
802drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
803 struct drm_crtc *crtc)
804{
805 struct drm_mode_config *config = &state->dev->mode_config;
806 struct drm_connector *connector;
807 struct drm_connector_state *conn_state;
808 int ret;
809
810 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
811 if (ret)
812 return ret;
813
814 DRM_DEBUG_KMS("Adding all current connectors for [CRTC:%d] to %p\n",
815 crtc->base.id, state);
816
817 /*
818 * Changed connectors are already in @state, so only need to look at the
819 * current configuration.
820 */
821 list_for_each_entry(connector, &config->connector_list, head) {
822 if (connector->state->crtc != crtc)
823 continue;
824
825 conn_state = drm_atomic_get_connector_state(state, connector);
826 if (IS_ERR(conn_state))
827 return PTR_ERR(conn_state);
828 }
829
830 return 0;
831}
832EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
833
834/**
835 * drm_atomic_connectors_for_crtc - count number of connected outputs
836 * @state: atomic state
837 * @crtc: DRM crtc
838 *
839 * This function counts all connectors which will be connected to @crtc
840 * according to @state. Useful to recompute the enable state for @crtc.
841 */
842int
843drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
844 struct drm_crtc *crtc)
845{
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200846 int i, num_connected_connectors = 0;
847
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100848 for (i = 0; i < state->num_connector; i++) {
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200849 struct drm_connector_state *conn_state;
850
851 conn_state = state->connector_states[i];
852
853 if (conn_state && conn_state->crtc == crtc)
854 num_connected_connectors++;
855 }
856
857 DRM_DEBUG_KMS("State %p has %i connectors for [CRTC:%d]\n",
858 state, num_connected_connectors, crtc->base.id);
859
860 return num_connected_connectors;
861}
862EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
863
864/**
865 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
866 * @state: atomic state
867 *
868 * This function should be used by legacy entry points which don't understand
869 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
870 * the slowpath completed.
871 */
872void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
873{
874 int ret;
875
876retry:
877 drm_modeset_backoff(state->acquire_ctx);
878
879 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
880 state->acquire_ctx);
881 if (ret)
882 goto retry;
883 ret = drm_modeset_lock_all_crtcs(state->dev,
884 state->acquire_ctx);
885 if (ret)
886 goto retry;
887}
888EXPORT_SYMBOL(drm_atomic_legacy_backoff);
889
890/**
891 * drm_atomic_check_only - check whether a given config would work
892 * @state: atomic configuration to check
893 *
894 * Note that this function can return -EDEADLK if the driver needed to acquire
895 * more locks but encountered a deadlock. The caller must then do the usual w/w
896 * backoff dance and restart. All other errors are fatal.
897 *
898 * Returns:
899 * 0 on success, negative error code on failure.
900 */
901int drm_atomic_check_only(struct drm_atomic_state *state)
902{
Rob Clark5e743732014-12-18 16:01:51 -0500903 struct drm_device *dev = state->dev;
904 struct drm_mode_config *config = &dev->mode_config;
905 int nplanes = config->num_total_plane;
906 int ncrtcs = config->num_crtc;
907 int i, ret = 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200908
909 DRM_DEBUG_KMS("checking %p\n", state);
910
Rob Clark5e743732014-12-18 16:01:51 -0500911 for (i = 0; i < nplanes; i++) {
912 struct drm_plane *plane = state->planes[i];
913
914 if (!plane)
915 continue;
916
917 ret = drm_atomic_plane_check(plane, state->plane_states[i]);
918 if (ret) {
919 DRM_DEBUG_KMS("[PLANE:%d] atomic core check failed\n",
920 plane->base.id);
921 return ret;
922 }
923 }
924
925 for (i = 0; i < ncrtcs; i++) {
926 struct drm_crtc *crtc = state->crtcs[i];
927
928 if (!crtc)
929 continue;
930
931 ret = drm_atomic_crtc_check(crtc, state->crtc_states[i]);
932 if (ret) {
933 DRM_DEBUG_KMS("[CRTC:%d] atomic core check failed\n",
934 crtc->base.id);
935 return ret;
936 }
937 }
938
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200939 if (config->funcs->atomic_check)
Rob Clark5e743732014-12-18 16:01:51 -0500940 ret = config->funcs->atomic_check(state->dev, state);
941
942 return ret;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200943}
944EXPORT_SYMBOL(drm_atomic_check_only);
945
946/**
947 * drm_atomic_commit - commit configuration atomically
948 * @state: atomic configuration to check
949 *
950 * Note that this function can return -EDEADLK if the driver needed to acquire
951 * more locks but encountered a deadlock. The caller must then do the usual w/w
952 * backoff dance and restart. All other errors are fatal.
953 *
954 * Also note that on successful execution ownership of @state is transferred
955 * from the caller of this function to the function itself. The caller must not
956 * free or in any other way access @state. If the function fails then the caller
957 * must clean up @state itself.
958 *
959 * Returns:
960 * 0 on success, negative error code on failure.
961 */
962int drm_atomic_commit(struct drm_atomic_state *state)
963{
964 struct drm_mode_config *config = &state->dev->mode_config;
965 int ret;
966
967 ret = drm_atomic_check_only(state);
968 if (ret)
969 return ret;
970
971 DRM_DEBUG_KMS("commiting %p\n", state);
972
973 return config->funcs->atomic_commit(state->dev, state, false);
974}
975EXPORT_SYMBOL(drm_atomic_commit);
976
977/**
978 * drm_atomic_async_commit - atomic&async configuration commit
979 * @state: atomic configuration to check
980 *
981 * Note that this function can return -EDEADLK if the driver needed to acquire
982 * more locks but encountered a deadlock. The caller must then do the usual w/w
983 * backoff dance and restart. All other errors are fatal.
984 *
985 * Also note that on successful execution ownership of @state is transferred
986 * from the caller of this function to the function itself. The caller must not
987 * free or in any other way access @state. If the function fails then the caller
988 * must clean up @state itself.
989 *
990 * Returns:
991 * 0 on success, negative error code on failure.
992 */
993int drm_atomic_async_commit(struct drm_atomic_state *state)
994{
995 struct drm_mode_config *config = &state->dev->mode_config;
996 int ret;
997
998 ret = drm_atomic_check_only(state);
999 if (ret)
1000 return ret;
1001
1002 DRM_DEBUG_KMS("commiting %p asynchronously\n", state);
1003
1004 return config->funcs->atomic_commit(state->dev, state, true);
1005}
1006EXPORT_SYMBOL(drm_atomic_async_commit);