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