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