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