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