Commit | Line | Data |
---|---|---|
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 | ||
036ef573 ML |
33 | /** |
34 | * drm_atomic_state_default_release - | |
35 | * release memory initialized by drm_atomic_state_init | |
36 | * @state: atomic state | |
37 | * | |
38 | * Free all the memory allocated by drm_atomic_state_init. | |
39 | * This is useful for drivers that subclass the atomic state. | |
40 | */ | |
41 | void drm_atomic_state_default_release(struct drm_atomic_state *state) | |
cc4ceb48 DV |
42 | { |
43 | kfree(state->connectors); | |
44 | kfree(state->connector_states); | |
45 | kfree(state->crtcs); | |
46 | kfree(state->crtc_states); | |
47 | kfree(state->planes); | |
48 | kfree(state->plane_states); | |
cc4ceb48 | 49 | } |
036ef573 | 50 | EXPORT_SYMBOL(drm_atomic_state_default_release); |
cc4ceb48 DV |
51 | |
52 | /** | |
036ef573 | 53 | * drm_atomic_state_init - init new atomic state |
cc4ceb48 | 54 | * @dev: DRM device |
036ef573 | 55 | * @state: atomic state |
cc4ceb48 | 56 | * |
036ef573 ML |
57 | * Default implementation for filling in a new atomic state. |
58 | * This is useful for drivers that subclass the atomic state. | |
cc4ceb48 | 59 | */ |
036ef573 ML |
60 | int |
61 | drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state) | |
cc4ceb48 | 62 | { |
d34f20d6 RC |
63 | /* TODO legacy paths should maybe do a better job about |
64 | * setting this appropriately? | |
65 | */ | |
66 | state->allow_modeset = true; | |
67 | ||
f52b69f1 DV |
68 | state->num_connector = ACCESS_ONCE(dev->mode_config.num_connector); |
69 | ||
cc4ceb48 DV |
70 | state->crtcs = kcalloc(dev->mode_config.num_crtc, |
71 | sizeof(*state->crtcs), GFP_KERNEL); | |
72 | if (!state->crtcs) | |
73 | goto fail; | |
74 | state->crtc_states = kcalloc(dev->mode_config.num_crtc, | |
75 | sizeof(*state->crtc_states), GFP_KERNEL); | |
76 | if (!state->crtc_states) | |
77 | goto fail; | |
78 | state->planes = kcalloc(dev->mode_config.num_total_plane, | |
79 | sizeof(*state->planes), GFP_KERNEL); | |
80 | if (!state->planes) | |
81 | goto fail; | |
82 | state->plane_states = kcalloc(dev->mode_config.num_total_plane, | |
83 | sizeof(*state->plane_states), GFP_KERNEL); | |
84 | if (!state->plane_states) | |
85 | goto fail; | |
f52b69f1 | 86 | state->connectors = kcalloc(state->num_connector, |
cc4ceb48 DV |
87 | sizeof(*state->connectors), |
88 | GFP_KERNEL); | |
89 | if (!state->connectors) | |
90 | goto fail; | |
f52b69f1 | 91 | state->connector_states = kcalloc(state->num_connector, |
cc4ceb48 DV |
92 | sizeof(*state->connector_states), |
93 | GFP_KERNEL); | |
94 | if (!state->connector_states) | |
95 | goto fail; | |
96 | ||
97 | state->dev = dev; | |
98 | ||
036ef573 | 99 | DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state); |
cc4ceb48 | 100 | |
036ef573 | 101 | return 0; |
cc4ceb48 | 102 | fail: |
036ef573 ML |
103 | drm_atomic_state_default_release(state); |
104 | return -ENOMEM; | |
105 | } | |
106 | EXPORT_SYMBOL(drm_atomic_state_init); | |
107 | ||
108 | /** | |
109 | * drm_atomic_state_alloc - allocate atomic state | |
110 | * @dev: DRM device | |
111 | * | |
112 | * This allocates an empty atomic state to track updates. | |
113 | */ | |
114 | struct drm_atomic_state * | |
115 | drm_atomic_state_alloc(struct drm_device *dev) | |
116 | { | |
117 | struct drm_mode_config *config = &dev->mode_config; | |
118 | struct drm_atomic_state *state; | |
119 | ||
120 | if (!config->funcs->atomic_state_alloc) { | |
121 | state = kzalloc(sizeof(*state), GFP_KERNEL); | |
122 | if (!state) | |
123 | return NULL; | |
124 | if (drm_atomic_state_init(dev, state) < 0) { | |
125 | kfree(state); | |
126 | return NULL; | |
127 | } | |
128 | return state; | |
129 | } | |
cc4ceb48 | 130 | |
036ef573 | 131 | return config->funcs->atomic_state_alloc(dev); |
cc4ceb48 DV |
132 | } |
133 | EXPORT_SYMBOL(drm_atomic_state_alloc); | |
134 | ||
135 | /** | |
036ef573 | 136 | * drm_atomic_state_default_clear - clear base atomic state |
cc4ceb48 DV |
137 | * @state: atomic state |
138 | * | |
036ef573 ML |
139 | * Default implementation for clearing atomic state. |
140 | * This is useful for drivers that subclass the atomic state. | |
cc4ceb48 | 141 | */ |
036ef573 | 142 | void drm_atomic_state_default_clear(struct drm_atomic_state *state) |
cc4ceb48 DV |
143 | { |
144 | struct drm_device *dev = state->dev; | |
6f75cea6 | 145 | struct drm_mode_config *config = &dev->mode_config; |
cc4ceb48 DV |
146 | int i; |
147 | ||
17a38d9c | 148 | DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state); |
cc4ceb48 | 149 | |
f52b69f1 | 150 | for (i = 0; i < state->num_connector; i++) { |
cc4ceb48 DV |
151 | struct drm_connector *connector = state->connectors[i]; |
152 | ||
153 | if (!connector) | |
154 | continue; | |
155 | ||
6f75cea6 DV |
156 | WARN_ON(!drm_modeset_is_locked(&config->connection_mutex)); |
157 | ||
cc4ceb48 DV |
158 | connector->funcs->atomic_destroy_state(connector, |
159 | state->connector_states[i]); | |
8a5c0bde | 160 | state->connectors[i] = NULL; |
9469244d | 161 | state->connector_states[i] = NULL; |
cc4ceb48 DV |
162 | } |
163 | ||
6f75cea6 | 164 | for (i = 0; i < config->num_crtc; i++) { |
cc4ceb48 DV |
165 | struct drm_crtc *crtc = state->crtcs[i]; |
166 | ||
167 | if (!crtc) | |
168 | continue; | |
169 | ||
170 | crtc->funcs->atomic_destroy_state(crtc, | |
171 | state->crtc_states[i]); | |
8a5c0bde | 172 | state->crtcs[i] = NULL; |
9469244d | 173 | state->crtc_states[i] = NULL; |
cc4ceb48 DV |
174 | } |
175 | ||
6f75cea6 | 176 | for (i = 0; i < config->num_total_plane; i++) { |
cc4ceb48 DV |
177 | struct drm_plane *plane = state->planes[i]; |
178 | ||
179 | if (!plane) | |
180 | continue; | |
181 | ||
182 | plane->funcs->atomic_destroy_state(plane, | |
183 | state->plane_states[i]); | |
8a5c0bde | 184 | state->planes[i] = NULL; |
9469244d | 185 | state->plane_states[i] = NULL; |
cc4ceb48 DV |
186 | } |
187 | } | |
036ef573 ML |
188 | EXPORT_SYMBOL(drm_atomic_state_default_clear); |
189 | ||
190 | /** | |
191 | * drm_atomic_state_clear - clear state object | |
192 | * @state: atomic state | |
193 | * | |
194 | * When the w/w mutex algorithm detects a deadlock we need to back off and drop | |
195 | * all locks. So someone else could sneak in and change the current modeset | |
196 | * configuration. Which means that all the state assembled in @state is no | |
197 | * longer an atomic update to the current state, but to some arbitrary earlier | |
198 | * state. Which could break assumptions the driver's ->atomic_check likely | |
199 | * relies on. | |
200 | * | |
201 | * Hence we must clear all cached state and completely start over, using this | |
202 | * function. | |
203 | */ | |
204 | void drm_atomic_state_clear(struct drm_atomic_state *state) | |
205 | { | |
206 | struct drm_device *dev = state->dev; | |
207 | struct drm_mode_config *config = &dev->mode_config; | |
208 | ||
209 | if (config->funcs->atomic_state_clear) | |
210 | config->funcs->atomic_state_clear(state); | |
211 | else | |
212 | drm_atomic_state_default_clear(state); | |
213 | } | |
cc4ceb48 DV |
214 | EXPORT_SYMBOL(drm_atomic_state_clear); |
215 | ||
216 | /** | |
217 | * drm_atomic_state_free - free all memory for an atomic state | |
218 | * @state: atomic state to deallocate | |
219 | * | |
220 | * This frees all memory associated with an atomic state, including all the | |
221 | * per-object state for planes, crtcs and connectors. | |
222 | */ | |
223 | void drm_atomic_state_free(struct drm_atomic_state *state) | |
224 | { | |
036ef573 ML |
225 | struct drm_device *dev; |
226 | struct drm_mode_config *config; | |
227 | ||
a0211bb4 ACO |
228 | if (!state) |
229 | return; | |
230 | ||
036ef573 ML |
231 | dev = state->dev; |
232 | config = &dev->mode_config; | |
233 | ||
cc4ceb48 DV |
234 | drm_atomic_state_clear(state); |
235 | ||
17a38d9c | 236 | DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state); |
cc4ceb48 | 237 | |
036ef573 ML |
238 | if (config->funcs->atomic_state_free) { |
239 | config->funcs->atomic_state_free(state); | |
240 | } else { | |
241 | drm_atomic_state_default_release(state); | |
242 | kfree(state); | |
243 | } | |
cc4ceb48 DV |
244 | } |
245 | EXPORT_SYMBOL(drm_atomic_state_free); | |
246 | ||
247 | /** | |
248 | * drm_atomic_get_crtc_state - get crtc state | |
249 | * @state: global atomic state object | |
250 | * @crtc: crtc to get state object for | |
251 | * | |
252 | * This function returns the crtc state for the given crtc, allocating it if | |
253 | * needed. It will also grab the relevant crtc lock to make sure that the state | |
254 | * is consistent. | |
255 | * | |
256 | * Returns: | |
257 | * | |
258 | * Either the allocated state or the error code encoded into the pointer. When | |
259 | * the error is EDEADLK then the w/w mutex code has detected a deadlock and the | |
260 | * entire atomic sequence must be restarted. All other errors are fatal. | |
261 | */ | |
262 | struct drm_crtc_state * | |
263 | drm_atomic_get_crtc_state(struct drm_atomic_state *state, | |
264 | struct drm_crtc *crtc) | |
265 | { | |
1b26a5e1 | 266 | int ret, index = drm_crtc_index(crtc); |
cc4ceb48 DV |
267 | struct drm_crtc_state *crtc_state; |
268 | ||
1b26a5e1 ML |
269 | crtc_state = drm_atomic_get_existing_crtc_state(state, crtc); |
270 | if (crtc_state) | |
271 | return crtc_state; | |
cc4ceb48 DV |
272 | |
273 | ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx); | |
274 | if (ret) | |
275 | return ERR_PTR(ret); | |
276 | ||
277 | crtc_state = crtc->funcs->atomic_duplicate_state(crtc); | |
278 | if (!crtc_state) | |
279 | return ERR_PTR(-ENOMEM); | |
280 | ||
281 | state->crtc_states[index] = crtc_state; | |
282 | state->crtcs[index] = crtc; | |
283 | crtc_state->state = state; | |
284 | ||
17a38d9c DV |
285 | DRM_DEBUG_ATOMIC("Added [CRTC:%d] %p state to %p\n", |
286 | crtc->base.id, crtc_state, state); | |
cc4ceb48 DV |
287 | |
288 | return crtc_state; | |
289 | } | |
290 | EXPORT_SYMBOL(drm_atomic_get_crtc_state); | |
291 | ||
819364da DS |
292 | /** |
293 | * drm_atomic_set_mode_for_crtc - set mode for CRTC | |
294 | * @state: the CRTC whose incoming state to update | |
295 | * @mode: kernel-internal mode to use for the CRTC, or NULL to disable | |
296 | * | |
297 | * Set a mode (originating from the kernel) on the desired CRTC state. Does | |
298 | * not change any other state properties, including enable, active, or | |
299 | * mode_changed. | |
300 | * | |
301 | * RETURNS: | |
302 | * Zero on success, error code on failure. Cannot return -EDEADLK. | |
303 | */ | |
304 | int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state, | |
305 | struct drm_display_mode *mode) | |
306 | { | |
99cf4a29 DS |
307 | struct drm_mode_modeinfo umode; |
308 | ||
819364da DS |
309 | /* Early return for no change. */ |
310 | if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0) | |
311 | return 0; | |
312 | ||
99cf4a29 DS |
313 | if (state->mode_blob) |
314 | drm_property_unreference_blob(state->mode_blob); | |
315 | state->mode_blob = NULL; | |
316 | ||
819364da | 317 | if (mode) { |
99cf4a29 DS |
318 | drm_mode_convert_to_umode(&umode, mode); |
319 | state->mode_blob = | |
320 | drm_property_create_blob(state->crtc->dev, | |
321 | sizeof(umode), | |
322 | &umode); | |
323 | if (IS_ERR(state->mode_blob)) | |
324 | return PTR_ERR(state->mode_blob); | |
325 | ||
819364da DS |
326 | drm_mode_copy(&state->mode, mode); |
327 | state->enable = true; | |
328 | DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n", | |
329 | mode->name, state); | |
330 | } else { | |
331 | memset(&state->mode, 0, sizeof(state->mode)); | |
332 | state->enable = false; | |
333 | DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n", | |
334 | state); | |
335 | } | |
336 | ||
337 | return 0; | |
338 | } | |
339 | EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc); | |
340 | ||
955f3c33 DS |
341 | /** |
342 | * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC | |
343 | * @state: the CRTC whose incoming state to update | |
344 | * @blob: pointer to blob property to use for mode | |
345 | * | |
346 | * Set a mode (originating from a blob property) on the desired CRTC state. | |
347 | * This function will take a reference on the blob property for the CRTC state, | |
348 | * and release the reference held on the state's existing mode property, if any | |
349 | * was set. | |
350 | * | |
351 | * RETURNS: | |
352 | * Zero on success, error code on failure. Cannot return -EDEADLK. | |
353 | */ | |
354 | int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state, | |
355 | struct drm_property_blob *blob) | |
356 | { | |
357 | if (blob == state->mode_blob) | |
358 | return 0; | |
359 | ||
360 | if (state->mode_blob) | |
361 | drm_property_unreference_blob(state->mode_blob); | |
362 | state->mode_blob = NULL; | |
363 | ||
364 | if (blob) { | |
365 | if (blob->length != sizeof(struct drm_mode_modeinfo) || | |
366 | drm_mode_convert_umode(&state->mode, | |
367 | (const struct drm_mode_modeinfo *) | |
368 | blob->data)) | |
369 | return -EINVAL; | |
370 | ||
371 | state->mode_blob = drm_property_reference_blob(blob); | |
372 | state->enable = true; | |
373 | DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n", | |
374 | state->mode.name, state); | |
375 | } else { | |
376 | memset(&state->mode, 0, sizeof(state->mode)); | |
377 | state->enable = false; | |
378 | DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n", | |
379 | state); | |
380 | } | |
381 | ||
382 | return 0; | |
383 | } | |
384 | EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc); | |
385 | ||
40ecc694 RC |
386 | /** |
387 | * drm_atomic_crtc_set_property - set property on CRTC | |
388 | * @crtc: the drm CRTC to set a property on | |
389 | * @state: the state object to update with the new property value | |
390 | * @property: the property to set | |
391 | * @val: the new property value | |
392 | * | |
393 | * Use this instead of calling crtc->atomic_set_property directly. | |
394 | * This function handles generic/core properties and calls out to | |
395 | * driver's ->atomic_set_property() for driver properties. To ensure | |
396 | * consistent behavior you must call this function rather than the | |
397 | * driver hook directly. | |
398 | * | |
399 | * RETURNS: | |
400 | * Zero on success, error code on failure | |
401 | */ | |
402 | int drm_atomic_crtc_set_property(struct drm_crtc *crtc, | |
403 | struct drm_crtc_state *state, struct drm_property *property, | |
404 | uint64_t val) | |
405 | { | |
eab3bbef DV |
406 | struct drm_device *dev = crtc->dev; |
407 | struct drm_mode_config *config = &dev->mode_config; | |
955f3c33 | 408 | int ret; |
eab3bbef | 409 | |
27798365 | 410 | if (property == config->prop_active) |
eab3bbef | 411 | state->active = val; |
955f3c33 DS |
412 | else if (property == config->prop_mode_id) { |
413 | struct drm_property_blob *mode = | |
414 | drm_property_lookup_blob(dev, val); | |
415 | ret = drm_atomic_set_mode_prop_for_crtc(state, mode); | |
416 | if (mode) | |
417 | drm_property_unreference_blob(mode); | |
418 | return ret; | |
419 | } | |
27798365 | 420 | else if (crtc->funcs->atomic_set_property) |
40ecc694 | 421 | return crtc->funcs->atomic_set_property(crtc, state, property, val); |
27798365 DS |
422 | else |
423 | return -EINVAL; | |
424 | ||
425 | return 0; | |
40ecc694 RC |
426 | } |
427 | EXPORT_SYMBOL(drm_atomic_crtc_set_property); | |
428 | ||
a97df1cc | 429 | /* |
ac9c9256 RC |
430 | * This function handles generic/core properties and calls out to |
431 | * driver's ->atomic_get_property() for driver properties. To ensure | |
432 | * consistent behavior you must call this function rather than the | |
433 | * driver hook directly. | |
ac9c9256 RC |
434 | */ |
435 | int drm_atomic_crtc_get_property(struct drm_crtc *crtc, | |
436 | const struct drm_crtc_state *state, | |
437 | struct drm_property *property, uint64_t *val) | |
438 | { | |
8f164ce4 DS |
439 | struct drm_device *dev = crtc->dev; |
440 | struct drm_mode_config *config = &dev->mode_config; | |
441 | ||
442 | if (property == config->prop_active) | |
443 | *val = state->active; | |
955f3c33 DS |
444 | else if (property == config->prop_mode_id) |
445 | *val = (state->mode_blob) ? state->mode_blob->base.id : 0; | |
8f164ce4 | 446 | else if (crtc->funcs->atomic_get_property) |
ac9c9256 | 447 | return crtc->funcs->atomic_get_property(crtc, state, property, val); |
8f164ce4 DS |
448 | else |
449 | return -EINVAL; | |
450 | ||
451 | return 0; | |
ac9c9256 | 452 | } |
ac9c9256 | 453 | |
5e743737 RC |
454 | /** |
455 | * drm_atomic_crtc_check - check crtc state | |
456 | * @crtc: crtc to check | |
457 | * @state: crtc state to check | |
458 | * | |
459 | * Provides core sanity checks for crtc state. | |
460 | * | |
461 | * RETURNS: | |
462 | * Zero on success, error code on failure | |
463 | */ | |
464 | static int drm_atomic_crtc_check(struct drm_crtc *crtc, | |
465 | struct drm_crtc_state *state) | |
466 | { | |
467 | /* NOTE: we explicitly don't enforce constraints such as primary | |
468 | * layer covering entire screen, since that is something we want | |
469 | * to allow (on hw that supports it). For hw that does not, it | |
470 | * should be checked in driver's crtc->atomic_check() vfunc. | |
471 | * | |
472 | * TODO: Add generic modeset state checks once we support those. | |
473 | */ | |
eab3bbef DV |
474 | |
475 | if (state->active && !state->enable) { | |
17a38d9c DV |
476 | DRM_DEBUG_ATOMIC("[CRTC:%d] active without enabled\n", |
477 | crtc->base.id); | |
eab3bbef DV |
478 | return -EINVAL; |
479 | } | |
480 | ||
99cf4a29 DS |
481 | /* The state->enable vs. state->mode_blob checks can be WARN_ON, |
482 | * as this is a kernel-internal detail that userspace should never | |
483 | * be able to trigger. */ | |
484 | if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) && | |
485 | WARN_ON(state->enable && !state->mode_blob)) { | |
486 | DRM_DEBUG_ATOMIC("[CRTC:%d] enabled without mode blob\n", | |
487 | crtc->base.id); | |
488 | return -EINVAL; | |
489 | } | |
490 | ||
491 | if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) && | |
492 | WARN_ON(!state->enable && state->mode_blob)) { | |
493 | DRM_DEBUG_ATOMIC("[CRTC:%d] disabled with mode blob\n", | |
494 | crtc->base.id); | |
495 | return -EINVAL; | |
496 | } | |
497 | ||
5e743737 RC |
498 | return 0; |
499 | } | |
500 | ||
cc4ceb48 DV |
501 | /** |
502 | * drm_atomic_get_plane_state - get plane state | |
503 | * @state: global atomic state object | |
504 | * @plane: plane to get state object for | |
505 | * | |
506 | * This function returns the plane state for the given plane, allocating it if | |
507 | * needed. It will also grab the relevant plane lock to make sure that the state | |
508 | * is consistent. | |
509 | * | |
510 | * Returns: | |
511 | * | |
512 | * Either the allocated state or the error code encoded into the pointer. When | |
513 | * the error is EDEADLK then the w/w mutex code has detected a deadlock and the | |
514 | * entire atomic sequence must be restarted. All other errors are fatal. | |
515 | */ | |
516 | struct drm_plane_state * | |
517 | drm_atomic_get_plane_state(struct drm_atomic_state *state, | |
518 | struct drm_plane *plane) | |
519 | { | |
1b26a5e1 | 520 | int ret, index = drm_plane_index(plane); |
cc4ceb48 DV |
521 | struct drm_plane_state *plane_state; |
522 | ||
1b26a5e1 ML |
523 | plane_state = drm_atomic_get_existing_plane_state(state, plane); |
524 | if (plane_state) | |
525 | return plane_state; | |
cc4ceb48 | 526 | |
4d02e2de | 527 | ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx); |
cc4ceb48 DV |
528 | if (ret) |
529 | return ERR_PTR(ret); | |
530 | ||
531 | plane_state = plane->funcs->atomic_duplicate_state(plane); | |
532 | if (!plane_state) | |
533 | return ERR_PTR(-ENOMEM); | |
534 | ||
535 | state->plane_states[index] = plane_state; | |
536 | state->planes[index] = plane; | |
537 | plane_state->state = state; | |
538 | ||
17a38d9c DV |
539 | DRM_DEBUG_ATOMIC("Added [PLANE:%d] %p state to %p\n", |
540 | plane->base.id, plane_state, state); | |
cc4ceb48 DV |
541 | |
542 | if (plane_state->crtc) { | |
543 | struct drm_crtc_state *crtc_state; | |
544 | ||
545 | crtc_state = drm_atomic_get_crtc_state(state, | |
546 | plane_state->crtc); | |
547 | if (IS_ERR(crtc_state)) | |
548 | return ERR_CAST(crtc_state); | |
549 | } | |
550 | ||
551 | return plane_state; | |
552 | } | |
553 | EXPORT_SYMBOL(drm_atomic_get_plane_state); | |
554 | ||
40ecc694 RC |
555 | /** |
556 | * drm_atomic_plane_set_property - set property on plane | |
557 | * @plane: the drm plane to set a property on | |
558 | * @state: the state object to update with the new property value | |
559 | * @property: the property to set | |
560 | * @val: the new property value | |
561 | * | |
562 | * Use this instead of calling plane->atomic_set_property directly. | |
563 | * This function handles generic/core properties and calls out to | |
564 | * driver's ->atomic_set_property() for driver properties. To ensure | |
565 | * consistent behavior you must call this function rather than the | |
566 | * driver hook directly. | |
567 | * | |
568 | * RETURNS: | |
569 | * Zero on success, error code on failure | |
570 | */ | |
571 | int drm_atomic_plane_set_property(struct drm_plane *plane, | |
572 | struct drm_plane_state *state, struct drm_property *property, | |
573 | uint64_t val) | |
574 | { | |
6b4959f4 RC |
575 | struct drm_device *dev = plane->dev; |
576 | struct drm_mode_config *config = &dev->mode_config; | |
577 | ||
578 | if (property == config->prop_fb_id) { | |
579 | struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val); | |
580 | drm_atomic_set_fb_for_plane(state, fb); | |
581 | if (fb) | |
582 | drm_framebuffer_unreference(fb); | |
583 | } else if (property == config->prop_crtc_id) { | |
584 | struct drm_crtc *crtc = drm_crtc_find(dev, val); | |
585 | return drm_atomic_set_crtc_for_plane(state, crtc); | |
586 | } else if (property == config->prop_crtc_x) { | |
587 | state->crtc_x = U642I64(val); | |
588 | } else if (property == config->prop_crtc_y) { | |
589 | state->crtc_y = U642I64(val); | |
590 | } else if (property == config->prop_crtc_w) { | |
591 | state->crtc_w = val; | |
592 | } else if (property == config->prop_crtc_h) { | |
593 | state->crtc_h = val; | |
594 | } else if (property == config->prop_src_x) { | |
595 | state->src_x = val; | |
596 | } else if (property == config->prop_src_y) { | |
597 | state->src_y = val; | |
598 | } else if (property == config->prop_src_w) { | |
599 | state->src_w = val; | |
600 | } else if (property == config->prop_src_h) { | |
601 | state->src_h = val; | |
1da30627 MR |
602 | } else if (property == config->rotation_property) { |
603 | state->rotation = val; | |
6b4959f4 RC |
604 | } else if (plane->funcs->atomic_set_property) { |
605 | return plane->funcs->atomic_set_property(plane, state, | |
606 | property, val); | |
607 | } else { | |
608 | return -EINVAL; | |
609 | } | |
610 | ||
611 | return 0; | |
40ecc694 RC |
612 | } |
613 | EXPORT_SYMBOL(drm_atomic_plane_set_property); | |
614 | ||
a97df1cc | 615 | /* |
ac9c9256 RC |
616 | * This function handles generic/core properties and calls out to |
617 | * driver's ->atomic_get_property() for driver properties. To ensure | |
618 | * consistent behavior you must call this function rather than the | |
619 | * driver hook directly. | |
ac9c9256 | 620 | */ |
a97df1cc DV |
621 | static int |
622 | drm_atomic_plane_get_property(struct drm_plane *plane, | |
ac9c9256 RC |
623 | const struct drm_plane_state *state, |
624 | struct drm_property *property, uint64_t *val) | |
625 | { | |
6b4959f4 RC |
626 | struct drm_device *dev = plane->dev; |
627 | struct drm_mode_config *config = &dev->mode_config; | |
628 | ||
629 | if (property == config->prop_fb_id) { | |
630 | *val = (state->fb) ? state->fb->base.id : 0; | |
631 | } else if (property == config->prop_crtc_id) { | |
632 | *val = (state->crtc) ? state->crtc->base.id : 0; | |
633 | } else if (property == config->prop_crtc_x) { | |
634 | *val = I642U64(state->crtc_x); | |
635 | } else if (property == config->prop_crtc_y) { | |
636 | *val = I642U64(state->crtc_y); | |
637 | } else if (property == config->prop_crtc_w) { | |
638 | *val = state->crtc_w; | |
639 | } else if (property == config->prop_crtc_h) { | |
640 | *val = state->crtc_h; | |
641 | } else if (property == config->prop_src_x) { | |
642 | *val = state->src_x; | |
643 | } else if (property == config->prop_src_y) { | |
644 | *val = state->src_y; | |
645 | } else if (property == config->prop_src_w) { | |
646 | *val = state->src_w; | |
647 | } else if (property == config->prop_src_h) { | |
648 | *val = state->src_h; | |
4cda09ca TU |
649 | } else if (property == config->rotation_property) { |
650 | *val = state->rotation; | |
6b4959f4 | 651 | } else if (plane->funcs->atomic_get_property) { |
ac9c9256 | 652 | return plane->funcs->atomic_get_property(plane, state, property, val); |
6b4959f4 RC |
653 | } else { |
654 | return -EINVAL; | |
655 | } | |
656 | ||
657 | return 0; | |
ac9c9256 | 658 | } |
ac9c9256 | 659 | |
5e743737 RC |
660 | /** |
661 | * drm_atomic_plane_check - check plane state | |
662 | * @plane: plane to check | |
663 | * @state: plane state to check | |
664 | * | |
665 | * Provides core sanity checks for plane state. | |
666 | * | |
667 | * RETURNS: | |
668 | * Zero on success, error code on failure | |
669 | */ | |
670 | static int drm_atomic_plane_check(struct drm_plane *plane, | |
671 | struct drm_plane_state *state) | |
672 | { | |
673 | unsigned int fb_width, fb_height; | |
ead8610d | 674 | int ret; |
5e743737 RC |
675 | |
676 | /* either *both* CRTC and FB must be set, or neither */ | |
677 | if (WARN_ON(state->crtc && !state->fb)) { | |
17a38d9c | 678 | DRM_DEBUG_ATOMIC("CRTC set but no FB\n"); |
5e743737 RC |
679 | return -EINVAL; |
680 | } else if (WARN_ON(state->fb && !state->crtc)) { | |
17a38d9c | 681 | DRM_DEBUG_ATOMIC("FB set but no CRTC\n"); |
5e743737 RC |
682 | return -EINVAL; |
683 | } | |
684 | ||
685 | /* if disabled, we don't care about the rest of the state: */ | |
686 | if (!state->crtc) | |
687 | return 0; | |
688 | ||
689 | /* Check whether this plane is usable on this CRTC */ | |
690 | if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) { | |
17a38d9c | 691 | DRM_DEBUG_ATOMIC("Invalid crtc for plane\n"); |
5e743737 RC |
692 | return -EINVAL; |
693 | } | |
694 | ||
695 | /* Check whether this plane supports the fb pixel format. */ | |
ead8610d LP |
696 | ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format); |
697 | if (ret) { | |
17a38d9c DV |
698 | DRM_DEBUG_ATOMIC("Invalid pixel format %s\n", |
699 | drm_get_format_name(state->fb->pixel_format)); | |
ead8610d | 700 | return ret; |
5e743737 RC |
701 | } |
702 | ||
703 | /* Give drivers some help against integer overflows */ | |
704 | if (state->crtc_w > INT_MAX || | |
705 | state->crtc_x > INT_MAX - (int32_t) state->crtc_w || | |
706 | state->crtc_h > INT_MAX || | |
707 | state->crtc_y > INT_MAX - (int32_t) state->crtc_h) { | |
17a38d9c DV |
708 | DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n", |
709 | state->crtc_w, state->crtc_h, | |
710 | state->crtc_x, state->crtc_y); | |
5e743737 RC |
711 | return -ERANGE; |
712 | } | |
713 | ||
714 | fb_width = state->fb->width << 16; | |
715 | fb_height = state->fb->height << 16; | |
716 | ||
717 | /* Make sure source coordinates are inside the fb. */ | |
718 | if (state->src_w > fb_width || | |
719 | state->src_x > fb_width - state->src_w || | |
720 | state->src_h > fb_height || | |
721 | state->src_y > fb_height - state->src_h) { | |
17a38d9c DV |
722 | DRM_DEBUG_ATOMIC("Invalid source coordinates " |
723 | "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n", | |
724 | state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10, | |
725 | state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10, | |
726 | state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10, | |
727 | state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10); | |
5e743737 RC |
728 | return -ENOSPC; |
729 | } | |
730 | ||
731 | return 0; | |
732 | } | |
733 | ||
cc4ceb48 DV |
734 | /** |
735 | * drm_atomic_get_connector_state - get connector state | |
736 | * @state: global atomic state object | |
737 | * @connector: connector to get state object for | |
738 | * | |
739 | * This function returns the connector state for the given connector, | |
740 | * allocating it if needed. It will also grab the relevant connector lock to | |
741 | * make sure that the state is consistent. | |
742 | * | |
743 | * Returns: | |
744 | * | |
745 | * Either the allocated state or the error code encoded into the pointer. When | |
746 | * the error is EDEADLK then the w/w mutex code has detected a deadlock and the | |
747 | * entire atomic sequence must be restarted. All other errors are fatal. | |
748 | */ | |
749 | struct drm_connector_state * | |
750 | drm_atomic_get_connector_state(struct drm_atomic_state *state, | |
751 | struct drm_connector *connector) | |
752 | { | |
753 | int ret, index; | |
754 | struct drm_mode_config *config = &connector->dev->mode_config; | |
755 | struct drm_connector_state *connector_state; | |
756 | ||
c7eb76f4 DV |
757 | ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx); |
758 | if (ret) | |
759 | return ERR_PTR(ret); | |
760 | ||
cc4ceb48 DV |
761 | index = drm_connector_index(connector); |
762 | ||
f52b69f1 DV |
763 | /* |
764 | * Construction of atomic state updates can race with a connector | |
765 | * hot-add which might overflow. In this case flip the table and just | |
766 | * restart the entire ioctl - no one is fast enough to livelock a cpu | |
767 | * with physical hotplug events anyway. | |
768 | * | |
769 | * Note that we only grab the indexes once we have the right lock to | |
770 | * prevent hotplug/unplugging of connectors. So removal is no problem, | |
771 | * at most the array is a bit too large. | |
772 | */ | |
773 | if (index >= state->num_connector) { | |
17a38d9c | 774 | DRM_DEBUG_ATOMIC("Hot-added connector would overflow state array, restarting\n"); |
fc2d2bc1 | 775 | return ERR_PTR(-EAGAIN); |
f52b69f1 DV |
776 | } |
777 | ||
cc4ceb48 DV |
778 | if (state->connector_states[index]) |
779 | return state->connector_states[index]; | |
780 | ||
cc4ceb48 DV |
781 | connector_state = connector->funcs->atomic_duplicate_state(connector); |
782 | if (!connector_state) | |
783 | return ERR_PTR(-ENOMEM); | |
784 | ||
785 | state->connector_states[index] = connector_state; | |
786 | state->connectors[index] = connector; | |
787 | connector_state->state = state; | |
788 | ||
17a38d9c DV |
789 | DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n", |
790 | connector->base.id, connector_state, state); | |
cc4ceb48 DV |
791 | |
792 | if (connector_state->crtc) { | |
793 | struct drm_crtc_state *crtc_state; | |
794 | ||
795 | crtc_state = drm_atomic_get_crtc_state(state, | |
796 | connector_state->crtc); | |
797 | if (IS_ERR(crtc_state)) | |
798 | return ERR_CAST(crtc_state); | |
799 | } | |
800 | ||
801 | return connector_state; | |
802 | } | |
803 | EXPORT_SYMBOL(drm_atomic_get_connector_state); | |
804 | ||
40ecc694 RC |
805 | /** |
806 | * drm_atomic_connector_set_property - set property on connector. | |
807 | * @connector: the drm connector to set a property on | |
808 | * @state: the state object to update with the new property value | |
809 | * @property: the property to set | |
810 | * @val: the new property value | |
811 | * | |
812 | * Use this instead of calling connector->atomic_set_property directly. | |
813 | * This function handles generic/core properties and calls out to | |
814 | * driver's ->atomic_set_property() for driver properties. To ensure | |
815 | * consistent behavior you must call this function rather than the | |
816 | * driver hook directly. | |
817 | * | |
818 | * RETURNS: | |
819 | * Zero on success, error code on failure | |
820 | */ | |
821 | int drm_atomic_connector_set_property(struct drm_connector *connector, | |
822 | struct drm_connector_state *state, struct drm_property *property, | |
823 | uint64_t val) | |
824 | { | |
825 | struct drm_device *dev = connector->dev; | |
826 | struct drm_mode_config *config = &dev->mode_config; | |
827 | ||
ae16c597 RC |
828 | if (property == config->prop_crtc_id) { |
829 | struct drm_crtc *crtc = drm_crtc_find(dev, val); | |
830 | return drm_atomic_set_crtc_for_connector(state, crtc); | |
831 | } else if (property == config->dpms_property) { | |
40ecc694 RC |
832 | /* setting DPMS property requires special handling, which |
833 | * is done in legacy setprop path for us. Disallow (for | |
834 | * now?) atomic writes to DPMS property: | |
835 | */ | |
836 | return -EINVAL; | |
837 | } else if (connector->funcs->atomic_set_property) { | |
838 | return connector->funcs->atomic_set_property(connector, | |
839 | state, property, val); | |
840 | } else { | |
841 | return -EINVAL; | |
842 | } | |
843 | } | |
844 | EXPORT_SYMBOL(drm_atomic_connector_set_property); | |
845 | ||
a97df1cc | 846 | /* |
ac9c9256 RC |
847 | * This function handles generic/core properties and calls out to |
848 | * driver's ->atomic_get_property() for driver properties. To ensure | |
849 | * consistent behavior you must call this function rather than the | |
850 | * driver hook directly. | |
ac9c9256 | 851 | */ |
a97df1cc DV |
852 | static int |
853 | drm_atomic_connector_get_property(struct drm_connector *connector, | |
ac9c9256 RC |
854 | const struct drm_connector_state *state, |
855 | struct drm_property *property, uint64_t *val) | |
856 | { | |
857 | struct drm_device *dev = connector->dev; | |
858 | struct drm_mode_config *config = &dev->mode_config; | |
859 | ||
ae16c597 RC |
860 | if (property == config->prop_crtc_id) { |
861 | *val = (state->crtc) ? state->crtc->base.id : 0; | |
862 | } else if (property == config->dpms_property) { | |
ac9c9256 RC |
863 | *val = connector->dpms; |
864 | } else if (connector->funcs->atomic_get_property) { | |
865 | return connector->funcs->atomic_get_property(connector, | |
866 | state, property, val); | |
867 | } else { | |
868 | return -EINVAL; | |
869 | } | |
870 | ||
871 | return 0; | |
872 | } | |
ac9c9256 | 873 | |
88a48e29 RC |
874 | int drm_atomic_get_property(struct drm_mode_object *obj, |
875 | struct drm_property *property, uint64_t *val) | |
876 | { | |
877 | struct drm_device *dev = property->dev; | |
878 | int ret; | |
879 | ||
880 | switch (obj->type) { | |
881 | case DRM_MODE_OBJECT_CONNECTOR: { | |
882 | struct drm_connector *connector = obj_to_connector(obj); | |
883 | WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex)); | |
884 | ret = drm_atomic_connector_get_property(connector, | |
885 | connector->state, property, val); | |
886 | break; | |
887 | } | |
888 | case DRM_MODE_OBJECT_CRTC: { | |
889 | struct drm_crtc *crtc = obj_to_crtc(obj); | |
890 | WARN_ON(!drm_modeset_is_locked(&crtc->mutex)); | |
891 | ret = drm_atomic_crtc_get_property(crtc, | |
892 | crtc->state, property, val); | |
893 | break; | |
894 | } | |
895 | case DRM_MODE_OBJECT_PLANE: { | |
896 | struct drm_plane *plane = obj_to_plane(obj); | |
897 | WARN_ON(!drm_modeset_is_locked(&plane->mutex)); | |
898 | ret = drm_atomic_plane_get_property(plane, | |
899 | plane->state, property, val); | |
900 | break; | |
901 | } | |
902 | default: | |
903 | ret = -EINVAL; | |
904 | break; | |
905 | } | |
906 | ||
907 | return ret; | |
908 | } | |
909 | ||
cc4ceb48 DV |
910 | /** |
911 | * drm_atomic_set_crtc_for_plane - set crtc for plane | |
07cc0ef6 | 912 | * @plane_state: the plane whose incoming state to update |
cc4ceb48 DV |
913 | * @crtc: crtc to use for the plane |
914 | * | |
915 | * Changing the assigned crtc for a plane requires us to grab the lock and state | |
916 | * for the new crtc, as needed. This function takes care of all these details | |
917 | * besides updating the pointer in the state object itself. | |
918 | * | |
919 | * Returns: | |
920 | * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK | |
921 | * then the w/w mutex code has detected a deadlock and the entire atomic | |
922 | * sequence must be restarted. All other errors are fatal. | |
923 | */ | |
924 | int | |
07cc0ef6 DV |
925 | drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state, |
926 | struct drm_crtc *crtc) | |
cc4ceb48 | 927 | { |
07cc0ef6 | 928 | struct drm_plane *plane = plane_state->plane; |
cc4ceb48 DV |
929 | struct drm_crtc_state *crtc_state; |
930 | ||
6ddd388a RC |
931 | if (plane_state->crtc) { |
932 | crtc_state = drm_atomic_get_crtc_state(plane_state->state, | |
933 | plane_state->crtc); | |
934 | if (WARN_ON(IS_ERR(crtc_state))) | |
935 | return PTR_ERR(crtc_state); | |
936 | ||
937 | crtc_state->plane_mask &= ~(1 << drm_plane_index(plane)); | |
938 | } | |
939 | ||
940 | plane_state->crtc = crtc; | |
941 | ||
cc4ceb48 DV |
942 | if (crtc) { |
943 | crtc_state = drm_atomic_get_crtc_state(plane_state->state, | |
944 | crtc); | |
945 | if (IS_ERR(crtc_state)) | |
946 | return PTR_ERR(crtc_state); | |
6ddd388a | 947 | crtc_state->plane_mask |= (1 << drm_plane_index(plane)); |
cc4ceb48 DV |
948 | } |
949 | ||
cc4ceb48 | 950 | if (crtc) |
17a38d9c DV |
951 | DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d]\n", |
952 | plane_state, crtc->base.id); | |
cc4ceb48 | 953 | else |
17a38d9c DV |
954 | DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n", |
955 | plane_state); | |
cc4ceb48 DV |
956 | |
957 | return 0; | |
958 | } | |
959 | EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane); | |
960 | ||
321ebf04 | 961 | /** |
16d78bc2 | 962 | * drm_atomic_set_fb_for_plane - set framebuffer for plane |
321ebf04 DV |
963 | * @plane_state: atomic state object for the plane |
964 | * @fb: fb to use for the plane | |
965 | * | |
966 | * Changing the assigned framebuffer for a plane requires us to grab a reference | |
967 | * to the new fb and drop the reference to the old fb, if there is one. This | |
968 | * function takes care of all these details besides updating the pointer in the | |
969 | * state object itself. | |
970 | */ | |
971 | void | |
972 | drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state, | |
973 | struct drm_framebuffer *fb) | |
974 | { | |
975 | if (plane_state->fb) | |
976 | drm_framebuffer_unreference(plane_state->fb); | |
977 | if (fb) | |
978 | drm_framebuffer_reference(fb); | |
979 | plane_state->fb = fb; | |
980 | ||
981 | if (fb) | |
17a38d9c DV |
982 | DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n", |
983 | fb->base.id, plane_state); | |
321ebf04 | 984 | else |
17a38d9c DV |
985 | DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n", |
986 | plane_state); | |
321ebf04 DV |
987 | } |
988 | EXPORT_SYMBOL(drm_atomic_set_fb_for_plane); | |
989 | ||
cc4ceb48 DV |
990 | /** |
991 | * drm_atomic_set_crtc_for_connector - set crtc for connector | |
992 | * @conn_state: atomic state object for the connector | |
993 | * @crtc: crtc to use for the connector | |
994 | * | |
995 | * Changing the assigned crtc for a connector requires us to grab the lock and | |
996 | * state for the new crtc, as needed. This function takes care of all these | |
997 | * details besides updating the pointer in the state object itself. | |
998 | * | |
999 | * Returns: | |
1000 | * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK | |
1001 | * then the w/w mutex code has detected a deadlock and the entire atomic | |
1002 | * sequence must be restarted. All other errors are fatal. | |
1003 | */ | |
1004 | int | |
1005 | drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state, | |
1006 | struct drm_crtc *crtc) | |
1007 | { | |
1008 | struct drm_crtc_state *crtc_state; | |
1009 | ||
1010 | if (crtc) { | |
1011 | crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc); | |
1012 | if (IS_ERR(crtc_state)) | |
1013 | return PTR_ERR(crtc_state); | |
1014 | } | |
1015 | ||
1016 | conn_state->crtc = crtc; | |
1017 | ||
1018 | if (crtc) | |
17a38d9c DV |
1019 | DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d]\n", |
1020 | conn_state, crtc->base.id); | |
cc4ceb48 | 1021 | else |
17a38d9c DV |
1022 | DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n", |
1023 | conn_state); | |
cc4ceb48 DV |
1024 | |
1025 | return 0; | |
1026 | } | |
1027 | EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector); | |
1028 | ||
1029 | /** | |
1030 | * drm_atomic_add_affected_connectors - add connectors for crtc | |
1031 | * @state: atomic state | |
1032 | * @crtc: DRM crtc | |
1033 | * | |
1034 | * This function walks the current configuration and adds all connectors | |
1035 | * currently using @crtc to the atomic configuration @state. Note that this | |
1036 | * function must acquire the connection mutex. This can potentially cause | |
1037 | * unneeded seralization if the update is just for the planes on one crtc. Hence | |
1038 | * drivers and helpers should only call this when really needed (e.g. when a | |
1039 | * full modeset needs to happen due to some change). | |
1040 | * | |
1041 | * Returns: | |
1042 | * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK | |
1043 | * then the w/w mutex code has detected a deadlock and the entire atomic | |
1044 | * sequence must be restarted. All other errors are fatal. | |
1045 | */ | |
1046 | int | |
1047 | drm_atomic_add_affected_connectors(struct drm_atomic_state *state, | |
1048 | struct drm_crtc *crtc) | |
1049 | { | |
1050 | struct drm_mode_config *config = &state->dev->mode_config; | |
1051 | struct drm_connector *connector; | |
1052 | struct drm_connector_state *conn_state; | |
1053 | int ret; | |
1054 | ||
1055 | ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx); | |
1056 | if (ret) | |
1057 | return ret; | |
1058 | ||
17a38d9c DV |
1059 | DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d] to %p\n", |
1060 | crtc->base.id, state); | |
cc4ceb48 DV |
1061 | |
1062 | /* | |
1063 | * Changed connectors are already in @state, so only need to look at the | |
1064 | * current configuration. | |
1065 | */ | |
1066 | list_for_each_entry(connector, &config->connector_list, head) { | |
1067 | if (connector->state->crtc != crtc) | |
1068 | continue; | |
1069 | ||
1070 | conn_state = drm_atomic_get_connector_state(state, connector); | |
1071 | if (IS_ERR(conn_state)) | |
1072 | return PTR_ERR(conn_state); | |
1073 | } | |
1074 | ||
1075 | return 0; | |
1076 | } | |
1077 | EXPORT_SYMBOL(drm_atomic_add_affected_connectors); | |
1078 | ||
e01e9f75 ML |
1079 | /** |
1080 | * drm_atomic_add_affected_planes - add planes for crtc | |
1081 | * @state: atomic state | |
1082 | * @crtc: DRM crtc | |
1083 | * | |
1084 | * This function walks the current configuration and adds all planes | |
1085 | * currently used by @crtc to the atomic configuration @state. This is useful | |
1086 | * when an atomic commit also needs to check all currently enabled plane on | |
1087 | * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC | |
1088 | * to avoid special code to force-enable all planes. | |
1089 | * | |
1090 | * Since acquiring a plane state will always also acquire the w/w mutex of the | |
1091 | * current CRTC for that plane (if there is any) adding all the plane states for | |
1092 | * a CRTC will not reduce parallism of atomic updates. | |
1093 | * | |
1094 | * Returns: | |
1095 | * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK | |
1096 | * then the w/w mutex code has detected a deadlock and the entire atomic | |
1097 | * sequence must be restarted. All other errors are fatal. | |
1098 | */ | |
1099 | int | |
1100 | drm_atomic_add_affected_planes(struct drm_atomic_state *state, | |
1101 | struct drm_crtc *crtc) | |
1102 | { | |
1103 | struct drm_plane *plane; | |
1104 | ||
1105 | WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc)); | |
1106 | ||
1107 | drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) { | |
1108 | struct drm_plane_state *plane_state = | |
1109 | drm_atomic_get_plane_state(state, plane); | |
1110 | ||
1111 | if (IS_ERR(plane_state)) | |
1112 | return PTR_ERR(plane_state); | |
1113 | } | |
1114 | return 0; | |
1115 | } | |
1116 | EXPORT_SYMBOL(drm_atomic_add_affected_planes); | |
1117 | ||
cc4ceb48 DV |
1118 | /** |
1119 | * drm_atomic_connectors_for_crtc - count number of connected outputs | |
1120 | * @state: atomic state | |
1121 | * @crtc: DRM crtc | |
1122 | * | |
1123 | * This function counts all connectors which will be connected to @crtc | |
1124 | * according to @state. Useful to recompute the enable state for @crtc. | |
1125 | */ | |
1126 | int | |
1127 | drm_atomic_connectors_for_crtc(struct drm_atomic_state *state, | |
1128 | struct drm_crtc *crtc) | |
1129 | { | |
df63b999 ACO |
1130 | struct drm_connector *connector; |
1131 | struct drm_connector_state *conn_state; | |
cc4ceb48 | 1132 | |
df63b999 | 1133 | int i, num_connected_connectors = 0; |
cc4ceb48 | 1134 | |
df63b999 ACO |
1135 | for_each_connector_in_state(state, connector, conn_state, i) { |
1136 | if (conn_state->crtc == crtc) | |
cc4ceb48 DV |
1137 | num_connected_connectors++; |
1138 | } | |
1139 | ||
17a38d9c DV |
1140 | DRM_DEBUG_ATOMIC("State %p has %i connectors for [CRTC:%d]\n", |
1141 | state, num_connected_connectors, crtc->base.id); | |
cc4ceb48 DV |
1142 | |
1143 | return num_connected_connectors; | |
1144 | } | |
1145 | EXPORT_SYMBOL(drm_atomic_connectors_for_crtc); | |
1146 | ||
1147 | /** | |
1148 | * drm_atomic_legacy_backoff - locking backoff for legacy ioctls | |
1149 | * @state: atomic state | |
1150 | * | |
1151 | * This function should be used by legacy entry points which don't understand | |
1152 | * -EDEADLK semantics. For simplicity this one will grab all modeset locks after | |
16d78bc2 | 1153 | * the slowpath completed. |
cc4ceb48 DV |
1154 | */ |
1155 | void drm_atomic_legacy_backoff(struct drm_atomic_state *state) | |
1156 | { | |
1157 | int ret; | |
1158 | ||
1159 | retry: | |
1160 | drm_modeset_backoff(state->acquire_ctx); | |
1161 | ||
1162 | ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex, | |
1163 | state->acquire_ctx); | |
1164 | if (ret) | |
1165 | goto retry; | |
1166 | ret = drm_modeset_lock_all_crtcs(state->dev, | |
1167 | state->acquire_ctx); | |
1168 | if (ret) | |
1169 | goto retry; | |
1170 | } | |
1171 | EXPORT_SYMBOL(drm_atomic_legacy_backoff); | |
1172 | ||
1173 | /** | |
1174 | * drm_atomic_check_only - check whether a given config would work | |
1175 | * @state: atomic configuration to check | |
1176 | * | |
1177 | * Note that this function can return -EDEADLK if the driver needed to acquire | |
1178 | * more locks but encountered a deadlock. The caller must then do the usual w/w | |
1179 | * backoff dance and restart. All other errors are fatal. | |
1180 | * | |
1181 | * Returns: | |
1182 | * 0 on success, negative error code on failure. | |
1183 | */ | |
1184 | int drm_atomic_check_only(struct drm_atomic_state *state) | |
1185 | { | |
5e743737 RC |
1186 | struct drm_device *dev = state->dev; |
1187 | struct drm_mode_config *config = &dev->mode_config; | |
df63b999 ACO |
1188 | struct drm_plane *plane; |
1189 | struct drm_plane_state *plane_state; | |
1190 | struct drm_crtc *crtc; | |
1191 | struct drm_crtc_state *crtc_state; | |
5e743737 | 1192 | int i, ret = 0; |
cc4ceb48 | 1193 | |
17a38d9c | 1194 | DRM_DEBUG_ATOMIC("checking %p\n", state); |
cc4ceb48 | 1195 | |
df63b999 ACO |
1196 | for_each_plane_in_state(state, plane, plane_state, i) { |
1197 | ret = drm_atomic_plane_check(plane, plane_state); | |
5e743737 | 1198 | if (ret) { |
17a38d9c DV |
1199 | DRM_DEBUG_ATOMIC("[PLANE:%d] atomic core check failed\n", |
1200 | plane->base.id); | |
5e743737 RC |
1201 | return ret; |
1202 | } | |
1203 | } | |
1204 | ||
df63b999 ACO |
1205 | for_each_crtc_in_state(state, crtc, crtc_state, i) { |
1206 | ret = drm_atomic_crtc_check(crtc, crtc_state); | |
5e743737 | 1207 | if (ret) { |
17a38d9c DV |
1208 | DRM_DEBUG_ATOMIC("[CRTC:%d] atomic core check failed\n", |
1209 | crtc->base.id); | |
5e743737 RC |
1210 | return ret; |
1211 | } | |
1212 | } | |
1213 | ||
cc4ceb48 | 1214 | if (config->funcs->atomic_check) |
5e743737 RC |
1215 | ret = config->funcs->atomic_check(state->dev, state); |
1216 | ||
d34f20d6 | 1217 | if (!state->allow_modeset) { |
df63b999 | 1218 | for_each_crtc_in_state(state, crtc, crtc_state, i) { |
eab3bbef DV |
1219 | if (crtc_state->mode_changed || |
1220 | crtc_state->active_changed) { | |
17a38d9c DV |
1221 | DRM_DEBUG_ATOMIC("[CRTC:%d] requires full modeset\n", |
1222 | crtc->base.id); | |
d34f20d6 RC |
1223 | return -EINVAL; |
1224 | } | |
1225 | } | |
1226 | } | |
1227 | ||
5e743737 | 1228 | return ret; |
cc4ceb48 DV |
1229 | } |
1230 | EXPORT_SYMBOL(drm_atomic_check_only); | |
1231 | ||
1232 | /** | |
1233 | * drm_atomic_commit - commit configuration atomically | |
1234 | * @state: atomic configuration to check | |
1235 | * | |
1236 | * Note that this function can return -EDEADLK if the driver needed to acquire | |
1237 | * more locks but encountered a deadlock. The caller must then do the usual w/w | |
1238 | * backoff dance and restart. All other errors are fatal. | |
1239 | * | |
1240 | * Also note that on successful execution ownership of @state is transferred | |
1241 | * from the caller of this function to the function itself. The caller must not | |
1242 | * free or in any other way access @state. If the function fails then the caller | |
1243 | * must clean up @state itself. | |
1244 | * | |
1245 | * Returns: | |
1246 | * 0 on success, negative error code on failure. | |
1247 | */ | |
1248 | int drm_atomic_commit(struct drm_atomic_state *state) | |
1249 | { | |
1250 | struct drm_mode_config *config = &state->dev->mode_config; | |
1251 | int ret; | |
1252 | ||
1253 | ret = drm_atomic_check_only(state); | |
1254 | if (ret) | |
1255 | return ret; | |
1256 | ||
17a38d9c | 1257 | DRM_DEBUG_ATOMIC("commiting %p\n", state); |
cc4ceb48 DV |
1258 | |
1259 | return config->funcs->atomic_commit(state->dev, state, false); | |
1260 | } | |
1261 | EXPORT_SYMBOL(drm_atomic_commit); | |
1262 | ||
1263 | /** | |
1264 | * drm_atomic_async_commit - atomic&async configuration commit | |
1265 | * @state: atomic configuration to check | |
1266 | * | |
1267 | * Note that this function can return -EDEADLK if the driver needed to acquire | |
1268 | * more locks but encountered a deadlock. The caller must then do the usual w/w | |
1269 | * backoff dance and restart. All other errors are fatal. | |
1270 | * | |
1271 | * Also note that on successful execution ownership of @state is transferred | |
1272 | * from the caller of this function to the function itself. The caller must not | |
1273 | * free or in any other way access @state. If the function fails then the caller | |
1274 | * must clean up @state itself. | |
1275 | * | |
1276 | * Returns: | |
1277 | * 0 on success, negative error code on failure. | |
1278 | */ | |
1279 | int drm_atomic_async_commit(struct drm_atomic_state *state) | |
1280 | { | |
1281 | struct drm_mode_config *config = &state->dev->mode_config; | |
1282 | int ret; | |
1283 | ||
1284 | ret = drm_atomic_check_only(state); | |
1285 | if (ret) | |
1286 | return ret; | |
1287 | ||
17a38d9c | 1288 | DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state); |
cc4ceb48 DV |
1289 | |
1290 | return config->funcs->atomic_commit(state->dev, state, true); | |
1291 | } | |
1292 | EXPORT_SYMBOL(drm_atomic_async_commit); | |
d34f20d6 RC |
1293 | |
1294 | /* | |
1295 | * The big monstor ioctl | |
1296 | */ | |
1297 | ||
1298 | static struct drm_pending_vblank_event *create_vblank_event( | |
1299 | struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data) | |
1300 | { | |
1301 | struct drm_pending_vblank_event *e = NULL; | |
1302 | unsigned long flags; | |
1303 | ||
1304 | spin_lock_irqsave(&dev->event_lock, flags); | |
1305 | if (file_priv->event_space < sizeof e->event) { | |
1306 | spin_unlock_irqrestore(&dev->event_lock, flags); | |
1307 | goto out; | |
1308 | } | |
1309 | file_priv->event_space -= sizeof e->event; | |
1310 | spin_unlock_irqrestore(&dev->event_lock, flags); | |
1311 | ||
1312 | e = kzalloc(sizeof *e, GFP_KERNEL); | |
1313 | if (e == NULL) { | |
1314 | spin_lock_irqsave(&dev->event_lock, flags); | |
1315 | file_priv->event_space += sizeof e->event; | |
1316 | spin_unlock_irqrestore(&dev->event_lock, flags); | |
1317 | goto out; | |
1318 | } | |
1319 | ||
1320 | e->event.base.type = DRM_EVENT_FLIP_COMPLETE; | |
1321 | e->event.base.length = sizeof e->event; | |
1322 | e->event.user_data = user_data; | |
1323 | e->base.event = &e->event.base; | |
1324 | e->base.file_priv = file_priv; | |
1325 | e->base.destroy = (void (*) (struct drm_pending_event *)) kfree; | |
1326 | ||
1327 | out: | |
1328 | return e; | |
1329 | } | |
1330 | ||
1331 | static void destroy_vblank_event(struct drm_device *dev, | |
1332 | struct drm_file *file_priv, struct drm_pending_vblank_event *e) | |
1333 | { | |
1334 | unsigned long flags; | |
1335 | ||
1336 | spin_lock_irqsave(&dev->event_lock, flags); | |
1337 | file_priv->event_space += sizeof e->event; | |
1338 | spin_unlock_irqrestore(&dev->event_lock, flags); | |
1339 | kfree(e); | |
1340 | } | |
1341 | ||
1342 | static int atomic_set_prop(struct drm_atomic_state *state, | |
1343 | struct drm_mode_object *obj, struct drm_property *prop, | |
1344 | uint64_t prop_value) | |
1345 | { | |
1346 | struct drm_mode_object *ref; | |
1347 | int ret; | |
1348 | ||
1349 | if (!drm_property_change_valid_get(prop, prop_value, &ref)) | |
1350 | return -EINVAL; | |
1351 | ||
1352 | switch (obj->type) { | |
1353 | case DRM_MODE_OBJECT_CONNECTOR: { | |
1354 | struct drm_connector *connector = obj_to_connector(obj); | |
1355 | struct drm_connector_state *connector_state; | |
1356 | ||
1357 | connector_state = drm_atomic_get_connector_state(state, connector); | |
1358 | if (IS_ERR(connector_state)) { | |
1359 | ret = PTR_ERR(connector_state); | |
1360 | break; | |
1361 | } | |
1362 | ||
1363 | ret = drm_atomic_connector_set_property(connector, | |
1364 | connector_state, prop, prop_value); | |
1365 | break; | |
1366 | } | |
1367 | case DRM_MODE_OBJECT_CRTC: { | |
1368 | struct drm_crtc *crtc = obj_to_crtc(obj); | |
1369 | struct drm_crtc_state *crtc_state; | |
1370 | ||
1371 | crtc_state = drm_atomic_get_crtc_state(state, crtc); | |
1372 | if (IS_ERR(crtc_state)) { | |
1373 | ret = PTR_ERR(crtc_state); | |
1374 | break; | |
1375 | } | |
1376 | ||
1377 | ret = drm_atomic_crtc_set_property(crtc, | |
1378 | crtc_state, prop, prop_value); | |
1379 | break; | |
1380 | } | |
1381 | case DRM_MODE_OBJECT_PLANE: { | |
1382 | struct drm_plane *plane = obj_to_plane(obj); | |
1383 | struct drm_plane_state *plane_state; | |
1384 | ||
1385 | plane_state = drm_atomic_get_plane_state(state, plane); | |
1386 | if (IS_ERR(plane_state)) { | |
1387 | ret = PTR_ERR(plane_state); | |
1388 | break; | |
1389 | } | |
1390 | ||
1391 | ret = drm_atomic_plane_set_property(plane, | |
1392 | plane_state, prop, prop_value); | |
1393 | break; | |
1394 | } | |
1395 | default: | |
1396 | ret = -EINVAL; | |
1397 | break; | |
1398 | } | |
1399 | ||
1400 | drm_property_change_valid_put(prop, ref); | |
1401 | return ret; | |
1402 | } | |
1403 | ||
1404 | int drm_mode_atomic_ioctl(struct drm_device *dev, | |
1405 | void *data, struct drm_file *file_priv) | |
1406 | { | |
1407 | struct drm_mode_atomic *arg = data; | |
1408 | uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr); | |
1409 | uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr); | |
1410 | uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr); | |
1411 | uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr); | |
1412 | unsigned int copied_objs, copied_props; | |
1413 | struct drm_atomic_state *state; | |
1414 | struct drm_modeset_acquire_ctx ctx; | |
1415 | struct drm_plane *plane; | |
df63b999 ACO |
1416 | struct drm_crtc *crtc; |
1417 | struct drm_crtc_state *crtc_state; | |
d34f20d6 RC |
1418 | unsigned plane_mask = 0; |
1419 | int ret = 0; | |
1420 | unsigned int i, j; | |
1421 | ||
1422 | /* disallow for drivers not supporting atomic: */ | |
1423 | if (!drm_core_check_feature(dev, DRIVER_ATOMIC)) | |
1424 | return -EINVAL; | |
1425 | ||
1426 | /* disallow for userspace that has not enabled atomic cap (even | |
1427 | * though this may be a bit overkill, since legacy userspace | |
1428 | * wouldn't know how to call this ioctl) | |
1429 | */ | |
1430 | if (!file_priv->atomic) | |
1431 | return -EINVAL; | |
1432 | ||
1433 | if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS) | |
1434 | return -EINVAL; | |
1435 | ||
1436 | if (arg->reserved) | |
1437 | return -EINVAL; | |
1438 | ||
1439 | if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) && | |
1440 | !dev->mode_config.async_page_flip) | |
1441 | return -EINVAL; | |
1442 | ||
1443 | /* can't test and expect an event at the same time. */ | |
1444 | if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) && | |
1445 | (arg->flags & DRM_MODE_PAGE_FLIP_EVENT)) | |
1446 | return -EINVAL; | |
1447 | ||
1448 | drm_modeset_acquire_init(&ctx, 0); | |
1449 | ||
1450 | state = drm_atomic_state_alloc(dev); | |
1451 | if (!state) | |
1452 | return -ENOMEM; | |
1453 | ||
1454 | state->acquire_ctx = &ctx; | |
1455 | state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET); | |
1456 | ||
1457 | retry: | |
1458 | copied_objs = 0; | |
1459 | copied_props = 0; | |
1460 | ||
1461 | for (i = 0; i < arg->count_objs; i++) { | |
1462 | uint32_t obj_id, count_props; | |
1463 | struct drm_mode_object *obj; | |
1464 | ||
1465 | if (get_user(obj_id, objs_ptr + copied_objs)) { | |
1466 | ret = -EFAULT; | |
1467 | goto fail; | |
1468 | } | |
1469 | ||
1470 | obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY); | |
1471 | if (!obj || !obj->properties) { | |
1472 | ret = -ENOENT; | |
1473 | goto fail; | |
1474 | } | |
1475 | ||
1476 | if (obj->type == DRM_MODE_OBJECT_PLANE) { | |
1477 | plane = obj_to_plane(obj); | |
1478 | plane_mask |= (1 << drm_plane_index(plane)); | |
1479 | plane->old_fb = plane->fb; | |
1480 | } | |
1481 | ||
1482 | if (get_user(count_props, count_props_ptr + copied_objs)) { | |
1483 | ret = -EFAULT; | |
1484 | goto fail; | |
1485 | } | |
1486 | ||
1487 | copied_objs++; | |
1488 | ||
1489 | for (j = 0; j < count_props; j++) { | |
1490 | uint32_t prop_id; | |
1491 | uint64_t prop_value; | |
1492 | struct drm_property *prop; | |
1493 | ||
1494 | if (get_user(prop_id, props_ptr + copied_props)) { | |
1495 | ret = -EFAULT; | |
1496 | goto fail; | |
1497 | } | |
1498 | ||
1499 | prop = drm_property_find(dev, prop_id); | |
1500 | if (!prop) { | |
1501 | ret = -ENOENT; | |
1502 | goto fail; | |
1503 | } | |
1504 | ||
42c5814c GR |
1505 | if (copy_from_user(&prop_value, |
1506 | prop_values_ptr + copied_props, | |
1507 | sizeof(prop_value))) { | |
d34f20d6 RC |
1508 | ret = -EFAULT; |
1509 | goto fail; | |
1510 | } | |
1511 | ||
1512 | ret = atomic_set_prop(state, obj, prop, prop_value); | |
1513 | if (ret) | |
1514 | goto fail; | |
1515 | ||
1516 | copied_props++; | |
1517 | } | |
1518 | } | |
1519 | ||
1520 | if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) { | |
df63b999 | 1521 | for_each_crtc_in_state(state, crtc, crtc_state, i) { |
d34f20d6 RC |
1522 | struct drm_pending_vblank_event *e; |
1523 | ||
d34f20d6 RC |
1524 | e = create_vblank_event(dev, file_priv, arg->user_data); |
1525 | if (!e) { | |
1526 | ret = -ENOMEM; | |
1527 | goto fail; | |
1528 | } | |
1529 | ||
1530 | crtc_state->event = e; | |
1531 | } | |
1532 | } | |
1533 | ||
1534 | if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) { | |
1535 | ret = drm_atomic_check_only(state); | |
1536 | /* _check_only() does not free state, unlike _commit() */ | |
1537 | drm_atomic_state_free(state); | |
1538 | } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) { | |
1539 | ret = drm_atomic_async_commit(state); | |
1540 | } else { | |
1541 | ret = drm_atomic_commit(state); | |
1542 | } | |
1543 | ||
1544 | /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping | |
1545 | * locks (ie. while it is still safe to deref plane->state). We | |
1546 | * need to do this here because the driver entry points cannot | |
1547 | * distinguish between legacy and atomic ioctls. | |
1548 | */ | |
1549 | drm_for_each_plane_mask(plane, dev, plane_mask) { | |
1550 | if (ret == 0) { | |
1551 | struct drm_framebuffer *new_fb = plane->state->fb; | |
1552 | if (new_fb) | |
1553 | drm_framebuffer_reference(new_fb); | |
1554 | plane->fb = new_fb; | |
1555 | plane->crtc = plane->state->crtc; | |
1556 | } else { | |
1557 | plane->old_fb = NULL; | |
1558 | } | |
1559 | if (plane->old_fb) { | |
1560 | drm_framebuffer_unreference(plane->old_fb); | |
1561 | plane->old_fb = NULL; | |
1562 | } | |
1563 | } | |
1564 | ||
1565 | drm_modeset_drop_locks(&ctx); | |
1566 | drm_modeset_acquire_fini(&ctx); | |
1567 | ||
1568 | return ret; | |
1569 | ||
1570 | fail: | |
1571 | if (ret == -EDEADLK) | |
1572 | goto backoff; | |
1573 | ||
1574 | if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) { | |
df63b999 | 1575 | for_each_crtc_in_state(state, crtc, crtc_state, i) { |
d34f20d6 RC |
1576 | destroy_vblank_event(dev, file_priv, crtc_state->event); |
1577 | crtc_state->event = NULL; | |
1578 | } | |
1579 | } | |
1580 | ||
1581 | drm_atomic_state_free(state); | |
1582 | ||
1583 | drm_modeset_drop_locks(&ctx); | |
1584 | drm_modeset_acquire_fini(&ctx); | |
1585 | ||
1586 | return ret; | |
1587 | ||
1588 | backoff: | |
1589 | drm_atomic_state_clear(state); | |
1590 | drm_modeset_backoff(&ctx); | |
1591 | ||
1592 | goto retry; | |
1593 | } |