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