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