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