]>
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 | ||
0500c04e SR |
29 | #include <linux/sync_file.h> |
30 | ||
cc4ceb48 | 31 | #include <drm/drm_atomic.h> |
72fdb40c | 32 | #include <drm/drm_atomic_uapi.h> |
75146591 | 33 | #include <drm/drm_bridge.h> |
0500c04e SR |
34 | #include <drm/drm_debugfs.h> |
35 | #include <drm/drm_device.h> | |
36 | #include <drm/drm_drv.h> | |
37 | #include <drm/drm_file.h> | |
38 | #include <drm/drm_fourcc.h> | |
5488dc16 | 39 | #include <drm/drm_mode.h> |
fceffb32 | 40 | #include <drm/drm_print.h> |
935774cd | 41 | #include <drm/drm_writeback.h> |
cc4ceb48 | 42 | |
be35f94f | 43 | #include "drm_crtc_internal.h" |
f02b604b | 44 | #include "drm_internal.h" |
be35f94f | 45 | |
b3ba3f6f | 46 | void __drm_crtc_commit_free(struct kref *kref) |
3b24f7d6 DV |
47 | { |
48 | struct drm_crtc_commit *commit = | |
49 | container_of(kref, struct drm_crtc_commit, ref); | |
50 | ||
51 | kfree(commit); | |
52 | } | |
b3ba3f6f | 53 | EXPORT_SYMBOL(__drm_crtc_commit_free); |
3b24f7d6 | 54 | |
b99c2c95 MR |
55 | /** |
56 | * drm_crtc_commit_wait - Waits for a commit to complete | |
57 | * @commit: &drm_crtc_commit to wait for | |
58 | * | |
59 | * Waits for a given &drm_crtc_commit to be programmed into the | |
60 | * hardware and flipped to. | |
61 | * | |
62 | * Returns: | |
63 | * | |
64 | * 0 on success, a negative error code otherwise. | |
65 | */ | |
66 | int drm_crtc_commit_wait(struct drm_crtc_commit *commit) | |
67 | { | |
68 | unsigned long timeout = 10 * HZ; | |
69 | int ret; | |
70 | ||
71 | if (!commit) | |
72 | return 0; | |
73 | ||
74 | ret = wait_for_completion_timeout(&commit->hw_done, timeout); | |
75 | if (!ret) { | |
76 | DRM_ERROR("hw_done timed out\n"); | |
77 | return -ETIMEDOUT; | |
78 | } | |
79 | ||
80 | /* | |
81 | * Currently no support for overwriting flips, hence | |
82 | * stall for previous one to execute completely. | |
83 | */ | |
84 | ret = wait_for_completion_timeout(&commit->flip_done, timeout); | |
85 | if (!ret) { | |
86 | DRM_ERROR("flip_done timed out\n"); | |
87 | return -ETIMEDOUT; | |
88 | } | |
89 | ||
90 | return 0; | |
91 | } | |
92 | EXPORT_SYMBOL(drm_crtc_commit_wait); | |
93 | ||
036ef573 ML |
94 | /** |
95 | * drm_atomic_state_default_release - | |
96 | * release memory initialized by drm_atomic_state_init | |
97 | * @state: atomic state | |
98 | * | |
99 | * Free all the memory allocated by drm_atomic_state_init. | |
da6c0596 DV |
100 | * This should only be used by drivers which are still subclassing |
101 | * &drm_atomic_state and haven't switched to &drm_private_state yet. | |
036ef573 ML |
102 | */ |
103 | void drm_atomic_state_default_release(struct drm_atomic_state *state) | |
cc4ceb48 DV |
104 | { |
105 | kfree(state->connectors); | |
cc4ceb48 | 106 | kfree(state->crtcs); |
cc4ceb48 | 107 | kfree(state->planes); |
b430c27a | 108 | kfree(state->private_objs); |
cc4ceb48 | 109 | } |
036ef573 | 110 | EXPORT_SYMBOL(drm_atomic_state_default_release); |
cc4ceb48 DV |
111 | |
112 | /** | |
036ef573 | 113 | * drm_atomic_state_init - init new atomic state |
cc4ceb48 | 114 | * @dev: DRM device |
036ef573 | 115 | * @state: atomic state |
cc4ceb48 | 116 | * |
036ef573 | 117 | * Default implementation for filling in a new atomic state. |
da6c0596 DV |
118 | * This should only be used by drivers which are still subclassing |
119 | * &drm_atomic_state and haven't switched to &drm_private_state yet. | |
cc4ceb48 | 120 | */ |
036ef573 ML |
121 | int |
122 | drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state) | |
cc4ceb48 | 123 | { |
0853695c CW |
124 | kref_init(&state->ref); |
125 | ||
d34f20d6 RC |
126 | /* TODO legacy paths should maybe do a better job about |
127 | * setting this appropriately? | |
128 | */ | |
129 | state->allow_modeset = true; | |
130 | ||
cc4ceb48 DV |
131 | state->crtcs = kcalloc(dev->mode_config.num_crtc, |
132 | sizeof(*state->crtcs), GFP_KERNEL); | |
133 | if (!state->crtcs) | |
134 | goto fail; | |
cc4ceb48 DV |
135 | state->planes = kcalloc(dev->mode_config.num_total_plane, |
136 | sizeof(*state->planes), GFP_KERNEL); | |
137 | if (!state->planes) | |
138 | goto fail; | |
cc4ceb48 DV |
139 | |
140 | state->dev = dev; | |
141 | ||
036ef573 | 142 | DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state); |
cc4ceb48 | 143 | |
036ef573 | 144 | return 0; |
cc4ceb48 | 145 | fail: |
036ef573 ML |
146 | drm_atomic_state_default_release(state); |
147 | return -ENOMEM; | |
148 | } | |
149 | EXPORT_SYMBOL(drm_atomic_state_init); | |
150 | ||
151 | /** | |
152 | * drm_atomic_state_alloc - allocate atomic state | |
153 | * @dev: DRM device | |
154 | * | |
155 | * This allocates an empty atomic state to track updates. | |
156 | */ | |
157 | struct drm_atomic_state * | |
158 | drm_atomic_state_alloc(struct drm_device *dev) | |
159 | { | |
160 | struct drm_mode_config *config = &dev->mode_config; | |
036ef573 ML |
161 | |
162 | if (!config->funcs->atomic_state_alloc) { | |
ac7c7483 DK |
163 | struct drm_atomic_state *state; |
164 | ||
036ef573 ML |
165 | state = kzalloc(sizeof(*state), GFP_KERNEL); |
166 | if (!state) | |
167 | return NULL; | |
168 | if (drm_atomic_state_init(dev, state) < 0) { | |
169 | kfree(state); | |
170 | return NULL; | |
171 | } | |
172 | return state; | |
173 | } | |
cc4ceb48 | 174 | |
036ef573 | 175 | return config->funcs->atomic_state_alloc(dev); |
cc4ceb48 DV |
176 | } |
177 | EXPORT_SYMBOL(drm_atomic_state_alloc); | |
178 | ||
179 | /** | |
036ef573 | 180 | * drm_atomic_state_default_clear - clear base atomic state |
cc4ceb48 DV |
181 | * @state: atomic state |
182 | * | |
036ef573 | 183 | * Default implementation for clearing atomic state. |
da6c0596 DV |
184 | * This should only be used by drivers which are still subclassing |
185 | * &drm_atomic_state and haven't switched to &drm_private_state yet. | |
cc4ceb48 | 186 | */ |
036ef573 | 187 | void drm_atomic_state_default_clear(struct drm_atomic_state *state) |
cc4ceb48 DV |
188 | { |
189 | struct drm_device *dev = state->dev; | |
6f75cea6 | 190 | struct drm_mode_config *config = &dev->mode_config; |
cc4ceb48 DV |
191 | int i; |
192 | ||
17a38d9c | 193 | DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state); |
cc4ceb48 | 194 | |
f52b69f1 | 195 | for (i = 0; i < state->num_connector; i++) { |
63e83c1d | 196 | struct drm_connector *connector = state->connectors[i].ptr; |
cc4ceb48 DV |
197 | |
198 | if (!connector) | |
199 | continue; | |
200 | ||
d2307dea | 201 | connector->funcs->atomic_destroy_state(connector, |
63e83c1d DV |
202 | state->connectors[i].state); |
203 | state->connectors[i].ptr = NULL; | |
204 | state->connectors[i].state = NULL; | |
f0b408ee VS |
205 | state->connectors[i].old_state = NULL; |
206 | state->connectors[i].new_state = NULL; | |
ad093607 | 207 | drm_connector_put(connector); |
cc4ceb48 DV |
208 | } |
209 | ||
6f75cea6 | 210 | for (i = 0; i < config->num_crtc; i++) { |
5d943aa6 | 211 | struct drm_crtc *crtc = state->crtcs[i].ptr; |
cc4ceb48 DV |
212 | |
213 | if (!crtc) | |
214 | continue; | |
215 | ||
216 | crtc->funcs->atomic_destroy_state(crtc, | |
5d943aa6 | 217 | state->crtcs[i].state); |
3b24f7d6 | 218 | |
5d943aa6 DV |
219 | state->crtcs[i].ptr = NULL; |
220 | state->crtcs[i].state = NULL; | |
f0b408ee VS |
221 | state->crtcs[i].old_state = NULL; |
222 | state->crtcs[i].new_state = NULL; | |
4364bcb2 LL |
223 | |
224 | if (state->crtcs[i].commit) { | |
225 | drm_crtc_commit_put(state->crtcs[i].commit); | |
226 | state->crtcs[i].commit = NULL; | |
227 | } | |
cc4ceb48 DV |
228 | } |
229 | ||
6f75cea6 | 230 | for (i = 0; i < config->num_total_plane; i++) { |
b8b5342b | 231 | struct drm_plane *plane = state->planes[i].ptr; |
cc4ceb48 DV |
232 | |
233 | if (!plane) | |
234 | continue; | |
235 | ||
236 | plane->funcs->atomic_destroy_state(plane, | |
b8b5342b DV |
237 | state->planes[i].state); |
238 | state->planes[i].ptr = NULL; | |
239 | state->planes[i].state = NULL; | |
f0b408ee VS |
240 | state->planes[i].old_state = NULL; |
241 | state->planes[i].new_state = NULL; | |
cc4ceb48 | 242 | } |
b430c27a PD |
243 | |
244 | for (i = 0; i < state->num_private_objs; i++) { | |
a4370c77 | 245 | struct drm_private_obj *obj = state->private_objs[i].ptr; |
b430c27a | 246 | |
a4370c77 VS |
247 | obj->funcs->atomic_destroy_state(obj, |
248 | state->private_objs[i].state); | |
249 | state->private_objs[i].ptr = NULL; | |
250 | state->private_objs[i].state = NULL; | |
b5cb2e5a VS |
251 | state->private_objs[i].old_state = NULL; |
252 | state->private_objs[i].new_state = NULL; | |
b430c27a PD |
253 | } |
254 | state->num_private_objs = 0; | |
255 | ||
21a01abb ML |
256 | if (state->fake_commit) { |
257 | drm_crtc_commit_put(state->fake_commit); | |
258 | state->fake_commit = NULL; | |
259 | } | |
cc4ceb48 | 260 | } |
036ef573 ML |
261 | EXPORT_SYMBOL(drm_atomic_state_default_clear); |
262 | ||
263 | /** | |
264 | * drm_atomic_state_clear - clear state object | |
265 | * @state: atomic state | |
266 | * | |
267 | * When the w/w mutex algorithm detects a deadlock we need to back off and drop | |
268 | * all locks. So someone else could sneak in and change the current modeset | |
269 | * configuration. Which means that all the state assembled in @state is no | |
270 | * longer an atomic update to the current state, but to some arbitrary earlier | |
d574528a DV |
271 | * state. Which could break assumptions the driver's |
272 | * &drm_mode_config_funcs.atomic_check likely relies on. | |
036ef573 ML |
273 | * |
274 | * Hence we must clear all cached state and completely start over, using this | |
275 | * function. | |
276 | */ | |
277 | void drm_atomic_state_clear(struct drm_atomic_state *state) | |
278 | { | |
279 | struct drm_device *dev = state->dev; | |
280 | struct drm_mode_config *config = &dev->mode_config; | |
281 | ||
282 | if (config->funcs->atomic_state_clear) | |
283 | config->funcs->atomic_state_clear(state); | |
284 | else | |
285 | drm_atomic_state_default_clear(state); | |
286 | } | |
cc4ceb48 DV |
287 | EXPORT_SYMBOL(drm_atomic_state_clear); |
288 | ||
289 | /** | |
0853695c CW |
290 | * __drm_atomic_state_free - free all memory for an atomic state |
291 | * @ref: This atomic state to deallocate | |
cc4ceb48 DV |
292 | * |
293 | * This frees all memory associated with an atomic state, including all the | |
42240c90 | 294 | * per-object state for planes, CRTCs and connectors. |
cc4ceb48 | 295 | */ |
0853695c | 296 | void __drm_atomic_state_free(struct kref *ref) |
cc4ceb48 | 297 | { |
0853695c CW |
298 | struct drm_atomic_state *state = container_of(ref, typeof(*state), ref); |
299 | struct drm_mode_config *config = &state->dev->mode_config; | |
036ef573 | 300 | |
cc4ceb48 DV |
301 | drm_atomic_state_clear(state); |
302 | ||
17a38d9c | 303 | DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state); |
cc4ceb48 | 304 | |
036ef573 ML |
305 | if (config->funcs->atomic_state_free) { |
306 | config->funcs->atomic_state_free(state); | |
307 | } else { | |
308 | drm_atomic_state_default_release(state); | |
309 | kfree(state); | |
310 | } | |
cc4ceb48 | 311 | } |
0853695c | 312 | EXPORT_SYMBOL(__drm_atomic_state_free); |
cc4ceb48 DV |
313 | |
314 | /** | |
42240c90 | 315 | * drm_atomic_get_crtc_state - get CRTC state |
cc4ceb48 | 316 | * @state: global atomic state object |
42240c90 | 317 | * @crtc: CRTC to get state object for |
cc4ceb48 | 318 | * |
42240c90 TR |
319 | * This function returns the CRTC state for the given CRTC, allocating it if |
320 | * needed. It will also grab the relevant CRTC lock to make sure that the state | |
cc4ceb48 DV |
321 | * is consistent. |
322 | * | |
fb6473a4 DV |
323 | * WARNING: Drivers may only add new CRTC states to a @state if |
324 | * drm_atomic_state.allow_modeset is set, or if it's a driver-internal commit | |
325 | * not created by userspace through an IOCTL call. | |
326 | * | |
cc4ceb48 DV |
327 | * Returns: |
328 | * | |
329 | * Either the allocated state or the error code encoded into the pointer. When | |
330 | * the error is EDEADLK then the w/w mutex code has detected a deadlock and the | |
331 | * entire atomic sequence must be restarted. All other errors are fatal. | |
332 | */ | |
333 | struct drm_crtc_state * | |
334 | drm_atomic_get_crtc_state(struct drm_atomic_state *state, | |
335 | struct drm_crtc *crtc) | |
336 | { | |
1b26a5e1 | 337 | int ret, index = drm_crtc_index(crtc); |
cc4ceb48 DV |
338 | struct drm_crtc_state *crtc_state; |
339 | ||
7f4eaa89 ML |
340 | WARN_ON(!state->acquire_ctx); |
341 | ||
1b26a5e1 ML |
342 | crtc_state = drm_atomic_get_existing_crtc_state(state, crtc); |
343 | if (crtc_state) | |
344 | return crtc_state; | |
cc4ceb48 DV |
345 | |
346 | ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx); | |
347 | if (ret) | |
348 | return ERR_PTR(ret); | |
349 | ||
350 | crtc_state = crtc->funcs->atomic_duplicate_state(crtc); | |
351 | if (!crtc_state) | |
352 | return ERR_PTR(-ENOMEM); | |
353 | ||
5d943aa6 | 354 | state->crtcs[index].state = crtc_state; |
581e49fe ML |
355 | state->crtcs[index].old_state = crtc->state; |
356 | state->crtcs[index].new_state = crtc_state; | |
5d943aa6 | 357 | state->crtcs[index].ptr = crtc; |
cc4ceb48 DV |
358 | crtc_state->state = state; |
359 | ||
fa3ab4c2 VS |
360 | DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n", |
361 | crtc->base.id, crtc->name, crtc_state, state); | |
cc4ceb48 DV |
362 | |
363 | return crtc_state; | |
364 | } | |
365 | EXPORT_SYMBOL(drm_atomic_get_crtc_state); | |
366 | ||
b2432adf VS |
367 | static int drm_atomic_crtc_check(const struct drm_crtc_state *old_crtc_state, |
368 | const struct drm_crtc_state *new_crtc_state) | |
5e743737 | 369 | { |
b2432adf VS |
370 | struct drm_crtc *crtc = new_crtc_state->crtc; |
371 | ||
5e743737 RC |
372 | /* NOTE: we explicitly don't enforce constraints such as primary |
373 | * layer covering entire screen, since that is something we want | |
374 | * to allow (on hw that supports it). For hw that does not, it | |
375 | * should be checked in driver's crtc->atomic_check() vfunc. | |
376 | * | |
377 | * TODO: Add generic modeset state checks once we support those. | |
378 | */ | |
eab3bbef | 379 | |
b2432adf | 380 | if (new_crtc_state->active && !new_crtc_state->enable) { |
fa3ab4c2 VS |
381 | DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n", |
382 | crtc->base.id, crtc->name); | |
eab3bbef DV |
383 | return -EINVAL; |
384 | } | |
385 | ||
99cf4a29 DS |
386 | /* The state->enable vs. state->mode_blob checks can be WARN_ON, |
387 | * as this is a kernel-internal detail that userspace should never | |
388 | * be able to trigger. */ | |
389 | if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) && | |
b2432adf | 390 | WARN_ON(new_crtc_state->enable && !new_crtc_state->mode_blob)) { |
fa3ab4c2 VS |
391 | DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n", |
392 | crtc->base.id, crtc->name); | |
99cf4a29 DS |
393 | return -EINVAL; |
394 | } | |
395 | ||
396 | if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) && | |
b2432adf | 397 | WARN_ON(!new_crtc_state->enable && new_crtc_state->mode_blob)) { |
fa3ab4c2 VS |
398 | DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n", |
399 | crtc->base.id, crtc->name); | |
99cf4a29 DS |
400 | return -EINVAL; |
401 | } | |
402 | ||
4cba6850 DV |
403 | /* |
404 | * Reject event generation for when a CRTC is off and stays off. | |
405 | * It wouldn't be hard to implement this, but userspace has a track | |
406 | * record of happily burning through 100% cpu (or worse, crash) when the | |
407 | * display pipe is suspended. To avoid all that fun just reject updates | |
408 | * that ask for events since likely that indicates a bug in the | |
409 | * compositor's drawing loop. This is consistent with the vblank IOCTL | |
410 | * and legacy page_flip IOCTL which also reject service on a disabled | |
411 | * pipe. | |
412 | */ | |
b2432adf VS |
413 | if (new_crtc_state->event && |
414 | !new_crtc_state->active && !old_crtc_state->active) { | |
6ac7c548 RK |
415 | DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requesting event but off\n", |
416 | crtc->base.id, crtc->name); | |
4cba6850 DV |
417 | return -EINVAL; |
418 | } | |
419 | ||
5e743737 RC |
420 | return 0; |
421 | } | |
422 | ||
fceffb32 RC |
423 | static void drm_atomic_crtc_print_state(struct drm_printer *p, |
424 | const struct drm_crtc_state *state) | |
425 | { | |
426 | struct drm_crtc *crtc = state->crtc; | |
427 | ||
428 | drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name); | |
429 | drm_printf(p, "\tenable=%d\n", state->enable); | |
430 | drm_printf(p, "\tactive=%d\n", state->active); | |
1452c25b | 431 | drm_printf(p, "\tself_refresh_active=%d\n", state->self_refresh_active); |
fceffb32 RC |
432 | drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed); |
433 | drm_printf(p, "\tmode_changed=%d\n", state->mode_changed); | |
434 | drm_printf(p, "\tactive_changed=%d\n", state->active_changed); | |
435 | drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed); | |
436 | drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed); | |
437 | drm_printf(p, "\tplane_mask=%x\n", state->plane_mask); | |
438 | drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask); | |
439 | drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask); | |
440 | drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode)); | |
441 | ||
442 | if (crtc->funcs->atomic_print_state) | |
443 | crtc->funcs->atomic_print_state(p, state); | |
444 | } | |
445 | ||
935774cd BS |
446 | static int drm_atomic_connector_check(struct drm_connector *connector, |
447 | struct drm_connector_state *state) | |
448 | { | |
449 | struct drm_crtc_state *crtc_state; | |
450 | struct drm_writeback_job *writeback_job = state->writeback_job; | |
47e22ff1 RS |
451 | const struct drm_display_info *info = &connector->display_info; |
452 | ||
453 | state->max_bpc = info->bpc ? info->bpc : 8; | |
454 | if (connector->max_bpc_property) | |
455 | state->max_bpc = min(state->max_bpc, state->max_requested_bpc); | |
935774cd BS |
456 | |
457 | if ((connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK) || !writeback_job) | |
458 | return 0; | |
459 | ||
460 | if (writeback_job->fb && !state->crtc) { | |
461 | DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] framebuffer without CRTC\n", | |
462 | connector->base.id, connector->name); | |
463 | return -EINVAL; | |
464 | } | |
465 | ||
466 | if (state->crtc) | |
467 | crtc_state = drm_atomic_get_existing_crtc_state(state->state, | |
468 | state->crtc); | |
469 | ||
470 | if (writeback_job->fb && !crtc_state->active) { | |
471 | DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] has framebuffer, but [CRTC:%d] is off\n", | |
472 | connector->base.id, connector->name, | |
473 | state->crtc->base.id); | |
474 | return -EINVAL; | |
475 | } | |
476 | ||
8581d510 LLATC |
477 | if (!writeback_job->fb) { |
478 | if (writeback_job->out_fence) { | |
479 | DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] requesting out-fence without framebuffer\n", | |
480 | connector->base.id, connector->name); | |
481 | return -EINVAL; | |
482 | } | |
483 | ||
484 | drm_writeback_cleanup_job(writeback_job); | |
485 | state->writeback_job = NULL; | |
b13cc8dd BS |
486 | } |
487 | ||
935774cd BS |
488 | return 0; |
489 | } | |
490 | ||
cc4ceb48 DV |
491 | /** |
492 | * drm_atomic_get_plane_state - get plane state | |
493 | * @state: global atomic state object | |
494 | * @plane: plane to get state object for | |
495 | * | |
496 | * This function returns the plane state for the given plane, allocating it if | |
497 | * needed. It will also grab the relevant plane lock to make sure that the state | |
498 | * is consistent. | |
499 | * | |
500 | * Returns: | |
501 | * | |
502 | * Either the allocated state or the error code encoded into the pointer. When | |
503 | * the error is EDEADLK then the w/w mutex code has detected a deadlock and the | |
504 | * entire atomic sequence must be restarted. All other errors are fatal. | |
505 | */ | |
506 | struct drm_plane_state * | |
507 | drm_atomic_get_plane_state(struct drm_atomic_state *state, | |
508 | struct drm_plane *plane) | |
509 | { | |
1b26a5e1 | 510 | int ret, index = drm_plane_index(plane); |
cc4ceb48 DV |
511 | struct drm_plane_state *plane_state; |
512 | ||
7f4eaa89 ML |
513 | WARN_ON(!state->acquire_ctx); |
514 | ||
e00fb856 VS |
515 | /* the legacy pointers should never be set */ |
516 | WARN_ON(plane->fb); | |
517 | WARN_ON(plane->old_fb); | |
518 | WARN_ON(plane->crtc); | |
519 | ||
1b26a5e1 ML |
520 | plane_state = drm_atomic_get_existing_plane_state(state, plane); |
521 | if (plane_state) | |
522 | return plane_state; | |
cc4ceb48 | 523 | |
4d02e2de | 524 | ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx); |
cc4ceb48 DV |
525 | if (ret) |
526 | return ERR_PTR(ret); | |
527 | ||
528 | plane_state = plane->funcs->atomic_duplicate_state(plane); | |
529 | if (!plane_state) | |
530 | return ERR_PTR(-ENOMEM); | |
531 | ||
b8b5342b DV |
532 | state->planes[index].state = plane_state; |
533 | state->planes[index].ptr = plane; | |
581e49fe ML |
534 | state->planes[index].old_state = plane->state; |
535 | state->planes[index].new_state = plane_state; | |
cc4ceb48 DV |
536 | plane_state->state = state; |
537 | ||
9f4c97a2 VS |
538 | DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n", |
539 | plane->base.id, plane->name, plane_state, state); | |
cc4ceb48 DV |
540 | |
541 | if (plane_state->crtc) { | |
542 | struct drm_crtc_state *crtc_state; | |
543 | ||
544 | crtc_state = drm_atomic_get_crtc_state(state, | |
545 | plane_state->crtc); | |
546 | if (IS_ERR(crtc_state)) | |
547 | return ERR_CAST(crtc_state); | |
548 | } | |
549 | ||
550 | return plane_state; | |
551 | } | |
552 | EXPORT_SYMBOL(drm_atomic_get_plane_state); | |
553 | ||
f8aeb41c | 554 | static bool |
d9be05b7 VS |
555 | plane_switching_crtc(const struct drm_plane_state *old_plane_state, |
556 | const struct drm_plane_state *new_plane_state) | |
f8aeb41c | 557 | { |
d9be05b7 | 558 | if (!old_plane_state->crtc || !new_plane_state->crtc) |
f8aeb41c DV |
559 | return false; |
560 | ||
d9be05b7 | 561 | if (old_plane_state->crtc == new_plane_state->crtc) |
f8aeb41c DV |
562 | return false; |
563 | ||
564 | /* This could be refined, but currently there's no helper or driver code | |
565 | * to implement direct switching of active planes nor userspace to take | |
566 | * advantage of more direct plane switching without the intermediate | |
567 | * full OFF state. | |
568 | */ | |
569 | return true; | |
570 | } | |
571 | ||
5e743737 RC |
572 | /** |
573 | * drm_atomic_plane_check - check plane state | |
d9be05b7 VS |
574 | * @old_plane_state: old plane state to check |
575 | * @new_plane_state: new plane state to check | |
5e743737 RC |
576 | * |
577 | * Provides core sanity checks for plane state. | |
578 | * | |
579 | * RETURNS: | |
580 | * Zero on success, error code on failure | |
581 | */ | |
d9be05b7 VS |
582 | static int drm_atomic_plane_check(const struct drm_plane_state *old_plane_state, |
583 | const struct drm_plane_state *new_plane_state) | |
5e743737 | 584 | { |
d9be05b7 VS |
585 | struct drm_plane *plane = new_plane_state->plane; |
586 | struct drm_crtc *crtc = new_plane_state->crtc; | |
587 | const struct drm_framebuffer *fb = new_plane_state->fb; | |
5e743737 | 588 | unsigned int fb_width, fb_height; |
d3b21767 LS |
589 | struct drm_mode_rect *clips; |
590 | uint32_t num_clips; | |
ead8610d | 591 | int ret; |
5e743737 RC |
592 | |
593 | /* either *both* CRTC and FB must be set, or neither */ | |
d9be05b7 | 594 | if (crtc && !fb) { |
b6f690ab VS |
595 | DRM_DEBUG_ATOMIC("[PLANE:%d:%s] CRTC set but no FB\n", |
596 | plane->base.id, plane->name); | |
5e743737 | 597 | return -EINVAL; |
d9be05b7 | 598 | } else if (fb && !crtc) { |
b6f690ab VS |
599 | DRM_DEBUG_ATOMIC("[PLANE:%d:%s] FB set but no CRTC\n", |
600 | plane->base.id, plane->name); | |
5e743737 RC |
601 | return -EINVAL; |
602 | } | |
603 | ||
604 | /* if disabled, we don't care about the rest of the state: */ | |
d9be05b7 | 605 | if (!crtc) |
5e743737 RC |
606 | return 0; |
607 | ||
608 | /* Check whether this plane is usable on this CRTC */ | |
d9be05b7 | 609 | if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) { |
b6f690ab | 610 | DRM_DEBUG_ATOMIC("Invalid [CRTC:%d:%s] for [PLANE:%d:%s]\n", |
d9be05b7 | 611 | crtc->base.id, crtc->name, |
b6f690ab | 612 | plane->base.id, plane->name); |
5e743737 RC |
613 | return -EINVAL; |
614 | } | |
615 | ||
616 | /* Check whether this plane supports the fb pixel format. */ | |
d9be05b7 VS |
617 | ret = drm_plane_check_pixel_format(plane, fb->format->format, |
618 | fb->modifier); | |
ead8610d | 619 | if (ret) { |
b3c11ac2 | 620 | struct drm_format_name_buf format_name; |
948de842 | 621 | |
b6f690ab VS |
622 | DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid pixel format %s, modifier 0x%llx\n", |
623 | plane->base.id, plane->name, | |
d9be05b7 | 624 | drm_get_format_name(fb->format->format, |
23163a7d | 625 | &format_name), |
d9be05b7 | 626 | fb->modifier); |
ead8610d | 627 | return ret; |
5e743737 RC |
628 | } |
629 | ||
630 | /* Give drivers some help against integer overflows */ | |
d9be05b7 VS |
631 | if (new_plane_state->crtc_w > INT_MAX || |
632 | new_plane_state->crtc_x > INT_MAX - (int32_t) new_plane_state->crtc_w || | |
633 | new_plane_state->crtc_h > INT_MAX || | |
634 | new_plane_state->crtc_y > INT_MAX - (int32_t) new_plane_state->crtc_h) { | |
b6f690ab VS |
635 | DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid CRTC coordinates %ux%u+%d+%d\n", |
636 | plane->base.id, plane->name, | |
d9be05b7 VS |
637 | new_plane_state->crtc_w, new_plane_state->crtc_h, |
638 | new_plane_state->crtc_x, new_plane_state->crtc_y); | |
5e743737 RC |
639 | return -ERANGE; |
640 | } | |
641 | ||
d9be05b7 VS |
642 | fb_width = fb->width << 16; |
643 | fb_height = fb->height << 16; | |
5e743737 RC |
644 | |
645 | /* Make sure source coordinates are inside the fb. */ | |
d9be05b7 VS |
646 | if (new_plane_state->src_w > fb_width || |
647 | new_plane_state->src_x > fb_width - new_plane_state->src_w || | |
648 | new_plane_state->src_h > fb_height || | |
649 | new_plane_state->src_y > fb_height - new_plane_state->src_h) { | |
b6f690ab | 650 | DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid source coordinates " |
0338f0d0 | 651 | "%u.%06ux%u.%06u+%u.%06u+%u.%06u (fb %ux%u)\n", |
b6f690ab | 652 | plane->base.id, plane->name, |
d9be05b7 VS |
653 | new_plane_state->src_w >> 16, |
654 | ((new_plane_state->src_w & 0xffff) * 15625) >> 10, | |
655 | new_plane_state->src_h >> 16, | |
656 | ((new_plane_state->src_h & 0xffff) * 15625) >> 10, | |
657 | new_plane_state->src_x >> 16, | |
658 | ((new_plane_state->src_x & 0xffff) * 15625) >> 10, | |
659 | new_plane_state->src_y >> 16, | |
660 | ((new_plane_state->src_y & 0xffff) * 15625) >> 10, | |
661 | fb->width, fb->height); | |
5e743737 RC |
662 | return -ENOSPC; |
663 | } | |
664 | ||
d3b21767 LS |
665 | clips = drm_plane_get_damage_clips(new_plane_state); |
666 | num_clips = drm_plane_get_damage_clips_count(new_plane_state); | |
667 | ||
668 | /* Make sure damage clips are valid and inside the fb. */ | |
669 | while (num_clips > 0) { | |
670 | if (clips->x1 >= clips->x2 || | |
671 | clips->y1 >= clips->y2 || | |
672 | clips->x1 < 0 || | |
673 | clips->y1 < 0 || | |
674 | clips->x2 > fb_width || | |
675 | clips->y2 > fb_height) { | |
676 | DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid damage clip %d %d %d %d\n", | |
677 | plane->base.id, plane->name, clips->x1, | |
678 | clips->y1, clips->x2, clips->y2); | |
679 | return -EINVAL; | |
680 | } | |
681 | clips++; | |
682 | num_clips--; | |
683 | } | |
684 | ||
d9be05b7 | 685 | if (plane_switching_crtc(old_plane_state, new_plane_state)) { |
9f4c97a2 VS |
686 | DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n", |
687 | plane->base.id, plane->name); | |
f8aeb41c DV |
688 | return -EINVAL; |
689 | } | |
690 | ||
5e743737 RC |
691 | return 0; |
692 | } | |
693 | ||
fceffb32 RC |
694 | static void drm_atomic_plane_print_state(struct drm_printer *p, |
695 | const struct drm_plane_state *state) | |
696 | { | |
697 | struct drm_plane *plane = state->plane; | |
698 | struct drm_rect src = drm_plane_state_src(state); | |
699 | struct drm_rect dest = drm_plane_state_dest(state); | |
700 | ||
701 | drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name); | |
702 | drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)"); | |
703 | drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0); | |
f02b604b NT |
704 | if (state->fb) |
705 | drm_framebuffer_print_info(p, 2, state->fb); | |
fceffb32 RC |
706 | drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest)); |
707 | drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src)); | |
708 | drm_printf(p, "\trotation=%x\n", state->rotation); | |
f8878bb2 | 709 | drm_printf(p, "\tnormalized-zpos=%x\n", state->normalized_zpos); |
56dbbaff VS |
710 | drm_printf(p, "\tcolor-encoding=%s\n", |
711 | drm_get_color_encoding_name(state->color_encoding)); | |
712 | drm_printf(p, "\tcolor-range=%s\n", | |
713 | drm_get_color_range_name(state->color_range)); | |
fceffb32 RC |
714 | |
715 | if (plane->funcs->atomic_print_state) | |
716 | plane->funcs->atomic_print_state(p, state); | |
717 | } | |
718 | ||
da6c0596 DV |
719 | /** |
720 | * DOC: handling driver private state | |
721 | * | |
722 | * Very often the DRM objects exposed to userspace in the atomic modeset api | |
723 | * (&drm_connector, &drm_crtc and &drm_plane) do not map neatly to the | |
724 | * underlying hardware. Especially for any kind of shared resources (e.g. shared | |
725 | * clocks, scaler units, bandwidth and fifo limits shared among a group of | |
726 | * planes or CRTCs, and so on) it makes sense to model these as independent | |
727 | * objects. Drivers then need to do similar state tracking and commit ordering for | |
728 | * such private (since not exposed to userpace) objects as the atomic core and | |
729 | * helpers already provide for connectors, planes and CRTCs. | |
730 | * | |
731 | * To make this easier on drivers the atomic core provides some support to track | |
732 | * driver private state objects using struct &drm_private_obj, with the | |
733 | * associated state struct &drm_private_state. | |
734 | * | |
735 | * Similar to userspace-exposed objects, private state structures can be | |
0380c684 DV |
736 | * acquired by calling drm_atomic_get_private_obj_state(). This also takes care |
737 | * of locking, hence drivers should not have a need to call drm_modeset_lock() | |
738 | * directly. Sequence of the actual hardware state commit is not handled, | |
739 | * drivers might need to keep track of struct drm_crtc_commit within subclassed | |
740 | * structure of &drm_private_state as necessary, e.g. similar to | |
741 | * &drm_plane_state.commit. See also &drm_atomic_state.fake_commit. | |
da6c0596 DV |
742 | * |
743 | * All private state structures contained in a &drm_atomic_state update can be | |
744 | * iterated using for_each_oldnew_private_obj_in_state(), | |
745 | * for_each_new_private_obj_in_state() and for_each_old_private_obj_in_state(). | |
746 | * Drivers are recommended to wrap these for each type of driver private state | |
747 | * object they have, filtering on &drm_private_obj.funcs using for_each_if(), at | |
748 | * least if they want to iterate over all objects of a given type. | |
749 | * | |
750 | * An earlier way to handle driver private state was by subclassing struct | |
751 | * &drm_atomic_state. But since that encourages non-standard ways to implement | |
752 | * the check/commit split atomic requires (by using e.g. "check and rollback or | |
753 | * commit instead" of "duplicate state, check, then either commit or release | |
754 | * duplicated state) it is deprecated in favour of using &drm_private_state. | |
755 | */ | |
756 | ||
a4370c77 VS |
757 | /** |
758 | * drm_atomic_private_obj_init - initialize private object | |
b962a120 | 759 | * @dev: DRM device this object will be attached to |
a4370c77 VS |
760 | * @obj: private object |
761 | * @state: initial private object state | |
762 | * @funcs: pointer to the struct of function pointers that identify the object | |
763 | * type | |
764 | * | |
765 | * Initialize the private object, which can be embedded into any | |
766 | * driver private object that needs its own atomic state. | |
767 | */ | |
768 | void | |
b962a120 RC |
769 | drm_atomic_private_obj_init(struct drm_device *dev, |
770 | struct drm_private_obj *obj, | |
a4370c77 VS |
771 | struct drm_private_state *state, |
772 | const struct drm_private_state_funcs *funcs) | |
773 | { | |
774 | memset(obj, 0, sizeof(*obj)); | |
775 | ||
b962a120 RC |
776 | drm_modeset_lock_init(&obj->lock); |
777 | ||
a4370c77 VS |
778 | obj->state = state; |
779 | obj->funcs = funcs; | |
b962a120 | 780 | list_add_tail(&obj->head, &dev->mode_config.privobj_list); |
a4370c77 VS |
781 | } |
782 | EXPORT_SYMBOL(drm_atomic_private_obj_init); | |
783 | ||
784 | /** | |
785 | * drm_atomic_private_obj_fini - finalize private object | |
786 | * @obj: private object | |
787 | * | |
788 | * Finalize the private object. | |
789 | */ | |
790 | void | |
791 | drm_atomic_private_obj_fini(struct drm_private_obj *obj) | |
792 | { | |
b962a120 | 793 | list_del(&obj->head); |
a4370c77 | 794 | obj->funcs->atomic_destroy_state(obj, obj->state); |
b962a120 | 795 | drm_modeset_lock_fini(&obj->lock); |
a4370c77 VS |
796 | } |
797 | EXPORT_SYMBOL(drm_atomic_private_obj_fini); | |
798 | ||
b430c27a PD |
799 | /** |
800 | * drm_atomic_get_private_obj_state - get private object state | |
801 | * @state: global atomic state | |
802 | * @obj: private object to get the state for | |
b430c27a PD |
803 | * |
804 | * This function returns the private object state for the given private object, | |
b962a120 RC |
805 | * allocating the state if needed. It will also grab the relevant private |
806 | * object lock to make sure that the state is consistent. | |
b430c27a PD |
807 | * |
808 | * RETURNS: | |
809 | * | |
810 | * Either the allocated state or the error code encoded into a pointer. | |
811 | */ | |
a4370c77 VS |
812 | struct drm_private_state * |
813 | drm_atomic_get_private_obj_state(struct drm_atomic_state *state, | |
814 | struct drm_private_obj *obj) | |
b430c27a | 815 | { |
b962a120 | 816 | int index, num_objs, i, ret; |
b430c27a PD |
817 | size_t size; |
818 | struct __drm_private_objs_state *arr; | |
a4370c77 | 819 | struct drm_private_state *obj_state; |
b430c27a PD |
820 | |
821 | for (i = 0; i < state->num_private_objs; i++) | |
a4370c77 VS |
822 | if (obj == state->private_objs[i].ptr) |
823 | return state->private_objs[i].state; | |
b430c27a | 824 | |
b962a120 RC |
825 | ret = drm_modeset_lock(&obj->lock, state->acquire_ctx); |
826 | if (ret) | |
827 | return ERR_PTR(ret); | |
828 | ||
b430c27a PD |
829 | num_objs = state->num_private_objs + 1; |
830 | size = sizeof(*state->private_objs) * num_objs; | |
831 | arr = krealloc(state->private_objs, size, GFP_KERNEL); | |
832 | if (!arr) | |
833 | return ERR_PTR(-ENOMEM); | |
834 | ||
835 | state->private_objs = arr; | |
836 | index = state->num_private_objs; | |
837 | memset(&state->private_objs[index], 0, sizeof(*state->private_objs)); | |
838 | ||
a4370c77 VS |
839 | obj_state = obj->funcs->atomic_duplicate_state(obj); |
840 | if (!obj_state) | |
b430c27a PD |
841 | return ERR_PTR(-ENOMEM); |
842 | ||
a4370c77 VS |
843 | state->private_objs[index].state = obj_state; |
844 | state->private_objs[index].old_state = obj->state; | |
845 | state->private_objs[index].new_state = obj_state; | |
846 | state->private_objs[index].ptr = obj; | |
e89ea355 | 847 | obj_state->state = state; |
a4370c77 | 848 | |
b430c27a PD |
849 | state->num_private_objs = num_objs; |
850 | ||
a4370c77 VS |
851 | DRM_DEBUG_ATOMIC("Added new private object %p state %p to %p\n", |
852 | obj, obj_state, state); | |
b430c27a | 853 | |
a4370c77 | 854 | return obj_state; |
b430c27a PD |
855 | } |
856 | EXPORT_SYMBOL(drm_atomic_get_private_obj_state); | |
857 | ||
9801a7ea ATC |
858 | /** |
859 | * drm_atomic_get_old_private_obj_state | |
860 | * @state: global atomic state object | |
861 | * @obj: private_obj to grab | |
862 | * | |
863 | * This function returns the old private object state for the given private_obj, | |
864 | * or NULL if the private_obj is not part of the global atomic state. | |
865 | */ | |
866 | struct drm_private_state * | |
867 | drm_atomic_get_old_private_obj_state(struct drm_atomic_state *state, | |
868 | struct drm_private_obj *obj) | |
869 | { | |
870 | int i; | |
871 | ||
872 | for (i = 0; i < state->num_private_objs; i++) | |
873 | if (obj == state->private_objs[i].ptr) | |
874 | return state->private_objs[i].old_state; | |
875 | ||
876 | return NULL; | |
877 | } | |
878 | EXPORT_SYMBOL(drm_atomic_get_old_private_obj_state); | |
879 | ||
880 | /** | |
881 | * drm_atomic_get_new_private_obj_state | |
882 | * @state: global atomic state object | |
883 | * @obj: private_obj to grab | |
884 | * | |
885 | * This function returns the new private object state for the given private_obj, | |
886 | * or NULL if the private_obj is not part of the global atomic state. | |
887 | */ | |
888 | struct drm_private_state * | |
889 | drm_atomic_get_new_private_obj_state(struct drm_atomic_state *state, | |
890 | struct drm_private_obj *obj) | |
891 | { | |
892 | int i; | |
893 | ||
894 | for (i = 0; i < state->num_private_objs; i++) | |
895 | if (obj == state->private_objs[i].ptr) | |
896 | return state->private_objs[i].new_state; | |
897 | ||
898 | return NULL; | |
899 | } | |
900 | EXPORT_SYMBOL(drm_atomic_get_new_private_obj_state); | |
901 | ||
1b27fbdd LP |
902 | /** |
903 | * drm_atomic_get_old_connector_for_encoder - Get old connector for an encoder | |
904 | * @state: Atomic state | |
905 | * @encoder: The encoder to fetch the connector state for | |
906 | * | |
907 | * This function finds and returns the connector that was connected to @encoder | |
908 | * as specified by the @state. | |
909 | * | |
910 | * If there is no connector in @state which previously had @encoder connected to | |
911 | * it, this function will return NULL. While this may seem like an invalid use | |
912 | * case, it is sometimes useful to differentiate commits which had no prior | |
913 | * connectors attached to @encoder vs ones that did (and to inspect their | |
914 | * state). This is especially true in enable hooks because the pipeline has | |
915 | * changed. | |
916 | * | |
917 | * Returns: The old connector connected to @encoder, or NULL if the encoder is | |
918 | * not connected. | |
919 | */ | |
920 | struct drm_connector * | |
921 | drm_atomic_get_old_connector_for_encoder(struct drm_atomic_state *state, | |
922 | struct drm_encoder *encoder) | |
923 | { | |
924 | struct drm_connector_state *conn_state; | |
925 | struct drm_connector *connector; | |
926 | unsigned int i; | |
927 | ||
928 | for_each_old_connector_in_state(state, connector, conn_state, i) { | |
929 | if (conn_state->best_encoder == encoder) | |
930 | return connector; | |
931 | } | |
932 | ||
933 | return NULL; | |
934 | } | |
935 | EXPORT_SYMBOL(drm_atomic_get_old_connector_for_encoder); | |
936 | ||
937 | /** | |
938 | * drm_atomic_get_new_connector_for_encoder - Get new connector for an encoder | |
939 | * @state: Atomic state | |
940 | * @encoder: The encoder to fetch the connector state for | |
941 | * | |
942 | * This function finds and returns the connector that will be connected to | |
943 | * @encoder as specified by the @state. | |
944 | * | |
945 | * If there is no connector in @state which will have @encoder connected to it, | |
946 | * this function will return NULL. While this may seem like an invalid use case, | |
947 | * it is sometimes useful to differentiate commits which have no connectors | |
948 | * attached to @encoder vs ones that do (and to inspect their state). This is | |
949 | * especially true in disable hooks because the pipeline will change. | |
950 | * | |
951 | * Returns: The new connector connected to @encoder, or NULL if the encoder is | |
952 | * not connected. | |
953 | */ | |
954 | struct drm_connector * | |
955 | drm_atomic_get_new_connector_for_encoder(struct drm_atomic_state *state, | |
956 | struct drm_encoder *encoder) | |
957 | { | |
958 | struct drm_connector_state *conn_state; | |
959 | struct drm_connector *connector; | |
960 | unsigned int i; | |
961 | ||
962 | for_each_new_connector_in_state(state, connector, conn_state, i) { | |
963 | if (conn_state->best_encoder == encoder) | |
964 | return connector; | |
965 | } | |
966 | ||
967 | return NULL; | |
968 | } | |
969 | EXPORT_SYMBOL(drm_atomic_get_new_connector_for_encoder); | |
970 | ||
cc4ceb48 DV |
971 | /** |
972 | * drm_atomic_get_connector_state - get connector state | |
973 | * @state: global atomic state object | |
974 | * @connector: connector to get state object for | |
975 | * | |
976 | * This function returns the connector state for the given connector, | |
977 | * allocating it if needed. It will also grab the relevant connector lock to | |
978 | * make sure that the state is consistent. | |
979 | * | |
980 | * Returns: | |
981 | * | |
982 | * Either the allocated state or the error code encoded into the pointer. When | |
983 | * the error is EDEADLK then the w/w mutex code has detected a deadlock and the | |
984 | * entire atomic sequence must be restarted. All other errors are fatal. | |
985 | */ | |
986 | struct drm_connector_state * | |
987 | drm_atomic_get_connector_state(struct drm_atomic_state *state, | |
988 | struct drm_connector *connector) | |
989 | { | |
990 | int ret, index; | |
991 | struct drm_mode_config *config = &connector->dev->mode_config; | |
992 | struct drm_connector_state *connector_state; | |
993 | ||
7f4eaa89 ML |
994 | WARN_ON(!state->acquire_ctx); |
995 | ||
c7eb76f4 DV |
996 | ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx); |
997 | if (ret) | |
998 | return ERR_PTR(ret); | |
999 | ||
cc4ceb48 DV |
1000 | index = drm_connector_index(connector); |
1001 | ||
f52b69f1 | 1002 | if (index >= state->num_connector) { |
63e83c1d | 1003 | struct __drm_connnectors_state *c; |
5fff80bb ML |
1004 | int alloc = max(index + 1, config->num_connector); |
1005 | ||
32ce2553 BG |
1006 | c = krealloc_array(state->connectors, alloc, |
1007 | sizeof(*state->connectors), GFP_KERNEL); | |
5fff80bb ML |
1008 | if (!c) |
1009 | return ERR_PTR(-ENOMEM); | |
1010 | ||
1011 | state->connectors = c; | |
1012 | memset(&state->connectors[state->num_connector], 0, | |
1013 | sizeof(*state->connectors) * (alloc - state->num_connector)); | |
1014 | ||
5fff80bb | 1015 | state->num_connector = alloc; |
f52b69f1 DV |
1016 | } |
1017 | ||
63e83c1d DV |
1018 | if (state->connectors[index].state) |
1019 | return state->connectors[index].state; | |
cc4ceb48 | 1020 | |
cc4ceb48 DV |
1021 | connector_state = connector->funcs->atomic_duplicate_state(connector); |
1022 | if (!connector_state) | |
1023 | return ERR_PTR(-ENOMEM); | |
1024 | ||
ad093607 | 1025 | drm_connector_get(connector); |
63e83c1d | 1026 | state->connectors[index].state = connector_state; |
581e49fe ML |
1027 | state->connectors[index].old_state = connector->state; |
1028 | state->connectors[index].new_state = connector_state; | |
63e83c1d | 1029 | state->connectors[index].ptr = connector; |
cc4ceb48 DV |
1030 | connector_state->state = state; |
1031 | ||
6ac7c548 RK |
1032 | DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d:%s] %p state to %p\n", |
1033 | connector->base.id, connector->name, | |
1034 | connector_state, state); | |
cc4ceb48 DV |
1035 | |
1036 | if (connector_state->crtc) { | |
1037 | struct drm_crtc_state *crtc_state; | |
1038 | ||
1039 | crtc_state = drm_atomic_get_crtc_state(state, | |
1040 | connector_state->crtc); | |
1041 | if (IS_ERR(crtc_state)) | |
1042 | return ERR_CAST(crtc_state); | |
1043 | } | |
1044 | ||
1045 | return connector_state; | |
1046 | } | |
1047 | EXPORT_SYMBOL(drm_atomic_get_connector_state); | |
1048 | ||
fceffb32 RC |
1049 | static void drm_atomic_connector_print_state(struct drm_printer *p, |
1050 | const struct drm_connector_state *state) | |
1051 | { | |
1052 | struct drm_connector *connector = state->connector; | |
1053 | ||
1054 | drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name); | |
1055 | drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)"); | |
1452c25b | 1056 | drm_printf(p, "\tself_refresh_aware=%d\n", state->self_refresh_aware); |
fceffb32 | 1057 | |
8cbc5caf BS |
1058 | if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK) |
1059 | if (state->writeback_job && state->writeback_job->fb) | |
1060 | drm_printf(p, "\tfb=%d\n", state->writeback_job->fb->base.id); | |
1061 | ||
fceffb32 RC |
1062 | if (connector->funcs->atomic_print_state) |
1063 | connector->funcs->atomic_print_state(p, state); | |
1064 | } | |
1065 | ||
75146591 BB |
1066 | /** |
1067 | * drm_atomic_get_bridge_state - get bridge state | |
1068 | * @state: global atomic state object | |
1069 | * @bridge: bridge to get state object for | |
1070 | * | |
1071 | * This function returns the bridge state for the given bridge, allocating it | |
1072 | * if needed. It will also grab the relevant bridge lock to make sure that the | |
1073 | * state is consistent. | |
1074 | * | |
1075 | * Returns: | |
1076 | * | |
1077 | * Either the allocated state or the error code encoded into the pointer. When | |
1078 | * the error is EDEADLK then the w/w mutex code has detected a deadlock and the | |
1079 | * entire atomic sequence must be restarted. | |
1080 | */ | |
1081 | struct drm_bridge_state * | |
1082 | drm_atomic_get_bridge_state(struct drm_atomic_state *state, | |
1083 | struct drm_bridge *bridge) | |
1084 | { | |
1085 | struct drm_private_state *obj_state; | |
1086 | ||
1087 | obj_state = drm_atomic_get_private_obj_state(state, &bridge->base); | |
1088 | if (IS_ERR(obj_state)) | |
1089 | return ERR_CAST(obj_state); | |
1090 | ||
1091 | return drm_priv_to_bridge_state(obj_state); | |
1092 | } | |
1093 | EXPORT_SYMBOL(drm_atomic_get_bridge_state); | |
1094 | ||
1095 | /** | |
1096 | * drm_atomic_get_old_bridge_state - get old bridge state, if it exists | |
1097 | * @state: global atomic state object | |
1098 | * @bridge: bridge to grab | |
1099 | * | |
1100 | * This function returns the old bridge state for the given bridge, or NULL if | |
1101 | * the bridge is not part of the global atomic state. | |
1102 | */ | |
1103 | struct drm_bridge_state * | |
1104 | drm_atomic_get_old_bridge_state(struct drm_atomic_state *state, | |
1105 | struct drm_bridge *bridge) | |
1106 | { | |
1107 | struct drm_private_state *obj_state; | |
1108 | ||
1109 | obj_state = drm_atomic_get_old_private_obj_state(state, &bridge->base); | |
1110 | if (!obj_state) | |
1111 | return NULL; | |
1112 | ||
1113 | return drm_priv_to_bridge_state(obj_state); | |
1114 | } | |
1115 | EXPORT_SYMBOL(drm_atomic_get_old_bridge_state); | |
1116 | ||
1117 | /** | |
1118 | * drm_atomic_get_new_bridge_state - get new bridge state, if it exists | |
1119 | * @state: global atomic state object | |
1120 | * @bridge: bridge to grab | |
1121 | * | |
1122 | * This function returns the new bridge state for the given bridge, or NULL if | |
1123 | * the bridge is not part of the global atomic state. | |
1124 | */ | |
1125 | struct drm_bridge_state * | |
1126 | drm_atomic_get_new_bridge_state(struct drm_atomic_state *state, | |
1127 | struct drm_bridge *bridge) | |
1128 | { | |
1129 | struct drm_private_state *obj_state; | |
1130 | ||
1131 | obj_state = drm_atomic_get_new_private_obj_state(state, &bridge->base); | |
1132 | if (!obj_state) | |
1133 | return NULL; | |
1134 | ||
1135 | return drm_priv_to_bridge_state(obj_state); | |
1136 | } | |
1137 | EXPORT_SYMBOL(drm_atomic_get_new_bridge_state); | |
1138 | ||
1139 | /** | |
1140 | * drm_atomic_add_encoder_bridges - add bridges attached to an encoder | |
1141 | * @state: atomic state | |
1142 | * @encoder: DRM encoder | |
1143 | * | |
1144 | * This function adds all bridges attached to @encoder. This is needed to add | |
1145 | * bridge states to @state and make them available when | |
91ea8330 BB |
1146 | * &drm_bridge_funcs.atomic_check(), &drm_bridge_funcs.atomic_pre_enable(), |
1147 | * &drm_bridge_funcs.atomic_enable(), | |
1148 | * &drm_bridge_funcs.atomic_disable_post_disable() are called. | |
75146591 BB |
1149 | * |
1150 | * Returns: | |
1151 | * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK | |
1152 | * then the w/w mutex code has detected a deadlock and the entire atomic | |
1153 | * sequence must be restarted. All other errors are fatal. | |
1154 | */ | |
1155 | int | |
1156 | drm_atomic_add_encoder_bridges(struct drm_atomic_state *state, | |
1157 | struct drm_encoder *encoder) | |
1158 | { | |
1159 | struct drm_bridge_state *bridge_state; | |
1160 | struct drm_bridge *bridge; | |
1161 | ||
1162 | if (!encoder) | |
1163 | return 0; | |
1164 | ||
1165 | DRM_DEBUG_ATOMIC("Adding all bridges for [encoder:%d:%s] to %p\n", | |
1166 | encoder->base.id, encoder->name, state); | |
1167 | ||
1168 | drm_for_each_bridge_in_chain(encoder, bridge) { | |
1169 | /* Skip bridges that don't implement the atomic state hooks. */ | |
1170 | if (!bridge->funcs->atomic_duplicate_state) | |
1171 | continue; | |
1172 | ||
1173 | bridge_state = drm_atomic_get_bridge_state(state, bridge); | |
1174 | if (IS_ERR(bridge_state)) | |
1175 | return PTR_ERR(bridge_state); | |
1176 | } | |
1177 | ||
1178 | return 0; | |
1179 | } | |
1180 | EXPORT_SYMBOL(drm_atomic_add_encoder_bridges); | |
1181 | ||
cc4ceb48 | 1182 | /** |
42240c90 | 1183 | * drm_atomic_add_affected_connectors - add connectors for CRTC |
cc4ceb48 | 1184 | * @state: atomic state |
42240c90 | 1185 | * @crtc: DRM CRTC |
cc4ceb48 DV |
1186 | * |
1187 | * This function walks the current configuration and adds all connectors | |
1188 | * currently using @crtc to the atomic configuration @state. Note that this | |
1189 | * function must acquire the connection mutex. This can potentially cause | |
42240c90 | 1190 | * unneeded seralization if the update is just for the planes on one CRTC. Hence |
cc4ceb48 DV |
1191 | * drivers and helpers should only call this when really needed (e.g. when a |
1192 | * full modeset needs to happen due to some change). | |
1193 | * | |
1194 | * Returns: | |
1195 | * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK | |
1196 | * then the w/w mutex code has detected a deadlock and the entire atomic | |
1197 | * sequence must be restarted. All other errors are fatal. | |
1198 | */ | |
1199 | int | |
1200 | drm_atomic_add_affected_connectors(struct drm_atomic_state *state, | |
1201 | struct drm_crtc *crtc) | |
1202 | { | |
1203 | struct drm_mode_config *config = &state->dev->mode_config; | |
1204 | struct drm_connector *connector; | |
1205 | struct drm_connector_state *conn_state; | |
613051da | 1206 | struct drm_connector_list_iter conn_iter; |
5351bbdd | 1207 | struct drm_crtc_state *crtc_state; |
cc4ceb48 DV |
1208 | int ret; |
1209 | ||
5351bbdd ML |
1210 | crtc_state = drm_atomic_get_crtc_state(state, crtc); |
1211 | if (IS_ERR(crtc_state)) | |
1212 | return PTR_ERR(crtc_state); | |
1213 | ||
cc4ceb48 DV |
1214 | ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx); |
1215 | if (ret) | |
1216 | return ret; | |
1217 | ||
fa3ab4c2 VS |
1218 | DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n", |
1219 | crtc->base.id, crtc->name, state); | |
cc4ceb48 DV |
1220 | |
1221 | /* | |
5351bbdd ML |
1222 | * Changed connectors are already in @state, so only need to look |
1223 | * at the connector_mask in crtc_state. | |
cc4ceb48 | 1224 | */ |
b982dab1 | 1225 | drm_connector_list_iter_begin(state->dev, &conn_iter); |
613051da | 1226 | drm_for_each_connector_iter(connector, &conn_iter) { |
73705732 | 1227 | if (!(crtc_state->connector_mask & drm_connector_mask(connector))) |
cc4ceb48 DV |
1228 | continue; |
1229 | ||
1230 | conn_state = drm_atomic_get_connector_state(state, connector); | |
613051da | 1231 | if (IS_ERR(conn_state)) { |
b982dab1 | 1232 | drm_connector_list_iter_end(&conn_iter); |
cc4ceb48 | 1233 | return PTR_ERR(conn_state); |
613051da | 1234 | } |
cc4ceb48 | 1235 | } |
b982dab1 | 1236 | drm_connector_list_iter_end(&conn_iter); |
cc4ceb48 DV |
1237 | |
1238 | return 0; | |
1239 | } | |
1240 | EXPORT_SYMBOL(drm_atomic_add_affected_connectors); | |
1241 | ||
e01e9f75 | 1242 | /** |
42240c90 | 1243 | * drm_atomic_add_affected_planes - add planes for CRTC |
e01e9f75 | 1244 | * @state: atomic state |
42240c90 | 1245 | * @crtc: DRM CRTC |
e01e9f75 ML |
1246 | * |
1247 | * This function walks the current configuration and adds all planes | |
1248 | * currently used by @crtc to the atomic configuration @state. This is useful | |
1249 | * when an atomic commit also needs to check all currently enabled plane on | |
1250 | * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC | |
1251 | * to avoid special code to force-enable all planes. | |
1252 | * | |
1253 | * Since acquiring a plane state will always also acquire the w/w mutex of the | |
1254 | * current CRTC for that plane (if there is any) adding all the plane states for | |
1255 | * a CRTC will not reduce parallism of atomic updates. | |
1256 | * | |
1257 | * Returns: | |
1258 | * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK | |
1259 | * then the w/w mutex code has detected a deadlock and the entire atomic | |
1260 | * sequence must be restarted. All other errors are fatal. | |
1261 | */ | |
1262 | int | |
1263 | drm_atomic_add_affected_planes(struct drm_atomic_state *state, | |
1264 | struct drm_crtc *crtc) | |
1265 | { | |
534903d6 VS |
1266 | const struct drm_crtc_state *old_crtc_state = |
1267 | drm_atomic_get_old_crtc_state(state, crtc); | |
e01e9f75 ML |
1268 | struct drm_plane *plane; |
1269 | ||
b4d93679 | 1270 | WARN_ON(!drm_atomic_get_new_crtc_state(state, crtc)); |
e01e9f75 | 1271 | |
b6f690ab VS |
1272 | DRM_DEBUG_ATOMIC("Adding all current planes for [CRTC:%d:%s] to %p\n", |
1273 | crtc->base.id, crtc->name, state); | |
1274 | ||
534903d6 | 1275 | drm_for_each_plane_mask(plane, state->dev, old_crtc_state->plane_mask) { |
e01e9f75 ML |
1276 | struct drm_plane_state *plane_state = |
1277 | drm_atomic_get_plane_state(state, plane); | |
1278 | ||
1279 | if (IS_ERR(plane_state)) | |
1280 | return PTR_ERR(plane_state); | |
1281 | } | |
1282 | return 0; | |
1283 | } | |
1284 | EXPORT_SYMBOL(drm_atomic_add_affected_planes); | |
1285 | ||
cc4ceb48 DV |
1286 | /** |
1287 | * drm_atomic_check_only - check whether a given config would work | |
1288 | * @state: atomic configuration to check | |
1289 | * | |
1290 | * Note that this function can return -EDEADLK if the driver needed to acquire | |
1291 | * more locks but encountered a deadlock. The caller must then do the usual w/w | |
1292 | * backoff dance and restart. All other errors are fatal. | |
1293 | * | |
1294 | * Returns: | |
1295 | * 0 on success, negative error code on failure. | |
1296 | */ | |
1297 | int drm_atomic_check_only(struct drm_atomic_state *state) | |
1298 | { | |
5e743737 RC |
1299 | struct drm_device *dev = state->dev; |
1300 | struct drm_mode_config *config = &dev->mode_config; | |
df63b999 | 1301 | struct drm_plane *plane; |
d9be05b7 VS |
1302 | struct drm_plane_state *old_plane_state; |
1303 | struct drm_plane_state *new_plane_state; | |
df63b999 | 1304 | struct drm_crtc *crtc; |
b2432adf VS |
1305 | struct drm_crtc_state *old_crtc_state; |
1306 | struct drm_crtc_state *new_crtc_state; | |
935774cd BS |
1307 | struct drm_connector *conn; |
1308 | struct drm_connector_state *conn_state; | |
fb6473a4 DV |
1309 | unsigned requested_crtc = 0; |
1310 | unsigned affected_crtc = 0; | |
5e743737 | 1311 | int i, ret = 0; |
cc4ceb48 | 1312 | |
17a38d9c | 1313 | DRM_DEBUG_ATOMIC("checking %p\n", state); |
cc4ceb48 | 1314 | |
fb6473a4 DV |
1315 | for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) |
1316 | requested_crtc |= drm_crtc_mask(crtc); | |
1317 | ||
d9be05b7 VS |
1318 | for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) { |
1319 | ret = drm_atomic_plane_check(old_plane_state, new_plane_state); | |
5e743737 | 1320 | if (ret) { |
9f4c97a2 VS |
1321 | DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n", |
1322 | plane->base.id, plane->name); | |
5e743737 RC |
1323 | return ret; |
1324 | } | |
1325 | } | |
1326 | ||
b2432adf VS |
1327 | for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { |
1328 | ret = drm_atomic_crtc_check(old_crtc_state, new_crtc_state); | |
5e743737 | 1329 | if (ret) { |
fa3ab4c2 VS |
1330 | DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n", |
1331 | crtc->base.id, crtc->name); | |
5e743737 RC |
1332 | return ret; |
1333 | } | |
1334 | } | |
1335 | ||
935774cd BS |
1336 | for_each_new_connector_in_state(state, conn, conn_state, i) { |
1337 | ret = drm_atomic_connector_check(conn, conn_state); | |
1338 | if (ret) { | |
1339 | DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] atomic core check failed\n", | |
1340 | conn->base.id, conn->name); | |
1341 | return ret; | |
1342 | } | |
1343 | } | |
1344 | ||
14d4e522 | 1345 | if (config->funcs->atomic_check) { |
5e743737 RC |
1346 | ret = config->funcs->atomic_check(state->dev, state); |
1347 | ||
14d4e522 LP |
1348 | if (ret) { |
1349 | DRM_DEBUG_ATOMIC("atomic driver check for %p failed: %d\n", | |
1350 | state, ret); | |
1351 | return ret; | |
1352 | } | |
1353 | } | |
a0ffc51e | 1354 | |
d34f20d6 | 1355 | if (!state->allow_modeset) { |
b2432adf VS |
1356 | for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { |
1357 | if (drm_atomic_crtc_needs_modeset(new_crtc_state)) { | |
fa3ab4c2 VS |
1358 | DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n", |
1359 | crtc->base.id, crtc->name); | |
d34f20d6 RC |
1360 | return -EINVAL; |
1361 | } | |
1362 | } | |
1363 | } | |
1364 | ||
fb6473a4 DV |
1365 | for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) |
1366 | affected_crtc |= drm_crtc_mask(crtc); | |
1367 | ||
1368 | /* | |
1369 | * For commits that allow modesets drivers can add other CRTCs to the | |
1370 | * atomic commit, e.g. when they need to reallocate global resources. | |
1371 | * This can cause spurious EBUSY, which robs compositors of a very | |
1372 | * effective sanity check for their drawing loop. Therefor only allow | |
1373 | * drivers to add unrelated CRTC states for modeset commits. | |
1374 | * | |
1375 | * FIXME: Should add affected_crtc mask to the ATOMIC IOCTL as an output | |
1376 | * so compositors know what's going on. | |
1377 | */ | |
1378 | if (affected_crtc != requested_crtc) { | |
1379 | DRM_DEBUG_ATOMIC("driver added CRTC to commit: requested 0x%x, affected 0x%0x\n", | |
1380 | requested_crtc, affected_crtc); | |
1381 | WARN(!state->allow_modeset, "adding CRTC not allowed without modesets: requested 0x%x, affected 0x%0x\n", | |
1382 | requested_crtc, affected_crtc); | |
1383 | } | |
1384 | ||
a0ffc51e | 1385 | return 0; |
cc4ceb48 DV |
1386 | } |
1387 | EXPORT_SYMBOL(drm_atomic_check_only); | |
1388 | ||
1389 | /** | |
1390 | * drm_atomic_commit - commit configuration atomically | |
1391 | * @state: atomic configuration to check | |
1392 | * | |
1393 | * Note that this function can return -EDEADLK if the driver needed to acquire | |
1394 | * more locks but encountered a deadlock. The caller must then do the usual w/w | |
1395 | * backoff dance and restart. All other errors are fatal. | |
1396 | * | |
76fede2f ML |
1397 | * This function will take its own reference on @state. |
1398 | * Callers should always release their reference with drm_atomic_state_put(). | |
cc4ceb48 DV |
1399 | * |
1400 | * Returns: | |
1401 | * 0 on success, negative error code on failure. | |
1402 | */ | |
1403 | int drm_atomic_commit(struct drm_atomic_state *state) | |
1404 | { | |
1405 | struct drm_mode_config *config = &state->dev->mode_config; | |
1406 | int ret; | |
1407 | ||
1408 | ret = drm_atomic_check_only(state); | |
1409 | if (ret) | |
1410 | return ret; | |
1411 | ||
a0752d4a | 1412 | DRM_DEBUG_ATOMIC("committing %p\n", state); |
cc4ceb48 DV |
1413 | |
1414 | return config->funcs->atomic_commit(state->dev, state, false); | |
1415 | } | |
1416 | EXPORT_SYMBOL(drm_atomic_commit); | |
1417 | ||
1418 | /** | |
d574528a | 1419 | * drm_atomic_nonblocking_commit - atomic nonblocking commit |
cc4ceb48 DV |
1420 | * @state: atomic configuration to check |
1421 | * | |
1422 | * Note that this function can return -EDEADLK if the driver needed to acquire | |
1423 | * more locks but encountered a deadlock. The caller must then do the usual w/w | |
1424 | * backoff dance and restart. All other errors are fatal. | |
1425 | * | |
76fede2f ML |
1426 | * This function will take its own reference on @state. |
1427 | * Callers should always release their reference with drm_atomic_state_put(). | |
cc4ceb48 DV |
1428 | * |
1429 | * Returns: | |
1430 | * 0 on success, negative error code on failure. | |
1431 | */ | |
b837ba0a | 1432 | int drm_atomic_nonblocking_commit(struct drm_atomic_state *state) |
cc4ceb48 DV |
1433 | { |
1434 | struct drm_mode_config *config = &state->dev->mode_config; | |
1435 | int ret; | |
1436 | ||
1437 | ret = drm_atomic_check_only(state); | |
1438 | if (ret) | |
1439 | return ret; | |
1440 | ||
a0752d4a | 1441 | DRM_DEBUG_ATOMIC("committing %p nonblocking\n", state); |
cc4ceb48 DV |
1442 | |
1443 | return config->funcs->atomic_commit(state->dev, state, true); | |
1444 | } | |
b837ba0a | 1445 | EXPORT_SYMBOL(drm_atomic_nonblocking_commit); |
d34f20d6 | 1446 | |
df737895 NT |
1447 | /* just used from drm-client and atomic-helper: */ |
1448 | int __drm_atomic_helper_disable_plane(struct drm_plane *plane, | |
1449 | struct drm_plane_state *plane_state) | |
1450 | { | |
1451 | int ret; | |
1452 | ||
1453 | ret = drm_atomic_set_crtc_for_plane(plane_state, NULL); | |
1454 | if (ret != 0) | |
1455 | return ret; | |
1456 | ||
1457 | drm_atomic_set_fb_for_plane(plane_state, NULL); | |
1458 | plane_state->crtc_x = 0; | |
1459 | plane_state->crtc_y = 0; | |
1460 | plane_state->crtc_w = 0; | |
1461 | plane_state->crtc_h = 0; | |
1462 | plane_state->src_x = 0; | |
1463 | plane_state->src_y = 0; | |
1464 | plane_state->src_w = 0; | |
1465 | plane_state->src_h = 0; | |
1466 | ||
1467 | return 0; | |
1468 | } | |
1469 | EXPORT_SYMBOL(__drm_atomic_helper_disable_plane); | |
1470 | ||
1471 | static int update_output_state(struct drm_atomic_state *state, | |
1472 | struct drm_mode_set *set) | |
1473 | { | |
1474 | struct drm_device *dev = set->crtc->dev; | |
1475 | struct drm_crtc *crtc; | |
1476 | struct drm_crtc_state *new_crtc_state; | |
1477 | struct drm_connector *connector; | |
1478 | struct drm_connector_state *new_conn_state; | |
1479 | int ret, i; | |
1480 | ||
1481 | ret = drm_modeset_lock(&dev->mode_config.connection_mutex, | |
1482 | state->acquire_ctx); | |
1483 | if (ret) | |
1484 | return ret; | |
1485 | ||
1486 | /* First disable all connectors on the target crtc. */ | |
1487 | ret = drm_atomic_add_affected_connectors(state, set->crtc); | |
1488 | if (ret) | |
1489 | return ret; | |
1490 | ||
1491 | for_each_new_connector_in_state(state, connector, new_conn_state, i) { | |
1492 | if (new_conn_state->crtc == set->crtc) { | |
1493 | ret = drm_atomic_set_crtc_for_connector(new_conn_state, | |
1494 | NULL); | |
1495 | if (ret) | |
1496 | return ret; | |
1497 | ||
1498 | /* Make sure legacy setCrtc always re-trains */ | |
1499 | new_conn_state->link_status = DRM_LINK_STATUS_GOOD; | |
1500 | } | |
1501 | } | |
1502 | ||
1503 | /* Then set all connectors from set->connectors on the target crtc */ | |
1504 | for (i = 0; i < set->num_connectors; i++) { | |
1505 | new_conn_state = drm_atomic_get_connector_state(state, | |
1506 | set->connectors[i]); | |
1507 | if (IS_ERR(new_conn_state)) | |
1508 | return PTR_ERR(new_conn_state); | |
1509 | ||
1510 | ret = drm_atomic_set_crtc_for_connector(new_conn_state, | |
1511 | set->crtc); | |
1512 | if (ret) | |
1513 | return ret; | |
1514 | } | |
1515 | ||
1516 | for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { | |
1517 | /* | |
1518 | * Don't update ->enable for the CRTC in the set_config request, | |
1519 | * since a mismatch would indicate a bug in the upper layers. | |
1520 | * The actual modeset code later on will catch any | |
1521 | * inconsistencies here. | |
1522 | */ | |
1523 | if (crtc == set->crtc) | |
1524 | continue; | |
1525 | ||
1526 | if (!new_crtc_state->connector_mask) { | |
1527 | ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state, | |
1528 | NULL); | |
1529 | if (ret < 0) | |
1530 | return ret; | |
1531 | ||
1532 | new_crtc_state->active = false; | |
1533 | } | |
1534 | } | |
1535 | ||
1536 | return 0; | |
1537 | } | |
1538 | ||
1539 | /* just used from drm-client and atomic-helper: */ | |
1540 | int __drm_atomic_helper_set_config(struct drm_mode_set *set, | |
1541 | struct drm_atomic_state *state) | |
1542 | { | |
1543 | struct drm_crtc_state *crtc_state; | |
1544 | struct drm_plane_state *primary_state; | |
1545 | struct drm_crtc *crtc = set->crtc; | |
1546 | int hdisplay, vdisplay; | |
1547 | int ret; | |
1548 | ||
1549 | crtc_state = drm_atomic_get_crtc_state(state, crtc); | |
1550 | if (IS_ERR(crtc_state)) | |
1551 | return PTR_ERR(crtc_state); | |
1552 | ||
1553 | primary_state = drm_atomic_get_plane_state(state, crtc->primary); | |
1554 | if (IS_ERR(primary_state)) | |
1555 | return PTR_ERR(primary_state); | |
1556 | ||
1557 | if (!set->mode) { | |
1558 | WARN_ON(set->fb); | |
1559 | WARN_ON(set->num_connectors); | |
1560 | ||
1561 | ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL); | |
1562 | if (ret != 0) | |
1563 | return ret; | |
1564 | ||
1565 | crtc_state->active = false; | |
1566 | ||
1567 | ret = drm_atomic_set_crtc_for_plane(primary_state, NULL); | |
1568 | if (ret != 0) | |
1569 | return ret; | |
1570 | ||
1571 | drm_atomic_set_fb_for_plane(primary_state, NULL); | |
1572 | ||
1573 | goto commit; | |
1574 | } | |
1575 | ||
1576 | WARN_ON(!set->fb); | |
1577 | WARN_ON(!set->num_connectors); | |
1578 | ||
1579 | ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode); | |
1580 | if (ret != 0) | |
1581 | return ret; | |
1582 | ||
1583 | crtc_state->active = true; | |
1584 | ||
1585 | ret = drm_atomic_set_crtc_for_plane(primary_state, crtc); | |
1586 | if (ret != 0) | |
1587 | return ret; | |
1588 | ||
1589 | drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay); | |
1590 | ||
1591 | drm_atomic_set_fb_for_plane(primary_state, set->fb); | |
1592 | primary_state->crtc_x = 0; | |
1593 | primary_state->crtc_y = 0; | |
1594 | primary_state->crtc_w = hdisplay; | |
1595 | primary_state->crtc_h = vdisplay; | |
1596 | primary_state->src_x = set->x << 16; | |
1597 | primary_state->src_y = set->y << 16; | |
1598 | if (drm_rotation_90_or_270(primary_state->rotation)) { | |
1599 | primary_state->src_w = vdisplay << 16; | |
1600 | primary_state->src_h = hdisplay << 16; | |
1601 | } else { | |
1602 | primary_state->src_w = hdisplay << 16; | |
1603 | primary_state->src_h = vdisplay << 16; | |
1604 | } | |
1605 | ||
1606 | commit: | |
1607 | ret = update_output_state(state, set); | |
1608 | if (ret) | |
1609 | return ret; | |
1610 | ||
1611 | return 0; | |
1612 | } | |
1613 | EXPORT_SYMBOL(__drm_atomic_helper_set_config); | |
1614 | ||
72fdb40c | 1615 | void drm_atomic_print_state(const struct drm_atomic_state *state) |
fceffb32 RC |
1616 | { |
1617 | struct drm_printer p = drm_info_printer(state->dev->dev); | |
1618 | struct drm_plane *plane; | |
1619 | struct drm_plane_state *plane_state; | |
1620 | struct drm_crtc *crtc; | |
1621 | struct drm_crtc_state *crtc_state; | |
1622 | struct drm_connector *connector; | |
1623 | struct drm_connector_state *connector_state; | |
1624 | int i; | |
1625 | ||
1626 | DRM_DEBUG_ATOMIC("checking %p\n", state); | |
1627 | ||
5721a380 | 1628 | for_each_new_plane_in_state(state, plane, plane_state, i) |
fceffb32 RC |
1629 | drm_atomic_plane_print_state(&p, plane_state); |
1630 | ||
5721a380 | 1631 | for_each_new_crtc_in_state(state, crtc, crtc_state, i) |
fceffb32 RC |
1632 | drm_atomic_crtc_print_state(&p, crtc_state); |
1633 | ||
5721a380 | 1634 | for_each_new_connector_in_state(state, connector, connector_state, i) |
fceffb32 RC |
1635 | drm_atomic_connector_print_state(&p, connector_state); |
1636 | } | |
1637 | ||
c2d85564 DV |
1638 | static void __drm_state_dump(struct drm_device *dev, struct drm_printer *p, |
1639 | bool take_locks) | |
6559c901 RC |
1640 | { |
1641 | struct drm_mode_config *config = &dev->mode_config; | |
1642 | struct drm_plane *plane; | |
1643 | struct drm_crtc *crtc; | |
1644 | struct drm_connector *connector; | |
613051da | 1645 | struct drm_connector_list_iter conn_iter; |
6559c901 | 1646 | |
3c499ea0 | 1647 | if (!drm_drv_uses_atomic_modeset(dev)) |
6559c901 RC |
1648 | return; |
1649 | ||
c2d85564 DV |
1650 | list_for_each_entry(plane, &config->plane_list, head) { |
1651 | if (take_locks) | |
1652 | drm_modeset_lock(&plane->mutex, NULL); | |
6559c901 | 1653 | drm_atomic_plane_print_state(p, plane->state); |
c2d85564 DV |
1654 | if (take_locks) |
1655 | drm_modeset_unlock(&plane->mutex); | |
1656 | } | |
6559c901 | 1657 | |
c2d85564 DV |
1658 | list_for_each_entry(crtc, &config->crtc_list, head) { |
1659 | if (take_locks) | |
1660 | drm_modeset_lock(&crtc->mutex, NULL); | |
6559c901 | 1661 | drm_atomic_crtc_print_state(p, crtc->state); |
c2d85564 DV |
1662 | if (take_locks) |
1663 | drm_modeset_unlock(&crtc->mutex); | |
1664 | } | |
6559c901 | 1665 | |
b982dab1 | 1666 | drm_connector_list_iter_begin(dev, &conn_iter); |
c2d85564 DV |
1667 | if (take_locks) |
1668 | drm_modeset_lock(&dev->mode_config.connection_mutex, NULL); | |
613051da | 1669 | drm_for_each_connector_iter(connector, &conn_iter) |
6559c901 | 1670 | drm_atomic_connector_print_state(p, connector->state); |
c2d85564 DV |
1671 | if (take_locks) |
1672 | drm_modeset_unlock(&dev->mode_config.connection_mutex); | |
b982dab1 | 1673 | drm_connector_list_iter_end(&conn_iter); |
6559c901 | 1674 | } |
c2d85564 DV |
1675 | |
1676 | /** | |
1677 | * drm_state_dump - dump entire device atomic state | |
1678 | * @dev: the drm device | |
1679 | * @p: where to print the state to | |
1680 | * | |
1681 | * Just for debugging. Drivers might want an option to dump state | |
1682 | * to dmesg in case of error irq's. (Hint, you probably want to | |
1683 | * ratelimit this!) | |
1684 | * | |
bd1fbef7 DV |
1685 | * The caller must wrap this drm_modeset_lock_all_ctx() and |
1686 | * drm_modeset_drop_locks(). If this is called from error irq handler, it should | |
1687 | * not be enabled by default - if you are debugging errors you might | |
1688 | * not care that this is racey, but calling this without all modeset locks held | |
1689 | * is inherently unsafe. | |
c2d85564 DV |
1690 | */ |
1691 | void drm_state_dump(struct drm_device *dev, struct drm_printer *p) | |
1692 | { | |
1693 | __drm_state_dump(dev, p, false); | |
1694 | } | |
6559c901 RC |
1695 | EXPORT_SYMBOL(drm_state_dump); |
1696 | ||
1697 | #ifdef CONFIG_DEBUG_FS | |
1698 | static int drm_state_info(struct seq_file *m, void *data) | |
1699 | { | |
1700 | struct drm_info_node *node = (struct drm_info_node *) m->private; | |
1701 | struct drm_device *dev = node->minor->dev; | |
1702 | struct drm_printer p = drm_seq_file_printer(m); | |
1703 | ||
c2d85564 | 1704 | __drm_state_dump(dev, &p, true); |
6559c901 RC |
1705 | |
1706 | return 0; | |
1707 | } | |
1708 | ||
1709 | /* any use in debugfs files to dump individual planes/crtc/etc? */ | |
1710 | static const struct drm_info_list drm_atomic_debugfs_list[] = { | |
1711 | {"state", drm_state_info, 0}, | |
1712 | }; | |
1713 | ||
7ce84471 | 1714 | void drm_atomic_debugfs_init(struct drm_minor *minor) |
6559c901 | 1715 | { |
e196e140 WK |
1716 | drm_debugfs_create_files(drm_atomic_debugfs_list, |
1717 | ARRAY_SIZE(drm_atomic_debugfs_list), | |
1718 | minor->debugfs_root, minor); | |
6559c901 RC |
1719 | } |
1720 | #endif |