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