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