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