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