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/kernel.h>
33 #include <linux/sysrq.h>
34 #include <linux/slab.h>
35 #include <linux/fb.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
42 static LIST_HEAD(kernel_fb_helper_list);
43
44 /**
45 * DOC: fbdev helpers
46 *
47 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
48 * mode setting driver. They can be used mostly independently from the crtc
49 * helper functions used by many drivers to implement the kernel mode setting
50 * interfaces.
51 *
52 * Initialization is done as a four-step process with drm_fb_helper_prepare(),
53 * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and
54 * drm_fb_helper_initial_config(). Drivers with fancier requirements than the
55 * default behaviour can override the third step with their own code.
56 * Teardown is done with drm_fb_helper_fini().
57 *
58 * At runtime drivers should restore the fbdev console by calling
59 * drm_fb_helper_restore_fbdev_mode() from their ->lastclose callback. They
60 * should also notify the fb helper code from updates to the output
61 * configuration by calling drm_fb_helper_hotplug_event(). For easier
62 * integration with the output polling code in drm_crtc_helper.c the modeset
63 * code provides a ->output_poll_changed callback.
64 *
65 * All other functions exported by the fb helper library can be used to
66 * implement the fbdev driver interface by the driver.
67 *
68 * It is possible, though perhaps somewhat tricky, to implement race-free
69 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
70 * helper must be called first to initialize the minimum required to make
71 * hotplug detection work. Drivers also need to make sure to properly set up
72 * the dev->mode_config.funcs member. After calling drm_kms_helper_poll_init()
73 * it is safe to enable interrupts and start processing hotplug events. At the
74 * same time, drivers should initialize all modeset objects such as CRTCs,
75 * encoders and connectors. To finish up the fbdev helper initialization, the
76 * drm_fb_helper_init() function is called. To probe for all attached displays
77 * and set up an initial configuration using the detected hardware, drivers
78 * should call drm_fb_helper_single_add_all_connectors() followed by
79 * drm_fb_helper_initial_config().
80 */
81
82 /**
83 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
84 * emulation helper
85 * @fb_helper: fbdev initialized with drm_fb_helper_init
86 *
87 * This functions adds all the available connectors for use with the given
88 * fb_helper. This is a separate step to allow drivers to freely assign
89 * connectors to the fbdev, e.g. if some are reserved for special purposes or
90 * not adequate to be used for the fbcon.
91 *
92 * Since this is part of the initial setup before the fbdev is published, no
93 * locking is required.
94 */
drm_fb_helper_single_add_all_connectors(struct drm_fb_helper * fb_helper)95 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
96 {
97 struct drm_device *dev = fb_helper->dev;
98 struct drm_connector *connector;
99 int i;
100
101 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
102 struct drm_fb_helper_connector *fb_helper_connector;
103
104 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
105 if (!fb_helper_connector)
106 goto fail;
107
108 fb_helper_connector->connector = connector;
109 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
110 }
111 return 0;
112 fail:
113 for (i = 0; i < fb_helper->connector_count; i++) {
114 kfree(fb_helper->connector_info[i]);
115 fb_helper->connector_info[i] = NULL;
116 }
117 fb_helper->connector_count = 0;
118 return -ENOMEM;
119 }
120 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
121
drm_fb_helper_add_one_connector(struct drm_fb_helper * fb_helper,struct drm_connector * connector)122 int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector)
123 {
124 struct drm_fb_helper_connector **temp;
125 struct drm_fb_helper_connector *fb_helper_connector;
126
127 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
128 if (fb_helper->connector_count + 1 > fb_helper->connector_info_alloc_count) {
129 temp = krealloc(fb_helper->connector_info, sizeof(struct drm_fb_helper_connector *) * (fb_helper->connector_count + 1), GFP_KERNEL);
130 if (!temp)
131 return -ENOMEM;
132
133 fb_helper->connector_info_alloc_count = fb_helper->connector_count + 1;
134 fb_helper->connector_info = temp;
135 }
136
137
138 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
139 if (!fb_helper_connector)
140 return -ENOMEM;
141
142 fb_helper_connector->connector = connector;
143 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
144 return 0;
145 }
146 EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
147
remove_from_modeset(struct drm_mode_set * set,struct drm_connector * connector)148 static void remove_from_modeset(struct drm_mode_set *set,
149 struct drm_connector *connector)
150 {
151 int i, j;
152
153 for (i = 0; i < set->num_connectors; i++) {
154 if (set->connectors[i] == connector)
155 break;
156 }
157
158 if (i == set->num_connectors)
159 return;
160
161 for (j = i + 1; j < set->num_connectors; j++) {
162 set->connectors[j - 1] = set->connectors[j];
163 }
164 set->num_connectors--;
165
166 /* because i915 is pissy about this..
167 * TODO maybe need to makes sure we set it back to !=NULL somewhere?
168 */
169 if (set->num_connectors == 0)
170 set->fb = NULL;
171 }
172
drm_fb_helper_remove_one_connector(struct drm_fb_helper * fb_helper,struct drm_connector * connector)173 int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
174 struct drm_connector *connector)
175 {
176 struct drm_fb_helper_connector *fb_helper_connector;
177 int i, j;
178
179 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
180
181 for (i = 0; i < fb_helper->connector_count; i++) {
182 if (fb_helper->connector_info[i]->connector == connector)
183 break;
184 }
185
186 if (i == fb_helper->connector_count)
187 return -EINVAL;
188 fb_helper_connector = fb_helper->connector_info[i];
189
190 for (j = i + 1; j < fb_helper->connector_count; j++) {
191 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
192 }
193 fb_helper->connector_count--;
194 kfree(fb_helper_connector);
195
196 /* also cleanup dangling references to the connector: */
197 for (i = 0; i < fb_helper->crtc_count; i++)
198 remove_from_modeset(&fb_helper->crtc_info[i].mode_set, connector);
199
200 return 0;
201 }
202 EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
203
drm_fb_helper_save_lut_atomic(struct drm_crtc * crtc,struct drm_fb_helper * helper)204 static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
205 {
206 uint16_t *r_base, *g_base, *b_base;
207 int i;
208
209 if (helper->funcs->gamma_get == NULL)
210 return;
211
212 r_base = crtc->gamma_store;
213 g_base = r_base + crtc->gamma_size;
214 b_base = g_base + crtc->gamma_size;
215
216 for (i = 0; i < crtc->gamma_size; i++)
217 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
218 }
219
drm_fb_helper_restore_lut_atomic(struct drm_crtc * crtc)220 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
221 {
222 uint16_t *r_base, *g_base, *b_base;
223
224 if (crtc->funcs->gamma_set == NULL)
225 return;
226
227 r_base = crtc->gamma_store;
228 g_base = r_base + crtc->gamma_size;
229 b_base = g_base + crtc->gamma_size;
230
231 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
232 }
233
234 /**
235 * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter
236 * @info: fbdev registered by the helper
237 */
drm_fb_helper_debug_enter(struct fb_info * info)238 int drm_fb_helper_debug_enter(struct fb_info *info)
239 {
240 struct drm_fb_helper *helper = info->par;
241 struct drm_crtc_helper_funcs *funcs;
242 int i;
243
244 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
245 for (i = 0; i < helper->crtc_count; i++) {
246 struct drm_mode_set *mode_set =
247 &helper->crtc_info[i].mode_set;
248
249 if (!mode_set->crtc->enabled)
250 continue;
251
252 funcs = mode_set->crtc->helper_private;
253 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
254 funcs->mode_set_base_atomic(mode_set->crtc,
255 mode_set->fb,
256 mode_set->x,
257 mode_set->y,
258 ENTER_ATOMIC_MODE_SET);
259 }
260 }
261
262 return 0;
263 }
264 EXPORT_SYMBOL(drm_fb_helper_debug_enter);
265
266 /* Find the real fb for a given fb helper CRTC */
drm_mode_config_fb(struct drm_crtc * crtc)267 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
268 {
269 struct drm_device *dev = crtc->dev;
270 struct drm_crtc *c;
271
272 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
273 if (crtc->base.id == c->base.id)
274 return c->primary->fb;
275 }
276
277 return NULL;
278 }
279
280 /**
281 * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave
282 * @info: fbdev registered by the helper
283 */
drm_fb_helper_debug_leave(struct fb_info * info)284 int drm_fb_helper_debug_leave(struct fb_info *info)
285 {
286 struct drm_fb_helper *helper = info->par;
287 struct drm_crtc *crtc;
288 struct drm_crtc_helper_funcs *funcs;
289 struct drm_framebuffer *fb;
290 int i;
291
292 for (i = 0; i < helper->crtc_count; i++) {
293 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
294 crtc = mode_set->crtc;
295 funcs = crtc->helper_private;
296 fb = drm_mode_config_fb(crtc);
297
298 if (!crtc->enabled)
299 continue;
300
301 if (!fb) {
302 DRM_ERROR("no fb to restore??\n");
303 continue;
304 }
305
306 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
307 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
308 crtc->y, LEAVE_ATOMIC_MODE_SET);
309 }
310
311 return 0;
312 }
313 EXPORT_SYMBOL(drm_fb_helper_debug_leave);
314
restore_fbdev_mode(struct drm_fb_helper * fb_helper)315 static bool restore_fbdev_mode(struct drm_fb_helper *fb_helper)
316 {
317 struct drm_device *dev = fb_helper->dev;
318 struct drm_plane *plane;
319 bool error = false;
320 int i;
321
322 drm_warn_on_modeset_not_all_locked(dev);
323
324 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
325 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
326 drm_plane_force_disable(plane);
327
328 if (dev->mode_config.rotation_property) {
329 drm_mode_plane_set_obj_prop(plane,
330 dev->mode_config.rotation_property,
331 BIT(DRM_ROTATE_0));
332 }
333 }
334
335 for (i = 0; i < fb_helper->crtc_count; i++) {
336 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
337 struct drm_crtc *crtc = mode_set->crtc;
338 int ret;
339
340 if (crtc->funcs->cursor_set) {
341 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
342 if (ret)
343 error = true;
344 }
345
346 ret = drm_mode_set_config_internal(mode_set);
347 if (ret)
348 error = true;
349 }
350 return error;
351 }
352 /**
353 * drm_fb_helper_restore_fbdev_mode - restore fbdev configuration
354 * @fb_helper: fbcon to restore
355 *
356 * This should be called from driver's drm ->lastclose callback
357 * when implementing an fbcon on top of kms using this helper. This ensures that
358 * the user isn't greeted with a black screen when e.g. X dies.
359 *
360 * Use this variant if you need to bypass locking (panic), or already
361 * hold all modeset locks. Otherwise use drm_fb_helper_restore_fbdev_mode_unlocked()
362 */
drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper * fb_helper)363 static bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
364 {
365 return restore_fbdev_mode(fb_helper);
366 }
367
368 /**
369 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
370 * @fb_helper: fbcon to restore
371 *
372 * This should be called from driver's drm ->lastclose callback
373 * when implementing an fbcon on top of kms using this helper. This ensures that
374 * the user isn't greeted with a black screen when e.g. X dies.
375 */
drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper * fb_helper)376 bool drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
377 {
378 struct drm_device *dev = fb_helper->dev;
379 bool ret;
380 bool do_delayed = false;
381
382 drm_modeset_lock_all(dev);
383 ret = restore_fbdev_mode(fb_helper);
384
385 do_delayed = fb_helper->delayed_hotplug;
386 if (do_delayed)
387 fb_helper->delayed_hotplug = false;
388 drm_modeset_unlock_all(dev);
389
390 if (do_delayed)
391 drm_fb_helper_hotplug_event(fb_helper);
392 return ret;
393 }
394 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
395
396 /*
397 * restore fbcon display for all kms driver's using this helper, used for sysrq
398 * and panic handling.
399 */
drm_fb_helper_force_kernel_mode(void)400 static bool drm_fb_helper_force_kernel_mode(void)
401 {
402 bool ret, error = false;
403 struct drm_fb_helper *helper;
404
405 if (list_empty(&kernel_fb_helper_list))
406 return false;
407
408 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
409 struct drm_device *dev = helper->dev;
410
411 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
412 continue;
413
414 /*
415 * NOTE: Use trylock mode to avoid deadlocks and sleeping in
416 * panic context.
417 */
418 if (__drm_modeset_lock_all(dev, true) != 0) {
419 error = true;
420 continue;
421 }
422
423 ret = drm_fb_helper_restore_fbdev_mode(helper);
424 if (ret)
425 error = true;
426
427 drm_modeset_unlock_all(dev);
428 }
429 return error;
430 }
431
drm_fb_helper_panic(struct notifier_block * n,unsigned long ununsed,void * panic_str)432 static int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
433 void *panic_str)
434 {
435 /*
436 * It's a waste of time and effort to switch back to text console
437 * if the kernel should reboot before panic messages can be seen.
438 */
439 if (panic_timeout < 0)
440 return 0;
441
442 pr_err("panic occurred, switching back to text console\n");
443 return drm_fb_helper_force_kernel_mode();
444 }
445
446 static struct notifier_block paniced = {
447 .notifier_call = drm_fb_helper_panic,
448 };
449
drm_fb_helper_is_bound(struct drm_fb_helper * fb_helper)450 static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
451 {
452 struct drm_device *dev = fb_helper->dev;
453 struct drm_crtc *crtc;
454 int bound = 0, crtcs_bound = 0;
455
456 /* Sometimes user space wants everything disabled, so don't steal the
457 * display if there's a master. */
458 if (dev->primary->master)
459 return false;
460
461 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
462 if (crtc->primary->fb)
463 crtcs_bound++;
464 if (crtc->primary->fb == fb_helper->fb)
465 bound++;
466 }
467
468 if (bound < crtcs_bound)
469 return false;
470
471 return true;
472 }
473
474 #ifdef CONFIG_MAGIC_SYSRQ
drm_fb_helper_restore_work_fn(struct work_struct * ignored)475 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
476 {
477 bool ret;
478 ret = drm_fb_helper_force_kernel_mode();
479 if (ret == true)
480 DRM_ERROR("Failed to restore crtc configuration\n");
481 }
482 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
483
drm_fb_helper_sysrq(int dummy1)484 static void drm_fb_helper_sysrq(int dummy1)
485 {
486 schedule_work(&drm_fb_helper_restore_work);
487 }
488
489 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
490 .handler = drm_fb_helper_sysrq,
491 .help_msg = "force-fb(V)",
492 .action_msg = "Restore framebuffer console",
493 };
494 #else
495 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
496 #endif
497
drm_fb_helper_dpms(struct fb_info * info,int dpms_mode)498 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
499 {
500 struct drm_fb_helper *fb_helper = info->par;
501 struct drm_device *dev = fb_helper->dev;
502 struct drm_crtc *crtc;
503 struct drm_connector *connector;
504 int i, j;
505
506 /*
507 * fbdev->blank can be called from irq context in case of a panic.
508 * Since we already have our own special panic handler which will
509 * restore the fbdev console mode completely, just bail out early.
510 */
511 if (oops_in_progress)
512 return;
513
514 /*
515 * For each CRTC in this fb, turn the connectors on/off.
516 */
517 drm_modeset_lock_all(dev);
518 if (!drm_fb_helper_is_bound(fb_helper)) {
519 drm_modeset_unlock_all(dev);
520 return;
521 }
522
523 for (i = 0; i < fb_helper->crtc_count; i++) {
524 crtc = fb_helper->crtc_info[i].mode_set.crtc;
525
526 if (!crtc->enabled)
527 continue;
528
529 /* Walk the connectors & encoders on this fb turning them on/off */
530 for (j = 0; j < fb_helper->connector_count; j++) {
531 connector = fb_helper->connector_info[j]->connector;
532 connector->funcs->dpms(connector, dpms_mode);
533 drm_object_property_set_value(&connector->base,
534 dev->mode_config.dpms_property, dpms_mode);
535 }
536 }
537 drm_modeset_unlock_all(dev);
538 }
539
540 /**
541 * drm_fb_helper_blank - implementation for ->fb_blank
542 * @blank: desired blanking state
543 * @info: fbdev registered by the helper
544 */
drm_fb_helper_blank(int blank,struct fb_info * info)545 int drm_fb_helper_blank(int blank, struct fb_info *info)
546 {
547 switch (blank) {
548 /* Display: On; HSync: On, VSync: On */
549 case FB_BLANK_UNBLANK:
550 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
551 break;
552 /* Display: Off; HSync: On, VSync: On */
553 case FB_BLANK_NORMAL:
554 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
555 break;
556 /* Display: Off; HSync: Off, VSync: On */
557 case FB_BLANK_HSYNC_SUSPEND:
558 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
559 break;
560 /* Display: Off; HSync: On, VSync: Off */
561 case FB_BLANK_VSYNC_SUSPEND:
562 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
563 break;
564 /* Display: Off; HSync: Off, VSync: Off */
565 case FB_BLANK_POWERDOWN:
566 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
567 break;
568 }
569 return 0;
570 }
571 EXPORT_SYMBOL(drm_fb_helper_blank);
572
drm_fb_helper_crtc_free(struct drm_fb_helper * helper)573 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
574 {
575 int i;
576
577 for (i = 0; i < helper->connector_count; i++)
578 kfree(helper->connector_info[i]);
579 kfree(helper->connector_info);
580 for (i = 0; i < helper->crtc_count; i++) {
581 kfree(helper->crtc_info[i].mode_set.connectors);
582 if (helper->crtc_info[i].mode_set.mode)
583 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
584 }
585 kfree(helper->crtc_info);
586 }
587
588 /**
589 * drm_fb_helper_prepare - setup a drm_fb_helper structure
590 * @dev: DRM device
591 * @helper: driver-allocated fbdev helper structure to set up
592 * @funcs: pointer to structure of functions associate with this helper
593 *
594 * Sets up the bare minimum to make the framebuffer helper usable. This is
595 * useful to implement race-free initialization of the polling helpers.
596 */
drm_fb_helper_prepare(struct drm_device * dev,struct drm_fb_helper * helper,const struct drm_fb_helper_funcs * funcs)597 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
598 const struct drm_fb_helper_funcs *funcs)
599 {
600 INIT_LIST_HEAD(&helper->kernel_fb_list);
601 helper->funcs = funcs;
602 helper->dev = dev;
603 }
604 EXPORT_SYMBOL(drm_fb_helper_prepare);
605
606 /**
607 * drm_fb_helper_init - initialize a drm_fb_helper structure
608 * @dev: drm device
609 * @fb_helper: driver-allocated fbdev helper structure to initialize
610 * @crtc_count: maximum number of crtcs to support in this fbdev emulation
611 * @max_conn_count: max connector count
612 *
613 * This allocates the structures for the fbdev helper with the given limits.
614 * Note that this won't yet touch the hardware (through the driver interfaces)
615 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
616 * to allow driver writes more control over the exact init sequence.
617 *
618 * Drivers must call drm_fb_helper_prepare() before calling this function.
619 *
620 * RETURNS:
621 * Zero if everything went ok, nonzero otherwise.
622 */
drm_fb_helper_init(struct drm_device * dev,struct drm_fb_helper * fb_helper,int crtc_count,int max_conn_count)623 int drm_fb_helper_init(struct drm_device *dev,
624 struct drm_fb_helper *fb_helper,
625 int crtc_count, int max_conn_count)
626 {
627 struct drm_crtc *crtc;
628 int i;
629
630 if (!max_conn_count)
631 return -EINVAL;
632
633 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
634 if (!fb_helper->crtc_info)
635 return -ENOMEM;
636
637 fb_helper->crtc_count = crtc_count;
638 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
639 if (!fb_helper->connector_info) {
640 kfree(fb_helper->crtc_info);
641 return -ENOMEM;
642 }
643 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
644 fb_helper->connector_count = 0;
645
646 for (i = 0; i < crtc_count; i++) {
647 fb_helper->crtc_info[i].mode_set.connectors =
648 kcalloc(max_conn_count,
649 sizeof(struct drm_connector *),
650 GFP_KERNEL);
651
652 if (!fb_helper->crtc_info[i].mode_set.connectors)
653 goto out_free;
654 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
655 }
656
657 i = 0;
658 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
659 fb_helper->crtc_info[i].mode_set.crtc = crtc;
660 i++;
661 }
662
663 return 0;
664 out_free:
665 drm_fb_helper_crtc_free(fb_helper);
666 return -ENOMEM;
667 }
668 EXPORT_SYMBOL(drm_fb_helper_init);
669
drm_fb_helper_fini(struct drm_fb_helper * fb_helper)670 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
671 {
672 if (!list_empty(&fb_helper->kernel_fb_list)) {
673 list_del(&fb_helper->kernel_fb_list);
674 if (list_empty(&kernel_fb_helper_list)) {
675 pr_info("drm: unregistered panic notifier\n");
676 atomic_notifier_chain_unregister(&panic_notifier_list,
677 &paniced);
678 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
679 }
680 }
681
682 drm_fb_helper_crtc_free(fb_helper);
683
684 }
685 EXPORT_SYMBOL(drm_fb_helper_fini);
686
setcolreg(struct drm_crtc * crtc,u16 red,u16 green,u16 blue,u16 regno,struct fb_info * info)687 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
688 u16 blue, u16 regno, struct fb_info *info)
689 {
690 struct drm_fb_helper *fb_helper = info->par;
691 struct drm_framebuffer *fb = fb_helper->fb;
692 int pindex;
693
694 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
695 u32 *palette;
696 u32 value;
697 /* place color in psuedopalette */
698 if (regno > 16)
699 return -EINVAL;
700 palette = (u32 *)info->pseudo_palette;
701 red >>= (16 - info->var.red.length);
702 green >>= (16 - info->var.green.length);
703 blue >>= (16 - info->var.blue.length);
704 value = (red << info->var.red.offset) |
705 (green << info->var.green.offset) |
706 (blue << info->var.blue.offset);
707 if (info->var.transp.length > 0) {
708 u32 mask = (1 << info->var.transp.length) - 1;
709 mask <<= info->var.transp.offset;
710 value |= mask;
711 }
712 palette[regno] = value;
713 return 0;
714 }
715
716 /*
717 * The driver really shouldn't advertise pseudo/directcolor
718 * visuals if it can't deal with the palette.
719 */
720 if (WARN_ON(!fb_helper->funcs->gamma_set ||
721 !fb_helper->funcs->gamma_get))
722 return -EINVAL;
723
724 pindex = regno;
725
726 if (fb->bits_per_pixel == 16) {
727 pindex = regno << 3;
728
729 if (fb->depth == 16 && regno > 63)
730 return -EINVAL;
731 if (fb->depth == 15 && regno > 31)
732 return -EINVAL;
733
734 if (fb->depth == 16) {
735 u16 r, g, b;
736 int i;
737 if (regno < 32) {
738 for (i = 0; i < 8; i++)
739 fb_helper->funcs->gamma_set(crtc, red,
740 green, blue, pindex + i);
741 }
742
743 fb_helper->funcs->gamma_get(crtc, &r,
744 &g, &b,
745 pindex >> 1);
746
747 for (i = 0; i < 4; i++)
748 fb_helper->funcs->gamma_set(crtc, r,
749 green, b,
750 (pindex >> 1) + i);
751 }
752 }
753
754 if (fb->depth != 16)
755 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
756 return 0;
757 }
758
759 /**
760 * drm_fb_helper_setcmap - implementation for ->fb_setcmap
761 * @cmap: cmap to set
762 * @info: fbdev registered by the helper
763 */
drm_fb_helper_setcmap(struct fb_cmap * cmap,struct fb_info * info)764 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
765 {
766 struct drm_fb_helper *fb_helper = info->par;
767 struct drm_device *dev = fb_helper->dev;
768 struct drm_crtc_helper_funcs *crtc_funcs;
769 u16 *red, *green, *blue, *transp;
770 struct drm_crtc *crtc;
771 int i, j, rc = 0;
772 int start;
773
774 drm_modeset_lock_all(dev);
775 if (!drm_fb_helper_is_bound(fb_helper)) {
776 drm_modeset_unlock_all(dev);
777 return -EBUSY;
778 }
779
780 for (i = 0; i < fb_helper->crtc_count; i++) {
781 crtc = fb_helper->crtc_info[i].mode_set.crtc;
782 crtc_funcs = crtc->helper_private;
783
784 red = cmap->red;
785 green = cmap->green;
786 blue = cmap->blue;
787 transp = cmap->transp;
788 start = cmap->start;
789
790 for (j = 0; j < cmap->len; j++) {
791 u16 hred, hgreen, hblue, htransp = 0xffff;
792
793 hred = *red++;
794 hgreen = *green++;
795 hblue = *blue++;
796
797 if (transp)
798 htransp = *transp++;
799
800 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
801 if (rc)
802 goto out;
803 }
804 if (crtc_funcs->load_lut)
805 crtc_funcs->load_lut(crtc);
806 }
807 out:
808 drm_modeset_unlock_all(dev);
809 return rc;
810 }
811 EXPORT_SYMBOL(drm_fb_helper_setcmap);
812
813 /**
814 * drm_fb_helper_check_var - implementation for ->fb_check_var
815 * @var: screeninfo to check
816 * @info: fbdev registered by the helper
817 */
drm_fb_helper_check_var(struct fb_var_screeninfo * var,struct fb_info * info)818 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
819 struct fb_info *info)
820 {
821 struct drm_fb_helper *fb_helper = info->par;
822 struct drm_framebuffer *fb = fb_helper->fb;
823 int depth;
824
825 if (var->pixclock != 0 || in_dbg_master())
826 return -EINVAL;
827
828 /* Need to resize the fb object !!! */
829 if (var->bits_per_pixel > fb->bits_per_pixel ||
830 var->xres > fb->width || var->yres > fb->height ||
831 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
832 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
833 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
834 var->xres, var->yres, var->bits_per_pixel,
835 var->xres_virtual, var->yres_virtual,
836 fb->width, fb->height, fb->bits_per_pixel);
837 return -EINVAL;
838 }
839
840 switch (var->bits_per_pixel) {
841 case 16:
842 depth = (var->green.length == 6) ? 16 : 15;
843 break;
844 case 32:
845 depth = (var->transp.length > 0) ? 32 : 24;
846 break;
847 default:
848 depth = var->bits_per_pixel;
849 break;
850 }
851
852 switch (depth) {
853 case 8:
854 var->red.offset = 0;
855 var->green.offset = 0;
856 var->blue.offset = 0;
857 var->red.length = 8;
858 var->green.length = 8;
859 var->blue.length = 8;
860 var->transp.length = 0;
861 var->transp.offset = 0;
862 break;
863 case 15:
864 var->red.offset = 10;
865 var->green.offset = 5;
866 var->blue.offset = 0;
867 var->red.length = 5;
868 var->green.length = 5;
869 var->blue.length = 5;
870 var->transp.length = 1;
871 var->transp.offset = 15;
872 break;
873 case 16:
874 var->red.offset = 11;
875 var->green.offset = 5;
876 var->blue.offset = 0;
877 var->red.length = 5;
878 var->green.length = 6;
879 var->blue.length = 5;
880 var->transp.length = 0;
881 var->transp.offset = 0;
882 break;
883 case 24:
884 var->red.offset = 16;
885 var->green.offset = 8;
886 var->blue.offset = 0;
887 var->red.length = 8;
888 var->green.length = 8;
889 var->blue.length = 8;
890 var->transp.length = 0;
891 var->transp.offset = 0;
892 break;
893 case 32:
894 var->red.offset = 16;
895 var->green.offset = 8;
896 var->blue.offset = 0;
897 var->red.length = 8;
898 var->green.length = 8;
899 var->blue.length = 8;
900 var->transp.length = 8;
901 var->transp.offset = 24;
902 break;
903 default:
904 return -EINVAL;
905 }
906 return 0;
907 }
908 EXPORT_SYMBOL(drm_fb_helper_check_var);
909
910 /**
911 * drm_fb_helper_set_par - implementation for ->fb_set_par
912 * @info: fbdev registered by the helper
913 *
914 * This will let fbcon do the mode init and is called at initialization time by
915 * the fbdev core when registering the driver, and later on through the hotplug
916 * callback.
917 */
drm_fb_helper_set_par(struct fb_info * info)918 int drm_fb_helper_set_par(struct fb_info *info)
919 {
920 struct drm_fb_helper *fb_helper = info->par;
921 struct fb_var_screeninfo *var = &info->var;
922
923 if (var->pixclock != 0) {
924 DRM_ERROR("PIXEL CLOCK SET\n");
925 return -EINVAL;
926 }
927
928 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
929
930 return 0;
931 }
932 EXPORT_SYMBOL(drm_fb_helper_set_par);
933
934 /**
935 * drm_fb_helper_pan_display - implementation for ->fb_pan_display
936 * @var: updated screen information
937 * @info: fbdev registered by the helper
938 */
drm_fb_helper_pan_display(struct fb_var_screeninfo * var,struct fb_info * info)939 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
940 struct fb_info *info)
941 {
942 struct drm_fb_helper *fb_helper = info->par;
943 struct drm_device *dev = fb_helper->dev;
944 struct drm_mode_set *modeset;
945 int ret = 0;
946 int i;
947
948 drm_modeset_lock_all(dev);
949 if (!drm_fb_helper_is_bound(fb_helper)) {
950 drm_modeset_unlock_all(dev);
951 return -EBUSY;
952 }
953
954 for (i = 0; i < fb_helper->crtc_count; i++) {
955 modeset = &fb_helper->crtc_info[i].mode_set;
956
957 modeset->x = var->xoffset;
958 modeset->y = var->yoffset;
959
960 if (modeset->num_connectors) {
961 ret = drm_mode_set_config_internal(modeset);
962 if (!ret) {
963 info->var.xoffset = var->xoffset;
964 info->var.yoffset = var->yoffset;
965 }
966 }
967 }
968 drm_modeset_unlock_all(dev);
969 return ret;
970 }
971 EXPORT_SYMBOL(drm_fb_helper_pan_display);
972
973 /*
974 * Allocates the backing storage and sets up the fbdev info structure through
975 * the ->fb_probe callback and then registers the fbdev and sets up the panic
976 * notifier.
977 */
drm_fb_helper_single_fb_probe(struct drm_fb_helper * fb_helper,int preferred_bpp)978 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
979 int preferred_bpp)
980 {
981 int ret = 0;
982 int crtc_count = 0;
983 int i;
984 struct fb_info *info;
985 struct drm_fb_helper_surface_size sizes;
986 int gamma_size = 0;
987
988 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
989 sizes.surface_depth = 24;
990 sizes.surface_bpp = 32;
991 sizes.fb_width = (unsigned)-1;
992 sizes.fb_height = (unsigned)-1;
993
994 /* if driver picks 8 or 16 by default use that
995 for both depth/bpp */
996 if (preferred_bpp != sizes.surface_bpp)
997 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
998
999 /* first up get a count of crtcs now in use and new min/maxes width/heights */
1000 for (i = 0; i < fb_helper->connector_count; i++) {
1001 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
1002 struct drm_cmdline_mode *cmdline_mode;
1003
1004 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
1005
1006 if (cmdline_mode->bpp_specified) {
1007 switch (cmdline_mode->bpp) {
1008 case 8:
1009 sizes.surface_depth = sizes.surface_bpp = 8;
1010 break;
1011 case 15:
1012 sizes.surface_depth = 15;
1013 sizes.surface_bpp = 16;
1014 break;
1015 case 16:
1016 sizes.surface_depth = sizes.surface_bpp = 16;
1017 break;
1018 case 24:
1019 sizes.surface_depth = sizes.surface_bpp = 24;
1020 break;
1021 case 32:
1022 sizes.surface_depth = 24;
1023 sizes.surface_bpp = 32;
1024 break;
1025 }
1026 break;
1027 }
1028 }
1029
1030 crtc_count = 0;
1031 for (i = 0; i < fb_helper->crtc_count; i++) {
1032 struct drm_display_mode *desired_mode;
1033 desired_mode = fb_helper->crtc_info[i].desired_mode;
1034
1035 if (desired_mode) {
1036 if (gamma_size == 0)
1037 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1038 if (desired_mode->hdisplay < sizes.fb_width)
1039 sizes.fb_width = desired_mode->hdisplay;
1040 if (desired_mode->vdisplay < sizes.fb_height)
1041 sizes.fb_height = desired_mode->vdisplay;
1042 if (desired_mode->hdisplay > sizes.surface_width)
1043 sizes.surface_width = desired_mode->hdisplay;
1044 if (desired_mode->vdisplay > sizes.surface_height)
1045 sizes.surface_height = desired_mode->vdisplay;
1046 crtc_count++;
1047 }
1048 }
1049
1050 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
1051 /* hmm everyone went away - assume VGA cable just fell out
1052 and will come back later. */
1053 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
1054 sizes.fb_width = sizes.surface_width = 1024;
1055 sizes.fb_height = sizes.surface_height = 768;
1056 }
1057
1058 /* push down into drivers */
1059 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1060 if (ret < 0)
1061 return ret;
1062
1063 info = fb_helper->fbdev;
1064
1065 /*
1066 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1067 * events, but at init time drm_setup_crtcs needs to be called before
1068 * the fb is allocated (since we need to figure out the desired size of
1069 * the fb before we can allocate it ...). Hence we need to fix things up
1070 * here again.
1071 */
1072 for (i = 0; i < fb_helper->crtc_count; i++)
1073 if (fb_helper->crtc_info[i].mode_set.num_connectors)
1074 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1075
1076
1077 info->var.pixclock = 0;
1078 if (register_framebuffer(info) < 0)
1079 return -EINVAL;
1080
1081 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
1082 info->node, info->fix.id);
1083
1084 /* Switch back to kernel console on panic */
1085 /* multi card linked list maybe */
1086 if (list_empty(&kernel_fb_helper_list)) {
1087 dev_info(fb_helper->dev->dev, "registered panic notifier\n");
1088 atomic_notifier_chain_register(&panic_notifier_list,
1089 &paniced);
1090 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
1091 }
1092
1093 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
1094
1095 return 0;
1096 }
1097
1098 /**
1099 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1100 * @info: fbdev registered by the helper
1101 * @pitch: desired pitch
1102 * @depth: desired depth
1103 *
1104 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1105 * fbdev emulations. Drivers which support acceleration methods which impose
1106 * additional constraints need to set up their own limits.
1107 *
1108 * Drivers should call this (or their equivalent setup code) from their
1109 * ->fb_probe callback.
1110 */
drm_fb_helper_fill_fix(struct fb_info * info,uint32_t pitch,uint32_t depth)1111 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1112 uint32_t depth)
1113 {
1114 info->fix.type = FB_TYPE_PACKED_PIXELS;
1115 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1116 FB_VISUAL_TRUECOLOR;
1117 info->fix.mmio_start = 0;
1118 info->fix.mmio_len = 0;
1119 info->fix.type_aux = 0;
1120 info->fix.xpanstep = 1; /* doing it in hw */
1121 info->fix.ypanstep = 1; /* doing it in hw */
1122 info->fix.ywrapstep = 0;
1123 info->fix.accel = FB_ACCEL_NONE;
1124
1125 info->fix.line_length = pitch;
1126 return;
1127 }
1128 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1129
1130 /**
1131 * drm_fb_helper_fill_var - initalizes variable fbdev information
1132 * @info: fbdev instance to set up
1133 * @fb_helper: fb helper instance to use as template
1134 * @fb_width: desired fb width
1135 * @fb_height: desired fb height
1136 *
1137 * Sets up the variable fbdev metainformation from the given fb helper instance
1138 * and the drm framebuffer allocated in fb_helper->fb.
1139 *
1140 * Drivers should call this (or their equivalent setup code) from their
1141 * ->fb_probe callback after having allocated the fbdev backing
1142 * storage framebuffer.
1143 */
drm_fb_helper_fill_var(struct fb_info * info,struct drm_fb_helper * fb_helper,uint32_t fb_width,uint32_t fb_height)1144 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
1145 uint32_t fb_width, uint32_t fb_height)
1146 {
1147 struct drm_framebuffer *fb = fb_helper->fb;
1148 info->pseudo_palette = fb_helper->pseudo_palette;
1149 info->var.xres_virtual = fb->width;
1150 info->var.yres_virtual = fb->height;
1151 info->var.bits_per_pixel = fb->bits_per_pixel;
1152 info->var.accel_flags = FB_ACCELF_TEXT;
1153 info->var.xoffset = 0;
1154 info->var.yoffset = 0;
1155 info->var.activate = FB_ACTIVATE_NOW;
1156 info->var.height = -1;
1157 info->var.width = -1;
1158
1159 switch (fb->depth) {
1160 case 8:
1161 info->var.red.offset = 0;
1162 info->var.green.offset = 0;
1163 info->var.blue.offset = 0;
1164 info->var.red.length = 8; /* 8bit DAC */
1165 info->var.green.length = 8;
1166 info->var.blue.length = 8;
1167 info->var.transp.offset = 0;
1168 info->var.transp.length = 0;
1169 break;
1170 case 15:
1171 info->var.red.offset = 10;
1172 info->var.green.offset = 5;
1173 info->var.blue.offset = 0;
1174 info->var.red.length = 5;
1175 info->var.green.length = 5;
1176 info->var.blue.length = 5;
1177 info->var.transp.offset = 15;
1178 info->var.transp.length = 1;
1179 break;
1180 case 16:
1181 info->var.red.offset = 11;
1182 info->var.green.offset = 5;
1183 info->var.blue.offset = 0;
1184 info->var.red.length = 5;
1185 info->var.green.length = 6;
1186 info->var.blue.length = 5;
1187 info->var.transp.offset = 0;
1188 break;
1189 case 24:
1190 info->var.red.offset = 16;
1191 info->var.green.offset = 8;
1192 info->var.blue.offset = 0;
1193 info->var.red.length = 8;
1194 info->var.green.length = 8;
1195 info->var.blue.length = 8;
1196 info->var.transp.offset = 0;
1197 info->var.transp.length = 0;
1198 break;
1199 case 32:
1200 info->var.red.offset = 16;
1201 info->var.green.offset = 8;
1202 info->var.blue.offset = 0;
1203 info->var.red.length = 8;
1204 info->var.green.length = 8;
1205 info->var.blue.length = 8;
1206 info->var.transp.offset = 24;
1207 info->var.transp.length = 8;
1208 break;
1209 default:
1210 break;
1211 }
1212
1213 info->var.xres = fb_width;
1214 info->var.yres = fb_height;
1215 }
1216 EXPORT_SYMBOL(drm_fb_helper_fill_var);
1217
drm_fb_helper_probe_connector_modes(struct drm_fb_helper * fb_helper,uint32_t maxX,uint32_t maxY)1218 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1219 uint32_t maxX,
1220 uint32_t maxY)
1221 {
1222 struct drm_connector *connector;
1223 int count = 0;
1224 int i;
1225
1226 for (i = 0; i < fb_helper->connector_count; i++) {
1227 connector = fb_helper->connector_info[i]->connector;
1228 count += connector->funcs->fill_modes(connector, maxX, maxY);
1229 }
1230
1231 return count;
1232 }
1233
drm_has_preferred_mode(struct drm_fb_helper_connector * fb_connector,int width,int height)1234 struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1235 {
1236 struct drm_display_mode *mode;
1237
1238 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1239 if (mode->hdisplay > width ||
1240 mode->vdisplay > height)
1241 continue;
1242 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1243 return mode;
1244 }
1245 return NULL;
1246 }
1247 EXPORT_SYMBOL(drm_has_preferred_mode);
1248
drm_has_cmdline_mode(struct drm_fb_helper_connector * fb_connector)1249 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1250 {
1251 return fb_connector->connector->cmdline_mode.specified;
1252 }
1253
drm_pick_cmdline_mode(struct drm_fb_helper_connector * fb_helper_conn,int width,int height)1254 struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1255 int width, int height)
1256 {
1257 struct drm_cmdline_mode *cmdline_mode;
1258 struct drm_display_mode *mode = NULL;
1259 bool prefer_non_interlace;
1260
1261 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
1262 if (cmdline_mode->specified == false)
1263 return mode;
1264
1265 /* attempt to find a matching mode in the list of modes
1266 * we have gotten so far, if not add a CVT mode that conforms
1267 */
1268 if (cmdline_mode->rb || cmdline_mode->margins)
1269 goto create_mode;
1270
1271 prefer_non_interlace = !cmdline_mode->interlace;
1272 again:
1273 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1274 /* check width/height */
1275 if (mode->hdisplay != cmdline_mode->xres ||
1276 mode->vdisplay != cmdline_mode->yres)
1277 continue;
1278
1279 if (cmdline_mode->refresh_specified) {
1280 if (mode->vrefresh != cmdline_mode->refresh)
1281 continue;
1282 }
1283
1284 if (cmdline_mode->interlace) {
1285 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1286 continue;
1287 } else if (prefer_non_interlace) {
1288 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1289 continue;
1290 }
1291 return mode;
1292 }
1293
1294 if (prefer_non_interlace) {
1295 prefer_non_interlace = false;
1296 goto again;
1297 }
1298
1299 create_mode:
1300 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1301 cmdline_mode);
1302 list_add(&mode->head, &fb_helper_conn->connector->modes);
1303 return mode;
1304 }
1305 EXPORT_SYMBOL(drm_pick_cmdline_mode);
1306
drm_connector_enabled(struct drm_connector * connector,bool strict)1307 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1308 {
1309 bool enable;
1310
1311 if (strict)
1312 enable = connector->status == connector_status_connected;
1313 else
1314 enable = connector->status != connector_status_disconnected;
1315
1316 return enable;
1317 }
1318
drm_enable_connectors(struct drm_fb_helper * fb_helper,bool * enabled)1319 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1320 bool *enabled)
1321 {
1322 bool any_enabled = false;
1323 struct drm_connector *connector;
1324 int i = 0;
1325
1326 for (i = 0; i < fb_helper->connector_count; i++) {
1327 connector = fb_helper->connector_info[i]->connector;
1328 enabled[i] = drm_connector_enabled(connector, true);
1329 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1330 enabled[i] ? "yes" : "no");
1331 any_enabled |= enabled[i];
1332 }
1333
1334 if (any_enabled)
1335 return;
1336
1337 for (i = 0; i < fb_helper->connector_count; i++) {
1338 connector = fb_helper->connector_info[i]->connector;
1339 enabled[i] = drm_connector_enabled(connector, false);
1340 }
1341 }
1342
drm_target_cloned(struct drm_fb_helper * fb_helper,struct drm_display_mode ** modes,bool * enabled,int width,int height)1343 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1344 struct drm_display_mode **modes,
1345 bool *enabled, int width, int height)
1346 {
1347 int count, i, j;
1348 bool can_clone = false;
1349 struct drm_fb_helper_connector *fb_helper_conn;
1350 struct drm_display_mode *dmt_mode, *mode;
1351
1352 /* only contemplate cloning in the single crtc case */
1353 if (fb_helper->crtc_count > 1)
1354 return false;
1355
1356 count = 0;
1357 for (i = 0; i < fb_helper->connector_count; i++) {
1358 if (enabled[i])
1359 count++;
1360 }
1361
1362 /* only contemplate cloning if more than one connector is enabled */
1363 if (count <= 1)
1364 return false;
1365
1366 /* check the command line or if nothing common pick 1024x768 */
1367 can_clone = true;
1368 for (i = 0; i < fb_helper->connector_count; i++) {
1369 if (!enabled[i])
1370 continue;
1371 fb_helper_conn = fb_helper->connector_info[i];
1372 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1373 if (!modes[i]) {
1374 can_clone = false;
1375 break;
1376 }
1377 for (j = 0; j < i; j++) {
1378 if (!enabled[j])
1379 continue;
1380 if (!drm_mode_equal(modes[j], modes[i]))
1381 can_clone = false;
1382 }
1383 }
1384
1385 if (can_clone) {
1386 DRM_DEBUG_KMS("can clone using command line\n");
1387 return true;
1388 }
1389
1390 /* try and find a 1024x768 mode on each connector */
1391 can_clone = true;
1392 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
1393
1394 for (i = 0; i < fb_helper->connector_count; i++) {
1395
1396 if (!enabled[i])
1397 continue;
1398
1399 fb_helper_conn = fb_helper->connector_info[i];
1400 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1401 if (drm_mode_equal(mode, dmt_mode))
1402 modes[i] = mode;
1403 }
1404 if (!modes[i])
1405 can_clone = false;
1406 }
1407
1408 if (can_clone) {
1409 DRM_DEBUG_KMS("can clone using 1024x768\n");
1410 return true;
1411 }
1412 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1413 return false;
1414 }
1415
drm_target_preferred(struct drm_fb_helper * fb_helper,struct drm_display_mode ** modes,bool * enabled,int width,int height)1416 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1417 struct drm_display_mode **modes,
1418 bool *enabled, int width, int height)
1419 {
1420 struct drm_fb_helper_connector *fb_helper_conn;
1421 int i;
1422
1423 for (i = 0; i < fb_helper->connector_count; i++) {
1424 fb_helper_conn = fb_helper->connector_info[i];
1425
1426 if (enabled[i] == false)
1427 continue;
1428
1429 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1430 fb_helper_conn->connector->base.id);
1431
1432 /* got for command line mode first */
1433 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1434 if (!modes[i]) {
1435 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1436 fb_helper_conn->connector->base.id);
1437 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1438 }
1439 /* No preferred modes, pick one off the list */
1440 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1441 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1442 break;
1443 }
1444 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1445 "none");
1446 }
1447 return true;
1448 }
1449
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)1450 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1451 struct drm_fb_helper_crtc **best_crtcs,
1452 struct drm_display_mode **modes,
1453 int n, int width, int height)
1454 {
1455 int c, o;
1456 struct drm_connector *connector;
1457 struct drm_connector_helper_funcs *connector_funcs;
1458 struct drm_encoder *encoder;
1459 int my_score, best_score, score;
1460 struct drm_fb_helper_crtc **crtcs, *crtc;
1461 struct drm_fb_helper_connector *fb_helper_conn;
1462
1463 if (n == fb_helper->connector_count)
1464 return 0;
1465
1466 fb_helper_conn = fb_helper->connector_info[n];
1467 connector = fb_helper_conn->connector;
1468
1469 best_crtcs[n] = NULL;
1470 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1471 if (modes[n] == NULL)
1472 return best_score;
1473
1474 crtcs = kzalloc(fb_helper->connector_count *
1475 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1476 if (!crtcs)
1477 return best_score;
1478
1479 my_score = 1;
1480 if (connector->status == connector_status_connected)
1481 my_score++;
1482 if (drm_has_cmdline_mode(fb_helper_conn))
1483 my_score++;
1484 if (drm_has_preferred_mode(fb_helper_conn, width, height))
1485 my_score++;
1486
1487 connector_funcs = connector->helper_private;
1488 encoder = connector_funcs->best_encoder(connector);
1489 if (!encoder)
1490 goto out;
1491
1492 /* select a crtc for this connector and then attempt to configure
1493 remaining connectors */
1494 for (c = 0; c < fb_helper->crtc_count; c++) {
1495 crtc = &fb_helper->crtc_info[c];
1496
1497 if ((encoder->possible_crtcs & (1 << c)) == 0)
1498 continue;
1499
1500 for (o = 0; o < n; o++)
1501 if (best_crtcs[o] == crtc)
1502 break;
1503
1504 if (o < n) {
1505 /* ignore cloning unless only a single crtc */
1506 if (fb_helper->crtc_count > 1)
1507 continue;
1508
1509 if (!drm_mode_equal(modes[o], modes[n]))
1510 continue;
1511 }
1512
1513 crtcs[n] = crtc;
1514 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1515 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1516 width, height);
1517 if (score > best_score) {
1518 best_score = score;
1519 memcpy(best_crtcs, crtcs,
1520 fb_helper->connector_count *
1521 sizeof(struct drm_fb_helper_crtc *));
1522 }
1523 }
1524 out:
1525 kfree(crtcs);
1526 return best_score;
1527 }
1528
drm_setup_crtcs(struct drm_fb_helper * fb_helper)1529 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1530 {
1531 struct drm_device *dev = fb_helper->dev;
1532 struct drm_fb_helper_crtc **crtcs;
1533 struct drm_display_mode **modes;
1534 struct drm_mode_set *modeset;
1535 bool *enabled;
1536 int width, height;
1537 int i;
1538
1539 DRM_DEBUG_KMS("\n");
1540
1541 width = dev->mode_config.max_width;
1542 height = dev->mode_config.max_height;
1543
1544 crtcs = kcalloc(dev->mode_config.num_connector,
1545 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1546 modes = kcalloc(dev->mode_config.num_connector,
1547 sizeof(struct drm_display_mode *), GFP_KERNEL);
1548 enabled = kcalloc(dev->mode_config.num_connector,
1549 sizeof(bool), GFP_KERNEL);
1550 if (!crtcs || !modes || !enabled) {
1551 DRM_ERROR("Memory allocation failed\n");
1552 goto out;
1553 }
1554
1555
1556 drm_enable_connectors(fb_helper, enabled);
1557
1558 if (!(fb_helper->funcs->initial_config &&
1559 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
1560 enabled, width, height))) {
1561 memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0]));
1562 memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0]));
1563
1564 if (!drm_target_cloned(fb_helper,
1565 modes, enabled, width, height) &&
1566 !drm_target_preferred(fb_helper,
1567 modes, enabled, width, height))
1568 DRM_ERROR("Unable to find initial modes\n");
1569
1570 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
1571 width, height);
1572
1573 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1574 }
1575
1576 /* need to set the modesets up here for use later */
1577 /* fill out the connector<->crtc mappings into the modesets */
1578 for (i = 0; i < fb_helper->crtc_count; i++) {
1579 modeset = &fb_helper->crtc_info[i].mode_set;
1580 modeset->num_connectors = 0;
1581 modeset->fb = NULL;
1582 }
1583
1584 for (i = 0; i < fb_helper->connector_count; i++) {
1585 struct drm_display_mode *mode = modes[i];
1586 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1587 modeset = &fb_crtc->mode_set;
1588
1589 if (mode && fb_crtc) {
1590 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1591 mode->name, fb_crtc->mode_set.crtc->base.id);
1592 fb_crtc->desired_mode = mode;
1593 if (modeset->mode)
1594 drm_mode_destroy(dev, modeset->mode);
1595 modeset->mode = drm_mode_duplicate(dev,
1596 fb_crtc->desired_mode);
1597 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1598 modeset->fb = fb_helper->fb;
1599 }
1600 }
1601
1602 /* Clear out any old modes if there are no more connected outputs. */
1603 for (i = 0; i < fb_helper->crtc_count; i++) {
1604 modeset = &fb_helper->crtc_info[i].mode_set;
1605 if (modeset->num_connectors == 0) {
1606 BUG_ON(modeset->fb);
1607 BUG_ON(modeset->num_connectors);
1608 if (modeset->mode)
1609 drm_mode_destroy(dev, modeset->mode);
1610 modeset->mode = NULL;
1611 }
1612 }
1613 out:
1614 kfree(crtcs);
1615 kfree(modes);
1616 kfree(enabled);
1617 }
1618
1619 /**
1620 * drm_fb_helper_initial_config - setup a sane initial connector configuration
1621 * @fb_helper: fb_helper device struct
1622 * @bpp_sel: bpp value to use for the framebuffer configuration
1623 *
1624 * Scans the CRTCs and connectors and tries to put together an initial setup.
1625 * At the moment, this is a cloned configuration across all heads with
1626 * a new framebuffer object as the backing store.
1627 *
1628 * Note that this also registers the fbdev and so allows userspace to call into
1629 * the driver through the fbdev interfaces.
1630 *
1631 * This function will call down into the ->fb_probe callback to let
1632 * the driver allocate and initialize the fbdev info structure and the drm
1633 * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
1634 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
1635 * values for the fbdev info structure.
1636 *
1637 * RETURNS:
1638 * Zero if everything went ok, nonzero otherwise.
1639 */
drm_fb_helper_initial_config(struct drm_fb_helper * fb_helper,int bpp_sel)1640 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1641 {
1642 struct drm_device *dev = fb_helper->dev;
1643 int count = 0;
1644
1645 mutex_lock(&dev->mode_config.mutex);
1646 count = drm_fb_helper_probe_connector_modes(fb_helper,
1647 dev->mode_config.max_width,
1648 dev->mode_config.max_height);
1649 mutex_unlock(&dev->mode_config.mutex);
1650 /*
1651 * we shouldn't end up with no modes here.
1652 */
1653 if (count == 0)
1654 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
1655
1656 drm_setup_crtcs(fb_helper);
1657
1658 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1659 }
1660 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1661
1662 /**
1663 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1664 * probing all the outputs attached to the fb
1665 * @fb_helper: the drm_fb_helper
1666 *
1667 * Scan the connectors attached to the fb_helper and try to put together a
1668 * setup after *notification of a change in output configuration.
1669 *
1670 * Called at runtime, takes the mode config locks to be able to check/change the
1671 * modeset configuration. Must be run from process context (which usually means
1672 * either the output polling work or a work item launched from the driver's
1673 * hotplug interrupt).
1674 *
1675 * Note that drivers may call this even before calling
1676 * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows
1677 * for a race-free fbcon setup and will make sure that the fbdev emulation will
1678 * not miss any hotplug events.
1679 *
1680 * RETURNS:
1681 * 0 on success and a non-zero error code otherwise.
1682 */
drm_fb_helper_hotplug_event(struct drm_fb_helper * fb_helper)1683 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1684 {
1685 struct drm_device *dev = fb_helper->dev;
1686 u32 max_width, max_height;
1687
1688 mutex_lock(&fb_helper->dev->mode_config.mutex);
1689 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
1690 fb_helper->delayed_hotplug = true;
1691 mutex_unlock(&fb_helper->dev->mode_config.mutex);
1692 return 0;
1693 }
1694 DRM_DEBUG_KMS("\n");
1695
1696 max_width = fb_helper->fb->width;
1697 max_height = fb_helper->fb->height;
1698
1699 drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height);
1700 mutex_unlock(&fb_helper->dev->mode_config.mutex);
1701
1702 drm_modeset_lock_all(dev);
1703 drm_setup_crtcs(fb_helper);
1704 drm_modeset_unlock_all(dev);
1705 drm_fb_helper_set_par(fb_helper->fbdev);
1706
1707 return 0;
1708 }
1709 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
1710
1711 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
1712 * but the module doesn't depend on any fb console symbols. At least
1713 * attempt to load fbcon to avoid leaving the system without a usable console.
1714 */
1715 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
drm_fb_helper_modinit(void)1716 static int __init drm_fb_helper_modinit(void)
1717 {
1718 const char *name = "fbcon";
1719 struct module *fbcon;
1720
1721 mutex_lock(&module_mutex);
1722 fbcon = find_module(name);
1723 mutex_unlock(&module_mutex);
1724
1725 if (!fbcon)
1726 request_module_nowait(name);
1727 return 0;
1728 }
1729
1730 module_init(drm_fb_helper_modinit);
1731 #endif
1732