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