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