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