blob: cac422916c7afbbaf38095354784fa3d81632294 [file] [log] [blame]
Dave Airlie785b93e2009-08-28 15:46:53 +10001/*
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 */
Sachin Kamatd56b1b92012-11-15 03:43:29 +000030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
Andy Shevchenko3b40a442010-02-02 14:40:32 -080032#include <linux/kernel.h>
Dave Airlie785b93e2009-08-28 15:46:53 +100033#include <linux/sysrq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Dave Airlie785b93e2009-08-28 15:46:53 +100035#include <linux/fb.h>
Paul Gortmakere0cd3602011-08-30 11:04:30 -040036#include <linux/module.h>
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/drmP.h>
38#include <drm/drm_crtc.h>
39#include <drm/drm_fb_helper.h>
40#include <drm/drm_crtc_helper.h>
Dave Airlie785b93e2009-08-28 15:46:53 +100041
42static LIST_HEAD(kernel_fb_helper_list);
43
Daniel Vetterd0ddc0332012-11-01 14:45:17 +010044/**
45 * DOC: fbdev helpers
46 *
47 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
Thierry Reding83c617c2014-04-29 11:44:35 +020048 * mode setting driver. They can be used mostly independently from the crtc
Daniel Vetterd0ddc0332012-11-01 14:45:17 +010049 * helper functions used by many drivers to implement the kernel mode setting
50 * interfaces.
Daniel Vetter207fd322013-01-20 22:13:14 +010051 *
Thierry Reding10a23102014-06-27 17:19:24 +020052 * 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().
Daniel Vetter207fd322013-01-20 22:13:14 +010057 *
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
Thierry Reding83c617c2014-04-29 11:44:35 +020063 * code provides a ->output_poll_changed callback.
Daniel Vetter207fd322013-01-20 22:13:14 +010064 *
65 * All other functions exported by the fb helper library can be used to
66 * implement the fbdev driver interface by the driver.
Thierry Reding10a23102014-06-27 17:19:24 +020067 *
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().
Daniel Vetterd0ddc0332012-11-01 14:45:17 +010080 */
81
Daniel Vetter207fd322013-01-20 22:13:14 +010082/**
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 */
Dave Airlie0b4c0f32010-03-30 05:34:15 +000095int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +100096{
Dave Airlie0b4c0f32010-03-30 05:34:15 +000097 struct drm_device *dev = fb_helper->dev;
98 struct drm_connector *connector;
99 int i;
Dave Airlied50ba252009-09-23 14:44:08 +1000100
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000101 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 }
Dave Airlied50ba252009-09-23 14:44:08 +1000111 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000112fail:
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;
Dave Airlied50ba252009-09-23 14:44:08 +1000119}
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000120EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
Dave Airlied50ba252009-09-23 14:44:08 +1000121
Dave Airlie65c2a892014-06-05 14:01:30 +1000122int 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) {
Damien Lespiau14f476f2014-08-08 19:15:20 +0100129 temp = krealloc(fb_helper->connector_info, sizeof(struct drm_fb_helper_connector *) * (fb_helper->connector_count + 1), GFP_KERNEL);
Dave Airlie65c2a892014-06-05 14:01:30 +1000130 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}
146EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
147
Rob Clark2148f182015-01-26 10:11:08 -0500148static 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
Dave Airlie65c2a892014-06-05 14:01:30 +1000173int 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);
Rob Clark2148f182015-01-26 10:11:08 -0500195
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
Dave Airlie65c2a892014-06-05 14:01:30 +1000200 return 0;
201}
202EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
203
Jason Wessel99231022010-10-13 14:09:43 -0500204static 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
Ville Syrjälä04c0c562013-05-27 20:19:57 +0300209 if (helper->funcs->gamma_get == NULL)
210 return;
211
Jason Wessel99231022010-10-13 14:09:43 -0500212 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
220static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
221{
222 uint16_t *r_base, *g_base, *b_base;
223
Laurent Pinchartebe0f242012-05-17 13:27:24 +0200224 if (crtc->funcs->gamma_set == NULL)
225 return;
226
Jason Wessel99231022010-10-13 14:09:43 -0500227 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
Daniel Vetter207fd322013-01-20 22:13:14 +0100234/**
235 * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter
236 * @info: fbdev registered by the helper
237 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500238int drm_fb_helper_debug_enter(struct fb_info *info)
239{
240 struct drm_fb_helper *helper = info->par;
Jani Nikulabe26a662015-03-11 11:51:06 +0200241 const struct drm_crtc_helper_funcs *funcs;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500242 int i;
243
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500244 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;
Jason Wessel99231022010-10-13 14:09:43 -0500253 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500254 funcs->mode_set_base_atomic(mode_set->crtc,
255 mode_set->fb,
256 mode_set->x,
Jason Wessel413d45d2010-09-26 06:47:25 -0500257 mode_set->y,
Jason Wessel21c74a82010-10-13 14:09:44 -0500258 ENTER_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500259 }
260 }
261
262 return 0;
263}
264EXPORT_SYMBOL(drm_fb_helper_debug_enter);
265
266/* Find the real fb for a given fb helper CRTC */
267static 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)
Matt Roperf4510a22014-04-01 15:22:40 -0700274 return c->primary->fb;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500275 }
276
277 return NULL;
278}
279
Daniel Vetter207fd322013-01-20 22:13:14 +0100280/**
281 * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave
282 * @info: fbdev registered by the helper
283 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500284int drm_fb_helper_debug_leave(struct fb_info *info)
285{
286 struct drm_fb_helper *helper = info->par;
287 struct drm_crtc *crtc;
Jani Nikulabe26a662015-03-11 11:51:06 +0200288 const struct drm_crtc_helper_funcs *funcs;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500289 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
Jason Wessel99231022010-10-13 14:09:43 -0500306 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500307 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
Jason Wessel21c74a82010-10-13 14:09:44 -0500308 crtc->y, LEAVE_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500309 }
310
311 return 0;
312}
313EXPORT_SYMBOL(drm_fb_helper_debug_leave);
314
Rob Clark5ea1f752014-05-30 12:29:48 -0400315static bool restore_fbdev_mode(struct drm_fb_helper *fb_helper)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100316{
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300317 struct drm_device *dev = fb_helper->dev;
318 struct drm_plane *plane;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100319 bool error = false;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300320 int i;
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100321
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300322 drm_warn_on_modeset_not_all_locked(dev);
323
Sonika Jindal9783de22014-08-05 11:26:57 +0530324 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Matt Ropere27dde32014-04-01 15:22:30 -0700325 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
326 drm_plane_force_disable(plane);
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100327
Sonika Jindal9783de22014-08-05 11:26:57 +0530328 if (dev->mode_config.rotation_property) {
Thomas Wood3a5f87c2014-08-20 14:45:00 +0100329 drm_mode_plane_set_obj_prop(plane,
330 dev->mode_config.rotation_property,
331 BIT(DRM_ROTATE_0));
Sonika Jindal9783de22014-08-05 11:26:57 +0530332 }
333 }
334
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100335 for (i = 0; i < fb_helper->crtc_count; i++) {
336 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300337 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
Daniel Vetter2d13b672012-12-11 13:47:23 +0100346 ret = drm_mode_set_config_internal(mode_set);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100347 if (ret)
348 error = true;
349 }
350 return error;
351}
Rob Clark5ea1f752014-05-30 12:29:48 -0400352/**
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 */
363static 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 */
376bool 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;
Dave Airliee2809c72014-11-26 13:15:24 +1000380 bool do_delayed = false;
381
Rob Clark5ea1f752014-05-30 12:29:48 -0400382 drm_modeset_lock_all(dev);
383 ret = restore_fbdev_mode(fb_helper);
Dave Airliee2809c72014-11-26 13:15:24 +1000384
385 do_delayed = fb_helper->delayed_hotplug;
386 if (do_delayed)
387 fb_helper->delayed_hotplug = false;
Rob Clark5ea1f752014-05-30 12:29:48 -0400388 drm_modeset_unlock_all(dev);
Dave Airliee2809c72014-11-26 13:15:24 +1000389
390 if (do_delayed)
391 drm_fb_helper_hotplug_event(fb_helper);
Rob Clark5ea1f752014-05-30 12:29:48 -0400392 return ret;
393}
394EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100395
Daniel Vetterd21bf462013-01-20 18:09:52 +0100396/*
397 * restore fbcon display for all kms driver's using this helper, used for sysrq
398 * and panic handling.
399 */
Sachin Kamat78b9c352012-08-01 17:15:32 +0530400static bool drm_fb_helper_force_kernel_mode(void)
Dave Airlie785b93e2009-08-28 15:46:53 +1000401{
Dave Airlie785b93e2009-08-28 15:46:53 +1000402 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) {
Thierry Redingb77f0762014-04-29 11:44:32 +0200409 struct drm_device *dev = helper->dev;
410
411 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100412 continue;
413
Daniel Vettercb597bb2014-07-27 19:09:33 +0200414 /*
415 * NOTE: Use trylock mode to avoid deadlocks and sleeping in
416 * panic context.
Rob Clark51fd3712013-11-19 12:10:12 -0500417 */
Daniel Vettercb597bb2014-07-27 19:09:33 +0200418 if (__drm_modeset_lock_all(dev, true) != 0) {
Thierry Redingb77f0762014-04-29 11:44:32 +0200419 error = true;
420 continue;
421 }
422
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100423 ret = drm_fb_helper_restore_fbdev_mode(helper);
424 if (ret)
425 error = true;
Thierry Redingb77f0762014-04-29 11:44:32 +0200426
Daniel Vettercb597bb2014-07-27 19:09:33 +0200427 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000428 }
429 return error;
430}
431
Daniel Vetter43c8a842013-01-20 18:18:07 +0100432static int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
Dave Airlie785b93e2009-08-28 15:46:53 +1000433 void *panic_str)
434{
Hugh Dickinsd68752c2011-10-17 17:06:40 -0700435 /*
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
Sachin Kamatd56b1b92012-11-15 03:43:29 +0000442 pr_err("panic occurred, switching back to text console\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000443 return drm_fb_helper_force_kernel_mode();
Dave Airlie785b93e2009-08-28 15:46:53 +1000444}
Dave Airlie785b93e2009-08-28 15:46:53 +1000445
446static struct notifier_block paniced = {
447 .notifier_call = drm_fb_helper_panic,
448};
449
Daniel Vetter20c60c32012-12-17 12:13:23 +0100450static 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
Paulo Zanoni520edd12013-11-27 18:24:08 -0200456 /* 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
Daniel Vetter20c60c32012-12-17 12:13:23 +0100461 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -0700462 if (crtc->primary->fb)
Daniel Vetter20c60c32012-12-17 12:13:23 +0100463 crtcs_bound++;
Matt Roperf4510a22014-04-01 15:22:40 -0700464 if (crtc->primary->fb == fb_helper->fb)
Daniel Vetter20c60c32012-12-17 12:13:23 +0100465 bound++;
466 }
467
468 if (bound < crtcs_bound)
469 return false;
Paulo Zanoni520edd12013-11-27 18:24:08 -0200470
Daniel Vetter20c60c32012-12-17 12:13:23 +0100471 return true;
472}
473
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200474#ifdef CONFIG_MAGIC_SYSRQ
Dave Airlie785b93e2009-08-28 15:46:53 +1000475static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
476{
Daniel Vetterd21bf462013-01-20 18:09:52 +0100477 bool ret;
478 ret = drm_fb_helper_force_kernel_mode();
479 if (ret == true)
480 DRM_ERROR("Failed to restore crtc configuration\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000481}
482static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
483
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700484static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000485{
486 schedule_work(&drm_fb_helper_restore_work);
487}
488
489static 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};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000494#else
495static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200496#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000497
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100498static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
Dave Airlie785b93e2009-08-28 15:46:53 +1000499{
500 struct drm_fb_helper *fb_helper = info->par;
501 struct drm_device *dev = fb_helper->dev;
502 struct drm_crtc *crtc;
Jesse Barnes023eb572010-07-02 10:48:08 -0700503 struct drm_connector *connector;
Jesse Barnes023eb572010-07-02 10:48:08 -0700504 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000505
506 /*
Daniel Vetter1b1d5392013-01-24 16:42:07 +0100507 * 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 /*
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100515 * For each CRTC in this fb, turn the connectors on/off.
Dave Airlie785b93e2009-08-28 15:46:53 +1000516 */
Daniel Vetter84849902012-12-02 00:28:11 +0100517 drm_modeset_lock_all(dev);
Daniel Vetter20c60c32012-12-17 12:13:23 +0100518 if (!drm_fb_helper_is_bound(fb_helper)) {
519 drm_modeset_unlock_all(dev);
520 return;
521 }
522
Jesse Barnese87b2c42009-09-17 18:14:41 -0700523 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000524 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000525
Dave Airlie8be48d92010-03-30 05:34:14 +0000526 if (!crtc->enabled)
527 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000528
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100529 /* Walk the connectors & encoders on this fb turning them on/off */
Jesse Barnes023eb572010-07-02 10:48:08 -0700530 for (j = 0; j < fb_helper->connector_count; j++) {
531 connector = fb_helper->connector_info[j]->connector;
Daniel Vettere04190e2012-09-07 10:14:52 +0200532 connector->funcs->dpms(connector, dpms_mode);
Rob Clark58495562012-10-11 20:50:56 -0500533 drm_object_property_set_value(&connector->base,
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100534 dev->mode_config.dpms_property, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700535 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000536 }
Daniel Vetter84849902012-12-02 00:28:11 +0100537 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000538}
539
Daniel Vetter207fd322013-01-20 22:13:14 +0100540/**
541 * drm_fb_helper_blank - implementation for ->fb_blank
542 * @blank: desired blanking state
543 * @info: fbdev registered by the helper
544 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000545int drm_fb_helper_blank(int blank, struct fb_info *info)
546{
547 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000548 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000549 case FB_BLANK_UNBLANK:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100550 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000551 break;
James Simmons731b5a12009-10-29 20:39:07 +0000552 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000553 case FB_BLANK_NORMAL:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100554 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000555 break;
James Simmons731b5a12009-10-29 20:39:07 +0000556 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000557 case FB_BLANK_HSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100558 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000559 break;
James Simmons731b5a12009-10-29 20:39:07 +0000560 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000561 case FB_BLANK_VSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100562 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
Dave Airlie785b93e2009-08-28 15:46:53 +1000563 break;
James Simmons731b5a12009-10-29 20:39:07 +0000564 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000565 case FB_BLANK_POWERDOWN:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100566 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000567 break;
568 }
569 return 0;
570}
571EXPORT_SYMBOL(drm_fb_helper_blank);
572
573static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
574{
575 int i;
576
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000577 for (i = 0; i < helper->connector_count; i++)
578 kfree(helper->connector_info[i]);
579 kfree(helper->connector_info);
Sascha Hauera1b77362012-02-01 11:38:22 +0100580 for (i = 0; i < helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000581 kfree(helper->crtc_info[i].mode_set.connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100582 if (helper->crtc_info[i].mode_set.mode)
583 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
584 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000585 kfree(helper->crtc_info);
586}
587
Daniel Vetter207fd322013-01-20 22:13:14 +0100588/**
Thierry Reding10a23102014-06-27 17:19:24 +0200589 * 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 */
597void 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}
604EXPORT_SYMBOL(drm_fb_helper_prepare);
605
606/**
Daniel Vetter207fd322013-01-20 22:13:14 +0100607 * 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 *
Thierry Reding10a23102014-06-27 17:19:24 +0200618 * Drivers must call drm_fb_helper_prepare() before calling this function.
Daniel Vetter207fd322013-01-20 22:13:14 +0100619 *
620 * RETURNS:
621 * Zero if everything went ok, nonzero otherwise.
622 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000623int drm_fb_helper_init(struct drm_device *dev,
624 struct drm_fb_helper *fb_helper,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000625 int crtc_count, int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000626{
Dave Airlie785b93e2009-08-28 15:46:53 +1000627 struct drm_crtc *crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000628 int i;
629
Xiubo Li04cfe972014-03-10 09:33:58 +0800630 if (!max_conn_count)
631 return -EINVAL;
632
Dave Airlie4abe3522010-03-30 05:34:18 +0000633 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
634 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000635 return -ENOMEM;
636
Dave Airlie4abe3522010-03-30 05:34:18 +0000637 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);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000641 return -ENOMEM;
642 }
Dave Airlie65c2a892014-06-05 14:01:30 +1000643 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
Dave Airlie4abe3522010-03-30 05:34:18 +0000644 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000645
646 for (i = 0; i < crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000647 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000648 kcalloc(max_conn_count,
649 sizeof(struct drm_connector *),
650 GFP_KERNEL);
651
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200652 if (!fb_helper->crtc_info[i].mode_set.connectors)
Dave Airlie785b93e2009-08-28 15:46:53 +1000653 goto out_free;
Dave Airlie4abe3522010-03-30 05:34:18 +0000654 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000655 }
656
657 i = 0;
658 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000659 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000660 i++;
661 }
Sascha Hauere9ad3182012-02-01 11:38:25 +0100662
Dave Airlie785b93e2009-08-28 15:46:53 +1000663 return 0;
664out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000665 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000666 return -ENOMEM;
667}
Dave Airlie4abe3522010-03-30 05:34:18 +0000668EXPORT_SYMBOL(drm_fb_helper_init);
669
670void 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)) {
Sachin Kamatd56b1b92012-11-15 03:43:29 +0000675 pr_info("drm: unregistered panic notifier\n");
Dave Airlie4abe3522010-03-30 05:34:18 +0000676 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
Dave Airlie4abe3522010-03-30 05:34:18 +0000684}
685EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000686
Dave Airliec850cb72009-10-23 18:49:03 +1000687static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000688 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
Dave Airliec850cb72009-10-23 18:49:03 +1000694 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);
Rob Clark9da12b6a2011-02-16 02:45:51 +0000707 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 }
Dave Airliec850cb72009-10-23 18:49:03 +1000712 palette[regno] = value;
713 return 0;
714 }
715
Ville Syrjälä04c0c562013-05-27 20:19:57 +0300716 /*
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
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000724 pindex = regno;
725
726 if (fb->bits_per_pixel == 16) {
727 pindex = regno << 3;
728
729 if (fb->depth == 16 && regno > 63)
Dave Airliec850cb72009-10-23 18:49:03 +1000730 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000731 if (fb->depth == 15 && regno > 31)
Dave Airliec850cb72009-10-23 18:49:03 +1000732 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000733
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);
Dave Airliec850cb72009-10-23 18:49:03 +1000756 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000757}
758
Daniel Vetter207fd322013-01-20 22:13:14 +0100759/**
760 * drm_fb_helper_setcmap - implementation for ->fb_setcmap
761 * @cmap: cmap to set
762 * @info: fbdev registered by the helper
763 */
Dave Airlie068143d2009-10-05 09:58:02 +1000764int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
765{
766 struct drm_fb_helper *fb_helper = info->par;
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300767 struct drm_device *dev = fb_helper->dev;
Jani Nikulabe26a662015-03-11 11:51:06 +0200768 const struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +1000769 u16 *red, *green, *blue, *transp;
770 struct drm_crtc *crtc;
roel062ac622011-03-07 18:00:34 +0100771 int i, j, rc = 0;
Dave Airlie068143d2009-10-05 09:58:02 +1000772 int start;
773
Rui Wang9aa609e2014-12-15 11:28:26 -0800774 if (__drm_modeset_lock_all(dev, !!oops_in_progress)) {
775 return -EBUSY;
776 }
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300777 if (!drm_fb_helper_is_bound(fb_helper)) {
778 drm_modeset_unlock_all(dev);
779 return -EBUSY;
780 }
781
Dave Airlie8be48d92010-03-30 05:34:14 +0000782 for (i = 0; i < fb_helper->crtc_count; i++) {
783 crtc = fb_helper->crtc_info[i].mode_set.crtc;
784 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +1000785
786 red = cmap->red;
787 green = cmap->green;
788 blue = cmap->blue;
789 transp = cmap->transp;
790 start = cmap->start;
791
roel062ac622011-03-07 18:00:34 +0100792 for (j = 0; j < cmap->len; j++) {
Dave Airlie068143d2009-10-05 09:58:02 +1000793 u16 hred, hgreen, hblue, htransp = 0xffff;
794
795 hred = *red++;
796 hgreen = *green++;
797 hblue = *blue++;
798
799 if (transp)
800 htransp = *transp++;
801
Dave Airliec850cb72009-10-23 18:49:03 +1000802 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
803 if (rc)
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300804 goto out;
Dave Airlie068143d2009-10-05 09:58:02 +1000805 }
Ville Syrjälä04c0c562013-05-27 20:19:57 +0300806 if (crtc_funcs->load_lut)
807 crtc_funcs->load_lut(crtc);
Dave Airlie068143d2009-10-05 09:58:02 +1000808 }
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300809 out:
810 drm_modeset_unlock_all(dev);
Dave Airlie068143d2009-10-05 09:58:02 +1000811 return rc;
812}
813EXPORT_SYMBOL(drm_fb_helper_setcmap);
814
Daniel Vetter207fd322013-01-20 22:13:14 +0100815/**
816 * drm_fb_helper_check_var - implementation for ->fb_check_var
817 * @var: screeninfo to check
818 * @info: fbdev registered by the helper
819 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000820int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
821 struct fb_info *info)
822{
823 struct drm_fb_helper *fb_helper = info->par;
824 struct drm_framebuffer *fb = fb_helper->fb;
825 int depth;
826
Jason Wesself90ebd92010-08-05 09:22:32 -0500827 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +1000828 return -EINVAL;
829
830 /* Need to resize the fb object !!! */
Chris Wilson62fb3762012-03-26 21:15:53 +0100831 if (var->bits_per_pixel > fb->bits_per_pixel ||
832 var->xres > fb->width || var->yres > fb->height ||
833 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
Dave Airlie509c7d82010-01-08 09:27:08 +1000834 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
Chris Wilson62fb3762012-03-26 21:15:53 +0100835 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
836 var->xres, var->yres, var->bits_per_pixel,
837 var->xres_virtual, var->yres_virtual,
Dave Airlie509c7d82010-01-08 09:27:08 +1000838 fb->width, fb->height, fb->bits_per_pixel);
Dave Airlie785b93e2009-08-28 15:46:53 +1000839 return -EINVAL;
840 }
841
842 switch (var->bits_per_pixel) {
843 case 16:
844 depth = (var->green.length == 6) ? 16 : 15;
845 break;
846 case 32:
847 depth = (var->transp.length > 0) ? 32 : 24;
848 break;
849 default:
850 depth = var->bits_per_pixel;
851 break;
852 }
853
854 switch (depth) {
855 case 8:
856 var->red.offset = 0;
857 var->green.offset = 0;
858 var->blue.offset = 0;
859 var->red.length = 8;
860 var->green.length = 8;
861 var->blue.length = 8;
862 var->transp.length = 0;
863 var->transp.offset = 0;
864 break;
865 case 15:
866 var->red.offset = 10;
867 var->green.offset = 5;
868 var->blue.offset = 0;
869 var->red.length = 5;
870 var->green.length = 5;
871 var->blue.length = 5;
872 var->transp.length = 1;
873 var->transp.offset = 15;
874 break;
875 case 16:
876 var->red.offset = 11;
877 var->green.offset = 5;
878 var->blue.offset = 0;
879 var->red.length = 5;
880 var->green.length = 6;
881 var->blue.length = 5;
882 var->transp.length = 0;
883 var->transp.offset = 0;
884 break;
885 case 24:
886 var->red.offset = 16;
887 var->green.offset = 8;
888 var->blue.offset = 0;
889 var->red.length = 8;
890 var->green.length = 8;
891 var->blue.length = 8;
892 var->transp.length = 0;
893 var->transp.offset = 0;
894 break;
895 case 32:
896 var->red.offset = 16;
897 var->green.offset = 8;
898 var->blue.offset = 0;
899 var->red.length = 8;
900 var->green.length = 8;
901 var->blue.length = 8;
902 var->transp.length = 8;
903 var->transp.offset = 24;
904 break;
905 default:
906 return -EINVAL;
907 }
908 return 0;
909}
910EXPORT_SYMBOL(drm_fb_helper_check_var);
911
Daniel Vetter207fd322013-01-20 22:13:14 +0100912/**
913 * drm_fb_helper_set_par - implementation for ->fb_set_par
914 * @info: fbdev registered by the helper
915 *
916 * This will let fbcon do the mode init and is called at initialization time by
917 * the fbdev core when registering the driver, and later on through the hotplug
918 * callback.
919 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000920int drm_fb_helper_set_par(struct fb_info *info)
921{
922 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +1000923 struct fb_var_screeninfo *var = &info->var;
Dave Airlie785b93e2009-08-28 15:46:53 +1000924
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100925 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +1000926 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000927 return -EINVAL;
928 }
929
Rob Clark5ea1f752014-05-30 12:29:48 -0400930 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +0000931
Dave Airlie785b93e2009-08-28 15:46:53 +1000932 return 0;
933}
934EXPORT_SYMBOL(drm_fb_helper_set_par);
935
Daniel Vetter207fd322013-01-20 22:13:14 +0100936/**
937 * drm_fb_helper_pan_display - implementation for ->fb_pan_display
938 * @var: updated screen information
939 * @info: fbdev registered by the helper
940 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000941int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
942 struct fb_info *info)
943{
944 struct drm_fb_helper *fb_helper = info->par;
945 struct drm_device *dev = fb_helper->dev;
946 struct drm_mode_set *modeset;
Dave Airlie785b93e2009-08-28 15:46:53 +1000947 int ret = 0;
948 int i;
949
Rui Wang9aa609e2014-12-15 11:28:26 -0800950 if (__drm_modeset_lock_all(dev, !!oops_in_progress)) {
951 return -EBUSY;
952 }
Daniel Vetter20c60c32012-12-17 12:13:23 +0100953 if (!drm_fb_helper_is_bound(fb_helper)) {
954 drm_modeset_unlock_all(dev);
955 return -EBUSY;
956 }
957
Dave Airlie8be48d92010-03-30 05:34:14 +0000958 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000959 modeset = &fb_helper->crtc_info[i].mode_set;
960
961 modeset->x = var->xoffset;
962 modeset->y = var->yoffset;
963
964 if (modeset->num_connectors) {
Daniel Vetter2d13b672012-12-11 13:47:23 +0100965 ret = drm_mode_set_config_internal(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +1000966 if (!ret) {
967 info->var.xoffset = var->xoffset;
968 info->var.yoffset = var->yoffset;
969 }
970 }
971 }
Daniel Vetter84849902012-12-02 00:28:11 +0100972 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000973 return ret;
974}
975EXPORT_SYMBOL(drm_fb_helper_pan_display);
976
Daniel Vetter8acf6582013-01-21 23:38:37 +0100977/*
Daniel Vetter207fd322013-01-20 22:13:14 +0100978 * Allocates the backing storage and sets up the fbdev info structure through
979 * the ->fb_probe callback and then registers the fbdev and sets up the panic
980 * notifier.
Daniel Vetter8acf6582013-01-21 23:38:37 +0100981 */
Daniel Vetterde1ace52013-01-20 21:50:49 +0100982static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
983 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +1000984{
Daniel Vetter8acf6582013-01-21 23:38:37 +0100985 int ret = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000986 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +0000987 int i;
Dave Airlie785b93e2009-08-28 15:46:53 +1000988 struct fb_info *info;
Dave Airlie38651672010-03-30 05:34:13 +0000989 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +0000990 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +0000991
992 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
993 sizes.surface_depth = 24;
994 sizes.surface_bpp = 32;
995 sizes.fb_width = (unsigned)-1;
996 sizes.fb_height = (unsigned)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +1000997
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000998 /* if driver picks 8 or 16 by default use that
999 for both depth/bpp */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001000 if (preferred_bpp != sizes.surface_bpp)
Dave Airlie38651672010-03-30 05:34:13 +00001001 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001002
Dave Airlie785b93e2009-08-28 15:46:53 +10001003 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001004 for (i = 0; i < fb_helper->connector_count; i++) {
1005 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +01001006 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +10001007
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001008 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +10001009
1010 if (cmdline_mode->bpp_specified) {
1011 switch (cmdline_mode->bpp) {
1012 case 8:
Dave Airlie38651672010-03-30 05:34:13 +00001013 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +10001014 break;
1015 case 15:
Dave Airlie38651672010-03-30 05:34:13 +00001016 sizes.surface_depth = 15;
1017 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001018 break;
1019 case 16:
Dave Airlie38651672010-03-30 05:34:13 +00001020 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001021 break;
1022 case 24:
Dave Airlie38651672010-03-30 05:34:13 +00001023 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +10001024 break;
1025 case 32:
Dave Airlie38651672010-03-30 05:34:13 +00001026 sizes.surface_depth = 24;
1027 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +10001028 break;
1029 }
1030 break;
1031 }
1032 }
1033
Dave Airlie8be48d92010-03-30 05:34:14 +00001034 crtc_count = 0;
1035 for (i = 0; i < fb_helper->crtc_count; i++) {
1036 struct drm_display_mode *desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001037 struct drm_mode_set *mode_set;
1038 int x, y, j;
1039 /* in case of tile group, are we the last tile vert or horiz?
1040 * If no tile group you are always the last one both vertically
1041 * and horizontally
1042 */
1043 bool lastv = true, lasth = true;
Rob Clark675c8322015-03-11 10:23:13 -04001044
Dave Airlie8be48d92010-03-30 05:34:14 +00001045 desired_mode = fb_helper->crtc_info[i].desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001046 mode_set = &fb_helper->crtc_info[i].mode_set;
Rob Clark675c8322015-03-11 10:23:13 -04001047
1048 if (!desired_mode)
1049 continue;
1050
1051 crtc_count++;
1052
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001053 x = fb_helper->crtc_info[i].x;
1054 y = fb_helper->crtc_info[i].y;
Rob Clark675c8322015-03-11 10:23:13 -04001055
1056 if (gamma_size == 0)
1057 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1058
1059 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1060 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
Rob Clark0e3704c2015-03-11 10:23:14 -04001061
1062 for (j = 0; j < mode_set->num_connectors; j++) {
1063 struct drm_connector *connector = mode_set->connectors[j];
1064 if (connector->has_tile) {
1065 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1066 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1067 /* cloning to multiple tiles is just crazy-talk, so: */
1068 break;
1069 }
1070 }
1071
1072 if (lasth)
1073 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1074 if (lastv)
1075 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
Dave Airlie785b93e2009-08-28 15:46:53 +10001076 }
1077
Dave Airlie38651672010-03-30 05:34:13 +00001078 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001079 /* hmm everyone went away - assume VGA cable just fell out
1080 and will come back later. */
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001081 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +00001082 sizes.fb_width = sizes.surface_width = 1024;
1083 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +10001084 }
1085
Dave Airlie38651672010-03-30 05:34:13 +00001086 /* push down into drivers */
Daniel Vetter8acf6582013-01-21 23:38:37 +01001087 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1088 if (ret < 0)
1089 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +10001090
Dave Airlie38651672010-03-30 05:34:13 +00001091 info = fb_helper->fbdev;
Dave Airlie785b93e2009-08-28 15:46:53 +10001092
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001093 /*
1094 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1095 * events, but at init time drm_setup_crtcs needs to be called before
1096 * the fb is allocated (since we need to figure out the desired size of
1097 * the fb before we can allocate it ...). Hence we need to fix things up
1098 * here again.
1099 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001100 for (i = 0; i < fb_helper->crtc_count; i++)
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001101 if (fb_helper->crtc_info[i].mode_set.num_connectors)
1102 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1103
Dave Airlie785b93e2009-08-28 15:46:53 +10001104
Daniel Vetter8acf6582013-01-21 23:38:37 +01001105 info->var.pixclock = 0;
1106 if (register_framebuffer(info) < 0)
1107 return -EINVAL;
Dave Airlie38651672010-03-30 05:34:13 +00001108
Daniel Vetter8acf6582013-01-21 23:38:37 +01001109 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
1110 info->node, info->fix.id);
Dave Airlie785b93e2009-08-28 15:46:53 +10001111
1112 /* Switch back to kernel console on panic */
1113 /* multi card linked list maybe */
1114 if (list_empty(&kernel_fb_helper_list)) {
Sachin Kamatd56b1b92012-11-15 03:43:29 +00001115 dev_info(fb_helper->dev->dev, "registered panic notifier\n");
Dave Airlie785b93e2009-08-28 15:46:53 +10001116 atomic_notifier_chain_register(&panic_notifier_list,
1117 &paniced);
1118 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
1119 }
Daniel Vetter8acf6582013-01-21 23:38:37 +01001120
1121 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
Dave Airlie38651672010-03-30 05:34:13 +00001122
Dave Airlie785b93e2009-08-28 15:46:53 +10001123 return 0;
1124}
Dave Airlie785b93e2009-08-28 15:46:53 +10001125
Daniel Vetter207fd322013-01-20 22:13:14 +01001126/**
1127 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1128 * @info: fbdev registered by the helper
1129 * @pitch: desired pitch
1130 * @depth: desired depth
1131 *
1132 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1133 * fbdev emulations. Drivers which support acceleration methods which impose
1134 * additional constraints need to set up their own limits.
1135 *
1136 * Drivers should call this (or their equivalent setup code) from their
1137 * ->fb_probe callback.
1138 */
Dave Airlie3632ef82011-01-15 09:27:00 +10001139void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1140 uint32_t depth)
1141{
1142 info->fix.type = FB_TYPE_PACKED_PIXELS;
1143 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1144 FB_VISUAL_TRUECOLOR;
1145 info->fix.mmio_start = 0;
1146 info->fix.mmio_len = 0;
1147 info->fix.type_aux = 0;
1148 info->fix.xpanstep = 1; /* doing it in hw */
1149 info->fix.ypanstep = 1; /* doing it in hw */
1150 info->fix.ywrapstep = 0;
1151 info->fix.accel = FB_ACCEL_NONE;
Dave Airlie3632ef82011-01-15 09:27:00 +10001152
1153 info->fix.line_length = pitch;
1154 return;
1155}
1156EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1157
Daniel Vetter207fd322013-01-20 22:13:14 +01001158/**
1159 * drm_fb_helper_fill_var - initalizes variable fbdev information
1160 * @info: fbdev instance to set up
1161 * @fb_helper: fb helper instance to use as template
1162 * @fb_width: desired fb width
1163 * @fb_height: desired fb height
1164 *
1165 * Sets up the variable fbdev metainformation from the given fb helper instance
1166 * and the drm framebuffer allocated in fb_helper->fb.
1167 *
1168 * Drivers should call this (or their equivalent setup code) from their
1169 * ->fb_probe callback after having allocated the fbdev backing
1170 * storage framebuffer.
1171 */
Dave Airlie38651672010-03-30 05:34:13 +00001172void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +10001173 uint32_t fb_width, uint32_t fb_height)
1174{
Dave Airlie38651672010-03-30 05:34:13 +00001175 struct drm_framebuffer *fb = fb_helper->fb;
1176 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +10001177 info->var.xres_virtual = fb->width;
1178 info->var.yres_virtual = fb->height;
1179 info->var.bits_per_pixel = fb->bits_per_pixel;
James Simmons57084d02010-12-20 19:10:39 +00001180 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +10001181 info->var.xoffset = 0;
1182 info->var.yoffset = 0;
1183 info->var.activate = FB_ACTIVATE_NOW;
1184 info->var.height = -1;
1185 info->var.width = -1;
1186
1187 switch (fb->depth) {
1188 case 8:
1189 info->var.red.offset = 0;
1190 info->var.green.offset = 0;
1191 info->var.blue.offset = 0;
1192 info->var.red.length = 8; /* 8bit DAC */
1193 info->var.green.length = 8;
1194 info->var.blue.length = 8;
1195 info->var.transp.offset = 0;
1196 info->var.transp.length = 0;
1197 break;
1198 case 15:
1199 info->var.red.offset = 10;
1200 info->var.green.offset = 5;
1201 info->var.blue.offset = 0;
1202 info->var.red.length = 5;
1203 info->var.green.length = 5;
1204 info->var.blue.length = 5;
1205 info->var.transp.offset = 15;
1206 info->var.transp.length = 1;
1207 break;
1208 case 16:
1209 info->var.red.offset = 11;
1210 info->var.green.offset = 5;
1211 info->var.blue.offset = 0;
1212 info->var.red.length = 5;
1213 info->var.green.length = 6;
1214 info->var.blue.length = 5;
1215 info->var.transp.offset = 0;
1216 break;
1217 case 24:
1218 info->var.red.offset = 16;
1219 info->var.green.offset = 8;
1220 info->var.blue.offset = 0;
1221 info->var.red.length = 8;
1222 info->var.green.length = 8;
1223 info->var.blue.length = 8;
1224 info->var.transp.offset = 0;
1225 info->var.transp.length = 0;
1226 break;
1227 case 32:
1228 info->var.red.offset = 16;
1229 info->var.green.offset = 8;
1230 info->var.blue.offset = 0;
1231 info->var.red.length = 8;
1232 info->var.green.length = 8;
1233 info->var.blue.length = 8;
1234 info->var.transp.offset = 24;
1235 info->var.transp.length = 8;
1236 break;
1237 default:
1238 break;
1239 }
1240
1241 info->var.xres = fb_width;
1242 info->var.yres = fb_height;
1243}
1244EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +00001245
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001246static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1247 uint32_t maxX,
1248 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +00001249{
1250 struct drm_connector *connector;
1251 int count = 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001252 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001253
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001254 for (i = 0; i < fb_helper->connector_count; i++) {
1255 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001256 count += connector->funcs->fill_modes(connector, maxX, maxY);
1257 }
1258
1259 return count;
1260}
1261
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001262struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
Dave Airlie38651672010-03-30 05:34:13 +00001263{
1264 struct drm_display_mode *mode;
1265
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001266 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Daniel Vetter9d3de132014-01-23 16:27:56 +01001267 if (mode->hdisplay > width ||
1268 mode->vdisplay > height)
Dave Airlie38651672010-03-30 05:34:13 +00001269 continue;
1270 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1271 return mode;
1272 }
1273 return NULL;
1274}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001275EXPORT_SYMBOL(drm_has_preferred_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001276
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001277static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00001278{
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001279 return fb_connector->connector->cmdline_mode.specified;
Dave Airlie38651672010-03-30 05:34:13 +00001280}
1281
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001282struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001283 int width, int height)
Dave Airlie38651672010-03-30 05:34:13 +00001284{
Chris Wilson1794d252011-04-17 07:43:32 +01001285 struct drm_cmdline_mode *cmdline_mode;
Daniel Stonef3af5c72015-03-19 04:33:01 +00001286 struct drm_display_mode *mode;
Takashi Iwaic683f422014-03-19 14:53:13 +01001287 bool prefer_non_interlace;
Dave Airlie38651672010-03-30 05:34:13 +00001288
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001289 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001290 if (cmdline_mode->specified == false)
Daniel Stonef3af5c72015-03-19 04:33:01 +00001291 return NULL;
Dave Airlie38651672010-03-30 05:34:13 +00001292
1293 /* attempt to find a matching mode in the list of modes
1294 * we have gotten so far, if not add a CVT mode that conforms
1295 */
1296 if (cmdline_mode->rb || cmdline_mode->margins)
1297 goto create_mode;
1298
Takashi Iwaic683f422014-03-19 14:53:13 +01001299 prefer_non_interlace = !cmdline_mode->interlace;
Daniel Stonef3af5c72015-03-19 04:33:01 +00001300again:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001301 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001302 /* check width/height */
1303 if (mode->hdisplay != cmdline_mode->xres ||
1304 mode->vdisplay != cmdline_mode->yres)
1305 continue;
1306
1307 if (cmdline_mode->refresh_specified) {
1308 if (mode->vrefresh != cmdline_mode->refresh)
1309 continue;
1310 }
1311
1312 if (cmdline_mode->interlace) {
1313 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1314 continue;
Takashi Iwaic683f422014-03-19 14:53:13 +01001315 } else if (prefer_non_interlace) {
1316 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1317 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001318 }
1319 return mode;
1320 }
1321
Takashi Iwaic683f422014-03-19 14:53:13 +01001322 if (prefer_non_interlace) {
1323 prefer_non_interlace = false;
1324 goto again;
1325 }
1326
Dave Airlie38651672010-03-30 05:34:13 +00001327create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +01001328 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1329 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001330 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00001331 return mode;
1332}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001333EXPORT_SYMBOL(drm_pick_cmdline_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001334
1335static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1336{
1337 bool enable;
1338
Sachin Kamat96081cd2012-11-15 03:43:30 +00001339 if (strict)
Dave Airlie38651672010-03-30 05:34:13 +00001340 enable = connector->status == connector_status_connected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001341 else
Dave Airlie38651672010-03-30 05:34:13 +00001342 enable = connector->status != connector_status_disconnected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001343
Dave Airlie38651672010-03-30 05:34:13 +00001344 return enable;
1345}
1346
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001347static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1348 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001349{
1350 bool any_enabled = false;
1351 struct drm_connector *connector;
1352 int i = 0;
1353
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001354 for (i = 0; i < fb_helper->connector_count; i++) {
1355 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001356 enabled[i] = drm_connector_enabled(connector, true);
1357 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1358 enabled[i] ? "yes" : "no");
1359 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001360 }
1361
1362 if (any_enabled)
1363 return;
1364
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001365 for (i = 0; i < fb_helper->connector_count; i++) {
1366 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001367 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001368 }
1369}
1370
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001371static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1372 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001373 struct drm_fb_offset *offsets,
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001374 bool *enabled, int width, int height)
1375{
1376 int count, i, j;
1377 bool can_clone = false;
1378 struct drm_fb_helper_connector *fb_helper_conn;
1379 struct drm_display_mode *dmt_mode, *mode;
1380
1381 /* only contemplate cloning in the single crtc case */
1382 if (fb_helper->crtc_count > 1)
1383 return false;
1384
1385 count = 0;
1386 for (i = 0; i < fb_helper->connector_count; i++) {
1387 if (enabled[i])
1388 count++;
1389 }
1390
1391 /* only contemplate cloning if more than one connector is enabled */
1392 if (count <= 1)
1393 return false;
1394
1395 /* check the command line or if nothing common pick 1024x768 */
1396 can_clone = true;
1397 for (i = 0; i < fb_helper->connector_count; i++) {
1398 if (!enabled[i])
1399 continue;
1400 fb_helper_conn = fb_helper->connector_info[i];
1401 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1402 if (!modes[i]) {
1403 can_clone = false;
1404 break;
1405 }
1406 for (j = 0; j < i; j++) {
1407 if (!enabled[j])
1408 continue;
1409 if (!drm_mode_equal(modes[j], modes[i]))
1410 can_clone = false;
1411 }
1412 }
1413
1414 if (can_clone) {
1415 DRM_DEBUG_KMS("can clone using command line\n");
1416 return true;
1417 }
1418
1419 /* try and find a 1024x768 mode on each connector */
1420 can_clone = true;
Adam Jacksonf6e252b2012-04-13 16:33:31 -04001421 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001422
1423 for (i = 0; i < fb_helper->connector_count; i++) {
1424
1425 if (!enabled[i])
1426 continue;
1427
1428 fb_helper_conn = fb_helper->connector_info[i];
1429 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1430 if (drm_mode_equal(mode, dmt_mode))
1431 modes[i] = mode;
1432 }
1433 if (!modes[i])
1434 can_clone = false;
1435 }
1436
1437 if (can_clone) {
1438 DRM_DEBUG_KMS("can clone using 1024x768\n");
1439 return true;
1440 }
1441 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1442 return false;
1443}
1444
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001445static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
1446 struct drm_display_mode **modes,
1447 struct drm_fb_offset *offsets,
1448 int idx,
1449 int h_idx, int v_idx)
1450{
1451 struct drm_fb_helper_connector *fb_helper_conn;
1452 int i;
1453 int hoffset = 0, voffset = 0;
1454
1455 for (i = 0; i < fb_helper->connector_count; i++) {
1456 fb_helper_conn = fb_helper->connector_info[i];
1457 if (!fb_helper_conn->connector->has_tile)
1458 continue;
1459
1460 if (!modes[i] && (h_idx || v_idx)) {
1461 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
1462 fb_helper_conn->connector->base.id);
1463 continue;
1464 }
1465 if (fb_helper_conn->connector->tile_h_loc < h_idx)
1466 hoffset += modes[i]->hdisplay;
1467
1468 if (fb_helper_conn->connector->tile_v_loc < v_idx)
1469 voffset += modes[i]->vdisplay;
1470 }
1471 offsets[idx].x = hoffset;
1472 offsets[idx].y = voffset;
1473 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
1474 return 0;
1475}
1476
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001477static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00001478 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001479 struct drm_fb_offset *offsets,
Dave Airlie38651672010-03-30 05:34:13 +00001480 bool *enabled, int width, int height)
1481{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001482 struct drm_fb_helper_connector *fb_helper_conn;
1483 int i;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001484 uint64_t conn_configured = 0, mask;
1485 int tile_pass = 0;
1486 mask = (1 << fb_helper->connector_count) - 1;
1487retry:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001488 for (i = 0; i < fb_helper->connector_count; i++) {
1489 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00001490
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001491 if (conn_configured & (1 << i))
Dave Airlie38651672010-03-30 05:34:13 +00001492 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001493
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001494 if (enabled[i] == false) {
1495 conn_configured |= (1 << i);
1496 continue;
1497 }
1498
1499 /* first pass over all the untiled connectors */
1500 if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
1501 continue;
1502
1503 if (tile_pass == 1) {
1504 if (fb_helper_conn->connector->tile_h_loc != 0 ||
1505 fb_helper_conn->connector->tile_v_loc != 0)
1506 continue;
1507
1508 } else {
1509 if (fb_helper_conn->connector->tile_h_loc != tile_pass -1 &&
1510 fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
1511 /* if this tile_pass doesn't cover any of the tiles - keep going */
1512 continue;
1513
1514 /* find the tile offsets for this pass - need
1515 to find all tiles left and above */
1516 drm_get_tile_offsets(fb_helper, modes, offsets,
1517 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
1518 }
Dave Airlie38651672010-03-30 05:34:13 +00001519 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001520 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00001521
1522 /* got for command line mode first */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001523 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001524 if (!modes[i]) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001525 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
1526 fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001527 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001528 }
1529 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001530 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1531 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00001532 break;
1533 }
1534 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1535 "none");
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001536 conn_configured |= (1 << i);
1537 }
1538
1539 if ((conn_configured & mask) != mask) {
1540 tile_pass++;
1541 goto retry;
Dave Airlie38651672010-03-30 05:34:13 +00001542 }
1543 return true;
1544}
1545
Dave Airlie8be48d92010-03-30 05:34:14 +00001546static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1547 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00001548 struct drm_display_mode **modes,
1549 int n, int width, int height)
1550{
1551 int c, o;
Dave Airlie8be48d92010-03-30 05:34:14 +00001552 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001553 struct drm_connector *connector;
Jani Nikulabe26a662015-03-11 11:51:06 +02001554 const struct drm_connector_helper_funcs *connector_funcs;
Dave Airlie38651672010-03-30 05:34:13 +00001555 struct drm_encoder *encoder;
Dave Airlie38651672010-03-30 05:34:13 +00001556 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00001557 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001558 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00001559
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001560 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00001561 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001562
1563 fb_helper_conn = fb_helper->connector_info[n];
1564 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001565
1566 best_crtcs[n] = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001567 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001568 if (modes[n] == NULL)
1569 return best_score;
1570
Dave Airlie8be48d92010-03-30 05:34:14 +00001571 crtcs = kzalloc(dev->mode_config.num_connector *
1572 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001573 if (!crtcs)
1574 return best_score;
1575
1576 my_score = 1;
1577 if (connector->status == connector_status_connected)
1578 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001579 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00001580 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001581 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00001582 my_score++;
1583
1584 connector_funcs = connector->helper_private;
1585 encoder = connector_funcs->best_encoder(connector);
1586 if (!encoder)
1587 goto out;
1588
Dave Airlie38651672010-03-30 05:34:13 +00001589 /* select a crtc for this connector and then attempt to configure
1590 remaining connectors */
Dave Airlie8be48d92010-03-30 05:34:14 +00001591 for (c = 0; c < fb_helper->crtc_count; c++) {
1592 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00001593
Sachin Kamat96081cd2012-11-15 03:43:30 +00001594 if ((encoder->possible_crtcs & (1 << c)) == 0)
Dave Airlie38651672010-03-30 05:34:13 +00001595 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001596
1597 for (o = 0; o < n; o++)
1598 if (best_crtcs[o] == crtc)
1599 break;
1600
1601 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001602 /* ignore cloning unless only a single crtc */
1603 if (fb_helper->crtc_count > 1)
1604 continue;
1605
1606 if (!drm_mode_equal(modes[o], modes[n]))
1607 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001608 }
1609
1610 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00001611 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1612 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00001613 width, height);
1614 if (score > best_score) {
Dave Airlie38651672010-03-30 05:34:13 +00001615 best_score = score;
1616 memcpy(best_crtcs, crtcs,
1617 dev->mode_config.num_connector *
Dave Airlie8be48d92010-03-30 05:34:14 +00001618 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00001619 }
Dave Airlie38651672010-03-30 05:34:13 +00001620 }
1621out:
1622 kfree(crtcs);
1623 return best_score;
1624}
1625
Dave Airlie8be48d92010-03-30 05:34:14 +00001626static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001627{
Dave Airlie8be48d92010-03-30 05:34:14 +00001628 struct drm_device *dev = fb_helper->dev;
1629 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00001630 struct drm_display_mode **modes;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001631 struct drm_fb_offset *offsets;
Dave Airlie8be48d92010-03-30 05:34:14 +00001632 struct drm_mode_set *modeset;
Dave Airlie38651672010-03-30 05:34:13 +00001633 bool *enabled;
1634 int width, height;
Jesse Barnes11e17a02013-02-19 13:31:39 -08001635 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001636
1637 DRM_DEBUG_KMS("\n");
1638
1639 width = dev->mode_config.max_width;
1640 height = dev->mode_config.max_height;
1641
Dave Airlie38651672010-03-30 05:34:13 +00001642 crtcs = kcalloc(dev->mode_config.num_connector,
Dave Airlie8be48d92010-03-30 05:34:14 +00001643 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001644 modes = kcalloc(dev->mode_config.num_connector,
1645 sizeof(struct drm_display_mode *), GFP_KERNEL);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001646 offsets = kcalloc(dev->mode_config.num_connector,
1647 sizeof(struct drm_fb_offset), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001648 enabled = kcalloc(dev->mode_config.num_connector,
1649 sizeof(bool), GFP_KERNEL);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001650 if (!crtcs || !modes || !enabled || !offsets) {
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00001651 DRM_ERROR("Memory allocation failed\n");
1652 goto out;
1653 }
1654
Dave Airlie38651672010-03-30 05:34:13 +00001655
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001656 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00001657
Jesse Barnes11e17a02013-02-19 13:31:39 -08001658 if (!(fb_helper->funcs->initial_config &&
1659 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001660 offsets,
Jesse Barnes11e17a02013-02-19 13:31:39 -08001661 enabled, width, height))) {
1662 memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0]));
1663 memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0]));
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001664 memset(offsets, 0, dev->mode_config.num_connector*sizeof(offsets[0]));
Jesse Barnes11e17a02013-02-19 13:31:39 -08001665
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001666 if (!drm_target_cloned(fb_helper, modes, offsets,
1667 enabled, width, height) &&
1668 !drm_target_preferred(fb_helper, modes, offsets,
1669 enabled, width, height))
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001670 DRM_ERROR("Unable to find initial modes\n");
Jesse Barnes11e17a02013-02-19 13:31:39 -08001671
1672 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
1673 width, height);
1674
1675 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001676 }
Dave Airlie38651672010-03-30 05:34:13 +00001677
Dave Airlie8be48d92010-03-30 05:34:14 +00001678 /* need to set the modesets up here for use later */
1679 /* fill out the connector<->crtc mappings into the modesets */
1680 for (i = 0; i < fb_helper->crtc_count; i++) {
1681 modeset = &fb_helper->crtc_info[i].mode_set;
1682 modeset->num_connectors = 0;
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001683 modeset->fb = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001684 }
Dave Airlie38651672010-03-30 05:34:13 +00001685
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001686 for (i = 0; i < fb_helper->connector_count; i++) {
Dave Airlie38651672010-03-30 05:34:13 +00001687 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001688 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001689 struct drm_fb_offset *offset = &offsets[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001690 modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00001691
Dave Airlie8be48d92010-03-30 05:34:14 +00001692 if (mode && fb_crtc) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001693 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
1694 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
Dave Airlie8be48d92010-03-30 05:34:14 +00001695 fb_crtc->desired_mode = mode;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001696 fb_crtc->x = offset->x;
1697 fb_crtc->y = offset->y;
Dave Airlie8be48d92010-03-30 05:34:14 +00001698 if (modeset->mode)
1699 drm_mode_destroy(dev, modeset->mode);
1700 modeset->mode = drm_mode_duplicate(dev,
1701 fb_crtc->desired_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001702 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001703 modeset->fb = fb_helper->fb;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001704 modeset->x = offset->x;
1705 modeset->y = offset->y;
Dave Airlie38651672010-03-30 05:34:13 +00001706 }
Dave Airlie38651672010-03-30 05:34:13 +00001707 }
1708
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001709 /* Clear out any old modes if there are no more connected outputs. */
1710 for (i = 0; i < fb_helper->crtc_count; i++) {
1711 modeset = &fb_helper->crtc_info[i].mode_set;
1712 if (modeset->num_connectors == 0) {
1713 BUG_ON(modeset->fb);
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001714 if (modeset->mode)
1715 drm_mode_destroy(dev, modeset->mode);
1716 modeset->mode = NULL;
1717 }
1718 }
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00001719out:
Dave Airlie38651672010-03-30 05:34:13 +00001720 kfree(crtcs);
1721 kfree(modes);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001722 kfree(offsets);
Dave Airlie38651672010-03-30 05:34:13 +00001723 kfree(enabled);
1724}
1725
1726/**
Daniel Vetter207fd322013-01-20 22:13:14 +01001727 * drm_fb_helper_initial_config - setup a sane initial connector configuration
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001728 * @fb_helper: fb_helper device struct
1729 * @bpp_sel: bpp value to use for the framebuffer configuration
Dave Airlie38651672010-03-30 05:34:13 +00001730 *
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001731 * Scans the CRTCs and connectors and tries to put together an initial setup.
Dave Airlie38651672010-03-30 05:34:13 +00001732 * At the moment, this is a cloned configuration across all heads with
1733 * a new framebuffer object as the backing store.
1734 *
Daniel Vetter207fd322013-01-20 22:13:14 +01001735 * Note that this also registers the fbdev and so allows userspace to call into
1736 * the driver through the fbdev interfaces.
1737 *
1738 * This function will call down into the ->fb_probe callback to let
1739 * the driver allocate and initialize the fbdev info structure and the drm
1740 * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
1741 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
1742 * values for the fbdev info structure.
1743 *
Dave Airlie38651672010-03-30 05:34:13 +00001744 * RETURNS:
1745 * Zero if everything went ok, nonzero otherwise.
1746 */
Thierry Reding01934c22014-12-19 11:21:32 +01001747int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00001748{
Dave Airlie8be48d92010-03-30 05:34:14 +00001749 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001750 int count = 0;
1751
Daniel Vetter53f19042014-03-20 14:26:35 +01001752 mutex_lock(&dev->mode_config.mutex);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001753 count = drm_fb_helper_probe_connector_modes(fb_helper,
1754 dev->mode_config.max_width,
1755 dev->mode_config.max_height);
Daniel Vetter53f19042014-03-20 14:26:35 +01001756 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie38651672010-03-30 05:34:13 +00001757 /*
1758 * we shouldn't end up with no modes here.
1759 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001760 if (count == 0)
Sachin Kamatd56b1b92012-11-15 03:43:29 +00001761 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
Sachin Kamat96081cd2012-11-15 03:43:30 +00001762
Dave Airlie8be48d92010-03-30 05:34:14 +00001763 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001764
Dave Airlie4abe3522010-03-30 05:34:18 +00001765 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001766}
Dave Airlie8be48d92010-03-30 05:34:14 +00001767EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00001768
Chris Wilson73943712011-04-22 11:03:57 +01001769/**
1770 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001771 * probing all the outputs attached to the fb
Chris Wilson73943712011-04-22 11:03:57 +01001772 * @fb_helper: the drm_fb_helper
1773 *
Chris Wilson73943712011-04-22 11:03:57 +01001774 * Scan the connectors attached to the fb_helper and try to put together a
1775 * setup after *notification of a change in output configuration.
1776 *
Daniel Vetter207fd322013-01-20 22:13:14 +01001777 * Called at runtime, takes the mode config locks to be able to check/change the
1778 * modeset configuration. Must be run from process context (which usually means
1779 * either the output polling work or a work item launched from the driver's
1780 * hotplug interrupt).
1781 *
Daniel Vetter50c3dc92014-06-27 17:19:22 +02001782 * Note that drivers may call this even before calling
1783 * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows
1784 * for a race-free fbcon setup and will make sure that the fbdev emulation will
1785 * not miss any hotplug events.
Daniel Vetter207fd322013-01-20 22:13:14 +01001786 *
Chris Wilson73943712011-04-22 11:03:57 +01001787 * RETURNS:
1788 * 0 on success and a non-zero error code otherwise.
1789 */
1790int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001791{
Chris Wilson73943712011-04-22 11:03:57 +01001792 struct drm_device *dev = fb_helper->dev;
Lespiau, Damien51bbd272013-09-28 16:24:05 +01001793 u32 max_width, max_height;
Dave Airlie4abe3522010-03-30 05:34:18 +00001794
Daniel Vetter89ced122013-04-11 14:26:55 +00001795 mutex_lock(&fb_helper->dev->mode_config.mutex);
Daniel Vetter50c3dc92014-06-27 17:19:22 +02001796 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001797 fb_helper->delayed_hotplug = true;
Daniel Vetter89ced122013-04-11 14:26:55 +00001798 mutex_unlock(&fb_helper->dev->mode_config.mutex);
Chris Wilson73943712011-04-22 11:03:57 +01001799 return 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001800 }
Dave Airlie38651672010-03-30 05:34:13 +00001801 DRM_DEBUG_KMS("\n");
1802
Dave Airlie4abe3522010-03-30 05:34:18 +00001803 max_width = fb_helper->fb->width;
1804 max_height = fb_helper->fb->height;
Dave Airlie4abe3522010-03-30 05:34:18 +00001805
Lespiau, Damien51bbd272013-09-28 16:24:05 +01001806 drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height);
Daniel Vetter89ced122013-04-11 14:26:55 +00001807 mutex_unlock(&fb_helper->dev->mode_config.mutex);
1808
1809 drm_modeset_lock_all(dev);
Dave Airlie8be48d92010-03-30 05:34:14 +00001810 drm_setup_crtcs(fb_helper);
Daniel Vetter84849902012-12-02 00:28:11 +01001811 drm_modeset_unlock_all(dev);
Daniel Vetter2180c3c2013-01-21 23:12:36 +01001812 drm_fb_helper_set_par(fb_helper->fbdev);
1813
1814 return 0;
Dave Airlie38651672010-03-30 05:34:13 +00001815}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001816EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00001817
David Rientjes6a108a12011-01-20 14:44:16 -08001818/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001819 * but the module doesn't depend on any fb console symbols. At least
1820 * attempt to load fbcon to avoid leaving the system without a usable console.
1821 */
David Rientjes6a108a12011-01-20 14:44:16 -08001822#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001823static int __init drm_fb_helper_modinit(void)
1824{
1825 const char *name = "fbcon";
1826 struct module *fbcon;
1827
1828 mutex_lock(&module_mutex);
1829 fbcon = find_module(name);
1830 mutex_unlock(&module_mutex);
1831
1832 if (!fbcon)
1833 request_module_nowait(name);
1834 return 0;
1835}
1836
1837module_init(drm_fb_helper_modinit);
1838#endif