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