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