]>
Commit | Line | Data |
---|---|---|
785b93ef DA |
1 | /* |
2 | * Copyright (c) 2006-2009 Red Hat Inc. | |
3 | * Copyright (c) 2006-2008 Intel Corporation | |
4 | * Copyright (c) 2007 Dave Airlie <airlied@linux.ie> | |
5 | * | |
6 | * DRM framebuffer helper functions | |
7 | * | |
8 | * Permission to use, copy, modify, distribute, and sell this software and its | |
9 | * documentation for any purpose is hereby granted without fee, provided that | |
10 | * the above copyright notice appear in all copies and that both that copyright | |
11 | * notice and this permission notice appear in supporting documentation, and | |
12 | * that the name of the copyright holders not be used in advertising or | |
13 | * publicity pertaining to distribution of the software without specific, | |
14 | * written prior permission. The copyright holders make no representations | |
15 | * about the suitability of this software for any purpose. It is provided "as | |
16 | * is" without express or implied warranty. | |
17 | * | |
18 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, | |
19 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO | |
20 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR | |
21 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, | |
22 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER | |
23 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE | |
24 | * OF THIS SOFTWARE. | |
25 | * | |
26 | * Authors: | |
27 | * Dave Airlie <airlied@linux.ie> | |
28 | * Jesse Barnes <jesse.barnes@intel.com> | |
29 | */ | |
d56b1b9d SK |
30 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
31 | ||
3b40a443 | 32 | #include <linux/kernel.h> |
785b93ef | 33 | #include <linux/sysrq.h> |
5a0e3ad6 | 34 | #include <linux/slab.h> |
785b93ef | 35 | #include <linux/fb.h> |
e0cd3608 | 36 | #include <linux/module.h> |
760285e7 DH |
37 | #include <drm/drmP.h> |
38 | #include <drm/drm_crtc.h> | |
39 | #include <drm/drm_fb_helper.h> | |
40 | #include <drm/drm_crtc_helper.h> | |
785b93ef DA |
41 | |
42 | static LIST_HEAD(kernel_fb_helper_list); | |
43 | ||
d0ddc033 DV |
44 | /** |
45 | * DOC: fbdev helpers | |
46 | * | |
47 | * The fb helper functions are useful to provide an fbdev on top of a drm kernel | |
83c617c5 | 48 | * mode setting driver. They can be used mostly independently from the crtc |
d0ddc033 DV |
49 | * helper functions used by many drivers to implement the kernel mode setting |
50 | * interfaces. | |
207fd329 | 51 | * |
10a23102 TR |
52 | * Initialization is done as a four-step process with drm_fb_helper_prepare(), |
53 | * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and | |
54 | * drm_fb_helper_initial_config(). Drivers with fancier requirements than the | |
55 | * default behaviour can override the third step with their own code. | |
56 | * Teardown is done with drm_fb_helper_fini(). | |
207fd329 DV |
57 | * |
58 | * At runtime drivers should restore the fbdev console by calling | |
59 | * drm_fb_helper_restore_fbdev_mode() from their ->lastclose callback. They | |
60 | * should also notify the fb helper code from updates to the output | |
61 | * configuration by calling drm_fb_helper_hotplug_event(). For easier | |
62 | * integration with the output polling code in drm_crtc_helper.c the modeset | |
83c617c5 | 63 | * code provides a ->output_poll_changed callback. |
207fd329 DV |
64 | * |
65 | * All other functions exported by the fb helper library can be used to | |
66 | * implement the fbdev driver interface by the driver. | |
10a23102 TR |
67 | * |
68 | * It is possible, though perhaps somewhat tricky, to implement race-free | |
69 | * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare() | |
70 | * helper must be called first to initialize the minimum required to make | |
71 | * hotplug detection work. Drivers also need to make sure to properly set up | |
72 | * the dev->mode_config.funcs member. After calling drm_kms_helper_poll_init() | |
73 | * it is safe to enable interrupts and start processing hotplug events. At the | |
74 | * same time, drivers should initialize all modeset objects such as CRTCs, | |
75 | * encoders and connectors. To finish up the fbdev helper initialization, the | |
76 | * drm_fb_helper_init() function is called. To probe for all attached displays | |
77 | * and set up an initial configuration using the detected hardware, drivers | |
78 | * should call drm_fb_helper_single_add_all_connectors() followed by | |
79 | * drm_fb_helper_initial_config(). | |
d0ddc033 DV |
80 | */ |
81 | ||
207fd329 DV |
82 | /** |
83 | * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev | |
84 | * emulation helper | |
85 | * @fb_helper: fbdev initialized with drm_fb_helper_init | |
86 | * | |
87 | * This functions adds all the available connectors for use with the given | |
88 | * fb_helper. This is a separate step to allow drivers to freely assign | |
89 | * connectors to the fbdev, e.g. if some are reserved for special purposes or | |
90 | * not adequate to be used for the fbcon. | |
91 | * | |
92 | * Since this is part of the initial setup before the fbdev is published, no | |
93 | * locking is required. | |
94 | */ | |
0b4c0f3f | 95 | int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper) |
d50ba256 | 96 | { |
0b4c0f3f DA |
97 | struct drm_device *dev = fb_helper->dev; |
98 | struct drm_connector *connector; | |
99 | int i; | |
d50ba256 | 100 | |
0b4c0f3f DA |
101 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
102 | struct drm_fb_helper_connector *fb_helper_connector; | |
103 | ||
104 | fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL); | |
105 | if (!fb_helper_connector) | |
106 | goto fail; | |
d50ba256 | 107 | |
0b4c0f3f DA |
108 | fb_helper_connector->connector = connector; |
109 | fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector; | |
110 | } | |
d50ba256 | 111 | return 0; |
0b4c0f3f DA |
112 | fail: |
113 | for (i = 0; i < fb_helper->connector_count; i++) { | |
114 | kfree(fb_helper->connector_info[i]); | |
115 | fb_helper->connector_info[i] = NULL; | |
116 | } | |
117 | fb_helper->connector_count = 0; | |
118 | return -ENOMEM; | |
d50ba256 | 119 | } |
0b4c0f3f | 120 | EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors); |
d50ba256 | 121 | |
65c2a89c DA |
122 | int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector) |
123 | { | |
124 | struct drm_fb_helper_connector **temp; | |
125 | struct drm_fb_helper_connector *fb_helper_connector; | |
126 | ||
127 | WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex)); | |
128 | if (fb_helper->connector_count + 1 > fb_helper->connector_info_alloc_count) { | |
129 | temp = krealloc(fb_helper->connector_info, sizeof(struct drm_fb_helper_connector) * (fb_helper->connector_count + 1), GFP_KERNEL); | |
130 | if (!temp) | |
131 | return -ENOMEM; | |
132 | ||
133 | fb_helper->connector_info_alloc_count = fb_helper->connector_count + 1; | |
134 | fb_helper->connector_info = temp; | |
135 | } | |
136 | ||
137 | ||
138 | fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL); | |
139 | if (!fb_helper_connector) | |
140 | return -ENOMEM; | |
141 | ||
142 | fb_helper_connector->connector = connector; | |
143 | fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector; | |
144 | return 0; | |
145 | } | |
146 | EXPORT_SYMBOL(drm_fb_helper_add_one_connector); | |
147 | ||
148 | int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper, | |
149 | struct drm_connector *connector) | |
150 | { | |
151 | struct drm_fb_helper_connector *fb_helper_connector; | |
152 | int i, j; | |
153 | ||
154 | WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex)); | |
155 | ||
156 | for (i = 0; i < fb_helper->connector_count; i++) { | |
157 | if (fb_helper->connector_info[i]->connector == connector) | |
158 | break; | |
159 | } | |
160 | ||
161 | if (i == fb_helper->connector_count) | |
162 | return -EINVAL; | |
163 | fb_helper_connector = fb_helper->connector_info[i]; | |
164 | ||
165 | for (j = i + 1; j < fb_helper->connector_count; j++) { | |
166 | fb_helper->connector_info[j - 1] = fb_helper->connector_info[j]; | |
167 | } | |
168 | fb_helper->connector_count--; | |
169 | kfree(fb_helper_connector); | |
170 | return 0; | |
171 | } | |
172 | EXPORT_SYMBOL(drm_fb_helper_remove_one_connector); | |
173 | ||
0b4c0f3f | 174 | static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper) |
d50ba256 | 175 | { |
0b4c0f3f DA |
176 | struct drm_fb_helper_connector *fb_helper_conn; |
177 | int i; | |
d50ba256 | 178 | |
0b4c0f3f | 179 | for (i = 0; i < fb_helper->connector_count; i++) { |
1794d257 CW |
180 | struct drm_cmdline_mode *mode; |
181 | struct drm_connector *connector; | |
d50ba256 DA |
182 | char *option = NULL; |
183 | ||
0b4c0f3f | 184 | fb_helper_conn = fb_helper->connector_info[i]; |
1794d257 CW |
185 | connector = fb_helper_conn->connector; |
186 | mode = &fb_helper_conn->cmdline_mode; | |
0b4c0f3f | 187 | |
d50ba256 | 188 | /* do something on return - turn off connector maybe */ |
25933820 | 189 | if (fb_get_options(connector->name, &option)) |
d50ba256 DA |
190 | continue; |
191 | ||
1794d257 CW |
192 | if (drm_mode_parse_command_line_for_connector(option, |
193 | connector, | |
194 | mode)) { | |
195 | if (mode->force) { | |
196 | const char *s; | |
197 | switch (mode->force) { | |
6c91083f SK |
198 | case DRM_FORCE_OFF: |
199 | s = "OFF"; | |
200 | break; | |
201 | case DRM_FORCE_ON_DIGITAL: | |
202 | s = "ON - dig"; | |
203 | break; | |
1794d257 | 204 | default: |
6c91083f SK |
205 | case DRM_FORCE_ON: |
206 | s = "ON"; | |
207 | break; | |
1794d257 CW |
208 | } |
209 | ||
210 | DRM_INFO("forcing %s connector %s\n", | |
25933820 | 211 | connector->name, s); |
1794d257 CW |
212 | connector->force = mode->force; |
213 | } | |
214 | ||
215 | DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n", | |
25933820 | 216 | connector->name, |
1794d257 CW |
217 | mode->xres, mode->yres, |
218 | mode->refresh_specified ? mode->refresh : 60, | |
219 | mode->rb ? " reduced blanking" : "", | |
220 | mode->margins ? " with margins" : "", | |
221 | mode->interlace ? " interlaced" : ""); | |
222 | } | |
223 | ||
d50ba256 DA |
224 | } |
225 | return 0; | |
226 | } | |
227 | ||
99231028 JW |
228 | static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper) |
229 | { | |
230 | uint16_t *r_base, *g_base, *b_base; | |
231 | int i; | |
232 | ||
04c0c569 VS |
233 | if (helper->funcs->gamma_get == NULL) |
234 | return; | |
235 | ||
99231028 JW |
236 | r_base = crtc->gamma_store; |
237 | g_base = r_base + crtc->gamma_size; | |
238 | b_base = g_base + crtc->gamma_size; | |
239 | ||
240 | for (i = 0; i < crtc->gamma_size; i++) | |
241 | helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i); | |
242 | } | |
243 | ||
244 | static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc) | |
245 | { | |
246 | uint16_t *r_base, *g_base, *b_base; | |
247 | ||
ebe0f244 LP |
248 | if (crtc->funcs->gamma_set == NULL) |
249 | return; | |
250 | ||
99231028 JW |
251 | r_base = crtc->gamma_store; |
252 | g_base = r_base + crtc->gamma_size; | |
253 | b_base = g_base + crtc->gamma_size; | |
254 | ||
255 | crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size); | |
256 | } | |
257 | ||
207fd329 DV |
258 | /** |
259 | * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter | |
260 | * @info: fbdev registered by the helper | |
261 | */ | |
1a7aba7f JB |
262 | int drm_fb_helper_debug_enter(struct fb_info *info) |
263 | { | |
264 | struct drm_fb_helper *helper = info->par; | |
265 | struct drm_crtc_helper_funcs *funcs; | |
266 | int i; | |
267 | ||
1a7aba7f JB |
268 | list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { |
269 | for (i = 0; i < helper->crtc_count; i++) { | |
270 | struct drm_mode_set *mode_set = | |
271 | &helper->crtc_info[i].mode_set; | |
272 | ||
273 | if (!mode_set->crtc->enabled) | |
274 | continue; | |
275 | ||
276 | funcs = mode_set->crtc->helper_private; | |
99231028 | 277 | drm_fb_helper_save_lut_atomic(mode_set->crtc, helper); |
1a7aba7f JB |
278 | funcs->mode_set_base_atomic(mode_set->crtc, |
279 | mode_set->fb, | |
280 | mode_set->x, | |
413d45d3 | 281 | mode_set->y, |
21c74a8e | 282 | ENTER_ATOMIC_MODE_SET); |
1a7aba7f JB |
283 | } |
284 | } | |
285 | ||
286 | return 0; | |
287 | } | |
288 | EXPORT_SYMBOL(drm_fb_helper_debug_enter); | |
289 | ||
290 | /* Find the real fb for a given fb helper CRTC */ | |
291 | static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc) | |
292 | { | |
293 | struct drm_device *dev = crtc->dev; | |
294 | struct drm_crtc *c; | |
295 | ||
296 | list_for_each_entry(c, &dev->mode_config.crtc_list, head) { | |
297 | if (crtc->base.id == c->base.id) | |
f4510a27 | 298 | return c->primary->fb; |
1a7aba7f JB |
299 | } |
300 | ||
301 | return NULL; | |
302 | } | |
303 | ||
207fd329 DV |
304 | /** |
305 | * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave | |
306 | * @info: fbdev registered by the helper | |
307 | */ | |
1a7aba7f JB |
308 | int drm_fb_helper_debug_leave(struct fb_info *info) |
309 | { | |
310 | struct drm_fb_helper *helper = info->par; | |
311 | struct drm_crtc *crtc; | |
312 | struct drm_crtc_helper_funcs *funcs; | |
313 | struct drm_framebuffer *fb; | |
314 | int i; | |
315 | ||
316 | for (i = 0; i < helper->crtc_count; i++) { | |
317 | struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set; | |
318 | crtc = mode_set->crtc; | |
319 | funcs = crtc->helper_private; | |
320 | fb = drm_mode_config_fb(crtc); | |
321 | ||
322 | if (!crtc->enabled) | |
323 | continue; | |
324 | ||
325 | if (!fb) { | |
326 | DRM_ERROR("no fb to restore??\n"); | |
327 | continue; | |
328 | } | |
329 | ||
99231028 | 330 | drm_fb_helper_restore_lut_atomic(mode_set->crtc); |
1a7aba7f | 331 | funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x, |
21c74a8e | 332 | crtc->y, LEAVE_ATOMIC_MODE_SET); |
1a7aba7f JB |
333 | } |
334 | ||
335 | return 0; | |
336 | } | |
337 | EXPORT_SYMBOL(drm_fb_helper_debug_leave); | |
338 | ||
5ea1f752 | 339 | static bool restore_fbdev_mode(struct drm_fb_helper *fb_helper) |
e8e7a2b8 | 340 | { |
3858bc5d VS |
341 | struct drm_device *dev = fb_helper->dev; |
342 | struct drm_plane *plane; | |
e8e7a2b8 | 343 | bool error = false; |
3858bc5d VS |
344 | int i; |
345 | ||
346 | drm_warn_on_modeset_not_all_locked(dev); | |
6aed8ec3 | 347 | |
9783de20 | 348 | list_for_each_entry(plane, &dev->mode_config.plane_list, head) { |
e27dde3e MR |
349 | if (plane->type != DRM_PLANE_TYPE_PRIMARY) |
350 | drm_plane_force_disable(plane); | |
6aed8ec3 | 351 | |
9783de20 SJ |
352 | if (dev->mode_config.rotation_property) { |
353 | drm_object_property_set_value(&plane->base, | |
354 | dev->mode_config.rotation_property, | |
355 | BIT(DRM_ROTATE_0)); | |
356 | } | |
357 | } | |
358 | ||
e8e7a2b8 DA |
359 | for (i = 0; i < fb_helper->crtc_count; i++) { |
360 | struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set; | |
3858bc5d VS |
361 | struct drm_crtc *crtc = mode_set->crtc; |
362 | int ret; | |
363 | ||
364 | if (crtc->funcs->cursor_set) { | |
365 | ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0); | |
366 | if (ret) | |
367 | error = true; | |
368 | } | |
369 | ||
2d13b679 | 370 | ret = drm_mode_set_config_internal(mode_set); |
e8e7a2b8 DA |
371 | if (ret) |
372 | error = true; | |
373 | } | |
374 | return error; | |
375 | } | |
5ea1f752 RC |
376 | /** |
377 | * drm_fb_helper_restore_fbdev_mode - restore fbdev configuration | |
378 | * @fb_helper: fbcon to restore | |
379 | * | |
380 | * This should be called from driver's drm ->lastclose callback | |
381 | * when implementing an fbcon on top of kms using this helper. This ensures that | |
382 | * the user isn't greeted with a black screen when e.g. X dies. | |
383 | * | |
384 | * Use this variant if you need to bypass locking (panic), or already | |
385 | * hold all modeset locks. Otherwise use drm_fb_helper_restore_fbdev_mode_unlocked() | |
386 | */ | |
387 | static bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper) | |
388 | { | |
389 | return restore_fbdev_mode(fb_helper); | |
390 | } | |
391 | ||
392 | /** | |
393 | * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration | |
394 | * @fb_helper: fbcon to restore | |
395 | * | |
396 | * This should be called from driver's drm ->lastclose callback | |
397 | * when implementing an fbcon on top of kms using this helper. This ensures that | |
398 | * the user isn't greeted with a black screen when e.g. X dies. | |
399 | */ | |
400 | bool drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper) | |
401 | { | |
402 | struct drm_device *dev = fb_helper->dev; | |
403 | bool ret; | |
404 | drm_modeset_lock_all(dev); | |
405 | ret = restore_fbdev_mode(fb_helper); | |
406 | drm_modeset_unlock_all(dev); | |
407 | return ret; | |
408 | } | |
409 | EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked); | |
e8e7a2b8 | 410 | |
d21bf469 DV |
411 | /* |
412 | * restore fbcon display for all kms driver's using this helper, used for sysrq | |
413 | * and panic handling. | |
414 | */ | |
78b9c353 | 415 | static bool drm_fb_helper_force_kernel_mode(void) |
785b93ef | 416 | { |
785b93ef DA |
417 | bool ret, error = false; |
418 | struct drm_fb_helper *helper; | |
419 | ||
420 | if (list_empty(&kernel_fb_helper_list)) | |
421 | return false; | |
422 | ||
423 | list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { | |
b77f0765 TR |
424 | struct drm_device *dev = helper->dev; |
425 | ||
426 | if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) | |
427 | continue; | |
428 | ||
51fd371b RC |
429 | /* NOTE: we use lockless flag below to avoid grabbing other |
430 | * modeset locks. So just trylock the underlying mutex | |
431 | * directly: | |
432 | */ | |
b77f0765 TR |
433 | if (!mutex_trylock(&dev->mode_config.mutex)) { |
434 | error = true; | |
e8e7a2b8 | 435 | continue; |
b77f0765 | 436 | } |
e8e7a2b8 DA |
437 | |
438 | ret = drm_fb_helper_restore_fbdev_mode(helper); | |
439 | if (ret) | |
440 | error = true; | |
b77f0765 TR |
441 | |
442 | mutex_unlock(&dev->mode_config.mutex); | |
785b93ef DA |
443 | } |
444 | return error; | |
445 | } | |
446 | ||
43c8a849 | 447 | static int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed, |
785b93ef DA |
448 | void *panic_str) |
449 | { | |
d68752cf HD |
450 | /* |
451 | * It's a waste of time and effort to switch back to text console | |
452 | * if the kernel should reboot before panic messages can be seen. | |
453 | */ | |
454 | if (panic_timeout < 0) | |
455 | return 0; | |
456 | ||
d56b1b9d | 457 | pr_err("panic occurred, switching back to text console\n"); |
785b93ef | 458 | return drm_fb_helper_force_kernel_mode(); |
785b93ef | 459 | } |
785b93ef DA |
460 | |
461 | static struct notifier_block paniced = { | |
462 | .notifier_call = drm_fb_helper_panic, | |
463 | }; | |
464 | ||
20c60c35 DV |
465 | static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper) |
466 | { | |
467 | struct drm_device *dev = fb_helper->dev; | |
468 | struct drm_crtc *crtc; | |
469 | int bound = 0, crtcs_bound = 0; | |
470 | ||
520edd13 PZ |
471 | /* Sometimes user space wants everything disabled, so don't steal the |
472 | * display if there's a master. */ | |
473 | if (dev->primary->master) | |
474 | return false; | |
475 | ||
20c60c35 | 476 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { |
f4510a27 | 477 | if (crtc->primary->fb) |
20c60c35 | 478 | crtcs_bound++; |
f4510a27 | 479 | if (crtc->primary->fb == fb_helper->fb) |
20c60c35 DV |
480 | bound++; |
481 | } | |
482 | ||
483 | if (bound < crtcs_bound) | |
484 | return false; | |
520edd13 | 485 | |
20c60c35 DV |
486 | return true; |
487 | } | |
488 | ||
bea1d35b | 489 | #ifdef CONFIG_MAGIC_SYSRQ |
785b93ef DA |
490 | static void drm_fb_helper_restore_work_fn(struct work_struct *ignored) |
491 | { | |
d21bf469 DV |
492 | bool ret; |
493 | ret = drm_fb_helper_force_kernel_mode(); | |
494 | if (ret == true) | |
495 | DRM_ERROR("Failed to restore crtc configuration\n"); | |
785b93ef DA |
496 | } |
497 | static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn); | |
498 | ||
1495cc9d | 499 | static void drm_fb_helper_sysrq(int dummy1) |
785b93ef DA |
500 | { |
501 | schedule_work(&drm_fb_helper_restore_work); | |
502 | } | |
503 | ||
504 | static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { | |
505 | .handler = drm_fb_helper_sysrq, | |
506 | .help_msg = "force-fb(V)", | |
507 | .action_msg = "Restore framebuffer console", | |
508 | }; | |
b8c40d62 RD |
509 | #else |
510 | static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { }; | |
bea1d35b | 511 | #endif |
785b93ef | 512 | |
3a8148c5 | 513 | static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode) |
785b93ef DA |
514 | { |
515 | struct drm_fb_helper *fb_helper = info->par; | |
516 | struct drm_device *dev = fb_helper->dev; | |
517 | struct drm_crtc *crtc; | |
023eb571 | 518 | struct drm_connector *connector; |
023eb571 | 519 | int i, j; |
785b93ef | 520 | |
928c2f0c DV |
521 | /* |
522 | * fbdev->blank can be called from irq context in case of a panic. | |
1b1d5397 DV |
523 | * Since we already have our own special panic handler which will |
524 | * restore the fbdev console mode completely, just bail out early. | |
525 | */ | |
526 | if (oops_in_progress) | |
527 | return; | |
528 | ||
785b93ef | 529 | /* |
3a8148c5 | 530 | * For each CRTC in this fb, turn the connectors on/off. |
785b93ef | 531 | */ |
84849903 | 532 | drm_modeset_lock_all(dev); |
20c60c35 DV |
533 | if (!drm_fb_helper_is_bound(fb_helper)) { |
534 | drm_modeset_unlock_all(dev); | |
535 | return; | |
536 | } | |
537 | ||
e87b2c42 | 538 | for (i = 0; i < fb_helper->crtc_count; i++) { |
8be48d92 | 539 | crtc = fb_helper->crtc_info[i].mode_set.crtc; |
785b93ef | 540 | |
8be48d92 DA |
541 | if (!crtc->enabled) |
542 | continue; | |
543 | ||
3a8148c5 | 544 | /* Walk the connectors & encoders on this fb turning them on/off */ |
023eb571 JB |
545 | for (j = 0; j < fb_helper->connector_count; j++) { |
546 | connector = fb_helper->connector_info[j]->connector; | |
e04190e0 | 547 | connector->funcs->dpms(connector, dpms_mode); |
58495563 | 548 | drm_object_property_set_value(&connector->base, |
3a8148c5 | 549 | dev->mode_config.dpms_property, dpms_mode); |
785b93ef | 550 | } |
785b93ef | 551 | } |
84849903 | 552 | drm_modeset_unlock_all(dev); |
785b93ef DA |
553 | } |
554 | ||
207fd329 DV |
555 | /** |
556 | * drm_fb_helper_blank - implementation for ->fb_blank | |
557 | * @blank: desired blanking state | |
558 | * @info: fbdev registered by the helper | |
559 | */ | |
785b93ef DA |
560 | int drm_fb_helper_blank(int blank, struct fb_info *info) |
561 | { | |
562 | switch (blank) { | |
731b5a15 | 563 | /* Display: On; HSync: On, VSync: On */ |
785b93ef | 564 | case FB_BLANK_UNBLANK: |
3a8148c5 | 565 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON); |
785b93ef | 566 | break; |
731b5a15 | 567 | /* Display: Off; HSync: On, VSync: On */ |
785b93ef | 568 | case FB_BLANK_NORMAL: |
3a8148c5 | 569 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); |
785b93ef | 570 | break; |
731b5a15 | 571 | /* Display: Off; HSync: Off, VSync: On */ |
785b93ef | 572 | case FB_BLANK_HSYNC_SUSPEND: |
3a8148c5 | 573 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); |
785b93ef | 574 | break; |
731b5a15 | 575 | /* Display: Off; HSync: On, VSync: Off */ |
785b93ef | 576 | case FB_BLANK_VSYNC_SUSPEND: |
3a8148c5 | 577 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND); |
785b93ef | 578 | break; |
731b5a15 | 579 | /* Display: Off; HSync: Off, VSync: Off */ |
785b93ef | 580 | case FB_BLANK_POWERDOWN: |
3a8148c5 | 581 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF); |
785b93ef DA |
582 | break; |
583 | } | |
584 | return 0; | |
585 | } | |
586 | EXPORT_SYMBOL(drm_fb_helper_blank); | |
587 | ||
588 | static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper) | |
589 | { | |
590 | int i; | |
591 | ||
0b4c0f3f DA |
592 | for (i = 0; i < helper->connector_count; i++) |
593 | kfree(helper->connector_info[i]); | |
594 | kfree(helper->connector_info); | |
a1b7736d | 595 | for (i = 0; i < helper->crtc_count; i++) { |
785b93ef | 596 | kfree(helper->crtc_info[i].mode_set.connectors); |
a1b7736d SH |
597 | if (helper->crtc_info[i].mode_set.mode) |
598 | drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode); | |
599 | } | |
785b93ef DA |
600 | kfree(helper->crtc_info); |
601 | } | |
602 | ||
10a23102 TR |
603 | /** |
604 | * drm_fb_helper_prepare - setup a drm_fb_helper structure | |
605 | * @dev: DRM device | |
606 | * @helper: driver-allocated fbdev helper structure to set up | |
607 | * @funcs: pointer to structure of functions associate with this helper | |
608 | * | |
609 | * Sets up the bare minimum to make the framebuffer helper usable. This is | |
610 | * useful to implement race-free initialization of the polling helpers. | |
611 | */ | |
612 | void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper, | |
613 | const struct drm_fb_helper_funcs *funcs) | |
614 | { | |
615 | INIT_LIST_HEAD(&helper->kernel_fb_list); | |
616 | helper->funcs = funcs; | |
617 | helper->dev = dev; | |
618 | } | |
619 | EXPORT_SYMBOL(drm_fb_helper_prepare); | |
620 | ||
207fd329 DV |
621 | /** |
622 | * drm_fb_helper_init - initialize a drm_fb_helper structure | |
623 | * @dev: drm device | |
624 | * @fb_helper: driver-allocated fbdev helper structure to initialize | |
625 | * @crtc_count: maximum number of crtcs to support in this fbdev emulation | |
626 | * @max_conn_count: max connector count | |
627 | * | |
628 | * This allocates the structures for the fbdev helper with the given limits. | |
629 | * Note that this won't yet touch the hardware (through the driver interfaces) | |
630 | * nor register the fbdev. This is only done in drm_fb_helper_initial_config() | |
631 | * to allow driver writes more control over the exact init sequence. | |
632 | * | |
10a23102 | 633 | * Drivers must call drm_fb_helper_prepare() before calling this function. |
207fd329 DV |
634 | * |
635 | * RETURNS: | |
636 | * Zero if everything went ok, nonzero otherwise. | |
637 | */ | |
4abe3520 DA |
638 | int drm_fb_helper_init(struct drm_device *dev, |
639 | struct drm_fb_helper *fb_helper, | |
eb1f8e4f | 640 | int crtc_count, int max_conn_count) |
785b93ef | 641 | { |
785b93ef | 642 | struct drm_crtc *crtc; |
785b93ef DA |
643 | int i; |
644 | ||
04cfe97e XL |
645 | if (!max_conn_count) |
646 | return -EINVAL; | |
647 | ||
4abe3520 DA |
648 | fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL); |
649 | if (!fb_helper->crtc_info) | |
785b93ef DA |
650 | return -ENOMEM; |
651 | ||
4abe3520 DA |
652 | fb_helper->crtc_count = crtc_count; |
653 | fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL); | |
654 | if (!fb_helper->connector_info) { | |
655 | kfree(fb_helper->crtc_info); | |
0b4c0f3f DA |
656 | return -ENOMEM; |
657 | } | |
65c2a89c | 658 | fb_helper->connector_info_alloc_count = dev->mode_config.num_connector; |
4abe3520 | 659 | fb_helper->connector_count = 0; |
785b93ef DA |
660 | |
661 | for (i = 0; i < crtc_count; i++) { | |
4abe3520 | 662 | fb_helper->crtc_info[i].mode_set.connectors = |
785b93ef DA |
663 | kcalloc(max_conn_count, |
664 | sizeof(struct drm_connector *), | |
665 | GFP_KERNEL); | |
666 | ||
4a1b0714 | 667 | if (!fb_helper->crtc_info[i].mode_set.connectors) |
785b93ef | 668 | goto out_free; |
4abe3520 | 669 | fb_helper->crtc_info[i].mode_set.num_connectors = 0; |
785b93ef DA |
670 | } |
671 | ||
672 | i = 0; | |
673 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { | |
4abe3520 | 674 | fb_helper->crtc_info[i].mode_set.crtc = crtc; |
785b93ef DA |
675 | i++; |
676 | } | |
e9ad3181 | 677 | |
785b93ef DA |
678 | return 0; |
679 | out_free: | |
4abe3520 | 680 | drm_fb_helper_crtc_free(fb_helper); |
785b93ef DA |
681 | return -ENOMEM; |
682 | } | |
4abe3520 DA |
683 | EXPORT_SYMBOL(drm_fb_helper_init); |
684 | ||
685 | void drm_fb_helper_fini(struct drm_fb_helper *fb_helper) | |
686 | { | |
687 | if (!list_empty(&fb_helper->kernel_fb_list)) { | |
688 | list_del(&fb_helper->kernel_fb_list); | |
689 | if (list_empty(&kernel_fb_helper_list)) { | |
d56b1b9d | 690 | pr_info("drm: unregistered panic notifier\n"); |
4abe3520 DA |
691 | atomic_notifier_chain_unregister(&panic_notifier_list, |
692 | &paniced); | |
693 | unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); | |
694 | } | |
695 | } | |
696 | ||
697 | drm_fb_helper_crtc_free(fb_helper); | |
698 | ||
4abe3520 DA |
699 | } |
700 | EXPORT_SYMBOL(drm_fb_helper_fini); | |
785b93ef | 701 | |
c850cb78 | 702 | static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green, |
b8c00ac5 DA |
703 | u16 blue, u16 regno, struct fb_info *info) |
704 | { | |
705 | struct drm_fb_helper *fb_helper = info->par; | |
706 | struct drm_framebuffer *fb = fb_helper->fb; | |
707 | int pindex; | |
708 | ||
c850cb78 DA |
709 | if (info->fix.visual == FB_VISUAL_TRUECOLOR) { |
710 | u32 *palette; | |
711 | u32 value; | |
712 | /* place color in psuedopalette */ | |
713 | if (regno > 16) | |
714 | return -EINVAL; | |
715 | palette = (u32 *)info->pseudo_palette; | |
716 | red >>= (16 - info->var.red.length); | |
717 | green >>= (16 - info->var.green.length); | |
718 | blue >>= (16 - info->var.blue.length); | |
719 | value = (red << info->var.red.offset) | | |
720 | (green << info->var.green.offset) | | |
721 | (blue << info->var.blue.offset); | |
9da12b6a RC |
722 | if (info->var.transp.length > 0) { |
723 | u32 mask = (1 << info->var.transp.length) - 1; | |
724 | mask <<= info->var.transp.offset; | |
725 | value |= mask; | |
726 | } | |
c850cb78 DA |
727 | palette[regno] = value; |
728 | return 0; | |
729 | } | |
730 | ||
04c0c569 VS |
731 | /* |
732 | * The driver really shouldn't advertise pseudo/directcolor | |
733 | * visuals if it can't deal with the palette. | |
734 | */ | |
735 | if (WARN_ON(!fb_helper->funcs->gamma_set || | |
736 | !fb_helper->funcs->gamma_get)) | |
737 | return -EINVAL; | |
738 | ||
b8c00ac5 DA |
739 | pindex = regno; |
740 | ||
741 | if (fb->bits_per_pixel == 16) { | |
742 | pindex = regno << 3; | |
743 | ||
744 | if (fb->depth == 16 && regno > 63) | |
c850cb78 | 745 | return -EINVAL; |
b8c00ac5 | 746 | if (fb->depth == 15 && regno > 31) |
c850cb78 | 747 | return -EINVAL; |
b8c00ac5 DA |
748 | |
749 | if (fb->depth == 16) { | |
750 | u16 r, g, b; | |
751 | int i; | |
752 | if (regno < 32) { | |
753 | for (i = 0; i < 8; i++) | |
754 | fb_helper->funcs->gamma_set(crtc, red, | |
755 | green, blue, pindex + i); | |
756 | } | |
757 | ||
758 | fb_helper->funcs->gamma_get(crtc, &r, | |
759 | &g, &b, | |
760 | pindex >> 1); | |
761 | ||
762 | for (i = 0; i < 4; i++) | |
763 | fb_helper->funcs->gamma_set(crtc, r, | |
764 | green, b, | |
765 | (pindex >> 1) + i); | |
766 | } | |
767 | } | |
768 | ||
769 | if (fb->depth != 16) | |
770 | fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex); | |
c850cb78 | 771 | return 0; |
b8c00ac5 DA |
772 | } |
773 | ||
207fd329 DV |
774 | /** |
775 | * drm_fb_helper_setcmap - implementation for ->fb_setcmap | |
776 | * @cmap: cmap to set | |
777 | * @info: fbdev registered by the helper | |
778 | */ | |
068143d3 DA |
779 | int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info) |
780 | { | |
781 | struct drm_fb_helper *fb_helper = info->par; | |
8391a3d5 | 782 | struct drm_device *dev = fb_helper->dev; |
8be48d92 | 783 | struct drm_crtc_helper_funcs *crtc_funcs; |
068143d3 DA |
784 | u16 *red, *green, *blue, *transp; |
785 | struct drm_crtc *crtc; | |
062ac622 | 786 | int i, j, rc = 0; |
068143d3 DA |
787 | int start; |
788 | ||
8391a3d5 VS |
789 | drm_modeset_lock_all(dev); |
790 | if (!drm_fb_helper_is_bound(fb_helper)) { | |
791 | drm_modeset_unlock_all(dev); | |
792 | return -EBUSY; | |
793 | } | |
794 | ||
8be48d92 DA |
795 | for (i = 0; i < fb_helper->crtc_count; i++) { |
796 | crtc = fb_helper->crtc_info[i].mode_set.crtc; | |
797 | crtc_funcs = crtc->helper_private; | |
068143d3 DA |
798 | |
799 | red = cmap->red; | |
800 | green = cmap->green; | |
801 | blue = cmap->blue; | |
802 | transp = cmap->transp; | |
803 | start = cmap->start; | |
804 | ||
062ac622 | 805 | for (j = 0; j < cmap->len; j++) { |
068143d3 DA |
806 | u16 hred, hgreen, hblue, htransp = 0xffff; |
807 | ||
808 | hred = *red++; | |
809 | hgreen = *green++; | |
810 | hblue = *blue++; | |
811 | ||
812 | if (transp) | |
813 | htransp = *transp++; | |
814 | ||
c850cb78 DA |
815 | rc = setcolreg(crtc, hred, hgreen, hblue, start++, info); |
816 | if (rc) | |
8391a3d5 | 817 | goto out; |
068143d3 | 818 | } |
04c0c569 VS |
819 | if (crtc_funcs->load_lut) |
820 | crtc_funcs->load_lut(crtc); | |
068143d3 | 821 | } |
8391a3d5 VS |
822 | out: |
823 | drm_modeset_unlock_all(dev); | |
068143d3 DA |
824 | return rc; |
825 | } | |
826 | EXPORT_SYMBOL(drm_fb_helper_setcmap); | |
827 | ||
207fd329 DV |
828 | /** |
829 | * drm_fb_helper_check_var - implementation for ->fb_check_var | |
830 | * @var: screeninfo to check | |
831 | * @info: fbdev registered by the helper | |
832 | */ | |
785b93ef DA |
833 | int drm_fb_helper_check_var(struct fb_var_screeninfo *var, |
834 | struct fb_info *info) | |
835 | { | |
836 | struct drm_fb_helper *fb_helper = info->par; | |
837 | struct drm_framebuffer *fb = fb_helper->fb; | |
838 | int depth; | |
839 | ||
f90ebd9e | 840 | if (var->pixclock != 0 || in_dbg_master()) |
785b93ef DA |
841 | return -EINVAL; |
842 | ||
843 | /* Need to resize the fb object !!! */ | |
62fb376e CW |
844 | if (var->bits_per_pixel > fb->bits_per_pixel || |
845 | var->xres > fb->width || var->yres > fb->height || | |
846 | var->xres_virtual > fb->width || var->yres_virtual > fb->height) { | |
509c7d83 | 847 | DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb " |
62fb376e CW |
848 | "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n", |
849 | var->xres, var->yres, var->bits_per_pixel, | |
850 | var->xres_virtual, var->yres_virtual, | |
509c7d83 | 851 | fb->width, fb->height, fb->bits_per_pixel); |
785b93ef DA |
852 | return -EINVAL; |
853 | } | |
854 | ||
855 | switch (var->bits_per_pixel) { | |
856 | case 16: | |
857 | depth = (var->green.length == 6) ? 16 : 15; | |
858 | break; | |
859 | case 32: | |
860 | depth = (var->transp.length > 0) ? 32 : 24; | |
861 | break; | |
862 | default: | |
863 | depth = var->bits_per_pixel; | |
864 | break; | |
865 | } | |
866 | ||
867 | switch (depth) { | |
868 | case 8: | |
869 | var->red.offset = 0; | |
870 | var->green.offset = 0; | |
871 | var->blue.offset = 0; | |
872 | var->red.length = 8; | |
873 | var->green.length = 8; | |
874 | var->blue.length = 8; | |
875 | var->transp.length = 0; | |
876 | var->transp.offset = 0; | |
877 | break; | |
878 | case 15: | |
879 | var->red.offset = 10; | |
880 | var->green.offset = 5; | |
881 | var->blue.offset = 0; | |
882 | var->red.length = 5; | |
883 | var->green.length = 5; | |
884 | var->blue.length = 5; | |
885 | var->transp.length = 1; | |
886 | var->transp.offset = 15; | |
887 | break; | |
888 | case 16: | |
889 | var->red.offset = 11; | |
890 | var->green.offset = 5; | |
891 | var->blue.offset = 0; | |
892 | var->red.length = 5; | |
893 | var->green.length = 6; | |
894 | var->blue.length = 5; | |
895 | var->transp.length = 0; | |
896 | var->transp.offset = 0; | |
897 | break; | |
898 | case 24: | |
899 | var->red.offset = 16; | |
900 | var->green.offset = 8; | |
901 | var->blue.offset = 0; | |
902 | var->red.length = 8; | |
903 | var->green.length = 8; | |
904 | var->blue.length = 8; | |
905 | var->transp.length = 0; | |
906 | var->transp.offset = 0; | |
907 | break; | |
908 | case 32: | |
909 | var->red.offset = 16; | |
910 | var->green.offset = 8; | |
911 | var->blue.offset = 0; | |
912 | var->red.length = 8; | |
913 | var->green.length = 8; | |
914 | var->blue.length = 8; | |
915 | var->transp.length = 8; | |
916 | var->transp.offset = 24; | |
917 | break; | |
918 | default: | |
919 | return -EINVAL; | |
920 | } | |
921 | return 0; | |
922 | } | |
923 | EXPORT_SYMBOL(drm_fb_helper_check_var); | |
924 | ||
207fd329 DV |
925 | /** |
926 | * drm_fb_helper_set_par - implementation for ->fb_set_par | |
927 | * @info: fbdev registered by the helper | |
928 | * | |
929 | * This will let fbcon do the mode init and is called at initialization time by | |
930 | * the fbdev core when registering the driver, and later on through the hotplug | |
931 | * callback. | |
932 | */ | |
785b93ef DA |
933 | int drm_fb_helper_set_par(struct fb_info *info) |
934 | { | |
935 | struct drm_fb_helper *fb_helper = info->par; | |
785b93ef | 936 | struct fb_var_screeninfo *var = &info->var; |
785b93ef | 937 | |
5349ef31 | 938 | if (var->pixclock != 0) { |
172e91f5 | 939 | DRM_ERROR("PIXEL CLOCK SET\n"); |
785b93ef DA |
940 | return -EINVAL; |
941 | } | |
942 | ||
5ea1f752 | 943 | drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper); |
4abe3520 DA |
944 | |
945 | if (fb_helper->delayed_hotplug) { | |
946 | fb_helper->delayed_hotplug = false; | |
eb1f8e4f | 947 | drm_fb_helper_hotplug_event(fb_helper); |
4abe3520 | 948 | } |
785b93ef DA |
949 | return 0; |
950 | } | |
951 | EXPORT_SYMBOL(drm_fb_helper_set_par); | |
952 | ||
207fd329 DV |
953 | /** |
954 | * drm_fb_helper_pan_display - implementation for ->fb_pan_display | |
955 | * @var: updated screen information | |
956 | * @info: fbdev registered by the helper | |
957 | */ | |
785b93ef DA |
958 | int drm_fb_helper_pan_display(struct fb_var_screeninfo *var, |
959 | struct fb_info *info) | |
960 | { | |
961 | struct drm_fb_helper *fb_helper = info->par; | |
962 | struct drm_device *dev = fb_helper->dev; | |
963 | struct drm_mode_set *modeset; | |
785b93ef DA |
964 | int ret = 0; |
965 | int i; | |
966 | ||
84849903 | 967 | drm_modeset_lock_all(dev); |
20c60c35 DV |
968 | if (!drm_fb_helper_is_bound(fb_helper)) { |
969 | drm_modeset_unlock_all(dev); | |
970 | return -EBUSY; | |
971 | } | |
972 | ||
8be48d92 | 973 | for (i = 0; i < fb_helper->crtc_count; i++) { |
785b93ef DA |
974 | modeset = &fb_helper->crtc_info[i].mode_set; |
975 | ||
976 | modeset->x = var->xoffset; | |
977 | modeset->y = var->yoffset; | |
978 | ||
979 | if (modeset->num_connectors) { | |
2d13b679 | 980 | ret = drm_mode_set_config_internal(modeset); |
785b93ef DA |
981 | if (!ret) { |
982 | info->var.xoffset = var->xoffset; | |
983 | info->var.yoffset = var->yoffset; | |
984 | } | |
985 | } | |
986 | } | |
84849903 | 987 | drm_modeset_unlock_all(dev); |
785b93ef DA |
988 | return ret; |
989 | } | |
990 | EXPORT_SYMBOL(drm_fb_helper_pan_display); | |
991 | ||
8acf658a | 992 | /* |
207fd329 DV |
993 | * Allocates the backing storage and sets up the fbdev info structure through |
994 | * the ->fb_probe callback and then registers the fbdev and sets up the panic | |
995 | * notifier. | |
8acf658a | 996 | */ |
de1ace5b DV |
997 | static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper, |
998 | int preferred_bpp) | |
785b93ef | 999 | { |
8acf658a | 1000 | int ret = 0; |
785b93ef | 1001 | int crtc_count = 0; |
4abe3520 | 1002 | int i; |
785b93ef | 1003 | struct fb_info *info; |
38651674 | 1004 | struct drm_fb_helper_surface_size sizes; |
8be48d92 | 1005 | int gamma_size = 0; |
38651674 DA |
1006 | |
1007 | memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size)); | |
1008 | sizes.surface_depth = 24; | |
1009 | sizes.surface_bpp = 32; | |
1010 | sizes.fb_width = (unsigned)-1; | |
1011 | sizes.fb_height = (unsigned)-1; | |
785b93ef | 1012 | |
b8c00ac5 DA |
1013 | /* if driver picks 8 or 16 by default use that |
1014 | for both depth/bpp */ | |
96081cdf | 1015 | if (preferred_bpp != sizes.surface_bpp) |
38651674 | 1016 | sizes.surface_depth = sizes.surface_bpp = preferred_bpp; |
96081cdf | 1017 | |
785b93ef | 1018 | /* first up get a count of crtcs now in use and new min/maxes width/heights */ |
0b4c0f3f DA |
1019 | for (i = 0; i < fb_helper->connector_count; i++) { |
1020 | struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i]; | |
1794d257 | 1021 | struct drm_cmdline_mode *cmdline_mode; |
8ef8678c | 1022 | |
0b4c0f3f | 1023 | cmdline_mode = &fb_helper_conn->cmdline_mode; |
d50ba256 DA |
1024 | |
1025 | if (cmdline_mode->bpp_specified) { | |
1026 | switch (cmdline_mode->bpp) { | |
1027 | case 8: | |
38651674 | 1028 | sizes.surface_depth = sizes.surface_bpp = 8; |
d50ba256 DA |
1029 | break; |
1030 | case 15: | |
38651674 DA |
1031 | sizes.surface_depth = 15; |
1032 | sizes.surface_bpp = 16; | |
d50ba256 DA |
1033 | break; |
1034 | case 16: | |
38651674 | 1035 | sizes.surface_depth = sizes.surface_bpp = 16; |
d50ba256 DA |
1036 | break; |
1037 | case 24: | |
38651674 | 1038 | sizes.surface_depth = sizes.surface_bpp = 24; |
d50ba256 DA |
1039 | break; |
1040 | case 32: | |
38651674 DA |
1041 | sizes.surface_depth = 24; |
1042 | sizes.surface_bpp = 32; | |
d50ba256 DA |
1043 | break; |
1044 | } | |
1045 | break; | |
1046 | } | |
1047 | } | |
1048 | ||
8be48d92 DA |
1049 | crtc_count = 0; |
1050 | for (i = 0; i < fb_helper->crtc_count; i++) { | |
1051 | struct drm_display_mode *desired_mode; | |
1052 | desired_mode = fb_helper->crtc_info[i].desired_mode; | |
1053 | ||
1054 | if (desired_mode) { | |
1055 | if (gamma_size == 0) | |
1056 | gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size; | |
1057 | if (desired_mode->hdisplay < sizes.fb_width) | |
1058 | sizes.fb_width = desired_mode->hdisplay; | |
1059 | if (desired_mode->vdisplay < sizes.fb_height) | |
1060 | sizes.fb_height = desired_mode->vdisplay; | |
1061 | if (desired_mode->hdisplay > sizes.surface_width) | |
1062 | sizes.surface_width = desired_mode->hdisplay; | |
1063 | if (desired_mode->vdisplay > sizes.surface_height) | |
1064 | sizes.surface_height = desired_mode->vdisplay; | |
785b93ef DA |
1065 | crtc_count++; |
1066 | } | |
1067 | } | |
1068 | ||
38651674 | 1069 | if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) { |
785b93ef DA |
1070 | /* hmm everyone went away - assume VGA cable just fell out |
1071 | and will come back later. */ | |
eb1f8e4f | 1072 | DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n"); |
19b4b445 DA |
1073 | sizes.fb_width = sizes.surface_width = 1024; |
1074 | sizes.fb_height = sizes.surface_height = 768; | |
785b93ef DA |
1075 | } |
1076 | ||
38651674 | 1077 | /* push down into drivers */ |
8acf658a DV |
1078 | ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes); |
1079 | if (ret < 0) | |
1080 | return ret; | |
785b93ef | 1081 | |
38651674 | 1082 | info = fb_helper->fbdev; |
785b93ef | 1083 | |
7e53f3a4 DV |
1084 | /* |
1085 | * Set the fb pointer - usually drm_setup_crtcs does this for hotplug | |
1086 | * events, but at init time drm_setup_crtcs needs to be called before | |
1087 | * the fb is allocated (since we need to figure out the desired size of | |
1088 | * the fb before we can allocate it ...). Hence we need to fix things up | |
1089 | * here again. | |
1090 | */ | |
96081cdf | 1091 | for (i = 0; i < fb_helper->crtc_count; i++) |
7e53f3a4 DV |
1092 | if (fb_helper->crtc_info[i].mode_set.num_connectors) |
1093 | fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb; | |
1094 | ||
785b93ef | 1095 | |
8acf658a DV |
1096 | info->var.pixclock = 0; |
1097 | if (register_framebuffer(info) < 0) | |
1098 | return -EINVAL; | |
38651674 | 1099 | |
8acf658a DV |
1100 | dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n", |
1101 | info->node, info->fix.id); | |
785b93ef DA |
1102 | |
1103 | /* Switch back to kernel console on panic */ | |
1104 | /* multi card linked list maybe */ | |
1105 | if (list_empty(&kernel_fb_helper_list)) { | |
d56b1b9d | 1106 | dev_info(fb_helper->dev->dev, "registered panic notifier\n"); |
785b93ef DA |
1107 | atomic_notifier_chain_register(&panic_notifier_list, |
1108 | &paniced); | |
1109 | register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); | |
1110 | } | |
8acf658a DV |
1111 | |
1112 | list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list); | |
38651674 | 1113 | |
785b93ef DA |
1114 | return 0; |
1115 | } | |
785b93ef | 1116 | |
207fd329 DV |
1117 | /** |
1118 | * drm_fb_helper_fill_fix - initializes fixed fbdev information | |
1119 | * @info: fbdev registered by the helper | |
1120 | * @pitch: desired pitch | |
1121 | * @depth: desired depth | |
1122 | * | |
1123 | * Helper to fill in the fixed fbdev information useful for a non-accelerated | |
1124 | * fbdev emulations. Drivers which support acceleration methods which impose | |
1125 | * additional constraints need to set up their own limits. | |
1126 | * | |
1127 | * Drivers should call this (or their equivalent setup code) from their | |
1128 | * ->fb_probe callback. | |
1129 | */ | |
3632ef89 DA |
1130 | void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch, |
1131 | uint32_t depth) | |
1132 | { | |
1133 | info->fix.type = FB_TYPE_PACKED_PIXELS; | |
1134 | info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR : | |
1135 | FB_VISUAL_TRUECOLOR; | |
1136 | info->fix.mmio_start = 0; | |
1137 | info->fix.mmio_len = 0; | |
1138 | info->fix.type_aux = 0; | |
1139 | info->fix.xpanstep = 1; /* doing it in hw */ | |
1140 | info->fix.ypanstep = 1; /* doing it in hw */ | |
1141 | info->fix.ywrapstep = 0; | |
1142 | info->fix.accel = FB_ACCEL_NONE; | |
3632ef89 DA |
1143 | |
1144 | info->fix.line_length = pitch; | |
1145 | return; | |
1146 | } | |
1147 | EXPORT_SYMBOL(drm_fb_helper_fill_fix); | |
1148 | ||
207fd329 DV |
1149 | /** |
1150 | * drm_fb_helper_fill_var - initalizes variable fbdev information | |
1151 | * @info: fbdev instance to set up | |
1152 | * @fb_helper: fb helper instance to use as template | |
1153 | * @fb_width: desired fb width | |
1154 | * @fb_height: desired fb height | |
1155 | * | |
1156 | * Sets up the variable fbdev metainformation from the given fb helper instance | |
1157 | * and the drm framebuffer allocated in fb_helper->fb. | |
1158 | * | |
1159 | * Drivers should call this (or their equivalent setup code) from their | |
1160 | * ->fb_probe callback after having allocated the fbdev backing | |
1161 | * storage framebuffer. | |
1162 | */ | |
38651674 | 1163 | void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper, |
785b93ef DA |
1164 | uint32_t fb_width, uint32_t fb_height) |
1165 | { | |
38651674 DA |
1166 | struct drm_framebuffer *fb = fb_helper->fb; |
1167 | info->pseudo_palette = fb_helper->pseudo_palette; | |
785b93ef DA |
1168 | info->var.xres_virtual = fb->width; |
1169 | info->var.yres_virtual = fb->height; | |
1170 | info->var.bits_per_pixel = fb->bits_per_pixel; | |
57084d05 | 1171 | info->var.accel_flags = FB_ACCELF_TEXT; |
785b93ef DA |
1172 | info->var.xoffset = 0; |
1173 | info->var.yoffset = 0; | |
1174 | info->var.activate = FB_ACTIVATE_NOW; | |
1175 | info->var.height = -1; | |
1176 | info->var.width = -1; | |
1177 | ||
1178 | switch (fb->depth) { | |
1179 | case 8: | |
1180 | info->var.red.offset = 0; | |
1181 | info->var.green.offset = 0; | |
1182 | info->var.blue.offset = 0; | |
1183 | info->var.red.length = 8; /* 8bit DAC */ | |
1184 | info->var.green.length = 8; | |
1185 | info->var.blue.length = 8; | |
1186 | info->var.transp.offset = 0; | |
1187 | info->var.transp.length = 0; | |
1188 | break; | |
1189 | case 15: | |
1190 | info->var.red.offset = 10; | |
1191 | info->var.green.offset = 5; | |
1192 | info->var.blue.offset = 0; | |
1193 | info->var.red.length = 5; | |
1194 | info->var.green.length = 5; | |
1195 | info->var.blue.length = 5; | |
1196 | info->var.transp.offset = 15; | |
1197 | info->var.transp.length = 1; | |
1198 | break; | |
1199 | case 16: | |
1200 | info->var.red.offset = 11; | |
1201 | info->var.green.offset = 5; | |
1202 | info->var.blue.offset = 0; | |
1203 | info->var.red.length = 5; | |
1204 | info->var.green.length = 6; | |
1205 | info->var.blue.length = 5; | |
1206 | info->var.transp.offset = 0; | |
1207 | break; | |
1208 | case 24: | |
1209 | info->var.red.offset = 16; | |
1210 | info->var.green.offset = 8; | |
1211 | info->var.blue.offset = 0; | |
1212 | info->var.red.length = 8; | |
1213 | info->var.green.length = 8; | |
1214 | info->var.blue.length = 8; | |
1215 | info->var.transp.offset = 0; | |
1216 | info->var.transp.length = 0; | |
1217 | break; | |
1218 | case 32: | |
1219 | info->var.red.offset = 16; | |
1220 | info->var.green.offset = 8; | |
1221 | info->var.blue.offset = 0; | |
1222 | info->var.red.length = 8; | |
1223 | info->var.green.length = 8; | |
1224 | info->var.blue.length = 8; | |
1225 | info->var.transp.offset = 24; | |
1226 | info->var.transp.length = 8; | |
1227 | break; | |
1228 | default: | |
1229 | break; | |
1230 | } | |
1231 | ||
1232 | info->var.xres = fb_width; | |
1233 | info->var.yres = fb_height; | |
1234 | } | |
1235 | EXPORT_SYMBOL(drm_fb_helper_fill_var); | |
38651674 | 1236 | |
0b4c0f3f DA |
1237 | static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper, |
1238 | uint32_t maxX, | |
1239 | uint32_t maxY) | |
38651674 DA |
1240 | { |
1241 | struct drm_connector *connector; | |
1242 | int count = 0; | |
0b4c0f3f | 1243 | int i; |
38651674 | 1244 | |
0b4c0f3f DA |
1245 | for (i = 0; i < fb_helper->connector_count; i++) { |
1246 | connector = fb_helper->connector_info[i]->connector; | |
38651674 DA |
1247 | count += connector->funcs->fill_modes(connector, maxX, maxY); |
1248 | } | |
1249 | ||
1250 | return count; | |
1251 | } | |
1252 | ||
2f1046f3 | 1253 | struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height) |
38651674 DA |
1254 | { |
1255 | struct drm_display_mode *mode; | |
1256 | ||
0b4c0f3f | 1257 | list_for_each_entry(mode, &fb_connector->connector->modes, head) { |
9d3de138 DV |
1258 | if (mode->hdisplay > width || |
1259 | mode->vdisplay > height) | |
38651674 DA |
1260 | continue; |
1261 | if (mode->type & DRM_MODE_TYPE_PREFERRED) | |
1262 | return mode; | |
1263 | } | |
1264 | return NULL; | |
1265 | } | |
2f1046f3 | 1266 | EXPORT_SYMBOL(drm_has_preferred_mode); |
38651674 | 1267 | |
0b4c0f3f | 1268 | static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector) |
38651674 | 1269 | { |
1794d257 | 1270 | struct drm_cmdline_mode *cmdline_mode; |
0b4c0f3f | 1271 | cmdline_mode = &fb_connector->cmdline_mode; |
38651674 DA |
1272 | return cmdline_mode->specified; |
1273 | } | |
1274 | ||
2f1046f3 | 1275 | struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn, |
0b4c0f3f | 1276 | int width, int height) |
38651674 | 1277 | { |
1794d257 | 1278 | struct drm_cmdline_mode *cmdline_mode; |
38651674 | 1279 | struct drm_display_mode *mode = NULL; |
c683f427 | 1280 | bool prefer_non_interlace; |
38651674 | 1281 | |
0b4c0f3f | 1282 | cmdline_mode = &fb_helper_conn->cmdline_mode; |
38651674 DA |
1283 | if (cmdline_mode->specified == false) |
1284 | return mode; | |
1285 | ||
1286 | /* attempt to find a matching mode in the list of modes | |
1287 | * we have gotten so far, if not add a CVT mode that conforms | |
1288 | */ | |
1289 | if (cmdline_mode->rb || cmdline_mode->margins) | |
1290 | goto create_mode; | |
1291 | ||
c683f427 TI |
1292 | prefer_non_interlace = !cmdline_mode->interlace; |
1293 | again: | |
0b4c0f3f | 1294 | list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { |
38651674 DA |
1295 | /* check width/height */ |
1296 | if (mode->hdisplay != cmdline_mode->xres || | |
1297 | mode->vdisplay != cmdline_mode->yres) | |
1298 | continue; | |
1299 | ||
1300 | if (cmdline_mode->refresh_specified) { | |
1301 | if (mode->vrefresh != cmdline_mode->refresh) | |
1302 | continue; | |
1303 | } | |
1304 | ||
1305 | if (cmdline_mode->interlace) { | |
1306 | if (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) | |
1307 | continue; | |
c683f427 TI |
1308 | } else if (prefer_non_interlace) { |
1309 | if (mode->flags & DRM_MODE_FLAG_INTERLACE) | |
1310 | continue; | |
38651674 DA |
1311 | } |
1312 | return mode; | |
1313 | } | |
1314 | ||
c683f427 TI |
1315 | if (prefer_non_interlace) { |
1316 | prefer_non_interlace = false; | |
1317 | goto again; | |
1318 | } | |
1319 | ||
38651674 | 1320 | create_mode: |
1794d257 CW |
1321 | mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev, |
1322 | cmdline_mode); | |
0b4c0f3f | 1323 | list_add(&mode->head, &fb_helper_conn->connector->modes); |
38651674 DA |
1324 | return mode; |
1325 | } | |
2f1046f3 | 1326 | EXPORT_SYMBOL(drm_pick_cmdline_mode); |
38651674 DA |
1327 | |
1328 | static bool drm_connector_enabled(struct drm_connector *connector, bool strict) | |
1329 | { | |
1330 | bool enable; | |
1331 | ||
96081cdf | 1332 | if (strict) |
38651674 | 1333 | enable = connector->status == connector_status_connected; |
96081cdf | 1334 | else |
38651674 | 1335 | enable = connector->status != connector_status_disconnected; |
96081cdf | 1336 | |
38651674 DA |
1337 | return enable; |
1338 | } | |
1339 | ||
0b4c0f3f DA |
1340 | static void drm_enable_connectors(struct drm_fb_helper *fb_helper, |
1341 | bool *enabled) | |
38651674 DA |
1342 | { |
1343 | bool any_enabled = false; | |
1344 | struct drm_connector *connector; | |
1345 | int i = 0; | |
1346 | ||
0b4c0f3f DA |
1347 | for (i = 0; i < fb_helper->connector_count; i++) { |
1348 | connector = fb_helper->connector_info[i]->connector; | |
38651674 DA |
1349 | enabled[i] = drm_connector_enabled(connector, true); |
1350 | DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id, | |
1351 | enabled[i] ? "yes" : "no"); | |
1352 | any_enabled |= enabled[i]; | |
38651674 DA |
1353 | } |
1354 | ||
1355 | if (any_enabled) | |
1356 | return; | |
1357 | ||
0b4c0f3f DA |
1358 | for (i = 0; i < fb_helper->connector_count; i++) { |
1359 | connector = fb_helper->connector_info[i]->connector; | |
38651674 | 1360 | enabled[i] = drm_connector_enabled(connector, false); |
38651674 DA |
1361 | } |
1362 | } | |
1363 | ||
1d42bbc8 DA |
1364 | static bool drm_target_cloned(struct drm_fb_helper *fb_helper, |
1365 | struct drm_display_mode **modes, | |
1366 | bool *enabled, int width, int height) | |
1367 | { | |
1368 | int count, i, j; | |
1369 | bool can_clone = false; | |
1370 | struct drm_fb_helper_connector *fb_helper_conn; | |
1371 | struct drm_display_mode *dmt_mode, *mode; | |
1372 | ||
1373 | /* only contemplate cloning in the single crtc case */ | |
1374 | if (fb_helper->crtc_count > 1) | |
1375 | return false; | |
1376 | ||
1377 | count = 0; | |
1378 | for (i = 0; i < fb_helper->connector_count; i++) { | |
1379 | if (enabled[i]) | |
1380 | count++; | |
1381 | } | |
1382 | ||
1383 | /* only contemplate cloning if more than one connector is enabled */ | |
1384 | if (count <= 1) | |
1385 | return false; | |
1386 | ||
1387 | /* check the command line or if nothing common pick 1024x768 */ | |
1388 | can_clone = true; | |
1389 | for (i = 0; i < fb_helper->connector_count; i++) { | |
1390 | if (!enabled[i]) | |
1391 | continue; | |
1392 | fb_helper_conn = fb_helper->connector_info[i]; | |
1393 | modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height); | |
1394 | if (!modes[i]) { | |
1395 | can_clone = false; | |
1396 | break; | |
1397 | } | |
1398 | for (j = 0; j < i; j++) { | |
1399 | if (!enabled[j]) | |
1400 | continue; | |
1401 | if (!drm_mode_equal(modes[j], modes[i])) | |
1402 | can_clone = false; | |
1403 | } | |
1404 | } | |
1405 | ||
1406 | if (can_clone) { | |
1407 | DRM_DEBUG_KMS("can clone using command line\n"); | |
1408 | return true; | |
1409 | } | |
1410 | ||
1411 | /* try and find a 1024x768 mode on each connector */ | |
1412 | can_clone = true; | |
f6e252ba | 1413 | dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false); |
1d42bbc8 DA |
1414 | |
1415 | for (i = 0; i < fb_helper->connector_count; i++) { | |
1416 | ||
1417 | if (!enabled[i]) | |
1418 | continue; | |
1419 | ||
1420 | fb_helper_conn = fb_helper->connector_info[i]; | |
1421 | list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { | |
1422 | if (drm_mode_equal(mode, dmt_mode)) | |
1423 | modes[i] = mode; | |
1424 | } | |
1425 | if (!modes[i]) | |
1426 | can_clone = false; | |
1427 | } | |
1428 | ||
1429 | if (can_clone) { | |
1430 | DRM_DEBUG_KMS("can clone using 1024x768\n"); | |
1431 | return true; | |
1432 | } | |
1433 | DRM_INFO("kms: can't enable cloning when we probably wanted to.\n"); | |
1434 | return false; | |
1435 | } | |
1436 | ||
0b4c0f3f | 1437 | static bool drm_target_preferred(struct drm_fb_helper *fb_helper, |
38651674 DA |
1438 | struct drm_display_mode **modes, |
1439 | bool *enabled, int width, int height) | |
1440 | { | |
0b4c0f3f DA |
1441 | struct drm_fb_helper_connector *fb_helper_conn; |
1442 | int i; | |
38651674 | 1443 | |
0b4c0f3f DA |
1444 | for (i = 0; i < fb_helper->connector_count; i++) { |
1445 | fb_helper_conn = fb_helper->connector_info[i]; | |
38651674 | 1446 | |
0b4c0f3f | 1447 | if (enabled[i] == false) |
38651674 | 1448 | continue; |
38651674 DA |
1449 | |
1450 | DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n", | |
0b4c0f3f | 1451 | fb_helper_conn->connector->base.id); |
38651674 DA |
1452 | |
1453 | /* got for command line mode first */ | |
0b4c0f3f | 1454 | modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height); |
38651674 DA |
1455 | if (!modes[i]) { |
1456 | DRM_DEBUG_KMS("looking for preferred mode on connector %d\n", | |
0b4c0f3f DA |
1457 | fb_helper_conn->connector->base.id); |
1458 | modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height); | |
38651674 DA |
1459 | } |
1460 | /* No preferred modes, pick one off the list */ | |
0b4c0f3f DA |
1461 | if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) { |
1462 | list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head) | |
38651674 DA |
1463 | break; |
1464 | } | |
1465 | DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name : | |
1466 | "none"); | |
38651674 DA |
1467 | } |
1468 | return true; | |
1469 | } | |
1470 | ||
8be48d92 DA |
1471 | static int drm_pick_crtcs(struct drm_fb_helper *fb_helper, |
1472 | struct drm_fb_helper_crtc **best_crtcs, | |
38651674 DA |
1473 | struct drm_display_mode **modes, |
1474 | int n, int width, int height) | |
1475 | { | |
1476 | int c, o; | |
8be48d92 | 1477 | struct drm_device *dev = fb_helper->dev; |
38651674 DA |
1478 | struct drm_connector *connector; |
1479 | struct drm_connector_helper_funcs *connector_funcs; | |
1480 | struct drm_encoder *encoder; | |
38651674 | 1481 | int my_score, best_score, score; |
8be48d92 | 1482 | struct drm_fb_helper_crtc **crtcs, *crtc; |
0b4c0f3f | 1483 | struct drm_fb_helper_connector *fb_helper_conn; |
38651674 | 1484 | |
0b4c0f3f | 1485 | if (n == fb_helper->connector_count) |
38651674 | 1486 | return 0; |
0b4c0f3f DA |
1487 | |
1488 | fb_helper_conn = fb_helper->connector_info[n]; | |
1489 | connector = fb_helper_conn->connector; | |
38651674 DA |
1490 | |
1491 | best_crtcs[n] = NULL; | |
8be48d92 | 1492 | best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height); |
38651674 DA |
1493 | if (modes[n] == NULL) |
1494 | return best_score; | |
1495 | ||
8be48d92 DA |
1496 | crtcs = kzalloc(dev->mode_config.num_connector * |
1497 | sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); | |
38651674 DA |
1498 | if (!crtcs) |
1499 | return best_score; | |
1500 | ||
1501 | my_score = 1; | |
1502 | if (connector->status == connector_status_connected) | |
1503 | my_score++; | |
0b4c0f3f | 1504 | if (drm_has_cmdline_mode(fb_helper_conn)) |
38651674 | 1505 | my_score++; |
0b4c0f3f | 1506 | if (drm_has_preferred_mode(fb_helper_conn, width, height)) |
38651674 DA |
1507 | my_score++; |
1508 | ||
1509 | connector_funcs = connector->helper_private; | |
1510 | encoder = connector_funcs->best_encoder(connector); | |
1511 | if (!encoder) | |
1512 | goto out; | |
1513 | ||
38651674 DA |
1514 | /* select a crtc for this connector and then attempt to configure |
1515 | remaining connectors */ | |
8be48d92 DA |
1516 | for (c = 0; c < fb_helper->crtc_count; c++) { |
1517 | crtc = &fb_helper->crtc_info[c]; | |
38651674 | 1518 | |
96081cdf | 1519 | if ((encoder->possible_crtcs & (1 << c)) == 0) |
38651674 | 1520 | continue; |
38651674 DA |
1521 | |
1522 | for (o = 0; o < n; o++) | |
1523 | if (best_crtcs[o] == crtc) | |
1524 | break; | |
1525 | ||
1526 | if (o < n) { | |
1d42bbc8 DA |
1527 | /* ignore cloning unless only a single crtc */ |
1528 | if (fb_helper->crtc_count > 1) | |
1529 | continue; | |
1530 | ||
1531 | if (!drm_mode_equal(modes[o], modes[n])) | |
1532 | continue; | |
38651674 DA |
1533 | } |
1534 | ||
1535 | crtcs[n] = crtc; | |
8be48d92 DA |
1536 | memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *)); |
1537 | score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1, | |
38651674 DA |
1538 | width, height); |
1539 | if (score > best_score) { | |
38651674 DA |
1540 | best_score = score; |
1541 | memcpy(best_crtcs, crtcs, | |
1542 | dev->mode_config.num_connector * | |
8be48d92 | 1543 | sizeof(struct drm_fb_helper_crtc *)); |
38651674 | 1544 | } |
38651674 DA |
1545 | } |
1546 | out: | |
1547 | kfree(crtcs); | |
1548 | return best_score; | |
1549 | } | |
1550 | ||
8be48d92 | 1551 | static void drm_setup_crtcs(struct drm_fb_helper *fb_helper) |
38651674 | 1552 | { |
8be48d92 DA |
1553 | struct drm_device *dev = fb_helper->dev; |
1554 | struct drm_fb_helper_crtc **crtcs; | |
38651674 | 1555 | struct drm_display_mode **modes; |
8be48d92 | 1556 | struct drm_mode_set *modeset; |
38651674 DA |
1557 | bool *enabled; |
1558 | int width, height; | |
11e17a08 | 1559 | int i; |
38651674 DA |
1560 | |
1561 | DRM_DEBUG_KMS("\n"); | |
1562 | ||
1563 | width = dev->mode_config.max_width; | |
1564 | height = dev->mode_config.max_height; | |
1565 | ||
38651674 | 1566 | crtcs = kcalloc(dev->mode_config.num_connector, |
8be48d92 | 1567 | sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); |
38651674 DA |
1568 | modes = kcalloc(dev->mode_config.num_connector, |
1569 | sizeof(struct drm_display_mode *), GFP_KERNEL); | |
1570 | enabled = kcalloc(dev->mode_config.num_connector, | |
1571 | sizeof(bool), GFP_KERNEL); | |
8c5eaca0 SK |
1572 | if (!crtcs || !modes || !enabled) { |
1573 | DRM_ERROR("Memory allocation failed\n"); | |
1574 | goto out; | |
1575 | } | |
1576 | ||
38651674 | 1577 | |
0b4c0f3f | 1578 | drm_enable_connectors(fb_helper, enabled); |
38651674 | 1579 | |
11e17a08 JB |
1580 | if (!(fb_helper->funcs->initial_config && |
1581 | fb_helper->funcs->initial_config(fb_helper, crtcs, modes, | |
1582 | enabled, width, height))) { | |
1583 | memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0])); | |
1584 | memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0])); | |
1585 | ||
1586 | if (!drm_target_cloned(fb_helper, | |
1587 | modes, enabled, width, height) && | |
1588 | !drm_target_preferred(fb_helper, | |
1589 | modes, enabled, width, height)) | |
1d42bbc8 | 1590 | DRM_ERROR("Unable to find initial modes\n"); |
38651674 | 1591 | |
11e17a08 JB |
1592 | DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", |
1593 | width, height); | |
38651674 | 1594 | |
11e17a08 JB |
1595 | drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height); |
1596 | } | |
8be48d92 DA |
1597 | |
1598 | /* need to set the modesets up here for use later */ | |
1599 | /* fill out the connector<->crtc mappings into the modesets */ | |
1600 | for (i = 0; i < fb_helper->crtc_count; i++) { | |
1601 | modeset = &fb_helper->crtc_info[i].mode_set; | |
1602 | modeset->num_connectors = 0; | |
7e53f3a4 | 1603 | modeset->fb = NULL; |
8be48d92 | 1604 | } |
38651674 | 1605 | |
0b4c0f3f | 1606 | for (i = 0; i < fb_helper->connector_count; i++) { |
38651674 | 1607 | struct drm_display_mode *mode = modes[i]; |
8be48d92 DA |
1608 | struct drm_fb_helper_crtc *fb_crtc = crtcs[i]; |
1609 | modeset = &fb_crtc->mode_set; | |
38651674 | 1610 | |
8be48d92 | 1611 | if (mode && fb_crtc) { |
38651674 | 1612 | DRM_DEBUG_KMS("desired mode %s set on crtc %d\n", |
8be48d92 DA |
1613 | mode->name, fb_crtc->mode_set.crtc->base.id); |
1614 | fb_crtc->desired_mode = mode; | |
1615 | if (modeset->mode) | |
1616 | drm_mode_destroy(dev, modeset->mode); | |
1617 | modeset->mode = drm_mode_duplicate(dev, | |
1618 | fb_crtc->desired_mode); | |
0b4c0f3f | 1619 | modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector; |
7e53f3a4 | 1620 | modeset->fb = fb_helper->fb; |
38651674 | 1621 | } |
38651674 DA |
1622 | } |
1623 | ||
7e53f3a4 DV |
1624 | /* Clear out any old modes if there are no more connected outputs. */ |
1625 | for (i = 0; i < fb_helper->crtc_count; i++) { | |
1626 | modeset = &fb_helper->crtc_info[i].mode_set; | |
1627 | if (modeset->num_connectors == 0) { | |
1628 | BUG_ON(modeset->fb); | |
1629 | BUG_ON(modeset->num_connectors); | |
1630 | if (modeset->mode) | |
1631 | drm_mode_destroy(dev, modeset->mode); | |
1632 | modeset->mode = NULL; | |
1633 | } | |
1634 | } | |
8c5eaca0 | 1635 | out: |
38651674 DA |
1636 | kfree(crtcs); |
1637 | kfree(modes); | |
1638 | kfree(enabled); | |
1639 | } | |
1640 | ||
1641 | /** | |
207fd329 | 1642 | * drm_fb_helper_initial_config - setup a sane initial connector configuration |
d0ddc033 DV |
1643 | * @fb_helper: fb_helper device struct |
1644 | * @bpp_sel: bpp value to use for the framebuffer configuration | |
38651674 | 1645 | * |
d0ddc033 | 1646 | * Scans the CRTCs and connectors and tries to put together an initial setup. |
38651674 DA |
1647 | * At the moment, this is a cloned configuration across all heads with |
1648 | * a new framebuffer object as the backing store. | |
1649 | * | |
207fd329 DV |
1650 | * Note that this also registers the fbdev and so allows userspace to call into |
1651 | * the driver through the fbdev interfaces. | |
1652 | * | |
1653 | * This function will call down into the ->fb_probe callback to let | |
1654 | * the driver allocate and initialize the fbdev info structure and the drm | |
1655 | * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and | |
1656 | * drm_fb_helper_fill_fix() are provided as helpers to setup simple default | |
1657 | * values for the fbdev info structure. | |
1658 | * | |
38651674 DA |
1659 | * RETURNS: |
1660 | * Zero if everything went ok, nonzero otherwise. | |
1661 | */ | |
4abe3520 | 1662 | bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel) |
38651674 | 1663 | { |
8be48d92 | 1664 | struct drm_device *dev = fb_helper->dev; |
38651674 DA |
1665 | int count = 0; |
1666 | ||
0b4c0f3f | 1667 | drm_fb_helper_parse_command_line(fb_helper); |
38651674 | 1668 | |
53f1904b | 1669 | mutex_lock(&dev->mode_config.mutex); |
0b4c0f3f DA |
1670 | count = drm_fb_helper_probe_connector_modes(fb_helper, |
1671 | dev->mode_config.max_width, | |
1672 | dev->mode_config.max_height); | |
53f1904b | 1673 | mutex_unlock(&dev->mode_config.mutex); |
38651674 DA |
1674 | /* |
1675 | * we shouldn't end up with no modes here. | |
1676 | */ | |
96081cdf | 1677 | if (count == 0) |
d56b1b9d | 1678 | dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n"); |
96081cdf | 1679 | |
8be48d92 | 1680 | drm_setup_crtcs(fb_helper); |
38651674 | 1681 | |
4abe3520 | 1682 | return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel); |
38651674 | 1683 | } |
8be48d92 | 1684 | EXPORT_SYMBOL(drm_fb_helper_initial_config); |
38651674 | 1685 | |
7394371d CW |
1686 | /** |
1687 | * drm_fb_helper_hotplug_event - respond to a hotplug notification by | |
d0ddc033 | 1688 | * probing all the outputs attached to the fb |
7394371d CW |
1689 | * @fb_helper: the drm_fb_helper |
1690 | * | |
7394371d CW |
1691 | * Scan the connectors attached to the fb_helper and try to put together a |
1692 | * setup after *notification of a change in output configuration. | |
1693 | * | |
207fd329 DV |
1694 | * Called at runtime, takes the mode config locks to be able to check/change the |
1695 | * modeset configuration. Must be run from process context (which usually means | |
1696 | * either the output polling work or a work item launched from the driver's | |
1697 | * hotplug interrupt). | |
1698 | * | |
50c3dc97 DV |
1699 | * Note that drivers may call this even before calling |
1700 | * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows | |
1701 | * for a race-free fbcon setup and will make sure that the fbdev emulation will | |
1702 | * not miss any hotplug events. | |
207fd329 | 1703 | * |
7394371d CW |
1704 | * RETURNS: |
1705 | * 0 on success and a non-zero error code otherwise. | |
1706 | */ | |
1707 | int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper) | |
38651674 | 1708 | { |
7394371d | 1709 | struct drm_device *dev = fb_helper->dev; |
51bbd276 | 1710 | u32 max_width, max_height; |
5c4426a7 | 1711 | |
89ced125 | 1712 | mutex_lock(&fb_helper->dev->mode_config.mutex); |
50c3dc97 | 1713 | if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) { |
4abe3520 | 1714 | fb_helper->delayed_hotplug = true; |
89ced125 | 1715 | mutex_unlock(&fb_helper->dev->mode_config.mutex); |
7394371d | 1716 | return 0; |
4abe3520 | 1717 | } |
eb1f8e4f | 1718 | DRM_DEBUG_KMS("\n"); |
4abe3520 | 1719 | |
eb1f8e4f DA |
1720 | max_width = fb_helper->fb->width; |
1721 | max_height = fb_helper->fb->height; | |
5c4426a7 | 1722 | |
51bbd276 | 1723 | drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height); |
89ced125 DV |
1724 | mutex_unlock(&fb_helper->dev->mode_config.mutex); |
1725 | ||
1726 | drm_modeset_lock_all(dev); | |
eb1f8e4f | 1727 | drm_setup_crtcs(fb_helper); |
84849903 | 1728 | drm_modeset_unlock_all(dev); |
2180c3c7 DV |
1729 | drm_fb_helper_set_par(fb_helper->fbdev); |
1730 | ||
1731 | return 0; | |
5c4426a7 | 1732 | } |
eb1f8e4f | 1733 | EXPORT_SYMBOL(drm_fb_helper_hotplug_event); |
5c4426a7 | 1734 | |
6a108a14 | 1735 | /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT) |
3ce05168 DF |
1736 | * but the module doesn't depend on any fb console symbols. At least |
1737 | * attempt to load fbcon to avoid leaving the system without a usable console. | |
1738 | */ | |
6a108a14 | 1739 | #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT) |
3ce05168 DF |
1740 | static int __init drm_fb_helper_modinit(void) |
1741 | { | |
1742 | const char *name = "fbcon"; | |
1743 | struct module *fbcon; | |
1744 | ||
1745 | mutex_lock(&module_mutex); | |
1746 | fbcon = find_module(name); | |
1747 | mutex_unlock(&module_mutex); | |
1748 | ||
1749 | if (!fbcon) | |
1750 | request_module_nowait(name); | |
1751 | return 0; | |
1752 | } | |
1753 | ||
1754 | module_init(drm_fb_helper_modinit); | |
1755 | #endif |