blob: 3a6b6635e3f5fa2cca00c07fa49d36860e86399f [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) {
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}
146EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
147
148int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
149 struct drm_connector *connector)
150{
151 struct drm_fb_helper_connector *fb_helper_connector;
152 int i, j;
153
154 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
155
156 for (i = 0; i < fb_helper->connector_count; i++) {
157 if (fb_helper->connector_info[i]->connector == connector)
158 break;
159 }
160
161 if (i == fb_helper->connector_count)
162 return -EINVAL;
163 fb_helper_connector = fb_helper->connector_info[i];
164
165 for (j = i + 1; j < fb_helper->connector_count; j++) {
166 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
167 }
168 fb_helper->connector_count--;
169 kfree(fb_helper_connector);
170 return 0;
171}
172EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
173
Jason Wessel99231022010-10-13 14:09:43 -0500174static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
175{
176 uint16_t *r_base, *g_base, *b_base;
177 int i;
178
Ville Syrjälä04c0c562013-05-27 20:19:57 +0300179 if (helper->funcs->gamma_get == NULL)
180 return;
181
Jason Wessel99231022010-10-13 14:09:43 -0500182 r_base = crtc->gamma_store;
183 g_base = r_base + crtc->gamma_size;
184 b_base = g_base + crtc->gamma_size;
185
186 for (i = 0; i < crtc->gamma_size; i++)
187 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
188}
189
190static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
191{
192 uint16_t *r_base, *g_base, *b_base;
193
Laurent Pinchartebe0f242012-05-17 13:27:24 +0200194 if (crtc->funcs->gamma_set == NULL)
195 return;
196
Jason Wessel99231022010-10-13 14:09:43 -0500197 r_base = crtc->gamma_store;
198 g_base = r_base + crtc->gamma_size;
199 b_base = g_base + crtc->gamma_size;
200
201 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
202}
203
Daniel Vetter207fd322013-01-20 22:13:14 +0100204/**
205 * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter
206 * @info: fbdev registered by the helper
207 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500208int drm_fb_helper_debug_enter(struct fb_info *info)
209{
210 struct drm_fb_helper *helper = info->par;
211 struct drm_crtc_helper_funcs *funcs;
212 int i;
213
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500214 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
215 for (i = 0; i < helper->crtc_count; i++) {
216 struct drm_mode_set *mode_set =
217 &helper->crtc_info[i].mode_set;
218
219 if (!mode_set->crtc->enabled)
220 continue;
221
222 funcs = mode_set->crtc->helper_private;
Jason Wessel99231022010-10-13 14:09:43 -0500223 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500224 funcs->mode_set_base_atomic(mode_set->crtc,
225 mode_set->fb,
226 mode_set->x,
Jason Wessel413d45d2010-09-26 06:47:25 -0500227 mode_set->y,
Jason Wessel21c74a82010-10-13 14:09:44 -0500228 ENTER_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500229 }
230 }
231
232 return 0;
233}
234EXPORT_SYMBOL(drm_fb_helper_debug_enter);
235
236/* Find the real fb for a given fb helper CRTC */
237static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
238{
239 struct drm_device *dev = crtc->dev;
240 struct drm_crtc *c;
241
242 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
243 if (crtc->base.id == c->base.id)
Matt Roperf4510a22014-04-01 15:22:40 -0700244 return c->primary->fb;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500245 }
246
247 return NULL;
248}
249
Daniel Vetter207fd322013-01-20 22:13:14 +0100250/**
251 * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave
252 * @info: fbdev registered by the helper
253 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500254int drm_fb_helper_debug_leave(struct fb_info *info)
255{
256 struct drm_fb_helper *helper = info->par;
257 struct drm_crtc *crtc;
258 struct drm_crtc_helper_funcs *funcs;
259 struct drm_framebuffer *fb;
260 int i;
261
262 for (i = 0; i < helper->crtc_count; i++) {
263 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
264 crtc = mode_set->crtc;
265 funcs = crtc->helper_private;
266 fb = drm_mode_config_fb(crtc);
267
268 if (!crtc->enabled)
269 continue;
270
271 if (!fb) {
272 DRM_ERROR("no fb to restore??\n");
273 continue;
274 }
275
Jason Wessel99231022010-10-13 14:09:43 -0500276 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500277 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
Jason Wessel21c74a82010-10-13 14:09:44 -0500278 crtc->y, LEAVE_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500279 }
280
281 return 0;
282}
283EXPORT_SYMBOL(drm_fb_helper_debug_leave);
284
Rob Clark5ea1f752014-05-30 12:29:48 -0400285static bool restore_fbdev_mode(struct drm_fb_helper *fb_helper)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100286{
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300287 struct drm_device *dev = fb_helper->dev;
288 struct drm_plane *plane;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100289 bool error = false;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300290 int i;
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100291
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300292 drm_warn_on_modeset_not_all_locked(dev);
293
294 list_for_each_entry(plane, &dev->mode_config.plane_list, head)
Matt Ropere27dde32014-04-01 15:22:30 -0700295 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
296 drm_plane_force_disable(plane);
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100297
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100298 for (i = 0; i < fb_helper->crtc_count; i++) {
299 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300300 struct drm_crtc *crtc = mode_set->crtc;
301 int ret;
302
303 if (crtc->funcs->cursor_set) {
304 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
305 if (ret)
306 error = true;
307 }
308
Daniel Vetter2d13b672012-12-11 13:47:23 +0100309 ret = drm_mode_set_config_internal(mode_set);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100310 if (ret)
311 error = true;
312 }
313 return error;
314}
Rob Clark5ea1f752014-05-30 12:29:48 -0400315/**
316 * drm_fb_helper_restore_fbdev_mode - restore fbdev configuration
317 * @fb_helper: fbcon to restore
318 *
319 * This should be called from driver's drm ->lastclose callback
320 * when implementing an fbcon on top of kms using this helper. This ensures that
321 * the user isn't greeted with a black screen when e.g. X dies.
322 *
323 * Use this variant if you need to bypass locking (panic), or already
324 * hold all modeset locks. Otherwise use drm_fb_helper_restore_fbdev_mode_unlocked()
325 */
326static bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
327{
328 return restore_fbdev_mode(fb_helper);
329}
330
331/**
332 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
333 * @fb_helper: fbcon to restore
334 *
335 * This should be called from driver's drm ->lastclose callback
336 * when implementing an fbcon on top of kms using this helper. This ensures that
337 * the user isn't greeted with a black screen when e.g. X dies.
338 */
339bool drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
340{
341 struct drm_device *dev = fb_helper->dev;
342 bool ret;
343 drm_modeset_lock_all(dev);
344 ret = restore_fbdev_mode(fb_helper);
345 drm_modeset_unlock_all(dev);
346 return ret;
347}
348EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100349
Daniel Vetterd21bf462013-01-20 18:09:52 +0100350/*
351 * restore fbcon display for all kms driver's using this helper, used for sysrq
352 * and panic handling.
353 */
Sachin Kamat78b9c352012-08-01 17:15:32 +0530354static bool drm_fb_helper_force_kernel_mode(void)
Dave Airlie785b93e2009-08-28 15:46:53 +1000355{
Dave Airlie785b93e2009-08-28 15:46:53 +1000356 bool ret, error = false;
357 struct drm_fb_helper *helper;
358
359 if (list_empty(&kernel_fb_helper_list))
360 return false;
361
362 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
Thierry Redingb77f0762014-04-29 11:44:32 +0200363 struct drm_device *dev = helper->dev;
364
365 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100366 continue;
367
Rob Clark51fd3712013-11-19 12:10:12 -0500368 /* NOTE: we use lockless flag below to avoid grabbing other
369 * modeset locks. So just trylock the underlying mutex
370 * directly:
371 */
Thierry Redingb77f0762014-04-29 11:44:32 +0200372 if (!mutex_trylock(&dev->mode_config.mutex)) {
373 error = true;
374 continue;
375 }
376
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100377 ret = drm_fb_helper_restore_fbdev_mode(helper);
378 if (ret)
379 error = true;
Thierry Redingb77f0762014-04-29 11:44:32 +0200380
381 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000382 }
383 return error;
384}
385
Daniel Vetter43c8a842013-01-20 18:18:07 +0100386static int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
Dave Airlie785b93e2009-08-28 15:46:53 +1000387 void *panic_str)
388{
Hugh Dickinsd68752c2011-10-17 17:06:40 -0700389 /*
390 * It's a waste of time and effort to switch back to text console
391 * if the kernel should reboot before panic messages can be seen.
392 */
393 if (panic_timeout < 0)
394 return 0;
395
Sachin Kamatd56b1b92012-11-15 03:43:29 +0000396 pr_err("panic occurred, switching back to text console\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000397 return drm_fb_helper_force_kernel_mode();
Dave Airlie785b93e2009-08-28 15:46:53 +1000398}
Dave Airlie785b93e2009-08-28 15:46:53 +1000399
400static struct notifier_block paniced = {
401 .notifier_call = drm_fb_helper_panic,
402};
403
Daniel Vetter20c60c32012-12-17 12:13:23 +0100404static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
405{
406 struct drm_device *dev = fb_helper->dev;
407 struct drm_crtc *crtc;
408 int bound = 0, crtcs_bound = 0;
409
Paulo Zanoni520edd12013-11-27 18:24:08 -0200410 /* Sometimes user space wants everything disabled, so don't steal the
411 * display if there's a master. */
412 if (dev->primary->master)
413 return false;
414
Daniel Vetter20c60c32012-12-17 12:13:23 +0100415 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -0700416 if (crtc->primary->fb)
Daniel Vetter20c60c32012-12-17 12:13:23 +0100417 crtcs_bound++;
Matt Roperf4510a22014-04-01 15:22:40 -0700418 if (crtc->primary->fb == fb_helper->fb)
Daniel Vetter20c60c32012-12-17 12:13:23 +0100419 bound++;
420 }
421
422 if (bound < crtcs_bound)
423 return false;
Paulo Zanoni520edd12013-11-27 18:24:08 -0200424
Daniel Vetter20c60c32012-12-17 12:13:23 +0100425 return true;
426}
427
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200428#ifdef CONFIG_MAGIC_SYSRQ
Dave Airlie785b93e2009-08-28 15:46:53 +1000429static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
430{
Daniel Vetterd21bf462013-01-20 18:09:52 +0100431 bool ret;
432 ret = drm_fb_helper_force_kernel_mode();
433 if (ret == true)
434 DRM_ERROR("Failed to restore crtc configuration\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000435}
436static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
437
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700438static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000439{
440 schedule_work(&drm_fb_helper_restore_work);
441}
442
443static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
444 .handler = drm_fb_helper_sysrq,
445 .help_msg = "force-fb(V)",
446 .action_msg = "Restore framebuffer console",
447};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000448#else
449static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200450#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000451
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100452static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
Dave Airlie785b93e2009-08-28 15:46:53 +1000453{
454 struct drm_fb_helper *fb_helper = info->par;
455 struct drm_device *dev = fb_helper->dev;
456 struct drm_crtc *crtc;
Jesse Barnes023eb572010-07-02 10:48:08 -0700457 struct drm_connector *connector;
Jesse Barnes023eb572010-07-02 10:48:08 -0700458 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000459
460 /*
Daniel Vetter1b1d5392013-01-24 16:42:07 +0100461 * fbdev->blank can be called from irq context in case of a panic.
462 * Since we already have our own special panic handler which will
463 * restore the fbdev console mode completely, just bail out early.
464 */
465 if (oops_in_progress)
466 return;
467
468 /*
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100469 * For each CRTC in this fb, turn the connectors on/off.
Dave Airlie785b93e2009-08-28 15:46:53 +1000470 */
Daniel Vetter84849902012-12-02 00:28:11 +0100471 drm_modeset_lock_all(dev);
Daniel Vetter20c60c32012-12-17 12:13:23 +0100472 if (!drm_fb_helper_is_bound(fb_helper)) {
473 drm_modeset_unlock_all(dev);
474 return;
475 }
476
Jesse Barnese87b2c42009-09-17 18:14:41 -0700477 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000478 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000479
Dave Airlie8be48d92010-03-30 05:34:14 +0000480 if (!crtc->enabled)
481 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000482
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100483 /* Walk the connectors & encoders on this fb turning them on/off */
Jesse Barnes023eb572010-07-02 10:48:08 -0700484 for (j = 0; j < fb_helper->connector_count; j++) {
485 connector = fb_helper->connector_info[j]->connector;
Daniel Vettere04190e2012-09-07 10:14:52 +0200486 connector->funcs->dpms(connector, dpms_mode);
Rob Clark58495562012-10-11 20:50:56 -0500487 drm_object_property_set_value(&connector->base,
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100488 dev->mode_config.dpms_property, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700489 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000490 }
Daniel Vetter84849902012-12-02 00:28:11 +0100491 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000492}
493
Daniel Vetter207fd322013-01-20 22:13:14 +0100494/**
495 * drm_fb_helper_blank - implementation for ->fb_blank
496 * @blank: desired blanking state
497 * @info: fbdev registered by the helper
498 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000499int drm_fb_helper_blank(int blank, struct fb_info *info)
500{
501 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000502 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000503 case FB_BLANK_UNBLANK:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100504 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000505 break;
James Simmons731b5a12009-10-29 20:39:07 +0000506 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000507 case FB_BLANK_NORMAL:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100508 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000509 break;
James Simmons731b5a12009-10-29 20:39:07 +0000510 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000511 case FB_BLANK_HSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100512 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000513 break;
James Simmons731b5a12009-10-29 20:39:07 +0000514 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000515 case FB_BLANK_VSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100516 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
Dave Airlie785b93e2009-08-28 15:46:53 +1000517 break;
James Simmons731b5a12009-10-29 20:39:07 +0000518 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000519 case FB_BLANK_POWERDOWN:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100520 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000521 break;
522 }
523 return 0;
524}
525EXPORT_SYMBOL(drm_fb_helper_blank);
526
527static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
528{
529 int i;
530
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000531 for (i = 0; i < helper->connector_count; i++)
532 kfree(helper->connector_info[i]);
533 kfree(helper->connector_info);
Sascha Hauera1b77362012-02-01 11:38:22 +0100534 for (i = 0; i < helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000535 kfree(helper->crtc_info[i].mode_set.connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100536 if (helper->crtc_info[i].mode_set.mode)
537 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
538 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000539 kfree(helper->crtc_info);
540}
541
Daniel Vetter207fd322013-01-20 22:13:14 +0100542/**
Thierry Reding10a23102014-06-27 17:19:24 +0200543 * drm_fb_helper_prepare - setup a drm_fb_helper structure
544 * @dev: DRM device
545 * @helper: driver-allocated fbdev helper structure to set up
546 * @funcs: pointer to structure of functions associate with this helper
547 *
548 * Sets up the bare minimum to make the framebuffer helper usable. This is
549 * useful to implement race-free initialization of the polling helpers.
550 */
551void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
552 const struct drm_fb_helper_funcs *funcs)
553{
554 INIT_LIST_HEAD(&helper->kernel_fb_list);
555 helper->funcs = funcs;
556 helper->dev = dev;
557}
558EXPORT_SYMBOL(drm_fb_helper_prepare);
559
560/**
Daniel Vetter207fd322013-01-20 22:13:14 +0100561 * drm_fb_helper_init - initialize a drm_fb_helper structure
562 * @dev: drm device
563 * @fb_helper: driver-allocated fbdev helper structure to initialize
564 * @crtc_count: maximum number of crtcs to support in this fbdev emulation
565 * @max_conn_count: max connector count
566 *
567 * This allocates the structures for the fbdev helper with the given limits.
568 * Note that this won't yet touch the hardware (through the driver interfaces)
569 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
570 * to allow driver writes more control over the exact init sequence.
571 *
Thierry Reding10a23102014-06-27 17:19:24 +0200572 * Drivers must call drm_fb_helper_prepare() before calling this function.
Daniel Vetter207fd322013-01-20 22:13:14 +0100573 *
574 * RETURNS:
575 * Zero if everything went ok, nonzero otherwise.
576 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000577int drm_fb_helper_init(struct drm_device *dev,
578 struct drm_fb_helper *fb_helper,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000579 int crtc_count, int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000580{
Dave Airlie785b93e2009-08-28 15:46:53 +1000581 struct drm_crtc *crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000582 int i;
583
Xiubo Li04cfe972014-03-10 09:33:58 +0800584 if (!max_conn_count)
585 return -EINVAL;
586
Dave Airlie4abe3522010-03-30 05:34:18 +0000587 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
588 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000589 return -ENOMEM;
590
Dave Airlie4abe3522010-03-30 05:34:18 +0000591 fb_helper->crtc_count = crtc_count;
592 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
593 if (!fb_helper->connector_info) {
594 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000595 return -ENOMEM;
596 }
Dave Airlie65c2a892014-06-05 14:01:30 +1000597 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
Dave Airlie4abe3522010-03-30 05:34:18 +0000598 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000599
600 for (i = 0; i < crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000601 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000602 kcalloc(max_conn_count,
603 sizeof(struct drm_connector *),
604 GFP_KERNEL);
605
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200606 if (!fb_helper->crtc_info[i].mode_set.connectors)
Dave Airlie785b93e2009-08-28 15:46:53 +1000607 goto out_free;
Dave Airlie4abe3522010-03-30 05:34:18 +0000608 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000609 }
610
611 i = 0;
612 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000613 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000614 i++;
615 }
Sascha Hauere9ad3182012-02-01 11:38:25 +0100616
Dave Airlie785b93e2009-08-28 15:46:53 +1000617 return 0;
618out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000619 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000620 return -ENOMEM;
621}
Dave Airlie4abe3522010-03-30 05:34:18 +0000622EXPORT_SYMBOL(drm_fb_helper_init);
623
624void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
625{
626 if (!list_empty(&fb_helper->kernel_fb_list)) {
627 list_del(&fb_helper->kernel_fb_list);
628 if (list_empty(&kernel_fb_helper_list)) {
Sachin Kamatd56b1b92012-11-15 03:43:29 +0000629 pr_info("drm: unregistered panic notifier\n");
Dave Airlie4abe3522010-03-30 05:34:18 +0000630 atomic_notifier_chain_unregister(&panic_notifier_list,
631 &paniced);
632 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
633 }
634 }
635
636 drm_fb_helper_crtc_free(fb_helper);
637
Dave Airlie4abe3522010-03-30 05:34:18 +0000638}
639EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000640
Dave Airliec850cb72009-10-23 18:49:03 +1000641static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000642 u16 blue, u16 regno, struct fb_info *info)
643{
644 struct drm_fb_helper *fb_helper = info->par;
645 struct drm_framebuffer *fb = fb_helper->fb;
646 int pindex;
647
Dave Airliec850cb72009-10-23 18:49:03 +1000648 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
649 u32 *palette;
650 u32 value;
651 /* place color in psuedopalette */
652 if (regno > 16)
653 return -EINVAL;
654 palette = (u32 *)info->pseudo_palette;
655 red >>= (16 - info->var.red.length);
656 green >>= (16 - info->var.green.length);
657 blue >>= (16 - info->var.blue.length);
658 value = (red << info->var.red.offset) |
659 (green << info->var.green.offset) |
660 (blue << info->var.blue.offset);
Rob Clark9da12b6a2011-02-16 02:45:51 +0000661 if (info->var.transp.length > 0) {
662 u32 mask = (1 << info->var.transp.length) - 1;
663 mask <<= info->var.transp.offset;
664 value |= mask;
665 }
Dave Airliec850cb72009-10-23 18:49:03 +1000666 palette[regno] = value;
667 return 0;
668 }
669
Ville Syrjälä04c0c562013-05-27 20:19:57 +0300670 /*
671 * The driver really shouldn't advertise pseudo/directcolor
672 * visuals if it can't deal with the palette.
673 */
674 if (WARN_ON(!fb_helper->funcs->gamma_set ||
675 !fb_helper->funcs->gamma_get))
676 return -EINVAL;
677
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000678 pindex = regno;
679
680 if (fb->bits_per_pixel == 16) {
681 pindex = regno << 3;
682
683 if (fb->depth == 16 && regno > 63)
Dave Airliec850cb72009-10-23 18:49:03 +1000684 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000685 if (fb->depth == 15 && regno > 31)
Dave Airliec850cb72009-10-23 18:49:03 +1000686 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000687
688 if (fb->depth == 16) {
689 u16 r, g, b;
690 int i;
691 if (regno < 32) {
692 for (i = 0; i < 8; i++)
693 fb_helper->funcs->gamma_set(crtc, red,
694 green, blue, pindex + i);
695 }
696
697 fb_helper->funcs->gamma_get(crtc, &r,
698 &g, &b,
699 pindex >> 1);
700
701 for (i = 0; i < 4; i++)
702 fb_helper->funcs->gamma_set(crtc, r,
703 green, b,
704 (pindex >> 1) + i);
705 }
706 }
707
708 if (fb->depth != 16)
709 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
Dave Airliec850cb72009-10-23 18:49:03 +1000710 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000711}
712
Daniel Vetter207fd322013-01-20 22:13:14 +0100713/**
714 * drm_fb_helper_setcmap - implementation for ->fb_setcmap
715 * @cmap: cmap to set
716 * @info: fbdev registered by the helper
717 */
Dave Airlie068143d2009-10-05 09:58:02 +1000718int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
719{
720 struct drm_fb_helper *fb_helper = info->par;
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300721 struct drm_device *dev = fb_helper->dev;
Dave Airlie8be48d92010-03-30 05:34:14 +0000722 struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +1000723 u16 *red, *green, *blue, *transp;
724 struct drm_crtc *crtc;
roel062ac622011-03-07 18:00:34 +0100725 int i, j, rc = 0;
Dave Airlie068143d2009-10-05 09:58:02 +1000726 int start;
727
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300728 drm_modeset_lock_all(dev);
729 if (!drm_fb_helper_is_bound(fb_helper)) {
730 drm_modeset_unlock_all(dev);
731 return -EBUSY;
732 }
733
Dave Airlie8be48d92010-03-30 05:34:14 +0000734 for (i = 0; i < fb_helper->crtc_count; i++) {
735 crtc = fb_helper->crtc_info[i].mode_set.crtc;
736 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +1000737
738 red = cmap->red;
739 green = cmap->green;
740 blue = cmap->blue;
741 transp = cmap->transp;
742 start = cmap->start;
743
roel062ac622011-03-07 18:00:34 +0100744 for (j = 0; j < cmap->len; j++) {
Dave Airlie068143d2009-10-05 09:58:02 +1000745 u16 hred, hgreen, hblue, htransp = 0xffff;
746
747 hred = *red++;
748 hgreen = *green++;
749 hblue = *blue++;
750
751 if (transp)
752 htransp = *transp++;
753
Dave Airliec850cb72009-10-23 18:49:03 +1000754 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
755 if (rc)
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300756 goto out;
Dave Airlie068143d2009-10-05 09:58:02 +1000757 }
Ville Syrjälä04c0c562013-05-27 20:19:57 +0300758 if (crtc_funcs->load_lut)
759 crtc_funcs->load_lut(crtc);
Dave Airlie068143d2009-10-05 09:58:02 +1000760 }
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300761 out:
762 drm_modeset_unlock_all(dev);
Dave Airlie068143d2009-10-05 09:58:02 +1000763 return rc;
764}
765EXPORT_SYMBOL(drm_fb_helper_setcmap);
766
Daniel Vetter207fd322013-01-20 22:13:14 +0100767/**
768 * drm_fb_helper_check_var - implementation for ->fb_check_var
769 * @var: screeninfo to check
770 * @info: fbdev registered by the helper
771 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000772int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
773 struct fb_info *info)
774{
775 struct drm_fb_helper *fb_helper = info->par;
776 struct drm_framebuffer *fb = fb_helper->fb;
777 int depth;
778
Jason Wesself90ebd92010-08-05 09:22:32 -0500779 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +1000780 return -EINVAL;
781
782 /* Need to resize the fb object !!! */
Chris Wilson62fb3762012-03-26 21:15:53 +0100783 if (var->bits_per_pixel > fb->bits_per_pixel ||
784 var->xres > fb->width || var->yres > fb->height ||
785 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
Dave Airlie509c7d82010-01-08 09:27:08 +1000786 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
Chris Wilson62fb3762012-03-26 21:15:53 +0100787 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
788 var->xres, var->yres, var->bits_per_pixel,
789 var->xres_virtual, var->yres_virtual,
Dave Airlie509c7d82010-01-08 09:27:08 +1000790 fb->width, fb->height, fb->bits_per_pixel);
Dave Airlie785b93e2009-08-28 15:46:53 +1000791 return -EINVAL;
792 }
793
794 switch (var->bits_per_pixel) {
795 case 16:
796 depth = (var->green.length == 6) ? 16 : 15;
797 break;
798 case 32:
799 depth = (var->transp.length > 0) ? 32 : 24;
800 break;
801 default:
802 depth = var->bits_per_pixel;
803 break;
804 }
805
806 switch (depth) {
807 case 8:
808 var->red.offset = 0;
809 var->green.offset = 0;
810 var->blue.offset = 0;
811 var->red.length = 8;
812 var->green.length = 8;
813 var->blue.length = 8;
814 var->transp.length = 0;
815 var->transp.offset = 0;
816 break;
817 case 15:
818 var->red.offset = 10;
819 var->green.offset = 5;
820 var->blue.offset = 0;
821 var->red.length = 5;
822 var->green.length = 5;
823 var->blue.length = 5;
824 var->transp.length = 1;
825 var->transp.offset = 15;
826 break;
827 case 16:
828 var->red.offset = 11;
829 var->green.offset = 5;
830 var->blue.offset = 0;
831 var->red.length = 5;
832 var->green.length = 6;
833 var->blue.length = 5;
834 var->transp.length = 0;
835 var->transp.offset = 0;
836 break;
837 case 24:
838 var->red.offset = 16;
839 var->green.offset = 8;
840 var->blue.offset = 0;
841 var->red.length = 8;
842 var->green.length = 8;
843 var->blue.length = 8;
844 var->transp.length = 0;
845 var->transp.offset = 0;
846 break;
847 case 32:
848 var->red.offset = 16;
849 var->green.offset = 8;
850 var->blue.offset = 0;
851 var->red.length = 8;
852 var->green.length = 8;
853 var->blue.length = 8;
854 var->transp.length = 8;
855 var->transp.offset = 24;
856 break;
857 default:
858 return -EINVAL;
859 }
860 return 0;
861}
862EXPORT_SYMBOL(drm_fb_helper_check_var);
863
Daniel Vetter207fd322013-01-20 22:13:14 +0100864/**
865 * drm_fb_helper_set_par - implementation for ->fb_set_par
866 * @info: fbdev registered by the helper
867 *
868 * This will let fbcon do the mode init and is called at initialization time by
869 * the fbdev core when registering the driver, and later on through the hotplug
870 * callback.
871 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000872int drm_fb_helper_set_par(struct fb_info *info)
873{
874 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +1000875 struct fb_var_screeninfo *var = &info->var;
Dave Airlie785b93e2009-08-28 15:46:53 +1000876
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100877 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +1000878 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000879 return -EINVAL;
880 }
881
Rob Clark5ea1f752014-05-30 12:29:48 -0400882 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +0000883
884 if (fb_helper->delayed_hotplug) {
885 fb_helper->delayed_hotplug = false;
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000886 drm_fb_helper_hotplug_event(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +0000887 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000888 return 0;
889}
890EXPORT_SYMBOL(drm_fb_helper_set_par);
891
Daniel Vetter207fd322013-01-20 22:13:14 +0100892/**
893 * drm_fb_helper_pan_display - implementation for ->fb_pan_display
894 * @var: updated screen information
895 * @info: fbdev registered by the helper
896 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000897int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
898 struct fb_info *info)
899{
900 struct drm_fb_helper *fb_helper = info->par;
901 struct drm_device *dev = fb_helper->dev;
902 struct drm_mode_set *modeset;
Dave Airlie785b93e2009-08-28 15:46:53 +1000903 int ret = 0;
904 int i;
905
Daniel Vetter84849902012-12-02 00:28:11 +0100906 drm_modeset_lock_all(dev);
Daniel Vetter20c60c32012-12-17 12:13:23 +0100907 if (!drm_fb_helper_is_bound(fb_helper)) {
908 drm_modeset_unlock_all(dev);
909 return -EBUSY;
910 }
911
Dave Airlie8be48d92010-03-30 05:34:14 +0000912 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000913 modeset = &fb_helper->crtc_info[i].mode_set;
914
915 modeset->x = var->xoffset;
916 modeset->y = var->yoffset;
917
918 if (modeset->num_connectors) {
Daniel Vetter2d13b672012-12-11 13:47:23 +0100919 ret = drm_mode_set_config_internal(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +1000920 if (!ret) {
921 info->var.xoffset = var->xoffset;
922 info->var.yoffset = var->yoffset;
923 }
924 }
925 }
Daniel Vetter84849902012-12-02 00:28:11 +0100926 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000927 return ret;
928}
929EXPORT_SYMBOL(drm_fb_helper_pan_display);
930
Daniel Vetter8acf6582013-01-21 23:38:37 +0100931/*
Daniel Vetter207fd322013-01-20 22:13:14 +0100932 * Allocates the backing storage and sets up the fbdev info structure through
933 * the ->fb_probe callback and then registers the fbdev and sets up the panic
934 * notifier.
Daniel Vetter8acf6582013-01-21 23:38:37 +0100935 */
Daniel Vetterde1ace52013-01-20 21:50:49 +0100936static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
937 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +1000938{
Daniel Vetter8acf6582013-01-21 23:38:37 +0100939 int ret = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000940 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +0000941 int i;
Dave Airlie785b93e2009-08-28 15:46:53 +1000942 struct fb_info *info;
Dave Airlie38651672010-03-30 05:34:13 +0000943 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +0000944 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +0000945
946 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
947 sizes.surface_depth = 24;
948 sizes.surface_bpp = 32;
949 sizes.fb_width = (unsigned)-1;
950 sizes.fb_height = (unsigned)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +1000951
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000952 /* if driver picks 8 or 16 by default use that
953 for both depth/bpp */
Sachin Kamat96081cd2012-11-15 03:43:30 +0000954 if (preferred_bpp != sizes.surface_bpp)
Dave Airlie38651672010-03-30 05:34:13 +0000955 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Sachin Kamat96081cd2012-11-15 03:43:30 +0000956
Dave Airlie785b93e2009-08-28 15:46:53 +1000957 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000958 for (i = 0; i < fb_helper->connector_count; i++) {
959 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +0100960 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +1000961
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200962 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +1000963
964 if (cmdline_mode->bpp_specified) {
965 switch (cmdline_mode->bpp) {
966 case 8:
Dave Airlie38651672010-03-30 05:34:13 +0000967 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +1000968 break;
969 case 15:
Dave Airlie38651672010-03-30 05:34:13 +0000970 sizes.surface_depth = 15;
971 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000972 break;
973 case 16:
Dave Airlie38651672010-03-30 05:34:13 +0000974 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000975 break;
976 case 24:
Dave Airlie38651672010-03-30 05:34:13 +0000977 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +1000978 break;
979 case 32:
Dave Airlie38651672010-03-30 05:34:13 +0000980 sizes.surface_depth = 24;
981 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +1000982 break;
983 }
984 break;
985 }
986 }
987
Dave Airlie8be48d92010-03-30 05:34:14 +0000988 crtc_count = 0;
989 for (i = 0; i < fb_helper->crtc_count; i++) {
990 struct drm_display_mode *desired_mode;
991 desired_mode = fb_helper->crtc_info[i].desired_mode;
Dave Airlie785b93e2009-08-28 15:46:53 +1000992
Dave Airlie8be48d92010-03-30 05:34:14 +0000993 if (desired_mode) {
994 if (gamma_size == 0)
995 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
996 if (desired_mode->hdisplay < sizes.fb_width)
997 sizes.fb_width = desired_mode->hdisplay;
998 if (desired_mode->vdisplay < sizes.fb_height)
999 sizes.fb_height = desired_mode->vdisplay;
1000 if (desired_mode->hdisplay > sizes.surface_width)
1001 sizes.surface_width = desired_mode->hdisplay;
1002 if (desired_mode->vdisplay > sizes.surface_height)
1003 sizes.surface_height = desired_mode->vdisplay;
Dave Airlie785b93e2009-08-28 15:46:53 +10001004 crtc_count++;
1005 }
1006 }
1007
Dave Airlie38651672010-03-30 05:34:13 +00001008 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001009 /* hmm everyone went away - assume VGA cable just fell out
1010 and will come back later. */
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001011 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +00001012 sizes.fb_width = sizes.surface_width = 1024;
1013 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +10001014 }
1015
Dave Airlie38651672010-03-30 05:34:13 +00001016 /* push down into drivers */
Daniel Vetter8acf6582013-01-21 23:38:37 +01001017 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1018 if (ret < 0)
1019 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +10001020
Dave Airlie38651672010-03-30 05:34:13 +00001021 info = fb_helper->fbdev;
Dave Airlie785b93e2009-08-28 15:46:53 +10001022
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001023 /*
1024 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1025 * events, but at init time drm_setup_crtcs needs to be called before
1026 * the fb is allocated (since we need to figure out the desired size of
1027 * the fb before we can allocate it ...). Hence we need to fix things up
1028 * here again.
1029 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001030 for (i = 0; i < fb_helper->crtc_count; i++)
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001031 if (fb_helper->crtc_info[i].mode_set.num_connectors)
1032 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1033
Dave Airlie785b93e2009-08-28 15:46:53 +10001034
Daniel Vetter8acf6582013-01-21 23:38:37 +01001035 info->var.pixclock = 0;
1036 if (register_framebuffer(info) < 0)
1037 return -EINVAL;
Dave Airlie38651672010-03-30 05:34:13 +00001038
Daniel Vetter8acf6582013-01-21 23:38:37 +01001039 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
1040 info->node, info->fix.id);
Dave Airlie785b93e2009-08-28 15:46:53 +10001041
1042 /* Switch back to kernel console on panic */
1043 /* multi card linked list maybe */
1044 if (list_empty(&kernel_fb_helper_list)) {
Sachin Kamatd56b1b92012-11-15 03:43:29 +00001045 dev_info(fb_helper->dev->dev, "registered panic notifier\n");
Dave Airlie785b93e2009-08-28 15:46:53 +10001046 atomic_notifier_chain_register(&panic_notifier_list,
1047 &paniced);
1048 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
1049 }
Daniel Vetter8acf6582013-01-21 23:38:37 +01001050
1051 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
Dave Airlie38651672010-03-30 05:34:13 +00001052
Dave Airlie785b93e2009-08-28 15:46:53 +10001053 return 0;
1054}
Dave Airlie785b93e2009-08-28 15:46:53 +10001055
Daniel Vetter207fd322013-01-20 22:13:14 +01001056/**
1057 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1058 * @info: fbdev registered by the helper
1059 * @pitch: desired pitch
1060 * @depth: desired depth
1061 *
1062 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1063 * fbdev emulations. Drivers which support acceleration methods which impose
1064 * additional constraints need to set up their own limits.
1065 *
1066 * Drivers should call this (or their equivalent setup code) from their
1067 * ->fb_probe callback.
1068 */
Dave Airlie3632ef82011-01-15 09:27:00 +10001069void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1070 uint32_t depth)
1071{
1072 info->fix.type = FB_TYPE_PACKED_PIXELS;
1073 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1074 FB_VISUAL_TRUECOLOR;
1075 info->fix.mmio_start = 0;
1076 info->fix.mmio_len = 0;
1077 info->fix.type_aux = 0;
1078 info->fix.xpanstep = 1; /* doing it in hw */
1079 info->fix.ypanstep = 1; /* doing it in hw */
1080 info->fix.ywrapstep = 0;
1081 info->fix.accel = FB_ACCEL_NONE;
Dave Airlie3632ef82011-01-15 09:27:00 +10001082
1083 info->fix.line_length = pitch;
1084 return;
1085}
1086EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1087
Daniel Vetter207fd322013-01-20 22:13:14 +01001088/**
1089 * drm_fb_helper_fill_var - initalizes variable fbdev information
1090 * @info: fbdev instance to set up
1091 * @fb_helper: fb helper instance to use as template
1092 * @fb_width: desired fb width
1093 * @fb_height: desired fb height
1094 *
1095 * Sets up the variable fbdev metainformation from the given fb helper instance
1096 * and the drm framebuffer allocated in fb_helper->fb.
1097 *
1098 * Drivers should call this (or their equivalent setup code) from their
1099 * ->fb_probe callback after having allocated the fbdev backing
1100 * storage framebuffer.
1101 */
Dave Airlie38651672010-03-30 05:34:13 +00001102void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +10001103 uint32_t fb_width, uint32_t fb_height)
1104{
Dave Airlie38651672010-03-30 05:34:13 +00001105 struct drm_framebuffer *fb = fb_helper->fb;
1106 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +10001107 info->var.xres_virtual = fb->width;
1108 info->var.yres_virtual = fb->height;
1109 info->var.bits_per_pixel = fb->bits_per_pixel;
James Simmons57084d02010-12-20 19:10:39 +00001110 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +10001111 info->var.xoffset = 0;
1112 info->var.yoffset = 0;
1113 info->var.activate = FB_ACTIVATE_NOW;
1114 info->var.height = -1;
1115 info->var.width = -1;
1116
1117 switch (fb->depth) {
1118 case 8:
1119 info->var.red.offset = 0;
1120 info->var.green.offset = 0;
1121 info->var.blue.offset = 0;
1122 info->var.red.length = 8; /* 8bit DAC */
1123 info->var.green.length = 8;
1124 info->var.blue.length = 8;
1125 info->var.transp.offset = 0;
1126 info->var.transp.length = 0;
1127 break;
1128 case 15:
1129 info->var.red.offset = 10;
1130 info->var.green.offset = 5;
1131 info->var.blue.offset = 0;
1132 info->var.red.length = 5;
1133 info->var.green.length = 5;
1134 info->var.blue.length = 5;
1135 info->var.transp.offset = 15;
1136 info->var.transp.length = 1;
1137 break;
1138 case 16:
1139 info->var.red.offset = 11;
1140 info->var.green.offset = 5;
1141 info->var.blue.offset = 0;
1142 info->var.red.length = 5;
1143 info->var.green.length = 6;
1144 info->var.blue.length = 5;
1145 info->var.transp.offset = 0;
1146 break;
1147 case 24:
1148 info->var.red.offset = 16;
1149 info->var.green.offset = 8;
1150 info->var.blue.offset = 0;
1151 info->var.red.length = 8;
1152 info->var.green.length = 8;
1153 info->var.blue.length = 8;
1154 info->var.transp.offset = 0;
1155 info->var.transp.length = 0;
1156 break;
1157 case 32:
1158 info->var.red.offset = 16;
1159 info->var.green.offset = 8;
1160 info->var.blue.offset = 0;
1161 info->var.red.length = 8;
1162 info->var.green.length = 8;
1163 info->var.blue.length = 8;
1164 info->var.transp.offset = 24;
1165 info->var.transp.length = 8;
1166 break;
1167 default:
1168 break;
1169 }
1170
1171 info->var.xres = fb_width;
1172 info->var.yres = fb_height;
1173}
1174EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +00001175
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001176static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1177 uint32_t maxX,
1178 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +00001179{
1180 struct drm_connector *connector;
1181 int count = 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001182 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001183
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001184 for (i = 0; i < fb_helper->connector_count; i++) {
1185 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001186 count += connector->funcs->fill_modes(connector, maxX, maxY);
1187 }
1188
1189 return count;
1190}
1191
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001192struct 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 +00001193{
1194 struct drm_display_mode *mode;
1195
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001196 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Daniel Vetter9d3de132014-01-23 16:27:56 +01001197 if (mode->hdisplay > width ||
1198 mode->vdisplay > height)
Dave Airlie38651672010-03-30 05:34:13 +00001199 continue;
1200 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1201 return mode;
1202 }
1203 return NULL;
1204}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001205EXPORT_SYMBOL(drm_has_preferred_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001206
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001207static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00001208{
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001209 return fb_connector->connector->cmdline_mode.specified;
Dave Airlie38651672010-03-30 05:34:13 +00001210}
1211
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001212struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001213 int width, int height)
Dave Airlie38651672010-03-30 05:34:13 +00001214{
Chris Wilson1794d252011-04-17 07:43:32 +01001215 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001216 struct drm_display_mode *mode = NULL;
Takashi Iwaic683f422014-03-19 14:53:13 +01001217 bool prefer_non_interlace;
Dave Airlie38651672010-03-30 05:34:13 +00001218
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001219 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001220 if (cmdline_mode->specified == false)
1221 return mode;
1222
1223 /* attempt to find a matching mode in the list of modes
1224 * we have gotten so far, if not add a CVT mode that conforms
1225 */
1226 if (cmdline_mode->rb || cmdline_mode->margins)
1227 goto create_mode;
1228
Takashi Iwaic683f422014-03-19 14:53:13 +01001229 prefer_non_interlace = !cmdline_mode->interlace;
1230 again:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001231 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001232 /* check width/height */
1233 if (mode->hdisplay != cmdline_mode->xres ||
1234 mode->vdisplay != cmdline_mode->yres)
1235 continue;
1236
1237 if (cmdline_mode->refresh_specified) {
1238 if (mode->vrefresh != cmdline_mode->refresh)
1239 continue;
1240 }
1241
1242 if (cmdline_mode->interlace) {
1243 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1244 continue;
Takashi Iwaic683f422014-03-19 14:53:13 +01001245 } else if (prefer_non_interlace) {
1246 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1247 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001248 }
1249 return mode;
1250 }
1251
Takashi Iwaic683f422014-03-19 14:53:13 +01001252 if (prefer_non_interlace) {
1253 prefer_non_interlace = false;
1254 goto again;
1255 }
1256
Dave Airlie38651672010-03-30 05:34:13 +00001257create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +01001258 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1259 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001260 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00001261 return mode;
1262}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001263EXPORT_SYMBOL(drm_pick_cmdline_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001264
1265static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1266{
1267 bool enable;
1268
Sachin Kamat96081cd2012-11-15 03:43:30 +00001269 if (strict)
Dave Airlie38651672010-03-30 05:34:13 +00001270 enable = connector->status == connector_status_connected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001271 else
Dave Airlie38651672010-03-30 05:34:13 +00001272 enable = connector->status != connector_status_disconnected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001273
Dave Airlie38651672010-03-30 05:34:13 +00001274 return enable;
1275}
1276
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001277static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1278 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001279{
1280 bool any_enabled = false;
1281 struct drm_connector *connector;
1282 int i = 0;
1283
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001284 for (i = 0; i < fb_helper->connector_count; i++) {
1285 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001286 enabled[i] = drm_connector_enabled(connector, true);
1287 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1288 enabled[i] ? "yes" : "no");
1289 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001290 }
1291
1292 if (any_enabled)
1293 return;
1294
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001295 for (i = 0; i < fb_helper->connector_count; i++) {
1296 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001297 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001298 }
1299}
1300
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001301static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1302 struct drm_display_mode **modes,
1303 bool *enabled, int width, int height)
1304{
1305 int count, i, j;
1306 bool can_clone = false;
1307 struct drm_fb_helper_connector *fb_helper_conn;
1308 struct drm_display_mode *dmt_mode, *mode;
1309
1310 /* only contemplate cloning in the single crtc case */
1311 if (fb_helper->crtc_count > 1)
1312 return false;
1313
1314 count = 0;
1315 for (i = 0; i < fb_helper->connector_count; i++) {
1316 if (enabled[i])
1317 count++;
1318 }
1319
1320 /* only contemplate cloning if more than one connector is enabled */
1321 if (count <= 1)
1322 return false;
1323
1324 /* check the command line or if nothing common pick 1024x768 */
1325 can_clone = true;
1326 for (i = 0; i < fb_helper->connector_count; i++) {
1327 if (!enabled[i])
1328 continue;
1329 fb_helper_conn = fb_helper->connector_info[i];
1330 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1331 if (!modes[i]) {
1332 can_clone = false;
1333 break;
1334 }
1335 for (j = 0; j < i; j++) {
1336 if (!enabled[j])
1337 continue;
1338 if (!drm_mode_equal(modes[j], modes[i]))
1339 can_clone = false;
1340 }
1341 }
1342
1343 if (can_clone) {
1344 DRM_DEBUG_KMS("can clone using command line\n");
1345 return true;
1346 }
1347
1348 /* try and find a 1024x768 mode on each connector */
1349 can_clone = true;
Adam Jacksonf6e252b2012-04-13 16:33:31 -04001350 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001351
1352 for (i = 0; i < fb_helper->connector_count; i++) {
1353
1354 if (!enabled[i])
1355 continue;
1356
1357 fb_helper_conn = fb_helper->connector_info[i];
1358 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1359 if (drm_mode_equal(mode, dmt_mode))
1360 modes[i] = mode;
1361 }
1362 if (!modes[i])
1363 can_clone = false;
1364 }
1365
1366 if (can_clone) {
1367 DRM_DEBUG_KMS("can clone using 1024x768\n");
1368 return true;
1369 }
1370 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1371 return false;
1372}
1373
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001374static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00001375 struct drm_display_mode **modes,
1376 bool *enabled, int width, int height)
1377{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001378 struct drm_fb_helper_connector *fb_helper_conn;
1379 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001380
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001381 for (i = 0; i < fb_helper->connector_count; i++) {
1382 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00001383
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001384 if (enabled[i] == false)
Dave Airlie38651672010-03-30 05:34:13 +00001385 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001386
1387 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001388 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00001389
1390 /* got for command line mode first */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001391 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001392 if (!modes[i]) {
1393 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001394 fb_helper_conn->connector->base.id);
1395 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001396 }
1397 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001398 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1399 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00001400 break;
1401 }
1402 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1403 "none");
Dave Airlie38651672010-03-30 05:34:13 +00001404 }
1405 return true;
1406}
1407
Dave Airlie8be48d92010-03-30 05:34:14 +00001408static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1409 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00001410 struct drm_display_mode **modes,
1411 int n, int width, int height)
1412{
1413 int c, o;
Dave Airlie8be48d92010-03-30 05:34:14 +00001414 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001415 struct drm_connector *connector;
1416 struct drm_connector_helper_funcs *connector_funcs;
1417 struct drm_encoder *encoder;
Dave Airlie38651672010-03-30 05:34:13 +00001418 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00001419 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001420 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00001421
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001422 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00001423 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001424
1425 fb_helper_conn = fb_helper->connector_info[n];
1426 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001427
1428 best_crtcs[n] = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001429 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001430 if (modes[n] == NULL)
1431 return best_score;
1432
Dave Airlie8be48d92010-03-30 05:34:14 +00001433 crtcs = kzalloc(dev->mode_config.num_connector *
1434 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001435 if (!crtcs)
1436 return best_score;
1437
1438 my_score = 1;
1439 if (connector->status == connector_status_connected)
1440 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001441 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00001442 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001443 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00001444 my_score++;
1445
1446 connector_funcs = connector->helper_private;
1447 encoder = connector_funcs->best_encoder(connector);
1448 if (!encoder)
1449 goto out;
1450
Dave Airlie38651672010-03-30 05:34:13 +00001451 /* select a crtc for this connector and then attempt to configure
1452 remaining connectors */
Dave Airlie8be48d92010-03-30 05:34:14 +00001453 for (c = 0; c < fb_helper->crtc_count; c++) {
1454 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00001455
Sachin Kamat96081cd2012-11-15 03:43:30 +00001456 if ((encoder->possible_crtcs & (1 << c)) == 0)
Dave Airlie38651672010-03-30 05:34:13 +00001457 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001458
1459 for (o = 0; o < n; o++)
1460 if (best_crtcs[o] == crtc)
1461 break;
1462
1463 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001464 /* ignore cloning unless only a single crtc */
1465 if (fb_helper->crtc_count > 1)
1466 continue;
1467
1468 if (!drm_mode_equal(modes[o], modes[n]))
1469 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001470 }
1471
1472 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00001473 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1474 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00001475 width, height);
1476 if (score > best_score) {
Dave Airlie38651672010-03-30 05:34:13 +00001477 best_score = score;
1478 memcpy(best_crtcs, crtcs,
1479 dev->mode_config.num_connector *
Dave Airlie8be48d92010-03-30 05:34:14 +00001480 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00001481 }
Dave Airlie38651672010-03-30 05:34:13 +00001482 }
1483out:
1484 kfree(crtcs);
1485 return best_score;
1486}
1487
Dave Airlie8be48d92010-03-30 05:34:14 +00001488static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001489{
Dave Airlie8be48d92010-03-30 05:34:14 +00001490 struct drm_device *dev = fb_helper->dev;
1491 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00001492 struct drm_display_mode **modes;
Dave Airlie8be48d92010-03-30 05:34:14 +00001493 struct drm_mode_set *modeset;
Dave Airlie38651672010-03-30 05:34:13 +00001494 bool *enabled;
1495 int width, height;
Jesse Barnes11e17a02013-02-19 13:31:39 -08001496 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001497
1498 DRM_DEBUG_KMS("\n");
1499
1500 width = dev->mode_config.max_width;
1501 height = dev->mode_config.max_height;
1502
Dave Airlie38651672010-03-30 05:34:13 +00001503 crtcs = kcalloc(dev->mode_config.num_connector,
Dave Airlie8be48d92010-03-30 05:34:14 +00001504 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001505 modes = kcalloc(dev->mode_config.num_connector,
1506 sizeof(struct drm_display_mode *), GFP_KERNEL);
1507 enabled = kcalloc(dev->mode_config.num_connector,
1508 sizeof(bool), GFP_KERNEL);
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00001509 if (!crtcs || !modes || !enabled) {
1510 DRM_ERROR("Memory allocation failed\n");
1511 goto out;
1512 }
1513
Dave Airlie38651672010-03-30 05:34:13 +00001514
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001515 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00001516
Jesse Barnes11e17a02013-02-19 13:31:39 -08001517 if (!(fb_helper->funcs->initial_config &&
1518 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
1519 enabled, width, height))) {
1520 memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0]));
1521 memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0]));
1522
1523 if (!drm_target_cloned(fb_helper,
1524 modes, enabled, width, height) &&
1525 !drm_target_preferred(fb_helper,
1526 modes, enabled, width, height))
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001527 DRM_ERROR("Unable to find initial modes\n");
Jesse Barnes11e17a02013-02-19 13:31:39 -08001528
1529 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
1530 width, height);
1531
1532 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001533 }
Dave Airlie38651672010-03-30 05:34:13 +00001534
Dave Airlie8be48d92010-03-30 05:34:14 +00001535 /* need to set the modesets up here for use later */
1536 /* fill out the connector<->crtc mappings into the modesets */
1537 for (i = 0; i < fb_helper->crtc_count; i++) {
1538 modeset = &fb_helper->crtc_info[i].mode_set;
1539 modeset->num_connectors = 0;
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001540 modeset->fb = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001541 }
Dave Airlie38651672010-03-30 05:34:13 +00001542
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001543 for (i = 0; i < fb_helper->connector_count; i++) {
Dave Airlie38651672010-03-30 05:34:13 +00001544 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001545 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1546 modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00001547
Dave Airlie8be48d92010-03-30 05:34:14 +00001548 if (mode && fb_crtc) {
Dave Airlie38651672010-03-30 05:34:13 +00001549 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
Dave Airlie8be48d92010-03-30 05:34:14 +00001550 mode->name, fb_crtc->mode_set.crtc->base.id);
1551 fb_crtc->desired_mode = mode;
1552 if (modeset->mode)
1553 drm_mode_destroy(dev, modeset->mode);
1554 modeset->mode = drm_mode_duplicate(dev,
1555 fb_crtc->desired_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001556 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001557 modeset->fb = fb_helper->fb;
Dave Airlie38651672010-03-30 05:34:13 +00001558 }
Dave Airlie38651672010-03-30 05:34:13 +00001559 }
1560
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001561 /* Clear out any old modes if there are no more connected outputs. */
1562 for (i = 0; i < fb_helper->crtc_count; i++) {
1563 modeset = &fb_helper->crtc_info[i].mode_set;
1564 if (modeset->num_connectors == 0) {
1565 BUG_ON(modeset->fb);
1566 BUG_ON(modeset->num_connectors);
1567 if (modeset->mode)
1568 drm_mode_destroy(dev, modeset->mode);
1569 modeset->mode = NULL;
1570 }
1571 }
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00001572out:
Dave Airlie38651672010-03-30 05:34:13 +00001573 kfree(crtcs);
1574 kfree(modes);
1575 kfree(enabled);
1576}
1577
1578/**
Daniel Vetter207fd322013-01-20 22:13:14 +01001579 * drm_fb_helper_initial_config - setup a sane initial connector configuration
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001580 * @fb_helper: fb_helper device struct
1581 * @bpp_sel: bpp value to use for the framebuffer configuration
Dave Airlie38651672010-03-30 05:34:13 +00001582 *
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001583 * Scans the CRTCs and connectors and tries to put together an initial setup.
Dave Airlie38651672010-03-30 05:34:13 +00001584 * At the moment, this is a cloned configuration across all heads with
1585 * a new framebuffer object as the backing store.
1586 *
Daniel Vetter207fd322013-01-20 22:13:14 +01001587 * Note that this also registers the fbdev and so allows userspace to call into
1588 * the driver through the fbdev interfaces.
1589 *
1590 * This function will call down into the ->fb_probe callback to let
1591 * the driver allocate and initialize the fbdev info structure and the drm
1592 * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
1593 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
1594 * values for the fbdev info structure.
1595 *
Dave Airlie38651672010-03-30 05:34:13 +00001596 * RETURNS:
1597 * Zero if everything went ok, nonzero otherwise.
1598 */
Dave Airlie4abe3522010-03-30 05:34:18 +00001599bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00001600{
Dave Airlie8be48d92010-03-30 05:34:14 +00001601 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001602 int count = 0;
1603
Daniel Vetter53f19042014-03-20 14:26:35 +01001604 mutex_lock(&dev->mode_config.mutex);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001605 count = drm_fb_helper_probe_connector_modes(fb_helper,
1606 dev->mode_config.max_width,
1607 dev->mode_config.max_height);
Daniel Vetter53f19042014-03-20 14:26:35 +01001608 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie38651672010-03-30 05:34:13 +00001609 /*
1610 * we shouldn't end up with no modes here.
1611 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001612 if (count == 0)
Sachin Kamatd56b1b92012-11-15 03:43:29 +00001613 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
Sachin Kamat96081cd2012-11-15 03:43:30 +00001614
Dave Airlie8be48d92010-03-30 05:34:14 +00001615 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001616
Dave Airlie4abe3522010-03-30 05:34:18 +00001617 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001618}
Dave Airlie8be48d92010-03-30 05:34:14 +00001619EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00001620
Chris Wilson73943712011-04-22 11:03:57 +01001621/**
1622 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001623 * probing all the outputs attached to the fb
Chris Wilson73943712011-04-22 11:03:57 +01001624 * @fb_helper: the drm_fb_helper
1625 *
Chris Wilson73943712011-04-22 11:03:57 +01001626 * Scan the connectors attached to the fb_helper and try to put together a
1627 * setup after *notification of a change in output configuration.
1628 *
Daniel Vetter207fd322013-01-20 22:13:14 +01001629 * Called at runtime, takes the mode config locks to be able to check/change the
1630 * modeset configuration. Must be run from process context (which usually means
1631 * either the output polling work or a work item launched from the driver's
1632 * hotplug interrupt).
1633 *
Daniel Vetter50c3dc92014-06-27 17:19:22 +02001634 * Note that drivers may call this even before calling
1635 * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows
1636 * for a race-free fbcon setup and will make sure that the fbdev emulation will
1637 * not miss any hotplug events.
Daniel Vetter207fd322013-01-20 22:13:14 +01001638 *
Chris Wilson73943712011-04-22 11:03:57 +01001639 * RETURNS:
1640 * 0 on success and a non-zero error code otherwise.
1641 */
1642int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001643{
Chris Wilson73943712011-04-22 11:03:57 +01001644 struct drm_device *dev = fb_helper->dev;
Lespiau, Damien51bbd272013-09-28 16:24:05 +01001645 u32 max_width, max_height;
Dave Airlie4abe3522010-03-30 05:34:18 +00001646
Daniel Vetter89ced122013-04-11 14:26:55 +00001647 mutex_lock(&fb_helper->dev->mode_config.mutex);
Daniel Vetter50c3dc92014-06-27 17:19:22 +02001648 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001649 fb_helper->delayed_hotplug = true;
Daniel Vetter89ced122013-04-11 14:26:55 +00001650 mutex_unlock(&fb_helper->dev->mode_config.mutex);
Chris Wilson73943712011-04-22 11:03:57 +01001651 return 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001652 }
Dave Airlie38651672010-03-30 05:34:13 +00001653 DRM_DEBUG_KMS("\n");
1654
Dave Airlie4abe3522010-03-30 05:34:18 +00001655 max_width = fb_helper->fb->width;
1656 max_height = fb_helper->fb->height;
Dave Airlie4abe3522010-03-30 05:34:18 +00001657
Lespiau, Damien51bbd272013-09-28 16:24:05 +01001658 drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height);
Daniel Vetter89ced122013-04-11 14:26:55 +00001659 mutex_unlock(&fb_helper->dev->mode_config.mutex);
1660
1661 drm_modeset_lock_all(dev);
Dave Airlie8be48d92010-03-30 05:34:14 +00001662 drm_setup_crtcs(fb_helper);
Daniel Vetter84849902012-12-02 00:28:11 +01001663 drm_modeset_unlock_all(dev);
Daniel Vetter2180c3c2013-01-21 23:12:36 +01001664 drm_fb_helper_set_par(fb_helper->fbdev);
1665
1666 return 0;
Dave Airlie38651672010-03-30 05:34:13 +00001667}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001668EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00001669
David Rientjes6a108a12011-01-20 14:44:16 -08001670/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001671 * but the module doesn't depend on any fb console symbols. At least
1672 * attempt to load fbcon to avoid leaving the system without a usable console.
1673 */
David Rientjes6a108a12011-01-20 14:44:16 -08001674#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001675static int __init drm_fb_helper_modinit(void)
1676{
1677 const char *name = "fbcon";
1678 struct module *fbcon;
1679
1680 mutex_lock(&module_mutex);
1681 fbcon = find_module(name);
1682 mutex_unlock(&module_mutex);
1683
1684 if (!fbcon)
1685 request_module_nowait(name);
1686 return 0;
1687}
1688
1689module_init(drm_fb_helper_modinit);
1690#endif