drm: extract legacy framebuffer remove
[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
cfe63423 32#include <linux/console.h>
3b40a443 33#include <linux/kernel.h>
785b93ef 34#include <linux/sysrq.h>
5a0e3ad6 35#include <linux/slab.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>
bbb1e524
RC
41#include <drm/drm_atomic.h>
42#include <drm/drm_atomic_helper.h>
785b93ef 43
699fbeea
VS
44#include "drm_crtc_helper_internal.h"
45
f64c5573
DV
46static bool drm_fbdev_emulation = true;
47module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600);
48MODULE_PARM_DESC(fbdev_emulation,
49 "Enable legacy fbdev emulation [default=true]");
50
5f152576
XL
51static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC;
52module_param(drm_fbdev_overalloc, int, 0444);
53MODULE_PARM_DESC(drm_fbdev_overalloc,
54 "Overallocation of the fbdev buffer (%) [default="
55 __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]");
56
785b93ef 57static LIST_HEAD(kernel_fb_helper_list);
a53ca635 58static DEFINE_MUTEX(kernel_fb_helper_lock);
785b93ef 59
d0ddc033
DV
60/**
61 * DOC: fbdev helpers
62 *
63 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
83c617c5 64 * mode setting driver. They can be used mostly independently from the crtc
d0ddc033
DV
65 * helper functions used by many drivers to implement the kernel mode setting
66 * interfaces.
207fd329 67 *
10a23102
TR
68 * Initialization is done as a four-step process with drm_fb_helper_prepare(),
69 * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and
70 * drm_fb_helper_initial_config(). Drivers with fancier requirements than the
71 * default behaviour can override the third step with their own code.
ed84e254
DV
72 * Teardown is done with drm_fb_helper_fini() after the fbdev device is
73 * unregisters using drm_fb_helper_unregister_fbi().
207fd329
DV
74 *
75 * At runtime drivers should restore the fbdev console by calling
6806cdf9
DV
76 * drm_fb_helper_restore_fbdev_mode_unlocked() from their &drm_driver.lastclose
77 * callback. They should also notify the fb helper code from updates to the
78 * output configuration by calling drm_fb_helper_hotplug_event(). For easier
207fd329 79 * integration with the output polling code in drm_crtc_helper.c the modeset
6806cdf9 80 * code provides a &drm_mode_config_funcs.output_poll_changed callback.
207fd329
DV
81 *
82 * All other functions exported by the fb helper library can be used to
83 * implement the fbdev driver interface by the driver.
10a23102
TR
84 *
85 * It is possible, though perhaps somewhat tricky, to implement race-free
86 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
87 * helper must be called first to initialize the minimum required to make
88 * hotplug detection work. Drivers also need to make sure to properly set up
6806cdf9 89 * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init()
10a23102
TR
90 * it is safe to enable interrupts and start processing hotplug events. At the
91 * same time, drivers should initialize all modeset objects such as CRTCs,
92 * encoders and connectors. To finish up the fbdev helper initialization, the
93 * drm_fb_helper_init() function is called. To probe for all attached displays
94 * and set up an initial configuration using the detected hardware, drivers
95 * should call drm_fb_helper_single_add_all_connectors() followed by
96 * drm_fb_helper_initial_config().
eaa434de 97 *
6806cdf9 98 * If &drm_framebuffer_funcs.dirty is set, the
2dad551c 99 * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will
6806cdf9 100 * accumulate changes and schedule &drm_fb_helper.dirty_work to run right
2dad551c
NT
101 * away. This worker then calls the dirty() function ensuring that it will
102 * always run in process context since the fb_*() function could be running in
103 * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io
104 * callback it will also schedule dirty_work with the damage collected from the
105 * mmap page writes.
d0ddc033
DV
106 */
107
966a6a13
CW
108#define drm_fb_helper_for_each_connector(fbh, i__) \
109 for (({ lockdep_assert_held(&(fbh)->dev->mode_config.mutex); }), \
110 i__ = 0; i__ < (fbh)->connector_count; i__++)
111
50021ff1
TR
112int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
113 struct drm_connector *connector)
39b8b2ed 114{
50021ff1 115 struct drm_fb_helper_connector *fb_conn;
39b8b2ed 116 struct drm_fb_helper_connector **temp;
50021ff1 117 unsigned int count;
39b8b2ed
TR
118
119 if (!drm_fbdev_emulation)
120 return 0;
121
122 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
50021ff1
TR
123
124 count = fb_helper->connector_count + 1;
125
126 if (count > fb_helper->connector_info_alloc_count) {
127 size_t size = count * sizeof(fb_conn);
128
129 temp = krealloc(fb_helper->connector_info, size, GFP_KERNEL);
39b8b2ed
TR
130 if (!temp)
131 return -ENOMEM;
132
50021ff1 133 fb_helper->connector_info_alloc_count = count;
39b8b2ed
TR
134 fb_helper->connector_info = temp;
135 }
136
50021ff1
TR
137 fb_conn = kzalloc(sizeof(*fb_conn), GFP_KERNEL);
138 if (!fb_conn)
39b8b2ed
TR
139 return -ENOMEM;
140
141 drm_connector_get(connector);
50021ff1
TR
142 fb_conn->connector = connector;
143 fb_helper->connector_info[fb_helper->connector_count++] = fb_conn;
39b8b2ed
TR
144 return 0;
145}
146EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
147
207fd329
DV
148/**
149 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
150 * emulation helper
151 * @fb_helper: fbdev initialized with drm_fb_helper_init
152 *
153 * This functions adds all the available connectors for use with the given
154 * fb_helper. This is a separate step to allow drivers to freely assign
155 * connectors to the fbdev, e.g. if some are reserved for special purposes or
156 * not adequate to be used for the fbcon.
157 *
169faeca
DV
158 * This function is protected against concurrent connector hotadds/removals
159 * using drm_fb_helper_add_one_connector() and
160 * drm_fb_helper_remove_one_connector().
207fd329 161 */
0b4c0f3f 162int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
d50ba256 163{
0b4c0f3f
DA
164 struct drm_device *dev = fb_helper->dev;
165 struct drm_connector *connector;
c36a3254
DV
166 struct drm_connector_list_iter conn_iter;
167 int i, ret = 0;
d50ba256 168
f64c5573
DV
169 if (!drm_fbdev_emulation)
170 return 0;
171
169faeca 172 mutex_lock(&dev->mode_config.mutex);
b982dab1 173 drm_connector_list_iter_begin(dev, &conn_iter);
c36a3254 174 drm_for_each_connector_iter(connector, &conn_iter) {
15fce29d 175 ret = drm_fb_helper_add_one_connector(fb_helper, connector);
0b4c0f3f 176
15fce29d 177 if (ret)
0b4c0f3f 178 goto fail;
0b4c0f3f 179 }
c36a3254
DV
180 goto out;
181
0b4c0f3f 182fail:
966a6a13 183 drm_fb_helper_for_each_connector(fb_helper, i) {
7dfcb36a
VS
184 struct drm_fb_helper_connector *fb_helper_connector =
185 fb_helper->connector_info[i];
186
ad093607 187 drm_connector_put(fb_helper_connector->connector);
7dfcb36a
VS
188
189 kfree(fb_helper_connector);
0b4c0f3f
DA
190 fb_helper->connector_info[i] = NULL;
191 }
192 fb_helper->connector_count = 0;
c36a3254 193out:
b982dab1 194 drm_connector_list_iter_end(&conn_iter);
169faeca
DV
195 mutex_unlock(&dev->mode_config.mutex);
196
15fce29d 197 return ret;
d50ba256 198}
0b4c0f3f 199EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
d50ba256 200
65c2a89c
DA
201int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
202 struct drm_connector *connector)
203{
204 struct drm_fb_helper_connector *fb_helper_connector;
205 int i, j;
206
f64c5573
DV
207 if (!drm_fbdev_emulation)
208 return 0;
209
65c2a89c
DA
210 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
211
212 for (i = 0; i < fb_helper->connector_count; i++) {
213 if (fb_helper->connector_info[i]->connector == connector)
214 break;
215 }
216
217 if (i == fb_helper->connector_count)
218 return -EINVAL;
219 fb_helper_connector = fb_helper->connector_info[i];
ad093607 220 drm_connector_put(fb_helper_connector->connector);
65c2a89c 221
4b4f99f5 222 for (j = i + 1; j < fb_helper->connector_count; j++)
65c2a89c 223 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
4b4f99f5 224
65c2a89c
DA
225 fb_helper->connector_count--;
226 kfree(fb_helper_connector);
2148f18f 227
65c2a89c
DA
228 return 0;
229}
230EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
231
99231028
JW
232static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
233{
234 uint16_t *r_base, *g_base, *b_base;
235 int i;
236
04c0c569
VS
237 if (helper->funcs->gamma_get == NULL)
238 return;
239
99231028
JW
240 r_base = crtc->gamma_store;
241 g_base = r_base + crtc->gamma_size;
242 b_base = g_base + crtc->gamma_size;
243
244 for (i = 0; i < crtc->gamma_size; i++)
245 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
246}
247
248static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
249{
250 uint16_t *r_base, *g_base, *b_base;
251
ebe0f244
LP
252 if (crtc->funcs->gamma_set == NULL)
253 return;
254
99231028
JW
255 r_base = crtc->gamma_store;
256 g_base = r_base + crtc->gamma_size;
257 b_base = g_base + crtc->gamma_size;
258
6d124ff8
DV
259 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
260 crtc->gamma_size, NULL);
99231028
JW
261}
262
207fd329 263/**
6806cdf9 264 * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
207fd329
DV
265 * @info: fbdev registered by the helper
266 */
1a7aba7f
JB
267int drm_fb_helper_debug_enter(struct fb_info *info)
268{
269 struct drm_fb_helper *helper = info->par;
be26a66d 270 const struct drm_crtc_helper_funcs *funcs;
1a7aba7f
JB
271 int i;
272
1a7aba7f
JB
273 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
274 for (i = 0; i < helper->crtc_count; i++) {
275 struct drm_mode_set *mode_set =
276 &helper->crtc_info[i].mode_set;
277
278 if (!mode_set->crtc->enabled)
279 continue;
280
281 funcs = mode_set->crtc->helper_private;
1b99b724
SC
282 if (funcs->mode_set_base_atomic == NULL)
283 continue;
284
9c79e0b1
DV
285 if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev))
286 continue;
287
99231028 288 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
1a7aba7f
JB
289 funcs->mode_set_base_atomic(mode_set->crtc,
290 mode_set->fb,
291 mode_set->x,
413d45d3 292 mode_set->y,
21c74a8e 293 ENTER_ATOMIC_MODE_SET);
1a7aba7f
JB
294 }
295 }
296
297 return 0;
298}
299EXPORT_SYMBOL(drm_fb_helper_debug_enter);
300
301/* Find the real fb for a given fb helper CRTC */
302static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
303{
304 struct drm_device *dev = crtc->dev;
305 struct drm_crtc *c;
306
6295d607 307 drm_for_each_crtc(c, dev) {
1a7aba7f 308 if (crtc->base.id == c->base.id)
f4510a27 309 return c->primary->fb;
1a7aba7f
JB
310 }
311
312 return NULL;
313}
314
207fd329 315/**
6806cdf9 316 * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
207fd329
DV
317 * @info: fbdev registered by the helper
318 */
1a7aba7f
JB
319int drm_fb_helper_debug_leave(struct fb_info *info)
320{
321 struct drm_fb_helper *helper = info->par;
322 struct drm_crtc *crtc;
be26a66d 323 const struct drm_crtc_helper_funcs *funcs;
1a7aba7f
JB
324 struct drm_framebuffer *fb;
325 int i;
326
327 for (i = 0; i < helper->crtc_count; i++) {
328 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
4b4f99f5 329
1a7aba7f
JB
330 crtc = mode_set->crtc;
331 funcs = crtc->helper_private;
332 fb = drm_mode_config_fb(crtc);
333
334 if (!crtc->enabled)
335 continue;
336
337 if (!fb) {
338 DRM_ERROR("no fb to restore??\n");
339 continue;
340 }
341
1b99b724
SC
342 if (funcs->mode_set_base_atomic == NULL)
343 continue;
344
9c79e0b1
DV
345 if (drm_drv_uses_atomic_modeset(crtc->dev))
346 continue;
347
99231028 348 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
1a7aba7f 349 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
21c74a8e 350 crtc->y, LEAVE_ATOMIC_MODE_SET);
1a7aba7f
JB
351 }
352
353 return 0;
354}
355EXPORT_SYMBOL(drm_fb_helper_debug_leave);
356
bbb1e524
RC
357static int restore_fbdev_mode_atomic(struct drm_fb_helper *fb_helper)
358{
359 struct drm_device *dev = fb_helper->dev;
360 struct drm_plane *plane;
361 struct drm_atomic_state *state;
362 int i, ret;
4b4f99f5 363 unsigned int plane_mask;
bbb1e524
RC
364
365 state = drm_atomic_state_alloc(dev);
366 if (!state)
367 return -ENOMEM;
368
369 state->acquire_ctx = dev->mode_config.acquire_ctx;
370retry:
f72c6b33 371 plane_mask = 0;
bbb1e524
RC
372 drm_for_each_plane(plane, dev) {
373 struct drm_plane_state *plane_state;
374
375 plane_state = drm_atomic_get_plane_state(state, plane);
376 if (IS_ERR(plane_state)) {
377 ret = PTR_ERR(plane_state);
378 goto fail;
379 }
380
31ad61e4 381 plane_state->rotation = DRM_ROTATE_0;
bbb1e524 382
f72c6b33
ML
383 plane->old_fb = plane->fb;
384 plane_mask |= 1 << drm_plane_index(plane);
385
bbb1e524
RC
386 /* disable non-primary: */
387 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
388 continue;
389
390 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
391 if (ret != 0)
392 goto fail;
393 }
394
4b4f99f5 395 for (i = 0; i < fb_helper->crtc_count; i++) {
bbb1e524
RC
396 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
397
398 ret = __drm_atomic_helper_set_config(mode_set, state);
399 if (ret != 0)
400 goto fail;
401 }
402
403 ret = drm_atomic_commit(state);
bbb1e524
RC
404
405fail:
f72c6b33 406 drm_atomic_clean_old_fb(dev, plane_mask, ret);
94284037 407
bbb1e524
RC
408 if (ret == -EDEADLK)
409 goto backoff;
410
0853695c 411 drm_atomic_state_put(state);
bbb1e524
RC
412 return ret;
413
414backoff:
415 drm_atomic_state_clear(state);
416 drm_atomic_legacy_backoff(state);
417
418 goto retry;
419}
420
b7bdf0a8 421static int restore_fbdev_mode(struct drm_fb_helper *fb_helper)
e8e7a2b8 422{
3858bc5d
VS
423 struct drm_device *dev = fb_helper->dev;
424 struct drm_plane *plane;
3858bc5d
VS
425 int i;
426
427 drm_warn_on_modeset_not_all_locked(dev);
6aed8ec3 428
a743d758 429 if (drm_drv_uses_atomic_modeset(dev))
bbb1e524
RC
430 return restore_fbdev_mode_atomic(fb_helper);
431
6295d607 432 drm_for_each_plane(plane, dev) {
e27dde3e
MR
433 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
434 drm_plane_force_disable(plane);
6aed8ec3 435
6686df8c 436 if (plane->rotation_property)
d138dd3c
VS
437 drm_mode_plane_set_obj_prop(plane,
438 plane->rotation_property,
439 DRM_ROTATE_0);
9783de20
SJ
440 }
441
e8e7a2b8
DA
442 for (i = 0; i < fb_helper->crtc_count; i++) {
443 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
3858bc5d
VS
444 struct drm_crtc *crtc = mode_set->crtc;
445 int ret;
446
03f9abb2
AD
447 if (crtc->funcs->cursor_set2) {
448 ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
449 if (ret)
48f87dd1 450 return ret;
03f9abb2 451 } else if (crtc->funcs->cursor_set) {
3858bc5d
VS
452 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
453 if (ret)
b7bdf0a8 454 return ret;
3858bc5d
VS
455 }
456
2d13b679 457 ret = drm_mode_set_config_internal(mode_set);
e8e7a2b8 458 if (ret)
b7bdf0a8 459 return ret;
e8e7a2b8 460 }
b7bdf0a8
DV
461
462 return 0;
e8e7a2b8 463}
5ea1f752
RC
464
465/**
466 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
467 * @fb_helper: fbcon to restore
468 *
6806cdf9 469 * This should be called from driver's drm &drm_driver.lastclose callback
5ea1f752
RC
470 * when implementing an fbcon on top of kms using this helper. This ensures that
471 * the user isn't greeted with a black screen when e.g. X dies.
b7bdf0a8
DV
472 *
473 * RETURNS:
474 * Zero if everything went ok, negative error code otherwise.
5ea1f752 475 */
b7bdf0a8 476int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
5ea1f752
RC
477{
478 struct drm_device *dev = fb_helper->dev;
b7bdf0a8
DV
479 bool do_delayed;
480 int ret;
e2809c7d 481
f64c5573
DV
482 if (!drm_fbdev_emulation)
483 return -ENODEV;
484
5ea1f752
RC
485 drm_modeset_lock_all(dev);
486 ret = restore_fbdev_mode(fb_helper);
e2809c7d
DA
487
488 do_delayed = fb_helper->delayed_hotplug;
489 if (do_delayed)
490 fb_helper->delayed_hotplug = false;
5ea1f752 491 drm_modeset_unlock_all(dev);
e2809c7d
DA
492
493 if (do_delayed)
494 drm_fb_helper_hotplug_event(fb_helper);
5ea1f752
RC
495 return ret;
496}
497EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
e8e7a2b8 498
2c4124fd
GU
499static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
500{
501 struct drm_device *dev = fb_helper->dev;
502 struct drm_crtc *crtc;
503 int bound = 0, crtcs_bound = 0;
504
4b4f99f5
TR
505 /*
506 * Sometimes user space wants everything disabled, so don't steal the
507 * display if there's a master.
508 */
f17b3ea3 509 if (READ_ONCE(dev->master))
2c4124fd
GU
510 return false;
511
512 drm_for_each_crtc(crtc, dev) {
513 if (crtc->primary->fb)
514 crtcs_bound++;
515 if (crtc->primary->fb == fb_helper->fb)
516 bound++;
517 }
518
519 if (bound < crtcs_bound)
520 return false;
521
522 return true;
523}
524
525#ifdef CONFIG_MAGIC_SYSRQ
d21bf469
DV
526/*
527 * restore fbcon display for all kms driver's using this helper, used for sysrq
528 * and panic handling.
529 */
78b9c353 530static bool drm_fb_helper_force_kernel_mode(void)
785b93ef 531{
785b93ef
DA
532 bool ret, error = false;
533 struct drm_fb_helper *helper;
534
535 if (list_empty(&kernel_fb_helper_list))
536 return false;
537
538 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
b77f0765
TR
539 struct drm_device *dev = helper->dev;
540
541 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
542 continue;
543
dd908c86 544 drm_modeset_lock_all(dev);
3d9e35a9 545 ret = restore_fbdev_mode(helper);
e8e7a2b8
DA
546 if (ret)
547 error = true;
cb597bb3 548 drm_modeset_unlock_all(dev);
785b93ef
DA
549 }
550 return error;
551}
552
785b93ef
DA
553static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
554{
d21bf469 555 bool ret;
4b4f99f5 556
d21bf469
DV
557 ret = drm_fb_helper_force_kernel_mode();
558 if (ret == true)
559 DRM_ERROR("Failed to restore crtc configuration\n");
785b93ef
DA
560}
561static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
562
1495cc9d 563static void drm_fb_helper_sysrq(int dummy1)
785b93ef
DA
564{
565 schedule_work(&drm_fb_helper_restore_work);
566}
567
568static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
569 .handler = drm_fb_helper_sysrq,
570 .help_msg = "force-fb(V)",
571 .action_msg = "Restore framebuffer console",
572};
b8c40d62
RD
573#else
574static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
bea1d35b 575#endif
785b93ef 576
3a8148c5 577static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
785b93ef
DA
578{
579 struct drm_fb_helper *fb_helper = info->par;
580 struct drm_device *dev = fb_helper->dev;
581 struct drm_crtc *crtc;
023eb571 582 struct drm_connector *connector;
023eb571 583 int i, j;
785b93ef
DA
584
585 /*
3a8148c5 586 * For each CRTC in this fb, turn the connectors on/off.
785b93ef 587 */
84849903 588 drm_modeset_lock_all(dev);
20c60c35
DV
589 if (!drm_fb_helper_is_bound(fb_helper)) {
590 drm_modeset_unlock_all(dev);
591 return;
592 }
593
e87b2c42 594 for (i = 0; i < fb_helper->crtc_count; i++) {
8be48d92 595 crtc = fb_helper->crtc_info[i].mode_set.crtc;
785b93ef 596
8be48d92
DA
597 if (!crtc->enabled)
598 continue;
599
3a8148c5 600 /* Walk the connectors & encoders on this fb turning them on/off */
966a6a13 601 drm_fb_helper_for_each_connector(fb_helper, j) {
023eb571 602 connector = fb_helper->connector_info[j]->connector;
e04190e0 603 connector->funcs->dpms(connector, dpms_mode);
58495563 604 drm_object_property_set_value(&connector->base,
3a8148c5 605 dev->mode_config.dpms_property, dpms_mode);
785b93ef 606 }
785b93ef 607 }
84849903 608 drm_modeset_unlock_all(dev);
785b93ef
DA
609}
610
207fd329 611/**
6806cdf9 612 * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
207fd329
DV
613 * @blank: desired blanking state
614 * @info: fbdev registered by the helper
615 */
785b93ef
DA
616int drm_fb_helper_blank(int blank, struct fb_info *info)
617{
c50bfd08
DV
618 if (oops_in_progress)
619 return -EBUSY;
620
785b93ef 621 switch (blank) {
731b5a15 622 /* Display: On; HSync: On, VSync: On */
785b93ef 623 case FB_BLANK_UNBLANK:
3a8148c5 624 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
785b93ef 625 break;
731b5a15 626 /* Display: Off; HSync: On, VSync: On */
785b93ef 627 case FB_BLANK_NORMAL:
3a8148c5 628 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
785b93ef 629 break;
731b5a15 630 /* Display: Off; HSync: Off, VSync: On */
785b93ef 631 case FB_BLANK_HSYNC_SUSPEND:
3a8148c5 632 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
785b93ef 633 break;
731b5a15 634 /* Display: Off; HSync: On, VSync: Off */
785b93ef 635 case FB_BLANK_VSYNC_SUSPEND:
3a8148c5 636 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
785b93ef 637 break;
731b5a15 638 /* Display: Off; HSync: Off, VSync: Off */
785b93ef 639 case FB_BLANK_POWERDOWN:
3a8148c5 640 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
785b93ef
DA
641 break;
642 }
643 return 0;
644}
645EXPORT_SYMBOL(drm_fb_helper_blank);
646
a2889606
VS
647static void drm_fb_helper_modeset_release(struct drm_fb_helper *helper,
648 struct drm_mode_set *modeset)
649{
650 int i;
651
652 for (i = 0; i < modeset->num_connectors; i++) {
ad093607 653 drm_connector_put(modeset->connectors[i]);
a2889606
VS
654 modeset->connectors[i] = NULL;
655 }
656 modeset->num_connectors = 0;
657
658 drm_mode_destroy(helper->dev, modeset->mode);
659 modeset->mode = NULL;
660
661 /* FIXME should hold a ref? */
662 modeset->fb = NULL;
663}
664
785b93ef
DA
665static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
666{
667 int i;
668
6e86d58b 669 for (i = 0; i < helper->connector_count; i++) {
ad093607 670 drm_connector_put(helper->connector_info[i]->connector);
0b4c0f3f 671 kfree(helper->connector_info[i]);
6e86d58b 672 }
0b4c0f3f 673 kfree(helper->connector_info);
a2889606 674
a1b7736d 675 for (i = 0; i < helper->crtc_count; i++) {
a2889606
VS
676 struct drm_mode_set *modeset = &helper->crtc_info[i].mode_set;
677
678 drm_fb_helper_modeset_release(helper, modeset);
679 kfree(modeset->connectors);
a1b7736d 680 }
785b93ef
DA
681 kfree(helper->crtc_info);
682}
683
cfe63423
NT
684static void drm_fb_helper_resume_worker(struct work_struct *work)
685{
686 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
687 resume_work);
688
689 console_lock();
690 fb_set_suspend(helper->fbdev, 0);
691 console_unlock();
692}
693
eaa434de
NT
694static void drm_fb_helper_dirty_work(struct work_struct *work)
695{
696 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
697 dirty_work);
698 struct drm_clip_rect *clip = &helper->dirty_clip;
699 struct drm_clip_rect clip_copy;
700 unsigned long flags;
701
702 spin_lock_irqsave(&helper->dirty_lock, flags);
703 clip_copy = *clip;
704 clip->x1 = clip->y1 = ~0;
705 clip->x2 = clip->y2 = 0;
706 spin_unlock_irqrestore(&helper->dirty_lock, flags);
707
87d3b658
TI
708 /* call dirty callback only when it has been really touched */
709 if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2)
710 helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
eaa434de
NT
711}
712
10a23102
TR
713/**
714 * drm_fb_helper_prepare - setup a drm_fb_helper structure
715 * @dev: DRM device
716 * @helper: driver-allocated fbdev helper structure to set up
717 * @funcs: pointer to structure of functions associate with this helper
718 *
719 * Sets up the bare minimum to make the framebuffer helper usable. This is
720 * useful to implement race-free initialization of the polling helpers.
721 */
722void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
723 const struct drm_fb_helper_funcs *funcs)
724{
725 INIT_LIST_HEAD(&helper->kernel_fb_list);
eaa434de 726 spin_lock_init(&helper->dirty_lock);
cfe63423 727 INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
eaa434de
NT
728 INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work);
729 helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0;
10a23102
TR
730 helper->funcs = funcs;
731 helper->dev = dev;
732}
733EXPORT_SYMBOL(drm_fb_helper_prepare);
734
207fd329 735/**
ed84e254 736 * drm_fb_helper_init - initialize a &struct drm_fb_helper
207fd329
DV
737 * @dev: drm device
738 * @fb_helper: driver-allocated fbdev helper structure to initialize
207fd329
DV
739 * @max_conn_count: max connector count
740 *
741 * This allocates the structures for the fbdev helper with the given limits.
742 * Note that this won't yet touch the hardware (through the driver interfaces)
743 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
744 * to allow driver writes more control over the exact init sequence.
745 *
10a23102 746 * Drivers must call drm_fb_helper_prepare() before calling this function.
207fd329
DV
747 *
748 * RETURNS:
749 * Zero if everything went ok, nonzero otherwise.
750 */
4abe3520
DA
751int drm_fb_helper_init(struct drm_device *dev,
752 struct drm_fb_helper *fb_helper,
e4563f6b 753 int max_conn_count)
785b93ef 754{
785b93ef 755 struct drm_crtc *crtc;
e4563f6b 756 struct drm_mode_config *config = &dev->mode_config;
785b93ef
DA
757 int i;
758
f64c5573
DV
759 if (!drm_fbdev_emulation)
760 return 0;
761
04cfe97e
XL
762 if (!max_conn_count)
763 return -EINVAL;
764
e4563f6b 765 fb_helper->crtc_info = kcalloc(config->num_crtc, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
4abe3520 766 if (!fb_helper->crtc_info)
785b93ef
DA
767 return -ENOMEM;
768
e4563f6b 769 fb_helper->crtc_count = config->num_crtc;
4abe3520
DA
770 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
771 if (!fb_helper->connector_info) {
772 kfree(fb_helper->crtc_info);
0b4c0f3f
DA
773 return -ENOMEM;
774 }
65c2a89c 775 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
4abe3520 776 fb_helper->connector_count = 0;
785b93ef 777
e4563f6b 778 for (i = 0; i < fb_helper->crtc_count; i++) {
4abe3520 779 fb_helper->crtc_info[i].mode_set.connectors =
785b93ef
DA
780 kcalloc(max_conn_count,
781 sizeof(struct drm_connector *),
782 GFP_KERNEL);
783
4a1b0714 784 if (!fb_helper->crtc_info[i].mode_set.connectors)
785b93ef 785 goto out_free;
4abe3520 786 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
785b93ef
DA
787 }
788
789 i = 0;
6295d607 790 drm_for_each_crtc(crtc, dev) {
4abe3520 791 fb_helper->crtc_info[i].mode_set.crtc = crtc;
785b93ef
DA
792 i++;
793 }
e9ad3181 794
785b93ef
DA
795 return 0;
796out_free:
4abe3520 797 drm_fb_helper_crtc_free(fb_helper);
785b93ef
DA
798 return -ENOMEM;
799}
4abe3520
DA
800EXPORT_SYMBOL(drm_fb_helper_init);
801
b8017d6c
AT
802/**
803 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
804 * @fb_helper: driver-allocated fbdev helper
805 *
806 * A helper to alloc fb_info and the members cmap and apertures. Called
da7bdda2
DV
807 * by the driver within the fb_probe fb_helper callback function. Drivers do not
808 * need to release the allocated fb_info structure themselves, this is
809 * automatically done when calling drm_fb_helper_fini().
b8017d6c
AT
810 *
811 * RETURNS:
812 * fb_info pointer if things went okay, pointer containing error code
813 * otherwise
814 */
815struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
816{
817 struct device *dev = fb_helper->dev->dev;
818 struct fb_info *info;
819 int ret;
820
821 info = framebuffer_alloc(0, dev);
822 if (!info)
823 return ERR_PTR(-ENOMEM);
824
825 ret = fb_alloc_cmap(&info->cmap, 256, 0);
826 if (ret)
827 goto err_release;
828
829 info->apertures = alloc_apertures(1);
830 if (!info->apertures) {
831 ret = -ENOMEM;
832 goto err_free_cmap;
833 }
834
835 fb_helper->fbdev = info;
836
837 return info;
838
839err_free_cmap:
840 fb_dealloc_cmap(&info->cmap);
841err_release:
842 framebuffer_release(info);
843 return ERR_PTR(ret);
844}
845EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
846
847/**
848 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
849 * @fb_helper: driver-allocated fbdev helper
850 *
851 * A wrapper around unregister_framebuffer, to release the fb_info
ed84e254
DV
852 * framebuffer device. This must be called before releasing all resources for
853 * @fb_helper by calling drm_fb_helper_fini().
b8017d6c
AT
854 */
855void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
856{
857 if (fb_helper && fb_helper->fbdev)
858 unregister_framebuffer(fb_helper->fbdev);
859}
860EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
861
ed84e254
DV
862/**
863 * drm_fb_helper_fini - finialize a &struct drm_fb_helper
864 * @fb_helper: driver-allocated fbdev helper
865 *
866 * This cleans up all remaining resources associated with @fb_helper. Must be
867 * called after drm_fb_helper_unlink_fbi() was called.
868 */
4abe3520
DA
869void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
870{
da7bdda2
DV
871 struct fb_info *info;
872
873 if (!drm_fbdev_emulation || !fb_helper)
f64c5573
DV
874 return;
875
da7bdda2
DV
876 info = fb_helper->fbdev;
877 if (info) {
878 if (info->cmap.len)
879 fb_dealloc_cmap(&info->cmap);
880 framebuffer_release(info);
881 }
882 fb_helper->fbdev = NULL;
883
24f76b2c 884 cancel_work_sync(&fb_helper->resume_work);
f21b9a92
CW
885 cancel_work_sync(&fb_helper->dirty_work);
886
a53ca635 887 mutex_lock(&kernel_fb_helper_lock);
4abe3520
DA
888 if (!list_empty(&fb_helper->kernel_fb_list)) {
889 list_del(&fb_helper->kernel_fb_list);
4b4f99f5 890 if (list_empty(&kernel_fb_helper_list))
4abe3520 891 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
4abe3520 892 }
a53ca635 893 mutex_unlock(&kernel_fb_helper_lock);
4abe3520
DA
894
895 drm_fb_helper_crtc_free(fb_helper);
896
4abe3520
DA
897}
898EXPORT_SYMBOL(drm_fb_helper_fini);
785b93ef 899
47074ab7
AT
900/**
901 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
902 * @fb_helper: driver-allocated fbdev helper
903 *
904 * A wrapper around unlink_framebuffer implemented by fbdev core
905 */
906void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
907{
908 if (fb_helper && fb_helper->fbdev)
909 unlink_framebuffer(fb_helper->fbdev);
910}
911EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
912
eaa434de
NT
913static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
914 u32 width, u32 height)
915{
916 struct drm_fb_helper *helper = info->par;
917 struct drm_clip_rect *clip = &helper->dirty_clip;
918 unsigned long flags;
919
920 if (!helper->fb->funcs->dirty)
921 return;
922
923 spin_lock_irqsave(&helper->dirty_lock, flags);
924 clip->x1 = min_t(u32, clip->x1, x);
925 clip->y1 = min_t(u32, clip->y1, y);
926 clip->x2 = max_t(u32, clip->x2, x + width);
927 clip->y2 = max_t(u32, clip->y2, y + height);
928 spin_unlock_irqrestore(&helper->dirty_lock, flags);
929
930 schedule_work(&helper->dirty_work);
931}
932
933/**
934 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
935 * @info: fb_info struct pointer
936 * @pagelist: list of dirty mmap framebuffer pages
937 *
6806cdf9 938 * This function is used as the &fb_deferred_io.deferred_io
eaa434de
NT
939 * callback function for flushing the fbdev mmap writes.
940 */
941void drm_fb_helper_deferred_io(struct fb_info *info,
942 struct list_head *pagelist)
943{
944 unsigned long start, end, min, max;
945 struct page *page;
946 u32 y1, y2;
947
948 min = ULONG_MAX;
949 max = 0;
950 list_for_each_entry(page, pagelist, lru) {
951 start = page->index << PAGE_SHIFT;
952 end = start + PAGE_SIZE - 1;
953 min = min(min, start);
954 max = max(max, end);
955 }
956
957 if (min < max) {
958 y1 = min / info->fix.line_length;
959 y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
960 info->var.yres);
961 drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
962 }
963}
964EXPORT_SYMBOL(drm_fb_helper_deferred_io);
965
cbb1a82e
AT
966/**
967 * drm_fb_helper_sys_read - wrapper around fb_sys_read
968 * @info: fb_info struct pointer
969 * @buf: userspace buffer to read from framebuffer memory
970 * @count: number of bytes to read from framebuffer memory
971 * @ppos: read offset within framebuffer memory
972 *
973 * A wrapper around fb_sys_read implemented by fbdev core
974 */
975ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
976 size_t count, loff_t *ppos)
977{
978 return fb_sys_read(info, buf, count, ppos);
979}
980EXPORT_SYMBOL(drm_fb_helper_sys_read);
981
982/**
983 * drm_fb_helper_sys_write - wrapper around fb_sys_write
984 * @info: fb_info struct pointer
985 * @buf: userspace buffer to write to framebuffer memory
986 * @count: number of bytes to write to framebuffer memory
987 * @ppos: write offset within framebuffer memory
988 *
989 * A wrapper around fb_sys_write implemented by fbdev core
990 */
991ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
992 size_t count, loff_t *ppos)
993{
eaa434de
NT
994 ssize_t ret;
995
996 ret = fb_sys_write(info, buf, count, ppos);
997 if (ret > 0)
998 drm_fb_helper_dirty(info, 0, 0, info->var.xres,
999 info->var.yres);
1000
1001 return ret;
cbb1a82e
AT
1002}
1003EXPORT_SYMBOL(drm_fb_helper_sys_write);
1004
742547b7
AT
1005/**
1006 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
1007 * @info: fbdev registered by the helper
1008 * @rect: info about rectangle to fill
1009 *
1010 * A wrapper around sys_fillrect implemented by fbdev core
1011 */
1012void drm_fb_helper_sys_fillrect(struct fb_info *info,
1013 const struct fb_fillrect *rect)
1014{
1015 sys_fillrect(info, rect);
eaa434de
NT
1016 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1017 rect->width, rect->height);
742547b7
AT
1018}
1019EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
1020
1021/**
1022 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
1023 * @info: fbdev registered by the helper
1024 * @area: info about area to copy
1025 *
1026 * A wrapper around sys_copyarea implemented by fbdev core
1027 */
1028void drm_fb_helper_sys_copyarea(struct fb_info *info,
1029 const struct fb_copyarea *area)
1030{
1031 sys_copyarea(info, area);
eaa434de
NT
1032 drm_fb_helper_dirty(info, area->dx, area->dy,
1033 area->width, area->height);
742547b7
AT
1034}
1035EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
1036
1037/**
1038 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
1039 * @info: fbdev registered by the helper
1040 * @image: info about image to blit
1041 *
1042 * A wrapper around sys_imageblit implemented by fbdev core
1043 */
1044void drm_fb_helper_sys_imageblit(struct fb_info *info,
1045 const struct fb_image *image)
1046{
1047 sys_imageblit(info, image);
eaa434de
NT
1048 drm_fb_helper_dirty(info, image->dx, image->dy,
1049 image->width, image->height);
742547b7
AT
1050}
1051EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
1052
1053/**
1054 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
1055 * @info: fbdev registered by the helper
1056 * @rect: info about rectangle to fill
1057 *
1058 * A wrapper around cfb_imageblit implemented by fbdev core
1059 */
1060void drm_fb_helper_cfb_fillrect(struct fb_info *info,
1061 const struct fb_fillrect *rect)
1062{
1063 cfb_fillrect(info, rect);
eaa434de
NT
1064 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1065 rect->width, rect->height);
742547b7
AT
1066}
1067EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
1068
1069/**
1070 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
1071 * @info: fbdev registered by the helper
1072 * @area: info about area to copy
1073 *
1074 * A wrapper around cfb_copyarea implemented by fbdev core
1075 */
1076void drm_fb_helper_cfb_copyarea(struct fb_info *info,
1077 const struct fb_copyarea *area)
1078{
1079 cfb_copyarea(info, area);
eaa434de
NT
1080 drm_fb_helper_dirty(info, area->dx, area->dy,
1081 area->width, area->height);
742547b7
AT
1082}
1083EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
1084
1085/**
1086 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
1087 * @info: fbdev registered by the helper
1088 * @image: info about image to blit
1089 *
1090 * A wrapper around cfb_imageblit implemented by fbdev core
1091 */
1092void drm_fb_helper_cfb_imageblit(struct fb_info *info,
1093 const struct fb_image *image)
1094{
1095 cfb_imageblit(info, image);
eaa434de
NT
1096 drm_fb_helper_dirty(info, image->dx, image->dy,
1097 image->width, image->height);
742547b7
AT
1098}
1099EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
1100
fdefa58a
AT
1101/**
1102 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
1103 * @fb_helper: driver-allocated fbdev helper
28579f37 1104 * @suspend: whether to suspend or resume
fdefa58a 1105 *
cfe63423
NT
1106 * A wrapper around fb_set_suspend implemented by fbdev core.
1107 * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
1108 * the lock yourself
fdefa58a 1109 */
28579f37 1110void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
fdefa58a
AT
1111{
1112 if (fb_helper && fb_helper->fbdev)
28579f37 1113 fb_set_suspend(fb_helper->fbdev, suspend);
fdefa58a
AT
1114}
1115EXPORT_SYMBOL(drm_fb_helper_set_suspend);
1116
cfe63423
NT
1117/**
1118 * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
1119 * takes the console lock
1120 * @fb_helper: driver-allocated fbdev helper
28579f37 1121 * @suspend: whether to suspend or resume
cfe63423
NT
1122 *
1123 * A wrapper around fb_set_suspend() that takes the console lock. If the lock
1124 * isn't available on resume, a worker is tasked with waiting for the lock
1125 * to become available. The console lock can be pretty contented on resume
1126 * due to all the printk activity.
1127 *
1128 * This function can be called multiple times with the same state since
6806cdf9 1129 * &fb_info.state is checked to see if fbdev is running or not before locking.
cfe63423
NT
1130 *
1131 * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
1132 */
1133void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
28579f37 1134 bool suspend)
cfe63423
NT
1135{
1136 if (!fb_helper || !fb_helper->fbdev)
1137 return;
1138
1139 /* make sure there's no pending/ongoing resume */
1140 flush_work(&fb_helper->resume_work);
1141
1142 if (suspend) {
1143 if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING)
1144 return;
1145
1146 console_lock();
1147
1148 } else {
1149 if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING)
1150 return;
1151
1152 if (!console_trylock()) {
1153 schedule_work(&fb_helper->resume_work);
1154 return;
1155 }
1156 }
1157
1158 fb_set_suspend(fb_helper->fbdev, suspend);
1159 console_unlock();
1160}
1161EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked);
1162
c850cb78 1163static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
b8c00ac5
DA
1164 u16 blue, u16 regno, struct fb_info *info)
1165{
1166 struct drm_fb_helper *fb_helper = info->par;
1167 struct drm_framebuffer *fb = fb_helper->fb;
b8c00ac5 1168
c850cb78
DA
1169 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
1170 u32 *palette;
1171 u32 value;
1172 /* place color in psuedopalette */
1173 if (regno > 16)
1174 return -EINVAL;
1175 palette = (u32 *)info->pseudo_palette;
1176 red >>= (16 - info->var.red.length);
1177 green >>= (16 - info->var.green.length);
1178 blue >>= (16 - info->var.blue.length);
1179 value = (red << info->var.red.offset) |
1180 (green << info->var.green.offset) |
1181 (blue << info->var.blue.offset);
9da12b6a
RC
1182 if (info->var.transp.length > 0) {
1183 u32 mask = (1 << info->var.transp.length) - 1;
4b4f99f5 1184
9da12b6a
RC
1185 mask <<= info->var.transp.offset;
1186 value |= mask;
1187 }
c850cb78
DA
1188 palette[regno] = value;
1189 return 0;
1190 }
1191
04c0c569
VS
1192 /*
1193 * The driver really shouldn't advertise pseudo/directcolor
1194 * visuals if it can't deal with the palette.
1195 */
1196 if (WARN_ON(!fb_helper->funcs->gamma_set ||
1197 !fb_helper->funcs->gamma_get))
1198 return -EINVAL;
1199
272725c7 1200 WARN_ON(fb->format->cpp[0] != 1);
b8c00ac5 1201
fef1480d 1202 fb_helper->funcs->gamma_set(crtc, red, green, blue, regno);
b8c00ac5 1203
c850cb78 1204 return 0;
b8c00ac5
DA
1205}
1206
207fd329 1207/**
6806cdf9 1208 * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
207fd329
DV
1209 * @cmap: cmap to set
1210 * @info: fbdev registered by the helper
1211 */
068143d3
DA
1212int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1213{
1214 struct drm_fb_helper *fb_helper = info->par;
8391a3d5 1215 struct drm_device *dev = fb_helper->dev;
be26a66d 1216 const struct drm_crtc_helper_funcs *crtc_funcs;
068143d3
DA
1217 u16 *red, *green, *blue, *transp;
1218 struct drm_crtc *crtc;
062ac622 1219 int i, j, rc = 0;
068143d3
DA
1220 int start;
1221
c50bfd08 1222 if (oops_in_progress)
9aa609e1 1223 return -EBUSY;
c50bfd08
DV
1224
1225 drm_modeset_lock_all(dev);
8391a3d5
VS
1226 if (!drm_fb_helper_is_bound(fb_helper)) {
1227 drm_modeset_unlock_all(dev);
1228 return -EBUSY;
1229 }
1230
8be48d92
DA
1231 for (i = 0; i < fb_helper->crtc_count; i++) {
1232 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1233 crtc_funcs = crtc->helper_private;
068143d3
DA
1234
1235 red = cmap->red;
1236 green = cmap->green;
1237 blue = cmap->blue;
1238 transp = cmap->transp;
1239 start = cmap->start;
1240
062ac622 1241 for (j = 0; j < cmap->len; j++) {
068143d3
DA
1242 u16 hred, hgreen, hblue, htransp = 0xffff;
1243
1244 hred = *red++;
1245 hgreen = *green++;
1246 hblue = *blue++;
1247
1248 if (transp)
1249 htransp = *transp++;
1250
c850cb78
DA
1251 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
1252 if (rc)
8391a3d5 1253 goto out;
068143d3 1254 }
04c0c569
VS
1255 if (crtc_funcs->load_lut)
1256 crtc_funcs->load_lut(crtc);
068143d3 1257 }
8391a3d5
VS
1258 out:
1259 drm_modeset_unlock_all(dev);
068143d3
DA
1260 return rc;
1261}
1262EXPORT_SYMBOL(drm_fb_helper_setcmap);
1263
0f3bbe07
MR
1264/**
1265 * drm_fb_helper_ioctl - legacy ioctl implementation
1266 * @info: fbdev registered by the helper
1267 * @cmd: ioctl command
1268 * @arg: ioctl argument
1269 *
1270 * A helper to implement the standard fbdev ioctl. Only
1271 * FBIO_WAITFORVSYNC is implemented for now.
1272 */
1273int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
1274 unsigned long arg)
1275{
1276 struct drm_fb_helper *fb_helper = info->par;
1277 struct drm_device *dev = fb_helper->dev;
1278 struct drm_mode_set *mode_set;
1279 struct drm_crtc *crtc;
1280 int ret = 0;
1281
1282 mutex_lock(&dev->mode_config.mutex);
1283 if (!drm_fb_helper_is_bound(fb_helper)) {
1284 ret = -EBUSY;
1285 goto unlock;
1286 }
1287
1288 switch (cmd) {
1289 case FBIO_WAITFORVSYNC:
1290 /*
1291 * Only consider the first CRTC.
1292 *
1293 * This ioctl is supposed to take the CRTC number as
1294 * an argument, but in fbdev times, what that number
1295 * was supposed to be was quite unclear, different
1296 * drivers were passing that argument differently
1297 * (some by reference, some by value), and most of the
1298 * userspace applications were just hardcoding 0 as an
1299 * argument.
1300 *
1301 * The first CRTC should be the integrated panel on
1302 * most drivers, so this is the best choice we can
1303 * make. If we're not smart enough here, one should
1304 * just consider switch the userspace to KMS.
1305 */
1306 mode_set = &fb_helper->crtc_info[0].mode_set;
1307 crtc = mode_set->crtc;
1308
1309 /*
1310 * Only wait for a vblank event if the CRTC is
1311 * enabled, otherwise just don't do anythintg,
1312 * not even report an error.
1313 */
1314 ret = drm_crtc_vblank_get(crtc);
1315 if (!ret) {
1316 drm_crtc_wait_one_vblank(crtc);
1317 drm_crtc_vblank_put(crtc);
1318 }
1319
1320 ret = 0;
1321 goto unlock;
1322 default:
1323 ret = -ENOTTY;
1324 }
1325
1326unlock:
1327 mutex_unlock(&dev->mode_config.mutex);
1328 return ret;
1329}
1330EXPORT_SYMBOL(drm_fb_helper_ioctl);
1331
207fd329 1332/**
6806cdf9 1333 * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
207fd329
DV
1334 * @var: screeninfo to check
1335 * @info: fbdev registered by the helper
1336 */
785b93ef
DA
1337int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1338 struct fb_info *info)
1339{
1340 struct drm_fb_helper *fb_helper = info->par;
1341 struct drm_framebuffer *fb = fb_helper->fb;
1342 int depth;
1343
f90ebd9e 1344 if (var->pixclock != 0 || in_dbg_master())
785b93ef
DA
1345 return -EINVAL;
1346
865afb11
SA
1347 /*
1348 * Changes struct fb_var_screeninfo are currently not pushed back
1349 * to KMS, hence fail if different settings are requested.
1350 */
272725c7 1351 if (var->bits_per_pixel != fb->format->cpp[0] * 8 ||
12ffed96
MD
1352 var->xres > fb->width || var->yres > fb->height ||
1353 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
1354 DRM_DEBUG("fb requested width/height/bpp can't fit in current fb "
62fb376e
CW
1355 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1356 var->xres, var->yres, var->bits_per_pixel,
1357 var->xres_virtual, var->yres_virtual,
272725c7 1358 fb->width, fb->height, fb->format->cpp[0] * 8);
785b93ef
DA
1359 return -EINVAL;
1360 }
1361
1362 switch (var->bits_per_pixel) {
1363 case 16:
1364 depth = (var->green.length == 6) ? 16 : 15;
1365 break;
1366 case 32:
1367 depth = (var->transp.length > 0) ? 32 : 24;
1368 break;
1369 default:
1370 depth = var->bits_per_pixel;
1371 break;
1372 }
1373
1374 switch (depth) {
1375 case 8:
1376 var->red.offset = 0;
1377 var->green.offset = 0;
1378 var->blue.offset = 0;
1379 var->red.length = 8;
1380 var->green.length = 8;
1381 var->blue.length = 8;
1382 var->transp.length = 0;
1383 var->transp.offset = 0;
1384 break;
1385 case 15:
1386 var->red.offset = 10;
1387 var->green.offset = 5;
1388 var->blue.offset = 0;
1389 var->red.length = 5;
1390 var->green.length = 5;
1391 var->blue.length = 5;
1392 var->transp.length = 1;
1393 var->transp.offset = 15;
1394 break;
1395 case 16:
1396 var->red.offset = 11;
1397 var->green.offset = 5;
1398 var->blue.offset = 0;
1399 var->red.length = 5;
1400 var->green.length = 6;
1401 var->blue.length = 5;
1402 var->transp.length = 0;
1403 var->transp.offset = 0;
1404 break;
1405 case 24:
1406 var->red.offset = 16;
1407 var->green.offset = 8;
1408 var->blue.offset = 0;
1409 var->red.length = 8;
1410 var->green.length = 8;
1411 var->blue.length = 8;
1412 var->transp.length = 0;
1413 var->transp.offset = 0;
1414 break;
1415 case 32:
1416 var->red.offset = 16;
1417 var->green.offset = 8;
1418 var->blue.offset = 0;
1419 var->red.length = 8;
1420 var->green.length = 8;
1421 var->blue.length = 8;
1422 var->transp.length = 8;
1423 var->transp.offset = 24;
1424 break;
1425 default:
1426 return -EINVAL;
1427 }
1428 return 0;
1429}
1430EXPORT_SYMBOL(drm_fb_helper_check_var);
1431
207fd329 1432/**
6806cdf9 1433 * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
207fd329
DV
1434 * @info: fbdev registered by the helper
1435 *
1436 * This will let fbcon do the mode init and is called at initialization time by
1437 * the fbdev core when registering the driver, and later on through the hotplug
1438 * callback.
1439 */
785b93ef
DA
1440int drm_fb_helper_set_par(struct fb_info *info)
1441{
1442 struct drm_fb_helper *fb_helper = info->par;
785b93ef 1443 struct fb_var_screeninfo *var = &info->var;
785b93ef 1444
c50bfd08
DV
1445 if (oops_in_progress)
1446 return -EBUSY;
1447
5349ef31 1448 if (var->pixclock != 0) {
172e91f5 1449 DRM_ERROR("PIXEL CLOCK SET\n");
785b93ef
DA
1450 return -EINVAL;
1451 }
1452
5ea1f752 1453 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
4abe3520 1454
785b93ef
DA
1455 return 0;
1456}
1457EXPORT_SYMBOL(drm_fb_helper_set_par);
1458
1edf0269 1459static int pan_display_atomic(struct fb_var_screeninfo *var,
a0fb6ad7 1460 struct fb_info *info)
1edf0269
RC
1461{
1462 struct drm_fb_helper *fb_helper = info->par;
1463 struct drm_device *dev = fb_helper->dev;
1464 struct drm_atomic_state *state;
07d3bad6 1465 struct drm_plane *plane;
1edf0269 1466 int i, ret;
4b4f99f5 1467 unsigned int plane_mask;
1edf0269
RC
1468
1469 state = drm_atomic_state_alloc(dev);
1470 if (!state)
1471 return -ENOMEM;
1472
1473 state->acquire_ctx = dev->mode_config.acquire_ctx;
1474retry:
07d3bad6 1475 plane_mask = 0;
4b4f99f5 1476 for (i = 0; i < fb_helper->crtc_count; i++) {
1edf0269
RC
1477 struct drm_mode_set *mode_set;
1478
1479 mode_set = &fb_helper->crtc_info[i].mode_set;
1480
1481 mode_set->x = var->xoffset;
1482 mode_set->y = var->yoffset;
1483
1484 ret = __drm_atomic_helper_set_config(mode_set, state);
1485 if (ret != 0)
1486 goto fail;
07d3bad6
ML
1487
1488 plane = mode_set->crtc->primary;
7118fd9b 1489 plane_mask |= (1 << drm_plane_index(plane));
07d3bad6 1490 plane->old_fb = plane->fb;
1edf0269
RC
1491 }
1492
1493 ret = drm_atomic_commit(state);
1494 if (ret != 0)
1495 goto fail;
1496
1497 info->var.xoffset = var->xoffset;
1498 info->var.yoffset = var->yoffset;
1499
1edf0269 1500fail:
07d3bad6 1501 drm_atomic_clean_old_fb(dev, plane_mask, ret);
a0fb6ad7 1502
1edf0269
RC
1503 if (ret == -EDEADLK)
1504 goto backoff;
1505
0853695c 1506 drm_atomic_state_put(state);
1edf0269
RC
1507 return ret;
1508
1509backoff:
1510 drm_atomic_state_clear(state);
1511 drm_atomic_legacy_backoff(state);
1512
1513 goto retry;
1514}
1515
207fd329 1516/**
6806cdf9 1517 * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
207fd329
DV
1518 * @var: updated screen information
1519 * @info: fbdev registered by the helper
1520 */
785b93ef
DA
1521int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1522 struct fb_info *info)
1523{
1524 struct drm_fb_helper *fb_helper = info->par;
1525 struct drm_device *dev = fb_helper->dev;
1526 struct drm_mode_set *modeset;
785b93ef
DA
1527 int ret = 0;
1528 int i;
1529
c50bfd08 1530 if (oops_in_progress)
9aa609e1 1531 return -EBUSY;
c50bfd08
DV
1532
1533 drm_modeset_lock_all(dev);
20c60c35
DV
1534 if (!drm_fb_helper_is_bound(fb_helper)) {
1535 drm_modeset_unlock_all(dev);
1536 return -EBUSY;
1537 }
1538
a743d758 1539 if (drm_drv_uses_atomic_modeset(dev)) {
1edf0269
RC
1540 ret = pan_display_atomic(var, info);
1541 goto unlock;
1542 }
1543
8be48d92 1544 for (i = 0; i < fb_helper->crtc_count; i++) {
785b93ef
DA
1545 modeset = &fb_helper->crtc_info[i].mode_set;
1546
1547 modeset->x = var->xoffset;
1548 modeset->y = var->yoffset;
1549
1550 if (modeset->num_connectors) {
2d13b679 1551 ret = drm_mode_set_config_internal(modeset);
785b93ef
DA
1552 if (!ret) {
1553 info->var.xoffset = var->xoffset;
1554 info->var.yoffset = var->yoffset;
1555 }
1556 }
1557 }
1edf0269 1558unlock:
84849903 1559 drm_modeset_unlock_all(dev);
785b93ef
DA
1560 return ret;
1561}
1562EXPORT_SYMBOL(drm_fb_helper_pan_display);
1563
8acf658a 1564/*
207fd329
DV
1565 * Allocates the backing storage and sets up the fbdev info structure through
1566 * the ->fb_probe callback and then registers the fbdev and sets up the panic
1567 * notifier.
8acf658a 1568 */
de1ace5b
DV
1569static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1570 int preferred_bpp)
785b93ef 1571{
8acf658a 1572 int ret = 0;
785b93ef 1573 int crtc_count = 0;
4abe3520 1574 int i;
38651674 1575 struct drm_fb_helper_surface_size sizes;
8be48d92 1576 int gamma_size = 0;
38651674
DA
1577
1578 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1579 sizes.surface_depth = 24;
1580 sizes.surface_bpp = 32;
4b4f99f5
TR
1581 sizes.fb_width = (u32)-1;
1582 sizes.fb_height = (u32)-1;
785b93ef 1583
4b4f99f5 1584 /* if driver picks 8 or 16 by default use that for both depth/bpp */
96081cdf 1585 if (preferred_bpp != sizes.surface_bpp)
38651674 1586 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
96081cdf 1587
785b93ef 1588 /* first up get a count of crtcs now in use and new min/maxes width/heights */
966a6a13 1589 drm_fb_helper_for_each_connector(fb_helper, i) {
0b4c0f3f 1590 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
1794d257 1591 struct drm_cmdline_mode *cmdline_mode;
8ef8678c 1592
eaf99c74 1593 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
d50ba256
DA
1594
1595 if (cmdline_mode->bpp_specified) {
1596 switch (cmdline_mode->bpp) {
1597 case 8:
38651674 1598 sizes.surface_depth = sizes.surface_bpp = 8;
d50ba256
DA
1599 break;
1600 case 15:
38651674
DA
1601 sizes.surface_depth = 15;
1602 sizes.surface_bpp = 16;
d50ba256
DA
1603 break;
1604 case 16:
38651674 1605 sizes.surface_depth = sizes.surface_bpp = 16;
d50ba256
DA
1606 break;
1607 case 24:
38651674 1608 sizes.surface_depth = sizes.surface_bpp = 24;
d50ba256
DA
1609 break;
1610 case 32:
38651674
DA
1611 sizes.surface_depth = 24;
1612 sizes.surface_bpp = 32;
d50ba256
DA
1613 break;
1614 }
1615 break;
1616 }
1617 }
1618
8be48d92
DA
1619 crtc_count = 0;
1620 for (i = 0; i < fb_helper->crtc_count; i++) {
1621 struct drm_display_mode *desired_mode;
0e3704c9
RC
1622 struct drm_mode_set *mode_set;
1623 int x, y, j;
1624 /* in case of tile group, are we the last tile vert or horiz?
1625 * If no tile group you are always the last one both vertically
1626 * and horizontally
1627 */
1628 bool lastv = true, lasth = true;
675c8328 1629
8be48d92 1630 desired_mode = fb_helper->crtc_info[i].desired_mode;
0e3704c9 1631 mode_set = &fb_helper->crtc_info[i].mode_set;
675c8328
RC
1632
1633 if (!desired_mode)
1634 continue;
1635
1636 crtc_count++;
1637
b0ee9e7f
DA
1638 x = fb_helper->crtc_info[i].x;
1639 y = fb_helper->crtc_info[i].y;
675c8328
RC
1640
1641 if (gamma_size == 0)
1642 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1643
1644 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1645 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
0e3704c9
RC
1646
1647 for (j = 0; j < mode_set->num_connectors; j++) {
1648 struct drm_connector *connector = mode_set->connectors[j];
4b4f99f5 1649
0e3704c9
RC
1650 if (connector->has_tile) {
1651 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1652 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1653 /* cloning to multiple tiles is just crazy-talk, so: */
1654 break;
1655 }
1656 }
1657
1658 if (lasth)
1659 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1660 if (lastv)
1661 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
785b93ef
DA
1662 }
1663
38651674 1664 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
4b4f99f5
TR
1665 /*
1666 * hmm everyone went away - assume VGA cable just fell out
1667 * and will come back later.
1668 */
eb1f8e4f 1669 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
19b4b445
DA
1670 sizes.fb_width = sizes.surface_width = 1024;
1671 sizes.fb_height = sizes.surface_height = 768;
785b93ef
DA
1672 }
1673
5f152576
XL
1674 /* Handle our overallocation */
1675 sizes.surface_height *= drm_fbdev_overalloc;
1676 sizes.surface_height /= 100;
1677
38651674 1678 /* push down into drivers */
8acf658a
DV
1679 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1680 if (ret < 0)
1681 return ret;
785b93ef 1682
7e53f3a4
DV
1683 /*
1684 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1685 * events, but at init time drm_setup_crtcs needs to be called before
1686 * the fb is allocated (since we need to figure out the desired size of
1687 * the fb before we can allocate it ...). Hence we need to fix things up
1688 * here again.
1689 */
96081cdf 1690 for (i = 0; i < fb_helper->crtc_count; i++)
7e53f3a4
DV
1691 if (fb_helper->crtc_info[i].mode_set.num_connectors)
1692 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1693
785b93ef
DA
1694 return 0;
1695}
785b93ef 1696
207fd329
DV
1697/**
1698 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1699 * @info: fbdev registered by the helper
1700 * @pitch: desired pitch
1701 * @depth: desired depth
1702 *
1703 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1704 * fbdev emulations. Drivers which support acceleration methods which impose
1705 * additional constraints need to set up their own limits.
1706 *
1707 * Drivers should call this (or their equivalent setup code) from their
6806cdf9 1708 * &drm_fb_helper_funcs.fb_probe callback.
207fd329 1709 */
3632ef89
DA
1710void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1711 uint32_t depth)
1712{
1713 info->fix.type = FB_TYPE_PACKED_PIXELS;
1714 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1715 FB_VISUAL_TRUECOLOR;
1716 info->fix.mmio_start = 0;
1717 info->fix.mmio_len = 0;
1718 info->fix.type_aux = 0;
1719 info->fix.xpanstep = 1; /* doing it in hw */
1720 info->fix.ypanstep = 1; /* doing it in hw */
1721 info->fix.ywrapstep = 0;
1722 info->fix.accel = FB_ACCEL_NONE;
3632ef89
DA
1723
1724 info->fix.line_length = pitch;
3632ef89
DA
1725}
1726EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1727
207fd329
DV
1728/**
1729 * drm_fb_helper_fill_var - initalizes variable fbdev information
1730 * @info: fbdev instance to set up
1731 * @fb_helper: fb helper instance to use as template
1732 * @fb_width: desired fb width
1733 * @fb_height: desired fb height
1734 *
1735 * Sets up the variable fbdev metainformation from the given fb helper instance
6806cdf9 1736 * and the drm framebuffer allocated in &drm_fb_helper.fb.
207fd329
DV
1737 *
1738 * Drivers should call this (or their equivalent setup code) from their
6806cdf9
DV
1739 * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
1740 * backing storage framebuffer.
207fd329 1741 */
38651674 1742void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
785b93ef
DA
1743 uint32_t fb_width, uint32_t fb_height)
1744{
38651674 1745 struct drm_framebuffer *fb = fb_helper->fb;
4b4f99f5 1746
38651674 1747 info->pseudo_palette = fb_helper->pseudo_palette;
785b93ef
DA
1748 info->var.xres_virtual = fb->width;
1749 info->var.yres_virtual = fb->height;
272725c7 1750 info->var.bits_per_pixel = fb->format->cpp[0] * 8;
57084d05 1751 info->var.accel_flags = FB_ACCELF_TEXT;
785b93ef
DA
1752 info->var.xoffset = 0;
1753 info->var.yoffset = 0;
1754 info->var.activate = FB_ACTIVATE_NOW;
1755 info->var.height = -1;
1756 info->var.width = -1;
1757
b00c600e 1758 switch (fb->format->depth) {
785b93ef
DA
1759 case 8:
1760 info->var.red.offset = 0;
1761 info->var.green.offset = 0;
1762 info->var.blue.offset = 0;
1763 info->var.red.length = 8; /* 8bit DAC */
1764 info->var.green.length = 8;
1765 info->var.blue.length = 8;
1766 info->var.transp.offset = 0;
1767 info->var.transp.length = 0;
1768 break;
1769 case 15:
1770 info->var.red.offset = 10;
1771 info->var.green.offset = 5;
1772 info->var.blue.offset = 0;
1773 info->var.red.length = 5;
1774 info->var.green.length = 5;
1775 info->var.blue.length = 5;
1776 info->var.transp.offset = 15;
1777 info->var.transp.length = 1;
1778 break;
1779 case 16:
1780 info->var.red.offset = 11;
1781 info->var.green.offset = 5;
1782 info->var.blue.offset = 0;
1783 info->var.red.length = 5;
1784 info->var.green.length = 6;
1785 info->var.blue.length = 5;
1786 info->var.transp.offset = 0;
1787 break;
1788 case 24:
1789 info->var.red.offset = 16;
1790 info->var.green.offset = 8;
1791 info->var.blue.offset = 0;
1792 info->var.red.length = 8;
1793 info->var.green.length = 8;
1794 info->var.blue.length = 8;
1795 info->var.transp.offset = 0;
1796 info->var.transp.length = 0;
1797 break;
1798 case 32:
1799 info->var.red.offset = 16;
1800 info->var.green.offset = 8;
1801 info->var.blue.offset = 0;
1802 info->var.red.length = 8;
1803 info->var.green.length = 8;
1804 info->var.blue.length = 8;
1805 info->var.transp.offset = 24;
1806 info->var.transp.length = 8;
1807 break;
1808 default:
1809 break;
1810 }
1811
1812 info->var.xres = fb_width;
1813 info->var.yres = fb_height;
1814}
1815EXPORT_SYMBOL(drm_fb_helper_fill_var);
38651674 1816
0b4c0f3f
DA
1817static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1818 uint32_t maxX,
1819 uint32_t maxY)
38651674
DA
1820{
1821 struct drm_connector *connector;
1822 int count = 0;
0b4c0f3f 1823 int i;
38651674 1824
966a6a13 1825 drm_fb_helper_for_each_connector(fb_helper, i) {
0b4c0f3f 1826 connector = fb_helper->connector_info[i]->connector;
38651674
DA
1827 count += connector->funcs->fill_modes(connector, maxX, maxY);
1828 }
1829
1830 return count;
1831}
1832
2f1046f3 1833struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
38651674
DA
1834{
1835 struct drm_display_mode *mode;
1836
0b4c0f3f 1837 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
9d3de138
DV
1838 if (mode->hdisplay > width ||
1839 mode->vdisplay > height)
38651674
DA
1840 continue;
1841 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1842 return mode;
1843 }
1844 return NULL;
1845}
2f1046f3 1846EXPORT_SYMBOL(drm_has_preferred_mode);
38651674 1847
0b4c0f3f 1848static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
38651674 1849{
eaf99c74 1850 return fb_connector->connector->cmdline_mode.specified;
38651674
DA
1851}
1852
a09759e8 1853struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn)
38651674 1854{
1794d257 1855 struct drm_cmdline_mode *cmdline_mode;
f3af5c7d 1856 struct drm_display_mode *mode;
c683f427 1857 bool prefer_non_interlace;
38651674 1858
eaf99c74 1859 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
38651674 1860 if (cmdline_mode->specified == false)
f3af5c7d 1861 return NULL;
38651674
DA
1862
1863 /* attempt to find a matching mode in the list of modes
1864 * we have gotten so far, if not add a CVT mode that conforms
1865 */
1866 if (cmdline_mode->rb || cmdline_mode->margins)
1867 goto create_mode;
1868
c683f427 1869 prefer_non_interlace = !cmdline_mode->interlace;
f3af5c7d 1870again:
0b4c0f3f 1871 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
38651674
DA
1872 /* check width/height */
1873 if (mode->hdisplay != cmdline_mode->xres ||
1874 mode->vdisplay != cmdline_mode->yres)
1875 continue;
1876
1877 if (cmdline_mode->refresh_specified) {
1878 if (mode->vrefresh != cmdline_mode->refresh)
1879 continue;
1880 }
1881
1882 if (cmdline_mode->interlace) {
1883 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1884 continue;
c683f427
TI
1885 } else if (prefer_non_interlace) {
1886 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1887 continue;
38651674
DA
1888 }
1889 return mode;
1890 }
1891
c683f427
TI
1892 if (prefer_non_interlace) {
1893 prefer_non_interlace = false;
1894 goto again;
1895 }
1896
38651674 1897create_mode:
1794d257
CW
1898 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1899 cmdline_mode);
0b4c0f3f 1900 list_add(&mode->head, &fb_helper_conn->connector->modes);
38651674
DA
1901 return mode;
1902}
2f1046f3 1903EXPORT_SYMBOL(drm_pick_cmdline_mode);
38651674
DA
1904
1905static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1906{
1907 bool enable;
1908
96081cdf 1909 if (strict)
38651674 1910 enable = connector->status == connector_status_connected;
96081cdf 1911 else
38651674 1912 enable = connector->status != connector_status_disconnected;
96081cdf 1913
38651674
DA
1914 return enable;
1915}
1916
0b4c0f3f
DA
1917static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1918 bool *enabled)
38651674
DA
1919{
1920 bool any_enabled = false;
1921 struct drm_connector *connector;
1922 int i = 0;
1923
966a6a13 1924 drm_fb_helper_for_each_connector(fb_helper, i) {
0b4c0f3f 1925 connector = fb_helper->connector_info[i]->connector;
38651674
DA
1926 enabled[i] = drm_connector_enabled(connector, true);
1927 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1928 enabled[i] ? "yes" : "no");
1929 any_enabled |= enabled[i];
38651674
DA
1930 }
1931
1932 if (any_enabled)
1933 return;
1934
966a6a13 1935 drm_fb_helper_for_each_connector(fb_helper, i) {
0b4c0f3f 1936 connector = fb_helper->connector_info[i]->connector;
38651674 1937 enabled[i] = drm_connector_enabled(connector, false);
38651674
DA
1938 }
1939}
1940
1d42bbc8
DA
1941static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1942 struct drm_display_mode **modes,
b0ee9e7f 1943 struct drm_fb_offset *offsets,
1d42bbc8
DA
1944 bool *enabled, int width, int height)
1945{
1946 int count, i, j;
1947 bool can_clone = false;
1948 struct drm_fb_helper_connector *fb_helper_conn;
1949 struct drm_display_mode *dmt_mode, *mode;
1950
1951 /* only contemplate cloning in the single crtc case */
1952 if (fb_helper->crtc_count > 1)
1953 return false;
1954
1955 count = 0;
966a6a13 1956 drm_fb_helper_for_each_connector(fb_helper, i) {
1d42bbc8
DA
1957 if (enabled[i])
1958 count++;
1959 }
1960
1961 /* only contemplate cloning if more than one connector is enabled */
1962 if (count <= 1)
1963 return false;
1964
1965 /* check the command line or if nothing common pick 1024x768 */
1966 can_clone = true;
966a6a13 1967 drm_fb_helper_for_each_connector(fb_helper, i) {
1d42bbc8
DA
1968 if (!enabled[i])
1969 continue;
1970 fb_helper_conn = fb_helper->connector_info[i];
a09759e8 1971 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
1d42bbc8
DA
1972 if (!modes[i]) {
1973 can_clone = false;
1974 break;
1975 }
1976 for (j = 0; j < i; j++) {
1977 if (!enabled[j])
1978 continue;
1979 if (!drm_mode_equal(modes[j], modes[i]))
1980 can_clone = false;
1981 }
1982 }
1983
1984 if (can_clone) {
1985 DRM_DEBUG_KMS("can clone using command line\n");
1986 return true;
1987 }
1988
1989 /* try and find a 1024x768 mode on each connector */
1990 can_clone = true;
f6e252ba 1991 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
1d42bbc8 1992
966a6a13 1993 drm_fb_helper_for_each_connector(fb_helper, i) {
1d42bbc8
DA
1994 if (!enabled[i])
1995 continue;
1996
1997 fb_helper_conn = fb_helper->connector_info[i];
1998 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1999 if (drm_mode_equal(mode, dmt_mode))
2000 modes[i] = mode;
2001 }
2002 if (!modes[i])
2003 can_clone = false;
2004 }
2005
2006 if (can_clone) {
2007 DRM_DEBUG_KMS("can clone using 1024x768\n");
2008 return true;
2009 }
2010 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
2011 return false;
2012}
2013
b0ee9e7f
DA
2014static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
2015 struct drm_display_mode **modes,
2016 struct drm_fb_offset *offsets,
2017 int idx,
2018 int h_idx, int v_idx)
2019{
2020 struct drm_fb_helper_connector *fb_helper_conn;
2021 int i;
2022 int hoffset = 0, voffset = 0;
2023
966a6a13 2024 drm_fb_helper_for_each_connector(fb_helper, i) {
b0ee9e7f
DA
2025 fb_helper_conn = fb_helper->connector_info[i];
2026 if (!fb_helper_conn->connector->has_tile)
2027 continue;
2028
2029 if (!modes[i] && (h_idx || v_idx)) {
2030 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
2031 fb_helper_conn->connector->base.id);
2032 continue;
2033 }
2034 if (fb_helper_conn->connector->tile_h_loc < h_idx)
2035 hoffset += modes[i]->hdisplay;
2036
2037 if (fb_helper_conn->connector->tile_v_loc < v_idx)
2038 voffset += modes[i]->vdisplay;
2039 }
2040 offsets[idx].x = hoffset;
2041 offsets[idx].y = voffset;
2042 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
2043 return 0;
2044}
2045
0b4c0f3f 2046static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
38651674 2047 struct drm_display_mode **modes,
b0ee9e7f 2048 struct drm_fb_offset *offsets,
38651674
DA
2049 bool *enabled, int width, int height)
2050{
0b4c0f3f 2051 struct drm_fb_helper_connector *fb_helper_conn;
c96521ee
CW
2052 const u64 mask = BIT_ULL(fb_helper->connector_count) - 1;
2053 u64 conn_configured = 0;
b0ee9e7f 2054 int tile_pass = 0;
c96521ee
CW
2055 int i;
2056
b0ee9e7f 2057retry:
966a6a13 2058 drm_fb_helper_for_each_connector(fb_helper, i) {
0b4c0f3f 2059 fb_helper_conn = fb_helper->connector_info[i];
38651674 2060
c96521ee 2061 if (conn_configured & BIT_ULL(i))
b0ee9e7f
DA
2062 continue;
2063
2064 if (enabled[i] == false) {
c96521ee 2065 conn_configured |= BIT_ULL(i);
b0ee9e7f
DA
2066 continue;
2067 }
2068
2069 /* first pass over all the untiled connectors */
2070 if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
38651674 2071 continue;
38651674 2072
b0ee9e7f
DA
2073 if (tile_pass == 1) {
2074 if (fb_helper_conn->connector->tile_h_loc != 0 ||
2075 fb_helper_conn->connector->tile_v_loc != 0)
2076 continue;
2077
2078 } else {
4b4f99f5 2079 if (fb_helper_conn->connector->tile_h_loc != tile_pass - 1 &&
b0ee9e7f
DA
2080 fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
2081 /* if this tile_pass doesn't cover any of the tiles - keep going */
2082 continue;
2083
4b4f99f5
TR
2084 /*
2085 * find the tile offsets for this pass - need to find
2086 * all tiles left and above
2087 */
b0ee9e7f
DA
2088 drm_get_tile_offsets(fb_helper, modes, offsets,
2089 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
2090 }
38651674 2091 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
0b4c0f3f 2092 fb_helper_conn->connector->base.id);
38651674
DA
2093
2094 /* got for command line mode first */
a09759e8 2095 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
38651674 2096 if (!modes[i]) {
b0ee9e7f
DA
2097 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
2098 fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0);
0b4c0f3f 2099 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
38651674
DA
2100 }
2101 /* No preferred modes, pick one off the list */
0b4c0f3f
DA
2102 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
2103 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
38651674
DA
2104 break;
2105 }
2106 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
2107 "none");
c96521ee 2108 conn_configured |= BIT_ULL(i);
b0ee9e7f
DA
2109 }
2110
2111 if ((conn_configured & mask) != mask) {
2112 tile_pass++;
2113 goto retry;
38651674
DA
2114 }
2115 return true;
2116}
2117
8be48d92
DA
2118static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
2119 struct drm_fb_helper_crtc **best_crtcs,
38651674
DA
2120 struct drm_display_mode **modes,
2121 int n, int width, int height)
2122{
2123 int c, o;
2124 struct drm_connector *connector;
be26a66d 2125 const struct drm_connector_helper_funcs *connector_funcs;
38651674 2126 struct drm_encoder *encoder;
38651674 2127 int my_score, best_score, score;
8be48d92 2128 struct drm_fb_helper_crtc **crtcs, *crtc;
0b4c0f3f 2129 struct drm_fb_helper_connector *fb_helper_conn;
38651674 2130
0b4c0f3f 2131 if (n == fb_helper->connector_count)
38651674 2132 return 0;
0b4c0f3f
DA
2133
2134 fb_helper_conn = fb_helper->connector_info[n];
2135 connector = fb_helper_conn->connector;
38651674
DA
2136
2137 best_crtcs[n] = NULL;
8be48d92 2138 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
38651674
DA
2139 if (modes[n] == NULL)
2140 return best_score;
2141
255f0e7c 2142 crtcs = kzalloc(fb_helper->connector_count *
8be48d92 2143 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
38651674
DA
2144 if (!crtcs)
2145 return best_score;
2146
2147 my_score = 1;
2148 if (connector->status == connector_status_connected)
2149 my_score++;
0b4c0f3f 2150 if (drm_has_cmdline_mode(fb_helper_conn))
38651674 2151 my_score++;
0b4c0f3f 2152 if (drm_has_preferred_mode(fb_helper_conn, width, height))
38651674
DA
2153 my_score++;
2154
2155 connector_funcs = connector->helper_private;
c61b93fe
BB
2156
2157 /*
2158 * If the DRM device implements atomic hooks and ->best_encoder() is
2159 * NULL we fallback to the default drm_atomic_helper_best_encoder()
2160 * helper.
2161 */
a743d758 2162 if (drm_drv_uses_atomic_modeset(fb_helper->dev) &&
c61b93fe
BB
2163 !connector_funcs->best_encoder)
2164 encoder = drm_atomic_helper_best_encoder(connector);
2165 else
2166 encoder = connector_funcs->best_encoder(connector);
2167
38651674
DA
2168 if (!encoder)
2169 goto out;
2170
4b4f99f5
TR
2171 /*
2172 * select a crtc for this connector and then attempt to configure
2173 * remaining connectors
2174 */
8be48d92
DA
2175 for (c = 0; c < fb_helper->crtc_count; c++) {
2176 crtc = &fb_helper->crtc_info[c];
38651674 2177
96081cdf 2178 if ((encoder->possible_crtcs & (1 << c)) == 0)
38651674 2179 continue;
38651674
DA
2180
2181 for (o = 0; o < n; o++)
2182 if (best_crtcs[o] == crtc)
2183 break;
2184
2185 if (o < n) {
1d42bbc8
DA
2186 /* ignore cloning unless only a single crtc */
2187 if (fb_helper->crtc_count > 1)
2188 continue;
2189
2190 if (!drm_mode_equal(modes[o], modes[n]))
2191 continue;
38651674
DA
2192 }
2193
2194 crtcs[n] = crtc;
8be48d92
DA
2195 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
2196 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
38651674
DA
2197 width, height);
2198 if (score > best_score) {
38651674
DA
2199 best_score = score;
2200 memcpy(best_crtcs, crtcs,
255f0e7c 2201 fb_helper->connector_count *
8be48d92 2202 sizeof(struct drm_fb_helper_crtc *));
38651674 2203 }
38651674
DA
2204 }
2205out:
2206 kfree(crtcs);
2207 return best_score;
2208}
2209
64e94407
CW
2210static void drm_setup_crtcs(struct drm_fb_helper *fb_helper,
2211 u32 width, u32 height)
38651674 2212{
8be48d92
DA
2213 struct drm_device *dev = fb_helper->dev;
2214 struct drm_fb_helper_crtc **crtcs;
38651674 2215 struct drm_display_mode **modes;
b0ee9e7f 2216 struct drm_fb_offset *offsets;
38651674 2217 bool *enabled;
11e17a08 2218 int i;
38651674
DA
2219
2220 DRM_DEBUG_KMS("\n");
64e94407
CW
2221 if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0)
2222 DRM_DEBUG_KMS("No connectors reported connected with modes\n");
38651674 2223
966a6a13
CW
2224 /* prevent concurrent modification of connector_count by hotplug */
2225 lockdep_assert_held(&fb_helper->dev->mode_config.mutex);
2226
383b2e57 2227 crtcs = kcalloc(fb_helper->connector_count,
8be48d92 2228 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
383b2e57 2229 modes = kcalloc(fb_helper->connector_count,
38651674 2230 sizeof(struct drm_display_mode *), GFP_KERNEL);
383b2e57 2231 offsets = kcalloc(fb_helper->connector_count,
b0ee9e7f 2232 sizeof(struct drm_fb_offset), GFP_KERNEL);
383b2e57 2233 enabled = kcalloc(fb_helper->connector_count,
38651674 2234 sizeof(bool), GFP_KERNEL);
b0ee9e7f 2235 if (!crtcs || !modes || !enabled || !offsets) {
8c5eaca0
SK
2236 DRM_ERROR("Memory allocation failed\n");
2237 goto out;
2238 }
2239
0b4c0f3f 2240 drm_enable_connectors(fb_helper, enabled);
38651674 2241
11e17a08
JB
2242 if (!(fb_helper->funcs->initial_config &&
2243 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
b0ee9e7f 2244 offsets,
11e17a08 2245 enabled, width, height))) {
383b2e57
ML
2246 memset(modes, 0, fb_helper->connector_count*sizeof(modes[0]));
2247 memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0]));
2248 memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0]));
11e17a08 2249
b0ee9e7f
DA
2250 if (!drm_target_cloned(fb_helper, modes, offsets,
2251 enabled, width, height) &&
2252 !drm_target_preferred(fb_helper, modes, offsets,
2253 enabled, width, height))
1d42bbc8 2254 DRM_ERROR("Unable to find initial modes\n");
38651674 2255
11e17a08
JB
2256 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
2257 width, height);
38651674 2258
11e17a08
JB
2259 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
2260 }
8be48d92
DA
2261
2262 /* need to set the modesets up here for use later */
2263 /* fill out the connector<->crtc mappings into the modesets */
a2889606
VS
2264 for (i = 0; i < fb_helper->crtc_count; i++)
2265 drm_fb_helper_modeset_release(fb_helper,
2266 &fb_helper->crtc_info[i].mode_set);
38651674 2267
966a6a13 2268 drm_fb_helper_for_each_connector(fb_helper, i) {
38651674 2269 struct drm_display_mode *mode = modes[i];
8be48d92 2270 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
b0ee9e7f 2271 struct drm_fb_offset *offset = &offsets[i];
a2889606 2272 struct drm_mode_set *modeset = &fb_crtc->mode_set;
38651674 2273
8be48d92 2274 if (mode && fb_crtc) {
a2889606
VS
2275 struct drm_connector *connector =
2276 fb_helper->connector_info[i]->connector;
2277
b0ee9e7f
DA
2278 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
2279 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
a2889606 2280
8be48d92 2281 fb_crtc->desired_mode = mode;
b0ee9e7f
DA
2282 fb_crtc->x = offset->x;
2283 fb_crtc->y = offset->y;
8be48d92
DA
2284 modeset->mode = drm_mode_duplicate(dev,
2285 fb_crtc->desired_mode);
ad093607 2286 drm_connector_get(connector);
a2889606 2287 modeset->connectors[modeset->num_connectors++] = connector;
7e53f3a4 2288 modeset->fb = fb_helper->fb;
b0ee9e7f
DA
2289 modeset->x = offset->x;
2290 modeset->y = offset->y;
7e53f3a4
DV
2291 }
2292 }
8c5eaca0 2293out:
38651674
DA
2294 kfree(crtcs);
2295 kfree(modes);
b0ee9e7f 2296 kfree(offsets);
38651674
DA
2297 kfree(enabled);
2298}
2299
2300/**
207fd329 2301 * drm_fb_helper_initial_config - setup a sane initial connector configuration
d0ddc033
DV
2302 * @fb_helper: fb_helper device struct
2303 * @bpp_sel: bpp value to use for the framebuffer configuration
38651674 2304 *
d0ddc033 2305 * Scans the CRTCs and connectors and tries to put together an initial setup.
38651674
DA
2306 * At the moment, this is a cloned configuration across all heads with
2307 * a new framebuffer object as the backing store.
2308 *
207fd329
DV
2309 * Note that this also registers the fbdev and so allows userspace to call into
2310 * the driver through the fbdev interfaces.
2311 *
6806cdf9
DV
2312 * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
2313 * to let the driver allocate and initialize the fbdev info structure and the
2314 * drm framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
207fd329
DV
2315 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
2316 * values for the fbdev info structure.
2317 *
40f8cf4b
DV
2318 * HANG DEBUGGING:
2319 *
2320 * When you have fbcon support built-in or already loaded, this function will do
2321 * a full modeset to setup the fbdev console. Due to locking misdesign in the
2322 * VT/fbdev subsystem that entire modeset sequence has to be done while holding
2323 * console_lock. Until console_unlock is called no dmesg lines will be sent out
2324 * to consoles, not even serial console. This means when your driver crashes,
2325 * you will see absolutely nothing else but a system stuck in this function,
2326 * with no further output. Any kind of printk() you place within your own driver
2327 * or in the drm core modeset code will also never show up.
2328 *
2329 * Standard debug practice is to run the fbcon setup without taking the
2330 * console_lock as a hack, to be able to see backtraces and crashes on the
2331 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
2332 * cmdline option.
2333 *
2334 * The other option is to just disable fbdev emulation since very likely the
af509d38
L
2335 * first modeset from userspace will crash in the same way, and is even easier
2336 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
40f8cf4b
DV
2337 * kernel cmdline option.
2338 *
38651674
DA
2339 * RETURNS:
2340 * Zero if everything went ok, nonzero otherwise.
2341 */
01934c2a 2342int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
38651674 2343{
8be48d92 2344 struct drm_device *dev = fb_helper->dev;
966a6a13 2345 struct fb_info *info;
966a6a13 2346 int ret;
38651674 2347
f64c5573
DV
2348 if (!drm_fbdev_emulation)
2349 return 0;
2350
53f1904b 2351 mutex_lock(&dev->mode_config.mutex);
64e94407
CW
2352 drm_setup_crtcs(fb_helper,
2353 dev->mode_config.max_width,
2354 dev->mode_config.max_height);
966a6a13
CW
2355 ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
2356 mutex_unlock(&dev->mode_config.mutex);
2357 if (ret)
2358 return ret;
38651674 2359
966a6a13
CW
2360 info = fb_helper->fbdev;
2361 info->var.pixclock = 0;
2362 ret = register_framebuffer(info);
2363 if (ret < 0)
2364 return ret;
2365
2366 dev_info(dev->dev, "fb%d: %s frame buffer device\n",
2367 info->node, info->fix.id);
2368
a53ca635 2369 mutex_lock(&kernel_fb_helper_lock);
966a6a13
CW
2370 if (list_empty(&kernel_fb_helper_list))
2371 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
2372
2373 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
a53ca635 2374 mutex_unlock(&kernel_fb_helper_lock);
966a6a13
CW
2375
2376 return 0;
38651674 2377}
8be48d92 2378EXPORT_SYMBOL(drm_fb_helper_initial_config);
38651674 2379
7394371d
CW
2380/**
2381 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
d0ddc033 2382 * probing all the outputs attached to the fb
7394371d
CW
2383 * @fb_helper: the drm_fb_helper
2384 *
7394371d 2385 * Scan the connectors attached to the fb_helper and try to put together a
62cacc79 2386 * setup after notification of a change in output configuration.
7394371d 2387 *
207fd329
DV
2388 * Called at runtime, takes the mode config locks to be able to check/change the
2389 * modeset configuration. Must be run from process context (which usually means
2390 * either the output polling work or a work item launched from the driver's
2391 * hotplug interrupt).
2392 *
50c3dc97 2393 * Note that drivers may call this even before calling
af509d38 2394 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
50c3dc97
DV
2395 * for a race-free fbcon setup and will make sure that the fbdev emulation will
2396 * not miss any hotplug events.
207fd329 2397 *
7394371d
CW
2398 * RETURNS:
2399 * 0 on success and a non-zero error code otherwise.
2400 */
2401int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
38651674 2402{
7394371d 2403 struct drm_device *dev = fb_helper->dev;
5c4426a7 2404
f64c5573
DV
2405 if (!drm_fbdev_emulation)
2406 return 0;
2407
64e94407 2408 mutex_lock(&dev->mode_config.mutex);
50c3dc97 2409 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
4abe3520 2410 fb_helper->delayed_hotplug = true;
64e94407 2411 mutex_unlock(&dev->mode_config.mutex);
7394371d 2412 return 0;
4abe3520 2413 }
eb1f8e4f 2414 DRM_DEBUG_KMS("\n");
4abe3520 2415
64e94407 2416 drm_setup_crtcs(fb_helper, fb_helper->fb->width, fb_helper->fb->height);
5c4426a7 2417
64e94407 2418 mutex_unlock(&dev->mode_config.mutex);
89ced125 2419
2180c3c7
DV
2420 drm_fb_helper_set_par(fb_helper->fbdev);
2421
2422 return 0;
5c4426a7 2423}
eb1f8e4f 2424EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
5c4426a7 2425
6a108a14 2426/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
3ce05168
DF
2427 * but the module doesn't depend on any fb console symbols. At least
2428 * attempt to load fbcon to avoid leaving the system without a usable console.
2429 */
70412cfa 2430int __init drm_fb_helper_modinit(void)
3ce05168 2431{
70412cfa 2432#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
3ce05168
DF
2433 const char *name = "fbcon";
2434 struct module *fbcon;
2435
2436 mutex_lock(&module_mutex);
2437 fbcon = find_module(name);
2438 mutex_unlock(&module_mutex);
2439
2440 if (!fbcon)
2441 request_module_nowait(name);
70412cfa 2442#endif
3ce05168
DF
2443 return 0;
2444}
70412cfa 2445EXPORT_SYMBOL(drm_fb_helper_modinit);