drm: tweak getconnector locking
[deliverable/linux.git] / drivers / gpu / drm / drm_atomic.c
CommitLineData
cc4ceb48
DV
1/*
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
f52b69f1
DV
59 state->num_connector = ACCESS_ONCE(dev->mode_config.num_connector);
60
cc4ceb48
DV
61 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;
f52b69f1 77 state->connectors = kcalloc(state->num_connector,
cc4ceb48
DV
78 sizeof(*state->connectors),
79 GFP_KERNEL);
80 if (!state->connectors)
81 goto fail;
f52b69f1 82 state->connector_states = kcalloc(state->num_connector,
cc4ceb48
DV
83 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;
6f75cea6 117 struct drm_mode_config *config = &dev->mode_config;
cc4ceb48
DV
118 int i;
119
120 DRM_DEBUG_KMS("Clearing atomic state %p\n", state);
121
f52b69f1 122 for (i = 0; i < state->num_connector; i++) {
cc4ceb48
DV
123 struct drm_connector *connector = state->connectors[i];
124
125 if (!connector)
126 continue;
127
6f75cea6
DV
128 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
129
cc4ceb48
DV
130 connector->funcs->atomic_destroy_state(connector,
131 state->connector_states[i]);
132 }
133
6f75cea6 134 for (i = 0; i < config->num_crtc; i++) {
cc4ceb48
DV
135 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
6f75cea6 144 for (i = 0; i < config->num_total_plane; i++) {
cc4ceb48
DV
145 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
40ecc694
RC
219/**
220 * 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
ac9c9256
RC
245/**
246 * 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
cc4ceb48
DV
271/**
272 * drm_atomic_get_plane_state - get plane state
273 * @state: global atomic state object
274 * @plane: plane to get state object for
275 *
276 * This function returns the plane state for the given plane, allocating it if
277 * needed. It will also grab the relevant plane lock to make sure that the state
278 * is consistent.
279 *
280 * Returns:
281 *
282 * Either the allocated state or the error code encoded into the pointer. When
283 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
284 * entire atomic sequence must be restarted. All other errors are fatal.
285 */
286struct drm_plane_state *
287drm_atomic_get_plane_state(struct drm_atomic_state *state,
288 struct drm_plane *plane)
289{
290 int ret, index;
291 struct drm_plane_state *plane_state;
292
293 index = drm_plane_index(plane);
294
295 if (state->plane_states[index])
296 return state->plane_states[index];
297
4d02e2de 298 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
cc4ceb48
DV
299 if (ret)
300 return ERR_PTR(ret);
301
302 plane_state = plane->funcs->atomic_duplicate_state(plane);
303 if (!plane_state)
304 return ERR_PTR(-ENOMEM);
305
306 state->plane_states[index] = plane_state;
307 state->planes[index] = plane;
308 plane_state->state = state;
309
310 DRM_DEBUG_KMS("Added [PLANE:%d] %p state to %p\n",
311 plane->base.id, plane_state, state);
312
313 if (plane_state->crtc) {
314 struct drm_crtc_state *crtc_state;
315
316 crtc_state = drm_atomic_get_crtc_state(state,
317 plane_state->crtc);
318 if (IS_ERR(crtc_state))
319 return ERR_CAST(crtc_state);
320 }
321
322 return plane_state;
323}
324EXPORT_SYMBOL(drm_atomic_get_plane_state);
325
40ecc694
RC
326/**
327 * drm_atomic_plane_set_property - set property on plane
328 * @plane: the drm plane to set a property on
329 * @state: the state object to update with the new property value
330 * @property: the property to set
331 * @val: the new property value
332 *
333 * Use this instead of calling plane->atomic_set_property directly.
334 * This function handles generic/core properties and calls out to
335 * driver's ->atomic_set_property() for driver properties. To ensure
336 * consistent behavior you must call this function rather than the
337 * driver hook directly.
338 *
339 * RETURNS:
340 * Zero on success, error code on failure
341 */
342int drm_atomic_plane_set_property(struct drm_plane *plane,
343 struct drm_plane_state *state, struct drm_property *property,
344 uint64_t val)
345{
346 if (plane->funcs->atomic_set_property)
347 return plane->funcs->atomic_set_property(plane, state, property, val);
348 return -EINVAL;
349}
350EXPORT_SYMBOL(drm_atomic_plane_set_property);
351
ac9c9256
RC
352/**
353 * drm_atomic_plane_get_property - get property on plane
354 * @plane: the drm plane to get a property on
355 * @state: the state object with the property value to read
356 * @property: the property to get
357 * @val: the property value (returned by reference)
358 *
359 * Use this instead of calling plane->atomic_get_property directly.
360 * This function handles generic/core properties and calls out to
361 * driver's ->atomic_get_property() for driver properties. To ensure
362 * consistent behavior you must call this function rather than the
363 * driver hook directly.
364 *
365 * RETURNS:
366 * Zero on success, error code on failure
367 */
368int drm_atomic_plane_get_property(struct drm_plane *plane,
369 const struct drm_plane_state *state,
370 struct drm_property *property, uint64_t *val)
371{
372 if (plane->funcs->atomic_get_property)
373 return plane->funcs->atomic_get_property(plane, state, property, val);
374 return -EINVAL;
375}
376EXPORT_SYMBOL(drm_atomic_plane_get_property);
377
cc4ceb48
DV
378/**
379 * drm_atomic_get_connector_state - get connector state
380 * @state: global atomic state object
381 * @connector: connector to get state object for
382 *
383 * This function returns the connector state for the given connector,
384 * allocating it if needed. It will also grab the relevant connector lock to
385 * make sure that the state is consistent.
386 *
387 * Returns:
388 *
389 * Either the allocated state or the error code encoded into the pointer. When
390 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
391 * entire atomic sequence must be restarted. All other errors are fatal.
392 */
393struct drm_connector_state *
394drm_atomic_get_connector_state(struct drm_atomic_state *state,
395 struct drm_connector *connector)
396{
397 int ret, index;
398 struct drm_mode_config *config = &connector->dev->mode_config;
399 struct drm_connector_state *connector_state;
400
c7eb76f4
DV
401 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
402 if (ret)
403 return ERR_PTR(ret);
404
cc4ceb48
DV
405 index = drm_connector_index(connector);
406
f52b69f1
DV
407 /*
408 * Construction of atomic state updates can race with a connector
409 * hot-add which might overflow. In this case flip the table and just
410 * restart the entire ioctl - no one is fast enough to livelock a cpu
411 * with physical hotplug events anyway.
412 *
413 * Note that we only grab the indexes once we have the right lock to
414 * prevent hotplug/unplugging of connectors. So removal is no problem,
415 * at most the array is a bit too large.
416 */
417 if (index >= state->num_connector) {
418 DRM_DEBUG_KMS("Hot-added connector would overflow state array, restarting\n");
fc2d2bc1 419 return ERR_PTR(-EAGAIN);
f52b69f1
DV
420 }
421
cc4ceb48
DV
422 if (state->connector_states[index])
423 return state->connector_states[index];
424
cc4ceb48
DV
425 connector_state = connector->funcs->atomic_duplicate_state(connector);
426 if (!connector_state)
427 return ERR_PTR(-ENOMEM);
428
429 state->connector_states[index] = connector_state;
430 state->connectors[index] = connector;
431 connector_state->state = state;
432
433 DRM_DEBUG_KMS("Added [CONNECTOR:%d] %p state to %p\n",
434 connector->base.id, connector_state, state);
435
436 if (connector_state->crtc) {
437 struct drm_crtc_state *crtc_state;
438
439 crtc_state = drm_atomic_get_crtc_state(state,
440 connector_state->crtc);
441 if (IS_ERR(crtc_state))
442 return ERR_CAST(crtc_state);
443 }
444
445 return connector_state;
446}
447EXPORT_SYMBOL(drm_atomic_get_connector_state);
448
40ecc694
RC
449/**
450 * drm_atomic_connector_set_property - set property on connector.
451 * @connector: the drm connector to set a property on
452 * @state: the state object to update with the new property value
453 * @property: the property to set
454 * @val: the new property value
455 *
456 * Use this instead of calling connector->atomic_set_property directly.
457 * This function handles generic/core properties and calls out to
458 * driver's ->atomic_set_property() for driver properties. To ensure
459 * consistent behavior you must call this function rather than the
460 * driver hook directly.
461 *
462 * RETURNS:
463 * Zero on success, error code on failure
464 */
465int drm_atomic_connector_set_property(struct drm_connector *connector,
466 struct drm_connector_state *state, struct drm_property *property,
467 uint64_t val)
468{
469 struct drm_device *dev = connector->dev;
470 struct drm_mode_config *config = &dev->mode_config;
471
472 if (property == config->dpms_property) {
473 /* setting DPMS property requires special handling, which
474 * is done in legacy setprop path for us. Disallow (for
475 * now?) atomic writes to DPMS property:
476 */
477 return -EINVAL;
478 } else if (connector->funcs->atomic_set_property) {
479 return connector->funcs->atomic_set_property(connector,
480 state, property, val);
481 } else {
482 return -EINVAL;
483 }
484}
485EXPORT_SYMBOL(drm_atomic_connector_set_property);
486
ac9c9256
RC
487/**
488 * drm_atomic_connector_get_property - get property on connector
489 * @connector: the drm connector to get a property on
490 * @state: the state object with the property value to read
491 * @property: the property to get
492 * @val: the property value (returned by reference)
493 *
494 * Use this instead of calling connector->atomic_get_property directly.
495 * This function handles generic/core properties and calls out to
496 * driver's ->atomic_get_property() for driver properties. To ensure
497 * consistent behavior you must call this function rather than the
498 * driver hook directly.
499 *
500 * RETURNS:
501 * Zero on success, error code on failure
502 */
503int drm_atomic_connector_get_property(struct drm_connector *connector,
504 const struct drm_connector_state *state,
505 struct drm_property *property, uint64_t *val)
506{
507 struct drm_device *dev = connector->dev;
508 struct drm_mode_config *config = &dev->mode_config;
509
510 if (property == config->dpms_property) {
511 *val = connector->dpms;
512 } else if (connector->funcs->atomic_get_property) {
513 return connector->funcs->atomic_get_property(connector,
514 state, property, val);
515 } else {
516 return -EINVAL;
517 }
518
519 return 0;
520}
521EXPORT_SYMBOL(drm_atomic_connector_get_property);
522
cc4ceb48
DV
523/**
524 * drm_atomic_set_crtc_for_plane - set crtc for plane
07cc0ef6 525 * @plane_state: the plane whose incoming state to update
cc4ceb48
DV
526 * @crtc: crtc to use for the plane
527 *
528 * Changing the assigned crtc for a plane requires us to grab the lock and state
529 * for the new crtc, as needed. This function takes care of all these details
530 * besides updating the pointer in the state object itself.
531 *
532 * Returns:
533 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
534 * then the w/w mutex code has detected a deadlock and the entire atomic
535 * sequence must be restarted. All other errors are fatal.
536 */
537int
07cc0ef6
DV
538drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
539 struct drm_crtc *crtc)
cc4ceb48 540{
07cc0ef6 541 struct drm_plane *plane = plane_state->plane;
cc4ceb48
DV
542 struct drm_crtc_state *crtc_state;
543
6ddd388a
RC
544 if (plane_state->crtc) {
545 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
546 plane_state->crtc);
547 if (WARN_ON(IS_ERR(crtc_state)))
548 return PTR_ERR(crtc_state);
549
550 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
551 }
552
553 plane_state->crtc = crtc;
554
cc4ceb48
DV
555 if (crtc) {
556 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
557 crtc);
558 if (IS_ERR(crtc_state))
559 return PTR_ERR(crtc_state);
6ddd388a 560 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
cc4ceb48
DV
561 }
562
cc4ceb48
DV
563 if (crtc)
564 DRM_DEBUG_KMS("Link plane state %p to [CRTC:%d]\n",
565 plane_state, crtc->base.id);
566 else
567 DRM_DEBUG_KMS("Link plane state %p to [NOCRTC]\n", plane_state);
568
569 return 0;
570}
571EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
572
321ebf04
DV
573/**
574 * drm_atomic_set_fb_for_plane - set crtc for plane
575 * @plane_state: atomic state object for the plane
576 * @fb: fb to use for the plane
577 *
578 * Changing the assigned framebuffer for a plane requires us to grab a reference
579 * to the new fb and drop the reference to the old fb, if there is one. This
580 * function takes care of all these details besides updating the pointer in the
581 * state object itself.
582 */
583void
584drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
585 struct drm_framebuffer *fb)
586{
587 if (plane_state->fb)
588 drm_framebuffer_unreference(plane_state->fb);
589 if (fb)
590 drm_framebuffer_reference(fb);
591 plane_state->fb = fb;
592
593 if (fb)
594 DRM_DEBUG_KMS("Set [FB:%d] for plane state %p\n",
595 fb->base.id, plane_state);
596 else
597 DRM_DEBUG_KMS("Set [NOFB] for plane state %p\n", plane_state);
598}
599EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
600
cc4ceb48
DV
601/**
602 * drm_atomic_set_crtc_for_connector - set crtc for connector
603 * @conn_state: atomic state object for the connector
604 * @crtc: crtc to use for the connector
605 *
606 * Changing the assigned crtc for a connector requires us to grab the lock and
607 * state for the new crtc, as needed. This function takes care of all these
608 * details besides updating the pointer in the state object itself.
609 *
610 * Returns:
611 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
612 * then the w/w mutex code has detected a deadlock and the entire atomic
613 * sequence must be restarted. All other errors are fatal.
614 */
615int
616drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
617 struct drm_crtc *crtc)
618{
619 struct drm_crtc_state *crtc_state;
620
621 if (crtc) {
622 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
623 if (IS_ERR(crtc_state))
624 return PTR_ERR(crtc_state);
625 }
626
627 conn_state->crtc = crtc;
628
629 if (crtc)
630 DRM_DEBUG_KMS("Link connector state %p to [CRTC:%d]\n",
631 conn_state, crtc->base.id);
632 else
633 DRM_DEBUG_KMS("Link connector state %p to [NOCRTC]\n",
634 conn_state);
635
636 return 0;
637}
638EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
639
640/**
641 * drm_atomic_add_affected_connectors - add connectors for crtc
642 * @state: atomic state
643 * @crtc: DRM crtc
644 *
645 * This function walks the current configuration and adds all connectors
646 * currently using @crtc to the atomic configuration @state. Note that this
647 * function must acquire the connection mutex. This can potentially cause
648 * unneeded seralization if the update is just for the planes on one crtc. Hence
649 * drivers and helpers should only call this when really needed (e.g. when a
650 * full modeset needs to happen due to some change).
651 *
652 * Returns:
653 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
654 * then the w/w mutex code has detected a deadlock and the entire atomic
655 * sequence must be restarted. All other errors are fatal.
656 */
657int
658drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
659 struct drm_crtc *crtc)
660{
661 struct drm_mode_config *config = &state->dev->mode_config;
662 struct drm_connector *connector;
663 struct drm_connector_state *conn_state;
664 int ret;
665
666 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
667 if (ret)
668 return ret;
669
670 DRM_DEBUG_KMS("Adding all current connectors for [CRTC:%d] to %p\n",
671 crtc->base.id, state);
672
673 /*
674 * Changed connectors are already in @state, so only need to look at the
675 * current configuration.
676 */
677 list_for_each_entry(connector, &config->connector_list, head) {
678 if (connector->state->crtc != crtc)
679 continue;
680
681 conn_state = drm_atomic_get_connector_state(state, connector);
682 if (IS_ERR(conn_state))
683 return PTR_ERR(conn_state);
684 }
685
686 return 0;
687}
688EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
689
690/**
691 * drm_atomic_connectors_for_crtc - count number of connected outputs
692 * @state: atomic state
693 * @crtc: DRM crtc
694 *
695 * This function counts all connectors which will be connected to @crtc
696 * according to @state. Useful to recompute the enable state for @crtc.
697 */
698int
699drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
700 struct drm_crtc *crtc)
701{
cc4ceb48
DV
702 int i, num_connected_connectors = 0;
703
f52b69f1 704 for (i = 0; i < state->num_connector; i++) {
cc4ceb48
DV
705 struct drm_connector_state *conn_state;
706
707 conn_state = state->connector_states[i];
708
709 if (conn_state && conn_state->crtc == crtc)
710 num_connected_connectors++;
711 }
712
713 DRM_DEBUG_KMS("State %p has %i connectors for [CRTC:%d]\n",
714 state, num_connected_connectors, crtc->base.id);
715
716 return num_connected_connectors;
717}
718EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
719
720/**
721 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
722 * @state: atomic state
723 *
724 * This function should be used by legacy entry points which don't understand
725 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
726 * the slowpath completed.
727 */
728void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
729{
730 int ret;
731
732retry:
733 drm_modeset_backoff(state->acquire_ctx);
734
735 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
736 state->acquire_ctx);
737 if (ret)
738 goto retry;
739 ret = drm_modeset_lock_all_crtcs(state->dev,
740 state->acquire_ctx);
741 if (ret)
742 goto retry;
743}
744EXPORT_SYMBOL(drm_atomic_legacy_backoff);
745
746/**
747 * drm_atomic_check_only - check whether a given config would work
748 * @state: atomic configuration to check
749 *
750 * Note that this function can return -EDEADLK if the driver needed to acquire
751 * more locks but encountered a deadlock. The caller must then do the usual w/w
752 * backoff dance and restart. All other errors are fatal.
753 *
754 * Returns:
755 * 0 on success, negative error code on failure.
756 */
757int drm_atomic_check_only(struct drm_atomic_state *state)
758{
759 struct drm_mode_config *config = &state->dev->mode_config;
760
761 DRM_DEBUG_KMS("checking %p\n", state);
762
763 if (config->funcs->atomic_check)
764 return config->funcs->atomic_check(state->dev, state);
765 else
766 return 0;
767}
768EXPORT_SYMBOL(drm_atomic_check_only);
769
770/**
771 * drm_atomic_commit - commit configuration atomically
772 * @state: atomic configuration to check
773 *
774 * Note that this function can return -EDEADLK if the driver needed to acquire
775 * more locks but encountered a deadlock. The caller must then do the usual w/w
776 * backoff dance and restart. All other errors are fatal.
777 *
778 * Also note that on successful execution ownership of @state is transferred
779 * from the caller of this function to the function itself. The caller must not
780 * free or in any other way access @state. If the function fails then the caller
781 * must clean up @state itself.
782 *
783 * Returns:
784 * 0 on success, negative error code on failure.
785 */
786int drm_atomic_commit(struct drm_atomic_state *state)
787{
788 struct drm_mode_config *config = &state->dev->mode_config;
789 int ret;
790
791 ret = drm_atomic_check_only(state);
792 if (ret)
793 return ret;
794
795 DRM_DEBUG_KMS("commiting %p\n", state);
796
797 return config->funcs->atomic_commit(state->dev, state, false);
798}
799EXPORT_SYMBOL(drm_atomic_commit);
800
801/**
802 * drm_atomic_async_commit - atomic&async configuration commit
803 * @state: atomic configuration to check
804 *
805 * Note that this function can return -EDEADLK if the driver needed to acquire
806 * more locks but encountered a deadlock. The caller must then do the usual w/w
807 * backoff dance and restart. All other errors are fatal.
808 *
809 * Also note that on successful execution ownership of @state is transferred
810 * from the caller of this function to the function itself. The caller must not
811 * free or in any other way access @state. If the function fails then the caller
812 * must clean up @state itself.
813 *
814 * Returns:
815 * 0 on success, negative error code on failure.
816 */
817int drm_atomic_async_commit(struct drm_atomic_state *state)
818{
819 struct drm_mode_config *config = &state->dev->mode_config;
820 int ret;
821
822 ret = drm_atomic_check_only(state);
823 if (ret)
824 return ret;
825
826 DRM_DEBUG_KMS("commiting %p asynchronously\n", state);
827
828 return config->funcs->atomic_commit(state->dev, state, true);
829}
830EXPORT_SYMBOL(drm_atomic_async_commit);
This page took 0.097195 seconds and 5 git commands to generate.