blob: 0c0c39bac23da21a368ef94681da10b173994660 [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
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
Sonika Jindal9783de22014-08-05 11:26:57 +0530294 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
Sonika Jindal9783de22014-08-05 11:26:57 +0530298 if (dev->mode_config.rotation_property) {
Thomas Wood3a5f87c2014-08-20 14:45:00 +0100299 drm_mode_plane_set_obj_prop(plane,
300 dev->mode_config.rotation_property,
301 BIT(DRM_ROTATE_0));
Sonika Jindal9783de22014-08-05 11:26:57 +0530302 }
303 }
304
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100305 for (i = 0; i < fb_helper->crtc_count; i++) {
306 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300307 struct drm_crtc *crtc = mode_set->crtc;
308 int ret;
309
310 if (crtc->funcs->cursor_set) {
311 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
312 if (ret)
313 error = true;
314 }
315
Daniel Vetter2d13b672012-12-11 13:47:23 +0100316 ret = drm_mode_set_config_internal(mode_set);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100317 if (ret)
318 error = true;
319 }
320 return error;
321}
Rob Clark5ea1f752014-05-30 12:29:48 -0400322/**
323 * drm_fb_helper_restore_fbdev_mode - restore fbdev configuration
324 * @fb_helper: fbcon to restore
325 *
326 * This should be called from driver's drm ->lastclose callback
327 * when implementing an fbcon on top of kms using this helper. This ensures that
328 * the user isn't greeted with a black screen when e.g. X dies.
329 *
330 * Use this variant if you need to bypass locking (panic), or already
331 * hold all modeset locks. Otherwise use drm_fb_helper_restore_fbdev_mode_unlocked()
332 */
333static bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
334{
335 return restore_fbdev_mode(fb_helper);
336}
337
338/**
339 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
340 * @fb_helper: fbcon to restore
341 *
342 * This should be called from driver's drm ->lastclose callback
343 * when implementing an fbcon on top of kms using this helper. This ensures that
344 * the user isn't greeted with a black screen when e.g. X dies.
345 */
346bool drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
347{
348 struct drm_device *dev = fb_helper->dev;
349 bool ret;
350 drm_modeset_lock_all(dev);
351 ret = restore_fbdev_mode(fb_helper);
352 drm_modeset_unlock_all(dev);
353 return ret;
354}
355EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100356
Daniel Vetterd21bf462013-01-20 18:09:52 +0100357/*
358 * restore fbcon display for all kms driver's using this helper, used for sysrq
359 * and panic handling.
360 */
Sachin Kamat78b9c352012-08-01 17:15:32 +0530361static bool drm_fb_helper_force_kernel_mode(void)
Dave Airlie785b93e2009-08-28 15:46:53 +1000362{
Dave Airlie785b93e2009-08-28 15:46:53 +1000363 bool ret, error = false;
364 struct drm_fb_helper *helper;
365
366 if (list_empty(&kernel_fb_helper_list))
367 return false;
368
369 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
Thierry Redingb77f0762014-04-29 11:44:32 +0200370 struct drm_device *dev = helper->dev;
371
372 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100373 continue;
374
Daniel Vettercb597bb2014-07-27 19:09:33 +0200375 /*
376 * NOTE: Use trylock mode to avoid deadlocks and sleeping in
377 * panic context.
Rob Clark51fd3712013-11-19 12:10:12 -0500378 */
Daniel Vettercb597bb2014-07-27 19:09:33 +0200379 if (__drm_modeset_lock_all(dev, true) != 0) {
Thierry Redingb77f0762014-04-29 11:44:32 +0200380 error = true;
381 continue;
382 }
383
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100384 ret = drm_fb_helper_restore_fbdev_mode(helper);
385 if (ret)
386 error = true;
Thierry Redingb77f0762014-04-29 11:44:32 +0200387
Daniel Vettercb597bb2014-07-27 19:09:33 +0200388 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000389 }
390 return error;
391}
392
Daniel Vetter43c8a842013-01-20 18:18:07 +0100393static int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
Dave Airlie785b93e2009-08-28 15:46:53 +1000394 void *panic_str)
395{
Hugh Dickinsd68752c2011-10-17 17:06:40 -0700396 /*
397 * It's a waste of time and effort to switch back to text console
398 * if the kernel should reboot before panic messages can be seen.
399 */
400 if (panic_timeout < 0)
401 return 0;
402
Sachin Kamatd56b1b92012-11-15 03:43:29 +0000403 pr_err("panic occurred, switching back to text console\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000404 return drm_fb_helper_force_kernel_mode();
Dave Airlie785b93e2009-08-28 15:46:53 +1000405}
Dave Airlie785b93e2009-08-28 15:46:53 +1000406
407static struct notifier_block paniced = {
408 .notifier_call = drm_fb_helper_panic,
409};
410
Daniel Vetter20c60c32012-12-17 12:13:23 +0100411static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
412{
413 struct drm_device *dev = fb_helper->dev;
414 struct drm_crtc *crtc;
415 int bound = 0, crtcs_bound = 0;
416
Paulo Zanoni520edd12013-11-27 18:24:08 -0200417 /* Sometimes user space wants everything disabled, so don't steal the
418 * display if there's a master. */
419 if (dev->primary->master)
420 return false;
421
Daniel Vetter20c60c32012-12-17 12:13:23 +0100422 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -0700423 if (crtc->primary->fb)
Daniel Vetter20c60c32012-12-17 12:13:23 +0100424 crtcs_bound++;
Matt Roperf4510a22014-04-01 15:22:40 -0700425 if (crtc->primary->fb == fb_helper->fb)
Daniel Vetter20c60c32012-12-17 12:13:23 +0100426 bound++;
427 }
428
429 if (bound < crtcs_bound)
430 return false;
Paulo Zanoni520edd12013-11-27 18:24:08 -0200431
Daniel Vetter20c60c32012-12-17 12:13:23 +0100432 return true;
433}
434
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200435#ifdef CONFIG_MAGIC_SYSRQ
Dave Airlie785b93e2009-08-28 15:46:53 +1000436static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
437{
Daniel Vetterd21bf462013-01-20 18:09:52 +0100438 bool ret;
439 ret = drm_fb_helper_force_kernel_mode();
440 if (ret == true)
441 DRM_ERROR("Failed to restore crtc configuration\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000442}
443static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
444
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700445static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000446{
447 schedule_work(&drm_fb_helper_restore_work);
448}
449
450static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
451 .handler = drm_fb_helper_sysrq,
452 .help_msg = "force-fb(V)",
453 .action_msg = "Restore framebuffer console",
454};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000455#else
456static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200457#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000458
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100459static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
Dave Airlie785b93e2009-08-28 15:46:53 +1000460{
461 struct drm_fb_helper *fb_helper = info->par;
462 struct drm_device *dev = fb_helper->dev;
463 struct drm_crtc *crtc;
Jesse Barnes023eb572010-07-02 10:48:08 -0700464 struct drm_connector *connector;
Jesse Barnes023eb572010-07-02 10:48:08 -0700465 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000466
467 /*
Daniel Vetter1b1d5392013-01-24 16:42:07 +0100468 * fbdev->blank can be called from irq context in case of a panic.
469 * Since we already have our own special panic handler which will
470 * restore the fbdev console mode completely, just bail out early.
471 */
472 if (oops_in_progress)
473 return;
474
475 /*
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100476 * For each CRTC in this fb, turn the connectors on/off.
Dave Airlie785b93e2009-08-28 15:46:53 +1000477 */
Daniel Vetter84849902012-12-02 00:28:11 +0100478 drm_modeset_lock_all(dev);
Daniel Vetter20c60c32012-12-17 12:13:23 +0100479 if (!drm_fb_helper_is_bound(fb_helper)) {
480 drm_modeset_unlock_all(dev);
481 return;
482 }
483
Jesse Barnese87b2c42009-09-17 18:14:41 -0700484 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000485 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000486
Dave Airlie8be48d92010-03-30 05:34:14 +0000487 if (!crtc->enabled)
488 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000489
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100490 /* Walk the connectors & encoders on this fb turning them on/off */
Jesse Barnes023eb572010-07-02 10:48:08 -0700491 for (j = 0; j < fb_helper->connector_count; j++) {
492 connector = fb_helper->connector_info[j]->connector;
Daniel Vettere04190e2012-09-07 10:14:52 +0200493 connector->funcs->dpms(connector, dpms_mode);
Rob Clark58495562012-10-11 20:50:56 -0500494 drm_object_property_set_value(&connector->base,
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100495 dev->mode_config.dpms_property, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700496 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000497 }
Daniel Vetter84849902012-12-02 00:28:11 +0100498 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000499}
500
Daniel Vetter207fd322013-01-20 22:13:14 +0100501/**
502 * drm_fb_helper_blank - implementation for ->fb_blank
503 * @blank: desired blanking state
504 * @info: fbdev registered by the helper
505 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000506int drm_fb_helper_blank(int blank, struct fb_info *info)
507{
508 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000509 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000510 case FB_BLANK_UNBLANK:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100511 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000512 break;
James Simmons731b5a12009-10-29 20:39:07 +0000513 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000514 case FB_BLANK_NORMAL:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100515 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000516 break;
James Simmons731b5a12009-10-29 20:39:07 +0000517 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000518 case FB_BLANK_HSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100519 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000520 break;
James Simmons731b5a12009-10-29 20:39:07 +0000521 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000522 case FB_BLANK_VSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100523 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
Dave Airlie785b93e2009-08-28 15:46:53 +1000524 break;
James Simmons731b5a12009-10-29 20:39:07 +0000525 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000526 case FB_BLANK_POWERDOWN:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100527 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000528 break;
529 }
530 return 0;
531}
532EXPORT_SYMBOL(drm_fb_helper_blank);
533
534static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
535{
536 int i;
537
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000538 for (i = 0; i < helper->connector_count; i++)
539 kfree(helper->connector_info[i]);
540 kfree(helper->connector_info);
Sascha Hauera1b77362012-02-01 11:38:22 +0100541 for (i = 0; i < helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000542 kfree(helper->crtc_info[i].mode_set.connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100543 if (helper->crtc_info[i].mode_set.mode)
544 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
545 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000546 kfree(helper->crtc_info);
547}
548
Daniel Vetter207fd322013-01-20 22:13:14 +0100549/**
Thierry Reding10a23102014-06-27 17:19:24 +0200550 * drm_fb_helper_prepare - setup a drm_fb_helper structure
551 * @dev: DRM device
552 * @helper: driver-allocated fbdev helper structure to set up
553 * @funcs: pointer to structure of functions associate with this helper
554 *
555 * Sets up the bare minimum to make the framebuffer helper usable. This is
556 * useful to implement race-free initialization of the polling helpers.
557 */
558void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
559 const struct drm_fb_helper_funcs *funcs)
560{
561 INIT_LIST_HEAD(&helper->kernel_fb_list);
562 helper->funcs = funcs;
563 helper->dev = dev;
564}
565EXPORT_SYMBOL(drm_fb_helper_prepare);
566
567/**
Daniel Vetter207fd322013-01-20 22:13:14 +0100568 * drm_fb_helper_init - initialize a drm_fb_helper structure
569 * @dev: drm device
570 * @fb_helper: driver-allocated fbdev helper structure to initialize
571 * @crtc_count: maximum number of crtcs to support in this fbdev emulation
572 * @max_conn_count: max connector count
573 *
574 * This allocates the structures for the fbdev helper with the given limits.
575 * Note that this won't yet touch the hardware (through the driver interfaces)
576 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
577 * to allow driver writes more control over the exact init sequence.
578 *
Thierry Reding10a23102014-06-27 17:19:24 +0200579 * Drivers must call drm_fb_helper_prepare() before calling this function.
Daniel Vetter207fd322013-01-20 22:13:14 +0100580 *
581 * RETURNS:
582 * Zero if everything went ok, nonzero otherwise.
583 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000584int drm_fb_helper_init(struct drm_device *dev,
585 struct drm_fb_helper *fb_helper,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000586 int crtc_count, int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000587{
Dave Airlie785b93e2009-08-28 15:46:53 +1000588 struct drm_crtc *crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000589 int i;
590
Xiubo Li04cfe972014-03-10 09:33:58 +0800591 if (!max_conn_count)
592 return -EINVAL;
593
Dave Airlie4abe3522010-03-30 05:34:18 +0000594 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
595 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000596 return -ENOMEM;
597
Dave Airlie4abe3522010-03-30 05:34:18 +0000598 fb_helper->crtc_count = crtc_count;
599 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
600 if (!fb_helper->connector_info) {
601 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000602 return -ENOMEM;
603 }
Dave Airlie65c2a892014-06-05 14:01:30 +1000604 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
Dave Airlie4abe3522010-03-30 05:34:18 +0000605 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000606
607 for (i = 0; i < crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000608 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000609 kcalloc(max_conn_count,
610 sizeof(struct drm_connector *),
611 GFP_KERNEL);
612
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200613 if (!fb_helper->crtc_info[i].mode_set.connectors)
Dave Airlie785b93e2009-08-28 15:46:53 +1000614 goto out_free;
Dave Airlie4abe3522010-03-30 05:34:18 +0000615 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000616 }
617
618 i = 0;
619 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000620 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000621 i++;
622 }
Sascha Hauere9ad3182012-02-01 11:38:25 +0100623
Dave Airlie785b93e2009-08-28 15:46:53 +1000624 return 0;
625out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000626 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000627 return -ENOMEM;
628}
Dave Airlie4abe3522010-03-30 05:34:18 +0000629EXPORT_SYMBOL(drm_fb_helper_init);
630
631void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
632{
633 if (!list_empty(&fb_helper->kernel_fb_list)) {
634 list_del(&fb_helper->kernel_fb_list);
635 if (list_empty(&kernel_fb_helper_list)) {
Sachin Kamatd56b1b92012-11-15 03:43:29 +0000636 pr_info("drm: unregistered panic notifier\n");
Dave Airlie4abe3522010-03-30 05:34:18 +0000637 atomic_notifier_chain_unregister(&panic_notifier_list,
638 &paniced);
639 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
640 }
641 }
642
643 drm_fb_helper_crtc_free(fb_helper);
644
Dave Airlie4abe3522010-03-30 05:34:18 +0000645}
646EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000647
Dave Airliec850cb72009-10-23 18:49:03 +1000648static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000649 u16 blue, u16 regno, struct fb_info *info)
650{
651 struct drm_fb_helper *fb_helper = info->par;
652 struct drm_framebuffer *fb = fb_helper->fb;
653 int pindex;
654
Dave Airliec850cb72009-10-23 18:49:03 +1000655 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
656 u32 *palette;
657 u32 value;
658 /* place color in psuedopalette */
659 if (regno > 16)
660 return -EINVAL;
661 palette = (u32 *)info->pseudo_palette;
662 red >>= (16 - info->var.red.length);
663 green >>= (16 - info->var.green.length);
664 blue >>= (16 - info->var.blue.length);
665 value = (red << info->var.red.offset) |
666 (green << info->var.green.offset) |
667 (blue << info->var.blue.offset);
Rob Clark9da12b6a2011-02-16 02:45:51 +0000668 if (info->var.transp.length > 0) {
669 u32 mask = (1 << info->var.transp.length) - 1;
670 mask <<= info->var.transp.offset;
671 value |= mask;
672 }
Dave Airliec850cb72009-10-23 18:49:03 +1000673 palette[regno] = value;
674 return 0;
675 }
676
Ville Syrjälä04c0c562013-05-27 20:19:57 +0300677 /*
678 * The driver really shouldn't advertise pseudo/directcolor
679 * visuals if it can't deal with the palette.
680 */
681 if (WARN_ON(!fb_helper->funcs->gamma_set ||
682 !fb_helper->funcs->gamma_get))
683 return -EINVAL;
684
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000685 pindex = regno;
686
687 if (fb->bits_per_pixel == 16) {
688 pindex = regno << 3;
689
690 if (fb->depth == 16 && regno > 63)
Dave Airliec850cb72009-10-23 18:49:03 +1000691 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000692 if (fb->depth == 15 && regno > 31)
Dave Airliec850cb72009-10-23 18:49:03 +1000693 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000694
695 if (fb->depth == 16) {
696 u16 r, g, b;
697 int i;
698 if (regno < 32) {
699 for (i = 0; i < 8; i++)
700 fb_helper->funcs->gamma_set(crtc, red,
701 green, blue, pindex + i);
702 }
703
704 fb_helper->funcs->gamma_get(crtc, &r,
705 &g, &b,
706 pindex >> 1);
707
708 for (i = 0; i < 4; i++)
709 fb_helper->funcs->gamma_set(crtc, r,
710 green, b,
711 (pindex >> 1) + i);
712 }
713 }
714
715 if (fb->depth != 16)
716 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
Dave Airliec850cb72009-10-23 18:49:03 +1000717 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000718}
719
Daniel Vetter207fd322013-01-20 22:13:14 +0100720/**
721 * drm_fb_helper_setcmap - implementation for ->fb_setcmap
722 * @cmap: cmap to set
723 * @info: fbdev registered by the helper
724 */
Dave Airlie068143d2009-10-05 09:58:02 +1000725int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
726{
727 struct drm_fb_helper *fb_helper = info->par;
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300728 struct drm_device *dev = fb_helper->dev;
Dave Airlie8be48d92010-03-30 05:34:14 +0000729 struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +1000730 u16 *red, *green, *blue, *transp;
731 struct drm_crtc *crtc;
roel062ac622011-03-07 18:00:34 +0100732 int i, j, rc = 0;
Dave Airlie068143d2009-10-05 09:58:02 +1000733 int start;
734
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300735 drm_modeset_lock_all(dev);
736 if (!drm_fb_helper_is_bound(fb_helper)) {
737 drm_modeset_unlock_all(dev);
738 return -EBUSY;
739 }
740
Dave Airlie8be48d92010-03-30 05:34:14 +0000741 for (i = 0; i < fb_helper->crtc_count; i++) {
742 crtc = fb_helper->crtc_info[i].mode_set.crtc;
743 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +1000744
745 red = cmap->red;
746 green = cmap->green;
747 blue = cmap->blue;
748 transp = cmap->transp;
749 start = cmap->start;
750
roel062ac622011-03-07 18:00:34 +0100751 for (j = 0; j < cmap->len; j++) {
Dave Airlie068143d2009-10-05 09:58:02 +1000752 u16 hred, hgreen, hblue, htransp = 0xffff;
753
754 hred = *red++;
755 hgreen = *green++;
756 hblue = *blue++;
757
758 if (transp)
759 htransp = *transp++;
760
Dave Airliec850cb72009-10-23 18:49:03 +1000761 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
762 if (rc)
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300763 goto out;
Dave Airlie068143d2009-10-05 09:58:02 +1000764 }
Ville Syrjälä04c0c562013-05-27 20:19:57 +0300765 if (crtc_funcs->load_lut)
766 crtc_funcs->load_lut(crtc);
Dave Airlie068143d2009-10-05 09:58:02 +1000767 }
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300768 out:
769 drm_modeset_unlock_all(dev);
Dave Airlie068143d2009-10-05 09:58:02 +1000770 return rc;
771}
772EXPORT_SYMBOL(drm_fb_helper_setcmap);
773
Daniel Vetter207fd322013-01-20 22:13:14 +0100774/**
775 * drm_fb_helper_check_var - implementation for ->fb_check_var
776 * @var: screeninfo to check
777 * @info: fbdev registered by the helper
778 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000779int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
780 struct fb_info *info)
781{
782 struct drm_fb_helper *fb_helper = info->par;
783 struct drm_framebuffer *fb = fb_helper->fb;
784 int depth;
785
Jason Wesself90ebd92010-08-05 09:22:32 -0500786 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +1000787 return -EINVAL;
788
789 /* Need to resize the fb object !!! */
Chris Wilson62fb3762012-03-26 21:15:53 +0100790 if (var->bits_per_pixel > fb->bits_per_pixel ||
791 var->xres > fb->width || var->yres > fb->height ||
792 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
Dave Airlie509c7d82010-01-08 09:27:08 +1000793 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
Chris Wilson62fb3762012-03-26 21:15:53 +0100794 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
795 var->xres, var->yres, var->bits_per_pixel,
796 var->xres_virtual, var->yres_virtual,
Dave Airlie509c7d82010-01-08 09:27:08 +1000797 fb->width, fb->height, fb->bits_per_pixel);
Dave Airlie785b93e2009-08-28 15:46:53 +1000798 return -EINVAL;
799 }
800
801 switch (var->bits_per_pixel) {
802 case 16:
803 depth = (var->green.length == 6) ? 16 : 15;
804 break;
805 case 32:
806 depth = (var->transp.length > 0) ? 32 : 24;
807 break;
808 default:
809 depth = var->bits_per_pixel;
810 break;
811 }
812
813 switch (depth) {
814 case 8:
815 var->red.offset = 0;
816 var->green.offset = 0;
817 var->blue.offset = 0;
818 var->red.length = 8;
819 var->green.length = 8;
820 var->blue.length = 8;
821 var->transp.length = 0;
822 var->transp.offset = 0;
823 break;
824 case 15:
825 var->red.offset = 10;
826 var->green.offset = 5;
827 var->blue.offset = 0;
828 var->red.length = 5;
829 var->green.length = 5;
830 var->blue.length = 5;
831 var->transp.length = 1;
832 var->transp.offset = 15;
833 break;
834 case 16:
835 var->red.offset = 11;
836 var->green.offset = 5;
837 var->blue.offset = 0;
838 var->red.length = 5;
839 var->green.length = 6;
840 var->blue.length = 5;
841 var->transp.length = 0;
842 var->transp.offset = 0;
843 break;
844 case 24:
845 var->red.offset = 16;
846 var->green.offset = 8;
847 var->blue.offset = 0;
848 var->red.length = 8;
849 var->green.length = 8;
850 var->blue.length = 8;
851 var->transp.length = 0;
852 var->transp.offset = 0;
853 break;
854 case 32:
855 var->red.offset = 16;
856 var->green.offset = 8;
857 var->blue.offset = 0;
858 var->red.length = 8;
859 var->green.length = 8;
860 var->blue.length = 8;
861 var->transp.length = 8;
862 var->transp.offset = 24;
863 break;
864 default:
865 return -EINVAL;
866 }
867 return 0;
868}
869EXPORT_SYMBOL(drm_fb_helper_check_var);
870
Daniel Vetter207fd322013-01-20 22:13:14 +0100871/**
872 * drm_fb_helper_set_par - implementation for ->fb_set_par
873 * @info: fbdev registered by the helper
874 *
875 * This will let fbcon do the mode init and is called at initialization time by
876 * the fbdev core when registering the driver, and later on through the hotplug
877 * callback.
878 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000879int drm_fb_helper_set_par(struct fb_info *info)
880{
881 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +1000882 struct fb_var_screeninfo *var = &info->var;
Dave Airlie785b93e2009-08-28 15:46:53 +1000883
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100884 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +1000885 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000886 return -EINVAL;
887 }
888
Rob Clark5ea1f752014-05-30 12:29:48 -0400889 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +0000890
891 if (fb_helper->delayed_hotplug) {
892 fb_helper->delayed_hotplug = false;
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000893 drm_fb_helper_hotplug_event(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +0000894 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000895 return 0;
896}
897EXPORT_SYMBOL(drm_fb_helper_set_par);
898
Daniel Vetter207fd322013-01-20 22:13:14 +0100899/**
900 * drm_fb_helper_pan_display - implementation for ->fb_pan_display
901 * @var: updated screen information
902 * @info: fbdev registered by the helper
903 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000904int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
905 struct fb_info *info)
906{
907 struct drm_fb_helper *fb_helper = info->par;
908 struct drm_device *dev = fb_helper->dev;
909 struct drm_mode_set *modeset;
Dave Airlie785b93e2009-08-28 15:46:53 +1000910 int ret = 0;
911 int i;
912
Daniel Vetter84849902012-12-02 00:28:11 +0100913 drm_modeset_lock_all(dev);
Daniel Vetter20c60c32012-12-17 12:13:23 +0100914 if (!drm_fb_helper_is_bound(fb_helper)) {
915 drm_modeset_unlock_all(dev);
916 return -EBUSY;
917 }
918
Dave Airlie8be48d92010-03-30 05:34:14 +0000919 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000920 modeset = &fb_helper->crtc_info[i].mode_set;
921
922 modeset->x = var->xoffset;
923 modeset->y = var->yoffset;
924
925 if (modeset->num_connectors) {
Daniel Vetter2d13b672012-12-11 13:47:23 +0100926 ret = drm_mode_set_config_internal(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +1000927 if (!ret) {
928 info->var.xoffset = var->xoffset;
929 info->var.yoffset = var->yoffset;
930 }
931 }
932 }
Daniel Vetter84849902012-12-02 00:28:11 +0100933 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000934 return ret;
935}
936EXPORT_SYMBOL(drm_fb_helper_pan_display);
937
Daniel Vetter8acf6582013-01-21 23:38:37 +0100938/*
Daniel Vetter207fd322013-01-20 22:13:14 +0100939 * Allocates the backing storage and sets up the fbdev info structure through
940 * the ->fb_probe callback and then registers the fbdev and sets up the panic
941 * notifier.
Daniel Vetter8acf6582013-01-21 23:38:37 +0100942 */
Daniel Vetterde1ace52013-01-20 21:50:49 +0100943static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
944 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +1000945{
Daniel Vetter8acf6582013-01-21 23:38:37 +0100946 int ret = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000947 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +0000948 int i;
Dave Airlie785b93e2009-08-28 15:46:53 +1000949 struct fb_info *info;
Dave Airlie38651672010-03-30 05:34:13 +0000950 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +0000951 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +0000952
953 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
954 sizes.surface_depth = 24;
955 sizes.surface_bpp = 32;
956 sizes.fb_width = (unsigned)-1;
957 sizes.fb_height = (unsigned)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +1000958
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000959 /* if driver picks 8 or 16 by default use that
960 for both depth/bpp */
Sachin Kamat96081cd2012-11-15 03:43:30 +0000961 if (preferred_bpp != sizes.surface_bpp)
Dave Airlie38651672010-03-30 05:34:13 +0000962 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Sachin Kamat96081cd2012-11-15 03:43:30 +0000963
Dave Airlie785b93e2009-08-28 15:46:53 +1000964 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000965 for (i = 0; i < fb_helper->connector_count; i++) {
966 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +0100967 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +1000968
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200969 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +1000970
971 if (cmdline_mode->bpp_specified) {
972 switch (cmdline_mode->bpp) {
973 case 8:
Dave Airlie38651672010-03-30 05:34:13 +0000974 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +1000975 break;
976 case 15:
Dave Airlie38651672010-03-30 05:34:13 +0000977 sizes.surface_depth = 15;
978 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000979 break;
980 case 16:
Dave Airlie38651672010-03-30 05:34:13 +0000981 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000982 break;
983 case 24:
Dave Airlie38651672010-03-30 05:34:13 +0000984 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +1000985 break;
986 case 32:
Dave Airlie38651672010-03-30 05:34:13 +0000987 sizes.surface_depth = 24;
988 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +1000989 break;
990 }
991 break;
992 }
993 }
994
Dave Airlie8be48d92010-03-30 05:34:14 +0000995 crtc_count = 0;
996 for (i = 0; i < fb_helper->crtc_count; i++) {
997 struct drm_display_mode *desired_mode;
998 desired_mode = fb_helper->crtc_info[i].desired_mode;
Dave Airlie785b93e2009-08-28 15:46:53 +1000999
Dave Airlie8be48d92010-03-30 05:34:14 +00001000 if (desired_mode) {
1001 if (gamma_size == 0)
1002 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1003 if (desired_mode->hdisplay < sizes.fb_width)
1004 sizes.fb_width = desired_mode->hdisplay;
1005 if (desired_mode->vdisplay < sizes.fb_height)
1006 sizes.fb_height = desired_mode->vdisplay;
1007 if (desired_mode->hdisplay > sizes.surface_width)
1008 sizes.surface_width = desired_mode->hdisplay;
1009 if (desired_mode->vdisplay > sizes.surface_height)
1010 sizes.surface_height = desired_mode->vdisplay;
Dave Airlie785b93e2009-08-28 15:46:53 +10001011 crtc_count++;
1012 }
1013 }
1014
Dave Airlie38651672010-03-30 05:34:13 +00001015 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001016 /* hmm everyone went away - assume VGA cable just fell out
1017 and will come back later. */
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001018 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +00001019 sizes.fb_width = sizes.surface_width = 1024;
1020 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +10001021 }
1022
Dave Airlie38651672010-03-30 05:34:13 +00001023 /* push down into drivers */
Daniel Vetter8acf6582013-01-21 23:38:37 +01001024 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1025 if (ret < 0)
1026 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +10001027
Dave Airlie38651672010-03-30 05:34:13 +00001028 info = fb_helper->fbdev;
Dave Airlie785b93e2009-08-28 15:46:53 +10001029
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001030 /*
1031 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1032 * events, but at init time drm_setup_crtcs needs to be called before
1033 * the fb is allocated (since we need to figure out the desired size of
1034 * the fb before we can allocate it ...). Hence we need to fix things up
1035 * here again.
1036 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001037 for (i = 0; i < fb_helper->crtc_count; i++)
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001038 if (fb_helper->crtc_info[i].mode_set.num_connectors)
1039 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1040
Dave Airlie785b93e2009-08-28 15:46:53 +10001041
Daniel Vetter8acf6582013-01-21 23:38:37 +01001042 info->var.pixclock = 0;
1043 if (register_framebuffer(info) < 0)
1044 return -EINVAL;
Dave Airlie38651672010-03-30 05:34:13 +00001045
Daniel Vetter8acf6582013-01-21 23:38:37 +01001046 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
1047 info->node, info->fix.id);
Dave Airlie785b93e2009-08-28 15:46:53 +10001048
1049 /* Switch back to kernel console on panic */
1050 /* multi card linked list maybe */
1051 if (list_empty(&kernel_fb_helper_list)) {
Sachin Kamatd56b1b92012-11-15 03:43:29 +00001052 dev_info(fb_helper->dev->dev, "registered panic notifier\n");
Dave Airlie785b93e2009-08-28 15:46:53 +10001053 atomic_notifier_chain_register(&panic_notifier_list,
1054 &paniced);
1055 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
1056 }
Daniel Vetter8acf6582013-01-21 23:38:37 +01001057
1058 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
Dave Airlie38651672010-03-30 05:34:13 +00001059
Dave Airlie785b93e2009-08-28 15:46:53 +10001060 return 0;
1061}
Dave Airlie785b93e2009-08-28 15:46:53 +10001062
Daniel Vetter207fd322013-01-20 22:13:14 +01001063/**
1064 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1065 * @info: fbdev registered by the helper
1066 * @pitch: desired pitch
1067 * @depth: desired depth
1068 *
1069 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1070 * fbdev emulations. Drivers which support acceleration methods which impose
1071 * additional constraints need to set up their own limits.
1072 *
1073 * Drivers should call this (or their equivalent setup code) from their
1074 * ->fb_probe callback.
1075 */
Dave Airlie3632ef82011-01-15 09:27:00 +10001076void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1077 uint32_t depth)
1078{
1079 info->fix.type = FB_TYPE_PACKED_PIXELS;
1080 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1081 FB_VISUAL_TRUECOLOR;
1082 info->fix.mmio_start = 0;
1083 info->fix.mmio_len = 0;
1084 info->fix.type_aux = 0;
1085 info->fix.xpanstep = 1; /* doing it in hw */
1086 info->fix.ypanstep = 1; /* doing it in hw */
1087 info->fix.ywrapstep = 0;
1088 info->fix.accel = FB_ACCEL_NONE;
Dave Airlie3632ef82011-01-15 09:27:00 +10001089
1090 info->fix.line_length = pitch;
1091 return;
1092}
1093EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1094
Daniel Vetter207fd322013-01-20 22:13:14 +01001095/**
1096 * drm_fb_helper_fill_var - initalizes variable fbdev information
1097 * @info: fbdev instance to set up
1098 * @fb_helper: fb helper instance to use as template
1099 * @fb_width: desired fb width
1100 * @fb_height: desired fb height
1101 *
1102 * Sets up the variable fbdev metainformation from the given fb helper instance
1103 * and the drm framebuffer allocated in fb_helper->fb.
1104 *
1105 * Drivers should call this (or their equivalent setup code) from their
1106 * ->fb_probe callback after having allocated the fbdev backing
1107 * storage framebuffer.
1108 */
Dave Airlie38651672010-03-30 05:34:13 +00001109void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +10001110 uint32_t fb_width, uint32_t fb_height)
1111{
Dave Airlie38651672010-03-30 05:34:13 +00001112 struct drm_framebuffer *fb = fb_helper->fb;
1113 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +10001114 info->var.xres_virtual = fb->width;
1115 info->var.yres_virtual = fb->height;
1116 info->var.bits_per_pixel = fb->bits_per_pixel;
James Simmons57084d02010-12-20 19:10:39 +00001117 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +10001118 info->var.xoffset = 0;
1119 info->var.yoffset = 0;
1120 info->var.activate = FB_ACTIVATE_NOW;
1121 info->var.height = -1;
1122 info->var.width = -1;
1123
1124 switch (fb->depth) {
1125 case 8:
1126 info->var.red.offset = 0;
1127 info->var.green.offset = 0;
1128 info->var.blue.offset = 0;
1129 info->var.red.length = 8; /* 8bit DAC */
1130 info->var.green.length = 8;
1131 info->var.blue.length = 8;
1132 info->var.transp.offset = 0;
1133 info->var.transp.length = 0;
1134 break;
1135 case 15:
1136 info->var.red.offset = 10;
1137 info->var.green.offset = 5;
1138 info->var.blue.offset = 0;
1139 info->var.red.length = 5;
1140 info->var.green.length = 5;
1141 info->var.blue.length = 5;
1142 info->var.transp.offset = 15;
1143 info->var.transp.length = 1;
1144 break;
1145 case 16:
1146 info->var.red.offset = 11;
1147 info->var.green.offset = 5;
1148 info->var.blue.offset = 0;
1149 info->var.red.length = 5;
1150 info->var.green.length = 6;
1151 info->var.blue.length = 5;
1152 info->var.transp.offset = 0;
1153 break;
1154 case 24:
1155 info->var.red.offset = 16;
1156 info->var.green.offset = 8;
1157 info->var.blue.offset = 0;
1158 info->var.red.length = 8;
1159 info->var.green.length = 8;
1160 info->var.blue.length = 8;
1161 info->var.transp.offset = 0;
1162 info->var.transp.length = 0;
1163 break;
1164 case 32:
1165 info->var.red.offset = 16;
1166 info->var.green.offset = 8;
1167 info->var.blue.offset = 0;
1168 info->var.red.length = 8;
1169 info->var.green.length = 8;
1170 info->var.blue.length = 8;
1171 info->var.transp.offset = 24;
1172 info->var.transp.length = 8;
1173 break;
1174 default:
1175 break;
1176 }
1177
1178 info->var.xres = fb_width;
1179 info->var.yres = fb_height;
1180}
1181EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +00001182
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001183static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1184 uint32_t maxX,
1185 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +00001186{
1187 struct drm_connector *connector;
1188 int count = 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001189 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001190
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001191 for (i = 0; i < fb_helper->connector_count; i++) {
1192 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001193 count += connector->funcs->fill_modes(connector, maxX, maxY);
1194 }
1195
1196 return count;
1197}
1198
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001199struct 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 +00001200{
1201 struct drm_display_mode *mode;
1202
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001203 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Daniel Vetter9d3de132014-01-23 16:27:56 +01001204 if (mode->hdisplay > width ||
1205 mode->vdisplay > height)
Dave Airlie38651672010-03-30 05:34:13 +00001206 continue;
1207 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1208 return mode;
1209 }
1210 return NULL;
1211}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001212EXPORT_SYMBOL(drm_has_preferred_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001213
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001214static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00001215{
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001216 return fb_connector->connector->cmdline_mode.specified;
Dave Airlie38651672010-03-30 05:34:13 +00001217}
1218
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001219struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001220 int width, int height)
Dave Airlie38651672010-03-30 05:34:13 +00001221{
Chris Wilson1794d252011-04-17 07:43:32 +01001222 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001223 struct drm_display_mode *mode = NULL;
Takashi Iwaic683f422014-03-19 14:53:13 +01001224 bool prefer_non_interlace;
Dave Airlie38651672010-03-30 05:34:13 +00001225
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001226 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001227 if (cmdline_mode->specified == false)
1228 return mode;
1229
1230 /* attempt to find a matching mode in the list of modes
1231 * we have gotten so far, if not add a CVT mode that conforms
1232 */
1233 if (cmdline_mode->rb || cmdline_mode->margins)
1234 goto create_mode;
1235
Takashi Iwaic683f422014-03-19 14:53:13 +01001236 prefer_non_interlace = !cmdline_mode->interlace;
1237 again:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001238 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001239 /* check width/height */
1240 if (mode->hdisplay != cmdline_mode->xres ||
1241 mode->vdisplay != cmdline_mode->yres)
1242 continue;
1243
1244 if (cmdline_mode->refresh_specified) {
1245 if (mode->vrefresh != cmdline_mode->refresh)
1246 continue;
1247 }
1248
1249 if (cmdline_mode->interlace) {
1250 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1251 continue;
Takashi Iwaic683f422014-03-19 14:53:13 +01001252 } else if (prefer_non_interlace) {
1253 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1254 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001255 }
1256 return mode;
1257 }
1258
Takashi Iwaic683f422014-03-19 14:53:13 +01001259 if (prefer_non_interlace) {
1260 prefer_non_interlace = false;
1261 goto again;
1262 }
1263
Dave Airlie38651672010-03-30 05:34:13 +00001264create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +01001265 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1266 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001267 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00001268 return mode;
1269}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001270EXPORT_SYMBOL(drm_pick_cmdline_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001271
1272static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1273{
1274 bool enable;
1275
Sachin Kamat96081cd2012-11-15 03:43:30 +00001276 if (strict)
Dave Airlie38651672010-03-30 05:34:13 +00001277 enable = connector->status == connector_status_connected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001278 else
Dave Airlie38651672010-03-30 05:34:13 +00001279 enable = connector->status != connector_status_disconnected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001280
Dave Airlie38651672010-03-30 05:34:13 +00001281 return enable;
1282}
1283
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001284static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1285 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001286{
1287 bool any_enabled = false;
1288 struct drm_connector *connector;
1289 int i = 0;
1290
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001291 for (i = 0; i < fb_helper->connector_count; i++) {
1292 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001293 enabled[i] = drm_connector_enabled(connector, true);
1294 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1295 enabled[i] ? "yes" : "no");
1296 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001297 }
1298
1299 if (any_enabled)
1300 return;
1301
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001302 for (i = 0; i < fb_helper->connector_count; i++) {
1303 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001304 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001305 }
1306}
1307
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001308static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1309 struct drm_display_mode **modes,
1310 bool *enabled, int width, int height)
1311{
1312 int count, i, j;
1313 bool can_clone = false;
1314 struct drm_fb_helper_connector *fb_helper_conn;
1315 struct drm_display_mode *dmt_mode, *mode;
1316
1317 /* only contemplate cloning in the single crtc case */
1318 if (fb_helper->crtc_count > 1)
1319 return false;
1320
1321 count = 0;
1322 for (i = 0; i < fb_helper->connector_count; i++) {
1323 if (enabled[i])
1324 count++;
1325 }
1326
1327 /* only contemplate cloning if more than one connector is enabled */
1328 if (count <= 1)
1329 return false;
1330
1331 /* check the command line or if nothing common pick 1024x768 */
1332 can_clone = true;
1333 for (i = 0; i < fb_helper->connector_count; i++) {
1334 if (!enabled[i])
1335 continue;
1336 fb_helper_conn = fb_helper->connector_info[i];
1337 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1338 if (!modes[i]) {
1339 can_clone = false;
1340 break;
1341 }
1342 for (j = 0; j < i; j++) {
1343 if (!enabled[j])
1344 continue;
1345 if (!drm_mode_equal(modes[j], modes[i]))
1346 can_clone = false;
1347 }
1348 }
1349
1350 if (can_clone) {
1351 DRM_DEBUG_KMS("can clone using command line\n");
1352 return true;
1353 }
1354
1355 /* try and find a 1024x768 mode on each connector */
1356 can_clone = true;
Adam Jacksonf6e252b2012-04-13 16:33:31 -04001357 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001358
1359 for (i = 0; i < fb_helper->connector_count; i++) {
1360
1361 if (!enabled[i])
1362 continue;
1363
1364 fb_helper_conn = fb_helper->connector_info[i];
1365 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1366 if (drm_mode_equal(mode, dmt_mode))
1367 modes[i] = mode;
1368 }
1369 if (!modes[i])
1370 can_clone = false;
1371 }
1372
1373 if (can_clone) {
1374 DRM_DEBUG_KMS("can clone using 1024x768\n");
1375 return true;
1376 }
1377 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1378 return false;
1379}
1380
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001381static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00001382 struct drm_display_mode **modes,
1383 bool *enabled, int width, int height)
1384{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001385 struct drm_fb_helper_connector *fb_helper_conn;
1386 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001387
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001388 for (i = 0; i < fb_helper->connector_count; i++) {
1389 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00001390
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001391 if (enabled[i] == false)
Dave Airlie38651672010-03-30 05:34:13 +00001392 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001393
1394 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001395 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00001396
1397 /* got for command line mode first */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001398 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001399 if (!modes[i]) {
1400 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001401 fb_helper_conn->connector->base.id);
1402 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001403 }
1404 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001405 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1406 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00001407 break;
1408 }
1409 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1410 "none");
Dave Airlie38651672010-03-30 05:34:13 +00001411 }
1412 return true;
1413}
1414
Dave Airlie8be48d92010-03-30 05:34:14 +00001415static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1416 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00001417 struct drm_display_mode **modes,
1418 int n, int width, int height)
1419{
1420 int c, o;
Dave Airlie8be48d92010-03-30 05:34:14 +00001421 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001422 struct drm_connector *connector;
1423 struct drm_connector_helper_funcs *connector_funcs;
1424 struct drm_encoder *encoder;
Dave Airlie38651672010-03-30 05:34:13 +00001425 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00001426 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001427 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00001428
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001429 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00001430 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001431
1432 fb_helper_conn = fb_helper->connector_info[n];
1433 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001434
1435 best_crtcs[n] = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001436 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001437 if (modes[n] == NULL)
1438 return best_score;
1439
Dave Airlie8be48d92010-03-30 05:34:14 +00001440 crtcs = kzalloc(dev->mode_config.num_connector *
1441 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001442 if (!crtcs)
1443 return best_score;
1444
1445 my_score = 1;
1446 if (connector->status == connector_status_connected)
1447 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001448 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00001449 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001450 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00001451 my_score++;
1452
1453 connector_funcs = connector->helper_private;
1454 encoder = connector_funcs->best_encoder(connector);
1455 if (!encoder)
1456 goto out;
1457
Dave Airlie38651672010-03-30 05:34:13 +00001458 /* select a crtc for this connector and then attempt to configure
1459 remaining connectors */
Dave Airlie8be48d92010-03-30 05:34:14 +00001460 for (c = 0; c < fb_helper->crtc_count; c++) {
1461 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00001462
Sachin Kamat96081cd2012-11-15 03:43:30 +00001463 if ((encoder->possible_crtcs & (1 << c)) == 0)
Dave Airlie38651672010-03-30 05:34:13 +00001464 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001465
1466 for (o = 0; o < n; o++)
1467 if (best_crtcs[o] == crtc)
1468 break;
1469
1470 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001471 /* ignore cloning unless only a single crtc */
1472 if (fb_helper->crtc_count > 1)
1473 continue;
1474
1475 if (!drm_mode_equal(modes[o], modes[n]))
1476 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001477 }
1478
1479 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00001480 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1481 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00001482 width, height);
1483 if (score > best_score) {
Dave Airlie38651672010-03-30 05:34:13 +00001484 best_score = score;
1485 memcpy(best_crtcs, crtcs,
1486 dev->mode_config.num_connector *
Dave Airlie8be48d92010-03-30 05:34:14 +00001487 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00001488 }
Dave Airlie38651672010-03-30 05:34:13 +00001489 }
1490out:
1491 kfree(crtcs);
1492 return best_score;
1493}
1494
Dave Airlie8be48d92010-03-30 05:34:14 +00001495static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001496{
Dave Airlie8be48d92010-03-30 05:34:14 +00001497 struct drm_device *dev = fb_helper->dev;
1498 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00001499 struct drm_display_mode **modes;
Dave Airlie8be48d92010-03-30 05:34:14 +00001500 struct drm_mode_set *modeset;
Dave Airlie38651672010-03-30 05:34:13 +00001501 bool *enabled;
1502 int width, height;
Jesse Barnes11e17a02013-02-19 13:31:39 -08001503 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001504
1505 DRM_DEBUG_KMS("\n");
1506
1507 width = dev->mode_config.max_width;
1508 height = dev->mode_config.max_height;
1509
Dave Airlie38651672010-03-30 05:34:13 +00001510 crtcs = kcalloc(dev->mode_config.num_connector,
Dave Airlie8be48d92010-03-30 05:34:14 +00001511 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001512 modes = kcalloc(dev->mode_config.num_connector,
1513 sizeof(struct drm_display_mode *), GFP_KERNEL);
1514 enabled = kcalloc(dev->mode_config.num_connector,
1515 sizeof(bool), GFP_KERNEL);
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00001516 if (!crtcs || !modes || !enabled) {
1517 DRM_ERROR("Memory allocation failed\n");
1518 goto out;
1519 }
1520
Dave Airlie38651672010-03-30 05:34:13 +00001521
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001522 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00001523
Jesse Barnes11e17a02013-02-19 13:31:39 -08001524 if (!(fb_helper->funcs->initial_config &&
1525 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
1526 enabled, width, height))) {
1527 memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0]));
1528 memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0]));
1529
1530 if (!drm_target_cloned(fb_helper,
1531 modes, enabled, width, height) &&
1532 !drm_target_preferred(fb_helper,
1533 modes, enabled, width, height))
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001534 DRM_ERROR("Unable to find initial modes\n");
Jesse Barnes11e17a02013-02-19 13:31:39 -08001535
1536 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
1537 width, height);
1538
1539 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001540 }
Dave Airlie38651672010-03-30 05:34:13 +00001541
Dave Airlie8be48d92010-03-30 05:34:14 +00001542 /* need to set the modesets up here for use later */
1543 /* fill out the connector<->crtc mappings into the modesets */
1544 for (i = 0; i < fb_helper->crtc_count; i++) {
1545 modeset = &fb_helper->crtc_info[i].mode_set;
1546 modeset->num_connectors = 0;
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001547 modeset->fb = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001548 }
Dave Airlie38651672010-03-30 05:34:13 +00001549
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001550 for (i = 0; i < fb_helper->connector_count; i++) {
Dave Airlie38651672010-03-30 05:34:13 +00001551 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001552 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1553 modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00001554
Dave Airlie8be48d92010-03-30 05:34:14 +00001555 if (mode && fb_crtc) {
Dave Airlie38651672010-03-30 05:34:13 +00001556 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
Dave Airlie8be48d92010-03-30 05:34:14 +00001557 mode->name, fb_crtc->mode_set.crtc->base.id);
1558 fb_crtc->desired_mode = mode;
1559 if (modeset->mode)
1560 drm_mode_destroy(dev, modeset->mode);
1561 modeset->mode = drm_mode_duplicate(dev,
1562 fb_crtc->desired_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001563 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001564 modeset->fb = fb_helper->fb;
Dave Airlie38651672010-03-30 05:34:13 +00001565 }
Dave Airlie38651672010-03-30 05:34:13 +00001566 }
1567
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001568 /* Clear out any old modes if there are no more connected outputs. */
1569 for (i = 0; i < fb_helper->crtc_count; i++) {
1570 modeset = &fb_helper->crtc_info[i].mode_set;
1571 if (modeset->num_connectors == 0) {
1572 BUG_ON(modeset->fb);
1573 BUG_ON(modeset->num_connectors);
1574 if (modeset->mode)
1575 drm_mode_destroy(dev, modeset->mode);
1576 modeset->mode = NULL;
1577 }
1578 }
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00001579out:
Dave Airlie38651672010-03-30 05:34:13 +00001580 kfree(crtcs);
1581 kfree(modes);
1582 kfree(enabled);
1583}
1584
1585/**
Daniel Vetter207fd322013-01-20 22:13:14 +01001586 * drm_fb_helper_initial_config - setup a sane initial connector configuration
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001587 * @fb_helper: fb_helper device struct
1588 * @bpp_sel: bpp value to use for the framebuffer configuration
Dave Airlie38651672010-03-30 05:34:13 +00001589 *
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001590 * Scans the CRTCs and connectors and tries to put together an initial setup.
Dave Airlie38651672010-03-30 05:34:13 +00001591 * At the moment, this is a cloned configuration across all heads with
1592 * a new framebuffer object as the backing store.
1593 *
Daniel Vetter207fd322013-01-20 22:13:14 +01001594 * Note that this also registers the fbdev and so allows userspace to call into
1595 * the driver through the fbdev interfaces.
1596 *
1597 * This function will call down into the ->fb_probe callback to let
1598 * the driver allocate and initialize the fbdev info structure and the drm
1599 * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
1600 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
1601 * values for the fbdev info structure.
1602 *
Dave Airlie38651672010-03-30 05:34:13 +00001603 * RETURNS:
1604 * Zero if everything went ok, nonzero otherwise.
1605 */
Dave Airlie4abe3522010-03-30 05:34:18 +00001606bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00001607{
Dave Airlie8be48d92010-03-30 05:34:14 +00001608 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001609 int count = 0;
1610
Daniel Vetter53f19042014-03-20 14:26:35 +01001611 mutex_lock(&dev->mode_config.mutex);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001612 count = drm_fb_helper_probe_connector_modes(fb_helper,
1613 dev->mode_config.max_width,
1614 dev->mode_config.max_height);
Daniel Vetter53f19042014-03-20 14:26:35 +01001615 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie38651672010-03-30 05:34:13 +00001616 /*
1617 * we shouldn't end up with no modes here.
1618 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001619 if (count == 0)
Sachin Kamatd56b1b92012-11-15 03:43:29 +00001620 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
Sachin Kamat96081cd2012-11-15 03:43:30 +00001621
Dave Airlie8be48d92010-03-30 05:34:14 +00001622 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001623
Dave Airlie4abe3522010-03-30 05:34:18 +00001624 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001625}
Dave Airlie8be48d92010-03-30 05:34:14 +00001626EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00001627
Chris Wilson73943712011-04-22 11:03:57 +01001628/**
1629 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001630 * probing all the outputs attached to the fb
Chris Wilson73943712011-04-22 11:03:57 +01001631 * @fb_helper: the drm_fb_helper
1632 *
Chris Wilson73943712011-04-22 11:03:57 +01001633 * Scan the connectors attached to the fb_helper and try to put together a
1634 * setup after *notification of a change in output configuration.
1635 *
Daniel Vetter207fd322013-01-20 22:13:14 +01001636 * Called at runtime, takes the mode config locks to be able to check/change the
1637 * modeset configuration. Must be run from process context (which usually means
1638 * either the output polling work or a work item launched from the driver's
1639 * hotplug interrupt).
1640 *
Daniel Vetter50c3dc92014-06-27 17:19:22 +02001641 * Note that drivers may call this even before calling
1642 * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows
1643 * for a race-free fbcon setup and will make sure that the fbdev emulation will
1644 * not miss any hotplug events.
Daniel Vetter207fd322013-01-20 22:13:14 +01001645 *
Chris Wilson73943712011-04-22 11:03:57 +01001646 * RETURNS:
1647 * 0 on success and a non-zero error code otherwise.
1648 */
1649int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001650{
Chris Wilson73943712011-04-22 11:03:57 +01001651 struct drm_device *dev = fb_helper->dev;
Lespiau, Damien51bbd272013-09-28 16:24:05 +01001652 u32 max_width, max_height;
Dave Airlie4abe3522010-03-30 05:34:18 +00001653
Daniel Vetter89ced122013-04-11 14:26:55 +00001654 mutex_lock(&fb_helper->dev->mode_config.mutex);
Daniel Vetter50c3dc92014-06-27 17:19:22 +02001655 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001656 fb_helper->delayed_hotplug = true;
Daniel Vetter89ced122013-04-11 14:26:55 +00001657 mutex_unlock(&fb_helper->dev->mode_config.mutex);
Chris Wilson73943712011-04-22 11:03:57 +01001658 return 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001659 }
Dave Airlie38651672010-03-30 05:34:13 +00001660 DRM_DEBUG_KMS("\n");
1661
Dave Airlie4abe3522010-03-30 05:34:18 +00001662 max_width = fb_helper->fb->width;
1663 max_height = fb_helper->fb->height;
Dave Airlie4abe3522010-03-30 05:34:18 +00001664
Lespiau, Damien51bbd272013-09-28 16:24:05 +01001665 drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height);
Daniel Vetter89ced122013-04-11 14:26:55 +00001666 mutex_unlock(&fb_helper->dev->mode_config.mutex);
1667
1668 drm_modeset_lock_all(dev);
Dave Airlie8be48d92010-03-30 05:34:14 +00001669 drm_setup_crtcs(fb_helper);
Daniel Vetter84849902012-12-02 00:28:11 +01001670 drm_modeset_unlock_all(dev);
Daniel Vetter2180c3c2013-01-21 23:12:36 +01001671 drm_fb_helper_set_par(fb_helper->fbdev);
1672
1673 return 0;
Dave Airlie38651672010-03-30 05:34:13 +00001674}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001675EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00001676
David Rientjes6a108a12011-01-20 14:44:16 -08001677/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001678 * but the module doesn't depend on any fb console symbols. At least
1679 * attempt to load fbcon to avoid leaving the system without a usable console.
1680 */
David Rientjes6a108a12011-01-20 14:44:16 -08001681#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001682static int __init drm_fb_helper_modinit(void)
1683{
1684 const char *name = "fbcon";
1685 struct module *fbcon;
1686
1687 mutex_lock(&module_mutex);
1688 fbcon = find_module(name);
1689 mutex_unlock(&module_mutex);
1690
1691 if (!fbcon)
1692 request_module_nowait(name);
1693 return 0;
1694}
1695
1696module_init(drm_fb_helper_modinit);
1697#endif