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