]>
Commit | Line | Data |
---|---|---|
f453ba04 DA |
1 | /* |
2 | * Copyright (c) 2006-2008 Intel Corporation | |
3 | * Copyright (c) 2007 Dave Airlie <airlied@linux.ie> | |
4 | * | |
5 | * DRM core CRTC related functions | |
6 | * | |
7 | * Permission to use, copy, modify, distribute, and sell this software and its | |
8 | * documentation for any purpose is hereby granted without fee, provided that | |
9 | * the above copyright notice appear in all copies and that both that copyright | |
10 | * notice and this permission notice appear in supporting documentation, and | |
11 | * that the name of the copyright holders not be used in advertising or | |
12 | * publicity pertaining to distribution of the software without specific, | |
13 | * written prior permission. The copyright holders make no representations | |
14 | * about the suitability of this software for any purpose. It is provided "as | |
15 | * is" without express or implied warranty. | |
16 | * | |
17 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, | |
18 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO | |
19 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR | |
20 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, | |
21 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER | |
22 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE | |
23 | * OF THIS SOFTWARE. | |
24 | * | |
25 | * Authors: | |
26 | * Keith Packard | |
27 | * Eric Anholt <eric@anholt.net> | |
28 | * Dave Airlie <airlied@linux.ie> | |
29 | * Jesse Barnes <jesse.barnes@intel.com> | |
30 | */ | |
31 | ||
ba6f5826 | 32 | #include <linux/kernel.h> |
2d1a8a48 | 33 | #include <linux/export.h> |
0603ba14 | 34 | #include <linux/moduleparam.h> |
2d1a8a48 | 35 | |
760285e7 DH |
36 | #include <drm/drmP.h> |
37 | #include <drm/drm_crtc.h> | |
38 | #include <drm/drm_fourcc.h> | |
39 | #include <drm/drm_crtc_helper.h> | |
40 | #include <drm/drm_fb_helper.h> | |
41 | #include <drm/drm_edid.h> | |
f453ba04 | 42 | |
92b6f89f DV |
43 | MODULE_AUTHOR("David Airlie, Jesse Barnes"); |
44 | MODULE_DESCRIPTION("DRM KMS helper"); | |
45 | MODULE_LICENSE("GPL and additional rights"); | |
46 | ||
0d4ed4c8 DV |
47 | /** |
48 | * drm_helper_move_panel_connectors_to_head() - move panels to the front in the | |
49 | * connector list | |
50 | * @dev: drm device to operate on | |
51 | * | |
52 | * Some userspace presumes that the first connected connector is the main | |
53 | * display, where it's supposed to display e.g. the login screen. For | |
54 | * laptops, this should be the main panel. Use this function to sort all | |
55 | * (eDP/LVDS) panels to the front of the connector list, instead of | |
56 | * painstakingly trying to initialize them in the right order. | |
57 | */ | |
cfc1a062 DV |
58 | void drm_helper_move_panel_connectors_to_head(struct drm_device *dev) |
59 | { | |
60 | struct drm_connector *connector, *tmp; | |
61 | struct list_head panel_list; | |
62 | ||
63 | INIT_LIST_HEAD(&panel_list); | |
64 | ||
65 | list_for_each_entry_safe(connector, tmp, | |
66 | &dev->mode_config.connector_list, head) { | |
67 | if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS || | |
68 | connector->connector_type == DRM_MODE_CONNECTOR_eDP) | |
69 | list_move_tail(&connector->head, &panel_list); | |
70 | } | |
71 | ||
72 | list_splice(&panel_list, &dev->mode_config.connector_list); | |
73 | } | |
74 | EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head); | |
75 | ||
c9fb15f6 KP |
76 | /** |
77 | * drm_helper_encoder_in_use - check if a given encoder is in use | |
78 | * @encoder: encoder to check | |
79 | * | |
3fcc42e0 DV |
80 | * Checks whether @encoder is with the current mode setting output configuration |
81 | * in use by any connector. This doesn't mean that it is actually enabled since | |
82 | * the DPMS state is tracked separately. | |
c9fb15f6 | 83 | * |
3fcc42e0 DV |
84 | * Returns: |
85 | * True if @encoder is used, false otherwise. | |
c9fb15f6 KP |
86 | */ |
87 | bool drm_helper_encoder_in_use(struct drm_encoder *encoder) | |
88 | { | |
89 | struct drm_connector *connector; | |
90 | struct drm_device *dev = encoder->dev; | |
62ff94a5 | 91 | |
ba6f5826 SA |
92 | /* |
93 | * We can expect this mutex to be locked if we are not panicking. | |
94 | * Locking is currently fubar in the panic handler. | |
95 | */ | |
8d4ad9d4 | 96 | if (!oops_in_progress) { |
ba6f5826 | 97 | WARN_ON(!mutex_is_locked(&dev->mode_config.mutex)); |
8d4ad9d4 DA |
98 | WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex)); |
99 | } | |
ba6f5826 | 100 | |
c9fb15f6 KP |
101 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) |
102 | if (connector->encoder == encoder) | |
103 | return true; | |
104 | return false; | |
105 | } | |
106 | EXPORT_SYMBOL(drm_helper_encoder_in_use); | |
107 | ||
f453ba04 DA |
108 | /** |
109 | * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config | |
110 | * @crtc: CRTC to check | |
111 | * | |
3fcc42e0 DV |
112 | * Checks whether @crtc is with the current mode setting output configuration |
113 | * in use by any connector. This doesn't mean that it is actually enabled since | |
114 | * the DPMS state is tracked separately. | |
f453ba04 | 115 | * |
3fcc42e0 DV |
116 | * Returns: |
117 | * True if @crtc is used, false otherwise. | |
f453ba04 DA |
118 | */ |
119 | bool drm_helper_crtc_in_use(struct drm_crtc *crtc) | |
120 | { | |
121 | struct drm_encoder *encoder; | |
122 | struct drm_device *dev = crtc->dev; | |
62ff94a5 | 123 | |
ba6f5826 SA |
124 | /* |
125 | * We can expect this mutex to be locked if we are not panicking. | |
126 | * Locking is currently fubar in the panic handler. | |
127 | */ | |
128 | if (!oops_in_progress) | |
129 | WARN_ON(!mutex_is_locked(&dev->mode_config.mutex)); | |
130 | ||
f453ba04 | 131 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) |
c9fb15f6 | 132 | if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder)) |
f453ba04 DA |
133 | return true; |
134 | return false; | |
135 | } | |
136 | EXPORT_SYMBOL(drm_helper_crtc_in_use); | |
137 | ||
86a1b9d1 BS |
138 | static void |
139 | drm_encoder_disable(struct drm_encoder *encoder) | |
140 | { | |
141 | struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private; | |
142 | ||
3b336ec4 SP |
143 | if (encoder->bridge) |
144 | encoder->bridge->funcs->disable(encoder->bridge); | |
145 | ||
86a1b9d1 BS |
146 | if (encoder_funcs->disable) |
147 | (*encoder_funcs->disable)(encoder); | |
148 | else | |
149 | (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF); | |
3b336ec4 SP |
150 | |
151 | if (encoder->bridge) | |
152 | encoder->bridge->funcs->post_disable(encoder->bridge); | |
86a1b9d1 BS |
153 | } |
154 | ||
b182cc59 | 155 | static void __drm_helper_disable_unused_functions(struct drm_device *dev) |
f453ba04 DA |
156 | { |
157 | struct drm_encoder *encoder; | |
f453ba04 DA |
158 | struct drm_crtc *crtc; |
159 | ||
62ff94a5 DV |
160 | drm_warn_on_modeset_not_all_locked(dev); |
161 | ||
f453ba04 | 162 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
92971021 | 163 | if (!drm_helper_encoder_in_use(encoder)) { |
86a1b9d1 | 164 | drm_encoder_disable(encoder); |
b5e6c1da | 165 | /* disconnect encoder from any connector */ |
9c552dd7 | 166 | encoder->crtc = NULL; |
a3a0544b | 167 | } |
f453ba04 DA |
168 | } |
169 | ||
170 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { | |
171 | struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; | |
172 | crtc->enabled = drm_helper_crtc_in_use(crtc); | |
173 | if (!crtc->enabled) { | |
5c8d7171 AD |
174 | if (crtc_funcs->disable) |
175 | (*crtc_funcs->disable)(crtc); | |
176 | else | |
177 | (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF); | |
f4510a27 | 178 | crtc->primary->fb = NULL; |
f453ba04 DA |
179 | } |
180 | } | |
181 | } | |
b182cc59 DV |
182 | |
183 | /** | |
184 | * drm_helper_disable_unused_functions - disable unused objects | |
185 | * @dev: DRM device | |
186 | * | |
187 | * This function walks through the entire mode setting configuration of @dev. It | |
188 | * will remove any crtc links of unused encoders and encoder links of | |
189 | * disconnected connectors. Then it will disable all unused encoders and crtcs | |
190 | * either by calling their disable callback if available or by calling their | |
191 | * dpms callback with DRM_MODE_DPMS_OFF. | |
192 | */ | |
193 | void drm_helper_disable_unused_functions(struct drm_device *dev) | |
194 | { | |
195 | drm_modeset_lock_all(dev); | |
196 | __drm_helper_disable_unused_functions(dev); | |
197 | drm_modeset_unlock_all(dev); | |
198 | } | |
f453ba04 DA |
199 | EXPORT_SYMBOL(drm_helper_disable_unused_functions); |
200 | ||
7bec756c JB |
201 | /* |
202 | * Check the CRTC we're going to map each output to vs. its current | |
203 | * CRTC. If they don't match, we have to disable the output and the CRTC | |
204 | * since the driver will have to re-route things. | |
205 | */ | |
206 | static void | |
207 | drm_crtc_prepare_encoders(struct drm_device *dev) | |
208 | { | |
209 | struct drm_encoder_helper_funcs *encoder_funcs; | |
210 | struct drm_encoder *encoder; | |
211 | ||
212 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { | |
213 | encoder_funcs = encoder->helper_private; | |
214 | /* Disable unused encoders */ | |
215 | if (encoder->crtc == NULL) | |
86a1b9d1 | 216 | drm_encoder_disable(encoder); |
7bec756c JB |
217 | /* Disable encoders whose CRTC is about to change */ |
218 | if (encoder_funcs->get_crtc && | |
219 | encoder->crtc != (*encoder_funcs->get_crtc)(encoder)) | |
86a1b9d1 | 220 | drm_encoder_disable(encoder); |
7bec756c JB |
221 | } |
222 | } | |
223 | ||
f453ba04 | 224 | /** |
0d4ed4c8 | 225 | * drm_crtc_helper_set_mode - internal helper to set a mode |
f453ba04 DA |
226 | * @crtc: CRTC to program |
227 | * @mode: mode to use | |
4c9287c6 AD |
228 | * @x: horizontal offset into the surface |
229 | * @y: vertical offset into the surface | |
0d4ed4c8 | 230 | * @old_fb: old framebuffer, for cleanup |
f453ba04 | 231 | * |
f453ba04 | 232 | * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance |
0d4ed4c8 DV |
233 | * to fixup or reject the mode prior to trying to set it. This is an internal |
234 | * helper that drivers could e.g. use to update properties that require the | |
235 | * entire output pipe to be disabled and re-enabled in a new configuration. For | |
236 | * example for changing whether audio is enabled on a hdmi link or for changing | |
237 | * panel fitter or dither attributes. It is also called by the | |
238 | * drm_crtc_helper_set_config() helper function to drive the mode setting | |
239 | * sequence. | |
f453ba04 | 240 | * |
3fcc42e0 DV |
241 | * Returns: |
242 | * True if the mode was set successfully, false otherwise. | |
f453ba04 DA |
243 | */ |
244 | bool drm_crtc_helper_set_mode(struct drm_crtc *crtc, | |
245 | struct drm_display_mode *mode, | |
3c4fdcfb KH |
246 | int x, int y, |
247 | struct drm_framebuffer *old_fb) | |
f453ba04 DA |
248 | { |
249 | struct drm_device *dev = crtc->dev; | |
7e99acdc | 250 | struct drm_display_mode *adjusted_mode, saved_mode; |
f453ba04 DA |
251 | struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; |
252 | struct drm_encoder_helper_funcs *encoder_funcs; | |
253 | int saved_x, saved_y; | |
7e99acdc | 254 | bool saved_enabled; |
f453ba04 DA |
255 | struct drm_encoder *encoder; |
256 | bool ret = true; | |
257 | ||
62ff94a5 DV |
258 | drm_warn_on_modeset_not_all_locked(dev); |
259 | ||
7e99acdc | 260 | saved_enabled = crtc->enabled; |
f453ba04 | 261 | crtc->enabled = drm_helper_crtc_in_use(crtc); |
f453ba04 DA |
262 | if (!crtc->enabled) |
263 | return true; | |
264 | ||
021a8455 | 265 | adjusted_mode = drm_mode_duplicate(dev, mode); |
7e99acdc IH |
266 | if (!adjusted_mode) { |
267 | crtc->enabled = saved_enabled; | |
6bfc56aa | 268 | return false; |
7e99acdc | 269 | } |
021a8455 | 270 | |
f453ba04 DA |
271 | saved_mode = crtc->mode; |
272 | saved_x = crtc->x; | |
273 | saved_y = crtc->y; | |
274 | ||
275 | /* Update crtc values up front so the driver can rely on them for mode | |
276 | * setting. | |
277 | */ | |
278 | crtc->mode = *mode; | |
279 | crtc->x = x; | |
280 | crtc->y = y; | |
281 | ||
f453ba04 DA |
282 | /* Pass our mode to the connectors and the CRTC to give them a chance to |
283 | * adjust it according to limitations or connector properties, and also | |
284 | * a chance to reject the mode entirely. | |
285 | */ | |
286 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { | |
287 | ||
288 | if (encoder->crtc != crtc) | |
289 | continue; | |
3b336ec4 SP |
290 | |
291 | if (encoder->bridge && encoder->bridge->funcs->mode_fixup) { | |
292 | ret = encoder->bridge->funcs->mode_fixup( | |
293 | encoder->bridge, mode, adjusted_mode); | |
294 | if (!ret) { | |
295 | DRM_DEBUG_KMS("Bridge fixup failed\n"); | |
296 | goto done; | |
297 | } | |
298 | } | |
299 | ||
f453ba04 DA |
300 | encoder_funcs = encoder->helper_private; |
301 | if (!(ret = encoder_funcs->mode_fixup(encoder, mode, | |
302 | adjusted_mode))) { | |
836e53d7 | 303 | DRM_DEBUG_KMS("Encoder fixup failed\n"); |
f453ba04 DA |
304 | goto done; |
305 | } | |
306 | } | |
307 | ||
308 | if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) { | |
836e53d7 | 309 | DRM_DEBUG_KMS("CRTC fixup failed\n"); |
f453ba04 DA |
310 | goto done; |
311 | } | |
9440106b | 312 | DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id); |
f453ba04 DA |
313 | |
314 | /* Prepare the encoders and CRTCs before setting the mode. */ | |
315 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { | |
316 | ||
317 | if (encoder->crtc != crtc) | |
318 | continue; | |
3b336ec4 SP |
319 | |
320 | if (encoder->bridge) | |
321 | encoder->bridge->funcs->disable(encoder->bridge); | |
322 | ||
f453ba04 DA |
323 | encoder_funcs = encoder->helper_private; |
324 | /* Disable the encoders as the first thing we do. */ | |
325 | encoder_funcs->prepare(encoder); | |
3b336ec4 SP |
326 | |
327 | if (encoder->bridge) | |
328 | encoder->bridge->funcs->post_disable(encoder->bridge); | |
f453ba04 DA |
329 | } |
330 | ||
7bec756c JB |
331 | drm_crtc_prepare_encoders(dev); |
332 | ||
f453ba04 DA |
333 | crtc_funcs->prepare(crtc); |
334 | ||
335 | /* Set up the DPLL and any encoders state that needs to adjust or depend | |
336 | * on the DPLL. | |
337 | */ | |
5c3b82e2 CW |
338 | ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb); |
339 | if (!ret) | |
340 | goto done; | |
f453ba04 DA |
341 | |
342 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { | |
343 | ||
344 | if (encoder->crtc != crtc) | |
345 | continue; | |
346 | ||
9440106b | 347 | DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n", |
83a8cfd3 | 348 | encoder->base.id, encoder->name, |
9440106b | 349 | mode->base.id, mode->name); |
f453ba04 DA |
350 | encoder_funcs = encoder->helper_private; |
351 | encoder_funcs->mode_set(encoder, mode, adjusted_mode); | |
3b336ec4 SP |
352 | |
353 | if (encoder->bridge && encoder->bridge->funcs->mode_set) | |
354 | encoder->bridge->funcs->mode_set(encoder->bridge, mode, | |
355 | adjusted_mode); | |
f453ba04 DA |
356 | } |
357 | ||
358 | /* Now enable the clocks, plane, pipe, and connectors that we set up. */ | |
359 | crtc_funcs->commit(crtc); | |
360 | ||
361 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { | |
362 | ||
363 | if (encoder->crtc != crtc) | |
364 | continue; | |
365 | ||
3b336ec4 SP |
366 | if (encoder->bridge) |
367 | encoder->bridge->funcs->pre_enable(encoder->bridge); | |
368 | ||
f453ba04 DA |
369 | encoder_funcs = encoder->helper_private; |
370 | encoder_funcs->commit(encoder); | |
371 | ||
3b336ec4 SP |
372 | if (encoder->bridge) |
373 | encoder->bridge->funcs->enable(encoder->bridge); | |
f453ba04 DA |
374 | } |
375 | ||
27641c3f MK |
376 | /* Store real post-adjustment hardware mode. */ |
377 | crtc->hwmode = *adjusted_mode; | |
378 | ||
379 | /* Calculate and store various constants which | |
380 | * are later needed by vblank and swap-completion | |
381 | * timestamping. They are derived from true hwmode. | |
382 | */ | |
545cdd55 | 383 | drm_calc_timestamping_constants(crtc, &crtc->hwmode); |
27641c3f | 384 | |
f453ba04 DA |
385 | /* FIXME: add subpixel order */ |
386 | done: | |
021a8455 | 387 | drm_mode_destroy(dev, adjusted_mode); |
f453ba04 | 388 | if (!ret) { |
7e99acdc | 389 | crtc->enabled = saved_enabled; |
f453ba04 DA |
390 | crtc->mode = saved_mode; |
391 | crtc->x = saved_x; | |
392 | crtc->y = saved_y; | |
393 | } | |
394 | ||
395 | return ret; | |
396 | } | |
397 | EXPORT_SYMBOL(drm_crtc_helper_set_mode); | |
398 | ||
a74591d7 | 399 | static void |
6eebd6bb CW |
400 | drm_crtc_helper_disable(struct drm_crtc *crtc) |
401 | { | |
402 | struct drm_device *dev = crtc->dev; | |
403 | struct drm_connector *connector; | |
404 | struct drm_encoder *encoder; | |
405 | ||
406 | /* Decouple all encoders and their attached connectors from this crtc */ | |
407 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { | |
408 | if (encoder->crtc != crtc) | |
409 | continue; | |
410 | ||
411 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { | |
412 | if (connector->encoder != encoder) | |
413 | continue; | |
414 | ||
415 | connector->encoder = NULL; | |
a6ad6230 TR |
416 | |
417 | /* | |
418 | * drm_helper_disable_unused_functions() ought to be | |
419 | * doing this, but since we've decoupled the encoder | |
420 | * from the connector above, the required connection | |
421 | * between them is henceforth no longer available. | |
422 | */ | |
423 | connector->dpms = DRM_MODE_DPMS_OFF; | |
6eebd6bb CW |
424 | } |
425 | } | |
426 | ||
b182cc59 | 427 | __drm_helper_disable_unused_functions(dev); |
6eebd6bb CW |
428 | } |
429 | ||
f453ba04 DA |
430 | /** |
431 | * drm_crtc_helper_set_config - set a new config from userspace | |
0d4ed4c8 | 432 | * @set: mode set configuration |
f453ba04 | 433 | * |
0d4ed4c8 | 434 | * Setup a new configuration, provided by the upper layers (either an ioctl call |
e227867f | 435 | * from userspace or internally e.g. from the fbdev support code) in @set, and |
0d4ed4c8 DV |
436 | * enable it. This is the main helper functions for drivers that implement |
437 | * kernel mode setting with the crtc helper functions and the assorted | |
438 | * ->prepare(), ->modeset() and ->commit() helper callbacks. | |
f453ba04 | 439 | * |
3fcc42e0 DV |
440 | * Returns: |
441 | * Returns 0 on success, negative errno numbers on failure. | |
f453ba04 DA |
442 | */ |
443 | int drm_crtc_helper_set_config(struct drm_mode_set *set) | |
444 | { | |
445 | struct drm_device *dev; | |
02ee4e94 | 446 | struct drm_crtc *new_crtc; |
e67aae79 | 447 | struct drm_encoder *save_encoders, *new_encoder, *encoder; |
4cb72b17 JB |
448 | bool mode_changed = false; /* if true do a full mode set */ |
449 | bool fb_changed = false; /* if true and !mode_changed just do a flip */ | |
e67aae79 | 450 | struct drm_connector *save_connectors, *connector; |
f453ba04 DA |
451 | int count = 0, ro, fail = 0; |
452 | struct drm_crtc_helper_funcs *crtc_funcs; | |
c5006cfe | 453 | struct drm_mode_set save_set; |
4a1b0714 | 454 | int ret; |
bf9dc102 | 455 | int i; |
f453ba04 | 456 | |
58367ed6 | 457 | DRM_DEBUG_KMS("\n"); |
f453ba04 | 458 | |
e58de880 DV |
459 | BUG_ON(!set); |
460 | BUG_ON(!set->crtc); | |
461 | BUG_ON(!set->crtc->helper_private); | |
f453ba04 | 462 | |
e58de880 DV |
463 | /* Enforce sane interface api - has been abused by the fb helper. */ |
464 | BUG_ON(!set->mode && set->fb); | |
465 | BUG_ON(set->fb && set->num_connectors == 0); | |
f453ba04 DA |
466 | |
467 | crtc_funcs = set->crtc->helper_private; | |
468 | ||
ede3ff52 CW |
469 | if (!set->mode) |
470 | set->fb = NULL; | |
471 | ||
9440106b JG |
472 | if (set->fb) { |
473 | DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n", | |
474 | set->crtc->base.id, set->fb->base.id, | |
475 | (int)set->num_connectors, set->x, set->y); | |
476 | } else { | |
ede3ff52 | 477 | DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id); |
a74591d7 TR |
478 | drm_crtc_helper_disable(set->crtc); |
479 | return 0; | |
9440106b | 480 | } |
f453ba04 DA |
481 | |
482 | dev = set->crtc->dev; | |
483 | ||
62ff94a5 DV |
484 | drm_warn_on_modeset_not_all_locked(dev); |
485 | ||
02ee4e94 IH |
486 | /* |
487 | * Allocate space for the backup of all (non-pointer) encoder and | |
488 | * connector data. | |
489 | */ | |
e67aae79 MM |
490 | save_encoders = kzalloc(dev->mode_config.num_encoder * |
491 | sizeof(struct drm_encoder), GFP_KERNEL); | |
02ee4e94 | 492 | if (!save_encoders) |
f453ba04 | 493 | return -ENOMEM; |
f453ba04 | 494 | |
e67aae79 MM |
495 | save_connectors = kzalloc(dev->mode_config.num_connector * |
496 | sizeof(struct drm_connector), GFP_KERNEL); | |
497 | if (!save_connectors) { | |
e67aae79 MM |
498 | kfree(save_encoders); |
499 | return -ENOMEM; | |
500 | } | |
501 | ||
02ee4e94 IH |
502 | /* |
503 | * Copy data. Note that driver private data is not affected. | |
e67aae79 MM |
504 | * Should anything bad happen only the expected state is |
505 | * restored, not the drivers personal bookkeeping. | |
506 | */ | |
e67aae79 MM |
507 | count = 0; |
508 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { | |
509 | save_encoders[count++] = *encoder; | |
510 | } | |
511 | ||
512 | count = 0; | |
513 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { | |
514 | save_connectors[count++] = *connector; | |
515 | } | |
516 | ||
c5006cfe JB |
517 | save_set.crtc = set->crtc; |
518 | save_set.mode = &set->crtc->mode; | |
519 | save_set.x = set->crtc->x; | |
520 | save_set.y = set->crtc->y; | |
f4510a27 | 521 | save_set.fb = set->crtc->primary->fb; |
c5006cfe | 522 | |
f453ba04 DA |
523 | /* We should be able to check here if the fb has the same properties |
524 | * and then just flip_or_move it */ | |
f4510a27 | 525 | if (set->crtc->primary->fb != set->fb) { |
712531bf | 526 | /* If we have no fb then treat it as a full mode set */ |
f4510a27 | 527 | if (set->crtc->primary->fb == NULL) { |
58367ed6 | 528 | DRM_DEBUG_KMS("crtc has no fb, full mode set\n"); |
712531bf | 529 | mode_changed = true; |
4cb72b17 JB |
530 | } else if (set->fb == NULL) { |
531 | mode_changed = true; | |
ce83adf7 | 532 | } else if (set->fb->pixel_format != |
f4510a27 | 533 | set->crtc->primary->fb->pixel_format) { |
ce83adf7 | 534 | mode_changed = true; |
8dff4742 | 535 | } else |
712531bf | 536 | fb_changed = true; |
f453ba04 DA |
537 | } |
538 | ||
539 | if (set->x != set->crtc->x || set->y != set->crtc->y) | |
712531bf | 540 | fb_changed = true; |
f453ba04 DA |
541 | |
542 | if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) { | |
58367ed6 | 543 | DRM_DEBUG_KMS("modes are different, full mode set\n"); |
f453ba04 DA |
544 | drm_mode_debug_printmodeline(&set->crtc->mode); |
545 | drm_mode_debug_printmodeline(set->mode); | |
712531bf | 546 | mode_changed = true; |
f453ba04 DA |
547 | } |
548 | ||
549 | /* a) traverse passed in connector list and get encoders for them */ | |
550 | count = 0; | |
551 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { | |
552 | struct drm_connector_helper_funcs *connector_funcs = | |
553 | connector->helper_private; | |
f453ba04 DA |
554 | new_encoder = connector->encoder; |
555 | for (ro = 0; ro < set->num_connectors; ro++) { | |
556 | if (set->connectors[ro] == connector) { | |
557 | new_encoder = connector_funcs->best_encoder(connector); | |
558 | /* if we can't get an encoder for a connector | |
559 | we are setting now - then fail */ | |
560 | if (new_encoder == NULL) | |
561 | /* don't break so fail path works correct */ | |
562 | fail = 1; | |
25f397a4 DV |
563 | |
564 | if (connector->dpms != DRM_MODE_DPMS_ON) { | |
565 | DRM_DEBUG_KMS("connector dpms not on, full mode switch\n"); | |
566 | mode_changed = true; | |
567 | } | |
177cf92d DV |
568 | |
569 | break; | |
f453ba04 DA |
570 | } |
571 | } | |
572 | ||
573 | if (new_encoder != connector->encoder) { | |
58367ed6 | 574 | DRM_DEBUG_KMS("encoder changed, full mode switch\n"); |
712531bf | 575 | mode_changed = true; |
ff846ab7 MM |
576 | /* If the encoder is reused for another connector, then |
577 | * the appropriate crtc will be set later. | |
578 | */ | |
ff6fdbed MM |
579 | if (connector->encoder) |
580 | connector->encoder->crtc = NULL; | |
f453ba04 DA |
581 | connector->encoder = new_encoder; |
582 | } | |
583 | } | |
584 | ||
585 | if (fail) { | |
586 | ret = -EINVAL; | |
e67aae79 | 587 | goto fail; |
f453ba04 DA |
588 | } |
589 | ||
590 | count = 0; | |
591 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { | |
592 | if (!connector->encoder) | |
593 | continue; | |
594 | ||
f453ba04 DA |
595 | if (connector->encoder->crtc == set->crtc) |
596 | new_crtc = NULL; | |
597 | else | |
598 | new_crtc = connector->encoder->crtc; | |
599 | ||
600 | for (ro = 0; ro < set->num_connectors; ro++) { | |
601 | if (set->connectors[ro] == connector) | |
602 | new_crtc = set->crtc; | |
603 | } | |
7bec756c JB |
604 | |
605 | /* Make sure the new CRTC will work with the encoder */ | |
606 | if (new_crtc && | |
607 | !drm_encoder_crtc_ok(connector->encoder, new_crtc)) { | |
608 | ret = -EINVAL; | |
e67aae79 | 609 | goto fail; |
7bec756c | 610 | } |
f453ba04 | 611 | if (new_crtc != connector->encoder->crtc) { |
58367ed6 | 612 | DRM_DEBUG_KMS("crtc changed, full mode switch\n"); |
712531bf | 613 | mode_changed = true; |
f453ba04 DA |
614 | connector->encoder->crtc = new_crtc; |
615 | } | |
9440106b JG |
616 | if (new_crtc) { |
617 | DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n", | |
25933820 | 618 | connector->base.id, connector->name, |
9440106b JG |
619 | new_crtc->base.id); |
620 | } else { | |
621 | DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n", | |
25933820 | 622 | connector->base.id, connector->name); |
9440106b | 623 | } |
f453ba04 DA |
624 | } |
625 | ||
626 | /* mode_set_base is not a required function */ | |
712531bf JB |
627 | if (fb_changed && !crtc_funcs->mode_set_base) |
628 | mode_changed = true; | |
f453ba04 | 629 | |
712531bf | 630 | if (mode_changed) { |
48b1f5dd | 631 | if (drm_helper_crtc_in_use(set->crtc)) { |
58367ed6 ZY |
632 | DRM_DEBUG_KMS("attempting to set mode from" |
633 | " userspace\n"); | |
f453ba04 | 634 | drm_mode_debug_printmodeline(set->mode); |
f4510a27 | 635 | set->crtc->primary->fb = set->fb; |
f453ba04 | 636 | if (!drm_crtc_helper_set_mode(set->crtc, set->mode, |
3c4fdcfb | 637 | set->x, set->y, |
bec2eac3 | 638 | save_set.fb)) { |
9440106b JG |
639 | DRM_ERROR("failed to set mode on [CRTC:%d]\n", |
640 | set->crtc->base.id); | |
f4510a27 | 641 | set->crtc->primary->fb = save_set.fb; |
f453ba04 | 642 | ret = -EINVAL; |
e67aae79 | 643 | goto fail; |
f453ba04 | 644 | } |
25f397a4 DV |
645 | DRM_DEBUG_KMS("Setting connector DPMS state to on\n"); |
646 | for (i = 0; i < set->num_connectors; i++) { | |
647 | DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id, | |
25933820 | 648 | set->connectors[i]->name); |
25f397a4 DV |
649 | set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON); |
650 | } | |
f453ba04 | 651 | } |
b182cc59 | 652 | __drm_helper_disable_unused_functions(dev); |
712531bf | 653 | } else if (fb_changed) { |
9b1596af BS |
654 | set->crtc->x = set->x; |
655 | set->crtc->y = set->y; | |
f4510a27 | 656 | set->crtc->primary->fb = set->fb; |
5c3b82e2 | 657 | ret = crtc_funcs->mode_set_base(set->crtc, |
bec2eac3 | 658 | set->x, set->y, save_set.fb); |
0ba41e44 | 659 | if (ret != 0) { |
fbce4064 IH |
660 | set->crtc->x = save_set.x; |
661 | set->crtc->y = save_set.y; | |
f4510a27 | 662 | set->crtc->primary->fb = save_set.fb; |
e67aae79 | 663 | goto fail; |
0ba41e44 | 664 | } |
f453ba04 DA |
665 | } |
666 | ||
e67aae79 | 667 | kfree(save_connectors); |
f453ba04 | 668 | kfree(save_encoders); |
f453ba04 DA |
669 | return 0; |
670 | ||
e67aae79 MM |
671 | fail: |
672 | /* Restore all previous data. */ | |
e67aae79 MM |
673 | count = 0; |
674 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { | |
675 | *encoder = save_encoders[count++]; | |
e62fb64e | 676 | } |
e67aae79 | 677 | |
f453ba04 DA |
678 | count = 0; |
679 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { | |
e67aae79 | 680 | *connector = save_connectors[count++]; |
f453ba04 | 681 | } |
e67aae79 | 682 | |
c5006cfe JB |
683 | /* Try to restore the config */ |
684 | if (mode_changed && | |
685 | !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x, | |
686 | save_set.y, save_set.fb)) | |
687 | DRM_ERROR("failed to restore config after modeset failure\n"); | |
688 | ||
e67aae79 | 689 | kfree(save_connectors); |
f453ba04 DA |
690 | kfree(save_encoders); |
691 | return ret; | |
692 | } | |
693 | EXPORT_SYMBOL(drm_crtc_helper_set_config); | |
694 | ||
c9fb15f6 KP |
695 | static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder) |
696 | { | |
697 | int dpms = DRM_MODE_DPMS_OFF; | |
698 | struct drm_connector *connector; | |
699 | struct drm_device *dev = encoder->dev; | |
700 | ||
701 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) | |
702 | if (connector->encoder == encoder) | |
703 | if (connector->dpms < dpms) | |
704 | dpms = connector->dpms; | |
705 | return dpms; | |
706 | } | |
707 | ||
3b336ec4 SP |
708 | /* Helper which handles bridge ordering around encoder dpms */ |
709 | static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode) | |
710 | { | |
711 | struct drm_bridge *bridge = encoder->bridge; | |
712 | struct drm_encoder_helper_funcs *encoder_funcs; | |
713 | ||
714 | if (bridge) { | |
715 | if (mode == DRM_MODE_DPMS_ON) | |
716 | bridge->funcs->pre_enable(bridge); | |
717 | else | |
718 | bridge->funcs->disable(bridge); | |
719 | } | |
720 | ||
721 | encoder_funcs = encoder->helper_private; | |
722 | if (encoder_funcs->dpms) | |
723 | encoder_funcs->dpms(encoder, mode); | |
724 | ||
725 | if (bridge) { | |
726 | if (mode == DRM_MODE_DPMS_ON) | |
727 | bridge->funcs->enable(bridge); | |
728 | else | |
729 | bridge->funcs->post_disable(bridge); | |
730 | } | |
731 | } | |
732 | ||
c9fb15f6 KP |
733 | static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc) |
734 | { | |
735 | int dpms = DRM_MODE_DPMS_OFF; | |
736 | struct drm_connector *connector; | |
737 | struct drm_device *dev = crtc->dev; | |
738 | ||
739 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) | |
740 | if (connector->encoder && connector->encoder->crtc == crtc) | |
741 | if (connector->dpms < dpms) | |
742 | dpms = connector->dpms; | |
743 | return dpms; | |
744 | } | |
745 | ||
746 | /** | |
0d4ed4c8 DV |
747 | * drm_helper_connector_dpms() - connector dpms helper implementation |
748 | * @connector: affected connector | |
749 | * @mode: DPMS mode | |
c9fb15f6 | 750 | * |
0d4ed4c8 DV |
751 | * This is the main helper function provided by the crtc helper framework for |
752 | * implementing the DPMS connector attribute. It computes the new desired DPMS | |
753 | * state for all encoders and crtcs in the output mesh and calls the ->dpms() | |
754 | * callback provided by the driver appropriately. | |
c9fb15f6 KP |
755 | */ |
756 | void drm_helper_connector_dpms(struct drm_connector *connector, int mode) | |
757 | { | |
758 | struct drm_encoder *encoder = connector->encoder; | |
759 | struct drm_crtc *crtc = encoder ? encoder->crtc : NULL; | |
3b336ec4 | 760 | int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF; |
c9fb15f6 KP |
761 | |
762 | if (mode == connector->dpms) | |
763 | return; | |
764 | ||
765 | old_dpms = connector->dpms; | |
766 | connector->dpms = mode; | |
767 | ||
3b336ec4 SP |
768 | if (encoder) |
769 | encoder_dpms = drm_helper_choose_encoder_dpms(encoder); | |
770 | ||
c9fb15f6 KP |
771 | /* from off to on, do crtc then encoder */ |
772 | if (mode < old_dpms) { | |
773 | if (crtc) { | |
774 | struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; | |
775 | if (crtc_funcs->dpms) | |
776 | (*crtc_funcs->dpms) (crtc, | |
777 | drm_helper_choose_crtc_dpms(crtc)); | |
778 | } | |
3b336ec4 SP |
779 | if (encoder) |
780 | drm_helper_encoder_dpms(encoder, encoder_dpms); | |
c9fb15f6 KP |
781 | } |
782 | ||
783 | /* from on to off, do encoder then crtc */ | |
784 | if (mode > old_dpms) { | |
3b336ec4 SP |
785 | if (encoder) |
786 | drm_helper_encoder_dpms(encoder, encoder_dpms); | |
c9fb15f6 KP |
787 | if (crtc) { |
788 | struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; | |
789 | if (crtc_funcs->dpms) | |
790 | (*crtc_funcs->dpms) (crtc, | |
791 | drm_helper_choose_crtc_dpms(crtc)); | |
792 | } | |
793 | } | |
794 | ||
795 | return; | |
796 | } | |
797 | EXPORT_SYMBOL(drm_helper_connector_dpms); | |
798 | ||
3fcc42e0 DV |
799 | /** |
800 | * drm_helper_mode_fill_fb_struct - fill out framebuffer metadata | |
801 | * @fb: drm_framebuffer object to fill out | |
802 | * @mode_cmd: metadata from the userspace fb creation request | |
803 | * | |
804 | * This helper can be used in a drivers fb_create callback to pre-fill the fb's | |
805 | * metadata fields. | |
806 | */ | |
9fd93784 DV |
807 | void drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb, |
808 | struct drm_mode_fb_cmd2 *mode_cmd) | |
f453ba04 | 809 | { |
01f2c773 VS |
810 | int i; |
811 | ||
f453ba04 DA |
812 | fb->width = mode_cmd->width; |
813 | fb->height = mode_cmd->height; | |
01f2c773 VS |
814 | for (i = 0; i < 4; i++) { |
815 | fb->pitches[i] = mode_cmd->pitches[i]; | |
816 | fb->offsets[i] = mode_cmd->offsets[i]; | |
817 | } | |
248dbc23 | 818 | drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth, |
308e5bcb JB |
819 | &fb->bits_per_pixel); |
820 | fb->pixel_format = mode_cmd->pixel_format; | |
f453ba04 DA |
821 | } |
822 | EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct); | |
823 | ||
aa4cd910 DV |
824 | /** |
825 | * drm_helper_resume_force_mode - force-restore mode setting configuration | |
826 | * @dev: drm_device which should be restored | |
827 | * | |
828 | * Drivers which use the mode setting helpers can use this function to | |
829 | * force-restore the mode setting configuration e.g. on resume or when something | |
830 | * else might have trampled over the hw state (like some overzealous old BIOSen | |
831 | * tended to do). | |
00d762cb DV |
832 | * |
833 | * This helper doesn't provide a error return value since restoring the old | |
834 | * config should never fail due to resource allocation issues since the driver | |
835 | * has successfully set the restored configuration already. Hence this should | |
836 | * boil down to the equivalent of a few dpms on calls, which also don't provide | |
837 | * an error code. | |
838 | * | |
839 | * Drivers where simply restoring an old configuration again might fail (e.g. | |
840 | * due to slight differences in allocating shared resources when the | |
841 | * configuration is restored in a different order than when userspace set it up) | |
842 | * need to use their own restore logic. | |
aa4cd910 | 843 | */ |
00d762cb | 844 | void drm_helper_resume_force_mode(struct drm_device *dev) |
f453ba04 DA |
845 | { |
846 | struct drm_crtc *crtc; | |
89347bb8 | 847 | struct drm_encoder *encoder; |
89347bb8 | 848 | struct drm_crtc_helper_funcs *crtc_funcs; |
00d762cb DV |
849 | int encoder_dpms; |
850 | bool ret; | |
f453ba04 | 851 | |
3ea87855 | 852 | drm_modeset_lock_all(dev); |
f453ba04 DA |
853 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { |
854 | ||
855 | if (!crtc->enabled) | |
856 | continue; | |
857 | ||
3c4fdcfb | 858 | ret = drm_crtc_helper_set_mode(crtc, &crtc->mode, |
f4510a27 | 859 | crtc->x, crtc->y, crtc->primary->fb); |
f453ba04 | 860 | |
00d762cb | 861 | /* Restoring the old config should never fail! */ |
f453ba04 DA |
862 | if (ret == false) |
863 | DRM_ERROR("failed to set mode on crtc %p\n", crtc); | |
89347bb8 DJ |
864 | |
865 | /* Turn off outputs that were already powered off */ | |
866 | if (drm_helper_choose_crtc_dpms(crtc)) { | |
867 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { | |
868 | ||
869 | if(encoder->crtc != crtc) | |
870 | continue; | |
871 | ||
3b336ec4 SP |
872 | encoder_dpms = drm_helper_choose_encoder_dpms( |
873 | encoder); | |
874 | ||
875 | drm_helper_encoder_dpms(encoder, encoder_dpms); | |
89347bb8 | 876 | } |
817e631e CW |
877 | |
878 | crtc_funcs = crtc->helper_private; | |
879 | if (crtc_funcs->dpms) | |
880 | (*crtc_funcs->dpms) (crtc, | |
881 | drm_helper_choose_crtc_dpms(crtc)); | |
89347bb8 | 882 | } |
f453ba04 | 883 | } |
00d762cb | 884 | |
af4fcb57 | 885 | /* disable the unused connectors while restoring the modesetting */ |
b182cc59 | 886 | __drm_helper_disable_unused_functions(dev); |
3ea87855 | 887 | drm_modeset_unlock_all(dev); |
f453ba04 DA |
888 | } |
889 | EXPORT_SYMBOL(drm_helper_resume_force_mode); |